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:
Sean Cross 2023-04-06 09:29:48 +08:00
parent 5c52fbd522
commit 3469c292ec
2 changed files with 251 additions and 236 deletions

View file

@ -65,10 +65,10 @@ pub enum RiscvException {
StoreAccessFault(usize /* epc */, usize /* target address */),
/// 0 8
CallFromUMode(usize /* epc */),
CallFromUMode(usize /* epc */, usize /* ??? */),
/// 0 9
CallFromSMode(usize /* epc */),
CallFromSMode(usize /* epc */, usize /* ??? */),
// [reserved]
/// 0 11
@ -148,8 +148,8 @@ impl fmt::Display for RiscvException {
StoreAccessFault(epc, tval) => {
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),
CallFromSMode(epc) => write!(f, "Call from Supervisor mode at 0x{:08x}", epc),
CallFromUMode(epc, tval) => write!(f, "Call from User mode at 0x{:08x} (???: 0x{:08x})", epc, tval),
CallFromSMode(epc, tval) => write!(f, "Call from Supervisor mode at 0x{:08x} (???: 0x{:08x})", epc, tval),
// --reserved--
CallFromMMode(epc) => write!(f, "Call from Machine mode at 0x{:08x}", epc),
InstructionPageFault(epc, tval) => write!(
@ -203,8 +203,8 @@ impl RiscvException {
5 => LoadAccessFault(epc, tval),
6 => StoreAddressMisaligned(epc, tval),
7 => StoreAccessFault(epc, tval),
8 => CallFromUMode(epc),
9 => CallFromSMode(epc),
8 => CallFromUMode(epc, tval),
9 => CallFromSMode(epc, tval),
// --reserved--
11 => CallFromMMode(epc),
12 => InstructionPageFault(epc, tval),

View file

@ -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::services::SystemServices;
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};
extern "Rust" {
@ -23,19 +23,22 @@ static IRQ_ENABLED: AtomicBool = AtomicBool::new(true);
fn sim_read() -> 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
}
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
pub fn disable_all_irqs() {
if !IRQ_ENABLED.load(Ordering::Relaxed) {
panic!("IRQs were disabled twice!");
}
SIM_BACKING.store(sim_read(), Ordering::Relaxed);
IRQ_ENABLED.store(false, Ordering::Relaxed);
sim_write(0x0);
@ -54,16 +57,11 @@ pub fn enable_irq(irq_no: usize) {
// Note that the vexriscv "IRQ Mask" register is inverse-logic --
// that is, setting a bit in the "mask" register unmasks (i.e. enables) it.
if IRQ_ENABLED.load(Ordering::Relaxed) {
let existing = sim_read();
let new = existing | (1 << irq_no);
// println!("Enabling IRQ {} (active): {:08x} -> {:08x}", irq_no, existing, new);
sim_write(new);
sim_write(sim_read() | (1 << irq_no));
} else {
SIM_BACKING
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |existing| {
let new = existing | (1 << irq_no);
// println!("Enabling IRQ {} (masked: {:08x}): {:08x} -> {:08x}", irq_no, sim_read(), existing, new);
Some(new)
Some(existing | (1 << irq_no))
})
.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.
pub fn disable_irq(irq_no: usize) {
if IRQ_ENABLED.load(Ordering::Relaxed) {
let existing = sim_read();
let new = existing & !(1 << irq_no);
// println!(
// "Disabling IRQ {} (active): {:08x} -> {:08x}",
// irq_no, existing, new
// );
sim_write(new);
sim_write(sim_read() & !(1 << irq_no));
} else {
SIM_BACKING
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |existing| {
let new = existing & !(1 << irq_no);
println!(
"Disabling IRQ {} (masked: {:08x}): {:08x} -> {:08x}",
irq_no,
sim_read(),
existing,
new
);
Some(new)
Some(existing & !(1 << irq_no))
})
.ok();
}
@ -197,229 +181,260 @@ pub extern "C" fn trap_handler(
}
let pid = current_pid();
let epc = sepc::read();
if (sc.bits() == 9) || (sc.bits() == 8) {
// We got here because of an `ecall` instruction. When we return, skip
// past this instruction. If this is a call such as `SwitchTo`, then we
// will want to adjust the return value of the current process prior to
// performing the switch in order to avoid constantly executing the same
// instruction.
let tid = ArchProcess::with_current_mut(|p| {
p.current_thread_mut().sepc += 4;
p.current_tid()
});
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 ex = RiscvException::from_regs(sc.bits(), epc, stval::read());
match ex {
// Syscall
RiscvException::CallFromSMode(_epc, _) | RiscvException::CallFromUMode(_epc, _) => {
// We got here because of an `ecall` instruction, either from User mode (sc==8)
// or from Supervisor mode (sc==9). When we return, skip past the `ecall`
// instruction.
// If this is a call such as `SwitchTo`, then we will want to adjust the return
// value of the current process prior to performing the switch in order to
// avoid constantly executing the same instruction.
let tid = ArchProcess::with_current_mut(|p| {
p.current_thread_mut().sepc += 4;
p.current_tid()
});
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)
.unwrap_or_else(xous_kernel::Result::Error);
let response =
crate::syscall::handle(pid, tid, unsafe { PREVIOUS_PAIR.is_some() }, call)
.unwrap_or_else(xous_kernel::Result::Error);
// println!("Syscall Result: {:?}", response);
ArchProcess::with_current_mut(|p| {
let thread = p.current_thread();
// If we're resuming a process that was previously sleeping, restore the
// thread context. Otherwise, keep the thread context the same and pass
// the return values in 8 argument registers.
if response == xous_kernel::Result::ResumeProcess {
crate::arch::syscall::resume(current_pid().get() == 1, thread);
} else {
// println!("Returning to address {:08x}", thread.sepc);
unsafe { _xous_syscall_return_result(&response, thread) };
// println!("Syscall Result: {:?}", response);
ArchProcess::with_current_mut(|p| {
let thread = p.current_thread();
// If we're resuming a process that was previously sleeping, restore the
// thread context. Otherwise, keep the thread context the same and pass
// the return values in 8 argument registers.
if response == xous_kernel::Result::ResumeProcess {
crate::arch::syscall::resume(current_pid().get() == 1, thread);
} else {
// println!("Returning to address {:08x}", thread.sepc);
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
// or returning from a handler or thread. If so, handle the exception
// and return right away.
match ex {
RiscvException::StorePageFault(_pc, addr)
| RiscvException::LoadPageFault(_pc, addr) => {
#[cfg(all(feature = "debug-print", feature = "print-panics"))]
print!(
"KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x} - ",
pid, ex, _pc, addr
);
crate::arch::mem::ensure_page_exists_inner(addr)
.map(|_new_page| {
#[cfg(all(feature = "debug-print", feature = "print-panics"))]
klog!("Handing page {:08x} to process", _new_page);
ArchProcess::with_current_mut(|process| {
crate::arch::syscall::resume(
current_pid().get() == 1,
process.current_thread(),
)
});
})
.ok(); // If this fails, fall through.
}
RiscvException::StorePageFault(_pc, addr) | RiscvException::LoadPageFault(_pc, addr) => {
#[cfg(all(feature = "debug-print", feature = "print-panics"))]
print!(
"KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x} - ",
pid, ex, _pc, addr
);
crate::arch::mem::ensure_page_exists_inner(addr)
.map(|_new_page| {
#[cfg(all(feature = "debug-print", feature = "print-panics"))]
klog!("Handing page {:08x} to process", _new_page);
ArchProcess::with_current_mut(|process| {
crate::arch::syscall::resume(
current_pid().get() == 1,
process.current_thread(),
)
});
})
.ok(); // If this fails, fall through.
}
RiscvException::InstructionPageFault(RETURN_FROM_EXCEPTION_HANDLER, _offset) => {
// This address indicates the exception handler
SystemServices::with_mut(|ss| {
ss.finish_exception_handler_and_resume(pid)
.expect("unable to finish exception handler")
});
RiscvException::InstructionPageFault(RETURN_FROM_EXCEPTION_HANDLER, _offset) => {
// This address indicates the exception handler
SystemServices::with_mut(|ss| {
ss.finish_exception_handler_and_resume(pid)
.expect("unable to finish exception handler")
});
// Resume the new thread within the same process.
ArchProcess::with_current_mut(|p| {
// Adjust the program counter by the amount returned by the exception handler
let pc_adjust = a0 as isize;
if pc_adjust < 0 {
p.current_thread_mut().sepc -= pc_adjust.abs() as usize;
} else {
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.
ArchProcess::with_current_mut(|p| {
// Adjust the program counter by the amount returned by the exception handler
let pc_adjust = a0 as isize;
if pc_adjust < 0 {
p.current_thread_mut().sepc -= pc_adjust.abs() as usize;
} else {
p.current_thread_mut().sepc += pc_adjust.abs() as usize;
}
// Resume the new thread within the same process.
ArchProcess::with_current_mut(|p| {
crate::arch::syscall::resume(current_pid().get() == 1, p.current_thread())
});
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();
}
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
// global variable.
let (previous_pid, previous_context) = unsafe {
PREVIOUS_PAIR
.take()
.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.
// 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(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
// global variable.
let (previous_pid, previous_context) = unsafe {
PREVIOUS_PAIR
.take()
.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| {
ss.finish_callback_and_resume(previous_pid, previous_context)
.expect("unable to resume previous PID")
ss.pause_process_for_debug(pid)
.expect("couldn't debug current process");
crate::syscall::reset_switchto_caller();
});
// Re-enable interrupts now that they're handled
enable_all_irqs();
// Resume the parent process.
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 {}
_ => {
println!("!!! Unrecognized exception: {:?}", ex);
}
// 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())
})
}