From 707c6653985f55463e8a59a58b248ba8af3b44b2 Mon Sep 17 00:00:00 2001 From: Minep Date: Mon, 2 Jan 2023 14:25:22 +0000 Subject: [PATCH] regression: elf loading --- lunaix-os/includes/lunaix/spike.h | 68 +++++++++++++++--------------- lunaix-os/kernel/asm/x86/pfault.c | 2 +- lunaix-os/kernel/loader/elf.c | 21 +++++---- lunaix-os/kernel/loader/exec.c | 16 ++++--- lunaix-os/kernel/proc0.c | 2 +- lunaix-os/kernel/process/process.c | 2 +- lunaix-os/uprog/init.c | 6 +-- lunaix-os/usr/uwrap.S | 1 + 8 files changed, 64 insertions(+), 54 deletions(-) diff --git a/lunaix-os/includes/lunaix/spike.h b/lunaix-os/includes/lunaix/spike.h index f87b00a..6cece75 100644 --- a/lunaix-os/includes/lunaix/spike.h +++ b/lunaix-os/includes/lunaix/spike.h @@ -27,40 +27,40 @@ * */ #define ILOG2(x) \ - __builtin_constant_p(x) ? ((x) == 0 ? 0 \ - : ((x) & (1ul << 31)) ? 31 \ - : ((x) & (1ul << 30)) ? 30 \ - : ((x) & (1ul << 29)) ? 29 \ - : ((x) & (1ul << 28)) ? 28 \ - : ((x) & (1ul << 27)) ? 27 \ - : ((x) & (1ul << 26)) ? 26 \ - : ((x) & (1ul << 25)) ? 25 \ - : ((x) & (1ul << 24)) ? 24 \ - : ((x) & (1ul << 23)) ? 23 \ - : ((x) & (1ul << 22)) ? 22 \ - : ((x) & (1ul << 21)) ? 21 \ - : ((x) & (1ul << 20)) ? 20 \ - : ((x) & (1ul << 19)) ? 19 \ - : ((x) & (1ul << 18)) ? 18 \ - : ((x) & (1ul << 17)) ? 17 \ - : ((x) & (1ul << 16)) ? 16 \ - : ((x) & (1ul << 15)) ? 15 \ - : ((x) & (1ul << 14)) ? 14 \ - : ((x) & (1ul << 13)) ? 13 \ - : ((x) & (1ul << 12)) ? 12 \ - : ((x) & (1ul << 11)) ? 11 \ - : ((x) & (1ul << 10)) ? 10 \ - : ((x) & (1ul << 9)) ? 9 \ - : ((x) & (1ul << 8)) ? 8 \ - : ((x) & (1ul << 7)) ? 7 \ - : ((x) & (1ul << 6)) ? 6 \ - : ((x) & (1ul << 5)) ? 5 \ - : ((x) & (1ul << 4)) ? 4 \ - : ((x) & (1ul << 3)) ? 3 \ - : ((x) & (1ul << 2)) ? 2 \ - : ((x) & (1ul << 1)) ? 1 \ - : 0) \ - : (31 - __builtin_clz(x)) + (__builtin_constant_p(x) ? ((x) == 0 ? 0 \ + : ((x) & (1ul << 31)) ? 31 \ + : ((x) & (1ul << 30)) ? 30 \ + : ((x) & (1ul << 29)) ? 29 \ + : ((x) & (1ul << 28)) ? 28 \ + : ((x) & (1ul << 27)) ? 27 \ + : ((x) & (1ul << 26)) ? 26 \ + : ((x) & (1ul << 25)) ? 25 \ + : ((x) & (1ul << 24)) ? 24 \ + : ((x) & (1ul << 23)) ? 23 \ + : ((x) & (1ul << 22)) ? 22 \ + : ((x) & (1ul << 21)) ? 21 \ + : ((x) & (1ul << 20)) ? 20 \ + : ((x) & (1ul << 19)) ? 19 \ + : ((x) & (1ul << 18)) ? 18 \ + : ((x) & (1ul << 17)) ? 17 \ + : ((x) & (1ul << 16)) ? 16 \ + : ((x) & (1ul << 15)) ? 15 \ + : ((x) & (1ul << 14)) ? 14 \ + : ((x) & (1ul << 13)) ? 13 \ + : ((x) & (1ul << 12)) ? 12 \ + : ((x) & (1ul << 11)) ? 11 \ + : ((x) & (1ul << 10)) ? 10 \ + : ((x) & (1ul << 9)) ? 9 \ + : ((x) & (1ul << 8)) ? 8 \ + : ((x) & (1ul << 7)) ? 7 \ + : ((x) & (1ul << 6)) ? 6 \ + : ((x) & (1ul << 5)) ? 5 \ + : ((x) & (1ul << 4)) ? 4 \ + : ((x) & (1ul << 3)) ? 3 \ + : ((x) & (1ul << 2)) ? 2 \ + : ((x) & (1ul << 1)) ? 1 \ + : 0) \ + : (31 - __builtin_clz(x))) #define __USER__ __attribute__((section(".usrtext"))) diff --git a/lunaix-os/kernel/asm/x86/pfault.c b/lunaix-os/kernel/asm/x86/pfault.c index 0d5131f..f6c4355 100644 --- a/lunaix-os/kernel/asm/x86/pfault.c +++ b/lunaix-os/kernel/asm/x86/pfault.c @@ -89,7 +89,7 @@ intr_routine_page_fault(const isr_param* param) if (hit_region->mfile && !PG_IS_PRESENT(*pte)) { struct v_file* file = hit_region->mfile; u32_t offset = - (ptr - hit_region->start) & (PG_SIZE - 1) + hit_region->foff; + ((ptr - hit_region->start) & (PG_SIZE - 1)) + hit_region->foff; uintptr_t pa = pmm_alloc_page(__current->pid, 0); if (!pa) { diff --git a/lunaix-os/kernel/loader/elf.c b/lunaix-os/kernel/loader/elf.c index c7467aa..241ddf9 100644 --- a/lunaix-os/kernel/loader/elf.c +++ b/lunaix-os/kernel/loader/elf.c @@ -8,23 +8,24 @@ #include int -__elf_populate_mapped(struct mm_region* region, void* pg, off_t offset) +__elf_populate_mapped(struct mm_region* region, void* pg, off_t segfoff) { size_t segsz = region->flen; - size_t segoff = offset - region->foff; + size_t segmoff = segfoff - region->foff; - if (segoff >= segsz) { + if (segmoff >= segsz) { return 0; } struct v_file* file = region->mfile; - size_t rdlen = MIN(segsz - segoff, PG_SIZE); + size_t rdlen = MIN(segsz - segmoff, PG_SIZE); if (rdlen == PG_SIZE) { // This is because we want to exploit any optimization on read_page - return file->ops->read_page(file->inode, pg, PG_SIZE, offset); + return file->ops->read_page(file->inode, pg, PG_SIZE, segfoff); } else { - return file->ops->read(file->inode, pg, rdlen, offset); + // we don't want to over-read the segment! + return file->ops->read(file->inode, pg, rdlen, segfoff); } } @@ -82,7 +83,11 @@ elf_setup_mapping(struct ld_param* ldparam, } tbl_sz = 1 << ILOG2(tbl_sz); - phdrs = elfile->ops->read(elfile->inode, phdrs, tbl_sz, ehdr->e_phoff); + status = elfile->ops->read(elfile->inode, phdrs, tbl_sz, ehdr->e_phoff); + + if (status < 0) { + goto done; + } if (PG_ALIGN(phdrs[0].p_va) != USER_START) { status = ENOEXEC; @@ -122,7 +127,7 @@ elf_load(struct ld_param* ldparam, struct v_file* elfile) struct elf32_ehdr* ehdr = valloc(SIZE_EHDR); int status = elfile->ops->read(elfile->inode, ehdr, SIZE_EHDR, 0); - if (status) { + if (status < 0) { goto done; } diff --git a/lunaix-os/kernel/loader/exec.c b/lunaix-os/kernel/loader/exec.c index 1c206d2..cf0a422 100644 --- a/lunaix-os/kernel/loader/exec.c +++ b/lunaix-os/kernel/loader/exec.c @@ -60,6 +60,8 @@ __exec_remap_heap(struct ld_param* param, struct proc_mm* pvms) heap->region_copied = __heap_copied; mm_index((void**)&pvms->heap, heap); + + return status; } int @@ -112,15 +114,17 @@ exec_load(struct ld_param* param, if (envp) memcpy(arg_start + sz_argv, (void*)envp, sz_envp); - struct usr_exec_param* param = mapped; - *param = (struct usr_exec_param){ .argc = argv_len, - .argv = arg_start, - .envc = envp_len, - .envp = arg_start + sz_argv, - .info = param->info }; ptr_t* ustack = (ptr_t*)USTACK_TOP; + struct usr_exec_param* exec_param = mapped; + ustack[-1] = (ptr_t)mapped; param->info.stack_top = &ustack[-1]; + + *exec_param = (struct usr_exec_param){ .argc = argv_len, + .argv = arg_start, + .envc = envp_len, + .envp = arg_start + sz_argv, + .info = param->info }; } else { // TODO need to find a way to inject argv and envp remotely fail("not implemented"); diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index ef97c77..abd0ac0 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -67,7 +67,7 @@ exec_initd() { int errno = 0; struct ld_param param; - char filename[] = "/mnt/lunaix-os/usr/initd"; + char filename[] = "/mnt/lunaix-os/usr/init"; ld_create_param(¶m, __current, VMS_SELF); diff --git a/lunaix-os/kernel/process/process.c b/lunaix-os/kernel/process/process.c index 414a840..23ff6a4 100644 --- a/lunaix-os/kernel/process/process.c +++ b/lunaix-os/kernel/process/process.c @@ -162,7 +162,7 @@ init_proc_user_space(struct proc_info* pcb) } mapped->region_copied = __stack_copied; - mm_index((void**)&pcb->mm.heap, mapped); + mm_index((void**)&pcb->mm.stack, mapped); // TODO other uspace initialization stuff diff --git a/lunaix-os/uprog/init.c b/lunaix-os/uprog/init.c index a197245..0d1eebc 100644 --- a/lunaix-os/uprog/init.c +++ b/lunaix-os/uprog/init.c @@ -7,17 +7,17 @@ main(int argc, const char** argv) { int errno = 0; - if ((errno = open("/dev/tty", 0))) { + if ((errno = open("/dev/tty", 0)) < 0) { syslog(2, "fail to open tty (%d)\n", errno); return 0; } - if ((errno = dup(errno))) { + if ((errno = dup(errno)) < 0) { syslog(2, "fail to setup tty i/o (%d)\n", errno); return 0; } - syslog(0, "user space!\n"); + syslog(0, "(%p) user space!\n", main); return 0; } \ No newline at end of file diff --git a/lunaix-os/usr/uwrap.S b/lunaix-os/usr/uwrap.S index 1f07722..1cd6515 100644 --- a/lunaix-os/usr/uwrap.S +++ b/lunaix-os/usr/uwrap.S @@ -4,6 +4,7 @@ .section .text .global _u_start _u_start: + movl $2, %eax call usr_pre_init jnz 1f -- 2.27.0