Skip to content

Commit 998242c

Browse files
committed
feat(sbi): 封装 mtvec 操作方便管理
1 parent 65c7d27 commit 998242c

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

rustsbi-qemu/src/execute/runtime.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ impl Runtime {
2020
use riscv::register::mstatus;
2121

2222
unsafe {
23-
asm!("csrw mstatus, {}", in(reg)0);
2423
mstatus::set_mpp(mstatus::MPP::Supervisor);
2524
mstatus::set_mie();
2625
};
@@ -66,7 +65,7 @@ impl Runtime {
6665

6766
clint::get().clear_soft(hart_id());
6867
unsafe {
69-
use riscv::register::{medeleg, mie, mtvec};
68+
use riscv::register::{medeleg, mie};
7069

7170
mstatus::clear_mie();
7271
asm!("csrw mip, {}", in(reg) 0);
@@ -76,7 +75,7 @@ impl Runtime {
7675
medeleg::clear_supervisor_env_call();
7776
medeleg::clear_machine_env_call();
7877

79-
mtvec::write(from_supervisor_save as usize, mtvec::TrapMode::Direct);
78+
crate::set_mtcev(from_supervisor_save as usize);
8079
mie::set_mext();
8180
mie::set_msoft();
8281
}

rustsbi-qemu/src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ static HSM: Once<qemu_hsm::QemuHsm> = Once::new();
112112

113113
/// rust 入口。
114114
extern "C" fn rust_main(_hartid: usize, opaque: usize) {
115-
use riscv::register::mtvec;
116-
unsafe { mtvec::write(early_trap as _, mtvec::TrapMode::Direct) };
115+
unsafe { set_mtcev(early_trap as _) };
117116

118117
#[link_section = ".bss.uninit"]
119118
static BOARD_INFO: Once<device_tree::BoardInfo> = Once::new();
@@ -294,3 +293,9 @@ impl PmpCfg {
294293
fn hart_id() -> usize {
295294
riscv::register::mhartid::read()
296295
}
296+
297+
#[inline(always)]
298+
unsafe fn set_mtcev(trap_handler: usize) {
299+
use riscv::register::mtvec;
300+
mtvec::write(trap_handler, mtvec::TrapMode::Direct);
301+
}

rustsbi-qemu/src/qemu_hsm.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Hart state monitor designed for QEMU
22
3-
use crate::{clint::Clint, entry, hart_id, Supervisor, NUM_HART_MAX, SUPERVISOR_ENTRY};
3+
use crate::{clint::Clint, entry, hart_id, set_mtcev, Supervisor, NUM_HART_MAX, SUPERVISOR_ENTRY};
44
use core::{mem::MaybeUninit, sync::atomic::AtomicU8};
55
use rustsbi::SbiRet;
66
use spin::Mutex;
@@ -35,15 +35,19 @@ impl QemuHsm {
3535
let mut supervisor = unsafe { supervisor.assume_init() };
3636
for id in 0..smp {
3737
state[id] = AtomicU8::new(START_PENDING);
38-
// 执行全局初始化的硬件线程将直通特权软件
39-
supervisor[id] = Mutex::new(if id == hart_id() {
40-
Some(Supervisor {
41-
start_addr: SUPERVISOR_ENTRY,
42-
opaque,
43-
})
44-
} else {
45-
None
46-
});
38+
supervisor[id] = Mutex::new(
39+
// 执行全局初始化的硬件线程将直通特权软件
40+
if id == hart_id() {
41+
Some(Supervisor {
42+
start_addr: SUPERVISOR_ENTRY,
43+
opaque,
44+
})
45+
}
46+
// 否则将在下一个步骤被关闭
47+
else {
48+
None
49+
},
50+
);
4751
}
4852

4953
Self {
@@ -95,7 +99,7 @@ impl QemuHsm {
9599
/// 此时核状态必然是不可干预的 Pending 状态,中断业已关闭。
96100
pub fn finallize_before_stop(&self) {
97101
use core::sync::atomic::Ordering::{AcqRel, Acquire};
98-
use riscv::register::{mie, mip, mtvec};
102+
use riscv::register::{mie, mip};
99103

100104
// 检查当前状态是重启前的挂起状态
101105
let state = &self.state[hart_id()];
@@ -117,7 +121,7 @@ impl QemuHsm {
117121
unsafe {
118122
mip::clear_msoft();
119123
mie::set_msoft();
120-
mtvec::write(entry as _, mtvec::TrapMode::Direct);
124+
set_mtcev(entry as _);
121125
}
122126
if let Err(unexpected) = state.compare_exchange(current, new, AcqRel, Acquire) {
123127
panic!("failed to reboot for a race {current:?} => {unexpected:?}")

0 commit comments

Comments
 (0)