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. bl rt_hw_cpu_id_set
  15. /* read cpu id, stop slave cores */
  16. mrs x0, tpidr_el1
  17. cbz x0, .L__cpu_0 /* .L prefix is the local label in ELF */
  18. /* cpu id > 0, stop */
  19. /* cpu id == 0 will also goto here after returned from entry() if possible */
  20. .L__current_cpu_idle:
  21. wfe
  22. b .L__current_cpu_idle
  23. .L__cpu_0:
  24. /* set stack before our code, Define stack pointer for current exception level */
  25. adr x1, __start
  26. /* set up EL1 */
  27. mrs x0, CurrentEL /* CurrentEL Register. bit 2, 3. Others reserved */
  28. and x0, x0, #12 /* clear reserved bits */
  29. /* running at EL3? */
  30. cmp x0, #12 /* 1100b. So, EL3 */
  31. bne .L__not_in_el3 /* 11? !EL3 -> 5: */
  32. /* should never be executed, just for completeness. (EL3) */
  33. mov x2, #0x5b1
  34. msr scr_el3, x2 /* SCR_ELn Secure Configuration Register */
  35. mov x2, #0x3c9
  36. msr spsr_el3, x2 /* SPSR_ELn. Saved Program Status Register. 1111001001 */
  37. adr x2, .L__not_in_el3
  38. msr elr_el3, x2
  39. eret /* Exception Return: from EL3, continue from .L__not_in_el3 */
  40. .L__not_in_el3: /* running at EL2 or EL1 */
  41. cmp x0, #4 /* 0x04 0100 EL1 */
  42. beq .L__in_el1 /* EL1 -> 5: */
  43. mrs x0, hcr_el2
  44. bic x0, x0, #0xff
  45. msr hcr_el2, x0
  46. msr sp_el1, x1 /* in EL2, set sp of EL1 to _start */
  47. /* enable CNTP for EL1 */
  48. mrs x0, cnthctl_el2 /* Counter-timer Hypervisor Control register */
  49. orr x0, x0, #3
  50. msr cnthctl_el2, x0
  51. msr cntvoff_el2, xzr
  52. /* enable AArch64 in EL1 */
  53. mov x0, #(1 << 31) /* AArch64 */
  54. orr x0, x0, #(1 << 1) /* SWIO hardwired on Pi3 */
  55. msr hcr_el2, x0
  56. mrs x0, hcr_el2
  57. /* change execution level to EL1 */
  58. mov x2, #0x3c4
  59. msr spsr_el2, x2 /* 1111000100 */
  60. adr x2, .L__in_el1
  61. msr elr_el2, x2
  62. eret /* exception return. from EL2. continue from .L__in_el1 */
  63. .L__in_el1:
  64. #ifdef RT_USING_LWP
  65. ldr x9, =PV_OFFSET
  66. #else
  67. mov x9, #0
  68. #endif
  69. mov sp, x1 /* in EL1. Set sp to _start */
  70. /* Set CPACR_EL1 (Architecture Feature Access Control Register) to avoid trap from SIMD or float point instruction */
  71. mov x1, #0x00300000 /* Don't trap any SIMD/FP instructions in both EL0 and EL1 */
  72. msr cpacr_el1, x1
  73. /* clear bss */
  74. ldr x1, =__bss_start /* get bss start address */
  75. ldr x2, =__bss_end
  76. sub x2, x2, x1 /* get bss size */
  77. add x1, x1, x9
  78. and x3, x2, #7 /* x3 is < 7 */
  79. ldr x4, =~0x7
  80. and x2, x2, x4 /* mask ~7 */
  81. .L__clean_bss_loop:
  82. cbz x2, .L__clean_bss_loop_1
  83. str xzr, [x1], #8
  84. sub x2, x2, #8
  85. b .L__clean_bss_loop
  86. .L__clean_bss_loop_1:
  87. cbz x3, .L__jump_to_entry
  88. strb wzr, [x1], #1
  89. sub x3, x3, #1
  90. b .L__clean_bss_loop_1
  91. .L__jump_to_entry: /* jump to C code, should not return */
  92. bl mmu_tcr_init
  93. adr x1, __start
  94. ldr x0, =~0x1fffff
  95. and x0, x1, x0
  96. add x1, x0, #0x1000
  97. msr ttbr0_el1, x0
  98. msr ttbr1_el1, x1
  99. dsb sy
  100. ldr x2, =0x40000000 /* map 1G memory for kernel space */
  101. #ifdef RT_USING_LWP
  102. ldr x3, =PV_OFFSET
  103. #endif
  104. bl rt_hw_mmu_setup_early
  105. ldr x30, =after_mmu_enable /* set LR to after_mmu_enable function, it's a v_addr */
  106. mrs x1, sctlr_el1
  107. bic x1, x1, #(3 << 3) /* dis SA, SA0 */
  108. bic x1, x1, #(1 << 1) /* dis A */
  109. orr x1, x1, #(1 << 12) /* I */
  110. orr x1, x1, #(1 << 2) /* C */
  111. orr x1, x1, #(1 << 0) /* M */
  112. msr sctlr_el1, x1 /* enable MMU */
  113. dsb sy
  114. isb sy
  115. ic ialluis /* Invalidate all instruction caches in Inner Shareable domain to Point of Unification */
  116. dsb sy
  117. isb sy
  118. tlbi vmalle1 /* Invalidate all stage 1 translations used at EL1 with the current VMID */
  119. dsb sy
  120. isb sy
  121. ret
  122. after_mmu_enable:
  123. #ifdef RT_USING_LWP
  124. mrs x0, tcr_el1 /* disable ttbr0, only using kernel space */
  125. orr x0, x0, #(1 << 7)
  126. msr tcr_el1, x0
  127. msr ttbr0_el1, xzr
  128. dsb sy
  129. #endif
  130. mov x0, #1
  131. msr spsel, x0
  132. adr x1, __start
  133. mov sp, x1 /* sp_el1 set to _start */
  134. b rtthread_startup
  135. #ifdef RT_USING_SMP
  136. /**
  137. * secondary cpu
  138. */
  139. .globl _secondary_cpu_entry
  140. _secondary_cpu_entry:
  141. bl rt_hw_cpu_id_set
  142. adr x1, __start
  143. /* set up EL1 */
  144. mrs x0, CurrentEL /* CurrentEL Register. bit 2, 3. Others reserved */
  145. and x0, x0, #12 /* clear reserved bits */
  146. /* running at EL3? */
  147. cmp x0, #12 /* 1100b. So, EL3 */
  148. bne .L__not_in_el3_cpux /* 11? !EL3 -> 5: */
  149. /* should never be executed, just for completeness. (EL3) */
  150. mov x2, #0x5b1
  151. msr scr_el3, x2 /* SCR_ELn Secure Configuration Register */
  152. mov x2, #0x3c9
  153. msr spsr_el3, x2 /* SPSR_ELn. Saved Program Status Register. 1111001001 */
  154. adr x2, .L__not_in_el3_cpux
  155. msr elr_el3, x2
  156. eret /* Exception Return: from EL3, continue from .L__not_in_el3 */
  157. .L__not_in_el3_cpux: /* running at EL2 or EL1 */
  158. cmp x0, #4 /* 0x04 0100 EL1 */
  159. beq .L__in_el1_cpux /* EL1 -> 5: */
  160. mrs x0, hcr_el2
  161. bic x0, x0, #0xff
  162. msr hcr_el2, x0
  163. msr sp_el1, x1 /* in EL2, set sp of EL1 to _start */
  164. /* enable CNTP for EL1 */
  165. mrs x0, cnthctl_el2 /* Counter-timer Hypervisor Control register */
  166. orr x0, x0, #3
  167. msr cnthctl_el2, x0
  168. msr cntvoff_el2, xzr
  169. /* enable AArch64 in EL1 */
  170. mov x0, #(1 << 31) /* AArch64 */
  171. orr x0, x0, #(1 << 1) /* SWIO hardwired on Pi3 */
  172. msr hcr_el2, x0
  173. mrs x0, hcr_el2
  174. /* change execution level to EL1 */
  175. mov x2, #0x3c4
  176. msr spsr_el2, x2 /* 1111000100 */
  177. adr x2, .L__in_el1_cpux
  178. msr elr_el2, x2
  179. eret /* exception return. from EL2. continue from .L__in_el1 */
  180. .L__in_el1_cpux:
  181. adr x19, .L__in_el1_cpux
  182. ldr x8, =.L__in_el1_cpux
  183. sub x19, x19, x8 /* get PV_OFFSET */
  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
  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