use crate::{println, serial_println};
use core::arch::global_asm;
use riscv::register::{
scause::{self, Exception, Interrupt, Trap},
stval, stvec,
};
const TRAP_STACK_SIZE: usize = 4096;
#[repr(align(16))]
struct TrapStack([u8; TRAP_STACK_SIZE]);
static mut TRAP_STACK: TrapStack = TrapStack([0; TRAP_STACK_SIZE]);
pub fn trap_stack_top() -> usize {
unsafe { (&raw const TRAP_STACK.0 as *const u8 as usize) + TRAP_STACK_SIZE }
}
#[repr(C)]
pub struct TrapFrame {
pub x: [usize; 32], pub sepc: usize,
pub sstatus: usize,
pub satp: usize,
}
impl TrapFrame {
pub fn save_to(&self, ctx: &mut crate::process::ProcessContext) {
ctx.x.copy_from_slice(&self.x[1..]); ctx.sepc = self.sepc;
ctx.sstatus = self.sstatus;
ctx.satp = self.satp;
}
pub fn load_from(&mut self, ctx: &crate::process::ProcessContext) {
self.x[0] = 0;
self.x[1..].copy_from_slice(&ctx.x);
self.sepc = ctx.sepc;
self.sstatus = ctx.sstatus;
self.satp = ctx.satp;
}
}
global_asm!(
".section .text.trap, \"ax\"",
".align 2",
".globl trap_entry",
"trap_entry:",
" csrrw sp, sscratch, sp",
" addi sp, sp, -280",
" sd zero, 0(sp)",
" sd x1, 8(sp)",
" sd x3, 24(sp)",
" sd x4, 32(sp)",
" sd x5, 40(sp)", " sd x6, 48(sp)", " sd x7, 56(sp)",
" sd x8, 64(sp)",
" sd x9, 72(sp)",
" sd x10, 80(sp)",
" sd x11, 88(sp)",
" sd x12, 96(sp)",
" sd x13, 104(sp)",
" sd x14, 112(sp)",
" sd x15, 120(sp)",
" sd x16, 128(sp)",
" sd x17, 136(sp)",
" sd x18, 144(sp)",
" sd x19, 152(sp)",
" sd x20, 160(sp)",
" sd x21, 168(sp)",
" sd x22, 176(sp)",
" sd x23, 184(sp)",
" sd x24, 192(sp)",
" sd x25, 200(sp)",
" sd x26, 208(sp)",
" sd x27, 216(sp)",
" sd x28, 224(sp)",
" sd x29, 232(sp)",
" sd x30, 240(sp)",
" sd x31, 248(sp)",
" csrr t0, sscratch",
" sd t0, 16(sp)",
" csrw sscratch, sp",
" csrr t0, sepc",
" sd t0, 256(sp)",
" csrr t0, sstatus",
" sd t0, 264(sp)",
" csrr t0, satp",
" sd t0, 272(sp)",
" mv a0, sp",
" call rust_trap_handler",
".globl trap_return",
"trap_return:",
" addi t0, sp, 280",
" csrw sscratch, t0",
" ld t0, 272(sp)",
" csrw satp, t0",
" sfence.vma",
" ld t0, 264(sp)",
" csrw sstatus, t0",
" ld t0, 256(sp)",
" csrw sepc, t0",
" mv t6, sp",
" ld x1, 8(t6)",
" ld x3, 24(t6)",
" ld x4, 32(t6)",
" ld x5, 40(t6)",
" ld x6, 48(t6)",
" ld x7, 56(t6)",
" ld x8, 64(t6)",
" ld x9, 72(t6)",
" ld x10, 80(t6)",
" ld x11, 88(t6)",
" ld x12, 96(t6)",
" ld x13, 104(t6)",
" ld x14, 112(t6)",
" ld x15, 120(t6)",
" ld x16, 128(t6)",
" ld x17, 136(t6)",
" ld x18, 144(t6)",
" ld x19, 152(t6)",
" ld x20, 160(t6)",
" ld x21, 168(t6)",
" ld x22, 176(t6)",
" ld x23, 184(t6)",
" ld x24, 192(t6)",
" ld x25, 200(t6)",
" ld x26, 208(t6)",
" ld x27, 216(t6)",
" ld x28, 224(t6)",
" ld x29, 232(t6)",
" ld x30, 240(t6)",
" ld x2, 16(t6)",
" ld x31, 248(t6)",
" sret",
);
extern "C" {
fn trap_entry();
}
pub fn init_idt() {
unsafe {
core::arch::asm!("csrw sscratch, {}", in(reg) trap_stack_top());
stvec::write(trap_entry as *const () as usize, stvec::TrapMode::Direct);
riscv::register::sstatus::set_sum();
}
serial_println!("[INTERRUPT] Trap vector initialized");
unsafe {
riscv::register::sie::set_stimer();
riscv::register::sie::set_sext();
}
set_next_timer();
serial_println!("[INTERRUPT] Timer + External interrupt sources configured");
}
#[no_mangle]
pub extern "C" fn rust_trap_handler(tf: &mut TrapFrame) {
let cause = scause::read();
let stval_val = stval::read();
match cause.cause() {
Trap::Interrupt(interrupt) => match interrupt {
Interrupt::SupervisorTimer => {
timer_interrupt_handler(tf);
}
Interrupt::SupervisorExternal => {
external_interrupt_handler();
}
Interrupt::SupervisorSoft => {
software_interrupt_handler();
}
_ => {
serial_println!(
"Unhandled interrupt! scause={:?} sepc={:#x} stval={:#x}",
cause.cause(),
tf.sepc,
stval_val
);
crate::hlt_loop();
}
},
Trap::Exception(exception) => match exception {
Exception::Breakpoint => {
breakpoint_handler(tf);
}
Exception::LoadPageFault
| Exception::StorePageFault
| Exception::InstructionPageFault => {
page_fault_handler(cause.cause(), stval_val, tf.sepc);
}
Exception::IllegalInstruction => {
illegal_instruction_handler(tf.sepc, stval_val);
}
Exception::UserEnvCall => {
syscall_handler(tf);
}
_ => {
serial_println!(
"Unhandled exception! scause={:?} sepc={:#x} stval={:#x}",
cause.cause(),
tf.sepc,
stval_val
);
crate::hlt_loop();
}
},
}
}
fn timer_interrupt_handler(tf: &mut TrapFrame) {
crate::task::keyboard::poll_keyboard();
set_next_timer();
crate::process::scheduler_tick(tf);
}
fn external_interrupt_handler() {
crate::block::virtio_blk::try_handle_interrupt();
}
fn software_interrupt_handler() {
serial_println!("[INTERRUPT] Software interrupt received");
}
fn breakpoint_handler(tf: &mut TrapFrame) {
let sepc_val = tf.sepc;
serial_println!("[EXCEPTION] Breakpoint at {:#x}", sepc_val);
println!("EXCEPTION: BREAKPOINT at {:#x}", sepc_val);
tf.sepc = sepc_val.wrapping_add(2);
}
fn page_fault_handler(cause: Trap, stval_val: usize, sepc_val: usize) {
serial_println!(
"[EXCEPTION] Page Fault\n Type: {:?}\n Address: {:#x}\n PC: {:#x}",
cause,
stval_val,
sepc_val
);
println!("EXCEPTION: PAGE FAULT");
println!("Accessed Address: {:#x}", stval_val);
println!("Exception PC: {:#x}", sepc_val);
println!("Fault Type: {:?}", cause);
crate::hlt_loop();
}
fn illegal_instruction_handler(sepc_val: usize, stval_val: usize) {
panic!(
"EXCEPTION: ILLEGAL INSTRUCTION\nPC: {:#x}\nInstruction: {:#x}",
sepc_val, stval_val
);
}
fn syscall_handler(tf: &mut TrapFrame) {
crate::syscall::syscall_trap_dispatcher(tf);
}
pub fn without_interrupts<F, R>(f: F) -> R
where
F: FnOnce() -> R,
{
use riscv::register::sstatus;
let sie = sstatus::read().sie();
if sie {
unsafe {
riscv::register::sstatus::clear_sie();
}
}
let ret = f();
if sie {
unsafe {
riscv::register::sstatus::set_sie();
}
}
ret
}
pub fn enable_interrupts() {
unsafe {
riscv::register::sstatus::set_sie();
}
}
pub fn disable_interrupts() {
unsafe {
riscv::register::sstatus::clear_sie();
}
}
fn set_next_timer() {
const TIMER_INTERVAL: u64 = 1_000_000; let time = riscv::register::time::read64();
sbi_set_timer(time + TIMER_INTERVAL);
}
fn sbi_set_timer(stime_value: u64) {
#[cfg(target_arch = "riscv64")]
unsafe {
core::arch::asm!(
"mv a0, {0}",
"li a7, 0",
"ecall",
in(reg) stime_value,
lateout("a0") _,
lateout("a1") _,
options(nostack)
);
}
#[cfg(not(target_arch = "riscv64"))]
{
let _ = stime_value;
}
}
#[cfg(test)]
#[test_case]
fn test_breakpoint_exception() {
use crate::serial_println;
serial_println!("[TEST] test_breakpoint_exception...");
unsafe {
core::arch::asm!("ebreak");
}
serial_println!("[TEST] Breakpoint handled successfully");
}