Skip to content

fix: making rustsbi-qemu completely runnable again. #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Xtask will now print error when system does not have qemu installed
- Fix dtb parsing for qemu 7.2

## [0.1.0] - 2022-02-13

### Added

- Adapts to RustSBI version 0.2.0
- Implement SBI non-retentive resume procedure
- PMP updates, use stabilized core::arch::asm! macro, thanks to @wyfcyx
- Fixes on usage of CLINT peripheral, thanks to @duskmoon314
- Numerous fixes to HSM module implementation, more documents

## [0.1.1] - 2022-03-23

### Added
Expand All @@ -45,15 +55,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Use Rust Edition 2021
- Modify test kernel message

## [0.1.0] - 2022-02-13
## [0.1.2] - 2025-05-16

### Added
### Modified

- Adapts to RustSBI version 0.2.0
- Implement SBI non-retentive resume procedure
- PMP updates, use stabilized core::arch::asm! macro, thanks to @wyfcyx
- Fixes on usage of CLINT peripheral, thanks to @duskmoon314
- Numerous fixes to HSM module implementation, more documents
- Bump fast-trap version to 0.1.0
- Bump aclint version to 0.1.0

### Fixed

- Reorder date for CHANGELOG.md
- Replace #[naked] with #[unsafe(naked)] across all relevant functions.
- Remove `#![feature(naked_functions, asm_const)]` as they are no longer needed.
- Switche to the `naked_asm!` macro to comply with new naked function requirements.
- Remove `options(noreturn)`, which is not allowed in `naked_asm!`.
- Fix warning `creating a mutable/shared reference to mutable static` in multiple files.

[Unreleased]: https://github.com/rustsbi/rustsbi-qemu/compare/v0.1.1...HEAD
[0.1.1]: https://github.com/rustsbi/rustsbi-qemu/compare/v0.1.0...v0.1.1
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions bench-kernel/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#![no_std]
#![no_main]
#![feature(naked_functions, asm_const)]
#![deny(warnings)]

use rcore_console::log;
use riscv::register::*;
use sbi_rt::*;
use uart16550::Uart16550;

