entry_point.S 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /* clear bss */
  76. GET_PHY x1, __bss_start
  77. GET_PHY x2, __bss_end
  78. sub x2, x2, x1 /* get bss size */
  79. and x3, x2, #7 /* x3 is < 7 */
  80. ldr x4, =~0x7
  81. and x2, x2, x4 /* mask ~7 */
  82. .L__clean_bss_loop:
  83. cbz x2, .L__clean_bss_loop_1
  84. str xzr, [x1], #8
  85. sub x2, x2, #8
  86. b .L__clean_bss_loop
  87. .L__clean_bss_loop_1:
  88. cbz x3, .L__jump_to_entry
  89. strb wzr, [x1], #1
  90. sub x3, x3, #1
  91. b .L__clean_bss_loop_1
  92. .L__jump_to_entry: /* jump to C code, should not return */
  93. bl mmu_tcr_init
  94. adr x1, __start /* install early page table */
  95. ldr x0, =~0x1fffff
  96. and x0, x1, x0
  97. add x1, x0, #0x1000
  98. msr ttbr0_el1, x0
  99. msr ttbr1_el1, x1
  100. dsb sy
  101. #ifdef RT_USING_SMART
  102. ldr x2, =__start
  103. GET_PHY x3, __start
  104. sub x3, x3, x2
  105. #endif
  106. ldr x2, =0x40000000 /* map 1G memory for kernel space */
  107. bl rt_hw_mem_setup_early
  108. ldr x30, =after_mmu_enable /* set LR to after_mmu_enable function, it's a v_addr */
  109. mrs x1, sctlr_el1
  110. bic x1, x1, #(3 << 3) /* dis SA, SA0 */
  111. bic x1, x1, #(1 << 1) /* dis A */
  112. orr x1, x1, #(1 << 12) /* I */
  113. orr x1, x1, #(1 << 2) /* C */
  114. orr x1, x1, #(1 << 0) /* M */
  115. msr sctlr_el1, x1 /* enable MMU */
  116. dsb ish
  117. isb
  118. ic ialluis /* Invalidate all instruction caches in Inner Shareable domain to Point of Unification */
  119. dsb ish
  120. isb
  121. tlbi vmalle1 /* Invalidate all stage 1 translations used at EL1 with the current VMID */
  122. dsb ish
  123. isb
  124. ret
  125. after_mmu_enable:
  126. #ifdef RT_USING_SMART
  127. mrs x0, tcr_el1 /* disable ttbr0, only using kernel space */
  128. orr x0, x0, #(1 << 7)
  129. msr tcr_el1, x0
  130. msr ttbr0_el1, xzr
  131. dsb sy
  132. #endif
  133. mov x0, #1
  134. msr spsel, x0
  135. adr x1, __start
  136. mov sp, x1 /* sp_el1 set to _start */
  137. b rtthread_startup
  138. #ifdef RT_USING_SMP
  139. /**
  140. * secondary cpu
  141. */
  142. .global _secondary_cpu_entry
  143. _secondary_cpu_entry:
  144. bl rt_hw_cpu_id_set
  145. adr x1, __start
  146. /* set up EL1 */
  147. mrs x0, CurrentEL /* CurrentEL Register. bit 2, 3. Others reserved */
  148. and x0, x0, #12 /* clear reserved bits */
  149. /* running at EL3? */
  150. cmp x0, #12 /* 1100b. So, EL3 */
  151. bne .L__not_in_el3_cpux /* 11? !EL3 -> 5: */
  152. /* should never be executed, just for completeness. (EL3) */
  153. mov x2, #0x5b1
  154. msr scr_el3, x2 /* SCR_ELn Secure Configuration Register */
  155. mov x2, #0x3c9
  156. msr spsr_el3, x2 /* SPSR_ELn. Saved Program Status Register. 1111001001 */
  157. adr x2, .L__not_in_el3_cpux
  158. msr elr_el3, x2
  159. eret /* Exception Return: from EL3, continue from .L__not_in_el3 */
  160. .L__not_in_el3_cpux: /* running at EL2 or EL1 */
  161. cmp x0, #4 /* 0x04 0100 EL1 */
  162. beq .L__in_el1_cpux /* EL1 -> 5: */
  163. mrs x0, hcr_el2
  164. bic x0, x0, #0xff
  165. msr hcr_el2, x0
  166. msr sp_el1, x1 /* in EL2, set sp of EL1 to _start */
  167. /* enable CNTP for EL1 */
  168. mrs x0, cnthctl_el2 /* Counter-timer Hypervisor Control register */
  169. orr x0, x0, #3
  170. msr cnthctl_el2, x0
  171. msr cntvoff_el2, xzr
  172. /* enable AArch64 in EL1 */
  173. mov x0, #(1 << 31) /* AArch64 */
  174. orr x0, x0, #(1 << 1) /* SWIO hardwired on Pi3 */
  175. msr hcr_el2, x0
  176. mrs x0, hcr_el2
  177. /* change execution level to EL1 */
  178. mov x2, #0x3c4
  179. msr spsr_el2, x2 /* 1111000100 */
  180. adr x2, .L__in_el1_cpux
  181. msr elr_el2, x2
  182. eret /* exception return. from EL2. continue from .L__in_el1 */
  183. .L__in_el1_cpux:
  184. mrs x0, tpidr_el1
  185. /* each cpu init stack is 8k */
  186. sub x1, x1, x0, lsl #13
  187. mov sp, x1 /* in EL1. Set sp to _start */
  188. /* Set CPACR_EL1 (Architecture Feature Access Control Register) to avoid trap from SIMD or float point instruction */
  189. mov x1, #0x00300000 /* Don't trap any SIMD/FP instructions in both EL0 and EL1 */
  190. msr cpacr_el1, x1
  191. .L__jump_to_entry_cpux: /* jump to C code, should not return */
  192. /* init mmu early */
  193. bl mmu_tcr_init
  194. adr x1, __start /* GET & setup early page table */
  195. ldr x0, =~0x1fffff
  196. and x0, x1, x0
  197. add x1, x0, #0x1000
  198. msr ttbr0_el1, x0
  199. msr ttbr1_el1, x1
  200. dsb sy
  201. ldr x30, =after_mmu_enable_cpux /* set LR to after_mmu_enable function, it's a v_addr */
  202. mrs x1, sctlr_el1
  203. bic x1, x1, #(3 << 3) /* dis SA, SA0 */
  204. bic x1, x1, #(1 << 1) /* dis A */
  205. orr x1, x1, #(1 << 12) /* I */
  206. orr x1, x1, #(1 << 2) /* C */
  207. orr x1, x1, #(1 << 0) /* M */
  208. msr sctlr_el1, x1 /* enable MMU */
  209. dsb sy
  210. isb sy
  211. ic ialluis /* Invalidate all instruction caches in Inner Shareable domain to Point of Unification */
  212. dsb sy
  213. isb sy
  214. tlbi vmalle1 /* Invalidate all stage 1 translations used at EL1 with the current VMID */
  215. dsb sy
  216. isb sy
  217. ret
  218. after_mmu_enable_cpux:
  219. mrs x0, tcr_el1 /* disable ttbr0, only using kernel space */
  220. orr x0, x0, #(1 << 7)
  221. msr tcr_el1, x0
  222. msr ttbr0_el1, xzr
  223. dsb sy
  224. mov x0, #1
  225. msr spsel, x0
  226. mrs x0, tpidr_el1
  227. /* each cpu init stack is 8k */
  228. adr x1, __start
  229. sub x1, x1, x0, lsl #13
  230. mov sp, x1 /* in EL1. Set sp to _start */
  231. b rt_hw_secondary_cpu_bsp_start
  232. #endif