From: Arnd Bergmann I/O devices that understand the st64bv0 instruction for doing shared virtual access from userspace need to set the pasid value in the accdata_el1 register, but in general we want this to be disabled unless a driver actually hands an MMIO area to userspace that accepts these commands and installs a pasid using iommu_sva_bind_device(). Rather than trying to find all threads that use an mm_struct by the time the pasid created, iommu_sva_bind_device() only stores the pasid in 'struct iommu_mm_data' and sets up the iommu domain in the device. The first attempt to use SVA from userspace traps into do_el0_other(), which checks that this is indeed valid and then sets the configured pasid before enabling SCTLR_EL1_EnAS0. During fork() and exec(), the mm->iommu_mm is cleaned up, so SCTLR_EL1_EnAS0 has to get turned off as well. If a process with accdata set enters suspend, restore the value when waking up in case the firmware lost it. The design is roughly based on what x86 does with try_fixup_enqcmd_gp(). Signed-off-by: Arnd Bergmann --- arch/arm64/Kconfig | 13 ++++++ arch/arm64/include/asm/cpufeature.h | 5 +++ arch/arm64/include/asm/el2_setup.h | 12 ++++++ arch/arm64/include/asm/exception.h | 1 + arch/arm64/include/asm/processor.h | 2 +- arch/arm64/include/asm/sysreg.h | 2 - arch/arm64/kernel/entry-common.c | 11 +++++ arch/arm64/kernel/process.c | 42 +++++++++++++++++++ arch/arm64/kernel/suspend.c | 19 +++++++++ arch/arm64/kernel/traps.c | 63 +++++++++++++++++++++++++++++ arch/arm64/tools/sysreg | 5 +++ 11 files changed, 172 insertions(+), 3 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 964a816b56b2..a715497de76b 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2324,6 +2324,19 @@ config ARM64_EPAN The feature is detected at runtime, and will remain disabled if the cpu does not implement the feature. + +config ARM64_LS64_ACCDATA + bool "Enable support for 64-byte EL0 stores with status (LS64_ACCDATA)" + depends on IOMMU_SVA + default y + help + 64-byte EL0 stores with status use the st64bv0 instruction to + initiate a transaction on an I/O device with shared virtual + addressing. + + The feature is detected at runtime, and will remain disabled + if the cpu does not implement the feature. + endmenu # "ARMv8.7 architectural features" config AS_HAS_MOPS diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h index 7d7b8eb7f72f..e5367344e817 100644 --- a/arch/arm64/include/asm/cpufeature.h +++ b/arch/arm64/include/asm/cpufeature.h @@ -914,6 +914,11 @@ static inline bool system_supports_bbml3(void) return alternative_has_cap_unlikely(ARM64_HAS_BBML3); } +static inline bool system_supports_ls64_accdata(void) +{ + return cpus_have_final_cap(ARM64_HAS_LS64_ACCDATA); +} + int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt); bool try_emulate_mrs(struct pt_regs *regs, u32 isn); diff --git a/arch/arm64/include/asm/el2_setup.h b/arch/arm64/include/asm/el2_setup.h index 12a500181291..fd6e06bdacfb 100644 --- a/arch/arm64/include/asm/el2_setup.h +++ b/arch/arm64/include/asm/el2_setup.h @@ -93,6 +93,9 @@ cmp x1, #ID_AA64ISAR1_EL1_LS64_LS64_V b.lt .Lset_hcrx_\@ orr x0, x0, #HCRX_EL2_EnASR + cmp x1, #ID_AA64ISAR1_EL1_LS64_LS64_ACCDATA + b.lt .Lset_hcrx_\@ + orr x0, x0, #HCRX_EL2_EnAS0 .Lset_hcrx_\@: msr_s SYS_HCRX_EL2, x0 @@ -394,6 +397,15 @@ orr x0, x0, #HFGRTR_EL2_nGCS_EL0_MASK .Lskip_gce_fgt_\@: + mrs_s x1, SYS_ID_AA64ISAR1_EL1 + ubfx x1, x1, #ID_AA64ISAR1_EL1_LS64_SHIFT, #4 + cmp x1, #ID_AA64ISAR1_EL1_LS64_LS64_ACCDATA + b.lt .Lskip_ls64_accdata_\@ + + /* Disable traps of access to ACCDATA_EL1 register */ + orr x0, x0, #HFGRTR_EL2_nACCDATA_EL1 + +.Lskip_ls64_accdata_\@: .Lset_fgt_\@: msr_s SYS_HFGRTR_EL2, x0 diff --git a/arch/arm64/include/asm/exception.h b/arch/arm64/include/asm/exception.h index a2da3cb21c24..7c61b50718d6 100644 --- a/arch/arm64/include/asm/exception.h +++ b/arch/arm64/include/asm/exception.h @@ -81,6 +81,7 @@ void do_el0_sys(unsigned long esr, struct pt_regs *regs); void do_sp_pc_abort(unsigned long addr, unsigned long esr, struct pt_regs *regs); void bad_el0_sync(struct pt_regs *regs, int reason, unsigned long esr); void do_el0_cp15(unsigned long esr, struct pt_regs *regs); +void do_el0_other(struct pt_regs *regs, unsigned long esr); int do_compat_alignment_fixup(unsigned long addr, struct pt_regs *regs); void do_el0_svc(struct pt_regs *regs); void do_el0_svc_compat(struct pt_regs *regs); diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index 481f1999f7c1..bf5ea7bc871a 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -264,7 +264,7 @@ static inline void task_set_sve_vl_onexec(struct task_struct *task, #define SCTLR_USER_MASK \ (SCTLR_ELx_ENIA | SCTLR_ELx_ENIB | SCTLR_ELx_ENDA | SCTLR_ELx_ENDB | \ - SCTLR_EL1_TCF0_MASK | SCTLR_EL1_TCSO0_MASK) + SCTLR_EL1_TCF0_MASK | SCTLR_EL1_TCSO0_MASK | SCTLR_EL1_EnAS0_MASK) static inline void arch_thread_struct_whitelist(unsigned long *offset, unsigned long *size) diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h index 7cba1216efd2..2bd2fda564d3 100644 --- a/arch/arm64/include/asm/sysreg.h +++ b/arch/arm64/include/asm/sysreg.h @@ -392,8 +392,6 @@ #define SYS_ICC_IGRPEN0_EL1 sys_reg(3, 0, 12, 12, 6) #define SYS_ICC_IGRPEN1_EL1 sys_reg(3, 0, 12, 12, 7) -#define SYS_ACCDATA_EL1 sys_reg(3, 0, 13, 0, 5) - #define SYS_CNTKCTL_EL1 sys_reg(3, 0, 14, 1, 0) #define SYS_AIDR_EL1 sys_reg(3, 1, 0, 0, 7) diff --git a/arch/arm64/kernel/entry-common.c b/arch/arm64/kernel/entry-common.c index beb56891fc4f..03ecc73c44a4 100644 --- a/arch/arm64/kernel/entry-common.c +++ b/arch/arm64/kernel/entry-common.c @@ -807,6 +807,14 @@ static void noinstr el0_fpac(struct pt_regs *regs, unsigned long esr) arm64_exit_to_user_mode(regs); } +static void noinstr el0_other(struct pt_regs *regs, unsigned long esr) +{ + arm64_enter_from_user_mode(regs); + local_daif_restore(DAIF_PROCCTX); + do_el0_other(regs, esr); + arm64_exit_to_user_mode(regs); +} + asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs) { unsigned long esr = read_sysreg(esr_el1); @@ -870,6 +878,9 @@ asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs) case ESR_ELx_EC_FPAC: el0_fpac(regs, esr); break; + case ESR_ELx_EC_OTHER: + el0_other(regs, esr); + break; default: el0_inv(regs, esr); } diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index 42b15b0c069f..997683ea6e79 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -340,6 +341,44 @@ static int copy_thread_gcs(struct task_struct *p, #endif +#ifdef CONFIG_ARM64_LS64_ACCDATA +static void flush_ls64_accdata(void) +{ + if (!system_supports_ls64_accdata() || + !(current->thread.sctlr_user & SCTLR_EL1_EnAS0)) + return; + + preempt_disable(); + current->thread.sctlr_user &= ~SCTLR_EL1_EnAS0; + update_sctlr_el1(current->thread.sctlr_user); + preempt_enable(); +} + +static void copy_thread_ls64_accdata(struct task_struct *p) +{ + /* Turn off ls64_accdata for new thread */ + p->thread.sctlr_user &= ~SCTLR_EL1_EnAS0; +} + +/* switch PASID if new task has enabled st64bv0 */ +static void ls64_accdata_thread_switch(struct task_struct *next) +{ + u32 pasid; + + if (!(next->thread.sctlr_user & SCTLR_EL1_EnAS0)) + return; + + pasid = mm_get_enqcmd_pasid(next->mm); + if (pasid != IOMMU_PASID_INVALID) + write_sysreg_s(pasid, SYS_ACCDATA_EL1); +} + +#else +static void flush_ls64_accdata(void) { } +static void copy_thread_ls64_accdata(struct task_struct *p) { } +static void ls64_accdata_thread_switch(struct task_struct *next) { } +#endif + void flush_thread(void) { fpsimd_flush_thread(); @@ -348,6 +387,7 @@ void flush_thread(void) flush_tagged_addr_state(); flush_poe(); flush_gcs(); + flush_ls64_accdata(); } void exit_thread(struct task_struct *tsk) @@ -524,6 +564,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) if (system_supports_poe()) p->thread.por_el0 = POR_EL0_INIT; } + copy_thread_ls64_accdata(p); p->thread.cpu_context.pc = (unsigned long)ret_from_fork; p->thread.cpu_context.sp = (unsigned long)childregs; /* @@ -762,6 +803,7 @@ struct task_struct *__switch_to(struct task_struct *prev, ptrauth_thread_switch_user(next); permission_overlay_switch(next); gcs_thread_switch(next); + ls64_accdata_thread_switch(next); /* * Complete any pending TLB or cache maintenance on this CPU in case the diff --git a/arch/arm64/kernel/suspend.c b/arch/arm64/kernel/suspend.c index 4c281ae6babe..1dcd7b59cc4d 100644 --- a/arch/arm64/kernel/suspend.c +++ b/arch/arm64/kernel/suspend.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include #include @@ -42,6 +43,21 @@ void __init cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int)) hw_breakpoint_restore = hw_bp_restore; } +static void ls64_accdata_suspend_exit(void) +{ + u32 pasid; + + if (!IS_ENABLED(CONFIG_ARM64_LS64_ACCDATA) || + !system_supports_ls64_accdata()) + return; + + /* if current thread has accdata enabled, restore the pasid value */ + if (current->thread.sctlr_user & SCTLR_EL1_EnAS0) { + pasid = mm_get_enqcmd_pasid(current->mm); + write_sysreg_s(pasid, SYS_ACCDATA_EL1); + } +} + void notrace __cpu_suspend_exit(void) { unsigned int cpu = smp_processor_id(); @@ -86,6 +102,9 @@ void notrace __cpu_suspend_exit(void) /* Restore additional feature-specific configuration */ ptrauth_suspend_exit(); + + /* Restore accdata for st64bv0 */ + ls64_accdata_suspend_exit(); } /* diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c index d6391ab20db3..2ba9344c3560 100644 --- a/arch/arm64/kernel/traps.c +++ b/arch/arm64/kernel/traps.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -543,6 +544,67 @@ void do_el1_mops(struct pt_regs *regs, unsigned long esr) kernel_fastforward_single_step(regs); } +/* + * PASID/ACCDATA is shared by the entire mm, while sctlr_el1 + * is maintained per thread. Keep things simple and enable + * ACCDATA dynamically on first access if the feature is + * available and a valid pasid is set. + * + * After exiting back to userspace, the st64bv0 instruction + * should no longer trap. + */ +static bool try_enable_st64bv0(void) +{ + u32 pasid; + u64 sctlr; + + /* hardware support is enabled, kernel support is not */ + if (!IS_ENABLED(CONFIG_ARM64_LS64_ACCDATA)) + return false; + + /* unexpectedly already enabled, signal bus error */ + if (current->thread.sctlr_user & SCTLR_EL1_EnAS0) + return false; + + /* no PASID set, disallow */ + pasid = mm_get_enqcmd_pasid(current->mm); + if (pasid == IOMMU_PASID_INVALID) + return false; + + preempt_disable(); + write_sysreg_s(pasid, SYS_ACCDATA_EL1); + sctlr = current->thread.sctlr_user; + sctlr |= SCTLR_EL1_EnAS0; + current->thread.sctlr_user = sctlr; + update_sctlr_el1(sctlr); + preempt_enable(); + + return true; +} + +void do_el0_other(struct pt_regs *regs, unsigned long esr) +{ + switch (ESR_ELx_ISS(esr)) { + case ESR_ELx_ISS_OTHER_ST64BV0: + if (!system_supports_ls64_accdata()) + break; + + if (try_enable_st64bv0()) + return; + + force_signal_inject(SIGBUS, BUS_OBJERR, regs->pc, 0); + return; + + case ESR_ELx_ISS_OTHER_ST64BV: + case ESR_ELx_ISS_OTHER_LDST64B: + default: + break; + } + + /* Treat all other "other" trapping instructions as illegal opcode */ + force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0); +} + #define __user_cache_maint(insn, address, res) \ if (address >= TASK_SIZE_MAX) { \ res = -EFAULT; \ @@ -842,6 +904,7 @@ static const char *esr_class_str[] = { [ESR_ELx_EC_FP_ASIMD] = "ASIMD", [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS", [ESR_ELx_EC_PAC] = "PAC", + [ESR_ELx_EC_OTHER] = "OTHER", [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC", [ESR_ELx_EC_BTI] = "BTI", [ESR_ELx_EC_ILL] = "PSTATE.IL", diff --git a/arch/arm64/tools/sysreg b/arch/arm64/tools/sysreg index 28d319e63d8f..7612bacdb018 100644 --- a/arch/arm64/tools/sysreg +++ b/arch/arm64/tools/sysreg @@ -3707,6 +3707,11 @@ Sysreg TPIDR_EL1 3 0 13 0 4 Field 63:0 ThreadID EndSysreg +Sysreg ACCDATA_EL1 3 0 13 0 5 +Res0 63:32 +Field 31:0 ACCDATA +EndSysreg + Sysreg RCWMASK_EL1 3 0 13 0 6 Field 63:0 RCWMASK EndSysreg -- 2.53.0