Skip to content

Commit 666f3fd

Browse files
committed
fix: clint send_soft max_hart_id times
- `0..max_hart_id` instead of `0..=max_hart_id` - Format codes with rustfmt
1 parent 4096048 commit 666f3fd

File tree

8 files changed

+101
-63
lines changed

8 files changed

+101
-63
lines changed

rustsbi-qemu/src/execute.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use crate::feature;
2-
use crate::qemu_hsm::{QemuHsm, HsmCommand, pause};
2+
use crate::qemu_hsm::{pause, HsmCommand, QemuHsm};
33
use crate::runtime::{MachineTrap, Runtime, SupervisorContext};
44
use core::{
55
ops::{Generator, GeneratorState},
66
pin::Pin,
77
};
8-
use riscv::register::{mie, mip, scause::{Trap, Exception}};
8+
use riscv::register::{
9+
mie, mip,
10+
scause::{Exception, Trap},
11+
};
912

1013
pub fn execute_supervisor(supervisor_mepc: usize, a0: usize, a1: usize, hsm: QemuHsm) -> ! {
1114
let mut rt = Runtime::new_sbi_supervisor(supervisor_mepc, a0, a1);
@@ -27,7 +30,10 @@ pub fn execute_supervisor(supervisor_mepc: usize, a0: usize, a1: usize, hsm: Qem
2730
if !emulate_illegal_instruction(ctx, ins) {
2831
unsafe {
2932
if feature::should_transfer_trap(ctx) {
30-
feature::do_transfer_trap(ctx, Trap::Exception(Exception::IllegalInstruction))
33+
feature::do_transfer_trap(
34+
ctx,
35+
Trap::Exception(Exception::IllegalInstruction),
36+
)
3137
} else {
3238
fail_illegal_instruction(ctx, ins)
3339
}
@@ -62,13 +68,15 @@ pub fn execute_supervisor(supervisor_mepc: usize, a0: usize, a1: usize, hsm: Qem
6268
unimplemented!("not RISC-V instruction set architecture")
6369
}
6470
};
65-
},
71+
}
6672
Some(HsmCommand::Stop) => {
6773
// no hart stop command in qemu, record stop state and pause
6874
hsm.record_current_stop_finished();
6975
pause();
70-
},
71-
None => panic!("rustsbi-qemu: machine soft interrupt with no hart state monitor command"),
76+
}
77+
None => panic!(
78+
"rustsbi-qemu: machine soft interrupt with no hart state monitor command"
79+
),
7280
},
7381
GeneratorState::Complete(()) => {
7482
use rustsbi::Reset;

rustsbi-qemu/src/feature.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ mod emulate_rdtime;
22
mod transfer_trap;
33

44
pub use emulate_rdtime::emulate_rdtime;
5-
pub use transfer_trap::{should_transfer_trap, do_transfer_trap};
5+
pub use transfer_trap::{do_transfer_trap, should_transfer_trap};

rustsbi-qemu/src/feature/transfer_trap.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::runtime::SupervisorContext;
22
use riscv::register::{
3-
scause, stval, mtval, sepc, mstatus::{self, MPP, SPP}, stvec
3+
mstatus::{self, MPP, SPP},
4+
mtval, scause, sepc, stval, stvec,
45
};
56

67
#[inline]

rustsbi-qemu/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ mod execute;
1616
mod feature;
1717
mod hart_csr_utils;
1818
mod ns16550a;
19+
mod qemu_hsm;
1920
mod runtime;
2021
mod test_device;
21-
mod qemu_hsm;
2222

2323
use buddy_system_allocator::LockedHeap;
2424
use core::panic::PanicInfo;
@@ -74,16 +74,17 @@ extern "C" fn rust_main(hartid: usize, opqaue: usize) -> ! {
7474
}
7575
delegate_interrupt_exception();
7676
set_pmp();
77-
unsafe { // enable wake by ipi
77+
unsafe {
78+
// enable wake by ipi
7879
riscv::register::mstatus::set_mie();
7980
}
8081
if hartid == 0 {
8182
// print hart csr configuration
8283
hart_csr_utils::print_hart_csrs();
8384
// start other harts
8485
let clint = clint::Clint::new(0x2000000 as *mut u8);
85-
let max_hart_id = * { count_harts::MAX_HART_ID.lock() };
86-
for target_hart_id in 0..=max_hart_id {
86+
let max_hart_id = *{ count_harts::MAX_HART_ID.lock() };
87+
for target_hart_id in 0..max_hart_id {
8788
if target_hart_id != 0 {
8889
clint.send_soft(target_hart_id);
8990
}

rustsbi-qemu/src/qemu_hsm.rs

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
//! Hart state monitor designed for QEMU
22
3-
use hashbrown::HashMap;
4-
use rustsbi::SbiRet;
5-
use core::sync::atomic::{AtomicU8, Ordering};
63
use alloc::sync::Arc;
4+
use core::sync::atomic::{AtomicU8, Ordering};
5+
use hashbrown::HashMap;
76
use riscv::register::mstatus::{self, MPP};
7+
use rustsbi::SbiRet;
88

99
#[allow(unused)]
1010
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1111
#[repr(u8)]
1212
enum HsmState {
1313
/// The hart is physically powered-up and executing normally.
1414
Started = 0,
15-
/// The hart is not executing in supervisor-mode or any lower privilege mode.
16-
/// It is probably powered-down by the SBI implementation if the underlying platform has a mechanism
15+
/// The hart is not executing in supervisor-mode or any lower privilege mode.
16+
/// It is probably powered-down by the SBI implementation if the underlying platform has a mechanism
1717
/// to physically power-down harts.
1818
Stopped = 1,
19-
/// Some other hart has requested to start (or power-up) the hart from the STOPPED state
19+
/// Some other hart has requested to start (or power-up) the hart from the STOPPED state
2020
/// and the SBI implementation is still working to get the hart in the STARTED state.
2121
StartPending = 2,
22-
/// The hart has requested to stop (or power-down) itself from the STARTED state
22+
/// The hart has requested to stop (or power-down) itself from the STARTED state
2323
/// and the SBI implementation is still working to get the hart in the STOPPED state.
2424
StopPending = 3,
2525
/// This hart is in a platform specific suspend (or low power) state.
2626
Suspended = 4,
27-
/// The hart has requested to put itself in a platform specific low power state from the STARTED state
27+
/// The hart has requested to put itself in a platform specific low power state from the STARTED state
2828
/// and the SBI implementation is still working to get the hart in the platform specific SUSPENDED state.
2929
SuspendPending = 5,
30-
/// An interrupt or platform specific hardware event has caused the hart to resume normal execution from
30+
/// An interrupt or platform specific hardware event has caused the hart to resume normal execution from
3131
/// the SUSPENDED state and the SBI implementation is still working to get the hart in the STARTED state.
3232
ResumePending = 6,
3333
}
@@ -61,12 +61,16 @@ impl QemuHsm {
6161
}
6262
pub(crate) fn record_current_stop_finished(&self) {
6363
let hart_id = riscv::register::mhartid::read();
64-
self.state.lock().entry(hart_id)
64+
self.state
65+
.lock()
66+
.entry(hart_id)
6567
.insert(AtomicU8::new(HsmState::Stopped as u8));
6668
}
6769
pub(crate) fn record_current_start_finished(&self) {
6870
let hart_id = riscv::register::mhartid::read();
69-
self.state.lock().entry(hart_id)
71+
self.state
72+
.lock()
73+
.entry(hart_id)
7074
.insert(AtomicU8::new(HsmState::Started as u8));
7175
}
7276
}
@@ -76,28 +80,31 @@ impl rustsbi::Hsm for QemuHsm {
7680
// previous privileged mode should be user or supervisor; start from machine mode is not supported
7781
let mpp = mstatus::read().mpp();
7882
if mpp != MPP::Supervisor && mpp != MPP::User {
79-
return SbiRet::invalid_param()
80-
}
83+
return SbiRet::invalid_param();
84+
}
8185
// try to modify state to start hart
8286
let mut state_lock = self.state.lock();
83-
let current_state = state_lock.entry(hart_id)
87+
let current_state = state_lock
88+
.entry(hart_id)
8489
.or_insert(AtomicU8::new(HsmState::Stopped as u8))
8590
.compare_exchange(
8691
HsmState::Stopped as u8,
8792
HsmState::StartPending as u8,
8893
Ordering::AcqRel,
8994
Ordering::Acquire,
90-
);
95+
);
9196
if current_state == Err(HsmState::Started as u8) {
92-
return SbiRet::already_available()
97+
return SbiRet::already_available();
9398
}
9499
// hart is already transitioning from started state
95100
if current_state != Ok(HsmState::Stopped as u8) {
96-
return SbiRet::invalid_param()
101+
return SbiRet::invalid_param();
97102
}
98103
// fill in the parameter
99104
let mut config_lock = self.last_command.lock();
100-
config_lock.entry(hart_id).insert(HsmCommand::Start(start_addr, opaque));
105+
config_lock
106+
.entry(hart_id)
107+
.insert(HsmCommand::Start(start_addr, opaque));
101108
drop(config_lock);
102109
drop(state_lock);
103110
// now, start the target hart
@@ -108,23 +115,24 @@ impl rustsbi::Hsm for QemuHsm {
108115
fn hart_stop(&self, hart_id: usize) -> SbiRet {
109116
// try to set current target hart state to stop pending
110117
let mut state_lock = self.state.lock();
111-
let current_state = state_lock.entry(hart_id)
118+
let current_state = state_lock
119+
.entry(hart_id)
112120
.or_insert(AtomicU8::new(HsmState::Stopped as u8))
113121
.compare_exchange(
114122
HsmState::Started as u8,
115123
HsmState::StopPending as u8,
116124
Ordering::AcqRel,
117125
Ordering::Acquire,
118-
);
126+
);
119127
// check current hart state
120128
if current_state == Err(HsmState::Started as u8) {
121-
return SbiRet::failed() // illegal state
129+
return SbiRet::failed(); // illegal state
122130
}
123131
// fill in the parameter
124132
let mut config_lock = self.last_command.lock();
125133
config_lock.entry(hart_id).insert(HsmCommand::Stop);
126134
drop(config_lock);
127-
drop(state_lock);
135+
drop(state_lock);
128136
// stop the target hart
129137
let clint = crate::clint::Clint::new(0x2000000 as *mut u8);
130138
clint.send_soft(hart_id);
@@ -133,20 +141,20 @@ impl rustsbi::Hsm for QemuHsm {
133141
fn hart_get_status(&self, hart_id: usize) -> SbiRet {
134142
self.state.lock().get(&hart_id).map_or(
135143
SbiRet::invalid_param(), // if given hart is invalid
136-
|a| SbiRet::ok(a.load(Ordering::Relaxed) as usize)
144+
|a| SbiRet::ok(a.load(Ordering::Relaxed) as usize),
137145
)
138146
}
139147
fn hart_suspend(&self, suspend_type: u32, resume_addr: usize, opaque: usize) -> SbiRet {
140-
match suspend_type {
141-
// Resuming from a retentive suspend state is straight forward and the supervisor-mode software
148+
match suspend_type {
149+
// Resuming from a retentive suspend state is straight forward and the supervisor-mode software
142150
// will see SBI suspend call return without any failures.
143151
SUSPEND_RETENTIVE => {
144152
pause(); // pause and wait for machine level ipi
145153
SbiRet::ok(0)
146-
},
147-
// Resuming from a non-retentive suspend state is relatively more involved and requires software
148-
// to restore various hart registers and CSRs for all privilege modes.
149-
// Upon resuming from non-retentive suspend state, the hart will jump to supervisor-mode at address
154+
}
155+
// Resuming from a non-retentive suspend state is relatively more involved and requires software
156+
// to restore various hart registers and CSRs for all privilege modes.
157+
// Upon resuming from non-retentive suspend state, the hart will jump to supervisor-mode at address
150158
// specified by `resume_addr` with specific registers values described in the table below:
151159
//
152160
// | Register Name | Register Value
@@ -178,8 +186,8 @@ impl rustsbi::Hsm for QemuHsm {
178186
unimplemented!("not RISC-V instruction set architecture")
179187
}
180188
};
181-
},
182-
_ => SbiRet::not_supported()
189+
}
190+
_ => SbiRet::not_supported(),
183191
}
184192
}
185193
}
@@ -189,10 +197,10 @@ const SUSPEND_NON_RETENTIVE: u32 = 0x80000000;
189197

190198
// Pause current hart, wake through inter-processor interrupt
191199
pub fn pause() {
192-
use riscv::asm::wfi;
193-
use riscv::register::{mie, mip, mhartid};
194200
use crate::clint::Clint;
195-
unsafe {
201+
use riscv::asm::wfi;
202+
use riscv::register::{mhartid, mie, mip};
203+
unsafe {
196204
let hartid = mhartid::read();
197205
let clint = Clint::new(0x2000000 as *mut u8);
198206
clint.clear_soft(hartid); // Clear IPI
@@ -208,5 +216,5 @@ pub fn pause() {
208216
mie::clear_msoft(); // Stop listening for software interrupts
209217
}
210218
clint.clear_soft(hartid); // Clear IPI
211-
}
212-
}
219+
}
220+
}

