Skip to content

Fix RISC-V vector state preservation across context switches - #145

Open
whensun wants to merge 4 commits into
IBM:mainfrom
whensun:fix/vector-context-switch
Open

whensun wants to merge 4 commits into
IBM:mainfrom
whensun:fix/vector-context-switch

Conversation

@whensun

@whensun whensun commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Vector registers v0–v31
  • Vector CSRs vl, vtype, vstart, and vcsr
  • Vector fixed-point state vxrm and vxsat through vcsr

The implementation integrates vector state management into the existing hart architectural state and context-switching mechanisms.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature
  • Breaking change
  • Refactorization

How to test this PR?

  1. Build ACE-RISCV with the RISC-V Vector extension enabled.
  2. Initialize vector registers and CSRs with known values.
  3. Trigger context switches between confidential VMs (VS → VS).
  4. Trigger context switches between a confidential VM and the hypervisor (VS → HS → VS).
  5. Verify that the original vector state is restored.

Signed-off-by: whensun <theodoruswensanfebruanto025@gmail.com>
Signed-off-by: whensun <theodoruswensanfebruanto025@gmail.com>
Signed-off-by: whensun <theodoruswensanfebruanto025@gmail.com>

@wojciechozga wojciechozga left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR. I left a few comments, please have a look

@whensun

whensun commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Hi Wojciech,

Thank you for the review. I have addressed all six comments in the latest commit.

The changes include:

  • Renamed vector to vrs.
  • Added comments explaining SR_VS_DIRTY.
  • Replaced individual vector register loads/stores with vs8r.v and vl8re8.v.
  • Added error handling for vector state initialization.
  • Reused ReadWriteRiscvCsr for vector CSRs.
  • Moved VLEN validation to initialization, including per-hart compatibility checks.

Validation:

  • Rust compilation: PASS
  • OpenSBI firmware build: PASS
  • ACE-RISCV regression 8 test cases: PASS

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>
@whensun
whensun force-pushed the fix/vector-context-switch branch from 9854224 to d237db4 Compare November 7, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants