entry_point.S 9.3 KB

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