head.S 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. #define __PHYS_OFFSET (KERNEL_START - TEXT_OFFSET)
  43. #if (TEXT_OFFSET & 0xfff) != 0
  44. #error TEXT_OFFSET must be at least 4KB aligned
  45. #elif (PAGE_OFFSET & 0x1fffff) != 0
  46. #error PAGE_OFFSET must be at least 2MB aligned
  47. #elif TEXT_OFFSET > 0x1fffff
  48. #error TEXT_OFFSET must be less than 2MB
  49. #endif
  50. /*
  51. * Kernel startup entry point.
  52. * ---------------------------
  53. *
  54. * The requirements are:
  55. * MMU = off, D-cache = off, I-cache = on or off,
  56. * x0 = physical address to the FDT blob.
  57. *
  58. * This code is mostly position independent so you call this at
  59. * __pa(PAGE_OFFSET + TEXT_OFFSET).
  60. *
  61. * Note that the callee-saved registers are used for storing variables
  62. * that are useful before the MMU is enabled. The allocations are described
  63. * in the entry routines.
  64. */
  65. __HEAD
  66. _head:
  67. /*
  68. * DO NOT MODIFY. Image header expected by Linux boot-loaders.
  69. */
  70. #ifdef CONFIG_EFI
  71. /*
  72. * This add instruction has no meaningful effect except that
  73. * its opcode forms the magic "MZ" signature required by UEFI.
  74. */
  75. add x13, x18, #0x16
  76. b stext
  77. #else
  78. b stext // branch to kernel start, magic
  79. .long 0 // reserved
  80. #endif
  81. le64sym _kernel_offset_le // Image load offset from start of RAM, little-endian
  82. le64sym _kernel_size_le // Effective size of kernel image, little-endian
  83. le64sym _kernel_flags_le // Informative flags, little-endian
  84. .quad 0 // reserved
  85. .quad 0 // reserved
  86. .quad 0 // reserved
  87. .byte 0x41 // Magic number, "ARM\x64"
  88. .byte 0x52
  89. .byte 0x4d
  90. .byte 0x64
  91. #ifdef CONFIG_EFI
  92. .long pe_header - _head // Offset to the PE header.
  93. #else
  94. .word 0 // reserved
  95. #endif
  96. #ifdef CONFIG_EFI
  97. .align 3
  98. pe_header:
  99. .ascii "PE"
  100. .short 0
  101. coff_header:
  102. .short 0xaa64 // AArch64
  103. .short 2 // nr_sections
  104. .long 0 // TimeDateStamp
  105. .long 0 // PointerToSymbolTable
  106. .long 1 // NumberOfSymbols
  107. .short section_table - optional_header // SizeOfOptionalHeader
  108. .short 0x206 // Characteristics.
  109. // IMAGE_FILE_DEBUG_STRIPPED |
  110. // IMAGE_FILE_EXECUTABLE_IMAGE |
  111. // IMAGE_FILE_LINE_NUMS_STRIPPED
  112. optional_header:
  113. .short 0x20b // PE32+ format
  114. .byte 0x02 // MajorLinkerVersion
  115. .byte 0x14 // MinorLinkerVersion
  116. .long _end - efi_header_end // SizeOfCode
  117. .long 0 // SizeOfInitializedData
  118. .long 0 // SizeOfUninitializedData
  119. .long __efistub_entry - _head // AddressOfEntryPoint
  120. .long efi_header_end - _head // BaseOfCode
  121. extra_header_fields:
  122. .quad 0 // ImageBase
  123. .long 0x1000 // SectionAlignment
  124. .long PECOFF_FILE_ALIGNMENT // FileAlignment
  125. .short 0 // MajorOperatingSystemVersion
  126. .short 0 // MinorOperatingSystemVersion
  127. .short 0 // MajorImageVersion
  128. .short 0 // MinorImageVersion
  129. .short 0 // MajorSubsystemVersion
  130. .short 0 // MinorSubsystemVersion
  131. .long 0 // Win32VersionValue
  132. .long _end - _head // SizeOfImage
  133. // Everything before the kernel image is considered part of the header
  134. .long efi_header_end - _head // SizeOfHeaders
  135. .long 0 // CheckSum
  136. .short 0xa // Subsystem (EFI application)
  137. .short 0 // DllCharacteristics
  138. .quad 0 // SizeOfStackReserve
  139. .quad 0 // SizeOfStackCommit
  140. .quad 0 // SizeOfHeapReserve
  141. .quad 0 // SizeOfHeapCommit
  142. .long 0 // LoaderFlags
  143. .long (section_table - .) / 8 // NumberOfRvaAndSizes
  144. .quad 0 // ExportTable
  145. .quad 0 // ImportTable
  146. .quad 0 // ResourceTable
  147. .quad 0 // ExceptionTable
  148. .quad 0 // CertificationTable
  149. .quad 0 // BaseRelocationTable
  150. #ifdef CONFIG_DEBUG_EFI
  151. .long efi_debug_table - _head // DebugTable
  152. .long efi_debug_table_size
  153. #endif
  154. // Section table
  155. section_table:
  156. /*
  157. * The EFI application loader requires a relocation section
  158. * because EFI applications must be relocatable. This is a
  159. * dummy section as far as we are concerned.
  160. */
  161. .ascii ".reloc"
  162. .byte 0
  163. .byte 0 // end of 0 padding of section name
  164. .long 0
  165. .long 0
  166. .long 0 // SizeOfRawData
  167. .long 0 // PointerToRawData
  168. .long 0 // PointerToRelocations
  169. .long 0 // PointerToLineNumbers
  170. .short 0 // NumberOfRelocations
  171. .short 0 // NumberOfLineNumbers
  172. .long 0x42100040 // Characteristics (section flags)
  173. .ascii ".text"
  174. .byte 0
  175. .byte 0
  176. .byte 0 // end of 0 padding of section name
  177. .long _end - efi_header_end // VirtualSize
  178. .long efi_header_end - _head // VirtualAddress
  179. .long _edata - efi_header_end // SizeOfRawData
  180. .long efi_header_end - _head // PointerToRawData
  181. .long 0 // PointerToRelocations (0 for executables)
  182. .long 0 // PointerToLineNumbers (0 for executables)
  183. .short 0 // NumberOfRelocations (0 for executables)
  184. .short 0 // NumberOfLineNumbers (0 for executables)
  185. .long 0xe0500020 // Characteristics (section flags)
  186. #ifdef CONFIG_DEBUG_EFI
  187. /*
  188. * The debug table is referenced via its Relative Virtual Address (RVA),
  189. * which is only defined for those parts of the image that are covered
  190. * by a section declaration. Since this header is not covered by any
  191. * section, the debug table must be emitted elsewhere. So stick it in
  192. * the .init.rodata section instead.
  193. *
  194. * Note that the EFI debug entry itself may legally have a zero RVA,
  195. * which means we can simply put it right after the section headers.
  196. */
  197. __INITRODATA
  198. .align 2
  199. efi_debug_table:
  200. // EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
  201. .long 0 // Characteristics
  202. .long 0 // TimeDateStamp
  203. .short 0 // MajorVersion
  204. .short 0 // MinorVersion
  205. .long 2 // Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW
  206. .long efi_debug_entry_size // SizeOfData
  207. .long 0 // RVA
  208. .long efi_debug_entry - _head // FileOffset
  209. .set efi_debug_table_size, . - efi_debug_table
  210. .previous
  211. efi_debug_entry:
  212. // EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
  213. .ascii "NB10" // Signature
  214. .long 0 // Unknown
  215. .long 0 // Unknown2
  216. .long 0 // Unknown3
  217. .asciz VMLINUX_PATH
  218. .set efi_debug_entry_size, . - efi_debug_entry
  219. #endif
  220. /*
  221. * EFI will load .text onwards at the 4k section alignment
  222. * described in the PE/COFF header. To ensure that instruction
  223. * sequences using an adrp and a :lo12: immediate will function
  224. * correctly at this alignment, we must ensure that .text is
  225. * placed at a 4k boundary in the Image to begin with.
  226. */
  227. .align 12
  228. efi_header_end:
  229. #endif
  230. __INIT
  231. /*
  232. * The following callee saved general purpose registers are used on the
  233. * primary lowlevel boot path:
  234. *
  235. * Register Scope Purpose
  236. * x21 stext() .. start_kernel() FDT pointer passed at boot in x0
  237. * x23 stext() .. start_kernel() physical misalignment/KASLR offset
  238. * x28 __create_page_tables() callee preserved temp register
  239. * x19/x20 __primary_switch() callee preserved temp registers
  240. */
  241. ENTRY(stext)
  242. bl preserve_boot_args
  243. bl el2_setup // Drop to EL1, w0=cpu_boot_mode
  244. adrp x23, __PHYS_OFFSET
  245. and x23, x23, MIN_KIMG_ALIGN - 1 // KASLR offset, defaults to 0
  246. bl set_cpu_boot_mode_flag
  247. bl __create_page_tables
  248. /*
  249. * The following calls CPU setup code, see arch/arm64/mm/proc.S for
  250. * details.
  251. * On return, the CPU will be ready for the MMU to be turned on and
  252. * the TCR will have been set.
  253. */
  254. bl __cpu_setup // initialise processor
  255. b __primary_switch
  256. ENDPROC(stext)
  257. /*
  258. * Preserve the arguments passed by the bootloader in x0 .. x3
  259. */
  260. preserve_boot_args:
  261. mov x21, x0 // x21=FDT
  262. adr_l x0, boot_args // record the contents of
  263. stp x21, x1, [x0] // x0 .. x3 at kernel entry
  264. stp x2, x3, [x0, #16]
  265. dmb sy // needed before dc ivac with
  266. // MMU off
  267. add x1, x0, #0x20 // 4 x 8 bytes
  268. b __inval_cache_range // tail call
  269. ENDPROC(preserve_boot_args)
  270. /*
  271. * Macro to create a table entry to the next page.
  272. *
  273. * tbl: page table address
  274. * virt: virtual address
  275. * shift: #imm page table shift
  276. * ptrs: #imm pointers per table page
  277. *
  278. * Preserves: virt
  279. * Corrupts: tmp1, tmp2
  280. * Returns: tbl -> next level table page address
  281. */
  282. .macro create_table_entry, tbl, virt, shift, ptrs, tmp1, tmp2
  283. lsr \tmp1, \virt, #\shift
  284. and \tmp1, \tmp1, #\ptrs - 1 // table index
  285. add \tmp2, \tbl, #PAGE_SIZE
  286. orr \tmp2, \tmp2, #PMD_TYPE_TABLE // address of next table and entry type
  287. str \tmp2, [\tbl, \tmp1, lsl #3]
  288. add \tbl, \tbl, #PAGE_SIZE // next level table page
  289. .endm
  290. /*
  291. * Macro to populate the PGD (and possibily PUD) for the corresponding
  292. * block entry in the next level (tbl) for the given virtual address.
  293. *
  294. * Preserves: tbl, next, virt
  295. * Corrupts: tmp1, tmp2
  296. */
  297. .macro create_pgd_entry, tbl, virt, tmp1, tmp2
  298. create_table_entry \tbl, \virt, PGDIR_SHIFT, PTRS_PER_PGD, \tmp1, \tmp2
  299. #if SWAPPER_PGTABLE_LEVELS > 3
  300. create_table_entry \tbl, \virt, PUD_SHIFT, PTRS_PER_PUD, \tmp1, \tmp2
  301. #endif
  302. #if SWAPPER_PGTABLE_LEVELS > 2
  303. create_table_entry \tbl, \virt, SWAPPER_TABLE_SHIFT, PTRS_PER_PTE, \tmp1, \tmp2
  304. #endif
  305. .endm
  306. /*
  307. * Macro to populate block entries in the page table for the start..end
  308. * virtual range (inclusive).
  309. *
  310. * Preserves: tbl, flags
  311. * Corrupts: phys, start, end, pstate
  312. */
  313. .macro create_block_map, tbl, flags, phys, start, end
  314. lsr \phys, \phys, #SWAPPER_BLOCK_SHIFT
  315. lsr \start, \start, #SWAPPER_BLOCK_SHIFT
  316. and \start, \start, #PTRS_PER_PTE - 1 // table index
  317. orr \phys, \flags, \phys, lsl #SWAPPER_BLOCK_SHIFT // table entry
  318. lsr \end, \end, #SWAPPER_BLOCK_SHIFT
  319. and \end, \end, #PTRS_PER_PTE - 1 // table end index
  320. 9999: str \phys, [\tbl, \start, lsl #3] // store the entry
  321. add \start, \start, #1 // next entry
  322. add \phys, \phys, #SWAPPER_BLOCK_SIZE // next block
  323. cmp \start, \end
  324. b.ls 9999b
  325. .endm
  326. /*
  327. * Setup the initial page tables. We only setup the barest amount which is
  328. * required to get the kernel running. The following sections are required:
  329. * - identity mapping to enable the MMU (low address, TTBR0)
  330. * - first few MB of the kernel linear mapping to jump to once the MMU has
  331. * been enabled
  332. */
  333. __create_page_tables:
  334. mov x28, lr
  335. /*
  336. * Invalidate the idmap and swapper page tables to avoid potential
  337. * dirty cache lines being evicted.
  338. */
  339. adrp x0, idmap_pg_dir
  340. adrp x1, swapper_pg_dir + SWAPPER_DIR_SIZE + RESERVED_TTBR0_SIZE
  341. bl __inval_cache_range
  342. /*
  343. * Clear the idmap and swapper page tables.
  344. */
  345. adrp x0, idmap_pg_dir
  346. adrp x6, swapper_pg_dir + SWAPPER_DIR_SIZE + RESERVED_TTBR0_SIZE
  347. 1: stp xzr, xzr, [x0], #16
  348. stp xzr, xzr, [x0], #16
  349. stp xzr, xzr, [x0], #16
  350. stp xzr, xzr, [x0], #16
  351. cmp x0, x6
  352. b.lo 1b
  353. mov x7, SWAPPER_MM_MMUFLAGS
  354. /*
  355. * Create the identity mapping.
  356. */
  357. adrp x0, idmap_pg_dir
  358. adrp x3, __idmap_text_start // __pa(__idmap_text_start)
  359. #ifndef CONFIG_ARM64_VA_BITS_48
  360. #define EXTRA_SHIFT (PGDIR_SHIFT + PAGE_SHIFT - 3)
  361. #define EXTRA_PTRS (1 << (48 - EXTRA_SHIFT))
  362. /*
  363. * If VA_BITS < 48, it may be too small to allow for an ID mapping to be
  364. * created that covers system RAM if that is located sufficiently high
  365. * in the physical address space. So for the ID map, use an extended
  366. * virtual range in that case, by configuring an additional translation
  367. * level.
  368. * First, we have to verify our assumption that the current value of
  369. * VA_BITS was chosen such that all translation levels are fully
  370. * utilised, and that lowering T0SZ will always result in an additional
  371. * translation level to be configured.
  372. */
  373. #if VA_BITS != EXTRA_SHIFT
  374. #error "Mismatch between VA_BITS and page size/number of translation levels"
  375. #endif
  376. /*
  377. * Calculate the maximum allowed value for TCR_EL1.T0SZ so that the
  378. * entire ID map region can be mapped. As T0SZ == (64 - #bits used),
  379. * this number conveniently equals the number of leading zeroes in
  380. * the physical address of __idmap_text_end.
  381. */
  382. adrp x5, __idmap_text_end
  383. clz x5, x5
  384. cmp x5, TCR_T0SZ(VA_BITS) // default T0SZ small enough?
  385. b.ge 1f // .. then skip additional level
  386. adr_l x6, idmap_t0sz
  387. str x5, [x6]
  388. dmb sy
  389. dc ivac, x6 // Invalidate potentially stale cache line
  390. create_table_entry x0, x3, EXTRA_SHIFT, EXTRA_PTRS, x5, x6
  391. 1:
  392. #endif
  393. create_pgd_entry x0, x3, x5, x6
  394. mov x5, x3 // __pa(__idmap_text_start)
  395. adr_l x6, __idmap_text_end // __pa(__idmap_text_end)
  396. create_block_map x0, x7, x3, x5, x6
  397. /*
  398. * Map the kernel image (starting with PHYS_OFFSET).
  399. */
  400. adrp x0, swapper_pg_dir
  401. mov_q x5, KIMAGE_VADDR + TEXT_OFFSET // compile time __va(_text)
  402. add x5, x5, x23 // add KASLR displacement
  403. create_pgd_entry x0, x5, x3, x6
  404. adrp x6, _end // runtime __pa(_end)
  405. adrp x3, _text // runtime __pa(_text)
  406. sub x6, x6, x3 // _end - _text
  407. add x6, x6, x5 // runtime __va(_end)
  408. create_block_map x0, x7, x3, x5, x6
  409. /*
  410. * Since the page tables have been populated with non-cacheable
  411. * accesses (MMU disabled), invalidate the idmap and swapper page
  412. * tables again to remove any speculatively loaded cache lines.
  413. */
  414. adrp x0, idmap_pg_dir
  415. adrp x1, swapper_pg_dir + SWAPPER_DIR_SIZE + RESERVED_TTBR0_SIZE
  416. dmb sy
  417. bl __inval_cache_range
  418. ret x28
  419. ENDPROC(__create_page_tables)
  420. .ltorg
  421. /*
  422. * The following fragment of code is executed with the MMU enabled.
  423. *
  424. * x0 = __PHYS_OFFSET
  425. */
  426. __primary_switched:
  427. adrp x4, init_thread_union
  428. add sp, x4, #THREAD_SIZE
  429. adr_l x5, init_task
  430. msr sp_el0, x5 // Save thread_info
  431. adr_l x8, vectors // load VBAR_EL1 with virtual
  432. msr vbar_el1, x8 // vector table address
  433. isb
  434. stp xzr, x30, [sp, #-16]!
  435. mov x29, sp
  436. str_l x21, __fdt_pointer, x5 // Save FDT pointer
  437. ldr_l x4, kimage_vaddr // Save the offset between
  438. sub x4, x4, x0 // the kernel virtual and
  439. str_l x4, kimage_voffset, x5 // physical mappings
  440. // Clear BSS
  441. adr_l x0, __bss_start
  442. mov x1, xzr
  443. adr_l x2, __bss_stop
  444. sub x2, x2, x0
  445. bl __pi_memset
  446. dsb ishst // Make zero page visible to PTW
  447. #ifdef CONFIG_KASAN
  448. bl kasan_early_init
  449. #endif
  450. #ifdef CONFIG_RANDOMIZE_BASE
  451. tst x23, ~(MIN_KIMG_ALIGN - 1) // already running randomized?
  452. b.ne 0f
  453. mov x0, x21 // pass FDT address in x0
  454. mov x1, x23 // pass modulo offset in x1
  455. bl kaslr_early_init // parse FDT for KASLR options
  456. cbz x0, 0f // KASLR disabled? just proceed
  457. orr x23, x23, x0 // record KASLR offset
  458. ldp x29, x30, [sp], #16 // we must enable KASLR, return
  459. ret // to __primary_switch()
  460. 0:
  461. #endif
  462. b start_kernel
  463. ENDPROC(__primary_switched)
  464. /*
  465. * end early head section, begin head code that is also used for
  466. * hotplug and needs to have the same protections as the text region
  467. */
  468. .section ".idmap.text","ax"
  469. ENTRY(kimage_vaddr)
  470. .quad _text - TEXT_OFFSET
  471. /*
  472. * If we're fortunate enough to boot at EL2, ensure that the world is
  473. * sane before dropping to EL1.
  474. *
  475. * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in w0 if
  476. * booted in EL1 or EL2 respectively.
  477. */
  478. ENTRY(el2_setup)
  479. mrs x0, CurrentEL
  480. cmp x0, #CurrentEL_EL2
  481. b.ne 1f
  482. mrs x0, sctlr_el2
  483. CPU_BE( orr x0, x0, #(1 << 25) ) // Set the EE bit for EL2
  484. CPU_LE( bic x0, x0, #(1 << 25) ) // Clear the EE bit for EL2
  485. msr sctlr_el2, x0
  486. b 2f
  487. 1: mrs x0, sctlr_el1
  488. CPU_BE( orr x0, x0, #(3 << 24) ) // Set the EE and E0E bits for EL1
  489. CPU_LE( bic x0, x0, #(3 << 24) ) // Clear the EE and E0E bits for EL1
  490. msr sctlr_el1, x0
  491. mov w0, #BOOT_CPU_MODE_EL1 // This cpu booted in EL1
  492. isb
  493. ret
  494. 2:
  495. #ifdef CONFIG_ARM64_VHE
  496. /*
  497. * Check for VHE being present. For the rest of the EL2 setup,
  498. * x2 being non-zero indicates that we do have VHE, and that the
  499. * kernel is intended to run at EL2.
  500. */
  501. mrs x2, id_aa64mmfr1_el1
  502. ubfx x2, x2, #8, #4
  503. #else
  504. mov x2, xzr
  505. #endif
  506. /* Hyp configuration. */
  507. mov x0, #HCR_RW // 64-bit EL1
  508. cbz x2, set_hcr
  509. orr x0, x0, #HCR_TGE // Enable Host Extensions
  510. orr x0, x0, #HCR_E2H
  511. set_hcr:
  512. msr hcr_el2, x0
  513. isb
  514. /*
  515. * Allow Non-secure EL1 and EL0 to access physical timer and counter.
  516. * This is not necessary for VHE, since the host kernel runs in EL2,
  517. * and EL0 accesses are configured in the later stage of boot process.
  518. * Note that when HCR_EL2.E2H == 1, CNTHCTL_EL2 has the same bit layout
  519. * as CNTKCTL_EL1, and CNTKCTL_EL1 accessing instructions are redefined
  520. * to access CNTHCTL_EL2. This allows the kernel designed to run at EL1
  521. * to transparently mess with the EL0 bits via CNTKCTL_EL1 access in
  522. * EL2.
  523. */
  524. cbnz x2, 1f
  525. mrs x0, cnthctl_el2
  526. orr x0, x0, #3 // Enable EL1 physical timers
  527. msr cnthctl_el2, x0
  528. 1:
  529. msr cntvoff_el2, xzr // Clear virtual offset
  530. #ifdef CONFIG_ARM_GIC_V3
  531. /* GICv3 system register access */
  532. mrs x0, id_aa64pfr0_el1
  533. ubfx x0, x0, #24, #4
  534. cmp x0, #1
  535. b.ne 3f
  536. mrs_s x0, SYS_ICC_SRE_EL2
  537. orr x0, x0, #ICC_SRE_EL2_SRE // Set ICC_SRE_EL2.SRE==1
  538. orr x0, x0, #ICC_SRE_EL2_ENABLE // Set ICC_SRE_EL2.Enable==1
  539. msr_s SYS_ICC_SRE_EL2, x0
  540. isb // Make sure SRE is now set
  541. mrs_s x0, SYS_ICC_SRE_EL2 // Read SRE back,
  542. tbz x0, #0, 3f // and check that it sticks
  543. msr_s SYS_ICH_HCR_EL2, xzr // Reset ICC_HCR_EL2 to defaults
  544. 3:
  545. #endif
  546. /* Populate ID registers. */
  547. mrs x0, midr_el1
  548. mrs x1, mpidr_el1
  549. msr vpidr_el2, x0
  550. msr vmpidr_el2, x1
  551. /*
  552. * When VHE is not in use, early init of EL2 and EL1 needs to be
  553. * done here.
  554. * When VHE _is_ in use, EL1 will not be used in the host and
  555. * requires no configuration, and all non-hyp-specific EL2 setup
  556. * will be done via the _EL1 system register aliases in __cpu_setup.
  557. */
  558. cbnz x2, 1f
  559. /* sctlr_el1 */
  560. mov x0, #0x0800 // Set/clear RES{1,0} bits
  561. CPU_BE( movk x0, #0x33d0, lsl #16 ) // Set EE and E0E on BE systems
  562. CPU_LE( movk x0, #0x30d0, lsl #16 ) // Clear EE and E0E on LE systems
  563. msr sctlr_el1, x0
  564. /* Coprocessor traps. */
  565. mov x0, #0x33ff
  566. msr cptr_el2, x0 // Disable copro. traps to EL2
  567. 1:
  568. #ifdef CONFIG_COMPAT
  569. msr hstr_el2, xzr // Disable CP15 traps to EL2
  570. #endif
  571. /* EL2 debug */
  572. mrs x1, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
  573. sbfx x0, x1, #8, #4
  574. cmp x0, #1
  575. b.lt 4f // Skip if no PMU present
  576. mrs x0, pmcr_el0 // Disable debug access traps
  577. ubfx x0, x0, #11, #5 // to EL2 and allow access to
  578. 4:
  579. csel x3, xzr, x0, lt // all PMU counters from EL1
  580. /* Statistical profiling */
  581. ubfx x0, x1, #32, #4 // Check ID_AA64DFR0_EL1 PMSVer
  582. cbz x0, 6f // Skip if SPE not present
  583. cbnz x2, 5f // VHE?
  584. mov x1, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
  585. orr x3, x3, x1 // If we don't have VHE, then
  586. b 6f // use EL1&0 translation.
  587. 5: // For VHE, use EL2 translation
  588. orr x3, x3, #MDCR_EL2_TPMS // and disable access from EL1
  589. 6:
  590. msr mdcr_el2, x3 // Configure debug traps
  591. /* Stage-2 translation */
  592. msr vttbr_el2, xzr
  593. cbz x2, install_el2_stub
  594. mov w0, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
  595. isb
  596. ret
  597. install_el2_stub:
  598. /* Hypervisor stub */
  599. adr_l x0, __hyp_stub_vectors
  600. msr vbar_el2, x0
  601. /* spsr */
  602. mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  603. PSR_MODE_EL1h)
  604. msr spsr_el2, x0
  605. msr elr_el2, lr
  606. mov w0, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
  607. eret
  608. ENDPROC(el2_setup)
  609. /*
  610. * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed
  611. * in w0. See arch/arm64/include/asm/virt.h for more info.
  612. */
  613. set_cpu_boot_mode_flag:
  614. adr_l x1, __boot_cpu_mode
  615. cmp w0, #BOOT_CPU_MODE_EL2
  616. b.ne 1f
  617. add x1, x1, #4
  618. 1: str w0, [x1] // This CPU has booted in EL1
  619. dmb sy
  620. dc ivac, x1 // Invalidate potentially stale cache line
  621. ret
  622. ENDPROC(set_cpu_boot_mode_flag)
  623. /*
  624. * These values are written with the MMU off, but read with the MMU on.
  625. * Writers will invalidate the corresponding address, discarding up to a
  626. * 'Cache Writeback Granule' (CWG) worth of data. The linker script ensures
  627. * sufficient alignment that the CWG doesn't overlap another section.
  628. */
  629. .pushsection ".mmuoff.data.write", "aw"
  630. /*
  631. * We need to find out the CPU boot mode long after boot, so we need to
  632. * store it in a writable variable.
  633. *
  634. * This is not in .bss, because we set it sufficiently early that the boot-time
  635. * zeroing of .bss would clobber it.
  636. */
  637. ENTRY(__boot_cpu_mode)
  638. .long BOOT_CPU_MODE_EL2
  639. .long BOOT_CPU_MODE_EL1
  640. /*
  641. * The booting CPU updates the failed status @__early_cpu_boot_status,
  642. * with MMU turned off.
  643. */
  644. ENTRY(__early_cpu_boot_status)
  645. .long 0
  646. .popsection
  647. /*
  648. * This provides a "holding pen" for platforms to hold all secondary
  649. * cores are held until we're ready for them to initialise.
  650. */
  651. ENTRY(secondary_holding_pen)
  652. bl el2_setup // Drop to EL1, w0=cpu_boot_mode
  653. bl set_cpu_boot_mode_flag
  654. mrs x0, mpidr_el1
  655. mov_q x1, MPIDR_HWID_BITMASK
  656. and x0, x0, x1
  657. adr_l x3, secondary_holding_pen_release
  658. pen: ldr x4, [x3]
  659. cmp x4, x0
  660. b.eq secondary_startup
  661. wfe
  662. b pen
  663. ENDPROC(secondary_holding_pen)
  664. /*
  665. * Secondary entry point that jumps straight into the kernel. Only to
  666. * be used where CPUs are brought online dynamically by the kernel.
  667. */
  668. ENTRY(secondary_entry)
  669. bl el2_setup // Drop to EL1
  670. bl set_cpu_boot_mode_flag
  671. b secondary_startup
  672. ENDPROC(secondary_entry)
  673. secondary_startup:
  674. /*
  675. * Common entry point for secondary CPUs.
  676. */
  677. bl __cpu_setup // initialise processor
  678. bl __enable_mmu
  679. ldr x8, =__secondary_switched
  680. br x8
  681. ENDPROC(secondary_startup)
  682. __secondary_switched:
  683. adr_l x5, vectors
  684. msr vbar_el1, x5
  685. isb
  686. adr_l x0, secondary_data
  687. ldr x1, [x0, #CPU_BOOT_STACK] // get secondary_data.stack
  688. mov sp, x1
  689. ldr x2, [x0, #CPU_BOOT_TASK]
  690. msr sp_el0, x2
  691. mov x29, #0
  692. b secondary_start_kernel
  693. ENDPROC(__secondary_switched)
  694. /*
  695. * The booting CPU updates the failed status @__early_cpu_boot_status,
  696. * with MMU turned off.
  697. *
  698. * update_early_cpu_boot_status tmp, status
  699. * - Corrupts tmp1, tmp2
  700. * - Writes 'status' to __early_cpu_boot_status and makes sure
  701. * it is committed to memory.
  702. */
  703. .macro update_early_cpu_boot_status status, tmp1, tmp2
  704. mov \tmp2, #\status
  705. adr_l \tmp1, __early_cpu_boot_status
  706. str \tmp2, [\tmp1]
  707. dmb sy
  708. dc ivac, \tmp1 // Invalidate potentially stale cache line
  709. .endm
  710. /*
  711. * Enable the MMU.
  712. *
  713. * x0 = SCTLR_EL1 value for turning on the MMU.
  714. *
  715. * Returns to the caller via x30/lr. This requires the caller to be covered
  716. * by the .idmap.text section.
  717. *
  718. * Checks if the selected granule size is supported by the CPU.
  719. * If it isn't, park the CPU
  720. */
  721. ENTRY(__enable_mmu)
  722. mrs x1, ID_AA64MMFR0_EL1
  723. ubfx x2, x1, #ID_AA64MMFR0_TGRAN_SHIFT, 4
  724. cmp x2, #ID_AA64MMFR0_TGRAN_SUPPORTED
  725. b.ne __no_granule_support
  726. update_early_cpu_boot_status 0, x1, x2
  727. adrp x1, idmap_pg_dir
  728. adrp x2, swapper_pg_dir
  729. msr ttbr0_el1, x1 // load TTBR0
  730. msr ttbr1_el1, x2 // load TTBR1
  731. isb
  732. msr sctlr_el1, x0
  733. isb
  734. /*
  735. * Invalidate the local I-cache so that any instructions fetched
  736. * speculatively from the PoC are discarded, since they may have
  737. * been dynamically patched at the PoU.
  738. */
  739. ic iallu
  740. dsb nsh
  741. isb
  742. ret
  743. ENDPROC(__enable_mmu)
  744. __no_granule_support:
  745. /* Indicate that this CPU can't boot and is stuck in the kernel */
  746. update_early_cpu_boot_status CPU_STUCK_IN_KERNEL, x1, x2
  747. 1:
  748. wfe
  749. wfi
  750. b 1b
  751. ENDPROC(__no_granule_support)
  752. #ifdef CONFIG_RELOCATABLE
  753. __relocate_kernel:
  754. /*
  755. * Iterate over each entry in the relocation table, and apply the
  756. * relocations in place.
  757. */
  758. ldr w9, =__rela_offset // offset to reloc table
  759. ldr w10, =__rela_size // size of reloc table
  760. mov_q x11, KIMAGE_VADDR // default virtual offset
  761. add x11, x11, x23 // actual virtual offset
  762. add x9, x9, x11 // __va(.rela)
  763. add x10, x9, x10 // __va(.rela) + sizeof(.rela)
  764. 0: cmp x9, x10
  765. b.hs 1f
  766. ldp x11, x12, [x9], #24
  767. ldr x13, [x9, #-8]
  768. cmp w12, #R_AARCH64_RELATIVE
  769. b.ne 0b
  770. add x13, x13, x23 // relocate
  771. str x13, [x11, x23]
  772. b 0b
  773. 1: ret
  774. ENDPROC(__relocate_kernel)
  775. #endif
  776. __primary_switch:
  777. #ifdef CONFIG_RANDOMIZE_BASE
  778. mov x19, x0 // preserve new SCTLR_EL1 value
  779. mrs x20, sctlr_el1 // preserve old SCTLR_EL1 value
  780. #endif
  781. bl __enable_mmu
  782. #ifdef CONFIG_RELOCATABLE
  783. bl __relocate_kernel
  784. #ifdef CONFIG_RANDOMIZE_BASE
  785. ldr x8, =__primary_switched
  786. adrp x0, __PHYS_OFFSET
  787. blr x8
  788. /*
  789. * If we return here, we have a KASLR displacement in x23 which we need
  790. * to take into account by discarding the current kernel mapping and
  791. * creating a new one.
  792. */
  793. msr sctlr_el1, x20 // disable the MMU
  794. isb
  795. bl __create_page_tables // recreate kernel mapping
  796. tlbi vmalle1 // Remove any stale TLB entries
  797. dsb nsh
  798. msr sctlr_el1, x19 // re-enable the MMU
  799. isb
  800. ic iallu // flush instructions fetched
  801. dsb nsh // via old mapping
  802. isb
  803. bl __relocate_kernel
  804. #endif
  805. #endif
  806. ldr x8, =__primary_switched
  807. adrp x0, __PHYS_OFFSET
  808. br x8
  809. ENDPROC(__primary_switch)