diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2022-11-11 19:46:22 +0200 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2022-11-11 19:46:22 +0200 |
| commit | 1d37792fb7a47135f0e7b7fc245e83bb1d8fc496 (patch) | |
| tree | 09ec92012bb9d74d5d26bc7a4a7647c36c9acb3e /common | |
| parent | 9edd65c48ee75751327c6ee6d42a7009e726d667 (diff) | |
| download | kmi-1d37792fb7a47135f0e7b7fc245e83bb1d8fc496.tar.gz kmi-1d37792fb7a47135f0e7b7fc245e83bb1d8fc496.zip | |
less buggy fork
+ Not strictly done yet, but we can swap between two processes :D
Diffstat (limited to 'common')
| -rw-r--r-- | common/tcb.c | 19 | ||||
| -rw-r--r-- | common/uapi/proc.c | 9 | ||||
| -rw-r--r-- | common/vmem.c | 28 |
3 files changed, 42 insertions, 14 deletions
diff --git a/common/tcb.c b/common/tcb.c index 5f0e97b..0a676af 100644 --- a/common/tcb.c +++ b/common/tcb.c @@ -19,6 +19,7 @@ #include <arch/cpu.h> #include <arch/vmem.h> +#include <arch/proc.h> /* arguably exessively many globals... */ /** Thread ID to start looking from when allocating new ID. */ @@ -63,7 +64,7 @@ static id_t __alloc_tid(struct tcb *t) { /** \todo this would need some locking or something... */ for (size_t i = start_tid; i < num_tids; ++i) { - if (tcbs[i]) + if (tcbs[i] || i == 0) continue; tcbs[i] = t; @@ -158,6 +159,7 @@ struct tcb *create_thread(struct tcb *p) init_uvmem(t, UVMEM_START, UVMEM_END); t->proc.vmem = create_vmem(); t->pid = t->tid; + t->rid = t->tid; p = t; } @@ -172,13 +174,20 @@ struct tcb *create_thread(struct tcb *p) /** * Copy process. * - * @param n New process. * @param p Parent process. + * @param n New process. * @return \ref OK. */ -static stat_t __copy_proc(struct tcb *n, struct tcb *p) +static stat_t __copy_proc(struct tcb *p, struct tcb *n) { - /* execution continuation? */ + /** @todo setup rpc stack stuff */ + /** @todo I think keeping track of userspace stack stuff is unnecessary, + * unless we want unlimited stack size but that sounds dumb. Anycase, we + * need to duplicate stack info, whatever we do. */ + /** @todo should there be in-kernel child tracking? */ + n->exec = p->exec; + clone_regs(n, p); + copy_caps(n->caps, p->caps); return clone_mem_regions(n, p); } @@ -191,7 +200,7 @@ struct tcb *create_proc(struct tcb *p) if (!n) return 0; - if (likely(p)) + if (p) __copy_proc(p, n); /* we have a parent thread */ return n; diff --git a/common/uapi/proc.c b/common/uapi/proc.c index 5dbd90c..969dcd6 100644 --- a/common/uapi/proc.c +++ b/common/uapi/proc.c @@ -62,8 +62,9 @@ SYSCALL_DEFINE0(fork)(){ if (!t) return SYS_RET1(ERR_OOMEM); - /* prepare args for when we eventually swap to the new proc */ - set_args(t, SYS_RET2(OK, 0)); + /* prepare args for when we eventually swap to the new proc, giving + * parent ID as third return value */ + set_args(t, SYS_RET3(OK, 0, c->pid)); return SYS_RET2(OK, t->pid); } @@ -166,5 +167,9 @@ SYSCALL_DEFINE1(swap)(sys_arg_t tid){ /* switch over to new thread */ use_tcb(t); + /* set return value for current thread */ + set_args(c, SYS_RET1(OK)); + + /* get register state for new thread */ return get_args(t); } diff --git a/common/vmem.c b/common/vmem.c index 42b4adf..458624c 100644 --- a/common/vmem.c +++ b/common/vmem.c @@ -19,15 +19,29 @@ stat_t init_uvmem(struct tcb *t, vm_t base, vm_t top) return init_region(&t->sp_r, base, top); } -static stat_t __clone_mapped_region(struct tcb *d, struct tcb *s, struct mem_region *m) +/** + * Clone process memory region. + * + * @param d Destination tcb. + * @param s Source tcb. + * @param m Memory region to clone. + * @return \ref ERR_MISC if clone failed, otherwise \ref OK. + * + * @todo check shared memory regions. + */ +static stat_t __clone_mapped_region(struct tcb *d, struct tcb *s, + struct mem_region *m) { - size_t size = 0; - vm_t va = alloc_fixed_region(&d->sp_r, m->start, m->end - m->start, - &size, m->flags); + vm_t start = m->start * order_size(BASE_PAGE); + vm_t end = m->end * order_size(BASE_PAGE); + + size_t size = end - start, actual_size = 0; + vm_t va = alloc_fixed_region(&d->sp_r, start, size, + &actual_size, m->flags); - catastrophic_assert(va == m->start); + catastrophic_assert(va == start); - if (copy_allocd_region(d->proc.vmem, va, size, m->flags, s)) + if (!copy_allocd_region(d->proc.vmem, va, size, m->flags, s->proc.vmem)) return ERR_MISC; return OK; @@ -220,7 +234,7 @@ stat_t alloc_shared_wrapper(struct vmem *b, pm_t *offset, vm_t vaddr, } stat_t copy_allocd_wrapper(struct vmem *b, pm_t *offset, vm_t vaddr, - vmflags_t flags, enum mm_order order, void *data) + vmflags_t flags, enum mm_order order, void *data) { struct vmem *s = (struct vmem *)data; |
