fpsimd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers
  4. *
  5. * Copyright 2018 Arm Limited
  6. * Author: Dave Martin <Dave.Martin@arm.com>
  7. */
  8. #include <linux/irqflags.h>
  9. #include <linux/sched.h>
  10. #include <linux/thread_info.h>
  11. #include <linux/kvm_host.h>
  12. #include <asm/kvm_asm.h>
  13. #include <asm/kvm_host.h>
  14. #include <asm/kvm_mmu.h>
  15. #include <asm/sysreg.h>
  16. /*
  17. * Called on entry to KVM_RUN unless this vcpu previously ran at least
  18. * once and the most recent prior KVM_RUN for this vcpu was called from
  19. * the same task as current (highly likely).
  20. *
  21. * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu),
  22. * such that on entering hyp the relevant parts of current are already
  23. * mapped.
  24. */
  25. int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
  26. {
  27. int ret;
  28. struct thread_info *ti = &current->thread_info;
  29. struct user_fpsimd_state *fpsimd = &current->thread.uw.fpsimd_state;
  30. /*
  31. * Make sure the host task thread flags and fpsimd state are
  32. * visible to hyp:
  33. */
  34. ret = create_hyp_mappings(ti, ti + 1, PAGE_HYP);
  35. if (ret)
  36. goto error;
  37. ret = create_hyp_mappings(fpsimd, fpsimd + 1, PAGE_HYP);
  38. if (ret)
  39. goto error;
  40. vcpu->arch.host_thread_info = kern_hyp_va(ti);
  41. vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
  42. error:
  43. return ret;
  44. }
  45. /*
  46. * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
  47. * The actual loading is done by the FPSIMD access trap taken to hyp.
  48. *
  49. * Here, we just set the correct metadata to indicate that the FPSIMD
  50. * state in the cpu regs (if any) belongs to current on the host.
  51. *
  52. * TIF_SVE is backed up here, since it may get clobbered with guest state.
  53. * This flag is restored by kvm_arch_vcpu_put_fp(vcpu).
  54. */
  55. void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
  56. {
  57. BUG_ON(!current->mm);
  58. vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |
  59. KVM_ARM64_HOST_SVE_IN_USE |
  60. KVM_ARM64_HOST_SVE_ENABLED);
  61. vcpu->arch.flags |= KVM_ARM64_FP_HOST;
  62. if (test_thread_flag(TIF_SVE))
  63. vcpu->arch.flags |= KVM_ARM64_HOST_SVE_IN_USE;
  64. if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
  65. vcpu->arch.flags |= KVM_ARM64_HOST_SVE_ENABLED;
  66. }
  67. /*
  68. * If the guest FPSIMD state was loaded, update the host's context
  69. * tracking data mark the CPU FPSIMD regs as dirty and belonging to vcpu
  70. * so that they will be written back if the kernel clobbers them due to
  71. * kernel-mode NEON before re-entry into the guest.
  72. */
  73. void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
  74. {
  75. WARN_ON_ONCE(!irqs_disabled());
  76. if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
  77. fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.gp_regs.fp_regs);
  78. clear_thread_flag(TIF_FOREIGN_FPSTATE);
  79. clear_thread_flag(TIF_SVE);
  80. }
  81. }
  82. /*
  83. * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
  84. * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
  85. * disappears and another task or vcpu appears that recycles the same
  86. * struct fpsimd_state.
  87. */
  88. void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
  89. {
  90. unsigned long flags;
  91. local_irq_save(flags);
  92. if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
  93. /* Clean guest FP state to memory and invalidate cpu view */
  94. fpsimd_save();
  95. fpsimd_flush_cpu_state();
  96. } else if (system_supports_sve()) {
  97. /*
  98. * The FPSIMD/SVE state in the CPU has not been touched, and we
  99. * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
  100. * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
  101. * for EL0. To avoid spurious traps, restore the trap state
  102. * seen by kvm_arch_vcpu_load_fp():
  103. */
  104. if (vcpu->arch.flags & KVM_ARM64_HOST_SVE_ENABLED)
  105. sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
  106. else
  107. sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
  108. }
  109. update_thread_flag(TIF_SVE,
  110. vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE);
  111. local_irq_restore(flags);
  112. }