Skip to content

Commit 51a9c71

Browse files
committed
test: 补充模拟软件陷入的示例
Signed-off-by: YdrMaster <ydrml@hotmail.com>
1 parent cb280f3 commit 51a9c71

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

test-app/src/main.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ use core::{
77
arch::asm,
88
mem::{forget, MaybeUninit},
99
ptr::{null, NonNull},
10+
unreachable,
1011
};
1112
use dtb_walker::{Dtb, DtbObj, HeaderError, Str, WalkOperation};
1213
use fast_trap::{
13-
load_direct_trap_entry, reuse_stack_for_trap, trap_entry, FastContext, FastResult, FlowContext,
14-
FreeTrapStack, TrapStackBlock,
14+
load_direct_trap_entry, reuse_stack_for_trap, soft_trap, trap_entry, FastContext, FastResult,
15+
FlowContext, FreeTrapStack, TrapStackBlock,
1516
};
1617
use rcore_console::log;
1718
use riscv::register::*;
@@ -20,6 +21,7 @@ use uart_16550::MmioSerialPort;
2021

2122
#[link_section = ".bss.uninit"]
2223
static mut ROOT_STACK: Stack = Stack([0; 4096]);
24+
static mut FREE_STACK: Stack = Stack([0; 4096]);
2325
static mut ROOT_CONTEXT: FlowContext = FlowContext::ZERO;
2426

2527
#[naked]
@@ -133,17 +135,30 @@ extern "C" fn rust_main(_hartid: usize, dtb: *const u8) {
133135
.unwrap()
134136
.load();
135137

138+
{
139+
// 叠加一个陷入栈用于临时保护
140+
let _loaded = FreeTrapStack::new(
141+
StackRef(unsafe { &mut FREE_STACK }),
142+
context_ptr,
143+
fast_handler,
144+
)
145+
.unwrap()
146+
.load();
147+
// 模拟陷入
148+
unsafe { soft_trap(cause::CALL) };
149+
}
150+
136151
#[cfg(feature = "m-mode")]
137152
{
138153
assert_ne!(0x5050, mscratch::read());
139154
log::debug!("mscratch: {:#x}", mscratch::read());
140-
unsafe { asm!("csrw mcause, {}", in(reg) 24) };
155+
unsafe { asm!("csrw mcause, {}", in(reg) cause::BOOT) };
141156
}
142157
#[cfg(feature = "s-mode")]
143158
{
144159
assert_ne!(0x5050, sscratch::read());
145160
log::debug!("sscratch: {:#x}", sscratch::read());
146-
unsafe { asm!("csrw scause, {}", in(reg) 24) };
161+
unsafe { asm!("csrw scause, {}", in(reg) cause::BOOT) };
147162
}
148163

149164
// 忘了它,在汇编里触发陷入还要用
@@ -153,6 +168,11 @@ extern "C" fn rust_main(_hartid: usize, dtb: *const u8) {
153168
unsafe { load_direct_trap_entry() };
154169
}
155170

171+
mod cause {
172+
pub(super) const BOOT: usize = 24;
173+
pub(super) const CALL: usize = 25;
174+
}
175+
156176
extern "C" fn fast_handler(
157177
mut ctx: FastContext,
158178
a1: usize,
@@ -174,10 +194,13 @@ extern "C" fn fast_handler(
174194
unsafe { &*TEST }.pass()
175195
}
176196
T::Exception(E::Unknown) => {
177-
mepc::write(exception as _);
197+
match cause.bits() {
198+
cause::BOOT => mepc::write(exception as _),
199+
cause::CALL => log::warn!("call fast-trap inline!"),
200+
_ => unreachable!(),
201+
}
178202
unsafe { mstatus::set_mpp(mstatus::MPP::Machine) };
179-
let a0 = ctx.a0();
180-
ctx.regs().a = [a0, a1, a2, a3, a4, a5, a6, a7];
203+
ctx.regs().a = [ctx.a0(), a1, a2, a3, a4, a5, a6, a7];
181204
ctx.restore()
182205
}
183206
T::Exception(_) | T::Interrupt(_) => unreachable!(),
@@ -194,10 +217,13 @@ extern "C" fn fast_handler(
194217
unsafe { &*TEST }.pass()
195218
}
196219
T::Exception(E::Unknown) => {
197-
sepc::write(exception as _);
220+
match cause.bits() {
221+
cause::BOOT => mepc::write(exception as _),
222+
cause::CALL => log::warn!("call fast-trap inline!"),
223+
_ => unreachable!(),
224+
}
198225
unsafe { sstatus::set_spp(sstatus::SPP::Supervisor) };
199-
let a0 = ctx.a0();
200-
ctx.regs().a = [a0, a1, a2, a3, a4, a5, a6, a7];
226+
ctx.regs().a = [ctx.a0(), a1, a2, a3, a4, a5, a6, a7];
201227
ctx.restore()
202228
}
203229
T::Exception(_) | T::Interrupt(_) => unreachable!(),

0 commit comments

Comments
 (0)