int mon_start_user (int argc, char **argv, struct Trapframe *tf) { if (CID != 0) { dprintf( "The process is already running. If you want to run the program again, please restart qemu.\n"); return 0; } uint8_t * exe = _binary___obj_proc_dummy_dummy_start; CID = alloc_mem_quota (0, 1024 * 1024); elf_load (exe, CID); dprintf("Program 0x%08x is loaded.\n", exe); set_pdir_base (CID); entry_t entry = (entry_t) elf_entry (exe); entry(); return 0; }
struct _elf * elf_load_addr (const char * filename, uint64_t load_address) { struct _elf * elf; elf = elf_load(filename); elf->load_address = load_address; return elf; }
/** Parse an executable image in the kernel memory. * * If the image belongs to a program loader, it is registered as such, * (and *task is set to NULL). Otherwise a task is created from the * executable image. The task is returned in *task. * * @param[in] image_addr Address of an executable program image. * @param[in] name Name to set for the program's task. * @param[out] prg Buffer for storing program info. * If image_addr points to a loader image, * prg->task will be set to NULL and EOK * will be returned. * * @return EOK on success or negative error code. * */ int program_create_from_image(void *image_addr, char *name, program_t *prg) { as_t *as = as_create(0); if (!as) return ENOMEM; prg->loader_status = elf_load((elf_header_t *) image_addr, as, 0); if (prg->loader_status != EE_OK) { as_destroy(as); prg->task = NULL; prg->main_thread = NULL; if (prg->loader_status != EE_LOADER) return ENOTSUP; /* Register image as the program loader */ if (program_loader != NULL) return ELIMIT; program_loader = image_addr; printf("Program loader at %p\n", (void *) image_addr); return EOK; } return program_create(as, ((elf_header_t *) image_addr)->e_entry, name, prg); }
void boot(void) { char *path=pop_fstr_copy(), *param; // char *param="root=/dev/hda2 console=ttyS0,115200n8 console=tty0"; if(!path) { printk("[x86] Booting default not supported.\n"); return; } param = strchr(path, ' '); if(param) { *param = '\0'; param++; } printk("[x86] Booting file '%s' with parameters '%s'\n",path, param); if (elf_load(&sys_info, path, param) == LOADER_NOT_SUPPORT) if (linux_load(&sys_info, path, param) == LOADER_NOT_SUPPORT) printk("Unsupported image format\n"); free(path); }
/** Create a task from the program loader image. * * @param prg Buffer for storing program info. * @param name Name to set for the program's task. * * @return EOK on success or negative error code. * */ int program_create_loader(program_t *prg, char *name) { as_t *as = as_create(0); if (!as) return ENOMEM; void *loader = program_loader; if (!loader) { as_destroy(as); printf("Cannot spawn loader as none was registered\n"); return ENOENT; } prg->loader_status = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER); if (prg->loader_status != EE_OK) { as_destroy(as); printf("Cannot spawn loader (%s)\n", elf_error(prg->loader_status)); return ENOENT; } return program_create(as, ((elf_header_t *) program_loader)->e_entry, name, prg); }
void cmd_elf(char *arg) { int fd, ret; struct file *fp; int isElf; elf_header_t header; fd = call_syscall(5, (uintptr_t) arg, 0, 0); if (fd <= 0) { _printk("open failed: %d\n", fd); return; } fp = current->owner->file_table[fd]; isElf = elf_probe(fp); _printk("file(%d) is %sELF\n", fd, isElf ? "" : "not "); if (!isElf) { call_syscall(6, fd, 0, 0); return; } fp->fops->read(fp, &header, sizeof(elf_header_t)); _printk("ELF Type: %s\n", elf_type_as_string(header.e_type)); _printk("Entry Point: 0x%x\n", header.e_entry); ret = elf_load(fp); _printk("ELF RET: %s\n", errno_to_string(ret)); }
int sys_execve(const char *filename, char *const argv[], char *const envp[]) { elf_t bin; if (!elf_read(filename, &bin)) { return -1; } elf_load(&bin); elf_start(&bin, argv, envp); /* never reached */ return -1; }
int main(void) { static char buf[16]; static long size = -1; static unsigned char *loadbuf = NULL; char *entry_point; void (*f)(void); extern int buffer_start; /* リンカ・スクリプトで定義されているバッファ */ INTR_DISABLE; /* 割込み無効にする */ init(); puts("kzload (kozos boot loader) started.\n"); while (1) { puts("kzload> "); /* prompt */ gets(buf); if(!strcmp(buf, "load")) { /* XMODEMでのファイルのダウンロード */ loadbuf = (char *)(&buffer_start); size = xmodem_recv(loadbuf); wait(); /* 転送アプリが終了し端末アプリに制御が戻るまで待ち合わせる */ if(size < 0) { puts("\nXMODEM receive error!\n"); } else { puts("\nXMODEM receive succeeded.\n"); } } else if(!strcmp(buf, "dump")) { /* メモリの16進ダンプ出力 */ puts("size: "); putxval(size, 0); puts("\n"); dump(loadbuf, size); } else if(!strcmp(buf, "run")) { /* ELF形式ファイルの実行 */ entry_point = elf_load(loadbuf); if(!entry_point) { puts("run error!\n"); } else { puts("starting from entry point: "); putxval((unsigned long)entry_point, 0); puts("\n"); f = (void (*)(void))entry_point; /* ロードしたプログラムに処理を渡す */ f(); /* ここには返ってこない */ } } else { puts("unknown command.\n"); } } return 0; }
int main(void) { static char buf[16]; static long size = -1; static unsigned char *loadbuf = NULL; char *entry_point; void (*f)(void); extern int buffer_start; INTR_DISABLE; init(); puts("kzload (kozos boot loader) started.\n"); while (1) { puts("kzload> "); gets(buf); if (!strcmp(buf, "load")) { loadbuf = (char *)&buffer_start; size = xmodem_recv(loadbuf); wait(); if (size < 0) { puts("\nXMODEM receive error!\n"); } else { puts("\nXMODEM receive succeeded.\n"); } } else if (!strcmp(buf, "dump")) { puts("size: "); putxval(size, 0); puts("\n"); dump(loadbuf, size); } else if (!strcmp(buf, "run")) { entry_point = elf_load(loadbuf); if (!entry_point) { puts("run error\n"); } else { puts("starting from entry point: "); putxval((unsigned long)entry_point, 0); puts("\n"); f = (void (*)(void))entry_point; f(); /* not reach here */ } } else { puts("unknown.\n"); } } return 0; }
static int get_dynamic_tag(Context * ctx, ELF_File * file, int tag, ContextAddress * addr) { unsigned i, j; for (i = 1; i < file->section_cnt; i++) { ELF_Section * sec = file->sections + i; if (sec->size == 0) continue; if (sec->name == NULL) continue; if (strcmp(sec->name, ".dynamic") == 0) { ContextAddress sec_addr = elf_map_to_run_time_address(ctx, file, sec, (ContextAddress)sec->addr); if (errno) return -1; if (elf_load(sec) < 0) return -1; if (file->elf64) { unsigned cnt = (unsigned)(sec->size / sizeof(Elf64_Dyn)); for (j = 0; j < cnt; j++) { Elf64_Dyn dyn = *((Elf64_Dyn *)sec->data + j); if (file->byte_swap) SWAP(dyn.d_tag); if (dyn.d_tag == DT_NULL) break; if (dyn.d_tag == tag) { if (context_read_mem(ctx, sec_addr + j * sizeof(dyn), &dyn, sizeof(dyn)) < 0) return -1; if (file->byte_swap) { SWAP(dyn.d_tag); SWAP(dyn.d_un.d_ptr); } if (dyn.d_tag != tag) continue; if (addr != NULL) *addr = (ContextAddress)dyn.d_un.d_ptr; return 0; } } } else { unsigned cnt = (unsigned)(sec->size / sizeof(Elf32_Dyn)); for (j = 0; j < cnt; j++) { Elf32_Dyn dyn = *((Elf32_Dyn *)sec->data + j); if (file->byte_swap) SWAP(dyn.d_tag); if (dyn.d_tag == DT_NULL) break; if (dyn.d_tag == tag) { if (context_read_mem(ctx, sec_addr + j * sizeof(dyn), &dyn, sizeof(dyn)) < 0) return -1; if (file->byte_swap) { SWAP(dyn.d_tag); SWAP(dyn.d_un.d_ptr); } if (dyn.d_tag != tag) continue; if (addr != NULL) *addr = (ContextAddress)dyn.d_un.d_ptr; return 0; } } } } } errno = ENOENT; return -1; }
int main(void) { static char buf[16]; static long size = -1; static unsigned char *loadbuf = NULL; extern int buffer_start; /* This buffer is defined in the linker script */ char *entry_point; void (*f)(void); INTR_DISABLE; /* disable interruption */ init(); puts("Hello World!\n"); puts("kzload (kozos boot loader) started.\n"); while (1) { puts("kzload> "); /* display of prompt */ gets(buf); /* receipt of commands via serial */ if (!strcmp(buf, "load")) { /* file download in XMODEM */ loadbuf = (char *)(&buffer_start); size = xmodem_recv(loadbuf); wait(); /* Wait until transfer application terminates and terminal console can be controled. */ if (size < 0) { puts("\nXMODEM receive error!\n"); } else { puts("\nXMODEM receive succeeded.\n"); } } else if (!strcmp(buf, "dump")) { /* hexadecimal dump of a memory */ puts("size: "); putxval(size, 0); puts("\n"); dump(loadbuf, size); } else if (!strcmp(buf, "run")) { /* execute ELF files */ entry_point = elf_load(loadbuf); /* load ELF files to a memory */ if (!entry_point) { puts("run error!\n"); } else { puts("starting from entry point: "); putxval((unsigned long)entry_point, 0); puts("\n"); f = (void (*)(void))entry_point; f(); /* operation is switched to the loaded program at this point */ } } else { puts("unknown.\n"); } } return 0; }
int main( void ) { static char buf[16]; static long size = -1; static unsigned char* loadbuf = NULL; extern int buffer_start; /* buffer defined at ld.scr */ init(); puts("kzload (kozos boot loader) started.\n"); while(1) { puts("kzload> "); gets(buf); if(strcmp(buf, "load") == 0) { loadbuf = (char*)(&buffer_start); size = xmodem_recv(loadbuf); wait(); if(size < 0) { puts("\nXMODEM receive error!\n"); } else { puts("\nXMODEM receive succeeded.\n"); } } else if(strcmp(buf, "dump") == 0) { puts("size: "); putxval(size, 0); puts("\n"); dump(loadbuf, size); } else if(strcmp(buf, "run") == 0) { elf_load(loadbuf); } else { puts("unknown.\n"); } } return 0; }
void modules_init(struct multiboot *mb) { if(mb->mbs_mods_count) { printk("Modules are currently not paging compatible, skipping\n"); return; struct mb_module *modules=mb->mbs_mods_addr; size_t i; for(i=0;i<mb->mbs_mods_count;i++) { elf_load(modules[i].start,modules[i].end-modules[i].start); } } }
unsigned int exec(char *name, char **argv, char **envp) { int argc = 0; /* locate file */ struct rdfs_file *f = rdfs_getfile(name); if(!f){ printk("could not exec file '%s'. file does not exist.\n",name); return 0; } /* load file */ void *entry_point = elf_load(f->data); if(!entry_point){ printk("could not exec file '%s'. file could not be loaded.\n",name); return 0; } /* setup thread */ struct thread_struct *t = create_thread(); t->ctx.pc = (long) entry_point; memset(t->stack,0,THREAD_STACK_SIZE); if(argv != NULL) while(argv[++argc] != NULL); /* Count number of arguments */ // these are cpu specific and should be moved somewhere else #ifdef CPU_IS_68k t->ctx.sp = (long) t->stack; t->ctx.sr = 0x2000; /* args for main() */ t->ctx.d1 = (long) argc; t->ctx.d2 = (long) argv; t->ctx.d3 = (long) envp; #endif #ifdef CPU_IS_PPC t->ctx.r1 = (long) t->stack; t->ctx.r3 = (long) argc; t->ctx.r4 = (long) argv; t->ctx.r5 = (long) envp; #endif t->state = THREAD_READY; return t->id; }
int main() { L4_Word_t a; L4_Word_t b; L4_Word_t c; cleanimg(); elf_load((L4_Word_t)binary_user_image_start,(L4_Word_t)binary_user_image_start+binary_user_image_size,&a,&b,&c); makeimg(); return 0; }
int main(void) { extern int program_buffer; memset(&program_buffer, 0x00, 0x001d00); init(); puts("kzload (kozos boot loader) started.\n"); while(1){ static long program_size = -1; #define CommandSize 16 static char command_buff[CommandSize]; memset(command_buff, 0x00, CommandSize); puts("kzload> "); gets(command_buff); #define MATCH(command) (strcmp(command_buff,command) == 0) if(MATCH("load")){ // XMODEMでファイルをダウンロード memset(&program_buffer, 0x00, 0x001d00); program_size = receive_program((unsigned char*)&program_buffer); } else if(MATCH("dump")){ puts("size: "); putxval(program_size, 12); puts("\n"); dump_h((unsigned char*)&program_buffer, program_size); } else if(MATCH("run")) { const char *ep = elf_load((unsigned char*)&program_buffer); const void (*entry_point)(void) = (void(*)(void))ep; if( entry_point == NULL ){ puts("no entry_point.\n"); } else { puts("start at 0x"); putxval(entry_point, 6); puts("\n"); entry_point(); } } else { puts("unknown command.\n"); } } // end of while(1) return 0; }
int main(int argc, char *argv[]) { struct elf_data elf; int ret; printf("\n"); if (argc < 2) { printf("Please specify an ELF file on the command-line\n\n"); printf(" test \"elffile\"\n"); printf(" where \"elffile\" is the filename of the ELF file to investigate\n"); } elf.filename = argv[1]; elf.verbose = 1; ret = elf_load(&elf); if (ret) { switch(ret) { case ERR_ELF_FILE_OPEN: printf("File open failed\n"); break; case ERR_ELF_FILE_PREMATURE_EOF: printf("File open failed\n"); break; case ERR_ELF_FILE_READ: printf("File open failed\n"); break; case ERR_ELF_INCOMPATIBLE: printf("Incompatibke file\n"); break; case ERR_ELF_MEMORY_ALLOCATION: printf("Memory allocation error\n"); break; default: printf("Unknown error: %d\n", ret); break; } return(ret); } elf_print_header(&elf); elf_print_sections(&elf); return(0); }
static void execute_elf(void *arg) { int fd, size, i, j; void *entry; //extern void *end; void *elf_begin = ((void *)(&end)) + 4*1024; uint8_t *tmp = elf_begin; printf("Load file to RAM\n"); fd = fat_open(path, O_RDONLY); size = fat_fsize(fd); for(j = 512; j < size; j += 512) { fat_read_sect(fd); for(i = 0; i < 512; i++) { *tmp++ = fat_buf[i]; } } fat_read_sect(fd); for(i = 0; i < j - size; i++) { *tmp++ = fat_buf[i]; } fat_close(fd); printf("File loaded\n"); printf("Highest addr used is 0x%X\n", tmp); //input_poll(); mmu040_init(); terminal_clear(); printf("MMU Init\n"); if(!(entry = elf_load(elf_begin, do_debug))) { printf("Failed to load ELF\n"); input_poll(); return; } printf("ELF load successful, entry is 0x%X, press any key\n", entry); //input_poll(); //printf("Here we have 0x%X\n", *((uint32_t *) entry)); //input_poll(); mmu_disable(); if(do_debug) { char *argv[] = {"debug"}; mmu_enable_and_jump(entry, 1, argv); } else { mmu_enable_and_jump(entry, 0, NULL); } }
static void load_init_image( struct startup_l3_info* l3i, 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 ELF64 binary */ struct multiboot_header_tag *multiboot = (struct multiboot_header_tag *) local_phys_to_mem( armv8_glbl_core_data->multiboot_image.base); struct multiboot_tag_module_64 *module = multiboot2_find_module_64( multiboot, armv8_glbl_core_data->multiboot_image.length, 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_AARCH64, startup_alloc_init, l3i, 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 Elf64_Shdr* got_shdr = elf64_find_section_header_name((lvaddr_t)elf_base, elf_bytes, ".got"); if (got_shdr) { *got_base = got_shdr->sh_addr; } }
void run_start( void ) { char *entry_point; void (*f)(void); entry_point = elf_load(loadbuf); /* メモリ上に展開(ロード) */ puts("entry point: "); putxval((unsigned long)entry_point, 0); if (!entry_point) { puts("\nrun error!\n"); } else { puts("\nstarting from entry point: "); putxval((unsigned long)entry_point, 0); puts("\n"); f = (void (*)(void))entry_point; f(); /* ここで,ロードしたプログラムに処理を渡す */ /* ここには返ってこない */ } }
static void load_init_image( struct startup_l2_info* l2i, const uint8_t* initrd_base, size_t initrd_bytes, genvaddr_t* init_ep, genvaddr_t* got_base ) { const uint8_t* elf_base; size_t elf_bytes; int found; STARTUP_PROGRESS(); *init_ep = *got_base = 0; found = cpio_get_file_by_name(initrd_base, initrd_bytes, BSP_INIT_MODULE_NAME, &elf_base, &elf_bytes); if (!found) { panic("Failed to find " BSP_INIT_MODULE_NAME "\n"); } debug(SUBSYS_STARTUP, "load_init_image %p %08x\n", initrd_base, initrd_bytes); errval_t err = elf_load(EM_ARM, startup_alloc_init, l2i, (lvaddr_t)elf_base, elf_bytes, init_ep); if (err_is_fail(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; } }
unsigned int proc_create(void *elf_addr, unsigned int quota) { unsigned int pid, id; id = get_curid(); pid = thread_spawn((void *) proc_start_user, id, quota); elf_load(elf_addr, pid); uctx_pool[pid].es = CPU_GDT_UDATA | 3; uctx_pool[pid].ds = CPU_GDT_UDATA | 3; uctx_pool[pid].cs = CPU_GDT_UCODE | 3; uctx_pool[pid].ss = CPU_GDT_UDATA | 3; uctx_pool[pid].esp = VM_USERHI; uctx_pool[pid].eflags = FL_IF; uctx_pool[pid].eip = elf_entry(elf_addr); seg_init_proc(get_pcpu_idx(), pid); return pid; }
int bootxxx(void *readsector, void *disklabel, osdsc_t *od) { int fd; char *errmsg; extern char end[], edata[]; memset(edata, 0, end - edata); /* XXX: Limit should be 16MB */ setheap(end, (void*)0x1000000); printf("\033v\nNetBSD/Atari tertiary bootloader " "($Revision: 1.8 $)\n\n"); if (init_dskio(readsector, disklabel, od->rootfs)) return -1; sys_info(od); if (!(od->cputype & ATARI_ANYCPU)) { printf("Unknown CPU-type.\n"); return -2; } if ((fd = open(od->osname, 0)) < 0) { printf("Cannot open kernel '%s'\n", od->osname); return -3; } #ifndef __ELF__ /* a.out */ if (aout_load(fd, od, &errmsg, 1) != 0) #else if (elf_load(fd, od, &errmsg, 1) != 0) #endif return -4; boot_BSD(&od->kp); return -5; /* NOTREACHED */ }
int main(void) { static char buf[16]; static long size = -1; static unsigned char *loadbuf = NULL; extern int buffer_start; /* リンカ・スクリプトで定義されているバッファ */ init(); puts("kzload (kozos boot loader) started.\n"); while (1) { puts("kzload> "); /* プロンプト表示 */ gets(buf); /* シリアルからのコマンド受信 */ if (!strcmp(buf, "load")) { /* XMODEMでのファイルのダウンロード */ loadbuf = (char *)(&buffer_start); size = xmodem_recv(loadbuf); wait(); /* 転送アプリが終了し端末アプリに制御が戻るまで待ち合わせる */ if (size < 0) { puts("\nXMODEM receive error!\n"); } else { puts("\nXMODEM receive succeeded.\n"); } } else if (!strcmp(buf, "dump")) { /* メモリの16進ダンプ出力 */ puts("size: "); putxval(size, 0); puts("\n"); dump(loadbuf, size); } else if (!strcmp(buf, "run")) { /* ELF形式ファイルの実行 */ elf_load(loadbuf); /* メモリ上に展開(ロード) */ } else { puts("unknown.\n"); } } return 0; }
uint32_t elf_load_fd(struct sys_state *sys, int fd, int flags, struct elf_load_info *info) { uint32_t entry; stat_s stat; void *base; int err; err = sys_fstat(fd, &stat); if (err) { debug("Failed to stat ELF (%d)\n", err); sys_exit(1); } base = sys_mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (IS_ERROR(base)) { debug("Failed to mmap ELF file\n"); sys_exit(1); } entry = elf_load(sys, base, flags, info); sys_munmap(base, stat.st_size); return entry; }
int16 command_run (void) { int8 *entry_point; void (*f) (void); entry_point = elf_load (xmodem_recv_buffer_start_address); if (!entry_point) { put_string ("run error!\n"); } else { put_string ("starting from entry point:"); put_hex ((uint32) entry_point, 6); put_string ("\n"); put_string ("\n"); f = (void (*) (void)) entry_point; f (); } return 0; }
struct dcb *spawn_app_init(struct arm_core_data *core_data, const char *name, alloc_phys_func alloc_phys) { errval_t err; /* Construct cmdline args */ // Core id of the core that booted this core char coreidchar[10]; snprintf(coreidchar, sizeof(coreidchar), "%d", core_data->src_core_id); // IPI channel id of core that booted this core char chanidchar[30]; snprintf(chanidchar, sizeof(chanidchar), "chanid=%"PRIu32, core_data->chan_id); // Arch id of the core that booted this core char archidchar[30]; snprintf(archidchar, sizeof(archidchar), "archid=%d", core_data->src_arch_id); const char *argv[5] = { name, coreidchar, chanidchar, archidchar }; int argc = 4; struct dcb *init_dcb = spawn_init_common(name, argc, argv,0, alloc_phys); // Urpc frame cap struct cte *urpc_frame_cte = caps_locate_slot(CNODE(spawn_state.taskcn), TASKCN_SLOT_MON_URPC); // XXX: Create as devframe so the memory is not zeroed out err = caps_create_new(ObjType_DevFrame, core_data->urpc_frame_base, core_data->urpc_frame_bits, core_data->urpc_frame_bits, urpc_frame_cte); assert(err_is_ok(err)); urpc_frame_cte->cap.type = ObjType_Frame; lpaddr_t urpc_ptr = gen_phys_to_local_phys(urpc_frame_cte->cap.u.frame.base); /* Map urpc frame at MON_URPC_BASE */ spawn_init_map(init_l2, INIT_VBASE, MON_URPC_VBASE, urpc_ptr, MON_URPC_SIZE, INIT_PERM_RW); struct startup_l2_info l2_info = { init_l2, INIT_VBASE }; // elf load the domain genvaddr_t entry_point, got_base=0; err = elf_load(EM_ARM, startup_alloc_init, &l2_info, local_phys_to_mem(core_data->monitor_binary), core_data->monitor_binary_size, &entry_point); if (err_is_fail(err)) { //err_print_calltrace(err); panic("ELF load of init module failed!"); } // TODO: Fix application linkage so that it's non-PIC. struct Elf32_Shdr* got_shdr = elf32_find_section_header_name(local_phys_to_mem(core_data->monitor_binary), core_data->monitor_binary_size, ".got"); if (got_shdr) { got_base = got_shdr->sh_addr; } struct dispatcher_shared_arm *disp_arm = get_dispatcher_shared_arm(init_dcb->disp); disp_arm->enabled_save_area.named.r10 = got_base; disp_arm->got_base = got_base; disp_arm->disabled_save_area.named.pc = entry_point; #ifndef __ARM_ARCH_7M__ //the armv7-m profile does not have such a mode field disp_arm->disabled_save_area.named.cpsr = ARM_MODE_USR | CPSR_F_MASK; #endif disp_arm->disabled_save_area.named.r10 = got_base; //disp_arm->disabled_save_area.named.rtls = INIT_DISPATCHER_VBASE; return init_dcb; }
int sys_execve(const char *filename, char *const argv[], char *const envp[]){ __UNUSED_VARIABLE(envp); int ret; unsigned int sp; unsigned int *tmp_sp; char *tmp_argv[6]; unsigned int argv_pos; int argc, i, len; int stack_end; ret = elf_load(filename); if(ret < 0) return ret; stack_end = ret; Task *prev_task = Kernel::getInstance()->taskmanager.getCurrentTask(); Task *current = Kernel::getInstance()->taskmanager.getTask(); if(current == NULL){ lcd_printf("Task Allocation Error\n"); for(;;); } sp = 0x80000; // max memory size argc = 0; while(argv[argc] != 0) argc++; for(i = 0; i < argc; i++){ len = strlen(argv[i]) + 1; sp -= len; sp &= 0xfffffff0; //16 byte alignment tmp_argv[i] = (char *)sp; memcpy((void *)sp, argv[i], len); } sp -= 4 * (argc + 1); // num of argv and argv[argc] argv_pos = sp; tmp_sp = (unsigned int *)sp; for(i = 0; i < argc; i++){ *tmp_sp = (unsigned int)tmp_argv[i]; tmp_sp++; } *tmp_sp = 0; sp -= 4*3; // argc, argv, envp tmp_sp = (unsigned int *)sp; *tmp_sp++ = argc; *tmp_sp++ = argv_pos; //argv *tmp_sp = 0; //envp current->tss.sp = sp; current->tss.ra = 0x20000; current->tss.cp0_status = 0; current->tss.cp0_epc = 0; current->tss.cp0_cause = 0; current->stack_start = sp; current->stack_end = stack_end; current->brk = stack_end; invalidate_icache(); invalidate_dcache(); Kernel::getInstance()->taskmanager.switchContext(prev_task, current); return 0; }
int main(int argc, char *argv[]) { void *tarfile_addr; size_t tarfile_length; void *elf_file_addr; size_t elf_file_length; void *free_space; uint32_t cr3; char *pfree; kprintf("Loading the Anvil...\n"); parse_cmd_line(argc, argv); cpu_init(); /* * Get all the Grub info now so we can touch memory and it won't matter * too much if we trash it. */ mbi_mem_get_info(); free_space = mbi_module_get_info(&tarfile_addr, &tarfile_length); // free_space = 0x80100000; // tarfile_addr = (uintptr_t)0x80012000; // tarfile_length = 3150336; sysinfo.initrd_info.base_addr = (uintptr_t)tarfile_addr; sysinfo.initrd_info.length = tarfile_length; get_sysinfo(); /* Load the OS. We'll be loading 2 files, the kernel and init */ pfree = free_space; kprintf("\nLoading kernel\n========================\n"); /* Assume the module is a TAR file and search it for the files */ if (tar_search(tarfile_addr, tarfile_length, "anvil-knl", &elf_file_addr, &elf_file_length) == -1) { kprintf("Couldn't untar kernel\n"); return 0; } /* Now load the kernel. It should be a single segment */ if (elf_load(pfree, elf_file_addr, &sysinfo.kernel_program_info) == -1) { return 0; } sysinfo.kernel_program_info.seginfo[0].paddr = (uintptr_t)pfree; /* Skip over the kernel (and an 8 page stack */ pfree += sysinfo.kernel_program_info.seginfo[0].length + 8 * __PAGESIZE; kprintf("\nLoading init\n========================\n"); /* Assume the module is a TAR file and search it for the pieces */ if (tar_search(tarfile_addr, tarfile_length, "init", &elf_file_addr, &elf_file_length) == 0) { /* Load init */ if (elf_load(pfree, elf_file_addr, &sysinfo.init_program_info) == -1) { return 0; } } else { kprintf("init not found, ignoring\n"); } sysinfo.init_program_info.seginfo[0].paddr = (uintptr_t)pfree; sysinfo.init_program_info.seginfo[1].paddr = (uintptr_t)pfree + sysinfo.init_program_info.seginfo[0].length; vbe_init(); cr3 = paging_init(free_space, sysinfo.kernel_program_info.seginfo[0].length); cpu_switch_longmode(cr3); /* * The return value tells the code in boot.S what to do * * 0: just hang * 1: launch 32bit kernel * 2: launch 64bit kernel * * any other value just hang */ return 2; }
static errval_t elf_allocate(void *state, genvaddr_t base, size_t size, uint32_t flags, void **retbase) { errval_t err; struct spawninfo *si = state; // Increase size by space wasted on first page due to page-alignment size_t base_offset = BASE_PAGE_OFFSET(base); size += base_offset; base -= base_offset; // Page-align size = ROUND_UP(size, BASE_PAGE_SIZE); cslot_t vspace_slot = si->elfload_slot; // Allocate the frames size_t sz = 0; for (lpaddr_t offset = 0; offset < size; offset += sz) { sz = 1UL << log2floor(size - offset); struct capref frame = { .cnode = si->segcn, .slot = si->elfload_slot++, }; err = frame_create(frame, sz, NULL); if (err_is_fail(err)) { return err_push(err, LIB_ERR_FRAME_CREATE); } } cslot_t spawn_vspace_slot = si->elfload_slot; cslot_t new_slot_count = si->elfload_slot - vspace_slot; // create copies of the frame capabilities for spawn vspace for (int copy_idx = 0; copy_idx < new_slot_count; copy_idx++) { struct capref frame = { .cnode = si->segcn, .slot = vspace_slot + copy_idx, }; struct capref spawn_frame = { .cnode = si->segcn, .slot = si->elfload_slot++, }; err = cap_copy(spawn_frame, frame); if (err_is_fail(err)) { // TODO: make debug printf printf("cap_copy failed for src_slot = %"PRIuCSLOT", dest_slot = %"PRIuCSLOT"\n", frame.slot, spawn_frame.slot); return err_push(err, LIB_ERR_CAP_COPY); } } /* Map into my vspace */ struct memobj *memobj = malloc(sizeof(struct memobj_anon)); if (!memobj) { return LIB_ERR_MALLOC_FAIL; } struct vregion *vregion = malloc(sizeof(struct vregion)); if (!vregion) { return LIB_ERR_MALLOC_FAIL; } // Create the objects err = memobj_create_anon((struct memobj_anon*)memobj, size, 0); if (err_is_fail(err)) { return err_push(err, LIB_ERR_MEMOBJ_CREATE_ANON); } err = vregion_map(vregion, get_current_vspace(), memobj, 0, size, VREGION_FLAGS_READ_WRITE); if (err_is_fail(err)) { return err_push(err, LIB_ERR_VSPACE_MAP); } for (lvaddr_t offset = 0; offset < size; offset += sz) { sz = 1UL << log2floor(size - offset); struct capref frame = { .cnode = si->segcn, .slot = vspace_slot++, }; genvaddr_t genvaddr = vspace_lvaddr_to_genvaddr(offset); err = memobj->f.fill(memobj, genvaddr, frame, sz); if (err_is_fail(err)) { return err_push(err, LIB_ERR_MEMOBJ_FILL); } err = memobj->f.pagefault(memobj, vregion, offset, 0); if (err_is_fail(err)) { DEBUG_ERR(err, "lib_err_memobj_pagefault_handler"); return err_push(err, LIB_ERR_MEMOBJ_PAGEFAULT_HANDLER); } } /* Map into spawn vspace */ struct memobj *spawn_memobj = NULL; struct vregion *spawn_vregion = NULL; err = spawn_vspace_map_anon_fixed_attr(si, base, size, &spawn_vregion, &spawn_memobj, elf_to_vregion_flags(flags)); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_VSPACE_MAP); } for (lvaddr_t offset = 0; offset < size; offset += sz) { sz = 1UL << log2floor(size - offset); struct capref frame = { .cnode = si->segcn, .slot = spawn_vspace_slot++, }; genvaddr_t genvaddr = vspace_lvaddr_to_genvaddr(offset); err = memobj->f.fill(spawn_memobj, genvaddr, frame, sz); if (err_is_fail(err)) { return err_push(err, LIB_ERR_MEMOBJ_FILL); } err = spawn_memobj->f.pagefault(spawn_memobj, spawn_vregion, offset, 0); if (err_is_fail(err)) { DEBUG_ERR(err, "lib_err_memobj_pagefault_handler"); return err_push(err, LIB_ERR_MEMOBJ_PAGEFAULT_HANDLER); } } genvaddr_t genvaddr = vregion_get_base_addr(vregion) + base_offset; *retbase = (void*)vspace_genvaddr_to_lvaddr(genvaddr); return SYS_ERR_OK; } /** * \brief Load the elf image */ errval_t spawn_arch_load(struct spawninfo *si, lvaddr_t binary, size_t binary_size, genvaddr_t *entry, void** arch_info) { errval_t err; // Reset the elfloader_slot si->elfload_slot = 0; struct capref cnode_cap = { .cnode = si->rootcn, .slot = ROOTCN_SLOT_SEGCN, }; err = cnode_create_raw(cnode_cap, &si->segcn, DEFAULT_CNODE_SLOTS, NULL); if (err_is_fail(err)) { return err_push(err, SPAWN_ERR_CREATE_SEGCN); } // TLS is NYI si->tls_init_base = 0; si->tls_init_len = si->tls_total_len = 0; // Load the binary err = elf_load(EM_HOST, elf_allocate, si, binary, binary_size, entry); if (err_is_fail(err)) { return err; } struct Elf32_Shdr* got_shdr = elf32_find_section_header_name(binary, binary_size, ".got"); if (got_shdr) { *arch_info = (void*)got_shdr->sh_addr; } else { return SPAWN_ERR_LOAD; } return SYS_ERR_OK; } void spawn_arch_set_registers(void *arch_load_info, dispatcher_handle_t handle, arch_registers_state_t *enabled_area, arch_registers_state_t *disabled_area) { assert(arch_load_info != NULL); uintptr_t got_base = (uintptr_t)arch_load_info; struct dispatcher_shared_arm* disp_arm = get_dispatcher_shared_arm(handle); disp_arm->got_base = got_base; enabled_area->regs[REG_OFFSET(PIC_REGISTER)] = got_base; disabled_area->regs[REG_OFFSET(PIC_REGISTER)] = got_base; #ifndef __ARM_ARCH_7M__ //armv7-m does not support these flags enabled_area->named.cpsr = CPSR_F_MASK | ARM_MODE_USR; disabled_area->named.cpsr = CPSR_F_MASK | ARM_MODE_USR; #endif }