1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2c1cc1552SCatalin Marinas /* 3c1cc1552SCatalin Marinas * Based on arch/arm/mm/mmu.c 4c1cc1552SCatalin Marinas * 5c1cc1552SCatalin Marinas * Copyright (C) 1995-2005 Russell King 6c1cc1552SCatalin Marinas * Copyright (C) 2012 ARM Ltd. 7c1cc1552SCatalin Marinas */ 8c1cc1552SCatalin Marinas 95a9e3e15SJisheng Zhang #include <linux/cache.h> 10c1cc1552SCatalin Marinas #include <linux/export.h> 11c1cc1552SCatalin Marinas #include <linux/kernel.h> 12c1cc1552SCatalin Marinas #include <linux/errno.h> 13c1cc1552SCatalin Marinas #include <linux/init.h> 1498d2e153STakahiro Akashi #include <linux/ioport.h> 1598d2e153STakahiro Akashi #include <linux/kexec.h> 1661bd93ceSArd Biesheuvel #include <linux/libfdt.h> 17c1cc1552SCatalin Marinas #include <linux/mman.h> 18c1cc1552SCatalin Marinas #include <linux/nodemask.h> 19c1cc1552SCatalin Marinas #include <linux/memblock.h> 20bbd6ec60SAnshuman Khandual #include <linux/memory.h> 21c1cc1552SCatalin Marinas #include <linux/fs.h> 222475ff9dSCatalin Marinas #include <linux/io.h> 232077be67SLaura Abbott #include <linux/mm.h> 246efd8499STobias Klauser #include <linux/vmalloc.h> 25c1cc1552SCatalin Marinas 2621ab99c2SMark Rutland #include <asm/barrier.h> 27c1cc1552SCatalin Marinas #include <asm/cputype.h> 28af86e597SLaura Abbott #include <asm/fixmap.h> 29068a17a5SMark Rutland #include <asm/kasan.h> 30b433dce0SSuzuki K. Poulose #include <asm/kernel-pgtable.h> 31c1cc1552SCatalin Marinas #include <asm/sections.h> 32c1cc1552SCatalin Marinas #include <asm/setup.h> 3387dfb311SMasahiro Yamada #include <linux/sizes.h> 34c1cc1552SCatalin Marinas #include <asm/tlb.h> 35c1cc1552SCatalin Marinas #include <asm/mmu_context.h> 361404d6f1SLaura Abbott #include <asm/ptdump.h> 37ec28bb9cSChintan Pandya #include <asm/tlbflush.h> 38ca15ca40SMike Rapoport #include <asm/pgalloc.h> 39c1cc1552SCatalin Marinas 40c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS BIT(0) 41d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS BIT(1) 4287143f40SArd Biesheuvel #define NO_EXEC_MAPPINGS BIT(2) /* assumes FEAT_HPDS is not used */ 43c0951366SArd Biesheuvel 447ba8f2b2SArd Biesheuvel u64 idmap_t0sz = TCR_T0SZ(VA_BITS_MIN); 45fa2a8445SKristina Martsenko u64 idmap_ptrs_per_pgd = PTRS_PER_PGD; 46dd006da2SArd Biesheuvel 475383cc6eSSteve Capper u64 __section(".mmuoff.data.write") vabits_actual; 485383cc6eSSteve Capper EXPORT_SYMBOL(vabits_actual); 49c1cc1552SCatalin Marinas 505a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init; 51a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset); 52a7f8de16SArd Biesheuvel 53c1cc1552SCatalin Marinas /* 54c1cc1552SCatalin Marinas * Empty_zero_page is a special page that is used for zero-initialized data 55c1cc1552SCatalin Marinas * and COW. 56c1cc1552SCatalin Marinas */ 575227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss; 58c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page); 59c1cc1552SCatalin Marinas 60f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss; 61f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused; 62f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused; 63f9040773SArd Biesheuvel 642330b7caSJun Yao static DEFINE_SPINLOCK(swapper_pgdir_lock); 652330b7caSJun Yao 662330b7caSJun Yao void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd) 672330b7caSJun Yao { 682330b7caSJun Yao pgd_t *fixmap_pgdp; 692330b7caSJun Yao 702330b7caSJun Yao spin_lock(&swapper_pgdir_lock); 7126a6f87eSJames Morse fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp)); 722330b7caSJun Yao WRITE_ONCE(*fixmap_pgdp, pgd); 732330b7caSJun Yao /* 742330b7caSJun Yao * We need dsb(ishst) here to ensure the page-table-walker sees 752330b7caSJun Yao * our new entry before set_p?d() returns. The fixmap's 762330b7caSJun Yao * flush_tlb_kernel_range() via clear_fixmap() does this for us. 772330b7caSJun Yao */ 782330b7caSJun Yao pgd_clear_fixmap(); 792330b7caSJun Yao spin_unlock(&swapper_pgdir_lock); 802330b7caSJun Yao } 812330b7caSJun Yao 82c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 83c1cc1552SCatalin Marinas unsigned long size, pgprot_t vma_prot) 84c1cc1552SCatalin Marinas { 85c1cc1552SCatalin Marinas if (!pfn_valid(pfn)) 86c1cc1552SCatalin Marinas return pgprot_noncached(vma_prot); 87c1cc1552SCatalin Marinas else if (file->f_flags & O_SYNC) 88c1cc1552SCatalin Marinas return pgprot_writecombine(vma_prot); 89c1cc1552SCatalin Marinas return vma_prot; 90c1cc1552SCatalin Marinas } 91c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot); 92c1cc1552SCatalin Marinas 9390292acaSYu Zhao static phys_addr_t __init early_pgtable_alloc(int shift) 94c1cc1552SCatalin Marinas { 957142392dSSuzuki K. Poulose phys_addr_t phys; 967142392dSSuzuki K. Poulose void *ptr; 977142392dSSuzuki K. Poulose 989a8dd708SMike Rapoport phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); 99ecc3e771SMike Rapoport if (!phys) 100ecc3e771SMike Rapoport panic("Failed to allocate page table page\n"); 101f4710445SMark Rutland 102f4710445SMark Rutland /* 103f4710445SMark Rutland * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE 104f4710445SMark Rutland * slot will be free, so we can (ab)use the FIX_PTE slot to initialise 105f4710445SMark Rutland * any level of table. 106f4710445SMark Rutland */ 107f4710445SMark Rutland ptr = pte_set_fixmap(phys); 108f4710445SMark Rutland 10921ab99c2SMark Rutland memset(ptr, 0, PAGE_SIZE); 11021ab99c2SMark Rutland 111f4710445SMark Rutland /* 112f4710445SMark Rutland * Implicit barriers also ensure the zeroed page is visible to the page 113f4710445SMark Rutland * table walker 114f4710445SMark Rutland */ 115f4710445SMark Rutland pte_clear_fixmap(); 116f4710445SMark Rutland 117f4710445SMark Rutland return phys; 118c1cc1552SCatalin Marinas } 119c1cc1552SCatalin Marinas 120e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new) 121e98216b5SArd Biesheuvel { 122e98216b5SArd Biesheuvel /* 123e98216b5SArd Biesheuvel * The following mapping attributes may be updated in live 124e98216b5SArd Biesheuvel * kernel mappings without the need for break-before-make. 125e98216b5SArd Biesheuvel */ 1260178dc76SCatalin Marinas pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG; 127e98216b5SArd Biesheuvel 128141d1497SArd Biesheuvel /* creating or taking down mappings is always safe */ 129141d1497SArd Biesheuvel if (old == 0 || new == 0) 130141d1497SArd Biesheuvel return true; 131141d1497SArd Biesheuvel 132141d1497SArd Biesheuvel /* live contiguous mappings may not be manipulated at all */ 133141d1497SArd Biesheuvel if ((old | new) & PTE_CONT) 134141d1497SArd Biesheuvel return false; 135141d1497SArd Biesheuvel 136753e8abcSArd Biesheuvel /* Transitioning from Non-Global to Global is unsafe */ 137753e8abcSArd Biesheuvel if (old & ~new & PTE_NG) 138753e8abcSArd Biesheuvel return false; 1394e602056SWill Deacon 1400178dc76SCatalin Marinas /* 1410178dc76SCatalin Marinas * Changing the memory type between Normal and Normal-Tagged is safe 1420178dc76SCatalin Marinas * since Tagged is considered a permission attribute from the 1430178dc76SCatalin Marinas * mismatched attribute aliases perspective. 1440178dc76SCatalin Marinas */ 1450178dc76SCatalin Marinas if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 1460178dc76SCatalin Marinas (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) && 1470178dc76SCatalin Marinas ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 1480178dc76SCatalin Marinas (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED))) 1490178dc76SCatalin Marinas mask |= PTE_ATTRINDX_MASK; 1500178dc76SCatalin Marinas 151141d1497SArd Biesheuvel return ((old ^ new) & ~mask) == 0; 152e98216b5SArd Biesheuvel } 153e98216b5SArd Biesheuvel 15420a004e7SWill Deacon static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end, 155d27cfa1fSArd Biesheuvel phys_addr_t phys, pgprot_t prot) 156c1cc1552SCatalin Marinas { 15720a004e7SWill Deacon pte_t *ptep; 158c1cc1552SCatalin Marinas 15920a004e7SWill Deacon ptep = pte_set_fixmap_offset(pmdp, addr); 160c1cc1552SCatalin Marinas do { 16120a004e7SWill Deacon pte_t old_pte = READ_ONCE(*ptep); 162e98216b5SArd Biesheuvel 16320a004e7SWill Deacon set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot)); 164e98216b5SArd Biesheuvel 165e98216b5SArd Biesheuvel /* 166e98216b5SArd Biesheuvel * After the PTE entry has been populated once, we 167e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 168e98216b5SArd Biesheuvel */ 16920a004e7SWill Deacon BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), 17020a004e7SWill Deacon READ_ONCE(pte_val(*ptep)))); 171e98216b5SArd Biesheuvel 172e393cf40SArd Biesheuvel phys += PAGE_SIZE; 17320a004e7SWill Deacon } while (ptep++, addr += PAGE_SIZE, addr != end); 174f4710445SMark Rutland 175f4710445SMark Rutland pte_clear_fixmap(); 176c1cc1552SCatalin Marinas } 177c1cc1552SCatalin Marinas 17820a004e7SWill Deacon static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr, 179d27cfa1fSArd Biesheuvel unsigned long end, phys_addr_t phys, 180d27cfa1fSArd Biesheuvel pgprot_t prot, 18190292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 182c0951366SArd Biesheuvel int flags) 183c1cc1552SCatalin Marinas { 184c1cc1552SCatalin Marinas unsigned long next; 18520a004e7SWill Deacon pmd_t pmd = READ_ONCE(*pmdp); 186c1cc1552SCatalin Marinas 18720a004e7SWill Deacon BUG_ON(pmd_sect(pmd)); 18820a004e7SWill Deacon if (pmd_none(pmd)) { 18987143f40SArd Biesheuvel pmdval_t pmdval = PMD_TYPE_TABLE | PMD_TABLE_UXN; 190d27cfa1fSArd Biesheuvel phys_addr_t pte_phys; 19187143f40SArd Biesheuvel 19287143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 19387143f40SArd Biesheuvel pmdval |= PMD_TABLE_PXN; 194132233a7SLaura Abbott BUG_ON(!pgtable_alloc); 19590292acaSYu Zhao pte_phys = pgtable_alloc(PAGE_SHIFT); 19687143f40SArd Biesheuvel __pmd_populate(pmdp, pte_phys, pmdval); 19720a004e7SWill Deacon pmd = READ_ONCE(*pmdp); 198c1cc1552SCatalin Marinas } 19920a004e7SWill Deacon BUG_ON(pmd_bad(pmd)); 200d27cfa1fSArd Biesheuvel 201d27cfa1fSArd Biesheuvel do { 202d27cfa1fSArd Biesheuvel pgprot_t __prot = prot; 203d27cfa1fSArd Biesheuvel 204d27cfa1fSArd Biesheuvel next = pte_cont_addr_end(addr, end); 205d27cfa1fSArd Biesheuvel 206d27cfa1fSArd Biesheuvel /* use a contiguous mapping if the range is suitably aligned */ 207d27cfa1fSArd Biesheuvel if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) && 208d27cfa1fSArd Biesheuvel (flags & NO_CONT_MAPPINGS) == 0) 209d27cfa1fSArd Biesheuvel __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 210d27cfa1fSArd Biesheuvel 21120a004e7SWill Deacon init_pte(pmdp, addr, next, phys, __prot); 212d27cfa1fSArd Biesheuvel 213d27cfa1fSArd Biesheuvel phys += next - addr; 214d27cfa1fSArd Biesheuvel } while (addr = next, addr != end); 215d27cfa1fSArd Biesheuvel } 216d27cfa1fSArd Biesheuvel 21720a004e7SWill Deacon static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end, 218d27cfa1fSArd Biesheuvel phys_addr_t phys, pgprot_t prot, 21990292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), int flags) 220d27cfa1fSArd Biesheuvel { 221d27cfa1fSArd Biesheuvel unsigned long next; 22220a004e7SWill Deacon pmd_t *pmdp; 223c1cc1552SCatalin Marinas 22420a004e7SWill Deacon pmdp = pmd_set_fixmap_offset(pudp, addr); 225c1cc1552SCatalin Marinas do { 22620a004e7SWill Deacon pmd_t old_pmd = READ_ONCE(*pmdp); 227e98216b5SArd Biesheuvel 228c1cc1552SCatalin Marinas next = pmd_addr_end(addr, end); 229e98216b5SArd Biesheuvel 230c1cc1552SCatalin Marinas /* try section mapping first */ 23183863f25SLaura Abbott if (((addr | next | phys) & ~SECTION_MASK) == 0 && 232c0951366SArd Biesheuvel (flags & NO_BLOCK_MAPPINGS) == 0) { 23320a004e7SWill Deacon pmd_set_huge(pmdp, phys, prot); 234e98216b5SArd Biesheuvel 235a55f9929SCatalin Marinas /* 236e98216b5SArd Biesheuvel * After the PMD entry has been populated once, we 237e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 238a55f9929SCatalin Marinas */ 239e98216b5SArd Biesheuvel BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd), 24020a004e7SWill Deacon READ_ONCE(pmd_val(*pmdp)))); 241a55f9929SCatalin Marinas } else { 24220a004e7SWill Deacon alloc_init_cont_pte(pmdp, addr, next, phys, prot, 243d27cfa1fSArd Biesheuvel pgtable_alloc, flags); 244e98216b5SArd Biesheuvel 245e98216b5SArd Biesheuvel BUG_ON(pmd_val(old_pmd) != 0 && 24620a004e7SWill Deacon pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp))); 247a55f9929SCatalin Marinas } 248c1cc1552SCatalin Marinas phys += next - addr; 24920a004e7SWill Deacon } while (pmdp++, addr = next, addr != end); 250f4710445SMark Rutland 251f4710445SMark Rutland pmd_clear_fixmap(); 252c1cc1552SCatalin Marinas } 253c1cc1552SCatalin Marinas 25420a004e7SWill Deacon static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr, 255d27cfa1fSArd Biesheuvel unsigned long end, phys_addr_t phys, 256d27cfa1fSArd Biesheuvel pgprot_t prot, 25790292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), int flags) 258d27cfa1fSArd Biesheuvel { 259d27cfa1fSArd Biesheuvel unsigned long next; 26020a004e7SWill Deacon pud_t pud = READ_ONCE(*pudp); 261d27cfa1fSArd Biesheuvel 262d27cfa1fSArd Biesheuvel /* 263d27cfa1fSArd Biesheuvel * Check for initial section mappings in the pgd/pud. 264d27cfa1fSArd Biesheuvel */ 26520a004e7SWill Deacon BUG_ON(pud_sect(pud)); 26620a004e7SWill Deacon if (pud_none(pud)) { 26787143f40SArd Biesheuvel pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_UXN; 268d27cfa1fSArd Biesheuvel phys_addr_t pmd_phys; 26987143f40SArd Biesheuvel 27087143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 27187143f40SArd Biesheuvel pudval |= PUD_TABLE_PXN; 272d27cfa1fSArd Biesheuvel BUG_ON(!pgtable_alloc); 27390292acaSYu Zhao pmd_phys = pgtable_alloc(PMD_SHIFT); 27487143f40SArd Biesheuvel __pud_populate(pudp, pmd_phys, pudval); 27520a004e7SWill Deacon pud = READ_ONCE(*pudp); 276d27cfa1fSArd Biesheuvel } 27720a004e7SWill Deacon BUG_ON(pud_bad(pud)); 278d27cfa1fSArd Biesheuvel 279d27cfa1fSArd Biesheuvel do { 280d27cfa1fSArd Biesheuvel pgprot_t __prot = prot; 281d27cfa1fSArd Biesheuvel 282d27cfa1fSArd Biesheuvel next = pmd_cont_addr_end(addr, end); 283d27cfa1fSArd Biesheuvel 284d27cfa1fSArd Biesheuvel /* use a contiguous mapping if the range is suitably aligned */ 285d27cfa1fSArd Biesheuvel if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) && 286d27cfa1fSArd Biesheuvel (flags & NO_CONT_MAPPINGS) == 0) 287d27cfa1fSArd Biesheuvel __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 288d27cfa1fSArd Biesheuvel 28920a004e7SWill Deacon init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags); 290d27cfa1fSArd Biesheuvel 291d27cfa1fSArd Biesheuvel phys += next - addr; 292d27cfa1fSArd Biesheuvel } while (addr = next, addr != end); 293d27cfa1fSArd Biesheuvel } 294d27cfa1fSArd Biesheuvel 295da141706SLaura Abbott static inline bool use_1G_block(unsigned long addr, unsigned long next, 296da141706SLaura Abbott unsigned long phys) 297da141706SLaura Abbott { 298da141706SLaura Abbott if (PAGE_SHIFT != 12) 299da141706SLaura Abbott return false; 300da141706SLaura Abbott 301da141706SLaura Abbott if (((addr | next | phys) & ~PUD_MASK) != 0) 302da141706SLaura Abbott return false; 303da141706SLaura Abbott 304da141706SLaura Abbott return true; 305da141706SLaura Abbott } 306da141706SLaura Abbott 30720a004e7SWill Deacon static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, 308da141706SLaura Abbott phys_addr_t phys, pgprot_t prot, 30990292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 310c0951366SArd Biesheuvel int flags) 311c1cc1552SCatalin Marinas { 312c1cc1552SCatalin Marinas unsigned long next; 31320a004e7SWill Deacon pud_t *pudp; 314e9f63768SMike Rapoport p4d_t *p4dp = p4d_offset(pgdp, addr); 315e9f63768SMike Rapoport p4d_t p4d = READ_ONCE(*p4dp); 316c1cc1552SCatalin Marinas 317e9f63768SMike Rapoport if (p4d_none(p4d)) { 31887143f40SArd Biesheuvel p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN; 319132233a7SLaura Abbott phys_addr_t pud_phys; 32087143f40SArd Biesheuvel 32187143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 32287143f40SArd Biesheuvel p4dval |= P4D_TABLE_PXN; 323132233a7SLaura Abbott BUG_ON(!pgtable_alloc); 32490292acaSYu Zhao pud_phys = pgtable_alloc(PUD_SHIFT); 32587143f40SArd Biesheuvel __p4d_populate(p4dp, pud_phys, p4dval); 326e9f63768SMike Rapoport p4d = READ_ONCE(*p4dp); 327c79b954bSJungseok Lee } 328e9f63768SMike Rapoport BUG_ON(p4d_bad(p4d)); 329c79b954bSJungseok Lee 330e9f63768SMike Rapoport pudp = pud_set_fixmap_offset(p4dp, addr); 331c1cc1552SCatalin Marinas do { 33220a004e7SWill Deacon pud_t old_pud = READ_ONCE(*pudp); 333e98216b5SArd Biesheuvel 334c1cc1552SCatalin Marinas next = pud_addr_end(addr, end); 335206a2a73SSteve Capper 336206a2a73SSteve Capper /* 337206a2a73SSteve Capper * For 4K granule only, attempt to put down a 1GB block 338206a2a73SSteve Capper */ 339c0951366SArd Biesheuvel if (use_1G_block(addr, next, phys) && 340c0951366SArd Biesheuvel (flags & NO_BLOCK_MAPPINGS) == 0) { 34120a004e7SWill Deacon pud_set_huge(pudp, phys, prot); 342206a2a73SSteve Capper 343206a2a73SSteve Capper /* 344e98216b5SArd Biesheuvel * After the PUD entry has been populated once, we 345e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 346206a2a73SSteve Capper */ 347e98216b5SArd Biesheuvel BUG_ON(!pgattr_change_is_safe(pud_val(old_pud), 34820a004e7SWill Deacon READ_ONCE(pud_val(*pudp)))); 349206a2a73SSteve Capper } else { 35020a004e7SWill Deacon alloc_init_cont_pmd(pudp, addr, next, phys, prot, 351c0951366SArd Biesheuvel pgtable_alloc, flags); 352e98216b5SArd Biesheuvel 353e98216b5SArd Biesheuvel BUG_ON(pud_val(old_pud) != 0 && 35420a004e7SWill Deacon pud_val(old_pud) != READ_ONCE(pud_val(*pudp))); 355206a2a73SSteve Capper } 356c1cc1552SCatalin Marinas phys += next - addr; 35720a004e7SWill Deacon } while (pudp++, addr = next, addr != end); 358f4710445SMark Rutland 359f4710445SMark Rutland pud_clear_fixmap(); 360c1cc1552SCatalin Marinas } 361c1cc1552SCatalin Marinas 36240f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, 36340f87d31SArd Biesheuvel unsigned long virt, phys_addr_t size, 36440f87d31SArd Biesheuvel pgprot_t prot, 36590292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 366c0951366SArd Biesheuvel int flags) 367c1cc1552SCatalin Marinas { 36832d18708SMasahiro Yamada unsigned long addr, end, next; 369974b9b2cSMike Rapoport pgd_t *pgdp = pgd_offset_pgd(pgdir, virt); 370c1cc1552SCatalin Marinas 371cc5d2b3bSMark Rutland /* 372cc5d2b3bSMark Rutland * If the virtual and physical address don't have the same offset 373cc5d2b3bSMark Rutland * within a page, we cannot map the region as the caller expects. 374cc5d2b3bSMark Rutland */ 375cc5d2b3bSMark Rutland if (WARN_ON((phys ^ virt) & ~PAGE_MASK)) 376cc5d2b3bSMark Rutland return; 377cc5d2b3bSMark Rutland 3789c4e08a3SMark Rutland phys &= PAGE_MASK; 379c1cc1552SCatalin Marinas addr = virt & PAGE_MASK; 38032d18708SMasahiro Yamada end = PAGE_ALIGN(virt + size); 381c1cc1552SCatalin Marinas 382c1cc1552SCatalin Marinas do { 383c1cc1552SCatalin Marinas next = pgd_addr_end(addr, end); 38420a004e7SWill Deacon alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc, 385c0951366SArd Biesheuvel flags); 386c1cc1552SCatalin Marinas phys += next - addr; 38720a004e7SWill Deacon } while (pgdp++, addr = next, addr != end); 388c1cc1552SCatalin Marinas } 389c1cc1552SCatalin Marinas 390475ba3fcSWill Deacon static phys_addr_t __pgd_pgtable_alloc(int shift) 391369aaab8SYu Zhao { 39250f11a8aSMike Rapoport void *ptr = (void *)__get_free_page(GFP_PGTABLE_KERNEL); 393369aaab8SYu Zhao BUG_ON(!ptr); 394369aaab8SYu Zhao 395369aaab8SYu Zhao /* Ensure the zeroed page is visible to the page table walker */ 396369aaab8SYu Zhao dsb(ishst); 397369aaab8SYu Zhao return __pa(ptr); 398369aaab8SYu Zhao } 399369aaab8SYu Zhao 40090292acaSYu Zhao static phys_addr_t pgd_pgtable_alloc(int shift) 401da141706SLaura Abbott { 402475ba3fcSWill Deacon phys_addr_t pa = __pgd_pgtable_alloc(shift); 40390292acaSYu Zhao 40490292acaSYu Zhao /* 40590292acaSYu Zhao * Call proper page table ctor in case later we need to 40690292acaSYu Zhao * call core mm functions like apply_to_page_range() on 40790292acaSYu Zhao * this pre-allocated page table. 40890292acaSYu Zhao * 40990292acaSYu Zhao * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is 41090292acaSYu Zhao * folded, and if so pgtable_pmd_page_ctor() becomes nop. 41190292acaSYu Zhao */ 41290292acaSYu Zhao if (shift == PAGE_SHIFT) 413b4ed71f5SMark Rutland BUG_ON(!pgtable_pte_page_ctor(phys_to_page(pa))); 41490292acaSYu Zhao else if (shift == PMD_SHIFT) 415475ba3fcSWill Deacon BUG_ON(!pgtable_pmd_page_ctor(phys_to_page(pa))); 41621ab99c2SMark Rutland 417475ba3fcSWill Deacon return pa; 418da141706SLaura Abbott } 419da141706SLaura Abbott 420132233a7SLaura Abbott /* 421132233a7SLaura Abbott * This function can only be used to modify existing table entries, 422132233a7SLaura Abbott * without allocating new levels of table. Note that this permits the 423132233a7SLaura Abbott * creation of new section or page entries. 424132233a7SLaura Abbott */ 425132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt, 426da141706SLaura Abbott phys_addr_t size, pgprot_t prot) 427d7ecbddfSMark Salter { 42877ad4ce6SMark Rutland if ((virt >= PAGE_END) && (virt < VMALLOC_START)) { 429d7ecbddfSMark Salter pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n", 430d7ecbddfSMark Salter &phys, virt); 431d7ecbddfSMark Salter return; 432d7ecbddfSMark Salter } 433d27cfa1fSArd Biesheuvel __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 434d27cfa1fSArd Biesheuvel NO_CONT_MAPPINGS); 435d7ecbddfSMark Salter } 436d7ecbddfSMark Salter 4378ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, 4388ce837ceSArd Biesheuvel unsigned long virt, phys_addr_t size, 439f14c66ceSArd Biesheuvel pgprot_t prot, bool page_mappings_only) 4408ce837ceSArd Biesheuvel { 441c0951366SArd Biesheuvel int flags = 0; 442c0951366SArd Biesheuvel 4431378dc3dSArd Biesheuvel BUG_ON(mm == &init_mm); 4441378dc3dSArd Biesheuvel 445c0951366SArd Biesheuvel if (page_mappings_only) 446d27cfa1fSArd Biesheuvel flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 447c0951366SArd Biesheuvel 44811509a30SMark Rutland __create_pgd_mapping(mm->pgd, phys, virt, size, prot, 449c0951366SArd Biesheuvel pgd_pgtable_alloc, flags); 450d7ecbddfSMark Salter } 451d7ecbddfSMark Salter 452aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt, 453da141706SLaura Abbott phys_addr_t size, pgprot_t prot) 454da141706SLaura Abbott { 45577ad4ce6SMark Rutland if ((virt >= PAGE_END) && (virt < VMALLOC_START)) { 456aa8c09beSArd Biesheuvel pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n", 457da141706SLaura Abbott &phys, virt); 458da141706SLaura Abbott return; 459da141706SLaura Abbott } 460da141706SLaura Abbott 461d27cfa1fSArd Biesheuvel __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 462d27cfa1fSArd Biesheuvel NO_CONT_MAPPINGS); 463aa8c09beSArd Biesheuvel 464aa8c09beSArd Biesheuvel /* flush the TLBs after updating live kernel mappings */ 465aa8c09beSArd Biesheuvel flush_tlb_kernel_range(virt, virt + size); 466da141706SLaura Abbott } 467da141706SLaura Abbott 46820a004e7SWill Deacon static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start, 46998d2e153STakahiro Akashi phys_addr_t end, pgprot_t prot, int flags) 470da141706SLaura Abbott { 47120a004e7SWill Deacon __create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start, 47298d2e153STakahiro Akashi prot, early_pgtable_alloc, flags); 473da141706SLaura Abbott } 474da141706SLaura Abbott 4755ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void) 4765ea5306cSArd Biesheuvel { 4775ea5306cSArd Biesheuvel /* 4785ea5306cSArd Biesheuvel * Remove the write permissions from the linear alias of .text/.rodata 4795ea5306cSArd Biesheuvel */ 480e2a073ddSArd Biesheuvel update_mapping_prot(__pa_symbol(_stext), (unsigned long)lm_alias(_stext), 481e2a073ddSArd Biesheuvel (unsigned long)__init_begin - (unsigned long)_stext, 4825ea5306cSArd Biesheuvel PAGE_KERNEL_RO); 4835ea5306cSArd Biesheuvel } 4845ea5306cSArd Biesheuvel 4852687275aSCatalin Marinas static bool crash_mem_map __initdata; 4862687275aSCatalin Marinas 4872687275aSCatalin Marinas static int __init enable_crash_mem_map(char *arg) 4882687275aSCatalin Marinas { 4892687275aSCatalin Marinas /* 4902687275aSCatalin Marinas * Proper parameter parsing is done by reserve_crashkernel(). We only 4912687275aSCatalin Marinas * need to know if the linear map has to avoid block mappings so that 4922687275aSCatalin Marinas * the crashkernel reservations can be unmapped later. 4932687275aSCatalin Marinas */ 4942687275aSCatalin Marinas crash_mem_map = true; 4952687275aSCatalin Marinas 4962687275aSCatalin Marinas return 0; 4972687275aSCatalin Marinas } 4982687275aSCatalin Marinas early_param("crashkernel", enable_crash_mem_map); 4992687275aSCatalin Marinas 50020a004e7SWill Deacon static void __init map_mem(pgd_t *pgdp) 501c1cc1552SCatalin Marinas { 50287143f40SArd Biesheuvel static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN); 503e2a073ddSArd Biesheuvel phys_addr_t kernel_start = __pa_symbol(_stext); 50498d2e153STakahiro Akashi phys_addr_t kernel_end = __pa_symbol(__init_begin); 505b10d6bcaSMike Rapoport phys_addr_t start, end; 50687143f40SArd Biesheuvel int flags = NO_EXEC_MAPPINGS; 507b10d6bcaSMike Rapoport u64 i; 50898d2e153STakahiro Akashi 50987143f40SArd Biesheuvel /* 51087143f40SArd Biesheuvel * Setting hierarchical PXNTable attributes on table entries covering 51187143f40SArd Biesheuvel * the linear region is only possible if it is guaranteed that no table 51287143f40SArd Biesheuvel * entries at any level are being shared between the linear region and 51387143f40SArd Biesheuvel * the vmalloc region. Check whether this is true for the PGD level, in 51487143f40SArd Biesheuvel * which case it is guaranteed to be true for all other levels as well. 51587143f40SArd Biesheuvel */ 51687143f40SArd Biesheuvel BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end)); 51787143f40SArd Biesheuvel 5182687275aSCatalin Marinas if (rodata_full || crash_mem_map || debug_pagealloc_enabled()) 51987143f40SArd Biesheuvel flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 52098d2e153STakahiro Akashi 52198d2e153STakahiro Akashi /* 52298d2e153STakahiro Akashi * Take care not to create a writable alias for the 52398d2e153STakahiro Akashi * read-only text and rodata sections of the kernel image. 52498d2e153STakahiro Akashi * So temporarily mark them as NOMAP to skip mappings in 52598d2e153STakahiro Akashi * the following for-loop 52698d2e153STakahiro Akashi */ 52798d2e153STakahiro Akashi memblock_mark_nomap(kernel_start, kernel_end - kernel_start); 528f6bc87c3SSteve Capper 529c1cc1552SCatalin Marinas /* map all the memory banks */ 530b10d6bcaSMike Rapoport for_each_mem_range(i, &start, &end) { 531c1cc1552SCatalin Marinas if (start >= end) 532c1cc1552SCatalin Marinas break; 5330178dc76SCatalin Marinas /* 5340178dc76SCatalin Marinas * The linear map must allow allocation tags reading/writing 5350178dc76SCatalin Marinas * if MTE is present. Otherwise, it has the same attributes as 5360178dc76SCatalin Marinas * PAGE_KERNEL. 5370178dc76SCatalin Marinas */ 538d15dfd31SCatalin Marinas __map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL), 539d15dfd31SCatalin Marinas flags); 540c1cc1552SCatalin Marinas } 54198d2e153STakahiro Akashi 54298d2e153STakahiro Akashi /* 543e2a073ddSArd Biesheuvel * Map the linear alias of the [_stext, __init_begin) interval 54498d2e153STakahiro Akashi * as non-executable now, and remove the write permission in 54598d2e153STakahiro Akashi * mark_linear_text_alias_ro() below (which will be called after 54698d2e153STakahiro Akashi * alternative patching has completed). This makes the contents 54798d2e153STakahiro Akashi * of the region accessible to subsystems such as hibernate, 54898d2e153STakahiro Akashi * but protects it from inadvertent modification or execution. 54998d2e153STakahiro Akashi * Note that contiguous mappings cannot be remapped in this way, 55098d2e153STakahiro Akashi * so we should avoid them here. 55198d2e153STakahiro Akashi */ 55220a004e7SWill Deacon __map_memblock(pgdp, kernel_start, kernel_end, 55398d2e153STakahiro Akashi PAGE_KERNEL, NO_CONT_MAPPINGS); 55498d2e153STakahiro Akashi memblock_clear_nomap(kernel_start, kernel_end - kernel_start); 555c1cc1552SCatalin Marinas } 556c1cc1552SCatalin Marinas 557da141706SLaura Abbott void mark_rodata_ro(void) 558da141706SLaura Abbott { 5592f39b5f9SJeremy Linton unsigned long section_size; 560f9040773SArd Biesheuvel 5612f39b5f9SJeremy Linton /* 5629fdc14c5SArd Biesheuvel * mark .rodata as read only. Use __init_begin rather than __end_rodata 5639fdc14c5SArd Biesheuvel * to cover NOTES and EXCEPTION_TABLE. 5642f39b5f9SJeremy Linton */ 5659fdc14c5SArd Biesheuvel section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata; 566aa8c09beSArd Biesheuvel update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata, 5672f39b5f9SJeremy Linton section_size, PAGE_KERNEL_RO); 568e98216b5SArd Biesheuvel 5691404d6f1SLaura Abbott debug_checkwx(); 570da141706SLaura Abbott } 571da141706SLaura Abbott 57220a004e7SWill Deacon static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end, 573d27cfa1fSArd Biesheuvel pgprot_t prot, struct vm_struct *vma, 57492bbd16eSWill Deacon int flags, unsigned long vm_flags) 575068a17a5SMark Rutland { 5762077be67SLaura Abbott phys_addr_t pa_start = __pa_symbol(va_start); 577068a17a5SMark Rutland unsigned long size = va_end - va_start; 578068a17a5SMark Rutland 579068a17a5SMark Rutland BUG_ON(!PAGE_ALIGNED(pa_start)); 580068a17a5SMark Rutland BUG_ON(!PAGE_ALIGNED(size)); 581068a17a5SMark Rutland 58220a004e7SWill Deacon __create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot, 583d27cfa1fSArd Biesheuvel early_pgtable_alloc, flags); 584f9040773SArd Biesheuvel 58592bbd16eSWill Deacon if (!(vm_flags & VM_NO_GUARD)) 58692bbd16eSWill Deacon size += PAGE_SIZE; 58792bbd16eSWill Deacon 588f9040773SArd Biesheuvel vma->addr = va_start; 589f9040773SArd Biesheuvel vma->phys_addr = pa_start; 590f9040773SArd Biesheuvel vma->size = size; 59192bbd16eSWill Deacon vma->flags = VM_MAP | vm_flags; 592f9040773SArd Biesheuvel vma->caller = __builtin_return_address(0); 593f9040773SArd Biesheuvel 594f9040773SArd Biesheuvel vm_area_add_early(vma); 595068a17a5SMark Rutland } 596068a17a5SMark Rutland 59728b066daSArd Biesheuvel static int __init parse_rodata(char *arg) 59828b066daSArd Biesheuvel { 599c55191e9SArd Biesheuvel int ret = strtobool(arg, &rodata_enabled); 600c55191e9SArd Biesheuvel if (!ret) { 601c55191e9SArd Biesheuvel rodata_full = false; 602c55191e9SArd Biesheuvel return 0; 603c55191e9SArd Biesheuvel } 604c55191e9SArd Biesheuvel 605c55191e9SArd Biesheuvel /* permit 'full' in addition to boolean options */ 606c55191e9SArd Biesheuvel if (strcmp(arg, "full")) 607c55191e9SArd Biesheuvel return -EINVAL; 608c55191e9SArd Biesheuvel 609c55191e9SArd Biesheuvel rodata_enabled = true; 610c55191e9SArd Biesheuvel rodata_full = true; 611c55191e9SArd Biesheuvel return 0; 61228b066daSArd Biesheuvel } 61328b066daSArd Biesheuvel early_param("rodata", parse_rodata); 61428b066daSArd Biesheuvel 61551a0048bSWill Deacon #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 61651a0048bSWill Deacon static int __init map_entry_trampoline(void) 61751a0048bSWill Deacon { 61851a0048bSWill Deacon pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; 61951a0048bSWill Deacon phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start); 62051a0048bSWill Deacon 62151a0048bSWill Deacon /* The trampoline is always mapped and can therefore be global */ 62251a0048bSWill Deacon pgprot_val(prot) &= ~PTE_NG; 62351a0048bSWill Deacon 62451a0048bSWill Deacon /* Map only the text into the trampoline page table */ 62551a0048bSWill Deacon memset(tramp_pg_dir, 0, PGD_SIZE); 62651a0048bSWill Deacon __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE, 627475ba3fcSWill Deacon prot, __pgd_pgtable_alloc, 0); 62851a0048bSWill Deacon 6296c27c408SWill Deacon /* Map both the text and data into the kernel page table */ 63051a0048bSWill Deacon __set_fixmap(FIX_ENTRY_TRAMP_TEXT, pa_start, prot); 6316c27c408SWill Deacon if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 6326c27c408SWill Deacon extern char __entry_tramp_data_start[]; 6336c27c408SWill Deacon 6346c27c408SWill Deacon __set_fixmap(FIX_ENTRY_TRAMP_DATA, 6356c27c408SWill Deacon __pa_symbol(__entry_tramp_data_start), 6366c27c408SWill Deacon PAGE_KERNEL_RO); 6376c27c408SWill Deacon } 6386c27c408SWill Deacon 63951a0048bSWill Deacon return 0; 64051a0048bSWill Deacon } 64151a0048bSWill Deacon core_initcall(map_entry_trampoline); 64251a0048bSWill Deacon #endif 64351a0048bSWill Deacon 644068a17a5SMark Rutland /* 645c8027285SMark Brown * Open coded check for BTI, only for use to determine configuration 646c8027285SMark Brown * for early mappings for before the cpufeature code has run. 647c8027285SMark Brown */ 648c8027285SMark Brown static bool arm64_early_this_cpu_has_bti(void) 649c8027285SMark Brown { 650c8027285SMark Brown u64 pfr1; 651c8027285SMark Brown 652c8027285SMark Brown if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)) 653c8027285SMark Brown return false; 654c8027285SMark Brown 65593ad55b7SMarc Zyngier pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1); 656c8027285SMark Brown return cpuid_feature_extract_unsigned_field(pfr1, 657c8027285SMark Brown ID_AA64PFR1_BT_SHIFT); 658c8027285SMark Brown } 659c8027285SMark Brown 660c8027285SMark Brown /* 661068a17a5SMark Rutland * Create fine-grained mappings for the kernel. 662068a17a5SMark Rutland */ 66320a004e7SWill Deacon static void __init map_kernel(pgd_t *pgdp) 664068a17a5SMark Rutland { 6652ebe088bSArd Biesheuvel static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext, 6662ebe088bSArd Biesheuvel vmlinux_initdata, vmlinux_data; 667068a17a5SMark Rutland 66828b066daSArd Biesheuvel /* 66928b066daSArd Biesheuvel * External debuggers may need to write directly to the text 67028b066daSArd Biesheuvel * mapping to install SW breakpoints. Allow this (only) when 67128b066daSArd Biesheuvel * explicitly requested with rodata=off. 67228b066daSArd Biesheuvel */ 67328b066daSArd Biesheuvel pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; 67428b066daSArd Biesheuvel 675d27cfa1fSArd Biesheuvel /* 676c8027285SMark Brown * If we have a CPU that supports BTI and a kernel built for 677c8027285SMark Brown * BTI then mark the kernel executable text as guarded pages 678c8027285SMark Brown * now so we don't have to rewrite the page tables later. 679c8027285SMark Brown */ 680c8027285SMark Brown if (arm64_early_this_cpu_has_bti()) 681c8027285SMark Brown text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP); 682c8027285SMark Brown 683c8027285SMark Brown /* 684d27cfa1fSArd Biesheuvel * Only rodata will be remapped with different permissions later on, 685d27cfa1fSArd Biesheuvel * all other segments are allowed to use contiguous mappings. 686d27cfa1fSArd Biesheuvel */ 687e2a073ddSArd Biesheuvel map_kernel_segment(pgdp, _stext, _etext, text_prot, &vmlinux_text, 0, 68892bbd16eSWill Deacon VM_NO_GUARD); 68920a004e7SWill Deacon map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL, 69092bbd16eSWill Deacon &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD); 69120a004e7SWill Deacon map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot, 69292bbd16eSWill Deacon &vmlinux_inittext, 0, VM_NO_GUARD); 69320a004e7SWill Deacon map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL, 69492bbd16eSWill Deacon &vmlinux_initdata, 0, VM_NO_GUARD); 69520a004e7SWill Deacon map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0); 696068a17a5SMark Rutland 697974b9b2cSMike Rapoport if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) { 698068a17a5SMark Rutland /* 699f9040773SArd Biesheuvel * The fixmap falls in a separate pgd to the kernel, and doesn't 700f9040773SArd Biesheuvel * live in the carveout for the swapper_pg_dir. We can simply 701f9040773SArd Biesheuvel * re-use the existing dir for the fixmap. 702068a17a5SMark Rutland */ 703974b9b2cSMike Rapoport set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START), 70420a004e7SWill Deacon READ_ONCE(*pgd_offset_k(FIXADDR_START))); 705f9040773SArd Biesheuvel } else if (CONFIG_PGTABLE_LEVELS > 3) { 706b333b0baSMark Rutland pgd_t *bm_pgdp; 707e9f63768SMike Rapoport p4d_t *bm_p4dp; 708b333b0baSMark Rutland pud_t *bm_pudp; 709f9040773SArd Biesheuvel /* 710f9040773SArd Biesheuvel * The fixmap shares its top level pgd entry with the kernel 711f9040773SArd Biesheuvel * mapping. This can really only occur when we are running 712f9040773SArd Biesheuvel * with 16k/4 levels, so we can simply reuse the pud level 713f9040773SArd Biesheuvel * entry instead. 714f9040773SArd Biesheuvel */ 715f9040773SArd Biesheuvel BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES)); 716974b9b2cSMike Rapoport bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START); 717e9f63768SMike Rapoport bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START); 718e9f63768SMike Rapoport bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START); 719b333b0baSMark Rutland pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd)); 720f9040773SArd Biesheuvel pud_clear_fixmap(); 721f9040773SArd Biesheuvel } else { 722f9040773SArd Biesheuvel BUG(); 723f9040773SArd Biesheuvel } 724068a17a5SMark Rutland 72520a004e7SWill Deacon kasan_copy_shadow(pgdp); 726068a17a5SMark Rutland } 727068a17a5SMark Rutland 728c1cc1552SCatalin Marinas void __init paging_init(void) 729c1cc1552SCatalin Marinas { 7302330b7caSJun Yao pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir)); 731068a17a5SMark Rutland 73220a004e7SWill Deacon map_kernel(pgdp); 73320a004e7SWill Deacon map_mem(pgdp); 734068a17a5SMark Rutland 735068a17a5SMark Rutland pgd_clear_fixmap(); 736068a17a5SMark Rutland 737068a17a5SMark Rutland cpu_replace_ttbr1(lm_alias(swapper_pg_dir)); 7382b5548b6SJun Yao init_mm.pgd = swapper_pg_dir; 739068a17a5SMark Rutland 7402b5548b6SJun Yao memblock_free(__pa_symbol(init_pg_dir), 7412b5548b6SJun Yao __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir)); 74224cc61d8SArd Biesheuvel 74324cc61d8SArd Biesheuvel memblock_allow_resize(); 744c1cc1552SCatalin Marinas } 745c1cc1552SCatalin Marinas 746c1cc1552SCatalin Marinas /* 747c1cc1552SCatalin Marinas * Check whether a kernel address is valid (derived from arch/x86/). 748c1cc1552SCatalin Marinas */ 749c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr) 750c1cc1552SCatalin Marinas { 75120a004e7SWill Deacon pgd_t *pgdp; 752e9f63768SMike Rapoport p4d_t *p4dp; 75320a004e7SWill Deacon pud_t *pudp, pud; 75420a004e7SWill Deacon pmd_t *pmdp, pmd; 75520a004e7SWill Deacon pte_t *ptep, pte; 756c1cc1552SCatalin Marinas 7578dd4daa0SShyam Thombre addr = arch_kasan_reset_tag(addr); 758c1cc1552SCatalin Marinas if ((((long)addr) >> VA_BITS) != -1UL) 759c1cc1552SCatalin Marinas return 0; 760c1cc1552SCatalin Marinas 76120a004e7SWill Deacon pgdp = pgd_offset_k(addr); 76220a004e7SWill Deacon if (pgd_none(READ_ONCE(*pgdp))) 763c1cc1552SCatalin Marinas return 0; 764c1cc1552SCatalin Marinas 765e9f63768SMike Rapoport p4dp = p4d_offset(pgdp, addr); 766e9f63768SMike Rapoport if (p4d_none(READ_ONCE(*p4dp))) 767e9f63768SMike Rapoport return 0; 768e9f63768SMike Rapoport 769e9f63768SMike Rapoport pudp = pud_offset(p4dp, addr); 77020a004e7SWill Deacon pud = READ_ONCE(*pudp); 77120a004e7SWill Deacon if (pud_none(pud)) 772c1cc1552SCatalin Marinas return 0; 773c1cc1552SCatalin Marinas 77420a004e7SWill Deacon if (pud_sect(pud)) 77520a004e7SWill Deacon return pfn_valid(pud_pfn(pud)); 776206a2a73SSteve Capper 77720a004e7SWill Deacon pmdp = pmd_offset(pudp, addr); 77820a004e7SWill Deacon pmd = READ_ONCE(*pmdp); 77920a004e7SWill Deacon if (pmd_none(pmd)) 780c1cc1552SCatalin Marinas return 0; 781c1cc1552SCatalin Marinas 78220a004e7SWill Deacon if (pmd_sect(pmd)) 78320a004e7SWill Deacon return pfn_valid(pmd_pfn(pmd)); 784da6e4cb6SDave Anderson 78520a004e7SWill Deacon ptep = pte_offset_kernel(pmdp, addr); 78620a004e7SWill Deacon pte = READ_ONCE(*ptep); 78720a004e7SWill Deacon if (pte_none(pte)) 788c1cc1552SCatalin Marinas return 0; 789c1cc1552SCatalin Marinas 79020a004e7SWill Deacon return pfn_valid(pte_pfn(pte)); 791c1cc1552SCatalin Marinas } 792bbd6ec60SAnshuman Khandual 793bbd6ec60SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG 794eee07935SAnshuman Khandual static void free_hotplug_page_range(struct page *page, size_t size, 795eee07935SAnshuman Khandual struct vmem_altmap *altmap) 796bbd6ec60SAnshuman Khandual { 797eee07935SAnshuman Khandual if (altmap) { 798eee07935SAnshuman Khandual vmem_altmap_free(altmap, size >> PAGE_SHIFT); 799eee07935SAnshuman Khandual } else { 800bbd6ec60SAnshuman Khandual WARN_ON(PageReserved(page)); 801bbd6ec60SAnshuman Khandual free_pages((unsigned long)page_address(page), get_order(size)); 802bbd6ec60SAnshuman Khandual } 803eee07935SAnshuman Khandual } 804bbd6ec60SAnshuman Khandual 805bbd6ec60SAnshuman Khandual static void free_hotplug_pgtable_page(struct page *page) 806bbd6ec60SAnshuman Khandual { 807eee07935SAnshuman Khandual free_hotplug_page_range(page, PAGE_SIZE, NULL); 808bbd6ec60SAnshuman Khandual } 809bbd6ec60SAnshuman Khandual 810bbd6ec60SAnshuman Khandual static bool pgtable_range_aligned(unsigned long start, unsigned long end, 811bbd6ec60SAnshuman Khandual unsigned long floor, unsigned long ceiling, 812bbd6ec60SAnshuman Khandual unsigned long mask) 813bbd6ec60SAnshuman Khandual { 814bbd6ec60SAnshuman Khandual start &= mask; 815bbd6ec60SAnshuman Khandual if (start < floor) 816bbd6ec60SAnshuman Khandual return false; 817bbd6ec60SAnshuman Khandual 818bbd6ec60SAnshuman Khandual if (ceiling) { 819bbd6ec60SAnshuman Khandual ceiling &= mask; 820bbd6ec60SAnshuman Khandual if (!ceiling) 821bbd6ec60SAnshuman Khandual return false; 822bbd6ec60SAnshuman Khandual } 823bbd6ec60SAnshuman Khandual 824bbd6ec60SAnshuman Khandual if (end - 1 > ceiling - 1) 825bbd6ec60SAnshuman Khandual return false; 826bbd6ec60SAnshuman Khandual return true; 827bbd6ec60SAnshuman Khandual } 828bbd6ec60SAnshuman Khandual 829bbd6ec60SAnshuman Khandual static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, 830eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 831eee07935SAnshuman Khandual struct vmem_altmap *altmap) 832bbd6ec60SAnshuman Khandual { 833bbd6ec60SAnshuman Khandual pte_t *ptep, pte; 834bbd6ec60SAnshuman Khandual 835bbd6ec60SAnshuman Khandual do { 836bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, addr); 837bbd6ec60SAnshuman Khandual pte = READ_ONCE(*ptep); 838bbd6ec60SAnshuman Khandual if (pte_none(pte)) 839bbd6ec60SAnshuman Khandual continue; 840bbd6ec60SAnshuman Khandual 841bbd6ec60SAnshuman Khandual WARN_ON(!pte_present(pte)); 842bbd6ec60SAnshuman Khandual pte_clear(&init_mm, addr, ptep); 843bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 844bbd6ec60SAnshuman Khandual if (free_mapped) 845eee07935SAnshuman Khandual free_hotplug_page_range(pte_page(pte), 846eee07935SAnshuman Khandual PAGE_SIZE, altmap); 847bbd6ec60SAnshuman Khandual } while (addr += PAGE_SIZE, addr < end); 848bbd6ec60SAnshuman Khandual } 849bbd6ec60SAnshuman Khandual 850bbd6ec60SAnshuman Khandual static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr, 851eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 852eee07935SAnshuman Khandual struct vmem_altmap *altmap) 853bbd6ec60SAnshuman Khandual { 854bbd6ec60SAnshuman Khandual unsigned long next; 855bbd6ec60SAnshuman Khandual pmd_t *pmdp, pmd; 856bbd6ec60SAnshuman Khandual 857bbd6ec60SAnshuman Khandual do { 858bbd6ec60SAnshuman Khandual next = pmd_addr_end(addr, end); 859bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, addr); 860bbd6ec60SAnshuman Khandual pmd = READ_ONCE(*pmdp); 861bbd6ec60SAnshuman Khandual if (pmd_none(pmd)) 862bbd6ec60SAnshuman Khandual continue; 863bbd6ec60SAnshuman Khandual 864bbd6ec60SAnshuman Khandual WARN_ON(!pmd_present(pmd)); 865bbd6ec60SAnshuman Khandual if (pmd_sect(pmd)) { 866bbd6ec60SAnshuman Khandual pmd_clear(pmdp); 867bbd6ec60SAnshuman Khandual 868bbd6ec60SAnshuman Khandual /* 869bbd6ec60SAnshuman Khandual * One TLBI should be sufficient here as the PMD_SIZE 870bbd6ec60SAnshuman Khandual * range is mapped with a single block entry. 871bbd6ec60SAnshuman Khandual */ 872bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 873bbd6ec60SAnshuman Khandual if (free_mapped) 874bbd6ec60SAnshuman Khandual free_hotplug_page_range(pmd_page(pmd), 875eee07935SAnshuman Khandual PMD_SIZE, altmap); 876bbd6ec60SAnshuman Khandual continue; 877bbd6ec60SAnshuman Khandual } 878bbd6ec60SAnshuman Khandual WARN_ON(!pmd_table(pmd)); 879eee07935SAnshuman Khandual unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap); 880bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 881bbd6ec60SAnshuman Khandual } 882bbd6ec60SAnshuman Khandual 883bbd6ec60SAnshuman Khandual static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr, 884eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 885eee07935SAnshuman Khandual struct vmem_altmap *altmap) 886bbd6ec60SAnshuman Khandual { 887bbd6ec60SAnshuman Khandual unsigned long next; 888bbd6ec60SAnshuman Khandual pud_t *pudp, pud; 889bbd6ec60SAnshuman Khandual 890bbd6ec60SAnshuman Khandual do { 891bbd6ec60SAnshuman Khandual next = pud_addr_end(addr, end); 892bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, addr); 893bbd6ec60SAnshuman Khandual pud = READ_ONCE(*pudp); 894bbd6ec60SAnshuman Khandual if (pud_none(pud)) 895bbd6ec60SAnshuman Khandual continue; 896bbd6ec60SAnshuman Khandual 897bbd6ec60SAnshuman Khandual WARN_ON(!pud_present(pud)); 898bbd6ec60SAnshuman Khandual if (pud_sect(pud)) { 899bbd6ec60SAnshuman Khandual pud_clear(pudp); 900bbd6ec60SAnshuman Khandual 901bbd6ec60SAnshuman Khandual /* 902bbd6ec60SAnshuman Khandual * One TLBI should be sufficient here as the PUD_SIZE 903bbd6ec60SAnshuman Khandual * range is mapped with a single block entry. 904bbd6ec60SAnshuman Khandual */ 905bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 906bbd6ec60SAnshuman Khandual if (free_mapped) 907bbd6ec60SAnshuman Khandual free_hotplug_page_range(pud_page(pud), 908eee07935SAnshuman Khandual PUD_SIZE, altmap); 909bbd6ec60SAnshuman Khandual continue; 910bbd6ec60SAnshuman Khandual } 911bbd6ec60SAnshuman Khandual WARN_ON(!pud_table(pud)); 912eee07935SAnshuman Khandual unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap); 913bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 914bbd6ec60SAnshuman Khandual } 915bbd6ec60SAnshuman Khandual 916bbd6ec60SAnshuman Khandual static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr, 917eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 918eee07935SAnshuman Khandual struct vmem_altmap *altmap) 919bbd6ec60SAnshuman Khandual { 920bbd6ec60SAnshuman Khandual unsigned long next; 921bbd6ec60SAnshuman Khandual p4d_t *p4dp, p4d; 922bbd6ec60SAnshuman Khandual 923bbd6ec60SAnshuman Khandual do { 924bbd6ec60SAnshuman Khandual next = p4d_addr_end(addr, end); 925bbd6ec60SAnshuman Khandual p4dp = p4d_offset(pgdp, addr); 926bbd6ec60SAnshuman Khandual p4d = READ_ONCE(*p4dp); 927bbd6ec60SAnshuman Khandual if (p4d_none(p4d)) 928bbd6ec60SAnshuman Khandual continue; 929bbd6ec60SAnshuman Khandual 930bbd6ec60SAnshuman Khandual WARN_ON(!p4d_present(p4d)); 931eee07935SAnshuman Khandual unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap); 932bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 933bbd6ec60SAnshuman Khandual } 934bbd6ec60SAnshuman Khandual 935bbd6ec60SAnshuman Khandual static void unmap_hotplug_range(unsigned long addr, unsigned long end, 936eee07935SAnshuman Khandual bool free_mapped, struct vmem_altmap *altmap) 937bbd6ec60SAnshuman Khandual { 938bbd6ec60SAnshuman Khandual unsigned long next; 939bbd6ec60SAnshuman Khandual pgd_t *pgdp, pgd; 940bbd6ec60SAnshuman Khandual 941eee07935SAnshuman Khandual /* 942eee07935SAnshuman Khandual * altmap can only be used as vmemmap mapping backing memory. 943eee07935SAnshuman Khandual * In case the backing memory itself is not being freed, then 944eee07935SAnshuman Khandual * altmap is irrelevant. Warn about this inconsistency when 945eee07935SAnshuman Khandual * encountered. 946eee07935SAnshuman Khandual */ 947eee07935SAnshuman Khandual WARN_ON(!free_mapped && altmap); 948eee07935SAnshuman Khandual 949bbd6ec60SAnshuman Khandual do { 950bbd6ec60SAnshuman Khandual next = pgd_addr_end(addr, end); 951bbd6ec60SAnshuman Khandual pgdp = pgd_offset_k(addr); 952bbd6ec60SAnshuman Khandual pgd = READ_ONCE(*pgdp); 953bbd6ec60SAnshuman Khandual if (pgd_none(pgd)) 954bbd6ec60SAnshuman Khandual continue; 955bbd6ec60SAnshuman Khandual 956bbd6ec60SAnshuman Khandual WARN_ON(!pgd_present(pgd)); 957eee07935SAnshuman Khandual unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap); 958bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 959bbd6ec60SAnshuman Khandual } 960bbd6ec60SAnshuman Khandual 961bbd6ec60SAnshuman Khandual static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr, 962bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 963bbd6ec60SAnshuman Khandual unsigned long ceiling) 964bbd6ec60SAnshuman Khandual { 965bbd6ec60SAnshuman Khandual pte_t *ptep, pte; 966bbd6ec60SAnshuman Khandual unsigned long i, start = addr; 967bbd6ec60SAnshuman Khandual 968bbd6ec60SAnshuman Khandual do { 969bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, addr); 970bbd6ec60SAnshuman Khandual pte = READ_ONCE(*ptep); 971bbd6ec60SAnshuman Khandual 972bbd6ec60SAnshuman Khandual /* 973bbd6ec60SAnshuman Khandual * This is just a sanity check here which verifies that 974bbd6ec60SAnshuman Khandual * pte clearing has been done by earlier unmap loops. 975bbd6ec60SAnshuman Khandual */ 976bbd6ec60SAnshuman Khandual WARN_ON(!pte_none(pte)); 977bbd6ec60SAnshuman Khandual } while (addr += PAGE_SIZE, addr < end); 978bbd6ec60SAnshuman Khandual 979bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK)) 980bbd6ec60SAnshuman Khandual return; 981bbd6ec60SAnshuman Khandual 982bbd6ec60SAnshuman Khandual /* 983bbd6ec60SAnshuman Khandual * Check whether we can free the pte page if the rest of the 984bbd6ec60SAnshuman Khandual * entries are empty. Overlap with other regions have been 985bbd6ec60SAnshuman Khandual * handled by the floor/ceiling check. 986bbd6ec60SAnshuman Khandual */ 987bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, 0UL); 988bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PTE; i++) { 989bbd6ec60SAnshuman Khandual if (!pte_none(READ_ONCE(ptep[i]))) 990bbd6ec60SAnshuman Khandual return; 991bbd6ec60SAnshuman Khandual } 992bbd6ec60SAnshuman Khandual 993bbd6ec60SAnshuman Khandual pmd_clear(pmdp); 994bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 995bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(ptep)); 996bbd6ec60SAnshuman Khandual } 997bbd6ec60SAnshuman Khandual 998bbd6ec60SAnshuman Khandual static void free_empty_pmd_table(pud_t *pudp, unsigned long addr, 999bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1000bbd6ec60SAnshuman Khandual unsigned long ceiling) 1001bbd6ec60SAnshuman Khandual { 1002bbd6ec60SAnshuman Khandual pmd_t *pmdp, pmd; 1003bbd6ec60SAnshuman Khandual unsigned long i, next, start = addr; 1004bbd6ec60SAnshuman Khandual 1005bbd6ec60SAnshuman Khandual do { 1006bbd6ec60SAnshuman Khandual next = pmd_addr_end(addr, end); 1007bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, addr); 1008bbd6ec60SAnshuman Khandual pmd = READ_ONCE(*pmdp); 1009bbd6ec60SAnshuman Khandual if (pmd_none(pmd)) 1010bbd6ec60SAnshuman Khandual continue; 1011bbd6ec60SAnshuman Khandual 1012bbd6ec60SAnshuman Khandual WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd)); 1013bbd6ec60SAnshuman Khandual free_empty_pte_table(pmdp, addr, next, floor, ceiling); 1014bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1015bbd6ec60SAnshuman Khandual 1016bbd6ec60SAnshuman Khandual if (CONFIG_PGTABLE_LEVELS <= 2) 1017bbd6ec60SAnshuman Khandual return; 1018bbd6ec60SAnshuman Khandual 1019bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK)) 1020bbd6ec60SAnshuman Khandual return; 1021bbd6ec60SAnshuman Khandual 1022bbd6ec60SAnshuman Khandual /* 1023bbd6ec60SAnshuman Khandual * Check whether we can free the pmd page if the rest of the 1024bbd6ec60SAnshuman Khandual * entries are empty. Overlap with other regions have been 1025bbd6ec60SAnshuman Khandual * handled by the floor/ceiling check. 1026bbd6ec60SAnshuman Khandual */ 1027bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, 0UL); 1028bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PMD; i++) { 1029bbd6ec60SAnshuman Khandual if (!pmd_none(READ_ONCE(pmdp[i]))) 1030bbd6ec60SAnshuman Khandual return; 1031bbd6ec60SAnshuman Khandual } 1032bbd6ec60SAnshuman Khandual 1033bbd6ec60SAnshuman Khandual pud_clear(pudp); 1034bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1035bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(pmdp)); 1036bbd6ec60SAnshuman Khandual } 1037bbd6ec60SAnshuman Khandual 1038bbd6ec60SAnshuman Khandual static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr, 1039bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1040bbd6ec60SAnshuman Khandual unsigned long ceiling) 1041bbd6ec60SAnshuman Khandual { 1042bbd6ec60SAnshuman Khandual pud_t *pudp, pud; 1043bbd6ec60SAnshuman Khandual unsigned long i, next, start = addr; 1044bbd6ec60SAnshuman Khandual 1045bbd6ec60SAnshuman Khandual do { 1046bbd6ec60SAnshuman Khandual next = pud_addr_end(addr, end); 1047bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, addr); 1048bbd6ec60SAnshuman Khandual pud = READ_ONCE(*pudp); 1049bbd6ec60SAnshuman Khandual if (pud_none(pud)) 1050bbd6ec60SAnshuman Khandual continue; 1051bbd6ec60SAnshuman Khandual 1052bbd6ec60SAnshuman Khandual WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud)); 1053bbd6ec60SAnshuman Khandual free_empty_pmd_table(pudp, addr, next, floor, ceiling); 1054bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1055bbd6ec60SAnshuman Khandual 1056bbd6ec60SAnshuman Khandual if (CONFIG_PGTABLE_LEVELS <= 3) 1057bbd6ec60SAnshuman Khandual return; 1058bbd6ec60SAnshuman Khandual 1059bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK)) 1060bbd6ec60SAnshuman Khandual return; 1061bbd6ec60SAnshuman Khandual 1062bbd6ec60SAnshuman Khandual /* 1063bbd6ec60SAnshuman Khandual * Check whether we can free the pud page if the rest of the 1064bbd6ec60SAnshuman Khandual * entries are empty. Overlap with other regions have been 1065bbd6ec60SAnshuman Khandual * handled by the floor/ceiling check. 1066bbd6ec60SAnshuman Khandual */ 1067bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, 0UL); 1068bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PUD; i++) { 1069bbd6ec60SAnshuman Khandual if (!pud_none(READ_ONCE(pudp[i]))) 1070bbd6ec60SAnshuman Khandual return; 1071bbd6ec60SAnshuman Khandual } 1072bbd6ec60SAnshuman Khandual 1073bbd6ec60SAnshuman Khandual p4d_clear(p4dp); 1074bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1075bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(pudp)); 1076bbd6ec60SAnshuman Khandual } 1077bbd6ec60SAnshuman Khandual 1078bbd6ec60SAnshuman Khandual static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr, 1079bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1080bbd6ec60SAnshuman Khandual unsigned long ceiling) 1081bbd6ec60SAnshuman Khandual { 1082bbd6ec60SAnshuman Khandual unsigned long next; 1083bbd6ec60SAnshuman Khandual p4d_t *p4dp, p4d; 1084bbd6ec60SAnshuman Khandual 1085bbd6ec60SAnshuman Khandual do { 1086bbd6ec60SAnshuman Khandual next = p4d_addr_end(addr, end); 1087bbd6ec60SAnshuman Khandual p4dp = p4d_offset(pgdp, addr); 1088bbd6ec60SAnshuman Khandual p4d = READ_ONCE(*p4dp); 1089bbd6ec60SAnshuman Khandual if (p4d_none(p4d)) 1090bbd6ec60SAnshuman Khandual continue; 1091bbd6ec60SAnshuman Khandual 1092bbd6ec60SAnshuman Khandual WARN_ON(!p4d_present(p4d)); 1093bbd6ec60SAnshuman Khandual free_empty_pud_table(p4dp, addr, next, floor, ceiling); 1094bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1095bbd6ec60SAnshuman Khandual } 1096bbd6ec60SAnshuman Khandual 1097bbd6ec60SAnshuman Khandual static void free_empty_tables(unsigned long addr, unsigned long end, 1098bbd6ec60SAnshuman Khandual unsigned long floor, unsigned long ceiling) 1099bbd6ec60SAnshuman Khandual { 1100bbd6ec60SAnshuman Khandual unsigned long next; 1101bbd6ec60SAnshuman Khandual pgd_t *pgdp, pgd; 1102bbd6ec60SAnshuman Khandual 1103bbd6ec60SAnshuman Khandual do { 1104bbd6ec60SAnshuman Khandual next = pgd_addr_end(addr, end); 1105bbd6ec60SAnshuman Khandual pgdp = pgd_offset_k(addr); 1106bbd6ec60SAnshuman Khandual pgd = READ_ONCE(*pgdp); 1107bbd6ec60SAnshuman Khandual if (pgd_none(pgd)) 1108bbd6ec60SAnshuman Khandual continue; 1109bbd6ec60SAnshuman Khandual 1110bbd6ec60SAnshuman Khandual WARN_ON(!pgd_present(pgd)); 1111bbd6ec60SAnshuman Khandual free_empty_p4d_table(pgdp, addr, next, floor, ceiling); 1112bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1113bbd6ec60SAnshuman Khandual } 1114bbd6ec60SAnshuman Khandual #endif 1115bbd6ec60SAnshuman Khandual 1116b433dce0SSuzuki K. Poulose #if !ARM64_SWAPPER_USES_SECTION_MAPS 11177b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 11187b73d978SChristoph Hellwig struct vmem_altmap *altmap) 1119c1cc1552SCatalin Marinas { 1120edb739eeSAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1121eee07935SAnshuman Khandual return vmemmap_populate_basepages(start, end, node, altmap); 1122c1cc1552SCatalin Marinas } 1123b433dce0SSuzuki K. Poulose #else /* !ARM64_SWAPPER_USES_SECTION_MAPS */ 11247b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 11257b73d978SChristoph Hellwig struct vmem_altmap *altmap) 1126c1cc1552SCatalin Marinas { 11270aad818bSJohannes Weiner unsigned long addr = start; 1128c1cc1552SCatalin Marinas unsigned long next; 112920a004e7SWill Deacon pgd_t *pgdp; 1130e9f63768SMike Rapoport p4d_t *p4dp; 113120a004e7SWill Deacon pud_t *pudp; 113220a004e7SWill Deacon pmd_t *pmdp; 1133c1cc1552SCatalin Marinas 1134edb739eeSAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1135c1cc1552SCatalin Marinas do { 1136c1cc1552SCatalin Marinas next = pmd_addr_end(addr, end); 1137c1cc1552SCatalin Marinas 113820a004e7SWill Deacon pgdp = vmemmap_pgd_populate(addr, node); 113920a004e7SWill Deacon if (!pgdp) 1140c1cc1552SCatalin Marinas return -ENOMEM; 1141c1cc1552SCatalin Marinas 1142e9f63768SMike Rapoport p4dp = vmemmap_p4d_populate(pgdp, addr, node); 1143e9f63768SMike Rapoport if (!p4dp) 1144e9f63768SMike Rapoport return -ENOMEM; 1145e9f63768SMike Rapoport 1146e9f63768SMike Rapoport pudp = vmemmap_pud_populate(p4dp, addr, node); 114720a004e7SWill Deacon if (!pudp) 1148c1cc1552SCatalin Marinas return -ENOMEM; 1149c1cc1552SCatalin Marinas 115020a004e7SWill Deacon pmdp = pmd_offset(pudp, addr); 115120a004e7SWill Deacon if (pmd_none(READ_ONCE(*pmdp))) { 1152c1cc1552SCatalin Marinas void *p = NULL; 1153c1cc1552SCatalin Marinas 1154eee07935SAnshuman Khandual p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap); 11559f84f39fSSudarshan Rajagopalan if (!p) { 11569f84f39fSSudarshan Rajagopalan if (vmemmap_populate_basepages(addr, next, node, altmap)) 1157c1cc1552SCatalin Marinas return -ENOMEM; 11589f84f39fSSudarshan Rajagopalan continue; 11599f84f39fSSudarshan Rajagopalan } 1160c1cc1552SCatalin Marinas 116120a004e7SWill Deacon pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL)); 1162c1cc1552SCatalin Marinas } else 116320a004e7SWill Deacon vmemmap_verify((pte_t *)pmdp, node, addr, next); 1164c1cc1552SCatalin Marinas } while (addr = next, addr != end); 1165c1cc1552SCatalin Marinas 1166c1cc1552SCatalin Marinas return 0; 1167c1cc1552SCatalin Marinas } 11688e01076aSOdin Ugedal #endif /* !ARM64_SWAPPER_USES_SECTION_MAPS */ 1169*40221c73SAnshuman Khandual 1170*40221c73SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG 117124b6d416SChristoph Hellwig void vmemmap_free(unsigned long start, unsigned long end, 117224b6d416SChristoph Hellwig struct vmem_altmap *altmap) 11730197518cSTang Chen { 1174bbd6ec60SAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1175bbd6ec60SAnshuman Khandual 1176eee07935SAnshuman Khandual unmap_hotplug_range(start, end, true, altmap); 1177bbd6ec60SAnshuman Khandual free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END); 11780197518cSTang Chen } 1179*40221c73SAnshuman Khandual #endif /* CONFIG_MEMORY_HOTPLUG */ 1180af86e597SLaura Abbott 1181af86e597SLaura Abbott static inline pud_t *fixmap_pud(unsigned long addr) 1182af86e597SLaura Abbott { 118320a004e7SWill Deacon pgd_t *pgdp = pgd_offset_k(addr); 1184e9f63768SMike Rapoport p4d_t *p4dp = p4d_offset(pgdp, addr); 1185e9f63768SMike Rapoport p4d_t p4d = READ_ONCE(*p4dp); 1186af86e597SLaura Abbott 1187e9f63768SMike Rapoport BUG_ON(p4d_none(p4d) || p4d_bad(p4d)); 1188af86e597SLaura Abbott 1189e9f63768SMike Rapoport return pud_offset_kimg(p4dp, addr); 1190af86e597SLaura Abbott } 1191af86e597SLaura Abbott 1192af86e597SLaura Abbott static inline pmd_t *fixmap_pmd(unsigned long addr) 1193af86e597SLaura Abbott { 119420a004e7SWill Deacon pud_t *pudp = fixmap_pud(addr); 119520a004e7SWill Deacon pud_t pud = READ_ONCE(*pudp); 1196af86e597SLaura Abbott 119720a004e7SWill Deacon BUG_ON(pud_none(pud) || pud_bad(pud)); 1198af86e597SLaura Abbott 119920a004e7SWill Deacon return pmd_offset_kimg(pudp, addr); 1200af86e597SLaura Abbott } 1201af86e597SLaura Abbott 1202af86e597SLaura Abbott static inline pte_t *fixmap_pte(unsigned long addr) 1203af86e597SLaura Abbott { 1204157962f5SArd Biesheuvel return &bm_pte[pte_index(addr)]; 1205af86e597SLaura Abbott } 1206af86e597SLaura Abbott 12072077be67SLaura Abbott /* 12082077be67SLaura Abbott * The p*d_populate functions call virt_to_phys implicitly so they can't be used 12092077be67SLaura Abbott * directly on kernel symbols (bm_p*d). This function is called too early to use 12102077be67SLaura Abbott * lm_alias so __p*d_populate functions must be used to populate with the 12112077be67SLaura Abbott * physical address from __pa_symbol. 12122077be67SLaura Abbott */ 1213af86e597SLaura Abbott void __init early_fixmap_init(void) 1214af86e597SLaura Abbott { 1215e9f63768SMike Rapoport pgd_t *pgdp; 1216e9f63768SMike Rapoport p4d_t *p4dp, p4d; 121720a004e7SWill Deacon pud_t *pudp; 121820a004e7SWill Deacon pmd_t *pmdp; 1219af86e597SLaura Abbott unsigned long addr = FIXADDR_START; 1220af86e597SLaura Abbott 122120a004e7SWill Deacon pgdp = pgd_offset_k(addr); 1222e9f63768SMike Rapoport p4dp = p4d_offset(pgdp, addr); 1223e9f63768SMike Rapoport p4d = READ_ONCE(*p4dp); 1224f80fb3a3SArd Biesheuvel if (CONFIG_PGTABLE_LEVELS > 3 && 1225e9f63768SMike Rapoport !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) { 1226f9040773SArd Biesheuvel /* 1227f9040773SArd Biesheuvel * We only end up here if the kernel mapping and the fixmap 1228f9040773SArd Biesheuvel * share the top level pgd entry, which should only happen on 1229f9040773SArd Biesheuvel * 16k/4 levels configurations. 1230f9040773SArd Biesheuvel */ 1231f9040773SArd Biesheuvel BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES)); 1232e9f63768SMike Rapoport pudp = pud_offset_kimg(p4dp, addr); 1233f9040773SArd Biesheuvel } else { 1234e9f63768SMike Rapoport if (p4d_none(p4d)) 1235c1fd78a7SArd Biesheuvel __p4d_populate(p4dp, __pa_symbol(bm_pud), P4D_TYPE_TABLE); 123620a004e7SWill Deacon pudp = fixmap_pud(addr); 1237f9040773SArd Biesheuvel } 123820a004e7SWill Deacon if (pud_none(READ_ONCE(*pudp))) 1239c1fd78a7SArd Biesheuvel __pud_populate(pudp, __pa_symbol(bm_pmd), PUD_TYPE_TABLE); 124020a004e7SWill Deacon pmdp = fixmap_pmd(addr); 124120a004e7SWill Deacon __pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE); 1242af86e597SLaura Abbott 1243af86e597SLaura Abbott /* 1244af86e597SLaura Abbott * The boot-ioremap range spans multiple pmds, for which 1245157962f5SArd Biesheuvel * we are not prepared: 1246af86e597SLaura Abbott */ 1247af86e597SLaura Abbott BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) 1248af86e597SLaura Abbott != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); 1249af86e597SLaura Abbott 125020a004e7SWill Deacon if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN))) 125120a004e7SWill Deacon || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) { 1252af86e597SLaura Abbott WARN_ON(1); 125320a004e7SWill Deacon pr_warn("pmdp %p != %p, %p\n", 125420a004e7SWill Deacon pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)), 1255af86e597SLaura Abbott fixmap_pmd(fix_to_virt(FIX_BTMAP_END))); 1256af86e597SLaura Abbott pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", 1257af86e597SLaura Abbott fix_to_virt(FIX_BTMAP_BEGIN)); 1258af86e597SLaura Abbott pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n", 1259af86e597SLaura Abbott fix_to_virt(FIX_BTMAP_END)); 1260af86e597SLaura Abbott 1261af86e597SLaura Abbott pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END); 1262af86e597SLaura Abbott pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN); 1263af86e597SLaura Abbott } 1264af86e597SLaura Abbott } 1265af86e597SLaura Abbott 126618b4b276SJames Morse /* 126718b4b276SJames Morse * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we 126818b4b276SJames Morse * ever need to use IPIs for TLB broadcasting, then we're in trouble here. 126918b4b276SJames Morse */ 1270af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx, 1271af86e597SLaura Abbott phys_addr_t phys, pgprot_t flags) 1272af86e597SLaura Abbott { 1273af86e597SLaura Abbott unsigned long addr = __fix_to_virt(idx); 127420a004e7SWill Deacon pte_t *ptep; 1275af86e597SLaura Abbott 1276b63dbef9SMark Rutland BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); 1277af86e597SLaura Abbott 127820a004e7SWill Deacon ptep = fixmap_pte(addr); 1279af86e597SLaura Abbott 1280af86e597SLaura Abbott if (pgprot_val(flags)) { 128120a004e7SWill Deacon set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags)); 1282af86e597SLaura Abbott } else { 128320a004e7SWill Deacon pte_clear(&init_mm, addr, ptep); 1284af86e597SLaura Abbott flush_tlb_kernel_range(addr, addr+PAGE_SIZE); 1285af86e597SLaura Abbott } 1286af86e597SLaura Abbott } 128761bd93ceSArd Biesheuvel 1288e112b032SHsin-Yi Wang void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) 128961bd93ceSArd Biesheuvel { 129061bd93ceSArd Biesheuvel const u64 dt_virt_base = __fix_to_virt(FIX_FDT); 1291f80fb3a3SArd Biesheuvel int offset; 129261bd93ceSArd Biesheuvel void *dt_virt; 129361bd93ceSArd Biesheuvel 129461bd93ceSArd Biesheuvel /* 129561bd93ceSArd Biesheuvel * Check whether the physical FDT address is set and meets the minimum 129661bd93ceSArd Biesheuvel * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be 129704a84810SArd Biesheuvel * at least 8 bytes so that we can always access the magic and size 129804a84810SArd Biesheuvel * fields of the FDT header after mapping the first chunk, double check 129904a84810SArd Biesheuvel * here if that is indeed the case. 130061bd93ceSArd Biesheuvel */ 130161bd93ceSArd Biesheuvel BUILD_BUG_ON(MIN_FDT_ALIGN < 8); 130261bd93ceSArd Biesheuvel if (!dt_phys || dt_phys % MIN_FDT_ALIGN) 130361bd93ceSArd Biesheuvel return NULL; 130461bd93ceSArd Biesheuvel 130561bd93ceSArd Biesheuvel /* 130661bd93ceSArd Biesheuvel * Make sure that the FDT region can be mapped without the need to 130761bd93ceSArd Biesheuvel * allocate additional translation table pages, so that it is safe 1308132233a7SLaura Abbott * to call create_mapping_noalloc() this early. 130961bd93ceSArd Biesheuvel * 131061bd93ceSArd Biesheuvel * On 64k pages, the FDT will be mapped using PTEs, so we need to 131161bd93ceSArd Biesheuvel * be in the same PMD as the rest of the fixmap. 131261bd93ceSArd Biesheuvel * On 4k pages, we'll use section mappings for the FDT so we only 131361bd93ceSArd Biesheuvel * have to be in the same PUD. 131461bd93ceSArd Biesheuvel */ 131561bd93ceSArd Biesheuvel BUILD_BUG_ON(dt_virt_base % SZ_2M); 131661bd93ceSArd Biesheuvel 1317b433dce0SSuzuki K. Poulose BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT != 1318b433dce0SSuzuki K. Poulose __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT); 131961bd93ceSArd Biesheuvel 1320b433dce0SSuzuki K. Poulose offset = dt_phys % SWAPPER_BLOCK_SIZE; 132161bd93ceSArd Biesheuvel dt_virt = (void *)dt_virt_base + offset; 132261bd93ceSArd Biesheuvel 132361bd93ceSArd Biesheuvel /* map the first chunk so we can read the size from the header */ 1324132233a7SLaura Abbott create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), 1325132233a7SLaura Abbott dt_virt_base, SWAPPER_BLOCK_SIZE, prot); 132661bd93ceSArd Biesheuvel 132704a84810SArd Biesheuvel if (fdt_magic(dt_virt) != FDT_MAGIC) 132861bd93ceSArd Biesheuvel return NULL; 132961bd93ceSArd Biesheuvel 1330f80fb3a3SArd Biesheuvel *size = fdt_totalsize(dt_virt); 1331f80fb3a3SArd Biesheuvel if (*size > MAX_FDT_SIZE) 133261bd93ceSArd Biesheuvel return NULL; 133361bd93ceSArd Biesheuvel 1334f80fb3a3SArd Biesheuvel if (offset + *size > SWAPPER_BLOCK_SIZE) 1335132233a7SLaura Abbott create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base, 1336f80fb3a3SArd Biesheuvel round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot); 1337f80fb3a3SArd Biesheuvel 1338f80fb3a3SArd Biesheuvel return dt_virt; 1339f80fb3a3SArd Biesheuvel } 1340f80fb3a3SArd Biesheuvel 134120a004e7SWill Deacon int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot) 1342324420bfSArd Biesheuvel { 1343f7f0097aSAnshuman Khandual pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot)); 134415122ee2SWill Deacon 134582034c23SLaura Abbott /* Only allow permission changes for now */ 134682034c23SLaura Abbott if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)), 134782034c23SLaura Abbott pud_val(new_pud))) 134815122ee2SWill Deacon return 0; 134915122ee2SWill Deacon 135087dedf7cSAnshuman Khandual VM_BUG_ON(phys & ~PUD_MASK); 135182034c23SLaura Abbott set_pud(pudp, new_pud); 1352324420bfSArd Biesheuvel return 1; 1353324420bfSArd Biesheuvel } 1354324420bfSArd Biesheuvel 135520a004e7SWill Deacon int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot) 1356324420bfSArd Biesheuvel { 1357f7f0097aSAnshuman Khandual pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot)); 135815122ee2SWill Deacon 135982034c23SLaura Abbott /* Only allow permission changes for now */ 136082034c23SLaura Abbott if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)), 136182034c23SLaura Abbott pmd_val(new_pmd))) 136215122ee2SWill Deacon return 0; 136315122ee2SWill Deacon 136487dedf7cSAnshuman Khandual VM_BUG_ON(phys & ~PMD_MASK); 136582034c23SLaura Abbott set_pmd(pmdp, new_pmd); 1366324420bfSArd Biesheuvel return 1; 1367324420bfSArd Biesheuvel } 1368324420bfSArd Biesheuvel 136920a004e7SWill Deacon int pud_clear_huge(pud_t *pudp) 1370324420bfSArd Biesheuvel { 137120a004e7SWill Deacon if (!pud_sect(READ_ONCE(*pudp))) 1372324420bfSArd Biesheuvel return 0; 137320a004e7SWill Deacon pud_clear(pudp); 1374324420bfSArd Biesheuvel return 1; 1375324420bfSArd Biesheuvel } 1376324420bfSArd Biesheuvel 137720a004e7SWill Deacon int pmd_clear_huge(pmd_t *pmdp) 1378324420bfSArd Biesheuvel { 137920a004e7SWill Deacon if (!pmd_sect(READ_ONCE(*pmdp))) 1380324420bfSArd Biesheuvel return 0; 138120a004e7SWill Deacon pmd_clear(pmdp); 1382324420bfSArd Biesheuvel return 1; 1383324420bfSArd Biesheuvel } 1384b6bdb751SToshi Kani 1385ec28bb9cSChintan Pandya int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr) 1386b6bdb751SToshi Kani { 1387ec28bb9cSChintan Pandya pte_t *table; 1388ec28bb9cSChintan Pandya pmd_t pmd; 1389ec28bb9cSChintan Pandya 1390ec28bb9cSChintan Pandya pmd = READ_ONCE(*pmdp); 1391ec28bb9cSChintan Pandya 1392fac880c7SMark Rutland if (!pmd_table(pmd)) { 13939c006972SWill Deacon VM_WARN_ON(1); 1394ec28bb9cSChintan Pandya return 1; 1395b6bdb751SToshi Kani } 1396b6bdb751SToshi Kani 1397ec28bb9cSChintan Pandya table = pte_offset_kernel(pmdp, addr); 1398ec28bb9cSChintan Pandya pmd_clear(pmdp); 1399ec28bb9cSChintan Pandya __flush_tlb_kernel_pgtable(addr); 1400ec28bb9cSChintan Pandya pte_free_kernel(NULL, table); 1401ec28bb9cSChintan Pandya return 1; 1402ec28bb9cSChintan Pandya } 1403ec28bb9cSChintan Pandya 1404ec28bb9cSChintan Pandya int pud_free_pmd_page(pud_t *pudp, unsigned long addr) 1405b6bdb751SToshi Kani { 1406ec28bb9cSChintan Pandya pmd_t *table; 1407ec28bb9cSChintan Pandya pmd_t *pmdp; 1408ec28bb9cSChintan Pandya pud_t pud; 1409ec28bb9cSChintan Pandya unsigned long next, end; 1410ec28bb9cSChintan Pandya 1411ec28bb9cSChintan Pandya pud = READ_ONCE(*pudp); 1412ec28bb9cSChintan Pandya 1413fac880c7SMark Rutland if (!pud_table(pud)) { 14149c006972SWill Deacon VM_WARN_ON(1); 1415ec28bb9cSChintan Pandya return 1; 1416ec28bb9cSChintan Pandya } 1417ec28bb9cSChintan Pandya 1418ec28bb9cSChintan Pandya table = pmd_offset(pudp, addr); 1419ec28bb9cSChintan Pandya pmdp = table; 1420ec28bb9cSChintan Pandya next = addr; 1421ec28bb9cSChintan Pandya end = addr + PUD_SIZE; 1422ec28bb9cSChintan Pandya do { 1423ec28bb9cSChintan Pandya pmd_free_pte_page(pmdp, next); 1424ec28bb9cSChintan Pandya } while (pmdp++, next += PMD_SIZE, next != end); 1425ec28bb9cSChintan Pandya 1426ec28bb9cSChintan Pandya pud_clear(pudp); 1427ec28bb9cSChintan Pandya __flush_tlb_kernel_pgtable(addr); 1428ec28bb9cSChintan Pandya pmd_free(NULL, table); 1429ec28bb9cSChintan Pandya return 1; 1430b6bdb751SToshi Kani } 14314ab21506SRobin Murphy 14324ab21506SRobin Murphy #ifdef CONFIG_MEMORY_HOTPLUG 1433bbd6ec60SAnshuman Khandual static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size) 1434bbd6ec60SAnshuman Khandual { 1435bbd6ec60SAnshuman Khandual unsigned long end = start + size; 1436bbd6ec60SAnshuman Khandual 1437bbd6ec60SAnshuman Khandual WARN_ON(pgdir != init_mm.pgd); 1438bbd6ec60SAnshuman Khandual WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END)); 1439bbd6ec60SAnshuman Khandual 1440eee07935SAnshuman Khandual unmap_hotplug_range(start, end, false, NULL); 1441bbd6ec60SAnshuman Khandual free_empty_tables(start, end, PAGE_OFFSET, PAGE_END); 1442bbd6ec60SAnshuman Khandual } 1443bbd6ec60SAnshuman Khandual 144403aaf83fSAnshuman Khandual struct range arch_get_mappable_range(void) 144558284a90SAnshuman Khandual { 144603aaf83fSAnshuman Khandual struct range mhp_range; 1447ee7febceSPavel Tatashin u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual)); 1448ee7febceSPavel Tatashin u64 end_linear_pa = __pa(PAGE_END - 1); 1449ee7febceSPavel Tatashin 1450ee7febceSPavel Tatashin if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 1451ee7febceSPavel Tatashin /* 1452ee7febceSPavel Tatashin * Check for a wrap, it is possible because of randomized linear 1453ee7febceSPavel Tatashin * mapping the start physical address is actually bigger than 1454ee7febceSPavel Tatashin * the end physical address. In this case set start to zero 1455ee7febceSPavel Tatashin * because [0, end_linear_pa] range must still be able to cover 1456ee7febceSPavel Tatashin * all addressable physical addresses. 1457ee7febceSPavel Tatashin */ 1458ee7febceSPavel Tatashin if (start_linear_pa > end_linear_pa) 1459ee7febceSPavel Tatashin start_linear_pa = 0; 1460ee7febceSPavel Tatashin } 1461ee7febceSPavel Tatashin 1462ee7febceSPavel Tatashin WARN_ON(start_linear_pa > end_linear_pa); 146303aaf83fSAnshuman Khandual 146458284a90SAnshuman Khandual /* 146558284a90SAnshuman Khandual * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)] 146658284a90SAnshuman Khandual * accommodating both its ends but excluding PAGE_END. Max physical 146758284a90SAnshuman Khandual * range which can be mapped inside this linear mapping range, must 146858284a90SAnshuman Khandual * also be derived from its end points. 146958284a90SAnshuman Khandual */ 1470ee7febceSPavel Tatashin mhp_range.start = start_linear_pa; 1471ee7febceSPavel Tatashin mhp_range.end = end_linear_pa; 1472ee7febceSPavel Tatashin 147303aaf83fSAnshuman Khandual return mhp_range; 147458284a90SAnshuman Khandual } 147558284a90SAnshuman Khandual 1476940519f0SMichal Hocko int arch_add_memory(int nid, u64 start, u64 size, 1477f5637d3bSLogan Gunthorpe struct mhp_params *params) 14784ab21506SRobin Murphy { 147987143f40SArd Biesheuvel int ret, flags = NO_EXEC_MAPPINGS; 14804ab21506SRobin Murphy 148103aaf83fSAnshuman Khandual VM_BUG_ON(!mhp_range_allowed(start, size, true)); 1482840b2398SMarco Elver 1483840b2398SMarco Elver /* 1484840b2398SMarco Elver * KFENCE requires linear map to be mapped at page granularity, so that 1485840b2398SMarco Elver * it is possible to protect/unprotect single pages in the KFENCE pool. 1486840b2398SMarco Elver */ 1487840b2398SMarco Elver if (rodata_full || debug_pagealloc_enabled() || 1488840b2398SMarco Elver IS_ENABLED(CONFIG_KFENCE)) 148987143f40SArd Biesheuvel flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 14904ab21506SRobin Murphy 14914ab21506SRobin Murphy __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start), 1492bfeb022fSLogan Gunthorpe size, params->pgprot, __pgd_pgtable_alloc, 1493bfeb022fSLogan Gunthorpe flags); 14944ab21506SRobin Murphy 149516993c0fSDan Williams memblock_clear_nomap(start, size); 149616993c0fSDan Williams 1497bbd6ec60SAnshuman Khandual ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT, 1498f5637d3bSLogan Gunthorpe params); 1499bbd6ec60SAnshuman Khandual if (ret) 1500bbd6ec60SAnshuman Khandual __remove_pgd_mapping(swapper_pg_dir, 1501bbd6ec60SAnshuman Khandual __phys_to_virt(start), size); 1502bbd6ec60SAnshuman Khandual return ret; 15034ab21506SRobin Murphy } 1504bbd6ec60SAnshuman Khandual 150522eb6346SDavid Hildenbrand void arch_remove_memory(int nid, u64 start, u64 size, 150622eb6346SDavid Hildenbrand struct vmem_altmap *altmap) 150722eb6346SDavid Hildenbrand { 150822eb6346SDavid Hildenbrand unsigned long start_pfn = start >> PAGE_SHIFT; 150922eb6346SDavid Hildenbrand unsigned long nr_pages = size >> PAGE_SHIFT; 151022eb6346SDavid Hildenbrand 1511feee6b29SDavid Hildenbrand __remove_pages(start_pfn, nr_pages, altmap); 1512bbd6ec60SAnshuman Khandual __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size); 151322eb6346SDavid Hildenbrand } 1514bbd6ec60SAnshuman Khandual 1515bbd6ec60SAnshuman Khandual /* 1516bbd6ec60SAnshuman Khandual * This memory hotplug notifier helps prevent boot memory from being 1517bbd6ec60SAnshuman Khandual * inadvertently removed as it blocks pfn range offlining process in 1518bbd6ec60SAnshuman Khandual * __offline_pages(). Hence this prevents both offlining as well as 1519bbd6ec60SAnshuman Khandual * removal process for boot memory which is initially always online. 1520bbd6ec60SAnshuman Khandual * In future if and when boot memory could be removed, this notifier 1521bbd6ec60SAnshuman Khandual * should be dropped and free_hotplug_page_range() should handle any 1522bbd6ec60SAnshuman Khandual * reserved pages allocated during boot. 1523bbd6ec60SAnshuman Khandual */ 1524bbd6ec60SAnshuman Khandual static int prevent_bootmem_remove_notifier(struct notifier_block *nb, 1525bbd6ec60SAnshuman Khandual unsigned long action, void *data) 1526bbd6ec60SAnshuman Khandual { 1527bbd6ec60SAnshuman Khandual struct mem_section *ms; 1528bbd6ec60SAnshuman Khandual struct memory_notify *arg = data; 1529bbd6ec60SAnshuman Khandual unsigned long end_pfn = arg->start_pfn + arg->nr_pages; 1530bbd6ec60SAnshuman Khandual unsigned long pfn = arg->start_pfn; 1531bbd6ec60SAnshuman Khandual 15329fb3d4a3SAnshuman Khandual if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE)) 1533bbd6ec60SAnshuman Khandual return NOTIFY_OK; 1534bbd6ec60SAnshuman Khandual 1535bbd6ec60SAnshuman Khandual for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 15369fb3d4a3SAnshuman Khandual unsigned long start = PFN_PHYS(pfn); 15379fb3d4a3SAnshuman Khandual unsigned long end = start + (1UL << PA_SECTION_SHIFT); 15389fb3d4a3SAnshuman Khandual 1539bbd6ec60SAnshuman Khandual ms = __pfn_to_section(pfn); 15409fb3d4a3SAnshuman Khandual if (!early_section(ms)) 15419fb3d4a3SAnshuman Khandual continue; 15429fb3d4a3SAnshuman Khandual 15439fb3d4a3SAnshuman Khandual if (action == MEM_GOING_OFFLINE) { 15449fb3d4a3SAnshuman Khandual /* 15459fb3d4a3SAnshuman Khandual * Boot memory removal is not supported. Prevent 15469fb3d4a3SAnshuman Khandual * it via blocking any attempted offline request 15479fb3d4a3SAnshuman Khandual * for the boot memory and just report it. 15489fb3d4a3SAnshuman Khandual */ 15499fb3d4a3SAnshuman Khandual pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end); 1550bbd6ec60SAnshuman Khandual return NOTIFY_BAD; 15519fb3d4a3SAnshuman Khandual } else if (action == MEM_OFFLINE) { 15529fb3d4a3SAnshuman Khandual /* 15539fb3d4a3SAnshuman Khandual * This should have never happened. Boot memory 15549fb3d4a3SAnshuman Khandual * offlining should have been prevented by this 15559fb3d4a3SAnshuman Khandual * very notifier. Probably some memory removal 15569fb3d4a3SAnshuman Khandual * procedure might have changed which would then 15579fb3d4a3SAnshuman Khandual * require further debug. 15589fb3d4a3SAnshuman Khandual */ 15599fb3d4a3SAnshuman Khandual pr_err("Boot memory [%lx %lx] offlined\n", start, end); 15609fb3d4a3SAnshuman Khandual 15619fb3d4a3SAnshuman Khandual /* 15629fb3d4a3SAnshuman Khandual * Core memory hotplug does not process a return 15639fb3d4a3SAnshuman Khandual * code from the notifier for MEM_OFFLINE events. 15649fb3d4a3SAnshuman Khandual * The error condition has been reported. Return 15659fb3d4a3SAnshuman Khandual * from here as if ignored. 15669fb3d4a3SAnshuman Khandual */ 15679fb3d4a3SAnshuman Khandual return NOTIFY_DONE; 15689fb3d4a3SAnshuman Khandual } 1569bbd6ec60SAnshuman Khandual } 1570bbd6ec60SAnshuman Khandual return NOTIFY_OK; 1571bbd6ec60SAnshuman Khandual } 1572bbd6ec60SAnshuman Khandual 1573bbd6ec60SAnshuman Khandual static struct notifier_block prevent_bootmem_remove_nb = { 1574bbd6ec60SAnshuman Khandual .notifier_call = prevent_bootmem_remove_notifier, 1575bbd6ec60SAnshuman Khandual }; 1576bbd6ec60SAnshuman Khandual 1577fdd99a41SAnshuman Khandual /* 1578fdd99a41SAnshuman Khandual * This ensures that boot memory sections on the platform are online 1579fdd99a41SAnshuman Khandual * from early boot. Memory sections could not be prevented from being 1580fdd99a41SAnshuman Khandual * offlined, unless for some reason they are not online to begin with. 1581fdd99a41SAnshuman Khandual * This helps validate the basic assumption on which the above memory 1582fdd99a41SAnshuman Khandual * event notifier works to prevent boot memory section offlining and 1583fdd99a41SAnshuman Khandual * its possible removal. 1584fdd99a41SAnshuman Khandual */ 1585fdd99a41SAnshuman Khandual static void validate_bootmem_online(void) 1586fdd99a41SAnshuman Khandual { 1587fdd99a41SAnshuman Khandual phys_addr_t start, end, addr; 1588fdd99a41SAnshuman Khandual struct mem_section *ms; 1589fdd99a41SAnshuman Khandual u64 i; 1590fdd99a41SAnshuman Khandual 1591fdd99a41SAnshuman Khandual /* 1592fdd99a41SAnshuman Khandual * Scanning across all memblock might be expensive 1593fdd99a41SAnshuman Khandual * on some big memory systems. Hence enable this 1594fdd99a41SAnshuman Khandual * validation only with DEBUG_VM. 1595fdd99a41SAnshuman Khandual */ 1596fdd99a41SAnshuman Khandual if (!IS_ENABLED(CONFIG_DEBUG_VM)) 1597fdd99a41SAnshuman Khandual return; 1598fdd99a41SAnshuman Khandual 1599fdd99a41SAnshuman Khandual for_each_mem_range(i, &start, &end) { 1600fdd99a41SAnshuman Khandual for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) { 1601fdd99a41SAnshuman Khandual ms = __pfn_to_section(PHYS_PFN(addr)); 1602fdd99a41SAnshuman Khandual 1603fdd99a41SAnshuman Khandual /* 1604fdd99a41SAnshuman Khandual * All memory ranges in the system at this point 1605fdd99a41SAnshuman Khandual * should have been marked as early sections. 1606fdd99a41SAnshuman Khandual */ 1607fdd99a41SAnshuman Khandual WARN_ON(!early_section(ms)); 1608fdd99a41SAnshuman Khandual 1609fdd99a41SAnshuman Khandual /* 1610fdd99a41SAnshuman Khandual * Memory notifier mechanism here to prevent boot 1611fdd99a41SAnshuman Khandual * memory offlining depends on the fact that each 1612fdd99a41SAnshuman Khandual * early section memory on the system is initially 1613fdd99a41SAnshuman Khandual * online. Otherwise a given memory section which 1614fdd99a41SAnshuman Khandual * is already offline will be overlooked and can 1615fdd99a41SAnshuman Khandual * be removed completely. Call out such sections. 1616fdd99a41SAnshuman Khandual */ 1617fdd99a41SAnshuman Khandual if (!online_section(ms)) 1618fdd99a41SAnshuman Khandual pr_err("Boot memory [%llx %llx] is offline, can be removed\n", 1619fdd99a41SAnshuman Khandual addr, addr + (1UL << PA_SECTION_SHIFT)); 1620fdd99a41SAnshuman Khandual } 1621fdd99a41SAnshuman Khandual } 1622fdd99a41SAnshuman Khandual } 1623fdd99a41SAnshuman Khandual 1624bbd6ec60SAnshuman Khandual static int __init prevent_bootmem_remove_init(void) 1625bbd6ec60SAnshuman Khandual { 1626cb45babeSAnshuman Khandual int ret = 0; 1627cb45babeSAnshuman Khandual 1628cb45babeSAnshuman Khandual if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE)) 1629cb45babeSAnshuman Khandual return ret; 1630cb45babeSAnshuman Khandual 1631fdd99a41SAnshuman Khandual validate_bootmem_online(); 1632cb45babeSAnshuman Khandual ret = register_memory_notifier(&prevent_bootmem_remove_nb); 1633cb45babeSAnshuman Khandual if (ret) 1634cb45babeSAnshuman Khandual pr_err("%s: Notifier registration failed %d\n", __func__, ret); 1635cb45babeSAnshuman Khandual 1636cb45babeSAnshuman Khandual return ret; 1637bbd6ec60SAnshuman Khandual } 1638cb45babeSAnshuman Khandual early_initcall(prevent_bootmem_remove_init); 163922eb6346SDavid Hildenbrand #endif 1640