diff --git a/src/multitasking/context_switch.rs b/src/multitasking/context_switch.rs index 64b57913..2e6ef279 100644 --- a/src/multitasking/context_switch.rs +++ b/src/multitasking/context_switch.rs @@ -41,11 +41,11 @@ impl Stack { } } -pub unsafe fn context_switch_to(thread_id: ThreadId, stack_pointer: VirtAddr) { +pub unsafe fn context_switch_to(new_stack_pointer: VirtAddr, prev_thread_id: ThreadId) { asm!( "call asm_context_switch" : - : "{rdi}"(stack_pointer), "{rsi}"(thread_id) + : "{rdi}"(new_stack_pointer), "{rsi}"(prev_thread_id) : "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rpb", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "rflags", "memory" : "intel", "volatile" @@ -72,8 +72,8 @@ global_asm!( ); #[no_mangle] -pub extern "C" fn add_paused_thread(paused_stack_pointer: VirtAddr, new_thread_id: ThreadId) { - with_scheduler(|s| s.add_paused_thread(paused_stack_pointer, new_thread_id)); +pub extern "C" fn add_paused_thread(paused_stack_pointer: VirtAddr, paused_thread_id: ThreadId) { + with_scheduler(|s| s.add_paused_thread(paused_stack_pointer, paused_thread_id)); } #[naked] diff --git a/src/multitasking/mod.rs b/src/multitasking/mod.rs index 3472cd64..2cd08b10 100644 --- a/src/multitasking/mod.rs +++ b/src/multitasking/mod.rs @@ -10,8 +10,8 @@ pub fn invoke_scheduler() { let next = SCHEDULER .try_lock() .and_then(|mut scheduler| scheduler.as_mut().and_then(|s| s.schedule())); - if let Some((next_id, next_stack_pointer)) = next { - unsafe { context_switch::context_switch_to(next_id, next_stack_pointer) }; + if let Some((next_stack_pointer, prev_thread_id)) = next { + unsafe { context_switch::context_switch_to(next_stack_pointer, prev_thread_id) }; } } diff --git a/src/multitasking/scheduler.rs b/src/multitasking/scheduler.rs index 739333ef..b52bc156 100644 --- a/src/multitasking/scheduler.rs +++ b/src/multitasking/scheduler.rs @@ -28,7 +28,7 @@ impl Scheduler { self.paused_threads.pop_front() } - pub fn schedule(&mut self) -> Option<(ThreadId, VirtAddr)> { + pub fn schedule(&mut self) -> Option<(VirtAddr, ThreadId)> { if let Some(next_id) = self.next_thread() { let next_thread = self .threads @@ -38,7 +38,8 @@ impl Scheduler { .stack_pointer() .take() .expect("paused thread has no stack pointer"); - Some((next_id, next_stack_pointer)) + let prev_thread_id = mem::replace(&mut self.current_thread_id, next_thread.id()); + Some((next_stack_pointer, prev_thread_id)) } else { None } @@ -47,9 +48,8 @@ impl Scheduler { pub(super) fn add_paused_thread( &mut self, paused_stack_pointer: VirtAddr, - next_thread_id: ThreadId, + paused_thread_id: ThreadId, ) { - let paused_thread_id = mem::replace(&mut self.current_thread_id, next_thread_id); let paused_thread = self .threads .get_mut(&paused_thread_id)