Skip to content

Commit 9c3b131

Browse files
committed
cleanup
Signed-off-by: YdrMaster <ydrml@hotmail.com>
1 parent aacf2af commit 9c3b131

File tree

5 files changed

+88
-134
lines changed

5 files changed

+88
-134
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1919
- Use crate aclint version 0.0.0 in rustsbi-qemu for aclint structs
2020
- Use crate os-xtask-utils version 0.0.0 in xtask builder
2121
- Use crate sifive-test-device version 0.0.0 instead of qemu-exit
22-
- Use `wfi` without enable mie
22+
- Use `wfi` for suspend and stop without enable mie
2323

2424
### Fixed
2525

hsm-cell/src/lib.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,17 @@ impl<T> LocalHsmCell<'_, T> {
6666
) {
6767
Ok(_) => break Ok(unsafe { (*self.0.val.get()).take().unwrap() }),
6868
Err(HART_STATE_START_PENDING_EXT) => spin_loop(),
69-
Err(HART_STATE_SUSPENDED) => {
70-
self.0.status.store(HART_STATE_STARTED, Ordering::Relaxed);
71-
break Ok(unsafe { (*self.0.val.get()).take().unwrap() });
72-
}
7369
Err(s) => break Err(s),
7470
}
7571
}
7672
}
7773

78-
/// 关闭。
79-
#[inline]
80-
pub fn stop_pending(&self) {
81-
self.0
82-
.status
83-
.store(HART_STATE_STOP_PENDING, Ordering::Relaxed)
84-
}
85-
8674
/// 关闭。
8775
#[inline]
8876
pub fn stop(&self) {
8977
self.0.status.store(HART_STATE_STOPPED, Ordering::Release)
9078
}
9179

