hyp-stub.S 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2012 Linaro Limited.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  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 along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/irqchip/arm-gic-v3.h>
  20. #include <linux/linkage.h>
  21. #include <asm/assembler.h>
  22. #include <asm/virt.h>
  23. #define HVC_GET_VECTORS -1
  24. #define HVC_SOFT_RESTART 1
  25. #ifndef ZIMAGE
  26. /*
  27. * For the kernel proper, we need to find out the CPU boot mode long after
  28. * boot, so we need to store it in a writable variable.
  29. *
  30. * This is not in .bss, because we set it sufficiently early that the boot-time
  31. * zeroing of .bss would clobber it.
  32. */
  33. .data
  34. ENTRY(__boot_cpu_mode)
  35. .long 0
  36. .text
  37. /*
  38. * Save the primary CPU boot mode. Requires 3 scratch registers.
  39. */
  40. .macro store_primary_cpu_mode reg1, reg2, reg3
  41. mrs \reg1, cpsr
  42. and \reg1, \reg1, #MODE_MASK
  43. adr \reg2, .L__boot_cpu_mode_offset
  44. ldr \reg3, [\reg2]
  45. str \reg1, [\reg2, \reg3]
  46. .endm
  47. /*
  48. * Compare the current mode with the one saved on the primary CPU.
  49. * If they don't match, record that fact. The Z bit indicates
  50. * if there's a match or not.
  51. * Requires 3 additionnal scratch registers.
  52. */
  53. .macro compare_cpu_mode_with_primary mode, reg1, reg2, reg3
  54. adr \reg2, .L__boot_cpu_mode_offset
  55. ldr \reg3, [\reg2]
  56. ldr \reg1, [\reg2, \reg3]
  57. cmp \mode, \reg1 @ matches primary CPU boot mode?
  58. orrne \reg1, \reg1, #BOOT_CPU_MODE_MISMATCH
  59. strne \reg1, [\reg2, \reg3] @ record what happened and give up
  60. .endm
  61. #else /* ZIMAGE */
  62. .macro store_primary_cpu_mode reg1:req, reg2:req, reg3:req
  63. .endm
  64. /*
  65. * The zImage loader only runs on one CPU, so we don't bother with mult-CPU
  66. * consistency checking:
  67. */
  68. .macro compare_cpu_mode_with_primary mode, reg1, reg2, reg3
  69. cmp \mode, \mode
  70. .endm
  71. #endif /* ZIMAGE */
  72. /*
  73. * Hypervisor stub installation functions.
  74. *
  75. * These must be called with the MMU and D-cache off.
  76. * They are not ABI compliant and are only intended to be called from the kernel
  77. * entry points in head.S.
  78. */
  79. @ Call this from the primary CPU
  80. ENTRY(__hyp_stub_install)
  81. store_primary_cpu_mode r4, r5, r6
  82. ENDPROC(__hyp_stub_install)
  83. @ fall through...
  84. @ Secondary CPUs should call here
  85. ENTRY(__hyp_stub_install_secondary)
  86. mrs r4, cpsr
  87. and r4, r4, #MODE_MASK
  88. /*
  89. * If the secondary has booted with a different mode, give up
  90. * immediately.
  91. */
  92. compare_cpu_mode_with_primary r4, r5, r6, r7
  93. retne lr
  94. /*
  95. * Once we have given up on one CPU, we do not try to install the
  96. * stub hypervisor on the remaining ones: because the saved boot mode
  97. * is modified, it can't compare equal to the CPSR mode field any
  98. * more.
  99. *
  100. * Otherwise...
  101. */
  102. cmp r4, #HYP_MODE
  103. retne lr @ give up if the CPU is not in HYP mode
  104. /*
  105. * Configure HSCTLR to set correct exception endianness/instruction set
  106. * state etc.
  107. * Turn off all traps
  108. * Eventually, CPU-specific code might be needed -- assume not for now
  109. *
  110. * This code relies on the "eret" instruction to synchronize the
  111. * various coprocessor accesses. This is done when we switch to SVC
  112. * (see safe_svcmode_maskall).
  113. */
  114. @ Now install the hypervisor stub:
  115. adr r7, __hyp_stub_vectors
  116. mcr p15, 4, r7, c12, c0, 0 @ set hypervisor vector base (HVBAR)
  117. @ Disable all traps, so we don't get any nasty surprise
  118. mov r7, #0
  119. mcr p15, 4, r7, c1, c1, 0 @ HCR
  120. mcr p15, 4, r7, c1, c1, 2 @ HCPTR
  121. mcr p15, 4, r7, c1, c1, 3 @ HSTR
  122. THUMB( orr r7, #(1 << 30) ) @ HSCTLR.TE
  123. ARM_BE8(orr r7, r7, #(1 << 25)) @ HSCTLR.EE
  124. mcr p15, 4, r7, c1, c0, 0 @ HSCTLR
  125. mrc p15, 4, r7, c1, c1, 1 @ HDCR
  126. and r7, #0x1f @ Preserve HPMN
  127. mcr p15, 4, r7, c1, c1, 1 @ HDCR
  128. @ Make sure NS-SVC is initialised appropriately
  129. mrc p15, 0, r7, c1, c0, 0 @ SCTLR
  130. orr r7, #(1 << 5) @ CP15 barriers enabled
  131. bic r7, #(3 << 7) @ Clear SED/ITD for v8 (RES0 for v7)
  132. bic r7, #(3 << 19) @ WXN and UWXN disabled
  133. mcr p15, 0, r7, c1, c0, 0 @ SCTLR
  134. mrc p15, 0, r7, c0, c0, 0 @ MIDR
  135. mcr p15, 4, r7, c0, c0, 0 @ VPIDR
  136. mrc p15, 0, r7, c0, c0, 5 @ MPIDR
  137. mcr p15, 4, r7, c0, c0, 5 @ VMPIDR
  138. #if !defined(ZIMAGE) && defined(CONFIG_ARM_ARCH_TIMER)
  139. @ make CNTP_* and CNTPCT accessible from PL1
  140. mrc p15, 0, r7, c0, c1, 1 @ ID_PFR1
  141. lsr r7, #16
  142. and r7, #0xf
  143. cmp r7, #1
  144. bne 1f
  145. mrc p15, 4, r7, c14, c1, 0 @ CNTHCTL
  146. orr r7, r7, #3 @ PL1PCEN | PL1PCTEN
  147. mcr p15, 4, r7, c14, c1, 0 @ CNTHCTL
  148. mov r7, #0
  149. mcrr p15, 4, r7, r7, c14 @ CNTVOFF
  150. @ Disable virtual timer in case it was counting
  151. mrc p15, 0, r7, c14, c3, 1 @ CNTV_CTL
  152. bic r7, #1 @ Clear ENABLE
  153. mcr p15, 0, r7, c14, c3, 1 @ CNTV_CTL
  154. 1:
  155. #endif
  156. #ifdef CONFIG_ARM_GIC_V3
  157. @ Check whether GICv3 system registers are available
  158. mrc p15, 0, r7, c0, c1, 1 @ ID_PFR1
  159. ubfx r7, r7, #28, #4
  160. cmp r7, #1
  161. bne 2f
  162. @ Enable system register accesses
  163. mrc p15, 4, r7, c12, c9, 5 @ ICC_HSRE
  164. orr r7, r7, #(ICC_SRE_EL2_ENABLE | ICC_SRE_EL2_SRE)
  165. mcr p15, 4, r7, c12, c9, 5 @ ICC_HSRE
  166. isb
  167. @ SRE bit could be forced to 0 by firmware.
  168. @ Check whether it sticks before accessing any other sysreg
  169. mrc p15, 4, r7, c12, c9, 5 @ ICC_HSRE
  170. tst r7, #ICC_SRE_EL2_SRE
  171. beq 2f
  172. mov r7, #0
  173. mcr p15, 4, r7, c12, c11, 0 @ ICH_HCR
  174. 2:
  175. #endif
  176. bx lr @ The boot CPU mode is left in r4.
  177. ENDPROC(__hyp_stub_install_secondary)
  178. __hyp_stub_do_trap:
  179. cmp r0, #HVC_GET_VECTORS
  180. bne 1f
  181. mrc p15, 4, r0, c12, c0, 0 @ get HVBAR
  182. b __hyp_stub_exit
  183. 1: teq r0, #HVC_SOFT_RESTART
  184. bne 1f
  185. mov r0, r3
  186. bx r0
  187. 1: mcrne p15, 4, r0, c12, c0, 0 @ set HVBAR
  188. __hyp_stub_exit:
  189. __ERET
  190. ENDPROC(__hyp_stub_do_trap)
  191. /*
  192. * __hyp_set_vectors: Call this after boot to set the initial hypervisor
  193. * vectors as part of hypervisor installation. On an SMP system, this should
  194. * be called on each CPU.
  195. *
  196. * r0 must be the physical address of the new vector table (which must lie in
  197. * the bottom 4GB of physical address space.
  198. *
  199. * r0 must be 32-byte aligned.
  200. *
  201. * Before calling this, you must check that the stub hypervisor is installed
  202. * everywhere, by waiting for any secondary CPUs to be brought up and then
  203. * checking that BOOT_CPU_MODE_HAVE_HYP(__boot_cpu_mode) is true.
  204. *
  205. * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
  206. * something else went wrong... in such cases, trying to install a new
  207. * hypervisor is unlikely to work as desired.
  208. *
  209. * When you call into your shiny new hypervisor, sp_hyp will contain junk,
  210. * so you will need to set that to something sensible at the new hypervisor's
  211. * initialisation entry point.
  212. */
  213. ENTRY(__hyp_get_vectors)
  214. mov r0, #HVC_GET_VECTORS
  215. __HVC(0)
  216. ret lr
  217. ENDPROC(__hyp_get_vectors)
  218. ENTRY(__hyp_set_vectors)
  219. tst r0, #31
  220. bne 1f
  221. __HVC(0)
  222. 1: ret lr
  223. ENDPROC(__hyp_set_vectors)
  224. ENTRY(__hyp_soft_restart)
  225. mov r3, r0
  226. mov r0, #HVC_SOFT_RESTART
  227. __HVC(0)
  228. mov r0, r3
  229. ret lr
  230. ENDPROC(__hyp_soft_restart)
  231. #ifndef ZIMAGE
  232. .align 2
  233. .L__boot_cpu_mode_offset:
  234. .long __boot_cpu_mode - .
  235. #endif
  236. .align 5
  237. __hyp_stub_vectors:
  238. __hyp_stub_reset: W(b) .
  239. __hyp_stub_und: W(b) .
  240. __hyp_stub_svc: W(b) .
  241. __hyp_stub_pabort: W(b) .
  242. __hyp_stub_dabort: W(b) .
  243. __hyp_stub_trap: W(b) __hyp_stub_do_trap
  244. __hyp_stub_irq: W(b) .
  245. __hyp_stub_fiq: W(b) .
  246. ENDPROC(__hyp_stub_vectors)