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 }