From a906d89cc18a3fa2d7919976022923056a2db197 Mon Sep 17 00:00:00 2001 From: Kevin Vigor Date: Thu, 2 Nov 2023 10:24:56 -0600 Subject: [PATCH] Ignore hartid in single-hart mode. Don't bother checking hart ID on startup in single-hart mode. Allows use of cores with unusual mhartid values and saves a few instructions. --- CHANGELOG.md | 1 + src/lib.rs | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a68160..c3557b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Removed bors in favor of GitHub Merge Queue - `start_trap_rust` is now marked as `unsafe` - Implement `r0` as inline assembly +- mhartid CSR is no longer read in single-hart mode, assumed zero ## [v0.11.0] - 2023-01-18 diff --git a/src/lib.rs b/src/lib.rs index 3e86c49..422f3f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -372,7 +372,10 @@ use core::sync::atomic::{compiler_fence, Ordering}; use riscv::register::{scause as xcause, stvec as xtvec, stvec::TrapMode as xTrapMode}; #[cfg(not(feature = "s-mode"))] -use riscv::register::{mcause as xcause, mhartid, mtvec as xtvec, mtvec::TrapMode as xTrapMode}; +use riscv::register::{mcause as xcause, mtvec as xtvec, mtvec::TrapMode as xTrapMode}; + +#[cfg(all(not(feature = "single-hart"), not(feature = "s-mode")))] +use riscv::register::mhartid; pub use riscv_rt_macros::{entry, pre_init}; @@ -404,13 +407,20 @@ pub unsafe extern "C" fn start_rust(a0: usize, a1: usize, a2: usize) -> ! { fn _mp_hook(hartid: usize) -> bool; } - // sbi passes hartid as first parameter (a0) - #[cfg(feature = "s-mode")] - let hartid = a0; - #[cfg(not(feature = "s-mode"))] - let hartid = mhartid::read(); + #[cfg(not(feature = "single-hart"))] + let run_init = { + // sbi passes hartid as first parameter (a0) + #[cfg(feature = "s-mode")] + let hartid = a0; + #[cfg(not(feature = "s-mode"))] + let hartid = mhartid::read(); + + _mp_hook(hartid) + }; + #[cfg(feature = "single-hart")] + let run_init = true; - if _mp_hook(hartid) { + if run_init { __pre_init(); // Initialize RAM @@ -661,6 +671,7 @@ pub unsafe extern "Rust" fn default_pre_init() {} #[doc(hidden)] #[no_mangle] #[rustfmt::skip] +#[cfg(not(feature = "single-hart"))] pub extern "Rust" fn default_mp_hook(hartid: usize) -> bool { match hartid { 0 => true,