switch.c 12 KB

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