mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
kernel: return a Result from syscall::handle
This will simplify error handling. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
f32596570f
commit
569a4196de
3 changed files with 81 additions and 33 deletions
|
|
@ -104,7 +104,7 @@ pub extern "C" fn trap_handler(
|
||||||
_xous_syscall_return_result(&xous::Result::Error(xous::Error::UnhandledSyscall), ctx)
|
_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);
|
println!("Result: {:?}", response);
|
||||||
let mut process = ProcessHandle::get();
|
let mut process = ProcessHandle::get();
|
||||||
|
|
|
||||||
|
|
@ -91,8 +91,9 @@ pub struct ProcessInner {
|
||||||
/// Maximum size of the heap
|
/// Maximum size of the heap
|
||||||
pub mem_heap_max: usize,
|
pub mem_heap_max: usize,
|
||||||
|
|
||||||
/// Which context to return to when `reply_message()` is called
|
/// A mapping of connection IDs to servers
|
||||||
pub ctx_reply: [(PID, u8); 30],
|
pub connection_map: [u8 /* connection id */; 32],
|
||||||
|
pub _reserved: [u8; 28],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ProcessInner {
|
impl Default for ProcessInner {
|
||||||
|
|
@ -105,7 +106,8 @@ impl Default for ProcessInner {
|
||||||
mem_heap_base: arch::mem::DEFAULT_HEAP_BASE,
|
mem_heap_base: arch::mem::DEFAULT_HEAP_BASE,
|
||||||
mem_heap_size: 0,
|
mem_heap_size: 0,
|
||||||
mem_heap_max: 524288,
|
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.
|
/// Allocate a new server ID for this process and return the address.
|
||||||
/// If the server table is full, return an error.
|
/// If the server table is full, return an error.
|
||||||
pub fn connect_to_server(&mut self, sid: SID) -> Result<CID, xous::Error> {
|
pub fn connect_to_server(&mut self, sid: SID) -> Result<CID, xous::Error> {
|
||||||
|
// 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)
|
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
|
/// Get a server based on a SID
|
||||||
pub fn server_mut(&mut self, sid: SID) -> Option<&mut Server> {
|
pub fn server_mut(&mut self, sid: SID) -> Option<&mut Server> {
|
||||||
for server in self.servers.iter_mut() {
|
for server in self.servers.iter_mut() {
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,7 @@ use xous::*;
|
||||||
// ) -> Result<XousMessage, xous::Error>;
|
// ) -> Result<XousMessage, xous::Error>;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
pub fn handle(call: SysCall) -> xous::Result {
|
pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error> {
|
||||||
let pid = arch::current_pid();
|
let pid = arch::current_pid();
|
||||||
|
|
||||||
println!("PID{} Syscall: {:?}", pid, call);
|
println!("PID{} Syscall: {:?}", pid, call);
|
||||||
|
|
@ -142,19 +142,18 @@ pub fn handle(call: SysCall) -> xous::Result {
|
||||||
let mut mm = MemoryManagerHandle::get();
|
let mut mm = MemoryManagerHandle::get();
|
||||||
// Don't let the address exceed the user area (unless it's PID 1)
|
// 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 {
|
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
|
// Don't allow mapping non-page values
|
||||||
} else if size & (PAGE_SIZE - 1) != 0 {
|
} else if size & (PAGE_SIZE - 1) != 0 {
|
||||||
// println!("map: bad alignment of size {:08x}", size);
|
// println!("map: bad alignment of size {:08x}", size);
|
||||||
return xous::Result::Error(xous::Error::BadAlignment);
|
return Err(xous::Error::BadAlignment);
|
||||||
}
|
}
|
||||||
// println!(
|
// println!(
|
||||||
// "Mapping {:08x} -> {:08x} ({} bytes, flags: {:?})",
|
// "Mapping {:08x} -> {:08x} ({} bytes, flags: {:?})",
|
||||||
// phys as u32, virt as u32, size, req_flags
|
// phys as u32, virt as u32, size, req_flags
|
||||||
// );
|
// );
|
||||||
let result = mm.map_range(phys, virt, size, req_flags)
|
let result = mm.map_range(phys, virt, size, req_flags)?;
|
||||||
.unwrap_or_else(|e| xous::Result::Error(e));
|
|
||||||
if let xous::Result::MemoryRange(ref r) = result {
|
if let xous::Result::MemoryRange(ref r) = result {
|
||||||
// If we're handing back an address in main RAM, zero it out
|
// If we're handing back an address in main RAM, zero it out
|
||||||
if phys as usize == 0 || mm.is_main_memory(phys) {
|
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");
|
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) => {
|
SysCall::IncreaseHeap(delta, flags) => {
|
||||||
if delta & 0xfff != 0 {
|
if delta & 0xfff != 0 {
|
||||||
return xous::Result::Error(xous::Error::BadAlignment);
|
return Err(xous::Error::BadAlignment);
|
||||||
}
|
}
|
||||||
let start = {
|
let start = {
|
||||||
let mut process = ProcessHandle::get();
|
let mut process = ProcessHandle::get();
|
||||||
|
|
||||||
if process.inner.mem_heap_size + delta > process.inner.mem_heap_max {
|
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;
|
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();
|
let mut mm = MemoryManagerHandle::get();
|
||||||
mm.reserve_range(start, delta, flags)
|
mm.reserve_range(start, delta, flags)
|
||||||
.unwrap_or_else(|e| xous::Result::Error(e))
|
|
||||||
}
|
}
|
||||||
SysCall::DecreaseHeap(delta) => {
|
SysCall::DecreaseHeap(delta) => {
|
||||||
if delta & 0xfff != 0 {
|
if delta & 0xfff != 0 {
|
||||||
return xous::Result::Error(xous::Error::BadAlignment);
|
return Err(xous::Error::BadAlignment);
|
||||||
}
|
}
|
||||||
let start = {
|
let start = {
|
||||||
let mut process = ProcessHandle::get();
|
let mut process = ProcessHandle::get();
|
||||||
|
|
||||||
if process.inner.mem_heap_size + delta > process.inner.mem_heap_max {
|
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;
|
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)
|
mm.unmap_page(page as *mut usize)
|
||||||
.expect("unable to unmap page");
|
.expect("unable to unmap page");
|
||||||
}
|
}
|
||||||
xous::Result::Ok
|
Ok(xous::Result::Ok)
|
||||||
}
|
}
|
||||||
SysCall::SwitchTo(pid, context) => {
|
SysCall::SwitchTo(pid, context) => {
|
||||||
if context as usize != 0 {
|
if context as usize != 0 {
|
||||||
|
|
@ -214,37 +212,32 @@ pub fn handle(call: SysCall) -> xous::Result {
|
||||||
let mut ss = SystemServicesHandle::get();
|
let mut ss = SystemServicesHandle::get();
|
||||||
ss.resume_pid(pid, ProcessState::Ready)
|
ss.resume_pid(pid, ProcessState::Ready)
|
||||||
.map(|_| xous::Result::ResumeProcess)
|
.map(|_| xous::Result::ResumeProcess)
|
||||||
.unwrap_or_else(|e| xous::Result::Error(e))
|
|
||||||
}
|
}
|
||||||
SysCall::ClaimInterrupt(no, callback, arg) => {
|
SysCall::ClaimInterrupt(no, callback, arg) => {
|
||||||
interrupt_claim(no, pid as definitions::PID, callback, arg)
|
interrupt_claim(no, pid as definitions::PID, callback, arg)
|
||||||
.map(|_| xous::Result::Ok)
|
.map(|_| xous::Result::Ok)
|
||||||
.unwrap_or_else(|e| xous::Result::Error(e))
|
|
||||||
}
|
}
|
||||||
SysCall::Yield => {
|
SysCall::Yield => {
|
||||||
let mut ss = SystemServicesHandle::get();
|
let mut ss = SystemServicesHandle::get();
|
||||||
let ppid = ss.get_process(pid).expect("Can't get current process").ppid;
|
let ppid = ss.get_process(pid).expect("Can't get current process").ppid;
|
||||||
assert_ne!(ppid, 0, "no parent process id");
|
assert_ne!(ppid, 0, "no parent process id");
|
||||||
ss.resume_pid(ppid, ProcessState::Ready)
|
ss.resume_pid(ppid, ProcessState::Ready)
|
||||||
.map(|_| xous::Result::ResumeProcess)
|
.map(|_| Ok(xous::Result::ResumeProcess))
|
||||||
.unwrap_or(xous::Result::Error(xous::Error::ProcessNotFound))
|
.unwrap_or(Err(xous::Error::ProcessNotFound))
|
||||||
}
|
}
|
||||||
SysCall::WaitMessage(sid) => {
|
SysCall::WaitMessage(sid) => {
|
||||||
let mut ss = SystemServicesHandle::get();
|
let mut ss = SystemServicesHandle::get();
|
||||||
// See if there is a pending message. If so, return immediately.
|
// See if there is a pending message. If so, return immediately.
|
||||||
let mut server = match ss.server_mut(sid) {
|
let mut server = ss.server_mut(sid).ok_or(xous::Error::ServerNotFound)?;
|
||||||
None => return xous::Result::Error(xous::Error::ServerNotFound),
|
|
||||||
Some(server) => server,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Ensure the server is for this PID
|
// Ensure the server is for this PID
|
||||||
if server.pid != 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 there is a pending message, return it immediately.
|
||||||
if let Some(msg) = server.take_next_message() {
|
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
|
// 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;
|
let ppid = ss.get_process(pid).expect("Can't get current process").ppid;
|
||||||
assert_ne!(ppid, 0, "no parent process id");
|
assert_ne!(ppid, 0, "no parent process id");
|
||||||
ss.resume_pid(ppid, ProcessState::Sleeping)
|
ss.resume_pid(ppid, ProcessState::Sleeping)
|
||||||
.map(|_| xous::Result::ResumeProcess)
|
.map(|_| Ok(xous::Result::ResumeProcess))
|
||||||
.unwrap_or(xous::Result::Error(xous::Error::ProcessNotFound))
|
.unwrap_or(Err(xous::Error::ProcessNotFound))
|
||||||
}
|
}
|
||||||
SysCall::WaitEvent => {
|
SysCall::WaitEvent => {
|
||||||
let mut ss = SystemServicesHandle::get();
|
let mut ss = SystemServicesHandle::get();
|
||||||
|
|
@ -261,21 +254,24 @@ pub fn handle(call: SysCall) -> xous::Result {
|
||||||
let ppid = process.ppid;
|
let ppid = process.ppid;
|
||||||
assert_ne!(ppid, 0, "no parent process id");
|
assert_ne!(ppid, 0, "no parent process id");
|
||||||
ss.resume_pid(ppid, ProcessState::Sleeping)
|
ss.resume_pid(ppid, ProcessState::Sleeping)
|
||||||
.map(|_| xous::Result::ResumeProcess)
|
.map(|_| Ok(xous::Result::ResumeProcess))
|
||||||
.unwrap_or(xous::Result::Error(xous::Error::ProcessNotFound))
|
.unwrap_or(Err(xous::Error::ProcessNotFound))
|
||||||
}
|
}
|
||||||
SysCall::CreateServer(name) => {
|
SysCall::CreateServer(name) => {
|
||||||
let mut ss = SystemServicesHandle::get();
|
let mut ss = SystemServicesHandle::get();
|
||||||
ss.create_server(name)
|
ss.create_server(name)
|
||||||
.map(|x| xous::Result::ServerID(x))
|
.map(|x| xous::Result::ServerID(x))
|
||||||
.unwrap_or_else(|e| xous::Result::Error(e))
|
|
||||||
}
|
}
|
||||||
SysCall::Connect(sid) => {
|
SysCall::Connect(sid) => {
|
||||||
let mut ss = SystemServicesHandle::get();
|
let mut ss = SystemServicesHandle::get();
|
||||||
ss.connect_to_server(sid)
|
ss.connect_to_server(sid)
|
||||||
.map(|x| xous::Result::ConnectionID(x))
|
.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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue