Skip to content

Commit 47290a2

Browse files
committed
style(sbi-rt): 增加一些常量定义,改名
1 parent 12eb25a commit 47290a2

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

sbi-rt/src/binary.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,28 @@ pub struct SbiRet {
1414
pub value: usize,
1515
}
1616

17-
pub const SBI_SUCCESS: usize = 0;
18-
pub const SBI_ERR_FAILED: usize = -1isize as _;
19-
pub const SBI_ERR_NOT_SUPPORTED: usize = -2isize as _;
20-
pub const SBI_ERR_INVALID_PARAM: usize = -3isize as _;
21-
pub const SBI_ERR_DENIED: usize = -4isize as _;
22-
pub const SBI_ERR_INVALID_ADDRESS: usize = -5isize as _;
23-
pub const SBI_ERR_ALREADY_AVAILABLE: usize = -6isize as _;
24-
pub const SBI_ERR_ALREADY_STARTED: usize = -7isize as _;
25-
pub const SBI_ERR_ALREADY_STOPPED: usize = -8isize as _;
17+
pub const RET_SUCCESS: usize = 0;
18+
pub const RET_ERR_FAILED: usize = -1isize as _;
19+
pub const RET_ERR_NOT_SUPPORTED: usize = -2isize as _;
20+
pub const RET_ERR_INVALID_PARAM: usize = -3isize as _;
21+
pub const RET_ERR_DENIED: usize = -4isize as _;
22+
pub const RET_ERR_INVALID_ADDRESS: usize = -5isize as _;
23+
pub const RET_ERR_ALREADY_AVAILABLE: usize = -6isize as _;
24+
pub const RET_ERR_ALREADY_STARTED: usize = -7isize as _;
25+
pub const RET_ERR_ALREADY_STOPPED: usize = -8isize as _;
2626

