Skip to content

Commit eefc0fc

Browse files
committed
cleanup
Signed-off-by: YdrMaster <[email protected]>
1 parent 1533a8f commit eefc0fc

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

rustsbi-qemu/src/main.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod clint;
77
mod device_tree;
88
mod hart_csr_utils;
99
mod qemu_test;
10+
mod riscv_spec;
1011
mod trap_vec;
1112

1213
mod constants {
@@ -32,10 +33,11 @@ use core::{
3233
use device_tree::BoardInfo;
3334
use fast_trap::{FastContext, FastResult, FlowContext, FreeTrapStack, TrapStackBlock};
3435
use hsm_cell::HsmCell;
35-
use riscv::register::*;
36+
use riscv_spec::*;
3637
use rustsbi::RustSBI;
3738
use sbi_spec::binary::SbiRet;
3839
use spin::{Mutex, Once};
40+
use trap_vec::{load_trap_vec, trap_vec};
3941
use uart_16550::MmioSerialPort;
4042

4143
/// 栈空间。
@@ -44,10 +46,6 @@ static mut ROOT_STACK: [Stack; NUM_HART_MAX] = [Stack::ZERO; NUM_HART_MAX];
4446

4547
/// 入口。
4648
///
47-
/// 1. 关中断
48-
/// 2. 设置启动栈
49-
/// 3. 跳转到 rust 入口函数
50-
///
5149
/// # Safety
5250
///
5351
/// 裸函数。
@@ -56,9 +54,6 @@ static mut ROOT_STACK: [Stack; NUM_HART_MAX] = [Stack::ZERO; NUM_HART_MAX];
5654
#[link_section = ".text.entry"]
5755
unsafe extern "C" fn _start() -> ! {
5856
asm!(
59-
// 关中断
60-
" csrw mie, zero",
61-
// 设置栈
6257
" la sp, {stack}
6358
li t0, {per_hart_stack_size}
6459
csrr t1, mhartid
@@ -67,14 +62,14 @@ unsafe extern "C" fn _start() -> ! {
6762
addi t1, t1, -1
6863
bnez t1, 1b
6964
call {move_stack}
65+
call {rust_main}
66+
j {trap}
7067
",
71-
" call {rust_main}",
72-
" j {trap}",
7368
per_hart_stack_size = const LEN_STACK_PER_HART,
7469
stack = sym ROOT_STACK,
7570
move_stack = sym fast_trap::reuse_stack_for_trap,
7671
rust_main = sym rust_main,
77-
trap = sym fast_trap::trap_entry,
72+
trap = sym trap_vec,
7873
options(noreturn)
7974
)
8075
}
@@ -175,8 +170,8 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) {
175170
asm!("csrw mideleg, {}", in(reg) !0);
176171
asm!("csrw medeleg, {}", in(reg) !0);
177172
asm!("csrw mcounteren, {}", in(reg) !0);
178-
medeleg::clear_supervisor_env_call();
179-
medeleg::clear_machine_env_call();
173+
riscv::register::medeleg::clear_supervisor_env_call();
174+
riscv::register::medeleg::clear_machine_env_call();
180175
}
181176
}
182177

@@ -187,6 +182,7 @@ fn hart_id() -> usize {
187182

188183
/// 设置 PMP。
189184
fn set_pmp(board_info: &BoardInfo) {
185+
use riscv::register::*;
190186
let mem = &board_info.mem;
191187
unsafe {
192188
pmpcfg0::set_pmp(0, Range::OFF, Permission::NONE, false);
@@ -220,7 +216,10 @@ extern "C" fn fast_handler(
220216
a6: usize,
221217
a7: usize,
222218
) -> FastResult {
223-
use mcause::{Exception as E, Interrupt as I, Trap as T};
219+
use riscv::register::{
220+
mcause::{self, Exception as E, Interrupt as I, Trap as T},
221+
mepc, mstatus, mtval,
222+
};
224223

225224
let cause = mcause::read();
226225
// 启动
@@ -232,21 +231,21 @@ extern "C" fn fast_handler(
232231
match unsafe { hart_ctx.hsm.local() }.start() {
233232
Ok(supervisor) => {
234233
unsafe {
235-
mtvec::write(trap_vec::trap_vec as _, mtvec::TrapMode::Vectored);
234+
load_trap_vec(true);
236235
mstatus::set_mpie();
237236
mstatus::set_mpp(mstatus::MPP::Supervisor);
238-
asm!("csrw mie, {}", in(reg) (1<<3)|(1<<7)|(1<<11));
237+
mie::write(mie::MSIE | mie::MTIE | mie::MEIE);
239238
}
240239
hart_ctx.trap.a[0] = hart_id;
241240
hart_ctx.trap.a[1] = supervisor.opaque;
242241
hart_ctx.trap.pc = supervisor.start_addr;
243242
}
244243
Err(_state) => {
245244
unsafe {
246-
mtvec::write(trap_vec::trap_vec as _, mtvec::TrapMode::Direct);
245+
load_trap_vec(false);
247246
mstatus::set_mpie();
248247
mstatus::set_mpp(mstatus::MPP::Machine);
249-
asm!("csrw mie, {}", in(reg) 1<<3);
248+
mie::write(mie::MSIE);
250249
};
251250
hart_ctx.trap.pc = _stop as usize;
252251
}
@@ -265,9 +264,9 @@ extern "C" fn fast_handler(
265264
if ret.is_ok() && a7 == hsm::EID_HSM {
266265
if a6 == hsm::HART_STOP {
267266
unsafe {
268-
mtvec::write(trap_vec::trap_vec as _, mtvec::TrapMode::Direct);
267+
load_trap_vec(true);
269268
mstatus::set_mpp(mstatus::MPP::Machine);
270-
asm!("csrw mie, {}", in(reg) 1<<3);
269+
mie::write(mie::MSIE);
271270
ROOT_STACK[hart_id()].hart_context().trap.pc = _stop as usize;
272271
}
273272
return ctx.call(0);
@@ -276,7 +275,7 @@ extern "C" fn fast_handler(
276275
&& ctx.a0() == hsm::HART_SUSPEND_TYPE_NON_RETENTIVE as usize
277276
{
278277
unsafe {
279-
mtvec::write(trap_vec::trap_vec as _, mtvec::TrapMode::Direct);
278+
load_trap_vec(false);
280279
mstatus::set_mpp(mstatus::MPP::Machine);
281280
ROOT_STACK[hart_id()].hart_context().trap.pc = _stop as usize;
282281
}

rustsbi-qemu/src/riscv_spec.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[allow(unused, missing_docs)]
2+
3+
pub mod mie {
4+
use core::arch::asm;
5+
6+
pub const SSIE: usize = 1 << 1;
7+
pub const VSSIE: usize = 1 << 2;
8+
pub const MSIE: usize = 1 << 3;
9+
pub const STIE: usize = 1 << 5;
10+
pub const VSTIE: usize = 1 << 6;
11+
pub const MTIE: usize = 1 << 7;
12+
pub const SEIE: usize = 1 << 9;
13+
pub const VSEIE: usize = 1 << 10;
14+
pub const MEIE: usize = 1 << 11;
15+
pub const SGEIE: usize = 1 << 12;
16+
17+
#[inline(always)]
18+
pub fn write(bits: usize) {
19+
unsafe { asm!("csrw mie, {}", in(reg) bits, options(nomem)) };
20+
}
21+
}

rustsbi-qemu/src/trap_vec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
use crate::clint;
22
use core::arch::asm;
33
use fast_trap::trap_entry;
4+
use riscv::register::mtvec::{self, TrapMode::*};
5+
6+
/// 加载陷入向量。
7+
#[inline]
8+
pub(crate) unsafe fn load_trap_vec(vec: bool) {
9+
mtvec::write(trap_vec as _, if vec { Vectored } else { Direct });
10+
}
411

512
/// 中断向量表
613
///

0 commit comments

Comments
 (0)