From 21efbba39340300a7ba9d5f4f94017f5ff956833 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sat, 2 Nov 2024 16:02:41 +0200 Subject: intial working sys_spawn --- src/elf.c | 29 ++++++++++++++--------------- src/proc.c | 4 ++-- src/uapi/proc.c | 20 ++++++++++++++++---- src/vmem.c | 2 +- 4 files changed, 33 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/elf.c b/src/elf.c index 2cdcb8b..23f9b95 100644 --- a/src/elf.c +++ b/src/elf.c @@ -34,12 +34,13 @@ static uint8_t __elf_to_uvflags(uint8_t elf_flags) } static stat_t __elf_map_section(struct tcb *t, - vm_t va, size_t vaz, - vm_t vf, size_t vfz, - uint8_t flags) + vm_t va, size_t vaz, + vm_t vf, size_t vfz, + uint8_t flags) { size_t region_size; - vm_t v = alloc_fixed_region(&t->uvmem.region, va, vaz, ®ion_size, flags); + vm_t v = alloc_fixed_region(&t->uvmem.region, va, vaz, ®ion_size, + flags); if (!v) return ERR_INVAL; @@ -59,7 +60,9 @@ static stat_t __elf_map_section(struct tcb *t, memcpy((void *)page, (void *)(vf + runner), z); } - if (map_vpage(t->proc.vmem, page, va + runner, flags, BASE_PAGE)) { + if (map_vpage(t->proc.vmem, + page, va + runner, + flags, BASE_PAGE)) { free_page(BASE_PAGE, page); return ERR_OOMEM; } @@ -77,17 +80,16 @@ static stat_t __elf_map_section(struct tcb *t, * @param phstart Program header start. * @param phnum Number of program header entries. * @param phsize Size of page header entry. + * @param OK on success, something else otherwise. */ static stat_t __map_exec(struct tcb *t, - vm_t bin, - uint8_t ei_c, - vm_t phstart, - size_t phnum, - size_t phsize) + vm_t bin, + uint8_t ei_c, + vm_t phstart, + size_t phnum, + size_t phsize) { assert(t && is_proc(t)); - /* temporarily visit process virtual memory */ - use_vmem(t->proc.vmem); /* create empty vmem so we don't have to worry about possible overlaps */ struct vmem *new_vmem = create_vmem(); @@ -106,7 +108,6 @@ static stat_t __map_exec(struct tcb *t, destroy_vmem(new_vmem); t->uvmem = old_uvmem; t->proc.vmem = old_vmem; - use_vmem(t->rpc.vmem); return ERR_OOMEM; } @@ -133,7 +134,6 @@ static stat_t __map_exec(struct tcb *t, t->proc.vmem = old_vmem; t->uvmem = old_uvmem; - use_vmem(t->rpc.vmem); return ERR_OOMEM; } } @@ -146,7 +146,6 @@ static stat_t __map_exec(struct tcb *t, t->proc.vmem = new_vmem; t->uvmem = new_uvmem; - use_vmem(t->rpc.vmem); return OK; } diff --git a/src/proc.c b/src/proc.c index c1dfc64..090ae20 100644 --- a/src/proc.c +++ b/src/proc.c @@ -25,8 +25,6 @@ stat_t prepare_proc(struct tcb *t, vm_t bin, vm_t interp) return ERR_INVAL; t->callback = entry; - set_thread(t); - set_return(t, entry); return OK; } @@ -56,6 +54,8 @@ stat_t init_proc(void *fdt, vm_t *proc_fdt, vm_t *proc_initrd) * save them here before we switch */ use_tcb(t); + set_thread(t); + set_return(t, t->callback); set_stack(t, t->rpc_stack - BASE_PAGE_SIZE); stat_t ret = prepare_proc(t, get_init_base(fdt), 0); diff --git a/src/uapi/proc.c b/src/uapi/proc.c index 8d90687..51a1252 100644 --- a/src/uapi/proc.c +++ b/src/uapi/proc.c @@ -128,7 +128,11 @@ SYSCALL_DEFINE2(exec)(struct tcb *t, sys_arg_t bin, sys_arg_t interp) if (prepare_proc(t, bin, interp)) return_args1(t, ERR_INVAL); + set_thread(t); + set_return(t, t->callback); set_ret4(t, 0, t->tid, SYS_USER_SPAWNED, t->pid); + + flush_tlb_full(); } /** @@ -150,16 +154,24 @@ SYSCALL_DEFINE2(spawn)(struct tcb *t, sys_arg_t bin, sys_arg_t interp) if (!n) return_args1(t, ERR_OOMEM); - /** @todo copy over bin and interp into new process, this is not enough */ n->notify_id = c->notify_id; - stat_t ret = OK; - if ((ret = prepare_proc(n, bin, interp))) { + if (prepare_proc(n, bin, interp)) { /* this kills the thread */ orphanize(t); unorphanize(t); - return_args1(t, ret); + return_args1(t, ERR_INVAL); } + /** @todo should permissions be transferred? Probably, not but now there + * is a slightly annoying asymmetry between fork+exec vs spawn... */ + + /* temporarily switch to new thread to manipulate stack */ + use_tcb(n); + set_thread(n); + set_return(n, n->callback); + set_ret4(n, 0, n->tid, SYS_USER_SPAWNED, n->pid); + use_tcb(t); + return_args1(t, n->pid); } diff --git a/src/vmem.c b/src/vmem.c index 2752d23..76e0ece 100644 --- a/src/vmem.c +++ b/src/vmem.c @@ -330,7 +330,7 @@ vm_t map_shared_fixed_uvmem(struct tcb *t, pm_t start, size_t size, assert(is_aligned(start, BASE_PAGE_SIZE)); const vm_t v = alloc_shared_region(&t->uvmem.region, - size, &size, flags, 0); + size, &size, flags, 0); if (!v) return 0; -- cgit v1.3