proc.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Based on arch/arm/mm/proc.S
  4. *
  5. * Copyright (C) 2001 Deep Blue Solutions Ltd.
  6. * Copyright (C) 2012 ARM Ltd.
  7. * Author: Catalin Marinas <[email protected]>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/linkage.h>
  11. #include <linux/pgtable.h>
  12. #include <linux/cfi_types.h>
  13. #include <asm/assembler.h>
  14. #include <asm/asm-offsets.h>
  15. #include <asm/asm_pointer_auth.h>
  16. #include <asm/hwcap.h>
  17. #include <asm/kernel-pgtable.h>
  18. #include <asm/pgtable-hwdef.h>
  19. #include <asm/cpufeature.h>
  20. #include <asm/alternative.h>
  21. #include <asm/smp.h>
  22. #include <asm/sysreg.h>
  23. #ifdef CONFIG_ARM64_64K_PAGES
  24. #define TCR_TG_FLAGS TCR_TG0_64K | TCR_TG1_64K
  25. #elif defined(CONFIG_ARM64_16K_PAGES)
  26. #define TCR_TG_FLAGS TCR_TG0_16K | TCR_TG1_16K
  27. #else /* CONFIG_ARM64_4K_PAGES */
  28. #define TCR_TG_FLAGS TCR_TG0_4K | TCR_TG1_4K
  29. #endif
  30. #ifdef CONFIG_RANDOMIZE_BASE
  31. #define TCR_KASLR_FLAGS TCR_NFD1
  32. #else
  33. #define TCR_KASLR_FLAGS 0
  34. #endif
  35. #define TCR_SMP_FLAGS TCR_SHARED
  36. /* PTWs cacheable, inner/outer WBWA */
  37. #define TCR_CACHE_FLAGS TCR_IRGN_WBWA | TCR_ORGN_WBWA
  38. #ifdef CONFIG_KASAN_SW_TAGS
  39. #define TCR_KASAN_SW_FLAGS TCR_TBI1 | TCR_TBID1
  40. #else
  41. #define TCR_KASAN_SW_FLAGS 0
  42. #endif
  43. #ifdef CONFIG_KASAN_HW_TAGS
  44. #define TCR_MTE_FLAGS TCR_TCMA1 | TCR_TBI1 | TCR_TBID1
  45. #elif defined(CONFIG_ARM64_MTE)
  46. /*
  47. * The mte_zero_clear_page_tags() implementation uses DC GZVA, which relies on
  48. * TBI being enabled at EL1.
  49. */
  50. #define TCR_MTE_FLAGS TCR_TBI1 | TCR_TBID1
  51. #else
  52. #define TCR_MTE_FLAGS 0
  53. #endif
  54. /*
  55. * Default MAIR_EL1. MT_NORMAL_TAGGED is initially mapped as Normal memory and
  56. * changed during mte_cpu_setup to Normal Tagged if the system supports MTE.
  57. */
  58. #define MAIR_EL1_SET \
  59. (MAIR_ATTRIDX(MAIR_ATTR_DEVICE_nGnRnE, MT_DEVICE_nGnRnE) | \
  60. MAIR_ATTRIDX(MAIR_ATTR_DEVICE_nGnRE, MT_DEVICE_nGnRE) | \
  61. MAIR_ATTRIDX(MAIR_ATTR_NORMAL_NC, MT_NORMAL_NC) | \
  62. MAIR_ATTRIDX(MAIR_ATTR_NORMAL, MT_NORMAL) | \
  63. MAIR_ATTRIDX(MAIR_ATTR_NORMAL, MT_NORMAL_TAGGED) | \
  64. MAIR_ATTRIDX(MAIR_ATTR_NORMAL_iNC_oWB, MT_NORMAL_iNC_oWB))
  65. #ifdef CONFIG_CPU_PM
  66. /**
  67. * cpu_do_suspend - save CPU registers context
  68. *
  69. * x0: virtual address of context pointer
  70. *
  71. * This must be kept in sync with struct cpu_suspend_ctx in <asm/suspend.h>.
  72. */
  73. SYM_FUNC_START(cpu_do_suspend)
  74. mrs x2, tpidr_el0
  75. mrs x3, tpidrro_el0
  76. mrs x4, contextidr_el1
  77. mrs x5, osdlr_el1
  78. mrs x6, cpacr_el1
  79. mrs x7, tcr_el1
  80. mrs x8, vbar_el1
  81. mrs x9, mdscr_el1
  82. mrs x10, oslsr_el1
  83. mrs x11, sctlr_el1
  84. get_this_cpu_offset x12
  85. mrs x13, sp_el0
  86. stp x2, x3, [x0]
  87. stp x4, x5, [x0, #16]
  88. stp x6, x7, [x0, #32]
  89. stp x8, x9, [x0, #48]
  90. stp x10, x11, [x0, #64]
  91. stp x12, x13, [x0, #80]
  92. /*
  93. * Save x18 as it may be used as a platform register, e.g. by shadow
  94. * call stack.
  95. */
  96. str x18, [x0, #96]
  97. ret
  98. SYM_FUNC_END(cpu_do_suspend)
  99. /**
  100. * cpu_do_resume - restore CPU register context
  101. *
  102. * x0: Address of context pointer
  103. */
  104. .pushsection ".idmap.text", "awx"
  105. SYM_FUNC_START(cpu_do_resume)
  106. ldp x2, x3, [x0]
  107. ldp x4, x5, [x0, #16]
  108. ldp x6, x8, [x0, #32]
  109. ldp x9, x10, [x0, #48]
  110. ldp x11, x12, [x0, #64]
  111. ldp x13, x14, [x0, #80]
  112. /*
  113. * Restore x18, as it may be used as a platform register, and clear
  114. * the buffer to minimize the risk of exposure when used for shadow
  115. * call stack.
  116. */
  117. ldr x18, [x0, #96]
  118. str xzr, [x0, #96]
  119. msr tpidr_el0, x2
  120. msr tpidrro_el0, x3
  121. msr contextidr_el1, x4
  122. msr cpacr_el1, x6
  123. /* Don't change t0sz here, mask those bits when restoring */
  124. mrs x7, tcr_el1
  125. bfi x8, x7, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH
  126. msr tcr_el1, x8
  127. msr vbar_el1, x9
  128. /*
  129. * __cpu_setup() cleared MDSCR_EL1.MDE and friends, before unmasking
  130. * debug exceptions. By restoring MDSCR_EL1 here, we may take a debug
  131. * exception. Mask them until local_daif_restore() in cpu_suspend()
  132. * resets them.
  133. */
  134. disable_daif
  135. msr mdscr_el1, x10
  136. msr sctlr_el1, x12
  137. set_this_cpu_offset x13
  138. msr sp_el0, x14
  139. /*
  140. * Restore oslsr_el1 by writing oslar_el1
  141. */
  142. msr osdlr_el1, x5
  143. ubfx x11, x11, #1, #1
  144. msr oslar_el1, x11
  145. reset_pmuserenr_el0 x0 // Disable PMU access from EL0
  146. reset_amuserenr_el0 x0 // Disable AMU access from EL0
  147. alternative_if ARM64_HAS_RAS_EXTN
  148. msr_s SYS_DISR_EL1, xzr
  149. alternative_else_nop_endif
  150. ptrauth_keys_install_kernel_nosync x14, x1, x2, x3
  151. isb
  152. ret
  153. SYM_FUNC_END(cpu_do_resume)
  154. .popsection
  155. #endif
  156. .pushsection ".idmap.text", "awx"
  157. .macro __idmap_cpu_set_reserved_ttbr1, tmp1, tmp2
  158. adrp \tmp1, reserved_pg_dir
  159. phys_to_ttbr \tmp2, \tmp1
  160. offset_ttbr1 \tmp2, \tmp1
  161. msr ttbr1_el1, \tmp2
  162. isb
  163. tlbi vmalle1
  164. dsb nsh
  165. isb
  166. .endm
  167. /*
  168. * void idmap_cpu_replace_ttbr1(phys_addr_t ttbr1)
  169. *
  170. * This is the low-level counterpart to cpu_replace_ttbr1, and should not be
  171. * called by anything else. It can only be executed from a TTBR0 mapping.
  172. */
  173. SYM_TYPED_FUNC_START(idmap_cpu_replace_ttbr1)
  174. save_and_disable_daif flags=x2
  175. __idmap_cpu_set_reserved_ttbr1 x1, x3
  176. offset_ttbr1 x0, x3
  177. msr ttbr1_el1, x0
  178. isb
  179. restore_daif x2
  180. ret
  181. SYM_FUNC_END(idmap_cpu_replace_ttbr1)
  182. .popsection
  183. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  184. #define KPTI_NG_PTE_FLAGS (PTE_ATTRINDX(MT_NORMAL) | SWAPPER_PTE_FLAGS)
  185. .pushsection ".idmap.text", "awx"
  186. .macro kpti_mk_tbl_ng, type, num_entries
  187. add end_\type\()p, cur_\type\()p, #\num_entries * 8
  188. .Ldo_\type:
  189. ldr \type, [cur_\type\()p] // Load the entry
  190. tbz \type, #0, .Lnext_\type // Skip invalid and
  191. tbnz \type, #11, .Lnext_\type // non-global entries
  192. orr \type, \type, #PTE_NG // Same bit for blocks and pages
  193. str \type, [cur_\type\()p] // Update the entry
  194. .ifnc \type, pte
  195. tbnz \type, #1, .Lderef_\type
  196. .endif
  197. .Lnext_\type:
  198. add cur_\type\()p, cur_\type\()p, #8
  199. cmp cur_\type\()p, end_\type\()p
  200. b.ne .Ldo_\type
  201. .endm
  202. /*
  203. * Dereference the current table entry and map it into the temporary
  204. * fixmap slot associated with the current level.
  205. */
  206. .macro kpti_map_pgtbl, type, level
  207. str xzr, [temp_pte, #8 * (\level + 1)] // break before make
  208. dsb nshst
  209. add pte, temp_pte, #PAGE_SIZE * (\level + 1)
  210. lsr pte, pte, #12
  211. tlbi vaae1, pte
  212. dsb nsh
  213. isb
  214. phys_to_pte pte, cur_\type\()p
  215. add cur_\type\()p, temp_pte, #PAGE_SIZE * (\level + 1)
  216. orr pte, pte, pte_flags
  217. str pte, [temp_pte, #8 * (\level + 1)]
  218. dsb nshst
  219. .endm
  220. /*
  221. * void __kpti_install_ng_mappings(int cpu, int num_secondaries, phys_addr_t temp_pgd,
  222. * unsigned long temp_pte_va)
  223. *
  224. * Called exactly once from stop_machine context by each CPU found during boot.
  225. */
  226. .pushsection ".data", "aw", %progbits
  227. SYM_DATA(__idmap_kpti_flag, .long 1)
  228. .popsection
  229. SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings)
  230. cpu .req w0
  231. temp_pte .req x0
  232. num_cpus .req w1
  233. pte_flags .req x1
  234. temp_pgd_phys .req x2
  235. swapper_ttb .req x3
  236. flag_ptr .req x4
  237. cur_pgdp .req x5
  238. end_pgdp .req x6
  239. pgd .req x7
  240. cur_pudp .req x8
  241. end_pudp .req x9
  242. cur_pmdp .req x11
  243. end_pmdp .req x12
  244. cur_ptep .req x14
  245. end_ptep .req x15
  246. pte .req x16
  247. valid .req x17
  248. mov x5, x3 // preserve temp_pte arg
  249. mrs swapper_ttb, ttbr1_el1
  250. adr_l flag_ptr, __idmap_kpti_flag
  251. cbnz cpu, __idmap_kpti_secondary
  252. /* We're the boot CPU. Wait for the others to catch up */
  253. sevl
  254. 1: wfe
  255. ldaxr w17, [flag_ptr]
  256. eor w17, w17, num_cpus
  257. cbnz w17, 1b
  258. /* Switch to the temporary page tables on this CPU only */
  259. __idmap_cpu_set_reserved_ttbr1 x8, x9
  260. offset_ttbr1 temp_pgd_phys, x8
  261. msr ttbr1_el1, temp_pgd_phys
  262. isb
  263. mov temp_pte, x5
  264. mov pte_flags, #KPTI_NG_PTE_FLAGS
  265. /* Everybody is enjoying the idmap, so we can rewrite swapper. */
  266. /* PGD */
  267. adrp cur_pgdp, swapper_pg_dir
  268. kpti_map_pgtbl pgd, 0
  269. kpti_mk_tbl_ng pgd, PTRS_PER_PGD
  270. /* Ensure all the updated entries are visible to secondary CPUs */
  271. dsb ishst
  272. /* We're done: fire up swapper_pg_dir again */
  273. __idmap_cpu_set_reserved_ttbr1 x8, x9
  274. msr ttbr1_el1, swapper_ttb
  275. isb
  276. /* Set the flag to zero to indicate that we're all done */
  277. str wzr, [flag_ptr]
  278. ret
  279. .Lderef_pgd:
  280. /* PUD */
  281. .if CONFIG_PGTABLE_LEVELS > 3
  282. pud .req x10
  283. pte_to_phys cur_pudp, pgd
  284. kpti_map_pgtbl pud, 1
  285. kpti_mk_tbl_ng pud, PTRS_PER_PUD
  286. b .Lnext_pgd
  287. .else /* CONFIG_PGTABLE_LEVELS <= 3 */
  288. pud .req pgd
  289. .set .Lnext_pud, .Lnext_pgd
  290. .endif
  291. .Lderef_pud:
  292. /* PMD */
  293. .if CONFIG_PGTABLE_LEVELS > 2
  294. pmd .req x13
  295. pte_to_phys cur_pmdp, pud
  296. kpti_map_pgtbl pmd, 2
  297. kpti_mk_tbl_ng pmd, PTRS_PER_PMD
  298. b .Lnext_pud
  299. .else /* CONFIG_PGTABLE_LEVELS <= 2 */
  300. pmd .req pgd
  301. .set .Lnext_pmd, .Lnext_pgd
  302. .endif
  303. .Lderef_pmd:
  304. /* PTE */
  305. pte_to_phys cur_ptep, pmd
  306. kpti_map_pgtbl pte, 3
  307. kpti_mk_tbl_ng pte, PTRS_PER_PTE
  308. b .Lnext_pmd
  309. .unreq cpu
  310. .unreq temp_pte
  311. .unreq num_cpus
  312. .unreq pte_flags
  313. .unreq temp_pgd_phys
  314. .unreq cur_pgdp
  315. .unreq end_pgdp
  316. .unreq pgd
  317. .unreq cur_pudp
  318. .unreq end_pudp
  319. .unreq pud
  320. .unreq cur_pmdp
  321. .unreq end_pmdp
  322. .unreq pmd
  323. .unreq cur_ptep
  324. .unreq end_ptep
  325. .unreq pte
  326. .unreq valid
  327. /* Secondary CPUs end up here */
  328. __idmap_kpti_secondary:
  329. /* Uninstall swapper before surgery begins */
  330. __idmap_cpu_set_reserved_ttbr1 x16, x17
  331. /* Increment the flag to let the boot CPU we're ready */
  332. 1: ldxr w16, [flag_ptr]
  333. add w16, w16, #1
  334. stxr w17, w16, [flag_ptr]
  335. cbnz w17, 1b
  336. /* Wait for the boot CPU to finish messing around with swapper */
  337. sevl
  338. 1: wfe
  339. ldxr w16, [flag_ptr]
  340. cbnz w16, 1b
  341. /* All done, act like nothing happened */
  342. msr ttbr1_el1, swapper_ttb
  343. isb
  344. ret
  345. .unreq swapper_ttb
  346. .unreq flag_ptr
  347. SYM_FUNC_END(idmap_kpti_install_ng_mappings)
  348. .popsection
  349. #endif
  350. /*
  351. * __cpu_setup
  352. *
  353. * Initialise the processor for turning the MMU on.
  354. *
  355. * Input:
  356. * x0 - actual number of VA bits (ignored unless VA_BITS > 48)
  357. * Output:
  358. * Return in x0 the value of the SCTLR_EL1 register.
  359. */
  360. .pushsection ".idmap.text", "awx"
  361. SYM_FUNC_START(__cpu_setup)
  362. tlbi vmalle1 // Invalidate local TLB
  363. dsb nsh
  364. mov x1, #3 << 20
  365. msr cpacr_el1, x1 // Enable FP/ASIMD
  366. mov x1, #1 << 12 // Reset mdscr_el1 and disable
  367. msr mdscr_el1, x1 // access to the DCC from EL0
  368. isb // Unmask debug exceptions now,
  369. enable_dbg // since this is per-cpu
  370. reset_pmuserenr_el0 x1 // Disable PMU access from EL0
  371. reset_amuserenr_el0 x1 // Disable AMU access from EL0
  372. /*
  373. * Default values for VMSA control registers. These will be adjusted
  374. * below depending on detected CPU features.
  375. */
  376. mair .req x17
  377. tcr .req x16
  378. mov_q mair, MAIR_EL1_SET
  379. mov_q tcr, TCR_TxSZ(VA_BITS) | TCR_CACHE_FLAGS | TCR_SMP_FLAGS | \
  380. TCR_TG_FLAGS | TCR_KASLR_FLAGS | TCR_ASID16 | \
  381. TCR_TBI0 | TCR_A1 | TCR_KASAN_SW_FLAGS | TCR_MTE_FLAGS
  382. tcr_clear_errata_bits tcr, x9, x5
  383. #ifdef CONFIG_ARM64_VA_BITS_52
  384. sub x9, xzr, x0
  385. add x9, x9, #64
  386. tcr_set_t1sz tcr, x9
  387. #else
  388. idmap_get_t0sz x9
  389. #endif
  390. tcr_set_t0sz tcr, x9
  391. /*
  392. * Set the IPS bits in TCR_EL1.
  393. */
  394. tcr_compute_pa_size tcr, #TCR_IPS_SHIFT, x5, x6
  395. #ifdef CONFIG_ARM64_HW_AFDBM
  396. /*
  397. * Enable hardware update of the Access Flags bit.
  398. * Hardware dirty bit management is enabled later,
  399. * via capabilities.
  400. */
  401. mrs x9, ID_AA64MMFR1_EL1
  402. and x9, x9, #0xf
  403. cbz x9, 1f
  404. orr tcr, tcr, #TCR_HA // hardware Access flag update
  405. 1:
  406. #endif /* CONFIG_ARM64_HW_AFDBM */
  407. msr mair_el1, mair
  408. msr tcr_el1, tcr
  409. /*
  410. * Prepare SCTLR
  411. */
  412. mov_q x0, INIT_SCTLR_EL1_MMU_ON
  413. ret // return to head.S
  414. .unreq mair
  415. .unreq tcr
  416. SYM_FUNC_END(__cpu_setup)