* [PATCH v4] riscv: Discard vector state on syscalls
@ 2023-06-29 14:22 Björn Töpel
2023-07-05 3:50 ` Andy Chiu
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Björn Töpel @ 2023-06-29 14:22 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, linux-riscv, Andy Chiu
Cc: Björn Töpel, linux-kernel, linux, Palmer Dabbelt,
Rémi Denis-Courmont, Darius Rad, Conor Dooley
From: Björn Töpel <bjorn@rivosinc.com>
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/ # [1]
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>
---
v3->v4:
Fixed build CONFIG_RISCV_ISA_V=n builds (Conor)
v2->v3:
Set state to Dirty after discard, for proper ptrace() handling
(Andy)
v1->v2:
Proper register restore for initial state (Andy)
Set registers to 1s, and not 0s (Darius)
---
arch/riscv/include/asm/vector.h | 34 +++++++++++++++++++++++++++++++++
arch/riscv/kernel/traps.c | 2 ++
2 files changed, 36 insertions(+)
diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h
index 04c0b07bf6cd..3d78930cab51 100644
--- a/arch/riscv/include/asm/vector.h
+++ b/arch/riscv/include/asm/vector.h
@@ -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)
diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index 5158961ea977..5ff63a784a6d 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -296,6 +296,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)
base-commit: 488833ccdcac118da16701f4ee0673b20ba47fe3
--
2.39.2
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v4] riscv: Discard vector state on syscalls
2023-06-29 14:22 [PATCH v4] riscv: Discard vector state on syscalls Björn Töpel
@ 2023-07-05 3:50 ` Andy Chiu
2023-07-05 23:38 ` Palmer Dabbelt
2023-07-05 23:50 ` patchwork-bot+linux-riscv
2 siblings, 0 replies; 4+ messages in thread
From: Andy Chiu @ 2023-07-05 3:50 UTC (permalink / raw)
To: Björn Töpel
Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, linux-riscv,
Björn Töpel, linux-kernel, linux, Palmer Dabbelt,
Rémi Denis-Courmont, Darius Rad, Conor Dooley
On Thu, Jun 29, 2023 at 10:22 PM Björn Töpel <bjorn@kernel.org> wrote:
>
> From: Björn Töpel <bjorn@rivosinc.com>
>
> 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/ # [1]
> 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>
Thanks,
Reviewed-by: Andy Chiu <andy.chiu@sifive.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v4] riscv: Discard vector state on syscalls
2023-06-29 14:22 [PATCH v4] riscv: Discard vector state on syscalls Björn Töpel
2023-07-05 3:50 ` Andy Chiu
@ 2023-07-05 23:38 ` Palmer Dabbelt
2023-07-05 23:50 ` patchwork-bot+linux-riscv
2 siblings, 0 replies; 4+ messages in thread
From: Palmer Dabbelt @ 2023-07-05 23:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, linux-riscv, Andy Chiu,
=C3=B6rn_T=C3=B6pel?=
Cc: =C3=B6rn_T=C3=B6pel?=, linux-kernel, linux,
C3=A9mi_Denis-Courmont?=, Darius Rad, Conor Dooley
On Thu, 29 Jun 2023 16:22:28 +0200, Björn Töpel wrote:
> 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.
>
> [...]
Applied, thanks!
[1/1] riscv: Discard vector state on syscalls
https://git.kernel.org/palmer/c/9657e9b7d253
Best regards,
--
Palmer Dabbelt <palmer@rivosinc.com>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v4] riscv: Discard vector state on syscalls
2023-06-29 14:22 [PATCH v4] riscv: Discard vector state on syscalls Björn Töpel
2023-07-05 3:50 ` Andy Chiu
2023-07-05 23:38 ` Palmer Dabbelt
@ 2023-07-05 23:50 ` patchwork-bot+linux-riscv
2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+linux-riscv @ 2023-07-05 23:50 UTC (permalink / raw)
To: =?utf-8?b?QmrDtnJuIFTDtnBlbCA8Ympvcm5Aa2VybmVsLm9yZz4=?=
Cc: linux-riscv, paul.walmsley, palmer, aou, andy.chiu, bjorn,
linux-kernel, linux, palmer, remi, darius, conor.dooley
Hello:
This patch was applied to riscv/linux.git (for-next)
by Palmer Dabbelt <palmer@rivosinc.com>:
On Thu, 29 Jun 2023 16:22:28 +0200 you wrote:
> From: Björn Töpel <bjorn@rivosinc.com>
>
> 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.
>
> [...]
Here is the summary with links:
- [v4] riscv: Discard vector state on syscalls
https://git.kernel.org/riscv/c/9657e9b7d253
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-07-05 23:50 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-29 14:22 [PATCH v4] riscv: Discard vector state on syscalls Björn Töpel
2023-07-05 3:50 ` Andy Chiu
2023-07-05 23:38 ` Palmer Dabbelt
2023-07-05 23:50 ` patchwork-bot+linux-riscv
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox