Skip to content

Commit c0008fa

Browse files
committed
riscv-rt: organize trap section
1 parent cdae64a commit c0008fa

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

riscv-rt/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1818
- In multi-hart targets, the hart ID is now validated earlier in the boot process.
1919
- General purpose registers are no longer zeroed, as this is not strictly necessary.
2020
This aligns with the `cortex-m-rt` crate.
21+
- Better organization of the `.trap` section:
22+
1. `_trap_vector` (if `v-trap` is enabled).
23+
2. `_start_trap` (defaults to `_default_start_trap`).
24+
3. `_start_INTERRUPT_trap` routines (if `v-trap` is enabled).
25+
4. `_start_DefaultHandler_trap` and `_continue_trap` (if `v-trap` is enabled).
26+
5. `_start_trap_rust`.
27+
6. Other code in `.trap` section (usually, none)
2128

2229
### Fixed
2330

riscv-rt/link.x.in

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,15 @@ SECTIONS
9090
/* Put reset handler first in .text section so it ends up as the entry */
9191
/* point of the program. */
9292
KEEP(*(.init));
93+
9394
. = ALIGN(4);
94-
*(.trap);
95-
*(.trap.rust);
95+
KEEP(*(.trap.vector)); /* for _trap_vector (vectored mode only) */
96+
KEEP(*(.trap.start)); /* for _start_trap routine */
97+
KEEP(*(.trap.start.*)); /* for _start_INTERRUPT_trap routines (vectored mode only) */
98+
KEEP(*(.trap.continue)); /* for _continue_trap routine (vectored mode only) */
99+
KEEP(*(.trap.rust)); /* for _start_trap_rust Rust function */
100+
KEEP(*(.trap .trap.*)); /* Other .trap symbols at the end */
101+
96102
*(.text.abort);
97103
*(.text .text.*);
98104

riscv-rt/macros/src/lib.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ pub fn default_start_trap(_input: TokenStream) -> TokenStream {
518518
format!(
519519
r#"
520520
core::arch::global_asm!(
521-
".section .trap, \"ax\"
521+
".section .trap.start, \"ax\"
522522
.align 4 /* Alignment required for xtvec */
523523
.global _default_start_trap
524524
_default_start_trap:
@@ -557,7 +557,7 @@ pub fn vectored_interrupt_trap(_input: TokenStream) -> TokenStream {
557557
let instructions = format!(
558558
r#"
559559
core::arch::global_asm!(
560-
".section .trap, \"ax\"
560+
".section .trap.continue, \"ax\"
561561
562562
.align 4
563563
.global _start_DefaultHandler_trap
@@ -566,7 +566,6 @@ _start_DefaultHandler_trap:
566566
{store_start} // store trap partially (only register a0)
567567
la a0, DefaultHandler // load interrupt handler address into a0
568568
569-
.align 4
570569
.global _continue_interrupt_trap
571570
_continue_interrupt_trap:
572571
{store_continue} // store trap partially (all registers except a0)
@@ -685,10 +684,11 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
685684
/// }
686685
/// ```
687686
pub fn core_interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
688-
let arch = if cfg!(feature = "v-trap") {
689-
RiscvArch::try_from_env()
690-
} else {
691-
None
687+
let arch = match () {
688+
#[cfg(feature = "v-trap")]
689+
() => RiscvArch::try_from_env(),
690+
#[cfg(not(feature = "v-trap"))]
691+
() => None,
692692
};
693693
trap(args, input, RiscvPacItem::CoreInterrupt, arch)
694694
}
@@ -746,13 +746,14 @@ fn trap(
746746
let export_name = format!("{int_ident:#}");
747747

748748
let start_trap = match arch {
749+
#[cfg(feature = "v-trap")]
749750
Some(arch) => {
750751
let trap = start_interrupt_trap(int_ident, arch);
751752
quote! {
752753
#trap
753754
}
754755
}
755-
None => proc_macro2::TokenStream::new(),
756+
_ => proc_macro2::TokenStream::new(),
756757
};
757758

758759
let pac_trait = pac_item.impl_trait();
@@ -772,6 +773,7 @@ fn trap(
772773
.into()
773774
}
774775

776+
#[cfg(feature = "v-trap")]
775777
fn start_interrupt_trap(ident: &syn::Ident, arch: RiscvArch) -> proc_macro2::TokenStream {
776778
let interrupt = ident.to_string();
777779
let width = arch.width();
@@ -780,9 +782,10 @@ fn start_interrupt_trap(ident: &syn::Ident, arch: RiscvArch) -> proc_macro2::Tok
780782

781783
let instructions = format!(
782784
r#"
785+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
783786
core::arch::global_asm!(
784-
".section .trap, \"ax\"
785-
.align 2
787+
".section .trap.start.{interrupt}, \"ax\"
788+
.align 4
786789
.global _start_{interrupt}_trap
787790
_start_{interrupt}_trap:
788791
addi sp, sp, -{trap_size} * {width} // allocate space for trap frame

riscv/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
### Changed
1515

1616
- Use `cfg(any(target_arch = "riscv32", target_arch = "riscv64"))` instead of `cfg(riscv)`.
17+
- `riscv::pac_enum(unsafe CoreInterrupt)` now locates the vector table at the `.trap.vector`
18+
section instead of `.trap`.
1719

1820
### Removed
1921

riscv/macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl PacEnumItem {
282282
r#"
283283
#[cfg(all(feature = "v-trap", any(target_arch = "riscv32", target_arch = "riscv64")))]
284284
core::arch::global_asm!("
285-
.section .trap, \"ax\"
285+
.section .trap.vector, \"ax\"
286286
.global _vector_table
287287
.type _vector_table, @function
288288

0 commit comments

Comments
 (0)