proc.S 11 KB

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