Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

riscv-rt: Add mtvec-align features for the vector table alignment #259

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion riscv-rt/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- Ensure the `.heap` section is 4-byte aligned
- Use `RISCV_MTVEC_ALIGN` to control the alignment constraint of the vector table.
- Ensure the `.heap` section is 4-byte aligned.
- Limit rustc cfg flags to `riscvi`, `riscvm`, `riscvf`, and `riscvd`.
- Temporary use of `RISCV_RT_LLVM_ARCH_PATCH` environment variable to include the
temporary patch required for avoid LLVM spurious errors.
Expand Down
41 changes: 11 additions & 30 deletions riscv-rt/src/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,14 @@ pub unsafe extern "C" fn _dispatch_core_interrupt(code: usize) {
}

// In vectored mode, we also must provide a vector table
#[cfg(all(
any(target_arch = "riscv32", target_arch = "riscv64"),
feature = "v-trap"
))]
core::arch::global_asm!(
r#" .section .trap, "ax"
.weak _vector_table
.type _vector_table, @function

.option push
.balign 0x4 // TODO check if this is the correct alignment
.option norelax
.option norvc

_vector_table:
j _start_trap // Interrupt 0 is used for exceptions
j _start_SupervisorSoft_trap
j _start_DefaultHandler_trap // Interrupt 2 is reserved
j _start_MachineSoft_trap
j _start_DefaultHandler_trap // Interrupt 4 is reserved
j _start_SupervisorTimer_trap
j _start_DefaultHandler_trap // Interrupt 6 is reserved
j _start_MachineTimer_trap
j _start_DefaultHandler_trap // Interrupt 8 is reserved
j _start_SupervisorExternal_trap
j _start_DefaultHandler_trap // Interrupt 10 is reserved
j _start_MachineExternal_trap

.option pop"#
);
#[cfg(feature = "v-trap")]
#[riscv::pac_enum(unsafe CoreInterruptNumber)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Interrupt {
SupervisorSoft = 1,
MachineSoft = 3,
SupervisorTimer = 5,
MachineTimer = 7,
SupervisorExternal = 9,
MachineExternal = 11,
}
1 change: 1 addition & 0 deletions riscv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- Use `RISCV_MTVEC_ALIGN` to control the alignment constraint of the vector table
- Simplify register macros with `cfg` field
- Align assembly functions with `cortex-m`
- Use CSR helper macros to define `marchid` register
Expand Down
22 changes: 20 additions & 2 deletions riscv/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,25 @@ impl PacEnumItem {
}

fn vector_table(&self) -> TokenStream2 {
let mut asm = String::from(
let mut align = match std::env::var("RISCV_MTVEC_ALIGN") {
Ok(x) => x.parse::<u32>().ok(),
Err(std::env::VarError::NotPresent) => Some(4),
Err(std::env::VarError::NotUnicode(_)) => None,
};
if let Some(value) = align {
if !value.is_power_of_two() || value < 4 {
align = None;
}
}
ia0 marked this conversation as resolved.
Show resolved Hide resolved
let align = match align {
Some(x) => x,
None => {
return quote!(compile_error!(
"RISCV_MTVEC_ALIGN is not a power of 2 (minimum 4)"
))
}
};
let mut asm = format!(
r#"
#[cfg(all(feature = "v-trap", any(target_arch = "riscv32", target_arch = "riscv64")))]
core::arch::global_asm!("
Expand All @@ -274,7 +292,7 @@ core::arch::global_asm!("
.type _vector_table, @function

.option push
.balign 0x4 // TODO check if this is the correct alignment
.balign {align}
.option norelax
.option norvc

Expand Down
Loading