From 1a6b1160a362d983fcc376d5e7628388c94ad73c Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Tue, 21 Jul 2020 09:44:16 +0800 Subject: [PATCH] syscall: move send_message into its own function This breaks up the giant `handle()` function. Signed-off-by: Sean Cross --- kernel/src/arch/hosted/irq.rs | 6 +- kernel/src/arch/hosted/mod.rs | 75 +++--- kernel/src/arch/hosted/process.rs | 133 ++++++---- kernel/src/server.rs | 14 +- kernel/src/services.rs | 80 ++++-- kernel/src/syscall.rs | 399 +++++++++++++++--------------- kernel/src/test/mod.rs | 27 +- xous-rs/src/arch/hosted/mod.rs | 268 ++++++++++++-------- xous-rs/src/definitions.rs | 6 +- xous-rs/src/syscall.rs | 1 + 10 files changed, 577 insertions(+), 432 deletions(-) diff --git a/kernel/src/arch/hosted/irq.rs b/kernel/src/arch/hosted/irq.rs index c153f568f..9744eda1c 100644 --- a/kernel/src/arch/hosted/irq.rs +++ b/kernel/src/arch/hosted/irq.rs @@ -1,4 +1,4 @@ -use xous::{ThreadID, PID}; +use xous::{TID, PID}; pub fn disable_all_irqs() { // There are no IRQs in a hosted environment, so there's nothing to do. @@ -16,10 +16,10 @@ pub fn disable_irq(_irq_no: usize) -> Result<(), xous::Error> { Err(xous::Error::UnhandledSyscall) } -pub unsafe fn take_isr_return_pair() -> Option<(PID, ThreadID)> { +pub unsafe fn take_isr_return_pair() -> Option<(PID, TID)> { unimplemented!() } -pub unsafe fn set_isr_return_pair(_pid: PID, _ctx: ThreadID) { +pub unsafe fn set_isr_return_pair(_pid: PID, _ctx: TID) { unimplemented!() } diff --git a/kernel/src/arch/hosted/mod.rs b/kernel/src/arch/hosted/mod.rs index 8de21b755..23fa70293 100644 --- a/kernel/src/arch/hosted/mod.rs +++ b/kernel/src/arch/hosted/mod.rs @@ -16,10 +16,10 @@ use std::time::Duration; use crate::arch::process::Process; use crate::services::SystemServices; -use xous::{MemoryAddress, Result, SysCall, PID}; +use xous::{MemoryAddress, Result, SysCall, TID, PID}; enum ThreadMessage { - SysCall(PID, SysCall), + SysCall(PID, TID, SysCall), NewConnection(TcpStream), } @@ -58,8 +58,12 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender) // "KERNEL({}): Client disconnected: {} ({:?}). Shutting down virtual process.", // pid, e, e // ); - chn.send(ThreadMessage::SysCall(pid, xous::SysCall::TerminateProcess)) - .unwrap(); + chn.send(ThreadMessage::SysCall( + pid, + 1, + xous::SysCall::TerminateProcess, + )) + .unwrap(); return; } // std::thread::sleep(Duration::from_millis(20)); @@ -70,7 +74,7 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender) *word = usize::from_le_bytes(incoming_word); } - let _thread_id = pkt[0]; + let thread_id = pkt[0]; let call = xous::SysCall::from_args( pkt[1], pkt[2], pkt[3], pkt[4], pkt[5], pkt[6], pkt[7], pkt[8], ); @@ -90,6 +94,7 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender) // println!("KERNEL({}): Read Error {}", pid, _e); chn.send(ThreadMessage::SysCall( pid, + thread_id, xous::SysCall::TerminateProcess, )) .unwrap(); @@ -110,10 +115,12 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender) sliced_data.len(), msg.buf.len() ); - msg.buf.addr = MemoryAddress::new(Box::into_raw(sliced_data) - as *mut usize - as usize) - .unwrap(); + msg.buf.addr = match MemoryAddress::new(Box::into_raw(sliced_data) + as *mut usize + as usize) { + Some(a) => a, + _ => unreachable!(), + }; } xous::Message::Scalar(_) => (), } @@ -122,7 +129,7 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender) // "Received packet: {:08x} {} {} {} {} {} {} {}: {:?}", // pkt[0], pkt[1], pkt[2], pkt[3], pkt[4], pkt[5], pkt[6], pkt[7], call // ); - chn.send(ThreadMessage::SysCall(pid, call)) + chn.send(ThreadMessage::SysCall(pid, thread_id, call)) .expect("couldn't make syscall"); } } @@ -253,7 +260,7 @@ pub fn idle() -> bool { // one context running at a time. SystemServices::with_mut(|ss| ss.switch_to(new_pid, Some(1))).unwrap(); } - ThreadMessage::SysCall(pid, call) => { + ThreadMessage::SysCall(pid, thread_id, call) => { // println!("KERNEL({}): Received syscall {:?}", pid, call); crate::arch::process::set_current_pid(pid); // println!("KERNEL({}): Now running as the new process", pid); @@ -270,47 +277,47 @@ pub fn idle() -> bool { // println!("KERNEL: Detected shutdown -- sending final \"Ok\" to the client"); let mut process = Process::current(); let mut response_vec = Vec::new(); + response_vec.extend_from_slice(&thread_id.to_le_bytes()); for word in Result::Ok.to_args().iter_mut() { response_vec.extend_from_slice(&word.to_le_bytes()); } process.send(&response_vec).unwrap_or_else(|_e| { // If we're unable to send data to the process, assume it's dead and terminate it. // println!("Unable to send response to process: {:?} -- terminating", _e); - crate::syscall::handle(pid, SysCall::TerminateProcess).ok(); + crate::syscall::handle(pid, thread_id, SysCall::TerminateProcess).ok(); }); // println!("KERNEL: Done sending"); } // Handle the syscall within the Xous kernel - let response = crate::syscall::handle(pid, call).unwrap_or_else(Result::Error); + let response = crate::syscall::handle(pid, thread_id, call).unwrap_or_else(Result::Error); // println!("KERNEL({}): Syscall response {:?}", pid, response); // There's a response if it wasn't a blocked process and we're not terminating. // Send the response back to the target. if response != Result::BlockedProcess && !is_terminate && !is_shutdown { - { - // The syscall may change what the current process is, but we always - // want to send a response to the process where the request came from. - // For this block, switch to the original PID, send the message, then - // switch back. - let existing_pid = crate::arch::process::current_pid(); - crate::arch::process::set_current_pid(pid); + // The syscall may change what the current process is, but we always + // want to send a response to the process where the request came from. + // For this block, switch to the original PID, send the message, then + // switch back. + let existing_pid = crate::arch::process::current_pid(); + crate::arch::process::set_current_pid(pid); - let mut process = Process::current(); - let mut response_vec = Vec::new(); - for word in response.to_args().iter_mut() { - response_vec.extend_from_slice(&word.to_le_bytes()); - } - process.send(&response_vec).unwrap_or_else(|_e| { - // If we're unable to send data to the process, assume it's dead and terminate it. - // println!( - // "KERNEL({}): Unable to send response to process: {:?} -- terminating", - // pid, _e - // ); - crate::syscall::handle(pid, SysCall::TerminateProcess).ok(); - }); - crate::arch::process::set_current_pid(existing_pid); + let mut process = Process::current(); + let mut response_vec = Vec::new(); + response_vec.extend_from_slice(&thread_id.to_le_bytes()); + for word in response.to_args().iter_mut() { + response_vec.extend_from_slice(&word.to_le_bytes()); } + process.send(&response_vec).unwrap_or_else(|_e| { + // If we're unable to send data to the process, assume it's dead and terminate it. + // println!( + // "KERNEL({}): Unable to send response to process: {:?} -- terminating", + // pid, _e + // ); + crate::syscall::handle(pid, thread_id, SysCall::TerminateProcess).ok(); + }); + crate::arch::process::set_current_pid(existing_pid); // SystemServices::with_mut(|ss| { // ss.switch_from(pid, 1, true)}).unwrap(); } diff --git a/kernel/src/arch/hosted/process.rs b/kernel/src/arch/hosted/process.rs index daf9a2a16..44db55b50 100644 --- a/kernel/src/arch/hosted/process.rs +++ b/kernel/src/arch/hosted/process.rs @@ -1,20 +1,15 @@ -pub const MAX_CONTEXT: ThreadID = 31; +pub const MAX_CONTEXT: TID = 31; use crate::services::ProcessInner; use core::cell::RefCell; use std::io::Write; use std::net::TcpStream; use std::thread_local; -use xous::{ContextInit, ThreadID, PID}; +use xous::{ContextInit, TID, PID}; pub const INITIAL_CONTEXT: usize = 1; pub struct Process { pid: PID, - - /// This enables the kernel to keep track of threads in the - /// target process, and know which threads are ready to - /// receive messages. - contexts: [Context; MAX_CONTEXT], } struct ProcessImpl { @@ -26,6 +21,14 @@ struct ProcessImpl { /// Memory that may need to be returned to the caller memory_to_return: Option>, + + /// This enables the kernel to keep track of threads in the + /// target process, and know which threads are ready to + /// receive messages. + contexts: [Context; MAX_CONTEXT], + + /// The currently-active thread for this proces + current_context: TID, } impl PartialEq for Process { @@ -83,7 +86,7 @@ impl ProcessInit { impl Process { pub fn current() -> Process { let current_pid = PROCESS_TABLE.with(|pt| pt.borrow().current); - Process { pid: current_pid, contexts: Default::default() } + Process { pid: current_pid } } /// Mark this process as running (on the current core?!) @@ -122,54 +125,88 @@ impl Process { pub fn setup_context( &mut self, - _context: ThreadID, + context: TID, _setup: ContextInit, ) -> Result<(), xous::Error> { - // if context != INITIAL_CONTEXT { - // return Err(xous::Error::ProcessNotFound); - // } - Ok(()) - } - - pub fn current_context(&mut self) -> ThreadID { - INITIAL_CONTEXT - } - - /// Set the current context number. - pub fn set_context(&mut self, context: ThreadID) -> Result<(), xous::Error> { - if context != INITIAL_CONTEXT { - panic!("context was {}, not 1", context); - } - Ok(()) - } - - #[allow(dead_code)] - pub fn find_free_context_nr(&self) -> Option { - for (index, context) in self.contexts.iter().enumerate() { - if index != 0 && context.allocated == false { - return Some(index as ThreadID + 1); - } - } - None - } - - pub fn set_context_result(&mut self, context: ThreadID, result: xous::Result) { - assert!( - context == INITIAL_CONTEXT, - "context is {} and not {}", + println!( + "KERNEL({}): Setting up context {} @ {:?}", + self.pid, context, - INITIAL_CONTEXT + std::thread::current() ); - let mut response = vec![]; - for word in result.to_args().iter_mut() { - response.extend_from_slice(&word.to_le_bytes()); - } - + assert!(context > 0); PROCESS_TABLE.with(|pt| { let mut process_table = pt.borrow_mut(); let current_pid_idx = process_table.current.get() as usize - 1; let process = &mut process_table.table[current_pid_idx].as_mut().unwrap(); + assert!(!process.contexts[context - 1].allocated); + process.contexts[context - 1].allocated = true; + println!( + "KERNEL({}): self.contexts[{}].allocated = {}", + current_pid_idx, + context - 1, + process.contexts[context - 1].allocated + ); + }); + Ok(()) + } + + pub fn current_context(&mut self) -> TID { + PROCESS_TABLE.with(|pt| { + let mut process_table = pt.borrow_mut(); + let current_pid_idx = process_table.current.get() as usize - 1; + let process = &mut process_table.table[current_pid_idx].as_mut().unwrap(); + process.current_context + }) + } + + /// Set the current context number. + pub fn set_context(&mut self, context: TID) -> Result<(), xous::Error> { + assert!(context > 0); + PROCESS_TABLE.with(|pt| { + let mut process_table = pt.borrow_mut(); + let current_pid_idx = process_table.current.get() as usize - 1; + let process = &mut process_table.table[current_pid_idx].as_mut().unwrap(); + assert!(process.contexts[context - 1].allocated); + process.current_context = context; + }); + Ok(()) + } + + #[allow(dead_code)] + pub fn find_free_context_nr(&self) -> Option { + PROCESS_TABLE.with(|pt| { + let mut process_table = pt.borrow_mut(); + let current_pid_idx = process_table.current.get() as usize - 1; + let process = &mut process_table.table[current_pid_idx].as_mut().unwrap(); + for (index, context) in process.contexts.iter().enumerate() { + if index != 0 && !context.allocated { + return Some(index as TID + 1); + } + } + None + }) + } + + pub fn set_context_result(&mut self, context: TID, result: xous::Result) { + assert!(context > 0); + PROCESS_TABLE.with(|pt| { + let mut process_table = pt.borrow_mut(); + let current_pid_idx = process_table.current.get() as usize - 1; + let process = &mut process_table.table[current_pid_idx].as_mut().unwrap(); + assert!( + process.contexts[context - 1].allocated, + "context {} is not allocated", + context, + ); + + let mut response = vec![]; + response.extend_from_slice(&context.to_le_bytes()); + for word in result.to_args().iter_mut() { + response.extend_from_slice(&word.to_le_bytes()); + } + if let Some(buf) = process.memory_to_return.take() { response.extend_from_slice(&buf); } @@ -199,6 +236,8 @@ impl Process { inner: Default::default(), conn: init_data.conn, memory_to_return: None, + current_context: INITIAL_CONTEXT, + contexts: [Context {allocated: false}; MAX_CONTEXT], }; if pid_idx >= process_table.table.len() { process_table.table.push(Some(process)); diff --git a/kernel/src/server.rs b/kernel/src/server.rs index c187d4d1f..e25ccd74f 100644 --- a/kernel/src/server.rs +++ b/kernel/src/server.rs @@ -1,6 +1,6 @@ pub use crate::arch::process::Context; use core::mem; -use xous::{ThreadID, MemoryAddress, MemoryRange, MemorySize, Message, PID, SID}; +use xous::{TID, MemoryAddress, MemoryRange, MemorySize, Message, PID, SID}; pub struct SenderID { pub sidx: usize, @@ -21,7 +21,7 @@ pub enum WaitingMessage { None, /// The memory was borrowed and should be returned to the given process. - BorrowedMemory(PID, ThreadID, MemoryAddress, MemoryAddress, MemorySize), + BorrowedMemory(PID, TID, MemoryAddress, MemoryAddress, MemorySize), /// The memory was moved, and so shouldn't be returned. MovedMemory, @@ -504,7 +504,7 @@ impl Server { pub fn queue_message( &mut self, pid: PID, - context: ThreadID, + context: TID, message: xous::Message, original_address: Option, ) -> core::result::Result { @@ -567,7 +567,7 @@ impl Server { pub fn queue_address( &mut self, pid: PID, - context: ThreadID, + context: TID, message: &Message, client_address: Option, ) -> core::result::Result { @@ -605,7 +605,7 @@ impl Server { /// Return a context ID that is available and blocking. If no such context /// ID exists, or if this server isn't actually ready to receive packets, /// return None. - pub fn take_available_context(&mut self) -> Option { + pub fn take_available_context(&mut self) -> Option { if self.ready_contexts == 0 { return None; } @@ -635,7 +635,7 @@ impl Server { /// # Panics /// /// If the context cannot be returned because it is already blocking. - pub fn return_available_context(&mut self, ctx_number: ThreadID) { + pub fn return_available_context(&mut self, ctx_number: TID) { if self.ready_contexts & 1 << ctx_number != 0 { panic!( "tried to return context {}, but it was already blocking", @@ -646,7 +646,7 @@ impl Server { } /// Add the given context to the list of ready and waiting contexts. - pub fn park_context(&mut self, context: ThreadID) { + pub fn park_context(&mut self, context: TID) { // println!("KERNEL({}): Parking context: {}", self.pid, context); self.ready_contexts |= 1 << context; } diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 4b99a45a8..f3362c088 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -10,7 +10,7 @@ use crate::filled_array; // use crate::mem::{MemoryManagerHandle, PAGE_SIZE}; use crate::server::Server; // use core::mem; -use xous::{pid_from_usize, ContextInit, Error, MemoryAddress, Message, ThreadID, CID, PID, SID}; +use xous::{pid_from_usize, ContextInit, Error, MemoryAddress, Message, TID, CID, PID, SID}; const MAX_PROCESS_COUNT: usize = 32; const MAX_SERVER_COUNT: usize = 32; @@ -366,7 +366,7 @@ impl SystemServices { pub fn finish_callback_and_resume( &mut self, pid: PID, - context: ThreadID, + context: TID, ) -> Result<(), xous::Error> { // Get the current process (which was the interrupt handler) and mark it // as Ready. Note that the new PID may very well be the same PID. @@ -480,7 +480,7 @@ 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: ThreadID) -> Result<(), xous::Error> { + pub fn ready_context(&mut self, pid: PID, context: TID) -> Result<(), xous::Error> { assert!(context == INITIAL_CONTEXT); let process = self.get_process_mut(pid)?; process.state = match process.state { @@ -500,7 +500,10 @@ impl SystemServices { pid, context, other ), }; - // println!("KERNEL({}): Readying context {} -> {:?}", pid, context, process.state); + println!( + "KERNEL({}): Readying context {} -> {:?}", + pid, context, process.state + ); Ok(()) } @@ -509,10 +512,14 @@ 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(&mut self, pid: PID, context: Option) -> Result<(), xous::Error> { // println!("KERNEL(?): Getting current process..."); let process = self.get_process_mut(pid)?; // println!("KERNEL(?): Current process is {:?}", process); + println!( + "switch_to({}:{:?}): Old state was {:?}", + pid, context, process.state + ); // Determine which context number to switch to process.state = match process.state { @@ -613,14 +620,18 @@ impl SystemServices { ProcessState::Running(new_mask) } }; + println!( + "switch_to({}:{:?}): New state is {:?}", + pid, context, process.state + ); // self.pid = pid; Ok(()) } - /// Switches away from the specified context ID. - /// If `can_resume` is `true`, then the current context ID will be placed - /// in the list of available context IDs. - /// If no context IDs are available, the thread will enter a `Sleeping` state. + /// Switches away from the specified process ID. + /// If `can_resume` is `true`, then the current thread ID will be placed + /// in the list of available thread IDs. + /// If no thread IDs are available, the process will enter a `Sleeping` state. /// /// # Panics /// @@ -628,24 +639,35 @@ impl SystemServices { pub fn switch_from( &mut self, pid: PID, - context: ThreadID, + context: TID, can_resume: bool, ) -> Result<(), xous::Error> { let process = self.get_process_mut(pid)?; + println!( + "switch_from({}:{}): Old state was {:?}", + 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 == 0 { - if can_resume { + ProcessState::Running(x) if x == 0 => { + if can_resume { + if cfg!(baremetal) { ProcessState::Ready(1 << context) } else { - ProcessState::Sleeping + ProcessState::Running(1 << context) } } else { + ProcessState::Sleeping + } + } + ProcessState::Running(x) => { + if cfg!(baremetal) { ProcessState::Ready(x | if can_resume { 1 << context } else { 0 }) + } else { + ProcessState::Running(x | if can_resume { 1 << context } else { 0 }) } } other => panic!( @@ -653,13 +675,17 @@ impl SystemServices { pid, other ), }; + println!( + "switch_from({}:{}): New state is {:?}", + pid, context, process.state + ); Ok(()) } pub fn set_context_result( &mut self, pid: PID, - context: ThreadID, + context: TID, result: xous::Result, ) -> Result<(), xous::Error> { // Temporarily switch into the target process memory space @@ -685,10 +711,10 @@ impl SystemServices { pub fn activate_process_context( &mut self, new_pid: PID, - mut new_context: ThreadID, + mut new_context: TID, can_resume: bool, advance_context: bool, - ) -> Result { + ) -> Result { // println!("Activating PID {}, context {}", new_pid, new_context); let previous_pid = self.current_pid(); let previous_context = self.current_context_nr(previous_pid); @@ -810,14 +836,14 @@ impl SystemServices { }; if advance_context { previous.current_context += 1; - if previous.current_context as ThreadID > arch::process::MAX_CONTEXT { + if previous.current_context as TID > arch::process::MAX_CONTEXT { previous.current_context = 0; } } - // println!( - // "Set previous process PID {} state to {:?} (with can_resume = {})", - // previous_pid, previous.state, can_resume - // ); + println!( + "Set previous process PID {} state to {:?} (with can_resume = {})", + previous_pid, previous.state, can_resume + ); } else { if self.current_context_nr(previous_pid) == new_context { if !can_resume { @@ -844,7 +870,7 @@ impl SystemServices { }; if advance_context { new.current_context += 1; - if new.current_context as ThreadID > arch::process::MAX_CONTEXT { + if new.current_context as TID > arch::process::MAX_CONTEXT { new.current_context = 0; } } @@ -1147,7 +1173,7 @@ impl SystemServices { &mut self, pid: PID, context_init: ContextInit, - ) -> Result { + ) -> Result { let mut process = self .get_process_mut(pid) .expect("couldn't find current process"); @@ -1170,6 +1196,8 @@ impl SystemServices { // &[arg.map(|x| x.get()).unwrap_or_default() as usize], // ); + println!("KERNEL({}): Created new context {}", pid, new_context_nr); + // Queue the thread to run process.state = match process.state { ProcessState::Running(x) => ProcessState::Running(x | (1 << new_context_nr)), @@ -1313,7 +1341,7 @@ impl SystemServices { &mut self, sidx: usize, pid: PID, - context: ThreadID, + context: TID, message: Message, original_address: Option, ) -> Result { @@ -1345,7 +1373,7 @@ impl SystemServices { &mut self, sidx: usize, pid: PID, - context: ThreadID, + context: TID, message: &Message, client_address: Option, ) -> Result { diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index b4ce04785..c42552c1f 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -8,12 +8,201 @@ use core::mem; use xous::*; /// This is the context that called SwitchTo -static mut SWITCHTO_CALLER: Option<(PID, ThreadID)> = None; +static mut SWITCHTO_CALLER: Option<(PID, TID)> = None; -pub fn handle(pid: PID, call: SysCall) -> core::result::Result { +fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallResult { + SystemServices::with_mut(|ss| { + let sidx = ss.sidx_from_cid(cid).ok_or(xous::Error::ServerNotFound)?; + // ::debug_here::debug_here!(); + + let server_pid = ss + .server_from_sidx(sidx) + .expect("server couldn't be located") + .pid; + + // Remember the address the message came from, in case we need to + // return it after the borrow is through. + let client_address = match &message { + Message::Scalar(_) => None, + Message::Move(msg) | Message::MutableBorrow(msg) | Message::Borrow(msg) => { + Some(msg.buf.addr) + } + }; + + // Translate memory messages from the client process to the server + // process. Additionally, determine whether the call is blocking. If + // so, switch to the server context right away. + let (message, blocking) = match message { + Message::Scalar(_) => (message, false), + Message::Move(msg) => { + let new_virt = ss.send_memory( + msg.buf.as_mut_ptr(), + server_pid, + core::ptr::null_mut(), + msg.buf.len(), + )?; + ( + Message::Move(MemoryMessage { + id: msg.id, + buf: MemoryRange::new(new_virt as usize, msg.buf.len()), + offset: msg.offset, + valid: msg.valid, + }), + false, + ) + } + Message::MutableBorrow(msg) => { + let new_virt = ss.lend_memory( + msg.buf.as_mut_ptr(), + server_pid, + core::ptr::null_mut(), + msg.buf.len(), + true, + )?; + ( + Message::MutableBorrow(MemoryMessage { + id: msg.id, + buf: MemoryRange::new(new_virt as usize, msg.buf.len()), + offset: msg.offset, + valid: msg.valid, + }), + true, + ) + } + Message::Borrow(msg) => { + let new_virt = ss.lend_memory( + msg.buf.as_mut_ptr(), + server_pid, + core::ptr::null_mut(), + msg.buf.len(), + false, + )?; + // println!( + // "Lending {} bytes from {:08x} in PID {} to {:08x} in PID {}", + // msg.buf.len(), + // msg.buf.as_mut_ptr() as usize, + // pid, + // new_virt as usize, + // server_pid, + // ); + ( + Message::Borrow(MemoryMessage { + id: msg.id, + buf: MemoryRange::new(new_virt as usize, msg.buf.len()), + offset: msg.offset, + valid: msg.valid, + }), + true, + ) + } + }; + + // If the server has an available context to receive the message, + // transfer it right away. + if let Some(ctx_number) = ss + .server_from_sidx(sidx) + .expect("server couldn't be located") + .take_available_context() + { + // println!( + // "There are contexts available to handle this message. Marking PID {} as Ready", + // server_pid + // ); + let sender = match message { + Message::Scalar(_) | Message::Move(_) => 0, + Message::Borrow(_) | Message::MutableBorrow(_) => ss + .remember_server_message( + sidx, + pid, + thread, + &message, + client_address, + ) + .or_else(|e| { + ss.server_from_sidx(sidx) + .expect("server couldn't be located") + .return_available_context(thread); + Err(e) + })?, + }; + let envelope = MessageEnvelope { sender, message }; + + // 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.server_from_sidx(sidx) + .expect("server couldn't be located") + .return_available_context(thread); + Err(e) + })?; + + if blocking && cfg!(baremetal) { + // println!("Activating Server context and switching away from Client"); + ss.activate_process_context(server_pid, ctx_number, !blocking, blocking) + .map(|_| Ok(xous::Result::Message(envelope))) + .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)) + .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), + ) + .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), + ) + .map(|_| xous::Result::Ok) + } + } else { + // Add this message to the queue. If the queue is full, this + // returns an error. + ss.queue_server_message(sidx, pid, thread, message, client_address)?; + + // Park this context if it's blocking. This is roughly + // equivalent to a "Yield". + if blocking { + if !cfg!(baremetal) { + ss.switch_from(pid, thread, false)?; + Ok(xous::Result::BlockedProcess) + } else { + // println!("Returning to parent"); + let process = ss.get_process(pid).expect("Can't get current process"); + let ppid = process.ppid; + unsafe { SWITCHTO_CALLER = None }; + ss.activate_process_context(ppid, 0, !blocking, blocking) + .map(|_| Ok(xous::Result::ResumeProcess)) + .unwrap_or(Err(xous::Error::ProcessNotFound)) + } + } else { + // println!("Returning to Client with Ok result"); + Ok(xous::Result::Ok) + } + } + }) +} + +pub fn handle(pid: PID, tid: TID, call: SysCall) -> core::result::Result { // let pid = arch::current_pid(); - // println!("PID{} Syscall: {:?}", pid, call); + println!("KERNEL({}): Syscall {:?}", pid, call); match call { SysCall::MapMemory(phys, virt, size, req_flags) => { let mut mm = MemoryManagerHandle::get(); @@ -239,12 +428,11 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result { - SystemServices::with_mut(|ss| { - ss.create_thread(pid, context_init) - .map(xous::Result::ThreadID) - }) - } + SysCall::CreateThread(context_init) => SystemServices::with_mut(|ss| { + #[allow(clippy::unit_arg)] + ss.create_thread(pid, context_init) + .map(xous::Result::ThreadID) + }), SysCall::CreateServer(name) => { SystemServices::with_mut(|ss| ss.create_server(pid, name).map(xous::Result::ServerID)) } @@ -326,198 +514,7 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result { - SystemServices::with_mut(|ss| { - let context_nr = ss.current_context_nr(pid); - let sidx = ss.sidx_from_cid(cid).ok_or(xous::Error::ServerNotFound)?; - // ::debug_here::debug_here!(); - - let server_pid = ss - .server_from_sidx(sidx) - .expect("server couldn't be located") - .pid; - - // Remember the address the message came from, in case we need to - // return it after the borrow is through. - let client_address = match &message { - Message::Scalar(_) => None, - Message::Move(msg) | Message::MutableBorrow(msg) | Message::Borrow(msg) => { - Some(msg.buf.addr) - } - }; - - // Translate memory messages from the client process to the server - // process. Additionally, determine whether the call is blocking. If - // so, switch to the server context right away. - let (message, blocking) = match message { - Message::Scalar(_) => (message, false), - Message::Move(msg) => { - let new_virt = ss.send_memory( - msg.buf.as_mut_ptr(), - server_pid, - core::ptr::null_mut(), - msg.buf.len(), - )?; - ( - Message::Move(MemoryMessage { - id: msg.id, - buf: MemoryRange::new(new_virt as usize, msg.buf.len()), - offset: msg.offset, - valid: msg.valid, - }), - false, - ) - } - Message::MutableBorrow(msg) => { - let new_virt = ss.lend_memory( - msg.buf.as_mut_ptr(), - server_pid, - core::ptr::null_mut(), - msg.buf.len(), - true, - )?; - ( - Message::MutableBorrow(MemoryMessage { - id: msg.id, - buf: MemoryRange::new(new_virt as usize, msg.buf.len()), - offset: msg.offset, - valid: msg.valid, - }), - true, - ) - } - Message::Borrow(msg) => { - let new_virt = ss.lend_memory( - msg.buf.as_mut_ptr(), - server_pid, - core::ptr::null_mut(), - msg.buf.len(), - false, - )?; - // println!( - // "Lending {} bytes from {:08x} in PID {} to {:08x} in PID {}", - // msg.buf.len(), - // msg.buf.as_mut_ptr() as usize, - // pid, - // new_virt as usize, - // server_pid, - // ); - ( - Message::Borrow(MemoryMessage { - id: msg.id, - buf: MemoryRange::new(new_virt as usize, msg.buf.len()), - offset: msg.offset, - valid: msg.valid, - }), - true, - ) - } - }; - - // If the server has an available context to receive the message, - // transfer it right away. - if let Some(ctx_number) = ss - .server_from_sidx(sidx) - .expect("server couldn't be located") - .take_available_context() - { - // println!( - // "There are contexts available to handle this message. Marking PID {} as Ready", - // server_pid - // ); - let sender = match message { - Message::Scalar(_) | Message::Move(_) => 0, - Message::Borrow(_) | Message::MutableBorrow(_) => ss - .remember_server_message( - sidx, - pid, - context_nr, - &message, - client_address, - ) - .or_else(|e| { - ss.server_from_sidx(sidx) - .expect("server couldn't be located") - .return_available_context(context_nr); - Err(e) - })?, - }; - let envelope = MessageEnvelope { sender, message }; - - // 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.server_from_sidx(sidx) - .expect("server couldn't be located") - .return_available_context(context_nr); - Err(e) - })?; - - if blocking && cfg!(baremetal) { - // println!("Activating Server context and switching away from Client"); - ss.activate_process_context(server_pid, ctx_number, !blocking, blocking) - .map(|_| Ok(xous::Result::Message(envelope))) - .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, context_nr, false)) - .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), - ) - .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), - ) - .map(|_| xous::Result::Ok) - } - } else { - // println!("No contexts available to handle this. Queueing message."); - // There is no server context we can use, so add the message to - // the queue. - let context_nr = ss.current_context_nr(pid); - - // Add this message to the queue. If the queue is full, this - // returns an error. - ss.queue_server_message(sidx, pid, context_nr, message, client_address)?; - - // Park this context if it's blocking. This is roughly - // equivalent to a "Yield". - if blocking { - if !cfg!(baremetal) { - ss.switch_from(pid, context_nr, false)?; - Ok(xous::Result::BlockedProcess) - } else { - // println!("Returning to parent"); - let process = ss.get_process(pid).expect("Can't get current process"); - let ppid = process.ppid; - unsafe { SWITCHTO_CALLER = None }; - ss.activate_process_context(ppid, 0, !blocking, blocking) - .map(|_| Ok(xous::Result::ResumeProcess)) - .unwrap_or(Err(xous::Error::ProcessNotFound)) - } - } else { - // println!("Returning to Client with Ok result"); - Ok(xous::Result::Ok) - } - } - }) + send_message(pid, tid, cid, message) } SysCall::TerminateProcess => SystemServices::with_mut(|ss| { let context_nr = ss.current_context_nr(pid); diff --git a/kernel/src/test/mod.rs b/kernel/src/test/mod.rs index 1550cc074..ea3a965a3 100644 --- a/kernel/src/test/mod.rs +++ b/kernel/src/test/mod.rs @@ -377,7 +377,7 @@ fn measure_memory_usage() { { // Run the "shutdown" test in its own thread. This ensures that // any TLS is freed when the thread exits. - let jh = as_process(|| shutdown()); + let jh = as_process(shutdown); jh.join() .expect("couldn't run shutdown test for measuring memory"); } @@ -437,9 +437,11 @@ fn server_client_same_process() { /// Test that one process can have multiple contexts #[test] fn multiple_contexts() { + // ::debug_here::debug_here!(); // Start the kernel in its own thread let main_thread = start_kernel(SERVER_SPEC); + let internal_server = as_process(|| { let server = xous::create_server(0x53_4534).expect("couldn't create server"); let connection = xous::connect(server).expect("couldn't connect to our own server"); @@ -451,17 +453,24 @@ fn multiple_contexts() { arg4: 5, }; - let client_thread = xous::create_thread(move || { + let mut server_threads = vec![]; + for _ in 0..30 { + server_threads.push(xous::create_thread(move || { let msg = xous::receive_message(server).expect("couldn't receive message"); assert_eq!(msg.message, xous::Message::Scalar(msg_contents)); - }).expect("couldn't spawn client thread"); + }).expect("couldn't spawn client thread")); + } - xous::send_message( - connection, - xous::Message::Scalar(msg_contents), - ) - .expect("couldn't send message"); - xous::wait_thread(client_thread).expect("couldn't wait for thread"); + for _ in &server_threads { + xous::send_message( + connection, + xous::Message::Scalar(msg_contents), + ) + .expect("couldn't send message"); + } + for server_thread in server_threads.into_iter() { + xous::wait_thread(server_thread).expect("couldn't wait for thread"); + } }); internal_server diff --git a/xous-rs/src/arch/hosted/mod.rs b/xous-rs/src/arch/hosted/mod.rs index 70cf48f35..caac372b3 100644 --- a/xous-rs/src/arch/hosted/mod.rs +++ b/xous-rs/src/arch/hosted/mod.rs @@ -1,15 +1,24 @@ use std::cell::RefCell; +use std::collections::HashMap; use std::io::{Read, Write}; use std::mem::size_of; use std::net::TcpStream; +use std::sync::{Arc, Mutex}; use std::thread_local; -use crate::{Result, ThreadID}; +use crate::{Result, TID}; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; pub type ContextInit = (); pub struct WaitHandle(std::thread::JoinHandle); +#[derive(Clone)] +struct ServerConnection { + send: Arc>, + recv: Arc>, + mailbox: Arc>>, +} + pub fn context_to_args(call: usize, _init: &ContextInit) -> [usize; 8] { [call, 0, 0, 0, 0, 0, 0, 0] } @@ -27,8 +36,8 @@ pub fn args_to_context( } thread_local!(static NETWORK_CONNECT_ADDRESS: RefCell = RefCell::new(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0))); -thread_local!(static XOUS_SERVER_CONNECTION: RefCell> = RefCell::new(None)); -thread_local!(static THREAD_ID: RefCell = RefCell::new(1)); +thread_local!(static XOUS_SERVER_CONNECTION: RefCell> = RefCell::new(None)); +thread_local!(static THREAD_ID: RefCell = RefCell::new(1)); /// Set the network address for this particular thread. pub fn set_xous_address(new_address: SocketAddr) { @@ -55,7 +64,7 @@ where pub fn create_thread_post( f: F, - thread_id: ThreadID, + thread_id: TID, ) -> core::result::Result, crate::Error> where F: FnOnce() -> T, @@ -63,7 +72,8 @@ where T: Send + 'static, { let server_address = xous_address(); - let server_connection = XOUS_SERVER_CONNECTION.with(|xsc| xsc.borrow().as_ref().unwrap().try_clone().unwrap()); + let server_connection = + XOUS_SERVER_CONNECTION.with(|xsc| xsc.borrow().as_ref().unwrap().clone()); Ok(std::thread::Builder::new() .spawn(move || { set_xous_address(server_address); @@ -71,7 +81,7 @@ where XOUS_SERVER_CONNECTION.with(|xsc| *xsc.borrow_mut() = Some(server_connection)); f() }) - .map(|j| WaitHandle(j)) + .map(WaitHandle) .map_err(|_| crate::Error::InternalError)?) } @@ -97,28 +107,158 @@ pub fn _xous_syscall( ret: &mut Result, ) { XOUS_SERVER_CONNECTION.with(|xsc| { - if xsc.borrow().is_none() { - NETWORK_CONNECT_ADDRESS.with(|nca| { - // println!("Opening connection to Xous server @ {}...", nca.borrow()); - let conn = TcpStream::connect(*nca.borrow()).unwrap(); - *xsc.borrow_mut() = Some(conn); - }); - } - _xous_syscall_to( - nr, - a1, - a2, - a3, - a4, - a5, - a6, - a7, - ret, - xsc.borrow_mut().as_mut().unwrap(), - ) + THREAD_ID.with(|tid| { + let call = crate::SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap(); + + if xsc.borrow().is_none() { + NETWORK_CONNECT_ADDRESS.with(|nca| { + // println!("Opening connection to Xous server @ {}...", nca.borrow()); + let conn = TcpStream::connect(*nca.borrow()).unwrap(); + *xsc.borrow_mut() = Some(ServerConnection { + send: Arc::new(Mutex::new(conn.try_clone().unwrap())), + recv: Arc::new(Mutex::new(conn)), + mailbox: Arc::new(Mutex::new(HashMap::new())), + }); + }); + } + _xous_syscall_to( + nr, + a1, + a2, + a3, + a4, + a5, + a6, + a7, + &call, + &mut xsc.borrow_mut().as_mut().unwrap().send.lock().unwrap(), + ); + _xous_syscall_result( + &call, + ret, + *tid.borrow(), + xsc.borrow_mut().as_mut().unwrap(), + ); + }) }) } +fn _xous_syscall_result( + call: &crate::SysCall, + ret: &mut Result, + thread_id: TID, + server_connection: &ServerConnection, +) { + // Check to see if this thread id has an entry in the mailbox already. + // This will block until the hashmap is free. + { + let mut mailbox = server_connection.mailbox.lock().unwrap(); + if let Some(entry) = mailbox.remove(&thread_id) { + *ret = entry; + return; + } + } + + // Receive the packet back + loop { + let mut stream = server_connection.recv.lock().unwrap(); + + // Now that we have the Stream mutex, temporarily take the Mailbox mutex to see if + // this thread ID is there. If it is, there's no need to read via the network. + // Note that the mailbox mutex is released if it isn't found. + { + let mut mailbox = server_connection.mailbox.lock().unwrap(); + if let Some(entry) = mailbox.remove(&thread_id) { + *ret = entry; + return; + } + } + + // This thread_id doesn't exist in the mailbox, so read additional data. + let mut pkt = [0usize; 8]; + let mut raw_bytes = [0u8; size_of::() * 9]; + stream.read_exact(&mut raw_bytes).expect("Server shut down"); + use std::convert::TryInto; + + let mut raw_bytes_chunks = raw_bytes.chunks(size_of::()); + + // Read the Thread ID, which comes across first, followed by the 8 words of + // the message data. + let msg_thread_id = + usize::from_le_bytes(raw_bytes_chunks.next().unwrap().try_into().unwrap()); + for (pkt_word, word) in pkt.iter_mut().zip(raw_bytes_chunks) { + *pkt_word = usize::from_le_bytes(word.try_into().unwrap()); + } + + let response = Result::from_args(pkt); + + // println!(" Response: {:?}", response); + if Result::BlockedProcess == response { + // println!(" Waiting again"); + continue; + } + if let crate::SysCall::SendMessage(_, ref msg) = call { + match msg { + crate::Message::MutableBorrow(crate::MemoryMessage { + id: _id, + buf, + offset: _offset, + valid: _valid, + }) => { + // Read the buffer back from the remote host. + use core::slice; + let mut data = + unsafe { slice::from_raw_parts_mut(buf.addr.get() as _, buf.size.get()) }; + stream.read_exact(&mut data).expect("Server shut down"); + // pkt.extend_from_slice(data); + } + + crate::Message::Borrow(crate::MemoryMessage { + id: _id, + buf, + offset: _offset, + valid: _valid, + }) => { + // Read the buffer back from the remote host and ensure it's the same + use core::slice; + let mut check_data = Vec::new(); + check_data.resize(buf.len(), 0); + let data = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len()) }; + stream + .read_exact(&mut check_data) + .expect("Server shut down"); + + assert_eq!(data, check_data.as_slice()); + } + + crate::Message::Move(crate::MemoryMessage { + id: _id, + buf: _buf, + offset: _offset, + valid: _valid, + }) => (), + // Nothing to do for Immutable borrow, since the memory can't change + crate::Message::Scalar(_) => (), + } + } + + // Now that we have the Stream mutex, temporarily take the Mailbox mutex to see if + // this thread ID is there. If it is, there's no need to read via the network. + // Note that the mailbox mutex is released if it isn't found. + { + // If the incoming message was for this thread, return it directly. + if msg_thread_id == thread_id { + *ret = response; + return; + } + + // Otherwise, add it to the mailbox and try again. + let mut mailbox = server_connection.mailbox.lock().unwrap(); + mailbox.insert(thread_id, response); + } + } +} + #[allow(clippy::too_many_arguments)] #[no_mangle] fn _xous_syscall_to( @@ -130,14 +270,13 @@ fn _xous_syscall_to( a5: usize, a6: usize, a7: usize, - ret: &mut Result, + call: &crate::SysCall, xsc: &mut TcpStream, ) { // println!( // "Making Syscall: {:?}", // crate::SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap() // ); - let call = crate::SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap(); // Send the packet to the server let mut pkt = vec![]; @@ -175,79 +314,4 @@ fn _xous_syscall_to( } xsc.write_all(&pkt).expect("Server shut down"); - - // Receive the packet back - loop { - let mut pkt = [0usize; 8]; - let mut word = [0u8; size_of::()]; - for pkt_word in pkt.iter_mut() { - xsc.read_exact(&mut word).expect("Server shut down"); - *pkt_word = usize::from_le_bytes(word); - } - - *ret = Result::from_args(pkt); - - // println!(" Response: {:?}", *ret); - if Result::BlockedProcess == *ret { - // println!(" Waiting again"); - } else { - if let crate::SysCall::SendMessage(_, ref msg) = call { - match msg { - crate::Message::MutableBorrow(crate::MemoryMessage { - id: _id, - buf, - offset: _offset, - valid: _valid, - }) => { - // Read the buffer back from the remote host. - use core::slice; - let mut data = unsafe { - slice::from_raw_parts_mut(buf.addr.get() as _, buf.size.get()) - }; - xsc.read_exact(&mut data).expect("Server shut down"); - // pkt.extend_from_slice(data); - } - - crate::Message::Borrow(crate::MemoryMessage { - id: _id, - buf, - offset: _offset, - valid: _valid, - }) => { - // Read the buffer back from the remote host and ensure it's the same - use core::slice; - let mut check_data = Vec::new(); - check_data.resize(buf.len(), 0); - let data = unsafe { - slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len()) - }; - xsc.read_exact(&mut check_data).expect("Server shut down"); - - assert_eq!(data, check_data.as_slice()); - // pkt.extend_from_slice(data); - } - - crate::Message::Move(crate::MemoryMessage { - id: _id, - buf: _buf, - offset: _offset, - valid: _valid, - }) => (),/*{ - let offset = buf.addr.get() as *mut u8; - let size = buf.size.get(); - extern crate alloc; - use alloc::alloc::{dealloc, Layout}; - let layout = Layout::from_size_align(size, 4096).unwrap(); - // Free memory that was moved - unsafe { - dealloc(offset, layout); - } - }*/ - // Nothing to do for Immutable borrow, since the memory can't change - crate::Message::Scalar(_) => (), - } - } - return; - } - } } diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 3c109fb1e..1e58a56c9 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -16,7 +16,7 @@ pub type SID = (usize, usize, usize, usize); pub type CID = usize; /// Context ID -pub type ThreadID = usize; +pub type TID = usize; /// Equivalent to a RISC-V Hart ID pub type CpuID = usize; @@ -368,7 +368,7 @@ pub enum Result { ServerID(SID), ConnectionID(CID), Message(MessageEnvelope), - ThreadID(ThreadID), + ThreadID(TID), /// The requested system call is unimplemented Unimplemented, @@ -452,7 +452,7 @@ impl Result { }; Result::Message(MessageEnvelope { sender, message }) } - 9 => Result::ThreadID(src[1] as ThreadID), + 9 => Result::ThreadID(src[1] as TID), 10 => Result::Unimplemented, 11 => Result::BlockedProcess, _ => Result::UnknownResult(src[1], src[2], src[3], src[4], src[5], src[6], src[7]), diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 983c0c0fb..a995d2119 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -750,6 +750,7 @@ pub fn receive_message(server: SID) -> core::result::Result