diff --git a/kernel/src/arch/riscv/irq.rs b/kernel/src/arch/riscv/irq.rs index 191458234..9b8c63de1 100644 --- a/kernel/src/arch/riscv/irq.rs +++ b/kernel/src/arch/riscv/irq.rs @@ -104,7 +104,7 @@ pub extern "C" fn trap_handler( _xous_syscall_return_result(&xous::Result::Error(xous::Error::UnhandledSyscall), ctx) }); - let response = crate::syscall::handle(call); + let response = crate::syscall::handle(call).unwrap_or_else(|e| xous::Result::Error(e)); println!("Result: {:?}", response); let mut process = ProcessHandle::get(); diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 0b695055c..843e4babd 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -91,8 +91,9 @@ pub struct ProcessInner { /// Maximum size of the heap pub mem_heap_max: usize, - /// Which context to return to when `reply_message()` is called - pub ctx_reply: [(PID, u8); 30], + /// A mapping of connection IDs to servers + pub connection_map: [u8 /* connection id */; 32], + pub _reserved: [u8; 28], } impl Default for ProcessInner { @@ -105,7 +106,8 @@ impl Default for ProcessInner { mem_heap_base: arch::mem::DEFAULT_HEAP_BASE, mem_heap_size: 0, mem_heap_max: 524288, - ctx_reply: [(0, 0); 30], + connection_map: [0; 32], + _reserved: [0; 28], } } } @@ -613,9 +615,59 @@ impl SystemServices { /// 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 { + // Check to see if we've already connected to this server. + // While doing this, find a free slot in case we haven't + // yet connected. + let mut slot_idx = None; + let mut process = ProcessHandle::get(); + + // Look through the connection map for (1) a free slot, and (2) an existing connection + for (idx, server_idx) in process.inner.connection_map.iter().enumerate() { + // If we find an empty slot, use it + if *server_idx == 0 { + slot_idx = Some(idx); + } + // If a connection to this server ID exists already, return it. + if self.servers[*server_idx as usize].sid == sid { + return Ok(idx as CID + 1); + } + } + let slot_idx = slot_idx.ok_or_else(|| xous::Error::OutOfMemory)?; + + // Look through all servers for one whose SID matches. + for (idx, server) in self.servers.iter().enumerate() { + if server.sid == sid { + process.inner.connection_map[slot_idx] = idx as u8 + 1; + return Ok(idx + 1); + } + } Err(xous::Error::OutOfMemory) } + /// Return a server based on the connection id and the current process + pub fn server_from_cid(&self, cid: CID) -> Option<&Server> { + if cid == 0 { + return None; + } + let process = ProcessHandle::get(); + if cid >= process.inner.connection_map.len() { + return None; + } + let server_idx = process.inner.connection_map[cid] as usize; + if server_idx == 0 { + return None; + } + let server_idx = server_idx - 1; + if server_idx >= self.servers.len() { + return None; + } + if let ServerState::Ready(_) = self.servers[server_idx].state { + } else { + return None; + } + Some(&self.servers[server_idx]) + } + /// Get a server based on a SID pub fn server_mut(&mut self, sid: SID) -> Option<&mut Server> { for server in self.servers.iter_mut() { diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 4d4877f1e..a6743a4f7 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -133,7 +133,7 @@ use xous::*; // ) -> Result; // } -pub fn handle(call: SysCall) -> xous::Result { +pub fn handle(call: SysCall) -> core::result::Result { let pid = arch::current_pid(); println!("PID{} Syscall: {:?}", pid, call); @@ -142,19 +142,18 @@ pub fn handle(call: SysCall) -> xous::Result { let mut mm = MemoryManagerHandle::get(); // Don't let the address exceed the user area (unless it's PID 1) if pid != 1 && (virt as usize) != 0 && (virt as usize) >= arch::mem::USER_AREA_END { - return xous::Result::Error(xous::Error::BadAddress); + return Err(xous::Error::BadAddress); // Don't allow mapping non-page values } else if size & (PAGE_SIZE - 1) != 0 { // println!("map: bad alignment of size {:08x}", size); - return xous::Result::Error(xous::Error::BadAlignment); + return Err(xous::Error::BadAlignment); } // println!( // "Mapping {:08x} -> {:08x} ({} bytes, flags: {:?})", // phys as u32, virt as u32, size, req_flags // ); - let result = mm.map_range(phys, virt, size, req_flags) - .unwrap_or_else(|e| xous::Result::Error(e)); + let result = mm.map_range(phys, virt, size, req_flags)?; if let xous::Result::MemoryRange(ref r) = result { // If we're handing back an address in main RAM, zero it out if phys as usize == 0 || mm.is_main_memory(phys) { @@ -164,17 +163,17 @@ pub fn handle(call: SysCall) -> xous::Result { crate::arch::mem::hand_page_to_user(offset as *mut usize).expect("couldn't hand page to user"); } } - result + Ok(result) } SysCall::IncreaseHeap(delta, flags) => { if delta & 0xfff != 0 { - return xous::Result::Error(xous::Error::BadAlignment); + return Err(xous::Error::BadAlignment); } let start = { let mut process = ProcessHandle::get(); if process.inner.mem_heap_size + delta > process.inner.mem_heap_max { - return xous::Result::Error(xous::Error::OutOfMemory); + return Err(xous::Error::OutOfMemory); } let start = process.inner.mem_heap_base + process.inner.mem_heap_size; @@ -183,17 +182,16 @@ pub fn handle(call: SysCall) -> xous::Result { }; let mut mm = MemoryManagerHandle::get(); mm.reserve_range(start, delta, flags) - .unwrap_or_else(|e| xous::Result::Error(e)) } SysCall::DecreaseHeap(delta) => { if delta & 0xfff != 0 { - return xous::Result::Error(xous::Error::BadAlignment); + return Err(xous::Error::BadAlignment); } let start = { let mut process = ProcessHandle::get(); if process.inner.mem_heap_size + delta > process.inner.mem_heap_max { - return xous::Result::Error(xous::Error::OutOfMemory); + return Err(xous::Error::OutOfMemory); } let start = process.inner.mem_heap_base + process.inner.mem_heap_size; @@ -205,7 +203,7 @@ pub fn handle(call: SysCall) -> xous::Result { mm.unmap_page(page as *mut usize) .expect("unable to unmap page"); } - xous::Result::Ok + Ok(xous::Result::Ok) } SysCall::SwitchTo(pid, context) => { if context as usize != 0 { @@ -214,37 +212,32 @@ pub fn handle(call: SysCall) -> xous::Result { let mut ss = SystemServicesHandle::get(); ss.resume_pid(pid, ProcessState::Ready) .map(|_| xous::Result::ResumeProcess) - .unwrap_or_else(|e| xous::Result::Error(e)) } SysCall::ClaimInterrupt(no, callback, arg) => { interrupt_claim(no, pid as definitions::PID, callback, arg) .map(|_| xous::Result::Ok) - .unwrap_or_else(|e| xous::Result::Error(e)) } SysCall::Yield => { let mut ss = SystemServicesHandle::get(); let ppid = ss.get_process(pid).expect("Can't get current process").ppid; assert_ne!(ppid, 0, "no parent process id"); ss.resume_pid(ppid, ProcessState::Ready) - .map(|_| xous::Result::ResumeProcess) - .unwrap_or(xous::Result::Error(xous::Error::ProcessNotFound)) + .map(|_| Ok(xous::Result::ResumeProcess)) + .unwrap_or(Err(xous::Error::ProcessNotFound)) } SysCall::WaitMessage(sid) => { let mut ss = SystemServicesHandle::get(); // See if there is a pending message. If so, return immediately. - let mut server = match ss.server_mut(sid) { - None => return xous::Result::Error(xous::Error::ServerNotFound), - Some(server) => server, - }; + let mut server = ss.server_mut(sid).ok_or(xous::Error::ServerNotFound)?; // Ensure the server is for this PID if server.pid != pid { - return xous::Result::Error(xous::Error::ServerNotFound); + return Err(xous::Error::ServerNotFound); } // If there is a pending message, return it immediately. if let Some(msg) = server.take_next_message() { - return xous::Result::Message(msg); + return Ok(xous::Result::Message(msg)); } // There is no pending message, so return control to the parent process @@ -252,8 +245,8 @@ pub fn handle(call: SysCall) -> xous::Result { let ppid = ss.get_process(pid).expect("Can't get current process").ppid; assert_ne!(ppid, 0, "no parent process id"); ss.resume_pid(ppid, ProcessState::Sleeping) - .map(|_| xous::Result::ResumeProcess) - .unwrap_or(xous::Result::Error(xous::Error::ProcessNotFound)) + .map(|_| Ok(xous::Result::ResumeProcess)) + .unwrap_or(Err(xous::Error::ProcessNotFound)) } SysCall::WaitEvent => { let mut ss = SystemServicesHandle::get(); @@ -261,21 +254,24 @@ pub fn handle(call: SysCall) -> xous::Result { let ppid = process.ppid; assert_ne!(ppid, 0, "no parent process id"); ss.resume_pid(ppid, ProcessState::Sleeping) - .map(|_| xous::Result::ResumeProcess) - .unwrap_or(xous::Result::Error(xous::Error::ProcessNotFound)) + .map(|_| Ok(xous::Result::ResumeProcess)) + .unwrap_or(Err(xous::Error::ProcessNotFound)) } SysCall::CreateServer(name) => { let mut ss = SystemServicesHandle::get(); ss.create_server(name) .map(|x| xous::Result::ServerID(x)) - .unwrap_or_else(|e| xous::Result::Error(e)) } SysCall::Connect(sid) => { let mut ss = SystemServicesHandle::get(); ss.connect_to_server(sid) .map(|x| xous::Result::ConnectionID(x)) - .unwrap_or_else(|e| xous::Result::Error(e)) } - _ => xous::Result::Error(xous::Error::UnhandledSyscall), + SysCall::SendMessage(cid, message) => { + let mut ss = SystemServicesHandle::get(); + let server = ss.server_from_cid(cid).ok_or(xous::Error::ServerNotFound)?; + Err(xous::Error::UnhandledSyscall) + } + _ => Err(xous::Error::UnhandledSyscall), } }