switch.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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/types.h>
  18. #include <linux/jump_label.h>
  19. #include <uapi/linux/psci.h>
  20. #include <kvm/arm_psci.h>
  21. #include <asm/kvm_asm.h>
  22. #include <asm/kvm_emulate.h>
  23. #include <asm/kvm_hyp.h>
  24. #include <asm/kvm_mmu.h>
  25. #include <asm/fpsimd.h>
  26. #include <asm/debug-monitors.h>
  27. static bool __hyp_text __fpsimd_enabled_nvhe(void)
  28. {
  29. return !(read_sysreg(cptr_el2) & CPTR_EL2_TFP);
  30. }
  31. static bool __hyp_text __fpsimd_enabled_vhe(void)
  32. {
  33. return !!(read_sysreg(cpacr_el1) & CPACR_EL1_FPEN);
  34. }
  35. static hyp_alternate_select(__fpsimd_is_enabled,
  36. __fpsimd_enabled_nvhe, __fpsimd_enabled_vhe,
  37. ARM64_HAS_VIRT_HOST_EXTN);
  38. bool __hyp_text __fpsimd_enabled(void)
  39. {
  40. return __fpsimd_is_enabled()();
  41. }
  42. static void __hyp_text __activate_traps_vhe(void)
  43. {
  44. u64 val;
  45. val = read_sysreg(cpacr_el1);
  46. val |= CPACR_EL1_TTA;
  47. val &= ~(CPACR_EL1_FPEN | CPACR_EL1_ZEN);
  48. write_sysreg(val, cpacr_el1);
  49. write_sysreg(kvm_get_hyp_vector(), vbar_el1);
  50. }
  51. static void __hyp_text __activate_traps_nvhe(void)
  52. {
  53. u64 val;
  54. val = CPTR_EL2_DEFAULT;
  55. val |= CPTR_EL2_TTA | CPTR_EL2_TFP | CPTR_EL2_TZ;
  56. write_sysreg(val, cptr_el2);
  57. }
  58. static hyp_alternate_select(__activate_traps_arch,
  59. __activate_traps_nvhe, __activate_traps_vhe,
  60. ARM64_HAS_VIRT_HOST_EXTN);
  61. static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
  62. {
  63. u64 val;
  64. /*
  65. * We are about to set CPTR_EL2.TFP to trap all floating point
  66. * register accesses to EL2, however, the ARM ARM clearly states that
  67. * traps are only taken to EL2 if the operation would not otherwise
  68. * trap to EL1. Therefore, always make sure that for 32-bit guests,
  69. * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
  70. * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
  71. * it will cause an exception.
  72. */
  73. val = vcpu->arch.hcr_el2;
  74. if (!(val & HCR_RW) && system_supports_fpsimd()) {
  75. write_sysreg(1 << 30, fpexc32_el2);
  76. isb();
  77. }
  78. if (val & HCR_RW) /* for AArch64 only: */
  79. val |= HCR_TID3; /* TID3: trap feature register accesses */
  80. write_sysreg(val, hcr_el2);
  81. if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN) && (val & HCR_VSE))
  82. write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2);
  83. /* Trap on AArch32 cp15 c15 accesses (EL1 or EL0) */
  84. write_sysreg(1 << 15, hstr_el2);
  85. /*
  86. * Make sure we trap PMU access from EL0 to EL2. Also sanitize
  87. * PMSELR_EL0 to make sure it never contains the cycle
  88. * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
  89. * EL1 instead of being trapped to EL2.
  90. */
  91. write_sysreg(0, pmselr_el0);
  92. write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
  93. write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
  94. __activate_traps_arch()();
  95. }
  96. static void __hyp_text __deactivate_traps_vhe(void)
  97. {
  98. extern char vectors[]; /* kernel exception vectors */
  99. u64 mdcr_el2 = read_sysreg(mdcr_el2);
  100. mdcr_el2 &= MDCR_EL2_HPMN_MASK |
  101. MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
  102. MDCR_EL2_TPMS;
  103. write_sysreg(mdcr_el2, mdcr_el2);
  104. write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
  105. write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1);
  106. write_sysreg(vectors, vbar_el1);
  107. }
  108. static void __hyp_text __deactivate_traps_nvhe(void)
  109. {
  110. u64 mdcr_el2 = read_sysreg(mdcr_el2);
  111. mdcr_el2 &= MDCR_EL2_HPMN_MASK;
  112. mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT;
  113. write_sysreg(mdcr_el2, mdcr_el2);
  114. write_sysreg(HCR_RW, hcr_el2);
  115. write_sysreg(CPTR_EL2_DEFAULT, cptr_el2);
  116. }
  117. static hyp_alternate_select(__deactivate_traps_arch,
  118. __deactivate_traps_nvhe, __deactivate_traps_vhe,
  119. ARM64_HAS_VIRT_HOST_EXTN);
  120. static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
  121. {
  122. /*
  123. * If we pended a virtual abort, preserve it until it gets
  124. * cleared. See D1.14.3 (Virtual Interrupts) for details, but
  125. * the crucial bit is "On taking a vSError interrupt,
  126. * HCR_EL2.VSE is cleared to 0."
  127. */
  128. if (vcpu->arch.hcr_el2 & HCR_VSE)
  129. vcpu->arch.hcr_el2 = read_sysreg(hcr_el2);
  130. __deactivate_traps_arch()();
  131. write_sysreg(0, hstr_el2);
  132. write_sysreg(0, pmuserenr_el0);
  133. }
  134. static void __hyp_text __activate_vm(struct kvm_vcpu *vcpu)
  135. {
  136. struct kvm *kvm = kern_hyp_va(vcpu->kvm);
  137. write_sysreg(kvm->arch.vttbr, vttbr_el2);
  138. }
  139. static void __hyp_text __deactivate_vm(struct kvm_vcpu *vcpu)
  140. {
  141. write_sysreg(0, vttbr_el2);
  142. }
  143. static void __hyp_text __vgic_save_state(struct kvm_vcpu *vcpu)
  144. {
  145. if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  146. __vgic_v3_save_state(vcpu);
  147. else
  148. __vgic_v2_save_state(vcpu);
  149. write_sysreg(read_sysreg(hcr_el2) & ~HCR_INT_OVERRIDE, hcr_el2);
  150. }
  151. static void __hyp_text __vgic_restore_state(struct kvm_vcpu *vcpu)
  152. {
  153. u64 val;
  154. val = read_sysreg(hcr_el2);
  155. val |= HCR_INT_OVERRIDE;
  156. val |= vcpu->arch.irq_lines;
  157. write_sysreg(val, hcr_el2);
  158. if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  159. __vgic_v3_restore_state(vcpu);
  160. else
  161. __vgic_v2_restore_state(vcpu);
  162. }
  163. static bool __hyp_text __true_value(void)
  164. {
  165. return true;
  166. }
  167. static bool __hyp_text __false_value(void)
  168. {
  169. return false;
  170. }
  171. static hyp_alternate_select(__check_arm_834220,
  172. __false_value, __true_value,
  173. ARM64_WORKAROUND_834220);
  174. static bool __hyp_text __translate_far_to_hpfar(u64 far, u64 *hpfar)
  175. {
  176. u64 par, tmp;
  177. /*
  178. * Resolve the IPA the hard way using the guest VA.
  179. *
  180. * Stage-1 translation already validated the memory access
  181. * rights. As such, we can use the EL1 translation regime, and
  182. * don't have to distinguish between EL0 and EL1 access.
  183. *
  184. * We do need to save/restore PAR_EL1 though, as we haven't
  185. * saved the guest context yet, and we may return early...
  186. */
  187. par = read_sysreg(par_el1);
  188. asm volatile("at s1e1r, %0" : : "r" (far));
  189. isb();
  190. tmp = read_sysreg(par_el1);
  191. write_sysreg(par, par_el1);
  192. if (unlikely(tmp & 1))
  193. return false; /* Translation failed, back to guest */
  194. /* Convert PAR to HPFAR format */
  195. *hpfar = ((tmp >> 12) & ((1UL << 36) - 1)) << 4;
  196. return true;
  197. }
  198. static bool __hyp_text __populate_fault_info(struct kvm_vcpu *vcpu)
  199. {
  200. u8 ec;
  201. u64 esr;
  202. u64 hpfar, far;
  203. esr = vcpu->arch.fault.esr_el2;
  204. ec = ESR_ELx_EC(esr);
  205. if (ec != ESR_ELx_EC_DABT_LOW && ec != ESR_ELx_EC_IABT_LOW)
  206. return true;
  207. far = read_sysreg_el2(far);
  208. /*
  209. * The HPFAR can be invalid if the stage 2 fault did not
  210. * happen during a stage 1 page table walk (the ESR_EL2.S1PTW
  211. * bit is clear) and one of the two following cases are true:
  212. * 1. The fault was due to a permission fault
  213. * 2. The processor carries errata 834220
  214. *
  215. * Therefore, for all non S1PTW faults where we either have a
  216. * permission fault or the errata workaround is enabled, we
  217. * resolve the IPA using the AT instruction.
  218. */
  219. if (!(esr & ESR_ELx_S1PTW) &&
  220. (__check_arm_834220()() || (esr & ESR_ELx_FSC_TYPE) == FSC_PERM)) {
  221. if (!__translate_far_to_hpfar(far, &hpfar))
  222. return false;
  223. } else {
  224. hpfar = read_sysreg(hpfar_el2);
  225. }
  226. vcpu->arch.fault.far_el2 = far;
  227. vcpu->arch.fault.hpfar_el2 = hpfar;
  228. return true;
  229. }
  230. /* Skip an instruction which has been emulated. Returns true if
  231. * execution can continue or false if we need to exit hyp mode because
  232. * single-step was in effect.
  233. */
  234. static bool __hyp_text __skip_instr(struct kvm_vcpu *vcpu)
  235. {
  236. *vcpu_pc(vcpu) = read_sysreg_el2(elr);
  237. if (vcpu_mode_is_32bit(vcpu)) {
  238. vcpu->arch.ctxt.gp_regs.regs.pstate = read_sysreg_el2(spsr);
  239. kvm_skip_instr32(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
  240. write_sysreg_el2(vcpu->arch.ctxt.gp_regs.regs.pstate, spsr);
  241. } else {
  242. *vcpu_pc(vcpu) += 4;
  243. }
  244. write_sysreg_el2(*vcpu_pc(vcpu), elr);
  245. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  246. vcpu->arch.fault.esr_el2 =
  247. (ESR_ELx_EC_SOFTSTP_LOW << ESR_ELx_EC_SHIFT) | 0x22;
  248. return false;
  249. } else {
  250. return true;
  251. }
  252. }
  253. int __hyp_text __kvm_vcpu_run(struct kvm_vcpu *vcpu)
  254. {
  255. struct kvm_cpu_context *host_ctxt;
  256. struct kvm_cpu_context *guest_ctxt;
  257. bool fp_enabled;
  258. u64 exit_code;
  259. vcpu = kern_hyp_va(vcpu);
  260. host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
  261. host_ctxt->__hyp_running_vcpu = vcpu;
  262. guest_ctxt = &vcpu->arch.ctxt;
  263. __sysreg_save_host_state(host_ctxt);
  264. __debug_cond_save_host_state(vcpu);
  265. __activate_traps(vcpu);
  266. __activate_vm(vcpu);
  267. __vgic_restore_state(vcpu);
  268. __timer_enable_traps(vcpu);
  269. /*
  270. * We must restore the 32-bit state before the sysregs, thanks
  271. * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
  272. */
  273. __sysreg32_restore_state(vcpu);
  274. __sysreg_restore_guest_state(guest_ctxt);
  275. __debug_restore_state(vcpu, kern_hyp_va(vcpu->arch.debug_ptr), guest_ctxt);
  276. /* Jump in the fire! */
  277. again:
  278. exit_code = __guest_enter(vcpu, host_ctxt);
  279. /* And we're baaack! */
  280. if (ARM_EXCEPTION_CODE(exit_code) != ARM_EXCEPTION_IRQ)
  281. vcpu->arch.fault.esr_el2 = read_sysreg_el2(esr);
  282. /*
  283. * We're using the raw exception code in order to only process
  284. * the trap if no SError is pending. We will come back to the
  285. * same PC once the SError has been injected, and replay the
  286. * trapping instruction.
  287. */
  288. if (exit_code == ARM_EXCEPTION_TRAP && !__populate_fault_info(vcpu))
  289. goto again;
  290. if (static_branch_unlikely(&vgic_v2_cpuif_trap) &&
  291. exit_code == ARM_EXCEPTION_TRAP) {
  292. bool valid;
  293. valid = kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_DABT_LOW &&
  294. kvm_vcpu_trap_get_fault_type(vcpu) == FSC_FAULT &&
  295. kvm_vcpu_dabt_isvalid(vcpu) &&
  296. !kvm_vcpu_dabt_isextabt(vcpu) &&
  297. !kvm_vcpu_dabt_iss1tw(vcpu);
  298. if (valid) {
  299. int ret = __vgic_v2_perform_cpuif_access(vcpu);
  300. if (ret == 1) {
  301. if (__skip_instr(vcpu))
  302. goto again;
  303. else
  304. exit_code = ARM_EXCEPTION_TRAP;
  305. }
  306. if (ret == -1) {
  307. /* Promote an illegal access to an
  308. * SError. If we would be returning
  309. * due to single-step clear the SS
  310. * bit so handle_exit knows what to
  311. * do after dealing with the error.
  312. */
  313. if (!__skip_instr(vcpu))
  314. *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
  315. exit_code = ARM_EXCEPTION_EL1_SERROR;
  316. }
  317. /* 0 falls through to be handler out of EL2 */
  318. }
  319. }
  320. if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
  321. exit_code == ARM_EXCEPTION_TRAP &&
  322. (kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 ||
  323. kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_CP15_32)) {
  324. int ret = __vgic_v3_perform_cpuif_access(vcpu);
  325. if (ret == 1) {
  326. if (__skip_instr(vcpu))
  327. goto again;
  328. else
  329. exit_code = ARM_EXCEPTION_TRAP;
  330. }
  331. /* 0 falls through to be handled out of EL2 */
  332. }
  333. if (cpus_have_const_cap(ARM64_HARDEN_BP_POST_GUEST_EXIT)) {
  334. u32 midr = read_cpuid_id();
  335. /* Apply BTAC predictors mitigation to all Falkor chips */
  336. if ((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR_V1)
  337. __qcom_hyp_sanitize_btac_predictors();
  338. }
  339. fp_enabled = __fpsimd_enabled();
  340. __sysreg_save_guest_state(guest_ctxt);
  341. __sysreg32_save_state(vcpu);
  342. __timer_disable_traps(vcpu);
  343. __vgic_save_state(vcpu);
  344. __deactivate_traps(vcpu);
  345. __deactivate_vm(vcpu);
  346. __sysreg_restore_host_state(host_ctxt);
  347. if (fp_enabled) {
  348. __fpsimd_save_state(&guest_ctxt->gp_regs.fp_regs);
  349. __fpsimd_restore_state(&host_ctxt->gp_regs.fp_regs);
  350. }
  351. __debug_save_state(vcpu, kern_hyp_va(vcpu->arch.debug_ptr), guest_ctxt);
  352. /*
  353. * This must come after restoring the host sysregs, since a non-VHE
  354. * system may enable SPE here and make use of the TTBRs.
  355. */
  356. __debug_cond_restore_host_state(vcpu);
  357. return exit_code;
  358. }
  359. static const char __hyp_panic_string[] = "HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n";
  360. static void __hyp_text __hyp_call_panic_nvhe(u64 spsr, u64 elr, u64 par,
  361. struct kvm_vcpu *vcpu)
  362. {
  363. unsigned long str_va;
  364. /*
  365. * Force the panic string to be loaded from the literal pool,
  366. * making sure it is a kernel address and not a PC-relative
  367. * reference.
  368. */
  369. asm volatile("ldr %0, =__hyp_panic_string" : "=r" (str_va));
  370. __hyp_do_panic(str_va,
  371. spsr, elr,
  372. read_sysreg(esr_el2), read_sysreg_el2(far),
  373. read_sysreg(hpfar_el2), par, vcpu);
  374. }
  375. static void __hyp_text __hyp_call_panic_vhe(u64 spsr, u64 elr, u64 par,
  376. struct kvm_vcpu *vcpu)
  377. {
  378. panic(__hyp_panic_string,
  379. spsr, elr,
  380. read_sysreg_el2(esr), read_sysreg_el2(far),
  381. read_sysreg(hpfar_el2), par, vcpu);
  382. }
  383. static hyp_alternate_select(__hyp_call_panic,
  384. __hyp_call_panic_nvhe, __hyp_call_panic_vhe,
  385. ARM64_HAS_VIRT_HOST_EXTN);
  386. void __hyp_text __noreturn hyp_panic(struct kvm_cpu_context *__host_ctxt)
  387. {
  388. struct kvm_vcpu *vcpu = NULL;
  389. u64 spsr = read_sysreg_el2(spsr);
  390. u64 elr = read_sysreg_el2(elr);
  391. u64 par = read_sysreg(par_el1);
  392. if (read_sysreg(vttbr_el2)) {
  393. struct kvm_cpu_context *host_ctxt;
  394. host_ctxt = kern_hyp_va(__host_ctxt);
  395. vcpu = host_ctxt->__hyp_running_vcpu;
  396. __timer_disable_traps(vcpu);
  397. __deactivate_traps(vcpu);
  398. __deactivate_vm(vcpu);
  399. __sysreg_restore_host_state(host_ctxt);
  400. }
  401. /* Call panic for real */
  402. __hyp_call_panic()(spsr, elr, par, vcpu);
  403. unreachable();
  404. }