From 3d63ed7a4ca25580b508ebea9aa9bf6223eee6cd Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Wed, 20 Nov 2024 09:39:23 +0800 Subject: [PATCH] Keep interrupts disabled during context switching --- ostd/src/task/mod.rs | 3 +++ ostd/src/task/processor.rs | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ostd/src/task/mod.rs b/ostd/src/task/mod.rs index 3b28385f..d9c7da6c 100644 --- a/ostd/src/task/mod.rs +++ b/ostd/src/task/mod.rs @@ -163,6 +163,9 @@ impl TaskOptions { /// all task will entering this function /// this function is mean to executing the task_fn in Task extern "C" fn kernel_task_entry() -> ! { + // See `switch_to_task` for why we need this. + crate::arch::irq::enable_local(); + let current_task = Task::current() .expect("no current task, it should have current task in kernel task entry"); diff --git a/ostd/src/task/processor.rs b/ostd/src/task/processor.rs index a92d81f0..9fb49505 100644 --- a/ostd/src/task/processor.rs +++ b/ostd/src/task/processor.rs @@ -69,7 +69,9 @@ pub(super) fn switch_to_task(next_task: Arc) { drop(unsafe { Arc::from_raw(old_prev) }); } - drop(irq_guard); + // Keep interrupts disabled during context switching. This will be enabled after switching to + // the target task (in the code below or in `kernel_task_entry`). + core::mem::forget(irq_guard); // SAFETY: // 1. `ctx` is only used in `reschedule()`. We have exclusive access to both the current task @@ -85,4 +87,7 @@ pub(super) fn switch_to_task(next_task: Arc) { // always possible. For example, `context_switch` can switch directly to the entry point of the // next task. Not dropping is just fine because the only consequence is that we delay the drop // to the next task switching. + + // See also `kernel_task_entry`. + crate::arch::irq::enable_local(); }