예제 #1
0
파일: kernel.py 프로젝트: paromix/okl4
def get_kernel_version(kernel_el, machine):
    (elf, _, _) = get_kernel_file(kernel_el, machine)
    (addr, size) = get_symbol(elf, "kernel_api_version")
    kern_ver = elf.get_value(addr, size, elf.endianess)
    if kern_ver == None:
        raise MergeError("Unable to locate kernel api symbol")
    return kern_ver
예제 #2
0
파일: kernel.py 프로젝트: paromix/okl4
def link_kernel(kernel_el, linker_script, linker_wrapper, kernel, soc,
                libraries):
    link_type = get_linker_type(linker_script)

    kernel_tmp = None
    if link_type == 'rvct':
        # Current elfweaver linker does not support RVCT so shell out to an
        # external linker
        kernel_tmp = tempfile.NamedTemporaryFile()

        # Remove the file at exit.
        atexit.register(os.remove, kernel_tmp.name)

        command = [
            os.path.abspath(linker_wrapper), kernel, kernel_tmp.name,
            linker_script, soc
        ] + libraries

        ret = subprocess.Popen(command,
                               cwd=os.path.dirname(linker_script)).wait()

        if ret != 0:
            raise MergeError("Failed to link kernel, return code %d" % ret)

        elf = UnpreparedElfFile(kernel_tmp.name)
        kernel_out_name = kernel_tmp.name
    else:
        elf = link([kernel, soc])

        # Just getting a random temp file name, there must be a nicer way
        # to do this?
        tmp = tempfile.NamedTemporaryFile()

        # Remove the file at exit.
        atexit.register(os.remove, tmp.name)

        kernel_out_name = tmp.name
        tmp.close()

        kernel_out = elf.prepare(elf.wordsize, elf.endianess)
        kernel_out.to_filename(kernel_out_name)

        elf = UnpreparedElfFile(kernel_out_name)

    # Kernel is linked, now add the segment names (as per old elfadorn)
    # Kernel elements seg_names overwrites defaults
    if hasattr(kernel_el, "seg_names"):
        seglist = [seg.strip() for seg in kernel_el.seg_names.split(",")]
    else:
        seglist = None

    link_type = get_linker_type(linker_script)

    if link_type == "rvct":
        scripts = [linker_script]
    else:
        scripts = []
    add_segment_names(elf, get_segment_names(elf, seglist, scripts, link_type))

    return elf, kernel_out_name, kernel_tmp
예제 #3
0
파일: kernel.py 프로젝트: openbox00/oktest
def get_kernel_file(kernel_el, machine):
    def relative(file):
        """Return a path taking relativity to XML include directories into account"""
        return os.path.join(kernel_el._path, file)
    if has_kernel_attr(kernel_el, KERNEL_PRELINK_TAGS):
        elf = UnpreparedElfFile(filename = relative(kernel_el.file))
        return (elf, kernel_el.file, None)
    elif has_kernel_attr(kernel_el, KERNEL_SDKLINK_TAGS):
        kernel_dir = os.path.join(relative(kernel_el.sdk), "kernel", machine.get_cpu())
        object_dir = os.path.join(kernel_dir, kernel_el.configuration, "object")
        lib_dir = os.path.join(kernel_dir, kernel_el.configuration, "libs")

        linker_script = os.path.abspath(os.path.join(object_dir, "linker.lds"))
        linker_wrapper = os.path.abspath(os.path.join(object_dir, "linker.sh"))
        kernel = os.path.abspath(os.path.join(object_dir, machine.get_cpu() +
                                              ".o"))
        soc = os.path.abspath(os.path.join(object_dir, kernel_el.platform +
                                           ".o"))
        libs = [os.path.abspath(lib) for lib in glob.glob(os.path.join(lib_dir,
                                                          "*.a"))]

        return link_kernel(kernel_el, linker_script, linker_wrapper, kernel,
                           soc, libs)
    elif has_kernel_attr(kernel_el, KERNEL_LINK_TAGS):
        libs = [relative(lib.strip()) for lib in kernel_el.libs.split(",")]

        return link_kernel(kernel_el, relative(kernel_el.linker_script),
                           relative(kernel_el.linker_wrapper),
                           relative(kernel_el.kernel),
                           relative(kernel_el.soc),
                           libs)

    raise MergeError("Invalid kernel tags; a prelinked kernel, SDK information or linking information must be provided")
예제 #4
0
    def machine_init(self, machine):
        self.cpu = machine.cpu
        if self.cpu not in self.arch_lookup:
            raise MergeError("Cpu %s not supported by nano kernel" % self.cpu)

        machine.set_kernel_heap_proximity(
            self.heap_proximity_map[self.arch_lookup[self.cpu]])
예제 #5
0
파일: kernel.py 프로젝트: paromix/okl4
def get_linker_type(linker_script):
    buffer = open(linker_script, "r").read()

    if "NOCOMPRESS" in buffer:
        return "rvct"
    elif "SECTIONS" in buffer:
        return "gnu"
    else:
        raise MergeError("Unkown linker.lds type")
예제 #6
0
파일: kernel.py 프로젝트: openbox00/oktest
def has_kernel_attr(kernel_el, test_tags):
    for tag in test_tags:
        if not hasattr(kernel_el, tag):
            break
    else:
        for tag in KERNEL_TAGS.difference(test_tags):
            if hasattr(kernel_el, tag):
                raise MergeError("Found unexpected kernel tag %s when %s has already been specificed" % (tag, test_tags))

        return True

    return False
예제 #7
0
파일: kernel.py 프로젝트: openbox00/oktest
    def layout_cells_post(self, image):
        # This must be called after image.layout() and before the cell
        # init scripts are written out.
        #
        # Do any required layout here

        self.kernel.layout_cells_post(self, image)

        if self.machine.arch_max_spaces != 0 and \
                self.total_spaces > self.machine.arch_max_spaces:
            raise MergeError("Tried to allocate %d spaces, kernel only allows %d."
            % (self.total_spaces, self.machine.arch_max_spaces))
예제 #8
0
파일: kernel.py 프로젝트: paromix/okl4
def check_api_versions(kernel):
    """ Return the kernel API version number.  """
    kern_ver = get_symbol(kernel, "kernel_api_version", True)
    if kern_ver:
        addr = kern_ver[0]
        size = kern_ver[1]
    else:
        return None
    kern_ver = kernel.get_value(addr, size, kernel.endianess)

    # Check that both the kernel and soc api versions match
    if kern_ver == MICRO_KERNEL_API_VERSION:
        (addr, size) = get_symbol(kernel, "soc_api_version")
        soc_ver = kernel.get_value(addr, size, kernel.endianess)
        if soc_ver == None:
            raise MergeError("Unable to locate soc api symbol")

        if kern_ver != soc_ver:
            raise MergeError(
                "Kernel api version %d doesn't match soc api version %d" %
                (kern_ver, soc_ver))

    return kern_ver
예제 #9
0
파일: kernel.py 프로젝트: openbox00/oktest
            def register_thread(self, entry, user_start, utcb, priority,
                    create = True):
                assert self.create or not create

                if self.max_priority is not None and \
                        priority > self.max_priority:
                    raise MergeError("Tried to create thread with priority %s," \
                    " but space (%s) max priority is %s." % (priority,
                                                             self.ns_node.abs_name(),
                                                             self.max_priority))

                thread = self.Thread(entry, user_start, utcb, priority, create)
                self.threads.append(thread)

                return thread
