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 <catalin.marinas@arm.com> 8 */ 9 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> 22#include <asm/smp.h> 23#include <asm/sysreg.h> 24 25#ifdef CONFIG_ARM64_64K_PAGES 26#define TCR_TG_FLAGS ((TCR_EL1_TG0_64K << TCR_EL1_TG0_SHIFT) |\ 27 (TCR_EL1_TG1_64K << TCR_EL1_TG1_SHIFT)) 28#elif defined(CONFIG_ARM64_16K_PAGES) 29#define TCR_TG_FLAGS ((TCR_EL1_TG0_16K << TCR_EL1_TG0_SHIFT) |\ 30 (TCR_EL1_TG1_16K << TCR_EL1_TG1_SHIFT)) 31#else /* CONFIG_ARM64_4K_PAGES */ 32#define TCR_TG_FLAGS ((TCR_EL1_TG0_4K << TCR_EL1_TG0_SHIFT) |\ 33 (TCR_EL1_TG1_4K << TCR_EL1_TG1_SHIFT)) 34#endif 35 36#ifdef CONFIG_RANDOMIZE_BASE 37#define TCR_KASLR_FLAGS TCR_EL1_NFD1 38#else 39#define TCR_KASLR_FLAGS 0 40#endif 41 42/* PTWs cacheable, inner/outer WBWA */ 43#define TCR_CACHE_FLAGS TCR_IRGN_WBWA | TCR_ORGN_WBWA 44 45#ifdef CONFIG_KASAN_SW_TAGS 46#define TCR_KASAN_SW_FLAGS TCR_EL1_TBI1 | TCR_EL1_TBID1 47#else 48#define TCR_KASAN_SW_FLAGS 0 49#endif 50 51#ifdef CONFIG_ARM64_MTE 52/* 53 * The mte_zero_clear_page_tags() implementation uses DC GZVA, which relies on 54 * TBI being enabled at EL1. TCMA1 is needed to treat accesses with the 55 * match-all tag (0xF) as Tag Unchecked, irrespective of the SCTLR_EL1.TCF 56 * setting. 57 */ 58#define TCR_MTE_FLAGS TCR_EL1_TCMA1 | TCR_EL1_TBI1 | TCR_EL1_TBID1 59#else 60#define TCR_MTE_FLAGS 0 61#endif 62 63#define TCR_IRGN_WBWA ((TCR_EL1_IRGN0_WBWA << TCR_EL1_IRGN0_SHIFT) |\ 64 (TCR_EL1_IRGN1_WBWA << TCR_EL1_IRGN1_SHIFT)) 65#define TCR_ORGN_WBWA ((TCR_EL1_ORGN0_WBWA << TCR_EL1_ORGN0_SHIFT) |\ 66 (TCR_EL1_ORGN1_WBWA << TCR_EL1_ORGN1_SHIFT)) 67#define TCR_SHARED ((TCR_EL1_SH0_INNER << TCR_EL1_SH0_SHIFT) |\ 68 (TCR_EL1_SH1_INNER << TCR_EL1_SH1_SHIFT)) 69 70/* 71 * Default MAIR_EL1. MT_NORMAL_TAGGED is initially mapped as Normal memory and 72 * changed during mte_cpu_setup to Normal Tagged if the system supports MTE. 73 */ 74#define MAIR_EL1_SET \ 75 (MAIR_ATTRIDX(MAIR_ATTR_DEVICE_nGnRnE, MT_DEVICE_nGnRnE) | \ 76 MAIR_ATTRIDX(MAIR_ATTR_DEVICE_nGnRE, MT_DEVICE_nGnRE) | \ 77 MAIR_ATTRIDX(MAIR_ATTR_NORMAL_NC, MT_NORMAL_NC) | \ 78 MAIR_ATTRIDX(MAIR_ATTR_NORMAL, MT_NORMAL) | \ 79 MAIR_ATTRIDX(MAIR_ATTR_NORMAL, MT_NORMAL_TAGGED)) 80 81#ifdef CONFIG_CPU_PM 82/** 83 * cpu_do_suspend - save CPU registers context 84 * 85 * x0: virtual address of context pointer 86 * 87 * This must be kept in sync with struct cpu_suspend_ctx in <asm/suspend.h>. 88 */ 89SYM_FUNC_START(cpu_do_suspend) 90 mrs x2, tpidr_el0 91 mrs x3, tpidrro_el0 92 mrs x4, contextidr_el1 93 mrs x5, osdlr_el1 94 mrs x6, cpacr_el1 95 mrs x7, tcr_el1 96 mrs x8, vbar_el1 97 mrs x9, mdscr_el1 98 mrs x10, oslsr_el1 99 mrs x11, sctlr_el1 100 get_this_cpu_offset x12 101 mrs x13, sp_el0 102 stp x2, x3, [x0] 103 stp x4, x5, [x0, #16] 104 stp x6, x7, [x0, #32] 105 stp x8, x9, [x0, #48] 106 stp x10, x11, [x0, #64] 107 stp x12, x13, [x0, #80] 108 /* 109 * Save x18 as it may be used as a platform register, e.g. by shadow 110 * call stack. 111 */ 112 str x18, [x0, #96] 113alternative_if ARM64_HAS_TCR2 114 mrs x2, REG_TCR2_EL1 115 str x2, [x0, #104] 116alternative_else_nop_endif 117alternative_if ARM64_SME 118 mrs_s x2, SYS_TPIDR2_EL0 119 str x2, [x0, #112] 120alternative_else_nop_endif 121 ret 122SYM_FUNC_END(cpu_do_suspend) 123 124/** 125 * cpu_do_resume - restore CPU register context 126 * 127 * x0: Address of context pointer 128 */ 129SYM_FUNC_START(cpu_do_resume) 130 ldp x2, x3, [x0] 131 ldp x4, x5, [x0, #16] 132 ldp x6, x8, [x0, #32] 133 ldp x9, x10, [x0, #48] 134 ldp x11, x12, [x0, #64] 135 ldp x13, x14, [x0, #80] 136 /* 137 * Restore x18, as it may be used as a platform register, and clear 138 * the buffer to minimize the risk of exposure when used for shadow 139 * call stack. 140 */ 141 ldr x18, [x0, #96] 142 str xzr, [x0, #96] 143 msr tpidr_el0, x2 144 msr tpidrro_el0, x3 145 msr contextidr_el1, x4 146 msr cpacr_el1, x6 147 148 /* Don't change t0sz here, mask those bits when restoring */ 149 mrs x7, tcr_el1 150 bfi x8, x7, TCR_EL1_T0SZ_SHIFT, TCR_EL1_T0SZ_WIDTH 151 152 msr tcr_el1, x8 153 msr vbar_el1, x9 154 msr mdscr_el1, x10 155alternative_if ARM64_HAS_TCR2 156 ldr x2, [x0, #104] 157 msr REG_TCR2_EL1, x2 158alternative_else_nop_endif 159alternative_if ARM64_SME 160 ldr x2, [x0, #112] 161 msr_s SYS_TPIDR2_EL0, x2 162alternative_else_nop_endif 163 164 msr sctlr_el1, x12 165 set_this_cpu_offset x13 166 msr sp_el0, x14 167 /* 168 * Restore oslsr_el1 by writing oslar_el1 169 */ 170 msr osdlr_el1, x5 171 ubfx x11, x11, #1, #1 172 msr oslar_el1, x11 173 reset_pmuserenr_el0 x0 // Disable PMU access from EL0 174 reset_amuserenr_el0 x0 // Disable AMU access from EL0 175 176alternative_if ARM64_HAS_RAS_EXTN 177 msr_s SYS_DISR_EL1, xzr 178alternative_else_nop_endif 179 180#ifdef CONFIG_ARM64_PSEUDO_NMI 181alternative_if ARM64_HAS_GIC_PRIO_MASKING 182 mov x1, #GIC_PRIO_IRQON 183 msr_s SYS_ICC_PMR_EL1, x1 184alternative_else_nop_endif 185#endif 186 187 ptrauth_keys_install_kernel_nosync x14, x1, x2, x3 188 isb 189 ret 190SYM_FUNC_END(cpu_do_resume) 191#endif 192 193 .pushsection ".idmap.text", "a" 194 195.macro __idmap_cpu_set_reserved_ttbr1, tmp1, tmp2 196 adrp \tmp1, reserved_pg_dir 197 phys_to_ttbr \tmp2, \tmp1 198 offset_ttbr1 \tmp2, \tmp1 199 msr ttbr1_el1, \tmp2 200 isb 201 tlbi vmalle1 202 dsb nsh 203 isb 204.endm 205 206/* 207 * void idmap_cpu_replace_ttbr1(phys_addr_t ttbr1) 208 * 209 * This is the low-level counterpart to cpu_replace_ttbr1, and should not be 210 * called by anything else. It can only be executed from a TTBR0 mapping. 211 */ 212SYM_TYPED_FUNC_START(idmap_cpu_replace_ttbr1) 213 __idmap_cpu_set_reserved_ttbr1 x1, x3 214 215 offset_ttbr1 x0, x3 216 msr ttbr1_el1, x0 217 isb 218 219 ret 220SYM_FUNC_END(idmap_cpu_replace_ttbr1) 221SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1) 222 .popsection 223 224#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 225 226#define KPTI_NG_PTE_FLAGS (PTE_ATTRINDX(MT_NORMAL) | PTE_TYPE_PAGE | \ 227 PTE_AF | PTE_SHARED | PTE_UXN | PTE_WRITE) 228 229 .pushsection ".idmap.text", "a" 230 231 .macro pte_to_phys, phys, pte 232 and \phys, \pte, #PTE_ADDR_LOW 233#ifdef CONFIG_ARM64_PA_BITS_52 234 and \pte, \pte, #PTE_ADDR_HIGH 235 orr \phys, \phys, \pte, lsl #PTE_ADDR_HIGH_SHIFT 236#endif 237 .endm 238 239 .macro kpti_mk_tbl_ng, type, num_entries 240 add end_\type\()p, cur_\type\()p, #\num_entries * 8 241.Ldo_\type: 242 ldr \type, [cur_\type\()p], #8 // Load the entry and advance 243 tbz \type, #0, .Lnext_\type // Skip invalid and 244 tbnz \type, #11, .Lnext_\type // non-global entries 245 orr \type, \type, #PTE_NG // Same bit for blocks and pages 246 str \type, [cur_\type\()p, #-8] // Update the entry 247 .ifnc \type, pte 248 tbnz \type, #1, .Lderef_\type 249 .endif 250.Lnext_\type: 251 cmp cur_\type\()p, end_\type\()p 252 b.ne .Ldo_\type 253 .endm 254 255 /* 256 * Dereference the current table entry and map it into the temporary 257 * fixmap slot associated with the current level. 258 */ 259 .macro kpti_map_pgtbl, type, level 260 str xzr, [temp_pte, #8 * (\level + 2)] // break before make 261 dsb nshst 262 add pte, temp_pte, #PAGE_SIZE * (\level + 2) 263 lsr pte, pte, #12 264 tlbi vaae1, pte 265 dsb nsh 266 isb 267 268 phys_to_pte pte, cur_\type\()p 269 add cur_\type\()p, temp_pte, #PAGE_SIZE * (\level + 2) 270 orr pte, pte, pte_flags 271 str pte, [temp_pte, #8 * (\level + 2)] 272 dsb nshst 273 .endm 274 275/* 276 * void __kpti_install_ng_mappings(int cpu, int num_secondaries, phys_addr_t temp_pgd, 277 * unsigned long temp_pte_va) 278 * 279 * Called exactly once from stop_machine context by each CPU found during boot. 280 */ 281SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings) 282 cpu .req w0 283 temp_pte .req x0 284 num_cpus .req w1 285 pte_flags .req x1 286 temp_pgd_phys .req x2 287 swapper_ttb .req x3 288 flag_ptr .req x4 289 cur_pgdp .req x5 290 end_pgdp .req x6 291 pgd .req x7 292 cur_pudp .req x8 293 end_pudp .req x9 294 cur_pmdp .req x11 295 end_pmdp .req x12 296 cur_ptep .req x14 297 end_ptep .req x15 298 pte .req x16 299 valid .req x17 300 cur_p4dp .req x19 301 end_p4dp .req x20 302 303 mov x5, x3 // preserve temp_pte arg 304 mrs swapper_ttb, ttbr1_el1 305 adr_l flag_ptr, idmap_kpti_bbml3_flag 306 307 cbnz cpu, __idmap_kpti_secondary 308 309#if CONFIG_PGTABLE_LEVELS > 4 310 stp x29, x30, [sp, #-32]! 311 mov x29, sp 312 stp x19, x20, [sp, #16] 313#endif 314 315 /* We're the boot CPU. Wait for the others to catch up */ 316 sevl 3171: wfe 318 ldaxr w17, [flag_ptr] 319 eor w17, w17, num_cpus 320 cbnz w17, 1b 321 322 /* Switch to the temporary page tables on this CPU only */ 323 __idmap_cpu_set_reserved_ttbr1 x8, x9 324 offset_ttbr1 temp_pgd_phys, x8 325 msr ttbr1_el1, temp_pgd_phys 326 isb 327 328 mov temp_pte, x5 329 mov_q pte_flags, KPTI_NG_PTE_FLAGS 330 331 /* Everybody is enjoying the idmap, so we can rewrite swapper. */ 332 333#ifdef CONFIG_ARM64_LPA2 334 /* 335 * If LPA2 support is configured, but 52-bit virtual addressing is not 336 * enabled at runtime, we will fall back to one level of paging less, 337 * and so we have to walk swapper_pg_dir as if we dereferenced its 338 * address from a PGD level entry, and terminate the PGD level loop 339 * right after. 340 */ 341 adrp pgd, swapper_pg_dir // walk &swapper_pg_dir at the next level 342 mov cur_pgdp, end_pgdp // must be equal to terminate the PGD loop 343alternative_if_not ARM64_HAS_VA52 344 b .Lderef_pgd // skip to the next level 345alternative_else_nop_endif 346 /* 347 * LPA2 based 52-bit virtual addressing requires 52-bit physical 348 * addressing to be enabled as well. In this case, the shareability 349 * bits are repurposed as physical address bits, and should not be 350 * set in pte_flags. 351 */ 352 bic pte_flags, pte_flags, #PTE_SHARED 353#endif 354 355 /* PGD */ 356 adrp cur_pgdp, swapper_pg_dir 357 kpti_map_pgtbl pgd, -1 358 kpti_mk_tbl_ng pgd, PTRS_PER_PGD 359 360 /* Ensure all the updated entries are visible to secondary CPUs */ 361 dsb ishst 362 363 /* We're done: fire up swapper_pg_dir again */ 364 __idmap_cpu_set_reserved_ttbr1 x8, x9 365 msr ttbr1_el1, swapper_ttb 366 isb 367 368 /* Set the flag to zero to indicate that we're all done */ 369 str wzr, [flag_ptr] 370#if CONFIG_PGTABLE_LEVELS > 4 371 ldp x19, x20, [sp, #16] 372 ldp x29, x30, [sp], #32 373#endif 374 ret 375 376.Lderef_pgd: 377 /* P4D */ 378 .if CONFIG_PGTABLE_LEVELS > 4 379 p4d .req x30 380 pte_to_phys cur_p4dp, pgd 381 kpti_map_pgtbl p4d, 0 382 kpti_mk_tbl_ng p4d, PTRS_PER_P4D 383 b .Lnext_pgd 384 .else /* CONFIG_PGTABLE_LEVELS <= 4 */ 385 p4d .req pgd 386 .set .Lnext_p4d, .Lnext_pgd 387 .endif 388 389.Lderef_p4d: 390 /* PUD */ 391 .if CONFIG_PGTABLE_LEVELS > 3 392 pud .req x10 393 pte_to_phys cur_pudp, p4d 394 kpti_map_pgtbl pud, 1 395 kpti_mk_tbl_ng pud, PTRS_PER_PUD 396 b .Lnext_p4d 397 .else /* CONFIG_PGTABLE_LEVELS <= 3 */ 398 pud .req pgd 399 .set .Lnext_pud, .Lnext_pgd 400 .endif 401 402.Lderef_pud: 403 /* PMD */ 404 .if CONFIG_PGTABLE_LEVELS > 2 405 pmd .req x13 406 pte_to_phys cur_pmdp, pud 407 kpti_map_pgtbl pmd, 2 408 kpti_mk_tbl_ng pmd, PTRS_PER_PMD 409 b .Lnext_pud 410 .else /* CONFIG_PGTABLE_LEVELS <= 2 */ 411 pmd .req pgd 412 .set .Lnext_pmd, .Lnext_pgd 413 .endif 414 415.Lderef_pmd: 416 /* PTE */ 417 pte_to_phys cur_ptep, pmd 418 kpti_map_pgtbl pte, 3 419 kpti_mk_tbl_ng pte, PTRS_PER_PTE 420 b .Lnext_pmd 421 422 .unreq cpu 423 .unreq temp_pte 424 .unreq num_cpus 425 .unreq pte_flags 426 .unreq temp_pgd_phys 427 .unreq cur_pgdp 428 .unreq end_pgdp 429 .unreq pgd 430 .unreq cur_pudp 431 .unreq end_pudp 432 .unreq pud 433 .unreq cur_pmdp 434 .unreq end_pmdp 435 .unreq pmd 436 .unreq cur_ptep 437 .unreq end_ptep 438 .unreq pte 439 .unreq valid 440 .unreq cur_p4dp 441 .unreq end_p4dp 442 .unreq p4d 443 444 /* Secondary CPUs end up here */ 445__idmap_kpti_secondary: 446 /* Uninstall swapper before surgery begins */ 447 __idmap_cpu_set_reserved_ttbr1 x16, x17 448 b scondary_cpu_wait 449 450 .unreq swapper_ttb 451 .unreq flag_ptr 452SYM_FUNC_END(idmap_kpti_install_ng_mappings) 453 .popsection 454#endif 455 456 .pushsection ".idmap.text", "a" 457SYM_TYPED_FUNC_START(wait_linear_map_split_to_ptes) 458 /* Must be same registers as in idmap_kpti_install_ng_mappings */ 459 swapper_ttb .req x3 460 flag_ptr .req x4 461 462 mrs swapper_ttb, ttbr1_el1 463 adr_l flag_ptr, idmap_kpti_bbml3_flag 464 __idmap_cpu_set_reserved_ttbr1 x16, x17 465 466scondary_cpu_wait: 467 /* Increment the flag to let the boot CPU we're ready */ 4681: ldxr w16, [flag_ptr] 469 add w16, w16, #1 470 stxr w17, w16, [flag_ptr] 471 cbnz w17, 1b 472 473 /* Wait for the boot CPU to finish messing around with swapper */ 474 sevl 4751: wfe 476 ldxr w16, [flag_ptr] 477 cbnz w16, 1b 478 479 /* All done, act like nothing happened */ 480 msr ttbr1_el1, swapper_ttb 481 isb 482 ret 483 484 .unreq swapper_ttb 485 .unreq flag_ptr 486SYM_FUNC_END(wait_linear_map_split_to_ptes) 487 .popsection 488 489/* 490 * __cpu_setup 491 * 492 * Initialise the processor for turning the MMU on. 493 * 494 * Output: 495 * Return in x0 the value of the SCTLR_EL1 register. 496 */ 497 .pushsection ".idmap.text", "a" 498SYM_FUNC_START(__cpu_setup) 499 tlbi vmalle1 // Invalidate local TLB 500 dsb nsh 501 502 msr cpacr_el1, xzr // Reset cpacr_el1 503 mov x1, MDSCR_EL1_TDCC // Reset mdscr_el1 and disable 504 msr mdscr_el1, x1 // access to the DCC from EL0 505 reset_pmuserenr_el0 x1 // Disable PMU access from EL0 506 reset_amuserenr_el0 x1 // Disable AMU access from EL0 507 508 /* 509 * Default values for VMSA control registers. These will be adjusted 510 * below depending on detected CPU features. 511 */ 512 mair .req x17 513 tcr .req x16 514 tcr2 .req x15 515 mov_q mair, MAIR_EL1_SET 516 mov_q tcr, TCR_T0SZ(IDMAP_VA_BITS) | TCR_T1SZ(VA_BITS_MIN) | TCR_CACHE_FLAGS | \ 517 TCR_SHARED | TCR_TG_FLAGS | TCR_KASLR_FLAGS | TCR_EL1_AS | \ 518 TCR_EL1_TBI0 | TCR_EL1_A1 | TCR_KASAN_SW_FLAGS | TCR_MTE_FLAGS 519 mov tcr2, xzr 520 521 tcr_clear_errata_bits tcr, x9, x5 522 523#ifdef CONFIG_ARM64_VA_BITS_52 524 mov x9, #64 - VA_BITS 525alternative_if ARM64_HAS_VA52 526 tcr_set_t1sz tcr, x9 527#ifdef CONFIG_ARM64_LPA2 528 orr tcr, tcr, #TCR_EL1_DS 529#endif 530alternative_else_nop_endif 531#endif 532 533 /* 534 * Set the IPS bits in TCR_EL1. 535 */ 536 tcr_compute_pa_size tcr, #TCR_EL1_IPS_SHIFT, x5, x6 537#ifdef CONFIG_ARM64_HW_AFDBM 538 /* 539 * Enable hardware update of the Access Flags bit. 540 * Hardware dirty bit management is enabled later, 541 * via capabilities. 542 */ 543 mrs x9, ID_AA64MMFR1_EL1 544 ubfx x9, x9, ID_AA64MMFR1_EL1_HAFDBS_SHIFT, #4 545 cbz x9, 1f 546 orr tcr, tcr, #TCR_EL1_HA // hardware Access flag update 547#ifdef CONFIG_ARM64_HAFT 548 cmp x9, ID_AA64MMFR1_EL1_HAFDBS_HAFT 549 b.lt 1f 550 orr tcr2, tcr2, TCR2_EL1_HAFT 551#endif /* CONFIG_ARM64_HAFT */ 5521: 553#endif /* CONFIG_ARM64_HW_AFDBM */ 554 msr mair_el1, mair 555 msr tcr_el1, tcr 556 557 mrs_s x1, SYS_ID_AA64MMFR3_EL1 558 ubfx x1, x1, #ID_AA64MMFR3_EL1_S1PIE_SHIFT, #4 559 cbz x1, .Lskip_indirection 560 561 mov_q x0, PIE_E0_ASM 562 msr REG_PIRE0_EL1, x0 563 mov_q x0, PIE_E1_ASM 564 msr REG_PIR_EL1, x0 565 566 orr tcr2, tcr2, TCR2_EL1_PIE 567 568.Lskip_indirection: 569 570 mrs_s x1, SYS_ID_AA64MMFR3_EL1 571 ubfx x1, x1, #ID_AA64MMFR3_EL1_TCRX_SHIFT, #4 572 cbz x1, 1f 573 msr REG_TCR2_EL1, tcr2 5741: 575 576 /* 577 * Prepare SCTLR 578 */ 579 mov_q x0, INIT_SCTLR_EL1_MMU_ON 580 ret // return to head.S 581 582 .unreq mair 583 .unreq tcr 584 .unreq tcr2 585SYM_FUNC_END(__cpu_setup) 586