Add support to handle misaligned accesses in S-mode
v2 cover Add support to handle misaligned accesses in S-mode
Since commit 61cadb9 ("Provide new description of misaligned load/store behavior compatible with privileged architecture.") in the RISC-V ISA manual, it is stated that misaligned load/store might not be supported. However, the RISC-V kernel uABI describes that misaligned accesses are supported. In order to support that, this series adds support for S-mode handling of misaligned accesses as well support for prctl(PR_UNALIGN).
Handling misaligned access in kernel allows for a finer grain control of the misaligned accesses behavior, and thanks to the prctl() call, can allow disabling misaligned access emulation to generate SIGBUS. User space can then optimize its software by removing such access based on SIGBUS generation.
This series is useful when using a SBI implementation that does not handle misaligned traps as well as detecting misaligned accesses generated by userspace application using the prctrl(PR_SET_UNALIGN) feature.
This series can be tested using the spike simulator[1] and a modified openSBI version[2] which allows to always delegate misaligned load/store to S-mode. A test[3] that exercise various instructions/registers can be executed to verify the unaligned access support.
[1] https://github.com/riscv-software-src/riscv-isa-sim
[2] https://github.com/rivosinc/opensbi/tree/dev/cleger/no_misaligned
[3] https://github.com/clementleger/unaligned_test
Changes in V2:
- Fix wrong fpu assembly function name (detected with llvm build)
- Changes the detection mechanism using direct detection in trap handler (CONFIG_M_MODE does not support extable and re-adding extable just for some boot time detection is a bit overkill)
- Fix commit order (used a variable introduce in a later commit)
- Add a CONFIG_RISCV_MISALIGNED option to completely disable misaligned handling in kernel and reduce text size
- Use for_each_present_cpu() instead of for_each_possible_cpu() in init
- Ensure that if unaligned_ctl was set, fail to online cpu if it does not emulate misaligned accesses.
Clément Léger (8):
riscv: remove unused functions in traps_misaligned.c
riscv: add support for misaligned trap handling in S-mode
riscv: report perf event for misaligned fault
riscv: add floating point insn support to misaligned access emulation
riscv: add support for sysctl unaligned_enabled control
riscv: annotate check_unaligned_access_boot_cpu() with __init
riscv: report misaligned accesses emulation to hwprobe
riscv: add support for PR_SET_UNALIGN and PR_GET_UNALIGN
arch/riscv/Kconfig | 9 + arch/riscv/include/asm/cpufeature.h | 18 ++ arch/riscv/include/asm/entry-common.h | 14 + arch/riscv/include/asm/processor.h | 9 + arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/cpufeature.c | 6 +- arch/riscv/kernel/fpu.S | 121 +++++++++ arch/riscv/kernel/process.c | 18 ++ arch/riscv/kernel/smpboot.c | 2 +- arch/riscv/kernel/traps.c | 9 - arch/riscv/kernel/traps_misaligned.c | 375 ++++++++++++++++++++++---- 11 files changed, 524 insertions(+), 59 deletions(-)
Signature · 1 line
2.42.0
PATCH v2 1/8 riscv: remove unused functions in traps_misaligned.c
Replace macros by the only two function calls that are done from this file, store_u8() and load_u8().
--- arch/riscv/kernel/traps_misaligned.c | 46 +++++----------------------- 1 file changed, 7 insertions(+), 39 deletions(-)
arch/riscv/kernel/traps_misaligned.c +7 -39
@@ -151,51 +151,19 @@#define PRECISION_S 0#define PRECISION_D 1 -#define DECLARE_UNPRIVILEGED_LOAD_FUNCTION (type, insn) \ -static inline type load_##type(const type *addr) \ -{ \ - type val; \ - asm (#insn " %0, %1" \ - : "=&r" (val) : "m" (*addr)); \ - return val; \ -} +static inline u8 load_u8 (const u8 *addr) +{ +u8 val; -#define DECLARE_UNPRIVILEGED_STORE_FUNCTION (type, insn) \ -static inline void store_##type(type *addr, type val) \ -{ \ - asm volatile (#insn " %0, %1\n" \ - : : "r" (val), "m" (*addr)); \ -} + asmvolatile ("lbu %0, %1" :"=&r" (val) :"m" (*addr)); -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u8, lbu) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u16, lhu) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s8, lb) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s16, lh) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s32, lw) -DECLARE_UNPRIVILEGED_STORE_FUNCTION(u8, sb) -DECLARE_UNPRIVILEGED_STORE_FUNCTION(u16, sh) -DECLARE_UNPRIVILEGED_STORE_FUNCTION(u32, sw) -#if defined(CONFIG_64BIT ) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u32, lwu) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u64, ld) -DECLARE_UNPRIVILEGED_STORE_FUNCTION(u64, sd) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(ulong, ld) -#else -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u32, lw) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(ulong, lw) - -static inline u64 load_u64 (const u64 *addr) -{ -return load_u32 ((u32 *)addr) - + ((u64 )load_u32 ((u32 *)addr +1 ) <<32 ); +return val; } -static inline void store_u64 (u64 *addr,u64 val) +static inline void store_u8 (u8 *addr,u8 val) { -store_u32 ((u32 *)addr, val); -store_u32 ((u32 *)addr +1 , val >>32 ); + asmvolatile ("sb %0, %1\n" : :"r" (val),"m" (*addr)); } -#endif static inline ulong get_insn (ulong mepc) {
Signature · 1 line
2.42.0
Re: [PATCH v2 1/8] riscv: remove unused functions in traps_misaligned.c
Quoted 15 lines · Clément Léger
Replace macros by the only two function calls that are done from this file,
store_u8()andload_u8().Signed-off-by Clément Léger <[email protected]>--- arch/riscv/kernel/traps_misaligned.c | 46 +++++----------------------- 1 file changed, 7 insertions(+), 39 deletions(-)arch/riscv/kernel/traps_misaligned.c +0
-0@@ -151,51 +151,19 @@#define PRECISION_S 0#define PRECISION_D 1
arch/riscv/kernel/traps_misaligned.c line 154
Quoted 10 lines
arch/riscv/kernel/traps_misaligned.c +1
-8-#define DECLARE_UNPRIVILEGED_LOAD_FUNCTION (type, insn) \ -static inline type load_##type(const type *addr) \ -{ \ - type val; \ - asm (#insn " %0, %1" \ - : "=&r" (val) : "m" (*addr)); \ - return val; \ -} +static inline u8 load_u8 (const u8 *addr)
Really a nit, and applies to the whole file: "static inline" in a .c file is just useless.
arch/riscv/kernel/traps_misaligned.c line 158
Quoted 10 lines
arch/riscv/kernel/traps_misaligned.c +3
-6+{ +u8 val; -#define DECLARE_UNPRIVILEGED_STORE_FUNCTION (type, insn) \ -static inline void store_##type(type *addr, type val) \ -{ \ - asm volatile (#insn " %0, %1\n" \ - : : "r" (val), "m" (*addr)); \ -} + asmvolatile ("lbu %0, %1" :"=&r" (val) :"m" (*addr));
Why do you need early clobber here?
Björn
Re: [PATCH v2 1/8] riscv: remove unused functions in traps_misaligned.c
Quoted 18 lines · Björn Töpel
Quoted 16 lines · Clément Léger
Replace macros by the only two function calls that are done from this file,
store_u8()andload_u8().Signed-off-by Clément Léger <[email protected]>--- arch/riscv/kernel/traps_misaligned.c | 46 +++++----------------------- 1 file changed, 7 insertions(+), 39 deletions(-)arch/riscv/kernel/traps_misaligned.c +0
-0@@ -151,51 +151,19 @@#define PRECISION_S 0#define PRECISION_D 1
Quoted 12 lines
arch/riscv/kernel/traps_misaligned.c line 154
Quoted 9 lines
arch/riscv/kernel/traps_misaligned.c +1
-8-#define DECLARE_UNPRIVILEGED_LOAD_FUNCTION (type, insn) \ -static inline type load_##type(const type *addr) \ -{ \ - type val; \ - asm (#insn " %0, %1" \ - : "=&r" (val) : "m" (*addr)); \ - return val; \ -} +static inline u8 load_u8 (const u8 *addr)Really a nit, and applies to the whole file: "static inline" in a .c file is just useless.
Oh yes clearly, I should have fixed that while factorizing, I simply kept them from the previous code.
Quoted 4 lines
Quoted 4 lines
arch/riscv/kernel/traps_misaligned.c +2
-0+{ +u8 val;
Quoted 9 lines
arch/riscv/kernel/traps_misaligned.c line 158
Quoted 7 lines
arch/riscv/kernel/traps_misaligned.c +1
-6-#define DECLARE_UNPRIVILEGED_STORE_FUNCTION (type, insn) \ -static inline void store_##type(type *addr, type val) \ -{ \ - asm volatile (#insn " %0, %1\n" \ - : : "r" (val), "m" (*addr)); \ -} + asmvolatile ("lbu %0, %1" :"=&r" (val) :"m" (*addr));Why do you need early clobber here?
Ditto, copy pasted from existing code but I don't think there is a need for early clobber here.
Clément
Quoted 3 lines
Björn
PATCH v2 2/8 riscv: add support for misaligned trap handling in S-mode
In reply to Clément LégerMisalignment trap handling is only supported for M-mode and uses direct accesses to user memory. In S-mode, when handling usermode fault, this requires to use the get_user()/put_user() accessors. Implement load_u8(), store_u8() and get_insn() using these accessors for userspace and direct text access for kernel.
--- arch/riscv/Kconfig | 8 ++ arch/riscv/include/asm/entry-common.h | 14 +++ arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/traps.c | 9 -- arch/riscv/kernel/traps_misaligned.c | 119 +++++++++++++++++++++++--- 5 files changed, 129 insertions(+), 23 deletions(-)
arch/riscv/Kconfig +8 -0
@@ -636,6 +636,14 @@ config THREAD_SIZE_ORDER Specify the Pages of thread stack size (from 4KB to 64KB), which also affects irq stack size, which is equal to thread stack size. +config RISCV_MISALIGNED + bool "Support misaligned load/store traps for kernel and userspace" + default y + help + Say Y here if you want the kernel to embed support for misaligned + load/store for both kernel and userspace. When disable, misaligned + accesses will generate SIGBUS in userspace and panic in kernel. + endmenu # "Platform type" menu "Kernel features"
arch/riscv/include/asm/entry-common.h +14 -0
@@ -8,4 +8,18 @@void handle_page_fault (struct pt_regs *regs);void handle_break (struct pt_regs *regs); +#ifdef CONFIG_RISCV_MISALIGNED +int handle_misaligned_load (struct pt_regs *regs); +int handle_misaligned_store (struct pt_regs *regs); +#else +static inline int handle_misaligned_load (struct pt_regs *regs) +{ +return -1 ; +} +static inline int handle_misaligned_store (struct pt_regs *regs) +{ +return -1 ; +} +#endif +#endif /* _ASM_RISCV_ENTRY_COMMON_H */
arch/riscv/kernel/Makefile +1 -1
@@ -59,7 +59,7 @@ obj-y += patch.o obj-y += probes/ obj-$(CONFIG_MMU) += vdso.o vdso/ -obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o +obj-$(CONFIG_RISCV_MISALIGNED) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_RISCV_ISA_V) += vector.o obj-$(CONFIG_SMP) += smpboot.o
arch/riscv/kernel/traps.c +0 -9
@@ -179,14 +179,6 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *reDO_ERROR_INFO (do_trap_load_fault,SIGSEGV ,SEGV_ACCERR ,"load access fault" ); -#ifndef CONFIG_RISCV_M_MODE -DO_ERROR_INFO (do_trap_load_misaligned, -SIGBUS ,BUS_ADRALN ,"Oops - load address misaligned" ); -DO_ERROR_INFO (do_trap_store_misaligned, -SIGBUS ,BUS_ADRALN ,"Oops - store (or AMO) address misaligned" ); -#else -int handle_misaligned_load (struct pt_regs *regs); -int handle_misaligned_store (struct pt_regs *regs); asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs) {
@@ -229,7 +221,6 @@ asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regsirqentry_nmi_exit (regs, state); } } -#endif DO_ERROR_INFO (do_trap_store_fault,SIGSEGV ,SEGV_ACCERR ,"store (or AMO) access fault" ); DO_ERROR_INFO(do_trap_ecall_s,
arch/riscv/kernel/traps_misaligned.c +106 -13
178 lines not on this page · raw message
Signature · 1 line
2.42.0
Re: [PATCH v2 2/8] riscv: add support for misaligned trap handling in S-mode
Quoted 147 lines · Clément Léger
Misalignment trap handling is only supported for M-mode and uses direct accesses to user memory. In S-mode, when handling usermode fault, this requires to use the get_user()/put_user() accessors. Implement
load_u8(),store_u8()andget_insn()using these accessors for userspace and direct text access for kernel.Signed-off-by Clément Léger <[email protected]>--- arch/riscv/Kconfig | 8 ++ arch/riscv/include/asm/entry-common.h | 14 +++ arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/traps.c | 9 -- arch/riscv/kernel/traps_misaligned.c | 119 +++++++++++++++++++++++--- 5 files changed, 129 insertions(+), 23 deletions(-)arch/riscv/Kconfig +8
-0@@ -636,6 +636,14 @@ config THREAD_SIZE_ORDER Specify the Pages of thread stack size (from 4KB to 64KB), which also affects irq stack size, which is equal to thread stack size. +config RISCV_MISALIGNED + bool "Support misaligned load/store traps for kernel and userspace" + default y + help + Say Y here if you want the kernel to embed support for misaligned + load/store for both kernel and userspace. When disable, misaligned + accesses will generate SIGBUS in userspace and panic in kernel. + endmenu # "Platform type" menu "Kernel features"arch/riscv/include/asm/entry-common.h +14
-0@@ -8,4 +8,18 @@void handle_page_fault (struct pt_regs *regs);void handle_break (struct pt_regs *regs); +#ifdef CONFIG_RISCV_MISALIGNED +int handle_misaligned_load (struct pt_regs *regs); +int handle_misaligned_store (struct pt_regs *regs); +#else +static inline int handle_misaligned_load (struct pt_regs *regs) +{ +return -1 ; +} +static inline int handle_misaligned_store (struct pt_regs *regs) +{ +return -1 ; +} +#endif +#endif /* _ASM_RISCV_ENTRY_COMMON_H */ arch/riscv/kernel/Makefile +1
-1@@ -59,7 +59,7 @@ obj-y += patch.o obj-y += probes/ obj-$(CONFIG_MMU) += vdso.o vdso/ -obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o +obj-$(CONFIG_RISCV_MISALIGNED) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_RISCV_ISA_V) += vector.o obj-$(CONFIG_SMP) += smpboot.oarch/riscv/kernel/traps.c +0
-9@@ -179,14 +179,6 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *reDO_ERROR_INFO (do_trap_load_fault,SIGSEGV ,SEGV_ACCERR ,"load access fault" ); -#ifndef CONFIG_RISCV_M_MODE -DO_ERROR_INFO (do_trap_load_misaligned, -SIGBUS ,BUS_ADRALN ,"Oops - load address misaligned" ); -DO_ERROR_INFO (do_trap_store_misaligned, -SIGBUS ,BUS_ADRALN ,"Oops - store (or AMO) address misaligned" ); -#else -int handle_misaligned_load (struct pt_regs *regs); -int handle_misaligned_store (struct pt_regs *regs); asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs) {@@ -229,7 +221,6 @@ asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regsirqentry_nmi_exit (regs, state); } } -#endif DO_ERROR_INFO (do_trap_store_fault,SIGSEGV ,SEGV_ACCERR ,"store (or AMO) access fault" ); DO_ERROR_INFO(do_trap_ecall_s,arch/riscv/kernel/traps_misaligned.c +12
-5@@ -12,6 +12,7 @@#include <asm/processor.h> #include <asm/ptrace.h> #include <asm/csr.h> +#include <asm/entry-common.h> #define INSN_MATCH_LB 0x3#define INSN_MASK_LB 0x707f@@ -151,21 +152,25 @@#define PRECISION_S 0#define PRECISION_D 1 -static inline u8 load_u8 (const u8 *addr) +#ifdef CONFIG_RISCV_M_MODE +static inline int load_u8 (struct pt_regs *regs,const u8 *addr,u8 *r_val) {u8 val; asmvolatile ("lbu %0, %1" :"=&r" (val) :"m" (*addr)); + *r_val = val; -return val; +return 0 ; } -static inline void store_u8 (u8 *addr,u8 val) +static inline int store_u8 (struct pt_regs *regs,u8 *addr,u8 val) { asmvolatile ("sb %0, %1\n" : :"r" (val),"m" (*addr)); + +return 0 ; } -static inline ulong get_insn (ulong mepc) +static inline int get_insn (struct pt_regs *regs,ulong mepc,ulong *r_insn) { register ulong __mepc asm ("a2") = mepc;ulong val, rvc_mask =3 , tmp;@@ -194,9 +199,87 @@ static inline ulong get_insn(ulong mepc) : [addr] "r" (__mepc), [rvc_mask] "r" (rvc_mask), [xlen_minus_16] "i" (XLEN_MINUS_16)); -return val; + *r_insn = val; + +return 0 ;
arch/riscv/kernel/traps_misaligned.c static inline ulong get_insn(ulong mepc) · line 214
Quoted 10 lines
arch/riscv/kernel/traps_misaligned.c +10
-0+} +#else +static inline int load_u8 (struct pt_regs *regs,const u8 *addr,u8 *r_val) +{ +if (user_mode (regs)) { +return __get_user (*r_val, addr); + }else { + *r_val = *addr; +return 0 ; + }
One nit (...well two) ;-)
If you're respinning I'd get rid of the "inlines", and personally I think early exit is easier to read. Applies to the whole patch.
| {
| if (user_mode(regs))
| return __get_user(*r_val, addr);
|
| *r_val = *addr;
| return 0;
| }Regardless if you change or not,
Re: [PATCH v2 2/8] riscv: add support for misaligned trap handling in S-mode
Quoted 162 lines · Björn Töpel
Quoted 147 lines · Clément Léger
Misalignment trap handling is only supported for M-mode and uses direct accesses to user memory. In S-mode, when handling usermode fault, this requires to use the get_user()/put_user() accessors. Implement
load_u8(),store_u8()andget_insn()using these accessors for userspace and direct text access for kernel.Signed-off-by Clément Léger <[email protected]>--- arch/riscv/Kconfig | 8 ++ arch/riscv/include/asm/entry-common.h | 14 +++ arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/traps.c | 9 -- arch/riscv/kernel/traps_misaligned.c | 119 +++++++++++++++++++++++--- 5 files changed, 129 insertions(+), 23 deletions(-)arch/riscv/Kconfig +8
-0@@ -636,6 +636,14 @@ config THREAD_SIZE_ORDER Specify the Pages of thread stack size (from 4KB to 64KB), which also affects irq stack size, which is equal to thread stack size. +config RISCV_MISALIGNED + bool "Support misaligned load/store traps for kernel and userspace" + default y + help + Say Y here if you want the kernel to embed support for misaligned + load/store for both kernel and userspace. When disable, misaligned + accesses will generate SIGBUS in userspace and panic in kernel. + endmenu # "Platform type" menu "Kernel features"arch/riscv/include/asm/entry-common.h +14
-0@@ -8,4 +8,18 @@void handle_page_fault (struct pt_regs *regs);void handle_break (struct pt_regs *regs); +#ifdef CONFIG_RISCV_MISALIGNED +int handle_misaligned_load (struct pt_regs *regs); +int handle_misaligned_store (struct pt_regs *regs); +#else +static inline int handle_misaligned_load (struct pt_regs *regs) +{ +return -1 ; +} +static inline int handle_misaligned_store (struct pt_regs *regs) +{ +return -1 ; +} +#endif +#endif /* _ASM_RISCV_ENTRY_COMMON_H */ arch/riscv/kernel/Makefile +1
-1@@ -59,7 +59,7 @@ obj-y += patch.o obj-y += probes/ obj-$(CONFIG_MMU) += vdso.o vdso/ -obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o +obj-$(CONFIG_RISCV_MISALIGNED) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_RISCV_ISA_V) += vector.o obj-$(CONFIG_SMP) += smpboot.oarch/riscv/kernel/traps.c +0
-9@@ -179,14 +179,6 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *reDO_ERROR_INFO (do_trap_load_fault,SIGSEGV ,SEGV_ACCERR ,"load access fault" ); -#ifndef CONFIG_RISCV_M_MODE -DO_ERROR_INFO (do_trap_load_misaligned, -SIGBUS ,BUS_ADRALN ,"Oops - load address misaligned" ); -DO_ERROR_INFO (do_trap_store_misaligned, -SIGBUS ,BUS_ADRALN ,"Oops - store (or AMO) address misaligned" ); -#else -int handle_misaligned_load (struct pt_regs *regs); -int handle_misaligned_store (struct pt_regs *regs); asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs) {@@ -229,7 +221,6 @@ asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regsirqentry_nmi_exit (regs, state); } } -#endif DO_ERROR_INFO (do_trap_store_fault,SIGSEGV ,SEGV_ACCERR ,"store (or AMO) access fault" ); DO_ERROR_INFO(do_trap_ecall_s,arch/riscv/kernel/traps_misaligned.c +12
-5@@ -12,6 +12,7 @@#include <asm/processor.h> #include <asm/ptrace.h> #include <asm/csr.h> +#include <asm/entry-common.h> #define INSN_MATCH_LB 0x3#define INSN_MASK_LB 0x707f@@ -151,21 +152,25 @@#define PRECISION_S 0#define PRECISION_D 1 -static inline u8 load_u8 (const u8 *addr) +#ifdef CONFIG_RISCV_M_MODE +static inline int load_u8 (struct pt_regs *regs,const u8 *addr,u8 *r_val) {u8 val; asmvolatile ("lbu %0, %1" :"=&r" (val) :"m" (*addr)); + *r_val = val; -return val; +return 0 ; } -static inline void store_u8 (u8 *addr,u8 val) +static inline int store_u8 (struct pt_regs *regs,u8 *addr,u8 val) { asmvolatile ("sb %0, %1\n" : :"r" (val),"m" (*addr)); + +return 0 ; } -static inline ulong get_insn (ulong mepc) +static inline int get_insn (struct pt_regs *regs,ulong mepc,ulong *r_insn) { register ulong __mepc asm ("a2") = mepc;ulong val, rvc_mask =3 , tmp;@@ -194,9 +199,87 @@ static inline ulong get_insn(ulong mepc) : [addr] "r" (__mepc), [rvc_mask] "r" (rvc_mask), [xlen_minus_16] "i" (XLEN_MINUS_16)); -return val; + *r_insn = val; + +return 0 ;arch/riscv/kernel/traps_misaligned.c static inline ulong get_insn(ulong mepc) · line 214
Quoted 10 lines
arch/riscv/kernel/traps_misaligned.c +10
-0+} +#else +static inline int load_u8 (struct pt_regs *regs,const u8 *addr,u8 *r_val) +{ +if (user_mode (regs)) { +return __get_user (*r_val, addr); + }else { + *r_val = *addr; +return 0 ; + }One nit (...well two) ;-)
Quoted 2 lines
If you're respinning I'd get rid of the "inlines",and personally I think early exit is easier to read. Applies to the whole patch.
Noted, I'll fix that (checkpatch suggested it so that makes sense).
Clément
Quoted 13 lines
| { | if (user_mode(regs)) | return __get_user(*r_val, addr); | | *r_val = *addr; | return 0; | }Regardless if you change or not,
Reviewed-by Björn Töpel <[email protected]>
PATCH v2 3/8 riscv: report perf event for misaligned fault
In reply to Clément LégerAdd missing calls to account for misaligned fault event using perf_sw_event().
--- arch/riscv/kernel/traps_misaligned.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/riscv/kernel/traps_misaligned.c +5 -0
@@ -6,6 +6,7 @@#include <linux/init.h> #include <linux/mm.h> #include <linux/module.h> +#include <linux/perf_event.h> #include <linux/irq.h> #include <linux/stringify.h>
@@ -294,6 +295,8 @@ int handle_misaligned_load(struct pt_regs *regs)unsigned long addr = regs->badaddr;int i, fp =0 , shift =0 , len =0 ; +perf_sw_event (PERF_COUNT_SW_ALIGNMENT_FAULTS ,1 , regs, addr); +if (get_insn (regs, epc, &insn))return -1 ;
@@ -382,6 +385,8 @@ int handle_misaligned_store(struct pt_regs *regs)unsigned long addr = regs->badaddr;int i, len =0 ; +perf_sw_event (PERF_COUNT_SW_ALIGNMENT_FAULTS ,1 , regs, addr); +if (get_insn (regs, epc, &insn))return -1 ;
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v2 3/8] riscv: report perf event for misaligned fault
Quoted 4 lines · Clément Léger
Add missing calls to account for misaligned fault event using
perf_sw_event().Signed-off-by Clément Léger <[email protected]>
PATCH v2 4/8 riscv: add floating point insn support to misaligned access emulation
In reply to Clément LégerThis support is partially based of openSBI misaligned emulation floating point instruction support. It provides support for the existing floating point instructions (both for 32/64 bits as well as compressed ones). Since floating point registers are not part of the pt_regs struct, we need to modify them directly using some assembly. We also dirty the pt_regs status in case we modify them to be sure context switch will save FP state. With this support, Linux is on par with openSBI support.
--- arch/riscv/kernel/fpu.S | 121 +++++++++++++++++++++ arch/riscv/kernel/traps_misaligned.c | 152 ++++++++++++++++++++++++++- 2 files changed, 269 insertions(+), 4 deletions(-)
arch/riscv/kernel/fpu.S +121 -0
@@ -104,3 +104,124 @@ ENTRY(__fstate_restore) csrc CSR_STATUS, t1 ret ENDPROC(__fstate_restore) + +#define get_f32(which) fmv.x.s a0, which; j 2f +#define put_f32(which) fmv.s.x which, a1; j 2f +#if __riscv_xlen == 64 +# define get_f64(which) fmv.x.d a0, which; j 2f +# define put_f64(which) fmv.d.x which, a1; j 2f +#else +# define get_f64(which) fsd which, 0(a1); j 2f +# define put_f64(which) fld which, 0(a1); j 2f +#endif + +.macro fp_access_prologue + /* + * Compute jump offset to store the correct FP register since we don't + * have indirect FP register access + */ + sll t0, a0, 3 + la t2, 1f + add t0, t0, t2 + li t1, SR_FS + csrs CSR_STATUS, t1 + jr t0 +1: +.endm + +.macro fp_access_epilogue +2: + csrc CSR_STATUS, t1 + ret +.endm + +#define fp_access_body(__access_func) \ + __access_func(f0); \ + __access_func(f1); \ + __access_func(f2); \ + __access_func(f3); \ + __access_func(f4); \ + __access_func(f5); \ + __access_func(f6); \ + __access_func(f7); \ + __access_func(f8); \ + __access_func(f9); \ + __access_func(f10); \ + __access_func(f11); \ + __access_func(f12); \ + __access_func(f13); \ + __access_func(f14); \ + __access_func(f15); \ + __access_func(f16); \ + __access_func(f17); \ + __access_func(f18); \ + __access_func(f19); \ + __access_func(f20); \ + __access_func(f21); \ + __access_func(f22); \ + __access_func(f23); \ + __access_func(f24); \ + __access_func(f25); \ + __access_func(f26); \ + __access_func(f27); \ + __access_func(f28); \ + __access_func(f29); \ + __access_func(f30); \ + __access_func(f31) + + +#ifdef CONFIG_RISCV_MISALIGNED + +/* + * Disable compressed instructions set to keep a constant offset between FP + * load/store/move instructions + */ +.option norvc +/* + * put_f32_reg - Set a FP register from a register containing the value + * a0 = FP register index to be set + * a1 = value to be loaded in the FP register + */ +SYM_FUNC_START(put_f32_reg) + fp_access_prologue + fp_access_body(put_f32) + fp_access_epilogue +SYM_FUNC_END(put_f32_reg) + +/* + * get_f32_reg - Get a FP register value and return it + * a0 = FP register index to be retrieved + */ +SYM_FUNC_START(get_f32_reg) + fp_access_prologue + fp_access_body(get_f32) + fp_access_epilogue +SYM_FUNC_END(get_f32_reg) + +/* + * put_f64_reg - Set a 64 bits FP register from a value or a pointer. + * a0 = FP register index to be set + * a1 = value/pointer to be loaded in the FP register (when xlen == 32 bits, we + * load the value to a pointer). + */ +SYM_FUNC_START(put_f64_reg) + fp_access_prologue + fp_access_body(put_f64) + fp_access_epilogue +SYM_FUNC_END(put_f64_reg) + +/* + * put_f64_reg - Get a 64 bits FP register value and returned it or store it to + * a pointer. + * a0 = FP register index to be retrieved + * a1 = If xlen == 32, pointer which should be loaded with the FP register value + * or unused if xlen == 64. In which case the FP register value is returned + * through a0 + */ +SYM_FUNC_START(get_f64_reg) + fp_access_prologue + fp_access_body(get_f64) + fp_access_epilogue +SYM_FUNC_END(get_f64_reg) + +#endif /* CONFIG_RISCV_MISALIGNED */
arch/riscv/kernel/traps_misaligned.c +148 -4
193 lines not on this page · raw message
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v2 5/8 riscv: add support for sysctl unaligned_enabled control
In reply to Clément LégerThis sysctl tuning option allows the user to disable misaligned access handling globally on the system. This will also be used by misaligned detection code to temporarily disable misaligned access handling.
--- arch/riscv/Kconfig | 1 + arch/riscv/kernel/traps_misaligned.c | 9 +++++++++ 2 files changed, 10 insertions(+)
arch/riscv/Kconfig +1 -0
@@ -638,6 +638,7 @@ config THREAD_SIZE_ORDER config RISCV_MISALIGNED bool "Support misaligned load/store traps for kernel and userspace" + select SYSCTL_ARCH_UNALIGN_ALLOW default y help Say Y here if you want the kernel to embed support for misaligned
arch/riscv/kernel/traps_misaligned.c +9 -0
@@ -396,6 +396,9 @@ union reg_data {u64 data_u64; }; +/* sysctl hooks */ +int unaligned_enabled__read_mostly =1 ;/* Enabled by default */ +int handle_misaligned_load (struct pt_regs *regs) {union reg_data val;
@@ -406,6 +409,9 @@ int handle_misaligned_load(struct pt_regs *regs)perf_sw_event (PERF_COUNT_SW_ALIGNMENT_FAULTS ,1 , regs, addr); +if (!unaligned_enabled) +return -1 ; +if (get_insn (regs, epc, &insn))return -1 ;
@@ -502,6 +508,9 @@ int handle_misaligned_store(struct pt_regs *regs)perf_sw_event (PERF_COUNT_SW_ALIGNMENT_FAULTS ,1 , regs, addr); +if (!unaligned_enabled) +return -1 ; +if (get_insn (regs, epc, &insn))return -1 ;
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v2 5/8] riscv: add support for sysctl unaligned_enabled control
Quoted 5 lines · Clément Léger
This sysctl tuning option allows the user to disable misaligned access handling globally on the system. This will also be used by misaligned detection code to temporarily disable misaligned access handling.
Signed-off-by Clément Léger <[email protected]>
PATCH v2 6/8 riscv: annotate check_unaligned_access_boot_cpu() with __init
In reply to Clément LégerThis function is solely called as an initcall, thus annotate it with __init.
--- arch/riscv/kernel/cpufeature.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/cpufeature.c +1 -1
@@ -645,7 +645,7 @@ void check_unaligned_access(int cpu)__free_pages (page,get_order (MISALIGNED_BUFFER_SIZE )); } -static int check_unaligned_access_boot_cpu (void ) +static int __init check_unaligned_access_boot_cpu (void ) {check_unaligned_access (0 );return 0 ;
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v2 6/8] riscv: annotate check_unaligned_access_boot_cpu() with __init
Quoted 5 lines · Clément Léger
This function is solely called as an initcall, thus annotate it with
__init.Signed-off-by Clément Léger <[email protected]>
Ah, nice fix.
PATCH v2 7/8 riscv: report misaligned accesses emulation to hwprobe
In reply to Clément Légerhwprobe provides a way to report if misaligned access are emulated. In order to correctly populate that feature, we can check if it actually traps when doing a misaligned access. This can be checked using an exception table entry which will actually be used when a misaligned access is done from kernel mode.
--- arch/riscv/include/asm/cpufeature.h | 18 +++++++++ arch/riscv/kernel/cpufeature.c | 4 ++ arch/riscv/kernel/smpboot.c | 2 +- arch/riscv/kernel/traps_misaligned.c | 56 ++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-)
arch/riscv/include/asm/cpufeature.h +18 -0
@@ -32,4 +32,22 @@ extern struct riscv_isainfo hart_isa[NR_CPUS];void check_unaligned_access (int cpu); +#ifdef CONFIG_RISCV_MISALIGNED +bool unaligned_ctl_available (void ); +bool check_unaligned_access_emulated (int cpu); +void unaligned_emulation_finish (void ); +#else +static inline bool unaligned_ctl_available (void ) +{ +return false; +} + +static inline bool check_unaligned_access_emulated (int cpu) +{ +return false; +} + +static inline void unaligned_emulation_finish (void ) {} +#endif +#endif
arch/riscv/kernel/cpufeature.c +4 -0
@@ -568,6 +568,9 @@ void check_unaligned_access(int cpu)void *src;long speed =RISCV_HWPROBE_MISALIGNED_SLOW ; +if (check_unaligned_access_emulated (cpu)) +return ; + page =alloc_pages (GFP_NOWAIT ,get_order (MISALIGNED_BUFFER_SIZE ));if (!page) {pr_warn ("Can't alloc pages to measure memcpy performance" );
@@ -648,6 +651,7 @@ void check_unaligned_access(int cpu)static int __init check_unaligned_access_boot_cpu (void ) {check_unaligned_access (0 ); +unaligned_emulation_finish ();return 0 ; }
arch/riscv/kernel/smpboot.c +1 -1
@@ -245,8 +245,8 @@ asmlinkage __visible void smp_callin(void)riscv_ipi_enable ();numa_add_cpu (curr_cpuid); -set_cpu_online (curr_cpuid,1 );check_unaligned_access (curr_cpuid); +set_cpu_online (curr_cpuid,1 );if (has_vector ()) {if (riscv_v_setup_vsize ())
arch/riscv/kernel/traps_misaligned.c +56 -0
77 lines not on this page · raw message
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v2 7/8] riscv: report misaligned accesses emulation to hwprobe
Quoted 126 lines · Clément Léger
hwprobe provides a way to report if misaligned access are emulated. In order to correctly populate that feature, we can check if it actually traps when doing a misaligned access. This can be checked using an exception table entry which will actually be used when a misaligned access is done from kernel mode.
Signed-off-by Clément Léger <[email protected]>--- arch/riscv/include/asm/cpufeature.h | 18 +++++++++ arch/riscv/kernel/cpufeature.c | 4 ++ arch/riscv/kernel/smpboot.c | 2 +- arch/riscv/kernel/traps_misaligned.c | 56 ++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-)arch/riscv/include/asm/cpufeature.h +18
-0@@ -32,4 +32,22 @@ extern struct riscv_isainfo hart_isa[NR_CPUS];void check_unaligned_access (int cpu); +#ifdef CONFIG_RISCV_MISALIGNED +bool unaligned_ctl_available (void ); +bool check_unaligned_access_emulated (int cpu); +void unaligned_emulation_finish (void ); +#else +static inline bool unaligned_ctl_available (void ) +{ +return false; +} + +static inline bool check_unaligned_access_emulated (int cpu) +{ +return false; +} + +static inline void unaligned_emulation_finish (void ) {} +#endif +#endif arch/riscv/kernel/cpufeature.c +4
-0@@ -568,6 +568,9 @@ void check_unaligned_access(int cpu)void *src;long speed =RISCV_HWPROBE_MISALIGNED_SLOW ; +if (check_unaligned_access_emulated (cpu)) +return ; + page =alloc_pages (GFP_NOWAIT ,get_order (MISALIGNED_BUFFER_SIZE ));if (!page) {pr_warn ("Can't alloc pages to measure memcpy performance" );@@ -648,6 +651,7 @@ void check_unaligned_access(int cpu)static int __init check_unaligned_access_boot_cpu (void ) {check_unaligned_access (0 ); +unaligned_emulation_finish ();return 0 ; }arch/riscv/kernel/smpboot.c +1
-1@@ -245,8 +245,8 @@ asmlinkage __visible void smp_callin(void)riscv_ipi_enable ();numa_add_cpu (curr_cpuid); -set_cpu_online (curr_cpuid,1 );check_unaligned_access (curr_cpuid); +set_cpu_online (curr_cpuid,1 );if (has_vector ()) {if (riscv_v_setup_vsize ())arch/riscv/kernel/traps_misaligned.c +19
-0@@ -14,6 +14,8 @@#include <asm/ptrace.h> #include <asm/csr.h> #include <asm/entry-common.h> +#include <asm/hwprobe.h> +#include <asm/cpufeature.h> #define INSN_MATCH_LB 0x3#define INSN_MASK_LB 0x707f@@ -396,6 +398,8 @@ union reg_data {u64 data_u64; }; +static bool unaligned_ctl__read_mostly ; +/* sysctl hooks */ int unaligned_enabled__read_mostly =1 ;/* Enabled by default */ @@ -409,6 +413,8 @@ int handle_misaligned_load(struct pt_regs *regs)perf_sw_event (PERF_COUNT_SW_ALIGNMENT_FAULTS ,1 , regs, addr); + *this_cpu_ptr (&misaligned_access_speed) =RISCV_HWPROBE_MISALIGNED_EMULATED ; +if (!unaligned_enabled)return -1 ;@@ -585,3 +591,53 @@ int handle_misaligned_store(struct pt_regs *regs)return 0 ; } + +bool check_unaligned_access_emulated (int cpu) +{ +long *mas_ptr =per_cpu_ptr (&misaligned_access_speed, cpu); +unsigned long tmp_var, tmp_val; +bool misaligned_emu_detected; + + *mas_ptr =RISCV_HWPROBE_MISALIGNED_UNKNOWN ; + + __asm__ __volatile__ ( +" " REG_L " %[tmp], 1(%[ptr])\n" + : [tmp]"=r" (tmp_val) : [ptr]"r" (&tmp_var) :"memory" ); +
arch/riscv/kernel/traps_misaligned.c int handle_misaligned_store(struct pt_regs *regs) · line 616
Quoted 10 lines
arch/riscv/kernel/traps_misaligned.c +10
-0+ misaligned_emu_detected = (*mas_ptr ==RISCV_HWPROBE_MISALIGNED_EMULATED ); +/* +* If unaligned_ctl is already set, this means that we detected that all +* CPUS uses emulated misaligned access at boot time. If that changed +* when hotplugging the new cpu, this is something we don't handle. +*/ +if (unlikely (unaligned_ctl && !misaligned_emu_detected)) { +pr_crit ("CPU misaligned accesses non homogeneous (expected all emulated)\n" ); +while (true) +cpu_relax ();
So the idea is to spin long enough that the wait_for_completion(&cpu_running, 1000ms) times out? Maybe there should be a wfi() in here as well so we're not just burning white hot. Have you verified that if we get here, the CPU will also get taken back down after the timeout? I wonder if __cpu_up() also needs a call to stop the CPU, in the case where that wait_for_completion_timeout() times out.
It also might be more intuitive to reorganize this such that the death loop happens in smp_callin(), as check_unaligned_access_emulated() is not a function you'd expect might sometimes never return.
-Evan
Re: [PATCH v2 7/8] riscv: report misaligned accesses emulation to hwprobe
Quoted 133 lines · Evan Green
Quoted 132 lines · Clément Léger
hwprobe provides a way to report if misaligned access are emulated. In order to correctly populate that feature, we can check if it actually traps when doing a misaligned access. This can be checked using an exception table entry which will actually be used when a misaligned access is done from kernel mode.
Signed-off-by Clément Léger <[email protected]>--- arch/riscv/include/asm/cpufeature.h | 18 +++++++++ arch/riscv/kernel/cpufeature.c | 4 ++ arch/riscv/kernel/smpboot.c | 2 +- arch/riscv/kernel/traps_misaligned.c | 56 ++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-)arch/riscv/include/asm/cpufeature.h +18
-0@@ -32,4 +32,22 @@ extern struct riscv_isainfo hart_isa[NR_CPUS];void check_unaligned_access (int cpu); +#ifdef CONFIG_RISCV_MISALIGNED +bool unaligned_ctl_available (void ); +bool check_unaligned_access_emulated (int cpu); +void unaligned_emulation_finish (void ); +#else +static inline bool unaligned_ctl_available (void ) +{ +return false; +} + +static inline bool check_unaligned_access_emulated (int cpu) +{ +return false; +} + +static inline void unaligned_emulation_finish (void ) {} +#endif +#endif arch/riscv/kernel/cpufeature.c +4
-0@@ -568,6 +568,9 @@ void check_unaligned_access(int cpu)void *src;long speed =RISCV_HWPROBE_MISALIGNED_SLOW ; +if (check_unaligned_access_emulated (cpu)) +return ; + page =alloc_pages (GFP_NOWAIT ,get_order (MISALIGNED_BUFFER_SIZE ));if (!page) {pr_warn ("Can't alloc pages to measure memcpy performance" );@@ -648,6 +651,7 @@ void check_unaligned_access(int cpu)static int __init check_unaligned_access_boot_cpu (void ) {check_unaligned_access (0 ); +unaligned_emulation_finish ();return 0 ; }arch/riscv/kernel/smpboot.c +1
-1@@ -245,8 +245,8 @@ asmlinkage __visible void smp_callin(void)riscv_ipi_enable ();numa_add_cpu (curr_cpuid); -set_cpu_online (curr_cpuid,1 );check_unaligned_access (curr_cpuid); +set_cpu_online (curr_cpuid,1 );if (has_vector ()) {if (riscv_v_setup_vsize ())arch/riscv/kernel/traps_misaligned.c +25
-0@@ -14,6 +14,8 @@#include <asm/ptrace.h> #include <asm/csr.h> #include <asm/entry-common.h> +#include <asm/hwprobe.h> +#include <asm/cpufeature.h> #define INSN_MATCH_LB 0x3#define INSN_MASK_LB 0x707f@@ -396,6 +398,8 @@ union reg_data {u64 data_u64; }; +static bool unaligned_ctl__read_mostly ; +/* sysctl hooks */ int unaligned_enabled__read_mostly =1 ;/* Enabled by default */ @@ -409,6 +413,8 @@ int handle_misaligned_load(struct pt_regs *regs)perf_sw_event (PERF_COUNT_SW_ALIGNMENT_FAULTS ,1 , regs, addr); + *this_cpu_ptr (&misaligned_access_speed) =RISCV_HWPROBE_MISALIGNED_EMULATED ; +if (!unaligned_enabled)return -1 ;@@ -585,3 +591,53 @@ int handle_misaligned_store(struct pt_regs *regs)return 0 ; } + +bool check_unaligned_access_emulated (int cpu) +{ +long *mas_ptr =per_cpu_ptr (&misaligned_access_speed, cpu); +unsigned long tmp_var, tmp_val; +bool misaligned_emu_detected; + + *mas_ptr =RISCV_HWPROBE_MISALIGNED_UNKNOWN ; + + __asm__ __volatile__ ( +" " REG_L " %[tmp], 1(%[ptr])\n" + : [tmp]"=r" (tmp_val) : [ptr]"r" (&tmp_var) :"memory" ); + + misaligned_emu_detected = (*mas_ptr ==RISCV_HWPROBE_MISALIGNED_EMULATED ); +/* +* If unaligned_ctl is already set, this means that we detected that all +* CPUS uses emulated misaligned access at boot time. If that changed +* when hotplugging the new cpu, this is something we don't handle. +*/
Quoted 8 lines
arch/riscv/kernel/traps_misaligned.c int handle_misaligned_store(struct pt_regs *regs) · line 616
Quoted 4 lines
arch/riscv/kernel/traps_misaligned.c +4
-0+if (unlikely (unaligned_ctl && !misaligned_emu_detected)) { +pr_crit ("CPU misaligned accesses non homogeneous (expected all emulated)\n" ); +while (true) +cpu_relax ();So the idea is to spin long enough that the
wait_for_completion(&cpu_running, 1000ms)times out? Maybe there should be awfi()in here as well so we're not just burning white hot.
Hi Evan,
Yes the idea is to let the timeout fail. We could potentially add some shared state to report an error during bring up and thus not wait up to the timeout end. I'll check that.
Regarding th wfi(), the cpu_relax() call is actually meant to do that. On RISC-V implementation, it ends up on a pause if Zihintpause is available
Quoted 4 lines
Have you verified that if we get here, the CPU will also get taken back down after the timeout? I wonder if
__cpu_up()also needs a call to stop the CPU, in the case where thatwait_for_completion_timeout()times out.
I actually checked that it is not brought up but not the complete path to check if it is correctly brought down, I'll check that.
Quoted 4 lines
It also might be more intuitive to reorganize this such that the death loop happens in
smp_callin(), ascheck_unaligned_access_emulated()is not a function you'd expect might sometimes never return.
Yes, that makes sense.
Clément
Quoted 2 lines
-Evan
PATCH v2 8/8 riscv: add support for PR_SET_UNALIGN and PR_GET_UNALIGN
In reply to Clément LégerNow that trap support is ready to handle misalignment errors in S-mode, allow the user to control the behavior of misaligned accesses using prctl(PR_SET_UNALIGN). Add an align_ctl flag in thread_struct which will be used to determine if we should SIGBUS the process or not on such fault.
--- arch/riscv/include/asm/processor.h | 9 +++++++++ arch/riscv/kernel/process.c | 18 ++++++++++++++++++ arch/riscv/kernel/traps_misaligned.c | 6 ++++++ 3 files changed, 33 insertions(+)
arch/riscv/include/asm/processor.h +9 -0
@@ -8,6 +8,7 @@#include <linux/const.h> #include <linux/cache.h> +#include <linux/prctl.h> #include <vdso/processor.h>
@@ -82,6 +83,7 @@ struct thread_struct {unsigned long bad_cause;unsigned long vstate_ctrl;struct __riscv_v_ext_state vstate; +unsigned long align_ctl; };/* Whitelist the fstate from the task_struct for hardened usercopy */
@@ -94,6 +96,7 @@ static inline void arch_thread_struct_whitelist(unsigned long *offset,#define INIT_THREAD { \ .sp = sizeof(init_stack) + (long)&init_stack, \ + .align_ctl = PR_UNALIGN_NOPRINT, \ }#define task_pt_regs (tsk) \
@@ -134,6 +137,12 @@ extern long riscv_v_vstate_ctrl_set_current(unsigned long arg);extern long riscv_v_vstate_ctrl_get_current (void );#endif /* CONFIG_RISCV_ISA_V */ +extern int get_unalign_ctl (struct task_struct *tsk,unsigned long addr); +extern int set_unalign_ctl (struct task_struct *tsk,unsigned int val); + +#define GET_UNALIGN_CTL (tsk, addr) get_unalign_ctl((tsk), (addr)) +#define SET_UNALIGN_CTL (tsk, val) set_unalign_ctl((tsk), (val)) +#endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */
arch/riscv/kernel/process.c +18 -0
@@ -25,6 +25,7 @@#include <asm/thread_info.h> #include <asm/cpuidle.h> #include <asm/vector.h> +#include <asm/cpufeature.h> registerunsigned long gp_in_global __asm__("gp" );
@@ -41,6 +42,23 @@ void arch_cpu_idle(void)cpu_do_idle (); } +int set_unalign_ctl (struct task_struct *tsk,unsigned int val) +{ +if (!unaligned_ctl_available ()) +return -EINVAL ; + + tsk->thread.align_ctl = val; +return 0 ; +} + +int get_unalign_ctl (struct task_struct *tsk,unsigned long adr) +{ +if (!unaligned_ctl_available ()) +return -EINVAL ; + +return put_user (tsk->thread.align_ctl, (unsigned long __user *)adr); +} +void __show_regs (struct pt_regs *regs) {show_regs_print_info (KERN_DEFAULT );
arch/riscv/kernel/traps_misaligned.c +6 -0
@@ -418,6 +418,9 @@ int handle_misaligned_load(struct pt_regs *regs)if (!unaligned_enabled)return -1 ; +if (user_mode (regs) && (current->thread.align_ctl &PR_UNALIGN_SIGBUS )) +return -1 ; +if (get_insn (regs, epc, &insn))return -1 ;
@@ -517,6 +520,9 @@ int handle_misaligned_store(struct pt_regs *regs)if (!unaligned_enabled)return -1 ; +if (user_mode (regs) && (current->thread.align_ctl &PR_UNALIGN_SIGBUS )) +return -1 ; +if (get_insn (regs, epc, &insn))return -1 ;
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v2 8/8] riscv: add support for PR_SET_UNALIGN and PR_GET_UNALIGN
Quoted 7 lines · Clément Léger
Now that trap support is ready to handle misalignment errors in S-mode, allow the user to control the behavior of misaligned accesses using
prctl(PR_SET_UNALIGN). Add analign_ctlflag inthread_structwhich will be used to determine if we should SIGBUS the process or not on such fault.Signed-off-by Clément Léger <[email protected]>
This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:
Hello:
This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:
Quoted 8 lines · On Wed, 4 Oct 2023 17:13:57 +0200 you wrote:
Since commit 61cadb9 ("Provide new description of misaligned load/store behavior compatible with privileged architecture.") in the RISC-V ISA manual, it is stated that misaligned load/store might not be supported. However, the RISC-V kernel uABI describes that misaligned accesses are supported. In order to support that, this series adds support for S-mode handling of misaligned accesses as well support for
prctl(PR_UNALIGN).[...]
Here is the summary with links:
- [v2,1/8] riscv: remove unused functions in traps_misaligned.c
https://git.kernel.org/riscv/c/f19c3b4239f5 - [v2,2/8] riscv: add support for misaligned trap handling in S-mode https://git.kernel.org/riscv/c/7c83232161f6 - [v2,3/8] riscv: report perf event for misaligned fault https://git.kernel.org/riscv/c/89c12fecdc4d - [v2,4/8] riscv: add floating point insn support to misaligned access emulation https://git.kernel.org/riscv/c/7c586a555a48 - [v2,5/8] riscv: add support for sysctl unaligned_enabled control https://git.kernel.org/riscv/c/bc38f61313d3 - [v2,6/8] riscv: annotate check_unaligned_access_boot_cpu() with __init https://git.kernel.org/riscv/c/90b11b470b2e - [v2,7/8] riscv: report misaligned accesses emulation to hwprobe https://git.kernel.org/riscv/c/71c54b3d169d - [v2,8/8] riscv: add support for PR_SET_UNALIGN and PR_GET_UNALIGN https://git.kernel.org/riscv/c/9f23a5d2f6b0
You are awesome, thank you!
Signature · 9 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv