switch.c 6.5 KB

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