hyp-stub.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Hypervisor stub
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. * Author: Marc Zyngier <[email protected]>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/linkage.h>
  10. #include <asm/assembler.h>
  11. #include <asm/el2_setup.h>
  12. #include <asm/kvm_arm.h>
  13. #include <asm/kvm_asm.h>
  14. #include <asm/ptrace.h>
  15. #include <asm/virt.h>
  16. .text
  17. .pushsection .hyp.text, "ax"
  18. .align 11
  19. SYM_CODE_START(__hyp_stub_vectors)
  20. ventry el2_sync_invalid // Synchronous EL2t
  21. ventry el2_irq_invalid // IRQ EL2t
  22. ventry el2_fiq_invalid // FIQ EL2t
  23. ventry el2_error_invalid // Error EL2t
  24. ventry elx_sync // Synchronous EL2h
  25. ventry el2_irq_invalid // IRQ EL2h
  26. ventry el2_fiq_invalid // FIQ EL2h
  27. ventry el2_error_invalid // Error EL2h
  28. ventry elx_sync // Synchronous 64-bit EL1
  29. ventry el1_irq_invalid // IRQ 64-bit EL1
  30. ventry el1_fiq_invalid // FIQ 64-bit EL1
  31. ventry el1_error_invalid // Error 64-bit EL1
  32. ventry el1_sync_invalid // Synchronous 32-bit EL1
  33. ventry el1_irq_invalid // IRQ 32-bit EL1
  34. ventry el1_fiq_invalid // FIQ 32-bit EL1
  35. ventry el1_error_invalid // Error 32-bit EL1
  36. SYM_CODE_END(__hyp_stub_vectors)
  37. .align 11
  38. SYM_CODE_START_LOCAL(elx_sync)
  39. cmp x0, #HVC_SET_VECTORS
  40. b.ne 1f
  41. msr vbar_el2, x1
  42. b 9f
  43. 1: cmp x0, #HVC_FINALISE_EL2
  44. b.eq __finalise_el2
  45. 2: cmp x0, #HVC_SOFT_RESTART
  46. b.ne 3f
  47. mov x0, x2
  48. mov x2, x4
  49. mov x4, x1
  50. mov x1, x3
  51. br x4 // no return
  52. 3: cmp x0, #HVC_RESET_VECTORS
  53. beq 9f // Nothing to reset!
  54. /* Someone called kvm_call_hyp() against the hyp-stub... */
  55. mov_q x0, HVC_STUB_ERR
  56. eret
  57. 9: mov x0, xzr
  58. eret
  59. SYM_CODE_END(elx_sync)
  60. SYM_CODE_START_LOCAL(__finalise_el2)
  61. finalise_el2_state
  62. // nVHE? No way! Give me the real thing!
  63. // Sanity check: MMU *must* be off
  64. mrs x1, sctlr_el2
  65. tbnz x1, #0, 1f
  66. // Needs to be VHE capable, obviously
  67. check_override id_aa64mmfr1 ID_AA64MMFR1_EL1_VH_SHIFT 2f 1f x1 x2
  68. 1: mov_q x0, HVC_STUB_ERR
  69. eret
  70. 2:
  71. // Engage the VHE magic!
  72. mov_q x0, HCR_HOST_VHE_FLAGS
  73. msr hcr_el2, x0
  74. isb
  75. // Use the EL1 allocated stack, per-cpu offset
  76. mrs x0, sp_el1
  77. mov sp, x0
  78. mrs x0, tpidr_el1
  79. msr tpidr_el2, x0
  80. // FP configuration, vectors
  81. mrs_s x0, SYS_CPACR_EL12
  82. msr cpacr_el1, x0
  83. mrs_s x0, SYS_VBAR_EL12
  84. msr vbar_el1, x0
  85. // Use EL2 translations for SPE & TRBE and disable access from EL1
  86. mrs x0, mdcr_el2
  87. bic x0, x0, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
  88. bic x0, x0, #(MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT)
  89. msr mdcr_el2, x0
  90. // Transfer the MM state from EL1 to EL2
  91. mrs_s x0, SYS_TCR_EL12
  92. msr tcr_el1, x0
  93. mrs_s x0, SYS_TTBR0_EL12
  94. msr ttbr0_el1, x0
  95. mrs_s x0, SYS_TTBR1_EL12
  96. msr ttbr1_el1, x0
  97. mrs_s x0, SYS_MAIR_EL12
  98. msr mair_el1, x0
  99. isb
  100. // Hack the exception return to stay at EL2
  101. mrs x0, spsr_el1
  102. and x0, x0, #~PSR_MODE_MASK
  103. mov x1, #PSR_MODE_EL2h
  104. orr x0, x0, x1
  105. msr spsr_el1, x0
  106. b enter_vhe
  107. SYM_CODE_END(__finalise_el2)
  108. // At the point where we reach enter_vhe(), we run with
  109. // the MMU off (which is enforced by __finalise_el2()).
  110. // We thus need to be in the idmap, or everything will
  111. // explode when enabling the MMU.
  112. .pushsection .idmap.text, "ax"
  113. SYM_CODE_START_LOCAL(enter_vhe)
  114. // Invalidate TLBs before enabling the MMU
  115. tlbi vmalle1
  116. dsb nsh
  117. isb
  118. // Enable the EL2 S1 MMU, as set up from EL1
  119. mrs_s x0, SYS_SCTLR_EL12
  120. set_sctlr_el1 x0
  121. // Disable the EL1 S1 MMU for a good measure
  122. mov_q x0, INIT_SCTLR_EL1_MMU_OFF
  123. msr_s SYS_SCTLR_EL12, x0
  124. mov x0, xzr
  125. eret
  126. SYM_CODE_END(enter_vhe)
  127. .popsection
  128. .macro invalid_vector label
  129. SYM_CODE_START_LOCAL(\label)
  130. b \label
  131. SYM_CODE_END(\label)
  132. .endm
  133. invalid_vector el2_sync_invalid
  134. invalid_vector el2_irq_invalid
  135. invalid_vector el2_fiq_invalid
  136. invalid_vector el2_error_invalid
  137. invalid_vector el1_sync_invalid
  138. invalid_vector el1_irq_invalid
  139. invalid_vector el1_fiq_invalid
  140. invalid_vector el1_error_invalid
  141. .popsection
  142. /*
  143. * __hyp_set_vectors: Call this after boot to set the initial hypervisor
  144. * vectors as part of hypervisor installation. On an SMP system, this should
  145. * be called on each CPU.
  146. *
  147. * x0 must be the physical address of the new vector table, and must be
  148. * 2KB aligned.
  149. *
  150. * Before calling this, you must check that the stub hypervisor is installed
  151. * everywhere, by waiting for any secondary CPUs to be brought up and then
  152. * checking that is_hyp_mode_available() is true.
  153. *
  154. * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
  155. * something else went wrong... in such cases, trying to install a new
  156. * hypervisor is unlikely to work as desired.
  157. *
  158. * When you call into your shiny new hypervisor, sp_el2 will contain junk,
  159. * so you will need to set that to something sensible at the new hypervisor's
  160. * initialisation entry point.
  161. */
  162. SYM_FUNC_START(__hyp_set_vectors)
  163. mov x1, x0
  164. mov x0, #HVC_SET_VECTORS
  165. hvc #0
  166. ret
  167. SYM_FUNC_END(__hyp_set_vectors)
  168. SYM_FUNC_START(__hyp_reset_vectors)
  169. mov x0, #HVC_RESET_VECTORS
  170. hvc #0
  171. ret
  172. SYM_FUNC_END(__hyp_reset_vectors)
  173. /*
  174. * Entry point to finalise EL2 and switch to VHE if deemed capable
  175. *
  176. * w0: boot mode, as returned by init_kernel_el()
  177. */
  178. SYM_FUNC_START(finalise_el2)
  179. // Need to have booted at EL2
  180. cmp w0, #BOOT_CPU_MODE_EL2
  181. b.ne 1f
  182. // and still be at EL1
  183. mrs x0, CurrentEL
  184. cmp x0, #CurrentEL_EL1
  185. b.ne 1f
  186. mov x0, #HVC_FINALISE_EL2
  187. hvc #0
  188. 1:
  189. ret
  190. SYM_FUNC_END(finalise_el2)