Skip to content

Commit cf82dbb

Browse files
authored
Merge pull request #18 from luojia65/main
Adapts to RustSBI 0.2.2
2 parents a2b0505 + 0b1b5d9 commit cf82dbb

File tree

10 files changed

+84
-57
lines changed

10 files changed

+84
-57
lines changed

CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
99

1010
### Added
1111

12+
### Modified
13+
14+
## [0.1.1] - 2022-03-23
15+
16+
### Added
17+
18+
- Adapts to RustSBI version 0.2.2, RISC-V SBI version 1.0.0 ratified
1219
- Handle possible failure of deref virtual address by machine trap detection
1320

1421
### Modified
1522

23+
- Use Rust Edition 2021
24+
- Modify test kernel message
25+
1626
## [0.1.0] - 2022-02-13
1727

1828
### Added
@@ -23,6 +33,6 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
2333
- Fixes on usage of CLINT peripheral, thanks to @duskmoon314
2434
- Numerous fixes to HSM module implementation, more documents
2535

26-
[Unreleased]: https://github.com/rustsbi/rustsbi-qemu/compare/v0.1.0...HEAD
27-
36+
[Unreleased]: https://github.com/rustsbi/rustsbi-qemu/compare/v0.1.1...HEAD
37+
[0.1.1]: https://github.com/rustsbi/rustsbi-qemu/compare/v0.1.0...v0.1.1
2838
[0.1.0]: https://github.com/rustsbi/rustsbi-qemu/releases/tag/v0.1.0

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ cargo qemu
1313

1414
When running `cargo qemu`, the test kernel will build and run. Expected output should be:
1515

16-
```
17-
[rustsbi] RustSBI version 0.2.0, adapting to RISC-V SBI v0.3
16+
[rustsbi] RustSBI version 0.2.2, adapting to RISC-V SBI v0.3
1817
.______ __ __ _______.___________. _______..______ __
1918
| _ \ | | | | / | | / || _ \ | |
2019
| |_) | | | | | | (----`---| |----`| (----`| |_) || |
2120
| / | | | | \ \ | | \ \ | _ < | |
2221
| |\ \----.| `--' |.----) | | | .----) | | |_) || |
2322
| _| `._____| \______/ |_______/ |__| |_______/ |______/ |__|
2423

25-
[rustsbi] Implementation: RustSBI-QEMU Version 0.1.0
24+
[rustsbi] Implementation: RustSBI-QEMU Version 0.1.1
2625
[rustsbi-dtb] Hart count: cluster0 with 8 cores
2726
[rustsbi] misa: RV64ACDFIMSU
2827
[rustsbi] mideleg: ssoft, stimer, sext (0x222)
@@ -35,15 +34,15 @@ When running `cargo qemu`, the test kernel will build and run. Expected output s
3534
<< Test-kernel: Hart id = 0, DTB physical address = 0x87000000
3635
>> Test-kernel: Testing base extension
3736
<< Test-kernel: Base extension version: 1
38-
<< Test-kernel: SBI specification version: 3
37+
<< Test-kernel: SBI specification version: 1.0
3938
<< Test-kernel: SBI implementation Id: 4
40-
<< Test-kernel: SBI implementation version: 200
39+
<< Test-kernel: SBI implementation version: 202
4140
<< Test-kernel: Device mvendorid: 0
4241
<< Test-kernel: Device marchid: 0
4342
<< Test-kernel: Device mimpid: 0
4443
>> Test-kernel: Testing SBI instruction emulation
45-
<< Test-kernel: Current time: 17fc45
46-
<< Test-kernel: Time after operation: 187678
44+
<< Test-kernel: Current time: d78c9
45+
<< Test-kernel: Time after operation: da00f
4746
>> Test-kernel: Trigger illegal exception
4847
<< Test-kernel: Value of scause: Exception(IllegalInstruction)
4948
<< Test-kernel: Illegal exception delegate success
@@ -52,6 +51,7 @@ When running `cargo qemu`, the test kernel will build and run. Expected output s
5251
>> Hart 1 state return value: 4
5352
>> Hart 2 state return value: 4
5453
>> Hart 3 state return value: 1
54+
>> Hart 4 state return value: 0
5555
<< Test-kernel: test for hart 0 success, wake another hart
5656
>> Wake hart 1, sbi return value 0
5757
>> Start test for hart 1, retentive suspend return value 0

rustsbi-qemu/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
[package]
22
name = "rustsbi-qemu"
3-
version = "0.1.0"
4-
edition = "2018"
3+
version = "0.1.1"
4+
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
rustsbi = "0.2.1"
9+
rustsbi = "0.2.2"
1010
buddy_system_allocator = "0.8"
1111
lazy_static = { version = "1", features = ["spin_no_std"] }
1212
spin = "0.9"
1313
riscv = { git = "https://github.com/rust-embedded/riscv", rev = "dc0bc37e", features = ["inline-asm"] }
1414
device_tree = { git = "https://github.com/rcore-os/device_tree-rs/" }
15-
embedded-hal = "0.2.6"
15+
embedded-hal = "0.2.7"
1616
nb = "1"
1717
bitflags = "1"
1818
bit_field = "0.10"
19-
hashbrown = "0.11"
19+
hashbrown = "0.12"

rustsbi-qemu/src/execute.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ use core::{
33
pin::Pin,
44
};
55

6-
use riscv::register::{mcause, mie, mip, scause::{Exception, Trap}};
7-
use riscv::register::scause::Interrupt;
6+
use riscv::register::{
7+
mcause, mie, mip,
8+
scause::{Exception, Interrupt, Trap},
9+
};
810

911
use crate::feature;
1012
use crate::prv_mem::{self, SupervisorPointer};
11-
use crate::qemu_hsm::{HsmCommand, pause, QemuHsm};
13+
use crate::qemu_hsm::{pause, HsmCommand, QemuHsm};
1214
use crate::runtime::{MachineTrap, Runtime, SupervisorContext};
1315

1416
pub fn execute_supervisor(supervisor_mepc: usize, hart_id: usize, a1: usize, hsm: QemuHsm) -> ! {
@@ -102,10 +104,7 @@ pub fn execute_supervisor(supervisor_mepc: usize, hart_id: usize, a1: usize, hsm
102104
let clint = crate::clint::Clint::new(0x2000000 as *mut u8);
103105
clint.clear_soft(hart_id); // Clear IPI
104106
if feature::should_transfer_trap(ctx) {
105-
feature::do_transfer_trap(
106-
ctx,
107-
Trap::Interrupt(Interrupt::SupervisorSoft),
108-
)
107+
feature::do_transfer_trap(ctx, Trap::Interrupt(Interrupt::SupervisorSoft))
109108
} else {
110109
panic!("rustsbi-qemu: machine soft interrupt with no hart state monitor command")
111110
}
@@ -140,7 +139,13 @@ fn fail_illegal_instruction(ctx: &mut SupervisorContext, ins: usize) -> ! {
140139

141140
fn fail_cant_read_exception_address(ctx: &mut SupervisorContext, cause: mcause::Exception) -> ! {
142141
#[cfg(target_pointer_width = "64")]
143-
panic!("can't read exception address, cause: {:?}, mepc: {:016x?}, context: {:016x?}", cause, ctx.mepc, ctx);
142+
panic!(
143+
"can't read exception address, cause: {:?}, mepc: {:016x?}, context: {:016x?}",
144+
cause, ctx.mepc, ctx
145+
);
144146
#[cfg(target_pointer_width = "32")]
145-
panic!("can't read exception address, cause: {:?}, mepc: {:08x?}, context: {:08x?}", cause, ctx.mepc, ctx);
147+
panic!(
148+
"can't read exception address, cause: {:?}, mepc: {:08x?}, context: {:08x?}",
149+
cause, ctx.mepc, ctx
150+
);
146151
}

rustsbi-qemu/src/main.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extern "C" fn rust_main(hartid: usize, opqaue: usize) -> ! {
6464
init_clint();
6565
init_test_device();
6666
println!(
67-
"[rustsbi] RustSBI version {}, adapting to RISC-V SBI v0.3",
67+
"[rustsbi] RustSBI version {}, adapting to RISC-V SBI v1.0.0",
6868
rustsbi::VERSION
6969
);
7070
println!("{}", rustsbi::LOGO);
@@ -180,16 +180,14 @@ fn set_pmp() {
180180
// A = NA4(naturally aligned 4-byte region, 2), only support a 4-byte pmp region
181181
// A = NAPOT(naturally aligned power-of-two region, 3), support a >=8-byte pmp region
182182
// When using NAPOT to match a address range [S,S+L), then the pmpaddr_i should be set to (S>>2)|((L>>2)-1)
183-
let calc_pmpaddr = |start_addr: usize, length: usize| {
184-
(start_addr >> 2) | ((length >> 2) - 1)
185-
};
183+
let calc_pmpaddr = |start_addr: usize, length: usize| (start_addr >> 2) | ((length >> 2) - 1);
186184
let mut pmpcfg0: usize = 0;
187185
// pmp region 0: RW, A=NAPOT, address range {0x1000_1000, 0x1000}, VIRT_VIRTIO
188186
// address range {0x1000_0000, 0x100}, VIRT_UART0
189187
// aligned address range {0x1000_0000, 0x2000}
190-
pmpcfg0 |= 0b11011;
188+
pmpcfg0 |= 0b11011;
191189
let pmpaddr0 = calc_pmpaddr(0x1000_0000, 0x2000);
192-
// pmp region 1: RW, A=NAPOT, address range {0x200_0000, 0x1_0000}, VIRT_CLINT
190+
// pmp region 1: RW, A=NAPOT, address range {0x200_0000, 0x1_0000}, VIRT_CLINT
193191
pmpcfg0 |= 0b11011 << 8;
194192
let pmpaddr1 = calc_pmpaddr(0x200_0000, 0x1_0000);
195193
// pmp region 2: RW, A=NAPOT, address range {0xC00_0000, 0x40_0000}, VIRT_PLIC
@@ -211,7 +209,7 @@ fn set_pmp() {
211209
in(reg) pmpaddr1,
212210
in(reg) pmpaddr2,
213211
in(reg) pmpaddr3,
214-
);
212+
);
215213
}
216214
}
217215

rustsbi-qemu/src/prv_mem.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
use core::arch::asm;
1212
use core::mem::{self, MaybeUninit};
1313

14-
use riscv::register::{mcause::{Exception, Mcause, Trap}, mcause, mstatus, mtvec::{self, Mtvec, TrapMode}};
14+
use riscv::register::{
15+
mcause::{self, Exception, Mcause, Trap},
16+
mstatus,
17+
mtvec::{self, Mtvec, TrapMode},
18+
};
1519

1620
/// Pointer at supervisor level
1721
///
@@ -44,20 +48,22 @@ pub unsafe fn try_read<T>(src: SupervisorPointer<T>) -> Result<T, mcause::Except
4448
panic!("rustsbi-qemu: mprv should be cleared before try_read")
4549
}
4650
for idx in (0..mem::size_of::<T>()).step_by(mem::size_of::<u32>()) {
47-
let nr = with_detect_trap(0, || asm!(
48-
"li {mprv_bit}, (1 << 17)",
49-
"csrs mstatus, {mprv_bit}",
50-
"lw {word}, 0({in_s_addr})",
51-
"csrc mstatus, {mprv_bit}",
52-
"sw {word}, 0({out_m_addr})",
53-
mprv_bit = out(reg) _,
54-
word = out(reg) _,
55-
in_s_addr = in(reg) src.inner.cast::<u8>().add(idx),
56-
out_m_addr = in(reg) ans.as_mut_ptr().cast::<u8>().add(idx),
57-
options(nostack),
58-
));
51+
let nr = with_detect_trap(0, || {
52+
asm!(
53+
"li {mprv_bit}, (1 << 17)",
54+
"csrs mstatus, {mprv_bit}",
55+
"lw {word}, 0({in_s_addr})",
56+
"csrc mstatus, {mprv_bit}",
57+
"sw {word}, 0({out_m_addr})",
58+
mprv_bit = out(reg) _,
59+
word = out(reg) _,
60+
in_s_addr = in(reg) src.inner.cast::<u8>().add(idx),
61+
out_m_addr = in(reg) ans.as_mut_ptr().cast::<u8>().add(idx),
62+
options(nostack),
63+
)
64+
});
5965
if nr != 0 {
60-
return Err(Exception::from(nr))
66+
return Err(Exception::from(nr));
6167
}
6268
}
6369
Ok(ans.assume_init())

rustsbi-qemu/src/qemu_pmu.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

test-kernel/src/main.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
use core::arch::asm;
88
use core::panic::PanicInfo;
99

10-
use riscv::register::{scause::{self, Exception, Trap}, sepc, /*sie, sstatus, */stvec::{self, TrapMode}};
1110
use riscv::register::scause::Interrupt;
11+
use riscv::register::{
12+
scause::{self, Exception, Trap},
13+
sepc,
14+
/*sie, sstatus, */ stvec::{self, TrapMode},
15+
};
1216

1317
#[macro_use]
1418
mod console;
@@ -123,9 +127,12 @@ fn test_base_extension() {
123127
sbi::shutdown()
124128
}
125129
println!("<< Test-kernel: Base extension version: {:x}", base_version);
130+
let spec_version = sbi::get_spec_version();
131+
let major = (spec_version >> 24) & 0x7F;
132+
let minor = spec_version & 0xFFFFFF;
126133
println!(
127-
"<< Test-kernel: SBI specification version: {:x}",
128-
sbi::get_spec_version()
134+
"<< Test-kernel: SBI specification version: {}.{}",
135+
major, minor
129136
);
130137
println!(
131138
"<< Test-kernel: SBI implementation Id: {:x}",
@@ -322,4 +329,4 @@ struct TrapFrame {
322329
a6: usize,
323330
a7: usize,
324331
tp: usize,
325-
}
332+
}

xtask/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{
77
process::{self, Command, Stdio},
88
};
99

10-
1110
// 不要修改DEFAULT_TARGET;如果你需要编译到别的目标,请使用--target编译选项!
1211
const DEFAULT_TARGET: &'static str = "riscv64imac-unknown-none-elf";
1312

@@ -255,7 +254,8 @@ fn xtask_gdb(xtask_env: &XtaskEnv) {
255254

256255
ctrlc::set_handler(move || {
257256
// when ctrl-c, don't exit gdb
258-
}).expect("disable Ctrl-C exit");
257+
})
258+
.expect("disable Ctrl-C exit");
259259

260260
let status = command.status().expect("run program");
261261
if !status.success() {

0 commit comments

Comments
 (0)