entry_point.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. #ifdef RT_USING_OFW
  179. /* Save devicetree info */
  180. mov x0, dtb_paddr
  181. bl rt_hw_fdt_install_early
  182. #endif
  183. b rtthread_startup
  184. #ifdef RT_USING_SMP
  185. /**
  186. * secondary cpu
  187. */
  188. .global _secondary_cpu_entry
  189. _secondary_cpu_entry:
  190. bl rt_hw_cpu_id_set
  191. adr x1, .el_stack_top
  192. /* set up EL1 */
  193. mrs x0, CurrentEL /* CurrentEL Register. bit 2, 3. Others reserved */
  194. and x0, x0, #12 /* clear reserved bits */
  195. /* running at EL3? */
  196. cmp x0, #12 /* 1100b. So, EL3 */
  197. bne .L__not_in_el3_cpux /* 11? !EL3 -> 5: */
  198. /* should never be executed, just for completeness. (EL3) */
  199. mov x2, #0x5b1
  200. msr scr_el3, x2 /* SCR_ELn Secure Configuration Register */
  201. mov x2, #0x3c9
  202. msr spsr_el3, x2 /* SPSR_ELn. Saved Program Status Register. 1111001001 */
  203. adr x2, .L__not_in_el3_cpux
  204. msr elr_el3, x2
  205. eret /* Exception Return: from EL3, continue from .L__not_in_el3 */
  206. .L__not_in_el3_cpux: /* running at EL2 or EL1 */
  207. cmp x0, #4 /* 0x04 0100 EL1 */
  208. beq .L__in_el1_cpux /* EL1 -> 5: */
  209. mrs x0, hcr_el2
  210. bic x0, x0, #0xff
  211. msr hcr_el2, x0
  212. msr sp_el1, x1 /* in EL2, set sp of EL1 to _start */
  213. /* enable CNTP for EL1 */
  214. mrs x0, cnthctl_el2 /* Counter-timer Hypervisor Control register */
  215. orr x0, x0, #3
  216. msr cnthctl_el2, x0
  217. msr cntvoff_el2, xzr
  218. /* enable AArch64 in EL1 */
  219. mov x0, #(1 << 31) /* AArch64 */
  220. orr x0, x0, #(1 << 1) /* SWIO hardwired on Pi3 */
  221. msr hcr_el2, x0
  222. mrs x0, hcr_el2
  223. /* change execution level to EL1 */
  224. mov x2, #0x3c4
  225. msr spsr_el2, x2 /* 1111000100 */
  226. adr x2, .L__in_el1_cpux
  227. msr elr_el2, x2
  228. eret /* exception return. from EL2. continue from .L__in_el1 */
  229. .L__in_el1_cpux:
  230. mrs x0, tpidr_el1
  231. /* each cpu init stack is 8k */
  232. sub x1, x1, x0, lsl #13
  233. mov sp, x1 /* in EL1. Set sp to _start */
  234. /* Set CPACR_EL1 (Architecture Feature Access Control Register) to avoid trap from SIMD or float point instruction */
  235. mov x1, #0x00300000 /* Don't trap any SIMD/FP instructions in both EL0 and EL1 */
  236. msr cpacr_el1, x1
  237. .L__jump_to_entry_cpux: /* jump to C code, should not return */
  238. /* init mmu early */
  239. bl mmu_tcr_init
  240. bl get_ttbrn_base
  241. add x1, x0, #0x1000
  242. msr ttbr0_el1, x0
  243. msr ttbr1_el1, x1
  244. dsb sy
  245. ldr x30, =after_mmu_enable_cpux /* set LR to after_mmu_enable function, it's a v_addr */
  246. mrs x1, sctlr_el1
  247. bic x1, x1, #(3 << 3) /* dis SA, SA0 */
  248. bic x1, x1, #(1 << 1) /* dis A */
  249. orr x1, x1, #(1 << 12) /* I */
  250. orr x1, x1, #(1 << 2) /* C */
  251. orr x1, x1, #(1 << 0) /* M */
  252. msr sctlr_el1, x1 /* enable MMU */
  253. dsb sy
  254. isb sy
  255. ic ialluis /* Invalidate all instruction caches in Inner Shareable domain to Point of Unification */
  256. dsb sy
  257. isb sy
  258. tlbi vmalle1 /* Invalidate all stage 1 translations used at EL1 with the current VMID */
  259. dsb sy
  260. isb sy
  261. ret
  262. after_mmu_enable_cpux:
  263. #ifdef RT_USING_SMART
  264. mrs x0, tcr_el1 /* disable ttbr0, only using kernel space */
  265. orr x0, x0, #(1 << 7)
  266. msr tcr_el1, x0
  267. msr ttbr0_el1, xzr
  268. dsb sy
  269. #endif
  270. mov x0, #1
  271. msr spsel, x0
  272. mrs x0, tpidr_el1
  273. /* each cpu init stack is 8k */
  274. adr x1, .el_stack_top
  275. sub x1, x1, x0, lsl #13
  276. mov sp, x1 /* in EL1. Set sp to _start */
  277. b rt_hw_secondary_cpu_bsp_start
  278. #endif
  279. #ifndef RT_CPUS_NR
  280. #define RT_CPUS_NR 1
  281. #endif
  282. .align 12
  283. .el_stack:
  284. .space (8192 * RT_CPUS_NR)
  285. .el_stack_top: