Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit 472f2bd

Browse files
authored
Merge pull request #64 from woshiluo/bump-riscv
feat: bump `riscv` to 1.12.1
2 parents 82d4e5b + 9b125f1 commit 472f2bd

File tree

4 files changed

+69
-41
lines changed

4 files changed

+69
-41
lines changed

Cargo.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prototyper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ forced-target = "riscv64imac-unknown-none-elf"
1212
aclint = "=0.1.0"
1313
log = "0.4"
1414
panic-halt = "1.0.0"
15-
riscv = "0.11.1"
15+
riscv = "0.12.1"
1616
sifive-test-device = "0.0.0"
1717
spin = "0.9.8"
1818
uart16550 = "0.0.1"

prototyper/src/fail.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use serde_device_tree::Dtb;
33

44
use crate::devicetree;
55

6+
use riscv::interrupt::machine::{Exception, Interrupt};
7+
use riscv::register::{mcause::Trap, mepc, mtval};
8+
69
#[cfg(all(feature = "payload", feature = "jump"))]
710
compile_error!("feature \"payload\" and feature \"jump\" cannot be enabled at the same time");
811

@@ -19,6 +22,15 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
1922
loop {}
2023
}
2124

25+
pub fn unsupported_trap(trap: Option<Trap<Interrupt, Exception>>) -> ! {
26+
error!("-----------------------------");
27+
error!("trap: {trap:?}");
28+
error!("mepc: {:#018x}", mepc::read());
29+
error!("mtval: {:#018x}", mtval::read());
30+
error!("-----------------------------");
31+
panic!("Stopped with unsupported trap")
32+
}
33+
2234
/// Handles device tree format parsing errors by logging and resetting.
2335
#[cold]
2436
pub fn device_tree_format(_err: devicetree::ParseDeviceTreeError) -> Dtb {

prototyper/src/sbi/trap/mod.rs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
pub mod boot;
22
pub mod handler;
33

4+
use crate::fail::unsupported_trap;
5+
46
use fast_trap::{FastContext, FastResult};
7+
use riscv::interrupt::machine::{Exception, Interrupt};
58
use riscv::register::{
6-
mcause::{self, Exception as E, Interrupt, Trap as T},
7-
mepc, mip, mstatus, mtval,
9+
mcause::{self, Trap},
10+
mepc, mip, mstatus,
811
};
912

1013
/// Fast trap handler for all trap.
@@ -25,47 +28,48 @@ pub extern "C" fn fast_handler(
2528
ctx.regs().a = [ctx.a0(), a1, a2, a3, a4, a5, a6, a7];
2629
};
2730

28-
match mcause::read().cause() {
29-
// Handle Msoft
30-
T::Interrupt(Interrupt::MachineSoft) => {
31-
save_regs(&mut ctx);
32-
handler::msoft_handler(ctx)
33-
}
34-
// Handle MTimer
35-
T::Interrupt(Interrupt::MachineTimer) => {
36-
use crate::sbi::ipi;
31+
match mcause::read().cause().try_into() {
32+
Ok(cause) => {
33+
match cause {
34+
// Handle Msoft
35+
Trap::Interrupt(Interrupt::MachineSoft) => {
36+
save_regs(&mut ctx);
37+
handler::msoft_handler(ctx)
38+
}
39+
// Handle MTimer
40+
Trap::Interrupt(Interrupt::MachineTimer) => {
41+
use crate::sbi::ipi;
3742

38-
ipi::clear_mtime();
39-
unsafe {
40-
mip::clear_stimer();
41-
}
42-
save_regs(&mut ctx);
43-
ctx.restore()
44-
}
45-
// Handle SBI calls
46-
T::Exception(E::SupervisorEnvCall) => {
47-
handler::sbi_call_handler(ctx, a1, a2, a3, a4, a5, a6, a7)
48-
}
49-
// Handle illegal instructions
50-
T::Exception(E::IllegalInstruction) => {
51-
if mstatus::read().mpp() == mstatus::MPP::Machine {
52-
panic!("Cannot handle illegal instruction exception from M-MODE");
53-
}
43+
ipi::clear_mtime();
44+
unsafe {
45+
mip::clear_stimer();
46+
}
47+
save_regs(&mut ctx);
48+
ctx.restore()
49+
}
50+
// Handle SBI calls
51+
Trap::Exception(Exception::SupervisorEnvCall) => {
52+
handler::sbi_call_handler(ctx, a1, a2, a3, a4, a5, a6, a7)
53+
}
54+
// Handle illegal instructions
55+
Trap::Exception(Exception::IllegalInstruction) => {
56+
if mstatus::read().mpp() == mstatus::MPP::Machine {
57+
panic!("Cannot handle illegal instruction exception from M-MODE");
58+
}
5459

55-
save_regs(&mut ctx);
56-
if !handler::illegal_instruction_handler(&mut ctx) {
57-
handler::delegate(&mut ctx);
60+
save_regs(&mut ctx);
61+
if !handler::illegal_instruction_handler(&mut ctx) {
62+
handler::delegate(&mut ctx);
63+
}
64+
ctx.restore()
65+
}
66+
// Handle other traps
67+
trap => unsupported_trap(Some(trap)),
5868
}
59-
ctx.restore()
6069
}
61-
// Handle other traps
62-
trap => {
63-
error!("-----------------------------");
64-
error!("trap: {trap:?}");
65-
error!("mepc: {:#018x}", mepc::read());
66-
error!("mtval: {:#018x}", mtval::read());
67-
error!("-----------------------------");
68-
panic!("Stopped with unsupported trap")
70+
Err(err) => {
71+
error!("Failed to parse mcause: {:?}", err);
72+
unsupported_trap(None);
6973
}
7074
}
7175
}

0 commit comments

Comments
 (0)