hyp-stub.S 7.5 KB

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