92-
/// 关闭。
93-
#[inline]
94-
pub fn suspend_non_retentive(&self, t: T) {
95-
unsafe { *self.0.val.get() = Some(t) };
96-
self.0.status.store(HART_STATE_SUSPENDED, Ordering::Relaxed)
97-
}
98-
9980
/// 关闭。
10081
#[inline]
10182
pub fn suspend(&self) {

rustsbi-qemu/src/clint.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ pub fn set_msip(hart_idx: usize) {
4141
unsafe { &*CLINT.load(Ordering::Relaxed) }.set_msip(hart_idx);
4242
}
4343

44+
#[inline]
45+
pub fn clear_msip() {
46+
unsafe { &*CLINT.load(Ordering::Relaxed) }.clear_msip(hart_id());
47+
}
48+
4449
#[inline]
4550
pub fn clear() {
4651
loop {

rustsbi-qemu/src/main.rs

Lines changed: 82 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use riscv_spec::*;
3636
use rustsbi::{spec::binary::SbiRet, RustSBI};
3737
use spin::{Mutex, Once};
3838
use trap_stack::{local_hsm, local_remote_hsm, remote_hsm};
39+
use trap_vec::trap_vec;
3940
use uart_16550::MmioSerialPort;
4041

4142
/// 入口。
@@ -54,16 +55,11 @@ unsafe extern "C" fn _start() -> ! {
5455
",
5556
locate_stack = sym trap_stack::locate,
5657
rust_main = sym rust_main,
57-
trap = sym trap_vec::trap_vec,
58+
trap = sym trap_vec,
5859
options(noreturn)
5960
)
6061
}
6162

62-
#[naked]
63-
unsafe extern "C" fn _stop() -> ! {
64-
asm!("0: wfi", "j 0b", options(noreturn))
65-
}
66-
6763
/// rust 入口。
6864
extern "C" fn rust_main(hartid: usize, opaque: usize) {
6965
static GENESIS: AtomicBool = AtomicBool::new(true);
@@ -145,12 +141,13 @@ extern "C" fn rust_main(hartid: usize, opaque: usize) {
145141
clint::clear();
146142
// 准备启动调度
147143
unsafe {
148-
asm!("csrw mcause, {}", in(reg) cause::BOOT);
149144
asm!("csrw mideleg, {}", in(reg) !0);
150145
asm!("csrw medeleg, {}", in(reg) !0);
151146
asm!("csrw mcounteren, {}", in(reg) !0);
152-
riscv::register::medeleg::clear_supervisor_env_call();
153-
riscv::register::medeleg::clear_machine_env_call();
147+
use riscv::register::{medeleg, mtvec};
148+
medeleg::clear_supervisor_env_call();
149+
medeleg::clear_machine_env_call();
150+
mtvec::write(trap_vec as _, mtvec::TrapMode::Vectored);
154151
}
155152
}
156153

@@ -181,10 +178,6 @@ fn set_pmp(board_info: &BoardInfo) {
181178
}
182179
}
183180

184-
mod cause {
185-
pub(crate) const BOOT: usize = 24;
186-
}
187-
188181
extern "C" fn fast_handler(
189182
mut ctx: FastContext,
190183
a1: usize,
@@ -196,123 +189,105 @@ extern "C" fn fast_handler(
196189
a7: usize,
197190
) -> FastResult {
198191
use riscv::register::{
199-
mcause::{self, Exception as E, Interrupt as I, Trap as T},
200-
mtval,
192+
mcause::{self, Exception as E, Trap as T},
193+
mtval, satp, sstatus,
201194
};
202195

203-
let cause = mcause::read();
204-
// 启动
205-
if (cause.cause() == T::Exception(E::Unknown) && cause.bits() == cause::BOOT)
206-
|| cause.cause() == T::Interrupt(I::MachineSoft)
207-
{
208-
let hart_id = hart_id();
196+
#[inline]
197+
fn boot(mut ctx: FastContext, start_addr: usize, opaque: usize) -> FastResult {
198+
unsafe {
199+
sstatus::clear_sie();
200+
satp::write(0);
201+
}
202+
ctx.regs().a[0] = hart_id();
203+
ctx.regs().a[1] = opaque;
204+
ctx.regs().pc = start_addr;
205+
ctx.call(2)
206+
}
207+
loop {
209208
match local_hsm().start() {
210209
Ok(supervisor) => {
211210
mstatus::update(|bits| {
212211
*bits &= !mstatus::MPP;
213212
*bits |= mstatus::MPIE | mstatus::MPP_SUPERVISOR;
214213
});
215214
mie::write(mie::MSIE | mie::MTIE);
216-
trap_vec::load(true);
217-
unsafe {
218-
riscv::register::sstatus::clear_sie();
219-
riscv::register::satp::write(0);
220-
}
221-
ctx.regs().a[0] = hart_id;
222-
ctx.regs().a[1] = supervisor.opaque;
223-
ctx.regs().pc = supervisor.start_addr;
215+
break boot(ctx, supervisor.start_addr, supervisor.opaque);
224216
}
225-
Err(_state) => {
226-
mstatus::update(|bits| {
227-
*bits &= !mstatus::MPP;
228-
*bits |= mstatus::MPIE | mstatus::MPP_MACHINE;
229-
});
217+
Err(sbi_spec::hsm::HART_STOP) => {
230218
mie::write(mie::MSIE);
231-
trap_vec::load(false);
232-
ctx.regs().pc = _stop as usize;
219+
unsafe { riscv::asm::wfi() };
220+
clint::clear_msip();
233221
}
234-
}
235-
return ctx.call(2);
236-
}
237-
match cause.cause() {
238-
// SBI call
239-
T::Exception(E::SupervisorEnvCall) => {
240-
use rustsbi::spec::{base, hsm, legacy};
241-
let mut ret = unsafe { SBI.assume_init_mut() }.handle_ecall(
242-
a7,
243-
a6,
244-
[ctx.a0(), a1, a2, a3, a4, a5],
245-
);
246-
if ret.is_ok() {
247-
match a7 {
248-
hsm::EID_HSM => {
249-
// 关闭
250-
if a6 == hsm::HART_STOP {
251-
local_hsm().stop();
252-
mie::write(mie::MSIE);
253-
trap_vec::load(false);
254-
ctx.regs().pc = _stop as _;
255-
return ctx.call(0);
222+
_ => match mcause::read().cause() {
223+
// SBI call
224+
T::Exception(E::SupervisorEnvCall) => {
225+
use rustsbi::spec::{base, hsm, legacy};
226+
let mut ret = unsafe { SBI.assume_init_mut() }.handle_ecall(
227+
a7,
228+
a6,
229+
[ctx.a0(), a1, a2, a3, a4, a5],
230+
);
231+
if ret.is_ok() {
232+
match (a7, a6) {
233+
// 关闭
234+
(hsm::EID_HSM, hsm::HART_STOP) => continue,
235+
// 不可恢复挂起
236+
(hsm::EID_HSM, hsm::HART_SUSPEND)
237+
if matches!(
238+
ctx.a0() as u32,
239+
hsm::HART_SUSPEND_TYPE_NON_RETENTIVE
240+
) =>
241+
{
242+
break boot(ctx, a1, a2);
243+
}
244+
// legacy console 探测
245+
(base::EID_BASE, base::PROBE_EXTENSION)
246+
if matches!(
247+
ctx.a0(),
248+
legacy::LEGACY_CONSOLE_PUTCHAR | legacy::LEGACY_CONSOLE_GETCHAR
249+
) =>
250+
{
251+
ret.value = 1;
252+
}
253+
_ => {}
256254
}
257-
// 不可恢复挂起
258-
if a6 == hsm::HART_SUSPEND
259-
&& ctx.a0() == hsm::HART_SUSPEND_TYPE_NON_RETENTIVE as usize
260-
{
261-
unsafe {
262-
riscv::register::sstatus::clear_sie();
263-
riscv::register::satp::write(0);
255+
} else {
256+
match a7 {
257+
legacy::LEGACY_CONSOLE_PUTCHAR => {
258+
print!("{}", ctx.a0() as u8 as char);
259+
ret.error = 0;
260+
ret.value = a1;
264261
}
265-
ctx.regs().a[0] = hart_id();
266-
ctx.regs().a[1] = a2;
267-
ctx.regs().pc = a1;
268-
return ctx.call(0);
262+
legacy::LEGACY_CONSOLE_GETCHAR => {
263+
ret.error = unsafe { UART.lock().assume_init_mut() }.receive() as _;
264+
ret.value = a1;
265+
}
266+
_ => {}
269267
}
270268
}
271-
base::EID_BASE
272-
if a6 == base::PROBE_EXTENSION
273-
&& matches!(
274-
ctx.a0(),
275-
legacy::LEGACY_CONSOLE_PUTCHAR | legacy::LEGACY_CONSOLE_GETCHAR
276-
) =>
277-
{
278-
ret.value = 1;
279-
}
280-
_ => {}
269+
ctx.regs().a = [ret.error, ret.value, a2, a3, a4, a5, a6, a7];
270+
mepc::next();
271+
break ctx.restore();
281272
}
282-
} else {
283-
match a7 {
284-
legacy::LEGACY_CONSOLE_PUTCHAR => {
285-
print!("{}", ctx.a0() as u8 as char);
286-
ret.error = 0;
287-
ret.value = a1;
288-
}
289-
legacy::LEGACY_CONSOLE_GETCHAR => {
290-
ret.error = unsafe { UART.lock().assume_init_mut() }.receive() as _;
291-
ret.value = a1;
292-
}
293-
_ => {}
294-
}
295-
}
296-
ctx.regs().a = [ret.error, ret.value, a2, a3, a4, a5, a6, a7];
297-
mepc::next();
298-
ctx.restore()
299-
}
300-
// 其他陷入
301-
trap => {
302-
println!(
303-
"
273+
// 其他陷入
274+
trap => {
275+
println!(
276+
"
304277
-----------------------------
305278
> trap: {trap:?}
306279
> mstatus: {:#018x}
307280
> mepc: {:#018x}
308281
> mtval: {:#018x}
309282
-----------------------------
310283
",
311-
mstatus::read(),
312-
mepc::read(),
313-
mtval::read()
314-
);
315-
panic!("stopped with unsupported trap")
284+
mstatus::read(),
285+
mepc::read(),
286+
mtval::read()
287+
);
288+
panic!("stopped with unsupported trap")
289+
}
290+
},
316291
}
317292
}
318293
}
@@ -386,7 +361,7 @@ impl rustsbi::Hsm for Hsm {
386361

387362
#[inline]
388363
fn hart_stop(&self) -> SbiRet {
389-
local_hsm().stop_pending();
364+
local_hsm().stop();
390365
SbiRet::success(0)
391366
}
392367

rustsbi-qemu/src/trap_vec.rs

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

136
/// 中断向量表
147
///

0 commit comments

Comments
 (0)