OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [config/] [configuration.py] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
#! /usr/bin/env python2.6
2
# -*- mode: python; coding: utf-8; -*-
3
import os, sys, shelve, shutil, re
4
from projpaths import *
5
from lib import *
6
from caps import *
7
 
8
 
9
class Container:
10
    def __init__(self, id):
11
        self.dirname = None
12
        self.name = None
13
        self.type = None
14
        self.id = id
15
        self.pager_lma = 0
16
        self.pager_vma = 0
17
        self.pager_size = 0
18
        self.pager_rw_section_start = 0
19
        self.pager_rw_section_end = 0
20
        self.pager_rx_section_start = 0
21
        self.pager_rx_section_end = 0
22
        self.pager_task_region_start = 0
23
        self.pager_task_region_end = 0
24
        self.pager_shm_region_start = 0
25
        self.pager_shm_region_end = 0
26
        self.pager_utcb_region_start = 0
27
        self.pager_utcb_region_end = 0
28
        self.linux_zreladdr = 0
29
        self.linux_page_offset = 0
30
        self.linux_phys_offset = 0
31
        self.linux_rootfs_address = 0
32
        self.physmem = {}
33
        self.physmem["START"] = {}
34
        self.physmem["END"] = {}
35
        self.virtmem = {}
36
        self.virtmem["START"] = {}
37
        self.virtmem["END"] = {}
38
        self.caps = {}
39
        self.virt_regions = 0
40
        self.phys_regions = 0
41
 
42
    def print_self(self):
43
        print '\nContainer %d' % self.id
44
        print 'Container type: %s' % self.type
45
        print 'Container Name: %s' % self.name
46
        print 'Container Pager lma: %s' % conv_hex(self.pager_lma)
47
        print 'Container Pager vma: %s' % conv_hex(self.pager_vma)
48
        print 'Container Pager shm region start: %s' % conv_hex(self.pager_shm_region_start)
49
        print 'Container Pager shm region end: %s' % conv_hex(self.pager_shm_region_end)
50
        print 'Container Pager task region start: %s' % conv_hex(self.pager_task_region_start)
51
        print 'Container Pager task region end: %s' % conv_hex(self.pager_task_region_end)
52
        print 'Container Pager utcb region start: %s' % conv_hex(self.pager_utcb_region_start)
53
        print 'Container Pager utcb region end: %s' % conv_hex(self.pager_utcb_region_end)
54
        print 'Container Virtual regions: %s' % self.virt_regions
55
        print 'Container Physical regions: %s' % self.phys_regions
56
        print 'Container Capabilities: %s' % self.caps
57
        print '\n'
58
 
59
class configuration:
60
 
61
    def __init__(self):
62
        # Mapping between cpu and gcc flags for it.
63
        # Optimized solution to derive gcc arch flag from cpu
64
        # gcc flag here is "-march"
65
        #                          cpu          -march flag
66
        self.arch_to_gcc_flag = (['ARM926',       'armv5'],
67
                                 ['ARM1136',      'armv6'],
68
                                 ['ARM11MPCORE',  'armv6k'],
69
                                 ['CORTEXA8',     'armv7-a'],
70
                                 ['CORTEXA9',     'armv7-a'])
71
        self.arch = None
72
        self.subarch = None
73
        self.platform = None
74
        self.cpu = None
75
        self.gcc_arch_flag = None
76
        self.toolchain_userspace = None
77
        self.toolchain_kernel = None
78
        self.all = []
79
        self.smp = False
80
        self.ncpu = 0
81
        self.containers = []
82
        self.ncontainers = 0
83
 
84
    # Get all name value symbols
85
    def get_all(self, name, val):
86
        self.all.append([name, val])
87
 
88
    # Convert line to name value pair, if possible
89
    def line_to_name_value(self, line):
90
        parts = line.split()
91
        if len(parts) > 0:
92
            if parts[0] == "#define":
93
                return parts[1], parts[2]
94
        return None
95
 
96
    # Check if SMP enable, and get NCPU if SMP
97
    def get_ncpu(self, name, value):
98
        if name[:len("CONFIG_SMP")] == "CONFIG_SMP":
99
            self.smp = bool(value)
100
        if name[:len("CONFIG_NCPU")] == "CONFIG_NCPU":
101
            self.ncpu = int(value)
102
 
103
    # Extract architecture from a name value pair
104
    def get_arch(self, name, val):
