mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
kernel: riscv: fixup exception debug handling
Debug handling was not correct, and didn't work at all. Additionally, interrupt masking didn't work. Fix these problems. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
5c52fbd522
commit
3469c292ec
2 changed files with 251 additions and 236 deletions
|
|
@ -65,10 +65,10 @@ pub enum RiscvException {
|
||||||
StoreAccessFault(usize /* epc */, usize /* target address */),
|
StoreAccessFault(usize /* epc */, usize /* target address */),
|
||||||
|
|
||||||
/// 0 8
|
/// 0 8
|
||||||
CallFromUMode(usize /* epc */),
|
CallFromUMode(usize /* epc */, usize /* ??? */),
|
||||||
|
|
||||||
/// 0 9
|
/// 0 9
|
||||||
CallFromSMode(usize /* epc */),
|
CallFromSMode(usize /* epc */, usize /* ??? */),
|
||||||
|
|
||||||
// [reserved]
|
// [reserved]
|
||||||
/// 0 11
|
/// 0 11
|
||||||
|
|
@ -148,8 +148,8 @@ impl fmt::Display for RiscvException {
|
||||||
StoreAccessFault(epc, tval) => {
|
StoreAccessFault(epc, tval) => {
|
||||||
write!(f, "Store access fault to 0x{:08x} at 0x{:08x}", tval, epc)
|
write!(f, "Store access fault to 0x{:08x} at 0x{:08x}", tval, epc)
|
||||||
}
|
}
|
||||||
CallFromUMode(epc) => write!(f, "Call from User mode at 0x{:08x}", epc),
|
CallFromUMode(epc, tval) => write!(f, "Call from User mode at 0x{:08x} (???: 0x{:08x})", epc, tval),
|
||||||
CallFromSMode(epc) => write!(f, "Call from Supervisor mode at 0x{:08x}", epc),
|
CallFromSMode(epc, tval) => write!(f, "Call from Supervisor mode at 0x{:08x} (???: 0x{:08x})", epc, tval),
|
||||||
// --reserved--
|
// --reserved--
|
||||||
CallFromMMode(epc) => write!(f, "Call from Machine mode at 0x{:08x}", epc),
|
CallFromMMode(epc) => write!(f, "Call from Machine mode at 0x{:08x}", epc),
|
||||||
InstructionPageFault(epc, tval) => write!(
|
InstructionPageFault(epc, tval) => write!(
|
||||||
|
|
@ -203,8 +203,8 @@ impl RiscvException {
|
||||||
5 => LoadAccessFault(epc, tval),
|
5 => LoadAccessFault(epc, tval),
|
||||||
6 => StoreAddressMisaligned(epc, tval),
|
6 => StoreAddressMisaligned(epc, tval),
|
||||||
7 => StoreAccessFault(epc, tval),
|
7 => StoreAccessFault(epc, tval),
|
||||||
8 => CallFromUMode(epc),
|
8 => CallFromUMode(epc, tval),
|
||||||
9 => CallFromSMode(epc),
|
9 => CallFromSMode(epc, tval),
|
||||||
// --reserved--
|
// --reserved--
|
||||||
11 => CallFromMMode(epc),
|
11 => CallFromMMode(epc),
|
||||||
12 => InstructionPageFault(epc, tval),
|
12 => InstructionPageFault(epc, tval),
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use crate::arch::process::{Process as ArchProcess, RETURN_FROM_EXCEPTION_HANDLER
|
||||||
use crate::arch::process::{Thread, EXIT_THREAD, RETURN_FROM_ISR};
|
use crate::arch::process::{Thread, EXIT_THREAD, RETURN_FROM_ISR};
|
||||||
use crate::services::SystemServices;
|
use crate::services::SystemServices;
|
||||||
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||||
use riscv::register::{scause, sepc, sstatus, stval, vexriscv::sip};
|
use riscv::register::{scause, sepc, sstatus, stval};
|
||||||
use xous_kernel::{SysCall, PID, TID};
|
use xous_kernel::{SysCall, PID, TID};
|
||||||
|
|
||||||
extern "Rust" {
|
extern "Rust" {
|
||||||
|
|
@ -23,19 +23,22 @@ static IRQ_ENABLED: AtomicBool = AtomicBool::new(true);
|
||||||
|
|
||||||
fn sim_read() -> usize {
|
fn sim_read() -> usize {
|
||||||
let existing: usize;
|
let existing: usize;
|
||||||
unsafe { core::arch::asm!("csrr {0}, 0x9c0", out(reg) existing) };
|
unsafe { core::arch::asm!("csrrs {0}, 0x9C0, zero", out(reg) existing) };
|
||||||
existing
|
existing
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sim_write(new: usize) {
|
fn sim_write(new: usize) {
|
||||||
unsafe { core::arch::asm!("csrw 0x9c0, {0}", in(reg) new) };
|
unsafe { core::arch::asm!("csrrw zero, 0x9C0, {0}", in(reg) new) };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sip_read() -> usize {
|
||||||
|
let existing: usize;
|
||||||
|
unsafe { core::arch::asm!("csrrs {0}, 0xDC0, zero", out(reg) existing) };
|
||||||
|
existing
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Disable external interrupts
|
/// Disable external interrupts
|
||||||
pub fn disable_all_irqs() {
|
pub fn disable_all_irqs() {
|
||||||
if !IRQ_ENABLED.load(Ordering::Relaxed) {
|
|
||||||
panic!("IRQs were disabled twice!");
|
|
||||||
}
|
|
||||||
SIM_BACKING.store(sim_read(), Ordering::Relaxed);
|
SIM_BACKING.store(sim_read(), Ordering::Relaxed);
|
||||||
IRQ_ENABLED.store(false, Ordering::Relaxed);
|
IRQ_ENABLED.store(false, Ordering::Relaxed);
|
||||||
sim_write(0x0);
|
sim_write(0x0);
|
||||||
|
|
@ -54,16 +57,11 @@ pub fn enable_irq(irq_no: usize) {
|
||||||
// Note that the vexriscv "IRQ Mask" register is inverse-logic --
|
// Note that the vexriscv "IRQ Mask" register is inverse-logic --
|
||||||
// that is, setting a bit in the "mask" register unmasks (i.e. enables) it.
|
// that is, setting a bit in the "mask" register unmasks (i.e. enables) it.
|
||||||
if IRQ_ENABLED.load(Ordering::Relaxed) {
|
if IRQ_ENABLED.load(Ordering::Relaxed) {
|
||||||
let existing = sim_read();
|
sim_write(sim_read() | (1 << irq_no));
|
||||||
let new = existing | (1 << irq_no);
|
|
||||||
// println!("Enabling IRQ {} (active): {:08x} -> {:08x}", irq_no, existing, new);
|
|
||||||
sim_write(new);
|
|
||||||
} else {
|
} else {
|
||||||
SIM_BACKING
|
SIM_BACKING
|
||||||
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |existing| {
|
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |existing| {
|
||||||
let new = existing | (1 << irq_no);
|
Some(existing | (1 << irq_no))
|
||||||
// println!("Enabling IRQ {} (masked: {:08x}): {:08x} -> {:08x}", irq_no, sim_read(), existing, new);
|
|
||||||
Some(new)
|
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
@ -73,25 +71,11 @@ pub fn enable_irq(irq_no: usize) {
|
||||||
/// SIM backing instead so that it will be disabled when interrupts are restored.
|
/// SIM backing instead so that it will be disabled when interrupts are restored.
|
||||||
pub fn disable_irq(irq_no: usize) {
|
pub fn disable_irq(irq_no: usize) {
|
||||||
if IRQ_ENABLED.load(Ordering::Relaxed) {
|
if IRQ_ENABLED.load(Ordering::Relaxed) {
|
||||||
let existing = sim_read();
|
sim_write(sim_read() & !(1 << irq_no));
|
||||||
let new = existing & !(1 << irq_no);
|
|
||||||
// println!(
|
|
||||||
// "Disabling IRQ {} (active): {:08x} -> {:08x}",
|
|
||||||
// irq_no, existing, new
|
|
||||||
// );
|
|
||||||
sim_write(new);
|
|
||||||
} else {
|
} else {
|
||||||
SIM_BACKING
|
SIM_BACKING
|
||||||
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |existing| {
|
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |existing| {
|
||||||
let new = existing & !(1 << irq_no);
|
Some(existing & !(1 << irq_no))
|
||||||
println!(
|
|
||||||
"Disabling IRQ {} (masked: {:08x}): {:08x} -> {:08x}",
|
|
||||||
irq_no,
|
|
||||||
sim_read(),
|
|
||||||
existing,
|
|
||||||
new
|
|
||||||
);
|
|
||||||
Some(new)
|
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
@ -197,229 +181,260 @@ pub extern "C" fn trap_handler(
|
||||||
}
|
}
|
||||||
|
|
||||||
let pid = current_pid();
|
let pid = current_pid();
|
||||||
|
let epc = sepc::read();
|
||||||
|
|
||||||
if (sc.bits() == 9) || (sc.bits() == 8) {
|
let ex = RiscvException::from_regs(sc.bits(), epc, stval::read());
|
||||||
// We got here because of an `ecall` instruction. When we return, skip
|
match ex {
|
||||||
// past this instruction. If this is a call such as `SwitchTo`, then we
|
// Syscall
|
||||||
// will want to adjust the return value of the current process prior to
|
RiscvException::CallFromSMode(_epc, _) | RiscvException::CallFromUMode(_epc, _) => {
|
||||||
// performing the switch in order to avoid constantly executing the same
|
// We got here because of an `ecall` instruction, either from User mode (sc==8)
|
||||||
// instruction.
|
// or from Supervisor mode (sc==9). When we return, skip past the `ecall`
|
||||||
let tid = ArchProcess::with_current_mut(|p| {
|
// instruction.
|
||||||
p.current_thread_mut().sepc += 4;
|
// If this is a call such as `SwitchTo`, then we will want to adjust the return
|
||||||
p.current_tid()
|
// value of the current process prior to performing the switch in order to
|
||||||
});
|
// avoid constantly executing the same instruction.
|
||||||
let call = SysCall::from_args(a0, a1, a2, a3, a4, a5, a6, a7).unwrap_or_else(|_| {
|
let tid = ArchProcess::with_current_mut(|p| {
|
||||||
ArchProcess::with_current_mut(|p| unsafe {
|
p.current_thread_mut().sepc += 4;
|
||||||
_xous_syscall_return_result(
|
p.current_tid()
|
||||||
&xous_kernel::Result::Error(xous_kernel::Error::UnhandledSyscall),
|
});
|
||||||
p.current_thread(),
|
let call = SysCall::from_args(a0, a1, a2, a3, a4, a5, a6, a7).unwrap_or_else(|_| {
|
||||||
)
|
ArchProcess::with_current_mut(|p| unsafe {
|
||||||
})
|
_xous_syscall_return_result(
|
||||||
});
|
&xous_kernel::Result::Error(xous_kernel::Error::UnhandledSyscall),
|
||||||
|
p.current_thread(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
let response = crate::syscall::handle(pid, tid, unsafe { PREVIOUS_PAIR.is_some() }, call)
|
let response =
|
||||||
.unwrap_or_else(xous_kernel::Result::Error);
|
crate::syscall::handle(pid, tid, unsafe { PREVIOUS_PAIR.is_some() }, call)
|
||||||
|
.unwrap_or_else(xous_kernel::Result::Error);
|
||||||
|
|
||||||
// println!("Syscall Result: {:?}", response);
|
// println!("Syscall Result: {:?}", response);
|
||||||
ArchProcess::with_current_mut(|p| {
|
ArchProcess::with_current_mut(|p| {
|
||||||
let thread = p.current_thread();
|
let thread = p.current_thread();
|
||||||
// If we're resuming a process that was previously sleeping, restore the
|
// If we're resuming a process that was previously sleeping, restore the
|
||||||
// thread context. Otherwise, keep the thread context the same and pass
|
// thread context. Otherwise, keep the thread context the same and pass
|
||||||
// the return values in 8 argument registers.
|
// the return values in 8 argument registers.
|
||||||
if response == xous_kernel::Result::ResumeProcess {
|
if response == xous_kernel::Result::ResumeProcess {
|
||||||
crate::arch::syscall::resume(current_pid().get() == 1, thread);
|
crate::arch::syscall::resume(current_pid().get() == 1, thread);
|
||||||
} else {
|
} else {
|
||||||
// println!("Returning to address {:08x}", thread.sepc);
|
// println!("Returning to address {:08x}", thread.sepc);
|
||||||
unsafe { _xous_syscall_return_result(&response, thread) };
|
unsafe { _xous_syscall_return_result(&response, thread) };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Hardware interrupt
|
||||||
|
RiscvException::UserExternalInterrupt(_)
|
||||||
|
| RiscvException::SupervisorExternalInterrupt(_) => {
|
||||||
|
let irqs_pending = sip_read() & sim_read();
|
||||||
|
|
||||||
|
// Safe to access globals since interrupts are disabled
|
||||||
|
// when this function runs.
|
||||||
|
unsafe {
|
||||||
|
if PREVIOUS_PAIR.is_none() {
|
||||||
|
let tid = crate::arch::process::current_tid();
|
||||||
|
PREVIOUS_PAIR = Some((pid, tid));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
crate::irq::handle(irqs_pending).expect("Couldn't handle IRQ");
|
||||||
}
|
ArchProcess::with_current_mut(|process| {
|
||||||
|
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let ex = RiscvException::from_regs(sc.bits(), sepc::read(), stval::read());
|
|
||||||
// println!("ex: {:?}", ex);
|
|
||||||
if sc.is_exception() {
|
|
||||||
// See if it's a known exception, such as writing to a demand-paged area
|
// See if it's a known exception, such as writing to a demand-paged area
|
||||||
// or returning from a handler or thread. If so, handle the exception
|
// or returning from a handler or thread. If so, handle the exception
|
||||||
// and return right away.
|
// and return right away.
|
||||||
match ex {
|
RiscvException::StorePageFault(_pc, addr) | RiscvException::LoadPageFault(_pc, addr) => {
|
||||||
RiscvException::StorePageFault(_pc, addr)
|
#[cfg(all(feature = "debug-print", feature = "print-panics"))]
|
||||||
| RiscvException::LoadPageFault(_pc, addr) => {
|
print!(
|
||||||
#[cfg(all(feature = "debug-print", feature = "print-panics"))]
|
"KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x} - ",
|
||||||
print!(
|
pid, ex, _pc, addr
|
||||||
"KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x} - ",
|
);
|
||||||
pid, ex, _pc, addr
|
crate::arch::mem::ensure_page_exists_inner(addr)
|
||||||
);
|
.map(|_new_page| {
|
||||||
crate::arch::mem::ensure_page_exists_inner(addr)
|
#[cfg(all(feature = "debug-print", feature = "print-panics"))]
|
||||||
.map(|_new_page| {
|
klog!("Handing page {:08x} to process", _new_page);
|
||||||
#[cfg(all(feature = "debug-print", feature = "print-panics"))]
|
ArchProcess::with_current_mut(|process| {
|
||||||
klog!("Handing page {:08x} to process", _new_page);
|
crate::arch::syscall::resume(
|
||||||
ArchProcess::with_current_mut(|process| {
|
current_pid().get() == 1,
|
||||||
crate::arch::syscall::resume(
|
process.current_thread(),
|
||||||
current_pid().get() == 1,
|
)
|
||||||
process.current_thread(),
|
});
|
||||||
)
|
})
|
||||||
});
|
.ok(); // If this fails, fall through.
|
||||||
})
|
}
|
||||||
.ok(); // If this fails, fall through.
|
|
||||||
}
|
|
||||||
|
|
||||||
RiscvException::InstructionPageFault(RETURN_FROM_EXCEPTION_HANDLER, _offset) => {
|
RiscvException::InstructionPageFault(RETURN_FROM_EXCEPTION_HANDLER, _offset) => {
|
||||||
// This address indicates the exception handler
|
// This address indicates the exception handler
|
||||||
SystemServices::with_mut(|ss| {
|
SystemServices::with_mut(|ss| {
|
||||||
ss.finish_exception_handler_and_resume(pid)
|
ss.finish_exception_handler_and_resume(pid)
|
||||||
.expect("unable to finish exception handler")
|
.expect("unable to finish exception handler")
|
||||||
});
|
});
|
||||||
|
|
||||||
// Resume the new thread within the same process.
|
// Resume the new thread within the same process.
|
||||||
ArchProcess::with_current_mut(|p| {
|
ArchProcess::with_current_mut(|p| {
|
||||||
// Adjust the program counter by the amount returned by the exception handler
|
// Adjust the program counter by the amount returned by the exception handler
|
||||||
let pc_adjust = a0 as isize;
|
let pc_adjust = a0 as isize;
|
||||||
if pc_adjust < 0 {
|
if pc_adjust < 0 {
|
||||||
p.current_thread_mut().sepc -= pc_adjust.abs() as usize;
|
p.current_thread_mut().sepc -= pc_adjust.abs() as usize;
|
||||||
} else {
|
} else {
|
||||||
p.current_thread_mut().sepc += pc_adjust.abs() as usize;
|
p.current_thread_mut().sepc += pc_adjust.abs() as usize;
|
||||||
}
|
|
||||||
|
|
||||||
crate::arch::syscall::resume(pid.get() == 1, p.current_thread())
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
RiscvException::InstructionPageFault(EXIT_THREAD, _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.
|
crate::arch::syscall::resume(pid.get() == 1, p.current_thread())
|
||||||
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());
|
||||||
|
|
||||||
|
// 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
RiscvException::InstructionPageFault(RETURN_FROM_ISR, _offset) => {
|
// Resume the new thread within the same process.
|
||||||
// If we hit this address, then an ISR has just returned. Since
|
ArchProcess::with_current_mut(|p| {
|
||||||
// we're in an interrupt context, it is safe to access this
|
crate::arch::syscall::resume(current_pid().get() == 1, p.current_thread())
|
||||||
// global variable.
|
});
|
||||||
let (previous_pid, previous_context) = unsafe {
|
}
|
||||||
PREVIOUS_PAIR
|
|
||||||
.take()
|
RiscvException::InstructionPageFault(RETURN_FROM_ISR, _offset) => {
|
||||||
.expect("got RETURN_FROM_ISR with no previous PID")
|
// If we hit this address, then an ISR has just returned. Since
|
||||||
};
|
// we're in an interrupt context, it is safe to access this
|
||||||
// println!(
|
// global variable.
|
||||||
// "ISR: Resuming previous pair of ({}, {})",
|
let (previous_pid, previous_context) = unsafe {
|
||||||
// previous_pid, previous_context
|
PREVIOUS_PAIR
|
||||||
// );
|
.take()
|
||||||
// Switch to the previous process' address space.
|
.expect("got RETURN_FROM_ISR with no previous PID")
|
||||||
|
};
|
||||||
|
// println!(
|
||||||
|
// "ISR: Resuming previous pair of ({}, {})",
|
||||||
|
// previous_pid, previous_context
|
||||||
|
// );
|
||||||
|
// Switch to the previous process' address space.
|
||||||
|
SystemServices::with_mut(|ss| {
|
||||||
|
ss.finish_callback_and_resume(previous_pid, previous_context)
|
||||||
|
.expect("unable to resume previous PID")
|
||||||
|
});
|
||||||
|
|
||||||
|
// Re-enable interrupts now that they're handled
|
||||||
|
enable_all_irqs();
|
||||||
|
|
||||||
|
ArchProcess::with_current_mut(|process| {
|
||||||
|
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gdb-stub")]
|
||||||
|
RiscvException::Breakpoint(_address) => {
|
||||||
|
let insn_lo = crate::arch::mem::peek_memory(epc as *mut u16).unwrap_or(0xffff);
|
||||||
|
let insn_hi = crate::arch::mem::peek_memory((epc + 2) as *mut u16).unwrap_or(0xffff);
|
||||||
|
if (insn_lo & 0xffff == 0x9002) || (insn_hi == 0x0010 && insn_lo == 0x0073) {
|
||||||
|
// Report that the process has stopped
|
||||||
|
let tid = ArchProcess::with_current_mut(|process| process.current_tid());
|
||||||
|
|
||||||
|
// Note that we report the current `epc` here without manipulation --
|
||||||
|
// the debugger will unpatch the opcode and re-issue the instruction.
|
||||||
|
crate::debug::gdb::report_stop(pid, tid, epc);
|
||||||
|
|
||||||
|
// Pause for debugging, which switches to the parent process
|
||||||
SystemServices::with_mut(|ss| {
|
SystemServices::with_mut(|ss| {
|
||||||
ss.finish_callback_and_resume(previous_pid, previous_context)
|
ss.pause_process_for_debug(pid)
|
||||||
.expect("unable to resume previous PID")
|
.expect("couldn't debug current process");
|
||||||
|
crate::syscall::reset_switchto_caller();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Re-enable interrupts now that they're handled
|
// Resume the parent process.
|
||||||
enable_all_irqs();
|
|
||||||
|
|
||||||
ArchProcess::with_current_mut(|process| {
|
ArchProcess::with_current_mut(|process| {
|
||||||
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
|
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
|
||||||
});
|
})
|
||||||
}
|
|
||||||
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
// This exception is not due to something we're aware of. In this case,
|
|
||||||
// determine if there is an exception handler in this particular program
|
|
||||||
// and call that handler if so.
|
|
||||||
if let Some(args) = generate_exception_args(&ex) {
|
|
||||||
if let Some(handler) = SystemServices::with_mut(|ss| ss.begin_exception_handler(pid)) {
|
|
||||||
klog!("Exception handler for process exists ({:x?})", handler);
|
|
||||||
// If this is the sort of exception that may be able to be handled by
|
|
||||||
// the userspace program, generate a list of arguments to pass to
|
|
||||||
// the handler.
|
|
||||||
// Invoke the handler in userspace and exit this exception handler.
|
|
||||||
klog!(
|
|
||||||
"At start of exception, current thread was: {}",
|
|
||||||
SystemServices::with(|ss| ss.get_process(pid).unwrap().current_thread)
|
|
||||||
);
|
|
||||||
ArchProcess::with_current_mut(|process| {
|
|
||||||
crate::arch::syscall::invoke(
|
|
||||||
process.thread_mut(crate::arch::process::EXCEPTION_TID),
|
|
||||||
current_pid().get() == 1,
|
|
||||||
handler.pc,
|
|
||||||
handler.sp,
|
|
||||||
RETURN_FROM_EXCEPTION_HANDLER,
|
|
||||||
&args,
|
|
||||||
);
|
|
||||||
crate::arch::syscall::resume(
|
|
||||||
current_pid().get() == 1,
|
|
||||||
process.thread(crate::arch::process::EXCEPTION_TID),
|
|
||||||
)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let is_kernel_failure = sstatus::read().spp() == sstatus::SPP::Supervisor;
|
_ => {
|
||||||
// The exception was not handled. We should terminate the program here.
|
println!("!!! Unrecognized exception: {:?}", ex);
|
||||||
// For now, let's halt the whole system instead so that it becomes
|
|
||||||
// immediately obvious that we screwed up. On harware this will trigger
|
|
||||||
// a watchdog reset.
|
|
||||||
println!(
|
|
||||||
"{}: CPU Exception on PID {}: {}",
|
|
||||||
if is_kernel_failure {
|
|
||||||
"!!! KERNEL FAILURE !!!"
|
|
||||||
} else {
|
|
||||||
"PROGRAM HALT"
|
|
||||||
},
|
|
||||||
pid,
|
|
||||||
ex
|
|
||||||
);
|
|
||||||
ArchProcess::with_current(|process| {
|
|
||||||
println!("Current thread {}:", process.current_tid());
|
|
||||||
process.print_current_thread();
|
|
||||||
});
|
|
||||||
|
|
||||||
// If this is a failure in the kernel, go into an infinite loop
|
|
||||||
MemoryMapping::current().print_map();
|
|
||||||
if is_kernel_failure {
|
|
||||||
#[allow(clippy::empty_loop)]
|
|
||||||
loop {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's not a failure in the kernel, terminate or debug the current process.
|
|
||||||
SystemServices::with_mut(|ss| {
|
|
||||||
#[cfg(feature = "gdb-stub")]
|
|
||||||
{
|
|
||||||
ss.pause_process_for_debug(pid)
|
|
||||||
.expect("couldn't debug current process");
|
|
||||||
crate::debug::gdb::report_terminated(pid);
|
|
||||||
println!("Program suspended. You may inspect it using gdb.");
|
|
||||||
}
|
|
||||||
#[cfg(not(feature = "gdb-stub"))]
|
|
||||||
ss.terminate_process(pid)
|
|
||||||
.expect("couldn't terminate current process");
|
|
||||||
crate::syscall::reset_switchto_caller();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Resume the parent process.
|
|
||||||
ArchProcess::with_current_mut(|process| {
|
|
||||||
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
let irqs_pending = sip::read();
|
|
||||||
|
|
||||||
// Safe to access globals since interrupts are disabled
|
|
||||||
// when this function runs.
|
|
||||||
unsafe {
|
|
||||||
if PREVIOUS_PAIR.is_none() {
|
|
||||||
let tid = crate::arch::process::current_tid();
|
|
||||||
PREVIOUS_PAIR = Some((pid, tid));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
crate::irq::handle(irqs_pending).expect("Couldn't handle IRQ");
|
|
||||||
ArchProcess::with_current_mut(|process| {
|
|
||||||
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This exception is not due to something we're aware of. In this case,
|
||||||
|
// determine if there is an exception handler in this particular program
|
||||||
|
// and call that handler if so.
|
||||||
|
if let Some(args) = generate_exception_args(&ex) {
|
||||||
|
if let Some(handler) = SystemServices::with_mut(|ss| ss.begin_exception_handler(pid)) {
|
||||||
|
klog!("Exception handler for process exists ({:x?})", handler);
|
||||||
|
// If this is the sort of exception that may be able to be handled by
|
||||||
|
// the userspace program, generate a list of arguments to pass to
|
||||||
|
// the handler.
|
||||||
|
// Invoke the handler in userspace and exit this exception handler.
|
||||||
|
klog!(
|
||||||
|
"At start of exception, current thread was: {}",
|
||||||
|
SystemServices::with(|ss| ss.get_process(pid).unwrap().current_thread)
|
||||||
|
);
|
||||||
|
ArchProcess::with_current_mut(|process| {
|
||||||
|
crate::arch::syscall::invoke(
|
||||||
|
process.thread_mut(crate::arch::process::EXCEPTION_TID),
|
||||||
|
current_pid().get() == 1,
|
||||||
|
handler.pc,
|
||||||
|
handler.sp,
|
||||||
|
RETURN_FROM_EXCEPTION_HANDLER,
|
||||||
|
&args,
|
||||||
|
);
|
||||||
|
crate::arch::syscall::resume(
|
||||||
|
current_pid().get() == 1,
|
||||||
|
process.thread(crate::arch::process::EXCEPTION_TID),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let is_kernel_failure = sstatus::read().spp() == sstatus::SPP::Supervisor;
|
||||||
|
// The exception was not handled. We should terminate the program here.
|
||||||
|
// For now, let's halt the whole system instead so that it becomes
|
||||||
|
// immediately obvious that we screwed up. On harware this will trigger
|
||||||
|
// a watchdog reset.
|
||||||
|
println!(
|
||||||
|
"{}: CPU Exception on PID {}: {}",
|
||||||
|
if is_kernel_failure {
|
||||||
|
"!!! KERNEL FAILURE !!!"
|
||||||
|
} else {
|
||||||
|
"PROGRAM HALT"
|
||||||
|
},
|
||||||
|
pid,
|
||||||
|
ex
|
||||||
|
);
|
||||||
|
ArchProcess::with_current(|process| {
|
||||||
|
println!("Current thread {}:", process.current_tid());
|
||||||
|
process.print_current_thread();
|
||||||
|
});
|
||||||
|
|
||||||
|
// If this is a failure in the kernel, go into an infinite loop
|
||||||
|
MemoryMapping::current().print_map();
|
||||||
|
if is_kernel_failure {
|
||||||
|
#[allow(clippy::empty_loop)]
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it's not a failure in the kernel, terminate or debug the current process.
|
||||||
|
SystemServices::with_mut(|ss| {
|
||||||
|
#[cfg(feature = "gdb-stub")]
|
||||||
|
{
|
||||||
|
ss.pause_process_for_debug(pid)
|
||||||
|
.expect("couldn't debug current process");
|
||||||
|
crate::debug::gdb::report_terminated(pid);
|
||||||
|
println!("Program suspended. You may inspect it using gdb.");
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "gdb-stub"))]
|
||||||
|
ss.terminate_process(pid)
|
||||||
|
.expect("couldn't terminate current process");
|
||||||
|
crate::syscall::reset_switchto_caller();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Resume the parent process.
|
||||||
|
ArchProcess::with_current_mut(|process| {
|
||||||
|
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue