Skip to content

Commit 9657e9b

Browse files
bjorn-rivospalmer-dabbelt
authored andcommitted
riscv: Discard vector state on syscalls
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/[email protected]/ # [1] Suggested-by: Darius Rad <[email protected]> Suggested-by: Palmer Dabbelt <[email protected]> Suggested-by: Rémi Denis-Courmont <[email protected]> Signed-off-by: Björn Töpel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 85fadc0 commit 9657e9b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

arch/riscv/include/asm/vector.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ static inline void __riscv_v_vstate_clean(struct pt_regs *regs)
3333
regs->status = (regs->status & ~SR_VS) | SR_VS_CLEAN;
3434
}
3535

36+
static inline void __riscv_v_vstate_dirty(struct pt_regs *regs)
37+
{
38+
regs->status = (regs->status & ~SR_VS) | SR_VS_DIRTY;
39+
}
40+
3641
static inline void riscv_v_vstate_off(struct pt_regs *regs)
3742
{
3843
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_
128133
riscv_v_disable();
129134
}
130135

136+
static inline void __riscv_v_vstate_discard(void)
137+
{
138+
unsigned long vl, vtype_inval = 1UL << (BITS_PER_LONG - 1);
139+
140+
riscv_v_enable();
141+
asm volatile (
142+
".option push\n\t"
143+
".option arch, +v\n\t"
144+
"vsetvli %0, x0, e8, m8, ta, ma\n\t"
145+
"vmv.v.i v0, -1\n\t"
146+
"vmv.v.i v8, -1\n\t"
147+
"vmv.v.i v16, -1\n\t"
148+
"vmv.v.i v24, -1\n\t"
149+
"vsetvl %0, x0, %1\n\t"
150+
".option pop\n\t"
151+
: "=&r" (vl) : "r" (vtype_inval) : "memory");
152+
riscv_v_disable();
153+
}
154+
155+
static inline void riscv_v_vstate_discard(struct pt_regs *regs)
156+
{
157+
if ((regs->status & SR_VS) == SR_VS_OFF)
158+
return;
159+
160+
__riscv_v_vstate_discard();
161+
__riscv_v_vstate_dirty(regs);
162+
}
163+
131164
static inline void riscv_v_vstate_save(struct task_struct *task,
132165
struct pt_regs *regs)
133166
{
@@ -173,6 +206,7 @@ static inline bool riscv_v_first_use_handler(struct pt_regs *regs) { return fals
173206
static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; }
174207
static inline bool riscv_v_vstate_ctrl_user_allowed(void) { return false; }
175208
#define riscv_v_vsize (0)
209+
#define riscv_v_vstate_discard(regs) do {} while (0)
176210
#define riscv_v_vstate_save(task, regs) do {} while (0)
177211
#define riscv_v_vstate_restore(task, regs) do {} while (0)
178212
#define __switch_to_vector(__prev, __next) do {} while (0)

arch/riscv/kernel/traps.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs)
302302
regs->epc += 4;
303303
regs->orig_a0 = regs->a0;
304304

305+
riscv_v_vstate_discard(regs);
306+
305307
syscall = syscall_enter_from_user_mode(regs, syscall);
306308

307309
if (syscall < NR_syscalls)

0 commit comments

Comments
 (0)