kernel: add framework for exceptions

Add the ability to invoke exception handlers. This is not yet properly
wired up, but the framework is there.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-08-02 11:04:47 +08:00
parent 2cd42edc22
commit f5cf94524a
3 changed files with 51 additions and 7 deletions

View file

@ -141,6 +141,25 @@ pub extern "C" fn trap_handler(
}) })
.expect("Couldn't allocate page"); .expect("Couldn't allocate page");
// If there is an exception of some sort, see if there is an exception handler
// available. If so, invoke it.
if let Some((pc, sp)) = SystemServices::with_mut(|ss| ss.handle_exception()) {
ArchProcess::with_current_mut(|process| {
crate::arch::syscall::invoke(
process.thread_mut(crate::arch::process::EXCEPTION_TID),
current_pid().get() == 1,
pc,
sp,
RETURN_FROM_EXCEPTION_HANDLER,
args,
);
crate::arch::syscall::resume(
current_pid().get() == 1,
process.thread(crate::arch::process::EXCEPTION_TID),
)
});
}
#[cfg(all(not(feature = "debug-print"), feature = "print-panics"))] #[cfg(all(not(feature = "debug-print"), feature = "print-panics"))]
print!( print!(
"KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x} - ", "KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x} - ",
@ -148,6 +167,22 @@ pub extern "C" fn trap_handler(
); );
println!("Page was not allocated"); println!("Page was not allocated");
} }
RiscvException::InstructionPageFault(RETURN_FROM_EXCEPTION_HANDLER, _offset) => {
let tid = ArchProcess::with_current(|process| process.current_tid());
// This address indicates a thread has exited. Destroy the thread.
// This activates another thread within this process.
if SystemServices::with_mut(|ss| ss.destroy_thread(pid, tid)).unwrap() {
crate::syscall::reset_switchto_caller();
}
// Resume the new thread within the same process.
ArchProcess::with_current_mut(|p| {
crate::arch::syscall::resume(current_pid().get() == 1, p.current_thread())
});
}
RiscvException::InstructionPageFault(EXIT_THREAD, _offset) => { RiscvException::InstructionPageFault(EXIT_THREAD, _offset) => {
let tid = ArchProcess::with_current(|process| process.current_tid()); let tid = ArchProcess::with_current(|process| process.current_tid());
@ -162,6 +197,7 @@ pub extern "C" fn trap_handler(
crate::arch::syscall::resume(current_pid().get() == 1, p.current_thread()) crate::arch::syscall::resume(current_pid().get() == 1, p.current_thread())
}); });
} }
RiscvException::InstructionPageFault(RETURN_FROM_ISR, _offset) => { RiscvException::InstructionPageFault(RETURN_FROM_ISR, _offset) => {
// If we hit this address, then an ISR has just returned. Since // If we hit this address, then an ISR has just returned. Since
// we're in an interrupt context, it is safe to access this // we're in an interrupt context, it is safe to access this
@ -188,6 +224,7 @@ pub extern "C" fn trap_handler(
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread()) crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
}); });
} }
_ => (), _ => (),
} }
println!("SYSTEM HALT: CPU Exception on PID {}: {}", pid, ex); println!("SYSTEM HALT: CPU Exception on PID {}: {}", pid, ex);

View file

@ -4,7 +4,8 @@
use core::mem; use core::mem;
static mut PROCESS: *mut ProcessImpl = 0xff80_1000 as *mut ProcessImpl; static mut PROCESS: *mut ProcessImpl = 0xff80_1000 as *mut ProcessImpl;
pub const MAX_THREAD: TID = 31; pub const MAX_THREAD: TID = 31;
pub const INITIAL_TID: TID = 1; pub const EXCEPTION_TID: TID = 1;
pub const INITIAL_TID: TID = 2;
pub const IRQ_TID: TID = 0; pub const IRQ_TID: TID = 0;
use crate::arch::mem::PAGE_SIZE; use crate::arch::mem::PAGE_SIZE;
use crate::services::ProcessInner; use crate::services::ProcessInner;
@ -21,6 +22,9 @@ pub const RETURN_FROM_ISR: usize = 0xff80_2000;
/// This is the address a thread will return to when it exits. /// This is the address a thread will return to when it exits.
pub const EXIT_THREAD: usize = 0xff80_3000; pub const EXIT_THREAD: usize = 0xff80_3000;
/// This is the address a thread will return to when it finishes handling an exception.
pub const RETURN_FROM_EXCEPTION_HANDLER: usize = 0xff80_4000;
// Thread IDs have three possible meaning: // Thread IDs have three possible meaning:
// Logical Thread ID: What the user sees // Logical Thread ID: What the user sees
// Thread Context Index: An index into the thread slice // Thread Context Index: An index into the thread slice

View file

@ -118,7 +118,7 @@ pub struct Process {
previous_thread: TID, previous_thread: TID,
/// When an exception is hit, the kernel will switch to this Thread. /// When an exception is hit, the kernel will switch to this Thread.
exception_thread: Option<TID>, exception_handler: Option<(usize /* handler */, usize /* Stack */)>,
} }
impl Default for Process { impl Default for Process {
@ -254,7 +254,7 @@ static mut SYSTEM_SERVICES: SystemServices = SystemServices {
mapping: arch::mem::DEFAULT_MEMORY_MAPPING, mapping: arch::mem::DEFAULT_MEMORY_MAPPING,
current_thread: 0 as TID, current_thread: 0 as TID,
previous_thread: INITIAL_TID as TID, previous_thread: INITIAL_TID as TID,
exception_thread: None, exception_handler: None,
}; MAX_PROCESS_COUNT], }; MAX_PROCESS_COUNT],
// Note we can't use MAX_SERVER_COUNT here because of how Rust's // Note we can't use MAX_SERVER_COUNT here because of how Rust's
// macro tokenization works // macro tokenization works
@ -665,9 +665,7 @@ impl SystemServices {
ProcessState::Exception(x) => { ProcessState::Exception(x) => {
process.activate()?; process.activate()?;
let mut p = crate::arch::process::Process::current(); let mut p = crate::arch::process::Process::current();
let tid = process let tid = crate::arch::process::EXCEPTION_TID;
.exception_thread
.expect("process is in exception state with no handler");
p.set_thread(tid)?; p.set_thread(tid)?;
process.current_thread = tid as _; process.current_thread = tid as _;
ProcessState::Exception(x) ProcessState::Exception(x)
@ -910,7 +908,7 @@ impl SystemServices {
} }
ProcessState::Setup(_) | ProcessState::Allocated => new_tid = INITIAL_TID, ProcessState::Setup(_) | ProcessState::Allocated => new_tid = INITIAL_TID,
ProcessState::Exception(_) => { ProcessState::Exception(_) => {
new.current_thread = new.exception_thread.expect("no exception state set"); new.current_thread = crate::arch::process::EXCEPTION_TID;
} }
ProcessState::Running(x) | ProcessState::Ready(x) => { ProcessState::Running(x) | ProcessState::Ready(x) => {
// If no new context is specified, take the previous // If no new context is specified, take the previous
@ -2127,6 +2125,11 @@ impl SystemServices {
Ok(()) Ok(())
} }
///
pub fn handle_exception(&mut self) -> Option<ExceptionHandler> {
None
}
/// Returns the process name, if any, of a given PID /// Returns the process name, if any, of a given PID
#[cfg(baremetal)] #[cfg(baremetal)]
pub fn process_name(&self, pid: PID) -> Option<&str> { pub fn process_name(&self, pid: PID) -> Option<&str> {