예제 #10
0
    def collect_xml(self, iguana_el, ignore_name, namespace, machine,
                    pools, kernel, image):
        """Handle an Iguana Server Compound Object"""
        cell = \
             kernel.register_cell(iguana_el.name,
                                  iguana_el.kernel_heap,
                                  max_caps = getattr(iguana_el, "caps", None))

        # New namespace for objects living in the root program's PD.
        ig_namespace = namespace.add_namespace(iguana_el.name)
        self.namespace = ig_namespace
        self.s_namespace = self.namespace
        self.name = iguana_el.name

        self.space = \
                   cell.register_space(self.namespace, "MAIN",
                                       is_privileged = True,
                                       max_clists = getattr(iguana_el,
                                                            "clists", None),
                                       max_spaces = getattr(iguana_el,
                                                            "spaces", None),
                                       max_mutexes = getattr(iguana_el,
                                                             "mutexes", None),
                                       max_threads = getattr(iguana_el,
                                                             "threads", None),
                                       plat_control = \
                                       getattr(iguana_el,
                                               "platform_control",
                                               False))
        self.setup_bootinfo_pools(namespace, pools)
        self.setup_device_namespace(namespace, machine)


        self.env = CellEnvironment(iguana_el.name, self.namespace,
                                   machine, image, kernel,
                                   self.space.mappings)
        cell.env = self.env

        pd = weaver.cells.iguana.bootinfo.RootServerPD(iguana_el.name,
                                                       ig_namespace)

        # Declare the default memory pools.
        def_virtpool = getattr(iguana_el, "virtpool",
                               pools.get_default_virtual_pool())
        def_physpool = getattr(iguana_el, "physpool",
                               pools.get_default_physical_pool())
        def_pager    = getattr(iguana_el, "pager", None)
        def_direct   = getattr(iguana_el, "direct", None)

        # Record any IRQs that are assigned to Iguana
        for irq_el in iguana_el.find_children("irq"):
            self.space.register_irq(irq_el.value)


        self.bootinfo.set_system_default_attrs(def_virtpool,
                                               def_physpool,
                                               image,
                                               def_pager,
                                               def_direct)

        # Iguana is not aware of segment ids.
        # The old mapping API uses segment 0 for all mapping ops.
        # Segment 0 needs to cover all of physical memory.
        # Subsequent segments will be created but never used,
        # but must still be mapped!

        # XXX: VVVVV THIS IS PURE EVIL VVVVV
        physpool_attrs = \
                       image.new_attrs(self.s_namespace.add_namespace("physpool_hack"))
        physpool_attrs.phys_addr = 0 #first_phys_base
        physpool_attrs.size = 0xfffff000 #first_phys_end
        physpool_attrs.attach = PF_R | PF_W | PF_X
        physpool_attrs.cache_policy = 0xff # XXX: Define me properly
        physpool_attrs.mem_type = physpool_attrs.unmapped
        self.space.register_mapping(physpool_attrs)
        # XXX: ^^^^^ THIS IS PURE EVIL ^^^^^

        filename = os.path.join(iguana_el._path, iguana_el.file)
        elf = UnpreparedElfFile(filename=filename)

        pd.set_default_pools(image, self.bootinfo)

        # Collect the object environment
        pd.add_env_ms(image, ig_namespace, machine, pools)
        env, extra_ms = \
             collect_environment_element(iguana_el.find_child('environment'),
                                         ig_namespace, machine, pools,
                                         image, self.bootinfo)

        segment_els = iguana_el.find_children("segment")
        segs = collect_elf_segments(elf,
                                    image.ROOT_PROGRAM,
                                    segment_els,
                                    filename,
                                    [],
                                    ig_namespace,
                                    image,
                                    machine,
                                    pools)

        self.elf_prog_segments = segs
        for seg in segs:
            self.space.register_mapping(seg.attrs)

        if elf.elf_type != ET_EXEC:
            raise MergeError, "All the merged ELF files must be of EXEC type."

        # Find out which version of libokl4 that iguana was built
        # against
        sym = elf.find_symbol("okl4_api_version")
        if sym == None:
            raise MergeError("Unable to locate the symbol 'okl4_api_version' "
                             "in file \"%s\".  Cells must link with libokl4." %
                             filename)

        self.api_version = elf.get_value(sym.value, sym.size,
                                         elf.endianess)
        if self.api_version == None:
            raise MergeError("Unable to read the symbol 'okl4_api_version' in "
                             "file \"%s\".  Cells must link with libokl4." %
                             filename)

        # Record any patches being made to the program.
        patch_els = iguana_el.find_children("patch")
        for patch in getattr(Iguana_el, "extra_patches", []):
            addr = get_symbol(elf, patch[0], True)
            if addr == None:
                continue
            addr = int(addr[0])+ int(patch[1])
            new_patch = Patch_el(address=hex(addr), bytes=patch[2],
                                 value=patch[3])
            patch_els.append(new_patch)
        collect_patches(elf, patch_els, filename, image)

        for extension_el in iguana_el.find_children("extension"):
            if not ignore_name.match(extension_el.name):
                collect_extension_element(extension_el,
                                          pd,
                                          ig_namespace,
                                          elf,
                                          image,
                                          machine,
                                          self.bootinfo,
                                          pools)

        # Collect the main thread.  The root program can only have one
        # thread, so this call chiefly is used to collect information
        # about the stack.
        #
        # The stack is not set up as a memsection, so it is not put in the
        # object environment.
        if not hasattr(iguana_el, 'priority'):
            iguana_el.priority = 255
        thread = collect_thread(elf, iguana_el, ignore_name, ig_namespace,
                                image, machine, pools, self.space,
                                entry = elf.entry_point,
                                name = 'iguana',
                                namespace_thread_name = "main",
                                cell_create_thread = True)
        pd.add_thread(thread)

        # Collect the heap.  Is there no element, create a fake one for
        # the collection code to use.
        #
        # The heap is not set up as a memsection, so it is not put in the
        # object environment.
        heap_el = iguana_el.find_child('heap')

        if heap_el is None:
            heap_el = ParsedElement('heap')

        heap_ms = collect_memsection_element(heap_el, ignore_name,
                                             ig_namespace, image,
                                             machine, pools)
        pd.attach_heap(heap_ms)

        self.space.register_mapping(heap_ms.ms.attrs)
        self.space.register_mapping(thread.get_stack().get_ms().attrs)

        self.elf_segments.extend([pd.env_ms.get_ms(),
            thread.get_stack().get_ms(), heap_ms.get_ms()] +
            [ms.get_ms() for ms in extra_ms])

        pd.add_environment(env)
        pd.utcb_size = image.utcb_size
        self.bootinfo.add_rootserver_pd(pd)

        # And now parse the programs and pd's
        collect_program_pd_elements(iguana_el, ignore_name,
                                    ig_namespace, image, machine,
                                    self.bootinfo, pools, kernel,
                                    cell)
