Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Based on arch/arm/mm/mmu.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 1995-2005 Russell King
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (C) 2012 ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/libfdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/nodemask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <asm/barrier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <asm/cputype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <asm/fixmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <asm/kasan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <asm/kernel-pgtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <asm/tlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <asm/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <asm/ptdump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <asm/pgalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define NO_BLOCK_MAPPINGS	BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #define NO_CONT_MAPPINGS	BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) u64 idmap_t0sz = TCR_T0SZ(VA_BITS_MIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) u64 __section(".mmuoff.data.write") vabits_actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) EXPORT_SYMBOL(vabits_actual);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) u64 kimage_voffset __ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) EXPORT_SYMBOL(kimage_voffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * Empty_zero_page is a special page that is used for zero-initialized data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * and COW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) EXPORT_SYMBOL(empty_zero_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) static DEFINE_SPINLOCK(swapper_pgdir_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static DEFINE_MUTEX(fixmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	pgd_t *fixmap_pgdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	spin_lock(&swapper_pgdir_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	WRITE_ONCE(*fixmap_pgdp, pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	 * We need dsb(ishst) here to ensure the page-table-walker sees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	 * our new entry before set_p?d() returns. The fixmap's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	 * flush_tlb_kernel_range() via clear_fixmap() does this for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	pgd_clear_fixmap();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	spin_unlock(&swapper_pgdir_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 			      unsigned long size, pgprot_t vma_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	if (!pfn_valid(pfn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 		return pgprot_noncached(vma_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	else if (file->f_flags & O_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		return pgprot_writecombine(vma_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	return vma_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) EXPORT_SYMBOL(phys_mem_access_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) static phys_addr_t __init early_pgtable_alloc(int shift)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	phys_addr_t phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	if (!phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		panic("Failed to allocate page table page\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	 * any level of table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	ptr = pte_set_fixmap(phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	memset(ptr, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	 * Implicit barriers also ensure the zeroed page is visible to the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	 * table walker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	pte_clear_fixmap();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	return phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) static bool pgattr_change_is_safe(u64 old, u64 new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	 * The following mapping attributes may be updated in live
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	 * kernel mappings without the need for break-before-make.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	/* creating or taking down mappings is always safe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	if (old == 0 || new == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	/* live contiguous mappings may not be manipulated at all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	if ((old | new) & PTE_CONT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	/* Transitioning from Non-Global to Global is unsafe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	if (old & ~new & PTE_NG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	 * Changing the memory type between Normal and Normal-Tagged is safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	 * since Tagged is considered a permission attribute from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	 * mismatched attribute aliases perspective.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	     (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	    ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	     (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 		mask |= PTE_ATTRINDX_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	return ((old ^ new) & ~mask) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		     phys_addr_t phys, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	pte_t *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	ptep = pte_set_fixmap_offset(pmdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		pte_t old_pte = READ_ONCE(*ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		 * After the PTE entry has been populated once, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		 * only allow updates to the permission attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 					      READ_ONCE(pte_val(*ptep))));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		phys += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	} while (ptep++, addr += PAGE_SIZE, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	pte_clear_fixmap();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 				unsigned long end, phys_addr_t phys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 				pgprot_t prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 				phys_addr_t (*pgtable_alloc)(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 				int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	pmd_t pmd = READ_ONCE(*pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	BUG_ON(pmd_sect(pmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	if (pmd_none(pmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		phys_addr_t pte_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		BUG_ON(!pgtable_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		pte_phys = pgtable_alloc(PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		__pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		pmd = READ_ONCE(*pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	BUG_ON(pmd_bad(pmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		pgprot_t __prot = prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		next = pte_cont_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		/* use a contiguous mapping if the range is suitably aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		    (flags & NO_CONT_MAPPINGS) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		init_pte(pmdp, addr, next, phys, __prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		phys += next - addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	} while (addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		     phys_addr_t phys, pgprot_t prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		     phys_addr_t (*pgtable_alloc)(int), int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	pmd_t *pmdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	pmdp = pmd_set_fixmap_offset(pudp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		pmd_t old_pmd = READ_ONCE(*pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		next = pmd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		/* try section mapping first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			pmd_set_huge(pmdp, phys, prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 			 * After the PMD entry has been populated once, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 			 * only allow updates to the permission attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 			BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 						      READ_ONCE(pmd_val(*pmdp))));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 			alloc_init_cont_pte(pmdp, addr, next, phys, prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 					    pgtable_alloc, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 			BUG_ON(pmd_val(old_pmd) != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 			       pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		phys += next - addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	} while (pmdp++, addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	pmd_clear_fixmap();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 				unsigned long end, phys_addr_t phys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 				pgprot_t prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 				phys_addr_t (*pgtable_alloc)(int), int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	pud_t pud = READ_ONCE(*pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	 * Check for initial section mappings in the pgd/pud.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	BUG_ON(pud_sect(pud));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	if (pud_none(pud)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		phys_addr_t pmd_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		BUG_ON(!pgtable_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		pmd_phys = pgtable_alloc(PMD_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		__pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		pud = READ_ONCE(*pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	BUG_ON(pud_bad(pud));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		pgprot_t __prot = prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		next = pmd_cont_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		/* use a contiguous mapping if the range is suitably aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		    (flags & NO_CONT_MAPPINGS) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		phys += next - addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	} while (addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) static inline bool use_1G_block(unsigned long addr, unsigned long next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 			unsigned long phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	if (PAGE_SHIFT != 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	if (((addr | next | phys) & ~PUD_MASK) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 			   phys_addr_t phys, pgprot_t prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			   phys_addr_t (*pgtable_alloc)(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			   int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	pud_t *pudp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	p4d_t *p4dp = p4d_offset(pgdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	p4d_t p4d = READ_ONCE(*p4dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	if (p4d_none(p4d)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		phys_addr_t pud_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		BUG_ON(!pgtable_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		pud_phys = pgtable_alloc(PUD_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		__p4d_populate(p4dp, pud_phys, PUD_TYPE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		p4d = READ_ONCE(*p4dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	BUG_ON(p4d_bad(p4d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	 * No need for locking during early boot. And it doesn't work as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	 * expected with KASLR enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	if (system_state != SYSTEM_BOOTING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		mutex_lock(&fixmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	pudp = pud_set_fixmap_offset(p4dp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		pud_t old_pud = READ_ONCE(*pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		next = pud_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		 * For 4K granule only, attempt to put down a 1GB block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		if (use_1G_block(addr, next, phys) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 			pud_set_huge(pudp, phys, prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 			 * After the PUD entry has been populated once, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 			 * only allow updates to the permission attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 			BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 						      READ_ONCE(pud_val(*pudp))));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 			alloc_init_cont_pmd(pudp, addr, next, phys, prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 					    pgtable_alloc, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 			BUG_ON(pud_val(old_pud) != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 			       pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		phys += next - addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	} while (pudp++, addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	pud_clear_fixmap();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	if (system_state != SYSTEM_BOOTING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		mutex_unlock(&fixmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 				 unsigned long virt, phys_addr_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 				 pgprot_t prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 				 phys_addr_t (*pgtable_alloc)(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 				 int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	unsigned long addr, end, next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	pgd_t *pgdp = pgd_offset_pgd(pgdir, virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	 * If the virtual and physical address don't have the same offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	 * within a page, we cannot map the region as the caller expects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	phys &= PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	addr = virt & PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	end = PAGE_ALIGN(virt + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		next = pgd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 			       flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		phys += next - addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	} while (pgdp++, addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) static phys_addr_t __pgd_pgtable_alloc(int shift)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	void *ptr = (void *)__get_free_page(GFP_PGTABLE_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	BUG_ON(!ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	/* Ensure the zeroed page is visible to the page table walker */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	dsb(ishst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	return __pa(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) static phys_addr_t pgd_pgtable_alloc(int shift)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	phys_addr_t pa = __pgd_pgtable_alloc(shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	 * Call proper page table ctor in case later we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	 * call core mm functions like apply_to_page_range() on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	 * this pre-allocated page table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	 * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	 * folded, and if so pgtable_pmd_page_ctor() becomes nop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	if (shift == PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		BUG_ON(!pgtable_pte_page_ctor(phys_to_page(pa)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	else if (shift == PMD_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		BUG_ON(!pgtable_pmd_page_ctor(phys_to_page(pa)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	return pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  * This function can only be used to modify existing table entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  * without allocating new levels of table. Note that this permits the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * creation of new section or page entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 				  phys_addr_t size, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if ((virt >= PAGE_END) && (virt < VMALLOC_START)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 			&phys, virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 			     NO_CONT_MAPPINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			       unsigned long virt, phys_addr_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			       pgprot_t prot, bool page_mappings_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	BUG_ON(mm == &init_mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (page_mappings_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 			     pgd_pgtable_alloc, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 				phys_addr_t size, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	if ((virt >= PAGE_END) && (virt < VMALLOC_START)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 			&phys, virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 			     NO_CONT_MAPPINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	/* flush the TLBs after updating live kernel mappings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	flush_tlb_kernel_range(virt, virt + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 				  phys_addr_t end, pgprot_t prot, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	__create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			     prot, early_pgtable_alloc, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) void __init mark_linear_text_alias_ro(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	 * Remove the write permissions from the linear alias of .text/.rodata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			    (unsigned long)__init_begin - (unsigned long)_text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 			    PAGE_KERNEL_RO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) static bool crash_mem_map __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) static int __init enable_crash_mem_map(char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	 * Proper parameter parsing is done by reserve_crashkernel(). We only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	 * need to know if the linear map has to avoid block mappings so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	 * the crashkernel reservations can be unmapped later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	crash_mem_map = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) early_param("crashkernel", enable_crash_mem_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) static void __init map_mem(pgd_t *pgdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	phys_addr_t kernel_start = __pa_symbol(_text);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	phys_addr_t kernel_end = __pa_symbol(__init_begin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	phys_addr_t start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	u64 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	if (rodata_full || debug_pagealloc_enabled() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	    IS_ENABLED(CONFIG_KFENCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	 * Take care not to create a writable alias for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	 * read-only text and rodata sections of the kernel image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	 * So temporarily mark them as NOMAP to skip mappings in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	 * the following for-loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) #ifdef CONFIG_KEXEC_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	if (crash_mem_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		if (IS_ENABLED(CONFIG_ZONE_DMA) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		    IS_ENABLED(CONFIG_ZONE_DMA32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		else if (crashk_res.end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			memblock_mark_nomap(crashk_res.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 					    resource_size(&crashk_res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	/* map all the memory banks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	for_each_mem_range(i, &start, &end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		if (start >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		 * The linear map must allow allocation tags reading/writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		 * if MTE is present. Otherwise, it has the same attributes as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		 * PAGE_KERNEL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		__map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 			       flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	 * Map the linear alias of the [_text, __init_begin) interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	 * as non-executable now, and remove the write permission in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	 * mark_linear_text_alias_ro() below (which will be called after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	 * alternative patching has completed). This makes the contents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	 * of the region accessible to subsystems such as hibernate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	 * but protects it from inadvertent modification or execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	 * Note that contiguous mappings cannot be remapped in this way,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	 * so we should avoid them here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	__map_memblock(pgdp, kernel_start, kernel_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		       PAGE_KERNEL, NO_CONT_MAPPINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	 * Use page-level mappings here so that we can shrink the region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	 * in page granularity and put back unused memory to buddy system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	 * through /sys/kernel/kexec_crash_size interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) #ifdef CONFIG_KEXEC_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	if (crash_mem_map &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	    !IS_ENABLED(CONFIG_ZONE_DMA) && !IS_ENABLED(CONFIG_ZONE_DMA32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		if (crashk_res.end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			__map_memblock(pgdp, crashk_res.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 				       crashk_res.end + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 				       PAGE_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 				       NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 			memblock_clear_nomap(crashk_res.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 					     resource_size(&crashk_res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) void mark_rodata_ro(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	unsigned long section_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	 * to cover NOTES and EXCEPTION_TABLE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 			    section_size, PAGE_KERNEL_RO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	debug_checkwx();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 				      pgprot_t prot, struct vm_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 				      int flags, unsigned long vm_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	phys_addr_t pa_start = __pa_symbol(va_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	unsigned long size = va_end - va_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	BUG_ON(!PAGE_ALIGNED(pa_start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	BUG_ON(!PAGE_ALIGNED(size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	__create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 			     early_pgtable_alloc, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	if (!(vm_flags & VM_NO_GUARD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		size += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	vma->addr	= va_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	vma->phys_addr	= pa_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	vma->size	= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	vma->flags	= VM_MAP | vm_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	vma->caller	= __builtin_return_address(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	vm_area_add_early(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) static int __init parse_rodata(char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	int ret = strtobool(arg, &rodata_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		rodata_full = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	/* permit 'full' in addition to boolean options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	if (strcmp(arg, "full"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	rodata_enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	rodata_full = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) early_param("rodata", parse_rodata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) static int __init map_entry_trampoline(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	/* The trampoline is always mapped and can therefore be global */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	pgprot_val(prot) &= ~PTE_NG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	/* Map only the text into the trampoline page table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	memset(tramp_pg_dir, 0, PGD_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	__create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 			     entry_tramp_text_size(), prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			     __pgd_pgtable_alloc, NO_BLOCK_MAPPINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	/* Map both the text and data into the kernel page table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		__set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 			     pa_start + i * PAGE_SIZE, prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		extern char __entry_tramp_data_start[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		__set_fixmap(FIX_ENTRY_TRAMP_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			     __pa_symbol(__entry_tramp_data_start),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 			     PAGE_KERNEL_RO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) core_initcall(map_entry_trampoline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  * Open coded check for BTI, only for use to determine configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669)  * for early mappings for before the cpufeature code has run.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) static bool arm64_early_this_cpu_has_bti(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	u64 pfr1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	return cpuid_feature_extract_unsigned_field(pfr1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 						    ID_AA64PFR1_BT_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684)  * Create fine-grained mappings for the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) static void __init map_kernel(pgd_t *pgdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 				vmlinux_initdata, vmlinux_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	 * External debuggers may need to write directly to the text
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	 * mapping to install SW breakpoints. Allow this (only) when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	 * explicitly requested with rodata=off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	 * If we have a CPU that supports BTI and a kernel built for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	 * BTI then mark the kernel executable text as guarded pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	 * now so we don't have to rewrite the page tables later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	if (arm64_early_this_cpu_has_bti())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	 * Only rodata will be remapped with different permissions later on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	 * all other segments are allowed to use contiguous mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	map_kernel_segment(pgdp, _text, _etext, text_prot, &vmlinux_text, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 			   VM_NO_GUARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 			   &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 			   &vmlinux_inittext, 0, VM_NO_GUARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 			   &vmlinux_initdata, 0, VM_NO_GUARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		 * The fixmap falls in a separate pgd to the kernel, and doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		 * live in the carveout for the swapper_pg_dir. We can simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		 * re-use the existing dir for the fixmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 			READ_ONCE(*pgd_offset_k(FIXADDR_START)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	} else if (CONFIG_PGTABLE_LEVELS > 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		pgd_t *bm_pgdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		p4d_t *bm_p4dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		pud_t *bm_pudp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		 * The fixmap shares its top level pgd entry with the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		 * mapping. This can really only occur when we are running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		 * with 16k/4 levels, so we can simply reuse the pud level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		 * entry instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		pud_clear_fixmap();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	kasan_copy_shadow(pgdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) void __init paging_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	map_kernel(pgdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	map_mem(pgdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	pgd_clear_fixmap();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	init_mm.pgd = swapper_pg_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	memblock_free(__pa_symbol(init_pg_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		      __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	memblock_allow_resize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770)  * Check whether a kernel address is valid (derived from arch/x86/).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) int kern_addr_valid(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	pgd_t *pgdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	p4d_t *p4dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	pud_t *pudp, pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	pmd_t *pmdp, pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	pte_t *ptep, pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	addr = arch_kasan_reset_tag(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	if ((((long)addr) >> VA_BITS) != -1UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	pgdp = pgd_offset_k(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	if (pgd_none(READ_ONCE(*pgdp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	p4dp = p4d_offset(pgdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	if (p4d_none(READ_ONCE(*p4dp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	pudp = pud_offset(p4dp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	pud = READ_ONCE(*pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	if (pud_none(pud))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (pud_sect(pud))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		return pfn_valid(pud_pfn(pud));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	pmdp = pmd_offset(pudp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	pmd = READ_ONCE(*pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	if (pmd_none(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	if (pmd_sect(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		return pfn_valid(pmd_pfn(pmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	ptep = pte_offset_kernel(pmdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	pte = READ_ONCE(*ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	if (pte_none(pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	return pfn_valid(pte_pfn(pte));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) #ifdef CONFIG_MEMORY_HOTPLUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) static void free_hotplug_page_range(struct page *page, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 				    struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	if (altmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		vmem_altmap_free(altmap, size >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		WARN_ON(PageReserved(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		free_pages((unsigned long)page_address(page), get_order(size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) static void free_hotplug_pgtable_page(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) static bool pgtable_range_aligned(unsigned long start, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 				  unsigned long floor, unsigned long ceiling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 				  unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	start &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	if (start < floor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	if (ceiling) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		ceiling &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		if (!ceiling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	if (end - 1 > ceiling - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 				    unsigned long end, bool free_mapped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 				    struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	pte_t *ptep, pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		ptep = pte_offset_kernel(pmdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		pte = READ_ONCE(*ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		if (pte_none(pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		WARN_ON(!pte_present(pte));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		pte_clear(&init_mm, addr, ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		if (free_mapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			free_hotplug_page_range(pte_page(pte),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 						PAGE_SIZE, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	} while (addr += PAGE_SIZE, addr < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 				    unsigned long end, bool free_mapped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 				    struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	pmd_t *pmdp, pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		next = pmd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		pmdp = pmd_offset(pudp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		pmd = READ_ONCE(*pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		if (pmd_none(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		WARN_ON(!pmd_present(pmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		if (pmd_sect(pmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			pmd_clear(pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			 * One TLBI should be sufficient here as the PMD_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			 * range is mapped with a single block entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			if (free_mapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 				free_hotplug_page_range(pmd_page(pmd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 							PMD_SIZE, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		WARN_ON(!pmd_table(pmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	} while (addr = next, addr < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 				    unsigned long end, bool free_mapped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 				    struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	pud_t *pudp, pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		next = pud_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		pudp = pud_offset(p4dp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		pud = READ_ONCE(*pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (pud_none(pud))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		WARN_ON(!pud_present(pud));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		if (pud_sect(pud)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 			pud_clear(pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			 * One TLBI should be sufficient here as the PUD_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 			 * range is mapped with a single block entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			if (free_mapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 				free_hotplug_page_range(pud_page(pud),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 							PUD_SIZE, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		WARN_ON(!pud_table(pud));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	} while (addr = next, addr < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 				    unsigned long end, bool free_mapped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 				    struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	p4d_t *p4dp, p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		next = p4d_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		p4dp = p4d_offset(pgdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		p4d = READ_ONCE(*p4dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		if (p4d_none(p4d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		WARN_ON(!p4d_present(p4d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	} while (addr = next, addr < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) static void unmap_hotplug_range(unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 				bool free_mapped, struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	pgd_t *pgdp, pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	 * altmap can only be used as vmemmap mapping backing memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	 * In case the backing memory itself is not being freed, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	 * altmap is irrelevant. Warn about this inconsistency when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	 * encountered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	WARN_ON(!free_mapped && altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		next = pgd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		pgdp = pgd_offset_k(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		pgd = READ_ONCE(*pgdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		if (pgd_none(pgd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		WARN_ON(!pgd_present(pgd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	} while (addr = next, addr < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 				 unsigned long end, unsigned long floor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 				 unsigned long ceiling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	pte_t *ptep, pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	unsigned long i, start = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		ptep = pte_offset_kernel(pmdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		pte = READ_ONCE(*ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		 * This is just a sanity check here which verifies that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		 * pte clearing has been done by earlier unmap loops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		WARN_ON(!pte_none(pte));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	} while (addr += PAGE_SIZE, addr < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	 * Check whether we can free the pte page if the rest of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	 * entries are empty. Overlap with other regions have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	 * handled by the floor/ceiling check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	ptep = pte_offset_kernel(pmdp, 0UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	for (i = 0; i < PTRS_PER_PTE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		if (!pte_none(READ_ONCE(ptep[i])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	pmd_clear(pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	__flush_tlb_kernel_pgtable(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	free_hotplug_pgtable_page(virt_to_page(ptep));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 				 unsigned long end, unsigned long floor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 				 unsigned long ceiling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	pmd_t *pmdp, pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	unsigned long i, next, start = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		next = pmd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		pmdp = pmd_offset(pudp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		pmd = READ_ONCE(*pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		if (pmd_none(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		free_empty_pte_table(pmdp, addr, next, floor, ceiling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	} while (addr = next, addr < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	if (CONFIG_PGTABLE_LEVELS <= 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	 * Check whether we can free the pmd page if the rest of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	 * entries are empty. Overlap with other regions have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	 * handled by the floor/ceiling check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	pmdp = pmd_offset(pudp, 0UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	for (i = 0; i < PTRS_PER_PMD; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		if (!pmd_none(READ_ONCE(pmdp[i])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	pud_clear(pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	__flush_tlb_kernel_pgtable(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	free_hotplug_pgtable_page(virt_to_page(pmdp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 				 unsigned long end, unsigned long floor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 				 unsigned long ceiling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	pud_t *pudp, pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	unsigned long i, next, start = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		next = pud_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		pudp = pud_offset(p4dp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		pud = READ_ONCE(*pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		if (pud_none(pud))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		free_empty_pmd_table(pudp, addr, next, floor, ceiling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	} while (addr = next, addr < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	if (CONFIG_PGTABLE_LEVELS <= 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	 * Check whether we can free the pud page if the rest of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	 * entries are empty. Overlap with other regions have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	 * handled by the floor/ceiling check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	pudp = pud_offset(p4dp, 0UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	for (i = 0; i < PTRS_PER_PUD; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		if (!pud_none(READ_ONCE(pudp[i])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	p4d_clear(p4dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	__flush_tlb_kernel_pgtable(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	free_hotplug_pgtable_page(virt_to_page(pudp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 				 unsigned long end, unsigned long floor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 				 unsigned long ceiling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	p4d_t *p4dp, p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		next = p4d_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		p4dp = p4d_offset(pgdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		p4d = READ_ONCE(*p4dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		if (p4d_none(p4d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		WARN_ON(!p4d_present(p4d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		free_empty_pud_table(p4dp, addr, next, floor, ceiling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	} while (addr = next, addr < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) static void free_empty_tables(unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 			      unsigned long floor, unsigned long ceiling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	pgd_t *pgdp, pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		next = pgd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		pgdp = pgd_offset_k(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		pgd = READ_ONCE(*pgdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		if (pgd_none(pgd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		WARN_ON(!pgd_present(pgd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		free_empty_p4d_table(pgdp, addr, next, floor, ceiling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	} while (addr = next, addr < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) #ifdef CONFIG_SPARSEMEM_VMEMMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) #if !ARM64_SWAPPER_USES_SECTION_MAPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	return vmemmap_populate_basepages(start, end, node, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) #else	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	unsigned long addr = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	pgd_t *pgdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	p4d_t *p4dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	pud_t *pudp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	pmd_t *pmdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		next = pmd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		pgdp = vmemmap_pgd_populate(addr, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		if (!pgdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		p4dp = vmemmap_p4d_populate(pgdp, addr, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		if (!p4dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		pudp = vmemmap_pud_populate(p4dp, addr, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		if (!pudp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		pmdp = pmd_offset(pudp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		if (pmd_none(READ_ONCE(*pmdp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 			void *p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 			p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 				if (vmemmap_populate_basepages(addr, next, node, altmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 					return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 			vmemmap_verify((pte_t *)pmdp, node, addr, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	} while (addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) #endif	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) void vmemmap_free(unsigned long start, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) #ifdef CONFIG_MEMORY_HOTPLUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	unmap_hotplug_range(start, end, true, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) #endif	/* CONFIG_SPARSEMEM_VMEMMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) static inline pud_t * fixmap_pud(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	pgd_t *pgdp = pgd_offset_k(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	p4d_t *p4dp = p4d_offset(pgdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	p4d_t p4d = READ_ONCE(*p4dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	BUG_ON(p4d_none(p4d) || p4d_bad(p4d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	return pud_offset_kimg(p4dp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) static inline pmd_t * fixmap_pmd(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	pud_t *pudp = fixmap_pud(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	pud_t pud = READ_ONCE(*pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	BUG_ON(pud_none(pud) || pud_bad(pud));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	return pmd_offset_kimg(pudp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) static inline pte_t * fixmap_pte(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	return &bm_pte[pte_index(addr)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)  * The p*d_populate functions call virt_to_phys implicitly so they can't be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)  * directly on kernel symbols (bm_p*d). This function is called too early to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)  * lm_alias so __p*d_populate functions must be used to populate with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)  * physical address from __pa_symbol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) void __init early_fixmap_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	pgd_t *pgdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	p4d_t *p4dp, p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	pud_t *pudp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	pmd_t *pmdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	unsigned long addr = FIXADDR_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	pgdp = pgd_offset_k(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	p4dp = p4d_offset(pgdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	p4d = READ_ONCE(*p4dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	if (CONFIG_PGTABLE_LEVELS > 3 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	    !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		 * We only end up here if the kernel mapping and the fixmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		 * share the top level pgd entry, which should only happen on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		 * 16k/4 levels configurations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		pudp = pud_offset_kimg(p4dp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		if (p4d_none(p4d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 			__p4d_populate(p4dp, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		pudp = fixmap_pud(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	if (pud_none(READ_ONCE(*pudp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		__pud_populate(pudp, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	pmdp = fixmap_pmd(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	__pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	 * The boot-ioremap range spans multiple pmds, for which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	 * we are not prepared:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	     || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		pr_warn("pmdp %p != %p, %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 			pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 			fix_to_virt(FIX_BTMAP_BEGIN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 			fix_to_virt(FIX_BTMAP_END));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)  * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)  * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) void __set_fixmap(enum fixed_addresses idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 			       phys_addr_t phys, pgprot_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	unsigned long addr = __fix_to_virt(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	pte_t *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	ptep = fixmap_pte(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	if (pgprot_val(flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		pte_clear(&init_mm, addr, ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	void *dt_virt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	 * Check whether the physical FDT address is set and meets the minimum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	 * at least 8 bytes so that we can always access the magic and size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	 * fields of the FDT header after mapping the first chunk, double check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	 * here if that is indeed the case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	 * Make sure that the FDT region can be mapped without the need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	 * allocate additional translation table pages, so that it is safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	 * to call create_mapping_noalloc() this early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	 * On 64k pages, the FDT will be mapped using PTEs, so we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	 * be in the same PMD as the rest of the fixmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	 * On 4k pages, we'll use section mappings for the FDT so we only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	 * have to be in the same PUD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	BUILD_BUG_ON(dt_virt_base % SZ_2M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		     __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	offset = dt_phys % SWAPPER_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	dt_virt = (void *)dt_virt_base + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	/* map the first chunk so we can read the size from the header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 			dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	if (fdt_magic(dt_virt) != FDT_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	*size = fdt_totalsize(dt_virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	if (*size > MAX_FDT_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	if (offset + *size > SWAPPER_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 			       round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	return dt_virt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) int __init arch_ioremap_p4d_supported(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) int __init arch_ioremap_pud_supported(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	 * Only 4k granule supports level 1 block mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	 * SW table walks can't handle removal of intermediate entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	return IS_ENABLED(CONFIG_ARM64_4K_PAGES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	       !IS_ENABLED(CONFIG_PTDUMP_DEBUGFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) int __init arch_ioremap_pmd_supported(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	/* See arch_ioremap_pud_supported() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	return !IS_ENABLED(CONFIG_PTDUMP_DEBUGFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	/* Only allow permission changes for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 				   pud_val(new_pud)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	VM_BUG_ON(phys & ~PUD_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	set_pud(pudp, new_pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	/* Only allow permission changes for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 				   pmd_val(new_pmd)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	VM_BUG_ON(phys & ~PMD_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	set_pmd(pmdp, new_pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) int pud_clear_huge(pud_t *pudp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	if (!pud_sect(READ_ONCE(*pudp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	pud_clear(pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) int pmd_clear_huge(pmd_t *pmdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	if (!pmd_sect(READ_ONCE(*pmdp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	pmd_clear(pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	pte_t *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	pmd_t pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	pmd = READ_ONCE(*pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	if (!pmd_table(pmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		VM_WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	table = pte_offset_kernel(pmdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	pmd_clear(pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	__flush_tlb_kernel_pgtable(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	pte_free_kernel(NULL, table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	pmd_t *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	pmd_t *pmdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	pud_t pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	unsigned long next, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	pud = READ_ONCE(*pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	if (!pud_table(pud)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		VM_WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	table = pmd_offset(pudp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	pmdp = table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	next = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	end = addr + PUD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		pmd_free_pte_page(pmdp, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	} while (pmdp++, next += PMD_SIZE, next != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	pud_clear(pudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	__flush_tlb_kernel_pgtable(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	pmd_free(NULL, table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) int p4d_free_pud_page(p4d_t *p4d, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	return 0;	/* Don't attempt a block mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) #ifdef CONFIG_MEMORY_HOTPLUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	unsigned long end = start + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	WARN_ON(pgdir != init_mm.pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	unmap_hotplug_range(start, end, false, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) static bool inside_linear_region(u64 start, u64 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	u64 end_linear_pa = __pa(PAGE_END - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		 * Check for a wrap, it is possible because of randomized linear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		 * mapping the start physical address is actually bigger than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		 * the end physical address. In this case set start to zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		 * because [0, end_linear_pa] range must still be able to cover
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		 * all addressable physical addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		if (start_linear_pa > end_linear_pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 			start_linear_pa = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	WARN_ON(start_linear_pa > end_linear_pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	 * accommodating both its ends but excluding PAGE_END. Max physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	 * range which can be mapped inside this linear mapping range, must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	 * also be derived from its end points.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	return start >= start_linear_pa && (start + size - 1) <= end_linear_pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) int arch_add_memory(int nid, u64 start, u64 size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		    struct mhp_params *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	int ret, flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	if (!inside_linear_region(start, size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		pr_err("[%llx %llx] is outside linear mapping region\n", start, start + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	 * KFENCE requires linear map to be mapped at page granularity, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	 * it is possible to protect/unprotect single pages in the KFENCE pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	if (rodata_full || debug_pagealloc_enabled() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	    IS_ENABLED(CONFIG_KFENCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	__create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 			     size, params->pgprot, __pgd_pgtable_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 			     flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	memblock_clear_nomap(start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 			   params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		__remove_pgd_mapping(swapper_pg_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 				     __phys_to_virt(start), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		max_pfn = PFN_UP(start + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		max_low_pfn = max_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) void arch_remove_memory(int nid, u64 start, u64 size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 			struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	unsigned long start_pfn = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	unsigned long nr_pages = size >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	__remove_pages(start_pfn, nr_pages, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	__remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) int check_range_driver_managed(u64 start, u64 size, const char *resource_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	struct mem_section *ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	unsigned long pfn = __phys_to_pfn(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	unsigned long end_pfn = __phys_to_pfn(start + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	res = lookup_resource(&iomem_resource, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	if (!res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		pr_err("%s: couldn't find memory resource for start 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 			   __func__, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	flags = res->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	if (!(flags & IORESOURCE_SYSRAM_DRIVER_MANAGED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	    strstr(resource_name, "System RAM (") != resource_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		ms = __pfn_to_section(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		if (early_section(ms))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) int populate_range_driver_managed(u64 start, u64 size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 			const char *resource_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	unsigned long virt = (unsigned long)phys_to_virt(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	if (check_range_driver_managed(start, size, resource_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	 * When rodata_full is enabled, memory is mapped at page size granule,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	 * as opposed to block mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	if (rodata_full || debug_pagealloc_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	__create_pgd_mapping(init_mm.pgd, start, virt, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 			     PAGE_KERNEL, NULL, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) EXPORT_SYMBOL_GPL(populate_range_driver_managed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) int depopulate_range_driver_managed(u64 start, u64 size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 			const char *resource_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	if (check_range_driver_managed(start, size, resource_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	unmap_hotplug_range(start, start + size, false, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) EXPORT_SYMBOL_GPL(depopulate_range_driver_managed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)  * This memory hotplug notifier helps prevent boot memory from being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634)  * inadvertently removed as it blocks pfn range offlining process in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)  * __offline_pages(). Hence this prevents both offlining as well as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636)  * removal process for boot memory which is initially always online.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637)  * In future if and when boot memory could be removed, this notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638)  * should be dropped and free_hotplug_page_range() should handle any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)  * reserved pages allocated during boot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 					   unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	struct mem_section *ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	struct memory_notify *arg = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	unsigned long end_pfn = arg->start_pfn + arg->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	unsigned long pfn = arg->start_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	if (action != MEM_GOING_OFFLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		ms = __pfn_to_section(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		if (early_section(ms))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 			return NOTIFY_BAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) static struct notifier_block prevent_bootmem_remove_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	.notifier_call = prevent_bootmem_remove_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) static int __init prevent_bootmem_remove_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	return register_memory_notifier(&prevent_bootmem_remove_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) device_initcall(prevent_bootmem_remove_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) #endif