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