switch.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/jump_label.h>
  18. #include <asm/kvm_asm.h>
  19. #include <asm/kvm_hyp.h>
  20. #include <asm/kvm_mmu.h>
  21. __asm__(".arch_extension virt");
  22. /*
  23. * Activate the traps, saving the host's fpexc register before
  24. * overwriting it. We'll restore it on VM exit.
  25. */
  26. static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu, u32 *fpexc_host)
  27. {
  28. u32 val;
  29. /*
  30. * We are about to set HCPTR.TCP10/11 to trap all floating point
  31. * register accesses to HYP, however, the ARM ARM clearly states that
  32. * traps are only taken to HYP if the operation would not otherwise
  33. * trap to SVC. Therefore, always make sure that for 32-bit guests,
  34. * we set FPEXC.EN to prevent traps to SVC, when setting the TCP bits.
  35. */
  36. val = read_sysreg(VFP_FPEXC);
  37. *fpexc_host = val;
  38. if (!(val & FPEXC_EN)) {
  39. write_sysreg(val | FPEXC_EN, VFP_FPEXC);
  40. isb();
  41. }
  42. write_sysreg(vcpu->arch.hcr, HCR);
  43. /* Trap on AArch32 cp15 c15 accesses (EL1 or EL0) */
  44. write_sysreg(HSTR_T(15), HSTR);
  45. write_sysreg(HCPTR_TTA | HCPTR_TCP(10) | HCPTR_TCP(11), HCPTR);
  46. val = read_sysreg(HDCR);
  47. val |= HDCR_TPM | HDCR_TPMCR; /* trap performance monitors */
  48. val |= HDCR_TDRA | HDCR_TDOSA | HDCR_TDA; /* trap debug regs */
  49. write_sysreg(val, HDCR);
  50. }
  51. static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
  52. {
  53. u32 val;
  54. /*
  55. * If we pended a virtual abort, preserve it until it gets
  56. * cleared. See B1.9.9 (Virtual Abort exception) for details,
  57. * but the crucial bit is the zeroing of HCR.VA in the
  58. * pseudocode.
  59. */
  60. if (vcpu->arch.hcr & HCR_VA)
  61. vcpu->arch.hcr = read_sysreg(HCR);
  62. write_sysreg(0, HCR);
  63. write_sysreg(0, HSTR);
  64. val = read_sysreg(HDCR);
  65. write_sysreg(val & ~(HDCR_TPM | HDCR_TPMCR), HDCR);
  66. write_sysreg(0, HCPTR);
  67. }
  68. static void __hyp_text __activate_vm(struct kvm_vcpu *vcpu)
  69. {
  70. struct kvm *kvm = kern_hyp_va(vcpu->kvm);
  71. write_sysreg(kvm->arch.vttbr, VTTBR);
  72. write_sysreg(vcpu->arch.midr, VPIDR);
  73. }
  74. static void __hyp_text __deactivate_vm(struct kvm_vcpu *vcpu)
  75. {
  76. write_sysreg(0, VTTBR);
  77. write_sysreg(read_sysreg(MIDR), VPIDR);
  78. }
  79. static void __hyp_text __vgic_save_state(struct kvm_vcpu *vcpu)
  80. {
  81. if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
  82. __vgic_v3_save_state(vcpu);
  83. __vgic_v3_deactivate_traps(vcpu);
  84. }
  85. }
  86. static void __hyp_text __vgic_restore_state(struct kvm_vcpu *vcpu)
  87. {
  88. if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
  89. __vgic_v3_activate_traps(vcpu);
  90. __vgic_v3_restore_state(vcpu);
  91. }
  92. }
  93. static bool __hyp_text __populate_fault_info(struct kvm_vcpu *vcpu)
  94. {
  95. u32 hsr = read_sysreg(HSR);
  96. u8 ec = hsr >> HSR_EC_SHIFT;
  97. u32 hpfar, far;
  98. vcpu->arch.fault.hsr = hsr;
  99. if (ec == HSR_EC_IABT)
  100. far = read_sysreg(HIFAR);
  101. else if (ec == HSR_EC_DABT)
  102. far = read_sysreg(HDFAR);
  103. else
  104. return true;
  105. /*
  106. * B3.13.5 Reporting exceptions taken to the Non-secure PL2 mode:
  107. *
  108. * Abort on the stage 2 translation for a memory access from a
  109. * Non-secure PL1 or PL0 mode:
  110. *
  111. * For any Access flag fault or Translation fault, and also for any
  112. * Permission fault on the stage 2 translation of a memory access
  113. * made as part of a translation table walk for a stage 1 translation,
  114. * the HPFAR holds the IPA that caused the fault. Otherwise, the HPFAR
  115. * is UNKNOWN.
  116. */
  117. if (!(hsr & HSR_DABT_S1PTW) && (hsr & HSR_FSC_TYPE) == FSC_PERM) {
  118. u64 par, tmp;
  119. par = read_sysreg(PAR);
  120. write_sysreg(far, ATS1CPR);
  121. isb();
  122. tmp = read_sysreg(PAR);
  123. write_sysreg(par, PAR);
  124. if (unlikely(tmp & 1))
  125. return false; /* Translation failed, back to guest */
  126. hpfar = ((tmp >> 12) & ((1UL << 28) - 1)) << 4;
  127. } else {
  128. hpfar = read_sysreg(HPFAR);
  129. }
  130. vcpu->arch.fault.hxfar = far;
  131. vcpu->arch.fault.hpfar = hpfar;
  132. return true;
  133. }
  134. int __hyp_text __kvm_vcpu_run_nvhe(struct kvm_vcpu *vcpu)
  135. {
  136. struct kvm_cpu_context *host_ctxt;
  137. struct kvm_cpu_context *guest_ctxt;
  138. bool fp_enabled;
  139. u64 exit_code;
  140. u32 fpexc;
  141. vcpu = kern_hyp_va(vcpu);
  142. write_sysreg(vcpu, HTPIDR);
  143. host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
  144. guest_ctxt = &vcpu->arch.ctxt;
  145. __sysreg_save_state(host_ctxt);
  146. __banked_save_state(host_ctxt);
  147. __activate_traps(vcpu, &fpexc);
  148. __activate_vm(vcpu);
  149. __vgic_restore_state(vcpu);
  150. __timer_enable_traps(vcpu);
  151. __sysreg_restore_state(guest_ctxt);
  152. __banked_restore_state(guest_ctxt);
  153. /* Jump in the fire! */
  154. again:
  155. exit_code = __guest_enter(vcpu, host_ctxt);
  156. /* And we're baaack! */
  157. if (exit_code == ARM_EXCEPTION_HVC && !__populate_fault_info(vcpu))
  158. goto again;
  159. fp_enabled = __vfp_enabled();
  160. __banked_save_state(guest_ctxt);
  161. __sysreg_save_state(guest_ctxt);
  162. __timer_disable_traps(vcpu);
  163. __vgic_save_state(vcpu);
  164. __deactivate_traps(vcpu);
  165. __deactivate_vm(vcpu);
  166. __banked_restore_state(host_ctxt);
  167. __sysreg_restore_state(host_ctxt);
  168. if (fp_enabled) {
  169. __vfp_save_state(&guest_ctxt->vfp);
  170. __vfp_restore_state(&host_ctxt->vfp);
  171. }
  172. write_sysreg(fpexc, VFP_FPEXC);
  173. return exit_code;
  174. }
  175. static const char * const __hyp_panic_string[] = {
  176. [ARM_EXCEPTION_RESET] = "\nHYP panic: RST PC:%08x CPSR:%08x",
  177. [ARM_EXCEPTION_UNDEFINED] = "\nHYP panic: UNDEF PC:%08x CPSR:%08x",
  178. [ARM_EXCEPTION_SOFTWARE] = "\nHYP panic: SVC PC:%08x CPSR:%08x",
  179. [ARM_EXCEPTION_PREF_ABORT] = "\nHYP panic: PABRT PC:%08x CPSR:%08x",
  180. [ARM_EXCEPTION_DATA_ABORT] = "\nHYP panic: DABRT PC:%08x ADDR:%08x",
  181. [ARM_EXCEPTION_IRQ] = "\nHYP panic: IRQ PC:%08x CPSR:%08x",
  182. [ARM_EXCEPTION_FIQ] = "\nHYP panic: FIQ PC:%08x CPSR:%08x",
  183. [ARM_EXCEPTION_HVC] = "\nHYP panic: HVC PC:%08x CPSR:%08x",
  184. };
  185. void __hyp_text __noreturn __hyp_panic(int cause)
  186. {
  187. u32 elr = read_special(ELR_hyp);
  188. u32 val;
  189. if (cause == ARM_EXCEPTION_DATA_ABORT)
  190. val = read_sysreg(HDFAR);
  191. else
  192. val = read_special(SPSR);
  193. if (read_sysreg(VTTBR)) {
  194. struct kvm_vcpu *vcpu;
  195. struct kvm_cpu_context *host_ctxt;
  196. vcpu = (struct kvm_vcpu *)read_sysreg(HTPIDR);
  197. host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
  198. __timer_disable_traps(vcpu);
  199. __deactivate_traps(vcpu);
  200. __deactivate_vm(vcpu);
  201. __banked_restore_state(host_ctxt);
  202. __sysreg_restore_state(host_ctxt);
  203. }
  204. /* Call panic for real */
  205. __hyp_do_panic(__hyp_panic_string[cause], elr, val);
  206. unreachable();
  207. }