lib: Missing vl and vtype preservation during Vector domain context switches
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- c
- Domain
- operating-systems
Research direction
Start by reading include/sbi/sbi_vector.h and the save/restore paths in lib/sbi/sbi_vector.c, then inspect the existing Vector context-switch test infrastructure. Verify the proposed reproducer by switching between domains with different vl and vtype values, confirming that the resumed domain sees its original configuration. Done means the context structure, save path, restore path, and regression coverage consistently preserve both CSRs, including vstart ordering.
Written by the indexing model from the issue text.
Description
Summary
lib/sbi/sbi_vector.c:sbi_vector_save() and sbi_vector_restore() do not preserve the RISC-V Vector CSRs vl and vtype during domain context switches.
The current implementation already saves and restores:
v0tov31vcsr, includingvxrmandvxsatvstart
However, vl and vtype are not included in the saved Vector context.
These two registers control the Vector length and configuration used by Vector instructions.
As a result, switching between domains with different Vector configurations may cause one domain to inherit the configuration left by another domain.
Affected code
File: include/sbi/sbi_vector.h
The current Vector context structure is:
struct sbi_vector_context {
unsigned long vcsr;
unsigned long vstart;
uint8_t vregs[];
};
There are no fields for vl or vtype.
File: lib/sbi/sbi_vector.c
In sbi_vector_save(), the current code saves:
dst->vcsr = csr_read(vcsr);
dst->vstart = csr_read(vstart);
It also saves all 32 Vector registers using:
SAVE_VREG(0);
SAVE_VREG(8);
SAVE_VREG(16);
SAVE_VREG(24);
However, it does not save vl or vtype.
In sbi_vector_restore(), the current implementation restores:
RESTORE_VREG(0);
RESTORE_VREG(8);
RESTORE_VREG(16);
RESTORE_VREG(24);
Then restores the Vector control CSRs:
csr_write(vcsr, src->vcsr);
csr_write(vstart, src->vstart);
However, there is no operation that restores vl or vtype.
Test case
The following test can check whether vl and vtype are preserved across two S-mode domain contexts.
This is a proposed reproducer based on the source code analysis.
Domain A
Configure Vector execution with SEW=16, LMUL=1, and vl=1.
li t0, 1
vsetvli t0, t0, e16, m1, ta, ma
csrr a0, vl
csrr a1, vtype
Expected state before switching:
vl = 0x1
vtype = 0xc8
Domain B
Change the Vector configuration to SEW=8, LMUL=1, and vl=0.
li t0, 0
vsetvli t0, t0, e8, m1, ta, ma
Domain B now has:
vl = 0x0
vtype = 0xc0
Domain A resumes
Read the Vector configuration again.
csrr a0, vl
csrr a1, vtype
Expected behavior
When Domain A resumes, OpenSBI should restore its original Vector configuration.
vl = 0x1
vtype = 0xc8
The Vector configuration of Domain B should not overwrite the saved configuration of Domain A.
Actual behavior
The source code does not save or restore vl and vtype.
The current implementation only preserves vcsr, vstart, and the Vector register file.
The whole-register load and store instructions used for Vector context switching do not restore the previous vl and vtype configuration.
Therefore, the source appears to leave these two CSRs outside the domain context preservation mechanism.
The runtime result of the proposed reproducer has not yet been verified.
Proposed fix
Add vl and vtype to struct sbi_vector_context.
Old code
struct sbi_vector_context {
unsigned long vcsr;
unsigned long vstart;
uint8_t vregs[];
};
New code
struct sbi_vector_context {
unsigned long vcsr;
unsigned long vstart;
unsigned long vl;
unsigned long vtype;
uint8_t vregs[];
};
In sbi_vector_save(), save the missing CSRs.
Old code
dst->vcsr = csr_read(vcsr);
dst->vstart = csr_read(vstart);
New code
dst->vcsr = csr_read(vcsr);
dst->vstart = csr_read(vstart);
dst->vl = csr_read(vl);
dst->vtype = csr_read(vtype);
In sbi_vector_restore(), restore vl and vtype using the vsetvl instruction.
These CSRs cannot be restored using ordinary CSR writes.
Old code
csr_write(vcsr, src->vcsr);
csr_write(vstart, src->vstart);
New code
asm volatile(
".option push\n\t"
".option arch, +v\n\t"
"vsetvl zero, %0, %1\n\t"
".option pop\n\t"
:
: "r"(src->vl), "r"(src->vtype)
: "memory"
);
csr_write(vcsr, src->vcsr);
csr_write(vstart, src->vstart);
The vsetvl instruction restores the saved Vector length and configuration.
It should execute after the Vector register file is restored.
The vstart CSR should be restored after vsetvl, because Vector configuration instructions reset vstart.
This is a proposed fix and has not yet been tested.
Explanation
The RISC-V Vector extension defines vl and vtype as architectural state.
vl specifies the active Vector length.
vtype specifies the Vector configuration, including the selected element width, register grouping, and policy bits.
Although these registers are not part of the Vector register file, they affect the execution of Vector instructions.
Saving v0 to v31 without preserving the corresponding vl and vtype values may leave the restored context with an incorrect Vector configuration.
The existing context switching code already attempts to preserve Vector architectural state.
Adding vl and vtype would complete the preservation of these Vector control registers.
Impact
Missing vl and vtype preservation may cause:
- Incorrect Vector length after a domain switch.
- Incorrect Vector element width or register grouping.
- Unexpected Vector instruction behavior.
- Incorrect Vector calculations.
- Unexpected illegal-instruction traps when a restored Vector configuration is invalid.
- Interference between domains using different Vector configurations.
If the affected contexts belong to independent security domains, incorrect restoration may also have isolation implications.
However, the current source analysis does not establish an exploitable security vulnerability.
Environment
- Project: OpenSBI
- Commit reviewed: 6ad246a1494807ee91e36ffcf60e4c30bb309d45
- Architecture: RISC-V RV64
- Extension: RISC-V Vector
- Affected files:
lib/sbi/sbi_vector.candinclude/sbi/sbi_vector.h
The issue applies to the inspected Vector context implementation when Vector support is compiled and enabled.
- Dominant language
- C
- Stars
- 1.6k
- Forks
- 734
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from riscv-software-src/opensbi
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
riscv-software-src/opensbi#425 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
riscv-software-src/opensbi#424 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
riscv-software-src/opensbi#439 ·
-
sbi_trap_redirect() does not clear SPELP when the interrupted context had no landing pad expectedOpen
Difficulty 3/5 1-2 days Newbie friendliness 67/100
riscv-software-src/opensbi#438 ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
riscv-software-src/opensbi#436 · 3 comments ·
All issues in riscv-software-src/opensbi
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
libreswan/libreswan#3185 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
category:port-update
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
llvm:instcombine
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
llvm/llvm-project#246503 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day