diff --git a/etc/make/conf.mk b/etc/make/conf.mk index fb358ee..1ea63ba 100644 --- a/etc/make/conf.mk +++ b/etc/make/conf.mk @@ -75,7 +75,8 @@ OBJS+=$(O)/sys/debug.o \ $(O)/sys/init.o \ $(O)/sys/vfs/vfs_ops.o \ $(O)/sys/vfs/vfs_access.o \ - $(O)/sys/time.o + $(O)/sys/time.o \ + $(O)/sys/thread.o # \ $(O)/sys/vfs/pty.o \ diff --git a/include/sys/thread.h b/include/sys/thread.h index 30d34f9..4664d7e 100644 --- a/include/sys/thread.h +++ b/include/sys/thread.h @@ -86,3 +86,6 @@ void thread_cleanup(struct thread *t); void thread_terminate(struct thread *t); void thread_signal(struct thread *t, int signum); + +void thread_ioctx_fork(struct thread *dst, struct thread *src); +void thread_ioctx_cleanup(struct thread *t); diff --git a/sys/amd64/sys/thread.c b/sys/amd64/sys/thread.c index 6d2ddee..b93009b 100644 --- a/sys/amd64/sys/thread.c +++ b/sys/amd64/sys/thread.c @@ -42,20 +42,6 @@ void thread_restore_context(struct thread *thr) { } } -static void thread_ioctx_fork(struct thread *dst, struct thread *src) { - for (size_t i = 0; i < THREAD_MAX_FDS; ++i) { - dst->fds[i] = src->fds[i]; - if (dst->fds[i]) { - ++dst->fds[i]->refcount; - } - } - - // Clone ioctx: cwd vnode reference and path - dst->ioctx.cwd_vnode = src->ioctx.cwd_vnode; - dst->ioctx.uid = src->ioctx.uid; - dst->ioctx.gid = src->ioctx.gid; -} - // Initialize platform context to default values // Assumes everything was allocated before // Requirements for calling: @@ -159,8 +145,6 @@ int thread_init( t->data.data_flags |= THREAD_NOHEAP_STACK3; } - // TODO: alloc argp pages here - kstack_sig = kmalloc(THREAD_KSTACK_SIZE); _assert(kstack_sig); } @@ -207,17 +191,7 @@ void thread_cleanup(struct thread *t) { _assert(t); // Release files - for (size_t i = 0; i < 4; ++i) { - if (t->fds[i]) { - vfs_close(&t->ioctx, t->fds[i]); - - _assert(t->fds[i]->refcount >= 0); - if (t->fds[i]->refcount == 0) { - kdebug("No one uses fd %d, freeing it\n", i); - kfree(t->fds[i]); - } - } - } + thread_ioctx_cleanup(t); // Release stacks if (!(t->data.data_flags & THREAD_NOHEAP_STACK0)) { @@ -236,22 +210,6 @@ void thread_cleanup(struct thread *t) { kfree(t); } -void thread_set_name(struct thread *t, const char *name) { - if (name) { - _assert(strlen(name) < 32); - strcpy(t->name, name); - } else { - t->name[0] = 0; - } -} - -// TODO: may be moved to platform-independent file -void thread_signal(struct thread *t, int s) { - _assert(t); - _assert(s > 0 && s <= 32); - t->sigq |= (1 << (s - 1)); -} - static uintptr_t argp_copy(struct thread *thr, const char *const argv[], size_t *arg_count) { uintptr_t dst_page_phys = amd64_phys_alloc_page(); if (dst_page_phys == MM_NADDR) { @@ -496,12 +454,15 @@ void amd64_thread_sigenter(struct thread *t, int signum) { ctx->__canary = AMD64_STACK_CTX_CANARY; if (t == get_cpu()->thread) { - panic("NYI\n"); + while (t->sigq) { + asm volatile ("sti; hlt; cli"); + } } } void amd64_thread_sigret(struct thread *t) { _assert(t); + t->data.rsp0 = t->data.rsp0s; } diff --git a/sys/amd64/sys_proc.c b/sys/amd64/sys_proc.c index 90be58e..e3dbc35 100644 --- a/sys/amd64/sys_proc.c +++ b/sys/amd64/sys_proc.c @@ -34,9 +34,9 @@ int sys_kill(int pid, int signum) { if (cur_thread == dst_thread) { // Suicide signal, just hang on and wait // until scheduler decides it's our time - asm volatile ("sti; hlt; cli"); - - kdebug("Returned from shit\n"); + while (cur_thread->sigq) { + asm volatile ("sti; hlt; cli"); + } } return 0; @@ -56,8 +56,6 @@ void sys_sigret(void) { thr->flags |= THREAD_SIGRET; asm volatile ("sti; hlt; cli"); - - panic("Fuck\n"); } int sys_waitpid(int pid, int *status) { diff --git a/sys/thread.c b/sys/thread.c new file mode 100644 index 0000000..18aba70 --- /dev/null +++ b/sys/thread.c @@ -0,0 +1,49 @@ +#include "sys/thread.h" +#include "sys/string.h" +#include "sys/assert.h" +#include "sys/debug.h" +#include "sys/heap.h" + +void thread_ioctx_fork(struct thread *dst, struct thread *src) { + for (size_t i = 0; i < THREAD_MAX_FDS; ++i) { + dst->fds[i] = src->fds[i]; + if (dst->fds[i]) { + ++dst->fds[i]->refcount; + } + } + + // Clone ioctx: cwd vnode reference and path + dst->ioctx.cwd_vnode = src->ioctx.cwd_vnode; + dst->ioctx.uid = src->ioctx.uid; + dst->ioctx.gid = src->ioctx.gid; +} + +void thread_ioctx_cleanup(struct thread *t) { + for (size_t i = 0; i < THREAD_MAX_FDS; ++i) { + if (t->fds[i]) { + vfs_close(&t->ioctx, t->fds[i]); + + _assert(t->fds[i]->refcount >= 0); + if (t->fds[i]->refcount == 0) { + kdebug("No one uses fd %d, freeing it\n", i); + kfree(t->fds[i]); + } + } + } +} + +void thread_signal(struct thread *t, int s) { + _assert(t); + _assert(s > 0 && s <= 32); + t->sigq |= (1 << (s - 1)); +} + +void thread_set_name(struct thread *t, const char *name) { + if (name) { + _assert(strlen(name) < 32); + strcpy(t->name, name); + } else { + t->name[0] = 0; + } +} +