entry_point.S 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (c) 2006-2023, 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. * 2024-01-18 Shell fix implicit dependency of cpuid management
  11. */
  12. #ifndef __ASSEMBLY__
  13. #define __ASSEMBLY__
  14. #endif
  15. #include <mmu.h>
  16. #include <rtconfig.h>
  17. #define ARM64_IMAGE_FLAG_BE_SHIFT 0
  18. #define ARM64_IMAGE_FLAG_PAGE_SIZE_SHIFT (ARM64_IMAGE_FLAG_BE_SHIFT + 1)
  19. #define ARM64_IMAGE_FLAG_PHYS_BASE_SHIFT (ARM64_IMAGE_FLAG_PAGE_SIZE_SHIFT + 2)
  20. #define ARM64_IMAGE_FLAG_LE 0
  21. #define ARM64_IMAGE_FLAG_BE 1
  22. #define ARM64_IMAGE_FLAG_PAGE_SIZE_4K 1
  23. #define ARM64_IMAGE_FLAG_PAGE_SIZE_16K 2
  24. #define ARM64_IMAGE_FLAG_PAGE_SIZE_64K 3
  25. #define ARM64_IMAGE_FLAG_PHYS_BASE 1
  26. #define _HEAD_FLAG(field) (_HEAD_FLAG_##field << ARM64_IMAGE_FLAG_##field##_SHIFT)
  27. #ifdef ARCH_CPU_BIG_ENDIAN
  28. #define _HEAD_FLAG_BE ARM64_IMAGE_FLAG_BE
  29. #else
  30. #define _HEAD_FLAG_BE ARM64_IMAGE_FLAG_LE
  31. #endif
  32. #define _HEAD_FLAG_PAGE_SIZE ((ARCH_PAGE_SHIFT - 10) / 2)
  33. #define _HEAD_FLAG_PHYS_BASE 1
  34. #define _HEAD_FLAGS (_HEAD_FLAG(BE) | _HEAD_FLAG(PAGE_SIZE) | _HEAD_FLAG(PHYS_BASE))
  35. .macro get_phy, reg, symbol
  36. adrp \reg, \symbol
  37. add \reg, \reg, #:lo12:\symbol
  38. .endm
  39. .macro get_pvoff, tmp, out
  40. ldr \tmp, =.boot_cpu_stack_top
  41. get_phy \out, .boot_cpu_stack_top
  42. sub \out, \out, \tmp
  43. .endm
  44. .section ".text.entrypoint","ax"
  45. #ifdef RT_USING_OFW
  46. /*
  47. * Our goal is to boot the rt-thread as possible without modifying the
  48. * bootloader's config, so we use the kernel's boot header for ARM64:
  49. * https://www.kernel.org/doc/html/latest/arch/arm64/booting.html#call-the-kernel-image
  50. */
  51. _head:
  52. b _start /* Executable code */
  53. .long 0 /* Executable code */
  54. .quad _text_offset /* Image load offset from start of RAM, little endian */
  55. .quad _end - _head /* Effective Image size, little endian (_end defined in link.lds) */
  56. .quad _HEAD_FLAGS /* Kernel flags, little endian */
  57. .quad 0 /* Reserved */
  58. .quad 0 /* Reserved */
  59. .quad 0 /* Reserved */
  60. .ascii "ARM\x64" /* Magic number */
  61. .long 0 /* Reserved (used for PE COFF offset) */
  62. #endif /* RT_USING_OFW */
  63. /* Variable registers: x21~x28 */
  64. dtb_paddr .req x21
  65. boot_arg0 .req x22
  66. boot_arg1 .req x23
  67. boot_arg2 .req x24
  68. stack_top .req x25
  69. .global _start
  70. _start:
  71. /*
  72. * Boot CPU general-purpose register settings:
  73. * x0 = physical address of device tree blob (dtb) in system RAM.
  74. * x1 = 0 (reserved for future use)
  75. * x2 = 0 (reserved for future use)
  76. * x3 = 0 (reserved for future use)
  77. */
  78. mov dtb_paddr, x0
  79. mov boot_arg0, x1
  80. mov boot_arg1, x2
  81. mov boot_arg2, x3
  82. /* Save cpu stack */
  83. get_phy stack_top, .boot_cpu_stack_top
  84. /* Save cpu id temp */
  85. #ifdef ARCH_USING_HW_THREAD_SELF
  86. msr tpidrro_el0, xzr
  87. /* Save thread self */
  88. #endif /* ARCH_USING_HW_THREAD_SELF */
  89. msr tpidr_el1, xzr
  90. bl init_cpu_el
  91. bl init_kernel_bss
  92. bl init_cpu_stack_early
  93. #ifdef RT_USING_OFW
  94. /* Save devicetree info */
  95. mov x0, dtb_paddr
  96. bl rt_hw_fdt_install_early
  97. #endif
  98. /* Now we are in the end of boot cpu process */
  99. ldr x8, =rtthread_startup
  100. b init_mmu_early
  101. /* never come back */
  102. kernel_start:
  103. /* jump to the PE's system entry */
  104. mov x29, xzr
  105. mov x30, x8
  106. br x8
  107. cpu_idle:
  108. wfe
  109. b cpu_idle
  110. #ifdef RT_USING_SMP
  111. .globl _secondary_cpu_entry
  112. _secondary_cpu_entry:
  113. #ifdef RT_USING_OFW
  114. /* Read cpu id */
  115. mrs x5, mpidr_el1
  116. ldr x1, =rt_cpu_mpidr_table
  117. get_pvoff x4 x2
  118. add x1, x1, x2
  119. mov x2, #0
  120. ldr x4, =0xff00ffffff
  121. and x0, x5, x4
  122. .cpu_id_confirm:
  123. add x2, x2, #1 /* Next cpu id inc */
  124. ldr x3, [x1], #8
  125. cmp x3, #0
  126. beq cpu_idle
  127. and x3, x3, x4
  128. cmp x3, x0
  129. bne .cpu_id_confirm
  130. /* Save this mpidr */
  131. str x5, [x1, #-8]
  132. /* Get cpu id success */
  133. sub x0, x2, #1
  134. #endif /* RT_USING_OFW */
  135. /* Save cpu id global */
  136. bl rt_hw_cpu_id_set
  137. bl rt_hw_cpu_id
  138. /* Set current cpu's stack top */
  139. sub x0, x0, #1
  140. mov x1, #ARCH_SECONDARY_CPU_STACK_SIZE
  141. get_phy x2, .secondary_cpu_stack_top
  142. msub stack_top, x0, x1, x2
  143. bl init_cpu_el
  144. bl init_cpu_stack_early
  145. /* secondary cpu start to startup */
  146. ldr x8, =rt_hw_secondary_cpu_bsp_start
  147. b enable_mmu_early
  148. #endif /* RT_USING_SMP */
  149. init_cpu_el:
  150. mrs x0, CurrentEL /* CurrentEL Register. bit 2, 3. Others reserved */
  151. lsr x0, x0, #2
  152. and x0, x0, #3
  153. /* running at EL3? */
  154. cmp x0, #3
  155. bne .init_cpu_hyp
  156. /* should never be executed, just for completeness. (EL3) */
  157. mov x1, #(1 << 0) /* EL0 and EL1 are in Non-Secure state */
  158. orr x1, x1, #(1 << 4) /* RES1 */
  159. orr x1, x1, #(1 << 5) /* RES1 */
  160. /* bic x1, x1, #(1 << 7) disable Secure Monitor Call */
  161. orr x1, x1, #(1 << 10) /* The next lower level is AArch64 */
  162. msr scr_el3, x1
  163. mov x1, #9 /* Next level is 0b1001->EL2h */
  164. orr x1, x1, #(1 << 6) /* Mask FIQ */
  165. orr x1, x1, #(1 << 7) /* Mask IRQ */
  166. orr x1, x1, #(1 << 8) /* Mask SError */
  167. orr x1, x1, #(1 << 9) /* Mask Debug Exception */
  168. msr spsr_el3, x1
  169. get_phy x1, .init_cpu_hyp
  170. msr elr_el3, x1
  171. eret
  172. .init_cpu_hyp:
  173. /* running at EL2? */
  174. cmp x0, #2 /* EL2 = 0b10 */
  175. bne .init_cpu_sys
  176. /* Enable CNTP for EL1 */
  177. mrs x0, cnthctl_el2 /* Counter-timer Hypervisor Control register */
  178. orr x0, x0, #(1 << 0) /* Don't traps NS EL0/1 accesses to the physical counter */
  179. orr x0, x0, #(1 << 1) /* Don't traps NS EL0/1 accesses to the physical timer */
  180. msr cnthctl_el2, x0
  181. msr cntvoff_el2, xzr
  182. mov x0, #(1 << 31) /* Enable AArch64 in EL1 */
  183. orr x0, x0, #(1 << 1) /* SWIO hardwired */
  184. msr hcr_el2, x0
  185. mov x0, #5 /* Next level is 0b0101->EL1h */
  186. orr x0, x0, #(1 << 6) /* Mask FIQ */
  187. orr x0, x0, #(1 << 7) /* Mask IRQ */
  188. orr x0, x0, #(1 << 8) /* Mask SError */
  189. orr x0, x0, #(1 << 9) /* Mask Debug Exception */
  190. msr spsr_el2, x0
  191. get_phy x0, .init_cpu_sys
  192. msr elr_el2, x0
  193. eret
  194. .init_cpu_sys:
  195. mrs x0, sctlr_el1
  196. bic x0, x0, #(3 << 3) /* Disable SP Alignment check */
  197. bic x0, x0, #(1 << 1) /* Disable Alignment check */
  198. msr sctlr_el1, x0
  199. mrs x0, cntkctl_el1
  200. orr x0, x0, #(1 << 1) /* Set EL0VCTEN, enabling the EL0 Virtual Count Timer */
  201. msr cntkctl_el1, x0
  202. /* Avoid trap from SIMD or float point instruction */
  203. mov x0, #0x00300000 /* Don't trap any SIMD/FP instructions in both EL0 and EL1 */
  204. msr cpacr_el1, x0
  205. /* Applying context change */
  206. dsb ish
  207. isb
  208. ret
  209. init_kernel_bss:
  210. get_phy x1, __bss_start
  211. get_phy x2, __bss_end
  212. sub x2, x2, x1 /* Get bss size */
  213. and x3, x2, #7 /* x3 is < 7 */
  214. ldr x4, =~0x7
  215. and x2, x2, x4 /* Mask ~7 */
  216. .clean_bss_loop_quad:
  217. cbz x2, .clean_bss_loop_byte
  218. str xzr, [x1], #8
  219. sub x2, x2, #8
  220. b .clean_bss_loop_quad
  221. .clean_bss_loop_byte:
  222. cbz x3, .clean_bss_end
  223. strb wzr, [x1], #1
  224. sub x3, x3, #1
  225. b .clean_bss_loop_byte
  226. .clean_bss_end:
  227. ret
  228. init_cpu_stack_early:
  229. msr spsel, #1
  230. mov sp, stack_top
  231. ret
  232. init_mmu_early:
  233. get_phy x0, .early_page_array
  234. bl set_free_page
  235. get_phy x0, .early_tbl0_page
  236. get_phy x1, .early_tbl1_page
  237. get_pvoff x2 x3
  238. ldr x2, =ARCH_EARLY_MAP_SIZE /* Map 1G memory for kernel space */
  239. bl rt_hw_mem_setup_early
  240. b enable_mmu_early
  241. enable_mmu_early:
  242. get_phy x0, .early_tbl0_page
  243. get_phy x1, .early_tbl1_page
  244. msr ttbr0_el1, x0
  245. msr ttbr1_el1, x1
  246. dsb sy
  247. bl mmu_tcr_init
  248. /*
  249. * OK, now, we don't use sp before jump to kernel, set sp to current cpu's
  250. * stack top to visual address
  251. */
  252. get_pvoff x1 x0
  253. mov x1, stack_top
  254. sub x1, x1, x0
  255. mov sp, x1
  256. ldr x30, =kernel_start /* Set LR to kernel_start function, it's virtual addresses */
  257. /* Enable page table translation */
  258. mrs x1, sctlr_el1
  259. orr x1, x1, #(1 << 12) /* Stage 1 instruction access Cacheability control */
  260. orr x1, x1, #(1 << 2) /* Cacheable Normal memory in stage1 */
  261. orr x1, x1, #(1 << 0) /* MMU Enable */
  262. msr sctlr_el1, x1
  263. dsb ish
  264. isb
  265. ic ialluis /* Invalidate all instruction caches in Inner Shareable domain to Point of Unification */
  266. dsb ish
  267. isb
  268. tlbi vmalle1 /* Invalidate all stage 1 translations used at EL1 with the current VMID */
  269. dsb ish
  270. isb
  271. ret
  272. /*
  273. * CPU stack builtin
  274. */
  275. .section ".bss.noclean.cpus_stack"
  276. .align 12
  277. .cpus_stack:
  278. #if defined(RT_USING_SMP) && RT_CPUS_NR > 1
  279. .space (ARCH_SECONDARY_CPU_STACK_SIZE * (RT_CPUS_NR - 1))
  280. #endif
  281. .secondary_cpu_stack_top:
  282. .space ARCH_SECONDARY_CPU_STACK_SIZE
  283. .boot_cpu_stack_top:
  284. /*
  285. * Early page builtin
  286. */
  287. .section ".bss.noclean.early_page"
  288. .align 12
  289. .early_tbl0_page:
  290. .space ARCH_PAGE_SIZE
  291. .early_tbl1_page:
  292. /* Map 4G -> 2M * 512 entries */
  293. .space 4 * ARCH_PAGE_SIZE
  294. .early_page_array:
  295. .space 24 * ARCH_PAGE_SIZE