예제 #11
0
파일: kernel.py 프로젝트: paromix/okl4
    def collect_xml(self, kernel_el, kernel_heap_size, namespace, image,
                    machine, pools):
        """Collect the attributes of the kernel element."""

        (elf, kernel_file, kernel_tmp) = get_kernel_file(kernel_el, machine)

        if elf.elf_type != ET_EXEC:
            raise MergeError, \
                  "All the merged ELF files must be of EXEC type."

        # New namespace for objects living in the kernel.
        kernel_namespace = namespace.add_namespace('kernel')

        # We setup our kernel type depending on the api version number
        kern_ver = check_api_versions(elf)
        if kern_ver == NANO_KERNEL_API_VERSION:
            self.kernel = NanoKernel(elf, kernel_namespace, machine)
        else:
            self.kernel = MicroKernel(kernel_namespace)

        assert self.total_threads is None
        self.total_threads = self.kernel.ABSOLUTE_MAX_THREADS

        # Record the default pools for cells to use if they don't have
        # defaults set.
        virtpool = getattr(kernel_el, "virtpool", None)
        physpool = getattr(kernel_el, "physpool", None)
        image.set_attrs_stack(def_virt=virtpool, def_phys=physpool)
        pools.set_default_pools(virtpool, physpool)

        segment_els = kernel_el.find_children("segment")
        patch_els = kernel_el.find_children("patch")

        machine.set_cache_attributes(elf)

        image.set_kernel(elf)
        segs = collect_elf_segments(elf, image.KERNEL, segment_els, 'kernel',
                                    self.kernel.segment_drops,
                                    kernel_namespace, image, machine, pools)
        # Set base segment
        self.base_segment = self.kernel.get_base_segment(elf, segs, image)

        if self.base_segment.attrs.phys_addr is not None and \
            (self.base_segment.attrs.phys_addr & ((1024 * 1024) - 1)) != 0:
            raise MergeError("Physical address of %s must be 1MB aligned!" %
                             self.base_segment.attrs.abs_name())

        elf = elf.prepare(elf.wordsize, elf.endianess)
        # The extra_patches attr may be added by a plugin.
        for patch in getattr(Kernel_el, "extra_patches", []):
            addr = get_symbol(elf, patch[0], True)
            if addr != None:
                addr = int(addr[0]) + int(patch[1])
                new_patch = Patch_el(address=hex(addr),
                                     bytes=patch[2],
                                     value=patch[3])
                patch_els.append(new_patch)

        collect_patches(elf, patch_els, kernel_file, image)

        # Collect the config elements, some of these relate to
        # heap and thread array, so do it first
        config_el = kernel_el.find_child("config")
        if config_el is not None:
            for option in config_el.find_children("option"):
                self.add_config(option.key, option.value)

        heap_attrs = image.new_attrs(
            namespace.root.add_namespace("kernel_heap"))
        heap_attrs.align = machine.kernel_heap_align
        heap_el = kernel_el.find_child("heap")
        if heap_el is not None:
            heap_attrs.phys_addr = getattr(heap_el, 'phys_addr',
                                           heap_attrs.phys_addr)
            heap_attrs.size = getattr(heap_el, 'size', heap_attrs.size)
            heap_attrs.align = getattr(heap_el, 'align', heap_attrs.align)

        # Override the size with the command line value, if present.
        if kernel_heap_size != 0:
            heap_attrs.size = kernel_heap_size

        # Nano needs a heap size set early on
        # Micro remains unassigned since it will get dynamically calculated later
        if not heap_attrs.size and isinstance(self.kernel, NanoKernel):
            heap_attrs.size = self.kernel.DEFAULT_KERNEL_HEAP_SIZE

        self.heap = image.set_kernel_heap(heap_attrs, machine, pools,
                                          self.base_segment.segment,
                                          isinstance(self.kernel, NanoKernel))
        image.add_group(machine.kernel_heap_proximity,
                        (self.base_segment, self.heap))
        self.register_heap(heap_attrs)

        if isinstance(self.kernel, MicroKernel):
            image.utcb_size = self.kernel.get_utcb_size(elf, image)

        self.collect_use_devices(kernel_el, image, machine, pools)

        if kernel_tmp:
            kernel_tmp.close()
예제 #12
0
파일: kernel.py 프로젝트: paromix/okl4
    def layout_cells_pre(self):
        # This is called before dynamic elements of cells as we should
        # be able to decide the number of cells, clists and mutexes at that
        # time

        clist_id = 0
        space_id = 0
        mutex_id = 0

        self.total_spaces = 0
        self.total_clists = 0
        self.total_mutexes = 0

        total_threads = 0

        for cell in self.cells.values():
            cap_slot = 0

            cell.clist_id = clist_id

            # Store the base id for the space, caps and mutex lists
            space_list_base = space_id
            cap_list_base = clist_id
            mutex_list_base = mutex_id

            # Init the number of used entries in the lists
            space_list_used = 0
            cap_list_used = 0
            mutex_list_used = len(cell.mutexes)

            cell_threads = 0

            tmp_cell_max_spaces = 0

            for space in cell.get_static_spaces():
                space.calc_maxes()
                if space.is_privileged and len(cell.spaces) > space.max_spaces:
                    space.max_spaces = len(cell.spaces)

                space.id = space_id
                space.space_id_base = space_id
                space_id += space.max_spaces
                if space.is_privileged:
                    cell.total_spaces += 1
                    self.total_spaces += space.max_spaces
                    tmp_cell_max_spaces = space.max_spaces
                else:
                    cell.total_spaces += space.max_spaces

                space.clist_id_base = clist_id
                clist_id += space.max_clists
                self.total_clists += space.max_clists

                space.mutex_id_base = mutex_id
                mutex_id += space.max_mutexes
                self.total_mutexes += space.max_mutexes

                for thread in space.get_static_threads():
                    thread.cap_slot = cap_slot
                    cap_slot += 1

                cell_threads += space.max_threads

                assigned_mutex_id = space.mutex_id_base
                for mutex in space.get_static_mutexes():
                    mutex.id = assigned_mutex_id
                    assigned_mutex_id += 1

                for cap in space.ipc_caps:
                    cap.clist = cell
                    cap.cap_slot = cap_slot
                    cap_slot += 1

                for cap in space.mutex_caps:
                    cap.clist = cell
                    cap.cap_slot = cap_slot
                    cap_slot += 1

                space_list_used += 1
                cap_list_used += cap_slot
                mutex_list_used += len(space.mutexes)

            if tmp_cell_max_spaces != 0 and \
               tmp_cell_max_spaces < cell.total_spaces:
                raise MergeError(
                    "Tried to allocate %s spaces, but cell max spaces is %s." %
                    (cell.total_spaces, tmp_cell_max_spaces))

            if cell.max_threads < cell_threads:
                cell.max_threads = cell_threads

            cell.space_list = (space_list_base, space_id - space_list_base,
                               space_list_used)
            cell.cap_list = (cap_list_base, clist_id - cap_list_base,
                             cap_list_used)
            cell.mutex_list = (mutex_list_base, mutex_id - mutex_list_base,
                               mutex_list_used)

            total_threads += cell_threads

        if self.total_threads < total_threads:
            raise MergeError, \
                  "Total number of threads (%d) exceeds kernel maximum of %d." % \
                  (total_threads, self.total_threads)

        # Now do any kernel specific layout
        self.kernel.layout_cells_pre(self)
