Skip to content

Add pre-default-start-trap feature #337

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

Merged
merged 5 commits into from
Aug 9, 2025
Merged
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
2 changes: 1 addition & 1 deletion riscv-rt/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
linker will place this new section in `REGION_BSS`.
- Additional feature `no-xie-xip` to work on chips without the XIE and XIP CSRs (e.g. ESP32-C2, ESP32-C3)
- Additional feature `defmt` which will implement `defmt::Format` on certain types

- Additional feature `pre-default-start-trap` to execute custom code before `_default_start_trap`
### Changed

- `main` function no longer needs to be close to `_start`. A linker script may copy
Expand Down
1 change: 1 addition & 0 deletions riscv-rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ no-xie-xip = []
device = []
memory = []
defmt = ["dep:defmt"]
pre-default-start-trap = ["riscv-rt-macros/pre-default-start-trap"]
1 change: 1 addition & 0 deletions riscv-rt/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ syn = { version = "2.0", features = ["extra-traits", "full"] }
s-mode = []
v-trap = []
u-boot = []
pre-default-start-trap = []
11 changes: 11 additions & 0 deletions riscv-rt/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,13 +606,24 @@ pub fn default_start_trap(_input: TokenStream) -> TokenStream {
#[cfg(not(feature = "s-mode"))]
let ret = "mret";

let pre_default_start_trap = if cfg!(feature = "pre-default-start-trap") {
r#"
j _pre_default_start_trap
.global _pre_default_start_trap_ret
_pre_default_start_trap_ret:
"#
} else {
""
};

format!(
r#"
core::arch::global_asm!(
".section .trap.start, \"ax\"
.balign 4 /* Alignment required for xtvec */
.global _default_start_trap
_default_start_trap:
{pre_default_start_trap}
addi sp, sp, - {trap_size} * {width}
{store}
add a0, sp, zero
Expand Down
32 changes: 32 additions & 0 deletions riscv-rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,38 @@
//! because when booting from elf, U-boot passes `argc` and `argv`. This feature also implies `single-hart`.
//! The only way to get boot-hart is through fdt, so other harts initialization is up to you.
//!
//! ## `pre-default-start-trap`
//!
//! This provides a mechanism to execute custom code prior to `_default_start_trap`.
//!
//! To use it, the user must define a symbol named `_pre_default_start_trap`, which the system will jump to.
//! After executing the custom code, control should return by jumping to `_pre_default_start_trap_ret`.
//!
//! It's recommended to place the code in the `.trap.start` section to make sure it's reachable from `_default_start_trap`.
//!
//! It is expected that the custom code does not clobber any registers.
//!
//! Please note that your code won't be run for interrupts in vectored mode.
//!
//! ### Example
//!
//! ```rust,no_run
//! core::arch::global_asm!(
//! r#"
//! .section .trap.start, "ax"
//! .extern _pre_default_start_trap_ret
//! .global _pre_default_start_trap
//!
//! _pre_default_start_trap:
//!
//! // your code goes here remember to not clobber any registers,
//! // use mscratch to retain a single register if needed
//!
//! // jump back to continue with _default_start_trap
//! j _pre_default_start_trap_ret
//! "#
//! );
//! ```
//! [attr-entry]: attr.entry.html
//! [attr-exception]: attr.exception.html
//! [attr-external-interrupt]: attr.external_interrupt.html
Expand Down
Loading