[PATCH] RISC-V: Clobber V registers on syscalls

Björn Töpel bjorn at kernel.org
Fri Jun 16 13:12:14 PDT 2023


Palmer Dabbelt <palmer at rivosinc.com> writes:

> The V registers are clobbered by standard ABI functions, so userspace
> probably doesn't have anything useful in them by the time we get to the
> kernel.  So let's just document that they're clobbered by syscalls and
> proactively clobber them.
>
> Signed-off-by: Palmer Dabbelt <palmer at rivosinc.com>
> ---
> IIRC we'd talked about doing this, but I didn't see anything in the
> docs.  I figure it's better to just proactively clobber the registers on
> syscalls, as that way userspace can't end up accidentally depending on
> them.
> ---
>  Documentation/riscv/vector.rst | 5 +++++
>  arch/riscv/kernel/traps.c      | 2 ++
>  2 files changed, 7 insertions(+)
>
> diff --git a/Documentation/riscv/vector.rst b/Documentation/riscv/vector.rst
> index 48f189d79e41..a4dfa954215b 100644
> --- a/Documentation/riscv/vector.rst
> +++ b/Documentation/riscv/vector.rst
> @@ -130,3 +130,8 @@ processes in form of sysctl knob:
>  
>      Modifying the system default enablement status does not affect the enablement
>      status of any existing process of thread that do not make an execve() call.
> +
> +3.  Vector Register State Across System Calls
> +---------------------------------------------
> +
> +Vector registers are clobbered by system calls.
> diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
> index 05ffdcd1424e..bb99a6379b37 100644
> --- a/arch/riscv/kernel/traps.c
> +++ b/arch/riscv/kernel/traps.c
> @@ -295,6 +295,8 @@ asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs)
>  		regs->epc += 4;
>  		regs->orig_a0 = regs->a0;
>  
> +		riscv_v_vstate_off(regs);
> +

Not off, right? Isn't it __riscv_v_vstate_clean() that you'd like to
call? Something like:

static void vstate_discard(struct pt_regs *regs)
{
       if ((regs->status & SR_VS) == SR_VS_DIRTY)
               __riscv_v_vstate_clean(regs);
}

Complemented by a !V config variant.


Björn



More information about the linux-riscv mailing list