entry_point.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. * 2023-04-29 GuEe-GUI support kernel's ARM64 boot header
  10. */
  11. #include "rtconfig.h"
  12. .section ".text.entrypoint","ax"
  13. #ifdef RT_USING_OFW
  14. /*
  15. * Our goal is to boot the rt-thread as possible without modifying the
  16. * bootloader's config, so we use the kernel's boot header for ARM64:
  17. * https://www.kernel.org/doc/html/latest/arm64/booting.html#call-the-kernel-image
  18. */
  19. _head:
  20. b _start /* Executable code */
  21. .long 0 /* Executable code */
  22. .quad _text_offset /* Image load offset from start of RAM, little endian */
  23. .quad _end - _head /* Effective Image size, little endian (_end defined in link.lds) */
  24. .quad 0xa /* Kernel flags, little endian */
  25. .quad 0 /* Reserved */
  26. .quad 0 /* Reserved */
  27. .quad 0 /* Reserved */
  28. .ascii "ARM\x64" /* Magic number */
  29. .long 0 /* Reserved (used for PE COFF offset) */
  30. #endif
  31. /* Variable registers: x21~x28 */
  32. dtb_paddr .req x21
  33. boot_arg0 .req x22
  34. boot_arg1 .req x23
  35. boot_arg2 .req x24
  36. stack_top .req x25
  37. .global _start
  38. _start:
  39. /*
  40. * Boot CPU general-purpose register settings:
  41. * x0 = physical address of device tree blob (dtb) in system RAM.
  42. * x1 = 0 (reserved for future use)
  43. * x2 = 0 (reserved for future use)
  44. * x3 = 0 (reserved for future use)
  45. */
  46. mov dtb_paddr, x0
  47. mov boot_arg0, x1
  48. mov boot_arg1, x2
  49. mov boot_arg2, x3
  50. #ifdef ARCH_ARM_BOOTWITH_FLUSH_CACHE
  51. bl __asm_flush_dcache_all
  52. #endif
  53. bl rt_hw_cpu_id_set
  54. /* read cpu id, stop slave cores */
  55. mrs x0, tpidr_el1
  56. cbz x0, .L__cpu_0 /* .L prefix is the local label in ELF */
  57. #ifndef RT_AMP_SLAVE
  58. /* cpu id > 0, stop */
  59. /* cpu id == 0 will also goto here after returned from entry() if possible */
  60. .L__current_cpu_idle:
  61. wfe
  62. b .L__current_cpu_idle
  63. #endif
  64. .L__cpu_0:
  65. /* set stack before our code, Define stack pointer for current exception level */
  66. adr x1, .el_stack_top
  67. /* set up EL1 */
  68. mrs x0, CurrentEL /* CurrentEL Register. bit 2, 3. Others reserved */
  69. and x0, x0, #12 /* clear reserved bits */
  70. /* running at EL3? */
  71. cmp x0, #12 /* 1100b. So, EL3 */
  72. bne .L__not_in_el3 /* 11? !EL3 -> 5: */
  73. /* should never be executed, just for completeness. (EL3) */
  74. mov x2, #0x5b1
  75. msr scr_el3, x2 /* SCR_ELn Secure Configuration Register */
  76. mov x2, #0x3c9
  77. msr spsr_el3, x2 /* SPSR_ELn. Saved Program Status Register. 1111001001 */
  78. adr x2, .L__not_in_el3
  79. msr elr_el3, x2
  80. eret /* Exception Return: from EL3, continue from .L__not_in_el3 */
  81. .L__not_in_el3: /* running at EL2 or EL1 */
  82. cmp x0, #4 /* 0x04 0100 EL1 */
  83. beq .L__in_el1 /* EL1 -> 5: */
  84. mrs x0, hcr_el2
  85. bic x0, x0, #0xff
  86. msr hcr_el2, x0
  87. msr sp_el1, x1 /* in EL2, set sp of EL1 to _start */
  88. /* enable CNTP for EL1 */
  89. mrs x0, cnthctl_el2 /* Counter-timer Hypervisor Control register */
  90. orr x0, x0, #3
  91. msr cnthctl_el2, x0
  92. msr cntvoff_el2, xzr
  93. /* enable AArch64 in EL1 */
  94. mov x0, #(1 << 31) /* AArch64 */
  95. orr x0, x0, #(1 << 1) /* SWIO hardwired on Pi3 */
  96. msr hcr_el2, x0
  97. mrs x0, hcr_el2
  98. /* change execution level to EL1 */
  99. mov x2, #0x3c4
  100. msr spsr_el2, x2 /* 1111000100 */
  101. adr x2, .L__in_el1
  102. msr elr_el2, x2
  103. eret /* exception return. from EL2. continue from .L__in_el1 */
  104. .macro GET_PHY reg, symbol
  105. adrp \reg, \symbol
  106. add \reg, \reg, #:lo12:\symbol
  107. .endm
  108. .L__in_el1:
  109. mov sp, x1 /* in EL1. Set sp to _start */
  110. /* Set CPACR_EL1 (Architecture Feature Access Control Register) to avoid trap from SIMD or float point instruction */
  111. mov x1, #0x00300000 /* Don't trap any SIMD/FP instructions in both EL0 and EL1 */
  112. msr cpacr_el1, x1
  113. /* applying context change */
  114. dsb ish
  115. isb
  116. /* clear bss */
  117. GET_PHY x1, __bss_start
  118. GET_PHY x2, __bss_end
  119. sub x2, x2, x1 /* get bss size */
  120. and x3, x2, #7 /* x3 is < 7 */
  121. ldr x4, =~0x7
  122. and x2, x2, x4 /* mask ~7 */
  123. .L__clean_bss_loop:
  124. cbz x2, .L__clean_bss_loop_1
  125. str xzr, [x1], #8
  126. sub x2, x2, #8
  127. b .L__clean_bss_loop
  128. .L__clean_bss_loop_1:
  129. cbz x3, .L__jump_to_entry
  130. strb wzr, [x1], #1
  131. sub x3, x3, #1
  132. b .L__clean_bss_loop_1
  133. .L__jump_to_entry: /* jump to C code, should not return */
  134. bl mmu_tcr_init
  135. bl get_ttbrn_base
  136. add x1, x0, #0x1000
  137. msr ttbr0_el1, x0
  138. msr ttbr1_el1, x1
  139. dsb sy
  140. #ifdef RT_USING_SMART
  141. ldr x2, =_start
  142. GET_PHY x3, _start
  143. sub x3, x3, x2
  144. #else
  145. mov x3,0
  146. #endif
  147. ldr x2, =0x10000000 /* map 256M memory for kernel space */
  148. bl rt_hw_mem_setup_early
  149. ldr x30, =after_mmu_enable /* set LR to after_mmu_enable function, it's a v_addr */
  150. mrs x1, sctlr_el1
  151. bic x1, x1, #(3 << 3) /* dis SA, SA0 */
  152. bic x1, x1, #(1 << 1) /* dis A */
  153. orr x1, x1, #(1 << 12) /* I */
  154. orr x1, x1, #(1 << 2) /* C */
  155. orr x1, x1, #(1 << 0) /* M */
  156. msr sctlr_el1, x1 /* enable MMU */
  157. dsb ish
  158. isb
  159. ic ialluis /* Invalidate all instruction caches in Inner Shareable domain to Point of Unification */
  160. dsb ish
  161. isb
  162. tlbi vmalle1 /* Invalidate all stage 1 translations used at EL1 with the current VMID */
  163. dsb ish
  164. isb
  165. ret
  166. after_mmu_enable:
  167. #ifdef RT_USING_SMART
  168. mrs x0, tcr_el1 /* disable ttbr0, only using kernel space */
  169. orr x0, x0, #(1 << 7)
  170. msr tcr_el1, x0
  171. msr ttbr0_el1, xzr
  172. dsb sy
  173. #endif
  174. mov x0, #1
  175. msr spsel, x0
  176. adr x1, .el_stack_top
  177. mov sp, x1 /* sp_el1 set to _start */
  178. b rtthread_startup
  179. #ifdef RT_USING_SMP
  180. /**
  181. * secondary cpu
  182. */
  183. .global _secondary_cpu_entry
  184. _secondary_cpu_entry:
  185. bl rt_hw_cpu_id_set
  186. adr x1, .el_stack_top
  187. /* set up EL1 */
  188. mrs x0, CurrentEL /* CurrentEL Register. bit 2, 3. Others reserved */
  189. and x0, x0, #12 /* clear reserved bits */
  190. /* running at EL3? */
  191. cmp x0, #12 /* 1100b. So, EL3 */
  192. bne .L__not_in_el3_cpux /* 11? !EL3 -> 5: */
  193. /* should never be executed, just for completeness. (EL3) */
  194. mov x2, #0x5b1
  195. msr scr_el3, x2 /* SCR_ELn Secure Configuration Register */
  196. mov x2, #0x3c9
  197. msr spsr_el3, x2 /* SPSR_ELn. Saved Program Status Register. 1111001001 */
  198. adr x2, .L__not_in_el3_cpux
  199. msr elr_el3, x2
  200. eret /* Exception Return: from EL3, continue from .L__not_in_el3 */
  201. .L__not_in_el3_cpux: /* running at EL2 or EL1 */
  202. cmp x0, #4 /* 0x04 0100 EL1 */
  203. beq .L__in_el1_cpux /* EL1 -> 5: */
  204. mrs x0, hcr_el2
  205. bic x0, x0, #0xff
  206. msr hcr_el2, x0
  207. msr sp_el1, x1 /* in EL2, set sp of EL1 to _start */
  208. /* enable CNTP for EL1 */
  209. mrs x0, cnthctl_el2 /* Counter-timer Hypervisor Control register */
  210. orr x0, x0, #3
  211. msr cnthctl_el2, x0
  212. msr cntvoff_el2, xzr
  213. /* enable AArch64 in EL1 */
  214. mov x0, #(1 << 31) /* AArch64 */
  215. orr x0, x0, #(1 << 1) /* SWIO hardwired on Pi3 */
  216. msr hcr_el2, x0
  217. mrs x0, hcr_el2
  218. /* change execution level to EL1 */
  219. mov x2, #0x3c4
  220. msr spsr_el2, x2 /* 1111000100 */
  221. adr x2, .L__in_el1_cpux
  222. msr elr_el2, x2
  223. eret /* exception return. from EL2. continue from .L__in_el1 */
  224. .L__in_el1_cpux:
  225. mrs x0, tpidr_el1
  226. /* each cpu init stack is 8k */
  227. sub x1, x1, x0, lsl #13
  228. mov sp, x1 /* in EL1. Set sp to _start */
  229. /* Set CPACR_EL1 (Architecture Feature Access Control Register) to avoid trap from SIMD or float point instruction */
  230. mov x1, #0x00300000 /* Don't trap any SIMD/FP instructions in both EL0 and EL1 */
  231. msr cpacr_el1, x1
  232. .L__jump_to_entry_cpux: /* jump to C code, should not return */
  233. /* init mmu early */
  234. bl mmu_tcr_init
  235. bl get_ttbrn_base
  236. add x1, x0, #0x1000
  237. msr ttbr0_el1, x0
  238. msr ttbr1_el1, x1
  239. dsb sy
  240. ldr x30, =after_mmu_enable_cpux /* set LR to after_mmu_enable function, it's a v_addr */
  241. mrs x1, sctlr_el1
  242. bic x1, x1, #(3 << 3) /* dis SA, SA0 */
  243. bic x1, x1, #(1 << 1) /* dis A */
  244. orr x1, x1, #(1 << 12) /* I */
  245. orr x1, x1, #(1 << 2) /* C */
  246. orr x1, x1, #(1 << 0) /* M */
  247. msr sctlr_el1, x1 /* enable MMU */
  248. dsb sy
  249. isb sy
  250. ic ialluis /* Invalidate all instruction caches in Inner Shareable domain to Point of Unification */
  251. dsb sy
  252. isb sy
  253. tlbi vmalle1 /* Invalidate all stage 1 translations used at EL1 with the current VMID */
  254. dsb sy
  255. isb sy
  256. ret
  257. after_mmu_enable_cpux:
  258. #ifdef RT_USING_SMART
  259. mrs x0, tcr_el1 /* disable ttbr0, only using kernel space */
  260. orr x0, x0, #(1 << 7)
  261. msr tcr_el1, x0
  262. msr ttbr0_el1, xzr
  263. dsb sy
  264. #endif
  265. mov x0, #1
  266. msr spsel, x0
  267. mrs x0, tpidr_el1
  268. /* each cpu init stack is 8k */
  269. adr x1, .el_stack_top
  270. sub x1, x1, x0, lsl #13
  271. mov sp, x1 /* in EL1. Set sp to _start */
  272. b rt_hw_secondary_cpu_bsp_start
  273. #endif
  274. #ifndef RT_CPUS_NR
  275. #define RT_CPUS_NR 1
  276. #endif
  277. .align 12
  278. .el_stack:
  279. .space (8192 * RT_CPUS_NR)
  280. .el_stack_top: