hyp-stub.S 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. ENTRY(__boot_cpu_mode)
  33. .long 0
  34. .text
  35. /*
  36. * Save the primary CPU boot mode. Requires 3 scratch registers.
  37. */
  38. .macro store_primary_cpu_mode reg1, reg2, reg3
  39. mrs \reg1, cpsr
  40. and \reg1, \reg1, #MODE_MASK
  41. adr \reg2, .L__boot_cpu_mode_offset
  42. ldr \reg3, [\reg2]
  43. str \reg1, [\reg2, \reg3]
  44. .endm
  45. /*
  46. * Compare the current mode with the one saved on the primary CPU.
  47. * If they don't match, record that fact. The Z bit indicates
  48. * if there's a match or not.
  49. * Requires 3 additionnal scratch registers.
  50. */
  51. .macro compare_cpu_mode_with_primary mode, reg1, reg2, reg3
  52. adr \reg2, .L__boot_cpu_mode_offset
  53. ldr \reg3, [\reg2]
  54. ldr \reg1, [\reg2, \reg3]
  55. cmp \mode, \reg1 @ matches primary CPU boot mode?
  56. orrne \reg1, \reg1, #BOOT_CPU_MODE_MISMATCH
  57. strne \reg1, [\reg2, \reg3] @ record what happened and give up
  58. .endm
  59. #else /* ZIMAGE */
  60. .macro store_primary_cpu_mode reg1:req, reg2:req, reg3:req
  61. .endm
  62. /*
  63. * The zImage loader only runs on one CPU, so we don't bother with mult-CPU
  64. * consistency checking:
  65. */
  66. .macro compare_cpu_mode_with_primary mode, reg1, reg2, reg3
  67. cmp \mode, \mode
  68. .endm
  69. #endif /* ZIMAGE */
  70. /*
  71. * Hypervisor stub installation functions.
  72. *
  73. * These must be called with the MMU and D-cache off.
  74. * They are not ABI compliant and are only intended to be called from the kernel
  75. * entry points in head.S.
  76. */
  77. @ Call this from the primary CPU
  78. ENTRY(__hyp_stub_install)
  79. store_primary_cpu_mode r4, r5, r6
  80. ENDPROC(__hyp_stub_install)
  81. @ fall through...
  82. @ Secondary CPUs should call here
  83. ENTRY(__hyp_stub_install_secondary)
  84. mrs r4, cpsr
  85. and r4, r4, #MODE_MASK
  86. /*
  87. * If the secondary has booted with a different mode, give up
  88. * immediately.
  89. */
  90. compare_cpu_mode_with_primary r4, r5, r6, r7
  91. retne lr
  92. /*
  93. * Once we have given up on one CPU, we do not try to install the
  94. * stub hypervisor on the remaining ones: because the saved boot mode
  95. * is modified, it can't compare equal to the CPSR mode field any
  96. * more.
  97. *
  98. * Otherwise...
  99. */
  100. cmp r4, #HYP_MODE
  101. retne lr @ give up if the CPU is not in HYP mode
  102. /*
  103. * Configure HSCTLR to set correct exception endianness/instruction set
  104. * state etc.
  105. * Turn off all traps
  106. * Eventually, CPU-specific code might be needed -- assume not for now
  107. *
  108. * This code relies on the "eret" instruction to synchronize the
  109. * various coprocessor accesses. This is done when we switch to SVC
  110. * (see safe_svcmode_maskall).
  111. */
  112. @ Now install the hypervisor stub:
  113. adr r7, __hyp_stub_vectors
  114. mcr p15, 4, r7, c12, c0, 0 @ set hypervisor vector base (HVBAR)
  115. @ Disable all traps, so we don't get any nasty surprise
  116. mov r7, #0
  117. mcr p15, 4, r7, c1, c1, 0 @ HCR
  118. mcr p15, 4, r7, c1, c1, 2 @ HCPTR
  119. mcr p15, 4, r7, c1, c1, 3 @ HSTR
  120. THUMB( orr r7, #(1 << 30) ) @ HSCTLR.TE
  121. ARM_BE8(orr r7, r7, #(1 << 25)) @ HSCTLR.EE
  122. mcr p15, 4, r7, c1, c0, 0 @ HSCTLR
  123. mrc p15, 4, r7, c1, c1, 1 @ HDCR
  124. and r7, #0x1f @ Preserve HPMN
  125. mcr p15, 4, r7, c1, c1, 1 @ HDCR
  126. @ Make sure NS-SVC is initialised appropriately
  127. mrc p15, 0, r7, c1, c0, 0 @ SCTLR
  128. orr r7, #(1 << 5) @ CP15 barriers enabled
  129. bic r7, #(3 << 7) @ Clear SED/ITD for v8 (RES0 for v7)
  130. bic r7, #(3 << 19) @ WXN and UWXN disabled
  131. mcr p15, 0, r7, c1, c0, 0 @ SCTLR
  132. mrc p15, 0, r7, c0, c0, 0 @ MIDR
  133. mcr p15, 4, r7, c0, c0, 0 @ VPIDR
  134. mrc p15, 0, r7, c0, c0, 5 @ MPIDR
  135. mcr p15, 4, r7, c0, c0, 5 @ VMPIDR
  136. #if !defined(ZIMAGE) && defined(CONFIG_ARM_ARCH_TIMER)
  137. @ make CNTP_* and CNTPCT accessible from PL1
  138. mrc p15, 0, r7, c0, c1, 1 @ ID_PFR1
  139. lsr r7, #16
  140. and r7, #0xf
  141. cmp r7, #1
  142. bne 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. cmp r7, #1
  159. bne 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. cmp r0, #-1
  178. mrceq p15, 4, r0, c12, c0, 0 @ get HVBAR
  179. mcrne p15, 4, r0, c12, c0, 0 @ set HVBAR
  180. __ERET
  181. ENDPROC(__hyp_stub_do_trap)
  182. /*
  183. * __hyp_set_vectors: Call this after boot to set the initial hypervisor
  184. * vectors as part of hypervisor installation. On an SMP system, this should
  185. * be called on each CPU.
  186. *
  187. * r0 must be the physical address of the new vector table (which must lie in
  188. * the bottom 4GB of physical address space.
  189. *
  190. * r0 must be 32-byte aligned.
  191. *
  192. * Before calling this, you must check that the stub hypervisor is installed
  193. * everywhere, by waiting for any secondary CPUs to be brought up and then
  194. * checking that BOOT_CPU_MODE_HAVE_HYP(__boot_cpu_mode) is true.
  195. *
  196. * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
  197. * something else went wrong... in such cases, trying to install a new
  198. * hypervisor is unlikely to work as desired.
  199. *
  200. * When you call into your shiny new hypervisor, sp_hyp will contain junk,
  201. * so you will need to set that to something sensible at the new hypervisor's
  202. * initialisation entry point.
  203. */
  204. ENTRY(__hyp_get_vectors)
  205. mov r0, #-1
  206. ENDPROC(__hyp_get_vectors)
  207. @ fall through
  208. ENTRY(__hyp_set_vectors)
  209. __HVC(0)
  210. ret lr
  211. ENDPROC(__hyp_set_vectors)
  212. #ifndef ZIMAGE
  213. .align 2
  214. .L__boot_cpu_mode_offset:
  215. .long __boot_cpu_mode - .
  216. #endif
  217. .align 5
  218. __hyp_stub_vectors:
  219. __hyp_stub_reset: W(b) .
  220. __hyp_stub_und: W(b) .
  221. __hyp_stub_svc: W(b) .
  222. __hyp_stub_pabort: W(b) .
  223. __hyp_stub_dabort: W(b) .
  224. __hyp_stub_trap: W(b) __hyp_stub_do_trap
  225. __hyp_stub_irq: W(b) .
  226. __hyp_stub_fiq: W(b) .
  227. ENDPROC(__hyp_stub_vectors)