From 6e290500db6855e821936716061187b04f9339f8 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Tue, 23 Mar 2021 19:38:48 +0800 Subject: [PATCH] kernel: riscv: fix wfi handling for interrupts Keep interrupts nominally disabled in supervisor mode, and only enable them for a brief period after WFI hits. If an interrupt occurs, then WFI becomes a NOP. The `idle()` function is only called when no more processes are running, which happens when all processes are waiting for an interrupt. Therefore, enable interrupts for a brief period when WFI returns in order to drain all pending interrupts and return to the caller to handle any change in the process table. Signed-off-by: Sean Cross --- kernel/src/arch/riscv.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/kernel/src/arch/riscv.rs b/kernel/src/arch/riscv.rs index a1a15138d..df6885888 100644 --- a/kernel/src/arch/riscv.rs +++ b/kernel/src/arch/riscv.rs @@ -19,7 +19,6 @@ pub fn current_pid() -> PID { pub fn init() { unsafe { - sstatus::set_sie(); sie::set_ssoft(); sie::set_sext(); } @@ -29,6 +28,21 @@ pub fn init() { /// Put the core to sleep until an interrupt hits. Returns `true` /// to indicate the kernel should not exit. pub fn idle() -> bool { + + // Issue `wfi`. This will return as soon as an external interrupt + // is available. unsafe { riscv::asm::wfi() }; + + // Enable interrupts temporarily in Supervisor mode, allowing them + // to drain. Aside from this brief instance, interrupts are + // disabled when running in Supervisor mode. + // + // These interrupts are handled by userspace, so code execution will + // immediately jump to the interrupt handler and return here after + // all interrupts have been handled. + unsafe { + sstatus::set_sie(); + sstatus::clear_sie(); + }; true }