예제 #13
0
    def collect_xml(self, okl4_el, ignore_name, namespace, machine, pools,
                    kernel, image):
        """Handle an Iguana Server Compound Object"""
        self.cell = \
             kernel.register_cell(okl4_el.name,
                                  okl4_el.kernel_heap,
                                  max_caps = getattr(okl4_el, "caps", None),
                                  max_priority = getattr(okl4_el, "max_priority", None))
        self.name = okl4_el.name
        self.namespace = namespace.add_namespace(self.name)
        self.space = \
                   self.cell.register_space(self.namespace, "MAIN",
                                is_privileged = True,
                                max_clists = getattr(okl4_el,
                                                     "clists", None),
                                max_spaces = getattr(okl4_el,
                                                     "spaces", None),
                                max_mutexes = getattr(okl4_el,
                                                      "mutexes", None),
                                max_threads = getattr(okl4_el,
                                                      "threads", None),
                                max_priority = getattr(okl4_el,
                                                       "max_priority", None),
                                plat_control = \
                                            getattr(okl4_el,
                                                    "platform_control", False))

        image.push_attrs(virtual=getattr(okl4_el, "virtpool", None),
                         physical=getattr(okl4_el, "physpool", None),
                         pager=make_pager_attr(getattr(okl4_el, "pager",
                                                       None)),
                         direct=getattr(okl4_el, "direct", None))

        (self.def_virtpool, self.def_physpool) = image.current_pools()

        self.collect_mutexes(okl4_el, self.namespace, self.space)

        env_el = okl4_el.find_child("environment")
        self.env = CellEnvironment(okl4_el.name, self.namespace, machine,
                                   image, kernel, self.space.mappings)

        if env_el != None:
            self._collect_environment(env_el, self.env)

        # Set these up now even though we can't actually assign values
        # till later
        self.phys_attrs = image.new_attrs(
            self.namespace.add_namespace("default_physpool"))
        self.phys_attrs.attach = PF_R | PF_W | PF_X
        self.phys_attrs.mem_type = self.phys_attrs.unmapped
        mapping = self.space.register_mapping(self.phys_attrs)
        self.env.add_physmem_segpool_entry("MAIN_PHYSMEM_SEGPOOL", mapping)

        self.virt_attrs = image.new_attrs(
            self.namespace.add_namespace("default_virtpool"))
        self.env.add_virtmem_pool_entry("MAIN_VIRTMEM_POOL", self.virt_attrs)

        self.space.utcb = image.new_attrs(
            self.namespace.add_namespace("main_utcb_area"))
        self.space.utcb.attach = PF_R

        filename = os.path.join(okl4_el._path, okl4_el.file)
        self.elf = UnpreparedElfFile(filename=filename)

        if self.elf.elf_type != ET_EXEC:
            raise MergeError("All the merged ELF files must be of EXEC type.")

        # Find out which version of libokl4 that the cell was built
        # against
        sym = self.elf.find_symbol("okl4_api_version")
        if sym == None:
            raise MergeError(
                "Unable to locate the symbol 'okl4_api_version' in file \"%s\".  Cells must link with libokl4."
                % filename)

        self.api_version = self.elf.get_value(sym.value, sym.size,
                                              self.elf.endianess)
        if self.api_version == None:
            raise MergeError(
                "Unable to read the symbol 'okl4_api_version' in file \"%s\".  Cells must link with libokl4."
                % filename)

        self.env.add_elf_info_entry(os.path.basename(okl4_el.file),
                                    image.PROGRAM, self.elf.entry_point)

        segment_els = okl4_el.find_children("segment")
        segs = collect_elf_segments(self.elf, image.PROGRAM, segment_els,
                                    filename, [], self.namespace, image,
                                    machine, pools)
        self.elf_prog_segments = segs

        for seg in segs:
            self.env.add_elf_segment_entry(
                okl4_el.name + '.' + seg.attrs.ns_node.name, seg.segment)
            seg_ns = seg.attrs.ns_node
            mapping = self.space.register_mapping(seg.attrs)
            self.add_standard_mem_caps(seg_ns, mapping, seg.attrs)

        patch_els = okl4_el.find_children("patch")
        collect_patches(self.elf, patch_els, filename, image)

        # Record any IRQs that are assigned to the initial program.
        for irq_el in okl4_el.find_children("irq"):
            self.space.register_irq(irq_el.value)
        self.env.add_device_irq_list("NO_DEVICE",
                                     [irq_el.value for irq_el \
                                      in okl4_el.find_children("irq")])

        # Collect the implicit thread
        if not hasattr(okl4_el, 'priority'):
            okl4_el.priority = kernel.kernel.MAX_PRIORITY

        threads = []
        threads.append(
            self.collect_thread(self.elf, okl4_el, self.namespace, image,
                                machine, pools, kernel, self.space,
                                self.elf.entry_point, "main", True))

        # FIXME: Need to check up on actual entry point's
        for thread_el in okl4_el.find_children("thread"):
            threads.append(
                self.collect_thread(self.elf,
                                    thread_el,
                                    self.namespace,
                                    image,
                                    machine,
                                    pools,
                                    kernel,
                                    self.space,
                                    "thread_start",
                                    cell_create_thread=True))

        device_mem = \
                   self.collect_use_devices(okl4_el, self.space,
                                            self.namespace, image,
                                            machine, pools)

        memsection_objs = \
                    self.collect_memsections(okl4_el, self.space,
                            self.namespace, image, machine, pools)

        # Collect all data for any extra spaces defined in the XML
        for space_el in okl4_el.find_children("space"):
            space_ns = self.namespace.add_namespace(space_el.name)
            space = self.cell.register_space(space_ns, space_el.name,
                        max_priority = getattr(space_el, "max_priority", \
                                       getattr(okl4_el, "max_priority", None)))

            image.push_attrs(virtual=getattr(space_el, "virtpool", None),
                             physical=getattr(space_el, "physpool", None),
                             pager=make_pager_attr(
                                 getattr(space_el, "pager", None)),
                             direct=getattr(space_el, "direct", None))

            for thread_el in space_el.find_children("thread"):
                threads.append(
                    self.collect_thread(self.elf,
                                        thread_el,
                                        space_ns,
                                        image,
                                        machine,
                                        pools,
                                        kernel,
                                        space,
                                        "thread_start",
                                        cell_create_thread=True))

            self.collect_mutexes(space_el, space_ns, space)

            device_mem.extend(
                self.collect_use_devices(space_el, space, space_ns, image,
                                         machine, pools))

            memsection_objs.extend(
                self.collect_memsections(space_el, space, space_ns, image,
                                         machine, pools))

            self.env.add_kspace_entry(space_el.name + "_KSPACE", space)

            space.utcb = image.new_attrs(
                space_ns.add_namespace(space_el.name + "_utcb_area"))
            space.utcb.attach = PF_R

        # Weave a kclist for the main space.
        main_kclist = self.env.add_kclist_entry("MAIN_KCLIST", self.cell)

        # Weave the root kspace object.
        main_kspace = self.env.add_kspace_entry("MAIN_KSPACE", self.space)

        # Weave the root protection domain object.
        self.env.add_pd_entry("MAIN_PD", self.space, main_kspace, 32,
                              machine.min_page_size(), self.elf)

        # Find heap and add it
        heap_el = okl4_el.find_child('heap')
        if heap_el is None:
            heap_el = ParsedElement('heap')

        heap_attr = collect_memobj_attrs(heap_el, self.namespace, image,
                                         machine)

        if heap_attr.size == None:
            heap_attr.size = DEFAULT_HEAP_SIZE

        self.heap_ms = image.add_memsection(heap_attr, machine, pools)
        self.cell.heap = self.heap_ms.attrs
        mapping = self.space.register_mapping(self.heap_ms.attrs)

        self.add_standard_mem_caps(heap_attr.ns_node, mapping, heap_attr)

        self.elf_segments = \
                [thread.get_stack_ms() for thread in threads] + \
                [self.heap_ms] + memsection_objs + device_mem

        self.env.add_kernel_info_entry(0, 0, kernel)

        # Add command line arguments
        commandline_el = okl4_el.find_child("commandline")

        if commandline_el is not None:
            args = [
                arg_el.value for arg_el in commandline_el.find_children("arg")
            ]
        else:
            args = []

        self.env.add_arg_list(args)

        self.cell.env = self.env