Skip to content

Commit 62bee35

Browse files
committed
style: 整理 execute
fix: panic 时以 system failure 退出 Signed-off-by: YdrMaster <ydrml@hotmail.com>
1 parent 7e177b3 commit 62bee35

File tree

3 files changed

+38
-50
lines changed

3 files changed

+38
-50
lines changed

rustsbi-qemu/src/execute.rs

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{clint, hart_id, qemu_hsm::QemuHsm, Supervisor};
2-
use riscv::register::{mstatus, mtval, scause, sepc, stval, stvec};
2+
use core::arch::asm;
3+
use riscv::register::*;
34

45
#[repr(usize)]
56
pub(crate) enum Operation {
@@ -8,9 +9,6 @@ pub(crate) enum Operation {
89
}
910

1011
pub(crate) fn execute_supervisor(hsm: &QemuHsm, supervisor: Supervisor) -> Operation {
11-
use core::arch::asm;
12-
use riscv::register::{medeleg, mie};
13-
1412
unsafe {
1513
mstatus::set_mpp(mstatus::MPP::Supervisor);
1614
mstatus::set_mie();
@@ -24,7 +22,6 @@ pub(crate) fn execute_supervisor(hsm: &QemuHsm, supervisor: Supervisor) -> Opera
2422
asm!("csrw mideleg, {}", in(reg) usize::MAX);
2523
asm!("csrw medeleg, {}", in(reg) usize::MAX);
2624
mstatus::clear_mie();
27-
medeleg::clear_illegal_instruction();
2825
medeleg::clear_supervisor_env_call();
2926
medeleg::clear_machine_env_call();
3027

@@ -35,56 +32,32 @@ pub(crate) fn execute_supervisor(hsm: &QemuHsm, supervisor: Supervisor) -> Opera
3532

3633
hsm.record_current_start_finished();
3734
loop {
38-
use riscv::register::{
39-
mcause::{self, Exception as E, Interrupt as I, Trap as T},
40-
mip,
41-
};
35+
use mcause::{Exception as E, Interrupt as I, Trap as T};
4236

4337
unsafe { m_to_s(&mut ctx) };
4438

4539
match mcause::read().cause() {
4640
T::Interrupt(I::MachineTimer) => unsafe {
41+
// set timer 同时打开中断,响应后即可关闭
4742
mie::clear_mtimer();
43+
// 转发给 supervisor
4844
mip::clear_mtimer();
4945
mip::set_stimer();
5046
},
51-
T::Interrupt(I::MachineSoft) => {
47+
T::Interrupt(I::MachineSoft) => unsafe {
48+
// 响应中断,清除中断标记
5249
crate::clint::get().clear_soft(hart_id());
53-
unsafe {
54-
mip::clear_msoft();
55-
mip::set_ssoft();
56-
}
57-
}
50+
// 转发给 supervisor
51+
mip::clear_msoft();
52+
mip::set_ssoft();
53+
},
5854
T::Exception(E::SupervisorEnvCall) => {
5955
if let Some(op) = ctx.handle_ecall() {
6056
return op;
6157
}
6258
}
63-
T::Exception(E::IllegalInstruction) => {
64-
use riscv::register::scause::{Exception as E, Trap as T};
65-
66-
// let instruction = mtval::read();
67-
// 尝试修正或代理指令
68-
// TODO 需要排查 qemu 哪些版本需要代理哪些指令?
69-
// TODO 标准 20191213 的表 24.3 列出了一些特殊的 CSR,SBI 软件负责将它们模拟出来,但 Qemu6.0+ 似乎不需要模拟 time
70-
// const OPCODE_MASK: usize = (1 << 7) - 1;
71-
// const REG_MASK: usize = (1 << 5) - 1;
72-
// const OPCODE_CSR: usize = 0b1110011;
73-
// const CSR_TIME: usize = 0xc01;
74-
// if let OPCODE_CSR = instruction & OPCODE_MASK {
75-
// if instruction >> 20 == CSR_TIME {
76-
// match (instruction >> 7) & REG_MASK {
77-
// 0 => {}
78-
// rd => *ctx.x_mut(rd) = crate::clint::get().get_mtime() as _,
79-
// }
80-
// continue;
81-
// }
82-
// }
83-
84-
ctx.do_transfer_trap(T::Exception(E::IllegalInstruction));
85-
}
86-
// TODO 可以修复非原子的非对齐访存
87-
t => panic!("unsupported trap: {t:?}"),
59+
// TODO 可以修复非原子的非对齐访存?
60+
t => ctx.trap_stop(t),
8861
}
8962
}
9063
}
@@ -107,7 +80,7 @@ impl Context {
10780
mepc: supervisor.start_addr,
10881
};
10982

110-
unsafe { core::arch::asm!("csrr {}, mstatus", out(reg) ctx.mstatus) };
83+
unsafe { asm!("csrr {}, mstatus", out(reg) ctx.mstatus) };
11184
*ctx.a_mut(0) = hart_id();
11285
*ctx.a_mut(1) = supervisor.opaque;
11386

@@ -188,6 +161,7 @@ impl Context {
188161
None
189162
}
190163

164+
#[allow(unused)]
191165
fn do_transfer_trap(&mut self, cause: scause::Trap) {
192166
unsafe {
193167
// 向 S 转发陷入
@@ -213,12 +187,29 @@ impl Context {
213187
mstatus::set_spie();
214188
mstatus::clear_sie();
215189
}
216-
core::arch::asm!("csrr {}, mstatus", out(reg) self.mstatus);
190+
asm!("csrr {}, mstatus", out(reg) self.mstatus);
217191
// 设置返回地址,返回到 S
218192
// TODO Vectored stvec?
219193
self.mepc = stvec::read().address();
220194
}
221195
}
196+
197+
fn trap_stop(&self, trap: mcause::Trap) -> ! {
198+
println!(
199+
"
200+
-----------------------------
201+
> trap: {trap:?}
202+
> mstatus: {:#018x}
203+
> mepc: {:#018x}
204+
> mtval: {:#018x}
205+
-----------------------------
206+
",
207+
self.mstatus,
208+
self.mepc,
209+
mtval::read()
210+
);
211+
panic!("stopped with unsupported trap")
212+
}
222213
}
223214

224215
/// M 态转到 S 态。
@@ -230,7 +221,7 @@ impl Context {
230221
/// 实际 x0(zero) 和 x2(sp) 不需要保存在这里。
231222
#[naked]
232223
unsafe extern "C" fn m_to_s(ctx: &mut Context) {
233-
core::arch::asm!(
224+
asm!(
234225
r"
235226
.altmacro
236227
.macro SAVE_M n
@@ -298,7 +289,7 @@ unsafe extern "C" fn m_to_s(ctx: &mut Context) {
298289
#[naked]
299290
#[link_section = ".text.trap_handler"]
300291
unsafe extern "C" fn s_to_m() {
301-
core::arch::asm!(
292+
asm!(
302293
r"
303294
.altmacro
304295
.macro SAVE_S n

rustsbi-qemu/src/main.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ struct Supervisor {
4040
#[cfg_attr(not(test), panic_handler)]
4141
fn panic(info: &core::panic::PanicInfo) -> ! {
4242
use rustsbi::{
43-
spec::srst::{RESET_REASON_NO_REASON, RESET_TYPE_SHUTDOWN},
43+
spec::srst::{RESET_REASON_SYSTEM_FAILURE, RESET_TYPE_SHUTDOWN},
4444
Reset,
4545
};
4646
// 输出的信息大概是“[rustsbi-panic] hart 0 panicked at ...”
4747
println!("[rustsbi-panic] hart {} {info}", hart_id());
4848
println!("[rustsbi-panic] system shutdown scheduled due to RustSBI panic");
49-
qemu_test::get().system_reset(RESET_TYPE_SHUTDOWN, RESET_REASON_NO_REASON);
50-
loop {
51-
core::hint::spin_loop();
52-
}
49+
qemu_test::get().system_reset(RESET_TYPE_SHUTDOWN, RESET_REASON_SYSTEM_FAILURE);
50+
unreachable!()
5351
}
5452

5553
/// 入口。

test-kernel/src/test.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ pub(crate) fn trap_execption_delegate(hartid: usize) {
8080
}
8181

8282
pub(crate) fn trap_interrupt_delegate(hartid: usize) {
83-
use core::arch::asm;
8483
use riscv::register::{
8584
scause::{Interrupt, Trap},
8685
sie, time,

0 commit comments

Comments
 (0)