proc.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Based on arch/arm/mm/proc.S
  3. *
  4. * Copyright (C) 2001 Deep Blue Solutions Ltd.
  5. * Copyright (C) 2012 ARM Ltd.
  6. * Author: Catalin Marinas <catalin.marinas@arm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/linkage.h>
  22. #include <asm/assembler.h>
  23. #include <asm/asm-offsets.h>
  24. #include <asm/hwcap.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/pgtable-hwdef.h>
  27. #include <asm/cpufeature.h>
  28. #include <asm/alternative.h>
  29. #ifdef CONFIG_ARM64_64K_PAGES
  30. #define TCR_TG_FLAGS TCR_TG0_64K | TCR_TG1_64K
  31. #elif defined(CONFIG_ARM64_16K_PAGES)
  32. #define TCR_TG_FLAGS TCR_TG0_16K | TCR_TG1_16K
  33. #else /* CONFIG_ARM64_4K_PAGES */
  34. #define TCR_TG_FLAGS TCR_TG0_4K | TCR_TG1_4K
  35. #endif
  36. #ifdef CONFIG_RANDOMIZE_BASE
  37. #define TCR_KASLR_FLAGS TCR_NFD1
  38. #else
  39. #define TCR_KASLR_FLAGS 0
  40. #endif
  41. #define TCR_SMP_FLAGS TCR_SHARED
  42. /* PTWs cacheable, inner/outer WBWA */
  43. #define TCR_CACHE_FLAGS TCR_IRGN_WBWA | TCR_ORGN_WBWA
  44. #define MAIR(attr, mt) ((attr) << ((mt) * 8))
  45. /*
  46. * cpu_do_idle()
  47. *
  48. * Idle the processor (wait for interrupt).
  49. */
  50. ENTRY(cpu_do_idle)
  51. dsb sy // WFI may enter a low-power mode
  52. wfi
  53. ret
  54. ENDPROC(cpu_do_idle)
  55. #ifdef CONFIG_CPU_PM
  56. /**
  57. * cpu_do_suspend - save CPU registers context
  58. *
  59. * x0: virtual address of context pointer
  60. */
  61. ENTRY(cpu_do_suspend)
  62. mrs x2, tpidr_el0
  63. mrs x3, tpidrro_el0
  64. mrs x4, contextidr_el1
  65. mrs x5, osdlr_el1
  66. mrs x6, cpacr_el1
  67. mrs x7, tcr_el1
  68. mrs x8, vbar_el1
  69. mrs x9, mdscr_el1
  70. mrs x10, oslsr_el1
  71. mrs x11, sctlr_el1
  72. alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
  73. mrs x12, tpidr_el1
  74. alternative_else
  75. mrs x12, tpidr_el2
  76. alternative_endif
  77. mrs x13, sp_el0
  78. stp x2, x3, [x0]
  79. stp x4, x5, [x0, #16]
  80. stp x6, x7, [x0, #32]
  81. stp x8, x9, [x0, #48]
  82. stp x10, x11, [x0, #64]
  83. stp x12, x13, [x0, #80]
  84. ret
  85. ENDPROC(cpu_do_suspend)
  86. /**
  87. * cpu_do_resume - restore CPU register context
  88. *
  89. * x0: Address of context pointer
  90. */
  91. .pushsection ".idmap.text", "awx"
  92. ENTRY(cpu_do_resume)
  93. ldp x2, x3, [x0]
  94. ldp x4, x5, [x0, #16]
  95. ldp x6, x8, [x0, #32]
  96. ldp x9, x10, [x0, #48]
  97. ldp x11, x12, [x0, #64]
  98. ldp x13, x14, [x0, #80]
  99. msr tpidr_el0, x2
  100. msr tpidrro_el0, x3
  101. msr contextidr_el1, x4
  102. msr cpacr_el1, x6
  103. /* Don't change t0sz here, mask those bits when restoring */
  104. mrs x7, tcr_el1
  105. bfi x8, x7, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH
  106. msr tcr_el1, x8
  107. msr vbar_el1, x9
  108. /*
  109. * __cpu_setup() cleared MDSCR_EL1.MDE and friends, before unmasking
  110. * debug exceptions. By restoring MDSCR_EL1 here, we may take a debug
  111. * exception. Mask them until local_daif_restore() in cpu_suspend()
  112. * resets them.
  113. */
  114. disable_daif
  115. msr mdscr_el1, x10
  116. msr sctlr_el1, x12
  117. alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
  118. msr tpidr_el1, x13
  119. alternative_else
  120. msr tpidr_el2, x13
  121. alternative_endif
  122. msr sp_el0, x14
  123. /*
  124. * Restore oslsr_el1 by writing oslar_el1
  125. */
  126. msr osdlr_el1, x5
  127. ubfx x11, x11, #1, #1
  128. msr oslar_el1, x11
  129. reset_pmuserenr_el0 x0 // Disable PMU access from EL0
  130. alternative_if ARM64_HAS_RAS_EXTN
  131. msr_s SYS_DISR_EL1, xzr
  132. alternative_else_nop_endif
  133. isb
  134. ret
  135. ENDPROC(cpu_do_resume)
  136. .popsection
  137. #endif
  138. /*
  139. * cpu_do_switch_mm(pgd_phys, tsk)
  140. *
  141. * Set the translation table base pointer to be pgd_phys.
  142. *
  143. * - pgd_phys - physical address of new TTB
  144. */
  145. ENTRY(cpu_do_switch_mm)
  146. mrs x2, ttbr1_el1
  147. mmid x1, x1 // get mm->context.id
  148. phys_to_ttbr x3, x0
  149. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  150. bfi x3, x1, #48, #16 // set the ASID field in TTBR0
  151. #endif
  152. bfi x2, x1, #48, #16 // set the ASID
  153. msr ttbr1_el1, x2 // in TTBR1 (since TCR.A1 is set)
  154. isb
  155. msr ttbr0_el1, x3 // now update TTBR0
  156. isb
  157. b post_ttbr_update_workaround // Back to C code...
  158. ENDPROC(cpu_do_switch_mm)
  159. .pushsection ".idmap.text", "awx"
  160. .macro __idmap_cpu_set_reserved_ttbr1, tmp1, tmp2
  161. adrp \tmp1, empty_zero_page
  162. phys_to_ttbr \tmp2, \tmp1
  163. msr ttbr1_el1, \tmp2
  164. isb
  165. tlbi vmalle1
  166. dsb nsh
  167. isb
  168. .endm
  169. /*
  170. * void idmap_cpu_replace_ttbr1(phys_addr_t new_pgd)
  171. *
  172. * This is the low-level counterpart to cpu_replace_ttbr1, and should not be
  173. * called by anything else. It can only be executed from a TTBR0 mapping.
  174. */
  175. ENTRY(idmap_cpu_replace_ttbr1)
  176. save_and_disable_daif flags=x2
  177. __idmap_cpu_set_reserved_ttbr1 x1, x3
  178. phys_to_ttbr x3, x0
  179. msr ttbr1_el1, x3
  180. isb
  181. restore_daif x2
  182. ret
  183. ENDPROC(idmap_cpu_replace_ttbr1)
  184. .popsection
  185. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  186. .pushsection ".idmap.text", "awx"
  187. .macro __idmap_kpti_get_pgtable_ent, type
  188. dc cvac, cur_\()\type\()p // Ensure any existing dirty
  189. dmb sy // lines are written back before
  190. ldr \type, [cur_\()\type\()p] // loading the entry
  191. tbz \type, #0, skip_\()\type // Skip invalid and
  192. tbnz \type, #11, skip_\()\type // non-global entries
  193. .endm
  194. .macro __idmap_kpti_put_pgtable_ent_ng, type
  195. orr \type, \type, #PTE_NG // Same bit for blocks and pages
  196. str \type, [cur_\()\type\()p] // Update the entry and ensure
  197. dmb sy // that it is visible to all
  198. dc civac, cur_\()\type\()p // CPUs.
  199. .endm
  200. /*
  201. * void __kpti_install_ng_mappings(int cpu, int num_cpus, phys_addr_t swapper)
  202. *
  203. * Called exactly once from stop_machine context by each CPU found during boot.
  204. */
  205. __idmap_kpti_flag:
  206. .long 1
  207. ENTRY(idmap_kpti_install_ng_mappings)
  208. cpu .req w0
  209. num_cpus .req w1
  210. swapper_pa .req x2
  211. swapper_ttb .req x3
  212. flag_ptr .req x4
  213. cur_pgdp .req x5
  214. end_pgdp .req x6
  215. pgd .req x7
  216. cur_pudp .req x8
  217. end_pudp .req x9
  218. pud .req x10
  219. cur_pmdp .req x11
  220. end_pmdp .req x12
  221. pmd .req x13
  222. cur_ptep .req x14
  223. end_ptep .req x15
  224. pte .req x16
  225. mrs swapper_ttb, ttbr1_el1
  226. adr flag_ptr, __idmap_kpti_flag
  227. cbnz cpu, __idmap_kpti_secondary
  228. /* We're the boot CPU. Wait for the others to catch up */
  229. sevl
  230. 1: wfe
  231. ldaxr w18, [flag_ptr]
  232. eor w18, w18, num_cpus
  233. cbnz w18, 1b
  234. /* We need to walk swapper, so turn off the MMU. */
  235. pre_disable_mmu_workaround
  236. mrs x18, sctlr_el1
  237. bic x18, x18, #SCTLR_ELx_M
  238. msr sctlr_el1, x18
  239. isb
  240. /* Everybody is enjoying the idmap, so we can rewrite swapper. */
  241. /* PGD */
  242. mov cur_pgdp, swapper_pa
  243. add end_pgdp, cur_pgdp, #(PTRS_PER_PGD * 8)
  244. do_pgd: __idmap_kpti_get_pgtable_ent pgd
  245. tbnz pgd, #1, walk_puds
  246. next_pgd:
  247. __idmap_kpti_put_pgtable_ent_ng pgd
  248. skip_pgd:
  249. add cur_pgdp, cur_pgdp, #8
  250. cmp cur_pgdp, end_pgdp
  251. b.ne do_pgd
  252. /* Publish the updated tables and nuke all the TLBs */
  253. dsb sy
  254. tlbi vmalle1is
  255. dsb ish
  256. isb
  257. /* We're done: fire up the MMU again */
  258. mrs x18, sctlr_el1
  259. orr x18, x18, #SCTLR_ELx_M
  260. msr sctlr_el1, x18
  261. isb
  262. /*
  263. * Invalidate the local I-cache so that any instructions fetched
  264. * speculatively from the PoC are discarded, since they may have
  265. * been dynamically patched at the PoU.
  266. */
  267. ic iallu
  268. dsb nsh
  269. isb
  270. /* Set the flag to zero to indicate that we're all done */
  271. str wzr, [flag_ptr]
  272. ret
  273. /* PUD */
  274. walk_puds:
  275. .if CONFIG_PGTABLE_LEVELS > 3
  276. pte_to_phys cur_pudp, pgd
  277. add end_pudp, cur_pudp, #(PTRS_PER_PUD * 8)
  278. do_pud: __idmap_kpti_get_pgtable_ent pud
  279. tbnz pud, #1, walk_pmds
  280. next_pud:
  281. __idmap_kpti_put_pgtable_ent_ng pud
  282. skip_pud:
  283. add cur_pudp, cur_pudp, 8
  284. cmp cur_pudp, end_pudp
  285. b.ne do_pud
  286. b next_pgd
  287. .else /* CONFIG_PGTABLE_LEVELS <= 3 */
  288. mov pud, pgd
  289. b walk_pmds
  290. next_pud:
  291. b next_pgd
  292. .endif
  293. /* PMD */
  294. walk_pmds:
  295. .if CONFIG_PGTABLE_LEVELS > 2
  296. pte_to_phys cur_pmdp, pud
  297. add end_pmdp, cur_pmdp, #(PTRS_PER_PMD * 8)
  298. do_pmd: __idmap_kpti_get_pgtable_ent pmd
  299. tbnz pmd, #1, walk_ptes
  300. next_pmd:
  301. __idmap_kpti_put_pgtable_ent_ng pmd
  302. skip_pmd:
  303. add cur_pmdp, cur_pmdp, #8
  304. cmp cur_pmdp, end_pmdp
  305. b.ne do_pmd
  306. b next_pud
  307. .else /* CONFIG_PGTABLE_LEVELS <= 2 */
  308. mov pmd, pud
  309. b walk_ptes
  310. next_pmd:
  311. b next_pud
  312. .endif
  313. /* PTE */
  314. walk_ptes:
  315. pte_to_phys cur_ptep, pmd
  316. add end_ptep, cur_ptep, #(PTRS_PER_PTE * 8)
  317. do_pte: __idmap_kpti_get_pgtable_ent pte
  318. __idmap_kpti_put_pgtable_ent_ng pte
  319. skip_pte:
  320. add cur_ptep, cur_ptep, #8
  321. cmp cur_ptep, end_ptep
  322. b.ne do_pte
  323. b next_pmd
  324. /* Secondary CPUs end up here */
  325. __idmap_kpti_secondary:
  326. /* Uninstall swapper before surgery begins */
  327. __idmap_cpu_set_reserved_ttbr1 x18, x17
  328. /* Increment the flag to let the boot CPU we're ready */
  329. 1: ldxr w18, [flag_ptr]
  330. add w18, w18, #1
  331. stxr w17, w18, [flag_ptr]
  332. cbnz w17, 1b
  333. /* Wait for the boot CPU to finish messing around with swapper */
  334. sevl
  335. 1: wfe
  336. ldxr w18, [flag_ptr]
  337. cbnz w18, 1b
  338. /* All done, act like nothing happened */
  339. msr ttbr1_el1, swapper_ttb
  340. isb
  341. ret
  342. .unreq cpu
  343. .unreq num_cpus
  344. .unreq swapper_pa
  345. .unreq swapper_ttb
  346. .unreq flag_ptr
  347. .unreq cur_pgdp
  348. .unreq end_pgdp
  349. .unreq pgd
  350. .unreq cur_pudp
  351. .unreq end_pudp
  352. .unreq pud
  353. .unreq cur_pmdp
  354. .unreq end_pmdp
  355. .unreq pmd
  356. .unreq cur_ptep
  357. .unreq end_ptep
  358. .unreq pte
  359. ENDPROC(idmap_kpti_install_ng_mappings)
  360. .popsection
  361. #endif
  362. /*
  363. * __cpu_setup
  364. *
  365. * Initialise the processor for turning the MMU on. Return in x0 the
  366. * value of the SCTLR_EL1 register.
  367. */
  368. .pushsection ".idmap.text", "awx"
  369. ENTRY(__cpu_setup)
  370. tlbi vmalle1 // Invalidate local TLB
  371. dsb nsh
  372. mov x0, #3 << 20
  373. msr cpacr_el1, x0 // Enable FP/ASIMD
  374. mov x0, #1 << 12 // Reset mdscr_el1 and disable
  375. msr mdscr_el1, x0 // access to the DCC from EL0
  376. isb // Unmask debug exceptions now,
  377. enable_dbg // since this is per-cpu
  378. reset_pmuserenr_el0 x0 // Disable PMU access from EL0
  379. /*
  380. * Memory region attributes for LPAE:
  381. *
  382. * n = AttrIndx[2:0]
  383. * n MAIR
  384. * DEVICE_nGnRnE 000 00000000
  385. * DEVICE_nGnRE 001 00000100
  386. * DEVICE_GRE 010 00001100
  387. * NORMAL_NC 011 01000100
  388. * NORMAL 100 11111111
  389. * NORMAL_WT 101 10111011
  390. */
  391. ldr x5, =MAIR(0x00, MT_DEVICE_nGnRnE) | \
  392. MAIR(0x04, MT_DEVICE_nGnRE) | \
  393. MAIR(0x0c, MT_DEVICE_GRE) | \
  394. MAIR(0x44, MT_NORMAL_NC) | \
  395. MAIR(0xff, MT_NORMAL) | \
  396. MAIR(0xbb, MT_NORMAL_WT)
  397. msr mair_el1, x5
  398. /*
  399. * Prepare SCTLR
  400. */
  401. mov_q x0, SCTLR_EL1_SET
  402. /*
  403. * Set/prepare TCR and TTBR. We use 512GB (39-bit) address range for
  404. * both user and kernel.
  405. */
  406. ldr x10, =TCR_TxSZ(VA_BITS) | TCR_CACHE_FLAGS | TCR_SMP_FLAGS | \
  407. TCR_TG_FLAGS | TCR_KASLR_FLAGS | TCR_ASID16 | \
  408. TCR_TBI0 | TCR_A1
  409. tcr_set_idmap_t0sz x10, x9
  410. /*
  411. * Set the IPS bits in TCR_EL1.
  412. */
  413. tcr_compute_pa_size x10, #TCR_IPS_SHIFT, x5, x6
  414. #ifdef CONFIG_ARM64_HW_AFDBM
  415. /*
  416. * Enable hardware update of the Access Flags bit.
  417. * Hardware dirty bit management is enabled later,
  418. * via capabilities.
  419. */
  420. mrs x9, ID_AA64MMFR1_EL1
  421. and x9, x9, #0xf
  422. cbz x9, 1f
  423. orr x10, x10, #TCR_HA // hardware Access flag update
  424. 1:
  425. #endif /* CONFIG_ARM64_HW_AFDBM */
  426. msr tcr_el1, x10
  427. ret // return to head.S
  428. ENDPROC(__cpu_setup)