[PATCH v3] RISC-V: Document that V registers are clobbered on syscalls

Maciej W. Rozycki macro at orcam.me.uk
Thu Jul 13 05:27:24 PDT 2023


On Mon, 19 Jun 2023, Palmer Dabbelt wrote:

> This is included in the ISA manual, but it's pretty common for bits of
> the ISA manual that are actually ABI to change.  So let's document it
> explicitly.

 I'm probably coming much too late for this to have any effect, but I find 
this design decision quite odd.  The V extension spec has this note:

"This scheme allows system calls that cause context switches to avoid 
saving and later restoring the vector registers."

however declaring vector registers syscall-clobbered does not actually let 
context switches in a preemptively multitasked OS such as Linux refrain 
from saving/restoring these registers, because switches will quite often 
if not predominantly (depending on application) happen asynchronously in 
response to a hardware interrupt such as a timer tick.  In which case 
vector registers will have to be saved for the old context and loaded for 
the new one anyway, and two variants of the context switch handler will be 
needed to take any performance advantage envisaged by the spec (and then 
the case of a syscall switching to a context previously preempted by a 
hardware interrupt will have to be handled correctly, where we can ignore 
values from the old context, but need to load ones for the new context).

 What has been the usual approach across CPU architectures for decades now 
to reduce expensive moves of extra register files, such as FPU, vector, 
DSP, etc. on context switches is just setting a privileged control bit 
that makes any instruction accessing such a register file trap and then 
the handler loads the file according to its previously saved contents for 
the current context, clears the control bit and restarts the instruction 
that faulted.  Depending on the OS design decision saving for the old 
context is done either in the trap handler beforehand or in the original 
context switch that took control away from the old context.  Consequently 
extra register files are only saved and loaded if ever touched by the 
context concerned, even if contexts are preempted by a hardware interrupt.

 Have I been missing anything here?

 FWIW,

  Maciej



More information about the linux-riscv mailing list