PageRenderTime 312ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/arm64/kernel/head.S

https://bitbucket.org/Tofe/picasso-kernel
Assembly | 537 lines | 504 code | 33 blank | 0 comment | 6 complexity | 16e7b4394914b4b68217b7bef2e2a5d9 MD5 | raw file
  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 <asm/assembler.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/asm-offsets.h>
  27. #include <asm/memory.h>
  28. #include <asm/thread_info.h>
  29. #include <asm/pgtable-hwdef.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/page.h>
  32. #include <asm/virt.h>
  33. /*
  34. * swapper_pg_dir is the virtual address of the initial page table. We place
  35. * the page tables 3 * PAGE_SIZE below KERNEL_RAM_VADDR. The idmap_pg_dir has
  36. * 2 pages and is placed below swapper_pg_dir.
  37. */
  38. #define KERNEL_RAM_VADDR (PAGE_OFFSET + TEXT_OFFSET)
  39. #if (KERNEL_RAM_VADDR & 0xfffff) != 0x80000
  40. #error KERNEL_RAM_VADDR must start at 0xXXX80000
  41. #endif
  42. #define SWAPPER_DIR_SIZE (3 * PAGE_SIZE)
  43. #define IDMAP_DIR_SIZE (2 * PAGE_SIZE)
  44. .globl swapper_pg_dir
  45. .equ swapper_pg_dir, KERNEL_RAM_VADDR - SWAPPER_DIR_SIZE
  46. .globl idmap_pg_dir
  47. .equ idmap_pg_dir, swapper_pg_dir - IDMAP_DIR_SIZE
  48. .macro pgtbl, ttb0, ttb1, phys
  49. add \ttb1, \phys, #TEXT_OFFSET - SWAPPER_DIR_SIZE
  50. sub \ttb0, \ttb1, #IDMAP_DIR_SIZE
  51. .endm
  52. #ifdef CONFIG_ARM64_64K_PAGES
  53. #define BLOCK_SHIFT PAGE_SHIFT
  54. #define BLOCK_SIZE PAGE_SIZE
  55. #else
  56. #define BLOCK_SHIFT SECTION_SHIFT
  57. #define BLOCK_SIZE SECTION_SIZE
  58. #endif
  59. #define KERNEL_START KERNEL_RAM_VADDR
  60. #define KERNEL_END _end
  61. /*
  62. * Initial memory map attributes.
  63. */
  64. #ifndef CONFIG_SMP
  65. #define PTE_FLAGS PTE_TYPE_PAGE | PTE_AF
  66. #define PMD_FLAGS PMD_TYPE_SECT | PMD_SECT_AF
  67. #else
  68. #define PTE_FLAGS PTE_TYPE_PAGE | PTE_AF | PTE_SHARED
  69. #define PMD_FLAGS PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S
  70. #endif
  71. #ifdef CONFIG_ARM64_64K_PAGES
  72. #define MM_MMUFLAGS PTE_ATTRINDX(MT_NORMAL) | PTE_FLAGS
  73. #define IO_MMUFLAGS PTE_ATTRINDX(MT_DEVICE_nGnRE) | PTE_XN | PTE_FLAGS
  74. #else
  75. #define MM_MMUFLAGS PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS
  76. #define IO_MMUFLAGS PMD_ATTRINDX(MT_DEVICE_nGnRE) | PMD_SECT_XN | PMD_FLAGS
  77. #endif
  78. /*
  79. * Kernel startup entry point.
  80. * ---------------------------
  81. *
  82. * The requirements are:
  83. * MMU = off, D-cache = off, I-cache = on or off,
  84. * x0 = physical address to the FDT blob.
  85. *
  86. * This code is mostly position independent so you call this at
  87. * __pa(PAGE_OFFSET + TEXT_OFFSET).
  88. *
  89. * Note that the callee-saved registers are used for storing variables
  90. * that are useful before the MMU is enabled. The allocations are described
  91. * in the entry routines.
  92. */
  93. __HEAD
  94. /*
  95. * DO NOT MODIFY. Image header expected by Linux boot-loaders.
  96. */
  97. b stext // branch to kernel start, magic
  98. .long 0 // reserved
  99. .quad TEXT_OFFSET // Image load offset from start of RAM
  100. .quad 0 // reserved
  101. .quad 0 // reserved
  102. ENTRY(stext)
  103. mov x21, x0 // x21=FDT
  104. bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
  105. bl el2_setup // Drop to EL1
  106. mrs x22, midr_el1 // x22=cpuid
  107. mov x0, x22
  108. bl lookup_processor_type
  109. mov x23, x0 // x23=current cpu_table
  110. cbz x23, __error_p // invalid processor (x23=0)?
  111. bl __vet_fdt
  112. bl __create_page_tables // x25=TTBR0, x26=TTBR1
  113. /*
  114. * The following calls CPU specific code in a position independent
  115. * manner. See arch/arm64/mm/proc.S for details. x23 = base of
  116. * cpu_info structure selected by lookup_processor_type above.
  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. ldr x27, __switch_data // address to jump to after
  121. // MMU has been enabled
  122. adr lr, __enable_mmu // return (PIC) address
  123. ldr x12, [x23, #CPU_INFO_SETUP]
  124. add x12, x12, x28 // __virt_to_phys
  125. br x12 // initialise processor
  126. ENDPROC(stext)
  127. /*
  128. * If we're fortunate enough to boot at EL2, ensure that the world is
  129. * sane before dropping to EL1.
  130. */
  131. ENTRY(el2_setup)
  132. mrs x0, CurrentEL
  133. cmp x0, #PSR_MODE_EL2t
  134. ccmp x0, #PSR_MODE_EL2h, #0x4, ne
  135. ldr x0, =__boot_cpu_mode // Compute __boot_cpu_mode
  136. add x0, x0, x28
  137. b.eq 1f
  138. str wzr, [x0] // Remember we don't have EL2...
  139. ret
  140. /* Hyp configuration. */
  141. 1: ldr w1, =BOOT_CPU_MODE_EL2
  142. str w1, [x0, #4] // This CPU has EL2
  143. mov x0, #(1 << 31) // 64-bit EL1
  144. msr hcr_el2, x0
  145. /* Generic timers. */
  146. mrs x0, cnthctl_el2
  147. orr x0, x0, #3 // Enable EL1 physical timers
  148. msr cnthctl_el2, x0
  149. msr cntvoff_el2, xzr // Clear virtual offset
  150. /* Populate ID registers. */
  151. mrs x0, midr_el1
  152. mrs x1, mpidr_el1
  153. msr vpidr_el2, x0
  154. msr vmpidr_el2, x1
  155. /* sctlr_el1 */
  156. mov x0, #0x0800 // Set/clear RES{1,0} bits
  157. movk x0, #0x30d0, lsl #16
  158. msr sctlr_el1, x0
  159. /* Coprocessor traps. */
  160. mov x0, #0x33ff
  161. msr cptr_el2, x0 // Disable copro. traps to EL2
  162. #ifdef CONFIG_COMPAT
  163. msr hstr_el2, xzr // Disable CP15 traps to EL2
  164. #endif
  165. /* Stage-2 translation */
  166. msr vttbr_el2, xzr
  167. /* Hypervisor stub */
  168. adr x0, __hyp_stub_vectors
  169. msr vbar_el2, x0
  170. /* spsr */
  171. mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  172. PSR_MODE_EL1h)
  173. msr spsr_el2, x0
  174. msr elr_el2, lr
  175. eret
  176. ENDPROC(el2_setup)
  177. /*
  178. * We need to find out the CPU boot mode long after boot, so we need to
  179. * store it in a writable variable.
  180. *
  181. * This is not in .bss, because we set it sufficiently early that the boot-time
  182. * zeroing of .bss would clobber it.
  183. */
  184. .pushsection .data
  185. ENTRY(__boot_cpu_mode)
  186. .long BOOT_CPU_MODE_EL2
  187. .long 0
  188. .popsection
  189. .align 3
  190. 2: .quad .
  191. .quad PAGE_OFFSET
  192. #ifdef CONFIG_SMP
  193. .pushsection .smp.pen.text, "ax"
  194. .align 3
  195. 1: .quad .
  196. .quad secondary_holding_pen_release
  197. /*
  198. * This provides a "holding pen" for platforms to hold all secondary
  199. * cores are held until we're ready for them to initialise.
  200. */
  201. ENTRY(secondary_holding_pen)
  202. bl __calc_phys_offset // x24=phys offset
  203. bl el2_setup // Drop to EL1
  204. mrs x0, mpidr_el1
  205. and x0, x0, #15 // CPU number
  206. adr x1, 1b
  207. ldp x2, x3, [x1]
  208. sub x1, x1, x2
  209. add x3, x3, x1
  210. pen: ldr x4, [x3]
  211. cmp x4, x0
  212. b.eq secondary_startup
  213. wfe
  214. b pen
  215. ENDPROC(secondary_holding_pen)
  216. .popsection
  217. ENTRY(secondary_startup)
  218. /*
  219. * Common entry point for secondary CPUs.
  220. */
  221. mrs x22, midr_el1 // x22=cpuid
  222. mov x0, x22
  223. bl lookup_processor_type
  224. mov x23, x0 // x23=current cpu_table
  225. cbz x23, __error_p // invalid processor (x23=0)?
  226. pgtbl x25, x26, x24 // x25=TTBR0, x26=TTBR1
  227. ldr x12, [x23, #CPU_INFO_SETUP]
  228. add x12, x12, x28 // __virt_to_phys
  229. blr x12 // initialise processor
  230. ldr x21, =secondary_data
  231. ldr x27, =__secondary_switched // address to jump to after enabling the MMU
  232. b __enable_mmu
  233. ENDPROC(secondary_startup)
  234. ENTRY(__secondary_switched)
  235. ldr x0, [x21] // get secondary_data.stack
  236. mov sp, x0
  237. mov x29, #0
  238. b secondary_start_kernel
  239. ENDPROC(__secondary_switched)
  240. #endif /* CONFIG_SMP */
  241. /*
  242. * Setup common bits before finally enabling the MMU. Essentially this is just
  243. * loading the page table pointer and vector base registers.
  244. *
  245. * On entry to this code, x0 must contain the SCTLR_EL1 value for turning on
  246. * the MMU.
  247. */
  248. __enable_mmu:
  249. ldr x5, =vectors
  250. msr vbar_el1, x5
  251. msr ttbr0_el1, x25 // load TTBR0
  252. msr ttbr1_el1, x26 // load TTBR1
  253. isb
  254. b __turn_mmu_on
  255. ENDPROC(__enable_mmu)
  256. /*
  257. * Enable the MMU. This completely changes the structure of the visible memory
  258. * space. You will not be able to trace execution through this.
  259. *
  260. * x0 = system control register
  261. * x27 = *virtual* address to jump to upon completion
  262. *
  263. * other registers depend on the function called upon completion
  264. */
  265. .align 6
  266. __turn_mmu_on:
  267. msr sctlr_el1, x0
  268. isb
  269. br x27
  270. ENDPROC(__turn_mmu_on)
  271. /*
  272. * Calculate the start of physical memory.
  273. */
  274. __calc_phys_offset:
  275. adr x0, 1f
  276. ldp x1, x2, [x0]
  277. sub x28, x0, x1 // x28 = PHYS_OFFSET - PAGE_OFFSET
  278. add x24, x2, x28 // x24 = PHYS_OFFSET
  279. ret
  280. ENDPROC(__calc_phys_offset)
  281. .align 3
  282. 1: .quad .
  283. .quad PAGE_OFFSET
  284. /*
  285. * Macro to populate the PGD for the corresponding block entry in the next
  286. * level (tbl) for the given virtual address.
  287. *
  288. * Preserves: pgd, tbl, virt
  289. * Corrupts: tmp1, tmp2
  290. */
  291. .macro create_pgd_entry, pgd, tbl, virt, tmp1, tmp2
  292. lsr \tmp1, \virt, #PGDIR_SHIFT
  293. and \tmp1, \tmp1, #PTRS_PER_PGD - 1 // PGD index
  294. orr \tmp2, \tbl, #3 // PGD entry table type
  295. str \tmp2, [\pgd, \tmp1, lsl #3]
  296. .endm
  297. /*
  298. * Macro to populate block entries in the page table for the start..end
  299. * virtual range (inclusive).
  300. *
  301. * Preserves: tbl, flags
  302. * Corrupts: phys, start, end, pstate
  303. */
  304. .macro create_block_map, tbl, flags, phys, start, end, idmap=0
  305. lsr \phys, \phys, #BLOCK_SHIFT
  306. .if \idmap
  307. and \start, \phys, #PTRS_PER_PTE - 1 // table index
  308. .else
  309. lsr \start, \start, #BLOCK_SHIFT
  310. and \start, \start, #PTRS_PER_PTE - 1 // table index
  311. .endif
  312. orr \phys, \flags, \phys, lsl #BLOCK_SHIFT // table entry
  313. .ifnc \start,\end
  314. lsr \end, \end, #BLOCK_SHIFT
  315. and \end, \end, #PTRS_PER_PTE - 1 // table end index
  316. .endif
  317. 9999: str \phys, [\tbl, \start, lsl #3] // store the entry
  318. .ifnc \start,\end
  319. add \start, \start, #1 // next entry
  320. add \phys, \phys, #BLOCK_SIZE // next block
  321. cmp \start, \end
  322. b.ls 9999b
  323. .endif
  324. .endm
  325. /*
  326. * Setup the initial page tables. We only setup the barest amount which is
  327. * required to get the kernel running. The following sections are required:
  328. * - identity mapping to enable the MMU (low address, TTBR0)
  329. * - first few MB of the kernel linear mapping to jump to once the MMU has
  330. * been enabled, including the FDT blob (TTBR1)
  331. */
  332. __create_page_tables:
  333. pgtbl x25, x26, x24 // idmap_pg_dir and swapper_pg_dir addresses
  334. /*
  335. * Clear the idmap and swapper page tables.
  336. */
  337. mov x0, x25
  338. add x6, x26, #SWAPPER_DIR_SIZE
  339. 1: stp xzr, xzr, [x0], #16
  340. stp xzr, xzr, [x0], #16
  341. stp xzr, xzr, [x0], #16
  342. stp xzr, xzr, [x0], #16
  343. cmp x0, x6
  344. b.lo 1b
  345. ldr x7, =MM_MMUFLAGS
  346. /*
  347. * Create the identity mapping.
  348. */
  349. add x0, x25, #PAGE_SIZE // section table address
  350. adr x3, __turn_mmu_on // virtual/physical address
  351. create_pgd_entry x25, x0, x3, x5, x6
  352. create_block_map x0, x7, x3, x5, x5, idmap=1
  353. /*
  354. * Map the kernel image (starting with PHYS_OFFSET).
  355. */
  356. add x0, x26, #PAGE_SIZE // section table address
  357. mov x5, #PAGE_OFFSET
  358. create_pgd_entry x26, x0, x5, x3, x6
  359. ldr x6, =KERNEL_END - 1
  360. mov x3, x24 // phys offset
  361. create_block_map x0, x7, x3, x5, x6
  362. /*
  363. * Map the FDT blob (maximum 2MB; must be within 512MB of
  364. * PHYS_OFFSET).
  365. */
  366. mov x3, x21 // FDT phys address
  367. and x3, x3, #~((1 << 21) - 1) // 2MB aligned
  368. mov x6, #PAGE_OFFSET
  369. sub x5, x3, x24 // subtract PHYS_OFFSET
  370. tst x5, #~((1 << 29) - 1) // within 512MB?
  371. csel x21, xzr, x21, ne // zero the FDT pointer
  372. b.ne 1f
  373. add x5, x5, x6 // __va(FDT blob)
  374. add x6, x5, #1 << 21 // 2MB for the FDT blob
  375. sub x6, x6, #1 // inclusive range
  376. create_block_map x0, x7, x3, x5, x6
  377. 1:
  378. ret
  379. ENDPROC(__create_page_tables)
  380. .ltorg
  381. .align 3
  382. .type __switch_data, %object
  383. __switch_data:
  384. .quad __mmap_switched
  385. .quad __data_loc // x4
  386. .quad _data // x5
  387. .quad __bss_start // x6
  388. .quad _end // x7
  389. .quad processor_id // x4
  390. .quad __fdt_pointer // x5
  391. .quad memstart_addr // x6
  392. .quad init_thread_union + THREAD_START_SP // sp
  393. /*
  394. * The following fragment of code is executed with the MMU on in MMU mode, and
  395. * uses absolute addresses; this is not position independent.
  396. */
  397. __mmap_switched:
  398. adr x3, __switch_data + 8
  399. ldp x4, x5, [x3], #16
  400. ldp x6, x7, [x3], #16
  401. cmp x4, x5 // Copy data segment if needed
  402. 1: ccmp x5, x6, #4, ne
  403. b.eq 2f
  404. ldr x16, [x4], #8
  405. str x16, [x5], #8
  406. b 1b
  407. 2:
  408. 1: cmp x6, x7
  409. b.hs 2f
  410. str xzr, [x6], #8 // Clear BSS
  411. b 1b
  412. 2:
  413. ldp x4, x5, [x3], #16
  414. ldr x6, [x3], #8
  415. ldr x16, [x3]
  416. mov sp, x16
  417. str x22, [x4] // Save processor ID
  418. str x21, [x5] // Save FDT pointer
  419. str x24, [x6] // Save PHYS_OFFSET
  420. mov x29, #0
  421. b start_kernel
  422. ENDPROC(__mmap_switched)
  423. /*
  424. * Exception handling. Something went wrong and we can't proceed. We ought to
  425. * tell the user, but since we don't have any guarantee that we're even
  426. * running on the right architecture, we do virtually nothing.
  427. */
  428. __error_p:
  429. ENDPROC(__error_p)
  430. __error:
  431. 1: nop
  432. b 1b
  433. ENDPROC(__error)
  434. /*
  435. * This function gets the processor ID in w0 and searches the cpu_table[] for
  436. * a match. It returns a pointer to the struct cpu_info it found. The
  437. * cpu_table[] must end with an empty (all zeros) structure.
  438. *
  439. * This routine can be called via C code and it needs to work with the MMU
  440. * both disabled and enabled (the offset is calculated automatically).
  441. */
  442. ENTRY(lookup_processor_type)
  443. adr x1, __lookup_processor_type_data
  444. ldp x2, x3, [x1]
  445. sub x1, x1, x2 // get offset between VA and PA
  446. add x3, x3, x1 // convert VA to PA
  447. 1:
  448. ldp w5, w6, [x3] // load cpu_id_val and cpu_id_mask
  449. cbz w5, 2f // end of list?
  450. and w6, w6, w0
  451. cmp w5, w6
  452. b.eq 3f
  453. add x3, x3, #CPU_INFO_SZ
  454. b 1b
  455. 2:
  456. mov x3, #0 // unknown processor
  457. 3:
  458. mov x0, x3
  459. ret
  460. ENDPROC(lookup_processor_type)
  461. .align 3
  462. .type __lookup_processor_type_data, %object
  463. __lookup_processor_type_data:
  464. .quad .
  465. .quad cpu_table
  466. .size __lookup_processor_type_data, . - __lookup_processor_type_data
  467. /*
  468. * Determine validity of the x21 FDT pointer.
  469. * The dtb must be 8-byte aligned and live in the first 512M of memory.
  470. */
  471. __vet_fdt:
  472. tst x21, #0x7
  473. b.ne 1f
  474. cmp x21, x24
  475. b.lt 1f
  476. mov x0, #(1 << 29)
  477. add x0, x0, x24
  478. cmp x21, x0
  479. b.ge 1f
  480. ret
  481. 1:
  482. mov x21, #0
  483. ret
  484. ENDPROC(__vet_fdt)