entry_point.S 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Date Author Notes
  7. * 2020-01-15 bigmagic the first version
  8. * 2020-08-10 SummerGift support clang compiler
  9. */
  10. #include "rtconfig.h"
  11. .section ".text.entrypoint","ax"
  12. .global __start
  13. __start:
  14. #ifdef ARCH_ARM_BOOTWITH_FLUSH_CACHE
  15. bl __asm_flush_dcache_all
  16. #endif
  17. bl rt_hw_cpu_id_set
  18. /* read cpu id, stop slave cores */
  19. mrs x0, tpidr_el1
  20. cbz x0, .L__cpu_0 /* .L prefix is the local label in ELF */
  21. /* cpu id > 0, stop */
  22. /* cpu id == 0 will also goto here after returned from entry() if possible */
  23. .L__current_cpu_idle:
  24. wfe
  25. b .L__current_cpu_idle
  26. .L__cpu_0:
  27. /* set stack before our code, Define stack pointer for current exception level */
  28. adr x1, __start
  29. /* set up EL1 */
  30. mrs x0, CurrentEL /* CurrentEL Register. bit 2, 3. Others reserved */
  31. and x0, x0, #12 /* clear reserved bits */
  32. /* running at EL3? */
  33. cmp x0, #12 /* 1100b. So, EL3 */
  34. bne .L__not_in_el3 /* 11? !EL3 -> 5: */
  35. /* should never be executed, just for completeness. (EL3) */
  36. mov x2, #0x5b1
  37. msr scr_el3, x2 /* SCR_ELn Secure Configuration Register */
  38. mov x2, #0x3c9
  39. msr spsr_el3, x2 /* SPSR_ELn. Saved Program Status Register. 1111001001 */
  40. adr x2, .L__not_in_el3
  41. msr elr_el3, x2
  42. eret /* Exception Return: from EL3, continue from .L__not_in_el3 */
  43. .L__not_in_el3: /* running at EL2 or EL1 */
  44. cmp x0, #4 /* 0x04 0100 EL1 */
  45. beq .L__in_el1 /* EL1 -> 5: */
  46. mrs x0, hcr_el2
  47. bic x0, x0, #0xff
  48. msr hcr_el2, x0
  49. msr sp_el1, x1 /* in EL2, set sp of EL1 to _start */
  50. /* enable CNTP for EL1 */
  51. mrs x0, cnthctl_el2 /* Counter-timer Hypervisor Control register */
  52. orr x0, x0, #3
  53. msr cnthctl_el2, x0
  54. msr cntvoff_el2, xzr
  55. /* enable AArch64 in EL1 */
  56. mov x0, #(1 << 31) /* AArch64 */
  57. orr x0, x0, #(1 << 1) /* SWIO hardwired on Pi3 */
  58. msr hcr_el2, x0
  59. mrs x0, hcr_el2
  60. /* change execution level to EL1 */
  61. mov x2, #0x3c4
  62. msr spsr_el2, x2 /* 1111000100 */
  63. adr x2, .L__in_el1
  64. msr elr_el2, x2
  65. eret /* exception return. from EL2. continue from .L__in_el1 */
  66. .macro GET_PHY reg, symbol
  67. adrp \reg, \symbol
  68. add \reg, \reg, #:lo12:\symbol
  69. .endm
  70. .L__in_el1:
  71. mov sp, x1 /* in EL1. Set sp to _start */
  72. /* Set CPACR_EL1 (Architecture Feature Access Control Register) to avoid trap from SIMD or float point instruction */
  73. mov x1, #0x00300000 /* Don't trap any SIMD/FP instructions in both EL0 and EL1 */
  74. msr cpacr_el1, x1
  75. /* applying context change */
  76. dsb ish
  77. isb
  78. /* clear bss */
  79. GET_PHY x1, __bss_start
  80. GET_PHY x2, __bss_end
  81. sub x2, x2, x1 /* get bss size */
  82. and x3, x2, #7 /* x3 is < 7 */
  83. ldr x4, =~0x7
  84. and x2, x2, x4 /* mask ~7 */
  85. .L__clean_bss_loop:
  86. cbz x2, .L__clean_bss_loop_1
  87. str xzr, [x1], #8
  88. sub x2, x2, #8
  89. b .L__clean_bss_loop
  90. .L__clean_bss_loop_1:
  91. cbz x3, .L__jump_to_entry
  92. strb wzr, [x1], #1
  93. sub x3, x3, #1
  94. b .L__clean_bss_loop_1
  95. .L__jump_to_entry: /* jump to C code, should not return */
  96. bl mmu_tcr_init
  97. adr x1, __start /* install early page table */
  98. ldr x0, =~0x1fffff
  99. and x0, x1, x0
  100. add x1, x0, #0x1000
  101. msr ttbr0_el1, x0
  102. msr ttbr1_el1, x1
  103. dsb sy
  104. #ifdef RT_USING_SMART
  105. ldr x2, =__start
  106. GET_PHY x3, __start
  107. sub x3, x3, x2
  108. #endif
  109. ldr x2, =0x40000000 /* map 1G memory for kernel space */
  110. bl rt_hw_mem_setup_early
  111. ldr x30, =after_mmu_enable /* set LR to after_mmu_enable function, it's a v_addr */
  112. mrs x1, sctlr_el1
  113. bic x1, x1, #(3 << 3) /* dis SA, SA0 */
  114. bic x1, x1, #(1 << 1) /* dis A */
  115. orr x1, x1, #(1 << 12) /* I */
  116. orr x1, x1, #(1 << 2) /* C */
  117. orr x1, x1, #(1 << 0) /* M */
  118. msr sctlr_el1, x1 /* enable MMU */
  119. dsb ish
  120. isb
  121. ic ialluis /* Invalidate all instruction caches in Inner Shareable domain to Point of Unification */
  122. dsb ish
  123. isb
  124. tlbi vmalle1 /* Invalidate all stage 1 translations used at EL1 with the current VMID */
  125. dsb ish
  126. isb
  127. ret
  128. after_mmu_enable:
  129. #ifdef RT_USING_SMART
  130. mrs x0, tcr_el1 /* disable ttbr0, only using kernel space */
  131. orr x0, x0, #(1 << 7)
  132. msr tcr_el1, x0
  133. msr ttbr0_el1, xzr
  134. dsb sy
  135. #endif
  136. mov x0, #1
  137. msr spsel, x0
  138. adr x1, __start
  139. mov sp, x1 /* sp_el1 set to _start */
  140. b rtthread_startup
  141. #ifdef RT_USING_SMP
  142. /**
  143. * secondary cpu
  144. */
  145. .global _secondary_cpu_entry
  146. _secondary_cpu_entry:
  147. bl rt_hw_cpu_id_set
  148. adr x1, __start
  149. /* set up EL1 */
  150. mrs x0, CurrentEL /* CurrentEL Register. bit 2, 3. Others reserved */
  151. and x0, x0, #12 /* clear reserved bits */
  152. /* running at EL3? */
  153. cmp x0, #12 /* 1100b. So, EL3 */
  154. bne .L__not_in_el3_cpux /* 11? !EL3 -> 5: */
  155. /* should never be executed, just for completeness. (EL3) */
  156. mov x2, #0x5b1
  157. msr scr_el3, x2 /* SCR_ELn Secure Configuration Register */
  158. mov x2, #0x3c9
  159. msr spsr_el3, x2 /* SPSR_ELn. Saved Program Status Register. 1111001001 */
  160. adr x2, .L__not_in_el3_cpux
  161. msr elr_el3, x2
  162. eret /* Exception Return: from EL3, continue from .L__not_in_el3 */
  163. .L__not_in_el3_cpux: /* running at EL2 or EL1 */
  164. cmp x0, #4 /* 0x04 0100 EL1 */
  165. beq .L__in_el1_cpux /* EL1 -> 5: */
  166. mrs x0, hcr_el2
  167. bic x0, x0, #0xff
  168. msr hcr_el2, x0
  169. msr sp_el1, x1 /* in EL2, set sp of EL1 to _start */
  170. /* enable CNTP for EL1 */
  171. mrs x0, cnthctl_el2 /* Counter-timer Hypervisor Control register */
  172. orr x0, x0, #3
  173. msr cnthctl_el2, x0
  174. msr cntvoff_el2, xzr
  175. /* enable AArch64 in EL1 */
  176. mov x0, #(1 << 31) /* AArch64 */
  177. orr x0, x0, #(1 << 1) /* SWIO hardwired on Pi3 */
  178. msr hcr_el2, x0
  179. mrs x0, hcr_el2
  180. /* change execution level to EL1 */
  181. mov x2, #0x3c4
  182. msr spsr_el2, x2 /* 1111000100 */
  183. adr x2, .L__in_el1_cpux
  184. msr elr_el2, x2
  185. eret /* exception return. from EL2. continue from .L__in_el1 */
  186. .L__in_el1_cpux:
  187. mrs x0, tpidr_el1
  188. /* each cpu init stack is 8k */
  189. sub x1, x1, x0, lsl #13
  190. mov sp, x1 /* in EL1. Set sp to _start */
  191. /* Set CPACR_EL1 (Architecture Feature Access Control Register) to avoid trap from SIMD or float point instruction */
  192. mov x1, #0x00300000 /* Don't trap any SIMD/FP instructions in both EL0 and EL1 */
  193. msr cpacr_el1, x1
  194. .L__jump_to_entry_cpux: /* jump to C code, should not return */
  195. /* init mmu early */
  196. bl mmu_tcr_init
  197. adr x1, __start /* GET & setup early page table */
  198. ldr x0, =~0x1fffff
  199. and x0, x1, x0
  200. add x1, x0, #0x1000
  201. msr ttbr0_el1, x0
  202. msr ttbr1_el1, x1
  203. dsb sy
  204. ldr x30, =after_mmu_enable_cpux /* set LR to after_mmu_enable function, it's a v_addr */
  205. mrs x1, sctlr_el1
  206. bic x1, x1, #(3 << 3) /* dis SA, SA0 */
  207. bic x1, x1, #(1 << 1) /* dis A */
  208. orr x1, x1, #(1 << 12) /* I */
  209. orr x1, x1, #(1 << 2) /* C */
  210. orr x1, x1, #(1 << 0) /* M */
  211. msr sctlr_el1, x1 /* enable MMU */
  212. dsb sy
  213. isb sy
  214. ic ialluis /* Invalidate all instruction caches in Inner Shareable domain to Point of Unification */
  215. dsb sy
  216. isb sy
  217. tlbi vmalle1 /* Invalidate all stage 1 translations used at EL1 with the current VMID */
  218. dsb sy
  219. isb sy
  220. ret
  221. after_mmu_enable_cpux:
  222. #ifdef RT_USING_SMART
  223. mrs x0, tcr_el1 /* disable ttbr0, only using kernel space */
  224. orr x0, x0, #(1 << 7)
  225. msr tcr_el1, x0
  226. msr ttbr0_el1, xzr
  227. dsb sy
  228. #endif
  229. mov x0, #1
  230. msr spsel, x0
  231. mrs x0, tpidr_el1
  232. /* each cpu init stack is 8k */
  233. adr x1, __start
  234. sub x1, x1, x0, lsl #13
  235. mov sp, x1 /* in EL1. Set sp to _start */
  236. b rt_hw_secondary_cpu_bsp_start
  237. #endif