1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Based on arch/arm/mm/proc.S
5 * Copyright (C) 2001 Deep Blue Solutions Ltd.
6 * Copyright (C) 2012 ARM Ltd.
7 * Author: Catalin Marinas <catalin.marinas@arm.com>
10 #include <linux/init.h>
11 #include <linux/linkage.h>
12 #include <linux/pgtable.h>
13 #include <linux/cfi_types.h>
14 #include <asm/assembler.h>
15 #include <asm/asm-offsets.h>
16 #include <asm/asm_pointer_auth.h>
17 #include <asm/hwcap.h>
18 #include <asm/kernel-pgtable.h>
19 #include <asm/pgtable-hwdef.h>
20 #include <asm/cpufeature.h>
21 #include <asm/alternative.h>
23 #include <asm/sysreg.h>
25 #ifdef CONFIG_ARM64_64K_PAGES
26 #define TCR_TG_FLAGS TCR_TG0_64K | TCR_TG1_64K
27 #elif defined(CONFIG_ARM64_16K_PAGES)
28 #define TCR_TG_FLAGS TCR_TG0_16K | TCR_TG1_16K
29 #else /* CONFIG_ARM64_4K_PAGES */
30 #define TCR_TG_FLAGS TCR_TG0_4K | TCR_TG1_4K
33 #ifdef CONFIG_RANDOMIZE_BASE
34 #define TCR_KASLR_FLAGS TCR_NFD1
36 #define TCR_KASLR_FLAGS 0
39 /* PTWs cacheable, inner/outer WBWA */
40 #define TCR_CACHE_FLAGS TCR_IRGN_WBWA | TCR_ORGN_WBWA
42 #ifdef CONFIG_KASAN_SW_TAGS
43 #define TCR_KASAN_SW_FLAGS TCR_TBI1 | TCR_TBID1
45 #define TCR_KASAN_SW_FLAGS 0
48 #ifdef CONFIG_KASAN_HW_TAGS
49 #define TCR_MTE_FLAGS TCR_TCMA1 | TCR_TBI1 | TCR_TBID1
50 #elif defined(CONFIG_ARM64_MTE)
52 * The mte_zero_clear_page_tags() implementation uses DC GZVA, which relies on
53 * TBI being enabled at EL1.
55 #define TCR_MTE_FLAGS TCR_TBI1 | TCR_TBID1
57 #define TCR_MTE_FLAGS 0
61 * Default MAIR_EL1. MT_NORMAL_TAGGED is initially mapped as Normal memory and
62 * changed during mte_cpu_setup to Normal Tagged if the system supports MTE.
64 #define MAIR_EL1_SET \
65 (MAIR_ATTRIDX(MAIR_ATTR_DEVICE_nGnRnE, MT_DEVICE_nGnRnE) | \
66 MAIR_ATTRIDX(MAIR_ATTR_DEVICE_nGnRE, MT_DEVICE_nGnRE) | \
67 MAIR_ATTRIDX(MAIR_ATTR_NORMAL_NC, MT_NORMAL_NC) | \
68 MAIR_ATTRIDX(MAIR_ATTR_NORMAL, MT_NORMAL) | \
69 MAIR_ATTRIDX(MAIR_ATTR_NORMAL, MT_NORMAL_TAGGED))
73 * cpu_do_suspend - save CPU registers context
75 * x0: virtual address of context pointer
77 * This must be kept in sync with struct cpu_suspend_ctx in <asm/suspend.h>.
79 SYM_FUNC_START(cpu_do_suspend)
82 mrs x4, contextidr_el1
90 get_this_cpu_offset x12
96 stp x10, x11, [x0, #64]
97 stp x12, x13, [x0, #80]
99 * Save x18 as it may be used as a platform register, e.g. by shadow
104 SYM_FUNC_END(cpu_do_suspend)
107 * cpu_do_resume - restore CPU register context
109 * x0: Address of context pointer
111 SYM_FUNC_START(cpu_do_resume)
113 ldp x4, x5, [x0, #16]
114 ldp x6, x8, [x0, #32]
115 ldp x9, x10, [x0, #48]
116 ldp x11, x12, [x0, #64]
117 ldp x13, x14, [x0, #80]
119 * Restore x18, as it may be used as a platform register, and clear
120 * the buffer to minimize the risk of exposure when used for shadow
127 msr contextidr_el1, x4
130 /* Don't change t0sz here, mask those bits when restoring */
132 bfi x8, x7, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH
139 set_this_cpu_offset x13
142 * Restore oslsr_el1 by writing oslar_el1
145 ubfx x11, x11, #1, #1
147 reset_pmuserenr_el0 x0 // Disable PMU access from EL0
148 reset_amuserenr_el0 x0 // Disable AMU access from EL0
150 alternative_if ARM64_HAS_RAS_EXTN
151 msr_s SYS_DISR_EL1, xzr
152 alternative_else_nop_endif
154 ptrauth_keys_install_kernel_nosync x14, x1, x2, x3
157 SYM_FUNC_END(cpu_do_resume)
160 .pushsection ".idmap.text", "a"
162 .macro __idmap_cpu_set_reserved_ttbr1, tmp1, tmp2
163 adrp \tmp1, reserved_pg_dir
164 phys_to_ttbr \tmp2, \tmp1
165 offset_ttbr1 \tmp2, \tmp1
174 * void idmap_cpu_replace_ttbr1(phys_addr_t ttbr1)
176 * This is the low-level counterpart to cpu_replace_ttbr1, and should not be
177 * called by anything else. It can only be executed from a TTBR0 mapping.
179 SYM_TYPED_FUNC_START(idmap_cpu_replace_ttbr1)
180 __idmap_cpu_set_reserved_ttbr1 x1, x3
187 SYM_FUNC_END(idmap_cpu_replace_ttbr1)
188 SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1)
191 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
193 #define KPTI_NG_PTE_FLAGS (PTE_ATTRINDX(MT_NORMAL) | PTE_TYPE_PAGE | \
194 PTE_AF | PTE_SHARED | PTE_UXN | PTE_WRITE)
196 .pushsection ".idmap.text", "a"
198 .macro pte_to_phys, phys, pte
199 and \phys, \pte, #PTE_ADDR_LOW
200 #ifdef CONFIG_ARM64_PA_BITS_52
201 and \pte, \pte, #PTE_ADDR_HIGH
202 orr \phys, \phys, \pte, lsl #PTE_ADDR_HIGH_SHIFT
206 .macro kpti_mk_tbl_ng, type, num_entries
207 add end_\type\()p, cur_\type\()p, #\num_entries * 8
209 ldr \type, [cur_\type\()p], #8 // Load the entry and advance
210 tbz \type, #0, .Lnext_\type // Skip invalid and
211 tbnz \type, #11, .Lnext_\type // non-global entries
212 orr \type, \type, #PTE_NG // Same bit for blocks and pages
213 str \type, [cur_\type\()p, #-8] // Update the entry
215 tbnz \type, #1, .Lderef_\type
218 cmp cur_\type\()p, end_\type\()p
223 * Dereference the current table entry and map it into the temporary
224 * fixmap slot associated with the current level.
226 .macro kpti_map_pgtbl, type, level
227 str xzr, [temp_pte, #8 * (\level + 2)] // break before make
229 add pte, temp_pte, #PAGE_SIZE * (\level + 2)
235 phys_to_pte pte, cur_\type\()p
236 add cur_\type\()p, temp_pte, #PAGE_SIZE * (\level + 2)
237 orr pte, pte, pte_flags
238 str pte, [temp_pte, #8 * (\level + 2)]
243 * void __kpti_install_ng_mappings(int cpu, int num_secondaries, phys_addr_t temp_pgd,
244 * unsigned long temp_pte_va)
246 * Called exactly once from stop_machine context by each CPU found during boot.
248 .pushsection ".data", "aw", %progbits
249 SYM_DATA(__idmap_kpti_flag, .long 1)
252 SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings)
257 temp_pgd_phys .req x2
274 mov x5, x3 // preserve temp_pte arg
275 mrs swapper_ttb, ttbr1_el1
276 adr_l flag_ptr, __idmap_kpti_flag
278 cbnz cpu, __idmap_kpti_secondary
280 #if CONFIG_PGTABLE_LEVELS > 4
281 stp x29, x30, [sp, #-32]!
283 stp x19, x20, [sp, #16]
286 /* We're the boot CPU. Wait for the others to catch up */
289 ldaxr w17, [flag_ptr]
290 eor w17, w17, num_cpus
293 /* Switch to the temporary page tables on this CPU only */
294 __idmap_cpu_set_reserved_ttbr1 x8, x9
295 offset_ttbr1 temp_pgd_phys, x8
296 msr ttbr1_el1, temp_pgd_phys
300 mov_q pte_flags, KPTI_NG_PTE_FLAGS
302 /* Everybody is enjoying the idmap, so we can rewrite swapper. */
304 #ifdef CONFIG_ARM64_LPA2
306 * If LPA2 support is configured, but 52-bit virtual addressing is not
307 * enabled at runtime, we will fall back to one level of paging less,
308 * and so we have to walk swapper_pg_dir as if we dereferenced its
309 * address from a PGD level entry, and terminate the PGD level loop
312 adrp pgd, swapper_pg_dir // walk &swapper_pg_dir at the next level
313 mov cur_pgdp, end_pgdp // must be equal to terminate the PGD loop
314 alternative_if_not ARM64_HAS_VA52
315 b .Lderef_pgd // skip to the next level
316 alternative_else_nop_endif
318 * LPA2 based 52-bit virtual addressing requires 52-bit physical
319 * addressing to be enabled as well. In this case, the shareability
320 * bits are repurposed as physical address bits, and should not be
323 bic pte_flags, pte_flags, #PTE_SHARED
327 adrp cur_pgdp, swapper_pg_dir
328 kpti_map_pgtbl pgd, -1
329 kpti_mk_tbl_ng pgd, PTRS_PER_PGD
331 /* Ensure all the updated entries are visible to secondary CPUs */
334 /* We're done: fire up swapper_pg_dir again */
335 __idmap_cpu_set_reserved_ttbr1 x8, x9
336 msr ttbr1_el1, swapper_ttb
339 /* Set the flag to zero to indicate that we're all done */
341 #if CONFIG_PGTABLE_LEVELS > 4
342 ldp x19, x20, [sp, #16]
343 ldp x29, x30, [sp], #32
349 .if CONFIG_PGTABLE_LEVELS > 4
351 pte_to_phys cur_p4dp, pgd
352 kpti_map_pgtbl p4d, 0
353 kpti_mk_tbl_ng p4d, PTRS_PER_P4D
355 .else /* CONFIG_PGTABLE_LEVELS <= 4 */
357 .set .Lnext_p4d, .Lnext_pgd
362 .if CONFIG_PGTABLE_LEVELS > 3
364 pte_to_phys cur_pudp, p4d
365 kpti_map_pgtbl pud, 1
366 kpti_mk_tbl_ng pud, PTRS_PER_PUD
368 .else /* CONFIG_PGTABLE_LEVELS <= 3 */
370 .set .Lnext_pud, .Lnext_pgd
375 .if CONFIG_PGTABLE_LEVELS > 2
377 pte_to_phys cur_pmdp, pud
378 kpti_map_pgtbl pmd, 2
379 kpti_mk_tbl_ng pmd, PTRS_PER_PMD
381 .else /* CONFIG_PGTABLE_LEVELS <= 2 */
383 .set .Lnext_pmd, .Lnext_pgd
388 pte_to_phys cur_ptep, pmd
389 kpti_map_pgtbl pte, 3
390 kpti_mk_tbl_ng pte, PTRS_PER_PTE
415 /* Secondary CPUs end up here */
416 __idmap_kpti_secondary:
417 /* Uninstall swapper before surgery begins */
418 __idmap_cpu_set_reserved_ttbr1 x16, x17
420 /* Increment the flag to let the boot CPU we're ready */
421 1: ldxr w16, [flag_ptr]
423 stxr w17, w16, [flag_ptr]
426 /* Wait for the boot CPU to finish messing around with swapper */
432 /* All done, act like nothing happened */
433 msr ttbr1_el1, swapper_ttb
439 SYM_FUNC_END(idmap_kpti_install_ng_mappings)
446 * Initialise the processor for turning the MMU on.
449 * Return in x0 the value of the SCTLR_EL1 register.
451 .pushsection ".idmap.text", "a"
452 SYM_FUNC_START(__cpu_setup)
453 tlbi vmalle1 // Invalidate local TLB
456 msr cpacr_el1, xzr // Reset cpacr_el1
457 mov x1, #1 << 12 // Reset mdscr_el1 and disable
458 msr mdscr_el1, x1 // access to the DCC from EL0
459 reset_pmuserenr_el0 x1 // Disable PMU access from EL0
460 reset_amuserenr_el0 x1 // Disable AMU access from EL0
463 * Default values for VMSA control registers. These will be adjusted
464 * below depending on detected CPU features.
468 mov_q mair, MAIR_EL1_SET
469 mov_q tcr, TCR_T0SZ(IDMAP_VA_BITS) | TCR_T1SZ(VA_BITS_MIN) | TCR_CACHE_FLAGS | \
470 TCR_SHARED | TCR_TG_FLAGS | TCR_KASLR_FLAGS | TCR_ASID16 | \
471 TCR_TBI0 | TCR_A1 | TCR_KASAN_SW_FLAGS | TCR_MTE_FLAGS
473 tcr_clear_errata_bits tcr, x9, x5
475 #ifdef CONFIG_ARM64_VA_BITS_52
476 mov x9, #64 - VA_BITS
477 alternative_if ARM64_HAS_VA52
479 #ifdef CONFIG_ARM64_LPA2
480 orr tcr, tcr, #TCR_DS
482 alternative_else_nop_endif
486 * Set the IPS bits in TCR_EL1.
488 tcr_compute_pa_size tcr, #TCR_IPS_SHIFT, x5, x6
489 #ifdef CONFIG_ARM64_HW_AFDBM
491 * Enable hardware update of the Access Flags bit.
492 * Hardware dirty bit management is enabled later,
495 mrs x9, ID_AA64MMFR1_EL1
496 and x9, x9, ID_AA64MMFR1_EL1_HAFDBS_MASK
498 orr tcr, tcr, #TCR_HA // hardware Access flag update
500 #endif /* CONFIG_ARM64_HW_AFDBM */
504 mrs_s x1, SYS_ID_AA64MMFR3_EL1
505 ubfx x1, x1, #ID_AA64MMFR3_EL1_S1PIE_SHIFT, #4
506 cbz x1, .Lskip_indirection
509 * The PROT_* macros describing the various memory types may resolve to
510 * C expressions if they include the PTE_MAYBE_* macros, and so they
511 * can only be used from C code. The PIE_E* constants below are also
512 * defined in terms of those macros, but will mask out those
513 * PTE_MAYBE_* constants, whether they are set or not. So #define them
514 * as 0x0 here so we can evaluate the PIE_E* constants in asm context.
517 #define PTE_MAYBE_NG 0
518 #define PTE_MAYBE_SHARED 0
521 msr REG_PIRE0_EL1, x0
526 #undef PTE_MAYBE_SHARED
528 mov x0, TCR2_EL1x_PIE
536 mov_q x0, INIT_SCTLR_EL1_MMU_ON
537 ret // return to head.S
541 SYM_FUNC_END(__cpu_setup)