2727
impl core::fmt::Debug for SbiRet {
2828
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2929
match self.error {
30-
SBI_SUCCESS => self.value.fmt(f),
31-
SBI_ERR_FAILED => write!(f, "<SBI call failed>"),
32-
SBI_ERR_NOT_SUPPORTED => write!(f, "<SBI feature not supported>"),
33-
SBI_ERR_INVALID_PARAM => write!(f, "<SBI invalid parameter>"),
34-
SBI_ERR_DENIED => write!(f, "<SBI denied>"),
35-
SBI_ERR_INVALID_ADDRESS => write!(f, "<SBI invalid address>"),
36-
SBI_ERR_ALREADY_AVAILABLE => write!(f, "<SBI already available>"),
37-
SBI_ERR_ALREADY_STARTED => write!(f, "<SBI already started>"),
38-
SBI_ERR_ALREADY_STOPPED => write!(f, "<SBI already stopped>"),
30+
RET_SUCCESS => self.value.fmt(f),
31+
RET_ERR_FAILED => write!(f, "<SBI call failed>"),
32+
RET_ERR_NOT_SUPPORTED => write!(f, "<SBI feature not supported>"),
33+
RET_ERR_INVALID_PARAM => write!(f, "<SBI invalid parameter>"),
34+
RET_ERR_DENIED => write!(f, "<SBI denied>"),
35+
RET_ERR_INVALID_ADDRESS => write!(f, "<SBI invalid address>"),
36+
RET_ERR_ALREADY_AVAILABLE => write!(f, "<SBI already available>"),
37+
RET_ERR_ALREADY_STARTED => write!(f, "<SBI already started>"),
38+
RET_ERR_ALREADY_STOPPED => write!(f, "<SBI already stopped>"),
3939
unknown => write!(f, "[SBI Unknown error: {unknown:#x}]"),
4040
}
4141
}
@@ -56,15 +56,15 @@ pub enum Error {
5656
impl SbiRet {
5757
pub const fn result(&self) -> Result<usize, Error> {
5858
match self.error {
59-
SBI_SUCCESS => Ok(self.value),
60-
SBI_ERR_FAILED => Err(Error::Failed),
61-
SBI_ERR_NOT_SUPPORTED => Err(Error::NotSupported),
62-
SBI_ERR_INVALID_PARAM => Err(Error::InvalidParam),
63-
SBI_ERR_DENIED => Err(Error::Denied),
64-
SBI_ERR_INVALID_ADDRESS => Err(Error::InvalidAddress),
65-
SBI_ERR_ALREADY_AVAILABLE => Err(Error::AlreadyAvailable),
66-
SBI_ERR_ALREADY_STARTED => Err(Error::AlreadyStarted),
67-
SBI_ERR_ALREADY_STOPPED => Err(Error::AlreadyStopped),
59+
RET_SUCCESS => Ok(self.value),
60+
RET_ERR_FAILED => Err(Error::Failed),
61+
RET_ERR_NOT_SUPPORTED => Err(Error::NotSupported),
62+
RET_ERR_INVALID_PARAM => Err(Error::InvalidParam),
63+
RET_ERR_DENIED => Err(Error::Denied),
64+
RET_ERR_INVALID_ADDRESS => Err(Error::InvalidAddress),
65+
RET_ERR_ALREADY_AVAILABLE => Err(Error::AlreadyAvailable),
66+
RET_ERR_ALREADY_STARTED => Err(Error::AlreadyStarted),
67+
RET_ERR_ALREADY_STOPPED => Err(Error::AlreadyStopped),
6868
unknown => Err(Error::Customed(unknown as _)),
6969
}
7070
}

sbi-rt/src/hsm.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const FID_HART_STOP: usize = 1;
99
const FID_HART_GET_STATUS: usize = 2;
1010
const FID_HART_SUSPEND: usize = 3;
1111

12+
pub const HART_STATE_STARTED: usize = 0;
13+
pub const HART_STATE_STOPPED: usize = 1;
14+
pub const HART_STATE_START_PENDING: usize = 2;
15+
pub const HART_STATE_STOP_PENDING: usize = 3;
16+
pub const HART_STATE_SUSPENDED: usize = 4;
17+
pub const HART_STATE_SUSPEND_PENDING: usize = 5;
18+
pub const HART_STATE_RESUME_PENDING: usize = 6;
19+
1220
#[inline]
1321
pub fn hart_start(hartid: usize, start_addr: usize, opaque: usize) -> SbiRet {
1422
sbi_call_3(EID_HSM, FID_HART_START, hartid, start_addr, opaque)

test-kernel/src/test.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub(crate) fn start_stop_harts(hartid: usize, smp: usize) {
118118
if id != hartid {
119119
println!("[test-kernel] Hart{id} is booting...");
120120
let ret = sbi::hart_start(id, test_start_entry as usize, 0);
121-
if ret.error != sbi::SBI_SUCCESS {
121+
if ret.error != sbi::RET_SUCCESS {
122122
panic!("[test-kernel] Start hart{id} failed: {ret:?}");
123123
}
124124
} else {
@@ -130,14 +130,17 @@ pub(crate) fn start_stop_harts(hartid: usize, smp: usize) {
130130
print!("[test-kernel] All harts boot successfully!\n");
131131
// 等待副核关闭
132132
for id in 0..smp {
133-
const STOPPED: sbi::SbiRet = sbi::SbiRet { error: 0, value: 1 };
133+
const STOPPED: sbi::SbiRet = sbi::SbiRet {
134+
error: sbi::RET_SUCCESS,
135+
value: sbi::HART_STATE_STOPPED,
136+
};
134137
if id != hartid {
135-
let status = sbi::hart_get_status(id);
136-
if status != STOPPED {
137-
println!("[test-kernel] Hart{id} waiting: {:?}.", status);
138-
} else {
139-
println!("[test-kernel] Hart{id} stopped.");
138+
while sbi::hart_get_status(id) != STOPPED {
139+
core::hint::spin_loop();
140140
}
141+
println!("[test-kernel] Hart{id} stopped.");
142+
} else {
143+
println!("[test-kernel] Hart{id} is the primary hart.");
141144
}
142145
}
143146
println!("[test-kernel] All harts stop successfully!");

0 commit comments

Comments
 (0)