diff --git a/kernel/src/arch/riscv/irq.rs b/kernel/src/arch/riscv/irq.rs index e9f4f5ee8..8af51eb92 100644 --- a/kernel/src/arch/riscv/irq.rs +++ b/kernel/src/arch/riscv/irq.rs @@ -187,6 +187,11 @@ pub extern "C" fn trap_handler( let epc = sepc::read(); let ex = RiscvException::from_regs(sc.bits(), epc, stval::read()); + { + let pid = current_pid(); + let ex = RiscvException::from_regs(sc.bits(), sepc::read(), stval::read()); + println!("IRQ -- KERNEL({}): RISC-V fault: {}", pid, ex); + } match ex { // Syscall RiscvException::CallFromSMode(_epc, _) | RiscvException::CallFromUMode(_epc, _) => { @@ -250,7 +255,7 @@ pub extern "C" fn trap_handler( // and return right away. 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); + println!("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"))] @@ -361,6 +366,27 @@ pub extern "C" fn trap_handler( } } + // Handle faulted instruction pages, because we can now actually have instruction pages that are + // swapped out. + + // TODO: this probably needs custom handling. + // TODO: check the bounds of the address, it should be in the expected area for code. If outside, + // fault. This is just to reduce potential attack surface. + #[cfg(feature = "swap")] + RiscvException::InstructionPageFault(_pc, addr) => { + #[cfg(all(feature = "debug-print", feature = "print-panics"))] + println!("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. + } + #[cfg(feature = "gdb-stub")] RiscvException::Breakpoint(_address) => { let insn_lo = crate::arch::mem::peek_memory(epc as *mut u16).unwrap_or(0xffff); diff --git a/kernel/src/arch/riscv/syscall.rs b/kernel/src/arch/riscv/syscall.rs index acc646ec6..1265d4582 100644 --- a/kernel/src/arch/riscv/syscall.rs +++ b/kernel/src/arch/riscv/syscall.rs @@ -34,11 +34,11 @@ pub fn resume(supervisor: bool, thread: &Thread) -> ! { // Return to the appropriate CPU mode set_supervisor(supervisor); - // println!( - // "Switching to PID {}, SP: {:08x}, PC: {:08x}", - // crate::arch::current_pid(), - // Thread.registers[1], - // Thread.sepc, - // ); + println!( + "Switching to PID {}, SP: {:08x}, PC: {:08x}", + crate::arch::current_pid(), + thread.registers[1], + thread.sepc, + ); unsafe { _xous_resume_context(thread.registers.as_ptr()) }; } diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index 31f2b1ec7..819feca6b 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -244,7 +244,7 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { // TODO: patch kernel arguments with Swap values...? } - if VVDBG { + if VVDBG || SDBG { println!("PID1 pagetables:"); #[cfg(feature = "atsama5d27")] debug::print_pagetable(cfg.processes[0].ttbr0);