From 608f44306a6f0d5692f5c81bcbfe7a621ec254ba Mon Sep 17 00:00:00 2001 From: Kimplul Date: Fri, 5 Jul 2024 21:02:52 +0300 Subject: implement bkl + Start out using big kernel lock, apparently seL4 thinks its good enough. I might have a go at using a more fair lock, and possibly moving to more fine-grained locks if I really feel the need to get scalability up. --- src/bkl.c | 3 +++ src/dispatch.c | 3 +++ src/ipi.c | 8 +++++++- src/main.c | 5 +++++ src/timer.c | 7 ++++++- src/uapi/ipc.c | 4 ++++ 6 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/bkl.c (limited to 'src') diff --git a/src/bkl.c b/src/bkl.c new file mode 100644 index 0000000..6dc120f --- /dev/null +++ b/src/bkl.c @@ -0,0 +1,3 @@ +#include + +spinlock_t bkl = 0; diff --git a/src/dispatch.c b/src/dispatch.c index 2786678..222611b 100644 --- a/src/dispatch.c +++ b/src/dispatch.c @@ -2,6 +2,7 @@ /* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ #include +#include #include /** @@ -31,5 +32,7 @@ void dispatch(sys_arg_t a, sys_arg_t b, sys_arg_t c, sys_arg_t d, sys_arg_t e, sys_arg_t f) { + bkl_lock(); handle_syscall(a, b, c, d, e, f, cur_tcb()); + bkl_unlock(); } diff --git a/src/ipi.c b/src/ipi.c index d00ba1c..eed3895 100644 --- a/src/ipi.c +++ b/src/ipi.c @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -33,14 +34,19 @@ void unqueue_ipi(struct tcb *t) void handle_ipi() { + bkl_lock(); + struct tcb *t = cur_tcb(); adjust_ipi(t); struct queue_head *q = queue_pop(&fifo); - if (!q) + if (!q) { + bkl_unlock(); return; + } struct tcb *r = container_of(q, struct tcb, ipi_queue); notify(r, 0); /* notify didn't take for whatever reason so return whence we came from */ + bkl_unlock(); } diff --git a/src/main.c b/src/main.c index 506d7fe..33d9596 100644 --- a/src/main.c +++ b/src/main.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -60,6 +61,8 @@ __noreturn void kernel(void *fdt, uintptr_t load_addr, struct vmem *d) /* we should be in kernelspace, so use the virtual address of our FDT. */ fdt = __va(fdt); + bkl_lock(); + /* dbg uses direct mapping at this point */ init_dbg(fdt); /* start up debugging in kernel IO */ @@ -101,6 +104,8 @@ __noreturn void main(unsigned long hart, void *fdt, uintptr_t load_addr) * have to get the function signature right */ (void)hart; + /** @todo some kind of lottery? */ + pm_t ram_base = __fdt_ram_base(fdt); pm_t ram_size = __fdt_ram_size(fdt); set_ram_base(ram_base); diff --git a/src/timer.c b/src/timer.c index 2607584..2d4ff01 100644 --- a/src/timer.c +++ b/src/timer.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -197,12 +198,16 @@ ticks_t nsecs_to_ticks(tunit_t nsecs) /* call to this function from exception handlers */ void handle_timer() { + bkl_lock(); struct timer *t = newest_timer(); remove_timer(t); struct tcb *r = get_tcb(t->tid); - if (!r) + if (!r) { + bkl_unlock(); return; + } notify(r, NOTIFY_TIMER); + bkl_unlock(); } diff --git a/src/uapi/ipc.c b/src/uapi/ipc.c index 3c24f1c..613d4a9 100644 --- a/src/uapi/ipc.c +++ b/src/uapi/ipc.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -171,6 +172,8 @@ static __noreturn void __run_notify(struct tcb *t, struct tcb *r) finalize_rpc(t, r, s); clear_bits(t->notify_flags, flags); + + bkl_unlock(); ret_userspace_fast(); unreachable(); } @@ -335,6 +338,7 @@ static void do_ipc(struct tcb *t, /* I tested out passing the return values as arguments to * ret_userspace_fast, but apparently that causes enough stack shuffling * to be slower overall. */ + bkl_unlock(); ret_userspace_fast(); } /** -- cgit v1.3