]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
arm64: Support systems without FP/ASIMD
authorSuzuki K Poulose <suzuki.poulose@arm.com>
Tue, 8 Nov 2016 13:56:21 +0000 (13:56 +0000)
committerCatalin Marinas <catalin.marinas@arm.com>
Wed, 16 Nov 2016 18:05:10 +0000 (18:05 +0000)
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>
arch/arm64/include/asm/cpufeature.h
arch/arm64/include/asm/neon.h
arch/arm64/kernel/cpufeature.c
arch/arm64/kernel/fpsimd.c
arch/arm64/kvm/handle_exit.c
arch/arm64/kvm/hyp/hyp-entry.S
arch/arm64/kvm/hyp/switch.c

index 8b63adb148e76b7855795b12683944a01b6060e9..0ef718b67c54bd031c657a7605e6c1edd51da805 100644 (file)
 #define ARM64_HAS_32BIT_EL0                    13
 #define ARM64_HYP_OFFSET_LOW                   14
 #define ARM64_MISMATCHED_CACHE_LINE_SIZE       15
+/*
+ * The macro below will be moved to asm/cpucaps.h together with the
+ * ARM64_NCAPS update.
+ */
+#define ARM64_HAS_NO_FPSIMD                    16
 
-#define ARM64_NCAPS                            16
+#define ARM64_NCAPS                            17
 
 #ifndef __ASSEMBLY__
 
@@ -231,6 +236,11 @@ static inline bool system_supports_mixed_endian_el0(void)
        return id_aa64mmfr0_mixed_endian_el0(read_system_reg(SYS_ID_AA64MMFR0_EL1));
 }
 
+static inline bool system_supports_fpsimd(void)
+{
+       return !cpus_have_const_cap(ARM64_HAS_NO_FPSIMD);
+}
+
 #endif /* __ASSEMBLY__ */
 
 #endif
index 13ce4cc18e268840036f0e7cfaa6f56fae52afa5..ad4cdc966c0f69ac524952caf996e8b4a99b14d2 100644 (file)
@@ -9,8 +9,9 @@
  */
 
 #include <linux/types.h>
+#include <asm/fpsimd.h>
 
-#define cpu_has_neon()         (1)
+#define cpu_has_neon()         system_supports_fpsimd()
 
 #define kernel_neon_begin()    kernel_neon_begin_partial(32)
 
index fc2bd1926607298d418721e29e11f906978fe9ad..f89385d794f6dfd6ac9164b405cd1c701147b146 100644 (file)
@@ -746,6 +746,14 @@ static bool hyp_offset_low(const struct arm64_cpu_capabilities *entry,
        return idmap_addr > GENMASK(VA_BITS - 2, 0) && !is_kernel_in_hyp_mode();
 }
 
+static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused)
+{
+       u64 pfr0 = read_system_reg(SYS_ID_AA64PFR0_EL1);
+
+       return cpuid_feature_extract_signed_field(pfr0,
+                                       ID_AA64PFR0_FP_SHIFT) < 0;
+}
+
 static const struct arm64_cpu_capabilities arm64_features[] = {
        {
                .desc = "GIC system register CPU interface",
@@ -829,6 +837,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
                .def_scope = SCOPE_SYSTEM,
                .matches = hyp_offset_low,
        },
+       {
+               /* FP/SIMD is not implemented */
+               .capability = ARM64_HAS_NO_FPSIMD,
+               .def_scope = SCOPE_SYSTEM,
+               .min_field_value = 0,
+               .matches = has_no_fpsimd,
+       },
        {},
 };
 
