diff --git a/kernel/src/arch/riscv/irq.rs b/kernel/src/arch/riscv/irq.rs index b9d83aaf1..871812885 100644 --- a/kernel/src/arch/riscv/irq.rs +++ b/kernel/src/arch/riscv/irq.rs @@ -141,6 +141,25 @@ pub extern "C" fn trap_handler( }) .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"))] print!( "KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x} - ", @@ -148,6 +167,22 @@ pub extern "C" fn trap_handler( ); 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) => { 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()) }); } + RiscvException::InstructionPageFault(RETURN_FROM_ISR, _offset) => { // If we hit this address, then an ISR has just returned. Since // 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()) }); } + _ => (), } println!("SYSTEM HALT: CPU Exception on PID {}: {}", pid, ex); diff --git a/kernel/src/arch/riscv/process.rs b/kernel/src/arch/riscv/process.rs index e61ab4c4e..5b43f94d0 100644 --- a/kernel/src/arch/riscv/process.rs +++ b/kernel/src/arch/riscv/process.rs @@ -4,7 +4,8 @@ use core::mem; static mut PROCESS: *mut ProcessImpl = 0xff80_1000 as *mut ProcessImpl; 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; use crate::arch::mem::PAGE_SIZE; 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. 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: // Logical Thread ID: What the user sees // Thread Context Index: An index into the thread slice diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 0453205c0..f7557ef25 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -118,7 +118,7 @@ pub struct Process { previous_thread: TID, /// When an exception is hit, the kernel will switch to this Thread. - exception_thread: Option, + exception_handler: Option<(usize /* handler */, usize /* Stack */)>, } impl Default for Process { @@ -254,7 +254,7 @@ static mut SYSTEM_SERVICES: SystemServices = SystemServices { mapping: arch::mem::DEFAULT_MEMORY_MAPPING, current_thread: 0 as TID, previous_thread: INITIAL_TID as TID, - exception_thread: None, + exception_handler: None, }; MAX_PROCESS_COUNT], // Note we can't use MAX_SERVER_COUNT here because of how Rust's // macro tokenization works @@ -665,9 +665,7 @@ impl SystemServices { ProcessState::Exception(x) => { process.activate()?; let mut p = crate::arch::process::Process::current(); - let tid = process - .exception_thread - .expect("process is in exception state with no handler"); + let tid = crate::arch::process::EXCEPTION_TID; p.set_thread(tid)?; process.current_thread = tid as _; ProcessState::Exception(x) @@ -910,7 +908,7 @@ impl SystemServices { } ProcessState::Setup(_) | ProcessState::Allocated => new_tid = INITIAL_TID, 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) => { // If no new context is specified, take the previous @@ -2127,6 +2125,11 @@ impl SystemServices { Ok(()) } + /// + pub fn handle_exception(&mut self) -> Option { + None + } + /// Returns the process name, if any, of a given PID #[cfg(baremetal)] pub fn process_name(&self, pid: PID) -> Option<&str> {