head.S 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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 <[email protected]>
  9. * Will Deacon <[email protected]>
  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. __INIT
  67. /*
  68. * The following callee saved general purpose registers are used on the
  69. * primary lowlevel boot path:
  70. *
  71. * Register Scope Purpose
  72. * x20 primary_entry() .. __primary_switch() CPU boot mode
  73. * x21 primary_entry() .. start_kernel() FDT pointer passed at boot in x0
  74. * x22 create_idmap() .. start_kernel() ID map VA of the DT blob
  75. * x23 primary_entry() .. start_kernel() physical misalignment/KASLR offset
  76. * x24 __primary_switch() linear map KASLR seed
  77. * x25 primary_entry() .. start_kernel() supported VA size
  78. * x28 create_idmap() callee preserved temp register
  79. */
  80. SYM_CODE_START(primary_entry)
  81. bl preserve_boot_args
  82. bl init_kernel_el // w0=cpu_boot_mode
  83. mov x20, x0
  84. bl create_idmap
  85. /*
  86. * The following calls CPU setup code, see arch/arm64/mm/proc.S for
  87. * details.
  88. * On return, the CPU will be ready for the MMU to be turned on and
  89. * the TCR will have been set.
  90. */
  91. #if VA_BITS > 48
  92. mrs_s x0, SYS_ID_AA64MMFR2_EL1
  93. tst x0, #0xf << ID_AA64MMFR2_EL1_VARange_SHIFT
  94. mov x0, #VA_BITS
  95. mov x25, #VA_BITS_MIN
  96. csel x25, x25, x0, eq
  97. mov x0, x25
  98. #endif
  99. bl __cpu_setup // initialise processor
  100. b __primary_switch
  101. SYM_CODE_END(primary_entry)
  102. /*
  103. * Preserve the arguments passed by the bootloader in x0 .. x3
  104. */
  105. SYM_CODE_START_LOCAL(preserve_boot_args)
  106. mov x21, x0 // x21=FDT
  107. adr_l x0, boot_args // record the contents of
  108. stp x21, x1, [x0] // x0 .. x3 at kernel entry
  109. stp x2, x3, [x0, #16]
  110. dmb sy // needed before dc ivac with
  111. // MMU off
  112. add x1, x0, #0x20 // 4 x 8 bytes
  113. b dcache_inval_poc // tail call
  114. SYM_CODE_END(preserve_boot_args)
  115. SYM_FUNC_START_LOCAL(clear_page_tables)
  116. /*
  117. * Clear the init page tables.
  118. */
  119. adrp x0, init_pg_dir
  120. adrp x1, init_pg_end
  121. sub x2, x1, x0
  122. mov x1, xzr
  123. b __pi_memset // tail call
  124. SYM_FUNC_END(clear_page_tables)
  125. /*
  126. * Macro to populate page table entries, these entries can be pointers to the next level
  127. * or last level entries pointing to physical memory.
  128. *
  129. * tbl: page table address
  130. * rtbl: pointer to page table or physical memory
  131. * index: start index to write
  132. * eindex: end index to write - [index, eindex] written to
  133. * flags: flags for pagetable entry to or in
  134. * inc: increment to rtbl between each entry
  135. * tmp1: temporary variable
  136. *
  137. * Preserves: tbl, eindex, flags, inc
  138. * Corrupts: index, tmp1
  139. * Returns: rtbl
  140. */
  141. .macro populate_entries, tbl, rtbl, index, eindex, flags, inc, tmp1
  142. .Lpe\@: phys_to_pte \tmp1, \rtbl
  143. orr \tmp1, \tmp1, \flags // tmp1 = table entry
  144. str \tmp1, [\tbl, \index, lsl #3]
  145. add \rtbl, \rtbl, \inc // rtbl = pa next level
  146. add \index, \index, #1
  147. cmp \index, \eindex
  148. b.ls .Lpe\@
  149. .endm
  150. /*
  151. * Compute indices of table entries from virtual address range. If multiple entries
  152. * were needed in the previous page table level then the next page table level is assumed
  153. * to be composed of multiple pages. (This effectively scales the end index).
  154. *
  155. * vstart: virtual address of start of range
  156. * vend: virtual address of end of range - we map [vstart, vend]
  157. * shift: shift used to transform virtual address into index
  158. * order: #imm 2log(number of entries in page table)
  159. * istart: index in table corresponding to vstart
  160. * iend: index in table corresponding to vend
  161. * count: On entry: how many extra entries were required in previous level, scales
  162. * our end index.
  163. * On exit: returns how many extra entries required for next page table level
  164. *
  165. * Preserves: vstart, vend
  166. * Returns: istart, iend, count
  167. */
  168. .macro compute_indices, vstart, vend, shift, order, istart, iend, count
  169. ubfx \istart, \vstart, \shift, \order
  170. ubfx \iend, \vend, \shift, \order
  171. add \iend, \iend, \count, lsl \order
  172. sub \count, \iend, \istart
  173. .endm
  174. /*
  175. * Map memory for specified virtual address range. Each level of page table needed supports
  176. * multiple entries. If a level requires n entries the next page table level is assumed to be
  177. * formed from n pages.
  178. *
  179. * tbl: location of page table
  180. * rtbl: address to be used for first level page table entry (typically tbl + PAGE_SIZE)
  181. * vstart: virtual address of start of range
  182. * vend: virtual address of end of range - we map [vstart, vend - 1]
  183. * flags: flags to use to map last level entries
  184. * phys: physical address corresponding to vstart - physical memory is contiguous
  185. * order: #imm 2log(number of entries in PGD table)
  186. *
  187. * If extra_shift is set, an extra level will be populated if the end address does
  188. * not fit in 'extra_shift' bits. This assumes vend is in the TTBR0 range.
  189. *
  190. * Temporaries: istart, iend, tmp, count, sv - these need to be different registers
  191. * Preserves: vstart, flags
  192. * Corrupts: tbl, rtbl, vend, istart, iend, tmp, count, sv
  193. */
  194. .macro map_memory, tbl, rtbl, vstart, vend, flags, phys, order, istart, iend, tmp, count, sv, extra_shift
  195. sub \vend, \vend, #1
  196. add \rtbl, \tbl, #PAGE_SIZE
  197. mov \count, #0
  198. .ifnb \extra_shift
  199. tst \vend, #~((1 << (\extra_shift)) - 1)
  200. b.eq .L_\@
  201. compute_indices \vstart, \vend, #\extra_shift, #(PAGE_SHIFT - 3), \istart, \iend, \count
  202. mov \sv, \rtbl
  203. populate_entries \tbl, \rtbl, \istart, \iend, #PMD_TYPE_TABLE, #PAGE_SIZE, \tmp
  204. mov \tbl, \sv
  205. .endif
  206. .L_\@:
  207. compute_indices \vstart, \vend, #PGDIR_SHIFT, #\order, \istart, \iend, \count
  208. mov \sv, \rtbl
  209. populate_entries \tbl, \rtbl, \istart, \iend, #PMD_TYPE_TABLE, #PAGE_SIZE, \tmp
  210. mov \tbl, \sv
  211. #if SWAPPER_PGTABLE_LEVELS > 3
  212. compute_indices \vstart, \vend, #PUD_SHIFT, #(PAGE_SHIFT - 3), \istart, \iend, \count
  213. mov \sv, \rtbl
  214. populate_entries \tbl, \rtbl, \istart, \iend, #PMD_TYPE_TABLE, #PAGE_SIZE, \tmp
  215. mov \tbl, \sv
  216. #endif
  217. #if SWAPPER_PGTABLE_LEVELS > 2
  218. compute_indices \vstart, \vend, #SWAPPER_TABLE_SHIFT, #(PAGE_SHIFT - 3), \istart, \iend, \count
  219. mov \sv, \rtbl
  220. populate_entries \tbl, \rtbl, \istart, \iend, #PMD_TYPE_TABLE, #PAGE_SIZE, \tmp
  221. mov \tbl, \sv
  222. #endif
  223. compute_indices \vstart, \vend, #SWAPPER_BLOCK_SHIFT, #(PAGE_SHIFT - 3), \istart, \iend, \count
  224. bic \rtbl, \phys, #SWAPPER_BLOCK_SIZE - 1
  225. populate_entries \tbl, \rtbl, \istart, \iend, \flags, #SWAPPER_BLOCK_SIZE, \tmp
  226. .endm
  227. /*
  228. * Remap a subregion created with the map_memory macro with modified attributes
  229. * or output address. The entire remapped region must have been covered in the
  230. * invocation of map_memory.
  231. *
  232. * x0: last level table address (returned in first argument to map_memory)
  233. * x1: start VA of the existing mapping
  234. * x2: start VA of the region to update
  235. * x3: end VA of the region to update (exclusive)
  236. * x4: start PA associated with the region to update
  237. * x5: attributes to set on the updated region
  238. * x6: order of the last level mappings
  239. */
  240. SYM_FUNC_START_LOCAL(remap_region)
  241. sub x3, x3, #1 // make end inclusive
  242. // Get the index offset for the start of the last level table
  243. lsr x1, x1, x6
  244. bfi x1, xzr, #0, #PAGE_SHIFT - 3
  245. // Derive the start and end indexes into the last level table
  246. // associated with the provided region
  247. lsr x2, x2, x6
  248. lsr x3, x3, x6
  249. sub x2, x2, x1
  250. sub x3, x3, x1
  251. mov x1, #1
  252. lsl x6, x1, x6 // block size at this level
  253. populate_entries x0, x4, x2, x3, x5, x6, x7
  254. ret
  255. SYM_FUNC_END(remap_region)
  256. SYM_FUNC_START_LOCAL(create_idmap)
  257. mov x28, lr
  258. /*
  259. * The ID map carries a 1:1 mapping of the physical address range
  260. * covered by the loaded image, which could be anywhere in DRAM. This
  261. * means that the required size of the VA (== PA) space is decided at
  262. * boot time, and could be more than the configured size of the VA
  263. * space for ordinary kernel and user space mappings.
  264. *
  265. * There are three cases to consider here:
  266. * - 39 <= VA_BITS < 48, and the ID map needs up to 48 VA bits to cover
  267. * the placement of the image. In this case, we configure one extra
  268. * level of translation on the fly for the ID map only. (This case
  269. * also covers 42-bit VA/52-bit PA on 64k pages).
  270. *
  271. * - VA_BITS == 48, and the ID map needs more than 48 VA bits. This can
  272. * only happen when using 64k pages, in which case we need to extend
  273. * the root level table rather than add a level. Note that we can
  274. * treat this case as 'always extended' as long as we take care not
  275. * to program an unsupported T0SZ value into the TCR register.
  276. *
  277. * - Combinations that would require two additional levels of
  278. * translation are not supported, e.g., VA_BITS==36 on 16k pages, or
  279. * VA_BITS==39/4k pages with 5-level paging, where the input address
  280. * requires more than 47 or 48 bits, respectively.
  281. */
  282. #if (VA_BITS < 48)
  283. #define IDMAP_PGD_ORDER (VA_BITS - PGDIR_SHIFT)
  284. #define EXTRA_SHIFT (PGDIR_SHIFT + PAGE_SHIFT - 3)
  285. /*
  286. * If VA_BITS < 48, we have to configure an additional table level.
  287. * First, we have to verify our assumption that the current value of
  288. * VA_BITS was chosen such that all translation levels are fully
  289. * utilised, and that lowering T0SZ will always result in an additional
  290. * translation level to be configured.
  291. */
  292. #if VA_BITS != EXTRA_SHIFT
  293. #error "Mismatch between VA_BITS and page size/number of translation levels"
  294. #endif
  295. #else
  296. #define IDMAP_PGD_ORDER (PHYS_MASK_SHIFT - PGDIR_SHIFT)
  297. #define EXTRA_SHIFT
  298. /*
  299. * If VA_BITS == 48, we don't have to configure an additional
  300. * translation level, but the top-level table has more entries.
  301. */
  302. #endif
  303. adrp x0, init_idmap_pg_dir
  304. adrp x3, _text
  305. adrp x6, _end + MAX_FDT_SIZE + SWAPPER_BLOCK_SIZE
  306. mov x7, SWAPPER_RX_MMUFLAGS
  307. map_memory x0, x1, x3, x6, x7, x3, IDMAP_PGD_ORDER, x10, x11, x12, x13, x14, EXTRA_SHIFT
  308. /* Remap the kernel page tables r/w in the ID map */
  309. adrp x1, _text
  310. adrp x2, init_pg_dir
  311. adrp x3, init_pg_end
  312. bic x4, x2, #SWAPPER_BLOCK_SIZE - 1
  313. mov x5, SWAPPER_RW_MMUFLAGS
  314. mov x6, #SWAPPER_BLOCK_SHIFT
  315. bl remap_region
  316. /* Remap the FDT after the kernel image */
  317. adrp x1, _text
  318. adrp x22, _end + SWAPPER_BLOCK_SIZE
  319. bic x2, x22, #SWAPPER_BLOCK_SIZE - 1
  320. bfi x22, x21, #0, #SWAPPER_BLOCK_SHIFT // remapped FDT address
  321. add x3, x2, #MAX_FDT_SIZE + SWAPPER_BLOCK_SIZE
  322. bic x4, x21, #SWAPPER_BLOCK_SIZE - 1
  323. mov x5, SWAPPER_RW_MMUFLAGS
  324. mov x6, #SWAPPER_BLOCK_SHIFT
  325. bl remap_region
  326. /*
  327. * Since the page tables have been populated with non-cacheable
  328. * accesses (MMU disabled), invalidate those tables again to
  329. * remove any speculatively loaded cache lines.
  330. */
  331. dmb sy
  332. adrp x0, init_idmap_pg_dir
  333. adrp x1, init_idmap_pg_end
  334. bl dcache_inval_poc
  335. ret x28
  336. SYM_FUNC_END(create_idmap)
  337. SYM_FUNC_START_LOCAL(create_kernel_mapping)
  338. adrp x0, init_pg_dir
  339. mov_q x5, KIMAGE_VADDR // compile time __va(_text)
  340. #ifdef CONFIG_RELOCATABLE
  341. add x5, x5, x23 // add KASLR displacement
  342. #endif
  343. adrp x6, _end // runtime __pa(_end)
  344. adrp x3, _text // runtime __pa(_text)
  345. sub x6, x6, x3 // _end - _text
  346. add x6, x6, x5 // runtime __va(_end)
  347. mov x7, SWAPPER_RW_MMUFLAGS
  348. map_memory x0, x1, x5, x6, x7, x3, (VA_BITS - PGDIR_SHIFT), x10, x11, x12, x13, x14
  349. dsb ishst // sync with page table walker
  350. ret
  351. SYM_FUNC_END(create_kernel_mapping)
  352. /*
  353. * Initialize CPU registers with task-specific and cpu-specific context.
  354. *
  355. * Create a final frame record at task_pt_regs(current)->stackframe, so
  356. * that the unwinder can identify the final frame record of any task by
  357. * its location in the task stack. We reserve the entire pt_regs space
  358. * for consistency with user tasks and kthreads.
  359. */
  360. .macro init_cpu_task tsk, tmp1, tmp2
  361. msr sp_el0, \tsk
  362. ldr \tmp1, [\tsk, #TSK_STACK]
  363. add sp, \tmp1, #THREAD_SIZE
  364. sub sp, sp, #PT_REGS_SIZE
  365. stp xzr, xzr, [sp, #S_STACKFRAME]
  366. add x29, sp, #S_STACKFRAME
  367. scs_load_current
  368. adr_l \tmp1, __per_cpu_offset
  369. ldr w\tmp2, [\tsk, #TSK_TI_CPU]
  370. ldr \tmp1, [\tmp1, \tmp2, lsl #3]
  371. set_this_cpu_offset \tmp1
  372. .endm
  373. /*
  374. * The following fragment of code is executed with the MMU enabled.
  375. *
  376. * x0 = __pa(KERNEL_START)
  377. */
  378. SYM_FUNC_START_LOCAL(__primary_switched)
  379. adr_l x4, init_task
  380. init_cpu_task x4, x5, x6
  381. adr_l x8, vectors // load VBAR_EL1 with virtual
  382. msr vbar_el1, x8 // vector table address
  383. isb
  384. stp x29, x30, [sp, #-16]!
  385. mov x29, sp
  386. str_l x21, __fdt_pointer, x5 // Save FDT pointer
  387. ldr_l x4, kimage_vaddr // Save the offset between
  388. sub x4, x4, x0 // the kernel virtual and
  389. str_l x4, kimage_voffset, x5 // physical mappings
  390. mov x0, x20
  391. bl set_cpu_boot_mode_flag
  392. // Clear BSS
  393. adr_l x0, __bss_start
  394. mov x1, xzr
  395. adr_l x2, __bss_stop
  396. sub x2, x2, x0
  397. bl __pi_memset
  398. dsb ishst // Make zero page visible to PTW
  399. #if VA_BITS > 48
  400. adr_l x8, vabits_actual // Set this early so KASAN early init
  401. str x25, [x8] // ... observes the correct value
  402. dc civac, x8 // Make visible to booting secondaries
  403. #endif
  404. #ifdef CONFIG_RANDOMIZE_BASE
  405. adrp x5, memstart_offset_seed // Save KASLR linear map seed
  406. strh w24, [x5, :lo12:memstart_offset_seed]
  407. #endif
  408. #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
  409. bl kasan_early_init
  410. #endif
  411. mov x0, x21 // pass FDT address in x0
  412. bl early_fdt_map // Try mapping the FDT early
  413. mov x0, x20 // pass the full boot status
  414. bl init_feature_override // Parse cpu feature overrides
  415. #ifdef CONFIG_UNWIND_PATCH_PAC_INTO_SCS
  416. bl scs_patch_vmlinux
  417. #endif
  418. mov x0, x20
  419. bl finalise_el2 // Prefer VHE if possible
  420. ldp x29, x30, [sp], #16
  421. bl start_kernel
  422. ASM_BUG()
  423. SYM_FUNC_END(__primary_switched)
  424. /*
  425. * end early head section, begin head code that is also used for
  426. * hotplug and needs to have the same protections as the text region
  427. */
  428. .section ".idmap.text","awx"
  429. /*
  430. * Starting from EL2 or EL1, configure the CPU to execute at the highest
  431. * reachable EL supported by the kernel in a chosen default state. If dropping
  432. * from EL2 to EL1, configure EL2 before configuring EL1.
  433. *
  434. * Since we cannot always rely on ERET synchronizing writes to sysregs (e.g. if
  435. * SCTLR_ELx.EOS is clear), we place an ISB prior to ERET.
  436. *
  437. * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in x0 if
  438. * booted in EL1 or EL2 respectively, with the top 32 bits containing
  439. * potential context flags. These flags are *not* stored in __boot_cpu_mode.
  440. */
  441. SYM_FUNC_START(init_kernel_el)
  442. mrs x0, CurrentEL
  443. cmp x0, #CurrentEL_EL2
  444. b.eq init_el2
  445. SYM_INNER_LABEL(init_el1, SYM_L_LOCAL)
  446. mov_q x0, INIT_SCTLR_EL1_MMU_OFF
  447. msr sctlr_el1, x0
  448. isb
  449. mov_q x0, INIT_PSTATE_EL1
  450. msr spsr_el1, x0
  451. msr elr_el1, lr
  452. mov w0, #BOOT_CPU_MODE_EL1
  453. eret
  454. SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
  455. mov_q x0, HCR_HOST_NVHE_FLAGS
  456. msr hcr_el2, x0
  457. isb
  458. init_el2_state
  459. /* Hypervisor stub */
  460. adr_l x0, __hyp_stub_vectors
  461. msr vbar_el2, x0
  462. isb
  463. mov_q x1, INIT_SCTLR_EL1_MMU_OFF
  464. /*
  465. * Fruity CPUs seem to have HCR_EL2.E2H set to RES1,
  466. * making it impossible to start in nVHE mode. Is that
  467. * compliant with the architecture? Absolutely not!
  468. */
  469. mrs x0, hcr_el2
  470. and x0, x0, #HCR_E2H
  471. cbz x0, 1f
  472. /* Set a sane SCTLR_EL1, the VHE way */
  473. msr_s SYS_SCTLR_EL12, x1
  474. mov x2, #BOOT_CPU_FLAG_E2H
  475. b 2f
  476. 1:
  477. msr sctlr_el1, x1
  478. mov x2, xzr
  479. 2:
  480. msr elr_el2, lr
  481. mov w0, #BOOT_CPU_MODE_EL2
  482. orr x0, x0, x2
  483. eret
  484. SYM_FUNC_END(init_kernel_el)
  485. /*
  486. * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed
  487. * in w0. See arch/arm64/include/asm/virt.h for more info.
  488. */
  489. SYM_FUNC_START_LOCAL(set_cpu_boot_mode_flag)
  490. adr_l x1, __boot_cpu_mode
  491. cmp w0, #BOOT_CPU_MODE_EL2
  492. b.ne 1f
  493. add x1, x1, #4
  494. 1: str w0, [x1] // Save CPU boot mode
  495. ret
  496. SYM_FUNC_END(set_cpu_boot_mode_flag)
  497. /*
  498. * This provides a "holding pen" for platforms to hold all secondary
  499. * cores are held until we're ready for them to initialise.
  500. */
  501. SYM_FUNC_START(secondary_holding_pen)
  502. bl init_kernel_el // w0=cpu_boot_mode
  503. mrs x2, mpidr_el1
  504. mov_q x1, MPIDR_HWID_BITMASK
  505. and x2, x2, x1
  506. adr_l x3, secondary_holding_pen_release
  507. pen: ldr x4, [x3]
  508. cmp x4, x2
  509. b.eq secondary_startup
  510. wfe
  511. b pen
  512. SYM_FUNC_END(secondary_holding_pen)
  513. /*
  514. * Secondary entry point that jumps straight into the kernel. Only to
  515. * be used where CPUs are brought online dynamically by the kernel.
  516. */
  517. SYM_FUNC_START(secondary_entry)
  518. bl init_kernel_el // w0=cpu_boot_mode
  519. b secondary_startup
  520. SYM_FUNC_END(secondary_entry)
  521. SYM_FUNC_START_LOCAL(secondary_startup)
  522. /*
  523. * Common entry point for secondary CPUs.
  524. */
  525. mov x20, x0 // preserve boot mode
  526. bl finalise_el2
  527. bl __cpu_secondary_check52bitva
  528. #if VA_BITS > 48
  529. ldr_l x0, vabits_actual
  530. #endif
  531. bl __cpu_setup // initialise processor
  532. adrp x1, swapper_pg_dir
  533. adrp x2, idmap_pg_dir
  534. bl __enable_mmu
  535. ldr x8, =__secondary_switched
  536. br x8
  537. SYM_FUNC_END(secondary_startup)
  538. SYM_FUNC_START_LOCAL(__secondary_switched)
  539. mov x0, x20
  540. bl set_cpu_boot_mode_flag
  541. str_l xzr, __early_cpu_boot_status, x3
  542. adr_l x5, vectors
  543. msr vbar_el1, x5
  544. isb
  545. adr_l x0, secondary_data
  546. ldr x2, [x0, #CPU_BOOT_TASK]
  547. cbz x2, __secondary_too_slow
  548. init_cpu_task x2, x1, x3
  549. #ifdef CONFIG_ARM64_PTR_AUTH
  550. ptrauth_keys_init_cpu x2, x3, x4, x5
  551. #endif
  552. bl secondary_start_kernel
  553. ASM_BUG()
  554. SYM_FUNC_END(__secondary_switched)
  555. SYM_FUNC_START_LOCAL(__secondary_too_slow)
  556. wfe
  557. wfi
  558. b __secondary_too_slow
  559. SYM_FUNC_END(__secondary_too_slow)
  560. /*
  561. * The booting CPU updates the failed status @__early_cpu_boot_status,
  562. * with MMU turned off.
  563. *
  564. * update_early_cpu_boot_status tmp, status
  565. * - Corrupts tmp1, tmp2
  566. * - Writes 'status' to __early_cpu_boot_status and makes sure
  567. * it is committed to memory.
  568. */
  569. .macro update_early_cpu_boot_status status, tmp1, tmp2
  570. mov \tmp2, #\status
  571. adr_l \tmp1, __early_cpu_boot_status
  572. str \tmp2, [\tmp1]
  573. dmb sy
  574. dc ivac, \tmp1 // Invalidate potentially stale cache line
  575. .endm
  576. /*
  577. * Enable the MMU.
  578. *
  579. * x0 = SCTLR_EL1 value for turning on the MMU.
  580. * x1 = TTBR1_EL1 value
  581. * x2 = ID map root table address
  582. *
  583. * Returns to the caller via x30/lr. This requires the caller to be covered
  584. * by the .idmap.text section.
  585. *
  586. * Checks if the selected granule size is supported by the CPU.
  587. * If it isn't, park the CPU
  588. */
  589. SYM_FUNC_START(__enable_mmu)
  590. mrs x3, ID_AA64MMFR0_EL1
  591. ubfx x3, x3, #ID_AA64MMFR0_EL1_TGRAN_SHIFT, 4
  592. cmp x3, #ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN
  593. b.lt __no_granule_support
  594. cmp x3, #ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX
  595. b.gt __no_granule_support
  596. phys_to_ttbr x2, x2
  597. msr ttbr0_el1, x2 // load TTBR0
  598. load_ttbr1 x1, x1, x3
  599. set_sctlr_el1 x0
  600. ret
  601. SYM_FUNC_END(__enable_mmu)
  602. SYM_FUNC_START(__cpu_secondary_check52bitva)
  603. #if VA_BITS > 48
  604. ldr_l x0, vabits_actual
  605. cmp x0, #52
  606. b.ne 2f
  607. mrs_s x0, SYS_ID_AA64MMFR2_EL1
  608. and x0, x0, #(0xf << ID_AA64MMFR2_EL1_VARange_SHIFT)
  609. cbnz x0, 2f
  610. update_early_cpu_boot_status \
  611. CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_52_BIT_VA, x0, x1
  612. 1: wfe
  613. wfi
  614. b 1b
  615. #endif
  616. 2: ret
  617. SYM_FUNC_END(__cpu_secondary_check52bitva)
  618. SYM_FUNC_START_LOCAL(__no_granule_support)
  619. /* Indicate that this CPU can't boot and is stuck in the kernel */
  620. update_early_cpu_boot_status \
  621. CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_NO_GRAN, x1, x2
  622. 1:
  623. wfe
  624. wfi
  625. b 1b
  626. SYM_FUNC_END(__no_granule_support)
  627. #ifdef CONFIG_RELOCATABLE
  628. SYM_FUNC_START_LOCAL(__relocate_kernel)
  629. /*
  630. * Iterate over each entry in the relocation table, and apply the
  631. * relocations in place.
  632. */
  633. adr_l x9, __rela_start
  634. adr_l x10, __rela_end
  635. mov_q x11, KIMAGE_VADDR // default virtual offset
  636. add x11, x11, x23 // actual virtual offset
  637. 0: cmp x9, x10
  638. b.hs 1f
  639. ldp x12, x13, [x9], #24
  640. ldr x14, [x9, #-8]
  641. cmp w13, #R_AARCH64_RELATIVE
  642. b.ne 0b
  643. add x14, x14, x23 // relocate
  644. str x14, [x12, x23]
  645. b 0b
  646. 1:
  647. #ifdef CONFIG_RELR
  648. /*
  649. * Apply RELR relocations.
  650. *
  651. * RELR is a compressed format for storing relative relocations. The
  652. * encoded sequence of entries looks like:
  653. * [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
  654. *
  655. * i.e. start with an address, followed by any number of bitmaps. The
  656. * address entry encodes 1 relocation. The subsequent bitmap entries
  657. * encode up to 63 relocations each, at subsequent offsets following
  658. * the last address entry.
  659. *
  660. * The bitmap entries must have 1 in the least significant bit. The
  661. * assumption here is that an address cannot have 1 in lsb. Odd
  662. * addresses are not supported. Any odd addresses are stored in the RELA
  663. * section, which is handled above.
  664. *
  665. * Excluding the least significant bit in the bitmap, each non-zero
  666. * bit in the bitmap represents a relocation to be applied to
  667. * a corresponding machine word that follows the base address
  668. * word. The second least significant bit represents the machine
  669. * word immediately following the initial address, and each bit
  670. * that follows represents the next word, in linear order. As such,
  671. * a single bitmap can encode up to 63 relocations in a 64-bit object.
  672. *
  673. * In this implementation we store the address of the next RELR table
  674. * entry in x9, the address being relocated by the current address or
  675. * bitmap entry in x13 and the address being relocated by the current
  676. * bit in x14.
  677. */
  678. adr_l x9, __relr_start
  679. adr_l x10, __relr_end
  680. 2: cmp x9, x10
  681. b.hs 7f
  682. ldr x11, [x9], #8
  683. tbnz x11, #0, 3f // branch to handle bitmaps
  684. add x13, x11, x23
  685. ldr x12, [x13] // relocate address entry
  686. add x12, x12, x23
  687. str x12, [x13], #8 // adjust to start of bitmap
  688. b 2b
  689. 3: mov x14, x13
  690. 4: lsr x11, x11, #1
  691. cbz x11, 6f
  692. tbz x11, #0, 5f // skip bit if not set
  693. ldr x12, [x14] // relocate bit
  694. add x12, x12, x23
  695. str x12, [x14]
  696. 5: add x14, x14, #8 // move to next bit's address
  697. b 4b
  698. 6: /*
  699. * Move to the next bitmap's address. 8 is the word size, and 63 is the
  700. * number of significant bits in a bitmap entry.
  701. */
  702. add x13, x13, #(8 * 63)
  703. b 2b
  704. 7:
  705. #endif
  706. ret
  707. SYM_FUNC_END(__relocate_kernel)
  708. #endif
  709. SYM_FUNC_START_LOCAL(__primary_switch)
  710. adrp x1, reserved_pg_dir
  711. adrp x2, init_idmap_pg_dir
  712. bl __enable_mmu
  713. #ifdef CONFIG_RELOCATABLE
  714. adrp x23, KERNEL_START
  715. and x23, x23, MIN_KIMG_ALIGN - 1
  716. #ifdef CONFIG_RANDOMIZE_BASE
  717. mov x0, x22
  718. adrp x1, init_pg_end
  719. mov sp, x1
  720. mov x29, xzr
  721. bl __pi_kaslr_early_init
  722. and x24, x0, #SZ_2M - 1 // capture memstart offset seed
  723. bic x0, x0, #SZ_2M - 1
  724. orr x23, x23, x0 // record kernel offset
  725. #endif
  726. #endif
  727. bl clear_page_tables
  728. bl create_kernel_mapping
  729. adrp x1, init_pg_dir
  730. load_ttbr1 x1, x1, x2
  731. #ifdef CONFIG_RELOCATABLE
  732. bl __relocate_kernel
  733. #endif
  734. ldr x8, =__primary_switched
  735. adrp x0, KERNEL_START // __pa(KERNEL_START)
  736. br x8
  737. SYM_FUNC_END(__primary_switch)