switch.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Copyright (C) 2015 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/arm-smccc.h>
  18. #include <linux/types.h>
  19. #include <linux/jump_label.h>
  20. #include <uapi/linux/psci.h>
  21. #include <kvm/arm_psci.h>
  22. #include <asm/cpufeature.h>
  23. #include <asm/extable.h>
  24. #include <asm/kprobes.h>
  25. #include <asm/kvm_asm.h>
  26. #include <asm/kvm_emulate.h>
  27. #include <asm/kvm_host.h>
  28. #include <asm/kvm_hyp.h>
  29. #include <asm/kvm_mmu.h>
  30. #include <asm/fpsimd.h>
  31. #include <asm/debug-monitors.h>
  32. #include <asm/processor.h>
  33. #include <asm/thread_info.h>
  34. extern struct exception_table_entry __start___kvm_ex_table;
  35. extern struct exception_table_entry __stop___kvm_ex_table;
  36. /* Check whether the FP regs were dirtied while in the host-side run loop: */
  37. static bool __hyp_text update_fp_enabled(struct kvm_vcpu *vcpu)
  38. {
  39. /*
  40. * When the system doesn't support FP/SIMD, we cannot rely on
  41. * the _TIF_FOREIGN_FPSTATE flag. However, we always inject an
  42. * abort on the very first access to FP and thus we should never
  43. * see KVM_ARM64_FP_ENABLED. For added safety, make sure we always
  44. * trap the accesses.
  45. */
  46. if (!system_supports_fpsimd() ||
  47. vcpu->arch.host_thread_info->flags & _TIF_FOREIGN_FPSTATE)
  48. vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |
  49. KVM_ARM64_FP_HOST);
  50. return !!(vcpu->arch.flags & KVM_ARM64_FP_ENABLED);
  51. }
  52. /* Save the 32-bit only FPSIMD system register state */
  53. static void __hyp_text __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu)
  54. {
  55. if (!vcpu_el1_is_32bit(vcpu))
  56. return;
  57. vcpu->arch.ctxt.sys_regs[FPEXC32_EL2] = read_sysreg(fpexc32_el2);
  58. }
  59. static void __hyp_text __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
  60. {
  61. /*
  62. * We are about to set CPTR_EL2.TFP to trap all floating point
  63. * register accesses to EL2, however, the ARM ARM clearly states that
  64. * traps are only taken to EL2 if the operation would not otherwise
  65. * trap to EL1. Therefore, always make sure that for 32-bit guests,
  66. * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
  67. * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
  68. * it will cause an exception.
  69. */
  70. if (vcpu_el1_is_32bit(vcpu) && system_supports_fpsimd()) {
  71. write_sysreg(1 << 30, fpexc32_el2);
  72. isb();
  73. }
  74. }
  75. static void __hyp_text __activate_traps_common(struct kvm_vcpu *vcpu)
  76. {
  77. /* Trap on AArch32 cp15 c15 (impdef sysregs) accesses (EL1 or EL0) */
  78. write_sysreg(1 << 15, hstr_el2);
  79. /*
  80. * Make sure we trap PMU access from EL0 to EL2. Also sanitize
  81. * PMSELR_EL0 to make sure it never contains the cycle
  82. * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
  83. * EL1 instead of being trapped to EL2.
  84. */
  85. write_sysreg(0, pmselr_el0);
  86. write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
  87. write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
  88. }
  89. static void __hyp_text __deactivate_traps_common(void)
  90. {
  91. write_sysreg(0, hstr_el2);
  92. write_sysreg(0, pmuserenr_el0);
  93. }
  94. static void activate_traps_vhe(struct kvm_vcpu *vcpu)
  95. {
  96. u64 val;
  97. val = read_sysreg(cpacr_el1);
  98. val |= CPACR_EL1_TTA;
  99. val &= ~CPACR_EL1_ZEN;
  100. if (!update_fp_enabled(vcpu)) {
  101. val &= ~CPACR_EL1_FPEN;
  102. __activate_traps_fpsimd32(vcpu);
  103. }
  104. write_sysreg(val, cpacr_el1);
  105. write_sysreg(kvm_get_hyp_vector(), vbar_el1);
  106. }
  107. NOKPROBE_SYMBOL(activate_traps_vhe);
  108. static void __hyp_text __activate_traps_nvhe(struct kvm_vcpu *vcpu)
  109. {
  110. u64 val;
  111. __activate_traps_common(vcpu);
  112. val = CPTR_EL2_DEFAULT;
  113. val |= CPTR_EL2_TTA | CPTR_EL2_TZ;
  114. if (!update_fp_enabled(vcpu)) {
  115. val |= CPTR_EL2_TFP;
  116. __activate_traps_fpsimd32(vcpu);
  117. }
  118. write_sysreg(val, cptr_el2);
  119. }
  120. static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
  121. {
  122. u64 hcr = vcpu->arch.hcr_el2;
  123. write_sysreg(hcr, hcr_el2);
  124. if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN) && (hcr & HCR_VSE))
  125. write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2);
  126. if (has_vhe())
  127. activate_traps_vhe(vcpu);
  128. else
  129. __activate_traps_nvhe(vcpu);
  130. }
  131. static void deactivate_traps_vhe(void)
  132. {
  133. extern char vectors[]; /* kernel exception vectors */
  134. write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
  135. write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1);
  136. write_sysreg(vectors, vbar_el1);
  137. }
  138. NOKPROBE_SYMBOL(deactivate_traps_vhe);
  139. static void __hyp_text __deactivate_traps_nvhe(void)
  140. {
  141. u64 mdcr_el2 = read_sysreg(mdcr_el2);
  142. __deactivate_traps_common();
  143. mdcr_el2 &= MDCR_EL2_HPMN_MASK;
  144. mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT;
  145. write_sysreg(mdcr_el2, mdcr_el2);
  146. write_sysreg(HCR_HOST_NVHE_FLAGS, hcr_el2);
  147. write_sysreg(CPTR_EL2_DEFAULT, cptr_el2);
  148. }
  149. static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
  150. {
  151. /*
  152. * If we pended a virtual abort, preserve it until it gets
  153. * cleared. See D1.14.3 (Virtual Interrupts) for details, but
  154. * the crucial bit is "On taking a vSError interrupt,
  155. * HCR_EL2.VSE is cleared to 0."
  156. */
  157. if (vcpu->arch.hcr_el2 & HCR_VSE)
  158. vcpu->arch.hcr_el2 = read_sysreg(hcr_el2);
  159. if (has_vhe())
  160. deactivate_traps_vhe();
  161. else
  162. __deactivate_traps_nvhe();
  163. }
  164. void activate_traps_vhe_load(struct kvm_vcpu *vcpu)
  165. {
  166. __activate_traps_common(vcpu);
  167. }
  168. void deactivate_traps_vhe_put(void)
  169. {
  170. u64 mdcr_el2 = read_sysreg(mdcr_el2);
  171. mdcr_el2 &= MDCR_EL2_HPMN_MASK |
  172. MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
  173. MDCR_EL2_TPMS;
  174. write_sysreg(mdcr_el2, mdcr_el2);
  175. __deactivate_traps_common();
  176. }
  177. static void __hyp_text __activate_vm(struct kvm *kvm)
  178. {
  179. write_sysreg(kvm->arch.vttbr, vttbr_el2);
  180. }
  181. static void __hyp_text __deactivate_vm(struct kvm_vcpu *vcpu)
  182. {
  183. write_sysreg(0, vttbr_el2);
  184. }
  185. /* Save VGICv3 state on non-VHE systems */
  186. static void __hyp_text __hyp_vgic_save_state(struct kvm_vcpu *vcpu)
  187. {
  188. if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
  189. __vgic_v3_save_state(vcpu);
  190. __vgic_v3_deactivate_traps(vcpu);
  191. }
  192. }
  193. /* Restore VGICv3 state on non_VEH systems */
  194. static void __hyp_text __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
  195. {
  196. if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
  197. __vgic_v3_activate_traps(vcpu);
  198. __vgic_v3_restore_state(vcpu);
  199. }
  200. }
  201. static bool __hyp_text __true_value(void)
  202. {
  203. return true;
  204. }
  205. static bool __hyp_text __false_value(void)
  206. {
  207. return false;
  208. }
  209. static hyp_alternate_select(__check_arm_834220,
  210. __false_value, __true_value,
  211. ARM64_WORKAROUND_834220);
  212. static bool __hyp_text __translate_far_to_hpfar(u64 far, u64 *hpfar)
  213. {
  214. u64 par, tmp;
  215. /*
  216. * Resolve the IPA the hard way using the guest VA.
  217. *
  218. * Stage-1 translation already validated the memory access
  219. * rights. As such, we can use the EL1 translation regime, and
  220. * don't have to distinguish between EL0 and EL1 access.
  221. *
  222. * We do need to save/restore PAR_EL1 though, as we haven't
  223. * saved the guest context yet, and we may return early...
  224. */
  225. par = read_sysreg(par_el1);
  226. if (!__kvm_at("s1e1r", far))
  227. tmp = read_sysreg(par_el1);
  228. else
  229. tmp = 1; /* back to the guest */
  230. write_sysreg(par, par_el1);
  231. if (unlikely(tmp & 1))
  232. return false; /* Translation failed, back to guest */
  233. /* Convert PAR to HPFAR format */
  234. *hpfar = ((tmp >> 12) & ((1UL << 36) - 1)) << 4;
  235. return true;
  236. }
  237. static bool __hyp_text __populate_fault_info(struct kvm_vcpu *vcpu)
  238. {
  239. u8 ec;
  240. u64 esr;
  241. u64 hpfar, far;
  242. esr = vcpu->arch.fault.esr_el2;
  243. ec = ESR_ELx_EC(esr);
  244. if (ec != ESR_ELx_EC_DABT_LOW && ec != ESR_ELx_EC_IABT_LOW)
  245. return true;
  246. far = read_sysreg_el2(far);
  247. /*
  248. * The HPFAR can be invalid if the stage 2 fault did not
  249. * happen during a stage 1 page table walk (the ESR_EL2.S1PTW
  250. * bit is clear) and one of the two following cases are true:
  251. * 1. The fault was due to a permission fault
  252. * 2. The processor carries errata 834220
  253. *
  254. * Therefore, for all non S1PTW faults where we either have a
  255. * permission fault or the errata workaround is enabled, we
  256. * resolve the IPA using the AT instruction.
  257. */
  258. if (!(esr & ESR_ELx_S1PTW) &&
  259. (__check_arm_834220()() || (esr & ESR_ELx_FSC_TYPE) == FSC_PERM)) {
  260. if (!__translate_far_to_hpfar(far, &hpfar))
  261. return false;
  262. } else {
  263. hpfar = read_sysreg(hpfar_el2);
  264. }
  265. vcpu->arch.fault.far_el2 = far;
  266. vcpu->arch.fault.hpfar_el2 = hpfar;
  267. return true;
  268. }
  269. /* Skip an instruction which has been emulated. Returns true if
  270. * execution can continue or false if we need to exit hyp mode because
  271. * single-step was in effect.
  272. */
  273. static bool __hyp_text __skip_instr(struct kvm_vcpu *vcpu)
  274. {
  275. *vcpu_pc(vcpu) = read_sysreg_el2(elr);
  276. if (vcpu_mode_is_32bit(vcpu)) {
  277. vcpu->arch.ctxt.gp_regs.regs.pstate = read_sysreg_el2(spsr);
  278. kvm_skip_instr32(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
  279. write_sysreg_el2(vcpu->arch.ctxt.gp_regs.regs.pstate, spsr);
  280. } else {
  281. *vcpu_pc(vcpu) += 4;
  282. }
  283. write_sysreg_el2(*vcpu_pc(vcpu), elr);
  284. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  285. vcpu->arch.fault.esr_el2 =
  286. (ESR_ELx_EC_SOFTSTP_LOW << ESR_ELx_EC_SHIFT) | 0x22;
  287. return false;
  288. } else {
  289. return true;
  290. }
  291. }
  292. static bool __hyp_text __hyp_switch_fpsimd(struct kvm_vcpu *vcpu)
  293. {
  294. struct user_fpsimd_state *host_fpsimd = vcpu->arch.host_fpsimd_state;
  295. if (has_vhe())
  296. write_sysreg(read_sysreg(cpacr_el1) | CPACR_EL1_FPEN,
  297. cpacr_el1);
  298. else
  299. write_sysreg(read_sysreg(cptr_el2) & ~(u64)CPTR_EL2_TFP,
  300. cptr_el2);
  301. isb();
  302. if (vcpu->arch.flags & KVM_ARM64_FP_HOST) {
  303. /*
  304. * In the SVE case, VHE is assumed: it is enforced by
  305. * Kconfig and kvm_arch_init().
  306. */
  307. if (system_supports_sve() &&
  308. (vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE)) {
  309. struct thread_struct *thread = container_of(
  310. host_fpsimd,
  311. struct thread_struct, uw.fpsimd_state);
  312. sve_save_state(sve_pffr(thread), &host_fpsimd->fpsr);
  313. } else {
  314. __fpsimd_save_state(host_fpsimd);
  315. }
  316. vcpu->arch.flags &= ~KVM_ARM64_FP_HOST;
  317. }
  318. __fpsimd_restore_state(&vcpu->arch.ctxt.gp_regs.fp_regs);
  319. /* Skip restoring fpexc32 for AArch64 guests */
  320. if (!(read_sysreg(hcr_el2) & HCR_RW))
  321. write_sysreg(vcpu->arch.ctxt.sys_regs[FPEXC32_EL2],
  322. fpexc32_el2);
  323. vcpu->arch.flags |= KVM_ARM64_FP_ENABLED;
  324. return true;
  325. }
  326. /*
  327. * Return true when we were able to fixup the guest exit and should return to
  328. * the guest, false when we should restore the host state and return to the
  329. * main run loop.
  330. */
  331. static bool __hyp_text fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
  332. {
  333. if (ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ)
  334. vcpu->arch.fault.esr_el2 = read_sysreg_el2(esr);
  335. /*
  336. * We're using the raw exception code in order to only process
  337. * the trap if no SError is pending. We will come back to the
  338. * same PC once the SError has been injected, and replay the
  339. * trapping instruction.
  340. */
  341. if (*exit_code != ARM_EXCEPTION_TRAP)
  342. goto exit;
  343. /*
  344. * We trap the first access to the FP/SIMD to save the host context
  345. * and restore the guest context lazily.
  346. * If FP/SIMD is not implemented, handle the trap and inject an
  347. * undefined instruction exception to the guest.
  348. */
  349. if (system_supports_fpsimd() &&
  350. kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_FP_ASIMD)
  351. return __hyp_switch_fpsimd(vcpu);
  352. if (!__populate_fault_info(vcpu))
  353. return true;
  354. if (static_branch_unlikely(&vgic_v2_cpuif_trap)) {
  355. bool valid;
  356. valid = kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_DABT_LOW &&
  357. kvm_vcpu_trap_get_fault_type(vcpu) == FSC_FAULT &&
  358. kvm_vcpu_dabt_isvalid(vcpu) &&
  359. !kvm_vcpu_dabt_isextabt(vcpu) &&
  360. !kvm_vcpu_abt_iss1tw(vcpu);
  361. if (valid) {
  362. int ret = __vgic_v2_perform_cpuif_access(vcpu);
  363. if (ret == 1 && __skip_instr(vcpu))
  364. return true;
  365. if (ret == -1) {
  366. /* Promote an illegal access to an
  367. * SError. If we would be returning
  368. * due to single-step clear the SS
  369. * bit so handle_exit knows what to
  370. * do after dealing with the error.
  371. */
  372. if (!__skip_instr(vcpu))
  373. *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
  374. *exit_code = ARM_EXCEPTION_EL1_SERROR;
  375. }
  376. goto exit;
  377. }
  378. }
  379. if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
  380. (kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 ||
  381. kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_CP15_32)) {
  382. int ret = __vgic_v3_perform_cpuif_access(vcpu);
  383. if (ret == 1 && __skip_instr(vcpu))
  384. return true;
  385. }
  386. exit:
  387. /* Return to the host kernel and handle the exit */
  388. return false;
  389. }
  390. static inline bool __hyp_text __needs_ssbd_off(struct kvm_vcpu *vcpu)
  391. {
  392. if (!cpus_have_const_cap(ARM64_SSBD))
  393. return false;
  394. return !(vcpu->arch.workaround_flags & VCPU_WORKAROUND_2_FLAG);
  395. }
  396. static void __hyp_text __set_guest_arch_workaround_state(struct kvm_vcpu *vcpu)
  397. {
  398. #ifdef CONFIG_ARM64_SSBD
  399. /*
  400. * The host runs with the workaround always present. If the
  401. * guest wants it disabled, so be it...
  402. */
  403. if (__needs_ssbd_off(vcpu) &&
  404. __hyp_this_cpu_read(arm64_ssbd_callback_required))
  405. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, 0, NULL);
  406. #endif
  407. }
  408. static void __hyp_text __set_host_arch_workaround_state(struct kvm_vcpu *vcpu)
  409. {
  410. #ifdef CONFIG_ARM64_SSBD
  411. /*
  412. * If the guest has disabled the workaround, bring it back on.
  413. */
  414. if (__needs_ssbd_off(vcpu) &&
  415. __hyp_this_cpu_read(arm64_ssbd_callback_required))
  416. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, 1, NULL);
  417. #endif
  418. }
  419. /* Switch to the guest for VHE systems running in EL2 */
  420. int kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
  421. {
  422. struct kvm_cpu_context *host_ctxt;
  423. struct kvm_cpu_context *guest_ctxt;
  424. u64 exit_code;
  425. host_ctxt = vcpu->arch.host_cpu_context;
  426. host_ctxt->__hyp_running_vcpu = vcpu;
  427. guest_ctxt = &vcpu->arch.ctxt;
  428. sysreg_save_host_state_vhe(host_ctxt);
  429. __activate_traps(vcpu);
  430. __activate_vm(vcpu->kvm);
  431. sysreg_restore_guest_state_vhe(guest_ctxt);
  432. __debug_switch_to_guest(vcpu);
  433. __set_guest_arch_workaround_state(vcpu);
  434. do {
  435. /* Jump in the fire! */
  436. exit_code = __guest_enter(vcpu, host_ctxt);
  437. /* And we're baaack! */
  438. } while (fixup_guest_exit(vcpu, &exit_code));
  439. __set_host_arch_workaround_state(vcpu);
  440. sysreg_save_guest_state_vhe(guest_ctxt);
  441. __deactivate_traps(vcpu);
  442. sysreg_restore_host_state_vhe(host_ctxt);
  443. if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED)
  444. __fpsimd_save_fpexc32(vcpu);
  445. __debug_switch_to_host(vcpu);
  446. return exit_code;
  447. }
  448. NOKPROBE_SYMBOL(kvm_vcpu_run_vhe);
  449. /* Switch to the guest for legacy non-VHE systems */
  450. int __hyp_text __kvm_vcpu_run_nvhe(struct kvm_vcpu *vcpu)
  451. {
  452. struct kvm_cpu_context *host_ctxt;
  453. struct kvm_cpu_context *guest_ctxt;
  454. u64 exit_code;
  455. vcpu = kern_hyp_va(vcpu);
  456. host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
  457. host_ctxt->__hyp_running_vcpu = vcpu;
  458. guest_ctxt = &vcpu->arch.ctxt;
  459. __sysreg_save_state_nvhe(host_ctxt);
  460. __debug_save_host_buffers_nvhe(vcpu);
  461. __activate_traps(vcpu);
  462. __activate_vm(kern_hyp_va(vcpu->kvm));
  463. __hyp_vgic_restore_state(vcpu);
  464. __timer_enable_traps(vcpu);
  465. /*
  466. * We must restore the 32-bit state before the sysregs, thanks
  467. * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
  468. */
  469. __sysreg32_restore_state(vcpu);
  470. __sysreg_restore_state_nvhe(guest_ctxt);
  471. __debug_switch_to_guest(vcpu);
  472. __set_guest_arch_workaround_state(vcpu);
  473. do {
  474. /* Jump in the fire! */
  475. exit_code = __guest_enter(vcpu, host_ctxt);
  476. /* And we're baaack! */
  477. } while (fixup_guest_exit(vcpu, &exit_code));
  478. __set_host_arch_workaround_state(vcpu);
  479. __sysreg_save_state_nvhe(guest_ctxt);
  480. __sysreg32_save_state(vcpu);
  481. __timer_disable_traps(vcpu);
  482. __hyp_vgic_save_state(vcpu);
  483. __deactivate_traps(vcpu);
  484. __deactivate_vm(vcpu);
  485. __sysreg_restore_state_nvhe(host_ctxt);
  486. if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED)
  487. __fpsimd_save_fpexc32(vcpu);
  488. __debug_switch_to_host(vcpu);
  489. /*
  490. * This must come after restoring the host sysregs, since a non-VHE
  491. * system may enable SPE here and make use of the TTBRs.
  492. */
  493. __debug_restore_host_buffers_nvhe(vcpu);
  494. return exit_code;
  495. }
  496. static const char __hyp_panic_string[] = "HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n";
  497. static void __hyp_text __hyp_call_panic_nvhe(u64 spsr, u64 elr, u64 par,
  498. struct kvm_cpu_context *__host_ctxt)
  499. {
  500. struct kvm_vcpu *vcpu;
  501. unsigned long str_va;
  502. vcpu = __host_ctxt->__hyp_running_vcpu;
  503. if (read_sysreg(vttbr_el2)) {
  504. __timer_disable_traps(vcpu);
  505. __deactivate_traps(vcpu);
  506. __deactivate_vm(vcpu);
  507. __sysreg_restore_state_nvhe(__host_ctxt);
  508. }
  509. /*
  510. * Force the panic string to be loaded from the literal pool,
  511. * making sure it is a kernel address and not a PC-relative
  512. * reference.
  513. */
  514. asm volatile("ldr %0, =%1" : "=r" (str_va) : "S" (__hyp_panic_string));
  515. __hyp_do_panic(str_va,
  516. spsr, elr,
  517. read_sysreg(esr_el2), read_sysreg_el2(far),
  518. read_sysreg(hpfar_el2), par, vcpu);
  519. }
  520. static void __hyp_call_panic_vhe(u64 spsr, u64 elr, u64 par,
  521. struct kvm_cpu_context *host_ctxt)
  522. {
  523. struct kvm_vcpu *vcpu;
  524. vcpu = host_ctxt->__hyp_running_vcpu;
  525. __deactivate_traps(vcpu);
  526. sysreg_restore_host_state_vhe(host_ctxt);
  527. panic(__hyp_panic_string,
  528. spsr, elr,
  529. read_sysreg_el2(esr), read_sysreg_el2(far),
  530. read_sysreg(hpfar_el2), par, vcpu);
  531. }
  532. NOKPROBE_SYMBOL(__hyp_call_panic_vhe);
  533. void __hyp_text __noreturn hyp_panic(struct kvm_cpu_context *host_ctxt)
  534. {
  535. u64 spsr = read_sysreg_el2(spsr);
  536. u64 elr = read_sysreg_el2(elr);
  537. u64 par = read_sysreg(par_el1);
  538. if (!has_vhe())
  539. __hyp_call_panic_nvhe(spsr, elr, par, host_ctxt);
  540. else
  541. __hyp_call_panic_vhe(spsr, elr, par, host_ctxt);
  542. unreachable();
  543. }
  544. asmlinkage void __hyp_text kvm_unexpected_el2_exception(void)
  545. {
  546. unsigned long addr, fixup;
  547. struct kvm_cpu_context *host_ctxt;
  548. struct exception_table_entry *entry, *end;
  549. unsigned long elr_el2 = read_sysreg(elr_el2);
  550. entry = hyp_symbol_addr(__start___kvm_ex_table);
  551. end = hyp_symbol_addr(__stop___kvm_ex_table);
  552. host_ctxt = __hyp_this_cpu_ptr(kvm_host_cpu_state);
  553. while (entry < end) {
  554. addr = (unsigned long)&entry->insn + entry->insn;
  555. fixup = (unsigned long)&entry->fixup + entry->fixup;
  556. if (addr != elr_el2) {
  557. entry++;
  558. continue;
  559. }
  560. write_sysreg(fixup, elr_el2);
  561. return;
  562. }
  563. hyp_panic(host_ctxt);
  564. }