Repository navigation
Fix RISC-V vector state preservation across context switches #145
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
Open
whensun
wants to merge
4
commits into
IBM:main
Choose a base branch
from
whensun:fix/vector-context-switch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
60be645
Fix RISC-V vector state preservation across context switches
whensun f8137af
Remove temporary VectorUnit diagnostics
whensun dbc8b67
Fix false-positive RISC-V Vector extension detection
whensun d237db4
Address review feedback on vector state preservation
whensun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 122 additions & 7 deletions
129
security-monitor/src/core/architecture/riscv/extensions/vector_unit.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,131 @@ | ||
| // SPDX-FileCopyrightText: 2023 IBM Corporation | ||
| // SPDX-FileContributor: Wojciech Ozga <woz@zurich.ibm.com>, IBM Research - Zurich | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| #![allow(unused)] | ||
|
|
||
| use crate::core::architecture::riscv::control_status_registers::ReadWriteRiscvCsr; | ||
| use crate::core::architecture::riscv::specification::{CSR_VCSR, CSR_VL, CSR_VSTART, CSR_VTYPE}; | ||
| use crate::core::hardware_setup::HardwareSetup; | ||
| use crate::error::Error; | ||
| use alloc::vec::Vec; | ||
| use core::ops::Range; | ||
| use core::arch::asm; | ||
|
|
||
| pub const MAX_NUMBER_OF_REGISTER_LENGTH: usize = 32; | ||
| const NUMBER_OF_VALUES_IN_REGISTER: usize = MAX_NUMBER_OF_REGISTER_LENGTH >> 3; | ||
| const NUMBER_OF_VECTOR_REGISTERS: usize = 32; | ||
|
|
||
| #[repr(C)] | ||
| pub struct VectorRegister(pub [u64; NUMBER_OF_VALUES_IN_REGISTER]); | ||
| pub struct VectorUnit { | ||
| registers: Vec<u8>, | ||
| vlenb: usize, | ||
| vl: ReadWriteRiscvCsr<CSR_VL>, | ||
| vtype: ReadWriteRiscvCsr<CSR_VTYPE>, | ||
| vstart: ReadWriteRiscvCsr<CSR_VSTART>, | ||
| vcsr: ReadWriteRiscvCsr<CSR_VCSR>, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| pub struct VectorRegisters(pub [usize; NUMBER_OF_VECTOR_REGISTERS]); | ||
| impl VectorUnit { | ||
| pub fn empty() -> Result<Self, Error> { | ||
| let vlenb = HardwareSetup::vector_vlenb()?; | ||
|
|
||
| let length = NUMBER_OF_VECTOR_REGISTERS.checked_mul(vlenb).ok_or(Error::InvalidVectorRegisterSize())?; | ||
|
|
||
| let mut registers = Vec::new(); | ||
| registers.try_reserve_exact(length).map_err(|_| Error::NotEnoughMemory())?; | ||
| registers.resize(length, 0); | ||
|
|
||
| Ok(Self { | ||
| registers, | ||
| vlenb, | ||
| vl: ReadWriteRiscvCsr::new(), | ||
| vtype: ReadWriteRiscvCsr::new(), | ||
| vstart: ReadWriteRiscvCsr::new(), | ||
| vcsr: ReadWriteRiscvCsr::new(), | ||
| }) | ||
| } | ||
|
|
||
| /// Saves vector state in main memory. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The vector extension must be supported and enabled. | ||
| pub unsafe fn save_in_main_memory(&mut self) { | ||
| self.vl.save_in_main_memory(); | ||
| self.vtype.save_in_main_memory(); | ||
| self.vstart.save_in_main_memory(); | ||
| self.vcsr.save_in_main_memory(); | ||
|
|
||
| self.vstart.write(0); | ||
|
|
||
| macro_rules! store { | ||
| ($index:literal) => { | ||
| unsafe { | ||
| asm!( | ||
| ".option push", | ||
| ".option arch, +v", | ||
| concat!( | ||
| "vs8r.v v", | ||
| stringify!($index), | ||
| ", ({address})" | ||
| ), | ||
| ".option pop", | ||
| address = in(reg) self.registers | ||
| .as_mut_ptr() | ||
| .add($index * self.vlenb), | ||
| ); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| store!(0); | ||
| store!(8); | ||
| store!(16); | ||
| store!(24); | ||
| } | ||
|
|
||
| /// Restores vector state from main memory. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The vector extension must be supported and enabled. | ||
| /// The current hart must have a compatible vector configuration. | ||
| pub unsafe fn restore_from_main_memory(&self) { | ||
| self.vstart.write(0); | ||
|
|
||
| macro_rules! load { | ||
| ($index:literal) => { | ||
| unsafe { | ||
| asm!( | ||
| ".option push", | ||
| ".option arch, +v", | ||
| concat!( | ||
| "vl8re8.v v", | ||
| stringify!($index), | ||
| ", ({address})" | ||
| ), | ||
| ".option pop", | ||
| address = in(reg) self.registers | ||
| .as_ptr() | ||
| .add($index * self.vlenb), | ||
| ); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| load!(0); | ||
| load!(8); | ||
| load!(16); | ||
| load!(24); | ||
|
|
||
| unsafe { | ||
| asm!( | ||
| ".option push", | ||
| ".option arch, +v", | ||
| "vsetvl zero, {saved_vl}, {saved_vtype}", | ||
| ".option pop", | ||
| saved_vl = in(reg) self.vl.read_from_main_memory(), | ||
| saved_vtype = in(reg) self.vtype.read_from_main_memory(), | ||
| ); | ||
| } | ||
|
|
||
| self.vcsr.restore_from_main_memory(); | ||
| self.vstart.restore_from_main_memory(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
please add a comment (like in FPU-related code) explaining that SR_VS_DIRTY flag enables the vector unit in mstatus, allowing accessing vector-related registers and instructions.
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.
Done. Added comments explaining that setting SR_VS_DIRTY enables access to vector registers and instructions.