Skip to content

Commit 6c0c255

Browse files
Merge pull request #337 from bjoernQ/pre_default_start_trap
Add pre-default-start-trap feature
2 parents 4bd25de + 429ba4b commit 6c0c255

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

riscv-rt/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
linker will place this new section in `REGION_BSS`.
1616
- Additional feature `no-xie-xip` to work on chips without the XIE and XIP CSRs (e.g. ESP32-C2, ESP32-C3)
1717
- Additional feature `defmt` which will implement `defmt::Format` on certain types
18-
18+
- Additional feature `pre-default-start-trap` to execute custom code before `_default_start_trap`
1919
### Changed
2020

2121
- `main` function no longer needs to be close to `_start`. A linker script may copy

riscv-rt/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ no-xie-xip = []
4646
device = []
4747
memory = []
4848
defmt = ["dep:defmt"]
49+
pre-default-start-trap = ["riscv-rt-macros/pre-default-start-trap"]

riscv-rt/macros/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ syn = { version = "2.0", features = ["extra-traits", "full"] }
2525
s-mode = []
2626
v-trap = []
2727
u-boot = []
28+
pre-default-start-trap = []

riscv-rt/macros/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,24 @@ pub fn default_start_trap(_input: TokenStream) -> TokenStream {
606606
#[cfg(not(feature = "s-mode"))]
607607
let ret = "mret";
608608

609+
let pre_default_start_trap = if cfg!(feature = "pre-default-start-trap") {
610+
r#"
611+
j _pre_default_start_trap
612+
.global _pre_default_start_trap_ret
613+
_pre_default_start_trap_ret:
614+
"#
615+
} else {
616+
""
617+
};
618+
609619
format!(
610620
r#"
611621
core::arch::global_asm!(
612622
".section .trap.start, \"ax\"
613623
.balign 4 /* Alignment required for xtvec */
614624
.global _default_start_trap
615625
_default_start_trap:
626+
{pre_default_start_trap}
616627
addi sp, sp, - {trap_size} * {width}
617628
{store}
618629
add a0, sp, zero

riscv-rt/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,38 @@
598598
//! because when booting from elf, U-boot passes `argc` and `argv`. This feature also implies `single-hart`.
599599
//! The only way to get boot-hart is through fdt, so other harts initialization is up to you.
600600
//!
601+
//! ## `pre-default-start-trap`
602+
//!
603+
//! This provides a mechanism to execute custom code prior to `_default_start_trap`.
604+
//!
605+
//! To use it, the user must define a symbol named `_pre_default_start_trap`, which the system will jump to.
606+
//! After executing the custom code, control should return by jumping to `_pre_default_start_trap_ret`.
607+
//!
608+
//! It's recommended to place the code in the `.trap.start` section to make sure it's reachable from `_default_start_trap`.
609+
//!
610+
//! It is expected that the custom code does not clobber any registers.
611+
//!
612+
//! Please note that your code won't be run for interrupts in vectored mode.
613+
//!
614+
//! ### Example
615+
//!
616+
//! ```rust,no_run
617+
//! core::arch::global_asm!(
618+
//! r#"
619+
//! .section .trap.start, "ax"
620+
//! .extern _pre_default_start_trap_ret
621+
//! .global _pre_default_start_trap
622+
//!
623+
//! _pre_default_start_trap:
624+
//!
625+
//! // your code goes here remember to not clobber any registers,
626+
//! // use mscratch to retain a single register if needed
627+
//!
628+
//! // jump back to continue with _default_start_trap
629+
//! j _pre_default_start_trap_ret
630+
//! "#
631+
//! );
632+
//! ```
601633
//! [attr-entry]: attr.entry.html
602634
//! [attr-exception]: attr.exception.html
603635
//! [attr-external-interrupt]: attr.external_interrupt.html

0 commit comments

Comments
 (0)