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> 256d47c23bSMike Rapoport #include <linux/set_memory.h> 26c1cc1552SCatalin Marinas 2721ab99c2SMark Rutland #include <asm/barrier.h> 28c1cc1552SCatalin Marinas #include <asm/cputype.h> 29af86e597SLaura Abbott #include <asm/fixmap.h> 30068a17a5SMark Rutland #include <asm/kasan.h> 31b433dce0SSuzuki K. Poulose #include <asm/kernel-pgtable.h> 32c1cc1552SCatalin Marinas #include <asm/sections.h> 33c1cc1552SCatalin Marinas #include <asm/setup.h> 3487dfb311SMasahiro Yamada #include <linux/sizes.h> 35c1cc1552SCatalin Marinas #include <asm/tlb.h> 36c1cc1552SCatalin Marinas #include <asm/mmu_context.h> 371404d6f1SLaura Abbott #include <asm/ptdump.h> 38ec28bb9cSChintan Pandya #include <asm/tlbflush.h> 39ca15ca40SMike Rapoport #include <asm/pgalloc.h> 40c1cc1552SCatalin Marinas 41c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS BIT(0) 42d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS BIT(1) 4387143f40SArd Biesheuvel #define NO_EXEC_MAPPINGS BIT(2) /* assumes FEAT_HPDS is not used */ 44c0951366SArd Biesheuvel 457ba8f2b2SArd Biesheuvel u64 idmap_t0sz = TCR_T0SZ(VA_BITS_MIN); 46fa2a8445SKristina Martsenko u64 idmap_ptrs_per_pgd = PTRS_PER_PGD; 47dd006da2SArd Biesheuvel 485383cc6eSSteve Capper u64 __section(".mmuoff.data.write") vabits_actual; 495383cc6eSSteve Capper EXPORT_SYMBOL(vabits_actual); 50c1cc1552SCatalin Marinas 515a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init; 52a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset); 53a7f8de16SArd Biesheuvel 54c1cc1552SCatalin Marinas /* 55c1cc1552SCatalin Marinas * Empty_zero_page is a special page that is used for zero-initialized data 56c1cc1552SCatalin Marinas * and COW. 57c1cc1552SCatalin Marinas */ 585227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss; 59c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page); 60c1cc1552SCatalin Marinas 61f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss; 62f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused; 63f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused; 64f9040773SArd Biesheuvel 652330b7caSJun Yao static DEFINE_SPINLOCK(swapper_pgdir_lock); 662330b7caSJun Yao 672330b7caSJun Yao void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd) 682330b7caSJun Yao { 692330b7caSJun Yao pgd_t *fixmap_pgdp; 702330b7caSJun Yao 712330b7caSJun Yao spin_lock(&swapper_pgdir_lock); 7226a6f87eSJames Morse fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp)); 732330b7caSJun Yao WRITE_ONCE(*fixmap_pgdp, pgd); 742330b7caSJun Yao /* 752330b7caSJun Yao * We need dsb(ishst) here to ensure the page-table-walker sees 762330b7caSJun Yao * our new entry before set_p?d() returns. The fixmap's 772330b7caSJun Yao * flush_tlb_kernel_range() via clear_fixmap() does this for us. 782330b7caSJun Yao */ 792330b7caSJun Yao pgd_clear_fixmap(); 802330b7caSJun Yao spin_unlock(&swapper_pgdir_lock); 812330b7caSJun Yao } 822330b7caSJun Yao 83c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 84c1cc1552SCatalin Marinas unsigned long size, pgprot_t vma_prot) 85c1cc1552SCatalin Marinas { 86873ba463SMike Rapoport if (!pfn_is_map_memory(pfn)) 87c1cc1552SCatalin Marinas return pgprot_noncached(vma_prot); 88c1cc1552SCatalin Marinas else if (file->f_flags & O_SYNC) 89c1cc1552SCatalin Marinas return pgprot_writecombine(vma_prot); 90c1cc1552SCatalin Marinas return vma_prot; 91c1cc1552SCatalin Marinas } 92c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot); 93c1cc1552SCatalin Marinas 9490292acaSYu Zhao static phys_addr_t __init early_pgtable_alloc(int shift) 95c1cc1552SCatalin Marinas { 967142392dSSuzuki K. Poulose phys_addr_t phys; 977142392dSSuzuki K. Poulose void *ptr; 987142392dSSuzuki K. Poulose 99c6975d7cSQian Cai phys = memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE, 0, 100c6975d7cSQian Cai MEMBLOCK_ALLOC_NOLEAKTRACE); 101ecc3e771SMike Rapoport if (!phys) 102ecc3e771SMike Rapoport panic("Failed to allocate page table page\n"); 103f4710445SMark Rutland 104f4710445SMark Rutland /* 105f4710445SMark Rutland * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE 106f4710445SMark Rutland * slot will be free, so we can (ab)use the FIX_PTE slot to initialise 107f4710445SMark Rutland * any level of table. 108f4710445SMark Rutland */ 109f4710445SMark Rutland ptr = pte_set_fixmap(phys); 110f4710445SMark Rutland 11121ab99c2SMark Rutland memset(ptr, 0, PAGE_SIZE); 11221ab99c2SMark Rutland 113f4710445SMark Rutland /* 114f4710445SMark Rutland * Implicit barriers also ensure the zeroed page is visible to the page 115f4710445SMark Rutland * table walker 116f4710445SMark Rutland */ 117f4710445SMark Rutland pte_clear_fixmap(); 118f4710445SMark Rutland 119f4710445SMark Rutland return phys; 120c1cc1552SCatalin Marinas } 121c1cc1552SCatalin Marinas 122e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new) 123e98216b5SArd Biesheuvel { 124e98216b5SArd Biesheuvel /* 125e98216b5SArd Biesheuvel * The following mapping attributes may be updated in live 126e98216b5SArd Biesheuvel * kernel mappings without the need for break-before-make. 127e98216b5SArd Biesheuvel */ 1280178dc76SCatalin Marinas pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG; 129e98216b5SArd Biesheuvel 130141d1497SArd Biesheuvel /* creating or taking down mappings is always safe */ 131141d1497SArd Biesheuvel if (old == 0 || new == 0) 132141d1497SArd Biesheuvel return true; 133141d1497SArd Biesheuvel 134141d1497SArd Biesheuvel /* live contiguous mappings may not be manipulated at all */ 135141d1497SArd Biesheuvel if ((old | new) & PTE_CONT) 136141d1497SArd Biesheuvel return false; 137141d1497SArd Biesheuvel 138753e8abcSArd Biesheuvel /* Transitioning from Non-Global to Global is unsafe */ 139753e8abcSArd Biesheuvel if (old & ~new & PTE_NG) 140753e8abcSArd Biesheuvel return false; 1414e602056SWill Deacon 1420178dc76SCatalin Marinas /* 1430178dc76SCatalin Marinas * Changing the memory type between Normal and Normal-Tagged is safe 1440178dc76SCatalin Marinas * since Tagged is considered a permission attribute from the 1450178dc76SCatalin Marinas * mismatched attribute aliases perspective. 1460178dc76SCatalin Marinas */ 1470178dc76SCatalin Marinas if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 1480178dc76SCatalin Marinas (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) && 1490178dc76SCatalin Marinas ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 1500178dc76SCatalin Marinas (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED))) 1510178dc76SCatalin Marinas mask |= PTE_ATTRINDX_MASK; 1520178dc76SCatalin Marinas 153141d1497SArd Biesheuvel return ((old ^ new) & ~mask) == 0; 154e98216b5SArd Biesheuvel } 155e98216b5SArd Biesheuvel 15620a004e7SWill Deacon static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end, 157d27cfa1fSArd Biesheuvel phys_addr_t phys, pgprot_t prot) 158c1cc1552SCatalin Marinas { 15920a004e7SWill Deacon pte_t *ptep; 160c1cc1552SCatalin Marinas 16120a004e7SWill Deacon ptep = pte_set_fixmap_offset(pmdp, addr); 162c1cc1552SCatalin Marinas do { 16320a004e7SWill Deacon pte_t old_pte = READ_ONCE(*ptep); 164e98216b5SArd Biesheuvel 16520a004e7SWill Deacon set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot)); 166e98216b5SArd Biesheuvel 167e98216b5SArd Biesheuvel /* 168e98216b5SArd Biesheuvel * After the PTE entry has been populated once, we 169e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 170e98216b5SArd Biesheuvel */ 17120a004e7SWill Deacon BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), 17220a004e7SWill Deacon READ_ONCE(pte_val(*ptep)))); 173e98216b5SArd Biesheuvel 174e393cf40SArd Biesheuvel phys += PAGE_SIZE; 17520a004e7SWill Deacon } while (ptep++, addr += PAGE_SIZE, addr != end); 176f4710445SMark Rutland 177f4710445SMark Rutland pte_clear_fixmap(); 178c1cc1552SCatalin Marinas } 179c1cc1552SCatalin Marinas 18020a004e7SWill Deacon static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr, 181d27cfa1fSArd Biesheuvel unsigned long end, phys_addr_t phys, 182d27cfa1fSArd Biesheuvel pgprot_t prot, 18390292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 184c0951366SArd Biesheuvel int flags) 185c1cc1552SCatalin Marinas { 186c1cc1552SCatalin Marinas unsigned long next; 18720a004e7SWill Deacon pmd_t pmd = READ_ONCE(*pmdp); 188c1cc1552SCatalin Marinas 18920a004e7SWill Deacon BUG_ON(pmd_sect(pmd)); 19020a004e7SWill Deacon if (pmd_none(pmd)) { 19187143f40SArd Biesheuvel pmdval_t pmdval = PMD_TYPE_TABLE | PMD_TABLE_UXN; 192d27cfa1fSArd Biesheuvel phys_addr_t pte_phys; 19387143f40SArd Biesheuvel 19487143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 19587143f40SArd Biesheuvel pmdval |= PMD_TABLE_PXN; 196132233a7SLaura Abbott BUG_ON(!pgtable_alloc); 19790292acaSYu Zhao pte_phys = pgtable_alloc(PAGE_SHIFT); 19887143f40SArd Biesheuvel __pmd_populate(pmdp, pte_phys, pmdval); 19920a004e7SWill Deacon pmd = READ_ONCE(*pmdp); 200c1cc1552SCatalin Marinas } 20120a004e7SWill Deacon BUG_ON(pmd_bad(pmd)); 202d27cfa1fSArd Biesheuvel 203d27cfa1fSArd Biesheuvel do { 204d27cfa1fSArd Biesheuvel pgprot_t __prot = prot; 205d27cfa1fSArd Biesheuvel 206d27cfa1fSArd Biesheuvel next = pte_cont_addr_end(addr, end); 207d27cfa1fSArd Biesheuvel 208d27cfa1fSArd Biesheuvel /* use a contiguous mapping if the range is suitably aligned */ 209d27cfa1fSArd Biesheuvel if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) && 210d27cfa1fSArd Biesheuvel (flags & NO_CONT_MAPPINGS) == 0) 211d27cfa1fSArd Biesheuvel __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 212d27cfa1fSArd Biesheuvel 21320a004e7SWill Deacon init_pte(pmdp, addr, next, phys, __prot); 214d27cfa1fSArd Biesheuvel 215d27cfa1fSArd Biesheuvel phys += next - addr; 216d27cfa1fSArd Biesheuvel } while (addr = next, addr != end); 217d27cfa1fSArd Biesheuvel } 218d27cfa1fSArd Biesheuvel 21920a004e7SWill Deacon static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end, 220d27cfa1fSArd Biesheuvel phys_addr_t phys, pgprot_t prot, 22190292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), int flags) 222d27cfa1fSArd Biesheuvel { 223d27cfa1fSArd Biesheuvel unsigned long next; 22420a004e7SWill Deacon pmd_t *pmdp; 225c1cc1552SCatalin Marinas 22620a004e7SWill Deacon pmdp = pmd_set_fixmap_offset(pudp, addr); 227c1cc1552SCatalin Marinas do { 22820a004e7SWill Deacon pmd_t old_pmd = READ_ONCE(*pmdp); 229e98216b5SArd Biesheuvel 230c1cc1552SCatalin Marinas next = pmd_addr_end(addr, end); 231e98216b5SArd Biesheuvel 232c1cc1552SCatalin Marinas /* try section mapping first */ 2334aaa87abSAnshuman Khandual if (((addr | next | phys) & ~PMD_MASK) == 0 && 234c0951366SArd Biesheuvel (flags & NO_BLOCK_MAPPINGS) == 0) { 23520a004e7SWill Deacon pmd_set_huge(pmdp, phys, prot); 236e98216b5SArd Biesheuvel 237a55f9929SCatalin Marinas /* 238e98216b5SArd Biesheuvel * After the PMD entry has been populated once, we 239e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 240a55f9929SCatalin Marinas */ 241e98216b5SArd Biesheuvel BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd), 24220a004e7SWill Deacon READ_ONCE(pmd_val(*pmdp)))); 243a55f9929SCatalin Marinas } else { 24420a004e7SWill Deacon alloc_init_cont_pte(pmdp, addr, next, phys, prot, 245d27cfa1fSArd Biesheuvel pgtable_alloc, flags); 246e98216b5SArd Biesheuvel 247e98216b5SArd Biesheuvel BUG_ON(pmd_val(old_pmd) != 0 && 24820a004e7SWill Deacon pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp))); 249a55f9929SCatalin Marinas } 250c1cc1552SCatalin Marinas phys += next - addr; 25120a004e7SWill Deacon } while (pmdp++, addr = next, addr != end); 252f4710445SMark Rutland 253f4710445SMark Rutland pmd_clear_fixmap(); 254c1cc1552SCatalin Marinas } 255c1cc1552SCatalin Marinas 25620a004e7SWill Deacon static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr, 257d27cfa1fSArd Biesheuvel unsigned long end, phys_addr_t phys, 258d27cfa1fSArd Biesheuvel pgprot_t prot, 25990292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), int flags) 260d27cfa1fSArd Biesheuvel { 261d27cfa1fSArd Biesheuvel unsigned long next; 26220a004e7SWill Deacon pud_t pud = READ_ONCE(*pudp); 263d27cfa1fSArd Biesheuvel 264d27cfa1fSArd Biesheuvel /* 265d27cfa1fSArd Biesheuvel * Check for initial section mappings in the pgd/pud. 266d27cfa1fSArd Biesheuvel */ 26720a004e7SWill Deacon BUG_ON(pud_sect(pud)); 26820a004e7SWill Deacon if (pud_none(pud)) { 26987143f40SArd Biesheuvel pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_UXN; 270d27cfa1fSArd Biesheuvel phys_addr_t pmd_phys; 27187143f40SArd Biesheuvel 27287143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 27387143f40SArd Biesheuvel pudval |= PUD_TABLE_PXN; 274d27cfa1fSArd Biesheuvel BUG_ON(!pgtable_alloc); 27590292acaSYu Zhao pmd_phys = pgtable_alloc(PMD_SHIFT); 27687143f40SArd Biesheuvel __pud_populate(pudp, pmd_phys, pudval); 27720a004e7SWill Deacon pud = READ_ONCE(*pudp); 278d27cfa1fSArd Biesheuvel } 27920a004e7SWill Deacon BUG_ON(pud_bad(pud)); 280d27cfa1fSArd Biesheuvel 281d27cfa1fSArd Biesheuvel do { 282d27cfa1fSArd Biesheuvel pgprot_t __prot = prot; 283d27cfa1fSArd Biesheuvel 284d27cfa1fSArd Biesheuvel next = pmd_cont_addr_end(addr, end); 285d27cfa1fSArd Biesheuvel 286d27cfa1fSArd Biesheuvel /* use a contiguous mapping if the range is suitably aligned */ 287d27cfa1fSArd Biesheuvel if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) && 288d27cfa1fSArd Biesheuvel (flags & NO_CONT_MAPPINGS) == 0) 289d27cfa1fSArd Biesheuvel __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 290d27cfa1fSArd Biesheuvel 29120a004e7SWill Deacon init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags); 292d27cfa1fSArd Biesheuvel 293d27cfa1fSArd Biesheuvel phys += next - addr; 294d27cfa1fSArd Biesheuvel } while (addr = next, addr != end); 295d27cfa1fSArd Biesheuvel } 296d27cfa1fSArd Biesheuvel 297da141706SLaura Abbott static inline bool use_1G_block(unsigned long addr, unsigned long next, 298da141706SLaura Abbott unsigned long phys) 299da141706SLaura Abbott { 300da141706SLaura Abbott if (PAGE_SHIFT != 12) 301da141706SLaura Abbott return false; 302da141706SLaura Abbott 303da141706SLaura Abbott if (((addr | next | phys) & ~PUD_MASK) != 0) 304da141706SLaura Abbott return false; 305da141706SLaura Abbott 306da141706SLaura Abbott return true; 307da141706SLaura Abbott } 308da141706SLaura Abbott 30920a004e7SWill Deacon static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, 310da141706SLaura Abbott phys_addr_t phys, pgprot_t prot, 31190292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 312c0951366SArd Biesheuvel int flags) 313c1cc1552SCatalin Marinas { 314c1cc1552SCatalin Marinas unsigned long next; 31520a004e7SWill Deacon pud_t *pudp; 316e9f63768SMike Rapoport p4d_t *p4dp = p4d_offset(pgdp, addr); 317e9f63768SMike Rapoport p4d_t p4d = READ_ONCE(*p4dp); 318c1cc1552SCatalin Marinas 319e9f63768SMike Rapoport if (p4d_none(p4d)) { 32087143f40SArd Biesheuvel p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN; 321132233a7SLaura Abbott phys_addr_t pud_phys; 32287143f40SArd Biesheuvel 32387143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 32487143f40SArd Biesheuvel p4dval |= P4D_TABLE_PXN; 325132233a7SLaura Abbott BUG_ON(!pgtable_alloc); 32690292acaSYu Zhao pud_phys = pgtable_alloc(PUD_SHIFT); 32787143f40SArd Biesheuvel __p4d_populate(p4dp, pud_phys, p4dval); 328e9f63768SMike Rapoport p4d = READ_ONCE(*p4dp); 329c79b954bSJungseok Lee } 330e9f63768SMike Rapoport BUG_ON(p4d_bad(p4d)); 331c79b954bSJungseok Lee 332e9f63768SMike Rapoport pudp = pud_set_fixmap_offset(p4dp, addr); 333c1cc1552SCatalin Marinas do { 33420a004e7SWill Deacon pud_t old_pud = READ_ONCE(*pudp); 335e98216b5SArd Biesheuvel 336c1cc1552SCatalin Marinas next = pud_addr_end(addr, end); 337206a2a73SSteve Capper 338206a2a73SSteve Capper /* 339206a2a73SSteve Capper * For 4K granule only, attempt to put down a 1GB block 340206a2a73SSteve Capper */ 341c0951366SArd Biesheuvel if (use_1G_block(addr, next, phys) && 342c0951366SArd Biesheuvel (flags & NO_BLOCK_MAPPINGS) == 0) { 34320a004e7SWill Deacon pud_set_huge(pudp, phys, prot); 344206a2a73SSteve Capper 345206a2a73SSteve Capper /* 346e98216b5SArd Biesheuvel * After the PUD entry has been populated once, we 347e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 348206a2a73SSteve Capper */ 349e98216b5SArd Biesheuvel BUG_ON(!pgattr_change_is_safe(pud_val(old_pud), 35020a004e7SWill Deacon READ_ONCE(pud_val(*pudp)))); 351206a2a73SSteve Capper } else { 35220a004e7SWill Deacon alloc_init_cont_pmd(pudp, addr, next, phys, prot, 353c0951366SArd Biesheuvel pgtable_alloc, flags); 354e98216b5SArd Biesheuvel 355e98216b5SArd Biesheuvel BUG_ON(pud_val(old_pud) != 0 && 35620a004e7SWill Deacon pud_val(old_pud) != READ_ONCE(pud_val(*pudp))); 357206a2a73SSteve Capper } 358c1cc1552SCatalin Marinas phys += next - addr; 35920a004e7SWill Deacon } while (pudp++, addr = next, addr != end); 360f4710445SMark Rutland 361f4710445SMark Rutland pud_clear_fixmap(); 362c1cc1552SCatalin Marinas } 363c1cc1552SCatalin Marinas 36440f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, 36540f87d31SArd Biesheuvel unsigned long virt, phys_addr_t size, 36640f87d31SArd Biesheuvel pgprot_t prot, 36790292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 368c0951366SArd Biesheuvel int flags) 369c1cc1552SCatalin Marinas { 37032d18708SMasahiro Yamada unsigned long addr, end, next; 371974b9b2cSMike Rapoport pgd_t *pgdp = pgd_offset_pgd(pgdir, virt); 372c1cc1552SCatalin Marinas 373cc5d2b3bSMark Rutland /* 374cc5d2b3bSMark Rutland * If the virtual and physical address don't have the same offset 375cc5d2b3bSMark Rutland * within a page, we cannot map the region as the caller expects. 376cc5d2b3bSMark Rutland */ 377cc5d2b3bSMark Rutland if (WARN_ON((phys ^ virt) & ~PAGE_MASK)) 378cc5d2b3bSMark Rutland return; 379cc5d2b3bSMark Rutland 3809c4e08a3SMark Rutland phys &= PAGE_MASK; 381c1cc1552SCatalin Marinas addr = virt & PAGE_MASK; 38232d18708SMasahiro Yamada end = PAGE_ALIGN(virt + size); 383c1cc1552SCatalin Marinas 384c1cc1552SCatalin Marinas do { 385c1cc1552SCatalin Marinas next = pgd_addr_end(addr, end); 38620a004e7SWill Deacon alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc, 387c0951366SArd Biesheuvel flags); 388c1cc1552SCatalin Marinas phys += next - addr; 38920a004e7SWill Deacon } while (pgdp++, addr = next, addr != end); 390c1cc1552SCatalin Marinas } 391c1cc1552SCatalin Marinas 392475ba3fcSWill Deacon static phys_addr_t __pgd_pgtable_alloc(int shift) 393369aaab8SYu Zhao { 39450f11a8aSMike Rapoport void *ptr = (void *)__get_free_page(GFP_PGTABLE_KERNEL); 395369aaab8SYu Zhao BUG_ON(!ptr); 396369aaab8SYu Zhao 397369aaab8SYu Zhao /* Ensure the zeroed page is visible to the page table walker */ 398369aaab8SYu Zhao dsb(ishst); 399369aaab8SYu Zhao return __pa(ptr); 400369aaab8SYu Zhao } 401369aaab8SYu Zhao 40290292acaSYu Zhao static phys_addr_t pgd_pgtable_alloc(int shift) 403da141706SLaura Abbott { 404475ba3fcSWill Deacon phys_addr_t pa = __pgd_pgtable_alloc(shift); 40590292acaSYu Zhao 40690292acaSYu Zhao /* 40790292acaSYu Zhao * Call proper page table ctor in case later we need to 40890292acaSYu Zhao * call core mm functions like apply_to_page_range() on 40990292acaSYu Zhao * this pre-allocated page table. 41090292acaSYu Zhao * 41190292acaSYu Zhao * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is 41290292acaSYu Zhao * folded, and if so pgtable_pmd_page_ctor() becomes nop. 41390292acaSYu Zhao */ 41490292acaSYu Zhao if (shift == PAGE_SHIFT) 415b4ed71f5SMark Rutland BUG_ON(!pgtable_pte_page_ctor(phys_to_page(pa))); 41690292acaSYu Zhao else if (shift == PMD_SHIFT) 417475ba3fcSWill Deacon BUG_ON(!pgtable_pmd_page_ctor(phys_to_page(pa))); 41821ab99c2SMark Rutland 419475ba3fcSWill Deacon return pa; 420da141706SLaura Abbott } 421da141706SLaura Abbott 422132233a7SLaura Abbott /* 423132233a7SLaura Abbott * This function can only be used to modify existing table entries, 424132233a7SLaura Abbott * without allocating new levels of table. Note that this permits the 425132233a7SLaura Abbott * creation of new section or page entries. 426132233a7SLaura Abbott */ 427132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt, 428da141706SLaura Abbott phys_addr_t size, pgprot_t prot) 429d7ecbddfSMark Salter { 43077ad4ce6SMark Rutland if ((virt >= PAGE_END) && (virt < VMALLOC_START)) { 431d7ecbddfSMark Salter pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n", 432d7ecbddfSMark Salter &phys, virt); 433d7ecbddfSMark Salter return; 434d7ecbddfSMark Salter } 435d27cfa1fSArd Biesheuvel __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 436d27cfa1fSArd Biesheuvel NO_CONT_MAPPINGS); 437d7ecbddfSMark Salter } 438d7ecbddfSMark Salter 4398ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, 4408ce837ceSArd Biesheuvel unsigned long virt, phys_addr_t size, 441f14c66ceSArd Biesheuvel pgprot_t prot, bool page_mappings_only) 4428ce837ceSArd Biesheuvel { 443c0951366SArd Biesheuvel int flags = 0; 444c0951366SArd Biesheuvel 4451378dc3dSArd Biesheuvel BUG_ON(mm == &init_mm); 4461378dc3dSArd Biesheuvel 447c0951366SArd Biesheuvel if (page_mappings_only) 448d27cfa1fSArd Biesheuvel flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 449c0951366SArd Biesheuvel 45011509a30SMark Rutland __create_pgd_mapping(mm->pgd, phys, virt, size, prot, 451c0951366SArd Biesheuvel pgd_pgtable_alloc, flags); 452d7ecbddfSMark Salter } 453d7ecbddfSMark Salter 454aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt, 455da141706SLaura Abbott phys_addr_t size, pgprot_t prot) 456da141706SLaura Abbott { 45777ad4ce6SMark Rutland if ((virt >= PAGE_END) && (virt < VMALLOC_START)) { 458aa8c09beSArd Biesheuvel pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n", 459da141706SLaura Abbott &phys, virt); 460da141706SLaura Abbott return; 461da141706SLaura Abbott } 462da141706SLaura Abbott 463d27cfa1fSArd Biesheuvel __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 464d27cfa1fSArd Biesheuvel NO_CONT_MAPPINGS); 465aa8c09beSArd Biesheuvel 466aa8c09beSArd Biesheuvel /* flush the TLBs after updating live kernel mappings */ 467aa8c09beSArd Biesheuvel flush_tlb_kernel_range(virt, virt + size); 468da141706SLaura Abbott } 469da141706SLaura Abbott 47020a004e7SWill Deacon static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start, 47198d2e153STakahiro Akashi phys_addr_t end, pgprot_t prot, int flags) 472da141706SLaura Abbott { 47320a004e7SWill Deacon __create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start, 47498d2e153STakahiro Akashi prot, early_pgtable_alloc, flags); 475da141706SLaura Abbott } 476da141706SLaura Abbott 4775ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void) 4785ea5306cSArd Biesheuvel { 4795ea5306cSArd Biesheuvel /* 4805ea5306cSArd Biesheuvel * Remove the write permissions from the linear alias of .text/.rodata 4815ea5306cSArd Biesheuvel */ 482e2a073ddSArd Biesheuvel update_mapping_prot(__pa_symbol(_stext), (unsigned long)lm_alias(_stext), 483e2a073ddSArd Biesheuvel (unsigned long)__init_begin - (unsigned long)_stext, 4845ea5306cSArd Biesheuvel PAGE_KERNEL_RO); 4855ea5306cSArd Biesheuvel } 4865ea5306cSArd Biesheuvel 4872687275aSCatalin Marinas static bool crash_mem_map __initdata; 4882687275aSCatalin Marinas 4892687275aSCatalin Marinas static int __init enable_crash_mem_map(char *arg) 4902687275aSCatalin Marinas { 4912687275aSCatalin Marinas /* 4922687275aSCatalin Marinas * Proper parameter parsing is done by reserve_crashkernel(). We only 4932687275aSCatalin Marinas * need to know if the linear map has to avoid block mappings so that 4942687275aSCatalin Marinas * the crashkernel reservations can be unmapped later. 4952687275aSCatalin Marinas */ 4962687275aSCatalin Marinas crash_mem_map = true; 4972687275aSCatalin Marinas 4982687275aSCatalin Marinas return 0; 4992687275aSCatalin Marinas } 5002687275aSCatalin Marinas early_param("crashkernel", enable_crash_mem_map); 5012687275aSCatalin Marinas 50220a004e7SWill Deacon static void __init map_mem(pgd_t *pgdp) 503c1cc1552SCatalin Marinas { 50487143f40SArd Biesheuvel static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN); 505e2a073ddSArd Biesheuvel phys_addr_t kernel_start = __pa_symbol(_stext); 50698d2e153STakahiro Akashi phys_addr_t kernel_end = __pa_symbol(__init_begin); 507b10d6bcaSMike Rapoport phys_addr_t start, end; 50887143f40SArd Biesheuvel int flags = NO_EXEC_MAPPINGS; 509b10d6bcaSMike Rapoport u64 i; 51098d2e153STakahiro Akashi 51187143f40SArd Biesheuvel /* 51287143f40SArd Biesheuvel * Setting hierarchical PXNTable attributes on table entries covering 51387143f40SArd Biesheuvel * the linear region is only possible if it is guaranteed that no table 51487143f40SArd Biesheuvel * entries at any level are being shared between the linear region and 51587143f40SArd Biesheuvel * the vmalloc region. Check whether this is true for the PGD level, in 51687143f40SArd Biesheuvel * which case it is guaranteed to be true for all other levels as well. 51787143f40SArd Biesheuvel */ 51887143f40SArd Biesheuvel BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end)); 51987143f40SArd Biesheuvel 5206d47c23bSMike Rapoport if (can_set_direct_map() || crash_mem_map || IS_ENABLED(CONFIG_KFENCE)) 52187143f40SArd Biesheuvel flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 52298d2e153STakahiro Akashi 52398d2e153STakahiro Akashi /* 52498d2e153STakahiro Akashi * Take care not to create a writable alias for the 52598d2e153STakahiro Akashi * read-only text and rodata sections of the kernel image. 52698d2e153STakahiro Akashi * So temporarily mark them as NOMAP to skip mappings in 52798d2e153STakahiro Akashi * the following for-loop 52898d2e153STakahiro Akashi */ 52998d2e153STakahiro Akashi memblock_mark_nomap(kernel_start, kernel_end - kernel_start); 530f6bc87c3SSteve Capper 531c1cc1552SCatalin Marinas /* map all the memory banks */ 532b10d6bcaSMike Rapoport for_each_mem_range(i, &start, &end) { 533c1cc1552SCatalin Marinas if (start >= end) 534c1cc1552SCatalin Marinas break; 5350178dc76SCatalin Marinas /* 5360178dc76SCatalin Marinas * The linear map must allow allocation tags reading/writing 5370178dc76SCatalin Marinas * if MTE is present. Otherwise, it has the same attributes as 5380178dc76SCatalin Marinas * PAGE_KERNEL. 5390178dc76SCatalin Marinas */ 540d15dfd31SCatalin Marinas __map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL), 541d15dfd31SCatalin Marinas flags); 542c1cc1552SCatalin Marinas } 54398d2e153STakahiro Akashi 54498d2e153STakahiro Akashi /* 545e2a073ddSArd Biesheuvel * Map the linear alias of the [_stext, __init_begin) interval 54698d2e153STakahiro Akashi * as non-executable now, and remove the write permission in 54798d2e153STakahiro Akashi * mark_linear_text_alias_ro() below (which will be called after 54898d2e153STakahiro Akashi * alternative patching has completed). This makes the contents 54998d2e153STakahiro Akashi * of the region accessible to subsystems such as hibernate, 55098d2e153STakahiro Akashi * but protects it from inadvertent modification or execution. 55198d2e153STakahiro Akashi * Note that contiguous mappings cannot be remapped in this way, 55298d2e153STakahiro Akashi * so we should avoid them here. 55398d2e153STakahiro Akashi */ 55420a004e7SWill Deacon __map_memblock(pgdp, kernel_start, kernel_end, 55598d2e153STakahiro Akashi PAGE_KERNEL, NO_CONT_MAPPINGS); 55698d2e153STakahiro Akashi memblock_clear_nomap(kernel_start, kernel_end - kernel_start); 557c1cc1552SCatalin Marinas } 558c1cc1552SCatalin Marinas 559da141706SLaura Abbott void mark_rodata_ro(void) 560da141706SLaura Abbott { 5612f39b5f9SJeremy Linton unsigned long section_size; 562f9040773SArd Biesheuvel 5632f39b5f9SJeremy Linton /* 5649fdc14c5SArd Biesheuvel * mark .rodata as read only. Use __init_begin rather than __end_rodata 5659fdc14c5SArd Biesheuvel * to cover NOTES and EXCEPTION_TABLE. 5662f39b5f9SJeremy Linton */ 5679fdc14c5SArd Biesheuvel section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata; 568aa8c09beSArd Biesheuvel update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata, 5692f39b5f9SJeremy Linton section_size, PAGE_KERNEL_RO); 570e98216b5SArd Biesheuvel 5711404d6f1SLaura Abbott debug_checkwx(); 572da141706SLaura Abbott } 573da141706SLaura Abbott 57420a004e7SWill Deacon static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end, 575d27cfa1fSArd Biesheuvel pgprot_t prot, struct vm_struct *vma, 57692bbd16eSWill Deacon int flags, unsigned long vm_flags) 577068a17a5SMark Rutland { 5782077be67SLaura Abbott phys_addr_t pa_start = __pa_symbol(va_start); 579068a17a5SMark Rutland unsigned long size = va_end - va_start; 580068a17a5SMark Rutland 581068a17a5SMark Rutland BUG_ON(!PAGE_ALIGNED(pa_start)); 582068a17a5SMark Rutland BUG_ON(!PAGE_ALIGNED(size)); 583068a17a5SMark Rutland 58420a004e7SWill Deacon __create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot, 585d27cfa1fSArd Biesheuvel early_pgtable_alloc, flags); 586f9040773SArd Biesheuvel 58792bbd16eSWill Deacon if (!(vm_flags & VM_NO_GUARD)) 58892bbd16eSWill Deacon size += PAGE_SIZE; 58992bbd16eSWill Deacon 590f9040773SArd Biesheuvel vma->addr = va_start; 591f9040773SArd Biesheuvel vma->phys_addr = pa_start; 592f9040773SArd Biesheuvel vma->size = size; 59392bbd16eSWill Deacon vma->flags = VM_MAP | vm_flags; 594f9040773SArd Biesheuvel vma->caller = __builtin_return_address(0); 595f9040773SArd Biesheuvel 596f9040773SArd Biesheuvel vm_area_add_early(vma); 597068a17a5SMark Rutland } 598068a17a5SMark Rutland 59928b066daSArd Biesheuvel static int __init parse_rodata(char *arg) 60028b066daSArd Biesheuvel { 601c55191e9SArd Biesheuvel int ret = strtobool(arg, &rodata_enabled); 602c55191e9SArd Biesheuvel if (!ret) { 603c55191e9SArd Biesheuvel rodata_full = false; 604c55191e9SArd Biesheuvel return 0; 605c55191e9SArd Biesheuvel } 606c55191e9SArd Biesheuvel 607c55191e9SArd Biesheuvel /* permit 'full' in addition to boolean options */ 608c55191e9SArd Biesheuvel if (strcmp(arg, "full")) 609c55191e9SArd Biesheuvel return -EINVAL; 610c55191e9SArd Biesheuvel 611c55191e9SArd Biesheuvel rodata_enabled = true; 612c55191e9SArd Biesheuvel rodata_full = true; 613c55191e9SArd Biesheuvel return 0; 61428b066daSArd Biesheuvel } 61528b066daSArd Biesheuvel early_param("rodata", parse_rodata); 61628b066daSArd Biesheuvel 61751a0048bSWill Deacon #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 61851a0048bSWill Deacon static int __init map_entry_trampoline(void) 61951a0048bSWill Deacon { 620*a9c406e6SJames Morse int i; 621*a9c406e6SJames Morse 62251a0048bSWill Deacon pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; 62351a0048bSWill Deacon phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start); 62451a0048bSWill Deacon 62551a0048bSWill Deacon /* The trampoline is always mapped and can therefore be global */ 62651a0048bSWill Deacon pgprot_val(prot) &= ~PTE_NG; 62751a0048bSWill Deacon 62851a0048bSWill Deacon /* Map only the text into the trampoline page table */ 62951a0048bSWill Deacon memset(tramp_pg_dir, 0, PGD_SIZE); 630*a9c406e6SJames Morse __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, 631*a9c406e6SJames Morse entry_tramp_text_size(), prot, 632*a9c406e6SJames Morse __pgd_pgtable_alloc, NO_BLOCK_MAPPINGS); 63351a0048bSWill Deacon 6346c27c408SWill Deacon /* Map both the text and data into the kernel page table */ 635*a9c406e6SJames Morse for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++) 636*a9c406e6SJames Morse __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i, 637*a9c406e6SJames Morse pa_start + i * PAGE_SIZE, prot); 638*a9c406e6SJames Morse 6396c27c408SWill Deacon if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 6406c27c408SWill Deacon extern char __entry_tramp_data_start[]; 6416c27c408SWill Deacon 6426c27c408SWill Deacon __set_fixmap(FIX_ENTRY_TRAMP_DATA, 6436c27c408SWill Deacon __pa_symbol(__entry_tramp_data_start), 6446c27c408SWill Deacon PAGE_KERNEL_RO); 6456c27c408SWill Deacon } 6466c27c408SWill Deacon 64751a0048bSWill Deacon return 0; 64851a0048bSWill Deacon } 64951a0048bSWill Deacon core_initcall(map_entry_trampoline); 65051a0048bSWill Deacon #endif 65151a0048bSWill Deacon 652068a17a5SMark Rutland /* 653c8027285SMark Brown * Open coded check for BTI, only for use to determine configuration 654c8027285SMark Brown * for early mappings for before the cpufeature code has run. 655c8027285SMark Brown */ 656c8027285SMark Brown static bool arm64_early_this_cpu_has_bti(void) 657c8027285SMark Brown { 658c8027285SMark Brown u64 pfr1; 659c8027285SMark Brown 660c8027285SMark Brown if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)) 661c8027285SMark Brown return false; 662c8027285SMark Brown 66393ad55b7SMarc Zyngier pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1); 664c8027285SMark Brown return cpuid_feature_extract_unsigned_field(pfr1, 665c8027285SMark Brown ID_AA64PFR1_BT_SHIFT); 666c8027285SMark Brown } 667c8027285SMark Brown 668c8027285SMark Brown /* 669068a17a5SMark Rutland * Create fine-grained mappings for the kernel. 670068a17a5SMark Rutland */ 67120a004e7SWill Deacon static void __init map_kernel(pgd_t *pgdp) 672068a17a5SMark Rutland { 6732ebe088bSArd Biesheuvel static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext, 6742ebe088bSArd Biesheuvel vmlinux_initdata, vmlinux_data; 675068a17a5SMark Rutland 67628b066daSArd Biesheuvel /* 67728b066daSArd Biesheuvel * External debuggers may need to write directly to the text 67828b066daSArd Biesheuvel * mapping to install SW breakpoints. Allow this (only) when 67928b066daSArd Biesheuvel * explicitly requested with rodata=off. 68028b066daSArd Biesheuvel */ 68128b066daSArd Biesheuvel pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; 68228b066daSArd Biesheuvel 683d27cfa1fSArd Biesheuvel /* 684c8027285SMark Brown * If we have a CPU that supports BTI and a kernel built for 685c8027285SMark Brown * BTI then mark the kernel executable text as guarded pages 686c8027285SMark Brown * now so we don't have to rewrite the page tables later. 687c8027285SMark Brown */ 688c8027285SMark Brown if (arm64_early_this_cpu_has_bti()) 689c8027285SMark Brown text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP); 690c8027285SMark Brown 691c8027285SMark Brown /* 692d27cfa1fSArd Biesheuvel * Only rodata will be remapped with different permissions later on, 693d27cfa1fSArd Biesheuvel * all other segments are allowed to use contiguous mappings. 694d27cfa1fSArd Biesheuvel */ 695e2a073ddSArd Biesheuvel map_kernel_segment(pgdp, _stext, _etext, text_prot, &vmlinux_text, 0, 69692bbd16eSWill Deacon VM_NO_GUARD); 69720a004e7SWill Deacon map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL, 69892bbd16eSWill Deacon &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD); 69920a004e7SWill Deacon map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot, 70092bbd16eSWill Deacon &vmlinux_inittext, 0, VM_NO_GUARD); 70120a004e7SWill Deacon map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL, 70292bbd16eSWill Deacon &vmlinux_initdata, 0, VM_NO_GUARD); 70320a004e7SWill Deacon map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0); 704068a17a5SMark Rutland 705974b9b2cSMike Rapoport if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) { 706068a17a5SMark Rutland /* 707f9040773SArd Biesheuvel * The fixmap falls in a separate pgd to the kernel, and doesn't 708f9040773SArd Biesheuvel * live in the carveout for the swapper_pg_dir. We can simply 709f9040773SArd Biesheuvel * re-use the existing dir for the fixmap. 710068a17a5SMark Rutland */ 711974b9b2cSMike Rapoport set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START), 71220a004e7SWill Deacon READ_ONCE(*pgd_offset_k(FIXADDR_START))); 713f9040773SArd Biesheuvel } else if (CONFIG_PGTABLE_LEVELS > 3) { 714b333b0baSMark Rutland pgd_t *bm_pgdp; 715e9f63768SMike Rapoport p4d_t *bm_p4dp; 716b333b0baSMark Rutland pud_t *bm_pudp; 717f9040773SArd Biesheuvel /* 718f9040773SArd Biesheuvel * The fixmap shares its top level pgd entry with the kernel 719f9040773SArd Biesheuvel * mapping. This can really only occur when we are running 720f9040773SArd Biesheuvel * with 16k/4 levels, so we can simply reuse the pud level 721f9040773SArd Biesheuvel * entry instead. 722f9040773SArd Biesheuvel */ 723f9040773SArd Biesheuvel BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES)); 724974b9b2cSMike Rapoport bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START); 725e9f63768SMike Rapoport bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START); 726e9f63768SMike Rapoport bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START); 727b333b0baSMark Rutland pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd)); 728f9040773SArd Biesheuvel pud_clear_fixmap(); 729f9040773SArd Biesheuvel } else { 730f9040773SArd Biesheuvel BUG(); 731f9040773SArd Biesheuvel } 732068a17a5SMark Rutland 73320a004e7SWill Deacon kasan_copy_shadow(pgdp); 734068a17a5SMark Rutland } 735068a17a5SMark Rutland 736c1cc1552SCatalin Marinas void __init paging_init(void) 737c1cc1552SCatalin Marinas { 7382330b7caSJun Yao pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir)); 739068a17a5SMark Rutland 74020a004e7SWill Deacon map_kernel(pgdp); 74120a004e7SWill Deacon map_mem(pgdp); 742068a17a5SMark Rutland 743068a17a5SMark Rutland pgd_clear_fixmap(); 744068a17a5SMark Rutland 745068a17a5SMark Rutland cpu_replace_ttbr1(lm_alias(swapper_pg_dir)); 7462b5548b6SJun Yao init_mm.pgd = swapper_pg_dir; 747068a17a5SMark Rutland 7483ecc6834SMike Rapoport memblock_phys_free(__pa_symbol(init_pg_dir), 7492b5548b6SJun Yao __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir)); 75024cc61d8SArd Biesheuvel 75124cc61d8SArd Biesheuvel memblock_allow_resize(); 752c1cc1552SCatalin Marinas } 753c1cc1552SCatalin Marinas 754c1cc1552SCatalin Marinas /* 755c1cc1552SCatalin Marinas * Check whether a kernel address is valid (derived from arch/x86/). 756c1cc1552SCatalin Marinas */ 757c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr) 758c1cc1552SCatalin Marinas { 75920a004e7SWill Deacon pgd_t *pgdp; 760e9f63768SMike Rapoport p4d_t *p4dp; 76120a004e7SWill Deacon pud_t *pudp, pud; 76220a004e7SWill Deacon pmd_t *pmdp, pmd; 76320a004e7SWill Deacon pte_t *ptep, pte; 764c1cc1552SCatalin Marinas 7658dd4daa0SShyam Thombre addr = arch_kasan_reset_tag(addr); 766c1cc1552SCatalin Marinas if ((((long)addr) >> VA_BITS) != -1UL) 767c1cc1552SCatalin Marinas return 0; 768c1cc1552SCatalin Marinas 76920a004e7SWill Deacon pgdp = pgd_offset_k(addr); 77020a004e7SWill Deacon if (pgd_none(READ_ONCE(*pgdp))) 771c1cc1552SCatalin Marinas return 0; 772c1cc1552SCatalin Marinas 773e9f63768SMike Rapoport p4dp = p4d_offset(pgdp, addr); 774e9f63768SMike Rapoport if (p4d_none(READ_ONCE(*p4dp))) 775e9f63768SMike Rapoport return 0; 776e9f63768SMike Rapoport 777e9f63768SMike Rapoport pudp = pud_offset(p4dp, addr); 77820a004e7SWill Deacon pud = READ_ONCE(*pudp); 77920a004e7SWill Deacon if (pud_none(pud)) 780c1cc1552SCatalin Marinas return 0; 781c1cc1552SCatalin Marinas 78220a004e7SWill Deacon if (pud_sect(pud)) 78320a004e7SWill Deacon return pfn_valid(pud_pfn(pud)); 784206a2a73SSteve Capper 78520a004e7SWill Deacon pmdp = pmd_offset(pudp, addr); 78620a004e7SWill Deacon pmd = READ_ONCE(*pmdp); 78720a004e7SWill Deacon if (pmd_none(pmd)) 788c1cc1552SCatalin Marinas return 0; 789c1cc1552SCatalin Marinas 79020a004e7SWill Deacon if (pmd_sect(pmd)) 79120a004e7SWill Deacon return pfn_valid(pmd_pfn(pmd)); 792da6e4cb6SDave Anderson 79320a004e7SWill Deacon ptep = pte_offset_kernel(pmdp, addr); 79420a004e7SWill Deacon pte = READ_ONCE(*ptep); 79520a004e7SWill Deacon if (pte_none(pte)) 796c1cc1552SCatalin Marinas return 0; 797c1cc1552SCatalin Marinas 79820a004e7SWill Deacon return pfn_valid(pte_pfn(pte)); 799c1cc1552SCatalin Marinas } 800bbd6ec60SAnshuman Khandual 801bbd6ec60SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG 802eee07935SAnshuman Khandual static void free_hotplug_page_range(struct page *page, size_t size, 803eee07935SAnshuman Khandual struct vmem_altmap *altmap) 804bbd6ec60SAnshuman Khandual { 805eee07935SAnshuman Khandual if (altmap) { 806eee07935SAnshuman Khandual vmem_altmap_free(altmap, size >> PAGE_SHIFT); 807eee07935SAnshuman Khandual } else { 808bbd6ec60SAnshuman Khandual WARN_ON(PageReserved(page)); 809bbd6ec60SAnshuman Khandual free_pages((unsigned long)page_address(page), get_order(size)); 810bbd6ec60SAnshuman Khandual } 811eee07935SAnshuman Khandual } 812bbd6ec60SAnshuman Khandual 813bbd6ec60SAnshuman Khandual static void free_hotplug_pgtable_page(struct page *page) 814bbd6ec60SAnshuman Khandual { 815eee07935SAnshuman Khandual free_hotplug_page_range(page, PAGE_SIZE, NULL); 816bbd6ec60SAnshuman Khandual } 817bbd6ec60SAnshuman Khandual 818bbd6ec60SAnshuman Khandual static bool pgtable_range_aligned(unsigned long start, unsigned long end, 819bbd6ec60SAnshuman Khandual unsigned long floor, unsigned long ceiling, 820bbd6ec60SAnshuman Khandual unsigned long mask) 821bbd6ec60SAnshuman Khandual { 822bbd6ec60SAnshuman Khandual start &= mask; 823bbd6ec60SAnshuman Khandual if (start < floor) 824bbd6ec60SAnshuman Khandual return false; 825bbd6ec60SAnshuman Khandual 826bbd6ec60SAnshuman Khandual if (ceiling) { 827bbd6ec60SAnshuman Khandual ceiling &= mask; 828bbd6ec60SAnshuman Khandual if (!ceiling) 829bbd6ec60SAnshuman Khandual return false; 830bbd6ec60SAnshuman Khandual } 831bbd6ec60SAnshuman Khandual 832bbd6ec60SAnshuman Khandual if (end - 1 > ceiling - 1) 833bbd6ec60SAnshuman Khandual return false; 834bbd6ec60SAnshuman Khandual return true; 835bbd6ec60SAnshuman Khandual } 836bbd6ec60SAnshuman Khandual 837bbd6ec60SAnshuman Khandual static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, 838eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 839eee07935SAnshuman Khandual struct vmem_altmap *altmap) 840bbd6ec60SAnshuman Khandual { 841bbd6ec60SAnshuman Khandual pte_t *ptep, pte; 842bbd6ec60SAnshuman Khandual 843bbd6ec60SAnshuman Khandual do { 844bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, addr); 845bbd6ec60SAnshuman Khandual pte = READ_ONCE(*ptep); 846bbd6ec60SAnshuman Khandual if (pte_none(pte)) 847bbd6ec60SAnshuman Khandual continue; 848bbd6ec60SAnshuman Khandual 849bbd6ec60SAnshuman Khandual WARN_ON(!pte_present(pte)); 850bbd6ec60SAnshuman Khandual pte_clear(&init_mm, addr, ptep); 851bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 852bbd6ec60SAnshuman Khandual if (free_mapped) 853eee07935SAnshuman Khandual free_hotplug_page_range(pte_page(pte), 854eee07935SAnshuman Khandual PAGE_SIZE, altmap); 855bbd6ec60SAnshuman Khandual } while (addr += PAGE_SIZE, addr < end); 856bbd6ec60SAnshuman Khandual } 857bbd6ec60SAnshuman Khandual 858bbd6ec60SAnshuman Khandual static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr, 859eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 860eee07935SAnshuman Khandual struct vmem_altmap *altmap) 861bbd6ec60SAnshuman Khandual { 862bbd6ec60SAnshuman Khandual unsigned long next; 863bbd6ec60SAnshuman Khandual pmd_t *pmdp, pmd; 864bbd6ec60SAnshuman Khandual 865bbd6ec60SAnshuman Khandual do { 866bbd6ec60SAnshuman Khandual next = pmd_addr_end(addr, end); 867bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, addr); 868bbd6ec60SAnshuman Khandual pmd = READ_ONCE(*pmdp); 869bbd6ec60SAnshuman Khandual if (pmd_none(pmd)) 870bbd6ec60SAnshuman Khandual continue; 871bbd6ec60SAnshuman Khandual 872bbd6ec60SAnshuman Khandual WARN_ON(!pmd_present(pmd)); 873bbd6ec60SAnshuman Khandual if (pmd_sect(pmd)) { 874bbd6ec60SAnshuman Khandual pmd_clear(pmdp); 875bbd6ec60SAnshuman Khandual 876bbd6ec60SAnshuman Khandual /* 877bbd6ec60SAnshuman Khandual * One TLBI should be sufficient here as the PMD_SIZE 878bbd6ec60SAnshuman Khandual * range is mapped with a single block entry. 879bbd6ec60SAnshuman Khandual */ 880bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 881bbd6ec60SAnshuman Khandual if (free_mapped) 882bbd6ec60SAnshuman Khandual free_hotplug_page_range(pmd_page(pmd), 883eee07935SAnshuman Khandual PMD_SIZE, altmap); 884bbd6ec60SAnshuman Khandual continue; 885bbd6ec60SAnshuman Khandual } 886bbd6ec60SAnshuman Khandual WARN_ON(!pmd_table(pmd)); 887eee07935SAnshuman Khandual unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap); 888bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 889bbd6ec60SAnshuman Khandual } 890bbd6ec60SAnshuman Khandual 891bbd6ec60SAnshuman Khandual static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr, 892eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 893eee07935SAnshuman Khandual struct vmem_altmap *altmap) 894bbd6ec60SAnshuman Khandual { 895bbd6ec60SAnshuman Khandual unsigned long next; 896bbd6ec60SAnshuman Khandual pud_t *pudp, pud; 897bbd6ec60SAnshuman Khandual 898bbd6ec60SAnshuman Khandual do { 899bbd6ec60SAnshuman Khandual next = pud_addr_end(addr, end); 900bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, addr); 901bbd6ec60SAnshuman Khandual pud = READ_ONCE(*pudp); 902bbd6ec60SAnshuman Khandual if (pud_none(pud)) 903bbd6ec60SAnshuman Khandual continue; 904bbd6ec60SAnshuman Khandual 905bbd6ec60SAnshuman Khandual WARN_ON(!pud_present(pud)); 906bbd6ec60SAnshuman Khandual if (pud_sect(pud)) { 907bbd6ec60SAnshuman Khandual pud_clear(pudp); 908bbd6ec60SAnshuman Khandual 909bbd6ec60SAnshuman Khandual /* 910bbd6ec60SAnshuman Khandual * One TLBI should be sufficient here as the PUD_SIZE 911bbd6ec60SAnshuman Khandual * range is mapped with a single block entry. 912bbd6ec60SAnshuman Khandual */ 913bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 914bbd6ec60SAnshuman Khandual if (free_mapped) 915bbd6ec60SAnshuman Khandual free_hotplug_page_range(pud_page(pud), 916eee07935SAnshuman Khandual PUD_SIZE, altmap); 917bbd6ec60SAnshuman Khandual continue; 918bbd6ec60SAnshuman Khandual } 919bbd6ec60SAnshuman Khandual WARN_ON(!pud_table(pud)); 920eee07935SAnshuman Khandual unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap); 921bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 922bbd6ec60SAnshuman Khandual } 923bbd6ec60SAnshuman Khandual 924bbd6ec60SAnshuman Khandual static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr, 925eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 926eee07935SAnshuman Khandual struct vmem_altmap *altmap) 927bbd6ec60SAnshuman Khandual { 928bbd6ec60SAnshuman Khandual unsigned long next; 929bbd6ec60SAnshuman Khandual p4d_t *p4dp, p4d; 930bbd6ec60SAnshuman Khandual 931bbd6ec60SAnshuman Khandual do { 932bbd6ec60SAnshuman Khandual next = p4d_addr_end(addr, end); 933bbd6ec60SAnshuman Khandual p4dp = p4d_offset(pgdp, addr); 934bbd6ec60SAnshuman Khandual p4d = READ_ONCE(*p4dp); 935bbd6ec60SAnshuman Khandual if (p4d_none(p4d)) 936bbd6ec60SAnshuman Khandual continue; 937bbd6ec60SAnshuman Khandual 938bbd6ec60SAnshuman Khandual WARN_ON(!p4d_present(p4d)); 939eee07935SAnshuman Khandual unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap); 940bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 941bbd6ec60SAnshuman Khandual } 942bbd6ec60SAnshuman Khandual 943bbd6ec60SAnshuman Khandual static void unmap_hotplug_range(unsigned long addr, unsigned long end, 944eee07935SAnshuman Khandual bool free_mapped, struct vmem_altmap *altmap) 945bbd6ec60SAnshuman Khandual { 946bbd6ec60SAnshuman Khandual unsigned long next; 947bbd6ec60SAnshuman Khandual pgd_t *pgdp, pgd; 948bbd6ec60SAnshuman Khandual 949eee07935SAnshuman Khandual /* 950eee07935SAnshuman Khandual * altmap can only be used as vmemmap mapping backing memory. 951eee07935SAnshuman Khandual * In case the backing memory itself is not being freed, then 952eee07935SAnshuman Khandual * altmap is irrelevant. Warn about this inconsistency when 953eee07935SAnshuman Khandual * encountered. 954eee07935SAnshuman Khandual */ 955eee07935SAnshuman Khandual WARN_ON(!free_mapped && altmap); 956eee07935SAnshuman Khandual 957bbd6ec60SAnshuman Khandual do { 958bbd6ec60SAnshuman Khandual next = pgd_addr_end(addr, end); 959bbd6ec60SAnshuman Khandual pgdp = pgd_offset_k(addr); 960bbd6ec60SAnshuman Khandual pgd = READ_ONCE(*pgdp); 961bbd6ec60SAnshuman Khandual if (pgd_none(pgd)) 962bbd6ec60SAnshuman Khandual continue; 963bbd6ec60SAnshuman Khandual 964bbd6ec60SAnshuman Khandual WARN_ON(!pgd_present(pgd)); 965eee07935SAnshuman Khandual unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap); 966bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 967bbd6ec60SAnshuman Khandual } 968bbd6ec60SAnshuman Khandual 969bbd6ec60SAnshuman Khandual static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr, 970bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 971bbd6ec60SAnshuman Khandual unsigned long ceiling) 972bbd6ec60SAnshuman Khandual { 973bbd6ec60SAnshuman Khandual pte_t *ptep, pte; 974bbd6ec60SAnshuman Khandual unsigned long i, start = addr; 975bbd6ec60SAnshuman Khandual 976bbd6ec60SAnshuman Khandual do { 977bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, addr); 978bbd6ec60SAnshuman Khandual pte = READ_ONCE(*ptep); 979bbd6ec60SAnshuman Khandual 980bbd6ec60SAnshuman Khandual /* 981bbd6ec60SAnshuman Khandual * This is just a sanity check here which verifies that 982bbd6ec60SAnshuman Khandual * pte clearing has been done by earlier unmap loops. 983bbd6ec60SAnshuman Khandual */ 984bbd6ec60SAnshuman Khandual WARN_ON(!pte_none(pte)); 985bbd6ec60SAnshuman Khandual } while (addr += PAGE_SIZE, addr < end); 986bbd6ec60SAnshuman Khandual 987bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK)) 988bbd6ec60SAnshuman Khandual return; 989bbd6ec60SAnshuman Khandual 990bbd6ec60SAnshuman Khandual /* 991bbd6ec60SAnshuman Khandual * Check whether we can free the pte page if the rest of the 992bbd6ec60SAnshuman Khandual * entries are empty. Overlap with other regions have been 993bbd6ec60SAnshuman Khandual * handled by the floor/ceiling check. 994bbd6ec60SAnshuman Khandual */ 995bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, 0UL); 996bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PTE; i++) { 997bbd6ec60SAnshuman Khandual if (!pte_none(READ_ONCE(ptep[i]))) 998bbd6ec60SAnshuman Khandual return; 999bbd6ec60SAnshuman Khandual } 1000bbd6ec60SAnshuman Khandual 1001bbd6ec60SAnshuman Khandual pmd_clear(pmdp); 1002bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1003bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(ptep)); 1004bbd6ec60SAnshuman Khandual } 1005bbd6ec60SAnshuman Khandual 1006bbd6ec60SAnshuman Khandual static void free_empty_pmd_table(pud_t *pudp, unsigned long addr, 1007bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1008bbd6ec60SAnshuman Khandual unsigned long ceiling) 1009bbd6ec60SAnshuman Khandual { 1010bbd6ec60SAnshuman Khandual pmd_t *pmdp, pmd; 1011bbd6ec60SAnshuman Khandual unsigned long i, next, start = addr; 1012bbd6ec60SAnshuman Khandual 1013bbd6ec60SAnshuman Khandual do { 1014bbd6ec60SAnshuman Khandual next = pmd_addr_end(addr, end); 1015bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, addr); 1016bbd6ec60SAnshuman Khandual pmd = READ_ONCE(*pmdp); 1017bbd6ec60SAnshuman Khandual if (pmd_none(pmd)) 1018bbd6ec60SAnshuman Khandual continue; 1019bbd6ec60SAnshuman Khandual 1020bbd6ec60SAnshuman Khandual WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd)); 1021bbd6ec60SAnshuman Khandual free_empty_pte_table(pmdp, addr, next, floor, ceiling); 1022bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1023bbd6ec60SAnshuman Khandual 1024bbd6ec60SAnshuman Khandual if (CONFIG_PGTABLE_LEVELS <= 2) 1025bbd6ec60SAnshuman Khandual return; 1026bbd6ec60SAnshuman Khandual 1027bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK)) 1028bbd6ec60SAnshuman Khandual return; 1029bbd6ec60SAnshuman Khandual 1030bbd6ec60SAnshuman Khandual /* 1031bbd6ec60SAnshuman Khandual * Check whether we can free the pmd page if the rest of the 1032bbd6ec60SAnshuman Khandual * entries are empty. Overlap with other regions have been 1033bbd6ec60SAnshuman Khandual * handled by the floor/ceiling check. 1034bbd6ec60SAnshuman Khandual */ 1035bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, 0UL); 1036bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PMD; i++) { 1037bbd6ec60SAnshuman Khandual if (!pmd_none(READ_ONCE(pmdp[i]))) 1038bbd6ec60SAnshuman Khandual return; 1039bbd6ec60SAnshuman Khandual } 1040bbd6ec60SAnshuman Khandual 1041bbd6ec60SAnshuman Khandual pud_clear(pudp); 1042bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1043bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(pmdp)); 1044bbd6ec60SAnshuman Khandual } 1045bbd6ec60SAnshuman Khandual 1046bbd6ec60SAnshuman Khandual static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr, 1047bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1048bbd6ec60SAnshuman Khandual unsigned long ceiling) 1049bbd6ec60SAnshuman Khandual { 1050bbd6ec60SAnshuman Khandual pud_t *pudp, pud; 1051bbd6ec60SAnshuman Khandual unsigned long i, next, start = addr; 1052bbd6ec60SAnshuman Khandual 1053bbd6ec60SAnshuman Khandual do { 1054bbd6ec60SAnshuman Khandual next = pud_addr_end(addr, end); 1055bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, addr); 1056bbd6ec60SAnshuman Khandual pud = READ_ONCE(*pudp); 1057bbd6ec60SAnshuman Khandual if (pud_none(pud)) 1058bbd6ec60SAnshuman Khandual continue; 1059bbd6ec60SAnshuman Khandual 1060bbd6ec60SAnshuman Khandual WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud)); 1061bbd6ec60SAnshuman Khandual free_empty_pmd_table(pudp, addr, next, floor, ceiling); 1062bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1063bbd6ec60SAnshuman Khandual 1064bbd6ec60SAnshuman Khandual if (CONFIG_PGTABLE_LEVELS <= 3) 1065bbd6ec60SAnshuman Khandual return; 1066bbd6ec60SAnshuman Khandual 1067bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK)) 1068bbd6ec60SAnshuman Khandual return; 1069bbd6ec60SAnshuman Khandual 1070bbd6ec60SAnshuman Khandual /* 1071bbd6ec60SAnshuman Khandual * Check whether we can free the pud page if the rest of the 1072bbd6ec60SAnshuman Khandual * entries are empty. Overlap with other regions have been 1073bbd6ec60SAnshuman Khandual * handled by the floor/ceiling check. 1074bbd6ec60SAnshuman Khandual */ 1075bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, 0UL); 1076bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PUD; i++) { 1077bbd6ec60SAnshuman Khandual if (!pud_none(READ_ONCE(pudp[i]))) 1078bbd6ec60SAnshuman Khandual return; 1079bbd6ec60SAnshuman Khandual } 1080bbd6ec60SAnshuman Khandual 1081bbd6ec60SAnshuman Khandual p4d_clear(p4dp); 1082bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1083bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(pudp)); 1084bbd6ec60SAnshuman Khandual } 1085bbd6ec60SAnshuman Khandual 1086bbd6ec60SAnshuman Khandual static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr, 1087bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1088bbd6ec60SAnshuman Khandual unsigned long ceiling) 1089bbd6ec60SAnshuman Khandual { 1090bbd6ec60SAnshuman Khandual unsigned long next; 1091bbd6ec60SAnshuman Khandual p4d_t *p4dp, p4d; 1092bbd6ec60SAnshuman Khandual 1093bbd6ec60SAnshuman Khandual do { 1094bbd6ec60SAnshuman Khandual next = p4d_addr_end(addr, end); 1095bbd6ec60SAnshuman Khandual p4dp = p4d_offset(pgdp, addr); 1096bbd6ec60SAnshuman Khandual p4d = READ_ONCE(*p4dp); 1097bbd6ec60SAnshuman Khandual if (p4d_none(p4d)) 1098bbd6ec60SAnshuman Khandual continue; 1099bbd6ec60SAnshuman Khandual 1100bbd6ec60SAnshuman Khandual WARN_ON(!p4d_present(p4d)); 1101bbd6ec60SAnshuman Khandual free_empty_pud_table(p4dp, addr, next, floor, ceiling); 1102bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1103bbd6ec60SAnshuman Khandual } 1104bbd6ec60SAnshuman Khandual 1105bbd6ec60SAnshuman Khandual static void free_empty_tables(unsigned long addr, unsigned long end, 1106bbd6ec60SAnshuman Khandual unsigned long floor, unsigned long ceiling) 1107bbd6ec60SAnshuman Khandual { 1108bbd6ec60SAnshuman Khandual unsigned long next; 1109bbd6ec60SAnshuman Khandual pgd_t *pgdp, pgd; 1110bbd6ec60SAnshuman Khandual 1111bbd6ec60SAnshuman Khandual do { 1112bbd6ec60SAnshuman Khandual next = pgd_addr_end(addr, end); 1113bbd6ec60SAnshuman Khandual pgdp = pgd_offset_k(addr); 1114bbd6ec60SAnshuman Khandual pgd = READ_ONCE(*pgdp); 1115bbd6ec60SAnshuman Khandual if (pgd_none(pgd)) 1116bbd6ec60SAnshuman Khandual continue; 1117bbd6ec60SAnshuman Khandual 1118bbd6ec60SAnshuman Khandual WARN_ON(!pgd_present(pgd)); 1119bbd6ec60SAnshuman Khandual free_empty_p4d_table(pgdp, addr, next, floor, ceiling); 1120bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1121bbd6ec60SAnshuman Khandual } 1122bbd6ec60SAnshuman Khandual #endif 1123bbd6ec60SAnshuman Khandual 11242062d44dSAnshuman Khandual #if !ARM64_KERNEL_USES_PMD_MAPS 11257b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 11267b73d978SChristoph Hellwig struct vmem_altmap *altmap) 1127c1cc1552SCatalin Marinas { 1128edb739eeSAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1129eee07935SAnshuman Khandual return vmemmap_populate_basepages(start, end, node, altmap); 1130c1cc1552SCatalin Marinas } 11312062d44dSAnshuman Khandual #else /* !ARM64_KERNEL_USES_PMD_MAPS */ 11327b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 11337b73d978SChristoph Hellwig struct vmem_altmap *altmap) 1134c1cc1552SCatalin Marinas { 11350aad818bSJohannes Weiner unsigned long addr = start; 1136c1cc1552SCatalin Marinas unsigned long next; 113720a004e7SWill Deacon pgd_t *pgdp; 1138e9f63768SMike Rapoport p4d_t *p4dp; 113920a004e7SWill Deacon pud_t *pudp; 114020a004e7SWill Deacon pmd_t *pmdp; 1141c1cc1552SCatalin Marinas 1142edb739eeSAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1143c1cc1552SCatalin Marinas do { 1144c1cc1552SCatalin Marinas next = pmd_addr_end(addr, end); 1145c1cc1552SCatalin Marinas 114620a004e7SWill Deacon pgdp = vmemmap_pgd_populate(addr, node); 114720a004e7SWill Deacon if (!pgdp) 1148c1cc1552SCatalin Marinas return -ENOMEM; 1149c1cc1552SCatalin Marinas 1150e9f63768SMike Rapoport p4dp = vmemmap_p4d_populate(pgdp, addr, node); 1151e9f63768SMike Rapoport if (!p4dp) 1152e9f63768SMike Rapoport return -ENOMEM; 1153e9f63768SMike Rapoport 1154e9f63768SMike Rapoport pudp = vmemmap_pud_populate(p4dp, addr, node); 115520a004e7SWill Deacon if (!pudp) 1156c1cc1552SCatalin Marinas return -ENOMEM; 1157c1cc1552SCatalin Marinas 115820a004e7SWill Deacon pmdp = pmd_offset(pudp, addr); 115920a004e7SWill Deacon if (pmd_none(READ_ONCE(*pmdp))) { 1160c1cc1552SCatalin Marinas void *p = NULL; 1161c1cc1552SCatalin Marinas 1162eee07935SAnshuman Khandual p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap); 11639f84f39fSSudarshan Rajagopalan if (!p) { 11649f84f39fSSudarshan Rajagopalan if (vmemmap_populate_basepages(addr, next, node, altmap)) 1165c1cc1552SCatalin Marinas return -ENOMEM; 11669f84f39fSSudarshan Rajagopalan continue; 11679f84f39fSSudarshan Rajagopalan } 1168c1cc1552SCatalin Marinas 116920a004e7SWill Deacon pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL)); 1170c1cc1552SCatalin Marinas } else 117120a004e7SWill Deacon vmemmap_verify((pte_t *)pmdp, node, addr, next); 1172c1cc1552SCatalin Marinas } while (addr = next, addr != end); 1173c1cc1552SCatalin Marinas 1174c1cc1552SCatalin Marinas return 0; 1175c1cc1552SCatalin Marinas } 11762062d44dSAnshuman Khandual #endif /* !ARM64_KERNEL_USES_PMD_MAPS */ 117740221c73SAnshuman Khandual 117840221c73SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG 117924b6d416SChristoph Hellwig void vmemmap_free(unsigned long start, unsigned long end, 118024b6d416SChristoph Hellwig struct vmem_altmap *altmap) 11810197518cSTang Chen { 1182bbd6ec60SAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1183bbd6ec60SAnshuman Khandual 1184eee07935SAnshuman Khandual unmap_hotplug_range(start, end, true, altmap); 1185bbd6ec60SAnshuman Khandual free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END); 11860197518cSTang Chen } 118740221c73SAnshuman Khandual #endif /* CONFIG_MEMORY_HOTPLUG */ 1188af86e597SLaura Abbott 1189af86e597SLaura Abbott static inline pud_t *fixmap_pud(unsigned long addr) 1190af86e597SLaura Abbott { 119120a004e7SWill Deacon pgd_t *pgdp = pgd_offset_k(addr); 1192e9f63768SMike Rapoport p4d_t *p4dp = p4d_offset(pgdp, addr); 1193e9f63768SMike Rapoport p4d_t p4d = READ_ONCE(*p4dp); 1194af86e597SLaura Abbott 1195e9f63768SMike Rapoport BUG_ON(p4d_none(p4d) || p4d_bad(p4d)); 1196af86e597SLaura Abbott 1197e9f63768SMike Rapoport return pud_offset_kimg(p4dp, addr); 1198af86e597SLaura Abbott } 1199af86e597SLaura Abbott 1200af86e597SLaura Abbott static inline pmd_t *fixmap_pmd(unsigned long addr) 1201af86e597SLaura Abbott { 120220a004e7SWill Deacon pud_t *pudp = fixmap_pud(addr); 120320a004e7SWill Deacon pud_t pud = READ_ONCE(*pudp); 1204af86e597SLaura Abbott 120520a004e7SWill Deacon BUG_ON(pud_none(pud) || pud_bad(pud)); 1206af86e597SLaura Abbott 120720a004e7SWill Deacon return pmd_offset_kimg(pudp, addr); 1208af86e597SLaura Abbott } 1209af86e597SLaura Abbott 1210af86e597SLaura Abbott static inline pte_t *fixmap_pte(unsigned long addr) 1211af86e597SLaura Abbott { 1212157962f5SArd Biesheuvel return &bm_pte[pte_index(addr)]; 1213af86e597SLaura Abbott } 1214af86e597SLaura Abbott 12152077be67SLaura Abbott /* 12162077be67SLaura Abbott * The p*d_populate functions call virt_to_phys implicitly so they can't be used 12172077be67SLaura Abbott * directly on kernel symbols (bm_p*d). This function is called too early to use 12182077be67SLaura Abbott * lm_alias so __p*d_populate functions must be used to populate with the 12192077be67SLaura Abbott * physical address from __pa_symbol. 12202077be67SLaura Abbott */ 1221af86e597SLaura Abbott void __init early_fixmap_init(void) 1222af86e597SLaura Abbott { 1223e9f63768SMike Rapoport pgd_t *pgdp; 1224e9f63768SMike Rapoport p4d_t *p4dp, p4d; 122520a004e7SWill Deacon pud_t *pudp; 122620a004e7SWill Deacon pmd_t *pmdp; 1227af86e597SLaura Abbott unsigned long addr = FIXADDR_START; 1228af86e597SLaura Abbott 122920a004e7SWill Deacon pgdp = pgd_offset_k(addr); 1230e9f63768SMike Rapoport p4dp = p4d_offset(pgdp, addr); 1231e9f63768SMike Rapoport p4d = READ_ONCE(*p4dp); 1232f80fb3a3SArd Biesheuvel if (CONFIG_PGTABLE_LEVELS > 3 && 1233e9f63768SMike Rapoport !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) { 1234f9040773SArd Biesheuvel /* 1235f9040773SArd Biesheuvel * We only end up here if the kernel mapping and the fixmap 1236f9040773SArd Biesheuvel * share the top level pgd entry, which should only happen on 1237f9040773SArd Biesheuvel * 16k/4 levels configurations. 1238f9040773SArd Biesheuvel */ 1239f9040773SArd Biesheuvel BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES)); 1240e9f63768SMike Rapoport pudp = pud_offset_kimg(p4dp, addr); 1241f9040773SArd Biesheuvel } else { 1242e9f63768SMike Rapoport if (p4d_none(p4d)) 1243c1fd78a7SArd Biesheuvel __p4d_populate(p4dp, __pa_symbol(bm_pud), P4D_TYPE_TABLE); 124420a004e7SWill Deacon pudp = fixmap_pud(addr); 1245f9040773SArd Biesheuvel } 124620a004e7SWill Deacon if (pud_none(READ_ONCE(*pudp))) 1247c1fd78a7SArd Biesheuvel __pud_populate(pudp, __pa_symbol(bm_pmd), PUD_TYPE_TABLE); 124820a004e7SWill Deacon pmdp = fixmap_pmd(addr); 124920a004e7SWill Deacon __pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE); 1250af86e597SLaura Abbott 1251af86e597SLaura Abbott /* 1252af86e597SLaura Abbott * The boot-ioremap range spans multiple pmds, for which 1253157962f5SArd Biesheuvel * we are not prepared: 1254af86e597SLaura Abbott */ 1255af86e597SLaura Abbott BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) 1256af86e597SLaura Abbott != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); 1257af86e597SLaura Abbott 125820a004e7SWill Deacon if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN))) 125920a004e7SWill Deacon || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) { 1260af86e597SLaura Abbott WARN_ON(1); 126120a004e7SWill Deacon pr_warn("pmdp %p != %p, %p\n", 126220a004e7SWill Deacon pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)), 1263af86e597SLaura Abbott fixmap_pmd(fix_to_virt(FIX_BTMAP_END))); 1264af86e597SLaura Abbott pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", 1265af86e597SLaura Abbott fix_to_virt(FIX_BTMAP_BEGIN)); 1266af86e597SLaura Abbott pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n", 1267af86e597SLaura Abbott fix_to_virt(FIX_BTMAP_END)); 1268af86e597SLaura Abbott 1269af86e597SLaura Abbott pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END); 1270af86e597SLaura Abbott pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN); 1271af86e597SLaura Abbott } 1272af86e597SLaura Abbott } 1273af86e597SLaura Abbott 127418b4b276SJames Morse /* 127518b4b276SJames Morse * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we 127618b4b276SJames Morse * ever need to use IPIs for TLB broadcasting, then we're in trouble here. 127718b4b276SJames Morse */ 1278af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx, 1279af86e597SLaura Abbott phys_addr_t phys, pgprot_t flags) 1280af86e597SLaura Abbott { 1281af86e597SLaura Abbott unsigned long addr = __fix_to_virt(idx); 128220a004e7SWill Deacon pte_t *ptep; 1283af86e597SLaura Abbott 1284b63dbef9SMark Rutland BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); 1285af86e597SLaura Abbott 128620a004e7SWill Deacon ptep = fixmap_pte(addr); 1287af86e597SLaura Abbott 1288af86e597SLaura Abbott if (pgprot_val(flags)) { 128920a004e7SWill Deacon set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags)); 1290af86e597SLaura Abbott } else { 129120a004e7SWill Deacon pte_clear(&init_mm, addr, ptep); 1292af86e597SLaura Abbott flush_tlb_kernel_range(addr, addr+PAGE_SIZE); 1293af86e597SLaura Abbott } 1294af86e597SLaura Abbott } 129561bd93ceSArd Biesheuvel 1296e112b032SHsin-Yi Wang void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) 129761bd93ceSArd Biesheuvel { 129861bd93ceSArd Biesheuvel const u64 dt_virt_base = __fix_to_virt(FIX_FDT); 1299f80fb3a3SArd Biesheuvel int offset; 130061bd93ceSArd Biesheuvel void *dt_virt; 130161bd93ceSArd Biesheuvel 130261bd93ceSArd Biesheuvel /* 130361bd93ceSArd Biesheuvel * Check whether the physical FDT address is set and meets the minimum 130461bd93ceSArd Biesheuvel * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be 130504a84810SArd Biesheuvel * at least 8 bytes so that we can always access the magic and size 130604a84810SArd Biesheuvel * fields of the FDT header after mapping the first chunk, double check 130704a84810SArd Biesheuvel * here if that is indeed the case. 130861bd93ceSArd Biesheuvel */ 130961bd93ceSArd Biesheuvel BUILD_BUG_ON(MIN_FDT_ALIGN < 8); 131061bd93ceSArd Biesheuvel if (!dt_phys || dt_phys % MIN_FDT_ALIGN) 131161bd93ceSArd Biesheuvel return NULL; 131261bd93ceSArd Biesheuvel 131361bd93ceSArd Biesheuvel /* 131461bd93ceSArd Biesheuvel * Make sure that the FDT region can be mapped without the need to 131561bd93ceSArd Biesheuvel * allocate additional translation table pages, so that it is safe 1316132233a7SLaura Abbott * to call create_mapping_noalloc() this early. 131761bd93ceSArd Biesheuvel * 131861bd93ceSArd Biesheuvel * On 64k pages, the FDT will be mapped using PTEs, so we need to 131961bd93ceSArd Biesheuvel * be in the same PMD as the rest of the fixmap. 132061bd93ceSArd Biesheuvel * On 4k pages, we'll use section mappings for the FDT so we only 132161bd93ceSArd Biesheuvel * have to be in the same PUD. 132261bd93ceSArd Biesheuvel */ 132361bd93ceSArd Biesheuvel BUILD_BUG_ON(dt_virt_base % SZ_2M); 132461bd93ceSArd Biesheuvel 1325b433dce0SSuzuki K. Poulose BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT != 1326b433dce0SSuzuki K. Poulose __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT); 132761bd93ceSArd Biesheuvel 1328b433dce0SSuzuki K. Poulose offset = dt_phys % SWAPPER_BLOCK_SIZE; 132961bd93ceSArd Biesheuvel dt_virt = (void *)dt_virt_base + offset; 133061bd93ceSArd Biesheuvel 133161bd93ceSArd Biesheuvel /* map the first chunk so we can read the size from the header */ 1332132233a7SLaura Abbott create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), 1333132233a7SLaura Abbott dt_virt_base, SWAPPER_BLOCK_SIZE, prot); 133461bd93ceSArd Biesheuvel 133504a84810SArd Biesheuvel if (fdt_magic(dt_virt) != FDT_MAGIC) 133661bd93ceSArd Biesheuvel return NULL; 133761bd93ceSArd Biesheuvel 1338f80fb3a3SArd Biesheuvel *size = fdt_totalsize(dt_virt); 1339f80fb3a3SArd Biesheuvel if (*size > MAX_FDT_SIZE) 134061bd93ceSArd Biesheuvel return NULL; 134161bd93ceSArd Biesheuvel 1342f80fb3a3SArd Biesheuvel if (offset + *size > SWAPPER_BLOCK_SIZE) 1343132233a7SLaura Abbott create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base, 1344f80fb3a3SArd Biesheuvel round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot); 1345f80fb3a3SArd Biesheuvel 1346f80fb3a3SArd Biesheuvel return dt_virt; 1347f80fb3a3SArd Biesheuvel } 1348f80fb3a3SArd Biesheuvel 134920a004e7SWill Deacon int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot) 1350324420bfSArd Biesheuvel { 1351f7f0097aSAnshuman Khandual pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot)); 135215122ee2SWill Deacon 135382034c23SLaura Abbott /* Only allow permission changes for now */ 135482034c23SLaura Abbott if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)), 135582034c23SLaura Abbott pud_val(new_pud))) 135615122ee2SWill Deacon return 0; 135715122ee2SWill Deacon 135887dedf7cSAnshuman Khandual VM_BUG_ON(phys & ~PUD_MASK); 135982034c23SLaura Abbott set_pud(pudp, new_pud); 1360324420bfSArd Biesheuvel return 1; 1361324420bfSArd Biesheuvel } 1362324420bfSArd Biesheuvel 136320a004e7SWill Deacon int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot) 1364324420bfSArd Biesheuvel { 1365f7f0097aSAnshuman Khandual pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot)); 136615122ee2SWill Deacon 136782034c23SLaura Abbott /* Only allow permission changes for now */ 136882034c23SLaura Abbott if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)), 136982034c23SLaura Abbott pmd_val(new_pmd))) 137015122ee2SWill Deacon return 0; 137115122ee2SWill Deacon 137287dedf7cSAnshuman Khandual VM_BUG_ON(phys & ~PMD_MASK); 137382034c23SLaura Abbott set_pmd(pmdp, new_pmd); 1374324420bfSArd Biesheuvel return 1; 1375324420bfSArd Biesheuvel } 1376324420bfSArd Biesheuvel 1377d8a71905SJonathan Marek int pud_clear_huge(pud_t *pudp) 1378d8a71905SJonathan Marek { 1379d8a71905SJonathan Marek if (!pud_sect(READ_ONCE(*pudp))) 1380d8a71905SJonathan Marek return 0; 1381d8a71905SJonathan Marek pud_clear(pudp); 1382d8a71905SJonathan Marek return 1; 1383d8a71905SJonathan Marek } 1384d8a71905SJonathan Marek 138520a004e7SWill Deacon int pmd_clear_huge(pmd_t *pmdp) 1386324420bfSArd Biesheuvel { 138720a004e7SWill Deacon if (!pmd_sect(READ_ONCE(*pmdp))) 1388324420bfSArd Biesheuvel return 0; 138920a004e7SWill Deacon pmd_clear(pmdp); 1390324420bfSArd Biesheuvel return 1; 1391324420bfSArd Biesheuvel } 1392b6bdb751SToshi Kani 1393ec28bb9cSChintan Pandya int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr) 1394b6bdb751SToshi Kani { 1395ec28bb9cSChintan Pandya pte_t *table; 1396ec28bb9cSChintan Pandya pmd_t pmd; 1397ec28bb9cSChintan Pandya 1398ec28bb9cSChintan Pandya pmd = READ_ONCE(*pmdp); 1399ec28bb9cSChintan Pandya 1400fac880c7SMark Rutland if (!pmd_table(pmd)) { 14019c006972SWill Deacon VM_WARN_ON(1); 1402ec28bb9cSChintan Pandya return 1; 1403b6bdb751SToshi Kani } 1404b6bdb751SToshi Kani 1405ec28bb9cSChintan Pandya table = pte_offset_kernel(pmdp, addr); 1406ec28bb9cSChintan Pandya pmd_clear(pmdp); 1407ec28bb9cSChintan Pandya __flush_tlb_kernel_pgtable(addr); 1408ec28bb9cSChintan Pandya pte_free_kernel(NULL, table); 1409ec28bb9cSChintan Pandya return 1; 1410ec28bb9cSChintan Pandya } 1411ec28bb9cSChintan Pandya 1412ec28bb9cSChintan Pandya int pud_free_pmd_page(pud_t *pudp, unsigned long addr) 1413b6bdb751SToshi Kani { 1414ec28bb9cSChintan Pandya pmd_t *table; 1415ec28bb9cSChintan Pandya pmd_t *pmdp; 1416ec28bb9cSChintan Pandya pud_t pud; 1417ec28bb9cSChintan Pandya unsigned long next, end; 1418ec28bb9cSChintan Pandya 1419ec28bb9cSChintan Pandya pud = READ_ONCE(*pudp); 1420ec28bb9cSChintan Pandya 1421fac880c7SMark Rutland if (!pud_table(pud)) { 14229c006972SWill Deacon VM_WARN_ON(1); 1423ec28bb9cSChintan Pandya return 1; 1424ec28bb9cSChintan Pandya } 1425ec28bb9cSChintan Pandya 1426ec28bb9cSChintan Pandya table = pmd_offset(pudp, addr); 1427ec28bb9cSChintan Pandya pmdp = table; 1428ec28bb9cSChintan Pandya next = addr; 1429ec28bb9cSChintan Pandya end = addr + PUD_SIZE; 1430ec28bb9cSChintan Pandya do { 1431ec28bb9cSChintan Pandya pmd_free_pte_page(pmdp, next); 1432ec28bb9cSChintan Pandya } while (pmdp++, next += PMD_SIZE, next != end); 1433ec28bb9cSChintan Pandya 1434ec28bb9cSChintan Pandya pud_clear(pudp); 1435ec28bb9cSChintan Pandya __flush_tlb_kernel_pgtable(addr); 1436ec28bb9cSChintan Pandya pmd_free(NULL, table); 1437ec28bb9cSChintan Pandya return 1; 1438b6bdb751SToshi Kani } 14394ab21506SRobin Murphy 14404ab21506SRobin Murphy #ifdef CONFIG_MEMORY_HOTPLUG 1441bbd6ec60SAnshuman Khandual static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size) 1442bbd6ec60SAnshuman Khandual { 1443bbd6ec60SAnshuman Khandual unsigned long end = start + size; 1444bbd6ec60SAnshuman Khandual 1445bbd6ec60SAnshuman Khandual WARN_ON(pgdir != init_mm.pgd); 1446bbd6ec60SAnshuman Khandual WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END)); 1447bbd6ec60SAnshuman Khandual 1448eee07935SAnshuman Khandual unmap_hotplug_range(start, end, false, NULL); 1449bbd6ec60SAnshuman Khandual free_empty_tables(start, end, PAGE_OFFSET, PAGE_END); 1450bbd6ec60SAnshuman Khandual } 1451bbd6ec60SAnshuman Khandual 145203aaf83fSAnshuman Khandual struct range arch_get_mappable_range(void) 145358284a90SAnshuman Khandual { 145403aaf83fSAnshuman Khandual struct range mhp_range; 1455ee7febceSPavel Tatashin u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual)); 1456ee7febceSPavel Tatashin u64 end_linear_pa = __pa(PAGE_END - 1); 1457ee7febceSPavel Tatashin 1458ee7febceSPavel Tatashin if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 1459ee7febceSPavel Tatashin /* 1460ee7febceSPavel Tatashin * Check for a wrap, it is possible because of randomized linear 1461ee7febceSPavel Tatashin * mapping the start physical address is actually bigger than 1462ee7febceSPavel Tatashin * the end physical address. In this case set start to zero 1463ee7febceSPavel Tatashin * because [0, end_linear_pa] range must still be able to cover 1464ee7febceSPavel Tatashin * all addressable physical addresses. 1465ee7febceSPavel Tatashin */ 1466ee7febceSPavel Tatashin if (start_linear_pa > end_linear_pa) 1467ee7febceSPavel Tatashin start_linear_pa = 0; 1468ee7febceSPavel Tatashin } 1469ee7febceSPavel Tatashin 1470ee7febceSPavel Tatashin WARN_ON(start_linear_pa > end_linear_pa); 147103aaf83fSAnshuman Khandual 147258284a90SAnshuman Khandual /* 147358284a90SAnshuman Khandual * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)] 147458284a90SAnshuman Khandual * accommodating both its ends but excluding PAGE_END. Max physical 147558284a90SAnshuman Khandual * range which can be mapped inside this linear mapping range, must 147658284a90SAnshuman Khandual * also be derived from its end points. 147758284a90SAnshuman Khandual */ 1478ee7febceSPavel Tatashin mhp_range.start = start_linear_pa; 1479ee7febceSPavel Tatashin mhp_range.end = end_linear_pa; 1480ee7febceSPavel Tatashin 148103aaf83fSAnshuman Khandual return mhp_range; 148258284a90SAnshuman Khandual } 148358284a90SAnshuman Khandual 1484940519f0SMichal Hocko int arch_add_memory(int nid, u64 start, u64 size, 1485f5637d3bSLogan Gunthorpe struct mhp_params *params) 14864ab21506SRobin Murphy { 148787143f40SArd Biesheuvel int ret, flags = NO_EXEC_MAPPINGS; 14884ab21506SRobin Murphy 148903aaf83fSAnshuman Khandual VM_BUG_ON(!mhp_range_allowed(start, size, true)); 1490840b2398SMarco Elver 1491840b2398SMarco Elver /* 1492840b2398SMarco Elver * KFENCE requires linear map to be mapped at page granularity, so that 1493840b2398SMarco Elver * it is possible to protect/unprotect single pages in the KFENCE pool. 1494840b2398SMarco Elver */ 14956d47c23bSMike Rapoport if (can_set_direct_map() || IS_ENABLED(CONFIG_KFENCE)) 149687143f40SArd Biesheuvel flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 14974ab21506SRobin Murphy 14984ab21506SRobin Murphy __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start), 1499bfeb022fSLogan Gunthorpe size, params->pgprot, __pgd_pgtable_alloc, 1500bfeb022fSLogan Gunthorpe flags); 15014ab21506SRobin Murphy 150216993c0fSDan Williams memblock_clear_nomap(start, size); 150316993c0fSDan Williams 1504bbd6ec60SAnshuman Khandual ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT, 1505f5637d3bSLogan Gunthorpe params); 1506bbd6ec60SAnshuman Khandual if (ret) 1507bbd6ec60SAnshuman Khandual __remove_pgd_mapping(swapper_pg_dir, 1508bbd6ec60SAnshuman Khandual __phys_to_virt(start), size); 15098fac67caSSudarshan Rajagopalan else { 15108fac67caSSudarshan Rajagopalan max_pfn = PFN_UP(start + size); 15118fac67caSSudarshan Rajagopalan max_low_pfn = max_pfn; 15128fac67caSSudarshan Rajagopalan } 15138fac67caSSudarshan Rajagopalan 1514bbd6ec60SAnshuman Khandual return ret; 15154ab21506SRobin Murphy } 1516bbd6ec60SAnshuman Khandual 151765a2aa5fSDavid Hildenbrand void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) 151822eb6346SDavid Hildenbrand { 151922eb6346SDavid Hildenbrand unsigned long start_pfn = start >> PAGE_SHIFT; 152022eb6346SDavid Hildenbrand unsigned long nr_pages = size >> PAGE_SHIFT; 152122eb6346SDavid Hildenbrand 1522feee6b29SDavid Hildenbrand __remove_pages(start_pfn, nr_pages, altmap); 1523bbd6ec60SAnshuman Khandual __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size); 152422eb6346SDavid Hildenbrand } 1525bbd6ec60SAnshuman Khandual 1526bbd6ec60SAnshuman Khandual /* 1527bbd6ec60SAnshuman Khandual * This memory hotplug notifier helps prevent boot memory from being 1528bbd6ec60SAnshuman Khandual * inadvertently removed as it blocks pfn range offlining process in 1529bbd6ec60SAnshuman Khandual * __offline_pages(). Hence this prevents both offlining as well as 1530bbd6ec60SAnshuman Khandual * removal process for boot memory which is initially always online. 1531bbd6ec60SAnshuman Khandual * In future if and when boot memory could be removed, this notifier 1532bbd6ec60SAnshuman Khandual * should be dropped and free_hotplug_page_range() should handle any 1533bbd6ec60SAnshuman Khandual * reserved pages allocated during boot. 1534bbd6ec60SAnshuman Khandual */ 1535bbd6ec60SAnshuman Khandual static int prevent_bootmem_remove_notifier(struct notifier_block *nb, 1536bbd6ec60SAnshuman Khandual unsigned long action, void *data) 1537bbd6ec60SAnshuman Khandual { 1538bbd6ec60SAnshuman Khandual struct mem_section *ms; 1539bbd6ec60SAnshuman Khandual struct memory_notify *arg = data; 1540bbd6ec60SAnshuman Khandual unsigned long end_pfn = arg->start_pfn + arg->nr_pages; 1541bbd6ec60SAnshuman Khandual unsigned long pfn = arg->start_pfn; 1542bbd6ec60SAnshuman Khandual 15439fb3d4a3SAnshuman Khandual if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE)) 1544bbd6ec60SAnshuman Khandual return NOTIFY_OK; 1545bbd6ec60SAnshuman Khandual 1546bbd6ec60SAnshuman Khandual for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 15479fb3d4a3SAnshuman Khandual unsigned long start = PFN_PHYS(pfn); 15489fb3d4a3SAnshuman Khandual unsigned long end = start + (1UL << PA_SECTION_SHIFT); 15499fb3d4a3SAnshuman Khandual 1550bbd6ec60SAnshuman Khandual ms = __pfn_to_section(pfn); 15519fb3d4a3SAnshuman Khandual if (!early_section(ms)) 15529fb3d4a3SAnshuman Khandual continue; 15539fb3d4a3SAnshuman Khandual 15549fb3d4a3SAnshuman Khandual if (action == MEM_GOING_OFFLINE) { 15559fb3d4a3SAnshuman Khandual /* 15569fb3d4a3SAnshuman Khandual * Boot memory removal is not supported. Prevent 15579fb3d4a3SAnshuman Khandual * it via blocking any attempted offline request 15589fb3d4a3SAnshuman Khandual * for the boot memory and just report it. 15599fb3d4a3SAnshuman Khandual */ 15609fb3d4a3SAnshuman Khandual pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end); 1561bbd6ec60SAnshuman Khandual return NOTIFY_BAD; 15629fb3d4a3SAnshuman Khandual } else if (action == MEM_OFFLINE) { 15639fb3d4a3SAnshuman Khandual /* 15649fb3d4a3SAnshuman Khandual * This should have never happened. Boot memory 15659fb3d4a3SAnshuman Khandual * offlining should have been prevented by this 15669fb3d4a3SAnshuman Khandual * very notifier. Probably some memory removal 15679fb3d4a3SAnshuman Khandual * procedure might have changed which would then 15689fb3d4a3SAnshuman Khandual * require further debug. 15699fb3d4a3SAnshuman Khandual */ 15709fb3d4a3SAnshuman Khandual pr_err("Boot memory [%lx %lx] offlined\n", start, end); 15719fb3d4a3SAnshuman Khandual 15729fb3d4a3SAnshuman Khandual /* 15739fb3d4a3SAnshuman Khandual * Core memory hotplug does not process a return 15749fb3d4a3SAnshuman Khandual * code from the notifier for MEM_OFFLINE events. 15759fb3d4a3SAnshuman Khandual * The error condition has been reported. Return 15769fb3d4a3SAnshuman Khandual * from here as if ignored. 15779fb3d4a3SAnshuman Khandual */ 15789fb3d4a3SAnshuman Khandual return NOTIFY_DONE; 15799fb3d4a3SAnshuman Khandual } 1580bbd6ec60SAnshuman Khandual } 1581bbd6ec60SAnshuman Khandual return NOTIFY_OK; 1582bbd6ec60SAnshuman Khandual } 1583bbd6ec60SAnshuman Khandual 1584bbd6ec60SAnshuman Khandual static struct notifier_block prevent_bootmem_remove_nb = { 1585bbd6ec60SAnshuman Khandual .notifier_call = prevent_bootmem_remove_notifier, 1586bbd6ec60SAnshuman Khandual }; 1587bbd6ec60SAnshuman Khandual 1588fdd99a41SAnshuman Khandual /* 1589fdd99a41SAnshuman Khandual * This ensures that boot memory sections on the platform are online 1590fdd99a41SAnshuman Khandual * from early boot. Memory sections could not be prevented from being 1591fdd99a41SAnshuman Khandual * offlined, unless for some reason they are not online to begin with. 1592fdd99a41SAnshuman Khandual * This helps validate the basic assumption on which the above memory 1593fdd99a41SAnshuman Khandual * event notifier works to prevent boot memory section offlining and 1594fdd99a41SAnshuman Khandual * its possible removal. 1595fdd99a41SAnshuman Khandual */ 1596fdd99a41SAnshuman Khandual static void validate_bootmem_online(void) 1597fdd99a41SAnshuman Khandual { 1598fdd99a41SAnshuman Khandual phys_addr_t start, end, addr; 1599fdd99a41SAnshuman Khandual struct mem_section *ms; 1600fdd99a41SAnshuman Khandual u64 i; 1601fdd99a41SAnshuman Khandual 1602fdd99a41SAnshuman Khandual /* 1603fdd99a41SAnshuman Khandual * Scanning across all memblock might be expensive 1604fdd99a41SAnshuman Khandual * on some big memory systems. Hence enable this 1605fdd99a41SAnshuman Khandual * validation only with DEBUG_VM. 1606fdd99a41SAnshuman Khandual */ 1607fdd99a41SAnshuman Khandual if (!IS_ENABLED(CONFIG_DEBUG_VM)) 1608fdd99a41SAnshuman Khandual return; 1609fdd99a41SAnshuman Khandual 1610fdd99a41SAnshuman Khandual for_each_mem_range(i, &start, &end) { 1611fdd99a41SAnshuman Khandual for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) { 1612fdd99a41SAnshuman Khandual ms = __pfn_to_section(PHYS_PFN(addr)); 1613fdd99a41SAnshuman Khandual 1614fdd99a41SAnshuman Khandual /* 1615fdd99a41SAnshuman Khandual * All memory ranges in the system at this point 1616fdd99a41SAnshuman Khandual * should have been marked as early sections. 1617fdd99a41SAnshuman Khandual */ 1618fdd99a41SAnshuman Khandual WARN_ON(!early_section(ms)); 1619fdd99a41SAnshuman Khandual 1620fdd99a41SAnshuman Khandual /* 1621fdd99a41SAnshuman Khandual * Memory notifier mechanism here to prevent boot 1622fdd99a41SAnshuman Khandual * memory offlining depends on the fact that each 1623fdd99a41SAnshuman Khandual * early section memory on the system is initially 1624fdd99a41SAnshuman Khandual * online. Otherwise a given memory section which 1625fdd99a41SAnshuman Khandual * is already offline will be overlooked and can 1626fdd99a41SAnshuman Khandual * be removed completely. Call out such sections. 1627fdd99a41SAnshuman Khandual */ 1628fdd99a41SAnshuman Khandual if (!online_section(ms)) 1629fdd99a41SAnshuman Khandual pr_err("Boot memory [%llx %llx] is offline, can be removed\n", 1630fdd99a41SAnshuman Khandual addr, addr + (1UL << PA_SECTION_SHIFT)); 1631fdd99a41SAnshuman Khandual } 1632fdd99a41SAnshuman Khandual } 1633fdd99a41SAnshuman Khandual } 1634fdd99a41SAnshuman Khandual 1635bbd6ec60SAnshuman Khandual static int __init prevent_bootmem_remove_init(void) 1636bbd6ec60SAnshuman Khandual { 1637cb45babeSAnshuman Khandual int ret = 0; 1638cb45babeSAnshuman Khandual 1639cb45babeSAnshuman Khandual if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE)) 1640cb45babeSAnshuman Khandual return ret; 1641cb45babeSAnshuman Khandual 1642fdd99a41SAnshuman Khandual validate_bootmem_online(); 1643cb45babeSAnshuman Khandual ret = register_memory_notifier(&prevent_bootmem_remove_nb); 1644cb45babeSAnshuman Khandual if (ret) 1645cb45babeSAnshuman Khandual pr_err("%s: Notifier registration failed %d\n", __func__, ret); 1646cb45babeSAnshuman Khandual 1647cb45babeSAnshuman Khandual return ret; 1648bbd6ec60SAnshuman Khandual } 1649cb45babeSAnshuman Khandual early_initcall(prevent_bootmem_remove_init); 165022eb6346SDavid Hildenbrand #endif 1651