kernel: expose threads to userspace (newthread and threadexit syscalls)

This commit is contained in:
Matheus
2024-04-14 16:12:52 -03:00
parent 2cb94c2c2e
commit 6d04deeac9
7 changed files with 147 additions and 41 deletions
+18 -7
View File
@@ -270,6 +270,21 @@ void sched_procexit() {
semaphore_signal(&sched_initproc->waitsem);
}
static void threadexit_internal(context_t *) {
thread_t *thread = _cpu()->thread;
// we don't need to access the current thread anymore, as any state will be ignored and we are running on the scheduler stack
// as well as not being preempted at all
_cpu()->thread = NULL;
thread->flags |= SCHED_THREAD_FLAGS_DEAD;
// because a thread deallocating its own data is a nightmare, thread deallocation and such will be left to whoever frees the proc it's tied to
// (likely an exit(2) call)
// FIXME this doesn't apply to kernel threads and something should be figured out for them
sched_stopcurrentthread();
}
__attribute__((noreturn)) void sched_threadexit() {
thread_t *thread = _cpu()->thread;
proc_t *proc = thread->proc;
@@ -290,13 +305,8 @@ __attribute__((noreturn)) void sched_threadexit() {
}
interrupt_set(false);
_cpu()->thread = NULL;
// because a thread deallocating its own data is a nightmare, thread deallocation and such will be left to whoever frees the proc it's tied to
// (likely an exit(2) call)
// FIXME this doesn't apply to kernel threads and something should be figured out for them
sched_stopcurrentthread();
arch_context_saveandcall(threadexit_internal, _cpu()->schedulerstack);
__builtin_unreachable();
}
// called right before going back to userspace in places like the syscall handler or arch_context_switch
@@ -323,6 +333,7 @@ void sched_stopotherthreads() {
proc->threads[i]->shouldexit = true;
sched_wakeup(proc->threads[i], SCHED_WAKEUP_REASON_INTERRUPTED);
proc->threads[i] = NULL;
}
spinlock_release(&proc->lock);
+1 -1
View File
@@ -115,7 +115,7 @@ syscallret_t syscall_execve(context_t *context, char *upath, char *uargv[], char
ret.ret = 0;
// close CLOEXEC fds
// TODO close CLOEXEC fds
sched_stopotherthreads();
+63
View File
@@ -0,0 +1,63 @@
#include <kernel/syscalls.h>
#include <kernel/scheduler.h>
#include <kernel/cmdline.h>
#include <kernel/alloc.h>
syscallret_t syscall_newthread(context_t *, void *entry, void *stack) {
syscallret_t ret = {
.ret = -1
};
// if the kernel was specified to have no user threading in the cmdline
if (cmdline_get("nomultithread")) {
ret.errno = ENOSYS;
return ret;
}
proc_t *proc = _cpu()->thread->proc;
spinlock_acquire(&proc->lock);
// if we currently don't want any more threads to start
if (proc->nomorethreads == true) {
spinlock_release(&proc->lock);
ret.errno = EAGAIN;
return ret;
}
int i;
for (i = 0; i < proc->threadtablesize; ++i) {
thread_t *thread = proc->threads[i];
if (thread == NULL || (thread->flags & SCHED_THREAD_FLAGS_DEAD) == 0)
continue;
// reuse the slot in the threads table
sched_destroythread(thread);
break;
}
if (i == proc->threadtablesize) {
// resize it
void *tmp = realloc(proc->threads, (proc->threadtablesize + 1) * sizeof(thread_t *));
if (tmp == NULL) {
ret.errno = ENOMEM;
goto cleanup;
}
proc->threads = tmp;
}
proc->threads[i] = sched_newthread(entry, 16 * PAGE_SIZE, 1, _cpu()->thread->proc, stack);
if (proc->threads[i] == NULL) {
ret.errno = ENOMEM;
goto cleanup;
}
proc->threads[i]->vmmctx = _cpu()->thread->vmmctx;
sched_queue(proc->threads[i]);
cleanup:
spinlock_release(&proc->lock);
return ret;
}
+6
View File
@@ -0,0 +1,6 @@
#include <kernel/syscalls.h>
#include <kernel/scheduler.h>
syscallret_t syscall_threadexit(context_t *) {
sched_threadexit();
}