Skip to main content

os/
interrupts.rs

1use crate::{println, serial_println};
2use core::arch::global_asm;
3use riscv::register::{
4    scause::{self, Exception, Interrupt, Trap},
5    stval, stvec,
6};
7
8const TRAP_STACK_SIZE: usize = 4096;
9
10#[repr(align(16))]
11struct TrapStack([u8; TRAP_STACK_SIZE]);
12
13static mut TRAP_STACK: TrapStack = TrapStack([0; TRAP_STACK_SIZE]);
14
15pub fn trap_stack_top() -> usize {
16    unsafe { (&raw const TRAP_STACK.0 as *const u8 as usize) + TRAP_STACK_SIZE }
17}
18
19// ── Trap frame — saved register state at trap entry ──────────────
20
21#[repr(C)]
22pub struct TrapFrame {
23    pub x: [usize; 32], // x0-x31  (x0 always 0)
24    pub sepc: usize,
25    pub sstatus: usize,
26    pub satp: usize,
27}
28
29impl TrapFrame {
30    /// Save trap frame state into a ProcessContext.
31    /// Called before a context switch to preserve the old task's state.
32    pub fn save_to(&self, ctx: &mut crate::process::ProcessContext) {
33        ctx.x.copy_from_slice(&self.x[1..]); // skip x0
34        ctx.sepc = self.sepc;
35        ctx.sstatus = self.sstatus;
36        ctx.satp = self.satp;
37    }
38
39    /// Load trap frame state from a ProcessContext.
40    /// Called after a context switch to resume the new task.
41    pub fn load_from(&mut self, ctx: &crate::process::ProcessContext) {
42        self.x[0] = 0;
43        self.x[1..].copy_from_slice(&ctx.x);
44        self.sepc = ctx.sepc;
45        self.sstatus = ctx.sstatus;
46        self.satp = ctx.satp;
47    }
48}
49
50// ── Assembly trap entry ──────────────────────────────────────────
51
52global_asm!(
53    ".section .text.trap, \"ax\"",
54    ".align 2",
55    ".globl trap_entry",
56    "trap_entry:",
57    // Swap sp with sscratch: sp ← kernel_sp, sscratch ← old_sp
58    "   csrrw sp, sscratch, sp",
59    // Allocate TrapFrame on kernel stack (32 regs + sepc + sstatus + satp = 35 * 8 = 280)
60    "   addi sp, sp, -280",
61    // x0 is hardwired to 0 — leave offset 0 as zero
62    "   sd zero, 0(sp)",
63    // Save GPRs x1-x31.  x5 (t0) and x6 (t1) must be saved BEFORE
64    // they are used as scratch below.
65    "   sd x1, 8(sp)",
66    "   sd x3, 24(sp)",
67    "   sd x4, 32(sp)",
68    "   sd x5, 40(sp)", // t0 — saved before use as scratch
69    "   sd x6, 48(sp)", // t1 — saved before use as scratch
70    "   sd x7, 56(sp)",
71    "   sd x8, 64(sp)",
72    "   sd x9, 72(sp)",
73    "   sd x10, 80(sp)",
74    "   sd x11, 88(sp)",
75    "   sd x12, 96(sp)",
76    "   sd x13, 104(sp)",
77    "   sd x14, 112(sp)",
78    "   sd x15, 120(sp)",
79    "   sd x16, 128(sp)",
80    "   sd x17, 136(sp)",
81    "   sd x18, 144(sp)",
82    "   sd x19, 152(sp)",
83    "   sd x20, 160(sp)",
84    "   sd x21, 168(sp)",
85    "   sd x22, 176(sp)",
86    "   sd x23, 184(sp)",
87    "   sd x24, 192(sp)",
88    "   sd x25, 200(sp)",
89    "   sd x26, 208(sp)",
90    "   sd x27, 216(sp)",
91    "   sd x28, 224(sp)",
92    "   sd x29, 232(sp)",
93    "   sd x30, 240(sp)",
94    "   sd x31, 248(sp)",
95    // Now safely use t0/t1 as scratch to save x2 (old sp from sscratch)
96    "   csrr t0, sscratch",
97    "   sd t0, 16(sp)",
98    // While handling this trap, sscratch points at the next free slot on
99    // the trap stack. A nested trap then allocates below this frame instead
100    // of falling back to the interrupted task stack.
101    "   csrw sscratch, sp",
102    // Save CSRs
103    "   csrr t0, sepc",
104    "   sd t0, 256(sp)",
105    "   csrr t0, sstatus",
106    "   sd t0, 264(sp)",
107    "   csrr t0, satp",
108    "   sd t0, 272(sp)",
109    // Call Rust handler: rust_trap_handler(&mut TrapFrame)
110    "   mv a0, sp",
111    "   call rust_trap_handler",
112    // ── Restore path ─────────────────────────────────────────────
113    ".globl trap_return",
114    "trap_return:",
115    // Save the next free trap-stack slot to sscratch.
116    "   addi t0, sp, 280",
117    "   csrw sscratch, t0",
118    // Restore satp + sfence.vma
119    "   ld t0, 272(sp)",
120    "   csrw satp, t0",
121    "   sfence.vma",
122    // Restore sstatus
123    "   ld t0, 264(sp)",
124    "   csrw sstatus, t0",
125    // Restore sepc
126    "   ld t0, 256(sp)",
127    "   csrw sepc, t0",
128    // Restore GPRs using t6 as base (sp/x2 is about to change).
129    // x2 MUST be restored LAST because subsequent loads would use
130    // the new sp value as their base, not the trap frame.
131    "   mv t6, sp",
132    "   ld x1, 8(t6)",
133    "   ld x3, 24(t6)",
134    "   ld x4, 32(t6)",
135    "   ld x5, 40(t6)",
136    "   ld x6, 48(t6)",
137    "   ld x7, 56(t6)",
138    "   ld x8, 64(t6)",
139    "   ld x9, 72(t6)",
140    "   ld x10, 80(t6)",
141    "   ld x11, 88(t6)",
142    "   ld x12, 96(t6)",
143    "   ld x13, 104(t6)",
144    "   ld x14, 112(t6)",
145    "   ld x15, 120(t6)",
146    "   ld x16, 128(t6)",
147    "   ld x17, 136(t6)",
148    "   ld x18, 144(t6)",
149    "   ld x19, 152(t6)",
150    "   ld x20, 160(t6)",
151    "   ld x21, 168(t6)",
152    "   ld x22, 176(t6)",
153    "   ld x23, 184(t6)",
154    "   ld x24, 192(t6)",
155    "   ld x25, 200(t6)",
156    "   ld x26, 208(t6)",
157    "   ld x27, 216(t6)",
158    "   ld x28, 224(t6)",
159    "   ld x29, 232(t6)",
160    "   ld x30, 240(t6)",
161    // Restore x2/sp — need to do this BEFORE x31 because x31 IS t6
162    // and loading it would clobber the base register
163    "   ld x2, 16(t6)",
164    // Restore x31/t6 LAST — nothing uses t6 as base after this
165    "   ld x31, 248(t6)",
166    "   sret",
167);
168
169extern "C" {
170    fn trap_entry();
171}
172
173// ── Initialisation ───────────────────────────────────────────────
174
175pub fn init_idt() {
176    unsafe {
177        // Use a dedicated trap stack so kernel-mode traps do not overwrite
178        // the currently executing stack frame.
179        core::arch::asm!("csrw sscratch, {}", in(reg) trap_stack_top());
180        // Point stvec to our manual trap entry
181        stvec::write(trap_entry as *const () as usize, stvec::TrapMode::Direct);
182
183        // Allow S-mode to access U-flagged pages (syscall buffers)
184        riscv::register::sstatus::set_sum();
185    }
186
187    serial_println!("[INTERRUPT] Trap vector initialized");
188
189    unsafe {
190        riscv::register::sie::set_stimer();
191        riscv::register::sie::set_sext();
192    }
193
194    set_next_timer();
195
196    serial_println!("[INTERRUPT] Timer + External interrupt sources configured");
197}
198
199// ── Main Rust trap handler ──────────────────────────────────────
200
201#[no_mangle]
202pub extern "C" fn rust_trap_handler(tf: &mut TrapFrame) {
203    let cause = scause::read();
204    let stval_val = stval::read();
205
206    match cause.cause() {
207        Trap::Interrupt(interrupt) => match interrupt {
208            Interrupt::SupervisorTimer => {
209                timer_interrupt_handler(tf);
210            }
211            Interrupt::SupervisorExternal => {
212                external_interrupt_handler();
213            }
214            Interrupt::SupervisorSoft => {
215                software_interrupt_handler();
216            }
217            _ => {
218                serial_println!(
219                    "Unhandled interrupt! scause={:?} sepc={:#x} stval={:#x}",
220                    cause.cause(),
221                    tf.sepc,
222                    stval_val
223                );
224                crate::hlt_loop();
225            }
226        },
227
228        Trap::Exception(exception) => match exception {
229            Exception::Breakpoint => {
230                breakpoint_handler(tf);
231            }
232            Exception::LoadPageFault
233            | Exception::StorePageFault
234            | Exception::InstructionPageFault => {
235                page_fault_handler(cause.cause(), stval_val, tf.sepc);
236            }
237            Exception::IllegalInstruction => {
238                illegal_instruction_handler(tf.sepc, stval_val);
239            }
240            Exception::UserEnvCall => {
241                syscall_handler(tf);
242            }
243            _ => {
244                serial_println!(
245                    "Unhandled exception! scause={:?} sepc={:#x} stval={:#x}",
246                    cause.cause(),
247                    tf.sepc,
248                    stval_val
249                );
250                crate::hlt_loop();
251            }
252        },
253    }
254}
255
256// ── Interrupt handlers ───────────────────────────────────────────
257
258fn timer_interrupt_handler(tf: &mut TrapFrame) {
259    crate::task::keyboard::poll_keyboard();
260    set_next_timer();
261
262    // Scheduler tick — may modify tf in place for context switch
263    crate::process::scheduler_tick(tf);
264}
265
266fn external_interrupt_handler() {
267    crate::block::virtio_blk::try_handle_interrupt();
268}
269
270fn software_interrupt_handler() {
271    serial_println!("[INTERRUPT] Software interrupt received");
272}
273
274// ── Exception handlers ───────────────────────────────────────────
275
276fn breakpoint_handler(tf: &mut TrapFrame) {
277    let sepc_val = tf.sepc;
278    serial_println!("[EXCEPTION] Breakpoint at {:#x}", sepc_val);
279    println!("EXCEPTION: BREAKPOINT at {:#x}", sepc_val);
280    // Skip the compressed c.ebreak emitted for the RISC-V IMAC target.
281    tf.sepc = sepc_val.wrapping_add(2);
282}
283
284fn page_fault_handler(cause: Trap, stval_val: usize, sepc_val: usize) {
285    serial_println!(
286        "[EXCEPTION] Page Fault\n  Type: {:?}\n  Address: {:#x}\n  PC: {:#x}",
287        cause,
288        stval_val,
289        sepc_val
290    );
291    println!("EXCEPTION: PAGE FAULT");
292    println!("Accessed Address: {:#x}", stval_val);
293    println!("Exception PC: {:#x}", sepc_val);
294    println!("Fault Type: {:?}", cause);
295    crate::hlt_loop();
296}
297
298fn illegal_instruction_handler(sepc_val: usize, stval_val: usize) {
299    panic!(
300        "EXCEPTION: ILLEGAL INSTRUCTION\nPC: {:#x}\nInstruction: {:#x}",
301        sepc_val, stval_val
302    );
303}
304
305// ── Syscall handler ─────────────────────────────────────────────
306
307fn syscall_handler(tf: &mut TrapFrame) {
308    crate::syscall::syscall_trap_dispatcher(tf);
309}
310
311// ── Interrupt control ────────────────────────────────────────────
312
313pub fn without_interrupts<F, R>(f: F) -> R
314where
315    F: FnOnce() -> R,
316{
317    use riscv::register::sstatus;
318    let sie = sstatus::read().sie();
319    if sie {
320        unsafe {
321            riscv::register::sstatus::clear_sie();
322        }
323    }
324    let ret = f();
325    if sie {
326        unsafe {
327            riscv::register::sstatus::set_sie();
328        }
329    }
330    ret
331}
332
333pub fn enable_interrupts() {
334    unsafe {
335        riscv::register::sstatus::set_sie();
336    }
337}
338
339pub fn disable_interrupts() {
340    unsafe {
341        riscv::register::sstatus::clear_sie();
342    }
343}
344
345// ── Timer helpers ────────────────────────────────────────────────
346
347fn set_next_timer() {
348    const TIMER_INTERVAL: u64 = 1_000_000; // ~100ms @ 10MHz
349    let time = riscv::register::time::read64();
350    sbi_set_timer(time + TIMER_INTERVAL);
351}
352
353fn sbi_set_timer(stime_value: u64) {
354    #[cfg(target_arch = "riscv64")]
355    unsafe {
356        core::arch::asm!(
357            "mv a0, {0}",
358            "li a7, 0",
359            "ecall",
360            in(reg) stime_value,
361            lateout("a0") _,
362            lateout("a1") _,
363            options(nostack)
364        );
365    }
366
367    #[cfg(not(target_arch = "riscv64"))]
368    {
369        let _ = stime_value;
370    }
371}
372
373// ── Tests ────────────────────────────────────────────────────────
374
375#[cfg(test)]
376#[test_case]
377fn test_breakpoint_exception() {
378    use crate::serial_println;
379    serial_println!("[TEST] test_breakpoint_exception...");
380    unsafe {
381        core::arch::asm!("ebreak");
382    }
383    serial_println!("[TEST] Breakpoint handled successfully");
384}