From 32eb3c202c1b9c001921c7d2609235bb6904b82e Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 6 Aug 2021 17:48:17 +0800 Subject: [PATCH] xous: add exception handler support This has been tested end-to-end. We now have exception handler support. There is a potential issue when passing messages to our own process in an exception handler. This will need to be investigated. Signed-off-by: Sean Cross --- xous-rs/src/definitions.rs | 16 +---- xous-rs/src/definitions/exceptions.rs | 84 +++++++++++++++++++++++++++ xous-rs/src/syscall.rs | 19 ++++-- 3 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 xous-rs/src/definitions/exceptions.rs diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 45da13fc8..8213b7a75 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -40,20 +40,8 @@ pub const EC_FW_PKG_LOC: u32 = 0x07FC_E000; pub const EC_FW_PKG_LEN: u32 = 0x0003_2000; pub const EC_REGION_LEN: u32 = 0x0008_0000; -#[derive(Debug)] -#[repr(usize)] -pub enum ExceptionType { - InstructionAddressMisaligned = 0, - InstructionAccessFault = 1, - IllegalInstruction = 2, - LoadAddressMisaligned = 3, - LoadAccessFault = 4, - StoreAddressMisaligned = 5, - StoreAccessFault = 6, - InstructionPageFault = 7, - LoadPageFault = 8, - StorePageFault = 9, -} +pub mod exceptions; +pub use exceptions::*; #[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Copy, Clone)] pub struct MessageSender { diff --git a/xous-rs/src/definitions/exceptions.rs b/xous-rs/src/definitions/exceptions.rs new file mode 100644 index 000000000..cb0d9e954 --- /dev/null +++ b/xous-rs/src/definitions/exceptions.rs @@ -0,0 +1,84 @@ +#[derive(Debug, PartialEq, Clone, Copy)] +#[repr(usize)] +pub enum ExceptionType { + InstructionAddressMisaligned = 0, + InstructionAccessFault = 1, + IllegalInstruction = 2, + LoadAddressMisaligned = 3, + LoadAccessFault = 4, + StoreAddressMisaligned = 5, + StoreAccessFault = 6, + InstructionPageFault = 7, + LoadPageFault = 8, + StorePageFault = 9, +} + +#[derive(Debug, PartialEq, Clone, Copy)] +pub enum Exception { + InstructionAddressMisaligned(usize /* epc */, usize /* addr */), + InstructionAccessFault(usize /* epc */, usize /* addr */), + IllegalInstruction(usize /* epc */, usize /* instruction */), + LoadAddressMisaligned(usize /* epc */, usize /* addr */), + LoadAccessFault(usize /* epc */, usize /* addr */), + StoreAddressMisaligned(usize /* epc */, usize /* addr */), + StoreAccessFault(usize /* epc */, usize /* addr */), + InstructionPageFault(usize /* epc */, usize /* addr */), + LoadPageFault(usize /* epc */, usize /* addr */), + StorePageFault(usize /* epc */, usize /* addr */), + Unknown(usize, usize, usize), +} + +impl Exception { + pub fn new(a0: usize, a1: usize, a2: usize) -> Exception { + match a0 { + 0 /*ExceptionType::InstructionAddressMisaligned as usize*/ => { + Exception::InstructionAddressMisaligned(a1, a2) + } + 1 /*ExceptionType::InstructionAccessFault as usize*/ => { + Exception::InstructionAccessFault(a1, a2) + } + 2 /*ExceptionType::IllegalInstruction as usize*/ => Exception::IllegalInstruction(a1, a2), + 3 /*ExceptionType::LoadAddressMisaligned as usize*/ => Exception::LoadAddressMisaligned(a1, a2), + 4 /*ExceptionType::LoadAccessFault as usize*/ => Exception::LoadAccessFault(a1, a2), + 5 /*ExceptionType::StoreAddressMisaligned as usize*/ => { + Exception::StoreAddressMisaligned(a1, a2) + } + 6 /*ExceptionType::StoreAccessFault as usize*/ => Exception::StoreAccessFault(a1, a2), + 7 /*ExceptionType::InstructionPageFault as usize*/ => Exception::InstructionPageFault(a1, a2), + 8 /*ExceptionType::LoadPageFault as usize*/ => Exception::LoadPageFault(a1, a2), + 9 /*ExceptionType::StorePageFault as usize*/ => Exception::StorePageFault(a1, a2), + _ => Exception::Unknown(a0, a1, a2), + } + } + + pub fn pc(&self) -> usize { + match *self { + Exception::InstructionAddressMisaligned(pc, _) + | Exception::InstructionAccessFault(pc, _) + | Exception::IllegalInstruction(pc, _) + | Exception::LoadAddressMisaligned(pc, _) + | Exception::LoadAccessFault(pc, _) + | Exception::StoreAddressMisaligned(pc, _) + | Exception::StoreAccessFault(pc, _) + | Exception::InstructionPageFault(pc, _) + | Exception::LoadPageFault(pc, _) + | Exception::StorePageFault(pc, _) + | Exception::Unknown(_, pc, _) => pc, + } + } + + pub fn address(&self) -> Option { + match *self { + Exception::InstructionAddressMisaligned(_, address) + | Exception::InstructionAccessFault(_, address) + | Exception::LoadAddressMisaligned(_, address) + | Exception::LoadAccessFault(_, address) + | Exception::StoreAddressMisaligned(_, address) + | Exception::StoreAccessFault(_, address) + | Exception::InstructionPageFault(_, address) + | Exception::LoadPageFault(_, address) + | Exception::StorePageFault(_, address) => Some(address), + _ => None, + } + } +} diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 72464afa2..f7f29aee4 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -1,7 +1,7 @@ use crate::{ - pid_from_usize, CpuID, Error, MemoryAddress, MemoryFlags, MemoryMessage, MemoryRange, - MemorySize, MemoryType, Message, MessageEnvelope, MessageSender, ProcessArgs, ProcessInit, - Result, ScalarMessage, SysCallResult, ThreadInit, CID, PID, SID, TID, + pid_from_usize, CpuID, Error, Exception, MemoryAddress, MemoryFlags, MemoryMessage, + MemoryRange, MemorySize, MemoryType, Message, MessageEnvelope, MessageSender, ProcessArgs, + ProcessInit, Result, ScalarMessage, SysCallResult, ThreadInit, CID, PID, SID, TID, }; use core::convert::{TryFrom, TryInto}; @@ -1634,11 +1634,19 @@ pub fn join_thread(tid: TID) -> core::result::Result { }) } +static EXCEPTION_HANDLER: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0); +fn handle_exception(exception_type: usize, arg1: usize, arg2: usize) -> isize { + let exception = crate::exceptions::Exception::new(exception_type, arg1, arg2); + let f = EXCEPTION_HANDLER.load(core::sync::atomic::Ordering::SeqCst); + let f = unsafe { core::mem::transmute:: isize>(f) }; + f(exception) +} + /// Sets the given function as this process' Exception handler. This function /// will be called whenever an Exception occurs such as a memory fault, /// illegal instruction, or a child process terminating. pub fn set_exception_handler( - handler: fn(crate::ExceptionType, usize, usize) -> isize, + handler: fn(crate::Exception) -> isize, ) -> core::result::Result<(), Error> { #[cfg(feature = "bit-flags")] let flags = crate::MemoryFlags::R | crate::MemoryFlags::W | crate::MemoryFlags::RESERVE; @@ -1646,8 +1654,9 @@ pub fn set_exception_handler( let flags = 0b0000_0010 | 0b0000_0100 | 0b0000_0001; let stack = crate::map_memory(None, None, 131_072, flags)?; + EXCEPTION_HANDLER.store(handler as usize, core::sync::atomic::Ordering::SeqCst); rsyscall(SysCall::SetExceptionHandler( - handler as usize, + handle_exception as usize, stack.as_ptr() as usize, )) .and_then(|result| {