mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
kernel: expose threads to userspace (newthread and threadexit syscalls)
This commit is contained in:
@@ -48,6 +48,8 @@ extern syscall_recvmsg
|
|||||||
extern syscall_listen
|
extern syscall_listen
|
||||||
extern syscall_connect
|
extern syscall_connect
|
||||||
extern syscall_accept
|
extern syscall_accept
|
||||||
|
extern syscall_newthread
|
||||||
|
extern syscall_threadexit
|
||||||
syscalltab:
|
syscalltab:
|
||||||
dq syscall_print
|
dq syscall_print
|
||||||
dq syscall_mmap
|
dq syscall_mmap
|
||||||
@@ -98,7 +100,9 @@ dq syscall_recvmsg
|
|||||||
dq syscall_listen
|
dq syscall_listen
|
||||||
dq syscall_connect
|
dq syscall_connect
|
||||||
dq syscall_accept
|
dq syscall_accept
|
||||||
syscallcount equ 49
|
dq syscall_newthread
|
||||||
|
dq syscall_threadexit
|
||||||
|
syscallcount equ 51
|
||||||
section .text
|
section .text
|
||||||
global arch_syscall_entry
|
global arch_syscall_entry
|
||||||
; on entry:
|
; on entry:
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#define SCHED_THREAD_FLAGS_SLEEP 4
|
#define SCHED_THREAD_FLAGS_SLEEP 4
|
||||||
#define SCHED_THREAD_FLAGS_INTERRUPTIBLE 8
|
#define SCHED_THREAD_FLAGS_INTERRUPTIBLE 8
|
||||||
#define SCHED_THREAD_FLAGS_PREEMPTED 16
|
#define SCHED_THREAD_FLAGS_PREEMPTED 16
|
||||||
|
#define SCHED_THREAD_FLAGS_DEAD 32
|
||||||
|
|
||||||
#define SCHED_PROC_STATE_NORMAL 0
|
#define SCHED_PROC_STATE_NORMAL 0
|
||||||
#define SCHED_PROC_STATE_ZOMBIE 1
|
#define SCHED_PROC_STATE_ZOMBIE 1
|
||||||
|
|||||||
@@ -270,6 +270,21 @@ void sched_procexit() {
|
|||||||
semaphore_signal(&sched_initproc->waitsem);
|
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() {
|
__attribute__((noreturn)) void sched_threadexit() {
|
||||||
thread_t *thread = _cpu()->thread;
|
thread_t *thread = _cpu()->thread;
|
||||||
proc_t *proc = thread->proc;
|
proc_t *proc = thread->proc;
|
||||||
@@ -290,13 +305,8 @@ __attribute__((noreturn)) void sched_threadexit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interrupt_set(false);
|
interrupt_set(false);
|
||||||
_cpu()->thread = NULL;
|
arch_context_saveandcall(threadexit_internal, _cpu()->schedulerstack);
|
||||||
|
__builtin_unreachable();
|
||||||
// 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// called right before going back to userspace in places like the syscall handler or arch_context_switch
|
// 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;
|
proc->threads[i]->shouldexit = true;
|
||||||
sched_wakeup(proc->threads[i], SCHED_WAKEUP_REASON_INTERRUPTED);
|
sched_wakeup(proc->threads[i], SCHED_WAKEUP_REASON_INTERRUPTED);
|
||||||
|
proc->threads[i] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
spinlock_release(&proc->lock);
|
spinlock_release(&proc->lock);
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ syscallret_t syscall_execve(context_t *context, char *upath, char *uargv[], char
|
|||||||
|
|
||||||
ret.ret = 0;
|
ret.ret = 0;
|
||||||
|
|
||||||
// close CLOEXEC fds
|
// TODO close CLOEXEC fds
|
||||||
|
|
||||||
sched_stopotherthreads();
|
sched_stopotherthreads();
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include <kernel/syscalls.h>
|
||||||
|
#include <kernel/scheduler.h>
|
||||||
|
|
||||||
|
syscallret_t syscall_threadexit(context_t *) {
|
||||||
|
sched_threadexit();
|
||||||
|
}
|
||||||
@@ -188,10 +188,10 @@ index 0000000..e5b087a
|
|||||||
+
|
+
|
||||||
diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp
|
diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp
|
||||||
new file mode 100644
|
new file mode 100644
|
||||||
index 0000000..37e2732
|
index 0000000..2e216f9
|
||||||
--- /dev/null
|
--- /dev/null
|
||||||
+++ mlibc-workdir/sysdeps/astral/generic/generic.cpp
|
+++ mlibc-workdir/sysdeps/astral/generic/generic.cpp
|
||||||
@@ -0,0 +1,570 @@
|
@@ -0,0 +1,587 @@
|
||||||
+#include <bits/ensure.h>
|
+#include <bits/ensure.h>
|
||||||
+#include <mlibc/debug.hpp>
|
+#include <mlibc/debug.hpp>
|
||||||
+#include <mlibc/all-sysdeps.hpp>
|
+#include <mlibc/all-sysdeps.hpp>
|
||||||
@@ -322,6 +322,23 @@ index 0000000..37e2732
|
|||||||
+ return 0;
|
+ return 0;
|
||||||
+ }
|
+ }
|
||||||
+
|
+
|
||||||
|
+ #ifndef MLIBC_BUILDING_RTLD
|
||||||
|
+
|
||||||
|
+ [[noreturn]] void sys_thread_exit() {
|
||||||
|
+ syscall(SYSCALL_THREADEXIT, nullptr);
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ extern "C" void __mlibc_thread_entry();
|
||||||
|
+
|
||||||
|
+ int sys_clone(void *tcb, pid_t *pid_out, void *stack) {
|
||||||
|
+ long ret;
|
||||||
|
+ long err = syscall(SYSCALL_NEWTHREAD, &ret, (uintptr_t)__mlibc_thread_entry, (uintptr_t)stack);
|
||||||
|
+ *pid_out = ret;
|
||||||
|
+ return err;
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ #endif
|
||||||
|
+
|
||||||
+ int sys_listen(int fd, int backlog) {
|
+ int sys_listen(int fd, int backlog) {
|
||||||
+ long ret;
|
+ long ret;
|
||||||
+ return syscall(SYSCALL_LISTEN, &ret, fd, backlog);
|
+ return syscall(SYSCALL_LISTEN, &ret, fd, backlog);
|
||||||
@@ -781,10 +798,10 @@ index 0000000..990eca0
|
|||||||
+#endif
|
+#endif
|
||||||
diff --git mlibc-workdir/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h
|
diff --git mlibc-workdir/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h
|
||||||
new file mode 100644
|
new file mode 100644
|
||||||
index 0000000..52590c3
|
index 0000000..a733e60
|
||||||
--- /dev/null
|
--- /dev/null
|
||||||
+++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h
|
+++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h
|
||||||
@@ -0,0 +1,72 @@
|
@@ -0,0 +1,74 @@
|
||||||
+#ifndef _SYSCALL_H_INCLUDE
|
+#ifndef _SYSCALL_H_INCLUDE
|
||||||
+#define _SYSCALL_H_INCLUDE
|
+#define _SYSCALL_H_INCLUDE
|
||||||
+
|
+
|
||||||
@@ -837,6 +854,8 @@ index 0000000..52590c3
|
|||||||
+#define SYSCALL_LISTEN 46
|
+#define SYSCALL_LISTEN 46
|
||||||
+#define SYSCALL_CONNECT 47
|
+#define SYSCALL_CONNECT 47
|
||||||
+#define SYSCALL_ACCEPT 48
|
+#define SYSCALL_ACCEPT 48
|
||||||
|
+#define SYSCALL_NEWTHREAD 49
|
||||||
|
+#define SYSCALL_THREADEXIT 50
|
||||||
+
|
+
|
||||||
+#include <stddef.h>
|
+#include <stddef.h>
|
||||||
+#include <stdint.h>
|
+#include <stdint.h>
|
||||||
@@ -940,22 +959,23 @@ index 0000000..31d913e
|
|||||||
+endif
|
+endif
|
||||||
diff --git mlibc-workdir/sysdeps/astral/threading/x86_64-thread-entry.S mlibc-workdir/sysdeps/astral/threading/x86_64-thread-entry.S
|
diff --git mlibc-workdir/sysdeps/astral/threading/x86_64-thread-entry.S mlibc-workdir/sysdeps/astral/threading/x86_64-thread-entry.S
|
||||||
new file mode 100644
|
new file mode 100644
|
||||||
index 0000000..33d258d
|
index 0000000..4cb3ae0
|
||||||
--- /dev/null
|
--- /dev/null
|
||||||
+++ mlibc-workdir/sysdeps/astral/threading/x86_64-thread-entry.S
|
+++ mlibc-workdir/sysdeps/astral/threading/x86_64-thread-entry.S
|
||||||
@@ -0,0 +1,6 @@
|
@@ -0,0 +1,7 @@
|
||||||
+ .section .text
|
+ .section .text
|
||||||
+.global __mlibc_thread_entry
|
+.global __mlibc_thread_entry
|
||||||
+__mlibc_thread_entry:
|
+__mlibc_thread_entry:
|
||||||
+ pop %rdi
|
+ pop %rdi
|
||||||
+ pop %rsi
|
+ pop %rsi
|
||||||
+ pop %rdx
|
+ pop %rdx
|
||||||
|
+ call __mlibc_thread_trampoline
|
||||||
diff --git mlibc-workdir/sysdeps/astral/threading/x86_64-thread.cpp mlibc-workdir/sysdeps/astral/threading/x86_64-thread.cpp
|
diff --git mlibc-workdir/sysdeps/astral/threading/x86_64-thread.cpp mlibc-workdir/sysdeps/astral/threading/x86_64-thread.cpp
|
||||||
new file mode 100644
|
new file mode 100644
|
||||||
index 0000000..e2569fc
|
index 0000000..3b893f0
|
||||||
--- /dev/null
|
--- /dev/null
|
||||||
+++ mlibc-workdir/sysdeps/astral/threading/x86_64-thread.cpp
|
+++ mlibc-workdir/sysdeps/astral/threading/x86_64-thread.cpp
|
||||||
@@ -0,0 +1,58 @@
|
@@ -0,0 +1,59 @@
|
||||||
+#include <sys/mman.h>
|
+#include <sys/mman.h>
|
||||||
+#include <mlibc/debug.hpp>
|
+#include <mlibc/debug.hpp>
|
||||||
+#include <errno.h>
|
+#include <errno.h>
|
||||||
@@ -963,54 +983,55 @@ index 0000000..e2569fc
|
|||||||
+#include <bits/ensure.h>
|
+#include <bits/ensure.h>
|
||||||
+#include <mlibc/tcb.hpp>
|
+#include <mlibc/tcb.hpp>
|
||||||
+
|
+
|
||||||
|
+extern "C" void __mlibc_thread_trampoline(void *(*fn)(void *), Tcb *tcb, void *arg) {
|
||||||
|
+ if (mlibc::sys_tcb_set(tcb)) {
|
||||||
|
+ __ensure(!"failed to set tcb for new thread");
|
||||||
|
+ }
|
||||||
+
|
+
|
||||||
+ /*
|
+ while (__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED) == 0) {
|
||||||
+extern "C" void __mlibc_thread_trampoline(void* (*fn)(void*), Tcb* tcb, void* arg){
|
+ mlibc::sys_futex_wait(&tcb->tid, 0, nullptr);
|
||||||
+ if (mlibc::sys_tcb_set(tcb))
|
+ }
|
||||||
+ __ensure(!"failed to set tcb for new thread");
|
|
||||||
+
|
+
|
||||||
+ while (__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED) == 0)
|
+ tcb->invokeThreadFunc(reinterpret_cast<void *>(fn), arg);
|
||||||
+ mlibc::sys_futex_wait(&tcb->tid, 0, nullptr);
|
|
||||||
+
|
+
|
||||||
+ //tcb->returnValue = fn(arg);
|
|
||||||
+
|
|
||||||
+ __atomic_store_n(&tcb->didExit, 1, __ATOMIC_RELEASE);
|
+ __atomic_store_n(&tcb->didExit, 1, __ATOMIC_RELEASE);
|
||||||
+ mlibc::sys_futex_wake(&tcb->didExit);
|
+ mlibc::sys_futex_wake(&tcb->didExit);
|
||||||
+
|
+
|
||||||
+ mlibc::sys_thread_exit();
|
+ mlibc::sys_thread_exit();
|
||||||
+}
|
+}
|
||||||
+ */
|
|
||||||
+
|
+
|
||||||
+#define DEFAULT_STACK 0x400000
|
+#define DEFAULT_STACK 0x400000
|
||||||
+
|
+
|
||||||
+namespace mlibc{
|
+namespace mlibc {
|
||||||
+ /*
|
+ int sys_prepare_stack(void **stack, void *entry, void *arg, void *tcb, size_t *stack_size, size_t *guard_size, void **stack_base) {
|
||||||
+
|
|
||||||
+ int sys_prepare_stack(void **stack, void *entry, void *arg, void *tcb, size_t *stack_size, size_t *guard_size){
|
|
||||||
+ // TODO guard
|
+ // TODO guard
|
||||||
+
|
+
|
||||||
+ mlibc::infoLogger() << "mlibc: sys_prepare_stack() does not setup a guard!" << frg::endlog;
|
+ mlibc::infoLogger() << "mlibc: sys_prepare_stack() does not setup a guard!" << frg::endlog;
|
||||||
+
|
+
|
||||||
+ *guard_size = 0;
|
+ *guard_size = 0;
|
||||||
+
|
+
|
||||||
+ *stack_size = *stack_size ? *stack_size : DEFAULT_STACK;
|
+ *stack_size = *stack_size ? *stack_size : DEFAULT_STACK;
|
||||||
+
|
+
|
||||||
+ if(!*stack){
|
+ if (!*stack) {
|
||||||
+ *stack = mmap(NULL, *stack_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0) + *stack_size;
|
+ *stack_base = mmap(NULL, *stack_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||||
+ if(*stack == MAP_FAILED)
|
+ if (*stack_base == MAP_FAILED) {
|
||||||
+ return errno;
|
+ return errno;
|
||||||
|
+ }
|
||||||
|
+ } else {
|
||||||
|
+ *stack_base = *stack;
|
||||||
+ }
|
+ }
|
||||||
+
|
|
||||||
+ void** stack_it = (void**)*stack;
|
|
||||||
+
|
+
|
||||||
|
+ *stack = (void *)((char *)*stack_base + *stack_size);
|
||||||
|
+
|
||||||
|
+ void **stack_it = (void **)*stack;
|
||||||
|
+
|
||||||
+ *--stack_it = arg;
|
+ *--stack_it = arg;
|
||||||
+ *--stack_it = tcb;
|
+ *--stack_it = tcb;
|
||||||
+ *--stack_it = entry;
|
+ *--stack_it = entry;
|
||||||
+
|
+
|
||||||
+ *stack = (void*)stack_it;
|
+ *stack = (void *)stack_it;
|
||||||
+
|
+
|
||||||
+ return 0;
|
+ return 0;
|
||||||
+ }
|
+ }
|
||||||
+ */
|
+
|
||||||
+
|
|
||||||
+}
|
+}
|
||||||
|
|||||||
Reference in New Issue
Block a user