Disable kernel traps before going to userspace

This commit is contained in:
Zejun Zhao 2025-08-08 20:30:20 +08:00 committed by Ruihan Li
parent cdd28787ed
commit 597b0b5f25
4 changed files with 9 additions and 3 deletions

View File

@ -77,6 +77,9 @@ impl RawUserContext {
/// On return, the context will be reset to the status before the trap.
/// Trap reason will be placed at `estat`.
pub(in crate::arch) fn run(&mut self) {
// Return to userspace with interrupts disabled. Otherwise, interrupts
// after switching `SAVE_SCRATCH` will mess up the CPU state.
crate::arch::irq::disable_local();
unsafe { run_user(self as *mut RawUserContext) }
}
}

View File

@ -103,6 +103,9 @@ impl RawUserContext {
/// On return, the context will be reset to the status before the trap.
/// Trap reason and error code will be placed at `scause` and `stval`.
pub(in crate::arch) fn run(&mut self) {
// Return to userspace with interrupts disabled. Otherwise, interrupts
// after switching `sscratch` will mess up the CPU state.
crate::arch::irq::disable_local();
unsafe { run_user(self) }
}
}

View File

@ -22,9 +22,6 @@
# extern "sysv64" fn syscall_return(&mut UserContext)
.global syscall_return
syscall_return:
# disable interrupt
cli
# save callee-saved registers
push r15
push r14

View File

@ -96,6 +96,9 @@ impl RawUserContext {
/// If `trap_num` is `0x100`, it will go user by `sysret` (`rcx` and `r11` are dropped),
/// otherwise it will use `iret`.
pub(in crate::arch) fn run(&mut self) {
// Return to userspace with interrupts disabled. Otherwise, interrupts
// after executing `swapgs` will mess up the CPU state.
crate::arch::irq::disable_local();
unsafe {
syscall_return(self);
}