diff --git a/kernel/src/arch/hosted.rs b/kernel/src/arch/hosted.rs index f12efd24d..56f20b36f 100644 --- a/kernel/src/arch/hosted.rs +++ b/kernel/src/arch/hosted.rs @@ -1,6 +1,7 @@ pub mod irq; pub mod mem; pub mod process; +pub mod rand; pub mod syscall; use std::cell::RefCell; @@ -548,14 +549,15 @@ pub fn idle() -> bool { "Unable to send response to process: {:?} -- terminating", _e ); - crate::syscall::handle(pid, thread_id, false, SysCall::TerminateProcess).ok(); + crate::syscall::handle(pid, thread_id, false, SysCall::TerminateProcess) + .ok(); }); // println!("KERNEL: Done sending"); } // Handle the syscall within the Xous kernel - let response = - crate::syscall::handle(pid, thread_id, false, call).unwrap_or_else(Result::Error); + let response = crate::syscall::handle(pid, thread_id, false, 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. @@ -584,7 +586,8 @@ pub fn idle() -> bool { "KERNEL({}): Unable to send response to process: {:?} -- terminating", pid, _e ); - crate::syscall::handle(pid, thread_id, false, SysCall::TerminateProcess).ok(); + crate::syscall::handle(pid, thread_id, false, SysCall::TerminateProcess) + .ok(); }); crate::arch::process::set_current_pid(existing_pid); } diff --git a/kernel/src/arch/hosted/rand.rs b/kernel/src/arch/hosted/rand.rs new file mode 100644 index 000000000..b5cdd3c46 --- /dev/null +++ b/kernel/src/arch/hosted/rand.rs @@ -0,0 +1,3 @@ +pub fn get_u32() -> u32 { + ::rand::random() +} diff --git a/kernel/src/arch/riscv.rs b/kernel/src/arch/riscv.rs index 9ecce8508..ebbcdc08e 100644 --- a/kernel/src/arch/riscv.rs +++ b/kernel/src/arch/riscv.rs @@ -5,6 +5,7 @@ pub mod exception; pub mod irq; pub mod mem; pub mod process; +pub mod rand; pub mod syscall; pub use process::Thread; diff --git a/kernel/src/arch/riscv/rand.rs b/kernel/src/arch/riscv/rand.rs new file mode 100644 index 000000000..17be78d44 --- /dev/null +++ b/kernel/src/arch/riscv/rand.rs @@ -0,0 +1,16 @@ +static mut LFSR: u32 = 0xace1u32; + +fn move_lfsr(mut lfsr: u32) -> u32 { + lfsr ^= lfsr >> 7; + lfsr ^= lfsr << 9; + lfsr ^= lfsr >> 13; + lfsr +} + +pub fn get_u32() -> u32 { + // The kernel is currently single-threaded, so this is a valid operation. + unsafe { + LFSR = move_lfsr(LFSR); + LFSR + } +} diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 648df6065..48b102630 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -1348,6 +1348,25 @@ impl SystemServices { Err(xous_kernel::Error::ServerNotFound) } + /// Generate a new server ID for this process and then create a new server. + /// If the + /// server table is full, or if there is not enough memory to map the server queue, + /// return an error. + /// + /// # Errors + /// + /// * **OutOfMemory**: A new page could not be assigned to store the server + /// queue. + /// * **ServerNotFound**: The server queue was full and a free slot could not + /// be found. + pub fn create_server( + &mut self, + pid: PID, + ) -> Result<(SID, CID), xous_kernel::Error> { + let sid = SID::from_u32(arch::rand::get_u32(), arch::rand::get_u32(), arch::rand::get_u32(), arch::rand::get_u32()); + self.create_server_with_address(pid, sid) + } + /// Allocate a new server ID for this process and return the address. If the /// server table is full, return an error. pub fn connect_to_server(&mut self, sid: SID) -> Result { diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 3b7bb8ac3..d8e519ed8 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -751,6 +751,10 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR ss.create_server_with_address(pid, name) .map(|(sid, cid)| xous_kernel::Result::NewServerID(sid, cid)) }), + SysCall::CreateServer => SystemServices::with_mut(|ss| { + ss.create_server(pid) + .map(|(sid, cid)| xous_kernel::Result::NewServerID(sid, cid)) + }), SysCall::TryConnect(sid) => SystemServices::with_mut(|ss| { ss.connect_to_server(sid) .map(xous_kernel::Result::ConnectionID)