switch.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 - ARM Ltd
  4. * Author: Marc Zyngier <[email protected]>
  5. */
  6. #include <hyp/switch.h>
  7. #include <linux/arm-smccc.h>
  8. #include <linux/kvm_host.h>
  9. #include <linux/types.h>
  10. #include <linux/jump_label.h>
  11. #include <linux/percpu.h>
  12. #include <uapi/linux/psci.h>
  13. #include <kvm/arm_psci.h>
  14. #include <asm/barrier.h>
  15. #include <asm/cpufeature.h>
  16. #include <asm/kprobes.h>
  17. #include <asm/kvm_asm.h>
  18. #include <asm/kvm_emulate.h>
  19. #include <asm/kvm_hyp.h>
  20. #include <asm/kvm_mmu.h>
  21. #include <asm/fpsimd.h>
  22. #include <asm/debug-monitors.h>
  23. #include <asm/processor.h>
  24. #include <asm/thread_info.h>
  25. #include <asm/vectors.h>
  26. /* VHE specific context */
  27. DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data);
  28. DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
  29. DEFINE_PER_CPU(unsigned long, kvm_hyp_vector);
  30. static void __activate_traps(struct kvm_vcpu *vcpu)
  31. {
  32. u64 val;
  33. ___activate_traps(vcpu);
  34. val = read_sysreg(cpacr_el1);
  35. val |= CPACR_EL1_TTA;
  36. val &= ~(CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN |
  37. CPACR_EL1_SMEN_EL0EN | CPACR_EL1_SMEN_EL1EN);
  38. /*
  39. * With VHE (HCR.E2H == 1), accesses to CPACR_EL1 are routed to
  40. * CPTR_EL2. In general, CPACR_EL1 has the same layout as CPTR_EL2,
  41. * except for some missing controls, such as TAM.
  42. * In this case, CPTR_EL2.TAM has the same position with or without
  43. * VHE (HCR.E2H == 1) which allows us to use here the CPTR_EL2.TAM
  44. * shift value for trapping the AMU accesses.
  45. */
  46. val |= CPTR_EL2_TAM;
  47. if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
  48. if (vcpu_has_sve(vcpu))
  49. val |= CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN;
  50. } else {
  51. val &= ~(CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN);
  52. __activate_traps_fpsimd32(vcpu);
  53. }
  54. write_sysreg(val, cpacr_el1);
  55. write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el1);
  56. }
  57. NOKPROBE_SYMBOL(__activate_traps);
  58. static void __deactivate_traps(struct kvm_vcpu *vcpu)
  59. {
  60. const char *host_vectors = vectors;
  61. ___deactivate_traps(vcpu);
  62. write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
  63. /*
  64. * ARM errata 1165522 and 1530923 require the actual execution of the
  65. * above before we can switch to the EL2/EL0 translation regime used by
  66. * the host.
  67. */
  68. asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT));
  69. write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1);
  70. if (!arm64_kernel_unmapped_at_el0())
  71. host_vectors = __this_cpu_read(this_cpu_vector);
  72. write_sysreg(host_vectors, vbar_el1);
  73. }
  74. NOKPROBE_SYMBOL(__deactivate_traps);
  75. void activate_traps_vhe_load(struct kvm_vcpu *vcpu)
  76. {
  77. __activate_traps_common(vcpu);
  78. }
  79. void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu)
  80. {
  81. __deactivate_traps_common(vcpu);
  82. }
  83. static void __deactivate_fpsimd_traps(struct kvm_vcpu *vcpu)
  84. {
  85. u64 reg = CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN;
  86. if (vcpu_has_sve(vcpu))
  87. reg |= CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN;
  88. sysreg_clear_set(cpacr_el1, 0, reg);
  89. }
  90. static void kvm_hyp_handle_fpsimd_host(struct kvm_vcpu *vcpu)
  91. {
  92. __fpsimd_save_state(vcpu->arch.host_fpsimd_state);
  93. }
  94. static const exit_handler_fn hyp_exit_handlers[] = {
  95. [0 ... ESR_ELx_EC_MAX] = NULL,
  96. [ESR_ELx_EC_CP15_32] = kvm_hyp_handle_cp15_32,
  97. [ESR_ELx_EC_SYS64] = kvm_hyp_handle_sysreg,
  98. [ESR_ELx_EC_SVE] = kvm_hyp_handle_fpsimd,
  99. [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd,
  100. [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low,
  101. [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low,
  102. [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low,
  103. [ESR_ELx_EC_PAC] = kvm_hyp_handle_ptrauth,
  104. };
  105. static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu)
  106. {
  107. return hyp_exit_handlers;
  108. }
  109. static void early_exit_filter(struct kvm_vcpu *vcpu, u64 *exit_code)
  110. {
  111. }
  112. /* Switch to the guest for VHE systems running in EL2 */
  113. static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
  114. {
  115. struct kvm_cpu_context *host_ctxt;
  116. struct kvm_cpu_context *guest_ctxt;
  117. u64 exit_code;
  118. host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
  119. host_ctxt->__hyp_running_vcpu = vcpu;
  120. guest_ctxt = &vcpu->arch.ctxt;
  121. sysreg_save_host_state_vhe(host_ctxt);
  122. /*
  123. * ARM erratum 1165522 requires us to configure both stage 1 and
  124. * stage 2 translation for the guest context before we clear
  125. * HCR_EL2.TGE.
  126. *
  127. * We have already configured the guest's stage 1 translation in
  128. * kvm_vcpu_load_sysregs_vhe above. We must now call
  129. * __load_stage2 before __activate_traps, because
  130. * __load_stage2 configures stage 2 translation, and
  131. * __activate_traps clear HCR_EL2.TGE (among other things).
  132. */
  133. __load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
  134. __activate_traps(vcpu);
  135. __kvm_adjust_pc(vcpu);
  136. sysreg_restore_guest_state_vhe(guest_ctxt);
  137. __debug_switch_to_guest(vcpu);
  138. do {
  139. /* Jump in the fire! */
  140. exit_code = __guest_enter(vcpu);
  141. /* And we're baaack! */
  142. } while (fixup_guest_exit(vcpu, &exit_code));
  143. sysreg_save_guest_state_vhe(guest_ctxt);
  144. __deactivate_traps(vcpu);
  145. sysreg_restore_host_state_vhe(host_ctxt);
  146. if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED)
  147. __fpsimd_save_fpexc32(vcpu);
  148. __debug_switch_to_host(vcpu);
  149. return exit_code;
  150. }
  151. NOKPROBE_SYMBOL(__kvm_vcpu_run_vhe);
  152. int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
  153. {
  154. int ret;
  155. local_daif_mask();
  156. /*
  157. * Having IRQs masked via PMR when entering the guest means the GIC
  158. * will not signal the CPU of interrupts of lower priority, and the
  159. * only way to get out will be via guest exceptions.
  160. * Naturally, we want to avoid this.
  161. *
  162. * local_daif_mask() already sets GIC_PRIO_PSR_I_SET, we just need a
  163. * dsb to ensure the redistributor is forwards EL2 IRQs to the CPU.
  164. */
  165. pmr_sync();
  166. ret = __kvm_vcpu_run_vhe(vcpu);
  167. /*
  168. * local_daif_restore() takes care to properly restore PSTATE.DAIF
  169. * and the GIC PMR if the host is using IRQ priorities.
  170. */
  171. local_daif_restore(DAIF_PROCCTX_NOIRQ);
  172. /*
  173. * When we exit from the guest we change a number of CPU configuration
  174. * parameters, such as traps. Make sure these changes take effect
  175. * before running the host or additional guests.
  176. */
  177. isb();
  178. return ret;
  179. }
  180. static void __hyp_call_panic(u64 spsr, u64 elr, u64 par)
  181. {
  182. struct kvm_cpu_context *host_ctxt;
  183. struct kvm_vcpu *vcpu;
  184. host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
  185. vcpu = host_ctxt->__hyp_running_vcpu;
  186. __deactivate_traps(vcpu);
  187. sysreg_restore_host_state_vhe(host_ctxt);
  188. panic("HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n",
  189. spsr, elr,
  190. read_sysreg_el2(SYS_ESR), read_sysreg_el2(SYS_FAR),
  191. read_sysreg(hpfar_el2), par, vcpu);
  192. }
  193. NOKPROBE_SYMBOL(__hyp_call_panic);
  194. void __noreturn hyp_panic(void)
  195. {
  196. u64 spsr = read_sysreg_el2(SYS_SPSR);
  197. u64 elr = read_sysreg_el2(SYS_ELR);
  198. u64 par = read_sysreg_par();
  199. __hyp_call_panic(spsr, elr, par);
  200. unreachable();
  201. }
  202. asmlinkage void kvm_unexpected_el2_exception(void)
  203. {
  204. __kvm_unexpected_el2_exception();
  205. }