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