diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 8bc925d09..c93074da2 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -20,6 +20,12 @@ const MAX_SERVER_COUNT: usize = 128; pub use crate::arch::process::{INITIAL_TID, MAX_PROCESS_COUNT}; +// fn log_process_update(f: &str, l: u32, process: &Process, old_state: ProcessState) { +// if process.pid.get() == 3 { +// println!("[{}:{}] Updated PID {:?} state: {:?} -> {:?}", f, l, process.pid, old_state, process.state); +// } +// } + /// A big unifying struct containing all of the system state. /// This is inherited from the stage 1 bootloader. pub struct SystemServices { @@ -325,6 +331,7 @@ impl SystemServices { process.ppid = PID::new_unchecked(1); process.pid = PID::new(pid as _).unwrap(); }; + // let old_state = process.state; if pid == 1 { process.state = ProcessState::Running(0); } else { @@ -337,6 +344,7 @@ impl SystemServices { 0, )); } + // log_process_update(file!(), line!(), process, old_state); } // Set up our handle with a bogus sp and pc. These will get updated @@ -433,28 +441,39 @@ impl SystemServices { .get_process_mut(current_pid) .expect("couldn't get current PID"); // println!("Finishing callback in PID {}", current_pid); + // let old_state = current.state; current.state = match current.state { ProcessState::Running(0) => ProcessState::Sleeping, ProcessState::Running(x) => ProcessState::Ready(x), y => panic!("current process was {:?}, not 'Running(_)'", y), }; + // log_process_update(file!(), line!(), current, old_state); // current.current_thread = current.previous_context; } // Get the new process, and ensure that it is in a state where it's fit - // to run. Again, if the new process isn't fit to run, then the system + // to run. Again, if the new process isn't fit to run, then the system // is in a very bad state. { let mut process = self.get_process_mut(pid)?; + let ppid = process.ppid; + // let old_state = process.state; // Ensure the new context is available to be run - let available_contexts = match process.state { + 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. + 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_contexts); + process.state = ProcessState::Running(available_threads); + // log_process_update(file!(), line!(), process, old_state); // process.current_thread = tid as u8; process.mapping.activate()?; process.activate()?; @@ -500,12 +519,14 @@ impl SystemServices { let mut current = self .get_process_mut(current_pid) .expect("couldn't get current PID"); + // let old_state = current.state; current.state = match current.state { ProcessState::Running(x) => { ProcessState::Ready(x | (1 << arch::process::current_tid())) } y => panic!("current process was {:?}, not 'Running(_)'", y), }; + // log_process_update(file!(), line!(), current, old_state); // println!("Making PID {} state {:?}", current_pid, current.state); } @@ -522,7 +543,9 @@ impl SystemServices { panic!("process hasn't been set up yet") } }; + // let old_state = process.state; process.state = ProcessState::Running(available_threads); + // log_process_update(file!(), line!(), process, old_state); process.previous_thread = process.current_thread; process.current_thread = arch::process::IRQ_TID; process.mapping.activate()?; @@ -580,6 +603,7 @@ impl SystemServices { /// it as Ready. pub fn ready_thread(&mut self, pid: PID, tid: TID) -> Result<(), xous_kernel::Error> { let process = self.get_process_mut(pid)?; + // let old_state = process.state; process.state = match process.state { ProcessState::Free => { panic!("PID {} was not running, so cannot wake thread {}", pid, tid) @@ -595,6 +619,7 @@ impl SystemServices { pid, tid, other ), }; + // log_process_update(file!(), line!(), process, old_state); klog!("Readying ({}:{}) -> {:?}", pid, tid, process.state); Ok(()) } @@ -615,6 +640,7 @@ impl SystemServices { // pid, tid, process.state // ); + // let old_state = process.state; // Determine which thread to switch to process.state = match process.state { ProcessState::Free => return Err(xous_kernel::Error::ProcessNotFound), @@ -721,6 +747,7 @@ impl SystemServices { ProcessState::Running(new_mask) } }; + // log_process_update(file!(), line!(), process, old_state); // println!( // "switch_to_thread({}:{:?}): New state is {:?} Thread is ", @@ -747,6 +774,7 @@ impl SystemServices { // ); // ArchProcess::with_current(|current| current.print_thread()); + // let old_state = process.state; process.state = match process.state { ProcessState::Running(x) if x & (1 << tid) != 0 => panic!( "PID {} thread {} was already queued for running when `switch_from_thread()` was called", @@ -774,6 +802,7 @@ impl SystemServices { ); }, }; + // log_process_update(file!(), line!(), process, old_state); // klog!( // "switch_from_thread({}:{}): New state is {:?}", // pid, tid, process.state @@ -900,6 +929,7 @@ impl SystemServices { // Set up the new process, if necessary. Remove the new thread from // the list of ready threads. + // let old_state = new.state; new.state = match new.state { ProcessState::Setup(thread_init) => { // println!("Setting up new process..."); @@ -920,6 +950,7 @@ impl SystemServices { ProcessState::Sleeping => ProcessState::Running(0), ProcessState::Debug(_) => panic!("Process was being debugged"), }; + // log_process_update(file!(), line!(), new, old_state); new.activate()?; // Mark the previous process as ready to run, since we just switched @@ -957,6 +988,7 @@ impl SystemServices { previous_pid, other ), }; + // log_process_update(file!(), line!(), previous, _oldstate); klog!( "PID {:?} state change from {:?} -> {:?}", previous_pid, @@ -986,6 +1018,7 @@ impl SystemServices { } // Transition to the new state. + // let old_state = new.state; new.state = if let ProcessState::Running(x) = new.state { let previous_tid = new.current_thread; // If no new thread is specified, take the previous @@ -1017,6 +1050,7 @@ impl SystemServices { previous_pid, new.state ) }; + // log_process_update(file!(), line!(), new, old_state); } let mut process = crate::arch::process::Process::current(); @@ -1416,6 +1450,7 @@ impl SystemServices { // println!("KERNEL({}): Created new thread {}", pid, new_tid); + // let old_state = process.state; // Queue the thread to run process.state = match process.state { ProcessState::Running(x) => ProcessState::Running(x | (1 << new_tid)), @@ -1431,6 +1466,7 @@ impl SystemServices { other ), }; + // log_process_update(file!(), line!(), process, old_state); Ok(new_tid) } @@ -1470,12 +1506,14 @@ impl SystemServices { let mut new_pid = pid; { let process = self.get_process_mut(pid)?; + // let old_state = process.state; process.state = if waiting_threads == 0 { new_pid = process.ppid; ProcessState::Sleeping } else { ProcessState::Ready(waiting_threads) }; + // log_process_update(file!(), line!(), process, old_state); } // Switch to the next available TID. This moves the process back to a `Running` state. @@ -2011,12 +2049,19 @@ impl SystemServices { ProcessState::Ready(tids | 1 << current_tid) } }; - self.get_process_mut(pid).unwrap().state = new_process_state; + { + let process = self.get_process_mut(pid).unwrap(); + // let old_state = process.state; + process.state = new_process_state; + // log_process_update(file!(), line!(), process, old_state); + } + // self.get_process_mut(pid).unwrap().state = new_process_state; Ok(()) } pub fn continue_process(&mut self, pid: PID) -> Result<(), xous_kernel::Error> { let process = self.get_process_mut(pid)?; + // let old_state = process.state; process.state = match process.state { ProcessState::Allocated => ProcessState::Allocated, ProcessState::Free => ProcessState::Free, @@ -2027,6 +2072,7 @@ impl SystemServices { ProcessState::Debug(tids) => ProcessState::Ready(tids), ProcessState::Running(tids) => ProcessState::Running(tids), }; + // log_process_update(file!(), line!(), process, old_state); Ok(()) } diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index be97a890c..33e93a4c7 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -146,89 +146,106 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes // If the server has an available thread to receive the message, // transfer it right away. - if ss.runnable(server_pid, None).unwrap() { - let server = ss - .server_from_sidx_mut(sidx) - .expect("server couldn't be located"); - if let Some(server_tid) = server.take_available_thread() { - // klog!( - // "there are threads available in PID {} to handle this message -- marking as Ready", - // server_pid - // ); - let sender_idx = if message.is_blocking() { - ss.remember_server_message(sidx, pid, thread, &message, client_address) - .map_err(|e| { - klog!("error remembering server message: {:?}", e); - ss.server_from_sidx_mut(sidx) - .expect("server couldn't be located") - .return_available_thread(server_tid); - e - })? + let server = ss + .server_from_sidx_mut(sidx) + .expect("server couldn't be located"); + if let Some(server_tid) = server.take_available_thread() { + // klog!( + // "there are threads available in PID {} to handle this message -- marking as Ready", + // server_pid + // ); + let sender_idx = if message.is_blocking() { + ss.remember_server_message(sidx, pid, thread, &message, client_address) + .map_err(|e| { + klog!("error remembering server message: {:?}", e); + ss.server_from_sidx_mut(sidx) + .expect("server couldn't be located") + .return_available_thread(server_tid); + e + })? + } else { + 0 + }; + let sender = SenderID::new(sidx, sender_idx, Some(pid)); + klog!( + "server connection data: sidx: {}, idx: {}, server pid: {}", + sidx, + sender_idx, + server_pid + ); + let envelope = MessageEnvelope { + sender: sender.into(), + body: message, + }; + + // Mark the server's context as "Ready". If this fails, return the context + // to the blocking list. + ss.ready_thread(server_pid, server_tid).map_err(|e| { + ss.server_from_sidx_mut(sidx) + .expect("server couldn't be located") + .return_available_thread(server_tid); + e + })?; + + let runnable = ss + .runnable(server_pid, Some(server_tid)) + .expect("server doesn't exist"); + // --- NOTE: Returning this value // + return if blocking && cfg!(baremetal) { + if !runnable { + // If it's not runnable (e.g. it's being debugged), switch to the parent. + let (ppid, ptid) = unsafe { SWITCHTO_CALLER.take().unwrap() }; + klog!("Activating Server parent process (server is blocked) and switching away from Client"); + ss.set_thread_result( + server_pid, + server_tid, + xous_kernel::Result::Message(envelope), + ) + .expect("couldn't set result for server thread"); + ss.activate_process_thread(thread, ppid, ptid, false) + .map(|_| Ok(xous_kernel::Result::ResumeProcess)) + .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)) } else { - 0 - }; - let sender = SenderID::new(sidx, sender_idx, Some(pid)); - klog!( - "server connection data: sidx: {}, idx: {}, server pid: {}", - sidx, - sender_idx, - server_pid - ); - let envelope = MessageEnvelope { - sender: sender.into(), - body: message, - }; - - // Mark the server's context as "Ready". If this fails, return the context - // to the blocking list. - ss.ready_thread(server_pid, server_tid).map_err(|e| { - ss.server_from_sidx_mut(sidx) - .expect("server couldn't be located") - .return_available_thread(server_tid); - e - })?; - - // --- NOTE: Returning this value // - return if blocking && cfg!(baremetal) { + // Switch to the server, since it's in a state to be run. klog!("Activating Server context and switching away from Client"); ss.activate_process_thread(thread, server_pid, server_tid, false) .map(|_| Ok(xous_kernel::Result::Message(envelope))) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)) - } else if blocking && !cfg!(baremetal) { - klog!("Blocking client, since it sent a blocking message"); - ss.switch_from_thread(pid, thread)?; - ss.switch_to_thread(server_pid, Some(server_tid))?; - ss.set_thread_result( - server_pid, - server_tid, - xous_kernel::Result::Message(envelope), - ) - .map(|_| xous_kernel::Result::BlockedProcess) - } else if cfg!(baremetal) { - klog!("Setting the return value of the Server ({}:{}) to {:?} and returning to Client", - server_pid, server_tid, envelope); - ss.set_thread_result( - server_pid, - server_tid, - xous_kernel::Result::Message(envelope), - ) - .map(|_| xous_kernel::Result::Ok) - } else { - klog!( - "setting the return value of the Server to {:?} and returning to Client", - envelope - ); - // "Switch to" the server PID when not running on bare metal. This ensures - // that it's "Running". - ss.switch_to_thread(server_pid, Some(server_tid))?; - ss.set_thread_result( - server_pid, - server_tid, - xous_kernel::Result::Message(envelope), - ) - .map(|_| xous_kernel::Result::Ok) - }; - } + } + } else if blocking && !cfg!(baremetal) { + klog!("Blocking client, since it sent a blocking message"); + ss.switch_from_thread(pid, thread)?; + ss.switch_to_thread(server_pid, Some(server_tid))?; + ss.set_thread_result( + server_pid, + server_tid, + xous_kernel::Result::Message(envelope), + ) + .map(|_| xous_kernel::Result::BlockedProcess) + } else if cfg!(baremetal) { + klog!("Setting the return value of the Server ({}:{}) to {:?} and returning to Client", + server_pid, server_tid, envelope); + ss.set_thread_result( + server_pid, + server_tid, + xous_kernel::Result::Message(envelope), + ) + .map(|_| xous_kernel::Result::Ok) + } else { + klog!( + "setting the return value of the Server to {:?} and returning to Client", + envelope + ); + // "Switch to" the server PID when not running on bare metal. This ensures + // that it's "Running". + ss.switch_to_thread(server_pid, Some(server_tid))?; + ss.set_thread_result( + server_pid, + server_tid, + xous_kernel::Result::Message(envelope), + ) + .map(|_| xous_kernel::Result::Ok) + }; } klog!( "no threads available in PID {} to handle this message, so queueing", @@ -344,12 +361,14 @@ fn return_memory( len.get(), )?; + let client_is_runnable = ss.runnable(client_pid, Some(client_tid))?; + // Unblock the client context to allow it to continue. - if !cfg!(baremetal) || in_irq { + if !cfg!(baremetal) || in_irq || !client_is_runnable { // Send a message to the client, in order to wake it up // print!(" [waking up PID {}:{}]", client_pid, client_tid); ss.ready_thread(client_pid, client_tid)?; - ss.switch_to_thread(client_pid, Some(client_tid))?; + // ss.switch_to_thread(client_pid, Some(client_tid))?; ss.set_thread_result( client_pid, client_tid, @@ -415,7 +434,9 @@ fn return_scalar( } }; - if !cfg!(baremetal) || in_irq { + let client_is_runnable = ss.runnable(client_pid, Some(client_tid))?; + + if !cfg!(baremetal) || in_irq || !client_is_runnable { // In a hosted environment, `switch_to_thread()` doesn't continue // execution from the new thread. Instead it continues in the old // thread. Therefore, we need to instruct the client to resume, and @@ -423,7 +444,7 @@ fn return_scalar( // In a baremetal environment, the opposite is true -- we instruct // the server to resume and return to the client. ss.set_thread_result(client_pid, client_tid, xous_kernel::Result::Scalar1(arg))?; - if in_irq { + if cfg!(baremetal) { ss.ready_thread(client_pid, client_tid)?; } Ok(xous_kernel::Result::Ok) @@ -484,7 +505,9 @@ fn return_scalar2( } }; - if !cfg!(baremetal) || in_irq { + let client_is_runnable = ss.runnable(client_pid, Some(client_tid))?; + + if !cfg!(baremetal) || in_irq || !client_is_runnable { // In a hosted environment, `switch_to_thread()` doesn't continue // execution from the new thread. Instead it continues in the old // thread. Therefore, we need to instruct the client to resume, and @@ -496,7 +519,7 @@ fn return_scalar2( client_tid, xous_kernel::Result::Scalar2(arg1, arg2), )?; - if in_irq { + if cfg!(baremetal) { ss.ready_thread(client_pid, client_tid)?; } Ok(xous_kernel::Result::Ok)