diff --git a/kernel/src/arch/hosted.rs b/kernel/src/arch/hosted.rs index fc61f7972..edd0f09f9 100644 --- a/kernel/src/arch/hosted.rs +++ b/kernel/src/arch/hosted.rs @@ -522,11 +522,13 @@ pub fn idle() -> bool { // Switch to this process immediately, which moves it from `Setup(_)` to `Running(0)`. // Note that in this system, multiple processes can be active at once. This is // similar to having one core for each process - SystemServices::with_mut(|ss| { - ss.create_thread(new_pid, ThreadInit {})?; - ss.switch_to_thread(new_pid, None) - }) - .unwrap(); + if new_pid != PID::new(1).unwrap() { + SystemServices::with_mut(|ss| { + ss.create_thread(new_pid, ThreadInit {})?; + ss.switch_to_thread(new_pid, None) + }) + .unwrap(); + } } ThreadMessage::SysCall(pid, thread_id, call) => { // println!("KERNEL({}): Received syscall {:?}", pid, call); diff --git a/kernel/src/arch/riscv/process.rs b/kernel/src/arch/riscv/process.rs index 74fc37088..3c79b0375 100644 --- a/kernel/src/arch/riscv/process.rs +++ b/kernel/src/arch/riscv/process.rs @@ -125,9 +125,7 @@ impl Process { let pid = unsafe { PROCESS_TABLE.current }; let hardware_pid = (riscv::register::satp::read().bits() >> 22) & ((1 << 9) - 1); assert!((pid.get() as usize) == hardware_pid); - Process { - pid, - } + Process { pid } } /// Mark this process as running on the current core @@ -299,7 +297,10 @@ impl Process { thread.sepc = unsafe { core::mem::transmute::<_, usize>(thread_init.call) }; thread.registers[1] = thread_init.stack.as_ptr() as usize + thread_init.stack.len(); - thread.registers[9] = thread_init.arg.map(|x| x.get()).unwrap_or_default(); + thread.registers[9] = thread_init.arg1; + thread.registers[10] = thread_init.arg2; + thread.registers[11] = thread_init.arg3; + thread.registers[12] = thread_init.arg4; #[cfg(any(feature = "debug-print", feature = "print-panics"))] { @@ -331,7 +332,11 @@ impl Process { Ok(()) } - pub fn setup_thread(&mut self, new_tid: TID, setup: ThreadInit) -> Result<(), xous_kernel::Error> { + pub fn setup_thread( + &mut self, + new_tid: TID, + setup: ThreadInit, + ) -> Result<(), xous_kernel::Error> { let entrypoint = unsafe { core::mem::transmute::<_, usize>(setup.call) }; // Create the new context and set it to run in the new address space. let pid = self.pid.get(); @@ -343,7 +348,7 @@ impl Process { entrypoint, setup.stack.as_ptr() as usize + setup.stack.len(), EXIT_THREAD, - &[setup.arg.map(|x| x.get() as usize).unwrap_or_default()], + &[setup.arg1, setup.arg2, setup.arg3, setup.arg4], ); Ok(()) } @@ -364,27 +369,45 @@ impl Process { ); println!( "T3:{:08x} T4:{:08x} T5:{:08x} T6:{:08x}", - _thread.registers[27], _thread.registers[28], _thread.registers[29], _thread.registers[30] + _thread.registers[27], + _thread.registers[28], + _thread.registers[29], + _thread.registers[30] ); println!( "S0:{:08x} S1:{:08x} S2:{:08x} S3:{:08x}", - _thread.registers[7], _thread.registers[8], _thread.registers[17], _thread.registers[18] + _thread.registers[7], + _thread.registers[8], + _thread.registers[17], + _thread.registers[18] ); println!( "S4:{:08x} S5:{:08x} S6:{:08x} S7:{:08x}", - _thread.registers[19], _thread.registers[20], _thread.registers[21], _thread.registers[22] + _thread.registers[19], + _thread.registers[20], + _thread.registers[21], + _thread.registers[22] ); println!( "S8:{:08x} S9:{:08x} S10:{:08x} S11:{:08x}", - _thread.registers[23], _thread.registers[24], _thread.registers[25], _thread.registers[26] + _thread.registers[23], + _thread.registers[24], + _thread.registers[25], + _thread.registers[26] ); println!( "A0:{:08x} A1:{:08x} A2:{:08x} A3:{:08x}", - _thread.registers[9], _thread.registers[10], _thread.registers[11], _thread.registers[12] + _thread.registers[9], + _thread.registers[10], + _thread.registers[11], + _thread.registers[12] ); println!( "A4:{:08x} A5:{:08x} A6:{:08x} A7:{:08x}", - _thread.registers[13], _thread.registers[14], _thread.registers[15], _thread.registers[16] + _thread.registers[13], + _thread.registers[14], + _thread.registers[15], + _thread.registers[16] ); } diff --git a/kernel/src/server-split-queues.rs b/kernel/src/server-split-queues.rs new file mode 100644 index 000000000..7b5985110 --- /dev/null +++ b/kernel/src/server-split-queues.rs @@ -0,0 +1,1051 @@ +// SPDX-FileCopyrightText: 2020 Sean Cross +// SPDX-License-Identifier: Apache-2.0 + +pub use crate::arch::process::Thread; +use core::mem; +use xous_kernel::{MemoryAddress, MemoryRange, MemorySize, Message, MessageSender, PID, SID, TID}; + +/// A pointer to resolve a server ID to a particular process +#[derive(PartialEq, Debug)] +pub struct Server { + /// A randomly-generated ID + pub sid: SID, + + /// The process that owns this server + pub pid: PID, + + /// Messages we have yet to process get stored here + incoming_queue: IncomingQueue, + + /// Messages to be returned get stored here + outgoing_queue: OutgoingQueue, + + /// The `context mask` is a bitfield of contexts that are able to handle + /// this message. If there are no available contexts, then messages will + /// need to be queued. + ready_threads: usize, +} + +pub struct IncomingQueue { + /// Messages coming from clients get stored in this array + #[cfg(baremetal)] + queue: &'static mut [IncomingQueuedMessage], + #[cfg(not(baremetal))] + queue: Vec, + + /// Where clients can write into the incoming queue + head: usize, + + /// Where the server is currently reading in the incoming queue + tail: usize, +} + +pub struct OutgoingQueue { + #[cfg(baremetal)] + queue: &'static mut [OutgoingQueuedMessage], + #[cfg(not(baremetal))] + queue: Vec, + + /// Where the server is currently writing in the outgoing queue + tail: usize, +} + +pub struct SenderID { + /// The index of the server within the SystemServices table + pub sidx: usize, + /// The index into the queue array + pub idx: usize, + /// The process ID that sent this message + pid: Option, +} + +impl SenderID { + pub fn new(sidx: usize, idx: usize, pid: Option) -> Self { + SenderID { sidx, idx, pid } + } +} + +impl From for SenderID { + fn from(item: usize) -> SenderID { + SenderID { + sidx: (item >> 16) & 0xff, + idx: item & 0xffff, + pid: PID::new((item >> 24) as u8), + } + } +} + +impl Into for SenderID { + fn into(self) -> usize { + (self.pid.map(|x| x.get() as usize).unwrap_or(0) << 24) + | ((self.sidx << 16) & 0x00ff0000) + | (self.idx & 0xffff) + } +} + +impl From for SenderID { + fn from(item: MessageSender) -> SenderID { + SenderID::from(item.to_usize()) + } +} + +impl Into for SenderID { + fn into(self) -> MessageSender { + MessageSender::from_usize(self.into()) + } +} + +#[derive(Debug)] +pub enum WaitingMessage { + /// There is no waiting message. + None, + + /// The memory was borrowed and should be returned to the given process. + BorrowedMemory(PID, TID, MemoryAddress, MemoryAddress, MemorySize), + + /// The memory was moved, and so shouldn't be returned. + MovedMemory, + + /// The message was a scalar message, so you should return the result to the process + ScalarMessage(PID, TID), + + /// This memory should be returned to the system. + ForgetMemory(MemoryRange), +} + +/// Internal representation of a queued message for a server. This should be +/// exactly 8 words / 32 bytes, yielding 128 queued messages per server +#[repr(usize)] +#[derive(PartialEq, Debug)] +enum IncomingQueuedMessage { + Empty, + BlockingScalarMessage( + u16, /* client PID */ + u16, /* client TID */ + usize, /* server return address */ + usize, /* id */ + usize, /* arg1 */ + usize, /* arg2 */ + usize, /* arg3 */ + usize, /* arg4 */ + ), + ScalarMessage( + u16, /* client PID */ + u16, /* client TID */ + usize, /* server return address */ + usize, /* id */ + usize, /* arg1 */ + usize, /* arg2 */ + usize, /* arg3 */ + usize, /* arg4 */ + ), + MemoryMessageSend( + u16, /* client PID */ + u16, /* client TID */ + usize, /* reserved */ + usize, /* id */ + usize, /* buf */ + usize, /* buf_size */ + usize, /* offset */ + usize, /* valid */ + ), + MemoryMessageROLend( + u16, /* client PID */ + u16, /* client TID */ + usize, /* address of memory base in server */ + usize, /* id */ + usize, /* buf */ + usize, /* buf_size */ + usize, /* offset */ + usize, /* valid */ + ), + MemoryMessageRWLend( + u16, /* client PID */ + u16, /* client TID */ + usize, /* address of memory base in server */ + usize, /* id */ + usize, /* buf */ + usize, /* buf_size */ + usize, /* offset */ + usize, /* valid */ + ), +} + +enum OutgoingQueuedMessage { + /// This slot is available for use + Empty, + + /// The process lending this memory terminated before + /// we could receive the message. + MemoryMessageROLendTerminated( + u16, /* client PID */ + u16, /* client TID */ + usize, /* address of memory base in server */ + usize, /* id */ + usize, /* buf */ + usize, /* buf_size */ + usize, /* offset */ + usize, /* valid */ + ), + + /// The process lending this memory terminated before + /// we could receive the message. + MemoryMessageRWLendTerminated( + u16, /* client PID */ + u16, /* client TID */ + usize, /* address of memory base in server */ + usize, /* id */ + usize, /* buf */ + usize, /* buf_size */ + usize, /* offset */ + usize, /* valid */ + ), + + /// The process waiting for the response terminated before + /// we could receive the message. + BlockingScalarTerminated( + u16, /* client PID */ + u16, /* client TID */ + usize, /* server return address */ + usize, /* id */ + usize, /* arg1 */ + usize, /* arg2 */ + usize, /* arg3 */ + usize, /* arg4 */ + ), + + /// When a message is taken that needs to be returned -- such as an ROLend + /// or RWLend -- the slot is replaced with a WaitingReturnMemory token and its + /// index is returned as the message sender. This is used to unblock the + /// sending process. + WaitingReturnMemory( + u16, /* client PID */ + u16, /* client TID */ + usize, /* address of memory base in server */ + usize, /* client base address */ + usize, /* Range size */ + ), + + /// When a server goes away, its memory must be forgotten instead of being returned + /// to the previous process. + WaitingForget( + u16, /* client PID */ + u16, /* client TID */ + usize, /* address of memory base in server */ + usize, /* client base address */ + usize, /* Range size */ + ), + + /// This is the state when a message is blocking, but has no associated memory + /// page. + WaitingReturnScalar( + u16, /* client PID */ + u16, /* client TID */ + usize, /* server return address */ + ), +} + +impl IncomingQueuedMessage { + fn is_free(&self) -> bool { + if *self == IncomingQueuedMessage::Empty { + true + } else { + false + } + } +} + +impl IncomingQueue { + pub fn new(backing: MemoryRange) -> Self { + #[cfg(baremetal)] + let queue = unsafe { + core::slice::from_raw_parts_mut( + backing.as_mut_ptr() as *mut IncomingQueuedMessage, + backing.len() / mem::size_of::() / 2, + ) + }; + #[cfg(not(baremetal))] + let queue = { + let mut queue = vec![]; + // TODO: Replace this with a direct operation on a passed-in page + queue.resize_with( + backing.size.get() / mem::size_of::(), + || IncomingQueuedMessage::Empty, + ); + queue + }; + + IncomingQueue { + queue, + head: 0, + tail: 0, + } + } + pub fn reset(&mut self) { + self.head = 0; + self.tail = 0; + } +} + +impl OutgoingQueue { + pub fn new(backing: MemoryRange) -> Self { + #[cfg(baremetal)] + let queue = unsafe { + core::slice::from_raw_parts_mut( + backing.as_mut_ptr() as *mut OutgoingQueuedMessage, + backing.len() / mem::size_of::() / 2, + ) + }; + #[cfg(not(baremetal))] + let queue = { + let mut queue = vec![]; + // TODO: Replace this with a direct operation on a passed-in page + queue.resize_with( + backing.size.get() / mem::size_of::(), + || OutgoingQueuedMessage::Empty, + ); + queue + }; + + IncomingQueue { + queue, + head: 0, + tail: 0, + } + } + + pub fn reset(&mut self) { + self.tail = 0; + } +} + +impl Server { + /// Initialize a server in the given option array. This function is + /// designed to be called with `new` pointing to an entry in a vec. + /// + /// # Errors + /// + /// * **MemoryInUse**: The provided Server option already exists + pub fn init( + new: &mut Option, + pid: PID, + sid: SID, + incoming_backing: MemoryRange, + outgoing_backing: MemoryRange, + ) -> Result<(), xous_kernel::Error> { + if new != &None { + return Err(xous_kernel::Error::MemoryInUse); + } + + let incoming_queue = IncomingQueue::new(incoming_backing); + let outgoing_queue = OutgoingQueue::new(outgoing_backing); + + *new = Some(Server { + sid, + pid, + incoming_queue, + outgoing_queue, + ready_threads: 0, + }); + Ok(()) + } + + /// Take a current slot and replace it with `None`, clearing out the contents of the queue. + pub fn destroy(current: &mut Option) -> Result<(), xous_kernel::Error> { + if let Some(mut server) = current.take() { + server.incoming_queue.reset(); + server.outgoing_queue.reset(); + server.ready_threads = 0; + } + Ok(()) + } + + // pub fn print_queue(&self) { + // println!(" Q Queue Head: {}", self.queue_head); + // println!(" Q Queue Tail: {}", self.queue_tail); + // for (_idx, _entry) in self.queue.iter().enumerate() { + // if _entry != &QueuedMessage::Empty { + // println!(" Q entry[{}]: {:?}", _idx, _entry); + // } + // } + // } + + /// When a process terminates, there may be memory that is lent to us. + /// Mark all of that memory to be discarded when it is returned, rather than + /// giving it back to the previous process space. + pub fn discard_messages_for_pid(&mut self, pid: PID) { + for entry in self.queue.iter_mut() { + match *entry { + QueuedMessage::MemoryMessageROLend( + msg_pid, + tid, + arg1, + arg2, + arg3, + arg4, + arg5, + arg6, + ) => { + if msg_pid == pid.get() as _ { + *entry = QueuedMessage::MemoryMessageROLendTerminated( + msg_pid, tid, arg1, arg2, arg3, arg4, arg5, arg6, + ); + } + } + QueuedMessage::MemoryMessageRWLend( + msg_pid, + tid, + arg1, + arg2, + arg3, + arg4, + arg5, + arg6, + ) => { + if msg_pid == pid.get() as _ { + *entry = QueuedMessage::MemoryMessageRWLendTerminated( + msg_pid, tid, arg1, arg2, arg3, arg4, arg5, arg6, + ); + } + } + QueuedMessage::BlockingScalarMessage( + msg_pid, + tid, + arg1, + arg2, + arg3, + arg4, + arg5, + arg6, + ) => { + if msg_pid == pid.get() as _ { + *entry = QueuedMessage::BlockingScalarTerminated( + msg_pid, tid, arg1, arg2, arg3, arg4, arg5, arg6, + ); + } + } + // For "Scalar" and "Move" messages, this memory has already + // been moved into this process, so memory will be reclaimed + // when the process terminates. + _ => (), + } + } + } + + /// Convert a `QueuedMesage::WaitingReturnMemory` into `QueuedMessage::Empty` + /// and return the pair. Advance the tail. Note that the `idx` could be + /// somewhere other than the tail, but as long as it points to a valid + /// message that's waiting a response, that's acceptable. + pub fn take_waiting_message( + &mut self, + idx: usize, + buf: Option<&MemoryRange>, + ) -> Result { + let current_val = self + .queue + .get_mut(idx) + .ok_or(xous_kernel::Error::BadAddress)?; + klog!("memory in queue[{}]: {:?}", idx, current_val); + let (pid, tid, server_addr, client_addr, len, forget, is_memory) = match *current_val { + QueuedMessage::WaitingReturnMemory(pid, tid, server_addr, client_addr, len) => { + (pid, tid, server_addr, client_addr, len, false, true) + } + QueuedMessage::WaitingForget(pid, tid, server_addr, client_addr, len) => { + (pid, tid, server_addr, client_addr, len, true, true) + } + QueuedMessage::WaitingReturnScalar(pid, tid, return_address) => { + (pid, tid, return_address, 0, 0, true, false) + } + _ => return Ok(WaitingMessage::None), + }; + + // Sanity check the specified address was correct, and matches what we + // had cached. + if is_memory && cfg!(baremetal) { + let buf = buf.expect("memory message expected but no buffer passed!"); + if server_addr != buf.as_ptr() as usize || len != buf.len() { + // println!("KERNEL: Memory is attached but the returned buffer doesn't match (len: {} vs {}), buf addr: {:08x} vs {:08x}", len, buf.len(), server_addr, buf.as_ptr() as usize); + Err(xous_kernel::Error::BadAddress)?; + } + } + *current_val = QueuedMessage::Empty; + if idx == self.queue_tail { + self.queue_tail += 1; + if self.queue_tail >= self.queue.len() { + self.queue_tail = 0; + } + // Advance the pointer in case we have a long string of Empty messages. + while self.queue_tail != self.queue_head + && self.queue[self.queue_tail] == QueuedMessage::Empty + { + self.queue_tail += 1; + if self.queue_tail >= self.queue.len() { + self.queue_tail = 0; + } + } + } + + // Destructure the PID and context ID from the `pid_tid` field + // klog!( + // "taking waiting message and returning to pid: {} tid: {}", + // pid, + // tid + // ); + + if !is_memory { + return Ok(WaitingMessage::ScalarMessage( + PID::new(pid as _).unwrap(), + tid as _, + )); + } + + if forget { + return Ok(WaitingMessage::ForgetMemory(MemoryRange::new( + server_addr, + len, + )?)); + } + + // If a `move` address somehow ends up here, indicate the memory has been moved. + let server_addr = match MemoryAddress::new(server_addr) { + Some(o) => o, + None => return Ok(WaitingMessage::MovedMemory), + }; + + let client_addr = MemoryAddress::new(client_addr) + .expect("client memory address was 0, but server address was not"); + let len = MemorySize::new(len).expect("memory length was 0, but address was not None"); + Ok(WaitingMessage::BorrowedMemory( + PID::new(pid as _).unwrap(), + tid as _, + server_addr, + client_addr, + len, + )) + } + + /// Remove a message from the server's queue and replace it with either a QueuedMessage::WaitingReturnMemory + /// or, for Scalar messages, QueuedMessage::Empty. + /// + /// For non-Scalar messages, you must call `take_waiting_message()` in order to return + /// memory to the calling process. + /// + /// # Returns + /// + /// * **None**: There are no waiting messages + /// ***Some(MessageEnvelope): This message is queued. + pub fn take_next_message(&mut self, sidx: usize) -> Option { + // klog!( + // "queue_head: ((({}))) queue_tail: ((({}))): {:?} CID: ((({})))", + // self.queue_head, + // self.queue_tail, + // self.queue[self.queue_tail], + // sidx + // ); + use core::convert::TryInto; + let mut queue_idx = self.queue_tail; + while queue_idx != self.queue_head { + let mut sender = SenderID::new(sidx, queue_idx, None); + // klog!("Message @ server.queue[{}]: {:?}", queue_idx, self.queue[queue_idx]); + let (result, response) = match self.queue[queue_idx] { + QueuedMessage::Empty => { + queue_idx += 1; + if queue_idx >= self.queue.len() { + queue_idx = 0; + } + continue; + } + QueuedMessage::WaitingReturnMemory(_, _, _, _, _) => { + queue_idx += 1; + if queue_idx >= self.queue.len() { + queue_idx = 0; + } + continue; + } + QueuedMessage::WaitingForget(_, _, _, _, _) => { + queue_idx += 1; + if queue_idx >= self.queue.len() { + queue_idx = 0; + } + continue; + } + QueuedMessage::WaitingReturnScalar(_, _, _) => { + queue_idx += 1; + if queue_idx >= self.queue.len() { + queue_idx = 0; + } + continue; + } + QueuedMessage::MemoryMessageROLend( + pid, + tid, + client_addr, + id, + buf, + buf_size, + offset, + valid, + ) => { + sender.pid = PID::new(pid.try_into().unwrap()); + ( + xous_kernel::MessageEnvelope { + sender: sender.into(), + body: xous_kernel::Message::Borrow(xous_kernel::MemoryMessage { + id, + buf: MemoryRange::new(buf, buf_size).ok()?, + offset: MemorySize::new(offset), + valid: MemorySize::new(valid), + }), + }, + QueuedMessage::WaitingReturnMemory(pid, tid, buf, client_addr, buf_size), + ) + } + QueuedMessage::MemoryMessageRWLend( + pid, + tid, + client_addr, + id, + buf, + buf_size, + offset, + valid, + ) => { + sender.pid = PID::new(pid.try_into().unwrap()); + ( + xous_kernel::MessageEnvelope { + sender: sender.into(), + body: xous_kernel::Message::MutableBorrow(xous_kernel::MemoryMessage { + id, + buf: MemoryRange::new(buf, buf_size).ok()?, + offset: MemorySize::new(offset), + valid: MemorySize::new(valid), + }), + }, + QueuedMessage::WaitingReturnMemory(pid, tid, buf, client_addr, buf_size), + ) + } + QueuedMessage::MemoryMessageROLendTerminated( + pid, + tid, + client_addr, + id, + buf, + buf_size, + offset, + valid, + ) => { + sender.pid = PID::new(pid.try_into().unwrap()); + ( + xous_kernel::MessageEnvelope { + sender: sender.into(), + body: xous_kernel::Message::Borrow(xous_kernel::MemoryMessage { + id, + buf: MemoryRange::new(buf, buf_size).ok()?, + offset: MemorySize::new(offset), + valid: MemorySize::new(valid), + }), + }, + QueuedMessage::WaitingReturnMemory(pid, tid, buf, client_addr, buf_size), + ) + } + QueuedMessage::MemoryMessageRWLendTerminated( + pid, + tid, + client_addr, + id, + buf, + buf_size, + offset, + valid, + ) => { + sender.pid = PID::new(pid.try_into().unwrap()); + ( + xous_kernel::MessageEnvelope { + sender: sender.into(), + body: xous_kernel::Message::MutableBorrow(xous_kernel::MemoryMessage { + id, + buf: MemoryRange::new(buf, buf_size).ok()?, + offset: MemorySize::new(offset), + valid: MemorySize::new(valid), + }), + }, + QueuedMessage::WaitingReturnMemory(pid, tid, buf, client_addr, buf_size), + ) + } + + QueuedMessage::BlockingScalarMessage( + pid, + tid, + client_addr, + id, + arg1, + arg2, + arg3, + arg4, + ) => { + sender.pid = PID::new(pid.try_into().unwrap()); + ( + xous_kernel::MessageEnvelope { + sender: sender.into(), + body: xous_kernel::Message::BlockingScalar( + xous_kernel::ScalarMessage { + id, + arg1, + arg2, + arg3, + arg4, + }, + ), + }, + QueuedMessage::WaitingReturnScalar(pid, tid, client_addr), + ) + } + QueuedMessage::MemoryMessageSend( + pid, + _tid, + _reserved, + id, + buf, + buf_size, + offset, + valid, + ) => { + sender.pid = PID::new(pid.try_into().unwrap()); + let msg = xous_kernel::MessageEnvelope { + sender: sender.into(), + body: xous_kernel::Message::Move(xous_kernel::MemoryMessage { + id, + buf: MemoryRange::new(buf, buf_size).ok()?, + offset: MemorySize::new(offset), + valid: MemorySize::new(valid), + }), + }; + self.queue[queue_idx] = QueuedMessage::Empty; + if queue_idx == self.queue_tail { + self.queue_tail += 1; + if self.queue_tail >= self.queue.len() { + self.queue_tail = 0; + } + } + return Some(msg); + } + + // Scalar messages have nothing to return, so they can go straight to the `Free` state + QueuedMessage::ScalarMessage(pid, _tid, _reserved, id, arg1, arg2, arg3, arg4) => { + sender.pid = PID::new(pid.try_into().unwrap()); + let msg = xous_kernel::MessageEnvelope { + sender: sender.into(), + body: xous_kernel::Message::Scalar(xous_kernel::ScalarMessage { + id, + arg1, + arg2, + arg3, + arg4, + }), + }; + self.queue[queue_idx] = QueuedMessage::Empty; + if queue_idx == self.queue_tail { + self.queue_tail += 1; + if self.queue_tail >= self.queue.len() { + self.queue_tail = 0; + } + } + return Some(msg); + } + QueuedMessage::BlockingScalarTerminated( + pid, + _tid, + _reserved, + id, + arg1, + arg2, + arg3, + arg4, + ) => { + sender.pid = PID::new(pid.try_into().unwrap()); + let msg = xous_kernel::MessageEnvelope { + sender: sender.into(), + body: xous_kernel::Message::Scalar(xous_kernel::ScalarMessage { + id, + arg1, + arg2, + arg3, + arg4, + }), + }; + self.queue[queue_idx] = QueuedMessage::Empty; + if queue_idx == self.queue_tail { + self.queue_tail += 1; + if self.queue_tail >= self.queue.len() { + self.queue_tail = 0; + } + } + return Some(msg); + } + }; + + self.queue[queue_idx] = response; + return Some(result); + } + None + } + + /// Add the given message to this server's queue. + /// + /// # Errors + /// + /// * **ServerQueueFull**: The server queue cannot accept any more messages + pub fn queue_message( + &mut self, + pid: PID, + context: TID, + message: xous_kernel::Message, + original_address: Option, + ) -> core::result::Result { + // klog!( + // "Queueing message: {:?} for pid: {} tid: {}", + // message, + // pid.get(), + // context + // ); + // Look for a free slot. + // if self.queue[self.queue_head] != QueuedMessage::Empty { + // return Err(xous_kernel::Error::ServerQueueFull); + // } + let mut queue_idx = self.queue_head; + loop { + if self.queue[queue_idx].is_free() || self.queue[queue_idx].is_incoming() { + break; + } + queue_idx += 1; + if queue_idx > self.queue.len() { + queue_idx = 0; + } + if queue_idx == self.queue_head { + klog!("Queue is full for PID {} -- examined every entry", pid); + Err(xous_kernel::Error::ServerQueueFull)?; + } + } + if self.queue[queue_idx].is_incoming() { + klog!("Queue is full for PID {} -- message was incoming", pid); + Err(xous_kernel::Error::ServerQueueFull)?; + } + + self.queue[queue_idx] = match message { + xous_kernel::Message::Scalar(msg) => QueuedMessage::ScalarMessage( + pid.get() as _, + context as _, + original_address.map(|x| x.get()).unwrap_or(0), + msg.id, + msg.arg1, + msg.arg2, + msg.arg3, + msg.arg4, + ), + xous_kernel::Message::BlockingScalar(msg) => QueuedMessage::BlockingScalarMessage( + pid.get() as _, + context as _, + original_address.map(|x| x.get()).unwrap_or(0), + msg.id, + msg.arg1, + msg.arg2, + msg.arg3, + msg.arg4, + ), + xous_kernel::Message::Move(msg) => QueuedMessage::MemoryMessageSend( + pid.get() as _, + context as _, + original_address.map(|x| x.get()).unwrap_or(0), + msg.id, + msg.buf.addr.get(), + msg.buf.size.get(), + msg.offset.map(|x| x.get()).unwrap_or(0) as usize, + msg.valid.map(|x| x.get()).unwrap_or(0) as usize, + ), + xous_kernel::Message::MutableBorrow(msg) => QueuedMessage::MemoryMessageRWLend( + pid.get() as _, + context as _, + original_address.map(|x| x.get()).unwrap_or(0), + msg.id, + msg.buf.addr.get(), + msg.buf.size.get(), + msg.offset.map(|x| x.get()).unwrap_or(0) as usize, + msg.valid.map(|x| x.get()).unwrap_or(0) as usize, + ), + xous_kernel::Message::Borrow(msg) => QueuedMessage::MemoryMessageROLend( + pid.get() as _, + context as _, + original_address.map(|x| x.get()).unwrap_or(0), + msg.id, + msg.buf.addr.get(), + msg.buf.size.get(), + msg.offset.map(|x| x.get()).unwrap_or(0) as usize, + msg.valid.map(|x| x.get()).unwrap_or(0) as usize, + ), + }; + + // let idx = self.queue_head; + if queue_idx == self.queue_head { + self.queue_head += 1; + if self.queue_head >= self.queue.len() { + self.queue_head = 0; + } + } + // klog!( + // "queue head: {} queue_tail: {}", + // self.queue_head, + // self.queue_tail + // ); + Ok(queue_idx) + } + + pub fn queue_response( + &mut self, + pid: PID, + context: TID, + message: &Message, + client_address: Option, + ) -> core::result::Result { + klog!( + "Queueing address message: {:?} (pid: {} tid: {})", + message, + pid.get(), + context + ); + // if self.queue[self.queue_head] != QueuedMessage::Empty { + // return Err(xous_kernel::Error::ServerQueueFull); + // } + let mut queue_idx = self.queue_head; + loop { + if self.queue[queue_idx].is_free() || self.queue[queue_idx].is_incoming() { + break; + } + queue_idx += 1; + if queue_idx > self.queue.len() { + queue_idx = 0; + } + if queue_idx == self.queue_head { + klog!("Queue is full for PID {} -- examined every entry", pid); + Err(xous_kernel::Error::ServerQueueFull)?; + } + } + if self.queue[queue_idx].is_incoming() { + klog!("Queue is full for PID {} -- message was incoming", pid); + Err(xous_kernel::Error::ServerQueueFull)?; + } + + self.queue[queue_idx] = match message { + xous_kernel::Message::Scalar(_) | xous_kernel::Message::BlockingScalar(_) => { + QueuedMessage::WaitingReturnScalar( + pid.get() as _, + context as _, + client_address.map(|x| x.get()).unwrap_or(0), + ) + } + xous_kernel::Message::Move(msg) => { + let server_address = msg.buf.addr.get(); + let len = msg.buf.size.get(); + QueuedMessage::WaitingForget( + pid.get() as _, + context as _, + server_address, + client_address.map(|x| x.get()).unwrap_or(0), + len, + ) + } + xous_kernel::Message::MutableBorrow(msg) | xous_kernel::Message::Borrow(msg) => { + let server_address = msg.buf.addr.get(); + let len = msg.buf.size.get(); + QueuedMessage::WaitingReturnMemory( + pid.get() as _, + context as _, + server_address, + client_address.map(|x| x.get()).unwrap_or(0), + len, + ) + } + }; + if queue_idx == self.queue_head { + self.queue_head += 1; + if self.queue_head >= self.queue.len() { + self.queue_head = 0; + } + } + Ok(queue_idx) + } + // assert!( + // mem::size_of::() == 32, + // "QueuedMessage was supposed to be 32 bytes, but instead was {} bytes", + // mem::size_of::() + // ); + + /// 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_thread(&mut self) -> Option { + if self.ready_threads == 0 { + return None; + } + let mut test_thread_mask = 1; + let mut thread_number = 0; + klog!( + "server pid {} ready threads: 0b{:08b}", + self.pid, + self.ready_threads + ); + if self.pid.get() == 2 && (self.ready_threads & !0b11) != 0 { + panic!("bogus thread state"); + } + loop { + // If the context mask matches this context number, remove it + // and return the index. + if self.ready_threads & test_thread_mask == test_thread_mask { + self.ready_threads &= !test_thread_mask; + if self.pid.get() == 2 && (self.ready_threads & !0b11) != 0 { + panic!("bogus thread state"); + } + return Some(thread_number); + } + // Advance to the next slot. + test_thread_mask = test_thread_mask.rotate_left(1); + thread_number += 1; + if test_thread_mask == 1 { + panic!("didn't find a free context, even though there should be one"); + } + } + } + + /// Return an available context to the blocking list. This is part of the + /// error condition when a message cannot be handled but the context has + /// already been claimed. + /// + /// # Panics + /// + /// If the context cannot be returned because it is already blocking. + pub fn return_available_thread(&mut self, tid: TID) { + if self.ready_threads & 1 << tid != 0 { + panic!( + "tried to return context {}, but it was already blocking", + tid + ); + } + self.ready_threads |= 1 << tid; + if self.pid.get() == 2 && (self.ready_threads & !0b11) != 0 { + panic!( + "bogus thread state after returning available thread {}", + tid + ); + } + } + + /// Add the given context to the list of ready and waiting contexts. + pub fn park_thread(&mut self, tid: TID) { + klog!("parking thread {}", tid); + assert!(self.ready_threads & (1 << tid) == 0); + self.ready_threads |= 1 << tid; + klog!("ready threads now: {:08b}", self.ready_threads); + if self.pid.get() == 2 && (self.ready_threads & !0b11) != 0 { + panic!("bogus thread state"); + } + } +} diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 658122717..712bad7d8 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -321,8 +321,10 @@ impl SystemServices { process.state = ProcessState::Setup(ThreadInit::new( unsafe { core::mem::transmute::(init.entrypoint) }, MemoryRange::new(init.sp, crate::arch::process::DEFAULT_STACK_SIZE).unwrap(), - MemoryAddress::new(pid), - [0u8; 12], + pid, + 0, + 0, + 0, )); } } diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 112ac856a..cc023bc04 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -185,7 +185,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes if blocking && cfg!(baremetal) { klog!("Activating Server context and switching away from Client"); - ss.activate_process_thread(thread, server_pid, server_tid, !blocking) + 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) { @@ -239,7 +239,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes let process = ss.get_process(pid).expect("Can't get current process"); let ppid = process.ppid; unsafe { SWITCHTO_CALLER = None }; - ss.activate_process_thread(thread, ppid, 0, !blocking) + ss.activate_process_thread(thread, ppid, 0, false) .map(|_| Ok(xous_kernel::Result::ResumeProcess)) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)) } else { @@ -260,6 +260,8 @@ fn return_memory( in_irq: bool, sender: MessageSender, buf: MemoryRange, + offset: Option, + valid: Option, ) -> SysCallResult { SystemServices::with_mut(|ss| { let sender = SenderID::from(sender); @@ -339,7 +341,7 @@ fn return_memory( // 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.set_thread_result(client_pid, client_tid, xous_kernel::Result::Ok)?; + ss.set_thread_result(client_pid, client_tid, xous_kernel::Result::MemoryReturned(offset, valid))?; // Return success to the server Ok(xous_kernel::Result::Ok) @@ -352,7 +354,7 @@ fn return_memory( // Switch to the client ss.ready_thread(client_pid, client_tid)?; ss.switch_to_thread(client_pid, Some(client_tid))?; - Ok(xous_kernel::Result::Ok) + Ok(xous_kernel::Result::MemoryReturned(offset, valid)) } }) } @@ -780,7 +782,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR ss.connect_to_server(sid) .map(xous_kernel::Result::ConnectionID) }), - SysCall::ReturnMemory(sender, buf) => return_memory(pid, tid, in_irq, sender, buf), + SysCall::ReturnMemory(sender, buf, offset, valid) => return_memory(pid, tid, in_irq, sender, buf, offset, valid), SysCall::ReturnScalar1(sender, arg) => return_scalar(pid, tid, in_irq, sender, arg), SysCall::ReturnScalar2(sender, arg1, arg2) => { return_scalar2(pid, tid, in_irq, sender, arg1, arg2) diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index 5e39bc8ee..da821ef8c 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -224,7 +224,8 @@ fn xmain() -> ! { if debugc{info!("GAM: chatlayout made st {:?} co {:?} pr {:?} in {:?}", chatlayout.status, chatlayout.content, chatlayout.predictive, chatlayout.input);} // make a thread to manage the status bar -- this needs to start after the IMEF is initialized // the status bar is a trusted element managed by the OS, and we are chosing to domicile this in the GAM process for now - xous::create_thread_simple(status_thread, chatlayout.status.gid()).expect("GAM: couldn't create status thread"); + let gid = chatlayout.status.gid(); + xous::create_thread_4(status_thread, gid[0] as _, gid[1] as _, gid[2] as _, gid[3] as _).expect("GAM: couldn't create status thread"); let mut powerdown_requested = false; let mut last_time: u64 = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap(); diff --git a/services/gam/src/status.rs b/services/gam/src/status.rs index d3ad58935..7cfbb2b9b 100644 --- a/services/gam/src/status.rs +++ b/services/gam/src/status.rs @@ -7,7 +7,8 @@ use core::convert::TryFrom; use blitstr_ref as blitstr; -pub fn status_thread(canvas_gid: [u32; 4]) { +pub fn status_thread(canvas_gid_0: usize, canvas_gid_1: usize, canvas_gid_2: usize, canvas_gid_3: usize) { + let canvas_gid = [canvas_gid_0 as u32, canvas_gid_1 as u32, canvas_gid_2 as u32, canvas_gid_3 as u32]; let debug1 = false; let status_gid: Gid = Gid::new(canvas_gid); if debug1{info!("GAM|status: my canvas {:?}", status_gid)}; @@ -114,4 +115,4 @@ pub fn status_thread(canvas_gid: [u32; 4]) { } } -} \ No newline at end of file +} diff --git a/services/kernel-test/src/main.rs b/services/kernel-test/src/main.rs index 92af93cba..b274b95d1 100644 --- a/services/kernel-test/src/main.rs +++ b/services/kernel-test/src/main.rs @@ -1,18 +1,14 @@ #![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_main)] -fn sleep_loop(sleep_ms: usize) { - let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap(); - let ticktimer_conn = xous::connect(ticktimer_server_id).unwrap(); +const EXTRA_KEY: usize = 42; + +fn sleep_loop_4(sleep_ms: usize, conn: usize, pid: usize, extra: usize) { let tid = xous::current_tid().unwrap(); - let pid = xous::current_pid().unwrap().get(); - let sleep_ms = match tid { - 2 => 3000, - 3 => 7000, - 4 => 12000, - _ => panic!("Unknown TID/PID"), - }; - // log::info!("My thread number is {}, sleeping 0x{:08x} ({}) ms", tid, sleep_ms, sleep_ms); + let conn = conn as _; + // let pid = xous::current_pid().unwrap().get(); + log::info!("My thread number is {}, sleeping 0x{:08x} ({}) ms and EXTRA: {}", tid, sleep_ms, sleep_ms, extra); + assert_eq!(extra, EXTRA_KEY, "extra key in arg 4 didn't match -- was {}, not {}", extra, EXTRA_KEY); let mut loop_count = 0; loop { @@ -22,9 +18,9 @@ fn sleep_loop(sleep_ms: usize) { // sleep_ms, // loop_count // ); - let start_time = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap(); - ticktimer_server::sleep_ms(ticktimer_conn, sleep_ms).unwrap(); - let end_time = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap(); + let start_time = ticktimer_server::elapsed_ms(conn).unwrap(); + ticktimer_server::sleep_ms(conn, sleep_ms).unwrap(); + let end_time = ticktimer_server::elapsed_ms(conn).unwrap(); log::info!( "TEST THREAD {}:{}: target {}ms, {} loops: Sleep finished (uptime: {}, took {} ms)", pid, @@ -38,20 +34,42 @@ fn sleep_loop(sleep_ms: usize) { } } +fn sleep_loop_3(sleep_ms: usize, conn: usize, pid: usize) { + sleep_loop_4(sleep_ms, conn, pid, EXTRA_KEY); +} + +fn sleep_loop_2(sleep_ms: usize, conn: usize) { + sleep_loop_3(sleep_ms, conn, xous::current_pid().unwrap().get() as _); +} + +fn sleep_loop_1(sleep_ms: usize) { + let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap(); + let ticktimer_conn = xous::connect(ticktimer_server_id).unwrap(); + sleep_loop_2(sleep_ms, ticktimer_conn as _); +} + +fn sleep_loop_0() { + let sleep_ms = (xous::current_pid().unwrap().get() as usize) * (xous::current_tid().unwrap() as usize); + let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap(); + let ticktimer_conn = xous::connect(ticktimer_server_id).unwrap(); + sleep_loop_2(sleep_ms, ticktimer_conn as _); +} + #[xous::xous_main] fn test_main() -> ! { - log_server::init_wait().unwrap(); - xous::create_thread_simple(sleep_loop, 10).unwrap(); - xous::create_thread_simple(sleep_loop, 5).unwrap(); - if xous::current_pid().unwrap().get() == 6 { - xous::create_thread_simple(sleep_loop, 2).unwrap(); - } + let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap(); + let ticktimer_conn = xous::connect(ticktimer_server_id).unwrap(); + + log_server::init_wait().unwrap(); + let pid = xous::current_pid().unwrap().get() as usize; + + xous::create_thread_0(sleep_loop_0).unwrap(); + xous::create_thread_1(sleep_loop_1, 4 * pid).unwrap(); + xous::create_thread_2(sleep_loop_2, 10 * pid, ticktimer_conn as _).unwrap(); + xous::create_thread_3(sleep_loop_3, 42 * pid, ticktimer_conn as _, pid).unwrap(); + xous::create_thread_4(sleep_loop_4, 180 * pid, ticktimer_conn as _, pid, EXTRA_KEY).unwrap(); - // let mut ms_count = 1; loop { xous::wait_event(); - // log::info!("Loop {}", ms_count); - // sleep_ms(ticktimer_conn, 1).unwrap(); - // ms_count += 1; } } diff --git a/services/log-server/src/main.rs b/services/log-server/src/main.rs index 8a5435169..b329dd591 100644 --- a/services/log-server/src/main.rs +++ b/services/log-server/src/main.rs @@ -224,7 +224,8 @@ fn handle_scalar( } } -fn reader_thread(mut output: implementation::OutputWriter) { +fn reader_thread(arg: usize) { + let mut output = unsafe { &mut *(arg as *mut implementation::OutputWriter) }; writeln!(output, "LOG: Xous Logging Server starting up...").unwrap(); writeln!(output, "LOG: ****************************************************************").unwrap(); @@ -331,10 +332,10 @@ fn some_main() -> ! { */ let mut output = implementation::init(); - let writer = output.get_writer(); + let mut writer = output.get_writer(); println!("LOG: my PID is {}", xous::process::id()); println!("LOG: Creating the reader thread"); - xous::create_thread_simple(reader_thread, writer).unwrap(); + xous::create_thread_1(reader_thread, &mut writer as *mut implementation::OutputWriter as usize).unwrap(); println!("LOG: Running the output"); output.run(); panic!("LOG: Exited"); diff --git a/services/shellchat/src/cmds/callback.rs b/services/shellchat/src/cmds/callback.rs index c250cc6a7..4c1791f34 100644 --- a/services/shellchat/src/cmds/callback.rs +++ b/services/shellchat/src/cmds/callback.rs @@ -3,7 +3,7 @@ use xous::{String, MessageEnvelope, Message, ScalarMessage}; use core::sync::atomic::{AtomicBool, Ordering}; static CB_RUN: AtomicBool = AtomicBool::new(false); -pub fn callback_thread(_: u32) { +pub fn callback_thread() { let ticktimer_conn = xous::connect(xous::SID::from_bytes(b"ticktimer-server").unwrap()).unwrap(); let callback_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_SHELLCHAT).unwrap(); @@ -29,7 +29,7 @@ pub struct CallBack { } impl CallBack { pub fn new() -> Self { - xous::create_thread_simple(callback_thread, 0).expect("SHCH: couldn't create callback generator thread"); + xous::create_thread_0(callback_thread).expect("SHCH: couldn't create callback generator thread"); CallBack { state: 0, callbacks: 0 @@ -59,4 +59,4 @@ impl<'a> ShellCmdApi<'a> for CallBack { Ok(Some(ret)) } -} \ No newline at end of file +} diff --git a/xous-rs/src/arch/hosted.rs b/xous-rs/src/arch/hosted.rs index aa172951c..ca1e586a8 100644 --- a/xous-rs/src/arch/hosted.rs +++ b/xous-rs/src/arch/hosted.rs @@ -297,6 +297,117 @@ fn xous_address() -> SocketAddr { .unwrap_or_else(default_xous_address) } +pub fn create_thread_0_pre( + _f: &fn() -> U, +) -> core::result::Result +where + U: Send + 'static, +{ + Ok(ThreadInit {}) +} +pub fn create_thread_1_pre( + _f: &fn(usize) -> U, + _arg1: &usize, +) -> core::result::Result +where + U: Send + 'static, +{ + Ok(ThreadInit {}) +} +pub fn create_thread_2_pre( + _f: &fn(usize, usize) -> U, + _arg1: &usize, + _arg2: &usize, +) -> core::result::Result +where + U: Send + 'static, +{ + Ok(ThreadInit {}) +} +pub fn create_thread_3_pre( + _f: &fn(usize, usize, usize) -> U, + _arg1: &usize, + _arg2: &usize, + _arg3: &usize, +) -> core::result::Result +where + U: Send + 'static, +{ + Ok(ThreadInit {}) +} +pub fn create_thread_4_pre( + _f: &fn(usize, usize, usize, usize) -> U, + _arg1: &usize, + _arg2: &usize, + _arg3: &usize, + _arg4: &usize, +) -> core::result::Result +where + U: Send + 'static, +{ + Ok(ThreadInit {}) +} + +pub fn create_thread_0_post( + f: fn() -> U, + thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + create_thread_post(move || f(), thread_id) +} + +pub fn create_thread_1_post( + f: fn(usize) -> U, + arg1: usize, + thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + create_thread_post(move || f(arg1), thread_id) +} + +pub fn create_thread_2_post( + f: fn(usize, usize) -> U, + arg1: usize, + arg2: usize, + thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + create_thread_post(move || f(arg1, arg2), thread_id) +} + +pub fn create_thread_3_post( + f: fn(usize, usize, usize) -> U, + arg1: usize, + arg2: usize, + arg3: usize, + thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + create_thread_post(move || f(arg1, arg2, arg3), thread_id) +} + +pub fn create_thread_4_post( + f: fn(usize, usize, usize, usize) -> U, + arg1: usize, + arg2: usize, + arg3: usize, + arg4: usize, + thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + create_thread_post(move || f(arg1, arg2, arg3, arg4), thread_id) +} + pub fn create_thread_simple_pre( _f: &fn(T) -> U, _arg: &T, diff --git a/xous-rs/src/arch/native.rs b/xous-rs/src/arch/native.rs index 7f0411329..bcebc89dd 100644 --- a/xous-rs/src/arch/native.rs +++ b/xous-rs/src/arch/native.rs @@ -1,4 +1,4 @@ -use crate::{MemoryAddress, MemoryRange, PID, TID}; +use crate::{MemoryRange, PID, TID}; use core::convert::TryInto; mod mem; @@ -11,24 +11,34 @@ pub struct ProcessArgs { #[derive(Debug, Clone, Copy, PartialEq)] pub struct ThreadInit { - pub call: fn(data: usize) -> usize, + /// Function pointer that accepts 0-4 arguments + pub call: usize, pub stack: MemoryRange, - pub arg: Option, - pub name: [u8; 12], + pub arg1: usize, + pub arg2: usize, + pub arg3: usize, + pub arg4: usize, + // pub name: [u8; 12], } impl ThreadInit { pub fn new( - call: fn(data: usize) -> usize, + call: usize, stack: MemoryRange, - arg: Option, - name: [u8; 12], + arg1: usize, + arg2: usize, + arg3: usize, + arg4: usize, + // name: [u8; 12], ) -> Self { ThreadInit { call, stack, - arg, - name, + arg1, + arg2, + arg3, + arg4, + // name, } } } @@ -36,10 +46,13 @@ impl ThreadInit { impl Default for ThreadInit { fn default() -> Self { ThreadInit { - call: unsafe { core::mem::transmute:: usize>(4) }, + call: 1, stack: MemoryRange::new(4, 4).unwrap(), - arg: None, - name: [0; 12], + arg1: 0, + arg2: 0, + arg3: 0, + arg4: 0, + // name: [0; 12], } } } @@ -60,16 +73,16 @@ pub struct ProcessInit { pub struct WaitHandle(core::marker::PhantomData); pub struct ProcessHandle(()); -pub fn thread_to_args(call: usize, init: &ThreadInit) -> [usize; 8] { +pub fn thread_to_args(syscall: usize, init: &ThreadInit) -> [usize; 8] { [ - call as usize, - init.call as usize, + syscall, + init.call, init.stack.as_ptr() as _, init.stack.len(), - init.arg.map(|x| x.get()).unwrap_or_default(), - 0, - 0, - 0, + init.arg1, + init.arg2, + init.arg3, + init.arg4, ] } @@ -81,18 +94,18 @@ pub fn args_to_thread( a2: usize, a3: usize, a4: usize, - _a5: usize, - _a6: usize, - _a7: usize, + a5: usize, + a6: usize, + a7: usize, ) -> core::result::Result { - let call = unsafe { core::mem::transmute(a1) }; - let stack = MemoryRange::new(a2, a3).map_err(|_| crate::Error::InvalidSyscall)?; - let arg = MemoryAddress::new(a4); Ok(ThreadInit { - call, - stack, - arg, - name: [0; 12], + call: a1, + stack: MemoryRange::new(a2, a3).map_err(|_| crate::Error::InvalidSyscall)?, + arg1: a4, + arg2: a5, + arg3: a6, + arg4: a7, + // name: [0; 12], }) } @@ -103,7 +116,6 @@ where T: Send + 'static, { todo!() - // Ok(ThreadInit {}) } pub fn create_thread_post( @@ -116,29 +128,10 @@ where T: Send + 'static, { todo!() - // let server_address = xous_address(); - // let server_connection = - // XOUS_SERVER_CONNECTION.with(|xsc| xsc.borrow().as_ref().unwrap().clone()); - // let process_id = PROCESS_ID.with(|pid| *pid.borrow()); - // Ok(std::thread::Builder::new() - // .spawn(move || { - // set_xous_address(server_address); - // THREAD_ID.with(|tid| *tid.borrow_mut() = thread_id); - // PROCESS_ID.with(|pid| *pid.borrow_mut() = process_id); - // XOUS_SERVER_CONNECTION.with(|xsc| *xsc.borrow_mut() = Some(server_connection)); - // f() - // }) - // .map(WaitHandle) - // .map_err(|_| crate::Error::InternalError)?) } pub fn wait_thread(_joiner: WaitHandle) -> crate::SysCallResult { todo!() - // joiner - // .0 - // .join() - // .map(|_| Result::Ok) - // .map_err(|_| crate::Error::InternalError) } pub fn process_to_args(call: usize, init: &ProcessInit) -> [usize; 8] { @@ -166,6 +159,129 @@ pub fn args_to_process( todo!() } +pub fn create_thread_0_pre(f: &fn() -> U) -> core::result::Result +where + U: Send + 'static, +{ + let start = unsafe { core::mem::transmute(*f) }; + create_thread_n_pre(start, &0, &0, &0, &0) +} + +pub fn create_thread_0_post( + f: fn() -> U, + thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + let start = unsafe { core::mem::transmute(f) }; + create_thread_n_post(start, 0, 0, 0, 0, thread_id) +} + +pub fn create_thread_1_pre( + f: &fn(usize) -> U, + arg1: &usize, +) -> core::result::Result +where + U: Send + 'static, +{ + let start = unsafe { core::mem::transmute(*f) }; + create_thread_n_pre(start, arg1, &0, &0, &0) +} + +pub fn create_thread_1_post( + f: fn(usize) -> U, + arg1: usize, + thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + let start = unsafe { core::mem::transmute(f) }; + create_thread_n_post(start, arg1, 0, 0, 0, thread_id) +} + +pub fn create_thread_2_pre( + f: &fn(usize, usize) -> U, + arg1: &usize, + arg2: &usize, +) -> core::result::Result +where + U: Send + 'static, +{ + let start = unsafe { core::mem::transmute(*f) }; + create_thread_n_pre(start, arg1, arg2, &0, &0) +} + +pub fn create_thread_2_post( + f: fn(usize, usize) -> U, + arg1: usize, + arg2: usize, + thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + let start = unsafe { core::mem::transmute(f) }; + create_thread_n_post(start, arg1, arg2, 0, 0, thread_id) +} + +pub fn create_thread_3_pre( + f: &fn(usize, usize, usize) -> U, + arg1: &usize, + arg2: &usize, + arg3: &usize, +) -> core::result::Result +where + U: Send + 'static, +{ + let start = unsafe { core::mem::transmute(*f) }; + create_thread_n_pre(start, arg1, arg2, arg3, &0) +} + +pub fn create_thread_3_post( + f: fn(usize, usize, usize) -> U, + arg1: usize, + arg2: usize, + arg3: usize, + thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + let start = unsafe { core::mem::transmute(f) }; + create_thread_n_post(start, arg1, arg2, arg3, 0, thread_id) +} + +pub fn create_thread_4_pre( + f: &fn(usize, usize, usize, usize) -> U, + arg1: &usize, + arg2: &usize, + arg3: &usize, + arg4: &usize, +) -> core::result::Result +where + U: Send + 'static, +{ + let start = unsafe { core::mem::transmute(*f) }; + create_thread_n_pre(start, arg1, arg2, arg3, arg4) +} + +pub fn create_thread_4_post( + f: fn(usize, usize, usize, usize) -> U, + arg1: usize, + arg2: usize, + arg3: usize, + arg4: usize, + thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + let start = unsafe { core::mem::transmute(f) }; + create_thread_n_post(start, arg1, arg2, arg3, arg4, thread_id) +} + pub fn create_thread_simple_pre( f: &fn(T) -> U, arg: &T, @@ -174,27 +290,37 @@ where T: Send + 'static, U: Send + 'static, { - #[cfg(feature = "bit-flags")] - let flags = crate::MemoryFlags::R | crate::MemoryFlags::W | crate::MemoryFlags::RESERVE; - #[cfg(not(feature = "bit-flags"))] - let flags = 0b0000_0010 | 0b0000_0100 | 0b0000_0001; - - let stack = crate::map_memory(None, None, 131_072, flags)?; - let start = unsafe { core::mem::transmute(*f) }; - let arg = unsafe { core::mem::transmute(arg) }; - Ok(ThreadInit::new(start, stack, Some(arg), [0; 12])) + create_thread_n_pre( + unsafe { core::mem::transmute(*f) }, + unsafe { core::mem::transmute(arg) }, + &0, + &0, + &0, + ) } pub fn create_thread_simple_post( - _f: fn(T) -> U, - _arg: T, - _thread_id: TID, + f: fn(T) -> U, + arg: T, + thread_id: TID, ) -> core::result::Result, crate::Error> where T: Send + 'static, U: Send + 'static, { - Ok(WaitHandle(core::marker::PhantomData)) + create_thread_n_post( + unsafe { core::mem::transmute(f) }, + unsafe { core::mem::transmute(&arg) }, + 0, + 0, + 0, + thread_id, + ) + // If we succeeded, the variable will be moved into the caller. Drop it from here. + .and_then(|f| { + core::mem::forget(arg); + Ok(f) + }) } /// If no connection exists, create a new connection to the server. This means @@ -211,6 +337,36 @@ pub fn create_process_post( todo!() } +pub fn create_thread_n_pre( + start: usize, + arg1: &usize, + arg2: &usize, + arg3: &usize, + arg4: &usize, +) -> core::result::Result { + #[cfg(feature = "bit-flags")] + let flags = crate::MemoryFlags::R | crate::MemoryFlags::W | crate::MemoryFlags::RESERVE; + #[cfg(not(feature = "bit-flags"))] + let flags = 0b0000_0010 | 0b0000_0100 | 0b0000_0001; + + let stack = crate::map_memory(None, None, 131_072, flags)?; + Ok(ThreadInit::new(start, stack, *arg1, *arg2, *arg3, *arg4)) +} + +pub fn create_thread_n_post( + _f: usize, + _arg1: usize, + _arg2: usize, + _arg3: usize, + _arg4: usize, + _thread_id: TID, +) -> core::result::Result, crate::Error> +where + U: Send + 'static, +{ + Ok(WaitHandle(core::marker::PhantomData)) +} + pub fn wait_process(_joiner: ProcessHandle) -> crate::SysCallResult { loop { crate::wait_event(); diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 1c14e45dc..4dd422818 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -1,4 +1,4 @@ -use core::convert::TryInto; +use core::convert::{TryFrom, TryInto}; use core::num::{NonZeroU8, NonZeroUsize}; pub type MemoryAddress = NonZeroUsize; @@ -393,7 +393,7 @@ impl Message { } } - pub fn message_type(&self) -> usize { + pub(crate) fn message_type(&self) -> usize { match *self { Message::MutableBorrow(_) => 1, Message::Borrow(_) => 2, @@ -402,6 +402,57 @@ impl Message { Message::BlockingScalar(_) => 5, } } + + /// Return the ID of this message + pub fn id(&self) -> usize { + match &self { + &Message::MutableBorrow(mem) | &Message::Borrow(mem) | &Message::Move(mem) => mem.id, + &Message::Scalar(s) | &Message::BlockingScalar(s) => s.id, + } + } +} + +impl TryFrom<(usize, usize, usize, usize, usize, usize)> for Message { + type Error = (); + fn try_from( + value: (usize, usize, usize, usize, usize, usize), + ) -> core::result::Result { + match value.0 { + 1 => Ok(Message::MutableBorrow(MemoryMessage { + id: value.1, + buf: MemoryRange::new(value.2, value.3).map_err(|_| ())?, + offset: MemoryAddress::new(value.4), + valid: MemorySize::new(value.5), + })), + 2 => Ok(Message::Borrow(MemoryMessage { + id: value.1, + buf: MemoryRange::new(value.2, value.3).map_err(|_| ())?, + offset: MemoryAddress::new(value.4), + valid: MemorySize::new(value.5), + })), + 3 => Ok(Message::Move(MemoryMessage { + id: value.1, + buf: MemoryRange::new(value.2, value.3).map_err(|_| ())?, + offset: MemoryAddress::new(value.4), + valid: MemorySize::new(value.5), + })), + 4 => Ok(Message::Scalar(ScalarMessage { + id: value.1, + arg1: value.2, + arg2: value.3, + arg3: value.4, + arg4: value.5, + })), + 5 => Ok(Message::BlockingScalar(ScalarMessage { + id: value.1, + arg1: value.2, + arg2: value.3, + arg3: value.4, + arg4: value.5, + })), + _ => Err(()), + } + } } #[repr(C)] @@ -567,6 +618,12 @@ pub enum Result { /// The message was successful but no value was returned. None, + /// Memory was returned, and more information is available. + MemoryReturned( + Option, /* offset */ + Option, /* valid */ + ), + UnknownResult(usize, usize, usize, usize, usize, usize, usize), } @@ -600,10 +657,29 @@ impl Result { Result::Scalar2(a, b) => [14, *a, *b, 0, 0, 0, 0, 0], Result::NewServerID(sid, cid) => { let s = sid.to_u32(); - [15, s.0 as _, s.1 as _, s.2 as _, s.3 as _, *cid as usize, 0, 0] + [ + 15, + s.0 as _, + s.1 as _, + s.2 as _, + s.3 as _, + *cid as usize, + 0, + 0, + ] } Result::WouldBlock => [16, 0, 0, 0, 0, 0, 0, 0], Result::None => [17, 0, 0, 0, 0, 0, 0, 0], + Result::MemoryReturned(offset, valid) => [ + 18, + offset.map(|o| o.get()).unwrap_or_default(), + valid.map(|v| v.get()).unwrap_or_default(), + 0, + 0, + 0, + 0, + 0, + ], Result::UnknownResult(arg1, arg2, arg3, arg4, arg5, arg6, arg7) => { [usize::MAX, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6, *arg7] } @@ -679,6 +755,7 @@ impl Result { ), 16 => Result::WouldBlock, 17 => Result::None, + 18 => Result::MemoryReturned(MemorySize::new(src[1]), MemorySize::new(src[2])), _ => Result::UnknownResult(src[0], src[1], src[2], src[3], src[4], src[5], src[6]), } } diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index fd55cccfc..1d0a5592d 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -3,7 +3,7 @@ use crate::{ MemorySize, MemoryType, Message, MessageEnvelope, MessageSender, ProcessArgs, ProcessInit, Result, ScalarMessage, SysCallResult, ThreadInit, CID, PID, SID, TID, }; -use core::convert::TryInto; +use core::convert::{TryFrom, TryInto}; // use num_derive::FromPrimitive; // use num_traits::FromPrimitive; @@ -253,7 +253,12 @@ pub enum SysCall { TrySendMessage(CID, Message), /// Return a Borrowed memory region to the sender - ReturnMemory(MessageSender, MemoryRange), + ReturnMemory( + MessageSender, /* source of this message */ + MemoryRange, /* address of range */ + Option, /* offset */ + Option, /* valid */ + ), /// Return a scalar to the sender ReturnScalar1(MessageSender, usize), @@ -597,13 +602,13 @@ impl SysCall { sc.arg4, ], }, - SysCall::ReturnMemory(sender, buf) => [ + SysCall::ReturnMemory(sender, buf, offset, valid) => [ SysCallNumber::ReturnMemory as usize, sender.to_usize(), buf.as_ptr() as usize, buf.len(), - 0, - 0, + offset.map(|o| o.get()).unwrap_or_default(), + valid.map(|v| v.get()).unwrap_or_default(), 0, 0, ], @@ -755,59 +760,15 @@ impl SysCall { SysCallNumber::Connect => { SysCall::Connect(SID::from_u32(a1 as _, a2 as _, a3 as _, a4 as _)) } - SysCallNumber::SendMessage => match a2 { - 1 => SysCall::SendMessage( - a1.try_into().unwrap(), - Message::MutableBorrow(MemoryMessage { - id: a3, - buf: MemoryRange::new(a4, a5)?, - offset: MemoryAddress::new(a6), - valid: MemorySize::new(a7), - }), - ), - 2 => SysCall::SendMessage( - a1.try_into().unwrap(), - Message::Borrow(MemoryMessage { - id: a3, - buf: MemoryRange::new(a4, a5)?, - offset: MemoryAddress::new(a6), - valid: MemorySize::new(a7), - }), - ), - 3 => SysCall::SendMessage( - a1.try_into().unwrap(), - Message::Move(MemoryMessage { - id: a3, - buf: MemoryRange::new(a4, a5)?, - offset: MemoryAddress::new(a6), - valid: MemorySize::new(a7), - }), - ), - 4 => SysCall::SendMessage( - a1.try_into().unwrap(), - Message::Scalar(ScalarMessage { - id: a3, - arg1: a4, - arg2: a5, - arg3: a6, - arg4: a7, - }), - ), - 5 => SysCall::SendMessage( - a1.try_into().unwrap(), - Message::BlockingScalar(ScalarMessage { - id: a3, - arg1: a4, - arg2: a5, - arg3: a6, - arg4: a7, - }), - ), - _ => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), - }, - SysCallNumber::ReturnMemory => { - SysCall::ReturnMemory(MessageSender::from_usize(a1), MemoryRange::new(a2, a3)?) - } + SysCallNumber::SendMessage => Message::try_from((a2, a3, a4, a5, a6, a7)) + .map(|m| SysCall::SendMessage(a1.try_into().unwrap(), m)) + .unwrap_or_else(|_| SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7)), + SysCallNumber::ReturnMemory => SysCall::ReturnMemory( + MessageSender::from_usize(a1), + MemoryRange::new(a2, a3)?, + MemorySize::new(a4), + MemorySize::new(a5), + ), SysCallNumber::CreateThread => { SysCall::CreateThread(crate::arch::args_to_thread(a1, a2, a3, a4, a5, a6, a7)?) } @@ -895,7 +856,7 @@ impl SysCall { Message::Move(_) | Message::Borrow(_) | Message::MutableBorrow(_) ) } - SysCall::ReturnMemory(_, _) => true, + SysCall::ReturnMemory(_, _, _, _) => true, _ => false, } } @@ -932,7 +893,7 @@ impl SysCall { /// Returns `true` if the associated syscall is returning memory pub fn is_return_memory(&self) -> bool { - matches!(self, SysCall::ReturnMemory(_, _)) + matches!(self, SysCall::ReturnMemory(_, _, _, _)) } /// If the syscall has memory attached to it, return the memory @@ -944,7 +905,7 @@ impl SysCall { | Message::MutableBorrow(memory_message) => Some(memory_message.buf), _ => None, }, - SysCall::ReturnMemory(_, range) => Some(*range), + SysCall::ReturnMemory(_, range, _, _) => Some(*range), _ => None, } } @@ -959,7 +920,7 @@ impl SysCall { | SysCall::ReturnToParent(_, _) | SysCall::ReturnScalar2(_, _, _) | SysCall::ReturnScalar1(_, _) - | SysCall::ReturnMemory(_, _) + | SysCall::ReturnMemory(_, _, _, _) ) } } @@ -1033,7 +994,24 @@ pub fn unmap_memory(range: MemoryRange) -> core::result::Result<(), Error> { /// Map the given physical address to the given virtual address. /// The `size` field must be page-aligned. pub fn return_memory(sender: MessageSender, mem: MemoryRange) -> core::result::Result<(), Error> { - let result = rsyscall(SysCall::ReturnMemory(sender, mem))?; + let result = rsyscall(SysCall::ReturnMemory(sender, mem, None, None))?; + if let crate::Result::Ok = result { + Ok(()) + } else if let Result::Error(e) = result { + Err(e) + } else { + Err(Error::InternalError) + } +} + +/// Map the given physical address to the given virtual address. +/// The `size` field must be page-aligned. +pub fn return_memory_offset( + sender: MessageSender, + mem: MemoryRange, + offset: Option, +) -> core::result::Result<(), Error> { + let result = rsyscall(SysCall::ReturnMemory(sender, mem, offset, None))?; if let crate::Result::Ok = result { Ok(()) } else if let Result::Error(e) = result { @@ -1253,6 +1231,7 @@ pub fn try_send_message(connection: CID, message: Message) -> core::result::Resu Ok(Result::Ok) => Ok(Result::Ok), Ok(Result::Scalar1(a)) => Ok(Result::Scalar1(a)), Ok(Result::Scalar2(a, b)) => Ok(Result::Scalar2(a, b)), + Ok(Result::MemoryReturned(offset, valid)) => Ok(Result::MemoryReturned(offset, valid)), Err(e) => Err(e), v => panic!("Unexpected return value: {:?}", v), } @@ -1294,6 +1273,7 @@ pub fn send_message(connection: CID, message: Message) -> core::result::Result Ok(Result::Ok), Ok(Result::Scalar1(a)) => Ok(Result::Scalar1(a)), Ok(Result::Scalar2(a, b)) => Ok(Result::Scalar2(a, b)), + Ok(Result::MemoryReturned(offset, valid)) => Ok(Result::MemoryReturned(offset, valid)), Err(e) => Err(e), v => panic!("Unexpected return value: {:?}", v), } @@ -1314,6 +1294,10 @@ pub fn wait_event() { rsyscall(SysCall::WaitEvent).expect("wait_event returned an error"); } +#[deprecated( + since = "0.2", + note = "Please use create_thread_n() or create_thread()" +)] pub fn create_thread_simple( f: fn(T) -> U, arg: T, @@ -1332,6 +1316,94 @@ where }) } +pub fn create_thread_0(f: fn() -> T) -> core::result::Result, Error> +where + T: Send + 'static, +{ + let thread_info = crate::arch::create_thread_0_pre(&f)?; + rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| { + if let Result::ThreadID(thread_id) = result { + crate::arch::create_thread_0_post(f, thread_id) + } else { + Err(Error::InternalError) + } + }) +} + +pub fn create_thread_1( + f: fn(usize) -> T, + arg1: usize, +) -> core::result::Result, Error> +where + T: Send + 'static, +{ + let thread_info = crate::arch::create_thread_1_pre(&f, &arg1)?; + rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| { + if let Result::ThreadID(thread_id) = result { + crate::arch::create_thread_1_post(f, arg1, thread_id) + } else { + Err(Error::InternalError) + } + }) +} + +pub fn create_thread_2( + f: fn(usize, usize) -> T, + arg1: usize, + arg2: usize, +) -> core::result::Result, Error> +where + T: Send + 'static, +{ + let thread_info = crate::arch::create_thread_2_pre(&f, &arg1, &arg2)?; + rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| { + if let Result::ThreadID(thread_id) = result { + crate::arch::create_thread_2_post(f, arg1, arg2, thread_id) + } else { + Err(Error::InternalError) + } + }) +} + +pub fn create_thread_3( + f: fn(usize, usize, usize) -> T, + arg1: usize, + arg2: usize, + arg3: usize, +) -> core::result::Result, Error> +where + T: Send + 'static, +{ + let thread_info = crate::arch::create_thread_3_pre(&f, &arg1, &arg2, &arg3)?; + rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| { + if let Result::ThreadID(thread_id) = result { + crate::arch::create_thread_3_post(f, arg1, arg2, arg3, thread_id) + } else { + Err(Error::InternalError) + } + }) +} + +pub fn create_thread_4( + f: fn(usize, usize, usize, usize) -> T, + arg1: usize, + arg2: usize, + arg3: usize, + arg4: usize, +) -> core::result::Result, Error> +where + T: Send + 'static, +{ + let thread_info = crate::arch::create_thread_4_pre(&f, &arg1, &arg2, &arg3, &arg4)?; + rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| { + if let Result::ThreadID(thread_id) = result { + crate::arch::create_thread_4_post(f, arg1, arg2, arg3, arg4, thread_id) + } else { + Err(Error::InternalError) + } + }) +} + /// Create a new thread with the given closure. pub fn create_thread(f: F) -> core::result::Result, Error> where