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

Björn Töpel bjorn at kernel.org
Wed Jun 21 14:40:56 PDT 2023


Andy Chiu <andy.chiu at sifive.com> writes:

>> +static inline void riscv_v_vstate_discard(struct pt_regs *regs)
>> +{
>> +       unsigned long vl;
>> +
>> +       if (!riscv_v_vstate_query(regs))
>> +               return;
>> +
>> +       riscv_v_vstate_on(regs);
>
> Do we need this riscv_v_vstate_on()?  If it is not on we'd return
> early in the previous "if" statement, right?

riscv_v_vstate_on() just set the state to Initial, right? Or do you mean
that riscv_v_vstate_query() is too much, and we should only check if the
state is dirty?

>> +
>> +       riscv_v_enable();
>> +       asm volatile (
>> +               ".option push\n\t"
>> +               ".option arch, +v\n\t"
>> +               "vsetvli        %0, x0, e8, m8, ta, ma\n\t"
>> +               "vmv.v.i        v0, 0\n\t"
>> +               "vmv.v.i        v8, 0\n\t"
>> +               "vmv.v.i        v16, 0\n\t"
>> +               "vmv.v.i        v24, 0\n\t"
>> +               ".option pop\n\t"
>> +               : "=&r" (vl) : : "memory");
>> +       riscv_v_disable();
>
> Maybe consider cleaning the vstate (status.vs) here. As such we don't
> have to save V during context switch. 

It's late, and I'm slower than usual. The regs are cleared, and the
state is Initial. No save on context switch, but restore, right?

> Or, maybe we could set vstate as off during syscall and discard V-reg
> + restore status.VS when returning back to userspace?

Hmm, interesting. We need to track the status.VS to restore somewhere...


Björn



More information about the linux-riscv mailing list