ahrightos-kernel 0.1.0

RISC-V 64 Rust teaching kernel for AhRightOS.
Documentation
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 }
}

// ── Trap frame — saved register state at trap entry ──────────────

#[repr(C)]
pub struct TrapFrame {
    pub x: [usize; 32], // x0-x31  (x0 always 0)
    pub sepc: usize,
    pub sstatus: usize,
    pub satp: usize,
}

impl TrapFrame {
    /// Save trap frame state into a ProcessContext.
    /// Called before a context switch to preserve the old task's state.
    pub fn save_to(&self, ctx: &mut crate::process::ProcessContext) {
        ctx.x.copy_from_slice(&self.x[1..]); // skip x0
        ctx.sepc = self.sepc;
        ctx.sstatus = self.sstatus;
        ctx.satp = self.satp;
    }

    /// Load trap frame state from a ProcessContext.
    /// Called after a context switch to resume the new task.
    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;
    }
}

// ── Assembly trap entry ──────────────────────────────────────────

global_asm!(
    ".section .text.trap, \"ax\"",
    ".align 2",
    ".globl trap_entry",
    "trap_entry:",
    // Swap sp with sscratch: sp ← kernel_sp, sscratch ← old_sp
    "   csrrw sp, sscratch, sp",
    // Allocate TrapFrame on kernel stack (32 regs + sepc + sstatus + satp = 35 * 8 = 280)
    "   addi sp, sp, -280",
    // x0 is hardwired to 0 — leave offset 0 as zero
    "   sd zero, 0(sp)",
    // Save GPRs x1-x31.  x5 (t0) and x6 (t1) must be saved BEFORE
    // they are used as scratch below.
    "   sd x1, 8(sp)",
    "   sd x3, 24(sp)",
    "   sd x4, 32(sp)",
    "   sd x5, 40(sp)", // t0 — saved before use as scratch
    "   sd x6, 48(sp)", // t1 — saved before use as scratch
    "   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)",
    // Now safely use t0/t1 as scratch to save x2 (old sp from sscratch)
    "   csrr t0, sscratch",
    "   sd t0, 16(sp)",
    // While handling this trap, sscratch points at the next free slot on
    // the trap stack. A nested trap then allocates below this frame instead
    // of falling back to the interrupted task stack.
    "   csrw sscratch, sp",
    // Save CSRs
    "   csrr t0, sepc",
    "   sd t0, 256(sp)",
    "   csrr t0, sstatus",
    "   sd t0, 264(sp)",
    "   csrr t0, satp",
    "   sd t0, 272(sp)",
    // Call Rust handler: rust_trap_handler(&mut TrapFrame)
    "   mv a0, sp",
    "   call rust_trap_handler",
    // ── Restore path ─────────────────────────────────────────────
    ".globl trap_return",
    "trap_return:",
    // Save the next free trap-stack slot to sscratch.
    "   addi t0, sp, 280",
    "   csrw sscratch, t0",
    // Restore satp + sfence.vma
    "   ld t0, 272(sp)",
    "   csrw satp, t0",
    "   sfence.vma",
    // Restore sstatus
    "   ld t0, 264(sp)",
    "   csrw sstatus, t0",
    // Restore sepc
    "   ld t0, 256(sp)",
    "   csrw sepc, t0",
    // Restore GPRs using t6 as base (sp/x2 is about to change).
    // x2 MUST be restored LAST because subsequent loads would use
    // the new sp value as their base, not the trap frame.
    "   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)",
    // Restore x2/sp — need to do this BEFORE x31 because x31 IS t6
    // and loading it would clobber the base register
    "   ld x2, 16(t6)",
    // Restore x31/t6 LAST — nothing uses t6 as base after this
    "   ld x31, 248(t6)",
    "   sret",
);

extern "C" {
    fn trap_entry();
}

// ── Initialisation ───────────────────────────────────────────────

pub fn init_idt() {
    unsafe {
        // Use a dedicated trap stack so kernel-mode traps do not overwrite
        // the currently executing stack frame.
        core::arch::asm!("csrw sscratch, {}", in(reg) trap_stack_top());
        // Point stvec to our manual trap entry
        stvec::write(trap_entry as *const () as usize, stvec::TrapMode::Direct);

        // Allow S-mode to access U-flagged pages (syscall buffers)
        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");
}

// ── Main Rust trap handler ──────────────────────────────────────

#[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();
            }
        },
    }
}

// ── Interrupt handlers ───────────────────────────────────────────

fn timer_interrupt_handler(tf: &mut TrapFrame) {
    crate::task::keyboard::poll_keyboard();
    set_next_timer();

    // Scheduler tick — may modify tf in place for context switch
    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");
}

// ── Exception handlers ───────────────────────────────────────────

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);
    // Skip the compressed c.ebreak emitted for the RISC-V IMAC target.
    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
    );
}

// ── Syscall handler ─────────────────────────────────────────────

fn syscall_handler(tf: &mut TrapFrame) {
    crate::syscall::syscall_trap_dispatcher(tf);
}

// ── Interrupt control ────────────────────────────────────────────

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();
    }
}

// ── Timer helpers ────────────────────────────────────────────────

fn set_next_timer() {
    const TIMER_INTERVAL: u64 = 1_000_000; // ~100ms @ 10MHz
    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;
    }
}

// ── Tests ────────────────────────────────────────────────────────

#[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");
}