head.S 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * Low-level CPU initialisation
  3. * Based on arch/arm/kernel/head.S
  4. *
  5. * Copyright (C) 1994-2002 Russell King
  6. * Copyright (C) 2003-2012 ARM Ltd.
  7. * Authors: Catalin Marinas <catalin.marinas@arm.com>
  8. * Will Deacon <will.deacon@arm.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/linkage.h>
  23. #include <linux/init.h>
  24. #include <linux/irqchip/arm-gic-v3.h>
  25. #include <asm/assembler.h>
  26. #include <asm/boot.h>
  27. #include <asm/ptrace.h>
  28. #include <asm/asm-offsets.h>
  29. #include <asm/cache.h>
  30. #include <asm/cputype.h>
  31. #include <asm/elf.h>
  32. #include <asm/kernel-pgtable.h>
  33. #include <asm/kvm_arm.h>
  34. #include <asm/memory.h>
  35. #include <asm/pgtable-hwdef.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/page.h>
  38. #include <asm/smp.h>
  39. #include <asm/sysreg.h>
  40. #include <asm/thread_info.h>
  41. #include <asm/virt.h>
  42. #include "efi-header.S"
  43. #define __PHYS_OFFSET (KERNEL_START - TEXT_OFFSET)
  44. #if (TEXT_OFFSET & 0xfff) != 0
  45. #error TEXT_OFFSET must be at least 4KB aligned
  46. #elif (PAGE_OFFSET & 0x1fffff) != 0
  47. #error PAGE_OFFSET must be at least 2MB aligned
  48. #elif TEXT_OFFSET > 0x1fffff
  49. #error TEXT_OFFSET must be less than 2MB
  50. #endif
  51. /*
  52. * Kernel startup entry point.
  53. * ---------------------------
  54. *
  55. * The requirements are:
  56. * MMU = off, D-cache = off, I-cache = on or off,
  57. * x0 = physical address to the FDT blob.
  58. *
  59. * This code is mostly position independent so you call this at
  60. * __pa(PAGE_OFFSET + TEXT_OFFSET).
  61. *
  62. * Note that the callee-saved registers are used for storing variables
  63. * that are useful before the MMU is enabled. The allocations are described
  64. * in the entry routines.
  65. */
  66. __HEAD
  67. _head:
  68. /*
  69. * DO NOT MODIFY. Image header expected by Linux boot-loaders.
  70. */
  71. #ifdef CONFIG_EFI
  72. /*
  73. * This add instruction has no meaningful effect except that
  74. * its opcode forms the magic "MZ" signature required by UEFI.
  75. */
  76. add x13, x18, #0x16
  77. b stext
  78. #else
  79. b stext // branch to kernel start, magic
  80. .long 0 // reserved
  81. #endif
  82. le64sym _kernel_offset_le // Image load offset from start of RAM, little-endian
  83. le64sym _kernel_size_le // Effective size of kernel image, little-endian
  84. le64sym _kernel_flags_le // Informative flags, little-endian
  85. .quad 0 // reserved
  86. .quad 0 // reserved
  87. .quad 0 // reserved
  88. .ascii "ARM\x64" // Magic number
  89. #ifdef CONFIG_EFI
  90. .long pe_header - _head // Offset to the PE header.
  91. pe_header:
  92. __EFI_PE_HEADER
  93. #else
  94. .long 0 // reserved
  95. #endif
  96. __INIT
  97. /*
  98. * The following callee saved general purpose registers are used on the
  99. * primary lowlevel boot path:
  100. *
  101. * Register Scope Purpose
  102. * x21 stext() .. start_kernel() FDT pointer passed at boot in x0
  103. * x23 stext() .. start_kernel() physical misalignment/KASLR offset
  104. * x28 __create_page_tables() callee preserved temp register
  105. * x19/x20 __primary_switch() callee preserved temp registers
  106. */
  107. ENTRY(stext)
  108. bl preserve_boot_args
  109. bl el2_setup // Drop to EL1, w0=cpu_boot_mode
  110. adrp x23, __PHYS_OFFSET
  111. and x23, x23, MIN_KIMG_ALIGN - 1 // KASLR offset, defaults to 0
  112. bl set_cpu_boot_mode_flag
  113. bl __create_page_tables
  114. /*
  115. * The following calls CPU setup code, see arch/arm64/mm/proc.S for
  116. * details.
  117. * On return, the CPU will be ready for the MMU to be turned on and
  118. * the TCR will have been set.
  119. */
  120. bl __cpu_setup // initialise processor
  121. b __primary_switch
  122. ENDPROC(stext)
  123. /*
  124. * Preserve the arguments passed by the bootloader in x0 .. x3
  125. */
  126. preserve_boot_args:
  127. mov x21, x0 // x21=FDT
  128. adr_l x0, boot_args // record the contents of
  129. stp x21, x1, [x0] // x0 .. x3 at kernel entry
  130. stp x2, x3, [x0, #16]
  131. dmb sy // needed before dc ivac with
  132. // MMU off
  133. mov x1, #0x20 // 4 x 8 bytes
  134. b __inval_dcache_area // tail call
  135. ENDPROC(preserve_boot_args)
  136. /*
  137. * Macro to create a table entry to the next page.
  138. *
  139. * tbl: page table address
  140. * virt: virtual address
  141. * shift: #imm page table shift
  142. * ptrs: #imm pointers per table page
  143. *
  144. * Preserves: virt
  145. * Corrupts: ptrs, tmp1, tmp2
  146. * Returns: tbl -> next level table page address
  147. */
  148. .macro create_table_entry, tbl, virt, shift, ptrs, tmp1, tmp2
  149. add \tmp1, \tbl, #PAGE_SIZE
  150. phys_to_pte \tmp2, \tmp1
  151. orr \tmp2, \tmp2, #PMD_TYPE_TABLE // address of next table and entry type
  152. lsr \tmp1, \virt, #\shift
  153. sub \ptrs, \ptrs, #1
  154. and \tmp1, \tmp1, \ptrs // table index
  155. str \tmp2, [\tbl, \tmp1, lsl #3]
  156. add \tbl, \tbl, #PAGE_SIZE // next level table page
  157. .endm
  158. /*
  159. * Macro to populate page table entries, these entries can be pointers to the next level
  160. * or last level entries pointing to physical memory.
  161. *
  162. * tbl: page table address
  163. * rtbl: pointer to page table or physical memory
  164. * index: start index to write
  165. * eindex: end index to write - [index, eindex] written to
  166. * flags: flags for pagetable entry to or in
  167. * inc: increment to rtbl between each entry
  168. * tmp1: temporary variable
  169. *
  170. * Preserves: tbl, eindex, flags, inc
  171. * Corrupts: index, tmp1
  172. * Returns: rtbl
  173. */
  174. .macro populate_entries, tbl, rtbl, index, eindex, flags, inc, tmp1
  175. .Lpe\@: phys_to_pte \tmp1, \rtbl
  176. orr \tmp1, \tmp1, \flags // tmp1 = table entry
  177. str \tmp1, [\tbl, \index, lsl #3]
  178. add \rtbl, \rtbl, \inc // rtbl = pa next level
  179. add \index, \index, #1
  180. cmp \index, \eindex
  181. b.ls .Lpe\@
  182. .endm
  183. /*
  184. * Compute indices of table entries from virtual address range. If multiple entries
  185. * were needed in the previous page table level then the next page table level is assumed
  186. * to be composed of multiple pages. (This effectively scales the end index).
  187. *
  188. * vstart: virtual address of start of range
  189. * vend: virtual address of end of range
  190. * shift: shift used to transform virtual address into index
  191. * ptrs: number of entries in page table
  192. * istart: index in table corresponding to vstart
  193. * iend: index in table corresponding to vend
  194. * count: On entry: how many extra entries were required in previous level, scales
  195. * our end index.
  196. * On exit: returns how many extra entries required for next page table level
  197. *
  198. * Preserves: vstart, vend, shift, ptrs
  199. * Returns: istart, iend, count
  200. */
  201. .macro compute_indices, vstart, vend, shift, ptrs, istart, iend, count
  202. lsr \iend, \vend, \shift
  203. mov \istart, \ptrs
  204. sub \istart, \istart, #1
  205. and \iend, \iend, \istart // iend = (vend >> shift) & (ptrs - 1)
  206. mov \istart, \ptrs
  207. mul \istart, \istart, \count
  208. add \iend, \iend, \istart // iend += (count - 1) * ptrs
  209. // our entries span multiple tables
  210. lsr \istart, \vstart, \shift
  211. mov \count, \ptrs
  212. sub \count, \count, #1
  213. and \istart, \istart, \count
  214. sub \count, \iend, \istart
  215. .endm
  216. /*
  217. * Map memory for specified virtual address range. Each level of page table needed supports
  218. * multiple entries. If a level requires n entries the next page table level is assumed to be
  219. * formed from n pages.
  220. *
  221. * tbl: location of page table
  222. * rtbl: address to be used for first level page table entry (typically tbl + PAGE_SIZE)
  223. * vstart: start address to map
  224. * vend: end address to map - we map [vstart, vend]
  225. * flags: flags to use to map last level entries
  226. * phys: physical address corresponding to vstart - physical memory is contiguous
  227. * pgds: the number of pgd entries
  228. *
  229. * Temporaries: istart, iend, tmp, count, sv - these need to be different registers
  230. * Preserves: vstart, vend, flags
  231. * Corrupts: tbl, rtbl, istart, iend, tmp, count, sv
  232. */
  233. .macro map_memory, tbl, rtbl, vstart, vend, flags, phys, pgds, istart, iend, tmp, count, sv
  234. add \rtbl, \tbl, #PAGE_SIZE
  235. mov \sv, \rtbl
  236. mov \count, #0
  237. compute_indices \vstart, \vend, #PGDIR_SHIFT, \pgds, \istart, \iend, \count
  238. populate_entries \tbl, \rtbl, \istart, \iend, #PMD_TYPE_TABLE, #PAGE_SIZE, \tmp
  239. mov \tbl, \sv
  240. mov \sv, \rtbl
  241. #if SWAPPER_PGTABLE_LEVELS > 3
  242. compute_indices \vstart, \vend, #PUD_SHIFT, #PTRS_PER_PUD, \istart, \iend, \count
  243. populate_entries \tbl, \rtbl, \istart, \iend, #PMD_TYPE_TABLE, #PAGE_SIZE, \tmp
  244. mov \tbl, \sv
  245. mov \sv, \rtbl
  246. #endif
  247. #if SWAPPER_PGTABLE_LEVELS > 2
  248. compute_indices \vstart, \vend, #SWAPPER_TABLE_SHIFT, #PTRS_PER_PMD, \istart, \iend, \count
  249. populate_entries \tbl, \rtbl, \istart, \iend, #PMD_TYPE_TABLE, #PAGE_SIZE, \tmp
  250. mov \tbl, \sv
  251. #endif
  252. compute_indices \vstart, \vend, #SWAPPER_BLOCK_SHIFT, #PTRS_PER_PTE, \istart, \iend, \count
  253. bic \count, \phys, #SWAPPER_BLOCK_SIZE - 1
  254. populate_entries \tbl, \count, \istart, \iend, \flags, #SWAPPER_BLOCK_SIZE, \tmp
  255. .endm
  256. /*
  257. * Setup the initial page tables. We only setup the barest amount which is
  258. * required to get the kernel running. The following sections are required:
  259. * - identity mapping to enable the MMU (low address, TTBR0)
  260. * - first few MB of the kernel linear mapping to jump to once the MMU has
  261. * been enabled
  262. */
  263. __create_page_tables:
  264. mov x28, lr
  265. /*
  266. * Invalidate the idmap and swapper page tables to avoid potential
  267. * dirty cache lines being evicted.
  268. */
  269. adrp x0, idmap_pg_dir
  270. adrp x1, swapper_pg_end
  271. sub x1, x1, x0
  272. bl __inval_dcache_area
  273. /*
  274. * Clear the idmap and swapper page tables.
  275. */
  276. adrp x0, idmap_pg_dir
  277. adrp x1, swapper_pg_end
  278. sub x1, x1, x0
  279. 1: stp xzr, xzr, [x0], #16
  280. stp xzr, xzr, [x0], #16
  281. stp xzr, xzr, [x0], #16
  282. stp xzr, xzr, [x0], #16
  283. subs x1, x1, #64
  284. b.ne 1b
  285. mov x7, SWAPPER_MM_MMUFLAGS
  286. /*
  287. * Create the identity mapping.
  288. */
  289. adrp x0, idmap_pg_dir
  290. adrp x3, __idmap_text_start // __pa(__idmap_text_start)
  291. /*
  292. * VA_BITS may be too small to allow for an ID mapping to be created
  293. * that covers system RAM if that is located sufficiently high in the
  294. * physical address space. So for the ID map, use an extended virtual
  295. * range in that case, and configure an additional translation level
  296. * if needed.
  297. *
  298. * Calculate the maximum allowed value for TCR_EL1.T0SZ so that the
  299. * entire ID map region can be mapped. As T0SZ == (64 - #bits used),
  300. * this number conveniently equals the number of leading zeroes in
  301. * the physical address of __idmap_text_end.
  302. */
  303. adrp x5, __idmap_text_end
  304. clz x5, x5
  305. cmp x5, TCR_T0SZ(VA_BITS) // default T0SZ small enough?
  306. b.ge 1f // .. then skip VA range extension
  307. adr_l x6, idmap_t0sz
  308. str x5, [x6]
  309. dmb sy
  310. dc ivac, x6 // Invalidate potentially stale cache line
  311. #if (VA_BITS < 48)
  312. #define EXTRA_SHIFT (PGDIR_SHIFT + PAGE_SHIFT - 3)
  313. #define EXTRA_PTRS (1 << (PHYS_MASK_SHIFT - EXTRA_SHIFT))
  314. /*
  315. * If VA_BITS < 48, we have to configure an additional table level.
  316. * First, we have to verify our assumption that the current value of
  317. * VA_BITS was chosen such that all translation levels are fully
  318. * utilised, and that lowering T0SZ will always result in an additional
  319. * translation level to be configured.
  320. */
  321. #if VA_BITS != EXTRA_SHIFT
  322. #error "Mismatch between VA_BITS and page size/number of translation levels"
  323. #endif
  324. mov x4, EXTRA_PTRS
  325. create_table_entry x0, x3, EXTRA_SHIFT, x4, x5, x6
  326. #else
  327. /*
  328. * If VA_BITS == 48, we don't have to configure an additional
  329. * translation level, but the top-level table has more entries.
  330. */
  331. mov x4, #1 << (PHYS_MASK_SHIFT - PGDIR_SHIFT)
  332. str_l x4, idmap_ptrs_per_pgd, x5
  333. #endif
  334. 1:
  335. ldr_l x4, idmap_ptrs_per_pgd
  336. mov x5, x3 // __pa(__idmap_text_start)
  337. adr_l x6, __idmap_text_end // __pa(__idmap_text_end)
  338. map_memory x0, x1, x3, x6, x7, x3, x4, x10, x11, x12, x13, x14
  339. /*
  340. * Map the kernel image (starting with PHYS_OFFSET).
  341. */
  342. adrp x0, swapper_pg_dir
  343. mov_q x5, KIMAGE_VADDR + TEXT_OFFSET // compile time __va(_text)
  344. add x5, x5, x23 // add KASLR displacement
  345. mov x4, PTRS_PER_PGD
  346. adrp x6, _end // runtime __pa(_end)
  347. adrp x3, _text // runtime __pa(_text)
  348. sub x6, x6, x3 // _end - _text
  349. add x6, x6, x5 // runtime __va(_end)
  350. map_memory x0, x1, x5, x6, x7, x3, x4, x10, x11, x12, x13, x14
  351. /*
  352. * Since the page tables have been populated with non-cacheable
  353. * accesses (MMU disabled), invalidate the idmap and swapper page
  354. * tables again to remove any speculatively loaded cache lines.
  355. */
  356. adrp x0, idmap_pg_dir
  357. adrp x1, swapper_pg_end
  358. sub x1, x1, x0
  359. dmb sy
  360. bl __inval_dcache_area
  361. ret x28
  362. ENDPROC(__create_page_tables)
  363. .ltorg
  364. /*
  365. * The following fragment of code is executed with the MMU enabled.
  366. *
  367. * x0 = __PHYS_OFFSET
  368. */
  369. __primary_switched:
  370. adrp x4, init_thread_union
  371. add sp, x4, #THREAD_SIZE
  372. adr_l x5, init_task
  373. msr sp_el0, x5 // Save thread_info
  374. adr_l x8, vectors // load VBAR_EL1 with virtual
  375. msr vbar_el1, x8 // vector table address
  376. isb
  377. stp xzr, x30, [sp, #-16]!
  378. mov x29, sp
  379. str_l x21, __fdt_pointer, x5 // Save FDT pointer
  380. ldr_l x4, kimage_vaddr // Save the offset between
  381. sub x4, x4, x0 // the kernel virtual and
  382. str_l x4, kimage_voffset, x5 // physical mappings
  383. // Clear BSS
  384. adr_l x0, __bss_start
  385. mov x1, xzr
  386. adr_l x2, __bss_stop
  387. sub x2, x2, x0
  388. bl __pi_memset
  389. dsb ishst // Make zero page visible to PTW
  390. #ifdef CONFIG_KASAN
  391. bl kasan_early_init
  392. #endif
  393. #ifdef CONFIG_RANDOMIZE_BASE
  394. tst x23, ~(MIN_KIMG_ALIGN - 1) // already running randomized?
  395. b.ne 0f
  396. mov x0, x21 // pass FDT address in x0
  397. bl kaslr_early_init // parse FDT for KASLR options
  398. cbz x0, 0f // KASLR disabled? just proceed
  399. orr x23, x23, x0 // record KASLR offset
  400. ldp x29, x30, [sp], #16 // we must enable KASLR, return
  401. ret // to __primary_switch()
  402. 0:
  403. #endif
  404. add sp, sp, #16
  405. mov x29, #0
  406. mov x30, #0
  407. b start_kernel
  408. ENDPROC(__primary_switched)
  409. /*
  410. * end early head section, begin head code that is also used for
  411. * hotplug and needs to have the same protections as the text region
  412. */
  413. .section ".idmap.text","awx"
  414. ENTRY(kimage_vaddr)
  415. .quad _text - TEXT_OFFSET
  416. /*
  417. * If we're fortunate enough to boot at EL2, ensure that the world is
  418. * sane before dropping to EL1.
  419. *
  420. * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in w0 if
  421. * booted in EL1 or EL2 respectively.
  422. */
  423. ENTRY(el2_setup)
  424. msr SPsel, #1 // We want to use SP_EL{1,2}
  425. mrs x0, CurrentEL
  426. cmp x0, #CurrentEL_EL2
  427. b.eq 1f
  428. mov_q x0, (SCTLR_EL1_RES1 | ENDIAN_SET_EL1)
  429. msr sctlr_el1, x0
  430. mov w0, #BOOT_CPU_MODE_EL1 // This cpu booted in EL1
  431. isb
  432. ret
  433. 1: mov_q x0, (SCTLR_EL2_RES1 | ENDIAN_SET_EL2)
  434. msr sctlr_el2, x0
  435. #ifdef CONFIG_ARM64_VHE
  436. /*
  437. * Check for VHE being present. For the rest of the EL2 setup,
  438. * x2 being non-zero indicates that we do have VHE, and that the
  439. * kernel is intended to run at EL2.
  440. */
  441. mrs x2, id_aa64mmfr1_el1
  442. ubfx x2, x2, #8, #4
  443. #else
  444. mov x2, xzr
  445. #endif
  446. /* Hyp configuration. */
  447. mov_q x0, HCR_HOST_NVHE_FLAGS
  448. cbz x2, set_hcr
  449. mov_q x0, HCR_HOST_VHE_FLAGS
  450. set_hcr:
  451. msr hcr_el2, x0
  452. isb
  453. /*
  454. * Allow Non-secure EL1 and EL0 to access physical timer and counter.
  455. * This is not necessary for VHE, since the host kernel runs in EL2,
  456. * and EL0 accesses are configured in the later stage of boot process.
  457. * Note that when HCR_EL2.E2H == 1, CNTHCTL_EL2 has the same bit layout
  458. * as CNTKCTL_EL1, and CNTKCTL_EL1 accessing instructions are redefined
  459. * to access CNTHCTL_EL2. This allows the kernel designed to run at EL1
  460. * to transparently mess with the EL0 bits via CNTKCTL_EL1 access in
  461. * EL2.
  462. */
  463. cbnz x2, 1f
  464. mrs x0, cnthctl_el2
  465. orr x0, x0, #3 // Enable EL1 physical timers
  466. msr cnthctl_el2, x0
  467. 1:
  468. msr cntvoff_el2, xzr // Clear virtual offset
  469. #ifdef CONFIG_ARM_GIC_V3
  470. /* GICv3 system register access */
  471. mrs x0, id_aa64pfr0_el1
  472. ubfx x0, x0, #24, #4
  473. cbz x0, 3f
  474. mrs_s x0, SYS_ICC_SRE_EL2
  475. orr x0, x0, #ICC_SRE_EL2_SRE // Set ICC_SRE_EL2.SRE==1
  476. orr x0, x0, #ICC_SRE_EL2_ENABLE // Set ICC_SRE_EL2.Enable==1
  477. msr_s SYS_ICC_SRE_EL2, x0
  478. isb // Make sure SRE is now set
  479. mrs_s x0, SYS_ICC_SRE_EL2 // Read SRE back,
  480. tbz x0, #0, 3f // and check that it sticks
  481. msr_s SYS_ICH_HCR_EL2, xzr // Reset ICC_HCR_EL2 to defaults
  482. 3:
  483. #endif
  484. /* Populate ID registers. */
  485. mrs x0, midr_el1
  486. mrs x1, mpidr_el1
  487. msr vpidr_el2, x0
  488. msr vmpidr_el2, x1
  489. #ifdef CONFIG_COMPAT
  490. msr hstr_el2, xzr // Disable CP15 traps to EL2
  491. #endif
  492. /* EL2 debug */
  493. mrs x1, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
  494. sbfx x0, x1, #8, #4
  495. cmp x0, #1
  496. b.lt 4f // Skip if no PMU present
  497. mrs x0, pmcr_el0 // Disable debug access traps
  498. ubfx x0, x0, #11, #5 // to EL2 and allow access to
  499. 4:
  500. csel x3, xzr, x0, lt // all PMU counters from EL1
  501. /* Statistical profiling */
  502. ubfx x0, x1, #32, #4 // Check ID_AA64DFR0_EL1 PMSVer
  503. cbz x0, 7f // Skip if SPE not present
  504. cbnz x2, 6f // VHE?
  505. mrs_s x4, SYS_PMBIDR_EL1 // If SPE available at EL2,
  506. and x4, x4, #(1 << SYS_PMBIDR_EL1_P_SHIFT)
  507. cbnz x4, 5f // then permit sampling of physical
  508. mov x4, #(1 << SYS_PMSCR_EL2_PCT_SHIFT | \
  509. 1 << SYS_PMSCR_EL2_PA_SHIFT)
  510. msr_s SYS_PMSCR_EL2, x4 // addresses and physical counter
  511. 5:
  512. mov x1, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
  513. orr x3, x3, x1 // If we don't have VHE, then
  514. b 7f // use EL1&0 translation.
  515. 6: // For VHE, use EL2 translation
  516. orr x3, x3, #MDCR_EL2_TPMS // and disable access from EL1
  517. 7:
  518. msr mdcr_el2, x3 // Configure debug traps
  519. /* LORegions */
  520. mrs x1, id_aa64mmfr1_el1
  521. ubfx x0, x1, #ID_AA64MMFR1_LOR_SHIFT, 4
  522. cbz x0, 1f
  523. msr_s SYS_LORC_EL1, xzr
  524. 1:
  525. /* Stage-2 translation */
  526. msr vttbr_el2, xzr
  527. cbz x2, install_el2_stub
  528. mov w0, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
  529. isb
  530. ret
  531. install_el2_stub:
  532. /*
  533. * When VHE is not in use, early init of EL2 and EL1 needs to be
  534. * done here.
  535. * When VHE _is_ in use, EL1 will not be used in the host and
  536. * requires no configuration, and all non-hyp-specific EL2 setup
  537. * will be done via the _EL1 system register aliases in __cpu_setup.
  538. */
  539. mov_q x0, (SCTLR_EL1_RES1 | ENDIAN_SET_EL1)
  540. msr sctlr_el1, x0
  541. /* Coprocessor traps. */
  542. mov x0, #0x33ff
  543. msr cptr_el2, x0 // Disable copro. traps to EL2
  544. /* SVE register access */
  545. mrs x1, id_aa64pfr0_el1
  546. ubfx x1, x1, #ID_AA64PFR0_SVE_SHIFT, #4
  547. cbz x1, 7f
  548. bic x0, x0, #CPTR_EL2_TZ // Also disable SVE traps
  549. msr cptr_el2, x0 // Disable copro. traps to EL2
  550. isb
  551. mov x1, #ZCR_ELx_LEN_MASK // SVE: Enable full vector
  552. msr_s SYS_ZCR_EL2, x1 // length for EL1.
  553. /* Hypervisor stub */
  554. 7: adr_l x0, __hyp_stub_vectors
  555. msr vbar_el2, x0
  556. /* spsr */
  557. mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  558. PSR_MODE_EL1h)
  559. msr spsr_el2, x0
  560. msr elr_el2, lr
  561. mov w0, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
  562. eret
  563. ENDPROC(el2_setup)
  564. /*
  565. * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed
  566. * in w0. See arch/arm64/include/asm/virt.h for more info.
  567. */
  568. set_cpu_boot_mode_flag:
  569. adr_l x1, __boot_cpu_mode
  570. cmp w0, #BOOT_CPU_MODE_EL2
  571. b.ne 1f
  572. add x1, x1, #4
  573. 1: str w0, [x1] // This CPU has booted in EL1
  574. dmb sy
  575. dc ivac, x1 // Invalidate potentially stale cache line
  576. ret
  577. ENDPROC(set_cpu_boot_mode_flag)
  578. /*
  579. * These values are written with the MMU off, but read with the MMU on.
  580. * Writers will invalidate the corresponding address, discarding up to a
  581. * 'Cache Writeback Granule' (CWG) worth of data. The linker script ensures
  582. * sufficient alignment that the CWG doesn't overlap another section.
  583. */
  584. .pushsection ".mmuoff.data.write", "aw"
  585. /*
  586. * We need to find out the CPU boot mode long after boot, so we need to
  587. * store it in a writable variable.
  588. *
  589. * This is not in .bss, because we set it sufficiently early that the boot-time
  590. * zeroing of .bss would clobber it.
  591. */
  592. ENTRY(__boot_cpu_mode)
  593. .long BOOT_CPU_MODE_EL2
  594. .long BOOT_CPU_MODE_EL1
  595. /*
  596. * The booting CPU updates the failed status @__early_cpu_boot_status,
  597. * with MMU turned off.
  598. */
  599. ENTRY(__early_cpu_boot_status)
  600. .quad 0
  601. .popsection
  602. /*
  603. * This provides a "holding pen" for platforms to hold all secondary
  604. * cores are held until we're ready for them to initialise.
  605. */
  606. ENTRY(secondary_holding_pen)
  607. bl el2_setup // Drop to EL1, w0=cpu_boot_mode
  608. bl set_cpu_boot_mode_flag
  609. mrs x0, mpidr_el1
  610. mov_q x1, MPIDR_HWID_BITMASK
  611. and x0, x0, x1
  612. adr_l x3, secondary_holding_pen_release
  613. pen: ldr x4, [x3]
  614. cmp x4, x0
  615. b.eq secondary_startup
  616. wfe
  617. b pen
  618. ENDPROC(secondary_holding_pen)
  619. /*
  620. * Secondary entry point that jumps straight into the kernel. Only to
  621. * be used where CPUs are brought online dynamically by the kernel.
  622. */
  623. ENTRY(secondary_entry)
  624. bl el2_setup // Drop to EL1
  625. bl set_cpu_boot_mode_flag
  626. b secondary_startup
  627. ENDPROC(secondary_entry)
  628. secondary_startup:
  629. /*
  630. * Common entry point for secondary CPUs.
  631. */
  632. bl __cpu_secondary_check52bitva
  633. bl __cpu_setup // initialise processor
  634. bl __enable_mmu
  635. ldr x8, =__secondary_switched
  636. br x8
  637. ENDPROC(secondary_startup)
  638. __secondary_switched:
  639. adr_l x5, vectors
  640. msr vbar_el1, x5
  641. isb
  642. adr_l x0, secondary_data
  643. ldr x1, [x0, #CPU_BOOT_STACK] // get secondary_data.stack
  644. mov sp, x1
  645. ldr x2, [x0, #CPU_BOOT_TASK]
  646. msr sp_el0, x2
  647. mov x29, #0
  648. mov x30, #0
  649. b secondary_start_kernel
  650. ENDPROC(__secondary_switched)
  651. /*
  652. * The booting CPU updates the failed status @__early_cpu_boot_status,
  653. * with MMU turned off.
  654. *
  655. * update_early_cpu_boot_status tmp, status
  656. * - Corrupts tmp1, tmp2
  657. * - Writes 'status' to __early_cpu_boot_status and makes sure
  658. * it is committed to memory.
  659. */
  660. .macro update_early_cpu_boot_status status, tmp1, tmp2
  661. mov \tmp2, #\status
  662. adr_l \tmp1, __early_cpu_boot_status
  663. str \tmp2, [\tmp1]
  664. dmb sy
  665. dc ivac, \tmp1 // Invalidate potentially stale cache line
  666. .endm
  667. /*
  668. * Enable the MMU.
  669. *
  670. * x0 = SCTLR_EL1 value for turning on the MMU.
  671. *
  672. * Returns to the caller via x30/lr. This requires the caller to be covered
  673. * by the .idmap.text section.
  674. *
  675. * Checks if the selected granule size is supported by the CPU.
  676. * If it isn't, park the CPU
  677. */
  678. ENTRY(__enable_mmu)
  679. mrs x1, ID_AA64MMFR0_EL1
  680. ubfx x2, x1, #ID_AA64MMFR0_TGRAN_SHIFT, 4
  681. cmp x2, #ID_AA64MMFR0_TGRAN_SUPPORTED
  682. b.ne __no_granule_support
  683. update_early_cpu_boot_status 0, x1, x2
  684. adrp x1, idmap_pg_dir
  685. adrp x2, swapper_pg_dir
  686. phys_to_ttbr x3, x1
  687. phys_to_ttbr x4, x2
  688. msr ttbr0_el1, x3 // load TTBR0
  689. msr ttbr1_el1, x4 // load TTBR1
  690. isb
  691. msr sctlr_el1, x0
  692. isb
  693. /*
  694. * Invalidate the local I-cache so that any instructions fetched
  695. * speculatively from the PoC are discarded, since they may have
  696. * been dynamically patched at the PoU.
  697. */
  698. ic iallu
  699. dsb nsh
  700. isb
  701. ret
  702. ENDPROC(__enable_mmu)
  703. ENTRY(__cpu_secondary_check52bitva)
  704. #ifdef CONFIG_ARM64_52BIT_VA
  705. ldr_l x0, vabits_user
  706. cmp x0, #52
  707. b.ne 2f
  708. mrs_s x0, SYS_ID_AA64MMFR2_EL1
  709. and x0, x0, #(0xf << ID_AA64MMFR2_LVA_SHIFT)
  710. cbnz x0, 2f
  711. adr_l x0, va52mismatch
  712. mov w1, #1
  713. strb w1, [x0]
  714. dmb sy
  715. dc ivac, x0 // Invalidate potentially stale cache line
  716. update_early_cpu_boot_status CPU_STUCK_IN_KERNEL, x0, x1
  717. 1: wfe
  718. wfi
  719. b 1b
  720. #endif
  721. 2: ret
  722. ENDPROC(__cpu_secondary_check52bitva)
  723. __no_granule_support:
  724. /* Indicate that this CPU can't boot and is stuck in the kernel */
  725. update_early_cpu_boot_status CPU_STUCK_IN_KERNEL, x1, x2
  726. 1:
  727. wfe
  728. wfi
  729. b 1b
  730. ENDPROC(__no_granule_support)
  731. #ifdef CONFIG_RELOCATABLE
  732. __relocate_kernel:
  733. /*
  734. * Iterate over each entry in the relocation table, and apply the
  735. * relocations in place.
  736. */
  737. ldr w9, =__rela_offset // offset to reloc table
  738. ldr w10, =__rela_size // size of reloc table
  739. mov_q x11, KIMAGE_VADDR // default virtual offset
  740. add x11, x11, x23 // actual virtual offset
  741. add x9, x9, x11 // __va(.rela)
  742. add x10, x9, x10 // __va(.rela) + sizeof(.rela)
  743. 0: cmp x9, x10
  744. b.hs 1f
  745. ldp x11, x12, [x9], #24
  746. ldr x13, [x9, #-8]
  747. cmp w12, #R_AARCH64_RELATIVE
  748. b.ne 0b
  749. add x13, x13, x23 // relocate
  750. str x13, [x11, x23]
  751. b 0b
  752. 1: ret
  753. ENDPROC(__relocate_kernel)
  754. #endif
  755. __primary_switch:
  756. #ifdef CONFIG_RANDOMIZE_BASE
  757. mov x19, x0 // preserve new SCTLR_EL1 value
  758. mrs x20, sctlr_el1 // preserve old SCTLR_EL1 value
  759. #endif
  760. bl __enable_mmu
  761. #ifdef CONFIG_RELOCATABLE
  762. bl __relocate_kernel
  763. #ifdef CONFIG_RANDOMIZE_BASE
  764. ldr x8, =__primary_switched
  765. adrp x0, __PHYS_OFFSET
  766. blr x8
  767. /*
  768. * If we return here, we have a KASLR displacement in x23 which we need
  769. * to take into account by discarding the current kernel mapping and
  770. * creating a new one.
  771. */
  772. pre_disable_mmu_workaround
  773. msr sctlr_el1, x20 // disable the MMU
  774. isb
  775. bl __create_page_tables // recreate kernel mapping
  776. tlbi vmalle1 // Remove any stale TLB entries
  777. dsb nsh
  778. isb
  779. msr sctlr_el1, x19 // re-enable the MMU
  780. isb
  781. ic iallu // flush instructions fetched
  782. dsb nsh // via old mapping
  783. isb
  784. bl __relocate_kernel
  785. #endif
  786. #endif
  787. ldr x8, =__primary_switched
  788. adrp x0, __PHYS_OFFSET
  789. br x8
  790. ENDPROC(__primary_switch)