Skip to content

Commit dd8e89f

Browse files
committed
Reformat code
1 parent b428030 commit dd8e89f

File tree

4 files changed

+71
-46
lines changed

4 files changed

+71
-46
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ xtask: mode: Debug
3131
[rustsbi] pmp0: 0x10000000 ..= 0x10001fff (rwx)
3232
[rustsbi] pmp1: 0x80000000 ..= 0x8fffffff (rwx)
3333
[rustsbi] pmp2: 0x0 ..= 0xffffffffffffff (---)
34-
D:\Applications\Scoop\apps\qemu\current\qemu-system-riscv64.exe: clint: invalid write: 00000020
3534
[rustsbi] enter supervisor 0x80200000
3635
<< Test-kernel: Hart id = 0, DTB physical address = 0x87000000
3736
>> Test-kernel: Testing base extension

rustsbi-qemu/src/execute.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ pub fn execute_supervisor(supervisor_mepc: usize, a0: usize, a1: usize, hsm: Qem
1919
let ctx = rt.context_mut();
2020
let param = [ctx.a0, ctx.a1, ctx.a2, ctx.a3, ctx.a4, ctx.a5];
2121
let ans = rustsbi::ecall(ctx.a7, ctx.a6, param);
22-
if ans.error == 0x233 { // hart non-retentive resume
22+
if ans.error == 0x233 {
23+
// hart non-retentive resume
2324
if let Some(HsmCommand::Start(start_paddr, opaque)) = hsm.last_command() {
2425
unsafe {
2526
riscv::register::satp::write(0);
@@ -58,7 +59,9 @@ pub fn execute_supervisor(supervisor_mepc: usize, a0: usize, a1: usize, hsm: Qem
5859
mie::clear_mtimer();
5960
},
6061
GeneratorState::Yielded(MachineTrap::MachineSoft()) => match hsm.last_command() {
61-
Some(HsmCommand::Start(_start_paddr, _opaque)) => panic!("rustsbi-qemu: illegal state"),
62+
Some(HsmCommand::Start(_start_paddr, _opaque)) => {
63+
panic!("rustsbi-qemu: illegal state")
64+
}
6265
Some(HsmCommand::Stop) => {
6366
// no hart stop command in qemu, record stop state and pause
6467
hsm.record_current_stop_finished();
@@ -74,8 +77,10 @@ pub fn execute_supervisor(supervisor_mepc: usize, a0: usize, a1: usize, hsm: Qem
7477
ctx.a0 = opaque;
7578
ctx.mepc = start_paddr;
7679
}
77-
},
78-
None => panic!("rustsbi-qemu: machine soft interrupt with no hart state monitor command"),
80+
}
81+
None => panic!(
82+
"rustsbi-qemu: machine soft interrupt with no hart state monitor command"
83+
),
7984
},
8085
GeneratorState::Complete(()) => {
8186
use rustsbi::Reset;

rustsbi-qemu/src/qemu_hsm.rs

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ enum HsmState {
3333
ResumePending = 6,
3434
}
3535

36-
// RustSBI-QEMU hart state monitor structure. It stores hart states for all harts,
37-
// and last command (see HsmCommand) when hart is requested to procceed HSM functions.
36+
// RustSBI-QEMU hart state monitor structure. It stores hart states for all harts,
37+
// and last command (see HsmCommand) when hart is requested to procceed HSM functions.
3838
//
3939
// RustSBI-QEMU makes use of machine software interrupt. Functions should modify `state` to
4040
// XxxPending before the actual procedure began. Then, caller should store next command structure
4141
// to `last_command`, and use IPI to invoke software interrupt on machine level.
42-
//
42+
//
4343
// When target hart received machine software interrupt, it should read and procceed command
4444
// from `last_command`. Then, after command execution makes progress, it should modify
4545
// `state` to Xxxed to mark that the HSM function has taken effect.
4646
//
4747
// These functions above are defined as asynchronous procedures. That means it returns before
48-
// acutal procedure has finished. There are functions to read its current state when the target hart
48+
// acutal procedure has finished. There are functions to read its current state when the target hart
4949
// is still in transition or after the transition is done. These functions may read from `last_command`
5050
// variable at any time.
5151
#[derive(Clone)]
@@ -54,8 +54,8 @@ pub struct QemuHsm {
5454
last_command: Arc<spin::Mutex<HashMap<usize, HsmCommand>>>,
5555
}
5656

57-
// RustSBI-QEMU HSM command, these commands apply to a remote given hart.
58-
//
57+
// RustSBI-QEMU HSM command, these commands apply to a remote given hart.
58+
//
5959
// Should be stored with hart id before software interrupt is invoked.
6060
// After software interrupt is received, the target hart should handle with HSM command structure
6161
// and run corresponding HSM procedures.
@@ -87,7 +87,7 @@ impl QemuHsm {
8787
}
8888
// Record that current hart id is marked as `Stopped` state.
8989
// It is used in interrupt handler, when hart stop command is received. Before this function,
90-
// the target hart is making preparations to stop; it records state and must stop immediately after
90+
// the target hart is making preparations to stop; it records state and must stop immediately after
9191
// this function is called.
9292
pub(crate) fn record_current_stop_finished(&self) {
9393
let hart_id = riscv::register::mhartid::read();
@@ -98,7 +98,7 @@ impl QemuHsm {
9898
}
9999
// Record that current hart id is marked as `Started` state.
100100
// It is used when hart stop command is received in interrupt handler.
101-
// The target hart (when in interrupt handler) is prepared to start, it marks itself into 'started',
101+
// The target hart (when in interrupt handler) is prepared to start, it marks itself into 'started',
102102
// and should jump to target address right away.
103103
pub(crate) fn record_current_start_finished(&self) {
104104
let hart_id = riscv::register::mhartid::read();
@@ -111,7 +111,7 @@ impl QemuHsm {
111111

112112
// Adapt RustSBI interface to RustSBI-QEMU's QemuHsm.
113113
impl rustsbi::Hsm for QemuHsm {
114-
// The supervisor software above RustSBI has called SBI environment to start a given `hart_id`
114+
// The supervisor software above RustSBI has called SBI environment to start a given `hart_id`
115115
// to address `start_addr` with parameter `opaque`.
116116
fn hart_start(&self, hart_id: usize, start_addr: usize, opaque: usize) -> SbiRet {
117117
// previous privileged mode should be user or supervisor; start from machine mode is not supported
@@ -129,21 +129,21 @@ impl rustsbi::Hsm for QemuHsm {
129129
HsmState::StartPending as u8,
130130
Ordering::AcqRel,
131131
Ordering::Acquire,
132-
);
132+
);
133133
// procceed with invalid hart states.
134134
// - the given hartid is already started, the compare exchange should fail and suggests current state as `Started`,
135135
// function should return error as already available.
136136
if current_state == Err(HsmState::Started as u8) {
137-
return SbiRet::already_available()
138-
}
137+
return SbiRet::already_available();
138+
}
139139
// - otherwise return invalid parameter, this may be caused for hart is already transitioning from started state
140140
if current_state != Ok(HsmState::Stopped as u8) {
141141
return SbiRet::invalid_param();
142142
}
143143
// todo: check start address
144144
/* SBI_ERR_INVALID_ADDRESS: start_addr is not valid possibly due to following reasons:
145-
* It is not a valid physical address.
146-
* The address is prohibited by PMP to run in supervisor mode. */
145+
* It is not a valid physical address.
146+
* The address is prohibited by PMP to run in supervisor mode. */
147147
// fill in the parameter
148148
let mut config_lock = self.last_command.lock();
149149
config_lock
@@ -154,8 +154,8 @@ impl rustsbi::Hsm for QemuHsm {
154154
// now, start the target hart
155155
let clint = crate::clint::Clint::new(0x2000000 as *mut u8);
156156
clint.send_soft(hart_id); // this does not block the current function
157-
// The following process is going to be handled in software interrupt handler, and
158-
// the function returns immediately as starting a hart is defined as an asynchronous procedure.
157+
// The following process is going to be handled in software interrupt handler, and
158+
// the function returns immediately as starting a hart is defined as an asynchronous procedure.
159159
SbiRet::ok(0)
160160
}
161161
fn hart_stop(&self, hart_id: usize) -> SbiRet {
@@ -172,7 +172,7 @@ impl rustsbi::Hsm for QemuHsm {
172172
);
173173
// check current hart state
174174
if current_state.is_err() {
175-
return SbiRet::failed() // illegal state
175+
return SbiRet::failed(); // illegal state
176176
}
177177
// fill in the parameter
178178
let mut config_lock = self.last_command.lock();
@@ -187,12 +187,12 @@ impl rustsbi::Hsm for QemuHsm {
187187
fn hart_get_status(&self, hart_id: usize) -> SbiRet {
188188
self.state.lock().get(&hart_id).map_or(
189189
SbiRet::invalid_param(), // not in `state` map structure, the given hart id is invalid
190-
|a| SbiRet::ok(a.load(Ordering::Relaxed) as usize)
190+
|a| SbiRet::ok(a.load(Ordering::Relaxed) as usize),
191191
)
192192
}
193193
// Supervisor requested current hart to suspend.
194194
//
195-
// In RustSBI-QEMU, if `suspend_type` is retentive, it pauses the current hart; `resume_addr`
195+
// In RustSBI-QEMU, if `suspend_type` is retentive, it pauses the current hart; `resume_addr`
196196
// and `opaque` is not used.
197197
// Otherwise, the current hart discards current supervisor context, and returns to another
198198
// `resume_addr` with parameter `opaque`.
@@ -204,24 +204,27 @@ impl rustsbi::Hsm for QemuHsm {
204204
// try to set current target hart state to stop pending
205205
let hart_id = riscv::register::mhartid::read();
206206
let mut state_lock = self.state.lock();
207-
let current_state = state_lock.entry(hart_id)
207+
let current_state = state_lock
208+
.entry(hart_id)
208209
.or_insert(AtomicU8::new(HsmState::Stopped as u8))
209210
.compare_exchange(
210211
HsmState::Started as u8,
211212
HsmState::SuspendPending as u8,
212213
Ordering::AcqRel,
213214
Ordering::Acquire,
214-
);
215+
);
215216
// check current hart state
216217
if current_state.is_err() {
217-
return SbiRet::failed() // illegal state
218+
return SbiRet::failed(); // illegal state
218219
}
219220
drop(state_lock);
220221
// actual suspend begin
221222
suspend_current_hart(&self); // pause and wait for machine level ipi
222-
// mark current hart as started
223+
// mark current hart as started
223224
let mut state_lock = self.state.lock();
224-
state_lock.entry(hart_id).insert(AtomicU8::new(HsmState::Started as u8));
225+
state_lock
226+
.entry(hart_id)
227+
.insert(AtomicU8::new(HsmState::Started as u8));
225228
drop(state_lock);
226229
SbiRet::ok(0)
227230
}
@@ -240,31 +243,37 @@ impl rustsbi::Hsm for QemuHsm {
240243
// try to set current target hart state to stop pending
241244
let hart_id = riscv::register::mhartid::read();
242245
let mut state_lock = self.state.lock();
243-
let current_state = state_lock.entry(hart_id)
246+
let current_state = state_lock
247+
.entry(hart_id)
244248
.or_insert(AtomicU8::new(HsmState::Stopped as u8))
245249
.compare_exchange(
246250
HsmState::Started as u8,
247251
HsmState::SuspendPending as u8,
248252
Ordering::AcqRel,
249253
Ordering::Acquire,
250-
);
254+
);
251255
// check current hart state
252256
if current_state.is_err() {
253-
return SbiRet::failed() // illegal state
257+
return SbiRet::failed(); // illegal state
254258
}
255259
drop(state_lock);
256260
// retentive suspend
257261
suspend_current_hart(&self);
258262
// begin wake process
259263
// send start command to runtime of current hart
260264
let mut config_lock = self.last_command.lock();
261-
config_lock.entry(hart_id).insert(HsmCommand::Start(resume_addr, opaque));
265+
config_lock
266+
.entry(hart_id)
267+
.insert(HsmCommand::Start(resume_addr, opaque));
262268
drop(config_lock);
263-
SbiRet { error: 0x233, value: 0x0 } // unreachable, the runtime identifies start command and perform the hart resume
264-
},
269+
SbiRet {
270+
error: 0x233,
271+
value: 0x0,
272+
} // unreachable, the runtime identifies start command and perform the hart resume
273+
}
265274
// There could be other platform specific suspend types; RustSBI-QEMU does not define any
266275
// platform suspend types. It gives SBI return value as not supported.
267-
_ => SbiRet::not_supported()
276+
_ => SbiRet::not_supported(),
268277
}
269278
}
270279
}
@@ -274,18 +283,20 @@ const SUSPEND_NON_RETENTIVE: u32 = 0x80000000;
274283

275284
// Suspend current hart and record resume state when wake
276285
pub fn suspend_current_hart(hsm: &QemuHsm) {
277-
use riscv::asm::wfi;
278-
use riscv::register::{mie, mip, mhartid};
279286
use crate::clint::Clint;
287+
use riscv::asm::wfi;
288+
use riscv::register::{mhartid, mie, mip};
280289
let hart_id = mhartid::read();
281290
let clint = Clint::new(0x2000000 as *mut u8);
282291
clint.clear_soft(hart_id); // Clear IPI
283292
unsafe { mip::clear_msoft() }; // clear machine software interrupt flag
284293
let prev_msoft = mie::read().msoft();
285294
unsafe { mie::set_msoft() }; // Start listening for software interrupts
286-
// mark current state as suspended
295+
// mark current state as suspended
287296
let mut state_lock = hsm.state.lock();
288-
state_lock.entry(hart_id).insert(AtomicU8::new(HsmState::Suspended as u8));
297+
state_lock
298+
.entry(hart_id)
299+
.insert(AtomicU8::new(HsmState::Suspended as u8));
289300
drop(state_lock);
290301
// actual suspended process
291302
loop {
@@ -296,7 +307,9 @@ pub fn suspend_current_hart(hsm: &QemuHsm) {
296307
}
297308
// mark current state as resume pending
298309
let mut state_lock = hsm.state.lock();
299-
state_lock.entry(hart_id).insert(AtomicU8::new(HsmState::ResumePending as u8));
310+
state_lock
311+
.entry(hart_id)
312+
.insert(AtomicU8::new(HsmState::ResumePending as u8));
300313
drop(state_lock);
301314
// resume
302315
if !prev_msoft {

test-kernel/src/main.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub extern "C" fn rust_main(hartid: usize, dtb_pa: usize) -> ! {
3030
unsafe { stvec::write(start_trap as usize, TrapMode::Direct) };
3131
println!(">> Test-kernel: Trigger illegal exception");
3232
unsafe { asm!("csrw mcycle, x0") }; // mcycle cannot be written, this is always a 4-byte illegal instruction
33-
}
33+
}
3434
if hartid == 0 {
3535
let sbi_ret = sbi::hart_stop(3);
3636
println!(">> Stop hart 3, return value {:?}", sbi_ret);
@@ -40,17 +40,24 @@ pub extern "C" fn rust_main(hartid: usize, dtb_pa: usize) -> ! {
4040
}
4141
} else if hartid == 1 {
4242
let sbi_ret = sbi::hart_suspend(0x00000000, 0, 0);
43-
println!(">> Start test for hart {}, retentive suspend return value {:?}", hartid, sbi_ret);
43+
println!(
44+
">> Start test for hart {}, retentive suspend return value {:?}",
45+
hartid, sbi_ret
46+
);
4447
} else if hartid == 2 {
4548
/* resume_addr should be physical address, and here pa == va */
4649
let sbi_ret = sbi::hart_suspend(0x80000000, hart_2_resume as usize, 0x4567890a);
4750
println!(">> Error for non-retentive suspend: {:?}", sbi_ret);
4851
loop {}
49-
} else { // hartid == 3
52+
} else {
53+
// hartid == 3
5054
loop {}
5155
}
5256
if hartid == 0 {
53-
println!("<< Test-kernel: test for hart {} success, wake another hart", hartid);
57+
println!(
58+
"<< Test-kernel: test for hart {} success, wake another hart",
59+
hartid
60+
);
5461
let bv: usize = 0b10;
5562
let sbi_ret = sbi::send_ipi(&bv as *const _ as usize, hartid); // wake hartid + 1
5663
println!(">> Wake hart 1, sbi return value {:?}", sbi_ret);
@@ -61,7 +68,8 @@ pub extern "C" fn rust_main(hartid: usize, dtb_pa: usize) -> ! {
6168
let sbi_ret = sbi::send_ipi(&bv as *const _ as usize, hartid); // wake hartid + 1
6269
println!(">> Wake hart 2, sbi return value {:?}", sbi_ret);
6370
loop {}
64-
} else { // hartid == 2 || hartid == 3
71+
} else {
72+
// hartid == 2 || hartid == 3
6573
unreachable!()
6674
}
6775
}

0 commit comments

Comments
 (0)