Skip to content

Commit 1ff3edf

Browse files
committed
Add pre-default-start-trap feature
1 parent fcde9ff commit 1ff3edf

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,15 @@
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 is expected that the custom code does not clobber any registers.
609+
//!
601610
//! [attr-entry]: attr.entry.html
602611
//! [attr-exception]: attr.exception.html
603612
//! [attr-external-interrupt]: attr.external_interrupt.html

0 commit comments

Comments
 (0)