regression: elf loading
authorMinep <zelong56@gmail.com>
Mon, 2 Jan 2023 14:25:22 +0000 (14:25 +0000)
committerMinep <zelong56@gmail.com>
Mon, 2 Jan 2023 14:25:22 +0000 (14:25 +0000)
lunaix-os/includes/lunaix/spike.h
lunaix-os/kernel/asm/x86/pfault.c
lunaix-os/kernel/loader/elf.c
lunaix-os/kernel/loader/exec.c
lunaix-os/kernel/proc0.c
lunaix-os/kernel/process/process.c
lunaix-os/uprog/init.c
lunaix-os/usr/uwrap.S

index f87b00a142985d88578b6a073c0ac11e6a246583..6cece75784b807cd6c5e32a60d11a948edac5e54 100644 (file)
  *
  */
 #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")))
 
index 0d5131f53def4ce02ea288afdaf1efa1391fdb9c..f6c4355a1c0fabc58fc745b315bd4d74d0c604c6 100644 (file)
@@ -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) {
index c7467aa9db5b614ab4af70ab844a0a1311a508b3..241ddf91b253823cb88b5600b3f116cdb7ef020d 100644 (file)
@@ -8,23 +8,24 @@
 #include <lunaix/spike.h>
 
 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;
     }
 
index 1c206d2566c3d99f31ab63ecbfc793b5d403f47a..cf0a422e2f1d7acab87cc8253d61fb1667ba4abb 100644 (file)
@@ -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");
index ef97c778a3144ae70f174c3c9a281dfbddcdf998..abd0ac0df4f3d925a6f7f171b2f8d9e67f4a37f7 100644 (file)
@@ -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(&param, __current, VMS_SELF);
 
index 414a840bee7cec61311fda203a7c6bb880db3ddc..23ff6a4dcc3e9a24ae115ebb532f4bed10de17bf 100644 (file)
@@ -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
 
index a1972452ebf614e287556a9596d205664813b9b9..0d1eebc9ed131a0db8f49a5cb7ada7d012f082c5 100644 (file)
@@ -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
index 1f077228da8d889b3829a6c00b875686b98860a5..1cd6515c57182386b4309204b6394d94e844730d 100644 (file)
@@ -4,6 +4,7 @@
 .section .text
     .global _u_start
     _u_start:
+        movl $2, %eax
         call usr_pre_init
         jnz 1f