diff options
author | Jon Santmyer <jon@jonsantmyer.com> | 2021-11-27 19:08:22 -0500 |
---|---|---|
committer | Jon Santmyer <jon@jonsantmyer.com> | 2021-11-27 19:08:22 -0500 |
commit | b0e7de879227c8bd9f01dc57ea28aef412a7fb44 (patch) | |
tree | da60c91f72eba0987097675769f9c6bf4504ec05 | |
parent | 8a929b4f450616790f6d43a3078a8c6925264aa2 (diff) | |
download | modit-slim-b0e7de879227c8bd9f01dc57ea28aef412a7fb44.tar.gz modit-slim-b0e7de879227c8bd9f01dc57ea28aef412a7fb44.tar.bz2 modit-slim-b0e7de879227c8bd9f01dc57ea28aef412a7fb44.zip |
fix page mangling; fix crt executing empty constructor lists
-rw-r--r-- | app/init/ird.c | 20 | ||||
-rw-r--r-- | app/init/main.c | 1 | ||||
-rw-r--r-- | app/init/rpcsrv.c | 4 | ||||
-rw-r--r-- | app/init/scheduler.c | 64 | ||||
-rw-r--r-- | app/init/scheduler.h | 2 | ||||
-rw-r--r-- | app/ld/main.c | 5 | ||||
-rw-r--r-- | app/vfs/main.c | 1 | ||||
-rw-r--r-- | bochsrc | 1 | ||||
m--------- | kernel | 0 | ||||
-rw-r--r-- | lib/c/libc.c | 7 | ||||
-rw-r--r-- | lib/c/stdio/puts.c | 2 | ||||
-rw-r--r-- | lib/c/stdlib/environ.c | 13 | ||||
-rw-r--r-- | lib/crt/x86/64/crti.s | 13 | ||||
-rw-r--r-- | lib/rpc/rpc.c | 22 | ||||
-rw-r--r-- | root/include/stdlib.h | 1 |
15 files changed, 96 insertions, 60 deletions
diff --git a/app/init/ird.c b/app/init/ird.c index 2de9ca2..260df48 100644 --- a/app/init/ird.c +++ b/app/init/ird.c @@ -5,8 +5,10 @@ #include <modit/syscall.h> #include <modit/task.h> #include <stdio.h> +#include <stdlib.h> DEFN_SYSCALL(initrd, SYSCALL_INITRD_GET) +DEFN_SYSCALL(spawn, SYSCALL_SPAWN) struct initrd_file *initrd_files = NULL; size_t initrd_filesz = 0; @@ -75,6 +77,22 @@ ird_spawn(const char *prg) { struct initrd_file *ldfile = ird_get("bin/ld"); struct initrd_file *prgfile = ird_get(prg); + + //Standard spawn call requires vfs and drivers; we need to do this manually + //These programs need to be able to run without environment variables + + printf("Preparing to spawn %s\n", prg); + uint8_t *ldstk = malloc(prgfile->size + 2); + memset(ldstk, 0, 2); + memcpy(&ldstk[2], prgfile->data, prgfile->size); + + struct syscall_exec_data spawndata = { + .program = ldfile->data, + .programsz = ldfile->size, + .stack = ldstk, + .stacksz = prgfile->size + 2 }; + cid_t prgcid = _syscall_spawn(&spawndata); + scheduler_addproc(1, prgcid, prg); } void @@ -87,7 +105,7 @@ ird_setup(void) //Detach ird server from init cid_t icid = fork_ghost(); if(icid != 0) { - scheduler_addctx(root, icid, 0); + scheduler_addctx(1, icid, 0); return; } diff --git a/app/init/main.c b/app/init/main.c index 348557c..8767024 100644 --- a/app/init/main.c +++ b/app/init/main.c @@ -1,5 +1,6 @@ #include "scheduler.h" #include "rpcsrv.h" +#include "ird.h" #include "sys/cid.h" #include "sys/ipc.h" diff --git a/app/init/rpcsrv.c b/app/init/rpcsrv.c index 2faf843..015d96a 100644 --- a/app/init/rpcsrv.c +++ b/app/init/rpcsrv.c @@ -74,7 +74,7 @@ rpcs_scheduler_addctx(struct rpc_args *args) cid_t pntid = args->sender; cid_t id = rpc_getarg(args, cid_t); - struct context *ctx = scheduler_findctx(root, pntid); + struct context *ctx = scheduler_findctx(proctree_root, pntid); struct process *parent = ctx->parent; return scheduler_addctx(parent->pid, id, 0); @@ -87,7 +87,7 @@ rpcs_scheduler_addproc(struct rpc_args *args) cid_t ctxid = rpc_getarg(args, cid_t); char *name = rpc_getarg(args, char*); - struct context *ctx = scheduler_findctx(root, pntid); + struct context *ctx = scheduler_findctx(proctree_root, pntid); struct process *parent = ctx->parent; return scheduler_addproc(parent->pid, ctxid, name); diff --git a/app/init/scheduler.c b/app/init/scheduler.c index 2486194..f024f81 100644 --- a/app/init/scheduler.c +++ b/app/init/scheduler.c @@ -8,7 +8,7 @@ #include "rpcsrv.h" #include <string.h> -struct process *root; +struct process *proctree_root; static mutex_t proctree_mutex; static size_t nprocid = 1; @@ -43,12 +43,12 @@ int scheduler_addctx(pid_t pid, cid_t id, uint8_t p) { mutex_take(&proctree_mutex); - struct process *parent = scheduler_findproc(root, pid); - if(scheduler_findctx(root, id) != NULL) { + struct process *parent = scheduler_findproc(proctree_root, pid); + if(scheduler_findctx(proctree_root, id) != NULL) { mutex_leave(&proctree_mutex); return -1; } - if(parent == NULL) parent = root; + if(parent == NULL) parent = proctree_root; struct context *new = malloc(sizeof(struct context)); new->parent = parent; @@ -72,12 +72,12 @@ int scheduler_addproc(pid_t pntid, cid_t cid, const char *name) { mutex_take(&proctree_mutex); - struct process *parent = scheduler_findproc(root, pntid); - if(scheduler_findctx(root, cid) != NULL) { + struct process *parent = scheduler_findproc(proctree_root, pntid); + if(scheduler_findctx(proctree_root, cid) != NULL) { mutex_leave(&proctree_mutex); return -1; } - if(parent == NULL) parent = root; + if(parent == NULL) parent = proctree_root; struct process *new = malloc(sizeof(struct process) + strlen(name)); new->pid = nprocid++; @@ -95,6 +95,11 @@ scheduler_addproc(pid_t pntid, cid_t cid, const char *name) new->contexts->scheduled = true; new->contexts->priority = 0; + struct clnode *node = malloc(sizeof(struct clnode)); + node->ctx = new->contexts; + node->next = list_tail; + list_tail = node; + return 0; } @@ -102,7 +107,7 @@ void scheduler_loop(void) { settimer(20); - swapto(1); + //swapto(1); while(1) { if(list_current == NULL) list_current = list_tail; struct context *ctx = list_current->ctx; @@ -120,36 +125,35 @@ void scheduler_setup(void) { const char *initname = "/bin/init"; - root = malloc(sizeof(struct process) + strlen(initname)); - root->pid = nprocid++; - root->next = NULL; - root->contexts = NULL; - root->children = NULL; - strcpy(root->name, initname); - - root->contexts = malloc(sizeof(struct context)); - root->contexts->parent = root; - root->contexts->cid = 1; - root->contexts->scheduled = true; - root->contexts->priority = 0; + proctree_root = malloc(sizeof(struct process) + strlen(initname)); + proctree_root->pid = nprocid++; + proctree_root->next = NULL; + proctree_root->contexts = NULL; + proctree_root->children = NULL; + strcpy(proctree_root->name, initname); + + struct context *initctx = malloc(sizeof(struct context)); + initctx = initctx; + initctx->parent = proctree_root; + initctx->cid = 1; + initctx->scheduled = true; + initctx->priority = 0; cid_t scid = fork_ghost(); if(scid != 0) { swapto(scid); return; } - - root->contexts->next = malloc(sizeof(struct context)); - root->contexts->next->next = root->contexts; - - root->contexts = root->contexts->next; - root->contexts->next->next = NULL; - root->contexts->cid = scid; - root->contexts->scheduled = false; - root->contexts->priority = 0; list_tail = list_current = malloc(sizeof(struct clnode)); - list_tail->ctx = root->contexts->next; + list_tail->ctx = initctx; + + initctx->next = malloc(sizeof(struct context)); + initctx = initctx->next; + initctx->cid = 2; + initctx->scheduled = false; + initctx->priority = 0; + list_tail->next = NULL; scheduler_loop(); diff --git a/app/init/scheduler.h b/app/init/scheduler.h index 7726251..d48562b 100644 --- a/app/init/scheduler.h +++ b/app/init/scheduler.h @@ -34,7 +34,7 @@ struct clnode { //Round list struct context *ctx; }; -extern struct process *root; +extern struct process *proctree_root; struct process *scheduler_findproc(struct process *base, pid_t id); struct context *scheduler_findctx(struct process *base, cid_t id); diff --git a/app/ld/main.c b/app/ld/main.c index dd00df4..4a83cae 100644 --- a/app/ld/main.c +++ b/app/ld/main.c @@ -38,7 +38,6 @@ memcpy(void *restrict dest, const void *restrict src, size_t n) void * memset(void *restrict dest, char b, size_t n) { - asm volatile("xchg %bx, %bx"); unsigned char *a = (unsigned char*)dest; while(n-- > 0) *a++ = b; return dest; @@ -80,12 +79,14 @@ elf_run(uintptr_t entry) PUSHV(nstack, arg); argc++; } + PUSHV(nstack, NULL); //Place envp uintptr_t envpl = nstack; for(const char *env = stack_data.envp; *env; env += strlen(env)) { PUSHV(nstack, env); } + PUSHV(nstack, NULL); //Place argc, [argv], [envp], entry PUSHV(nstack, argvl); @@ -142,7 +143,7 @@ main(char *data) //Loop through NULL-terminated envp list program = envp; - for(; *program; program += strlen(program)) prints(program); + for(; *program; program += strlen(program)) prints(program); program++; stack_data.argv = argv; diff --git a/app/vfs/main.c b/app/vfs/main.c index ab05a67..61a24bf 100644 --- a/app/vfs/main.c +++ b/app/vfs/main.c @@ -4,4 +4,5 @@ int main(int argc, char **argv) { printf("Hello, World!\n"); + while(1); } @@ -4,7 +4,6 @@ romimage: file=/usr/share/bochs/BIOS-bochs-latest vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest ata0-master: type=disk, mode=flat, path=disk1.hdd boot: disk -clock: sync=realtime, time0=local cpu: count=1, ips=1000000,reset_on_triple_fault=0 magic_break: enabled=1 log: bochsout.txt diff --git a/kernel b/kernel -Subproject c070667252e6db6bde6cb6b1d96a0c5bbb52b43 +Subproject 255e2d57b08236051b659323865b6ce2772071f diff --git a/lib/c/libc.c b/lib/c/libc.c index 4773d88..5ff18a1 100644 --- a/lib/c/libc.c +++ b/lib/c/libc.c @@ -1,8 +1,13 @@ +#include <stdlib.h> extern void __stdlib_heap_init(); +extern void __stdlib_environ_init(); void -__init_libc(void) +__init_libc(int argc, char **argv, char **envp) { + (void)argc; + (void)argv; __stdlib_heap_init(); + __stdlib_environ_init(envp); } diff --git a/lib/c/stdio/puts.c b/lib/c/stdio/puts.c index 55a3472..6072437 100644 --- a/lib/c/stdio/puts.c +++ b/lib/c/stdio/puts.c @@ -3,5 +3,5 @@ int puts(const char *s) { - return prints(s); + return printf("%s\n", s); } diff --git a/lib/c/stdlib/environ.c b/lib/c/stdlib/environ.c new file mode 100644 index 0000000..6ca11e7 --- /dev/null +++ b/lib/c/stdlib/environ.c @@ -0,0 +1,13 @@ +#include <stdlib.h> + +char **environ = NULL; + +void +__stdlib_environ_init(const char **envp) +{ + int envs = 0; + while(envp[envs++] != NULL); + + environ = malloc(sizeof(uintptr_t) * envs); + memcpy(environ, envp, sizeof(uintptr_t) * envs); +} diff --git a/lib/crt/x86/64/crti.s b/lib/crt/x86/64/crti.s index 1ef6f12..d2ce5ef 100644 --- a/lib/crt/x86/64/crti.s +++ b/lib/crt/x86/64/crti.s @@ -4,19 +4,6 @@ _init: push %rbp movq %rsp, %rbp - call _init_init_array - -.section .text -.global _init_init_array -_init_init_array: - movq $__init_array_start, %rax -_init_ias_iterate: - push %rax - call *(%rax) - pop %rax - addq $8, %rax - cmpq $-1, (%rax) - jne _init_ias_iterate .section .fini .global _fini diff --git a/lib/rpc/rpc.c b/lib/rpc/rpc.c index e637d64..1fa0270 100644 --- a/lib/rpc/rpc.c +++ b/lib/rpc/rpc.c @@ -5,12 +5,18 @@ struct rpc_handle *rpc_handles = NULL; rpcid_t rpcc = 0; -RPCFUNC *rpc_init_import; -RPCFUNC *rpc_init_export; +RPCFUNC __rpc_init_import = { + .id = 0, + .handler = 1, + .returns = 'v', + .expects = "s" +}; +RPCFUNC __rpc_init_export = { + .id = 1, + .handler = 1, + .returns = 'i', + .expects = "sv" +}; -__attribute__((constructor)) -void rpc_constructor(void) -{ - rpc_init_import = rpc_define(1, RPC_INIT_IMPORT, "s", 'v'); - rpc_init_export = rpc_define(1, RPC_INIT_EXPORT, "sv", 'i'); -} +RPCFUNC *rpc_init_import = &__rpc_init_import; +RPCFUNC *rpc_init_export = &__rpc_init_export; diff --git a/root/include/stdlib.h b/root/include/stdlib.h index 7a90436..f333ac9 100644 --- a/root/include/stdlib.h +++ b/root/include/stdlib.h @@ -7,6 +7,7 @@ #define HEAP_BUCKET_ALLOC_GRANULARITY 0x4000 extern uintptr_t __pbrk; +extern char **environ; void *memset(void *s, int c, size_t n); void *memcpy(void *d, const void *s, size_t n); |