diff --git a/examples/shell/src/main.rs b/examples/shell/src/main.rs index 59d0a1dbf..24058b9b8 100644 --- a/examples/shell/src/main.rs +++ b/examples/shell/src/main.rs @@ -30,13 +30,7 @@ fn main() { let mut connection = None; - use std::convert::TryInto; - let mut sid = (0, 0, 0, 0); - let mut byte_iter = b"xous-logs-output".chunks_exact(4); - sid.0 = u32::from_le_bytes(byte_iter.next().unwrap().try_into().unwrap()); - sid.1 = u32::from_le_bytes(byte_iter.next().unwrap().try_into().unwrap()); - sid.2 = u32::from_le_bytes(byte_iter.next().unwrap().try_into().unwrap()); - sid.3 = u32::from_le_bytes(byte_iter.next().unwrap().try_into().unwrap()); + let sid = xous::SID::from_bytes(b"xous-logs-output").unwrap(); println!("Attempting to connect to server..."); while connection.is_none() { diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index 1e7488249..29ab6f5ca 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -122,7 +122,7 @@ impl MemoryManager { let mut args_iter = args.iter(); let xarg_def = args_iter.next().expect("mm: no kernel arguments found"); assert!( - self.extra.len() == 0, + self.extra.is_empty(), "mm: self.extra.len() was {}, not 0", self.extra.len() ); diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 5a66d1c4b..eaba7bbee 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -526,7 +526,7 @@ impl SystemServices { // ensure it's the INITIAL_TID. Otherwise it's not valid. if let Some(ctx) = tid { if ctx != INITIAL_TID { - return Err(xous::Error::InvalidContext); + return Err(xous::Error::InvalidThread); } } @@ -557,7 +557,7 @@ impl SystemServices { Some(ctx) => { // Ensure the specified context is ready to run if x & (1 << ctx) == 0 { - return Err(xous::Error::InvalidContext); + return Err(xous::Error::InvalidThread); } ctx } @@ -601,7 +601,7 @@ impl SystemServices { if ready_threads & (1 << ctx) == 0 /*&& ctx != current_thread*/ { - return Err(xous::Error::InvalidContext); + return Err(xous::Error::InvalidThread); } ctx } diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index f9a1c92bd..403144db8 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -1,3 +1,4 @@ +use core::convert::TryInto; use core::num::{NonZeroU8, NonZeroUsize}; pub type MemoryAddress = NonZeroUsize; @@ -10,7 +11,45 @@ pub type MessageSender = usize; pub type Connection = usize; /// Server ID -pub type SID = (u32, u32, u32, u32); +#[derive(Debug, Copy, Clone, PartialEq)] +pub struct SID((u32, u32, u32, u32)); +impl SID { + pub fn from_bytes(b: &[u8]) -> Option { + if b.len() > 16 { + None + } else { + let mut sid = (0, 0, 0, 0); + let mut byte_iter = b.chunks_exact(4); + if let Some(val) = byte_iter.next() { + sid.0 = u32::from_le_bytes(val.try_into().ok()?); + } + if let Some(val) = byte_iter.next() { + sid.1 = u32::from_le_bytes(val.try_into().ok()?); + } + if let Some(val) = byte_iter.next() { + sid.2 = u32::from_le_bytes(val.try_into().ok()?); + } + if let Some(val) = byte_iter.next() { + sid.3 = u32::from_le_bytes(val.try_into().ok()?); + } + Some(SID(sid)) + } + } + pub fn from_u32(a0: u32, a1: u32, a2: u32, a3: u32) -> SID { + SID((a0, a1, a2, a3)) + } + pub fn to_u32(&self) -> (u32, u32, u32, u32) { + ((self.0).0, (self.0).1, (self.0).2, (self.0).3) + } +} + +impl std::str::FromStr for SID { + type Err = (); + + fn from_str(s: &str) -> core::result::Result { + Self::from_bytes(s.as_bytes()).ok_or(()) + } +} /// Connection ID pub type CID = usize; @@ -81,7 +120,7 @@ pub enum Error { UnhandledSyscall = 17, InvalidSyscall = 18, ShareViolation = 19, - InvalidContext = 20, + InvalidThread = 20, InvalidPID = 21, UnknownError = 22, } @@ -110,7 +149,7 @@ impl Error { 17 => UnhandledSyscall, 18 => InvalidSyscall, 19 => ShareViolation, - 20 => InvalidContext, + 20 => InvalidThread, 21 => InvalidPID, _ => UnknownError, } @@ -138,7 +177,7 @@ impl Error { UnhandledSyscall => 17, InvalidSyscall => 18, ShareViolation => 19, - InvalidContext => 20, + InvalidThread => 20, InvalidPID => 21, UnknownError => usize::MAX, } @@ -397,7 +436,10 @@ impl Result { [4, *count, *pid0, *ctx0, *pid1, *ctx1, *pid2, *ctx2] } Result::ResumeProcess => [5, 0, 0, 0, 0, 0, 0, 0], - Result::ServerID(sid) => [6, sid.0 as _, sid.1 as _, sid.2 as _, sid.3 as _, 0, 0, 0], + Result::ServerID(sid) => { + let s = sid.to_u32(); + [6, s.0 as _, s.1 as _, s.2 as _, s.3 as _, 0, 0, 0] + } Result::ConnectionID(cid) => [7, *cid, 0, 0, 0, 0, 0, 0], Result::Message(me) => { let me_enc = me.to_usize(); @@ -437,7 +479,12 @@ impl Result { } 4 => Result::ReadyThreads(src[1], src[2], src[3], src[4], src[5], src[6], src[7]), 5 => Result::ResumeProcess, - 6 => Result::ServerID((src[1] as _, src[2] as _, src[3] as _, src[4] as _)), + 6 => Result::ServerID(SID::from_u32( + src[1] as _, + src[2] as _, + src[3] as _, + src[4] as _, + )), 7 => Result::ConnectionID(src[1] as CID), 8 => { let sender = src[1]; diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index e3570b9ee..03359ee97 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -315,16 +315,19 @@ impl SysCall { ], SysCall::Yield => [SysCallNumber::Yield as usize, 0, 0, 0, 0, 0, 0, 0], SysCall::WaitEvent => [SysCallNumber::WaitEvent as usize, 0, 0, 0, 0, 0, 0, 0], - SysCall::ReceiveMessage(sid) => [ - SysCallNumber::ReceiveMessage as usize, - sid.0 as _, - sid.1 as _, - sid.2 as _, - sid.3 as _, - 0, - 0, - 0, - ], + SysCall::ReceiveMessage(sid) => { + let s = sid.to_u32(); + [ + SysCallNumber::ReceiveMessage as usize, + s.0 as _, + s.1 as _, + s.2 as _, + s.3 as _, + 0, + 0, + 0, + ] + } SysCall::ReturnToParentI(a1, a2) => [ SysCallNumber::ReturnToParentI as usize, a1.get() as usize, @@ -409,26 +412,32 @@ impl SysCall { 0, ], - SysCall::CreateServer(a1) => [ - SysCallNumber::CreateServer as usize, - a1.0 as _, - a1.1 as _, - a1.2 as _, - a1.3 as _, - 0, - 0, - 0, - ], - SysCall::Connect(sid) => [ - SysCallNumber::Connect as usize, - sid.0 as _, - sid.1 as _, - sid.2 as _, - sid.3 as _, - 0, - 0, - 0, - ], + SysCall::CreateServer(sid) => { + let s = sid.to_u32(); + [ + SysCallNumber::CreateServer as usize, + s.0 as _, + s.1 as _, + s.2 as _, + s.3 as _, + 0, + 0, + 0, + ] + } + SysCall::Connect(sid) => { + let s = sid.to_u32(); + [ + SysCallNumber::Connect as usize, + s.0 as _, + s.1 as _, + s.2 as _, + s.3 as _, + 0, + 0, + 0, + ] + } SysCall::SendMessage(a1, ref a2) => match a2 { Message::MutableBorrow(mm) => [ SysCallNumber::SendMessage as usize, @@ -536,7 +545,7 @@ impl SysCall { Some(SysCallNumber::Yield) => SysCall::Yield, Some(SysCallNumber::WaitEvent) => SysCall::WaitEvent, Some(SysCallNumber::ReceiveMessage) => { - SysCall::ReceiveMessage((a1 as _, a2 as _, a3 as _, a4 as _)) + SysCall::ReceiveMessage(SID::from_u32(a1 as _, a2 as _, a3 as _, a4 as _)) } Some(SysCallNumber::ReturnToParentI) => { SysCall::ReturnToParentI(pid_from_usize(a1)?, a2) @@ -566,9 +575,9 @@ impl SysCall { a4, ), Some(SysCallNumber::CreateServer) => { - SysCall::CreateServer((a1 as _, a2 as _, a3 as _, a4 as _)) + SysCall::CreateServer(SID::from_u32(a1 as _, a2 as _, a3 as _, a4 as _)) } - Some(SysCallNumber::Connect) => SysCall::Connect((a1 as _, a2 as _, a3 as _, a4 as _)), + Some(SysCallNumber::Connect) => SysCall::Connect(SID::from_u32(a1 as _, a2 as _, a3 as _, a4 as _)), Some(SysCallNumber::SendMessage) => match a2 { 1 => SysCall::SendMessage( a1, @@ -731,14 +740,7 @@ pub fn claim_interrupt( /// * **ServerExists**: A server has already registered with that name /// * **InvalidString**: The name was not a valid UTF-8 string pub fn create_server(name_bytes: &[u8; 16]) -> core::result::Result { - use std::convert::TryInto; - let mut sid = (0, 0, 0, 0); - let mut byte_iter = name_bytes.chunks_exact(4); - sid.0 = u32::from_le_bytes(byte_iter.next().unwrap().try_into().unwrap()); - sid.1 = u32::from_le_bytes(byte_iter.next().unwrap().try_into().unwrap()); - sid.2 = u32::from_le_bytes(byte_iter.next().unwrap().try_into().unwrap()); - sid.3 = u32::from_le_bytes(byte_iter.next().unwrap().try_into().unwrap()); - assert!(byte_iter.next().is_none()); + let sid = SID::from_bytes(name_bytes).ok_or(Error::InvalidString)?; let result = rsyscall(SysCall::CreateServer(sid))?; if let Result::ServerID(sid) = result {