Skip to content

Commit 0cdd0ca

Browse files
Merge pull request #320 from rust-embedded/rvrt-asm
`riscv-rt`: organize trap section
2 parents cdae64a + 06559d7 commit 0cdd0ca

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
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: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![deny(warnings)]
2+
#![allow(unknown_lints)] // reason = "required for next line"
3+
#![allow(clippy::manual_is_multiple_of)] // reason = "requires MSRV bump"
24

35
use proc_macro::TokenStream;
46
use proc_macro2::{Span, TokenStream as TokenStream2};
@@ -518,8 +520,8 @@ pub fn default_start_trap(_input: TokenStream) -> TokenStream {
518520
format!(
519521
r#"
520522
core::arch::global_asm!(
521-
".section .trap, \"ax\"
522-
.align 4 /* Alignment required for xtvec */
523+
".section .trap.start, \"ax\"
524+
.balign 4 /* Alignment required for xtvec */
523525
.global _default_start_trap
524526
_default_start_trap:
525527
addi sp, sp, - {trap_size} * {width}
@@ -557,16 +559,15 @@ pub fn vectored_interrupt_trap(_input: TokenStream) -> TokenStream {
557559
let instructions = format!(
558560
r#"
559561
core::arch::global_asm!(
560-
".section .trap, \"ax\"
562+
".section .trap.continue, \"ax\"
561563
562-
.align 4
564+
.balign 4
563565
.global _start_DefaultHandler_trap
564566
_start_DefaultHandler_trap:
565567
addi sp, sp, -{trap_size} * {width} // allocate space for trap frame
566568
{store_start} // store trap partially (only register a0)
567569
la a0, DefaultHandler // load interrupt handler address into a0
568570
569-
.align 4
570571
.global _continue_interrupt_trap
571572
_continue_interrupt_trap:
572573
{store_continue} // store trap partially (all registers except a0)
@@ -685,10 +686,11 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
685686
/// }
686687
/// ```
687688
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
689+
let arch = match () {
690+
#[cfg(feature = "v-trap")]
691+
() => RiscvArch::try_from_env(),
692+
#[cfg(not(feature = "v-trap"))]
693+
() => None,
692694
};
693695
trap(args, input, RiscvPacItem::CoreInterrupt, arch)
694696
}
@@ -746,13 +748,14 @@ fn trap(
746748
let export_name = format!("{int_ident:#}");
747749

748750
let start_trap = match arch {
751+
#[cfg(feature = "v-trap")]
749752
Some(arch) => {
750753
let trap = start_interrupt_trap(int_ident, arch);
751754
quote! {
752755
#trap
753756
}
754757
}
755-
None => proc_macro2::TokenStream::new(),
758+
_ => proc_macro2::TokenStream::new(),
756759
};
757760

758761
let pac_trait = pac_item.impl_trait();
@@ -772,6 +775,7 @@ fn trap(
772775
.into()
773776
}
774777

778+
#[cfg(feature = "v-trap")]
775779
fn start_interrupt_trap(ident: &syn::Ident, arch: RiscvArch) -> proc_macro2::TokenStream {
776780
let interrupt = ident.to_string();
777781
let width = arch.width();
@@ -780,9 +784,10 @@ fn start_interrupt_trap(ident: &syn::Ident, arch: RiscvArch) -> proc_macro2::Tok
780784

781785
let instructions = format!(
782786
r#"
787+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
783788
core::arch::global_asm!(
784-
".section .trap, \"ax\"
785-
.align 2
789+
".section .trap.start.{interrupt}, \"ax\"
790+
.balign 4
786791
.global _start_{interrupt}_trap
787792
_start_{interrupt}_trap:
788793
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)