#[naked]
#[unsafe(naked)]
#[no_mangle]
#[link_section = ".text.entry"]
unsafe extern "C" fn _start(hartid: usize, device_tree_paddr: usize) -> ! {
Expand All @@ -17,13 +16,12 @@ unsafe extern "C" fn _start(hartid: usize, device_tree_paddr: usize) -> ! {
#[link_section = ".bss.uninit"]
static mut STACK: [u8; STACK_SIZE] = [0u8; STACK_SIZE];

core::arch::asm!(
core::arch::naked_asm!(
"la sp, {stack} + {stack_size}",
"j {main}",
stack_size = const STACK_SIZE,
stack = sym STACK,
main = sym rust_main,
options(noreturn),
)
}

Expand Down Expand Up @@ -127,11 +125,17 @@ impl Uart16550Map {
impl rcore_console::Console for Console {
#[inline]
fn put_char(&self, c: u8) {
unsafe { UART.get().write(core::slice::from_ref(&c)) };
unsafe {
let mut_ref_uart = (*(&raw mut UART)).get();
mut_ref_uart.write(core::slice::from_ref(&c));
}
}

#[inline]
fn put_str(&self, s: &str) {
unsafe { UART.get().write(s.as_bytes()) };
unsafe {
let mut_ref_uart = (*(&raw mut UART)).get();
mut_ref_uart.write(s.as_bytes());
}
}
}
4 changes: 2 additions & 2 deletions rustsbi-qemu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ sbi-spec = { version = "0.0.7", features = ["legacy"] }
riscv = "0.10.1"
spin = "0.9"
rcore-console = "0.0.0"
aclint = "0.0.0"
aclint = "0.1.0"
sifive-test-device = "0.0.0"
dtb-walker = "=0.2.0-alpha.3"
uart16550 = "0.0.1"

hsm-cell = { path = "../hsm-cell" }
fast-trap = { version = "=0.0.1", features = ["riscv-m"] }
fast-trap = { version = "=0.1.0", features = ["riscv-m"] }
32 changes: 16 additions & 16 deletions rustsbi-qemu/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![no_std]
#![no_main]
#![feature(naked_functions, asm_const)]
#![deny(warnings)]

mod clint;
Expand All @@ -27,15 +26,13 @@ extern crate rcore_console;

use constants::*;
use core::{
arch::asm,
mem::MaybeUninit,
sync::atomic::{AtomicBool, Ordering},
arch::{asm, naked_asm}, mem::MaybeUninit, sync::atomic::{AtomicBool, Ordering}
};
use device_tree::BoardInfo;
use fast_trap::{FastContext, FastResult};
use riscv_spec::*;
use rustsbi::{RustSBI, SbiRet};
use spin::Once;
use spin::{Mutex, Once};
use trap_stack::{local_hsm, local_remote_hsm, remote_hsm};
use trap_vec::trap_vec;

Expand All @@ -44,19 +41,18 @@ use trap_vec::trap_vec;
/// # Safety
///
/// 裸函数。
#[naked]
#[unsafe(naked)]
#[no_mangle]
#[link_section = ".text.entry"]
unsafe extern "C" fn _start() -> ! {
asm!(
naked_asm!(
" call {locate_stack}
call {rust_main}
j {trap}
",
locate_stack = sym trap_stack::locate,
rust_main = sym rust_main,
trap = sym trap_vec,
options(noreturn),
)
}

Expand Down Expand Up @@ -112,13 +108,14 @@ extern "C" fn rust_main(hartid: usize, opaque: usize) {
firmware = _start as usize,
);
// 初始化 SBI
let sbi = SBI.lock().as_mut_ptr();
unsafe {
SBI = MaybeUninit::new(FixedRustSBI {
*sbi = FixedRustSBI {
clint: &clint::Clint,
hsm: Hsm,
reset: qemu_test::get(),
dbcn: dbcn::get(),
});
};
}
// 设置并打印 pmp
set_pmp(board_info);
Expand Down Expand Up @@ -222,11 +219,14 @@ extern "C" fn fast_handler(
// SBI call
T::Exception(E::SupervisorEnvCall) => {
use sbi_spec::{base, hsm, legacy};
let mut ret = unsafe { SBI.assume_init_mut() }.handle_ecall(
a7,
a6,
[ctx.a0(), a1, a2, a3, a4, a5],
);
let mut ret = unsafe {
let sbi = SBI.lock().as_mut_ptr();
(*sbi).handle_ecall(
a7,
a6,
[ctx.a0(), a1, a2, a3, a4, a5],
)
};
if ret.is_ok() {
match (a7, a6) {
// 关闭
Expand Down Expand Up @@ -337,7 +337,7 @@ impl rcore_console::Console for Console {
}
}

static mut SBI: MaybeUninit<FixedRustSBI> = MaybeUninit::uninit();
static SBI: Mutex<MaybeUninit<FixedRustSBI>> = Mutex::new(MaybeUninit::uninit());

#[derive(RustSBI)]
struct FixedRustSBI<'a> {
Expand Down
43 changes: 24 additions & 19 deletions rustsbi-qemu/src/trap_stack.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
use crate::{fast_handler, hart_id, Supervisor, LEN_STACK_PER_HART, NUM_HART_MAX};
use core::{mem::forget, ptr::NonNull};
use core::{cell::UnsafeCell, mem::forget, ptr::NonNull};
use fast_trap::{FlowContext, FreeTrapStack};
use hsm_cell::{HsmCell, LocalHsmCell, RemoteHsmCell};

#[repr(transparent)]
struct HartLocal(UnsafeCell<Stack>);

unsafe impl Sync for HartLocal {}

/// 栈空间。
#[link_section = ".bss.uninit"]
static mut ROOT_STACK: [Stack; NUM_HART_MAX] = [Stack::ZERO; NUM_HART_MAX];
static ROOT_STACK: [HartLocal; NUM_HART_MAX] = {
const INIT: HartLocal = HartLocal(UnsafeCell::new(Stack::ZERO));
[INIT; NUM_HART_MAX]
};

/// 定位每个 hart 的栈。
#[naked]
#[unsafe(naked)]
pub(crate) unsafe extern "C" fn locate() {
core::arch::asm!(
core::arch::naked_asm!(
" la sp, {stack}
li t0, {per_hart_stack_size}
csrr t1, mhartid
Expand All @@ -24,43 +32,40 @@ pub(crate) unsafe extern "C" fn locate() {
per_hart_stack_size = const LEN_STACK_PER_HART,
stack = sym ROOT_STACK,
move_stack = sym fast_trap::reuse_stack_for_trap,
options(noreturn),
)
}

/// 预备陷入栈。
pub(crate) fn prepare_for_trap() {
unsafe { ROOT_STACK.get_unchecked_mut(hart_id()).load_as_stack() };
unsafe {
let stack = &mut *ROOT_STACK.get_unchecked(hart_id()).0.get();
stack.load_as_stack()
};
}

/// 获取此 hart 的 local hsm 对象。
pub(crate) fn local_hsm() -> LocalHsmCell<'static, Supervisor> {
unsafe {
ROOT_STACK
.get_unchecked_mut(hart_id())
.hart_context()
.hsm
.local()
let stack = &mut *ROOT_STACK.get_unchecked(hart_id()).0.get();
stack.hart_context().hsm.local()
}
}

/// 获取此 hart 的 remote hsm 对象。
pub(crate) fn local_remote_hsm() -> RemoteHsmCell<'static, Supervisor> {
unsafe {
ROOT_STACK
.get_unchecked_mut(hart_id())
.hart_context()
.hsm
.remote()
let stack = &mut *ROOT_STACK.get_unchecked(hart_id()).0.get();
stack.hart_context().hsm.remote()
}
}

/// 获取任意 hart 的 remote hsm 对象。
pub(crate) fn remote_hsm(hart_id: usize) -> Option<RemoteHsmCell<'static, Supervisor>> {
unsafe {
ROOT_STACK
.get_mut(hart_id)
.map(|x| x.hart_context().hsm.remote())
ROOT_STACK.get(hart_id).map(|cell| {
let stack = &mut *cell.0.get();
stack.hart_context().hsm.remote()
})
}
}

Expand Down
17 changes: 7 additions & 10 deletions rustsbi-qemu/src/trap_vec.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use crate::clint::CLINT;
use aclint::SifiveClint as Clint;
use core::arch::asm;
use core::arch::naked_asm;
use fast_trap::trap_entry;

/// 中断向量表
///
/// # Safety
///
/// 裸函数。
#[naked]
#[unsafe(naked)]
pub(crate) unsafe extern "C" fn trap_vec() {
asm!(
naked_asm!(
".align 2",
".option push",
".option norvc",
Expand All @@ -30,7 +30,6 @@ pub(crate) unsafe extern "C" fn trap_vec() {
default = sym trap_entry,
mtimer = sym mtimer,
msoft = sym msoft,
options(noreturn)
)
}

Expand All @@ -39,9 +38,9 @@ pub(crate) unsafe extern "C" fn trap_vec() {
/// # Safety
///
/// 裸函数。
#[naked]
#[unsafe(naked)]
unsafe extern "C" fn mtimer() {
asm!(
naked_asm!(
// 换栈:
// sp : M sp
// mscratch: S sp
Expand Down Expand Up @@ -81,7 +80,6 @@ unsafe extern "C" fn mtimer() {
clint_ptr = sym CLINT,
// Clint::write_mtimecmp_naked(&self, hart_idx, val)
set_mtimecmp = sym Clint::write_mtimecmp_naked,
options(noreturn)
)
}

Expand All @@ -90,9 +88,9 @@ unsafe extern "C" fn mtimer() {
/// # Safety
///
/// 裸函数。
#[naked]
#[unsafe(naked)]
unsafe extern "C" fn msoft() {
asm!(
naked_asm!(
// 换栈:
// sp : M sp
// mscratch: S sp
Expand Down Expand Up @@ -125,6 +123,5 @@ unsafe extern "C" fn msoft() {
clint_ptr = sym CLINT,
// Clint::clear_msip_naked(&self, hart_idx)
clear_msip = sym Clint::clear_msip_naked,
options(noreturn)
)
}
Loading