105
        if name[:len("CONFIG_ARCH_")] == "CONFIG_ARCH_":
106
            parts = name.split("_", 3)
107
            self.arch = parts[2].lower()
108
 
109
    # Extract subarch from a name value pair
110
    def get_subarch(self, name, val):
111
        if name[:len("CONFIG_SUBARCH_")] == "CONFIG_SUBARCH_":
112
            parts = name.split("_", 3)
113
            self.subarch = parts[2].lower()
114
 
115
    # Extract platform from a name value pair
116
    def get_platform(self, name, val):
117
        if name[:len("CONFIG_PLATFORM_")] == "CONFIG_PLATFORM_":
118
            parts = name.split("_", 3)
119
            self.platform = parts[2].lower()
120
 
121
    # Extract cpu from a name value pair
122
    def get_cpu(self, name, val):
123
        if name[:len("CONFIG_CPU_")] == "CONFIG_CPU_":
124
            parts = name.split("_", 3)
125
            self.cpu = parts[2].lower()
126
 
127
            # derive gcc "-march" flag
128
            for cputype, archflag in self.arch_to_gcc_flag:
129
                if cputype == parts[2]:
130
                    self.gcc_arch_flag = archflag
131
 
132
    # Extract kernel space toolchain from a name value pair
133
    def get_toolchain(self, name, val):
134
        if name[:len("CONFIG_TOOLCHAIN_USERSPACE")] == \
135
                        "CONFIG_TOOLCHAIN_USERSPACE":
136
            parts = val.split("\"", 2)
137
            self.toolchain_userspace = parts[1]
138
 
139
        if name[:len("CONFIG_TOOLCHAIN_KERNEL")] == \
140
                        "CONFIG_TOOLCHAIN_KERNEL":
141
                parts = val.split("\"", 2)
142
                self.toolchain_kernel = parts[1]
143
 
144
 
145
    # Extract number of containers
146
    def get_ncontainers(self, name, val):
147
        if name[:len("CONFIG_CONTAINERS")] == "CONFIG_CONTAINERS":
148
            self.ncontainers = int(val)
149
 
150
    # TODO: Carry this over to Container() as static method???
151
    def get_container_parameter(self, id, param, val):
152
        if param[:len("PAGER_LMA")] == "PAGER_LMA":
153
            self.containers[id].pager_lma = int(val, 0)
154
        elif param[:len("PAGER_VMA")] == "PAGER_VMA":
155
            self.containers[id].pager_vma = int(val, 0)
156
        elif param[:len("PAGER_UTCB_START")] == "PAGER_UTCB_START":
157
            self.containers[id].pager_utcb_region_start = int(val, 0)
158
        elif param[:len("PAGER_UTCB_END")] == "PAGER_UTCB_END":
159
            self.containers[id].pager_utcb_region_end = int(val, 0)
160
        elif param[:len("PAGER_SHM_START")] == "PAGER_SHM_START":
161
            self.containers[id].pager_shm_region_start = int(val, 0)
162
        elif param[:len("PAGER_SHM_END")] == "PAGER_SHM_END":
163
            self.containers[id].pager_shm_region_end = int(val, 0)
164
        elif param[:len("PAGER_TASK_START")] == "PAGER_TASK_START":
165
            self.containers[id].pager_task_region_start = int(val, 0)
166
        elif param[:len("PAGER_TASK_END")] == "PAGER_TASK_END":
167
            self.containers[id].pager_task_region_end = int(val, 0)
168
        elif param[:len("LINUX_PAGE_OFFSET")] == "LINUX_PAGE_OFFSET":
169
            self.containers[id].linux_page_offset = int(val, 0)
170
            self.containers[id].pager_vma += int(val, 0)
171
        elif param[:len("LINUX_PHYS_OFFSET")] == "LINUX_PHYS_OFFSET":
172
            self.containers[id].linux_phys_offset = int(val, 0)
173
            self.containers[id].pager_lma += int(val, 0)
174
        elif param[:len("LINUX_ZRELADDR")] == "LINUX_ZRELADDR":
175
            self.containers[id].linux_zreladdr = int(val, 0)
176
        elif param[:len("LINUX_ROOTFS_ADDRESS")] == "LINUX_ROOTFS_ADDRESS":
177
            self.containers[id].linux_rootfs_address += int(val, 0)
178
        elif re.match(r"(VIRT|PHYS){1}([0-9]){1}(_){1}(START|END){1}", param):
