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