riscv

Context swap for RISC-V vector extensions


This is a question about the vector extension in RISC-V. Currently, I'm considering adapting my custom RTOS to support the vector extension.

Upon reviewing the spec for the vector extension, I've learned that the size of vector registers depends on vlen. When performing a context swap of vector registers between tasks, if vlen is 64 bits or less, it would suffice to execute the vle64.v instruction 31 times (excluding v0). However, if vlen is 2048 or 4096, the time required for context swapping of vector registers becomes immense.

If managing context on a per-task basis, it can be anticipated that significant time will be consumed for context swapping every time a task switches. In vector extension version 1.0-rc2-draft, it's explained that the mstatus.VS field is provided to avoid unnecessary context swaps. However, context swapping cannot be avoided when switching tasks because each task has its own context, making context swapping necessary.

So, how should one go about performing context swaps for the vector extension?


Solution

  • There are some importants to consider:

    1. Ultimately, if there are multiple tasks using vector registers then there is no other option than to save and restore vector context when context switching between those tasks.

    2. The use of vector instructions and registers is relatively uncommon in the general case.

    3. As a result of 1 and 2, operating system kernels tend to manage vector register context only if there is evidence that the user-space process/thread is actually using vector registers. If not, there is no need to save/restore the vector registers and hence no cost.

    The way this is supposed to work with RISC-V's 'V' extension is, as you have noticed already, dependent on the use of the mstatus.VS bitfield.

    The algorithm would be akin to:

    1. When the task is created, mstatus.VS is set to 'Off'.

    2. If the task attempts to use vector registers the processor will get an illegal instruction exception.

    3. In response to 2, the OS kernel sets some state in the task's control block signifying that it has used vector registers. The task is then allowed to proceed.

    4. When it is time to switch away this task's context (timeslice expiry/interrupt etc), the state in 3 is used to determine whether the vector register context needs to be saved within the task's context data structure or not.

    The above is what is essentially meant by 'Software will typically use VS to reduce context-swap overhead' in the RISC-V 'V' spec.

    Hope that helps.