index 394c61db55666d72c1a7f45da4583efbb5b47107..b883f1f75216ae27a863e8c970efbc0284a43601 100644 (file)
@@ -127,6 +127,8 @@ void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
 
 void fpsimd_thread_switch(struct task_struct *next)
 {
+       if (!system_supports_fpsimd())
+               return;
        /*
         * Save the current FPSIMD state to memory, but only if whatever is in
         * the registers is in fact the most recent userland FPSIMD state of
@@ -157,6 +159,8 @@ void fpsimd_thread_switch(struct task_struct *next)
 
 void fpsimd_flush_thread(void)
 {
+       if (!system_supports_fpsimd())
+               return;
        memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
        fpsimd_flush_task_state(current);
        set_thread_flag(TIF_FOREIGN_FPSTATE);
@@ -168,6 +172,8 @@ void fpsimd_flush_thread(void)
  */
 void fpsimd_preserve_current_state(void)
 {
+       if (!system_supports_fpsimd())
+               return;
        preempt_disable();
        if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
                fpsimd_save_state(&current->thread.fpsimd_state);
@@ -181,6 +187,8 @@ void fpsimd_preserve_current_state(void)
  */
 void fpsimd_restore_current_state(void)
 {
+       if (!system_supports_fpsimd())
+               return;
        preempt_disable();
        if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
                struct fpsimd_state *st = &current->thread.fpsimd_state;
@@ -199,6 +207,8 @@ void fpsimd_restore_current_state(void)
  */
 void fpsimd_update_current_state(struct fpsimd_state *state)
 {
+       if (!system_supports_fpsimd())
+               return;
        preempt_disable();
        fpsimd_load_state(state);
        if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
@@ -228,6 +238,8 @@ static DEFINE_PER_CPU(struct fpsimd_partial_state, softirq_fpsimdstate);
  */
 void kernel_neon_begin_partial(u32 num_regs)
 {
+       if (WARN_ON(!system_supports_fpsimd()))
+               return;
        if (in_interrupt()) {
                struct fpsimd_partial_state *s = this_cpu_ptr(
                        in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
@@ -252,6 +264,8 @@ EXPORT_SYMBOL(kernel_neon_begin_partial);
 
 void kernel_neon_end(void)
 {
+       if (!system_supports_fpsimd())
+               return;
        if (in_interrupt()) {
                struct fpsimd_partial_state *s = this_cpu_ptr(
                        in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
index a204adf29f0a6dedd1c47485564086ef9682f82b..1bfe30dfbfe77ffa2395528e008c058bd93b648d 100644 (file)
@@ -57,6 +57,16 @@ static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run)
        return 1;
 }
 
+/*
+ * Guest access to FP/ASIMD registers are routed to this handler only
+ * when the system doesn't support FP/ASIMD.
+ */
+static int handle_no_fpsimd(struct kvm_vcpu *vcpu, struct kvm_run *run)
+{
+       kvm_inject_undefined(vcpu);
+       return 1;
+}
+
 /**
  * kvm_handle_wfx - handle a wait-for-interrupts or wait-for-event
  *                 instruction executed by a guest
@@ -144,6 +154,7 @@ static exit_handle_fn arm_exit_handlers[] = {
        [ESR_ELx_EC_BREAKPT_LOW]= kvm_handle_guest_debug,
        [ESR_ELx_EC_BKPT32]     = kvm_handle_guest_debug,
        [ESR_ELx_EC_BRK64]      = kvm_handle_guest_debug,
+       [ESR_ELx_EC_FP_ASIMD]   = handle_no_fpsimd,
 };
 
 static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
index 4e92399f71054347e064cab9f9610dc64bf4f86f..5e9052f087f289851108daf8c5521a9f2fe9c617 100644 (file)
@@ -106,9 +106,16 @@ el1_trap:
         * x0: ESR_EC
         */
 
-       /* Guest accessed VFP/SIMD registers, save host, restore Guest */
+       /*
+        * We trap the first access to the FP/SIMD to save the host context
+        * and restore the guest context lazily.
+        * If FP/SIMD is not implemented, handle the trap and inject an
+        * undefined instruction exception to the guest.
+        */
+alternative_if_not ARM64_HAS_NO_FPSIMD
        cmp     x0, #ESR_ELx_EC_FP_ASIMD
        b.eq    __fpsimd_guest_restore
+alternative_else_nop_endif
 
        mrs     x1, tpidr_el2
        mov     x0, #ARM_EXCEPTION_TRAP
index 83037cd62d013afa36b85788bccb757508ffcb00..8bcae7b1470455dee931c395842d50fd23a97bf4 100644 (file)
@@ -21,6 +21,7 @@
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_hyp.h>
+#include <asm/fpsimd.h>
 
 static bool __hyp_text __fpsimd_enabled_nvhe(void)
 {
@@ -76,9 +77,11 @@ static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
         * traps are only taken to EL2 if the operation would not otherwise
         * trap to EL1.  Therefore, always make sure that for 32-bit guests,
         * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
+        * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
+        * it will cause an exception.
         */
        val = vcpu->arch.hcr_el2;
-       if (!(val & HCR_RW)) {
+       if (!(val & HCR_RW) && system_supports_fpsimd()) {
                write_sysreg(1 << 30, fpexc32_el2);
                isb();
        }