1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Based on arch/arm/mm/mmu.c 4 * 5 * Copyright (C) 1995-2005 Russell King 6 * Copyright (C) 2012 ARM Ltd. 7 */ 8 9 #include <linux/cache.h> 10 #include <linux/export.h> 11 #include <linux/kernel.h> 12 #include <linux/errno.h> 13 #include <linux/init.h> 14 #include <linux/ioport.h> 15 #include <linux/kexec.h> 16 #include <linux/libfdt.h> 17 #include <linux/mman.h> 18 #include <linux/nodemask.h> 19 #include <linux/memblock.h> 20 #include <linux/memremap.h> 21 #include <linux/memory.h> 22 #include <linux/fs.h> 23 #include <linux/io.h> 24 #include <linux/mm.h> 25 #include <linux/vmalloc.h> 26 #include <linux/set_memory.h> 27 #include <linux/kfence.h> 28 #include <linux/pkeys.h> 29 #include <linux/mm_inline.h> 30 #include <linux/pagewalk.h> 31 #include <linux/stop_machine.h> 32 33 #include <asm/barrier.h> 34 #include <asm/cputype.h> 35 #include <asm/fixmap.h> 36 #include <asm/kasan.h> 37 #include <asm/kernel-pgtable.h> 38 #include <asm/sections.h> 39 #include <asm/setup.h> 40 #include <linux/sizes.h> 41 #include <asm/tlb.h> 42 #include <asm/mmu_context.h> 43 #include <asm/ptdump.h> 44 #include <asm/tlbflush.h> 45 #include <asm/pgalloc.h> 46 #include <asm/kfence.h> 47 48 #define NO_BLOCK_MAPPINGS BIT(0) 49 #define NO_CONT_MAPPINGS BIT(1) 50 #define NO_EXEC_MAPPINGS BIT(2) /* assumes FEAT_HPDS is not used */ 51 52 DEFINE_STATIC_KEY_FALSE(arm64_ptdump_lock_key); 53 54 u64 kimage_voffset __ro_after_init; 55 EXPORT_SYMBOL(kimage_voffset); 56 57 u32 __boot_cpu_mode[] = { BOOT_CPU_MODE_EL2, BOOT_CPU_MODE_EL1 }; 58 59 static bool rodata_is_rw __ro_after_init = true; 60 61 /* 62 * The booting CPU updates the failed status @__early_cpu_boot_status, 63 * with MMU turned off. 64 */ 65 long __section(".mmuoff.data.write") __early_cpu_boot_status; 66 67 /* 68 * Empty_zero_page is a special page that is used for zero-initialized data 69 * and COW. 70 */ 71 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss; 72 EXPORT_SYMBOL(empty_zero_page); 73 74 static DEFINE_SPINLOCK(swapper_pgdir_lock); 75 static DEFINE_MUTEX(fixmap_lock); 76 77 void noinstr set_swapper_pgd(pgd_t *pgdp, pgd_t pgd) 78 { 79 pgd_t *fixmap_pgdp; 80 81 /* 82 * Don't bother with the fixmap if swapper_pg_dir is still mapped 83 * writable in the kernel mapping. 84 */ 85 if (rodata_is_rw) { 86 WRITE_ONCE(*pgdp, pgd); 87 dsb(ishst); 88 isb(); 89 return; 90 } 91 92 spin_lock(&swapper_pgdir_lock); 93 fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp)); 94 WRITE_ONCE(*fixmap_pgdp, pgd); 95 /* 96 * We need dsb(ishst) here to ensure the page-table-walker sees 97 * our new entry before set_p?d() returns. The fixmap's 98 * flush_tlb_kernel_range() via clear_fixmap() does this for us. 99 */ 100 pgd_clear_fixmap(); 101 spin_unlock(&swapper_pgdir_lock); 102 } 103 104 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 105 unsigned long size, pgprot_t vma_prot) 106 { 107 if (!pfn_is_map_memory(pfn)) 108 return pgprot_noncached(vma_prot); 109 else if (file->f_flags & O_SYNC) 110 return pgprot_writecombine(vma_prot); 111 return vma_prot; 112 } 113 EXPORT_SYMBOL(phys_mem_access_prot); 114 115 static phys_addr_t __init early_pgtable_alloc(enum pgtable_level pgtable_level) 116 { 117 phys_addr_t phys; 118 119 phys = memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE, 0, 120 MEMBLOCK_ALLOC_NOLEAKTRACE); 121 if (!phys) 122 panic("Failed to allocate page table page\n"); 123 124 return phys; 125 } 126 127 bool pgattr_change_is_safe(pteval_t old, pteval_t new) 128 { 129 /* 130 * The following mapping attributes may be updated in live 131 * kernel mappings without the need for break-before-make. 132 */ 133 pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG | 134 PTE_SWBITS_MASK; 135 136 /* creating or taking down mappings is always safe */ 137 if (!pte_valid(__pte(old)) || !pte_valid(__pte(new))) 138 return true; 139 140 /* A live entry's pfn should not change */ 141 if (pte_pfn(__pte(old)) != pte_pfn(__pte(new))) 142 return false; 143 144 /* live contiguous mappings may not be manipulated at all */ 145 if ((old | new) & PTE_CONT) 146 return false; 147 148 /* Transitioning from Non-Global to Global is unsafe */ 149 if (old & ~new & PTE_NG) 150 return false; 151 152 /* 153 * Changing the memory type between Normal and Normal-Tagged is safe 154 * since Tagged is considered a permission attribute from the 155 * mismatched attribute aliases perspective. 156 */ 157 if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 158 (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) && 159 ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 160 (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED))) 161 mask |= PTE_ATTRINDX_MASK; 162 163 return ((old ^ new) & ~mask) == 0; 164 } 165 166 static void init_clear_pgtable(void *table) 167 { 168 clear_page(table); 169 170 /* Ensure the zeroing is observed by page table walks. */ 171 dsb(ishst); 172 } 173 174 static void init_pte(pte_t *ptep, unsigned long addr, unsigned long end, 175 phys_addr_t phys, pgprot_t prot) 176 { 177 do { 178 pte_t old_pte = __ptep_get(ptep); 179 180 /* 181 * Required barriers to make this visible to the table walker 182 * are deferred to the end of alloc_init_cont_pte(). 183 */ 184 __set_pte_nosync(ptep, pfn_pte(__phys_to_pfn(phys), prot)); 185 186 /* 187 * After the PTE entry has been populated once, we 188 * only allow updates to the permission attributes. 189 */ 190 BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), 191 pte_val(__ptep_get(ptep)))); 192 193 phys += PAGE_SIZE; 194 } while (ptep++, addr += PAGE_SIZE, addr != end); 195 } 196 197 static int alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr, 198 unsigned long end, phys_addr_t phys, 199 pgprot_t prot, 200 phys_addr_t (*pgtable_alloc)(enum pgtable_level), 201 int flags) 202 { 203 unsigned long next; 204 pmd_t pmd = READ_ONCE(*pmdp); 205 pte_t *ptep; 206 207 BUG_ON(pmd_leaf(pmd)); 208 if (pmd_none(pmd)) { 209 pmdval_t pmdval = PMD_TYPE_TABLE | PMD_TABLE_UXN | PMD_TABLE_AF; 210 phys_addr_t pte_phys; 211 212 if (flags & NO_EXEC_MAPPINGS) 213 pmdval |= PMD_TABLE_PXN; 214 BUG_ON(!pgtable_alloc); 215 pte_phys = pgtable_alloc(PGTABLE_LEVEL_PTE); 216 if (pte_phys == INVALID_PHYS_ADDR) 217 return -ENOMEM; 218 ptep = pte_set_fixmap(pte_phys); 219 init_clear_pgtable(ptep); 220 ptep += pte_index(addr); 221 __pmd_populate(pmdp, pte_phys, pmdval); 222 } else { 223 BUG_ON(pmd_bad(pmd)); 224 ptep = pte_set_fixmap_offset(pmdp, addr); 225 } 226 227 do { 228 pgprot_t __prot = prot; 229 230 next = pte_cont_addr_end(addr, end); 231 232 /* use a contiguous mapping if the range is suitably aligned */ 233 if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) && 234 (flags & NO_CONT_MAPPINGS) == 0) 235 __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 236 237 init_pte(ptep, addr, next, phys, __prot); 238 239 ptep += pte_index(next) - pte_index(addr); 240 phys += next - addr; 241 } while (addr = next, addr != end); 242 243 /* 244 * Note: barriers and maintenance necessary to clear the fixmap slot 245 * ensure that all previous pgtable writes are visible to the table 246 * walker. 247 */ 248 pte_clear_fixmap(); 249 250 return 0; 251 } 252 253 static int init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end, 254 phys_addr_t phys, pgprot_t prot, 255 phys_addr_t (*pgtable_alloc)(enum pgtable_level), int flags) 256 { 257 unsigned long next; 258 259 do { 260 pmd_t old_pmd = READ_ONCE(*pmdp); 261 262 next = pmd_addr_end(addr, end); 263 264 /* try section mapping first */ 265 if (((addr | next | phys) & ~PMD_MASK) == 0 && 266 (flags & NO_BLOCK_MAPPINGS) == 0) { 267 pmd_set_huge(pmdp, phys, prot); 268 269 /* 270 * After the PMD entry has been populated once, we 271 * only allow updates to the permission attributes. 272 */ 273 BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd), 274 READ_ONCE(pmd_val(*pmdp)))); 275 } else { 276 int ret; 277 278 ret = alloc_init_cont_pte(pmdp, addr, next, phys, prot, 279 pgtable_alloc, flags); 280 if (ret) 281 return ret; 282 283 BUG_ON(pmd_val(old_pmd) != 0 && 284 pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp))); 285 } 286 phys += next - addr; 287 } while (pmdp++, addr = next, addr != end); 288 289 return 0; 290 } 291 292 static int alloc_init_cont_pmd(pud_t *pudp, unsigned long addr, 293 unsigned long end, phys_addr_t phys, 294 pgprot_t prot, 295 phys_addr_t (*pgtable_alloc)(enum pgtable_level), 296 int flags) 297 { 298 int ret; 299 unsigned long next; 300 pud_t pud = READ_ONCE(*pudp); 301 pmd_t *pmdp; 302 303 /* 304 * Check for initial section mappings in the pgd/pud. 305 */ 306 BUG_ON(pud_leaf(pud)); 307 if (pud_none(pud)) { 308 pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_UXN | PUD_TABLE_AF; 309 phys_addr_t pmd_phys; 310 311 if (flags & NO_EXEC_MAPPINGS) 312 pudval |= PUD_TABLE_PXN; 313 BUG_ON(!pgtable_alloc); 314 pmd_phys = pgtable_alloc(PGTABLE_LEVEL_PMD); 315 if (pmd_phys == INVALID_PHYS_ADDR) 316 return -ENOMEM; 317 pmdp = pmd_set_fixmap(pmd_phys); 318 init_clear_pgtable(pmdp); 319 pmdp += pmd_index(addr); 320 __pud_populate(pudp, pmd_phys, pudval); 321 } else { 322 BUG_ON(pud_bad(pud)); 323 pmdp = pmd_set_fixmap_offset(pudp, addr); 324 } 325 326 do { 327 pgprot_t __prot = prot; 328 329 next = pmd_cont_addr_end(addr, end); 330 331 /* use a contiguous mapping if the range is suitably aligned */ 332 if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) && 333 (flags & NO_CONT_MAPPINGS) == 0) 334 __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 335 336 ret = init_pmd(pmdp, addr, next, phys, __prot, pgtable_alloc, flags); 337 if (ret) 338 goto out; 339 340 pmdp += pmd_index(next) - pmd_index(addr); 341 phys += next - addr; 342 } while (addr = next, addr != end); 343 344 out: 345 pmd_clear_fixmap(); 346 347 return ret; 348 } 349 350 static int alloc_init_pud(p4d_t *p4dp, unsigned long addr, unsigned long end, 351 phys_addr_t phys, pgprot_t prot, 352 phys_addr_t (*pgtable_alloc)(enum pgtable_level), 353 int flags) 354 { 355 int ret = 0; 356 unsigned long next; 357 p4d_t p4d = READ_ONCE(*p4dp); 358 pud_t *pudp; 359 360 if (p4d_none(p4d)) { 361 p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN | P4D_TABLE_AF; 362 phys_addr_t pud_phys; 363 364 if (flags & NO_EXEC_MAPPINGS) 365 p4dval |= P4D_TABLE_PXN; 366 BUG_ON(!pgtable_alloc); 367 pud_phys = pgtable_alloc(PGTABLE_LEVEL_PUD); 368 if (pud_phys == INVALID_PHYS_ADDR) 369 return -ENOMEM; 370 pudp = pud_set_fixmap(pud_phys); 371 init_clear_pgtable(pudp); 372 pudp += pud_index(addr); 373 __p4d_populate(p4dp, pud_phys, p4dval); 374 } else { 375 BUG_ON(p4d_bad(p4d)); 376 pudp = pud_set_fixmap_offset(p4dp, addr); 377 } 378 379 do { 380 pud_t old_pud = READ_ONCE(*pudp); 381 382 next = pud_addr_end(addr, end); 383 384 /* 385 * For 4K granule only, attempt to put down a 1GB block 386 */ 387 if (pud_sect_supported() && 388 ((addr | next | phys) & ~PUD_MASK) == 0 && 389 (flags & NO_BLOCK_MAPPINGS) == 0) { 390 pud_set_huge(pudp, phys, prot); 391 392 /* 393 * After the PUD entry has been populated once, we 394 * only allow updates to the permission attributes. 395 */ 396 BUG_ON(!pgattr_change_is_safe(pud_val(old_pud), 397 READ_ONCE(pud_val(*pudp)))); 398 } else { 399 ret = alloc_init_cont_pmd(pudp, addr, next, phys, prot, 400 pgtable_alloc, flags); 401 if (ret) 402 goto out; 403 404 BUG_ON(pud_val(old_pud) != 0 && 405 pud_val(old_pud) != READ_ONCE(pud_val(*pudp))); 406 } 407 phys += next - addr; 408 } while (pudp++, addr = next, addr != end); 409 410 out: 411 pud_clear_fixmap(); 412 413 return ret; 414 } 415 416 static int alloc_init_p4d(pgd_t *pgdp, unsigned long addr, unsigned long end, 417 phys_addr_t phys, pgprot_t prot, 418 phys_addr_t (*pgtable_alloc)(enum pgtable_level), 419 int flags) 420 { 421 int ret; 422 unsigned long next; 423 pgd_t pgd = READ_ONCE(*pgdp); 424 p4d_t *p4dp; 425 426 if (pgd_none(pgd)) { 427 pgdval_t pgdval = PGD_TYPE_TABLE | PGD_TABLE_UXN | PGD_TABLE_AF; 428 phys_addr_t p4d_phys; 429 430 if (flags & NO_EXEC_MAPPINGS) 431 pgdval |= PGD_TABLE_PXN; 432 BUG_ON(!pgtable_alloc); 433 p4d_phys = pgtable_alloc(PGTABLE_LEVEL_P4D); 434 if (p4d_phys == INVALID_PHYS_ADDR) 435 return -ENOMEM; 436 p4dp = p4d_set_fixmap(p4d_phys); 437 init_clear_pgtable(p4dp); 438 p4dp += p4d_index(addr); 439 __pgd_populate(pgdp, p4d_phys, pgdval); 440 } else { 441 BUG_ON(pgd_bad(pgd)); 442 p4dp = p4d_set_fixmap_offset(pgdp, addr); 443 } 444 445 do { 446 p4d_t old_p4d = READ_ONCE(*p4dp); 447 448 next = p4d_addr_end(addr, end); 449 450 ret = alloc_init_pud(p4dp, addr, next, phys, prot, 451 pgtable_alloc, flags); 452 if (ret) 453 goto out; 454 455 BUG_ON(p4d_val(old_p4d) != 0 && 456 p4d_val(old_p4d) != READ_ONCE(p4d_val(*p4dp))); 457 458 phys += next - addr; 459 } while (p4dp++, addr = next, addr != end); 460 461 out: 462 p4d_clear_fixmap(); 463 464 return ret; 465 } 466 467 static int __create_pgd_mapping_locked(pgd_t *pgdir, phys_addr_t phys, 468 unsigned long virt, phys_addr_t size, 469 pgprot_t prot, 470 phys_addr_t (*pgtable_alloc)(enum pgtable_level), 471 int flags) 472 { 473 int ret; 474 unsigned long addr, end, next; 475 pgd_t *pgdp = pgd_offset_pgd(pgdir, virt); 476 477 /* 478 * If the virtual and physical address don't have the same offset 479 * within a page, we cannot map the region as the caller expects. 480 */ 481 if (WARN_ON((phys ^ virt) & ~PAGE_MASK)) 482 return -EINVAL; 483 484 phys &= PAGE_MASK; 485 addr = virt & PAGE_MASK; 486 end = PAGE_ALIGN(virt + size); 487 488 do { 489 next = pgd_addr_end(addr, end); 490 ret = alloc_init_p4d(pgdp, addr, next, phys, prot, pgtable_alloc, 491 flags); 492 if (ret) 493 return ret; 494 phys += next - addr; 495 } while (pgdp++, addr = next, addr != end); 496 497 return 0; 498 } 499 500 static int __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, 501 unsigned long virt, phys_addr_t size, 502 pgprot_t prot, 503 phys_addr_t (*pgtable_alloc)(enum pgtable_level), 504 int flags) 505 { 506 int ret; 507 508 mutex_lock(&fixmap_lock); 509 ret = __create_pgd_mapping_locked(pgdir, phys, virt, size, prot, 510 pgtable_alloc, flags); 511 mutex_unlock(&fixmap_lock); 512 513 return ret; 514 } 515 516 static void early_create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, 517 unsigned long virt, phys_addr_t size, 518 pgprot_t prot, 519 phys_addr_t (*pgtable_alloc)(enum pgtable_level), 520 int flags) 521 { 522 int ret; 523 524 ret = __create_pgd_mapping(pgdir, phys, virt, size, prot, pgtable_alloc, 525 flags); 526 if (ret) 527 panic("Failed to create page tables\n"); 528 } 529 530 static phys_addr_t __pgd_pgtable_alloc(struct mm_struct *mm, gfp_t gfp, 531 enum pgtable_level pgtable_level) 532 { 533 /* Page is zeroed by init_clear_pgtable() so don't duplicate effort. */ 534 struct ptdesc *ptdesc = pagetable_alloc(gfp & ~__GFP_ZERO, 0); 535 phys_addr_t pa; 536 537 if (!ptdesc) 538 return INVALID_PHYS_ADDR; 539 540 pa = page_to_phys(ptdesc_page(ptdesc)); 541 542 switch (pgtable_level) { 543 case PGTABLE_LEVEL_PTE: 544 BUG_ON(!pagetable_pte_ctor(mm, ptdesc)); 545 break; 546 case PGTABLE_LEVEL_PMD: 547 BUG_ON(!pagetable_pmd_ctor(mm, ptdesc)); 548 break; 549 case PGTABLE_LEVEL_PUD: 550 pagetable_pud_ctor(ptdesc); 551 break; 552 case PGTABLE_LEVEL_P4D: 553 pagetable_p4d_ctor(ptdesc); 554 break; 555 case PGTABLE_LEVEL_PGD: 556 VM_WARN_ON(1); 557 break; 558 } 559 560 return pa; 561 } 562 563 static phys_addr_t 564 pgd_pgtable_alloc_init_mm_gfp(enum pgtable_level pgtable_level, gfp_t gfp) 565 { 566 return __pgd_pgtable_alloc(&init_mm, gfp, pgtable_level); 567 } 568 569 static phys_addr_t __maybe_unused 570 pgd_pgtable_alloc_init_mm(enum pgtable_level pgtable_level) 571 { 572 return pgd_pgtable_alloc_init_mm_gfp(pgtable_level, GFP_PGTABLE_KERNEL); 573 } 574 575 static phys_addr_t 576 pgd_pgtable_alloc_special_mm(enum pgtable_level pgtable_level) 577 { 578 return __pgd_pgtable_alloc(NULL, GFP_PGTABLE_KERNEL, pgtable_level); 579 } 580 581 static void split_contpte(pte_t *ptep) 582 { 583 int i; 584 585 ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES); 586 for (i = 0; i < CONT_PTES; i++, ptep++) 587 __set_pte(ptep, pte_mknoncont(__ptep_get(ptep))); 588 } 589 590 static int split_pmd(pmd_t *pmdp, pmd_t pmd, gfp_t gfp, bool to_cont) 591 { 592 pmdval_t tableprot = PMD_TYPE_TABLE | PMD_TABLE_UXN | PMD_TABLE_AF; 593 unsigned long pfn = pmd_pfn(pmd); 594 pgprot_t prot = pmd_pgprot(pmd); 595 phys_addr_t pte_phys; 596 pte_t *ptep; 597 int i; 598 599 pte_phys = pgd_pgtable_alloc_init_mm_gfp(PGTABLE_LEVEL_PTE, gfp); 600 if (pte_phys == INVALID_PHYS_ADDR) 601 return -ENOMEM; 602 ptep = (pte_t *)phys_to_virt(pte_phys); 603 604 if (pgprot_val(prot) & PMD_SECT_PXN) 605 tableprot |= PMD_TABLE_PXN; 606 607 prot = __pgprot((pgprot_val(prot) & ~PTE_TYPE_MASK) | PTE_TYPE_PAGE); 608 if (!pmd_valid(pmd)) 609 prot = pte_pgprot(pte_mkinvalid(pfn_pte(0, prot))); 610 prot = __pgprot(pgprot_val(prot) & ~PTE_CONT); 611 if (to_cont) 612 prot = __pgprot(pgprot_val(prot) | PTE_CONT); 613 614 for (i = 0; i < PTRS_PER_PTE; i++, ptep++, pfn++) 615 __set_pte(ptep, pfn_pte(pfn, prot)); 616 617 /* 618 * Ensure the pte entries are visible to the table walker by the time 619 * the pmd entry that points to the ptes is visible. 620 */ 621 dsb(ishst); 622 __pmd_populate(pmdp, pte_phys, tableprot); 623 624 return 0; 625 } 626 627 static void split_contpmd(pmd_t *pmdp) 628 { 629 int i; 630 631 pmdp = PTR_ALIGN_DOWN(pmdp, sizeof(*pmdp) * CONT_PMDS); 632 for (i = 0; i < CONT_PMDS; i++, pmdp++) 633 set_pmd(pmdp, pmd_mknoncont(pmdp_get(pmdp))); 634 } 635 636 static int split_pud(pud_t *pudp, pud_t pud, gfp_t gfp, bool to_cont) 637 { 638 pudval_t tableprot = PUD_TYPE_TABLE | PUD_TABLE_UXN | PUD_TABLE_AF; 639 unsigned int step = PMD_SIZE >> PAGE_SHIFT; 640 unsigned long pfn = pud_pfn(pud); 641 pgprot_t prot = pud_pgprot(pud); 642 phys_addr_t pmd_phys; 643 pmd_t *pmdp; 644 int i; 645 646 pmd_phys = pgd_pgtable_alloc_init_mm_gfp(PGTABLE_LEVEL_PMD, gfp); 647 if (pmd_phys == INVALID_PHYS_ADDR) 648 return -ENOMEM; 649 pmdp = (pmd_t *)phys_to_virt(pmd_phys); 650 651 if (pgprot_val(prot) & PMD_SECT_PXN) 652 tableprot |= PUD_TABLE_PXN; 653 654 prot = __pgprot((pgprot_val(prot) & ~PMD_TYPE_MASK) | PMD_TYPE_SECT); 655 if (!pud_valid(pud)) 656 prot = pmd_pgprot(pmd_mkinvalid(pfn_pmd(0, prot))); 657 prot = __pgprot(pgprot_val(prot) & ~PTE_CONT); 658 if (to_cont) 659 prot = __pgprot(pgprot_val(prot) | PTE_CONT); 660 661 for (i = 0; i < PTRS_PER_PMD; i++, pmdp++, pfn += step) 662 set_pmd(pmdp, pfn_pmd(pfn, prot)); 663 664 /* 665 * Ensure the pmd entries are visible to the table walker by the time 666 * the pud entry that points to the pmds is visible. 667 */ 668 dsb(ishst); 669 __pud_populate(pudp, pmd_phys, tableprot); 670 671 return 0; 672 } 673 674 static int split_kernel_leaf_mapping_locked(unsigned long addr) 675 { 676 pgd_t *pgdp, pgd; 677 p4d_t *p4dp, p4d; 678 pud_t *pudp, pud; 679 pmd_t *pmdp, pmd; 680 pte_t *ptep, pte; 681 int ret = 0; 682 683 /* 684 * PGD: If addr is PGD aligned then addr already describes a leaf 685 * boundary. If not present then there is nothing to split. 686 */ 687 if (ALIGN_DOWN(addr, PGDIR_SIZE) == addr) 688 goto out; 689 pgdp = pgd_offset_k(addr); 690 pgd = pgdp_get(pgdp); 691 if (!pgd_present(pgd)) 692 goto out; 693 694 /* 695 * P4D: If addr is P4D aligned then addr already describes a leaf 696 * boundary. If not present then there is nothing to split. 697 */ 698 if (ALIGN_DOWN(addr, P4D_SIZE) == addr) 699 goto out; 700 p4dp = p4d_offset(pgdp, addr); 701 p4d = p4dp_get(p4dp); 702 if (!p4d_present(p4d)) 703 goto out; 704 705 /* 706 * PUD: If addr is PUD aligned then addr already describes a leaf 707 * boundary. If not present then there is nothing to split. Otherwise, 708 * if we have a pud leaf, split to contpmd. 709 */ 710 if (ALIGN_DOWN(addr, PUD_SIZE) == addr) 711 goto out; 712 pudp = pud_offset(p4dp, addr); 713 pud = pudp_get(pudp); 714 if (!pud_present(pud)) 715 goto out; 716 if (pud_leaf(pud)) { 717 ret = split_pud(pudp, pud, GFP_PGTABLE_KERNEL, true); 718 if (ret) 719 goto out; 720 } 721 722 /* 723 * CONTPMD: If addr is CONTPMD aligned then addr already describes a 724 * leaf boundary. If not present then there is nothing to split. 725 * Otherwise, if we have a contpmd leaf, split to pmd. 726 */ 727 if (ALIGN_DOWN(addr, CONT_PMD_SIZE) == addr) 728 goto out; 729 pmdp = pmd_offset(pudp, addr); 730 pmd = pmdp_get(pmdp); 731 if (!pmd_present(pmd)) 732 goto out; 733 if (pmd_leaf(pmd)) { 734 if (pmd_cont(pmd)) 735 split_contpmd(pmdp); 736 /* 737 * PMD: If addr is PMD aligned then addr already describes a 738 * leaf boundary. Otherwise, split to contpte. 739 */ 740 if (ALIGN_DOWN(addr, PMD_SIZE) == addr) 741 goto out; 742 ret = split_pmd(pmdp, pmd, GFP_PGTABLE_KERNEL, true); 743 if (ret) 744 goto out; 745 } 746 747 /* 748 * CONTPTE: If addr is CONTPTE aligned then addr already describes a 749 * leaf boundary. If not present then there is nothing to split. 750 * Otherwise, if we have a contpte leaf, split to pte. 751 */ 752 if (ALIGN_DOWN(addr, CONT_PTE_SIZE) == addr) 753 goto out; 754 ptep = pte_offset_kernel(pmdp, addr); 755 pte = __ptep_get(ptep); 756 if (!pte_present(pte)) 757 goto out; 758 if (pte_cont(pte)) 759 split_contpte(ptep); 760 761 out: 762 return ret; 763 } 764 765 static inline bool force_pte_mapping(void) 766 { 767 const bool bbml2 = system_capabilities_finalized() ? 768 system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort(); 769 770 if (debug_pagealloc_enabled()) 771 return true; 772 if (bbml2) 773 return false; 774 return rodata_full || arm64_kfence_can_set_direct_map() || is_realm_world(); 775 } 776 777 static DEFINE_MUTEX(pgtable_split_lock); 778 static bool linear_map_requires_bbml2; 779 780 int split_kernel_leaf_mapping(unsigned long start, unsigned long end) 781 { 782 int ret; 783 784 /* 785 * If the region is within a pte-mapped area, there is no need to try to 786 * split. Additionally, CONFIG_DEBUG_PAGEALLOC and CONFIG_KFENCE may 787 * change permissions from atomic context so for those cases (which are 788 * always pte-mapped), we must not go any further because taking the 789 * mutex below may sleep. Do not call force_pte_mapping() here because 790 * it could return a confusing result if called from a secondary cpu 791 * prior to finalizing caps. Instead, linear_map_requires_bbml2 gives us 792 * what we need. 793 */ 794 if (!linear_map_requires_bbml2 || is_kfence_address((void *)start)) 795 return 0; 796 797 if (!system_supports_bbml2_noabort()) { 798 /* 799 * !BBML2_NOABORT systems should not be trying to change 800 * permissions on anything that is not pte-mapped in the first 801 * place. Just return early and let the permission change code 802 * raise a warning if not already pte-mapped. 803 */ 804 if (system_capabilities_finalized()) 805 return 0; 806 807 /* 808 * Boot-time: split_kernel_leaf_mapping_locked() allocates from 809 * page allocator. Can't split until it's available. 810 */ 811 if (WARN_ON(!page_alloc_available)) 812 return -EBUSY; 813 814 /* 815 * Boot-time: Started secondary cpus but don't know if they 816 * support BBML2_NOABORT yet. Can't allow splitting in this 817 * window in case they don't. 818 */ 819 if (WARN_ON(num_online_cpus() > 1)) 820 return -EBUSY; 821 } 822 823 /* 824 * Ensure start and end are at least page-aligned since this is the 825 * finest granularity we can split to. 826 */ 827 if (start != PAGE_ALIGN(start) || end != PAGE_ALIGN(end)) 828 return -EINVAL; 829 830 mutex_lock(&pgtable_split_lock); 831 lazy_mmu_mode_enable(); 832 833 /* 834 * The split_kernel_leaf_mapping_locked() may sleep, it is not a 835 * problem for ARM64 since ARM64's lazy MMU implementation allows 836 * sleeping. 837 * 838 * Optimize for the common case of splitting out a single page from a 839 * larger mapping. Here we can just split on the "least aligned" of 840 * start and end and this will guarantee that there must also be a split 841 * on the more aligned address since the both addresses must be in the 842 * same contpte block and it must have been split to ptes. 843 */ 844 if (end - start == PAGE_SIZE) { 845 start = __ffs(start) < __ffs(end) ? start : end; 846 ret = split_kernel_leaf_mapping_locked(start); 847 } else { 848 ret = split_kernel_leaf_mapping_locked(start); 849 if (!ret) 850 ret = split_kernel_leaf_mapping_locked(end); 851 } 852 853 lazy_mmu_mode_disable(); 854 mutex_unlock(&pgtable_split_lock); 855 return ret; 856 } 857 858 static int split_to_ptes_pud_entry(pud_t *pudp, unsigned long addr, 859 unsigned long next, struct mm_walk *walk) 860 { 861 gfp_t gfp = *(gfp_t *)walk->private; 862 pud_t pud = pudp_get(pudp); 863 int ret = 0; 864 865 if (pud_leaf(pud)) 866 ret = split_pud(pudp, pud, gfp, false); 867 868 return ret; 869 } 870 871 static int split_to_ptes_pmd_entry(pmd_t *pmdp, unsigned long addr, 872 unsigned long next, struct mm_walk *walk) 873 { 874 gfp_t gfp = *(gfp_t *)walk->private; 875 pmd_t pmd = pmdp_get(pmdp); 876 int ret = 0; 877 878 if (pmd_leaf(pmd)) { 879 if (pmd_cont(pmd)) 880 split_contpmd(pmdp); 881 ret = split_pmd(pmdp, pmd, gfp, false); 882 883 /* 884 * We have split the pmd directly to ptes so there is no need to 885 * visit each pte to check if they are contpte. 886 */ 887 walk->action = ACTION_CONTINUE; 888 } 889 890 return ret; 891 } 892 893 static int split_to_ptes_pte_entry(pte_t *ptep, unsigned long addr, 894 unsigned long next, struct mm_walk *walk) 895 { 896 pte_t pte = __ptep_get(ptep); 897 898 if (pte_cont(pte)) 899 split_contpte(ptep); 900 901 return 0; 902 } 903 904 static const struct mm_walk_ops split_to_ptes_ops = { 905 .pud_entry = split_to_ptes_pud_entry, 906 .pmd_entry = split_to_ptes_pmd_entry, 907 .pte_entry = split_to_ptes_pte_entry, 908 }; 909 910 static int range_split_to_ptes(unsigned long start, unsigned long end, gfp_t gfp) 911 { 912 int ret; 913 914 lazy_mmu_mode_enable(); 915 ret = walk_kernel_page_table_range_lockless(start, end, 916 &split_to_ptes_ops, NULL, &gfp); 917 lazy_mmu_mode_disable(); 918 919 return ret; 920 } 921 922 u32 idmap_kpti_bbml2_flag; 923 924 static void __init init_idmap_kpti_bbml2_flag(void) 925 { 926 WRITE_ONCE(idmap_kpti_bbml2_flag, 1); 927 /* Must be visible to other CPUs before stop_machine() is called. */ 928 smp_mb(); 929 } 930 931 static int __init linear_map_split_to_ptes(void *__unused) 932 { 933 /* 934 * Repainting the linear map must be done by CPU0 (the boot CPU) because 935 * that's the only CPU that we know supports BBML2. The other CPUs will 936 * be held in a waiting area with the idmap active. 937 */ 938 if (!smp_processor_id()) { 939 unsigned long lstart = _PAGE_OFFSET(vabits_actual); 940 unsigned long lend = PAGE_END; 941 unsigned long kstart = (unsigned long)lm_alias(_stext); 942 unsigned long kend = (unsigned long)lm_alias(__init_begin); 943 int ret; 944 945 /* 946 * Wait for all secondary CPUs to be put into the waiting area. 947 */ 948 smp_cond_load_acquire(&idmap_kpti_bbml2_flag, VAL == num_online_cpus()); 949 950 /* 951 * Walk all of the linear map [lstart, lend), except the kernel 952 * linear map alias [kstart, kend), and split all mappings to 953 * PTE. The kernel alias remains static throughout runtime so 954 * can continue to be safely mapped with large mappings. 955 */ 956 ret = range_split_to_ptes(lstart, kstart, GFP_ATOMIC); 957 if (!ret) 958 ret = range_split_to_ptes(kend, lend, GFP_ATOMIC); 959 if (ret) 960 panic("Failed to split linear map\n"); 961 flush_tlb_kernel_range(lstart, lend); 962 963 /* 964 * Relies on dsb in flush_tlb_kernel_range() to avoid reordering 965 * before any page table split operations. 966 */ 967 WRITE_ONCE(idmap_kpti_bbml2_flag, 0); 968 } else { 969 typedef void (wait_split_fn)(void); 970 extern wait_split_fn wait_linear_map_split_to_ptes; 971 wait_split_fn *wait_fn; 972 973 wait_fn = (void *)__pa_symbol(wait_linear_map_split_to_ptes); 974 975 /* 976 * At least one secondary CPU doesn't support BBML2 so cannot 977 * tolerate the size of the live mappings changing. So have the 978 * secondary CPUs wait for the boot CPU to make the changes 979 * with the idmap active and init_mm inactive. 980 */ 981 cpu_install_idmap(); 982 wait_fn(); 983 cpu_uninstall_idmap(); 984 } 985 986 return 0; 987 } 988 989 void __init linear_map_maybe_split_to_ptes(void) 990 { 991 if (linear_map_requires_bbml2 && !system_supports_bbml2_noabort()) { 992 init_idmap_kpti_bbml2_flag(); 993 stop_machine(linear_map_split_to_ptes, NULL, cpu_online_mask); 994 } 995 } 996 997 /* 998 * This function can only be used to modify existing table entries, 999 * without allocating new levels of table. Note that this permits the 1000 * creation of new section or page entries. 1001 */ 1002 void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt, 1003 phys_addr_t size, pgprot_t prot) 1004 { 1005 if (virt < PAGE_OFFSET) { 1006 pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n", 1007 &phys, virt); 1008 return; 1009 } 1010 early_create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 1011 NO_CONT_MAPPINGS); 1012 } 1013 1014 void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, 1015 unsigned long virt, phys_addr_t size, 1016 pgprot_t prot, bool page_mappings_only) 1017 { 1018 int flags = 0; 1019 1020 BUG_ON(mm == &init_mm); 1021 1022 if (page_mappings_only) 1023 flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 1024 1025 early_create_pgd_mapping(mm->pgd, phys, virt, size, prot, 1026 pgd_pgtable_alloc_special_mm, flags); 1027 } 1028 1029 static void update_mapping_prot(phys_addr_t phys, unsigned long virt, 1030 phys_addr_t size, pgprot_t prot) 1031 { 1032 if (virt < PAGE_OFFSET) { 1033 pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n", 1034 &phys, virt); 1035 return; 1036 } 1037 1038 early_create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 1039 NO_CONT_MAPPINGS); 1040 1041 /* flush the TLBs after updating live kernel mappings */ 1042 flush_tlb_kernel_range(virt, virt + size); 1043 } 1044 1045 static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start, 1046 phys_addr_t end, pgprot_t prot, int flags) 1047 { 1048 early_create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start, 1049 prot, early_pgtable_alloc, flags); 1050 } 1051 1052 void __init mark_linear_text_alias_ro(void) 1053 { 1054 /* 1055 * Remove the write permissions from the linear alias of .text/.rodata 1056 */ 1057 update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text), 1058 (unsigned long)__init_begin - (unsigned long)_text, 1059 PAGE_KERNEL_RO); 1060 } 1061 1062 #ifdef CONFIG_KFENCE 1063 1064 bool __ro_after_init kfence_early_init = !!CONFIG_KFENCE_SAMPLE_INTERVAL; 1065 1066 /* early_param() will be parsed before map_mem() below. */ 1067 static int __init parse_kfence_early_init(char *arg) 1068 { 1069 int val; 1070 1071 if (get_option(&arg, &val)) 1072 kfence_early_init = !!val; 1073 return 0; 1074 } 1075 early_param("kfence.sample_interval", parse_kfence_early_init); 1076 1077 static phys_addr_t __init arm64_kfence_alloc_pool(void) 1078 { 1079 phys_addr_t kfence_pool; 1080 1081 if (!kfence_early_init) 1082 return 0; 1083 1084 kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE); 1085 if (!kfence_pool) { 1086 pr_err("failed to allocate kfence pool\n"); 1087 kfence_early_init = false; 1088 return 0; 1089 } 1090 1091 /* Temporarily mark as NOMAP. */ 1092 memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE); 1093 1094 return kfence_pool; 1095 } 1096 1097 static void __init arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) 1098 { 1099 if (!kfence_pool) 1100 return; 1101 1102 /* KFENCE pool needs page-level mapping. */ 1103 __map_memblock(pgdp, kfence_pool, kfence_pool + KFENCE_POOL_SIZE, 1104 pgprot_tagged(PAGE_KERNEL), 1105 NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS); 1106 memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE); 1107 __kfence_pool = phys_to_virt(kfence_pool); 1108 } 1109 1110 bool arch_kfence_init_pool(void) 1111 { 1112 unsigned long start = (unsigned long)__kfence_pool; 1113 unsigned long end = start + KFENCE_POOL_SIZE; 1114 int ret; 1115 1116 /* Exit early if we know the linear map is already pte-mapped. */ 1117 if (force_pte_mapping()) 1118 return true; 1119 1120 /* Kfence pool is already pte-mapped for the early init case. */ 1121 if (kfence_early_init) 1122 return true; 1123 1124 mutex_lock(&pgtable_split_lock); 1125 ret = range_split_to_ptes(start, end, GFP_PGTABLE_KERNEL); 1126 mutex_unlock(&pgtable_split_lock); 1127 1128 /* 1129 * Since the system supports bbml2_noabort, tlb invalidation is not 1130 * required here; the pgtable mappings have been split to pte but larger 1131 * entries may safely linger in the TLB. 1132 */ 1133 1134 return !ret; 1135 } 1136 #else /* CONFIG_KFENCE */ 1137 1138 static inline phys_addr_t arm64_kfence_alloc_pool(void) { return 0; } 1139 static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) { } 1140 1141 #endif /* CONFIG_KFENCE */ 1142 1143 static void __init map_mem(pgd_t *pgdp) 1144 { 1145 static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN); 1146 phys_addr_t kernel_start = __pa_symbol(_text); 1147 phys_addr_t kernel_end = __pa_symbol(__init_begin); 1148 phys_addr_t start, end; 1149 phys_addr_t early_kfence_pool; 1150 int flags = NO_EXEC_MAPPINGS; 1151 u64 i; 1152 1153 /* 1154 * Setting hierarchical PXNTable attributes on table entries covering 1155 * the linear region is only possible if it is guaranteed that no table 1156 * entries at any level are being shared between the linear region and 1157 * the vmalloc region. Check whether this is true for the PGD level, in 1158 * which case it is guaranteed to be true for all other levels as well. 1159 * (Unless we are running with support for LPA2, in which case the 1160 * entire reduced VA space is covered by a single pgd_t which will have 1161 * been populated without the PXNTable attribute by the time we get here.) 1162 */ 1163 BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end) && 1164 pgd_index(_PAGE_OFFSET(VA_BITS_MIN)) != PTRS_PER_PGD - 1); 1165 1166 early_kfence_pool = arm64_kfence_alloc_pool(); 1167 1168 linear_map_requires_bbml2 = !force_pte_mapping() && can_set_direct_map(); 1169 1170 if (force_pte_mapping()) 1171 flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 1172 1173 /* 1174 * Take care not to create a writable alias for the 1175 * read-only text and rodata sections of the kernel image. 1176 * So temporarily mark them as NOMAP to skip mappings in 1177 * the following for-loop 1178 */ 1179 memblock_mark_nomap(kernel_start, kernel_end - kernel_start); 1180 1181 /* map all the memory banks */ 1182 for_each_mem_range(i, &start, &end) { 1183 if (start >= end) 1184 break; 1185 /* 1186 * The linear map must allow allocation tags reading/writing 1187 * if MTE is present. Otherwise, it has the same attributes as 1188 * PAGE_KERNEL. 1189 */ 1190 __map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL), 1191 flags); 1192 } 1193 1194 /* 1195 * Map the linear alias of the [_text, __init_begin) interval 1196 * as non-executable now, and remove the write permission in 1197 * mark_linear_text_alias_ro() below (which will be called after 1198 * alternative patching has completed). This makes the contents 1199 * of the region accessible to subsystems such as hibernate, 1200 * but protects it from inadvertent modification or execution. 1201 * Note that contiguous mappings cannot be remapped in this way, 1202 * so we should avoid them here. 1203 */ 1204 __map_memblock(pgdp, kernel_start, kernel_end, 1205 PAGE_KERNEL, NO_CONT_MAPPINGS); 1206 memblock_clear_nomap(kernel_start, kernel_end - kernel_start); 1207 arm64_kfence_map_pool(early_kfence_pool, pgdp); 1208 } 1209 1210 void mark_rodata_ro(void) 1211 { 1212 unsigned long section_size; 1213 1214 /* 1215 * mark .rodata as read only. Use __init_begin rather than __end_rodata 1216 * to cover NOTES and EXCEPTION_TABLE. 1217 */ 1218 section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata; 1219 WRITE_ONCE(rodata_is_rw, false); 1220 update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata, 1221 section_size, PAGE_KERNEL_RO); 1222 /* mark the range between _text and _stext as read only. */ 1223 update_mapping_prot(__pa_symbol(_text), (unsigned long)_text, 1224 (unsigned long)_stext - (unsigned long)_text, 1225 PAGE_KERNEL_RO); 1226 } 1227 1228 static void __init declare_vma(struct vm_struct *vma, 1229 void *va_start, void *va_end, 1230 unsigned long vm_flags) 1231 { 1232 phys_addr_t pa_start = __pa_symbol(va_start); 1233 unsigned long size = va_end - va_start; 1234 1235 BUG_ON(!PAGE_ALIGNED(pa_start)); 1236 BUG_ON(!PAGE_ALIGNED(size)); 1237 1238 if (!(vm_flags & VM_NO_GUARD)) 1239 size += PAGE_SIZE; 1240 1241 vma->addr = va_start; 1242 vma->phys_addr = pa_start; 1243 vma->size = size; 1244 vma->flags = VM_MAP | vm_flags; 1245 vma->caller = __builtin_return_address(0); 1246 1247 vm_area_add_early(vma); 1248 } 1249 1250 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 1251 #define KPTI_NG_TEMP_VA (-(1UL << PMD_SHIFT)) 1252 1253 static phys_addr_t kpti_ng_temp_alloc __initdata; 1254 1255 static phys_addr_t __init kpti_ng_pgd_alloc(enum pgtable_level pgtable_level) 1256 { 1257 kpti_ng_temp_alloc -= PAGE_SIZE; 1258 return kpti_ng_temp_alloc; 1259 } 1260 1261 static int __init __kpti_install_ng_mappings(void *__unused) 1262 { 1263 typedef void (kpti_remap_fn)(int, int, phys_addr_t, unsigned long); 1264 extern kpti_remap_fn idmap_kpti_install_ng_mappings; 1265 kpti_remap_fn *remap_fn; 1266 1267 int cpu = smp_processor_id(); 1268 int levels = CONFIG_PGTABLE_LEVELS; 1269 int order = order_base_2(levels); 1270 u64 kpti_ng_temp_pgd_pa = 0; 1271 pgd_t *kpti_ng_temp_pgd; 1272 u64 alloc = 0; 1273 1274 if (levels == 5 && !pgtable_l5_enabled()) 1275 levels = 4; 1276 else if (levels == 4 && !pgtable_l4_enabled()) 1277 levels = 3; 1278 1279 remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings); 1280 1281 if (!cpu) { 1282 int ret; 1283 1284 alloc = __get_free_pages(GFP_ATOMIC | __GFP_ZERO, order); 1285 kpti_ng_temp_pgd = (pgd_t *)(alloc + (levels - 1) * PAGE_SIZE); 1286 kpti_ng_temp_alloc = kpti_ng_temp_pgd_pa = __pa(kpti_ng_temp_pgd); 1287 1288 // 1289 // Create a minimal page table hierarchy that permits us to map 1290 // the swapper page tables temporarily as we traverse them. 1291 // 1292 // The physical pages are laid out as follows: 1293 // 1294 // +--------+-/-------+-/------ +-/------ +-\\\--------+ 1295 // : PTE[] : | PMD[] : | PUD[] : | P4D[] : ||| PGD[] : 1296 // +--------+-\-------+-\------ +-\------ +-///--------+ 1297 // ^ 1298 // The first page is mapped into this hierarchy at a PMD_SHIFT 1299 // aligned virtual address, so that we can manipulate the PTE 1300 // level entries while the mapping is active. The first entry 1301 // covers the PTE[] page itself, the remaining entries are free 1302 // to be used as a ad-hoc fixmap. 1303 // 1304 ret = __create_pgd_mapping_locked(kpti_ng_temp_pgd, __pa(alloc), 1305 KPTI_NG_TEMP_VA, PAGE_SIZE, PAGE_KERNEL, 1306 kpti_ng_pgd_alloc, 0); 1307 if (ret) 1308 panic("Failed to create page tables\n"); 1309 } 1310 1311 cpu_install_idmap(); 1312 remap_fn(cpu, num_online_cpus(), kpti_ng_temp_pgd_pa, KPTI_NG_TEMP_VA); 1313 cpu_uninstall_idmap(); 1314 1315 if (!cpu) { 1316 free_pages(alloc, order); 1317 arm64_use_ng_mappings = true; 1318 } 1319 1320 return 0; 1321 } 1322 1323 void __init kpti_install_ng_mappings(void) 1324 { 1325 /* Check whether KPTI is going to be used */ 1326 if (!arm64_kernel_unmapped_at_el0()) 1327 return; 1328 1329 /* 1330 * We don't need to rewrite the page-tables if either we've done 1331 * it already or we have KASLR enabled and therefore have not 1332 * created any global mappings at all. 1333 */ 1334 if (arm64_use_ng_mappings) 1335 return; 1336 1337 init_idmap_kpti_bbml2_flag(); 1338 stop_machine(__kpti_install_ng_mappings, NULL, cpu_online_mask); 1339 } 1340 1341 static pgprot_t __init kernel_exec_prot(void) 1342 { 1343 return rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; 1344 } 1345 1346 static int __init map_entry_trampoline(void) 1347 { 1348 int i; 1349 1350 if (!arm64_kernel_unmapped_at_el0()) 1351 return 0; 1352 1353 pgprot_t prot = kernel_exec_prot(); 1354 phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start); 1355 1356 /* The trampoline is always mapped and can therefore be global */ 1357 pgprot_val(prot) &= ~PTE_NG; 1358 1359 /* Map only the text into the trampoline page table */ 1360 memset(tramp_pg_dir, 0, PGD_SIZE); 1361 early_create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, 1362 entry_tramp_text_size(), prot, 1363 pgd_pgtable_alloc_init_mm, NO_BLOCK_MAPPINGS); 1364 1365 /* Map both the text and data into the kernel page table */ 1366 for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++) 1367 __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i, 1368 pa_start + i * PAGE_SIZE, prot); 1369 1370 if (IS_ENABLED(CONFIG_RELOCATABLE)) 1371 __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i, 1372 pa_start + i * PAGE_SIZE, PAGE_KERNEL_RO); 1373 1374 return 0; 1375 } 1376 core_initcall(map_entry_trampoline); 1377 #endif 1378 1379 /* 1380 * Declare the VMA areas for the kernel 1381 */ 1382 static void __init declare_kernel_vmas(void) 1383 { 1384 static struct vm_struct vmlinux_seg[KERNEL_SEGMENT_COUNT]; 1385 1386 declare_vma(&vmlinux_seg[0], _text, _etext, VM_NO_GUARD); 1387 declare_vma(&vmlinux_seg[1], __start_rodata, __inittext_begin, VM_NO_GUARD); 1388 declare_vma(&vmlinux_seg[2], __inittext_begin, __inittext_end, VM_NO_GUARD); 1389 declare_vma(&vmlinux_seg[3], __initdata_begin, __initdata_end, VM_NO_GUARD); 1390 declare_vma(&vmlinux_seg[4], _data, _end, 0); 1391 } 1392 1393 void __pi_map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa, 1394 pgprot_t prot, int level, pte_t *tbl, bool may_use_cont, 1395 u64 va_offset); 1396 1397 static u8 idmap_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init, 1398 kpti_bbml2_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init; 1399 1400 static void __init create_idmap(void) 1401 { 1402 phys_addr_t start = __pa_symbol(__idmap_text_start); 1403 phys_addr_t end = __pa_symbol(__idmap_text_end); 1404 phys_addr_t ptep = __pa_symbol(idmap_ptes); 1405 1406 __pi_map_range(&ptep, start, end, start, PAGE_KERNEL_ROX, 1407 IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false, 1408 __phys_to_virt(ptep) - ptep); 1409 1410 if (linear_map_requires_bbml2 || 1411 (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0) && !arm64_use_ng_mappings)) { 1412 phys_addr_t pa = __pa_symbol(&idmap_kpti_bbml2_flag); 1413 1414 /* 1415 * The KPTI G-to-nG conversion code needs a read-write mapping 1416 * of its synchronization flag in the ID map. This is also used 1417 * when splitting the linear map to ptes if a secondary CPU 1418 * doesn't support bbml2. 1419 */ 1420 ptep = __pa_symbol(kpti_bbml2_ptes); 1421 __pi_map_range(&ptep, pa, pa + sizeof(u32), pa, PAGE_KERNEL, 1422 IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false, 1423 __phys_to_virt(ptep) - ptep); 1424 } 1425 } 1426 1427 void __init paging_init(void) 1428 { 1429 map_mem(swapper_pg_dir); 1430 1431 memblock_allow_resize(); 1432 1433 create_idmap(); 1434 declare_kernel_vmas(); 1435 } 1436 1437 #ifdef CONFIG_MEMORY_HOTPLUG 1438 static void free_hotplug_page_range(struct page *page, size_t size, 1439 struct vmem_altmap *altmap) 1440 { 1441 if (altmap) { 1442 vmem_altmap_free(altmap, size >> PAGE_SHIFT); 1443 } else { 1444 WARN_ON(PageReserved(page)); 1445 __free_pages(page, get_order(size)); 1446 } 1447 } 1448 1449 static void free_hotplug_pgtable_page(struct page *page) 1450 { 1451 free_hotplug_page_range(page, PAGE_SIZE, NULL); 1452 } 1453 1454 static bool pgtable_range_aligned(unsigned long start, unsigned long end, 1455 unsigned long floor, unsigned long ceiling, 1456 unsigned long mask) 1457 { 1458 start &= mask; 1459 if (start < floor) 1460 return false; 1461 1462 if (ceiling) { 1463 ceiling &= mask; 1464 if (!ceiling) 1465 return false; 1466 } 1467 1468 if (end - 1 > ceiling - 1) 1469 return false; 1470 return true; 1471 } 1472 1473 static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, 1474 unsigned long end, bool free_mapped, 1475 struct vmem_altmap *altmap) 1476 { 1477 pte_t *ptep, pte; 1478 1479 do { 1480 ptep = pte_offset_kernel(pmdp, addr); 1481 pte = __ptep_get(ptep); 1482 if (pte_none(pte)) 1483 continue; 1484 1485 WARN_ON(!pte_present(pte)); 1486 __pte_clear(&init_mm, addr, ptep); 1487 if (free_mapped) { 1488 /* CONT blocks are not supported in the vmemmap */ 1489 WARN_ON(pte_cont(pte)); 1490 flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 1491 free_hotplug_page_range(pte_page(pte), 1492 PAGE_SIZE, altmap); 1493 } 1494 /* unmap_hotplug_range() flushes TLB for !free_mapped */ 1495 } while (addr += PAGE_SIZE, addr < end); 1496 } 1497 1498 static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr, 1499 unsigned long end, bool free_mapped, 1500 struct vmem_altmap *altmap) 1501 { 1502 unsigned long next; 1503 pmd_t *pmdp, pmd; 1504 1505 do { 1506 next = pmd_addr_end(addr, end); 1507 pmdp = pmd_offset(pudp, addr); 1508 pmd = READ_ONCE(*pmdp); 1509 if (pmd_none(pmd)) 1510 continue; 1511 1512 WARN_ON(!pmd_present(pmd)); 1513 if (pmd_leaf(pmd)) { 1514 pmd_clear(pmdp); 1515 if (free_mapped) { 1516 /* CONT blocks are not supported in the vmemmap */ 1517 WARN_ON(pmd_cont(pmd)); 1518 flush_tlb_kernel_range(addr, addr + PMD_SIZE); 1519 free_hotplug_page_range(pmd_page(pmd), 1520 PMD_SIZE, altmap); 1521 } 1522 /* unmap_hotplug_range() flushes TLB for !free_mapped */ 1523 continue; 1524 } 1525 WARN_ON(!pmd_table(pmd)); 1526 unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap); 1527 } while (addr = next, addr < end); 1528 } 1529 1530 static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr, 1531 unsigned long end, bool free_mapped, 1532 struct vmem_altmap *altmap) 1533 { 1534 unsigned long next; 1535 pud_t *pudp, pud; 1536 1537 do { 1538 next = pud_addr_end(addr, end); 1539 pudp = pud_offset(p4dp, addr); 1540 pud = READ_ONCE(*pudp); 1541 if (pud_none(pud)) 1542 continue; 1543 1544 WARN_ON(!pud_present(pud)); 1545 if (pud_leaf(pud)) { 1546 pud_clear(pudp); 1547 if (free_mapped) { 1548 flush_tlb_kernel_range(addr, addr + PUD_SIZE); 1549 free_hotplug_page_range(pud_page(pud), 1550 PUD_SIZE, altmap); 1551 } 1552 /* unmap_hotplug_range() flushes TLB for !free_mapped */ 1553 continue; 1554 } 1555 WARN_ON(!pud_table(pud)); 1556 unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap); 1557 } while (addr = next, addr < end); 1558 } 1559 1560 static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr, 1561 unsigned long end, bool free_mapped, 1562 struct vmem_altmap *altmap) 1563 { 1564 unsigned long next; 1565 p4d_t *p4dp, p4d; 1566 1567 do { 1568 next = p4d_addr_end(addr, end); 1569 p4dp = p4d_offset(pgdp, addr); 1570 p4d = READ_ONCE(*p4dp); 1571 if (p4d_none(p4d)) 1572 continue; 1573 1574 WARN_ON(!p4d_present(p4d)); 1575 unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap); 1576 } while (addr = next, addr < end); 1577 } 1578 1579 static void unmap_hotplug_range(unsigned long addr, unsigned long end, 1580 bool free_mapped, struct vmem_altmap *altmap) 1581 { 1582 unsigned long start = addr; 1583 unsigned long next; 1584 pgd_t *pgdp, pgd; 1585 1586 /* 1587 * altmap can only be used as vmemmap mapping backing memory. 1588 * In case the backing memory itself is not being freed, then 1589 * altmap is irrelevant. Warn about this inconsistency when 1590 * encountered. 1591 */ 1592 WARN_ON(!free_mapped && altmap); 1593 1594 do { 1595 next = pgd_addr_end(addr, end); 1596 pgdp = pgd_offset_k(addr); 1597 pgd = READ_ONCE(*pgdp); 1598 if (pgd_none(pgd)) 1599 continue; 1600 1601 WARN_ON(!pgd_present(pgd)); 1602 unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap); 1603 } while (addr = next, addr < end); 1604 1605 if (!free_mapped) 1606 flush_tlb_kernel_range(start, end); 1607 } 1608 1609 static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr, 1610 unsigned long end, unsigned long floor, 1611 unsigned long ceiling) 1612 { 1613 pte_t *ptep, pte; 1614 unsigned long i, start = addr; 1615 1616 do { 1617 ptep = pte_offset_kernel(pmdp, addr); 1618 pte = __ptep_get(ptep); 1619 1620 /* 1621 * This is just a sanity check here which verifies that 1622 * pte clearing has been done by earlier unmap loops. 1623 */ 1624 WARN_ON(!pte_none(pte)); 1625 } while (addr += PAGE_SIZE, addr < end); 1626 1627 if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK)) 1628 return; 1629 1630 /* 1631 * Check whether we can free the pte page if the rest of the 1632 * entries are empty. Overlap with other regions have been 1633 * handled by the floor/ceiling check. 1634 */ 1635 ptep = pte_offset_kernel(pmdp, 0UL); 1636 for (i = 0; i < PTRS_PER_PTE; i++) { 1637 if (!pte_none(__ptep_get(&ptep[i]))) 1638 return; 1639 } 1640 1641 pmd_clear(pmdp); 1642 __flush_tlb_kernel_pgtable(start); 1643 free_hotplug_pgtable_page(virt_to_page(ptep)); 1644 } 1645 1646 static void free_empty_pmd_table(pud_t *pudp, unsigned long addr, 1647 unsigned long end, unsigned long floor, 1648 unsigned long ceiling) 1649 { 1650 pmd_t *pmdp, pmd; 1651 unsigned long i, next, start = addr; 1652 1653 do { 1654 next = pmd_addr_end(addr, end); 1655 pmdp = pmd_offset(pudp, addr); 1656 pmd = READ_ONCE(*pmdp); 1657 if (pmd_none(pmd)) 1658 continue; 1659 1660 WARN_ON(!pmd_present(pmd) || !pmd_table(pmd)); 1661 free_empty_pte_table(pmdp, addr, next, floor, ceiling); 1662 } while (addr = next, addr < end); 1663 1664 if (CONFIG_PGTABLE_LEVELS <= 2) 1665 return; 1666 1667 if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK)) 1668 return; 1669 1670 /* 1671 * Check whether we can free the pmd page if the rest of the 1672 * entries are empty. Overlap with other regions have been 1673 * handled by the floor/ceiling check. 1674 */ 1675 pmdp = pmd_offset(pudp, 0UL); 1676 for (i = 0; i < PTRS_PER_PMD; i++) { 1677 if (!pmd_none(READ_ONCE(pmdp[i]))) 1678 return; 1679 } 1680 1681 pud_clear(pudp); 1682 __flush_tlb_kernel_pgtable(start); 1683 free_hotplug_pgtable_page(virt_to_page(pmdp)); 1684 } 1685 1686 static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr, 1687 unsigned long end, unsigned long floor, 1688 unsigned long ceiling) 1689 { 1690 pud_t *pudp, pud; 1691 unsigned long i, next, start = addr; 1692 1693 do { 1694 next = pud_addr_end(addr, end); 1695 pudp = pud_offset(p4dp, addr); 1696 pud = READ_ONCE(*pudp); 1697 if (pud_none(pud)) 1698 continue; 1699 1700 WARN_ON(!pud_present(pud) || !pud_table(pud)); 1701 free_empty_pmd_table(pudp, addr, next, floor, ceiling); 1702 } while (addr = next, addr < end); 1703 1704 if (!pgtable_l4_enabled()) 1705 return; 1706 1707 if (!pgtable_range_aligned(start, end, floor, ceiling, P4D_MASK)) 1708 return; 1709 1710 /* 1711 * Check whether we can free the pud page if the rest of the 1712 * entries are empty. Overlap with other regions have been 1713 * handled by the floor/ceiling check. 1714 */ 1715 pudp = pud_offset(p4dp, 0UL); 1716 for (i = 0; i < PTRS_PER_PUD; i++) { 1717 if (!pud_none(READ_ONCE(pudp[i]))) 1718 return; 1719 } 1720 1721 p4d_clear(p4dp); 1722 __flush_tlb_kernel_pgtable(start); 1723 free_hotplug_pgtable_page(virt_to_page(pudp)); 1724 } 1725 1726 static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr, 1727 unsigned long end, unsigned long floor, 1728 unsigned long ceiling) 1729 { 1730 p4d_t *p4dp, p4d; 1731 unsigned long i, next, start = addr; 1732 1733 do { 1734 next = p4d_addr_end(addr, end); 1735 p4dp = p4d_offset(pgdp, addr); 1736 p4d = READ_ONCE(*p4dp); 1737 if (p4d_none(p4d)) 1738 continue; 1739 1740 WARN_ON(!p4d_present(p4d)); 1741 free_empty_pud_table(p4dp, addr, next, floor, ceiling); 1742 } while (addr = next, addr < end); 1743 1744 if (!pgtable_l5_enabled()) 1745 return; 1746 1747 if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK)) 1748 return; 1749 1750 /* 1751 * Check whether we can free the p4d page if the rest of the 1752 * entries are empty. Overlap with other regions have been 1753 * handled by the floor/ceiling check. 1754 */ 1755 p4dp = p4d_offset(pgdp, 0UL); 1756 for (i = 0; i < PTRS_PER_P4D; i++) { 1757 if (!p4d_none(READ_ONCE(p4dp[i]))) 1758 return; 1759 } 1760 1761 pgd_clear(pgdp); 1762 __flush_tlb_kernel_pgtable(start); 1763 free_hotplug_pgtable_page(virt_to_page(p4dp)); 1764 } 1765 1766 static void free_empty_tables(unsigned long addr, unsigned long end, 1767 unsigned long floor, unsigned long ceiling) 1768 { 1769 unsigned long next; 1770 pgd_t *pgdp, pgd; 1771 1772 do { 1773 next = pgd_addr_end(addr, end); 1774 pgdp = pgd_offset_k(addr); 1775 pgd = READ_ONCE(*pgdp); 1776 if (pgd_none(pgd)) 1777 continue; 1778 1779 WARN_ON(!pgd_present(pgd)); 1780 free_empty_p4d_table(pgdp, addr, next, floor, ceiling); 1781 } while (addr = next, addr < end); 1782 } 1783 #endif 1784 1785 void __meminit vmemmap_set_pmd(pmd_t *pmdp, void *p, int node, 1786 unsigned long addr, unsigned long next) 1787 { 1788 pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL)); 1789 } 1790 1791 int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node, 1792 unsigned long addr, unsigned long next) 1793 { 1794 vmemmap_verify((pte_t *)pmdp, node, addr, next); 1795 1796 return pmd_leaf(READ_ONCE(*pmdp)); 1797 } 1798 1799 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 1800 struct vmem_altmap *altmap) 1801 { 1802 WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1803 /* [start, end] should be within one section */ 1804 WARN_ON_ONCE(end - start > PAGES_PER_SECTION * sizeof(struct page)); 1805 1806 if (!IS_ENABLED(CONFIG_ARM64_4K_PAGES) || 1807 (end - start < PAGES_PER_SECTION * sizeof(struct page))) 1808 return vmemmap_populate_basepages(start, end, node, altmap); 1809 else 1810 return vmemmap_populate_hugepages(start, end, node, altmap); 1811 } 1812 1813 #ifdef CONFIG_MEMORY_HOTPLUG 1814 void vmemmap_free(unsigned long start, unsigned long end, 1815 struct vmem_altmap *altmap) 1816 { 1817 WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1818 1819 unmap_hotplug_range(start, end, true, altmap); 1820 free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END); 1821 } 1822 #endif /* CONFIG_MEMORY_HOTPLUG */ 1823 1824 int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot) 1825 { 1826 pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot)); 1827 1828 /* Only allow permission changes for now */ 1829 if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)), 1830 pud_val(new_pud))) 1831 return 0; 1832 1833 VM_BUG_ON(phys & ~PUD_MASK); 1834 set_pud(pudp, new_pud); 1835 return 1; 1836 } 1837 1838 int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot) 1839 { 1840 pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot)); 1841 1842 /* Only allow permission changes for now */ 1843 if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)), 1844 pmd_val(new_pmd))) 1845 return 0; 1846 1847 VM_BUG_ON(phys & ~PMD_MASK); 1848 set_pmd(pmdp, new_pmd); 1849 return 1; 1850 } 1851 1852 #ifndef __PAGETABLE_P4D_FOLDED 1853 void p4d_clear_huge(p4d_t *p4dp) 1854 { 1855 } 1856 #endif 1857 1858 int pud_clear_huge(pud_t *pudp) 1859 { 1860 if (!pud_leaf(READ_ONCE(*pudp))) 1861 return 0; 1862 pud_clear(pudp); 1863 return 1; 1864 } 1865 1866 int pmd_clear_huge(pmd_t *pmdp) 1867 { 1868 if (!pmd_leaf(READ_ONCE(*pmdp))) 1869 return 0; 1870 pmd_clear(pmdp); 1871 return 1; 1872 } 1873 1874 static int __pmd_free_pte_page(pmd_t *pmdp, unsigned long addr, 1875 bool acquire_mmap_lock) 1876 { 1877 pte_t *table; 1878 pmd_t pmd; 1879 1880 pmd = READ_ONCE(*pmdp); 1881 1882 if (!pmd_table(pmd)) { 1883 VM_WARN_ON(1); 1884 return 1; 1885 } 1886 1887 /* See comment in pud_free_pmd_page for static key logic */ 1888 table = pte_offset_kernel(pmdp, addr); 1889 pmd_clear(pmdp); 1890 __flush_tlb_kernel_pgtable(addr); 1891 if (static_branch_unlikely(&arm64_ptdump_lock_key) && acquire_mmap_lock) { 1892 mmap_read_lock(&init_mm); 1893 mmap_read_unlock(&init_mm); 1894 } 1895 1896 pte_free_kernel(NULL, table); 1897 return 1; 1898 } 1899 1900 int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr) 1901 { 1902 /* If ptdump is walking the pagetables, acquire init_mm.mmap_lock */ 1903 return __pmd_free_pte_page(pmdp, addr, /* acquire_mmap_lock = */ true); 1904 } 1905 1906 int pud_free_pmd_page(pud_t *pudp, unsigned long addr) 1907 { 1908 pmd_t *table; 1909 pmd_t *pmdp; 1910 pud_t pud; 1911 unsigned long next, end; 1912 1913 pud = READ_ONCE(*pudp); 1914 1915 if (!pud_table(pud)) { 1916 VM_WARN_ON(1); 1917 return 1; 1918 } 1919 1920 table = pmd_offset(pudp, addr); 1921 1922 /* 1923 * Our objective is to prevent ptdump from reading a PMD table which has 1924 * been freed. In this race, if pud_free_pmd_page observes the key on 1925 * (which got flipped by ptdump) then the mmap lock sequence here will, 1926 * as a result of the mmap write lock/unlock sequence in ptdump, give 1927 * us the correct synchronization. If not, this means that ptdump has 1928 * yet not started walking the pagetables - the sequence of barriers 1929 * issued by __flush_tlb_kernel_pgtable() guarantees that ptdump will 1930 * observe an empty PUD. 1931 */ 1932 pud_clear(pudp); 1933 __flush_tlb_kernel_pgtable(addr); 1934 if (static_branch_unlikely(&arm64_ptdump_lock_key)) { 1935 mmap_read_lock(&init_mm); 1936 mmap_read_unlock(&init_mm); 1937 } 1938 1939 pmdp = table; 1940 next = addr; 1941 end = addr + PUD_SIZE; 1942 do { 1943 if (pmd_present(pmdp_get(pmdp))) 1944 /* 1945 * PMD has been isolated, so ptdump won't see it. No 1946 * need to acquire init_mm.mmap_lock. 1947 */ 1948 __pmd_free_pte_page(pmdp, next, /* acquire_mmap_lock = */ false); 1949 } while (pmdp++, next += PMD_SIZE, next != end); 1950 1951 pmd_free(NULL, table); 1952 return 1; 1953 } 1954 1955 #ifdef CONFIG_MEMORY_HOTPLUG 1956 static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size) 1957 { 1958 unsigned long end = start + size; 1959 1960 WARN_ON(pgdir != init_mm.pgd); 1961 WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END)); 1962 1963 unmap_hotplug_range(start, end, false, NULL); 1964 free_empty_tables(start, end, PAGE_OFFSET, PAGE_END); 1965 } 1966 1967 struct range arch_get_mappable_range(void) 1968 { 1969 struct range mhp_range; 1970 phys_addr_t start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual)); 1971 phys_addr_t end_linear_pa = __pa(PAGE_END - 1); 1972 1973 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 1974 /* 1975 * Check for a wrap, it is possible because of randomized linear 1976 * mapping the start physical address is actually bigger than 1977 * the end physical address. In this case set start to zero 1978 * because [0, end_linear_pa] range must still be able to cover 1979 * all addressable physical addresses. 1980 */ 1981 if (start_linear_pa > end_linear_pa) 1982 start_linear_pa = 0; 1983 } 1984 1985 WARN_ON(start_linear_pa > end_linear_pa); 1986 1987 /* 1988 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)] 1989 * accommodating both its ends but excluding PAGE_END. Max physical 1990 * range which can be mapped inside this linear mapping range, must 1991 * also be derived from its end points. 1992 */ 1993 mhp_range.start = start_linear_pa; 1994 mhp_range.end = end_linear_pa; 1995 1996 return mhp_range; 1997 } 1998 1999 int arch_add_memory(int nid, u64 start, u64 size, 2000 struct mhp_params *params) 2001 { 2002 int ret, flags = NO_EXEC_MAPPINGS; 2003 2004 VM_BUG_ON(!mhp_range_allowed(start, size, true)); 2005 2006 if (force_pte_mapping()) 2007 flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 2008 2009 ret = __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start), 2010 size, params->pgprot, pgd_pgtable_alloc_init_mm, 2011 flags); 2012 if (ret) 2013 goto err; 2014 2015 memblock_clear_nomap(start, size); 2016 2017 ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT, 2018 params); 2019 if (ret) 2020 goto err; 2021 2022 /* Address of hotplugged memory can be smaller */ 2023 max_pfn = max(max_pfn, PFN_UP(start + size)); 2024 max_low_pfn = max_pfn; 2025 2026 return 0; 2027 2028 err: 2029 __remove_pgd_mapping(swapper_pg_dir, 2030 __phys_to_virt(start), size); 2031 return ret; 2032 } 2033 2034 void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) 2035 { 2036 unsigned long start_pfn = start >> PAGE_SHIFT; 2037 unsigned long nr_pages = size >> PAGE_SHIFT; 2038 2039 __remove_pages(start_pfn, nr_pages, altmap); 2040 __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size); 2041 } 2042 2043 2044 static bool addr_splits_kernel_leaf(unsigned long addr) 2045 { 2046 pgd_t *pgdp, pgd; 2047 p4d_t *p4dp, p4d; 2048 pud_t *pudp, pud; 2049 pmd_t *pmdp, pmd; 2050 pte_t *ptep, pte; 2051 2052 /* 2053 * If the given address points at a the start address of 2054 * a possible leaf, we certainly won't split. Otherwise, 2055 * check if we would actually split a leaf by traversing 2056 * the page tables further. 2057 */ 2058 if (IS_ALIGNED(addr, PGDIR_SIZE)) 2059 return false; 2060 2061 pgdp = pgd_offset_k(addr); 2062 pgd = pgdp_get(pgdp); 2063 if (!pgd_present(pgd)) 2064 return false; 2065 2066 if (IS_ALIGNED(addr, P4D_SIZE)) 2067 return false; 2068 2069 p4dp = p4d_offset(pgdp, addr); 2070 p4d = p4dp_get(p4dp); 2071 if (!p4d_present(p4d)) 2072 return false; 2073 2074 if (IS_ALIGNED(addr, PUD_SIZE)) 2075 return false; 2076 2077 pudp = pud_offset(p4dp, addr); 2078 pud = pudp_get(pudp); 2079 if (!pud_present(pud)) 2080 return false; 2081 2082 if (pud_leaf(pud)) 2083 return true; 2084 2085 if (IS_ALIGNED(addr, CONT_PMD_SIZE)) 2086 return false; 2087 2088 pmdp = pmd_offset(pudp, addr); 2089 pmd = pmdp_get(pmdp); 2090 if (!pmd_present(pmd)) 2091 return false; 2092 2093 if (pmd_cont(pmd)) 2094 return true; 2095 2096 if (IS_ALIGNED(addr, PMD_SIZE)) 2097 return false; 2098 2099 if (pmd_leaf(pmd)) 2100 return true; 2101 2102 if (IS_ALIGNED(addr, CONT_PTE_SIZE)) 2103 return false; 2104 2105 ptep = pte_offset_kernel(pmdp, addr); 2106 pte = __ptep_get(ptep); 2107 if (!pte_present(pte)) 2108 return false; 2109 2110 if (pte_cont(pte)) 2111 return true; 2112 2113 return !IS_ALIGNED(addr, PAGE_SIZE); 2114 } 2115 2116 static bool can_unmap_without_split(unsigned long pfn, unsigned long nr_pages) 2117 { 2118 unsigned long phys_start, phys_end, start, end; 2119 2120 phys_start = PFN_PHYS(pfn); 2121 phys_end = phys_start + nr_pages * PAGE_SIZE; 2122 2123 /* PFN range's linear map edges are leaf entry aligned */ 2124 start = __phys_to_virt(phys_start); 2125 end = __phys_to_virt(phys_end); 2126 if (addr_splits_kernel_leaf(start) || addr_splits_kernel_leaf(end)) { 2127 pr_warn("[%lx %lx] splits a leaf entry in linear map\n", 2128 phys_start, phys_end); 2129 return false; 2130 } 2131 2132 /* PFN range's vmemmap edges are leaf entry aligned */ 2133 BUILD_BUG_ON(!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)); 2134 start = (unsigned long)pfn_to_page(pfn); 2135 end = (unsigned long)pfn_to_page(pfn + nr_pages); 2136 if (addr_splits_kernel_leaf(start) || addr_splits_kernel_leaf(end)) { 2137 pr_warn("[%lx %lx] splits a leaf entry in vmemmap\n", 2138 phys_start, phys_end); 2139 return false; 2140 } 2141 return true; 2142 } 2143 2144 /* 2145 * This memory hotplug notifier helps prevent boot memory from being 2146 * inadvertently removed as it blocks pfn range offlining process in 2147 * __offline_pages(). Hence this prevents both offlining as well as 2148 * removal process for boot memory which is initially always online. 2149 * In future if and when boot memory could be removed, this notifier 2150 * should be dropped and free_hotplug_page_range() should handle any 2151 * reserved pages allocated during boot. 2152 * 2153 * This also blocks any memory remove that would have caused a split 2154 * in leaf entry in kernel linear or vmemmap mapping. 2155 */ 2156 static int prevent_memory_remove_notifier(struct notifier_block *nb, 2157 unsigned long action, void *data) 2158 { 2159 struct mem_section *ms; 2160 struct memory_notify *arg = data; 2161 unsigned long end_pfn = arg->start_pfn + arg->nr_pages; 2162 unsigned long pfn = arg->start_pfn; 2163 2164 if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE)) 2165 return NOTIFY_OK; 2166 2167 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 2168 unsigned long start = PFN_PHYS(pfn); 2169 unsigned long end = start + (1UL << PA_SECTION_SHIFT); 2170 2171 ms = __pfn_to_section(pfn); 2172 if (!early_section(ms)) 2173 continue; 2174 2175 if (action == MEM_GOING_OFFLINE) { 2176 /* 2177 * Boot memory removal is not supported. Prevent 2178 * it via blocking any attempted offline request 2179 * for the boot memory and just report it. 2180 */ 2181 pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end); 2182 return NOTIFY_BAD; 2183 } else if (action == MEM_OFFLINE) { 2184 /* 2185 * This should have never happened. Boot memory 2186 * offlining should have been prevented by this 2187 * very notifier. Probably some memory removal 2188 * procedure might have changed which would then 2189 * require further debug. 2190 */ 2191 pr_err("Boot memory [%lx %lx] offlined\n", start, end); 2192 2193 /* 2194 * Core memory hotplug does not process a return 2195 * code from the notifier for MEM_OFFLINE events. 2196 * The error condition has been reported. Return 2197 * from here as if ignored. 2198 */ 2199 return NOTIFY_DONE; 2200 } 2201 } 2202 2203 if (!can_unmap_without_split(pfn, arg->nr_pages)) 2204 return NOTIFY_BAD; 2205 2206 return NOTIFY_OK; 2207 } 2208 2209 static struct notifier_block prevent_memory_remove_nb = { 2210 .notifier_call = prevent_memory_remove_notifier, 2211 }; 2212 2213 /* 2214 * This ensures that boot memory sections on the platform are online 2215 * from early boot. Memory sections could not be prevented from being 2216 * offlined, unless for some reason they are not online to begin with. 2217 * This helps validate the basic assumption on which the above memory 2218 * event notifier works to prevent boot memory section offlining and 2219 * its possible removal. 2220 */ 2221 static void validate_bootmem_online(void) 2222 { 2223 phys_addr_t start, end, addr; 2224 struct mem_section *ms; 2225 u64 i; 2226 2227 /* 2228 * Scanning across all memblock might be expensive 2229 * on some big memory systems. Hence enable this 2230 * validation only with DEBUG_VM. 2231 */ 2232 if (!IS_ENABLED(CONFIG_DEBUG_VM)) 2233 return; 2234 2235 for_each_mem_range(i, &start, &end) { 2236 for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) { 2237 ms = __pfn_to_section(PHYS_PFN(addr)); 2238 2239 /* 2240 * All memory ranges in the system at this point 2241 * should have been marked as early sections. 2242 */ 2243 WARN_ON(!early_section(ms)); 2244 2245 /* 2246 * Memory notifier mechanism here to prevent boot 2247 * memory offlining depends on the fact that each 2248 * early section memory on the system is initially 2249 * online. Otherwise a given memory section which 2250 * is already offline will be overlooked and can 2251 * be removed completely. Call out such sections. 2252 */ 2253 if (!online_section(ms)) 2254 pr_err("Boot memory [%llx %llx] is offline, can be removed\n", 2255 addr, addr + (1UL << PA_SECTION_SHIFT)); 2256 } 2257 } 2258 } 2259 2260 static int __init prevent_memory_remove_init(void) 2261 { 2262 int ret = 0; 2263 2264 if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE)) 2265 return ret; 2266 2267 validate_bootmem_online(); 2268 ret = register_memory_notifier(&prevent_memory_remove_nb); 2269 if (ret) 2270 pr_err("%s: Notifier registration failed %d\n", __func__, ret); 2271 2272 return ret; 2273 } 2274 early_initcall(prevent_memory_remove_init); 2275 #endif 2276 2277 pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr, 2278 pte_t *ptep, unsigned int nr) 2279 { 2280 pte_t pte = get_and_clear_ptes(vma->vm_mm, addr, ptep, nr); 2281 2282 if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) { 2283 /* 2284 * Break-before-make (BBM) is required for all user space mappings 2285 * when the permission changes from executable to non-executable 2286 * in cases where cpu is affected with errata #2645198. 2287 */ 2288 if (pte_accessible(vma->vm_mm, pte) && pte_user_exec(pte)) 2289 __flush_tlb_range(vma, addr, nr * PAGE_SIZE, 2290 PAGE_SIZE, 3, TLBF_NOWALKCACHE); 2291 } 2292 2293 return pte; 2294 } 2295 2296 pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) 2297 { 2298 return modify_prot_start_ptes(vma, addr, ptep, 1); 2299 } 2300 2301 void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr, 2302 pte_t *ptep, pte_t old_pte, pte_t pte, 2303 unsigned int nr) 2304 { 2305 set_ptes(vma->vm_mm, addr, ptep, pte, nr); 2306 } 2307 2308 void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, 2309 pte_t old_pte, pte_t pte) 2310 { 2311 modify_prot_commit_ptes(vma, addr, ptep, old_pte, pte, 1); 2312 } 2313 2314 /* 2315 * Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD, 2316 * avoiding the possibility of conflicting TLB entries being allocated. 2317 */ 2318 void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp) 2319 { 2320 typedef void (ttbr_replace_func)(phys_addr_t); 2321 extern ttbr_replace_func idmap_cpu_replace_ttbr1; 2322 ttbr_replace_func *replace_phys; 2323 unsigned long daif; 2324 2325 /* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */ 2326 phys_addr_t ttbr1 = phys_to_ttbr(virt_to_phys(pgdp)); 2327 2328 if (cnp) 2329 ttbr1 |= TTBRx_EL1_CnP; 2330 2331 replace_phys = (void *)__pa_symbol(idmap_cpu_replace_ttbr1); 2332 2333 cpu_install_idmap(); 2334 2335 /* 2336 * We really don't want to take *any* exceptions while TTBR1 is 2337 * in the process of being replaced so mask everything. 2338 */ 2339 daif = local_daif_save(); 2340 replace_phys(ttbr1); 2341 local_daif_restore(daif); 2342 2343 cpu_uninstall_idmap(); 2344 } 2345 2346 #ifdef CONFIG_ARCH_HAS_PKEYS 2347 int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, unsigned long init_val) 2348 { 2349 u64 new_por; 2350 u64 old_por; 2351 2352 if (!system_supports_poe()) 2353 return -ENOSPC; 2354 2355 /* 2356 * This code should only be called with valid 'pkey' 2357 * values originating from in-kernel users. Complain 2358 * if a bad value is observed. 2359 */ 2360 if (WARN_ON_ONCE(pkey >= arch_max_pkey())) 2361 return -EINVAL; 2362 2363 /* Set the bits we need in POR: */ 2364 new_por = POE_RWX; 2365 if (init_val & PKEY_DISABLE_WRITE) 2366 new_por &= ~POE_W; 2367 if (init_val & PKEY_DISABLE_ACCESS) 2368 new_por &= ~POE_RW; 2369 if (init_val & PKEY_DISABLE_READ) 2370 new_por &= ~POE_R; 2371 if (init_val & PKEY_DISABLE_EXECUTE) 2372 new_por &= ~POE_X; 2373 2374 /* Shift the bits in to the correct place in POR for pkey: */ 2375 new_por = POR_ELx_PERM_PREP(pkey, new_por); 2376 2377 /* Get old POR and mask off any old bits in place: */ 2378 old_por = read_sysreg_s(SYS_POR_EL0); 2379 old_por &= ~(POE_MASK << POR_ELx_PERM_SHIFT(pkey)); 2380 2381 /* Write old part along with new part: */ 2382 write_sysreg_s(old_por | new_por, SYS_POR_EL0); 2383 2384 return 0; 2385 } 2386 #endif 2387