Skip to content

Commit 8eed873

Browse files
Merge pull request #371 from fortify-iq/picorv32
Feature-gate csr instructions
2 parents 78c9298 + 7d9d09d commit 8eed873

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

riscv-rt/CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- New `no-mhartid` feature to load 0 to `a0` instead of reading `mhartid`.
13+
- New `no-xtvec` feature that removes interrupt stuff.
14+
1015
### Changed
1116

1217
- Update license to `MIT or Apache-2.0`
@@ -23,7 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2328
### Added
2429

2530
- New `post-init` feature to run a Rust `__post_init` function before jumping to `main`.
26-
- New `#[riscv_rt::post_init]` attribute to aid in the definition of the `__post_init` function.
31+
- New `#[riscv_rt::post_init]` attribute to aid in the definition of the `__post_init` function.
2732
- Added `.uninit` section to the linker file. Due to its similarities with `.bss`, the
2833
linker will place this new section in `REGION_BSS`.
2934
- Additional feature `no-xie-xip` to work on chips without the XIE and XIP CSRs (e.g. ESP32-C2, ESP32-C3)
@@ -78,7 +83,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
7883
- New `device` feature to include `device.x` in `link.x`. This feature is based
7984
on the current implementation of `cortex-m-rt`.
8085
- New `memory` feature to include `memory.x` in `link.x`. This feature is based
81-
on the current implementation of `cortex-m-rt`. However, in contrast with
86+
on the current implementation of `cortex-m-rt`. However, in contrast with
8287
`cortex-m-rt`, including `memory.x` in the linker file is feature gated.
8388
The benefits of leaving this optional are backwards compatibility and
8489
allowing users to define less typical linker scripts that do not rely on a

riscv-rt/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ v-trap = ["riscv-rt-macros/v-trap", "riscv/rt-v-trap"]
4242
u-boot = ["riscv-rt-macros/u-boot", "single-hart"]
4343
no-interrupts = []
4444
no-exceptions = []
45+
no-mhartid = ["single-hart"]
4546
no-xie-xip = []
47+
no-xtvec = []
4648
device = []
4749
memory = []
4850
defmt = ["dep:defmt"]

riscv-rt/src/asm.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,26 @@ _abs_start:
6868
#[cfg(all(feature = "s-mode", not(feature = "no-xie-xip")))]
6969
"csrw sie, 0
7070
csrw sip, 0",
71-
#[cfg(all(not(feature = "s-mode"), not(feature = "no-xie-xip")))]
72-
"csrw mie, 0
73-
csrw mip, 0",
7471
#[cfg(not(feature = "s-mode"))]
75-
"csrr a0, mhartid", // Make sure that the hart ID is in a0 in M-mode
72+
{
73+
#[cfg(not(feature = "no-xie-xip"))]
74+
"csrw mie, 0
75+
csrw mip, 0",
76+
// Make sure that the hart ID is in a0 in M-mode
77+
#[cfg(not(feature = "no-mhartid"))]
78+
"csrr a0, mhartid",
79+
#[cfg(feature = "no-mhartid")]
80+
"li a0, 0",
81+
}
7682
// Set pre-init trap vector
77-
"la t0, _pre_init_trap",
78-
#[cfg(feature = "s-mode")]
79-
"csrw stvec, t0",
80-
#[cfg(not(feature = "s-mode"))]
81-
"csrw mtvec, t0",
83+
#[cfg(not(feature = "no-xtvec"))]
84+
{
85+
"la t0, _pre_init_trap",
86+
#[cfg(feature = "s-mode")]
87+
"csrw stvec, t0",
88+
#[cfg(not(feature = "s-mode"))]
89+
"csrw mtvec, t0",
90+
}
8291
// If multi-hart, assert that hart ID is valid
8392
#[cfg(not(feature = "single-hart"))]
8493
"lui t0, %hi(_max_hart_id)

riscv-rt/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,15 @@
565565
//!
566566
//! Saves a little code size if there is only one hart on the target.
567567
//!
568+
//! ## `no-mhartid`
569+
//!
570+
//! Skips reading `mhartid` and uses 0 instead. Useful for targets that doesn't implement this instruction.
571+
//! Automatically enables `single-hart`.
572+
//!
573+
//! ## `no-xtvec`
574+
//!
575+
//! Skips interrupts setup.
576+
//!
568577
//! ## `s-mode`
569578
//!
570579
//! Supervisor mode. While most registers/instructions have variants for both `mcause` and
@@ -729,9 +738,9 @@ pub unsafe extern "Rust" fn setup_interrupts() {
729738

730739
let xtvec_val = match () {
731740
#[cfg(not(feature = "v-trap"))]
732-
_ => Xtvec::new(_start_trap as usize, TrapMode::Direct),
741+
_ => Xtvec::new(_start_trap as *const () as usize, TrapMode::Direct),
733742
#[cfg(feature = "v-trap")]
734-
_ => Xtvec::new(_vector_table as usize, TrapMode::Vectored),
743+
_ => Xtvec::new(_vector_table as *const () as usize, TrapMode::Vectored),
735744
};
736745
xtvec::write(xtvec_val);
737746
}

0 commit comments

Comments
 (0)