head.S 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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/ptrace.h>
  27. #include <asm/asm-offsets.h>
  28. #include <asm/cache.h>
  29. #include <asm/cputype.h>
  30. #include <asm/elf.h>
  31. #include <asm/kernel-pgtable.h>
  32. #include <asm/kvm_arm.h>
  33. #include <asm/memory.h>
  34. #include <asm/pgtable-hwdef.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/page.h>
  37. #include <asm/smp.h>
  38. #include <asm/sysreg.h>
  39. #include <asm/thread_info.h>
  40. #include <asm/virt.h>
  41. #define __PHYS_OFFSET (KERNEL_START - TEXT_OFFSET)
  42. #if (TEXT_OFFSET & 0xfff) != 0
  43. #error TEXT_OFFSET must be at least 4KB aligned
  44. #elif (PAGE_OFFSET & 0x1fffff) != 0
  45. #error PAGE_OFFSET must be at least 2MB aligned
  46. #elif TEXT_OFFSET > 0x1fffff
  47. #error TEXT_OFFSET must be less than 2MB
  48. #endif
  49. #define KERNEL_START _text
  50. #define KERNEL_END _end
  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. .byte 0x41 // Magic number, "ARM\x64"
  89. .byte 0x52
  90. .byte 0x4d
  91. .byte 0x64
  92. #ifdef CONFIG_EFI
  93. .long pe_header - _head // Offset to the PE header.
  94. #else
  95. .word 0 // reserved
  96. #endif
  97. #ifdef CONFIG_EFI
  98. .globl __efistub_stext_offset
  99. .set __efistub_stext_offset, stext - _head
  100. .align 3
  101. pe_header:
  102. .ascii "PE"
  103. .short 0
  104. coff_header:
  105. .short 0xaa64 // AArch64
  106. .short 2 // nr_sections
  107. .long 0 // TimeDateStamp
  108. .long 0 // PointerToSymbolTable
  109. .long 1 // NumberOfSymbols
  110. .short section_table - optional_header // SizeOfOptionalHeader
  111. .short 0x206 // Characteristics.
  112. // IMAGE_FILE_DEBUG_STRIPPED |
  113. // IMAGE_FILE_EXECUTABLE_IMAGE |
  114. // IMAGE_FILE_LINE_NUMS_STRIPPED
  115. optional_header:
  116. .short 0x20b // PE32+ format
  117. .byte 0x02 // MajorLinkerVersion
  118. .byte 0x14 // MinorLinkerVersion
  119. .long _end - stext // SizeOfCode
  120. .long 0 // SizeOfInitializedData
  121. .long 0 // SizeOfUninitializedData
  122. .long __efistub_entry - _head // AddressOfEntryPoint
  123. .long __efistub_stext_offset // BaseOfCode
  124. extra_header_fields:
  125. .quad 0 // ImageBase
  126. .long 0x1000 // SectionAlignment
  127. .long PECOFF_FILE_ALIGNMENT // FileAlignment
  128. .short 0 // MajorOperatingSystemVersion
  129. .short 0 // MinorOperatingSystemVersion
  130. .short 0 // MajorImageVersion
  131. .short 0 // MinorImageVersion
  132. .short 0 // MajorSubsystemVersion
  133. .short 0 // MinorSubsystemVersion
  134. .long 0 // Win32VersionValue
  135. .long _end - _head // SizeOfImage
  136. // Everything before the kernel image is considered part of the header
  137. .long __efistub_stext_offset // SizeOfHeaders
  138. .long 0 // CheckSum
  139. .short 0xa // Subsystem (EFI application)
  140. .short 0 // DllCharacteristics
  141. .quad 0 // SizeOfStackReserve
  142. .quad 0 // SizeOfStackCommit
  143. .quad 0 // SizeOfHeapReserve
  144. .quad 0 // SizeOfHeapCommit
  145. .long 0 // LoaderFlags
  146. .long 0x6 // NumberOfRvaAndSizes
  147. .quad 0 // ExportTable
  148. .quad 0 // ImportTable
  149. .quad 0 // ResourceTable
  150. .quad 0 // ExceptionTable
  151. .quad 0 // CertificationTable
  152. .quad 0 // BaseRelocationTable
  153. // Section table
  154. section_table:
  155. /*
  156. * The EFI application loader requires a relocation section
  157. * because EFI applications must be relocatable. This is a
  158. * dummy section as far as we are concerned.
  159. */
  160. .ascii ".reloc"
  161. .byte 0
  162. .byte 0 // end of 0 padding of section name
  163. .long 0
  164. .long 0
  165. .long 0 // SizeOfRawData
  166. .long 0 // PointerToRawData
  167. .long 0 // PointerToRelocations
  168. .long 0 // PointerToLineNumbers
  169. .short 0 // NumberOfRelocations
  170. .short 0 // NumberOfLineNumbers
  171. .long 0x42100040 // Characteristics (section flags)
  172. .ascii ".text"
  173. .byte 0
  174. .byte 0
  175. .byte 0 // end of 0 padding of section name
  176. .long _end - stext // VirtualSize
  177. .long __efistub_stext_offset // VirtualAddress
  178. .long _edata - stext // SizeOfRawData
  179. .long __efistub_stext_offset // PointerToRawData
  180. .long 0 // PointerToRelocations (0 for executables)
  181. .long 0 // PointerToLineNumbers (0 for executables)
  182. .short 0 // NumberOfRelocations (0 for executables)
  183. .short 0 // NumberOfLineNumbers (0 for executables)
  184. .long 0xe0500020 // Characteristics (section flags)
  185. /*
  186. * EFI will load stext onwards at the 4k section alignment
  187. * described in the PE/COFF header. To ensure that instruction
  188. * sequences using an adrp and a :lo12: immediate will function
  189. * correctly at this alignment, we must ensure that stext is
  190. * placed at a 4k boundary in the Image to begin with.
  191. */
  192. .align 12
  193. #endif
  194. ENTRY(stext)
  195. bl preserve_boot_args
  196. bl el2_setup // Drop to EL1, w20=cpu_boot_mode
  197. mov x23, xzr // KASLR offset, defaults to 0
  198. adrp x24, __PHYS_OFFSET
  199. bl set_cpu_boot_mode_flag
  200. bl __create_page_tables // x25=TTBR0, x26=TTBR1
  201. /*
  202. * The following calls CPU setup code, see arch/arm64/mm/proc.S for
  203. * details.
  204. * On return, the CPU will be ready for the MMU to be turned on and
  205. * the TCR will have been set.
  206. */
  207. ldr x27, 0f // address to jump to after
  208. // MMU has been enabled
  209. adr_l lr, __enable_mmu // return (PIC) address
  210. b __cpu_setup // initialise processor
  211. ENDPROC(stext)
  212. .align 3
  213. 0: .quad __mmap_switched - (_head - TEXT_OFFSET) + KIMAGE_VADDR
  214. /*
  215. * Preserve the arguments passed by the bootloader in x0 .. x3
  216. */
  217. preserve_boot_args:
  218. mov x21, x0 // x21=FDT
  219. adr_l x0, boot_args // record the contents of
  220. stp x21, x1, [x0] // x0 .. x3 at kernel entry
  221. stp x2, x3, [x0, #16]
  222. dmb sy // needed before dc ivac with
  223. // MMU off
  224. add x1, x0, #0x20 // 4 x 8 bytes
  225. b __inval_cache_range // tail call
  226. ENDPROC(preserve_boot_args)
  227. /*
  228. * Macro to create a table entry to the next page.
  229. *
  230. * tbl: page table address
  231. * virt: virtual address
  232. * shift: #imm page table shift
  233. * ptrs: #imm pointers per table page
  234. *
  235. * Preserves: virt
  236. * Corrupts: tmp1, tmp2
  237. * Returns: tbl -> next level table page address
  238. */
  239. .macro create_table_entry, tbl, virt, shift, ptrs, tmp1, tmp2
  240. lsr \tmp1, \virt, #\shift
  241. and \tmp1, \tmp1, #\ptrs - 1 // table index
  242. add \tmp2, \tbl, #PAGE_SIZE
  243. orr \tmp2, \tmp2, #PMD_TYPE_TABLE // address of next table and entry type
  244. str \tmp2, [\tbl, \tmp1, lsl #3]
  245. add \tbl, \tbl, #PAGE_SIZE // next level table page
  246. .endm
  247. /*
  248. * Macro to populate the PGD (and possibily PUD) for the corresponding
  249. * block entry in the next level (tbl) for the given virtual address.
  250. *
  251. * Preserves: tbl, next, virt
  252. * Corrupts: tmp1, tmp2
  253. */
  254. .macro create_pgd_entry, tbl, virt, tmp1, tmp2
  255. create_table_entry \tbl, \virt, PGDIR_SHIFT, PTRS_PER_PGD, \tmp1, \tmp2
  256. #if SWAPPER_PGTABLE_LEVELS > 3
  257. create_table_entry \tbl, \virt, PUD_SHIFT, PTRS_PER_PUD, \tmp1, \tmp2
  258. #endif
  259. #if SWAPPER_PGTABLE_LEVELS > 2
  260. create_table_entry \tbl, \virt, SWAPPER_TABLE_SHIFT, PTRS_PER_PTE, \tmp1, \tmp2
  261. #endif
  262. .endm
  263. /*
  264. * Macro to populate block entries in the page table for the start..end
  265. * virtual range (inclusive).
  266. *
  267. * Preserves: tbl, flags
  268. * Corrupts: phys, start, end, pstate
  269. */
  270. .macro create_block_map, tbl, flags, phys, start, end
  271. lsr \phys, \phys, #SWAPPER_BLOCK_SHIFT
  272. lsr \start, \start, #SWAPPER_BLOCK_SHIFT
  273. and \start, \start, #PTRS_PER_PTE - 1 // table index
  274. orr \phys, \flags, \phys, lsl #SWAPPER_BLOCK_SHIFT // table entry
  275. lsr \end, \end, #SWAPPER_BLOCK_SHIFT
  276. and \end, \end, #PTRS_PER_PTE - 1 // table end index
  277. 9999: str \phys, [\tbl, \start, lsl #3] // store the entry
  278. add \start, \start, #1 // next entry
  279. add \phys, \phys, #SWAPPER_BLOCK_SIZE // next block
  280. cmp \start, \end
  281. b.ls 9999b
  282. .endm
  283. /*
  284. * Setup the initial page tables. We only setup the barest amount which is
  285. * required to get the kernel running. The following sections are required:
  286. * - identity mapping to enable the MMU (low address, TTBR0)
  287. * - first few MB of the kernel linear mapping to jump to once the MMU has
  288. * been enabled
  289. */
  290. __create_page_tables:
  291. adrp x25, idmap_pg_dir
  292. adrp x26, swapper_pg_dir
  293. mov x28, lr
  294. /*
  295. * Invalidate the idmap and swapper page tables to avoid potential
  296. * dirty cache lines being evicted.
  297. */
  298. mov x0, x25
  299. add x1, x26, #SWAPPER_DIR_SIZE
  300. bl __inval_cache_range
  301. /*
  302. * Clear the idmap and swapper page tables.
  303. */
  304. mov x0, x25
  305. add x6, x26, #SWAPPER_DIR_SIZE
  306. 1: stp xzr, xzr, [x0], #16
  307. stp xzr, xzr, [x0], #16
  308. stp xzr, xzr, [x0], #16
  309. stp xzr, xzr, [x0], #16
  310. cmp x0, x6
  311. b.lo 1b
  312. ldr x7, =SWAPPER_MM_MMUFLAGS
  313. /*
  314. * Create the identity mapping.
  315. */
  316. mov x0, x25 // idmap_pg_dir
  317. adrp x3, __idmap_text_start // __pa(__idmap_text_start)
  318. #ifndef CONFIG_ARM64_VA_BITS_48
  319. #define EXTRA_SHIFT (PGDIR_SHIFT + PAGE_SHIFT - 3)
  320. #define EXTRA_PTRS (1 << (48 - EXTRA_SHIFT))
  321. /*
  322. * If VA_BITS < 48, it may be too small to allow for an ID mapping to be
  323. * created that covers system RAM if that is located sufficiently high
  324. * in the physical address space. So for the ID map, use an extended
  325. * virtual range in that case, by configuring an additional translation
  326. * level.
  327. * First, we have to verify our assumption that the current value of
  328. * VA_BITS was chosen such that all translation levels are fully
  329. * utilised, and that lowering T0SZ will always result in an additional
  330. * translation level to be configured.
  331. */
  332. #if VA_BITS != EXTRA_SHIFT
  333. #error "Mismatch between VA_BITS and page size/number of translation levels"
  334. #endif
  335. /*
  336. * Calculate the maximum allowed value for TCR_EL1.T0SZ so that the
  337. * entire ID map region can be mapped. As T0SZ == (64 - #bits used),
  338. * this number conveniently equals the number of leading zeroes in
  339. * the physical address of __idmap_text_end.
  340. */
  341. adrp x5, __idmap_text_end
  342. clz x5, x5
  343. cmp x5, TCR_T0SZ(VA_BITS) // default T0SZ small enough?
  344. b.ge 1f // .. then skip additional level
  345. adr_l x6, idmap_t0sz
  346. str x5, [x6]
  347. dmb sy
  348. dc ivac, x6 // Invalidate potentially stale cache line
  349. create_table_entry x0, x3, EXTRA_SHIFT, EXTRA_PTRS, x5, x6
  350. 1:
  351. #endif
  352. create_pgd_entry x0, x3, x5, x6
  353. mov x5, x3 // __pa(__idmap_text_start)
  354. adr_l x6, __idmap_text_end // __pa(__idmap_text_end)
  355. create_block_map x0, x7, x3, x5, x6
  356. /*
  357. * Map the kernel image (starting with PHYS_OFFSET).
  358. */
  359. mov x0, x26 // swapper_pg_dir
  360. ldr x5, =KIMAGE_VADDR
  361. add x5, x5, x23 // add KASLR displacement
  362. create_pgd_entry x0, x5, x3, x6
  363. ldr w6, kernel_img_size
  364. add x6, x6, x5
  365. mov x3, x24 // phys offset
  366. create_block_map x0, x7, x3, x5, x6
  367. /*
  368. * Since the page tables have been populated with non-cacheable
  369. * accesses (MMU disabled), invalidate the idmap and swapper page
  370. * tables again to remove any speculatively loaded cache lines.
  371. */
  372. mov x0, x25
  373. add x1, x26, #SWAPPER_DIR_SIZE
  374. dmb sy
  375. bl __inval_cache_range
  376. ret x28
  377. ENDPROC(__create_page_tables)
  378. kernel_img_size:
  379. .long _end - (_head - TEXT_OFFSET)
  380. .ltorg
  381. /*
  382. * The following fragment of code is executed with the MMU enabled.
  383. */
  384. .set initial_sp, init_thread_union + THREAD_START_SP
  385. __mmap_switched:
  386. mov x28, lr // preserve LR
  387. adr_l x8, vectors // load VBAR_EL1 with virtual
  388. msr vbar_el1, x8 // vector table address
  389. isb
  390. // Clear BSS
  391. adr_l x0, __bss_start
  392. mov x1, xzr
  393. adr_l x2, __bss_stop
  394. sub x2, x2, x0
  395. bl __pi_memset
  396. dsb ishst // Make zero page visible to PTW
  397. #ifdef CONFIG_RELOCATABLE
  398. /*
  399. * Iterate over each entry in the relocation table, and apply the
  400. * relocations in place.
  401. */
  402. adr_l x8, __dynsym_start // start of symbol table
  403. adr_l x9, __reloc_start // start of reloc table
  404. adr_l x10, __reloc_end // end of reloc table
  405. 0: cmp x9, x10
  406. b.hs 2f
  407. ldp x11, x12, [x9], #24
  408. ldr x13, [x9, #-8]
  409. cmp w12, #R_AARCH64_RELATIVE
  410. b.ne 1f
  411. add x13, x13, x23 // relocate
  412. str x13, [x11, x23]
  413. b 0b
  414. 1: cmp w12, #R_AARCH64_ABS64
  415. b.ne 0b
  416. add x12, x12, x12, lsl #1 // symtab offset: 24x top word
  417. add x12, x8, x12, lsr #(32 - 3) // ... shifted into bottom word
  418. ldrsh w14, [x12, #6] // Elf64_Sym::st_shndx
  419. ldr x15, [x12, #8] // Elf64_Sym::st_value
  420. cmp w14, #-0xf // SHN_ABS (0xfff1) ?
  421. add x14, x15, x23 // relocate
  422. csel x15, x14, x15, ne
  423. add x15, x13, x15
  424. str x15, [x11, x23]
  425. b 0b
  426. 2: adr_l x8, kimage_vaddr // make relocated kimage_vaddr
  427. dc cvac, x8 // value visible to secondaries
  428. dsb sy // with MMU off
  429. #endif
  430. adr_l sp, initial_sp, x4
  431. mov x4, sp
  432. and x4, x4, #~(THREAD_SIZE - 1)
  433. msr sp_el0, x4 // Save thread_info
  434. str_l x21, __fdt_pointer, x5 // Save FDT pointer
  435. ldr_l x4, kimage_vaddr // Save the offset between
  436. sub x4, x4, x24 // the kernel virtual and
  437. str_l x4, kimage_voffset, x5 // physical mappings
  438. mov x29, #0
  439. #ifdef CONFIG_KASAN
  440. bl kasan_early_init
  441. #endif
  442. #ifdef CONFIG_RANDOMIZE_BASE
  443. cbnz x23, 0f // already running randomized?
  444. mov x0, x21 // pass FDT address in x0
  445. bl kaslr_early_init // parse FDT for KASLR options
  446. cbz x0, 0f // KASLR disabled? just proceed
  447. mov x23, x0 // record KASLR offset
  448. ret x28 // we must enable KASLR, return
  449. // to __enable_mmu()
  450. 0:
  451. #endif
  452. b start_kernel
  453. ENDPROC(__mmap_switched)
  454. /*
  455. * end early head section, begin head code that is also used for
  456. * hotplug and needs to have the same protections as the text region
  457. */
  458. .section ".text","ax"
  459. ENTRY(kimage_vaddr)
  460. .quad _text - TEXT_OFFSET
  461. /*
  462. * If we're fortunate enough to boot at EL2, ensure that the world is
  463. * sane before dropping to EL1.
  464. *
  465. * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in x20 if
  466. * booted in EL1 or EL2 respectively.
  467. */
  468. ENTRY(el2_setup)
  469. mrs x0, CurrentEL
  470. cmp x0, #CurrentEL_EL2
  471. b.ne 1f
  472. mrs x0, sctlr_el2
  473. CPU_BE( orr x0, x0, #(1 << 25) ) // Set the EE bit for EL2
  474. CPU_LE( bic x0, x0, #(1 << 25) ) // Clear the EE bit for EL2
  475. msr sctlr_el2, x0
  476. b 2f
  477. 1: mrs x0, sctlr_el1
  478. CPU_BE( orr x0, x0, #(3 << 24) ) // Set the EE and E0E bits for EL1
  479. CPU_LE( bic x0, x0, #(3 << 24) ) // Clear the EE and E0E bits for EL1
  480. msr sctlr_el1, x0
  481. mov w20, #BOOT_CPU_MODE_EL1 // This cpu booted in EL1
  482. isb
  483. ret
  484. 2:
  485. #ifdef CONFIG_ARM64_VHE
  486. /*
  487. * Check for VHE being present. For the rest of the EL2 setup,
  488. * x2 being non-zero indicates that we do have VHE, and that the
  489. * kernel is intended to run at EL2.
  490. */
  491. mrs x2, id_aa64mmfr1_el1
  492. ubfx x2, x2, #8, #4
  493. #else
  494. mov x2, xzr
  495. #endif
  496. /* Hyp configuration. */
  497. mov x0, #HCR_RW // 64-bit EL1
  498. cbz x2, set_hcr
  499. orr x0, x0, #HCR_TGE // Enable Host Extensions
  500. orr x0, x0, #HCR_E2H
  501. set_hcr:
  502. msr hcr_el2, x0
  503. isb
  504. /* Generic timers. */
  505. mrs x0, cnthctl_el2
  506. orr x0, x0, #3 // Enable EL1 physical timers
  507. msr cnthctl_el2, x0
  508. msr cntvoff_el2, xzr // Clear virtual offset
  509. #ifdef CONFIG_ARM_GIC_V3
  510. /* GICv3 system register access */
  511. mrs x0, id_aa64pfr0_el1
  512. ubfx x0, x0, #24, #4
  513. cmp x0, #1
  514. b.ne 3f
  515. mrs_s x0, ICC_SRE_EL2
  516. orr x0, x0, #ICC_SRE_EL2_SRE // Set ICC_SRE_EL2.SRE==1
  517. orr x0, x0, #ICC_SRE_EL2_ENABLE // Set ICC_SRE_EL2.Enable==1
  518. msr_s ICC_SRE_EL2, x0
  519. isb // Make sure SRE is now set
  520. mrs_s x0, ICC_SRE_EL2 // Read SRE back,
  521. tbz x0, #0, 3f // and check that it sticks
  522. msr_s ICH_HCR_EL2, xzr // Reset ICC_HCR_EL2 to defaults
  523. 3:
  524. #endif
  525. /* Populate ID registers. */
  526. mrs x0, midr_el1
  527. mrs x1, mpidr_el1
  528. msr vpidr_el2, x0
  529. msr vmpidr_el2, x1
  530. /* sctlr_el1 */
  531. mov x0, #0x0800 // Set/clear RES{1,0} bits
  532. CPU_BE( movk x0, #0x33d0, lsl #16 ) // Set EE and E0E on BE systems
  533. CPU_LE( movk x0, #0x30d0, lsl #16 ) // Clear EE and E0E on LE systems
  534. msr sctlr_el1, x0
  535. /* Coprocessor traps. */
  536. mov x0, #0x33ff
  537. msr cptr_el2, x0 // Disable copro. traps to EL2
  538. #ifdef CONFIG_COMPAT
  539. msr hstr_el2, xzr // Disable CP15 traps to EL2
  540. #endif
  541. /* EL2 debug */
  542. mrs x0, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
  543. sbfx x0, x0, #8, #4
  544. cmp x0, #1
  545. b.lt 4f // Skip if no PMU present
  546. mrs x0, pmcr_el0 // Disable debug access traps
  547. ubfx x0, x0, #11, #5 // to EL2 and allow access to
  548. msr mdcr_el2, x0 // all PMU counters from EL1
  549. 4:
  550. /* Stage-2 translation */
  551. msr vttbr_el2, xzr
  552. cbz x2, install_el2_stub
  553. mov w20, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
  554. isb
  555. ret
  556. install_el2_stub:
  557. /* Hypervisor stub */
  558. adrp x0, __hyp_stub_vectors
  559. add x0, x0, #:lo12:__hyp_stub_vectors
  560. msr vbar_el2, x0
  561. /* spsr */
  562. mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  563. PSR_MODE_EL1h)
  564. msr spsr_el2, x0
  565. msr elr_el2, lr
  566. mov w20, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
  567. eret
  568. ENDPROC(el2_setup)
  569. /*
  570. * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed
  571. * in x20. See arch/arm64/include/asm/virt.h for more info.
  572. */
  573. ENTRY(set_cpu_boot_mode_flag)
  574. adr_l x1, __boot_cpu_mode
  575. cmp w20, #BOOT_CPU_MODE_EL2
  576. b.ne 1f
  577. add x1, x1, #4
  578. 1: str w20, [x1] // This CPU has booted in EL1
  579. dmb sy
  580. dc ivac, x1 // Invalidate potentially stale cache line
  581. ret
  582. ENDPROC(set_cpu_boot_mode_flag)
  583. /*
  584. * We need to find out the CPU boot mode long after boot, so we need to
  585. * store it in a writable variable.
  586. *
  587. * This is not in .bss, because we set it sufficiently early that the boot-time
  588. * zeroing of .bss would clobber it.
  589. */
  590. .pushsection .data..cacheline_aligned
  591. .align L1_CACHE_SHIFT
  592. ENTRY(__boot_cpu_mode)
  593. .long BOOT_CPU_MODE_EL2
  594. .long BOOT_CPU_MODE_EL1
  595. .popsection
  596. /*
  597. * This provides a "holding pen" for platforms to hold all secondary
  598. * cores are held until we're ready for them to initialise.
  599. */
  600. ENTRY(secondary_holding_pen)
  601. bl el2_setup // Drop to EL1, w20=cpu_boot_mode
  602. bl set_cpu_boot_mode_flag
  603. mrs x0, mpidr_el1
  604. ldr x1, =MPIDR_HWID_BITMASK
  605. and x0, x0, x1
  606. adr_l x3, secondary_holding_pen_release
  607. pen: ldr x4, [x3]
  608. cmp x4, x0
  609. b.eq secondary_startup
  610. wfe
  611. b pen
  612. ENDPROC(secondary_holding_pen)
  613. /*
  614. * Secondary entry point that jumps straight into the kernel. Only to
  615. * be used where CPUs are brought online dynamically by the kernel.
  616. */
  617. ENTRY(secondary_entry)
  618. bl el2_setup // Drop to EL1
  619. bl set_cpu_boot_mode_flag
  620. b secondary_startup
  621. ENDPROC(secondary_entry)
  622. ENTRY(secondary_startup)
  623. /*
  624. * Common entry point for secondary CPUs.
  625. */
  626. adrp x25, idmap_pg_dir
  627. adrp x26, swapper_pg_dir
  628. bl __cpu_setup // initialise processor
  629. ldr x8, kimage_vaddr
  630. ldr w9, 0f
  631. sub x27, x8, w9, sxtw // address to jump to after enabling the MMU
  632. b __enable_mmu
  633. ENDPROC(secondary_startup)
  634. 0: .long (_text - TEXT_OFFSET) - __secondary_switched
  635. ENTRY(__secondary_switched)
  636. adr_l x5, vectors
  637. msr vbar_el1, x5
  638. isb
  639. adr_l x0, secondary_data
  640. ldr x0, [x0, #CPU_BOOT_STACK] // get secondary_data.stack
  641. mov sp, x0
  642. and x0, x0, #~(THREAD_SIZE - 1)
  643. msr sp_el0, x0 // save thread_info
  644. mov x29, #0
  645. b secondary_start_kernel
  646. ENDPROC(__secondary_switched)
  647. /*
  648. * The booting CPU updates the failed status @__early_cpu_boot_status,
  649. * with MMU turned off.
  650. *
  651. * update_early_cpu_boot_status tmp, status
  652. * - Corrupts tmp1, tmp2
  653. * - Writes 'status' to __early_cpu_boot_status and makes sure
  654. * it is committed to memory.
  655. */
  656. .macro update_early_cpu_boot_status status, tmp1, tmp2
  657. mov \tmp2, #\status
  658. str_l \tmp2, __early_cpu_boot_status, \tmp1
  659. dmb sy
  660. dc ivac, \tmp1 // Invalidate potentially stale cache line
  661. .endm
  662. .pushsection .data..cacheline_aligned
  663. .align L1_CACHE_SHIFT
  664. ENTRY(__early_cpu_boot_status)
  665. .long 0
  666. .popsection
  667. /*
  668. * Enable the MMU.
  669. *
  670. * x0 = SCTLR_EL1 value for turning on the MMU.
  671. * x27 = *virtual* address to jump to upon completion
  672. *
  673. * Other registers depend on the function called upon completion.
  674. *
  675. * Checks if the selected granule size is supported by the CPU.
  676. * If it isn't, park the CPU
  677. */
  678. .section ".idmap.text", "ax"
  679. __enable_mmu:
  680. mrs x18, sctlr_el1 // preserve old SCTLR_EL1 value
  681. mrs x1, ID_AA64MMFR0_EL1
  682. ubfx x2, x1, #ID_AA64MMFR0_TGRAN_SHIFT, 4
  683. cmp x2, #ID_AA64MMFR0_TGRAN_SUPPORTED
  684. b.ne __no_granule_support
  685. update_early_cpu_boot_status 0, x1, x2
  686. msr ttbr0_el1, x25 // load TTBR0
  687. msr ttbr1_el1, x26 // load TTBR1
  688. isb
  689. msr sctlr_el1, x0
  690. isb
  691. /*
  692. * Invalidate the local I-cache so that any instructions fetched
  693. * speculatively from the PoC are discarded, since they may have
  694. * been dynamically patched at the PoU.
  695. */
  696. ic iallu
  697. dsb nsh
  698. isb
  699. #ifdef CONFIG_RANDOMIZE_BASE
  700. mov x19, x0 // preserve new SCTLR_EL1 value
  701. blr x27
  702. /*
  703. * If we return here, we have a KASLR displacement in x23 which we need
  704. * to take into account by discarding the current kernel mapping and
  705. * creating a new one.
  706. */
  707. msr sctlr_el1, x18 // disable the MMU
  708. isb
  709. bl __create_page_tables // recreate kernel mapping
  710. msr sctlr_el1, x19 // re-enable the MMU
  711. isb
  712. ic ialluis // flush instructions fetched
  713. isb // via old mapping
  714. add x27, x27, x23 // relocated __mmap_switched
  715. #endif
  716. br x27
  717. ENDPROC(__enable_mmu)
  718. __no_granule_support:
  719. /* Indicate that this CPU can't boot and is stuck in the kernel */
  720. update_early_cpu_boot_status CPU_STUCK_IN_KERNEL, x1, x2
  721. 1:
  722. wfe
  723. wfi
  724. b 1b
  725. ENDPROC(__no_granule_support)