entry_point.S 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. .L__in_el1:
  67. #ifdef RT_USING_LWP
  68. ldr x9, =PV_OFFSET
  69. #else
  70. mov x9, #0
  71. #endif
  72. mov sp, x1 /* in EL1. Set sp to _start */
  73. /* Set CPACR_EL1 (Architecture Feature Access Control Register) to avoid trap from SIMD or float point instruction */
  74. mov x1, #0x00300000 /* Don't trap any SIMD/FP instructions in both EL0 and EL1 */
  75. msr cpacr_el1, x1
  76. /* clear bss */
  77. ldr x1, =__bss_start /* get bss start address */
  78. ldr x2, =__bss_end
  79. sub x2, x2, x1 /* get bss size */
  80. add x1, x1, x9
  81. and x3, x2, #7 /* x3 is < 7 */
  82. ldr x4, =~0x7
  83. and x2, x2, x4 /* mask ~7 */
  84. .L__clean_bss_loop:
  85. cbz x2, .L__clean_bss_loop_1
  86. str xzr, [x1], #8
  87. sub x2, x2, #8
  88. b .L__clean_bss_loop
  89. .L__clean_bss_loop_1:
  90. cbz x3, .L__jump_to_entry
  91. strb wzr, [x1], #1
  92. sub x3, x3, #1
  93. b .L__clean_bss_loop_1
  94. .L__jump_to_entry: /* jump to C code, should not return */
  95. bl mmu_tcr_init
  96. adr x1, __start
  97. ldr x0, =~0x1fffff
  98. and x0, x1, x0
  99. add x1, x0, #0x1000
  100. msr ttbr0_el1, x0
  101. msr ttbr1_el1, x1
  102. dsb sy
  103. ldr x2, =0x40000000 /* map 1G memory for kernel space */
  104. #ifdef RT_USING_LWP
  105. ldr x3, =PV_OFFSET
  106. #endif
  107. bl rt_hw_mmu_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 sy
  117. isb sy
  118. ic ialluis /* Invalidate all instruction caches in Inner Shareable domain to Point of Unification */
  119. dsb sy
  120. isb sy
  121. tlbi vmalle1 /* Invalidate all stage 1 translations used at EL1 with the current VMID */
  122. dsb sy
  123. isb sy
  124. ret
  125. after_mmu_enable:
  126. #ifdef RT_USING_LWP
  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. .globl _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. adr x19, .L__in_el1_cpux
  185. ldr x8, =.L__in_el1_cpux
  186. sub x19, x19, x8 /* get PV_OFFSET */
  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
  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. mrs x0, tcr_el1 /* disable ttbr0, only using kernel space */
  223. orr x0, x0, #(1 << 7)
  224. msr tcr_el1, x0
  225. msr ttbr0_el1, xzr
  226. dsb sy
  227. mov x0, #1
  228. msr spsel, x0
  229. mrs x0, tpidr_el1
  230. /* each cpu init stack is 8k */
  231. adr x1, __start
  232. sub x1, x1, x0, lsl #13
  233. mov sp, x1 /* in EL1. Set sp to _start */
  234. b rt_hw_secondary_cpu_bsp_start
  235. #endif