mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +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 */),
|
||||
|
||||
/// 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),
|
||||
|
|
|
|||
|
|
@ -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,13 +181,18 @@ 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
|
||||
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()
|
||||
|
|
@ -217,7 +206,8 @@ pub extern "C" fn trap_handler(
|
|||
})
|
||||
});
|
||||
|
||||
let response = crate::syscall::handle(pid, tid, unsafe { PREVIOUS_PAIR.is_some() }, call)
|
||||
let response =
|
||||
crate::syscall::handle(pid, tid, unsafe { PREVIOUS_PAIR.is_some() }, call)
|
||||
.unwrap_or_else(xous_kernel::Result::Error);
|
||||
|
||||
// println!("Syscall Result: {:?}", response);
|
||||
|
|
@ -234,16 +224,29 @@ pub extern "C" fn trap_handler(
|
|||
}
|
||||
});
|
||||
}
|
||||
// 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) => {
|
||||
RiscvException::StorePageFault(_pc, addr) | RiscvException::LoadPageFault(_pc, addr) => {
|
||||
#[cfg(all(feature = "debug-print", feature = "print-panics"))]
|
||||
print!(
|
||||
"KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x} - ",
|
||||
|
|
@ -326,7 +329,35 @@ pub extern "C" fn trap_handler(
|
|||
});
|
||||
}
|
||||
|
||||
_ => (),
|
||||
#[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.pause_process_for_debug(pid)
|
||||
.expect("couldn't debug 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())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
_ => {
|
||||
println!("!!! Unrecognized exception: {:?}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// This exception is not due to something we're aware of. In this case,
|
||||
|
|
@ -406,20 +437,4 @@ pub extern "C" fn trap_handler(
|
|||
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())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue