static void load_init_image( struct startup_l2_info* l2i, const char *name, genvaddr_t* init_ep, genvaddr_t* got_base ) { lvaddr_t elf_base; size_t elf_bytes; errval_t err; *init_ep = *got_base = 0; /* Load init ELF32 binary */ struct multiboot_modinfo *module = multiboot_find_module(name); if (module == NULL) { panic("Could not find init module!"); } elf_base = local_phys_to_mem(module->mod_start); elf_bytes = MULTIBOOT_MODULE_SIZE(*module); debug(SUBSYS_STARTUP, "load_init_image %p %08x\n", elf_base, elf_bytes); //printf("load_init_image %p %08x\n", elf_base, elf_bytes); err = elf_load(EM_ARM, startup_alloc_init, l2i, elf_base, elf_bytes, init_ep); if (err_is_fail(err)) { //err_print_calltrace(err); panic("ELF load of " BSP_INIT_MODULE_NAME " failed!\n"); } // TODO: Fix application linkage so that it's non-PIC. struct Elf32_Shdr* got_shdr = elf32_find_section_header_name((lvaddr_t)elf_base, elf_bytes, ".got"); if (got_shdr) { *got_base = got_shdr->sh_addr; } }
/** * \brief Setup an initial cspace * * Create an initial cspace layout */ static errval_t spawn_setup_cspace(struct spawninfo *si) { errval_t err; struct capref t1; /* Create root CNode */ err = cnode_create(&si->rootcn_cap, &si->rootcn, DEFAULT_CNODE_SLOTS, NULL); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_CREATE_ROOTCN); } /* Create taskcn */ err = cnode_create(&si->taskcn_cap, &si->taskcn, DEFAULT_CNODE_SLOTS, NULL); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_CREATE_TASKCN); } // Mint into rootcn setting the guard t1.cnode = si->rootcn; t1.slot = ROOTCN_SLOT_TASKCN; err = cap_mint(t1, si->taskcn_cap, 0, GUARD_REMAINDER(2 * DEFAULT_CNODE_BITS)); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_MINT_TASKCN); } /* Create slot_alloc_cnode */ t1.cnode = si->rootcn; t1.slot = ROOTCN_SLOT_SLOT_ALLOC0; err = cnode_create_raw(t1, NULL, (1<<SLOT_ALLOC_CNODE_BITS), NULL); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_CREATE_SLOTALLOC_CNODE); } t1.cnode = si->rootcn; t1.slot = ROOTCN_SLOT_SLOT_ALLOC1; err = cnode_create_raw(t1, NULL, (1<<SLOT_ALLOC_CNODE_BITS), NULL); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_CREATE_SLOTALLOC_CNODE); } t1.cnode = si->rootcn; t1.slot = ROOTCN_SLOT_SLOT_ALLOC2; err = cnode_create_raw(t1, NULL, (1<<SLOT_ALLOC_CNODE_BITS), NULL); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_CREATE_SLOTALLOC_CNODE); } // Create DCB si->dcb.cnode = si->taskcn; si->dcb.slot = TASKCN_SLOT_DISPATCHER; err = dispatcher_create(si->dcb); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_CREATE_DISPATCHER); } // Give domain endpoint to itself (in taskcn) struct capref selfep = { .cnode = si->taskcn, .slot = TASKCN_SLOT_SELFEP, }; err = cap_retype(selfep, si->dcb, ObjType_EndPoint, 0); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_CREATE_SELFEP); } // Map root CNode (in taskcn) t1.cnode = si->taskcn; t1.slot = TASKCN_SLOT_ROOTCN; err = cap_mint(t1, si->rootcn_cap, 0, 0); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_MINT_ROOTCN); } #ifdef TRACING_EXISTS // Set up tracing for the child err = trace_setup_child(si->taskcn, si->handle); if (err_is_fail(err)) { printf("Warning: error setting up tracing for child domain\n"); // SYS_DEBUG(err, ...); } #endif // XXX: copy over argspg? memset(&si->argspg, 0, sizeof(si->argspg)); /* Fill up basecn */ struct capref basecn_cap; struct cnoderef basecn; // Create basecn in rootcn basecn_cap.cnode = si->rootcn; basecn_cap.slot = ROOTCN_SLOT_BASE_PAGE_CN; err = cnode_create_raw(basecn_cap, &basecn, DEFAULT_CNODE_SLOTS, NULL); if (err_is_fail(err)) { return err_push(err, LIB_ERR_CNODE_CREATE); } // Place the ram caps for (uint8_t i = 0; i < DEFAULT_CNODE_SLOTS; i++) { struct capref base = { .cnode = basecn, .slot = i }; struct capref ram; err = ram_alloc(&ram, BASE_PAGE_BITS); if (err_is_fail(err)) { return err_push(err, LIB_ERR_RAM_ALLOC); } err = cap_copy(base, ram); if (err_is_fail(err)) { return err_push(err, LIB_ERR_CAP_COPY); } err = cap_destroy(ram); if (err_is_fail(err)) { return err_push(err, LIB_ERR_CAP_DESTROY); } } return SYS_ERR_OK; } static errval_t spawn_setup_vspace(struct spawninfo *si) { errval_t err; /* Create pagecn */ si->pagecn_cap = (struct capref){.cnode = si->rootcn, .slot = ROOTCN_SLOT_PAGECN}; err = cnode_create_raw(si->pagecn_cap, &si->pagecn, PAGE_CNODE_SLOTS, NULL); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_CREATE_PAGECN); } /* Init pagecn's slot allocator */ // XXX: satisfy a peculiarity of the single_slot_alloc_init_raw API size_t bufsize = SINGLE_SLOT_ALLOC_BUFLEN(PAGE_CNODE_SLOTS); void *buf = malloc(bufsize); assert(buf != NULL); err = single_slot_alloc_init_raw(&si->pagecn_slot_alloc, si->pagecn_cap, si->pagecn, PAGE_CNODE_SLOTS, buf, bufsize); if (err_is_fail(err)) { return err_push(err, LIB_ERR_SINGLE_SLOT_ALLOC_INIT_RAW); } // Create root of pagetable err = si->pagecn_slot_alloc.a.alloc(&si->pagecn_slot_alloc.a, &si->vtree); if (err_is_fail(err)) { return err_push(err, LIB_ERR_SLOT_ALLOC); } // top-level table should always live in slot 0 of pagecn assert(si->vtree.slot == 0); switch(si->cpu_type) { case CPU_X86_64: case CPU_K1OM: err = vnode_create(si->vtree, ObjType_VNode_x86_64_pml4); break; case CPU_X86_32: case CPU_SCC: #ifdef CONFIG_PAE err = vnode_create(si->vtree, ObjType_VNode_x86_32_pdpt); #else err = vnode_create(si->vtree, ObjType_VNode_x86_32_pdir); #endif break; case CPU_ARM5: case CPU_ARM7: err = vnode_create(si->vtree, ObjType_VNode_ARM_l1); break; default: assert(!"Other architecture"); return err_push(err, SPAWN_ERR_UNKNOWN_TARGET_ARCH); } if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_CREATE_VNODE); } err = spawn_vspace_init(si, si->vtree, si->cpu_type); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_VSPACE_INIT); } return SYS_ERR_OK; } #if 0 /** * \brief Lookup and map an image */ static errval_t spawn_map(const char *name, struct bootinfo *bi, lvaddr_t *binary, size_t *binary_size) { errval_t err; /* Get the module from the multiboot */ struct mem_region *module = multiboot_find_module(bi, name); if (module == NULL) { return SPAWN_ERR_FIND_MODULE; } /* Map the image */ err = spawn_map_module(module, binary_size, binary, NULL); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_MAP_MODULE); } return SYS_ERR_OK; }
/** * \brief Spawn a domain and give it the bootinfo struct. * Just monitor and memserv should be spawned using this. */ errval_t spawn_load_with_bootinfo(struct spawninfo *si, struct bootinfo *bi, const char *name, coreid_t coreid) { errval_t err; /* Get the module from the multiboot */ struct mem_region *module = multiboot_find_module(bi, name); if (module == NULL) { return SPAWN_ERR_FIND_MODULE; } /* Lookup and map the elf image */ lvaddr_t binary; size_t binary_size; err = spawn_map_module(module, &binary_size, &binary, NULL); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_ELF_MAP); } /* Determine cpu type */ err = spawn_determine_cputype(si, binary); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_DETERMINE_CPUTYPE); } /* Initialize cspace */ err = spawn_setup_cspace(si); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_SETUP_CSPACE); } /* Initialize vspace */ err = spawn_setup_vspace(si); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_VSPACE_INIT); } /* Load the image */ genvaddr_t entry; void* arch_info; si->name = name; err = spawn_arch_load(si, binary, binary_size, &entry, &arch_info); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_LOAD); } /* Setup dispatcher frame */ err = spawn_setup_dispatcher(si, coreid, name, entry, arch_info); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_SETUP_DISPATCHER); } /* Map bootinfo */ // XXX: Confusion address translation about l/gen/addr in entry genvaddr_t vaddr; err = spawn_map_bootinfo(si, &vaddr); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_MAP_BOOTINFO); } /* Construct cmdline args, 0 is name, 1 is bootinfo address, remaining are from the multiboot */ // Name char args[1024]; strcpy(args, name); strcat(args, " "); // bootinfo addr char vaddr_char[32]; // NB format here should be PRIuGENVADDR, but our ARM compiler has // an out-by-4 bytes issue when rendering 64-bit numbers using // __builtin_va_start/__builtin_va_arg. // [ gcc version 4.4.1 (Sourcery G++ Lite 2009q3-67) ] snprintf(vaddr_char, sizeof(vaddr_char), "%" PRIuPTR, (uintptr_t)vaddr); strcat(args, vaddr_char); strcat(args, " "); #ifdef __scc__ if(si->codeword == 0xcafebabe) { strcat(args, si->append_args); strcat(args, " "); } if(!strcmp(name, "scc/sbin/monitor")) { printf("starting monitor as '%s'\n", args); } #endif // Multiboot args char *multiboot_args; err = spawn_get_cmdline_args(module, &multiboot_args); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_GET_CMDLINE_ARGS); } // Lop off the name char *multiboot_args_lop = strchr(multiboot_args, ' '); if (multiboot_args_lop) { multiboot_args_lop++; strcat(args, multiboot_args_lop); } // Tokenize char *argv[MAX_CMDLINE_ARGS + 1]; spawn_tokenize_cmdargs(args, argv, ARRAY_LENGTH(argv)); // Setup err = spawn_setup_env(si, argv, environ); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_SETUP_ENV); } free(multiboot_args); // unmap bootinfo module pages spawn_unmap_module(binary); return SYS_ERR_OK; }
struct dcb *spawn_bsp_init(const char *name, alloc_phys_func alloc_phys) { errval_t err; /* Only the first core can run this code */ assert(apic_is_bsp()); /* Allocate bootinfo */ lpaddr_t bootinfo_phys = alloc_phys(BOOTINFO_SIZE); memset((void *)local_phys_to_mem(bootinfo_phys), 0, BOOTINFO_SIZE); /* Construct cmdline args */ char bootinfochar[16]; snprintf(bootinfochar, sizeof(bootinfochar), "%"PRIuLPADDR, BOOTINFO_BASE); const char *argv[6] = { "init", bootinfochar }; int argc = 2; #ifdef __scc__ if(glbl_core_data->urpc_frame_base != 0) { char coreidchar[10]; snprintf(coreidchar, sizeof(coreidchar), "%d", glbl_core_data->src_core_id); argv[argc++] = coreidchar; char chan_id_char[30]; snprintf(chan_id_char, sizeof(chan_id_char), "chanid=%"PRIu32, glbl_core_data->chan_id); argv[argc++] = chan_id_char; char urpc_frame_base_char[30]; snprintf(urpc_frame_base_char, sizeof(urpc_frame_base_char), "frame=%" PRIuGENPADDR, glbl_core_data->urpc_frame_base); argv[argc++] = urpc_frame_base_char; } #endif struct dcb *init_dcb = spawn_init_common(&spawn_state, name, argc, argv, bootinfo_phys, alloc_phys); /* Map bootinfo R/W into VSpace at vaddr 0x200000 (BOOTINFO_BASE) */ #ifdef CONFIG_PAE paging_x86_32_map_pdpte(&init_pdpte[0], mem_to_local_phys((lvaddr_t)init_pdir)); paging_x86_32_map_table(&init_pdir[1], mem_to_local_phys((lvaddr_t)init_ptable)); for (int i = 0; i < BOOTINFO_SIZE / BASE_PAGE_SIZE; i++) { paging_x86_32_map(&init_ptable[i], bootinfo_phys + i * BASE_PAGE_SIZE, INIT_PAGE_BITMAP | paging_elf_to_page_flags(PF_R|PF_W)); } #else paging_x86_32_map_table(&init_pdir[0], mem_to_local_phys((lvaddr_t)init_ptable)); for (int i = 0; i < BOOTINFO_SIZE / BASE_PAGE_SIZE; i++) { paging_x86_32_map(&init_ptable[i + 512], bootinfo_phys + i * BASE_PAGE_SIZE, INIT_PAGE_BITMAP | paging_elf_to_page_flags(PF_R|PF_W)); } #endif /* Load init ELF32 binary */ struct multiboot_modinfo *module = multiboot_find_module(name); if (module == NULL) { panic("Could not find init module!"); } genvaddr_t init_ep; err = elf_load(EM_386, startup_alloc_init, &spawn_state, local_phys_to_mem(module->mod_start), MULTIBOOT_MODULE_SIZE(*module), &init_ep); if (err_is_fail(err)) { //err_print_calltrace(err); panic("ELF load of init module failed!"); } struct dispatcher_shared_x86_32 *init_disp_x86_32 = get_dispatcher_shared_x86_32(init_dcb->disp); init_disp_x86_32->disabled_save_area.eip = init_ep; /* Create caps for init to use */ create_module_caps(&spawn_state); lpaddr_t init_alloc_end = alloc_phys(0); // XXX create_phys_caps(init_alloc_end); /* Fill bootinfo struct */ bootinfo->mem_spawn_core = NEEDED_KERNEL_SPACE; // Size of kernel /* for (int i = 0; i < bootinfo->regions_length; i++) { */ /* printf("%d region %d: 0x%09" PRIxPTR " - 0x%09lx (%lu MB, %u bits)\n", */ /* bootinfo->regions[i].mr_type, i, bootinfo->regions[i].mr_base, */ /* bootinfo->regions[i].mr_base + (1UL<<bootinfo->regions[i].mr_bits), */ /* bootinfo->regions[i].mr_bits >= 20 */ /* ? 1UL << (bootinfo->regions[i].mr_bits - 20) : 0, */ /* bootinfo->regions[i].mr_bits); */ /* } */ #if 0 // If app core, map (static) URPC channel if(kernel_scckernel != 0) { printf("SCC app kernel, frame at: 0x%x\n", kernel_scckernel); #define TASKCN_SLOT_MON_URPC (TASKCN_SLOTS_USER+6) ///< Frame cap for urpc comm. err = caps_create_new(ObjType_Frame, kernel_scckernel, 13, 13, caps_locate_slot(CNODE(taskcn), TASKCN_SLOT_MON_URPC)); assert(err_is_ok(err)); } #endif return init_dcb; }