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