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 <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-03-23 19:38:48 +08:00
parent eb4ab4ca35
commit 6e290500db

View file

@ -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
}