diff --git a/core/syscall.c b/core/syscall.c new file mode 100644 index 0000000..9d99a61 --- /dev/null +++ b/core/syscall.c @@ -0,0 +1,27 @@ +#include "klibc.h" +#include "stdarg.h" +#include "syscall.h" +#include "thread.h" + +int syscallExecute(int syscallId, const struct cpu_state *user_ctx){ + + int ret; + + switch (syscallId) { + case SYSCALL_ID_EXIT: + uint status; + ret = syscallGet1arg(user_ctx, &status); + if(ret != 0) + break; + threadExit(); + assert(0); + break; + case SYSCALL_ID_WRITE: + ret = printf("YOLO FROM USERSPACE\n"); + break; + default: + printf("Unknon syscall id %d\n", syscallId); + ret = -ENOENT; + } + return ret; +} diff --git a/core/syscall.h b/core/syscall.h new file mode 100644 index 0000000..d73e10e --- /dev/null +++ b/core/syscall.h @@ -0,0 +1,9 @@ +#pragma once + +#include "cpu_context.h" + + +#define SYSCALL_ID_EXIT 1 +#define SYSCALL_ID_WRITE 2 + +int syscallExecute(int syscallId, const struct cpu_state *user_ctx); diff --git a/core/thread.c b/core/thread.c index fa31335..42ab230 100644 --- a/core/thread.c +++ b/core/thread.c @@ -132,10 +132,13 @@ struct cpu_state *threadSwitch(struct cpu_state *prevCpu) disable_IRQs(flags); nextThread = threadSelectNext(); - currentThread->cpuState = prevCpu; - currentThread->state = READY; - currentThread = nextThread; - currentThread->state = RUNNING; + if (nextThread != currentThread) { + currentThread->cpuState = prevCpu; + currentThread->state = READY; + currentThread = nextThread; + currentThread->state = RUNNING; + threadPrepareContext(nextThread); + } restore_IRQs(flags);