riscv: Add vector ISA support
Subsystems (5)
v13 cover riscv: Add vector ISA support
This patchset is implemented based on vector 1.0 spec to add vector support in riscv Linux kernel. There are some assumptions for this implementations.
1. We assume all harts has the same ISA in the system.
2. We disable vector in both kernel andy user space [1] by default. Only enable an user's vector after an illegal instruction trap where it actually starts executing vector (the first-use trap [2]).
3. We detect "riscv,isa" to determine whether vector is support or not.
We defined a new structure __riscv_v_state in struct thread_struct to save /restore the vector related registers. It is used for both kernel space and user space.
- In kernel space, the datap pointer in __riscv_v_state will be allocated to save vector registers.
- In user space,
- In signal handler of user space, the structure is placed right after __riscv_ctx_hdr, which is embedded in fp reserved aera. This is required to avoid ABI break [2]. And datap points to the end of __riscv_v_state.
- In ptrace, the data will be put in ubuf in which we use riscv_vr_get()/riscv_vr_set() to get or set the __riscv_v_state data structure from/to it, datap pointer would be zeroed and vector registers will be copied to the address right after the __riscv_v_state structure in ubuf.
This patchset is rebased to v6.2-rc1 and it is tested by running several vector programs simultaneously. It also can get the correct ucontext_t in signal handler and restore correct context after sigreturn. It is also tested with ptrace() syscall to use PTRACE_GETREGSET/PTRACE_SETREGSET to get/set the vector registers.
Source tree: https://github.com/sifive/riscv-linux/tree/riscv/for-next/vector-v13
Links:
- [1] https://lore.kernel.org/all/20220921214439.1491510-17-stillson at rivosinc.com/
- [2] https://lore.kernel.org/all/73c0124c-4794-6e40-460c-b26df407f322 at rivosinc.com/T/#u
---
Changelog V13
- Rebase to latest risc-v next (v6.2-rc1)
- Re-organize the series to comply with bisect-ability
- Improve task switch with inline assembly
- Re-structure the signal frame to avoid user ABI break.
- Implemnt first-use trap and drop prctl for per-task V state enablement. Also, redirect this trap from hs to vs for kvm setup.
- Do not expose V context in ptrace/sigframe until the task start using V. But still reserve V context for size ofsigframe reported by auxv.
- Drop the kernel mode vector and leave it to another (future) series.
Changelog V12 (Chris)
- rebases to some point after v5.18-rc6
- add prctl to control per-process V state
Chnagelog V10
- Rebase to v5.18-rc6
- Merge several patches
- Refine codes
- Fix bugs
- Add kvm vector support
Changelog V9
- Rebase to v5.15
- Merge several patches
- Refine codes
- Fix a kernel panic issue
Changelog V8
- Rebase to v5.14
- Refine struct __riscv_v_state with struct __riscv_ctx_hdr
- Refine has_vector into a static key
- Defined __reserved space in struct sigcontext for vector and future extensions
Changelog V7
- Add support for kernel mode vector
- Add vector extension XOR implementation
- Optimize task switch codes of vector
- Allocate space for vector registers in start_thread()
- Fix an illegal instruction exception when accessing vlenb
- Optimize vector registers initialization
- Initialize vector registers with proper vsetvli then it can work normally
- Refine ptrace porting due to generic API changed
- Code clean up
Changelog V6
- Replace vle.v/vse.v instructions with vle8.v/vse8.v based on 0.9 spec
- Add comments based on mailinglist feedback
- Fix rv32 build error
Changelog V5
- Using regset_size() correctly in generic ptrace
- Fix the ptrace porting
- Fix compile warning
Changelog V4
- Support dynamic vlen
- Fix bugs: lazy save/resotre, not saving vtype
- Update VS bit offset based on latest vector spec
- Add new vector csr based on latest vector spec
- Code refine and removed unused macros
Changelog V3
- Rebase linux-5.6-rc3 and tested with qemu
- Seperate patches with Anup's advice
- Give out a ABI puzzle with unlimited vlen
Changelog V2
- Fixup typo "vecotr, fstate_save->vstate_save".
- Fixup wrong saved registers' length in vector.S.
- Seperate unrelated patches from this one.
Andy Chiu (6):
riscv: Clear vector regfile on bootup
riscv: Disable Vector Instructions for kernel itself
riscv: Introduce Vector enable/disable helpers
riscv: Allocate user's vector context in the first-use trap
riscv: signal: check fp-reserved words unconditionally
riscv: kvm: redirect illegal instruction traps to guests
Greentime Hu (7):
riscv: Add new csr defines related to vector extension
riscv: Introduce riscv_vsize to record size of Vector context
riscv: Introduce struct/helpers to save/restore per-task Vector state
riscv: Add task switch support for vector
riscv: Add ptrace vector support
riscv: signal: Add sigcontext save/restore for vector
riscv: Fix a kernel panic issue if $s2 is set to a specific value
before entering Linux
Guo Ren (3):
riscv: Rename __switch_to_aux -> fpu
riscv: Extending cpufeature.c to detect V-extension
riscv: Enable Vector code to be built
Vincent Chen (3):
riscv: signal: Report signal frame size to userspace via auxv
riscv: Add V extension to KVM ISA
riscv: KVM: Add vector lazy save/restore support
arch/riscv/Kconfig | 10 ++ arch/riscv/Makefile | 7 + arch/riscv/include/asm/csr.h | 18 +- arch/riscv/include/asm/elf.h | 9 + arch/riscv/include/asm/hwcap.h | 4 + arch/riscv/include/asm/insn.h | 24 +++ arch/riscv/include/asm/kvm_host.h | 2 + arch/riscv/include/asm/kvm_vcpu_vector.h | 77 +++++++++ arch/riscv/include/asm/processor.h | 3 + arch/riscv/include/asm/switch_to.h | 24 ++- arch/riscv/include/asm/thread_info.h | 3 + arch/riscv/include/asm/vector.h | 152 +++++++++++++++++ arch/riscv/include/uapi/asm/auxvec.h | 1 + arch/riscv/include/uapi/asm/hwcap.h | 1 + arch/riscv/include/uapi/asm/kvm.h | 8 + arch/riscv/include/uapi/asm/ptrace.h | 39 +++++ arch/riscv/include/uapi/asm/sigcontext.h | 16 +- arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/cpufeature.c | 22 +++ arch/riscv/kernel/entry.S | 6 +- arch/riscv/kernel/head.S | 37 ++++- arch/riscv/kernel/process.c | 18 ++ arch/riscv/kernel/ptrace.c | 71 ++++++++ arch/riscv/kernel/setup.c | 3 + arch/riscv/kernel/signal.c | 202 +++++++++++++++++++---- arch/riscv/kernel/traps.c | 14 +- arch/riscv/kernel/vector.c | 89 ++++++++++ arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu.c | 31 ++++ arch/riscv/kvm/vcpu_exit.c | 15 ++ arch/riscv/kvm/vcpu_vector.c | 177 ++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 32 files changed, 1041 insertions(+), 45 deletions(-) create mode 100644 arch/riscv/include/asm/kvm_vcpu_vector.h create mode 100644 arch/riscv/include/asm/vector.h create mode 100644 arch/riscv/kernel/vector.c create mode 100644 arch/riscv/kvm/vcpu_vector.c
Signature · 1 line
2.17.1
PATCH v13 1/19 riscv: Rename __switch_to_aux -> fpu
From: Guo Ren <[email protected]>
The name of __switch_to_aux is not clear and rename it with the determine function: __switch_to_fpu. Next we could add other regs' switch.
--- arch/riscv/include/asm/switch_to.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/riscv/include/asm/switch_to.h +3 -3
@@ -46,7 +46,7 @@ static inline void fstate_restore(struct task_struct *task, } } -static inline void __switch_to_aux (struct task_struct *prev, +static inline void __switch_to_fpu (struct task_struct *prev,struct task_struct *next) {struct pt_regs *regs;
@@ -65,7 +65,7 @@ static __always_inline bool has_fpu(void) static __always_inline bool has_fpu(void) { return false; }#define fstate_save (task, regs) do { } while (0)#define fstate_restore (task, regs) do { } while (0) -#define __switch_to_aux (__prev, __next) do { } while (0) +#define __switch_to_fpu (__prev, __next) do { } while (0)#endif extern struct task_struct *__switch_to(struct task_struct *,
@@ -76,7 +76,7 @@ do { \struct task_struct *__prev = (prev); \struct task_struct *__next = (next); \if (has_fpu ()) \ -__switch_to_aux (__prev, __next); \ +__switch_to_fpu (__prev, __next); \ ((last) =__switch_to (__prev, __next)); \ }while (0 )
Signature · 1 line
2.17.1
Re: [PATCH -next v13 01/19] riscv: Rename __switch_to_aux -> fpu
Quoted 11 lines · Andy Chiu
From: Guo Ren <[email protected]>
The name of
__switch_to_auxis not clear and rename it with the determine function:__switch_to_fpu. Next we could add other regs' switch.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Signed-off-by Greentime Hu <[email protected]>Reviewed-by Anup Patel <[email protected]>Reviewed-by Palmer Dabbelt <[email protected]>
With your SoB added here:
Quoted 38 lines
--- arch/riscv/include/asm/switch_to.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)arch/riscv/include/asm/switch_to.h +3
-3@@ -46,7 +46,7 @@ static inline void fstate_restore(struct task_struct *task, } } -static inline void __switch_to_aux (struct task_struct *prev, +static inline void __switch_to_fpu (struct task_struct *prev,struct task_struct *next) {struct pt_regs *regs;@@ -65,7 +65,7 @@ static __always_inline bool has_fpu(void) static __always_inline bool has_fpu(void) { return false; }#define fstate_save (task, regs) do { } while (0)#define fstate_restore (task, regs) do { } while (0) -#define __switch_to_aux (__prev, __next) do { } while (0) +#define __switch_to_fpu (__prev, __next) do { } while (0)#endif extern struct task_struct *__switch_to(struct task_struct *,@@ -76,7 +76,7 @@ do { \struct task_struct *__prev = (prev); \struct task_struct *__next = (next); \if (has_fpu ()) \ -__switch_to_aux (__prev, __next); \ +__switch_to_fpu (__prev, __next); \ ((last) =__switch_to (__prev, __next)); \ }while (0 )Signature · 1 line
2.17.1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230125/c1603353/attachment-0001.sig>
PATCH v13 2/19 riscv: Extending cpufeature.c to detect V-extension
In reply to Andy ChiuFrom: Guo Ren <[email protected]>
Add V-extension into riscv_isa_ext_keys array and detect it with isa string parsing.
--- arch/riscv/include/asm/hwcap.h | 4 ++++ arch/riscv/include/asm/vector.h | 26 ++++++++++++++++++++++++++ arch/riscv/include/uapi/asm/hwcap.h | 1 + arch/riscv/kernel/cpufeature.c | 12 ++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 arch/riscv/include/asm/vector.h
arch/riscv/include/asm/hwcap.h +4 -0
@@ -35,6 +35,7 @@ extern unsigned long elf_hwcap;#define RISCV_ISA_EXT_m ('m' - 'a')#define RISCV_ISA_EXT_s ('s' - 'a')#define RISCV_ISA_EXT_u ('u' - 'a') +#define RISCV_ISA_EXT_v ('v' - 'a')/* * Increse this to higher value as kernel support more ISA extensions.
@@ -73,6 +74,7 @@ static_assert(RISCV_ISA_EXT_ID_MAX <= RISCV_ISA_EXT_MAX);enum riscv_isa_ext_key {RISCV_ISA_EXT_KEY_FPU ,/* For 'F' and 'D' */ RISCV_ISA_EXT_KEY_SVINVAL , +RISCV_ISA_EXT_KEY_VECTOR ,/* For 'V' */ RISCV_ISA_EXT_KEY_ZIHINTPAUSE ,RISCV_ISA_EXT_KEY_MAX , };
@@ -95,6 +97,8 @@ static __always_inline int riscv_isa_ext2key(int num)return RISCV_ISA_EXT_KEY_FPU ;case RISCV_ISA_EXT_SVINVAL :return RISCV_ISA_EXT_KEY_SVINVAL ; +case RISCV_ISA_EXT_v: +return RISCV_ISA_EXT_KEY_VECTOR ;case RISCV_ISA_EXT_ZIHINTPAUSE :return RISCV_ISA_EXT_KEY_ZIHINTPAUSE ;default :
arch/riscv/include/asm/vector.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2020 SiFive +*/ + +#ifndef __ASM_RISCV_VECTOR_H +#define __ASM_RISCV_VECTOR_H + +#include <linux/types.h> + +#ifdef CONFIG_RISCV_ISA_V + +#include <asm/hwcap.h> + +static __always_inline bool has_vector(void) +{ +return static_branch_likely (&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_VECTOR ]); +} + +#else /* ! CONFIG_RISCV_ISA_V */ + +static __always_inline bool has_vector(void) { return false; } + +#endif /* CONFIG_RISCV_ISA_V */ + +#endif /* ! __ASM_RISCV_VECTOR_H */
arch/riscv/include/uapi/asm/hwcap.h +1 -0
@@ -21,5 +21,6 @@#define COMPAT_HWCAP_ISA_F (1 << ('F' - 'A'))#define COMPAT_HWCAP_ISA_D (1 << ('D' - 'A'))#define COMPAT_HWCAP_ISA_C (1 << ('C' - 'A')) +#define COMPAT_HWCAP_ISA_V (1 << ('V' - 'A'))#endif /* _UAPI_ASM_RISCV_HWCAP_H */
arch/riscv/kernel/cpufeature.c +12 -0
@@ -101,6 +101,7 @@ void __init riscv_fill_hwcap(void) isa2hwcap['f' -'a' ] =COMPAT_HWCAP_ISA_F ; isa2hwcap['d' -'a' ] =COMPAT_HWCAP_ISA_D ; isa2hwcap['c' -'a' ] =COMPAT_HWCAP_ISA_C ; + isa2hwcap['v' -'a' ] =COMPAT_HWCAP_ISA_V ; elf_hwcap =0 ;
@@ -256,6 +257,17 @@ void __init riscv_fill_hwcap(void) elf_hwcap &= ~COMPAT_HWCAP_ISA_F ; } +if (elf_hwcap &COMPAT_HWCAP_ISA_V ) { +#ifndef CONFIG_RISCV_ISA_V +/* +* ISA string in device tree might have 'v' flag, but +* CONFIG_RISCV_ISA_V is disabled in kernel. +* Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled. +*/ + elf_hwcap &= ~COMPAT_HWCAP_ISA_V ; +#endif + } +memset (print_str,0 ,sizeof (print_str));for (i =0 , j =0 ; i <NUM_ALPHA_EXTS ; i++)if (riscv_isa[0 ] &BIT_MASK (i))
Signature · 1 line
2.17.1
Re: [PATCH -next v13 02/19] riscv: Extending cpufeature.c to detect V-extension
Quoted 31 lines · Andy Chiu
From: Guo Ren <[email protected]>
Add V-extension into
riscv_isa_ext_keysarray and detect it with isa string parsing.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Signed-off-by Greentime Hu <[email protected]>Suggested-by Vineet Gupta <[email protected]>Signed-off-by Andy Chiu <[email protected]>--- arch/riscv/include/asm/hwcap.h | 4 ++++ arch/riscv/include/asm/vector.h | 26 ++++++++++++++++++++++++++ arch/riscv/include/uapi/asm/hwcap.h | 1 + arch/riscv/kernel/cpufeature.c | 12 ++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 arch/riscv/include/asm/vector.harch/riscv/include/asm/hwcap.h +1
-0@@ -35,6 +35,7 @@ extern unsigned long elf_hwcap;#define RISCV_ISA_EXT_m ('m' - 'a')#define RISCV_ISA_EXT_s ('s' - 'a')#define RISCV_ISA_EXT_u ('u' - 'a') +#define RISCV_ISA_EXT_v ('v' - 'a')/* * Increse this to higher value as kernel support more ISA extensions.
arch/riscv/include/asm/hwcap.h static_assert(RISCV_ISA_EXT_ID_MAX <= RISCV_ISA_EXT_MAX); · line 77
Quoted 4 lines
arch/riscv/include/asm/hwcap.h +1
-0@@ -73,6 +74,7 @@ static_assert(RISCV_ISA_EXT_ID_MAX <= RISCV_ISA_EXT_MAX);enum riscv_isa_ext_key {RISCV_ISA_EXT_KEY_FPU ,/* For 'F' and 'D' */ RISCV_ISA_EXT_KEY_SVINVAL , +RISCV_ISA_EXT_KEY_VECTOR ,/* For 'V' */
That's obvious surely, no?
arch/riscv/include/asm/hwcap.h static __always_inline int riscv_isa_ext2key(int num)
Quoted 4 lines
arch/riscv/include/asm/hwcap.h +0
-0RISCV_ISA_EXT_KEY_ZIHINTPAUSE , RISCV_ISA_EXT_KEY_MAX, };@@ -95,6 +97,8 @@ static __always_inline int riscv_isa_ext2key(int num)
You should probably check out Jisheng's series that deletes whole sections of this code, including this whole function.
https://lore.kernel.org/all/20230115154953.831-3-jszhang at kernel.org/T/#u
arch/riscv/include/asm/hwcap.h void __init riscv_fill_hwcap(void) · line 268
Quoted 13 lines
arch/riscv/include/asm/hwcap.h +9
-0@@ -256,6 +257,17 @@ void __init riscv_fill_hwcap(void) elf_hwcap &= ~COMPAT_HWCAP_ISA_F ; } +if (elf_hwcap &COMPAT_HWCAP_ISA_V ) { +#ifndef CONFIG_RISCV_ISA_V +/* +* ISA string in device tree might have 'v' flag, but +* CONFIG_RISCV_ISA_V is disabled in kernel. +* Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled. +*/ + elf_hwcap &= ~COMPAT_HWCAP_ISA_V ; +#endif
I know that a later patch in this series calls rvv_enable() here, which I'll comment on there, but I'd rather see IS_ENABLED as opposed to ifdefs in C files where possible.
Thanks,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230125/e5de0580/attachment-0001.sig>
Re: [PATCH -next v13 02/19] riscv: Extending cpufeature.c to detect V-extension
Quoted 40 lines · Conor Dooley
Quoted 31 lines · Andy Chiu
From: Guo Ren <[email protected]>
Add V-extension into
riscv_isa_ext_keysarray and detect it with isa string parsing.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Signed-off-by Greentime Hu <[email protected]>Suggested-by Vineet Gupta <[email protected]>Signed-off-by Andy Chiu <[email protected]>--- arch/riscv/include/asm/hwcap.h | 4 ++++ arch/riscv/include/asm/vector.h | 26 ++++++++++++++++++++++++++ arch/riscv/include/uapi/asm/hwcap.h | 1 + arch/riscv/kernel/cpufeature.c | 12 ++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 arch/riscv/include/asm/vector.harch/riscv/include/asm/hwcap.h +1
-0@@ -35,6 +35,7 @@ extern unsigned long elf_hwcap;#define RISCV_ISA_EXT_m ('m' - 'a')#define RISCV_ISA_EXT_s ('s' - 'a')#define RISCV_ISA_EXT_u ('u' - 'a') +#define RISCV_ISA_EXT_v ('v' - 'a')/* * Increse this to higher value as kernel support more ISA extensions. arch/riscv/include/asm/hwcap.h static_assert(RISCV_ISA_EXT_ID_MAX <= RISCV_ISA_EXT_MAX); · line 77
Quoted 4 lines
arch/riscv/include/asm/hwcap.h +1
-0@@ -73,6 +74,7 @@ static_assert(RISCV_ISA_EXT_ID_MAX <= RISCV_ISA_EXT_MAX);enum riscv_isa_ext_key {RISCV_ISA_EXT_KEY_FPU ,/* For 'F' and 'D' */ RISCV_ISA_EXT_KEY_SVINVAL , +RISCV_ISA_EXT_KEY_VECTOR ,/* For 'V' */ That's obvious surely, no?
Quoted 8 lines
arch/riscv/include/asm/hwcap.h static __always_inline int riscv_isa_ext2key(int num)
Quoted 4 lines
arch/riscv/include/asm/hwcap.h +0
-0RISCV_ISA_EXT_KEY_ZIHINTPAUSE , RISCV_ISA_EXT_KEY_MAX, };@@ -95,6 +97,8 @@ static __always_inline int riscv_isa_ext2key(int num)You should probably check out Jisheng's series that deletes whole sections of this code, including this whole function.
https://lore.kernel.org/all/20230115154953.831-3-jszhang at kernel.org/T/#u
Has that patch merged? It could be solved during the rebase for-next naturally.
Quoted 6 lines
Quoted 6 lines
arch/riscv/include/asm/hwcap.h +0
-0@@ -256,6 +257,17 @@ void __init riscv_fill_hwcap(void) elf_hwcap &= ~COMPAT_HWCAP_ISA_F ; }
Quoted 9 lines
arch/riscv/include/asm/hwcap.h void __init riscv_fill_hwcap(void) · line 268
Quoted 9 lines
arch/riscv/include/asm/hwcap.h +9
-0+if (elf_hwcap &COMPAT_HWCAP_ISA_V ) { +#ifndef CONFIG_RISCV_ISA_V +/* +* ISA string in device tree might have 'v' flag, but +* CONFIG_RISCV_ISA_V is disabled in kernel. +* Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled. +*/ + elf_hwcap &= ~COMPAT_HWCAP_ISA_V ; +#endif
if (elf_hwcap & COMPAT_HWCAP_ISA_V && !IS_ENABLED(CONFIG_RISCV_ISA_V)) {right?
Quoted 8 lines
I know that a later patch in this series calls
rvv_enable()here, which I'll comment on there, but I'd rather seeIS_ENABLEDas opposed to ifdefs in C files where possible.Thanks,
Conor.
Signature · 2 lines
Best Regards Guo Ren
Re: [PATCH -next v13 02/19] riscv: Extending cpufeature.c to detect V-extension
Quoted 49 lines · Guo Ren
Quoted 40 lines · Conor Dooley
Quoted 31 lines · Andy Chiu
From: Guo Ren <[email protected]>
Add V-extension into
riscv_isa_ext_keysarray and detect it with isa string parsing.Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Signed-off-by Greentime Hu <[email protected]>Suggested-by Vineet Gupta <[email protected]>Signed-off-by Andy Chiu <[email protected]>--- arch/riscv/include/asm/hwcap.h | 4 ++++ arch/riscv/include/asm/vector.h | 26 ++++++++++++++++++++++++++ arch/riscv/include/uapi/asm/hwcap.h | 1 + arch/riscv/kernel/cpufeature.c | 12 ++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 arch/riscv/include/asm/vector.harch/riscv/include/asm/hwcap.h +1
-0@@ -35,6 +35,7 @@ extern unsigned long elf_hwcap;#define RISCV_ISA_EXT_m ('m' - 'a')#define RISCV_ISA_EXT_s ('s' - 'a')#define RISCV_ISA_EXT_u ('u' - 'a') +#define RISCV_ISA_EXT_v ('v' - 'a')/* * Increse this to higher value as kernel support more ISA extensions. arch/riscv/include/asm/hwcap.h static_assert(RISCV_ISA_EXT_ID_MAX <= RISCV_ISA_EXT_MAX); · line 77
Quoted 4 lines
arch/riscv/include/asm/hwcap.h +1
-0@@ -73,6 +74,7 @@ static_assert(RISCV_ISA_EXT_ID_MAX <= RISCV_ISA_EXT_MAX);enum riscv_isa_ext_key {RISCV_ISA_EXT_KEY_FPU ,/* For 'F' and 'D' */ RISCV_ISA_EXT_KEY_SVINVAL , +RISCV_ISA_EXT_KEY_VECTOR ,/* For 'V' */ That's obvious surely, no?
Quoted 8 lines
arch/riscv/include/asm/hwcap.h static __always_inline int riscv_isa_ext2key(int num)
Quoted 4 lines
arch/riscv/include/asm/hwcap.h +0
-0RISCV_ISA_EXT_KEY_ZIHINTPAUSE , RISCV_ISA_EXT_KEY_MAX, };@@ -95,6 +97,8 @@ static __always_inline int riscv_isa_ext2key(int num)You should probably check out Jisheng's series that deletes whole sections of this code, including this whole function.
https://lore.kernel.org/all/20230115154953.831-3-jszhang at kernel.org/T/#u
Quoted 1 line
Has that patch merged? It could be solved during the rebase for-next naturally.
Not merged yet. Pretty sure Andy used for-next
as his base so that CI could test it more easily
I was just pointing out it's existence in case he
hadn't seen it.
Hopefully Jishengs stuff will make 6.3 :)
Quoted 29 lines
Quoted 7 lines
Quoted 7 lines
arch/riscv/include/asm/hwcap.h +0
-0@@ -256,6 +257,17 @@ void __init riscv_fill_hwcap(void) elf_hwcap &= ~COMPAT_HWCAP_ISA_F ; }Quoted 9 lines
arch/riscv/include/asm/hwcap.h void __init riscv_fill_hwcap(void) · line 268
Quoted 9 lines
arch/riscv/include/asm/hwcap.h +9
-0+if (elf_hwcap &COMPAT_HWCAP_ISA_V ) { +#ifndef CONFIG_RISCV_ISA_V +/* +* ISA string in device tree might have 'v' flag, but +* CONFIG_RISCV_ISA_V is disabled in kernel. +* Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled. +*/ + elf_hwcap &= ~COMPAT_HWCAP_ISA_V ; +#endif if (elf_hwcap & COMPAT_HWCAP_ISA_V && !IS_ENABLED(CONFIG_RISCV_ISA_V)) {right?
Quoted 8 lines
I know that a later patch in this series calls
rvv_enable()here, which I'll comment on there, but I'd rather seeIS_ENABLEDas opposed to ifdefs in C files where possible.Thanks,
Conor.
PATCH v13 3/19 riscv: Add new csr defines related to vector extension
In reply to Andy ChiuFrom: Greentime Hu <[email protected]>
Follow the riscv vector spec to add new csr numbers.
--- arch/riscv/include/asm/csr.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
arch/riscv/include/asm/csr.h +16 -2
@@ -24,16 +24,24 @@#define SR_FS_CLEAN _AC(0x00004000, UL)#define SR_FS_DIRTY _AC(0x00006000, UL) +#define SR_VS _AC(0x00000600, UL)/* Vector Status */ +#define SR_VS_OFF _AC(0x00000000, UL) +#define SR_VS_INITIAL _AC(0x00000200, UL) +#define SR_VS_CLEAN _AC(0x00000400, UL) +#define SR_VS_DIRTY _AC(0x00000600, UL) +#define SR_XS _AC(0x00018000, UL)/* Extension Status */ #define SR_XS_OFF _AC(0x00000000, UL)#define SR_XS_INITIAL _AC(0x00008000, UL)#define SR_XS_CLEAN _AC(0x00010000, UL)#define SR_XS_DIRTY _AC(0x00018000, UL) +#define SR_FS_VS (SR_FS | SR_VS)/* Vector and Floating-Point Unit */ +#ifndef CONFIG_64BIT -#define SR_SD _AC(0x80000000, UL)/* FS/XS dirty */ +#define SR_SD _AC(0x80000000, UL)/* FS/VS/XS dirty */ #else -#define SR_SD _AC(0x8000000000000000, UL)/* FS/XS dirty */ +#define SR_SD _AC(0x8000000000000000, UL)/* FS/VS/XS dirty */ #endif #ifdef CONFIG_64BIT
@@ -297,6 +305,12 @@#define CSR_MIMPID 0xf13#define CSR_MHARTID 0xf14 +#define CSR_VSTART 0x8 +#define CSR_VCSR 0xf +#define CSR_VL 0xc20 +#define CSR_VTYPE 0xc21 +#define CSR_VLENB 0xc22 +#ifdef CONFIG_RISCV_M_MODE # define CSR_STATUS CSR_MSTATUS# define CSR_IE CSR_MIE
Signature · 1 line
2.17.1
Re: [PATCH -next v13 03/19] riscv: Add new csr defines related to vector extension
Quoted 4 lines · Andy Chiu
From: Greentime Hu <[email protected]>
Follow the riscv vector spec to add new csr numbers.
Quoted 9 lines
[guoren at linux.alibaba.com: first porting for new vector related csr]Acked-by Guo Ren <[email protected]>Co-developed-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Co-developed-by Vincent Chen <[email protected]>Signed-off-by Vincent Chen <[email protected]>Signed-off-by Greentime Hu <[email protected]>Reviewed-by Palmer Dabbelt <[email protected]>Suggested-by Vineet Gupta <[email protected]>
This series has gone on for so long, that the tags barely seem to make sense any more. I won't pretend that I understand why quite so many people are required to add a few definitions of the spec! Nor can I be bothered reading 12 revisions on lore to find out why it was done this way...
They do seem to align with what is in the latest release, so:
I also noticed that you missed some tags from the v12 submission. Eg Heiko's here:
https://lore.kernel.org/linux-riscv/2096011.OBFZWjSADL at diego/
There's also one for patch 1:
https://lore.kernel.org/linux-riscv/5335635.Sb9uPGUboI at diego/
Thanks,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230125/a996bdc1/attachment-0001.sig>
PATCH v13 4/19 riscv: Clear vector regfile on bootup
In reply to Andy Chiuclear vector registers on boot if kernel supports V.
--- arch/riscv/kernel/head.S | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
arch/riscv/kernel/head.S +23 -0
@@ -431,6 +431,29 @@ ENTRY(reset_regs) csrw fcsr, 0 /* note that the caller must clear SR_FS */ #endif /* CONFIG_FPU */ + +#ifdef CONFIG_RISCV_ISA_V + csrr t0, CSR_MISA + li t1, COMPAT_HWCAP_ISA_V + and t0, t0, t1 + beqz t0, .Lreset_regs_done + + /* + * Clear vector registers and reset vcsr + * VLMAX has a defined value, VLEN is a constant, + * and this form of vsetvli is defined to set vl to VLMAX. + */ + li t1, SR_VS + csrs CSR_STATUS, t1 + csrs CSR_VCSR, x0 + vsetvli t1, x0, e8, m8, ta, ma + vmv.v.i v0, 0 + vmv.v.i v8, 0 + vmv.v.i v16, 0 + vmv.v.i v24, 0 + /* note that the caller must clear SR_VS */ +#endif /* CONFIG_RISCV_ISA_V */ + .Lreset_regs_done: ret END(reset_regs)
Signature · 1 line
2.17.1
Re: [PATCH -next v13 04/19] riscv: Clear vector regfile on bootup
Quoted 6 lines · Andy Chiu
clear vector registers on boot if kernel supports V.
Signed-off-by Greentime Hu <[email protected]>Signed-off-by Vineet Gupta <[email protected]>[vineetg: broke this out to a seperate patch]Signed-off-by Andy Chiu <[email protected]>
But this patch didn't carry over the long list of contributors from it's source? Seems a bit odd, that's all.
There was also an Rb from Palmer that got dropped too. Was that intentional?
https://lore.kernel.org/linux-riscv/20220921214439.1491510-6-stillson at rivosinc.com/
Thanks,
Conor.
Quoted 41 lines
--- arch/riscv/kernel/head.S | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)arch/riscv/kernel/head.S +23
-0@@ -431,6 +431,29 @@ ENTRY(reset_regs) csrw fcsr, 0 /* note that the caller must clear SR_FS */ #endif /* CONFIG_FPU */ + +#ifdef CONFIG_RISCV_ISA_V + csrr t0, CSR_MISA + li t1, COMPAT_HWCAP_ISA_V + and t0, t0, t1 + beqz t0, .Lreset_regs_done + + /* + * Clear vector registers and reset vcsr + * VLMAX has a defined value, VLEN is a constant, + * and this form of vsetvli is defined to set vl to VLMAX. + */ + li t1, SR_VS + csrs CSR_STATUS, t1 + csrs CSR_VCSR, x0 + vsetvli t1, x0, e8, m8, ta, ma + vmv.v.i v0, 0 + vmv.v.i v8, 0 + vmv.v.i v16, 0 + vmv.v.i v24, 0 + /* note that the caller must clear SR_VS */ +#endif /* CONFIG_RISCV_ISA_V */ + .Lreset_regs_done: ret END(reset_regs)Signature · 1 line
2.17.1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230125/3b5159b7/attachment-0001.sig>
Re: [PATCH -next v13 04/19] riscv: Clear vector regfile on bootup
Quoted 7 lines · Conor Dooley
Quoted 6 lines · Andy Chiu
clear vector registers on boot if kernel supports V.
Signed-off-by Greentime Hu<[email protected]>Signed-off-by Vineet Gupta<[email protected]>[vineetg: broke this out to a seperate patch]Signed-off-by Andy Chiu<[email protected]>
Quoted 5 lines
But this patch didn't carry over the long list of contributors from it's source? Seems a bit odd, that's all.
There was also an Rb from Palmer that got dropped too. Was that intentional?
https://lore.kernel.org/linux-riscv/20220921214439.1491510-6-stillson at rivosinc.com/
In v12 this and 5/19 were in one patch, which I broke off into two for clarity. Hence the Rb technically doesn't apply.
-Vineet
Re: [PATCH -next v13 04/19] riscv: Clear vector regfile on bootup
Quoted 9 lines · Vineet Gupta
Quoted 7 lines · Conor Dooley
Quoted 6 lines · Andy Chiu
clear vector registers on boot if kernel supports V.
Signed-off-by Greentime Hu<[email protected]>Signed-off-by Vineet Gupta<[email protected]>[vineetg: broke this out to a seperate patch]Signed-off-by Andy Chiu<[email protected]>
Quoted 8 lines
Quoted 5 lines
But this patch didn't carry over the long list of contributors from it's source? Seems a bit odd, that's all.
There was also an Rb from Palmer that got dropped too. Was that intentional?
https://lore.kernel.org/linux-riscv/20220921214439.1491510-6-stillson at rivosinc.com/In v12 this and 5/19 were in one patch, which I broke off into two for clarity. Hence the Rb technically doesn't apply.
Technically correct, the best kind, huh?
Anyways, I just noticed that this patch had a comment from Heiko that wasn't addressed to maybe the omission is for the better!
https://lore.kernel.org/linux-riscv/2331455.NG923GbCHz at diego/
Thanks,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230125/bb9750c5/attachment-0001.sig>
PATCH v13 5/19 riscv: Disable Vector Instructions for kernel itself
In reply to Andy ChiuDisable vector instructions execution for kernel mode at its entrances.
--- arch/riscv/kernel/entry.S | 6 +++--- arch/riscv/kernel/head.S | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-)
arch/riscv/kernel/entry.S +3 -3
@@ -77,10 +77,10 @@ _save_context: * Disable user-mode memory access as it should only be set in the * actual user copy routines. * - * Disable the FPU to detect illegal usage of floating point in kernel - * space. + * Disable the FPU/Vector to detect illegal usage of floating point + * or vector in kernel space. */ - li t0, SR_SUM | SR_FS + li t0, SR_SUM | SR_FS_VS REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0
arch/riscv/kernel/head.S +6 -6
@@ -140,10 +140,10 @@ secondary_start_sbi: .option pop /* - * Disable FPU to detect illegal usage of - * floating point in kernel space + * Disable FPU & VECTOR to detect illegal usage of + * floating point or vector in kernel space */ - li t0, SR_FS + li t0, SR_FS_VS csrc CSR_STATUS, t0 /* Set trap vector to spin forever to help debug */
@@ -234,10 +234,10 @@ pmp_done: .option pop /* - * Disable FPU to detect illegal usage of - * floating point in kernel space + * Disable FPU & VECTOR to detect illegal usage of + * floating point or vector in kernel space */ - li t0, SR_FS + li t0, SR_FS_VS csrc CSR_STATUS, t0 #ifdef CONFIG_RISCV_BOOT_SPINWAIT
Signature · 1 line
2.17.1
Re: [PATCH -next v13 05/19] riscv: Disable Vector Instructions for kernel itself
Quoted 12 lines · Andy Chiu
Disable vector instructions execution for kernel mode at its entrances.
Signed-off-by Guo Ren <[email protected]>Co-developed-by Vincent Chen <[email protected]>Signed-off-by Vincent Chen <[email protected]>Co-developed-by Han-Kuan Chen <[email protected]>Signed-off-by Han-Kuan Chen <[email protected]>Co-developed-by Greentime Hu <[email protected]>Signed-off-by Greentime Hu <[email protected]>Signed-off-by Vineet Gupta <[email protected]>[vineetg: split off vecreg file clearing]Signed-off-by Andy Chiu <[email protected]>
That's a hilarious co-developed-by list for adding "| foo". I can only assume that this is mostly related to the asm that was removed by Vineet?
Either way:
Quoted 58 lines
--- arch/riscv/kernel/entry.S | 6 +++--- arch/riscv/kernel/head.S | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-)arch/riscv/kernel/entry.S +3
-3@@ -77,10 +77,10 @@ _save_context: * Disable user-mode memory access as it should only be set in the * actual user copy routines. * - * Disable the FPU to detect illegal usage of floating point in kernel - * space. + * Disable the FPU/Vector to detect illegal usage of floating point + * or vector in kernel space. */ - li t0, SR_SUM | SR_FS + li t0, SR_SUM | SR_FS_VS REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0arch/riscv/kernel/head.S +6
-6@@ -140,10 +140,10 @@ secondary_start_sbi: .option pop /* - * Disable FPU to detect illegal usage of - * floating point in kernel space + * Disable FPU & VECTOR to detect illegal usage of + * floating point or vector in kernel space */ - li t0, SR_FS + li t0, SR_FS_VS csrc CSR_STATUS, t0 /* Set trap vector to spin forever to help debug */@@ -234,10 +234,10 @@ pmp_done: .option pop /* - * Disable FPU to detect illegal usage of - * floating point in kernel space + * Disable FPU & VECTOR to detect illegal usage of + * floating point or vector in kernel space */ - li t0, SR_FS + li t0, SR_FS_VS csrc CSR_STATUS, t0 #ifdef CONFIG_RISCV_BOOT_SPINWAITSignature · 1 line
2.17.1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230125/00af8020/attachment-0001.sig>
PATCH v13 6/19 riscv: Introduce Vector enable/disable helpers
In reply to Andy ChiuThese are small and likely to be frequently called so implement as inline routines (vs. function call).
--- arch/riscv/include/asm/vector.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
arch/riscv/include/asm/vector.h +11 -0
@@ -11,12 +11,23 @@#ifdef CONFIG_RISCV_ISA_V #include <asm/hwcap.h> +#include <asm/csr.h> static __always_inline bool has_vector(void) {return static_branch_likely (&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_VECTOR ]); } +static __always_inline void rvv_enable(void) +{ +csr_set (CSR_SSTATUS ,SR_VS ); +} + +static __always_inline void rvv_disable(void) +{ +csr_clear (CSR_SSTATUS ,SR_VS ); +} +#else /* ! CONFIG_RISCV_ISA_V */ static __always_inline bool has_vector(void) { return false; }
Signature · 1 line
2.17.1
Re: [PATCH -next v13 06/19] riscv: Introduce Vector enable/disable helpers
Hey Andy,
Quoted 12 lines · Andy Chiu
These are small and likely to be frequently called so implement as inline routines (vs. function call).
Co-developed-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Co-developed-by Vincent Chen <[email protected]>Signed-off-by Vincent Chen <[email protected]>Signed-off-by Greentime Hu <[email protected]>Signed-off-by Vineet Gupta <[email protected]>[vineetg: create new patch from meshup, introduced asm variant]Signed-off-by Andy Chiu <[email protected]>[andy.chiu: remove calls from asm thus remove asm vaiant]
Again, these chains are odd but a reflection of the series' history I guess.
Quoted 9 lines
--- arch/riscv/include/asm/vector.h | 11 +++++++++++ 1 file changed, 11 insertions(+)arch/riscv/include/asm/vector.h +0
-0
arch/riscv/include/asm/vector.h line 18
Quoted 8 lines
arch/riscv/include/asm/vector.h +1
-0@@ -11,12 +11,23 @@#ifdef CONFIG_RISCV_ISA_V #include <asm/hwcap.h> +#include <asm/csr.h> static __always_inline bool has_vector(void) {return static_branch_likely (&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_VECTOR ]);
This likely will need to drop the static branch due to Jisheng's series. See here for what the equivalent change to has_fpu() was:
https://lore.kernel.org/all/20230115154953.831-1-jszhang at kernel.org/ Hopefully that series has been queued by the time you are resubmitting this one.
arch/riscv/include/asm/vector.h line 21
Quoted 3 lines
arch/riscv/include/asm/vector.h +1
-0} +static __always_inline void rvv_enable(void)
I'm not keen on these function names. IMO, they should be riscv_v_{en,dis}able() to match other riscv specific functions, like those of zicbom or sbi stuff.
Other parts of this series use riscv_v_foo & riscv_vfoo. Some of the kvm bits spell out vector rather than v. Consistent naming of functions etc would be appreciated.
Thanks,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230126/53a3caaf/attachment-0001.sig>
PATCH v13 7/19 riscv: Introduce riscv_vsize to record size of Vector context
In reply to Andy ChiuFrom: Greentime Hu <[email protected]>
This patch is used to detect the size of CPU vector registers and use riscv_vsize to save the size of all the vector registers. It assumes all harts has the same capabilities in a SMP system.
--- arch/riscv/include/asm/vector.h | 3 +++ arch/riscv/kernel/cpufeature.c | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-)
arch/riscv/include/asm/vector.h +3 -0
@@ -13,6 +13,8 @@#include <asm/hwcap.h> #include <asm/csr.h> +extern unsigned long riscv_vsize; + static __always_inline bool has_vector(void) {return static_branch_likely (&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_VECTOR ]);
@@ -31,6 +33,7 @@ static __always_inline void rvv_disable(void)#else /* ! CONFIG_RISCV_ISA_V */ static __always_inline bool has_vector(void) { return false; } +#define riscv_vsize (0)#endif /* CONFIG_RISCV_ISA_V */
arch/riscv/kernel/cpufeature.c +11 -1
@@ -21,6 +21,7 @@#include <asm/processor.h> #include <asm/smp.h> #include <asm/switch_to.h> +#include <asm/vector.h> #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
@@ -31,6 +32,10 @@ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;DEFINE_STATIC_KEY_ARRAY_FALSE (riscv_isa_ext_keys,RISCV_ISA_EXT_KEY_MAX );EXPORT_SYMBOL (riscv_isa_ext_keys); +#ifdef CONFIG_RISCV_ISA_V +unsigned long riscv_vsize__read_mostly ; +EXPORT_SYMBOL_GPL (riscv_vsize); +#endif /** * riscv_isa_extension_base() - Get base extension word
@@ -258,7 +263,12 @@ void __init riscv_fill_hwcap(void) }if (elf_hwcap &COMPAT_HWCAP_ISA_V ) { -#ifndef CONFIG_RISCV_ISA_V +#ifdef CONFIG_RISCV_ISA_V +/* There are 32 vector registers with vlenb length. */ +rvv_enable (); + riscv_vsize =csr_read (CSR_VLENB ) *32 ; +rvv_disable (); +#else /* * ISA string in device tree might have 'v' flag, but * CONFIG_RISCV_ISA_V is disabled in kernel.
Signature · 1 line
2.17.1
Re: [PATCH -next v13 07/19] riscv: Introduce riscv_vsize to record size of Vector context
Hey again Andy!
Quoted 23 lines · Andy Chiu
From: Greentime Hu <[email protected]>
This patch is used to detect the size of CPU vector registers and use
riscv_vsizeto save the size of all the vector registers. It assumes all harts has the same capabilities in a SMP system.[guoren at linux.alibaba.com: add has_vector checking]Co-developed-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Co-developed-by Vincent Chen <[email protected]>Signed-off-by Vincent Chen <[email protected]>Signed-off-by Greentime Hu <[email protected]>Signed-off-by Andy Chiu <[email protected]>--- arch/riscv/include/asm/vector.h | 3 +++ arch/riscv/kernel/cpufeature.c | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-)arch/riscv/include/asm/vector.h +0
-0
arch/riscv/include/asm/vector.h line 16
Quoted 4 lines
arch/riscv/include/asm/vector.h +1
-0@@ -13,6 +13,8 @@#include <asm/hwcap.h> #include <asm/csr.h> +extern unsigned long riscv_vsize;
Hmm, naming this with a riscv_v_ prefix too would be good I think.
Quoted 25 lines
arch/riscv/include/asm/vector.h +2
-0+ static __always_inline bool has_vector(void) {return static_branch_likely (&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_VECTOR ]);@@ -31,6 +33,7 @@ static __always_inline void rvv_disable(void)#else /* ! CONFIG_RISCV_ISA_V */ static __always_inline bool has_vector(void) { return false; } +#define riscv_vsize (0)#endif /* CONFIG_RISCV_ISA_V */ arch/riscv/kernel/cpufeature.c +1
-0@@ -21,6 +21,7 @@#include <asm/processor.h> #include <asm/smp.h> #include <asm/switch_to.h> +#include <asm/vector.h> #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
arch/riscv/kernel/cpufeature.c static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; · line 38
Quoted 7 lines
arch/riscv/kernel/cpufeature.c +4
-0@@ -31,6 +32,10 @@ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;DEFINE_STATIC_KEY_ARRAY_FALSE (riscv_isa_ext_keys,RISCV_ISA_EXT_KEY_MAX );EXPORT_SYMBOL (riscv_isa_ext_keys); +#ifdef CONFIG_RISCV_ISA_V +unsigned long riscv_vsize__read_mostly ; +EXPORT_SYMBOL_GPL (riscv_vsize); +#endif
I really don't think that this should be in here at all. IMO, this should be moved out to vector.c or something along those lines and...
Quoted 4 lines
arch/riscv/kernel/cpufeature.c +0
-0/** * riscv_isa_extension_base() - Get base extension word
arch/riscv/kernel/cpufeature.c void __init riscv_fill_hwcap(void) · line 271
Quoted 10 lines
arch/riscv/kernel/cpufeature.c +6
-1@@ -258,7 +263,12 @@ void __init riscv_fill_hwcap(void) }if (elf_hwcap &COMPAT_HWCAP_ISA_V ) { -#ifndef CONFIG_RISCV_ISA_V +#ifdef CONFIG_RISCV_ISA_V +/* There are 32 vector registers with vlenb length. */ +rvv_enable (); + riscv_vsize =csr_read (CSR_VLENB ) *32 ; +rvv_disable (); +#else
...so should all of this code, especially the csr_read(). vector.c would then provide the actual implementation, and vector.h would provide an implementation with an empty function body.
The code here could the, following my previous suggestion of IS_ENABLED() look along the lines of:
if (elf_hwcap & COMPAT_HWCAP_ISA_V) {
if (IS_ENABLED(CONFIG_RISCV_ISA_V))
riscv_v_setup_vsize();
else
elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
}Having the csr_read() in particular looks rather out of place to me in this file. Better off keeping the vector stuff together & having unneeded ifdefs is my pet peeve ;)
Thanks,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230126/da828ac0/attachment-0001.sig>
PATCH v13 8/19 riscv: Introduce struct/helpers to save/restore per-task Vector state
In reply to Andy ChiuFrom: Greentime Hu <[email protected]>
Add vector state context struct to be added later in thread_struct. And prepare low-level helper functions to save/restore vector contexts.
This include Vector Regfile and CSRs holding dynamic configuration state (vstart, vl, vtype, vcsr). The Vec Register width could be implementation defined, but same for all processes, so that is saved separately.
This is not yet wired into final thread_struct - will be done when __switch_to actually starts doing this in later patches.
Given the variable (and potentially large) size of regfile, they are saved in dynamically allocated memory, pointed to by datap pointer in __riscv_v_state.
--- arch/riscv/include/asm/vector.h | 84 ++++++++++++++++++++++++++++ arch/riscv/include/uapi/asm/ptrace.h | 17 ++++++ 2 files changed, 101 insertions(+)
arch/riscv/include/asm/vector.h +84 -0
@@ -12,6 +12,9 @@#include <asm/hwcap.h> #include <asm/csr.h> +#include <asm/asm.h> + +#define CSR_STR (x) __ASM_STR(x)extern unsigned long riscv_vsize;
@@ -20,6 +23,26 @@ static __always_inline bool has_vector(void)return static_branch_likely (&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_VECTOR ]); } +static inline void __vstate_clean (struct pt_regs *regs) +{ + regs->status = (regs->status & ~(SR_VS )) |SR_VS_CLEAN ; +} + +static inline void vstate_off (struct pt_regs *regs) +{ + regs->status = (regs->status & ~SR_VS ) |SR_VS_OFF ; +} + +static inline void vstate_on (struct pt_regs *regs) +{ + regs->status = (regs->status & ~(SR_VS )) |SR_VS_INITIAL ; +} + +static inline bool vstate_query (struct pt_regs *regs) +{ +return (regs->status &SR_VS ) !=0 ; +} + static __always_inline void rvv_enable(void) {csr_set (CSR_SSTATUS ,SR_VS );
@@ -30,10 +53,71 @@ static __always_inline void rvv_disable(void)csr_clear (CSR_SSTATUS ,SR_VS ); } +static __always_inline void __vstate_csr_save(struct __riscv_v_state *dest) +{ + asmvolatile ( + "csrr %0, " CSR_STR(CSR_VSTART) "\n\t" + "csrr %1, " CSR_STR(CSR_VTYPE) "\n\t" + "csrr %2, " CSR_STR(CSR_VL) "\n\t" + "csrr %3, " CSR_STR(CSR_VCSR) "\n\t" + : "=r" (dest->vstart), "=r" (dest->vtype), "=r" (dest->vl), + "=r" (dest->vcsr) : :); +} + +static __always_inline void __vstate_csr_restore(struct __riscv_v_state *src) +{ + asmvolatile ( +"vsetvl x0, %2, %1\n\t" + "csrw " CSR_STR(CSR_VSTART) ", %0\n\t" + "csrw " CSR_STR(CSR_VCSR) ", %3\n\t" + : : "r" (src->vstart), "r" (src->vtype), "r" (src->vl), + "r" (src->vcsr) :); +} + +static inline void __vstate_save (struct __riscv_v_state *save_to,void *datap) +{ +rvv_enable (); +__vstate_csr_save (save_to); + asmvolatile ( +"vsetvli t4, x0, e8, m8, ta, ma\n\t" +"vse8.v v0, (%0)\n\t" +"add %0, %0, t4\n\t" +"vse8.v v8, (%0)\n\t" +"add %0, %0, t4\n\t" +"vse8.v v16, (%0)\n\t" +"add %0, %0, t4\n\t" +"vse8.v v24, (%0)\n\t" + : :"r" (datap) :"t4" ,"memory" ); +rvv_disable (); +} + +static inline void __vstate_restore (struct __riscv_v_state *restore_from, +void *datap) +{ +rvv_enable (); + asmvolatile ( +"vsetvli t4, x0, e8, m8, ta, ma\n\t" +"vle8.v v0, (%0)\n\t" +"add %0, %0, t4\n\t" +"vle8.v v8, (%0)\n\t" +"add %0, %0, t4\n\t" +"vle8.v v16, (%0)\n\t" +"add %0, %0, t4\n\t" +"vle8.v v24, (%0)\n\t" + : :"r" (datap) :"t4" ); +__vstate_csr_restore (restore_from); +rvv_disable (); +} +#else /* ! CONFIG_RISCV_ISA_V */ +struct pt_regs ; + static __always_inline bool has_vector(void) { return false; } +static inline bool vstate_query (struct pt_regs *regs) {return false; }#define riscv_vsize (0) +#define vstate_off (regs) do {} while (0) +#define vstate_on (regs) do {} while (0)#endif /* CONFIG_RISCV_ISA_V */
arch/riscv/include/uapi/asm/ptrace.h +17 -0
@@ -77,6 +77,23 @@ union __riscv_fp_state {struct __riscv_q_ext_state q; }; +struct __riscv_v_state { +unsigned long vstart; +unsigned long vl; +unsigned long vtype; +unsigned long vcsr; +void *datap; +/* +* In signal handler, datap will be set a correct user stack offset +* and vector registers will be copied to the address of datap +* pointer. +* +* In ptrace syscall, datap will be set to zero and the vector +* registers will be copied to the address right after this +* structure. +*/ +}; +#endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_RISCV_PTRACE_H */
Signature · 1 line
2.17.1
Re: [PATCH -next v13 08/19] riscv: Introduce struct/helpers to save/restore per-task Vector state
Quoted 43 lines · Andy Chiu
From: Greentime Hu <[email protected]>
Add vector state context struct to be added later in
thread_struct. And prepare low-level helper functions to save/restore vector contexts.This include Vector Regfile and CSRs holding dynamic configuration state (vstart, vl, vtype, vcsr). The Vec Register width could be implementation defined, but same for all processes, so that is saved separately.
This is not yet wired into final
thread_struct- will be done when__switch_toactually starts doing this in later patches.Given the variable (and potentially large) size of regfile, they are saved in dynamically allocated memory, pointed to by datap pointer in
__riscv_v_state.Co-developed-by Vincent Chen <[email protected]>Signed-off-by Vincent Chen <[email protected]>Signed-off-by Greentime Hu <[email protected]>Signed-off-by Vineet Gupta <[email protected]>[vineetg: merged bits from 2 different patches]Signed-off-by Andy Chiu <[email protected]>[andy.chiu: use inline asm to save/restore context, remove asm vaiant]--- arch/riscv/include/asm/vector.h | 84 ++++++++++++++++++++++++++++ arch/riscv/include/uapi/asm/ptrace.h | 17 ++++++ 2 files changed, 101 insertions(+)arch/riscv/include/asm/vector.h +3
-0@@ -12,6 +12,9 @@#include <asm/hwcap.h> #include <asm/csr.h> +#include <asm/asm.h> + +#define CSR_STR (x) __ASM_STR(x)extern unsigned long riscv_vsize;
arch/riscv/include/asm/vector.h static __always_inline bool has_vector(void) · line 26
Quoted 4 lines
arch/riscv/include/asm/vector.h +1
-0@@ -20,6 +23,26 @@ static __always_inline bool has_vector(void)return static_branch_likely (&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_VECTOR ]); } +static inline void __vstate_clean (struct pt_regs *regs)
Consistent prefixes here too please, riscv_v_vstate_clean() or similar and so on for the rest of the patch.
Thanks,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230126/fa94b982/attachment-0001.sig>
PATCH v13 9/19 riscv: Add task switch support for vector
In reply to Andy ChiuFrom: Greentime Hu <[email protected]>
This patch adds task switch support for vector. It also supports all lengths of vlen.
[guoren at linux.alibaba.com: First available porting to support vector context switching]
[nick.knight at sifive.com: Rewrite vector.S to support dynamic vlen, xlen and code refine]
[vincent.chen at sifive.com: Fix the might_sleep issue in vstate_save, vstate_restore]
[andrew at sifive.com: Optimize task switch codes of vector] [ruinland.tsai at sifive.com: Fix the arch_release_task_struct free wrong datap issue]
[vineetg: Fixed lkp warning with W=1 build]
[andy.chiu: Use inline asm for task switches]
--- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/switch_to.h | 18 ++++++++++++++++++ arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/vector.h | 26 ++++++++++++++++++++++++++ arch/riscv/kernel/process.c | 18 ++++++++++++++++++ arch/riscv/kernel/traps.c | 14 ++++++++++++-- 6 files changed, 78 insertions(+), 2 deletions(-)
arch/riscv/include/asm/processor.h +1 -0
@@ -39,6 +39,7 @@ struct thread_struct {unsigned long s[12 ];/* s[0]: frame pointer */ struct __riscv_d_ext_state fstate;unsigned long bad_cause; +struct __riscv_v_state vstate; };/* Whitelist the fstate from the task_struct for hardened usercopy */
arch/riscv/include/asm/switch_to.h +18 -0
@@ -8,6 +8,7 @@#include <linux/jump_label.h> #include <linux/sched/task_stack.h> +#include <asm/vector.h> #include <asm/hwcap.h> #include <asm/processor.h> #include <asm/ptrace.h>
@@ -68,6 +69,21 @@ static __always_inline bool has_fpu(void) { return false; }#define __switch_to_fpu (__prev, __next) do { } while (0)#endif +#ifdef CONFIG_RISCV_ISA_V +static inline void __switch_to_vector (struct task_struct *prev, +struct task_struct *next) +{ +struct pt_regs *regs; + + regs =task_pt_regs (prev); +if (unlikely (regs->status &SR_SD )) +vstate_save (prev, regs); +vstate_restore (next,task_pt_regs (next)); +} +#else /* ! CONFIG_RISCV_ISA_V */ +#define __switch_to_vector (__prev, __next) do { } while (0) +#endif /* CONFIG_RISCV_ISA_V */ +extern struct task_struct *__switch_to (struct task_struct *,struct task_struct *);
@@ -77,6 +93,8 @@ do { \struct task_struct *__next = (next); \if (has_fpu ()) \__switch_to_fpu (__prev, __next); \ +if (has_vector ()) \ +__switch_to_vector (__prev, __next); \ ((last) =__switch_to (__prev, __next)); \ }while (0 )
arch/riscv/include/asm/thread_info.h +3 -0
@@ -79,6 +79,9 @@ struct thread_info { .preempt_count = INIT_PREEMPT_COUNT, \ } +void arch_release_task_struct (struct task_struct *tsk); +int arch_dup_task_struct (struct task_struct *dst,struct task_struct *src); +#endif /* !__ASSEMBLY__ */ /*
arch/riscv/include/asm/vector.h +26 -0
@@ -10,6 +10,8 @@#ifdef CONFIG_RISCV_ISA_V +#include <linux/sched.h> +#include <asm/ptrace.h> #include <asm/hwcap.h> #include <asm/csr.h> #include <asm/asm.h>
@@ -109,6 +111,28 @@ static inline void __vstate_restore(struct __riscv_v_state *restore_from, rvv_disable(); } +static inline void vstate_save (struct task_struct *task, +struct pt_regs *regs) +{ +if ((regs->status &SR_VS ) ==SR_VS_DIRTY ) { +struct __riscv_v_state *vstate = &task->thread.vstate; + +__vstate_save (vstate, vstate->datap); +__vstate_clean (regs); + } +} + +static inline void vstate_restore (struct task_struct *task, +struct pt_regs *regs) +{ +if ((regs->status &SR_VS ) !=SR_VS_OFF ) { +struct __riscv_v_state *vstate = &task->thread.vstate; + +__vstate_restore (vstate, vstate->datap); +__vstate_clean (regs); + } +} +#else /* ! CONFIG_RISCV_ISA_V */ struct pt_regs ;
@@ -116,6 +140,8 @@ struct pt_regs; static __always_inline bool has_vector(void) { return false; }static inline bool vstate_query (struct pt_regs *regs) {return false; }#define riscv_vsize (0) +#define vstate_save (task, regs) do {} while (0) +#define vstate_restore (task, regs) do {} while (0)#define vstate_off (regs) do {} while (0)#define vstate_on (regs) do {} while (0)
arch/riscv/kernel/process.c +18 -0
@@ -24,6 +24,7 @@#include <asm/switch_to.h> #include <asm/thread_info.h> #include <asm/cpuidle.h> +#include <asm/vector.h> registerunsigned long gp_in_global __asm__("gp" );
@@ -148,12 +149,28 @@ void flush_thread(void)fstate_off (current,task_pt_regs (current));memset (¤t->thread.fstate,0 ,sizeof (current->thread.fstate));#endif +#ifdef CONFIG_RISCV_ISA_V +/* Reset vector state */ +vstate_off (task_pt_regs (current)); +kfree (current->thread.vstate.datap); +memset (¤t->thread.vstate,0 ,sizeof (struct __riscv_v_state )); +#endif +} + +void arch_release_task_struct (struct task_struct *tsk) +{ +/* Free the vector context of datap. */ +if (has_vector () && tsk->thread.vstate.datap) +kfree (tsk->thread.vstate.datap); }int arch_dup_task_struct (struct task_struct *dst,struct task_struct *src) {fstate_save (src,task_pt_regs (src)); *dst = *src; +/* clear entire V context, including datap for a new task */ +memset (&dst->thread.vstate,0 ,sizeof (struct __riscv_v_state )); +return 0 ; }
@@ -186,6 +203,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) childregs->a0 =0 ;/* Return value of fork() */ p->thread.ra = (unsigned long )ret_from_fork; } +vstate_off (childregs); p->thread.sp = (unsigned long )childregs;/* kernel sp */ return 0 ; }
arch/riscv/kernel/traps.c +12 -2
@@ -24,6 +24,7 @@#include <asm/processor.h> #include <asm/ptrace.h> #include <asm/thread_info.h> +#include <asm/vector.h> int show_unhandled_signals =1 ;
@@ -111,8 +112,17 @@ DO_ERROR_INFO(do_trap_insn_misaligned, SIGBUS, BUS_ADRALN, "instruction address misaligned");DO_ERROR_INFO (do_trap_insn_fault,SIGSEGV ,SEGV_ACCERR ,"instruction access fault" ); -DO_ERROR_INFO (do_trap_insn_illegal, -SIGILL ,ILL_ILLOPC ,"illegal instruction" ); + +asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs) +{ +if (has_vector () &&user_mode (regs)) { +if (rvv_first_use_handler (regs)) +return ; + } +do_trap_error (regs,SIGILL ,ILL_ILLOPC , regs->epc, +"Oops - illegal instruction" ); +} +DO_ERROR_INFO (do_trap_load_fault,SIGSEGV ,SEGV_ACCERR ,"load access fault" );#ifndef CONFIG_RISCV_M_MODE
Signature · 1 line
2.17.1
Re: [PATCH -next v13 09/19] riscv: Add task switch support for vector
Quoted 17 lines · Andy Chiu
From: Greentime Hu <[email protected]>
This patch adds task switch support for vector. It also supports all lengths of vlen.
[guoren at linux.alibaba.com: First available porting to support vector context switching]
[nick.knight at sifive.com: Rewritevector.Sto support dynamic vlen, xlen and code refine]
[vincent.chen at sifive.com: Fix themight_sleepissue invstate_save,vstate_restore]
[andrew at sifive.com: Optimize task switch codes of vector] [ruinland.tsai at sifive.com: Fix thearch_release_task_structfree wrong datap issue]
[vineetg: Fixed lkp warning with W=1 build]
[andy.chiu: Use inline asm for task switches]
Quoted 12 lines
Suggested-by Andrew Waterman <[email protected]>Co-developed-by Nick Knight <[email protected]>Signed-off-by Nick Knight <[email protected]>Co-developed-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>Co-developed-by Vincent Chen <[email protected]>Signed-off-by Vincent Chen <[email protected]>Co-developed-by Ruinland Tsai <[email protected]>Signed-off-by Ruinland Tsai <[email protected]>Signed-off-by Greentime Hu <[email protected]>Signed-off-by Vineet Gupta <[email protected]>Signed-off-by Andy Chiu <[email protected]>
More comments about what people did than patch description, lol!
Anyways, this patch breaks the build for every config we have, so please
fix that when you are re-submitting:
https://patchwork.kernel.org/project/linux-riscv/patch/20230125142056.18356-10-andy.chiu at sifive.com/
Any of allmodconfig, rv32_defconfig, nommu_{k210,virt}_defconfig should reproduce with gcc 12.2 - but I have no idea if it's the same same failures for all 4.
Quoted 14 lines
--- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/switch_to.h | 18 ++++++++++++++++++ arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/vector.h | 26 ++++++++++++++++++++++++++ arch/riscv/kernel/process.c | 18 ++++++++++++++++++ arch/riscv/kernel/traps.c | 14 ++++++++++++-- 6 files changed, 78 insertions(+), 2 deletions(-)arch/riscv/include/asm/processor.h +0
-0
arch/riscv/include/asm/processor.h struct thread_struct { · line 42
Quoted 4 lines
arch/riscv/include/asm/processor.h +1
-0@@ -39,6 +39,7 @@ struct thread_struct {unsigned long s[12 ];/* s[0]: frame pointer */ struct __riscv_d_ext_state fstate;unsigned long bad_cause; +struct __riscv_v_state vstate;
__riscv_d_ext_state__riscv_v_state
:thinking: These should ideally match, probably no harm in adding the _ext to the v one, no?
Quoted 13 lines
arch/riscv/kernel/traps.c +1
-0@@ -24,6 +24,7 @@#include <asm/processor.h> #include <asm/ptrace.h> #include <asm/thread_info.h> +#include <asm/vector.h> int show_unhandled_signals =1 ;
arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 119
Quoted 10 lines
arch/riscv/kernel/traps.c +5
-2@@ -111,8 +112,17 @@ DO_ERROR_INFO(do_trap_insn_misaligned, SIGBUS, BUS_ADRALN, "instruction address misaligned");DO_ERROR_INFO (do_trap_insn_fault,SIGSEGV ,SEGV_ACCERR ,"instruction access fault" ); -DO_ERROR_INFO (do_trap_insn_illegal, -SIGILL ,ILL_ILLOPC ,"illegal instruction" ); + +asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs) +{ +if (has_vector () &&user_mode (regs)) { +if (rvv_first_use_handler (regs))
And there's your build error, as this function is only added in the next patch.
Thanks,
Conor.
arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 124
Quoted 5 lines
arch/riscv/kernel/traps.c +5
-0+return ; + } +do_trap_error (regs,SIGILL ,ILL_ILLOPC , regs->epc, +"Oops - illegal instruction" ); +}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230126/36576346/attachment-0001.sig>
Re: [PATCH -next v13 09/19] riscv: Add task switch support for vector
In reply to Andy ChiuHi Andy,
For some reason I was looking closely at this patch today.
Quoted 15 lines · Andy Chiu
/* Whitelist the fstate from the
task_structfor hardened usercopy */arch/riscv/include/asm/switch_to.h +1
-0@@ -8,6 +8,7 @@#include <linux/jump_label.h> #include <linux/sched/task_stack.h> +#include <asm/vector.h> #include <asm/hwcap.h> #include <asm/processor.h> #include <asm/ptrace.h> @@ -68,6 +69,21 @@ static __always_inline bool has_fpu(void) { return false; }#define __switch_to_fpu (__prev, __next) do { } while (0)
arch/riscv/include/asm/switch_to.h static __always_inline bool has_fpu(void) { return false; } · line 79
Quoted 10 lines
arch/riscv/include/asm/switch_to.h +8
-0#endif +#ifdef CONFIG_RISCV_ISA_V +static inline void __switch_to_vector (struct task_struct *prev, +struct task_struct *next) +{ +struct pt_regs *regs; + + regs =task_pt_regs (prev); +if (unlikely (regs->status &SR_SD ))
Do we really need to check SR_SD, isn't checking for? SR_VS_DIRTY enough. If yes, we can remove the check here and keep the existing one on vstate_save()
arch/riscv/include/asm/switch_to.h static __always_inline bool has_fpu(void) { return false; } · line 86
Quoted 7 lines
arch/riscv/include/asm/switch_to.h +7
-0+vstate_save (prev, regs); + vstate_restore(next, task_pt_regs(next)); +} +#else /* ! CONFIG_RISCV_ISA_V */ +#define __switch_to_vector (__prev, __next) do { } while (0) +#endif /* CONFIG_RISCV_ISA_V */ +
Can we de-lutter switch_to.h some more and move both the definitions of __switch_to_vector into vector.h ?
Quoted 12 lines
arch/riscv/include/asm/switch_to.h +2
-0extern struct task_struct *__switch_to (struct task_struct *,struct task_struct *);@@ -77,6 +93,8 @@ do { \struct task_struct *__next = (next); \if (has_fpu ()) \__switch_to_fpu (__prev, __next); \ +if (has_vector ()) \ +__switch_to_vector (__prev, __next); \ ((last) =__switch_to (__prev, __next)); \ }while (0 )
PATCH v13 10/19 riscv: Allocate user's vector context in the first-use trap
In reply to Andy ChiuVector unit is disabled by default for all user processes. Thus, a process will take a trap (illegal instruction) into kernel at the first time when it uses Vector. Only after then, the kernel allocates V context and starts take care of the context for that user process.
--- arch/riscv/include/asm/insn.h | 24 +++++++++ arch/riscv/include/asm/vector.h | 2 + arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/vector.c | 89 +++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 arch/riscv/kernel/vector.c
arch/riscv/include/asm/insn.h +24 -0
@@ -133,6 +133,24 @@#define RVG_OPCODE_JALR 0x67#define RVG_OPCODE_JAL 0x6f#define RVG_OPCODE_SYSTEM 0x73 +#define RVG_SYSTEM_CSR_OFF 20 +#define RVG_SYSTEM_CSR_MASK GENMASK(12, 0) + +/* parts of opcode for RVV */ +#define OPCODE_VECTOR 0x57 +#define LSFP_WIDTH_RVV_8 0 +#define LSFP_WIDTH_RVV_16 5 +#define LSFP_WIDTH_RVV_32 6 +#define LSFP_WIDTH_RVV_64 7 + +/* parts of opcode for RVF, RVD and RVQ */ +#define LSFP_WIDTH_OFF 12 +#define LSFP_WIDTH_MASK GENMASK(3, 0) +#define LSFP_WIDTH_FP_W 2 +#define LSFP_WIDTH_FP_D 3 +#define LSFP_WIDTH_FP_Q 4 +#define OPCODE_LOADFP 0x07 +#define OPCODE_STOREFP 0x27/* parts of opcode for RVC*/ #define RVC_OPCODE_C0 0x0
@@ -291,6 +309,12 @@ static __always_inline bool riscv_insn_is_branch(u32 code) (RVC_X (x_,RVC_B_IMM_7_6_OPOFF ,RVC_B_IMM_7_6_MASK ) <<RVC_B_IMM_7_6_OFF ) | \ (RVC_IMM_SIGN(x_) << RVC_B_IMM_SIGN_OFF); }) +#define EXTRACT_LOAD_STORE_FP_WIDTH (x) \ + ({typeof(x) x_ = (x); RV_X(x_, LSFP_WIDTH_OFF, LSFP_WIDTH_MASK); }) + +#define EXTRACT_SYSTEM_CSR (x) \ + ({typeof(x) x_ = (x); RV_X(x_, RVG_SYSTEM_CSR_OFF, RVG_SYSTEM_CSR_MASK); }) +/* * Get the immediate from a J-type instruction. *
arch/riscv/include/asm/vector.h +2 -0
@@ -19,6 +19,7 @@#define CSR_STR (x) __ASM_STR(x)extern unsigned long riscv_vsize; +bool rvv_first_use_handler (struct pt_regs *regs); static __always_inline bool has_vector(void) {
@@ -138,6 +139,7 @@ static inline void vstate_restore(struct task_struct *task,struct pt_regs ; static __always_inline bool has_vector(void) { return false; } +static inline bool rvv_first_use_handler (struct pt_regs *regs) {return false; }static inline bool vstate_query (struct pt_regs *regs) {return false; }#define riscv_vsize (0)#define vstate_save (task, regs) do {} while (0)
arch/riscv/kernel/Makefile +1 -0
@@ -55,6 +55,7 @@ obj-$(CONFIG_MMU) += vdso.o vdso/ obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o +obj-$(CONFIG_RISCV_ISA_V) += vector.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += cpu_ops.o
arch/riscv/kernel/vector.c new file +89 -0
89 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Hey Andy!
Quoted 4 lines · Andy Chiu
Vector unit is disabled by default for all user processes. Thus, a process will take a trap (illegal instruction) into kernel at the first time when it uses Vector. Only after then, the kernel allocates V context and starts take care of the context for that user process.
I'm mostly ambivalent about the methods you lot discussed for turning v on when needed, so this WFM :)
Quoted 16 lines
Suggested-by Richard Henderson <[email protected]>Link https://lore.kernel.org/r/3923eeee-e4dc-0911-40bf-84c34aee962d at linaro.orgSigned-off-by Andy Chiu <[email protected]>--- arch/riscv/include/asm/insn.h | 24 +++++++++ arch/riscv/include/asm/vector.h | 2 + arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/vector.c | 89 +++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 arch/riscv/kernel/vector.carch/riscv/include/asm/insn.h +0
-0
arch/riscv/include/asm/insn.h line 137
Quoted 5 lines
arch/riscv/include/asm/insn.h +2
-0@@ -133,6 +133,24 @@#define RVG_OPCODE_JALR 0x67#define RVG_OPCODE_JAL 0x6f#define RVG_OPCODE_SYSTEM 0x73 +#define RVG_SYSTEM_CSR_OFF 20 +#define RVG_SYSTEM_CSR_MASK GENMASK(12, 0)
These ones look good.
arch/riscv/include/asm/insn.h line 144
Quoted 7 lines
arch/riscv/include/asm/insn.h +7
-0+ +/* parts of opcode for RVV */ +#define OPCODE_VECTOR 0x57 +#define LSFP_WIDTH_RVV_8 0 +#define LSFP_WIDTH_RVV_16 5 +#define LSFP_WIDTH_RVV_32 6 +#define LSFP_WIDTH_RVV_64 7
All of this needs a prefix though, not the almost-postfix you've added. IOW, move the RVV to the start.
arch/riscv/include/asm/insn.h line 148
Quoted 4 lines
arch/riscv/include/asm/insn.h +4
-0+ +/* parts of opcode for RVF, RVD and RVQ */ +#define LSFP_WIDTH_OFF 12 +#define LSFP_WIDTH_MASK GENMASK(3, 0)
These all get an RVG_ prefix, no? Or does the Q prevent that? Either way, they do need a prefix.
arch/riscv/include/asm/insn.h line 151
Quoted 3 lines
arch/riscv/include/asm/insn.h +3
-0+#define LSFP_WIDTH_FP_W 2 +#define LSFP_WIDTH_FP_D 3 +#define LSFP_WIDTH_FP_Q 4
LSFP isn't something that has hits in the spec, which is annoying for cross checking IMO. If it were me, I'd likely do something like RVG_FLW_FSW_WIDTH since then it is abundantly clear what this is the width of.
arch/riscv/include/asm/insn.h line 153
Quoted 2 lines
arch/riscv/include/asm/insn.h +2
-0+#define OPCODE_LOADFP 0x07 +#define OPCODE_STOREFP 0x27
Same comment about prefix here. I'd be tempted to make these names match the spec too, but it is clear enough to me what this are at the moment.
arch/riscv/include/asm/insn.h static __always_inline bool riscv_insn_is_branch(u32 code) · line 315
Quoted 2 lines
arch/riscv/include/asm/insn.h +2
-0+#define EXTRACT_LOAD_STORE_FP_WIDTH (x) \ +#define EXTRACT_SYSTEM_CSR(x) \
Prefixes again here please!
arch/riscv/include/asm/vector.h line 22
Quoted 13 lines
arch/riscv/include/asm/insn.h +1
-0+/* * Get the immediate from a J-type instruction. * arch/riscv/include/asm/vector.h +1
-0@@ -19,6 +19,7 @@#define CSR_STR (x) __ASM_STR(x)extern unsigned long riscv_vsize; +bool rvv_first_use_handler (struct pt_regs *regs);
Please rename to riscv_v_...
arch/riscv/include/asm/vector.h line 25
Quoted 3 lines
arch/riscv/include/asm/vector.h +3
-0+static bool insn_is_vector (u32 insn_buf) +{ +u32 opcode = insn_buf & __INSN_OPCODE_MASK;
Newline here please...
arch/riscv/include/asm/vector.h
Quoted 6 lines
arch/riscv/include/asm/vector.h +6
-0+/* +* All V-related instructions, including CSR operations are 4-Byte. So, +* do not handle if the instruction length is not 4-Byte. +*/ +if (unlikely (GET_INSN_LENGTH (insn_buf) !=4 )) +return false;
...and one here please too!
arch/riscv/include/asm/vector.h
Quoted 3 lines
arch/riscv/include/asm/vector.h +3
-0+if (opcode ==OPCODE_VECTOR ) { +return true; + }
if (opcode == OPCODE_LOADFP || opcode == OPCODE_STOREFP) {The above returns, so there's no need for the else
arch/riscv/include/asm/vector.h
Quoted 5 lines
arch/riscv/include/asm/vector.h +5
-0+u32 width =EXTRACT_LOAD_STORE_FP_WIDTH (insn_buf); + +if (width ==LSFP_WIDTH_RVV_8 || width ==LSFP_WIDTH_RVV_16 || + width ==LSFP_WIDTH_RVV_32 || width ==LSFP_WIDTH_RVV_64 ) +return true;
I suppose you could also add else return false, thereby dropping the else in the line below too, but that's a matter of preference :)
arch/riscv/include/asm/vector.h
Quoted 9 lines
arch/riscv/include/asm/vector.h +9
-0+ }else if (opcode ==RVG_OPCODE_SYSTEM ) { +u32 csr =EXTRACT_SYSTEM_CSR (insn_buf); + +if ((csr >=CSR_VSTART && csr <=CSR_VCSR ) || + (csr >=CSR_VL && csr <=CSR_VLENB )) +return true; + } +return false; +}
I would like Heiko to take a look at this function!
I know we have the RISCV_INSN_FUNCS stuff that got newly added, but that's for single, named instructions. I'm just curious if there may be a neater way to go about doing this. AFAICT, the widths are all in funct3 - but it is a shame that 0b100 is Q and 0 is vector, as the macro works for matches and we can't use the upper bit for that.
There's prob something you could do with XORing and XNORing bits, but at that point it'd not be adding any clarity at all & it'd not be a RISCV_INSN_FUNCS anymore!
The actual opcode checks probably could be extracted though, but would love to know what Heiko thinks, even if that is "leave it as is".
arch/riscv/include/asm/vector.h
Quoted 2 lines
arch/riscv/include/asm/vector.h +2
-0+ +int rvv_thread_zalloc (void )
riscv_v_... and so on down the file
Quoted 6 lines
arch/riscv/include/asm/vector.h +6
-0+{ +void *datap; + + datap =kzalloc (riscv_vsize,GFP_KERNEL ); +if (!datap) +return -ENOMEM ;
arch/riscv/include/asm/vector.h
Quoted 10 lines
arch/riscv/include/asm/vector.h +10
-0+ current->thread.vstate.datap = datap; +memset (¤t->thread.vstate,0 , offsetof(struct __riscv_v_state , + datap)); +return 0 ; +} + +bool rvv_first_use_handler (struct pt_regs *regs) +{ +__user u32 *epc = (u32 *)regs->epc; +u32 tval = (u32 )regs->badaddr;
I'm dumb, what's the t here? This variable holds an instruction, right? Why not call it insn so it conveys some meaning?
arch/riscv/include/asm/vector.h
Quoted 13 lines
arch/riscv/include/asm/vector.h +13
-0+/* If V has been enabled then it is not the first-use trap */ +if (vstate_query (regs)) +return false; +/* Get the instruction */ +if (!tval) { +if (__get_user (tval, epc)) +return false; + } +/* Filter out non-V instructions */ +if (!insn_is_vector (tval)) +return false; +/* Sanity check. datap should be null by the time of the first-use trap */ +WARN_ON (current->thread.vstate.datap);
Is a WARN_ON sufficient here? If on the first use trap, it's non-null should we return false and trigger the trap error too?
arch/riscv/include/asm/vector.h
Quoted 11 lines
arch/riscv/include/asm/vector.h +11
-0+/* +* Now we sure that this is a V instruction. And it executes in the +* context where VS has been off. So, try to allocate the user's V +* context and resume execution. +*/ +if (rvv_thread_zalloc ()) { +force_sig (SIGKILL ); +return true; + } +vstate_on (regs); +return true;
Otherwise this looks sane to me!
Thanks,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230126/4cb5a1fe/attachment-0001.sig>
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Quoted 10 lines · Conor Dooley
arch/riscv/include/asm/insn.h line 144
Quoted 7 lines
arch/riscv/include/asm/insn.h +7
-0+ +/* parts of opcode for RVV */ +#define OPCODE_VECTOR 0x57 +#define LSFP_WIDTH_RVV_8 0 +#define LSFP_WIDTH_RVV_16 5 +#define LSFP_WIDTH_RVV_32 6 +#define LSFP_WIDTH_RVV_64 7All of this needs a prefix though, not the almost-postfix you've added. IOW, move the RVV to the start.
Thanks for the note. Changing to RVV_VL_VS_WIDTH_*
Quoted 9 lines
arch/riscv/include/asm/insn.h line 148
Quoted 5 lines
arch/riscv/include/asm/insn.h +4
-0+ +/* parts of opcode for RVF, RVD and RVQ */ +#define LSFP_WIDTH_OFF 12 +#define LSFP_WIDTH_MASK GENMASK(3, 0)These all get an
RVG_prefix, no? Or does the Q prevent that? Either way, they do need a prefix.
Quoted 8 lines
arch/riscv/include/asm/insn.h line 151
Quoted 3 lines
arch/riscv/include/asm/insn.h +3
-0+#define LSFP_WIDTH_FP_W 2 +#define LSFP_WIDTH_FP_D 3 +#define LSFP_WIDTH_FP_Q 4LSFP isn't something that has hits in the spec, which is annoying for cross checking IMO. If it were me, I'd likely do something like
RVG_FLW_FSW_WIDTHsince then it is abundantly clear what this is the width of.
Ok, s/LSFP_WIDTH_/RVFDQ_FL_FS_WIDTH_/
Quoted 7 lines
arch/riscv/include/asm/insn.h line 153
Quoted 3 lines
arch/riscv/include/asm/insn.h +2
-0+#define OPCODE_LOADFP 0x07 +#define OPCODE_STOREFP 0x27Same comment about prefix here. I'd be tempted to make these names match the spec too, but it is clear enough to me what this are at the moment.
These will be changed to RVFDQ_OPCODE_{FL|FS} In the next revision.
Quoted 4 lines
arch/riscv/include/asm/insn.h static __always_inline bool riscv_insn_is_branch(u32 code) · line 315
Quoted 2 lines
arch/riscv/include/asm/insn.h +2
-0+#define EXTRACT_LOAD_STORE_FP_WIDTH (x) \ +#define EXTRACT_SYSTEM_CSR(x) \Prefixes again here please!
Adding RVG prefix and changing to RVFDQ_EXRACT_FL_FS_WIDTH
Quoted 16 lines
arch/riscv/kernel/vector.c line 42
Quoted 3 lines
arch/riscv/kernel/vector.c new file +3
-0+if (opcode ==OPCODE_VECTOR ) { +return true; + }if (opcode == OPCODE_LOADFP || opcode == OPCODE_STOREFP) {The above returns, so there's no need for the else
arch/riscv/kernel/vector.c line 35
Quoted 5 lines
arch/riscv/kernel/vector.c new file +5
-0+u32 width =EXTRACT_LOAD_STORE_FP_WIDTH (insn_buf); + +if (width ==LSFP_WIDTH_RVV_8 || width ==LSFP_WIDTH_RVV_16 || + width ==LSFP_WIDTH_RVV_32 || width ==LSFP_WIDTH_RVV_64 ) +return true;I suppose you could also add else return false, thereby dropping the else in the line below too, but that's a matter of preference :)
Quoted 9 lines
arch/riscv/kernel/vector.c line 44
Quoted 9 lines
arch/riscv/kernel/vector.c new file +9
-0+ }else if (opcode ==RVG_OPCODE_SYSTEM ) { +u32 csr =EXTRACT_SYSTEM_CSR (insn_buf); + +if ((csr >=CSR_VSTART && csr <=CSR_VCSR ) || + (csr >=CSR_VL && csr <=CSR_VLENB )) +return true; + } +return false; +}
Changing it to a switch statement for better structuring.
Quoted 11 lines
I would like Heiko to take a look at this function!
I know we have theRISCV_INSN_FUNCSstuff that got newly added, but that's for single, named instructions. I'm just curious if there may be a neater way to go about doing this. AFAICT, the widths are all in funct3 - but it is a shame that 0b100 is Q and 0 is vector, as the macro works for matches and we can't use the upper bit for that.
There's prob something you could do with XORing and XNORing bits, but at that point it'd not be adding any clarity at all & it'd not be aRISCV_INSN_FUNCSanymore!
The actual opcode checks probably could be extracted though, but would love to know what Heiko thinks, even if that is "leave it as is".
I've checked the RISCV_INSN_FUNCS part recently. It seems good to match a single type of instruction, such as vector with OP-V opcode. However, I did not find an easy way of matching whole instructions introduced by RVV, which includes CSR operations on multiple CSRs and load/store with different widths. Yes, it would be great if we could distinguish VL and VS out by the upper bit of the width. Or even better if we could match CSR numbers for Vector this way. But I didn't find it.
Quoted 17 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ +int rvv_thread_zalloc(void)
riscv_v_... and so on down the fileQuoted 11 lines
arch/riscv/kernel/vector.c new file +11
-0+{ +void *datap; + + datap =kzalloc (riscv_vsize,GFP_KERNEL ); +if (!datap) +return -ENOMEM ; + current->thread.vstate.datap = datap; +memset (¤t->thread.vstate,0 , offsetof(struct __riscv_v_state , + datap)); +return 0 ; +}
Quoted 8 lines
arch/riscv/kernel/vector.c line 62
Quoted 5 lines
arch/riscv/kernel/vector.c new file +5
-0+ +bool rvv_first_use_handler (struct pt_regs *regs) +{ +__user u32 *epc = (u32 *)regs->epc; +u32 tval = (u32 )regs->badaddr;I'm dumb, what's the t here? This variable holds an instruction, right? Why not call it
insnso it conveys some meaning?
tval is the trap value register. I think it is the same as badaddr but you're right. insn has a better meaning here.
Quoted 15 lines
Quoted 4 lines
arch/riscv/kernel/vector.c new file +3
-0+/* If V has been enabled then it is not the first-use trap */ +if (vstate_query (regs)) +return false;arch/riscv/kernel/vector.c line 76
Quoted 10 lines
arch/riscv/kernel/vector.c new file +10
-0+/* Get the instruction */ +if (!tval) { +if (__get_user (tval, epc)) +return false; + } +/* Filter out non-V instructions */ +if (!insn_is_vector (tval)) +return false; +/* Sanity check. datap should be null by the time of the first-use trap */ +WARN_ON (current->thread.vstate.datap);
Quoted 2 lines
Is a
WARN_ONsufficient here? If on the first use trap, it's non-null should we return false and trigger the trap error too?
If we'd run into this warning message then there is a bug in kernel space. For example, if we did not properly free and clear the datap pointer. Or if we allocated datap somewhere else and did not set VS accordingly. Normally, current user space programs would not expect to run into this point, so I guess returning false here is not meaningful. This warning message is intended for kernel debugging only. Or, should we just strip out this check?
Quoted 13 lines
arch/riscv/kernel/vector.c line 87
Quoted 12 lines
arch/riscv/kernel/vector.c new file +11
-0+/* +* Now we sure that this is a V instruction. And it executes in the +* context where VS has been off. So, try to allocate the user's V +* context and resume execution. +*/ +if (rvv_thread_zalloc ()) { +force_sig (SIGKILL ); +return true; + } +vstate_on (regs); +return true;
Quoted 5 lines
Otherwise this looks sane to me!
Thanks,
Conor.
Thanks,
Andy.
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Quoted 14 lines · Andy Chiu
On Fri, Jan 27, 2023 at 7:11 AM Conor Dooley <[email protected]> wrote:
Changing it to a switch statement for better structuring.
Quoted 11 lines
I would like Heiko to take a look at this function!
I know we have theRISCV_INSN_FUNCSstuff that got newly added, but that's for single, named instructions. I'm just curious if there may be a neater way to go about doing this. AFAICT, the widths are all in funct3 - but it is a shame that 0b100 is Q and 0 is vector, as the macro works for matches and we can't use the upper bit for that.
There's prob something you could do with XORing and XNORing bits, but at that point it'd not be adding any clarity at all & it'd not be aRISCV_INSN_FUNCSanymore!
The actual opcode checks probably could be extracted though, but would love to know what Heiko thinks, even if that is "leave it as is".
Quoted 8 lines
I've checked the
RISCV_INSN_FUNCSpart recently. It seems good to match a single type of instruction, such as vector with OP-V opcode. However, I did not find an easy way of matching whole instructions introduced by RVV, which includes CSR operations on multiple CSRs and load/store with different widths. Yes, it would be great if we could distinguish VL and VS out by the upper bit of the width. Or even better if we could match CSR numbers for Vector this way. But I didn't find it.
Yup, I didn't see a straight forward way either.
I was hoping Heiko might have an idea!
Quoted 5 lines
Quoted 5 lines
arch/riscv/kernel/vector.c line 76
Quoted 2 lines
arch/riscv/kernel/vector.c new file +2
-0+/* Sanity check. datap should be null by the time of the first-use trap */ +WARN_ON (current->thread.vstate.datap);Is a
WARN_ONsufficient here? If on the first use trap, it's non-null should we return false and trigger the trap error too?
Quoted 7 lines
If we'd run into this warning message then there is a bug in kernel space. For example, if we did not properly free and clear the datap pointer. Or if we allocated datap somewhere else and did not set VS accordingly. Normally, current user space programs would not expect to run into this point, so I guess returning false here is not meaningful. This warning message is intended for kernel debugging only. Or, should we just strip out this check?
I suppose my question was "is it safe to warn and carry on, rather than disallow use of vector in this situation".
Thanks,
Conor.
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Quoted 1 line · Conor Dooley
I suppose my question was "is it safe to warn and carry on, rather than disallow use of vector in this situation".
Yes, I think it is safe to warn and carry on. This is a check for memory leak if future code did not allocate/free datap correctly.
Thanks,
Andy
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
In reply to Andy ChiuAndy,
(Keeping the huge Cc:-list for now...)
Quoted 81 lines · Andy Chiu
arch/riscv/kernel/vector.c new file +75
-0@@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2023 SiFive +* Author: Andy Chiu <[email protected]> +*/ +#include <linux/sched/signal.h> +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/sched.h> +#include <linux/uaccess.h> + +#include <asm/thread_info.h> +#include <asm/processor.h> +#include <asm/insn.h> +#include <asm/vector.h> +#include <asm/ptrace.h> +#include <asm/bug.h> + +static bool insn_is_vector (u32 insn_buf) +{ +u32 opcode = insn_buf & __INSN_OPCODE_MASK; +/* +* All V-related instructions, including CSR operations are 4-Byte. So, +* do not handle if the instruction length is not 4-Byte. +*/ +if (unlikely (GET_INSN_LENGTH (insn_buf) !=4 )) +return false; +if (opcode ==OPCODE_VECTOR ) { +return true; + }else if (opcode ==OPCODE_LOADFP || opcode ==OPCODE_STOREFP ) { +u32 width =EXTRACT_LOAD_STORE_FP_WIDTH (insn_buf); + +if (width ==LSFP_WIDTH_RVV_8 || width ==LSFP_WIDTH_RVV_16 || + width ==LSFP_WIDTH_RVV_32 || width ==LSFP_WIDTH_RVV_64 ) +return true; + }else if (opcode ==RVG_OPCODE_SYSTEM ) { +u32 csr =EXTRACT_SYSTEM_CSR (insn_buf); + +if ((csr >=CSR_VSTART && csr <=CSR_VCSR ) || + (csr >=CSR_VL && csr <=CSR_VLENB )) +return true; + } +return false; +} + +int rvv_thread_zalloc (void ) +{ +void *datap; + + datap =kzalloc (riscv_vsize,GFP_KERNEL ); +if (!datap) +return -ENOMEM ; + current->thread.vstate.datap = datap; +memset (¤t->thread.vstate,0 , offsetof(struct __riscv_v_state , + datap)); +return 0 ; +} + +bool rvv_first_use_handler (struct pt_regs *regs) +{ +__user u32 *epc = (u32 *)regs->epc; +u32 tval = (u32 )regs->badaddr; + +/* If V has been enabled then it is not the first-use trap */ +if (vstate_query (regs)) +return false; +/* Get the instruction */ +if (!tval) { +if (__get_user (tval, epc)) +return false; + } +/* Filter out non-V instructions */ +if (!insn_is_vector (tval)) +return false; +/* Sanity check. datap should be null by the time of the first-use trap */
arch/riscv/kernel/vector.c line 85
Quoted 10 lines
arch/riscv/kernel/vector.c new file +10
-0+WARN_ON (current->thread.vstate.datap); +/* +* Now we sure that this is a V instruction. And it executes in the +* context where VS has been off. So, try to allocate the user's V +* context and resume execution. +*/ +if (rvv_thread_zalloc ()) { +force_sig (SIGKILL ); +return true; + }
Should the altstack size be taken into consideration, like x86 does in validate_sigaltstack() (see __xstate_request_perm()).
Related; Would it make sense to implement sigaltstack_size_valid() for riscv, analogous to x86?
Bj?rn
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Quoted 21 lines · Bj?rn T?pel
Quoted 21 lines
arch/riscv/kernel/vector.c new file +21
-0+bool rvv_first_use_handler (struct pt_regs *regs) +{ +__user u32 *epc = (u32 *)regs->epc; +u32 tval = (u32 )regs->badaddr; + +/* If V has been enabled then it is not the first-use trap */ +if (vstate_query (regs)) +return false; +/* Get the instruction */ +if (!tval) { +if (__get_user (tval, epc)) +return false; + } +/* Filter out non-V instructions */ +if (!insn_is_vector (tval)) +return false; +/* Sanity check. datap should be null by the time of the first-use trap */ +WARN_ON (current->thread.vstate.datap); +/* +* Now we sure that this is a V instruction. And it executes in the +* context where VS has been off. So, try to allocate the user's V
Quoted 8 lines
arch/riscv/kernel/vector.c line 85
Quoted 6 lines
arch/riscv/kernel/vector.c new file +6
-0+* context and resume execution. +*/ +if (rvv_thread_zalloc ()) { +force_sig (SIGKILL ); +return true; + }Should the altstack size be taken into consideration, like x86 does in
validate_sigaltstack()(see__xstate_request_perm()).
For a preexisting alternate stack ? Otherwise there is no "configuration" like x86 to cross-check against and V fault implies large'ish signal stack.
See below as well.
Quoted 2 lines
Related; Would it make sense to implement
sigaltstack_size_valid()for riscv, analogous to x86?
Indeed we need to do that for the case where alt stack is being setup, *after* V fault-on-first use.
But how to handle an existing alt stack which might not be big enough to handle V state ?
-Vineet
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Quoted 31 lines · Vineet Gupta
Quoted 21 lines · Bj?rn T?pel
Quoted 21 lines
arch/riscv/kernel/vector.c new file +21
-0+bool rvv_first_use_handler (struct pt_regs *regs) +{ +__user u32 *epc = (u32 *)regs->epc; +u32 tval = (u32 )regs->badaddr; + +/* If V has been enabled then it is not the first-use trap */ +if (vstate_query (regs)) +return false; +/* Get the instruction */ +if (!tval) { +if (__get_user (tval, epc)) +return false; + } +/* Filter out non-V instructions */ +if (!insn_is_vector (tval)) +return false; +/* Sanity check. datap should be null by the time of the first-use trap */ +WARN_ON (current->thread.vstate.datap); +/* +* Now we sure that this is a V instruction. And it executes in the +* context where VS has been off. So, try to allocate the user's V Quoted 8 lines
arch/riscv/kernel/vector.c line 85
Quoted 6 lines
arch/riscv/kernel/vector.c new file +6
-0+* context and resume execution. +*/ +if (rvv_thread_zalloc ()) { +force_sig (SIGKILL ); +return true; + }Should the altstack size be taken into consideration, like x86 does in
validate_sigaltstack()(see__xstate_request_perm()).
Quoted 1 line
For a preexisting alternate stack ?
Yes.
Quoted 5 lines
Otherwise there is no
"configuration" like x86 to cross-check against and V fault implies large'ish signal stack.
See below as well.
Quoted 7 lines
Quoted 2 lines
Related; Would it make sense to implement
sigaltstack_size_valid()for riscv, analogous to x86?Indeed we need to do that for the case where alt stack is being setup, *after* V fault-on-first use.
But how to handle an existing alt stack which might not be big enough to handle V state ?
What I'm getting at is a stricter check at the time of fault (SIGILL/enable V) handling. If the *existing* altstack is not big enough, kill the process -- similar to the rvv_thread_zalloc() handling above.
So, two changes:
1. Disallow V-enablement if the existing altstack does not fit a V-sized frame.
2. Sanitize altstack changes when V is enabled.
Other than the altstack handling, I think the series is a good state! It would great if we could see a v14 land in -next...
Bj?rn
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Hey Bj?rn,
Quoted 4 lines · Bj?rn T?pel
So, two changes:
1. Disallow V-enablement if the existing altstack does not fit a V-sized frame.
This could potentially break old programs (non-V) that load new system libraries (with V), If the program sets a small alt stack and takes the fault in some libraries that use V. However, existing implementation will also kill the process when the signal arrives, finding insufficient stack frame in such cases. I'd choose the second one if we only have these two options, because there is a chance that the signal handler may not even run.
Quoted 1 line
2. Sanitize altstack changes when V is enabled.
Yes, I'd like to have this. But it may be tricky when it comes to deciding whether V is enabled, due to the first-use trap. If V is commonly used in system libraries then it is likely that V will be enabled before an user set an altstack. Sanitizing this case would be easy and straightforward. But what if the user sets an altstack before enabling V in the first-use trap? This could happen on a statically program that has hand-written V routines. This takes us to the 1st question above, should we fail the user program immediately if the altstack is set too small?
Quoted 3 lines
Other than the altstack handling, I think the series is a good state! It would great if we could see a v14 land in -next...
Thanks. I am reforming the v14 patch and hoping the same to happen soon too!
Cheers,
Andy
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Quoted 7 lines · Andy Chiu
Hey Bj?rn,
Quoted 4 lines · Bj?rn T?pel
So, two changes:
1. Disallow V-enablement if the existing altstack does not fit a V-sized frame.
Quoted 7 lines
This could potentially break old programs (non-V) that load new system libraries (with V), If the program sets a small alt stack and takes the fault in some libraries that use V. However, existing implementation will also kill the process when the signal arrives, finding insufficient stack frame in such cases. I'd choose the second one if we only have these two options, because there is a chance that the signal handler may not even run.
I think we might have different views here. A process has a pre-V, a and post-V state. Is allowing a process to enter V without the correct preconditions a good idea? Allow to run with V turned on, but not able to correctly handle a signal (the stack is too small)?
This was the same argument that the Intel folks had when enabling AMX. Sure, AMX requires *explicit* enablement, but same rules should apply, no?
Quoted 10 lines
Quoted 1 line
2. Sanitize altstack changes when V is enabled.
Yes, I'd like to have this. But it may be tricky when it comes to deciding whether V is enabled, due to the first-use trap. If V is commonly used in system libraries then it is likely that V will be enabled before an user set an altstack. Sanitizing this case would be easy and straightforward. But what if the user sets an altstack before enabling V in the first-use trap? This could happen on a statically program that has hand-written V routines. This takes us to the 1st question above, should we fail the user program immediately if the altstack is set too small?
For me it's obvious to fail (always) "if the altstack is too small to enable V", because it allows to execute V without proper preconditions.
Personally, I prefer a stricter model. Only enter V if you can, and after entering it disallow changing the altstack.
Then again, this is *my* opinion and concern. What do other people think? I don't want to stall the series.
Quoted 4 lines
Quoted 3 lines
Other than the altstack handling, I think the series is a good state! It would great if we could see a v14 land in -next...
Thanks. I am reforming the v14 patch and hoping the same to happen soon too!
Thank you for your hard work! It would be awesome to *finally* have vector support in the kernel!
Bj?rn
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Quoted 16 lines · Bj?rn T?pel
Quoted 7 lines · Andy Chiu
Hey Bj?rn,
Quoted 4 lines · Bj?rn T?pel
So, two changes:
1. Disallow V-enablement if the existing altstack does not fit a V-sized frame.
Quoted 7 lines
This could potentially break old programs (non-V) that load new system libraries (with V), If the program sets a small alt stack and takes the fault in some libraries that use V. However, existing implementation will also kill the process when the signal arrives, finding insufficient stack frame in such cases. I'd choose the second one if we only have these two options, because there is a chance that the signal handler may not even run.
Quoted 4 lines
I think we might have different views here. A process has a pre-V, a and post-V state. Is allowing a process to enter V without the correct preconditions a good idea? Allow to run with V turned on, but not able to correctly handle a signal (the stack is too small)?
The requirement is sane, but the issue is user experience: User trying to bring up some V code has no clue that deep in some startup code some alt stack had been setup and causing his process to be terminated on first V code.
Quoted 11 lines
This was the same argument that the Intel folks had when enabling AMX. Sure, AMX requires *explicit* enablement, but same rules should apply, no?
Quoted 6 lines
Quoted 1 line
2. Sanitize altstack changes when V is enabled.
Yes, I'd like to have this. But it may be tricky when it comes to deciding whether V is enabled, due to the first-use trap. If V is commonly used in system libraries then it is likely that V will be enabled before an user set an altstack. Sanitizing this case would be easy and straightforward.
Good. Lets have this in v14 as it seems reasonably easy to implement.
Quoted 5 lines
Quoted 5 lines
But what if the user sets an altstack before
enabling V in the first-use trap? This could happen on a statically program that has hand-written V routines. This takes us to the 1st question above, should we fail the user program immediately if the altstack is set too small?
Please lets not cross threads. We discussed this already at top. While ideally required, seems tricky so lets start with post-V alt stack check.
Quoted 8 lines
For me it's obvious to fail (always) "if the altstack is too small to enable V", because it allows to execute V without proper preconditions.
Personally, I prefer a stricter model. Only enter V if you can, and after entering it disallow changing the altstack.
Then again, this is *my* opinion and concern. What do other people think? I don't want to stall the series.
I concur that the alt stack checking requirements are sensible in the long run. We can add the obvious check for post-V case and see if there is a sane way to flag pre-V case to.
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
Other than the altstack handling, I think the series is a good state! It would great if we could see a v14 land in -next...
Thanks. I am reforming the v14 patch and hoping the same to happen soon too!
Thank you for your hard work! It would be awesome to *finally* have vector support in the kernel!
Indeed we've come a long way, lets push the gear so we can use the coming cycle to flesh out any changes for a possible 6.4 inclusion.
Thx,
-Vineet
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Quoted 56 lines · Vineet Gupta
Quoted 16 lines · Bj?rn T?pel
Quoted 7 lines · Andy Chiu
Hey Bj?rn,
Quoted 4 lines · Bj?rn T?pel
So, two changes:
1. Disallow V-enablement if the existing altstack does not fit a V-sized frame.
Quoted 7 lines
This could potentially break old programs (non-V) that load new system libraries (with V), If the program sets a small alt stack and takes the fault in some libraries that use V. However, existing implementation will also kill the process when the signal arrives, finding insufficient stack frame in such cases. I'd choose the second one if we only have these two options, because there is a chance that the signal handler may not even run.
Quoted 4 lines
I think we might have different views here. A process has a pre-V, a and post-V state. Is allowing a process to enter V without the correct preconditions a good idea? Allow to run with V turned on, but not able to correctly handle a signal (the stack is too small)?
The requirement is sane, but the issue is user experience: User trying to bring up some V code has no clue that deep in some startup code some alt stack had been setup and causing his process to be terminated on first V code.
Quoted 11 lines
This was the same argument that the Intel folks had when enabling AMX. Sure, AMX requires *explicit* enablement, but same rules should apply, no?
Quoted 6 lines
Quoted 1 line
2. Sanitize altstack changes when V is enabled.
Yes, I'd like to have this. But it may be tricky when it comes to deciding whether V is enabled, due to the first-use trap. If V is commonly used in system libraries then it is likely that V will be enabled before an user set an altstack. Sanitizing this case would be easy and straightforward.
Good. Lets have this in v14 as it seems reasonably easy to implement.
Quoted 5 lines
Quoted 5 lines
But what if the user sets an altstack before
enabling V in the first-use trap? This could happen on a statically program that has hand-written V routines. This takes us to the 1st question above, should we fail the user program immediately if the altstack is set too small?Please lets not cross threads. We discussed this already at top. While ideally required, seems tricky so lets start with post-V alt stack check.
Quoted 6 lines
For me it's obvious to fail (always) "if the altstack is too small to enable V", because it allows to execute V without proper preconditions.
Personally, I prefer a stricter model. Only enter V if you can, and after entering it disallow changing the altstack.
Quoted 6 lines
Quoted 2 lines
Then again, this is *my* opinion and concern. What do other people think? I don't want to stall the series.
I concur that the alt stack checking requirements are sensible in the long run. We can add the obvious check for post-V case and see if there is a sane way to flag pre-V case to.
Reasonable. @Andy does this resonate with you as well?
Bj?rn
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Quoted 59 lines · Bj?rn T?pel
Quoted 56 lines · Vineet Gupta
Quoted 16 lines · Bj?rn T?pel
Quoted 7 lines · Andy Chiu
Hey Bj?rn,
Quoted 4 lines · Bj?rn T?pel
So, two changes:
1. Disallow V-enablement if the existing altstack does not fit a V-sized frame.
Quoted 7 lines
This could potentially break old programs (non-V) that load new system libraries (with V), If the program sets a small alt stack and takes the fault in some libraries that use V. However, existing implementation will also kill the process when the signal arrives, finding insufficient stack frame in such cases. I'd choose the second one if we only have these two options, because there is a chance that the signal handler may not even run.
Quoted 4 lines
I think we might have different views here. A process has a pre-V, a and post-V state. Is allowing a process to enter V without the correct preconditions a good idea? Allow to run with V turned on, but not able to correctly handle a signal (the stack is too small)?
The requirement is sane, but the issue is user experience: User trying to bring up some V code has no clue that deep in some startup code some alt stack had been setup and causing his process to be terminated on first V code.
Quoted 11 lines
This was the same argument that the Intel folks had when enabling AMX. Sure, AMX requires *explicit* enablement, but same rules should apply, no?
Quoted 6 lines
Quoted 1 line
2. Sanitize altstack changes when V is enabled.
Yes, I'd like to have this. But it may be tricky when it comes to deciding whether V is enabled, due to the first-use trap. If V is commonly used in system libraries then it is likely that V will be enabled before an user set an altstack. Sanitizing this case would be easy and straightforward.
Good. Lets have this in v14 as it seems reasonably easy to implement.
Quoted 5 lines
Quoted 5 lines
But what if the user sets an altstack before
enabling V in the first-use trap? This could happen on a statically program that has hand-written V routines. This takes us to the 1st question above, should we fail the user program immediately if the altstack is set too small?Please lets not cross threads. We discussed this already at top. While ideally required, seems tricky so lets start with post-V alt stack check.
Quoted 6 lines
For me it's obvious to fail (always) "if the altstack is too small to enable V", because it allows to execute V without proper preconditions.
Personally, I prefer a stricter model. Only enter V if you can, and after entering it disallow changing the altstack.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Then again, this is *my* opinion and concern. What do other people think? I don't want to stall the series.
I concur that the alt stack checking requirements are sensible in the long run. We can add the obvious check for post-V case and see if there is a sane way to flag pre-V case to.
Reasonable. @Andy does this resonate with you as well?
Yes, it makes sense to me. I am making this happen on v14 :)
Thanks,
Andy
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
In reply to Andy ChiuHi Andy,
Quoted 7 lines · Andy Chiu
arch/riscv/kernel/vector.c new file +7
-0+static bool insn_is_vector (u32 insn_buf) +{ +u32 opcode = insn_buf & __INSN_OPCODE_MASK; +/* +* All V-related instructions, including CSR operations are 4-Byte. So, +* do not handle if the instruction length is not 4-Byte. +*/
arch/riscv/kernel/vector.c line 35
Quoted 10 lines
arch/riscv/kernel/vector.c new file +10
-0+if (unlikely (GET_INSN_LENGTH (insn_buf) !=4 )) +return false; +if (opcode ==OPCODE_VECTOR ) { +return true; + }else if (opcode ==OPCODE_LOADFP || opcode ==OPCODE_STOREFP ) { +u32 width =EXTRACT_LOAD_STORE_FP_WIDTH (insn_buf); + +if (width ==LSFP_WIDTH_RVV_8 || width ==LSFP_WIDTH_RVV_16 || + width ==LSFP_WIDTH_RVV_32 || width ==LSFP_WIDTH_RVV_64 ) +return true;
What is the purpose of checking FP opcodes here ?
Quoted 9 lines
arch/riscv/kernel/vector.c new file +9
-0+ }else if (opcode ==RVG_OPCODE_SYSTEM ) { +u32 csr =EXTRACT_SYSTEM_CSR (insn_buf); + +if ((csr >=CSR_VSTART && csr <=CSR_VCSR ) || + (csr >=CSR_VL && csr <=CSR_VLENB )) +return true; + } +return false; +}
Re: [PATCH -next v13 10/19] riscv: Allocate user's vector context in the first-use trap
Quoted 14 lines · Vineet Gupta
Hi Andy,
Quoted 11 lines · Andy Chiu
arch/riscv/kernel/vector.c new file +11
-0+static bool insn_is_vector (u32 insn_buf) +{ +u32 opcode = insn_buf & __INSN_OPCODE_MASK; +/* +* All V-related instructions, including CSR operations are 4-Byte. So, +* do not handle if the instruction length is not 4-Byte. +*/ +if (unlikely (GET_INSN_LENGTH (insn_buf) !=4 )) +return false; +if (opcode ==OPCODE_VECTOR ) { +return true;
Quoted 8 lines
arch/riscv/kernel/vector.c line 35
Quoted 6 lines
arch/riscv/kernel/vector.c new file +6
-0+ }else if (opcode ==OPCODE_LOADFP || opcode ==OPCODE_STOREFP ) { +u32 width =EXTRACT_LOAD_STORE_FP_WIDTH (insn_buf); + +if (width ==LSFP_WIDTH_RVV_8 || width ==LSFP_WIDTH_RVV_16 || + width ==LSFP_WIDTH_RVV_32 || width ==LSFP_WIDTH_RVV_64 ) +return true;What is the purpose of checking FP opcodes here ?
From [1]: "The instructions in the vector extension fit under two existing major opcodes (LOAD-FP and STORE-FP) and one new major opcode (OP-V)."
[2] highlights the width encoding.
(And Zvamo is out from the spec, which used AMO,0x2f)
[1] https://github.com/riscv/riscv-v-spec/blob/master/v-spec.adoc#5-vector-instruction-formats
[2] https://github.com/riscv/riscv-v-spec/blob/master/v-spec.adoc#73-vector-loadstore-width-encoding
PATCH v13 11/19 riscv: Add ptrace vector support
In reply to Andy ChiuFrom: Greentime Hu <[email protected]>
This patch adds ptrace support for riscv vector. The vector registers will be saved in datap pointer of __riscv_v_state. This pointer will be set right after the __riscv_v_state data structure then it will be put in ubuf for ptrace system call to get or set. It will check if the datap got from ubuf is set to the correct address or not when the ptrace system call is trying to set the vector registers.
--- arch/riscv/include/uapi/asm/ptrace.h | 7 +++ arch/riscv/kernel/ptrace.c | 71 ++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 79 insertions(+)
arch/riscv/include/uapi/asm/ptrace.h +7 -0
@@ -94,6 +94,13 @@ struct __riscv_v_state {*/ }; +/* +* According to spec: The number of bits in a single vector register, +* VLEN >= ELEN, which must be a power of 2, and must be no greater than +* 2^16 = 65536bits = 8192bytes +*/ +#define RISCV_MAX_VLENB (8192) +#endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_RISCV_PTRACE_H */
arch/riscv/kernel/ptrace.c +71 -0
95 lines not on this page · raw message
include/uapi/linux/elf.h +1 -0
@@ -439,6 +439,7 @@ typedef struct elf64_shdr {#define NT_MIPS_DSP 0x800/* MIPS DSP ASE registers */ #define NT_MIPS_FP_MODE 0x801/* MIPS floating-point mode */ #define NT_MIPS_MSA 0x802/* MIPS SIMD registers */ +#define NT_RISCV_VECTOR 0x900/* RISC-V vector registers */ #define NT_LOONGARCH_CPUCFG 0xa00/* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01/* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02/* LoongArch Loongson SIMD Extension registers */
Signature · 1 line
2.17.1
PATCH v13 12/19 riscv: signal: check fp-reserved words unconditionally
In reply to Andy ChiuIn order to let kernel/user locate and identify an extension context on the existing sigframe, we are going to utilize reserved space of fp and encode the information there. And since the sigcontext has already preserved a space for fp context w or w/o CONFIG_FPU, we move those reserved words checking/setting routine back into generic code.
This commit also undone an additional logical change carried by the refactor commit 007f5c3589578
("Refactor FPU code in signal setup/return procedures"). Originally we did not restore fp context if restoring of gpr have failed. And it was fine on the other side. In such way the kernel could keep the regfiles intact, and potentially react at the failing point of restore.
--- arch/riscv/kernel/signal.c | 53 +++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 27 deletions(-)
arch/riscv/kernel/signal.c +26 -27
@@ -38,26 +38,13 @@ static long restore_fp_state(struct pt_regs *regs, {long err;struct __riscv_d_ext_state __user *state = &sc_fpregs->d; -size_t i; err =__copy_from_user (¤t->thread.fstate, state,sizeof (*state));if (unlikely (err))return err;fstate_restore (current, regs); - -/* We support no other extension state at this time. */ -for (i =0 ; i <ARRAY_SIZE (sc_fpregs->q.reserved); i++) { -u32 value; - - err =__get_user (value, &sc_fpregs->q.reserved[i]); -if (unlikely (err)) -break ; -if (value !=0 ) -return -EINVAL ; - } - -return err; +return 0 ; } static long save_fp_state(struct pt_regs *regs,
@@ -65,20 +52,9 @@ static long save_fp_state(struct pt_regs *regs, {long err;struct __riscv_d_ext_state __user *state = &sc_fpregs->d; -size_t i;fstate_save (current, regs); err =__copy_to_user (state, ¤t->thread.fstate,sizeof (*state)); -if (unlikely (err)) -return err; - -/* We support no other extension state at this time. */ -for (i =0 ; i <ARRAY_SIZE (sc_fpregs->q.reserved); i++) { - err =__put_user (0 , &sc_fpregs->q.reserved[i]); -if (unlikely (err)) -break ; - } -return err; }#else
@@ -90,11 +66,29 @@ static long restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) {long err; +size_t i; +/* sc_regs is structured the same as the start of pt_regs */ err =__copy_from_user (regs, &sc->sc_regs,sizeof (sc->sc_regs)); +if (unlikely (err)) +return err;/* Restore the floating-point state. */ -if (has_fpu ()) - err |=restore_fp_state (regs, &sc->sc_fpregs); +if (has_fpu ()) { + err =restore_fp_state (regs, &sc->sc_fpregs); +if (unlikely (err)) +return err; + } + +/* We support no other extension state at this time. */ +for (i =0 ; i <ARRAY_SIZE (sc->sc_fpregs.q.reserved); i++) { +u32 value; + + err =__get_user (value, &sc->sc_fpregs.q.reserved[i]); +if (unlikely (err)) +break ; +if (value !=0 ) +return -EINVAL ; + }return err; }
@@ -145,11 +139,16 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, {struct sigcontext __user *sc = &frame->uc.uc_mcontext;long err; +size_t i; +/* sc_regs is structured the same as the start of pt_regs */ err =__copy_to_user (&sc->sc_regs, regs,sizeof (sc->sc_regs));/* Save the floating-point state. */ if (has_fpu ()) err |=save_fp_state (regs, &sc->sc_fpregs); +/* We support no other extension state at this time. */ +for (i =0 ; i <ARRAY_SIZE (sc->sc_fpregs.q.reserved); i++) + err |=__put_user (0 , &sc->sc_fpregs.q.reserved[i]);return err; }
Signature · 1 line
2.17.1
PATCH v13 13/19 riscv: signal: Add sigcontext save/restore for vector
In reply to Andy ChiuFrom: Greentime Hu <[email protected]>
This patch facilitates the existing fp-reserved words for placement of the first extension's context header on the user's sigframe. A context header consists of a distinct magic word and the size, including the header itself, of an extension on the stack. Then, the frame is followed by the context of that extension, and then a header + context body for another extension if exists. If there is no more extension to come, then the frame must be ended with a null context header. A special case is rv64gc, where the kernel support no extensions requiring to expose additional regfile to the user. In such case the kernel would place the null context header right after the first reserved word of __riscv_q_ext_state when saving sigframe. And the kernel would check if all reserved words are zeros when a signal handler returns.
__riscv_q_ext_state---->| |<-__riscv_extra_ext_header
~ ~ .reserved[0]--->|0 |<- .reserved <-------|magic |<- .hdr | |size |_______ end of sc_fpregs | |ext-bdy| | ~ ~ +)size ------->|magic |<- another context header |size | |ext-bdy| ~ ~ |magic:0|<- null context header |size:0 |
The vector registers will be saved in datap pointer. The datap pointer will be allocated dynamically when the task needs in kernel space. On the other hand, datap pointer on the sigframe will be set right after the __riscv_v_state data structure.
--- arch/riscv/include/uapi/asm/ptrace.h | 15 ++ arch/riscv/include/uapi/asm/sigcontext.h | 16 ++- arch/riscv/kernel/setup.c | 3 + arch/riscv/kernel/signal.c | 175 ++++++++++++++++++++--- 4 files changed, 189 insertions(+), 20 deletions(-)
arch/riscv/include/uapi/asm/ptrace.h +15 -0
@@ -71,6 +71,21 @@ struct __riscv_q_ext_state {__u32 reserved[3 ]; }; +struct __riscv_ctx_hdr { +__u32 magic; +__u32 size; +}; + +struct __riscv_extra_ext_header { +__u32 __padding[129 ] __attribute__((aligned (16 ))); +/* +* Reserved for expansion of sigcontext structure. Currently zeroed +* upon signal, and must be zero upon sigreturn. +*/ +__u32 reserved; +struct __riscv_ctx_hdr hdr; +}; +union __riscv_fp_state {struct __riscv_f_ext_state f;struct __riscv_d_ext_state d;
arch/riscv/include/uapi/asm/sigcontext.h +15 -1
@@ -8,6 +8,17 @@#include <asm/ptrace.h> +/* The Magic number for signal context frame header. */ +#define RVV_MAGIC 0x53465457 +#define END_MAGIC 0x0 + +/* The size of END signal context header. */ +#define END_HDR_SIZE 0x0 + +struct __sc_riscv_v_state { +struct __riscv_v_state v_state; +} __attribute__((aligned (16 ))); +/* * Signal context structure *
@@ -16,7 +27,10 @@*/ struct sigcontext {struct user_regs_struct sc_regs; -union __riscv_fp_state sc_fpregs; +union { +union __riscv_fp_state sc_fpregs; +struct __riscv_extra_ext_header sc_extdesc; + }; };#endif /* _UAPI_ASM_RISCV_SIGCONTEXT_H */
arch/riscv/kernel/setup.c +3 -0
@@ -262,6 +262,8 @@ static void __init parse_dtb(void)#endif } +extern void __init init_rt_signal_env (void ); +void __init setup_arch (char **cmdline_p) {parse_dtb ();
@@ -299,6 +301,7 @@ void __init setup_arch(char **cmdline_p)riscv_init_cbom_blocksize ();riscv_fill_hwcap (); +init_rt_signal_env ();apply_boot_alternatives (); }
arch/riscv/kernel/signal.c +156 -19
241 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v13 14/19 riscv: signal: Report signal frame size to userspace via auxv
In reply to Andy ChiuFrom: Vincent Chen <[email protected]>
The vector register belongs to the signal context. They need to be stored and restored as entering and leaving the signal handler. According to the V-extension specification, the maximum length of the vector registers can be 2^(XLEN-1). Hence, if userspace refers to the MINSIGSTKSZ to create a sigframe, it may not be enough. To resolve this problem, this patch refers to the commit 94b07c1f8c39c
("arm64: signal: Report signal frame size to userspace via auxv") to enable userspace to know the minimum required sigframe size through the auxiliary vector and use it to allocate enough memory for signal context.
Note that auxv always reports size of the sigframe as if V exists for all starting processes, whenever the kernel has CONFIG_RISCV_ISA_V. The reason is that users usually reference this value to allocate an alternative signal stack, and the user may use V anytime. So the user must reserve a space for V-context in sigframe in case that the signal handler invokes after the kernel allocating V.
--- arch/riscv/include/asm/elf.h | 9 +++++++++ arch/riscv/include/asm/processor.h | 2 ++ arch/riscv/include/uapi/asm/auxvec.h | 1 + arch/riscv/kernel/signal.c | 20 +++++++++++++++----- 4 files changed, 27 insertions(+), 5 deletions(-)
arch/riscv/include/asm/elf.h +9 -0
@@ -103,6 +103,15 @@ do { \ get_cache_size(3, CACHE_TYPE_UNIFIED)); \NEW_AUX_ENT (AT_L3_CACHEGEOMETRY , \get_cache_geometry (3 ,CACHE_TYPE_UNIFIED )); \ +/* \ +* Should always be nonzero unless there's a kernel bug. \ +* If we haven't determined a sensible value to give to \ +* userspace, omit the entry: \ +*/ \ +if (likely (signal_minsigstksz)) \ +NEW_AUX_ENT (AT_MINSIGSTKSZ , signal_minsigstksz); \ +else \ +NEW_AUX_ENT (AT_IGNORE ,0 ); \ } while (0)#define ARCH_HAS_SETUP_ADDITIONAL_PAGES struct linux_binprm ;
arch/riscv/include/asm/processor.h +2 -0
@@ -7,6 +7,7 @@#define _ASM_RISCV_PROCESSOR_H#include <linux/const.h> +#include <linux/cache.h> #include <vdso/processor.h>
@@ -81,6 +82,7 @@ int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid);extern void riscv_fill_hwcap (void );extern int arch_dup_task_struct (struct task_struct *dst,struct task_struct *src); +extern unsigned long signal_minsigstksz__ro_after_init ;#endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */
arch/riscv/include/uapi/asm/auxvec.h +1 -0
@@ -35,5 +35,6 @@/* entries in ARCH_DLINFO */ #define AT_VECTOR_SIZE_ARCH 9 +#define AT_MINSIGSTKSZ 51#endif /* _UAPI_ASM_RISCV_AUXVEC_H */
arch/riscv/kernel/signal.c +15 -5
@@ -21,6 +21,8 @@#include <asm/vector.h> #include <asm/csr.h> +unsigned long __ro_after_init signal_minsigstksz; +extern u32 __user_rt_sigreturn[2 ];static size_t rvv_sc_size;
@@ -195,7 +197,7 @@ static long restore_sigcontext(struct pt_regs *regs,return -EINVAL ; } -static size_t cal_rt_frame_size (void ) +static size_t cal_rt_frame_size (bool cal_all) {struct rt_sigframe __user *frame;size_t frame_size;
@@ -203,8 +205,10 @@ static size_t cal_rt_frame_size(void) frame_size =sizeof (*frame); -if (has_vector () &&vstate_query (task_pt_regs (current))) - total_context_size += rvv_sc_size; +if (has_vector ()) { +if (cal_all ||vstate_query (task_pt_regs (current))) + total_context_size += rvv_sc_size; + }/* Preserved a __riscv_ctx_hdr for END signal context header. */ total_context_size +=sizeof (struct __riscv_ctx_hdr );
@@ -221,7 +225,7 @@ SYSCALL_DEFINE0(rt_sigreturn)struct rt_sigframe __user *frame;struct task_struct *task;sigset_t set; -size_t frame_size =cal_rt_frame_size (); +size_t frame_size =cal_rt_frame_size (false);/* Always make any pending restarted system calls return -EINTR */ current->restart_block.fn = do_no_restart_syscall;
@@ -309,7 +313,7 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, {struct rt_sigframe __user *frame;long err =0 ; -size_t frame_size =cal_rt_frame_size (); +size_t frame_size =cal_rt_frame_size (false); frame =get_sigframe (ksig, regs, frame_size);if (!access_ok (frame, frame_size))
@@ -472,4 +476,10 @@ void __init init_rt_signal_env(void) { rvv_sc_size =sizeof (struct __riscv_ctx_hdr ) +sizeof (struct __sc_riscv_v_state ) + riscv_vsize; +/* +* Determine the stack space required for guaranteed signal delivery. +* The signal_minsigstksz will be populated into the AT_MINSIGSTKSZ entry +* in the auxiliary array at process startup. +*/ + signal_minsigstksz =cal_rt_frame_size (true); }
Signature · 1 line
2.17.1
Re: [PATCH -next v13 14/19] riscv: signal: Report signal frame size to userspace via auxv
arch/riscv/include/asm/processor.h line 10
Quoted 9 lines · Andy Chiu
arch/riscv/include/asm/processor.h +1
-0@@ -7,6 +7,7 @@#define _ASM_RISCV_PROCESSOR_H#include <linux/const.h> +#include <linux/cache.h>
What have I missed that is the reason for adding this header?
arch/riscv/include/asm/processor.h static size_t cal_rt_frame_size(void) · line 207
Quoted 8 lines
arch/riscv/include/asm/processor.h +0
-2#include <vdso/processor.h> @@ -203,8 +205,10 @@ static size_t cal_rt_frame_size(void) frame_size =sizeof (*frame); -if (has_vector () &&vstate_query (task_pt_regs (current))) - total_context_size += rvv_sc_size;
Usual naming comment here about rvv.
Thanks,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230126/b8eb60e6/attachment-0001.sig>
Re: [PATCH -next v13 14/19] riscv: signal: Report signal frame size to userspace via auxv
Quoted 9 lines · Conor Dooley
Quoted 7 lines · Andy Chiu
arch/riscv/include/asm/processor.h +0
-0@@ -7,6 +7,7 @@#define _ASM_RISCV_PROCESSOR_H
Quoted 5 lines
arch/riscv/include/asm/processor.h line 10
Quoted 2 lines
arch/riscv/include/asm/processor.h +1
-0#include <linux/const.h> +#include <linux/cache.h> What have I missed that is the reason for adding this header?
__ro_after_init is defined in linux/cache.h, so we need that header.
Quoted 10 lines
arch/riscv/include/asm/processor.h static size_t cal_rt_frame_size(void) · line 207
Quoted 8 lines
arch/riscv/include/asm/processor.h +0
-2#include <vdso/processor.h> @@ -203,8 +205,10 @@ static size_t cal_rt_frame_size(void) frame_size =sizeof (*frame); -if (has_vector () &&vstate_query (task_pt_regs (current))) - total_context_size += rvv_sc_size;Usual naming comment here about rvv.
Ok, changing it to riscv_v_
Quoted 4 lines
Thanks,
Conor.
PATCH v13 15/19 riscv: Fix a kernel panic issue if $s2 is set to a specific value before entering Linux
In reply to Andy ChiuFrom: Greentime Hu <[email protected]>
Panic log:
[ 0.018707] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 [ 0.023060] Oops [#1] [ 0.023214] Modules linked in: [ 0.023725] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.14.0 #33 [ 0.023955] Hardware name: SiFive,FU800 (DT) [ 0.024150] epc : __vstate_save+0x1c/0x48 [ 0.024654] ra : arch_dup_task_struct+0x70/0x108 [ 0.024815] epc : ffffffff80005ad8 ra : ffffffff800035a8 sp : ffffffff81203d50 [ 0.025020] gp : ffffffff812e8290 tp : ffffffff8120bdc0 t0 : 0000000000000000 [ 0.025216] t1 : 0000000000000000 t2 : 0000000000000000 s0 : ffffffff81203d80 [ 0.025424] s1 : ffffffff8120bdc0 a0 : ffffffff8120c820 a1 : 0000000000000000 [ 0.025659] a2 : 0000000000001000 a3 : 0000000000000000 a4 : 0000000000000600 [ 0.025869] a5 : ffffffff8120cdc0 a6 : ffffffe00160b400 a7 : ffffffff80a1fe60 [ 0.026069] s2 : ffffffe0016b8000 s3 : ffffffff81204000 s4 : 0000000000004000 [ 0.026267] s5 : 0000000000000000 s6 : ffffffe0016b8000 s7 : ffffffe0016b9000 [ 0.026475] s8 : ffffffff81203ee0 s9 : 0000000000800300 s10: ffffffff812e9088 [ 0.026689] s11: ffffffd004008000 t3 : 0000000000000000 t4 : 0000000000000100 [ 0.026900] t5 : 0000000000000600 t6 : ffffffe00167bcc4 [ 0.027057] status: 8000000000000720 badaddr: 0000000000000000 cause: 000000000000000f [ 0.027344] [<ffffffff80005ad8>] __vstate_save+0x1c/0x48 [ 0.027567] [<ffffffff8000abe8>] copy_process+0x266/0x11a0 [ 0.027739] [<ffffffff8000bc98>] kernel_clone+0x90/0x2aa [ 0.027915] [<ffffffff8000c062>] kernel_thread+0x76/0x92 [ 0.028075] [<ffffffff8072e34c>] rest_init+0x26/0xfc [ 0.028242] [<ffffffff80800638>] arch_call_rest_init+0x10/0x18 [ 0.028423] [<ffffffff80800c4a>] start_kernel+0x5ce/0x5fe [ 0.029188] ---[ end trace 9a59af33f7ba3df4 ]--- [ 0.029479] Kernel panic - not syncing: Attempted to kill the idle task! [ 0.029907] ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]---
The NULL pointer accessing caused the kernel panic. There is a NULL pointer is because in vstate_save() function it will check (regs->status & SR_VS) == SR_VS_DIRTY and this is true, but it shouldn't be true because vector is not used here. Since vector is not used, datap won't be allocated so it is NULL. The reason why regs->status is set to a wrong value is because pt_regs->status is put in stack and it is polluted after setup_vm() called.
In prologue of setup_vm(), we can observe it will save s2 to stack however s2 is meaningless here because the caller is assembly code and s2 is just some value from previous stage. The compiler will base on calling convention to save the register to stack. Then 0x80008638 in s2 is saved to stack. It might be any value. In this failure case it is 0x80008638 and it will accidentally cause SR_VS_DIRTY to call the vstate_save() function.
(gdb) info addr setup_vm
Symbol "setup_vm" is a function at address 0xffffffff80802c8a. (gdb) va2pa 0xffffffff80802c8a
$64 = 0x80a02c8a
(gdb) x/10i 0x80a02c8a
0x80a02c8a: addi sp,sp,-48 0x80a02c8c: li a3,-1 0x80a02c8e: auipc a5,0xff7fd 0x80a02c92: addi a5,a5,882 0x80a02c96: sd s0,32(sp) 0x80a02c98: sd s2,16(sp) <-- store to stack
After returning from setup_vm()
(gdb) x/20i 0x0000000080201138
0x80201138: mv a0,s1 0x8020113a: auipc ra,0x802 0x8020113e: jalr -1200(ra) <-- jump to setup_vm() 0x80201142: auipc a0,0xa03
(gdb) p/x $sp
$70 = 0x81404000
(gdb) p/x *(struct pt_regs*)($sp-0x120)
$71 = {
epc = 0x0,
ra = 0x0,
sp = 0x0,
gp = 0x0,
tp = 0x0,
t0 = 0x0,
t1 = 0x0,
t2 = 0x0,
s0 = 0x0,
s1 = 0x0,
a0 = 0x0,
a1 = 0x0,
a2 = 0x0,
a3 = 0x81403f90,
a4 = 0x80c04000,
a5 = 0x1,
a6 = 0xffffffff81337000,
a7 = 0x81096700,
s2 = 0x81400000,
s3 = 0xffffffff81200000,
s4 = 0x81403fd0,
s5 = 0x80a02c6c,
s6 = 0x8000000000006800,
s7 = 0x0,
s8 = 0xfffffffffffffff3,
s9 = 0x80c01000,
s10 = 0x81096700,
s11 = 0x82200000,
t3 = 0x81404000,
t4 = 0x80a02dea,
t5 = 0x0,
t6 = 0x82200000,
status = 0x80008638, <- Wrong value in stack!!!
badaddr = 0x82200000,
cause = 0x0,
orig_a0 = 0x80201142
}
(gdb) p/x $pc
$72 = 0x80201142
(gdb) p/x sizeof(struct pt_regs)
$73 = 0x120
--- arch/riscv/kernel/head.S | 2 ++ 1 file changed, 2 insertions(+)
arch/riscv/kernel/head.S +2 -0
@@ -301,6 +301,7 @@ clear_bss_done: la tp, init_task la sp, init_thread_union + THREAD_SIZE XIP_FIXUP_OFFSET sp + addi sp, sp, -PT_SIZE #ifdef CONFIG_BUILTIN_DTB la a0, __dtb_start XIP_FIXUP_OFFSET a0
@@ -318,6 +319,7 @@ clear_bss_done: /* Restore C environment */ la tp, init_task la sp, init_thread_union + THREAD_SIZE + addi sp, sp, -PT_SIZE #ifdef CONFIG_KASAN call kasan_early_init
Signature · 1 line
2.17.1
Re: [PATCH -next v13 15/19] riscv: Fix a kernel panic issue if $s2 is set to a specific value before entering Linux
Hey Andy,
Quoted 61 lines · Andy Chiu
From: Greentime Hu <[email protected]>
Panic log:
[ 0.018707] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 [ 0.023060] Oops [#1] [ 0.023214] Modules linked in: [ 0.023725] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.14.0 #33 [ 0.023955] Hardware name: SiFive,FU800 (DT) [ 0.024150] epc : __vstate_save+0x1c/0x48 [ 0.024654] ra : arch_dup_task_struct+0x70/0x108 [ 0.024815] epc : ffffffff80005ad8 ra : ffffffff800035a8 sp : ffffffff81203d50 [ 0.025020] gp : ffffffff812e8290 tp : ffffffff8120bdc0 t0 : 0000000000000000 [ 0.025216] t1 : 0000000000000000 t2 : 0000000000000000 s0 : ffffffff81203d80 [ 0.025424] s1 : ffffffff8120bdc0 a0 : ffffffff8120c820 a1 : 0000000000000000 [ 0.025659] a2 : 0000000000001000 a3 : 0000000000000000 a4 : 0000000000000600 [ 0.025869] a5 : ffffffff8120cdc0 a6 : ffffffe00160b400 a7 : ffffffff80a1fe60 [ 0.026069] s2 : ffffffe0016b8000 s3 : ffffffff81204000 s4 : 0000000000004000 [ 0.026267] s5 : 0000000000000000 s6 : ffffffe0016b8000 s7 : ffffffe0016b9000 [ 0.026475] s8 : ffffffff81203ee0 s9 : 0000000000800300 s10: ffffffff812e9088 [ 0.026689] s11: ffffffd004008000 t3 : 0000000000000000 t4 : 0000000000000100 [ 0.026900] t5 : 0000000000000600 t6 : ffffffe00167bcc4 [ 0.027057] status: 8000000000000720 badaddr: 0000000000000000 cause: 000000000000000f [ 0.027344] [<ffffffff80005ad8>] __vstate_save+0x1c/0x48 [ 0.027567] [<ffffffff8000abe8>] copy_process+0x266/0x11a0 [ 0.027739] [<ffffffff8000bc98>] kernel_clone+0x90/0x2aa [ 0.027915] [<ffffffff8000c062>] kernel_thread+0x76/0x92 [ 0.028075] [<ffffffff8072e34c>] rest_init+0x26/0xfc [ 0.028242] [<ffffffff80800638>] arch_call_rest_init+0x10/0x18 [ 0.028423] [<ffffffff80800c4a>] start_kernel+0x5ce/0x5fe [ 0.029188] ---[ end trace 9a59af33f7ba3df4 ]--- [ 0.029479] Kernel panic - not syncing: Attempted to kill the idle task! [ 0.029907] ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]---The NULL pointer accessing caused the kernel panic. There is a NULL pointer is because in
vstate_save()function it will check (regs->status&SR_VS) ==SR_VS_DIRTYand this is true, but it shouldn't be true because vector is not used here. Since vector is not used, datap won't be allocated so it is NULL. The reason whyregs->statusis set to a wrong value is becausept_regs->statusis put in stack and it is polluted aftersetup_vm()called.In prologue of
setup_vm(), we can observe it will save s2 to stack however s2 is meaningless here because the caller is assembly code and s2 is just some value from previous stage. The compiler will base on calling convention to save the register to stack. Then0x80008638in s2 is saved to stack. It might be any value. In this failure case it is0x80008638and it will accidentally causeSR_VS_DIRTYto call thevstate_save()function.arch/riscv/kernel/head.S +1
-0@@ -301,6 +301,7 @@ clear_bss_done: la tp, init_task la sp, init_thread_union + THREAD_SIZE XIP_FIXUP_OFFSET sp + addi sp, sp, -PT_SIZE #ifdef CONFIG_BUILTIN_DTB la a0, __dtb_start XIP_FIXUP_OFFSET a0
arch/riscv/kernel/head.S clear_bss_done: · line 322
Quoted 4 lines
arch/riscv/kernel/head.S +1
-0@@ -318,6 +319,7 @@ clear_bss_done: /* Restore C environment */ la tp, init_task la sp, init_thread_union + THREAD_SIZE + addi sp, sp, -PT_SIZE
I've got no idea about this stuff, so I was just poking around at existing, similar bits of asm.
grepping for code that does this (with your series applied), you are the only one who is using PT_SIZE rather than PT_SIZE_ON_STACK:
rg "\bPT_SIZE" arch/riscvarch/riscv/kernel/head.S
304: addi sp, sp, -PT_SIZE
322: addi sp, sp, -PT_SIZE
arch/riscv/kernel/asm-offsets.c
78: DEFINE(PT_SIZE, sizeof(struct pt_regs));
472: DEFINE(PT_SIZE_ON_STACK, ALIGN(sizeof(struct pt_regs), STACK_ALIGN));
arch/riscv/kernel/probes/rethook_trampoline.S
79: addi sp, sp, -(PT_SIZE_ON_STACK)
90: addi sp, sp, PT_SIZE_ON_STACK
arch/riscv/kernel/entry.S
35: addi sp, sp, -(PT_SIZE_ON_STACK)
45: addi sp, sp, -(PT_SIZE_ON_STACK)
277: addi s0, sp, PT_SIZE_ON_STACK
417: addi sp, sp, -(PT_SIZE_ON_STACK)
461: addi sp, sp, -(PT_SIZE_ON_STACK)
arch/riscv/kernel/mcount-dyn.S
61: addi sp, sp, -PT_SIZE_ON_STACK
64: addi sp, sp, PT_SIZE_ON_STACK
66: addi sp, sp, -PT_SIZE_ON_STACK
104: addi sp, sp, PT_SIZE_ON_STACK
106: addi sp, sp, -PT_SIZE_ON_STACK
139: addi sp, sp, PT_SIZE_ON_STACK
179: REG_L a1, PT_SIZE_ON_STACK(sp)
As I said, I don't know this area, so I am just pointing out the difference, and wondering if it is intentional!
Cheers,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230127/12c4f52d/attachment-0001.sig>
Re: [PATCH -next v13 15/19] riscv: Fix a kernel panic issue if $s2 is set to a specific value before entering Linux
Quoted 2 lines · Conor Dooley
grepping for code that does this (with your series applied), you are the only one who is using
PT_SIZErather thanPT_SIZE_ON_STACK:
Yes, you are right. It should be PT_SIZE_ON_STACK, which considers alignment of the stack top. The task_pt_regs() counter parts, which is the macro that operates on it, also aligns to STACK_ALIGN. So , we should use PT_SIZE_ON_STACK instead of PT_SIZE here.
#define task_pt_regs(tsk) \
((struct pt_regs *)(task_stack_page(tsk) + THREAD_SIZE \
- ALIGN(sizeof(struct pt_regs), STACK_ALIGN)))
Thanks,
Andy
PATCH v13 16/19 riscv: Add V extension to KVM ISA
In reply to Andy ChiuFrom: Vincent Chen <[email protected]>
Add V extension to KVM isa extension list to enable supporting of V extension on VCPUs.
--- arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/vcpu.c | 1 + 2 files changed, 2 insertions(+)
arch/riscv/include/uapi/asm/kvm.h +1 -0
@@ -100,6 +100,7 @@ enum KVM_RISCV_ISA_EXT_ID {KVM_RISCV_ISA_EXT_H ,KVM_RISCV_ISA_EXT_I ,KVM_RISCV_ISA_EXT_M , +KVM_RISCV_ISA_EXT_V ,KVM_RISCV_ISA_EXT_SVPBMT ,KVM_RISCV_ISA_EXT_SSTC , KVM_RISCV_ISA_EXT_SVINVAL,
arch/riscv/kvm/vcpu.c +1 -0
@@ -57,6 +57,7 @@ static const unsigned long kvm_isa_ext_arr[] = { [KVM_RISCV_ISA_EXT_H] = RISCV_ISA_EXT_h, [KVM_RISCV_ISA_EXT_I] = RISCV_ISA_EXT_i, [KVM_RISCV_ISA_EXT_M] = RISCV_ISA_EXT_m, + [KVM_RISCV_ISA_EXT_V] = RISCV_ISA_EXT_v,KVM_ISA_EXT_ARR (SSTC ), KVM_ISA_EXT_ARR(SVINVAL),
Signature · 1 line
2.17.1
Re: [PATCH -next v13 16/19] riscv: Add V extension to KVM ISA
Quoted 1 line · Andy Chiu
riscv: Add V extension to KVM ISA
I figure this should probably be "riscv: kvm:" or some variant with more capital letters.
Quoted 18 lines
From: Vincent Chen <[email protected]>
Add V extension to KVM isa extension list to enable supporting of V extension on VCPUs.
Signed-off-by Vincent Chen <[email protected]>Signed-off-by Greentime Hu <[email protected]>Signed-off-by Andy Chiu <[email protected]>--- arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/vcpu.c | 1 + 2 files changed, 2 insertions(+)arch/riscv/include/uapi/asm/kvm.h +0
-0
arch/riscv/include/uapi/asm/kvm.h enum KVM_RISCV_ISA_EXT_ID { · line 106
Quoted 7 lines
arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -100,6 +100,7 @@ enum KVM_RISCV_ISA_EXT_ID {KVM_RISCV_ISA_EXT_H ,KVM_RISCV_ISA_EXT_I ,KVM_RISCV_ISA_EXT_M , +KVM_RISCV_ISA_EXT_V ,KVM_RISCV_ISA_EXT_SVPBMT ,KVM_RISCV_ISA_EXT_SSTC , KVM_RISCV_ISA_EXT_SVINVAL,
Ehh, this UAPI so, AFAIU, you cannot add this in the middle of the enum and new entries must go at the bottom. Quoting Drew: "we can't touch enum KVM_RISCV_ISA_EXT_ID as that's UAPI. All new extensions must be added at the bottom. We originally also had to keep kvm_isa_ext_arr[] in that order, but commit 1b5cbb8733f9 ("RISC-V: KVM: Make ISA ext mappings explicit") allows us to list its elements in any order."
arch/riscv/kvm/vcpu.c static const unsigned long kvm_isa_ext_arr[] = { · line 63
Quoted 12 lines
arch/riscv/kvm/vcpu.c +1
-0@@ -57,6 +57,7 @@ static const unsigned long kvm_isa_ext_arr[] = { [KVM_RISCV_ISA_EXT_H] = RISCV_ISA_EXT_h, [KVM_RISCV_ISA_EXT_I] = RISCV_ISA_EXT_i, [KVM_RISCV_ISA_EXT_M] = RISCV_ISA_EXT_m, + [KVM_RISCV_ISA_EXT_V] = RISCV_ISA_EXT_v,KVM_ISA_EXT_ARR (SSTC ), KVM_ISA_EXT_ARR(SVINVAL),
This one here is fine however.
Thanks,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230127/56e78d89/attachment-0001.sig>
Re: [PATCH -next v13 16/19] riscv: Add V extension to KVM ISA
Quoted 6 lines · Conor Dooley
Quoted 1 line · Andy Chiu
riscv: Add V extension to KVM ISA
I figure this should probably be "riscv: kvm:" or some variant with more capital letters.
Ok. adding it
Quoted 11 lines
Quoted 11 lines
arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -100,6 +100,7 @@ enum KVM_RISCV_ISA_EXT_ID {KVM_RISCV_ISA_EXT_H ,KVM_RISCV_ISA_EXT_I ,KVM_RISCV_ISA_EXT_M , +KVM_RISCV_ISA_EXT_V ,KVM_RISCV_ISA_EXT_SVPBMT , KVM_RISCV_ISA_EXT_SSTC,
Quoted 9 lines
arch/riscv/include/uapi/asm/kvm.h enum KVM_RISCV_ISA_EXT_ID { · line 106
Quoted 1 line
arch/riscv/include/uapi/asm/kvm.h +0
-0KVM_RISCV_ISA_EXT_SVINVAL,Ehh, this UAPI so, AFAIU, you cannot add this in the middle of the enum and new entries must go at the bottom. Quoting Drew: "we can't touch enum
KVM_RISCV_ISA_EXT_IDas that's UAPI. All new extensions must be added at the bottom. We originally also had to keep kvm_isa_ext_arr[] in that order, but commit1b5cbb8733f9("RISC-V: KVM: Make ISA ext mappings explicit") allows us to list its elements in any order."
Thanks to mentioning this potential ABI break, I have moved it to the end at v14 revision.
@@ -105,6 +105,7 @@ enum KVM_RISCV_ISA_EXT_ID {
KVM_RISCV_ISA_EXT_SVINVAL, KVM_RISCV_ISA_EXT_ZIHINTPAUSE, KVM_RISCV_ISA_EXT_ZICBOM,
+ KVM_RISCV_ISA_EXT_V,
KVM_RISCV_ISA_EXT_MAX,
};
Quoted 11 lines
Quoted 11 lines
arch/riscv/kvm/vcpu.c +1
-0@@ -57,6 +57,7 @@ static const unsigned long kvm_isa_ext_arr[] = { [KVM_RISCV_ISA_EXT_H] = RISCV_ISA_EXT_h, [KVM_RISCV_ISA_EXT_I] = RISCV_ISA_EXT_i, [KVM_RISCV_ISA_EXT_M] = RISCV_ISA_EXT_m, + [KVM_RISCV_ISA_EXT_V] = RISCV_ISA_EXT_v,
Quoted 4 lines
arch/riscv/kvm/vcpu.c static const unsigned long kvm_isa_ext_arr[] = { · line 63
Quoted 2 lines
arch/riscv/kvm/vcpu.c +0
-0KVM_ISA_EXT_ARR (SSTC ), KVM_ISA_EXT_ARR(SVINVAL),This one here is fine however.
Great!
Quoted 3 lines
Thanks,
Conor.
PATCH v13 17/19 riscv: KVM: Add vector lazy save/restore support
In reply to Andy ChiuFrom: Vincent Chen <[email protected]>
This patch adds vector context save/restore for guest VCPUs. To reduce the impact on KVM performance, the implementation imitates the FP context switch mechanism to lazily store and restore the vector context only when the kernel enters/exits the in-kernel run loop and not during the KVM world switch.
--- arch/riscv/include/asm/kvm_host.h | 2 + arch/riscv/include/asm/kvm_vcpu_vector.h | 77 ++++++++++ arch/riscv/include/uapi/asm/kvm.h | 7 + arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu.c | 30 ++++ arch/riscv/kvm/vcpu_vector.c | 177 +++++++++++++++++++++++ 6 files changed, 294 insertions(+) create mode 100644 arch/riscv/include/asm/kvm_vcpu_vector.h create mode 100644 arch/riscv/kvm/vcpu_vector.c
arch/riscv/include/asm/kvm_host.h +2 -0
@@ -14,6 +14,7 @@#include <linux/kvm_types.h> #include <linux/spinlock.h> #include <asm/hwcap.h> +#include <asm/ptrace.h> #include <asm/kvm_vcpu_fp.h> #include <asm/kvm_vcpu_insn.h> #include <asm/kvm_vcpu_sbi.h>
@@ -140,6 +141,7 @@ struct kvm_cpu_context {unsigned long sstatus;unsigned long hstatus;union __riscv_fp_state fp; +struct __riscv_v_state vector; };struct kvm_vcpu_csr {
arch/riscv/include/asm/kvm_vcpu_vector.h new file +77 -0
77 lines not on this page · raw message
arch/riscv/include/uapi/asm/kvm.h +7 -0
@@ -153,6 +153,13 @@ enum KVM_RISCV_ISA_EXT_ID {/* ISA Extension registers are mapped as type 7 */ #define KVM_REG_RISCV_ISA_EXT (0x07 << KVM_REG_RISCV_TYPE_SHIFT) +/* V extension registers are mapped as type 8 */ +#define KVM_REG_RISCV_VECTOR (0x08 << KVM_REG_RISCV_TYPE_SHIFT) +#define KVM_REG_RISCV_VECTOR_CSR_REG (name) \ + (offsetof(struct __riscv_v_state, name) / sizeof(unsigned long)) +#define KVM_REG_RISCV_VECTOR_REG (n) \ + ((n) + sizeof(struct __riscv_v_state) / sizeof(unsigned long)) +#endif #endif /* __LINUX_KVM_RISCV_H */
arch/riscv/kvm/Makefile +1 -0
@@ -17,6 +17,7 @@ kvm-y += mmu.o kvm-y += vcpu.o kvm-y += vcpu_exit.o kvm-y += vcpu_fp.o +kvm-y += vcpu_vector.o kvm-y += vcpu_insn.o kvm-y += vcpu_switch.o kvm-y += vcpu_sbi.o
arch/riscv/kvm/vcpu.c +30 -0
@@ -22,6 +22,8 @@#include <asm/cacheflush.h> #include <asm/hwcap.h> #include <asm/sbi.h> +#include <asm/vector.h> +#include <asm/kvm_vcpu_vector.h> const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),
@@ -134,6 +136,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)kvm_riscv_vcpu_fp_reset (vcpu); +kvm_riscv_vcpu_vector_reset (vcpu); +kvm_riscv_vcpu_timer_reset (vcpu);WRITE_ONCE (vcpu->arch.irqs_pending,0 );
@@ -189,6 +193,15 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) cntx->hstatus |=HSTATUS_SPVP ; cntx->hstatus |=HSTATUS_SPV ; +if (has_vector ()) { + cntx->vector.datap =kmalloc (riscv_vsize,GFP_KERNEL ); +if (!cntx->vector.datap) +return -ENOMEM ; + vcpu->arch.host_context.vector.datap =kzalloc (riscv_vsize,GFP_KERNEL ); +if (!vcpu->arch.host_context.vector.datap) +return -ENOMEM ; + } +/* By default, make CY, TM, and IR counters accessible in VU mode */ reset_csr->scounteren =0x7 ;
@@ -219,6 +232,9 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)/* Free unused pages pre-allocated for G-stage page table mappings */ kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_cache); + +/* Free vector context space for host and guest kernel */ +kvm_riscv_vcpu_free_vector_context (vcpu); }int kvm_cpu_has_pending_timer (struct kvm_vcpu *vcpu)
@@ -595,6 +611,9 @@ static int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu, KVM_REG_RISCV_FP_D);case KVM_REG_RISCV_ISA_EXT :return kvm_riscv_vcpu_set_reg_isa_ext (vcpu, reg); +case KVM_REG_RISCV_VECTOR : +return kvm_riscv_vcpu_set_reg_vector (vcpu, reg, +KVM_REG_RISCV_VECTOR );default :break ; }
@@ -622,6 +641,9 @@ static int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu, KVM_REG_RISCV_FP_D);case KVM_REG_RISCV_ISA_EXT :return kvm_riscv_vcpu_get_reg_isa_ext (vcpu, reg); +case KVM_REG_RISCV_VECTOR : +return kvm_riscv_vcpu_get_reg_vector (vcpu, reg, +KVM_REG_RISCV_VECTOR );default :break ; }
@@ -888,6 +910,9 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_riscv_vcpu_host_fp_save (&vcpu->arch.host_context);kvm_riscv_vcpu_guest_fp_restore (&vcpu->arch.guest_context, vcpu->arch.isa); +kvm_riscv_vcpu_host_vector_save (&vcpu->arch.host_context); +kvm_riscv_vcpu_guest_vector_restore (&vcpu->arch.guest_context, + vcpu->arch.isa); vcpu->cpu = cpu; }
@@ -903,6 +928,11 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)kvm_riscv_vcpu_host_fp_restore (&vcpu->arch.host_context);kvm_riscv_vcpu_timer_save (vcpu); +kvm_riscv_vcpu_guest_vector_save (&vcpu->arch.guest_context, + vcpu->arch.isa); +kvm_riscv_vcpu_host_vector_restore (&vcpu->arch.host_context); + +csr_write (CSR_HGATP ,0 ); csr->vsstatus =csr_read (CSR_VSSTATUS ); csr->vsie =csr_read (CSR_VSIE );
arch/riscv/kvm/vcpu_vector.c new file +177 -0
177 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v13 18/19 riscv: kvm: redirect illegal instruction traps to guests
In reply to Andy ChiuRunning below m-mode, an illegal instruction trap where m-mode could not handle would be redirected back to s-mode. However, kvm running in hs-mode terminates the vs-mode software when it receive such exception code. Instead, it should redirect the trap back to vs-mode, and let vs-mode trap handler decide the next step.
Besides, hs-mode should run transparently to vs-mode. So terminating guest OS breaks assumption for the kernel running in vs-mode.
We use first-use trap to enable Vector for user space processes. This means that the user process running in u- or vu- mode will take an illegal instruction trap for the first time using V. Then the s- or vs- mode kernel would allocate V for the process. Thus, we must redirect the trap back to vs-mode in order to get the first-use trap working for guest OSes here.
--- arch/riscv/kvm/vcpu_exit.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
arch/riscv/kvm/vcpu_exit.c +15 -0
@@ -162,6 +162,16 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu, vcpu->arch.guest_context.sepc =csr_read (CSR_VSTVEC ); } +static int vcpu_trap_redirect_vs (struct kvm_vcpu *vcpu, +struct kvm_cpu_trap *trap) +{ +/* set up trap handler and trap info when it gets back to vs */ +kvm_riscv_vcpu_trap_redirect (vcpu, trap); +/* return to s-mode by setting vcpu's SPP */ + vcpu->arch.guest_context.sstatus |=SR_SPP ; +return 1 ; +} +/* * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on * proper exit to userspace.
@@ -179,6 +189,10 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, ret = -EFAULT ; run->exit_reason =KVM_EXIT_UNKNOWN ;switch (trap->scause) { +case EXC_INST_ILLEGAL : +if (vcpu->arch.guest_context.hstatus &HSTATUS_SPV ) + ret =vcpu_trap_redirect_vs (vcpu, trap); +break ;case EXC_VIRTUAL_INST_FAULT :if (vcpu->arch.guest_context.hstatus &HSTATUS_SPV ) ret =kvm_riscv_vcpu_virtual_insn (vcpu, run, trap);
@@ -206,6 +220,7 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, vcpu->arch.guest_context.hstatus);kvm_err ("SCAUSE=0x%lx STVAL=0x%lx HTVAL=0x%lx HTINST=0x%lx\n" , trap->scause, trap->stval, trap->htval, trap->htinst); + asmvolatile ("ebreak\n\t" ); }return ret;
Signature · 1 line
2.17.1
Re: [PATCH -next v13 18/19] riscv: kvm: redirect illegal instruction traps to guests
Quoted 10 lines · Andy Chiu
Running below m-mode, an illegal instruction trap where m-mode could not handle would be redirected back to s-mode. However, kvm running in hs-mode terminates the vs-mode software when it receive such exception code. Instead, it should redirect the trap back to vs-mode, and let vs-mode trap handler decide the next step.
Besides, hs-mode should run transparently to vs-mode. So terminating guest OS breaks assumption for the kernel running in vs-mode.
Quoted 6 lines
We use first-use trap to enable Vector for user space processes. This means that the user process running in u- or vu- mode will take an illegal instruction trap for the first time using V. Then the s- or vs- mode kernel would allocate V for the process. Thus, we must redirect the trap back to vs-mode in order to get the first-use trap working for guest OSes here.
In general, it is a good strategy to always redirect illegal instruction traps to VS-mode.
Quoted 11 lines
Signed-off-by Andy Chiu <[email protected]>--- arch/riscv/kvm/vcpu_exit.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)arch/riscv/kvm/vcpu_exit.c +0
-0
arch/riscv/kvm/vcpu_exit.c void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu, · line 171
Quoted 10 lines
arch/riscv/kvm/vcpu_exit.c +7
-0@@ -162,6 +162,16 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu, vcpu->arch.guest_context.sepc =csr_read (CSR_VSTVEC ); } +static int vcpu_trap_redirect_vs (struct kvm_vcpu *vcpu, +struct kvm_cpu_trap *trap) +{ +/* set up trap handler and trap info when it gets back to vs */ +kvm_riscv_vcpu_trap_redirect (vcpu, trap); +/* return to s-mode by setting vcpu's SPP */ + vcpu->arch.guest_context.sstatus |=SR_SPP ;
Setting sstatus.SPP needs to be done in kvm_riscv_vcpu_trap_redirect() because for guest all traps are always taken by VS-mode.
Quoted 18 lines
arch/riscv/kvm/vcpu_exit.c +7
-0+return 1 ; +} +/* * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on * proper exit to userspace. @@ -179,6 +189,10 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, ret = -EFAULT ; run->exit_reason =KVM_EXIT_UNKNOWN ;switch (trap->scause) { +case EXC_INST_ILLEGAL : +if (vcpu->arch.guest_context.hstatus &HSTATUS_SPV ) + ret =vcpu_trap_redirect_vs (vcpu, trap); +break ;case EXC_VIRTUAL_INST_FAULT :if (vcpu->arch.guest_context.hstatus &HSTATUS_SPV ) ret =kvm_riscv_vcpu_virtual_insn (vcpu, run, trap);
arch/riscv/kvm/vcpu_exit.c int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 223
Quoted 4 lines
arch/riscv/kvm/vcpu_exit.c +1
-0@@ -206,6 +220,7 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, vcpu->arch.guest_context.hstatus);kvm_err ("SCAUSE=0x%lx STVAL=0x%lx HTVAL=0x%lx HTINST=0x%lx\n" , trap->scause, trap->stval, trap->htval, trap->htinst); + asmvolatile ("ebreak\n\t" );
This is not a related change.
Quoted 11 lines
arch/riscv/kvm/vcpu_exit.c +0
-1}return ret; --2.17.1
_______________________________________________
linux-riscv mailing list
linux-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
Overall, this patch can be accepted independent of this series due to its usefulness.
I send a v2 of this patch separately.
Regards,
Anup
Re: [PATCH -next v13 18/19] riscv: kvm: redirect illegal instruction traps to guests
Quoted 36 lines · Anup Patel
Quoted 10 lines · Andy Chiu
Running below m-mode, an illegal instruction trap where m-mode could not handle would be redirected back to s-mode. However, kvm running in hs-mode terminates the vs-mode software when it receive such exception code. Instead, it should redirect the trap back to vs-mode, and let vs-mode trap handler decide the next step.
Besides, hs-mode should run transparently to vs-mode. So terminating guest OS breaks assumption for the kernel running in vs-mode.
Quoted 6 lines
We use first-use trap to enable Vector for user space processes. This means that the user process running in u- or vu- mode will take an illegal instruction trap for the first time using V. Then the s- or vs- mode kernel would allocate V for the process. Thus, we must redirect the trap back to vs-mode in order to get the first-use trap working for guest OSes here.
In general, it is a good strategy to always redirect illegal instruction traps to VS-mode.
Quoted 14 lines
Signed-off-by Andy Chiu <[email protected]>--- arch/riscv/kvm/vcpu_exit.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)arch/riscv/kvm/vcpu_exit.c +0
-0@@ -162,6 +162,16 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu, vcpu->arch.guest_context.sepc =csr_read (CSR_VSTVEC ); }
Quoted 10 lines
arch/riscv/kvm/vcpu_exit.c void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu, · line 171
Quoted 7 lines
arch/riscv/kvm/vcpu_exit.c +7
-0+static int vcpu_trap_redirect_vs (struct kvm_vcpu *vcpu, +struct kvm_cpu_trap *trap) +{ +/* set up trap handler and trap info when it gets back to vs */ +kvm_riscv_vcpu_trap_redirect (vcpu, trap); +/* return to s-mode by setting vcpu's SPP */ + vcpu->arch.guest_context.sstatus |=SR_SPP ;Setting sstatus.SPP needs to be done in
kvm_riscv_vcpu_trap_redirect()because for guest all traps are always taken by VS-mode.
NIce. Sorry that I didn't dig much into the kvm part so I thought it was left to VU-mode on purpose.
Quoted 17 lines
Quoted 17 lines
arch/riscv/kvm/vcpu_exit.c +7
-0+return 1 ; +} +/* * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on * proper exit to userspace. @@ -179,6 +189,10 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, ret = -EFAULT ; run->exit_reason =KVM_EXIT_UNKNOWN ;switch (trap->scause) { +case EXC_INST_ILLEGAL : +if (vcpu->arch.guest_context.hstatus &HSTATUS_SPV ) + ret =vcpu_trap_redirect_vs (vcpu, trap); +break ;case EXC_VIRTUAL_INST_FAULT :if (vcpu->arch.guest_context.hstatus &HSTATUS_SPV )
Quoted 9 lines
arch/riscv/kvm/vcpu_exit.c int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 223
Quoted 6 lines
arch/riscv/kvm/vcpu_exit.c +1
-0ret =kvm_riscv_vcpu_virtual_insn (vcpu, run, trap);@@ -206,6 +220,7 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, vcpu->arch.guest_context.hstatus);kvm_err ("SCAUSE=0x%lx STVAL=0x%lx HTVAL=0x%lx HTINST=0x%lx\n" , trap->scause, trap->stval, trap->htval, trap->htinst); + asmvolatile ("ebreak\n\t" );This is not a related change.
Oops, that was a mistake.
Quoted 12 lines
Quoted 11 lines
arch/riscv/kvm/vcpu_exit.c +0
-1}return ret; --2.17.1
_______________________________________________
linux-riscv mailing list
linux-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 4 lines
Overall, this patch can be accepted independent of this series due to its usefulness.
I send a v2 of this patch separately.
Thank you Anup. I will leave this patch there for the following revision of the vector patches.
Cheers,
Andy
PATCH v13 19/19 riscv: Enable Vector code to be built
In reply to Andy ChiuFrom: Guo Ren <[email protected]>
This patch adds a config which enables vector feature from the kernel space.
--- arch/riscv/Kconfig | 10 ++++++++++ arch/riscv/Makefile | 7 +++++++ 2 files changed, 17 insertions(+)
arch/riscv/Kconfig +10 -0
@@ -416,6 +416,16 @@ config RISCV_ISA_SVPBMT If you don't know what to do here, say Y. +config RISCV_ISA_V + bool "VECTOR extension support" + depends on GCC_VERSION >= 120000 || CLANG_VERSION >= 130000 + default n + help + Say N here if you want to disable all vector related procedure + in the kernel. + + If you don't know what to do here, say Y. + config TOOLCHAIN_HAS_ZICBOM bool default y
arch/riscv/Makefile +7 -0
@@ -52,6 +52,13 @@ riscv-march-$(CONFIG_ARCH_RV32I) := rv32ima riscv-march-$(CONFIG_ARCH_RV64I) := rv64ima riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)c +riscv-march-$(CONFIG_RISCV_ISA_V) := $(riscv-march-y)v + +ifeq ($(CONFIG_RISCV_ISA_V), y) +ifeq ($(CONFIG_CC_IS_CLANG), y) + riscv-march-y += -mno-implicit-float -menable-experimental-extensions +endif +endif # Newer binutils versions default to ISA spec version 20191213 which moves some # instructions from the I extension to the Zicsr and Zifencei extensions.
Signature · 1 line
2.17.1
Re: [PATCH -next v13 19/19] riscv: Enable Vector code to be built
Hey Andy,
Thanks for respinning this, I think a lot of people will be happy to see it!
Quoted 5 lines · Andy Chiu
arch/riscv/Makefile +0
-0
arch/riscv/Makefile riscv-march-$(CONFIG_ARCH_RV32I) := rv32ima · line 61
Quoted 10 lines
arch/riscv/Makefile +7
-0@@ -52,6 +52,13 @@ riscv-march-$(CONFIG_ARCH_RV32I) := rv32ima riscv-march-$(CONFIG_ARCH_RV64I) := rv64ima riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)c +riscv-march-$(CONFIG_RISCV_ISA_V) := $(riscv-march-y)v + +ifeq ($(CONFIG_RISCV_ISA_V), y) +ifeq ($(CONFIG_CC_IS_CLANG), y) + riscv-march-y += -mno-implicit-float -menable-experimental-extensions +endif +endif
Uh, so I don't think this was actually tested with (a recent version of) clang:
clang-15: error: unknown argument: '-menable-experimental-extensions_zicbom_zihintpause'
Firstly, no-implicit-float is a CFLAG, so why add it to march? There is an existing patch on the list for enabling this flag, but I recall Palmer saying that it was not actually needed? Palmer, do you remember why that was?
I dunno what enable-experimental-extensions is, but I can guess. Do we really want to enable vector for toolchains where the support is considered experimental? I'm not au fait with the details of clang versions nor versions of the Vector spec, so take the following with a bit of a pinch of salt...
Since you've allowed this to be built with anything later than clang 13, does that mean that different versions of clang may generate vector code that are not compatible?
I'm especially concerned by:
https://github.com/riscv/riscv-v-spec/releases/tag/0.9
which appears to be most recently released version of the spec, prior to clang/llvm 13 being released.
arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 421
Quoted 11 lines
arch/riscv/Kconfig +3
-0@@ -416,6 +416,16 @@ config RISCV_ISA_SVPBMT If you don't know what to do here, say Y. +config RISCV_ISA_V + bool "VECTOR extension support" + depends on GCC_VERSION >= 120000 || CLANG_VERSION >= 130000
Are these definitely the versions you want to support? What are the earliest (upstream) versions that support the frozen version of the vector spec?
Also, please copy what has been done with "TOOLCHAIN_HAS_FOO" for other
extensions and check this support with cc-option instead. Similarly,
you'll need to gate this support on the linker being capable of
accepting vector:
/stuff/toolchains/gcc-11/bin/riscv64-unknown-linux-gnu-ld: -march=rv64i2p0_m2p0_a2p0_f2p0_d2p0_c2p0_v1p0_zihintpause2p0_zve32f1p0_zve32x1p0_zve64d1p0_zve64f1p0_zve64x1p0_zvl128b1p0_zvl32b1p0_zvl64b1p0: prefixed ISA extension must separate with _ /stuff/toolchains/gcc-11/bin/riscv64-unknown-linux-gnu-ld: failed to merge target specific data of file arch/riscv/kernel/vdso/vgettimeofday.o
arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 422
Quoted 1 line
arch/riscv/Kconfig +1
-0+ default n
I forget, but is the reason for this being default n, when the others are default y a conscious choice?
I'm a bit of a goldfish sometimes memory wise, and I don't remember if that was an outcome of the previous discussions.
If it is intentionally different, that needs to be in the changelog IMO.
arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 429
Quoted 7 lines
arch/riscv/Kconfig +6
-0+ help + Say N here if you want to disable all vector related procedure + in the kernel. + + If you don't know what to do here, say Y. + config TOOLCHAIN_HAS_ZICBOM
^ you can use this one here as an example :)
I'll reply here again once the patchwork automation has given the series a once over and see if it comes up with any other build issues. Thanks,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230125/6467e834/attachment-0001.sig>
Re: [PATCH -next v13 19/19] riscv: Enable Vector code to be built
Quoted 33 lines · Conor Dooley
Hey Andy,
Thanks for respinning this, I think a lot of people will be happy to see it!
Quoted 8 lines · Andy Chiu
arch/riscv/Makefile +0
-0@@ -52,6 +52,13 @@ riscv-march-$(CONFIG_ARCH_RV32I) := rv32imariscv-march-$(CONFIG_ARCH_RV64I) := rv64ima
riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd
riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)carch/riscv/Makefile riscv-march-$(CONFIG_ARCH_RV32I) := rv32ima · line 61
Quoted 7 lines
arch/riscv/Makefile +7
-0+riscv-march-$(CONFIG_RISCV_ISA_V) := $(riscv-march-y)v + +ifeq ($(CONFIG_RISCV_ISA_V), y) +ifeq ($(CONFIG_CC_IS_CLANG), y) + riscv-march-y += -mno-implicit-float -menable-experimental-extensions +endif +endifUh, so I don't think this was actually tested with (a recent version of) clang:
clang-15: error: unknown argument: '-menable-experimental-extensions_zicbom_zihintpause'Firstly, no-implicit-float is a CFLAG, so why add it to march? There is an existing patch on the list for enabling this flag, but I recall Palmer saying that it was not actually needed? Palmer, do you remember why that was?
Quoted 12 lines
I dunno what enable-experimental-extensions is, but I can guess. Do we really want to enable vector for toolchains where the support is considered experimental? I'm not au fait with the details of clang versions nor versions of the Vector spec, so take the following with a bit of a pinch of salt...
Since you've allowed this to be built with anything later than clang 13, does that mean that different versions of clang may generate vector code that are not compatible?
I'm especially concerned by:
https://github.com/riscv/riscv-v-spec/releases/tag/0.9
which appears to be most recently released version of the spec, prior to clang/llvm 13 being released.
For implementations of unratified extensions you both have to enable them with -menable-experimental-extensions and have to explicitly specify the version in the -march string specifically so this isn?t a concern. Only once ratified can you use the unversioned extension, which is implicitly the ratified version (ignoring the whole i2p0 vs i2p1 fiasco).
But no, you probably don?t want experimental implementations, which can exist when the ratified version is implemented in theory (so there?s no compatibility concern based on ISA changes) but isn?t deemed production-ready (e.g. potential ABI instability in the case of something like V).
Jess
Re: [PATCH -next v13 19/19] riscv: Enable Vector code to be built
Quoted 41 lines · Jessica Clarke
Quoted 27 lines · Conor Dooley
Quoted 8 lines · Andy Chiu
arch/riscv/Makefile +0
-0@@ -52,6 +52,13 @@ riscv-march-$(CONFIG_ARCH_RV32I) := rv32imariscv-march-$(CONFIG_ARCH_RV64I) := rv64ima
riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd
riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)carch/riscv/Makefile riscv-march-$(CONFIG_ARCH_RV32I) := rv32ima · line 61
Quoted 7 lines
arch/riscv/Makefile +7
-0+riscv-march-$(CONFIG_RISCV_ISA_V) := $(riscv-march-y)v + +ifeq ($(CONFIG_RISCV_ISA_V), y) +ifeq ($(CONFIG_CC_IS_CLANG), y) + riscv-march-y += -mno-implicit-float -menable-experimental-extensions +endif +endifUh, so I don't think this was actually tested with (a recent version of) clang:
clang-15: error: unknown argument: '-menable-experimental-extensions_zicbom_zihintpause'Firstly, no-implicit-float is a CFLAG, so why add it to march? There is an existing patch on the list for enabling this flag, but I recall Palmer saying that it was not actually needed? Palmer, do you remember why that was?
Quoted 12 lines
I dunno what enable-experimental-extensions is, but I can guess. Do we really want to enable vector for toolchains where the support is considered experimental? I'm not au fait with the details of clang versions nor versions of the Vector spec, so take the following with a bit of a pinch of salt...
Since you've allowed this to be built with anything later than clang 13, does that mean that different versions of clang may generate vector code that are not compatible?
I'm especially concerned by:
https://github.com/riscv/riscv-v-spec/releases/tag/0.9
which appears to be most recently released version of the spec, prior to clang/llvm 13 being released.
Quoted 6 lines
For implementations of unratified extensions you both have to enable them with -menable-experimental-extensions and have to explicitly specify the version in the -march string specifically so this isn?t a concern. Only once ratified can you use the unversioned extension, which is implicitly the ratified version (ignoring the whole i2p0 vs i2p1 fiasco).
Ahh, thanks for the clarification Jess.
Quoted 5 lines
But no, you probably don?t want experimental implementations, which can exist when the ratified version is implemented in theory (so there?s no compatibility concern based on ISA changes) but isn?t deemed production-ready (e.g. potential ABI instability in the case of something like V).
And I guess, if you turn it on for one, it's on for all. While the vector extension might be okay in that regard, another extension well not be okay to use the "unversioned" experimental version of. Sounds like removing that option and picking the version of clang that adds the actual implementation is a better approach, at least IMO.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230125/a31ba3da/attachment-0001.sig>
Re: [PATCH -next v13 19/19] riscv: Enable Vector code to be built
In reply to Conor DooleyQuoted 9 lines · Conor Dooley
Uh, so I don't think this was actually tested with (a recent version of) clang:
clang-15: error: unknown argument: '-menable-experimental-extensions_zicbom_zihintpause'Firstly, no-implicit-float is a CFLAG, so why add it to march? There is an existing patch on the list for enabling this flag, but I recall Palmer saying that it was not actually needed? Palmer, do you remember why that was?
Quoted 8 lines
I dunno what enable-experimental-extensions is, but I can guess. Do we really want to enable vector for toolchains where the support is considered experimental? I'm not au fait with the details of clang versions nor versions of the Vector spec, so take the following with a bit of a pinch of salt...
Since you've allowed this to be built with anything later than clang 13, does that mean that different versions of clang may generate vector code that are not compatible?
Thanks for pointing this out. We found that Vector in clang13 was still an experimental feature. And the first version of clang which lists Vector v1.0 (ratified) as a standard support is clang14. Thus, we will require the minimum clang toolchain version to be 14 in the following revision.
Quoted 13 lines
I'm especially concerned by:
https://github.com/riscv/riscv-v-spec/releases/tag/0.9
which appears to be most recently released version of the spec, prior to clang/llvm 13 being released.Quoted 8 lines
arch/riscv/Kconfig +0
-0@@ -416,6 +416,16 @@ config RISCV_ISA_SVPBMT If you don't know what to do here, say Y.
Quoted 7 lines
arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 421
Quoted 3 lines
arch/riscv/Kconfig +3
-0+config RISCV_ISA_V + bool "VECTOR extension support" + depends on GCC_VERSION >= 120000 || CLANG_VERSION >= 130000Are these definitely the versions you want to support? What are the earliest (upstream) versions that support the frozen version of the vector spec?
It is 14 for clang and 2.38 for GNU binutils
Quoted 3 lines
Also, please copy what has been done with "
TOOLCHAIN_HAS_FOO" for other extensions and check this support with cc-option instead. Similarly,
Yes, updating it.
Quoted 9 lines
you'll need to gate this support on the linker being capable of
accepting vector:
/stuff/toolchains/gcc-11/bin/riscv64-unknown-linux-gnu-ld: -march=rv64i2p0_m2p0_a2p0_f2p0_d2p0_c2p0_v1p0_zihintpause2p0_zve32f1p0_zve32x1p0_zve64d1p0_zve64f1p0_zve64x1p0_zvl128b1p0_zvl32b1p0_zvl64b1p0: prefixed ISA extension must separate with _ /stuff/toolchains/gcc-11/bin/riscv64-unknown-linux-gnu-ld: failed to merge target specific data of filearch/riscv/kernel/vdso/vgettimeofday.oarch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 422
Quoted 1 line
arch/riscv/Kconfig +1
-0+ default nI forget, but is the reason for this being default n, when the others are default y a conscious choice?
Yes, I think it could be y if V is allocated in the first-use trap, as far as I'm concerned. Hey Vineet, do you have any comments about that?
Quoted 4 lines
I'm a bit of a goldfish sometimes memory wise, and I don't remember if that was an outcome of the previous discussions.
If it is intentionally different, that needs to be in the changelog IMO.
Quoted 9 lines
arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 429
Quoted 7 lines
arch/riscv/Kconfig +6
-0+ help + Say N here if you want to disable all vector related procedure + in the kernel. + + If you don't know what to do here, say Y. + config TOOLCHAIN_HAS_ZICBOM^ you can use this one here as an example :)
Ok! Thanks
Quoted 6 lines
I'll reply here again once the patchwork automation has given the series a once over and see if it comes up with any other build issues. Thanks,
Conor.
Thanks,
Andy
Re: [PATCH -next v13 19/19] riscv: Enable Vector code to be built
Quoted 5 lines · Andy Chiu
Quoted 3 lines
Quoted 1 line
+ default n
I forget, but is the reason for this being default n, when the others are default y a conscious choice?
Yes, I think it could be y if V is allocated in the first-use trap, as far as I'm concerned. Hey Vineet, do you have any comments about that?
Yes I think this can be enabled by default now that everything is allocated on demand.
FWIW thread_struct would have 5 word overhead due to struct __riscv_v_state but nothing I would worry about too much.
-Vineet
Re: [PATCH -next v13 19/19] riscv: Enable Vector code to be built
In reply to Conor DooleyQuoted 1 line · Conor Dooley
Firstly, no-implicit-float is a CFLAG, so why add it to march?
I placed it in march because I thought we need the flag in vdso. And, KBUILD_CFLAGS is not enough for vdso. However, I think we don't need this flag in vdso since it is run in user space anyway.
Quoted 2 lines
There is an existing patch on the list for enabling this flag, but I recall Palmer saying that it was not actually needed?
The flag is needed for clang builds to prevent auto-vectorization from using V in the kernel code [1].
Quoted 1 line
Palmer, do you remember why that was?
The discussion[2] suggested that we need this flag, IIUC. But somehow the patch did make it into the tree.
Quoted 1 line
[1]https://lore.kernel.org/all/CAOnJCULtT-y9vo6YhW7bW9XyKRdod-hvFfr02jHVamR_LcsKdA at mail.gmail.com/ [2]https://lore.kernel.org/all/20221216185012.2342675-1-abdulras at google.com/
Re: [PATCH -next v13 19/19] riscv: Enable Vector code to be built
Quoted 5 lines · Andy Chiu
Quoted 1 line · Conor Dooley
Firstly, no-implicit-float is a CFLAG, so why add it to march?
I placed it in march because I thought we need the flag in vdso. And,
KBUILD_CFLAGSis not enough for vdso. However, I think we don't need this flag in vdso since it is run in user space anyway.
Quoted 8 lines
Quoted 2 lines
There is an existing patch on the list for enabling this flag, but I recall Palmer saying that it was not actually needed?
The flag is needed for clang builds to prevent auto-vectorization from using V in the kernel code [1].
Quoted 1 line
Palmer, do you remember why that was?
The discussion[2] suggested that we need this flag, IIUC. But somehow the patch did make it into the tree.
I know, in [1] I left an R-b as the patch seemed reasonable to me. Palmer mentioned some reason for not thinking it was actually needed but not on-list, so I was hoping he'd comment!
And I suppose, it never got any further attention as it isn't needed by any in-tree code?
Quoted 2 lines
[1]https://lore.kernel.org/all/CAOnJCULtT-y9vo6YhW7bW9XyKRdod-hvFfr02jHVamR_LcsKdA at mail.gmail.com/ [2]https://lore.kernel.org/all/20221216185012.2342675-1-abdulras at google.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230130/c4808e56/attachment-0001.sig>
Re: [PATCH -next v13 19/19] riscv: Enable Vector code to be built
Hey Andy,
Quoted 11 lines · Conor Dooley
Quoted 10 lines · Andy Chiu
Quoted 1 line · Conor Dooley
Firstly, no-implicit-float is a CFLAG, so why add it to march?
I placed it in march because I thought we need the flag in vdso. And,
KBUILD_CFLAGSis not enough for vdso. However, I think we don't need this flag in vdso since it is run in user space anyway.Quoted 2 lines
There is an existing patch on the list for enabling this flag, but I recall Palmer saying that it was not actually needed?
The flag is needed for clang builds to prevent auto-vectorization from using V in the kernel code [1].
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Palmer, do you remember why that was?
The discussion[2] suggested that we need this flag, IIUC. But somehow the patch did make it into the tree.
I know, in [1] I left an R-b as the patch seemed reasonable to me. Palmer mentioned some reason for not thinking it was actually needed but not on-list, so I was hoping he'd comment!
Palmer replied there today with his rationale & an expectation that we
do the same thing for vector as we did for float:
https://lore.kernel.org/linux-riscv/mhng-4c71ada6-003c-414f-9a74-efa3ccd2856b at palmer-ri-x1c9/T/#m366779709bbcf7672b5277b3bb27a7d6ce6c6115
Quoted 6 lines
And I suppose, it never got any further attention as it isn't needed by any in-tree code?
Quoted 2 lines
[1]https://lore.kernel.org/all/CAOnJCULtT-y9vo6YhW7bW9XyKRdod-hvFfr02jHVamR_LcsKdA at mail.gmail.com/ [2]https://lore.kernel.org/all/20221216185012.2342675-1-abdulras at google.com/
Cheers,
Conor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/kvm-riscv/attachments/20230208/1a991ce6/attachment-0001.sig>