sysreg-sr.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012-2015 - ARM Ltd
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. */
  6. #include <hyp/sysreg-sr.h>
  7. #include <linux/compiler.h>
  8. #include <linux/kvm_host.h>
  9. #include <asm/kprobes.h>
  10. #include <asm/kvm_asm.h>
  11. #include <asm/kvm_emulate.h>
  12. #include <asm/kvm_hyp.h>
  13. #include <asm/kvm_nested.h>
  14. static void __sysreg_save_vel2_state(struct kvm_vcpu *vcpu)
  15. {
  16. /* These registers are common with EL1 */
  17. __vcpu_assign_sys_reg(vcpu, PAR_EL1, read_sysreg(par_el1));
  18. __vcpu_assign_sys_reg(vcpu, TPIDR_EL1, read_sysreg(tpidr_el1));
  19. __vcpu_assign_sys_reg(vcpu, ESR_EL2, read_sysreg_el1(SYS_ESR));
  20. __vcpu_assign_sys_reg(vcpu, AFSR0_EL2, read_sysreg_el1(SYS_AFSR0));
  21. __vcpu_assign_sys_reg(vcpu, AFSR1_EL2, read_sysreg_el1(SYS_AFSR1));
  22. __vcpu_assign_sys_reg(vcpu, FAR_EL2, read_sysreg_el1(SYS_FAR));
  23. __vcpu_assign_sys_reg(vcpu, MAIR_EL2, read_sysreg_el1(SYS_MAIR));
  24. __vcpu_assign_sys_reg(vcpu, VBAR_EL2, read_sysreg_el1(SYS_VBAR));
  25. __vcpu_assign_sys_reg(vcpu, CONTEXTIDR_EL2, read_sysreg_el1(SYS_CONTEXTIDR));
  26. __vcpu_assign_sys_reg(vcpu, AMAIR_EL2, read_sysreg_el1(SYS_AMAIR));
  27. /*
  28. * In VHE mode those registers are compatible between EL1 and EL2,
  29. * and the guest uses the _EL1 versions on the CPU naturally.
  30. * So we save them into their _EL2 versions here.
  31. * For nVHE mode we trap accesses to those registers, so our
  32. * _EL2 copy in sys_regs[] is always up-to-date and we don't need
  33. * to save anything here.
  34. */
  35. if (vcpu_el2_e2h_is_set(vcpu)) {
  36. u64 val;
  37. /*
  38. * We don't save CPTR_EL2, as accesses to CPACR_EL1
  39. * are always trapped, ensuring that the in-memory
  40. * copy is always up-to-date. A small blessing...
  41. */
  42. __vcpu_assign_sys_reg(vcpu, SCTLR_EL2, read_sysreg_el1(SYS_SCTLR));
  43. __vcpu_assign_sys_reg(vcpu, TTBR0_EL2, read_sysreg_el1(SYS_TTBR0));
  44. __vcpu_assign_sys_reg(vcpu, TTBR1_EL2, read_sysreg_el1(SYS_TTBR1));
  45. __vcpu_assign_sys_reg(vcpu, TCR_EL2, read_sysreg_el1(SYS_TCR));
  46. if (ctxt_has_tcrx(&vcpu->arch.ctxt)) {
  47. __vcpu_assign_sys_reg(vcpu, TCR2_EL2, read_sysreg_el1(SYS_TCR2));
  48. if (ctxt_has_s1pie(&vcpu->arch.ctxt)) {
  49. __vcpu_assign_sys_reg(vcpu, PIRE0_EL2, read_sysreg_el1(SYS_PIRE0));
  50. __vcpu_assign_sys_reg(vcpu, PIR_EL2, read_sysreg_el1(SYS_PIR));
  51. }
  52. if (ctxt_has_s1poe(&vcpu->arch.ctxt))
  53. __vcpu_assign_sys_reg(vcpu, POR_EL2, read_sysreg_el1(SYS_POR));
  54. }
  55. /*
  56. * The EL1 view of CNTKCTL_EL1 has a bunch of RES0 bits where
  57. * the interesting CNTHCTL_EL2 bits live. So preserve these
  58. * bits when reading back the guest-visible value.
  59. */
  60. val = read_sysreg_el1(SYS_CNTKCTL);
  61. val &= CNTKCTL_VALID_BITS;
  62. __vcpu_rmw_sys_reg(vcpu, CNTHCTL_EL2, &=, ~CNTKCTL_VALID_BITS);
  63. __vcpu_rmw_sys_reg(vcpu, CNTHCTL_EL2, |=, val);
  64. }
  65. __vcpu_assign_sys_reg(vcpu, SP_EL2, read_sysreg(sp_el1));
  66. __vcpu_assign_sys_reg(vcpu, ELR_EL2, read_sysreg_el1(SYS_ELR));
  67. __vcpu_assign_sys_reg(vcpu, SPSR_EL2, read_sysreg_el1(SYS_SPSR));
  68. if (ctxt_has_sctlr2(&vcpu->arch.ctxt))
  69. __vcpu_assign_sys_reg(vcpu, SCTLR2_EL2, read_sysreg_el1(SYS_SCTLR2));
  70. }
  71. static void __sysreg_restore_vel2_state(struct kvm_vcpu *vcpu)
  72. {
  73. u64 val;
  74. /* These registers are common with EL1 */
  75. write_sysreg(__vcpu_sys_reg(vcpu, PAR_EL1), par_el1);
  76. write_sysreg(__vcpu_sys_reg(vcpu, TPIDR_EL1), tpidr_el1);
  77. write_sysreg(ctxt_midr_el1(&vcpu->arch.ctxt), vpidr_el2);
  78. write_sysreg(__vcpu_sys_reg(vcpu, MPIDR_EL1), vmpidr_el2);
  79. write_sysreg_el1(__vcpu_sys_reg(vcpu, MAIR_EL2), SYS_MAIR);
  80. write_sysreg_el1(__vcpu_sys_reg(vcpu, VBAR_EL2), SYS_VBAR);
  81. write_sysreg_el1(__vcpu_sys_reg(vcpu, CONTEXTIDR_EL2), SYS_CONTEXTIDR);
  82. write_sysreg_el1(__vcpu_sys_reg(vcpu, AMAIR_EL2), SYS_AMAIR);
  83. if (vcpu_el2_e2h_is_set(vcpu)) {
  84. /*
  85. * In VHE mode those registers are compatible between
  86. * EL1 and EL2.
  87. */
  88. write_sysreg_el1(__vcpu_sys_reg(vcpu, SCTLR_EL2), SYS_SCTLR);
  89. write_sysreg_el1(__vcpu_sys_reg(vcpu, CPTR_EL2), SYS_CPACR);
  90. write_sysreg_el1(__vcpu_sys_reg(vcpu, TTBR0_EL2), SYS_TTBR0);
  91. write_sysreg_el1(__vcpu_sys_reg(vcpu, TTBR1_EL2), SYS_TTBR1);
  92. write_sysreg_el1(__vcpu_sys_reg(vcpu, TCR_EL2), SYS_TCR);
  93. write_sysreg_el1(__vcpu_sys_reg(vcpu, CNTHCTL_EL2), SYS_CNTKCTL);
  94. } else {
  95. /*
  96. * CNTHCTL_EL2 only affects EL1 when running nVHE, so
  97. * no need to restore it.
  98. */
  99. val = translate_sctlr_el2_to_sctlr_el1(__vcpu_sys_reg(vcpu, SCTLR_EL2));
  100. write_sysreg_el1(val, SYS_SCTLR);
  101. val = translate_cptr_el2_to_cpacr_el1(__vcpu_sys_reg(vcpu, CPTR_EL2));
  102. write_sysreg_el1(val, SYS_CPACR);
  103. val = translate_ttbr0_el2_to_ttbr0_el1(__vcpu_sys_reg(vcpu, TTBR0_EL2));
  104. write_sysreg_el1(val, SYS_TTBR0);
  105. val = translate_tcr_el2_to_tcr_el1(__vcpu_sys_reg(vcpu, TCR_EL2));
  106. write_sysreg_el1(val, SYS_TCR);
  107. }
  108. if (ctxt_has_tcrx(&vcpu->arch.ctxt)) {
  109. write_sysreg_el1(__vcpu_sys_reg(vcpu, TCR2_EL2), SYS_TCR2);
  110. if (ctxt_has_s1pie(&vcpu->arch.ctxt)) {
  111. write_sysreg_el1(__vcpu_sys_reg(vcpu, PIR_EL2), SYS_PIR);
  112. write_sysreg_el1(__vcpu_sys_reg(vcpu, PIRE0_EL2), SYS_PIRE0);
  113. }
  114. if (ctxt_has_s1poe(&vcpu->arch.ctxt))
  115. write_sysreg_el1(__vcpu_sys_reg(vcpu, POR_EL2), SYS_POR);
  116. }
  117. write_sysreg_el1(__vcpu_sys_reg(vcpu, ESR_EL2), SYS_ESR);
  118. write_sysreg_el1(__vcpu_sys_reg(vcpu, AFSR0_EL2), SYS_AFSR0);
  119. write_sysreg_el1(__vcpu_sys_reg(vcpu, AFSR1_EL2), SYS_AFSR1);
  120. write_sysreg_el1(__vcpu_sys_reg(vcpu, FAR_EL2), SYS_FAR);
  121. write_sysreg(__vcpu_sys_reg(vcpu, SP_EL2), sp_el1);
  122. write_sysreg_el1(__vcpu_sys_reg(vcpu, ELR_EL2), SYS_ELR);
  123. write_sysreg_el1(__vcpu_sys_reg(vcpu, SPSR_EL2), SYS_SPSR);
  124. if (ctxt_has_sctlr2(&vcpu->arch.ctxt))
  125. write_sysreg_el1(__vcpu_sys_reg(vcpu, SCTLR2_EL2), SYS_SCTLR2);
  126. }
  127. /*
  128. * VHE: Host and guest must save mdscr_el1 and sp_el0 (and the PC and
  129. * pstate, which are handled as part of the el2 return state) on every
  130. * switch (sp_el0 is being dealt with in the assembly code).
  131. * tpidr_el0 and tpidrro_el0 only need to be switched when going
  132. * to host userspace or a different VCPU. EL1 registers only need to be
  133. * switched when potentially going to run a different VCPU. The latter two
  134. * classes are handled as part of kvm_arch_vcpu_load and kvm_arch_vcpu_put.
  135. */
  136. void sysreg_save_host_state_vhe(struct kvm_cpu_context *ctxt)
  137. {
  138. __sysreg_save_common_state(ctxt);
  139. }
  140. NOKPROBE_SYMBOL(sysreg_save_host_state_vhe);
  141. void sysreg_save_guest_state_vhe(struct kvm_cpu_context *ctxt)
  142. {
  143. __sysreg_save_common_state(ctxt);
  144. __sysreg_save_el2_return_state(ctxt);
  145. }
  146. NOKPROBE_SYMBOL(sysreg_save_guest_state_vhe);
  147. void sysreg_restore_host_state_vhe(struct kvm_cpu_context *ctxt)
  148. {
  149. __sysreg_restore_common_state(ctxt);
  150. }
  151. NOKPROBE_SYMBOL(sysreg_restore_host_state_vhe);
  152. void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt)
  153. {
  154. __sysreg_restore_common_state(ctxt);
  155. __sysreg_restore_el2_return_state(ctxt);
  156. }
  157. NOKPROBE_SYMBOL(sysreg_restore_guest_state_vhe);
  158. /**
  159. * __vcpu_load_switch_sysregs - Load guest system registers to the physical CPU
  160. *
  161. * @vcpu: The VCPU pointer
  162. *
  163. * Load system registers that do not affect the host's execution, for
  164. * example EL1 system registers on a VHE system where the host kernel
  165. * runs at EL2. This function is called from KVM's vcpu_load() function
  166. * and loading system register state early avoids having to load them on
  167. * every entry to the VM.
  168. */
  169. void __vcpu_load_switch_sysregs(struct kvm_vcpu *vcpu)
  170. {
  171. struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt;
  172. struct kvm_cpu_context *host_ctxt;
  173. u64 midr, mpidr;
  174. host_ctxt = host_data_ptr(host_ctxt);
  175. __sysreg_save_user_state(host_ctxt);
  176. /*
  177. * When running a normal EL1 guest, we only load a new vcpu
  178. * after a context switch, which involves a DSB, so all
  179. * speculative EL1&0 walks will have already completed.
  180. * If running NV, the vcpu may transition between vEL1 and
  181. * vEL2 without a context switch, so make sure we complete
  182. * those walks before loading a new context.
  183. */
  184. if (vcpu_has_nv(vcpu))
  185. dsb(nsh);
  186. /*
  187. * Load guest EL1 and user state
  188. *
  189. * We must restore the 32-bit state before the sysregs, thanks
  190. * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
  191. */
  192. __sysreg32_restore_state(vcpu);
  193. __sysreg_restore_user_state(guest_ctxt);
  194. if (unlikely(is_hyp_ctxt(vcpu))) {
  195. __sysreg_restore_vel2_state(vcpu);
  196. } else {
  197. if (vcpu_has_nv(vcpu)) {
  198. /*
  199. * As we're restoring a nested guest, set the value
  200. * provided by the guest hypervisor.
  201. */
  202. midr = ctxt_sys_reg(guest_ctxt, VPIDR_EL2);
  203. mpidr = ctxt_sys_reg(guest_ctxt, VMPIDR_EL2);
  204. } else {
  205. midr = ctxt_midr_el1(guest_ctxt);
  206. mpidr = ctxt_sys_reg(guest_ctxt, MPIDR_EL1);
  207. }
  208. __sysreg_restore_el1_state(guest_ctxt, midr, mpidr);
  209. }
  210. vcpu_set_flag(vcpu, SYSREGS_ON_CPU);
  211. }
  212. /**
  213. * __vcpu_put_switch_sysregs - Restore host system registers to the physical CPU
  214. *
  215. * @vcpu: The VCPU pointer
  216. *
  217. * Save guest system registers that do not affect the host's execution, for
  218. * example EL1 system registers on a VHE system where the host kernel
  219. * runs at EL2. This function is called from KVM's vcpu_put() function
  220. * and deferring saving system register state until we're no longer running the
  221. * VCPU avoids having to save them on every exit from the VM.
  222. */
  223. void __vcpu_put_switch_sysregs(struct kvm_vcpu *vcpu)
  224. {
  225. struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt;
  226. struct kvm_cpu_context *host_ctxt;
  227. host_ctxt = host_data_ptr(host_ctxt);
  228. if (unlikely(is_hyp_ctxt(vcpu)))
  229. __sysreg_save_vel2_state(vcpu);
  230. else
  231. __sysreg_save_el1_state(guest_ctxt);
  232. __sysreg_save_user_state(guest_ctxt);
  233. __sysreg32_save_state(vcpu);
  234. /* Restore host user state */
  235. __sysreg_restore_user_state(host_ctxt);
  236. vcpu_clear_flag(vcpu, SYSREGS_ON_CPU);
  237. }