From efdc433ebc9a55f96c14ba631055be750e59e347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Holz?= Date: Wed, 7 Feb 2024 19:13:19 +0100 Subject: [PATCH] Kernel/riscv64: Implement thread_context_first_enter thread_context_first_enter reuses the context restoring code in the trap handler, just like other arches already do. The `ld x2, 1*8(sp)` is unnecessary in the trap handler, as the stack pointer should be equal to the stack pointer slot in the RegisterState if the trap is from supervisor mode (and we currently don't support user traps). This load will however make us unable to reuse that code for thread_context_first_enter. --- Kernel/Arch/riscv64/Processor.cpp | 17 +++++++++++++++-- Kernel/Arch/riscv64/trap_handler.S | 5 +++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Kernel/Arch/riscv64/Processor.cpp b/Kernel/Arch/riscv64/Processor.cpp index dd24dff145..4ed0895ad3 100644 --- a/Kernel/Arch/riscv64/Processor.cpp +++ b/Kernel/Arch/riscv64/Processor.cpp @@ -276,6 +276,12 @@ ErrorOr> ProcessorBase::capture_stack_trace(Thread&, size return Vector {}; } +extern "C" void context_first_init(Thread* from_thread, Thread* to_thread); +extern "C" void context_first_init(Thread* from_thread, Thread* to_thread) +{ + do_context_first_init(from_thread, to_thread); +} + extern "C" void enter_thread_context(Thread* from_thread, Thread* to_thread); extern "C" void enter_thread_context(Thread* from_thread, Thread* to_thread) { @@ -304,9 +310,16 @@ extern "C" void enter_thread_context(Thread* from_thread, Thread* to_thread) load_fpu_state(&to_thread->fpu_state()); } -NAKED void thread_context_first_enter(void) +NAKED void thread_context_first_enter() { - asm("unimp"); + asm( + "ld a0, 0(sp) \n" + "ld a1, 8(sp) \n" + "addi sp, sp, 32 \n" + "call context_first_init \n" + "mv a0, sp \n" + "call exit_trap \n" + "tail restore_context_and_sret \n"); } NAKED void do_assume_context(Thread*, u32) diff --git a/Kernel/Arch/riscv64/trap_handler.S b/Kernel/Arch/riscv64/trap_handler.S index edee17fb51..87cc226da4 100644 --- a/Kernel/Arch/riscv64/trap_handler.S +++ b/Kernel/Arch/riscv64/trap_handler.S @@ -90,6 +90,9 @@ asm_trap_handler: call trap_handler +.global restore_context_and_sret +restore_context_and_sret: + // Remove TrapFrame from the stack addi sp, sp, 16 @@ -131,7 +134,5 @@ asm_trap_handler: ld x30, 29*8(sp) ld x31, 30*8(sp) - ld x2, 1*8(sp) - addi sp, sp, REGISTER_STATE_SIZE sret