Skip to content

Commit 3663237

Browse files
committed
Add mtvec-align-{16,64,256} features
1 parent a0fac61 commit 3663237

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

riscv-rt/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12-
- Ensure the vector table is 256-byte aligned.
12+
- Add `mtvec-align-{16,64,256}` features to control the vector table alignment.
1313
- Ensure the `.heap` section is 4-byte aligned.
1414
- Limit rustc cfg flags to `riscvi`, `riscvm`, `riscvf`, and `riscvd`.
1515
- Temporary use of `RISCV_RT_LLVM_ARCH_PATCH` environment variable to include the

riscv-rt/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ v-trap = ["riscv-rt-macros/v-trap"]
3737
u-boot = ["riscv-rt-macros/u-boot", "single-hart"]
3838
no-interrupts = []
3939
no-exceptions = []
40+
# Alignment requirement of the vector table in bytes (default is 4).
41+
mtvec-align-16 = []
42+
mtvec-align-64 = ["mtvec-align-16"]
43+
mtvec-align-256 = ["mtvec-align-64"]

riscv-rt/src/asm.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
11
use core::arch::global_asm;
22

3-
/// Parse cfg attributes inside a global_asm call.
4-
macro_rules! cfg_global_asm {
5-
{@inner, [$($x:tt)*], } => {
6-
global_asm!{$($x)*}
7-
};
8-
(@inner, [$($x:tt)*], #[cfg($meta:meta)] $asm:literal, $($rest:tt)*) => {
9-
#[cfg($meta)]
10-
cfg_global_asm!{@inner, [$($x)* $asm,], $($rest)*}
11-
#[cfg(not($meta))]
12-
cfg_global_asm!{@inner, [$($x)*], $($rest)*}
13-
};
14-
{@inner, [$($x:tt)*], $asm:literal, $($rest:tt)*} => {
15-
cfg_global_asm!{@inner, [$($x)* $asm,], $($rest)*}
16-
};
17-
{$($asms:tt)*} => {
18-
cfg_global_asm!{@inner, [], $($asms)*}
19-
};
20-
}
21-
223
// Provisional patch to avoid LLVM spurious errors when compiling in release mode.
234
// This patch is somewhat hardcoded and relies on the fact that the rustc compiler
245
// only supports a limited combination of ISA extensions. This patch should be

riscv-rt/src/interrupts.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,20 @@ pub unsafe extern "C" fn _dispatch_core_interrupt(code: usize) {
7575
any(target_arch = "riscv32", target_arch = "riscv64"),
7676
feature = "v-trap"
7777
))]
78-
core::arch::global_asm!(
78+
cfg_global_asm!(
7979
r#" .section .trap, "ax"
8080
.weak _vector_table
8181
.type _vector_table, @function
82-
83-
.option push
84-
.balign 0x100 // RISC-V requires 4, but implementations may be stricter
85-
.option norelax
82+
.option push"#,
83+
#[cfg(not(feature = "mtvec-align-16"))]
84+
".balign 0x4",
85+
#[cfg(all(feature = "mtvec-align-16", not(feature = "mtvec-align-64")))]
86+
".balign 0x10",
87+
#[cfg(all(feature = "mtvec-align-64", not(feature = "mtvec-align-256")))]
88+
".balign 0x40",
89+
#[cfg(feature = "mtvec-align-256")]
90+
".balign 0x100",
91+
r#" .option norelax
8692
.option norvc
8793
8894
_vector_table:
@@ -99,5 +105,5 @@ core::arch::global_asm!(
99105
j _start_DefaultHandler_trap // Interrupt 10 is reserved
100106
j _start_MachineExternal_trap
101107
102-
.option pop"#
108+
.option pop"#,
103109
);

riscv-rt/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,25 @@
532532
#![no_std]
533533
#![deny(missing_docs)]
534534

535+
/// Parse cfg attributes inside a global_asm call.
536+
macro_rules! cfg_global_asm {
537+
{@inner, [$($x:tt)*], } => {
538+
core::arch::global_asm!{$($x)*}
539+
};
540+
(@inner, [$($x:tt)*], #[cfg($meta:meta)] $asm:literal, $($rest:tt)*) => {
541+
#[cfg($meta)]
542+
cfg_global_asm!{@inner, [$($x)* $asm,], $($rest)*}
543+
#[cfg(not($meta))]
544+
cfg_global_asm!{@inner, [$($x)*], $($rest)*}
545+
};
546+
{@inner, [$($x:tt)*], $asm:literal, $($rest:tt)*} => {
547+
cfg_global_asm!{@inner, [$($x)* $asm,], $($rest)*}
548+
};
549+
{$($asms:tt)*} => {
550+
cfg_global_asm!{@inner, [], $($asms)*}
551+
};
552+
}
553+
535554
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
536555
mod asm;
537556

0 commit comments

Comments
 (0)