diff --git a/src/main.rs b/src/main.rs index a298937..b24a777 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use core::panic::PanicInfo; mod heap; +mod trap; mod uart; #[naked] @@ -30,9 +31,17 @@ unsafe extern "C" fn _enter() -> ! { "la gp, _global_pointer", ".option pop", // pops the current option stack + // delegate all traps to S-mode trap handler + "li t5, 0xffff", + "csrw medeleg, t5", // delegate all machine exceptions + "csrw mideleg, t5", // delegate all machine interrupts + // set the stack pointer "la sp, _init_stack_top", + "la t2, {trap_vector}", + "csrw mtvec, t2", + // clear the BSS "la t0, _bss_start", "la t1, _bss_end", @@ -42,13 +51,8 @@ unsafe extern "C" fn _enter() -> ! { "addi t0, t0, 1", "bne t0, t1, 1b", "2:", - // BSS is clear! - - // delegate all traps to S-mode trap handler - "li t5, 0xffff", - "csrw medeleg, t5", // delegate all machine exceptions - "csrw mideleg, t5", // delegate all machine interrupts "j 4f", + // BSS is clear! // busy loop if hartid =/= 0 "3:", @@ -59,6 +63,7 @@ unsafe extern "C" fn _enter() -> ! { // "tail-call" to {start} (call without saving a return address) "tail {start}", start = sym start, // {start} refers to the function [start] below + trap_vector = sym trap::trap_handler, // {trap_vector} refers to function [trap::trap_handler] options(noreturn) // we must handle "returning" from assembly ); } diff --git a/src/script.ld b/src/script.ld index 959dda3..6513164 100644 --- a/src/script.ld +++ b/src/script.ld @@ -38,7 +38,7 @@ SECTIONS { } >ram AT>ram :bss # and this goes into the bss segment . = ALIGN(16); # our stack needs to be 16-byte aligned, per the C calling convention - PROVIDE(_init_stack_top = . + 0x1000); # reserve 0x1000 bytes for the initialisation stack + PROVIDE(_init_stack_top = . + 0x1000); # reserve 0x1000 bytes (4 kB) for the initialisation stack PROVIDE(_kernel_heap_bottom = _init_stack_top); # allocate heap to remaining physical memory PROVIDE(_kernel_heap_top = ORIGIN(ram) + LENGTH(ram)); # top of heap is end of ram diff --git a/src/trap.rs b/src/trap.rs new file mode 100644 index 0000000..a285b0a --- /dev/null +++ b/src/trap.rs @@ -0,0 +1,8 @@ +#[naked] +pub unsafe extern "C" fn trap_handler() -> ! { + use core::arch::asm; + asm!( + "mret", + options(noreturn) + ); +} \ No newline at end of file