From 954a379c2dcbbee4e623c73dc14b48e2d02fd128 Mon Sep 17 00:00:00 2001 From: Amoelle Date: Wed, 23 Jul 2025 15:52:18 +0300 Subject: [PATCH] finish implementation of running multiple processes concurrently (unsafely) --- kernel.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/kernel.c b/kernel.c index e55a6f5..a5a1255 100644 --- a/kernel.c +++ b/kernel.c @@ -43,7 +43,9 @@ __attribute__((naked)) __attribute__((aligned(4))) void kernel_entry(void) { __asm__ __volatile__( - "csrw sscratch, sp\n" + // Retrieve the kernel stack of the running process from sscratch + "csrrw sp, sscratch, sp\n" + "addi sp, sp, -4 * 31\n" "sw ra, 4 * 0(sp)\n" "sw gp, 4 * 1(sp)\n" @@ -76,9 +78,11 @@ void kernel_entry(void) { "sw s10, 4 * 28(sp)\n" "sw s11, 4 * 29(sp)\n" + // Retrieve and save the sp at the time of exception "csrr a0, sscratch\n" "sw a0, 4 * 30(sp)\n" + // Reset the kernel stack "mv a0, sp\n" "call handle_trap\n" @@ -230,6 +234,12 @@ void yield(void) { if (next == current_proc) return; + __asm__ __volatile__( + "csrw sscratch, %[sscratch]\n" + : + : [sscratch] "r" ((uint32_t) &next->stack[sizeof(next->stack)]) + ); + // Context switch struct process *prev = current_proc; current_proc = next;