From 5f127b45481b97ed743c7e9ca2eefe2799715894 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 15 May 2024 04:24:19 +0800 Subject: [PATCH] create swap resume functions; differentiate irq context calls - invoking the swapper context is different in an IRQ because we don't want to modify the runnability states of the original process - didn't want to make a custom version of swap resume but with the IRQ issue it made sense. In the process of doing this, we can also optimize out a memory space transition, so it's probably worth it even if we're not entirely going down the right path to fix the bug of interest. --- kernel/src/services.rs | 72 +++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 70573b6c4..bef122168 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -38,6 +38,7 @@ pub enum CallbackType { /// args: irq_no, arg Interrupt(usize, *mut usize), Swap([usize; 8]), + SwapInIrq([usize; 8]), } #[derive(Debug, Copy, Clone, PartialEq)] @@ -521,6 +522,42 @@ impl SystemServices { pub fn current_pid(&self) -> PID { arch::process::current_pid() } + /// Must be called from the swapper's context. Resets the runnable states of the swapper. + pub fn finish_swap(&mut self) { + let current_pid = self.current_pid(); + let current = self.get_process_mut(current_pid).expect("couldn't get current PID"); + current.state = match current.state { + ProcessState::Running(0) => ProcessState::Sleeping, + ProcessState::Running(x) => ProcessState::Ready(x), + #[cfg(feature = "gdb-stub")] + ProcessState::DebugIrq(x) => ProcessState::Debug(x), + y => panic!("current process was {:?}, not 'Running(_)'", y), + }; + } + + pub fn swap_resume_to_userspace(&mut self, pid: PID, tid: TID) -> Result<(), xous_kernel::Error> { + let process = self.get_process_mut(pid)?; + #[cfg(feature = "gdb-stub")] + let ppid = process.ppid; + // Ensure the new context is available to be run + let available_threads = match process.state { + ProcessState::Ready(x) if x & 1 << tid != 0 => x & !(1 << tid), + // If we're currently debugging the process, return to its parent. + // This can happen when the process handles a debug interrupt. + #[cfg(feature = "gdb-stub")] + ProcessState::Debug(_) => { + crate::syscall::reset_switchto_caller(); + return self.switch_to_thread(ppid, None); + } + other => panic!( + "process {} was in an invalid state {:?} -- thread {} not available to run", + pid, other, tid + ), + }; + process.state = ProcessState::Running(available_threads); + Ok(()) + } + /// Create a stack frame in the specified process and jump to it. /// 1. Pause the current process and switch to the new one /// 2. Save the process state, if it hasn't already been saved @@ -602,21 +639,26 @@ impl SystemServices { // isn't running, that means the system has gotten into an invalid // state. - { - let current_pid = self.current_pid(); - let current = self.get_process_mut(current_pid).expect("couldn't get current PID"); - // The current thread should never be 0, but for some reason it ends up - // as 0 after resuming from suspend. It's unclear how this happens. - if current.current_thread == 0 { - current.current_thread = arch::process::current_tid(); + match cb_type { + CallbackType::SwapInIrq(_) => { + // don't manipulate current PID state at all if handling an IRQ fault + } + _ => { + let current_pid = self.current_pid(); + let current = self.get_process_mut(current_pid).expect("couldn't get current PID"); + // The current thread should never be 0, but for some reason it ends up + // as 0 after resuming from suspend. It's unclear how this happens. + if current.current_thread == 0 { + current.current_thread = arch::process::current_tid(); + } + // let old_state = current.state; + current.state = match current.state { + ProcessState::Running(x) => ProcessState::Ready(x | (1 << current.current_thread)), + y => panic!("current process was {:?}, not 'Running(_)'", y), + }; + // log_process_update(file!(), line!(), current, old_state); + // println!("Making PID {} state {:?}", current_pid, current.state); } - // let old_state = current.state; - current.state = match current.state { - ProcessState::Running(x) => ProcessState::Ready(x | (1 << current.current_thread)), - y => panic!("current process was {:?}, not 'Running(_)'", y), - }; - // log_process_update(file!(), line!(), current, old_state); - // println!("Making PID {} state {:?}", current_pid, current.state); } // Get the new process, and ensure that it is in a state where it's fit @@ -689,7 +731,7 @@ impl SystemServices { &[irq_no, arg as usize], ); } - CallbackType::Swap(args) => { + CallbackType::Swap(args) | CallbackType::SwapInIrq(args) => { arch::syscall::invoke( arch_process.current_thread_mut(), pid.get() == 1,