Prctl to enable vector commands, previous vector patches rebased
Subsystems (4)
cover Prctl to enable vector commands, previous vector patches rebased
This patch adds a prctl to enable, disable, or query whether vectors are enabled or not. This is to allow a process to "opt out" of the overhead incurred by using vectors. Because this is build on top of an existing set of patches to work with vectors, they have been rebased to Linux 6.0-rc1.
Chris Stillson (1):
riscv: prctl to enable vector commands
Greentime Hu (9):
riscv: Add new csr defines related to vector extension
riscv: Add has_vector/riscv_vsize to save vector features.
riscv: Add vector struct and assembler definitions
riscv: Add task switch support for vector
riscv: Add ptrace vector support
riscv: Add sigcontext save/restore for vector
riscv: Add support for kernel mode vector
riscv: Add vector extension XOR implementation
riscv: Fix a kernel panic issue if $s2 is set to a specific value
before entering Linux
Guo Ren (4):
riscv: Rename __switch_to_aux -> fpu
riscv: Extending cpufeature.c to detect V-extension
riscv: Add vector feature to compile
riscv: Reset vector register
Vincent Chen (3):
riscv: signal: Report signal frame size to userspace via auxv
riscv: Add V extension to KVM ISA allow list
riscv: KVM: Add vector lazy save/restore support
arch/riscv/Kconfig | 15 +- arch/riscv/Makefile | 1 + arch/riscv/configs/defconfig | 6 + arch/riscv/include/asm/csr.h | 16 ++- arch/riscv/include/asm/elf.h | 47 +++--- arch/riscv/include/asm/hwcap.h | 1 + arch/riscv/include/asm/kvm_host.h | 2 + arch/riscv/include/asm/kvm_vcpu_vector.h | 65 +++++++++ arch/riscv/include/asm/processor.h | 9 ++ arch/riscv/include/asm/switch_to.h | 83 ++++++++++- arch/riscv/include/asm/vector.h | 17 +++ arch/riscv/include/asm/xor.h | 82 +++++++++++ arch/riscv/include/uapi/asm/auxvec.h | 1 + arch/riscv/include/uapi/asm/hwcap.h | 1 + arch/riscv/include/uapi/asm/kvm.h | 7 + arch/riscv/include/uapi/asm/ptrace.h | 23 +++ arch/riscv/include/uapi/asm/sigcontext.h | 24 ++++ arch/riscv/kernel/Makefile | 2 + arch/riscv/kernel/asm-offsets.c | 15 ++ arch/riscv/kernel/cpufeature.c | 21 +++ arch/riscv/kernel/entry.S | 6 +- arch/riscv/kernel/head.S | 37 ++++- arch/riscv/kernel/kernel_mode_vector.c | 132 +++++++++++++++++ arch/riscv/kernel/process.c | 61 ++++++++ arch/riscv/kernel/ptrace.c | 71 ++++++++++ arch/riscv/kernel/riscv_ksyms.c | 6 + arch/riscv/kernel/signal.c | 173 ++++++++++++++++++++++- arch/riscv/kernel/vector.S | 102 +++++++++++++ arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu.c | 32 +++++ arch/riscv/kvm/vcpu_switch.S | 69 +++++++++ arch/riscv/kvm/vcpu_vector.c | 173 +++++++++++++++++++++++ arch/riscv/lib/Makefile | 1 + arch/riscv/lib/xor.S | 81 +++++++++++ include/uapi/linux/elf.h | 1 + include/uapi/linux/prctl.h | 6 + kernel/sys.c | 7 + 37 files changed, 1355 insertions(+), 42 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/include/asm/xor.h create mode 100644 arch/riscv/kernel/kernel_mode_vector.c create mode 100644 arch/riscv/kernel/vector.S create mode 100644 arch/riscv/kvm/vcpu_vector.c create mode 100644 arch/riscv/lib/xor.S
Signature · 1 line
2.25.1
PATCH 1/17 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.25.1
PATCH 2/17 riscv: Extending cpufeature.c to detect V-extension
In reply to Chris StillsonFrom: Guo Ren <[email protected]>
Current cpufeature.c doesn't support detecting V-extension, because "rv64" also contain a 'v' letter and we need to skip it.
--- arch/riscv/include/uapi/asm/hwcap.h | 1 + arch/riscv/kernel/cpufeature.c | 1 + 2 files changed, 2 insertions(+)
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 +1 -0
@@ -83,6 +83,7 @@ void __init riscv_fill_hwcap(void) isa2hwcap['f' ] = isa2hwcap['F' ] =COMPAT_HWCAP_ISA_F ; isa2hwcap['d' ] = isa2hwcap['D' ] =COMPAT_HWCAP_ISA_D ; isa2hwcap['c' ] = isa2hwcap['C' ] =COMPAT_HWCAP_ISA_C ; + isa2hwcap['v' ] = isa2hwcap['V' ] =COMPAT_HWCAP_ISA_V ; elf_hwcap =0 ;
Signature · 1 line
2.25.1
PATCH 2/17 riscv: Extending cpufeature.c to detect V-extension
On Wed, Sep 21, 2022 at 12:46:14PM -0700, Chris Stillson wrote:
Hey Chris,
Looks like this only partially made it to the list. The cover letter is missing as are a bunch of the patches in the middle, see:
https://lore.kernel.org/linux-riscv/20220921194629.1480202-3-stillson at rivosinc.com/T/#t
Also, when you run format-patch to generate the patches, please add the -v flag with a number so that the patchset gets the version number added to it.
Ohh, and Palmer's non-rivos email looks wrong?
Thanks,
Conor.
PATCH 2/17 riscv: Extending cpufeature.c to detect V-extension
Quoted 7 lines · Conor Dooley
On Wed, Sep 21, 2022 at 12:46:14PM -0700, Chris Stillson wrote:
Hey Chris,
Looks like this only partially made it to the list. The cover letter is missing as are a bunch of the patches in the middle, see:
https://lore.kernel.org/linux-riscv/20220921194629.1480202-3-stillson at rivosinc.com/T/#t
nvm, rest of it seems to have finally made it through now.
Quoted 5 lines
Also, when you run format-patch to generate the patches, please add the
-vflag with a number so that the patchset gets the version number added to it.Ohh, and Palmer's non-rivos email looks wrong?
Rest still applies though & rerolling as v12 would be nice so that the tools don't get confused about which version is which since you've now sent a pair of v1s.
Your SoB is still missing from the individual patches too.
Getting there though, this one is at least threaded & the patches are not whitespace damaged/line wrapped anymore AFAICT.
Thanks,
Conor.
PATCH 3/17 riscv: Add new csr defines related to vector extension
In reply to Chris StillsonFrom: Greentime Hu <[email protected]>
Follow the riscv vector spec to add new csr numbers.
--- arch/riscv/include/asm/csr.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
arch/riscv/include/asm/csr.h +14 -2
@@ -24,6 +24,12 @@#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)
@@ -31,9 +37,9 @@#define SR_XS_DIRTY _AC(0x00018000, UL)#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 +303,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.25.1
PATCH 4/17 riscv: Add vector feature to compile
In reply to Chris StillsonFrom: Guo Ren <[email protected]>
This patch adds a new config option which could enable assembler's vector feature.
--- arch/riscv/Kconfig | 15 +++++++++++++-- arch/riscv/Makefile | 1 + 2 files changed, 14 insertions(+), 2 deletions(-)
arch/riscv/Kconfig +13 -2
@@ -432,7 +432,17 @@ config FPU If you don't know what to do here, say Y. -endmenu # "Platform type" +config VECTOR + bool "VECTOR 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. + +endmenu menu "Kernel features"
@@ -556,6 +566,7 @@ config CMDLINE_EXTEND cases where the provided arguments are insufficient and you don't want to or cannot modify them. + config CMDLINE_FORCE bool "Always use the default kernel command string" help
@@ -648,7 +659,7 @@ config XIP_PHYS_ADDR be linked for and stored to. This address is dependent on your own flash usage. -endmenu # "Boot options" +endmenu config BUILTIN_DTB bool
arch/riscv/Makefile +1 -0
@@ -50,6 +50,7 @@ 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_VECTOR) := $(riscv-march-y)v # 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.25.1
PATCH 5/17 riscv: Add has_vector/riscv_vsize to save vector features.
In reply to Chris StillsonFrom: Greentime Hu <[email protected]>
This patch is used to detect vector support status of CPU and use riscv_vsize to save the size of all the vector registers. It assumes all harts has the same capabilities in SMP system.
--- arch/riscv/include/asm/vector.h | 14 +++++ arch/riscv/kernel/cpufeature.c | 19 +++++++ arch/riscv/kernel/riscv_ksyms.c | 6 +++ arch/riscv/kernel/vector.S | 93 +++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 arch/riscv/include/asm/vector.h create mode 100644 arch/riscv/kernel/vector.S
arch/riscv/include/asm/vector.h new file +14 -0
@@ -0,0 +1,14 @@ +/* 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> + +void rvv_enable (void ); +void rvv_disable (void ); + +#endif /* ! __ASM_RISCV_VECTOR_H */
arch/riscv/kernel/cpufeature.c +19 -0
@@ -30,6 +30,14 @@ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;__ro_after_init DEFINE_STATIC_KEY_ARRAY_FALSE (riscv_isa_ext_keys,RISCV_ISA_EXT_KEY_MAX );EXPORT_SYMBOL (riscv_isa_ext_keys); +#ifdef CONFIG_FPU +__ro_after_init DEFINE_STATIC_KEY_FALSE (cpu_hwcap_fpu); +#endif +#ifdef CONFIG_VECTOR +#include <asm/vector.h> +__ro_after_init DEFINE_STATIC_KEY_FALSE (cpu_hwcap_vector); +unsigned long riscv_vsize__read_mostly ; +#endif /** * riscv_isa_extension_base() - Get base extension word
@@ -249,6 +257,16 @@ void __init riscv_fill_hwcap(void)if (j >=0 )static_branch_enable (&riscv_isa_ext_keys[j]); } + +#ifdef CONFIG_VECTOR +if (elf_hwcap &COMPAT_HWCAP_ISA_V ) { +static_branch_enable (&cpu_hwcap_vector); +/* There are 32 vector registers with vlenb length. */ +rvv_enable (); + riscv_vsize =csr_read (CSR_VLENB ) *32 ; +rvv_disable (); + } +#endif }#ifdef CONFIG_RISCV_ALTERNATIVE
@@ -328,3 +346,4 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, } }#endif +}
arch/riscv/kernel/riscv_ksyms.c +6 -0
@@ -15,3 +15,9 @@ EXPORT_SYMBOL(memmove);EXPORT_SYMBOL (__memset);EXPORT_SYMBOL (__memcpy);EXPORT_SYMBOL (__memmove); + +#ifdef CONFIG_VECTOR +#include <asm/vector.h> +EXPORT_SYMBOL (rvv_enable); +EXPORT_SYMBOL (rvv_disable); +#endif
arch/riscv/kernel/vector.S new file +93 -0
93 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 6/17 riscv: Reset vector register
In reply to Chris StillsonFrom: Guo Ren <[email protected]>
Reset vector registers at boot-time and disable vector instructions execution for kernel mode.
--- arch/riscv/kernel/entry.S | 6 +++--- arch/riscv/kernel/head.S | 35 +++++++++++++++++++++++++++++------ 2 files changed, 32 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 | SR_VS REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0
arch/riscv/kernel/head.S +29 -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 | SR_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 | SR_VS csrc CSR_STATUS, t0 #ifdef CONFIG_RISCV_BOOT_SPINWAIT
@@ -431,6 +431,29 @@ ENTRY(reset_regs) csrw fcsr, 0 /* note that the caller must clear SR_FS */ #endif /* CONFIG_FPU */ + +#ifdef CONFIG_VECTOR + 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_VECTOR */ + .Lreset_regs_done: ret END(reset_regs)
Signature · 1 line
2.25.1
PATCH 7/17 riscv: Add vector struct and assembler definitions
In reply to Chris StillsonFrom: Greentime Hu <[email protected]>
Add vector state context struct in struct thread and asm-offsets.c definitions.
The vector registers will be saved in datap pointer of __riscv_v_state. It will be dynamically allocated in kernel space. It will be put right after the __riscv_v_state data structure in user space.
--- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/uapi/asm/ptrace.h | 17 +++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 6 ++++++ 3 files changed, 24 insertions(+)
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/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 */
arch/riscv/kernel/asm-offsets.c +6 -0
@@ -75,6 +75,12 @@ void asm_offsets(void)OFFSET (TSK_STACK_CANARY , task_struct, stack_canary);#endif +OFFSET (RISCV_V_STATE_VSTART , __riscv_v_state, vstart); +OFFSET (RISCV_V_STATE_VL , __riscv_v_state, vl); +OFFSET (RISCV_V_STATE_VTYPE , __riscv_v_state, vtype); +OFFSET (RISCV_V_STATE_VCSR , __riscv_v_state, vcsr); +OFFSET (RISCV_V_STATE_DATAP , __riscv_v_state, datap); +DEFINE (PT_SIZE ,sizeof (struct pt_regs ));OFFSET (PT_EPC , pt_regs, epc);OFFSET (PT_RA , pt_regs, ra);
Signature · 1 line
2.25.1
PATCH 8/17 riscv: Add task switch support for vector
In reply to Chris StillsonFrom: Greentime Hu <[email protected]>
This patch adds task switch support for vector. It supports partial lazy save and restore mechanism. 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]
--- arch/riscv/include/asm/switch_to.h | 66 ++++++++++++++++++++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/process.c | 43 +++++++++++++++++++ 3 files changed, 110 insertions(+)
arch/riscv/include/asm/switch_to.h +66 -0
@@ -7,11 +7,13 @@#define _ASM_RISCV_SWITCH_TO_H#include <linux/jump_label.h> +#include <linux/slab.h> #include <linux/sched/task_stack.h> #include <asm/hwcap.h> #include <asm/processor.h> #include <asm/ptrace.h> #include <asm/csr.h> +#include <asm/asm-offsets.h> #ifdef CONFIG_FPU extern void __fstate_save (struct task_struct *save_to);
@@ -68,6 +70,68 @@ static __always_inline bool has_fpu(void) { return false; }#define __switch_to_fpu (__prev, __next) do { } while (0)#endif +#ifdef CONFIG_VECTOR +extern struct static_key_false cpu_hwcap_vector; +static __always_inline bool has_vector(void) +{ +return static_branch_likely (&cpu_hwcap_vector); +} +extern unsigned long riscv_vsize; +extern void __vstate_save (struct __riscv_v_state *save_to,void *datap); +extern void __vstate_restore (struct __riscv_v_state *restore_from,void *datap); + +static inline void __vstate_clean (struct pt_regs *regs) +{ + regs->status = (regs->status & ~(SR_VS )) |SR_VS_CLEAN ; +} + +static inline void vstate_off (struct task_struct *task, +struct pt_regs *regs) +{ + regs->status = (regs->status & ~SR_VS ) |SR_VS_OFF ; +} + +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); + } +} + +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 +static __always_inline bool has_vector(void) { return false; } +#define riscv_vsize (0) +#define vstate_save (task, regs) do { } while (0) +#define vstate_restore (task, regs) do { } while (0) +#define __switch_to_vector (__prev, __next) do { } while (0) +#endif +extern struct task_struct *__switch_to (struct task_struct *,struct task_struct *);
@@ -77,6 +141,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/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_VECTOR) += vector.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += cpu_ops.o
arch/riscv/kernel/process.c +43 -0
71 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 9/17 riscv: Add ptrace vector support
In reply to Chris StillsonFrom: 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 | 6 +++ arch/riscv/kernel/ptrace.c | 71 ++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 78 insertions(+)
arch/riscv/include/uapi/asm/ptrace.h +6 -0
@@ -94,6 +94,12 @@ 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
89 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.25.1
PATCH 10/17 riscv: Add sigcontext save/restore for vector
In reply to Chris StillsonFrom: Greentime Hu <[email protected]>
This patch adds sigcontext save/restore for vector. The vector registers will be saved in datap pointer. The datap pointer will be allocated dynamically when the task needs in kernel space. The datap pointer will be set right after the __riscv_v_state data structure to save all the vector registers in the signal handler stack.
--- arch/riscv/include/uapi/asm/sigcontext.h | 24 ++++ arch/riscv/kernel/asm-offsets.c | 2 + arch/riscv/kernel/signal.c | 165 ++++++++++++++++++++++- 3 files changed, 187 insertions(+), 4 deletions(-)
arch/riscv/include/uapi/asm/sigcontext.h +24 -0
@@ -8,6 +8,23 @@#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 __riscv_ctx_hdr { +__u32 magic; +__u32 size; +}; + +struct __sc_riscv_v_state { +struct __riscv_ctx_hdr head; +struct __riscv_v_state v_state; +} __attribute__((aligned (16 ))); +/* * Signal context structure *
@@ -17,6 +34,13 @@struct sigcontext {struct user_regs_struct sc_regs;union __riscv_fp_state sc_fpregs; +/* +* 4K + 128 reserved for vector state and future expansion. +* This space is enough to store the vector context whose VLENB +* is less or equal to 128. +* (The size of the vector context is 4144 byte as VLENB is 128) +*/ +__u8 __reserved[4224 ] __attribute__((__aligned__ (16 ))); };#endif /* _UAPI_ASM_RISCV_SIGCONTEXT_H */
arch/riscv/kernel/asm-offsets.c +2 -0
@@ -75,6 +75,8 @@ void asm_offsets(void)OFFSET (TSK_STACK_CANARY , task_struct, stack_canary);#endif +OFFSET (RISCV_V_STATE_MAGIC , __riscv_ctx_hdr, magic); +OFFSET (RISCV_V_STATE_SIZE , __riscv_ctx_hdr, size);OFFSET (RISCV_V_STATE_VSTART , __riscv_v_state, vstart);OFFSET (RISCV_V_STATE_VL , __riscv_v_state, vl);OFFSET (RISCV_V_STATE_VTYPE , __riscv_v_state, vtype);
arch/riscv/kernel/signal.c +161 -4
228 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 11/17 riscv: signal: Report signal frame size to userspace via auxv
In reply to Chris StillsonFrom: 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.
--- arch/riscv/include/asm/elf.h | 47 +++++++++++++++------------- arch/riscv/include/asm/processor.h | 2 ++ arch/riscv/include/uapi/asm/auxvec.h | 1 + arch/riscv/kernel/signal.c | 8 +++++ 4 files changed, 36 insertions(+), 22 deletions(-)
arch/riscv/include/asm/elf.h +25 -22
@@ -77,28 +77,31 @@ extern unsigned long elf_hwcap;#define COMPAT_ELF_PLATFORM (NULL)#ifdef CONFIG_MMU -#define ARCH_DLINFO \ -do { \ -/* \ -* Note that we add ulong after elf_addr_t because \ -* casting current->mm->context.vdso triggers a cast \ -* warning of cast from pointer to integer for \ -* COMPAT ELFCLASS32. \ -*/ \ -NEW_AUX_ENT (AT_SYSINFO_EHDR , \ - (elf_addr_t )(ulong )current->mm->context.vdso); \ -NEW_AUX_ENT (AT_L1I_CACHESIZE , \ -get_cache_size (1 ,CACHE_TYPE_INST )); \ -NEW_AUX_ENT (AT_L1I_CACHEGEOMETRY , \ -get_cache_geometry (1 ,CACHE_TYPE_INST )); \ -NEW_AUX_ENT (AT_L1D_CACHESIZE , \ -get_cache_size (1 ,CACHE_TYPE_DATA )); \ -NEW_AUX_ENT (AT_L1D_CACHEGEOMETRY , \ -get_cache_geometry (1 ,CACHE_TYPE_DATA )); \ -NEW_AUX_ENT (AT_L2_CACHESIZE , \ -get_cache_size (2 ,CACHE_TYPE_UNIFIED )); \ -NEW_AUX_ENT (AT_L2_CACHEGEOMETRY , \ -get_cache_geometry (2 ,CACHE_TYPE_UNIFIED )); \ +#define ARCH_DLINFO \ +do { \ + NEW_AUX_ENT(AT_SYSINFO_EHDR, \ + (elf_addr_t)current->mm->context.vdso); \ + NEW_AUX_ENT(AT_L1I_CACHESIZE, \ + get_cache_size(1, CACHE_TYPE_INST)); \ + NEW_AUX_ENT(AT_L1I_CACHEGEOMETRY, \ + get_cache_geometry(1, CACHE_TYPE_INST)); \ + NEW_AUX_ENT(AT_L1D_CACHESIZE, \ + get_cache_size(1, CACHE_TYPE_DATA)); \ + NEW_AUX_ENT(AT_L1D_CACHEGEOMETRY, \ + get_cache_geometry(1, CACHE_TYPE_DATA)); \ + NEW_AUX_ENT(AT_L2_CACHESIZE, \ + get_cache_size(2, CACHE_TYPE_UNIFIED)); \ + NEW_AUX_ENT(AT_L2_CACHEGEOMETRY, \ + get_cache_geometry(2, 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>
@@ -86,6 +87,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
@@ -33,5 +33,6 @@/* entries in ARCH_DLINFO */ #define AT_VECTOR_SIZE_ARCH 7 +#define AT_MINSIGSTKSZ 51#endif /* _UAPI_ASM_RISCV_AUXVEC_H */
arch/riscv/kernel/signal.c +8 -0
@@ -478,8 +478,16 @@ asmlinkage __visible void do_notify_resume(struct pt_regs *regs,resume_user_mode_work (regs); } +unsigned long __ro_after_init signal_minsigstksz; +void init_rt_signal_env (void );void __init init_rt_signal_env (void ) { rvv_sc_size =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 (); }
Signature · 1 line
2.25.1
PATCH 12/17 riscv: Add support for kernel mode vector
In reply to Chris StillsonFrom: Greentime Hu <[email protected]>
Add kernel_rvv_begin() and kernel_rvv_end() function declarations and corresponding definitions in kernel_mode_vector.c
These are needed to wrap uses of vector in kernel mode.
--- arch/riscv/include/asm/vector.h | 3 + arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/kernel_mode_vector.c | 132 +++++++++++++++++++++++++ arch/riscv/kernel/vector.S | 9 ++ 4 files changed, 145 insertions(+) create mode 100644 arch/riscv/kernel/kernel_mode_vector.c
arch/riscv/include/asm/vector.h +3 -0
@@ -10,5 +10,8 @@void rvv_enable (void );void rvv_disable (void ); +void kernel_rvv_begin (void ); +void kernel_rvv_end (void ); +void vector_flush_cpu_state (void );#endif /* ! __ASM_RISCV_VECTOR_H */
arch/riscv/kernel/Makefile +1 -0
@@ -56,6 +56,7 @@ obj-$(CONFIG_MMU) += vdso.o vdso/ obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_VECTOR) += vector.o +obj-$(CONFIG_VECTOR) += kernel_mode_vector.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += cpu_ops.o
arch/riscv/kernel/kernel_mode_vector.c new file +132 -0
132 lines not on this page · raw message
arch/riscv/kernel/vector.S +9 -0
@@ -91,3 +91,12 @@ ENTRY(rvv_disable) csrc CSR_STATUS, status ret ENDPROC(rvv_disable) + +ENTRY(vector_flush_cpu_state) + vsetvli t0, 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 + ret +ENDPROC(vector_flush_cpu_state)
Signature · 1 line
2.25.1
PATCH 13/17 riscv: Add vector extension XOR implementation
In reply to Chris StillsonFrom: Greentime Hu <[email protected]>
This patch adds support for vector optimized XOR and it is tested in qemu.
--- arch/riscv/include/asm/xor.h | 82 ++++++++++++++++++++++++++++++++++++ arch/riscv/lib/Makefile | 1 + arch/riscv/lib/xor.S | 81 +++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 arch/riscv/include/asm/xor.h create mode 100644 arch/riscv/lib/xor.S
arch/riscv/include/asm/xor.h new file +82 -0
@@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2021 SiFive +*/ + +#include <linux/hardirq.h> +#include <asm-generic/xor.h> +#ifdef CONFIG_VECTOR +#include <asm/vector.h> +#include <asm/switch_to.h> + +void xor_regs_2_ (unsigned long bytes,unsigned long * __restrict p1, +const unsigned long * __restrict p2); +void xor_regs_3_ (unsigned long bytes,unsigned long * __restrict p1, +const unsigned long * __restrict p2, +const unsigned long * __restrict p3); +void xor_regs_4_ (unsigned long bytes,unsigned long * __restrict p1, +const unsigned long * __restrict p2, +const unsigned long * __restrict p3, +const unsigned long * __restrict p4); +void xor_regs_5_ (unsigned long bytes,unsigned long * __restrict p1, +const unsigned long * __restrict p2, +const unsigned long * __restrict p3, +const unsigned long * __restrict p4, +const unsigned long * __restrict p5); + +static void xor_rvv_2 (unsigned long bytes,unsigned long * __restrict p1, +const unsigned long * __restrict p2) +{ +kernel_rvv_begin (); +xor_regs_2_ (bytes, p1, p2); +kernel_rvv_end (); +} + +static void xor_rvv_3 (unsigned long bytes,unsigned long * __restrict p1, +const unsigned long * __restrict p2, +const unsigned long * __restrict p3) +{ +kernel_rvv_begin (); +xor_regs_3_ (bytes, p1, p2, p3); +kernel_rvv_end (); +} + +static void xor_rvv_4 (unsigned long bytes,unsigned long * __restrict p1, +const unsigned long * __restrict p2, +const unsigned long * __restrict p3, +const unsigned long * __restrict p4) +{ +kernel_rvv_begin (); +xor_regs_4_ (bytes, p1, p2, p3, p4); +kernel_rvv_end (); +} + +static void xor_rvv_5 (unsigned long bytes,unsigned long * __restrict p1, +const unsigned long * __restrict p2, +const unsigned long * __restrict p3, +const unsigned long * __restrict p4, +const unsigned long * __restrict p5) +{ +kernel_rvv_begin (); +xor_regs_5_ (bytes, p1, p2, p3, p4, p5); +kernel_rvv_end (); +} + +static struct xor_block_template xor_block_rvv = { + .name ="rvv" , + .do_2 = xor_rvv_2, + .do_3 = xor_rvv_3, + .do_4 = xor_rvv_4, + .do_5 = xor_rvv_5 +}; + +#undef XOR_TRY_TEMPLATES +#define XOR_TRY_TEMPLATES \ + do { \ + xor_speed(&xor_block_8regs); \ + xor_speed(&xor_block_32regs); \ + if (has_vector()) { \ + xor_speed(&xor_block_rvv);\ + } \ + } while (0) +#endif
arch/riscv/lib/Makefile +1 -0
@@ -7,3 +7,4 @@ lib-$(CONFIG_MMU) += uaccess.o lib-$(CONFIG_64BIT) += tishift.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o +lib-$(CONFIG_VECTOR) += xor.o
arch/riscv/lib/xor.S new file +81 -0
81 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 14/17 riscv: Fix a kernel panic issue if $s2 is set to a specific value before entering Linux
In reply to Chris StillsonFrom: 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.25.1
PATCH 15/17 riscv: Add V extension to KVM ISA allow list
In reply to Chris StillsonFrom: Vincent Chen <[email protected]>
Add V extension to KVM_RISCV_ISA_ALLOWED list to enable VCPU to support V extension.
--- arch/riscv/include/asm/hwcap.h | 1 + 1 file changed, 1 insertion(+)
arch/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.
Signature · 1 line
2.25.1
PATCH 16/17 riscv: KVM: Add vector lazy save/restore support
In reply to Chris StillsonFrom: 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 | 65 +++++++++ arch/riscv/include/uapi/asm/kvm.h | 7 + arch/riscv/kernel/asm-offsets.c | 7 + arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu.c | 32 +++++ arch/riscv/kvm/vcpu_switch.S | 69 +++++++++ arch/riscv/kvm/vcpu_vector.c | 173 +++++++++++++++++++++++ 8 files changed, 356 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
@@ -17,6 +17,7 @@#include <asm/hwcap.h> #include <asm/kvm_vcpu_fp.h> #include <asm/kvm_vcpu_insn.h> +#include <asm/kvm_vcpu_vector.h> #include <asm/kvm_vcpu_timer.h> #define KVM_MAX_VCPUS 1024
@@ -143,6 +144,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 +65 -0
65 lines not on this page · raw message
arch/riscv/include/uapi/asm/kvm.h +7 -0
@@ -96,6 +96,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_MAX,
@@ -145,6 +146,12 @@ 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 7 */ +#define KVM_REG_RISCV_VECTOR (0x07 << 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/kernel/asm-offsets.c +7 -0
@@ -278,6 +278,13 @@ void asm_offsets(void)OFFSET (KVM_ARCH_FP_D_F31 , kvm_cpu_context, fp.d.f[31 ]);OFFSET (KVM_ARCH_FP_D_FCSR , kvm_cpu_context, fp.d.fcsr); +/* V extension */ + +OFFSET (KVM_ARCH_VECTOR_VSTART , kvm_cpu_context, vector.vstart); +OFFSET (KVM_ARCH_VECTOR_VL , kvm_cpu_context, vector.vl); +OFFSET (KVM_ARCH_VECTOR_VTYPE , kvm_cpu_context, vector.vtype); +OFFSET (KVM_ARCH_VECTOR_VCSR , kvm_cpu_context, vector.vcsr); +OFFSET (KVM_ARCH_VECTOR_DATAP , kvm_cpu_context, vector.datap);/* * THREAD_{F,X}* might be larger than a S-type offset can handle, but * these are used in performance-sensitive assembly so we can't resort
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 +32 -0
@@ -19,6 +19,7 @@#include <linux/kvm_host.h> #include <asm/csr.h> #include <asm/hwcap.h> +#include <asm/switch_to.h> const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),
@@ -51,6 +52,7 @@ static const unsigned long kvm_isa_ext_arr[] = { RISCV_ISA_EXT_h, RISCV_ISA_EXT_i, RISCV_ISA_EXT_m, + RISCV_ISA_EXT_v,RISCV_ISA_EXT_SVPBMT , RISCV_ISA_EXT_SSTC, };
@@ -79,6 +81,7 @@ static bool kvm_riscv_vcpu_isa_enable_allowed(unsigned long ext)return true; } +//CMS FIXME static bool kvm_riscv_vcpu_isa_disable_allowed (unsigned long ext) {switch (ext) {
@@ -121,6 +124,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 );
@@ -171,6 +176,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 ;
@@ -201,6 +215,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)
@@ -539,6 +556,9 @@ static int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu, KVM_REG_RISCV_FP_D); else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_ISA_EXT)return kvm_riscv_vcpu_set_reg_isa_ext (vcpu, reg); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_VECTOR) +return kvm_riscv_vcpu_set_reg_vector (vcpu, reg, +KVM_REG_RISCV_VECTOR );return -EINVAL ; }
@@ -562,6 +582,9 @@ static int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu, KVM_REG_RISCV_FP_D); else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_ISA_EXT)return kvm_riscv_vcpu_get_reg_isa_ext (vcpu, reg); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_VECTOR) +return kvm_riscv_vcpu_get_reg_vector (vcpu, reg, +KVM_REG_RISCV_VECTOR );return -EINVAL ; }
@@ -818,6 +841,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; }
@@ -834,6 +860,12 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)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 ); csr->vstvec =csr_read (CSR_VSTVEC );
arch/riscv/kvm/vcpu_switch.S +69 -0
72 lines not on this page · raw message
arch/riscv/kvm/vcpu_vector.c new file +173 -0
173 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 17/17 riscv: prctl to enable vector commands
In reply to Chris StillsonThis code makes enabling the vector extension on a riscv manchine optional by adding an option to prctl() to allow a process to enable, disable or query its vector context state.
-added prctl to enable/disable/query current vector state -added actual function in riscv specific code to change/query the process state
- Fixed problem with initial set of patches
(missing some EXPORT_SYMBOL() macro calls)
- rebased to 6.0-rc1
--- arch/riscv/configs/defconfig | 6 ++++++ arch/riscv/include/asm/kvm_vcpu_vector.h | 8 ++++---- arch/riscv/include/asm/processor.h | 6 ++++++ arch/riscv/include/asm/switch_to.h | 11 +++++++++++ arch/riscv/kernel/cpufeature.c | 3 ++- arch/riscv/kernel/process.c | 20 +++++++++++++++++++- arch/riscv/kvm/vcpu_vector.c | 14 +++++++------- include/uapi/linux/prctl.h | 6 ++++++ kernel/sys.c | 7 +++++++ 9 files changed, 68 insertions(+), 13 deletions(-)
arch/riscv/configs/defconfig +6 -0
@@ -209,3 +209,9 @@ CONFIG_RCU_EQS_DEBUG=y # CONFIG_FTRACE is not set # CONFIG_RUNTIME_TESTING_MENU is not set CONFIG_MEMTEST=y +CONFIG_ARCH_RV64I=y +CONFIG_64BIT=y +CONFIG_VECTOR=y +CONFIG_ARCH_RV64I=y +CONFIG_64BIT=y +CONFIG_VECTOR=y
arch/riscv/include/asm/kvm_vcpu_vector.h +4 -4
@@ -22,9 +22,9 @@ void __kvm_riscv_vector_save(struct kvm_cpu_context *context);void __kvm_riscv_vector_restore (struct kvm_cpu_context *context);void kvm_riscv_vcpu_vector_reset (struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_guest_vector_save (struct kvm_cpu_context *cntx, -unsigned long isa); +unsigned long *isa);void kvm_riscv_vcpu_guest_vector_restore (struct kvm_cpu_context *cntx, -unsigned long isa); +unsigned long *isa);void kvm_riscv_vcpu_host_vector_save (struct kvm_cpu_context *cntx);void kvm_riscv_vcpu_host_vector_restore (struct kvm_cpu_context *cntx);void kvm_riscv_vcpu_free_vector_context (struct kvm_vcpu *vcpu);
@@ -34,12 +34,12 @@ static inline void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu) }static inline void kvm_riscv_vcpu_guest_vector_save (struct kvm_cpu_context *cntx, -unsigned long isa) +unsigned long *isa) { }static inline void kvm_riscv_vcpu_guest_vector_restore (struct kvm_cpu_context *cntx, -unsigned long isa) +unsigned long *isa) { }
arch/riscv/include/asm/processor.h +6 -0
@@ -88,6 +88,12 @@ 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 ; + +#ifdef CONFIG_VECTOR +extern int rvv_proc_enable (unsigned long x); +#define RVV_PROC_ENABLE (x) rvv_proc_enable(x) +#endif +#endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */
arch/riscv/include/asm/switch_to.h +11 -0
@@ -80,6 +80,17 @@ extern unsigned long riscv_vsize;extern void __vstate_save (struct __riscv_v_state *save_to,void *datap);extern void __vstate_restore (struct __riscv_v_state *restore_from,void *datap); +static inline bool vstate_query (struct pt_regs *regs) +{ +return (regs->status &SR_VS ) !=0 ; +} + +static inline void vstate_on (struct task_struct *task, +struct pt_regs *regs) +{ + regs->status = (regs->status & ~(SR_VS )) |SR_VS_INITIAL ; +} +static inline void __vstate_clean (struct pt_regs *regs) { regs->status = (regs->status & ~(SR_VS )) |SR_VS_CLEAN ;
arch/riscv/kernel/cpufeature.c +2 -1
@@ -37,6 +37,8 @@ __ro_after_init DEFINE_STATIC_KEY_FALSE(cpu_hwcap_fpu);#include <asm/vector.h> __ro_after_init DEFINE_STATIC_KEY_FALSE (cpu_hwcap_vector);unsigned long riscv_vsize__read_mostly ; +EXPORT_SYMBOL (cpu_hwcap_vector); +EXPORT_SYMBOL (riscv_vsize);#endif /**
@@ -346,4 +348,3 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, } }#endif -}
arch/riscv/kernel/process.c +19 -1
@@ -15,6 +15,7 @@#include <linux/tick.h> #include <linux/ptrace.h> #include <linux/uaccess.h> +#include <linux/prctl.h> #include <asm/unistd.h> #include <asm/processor.h>
@@ -134,7 +135,6 @@ void start_thread(struct pt_regs *regs, unsigned long pc,if (WARN_ON (!vstate->datap))return ; } - regs->status |=SR_VS_INITIAL ;/* * Restore the initial value to the vector register
@@ -230,3 +230,21 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.sp = (unsigned long )childregs;/* kernel sp */ return 0 ; } + +#ifdef CONFIG_VECTOR +int rvv_proc_enable (unsigned long x) { +switch (x) { +case PR_RVV_DISABLE : +vstate_off (current,task_pt_regs (current)); +return 0 ; +case PR_RVV_ENABLE : +vstate_on (current,task_pt_regs (current)); +return 0 ; +case PR_RVV_QUERY : +return vstate_query (task_pt_regs (current)); +default : +return -(EINVAL ); + + } +} +#endif
arch/riscv/kvm/vcpu_vector.c +7 -7
@@ -20,7 +20,7 @@extern unsigned long riscv_vsize;void kvm_riscv_vcpu_vector_reset (struct kvm_vcpu *vcpu) { -unsigned long isa = vcpu->arch.isa; +unsigned long isa = *vcpu->arch.isa;struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; cntx->sstatus &= ~SR_VS ;
@@ -39,20 +39,20 @@ static void kvm_riscv_vcpu_vector_clean(struct kvm_cpu_context *cntx) }void kvm_riscv_vcpu_guest_vector_save (struct kvm_cpu_context *cntx, -unsigned long isa) +unsigned long *isa) {if ((cntx->sstatus &SR_VS ) ==SR_VS_DIRTY ) { -if (riscv_isa_extension_available (&isa, v)) +if (riscv_isa_extension_available (isa, v))__kvm_riscv_vector_save (cntx);kvm_riscv_vcpu_vector_clean (cntx); } }void kvm_riscv_vcpu_guest_vector_restore (struct kvm_cpu_context *cntx, -unsigned long isa) +unsigned long *isa) {if ((cntx->sstatus &SR_VS ) !=SR_VS_OFF ) { -if (riscv_isa_extension_available (&isa, v)) +if (riscv_isa_extension_available (isa, v))__kvm_riscv_vector_restore (cntx);kvm_riscv_vcpu_vector_clean (cntx); }
@@ -122,7 +122,7 @@ int kvm_riscv_vcpu_get_reg_vector(struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg, unsigned long rtype) { - unsigned long isa = vcpu->arch.isa; + unsigned long isa = *vcpu->arch.isa;unsigned long __user *uaddr = (unsigned long __user *)(unsigned long )reg->addr; unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK |
@@ -149,7 +149,7 @@ int kvm_riscv_vcpu_set_reg_vector(struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg, unsigned long rtype) { - unsigned long isa = vcpu->arch.isa; + unsigned long isa = *vcpu->arch.isa;unsigned long __user *uaddr = (unsigned long __user *)(unsigned long )reg->addr; unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK |
include/uapi/linux/prctl.h +6 -0
@@ -281,6 +281,12 @@ struct prctl_mm_map {# define PR_SME_VL_LEN_MASK 0xffff# define PR_SME_VL_INHERIT (1 << 17)/* inherit across exec */ +/* RISC-V V vector extension */ +#define PR_RVV_STATE 65 +# define PR_RVV_DISABLE 0 +# define PR_RVV_ENABLE 1 +# define PR_RVV_QUERY 2 +#define PR_SET_VMA 0x53564d41# define PR_SET_VMA_ANON_NAME 0
kernel/sys.c +7 -0
@@ -138,6 +138,9 @@#ifndef GET_TAGGED_ADDR_CTRL # define GET_TAGGED_ADDR_CTRL () (-EINVAL)#endif +#ifndef RVV_PROC_ENABLE +# define RVV_PROC_ENABLE (x) (-EINVAL) +#endif /* * this is where the system-wide overflow UID and GID are defined, for
@@ -2620,6 +2623,10 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, error =sched_core_share_pid (arg2, arg3, arg4, arg5);break ;#endif +case PR_RVV_STATE : + error =RVV_PROC_ENABLE (arg2); +break ; +case PR_SET_VMA : error =prctl_set_vma (arg2, arg3, arg4, arg5);break ;
Signature · 1 line
2.25.1