switch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 <hyp/sysreg-sr.h>
  8. #include <linux/kvm_host.h>
  9. #include <linux/types.h>
  10. #include <linux/jump_label.h>
  11. #include <uapi/linux/psci.h>
  12. #include <kvm/arm_psci.h>
  13. #include <asm/barrier.h>
  14. #include <asm/cpufeature.h>
  15. #include <asm/kprobes.h>
  16. #include <asm/kvm_asm.h>
  17. #include <asm/kvm_emulate.h>
  18. #include <asm/kvm_hyp.h>
  19. #include <asm/kvm_hypevents.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 <nvhe/mem_protect.h>
  25. #include <nvhe/pkvm.h>
  26. /* Non-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. extern void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc);
  31. static void __activate_traps(struct kvm_vcpu *vcpu)
  32. {
  33. u64 val;
  34. ___activate_traps(vcpu);
  35. __activate_traps_common(vcpu);
  36. val = vcpu->arch.cptr_el2;
  37. val |= CPTR_EL2_TTA | CPTR_EL2_TAM;
  38. if (vcpu->arch.fp_state != FP_STATE_GUEST_OWNED) {
  39. val |= CPTR_EL2_TFP | CPTR_EL2_TZ;
  40. __activate_traps_fpsimd32(vcpu);
  41. }
  42. if (cpus_have_final_cap(ARM64_SME))
  43. val |= CPTR_EL2_TSM;
  44. write_sysreg(val, cptr_el2);
  45. write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2);
  46. if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
  47. struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt;
  48. isb();
  49. /*
  50. * At this stage, and thanks to the above isb(), S2 is
  51. * configured and enabled. We can now restore the guest's S1
  52. * configuration: SCTLR, and only then TCR.
  53. */
  54. write_sysreg_el1(ctxt_sys_reg(ctxt, SCTLR_EL1), SYS_SCTLR);
  55. isb();
  56. write_sysreg_el1(ctxt_sys_reg(ctxt, TCR_EL1), SYS_TCR);
  57. }
  58. }
  59. static void __deactivate_traps(struct kvm_vcpu *vcpu)
  60. {
  61. extern char __kvm_hyp_host_vector[];
  62. u64 cptr;
  63. ___deactivate_traps(vcpu);
  64. if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
  65. u64 val;
  66. /*
  67. * Set the TCR and SCTLR registers in the exact opposite
  68. * sequence as __activate_traps (first prevent walks,
  69. * then force the MMU on). A generous sprinkling of isb()
  70. * ensure that things happen in this exact order.
  71. */
  72. val = read_sysreg_el1(SYS_TCR);
  73. write_sysreg_el1(val | TCR_EPD1_MASK | TCR_EPD0_MASK, SYS_TCR);
  74. isb();
  75. val = read_sysreg_el1(SYS_SCTLR);
  76. write_sysreg_el1(val | SCTLR_ELx_M, SYS_SCTLR);
  77. isb();
  78. }
  79. __deactivate_traps_common(vcpu);
  80. write_sysreg(this_cpu_ptr(&kvm_init_params)->hcr_el2, hcr_el2);
  81. cptr = CPTR_EL2_DEFAULT;
  82. if (vcpu_has_sve(vcpu) && (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED))
  83. cptr |= CPTR_EL2_TZ;
  84. if (cpus_have_final_cap(ARM64_SME))
  85. cptr &= ~CPTR_EL2_TSM;
  86. write_sysreg(cptr, cptr_el2);
  87. write_sysreg(__kvm_hyp_host_vector, vbar_el2);
  88. }
  89. static void __deactivate_fpsimd_traps(struct kvm_vcpu *vcpu)
  90. {
  91. u64 reg = CPTR_EL2_TFP;
  92. if (vcpu_has_sve(vcpu) ||
  93. (is_protected_kvm_enabled() && system_supports_sve())) {
  94. reg |= CPTR_EL2_TZ;
  95. }
  96. sysreg_clear_set(cptr_el2, reg, 0);
  97. }
  98. /* Save VGICv3 state on non-VHE systems */
  99. static void __hyp_vgic_save_state(struct kvm_vcpu *vcpu)
  100. {
  101. if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
  102. __vgic_v3_save_state(&vcpu->arch.vgic_cpu.vgic_v3);
  103. __vgic_v3_deactivate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
  104. }
  105. }
  106. /* Restore VGICv3 state on non-VHE systems */
  107. static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
  108. {
  109. if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
  110. __vgic_v3_activate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
  111. __vgic_v3_restore_state(&vcpu->arch.vgic_cpu.vgic_v3);
  112. }
  113. }
  114. /*
  115. * Disable host events, enable guest events
  116. */
  117. #ifdef CONFIG_HW_PERF_EVENTS
  118. static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
  119. {
  120. struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
  121. if (pmu->events_host)
  122. write_sysreg(pmu->events_host, pmcntenclr_el0);
  123. if (pmu->events_guest)
  124. write_sysreg(pmu->events_guest, pmcntenset_el0);
  125. return (pmu->events_host || pmu->events_guest);
  126. }
  127. /*
  128. * Disable guest events, enable host events
  129. */
  130. static void __pmu_switch_to_host(struct kvm_vcpu *vcpu)
  131. {
  132. struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
  133. if (pmu->events_guest)
  134. write_sysreg(pmu->events_guest, pmcntenclr_el0);
  135. if (pmu->events_host)
  136. write_sysreg(pmu->events_host, pmcntenset_el0);
  137. }
  138. #else
  139. #define __pmu_switch_to_guest(v) ({ false; })
  140. #define __pmu_switch_to_host(v) do {} while (0)
  141. #endif
  142. /*
  143. * Handler for protected VM MSR, MRS or System instruction execution in AArch64.
  144. *
  145. * Returns true if the hypervisor has handled the exit, and control should go
  146. * back to the guest, or false if it hasn't.
  147. */
  148. static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code)
  149. {
  150. /*
  151. * Make sure we handle the exit for workarounds and ptrauth
  152. * before the pKVM handling, as the latter could decide to
  153. * UNDEF.
  154. */
  155. return (kvm_hyp_handle_sysreg(vcpu, exit_code) ||
  156. kvm_handle_pvm_sysreg(vcpu, exit_code));
  157. }
  158. static void kvm_hyp_handle_fpsimd_host(struct kvm_vcpu *vcpu)
  159. {
  160. /*
  161. * Non-protected kvm relies on the host restoring its sve state.
  162. * Protected kvm restores the host's sve state as not to reveal that
  163. * fpsimd was used by a guest nor leak upper sve bits.
  164. */
  165. if (unlikely(is_protected_kvm_enabled() && system_supports_sve())) {
  166. struct kvm_host_sve_state *sve_state = get_host_sve_state(vcpu);
  167. sve_state->zcr_el1 = read_sysreg_el1(SYS_ZCR);
  168. pkvm_set_max_sve_vq();
  169. __sve_save_state(sve_state->sve_regs +
  170. sve_ffr_offset(kvm_host_sve_max_vl),
  171. &sve_state->fpsr);
  172. /* Still trap SVE since it's handled by hyp in pKVM. */
  173. if (!vcpu_has_sve(vcpu))
  174. sysreg_clear_set(cptr_el2, 0, CPTR_EL2_TZ);
  175. } else {
  176. __fpsimd_save_state(get_host_fpsimd_state(vcpu));
  177. }
  178. }
  179. static const exit_handler_fn hyp_exit_handlers[] = {
  180. [0 ... ESR_ELx_EC_MAX] = NULL,
  181. [ESR_ELx_EC_CP15_32] = kvm_hyp_handle_cp15_32,
  182. [ESR_ELx_EC_HVC64] = kvm_hyp_handle_hvc64,
  183. [ESR_ELx_EC_SYS64] = kvm_hyp_handle_sysreg,
  184. [ESR_ELx_EC_SVE] = kvm_hyp_handle_fpsimd,
  185. [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd,
  186. [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low,
  187. [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low,
  188. [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low,
  189. [ESR_ELx_EC_PAC] = kvm_hyp_handle_ptrauth,
  190. };
  191. static const exit_handler_fn pvm_exit_handlers[] = {
  192. [0 ... ESR_ELx_EC_MAX] = NULL,
  193. [ESR_ELx_EC_HVC64] = kvm_handle_pvm_hvc64,
  194. [ESR_ELx_EC_SYS64] = kvm_handle_pvm_sys64,
  195. [ESR_ELx_EC_SVE] = kvm_handle_pvm_restricted,
  196. [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd,
  197. [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low,
  198. [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low,
  199. [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low,
  200. [ESR_ELx_EC_PAC] = kvm_hyp_handle_ptrauth,
  201. };
  202. static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu)
  203. {
  204. if (unlikely(vcpu_is_protected(vcpu)))
  205. return pvm_exit_handlers;
  206. return hyp_exit_handlers;
  207. }
  208. /*
  209. * Some guests (e.g., protected VMs) are not be allowed to run in AArch32.
  210. * The ARMv8 architecture does not give the hypervisor a mechanism to prevent a
  211. * guest from dropping to AArch32 EL0 if implemented by the CPU. If the
  212. * hypervisor spots a guest in such a state ensure it is handled, and don't
  213. * trust the host to spot or fix it. The check below is based on the one in
  214. * kvm_arch_vcpu_ioctl_run().
  215. *
  216. * Returns false if the guest ran in AArch32 when it shouldn't have, and
  217. * thus should exit to the host, or true if a the guest run loop can continue.
  218. */
  219. static void early_exit_filter(struct kvm_vcpu *vcpu, u64 *exit_code)
  220. {
  221. if (unlikely(vcpu_is_protected(vcpu) && vcpu_mode_is_32bit(vcpu))) {
  222. /*
  223. * As we have caught the guest red-handed, decide that it isn't
  224. * fit for purpose anymore by making the vcpu invalid. The VMM
  225. * can try and fix it by re-initializing the vcpu with
  226. * KVM_ARM_VCPU_INIT, however, this is likely not possible for
  227. * protected VMs.
  228. */
  229. vcpu->arch.target = -1;
  230. *exit_code &= BIT(ARM_EXIT_WITH_SERROR_BIT);
  231. *exit_code |= ARM_EXCEPTION_IL;
  232. }
  233. }
  234. /* Switch to the guest for legacy non-VHE systems */
  235. int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
  236. {
  237. struct kvm_cpu_context *host_ctxt;
  238. struct kvm_cpu_context *guest_ctxt;
  239. struct kvm_s2_mmu *mmu;
  240. bool pmu_switch_needed;
  241. u64 exit_code;
  242. /*
  243. * Having IRQs masked via PMR when entering the guest means the GIC
  244. * will not signal the CPU of interrupts of lower priority, and the
  245. * only way to get out will be via guest exceptions.
  246. * Naturally, we want to avoid this.
  247. */
  248. if (system_uses_irq_prio_masking()) {
  249. gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
  250. pmr_sync();
  251. }
  252. host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
  253. host_ctxt->__hyp_running_vcpu = vcpu;
  254. guest_ctxt = &vcpu->arch.ctxt;
  255. pmu_switch_needed = __pmu_switch_to_guest(vcpu);
  256. __sysreg_save_state_nvhe(host_ctxt);
  257. /*
  258. * We must flush and disable the SPE buffer for nVHE, as
  259. * the translation regime(EL1&0) is going to be loaded with
  260. * that of the guest. And we must do this before we change the
  261. * translation regime to EL2 (via MDCR_EL2_E2PB == 0) and
  262. * before we load guest Stage1.
  263. */
  264. __debug_save_host_buffers_nvhe(vcpu);
  265. __kvm_adjust_pc(vcpu);
  266. /*
  267. * We must restore the 32-bit state before the sysregs, thanks
  268. * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
  269. *
  270. * Also, and in order to be able to deal with erratum #1319537 (A57)
  271. * and #1319367 (A72), we must ensure that all VM-related sysreg are
  272. * restored before we enable S2 translation.
  273. */
  274. __sysreg32_restore_state(vcpu);
  275. __sysreg_restore_state_nvhe(guest_ctxt);
  276. mmu = kern_hyp_va(vcpu->arch.hw_mmu);
  277. __load_stage2(mmu, kern_hyp_va(mmu->arch));
  278. __activate_traps(vcpu);
  279. __hyp_vgic_restore_state(vcpu);
  280. __timer_enable_traps(vcpu);
  281. __debug_switch_to_guest(vcpu);
  282. do {
  283. trace_hyp_exit();
  284. /* Jump in the fire! */
  285. exit_code = __guest_enter(vcpu);
  286. /* And we're baaack! */
  287. trace_hyp_enter();
  288. } while (fixup_guest_exit(vcpu, &exit_code));
  289. __sysreg_save_state_nvhe(guest_ctxt);
  290. __sysreg32_save_state(vcpu);
  291. __timer_disable_traps(vcpu);
  292. __hyp_vgic_save_state(vcpu);
  293. __deactivate_traps(vcpu);
  294. __load_host_stage2();
  295. __sysreg_restore_state_nvhe(host_ctxt);
  296. if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED)
  297. __fpsimd_save_fpexc32(vcpu);
  298. __debug_switch_to_host(vcpu);
  299. /*
  300. * This must come after restoring the host sysregs, since a non-VHE
  301. * system may enable SPE here and make use of the TTBRs.
  302. */
  303. __debug_restore_host_buffers_nvhe(vcpu);
  304. if (pmu_switch_needed)
  305. __pmu_switch_to_host(vcpu);
  306. /* Returning to host will clear PSR.I, remask PMR if needed */
  307. if (system_uses_irq_prio_masking())
  308. gic_write_pmr(GIC_PRIO_IRQOFF);
  309. host_ctxt->__hyp_running_vcpu = NULL;
  310. return exit_code;
  311. }
  312. static void (*hyp_panic_notifier)(struct kvm_cpu_context *host_ctxt);
  313. int __pkvm_register_hyp_panic_notifier(void (*cb)(struct kvm_cpu_context *host_ctxt))
  314. {
  315. return cmpxchg(&hyp_panic_notifier, NULL, cb) ? -EBUSY : 0;
  316. }
  317. asmlinkage void __noreturn hyp_panic(void)
  318. {
  319. u64 spsr = read_sysreg_el2(SYS_SPSR);
  320. u64 elr = read_sysreg_el2(SYS_ELR);
  321. u64 par = read_sysreg_par();
  322. struct kvm_cpu_context *host_ctxt;
  323. struct kvm_vcpu *vcpu;
  324. host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
  325. vcpu = host_ctxt->__hyp_running_vcpu;
  326. if (READ_ONCE(hyp_panic_notifier))
  327. hyp_panic_notifier(host_ctxt);
  328. if (vcpu) {
  329. __timer_disable_traps(vcpu);
  330. __deactivate_traps(vcpu);
  331. __load_host_stage2();
  332. __sysreg_restore_state_nvhe(host_ctxt);
  333. }
  334. /* Prepare to dump kvm nvhe hyp stacktrace */
  335. kvm_nvhe_prepare_backtrace((unsigned long)__builtin_frame_address(0),
  336. _THIS_IP_);
  337. __hyp_do_panic(host_ctxt, spsr, elr, par);
  338. unreachable();
  339. }
  340. asmlinkage void __noreturn hyp_panic_bad_stack(void)
  341. {
  342. hyp_panic();
  343. }
  344. asmlinkage void kvm_unexpected_el2_exception(void)
  345. {
  346. __kvm_unexpected_el2_exception();
  347. }