lib: Missing vl and vtype preservation during Vector domain context switches

Open
#434 1 comment 0 reactions 0 assignees View on GitHub

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

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:

  • v0 to v31
  • vcsr, including vxrm and vxsat
  • vstart

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.c and include/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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from riscv-software-src/opensbi

All issues in riscv-software-src/opensbi

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.