179
            matchobj = re.match(r"(VIRT|PHYS){1}([0-9]){1}(_){1}(START|END){1}", param)
180
            virtphys, regionidstr, discard1, startend = matchobj.groups()
181
            regionid = int(regionidstr)
182
            if virtphys == "VIRT":
183
                self.containers[id].virtmem[startend][regionid] = val
184
                if regionid + 1 > self.containers[id].virt_regions:
185
                    self.containers[id].virt_regions = regionid + 1
186
            if virtphys == "PHYS":
187
                self.containers[id].physmem[startend][regionid] = val
188
                if regionid + 1 > self.containers[id].phys_regions:
189
                    self.containers[id].phys_regions = regionid + 1
190
        elif param[:len("OPT_NAME")] == "OPT_NAME":
191
            name = val[1:-1].lower()
192
            self.containers[id].name = name
193
        elif param[:len("BAREMETAL_PROJ_")] == "BAREMETAL_PROJ_":
194
            param1 = param.split("_", 2)
195
            self.containers[id].dirname = param1[2].lower()
196
        elif param[:len("CAP_")] == "CAP_":
197
            prefix, param_rest = param.split('_', 1)
198
            prepare_capability(self.containers[id], param_rest, val)
199
        else:
200
            param1, param2 = param.split("_", 1)
201
            if param1 == "TYPE":
202
                if param2 == "LINUX":
203
                    self.containers[id].type = "linux"
204
                elif param2 == "POSIX":
205
                    self.containers[id].type = "posix"
206
                elif param2 == "BAREMETAL":
207
                    self.containers[id].type = "baremetal"
208
    # Extract parameters for containers
209
    def get_container_parameters(self, name, val):
210
        matchobj = re.match(r"(CONFIG_CONT){1}([0-9]){1}(\w+)", name)
211
        if not matchobj:
212
            return None
213
 
214
        prefix, idstr, param = matchobj.groups()
215
        id = int(idstr)
216
 
217
        # Create and add new container if this id was not seen
218
        self.check_add_container(id)
219
 
220
        # Get rid of '_' in front
221
        param = param[1:]
222
 
223
        # Check and store info on this parameter
224
        self.get_container_parameter(id, param, val)
225
        #self.containers_print(self.containers)
226
 
227
    # Used for sorting container members,
228
    # with this we are sure containers are sorted by id value
229
    @staticmethod
230
    def compare_containers(cont, cont2):
231
        if cont.id < cont2.id:
232
            return -1
233
        if cont.id == cont2.id:
234
            print "compare_containers: Error, containers have same id."
235
            exit(1)
236
        if cont.id > cont2.id:
237
            return 1
238
 
239
    def check_add_container(self, id):
240
        for cont in self.containers:
241
            if id == cont.id:
242
                return
243
 
244
        # If symbol to describe number of containers
245
        # Has already been visited, use that number
246
        # as an extra checking.
247
        if self.ncontainers > 0:
248
            # Sometimes unwanted symbols slip through
249
            if id >= self.ncontainers:
250
                return
251
 
252
        container = Container(id)
253
        self.containers.append(container)
254
 
255
        # Make sure elements in order for indexed accessing
256
        self.containers.sort(self.compare_containers)
257
 
258
    def config_print(self):
259
        print 'Configuration\n'
260
        print '-------------\n'
261
        print 'Arch: %s, %s' % (self.arch, self.subarch)
262
        print 'Platform: %s' % self.platform
263
        print 'Symbols:\n %s' % self.all
264
        print 'Containers: %d' % self.ncontainers
265
        self.containers_print()
266
 
267
    def containers_print(self):
268
        for c in self.containers:
269
            c.print_self()
270
 
271
def configuration_save(config):
272
    if not os.path.exists(CONFIG_SHELVE_DIR):
273
        os.mkdir(CONFIG_SHELVE_DIR)
274
 
275
    config_shelve = shelve.open(CONFIG_SHELVE)
276
    config_shelve["configuration"] = config
277
 
278
    config_shelve["arch"] = config.arch
279
    config_shelve["subarch"] = config.subarch
280
    config_shelve["platform"] = config.platform
281
    config_shelve["cpu"] = config.cpu
282
    config_shelve["all_symbols"] = config.all
283
    config_shelve.close()
284
 
285
def configuration_retrieve():
286
    # Get configuration information
287
    config_shelve = shelve.open(CONFIG_SHELVE)
288
    config = config_shelve["configuration"]
289
    return config

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.