fpsimd.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <[email protected]>
  7. */
  8. #include <linux/irqflags.h>
  9. #include <linux/sched.h>
  10. #include <linux/kvm_host.h>
  11. #include <asm/fpsimd.h>
  12. #include <asm/kvm_asm.h>
  13. #include <asm/kvm_hyp.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. struct user_fpsimd_state *fpsimd = &current->thread.uw.fpsimd_state;
  28. int ret;
  29. /* pKVM has its own tracking of the host fpsimd state. */
  30. if (is_protected_kvm_enabled())
  31. return 0;
  32. /* Make sure the host task fpsimd state is visible to hyp: */
  33. ret = kvm_share_hyp(fpsimd, fpsimd + 1);
  34. if (ret)
  35. return ret;
  36. vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
  37. return 0;
  38. }
  39. /*
  40. * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
  41. * The actual loading is done by the FPSIMD access trap taken to hyp.
  42. *
  43. * Here, we just set the correct metadata to indicate that the FPSIMD
  44. * state in the cpu regs (if any) belongs to current on the host.
  45. */
  46. void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
  47. {
  48. BUG_ON(!current->mm);
  49. BUG_ON(test_thread_flag(TIF_SVE));
  50. if (!system_supports_fpsimd())
  51. return;
  52. vcpu->arch.fp_state = FP_STATE_HOST_OWNED;
  53. vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);
  54. if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
  55. vcpu_set_flag(vcpu, HOST_SVE_ENABLED);
  56. /*
  57. * We don't currently support SME guests but if we leave
  58. * things in streaming mode then when the guest starts running
  59. * FPSIMD or SVE code it may generate SME traps so as a
  60. * special case if we are in streaming mode we force the host
  61. * state to be saved now and exit streaming mode so that we
  62. * don't have to handle any SME traps for valid guest
  63. * operations. Do this for ZA as well for now for simplicity.
  64. */
  65. if (system_supports_sme()) {
  66. vcpu_clear_flag(vcpu, HOST_SME_ENABLED);
  67. if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN)
  68. vcpu_set_flag(vcpu, HOST_SME_ENABLED);
  69. if (read_sysreg_s(SYS_SVCR) & (SVCR_SM_MASK | SVCR_ZA_MASK)) {
  70. vcpu->arch.fp_state = FP_STATE_FREE;
  71. fpsimd_save_and_flush_cpu_state();
  72. }
  73. }
  74. }
  75. /*
  76. * Called just before entering the guest once we are no longer preemptable
  77. * and interrupts are disabled. If we have managed to run anything using
  78. * FP while we were preemptible (such as off the back of an interrupt),
  79. * then neither the host nor the guest own the FP hardware (and it was the
  80. * responsibility of the code that used FP to save the existing state).
  81. */
  82. void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
  83. {
  84. if (test_thread_flag(TIF_FOREIGN_FPSTATE))
  85. vcpu->arch.fp_state = FP_STATE_FREE;
  86. }
  87. /*
  88. * Called just after exiting the guest. If the guest FPSIMD state
  89. * was loaded, update the host's context tracking data mark the CPU
  90. * FPSIMD regs as dirty and belonging to vcpu so that they will be
  91. * written back if the kernel clobbers them due to kernel-mode NEON
  92. * before re-entry into the guest.
  93. */
  94. void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
  95. {
  96. WARN_ON_ONCE(!irqs_disabled());
  97. if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
  98. /*
  99. * Currently we do not support SME guests so SVCR is
  100. * always 0 and we just need a variable to point to.
  101. */
  102. fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.fp_regs,
  103. vcpu->arch.sve_state,
  104. vcpu->arch.sve_max_vl,
  105. NULL, 0, &vcpu->arch.svcr);
  106. clear_thread_flag(TIF_FOREIGN_FPSTATE);
  107. update_thread_flag(TIF_SVE, vcpu_has_sve(vcpu));
  108. }
  109. }
  110. /*
  111. * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
  112. * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
  113. * disappears and another task or vcpu appears that recycles the same
  114. * struct fpsimd_state.
  115. */
  116. void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
  117. {
  118. unsigned long flags;
  119. local_irq_save(flags);
  120. /*
  121. * If we have VHE then the Hyp code will reset CPACR_EL1 to
  122. * CPACR_EL1_DEFAULT and we need to reenable SME.
  123. */
  124. if (has_vhe() && system_supports_sme()) {
  125. /* Also restore EL0 state seen on entry */
  126. if (vcpu_get_flag(vcpu, HOST_SME_ENABLED))
  127. sysreg_clear_set(CPACR_EL1, 0,
  128. CPACR_EL1_SMEN_EL0EN |
  129. CPACR_EL1_SMEN_EL1EN);
  130. else
  131. sysreg_clear_set(CPACR_EL1,
  132. CPACR_EL1_SMEN_EL0EN,
  133. CPACR_EL1_SMEN_EL1EN);
  134. }
  135. if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
  136. if (vcpu_has_sve(vcpu)) {
  137. __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
  138. /*
  139. * Restore the VL that was saved when bound to the CPU,
  140. * which is the maximum VL for the guest. Because
  141. * the layout of the data when saving the sve state
  142. * depends on the VL, we need to use a consistent VL.
  143. * Note that this means that at guest exit ZCR_EL1 is
  144. * not necessarily the same as on guest entry.
  145. *
  146. * Flushing the cpu state sets the TIF_FOREIGN_FPSTATE
  147. * bit for the context, which lets the kernel restore
  148. * the sve state, including ZCR_EL1 later.
  149. */
  150. if (!has_vhe())
  151. sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
  152. SYS_ZCR_EL1);
  153. }
  154. fpsimd_save_and_flush_cpu_state();
  155. } else if (has_vhe() && system_supports_sve()) {
  156. /*
  157. * The FPSIMD/SVE state in the CPU has not been touched, and we
  158. * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
  159. * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
  160. * for EL0. To avoid spurious traps, restore the trap state
  161. * seen by kvm_arch_vcpu_load_fp():
  162. */
  163. if (vcpu_get_flag(vcpu, HOST_SVE_ENABLED))
  164. sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
  165. else
  166. sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
  167. }
  168. update_thread_flag(TIF_SVE, 0);
  169. local_irq_restore(flags);
  170. }