Repository navigation
Conversation
Signed-off-by: whensun <theodoruswensanfebruanto025@gmail.com>
Signed-off-by: whensun <theodoruswensanfebruanto025@gmail.com>
Signed-off-by: whensun <theodoruswensanfebruanto025@gmail.com>
| csrs: ControlStatusRegisters, | ||
| fprs: FloatingPointUnit, | ||
| sstc: SupervisorTimerExtension, | ||
| vector: VectorUnit, |
There was a problem hiding this comment.
to follow naming convention, should we name it vrs (vector registers) instead of vector?
There was a problem hiding this comment.
Done. Renamed vector to vrs in HartArchitecturalState and updated all related accessors and references.
| } | ||
|
|
||
| if HardwareSetup::is_extension_supported(HardwareExtension::VectorExtension) { | ||
| self.csrs().mstatus.read_and_set_bits(SR_VS_DIRTY); |
There was a problem hiding this comment.
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.
Done. Added comments explaining that setting SR_VS_DIRTY enables access to vector registers and instructions.
| asm!( | ||
| ".option push", | ||
| ".option arch, +v", | ||
| concat!("vs1r.v v", stringify!($index), ", ({address})"), |
There was a problem hiding this comment.
vs8r.v stores more registers at a time what reduces the total number of instructions per context switch. An analog approach should be followed for the load from memory
There was a problem hiding this comment.
Done. Replaced the 32 individual register store/load instructions with four vs8r.v instructions and four vl8re8.v instructions.
|
|
||
| assert!(vlenb > 0); | ||
|
|
||
| let length = NUMBER_OF_VECTOR_REGISTERS.checked_mul(vlenb).expect("Invalid vector register size"); |
There was a problem hiding this comment.
Please return an error instead of panicking
There was a problem hiding this comment.
I've changed VectorUnit::empty() to return Result<Self, Error>, with explicit error handling for invalid vector register sizes and allocation failures. I've updated the callers to propagate these errors.
| vlenb: usize, | ||
| vl: usize, | ||
| vtype: usize, | ||
| vstart: usize, | ||
| vcsr: usize, |
There was a problem hiding this comment.
please reuse ReadWriteRiscvCsr similarly to FPU code. Then minimize custom assembly blocks by calling respective functions.
| } | ||
|
|
||
| pub unsafe fn save_in_main_memory(&mut self) { | ||
| unsafe { self.validate_vlenb() }; |
There was a problem hiding this comment.
Do we need this checks for every context switch? Wouldn't it be enough to check vlenb once? If vlenb could differ among harts, we should then check it when deciding if V ext should be supported in the initialization procedure. Otherwise, the validate_vlenb should return an error and not panic on a mismatch.
There was a problem hiding this comment.
I've moved the VLEN validation to initialization instead of checking it during every context switch. The boot hart records the expected vlenb, and each hart validates its value during setup. Invalid or inconsistent values return an error from the validation function.
wojciechozga
left a comment
There was a problem hiding this comment.
Thanks for this PR. I left a few comments, please have a look
|
Hi Wojciech, Thank you for the review. I have addressed all six comments in the latest commit. The changes include:
Validation:
The regression test cases cover vector CSR preservation across VS-to-VS and VS-to-HS-to-VS context switches. Could you please review the updated implementation when you have a chance? Thanks! |
Signed-off-by: whensun <theodoruswensanfebruanto025@gmail.com>
9854224 to
d237db4
Compare
Description of the changes
This PR fixes missing RISC-V Vector architectural state preservation during context switches between confidential VMs and the hypervisor.
Previously, vector registers and control/status registers were not saved and restored during context switches, potentially causing vector state corruption.
This PR introduces vector state preservation for:
v0–v31vl,vtype,vstart, andvcsrvxrmandvxsatthroughvcsrThe implementation integrates vector state management into the existing hart architectural state and context-switching mechanisms.
Type of change
How to test this PR?