diff --git a/xous-rs/src/arch/riscv/process.rs b/xous-rs/src/arch/riscv/process.rs index 7106b16a9..8f8850cb9 100644 --- a/xous-rs/src/arch/riscv/process.rs +++ b/xous-rs/src/arch/riscv/process.rs @@ -16,7 +16,7 @@ impl<'a> ProcessArgs<'a> { stub: &[u8], load_address: crate::MemoryAddress, entrypoint: crate::MemoryAddress, - ) -> ProcessArgs { + ) -> ProcessArgs<'_> { ProcessArgs { load_address, entrypoint, diff --git a/xous-rs/src/lib.rs b/xous-rs/src/lib.rs index c86a9dd51..4120fb4ec 100644 --- a/xous-rs/src/lib.rs +++ b/xous-rs/src/lib.rs @@ -5,6 +5,7 @@ pub mod arch; pub mod carton; pub mod definitions; +pub mod pagebuffer; pub mod process; pub mod services; pub mod string; @@ -13,6 +14,7 @@ pub mod syscall; pub use arch::{ProcessArgs, ProcessInit, ProcessKey, ProcessStartup, ThreadInit}; pub use definitions::*; +pub use pagebuffer::*; pub use string::*; pub use stringbuffer::*; pub use syscall::*; diff --git a/xous-rs/src/pagebuffer.rs b/xous-rs/src/pagebuffer.rs new file mode 100644 index 000000000..b303776af --- /dev/null +++ b/xous-rs/src/pagebuffer.rs @@ -0,0 +1,106 @@ +use core::fmt; + +/// A variant of StringBuffer that is stack-allocated. Used primarily for passing debug information +/// to and from the kernel. +#[repr(align(4096))] +pub struct PageBuf { + len: usize, + data: [u8; 4092], +} + +impl PageBuf { + pub fn new() -> Self { Self { len: 0, data: [0u8; 4092] } } + + /// Returns the written data as a byte slice + pub fn as_bytes(&self) -> &[u8] { &self.data[..self.len] } + + /// Returns the written data as a string slice + /// Safe because we maintain UTF-8 validity in write_str + pub fn as_str(&self) -> &str { unsafe { core::str::from_utf8_unchecked(&self.data[..self.len]) } } + + /// Clears the buffer + pub fn clear(&mut self) { self.len = 0; } + + /// Returns the number of bytes written + pub fn len(&self) -> usize { self.len } + + /// Returns remaining capacity + pub fn remaining(&self) -> usize { self.data.len() - self.len } + + /// Reconstructs a PageBuf reference from a raw pointer address. + /// + /// # Safety + /// + /// The caller must ensure: + /// - The pointer points to a valid, initialized PageBuf + /// - The memory is properly aligned (4096 bytes) + /// - The memory will remain valid for the lifetime 'a + /// - No other mutable references to this memory exist + /// + /// # Panics + /// + /// Panics if: + /// - The pointer is null + /// - The pointer is not 4096-byte aligned + /// - The len field exceeds 4092 + /// - The data[..len] contains invalid UTF-8 + pub unsafe fn from_raw_ptr_mut<'a>(ptr: usize) -> &'a mut Self { + // Check non-null + assert_ne!(ptr, 0, "PageBuf pointer must not be null"); + + // Check alignment + assert_eq!( + ptr % 4096, + 0, + "PageBuf pointer must be 4096-byte aligned, got alignment of {}", + ptr % 4096 + ); + + // Cast to reference + let page_buf = &mut *(ptr as *mut PageBuf); + + // Validate len field + assert!(page_buf.len <= 4092, "PageBuf len ({}) exceeds maximum (4092)", page_buf.len); + + // Validate UTF-8 invariant + core::str::from_utf8(&page_buf.data[..page_buf.len]).expect("PageBuf data must contain valid UTF-8"); + + page_buf + } + + /// Returns the address of this PageBuf as a usize for passing across boundaries + pub fn as_ptr(&self) -> usize { self as *const Self as usize } +} + +impl fmt::Write for PageBuf { + fn write_str(&mut self, s: &str) -> fmt::Result { + let bytes = s.as_bytes(); + let available = self.data.len() - self.len; + + if available == 0 { + // Buffer is full, silently discard + return Ok(()); + } + + let to_write = if bytes.len() <= available { + bytes.len() + } else { + // Find the last valid UTF-8 boundary within available space + // This prevents cutting in the middle of a multi-byte character + let mut boundary = available; + while boundary > 0 && !s.is_char_boundary(boundary) { + boundary -= 1; + } + boundary + }; + + self.data[self.len..self.len + to_write].copy_from_slice(&bytes[..to_write]); + self.len += to_write; + + Ok(()) + } +} + +impl Default for PageBuf { + fn default() -> Self { Self::new() } +} diff --git a/xous-rs/src/services/nameserver.rs b/xous-rs/src/services/nameserver.rs index f543b0c5f..e2d749933 100644 --- a/xous-rs/src/services/nameserver.rs +++ b/xous-rs/src/services/nameserver.rs @@ -70,11 +70,11 @@ pub fn connect(name: &str) -> Option { if result == 0 { let cid = unsafe { response_ptr.add(1).read() }; - let mut token = [0u32; 4]; - token[0] = unsafe { response_ptr.add(2).read() }; - token[1] = unsafe { response_ptr.add(3).read() }; - token[2] = unsafe { response_ptr.add(4).read() }; - token[3] = unsafe { response_ptr.add(5).read() }; + // let mut token = [0u32; 4]; + // token[0] = unsafe { response_ptr.add(2).read() }; + // token[1] = unsafe { response_ptr.add(3).read() }; + // token[2] = unsafe { response_ptr.add(4).read() }; + // token[3] = unsafe { response_ptr.add(5).read() }; // println!("Successfully connected to {}. CID: {}, token: {:?}", name, cid, token); Some(cid) } else { diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 04f61ec7d..9d38e6c9f 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -561,6 +561,17 @@ pub enum SysCall { #[cfg(feature = "raw-trng")] RawTrng(usize, usize, usize, usize, usize, usize, usize), + /// Used for platform specific kernel ABIs. Generally reserved for things like debug, + /// performance-monitoring, and power management. + /// + /// ## Arguments + /// * In general, the first `usize` is a meta-opcode that helps dispatch what the nature of the + /// remaining arguments should be. + /// + /// ## Returns + /// Returns a Scalar5, whose ABI is determined by the meta-opcode + PlatformSpecific(usize, usize, usize, usize, usize, usize, usize), + /// This syscall does not exist. It captures all possible /// arguments so detailed analysis can be performed. Invalid(usize, usize, usize, usize, usize, usize, usize), @@ -618,6 +629,7 @@ pub enum SysCallNumber { SwapOp = 44, #[cfg(feature = "raw-trng")] RawTrng = 45, + PlatformSpecific = 46, } impl SysCallNumber { @@ -673,6 +685,7 @@ impl SysCallNumber { 44 => SwapOp, #[cfg(feature = "raw-trng")] 45 => RawTrng, + 46 => PlatformSpecific, _ => Invalid, } } @@ -926,6 +939,9 @@ impl SysCall { SysCall::RawTrng(a1, a2, a3, a4, a5, a6, a7) => { [SysCallNumber::RawTrng as usize, *a1, *a2, *a3, *a4, *a5, *a6, *a7] } + SysCall::PlatformSpecific(a1, a2, a3, a4, a5, a6, a7) => { + [SysCallNumber::PlatformSpecific as usize, *a1, *a2, *a3, *a4, *a5, *a6, *a7] + } SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7) => { [SysCallNumber::Invalid as usize, *a1, *a2, *a3, *a4, *a5, *a6, *a7] } @@ -1080,6 +1096,7 @@ impl SysCall { SysCallNumber::SwapOp => SysCall::SwapOp(a1, a2, a3, a4, a5, a6, a7), #[cfg(feature = "raw-trng")] SysCallNumber::RawTrng => SysCall::RawTrng(a1, a2, a3, a4, a5, a6, a7), + SysCallNumber::PlatformSpecific => SysCall::PlatformSpecific(a1, a2, a3, a4, a5, a6, a7), SysCallNumber::Invalid => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), }) }