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
43 u64 idmap_t0sz = TCR_T0SZ(VA_BITS_MIN);
44 u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
45
46 u64 __section(".mmuoff.data.write") vabits_actual;
47 EXPORT_SYMBOL(vabits_actual);
48
49 u64 kimage_voffset __ro_after_init;
50 EXPORT_SYMBOL(kimage_voffset);
51
52 /*
53 * Empty_zero_page is a special page that is used for zero-initialized data
54 * and COW.
55 */
56 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
57 EXPORT_SYMBOL(empty_zero_page);
58
59 static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
60 static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
61 static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
62
63 static DEFINE_SPINLOCK(swapper_pgdir_lock);
64 static DEFINE_MUTEX(fixmap_lock);
65
set_swapper_pgd(pgd_t * pgdp,pgd_t pgd)66 void 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
phys_mem_access_prot(struct file * file,unsigned long pfn,unsigned long size,pgprot_t vma_prot)82 pgprot_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 }
91 EXPORT_SYMBOL(phys_mem_access_prot);
92
early_pgtable_alloc(int shift)93 static 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
pgattr_change_is_safe(u64 old,u64 new)120 static 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
init_pte(pmd_t * pmdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot)154 static 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
alloc_init_cont_pte(pmd_t * pmdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(int),int flags)178 static 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
init_pmd(pud_t * pudp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(int),int flags)213 static 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
alloc_init_cont_pmd(pud_t * pudp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(int),int flags)250 static 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
use_1G_block(unsigned long addr,unsigned long next,unsigned long phys)287 static 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
alloc_init_pud(pgd_t * pgdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(int),int flags)299 static 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
__create_pgd_mapping(pgd_t * pgdir,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,phys_addr_t (* pgtable_alloc)(int),int flags)358 static 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
__pgd_pgtable_alloc(int shift)386 static 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
pgd_pgtable_alloc(int shift)396 static 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 */
create_mapping_noalloc(phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)421 static 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
create_pgd_mapping(struct mm_struct * mm,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,bool page_mappings_only)433 void __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
update_mapping_prot(phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)448 static 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
__map_memblock(pgd_t * pgdp,phys_addr_t start,phys_addr_t end,pgprot_t prot,int flags)464 static 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
mark_linear_text_alias_ro(void)471 void __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
481 static bool crash_mem_map __initdata;
482
enable_crash_mem_map(char * arg)483 static 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 }
494 early_param("crashkernel", enable_crash_mem_map);
495
map_mem(pgd_t * pgdp)496 static 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 IS_ENABLED(CONFIG_KFENCE))
506 flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
507
508 /*
509 * Take care not to create a writable alias for the
510 * read-only text and rodata sections of the kernel image.
511 * So temporarily mark them as NOMAP to skip mappings in
512 * the following for-loop
513 */
514 memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
515
516 #ifdef CONFIG_KEXEC_CORE
517 if (crash_mem_map) {
518 if (IS_ENABLED(CONFIG_ZONE_DMA) ||
519 IS_ENABLED(CONFIG_ZONE_DMA32))
520 flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
521 else if (crashk_res.end)
522 memblock_mark_nomap(crashk_res.start,
523 resource_size(&crashk_res));
524 }
525 #endif
526
527 /* map all the memory banks */
528 for_each_mem_range(i, &start, &end) {
529 if (start >= end)
530 break;
531 /*
532 * The linear map must allow allocation tags reading/writing
533 * if MTE is present. Otherwise, it has the same attributes as
534 * PAGE_KERNEL.
535 */
536 __map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL),
537 flags);
538 }
539
540 /*
541 * Map the linear alias of the [_text, __init_begin) interval
542 * as non-executable now, and remove the write permission in
543 * mark_linear_text_alias_ro() below (which will be called after
544 * alternative patching has completed). This makes the contents
545 * of the region accessible to subsystems such as hibernate,
546 * but protects it from inadvertent modification or execution.
547 * Note that contiguous mappings cannot be remapped in this way,
548 * so we should avoid them here.
549 */
550 __map_memblock(pgdp, kernel_start, kernel_end,
551 PAGE_KERNEL, NO_CONT_MAPPINGS);
552 memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
553
554 /*
555 * Use page-level mappings here so that we can shrink the region
556 * in page granularity and put back unused memory to buddy system
557 * through /sys/kernel/kexec_crash_size interface.
558 */
559 #ifdef CONFIG_KEXEC_CORE
560 if (crash_mem_map &&
561 !IS_ENABLED(CONFIG_ZONE_DMA) && !IS_ENABLED(CONFIG_ZONE_DMA32)) {
562 if (crashk_res.end) {
563 __map_memblock(pgdp, crashk_res.start,
564 crashk_res.end + 1,
565 PAGE_KERNEL,
566 NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
567 memblock_clear_nomap(crashk_res.start,
568 resource_size(&crashk_res));
569 }
570 }
571 #endif
572 }
573
mark_rodata_ro(void)574 void mark_rodata_ro(void)
575 {
576 unsigned long section_size;
577
578 /*
579 * mark .rodata as read only. Use __init_begin rather than __end_rodata
580 * to cover NOTES and EXCEPTION_TABLE.
581 */
582 section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
583 update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
584 section_size, PAGE_KERNEL_RO);
585
586 debug_checkwx();
587 }
588
map_kernel_segment(pgd_t * pgdp,void * va_start,void * va_end,pgprot_t prot,struct vm_struct * vma,int flags,unsigned long vm_flags)589 static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
590 pgprot_t prot, struct vm_struct *vma,
591 int flags, unsigned long vm_flags)
592 {
593 phys_addr_t pa_start = __pa_symbol(va_start);
594 unsigned long size = va_end - va_start;
595
596 BUG_ON(!PAGE_ALIGNED(pa_start));
597 BUG_ON(!PAGE_ALIGNED(size));
598
599 __create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
600 early_pgtable_alloc, flags);
601
602 if (!(vm_flags & VM_NO_GUARD))
603 size += PAGE_SIZE;
604
605 vma->addr = va_start;
606 vma->phys_addr = pa_start;
607 vma->size = size;
608 vma->flags = VM_MAP | vm_flags;
609 vma->caller = __builtin_return_address(0);
610
611 vm_area_add_early(vma);
612 }
613
parse_rodata(char * arg)614 static int __init parse_rodata(char *arg)
615 {
616 int ret = strtobool(arg, &rodata_enabled);
617 if (!ret) {
618 rodata_full = false;
619 return 0;
620 }
621
622 /* permit 'full' in addition to boolean options */
623 if (strcmp(arg, "full"))
624 return -EINVAL;
625
626 rodata_enabled = true;
627 rodata_full = true;
628 return 0;
629 }
630 early_param("rodata", parse_rodata);
631
632 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
map_entry_trampoline(void)633 static int __init map_entry_trampoline(void)
634 {
635 int i;
636
637 pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
638 phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
639
640 /* The trampoline is always mapped and can therefore be global */
641 pgprot_val(prot) &= ~PTE_NG;
642
643 /* Map only the text into the trampoline page table */
644 memset(tramp_pg_dir, 0, PGD_SIZE);
645 __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS,
646 entry_tramp_text_size(), prot,
647 __pgd_pgtable_alloc, NO_BLOCK_MAPPINGS);
648
649 /* Map both the text and data into the kernel page table */
650 for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++)
651 __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
652 pa_start + i * PAGE_SIZE, prot);
653
654 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
655 extern char __entry_tramp_data_start[];
656
657 __set_fixmap(FIX_ENTRY_TRAMP_DATA,
658 __pa_symbol(__entry_tramp_data_start),
659 PAGE_KERNEL_RO);
660 }
661
662 return 0;
663 }
664 core_initcall(map_entry_trampoline);
665 #endif
666
667 /*
668 * Open coded check for BTI, only for use to determine configuration
669 * for early mappings for before the cpufeature code has run.
670 */
arm64_early_this_cpu_has_bti(void)671 static bool arm64_early_this_cpu_has_bti(void)
672 {
673 u64 pfr1;
674
675 if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
676 return false;
677
678 pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
679 return cpuid_feature_extract_unsigned_field(pfr1,
680 ID_AA64PFR1_BT_SHIFT);
681 }
682
683 /*
684 * Create fine-grained mappings for the kernel.
685 */
map_kernel(pgd_t * pgdp)686 static void __init map_kernel(pgd_t *pgdp)
687 {
688 static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
689 vmlinux_initdata, vmlinux_data;
690
691 /*
692 * External debuggers may need to write directly to the text
693 * mapping to install SW breakpoints. Allow this (only) when
694 * explicitly requested with rodata=off.
695 */
696 pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
697
698 /*
699 * If we have a CPU that supports BTI and a kernel built for
700 * BTI then mark the kernel executable text as guarded pages
701 * now so we don't have to rewrite the page tables later.
702 */
703 if (arm64_early_this_cpu_has_bti())
704 text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP);
705
706 /*
707 * Only rodata will be remapped with different permissions later on,
708 * all other segments are allowed to use contiguous mappings.
709 */
710 map_kernel_segment(pgdp, _text, _etext, text_prot, &vmlinux_text, 0,
711 VM_NO_GUARD);
712 map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
713 &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
714 map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
715 &vmlinux_inittext, 0, VM_NO_GUARD);
716 map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
717 &vmlinux_initdata, 0, VM_NO_GUARD);
718 map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
719
720 if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) {
721 /*
722 * The fixmap falls in a separate pgd to the kernel, and doesn't
723 * live in the carveout for the swapper_pg_dir. We can simply
724 * re-use the existing dir for the fixmap.
725 */
726 set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START),
727 READ_ONCE(*pgd_offset_k(FIXADDR_START)));
728 } else if (CONFIG_PGTABLE_LEVELS > 3) {
729 pgd_t *bm_pgdp;
730 p4d_t *bm_p4dp;
731 pud_t *bm_pudp;
732 /*
733 * The fixmap shares its top level pgd entry with the kernel
734 * mapping. This can really only occur when we are running
735 * with 16k/4 levels, so we can simply reuse the pud level
736 * entry instead.
737 */
738 BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
739 bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START);
740 bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START);
741 bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START);
742 pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd));
743 pud_clear_fixmap();
744 } else {
745 BUG();
746 }
747
748 kasan_copy_shadow(pgdp);
749 }
750
paging_init(void)751 void __init paging_init(void)
752 {
753 pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
754
755 map_kernel(pgdp);
756 map_mem(pgdp);
757
758 pgd_clear_fixmap();
759
760 cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
761 init_mm.pgd = swapper_pg_dir;
762
763 memblock_free(__pa_symbol(init_pg_dir),
764 __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
765
766 memblock_allow_resize();
767 }
768
769 /*
770 * Check whether a kernel address is valid (derived from arch/x86/).
771 */
kern_addr_valid(unsigned long addr)772 int kern_addr_valid(unsigned long addr)
773 {
774 pgd_t *pgdp;
775 p4d_t *p4dp;
776 pud_t *pudp, pud;
777 pmd_t *pmdp, pmd;
778 pte_t *ptep, pte;
779
780 addr = arch_kasan_reset_tag(addr);
781 if ((((long)addr) >> VA_BITS) != -1UL)
782 return 0;
783
784 pgdp = pgd_offset_k(addr);
785 if (pgd_none(READ_ONCE(*pgdp)))
786 return 0;
787
788 p4dp = p4d_offset(pgdp, addr);
789 if (p4d_none(READ_ONCE(*p4dp)))
790 return 0;
791
792 pudp = pud_offset(p4dp, addr);
793 pud = READ_ONCE(*pudp);
794 if (pud_none(pud))
795 return 0;
796
797 if (pud_sect(pud))
798 return pfn_valid(pud_pfn(pud));
799
800 pmdp = pmd_offset(pudp, addr);
801 pmd = READ_ONCE(*pmdp);
802 if (pmd_none(pmd))
803 return 0;
804
805 if (pmd_sect(pmd))
806 return pfn_valid(pmd_pfn(pmd));
807
808 ptep = pte_offset_kernel(pmdp, addr);
809 pte = READ_ONCE(*ptep);
810 if (pte_none(pte))
811 return 0;
812
813 return pfn_valid(pte_pfn(pte));
814 }
815
816 #ifdef CONFIG_MEMORY_HOTPLUG
free_hotplug_page_range(struct page * page,size_t size,struct vmem_altmap * altmap)817 static void free_hotplug_page_range(struct page *page, size_t size,
818 struct vmem_altmap *altmap)
819 {
820 if (altmap) {
821 vmem_altmap_free(altmap, size >> PAGE_SHIFT);
822 } else {
823 WARN_ON(PageReserved(page));
824 free_pages((unsigned long)page_address(page), get_order(size));
825 }
826 }
827
free_hotplug_pgtable_page(struct page * page)828 static void free_hotplug_pgtable_page(struct page *page)
829 {
830 free_hotplug_page_range(page, PAGE_SIZE, NULL);
831 }
832
pgtable_range_aligned(unsigned long start,unsigned long end,unsigned long floor,unsigned long ceiling,unsigned long mask)833 static bool pgtable_range_aligned(unsigned long start, unsigned long end,
834 unsigned long floor, unsigned long ceiling,
835 unsigned long mask)
836 {
837 start &= mask;
838 if (start < floor)
839 return false;
840
841 if (ceiling) {
842 ceiling &= mask;
843 if (!ceiling)
844 return false;
845 }
846
847 if (end - 1 > ceiling - 1)
848 return false;
849 return true;
850 }
851
unmap_hotplug_pte_range(pmd_t * pmdp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)852 static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
853 unsigned long end, bool free_mapped,
854 struct vmem_altmap *altmap)
855 {
856 pte_t *ptep, pte;
857
858 do {
859 ptep = pte_offset_kernel(pmdp, addr);
860 pte = READ_ONCE(*ptep);
861 if (pte_none(pte))
862 continue;
863
864 WARN_ON(!pte_present(pte));
865 pte_clear(&init_mm, addr, ptep);
866 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
867 if (free_mapped)
868 free_hotplug_page_range(pte_page(pte),
869 PAGE_SIZE, altmap);
870 } while (addr += PAGE_SIZE, addr < end);
871 }
872
unmap_hotplug_pmd_range(pud_t * pudp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)873 static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
874 unsigned long end, bool free_mapped,
875 struct vmem_altmap *altmap)
876 {
877 unsigned long next;
878 pmd_t *pmdp, pmd;
879
880 do {
881 next = pmd_addr_end(addr, end);
882 pmdp = pmd_offset(pudp, addr);
883 pmd = READ_ONCE(*pmdp);
884 if (pmd_none(pmd))
885 continue;
886
887 WARN_ON(!pmd_present(pmd));
888 if (pmd_sect(pmd)) {
889 pmd_clear(pmdp);
890
891 /*
892 * One TLBI should be sufficient here as the PMD_SIZE
893 * range is mapped with a single block entry.
894 */
895 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
896 if (free_mapped)
897 free_hotplug_page_range(pmd_page(pmd),
898 PMD_SIZE, altmap);
899 continue;
900 }
901 WARN_ON(!pmd_table(pmd));
902 unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap);
903 } while (addr = next, addr < end);
904 }
905
unmap_hotplug_pud_range(p4d_t * p4dp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)906 static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
907 unsigned long end, bool free_mapped,
908 struct vmem_altmap *altmap)
909 {
910 unsigned long next;
911 pud_t *pudp, pud;
912
913 do {
914 next = pud_addr_end(addr, end);
915 pudp = pud_offset(p4dp, addr);
916 pud = READ_ONCE(*pudp);
917 if (pud_none(pud))
918 continue;
919
920 WARN_ON(!pud_present(pud));
921 if (pud_sect(pud)) {
922 pud_clear(pudp);
923
924 /*
925 * One TLBI should be sufficient here as the PUD_SIZE
926 * range is mapped with a single block entry.
927 */
928 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
929 if (free_mapped)
930 free_hotplug_page_range(pud_page(pud),
931 PUD_SIZE, altmap);
932 continue;
933 }
934 WARN_ON(!pud_table(pud));
935 unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap);
936 } while (addr = next, addr < end);
937 }
938
unmap_hotplug_p4d_range(pgd_t * pgdp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)939 static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
940 unsigned long end, bool free_mapped,
941 struct vmem_altmap *altmap)
942 {
943 unsigned long next;
944 p4d_t *p4dp, p4d;
945
946 do {
947 next = p4d_addr_end(addr, end);
948 p4dp = p4d_offset(pgdp, addr);
949 p4d = READ_ONCE(*p4dp);
950 if (p4d_none(p4d))
951 continue;
952
953 WARN_ON(!p4d_present(p4d));
954 unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap);
955 } while (addr = next, addr < end);
956 }
957
unmap_hotplug_range(unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)958 static void unmap_hotplug_range(unsigned long addr, unsigned long end,
959 bool free_mapped, struct vmem_altmap *altmap)
960 {
961 unsigned long next;
962 pgd_t *pgdp, pgd;
963
964 /*
965 * altmap can only be used as vmemmap mapping backing memory.
966 * In case the backing memory itself is not being freed, then
967 * altmap is irrelevant. Warn about this inconsistency when
968 * encountered.
969 */
970 WARN_ON(!free_mapped && altmap);
971
972 do {
973 next = pgd_addr_end(addr, end);
974 pgdp = pgd_offset_k(addr);
975 pgd = READ_ONCE(*pgdp);
976 if (pgd_none(pgd))
977 continue;
978
979 WARN_ON(!pgd_present(pgd));
980 unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
981 } while (addr = next, addr < end);
982 }
983
free_empty_pte_table(pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)984 static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr,
985 unsigned long end, unsigned long floor,
986 unsigned long ceiling)
987 {
988 pte_t *ptep, pte;
989 unsigned long i, start = addr;
990
991 do {
992 ptep = pte_offset_kernel(pmdp, addr);
993 pte = READ_ONCE(*ptep);
994
995 /*
996 * This is just a sanity check here which verifies that
997 * pte clearing has been done by earlier unmap loops.
998 */
999 WARN_ON(!pte_none(pte));
1000 } while (addr += PAGE_SIZE, addr < end);
1001
1002 if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK))
1003 return;
1004
1005 /*
1006 * Check whether we can free the pte page if the rest of the
1007 * entries are empty. Overlap with other regions have been
1008 * handled by the floor/ceiling check.
1009 */
1010 ptep = pte_offset_kernel(pmdp, 0UL);
1011 for (i = 0; i < PTRS_PER_PTE; i++) {
1012 if (!pte_none(READ_ONCE(ptep[i])))
1013 return;
1014 }
1015
1016 pmd_clear(pmdp);
1017 __flush_tlb_kernel_pgtable(start);
1018 free_hotplug_pgtable_page(virt_to_page(ptep));
1019 }
1020
free_empty_pmd_table(pud_t * pudp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1021 static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
1022 unsigned long end, unsigned long floor,
1023 unsigned long ceiling)
1024 {
1025 pmd_t *pmdp, pmd;
1026 unsigned long i, next, start = addr;
1027
1028 do {
1029 next = pmd_addr_end(addr, end);
1030 pmdp = pmd_offset(pudp, addr);
1031 pmd = READ_ONCE(*pmdp);
1032 if (pmd_none(pmd))
1033 continue;
1034
1035 WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd));
1036 free_empty_pte_table(pmdp, addr, next, floor, ceiling);
1037 } while (addr = next, addr < end);
1038
1039 if (CONFIG_PGTABLE_LEVELS <= 2)
1040 return;
1041
1042 if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
1043 return;
1044
1045 /*
1046 * Check whether we can free the pmd page if the rest of the
1047 * entries are empty. Overlap with other regions have been
1048 * handled by the floor/ceiling check.
1049 */
1050 pmdp = pmd_offset(pudp, 0UL);
1051 for (i = 0; i < PTRS_PER_PMD; i++) {
1052 if (!pmd_none(READ_ONCE(pmdp[i])))
1053 return;
1054 }
1055
1056 pud_clear(pudp);
1057 __flush_tlb_kernel_pgtable(start);
1058 free_hotplug_pgtable_page(virt_to_page(pmdp));
1059 }
1060
free_empty_pud_table(p4d_t * p4dp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1061 static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,
1062 unsigned long end, unsigned long floor,
1063 unsigned long ceiling)
1064 {
1065 pud_t *pudp, pud;
1066 unsigned long i, next, start = addr;
1067
1068 do {
1069 next = pud_addr_end(addr, end);
1070 pudp = pud_offset(p4dp, addr);
1071 pud = READ_ONCE(*pudp);
1072 if (pud_none(pud))
1073 continue;
1074
1075 WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud));
1076 free_empty_pmd_table(pudp, addr, next, floor, ceiling);
1077 } while (addr = next, addr < end);
1078
1079 if (CONFIG_PGTABLE_LEVELS <= 3)
1080 return;
1081
1082 if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
1083 return;
1084
1085 /*
1086 * Check whether we can free the pud page if the rest of the
1087 * entries are empty. Overlap with other regions have been
1088 * handled by the floor/ceiling check.
1089 */
1090 pudp = pud_offset(p4dp, 0UL);
1091 for (i = 0; i < PTRS_PER_PUD; i++) {
1092 if (!pud_none(READ_ONCE(pudp[i])))
1093 return;
1094 }
1095
1096 p4d_clear(p4dp);
1097 __flush_tlb_kernel_pgtable(start);
1098 free_hotplug_pgtable_page(virt_to_page(pudp));
1099 }
1100
free_empty_p4d_table(pgd_t * pgdp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1101 static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
1102 unsigned long end, unsigned long floor,
1103 unsigned long ceiling)
1104 {
1105 unsigned long next;
1106 p4d_t *p4dp, p4d;
1107
1108 do {
1109 next = p4d_addr_end(addr, end);
1110 p4dp = p4d_offset(pgdp, addr);
1111 p4d = READ_ONCE(*p4dp);
1112 if (p4d_none(p4d))
1113 continue;
1114
1115 WARN_ON(!p4d_present(p4d));
1116 free_empty_pud_table(p4dp, addr, next, floor, ceiling);
1117 } while (addr = next, addr < end);
1118 }
1119
free_empty_tables(unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1120 static void free_empty_tables(unsigned long addr, unsigned long end,
1121 unsigned long floor, unsigned long ceiling)
1122 {
1123 unsigned long next;
1124 pgd_t *pgdp, pgd;
1125
1126 do {
1127 next = pgd_addr_end(addr, end);
1128 pgdp = pgd_offset_k(addr);
1129 pgd = READ_ONCE(*pgdp);
1130 if (pgd_none(pgd))
1131 continue;
1132
1133 WARN_ON(!pgd_present(pgd));
1134 free_empty_p4d_table(pgdp, addr, next, floor, ceiling);
1135 } while (addr = next, addr < end);
1136 }
1137 #endif
1138
1139 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1140 #if !ARM64_SWAPPER_USES_SECTION_MAPS
vmemmap_populate(unsigned long start,unsigned long end,int node,struct vmem_altmap * altmap)1141 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1142 struct vmem_altmap *altmap)
1143 {
1144 return vmemmap_populate_basepages(start, end, node, altmap);
1145 }
1146 #else /* !ARM64_SWAPPER_USES_SECTION_MAPS */
vmemmap_populate(unsigned long start,unsigned long end,int node,struct vmem_altmap * altmap)1147 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1148 struct vmem_altmap *altmap)
1149 {
1150 unsigned long addr = start;
1151 unsigned long next;
1152 pgd_t *pgdp;
1153 p4d_t *p4dp;
1154 pud_t *pudp;
1155 pmd_t *pmdp;
1156
1157 do {
1158 next = pmd_addr_end(addr, end);
1159
1160 pgdp = vmemmap_pgd_populate(addr, node);
1161 if (!pgdp)
1162 return -ENOMEM;
1163
1164 p4dp = vmemmap_p4d_populate(pgdp, addr, node);
1165 if (!p4dp)
1166 return -ENOMEM;
1167
1168 pudp = vmemmap_pud_populate(p4dp, addr, node);
1169 if (!pudp)
1170 return -ENOMEM;
1171
1172 pmdp = pmd_offset(pudp, addr);
1173 if (pmd_none(READ_ONCE(*pmdp))) {
1174 void *p = NULL;
1175
1176 p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
1177 if (!p) {
1178 if (vmemmap_populate_basepages(addr, next, node, altmap))
1179 return -ENOMEM;
1180 continue;
1181 }
1182
1183 pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
1184 } else
1185 vmemmap_verify((pte_t *)pmdp, node, addr, next);
1186 } while (addr = next, addr != end);
1187
1188 return 0;
1189 }
1190 #endif /* !ARM64_SWAPPER_USES_SECTION_MAPS */
vmemmap_free(unsigned long start,unsigned long end,struct vmem_altmap * altmap)1191 void vmemmap_free(unsigned long start, unsigned long end,
1192 struct vmem_altmap *altmap)
1193 {
1194 #ifdef CONFIG_MEMORY_HOTPLUG
1195 WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1196
1197 unmap_hotplug_range(start, end, true, altmap);
1198 free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END);
1199 #endif
1200 }
1201 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
1202
fixmap_pud(unsigned long addr)1203 static inline pud_t * fixmap_pud(unsigned long addr)
1204 {
1205 pgd_t *pgdp = pgd_offset_k(addr);
1206 p4d_t *p4dp = p4d_offset(pgdp, addr);
1207 p4d_t p4d = READ_ONCE(*p4dp);
1208
1209 BUG_ON(p4d_none(p4d) || p4d_bad(p4d));
1210
1211 return pud_offset_kimg(p4dp, addr);
1212 }
1213
fixmap_pmd(unsigned long addr)1214 static inline pmd_t * fixmap_pmd(unsigned long addr)
1215 {
1216 pud_t *pudp = fixmap_pud(addr);
1217 pud_t pud = READ_ONCE(*pudp);
1218
1219 BUG_ON(pud_none(pud) || pud_bad(pud));
1220
1221 return pmd_offset_kimg(pudp, addr);
1222 }
1223
fixmap_pte(unsigned long addr)1224 static inline pte_t * fixmap_pte(unsigned long addr)
1225 {
1226 return &bm_pte[pte_index(addr)];
1227 }
1228
1229 /*
1230 * The p*d_populate functions call virt_to_phys implicitly so they can't be used
1231 * directly on kernel symbols (bm_p*d). This function is called too early to use
1232 * lm_alias so __p*d_populate functions must be used to populate with the
1233 * physical address from __pa_symbol.
1234 */
early_fixmap_init(void)1235 void __init early_fixmap_init(void)
1236 {
1237 pgd_t *pgdp;
1238 p4d_t *p4dp, p4d;
1239 pud_t *pudp;
1240 pmd_t *pmdp;
1241 unsigned long addr = FIXADDR_START;
1242
1243 pgdp = pgd_offset_k(addr);
1244 p4dp = p4d_offset(pgdp, addr);
1245 p4d = READ_ONCE(*p4dp);
1246 if (CONFIG_PGTABLE_LEVELS > 3 &&
1247 !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) {
1248 /*
1249 * We only end up here if the kernel mapping and the fixmap
1250 * share the top level pgd entry, which should only happen on
1251 * 16k/4 levels configurations.
1252 */
1253 BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
1254 pudp = pud_offset_kimg(p4dp, addr);
1255 } else {
1256 if (p4d_none(p4d))
1257 __p4d_populate(p4dp, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
1258 pudp = fixmap_pud(addr);
1259 }
1260 if (pud_none(READ_ONCE(*pudp)))
1261 __pud_populate(pudp, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
1262 pmdp = fixmap_pmd(addr);
1263 __pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
1264
1265 /*
1266 * The boot-ioremap range spans multiple pmds, for which
1267 * we are not prepared:
1268 */
1269 BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
1270 != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
1271
1272 if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
1273 || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
1274 WARN_ON(1);
1275 pr_warn("pmdp %p != %p, %p\n",
1276 pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
1277 fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
1278 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
1279 fix_to_virt(FIX_BTMAP_BEGIN));
1280 pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
1281 fix_to_virt(FIX_BTMAP_END));
1282
1283 pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
1284 pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
1285 }
1286 }
1287
1288 /*
1289 * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
1290 * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
1291 */
__set_fixmap(enum fixed_addresses idx,phys_addr_t phys,pgprot_t flags)1292 void __set_fixmap(enum fixed_addresses idx,
1293 phys_addr_t phys, pgprot_t flags)
1294 {
1295 unsigned long addr = __fix_to_virt(idx);
1296 pte_t *ptep;
1297
1298 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
1299
1300 ptep = fixmap_pte(addr);
1301
1302 if (pgprot_val(flags)) {
1303 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
1304 } else {
1305 pte_clear(&init_mm, addr, ptep);
1306 flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
1307 }
1308 }
1309
fixmap_remap_fdt(phys_addr_t dt_phys,int * size,pgprot_t prot)1310 void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
1311 {
1312 const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
1313 int offset;
1314 void *dt_virt;
1315
1316 /*
1317 * Check whether the physical FDT address is set and meets the minimum
1318 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
1319 * at least 8 bytes so that we can always access the magic and size
1320 * fields of the FDT header after mapping the first chunk, double check
1321 * here if that is indeed the case.
1322 */
1323 BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
1324 if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
1325 return NULL;
1326
1327 /*
1328 * Make sure that the FDT region can be mapped without the need to
1329 * allocate additional translation table pages, so that it is safe
1330 * to call create_mapping_noalloc() this early.
1331 *
1332 * On 64k pages, the FDT will be mapped using PTEs, so we need to
1333 * be in the same PMD as the rest of the fixmap.
1334 * On 4k pages, we'll use section mappings for the FDT so we only
1335 * have to be in the same PUD.
1336 */
1337 BUILD_BUG_ON(dt_virt_base % SZ_2M);
1338
1339 BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
1340 __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
1341
1342 offset = dt_phys % SWAPPER_BLOCK_SIZE;
1343 dt_virt = (void *)dt_virt_base + offset;
1344
1345 /* map the first chunk so we can read the size from the header */
1346 create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
1347 dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
1348
1349 if (fdt_magic(dt_virt) != FDT_MAGIC)
1350 return NULL;
1351
1352 *size = fdt_totalsize(dt_virt);
1353 if (*size > MAX_FDT_SIZE)
1354 return NULL;
1355
1356 if (offset + *size > SWAPPER_BLOCK_SIZE)
1357 create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
1358 round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
1359
1360 return dt_virt;
1361 }
1362
arch_ioremap_p4d_supported(void)1363 int __init arch_ioremap_p4d_supported(void)
1364 {
1365 return 0;
1366 }
1367
arch_ioremap_pud_supported(void)1368 int __init arch_ioremap_pud_supported(void)
1369 {
1370 /*
1371 * Only 4k granule supports level 1 block mappings.
1372 * SW table walks can't handle removal of intermediate entries.
1373 */
1374 return IS_ENABLED(CONFIG_ARM64_4K_PAGES) &&
1375 !IS_ENABLED(CONFIG_PTDUMP_DEBUGFS);
1376 }
1377
arch_ioremap_pmd_supported(void)1378 int __init arch_ioremap_pmd_supported(void)
1379 {
1380 /* See arch_ioremap_pud_supported() */
1381 return !IS_ENABLED(CONFIG_PTDUMP_DEBUGFS);
1382 }
1383
pud_set_huge(pud_t * pudp,phys_addr_t phys,pgprot_t prot)1384 int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
1385 {
1386 pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot));
1387
1388 /* Only allow permission changes for now */
1389 if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
1390 pud_val(new_pud)))
1391 return 0;
1392
1393 VM_BUG_ON(phys & ~PUD_MASK);
1394 set_pud(pudp, new_pud);
1395 return 1;
1396 }
1397
pmd_set_huge(pmd_t * pmdp,phys_addr_t phys,pgprot_t prot)1398 int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
1399 {
1400 pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot));
1401
1402 /* Only allow permission changes for now */
1403 if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
1404 pmd_val(new_pmd)))
1405 return 0;
1406
1407 VM_BUG_ON(phys & ~PMD_MASK);
1408 set_pmd(pmdp, new_pmd);
1409 return 1;
1410 }
1411
pud_clear_huge(pud_t * pudp)1412 int pud_clear_huge(pud_t *pudp)
1413 {
1414 if (!pud_sect(READ_ONCE(*pudp)))
1415 return 0;
1416 pud_clear(pudp);
1417 return 1;
1418 }
1419
pmd_clear_huge(pmd_t * pmdp)1420 int pmd_clear_huge(pmd_t *pmdp)
1421 {
1422 if (!pmd_sect(READ_ONCE(*pmdp)))
1423 return 0;
1424 pmd_clear(pmdp);
1425 return 1;
1426 }
1427
pmd_free_pte_page(pmd_t * pmdp,unsigned long addr)1428 int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
1429 {
1430 pte_t *table;
1431 pmd_t pmd;
1432
1433 pmd = READ_ONCE(*pmdp);
1434
1435 if (!pmd_table(pmd)) {
1436 VM_WARN_ON(1);
1437 return 1;
1438 }
1439
1440 table = pte_offset_kernel(pmdp, addr);
1441 pmd_clear(pmdp);
1442 __flush_tlb_kernel_pgtable(addr);
1443 pte_free_kernel(NULL, table);
1444 return 1;
1445 }
1446
pud_free_pmd_page(pud_t * pudp,unsigned long addr)1447 int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
1448 {
1449 pmd_t *table;
1450 pmd_t *pmdp;
1451 pud_t pud;
1452 unsigned long next, end;
1453
1454 pud = READ_ONCE(*pudp);
1455
1456 if (!pud_table(pud)) {
1457 VM_WARN_ON(1);
1458 return 1;
1459 }
1460
1461 table = pmd_offset(pudp, addr);
1462 pmdp = table;
1463 next = addr;
1464 end = addr + PUD_SIZE;
1465 do {
1466 pmd_free_pte_page(pmdp, next);
1467 } while (pmdp++, next += PMD_SIZE, next != end);
1468
1469 pud_clear(pudp);
1470 __flush_tlb_kernel_pgtable(addr);
1471 pmd_free(NULL, table);
1472 return 1;
1473 }
1474
p4d_free_pud_page(p4d_t * p4d,unsigned long addr)1475 int p4d_free_pud_page(p4d_t *p4d, unsigned long addr)
1476 {
1477 return 0; /* Don't attempt a block mapping */
1478 }
1479
1480 #ifdef CONFIG_MEMORY_HOTPLUG
__remove_pgd_mapping(pgd_t * pgdir,unsigned long start,u64 size)1481 static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
1482 {
1483 unsigned long end = start + size;
1484
1485 WARN_ON(pgdir != init_mm.pgd);
1486 WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END));
1487
1488 unmap_hotplug_range(start, end, false, NULL);
1489 free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
1490 }
1491
inside_linear_region(u64 start,u64 size)1492 static bool inside_linear_region(u64 start, u64 size)
1493 {
1494 u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual));
1495 u64 end_linear_pa = __pa(PAGE_END - 1);
1496
1497 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
1498 /*
1499 * Check for a wrap, it is possible because of randomized linear
1500 * mapping the start physical address is actually bigger than
1501 * the end physical address. In this case set start to zero
1502 * because [0, end_linear_pa] range must still be able to cover
1503 * all addressable physical addresses.
1504 */
1505 if (start_linear_pa > end_linear_pa)
1506 start_linear_pa = 0;
1507 }
1508
1509 WARN_ON(start_linear_pa > end_linear_pa);
1510
1511 /*
1512 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
1513 * accommodating both its ends but excluding PAGE_END. Max physical
1514 * range which can be mapped inside this linear mapping range, must
1515 * also be derived from its end points.
1516 */
1517 return start >= start_linear_pa && (start + size - 1) <= end_linear_pa;
1518 }
1519
arch_add_memory(int nid,u64 start,u64 size,struct mhp_params * params)1520 int arch_add_memory(int nid, u64 start, u64 size,
1521 struct mhp_params *params)
1522 {
1523 int ret, flags = 0;
1524
1525 if (!inside_linear_region(start, size)) {
1526 pr_err("[%llx %llx] is outside linear mapping region\n", start, start + size);
1527 return -EINVAL;
1528 }
1529
1530 /*
1531 * KFENCE requires linear map to be mapped at page granularity, so that
1532 * it is possible to protect/unprotect single pages in the KFENCE pool.
1533 */
1534 if (rodata_full || debug_pagealloc_enabled() ||
1535 IS_ENABLED(CONFIG_KFENCE))
1536 flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
1537
1538 __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
1539 size, params->pgprot, __pgd_pgtable_alloc,
1540 flags);
1541
1542 memblock_clear_nomap(start, size);
1543
1544 ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
1545 params);
1546 if (ret)
1547 __remove_pgd_mapping(swapper_pg_dir,
1548 __phys_to_virt(start), size);
1549 else {
1550 max_pfn = PFN_UP(start + size);
1551 max_low_pfn = max_pfn;
1552 }
1553
1554 return ret;
1555 }
1556
arch_remove_memory(int nid,u64 start,u64 size,struct vmem_altmap * altmap)1557 void arch_remove_memory(int nid, u64 start, u64 size,
1558 struct vmem_altmap *altmap)
1559 {
1560 unsigned long start_pfn = start >> PAGE_SHIFT;
1561 unsigned long nr_pages = size >> PAGE_SHIFT;
1562
1563 __remove_pages(start_pfn, nr_pages, altmap);
1564 __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
1565 }
1566
check_range_driver_managed(u64 start,u64 size,const char * resource_name)1567 int check_range_driver_managed(u64 start, u64 size, const char *resource_name)
1568 {
1569 struct mem_section *ms;
1570 unsigned long pfn = __phys_to_pfn(start);
1571 unsigned long end_pfn = __phys_to_pfn(start + size);
1572 struct resource *res;
1573 unsigned long flags;
1574
1575 res = lookup_resource(&iomem_resource, start);
1576 if (!res) {
1577 pr_err("%s: couldn't find memory resource for start 0x%llx\n",
1578 __func__, start);
1579 return -EINVAL;
1580 }
1581
1582 flags = res->flags;
1583
1584 if (!(flags & IORESOURCE_SYSRAM_DRIVER_MANAGED) ||
1585 strstr(resource_name, "System RAM (") != resource_name)
1586 return -EINVAL;
1587
1588 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1589 ms = __pfn_to_section(pfn);
1590 if (early_section(ms))
1591 return -EINVAL;
1592 }
1593
1594 return 0;
1595 }
1596
populate_range_driver_managed(u64 start,u64 size,const char * resource_name)1597 int populate_range_driver_managed(u64 start, u64 size,
1598 const char *resource_name)
1599 {
1600 unsigned long virt = (unsigned long)phys_to_virt(start);
1601 int flags = 0;
1602
1603 if (check_range_driver_managed(start, size, resource_name))
1604 return -EINVAL;
1605
1606 /*
1607 * When rodata_full is enabled, memory is mapped at page size granule,
1608 * as opposed to block mapping.
1609 */
1610 if (rodata_full || debug_pagealloc_enabled())
1611 flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
1612
1613 __create_pgd_mapping(init_mm.pgd, start, virt, size,
1614 PAGE_KERNEL, NULL, flags);
1615
1616 return 0;
1617 }
1618 EXPORT_SYMBOL_GPL(populate_range_driver_managed);
1619
depopulate_range_driver_managed(u64 start,u64 size,const char * resource_name)1620 int depopulate_range_driver_managed(u64 start, u64 size,
1621 const char *resource_name)
1622 {
1623 if (check_range_driver_managed(start, size, resource_name))
1624 return -EINVAL;
1625
1626 unmap_hotplug_range(start, start + size, false, NULL);
1627
1628 return 0;
1629 }
1630 EXPORT_SYMBOL_GPL(depopulate_range_driver_managed);
1631
1632 /*
1633 * This memory hotplug notifier helps prevent boot memory from being
1634 * inadvertently removed as it blocks pfn range offlining process in
1635 * __offline_pages(). Hence this prevents both offlining as well as
1636 * removal process for boot memory which is initially always online.
1637 * In future if and when boot memory could be removed, this notifier
1638 * should be dropped and free_hotplug_page_range() should handle any
1639 * reserved pages allocated during boot.
1640 */
prevent_bootmem_remove_notifier(struct notifier_block * nb,unsigned long action,void * data)1641 static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
1642 unsigned long action, void *data)
1643 {
1644 struct mem_section *ms;
1645 struct memory_notify *arg = data;
1646 unsigned long end_pfn = arg->start_pfn + arg->nr_pages;
1647 unsigned long pfn = arg->start_pfn;
1648
1649 if (action != MEM_GOING_OFFLINE)
1650 return NOTIFY_OK;
1651
1652 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1653 ms = __pfn_to_section(pfn);
1654 if (early_section(ms))
1655 return NOTIFY_BAD;
1656 }
1657 return NOTIFY_OK;
1658 }
1659
1660 static struct notifier_block prevent_bootmem_remove_nb = {
1661 .notifier_call = prevent_bootmem_remove_notifier,
1662 };
1663
prevent_bootmem_remove_init(void)1664 static int __init prevent_bootmem_remove_init(void)
1665 {
1666 return register_memory_notifier(&prevent_bootmem_remove_nb);
1667 }
1668 device_initcall(prevent_bootmem_remove_init);
1669 #endif
1670