riscv: Discard vector state on syscalls
authorBjörn Töpel <bjorn@rivosinc.com>
Thu, 29 Jun 2023 14:22:28 +0000 (16:22 +0200)
committerPalmer Dabbelt <palmer@rivosinc.com>
Tue, 4 Jul 2023 15:59:24 +0000 (08:59 -0700)
The RISC-V vector specification states:
  Executing a system call causes all caller-saved vector registers
  (v0-v31, vl, vtype) and vstart to become unspecified.

The vector registers are set to all 1s, vill is set (invalid), and the
vector status is set to Dirty.

That way we can prevent userspace from accidentally relying on the
stated save.

Rémi pointed out [1] that writing to the registers might be
superfluous, and setting vill is sufficient.

Link: https://lore.kernel.org/linux-riscv/12784326.9UPPK3MAeB@basile.remlab.net/
Suggested-by: Darius Rad <darius@bluespec.com>
Suggested-by: Palmer Dabbelt <palmer@rivosinc.com>
Suggested-by: Rémi Denis-Courmont <remi@remlab.net>
Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
Link: https://lore.kernel.org/r/20230629142228.1125715-1-bjorn@kernel.org
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
arch/riscv/include/asm/vector.h
arch/riscv/kernel/traps.c

index 04c0b07bf6cdff68495a1cf25bad93703ceb3a25..3d78930cab51381ef7c7fd853049957e285801c7 100644 (file)
@@ -33,6 +33,11 @@ static inline void __riscv_v_vstate_clean(struct pt_regs *regs)
        regs->status = (regs->status & ~SR_VS) | SR_VS_CLEAN;
 }
 
+static inline void __riscv_v_vstate_dirty(struct pt_regs *regs)
+{
+       regs->status = (regs->status & ~SR_VS) | SR_VS_DIRTY;
+}
+
 static inline void riscv_v_vstate_off(struct pt_regs *regs)
 {
        regs->status = (regs->status & ~SR_VS) | SR_VS_OFF;
@@ -128,6 +133,34 @@ static inline void __riscv_v_vstate_restore(struct __riscv_v_ext_state *restore_
        riscv_v_disable();
 }
 
+static inline void __riscv_v_vstate_discard(void)
+{
+       unsigned long vl, vtype_inval = 1UL << (BITS_PER_LONG - 1);
+
+       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, -1\n\t"
+               "vmv.v.i        v8, -1\n\t"
+               "vmv.v.i        v16, -1\n\t"
+               "vmv.v.i        v24, -1\n\t"
+               "vsetvl         %0, x0, %1\n\t"
+               ".option pop\n\t"
+               : "=&r" (vl) : "r" (vtype_inval) : "memory");
+       riscv_v_disable();
+}
+
+static inline void riscv_v_vstate_discard(struct pt_regs *regs)
+{
+       if ((regs->status & SR_VS) == SR_VS_OFF)
+               return;
+
+       __riscv_v_vstate_discard();
+       __riscv_v_vstate_dirty(regs);
+}
+
 static inline void riscv_v_vstate_save(struct task_struct *task,
                                       struct pt_regs *regs)
 {
@@ -173,6 +206,7 @@ static inline bool riscv_v_first_use_handler(struct pt_regs *regs) { return fals
 static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; }
 static inline bool riscv_v_vstate_ctrl_user_allowed(void) { return false; }
 #define riscv_v_vsize (0)
+#define riscv_v_vstate_discard(regs)           do {} while (0)
 #define riscv_v_vstate_save(task, regs)                do {} while (0)
 #define riscv_v_vstate_restore(task, regs)     do {} while (0)
 #define __switch_to_vector(__prev, __next)     do {} while (0)
index bc02b282a4036b958420d2ddf1e105e6736df001..f910dfccbf5d2aaf5b838cdbfd809f8e937bdfd5 100644 (file)
@@ -302,6 +302,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_discard(regs);
+
                syscall = syscall_enter_from_user_mode(regs, syscall);
 
                if (syscall < NR_syscalls)