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> 20dc90f084SChristoph Hellwig #include <linux/memremap.h> 21bbd6ec60SAnshuman Khandual #include <linux/memory.h> 22c1cc1552SCatalin Marinas #include <linux/fs.h> 232475ff9dSCatalin Marinas #include <linux/io.h> 242077be67SLaura Abbott #include <linux/mm.h> 256efd8499STobias Klauser #include <linux/vmalloc.h> 266d47c23bSMike Rapoport #include <linux/set_memory.h> 27c1cc1552SCatalin Marinas 2821ab99c2SMark Rutland #include <asm/barrier.h> 29c1cc1552SCatalin Marinas #include <asm/cputype.h> 30af86e597SLaura Abbott #include <asm/fixmap.h> 31068a17a5SMark Rutland #include <asm/kasan.h> 32b433dce0SSuzuki K. Poulose #include <asm/kernel-pgtable.h> 33c1cc1552SCatalin Marinas #include <asm/sections.h> 34c1cc1552SCatalin Marinas #include <asm/setup.h> 3587dfb311SMasahiro Yamada #include <linux/sizes.h> 36c1cc1552SCatalin Marinas #include <asm/tlb.h> 37c1cc1552SCatalin Marinas #include <asm/mmu_context.h> 381404d6f1SLaura Abbott #include <asm/ptdump.h> 39ec28bb9cSChintan Pandya #include <asm/tlbflush.h> 40ca15ca40SMike Rapoport #include <asm/pgalloc.h> 41c1cc1552SCatalin Marinas 42c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS BIT(0) 43d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS BIT(1) 4487143f40SArd Biesheuvel #define NO_EXEC_MAPPINGS BIT(2) /* assumes FEAT_HPDS is not used */ 45c0951366SArd Biesheuvel 467ba8f2b2SArd Biesheuvel u64 idmap_t0sz = TCR_T0SZ(VA_BITS_MIN); 47fa2a8445SKristina Martsenko u64 idmap_ptrs_per_pgd = PTRS_PER_PGD; 48dd006da2SArd Biesheuvel 495383cc6eSSteve Capper u64 __section(".mmuoff.data.write") vabits_actual; 505383cc6eSSteve Capper EXPORT_SYMBOL(vabits_actual); 51c1cc1552SCatalin Marinas 52*475031b6SArd Biesheuvel u64 kimage_vaddr __ro_after_init = (u64)&_text; 53*475031b6SArd Biesheuvel EXPORT_SYMBOL(kimage_vaddr); 54*475031b6SArd Biesheuvel 555a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init; 56a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset); 57a7f8de16SArd Biesheuvel 58c1cc1552SCatalin Marinas /* 59c1cc1552SCatalin Marinas * Empty_zero_page is a special page that is used for zero-initialized data 60c1cc1552SCatalin Marinas * and COW. 61c1cc1552SCatalin Marinas */ 625227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss; 63c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page); 64c1cc1552SCatalin Marinas 65f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss; 66f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused; 67f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused; 68f9040773SArd Biesheuvel 692330b7caSJun Yao static DEFINE_SPINLOCK(swapper_pgdir_lock); 70ee017ee3SJianyong Wu static DEFINE_MUTEX(fixmap_lock); 712330b7caSJun Yao 722330b7caSJun Yao void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd) 732330b7caSJun Yao { 742330b7caSJun Yao pgd_t *fixmap_pgdp; 752330b7caSJun Yao 762330b7caSJun Yao spin_lock(&swapper_pgdir_lock); 7726a6f87eSJames Morse fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp)); 782330b7caSJun Yao WRITE_ONCE(*fixmap_pgdp, pgd); 792330b7caSJun Yao /* 802330b7caSJun Yao * We need dsb(ishst) here to ensure the page-table-walker sees 812330b7caSJun Yao * our new entry before set_p?d() returns. The fixmap's 822330b7caSJun Yao * flush_tlb_kernel_range() via clear_fixmap() does this for us. 832330b7caSJun Yao */ 842330b7caSJun Yao pgd_clear_fixmap(); 852330b7caSJun Yao spin_unlock(&swapper_pgdir_lock); 862330b7caSJun Yao } 872330b7caSJun Yao 88c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 89c1cc1552SCatalin Marinas unsigned long size, pgprot_t vma_prot) 90c1cc1552SCatalin Marinas { 91873ba463SMike Rapoport if (!pfn_is_map_memory(pfn)) 92c1cc1552SCatalin Marinas return pgprot_noncached(vma_prot); 93c1cc1552SCatalin Marinas else if (file->f_flags & O_SYNC) 94c1cc1552SCatalin Marinas return pgprot_writecombine(vma_prot); 95c1cc1552SCatalin Marinas return vma_prot; 96c1cc1552SCatalin Marinas } 97c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot); 98c1cc1552SCatalin Marinas 9990292acaSYu Zhao static phys_addr_t __init early_pgtable_alloc(int shift) 100c1cc1552SCatalin Marinas { 1017142392dSSuzuki K. Poulose phys_addr_t phys; 1027142392dSSuzuki K. Poulose void *ptr; 1037142392dSSuzuki K. Poulose 104c6975d7cSQian Cai phys = memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE, 0, 105c6975d7cSQian Cai MEMBLOCK_ALLOC_NOLEAKTRACE); 106ecc3e771SMike Rapoport if (!phys) 107ecc3e771SMike Rapoport panic("Failed to allocate page table page\n"); 108f4710445SMark Rutland 109f4710445SMark Rutland /* 110f4710445SMark Rutland * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE 111f4710445SMark Rutland * slot will be free, so we can (ab)use the FIX_PTE slot to initialise 112f4710445SMark Rutland * any level of table. 113f4710445SMark Rutland */ 114f4710445SMark Rutland ptr = pte_set_fixmap(phys); 115f4710445SMark Rutland 11621ab99c2SMark Rutland memset(ptr, 0, PAGE_SIZE); 11721ab99c2SMark Rutland 118f4710445SMark Rutland /* 119f4710445SMark Rutland * Implicit barriers also ensure the zeroed page is visible to the page 120f4710445SMark Rutland * table walker 121f4710445SMark Rutland */ 122f4710445SMark Rutland pte_clear_fixmap(); 123f4710445SMark Rutland 124f4710445SMark Rutland return phys; 125c1cc1552SCatalin Marinas } 126c1cc1552SCatalin Marinas 127e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new) 128e98216b5SArd Biesheuvel { 129e98216b5SArd Biesheuvel /* 130e98216b5SArd Biesheuvel * The following mapping attributes may be updated in live 131e98216b5SArd Biesheuvel * kernel mappings without the need for break-before-make. 132e98216b5SArd Biesheuvel */ 1330178dc76SCatalin Marinas pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG; 134e98216b5SArd Biesheuvel 135141d1497SArd Biesheuvel /* creating or taking down mappings is always safe */ 136141d1497SArd Biesheuvel if (old == 0 || new == 0) 137141d1497SArd Biesheuvel return true; 138141d1497SArd Biesheuvel 139141d1497SArd Biesheuvel /* live contiguous mappings may not be manipulated at all */ 140141d1497SArd Biesheuvel if ((old | new) & PTE_CONT) 141141d1497SArd Biesheuvel return false; 142141d1497SArd Biesheuvel 143753e8abcSArd Biesheuvel /* Transitioning from Non-Global to Global is unsafe */ 144753e8abcSArd Biesheuvel if (old & ~new & PTE_NG) 145753e8abcSArd Biesheuvel return false; 1464e602056SWill Deacon 1470178dc76SCatalin Marinas /* 1480178dc76SCatalin Marinas * Changing the memory type between Normal and Normal-Tagged is safe 1490178dc76SCatalin Marinas * since Tagged is considered a permission attribute from the 1500178dc76SCatalin Marinas * mismatched attribute aliases perspective. 1510178dc76SCatalin Marinas */ 1520178dc76SCatalin Marinas if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 1530178dc76SCatalin Marinas (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) && 1540178dc76SCatalin Marinas ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 1550178dc76SCatalin Marinas (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED))) 1560178dc76SCatalin Marinas mask |= PTE_ATTRINDX_MASK; 1570178dc76SCatalin Marinas 158141d1497SArd Biesheuvel return ((old ^ new) & ~mask) == 0; 159e98216b5SArd Biesheuvel } 160e98216b5SArd Biesheuvel 16120a004e7SWill Deacon static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end, 162d27cfa1fSArd Biesheuvel phys_addr_t phys, pgprot_t prot) 163c1cc1552SCatalin Marinas { 16420a004e7SWill Deacon pte_t *ptep; 165c1cc1552SCatalin Marinas 16620a004e7SWill Deacon ptep = pte_set_fixmap_offset(pmdp, addr); 167c1cc1552SCatalin Marinas do { 16820a004e7SWill Deacon pte_t old_pte = READ_ONCE(*ptep); 169e98216b5SArd Biesheuvel 17020a004e7SWill Deacon set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot)); 171e98216b5SArd Biesheuvel 172e98216b5SArd Biesheuvel /* 173e98216b5SArd Biesheuvel * After the PTE entry has been populated once, we 174e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 175e98216b5SArd Biesheuvel */ 17620a004e7SWill Deacon BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), 17720a004e7SWill Deacon READ_ONCE(pte_val(*ptep)))); 178e98216b5SArd Biesheuvel 179e393cf40SArd Biesheuvel phys += PAGE_SIZE; 18020a004e7SWill Deacon } while (ptep++, addr += PAGE_SIZE, addr != end); 181f4710445SMark Rutland 182f4710445SMark Rutland pte_clear_fixmap(); 183c1cc1552SCatalin Marinas } 184c1cc1552SCatalin Marinas 18520a004e7SWill Deacon static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr, 186d27cfa1fSArd Biesheuvel unsigned long end, phys_addr_t phys, 187d27cfa1fSArd Biesheuvel pgprot_t prot, 18890292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 189c0951366SArd Biesheuvel int flags) 190c1cc1552SCatalin Marinas { 191c1cc1552SCatalin Marinas unsigned long next; 19220a004e7SWill Deacon pmd_t pmd = READ_ONCE(*pmdp); 193c1cc1552SCatalin Marinas 19420a004e7SWill Deacon BUG_ON(pmd_sect(pmd)); 19520a004e7SWill Deacon if (pmd_none(pmd)) { 19687143f40SArd Biesheuvel pmdval_t pmdval = PMD_TYPE_TABLE | PMD_TABLE_UXN; 197d27cfa1fSArd Biesheuvel phys_addr_t pte_phys; 19887143f40SArd Biesheuvel 19987143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 20087143f40SArd Biesheuvel pmdval |= PMD_TABLE_PXN; 201132233a7SLaura Abbott BUG_ON(!pgtable_alloc); 20290292acaSYu Zhao pte_phys = pgtable_alloc(PAGE_SHIFT); 20387143f40SArd Biesheuvel __pmd_populate(pmdp, pte_phys, pmdval); 20420a004e7SWill Deacon pmd = READ_ONCE(*pmdp); 205c1cc1552SCatalin Marinas } 20620a004e7SWill Deacon BUG_ON(pmd_bad(pmd)); 207d27cfa1fSArd Biesheuvel 208d27cfa1fSArd Biesheuvel do { 209d27cfa1fSArd Biesheuvel pgprot_t __prot = prot; 210d27cfa1fSArd Biesheuvel 211d27cfa1fSArd Biesheuvel next = pte_cont_addr_end(addr, end); 212d27cfa1fSArd Biesheuvel 213d27cfa1fSArd Biesheuvel /* use a contiguous mapping if the range is suitably aligned */ 214d27cfa1fSArd Biesheuvel if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) && 215d27cfa1fSArd Biesheuvel (flags & NO_CONT_MAPPINGS) == 0) 216d27cfa1fSArd Biesheuvel __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 217d27cfa1fSArd Biesheuvel 21820a004e7SWill Deacon init_pte(pmdp, addr, next, phys, __prot); 219d27cfa1fSArd Biesheuvel 220d27cfa1fSArd Biesheuvel phys += next - addr; 221d27cfa1fSArd Biesheuvel } while (addr = next, addr != end); 222d27cfa1fSArd Biesheuvel } 223d27cfa1fSArd Biesheuvel 22420a004e7SWill Deacon static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end, 225d27cfa1fSArd Biesheuvel phys_addr_t phys, pgprot_t prot, 22690292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), int flags) 227d27cfa1fSArd Biesheuvel { 228d27cfa1fSArd Biesheuvel unsigned long next; 22920a004e7SWill Deacon pmd_t *pmdp; 230c1cc1552SCatalin Marinas 23120a004e7SWill Deacon pmdp = pmd_set_fixmap_offset(pudp, addr); 232c1cc1552SCatalin Marinas do { 23320a004e7SWill Deacon pmd_t old_pmd = READ_ONCE(*pmdp); 234e98216b5SArd Biesheuvel 235c1cc1552SCatalin Marinas next = pmd_addr_end(addr, end); 236e98216b5SArd Biesheuvel 237c1cc1552SCatalin Marinas /* try section mapping first */ 2384aaa87abSAnshuman Khandual if (((addr | next | phys) & ~PMD_MASK) == 0 && 239c0951366SArd Biesheuvel (flags & NO_BLOCK_MAPPINGS) == 0) { 24020a004e7SWill Deacon pmd_set_huge(pmdp, phys, prot); 241e98216b5SArd Biesheuvel 242a55f9929SCatalin Marinas /* 243e98216b5SArd Biesheuvel * After the PMD entry has been populated once, we 244e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 245a55f9929SCatalin Marinas */ 246e98216b5SArd Biesheuvel BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd), 24720a004e7SWill Deacon READ_ONCE(pmd_val(*pmdp)))); 248a55f9929SCatalin Marinas } else { 24920a004e7SWill Deacon alloc_init_cont_pte(pmdp, addr, next, phys, prot, 250d27cfa1fSArd Biesheuvel pgtable_alloc, flags); 251e98216b5SArd Biesheuvel 252e98216b5SArd Biesheuvel BUG_ON(pmd_val(old_pmd) != 0 && 25320a004e7SWill Deacon pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp))); 254a55f9929SCatalin Marinas } 255c1cc1552SCatalin Marinas phys += next - addr; 25620a004e7SWill Deacon } while (pmdp++, addr = next, addr != end); 257f4710445SMark Rutland 258f4710445SMark Rutland pmd_clear_fixmap(); 259c1cc1552SCatalin Marinas } 260c1cc1552SCatalin Marinas 26120a004e7SWill Deacon static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr, 262d27cfa1fSArd Biesheuvel unsigned long end, phys_addr_t phys, 263d27cfa1fSArd Biesheuvel pgprot_t prot, 26490292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), int flags) 265d27cfa1fSArd Biesheuvel { 266d27cfa1fSArd Biesheuvel unsigned long next; 26720a004e7SWill Deacon pud_t pud = READ_ONCE(*pudp); 268d27cfa1fSArd Biesheuvel 269d27cfa1fSArd Biesheuvel /* 270d27cfa1fSArd Biesheuvel * Check for initial section mappings in the pgd/pud. 271d27cfa1fSArd Biesheuvel */ 27220a004e7SWill Deacon BUG_ON(pud_sect(pud)); 27320a004e7SWill Deacon if (pud_none(pud)) { 27487143f40SArd Biesheuvel pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_UXN; 275d27cfa1fSArd Biesheuvel phys_addr_t pmd_phys; 27687143f40SArd Biesheuvel 27787143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 27887143f40SArd Biesheuvel pudval |= PUD_TABLE_PXN; 279d27cfa1fSArd Biesheuvel BUG_ON(!pgtable_alloc); 28090292acaSYu Zhao pmd_phys = pgtable_alloc(PMD_SHIFT); 28187143f40SArd Biesheuvel __pud_populate(pudp, pmd_phys, pudval); 28220a004e7SWill Deacon pud = READ_ONCE(*pudp); 283d27cfa1fSArd Biesheuvel } 28420a004e7SWill Deacon BUG_ON(pud_bad(pud)); 285d27cfa1fSArd Biesheuvel 286d27cfa1fSArd Biesheuvel do { 287d27cfa1fSArd Biesheuvel pgprot_t __prot = prot; 288d27cfa1fSArd Biesheuvel 289d27cfa1fSArd Biesheuvel next = pmd_cont_addr_end(addr, end); 290d27cfa1fSArd Biesheuvel 291d27cfa1fSArd Biesheuvel /* use a contiguous mapping if the range is suitably aligned */ 292d27cfa1fSArd Biesheuvel if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) && 293d27cfa1fSArd Biesheuvel (flags & NO_CONT_MAPPINGS) == 0) 294d27cfa1fSArd Biesheuvel __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 295d27cfa1fSArd Biesheuvel 29620a004e7SWill Deacon init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags); 297d27cfa1fSArd Biesheuvel 298d27cfa1fSArd Biesheuvel phys += next - addr; 299d27cfa1fSArd Biesheuvel } while (addr = next, addr != end); 300d27cfa1fSArd Biesheuvel } 301d27cfa1fSArd Biesheuvel 30220a004e7SWill Deacon static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, 303da141706SLaura Abbott phys_addr_t phys, pgprot_t prot, 30490292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 305c0951366SArd Biesheuvel int flags) 306c1cc1552SCatalin Marinas { 307c1cc1552SCatalin Marinas unsigned long next; 30820a004e7SWill Deacon pud_t *pudp; 309e9f63768SMike Rapoport p4d_t *p4dp = p4d_offset(pgdp, addr); 310e9f63768SMike Rapoport p4d_t p4d = READ_ONCE(*p4dp); 311c1cc1552SCatalin Marinas 312e9f63768SMike Rapoport if (p4d_none(p4d)) { 31387143f40SArd Biesheuvel p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN; 314132233a7SLaura Abbott phys_addr_t pud_phys; 31587143f40SArd Biesheuvel 31687143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 31787143f40SArd Biesheuvel p4dval |= P4D_TABLE_PXN; 318132233a7SLaura Abbott BUG_ON(!pgtable_alloc); 31990292acaSYu Zhao pud_phys = pgtable_alloc(PUD_SHIFT); 32087143f40SArd Biesheuvel __p4d_populate(p4dp, pud_phys, p4dval); 321e9f63768SMike Rapoport p4d = READ_ONCE(*p4dp); 322c79b954bSJungseok Lee } 323e9f63768SMike Rapoport BUG_ON(p4d_bad(p4d)); 324c79b954bSJungseok Lee 325ee017ee3SJianyong Wu /* 326ee017ee3SJianyong Wu * No need for locking during early boot. And it doesn't work as 327ee017ee3SJianyong Wu * expected with KASLR enabled. 328ee017ee3SJianyong Wu */ 329ee017ee3SJianyong Wu if (system_state != SYSTEM_BOOTING) 330ee017ee3SJianyong Wu mutex_lock(&fixmap_lock); 331e9f63768SMike Rapoport pudp = pud_set_fixmap_offset(p4dp, addr); 332c1cc1552SCatalin Marinas do { 33320a004e7SWill Deacon pud_t old_pud = READ_ONCE(*pudp); 334e98216b5SArd Biesheuvel 335c1cc1552SCatalin Marinas next = pud_addr_end(addr, end); 336206a2a73SSteve Capper 337206a2a73SSteve Capper /* 338206a2a73SSteve Capper * For 4K granule only, attempt to put down a 1GB block 339206a2a73SSteve Capper */ 3401310222cSAnshuman Khandual if (pud_sect_supported() && 3411310222cSAnshuman Khandual ((addr | next | phys) & ~PUD_MASK) == 0 && 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(); 362ee017ee3SJianyong Wu if (system_state != SYSTEM_BOOTING) 363ee017ee3SJianyong Wu mutex_unlock(&fixmap_lock); 364c1cc1552SCatalin Marinas } 365c1cc1552SCatalin Marinas 36640f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, 36740f87d31SArd Biesheuvel unsigned long virt, phys_addr_t size, 36840f87d31SArd Biesheuvel pgprot_t prot, 36990292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 370c0951366SArd Biesheuvel int flags) 371c1cc1552SCatalin Marinas { 37232d18708SMasahiro Yamada unsigned long addr, end, next; 373974b9b2cSMike Rapoport pgd_t *pgdp = pgd_offset_pgd(pgdir, virt); 374c1cc1552SCatalin Marinas 375cc5d2b3bSMark Rutland /* 376cc5d2b3bSMark Rutland * If the virtual and physical address don't have the same offset 377cc5d2b3bSMark Rutland * within a page, we cannot map the region as the caller expects. 378cc5d2b3bSMark Rutland */ 379cc5d2b3bSMark Rutland if (WARN_ON((phys ^ virt) & ~PAGE_MASK)) 380cc5d2b3bSMark Rutland return; 381cc5d2b3bSMark Rutland 3829c4e08a3SMark Rutland phys &= PAGE_MASK; 383c1cc1552SCatalin Marinas addr = virt & PAGE_MASK; 38432d18708SMasahiro Yamada end = PAGE_ALIGN(virt + size); 385c1cc1552SCatalin Marinas 386c1cc1552SCatalin Marinas do { 387c1cc1552SCatalin Marinas next = pgd_addr_end(addr, end); 38820a004e7SWill Deacon alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc, 389c0951366SArd Biesheuvel flags); 390c1cc1552SCatalin Marinas phys += next - addr; 39120a004e7SWill Deacon } while (pgdp++, addr = next, addr != end); 392c1cc1552SCatalin Marinas } 393c1cc1552SCatalin Marinas 394475ba3fcSWill Deacon static phys_addr_t __pgd_pgtable_alloc(int shift) 395369aaab8SYu Zhao { 39650f11a8aSMike Rapoport void *ptr = (void *)__get_free_page(GFP_PGTABLE_KERNEL); 397369aaab8SYu Zhao BUG_ON(!ptr); 398369aaab8SYu Zhao 399369aaab8SYu Zhao /* Ensure the zeroed page is visible to the page table walker */ 400369aaab8SYu Zhao dsb(ishst); 401369aaab8SYu Zhao return __pa(ptr); 402369aaab8SYu Zhao } 403369aaab8SYu Zhao 40490292acaSYu Zhao static phys_addr_t pgd_pgtable_alloc(int shift) 405da141706SLaura Abbott { 406475ba3fcSWill Deacon phys_addr_t pa = __pgd_pgtable_alloc(shift); 40790292acaSYu Zhao 40890292acaSYu Zhao /* 40990292acaSYu Zhao * Call proper page table ctor in case later we need to 41090292acaSYu Zhao * call core mm functions like apply_to_page_range() on 41190292acaSYu Zhao * this pre-allocated page table. 41290292acaSYu Zhao * 41390292acaSYu Zhao * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is 41490292acaSYu Zhao * folded, and if so pgtable_pmd_page_ctor() becomes nop. 41590292acaSYu Zhao */ 41690292acaSYu Zhao if (shift == PAGE_SHIFT) 417b4ed71f5SMark Rutland BUG_ON(!pgtable_pte_page_ctor(phys_to_page(pa))); 41890292acaSYu Zhao else if (shift == PMD_SHIFT) 419475ba3fcSWill Deacon BUG_ON(!pgtable_pmd_page_ctor(phys_to_page(pa))); 42021ab99c2SMark Rutland 421475ba3fcSWill Deacon return pa; 422da141706SLaura Abbott } 423da141706SLaura Abbott 424132233a7SLaura Abbott /* 425132233a7SLaura Abbott * This function can only be used to modify existing table entries, 426132233a7SLaura Abbott * without allocating new levels of table. Note that this permits the 427132233a7SLaura Abbott * creation of new section or page entries. 428132233a7SLaura Abbott */ 429132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt, 430da141706SLaura Abbott phys_addr_t size, pgprot_t prot) 431d7ecbddfSMark Salter { 43277ad4ce6SMark Rutland if ((virt >= PAGE_END) && (virt < VMALLOC_START)) { 433d7ecbddfSMark Salter pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n", 434d7ecbddfSMark Salter &phys, virt); 435d7ecbddfSMark Salter return; 436d7ecbddfSMark Salter } 437d27cfa1fSArd Biesheuvel __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 438d27cfa1fSArd Biesheuvel NO_CONT_MAPPINGS); 439d7ecbddfSMark Salter } 440d7ecbddfSMark Salter 4418ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, 4428ce837ceSArd Biesheuvel unsigned long virt, phys_addr_t size, 443f14c66ceSArd Biesheuvel pgprot_t prot, bool page_mappings_only) 4448ce837ceSArd Biesheuvel { 445c0951366SArd Biesheuvel int flags = 0; 446c0951366SArd Biesheuvel 4471378dc3dSArd Biesheuvel BUG_ON(mm == &init_mm); 4481378dc3dSArd Biesheuvel 449c0951366SArd Biesheuvel if (page_mappings_only) 450d27cfa1fSArd Biesheuvel flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 451c0951366SArd Biesheuvel 45211509a30SMark Rutland __create_pgd_mapping(mm->pgd, phys, virt, size, prot, 453c0951366SArd Biesheuvel pgd_pgtable_alloc, flags); 454d7ecbddfSMark Salter } 455d7ecbddfSMark Salter 456aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt, 457da141706SLaura Abbott phys_addr_t size, pgprot_t prot) 458da141706SLaura Abbott { 45977ad4ce6SMark Rutland if ((virt >= PAGE_END) && (virt < VMALLOC_START)) { 460aa8c09beSArd Biesheuvel pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n", 461da141706SLaura Abbott &phys, virt); 462da141706SLaura Abbott return; 463da141706SLaura Abbott } 464da141706SLaura Abbott 465d27cfa1fSArd Biesheuvel __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 466d27cfa1fSArd Biesheuvel NO_CONT_MAPPINGS); 467aa8c09beSArd Biesheuvel 468aa8c09beSArd Biesheuvel /* flush the TLBs after updating live kernel mappings */ 469aa8c09beSArd Biesheuvel flush_tlb_kernel_range(virt, virt + size); 470da141706SLaura Abbott } 471da141706SLaura Abbott 47220a004e7SWill Deacon static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start, 47398d2e153STakahiro Akashi phys_addr_t end, pgprot_t prot, int flags) 474da141706SLaura Abbott { 47520a004e7SWill Deacon __create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start, 47698d2e153STakahiro Akashi prot, early_pgtable_alloc, flags); 477da141706SLaura Abbott } 478da141706SLaura Abbott 4795ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void) 4805ea5306cSArd Biesheuvel { 4815ea5306cSArd Biesheuvel /* 4825ea5306cSArd Biesheuvel * Remove the write permissions from the linear alias of .text/.rodata 4835ea5306cSArd Biesheuvel */ 484e2a073ddSArd Biesheuvel update_mapping_prot(__pa_symbol(_stext), (unsigned long)lm_alias(_stext), 485e2a073ddSArd Biesheuvel (unsigned long)__init_begin - (unsigned long)_stext, 4865ea5306cSArd Biesheuvel PAGE_KERNEL_RO); 4875ea5306cSArd Biesheuvel } 4885ea5306cSArd Biesheuvel 4892687275aSCatalin Marinas static bool crash_mem_map __initdata; 4902687275aSCatalin Marinas 4912687275aSCatalin Marinas static int __init enable_crash_mem_map(char *arg) 4922687275aSCatalin Marinas { 4932687275aSCatalin Marinas /* 4942687275aSCatalin Marinas * Proper parameter parsing is done by reserve_crashkernel(). We only 4952687275aSCatalin Marinas * need to know if the linear map has to avoid block mappings so that 4962687275aSCatalin Marinas * the crashkernel reservations can be unmapped later. 4972687275aSCatalin Marinas */ 4982687275aSCatalin Marinas crash_mem_map = true; 4992687275aSCatalin Marinas 5002687275aSCatalin Marinas return 0; 5012687275aSCatalin Marinas } 5022687275aSCatalin Marinas early_param("crashkernel", enable_crash_mem_map); 5032687275aSCatalin Marinas 50420a004e7SWill Deacon static void __init map_mem(pgd_t *pgdp) 505c1cc1552SCatalin Marinas { 50687143f40SArd Biesheuvel static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN); 507e2a073ddSArd Biesheuvel phys_addr_t kernel_start = __pa_symbol(_stext); 50898d2e153STakahiro Akashi phys_addr_t kernel_end = __pa_symbol(__init_begin); 509b10d6bcaSMike Rapoport phys_addr_t start, end; 51087143f40SArd Biesheuvel int flags = NO_EXEC_MAPPINGS; 511b10d6bcaSMike Rapoport u64 i; 51298d2e153STakahiro Akashi 51387143f40SArd Biesheuvel /* 51487143f40SArd Biesheuvel * Setting hierarchical PXNTable attributes on table entries covering 51587143f40SArd Biesheuvel * the linear region is only possible if it is guaranteed that no table 51687143f40SArd Biesheuvel * entries at any level are being shared between the linear region and 51787143f40SArd Biesheuvel * the vmalloc region. Check whether this is true for the PGD level, in 51887143f40SArd Biesheuvel * which case it is guaranteed to be true for all other levels as well. 51987143f40SArd Biesheuvel */ 52087143f40SArd Biesheuvel BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end)); 52187143f40SArd Biesheuvel 52203149563SVijay Balakrishna if (can_set_direct_map() || IS_ENABLED(CONFIG_KFENCE)) 52387143f40SArd Biesheuvel flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 52498d2e153STakahiro Akashi 52598d2e153STakahiro Akashi /* 52698d2e153STakahiro Akashi * Take care not to create a writable alias for the 52798d2e153STakahiro Akashi * read-only text and rodata sections of the kernel image. 52898d2e153STakahiro Akashi * So temporarily mark them as NOMAP to skip mappings in 52998d2e153STakahiro Akashi * the following for-loop 53098d2e153STakahiro Akashi */ 53198d2e153STakahiro Akashi memblock_mark_nomap(kernel_start, kernel_end - kernel_start); 532f6bc87c3SSteve Capper 53303149563SVijay Balakrishna #ifdef CONFIG_KEXEC_CORE 53403149563SVijay Balakrishna if (crash_mem_map) { 53503149563SVijay Balakrishna if (IS_ENABLED(CONFIG_ZONE_DMA) || 53603149563SVijay Balakrishna IS_ENABLED(CONFIG_ZONE_DMA32)) 53703149563SVijay Balakrishna flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 53803149563SVijay Balakrishna else if (crashk_res.end) 53903149563SVijay Balakrishna memblock_mark_nomap(crashk_res.start, 54003149563SVijay Balakrishna resource_size(&crashk_res)); 54103149563SVijay Balakrishna } 54203149563SVijay Balakrishna #endif 54303149563SVijay Balakrishna 544c1cc1552SCatalin Marinas /* map all the memory banks */ 545b10d6bcaSMike Rapoport for_each_mem_range(i, &start, &end) { 546c1cc1552SCatalin Marinas if (start >= end) 547c1cc1552SCatalin Marinas break; 5480178dc76SCatalin Marinas /* 5490178dc76SCatalin Marinas * The linear map must allow allocation tags reading/writing 5500178dc76SCatalin Marinas * if MTE is present. Otherwise, it has the same attributes as 5510178dc76SCatalin Marinas * PAGE_KERNEL. 5520178dc76SCatalin Marinas */ 553d15dfd31SCatalin Marinas __map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL), 554d15dfd31SCatalin Marinas flags); 555c1cc1552SCatalin Marinas } 55698d2e153STakahiro Akashi 55798d2e153STakahiro Akashi /* 558e2a073ddSArd Biesheuvel * Map the linear alias of the [_stext, __init_begin) interval 55998d2e153STakahiro Akashi * as non-executable now, and remove the write permission in 56098d2e153STakahiro Akashi * mark_linear_text_alias_ro() below (which will be called after 56198d2e153STakahiro Akashi * alternative patching has completed). This makes the contents 56298d2e153STakahiro Akashi * of the region accessible to subsystems such as hibernate, 56398d2e153STakahiro Akashi * but protects it from inadvertent modification or execution. 56498d2e153STakahiro Akashi * Note that contiguous mappings cannot be remapped in this way, 56598d2e153STakahiro Akashi * so we should avoid them here. 56698d2e153STakahiro Akashi */ 56720a004e7SWill Deacon __map_memblock(pgdp, kernel_start, kernel_end, 56898d2e153STakahiro Akashi PAGE_KERNEL, NO_CONT_MAPPINGS); 56998d2e153STakahiro Akashi memblock_clear_nomap(kernel_start, kernel_end - kernel_start); 57003149563SVijay Balakrishna 57103149563SVijay Balakrishna /* 57203149563SVijay Balakrishna * Use page-level mappings here so that we can shrink the region 57303149563SVijay Balakrishna * in page granularity and put back unused memory to buddy system 57403149563SVijay Balakrishna * through /sys/kernel/kexec_crash_size interface. 57503149563SVijay Balakrishna */ 57603149563SVijay Balakrishna #ifdef CONFIG_KEXEC_CORE 57703149563SVijay Balakrishna if (crash_mem_map && 57803149563SVijay Balakrishna !IS_ENABLED(CONFIG_ZONE_DMA) && !IS_ENABLED(CONFIG_ZONE_DMA32)) { 57903149563SVijay Balakrishna if (crashk_res.end) { 58003149563SVijay Balakrishna __map_memblock(pgdp, crashk_res.start, 58103149563SVijay Balakrishna crashk_res.end + 1, 58203149563SVijay Balakrishna PAGE_KERNEL, 58303149563SVijay Balakrishna NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS); 58403149563SVijay Balakrishna memblock_clear_nomap(crashk_res.start, 58503149563SVijay Balakrishna resource_size(&crashk_res)); 58603149563SVijay Balakrishna } 58703149563SVijay Balakrishna } 58803149563SVijay Balakrishna #endif 589c1cc1552SCatalin Marinas } 590c1cc1552SCatalin Marinas 591da141706SLaura Abbott void mark_rodata_ro(void) 592da141706SLaura Abbott { 5932f39b5f9SJeremy Linton unsigned long section_size; 594f9040773SArd Biesheuvel 5952f39b5f9SJeremy Linton /* 5969fdc14c5SArd Biesheuvel * mark .rodata as read only. Use __init_begin rather than __end_rodata 5979fdc14c5SArd Biesheuvel * to cover NOTES and EXCEPTION_TABLE. 5982f39b5f9SJeremy Linton */ 5999fdc14c5SArd Biesheuvel section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata; 600aa8c09beSArd Biesheuvel update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata, 6012f39b5f9SJeremy Linton section_size, PAGE_KERNEL_RO); 602e98216b5SArd Biesheuvel 6031404d6f1SLaura Abbott debug_checkwx(); 604da141706SLaura Abbott } 605da141706SLaura Abbott 60620a004e7SWill Deacon static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end, 607d27cfa1fSArd Biesheuvel pgprot_t prot, struct vm_struct *vma, 60892bbd16eSWill Deacon int flags, unsigned long vm_flags) 609068a17a5SMark Rutland { 6102077be67SLaura Abbott phys_addr_t pa_start = __pa_symbol(va_start); 611068a17a5SMark Rutland unsigned long size = va_end - va_start; 612068a17a5SMark Rutland 613068a17a5SMark Rutland BUG_ON(!PAGE_ALIGNED(pa_start)); 614068a17a5SMark Rutland BUG_ON(!PAGE_ALIGNED(size)); 615068a17a5SMark Rutland 61620a004e7SWill Deacon __create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot, 617d27cfa1fSArd Biesheuvel early_pgtable_alloc, flags); 618f9040773SArd Biesheuvel 61992bbd16eSWill Deacon if (!(vm_flags & VM_NO_GUARD)) 62092bbd16eSWill Deacon size += PAGE_SIZE; 62192bbd16eSWill Deacon 622f9040773SArd Biesheuvel vma->addr = va_start; 623f9040773SArd Biesheuvel vma->phys_addr = pa_start; 624f9040773SArd Biesheuvel vma->size = size; 62592bbd16eSWill Deacon vma->flags = VM_MAP | vm_flags; 626f9040773SArd Biesheuvel vma->caller = __builtin_return_address(0); 627f9040773SArd Biesheuvel 628f9040773SArd Biesheuvel vm_area_add_early(vma); 629068a17a5SMark Rutland } 630068a17a5SMark Rutland 63128b066daSArd Biesheuvel static int __init parse_rodata(char *arg) 63228b066daSArd Biesheuvel { 633c55191e9SArd Biesheuvel int ret = strtobool(arg, &rodata_enabled); 634c55191e9SArd Biesheuvel if (!ret) { 635c55191e9SArd Biesheuvel rodata_full = false; 636c55191e9SArd Biesheuvel return 0; 637c55191e9SArd Biesheuvel } 638c55191e9SArd Biesheuvel 639c55191e9SArd Biesheuvel /* permit 'full' in addition to boolean options */ 640c55191e9SArd Biesheuvel if (strcmp(arg, "full")) 641c55191e9SArd Biesheuvel return -EINVAL; 642c55191e9SArd Biesheuvel 643c55191e9SArd Biesheuvel rodata_enabled = true; 644c55191e9SArd Biesheuvel rodata_full = true; 645c55191e9SArd Biesheuvel return 0; 64628b066daSArd Biesheuvel } 64728b066daSArd Biesheuvel early_param("rodata", parse_rodata); 64828b066daSArd Biesheuvel 64951a0048bSWill Deacon #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 65051a0048bSWill Deacon static int __init map_entry_trampoline(void) 65151a0048bSWill Deacon { 652a9c406e6SJames Morse int i; 653a9c406e6SJames Morse 65451a0048bSWill Deacon pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; 65551a0048bSWill Deacon phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start); 65651a0048bSWill Deacon 65751a0048bSWill Deacon /* The trampoline is always mapped and can therefore be global */ 65851a0048bSWill Deacon pgprot_val(prot) &= ~PTE_NG; 65951a0048bSWill Deacon 66051a0048bSWill Deacon /* Map only the text into the trampoline page table */ 66151a0048bSWill Deacon memset(tramp_pg_dir, 0, PGD_SIZE); 662a9c406e6SJames Morse __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, 663a9c406e6SJames Morse entry_tramp_text_size(), prot, 664a9c406e6SJames Morse __pgd_pgtable_alloc, NO_BLOCK_MAPPINGS); 66551a0048bSWill Deacon 6666c27c408SWill Deacon /* Map both the text and data into the kernel page table */ 667a9c406e6SJames Morse for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++) 668a9c406e6SJames Morse __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i, 669a9c406e6SJames Morse pa_start + i * PAGE_SIZE, prot); 670a9c406e6SJames Morse 6716c27c408SWill Deacon if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 6726c27c408SWill Deacon extern char __entry_tramp_data_start[]; 6736c27c408SWill Deacon 6746c27c408SWill Deacon __set_fixmap(FIX_ENTRY_TRAMP_DATA, 6756c27c408SWill Deacon __pa_symbol(__entry_tramp_data_start), 6766c27c408SWill Deacon PAGE_KERNEL_RO); 6776c27c408SWill Deacon } 6786c27c408SWill Deacon 67951a0048bSWill Deacon return 0; 68051a0048bSWill Deacon } 68151a0048bSWill Deacon core_initcall(map_entry_trampoline); 68251a0048bSWill Deacon #endif 68351a0048bSWill Deacon 684068a17a5SMark Rutland /* 685c8027285SMark Brown * Open coded check for BTI, only for use to determine configuration 686c8027285SMark Brown * for early mappings for before the cpufeature code has run. 687c8027285SMark Brown */ 688c8027285SMark Brown static bool arm64_early_this_cpu_has_bti(void) 689c8027285SMark Brown { 690c8027285SMark Brown u64 pfr1; 691c8027285SMark Brown 692c8027285SMark Brown if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)) 693c8027285SMark Brown return false; 694c8027285SMark Brown 69593ad55b7SMarc Zyngier pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1); 696c8027285SMark Brown return cpuid_feature_extract_unsigned_field(pfr1, 697c8027285SMark Brown ID_AA64PFR1_BT_SHIFT); 698c8027285SMark Brown } 699c8027285SMark Brown 700c8027285SMark Brown /* 701068a17a5SMark Rutland * Create fine-grained mappings for the kernel. 702068a17a5SMark Rutland */ 70320a004e7SWill Deacon static void __init map_kernel(pgd_t *pgdp) 704068a17a5SMark Rutland { 7052ebe088bSArd Biesheuvel static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext, 7062ebe088bSArd Biesheuvel vmlinux_initdata, vmlinux_data; 707068a17a5SMark Rutland 70828b066daSArd Biesheuvel /* 70928b066daSArd Biesheuvel * External debuggers may need to write directly to the text 71028b066daSArd Biesheuvel * mapping to install SW breakpoints. Allow this (only) when 71128b066daSArd Biesheuvel * explicitly requested with rodata=off. 71228b066daSArd Biesheuvel */ 71328b066daSArd Biesheuvel pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; 71428b066daSArd Biesheuvel 715d27cfa1fSArd Biesheuvel /* 716c8027285SMark Brown * If we have a CPU that supports BTI and a kernel built for 717c8027285SMark Brown * BTI then mark the kernel executable text as guarded pages 718c8027285SMark Brown * now so we don't have to rewrite the page tables later. 719c8027285SMark Brown */ 720c8027285SMark Brown if (arm64_early_this_cpu_has_bti()) 721c8027285SMark Brown text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP); 722c8027285SMark Brown 723c8027285SMark Brown /* 724d27cfa1fSArd Biesheuvel * Only rodata will be remapped with different permissions later on, 725d27cfa1fSArd Biesheuvel * all other segments are allowed to use contiguous mappings. 726d27cfa1fSArd Biesheuvel */ 727e2a073ddSArd Biesheuvel map_kernel_segment(pgdp, _stext, _etext, text_prot, &vmlinux_text, 0, 72892bbd16eSWill Deacon VM_NO_GUARD); 72920a004e7SWill Deacon map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL, 73092bbd16eSWill Deacon &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD); 73120a004e7SWill Deacon map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot, 73292bbd16eSWill Deacon &vmlinux_inittext, 0, VM_NO_GUARD); 73320a004e7SWill Deacon map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL, 73492bbd16eSWill Deacon &vmlinux_initdata, 0, VM_NO_GUARD); 73520a004e7SWill Deacon map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0); 736068a17a5SMark Rutland 737974b9b2cSMike Rapoport if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) { 738068a17a5SMark Rutland /* 739f9040773SArd Biesheuvel * The fixmap falls in a separate pgd to the kernel, and doesn't 740f9040773SArd Biesheuvel * live in the carveout for the swapper_pg_dir. We can simply 741f9040773SArd Biesheuvel * re-use the existing dir for the fixmap. 742068a17a5SMark Rutland */ 743974b9b2cSMike Rapoport set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START), 74420a004e7SWill Deacon READ_ONCE(*pgd_offset_k(FIXADDR_START))); 745f9040773SArd Biesheuvel } else if (CONFIG_PGTABLE_LEVELS > 3) { 746b333b0baSMark Rutland pgd_t *bm_pgdp; 747e9f63768SMike Rapoport p4d_t *bm_p4dp; 748b333b0baSMark Rutland pud_t *bm_pudp; 749f9040773SArd Biesheuvel /* 750f9040773SArd Biesheuvel * The fixmap shares its top level pgd entry with the kernel 751f9040773SArd Biesheuvel * mapping. This can really only occur when we are running 752f9040773SArd Biesheuvel * with 16k/4 levels, so we can simply reuse the pud level 753f9040773SArd Biesheuvel * entry instead. 754f9040773SArd Biesheuvel */ 755f9040773SArd Biesheuvel BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES)); 756974b9b2cSMike Rapoport bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START); 757e9f63768SMike Rapoport bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START); 758e9f63768SMike Rapoport bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START); 759b333b0baSMark Rutland pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd)); 760f9040773SArd Biesheuvel pud_clear_fixmap(); 761f9040773SArd Biesheuvel } else { 762f9040773SArd Biesheuvel BUG(); 763f9040773SArd Biesheuvel } 764068a17a5SMark Rutland 76520a004e7SWill Deacon kasan_copy_shadow(pgdp); 766068a17a5SMark Rutland } 767068a17a5SMark Rutland 768c1cc1552SCatalin Marinas void __init paging_init(void) 769c1cc1552SCatalin Marinas { 7702330b7caSJun Yao pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir)); 771068a17a5SMark Rutland 77220a004e7SWill Deacon map_kernel(pgdp); 77320a004e7SWill Deacon map_mem(pgdp); 774068a17a5SMark Rutland 775068a17a5SMark Rutland pgd_clear_fixmap(); 776068a17a5SMark Rutland 777068a17a5SMark Rutland cpu_replace_ttbr1(lm_alias(swapper_pg_dir)); 7782b5548b6SJun Yao init_mm.pgd = swapper_pg_dir; 779068a17a5SMark Rutland 7803ecc6834SMike Rapoport memblock_phys_free(__pa_symbol(init_pg_dir), 7812b5548b6SJun Yao __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir)); 78224cc61d8SArd Biesheuvel 78324cc61d8SArd Biesheuvel memblock_allow_resize(); 784c1cc1552SCatalin Marinas } 785c1cc1552SCatalin Marinas 786c1cc1552SCatalin Marinas /* 787c1cc1552SCatalin Marinas * Check whether a kernel address is valid (derived from arch/x86/). 788c1cc1552SCatalin Marinas */ 789c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr) 790c1cc1552SCatalin Marinas { 79120a004e7SWill Deacon pgd_t *pgdp; 792e9f63768SMike Rapoport p4d_t *p4dp; 79320a004e7SWill Deacon pud_t *pudp, pud; 79420a004e7SWill Deacon pmd_t *pmdp, pmd; 79520a004e7SWill Deacon pte_t *ptep, pte; 796c1cc1552SCatalin Marinas 7978dd4daa0SShyam Thombre addr = arch_kasan_reset_tag(addr); 798c1cc1552SCatalin Marinas if ((((long)addr) >> VA_BITS) != -1UL) 799c1cc1552SCatalin Marinas return 0; 800c1cc1552SCatalin Marinas 80120a004e7SWill Deacon pgdp = pgd_offset_k(addr); 80220a004e7SWill Deacon if (pgd_none(READ_ONCE(*pgdp))) 803c1cc1552SCatalin Marinas return 0; 804c1cc1552SCatalin Marinas 805e9f63768SMike Rapoport p4dp = p4d_offset(pgdp, addr); 806e9f63768SMike Rapoport if (p4d_none(READ_ONCE(*p4dp))) 807e9f63768SMike Rapoport return 0; 808e9f63768SMike Rapoport 809e9f63768SMike Rapoport pudp = pud_offset(p4dp, addr); 81020a004e7SWill Deacon pud = READ_ONCE(*pudp); 81120a004e7SWill Deacon if (pud_none(pud)) 812c1cc1552SCatalin Marinas return 0; 813c1cc1552SCatalin Marinas 81420a004e7SWill Deacon if (pud_sect(pud)) 81520a004e7SWill Deacon return pfn_valid(pud_pfn(pud)); 816206a2a73SSteve Capper 81720a004e7SWill Deacon pmdp = pmd_offset(pudp, addr); 81820a004e7SWill Deacon pmd = READ_ONCE(*pmdp); 81920a004e7SWill Deacon if (pmd_none(pmd)) 820c1cc1552SCatalin Marinas return 0; 821c1cc1552SCatalin Marinas 82220a004e7SWill Deacon if (pmd_sect(pmd)) 82320a004e7SWill Deacon return pfn_valid(pmd_pfn(pmd)); 824da6e4cb6SDave Anderson 82520a004e7SWill Deacon ptep = pte_offset_kernel(pmdp, addr); 82620a004e7SWill Deacon pte = READ_ONCE(*ptep); 82720a004e7SWill Deacon if (pte_none(pte)) 828c1cc1552SCatalin Marinas return 0; 829c1cc1552SCatalin Marinas 83020a004e7SWill Deacon return pfn_valid(pte_pfn(pte)); 831c1cc1552SCatalin Marinas } 832bbd6ec60SAnshuman Khandual 833bbd6ec60SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG 834eee07935SAnshuman Khandual static void free_hotplug_page_range(struct page *page, size_t size, 835eee07935SAnshuman Khandual struct vmem_altmap *altmap) 836bbd6ec60SAnshuman Khandual { 837eee07935SAnshuman Khandual if (altmap) { 838eee07935SAnshuman Khandual vmem_altmap_free(altmap, size >> PAGE_SHIFT); 839eee07935SAnshuman Khandual } else { 840bbd6ec60SAnshuman Khandual WARN_ON(PageReserved(page)); 841bbd6ec60SAnshuman Khandual free_pages((unsigned long)page_address(page), get_order(size)); 842bbd6ec60SAnshuman Khandual } 843eee07935SAnshuman Khandual } 844bbd6ec60SAnshuman Khandual 845bbd6ec60SAnshuman Khandual static void free_hotplug_pgtable_page(struct page *page) 846bbd6ec60SAnshuman Khandual { 847eee07935SAnshuman Khandual free_hotplug_page_range(page, PAGE_SIZE, NULL); 848bbd6ec60SAnshuman Khandual } 849bbd6ec60SAnshuman Khandual 850bbd6ec60SAnshuman Khandual static bool pgtable_range_aligned(unsigned long start, unsigned long end, 851bbd6ec60SAnshuman Khandual unsigned long floor, unsigned long ceiling, 852bbd6ec60SAnshuman Khandual unsigned long mask) 853bbd6ec60SAnshuman Khandual { 854bbd6ec60SAnshuman Khandual start &= mask; 855bbd6ec60SAnshuman Khandual if (start < floor) 856bbd6ec60SAnshuman Khandual return false; 857bbd6ec60SAnshuman Khandual 858bbd6ec60SAnshuman Khandual if (ceiling) { 859bbd6ec60SAnshuman Khandual ceiling &= mask; 860bbd6ec60SAnshuman Khandual if (!ceiling) 861bbd6ec60SAnshuman Khandual return false; 862bbd6ec60SAnshuman Khandual } 863bbd6ec60SAnshuman Khandual 864bbd6ec60SAnshuman Khandual if (end - 1 > ceiling - 1) 865bbd6ec60SAnshuman Khandual return false; 866bbd6ec60SAnshuman Khandual return true; 867bbd6ec60SAnshuman Khandual } 868bbd6ec60SAnshuman Khandual 869bbd6ec60SAnshuman Khandual static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, 870eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 871eee07935SAnshuman Khandual struct vmem_altmap *altmap) 872bbd6ec60SAnshuman Khandual { 873bbd6ec60SAnshuman Khandual pte_t *ptep, pte; 874bbd6ec60SAnshuman Khandual 875bbd6ec60SAnshuman Khandual do { 876bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, addr); 877bbd6ec60SAnshuman Khandual pte = READ_ONCE(*ptep); 878bbd6ec60SAnshuman Khandual if (pte_none(pte)) 879bbd6ec60SAnshuman Khandual continue; 880bbd6ec60SAnshuman Khandual 881bbd6ec60SAnshuman Khandual WARN_ON(!pte_present(pte)); 882bbd6ec60SAnshuman Khandual pte_clear(&init_mm, addr, ptep); 883bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 884bbd6ec60SAnshuman Khandual if (free_mapped) 885eee07935SAnshuman Khandual free_hotplug_page_range(pte_page(pte), 886eee07935SAnshuman Khandual PAGE_SIZE, altmap); 887bbd6ec60SAnshuman Khandual } while (addr += PAGE_SIZE, addr < end); 888bbd6ec60SAnshuman Khandual } 889bbd6ec60SAnshuman Khandual 890bbd6ec60SAnshuman Khandual static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr, 891eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 892eee07935SAnshuman Khandual struct vmem_altmap *altmap) 893bbd6ec60SAnshuman Khandual { 894bbd6ec60SAnshuman Khandual unsigned long next; 895bbd6ec60SAnshuman Khandual pmd_t *pmdp, pmd; 896bbd6ec60SAnshuman Khandual 897bbd6ec60SAnshuman Khandual do { 898bbd6ec60SAnshuman Khandual next = pmd_addr_end(addr, end); 899bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, addr); 900bbd6ec60SAnshuman Khandual pmd = READ_ONCE(*pmdp); 901bbd6ec60SAnshuman Khandual if (pmd_none(pmd)) 902bbd6ec60SAnshuman Khandual continue; 903bbd6ec60SAnshuman Khandual 904bbd6ec60SAnshuman Khandual WARN_ON(!pmd_present(pmd)); 905bbd6ec60SAnshuman Khandual if (pmd_sect(pmd)) { 906bbd6ec60SAnshuman Khandual pmd_clear(pmdp); 907bbd6ec60SAnshuman Khandual 908bbd6ec60SAnshuman Khandual /* 909bbd6ec60SAnshuman Khandual * One TLBI should be sufficient here as the PMD_SIZE 910bbd6ec60SAnshuman Khandual * range is mapped with a single block entry. 911bbd6ec60SAnshuman Khandual */ 912bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 913bbd6ec60SAnshuman Khandual if (free_mapped) 914bbd6ec60SAnshuman Khandual free_hotplug_page_range(pmd_page(pmd), 915eee07935SAnshuman Khandual PMD_SIZE, altmap); 916bbd6ec60SAnshuman Khandual continue; 917bbd6ec60SAnshuman Khandual } 918bbd6ec60SAnshuman Khandual WARN_ON(!pmd_table(pmd)); 919eee07935SAnshuman Khandual unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap); 920bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 921bbd6ec60SAnshuman Khandual } 922bbd6ec60SAnshuman Khandual 923bbd6ec60SAnshuman Khandual static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr, 924eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 925eee07935SAnshuman Khandual struct vmem_altmap *altmap) 926bbd6ec60SAnshuman Khandual { 927bbd6ec60SAnshuman Khandual unsigned long next; 928bbd6ec60SAnshuman Khandual pud_t *pudp, pud; 929bbd6ec60SAnshuman Khandual 930bbd6ec60SAnshuman Khandual do { 931bbd6ec60SAnshuman Khandual next = pud_addr_end(addr, end); 932bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, addr); 933bbd6ec60SAnshuman Khandual pud = READ_ONCE(*pudp); 934bbd6ec60SAnshuman Khandual if (pud_none(pud)) 935bbd6ec60SAnshuman Khandual continue; 936bbd6ec60SAnshuman Khandual 937bbd6ec60SAnshuman Khandual WARN_ON(!pud_present(pud)); 938bbd6ec60SAnshuman Khandual if (pud_sect(pud)) { 939bbd6ec60SAnshuman Khandual pud_clear(pudp); 940bbd6ec60SAnshuman Khandual 941bbd6ec60SAnshuman Khandual /* 942bbd6ec60SAnshuman Khandual * One TLBI should be sufficient here as the PUD_SIZE 943bbd6ec60SAnshuman Khandual * range is mapped with a single block entry. 944bbd6ec60SAnshuman Khandual */ 945bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 946bbd6ec60SAnshuman Khandual if (free_mapped) 947bbd6ec60SAnshuman Khandual free_hotplug_page_range(pud_page(pud), 948eee07935SAnshuman Khandual PUD_SIZE, altmap); 949bbd6ec60SAnshuman Khandual continue; 950bbd6ec60SAnshuman Khandual } 951bbd6ec60SAnshuman Khandual WARN_ON(!pud_table(pud)); 952eee07935SAnshuman Khandual unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap); 953bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 954bbd6ec60SAnshuman Khandual } 955bbd6ec60SAnshuman Khandual 956bbd6ec60SAnshuman Khandual static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr, 957eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 958eee07935SAnshuman Khandual struct vmem_altmap *altmap) 959bbd6ec60SAnshuman Khandual { 960bbd6ec60SAnshuman Khandual unsigned long next; 961bbd6ec60SAnshuman Khandual p4d_t *p4dp, p4d; 962bbd6ec60SAnshuman Khandual 963bbd6ec60SAnshuman Khandual do { 964bbd6ec60SAnshuman Khandual next = p4d_addr_end(addr, end); 965bbd6ec60SAnshuman Khandual p4dp = p4d_offset(pgdp, addr); 966bbd6ec60SAnshuman Khandual p4d = READ_ONCE(*p4dp); 967bbd6ec60SAnshuman Khandual if (p4d_none(p4d)) 968bbd6ec60SAnshuman Khandual continue; 969bbd6ec60SAnshuman Khandual 970bbd6ec60SAnshuman Khandual WARN_ON(!p4d_present(p4d)); 971eee07935SAnshuman Khandual unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap); 972bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 973bbd6ec60SAnshuman Khandual } 974bbd6ec60SAnshuman Khandual 975bbd6ec60SAnshuman Khandual static void unmap_hotplug_range(unsigned long addr, unsigned long end, 976eee07935SAnshuman Khandual bool free_mapped, struct vmem_altmap *altmap) 977bbd6ec60SAnshuman Khandual { 978bbd6ec60SAnshuman Khandual unsigned long next; 979bbd6ec60SAnshuman Khandual pgd_t *pgdp, pgd; 980bbd6ec60SAnshuman Khandual 981eee07935SAnshuman Khandual /* 982eee07935SAnshuman Khandual * altmap can only be used as vmemmap mapping backing memory. 983eee07935SAnshuman Khandual * In case the backing memory itself is not being freed, then 984eee07935SAnshuman Khandual * altmap is irrelevant. Warn about this inconsistency when 985eee07935SAnshuman Khandual * encountered. 986eee07935SAnshuman Khandual */ 987eee07935SAnshuman Khandual WARN_ON(!free_mapped && altmap); 988eee07935SAnshuman Khandual 989bbd6ec60SAnshuman Khandual do { 990bbd6ec60SAnshuman Khandual next = pgd_addr_end(addr, end); 991bbd6ec60SAnshuman Khandual pgdp = pgd_offset_k(addr); 992bbd6ec60SAnshuman Khandual pgd = READ_ONCE(*pgdp); 993bbd6ec60SAnshuman Khandual if (pgd_none(pgd)) 994bbd6ec60SAnshuman Khandual continue; 995bbd6ec60SAnshuman Khandual 996bbd6ec60SAnshuman Khandual WARN_ON(!pgd_present(pgd)); 997eee07935SAnshuman Khandual unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap); 998bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 999bbd6ec60SAnshuman Khandual } 1000bbd6ec60SAnshuman Khandual 1001bbd6ec60SAnshuman Khandual static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr, 1002bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1003bbd6ec60SAnshuman Khandual unsigned long ceiling) 1004bbd6ec60SAnshuman Khandual { 1005bbd6ec60SAnshuman Khandual pte_t *ptep, pte; 1006bbd6ec60SAnshuman Khandual unsigned long i, start = addr; 1007bbd6ec60SAnshuman Khandual 1008bbd6ec60SAnshuman Khandual do { 1009bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, addr); 1010bbd6ec60SAnshuman Khandual pte = READ_ONCE(*ptep); 1011bbd6ec60SAnshuman Khandual 1012bbd6ec60SAnshuman Khandual /* 1013bbd6ec60SAnshuman Khandual * This is just a sanity check here which verifies that 1014bbd6ec60SAnshuman Khandual * pte clearing has been done by earlier unmap loops. 1015bbd6ec60SAnshuman Khandual */ 1016bbd6ec60SAnshuman Khandual WARN_ON(!pte_none(pte)); 1017bbd6ec60SAnshuman Khandual } while (addr += PAGE_SIZE, addr < end); 1018bbd6ec60SAnshuman Khandual 1019bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK)) 1020bbd6ec60SAnshuman Khandual return; 1021bbd6ec60SAnshuman Khandual 1022bbd6ec60SAnshuman Khandual /* 1023bbd6ec60SAnshuman Khandual * Check whether we can free the pte 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 ptep = pte_offset_kernel(pmdp, 0UL); 1028bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PTE; i++) { 1029bbd6ec60SAnshuman Khandual if (!pte_none(READ_ONCE(ptep[i]))) 1030bbd6ec60SAnshuman Khandual return; 1031bbd6ec60SAnshuman Khandual } 1032bbd6ec60SAnshuman Khandual 1033bbd6ec60SAnshuman Khandual pmd_clear(pmdp); 1034bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1035bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(ptep)); 1036bbd6ec60SAnshuman Khandual } 1037bbd6ec60SAnshuman Khandual 1038bbd6ec60SAnshuman Khandual static void free_empty_pmd_table(pud_t *pudp, unsigned long addr, 1039bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1040bbd6ec60SAnshuman Khandual unsigned long ceiling) 1041bbd6ec60SAnshuman Khandual { 1042bbd6ec60SAnshuman Khandual pmd_t *pmdp, pmd; 1043bbd6ec60SAnshuman Khandual unsigned long i, next, start = addr; 1044bbd6ec60SAnshuman Khandual 1045bbd6ec60SAnshuman Khandual do { 1046bbd6ec60SAnshuman Khandual next = pmd_addr_end(addr, end); 1047bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, addr); 1048bbd6ec60SAnshuman Khandual pmd = READ_ONCE(*pmdp); 1049bbd6ec60SAnshuman Khandual if (pmd_none(pmd)) 1050bbd6ec60SAnshuman Khandual continue; 1051bbd6ec60SAnshuman Khandual 1052bbd6ec60SAnshuman Khandual WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd)); 1053bbd6ec60SAnshuman Khandual free_empty_pte_table(pmdp, addr, next, floor, ceiling); 1054bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1055bbd6ec60SAnshuman Khandual 1056bbd6ec60SAnshuman Khandual if (CONFIG_PGTABLE_LEVELS <= 2) 1057bbd6ec60SAnshuman Khandual return; 1058bbd6ec60SAnshuman Khandual 1059bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK)) 1060bbd6ec60SAnshuman Khandual return; 1061bbd6ec60SAnshuman Khandual 1062bbd6ec60SAnshuman Khandual /* 1063bbd6ec60SAnshuman Khandual * Check whether we can free the pmd 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 pmdp = pmd_offset(pudp, 0UL); 1068bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PMD; i++) { 1069bbd6ec60SAnshuman Khandual if (!pmd_none(READ_ONCE(pmdp[i]))) 1070bbd6ec60SAnshuman Khandual return; 1071bbd6ec60SAnshuman Khandual } 1072bbd6ec60SAnshuman Khandual 1073bbd6ec60SAnshuman Khandual pud_clear(pudp); 1074bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1075bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(pmdp)); 1076bbd6ec60SAnshuman Khandual } 1077bbd6ec60SAnshuman Khandual 1078bbd6ec60SAnshuman Khandual static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr, 1079bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1080bbd6ec60SAnshuman Khandual unsigned long ceiling) 1081bbd6ec60SAnshuman Khandual { 1082bbd6ec60SAnshuman Khandual pud_t *pudp, pud; 1083bbd6ec60SAnshuman Khandual unsigned long i, next, start = addr; 1084bbd6ec60SAnshuman Khandual 1085bbd6ec60SAnshuman Khandual do { 1086bbd6ec60SAnshuman Khandual next = pud_addr_end(addr, end); 1087bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, addr); 1088bbd6ec60SAnshuman Khandual pud = READ_ONCE(*pudp); 1089bbd6ec60SAnshuman Khandual if (pud_none(pud)) 1090bbd6ec60SAnshuman Khandual continue; 1091bbd6ec60SAnshuman Khandual 1092bbd6ec60SAnshuman Khandual WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud)); 1093bbd6ec60SAnshuman Khandual free_empty_pmd_table(pudp, addr, next, floor, ceiling); 1094bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1095bbd6ec60SAnshuman Khandual 1096bbd6ec60SAnshuman Khandual if (CONFIG_PGTABLE_LEVELS <= 3) 1097bbd6ec60SAnshuman Khandual return; 1098bbd6ec60SAnshuman Khandual 1099bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK)) 1100bbd6ec60SAnshuman Khandual return; 1101bbd6ec60SAnshuman Khandual 1102bbd6ec60SAnshuman Khandual /* 1103bbd6ec60SAnshuman Khandual * Check whether we can free the pud page if the rest of the 1104bbd6ec60SAnshuman Khandual * entries are empty. Overlap with other regions have been 1105bbd6ec60SAnshuman Khandual * handled by the floor/ceiling check. 1106bbd6ec60SAnshuman Khandual */ 1107bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, 0UL); 1108bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PUD; i++) { 1109bbd6ec60SAnshuman Khandual if (!pud_none(READ_ONCE(pudp[i]))) 1110bbd6ec60SAnshuman Khandual return; 1111bbd6ec60SAnshuman Khandual } 1112bbd6ec60SAnshuman Khandual 1113bbd6ec60SAnshuman Khandual p4d_clear(p4dp); 1114bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1115bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(pudp)); 1116bbd6ec60SAnshuman Khandual } 1117bbd6ec60SAnshuman Khandual 1118bbd6ec60SAnshuman Khandual static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr, 1119bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1120bbd6ec60SAnshuman Khandual unsigned long ceiling) 1121bbd6ec60SAnshuman Khandual { 1122bbd6ec60SAnshuman Khandual unsigned long next; 1123bbd6ec60SAnshuman Khandual p4d_t *p4dp, p4d; 1124bbd6ec60SAnshuman Khandual 1125bbd6ec60SAnshuman Khandual do { 1126bbd6ec60SAnshuman Khandual next = p4d_addr_end(addr, end); 1127bbd6ec60SAnshuman Khandual p4dp = p4d_offset(pgdp, addr); 1128bbd6ec60SAnshuman Khandual p4d = READ_ONCE(*p4dp); 1129bbd6ec60SAnshuman Khandual if (p4d_none(p4d)) 1130bbd6ec60SAnshuman Khandual continue; 1131bbd6ec60SAnshuman Khandual 1132bbd6ec60SAnshuman Khandual WARN_ON(!p4d_present(p4d)); 1133bbd6ec60SAnshuman Khandual free_empty_pud_table(p4dp, addr, next, floor, ceiling); 1134bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1135bbd6ec60SAnshuman Khandual } 1136bbd6ec60SAnshuman Khandual 1137bbd6ec60SAnshuman Khandual static void free_empty_tables(unsigned long addr, unsigned long end, 1138bbd6ec60SAnshuman Khandual unsigned long floor, unsigned long ceiling) 1139bbd6ec60SAnshuman Khandual { 1140bbd6ec60SAnshuman Khandual unsigned long next; 1141bbd6ec60SAnshuman Khandual pgd_t *pgdp, pgd; 1142bbd6ec60SAnshuman Khandual 1143bbd6ec60SAnshuman Khandual do { 1144bbd6ec60SAnshuman Khandual next = pgd_addr_end(addr, end); 1145bbd6ec60SAnshuman Khandual pgdp = pgd_offset_k(addr); 1146bbd6ec60SAnshuman Khandual pgd = READ_ONCE(*pgdp); 1147bbd6ec60SAnshuman Khandual if (pgd_none(pgd)) 1148bbd6ec60SAnshuman Khandual continue; 1149bbd6ec60SAnshuman Khandual 1150bbd6ec60SAnshuman Khandual WARN_ON(!pgd_present(pgd)); 1151bbd6ec60SAnshuman Khandual free_empty_p4d_table(pgdp, addr, next, floor, ceiling); 1152bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1153bbd6ec60SAnshuman Khandual } 1154bbd6ec60SAnshuman Khandual #endif 1155bbd6ec60SAnshuman Khandual 11562062d44dSAnshuman Khandual #if !ARM64_KERNEL_USES_PMD_MAPS 11577b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 11587b73d978SChristoph Hellwig struct vmem_altmap *altmap) 1159c1cc1552SCatalin Marinas { 1160edb739eeSAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1161eee07935SAnshuman Khandual return vmemmap_populate_basepages(start, end, node, altmap); 1162c1cc1552SCatalin Marinas } 11632062d44dSAnshuman Khandual #else /* !ARM64_KERNEL_USES_PMD_MAPS */ 11647b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 11657b73d978SChristoph Hellwig struct vmem_altmap *altmap) 1166c1cc1552SCatalin Marinas { 11670aad818bSJohannes Weiner unsigned long addr = start; 1168c1cc1552SCatalin Marinas unsigned long next; 116920a004e7SWill Deacon pgd_t *pgdp; 1170e9f63768SMike Rapoport p4d_t *p4dp; 117120a004e7SWill Deacon pud_t *pudp; 117220a004e7SWill Deacon pmd_t *pmdp; 1173c1cc1552SCatalin Marinas 1174edb739eeSAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1175c1cc1552SCatalin Marinas do { 1176c1cc1552SCatalin Marinas next = pmd_addr_end(addr, end); 1177c1cc1552SCatalin Marinas 117820a004e7SWill Deacon pgdp = vmemmap_pgd_populate(addr, node); 117920a004e7SWill Deacon if (!pgdp) 1180c1cc1552SCatalin Marinas return -ENOMEM; 1181c1cc1552SCatalin Marinas 1182e9f63768SMike Rapoport p4dp = vmemmap_p4d_populate(pgdp, addr, node); 1183e9f63768SMike Rapoport if (!p4dp) 1184e9f63768SMike Rapoport return -ENOMEM; 1185e9f63768SMike Rapoport 1186e9f63768SMike Rapoport pudp = vmemmap_pud_populate(p4dp, addr, node); 118720a004e7SWill Deacon if (!pudp) 1188c1cc1552SCatalin Marinas return -ENOMEM; 1189c1cc1552SCatalin Marinas 119020a004e7SWill Deacon pmdp = pmd_offset(pudp, addr); 119120a004e7SWill Deacon if (pmd_none(READ_ONCE(*pmdp))) { 1192c1cc1552SCatalin Marinas void *p = NULL; 1193c1cc1552SCatalin Marinas 1194eee07935SAnshuman Khandual p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap); 11959f84f39fSSudarshan Rajagopalan if (!p) { 11969f84f39fSSudarshan Rajagopalan if (vmemmap_populate_basepages(addr, next, node, altmap)) 1197c1cc1552SCatalin Marinas return -ENOMEM; 11989f84f39fSSudarshan Rajagopalan continue; 11999f84f39fSSudarshan Rajagopalan } 1200c1cc1552SCatalin Marinas 120120a004e7SWill Deacon pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL)); 1202c1cc1552SCatalin Marinas } else 120320a004e7SWill Deacon vmemmap_verify((pte_t *)pmdp, node, addr, next); 1204c1cc1552SCatalin Marinas } while (addr = next, addr != end); 1205c1cc1552SCatalin Marinas 1206c1cc1552SCatalin Marinas return 0; 1207c1cc1552SCatalin Marinas } 12082062d44dSAnshuman Khandual #endif /* !ARM64_KERNEL_USES_PMD_MAPS */ 120940221c73SAnshuman Khandual 121040221c73SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG 121124b6d416SChristoph Hellwig void vmemmap_free(unsigned long start, unsigned long end, 121224b6d416SChristoph Hellwig struct vmem_altmap *altmap) 12130197518cSTang Chen { 1214bbd6ec60SAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1215bbd6ec60SAnshuman Khandual 1216eee07935SAnshuman Khandual unmap_hotplug_range(start, end, true, altmap); 1217bbd6ec60SAnshuman Khandual free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END); 12180197518cSTang Chen } 121940221c73SAnshuman Khandual #endif /* CONFIG_MEMORY_HOTPLUG */ 1220af86e597SLaura Abbott 1221af86e597SLaura Abbott static inline pud_t *fixmap_pud(unsigned long addr) 1222af86e597SLaura Abbott { 122320a004e7SWill Deacon pgd_t *pgdp = pgd_offset_k(addr); 1224e9f63768SMike Rapoport p4d_t *p4dp = p4d_offset(pgdp, addr); 1225e9f63768SMike Rapoport p4d_t p4d = READ_ONCE(*p4dp); 1226af86e597SLaura Abbott 1227e9f63768SMike Rapoport BUG_ON(p4d_none(p4d) || p4d_bad(p4d)); 1228af86e597SLaura Abbott 1229e9f63768SMike Rapoport return pud_offset_kimg(p4dp, addr); 1230af86e597SLaura Abbott } 1231af86e597SLaura Abbott 1232af86e597SLaura Abbott static inline pmd_t *fixmap_pmd(unsigned long addr) 1233af86e597SLaura Abbott { 123420a004e7SWill Deacon pud_t *pudp = fixmap_pud(addr); 123520a004e7SWill Deacon pud_t pud = READ_ONCE(*pudp); 1236af86e597SLaura Abbott 123720a004e7SWill Deacon BUG_ON(pud_none(pud) || pud_bad(pud)); 1238af86e597SLaura Abbott 123920a004e7SWill Deacon return pmd_offset_kimg(pudp, addr); 1240af86e597SLaura Abbott } 1241af86e597SLaura Abbott 1242af86e597SLaura Abbott static inline pte_t *fixmap_pte(unsigned long addr) 1243af86e597SLaura Abbott { 1244157962f5SArd Biesheuvel return &bm_pte[pte_index(addr)]; 1245af86e597SLaura Abbott } 1246af86e597SLaura Abbott 12472077be67SLaura Abbott /* 12482077be67SLaura Abbott * The p*d_populate functions call virt_to_phys implicitly so they can't be used 12492077be67SLaura Abbott * directly on kernel symbols (bm_p*d). This function is called too early to use 12502077be67SLaura Abbott * lm_alias so __p*d_populate functions must be used to populate with the 12512077be67SLaura Abbott * physical address from __pa_symbol. 12522077be67SLaura Abbott */ 1253af86e597SLaura Abbott void __init early_fixmap_init(void) 1254af86e597SLaura Abbott { 1255e9f63768SMike Rapoport pgd_t *pgdp; 1256e9f63768SMike Rapoport p4d_t *p4dp, p4d; 125720a004e7SWill Deacon pud_t *pudp; 125820a004e7SWill Deacon pmd_t *pmdp; 1259af86e597SLaura Abbott unsigned long addr = FIXADDR_START; 1260af86e597SLaura Abbott 126120a004e7SWill Deacon pgdp = pgd_offset_k(addr); 1262e9f63768SMike Rapoport p4dp = p4d_offset(pgdp, addr); 1263e9f63768SMike Rapoport p4d = READ_ONCE(*p4dp); 1264f80fb3a3SArd Biesheuvel if (CONFIG_PGTABLE_LEVELS > 3 && 1265e9f63768SMike Rapoport !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) { 1266f9040773SArd Biesheuvel /* 1267f9040773SArd Biesheuvel * We only end up here if the kernel mapping and the fixmap 1268f9040773SArd Biesheuvel * share the top level pgd entry, which should only happen on 1269f9040773SArd Biesheuvel * 16k/4 levels configurations. 1270f9040773SArd Biesheuvel */ 1271f9040773SArd Biesheuvel BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES)); 1272e9f63768SMike Rapoport pudp = pud_offset_kimg(p4dp, addr); 1273f9040773SArd Biesheuvel } else { 1274e9f63768SMike Rapoport if (p4d_none(p4d)) 1275c1fd78a7SArd Biesheuvel __p4d_populate(p4dp, __pa_symbol(bm_pud), P4D_TYPE_TABLE); 127620a004e7SWill Deacon pudp = fixmap_pud(addr); 1277f9040773SArd Biesheuvel } 127820a004e7SWill Deacon if (pud_none(READ_ONCE(*pudp))) 1279c1fd78a7SArd Biesheuvel __pud_populate(pudp, __pa_symbol(bm_pmd), PUD_TYPE_TABLE); 128020a004e7SWill Deacon pmdp = fixmap_pmd(addr); 128120a004e7SWill Deacon __pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE); 1282af86e597SLaura Abbott 1283af86e597SLaura Abbott /* 1284af86e597SLaura Abbott * The boot-ioremap range spans multiple pmds, for which 1285157962f5SArd Biesheuvel * we are not prepared: 1286af86e597SLaura Abbott */ 1287af86e597SLaura Abbott BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) 1288af86e597SLaura Abbott != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); 1289af86e597SLaura Abbott 129020a004e7SWill Deacon if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN))) 129120a004e7SWill Deacon || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) { 1292af86e597SLaura Abbott WARN_ON(1); 129320a004e7SWill Deacon pr_warn("pmdp %p != %p, %p\n", 129420a004e7SWill Deacon pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)), 1295af86e597SLaura Abbott fixmap_pmd(fix_to_virt(FIX_BTMAP_END))); 1296af86e597SLaura Abbott pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", 1297af86e597SLaura Abbott fix_to_virt(FIX_BTMAP_BEGIN)); 1298af86e597SLaura Abbott pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n", 1299af86e597SLaura Abbott fix_to_virt(FIX_BTMAP_END)); 1300af86e597SLaura Abbott 1301af86e597SLaura Abbott pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END); 1302af86e597SLaura Abbott pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN); 1303af86e597SLaura Abbott } 1304af86e597SLaura Abbott } 1305af86e597SLaura Abbott 130618b4b276SJames Morse /* 130718b4b276SJames Morse * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we 130818b4b276SJames Morse * ever need to use IPIs for TLB broadcasting, then we're in trouble here. 130918b4b276SJames Morse */ 1310af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx, 1311af86e597SLaura Abbott phys_addr_t phys, pgprot_t flags) 1312af86e597SLaura Abbott { 1313af86e597SLaura Abbott unsigned long addr = __fix_to_virt(idx); 131420a004e7SWill Deacon pte_t *ptep; 1315af86e597SLaura Abbott 1316b63dbef9SMark Rutland BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); 1317af86e597SLaura Abbott 131820a004e7SWill Deacon ptep = fixmap_pte(addr); 1319af86e597SLaura Abbott 1320af86e597SLaura Abbott if (pgprot_val(flags)) { 132120a004e7SWill Deacon set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags)); 1322af86e597SLaura Abbott } else { 132320a004e7SWill Deacon pte_clear(&init_mm, addr, ptep); 1324af86e597SLaura Abbott flush_tlb_kernel_range(addr, addr+PAGE_SIZE); 1325af86e597SLaura Abbott } 1326af86e597SLaura Abbott } 132761bd93ceSArd Biesheuvel 1328e112b032SHsin-Yi Wang void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) 132961bd93ceSArd Biesheuvel { 133061bd93ceSArd Biesheuvel const u64 dt_virt_base = __fix_to_virt(FIX_FDT); 1331f80fb3a3SArd Biesheuvel int offset; 133261bd93ceSArd Biesheuvel void *dt_virt; 133361bd93ceSArd Biesheuvel 133461bd93ceSArd Biesheuvel /* 133561bd93ceSArd Biesheuvel * Check whether the physical FDT address is set and meets the minimum 133661bd93ceSArd Biesheuvel * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be 133704a84810SArd Biesheuvel * at least 8 bytes so that we can always access the magic and size 133804a84810SArd Biesheuvel * fields of the FDT header after mapping the first chunk, double check 133904a84810SArd Biesheuvel * here if that is indeed the case. 134061bd93ceSArd Biesheuvel */ 134161bd93ceSArd Biesheuvel BUILD_BUG_ON(MIN_FDT_ALIGN < 8); 134261bd93ceSArd Biesheuvel if (!dt_phys || dt_phys % MIN_FDT_ALIGN) 134361bd93ceSArd Biesheuvel return NULL; 134461bd93ceSArd Biesheuvel 134561bd93ceSArd Biesheuvel /* 134661bd93ceSArd Biesheuvel * Make sure that the FDT region can be mapped without the need to 134761bd93ceSArd Biesheuvel * allocate additional translation table pages, so that it is safe 1348132233a7SLaura Abbott * to call create_mapping_noalloc() this early. 134961bd93ceSArd Biesheuvel * 135061bd93ceSArd Biesheuvel * On 64k pages, the FDT will be mapped using PTEs, so we need to 135161bd93ceSArd Biesheuvel * be in the same PMD as the rest of the fixmap. 135261bd93ceSArd Biesheuvel * On 4k pages, we'll use section mappings for the FDT so we only 135361bd93ceSArd Biesheuvel * have to be in the same PUD. 135461bd93ceSArd Biesheuvel */ 135561bd93ceSArd Biesheuvel BUILD_BUG_ON(dt_virt_base % SZ_2M); 135661bd93ceSArd Biesheuvel 1357b433dce0SSuzuki K. Poulose BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT != 1358b433dce0SSuzuki K. Poulose __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT); 135961bd93ceSArd Biesheuvel 1360b433dce0SSuzuki K. Poulose offset = dt_phys % SWAPPER_BLOCK_SIZE; 136161bd93ceSArd Biesheuvel dt_virt = (void *)dt_virt_base + offset; 136261bd93ceSArd Biesheuvel 136361bd93ceSArd Biesheuvel /* map the first chunk so we can read the size from the header */ 1364132233a7SLaura Abbott create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), 1365132233a7SLaura Abbott dt_virt_base, SWAPPER_BLOCK_SIZE, prot); 136661bd93ceSArd Biesheuvel 136704a84810SArd Biesheuvel if (fdt_magic(dt_virt) != FDT_MAGIC) 136861bd93ceSArd Biesheuvel return NULL; 136961bd93ceSArd Biesheuvel 1370f80fb3a3SArd Biesheuvel *size = fdt_totalsize(dt_virt); 1371f80fb3a3SArd Biesheuvel if (*size > MAX_FDT_SIZE) 137261bd93ceSArd Biesheuvel return NULL; 137361bd93ceSArd Biesheuvel 1374f80fb3a3SArd Biesheuvel if (offset + *size > SWAPPER_BLOCK_SIZE) 1375132233a7SLaura Abbott create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base, 1376f80fb3a3SArd Biesheuvel round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot); 1377f80fb3a3SArd Biesheuvel 1378f80fb3a3SArd Biesheuvel return dt_virt; 1379f80fb3a3SArd Biesheuvel } 1380f80fb3a3SArd Biesheuvel 138120a004e7SWill Deacon int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot) 1382324420bfSArd Biesheuvel { 1383f7f0097aSAnshuman Khandual pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot)); 138415122ee2SWill Deacon 138582034c23SLaura Abbott /* Only allow permission changes for now */ 138682034c23SLaura Abbott if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)), 138782034c23SLaura Abbott pud_val(new_pud))) 138815122ee2SWill Deacon return 0; 138915122ee2SWill Deacon 139087dedf7cSAnshuman Khandual VM_BUG_ON(phys & ~PUD_MASK); 139182034c23SLaura Abbott set_pud(pudp, new_pud); 1392324420bfSArd Biesheuvel return 1; 1393324420bfSArd Biesheuvel } 1394324420bfSArd Biesheuvel 139520a004e7SWill Deacon int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot) 1396324420bfSArd Biesheuvel { 1397f7f0097aSAnshuman Khandual pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot)); 139815122ee2SWill Deacon 139982034c23SLaura Abbott /* Only allow permission changes for now */ 140082034c23SLaura Abbott if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)), 140182034c23SLaura Abbott pmd_val(new_pmd))) 140215122ee2SWill Deacon return 0; 140315122ee2SWill Deacon 140487dedf7cSAnshuman Khandual VM_BUG_ON(phys & ~PMD_MASK); 140582034c23SLaura Abbott set_pmd(pmdp, new_pmd); 1406324420bfSArd Biesheuvel return 1; 1407324420bfSArd Biesheuvel } 1408324420bfSArd Biesheuvel 1409d8a71905SJonathan Marek int pud_clear_huge(pud_t *pudp) 1410d8a71905SJonathan Marek { 1411d8a71905SJonathan Marek if (!pud_sect(READ_ONCE(*pudp))) 1412d8a71905SJonathan Marek return 0; 1413d8a71905SJonathan Marek pud_clear(pudp); 1414d8a71905SJonathan Marek return 1; 1415d8a71905SJonathan Marek } 1416d8a71905SJonathan Marek 141720a004e7SWill Deacon int pmd_clear_huge(pmd_t *pmdp) 1418324420bfSArd Biesheuvel { 141920a004e7SWill Deacon if (!pmd_sect(READ_ONCE(*pmdp))) 1420324420bfSArd Biesheuvel return 0; 142120a004e7SWill Deacon pmd_clear(pmdp); 1422324420bfSArd Biesheuvel return 1; 1423324420bfSArd Biesheuvel } 1424b6bdb751SToshi Kani 1425ec28bb9cSChintan Pandya int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr) 1426b6bdb751SToshi Kani { 1427ec28bb9cSChintan Pandya pte_t *table; 1428ec28bb9cSChintan Pandya pmd_t pmd; 1429ec28bb9cSChintan Pandya 1430ec28bb9cSChintan Pandya pmd = READ_ONCE(*pmdp); 1431ec28bb9cSChintan Pandya 1432fac880c7SMark Rutland if (!pmd_table(pmd)) { 14339c006972SWill Deacon VM_WARN_ON(1); 1434ec28bb9cSChintan Pandya return 1; 1435b6bdb751SToshi Kani } 1436b6bdb751SToshi Kani 1437ec28bb9cSChintan Pandya table = pte_offset_kernel(pmdp, addr); 1438ec28bb9cSChintan Pandya pmd_clear(pmdp); 1439ec28bb9cSChintan Pandya __flush_tlb_kernel_pgtable(addr); 1440ec28bb9cSChintan Pandya pte_free_kernel(NULL, table); 1441ec28bb9cSChintan Pandya return 1; 1442ec28bb9cSChintan Pandya } 1443ec28bb9cSChintan Pandya 1444ec28bb9cSChintan Pandya int pud_free_pmd_page(pud_t *pudp, unsigned long addr) 1445b6bdb751SToshi Kani { 1446ec28bb9cSChintan Pandya pmd_t *table; 1447ec28bb9cSChintan Pandya pmd_t *pmdp; 1448ec28bb9cSChintan Pandya pud_t pud; 1449ec28bb9cSChintan Pandya unsigned long next, end; 1450ec28bb9cSChintan Pandya 1451ec28bb9cSChintan Pandya pud = READ_ONCE(*pudp); 1452ec28bb9cSChintan Pandya 1453fac880c7SMark Rutland if (!pud_table(pud)) { 14549c006972SWill Deacon VM_WARN_ON(1); 1455ec28bb9cSChintan Pandya return 1; 1456ec28bb9cSChintan Pandya } 1457ec28bb9cSChintan Pandya 1458ec28bb9cSChintan Pandya table = pmd_offset(pudp, addr); 1459ec28bb9cSChintan Pandya pmdp = table; 1460ec28bb9cSChintan Pandya next = addr; 1461ec28bb9cSChintan Pandya end = addr + PUD_SIZE; 1462ec28bb9cSChintan Pandya do { 1463ec28bb9cSChintan Pandya pmd_free_pte_page(pmdp, next); 1464ec28bb9cSChintan Pandya } while (pmdp++, next += PMD_SIZE, next != end); 1465ec28bb9cSChintan Pandya 1466ec28bb9cSChintan Pandya pud_clear(pudp); 1467ec28bb9cSChintan Pandya __flush_tlb_kernel_pgtable(addr); 1468ec28bb9cSChintan Pandya pmd_free(NULL, table); 1469ec28bb9cSChintan Pandya return 1; 1470b6bdb751SToshi Kani } 14714ab21506SRobin Murphy 14724ab21506SRobin Murphy #ifdef CONFIG_MEMORY_HOTPLUG 1473bbd6ec60SAnshuman Khandual static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size) 1474bbd6ec60SAnshuman Khandual { 1475bbd6ec60SAnshuman Khandual unsigned long end = start + size; 1476bbd6ec60SAnshuman Khandual 1477bbd6ec60SAnshuman Khandual WARN_ON(pgdir != init_mm.pgd); 1478bbd6ec60SAnshuman Khandual WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END)); 1479bbd6ec60SAnshuman Khandual 1480eee07935SAnshuman Khandual unmap_hotplug_range(start, end, false, NULL); 1481bbd6ec60SAnshuman Khandual free_empty_tables(start, end, PAGE_OFFSET, PAGE_END); 1482bbd6ec60SAnshuman Khandual } 1483bbd6ec60SAnshuman Khandual 148403aaf83fSAnshuman Khandual struct range arch_get_mappable_range(void) 148558284a90SAnshuman Khandual { 148603aaf83fSAnshuman Khandual struct range mhp_range; 1487ee7febceSPavel Tatashin u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual)); 1488ee7febceSPavel Tatashin u64 end_linear_pa = __pa(PAGE_END - 1); 1489ee7febceSPavel Tatashin 1490ee7febceSPavel Tatashin if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 1491ee7febceSPavel Tatashin /* 1492ee7febceSPavel Tatashin * Check for a wrap, it is possible because of randomized linear 1493ee7febceSPavel Tatashin * mapping the start physical address is actually bigger than 1494ee7febceSPavel Tatashin * the end physical address. In this case set start to zero 1495ee7febceSPavel Tatashin * because [0, end_linear_pa] range must still be able to cover 1496ee7febceSPavel Tatashin * all addressable physical addresses. 1497ee7febceSPavel Tatashin */ 1498ee7febceSPavel Tatashin if (start_linear_pa > end_linear_pa) 1499ee7febceSPavel Tatashin start_linear_pa = 0; 1500ee7febceSPavel Tatashin } 1501ee7febceSPavel Tatashin 1502ee7febceSPavel Tatashin WARN_ON(start_linear_pa > end_linear_pa); 150303aaf83fSAnshuman Khandual 150458284a90SAnshuman Khandual /* 150558284a90SAnshuman Khandual * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)] 150658284a90SAnshuman Khandual * accommodating both its ends but excluding PAGE_END. Max physical 150758284a90SAnshuman Khandual * range which can be mapped inside this linear mapping range, must 150858284a90SAnshuman Khandual * also be derived from its end points. 150958284a90SAnshuman Khandual */ 1510ee7febceSPavel Tatashin mhp_range.start = start_linear_pa; 1511ee7febceSPavel Tatashin mhp_range.end = end_linear_pa; 1512ee7febceSPavel Tatashin 151303aaf83fSAnshuman Khandual return mhp_range; 151458284a90SAnshuman Khandual } 151558284a90SAnshuman Khandual 1516940519f0SMichal Hocko int arch_add_memory(int nid, u64 start, u64 size, 1517f5637d3bSLogan Gunthorpe struct mhp_params *params) 15184ab21506SRobin Murphy { 151987143f40SArd Biesheuvel int ret, flags = NO_EXEC_MAPPINGS; 15204ab21506SRobin Murphy 152103aaf83fSAnshuman Khandual VM_BUG_ON(!mhp_range_allowed(start, size, true)); 1522840b2398SMarco Elver 1523840b2398SMarco Elver /* 1524840b2398SMarco Elver * KFENCE requires linear map to be mapped at page granularity, so that 1525840b2398SMarco Elver * it is possible to protect/unprotect single pages in the KFENCE pool. 1526840b2398SMarco Elver */ 15276d47c23bSMike Rapoport if (can_set_direct_map() || IS_ENABLED(CONFIG_KFENCE)) 152887143f40SArd Biesheuvel flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 15294ab21506SRobin Murphy 15304ab21506SRobin Murphy __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start), 1531bfeb022fSLogan Gunthorpe size, params->pgprot, __pgd_pgtable_alloc, 1532bfeb022fSLogan Gunthorpe flags); 15334ab21506SRobin Murphy 153416993c0fSDan Williams memblock_clear_nomap(start, size); 153516993c0fSDan Williams 1536bbd6ec60SAnshuman Khandual ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT, 1537f5637d3bSLogan Gunthorpe params); 1538bbd6ec60SAnshuman Khandual if (ret) 1539bbd6ec60SAnshuman Khandual __remove_pgd_mapping(swapper_pg_dir, 1540bbd6ec60SAnshuman Khandual __phys_to_virt(start), size); 15418fac67caSSudarshan Rajagopalan else { 15428fac67caSSudarshan Rajagopalan max_pfn = PFN_UP(start + size); 15438fac67caSSudarshan Rajagopalan max_low_pfn = max_pfn; 15448fac67caSSudarshan Rajagopalan } 15458fac67caSSudarshan Rajagopalan 1546bbd6ec60SAnshuman Khandual return ret; 15474ab21506SRobin Murphy } 1548bbd6ec60SAnshuman Khandual 154965a2aa5fSDavid Hildenbrand void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) 155022eb6346SDavid Hildenbrand { 155122eb6346SDavid Hildenbrand unsigned long start_pfn = start >> PAGE_SHIFT; 155222eb6346SDavid Hildenbrand unsigned long nr_pages = size >> PAGE_SHIFT; 155322eb6346SDavid Hildenbrand 1554feee6b29SDavid Hildenbrand __remove_pages(start_pfn, nr_pages, altmap); 1555bbd6ec60SAnshuman Khandual __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size); 155622eb6346SDavid Hildenbrand } 1557bbd6ec60SAnshuman Khandual 1558bbd6ec60SAnshuman Khandual /* 1559bbd6ec60SAnshuman Khandual * This memory hotplug notifier helps prevent boot memory from being 1560bbd6ec60SAnshuman Khandual * inadvertently removed as it blocks pfn range offlining process in 1561bbd6ec60SAnshuman Khandual * __offline_pages(). Hence this prevents both offlining as well as 1562bbd6ec60SAnshuman Khandual * removal process for boot memory which is initially always online. 1563bbd6ec60SAnshuman Khandual * In future if and when boot memory could be removed, this notifier 1564bbd6ec60SAnshuman Khandual * should be dropped and free_hotplug_page_range() should handle any 1565bbd6ec60SAnshuman Khandual * reserved pages allocated during boot. 1566bbd6ec60SAnshuman Khandual */ 1567bbd6ec60SAnshuman Khandual static int prevent_bootmem_remove_notifier(struct notifier_block *nb, 1568bbd6ec60SAnshuman Khandual unsigned long action, void *data) 1569bbd6ec60SAnshuman Khandual { 1570bbd6ec60SAnshuman Khandual struct mem_section *ms; 1571bbd6ec60SAnshuman Khandual struct memory_notify *arg = data; 1572bbd6ec60SAnshuman Khandual unsigned long end_pfn = arg->start_pfn + arg->nr_pages; 1573bbd6ec60SAnshuman Khandual unsigned long pfn = arg->start_pfn; 1574bbd6ec60SAnshuman Khandual 15759fb3d4a3SAnshuman Khandual if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE)) 1576bbd6ec60SAnshuman Khandual return NOTIFY_OK; 1577bbd6ec60SAnshuman Khandual 1578bbd6ec60SAnshuman Khandual for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 15799fb3d4a3SAnshuman Khandual unsigned long start = PFN_PHYS(pfn); 15809fb3d4a3SAnshuman Khandual unsigned long end = start + (1UL << PA_SECTION_SHIFT); 15819fb3d4a3SAnshuman Khandual 1582bbd6ec60SAnshuman Khandual ms = __pfn_to_section(pfn); 15839fb3d4a3SAnshuman Khandual if (!early_section(ms)) 15849fb3d4a3SAnshuman Khandual continue; 15859fb3d4a3SAnshuman Khandual 15869fb3d4a3SAnshuman Khandual if (action == MEM_GOING_OFFLINE) { 15879fb3d4a3SAnshuman Khandual /* 15889fb3d4a3SAnshuman Khandual * Boot memory removal is not supported. Prevent 15899fb3d4a3SAnshuman Khandual * it via blocking any attempted offline request 15909fb3d4a3SAnshuman Khandual * for the boot memory and just report it. 15919fb3d4a3SAnshuman Khandual */ 15929fb3d4a3SAnshuman Khandual pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end); 1593bbd6ec60SAnshuman Khandual return NOTIFY_BAD; 15949fb3d4a3SAnshuman Khandual } else if (action == MEM_OFFLINE) { 15959fb3d4a3SAnshuman Khandual /* 15969fb3d4a3SAnshuman Khandual * This should have never happened. Boot memory 15979fb3d4a3SAnshuman Khandual * offlining should have been prevented by this 15989fb3d4a3SAnshuman Khandual * very notifier. Probably some memory removal 15999fb3d4a3SAnshuman Khandual * procedure might have changed which would then 16009fb3d4a3SAnshuman Khandual * require further debug. 16019fb3d4a3SAnshuman Khandual */ 16029fb3d4a3SAnshuman Khandual pr_err("Boot memory [%lx %lx] offlined\n", start, end); 16039fb3d4a3SAnshuman Khandual 16049fb3d4a3SAnshuman Khandual /* 16059fb3d4a3SAnshuman Khandual * Core memory hotplug does not process a return 16069fb3d4a3SAnshuman Khandual * code from the notifier for MEM_OFFLINE events. 16079fb3d4a3SAnshuman Khandual * The error condition has been reported. Return 16089fb3d4a3SAnshuman Khandual * from here as if ignored. 16099fb3d4a3SAnshuman Khandual */ 16109fb3d4a3SAnshuman Khandual return NOTIFY_DONE; 16119fb3d4a3SAnshuman Khandual } 1612bbd6ec60SAnshuman Khandual } 1613bbd6ec60SAnshuman Khandual return NOTIFY_OK; 1614bbd6ec60SAnshuman Khandual } 1615bbd6ec60SAnshuman Khandual 1616bbd6ec60SAnshuman Khandual static struct notifier_block prevent_bootmem_remove_nb = { 1617bbd6ec60SAnshuman Khandual .notifier_call = prevent_bootmem_remove_notifier, 1618bbd6ec60SAnshuman Khandual }; 1619bbd6ec60SAnshuman Khandual 1620fdd99a41SAnshuman Khandual /* 1621fdd99a41SAnshuman Khandual * This ensures that boot memory sections on the platform are online 1622fdd99a41SAnshuman Khandual * from early boot. Memory sections could not be prevented from being 1623fdd99a41SAnshuman Khandual * offlined, unless for some reason they are not online to begin with. 1624fdd99a41SAnshuman Khandual * This helps validate the basic assumption on which the above memory 1625fdd99a41SAnshuman Khandual * event notifier works to prevent boot memory section offlining and 1626fdd99a41SAnshuman Khandual * its possible removal. 1627fdd99a41SAnshuman Khandual */ 1628fdd99a41SAnshuman Khandual static void validate_bootmem_online(void) 1629fdd99a41SAnshuman Khandual { 1630fdd99a41SAnshuman Khandual phys_addr_t start, end, addr; 1631fdd99a41SAnshuman Khandual struct mem_section *ms; 1632fdd99a41SAnshuman Khandual u64 i; 1633fdd99a41SAnshuman Khandual 1634fdd99a41SAnshuman Khandual /* 1635fdd99a41SAnshuman Khandual * Scanning across all memblock might be expensive 1636fdd99a41SAnshuman Khandual * on some big memory systems. Hence enable this 1637fdd99a41SAnshuman Khandual * validation only with DEBUG_VM. 1638fdd99a41SAnshuman Khandual */ 1639fdd99a41SAnshuman Khandual if (!IS_ENABLED(CONFIG_DEBUG_VM)) 1640fdd99a41SAnshuman Khandual return; 1641fdd99a41SAnshuman Khandual 1642fdd99a41SAnshuman Khandual for_each_mem_range(i, &start, &end) { 1643fdd99a41SAnshuman Khandual for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) { 1644fdd99a41SAnshuman Khandual ms = __pfn_to_section(PHYS_PFN(addr)); 1645fdd99a41SAnshuman Khandual 1646fdd99a41SAnshuman Khandual /* 1647fdd99a41SAnshuman Khandual * All memory ranges in the system at this point 1648fdd99a41SAnshuman Khandual * should have been marked as early sections. 1649fdd99a41SAnshuman Khandual */ 1650fdd99a41SAnshuman Khandual WARN_ON(!early_section(ms)); 1651fdd99a41SAnshuman Khandual 1652fdd99a41SAnshuman Khandual /* 1653fdd99a41SAnshuman Khandual * Memory notifier mechanism here to prevent boot 1654fdd99a41SAnshuman Khandual * memory offlining depends on the fact that each 1655fdd99a41SAnshuman Khandual * early section memory on the system is initially 1656fdd99a41SAnshuman Khandual * online. Otherwise a given memory section which 1657fdd99a41SAnshuman Khandual * is already offline will be overlooked and can 1658fdd99a41SAnshuman Khandual * be removed completely. Call out such sections. 1659fdd99a41SAnshuman Khandual */ 1660fdd99a41SAnshuman Khandual if (!online_section(ms)) 1661fdd99a41SAnshuman Khandual pr_err("Boot memory [%llx %llx] is offline, can be removed\n", 1662fdd99a41SAnshuman Khandual addr, addr + (1UL << PA_SECTION_SHIFT)); 1663fdd99a41SAnshuman Khandual } 1664fdd99a41SAnshuman Khandual } 1665fdd99a41SAnshuman Khandual } 1666fdd99a41SAnshuman Khandual 1667bbd6ec60SAnshuman Khandual static int __init prevent_bootmem_remove_init(void) 1668bbd6ec60SAnshuman Khandual { 1669cb45babeSAnshuman Khandual int ret = 0; 1670cb45babeSAnshuman Khandual 1671cb45babeSAnshuman Khandual if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE)) 1672cb45babeSAnshuman Khandual return ret; 1673cb45babeSAnshuman Khandual 1674fdd99a41SAnshuman Khandual validate_bootmem_online(); 1675cb45babeSAnshuman Khandual ret = register_memory_notifier(&prevent_bootmem_remove_nb); 1676cb45babeSAnshuman Khandual if (ret) 1677cb45babeSAnshuman Khandual pr_err("%s: Notifier registration failed %d\n", __func__, ret); 1678cb45babeSAnshuman Khandual 1679cb45babeSAnshuman Khandual return ret; 1680bbd6ec60SAnshuman Khandual } 1681cb45babeSAnshuman Khandual early_initcall(prevent_bootmem_remove_init); 168222eb6346SDavid Hildenbrand #endif 1683