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 31 #include <asm/barrier.h> 32 #include <asm/cputype.h> 33 #include <asm/fixmap.h> 34 #include <asm/kasan.h> 35 #include <asm/kernel-pgtable.h> 36 #include <asm/sections.h> 37 #include <asm/setup.h> 38 #include <linux/sizes.h> 39 #include <asm/tlb.h> 40 #include <asm/mmu_context.h> 41 #include <asm/ptdump.h> 42 #include <asm/tlbflush.h> 43 #include <asm/pgalloc.h> 44 #include <asm/kfence.h> 45 46 #define NO_BLOCK_MAPPINGS BIT(0) 47 #define NO_CONT_MAPPINGS BIT(1) 48 #define NO_EXEC_MAPPINGS BIT(2) /* assumes FEAT_HPDS is not used */ 49 50 enum pgtable_type { 51 TABLE_PTE, 52 TABLE_PMD, 53 TABLE_PUD, 54 TABLE_P4D, 55 }; 56 57 u64 kimage_voffset __ro_after_init; 58 EXPORT_SYMBOL(kimage_voffset); 59 60 u32 __boot_cpu_mode[] = { BOOT_CPU_MODE_EL2, BOOT_CPU_MODE_EL1 }; 61 62 static bool rodata_is_rw __ro_after_init = true; 63 64 /* 65 * The booting CPU updates the failed status @__early_cpu_boot_status, 66 * with MMU turned off. 67 */ 68 long __section(".mmuoff.data.write") __early_cpu_boot_status; 69 70 /* 71 * Empty_zero_page is a special page that is used for zero-initialized data 72 * and COW. 73 */ 74 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss; 75 EXPORT_SYMBOL(empty_zero_page); 76 77 static DEFINE_SPINLOCK(swapper_pgdir_lock); 78 static DEFINE_MUTEX(fixmap_lock); 79 80 void noinstr set_swapper_pgd(pgd_t *pgdp, pgd_t pgd) 81 { 82 pgd_t *fixmap_pgdp; 83 84 /* 85 * Don't bother with the fixmap if swapper_pg_dir is still mapped 86 * writable in the kernel mapping. 87 */ 88 if (rodata_is_rw) { 89 WRITE_ONCE(*pgdp, pgd); 90 dsb(ishst); 91 isb(); 92 return; 93 } 94 95 spin_lock(&swapper_pgdir_lock); 96 fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp)); 97 WRITE_ONCE(*fixmap_pgdp, pgd); 98 /* 99 * We need dsb(ishst) here to ensure the page-table-walker sees 100 * our new entry before set_p?d() returns. The fixmap's 101 * flush_tlb_kernel_range() via clear_fixmap() does this for us. 102 */ 103 pgd_clear_fixmap(); 104 spin_unlock(&swapper_pgdir_lock); 105 } 106 107 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 108 unsigned long size, pgprot_t vma_prot) 109 { 110 if (!pfn_is_map_memory(pfn)) 111 return pgprot_noncached(vma_prot); 112 else if (file->f_flags & O_SYNC) 113 return pgprot_writecombine(vma_prot); 114 return vma_prot; 115 } 116 EXPORT_SYMBOL(phys_mem_access_prot); 117 118 static phys_addr_t __init early_pgtable_alloc(enum pgtable_type pgtable_type) 119 { 120 phys_addr_t phys; 121 122 phys = memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE, 0, 123 MEMBLOCK_ALLOC_NOLEAKTRACE); 124 if (!phys) 125 panic("Failed to allocate page table page\n"); 126 127 return phys; 128 } 129 130 bool pgattr_change_is_safe(pteval_t old, pteval_t new) 131 { 132 /* 133 * The following mapping attributes may be updated in live 134 * kernel mappings without the need for break-before-make. 135 */ 136 pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG | 137 PTE_SWBITS_MASK; 138 139 /* creating or taking down mappings is always safe */ 140 if (!pte_valid(__pte(old)) || !pte_valid(__pte(new))) 141 return true; 142 143 /* A live entry's pfn should not change */ 144 if (pte_pfn(__pte(old)) != pte_pfn(__pte(new))) 145 return false; 146 147 /* live contiguous mappings may not be manipulated at all */ 148 if ((old | new) & PTE_CONT) 149 return false; 150 151 /* Transitioning from Non-Global to Global is unsafe */ 152 if (old & ~new & PTE_NG) 153 return false; 154 155 /* 156 * Changing the memory type between Normal and Normal-Tagged is safe 157 * since Tagged is considered a permission attribute from the 158 * mismatched attribute aliases perspective. 159 */ 160 if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 161 (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) && 162 ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 163 (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED))) 164 mask |= PTE_ATTRINDX_MASK; 165 166 return ((old ^ new) & ~mask) == 0; 167 } 168 169 static void init_clear_pgtable(void *table) 170 { 171 clear_page(table); 172 173 /* Ensure the zeroing is observed by page table walks. */ 174 dsb(ishst); 175 } 176 177 static void init_pte(pte_t *ptep, unsigned long addr, unsigned long end, 178 phys_addr_t phys, pgprot_t prot) 179 { 180 do { 181 pte_t old_pte = __ptep_get(ptep); 182 183 /* 184 * Required barriers to make this visible to the table walker 185 * are deferred to the end of alloc_init_cont_pte(). 186 */ 187 __set_pte_nosync(ptep, pfn_pte(__phys_to_pfn(phys), prot)); 188 189 /* 190 * After the PTE entry has been populated once, we 191 * only allow updates to the permission attributes. 192 */ 193 BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), 194 pte_val(__ptep_get(ptep)))); 195 196 phys += PAGE_SIZE; 197 } while (ptep++, addr += PAGE_SIZE, addr != end); 198 } 199 200 static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr, 201 unsigned long end, phys_addr_t phys, 202 pgprot_t prot, 203 phys_addr_t (*pgtable_alloc)(enum pgtable_type), 204 int flags) 205 { 206 unsigned long next; 207 pmd_t pmd = READ_ONCE(*pmdp); 208 pte_t *ptep; 209 210 BUG_ON(pmd_sect(pmd)); 211 if (pmd_none(pmd)) { 212 pmdval_t pmdval = PMD_TYPE_TABLE | PMD_TABLE_UXN | PMD_TABLE_AF; 213 phys_addr_t pte_phys; 214 215 if (flags & NO_EXEC_MAPPINGS) 216 pmdval |= PMD_TABLE_PXN; 217 BUG_ON(!pgtable_alloc); 218 pte_phys = pgtable_alloc(TABLE_PTE); 219 ptep = pte_set_fixmap(pte_phys); 220 init_clear_pgtable(ptep); 221 ptep += pte_index(addr); 222 __pmd_populate(pmdp, pte_phys, pmdval); 223 } else { 224 BUG_ON(pmd_bad(pmd)); 225 ptep = pte_set_fixmap_offset(pmdp, addr); 226 } 227 228 do { 229 pgprot_t __prot = prot; 230 231 next = pte_cont_addr_end(addr, end); 232 233 /* use a contiguous mapping if the range is suitably aligned */ 234 if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) && 235 (flags & NO_CONT_MAPPINGS) == 0) 236 __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 237 238 init_pte(ptep, addr, next, phys, __prot); 239 240 ptep += pte_index(next) - pte_index(addr); 241 phys += next - addr; 242 } while (addr = next, addr != end); 243 244 /* 245 * Note: barriers and maintenance necessary to clear the fixmap slot 246 * ensure that all previous pgtable writes are visible to the table 247 * walker. 248 */ 249 pte_clear_fixmap(); 250 } 251 252 static void init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end, 253 phys_addr_t phys, pgprot_t prot, 254 phys_addr_t (*pgtable_alloc)(enum pgtable_type), int flags) 255 { 256 unsigned long next; 257 258 do { 259 pmd_t old_pmd = READ_ONCE(*pmdp); 260 261 next = pmd_addr_end(addr, end); 262 263 /* try section mapping first */ 264 if (((addr | next | phys) & ~PMD_MASK) == 0 && 265 (flags & NO_BLOCK_MAPPINGS) == 0) { 266 pmd_set_huge(pmdp, phys, prot); 267 268 /* 269 * After the PMD entry has been populated once, we 270 * only allow updates to the permission attributes. 271 */ 272 BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd), 273 READ_ONCE(pmd_val(*pmdp)))); 274 } else { 275 alloc_init_cont_pte(pmdp, addr, next, phys, prot, 276 pgtable_alloc, flags); 277 278 BUG_ON(pmd_val(old_pmd) != 0 && 279 pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp))); 280 } 281 phys += next - addr; 282 } while (pmdp++, addr = next, addr != end); 283 } 284 285 static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr, 286 unsigned long end, phys_addr_t phys, 287 pgprot_t prot, 288 phys_addr_t (*pgtable_alloc)(enum pgtable_type), 289 int flags) 290 { 291 unsigned long next; 292 pud_t pud = READ_ONCE(*pudp); 293 pmd_t *pmdp; 294 295 /* 296 * Check for initial section mappings in the pgd/pud. 297 */ 298 BUG_ON(pud_sect(pud)); 299 if (pud_none(pud)) { 300 pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_UXN | PUD_TABLE_AF; 301 phys_addr_t pmd_phys; 302 303 if (flags & NO_EXEC_MAPPINGS) 304 pudval |= PUD_TABLE_PXN; 305 BUG_ON(!pgtable_alloc); 306 pmd_phys = pgtable_alloc(TABLE_PMD); 307 pmdp = pmd_set_fixmap(pmd_phys); 308 init_clear_pgtable(pmdp); 309 pmdp += pmd_index(addr); 310 __pud_populate(pudp, pmd_phys, pudval); 311 } else { 312 BUG_ON(pud_bad(pud)); 313 pmdp = pmd_set_fixmap_offset(pudp, addr); 314 } 315 316 do { 317 pgprot_t __prot = prot; 318 319 next = pmd_cont_addr_end(addr, end); 320 321 /* use a contiguous mapping if the range is suitably aligned */ 322 if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) && 323 (flags & NO_CONT_MAPPINGS) == 0) 324 __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 325 326 init_pmd(pmdp, addr, next, phys, __prot, pgtable_alloc, flags); 327 328 pmdp += pmd_index(next) - pmd_index(addr); 329 phys += next - addr; 330 } while (addr = next, addr != end); 331 332 pmd_clear_fixmap(); 333 } 334 335 static void alloc_init_pud(p4d_t *p4dp, unsigned long addr, unsigned long end, 336 phys_addr_t phys, pgprot_t prot, 337 phys_addr_t (*pgtable_alloc)(enum pgtable_type), 338 int flags) 339 { 340 unsigned long next; 341 p4d_t p4d = READ_ONCE(*p4dp); 342 pud_t *pudp; 343 344 if (p4d_none(p4d)) { 345 p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN | P4D_TABLE_AF; 346 phys_addr_t pud_phys; 347 348 if (flags & NO_EXEC_MAPPINGS) 349 p4dval |= P4D_TABLE_PXN; 350 BUG_ON(!pgtable_alloc); 351 pud_phys = pgtable_alloc(TABLE_PUD); 352 pudp = pud_set_fixmap(pud_phys); 353 init_clear_pgtable(pudp); 354 pudp += pud_index(addr); 355 __p4d_populate(p4dp, pud_phys, p4dval); 356 } else { 357 BUG_ON(p4d_bad(p4d)); 358 pudp = pud_set_fixmap_offset(p4dp, addr); 359 } 360 361 do { 362 pud_t old_pud = READ_ONCE(*pudp); 363 364 next = pud_addr_end(addr, end); 365 366 /* 367 * For 4K granule only, attempt to put down a 1GB block 368 */ 369 if (pud_sect_supported() && 370 ((addr | next | phys) & ~PUD_MASK) == 0 && 371 (flags & NO_BLOCK_MAPPINGS) == 0) { 372 pud_set_huge(pudp, phys, prot); 373 374 /* 375 * After the PUD entry has been populated once, we 376 * only allow updates to the permission attributes. 377 */ 378 BUG_ON(!pgattr_change_is_safe(pud_val(old_pud), 379 READ_ONCE(pud_val(*pudp)))); 380 } else { 381 alloc_init_cont_pmd(pudp, addr, next, phys, prot, 382 pgtable_alloc, flags); 383 384 BUG_ON(pud_val(old_pud) != 0 && 385 pud_val(old_pud) != READ_ONCE(pud_val(*pudp))); 386 } 387 phys += next - addr; 388 } while (pudp++, addr = next, addr != end); 389 390 pud_clear_fixmap(); 391 } 392 393 static void alloc_init_p4d(pgd_t *pgdp, unsigned long addr, unsigned long end, 394 phys_addr_t phys, pgprot_t prot, 395 phys_addr_t (*pgtable_alloc)(enum pgtable_type), 396 int flags) 397 { 398 unsigned long next; 399 pgd_t pgd = READ_ONCE(*pgdp); 400 p4d_t *p4dp; 401 402 if (pgd_none(pgd)) { 403 pgdval_t pgdval = PGD_TYPE_TABLE | PGD_TABLE_UXN | PGD_TABLE_AF; 404 phys_addr_t p4d_phys; 405 406 if (flags & NO_EXEC_MAPPINGS) 407 pgdval |= PGD_TABLE_PXN; 408 BUG_ON(!pgtable_alloc); 409 p4d_phys = pgtable_alloc(TABLE_P4D); 410 p4dp = p4d_set_fixmap(p4d_phys); 411 init_clear_pgtable(p4dp); 412 p4dp += p4d_index(addr); 413 __pgd_populate(pgdp, p4d_phys, pgdval); 414 } else { 415 BUG_ON(pgd_bad(pgd)); 416 p4dp = p4d_set_fixmap_offset(pgdp, addr); 417 } 418 419 do { 420 p4d_t old_p4d = READ_ONCE(*p4dp); 421 422 next = p4d_addr_end(addr, end); 423 424 alloc_init_pud(p4dp, addr, next, phys, prot, 425 pgtable_alloc, flags); 426 427 BUG_ON(p4d_val(old_p4d) != 0 && 428 p4d_val(old_p4d) != READ_ONCE(p4d_val(*p4dp))); 429 430 phys += next - addr; 431 } while (p4dp++, addr = next, addr != end); 432 433 p4d_clear_fixmap(); 434 } 435 436 static void __create_pgd_mapping_locked(pgd_t *pgdir, phys_addr_t phys, 437 unsigned long virt, phys_addr_t size, 438 pgprot_t prot, 439 phys_addr_t (*pgtable_alloc)(enum pgtable_type), 440 int flags) 441 { 442 unsigned long addr, end, next; 443 pgd_t *pgdp = pgd_offset_pgd(pgdir, virt); 444 445 /* 446 * If the virtual and physical address don't have the same offset 447 * within a page, we cannot map the region as the caller expects. 448 */ 449 if (WARN_ON((phys ^ virt) & ~PAGE_MASK)) 450 return; 451 452 phys &= PAGE_MASK; 453 addr = virt & PAGE_MASK; 454 end = PAGE_ALIGN(virt + size); 455 456 do { 457 next = pgd_addr_end(addr, end); 458 alloc_init_p4d(pgdp, addr, next, phys, prot, pgtable_alloc, 459 flags); 460 phys += next - addr; 461 } while (pgdp++, addr = next, addr != end); 462 } 463 464 static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, 465 unsigned long virt, phys_addr_t size, 466 pgprot_t prot, 467 phys_addr_t (*pgtable_alloc)(enum pgtable_type), 468 int flags) 469 { 470 mutex_lock(&fixmap_lock); 471 __create_pgd_mapping_locked(pgdir, phys, virt, size, prot, 472 pgtable_alloc, flags); 473 mutex_unlock(&fixmap_lock); 474 } 475 476 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 477 extern __alias(__create_pgd_mapping_locked) 478 void create_kpti_ng_temp_pgd(pgd_t *pgdir, phys_addr_t phys, unsigned long virt, 479 phys_addr_t size, pgprot_t prot, 480 phys_addr_t (*pgtable_alloc)(enum pgtable_type), 481 int flags); 482 #endif 483 484 static phys_addr_t __pgd_pgtable_alloc(struct mm_struct *mm, 485 enum pgtable_type pgtable_type) 486 { 487 /* Page is zeroed by init_clear_pgtable() so don't duplicate effort. */ 488 struct ptdesc *ptdesc = pagetable_alloc(GFP_PGTABLE_KERNEL & ~__GFP_ZERO, 0); 489 phys_addr_t pa; 490 491 BUG_ON(!ptdesc); 492 pa = page_to_phys(ptdesc_page(ptdesc)); 493 494 switch (pgtable_type) { 495 case TABLE_PTE: 496 BUG_ON(!pagetable_pte_ctor(mm, ptdesc)); 497 break; 498 case TABLE_PMD: 499 BUG_ON(!pagetable_pmd_ctor(mm, ptdesc)); 500 break; 501 case TABLE_PUD: 502 pagetable_pud_ctor(ptdesc); 503 break; 504 case TABLE_P4D: 505 pagetable_p4d_ctor(ptdesc); 506 break; 507 } 508 509 return pa; 510 } 511 512 static phys_addr_t __maybe_unused 513 pgd_pgtable_alloc_init_mm(enum pgtable_type pgtable_type) 514 { 515 return __pgd_pgtable_alloc(&init_mm, pgtable_type); 516 } 517 518 static phys_addr_t 519 pgd_pgtable_alloc_special_mm(enum pgtable_type pgtable_type) 520 { 521 return __pgd_pgtable_alloc(NULL, pgtable_type); 522 } 523 524 /* 525 * This function can only be used to modify existing table entries, 526 * without allocating new levels of table. Note that this permits the 527 * creation of new section or page entries. 528 */ 529 void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt, 530 phys_addr_t size, pgprot_t prot) 531 { 532 if (virt < PAGE_OFFSET) { 533 pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n", 534 &phys, virt); 535 return; 536 } 537 __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 538 NO_CONT_MAPPINGS); 539 } 540 541 void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, 542 unsigned long virt, phys_addr_t size, 543 pgprot_t prot, bool page_mappings_only) 544 { 545 int flags = 0; 546 547 BUG_ON(mm == &init_mm); 548 549 if (page_mappings_only) 550 flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 551 552 __create_pgd_mapping(mm->pgd, phys, virt, size, prot, 553 pgd_pgtable_alloc_special_mm, flags); 554 } 555 556 static void update_mapping_prot(phys_addr_t phys, unsigned long virt, 557 phys_addr_t size, pgprot_t prot) 558 { 559 if (virt < PAGE_OFFSET) { 560 pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n", 561 &phys, virt); 562 return; 563 } 564 565 __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 566 NO_CONT_MAPPINGS); 567 568 /* flush the TLBs after updating live kernel mappings */ 569 flush_tlb_kernel_range(virt, virt + size); 570 } 571 572 static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start, 573 phys_addr_t end, pgprot_t prot, int flags) 574 { 575 __create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start, 576 prot, early_pgtable_alloc, flags); 577 } 578 579 void __init mark_linear_text_alias_ro(void) 580 { 581 /* 582 * Remove the write permissions from the linear alias of .text/.rodata 583 */ 584 update_mapping_prot(__pa_symbol(_stext), (unsigned long)lm_alias(_stext), 585 (unsigned long)__init_begin - (unsigned long)_stext, 586 PAGE_KERNEL_RO); 587 } 588 589 #ifdef CONFIG_KFENCE 590 591 bool __ro_after_init kfence_early_init = !!CONFIG_KFENCE_SAMPLE_INTERVAL; 592 593 /* early_param() will be parsed before map_mem() below. */ 594 static int __init parse_kfence_early_init(char *arg) 595 { 596 int val; 597 598 if (get_option(&arg, &val)) 599 kfence_early_init = !!val; 600 return 0; 601 } 602 early_param("kfence.sample_interval", parse_kfence_early_init); 603 604 static phys_addr_t __init arm64_kfence_alloc_pool(void) 605 { 606 phys_addr_t kfence_pool; 607 608 if (!kfence_early_init) 609 return 0; 610 611 kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE); 612 if (!kfence_pool) { 613 pr_err("failed to allocate kfence pool\n"); 614 kfence_early_init = false; 615 return 0; 616 } 617 618 /* Temporarily mark as NOMAP. */ 619 memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE); 620 621 return kfence_pool; 622 } 623 624 static void __init arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) 625 { 626 if (!kfence_pool) 627 return; 628 629 /* KFENCE pool needs page-level mapping. */ 630 __map_memblock(pgdp, kfence_pool, kfence_pool + KFENCE_POOL_SIZE, 631 pgprot_tagged(PAGE_KERNEL), 632 NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS); 633 memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE); 634 __kfence_pool = phys_to_virt(kfence_pool); 635 } 636 #else /* CONFIG_KFENCE */ 637 638 static inline phys_addr_t arm64_kfence_alloc_pool(void) { return 0; } 639 static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) { } 640 641 #endif /* CONFIG_KFENCE */ 642 643 static void __init map_mem(pgd_t *pgdp) 644 { 645 static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN); 646 phys_addr_t kernel_start = __pa_symbol(_stext); 647 phys_addr_t kernel_end = __pa_symbol(__init_begin); 648 phys_addr_t start, end; 649 phys_addr_t early_kfence_pool; 650 int flags = NO_EXEC_MAPPINGS; 651 u64 i; 652 653 /* 654 * Setting hierarchical PXNTable attributes on table entries covering 655 * the linear region is only possible if it is guaranteed that no table 656 * entries at any level are being shared between the linear region and 657 * the vmalloc region. Check whether this is true for the PGD level, in 658 * which case it is guaranteed to be true for all other levels as well. 659 * (Unless we are running with support for LPA2, in which case the 660 * entire reduced VA space is covered by a single pgd_t which will have 661 * been populated without the PXNTable attribute by the time we get here.) 662 */ 663 BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end) && 664 pgd_index(_PAGE_OFFSET(VA_BITS_MIN)) != PTRS_PER_PGD - 1); 665 666 early_kfence_pool = arm64_kfence_alloc_pool(); 667 668 if (can_set_direct_map()) 669 flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 670 671 /* 672 * Take care not to create a writable alias for the 673 * read-only text and rodata sections of the kernel image. 674 * So temporarily mark them as NOMAP to skip mappings in 675 * the following for-loop 676 */ 677 memblock_mark_nomap(kernel_start, kernel_end - kernel_start); 678 679 /* map all the memory banks */ 680 for_each_mem_range(i, &start, &end) { 681 if (start >= end) 682 break; 683 /* 684 * The linear map must allow allocation tags reading/writing 685 * if MTE is present. Otherwise, it has the same attributes as 686 * PAGE_KERNEL. 687 */ 688 __map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL), 689 flags); 690 } 691 692 /* 693 * Map the linear alias of the [_stext, __init_begin) interval 694 * as non-executable now, and remove the write permission in 695 * mark_linear_text_alias_ro() below (which will be called after 696 * alternative patching has completed). This makes the contents 697 * of the region accessible to subsystems such as hibernate, 698 * but protects it from inadvertent modification or execution. 699 * Note that contiguous mappings cannot be remapped in this way, 700 * so we should avoid them here. 701 */ 702 __map_memblock(pgdp, kernel_start, kernel_end, 703 PAGE_KERNEL, NO_CONT_MAPPINGS); 704 memblock_clear_nomap(kernel_start, kernel_end - kernel_start); 705 arm64_kfence_map_pool(early_kfence_pool, pgdp); 706 } 707 708 void mark_rodata_ro(void) 709 { 710 unsigned long section_size; 711 712 /* 713 * mark .rodata as read only. Use __init_begin rather than __end_rodata 714 * to cover NOTES and EXCEPTION_TABLE. 715 */ 716 section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata; 717 WRITE_ONCE(rodata_is_rw, false); 718 update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata, 719 section_size, PAGE_KERNEL_RO); 720 } 721 722 static void __init declare_vma(struct vm_struct *vma, 723 void *va_start, void *va_end, 724 unsigned long vm_flags) 725 { 726 phys_addr_t pa_start = __pa_symbol(va_start); 727 unsigned long size = va_end - va_start; 728 729 BUG_ON(!PAGE_ALIGNED(pa_start)); 730 BUG_ON(!PAGE_ALIGNED(size)); 731 732 if (!(vm_flags & VM_NO_GUARD)) 733 size += PAGE_SIZE; 734 735 vma->addr = va_start; 736 vma->phys_addr = pa_start; 737 vma->size = size; 738 vma->flags = VM_MAP | vm_flags; 739 vma->caller = __builtin_return_address(0); 740 741 vm_area_add_early(vma); 742 } 743 744 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 745 static pgprot_t kernel_exec_prot(void) 746 { 747 return rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; 748 } 749 750 static int __init map_entry_trampoline(void) 751 { 752 int i; 753 754 if (!arm64_kernel_unmapped_at_el0()) 755 return 0; 756 757 pgprot_t prot = kernel_exec_prot(); 758 phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start); 759 760 /* The trampoline is always mapped and can therefore be global */ 761 pgprot_val(prot) &= ~PTE_NG; 762 763 /* Map only the text into the trampoline page table */ 764 memset(tramp_pg_dir, 0, PGD_SIZE); 765 __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, 766 entry_tramp_text_size(), prot, 767 pgd_pgtable_alloc_init_mm, NO_BLOCK_MAPPINGS); 768 769 /* Map both the text and data into the kernel page table */ 770 for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++) 771 __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i, 772 pa_start + i * PAGE_SIZE, prot); 773 774 if (IS_ENABLED(CONFIG_RELOCATABLE)) 775 __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i, 776 pa_start + i * PAGE_SIZE, PAGE_KERNEL_RO); 777 778 return 0; 779 } 780 core_initcall(map_entry_trampoline); 781 #endif 782 783 /* 784 * Declare the VMA areas for the kernel 785 */ 786 static void __init declare_kernel_vmas(void) 787 { 788 static struct vm_struct vmlinux_seg[KERNEL_SEGMENT_COUNT]; 789 790 declare_vma(&vmlinux_seg[0], _stext, _etext, VM_NO_GUARD); 791 declare_vma(&vmlinux_seg[1], __start_rodata, __inittext_begin, VM_NO_GUARD); 792 declare_vma(&vmlinux_seg[2], __inittext_begin, __inittext_end, VM_NO_GUARD); 793 declare_vma(&vmlinux_seg[3], __initdata_begin, __initdata_end, VM_NO_GUARD); 794 declare_vma(&vmlinux_seg[4], _data, _end, 0); 795 } 796 797 void __pi_map_range(u64 *pgd, u64 start, u64 end, u64 pa, pgprot_t prot, 798 int level, pte_t *tbl, bool may_use_cont, u64 va_offset); 799 800 static u8 idmap_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init, 801 kpti_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init; 802 803 static void __init create_idmap(void) 804 { 805 u64 start = __pa_symbol(__idmap_text_start); 806 u64 end = __pa_symbol(__idmap_text_end); 807 u64 ptep = __pa_symbol(idmap_ptes); 808 809 __pi_map_range(&ptep, start, end, start, PAGE_KERNEL_ROX, 810 IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false, 811 __phys_to_virt(ptep) - ptep); 812 813 if (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0) && !arm64_use_ng_mappings) { 814 extern u32 __idmap_kpti_flag; 815 u64 pa = __pa_symbol(&__idmap_kpti_flag); 816 817 /* 818 * The KPTI G-to-nG conversion code needs a read-write mapping 819 * of its synchronization flag in the ID map. 820 */ 821 ptep = __pa_symbol(kpti_ptes); 822 __pi_map_range(&ptep, pa, pa + sizeof(u32), pa, PAGE_KERNEL, 823 IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false, 824 __phys_to_virt(ptep) - ptep); 825 } 826 } 827 828 void __init paging_init(void) 829 { 830 map_mem(swapper_pg_dir); 831 832 memblock_allow_resize(); 833 834 create_idmap(); 835 declare_kernel_vmas(); 836 } 837 838 #ifdef CONFIG_MEMORY_HOTPLUG 839 static void free_hotplug_page_range(struct page *page, size_t size, 840 struct vmem_altmap *altmap) 841 { 842 if (altmap) { 843 vmem_altmap_free(altmap, size >> PAGE_SHIFT); 844 } else { 845 WARN_ON(PageReserved(page)); 846 free_pages((unsigned long)page_address(page), get_order(size)); 847 } 848 } 849 850 static void free_hotplug_pgtable_page(struct page *page) 851 { 852 free_hotplug_page_range(page, PAGE_SIZE, NULL); 853 } 854 855 static bool pgtable_range_aligned(unsigned long start, unsigned long end, 856 unsigned long floor, unsigned long ceiling, 857 unsigned long mask) 858 { 859 start &= mask; 860 if (start < floor) 861 return false; 862 863 if (ceiling) { 864 ceiling &= mask; 865 if (!ceiling) 866 return false; 867 } 868 869 if (end - 1 > ceiling - 1) 870 return false; 871 return true; 872 } 873 874 static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, 875 unsigned long end, bool free_mapped, 876 struct vmem_altmap *altmap) 877 { 878 pte_t *ptep, pte; 879 880 do { 881 ptep = pte_offset_kernel(pmdp, addr); 882 pte = __ptep_get(ptep); 883 if (pte_none(pte)) 884 continue; 885 886 WARN_ON(!pte_present(pte)); 887 __pte_clear(&init_mm, addr, ptep); 888 flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 889 if (free_mapped) 890 free_hotplug_page_range(pte_page(pte), 891 PAGE_SIZE, altmap); 892 } while (addr += PAGE_SIZE, addr < end); 893 } 894 895 static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr, 896 unsigned long end, bool free_mapped, 897 struct vmem_altmap *altmap) 898 { 899 unsigned long next; 900 pmd_t *pmdp, pmd; 901 902 do { 903 next = pmd_addr_end(addr, end); 904 pmdp = pmd_offset(pudp, addr); 905 pmd = READ_ONCE(*pmdp); 906 if (pmd_none(pmd)) 907 continue; 908 909 WARN_ON(!pmd_present(pmd)); 910 if (pmd_sect(pmd)) { 911 pmd_clear(pmdp); 912 913 /* 914 * One TLBI should be sufficient here as the PMD_SIZE 915 * range is mapped with a single block entry. 916 */ 917 flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 918 if (free_mapped) 919 free_hotplug_page_range(pmd_page(pmd), 920 PMD_SIZE, altmap); 921 continue; 922 } 923 WARN_ON(!pmd_table(pmd)); 924 unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap); 925 } while (addr = next, addr < end); 926 } 927 928 static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr, 929 unsigned long end, bool free_mapped, 930 struct vmem_altmap *altmap) 931 { 932 unsigned long next; 933 pud_t *pudp, pud; 934 935 do { 936 next = pud_addr_end(addr, end); 937 pudp = pud_offset(p4dp, addr); 938 pud = READ_ONCE(*pudp); 939 if (pud_none(pud)) 940 continue; 941 942 WARN_ON(!pud_present(pud)); 943 if (pud_sect(pud)) { 944 pud_clear(pudp); 945 946 /* 947 * One TLBI should be sufficient here as the PUD_SIZE 948 * range is mapped with a single block entry. 949 */ 950 flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 951 if (free_mapped) 952 free_hotplug_page_range(pud_page(pud), 953 PUD_SIZE, altmap); 954 continue; 955 } 956 WARN_ON(!pud_table(pud)); 957 unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap); 958 } while (addr = next, addr < end); 959 } 960 961 static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr, 962 unsigned long end, bool free_mapped, 963 struct vmem_altmap *altmap) 964 { 965 unsigned long next; 966 p4d_t *p4dp, p4d; 967 968 do { 969 next = p4d_addr_end(addr, end); 970 p4dp = p4d_offset(pgdp, addr); 971 p4d = READ_ONCE(*p4dp); 972 if (p4d_none(p4d)) 973 continue; 974 975 WARN_ON(!p4d_present(p4d)); 976 unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap); 977 } while (addr = next, addr < end); 978 } 979 980 static void unmap_hotplug_range(unsigned long addr, unsigned long end, 981 bool free_mapped, struct vmem_altmap *altmap) 982 { 983 unsigned long next; 984 pgd_t *pgdp, pgd; 985 986 /* 987 * altmap can only be used as vmemmap mapping backing memory. 988 * In case the backing memory itself is not being freed, then 989 * altmap is irrelevant. Warn about this inconsistency when 990 * encountered. 991 */ 992 WARN_ON(!free_mapped && altmap); 993 994 do { 995 next = pgd_addr_end(addr, end); 996 pgdp = pgd_offset_k(addr); 997 pgd = READ_ONCE(*pgdp); 998 if (pgd_none(pgd)) 999 continue; 1000 1001 WARN_ON(!pgd_present(pgd)); 1002 unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap); 1003 } while (addr = next, addr < end); 1004 } 1005 1006 static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr, 1007 unsigned long end, unsigned long floor, 1008 unsigned long ceiling) 1009 { 1010 pte_t *ptep, pte; 1011 unsigned long i, start = addr; 1012 1013 do { 1014 ptep = pte_offset_kernel(pmdp, addr); 1015 pte = __ptep_get(ptep); 1016 1017 /* 1018 * This is just a sanity check here which verifies that 1019 * pte clearing has been done by earlier unmap loops. 1020 */ 1021 WARN_ON(!pte_none(pte)); 1022 } while (addr += PAGE_SIZE, addr < end); 1023 1024 if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK)) 1025 return; 1026 1027 /* 1028 * Check whether we can free the pte page if the rest of the 1029 * entries are empty. Overlap with other regions have been 1030 * handled by the floor/ceiling check. 1031 */ 1032 ptep = pte_offset_kernel(pmdp, 0UL); 1033 for (i = 0; i < PTRS_PER_PTE; i++) { 1034 if (!pte_none(__ptep_get(&ptep[i]))) 1035 return; 1036 } 1037 1038 pmd_clear(pmdp); 1039 __flush_tlb_kernel_pgtable(start); 1040 free_hotplug_pgtable_page(virt_to_page(ptep)); 1041 } 1042 1043 static void free_empty_pmd_table(pud_t *pudp, unsigned long addr, 1044 unsigned long end, unsigned long floor, 1045 unsigned long ceiling) 1046 { 1047 pmd_t *pmdp, pmd; 1048 unsigned long i, next, start = addr; 1049 1050 do { 1051 next = pmd_addr_end(addr, end); 1052 pmdp = pmd_offset(pudp, addr); 1053 pmd = READ_ONCE(*pmdp); 1054 if (pmd_none(pmd)) 1055 continue; 1056 1057 WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd)); 1058 free_empty_pte_table(pmdp, addr, next, floor, ceiling); 1059 } while (addr = next, addr < end); 1060 1061 if (CONFIG_PGTABLE_LEVELS <= 2) 1062 return; 1063 1064 if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK)) 1065 return; 1066 1067 /* 1068 * Check whether we can free the pmd page if the rest of the 1069 * entries are empty. Overlap with other regions have been 1070 * handled by the floor/ceiling check. 1071 */ 1072 pmdp = pmd_offset(pudp, 0UL); 1073 for (i = 0; i < PTRS_PER_PMD; i++) { 1074 if (!pmd_none(READ_ONCE(pmdp[i]))) 1075 return; 1076 } 1077 1078 pud_clear(pudp); 1079 __flush_tlb_kernel_pgtable(start); 1080 free_hotplug_pgtable_page(virt_to_page(pmdp)); 1081 } 1082 1083 static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr, 1084 unsigned long end, unsigned long floor, 1085 unsigned long ceiling) 1086 { 1087 pud_t *pudp, pud; 1088 unsigned long i, next, start = addr; 1089 1090 do { 1091 next = pud_addr_end(addr, end); 1092 pudp = pud_offset(p4dp, addr); 1093 pud = READ_ONCE(*pudp); 1094 if (pud_none(pud)) 1095 continue; 1096 1097 WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud)); 1098 free_empty_pmd_table(pudp, addr, next, floor, ceiling); 1099 } while (addr = next, addr < end); 1100 1101 if (!pgtable_l4_enabled()) 1102 return; 1103 1104 if (!pgtable_range_aligned(start, end, floor, ceiling, P4D_MASK)) 1105 return; 1106 1107 /* 1108 * Check whether we can free the pud page if the rest of the 1109 * entries are empty. Overlap with other regions have been 1110 * handled by the floor/ceiling check. 1111 */ 1112 pudp = pud_offset(p4dp, 0UL); 1113 for (i = 0; i < PTRS_PER_PUD; i++) { 1114 if (!pud_none(READ_ONCE(pudp[i]))) 1115 return; 1116 } 1117 1118 p4d_clear(p4dp); 1119 __flush_tlb_kernel_pgtable(start); 1120 free_hotplug_pgtable_page(virt_to_page(pudp)); 1121 } 1122 1123 static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr, 1124 unsigned long end, unsigned long floor, 1125 unsigned long ceiling) 1126 { 1127 p4d_t *p4dp, p4d; 1128 unsigned long i, next, start = addr; 1129 1130 do { 1131 next = p4d_addr_end(addr, end); 1132 p4dp = p4d_offset(pgdp, addr); 1133 p4d = READ_ONCE(*p4dp); 1134 if (p4d_none(p4d)) 1135 continue; 1136 1137 WARN_ON(!p4d_present(p4d)); 1138 free_empty_pud_table(p4dp, addr, next, floor, ceiling); 1139 } while (addr = next, addr < end); 1140 1141 if (!pgtable_l5_enabled()) 1142 return; 1143 1144 if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK)) 1145 return; 1146 1147 /* 1148 * Check whether we can free the p4d page if the rest of the 1149 * entries are empty. Overlap with other regions have been 1150 * handled by the floor/ceiling check. 1151 */ 1152 p4dp = p4d_offset(pgdp, 0UL); 1153 for (i = 0; i < PTRS_PER_P4D; i++) { 1154 if (!p4d_none(READ_ONCE(p4dp[i]))) 1155 return; 1156 } 1157 1158 pgd_clear(pgdp); 1159 __flush_tlb_kernel_pgtable(start); 1160 free_hotplug_pgtable_page(virt_to_page(p4dp)); 1161 } 1162 1163 static void free_empty_tables(unsigned long addr, unsigned long end, 1164 unsigned long floor, unsigned long ceiling) 1165 { 1166 unsigned long next; 1167 pgd_t *pgdp, pgd; 1168 1169 do { 1170 next = pgd_addr_end(addr, end); 1171 pgdp = pgd_offset_k(addr); 1172 pgd = READ_ONCE(*pgdp); 1173 if (pgd_none(pgd)) 1174 continue; 1175 1176 WARN_ON(!pgd_present(pgd)); 1177 free_empty_p4d_table(pgdp, addr, next, floor, ceiling); 1178 } while (addr = next, addr < end); 1179 } 1180 #endif 1181 1182 void __meminit vmemmap_set_pmd(pmd_t *pmdp, void *p, int node, 1183 unsigned long addr, unsigned long next) 1184 { 1185 pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL)); 1186 } 1187 1188 int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node, 1189 unsigned long addr, unsigned long next) 1190 { 1191 vmemmap_verify((pte_t *)pmdp, node, addr, next); 1192 1193 return pmd_sect(READ_ONCE(*pmdp)); 1194 } 1195 1196 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 1197 struct vmem_altmap *altmap) 1198 { 1199 WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1200 /* [start, end] should be within one section */ 1201 WARN_ON_ONCE(end - start > PAGES_PER_SECTION * sizeof(struct page)); 1202 1203 if (!IS_ENABLED(CONFIG_ARM64_4K_PAGES) || 1204 (end - start < PAGES_PER_SECTION * sizeof(struct page))) 1205 return vmemmap_populate_basepages(start, end, node, altmap); 1206 else 1207 return vmemmap_populate_hugepages(start, end, node, altmap); 1208 } 1209 1210 #ifdef CONFIG_MEMORY_HOTPLUG 1211 void vmemmap_free(unsigned long start, unsigned long end, 1212 struct vmem_altmap *altmap) 1213 { 1214 WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1215 1216 unmap_hotplug_range(start, end, true, altmap); 1217 free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END); 1218 } 1219 #endif /* CONFIG_MEMORY_HOTPLUG */ 1220 1221 int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot) 1222 { 1223 pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot)); 1224 1225 /* Only allow permission changes for now */ 1226 if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)), 1227 pud_val(new_pud))) 1228 return 0; 1229 1230 VM_BUG_ON(phys & ~PUD_MASK); 1231 set_pud(pudp, new_pud); 1232 return 1; 1233 } 1234 1235 int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot) 1236 { 1237 pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot)); 1238 1239 /* Only allow permission changes for now */ 1240 if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)), 1241 pmd_val(new_pmd))) 1242 return 0; 1243 1244 VM_BUG_ON(phys & ~PMD_MASK); 1245 set_pmd(pmdp, new_pmd); 1246 return 1; 1247 } 1248 1249 #ifndef __PAGETABLE_P4D_FOLDED 1250 void p4d_clear_huge(p4d_t *p4dp) 1251 { 1252 } 1253 #endif 1254 1255 int pud_clear_huge(pud_t *pudp) 1256 { 1257 if (!pud_sect(READ_ONCE(*pudp))) 1258 return 0; 1259 pud_clear(pudp); 1260 return 1; 1261 } 1262 1263 int pmd_clear_huge(pmd_t *pmdp) 1264 { 1265 if (!pmd_sect(READ_ONCE(*pmdp))) 1266 return 0; 1267 pmd_clear(pmdp); 1268 return 1; 1269 } 1270 1271 int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr) 1272 { 1273 pte_t *table; 1274 pmd_t pmd; 1275 1276 pmd = READ_ONCE(*pmdp); 1277 1278 if (!pmd_table(pmd)) { 1279 VM_WARN_ON(1); 1280 return 1; 1281 } 1282 1283 table = pte_offset_kernel(pmdp, addr); 1284 pmd_clear(pmdp); 1285 __flush_tlb_kernel_pgtable(addr); 1286 pte_free_kernel(NULL, table); 1287 return 1; 1288 } 1289 1290 int pud_free_pmd_page(pud_t *pudp, unsigned long addr) 1291 { 1292 pmd_t *table; 1293 pmd_t *pmdp; 1294 pud_t pud; 1295 unsigned long next, end; 1296 1297 pud = READ_ONCE(*pudp); 1298 1299 if (!pud_table(pud)) { 1300 VM_WARN_ON(1); 1301 return 1; 1302 } 1303 1304 table = pmd_offset(pudp, addr); 1305 pmdp = table; 1306 next = addr; 1307 end = addr + PUD_SIZE; 1308 do { 1309 if (pmd_present(pmdp_get(pmdp))) 1310 pmd_free_pte_page(pmdp, next); 1311 } while (pmdp++, next += PMD_SIZE, next != end); 1312 1313 pud_clear(pudp); 1314 __flush_tlb_kernel_pgtable(addr); 1315 pmd_free(NULL, table); 1316 return 1; 1317 } 1318 1319 #ifdef CONFIG_MEMORY_HOTPLUG 1320 static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size) 1321 { 1322 unsigned long end = start + size; 1323 1324 WARN_ON(pgdir != init_mm.pgd); 1325 WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END)); 1326 1327 unmap_hotplug_range(start, end, false, NULL); 1328 free_empty_tables(start, end, PAGE_OFFSET, PAGE_END); 1329 } 1330 1331 struct range arch_get_mappable_range(void) 1332 { 1333 struct range mhp_range; 1334 u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual)); 1335 u64 end_linear_pa = __pa(PAGE_END - 1); 1336 1337 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 1338 /* 1339 * Check for a wrap, it is possible because of randomized linear 1340 * mapping the start physical address is actually bigger than 1341 * the end physical address. In this case set start to zero 1342 * because [0, end_linear_pa] range must still be able to cover 1343 * all addressable physical addresses. 1344 */ 1345 if (start_linear_pa > end_linear_pa) 1346 start_linear_pa = 0; 1347 } 1348 1349 WARN_ON(start_linear_pa > end_linear_pa); 1350 1351 /* 1352 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)] 1353 * accommodating both its ends but excluding PAGE_END. Max physical 1354 * range which can be mapped inside this linear mapping range, must 1355 * also be derived from its end points. 1356 */ 1357 mhp_range.start = start_linear_pa; 1358 mhp_range.end = end_linear_pa; 1359 1360 return mhp_range; 1361 } 1362 1363 int arch_add_memory(int nid, u64 start, u64 size, 1364 struct mhp_params *params) 1365 { 1366 int ret, flags = NO_EXEC_MAPPINGS; 1367 1368 VM_BUG_ON(!mhp_range_allowed(start, size, true)); 1369 1370 if (can_set_direct_map()) 1371 flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 1372 1373 __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start), 1374 size, params->pgprot, pgd_pgtable_alloc_init_mm, 1375 flags); 1376 1377 memblock_clear_nomap(start, size); 1378 1379 ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT, 1380 params); 1381 if (ret) 1382 __remove_pgd_mapping(swapper_pg_dir, 1383 __phys_to_virt(start), size); 1384 else { 1385 /* Address of hotplugged memory can be smaller */ 1386 max_pfn = max(max_pfn, PFN_UP(start + size)); 1387 max_low_pfn = max_pfn; 1388 } 1389 1390 return ret; 1391 } 1392 1393 void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) 1394 { 1395 unsigned long start_pfn = start >> PAGE_SHIFT; 1396 unsigned long nr_pages = size >> PAGE_SHIFT; 1397 1398 __remove_pages(start_pfn, nr_pages, altmap); 1399 __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size); 1400 } 1401 1402 /* 1403 * This memory hotplug notifier helps prevent boot memory from being 1404 * inadvertently removed as it blocks pfn range offlining process in 1405 * __offline_pages(). Hence this prevents both offlining as well as 1406 * removal process for boot memory which is initially always online. 1407 * In future if and when boot memory could be removed, this notifier 1408 * should be dropped and free_hotplug_page_range() should handle any 1409 * reserved pages allocated during boot. 1410 */ 1411 static int prevent_bootmem_remove_notifier(struct notifier_block *nb, 1412 unsigned long action, void *data) 1413 { 1414 struct mem_section *ms; 1415 struct memory_notify *arg = data; 1416 unsigned long end_pfn = arg->start_pfn + arg->nr_pages; 1417 unsigned long pfn = arg->start_pfn; 1418 1419 if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE)) 1420 return NOTIFY_OK; 1421 1422 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 1423 unsigned long start = PFN_PHYS(pfn); 1424 unsigned long end = start + (1UL << PA_SECTION_SHIFT); 1425 1426 ms = __pfn_to_section(pfn); 1427 if (!early_section(ms)) 1428 continue; 1429 1430 if (action == MEM_GOING_OFFLINE) { 1431 /* 1432 * Boot memory removal is not supported. Prevent 1433 * it via blocking any attempted offline request 1434 * for the boot memory and just report it. 1435 */ 1436 pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end); 1437 return NOTIFY_BAD; 1438 } else if (action == MEM_OFFLINE) { 1439 /* 1440 * This should have never happened. Boot memory 1441 * offlining should have been prevented by this 1442 * very notifier. Probably some memory removal 1443 * procedure might have changed which would then 1444 * require further debug. 1445 */ 1446 pr_err("Boot memory [%lx %lx] offlined\n", start, end); 1447 1448 /* 1449 * Core memory hotplug does not process a return 1450 * code from the notifier for MEM_OFFLINE events. 1451 * The error condition has been reported. Return 1452 * from here as if ignored. 1453 */ 1454 return NOTIFY_DONE; 1455 } 1456 } 1457 return NOTIFY_OK; 1458 } 1459 1460 static struct notifier_block prevent_bootmem_remove_nb = { 1461 .notifier_call = prevent_bootmem_remove_notifier, 1462 }; 1463 1464 /* 1465 * This ensures that boot memory sections on the platform are online 1466 * from early boot. Memory sections could not be prevented from being 1467 * offlined, unless for some reason they are not online to begin with. 1468 * This helps validate the basic assumption on which the above memory 1469 * event notifier works to prevent boot memory section offlining and 1470 * its possible removal. 1471 */ 1472 static void validate_bootmem_online(void) 1473 { 1474 phys_addr_t start, end, addr; 1475 struct mem_section *ms; 1476 u64 i; 1477 1478 /* 1479 * Scanning across all memblock might be expensive 1480 * on some big memory systems. Hence enable this 1481 * validation only with DEBUG_VM. 1482 */ 1483 if (!IS_ENABLED(CONFIG_DEBUG_VM)) 1484 return; 1485 1486 for_each_mem_range(i, &start, &end) { 1487 for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) { 1488 ms = __pfn_to_section(PHYS_PFN(addr)); 1489 1490 /* 1491 * All memory ranges in the system at this point 1492 * should have been marked as early sections. 1493 */ 1494 WARN_ON(!early_section(ms)); 1495 1496 /* 1497 * Memory notifier mechanism here to prevent boot 1498 * memory offlining depends on the fact that each 1499 * early section memory on the system is initially 1500 * online. Otherwise a given memory section which 1501 * is already offline will be overlooked and can 1502 * be removed completely. Call out such sections. 1503 */ 1504 if (!online_section(ms)) 1505 pr_err("Boot memory [%llx %llx] is offline, can be removed\n", 1506 addr, addr + (1UL << PA_SECTION_SHIFT)); 1507 } 1508 } 1509 } 1510 1511 static int __init prevent_bootmem_remove_init(void) 1512 { 1513 int ret = 0; 1514 1515 if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE)) 1516 return ret; 1517 1518 validate_bootmem_online(); 1519 ret = register_memory_notifier(&prevent_bootmem_remove_nb); 1520 if (ret) 1521 pr_err("%s: Notifier registration failed %d\n", __func__, ret); 1522 1523 return ret; 1524 } 1525 early_initcall(prevent_bootmem_remove_init); 1526 #endif 1527 1528 pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr, 1529 pte_t *ptep, unsigned int nr) 1530 { 1531 pte_t pte = get_and_clear_ptes(vma->vm_mm, addr, ptep, nr); 1532 1533 if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) { 1534 /* 1535 * Break-before-make (BBM) is required for all user space mappings 1536 * when the permission changes from executable to non-executable 1537 * in cases where cpu is affected with errata #2645198. 1538 */ 1539 if (pte_accessible(vma->vm_mm, pte) && pte_user_exec(pte)) 1540 __flush_tlb_range(vma, addr, nr * PAGE_SIZE, 1541 PAGE_SIZE, true, 3); 1542 } 1543 1544 return pte; 1545 } 1546 1547 pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) 1548 { 1549 return modify_prot_start_ptes(vma, addr, ptep, 1); 1550 } 1551 1552 void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr, 1553 pte_t *ptep, pte_t old_pte, pte_t pte, 1554 unsigned int nr) 1555 { 1556 set_ptes(vma->vm_mm, addr, ptep, pte, nr); 1557 } 1558 1559 void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, 1560 pte_t old_pte, pte_t pte) 1561 { 1562 modify_prot_commit_ptes(vma, addr, ptep, old_pte, pte, 1); 1563 } 1564 1565 /* 1566 * Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD, 1567 * avoiding the possibility of conflicting TLB entries being allocated. 1568 */ 1569 void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp) 1570 { 1571 typedef void (ttbr_replace_func)(phys_addr_t); 1572 extern ttbr_replace_func idmap_cpu_replace_ttbr1; 1573 ttbr_replace_func *replace_phys; 1574 unsigned long daif; 1575 1576 /* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */ 1577 phys_addr_t ttbr1 = phys_to_ttbr(virt_to_phys(pgdp)); 1578 1579 if (cnp) 1580 ttbr1 |= TTBR_CNP_BIT; 1581 1582 replace_phys = (void *)__pa_symbol(idmap_cpu_replace_ttbr1); 1583 1584 cpu_install_idmap(); 1585 1586 /* 1587 * We really don't want to take *any* exceptions while TTBR1 is 1588 * in the process of being replaced so mask everything. 1589 */ 1590 daif = local_daif_save(); 1591 replace_phys(ttbr1); 1592 local_daif_restore(daif); 1593 1594 cpu_uninstall_idmap(); 1595 } 1596 1597 #ifdef CONFIG_ARCH_HAS_PKEYS 1598 int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, unsigned long init_val) 1599 { 1600 u64 new_por; 1601 u64 old_por; 1602 1603 if (!system_supports_poe()) 1604 return -ENOSPC; 1605 1606 /* 1607 * This code should only be called with valid 'pkey' 1608 * values originating from in-kernel users. Complain 1609 * if a bad value is observed. 1610 */ 1611 if (WARN_ON_ONCE(pkey >= arch_max_pkey())) 1612 return -EINVAL; 1613 1614 /* Set the bits we need in POR: */ 1615 new_por = POE_RWX; 1616 if (init_val & PKEY_DISABLE_WRITE) 1617 new_por &= ~POE_W; 1618 if (init_val & PKEY_DISABLE_ACCESS) 1619 new_por &= ~POE_RW; 1620 if (init_val & PKEY_DISABLE_READ) 1621 new_por &= ~POE_R; 1622 if (init_val & PKEY_DISABLE_EXECUTE) 1623 new_por &= ~POE_X; 1624 1625 /* Shift the bits in to the correct place in POR for pkey: */ 1626 new_por = POR_ELx_PERM_PREP(pkey, new_por); 1627 1628 /* Get old POR and mask off any old bits in place: */ 1629 old_por = read_sysreg_s(SYS_POR_EL0); 1630 old_por &= ~(POE_MASK << POR_ELx_PERM_SHIFT(pkey)); 1631 1632 /* Write old part along with new part: */ 1633 write_sysreg_s(old_por | new_por, SYS_POR_EL0); 1634 1635 return 0; 1636 } 1637 #endif 1638