diff --git a/kernel/src/arch/hosted/mod.rs b/kernel/src/arch/hosted/mod.rs index 23fa70293..35e243168 100644 --- a/kernel/src/arch/hosted/mod.rs +++ b/kernel/src/arch/hosted/mod.rs @@ -258,7 +258,7 @@ pub fn idle() -> bool { // Note that in this system, multiple processes can be active at once. This is // similar to having one core for each process. However, each process may only have // one context running at a time. - SystemServices::with_mut(|ss| ss.switch_to(new_pid, Some(1))).unwrap(); + SystemServices::with_mut(|ss| ss.switch_to_thread(new_pid, Some(1))).unwrap(); } ThreadMessage::SysCall(pid, thread_id, call) => { // println!("KERNEL({}): Received syscall {:?}", pid, call); diff --git a/kernel/src/arch/hosted/process.rs b/kernel/src/arch/hosted/process.rs index 3f3e795a8..150dfc493 100644 --- a/kernel/src/arch/hosted/process.rs +++ b/kernel/src/arch/hosted/process.rs @@ -6,7 +6,7 @@ use std::net::TcpStream; use std::thread_local; use xous::{ContextInit, TID, PID}; -pub const INITIAL_CONTEXT: usize = 1; +pub const INITIAL_TID: usize = 1; pub struct Process { pid: PID, @@ -236,7 +236,7 @@ impl Process { inner: Default::default(), conn: init_data.conn, memory_to_return: None, - current_context: INITIAL_CONTEXT, + current_context: INITIAL_TID, contexts: [Context {allocated: false}; MAX_CONTEXT], }; if pid_idx >= process_table.table.len() { diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 17d4fef14..5f865113f 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -15,7 +15,7 @@ use xous::{pid_from_usize, ContextInit, Error, MemoryAddress, Message, TID, CID, const MAX_PROCESS_COUNT: usize = 32; const MAX_SERVER_COUNT: usize = 32; -pub use crate::arch::process::INITIAL_CONTEXT; +pub use crate::arch::process::INITIAL_TID; /// A big unifying struct containing all of the system state. /// This is inherited from the stage 1 bootloader. @@ -200,7 +200,7 @@ thread_local!(static SYSTEM_SERVICES: RefCell = RefCell::new(Sys pid: unsafe { PID::new_unchecked(1) }, mapping: arch::mem::DEFAULT_MEMORY_MAPPING, current_context: 0, - previous_context: INITIAL_CONTEXT as u8, + previous_context: INITIAL_TID as u8, }; MAX_PROCESS_COUNT], // Note we can't use MAX_SERVER_COUNT here because of how Rust's // macro tokenization works @@ -281,8 +281,8 @@ impl SystemServices { // Set up our handle with a bogus sp and pc. These will get updated // once a context switch _away_ from the kernel occurs, however we need // to make sure other fields such as "thread number" are all valid. - // ProcessHandle::get().init(0, 0, INITIAL_CONTEXT); - self.processes[0].current_context = INITIAL_CONTEXT as u8; + // ProcessHandle::get().init(0, 0, INITIAL_TID); + self.processes[0].current_context = INITIAL_TID as u8; } /// Add a new entry to the process table. This results in a new address space @@ -480,24 +480,24 @@ impl SystemServices { /// Mark the specified context as ready to run. If the thread is Sleeping, mark /// it as Ready. - pub fn ready_context(&mut self, pid: PID, context: TID) -> Result<(), xous::Error> { - assert!(context == INITIAL_CONTEXT); + pub fn ready_thread(&mut self, pid: PID, tid: TID) -> Result<(), xous::Error> { + assert!(tid == INITIAL_TID); let process = self.get_process_mut(pid)?; process.state = match process.state { ProcessState::Free => panic!( "PID {} was not running, so cannot wake context {}", - pid, context + pid, tid ), - ProcessState::Running(x) if x & (1 << context) == 0 => { - ProcessState::Running(x | (1 << context)) + ProcessState::Running(x) if x & (1 << tid) == 0 => { + ProcessState::Running(x | (1 << tid)) } - ProcessState::Ready(x) if x & (1 << context) == 0 => { - ProcessState::Ready(x | (1 << context)) + ProcessState::Ready(x) if x & (1 << tid) == 0 => { + ProcessState::Ready(x | (1 << tid)) } - ProcessState::Sleeping => ProcessState::Ready(1 << context), + ProcessState::Sleeping => ProcessState::Ready(1 << tid), other => panic!( "PID {} was not in a state to wake context {}: {:?}", - pid, context, other + pid, tid, other ), }; // println!( @@ -512,7 +512,7 @@ impl SystemServices { /// # Panics /// /// If the current process is not running, or if it's "Running" but has no free contexts - pub fn switch_to(&mut self, pid: PID, context: Option) -> Result<(), xous::Error> { + pub fn switch_to_thread(&mut self, pid: PID, context: Option) -> Result<(), xous::Error> { let process = self.get_process_mut(pid)?; // println!( // "switch_to({}:{:?}): Old state was {:?}", @@ -528,17 +528,17 @@ impl SystemServices { process.activate()?; // If a context is specified for a Setup task to switch to, - // ensure it's the INITIAL_CONTEXT. Otherwise it's not valid. + // ensure it's the INITIAL_TID. Otherwise it's not valid. if let Some(ctx) = context { - if ctx != INITIAL_CONTEXT { + if ctx != INITIAL_TID { return Err(xous::Error::InvalidContext); } } let mut p = crate::arch::process::Process::current(); - p.setup_context(INITIAL_CONTEXT, setup)?; - p.set_context(INITIAL_CONTEXT)?; - process.current_context = INITIAL_CONTEXT as u8; + p.setup_context(INITIAL_TID, setup)?; + p.set_context(INITIAL_TID)?; + process.current_context = INITIAL_TID as u8; // Mark the current proces state as "running, and no waiting contexts" ProcessState::Running(0) @@ -634,11 +634,10 @@ impl SystemServices { /// # Panics /// /// If the current process is not running. - pub fn switch_from( + pub fn switch_from_thread( &mut self, pid: PID, - context: TID, - can_resume: bool, + tid: TID, ) -> Result<(), xous::Error> { let process = self.get_process_mut(pid)?; // println!( @@ -646,31 +645,31 @@ impl SystemServices { // pid, context, process.state // ); process.state = match process.state { - ProcessState::Running(x) if x & (1 << context) != 0 => panic!( - "PID {} context {} was already queued for running when `switch_from()` was called", - pid, context + ProcessState::Running(x) if x & (1 << tid) != 0 => panic!( + "PID {} thread {} was already queued for running when `switch_from_thread()` was called", + pid, tid ), ProcessState::Running(x) if x == 0 => { - if can_resume { - if cfg!(baremetal) { - ProcessState::Ready(1 << context) - } else { - ProcessState::Running(1 << context) - } - } else { + // if can_resume { + // if cfg!(baremetal) { + // ProcessState::Ready(1 << tid) + // } else { + // ProcessState::Running(1 << tid) + // } + // } else { ProcessState::Sleeping - } + // } } ProcessState::Running(x) => { if cfg!(baremetal) { - ProcessState::Ready(x | if can_resume { 1 << context } else { 0 }) + ProcessState::Ready(x) } else { - ProcessState::Running(x | if can_resume { 1 << context } else { 0 }) + ProcessState::Running(x) } } other => panic!( - "PID {} was not in a state to be switched from: {:?}", - pid, other + "PID {} TID {} was not in a state to be switched from: {:?}", + pid, tid, other ), }; // println!( @@ -680,7 +679,7 @@ impl SystemServices { Ok(()) } - pub fn set_context_result( + pub fn set_thread_result( &mut self, pid: PID, context: TID, @@ -726,7 +725,7 @@ impl SystemServices { // Ensure the new process can be run. match new.state { ProcessState::Free => return Err(xous::Error::ProcessNotFound), - ProcessState::Setup(_) => new_context = INITIAL_CONTEXT, + ProcessState::Setup(_) => new_context = INITIAL_TID, ProcessState::Running(x) | ProcessState::Ready(x) => { // If no new context is specified, take the previous // context. If that is not runnable, do a round-robin @@ -780,7 +779,7 @@ impl SystemServices { // "Initializing new process with stack size of {} bytes", // stack_size // ); - // // process.init(entrypoint, stack, INITIAL_CONTEXT); + // // process.init(entrypoint, stack, INITIAL_TID); // // Mark the stack as "unallocated-but-free" // let init_sp = stack & !0xfff; // let mut memory_manager = MemoryManagerHandle::get(); diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 9c2b4c35e..851af3d65 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -123,7 +123,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes // Mark the server's context as "Ready". If this fails, return the context // to the blocking list. - ss.ready_context(server_pid, ctx_number).or_else(|e| { + ss.ready_thread(server_pid, ctx_number).or_else(|e| { ss.server_from_sidx(sidx) .expect("server couldn't be located") .return_available_thread(thread); @@ -137,20 +137,20 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes .unwrap_or(Err(xous::Error::ProcessNotFound)) } else if blocking && !cfg!(baremetal) { // println!("Blocking client, since it sent a blocking message"); - ss.switch_to(server_pid, None)?; - ss.set_context_result(server_pid, ctx_number, xous::Result::Message(envelope)) - .and_then(|_| ss.switch_from(pid, thread, false)) + ss.switch_to_thread(server_pid, None)?; + ss.set_thread_result(server_pid, ctx_number, xous::Result::Message(envelope)) + .and_then(|_| ss.switch_from_thread(pid, thread)) .map(|_| xous::Result::BlockedProcess) } else if cfg!(baremetal) { // println!("Setting the return value of the Server and returning to Client"); - ss.set_context_result(server_pid, ctx_number, xous::Result::Message(envelope)) + ss.set_thread_result(server_pid, ctx_number, xous::Result::Message(envelope)) .map(|_| xous::Result::Ok) } else { // println!("Setting the return value of the Server and returning to Client"); // "Switch to" the server PID when not running on bare metal. This ensures // that it's "Running". - ss.switch_to(server_pid, None)?; - ss.set_context_result(server_pid, ctx_number, xous::Result::Message(envelope)) + ss.switch_to_thread(server_pid, None)?; + ss.set_thread_result(server_pid, ctx_number, xous::Result::Message(envelope)) .map(|_| xous::Result::Ok) } } else { @@ -162,7 +162,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes // equivalent to a "Yield". if blocking { if !cfg!(baremetal) { - ss.switch_from(pid, thread, false)?; + ss.switch_from_thread(pid, thread)?; Ok(xous::Result::BlockedProcess) } else { // println!("Returning to parent"); @@ -222,7 +222,7 @@ fn receive_message(pid: PID, tid: TID, sid: SID) -> SysCallResult { // For hosted targets, simply return `BlockedProcess` indicating we'll make // a callback to their socket at a later time. else { - ss.switch_from(pid, tid, false) + ss.switch_from_thread(pid, tid) .map(|_| xous::Result::BlockedProcess) } }) @@ -493,18 +493,18 @@ pub fn handle( // "KERNEL({}): Unblocking PID {} CTX {}", // pid, client_pid, client_ctx // ); - ss.ready_context(client_pid, client_ctx)?; - ss.set_context_result(client_pid, client_ctx, xous::Result::Ok)?; - ss.switch_to(client_pid, Some(client_ctx))?; + ss.ready_thread(client_pid, client_ctx)?; + ss.set_thread_result(client_pid, client_ctx, xous::Result::Ok)?; + ss.switch_to_thread(client_pid, Some(client_ctx))?; Ok(xous::Result::Ok) }) } SysCall::SendMessage(cid, message) => send_message(pid, tid, cid, message), SysCall::TerminateProcess => SystemServices::with_mut(|ss| { - ss.switch_from(pid, tid, false)?; + ss.switch_from_thread(pid, tid)?; let ppid = ss.terminate_process(pid)?; if cfg!(baremetal) { - ss.switch_to(ppid, None) + ss.switch_to_thread(ppid, None) .map(|_| xous::Result::ResumeProcess) } else { Ok(xous::Result::Ok) diff --git a/kernel/src/test/mod.rs b/kernel/src/test/mod.rs index ea3a965a3..eca1a6cd6 100644 --- a/kernel/src/test/mod.rs +++ b/kernel/src/test/mod.rs @@ -482,3 +482,10 @@ fn multiple_contexts() { main_thread.join().expect("couldn't join kernel process"); } + +#[test] +fn multiple_multiple_contexts() { + for _ in 0..60 { + multiple_contexts(); + } +}