switch.c 6.7 KB

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