-
Notifications
You must be signed in to change notification settings - Fork 169
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
base: master
Are you sure you want to change the base?
Conversation
The RISC-V Privileged Specification (version 1.11) for mtvec CSR says: > The value in the BASE field must always be aligned on a 4-byte boundary, and the MODE setting may > impose additional alignment constraints on the value in the BASE field. > [...] > An implementation may have different alignment constraints for different modes. In particular, > MODE=Vectored may have stricter alignment constraints than MODE=Direct. The Ibex Reference Guide for mtvec CSR says[^1]: > The trap-vector base address, always aligned to 256 bytes I'm not sure how we can parametrize mtvec alignment without cargo features, so this PR simply uses the stricter alignment. If a cargo feature is preferred, I can add `mtvec-align-256`. Note that multiple `mtvec-align-N` features are additive because the stricter one wins. [^1]: https://ibex-core.readthedocs.io/en/latest/03_reference/cs_registers.html#machine-trap-vector-base-address-mtvec
Truly, each interrupt controller has its own alignment requirements. For instance, the riscv-fast-interrupt specification (CLIC) specifies 4-byte alignment in CLINT mode and 64-byte alignment in CLIC mode. This has been an issue for us at https://github.com/soc-hub-fi/atalanta working on an Ibex-based CPU design as well, as not being able to parameterize this forces us to flag out & write the trap vector ourselves. I'd be in favor of a flag such as |
I think the best approach is using a linker symbol, such as _heap_size, for instance. By default, the symbol should be 4. PACs can overwrite it according to their particular needs. The linker script should check that the alignment is a power of 2, I guess (and greater than 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how we can parametrize mtvec alignment without cargo features, so this PR simply uses the stricter alignment.
One possibility is to use const generics to parameterize the Mtvec::set_address
function directly, or offer another Mtvec::set_address_aligned<const N: usize>
.
Something like:
impl Mtvec {
pub fn set_address_aligned<const N: usize>(&mut self, address: usize) {
// check alignment does not violate spec requirements
const _: () = assert!(N >= 4);
const _: () = assert_eq!(N % 4, 0);
const MASK: usize = usize::MAX & !((1usize << N) - 1);
self.set_address(address & MASK);
}
}
The above could also obviously be made into a fallible try_set_address_aligned
, just used this one for brevity.
For the actual alignment of the trap vector function, I think @romancardenas approach is the right way to go.
It kind of scales thanks to the I used the values 4 (default), 16, 64, and 256, since 4, 64, and 256 are used, and 16 is the missing power of 2 with an even exponent.
I don't think this works?
This seems unrelated. This PR is about letting users configure the alignment of the vector table in |
#[allow(unused_macros)] | ||
macro_rules! cfg_global_asm { | ||
{@inner, [$($x:tt)*], } => { | ||
core::arch::global_asm!{$($x)*} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I moved this from src/asm.rs
and did 2 changes:
- Fully qualify
core::arch::global_asm!()
- All
unused_macros
since it's only used onriscv32
,riscv64
, or withoutno-interrupts
.
Perhaps another option is using a procedural macro with environment variables (e.g., Line 44 in b391a48
PACs would set the mtvec alignment as part of their (I'm not against the approach of this PR, but I'd rather have something more configurable that would not potentially lead to more features etc.) |
THe Line 277 in b391a48
We can make a new version of this macro that also allows to define the alignment of the vector table and use it in |
The good thing about the |
The RISC-V Privileged Specification (version 1.11) for mtvec CSR says:
The Ibex Reference Guide for mtvec CSR says1:
Other implementations have yet other alignment constraints (see #259 (comment)).
This PR introduces the
mtvec-align-{16,64,256}
cargo features to let users choose the alignment constraint appropriate to their implementation.Footnotes
https://ibex-core.readthedocs.io/en/latest/03_reference/cs_registers.html#machine-trap-vector-base-address-mtvec ↩