diff options
| author | Suzuki K Poulose <suzuki.poulose@arm.com> | 2016-11-08 08:56:21 -0500 |
|---|---|---|
| committer | Catalin Marinas <catalin.marinas@arm.com> | 2016-11-16 13:05:10 -0500 |
| commit | 82e0191a1aa11abfddb22c8944989b7735560efc (patch) | |
| tree | 192b21e7191e2385dc4a58f0562fff4a2f5f68b4 | |
| parent | a4023f682739439b434165b54af7cb3676a4766e (diff) | |
arm64: Support systems without FP/ASIMD
The arm64 kernel assumes that FP/ASIMD units are always present
and accesses the FP/ASIMD specific registers unconditionally. This
could cause problems when they are absent. This patch adds the
support for kernel handling systems without FP/ASIMD by skipping the
register access within the kernel. For kvm, we trap the accesses
to FP/ASIMD and inject an undefined instruction exception to the VM.
The callers of the exported kernel_neon_begin_partial() should
make sure that the FP/ASIMD is supported.
Cc: Will Deacon <will.deacon@arm.com>
Cc: Christoffer Dall <christoffer.dall@linaro.org>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
[catalin.marinas@arm.com: add comment on the ARM64_HAS_NO_FPSIMD conflict and the new location]
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
| -rw-r--r-- | arch/arm64/include/asm/cpufeature.h | 12 | ||||
| -rw-r--r-- | arch/arm64/include/asm/neon.h | 3 | ||||
| -rw-r--r-- | arch/arm64/kernel/cpufeature.c | 15 | ||||
| -rw-r--r-- | arch/arm64/kernel/fpsimd.c | 14 | ||||
| -rw-r--r-- | arch/arm64/kvm/handle_exit.c | 11 | ||||
| -rw-r--r-- | arch/arm64/kvm/hyp/hyp-entry.S | 9 | ||||
| -rw-r--r-- | arch/arm64/kvm/hyp/switch.c | 5 |
7 files changed, 65 insertions, 4 deletions
diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h index 8b63adb148e7..0ef718b67c54 100644 --- a/arch/arm64/include/asm/cpufeature.h +++ b/arch/arm64/include/asm/cpufeature.h | |||
| @@ -38,8 +38,13 @@ | |||
| 38 | #define ARM64_HAS_32BIT_EL0 13 | 38 | #define ARM64_HAS_32BIT_EL0 13 |
| 39 | #define ARM64_HYP_OFFSET_LOW 14 | 39 | #define ARM64_HYP_OFFSET_LOW 14 |
| 40 | #define ARM64_MISMATCHED_CACHE_LINE_SIZE 15 | 40 | #define ARM64_MISMATCHED_CACHE_LINE_SIZE 15 |
| 41 | /* | ||
| 42 | * The macro below will be moved to asm/cpucaps.h together with the | ||
| 43 | * ARM64_NCAPS update. | ||
| 44 | */ | ||
| 45 | #define ARM64_HAS_NO_FPSIMD 16 | ||
| 41 | 46 | ||
| 42 | #define ARM64_NCAPS 16 | 47 | #define ARM64_NCAPS 17 |
| 43 | 48 | ||
| 44 | #ifndef __ASSEMBLY__ | 49 | #ifndef __ASSEMBLY__ |
| 45 | 50 | ||
| @@ -231,6 +236,11 @@ static inline bool system_supports_mixed_endian_el0(void) | |||
| 231 | return id_aa64mmfr0_mixed_endian_el0(read_system_reg(SYS_ID_AA64MMFR0_EL1)); | 236 | return id_aa64mmfr0_mixed_endian_el0(read_system_reg(SYS_ID_AA64MMFR0_EL1)); |
| 232 | } | 237 | } |
| 233 | 238 | ||
| 239 | static inline bool system_supports_fpsimd(void) | ||
| 240 | { | ||
| 241 | return !cpus_have_const_cap(ARM64_HAS_NO_FPSIMD); | ||
| 242 | } | ||
| 243 | |||
| 234 | #endif /* __ASSEMBLY__ */ | 244 | #endif /* __ASSEMBLY__ */ |
| 235 | 245 | ||
| 236 | #endif | 246 | #endif |
diff --git a/arch/arm64/include/asm/neon.h b/arch/arm64/include/asm/neon.h index 13ce4cc18e26..ad4cdc966c0f 100644 --- a/arch/arm64/include/asm/neon.h +++ b/arch/arm64/include/asm/neon.h | |||
| @@ -9,8 +9,9 @@ | |||
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | #include <linux/types.h> | 11 | #include <linux/types.h> |
| 12 | #include <asm/fpsimd.h> | ||
| 12 | 13 | ||
| 13 | #define cpu_has_neon() (1) | 14 | #define cpu_has_neon() system_supports_fpsimd() |
| 14 | 15 | ||
| 15 | #define kernel_neon_begin() kernel_neon_begin_partial(32) | 16 | #define kernel_neon_begin() kernel_neon_begin_partial(32) |
| 16 | 17 | ||
diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index fc2bd1926607..f89385d794f6 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c | |||
| @@ -746,6 +746,14 @@ static bool hyp_offset_low(const struct arm64_cpu_capabilities *entry, | |||
| 746 | return idmap_addr > GENMASK(VA_BITS - 2, 0) && !is_kernel_in_hyp_mode(); | 746 | return idmap_addr > GENMASK(VA_BITS - 2, 0) && !is_kernel_in_hyp_mode(); |
| 747 | } | 747 | } |
| 748 | 748 | ||
| 749 | static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused) | ||
| 750 | { | ||
| 751 | u64 pfr0 = read_system_reg(SYS_ID_AA64PFR0_EL1); | ||
| 752 | |||
| 753 | return cpuid_feature_extract_signed_field(pfr0, | ||
| 754 | ID_AA64PFR0_FP_SHIFT) < 0; | ||
| 755 | } | ||
| 756 | |||
| 749 | static const struct arm64_cpu_capabilities arm64_features[] = { | 757 | static const struct arm64_cpu_capabilities arm64_features[] = { |
| 750 | { | 758 | { |
| 751 | .desc = "GIC system register CPU interface", | 759 | .desc = "GIC system register CPU interface", |
| @@ -829,6 +837,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = { | |||
| 829 | .def_scope = SCOPE_SYSTEM, | 837 | .def_scope = SCOPE_SYSTEM, |
| 830 | .matches = hyp_offset_low, | 838 | .matches = hyp_offset_low, |
| 831 | }, | 839 | }, |
| 840 | { | ||
| 841 | /* FP/SIMD is not implemented */ | ||
| 842 | .capability = ARM64_HAS_NO_FPSIMD, | ||
| 843 | .def_scope = SCOPE_SYSTEM, | ||
| 844 | .min_field_value = 0, | ||
| 845 | .matches = has_no_fpsimd, | ||
| 846 | }, | ||
| 832 | {}, | 847 | {}, |
| 833 | }; | 848 | }; |
| 834 | 849 | ||
diff --git a/arch/arm64/kernel/fpsimd.c b/arch/arm64/kernel/fpsimd.c index 394c61db5566..b883f1f75216 100644 --- a/arch/arm64/kernel/fpsimd.c +++ b/arch/arm64/kernel/fpsimd.c | |||
| @@ -127,6 +127,8 @@ void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs) | |||
| 127 | 127 | ||
| 128 | void fpsimd_thread_switch(struct task_struct *next) | 128 | void fpsimd_thread_switch(struct task_struct *next) |
| 129 | { | 129 | { |
| 130 | if (!system_supports_fpsimd()) | ||
| 131 | return; | ||
| 130 | /* | 132 | /* |
| 131 | * Save the current FPSIMD state to memory, but only if whatever is in | 133 | * Save the current FPSIMD state to memory, but only if whatever is in |
| 132 | * the registers is in fact the most recent userland FPSIMD state of | 134 | * the registers is in fact the most recent userland FPSIMD state of |
| @@ -157,6 +159,8 @@ void fpsimd_thread_switch(struct task_struct *next) | |||
| 157 | 159 | ||
| 158 | void fpsimd_flush_thread(void) | 160 | void fpsimd_flush_thread(void) |
| 159 | { | 161 | { |
| 162 | if (!system_supports_fpsimd()) | ||
| 163 | return; | ||
| 160 | memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state)); | 164 | memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state)); |
| 161 | fpsimd_flush_task_state(current); | 165 | fpsimd_flush_task_state(current); |
| 162 | set_thread_flag(TIF_FOREIGN_FPSTATE); | 166 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
| @@ -168,6 +172,8 @@ void fpsimd_flush_thread(void) | |||
| 168 | */ | 172 | */ |
| 169 | void fpsimd_preserve_current_state(void) | 173 | void fpsimd_preserve_current_state(void) |
| 170 | { | 174 | { |
| 175 | if (!system_supports_fpsimd()) | ||
| 176 | return; | ||
| 171 | preempt_disable(); | 177 | preempt_disable(); |
| 172 | if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) | 178 | if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) |
| 173 | fpsimd_save_state(¤t->thread.fpsimd_state); | 179 | fpsimd_save_state(¤t->thread.fpsimd_state); |
| @@ -181,6 +187,8 @@ void fpsimd_preserve_current_state(void) | |||
| 181 | */ | 187 | */ |
| 182 | void fpsimd_restore_current_state(void) | 188 | void fpsimd_restore_current_state(void) |
| 183 | { | 189 | { |
| 190 | if (!system_supports_fpsimd()) | ||
| 191 | return; | ||
| 184 | preempt_disable(); | 192 | preempt_disable(); |
| 185 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { | 193 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 186 | struct fpsimd_state *st = ¤t->thread.fpsimd_state; | 194 | struct fpsimd_state *st = ¤t->thread.fpsimd_state; |
| @@ -199,6 +207,8 @@ void fpsimd_restore_current_state(void) | |||
| 199 | */ | 207 | */ |
| 200 | void fpsimd_update_current_state(struct fpsimd_state *state) | 208 | void fpsimd_update_current_state(struct fpsimd_state *state) |
| 201 | { | 209 | { |
| 210 | if (!system_supports_fpsimd()) | ||
| 211 | return; | ||
| 202 | preempt_disable(); | 212 | preempt_disable(); |
| 203 | fpsimd_load_state(state); | 213 | fpsimd_load_state(state); |
| 204 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { | 214 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| @@ -228,6 +238,8 @@ static DEFINE_PER_CPU(struct fpsimd_partial_state, softirq_fpsimdstate); | |||
| 228 | */ | 238 | */ |
| 229 | void kernel_neon_begin_partial(u32 num_regs) | 239 | void kernel_neon_begin_partial(u32 num_regs) |
| 230 | { | 240 | { |
| 241 | if (WARN_ON(!system_supports_fpsimd())) | ||
| 242 | return; | ||
| 231 | if (in_interrupt()) { | 243 | if (in_interrupt()) { |
| 232 | struct fpsimd_partial_state *s = this_cpu_ptr( | 244 | struct fpsimd_partial_state *s = this_cpu_ptr( |
| 233 | in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate); | 245 | in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate); |
| @@ -252,6 +264,8 @@ EXPORT_SYMBOL(kernel_neon_begin_partial); | |||
| 252 | 264 | ||
| 253 | void kernel_neon_end(void) | 265 | void kernel_neon_end(void) |
| 254 | { | 266 | { |
| 267 | if (!system_supports_fpsimd()) | ||
| 268 | return; | ||
| 255 | if (in_interrupt()) { | 269 | if (in_interrupt()) { |
| 256 | struct fpsimd_partial_state *s = this_cpu_ptr( | 270 | struct fpsimd_partial_state *s = this_cpu_ptr( |
| 257 | in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate); | 271 | in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate); |
diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index a204adf29f0a..1bfe30dfbfe7 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c | |||
| @@ -57,6 +57,16 @@ static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run) | |||
| 57 | return 1; | 57 | return 1; |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | /* | ||
| 61 | * Guest access to FP/ASIMD registers are routed to this handler only | ||
| 62 | * when the system doesn't support FP/ASIMD. | ||
| 63 | */ | ||
| 64 | static int handle_no_fpsimd(struct kvm_vcpu *vcpu, struct kvm_run *run) | ||
| 65 | { | ||
| 66 | kvm_inject_undefined(vcpu); | ||
| 67 | return 1; | ||
| 68 | } | ||
| 69 | |||
| 60 | /** | 70 | /** |
| 61 | * kvm_handle_wfx - handle a wait-for-interrupts or wait-for-event | 71 | * kvm_handle_wfx - handle a wait-for-interrupts or wait-for-event |
| 62 | * instruction executed by a guest | 72 | * instruction executed by a guest |
| @@ -144,6 +154,7 @@ static exit_handle_fn arm_exit_handlers[] = { | |||
| 144 | [ESR_ELx_EC_BREAKPT_LOW]= kvm_handle_guest_debug, | 154 | [ESR_ELx_EC_BREAKPT_LOW]= kvm_handle_guest_debug, |
| 145 | [ESR_ELx_EC_BKPT32] = kvm_handle_guest_debug, | 155 | [ESR_ELx_EC_BKPT32] = kvm_handle_guest_debug, |
| 146 | [ESR_ELx_EC_BRK64] = kvm_handle_guest_debug, | 156 | [ESR_ELx_EC_BRK64] = kvm_handle_guest_debug, |
| 157 | [ESR_ELx_EC_FP_ASIMD] = handle_no_fpsimd, | ||
| 147 | }; | 158 | }; |
| 148 | 159 | ||
| 149 | static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu) | 160 | static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu) |
diff --git a/arch/arm64/kvm/hyp/hyp-entry.S b/arch/arm64/kvm/hyp/hyp-entry.S index 4e92399f7105..5e9052f087f2 100644 --- a/arch/arm64/kvm/hyp/hyp-entry.S +++ b/arch/arm64/kvm/hyp/hyp-entry.S | |||
| @@ -106,9 +106,16 @@ el1_trap: | |||
| 106 | * x0: ESR_EC | 106 | * x0: ESR_EC |
| 107 | */ | 107 | */ |
| 108 | 108 | ||
| 109 | /* Guest accessed VFP/SIMD registers, save host, restore Guest */ | 109 | /* |
| 110 | * We trap the first access to the FP/SIMD to save the host context | ||
| 111 | * and restore the guest context lazily. | ||
| 112 | * If FP/SIMD is not implemented, handle the trap and inject an | ||
| 113 | * undefined instruction exception to the guest. | ||
| 114 | */ | ||
| 115 | alternative_if_not ARM64_HAS_NO_FPSIMD | ||
| 110 | cmp x0, #ESR_ELx_EC_FP_ASIMD | 116 | cmp x0, #ESR_ELx_EC_FP_ASIMD |
| 111 | b.eq __fpsimd_guest_restore | 117 | b.eq __fpsimd_guest_restore |
| 118 | alternative_else_nop_endif | ||
| 112 | 119 | ||
| 113 | mrs x1, tpidr_el2 | 120 | mrs x1, tpidr_el2 |
| 114 | mov x0, #ARM_EXCEPTION_TRAP | 121 | mov x0, #ARM_EXCEPTION_TRAP |
diff --git a/arch/arm64/kvm/hyp/switch.c b/arch/arm64/kvm/hyp/switch.c index 83037cd62d01..8bcae7b14704 100644 --- a/arch/arm64/kvm/hyp/switch.c +++ b/arch/arm64/kvm/hyp/switch.c | |||
| @@ -21,6 +21,7 @@ | |||
| 21 | #include <asm/kvm_asm.h> | 21 | #include <asm/kvm_asm.h> |
| 22 | #include <asm/kvm_emulate.h> | 22 | #include <asm/kvm_emulate.h> |
| 23 | #include <asm/kvm_hyp.h> | 23 | #include <asm/kvm_hyp.h> |
| 24 | #include <asm/fpsimd.h> | ||
| 24 | 25 | ||
| 25 | static bool __hyp_text __fpsimd_enabled_nvhe(void) | 26 | static bool __hyp_text __fpsimd_enabled_nvhe(void) |
| 26 | { | 27 | { |
| @@ -76,9 +77,11 @@ static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu) | |||
| 76 | * traps are only taken to EL2 if the operation would not otherwise | 77 | * traps are only taken to EL2 if the operation would not otherwise |
| 77 | * trap to EL1. Therefore, always make sure that for 32-bit guests, | 78 | * trap to EL1. Therefore, always make sure that for 32-bit guests, |
| 78 | * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit. | 79 | * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit. |
| 80 | * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to | ||
| 81 | * it will cause an exception. | ||
| 79 | */ | 82 | */ |
| 80 | val = vcpu->arch.hcr_el2; | 83 | val = vcpu->arch.hcr_el2; |
| 81 | if (!(val & HCR_RW)) { | 84 | if (!(val & HCR_RW) && system_supports_fpsimd()) { |
| 82 | write_sysreg(1 << 30, fpexc32_el2); | 85 | write_sysreg(1 << 30, fpexc32_el2); |
| 83 | isb(); | 86 | isb(); |
| 84 | } | 87 | } |
