diff --git a/risc_v/ch4/src/asm/trap.S b/risc_v/ch4/src/asm/trap.S index 5123406..3ce8170 100644 --- a/risc_v/ch4/src/asm/trap.S +++ b/risc_v/ch4/src/asm/trap.S @@ -81,7 +81,7 @@ m_trap_vector: .endr 3: - # Go to C++ + # Go to Rust # usize trap_handler(mepc, mcause) # trap_handler returns the new mepc # via a0 @@ -182,7 +182,7 @@ s_trap_vector: .endr 3: - # Go to C++ + # Go to Rust # usize trap_handler(mepc, mcause) # trap_handler returns the new mepc # via a0 diff --git a/risc_v/ch4/src/lib.rs b/risc_v/ch4/src/lib.rs index 4c7515e..fc329cb 100755 --- a/risc_v/ch4/src/lib.rs +++ b/risc_v/ch4/src/lib.rs @@ -255,7 +255,7 @@ extern "C" fn kinit() -> usize { // space application requires services. Since the user space application // only knows virtual addresses, we have to translate silently behind // the scenes. - let p = 0x8005_7000 as usize; + let p = 0x0200_0000 as usize; let m = page::virt_to_phys(&root, p).unwrap_or(0); println!("Walk 0x{:x} = 0x{:x}", p, m); // When we return from here, we'll go back to boot.S and switch into @@ -298,6 +298,10 @@ extern "C" fn kmain() { let sparkle_heart = String::from_utf8(sparkle_heart).unwrap(); println!("String = {}", sparkle_heart); } + unsafe { + let val = 0x0200_0000 as *mut u32; + val.write_volatile(1); + } // If we get here, the Box, vec, and String should all be freed since // they go out of scope. This calls their "Drop" trait. // Now see if we can read stuff: diff --git a/risc_v/ch4/src/trap.rs b/risc_v/ch4/src/trap.rs index b9cef5b..0343588 100755 --- a/risc_v/ch4/src/trap.rs +++ b/risc_v/ch4/src/trap.rs @@ -3,14 +3,26 @@ // Stephen Marz // 10 October 2019 +extern "C" { + static KERNEL_TABLE: usize; +} + #[no_mangle] extern "C" fn s_trap(epc: usize, tval: usize, cause: usize) -> usize { - epc + println!("STRAP (cause: 0x{:x} @ 0x{:x})", cause, epc); + unsafe { + // Switch to kernel's page table. + // table / 4096 Sv39 + let satp = KERNEL_TABLE >> 12 | 8 << 60; + asm!("csrw satp, $0" :: "r"(satp)); + } + epc + 4 } #[no_mangle] extern "C" fn m_trap(epc: usize, tval: usize, cause: usize, hart: usize) -> usize { - epc + println!("MTRAP (cause: 0x{:x} @ 0x{:x})", cause, epc); + epc + 4 } \ No newline at end of file