PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm64/kvm/hyp/switch.c

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