head.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Low-level CPU initialisation
  4. * Based on arch/arm/kernel/head.S
  5. *
  6. * Copyright (C) 1994-2002 Russell King
  7. * Copyright (C) 2003-2012 ARM Ltd.
  8. * Authors: Catalin Marinas <catalin.marinas@arm.com>
  9. * Will Deacon <will.deacon@arm.com>
  10. */
  11. #include <linux/linkage.h>
  12. #include <linux/init.h>
  13. #include <linux/pgtable.h>
  14. #include <asm/asm_pointer_auth.h>
  15. #include <asm/assembler.h>
  16. #include <asm/boot.h>
  17. #include <asm/bug.h>
  18. #include <asm/ptrace.h>
  19. #include <asm/asm-offsets.h>
  20. #include <asm/cache.h>
  21. #include <asm/cputype.h>
  22. #include <asm/el2_setup.h>
  23. #include <asm/elf.h>
  24. #include <asm/image.h>
  25. #include <asm/kernel-pgtable.h>
  26. #include <asm/kvm_arm.h>
  27. #include <asm/memory.h>
  28. #include <asm/pgtable-hwdef.h>
  29. #include <asm/page.h>
  30. #include <asm/scs.h>
  31. #include <asm/smp.h>
  32. #include <asm/sysreg.h>
  33. #include <asm/thread_info.h>
  34. #include <asm/virt.h>
  35. #include "efi-header.S"
  36. #if (PAGE_OFFSET & 0x1fffff) != 0
  37. #error PAGE_OFFSET must be at least 2MB aligned
  38. #endif
  39. /*
  40. * Kernel startup entry point.
  41. * ---------------------------
  42. *
  43. * The requirements are:
  44. * MMU = off, D-cache = off, I-cache = on or off,
  45. * x0 = physical address to the FDT blob.
  46. *
  47. * Note that the callee-saved registers are used for storing variables
  48. * that are useful before the MMU is enabled. The allocations are described
  49. * in the entry routines.
  50. */
  51. __HEAD
  52. /*
  53. * DO NOT MODIFY. Image header expected by Linux boot-loaders.
  54. */
  55. efi_signature_nop // special NOP to identity as PE/COFF executable
  56. b primary_entry // branch to kernel start, magic
  57. .quad 0 // Image load offset from start of RAM, little-endian
  58. le64sym _kernel_size_le // Effective size of kernel image, little-endian
  59. le64sym _kernel_flags_le // Informative flags, little-endian
  60. .quad 0 // reserved
  61. .quad 0 // reserved
  62. .quad 0 // reserved
  63. .ascii ARM64_IMAGE_MAGIC // Magic number
  64. .long .Lpe_header_offset // Offset to the PE header.
  65. __EFI_PE_HEADER
  66. .section ".idmap.text","a"
  67. /*
  68. * The following callee saved general purpose registers are used on the
  69. * primary lowlevel boot path:
  70. *
  71. * Register Scope Purpose
  72. * x19 primary_entry() .. start_kernel() whether we entered with the MMU on
  73. * x20 primary_entry() .. __primary_switch() CPU boot mode
  74. * x21 primary_entry() .. start_kernel() FDT pointer passed at boot in x0
  75. */
  76. SYM_CODE_START(primary_entry)
  77. bl record_mmu_state
  78. bl preserve_boot_args
  79. adrp x1, early_init_stack
  80. mov sp, x1
  81. mov x29, xzr
  82. adrp x0, init_idmap_pg_dir
  83. mov x1, xzr
  84. bl __pi_create_init_idmap
  85. /*
  86. * If the page tables have been populated with non-cacheable
  87. * accesses (MMU disabled), invalidate those tables again to
  88. * remove any speculatively loaded cache lines.
  89. */
  90. cbnz x19, 0f
  91. dmb sy
  92. mov x1, x0 // end of used region
  93. adrp x0, init_idmap_pg_dir
  94. adr_l x2, dcache_inval_poc
  95. blr x2
  96. b 1f
  97. /*
  98. * If we entered with the MMU and caches on, clean the ID mapped part
  99. * of the primary boot code to the PoC so we can safely execute it with
  100. * the MMU off.
  101. */
  102. 0: adrp x0, __idmap_text_start
  103. adr_l x1, __idmap_text_end
  104. adr_l x2, dcache_clean_poc
  105. blr x2
  106. 1: mov x0, x19
  107. bl init_kernel_el // w0=cpu_boot_mode
  108. mov x20, x0
  109. /*
  110. * The following calls CPU setup code, see arch/arm64/mm/proc.S for
  111. * details.
  112. * On return, the CPU will be ready for the MMU to be turned on and
  113. * the TCR will have been set.
  114. */
  115. bl __cpu_setup // initialise processor
  116. b __primary_switch
  117. SYM_CODE_END(primary_entry)
  118. __INIT
  119. SYM_CODE_START_LOCAL(record_mmu_state)
  120. mrs x19, CurrentEL
  121. cmp x19, #CurrentEL_EL2
  122. mrs x19, sctlr_el1
  123. b.ne 0f
  124. mrs x19, sctlr_el2
  125. 0:
  126. CPU_LE( tbnz x19, #SCTLR_ELx_EE_SHIFT, 1f )
  127. CPU_BE( tbz x19, #SCTLR_ELx_EE_SHIFT, 1f )
  128. tst x19, #SCTLR_ELx_C // Z := (C == 0)
  129. and x19, x19, #SCTLR_ELx_M // isolate M bit
  130. csel x19, xzr, x19, eq // clear x19 if Z
  131. ret
  132. /*
  133. * Set the correct endianness early so all memory accesses issued
  134. * before init_kernel_el() occur in the correct byte order. Note that
  135. * this means the MMU must be disabled, or the active ID map will end
  136. * up getting interpreted with the wrong byte order.
  137. */
  138. 1: eor x19, x19, #SCTLR_ELx_EE
  139. bic x19, x19, #SCTLR_ELx_M
  140. b.ne 2f
  141. pre_disable_mmu_workaround
  142. msr sctlr_el2, x19
  143. b 3f
  144. 2: pre_disable_mmu_workaround
  145. msr sctlr_el1, x19
  146. 3: isb
  147. mov x19, xzr
  148. ret
  149. SYM_CODE_END(record_mmu_state)
  150. /*
  151. * Preserve the arguments passed by the bootloader in x0 .. x3
  152. */
  153. SYM_CODE_START_LOCAL(preserve_boot_args)
  154. mov x21, x0 // x21=FDT
  155. adr_l x0, boot_args // record the contents of
  156. stp x21, x1, [x0] // x0 .. x3 at kernel entry
  157. stp x2, x3, [x0, #16]
  158. cbnz x19, 0f // skip cache invalidation if MMU is on
  159. dmb sy // needed before dc ivac with
  160. // MMU off
  161. add x1, x0, #0x20 // 4 x 8 bytes
  162. b dcache_inval_poc // tail call
  163. 0: str_l x19, mmu_enabled_at_boot, x0
  164. ret
  165. SYM_CODE_END(preserve_boot_args)
  166. /*
  167. * Initialize CPU registers with task-specific and cpu-specific context.
  168. *
  169. * Create a final frame record at task_pt_regs(current)->stackframe, so
  170. * that the unwinder can identify the final frame record of any task by
  171. * its location in the task stack. We reserve the entire pt_regs space
  172. * for consistency with user tasks and kthreads.
  173. */
  174. .macro init_cpu_task tsk, tmp1, tmp2
  175. msr sp_el0, \tsk
  176. ldr \tmp1, [\tsk, #TSK_STACK]
  177. add sp, \tmp1, #THREAD_SIZE
  178. sub sp, sp, #PT_REGS_SIZE
  179. stp xzr, xzr, [sp, #S_STACKFRAME]
  180. add x29, sp, #S_STACKFRAME
  181. scs_load_current
  182. adr_l \tmp1, __per_cpu_offset
  183. ldr w\tmp2, [\tsk, #TSK_TI_CPU]
  184. ldr \tmp1, [\tmp1, \tmp2, lsl #3]
  185. set_this_cpu_offset \tmp1
  186. .endm
  187. /*
  188. * The following fragment of code is executed with the MMU enabled.
  189. *
  190. * x0 = __pa(KERNEL_START)
  191. */
  192. SYM_FUNC_START_LOCAL(__primary_switched)
  193. adr_l x4, init_task
  194. init_cpu_task x4, x5, x6
  195. adr_l x8, vectors // load VBAR_EL1 with virtual
  196. msr vbar_el1, x8 // vector table address
  197. isb
  198. stp x29, x30, [sp, #-16]!
  199. mov x29, sp
  200. str_l x21, __fdt_pointer, x5 // Save FDT pointer
  201. adrp x4, _text // Save the offset between
  202. sub x4, x4, x0 // the kernel virtual and
  203. str_l x4, kimage_voffset, x5 // physical mappings
  204. mov x0, x20
  205. bl set_cpu_boot_mode_flag
  206. #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
  207. bl kasan_early_init
  208. #endif
  209. mov x0, x20
  210. bl finalise_el2 // Prefer VHE if possible
  211. ldp x29, x30, [sp], #16
  212. bl start_kernel
  213. ASM_BUG()
  214. SYM_FUNC_END(__primary_switched)
  215. /*
  216. * end early head section, begin head code that is also used for
  217. * hotplug and needs to have the same protections as the text region
  218. */
  219. .section ".idmap.text","a"
  220. /*
  221. * Starting from EL2 or EL1, configure the CPU to execute at the highest
  222. * reachable EL supported by the kernel in a chosen default state. If dropping
  223. * from EL2 to EL1, configure EL2 before configuring EL1.
  224. *
  225. * Since we cannot always rely on ERET synchronizing writes to sysregs (e.g. if
  226. * SCTLR_ELx.EOS is clear), we place an ISB prior to ERET.
  227. *
  228. * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in x0 if
  229. * booted in EL1 or EL2 respectively, with the top 32 bits containing
  230. * potential context flags. These flags are *not* stored in __boot_cpu_mode.
  231. *
  232. * x0: whether we are being called from the primary boot path with the MMU on
  233. */
  234. SYM_FUNC_START(init_kernel_el)
  235. mrs x1, CurrentEL
  236. cmp x1, #CurrentEL_EL2
  237. b.eq init_el2
  238. SYM_INNER_LABEL(init_el1, SYM_L_LOCAL)
  239. mov_q x0, INIT_SCTLR_EL1_MMU_OFF
  240. pre_disable_mmu_workaround
  241. msr sctlr_el1, x0
  242. isb
  243. mov_q x0, INIT_PSTATE_EL1
  244. msr spsr_el1, x0
  245. msr elr_el1, lr
  246. mov w0, #BOOT_CPU_MODE_EL1
  247. eret
  248. SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
  249. msr elr_el2, lr
  250. // clean all HYP code to the PoC if we booted at EL2 with the MMU on
  251. cbz x0, 0f
  252. adrp x0, __hyp_idmap_text_start
  253. adr_l x1, __hyp_text_end
  254. adr_l x2, dcache_clean_poc
  255. blr x2
  256. mov_q x0, INIT_SCTLR_EL2_MMU_OFF
  257. pre_disable_mmu_workaround
  258. msr sctlr_el2, x0
  259. isb
  260. 0:
  261. mov_q x0, HCR_HOST_NVHE_FLAGS
  262. /*
  263. * Compliant CPUs advertise their VHE-onlyness with
  264. * ID_AA64MMFR4_EL1.E2H0 < 0. HCR_EL2.E2H can be
  265. * RES1 in that case. Publish the E2H bit early so that
  266. * it can be picked up by the init_el2_state macro.
  267. *
  268. * Fruity CPUs seem to have HCR_EL2.E2H set to RAO/WI, but
  269. * don't advertise it (they predate this relaxation).
  270. */
  271. mrs_s x1, SYS_ID_AA64MMFR4_EL1
  272. tbz x1, #(ID_AA64MMFR4_EL1_E2H0_SHIFT + ID_AA64MMFR4_EL1_E2H0_WIDTH - 1), 1f
  273. orr x0, x0, #HCR_E2H
  274. 1:
  275. msr hcr_el2, x0
  276. isb
  277. init_el2_state
  278. /* Hypervisor stub */
  279. adr_l x0, __hyp_stub_vectors
  280. msr vbar_el2, x0
  281. isb
  282. mov_q x1, INIT_SCTLR_EL1_MMU_OFF
  283. mrs x0, hcr_el2
  284. and x0, x0, #HCR_E2H
  285. cbz x0, 2f
  286. /* Set a sane SCTLR_EL1, the VHE way */
  287. msr_s SYS_SCTLR_EL12, x1
  288. mov x2, #BOOT_CPU_FLAG_E2H
  289. b 3f
  290. 2:
  291. msr sctlr_el1, x1
  292. mov x2, xzr
  293. 3:
  294. __init_el2_nvhe_prepare_eret
  295. mov w0, #BOOT_CPU_MODE_EL2
  296. orr x0, x0, x2
  297. eret
  298. SYM_FUNC_END(init_kernel_el)
  299. /*
  300. * This provides a "holding pen" for platforms to hold all secondary
  301. * cores are held until we're ready for them to initialise.
  302. */
  303. SYM_FUNC_START(secondary_holding_pen)
  304. mov x0, xzr
  305. bl init_kernel_el // w0=cpu_boot_mode
  306. mrs x2, mpidr_el1
  307. mov_q x1, MPIDR_HWID_BITMASK
  308. and x2, x2, x1
  309. adr_l x3, secondary_holding_pen_release
  310. pen: ldr x4, [x3]
  311. cmp x4, x2
  312. b.eq secondary_startup
  313. wfe
  314. b pen
  315. SYM_FUNC_END(secondary_holding_pen)
  316. /*
  317. * Secondary entry point that jumps straight into the kernel. Only to
  318. * be used where CPUs are brought online dynamically by the kernel.
  319. */
  320. SYM_FUNC_START(secondary_entry)
  321. mov x0, xzr
  322. bl init_kernel_el // w0=cpu_boot_mode
  323. b secondary_startup
  324. SYM_FUNC_END(secondary_entry)
  325. SYM_FUNC_START_LOCAL(secondary_startup)
  326. /*
  327. * Common entry point for secondary CPUs.
  328. */
  329. mov x20, x0 // preserve boot mode
  330. #ifdef CONFIG_ARM64_VA_BITS_52
  331. alternative_if ARM64_HAS_VA52
  332. bl __cpu_secondary_check52bitva
  333. alternative_else_nop_endif
  334. #endif
  335. bl __cpu_setup // initialise processor
  336. adrp x1, swapper_pg_dir
  337. adrp x2, idmap_pg_dir
  338. bl __enable_mmu
  339. ldr x8, =__secondary_switched
  340. br x8
  341. SYM_FUNC_END(secondary_startup)
  342. .text
  343. SYM_FUNC_START_LOCAL(__secondary_switched)
  344. mov x0, x20
  345. bl set_cpu_boot_mode_flag
  346. mov x0, x20
  347. bl finalise_el2
  348. str_l xzr, __early_cpu_boot_status, x3
  349. adr_l x5, vectors
  350. msr vbar_el1, x5
  351. isb
  352. adr_l x0, secondary_data
  353. ldr x2, [x0, #CPU_BOOT_TASK]
  354. cbz x2, __secondary_too_slow
  355. init_cpu_task x2, x1, x3
  356. #ifdef CONFIG_ARM64_PTR_AUTH
  357. ptrauth_keys_init_cpu x2, x3, x4, x5
  358. #endif
  359. bl secondary_start_kernel
  360. ASM_BUG()
  361. SYM_FUNC_END(__secondary_switched)
  362. SYM_FUNC_START_LOCAL(__secondary_too_slow)
  363. wfe
  364. wfi
  365. b __secondary_too_slow
  366. SYM_FUNC_END(__secondary_too_slow)
  367. /*
  368. * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed
  369. * in w0. See arch/arm64/include/asm/virt.h for more info.
  370. */
  371. SYM_FUNC_START_LOCAL(set_cpu_boot_mode_flag)
  372. adr_l x1, __boot_cpu_mode
  373. cmp w0, #BOOT_CPU_MODE_EL2
  374. b.ne 1f
  375. add x1, x1, #4
  376. 1: str w0, [x1] // Save CPU boot mode
  377. ret
  378. SYM_FUNC_END(set_cpu_boot_mode_flag)
  379. /*
  380. * The booting CPU updates the failed status @__early_cpu_boot_status,
  381. * with MMU turned off.
  382. *
  383. * update_early_cpu_boot_status tmp, status
  384. * - Corrupts tmp1, tmp2
  385. * - Writes 'status' to __early_cpu_boot_status and makes sure
  386. * it is committed to memory.
  387. */
  388. .macro update_early_cpu_boot_status status, tmp1, tmp2
  389. mov \tmp2, #\status
  390. adr_l \tmp1, __early_cpu_boot_status
  391. str \tmp2, [\tmp1]
  392. dmb sy
  393. dc ivac, \tmp1 // Invalidate potentially stale cache line
  394. .endm
  395. /*
  396. * Enable the MMU.
  397. *
  398. * x0 = SCTLR_EL1 value for turning on the MMU.
  399. * x1 = TTBR1_EL1 value
  400. * x2 = ID map root table address
  401. *
  402. * Returns to the caller via x30/lr. This requires the caller to be covered
  403. * by the .idmap.text section.
  404. *
  405. * Checks if the selected granule size is supported by the CPU.
  406. * If it isn't, park the CPU
  407. */
  408. .section ".idmap.text","a"
  409. SYM_FUNC_START(__enable_mmu)
  410. mrs x3, ID_AA64MMFR0_EL1
  411. ubfx x3, x3, #ID_AA64MMFR0_EL1_TGRAN_SHIFT, 4
  412. cmp x3, #ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN
  413. b.lt __no_granule_support
  414. cmp x3, #ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX
  415. b.gt __no_granule_support
  416. phys_to_ttbr x2, x2
  417. msr ttbr0_el1, x2 // load TTBR0
  418. load_ttbr1 x1, x1, x3
  419. set_sctlr_el1 x0
  420. ret
  421. SYM_FUNC_END(__enable_mmu)
  422. #ifdef CONFIG_ARM64_VA_BITS_52
  423. SYM_FUNC_START(__cpu_secondary_check52bitva)
  424. #ifndef CONFIG_ARM64_LPA2
  425. mrs_s x0, SYS_ID_AA64MMFR2_EL1
  426. and x0, x0, ID_AA64MMFR2_EL1_VARange_MASK
  427. cbnz x0, 2f
  428. #else
  429. mrs x0, id_aa64mmfr0_el1
  430. sbfx x0, x0, #ID_AA64MMFR0_EL1_TGRAN_SHIFT, 4
  431. cmp x0, #ID_AA64MMFR0_EL1_TGRAN_LPA2
  432. b.ge 2f
  433. #endif
  434. update_early_cpu_boot_status \
  435. CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_52_BIT_VA, x0, x1
  436. 1: wfe
  437. wfi
  438. b 1b
  439. 2: ret
  440. SYM_FUNC_END(__cpu_secondary_check52bitva)
  441. #endif
  442. SYM_FUNC_START_LOCAL(__no_granule_support)
  443. /* Indicate that this CPU can't boot and is stuck in the kernel */
  444. update_early_cpu_boot_status \
  445. CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_NO_GRAN, x1, x2
  446. 1:
  447. wfe
  448. wfi
  449. b 1b
  450. SYM_FUNC_END(__no_granule_support)
  451. SYM_FUNC_START_LOCAL(__primary_switch)
  452. adrp x1, reserved_pg_dir
  453. adrp x2, init_idmap_pg_dir
  454. bl __enable_mmu
  455. adrp x1, early_init_stack
  456. mov sp, x1
  457. mov x29, xzr
  458. mov x0, x20 // pass the full boot status
  459. mov x1, x21 // pass the FDT
  460. bl __pi_early_map_kernel // Map and relocate the kernel
  461. ldr x8, =__primary_switched
  462. adrp x0, KERNEL_START // __pa(KERNEL_START)
  463. br x8
  464. SYM_FUNC_END(__primary_switch)