Skip to content

Commit f2b1dea

Browse files
Merge branch 'master' into new_build
2 parents b84edd6 + 0383469 commit f2b1dea

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

riscv-rt/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
- New GitHub workflow for checking invalid labels in PRs
1313
- New GitHub workflow for checking modifications on CHANGELOG.md
1414
- New GitHub workflow for checking clippy lints in PRs
15+
- Optional cargo feature `single-hart` for single CPU targets
1516

1617
### Changed
1718

@@ -20,6 +21,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2021
- `start_trap_rust` is now marked as `unsafe`
2122
- Implement `r0` as inline assembly
2223
- Use `${ARCH_WIDTH}` in `link.x.in` to adapt to different archs
24+
- mhartid CSR is no longer read in single-hart mode, assumed zero
25+
- Ensure stack pointer is 16-byte aligned before jumping to Rust entry point
2326

2427
## [v0.11.0] - 2023-01-18
2528

riscv-rt/src/asm.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,16 @@ _abs_start:
8585
.option push
8686
.option norelax
8787
la gp, __global_pointer$
88-
.option pop",
88+
.option pop
89+
// Allocate stacks",
8990
#[cfg(all(not(feature = "single-hart"), feature = "s-mode"))]
9091
"mv t2, a0 // the hartid is passed as parameter by SMODE",
9192
#[cfg(all(not(feature = "single-hart"), not(feature = "s-mode")))]
9293
"csrr t2, mhartid",
9394
#[cfg(not(feature = "single-hart"))]
9495
"lui t0, %hi(_max_hart_id)
9596
add t0, t0, %lo(_max_hart_id)
96-
bgtu t2, t0, abort",
97-
"// Allocate stacks
98-
la sp, _stack_start
97+
bgtu t2, t0, abort
9998
lui t0, %hi(_hart_stack_size)
10099
add t0, t0, %lo(_hart_stack_size)",
101100
#[cfg(all(not(feature = "single-hart"), riscvm))]
@@ -109,8 +108,10 @@ _abs_start:
109108
addi t1, t1, -1
110109
bnez t1, 1b
111110
2: ",
112-
"sub sp, sp, t0
113-
111+
"la t1, _stack_start",
112+
#[cfg(not(feature = "single-hart"))]
113+
"sub t1, t1, t0",
114+
"andi sp, t1, -16 // Force 16-byte alignment
114115
// Set frame pointer
115116
add s0, sp, zero
116117
@@ -135,6 +136,8 @@ _abs_start:
135136
#[rustfmt::skip]
136137
macro_rules! trap_handler {
137138
($STORE:ident, $LOAD:ident, $BYTES:literal, $TRAP_SIZE:literal, [$(($REG:ident, $LOCATION:literal)),*]) => {
139+
// ensure we do not break that sp is 16-byte aligned
140+
const _: () = assert!(($TRAP_SIZE * $BYTES) % 16 == 0);
138141
global_asm!(
139142
"
140143
.section .trap, \"ax\"

riscv-rt/src/lib.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@
327327
//!
328328
//! ## `single-hart`
329329
//!
330-
//! This feature saves a little code size by removing unnecessary stack space calculation if there is only one hart on the target.
330+
//! This feature saves a little code size if there is only one hart on the target.
331331
//!
332332
//! ## `s-mode`
333333
//!
@@ -372,7 +372,10 @@ use core::sync::atomic::{compiler_fence, Ordering};
372372
use riscv::register::{scause as xcause, stvec as xtvec, stvec::TrapMode as xTrapMode};
373373

374374
#[cfg(not(feature = "s-mode"))]
375-
use riscv::register::{mcause as xcause, mhartid, mtvec as xtvec, mtvec::TrapMode as xTrapMode};
375+
use riscv::register::{mcause as xcause, mtvec as xtvec, mtvec::TrapMode as xTrapMode};
376+
377+
#[cfg(all(not(feature = "single-hart"), not(feature = "s-mode")))]
378+
use riscv::register::mhartid;
376379

377380
pub use riscv_rt_macros::{entry, pre_init};
378381

@@ -404,13 +407,20 @@ pub unsafe extern "C" fn start_rust(a0: usize, a1: usize, a2: usize) -> ! {
404407
fn _mp_hook(hartid: usize) -> bool;
405408
}
406409

407-
// sbi passes hartid as first parameter (a0)
408-
#[cfg(feature = "s-mode")]
409-
let hartid = a0;
410-
#[cfg(not(feature = "s-mode"))]
411-
let hartid = mhartid::read();
410+
#[cfg(not(feature = "single-hart"))]
411+
let run_init = {
412+
// sbi passes hartid as first parameter (a0)
413+
#[cfg(feature = "s-mode")]
414+
let hartid = a0;
415+
#[cfg(not(feature = "s-mode"))]
416+
let hartid = mhartid::read();
417+
418+
_mp_hook(hartid)
419+
};
420+
#[cfg(feature = "single-hart")]
421+
let run_init = true;
412422

413-
if _mp_hook(hartid) {
423+
if run_init {
414424
__pre_init();
415425

416426
// Initialize RAM
@@ -661,6 +671,7 @@ pub unsafe extern "Rust" fn default_pre_init() {}
661671
#[doc(hidden)]
662672
#[no_mangle]
663673
#[rustfmt::skip]
674+
#[cfg(not(feature = "single-hart"))]
664675
pub extern "Rust" fn default_mp_hook(hartid: usize) -> bool {
665676
match hartid {
666677
0 => true,

0 commit comments

Comments
 (0)