hyp-stub.S 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Hypervisor stub
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. * Author: Marc Zyngier <marc.zyngier@arm.com>
  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 0f 1f x1 x2
  68. 0: // Check whether we only want the hypervisor to run VHE, not the kernel
  69. adr_l x1, arm64_sw_feature_override
  70. ldr x2, [x1, FTR_OVR_VAL_OFFSET]
  71. ldr x1, [x1, FTR_OVR_MASK_OFFSET]
  72. and x2, x2, x1
  73. ubfx x2, x2, #ARM64_SW_FEATURE_OVERRIDE_HVHE, #4
  74. cbz x2, 2f
  75. 1: mov_q x0, HVC_STUB_ERR
  76. eret
  77. 2:
  78. // Engage the VHE magic!
  79. mov_q x0, HCR_HOST_VHE_FLAGS
  80. msr hcr_el2, x0
  81. isb
  82. // Use the EL1 allocated stack, per-cpu offset
  83. mrs x0, sp_el1
  84. mov sp, x0
  85. mrs x0, tpidr_el1
  86. msr tpidr_el2, x0
  87. // FP configuration, vectors
  88. mrs_s x0, SYS_CPACR_EL12
  89. msr cpacr_el1, x0
  90. mrs_s x0, SYS_VBAR_EL12
  91. msr vbar_el1, x0
  92. // Use EL2 translations for SPE & TRBE and disable access from EL1
  93. mrs x0, mdcr_el2
  94. bic x0, x0, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
  95. bic x0, x0, #(MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT)
  96. msr mdcr_el2, x0
  97. // Transfer the MM state from EL1 to EL2
  98. mrs_s x0, SYS_TCR_EL12
  99. msr tcr_el1, x0
  100. mrs_s x0, SYS_TTBR0_EL12
  101. msr ttbr0_el1, x0
  102. mrs_s x0, SYS_TTBR1_EL12
  103. msr ttbr1_el1, x0
  104. mrs_s x0, SYS_MAIR_EL12
  105. msr mair_el1, x0
  106. mrs x1, REG_ID_AA64MMFR3_EL1
  107. ubfx x1, x1, #ID_AA64MMFR3_EL1_TCRX_SHIFT, #4
  108. cbz x1, .Lskip_tcr2
  109. mrs x0, REG_TCR2_EL12
  110. msr REG_TCR2_EL1, x0
  111. // Transfer permission indirection state
  112. mrs x1, REG_ID_AA64MMFR3_EL1
  113. ubfx x1, x1, #ID_AA64MMFR3_EL1_S1PIE_SHIFT, #4
  114. cbz x1, .Lskip_indirection
  115. mrs x0, REG_PIRE0_EL12
  116. msr REG_PIRE0_EL1, x0
  117. mrs x0, REG_PIR_EL12
  118. msr REG_PIR_EL1, x0
  119. .Lskip_indirection:
  120. .Lskip_tcr2:
  121. isb
  122. // Hack the exception return to stay at EL2
  123. mrs x0, spsr_el1
  124. and x0, x0, #~PSR_MODE_MASK
  125. mov x1, #PSR_MODE_EL2h
  126. orr x0, x0, x1
  127. msr spsr_el1, x0
  128. b enter_vhe
  129. SYM_CODE_END(__finalise_el2)
  130. // At the point where we reach enter_vhe(), we run with
  131. // the MMU off (which is enforced by __finalise_el2()).
  132. // We thus need to be in the idmap, or everything will
  133. // explode when enabling the MMU.
  134. .pushsection .idmap.text, "ax"
  135. SYM_CODE_START_LOCAL(enter_vhe)
  136. // Invalidate TLBs before enabling the MMU
  137. tlbi vmalle1
  138. dsb nsh
  139. isb
  140. // Enable the EL2 S1 MMU, as set up from EL1
  141. mrs_s x0, SYS_SCTLR_EL12
  142. set_sctlr_el1 x0
  143. // Disable the EL1 S1 MMU for a good measure
  144. mov_q x0, INIT_SCTLR_EL1_MMU_OFF
  145. msr_s SYS_SCTLR_EL12, x0
  146. mov x0, xzr
  147. eret
  148. SYM_CODE_END(enter_vhe)
  149. .popsection
  150. .macro invalid_vector label
  151. SYM_CODE_START_LOCAL(\label)
  152. b \label
  153. SYM_CODE_END(\label)
  154. .endm
  155. invalid_vector el2_sync_invalid
  156. invalid_vector el2_irq_invalid
  157. invalid_vector el2_fiq_invalid
  158. invalid_vector el2_error_invalid
  159. invalid_vector el1_sync_invalid
  160. invalid_vector el1_irq_invalid
  161. invalid_vector el1_fiq_invalid
  162. invalid_vector el1_error_invalid
  163. .popsection
  164. /*
  165. * __hyp_set_vectors: Call this after boot to set the initial hypervisor
  166. * vectors as part of hypervisor installation. On an SMP system, this should
  167. * be called on each CPU.
  168. *
  169. * x0 must be the physical address of the new vector table, and must be
  170. * 2KB aligned.
  171. *
  172. * Before calling this, you must check that the stub hypervisor is installed
  173. * everywhere, by waiting for any secondary CPUs to be brought up and then
  174. * checking that is_hyp_mode_available() is true.
  175. *
  176. * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
  177. * something else went wrong... in such cases, trying to install a new
  178. * hypervisor is unlikely to work as desired.
  179. *
  180. * When you call into your shiny new hypervisor, sp_el2 will contain junk,
  181. * so you will need to set that to something sensible at the new hypervisor's
  182. * initialisation entry point.
  183. */
  184. SYM_FUNC_START(__hyp_set_vectors)
  185. mov x1, x0
  186. mov x0, #HVC_SET_VECTORS
  187. hvc #0
  188. ret
  189. SYM_FUNC_END(__hyp_set_vectors)
  190. SYM_FUNC_START(__hyp_reset_vectors)
  191. mov x0, #HVC_RESET_VECTORS
  192. hvc #0
  193. ret
  194. SYM_FUNC_END(__hyp_reset_vectors)
  195. /*
  196. * Entry point to finalise EL2 and switch to VHE if deemed capable
  197. *
  198. * w0: boot mode, as returned by init_kernel_el()
  199. */
  200. SYM_FUNC_START(finalise_el2)
  201. // Need to have booted at EL2
  202. cmp w0, #BOOT_CPU_MODE_EL2
  203. b.ne 1f
  204. // and still be at EL1
  205. mrs x0, CurrentEL
  206. cmp x0, #CurrentEL_EL1
  207. b.ne 1f
  208. mov x0, #HVC_FINALISE_EL2
  209. hvc #0
  210. 1:
  211. ret
  212. SYM_FUNC_END(finalise_el2)