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 <[email protected]>, 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,13 @@ | |
| // SPDX-FileContributor: Wojciech Ozga <[email protected]>, IBM Research - Zurich | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| #![allow(unused)] | ||
| pub(crate) use control_status_registers::ReadWriteRiscvCsr; | ||
| pub use control_status_registers::{CSR, ControlStatusRegister, ControlStatusRegisters}; | ||
| pub use extensions::HardwareExtension; | ||
| pub use extensions::compressed_instructions::decode_result_register; | ||
| pub use extensions::floating_point_unit::FloatingPointUnit; | ||
| pub use extensions::supervisor_timer_extension::SupervisorTimerExtension; | ||
| pub use extensions::vector_unit::VectorUnit; | ||
| pub use general_purpose_registers::{GeneralPurposeRegister, GeneralPurposeRegisters}; | ||
| pub use hart_architectural_state::HartArchitecturalState; | ||
| pub use hart_lifecycle_state::HartLifecycleState; | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| // SPDX-FileContributor: Wojciech Ozga <[email protected]>, IBM Research - Zurich | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| use crate::core::architecture::riscv::sbi::NaclSharedMemory; | ||
| use crate::core::architecture::riscv::specification::SR_FS; | ||
| use crate::core::architecture::riscv::specification::{SR_FS, SR_VS_DIRTY}; | ||
| use crate::core::architecture::specification::SR_FS_DIRTY; | ||
| use crate::core::architecture::{ | ||
| ControlStatusRegisters, GeneralPurposeRegisters, HardwareExtension, HartArchitecturalState, SupervisorTimerExtension, | ||
|
|
@@ -28,12 +28,12 @@ pub struct HypervisorHart { | |
| } | ||
|
|
||
| impl HypervisorHart { | ||
| pub fn new(hypervisor_memory_protector: HypervisorMemoryProtector) -> Self { | ||
| Self { | ||
| hypervisor_hart_state: HartArchitecturalState::empty(), | ||
| pub fn new(hypervisor_memory_protector: HypervisorMemoryProtector) -> Result<Self, Error> { | ||
| Ok(Self { | ||
| hypervisor_hart_state: HartArchitecturalState::empty()?, | ||
| shared_memory: NaclSharedMemory::uninitialized(), | ||
| hypervisor_memory_protector, | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /// Saves the state of the hypervisor hart in the main memory. This is part of the heavy context switch. | ||
|
|
@@ -52,6 +52,13 @@ impl HypervisorHart { | |
| } | ||
| } | ||
| } | ||
|
|
||
| if HardwareSetup::is_extension_supported(HardwareExtension::VectorExtension) { | ||
| // Enable the vector unit in mstatus to access vector | ||
| // registers and execute vector instructions. | ||
| self.csrs().mstatus.read_and_set_bits(SR_VS_DIRTY); | ||
| unsafe { self.hypervisor_hart_state.vrs_mut().save_in_main_memory() }; | ||
| } | ||
| } | ||
|
|
||
| /// Restores the state of the hypervisor hart from the main memory. This is part of the heavy context switch. | ||
|
|
@@ -65,6 +72,13 @@ impl HypervisorHart { | |
| // below unsafe is ok because we checked that FPU hardware is available and we enabled it in mstatus. | ||
| unsafe { self.hypervisor_hart_state.fprs_mut().restore_from_main_memory() }; | ||
| } | ||
|
|
||
| if HardwareSetup::is_extension_supported(HardwareExtension::VectorExtension) { | ||
| // Enable the vector unit in mstatus to access vector | ||
| // registers and execute vector instructions. | ||
| self.csrs().mstatus.read_and_set_bits(SR_VS_DIRTY); | ||
| unsafe { self.hypervisor_hart_state.vrs().restore_from_main_memory() }; | ||
| } | ||
| } | ||
|
|
||
| pub fn set_shared_memory(&mut self, base_address: NonConfidentialMemoryAddress) -> Result<(), Error> { | ||
|
|
||
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.