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