test-kernel/src/main.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#[macro_use]
88
mod console;
9-
mod sbi;
109
mod mm;
10+
mod sbi;
1111

1212
use riscv::register::{
1313
scause::{self, Exception, Trap},
@@ -16,7 +16,8 @@ use riscv::register::{
1616
};
1717

1818
pub extern "C" fn rust_main(hartid: usize, dtb_pa: usize) -> ! {
19-
if hartid == 0 { // initialization
19+
if hartid == 0 {
20+
// initialization
2021
mm::init_heap();
2122
}
2223
if hartid == 0 {
@@ -26,7 +27,7 @@ pub extern "C" fn rust_main(hartid: usize, dtb_pa: usize) -> ! {
2627
);
2728
test_base_extension();
2829
test_sbi_ins_emulation();
29-
}
30+
}
3031
if hartid == 0 {
3132
let sbi_ret = sbi::hart_stop(1);
3233
println!(">> Stop hart 1, return value {:?}", sbi_ret);
@@ -36,21 +37,27 @@ pub extern "C" fn rust_main(hartid: usize, dtb_pa: usize) -> ! {
3637
}
3738
} else {
3839
let sbi_ret = sbi::hart_suspend(0x00000000, 0, 0);
39-
println!(">> Start test for hart {}, suspend return value {:?}", hartid, sbi_ret);
40+
println!(
41+
">> Start test for hart {}, suspend return value {:?}",
42+
hartid, sbi_ret
43+
);
4044
}
4145
unsafe { stvec::write(start_trap as usize, TrapMode::Direct) };
4246
println!(">> Test-kernel: Trigger illegal exception");
4347
unsafe { asm!("csrw mcycle, x0") }; // mcycle cannot be written, this is always a 4-byte illegal instruction
4448
if hartid != 3 {
45-
println!("<< Test-kernel: test for hart {} success, wake another hart", hartid);
49+
println!(
50+
"<< Test-kernel: test for hart {} success, wake another hart",
51+
hartid
52+
);
4653
let bv: usize = 0b10;
4754
let sbi_ret = sbi::send_ipi(&bv as *const _ as usize, hartid); // wake hartid + 1
4855
println!(">> Wake, sbi return value {:?}", sbi_ret);
4956
loop {} // wait for machine shutdown
5057
} else {
5158
println!("<< Test-kernel: All hart SBI test SUCCESS, shutdown");
5259
sbi::shutdown()
53-
}
60+
}
5461
}
5562

5663
fn test_base_extension() {
@@ -141,7 +148,7 @@ unsafe extern "C" fn entry() -> ! {
141148
addi t0, t0, %pcrel_lo(1b)
142149
jr t0
143150
",
144-
boot_stack = sym BOOT_STACK,
151+
boot_stack = sym BOOT_STACK,
145152
rust_main = sym rust_main,
146153
options(noreturn))
147154
}

test-kernel/src/mm.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,5 @@ static mut HEAP_SPACE: [u8; HEAP_SIZE] = [0; HEAP_SIZE];
77
static HEAP: LockedHeap<32> = LockedHeap::empty();
88

99
pub fn init_heap() {
10-
unsafe {
11-
HEAP
12-
.lock()
13-
.init(HEAP_SPACE.as_ptr() as usize, HEAP_SIZE)
14-
}
10+
unsafe { HEAP.lock().init(HEAP_SPACE.as_ptr() as usize, HEAP_SIZE) }
1511
}

0 commit comments

Comments
 (0)