Skip to content

Commit 0b5ba33

Browse files
committed
temp: 响应 sbi call
Signed-off-by: YdrMaster <[email protected]>
1 parent bfe7b0a commit 0b5ba33

File tree

3 files changed

+67
-75
lines changed

3 files changed

+67
-75
lines changed

hsm-cell/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ unsafe impl<T: Send> Send for HsmCell<T> {}
2828
const HART_STATE_START_PENDING_EXT: usize = usize::MAX;
2929

3030
impl<T> HsmCell<T> {
31+
/// 创建一个新的共享对象。
32+
pub const fn new() -> Self {
33+
Self {
34+
status: AtomicUsize::new(HART_STATE_STOPPED),
35+
val: UnsafeCell::new(None),
36+
}
37+
}
38+
3139
/// 从当前硬件线程的状态中获取线程间共享对象。
3240
///
3341
/// # Safety

rustsbi-qemu/src/clint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ pub(crate) struct Clint;
66
impl Ipi for Clint {
77
#[inline]
88
fn send_ipi(&self, hart_mask: HartMask) -> SbiRet {
9-
let hsm = crate::HSM.wait();
9+
// let hsm = crate::HSM.wait();
1010
for i in 0..crate::NUM_HART_MAX {
11-
if hart_mask.has_bit(i) && hsm.is_ipi_allowed(i) {
11+
if hart_mask.has_bit(i) {
12+
//&& hsm.is_ipi_allowed(i) {
1213
msip::send(i);
1314
}
1415
}

rustsbi-qemu/src/main.rs

Lines changed: 56 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#![no_std]
22
#![no_main]
33
#![feature(naked_functions, asm_const)]
4-
#![deny(warnings)]
4+
// #![deny(warnings)]
55

66
mod clint;
77
mod device_tree;
8-
mod execute;
98
mod hart_csr_utils;
10-
mod qemu_hsm;
119
mod qemu_test;
1210

1311
mod constants {
@@ -31,9 +29,8 @@ use core::{
3129
sync::atomic::{AtomicBool, Ordering},
3230
};
3331
use device_tree::BoardInfo;
34-
use execute::Operation;
3532
use fast_trap::{
36-
load_direct_trap_entry, reuse_stack_for_trap, FastContext, FastResult, FlowContext,
33+
load_direct_trap_entry, reuse_stack_for_trap, trap_entry, FastContext, FastResult, FlowContext,
3734
FreeTrapStack, TrapStackBlock,
3835
};
3936
use hsm_cell::HsmCell;
@@ -61,29 +58,24 @@ static mut ROOT_STACK: [Stack; NUM_HART_MAX] = [Stack::ZERO; NUM_HART_MAX];
6158
unsafe extern "C" fn _start() -> ! {
6259
asm!(
6360
// 关中断
64-
" csrw mie, zero",
61+
" csrw mie, zero",
6562
// 设置栈
66-
" la sp, {stack}
67-
li t0, {per_hart_stack_size}
68-
csrr t1, mhartid
69-
addi t1, t1, 1
70-
1: add sp, sp, t0
71-
addi t1, t1, -1
72-
bnez t1, 1b
73-
call {move_stack}
74-
",
75-
" call {rust_main}",
76-
// 清理,然后重启或等待
77-
" call {finalize}
78-
bnez a0, _start
79-
1: wfi
80-
j 1b
63+
" la sp, {stack}
64+
li t0, {per_hart_stack_size}
65+
csrr t1, mhartid
66+
addi t1, t1, 1
67+
1: add sp, sp, t0
68+
addi t1, t1, -1
69+
bnez t1, 1b
70+
call {move_stack}
8171
",
72+
" call {rust_main}",
73+
" j {trap}",
8274
per_hart_stack_size = const LEN_STACK_PER_HART,
8375
stack = sym ROOT_STACK,
8476
move_stack = sym reuse_stack_for_trap,
8577
rust_main = sym rust_main,
86-
finalize = sym finalize,
78+
trap = sym trap_entry,
8779
options(noreturn)
8880
)
8981
}
@@ -93,19 +85,19 @@ unsafe extern "C" fn _stop() -> ! {
9385
asm!("wfi", options(noreturn))
9486
}
9587

96-
static HSM: Once<qemu_hsm::QemuHsm> = Once::new();
88+
static mut SBI: MaybeUninit<FixedRustSBI> = MaybeUninit::uninit();
9789

9890
type FixedRustSBI<'a> = RustSBI<
9991
&'a clint::Clint,
10092
&'a clint::Clint,
10193
Infallible,
102-
&'a qemu_hsm::QemuHsm,
94+
Infallible,
10395
&'a qemu_test::QemuTest,
10496
Infallible,
10597
>;
10698

10799
/// rust 入口。
108-
extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
100+
extern "C" fn rust_main(_hartid: usize, opaque: usize) {
109101
static GENESIS: AtomicBool = AtomicBool::new(true);
110102
static BOARD_INFO: Once<BoardInfo> = Once::new();
111103

@@ -124,7 +116,6 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
124116
rcore_console::set_log_level(option_env!("LOG"));
125117
clint::init(board_info.clint.start);
126118
qemu_test::init(board_info.test.start);
127-
HSM.call_once(|| qemu_hsm::QemuHsm::new(NUM_HART_MAX, opaque));
128119
// 打印启动信息
129120
print!(
130121
"\
@@ -149,6 +140,16 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
149140
dtb = board_info.dtb,
150141
firmware = _start as usize,
151142
);
143+
// 初始化 SBI
144+
unsafe {
145+
SBI = MaybeUninit::new(
146+
rustsbi::Builder::new_machine()
147+
.with_ipi(&clint::Clint)
148+
.with_timer(&clint::Clint)
149+
.with_reset(qemu_test::get())
150+
.build(),
151+
);
152+
};
152153
// 设置并打印 pmp
153154
set_pmp(board_info);
154155
hart_csr_utils::print_pmps();
@@ -165,6 +166,9 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
165166
opaque,
166167
});
167168
}
169+
// 清理 clint
170+
clint::msip::clear();
171+
clint::mtimecmp::clear();
168172
} else {
169173
// 设置 pmp
170174
set_pmp(BOARD_INFO.wait());
@@ -184,42 +188,6 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
184188
mie::set_mtimer();
185189
load_direct_trap_entry();
186190
}
187-
188-
let hsm = HSM.wait();
189-
if let Some(supervisor) = hsm.take_supervisor() {
190-
// 初始化 SBI 服务
191-
let sbi = rustsbi::Builder::new_machine()
192-
.with_ipi(&clint::Clint)
193-
.with_timer(&clint::Clint)
194-
.with_reset(qemu_test::get())
195-
.with_hsm(hsm)
196-
.build();
197-
execute::execute_supervisor(sbi, hsm, supervisor)
198-
} else {
199-
Operation::Stop
200-
}
201-
}
202-
203-
/// 准备好不可恢复休眠或关闭
204-
///
205-
/// 在隔离的环境(汇编)调用,以确保 main 中使用的堆资源完全释放。
206-
/// (只是作为示例,因为这个版本完全不使用堆)
207-
unsafe extern "C" fn finalize(op: Operation) -> ! {
208-
match op {
209-
Operation::Stop => {
210-
HSM.wait().finalize_before_stop();
211-
riscv::interrupt::enable();
212-
// 从中断响应直接回 entry
213-
loop {
214-
riscv::asm::wfi();
215-
}
216-
}
217-
Operation::SystemReset => {
218-
// TODO 等待其他硬件线程关闭
219-
// 直接回 entry
220-
_start()
221-
}
222-
}
223191
}
224192

225193
#[inline(always)]
@@ -253,14 +221,14 @@ mod cause {
253221
}
254222

255223
extern "C" fn fast_handler(
256-
ctx: FastContext,
257-
_a1: usize,
258-
_a2: usize,
259-
_a3: usize,
260-
_a4: usize,
261-
_a5: usize,
262-
_a6: usize,
263-
_a7: usize,
224+
mut ctx: FastContext,
225+
a1: usize,
226+
a2: usize,
227+
a3: usize,
228+
a4: usize,
229+
a5: usize,
230+
a6: usize,
231+
a7: usize,
264232
) -> FastResult {
265233
use mcause::{Exception as E, Trap as T};
266234

@@ -279,17 +247,30 @@ extern "C" fn fast_handler(
279247
hart_ctx.trap.a[0] = hart_id;
280248
hart_ctx.trap.a[1] = supervisor.opaque;
281249
hart_ctx.trap.pc = supervisor.start_addr;
250+
println!("trap = {:?}", (&hart_ctx.trap) as *const _);
282251
}
283-
// TODO 检查状态,设置启动参数
284252
Err(_state) => {
285253
hart_ctx.trap.pc = _stop as usize;
254+
println!("{_state:#x}");
286255
}
287256
}
288257
ctx.call(2)
289258
}
290259
_ => unreachable!(),
291260
},
292-
T::Exception(_) | T::Interrupt(_) => todo!(),
261+
T::Exception(E::SupervisorEnvCall) => {
262+
let ret = unsafe { SBI.assume_init_mut() }.handle_ecall(
263+
a7,
264+
a6,
265+
[ctx.a0(), a1, a2, a3, a4, a5],
266+
);
267+
mepc::write(mepc::read() + 4);
268+
ctx.save_args(ret.value, a2, a3, a4, a5, a6, a7);
269+
ctx.write_a(0, ret.error);
270+
ctx.restore()
271+
}
272+
T::Exception(e) => todo!("{e:?}"),
273+
T::Interrupt(i) => todo!("{i:?}"),
293274
}
294275
}
295276

@@ -325,7 +306,9 @@ impl Stack {
325306
}
326307

327308
fn load_as_stack(&'static mut self) {
328-
let ptr = unsafe { NonNull::new_unchecked(&mut self.hart_context().trap) };
309+
let hart = self.hart_context();
310+
hart.hsm = HsmCell::new();
311+
let ptr = unsafe { NonNull::new_unchecked(&mut hart.trap) };
329312
forget(
330313
FreeTrapStack::new(StackRef(self), ptr, fast_handler)
331314
.unwrap()

0 commit comments

Comments
 (0)