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, SBI call for misaligned trap delegation as well prctl support for PR_SET_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 relies on a SBI extension [1] allowing to request delegation of the misaligned load/store traps to the S-mode software. This extension has been submitted for review to the riscv tech-prs group. An OpenSBI implementation for this spec is available at [2].
This series can be tested using the spike simulator [3] which allows to either handles misaligned access in hardware or let the software do it.
With hardware misaligned access support:
$ ./spike --misaligned --initrd=rootfs.cpio \ --kernel=arch/riscv/boot/Image opensbi_fw_jump.elf
Without hardware misaligned access support:
$ ./spike --initrd=rootfs.cpio --kernel=arch/riscv/boot/Image \ opensbi_fw_jump.elf
[1] https://lists.riscv.org/g/tech-prs/message/540
[2] https://github.com/rivosinc/opensbi/tree/dev/cleger/fw_feature_upstream
[3] https://github.com/riscv-software-src/riscv-isa-sim
Changes in V2:
- Fix prototypes declaration for handle_misaligned_load/store()
- Fix build for !CONFIG_FPU
- Added conditional build for various part of misaligned code handling
- Added in-kernel misalignement fault support to be equivalent to existing SBI support
- Added support for misalignment emulation detection for hwprobe reporting
- Modified set/get_unaligned_ctl to use this detection of emulation
- Added support for sysctl "unaligned_enabled"
Clément Léger (9):
riscv: remove unused functions in traps_misaligned.c
riscv: avoid missing prototypes warning
riscv: add support for misaligned handling in S-mode
riscv: report perf event for misaligned fault
riscv: add support for sysctl unaligned_enabled control
riscv: add support for SBI misalignment trap delegation
riscv: report misaligned accesses emulation to hwprobe
riscv: add support for PR_SET_UNALIGN and PR_GET_UNALIGN
riscv: add floating point insn support to misaligned access emulation
arch/riscv/Kconfig | 1 + arch/riscv/include/asm/cpufeature.h | 10 + arch/riscv/include/asm/entry-common.h | 3 + arch/riscv/include/asm/processor.h | 9 + arch/riscv/include/asm/sbi.h | 11 + arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/fpu.S | 117 +++++++++ arch/riscv/kernel/process.c | 18 ++ arch/riscv/kernel/sbi.c | 21 ++ arch/riscv/kernel/setup.c | 2 + arch/riscv/kernel/traps.c | 9 - arch/riscv/kernel/traps_misaligned.c | 352 ++++++++++++++++++++++---- 12 files changed, 498 insertions(+), 57 deletions(-)
Signature · 1 line
2.40.1
RFC PATCH v2 1/9 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.40.1
RFC PATCH v2 2/9 riscv: avoid missing prototypes warning
In reply to Clément LégerDeclare handle_misaligned_store/load() functions in entry-common.h and include that file in traps_misaligned.c file to avoid warnings.
--- arch/riscv/include/asm/entry-common.h | 3 +++ arch/riscv/kernel/traps.c | 2 -- arch/riscv/kernel/traps_misaligned.c | 1 + 3 files changed, 4 insertions(+), 2 deletions(-)
arch/riscv/include/asm/entry-common.h +3 -0
@@ -8,4 +8,7 @@void handle_page_fault (struct pt_regs *regs);void handle_break (struct pt_regs *regs); +int handle_misaligned_load (struct pt_regs *regs); +int handle_misaligned_store (struct pt_regs *regs); +#endif /* _ASM_RISCV_ENTRY_COMMON_H */
arch/riscv/kernel/traps.c +0 -2
@@ -155,8 +155,6 @@ DO_ERROR_INFO(do_trap_load_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) {
arch/riscv/kernel/traps_misaligned.c +1 -0
@@ -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
Signature · 1 line
2.40.1
Re: [RFC V2 PATCH 2/9] riscv: avoid missing prototypes warning
Quoted 15 lines · Clément Léger
Declare handle_misaligned_store/load() functions in
entry-common.hand include that file intraps_misaligned.cfile to avoid warnings.Signed-off-by Clément Léger <[email protected]>--- arch/riscv/include/asm/entry-common.h | 3 +++ arch/riscv/kernel/traps.c | 2 -- arch/riscv/kernel/traps_misaligned.c | 1 + 3 files changed, 4 insertions(+), 2 deletions(-)arch/riscv/include/asm/entry-common.h +0
-0
arch/riscv/include/asm/entry-common.h line 13
Quoted 6 lines
arch/riscv/include/asm/entry-common.h +3
-0@@ -8,4 +8,7 @@void handle_page_fault (struct pt_regs *regs);void handle_break (struct pt_regs *regs); +int handle_misaligned_load (struct pt_regs *regs); +int handle_misaligned_store (struct pt_regs *regs); +
Would this work when CONFIG_RISCV_M_MODE is disabled?
The handle_misaligned_load/store functions are only defined if CONFIG_RISCV_M_MODE is enabled. I would expect warnings if CONFIG_RISCV_M_MODE is siabled.
-Stafford
Quoted 29 lines
arch/riscv/include/asm/entry-common.h +0
-0#endif /* _ASM_RISCV_ENTRY_COMMON_H */ arch/riscv/kernel/traps.c +0
-2@@ -155,8 +155,6 @@ DO_ERROR_INFO(do_trap_load_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) {arch/riscv/kernel/traps_misaligned.c +1
-0@@ -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 0x707fSignature · 1 line
2.40.1
RFC PATCH v2 3/9 riscv: add support for misaligned handling in S-mode
In reply to Clément LégerMisalignment handling is only supported for M-mode and uses direct accesses to user memory. in S-mode, this requires to use the get_user()/put_user() accessors. Implement load_u8(), store_u8() and get_insn() using these accessors.
--- arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/traps.c | 7 -- arch/riscv/kernel/traps_misaligned.c | 118 ++++++++++++++++++++++++--- 3 files changed, 106 insertions(+), 21 deletions(-)
arch/riscv/kernel/Makefile +1 -1
@@ -55,10 +55,10 @@ obj-y += riscv_ksyms.o obj-y += stacktrace.o obj-y += cacheinfo.o obj-y += patch.o +obj-y += traps_misaligned.o obj-y += probes/ obj-$(CONFIG_MMU) += vdso.o vdso/ -obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o
arch/riscv/kernel/traps.c +0 -7
@@ -149,12 +149,6 @@ DO_ERROR_INFO(do_trap_insn_illegal, SIGILL, ILL_ILLOPC, "illegal instruction");DO_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 asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs) {
@@ -197,7 +191,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 +105 -13
171 lines not on this page · raw message
Signature · 1 line
2.40.1
Re: [RFC V2 PATCH 3/9] riscv: add support for misaligned handling in S-mode
Quoted 17 lines · Clément Léger
Misalignment handling is only supported for M-mode and uses direct accesses to user memory. in S-mode, this requires to use the get_user()/put_user() accessors. Implement
load_u8(),store_u8()andget_insn()using these accessors.Signed-off-by Clément Léger <[email protected]>--- arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/traps.c | 7 -- arch/riscv/kernel/traps_misaligned.c | 118 ++++++++++++++++++++++++--- 3 files changed, 106 insertions(+), 21 deletions(-)arch/riscv/kernel/Makefile +0
-0
arch/riscv/kernel/Makefile obj-y += riscv_ksyms.o · line 61
Quoted 8 lines
arch/riscv/kernel/Makefile +1
-1@@ -55,10 +55,10 @@ obj-y += riscv_ksyms.o obj-y += stacktrace.o obj-y += cacheinfo.o obj-y += patch.o +obj-y += traps_misaligned.o obj-y += probes/ obj-$(CONFIG_MMU) += vdso.o vdso/ -obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o
I think it would be better to combine this wath with "[PATCH 2/9] riscv: avoid missing prototypes warning" from the series to avoid the breakage on commit 2/9.
Also, it would be a bit more clear of the intent.
Quoted 152 lines
arch/riscv/kernel/Makefile +0
-0obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.oarch/riscv/kernel/traps.c +0
-7@@ -149,12 +149,6 @@ DO_ERROR_INFO(do_trap_insn_illegal, SIGILL, ILL_ILLOPC, "illegal instruction");DO_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 asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs) {@@ -197,7 +191,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 +87
-5@@ -152,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;@@ -195,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 ; +} +#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 ; + } } +static inline int store_u8 (struct pt_regs *regs,u8 *addr,u8 val) +{ +if (user_mode (regs)) { +return __put_user (val, addr); + }else { + *addr = val; +return 0 ; + } +} + +#define __read_insn (regs, insn, insn_addr) \ +({ \ + int __ret; \ + \ + if (user_mode(regs)) { \ + __ret = __get_user(insn, insn_addr); \ + } else { \ + insn = *insn_addr; \ + __ret = 0; \ + } \ + \ + __ret; \ +}) + +static inline int get_insn (struct pt_regs *regs,ulong epc,ulong *r_insn) +{ +ulong insn =0 ; + +if (epc &0x2 ) { +ulong tmp =0 ; +u16 __user *insn_addr = (u16 __user *)epc; + +if (__read_insn (regs, insn, insn_addr)) +return -EFAULT ; +/* __get_user() uses regular "lw" which sign extend the loaded +* value make sure to clear higher order bits in case we "or" it +* below with the upper 16 bits half. +*/ + insn &=GENMASK (15 ,0 ); +if ((insn & __INSN_LENGTH_MASK) != __INSN_LENGTH_32) { + *r_insn = insn; +return 0 ; + } + insn_addr++; +if (__read_insn (regs, tmp, insn_addr)) +return -EFAULT ; + *r_insn = (tmp <<16 ) | insn; + +return 0 ; + }else { +u32 __user *insn_addr = (u32 __user *)epc; + +if (__read_insn (regs, insn, insn_addr)) +return -EFAULT ; +if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) { + *r_insn = insn; +return 0 ; + } + insn &=GENMASK (15 ,0 ); + *r_insn = insn; + +return 0 ; + } +} +#endif +union reg_data {u8 data_bytes[8 ];ulong data_ulong;
arch/riscv/kernel/traps_misaligned.c int handle_misaligned_load(struct pt_regs *regs) · line 294
Quoted 7 lines
arch/riscv/kernel/traps_misaligned.c +2
-2@@ -208,10 +290,13 @@ int handle_misaligned_load(struct pt_regs *regs) {union reg_data val;unsigned long epc = regs->epc; -unsigned long insn =get_insn (epc); -unsigned long addr =csr_read (mtval); +unsigned long insn; +unsigned long addr = regs->badaddr;
Could your commit messages talk a bit about the change from using mtval to regs->badaddr? Will this sill work in M-mode? I think so, but it would be good to explain the change.
Quoted 53 lines
arch/riscv/kernel/traps_misaligned.c +16
-6int i, fp =0 , shift =0 , len =0 ; +if (get_insn (regs, epc, &insn)) +return -1 ; + regs->epc =0 ;if ((insn &INSN_MASK_LW ) ==INSN_MATCH_LW ) {@@ -275,8 +360,10 @@ int handle_misaligned_load(struct pt_regs *regs) } val.data_u64 =0 ; -for (i =0 ; i < len; i++) - val.data_bytes[i] =load_u8 ((void *)(addr + i)); +for (i =0 ; i < len; i++) { +if (load_u8 (regs, (void *)(addr + i), &val.data_bytes[i])) +return -1 ; + }if (fp)return -1 ;@@ -291,10 +378,13 @@ int handle_misaligned_store(struct pt_regs *regs) {union reg_data val;unsigned long epc = regs->epc; -unsigned long insn =get_insn (epc); -unsigned long addr =csr_read (mtval); +unsigned long insn; +unsigned long addr = regs->badaddr;int i, len =0 ; +if (get_insn (regs, epc, &insn)) +return -1 ; + regs->epc =0 ; val.data_ulong =GET_RS2 (insn, regs);@@ -328,8 +418,10 @@ int handle_misaligned_store(struct pt_regs *regs)return -1 ; } -for (i =0 ; i < len; i++) -store_u8 ((void *)(addr + i), val.data_bytes[i]); +for (i =0 ; i < len; i++) { +if (store_u8 (regs, (void *)(addr + i), val.data_bytes[i])) +return -1 ; + } regs->epc = epc +INSN_LEN (insn);Signature · 1 line
2.40.1
RFC PATCH v2 4/9 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 · 1 line
2.40.1
RFC PATCH v2 5/9 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
@@ -139,6 +139,7 @@ config RISCV select RISCV_TIMER if RISCV_SBI select SIFIVE_PLIC select SPARSE_IRQ + select SYSCTL_ARCH_UNALIGN_ALLOW select SYSCTL_EXCEPTION_TRACE select THREAD_INFO_IN_TASK select TRACE_IRQFLAGS_SUPPORT
arch/riscv/kernel/traps_misaligned.c +9 -0
@@ -287,6 +287,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;
@@ -297,6 +300,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 ;
@@ -387,6 +393,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 · 1 line
2.40.1
RFC PATCH v2 6/9 riscv: add support for SBI misalignment trap delegation
In reply to Clément LégerAdd support for misalignment trap delegation by setting it with SBI_EXT_FW_FEATURE SBI extension. This extension allows to control SBI behavior by requesting to set them to specific value. In order to implement prctl(PR_SET_UNALIGN, PR_UNALIGN_SIGBUS) behavior properly, we need to let the kernel handle the misalignment error by itself.
--- arch/riscv/include/asm/sbi.h | 11 +++++++++++ arch/riscv/kernel/sbi.c | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+)
arch/riscv/include/asm/sbi.h +11 -0
@@ -30,6 +30,7 @@ enum sbi_ext_id {SBI_EXT_HSM =0x48534D ,SBI_EXT_SRST =0x53525354 ,SBI_EXT_PMU =0x504D55 , +SBI_EXT_FW_FEATURE =0x46574654 ,/* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000,
@@ -236,6 +237,16 @@ enum sbi_pmu_ctr_type {/* Flags defined for counter stop function */ #define SBI_PMU_STOP_FLAG_RESET (1 << 0) +/* SBI function IDs for FW feature extension */ +#define SBI_EXT_FW_FEATURE_SET 0x0 +#define SBI_EXT_FW_FEATURE_GET 0x1 + +enum sbi_fw_features_t { +SBI_FW_FEATURE_MISALIGNED_DELEG =0 , + +SBI_FW_FEATURE_MAX , +}; +#define SBI_SPEC_VERSION_DEFAULT 0x1#define SBI_SPEC_VERSION_MAJOR_SHIFT 24#define SBI_SPEC_VERSION_MAJOR_MASK 0x7f
arch/riscv/kernel/sbi.c +21 -0
@@ -494,6 +494,16 @@ int sbi_remote_hfence_vvma_asid(const struct cpumask *cpu_mask, }EXPORT_SYMBOL (sbi_remote_hfence_vvma_asid); +static int sbi_fw_feature_set (enum sbi_fw_features_t feature,bool set) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_FW_FEATURE ,SBI_EXT_FW_FEATURE_SET , feature, + set,0 ,0 ,0 ,0 ); + +return sbi_err_map_linux_errno (ret.error); +} +static void sbi_srst_reset (unsigned long type,unsigned long reason) { sbi_ecall(SBI_EXT_SRST, SBI_EXT_SRST_RESET, type, reason,
@@ -624,6 +634,17 @@ void __init sbi_init(void) sbi_srst_reboot_nb.priority =192 ;register_restart_handler (&sbi_srst_reboot_nb); } +/* +* TODO: this will likely need to be updated when SBI extension +* is ratified +*/ +if ((sbi_spec_version >=sbi_mk_version (1 ,0 )) && + (sbi_probe_extension (SBI_EXT_FW_FEATURE ) >0 )) { +pr_info ("SBI FW_FEATURE extension detected\n" ); +/* Request misaligned handling delegation */ +sbi_fw_feature_set (SBI_FW_FEATURE_MISALIGNED_DELEG , + true); + } }else { __sbi_set_timer = __sbi_set_timer_v01; __sbi_send_ipi = __sbi_send_ipi_v01;
Signature · 1 line
2.40.1
RFC PATCH v2 7/9 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, if the SBI delegated us misaligned access handling, then 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 | 2 ++ arch/riscv/kernel/setup.c | 2 ++ arch/riscv/kernel/traps_misaligned.c | 32 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+)
arch/riscv/include/asm/cpufeature.h +2 -0
@@ -20,4 +20,6 @@ DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);DECLARE_PER_CPU (long, misaligned_access_speed); +void __init misaligned_emulation_init (void ); +#endif
arch/riscv/kernel/setup.c +2 -0
@@ -23,6 +23,7 @@#include <asm/alternative.h> #include <asm/cacheflush.h> +#include <asm/cpufeature.h> #include <asm/cpu_ops.h> #include <asm/early_ioremap.h> #include <asm/pgtable.h>
@@ -284,6 +285,7 @@ void __init setup_arch(char **cmdline_p)init_resources ();sbi_init (); +misaligned_emulation_init ();#ifdef CONFIG_KASAN kasan_init ();
arch/riscv/kernel/traps_misaligned.c +32 -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
@@ -441,3 +443,33 @@ int handle_misaligned_store(struct pt_regs *regs)return 0 ; } + +void __init misaligned_emulation_init (void ) +{ +int cpu; +unsigned long emulated =1 , tmp_var; + +/* Temporarily disable unaligned accesses support so that we fixup the +* exception for code below. +*/ + unaligned_enabled =0 ; + + __asm__ __volatile__ ( +"1:\n" +" ld %[tmp], 1(%[ptr])\n" +" li %[emulated], 0\n" +"2:\n" + _ASM_EXTABLE(1b, 2b) + : [emulated] "+r" (emulated), [tmp] "=r" (tmp_var) + : [ptr] "r" (&tmp_var) + : "memory" ); + + unaligned_enabled =1 ; +if (!emulated) +return ; + +for_each_possible_cpu (cpu) { +per_cpu (misaligned_access_speed, cpu) = +RISCV_HWPROBE_MISALIGNED_EMULATED ; + } +}
Signature · 1 line
2.40.1
kernel test robot
build botRe: [RFC V2 PATCH 7/9] riscv: report misaligned accesses emulation to hwprobe
kernel test robot noticed the following build errors:
Hi Clément,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on openrisc/for-next]
[also build test ERROR on v6.4]
[cannot apply to linus/master next-20230704]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Cl-ment-L-ger/riscv-remove-unused-functions-in-traps_misaligned-c/20230704-221341 base: https://github.com/openrisc/linux.git for-next patch link: https://lore.kernel.org/r/20230704140924.315594-8-cleger%40rivosinc.com patch subject: [RFC V2 PATCH 7/9] riscv: report misaligned accesses emulation to hwprobe config: riscv-rv32_defconfig (https://download.01.org/0day-ci/archive/20230705/[email protected]/config) compiler: riscv32-linux-gcc (GCC) 12.3.0 reproduce: (https://download.01.org/0day-ci/archive/20230705/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
arch/riscv/kernel/traps_misaligned.c: Assembler messages:
>> arch/riscv/kernel/traps_misaligned.c:458: Error: unrecognized opcode `ld a4,1(a4)'
vim +458 arch/riscv/kernel/traps_misaligned.c
446
447 void __init misaligned_emulation_init(void)
448 {
449 int cpu;
450 unsigned long emulated = 1, tmp_var;
451
452 /* Temporarily disable unaligned accesses support so that we fixup the
453 * exception for code below.
454 */
455 unaligned_enabled = 0;
456
457 __asm__ __volatile__ (
> 458 "1:\n"
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH v2 8/9 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 misalignment accesses using prctl(). 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/cpufeature.h | 8 ++++++++ arch/riscv/include/asm/processor.h | 9 +++++++++ arch/riscv/kernel/process.c | 18 ++++++++++++++++++ arch/riscv/kernel/traps_misaligned.c | 7 +++++++ 4 files changed, 42 insertions(+)
arch/riscv/include/asm/cpufeature.h +8 -0
@@ -6,6 +6,8 @@#ifndef _ASM_CPUFEATURE_H#define _ASM_CPUFEATURE_H +#include <asm/hwprobe.h> +/* * These are probed via a device_initcall(), via either the SBI or directly * from the corresponding CSRs.
@@ -20,6 +22,12 @@ DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);DECLARE_PER_CPU (long, misaligned_access_speed); +static inline bool misaligned_access_emulated (void ) +{ +return per_cpu (misaligned_access_speed,0 ) == +RISCV_HWPROBE_MISALIGNED_EMULATED ; +} +void __init misaligned_emulation_init (void );#endif
arch/riscv/include/asm/processor.h +9 -0
@@ -7,6 +7,7 @@#define _ASM_RISCV_PROCESSOR_H#include <linux/const.h> +#include <linux/prctl.h> #include <vdso/processor.h>
@@ -39,6 +40,7 @@ struct thread_struct {unsigned long s[12 ];/* s[0]: frame pointer */ struct __riscv_d_ext_state fstate;unsigned long bad_cause; +unsigned long align_ctl; };/* Whitelist the fstate from the task_struct for hardened usercopy */
@@ -51,6 +53,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) \
@@ -80,6 +83,12 @@ 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 int get_unalign_ctl (struct task_struct *,unsigned long addr); +extern int set_unalign_ctl (struct task_struct *,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
@@ -24,6 +24,7 @@#include <asm/switch_to.h> #include <asm/thread_info.h> #include <asm/cpuidle.h> +#include <asm/cpufeature.h> registerunsigned long gp_in_global __asm__("gp" );
@@ -40,6 +41,23 @@ void arch_cpu_idle(void)cpu_do_idle (); } +int set_unalign_ctl (struct task_struct *tsk,unsigned int val) +{ +if (!misaligned_access_emulated ()) +return -EINVAL ; + + tsk->thread.align_ctl = val; +return 0 ; +} + +int get_unalign_ctl (struct task_struct *tsk,unsigned long adr) +{ +if (!misaligned_access_emulated ()) +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 +7 -0
@@ -9,6 +9,7 @@#include <linux/perf_event.h> #include <linux/irq.h> #include <linux/stringify.h> +#include <linux/prctl.h> #include <asm/processor.h> #include <asm/ptrace.h>
@@ -305,6 +306,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 ;
@@ -398,6 +402,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 · 1 line
2.40.1
RFC PATCH v2 9/9 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 | 117 +++++++++++++++++++++ arch/riscv/kernel/traps_misaligned.c | 152 ++++++++++++++++++++++++++- 2 files changed, 265 insertions(+), 4 deletions(-)
arch/riscv/kernel/fpu.S +117 -0
@@ -104,3 +104,120 @@ 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) + + +/* + * 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(put_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)
arch/riscv/kernel/traps_misaligned.c +148 -4
193 lines not on this page · raw message
Signature · 1 line
2.40.1