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 #include <linux/kfence.h>
28 #include <linux/pkeys.h>
29 #include <linux/mm_inline.h>
30 #include <linux/pagewalk.h>
31 #include <linux/stop_machine.h>
32
33 #include <asm/barrier.h>
34 #include <asm/cputype.h>
35 #include <asm/fixmap.h>
36 #include <asm/kasan.h>
37 #include <asm/kernel-pgtable.h>
38 #include <asm/sections.h>
39 #include <asm/setup.h>
40 #include <linux/sizes.h>
41 #include <asm/tlb.h>
42 #include <asm/mmu_context.h>
43 #include <asm/ptdump.h>
44 #include <asm/tlbflush.h>
45 #include <asm/pgalloc.h>
46 #include <asm/kfence.h>
47
48 #define NO_BLOCK_MAPPINGS BIT(0)
49 #define NO_CONT_MAPPINGS BIT(1)
50 #define NO_EXEC_MAPPINGS BIT(2) /* assumes FEAT_HPDS is not used */
51
52 DEFINE_STATIC_KEY_FALSE(arm64_ptdump_lock_key);
53
54 u64 kimage_voffset __ro_after_init;
55 EXPORT_SYMBOL(kimage_voffset);
56
57 u32 __boot_cpu_mode[] = { BOOT_CPU_MODE_EL2, BOOT_CPU_MODE_EL1 };
58
59 static bool rodata_is_rw __ro_after_init = true;
60
61 /*
62 * The booting CPU updates the failed status @__early_cpu_boot_status,
63 * with MMU turned off.
64 */
65 long __section(".mmuoff.data.write") __early_cpu_boot_status;
66
67 /*
68 * Empty_zero_page is a special page that is used for zero-initialized data
69 * and COW.
70 */
71 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
72 EXPORT_SYMBOL(empty_zero_page);
73
74 static DEFINE_SPINLOCK(swapper_pgdir_lock);
75 static DEFINE_MUTEX(fixmap_lock);
76
set_swapper_pgd(pgd_t * pgdp,pgd_t pgd)77 void noinstr set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
78 {
79 pgd_t *fixmap_pgdp;
80
81 /*
82 * Don't bother with the fixmap if swapper_pg_dir is still mapped
83 * writable in the kernel mapping.
84 */
85 if (rodata_is_rw) {
86 WRITE_ONCE(*pgdp, pgd);
87 dsb(ishst);
88 isb();
89 return;
90 }
91
92 spin_lock(&swapper_pgdir_lock);
93 fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
94 WRITE_ONCE(*fixmap_pgdp, pgd);
95 /*
96 * We need dsb(ishst) here to ensure the page-table-walker sees
97 * our new entry before set_p?d() returns. The fixmap's
98 * flush_tlb_kernel_range() via clear_fixmap() does this for us.
99 */
100 pgd_clear_fixmap();
101 spin_unlock(&swapper_pgdir_lock);
102 }
103
phys_mem_access_prot(struct file * file,unsigned long pfn,unsigned long size,pgprot_t vma_prot)104 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
105 unsigned long size, pgprot_t vma_prot)
106 {
107 if (!pfn_is_map_memory(pfn))
108 return pgprot_noncached(vma_prot);
109 else if (file->f_flags & O_SYNC)
110 return pgprot_writecombine(vma_prot);
111 return vma_prot;
112 }
113 EXPORT_SYMBOL(phys_mem_access_prot);
114
early_pgtable_alloc(enum pgtable_type pgtable_type)115 static phys_addr_t __init early_pgtable_alloc(enum pgtable_type pgtable_type)
116 {
117 phys_addr_t phys;
118
119 phys = memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE, 0,
120 MEMBLOCK_ALLOC_NOLEAKTRACE);
121 if (!phys)
122 panic("Failed to allocate page table page\n");
123
124 return phys;
125 }
126
pgattr_change_is_safe(pteval_t old,pteval_t new)127 bool pgattr_change_is_safe(pteval_t old, pteval_t new)
128 {
129 /*
130 * The following mapping attributes may be updated in live
131 * kernel mappings without the need for break-before-make.
132 */
133 pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG |
134 PTE_SWBITS_MASK;
135
136 /* creating or taking down mappings is always safe */
137 if (!pte_valid(__pte(old)) || !pte_valid(__pte(new)))
138 return true;
139
140 /* A live entry's pfn should not change */
141 if (pte_pfn(__pte(old)) != pte_pfn(__pte(new)))
142 return false;
143
144 /* live contiguous mappings may not be manipulated at all */
145 if ((old | new) & PTE_CONT)
146 return false;
147
148 /* Transitioning from Non-Global to Global is unsafe */
149 if (old & ~new & PTE_NG)
150 return false;
151
152 /*
153 * Changing the memory type between Normal and Normal-Tagged is safe
154 * since Tagged is considered a permission attribute from the
155 * mismatched attribute aliases perspective.
156 */
157 if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
158 (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) &&
159 ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
160 (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)))
161 mask |= PTE_ATTRINDX_MASK;
162
163 return ((old ^ new) & ~mask) == 0;
164 }
165
init_clear_pgtable(void * table)166 static void init_clear_pgtable(void *table)
167 {
168 clear_page(table);
169
170 /* Ensure the zeroing is observed by page table walks. */
171 dsb(ishst);
172 }
173
init_pte(pte_t * ptep,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot)174 static void init_pte(pte_t *ptep, unsigned long addr, unsigned long end,
175 phys_addr_t phys, pgprot_t prot)
176 {
177 do {
178 pte_t old_pte = __ptep_get(ptep);
179
180 /*
181 * Required barriers to make this visible to the table walker
182 * are deferred to the end of alloc_init_cont_pte().
183 */
184 __set_pte_nosync(ptep, pfn_pte(__phys_to_pfn(phys), prot));
185
186 /*
187 * After the PTE entry has been populated once, we
188 * only allow updates to the permission attributes.
189 */
190 BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
191 pte_val(__ptep_get(ptep))));
192
193 phys += PAGE_SIZE;
194 } while (ptep++, addr += PAGE_SIZE, addr != end);
195 }
196
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)(enum pgtable_type),int flags)197 static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
198 unsigned long end, phys_addr_t phys,
199 pgprot_t prot,
200 phys_addr_t (*pgtable_alloc)(enum pgtable_type),
201 int flags)
202 {
203 unsigned long next;
204 pmd_t pmd = READ_ONCE(*pmdp);
205 pte_t *ptep;
206
207 BUG_ON(pmd_sect(pmd));
208 if (pmd_none(pmd)) {
209 pmdval_t pmdval = PMD_TYPE_TABLE | PMD_TABLE_UXN | PMD_TABLE_AF;
210 phys_addr_t pte_phys;
211
212 if (flags & NO_EXEC_MAPPINGS)
213 pmdval |= PMD_TABLE_PXN;
214 BUG_ON(!pgtable_alloc);
215 pte_phys = pgtable_alloc(TABLE_PTE);
216 ptep = pte_set_fixmap(pte_phys);
217 init_clear_pgtable(ptep);
218 ptep += pte_index(addr);
219 __pmd_populate(pmdp, pte_phys, pmdval);
220 } else {
221 BUG_ON(pmd_bad(pmd));
222 ptep = pte_set_fixmap_offset(pmdp, addr);
223 }
224
225 do {
226 pgprot_t __prot = prot;
227
228 next = pte_cont_addr_end(addr, end);
229
230 /* use a contiguous mapping if the range is suitably aligned */
231 if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
232 (flags & NO_CONT_MAPPINGS) == 0)
233 __prot = __pgprot(pgprot_val(prot) | PTE_CONT);
234
235 init_pte(ptep, addr, next, phys, __prot);
236
237 ptep += pte_index(next) - pte_index(addr);
238 phys += next - addr;
239 } while (addr = next, addr != end);
240
241 /*
242 * Note: barriers and maintenance necessary to clear the fixmap slot
243 * ensure that all previous pgtable writes are visible to the table
244 * walker.
245 */
246 pte_clear_fixmap();
247 }
248
init_pmd(pmd_t * pmdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)249 static void init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end,
250 phys_addr_t phys, pgprot_t prot,
251 phys_addr_t (*pgtable_alloc)(enum pgtable_type), int flags)
252 {
253 unsigned long next;
254
255 do {
256 pmd_t old_pmd = READ_ONCE(*pmdp);
257
258 next = pmd_addr_end(addr, end);
259
260 /* try section mapping first */
261 if (((addr | next | phys) & ~PMD_MASK) == 0 &&
262 (flags & NO_BLOCK_MAPPINGS) == 0) {
263 pmd_set_huge(pmdp, phys, prot);
264
265 /*
266 * After the PMD entry has been populated once, we
267 * only allow updates to the permission attributes.
268 */
269 BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
270 READ_ONCE(pmd_val(*pmdp))));
271 } else {
272 alloc_init_cont_pte(pmdp, addr, next, phys, prot,
273 pgtable_alloc, flags);
274
275 BUG_ON(pmd_val(old_pmd) != 0 &&
276 pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
277 }
278 phys += next - addr;
279 } while (pmdp++, addr = next, addr != end);
280 }
281
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)(enum pgtable_type),int flags)282 static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
283 unsigned long end, phys_addr_t phys,
284 pgprot_t prot,
285 phys_addr_t (*pgtable_alloc)(enum pgtable_type),
286 int flags)
287 {
288 unsigned long next;
289 pud_t pud = READ_ONCE(*pudp);
290 pmd_t *pmdp;
291
292 /*
293 * Check for initial section mappings in the pgd/pud.
294 */
295 BUG_ON(pud_sect(pud));
296 if (pud_none(pud)) {
297 pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_UXN | PUD_TABLE_AF;
298 phys_addr_t pmd_phys;
299
300 if (flags & NO_EXEC_MAPPINGS)
301 pudval |= PUD_TABLE_PXN;
302 BUG_ON(!pgtable_alloc);
303 pmd_phys = pgtable_alloc(TABLE_PMD);
304 pmdp = pmd_set_fixmap(pmd_phys);
305 init_clear_pgtable(pmdp);
306 pmdp += pmd_index(addr);
307 __pud_populate(pudp, pmd_phys, pudval);
308 } else {
309 BUG_ON(pud_bad(pud));
310 pmdp = pmd_set_fixmap_offset(pudp, addr);
311 }
312
313 do {
314 pgprot_t __prot = prot;
315
316 next = pmd_cont_addr_end(addr, end);
317
318 /* use a contiguous mapping if the range is suitably aligned */
319 if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
320 (flags & NO_CONT_MAPPINGS) == 0)
321 __prot = __pgprot(pgprot_val(prot) | PTE_CONT);
322
323 init_pmd(pmdp, addr, next, phys, __prot, pgtable_alloc, flags);
324
325 pmdp += pmd_index(next) - pmd_index(addr);
326 phys += next - addr;
327 } while (addr = next, addr != end);
328
329 pmd_clear_fixmap();
330 }
331
alloc_init_pud(p4d_t * p4dp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)332 static void alloc_init_pud(p4d_t *p4dp, unsigned long addr, unsigned long end,
333 phys_addr_t phys, pgprot_t prot,
334 phys_addr_t (*pgtable_alloc)(enum pgtable_type),
335 int flags)
336 {
337 unsigned long next;
338 p4d_t p4d = READ_ONCE(*p4dp);
339 pud_t *pudp;
340
341 if (p4d_none(p4d)) {
342 p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN | P4D_TABLE_AF;
343 phys_addr_t pud_phys;
344
345 if (flags & NO_EXEC_MAPPINGS)
346 p4dval |= P4D_TABLE_PXN;
347 BUG_ON(!pgtable_alloc);
348 pud_phys = pgtable_alloc(TABLE_PUD);
349 pudp = pud_set_fixmap(pud_phys);
350 init_clear_pgtable(pudp);
351 pudp += pud_index(addr);
352 __p4d_populate(p4dp, pud_phys, p4dval);
353 } else {
354 BUG_ON(p4d_bad(p4d));
355 pudp = pud_set_fixmap_offset(p4dp, addr);
356 }
357
358 do {
359 pud_t old_pud = READ_ONCE(*pudp);
360
361 next = pud_addr_end(addr, end);
362
363 /*
364 * For 4K granule only, attempt to put down a 1GB block
365 */
366 if (pud_sect_supported() &&
367 ((addr | next | phys) & ~PUD_MASK) == 0 &&
368 (flags & NO_BLOCK_MAPPINGS) == 0) {
369 pud_set_huge(pudp, phys, prot);
370
371 /*
372 * After the PUD entry has been populated once, we
373 * only allow updates to the permission attributes.
374 */
375 BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
376 READ_ONCE(pud_val(*pudp))));
377 } else {
378 alloc_init_cont_pmd(pudp, addr, next, phys, prot,
379 pgtable_alloc, flags);
380
381 BUG_ON(pud_val(old_pud) != 0 &&
382 pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
383 }
384 phys += next - addr;
385 } while (pudp++, addr = next, addr != end);
386
387 pud_clear_fixmap();
388 }
389
alloc_init_p4d(pgd_t * pgdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)390 static void alloc_init_p4d(pgd_t *pgdp, unsigned long addr, unsigned long end,
391 phys_addr_t phys, pgprot_t prot,
392 phys_addr_t (*pgtable_alloc)(enum pgtable_type),
393 int flags)
394 {
395 unsigned long next;
396 pgd_t pgd = READ_ONCE(*pgdp);
397 p4d_t *p4dp;
398
399 if (pgd_none(pgd)) {
400 pgdval_t pgdval = PGD_TYPE_TABLE | PGD_TABLE_UXN | PGD_TABLE_AF;
401 phys_addr_t p4d_phys;
402
403 if (flags & NO_EXEC_MAPPINGS)
404 pgdval |= PGD_TABLE_PXN;
405 BUG_ON(!pgtable_alloc);
406 p4d_phys = pgtable_alloc(TABLE_P4D);
407 p4dp = p4d_set_fixmap(p4d_phys);
408 init_clear_pgtable(p4dp);
409 p4dp += p4d_index(addr);
410 __pgd_populate(pgdp, p4d_phys, pgdval);
411 } else {
412 BUG_ON(pgd_bad(pgd));
413 p4dp = p4d_set_fixmap_offset(pgdp, addr);
414 }
415
416 do {
417 p4d_t old_p4d = READ_ONCE(*p4dp);
418
419 next = p4d_addr_end(addr, end);
420
421 alloc_init_pud(p4dp, addr, next, phys, prot,
422 pgtable_alloc, flags);
423
424 BUG_ON(p4d_val(old_p4d) != 0 &&
425 p4d_val(old_p4d) != READ_ONCE(p4d_val(*p4dp)));
426
427 phys += next - addr;
428 } while (p4dp++, addr = next, addr != end);
429
430 p4d_clear_fixmap();
431 }
432
__create_pgd_mapping_locked(pgd_t * pgdir,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)433 static void __create_pgd_mapping_locked(pgd_t *pgdir, phys_addr_t phys,
434 unsigned long virt, phys_addr_t size,
435 pgprot_t prot,
436 phys_addr_t (*pgtable_alloc)(enum pgtable_type),
437 int flags)
438 {
439 unsigned long addr, end, next;
440 pgd_t *pgdp = pgd_offset_pgd(pgdir, virt);
441
442 /*
443 * If the virtual and physical address don't have the same offset
444 * within a page, we cannot map the region as the caller expects.
445 */
446 if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
447 return;
448
449 phys &= PAGE_MASK;
450 addr = virt & PAGE_MASK;
451 end = PAGE_ALIGN(virt + size);
452
453 do {
454 next = pgd_addr_end(addr, end);
455 alloc_init_p4d(pgdp, addr, next, phys, prot, pgtable_alloc,
456 flags);
457 phys += next - addr;
458 } while (pgdp++, addr = next, addr != end);
459 }
460
__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)(enum pgtable_type),int flags)461 static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
462 unsigned long virt, phys_addr_t size,
463 pgprot_t prot,
464 phys_addr_t (*pgtable_alloc)(enum pgtable_type),
465 int flags)
466 {
467 mutex_lock(&fixmap_lock);
468 __create_pgd_mapping_locked(pgdir, phys, virt, size, prot,
469 pgtable_alloc, flags);
470 mutex_unlock(&fixmap_lock);
471 }
472
473 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
474 extern __alias(__create_pgd_mapping_locked)
475 void create_kpti_ng_temp_pgd(pgd_t *pgdir, phys_addr_t phys, unsigned long virt,
476 phys_addr_t size, pgprot_t prot,
477 phys_addr_t (*pgtable_alloc)(enum pgtable_type),
478 int flags);
479 #endif
480
481 #define INVALID_PHYS_ADDR (-1ULL)
482
__pgd_pgtable_alloc(struct mm_struct * mm,gfp_t gfp,enum pgtable_type pgtable_type)483 static phys_addr_t __pgd_pgtable_alloc(struct mm_struct *mm, gfp_t gfp,
484 enum pgtable_type pgtable_type)
485 {
486 /* Page is zeroed by init_clear_pgtable() so don't duplicate effort. */
487 struct ptdesc *ptdesc = pagetable_alloc(gfp & ~__GFP_ZERO, 0);
488 phys_addr_t pa;
489
490 if (!ptdesc)
491 return INVALID_PHYS_ADDR;
492
493 pa = page_to_phys(ptdesc_page(ptdesc));
494
495 switch (pgtable_type) {
496 case TABLE_PTE:
497 BUG_ON(!pagetable_pte_ctor(mm, ptdesc));
498 break;
499 case TABLE_PMD:
500 BUG_ON(!pagetable_pmd_ctor(mm, ptdesc));
501 break;
502 case TABLE_PUD:
503 pagetable_pud_ctor(ptdesc);
504 break;
505 case TABLE_P4D:
506 pagetable_p4d_ctor(ptdesc);
507 break;
508 }
509
510 return pa;
511 }
512
513 static phys_addr_t
try_pgd_pgtable_alloc_init_mm(enum pgtable_type pgtable_type,gfp_t gfp)514 try_pgd_pgtable_alloc_init_mm(enum pgtable_type pgtable_type, gfp_t gfp)
515 {
516 return __pgd_pgtable_alloc(&init_mm, gfp, pgtable_type);
517 }
518
519 static phys_addr_t __maybe_unused
pgd_pgtable_alloc_init_mm(enum pgtable_type pgtable_type)520 pgd_pgtable_alloc_init_mm(enum pgtable_type pgtable_type)
521 {
522 phys_addr_t pa;
523
524 pa = __pgd_pgtable_alloc(&init_mm, GFP_PGTABLE_KERNEL, pgtable_type);
525 BUG_ON(pa == INVALID_PHYS_ADDR);
526 return pa;
527 }
528
529 static phys_addr_t
pgd_pgtable_alloc_special_mm(enum pgtable_type pgtable_type)530 pgd_pgtable_alloc_special_mm(enum pgtable_type pgtable_type)
531 {
532 phys_addr_t pa;
533
534 pa = __pgd_pgtable_alloc(NULL, GFP_PGTABLE_KERNEL, pgtable_type);
535 BUG_ON(pa == INVALID_PHYS_ADDR);
536 return pa;
537 }
538
split_contpte(pte_t * ptep)539 static void split_contpte(pte_t *ptep)
540 {
541 int i;
542
543 ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);
544 for (i = 0; i < CONT_PTES; i++, ptep++)
545 __set_pte(ptep, pte_mknoncont(__ptep_get(ptep)));
546 }
547
split_pmd(pmd_t * pmdp,pmd_t pmd,gfp_t gfp,bool to_cont)548 static int split_pmd(pmd_t *pmdp, pmd_t pmd, gfp_t gfp, bool to_cont)
549 {
550 pmdval_t tableprot = PMD_TYPE_TABLE | PMD_TABLE_UXN | PMD_TABLE_AF;
551 unsigned long pfn = pmd_pfn(pmd);
552 pgprot_t prot = pmd_pgprot(pmd);
553 phys_addr_t pte_phys;
554 pte_t *ptep;
555 int i;
556
557 pte_phys = try_pgd_pgtable_alloc_init_mm(TABLE_PTE, gfp);
558 if (pte_phys == INVALID_PHYS_ADDR)
559 return -ENOMEM;
560 ptep = (pte_t *)phys_to_virt(pte_phys);
561
562 if (pgprot_val(prot) & PMD_SECT_PXN)
563 tableprot |= PMD_TABLE_PXN;
564
565 prot = __pgprot((pgprot_val(prot) & ~PTE_TYPE_MASK) | PTE_TYPE_PAGE);
566 prot = __pgprot(pgprot_val(prot) & ~PTE_CONT);
567 if (to_cont)
568 prot = __pgprot(pgprot_val(prot) | PTE_CONT);
569
570 for (i = 0; i < PTRS_PER_PTE; i++, ptep++, pfn++)
571 __set_pte(ptep, pfn_pte(pfn, prot));
572
573 /*
574 * Ensure the pte entries are visible to the table walker by the time
575 * the pmd entry that points to the ptes is visible.
576 */
577 dsb(ishst);
578 __pmd_populate(pmdp, pte_phys, tableprot);
579
580 return 0;
581 }
582
split_contpmd(pmd_t * pmdp)583 static void split_contpmd(pmd_t *pmdp)
584 {
585 int i;
586
587 pmdp = PTR_ALIGN_DOWN(pmdp, sizeof(*pmdp) * CONT_PMDS);
588 for (i = 0; i < CONT_PMDS; i++, pmdp++)
589 set_pmd(pmdp, pmd_mknoncont(pmdp_get(pmdp)));
590 }
591
split_pud(pud_t * pudp,pud_t pud,gfp_t gfp,bool to_cont)592 static int split_pud(pud_t *pudp, pud_t pud, gfp_t gfp, bool to_cont)
593 {
594 pudval_t tableprot = PUD_TYPE_TABLE | PUD_TABLE_UXN | PUD_TABLE_AF;
595 unsigned int step = PMD_SIZE >> PAGE_SHIFT;
596 unsigned long pfn = pud_pfn(pud);
597 pgprot_t prot = pud_pgprot(pud);
598 phys_addr_t pmd_phys;
599 pmd_t *pmdp;
600 int i;
601
602 pmd_phys = try_pgd_pgtable_alloc_init_mm(TABLE_PMD, gfp);
603 if (pmd_phys == INVALID_PHYS_ADDR)
604 return -ENOMEM;
605 pmdp = (pmd_t *)phys_to_virt(pmd_phys);
606
607 if (pgprot_val(prot) & PMD_SECT_PXN)
608 tableprot |= PUD_TABLE_PXN;
609
610 prot = __pgprot((pgprot_val(prot) & ~PMD_TYPE_MASK) | PMD_TYPE_SECT);
611 prot = __pgprot(pgprot_val(prot) & ~PTE_CONT);
612 if (to_cont)
613 prot = __pgprot(pgprot_val(prot) | PTE_CONT);
614
615 for (i = 0; i < PTRS_PER_PMD; i++, pmdp++, pfn += step)
616 set_pmd(pmdp, pfn_pmd(pfn, prot));
617
618 /*
619 * Ensure the pmd entries are visible to the table walker by the time
620 * the pud entry that points to the pmds is visible.
621 */
622 dsb(ishst);
623 __pud_populate(pudp, pmd_phys, tableprot);
624
625 return 0;
626 }
627
split_kernel_leaf_mapping_locked(unsigned long addr)628 static int split_kernel_leaf_mapping_locked(unsigned long addr)
629 {
630 pgd_t *pgdp, pgd;
631 p4d_t *p4dp, p4d;
632 pud_t *pudp, pud;
633 pmd_t *pmdp, pmd;
634 pte_t *ptep, pte;
635 int ret = 0;
636
637 /*
638 * PGD: If addr is PGD aligned then addr already describes a leaf
639 * boundary. If not present then there is nothing to split.
640 */
641 if (ALIGN_DOWN(addr, PGDIR_SIZE) == addr)
642 goto out;
643 pgdp = pgd_offset_k(addr);
644 pgd = pgdp_get(pgdp);
645 if (!pgd_present(pgd))
646 goto out;
647
648 /*
649 * P4D: If addr is P4D aligned then addr already describes a leaf
650 * boundary. If not present then there is nothing to split.
651 */
652 if (ALIGN_DOWN(addr, P4D_SIZE) == addr)
653 goto out;
654 p4dp = p4d_offset(pgdp, addr);
655 p4d = p4dp_get(p4dp);
656 if (!p4d_present(p4d))
657 goto out;
658
659 /*
660 * PUD: If addr is PUD aligned then addr already describes a leaf
661 * boundary. If not present then there is nothing to split. Otherwise,
662 * if we have a pud leaf, split to contpmd.
663 */
664 if (ALIGN_DOWN(addr, PUD_SIZE) == addr)
665 goto out;
666 pudp = pud_offset(p4dp, addr);
667 pud = pudp_get(pudp);
668 if (!pud_present(pud))
669 goto out;
670 if (pud_leaf(pud)) {
671 ret = split_pud(pudp, pud, GFP_PGTABLE_KERNEL, true);
672 if (ret)
673 goto out;
674 }
675
676 /*
677 * CONTPMD: If addr is CONTPMD aligned then addr already describes a
678 * leaf boundary. If not present then there is nothing to split.
679 * Otherwise, if we have a contpmd leaf, split to pmd.
680 */
681 if (ALIGN_DOWN(addr, CONT_PMD_SIZE) == addr)
682 goto out;
683 pmdp = pmd_offset(pudp, addr);
684 pmd = pmdp_get(pmdp);
685 if (!pmd_present(pmd))
686 goto out;
687 if (pmd_leaf(pmd)) {
688 if (pmd_cont(pmd))
689 split_contpmd(pmdp);
690 /*
691 * PMD: If addr is PMD aligned then addr already describes a
692 * leaf boundary. Otherwise, split to contpte.
693 */
694 if (ALIGN_DOWN(addr, PMD_SIZE) == addr)
695 goto out;
696 ret = split_pmd(pmdp, pmd, GFP_PGTABLE_KERNEL, true);
697 if (ret)
698 goto out;
699 }
700
701 /*
702 * CONTPTE: If addr is CONTPTE aligned then addr already describes a
703 * leaf boundary. If not present then there is nothing to split.
704 * Otherwise, if we have a contpte leaf, split to pte.
705 */
706 if (ALIGN_DOWN(addr, CONT_PTE_SIZE) == addr)
707 goto out;
708 ptep = pte_offset_kernel(pmdp, addr);
709 pte = __ptep_get(ptep);
710 if (!pte_present(pte))
711 goto out;
712 if (pte_cont(pte))
713 split_contpte(ptep);
714
715 out:
716 return ret;
717 }
718
719 static DEFINE_MUTEX(pgtable_split_lock);
720
split_kernel_leaf_mapping(unsigned long start,unsigned long end)721 int split_kernel_leaf_mapping(unsigned long start, unsigned long end)
722 {
723 int ret;
724
725 /*
726 * !BBML2_NOABORT systems should not be trying to change permissions on
727 * anything that is not pte-mapped in the first place. Just return early
728 * and let the permission change code raise a warning if not already
729 * pte-mapped.
730 */
731 if (!system_supports_bbml2_noabort())
732 return 0;
733
734 /*
735 * Ensure start and end are at least page-aligned since this is the
736 * finest granularity we can split to.
737 */
738 if (start != PAGE_ALIGN(start) || end != PAGE_ALIGN(end))
739 return -EINVAL;
740
741 mutex_lock(&pgtable_split_lock);
742 arch_enter_lazy_mmu_mode();
743
744 /*
745 * The split_kernel_leaf_mapping_locked() may sleep, it is not a
746 * problem for ARM64 since ARM64's lazy MMU implementation allows
747 * sleeping.
748 *
749 * Optimize for the common case of splitting out a single page from a
750 * larger mapping. Here we can just split on the "least aligned" of
751 * start and end and this will guarantee that there must also be a split
752 * on the more aligned address since the both addresses must be in the
753 * same contpte block and it must have been split to ptes.
754 */
755 if (end - start == PAGE_SIZE) {
756 start = __ffs(start) < __ffs(end) ? start : end;
757 ret = split_kernel_leaf_mapping_locked(start);
758 } else {
759 ret = split_kernel_leaf_mapping_locked(start);
760 if (!ret)
761 ret = split_kernel_leaf_mapping_locked(end);
762 }
763
764 arch_leave_lazy_mmu_mode();
765 mutex_unlock(&pgtable_split_lock);
766 return ret;
767 }
768
split_to_ptes_pud_entry(pud_t * pudp,unsigned long addr,unsigned long next,struct mm_walk * walk)769 static int __init split_to_ptes_pud_entry(pud_t *pudp, unsigned long addr,
770 unsigned long next,
771 struct mm_walk *walk)
772 {
773 pud_t pud = pudp_get(pudp);
774 int ret = 0;
775
776 if (pud_leaf(pud))
777 ret = split_pud(pudp, pud, GFP_ATOMIC, false);
778
779 return ret;
780 }
781
split_to_ptes_pmd_entry(pmd_t * pmdp,unsigned long addr,unsigned long next,struct mm_walk * walk)782 static int __init split_to_ptes_pmd_entry(pmd_t *pmdp, unsigned long addr,
783 unsigned long next,
784 struct mm_walk *walk)
785 {
786 pmd_t pmd = pmdp_get(pmdp);
787 int ret = 0;
788
789 if (pmd_leaf(pmd)) {
790 if (pmd_cont(pmd))
791 split_contpmd(pmdp);
792 ret = split_pmd(pmdp, pmd, GFP_ATOMIC, false);
793
794 /*
795 * We have split the pmd directly to ptes so there is no need to
796 * visit each pte to check if they are contpte.
797 */
798 walk->action = ACTION_CONTINUE;
799 }
800
801 return ret;
802 }
803
split_to_ptes_pte_entry(pte_t * ptep,unsigned long addr,unsigned long next,struct mm_walk * walk)804 static int __init split_to_ptes_pte_entry(pte_t *ptep, unsigned long addr,
805 unsigned long next,
806 struct mm_walk *walk)
807 {
808 pte_t pte = __ptep_get(ptep);
809
810 if (pte_cont(pte))
811 split_contpte(ptep);
812
813 return 0;
814 }
815
816 static const struct mm_walk_ops split_to_ptes_ops __initconst = {
817 .pud_entry = split_to_ptes_pud_entry,
818 .pmd_entry = split_to_ptes_pmd_entry,
819 .pte_entry = split_to_ptes_pte_entry,
820 };
821
822 static bool linear_map_requires_bbml2 __initdata;
823
824 u32 idmap_kpti_bbml2_flag;
825
init_idmap_kpti_bbml2_flag(void)826 void __init init_idmap_kpti_bbml2_flag(void)
827 {
828 WRITE_ONCE(idmap_kpti_bbml2_flag, 1);
829 /* Must be visible to other CPUs before stop_machine() is called. */
830 smp_mb();
831 }
832
linear_map_split_to_ptes(void * __unused)833 static int __init linear_map_split_to_ptes(void *__unused)
834 {
835 /*
836 * Repainting the linear map must be done by CPU0 (the boot CPU) because
837 * that's the only CPU that we know supports BBML2. The other CPUs will
838 * be held in a waiting area with the idmap active.
839 */
840 if (!smp_processor_id()) {
841 unsigned long lstart = _PAGE_OFFSET(vabits_actual);
842 unsigned long lend = PAGE_END;
843 unsigned long kstart = (unsigned long)lm_alias(_stext);
844 unsigned long kend = (unsigned long)lm_alias(__init_begin);
845 int ret;
846
847 /*
848 * Wait for all secondary CPUs to be put into the waiting area.
849 */
850 smp_cond_load_acquire(&idmap_kpti_bbml2_flag, VAL == num_online_cpus());
851
852 /*
853 * Walk all of the linear map [lstart, lend), except the kernel
854 * linear map alias [kstart, kend), and split all mappings to
855 * PTE. The kernel alias remains static throughout runtime so
856 * can continue to be safely mapped with large mappings.
857 */
858 ret = walk_kernel_page_table_range_lockless(lstart, kstart,
859 &split_to_ptes_ops, NULL, NULL);
860 if (!ret)
861 ret = walk_kernel_page_table_range_lockless(kend, lend,
862 &split_to_ptes_ops, NULL, NULL);
863 if (ret)
864 panic("Failed to split linear map\n");
865 flush_tlb_kernel_range(lstart, lend);
866
867 /*
868 * Relies on dsb in flush_tlb_kernel_range() to avoid reordering
869 * before any page table split operations.
870 */
871 WRITE_ONCE(idmap_kpti_bbml2_flag, 0);
872 } else {
873 typedef void (wait_split_fn)(void);
874 extern wait_split_fn wait_linear_map_split_to_ptes;
875 wait_split_fn *wait_fn;
876
877 wait_fn = (void *)__pa_symbol(wait_linear_map_split_to_ptes);
878
879 /*
880 * At least one secondary CPU doesn't support BBML2 so cannot
881 * tolerate the size of the live mappings changing. So have the
882 * secondary CPUs wait for the boot CPU to make the changes
883 * with the idmap active and init_mm inactive.
884 */
885 cpu_install_idmap();
886 wait_fn();
887 cpu_uninstall_idmap();
888 }
889
890 return 0;
891 }
892
linear_map_maybe_split_to_ptes(void)893 void __init linear_map_maybe_split_to_ptes(void)
894 {
895 if (linear_map_requires_bbml2 && !system_supports_bbml2_noabort()) {
896 init_idmap_kpti_bbml2_flag();
897 stop_machine(linear_map_split_to_ptes, NULL, cpu_online_mask);
898 }
899 }
900
901 /*
902 * This function can only be used to modify existing table entries,
903 * without allocating new levels of table. Note that this permits the
904 * creation of new section or page entries.
905 */
create_mapping_noalloc(phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)906 void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
907 phys_addr_t size, pgprot_t prot)
908 {
909 if (virt < PAGE_OFFSET) {
910 pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
911 &phys, virt);
912 return;
913 }
914 __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
915 NO_CONT_MAPPINGS);
916 }
917
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)918 void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
919 unsigned long virt, phys_addr_t size,
920 pgprot_t prot, bool page_mappings_only)
921 {
922 int flags = 0;
923
924 BUG_ON(mm == &init_mm);
925
926 if (page_mappings_only)
927 flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
928
929 __create_pgd_mapping(mm->pgd, phys, virt, size, prot,
930 pgd_pgtable_alloc_special_mm, flags);
931 }
932
update_mapping_prot(phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)933 static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
934 phys_addr_t size, pgprot_t prot)
935 {
936 if (virt < PAGE_OFFSET) {
937 pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
938 &phys, virt);
939 return;
940 }
941
942 __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
943 NO_CONT_MAPPINGS);
944
945 /* flush the TLBs after updating live kernel mappings */
946 flush_tlb_kernel_range(virt, virt + size);
947 }
948
__map_memblock(pgd_t * pgdp,phys_addr_t start,phys_addr_t end,pgprot_t prot,int flags)949 static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
950 phys_addr_t end, pgprot_t prot, int flags)
951 {
952 __create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
953 prot, early_pgtable_alloc, flags);
954 }
955
mark_linear_text_alias_ro(void)956 void __init mark_linear_text_alias_ro(void)
957 {
958 /*
959 * Remove the write permissions from the linear alias of .text/.rodata
960 */
961 update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
962 (unsigned long)__init_begin - (unsigned long)_text,
963 PAGE_KERNEL_RO);
964 }
965
966 #ifdef CONFIG_KFENCE
967
968 bool __ro_after_init kfence_early_init = !!CONFIG_KFENCE_SAMPLE_INTERVAL;
969
970 /* early_param() will be parsed before map_mem() below. */
parse_kfence_early_init(char * arg)971 static int __init parse_kfence_early_init(char *arg)
972 {
973 int val;
974
975 if (get_option(&arg, &val))
976 kfence_early_init = !!val;
977 return 0;
978 }
979 early_param("kfence.sample_interval", parse_kfence_early_init);
980
arm64_kfence_alloc_pool(void)981 static phys_addr_t __init arm64_kfence_alloc_pool(void)
982 {
983 phys_addr_t kfence_pool;
984
985 if (!kfence_early_init)
986 return 0;
987
988 kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
989 if (!kfence_pool) {
990 pr_err("failed to allocate kfence pool\n");
991 kfence_early_init = false;
992 return 0;
993 }
994
995 /* Temporarily mark as NOMAP. */
996 memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE);
997
998 return kfence_pool;
999 }
1000
arm64_kfence_map_pool(phys_addr_t kfence_pool,pgd_t * pgdp)1001 static void __init arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp)
1002 {
1003 if (!kfence_pool)
1004 return;
1005
1006 /* KFENCE pool needs page-level mapping. */
1007 __map_memblock(pgdp, kfence_pool, kfence_pool + KFENCE_POOL_SIZE,
1008 pgprot_tagged(PAGE_KERNEL),
1009 NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
1010 memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE);
1011 __kfence_pool = phys_to_virt(kfence_pool);
1012 }
1013 #else /* CONFIG_KFENCE */
1014
arm64_kfence_alloc_pool(void)1015 static inline phys_addr_t arm64_kfence_alloc_pool(void) { return 0; }
arm64_kfence_map_pool(phys_addr_t kfence_pool,pgd_t * pgdp)1016 static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) { }
1017
1018 #endif /* CONFIG_KFENCE */
1019
force_pte_mapping(void)1020 static inline bool force_pte_mapping(void)
1021 {
1022 bool bbml2 = system_capabilities_finalized() ?
1023 system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
1024
1025 return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
1026 is_realm_world())) ||
1027 debug_pagealloc_enabled();
1028 }
1029
map_mem(pgd_t * pgdp)1030 static void __init map_mem(pgd_t *pgdp)
1031 {
1032 static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
1033 phys_addr_t kernel_start = __pa_symbol(_text);
1034 phys_addr_t kernel_end = __pa_symbol(__init_begin);
1035 phys_addr_t start, end;
1036 phys_addr_t early_kfence_pool;
1037 int flags = NO_EXEC_MAPPINGS;
1038 u64 i;
1039
1040 /*
1041 * Setting hierarchical PXNTable attributes on table entries covering
1042 * the linear region is only possible if it is guaranteed that no table
1043 * entries at any level are being shared between the linear region and
1044 * the vmalloc region. Check whether this is true for the PGD level, in
1045 * which case it is guaranteed to be true for all other levels as well.
1046 * (Unless we are running with support for LPA2, in which case the
1047 * entire reduced VA space is covered by a single pgd_t which will have
1048 * been populated without the PXNTable attribute by the time we get here.)
1049 */
1050 BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end) &&
1051 pgd_index(_PAGE_OFFSET(VA_BITS_MIN)) != PTRS_PER_PGD - 1);
1052
1053 early_kfence_pool = arm64_kfence_alloc_pool();
1054
1055 linear_map_requires_bbml2 = !force_pte_mapping() && can_set_direct_map();
1056
1057 if (force_pte_mapping())
1058 flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
1059
1060 /*
1061 * Take care not to create a writable alias for the
1062 * read-only text and rodata sections of the kernel image.
1063 * So temporarily mark them as NOMAP to skip mappings in
1064 * the following for-loop
1065 */
1066 memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
1067
1068 /* map all the memory banks */
1069 for_each_mem_range(i, &start, &end) {
1070 if (start >= end)
1071 break;
1072 /*
1073 * The linear map must allow allocation tags reading/writing
1074 * if MTE is present. Otherwise, it has the same attributes as
1075 * PAGE_KERNEL.
1076 */
1077 __map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL),
1078 flags);
1079 }
1080
1081 /*
1082 * Map the linear alias of the [_text, __init_begin) interval
1083 * as non-executable now, and remove the write permission in
1084 * mark_linear_text_alias_ro() below (which will be called after
1085 * alternative patching has completed). This makes the contents
1086 * of the region accessible to subsystems such as hibernate,
1087 * but protects it from inadvertent modification or execution.
1088 * Note that contiguous mappings cannot be remapped in this way,
1089 * so we should avoid them here.
1090 */
1091 __map_memblock(pgdp, kernel_start, kernel_end,
1092 PAGE_KERNEL, NO_CONT_MAPPINGS);
1093 memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
1094 arm64_kfence_map_pool(early_kfence_pool, pgdp);
1095 }
1096
mark_rodata_ro(void)1097 void mark_rodata_ro(void)
1098 {
1099 unsigned long section_size;
1100
1101 /*
1102 * mark .rodata as read only. Use __init_begin rather than __end_rodata
1103 * to cover NOTES and EXCEPTION_TABLE.
1104 */
1105 section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
1106 WRITE_ONCE(rodata_is_rw, false);
1107 update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
1108 section_size, PAGE_KERNEL_RO);
1109 /* mark the range between _text and _stext as read only. */
1110 update_mapping_prot(__pa_symbol(_text), (unsigned long)_text,
1111 (unsigned long)_stext - (unsigned long)_text,
1112 PAGE_KERNEL_RO);
1113 }
1114
declare_vma(struct vm_struct * vma,void * va_start,void * va_end,unsigned long vm_flags)1115 static void __init declare_vma(struct vm_struct *vma,
1116 void *va_start, void *va_end,
1117 unsigned long vm_flags)
1118 {
1119 phys_addr_t pa_start = __pa_symbol(va_start);
1120 unsigned long size = va_end - va_start;
1121
1122 BUG_ON(!PAGE_ALIGNED(pa_start));
1123 BUG_ON(!PAGE_ALIGNED(size));
1124
1125 if (!(vm_flags & VM_NO_GUARD))
1126 size += PAGE_SIZE;
1127
1128 vma->addr = va_start;
1129 vma->phys_addr = pa_start;
1130 vma->size = size;
1131 vma->flags = VM_MAP | vm_flags;
1132 vma->caller = __builtin_return_address(0);
1133
1134 vm_area_add_early(vma);
1135 }
1136
1137 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
kernel_exec_prot(void)1138 static pgprot_t kernel_exec_prot(void)
1139 {
1140 return rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
1141 }
1142
map_entry_trampoline(void)1143 static int __init map_entry_trampoline(void)
1144 {
1145 int i;
1146
1147 if (!arm64_kernel_unmapped_at_el0())
1148 return 0;
1149
1150 pgprot_t prot = kernel_exec_prot();
1151 phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
1152
1153 /* The trampoline is always mapped and can therefore be global */
1154 pgprot_val(prot) &= ~PTE_NG;
1155
1156 /* Map only the text into the trampoline page table */
1157 memset(tramp_pg_dir, 0, PGD_SIZE);
1158 __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS,
1159 entry_tramp_text_size(), prot,
1160 pgd_pgtable_alloc_init_mm, NO_BLOCK_MAPPINGS);
1161
1162 /* Map both the text and data into the kernel page table */
1163 for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++)
1164 __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
1165 pa_start + i * PAGE_SIZE, prot);
1166
1167 if (IS_ENABLED(CONFIG_RELOCATABLE))
1168 __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
1169 pa_start + i * PAGE_SIZE, PAGE_KERNEL_RO);
1170
1171 return 0;
1172 }
1173 core_initcall(map_entry_trampoline);
1174 #endif
1175
1176 /*
1177 * Declare the VMA areas for the kernel
1178 */
declare_kernel_vmas(void)1179 static void __init declare_kernel_vmas(void)
1180 {
1181 static struct vm_struct vmlinux_seg[KERNEL_SEGMENT_COUNT];
1182
1183 declare_vma(&vmlinux_seg[0], _text, _etext, VM_NO_GUARD);
1184 declare_vma(&vmlinux_seg[1], __start_rodata, __inittext_begin, VM_NO_GUARD);
1185 declare_vma(&vmlinux_seg[2], __inittext_begin, __inittext_end, VM_NO_GUARD);
1186 declare_vma(&vmlinux_seg[3], __initdata_begin, __initdata_end, VM_NO_GUARD);
1187 declare_vma(&vmlinux_seg[4], _data, _end, 0);
1188 }
1189
1190 void __pi_map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa,
1191 pgprot_t prot, int level, pte_t *tbl, bool may_use_cont,
1192 u64 va_offset);
1193
1194 static u8 idmap_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init,
1195 kpti_bbml2_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init;
1196
create_idmap(void)1197 static void __init create_idmap(void)
1198 {
1199 phys_addr_t start = __pa_symbol(__idmap_text_start);
1200 phys_addr_t end = __pa_symbol(__idmap_text_end);
1201 phys_addr_t ptep = __pa_symbol(idmap_ptes);
1202
1203 __pi_map_range(&ptep, start, end, start, PAGE_KERNEL_ROX,
1204 IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false,
1205 __phys_to_virt(ptep) - ptep);
1206
1207 if (linear_map_requires_bbml2 ||
1208 (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0) && !arm64_use_ng_mappings)) {
1209 phys_addr_t pa = __pa_symbol(&idmap_kpti_bbml2_flag);
1210
1211 /*
1212 * The KPTI G-to-nG conversion code needs a read-write mapping
1213 * of its synchronization flag in the ID map. This is also used
1214 * when splitting the linear map to ptes if a secondary CPU
1215 * doesn't support bbml2.
1216 */
1217 ptep = __pa_symbol(kpti_bbml2_ptes);
1218 __pi_map_range(&ptep, pa, pa + sizeof(u32), pa, PAGE_KERNEL,
1219 IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false,
1220 __phys_to_virt(ptep) - ptep);
1221 }
1222 }
1223
paging_init(void)1224 void __init paging_init(void)
1225 {
1226 map_mem(swapper_pg_dir);
1227
1228 memblock_allow_resize();
1229
1230 create_idmap();
1231 declare_kernel_vmas();
1232 }
1233
1234 #ifdef CONFIG_MEMORY_HOTPLUG
free_hotplug_page_range(struct page * page,size_t size,struct vmem_altmap * altmap)1235 static void free_hotplug_page_range(struct page *page, size_t size,
1236 struct vmem_altmap *altmap)
1237 {
1238 if (altmap) {
1239 vmem_altmap_free(altmap, size >> PAGE_SHIFT);
1240 } else {
1241 WARN_ON(PageReserved(page));
1242 __free_pages(page, get_order(size));
1243 }
1244 }
1245
free_hotplug_pgtable_page(struct page * page)1246 static void free_hotplug_pgtable_page(struct page *page)
1247 {
1248 free_hotplug_page_range(page, PAGE_SIZE, NULL);
1249 }
1250
pgtable_range_aligned(unsigned long start,unsigned long end,unsigned long floor,unsigned long ceiling,unsigned long mask)1251 static bool pgtable_range_aligned(unsigned long start, unsigned long end,
1252 unsigned long floor, unsigned long ceiling,
1253 unsigned long mask)
1254 {
1255 start &= mask;
1256 if (start < floor)
1257 return false;
1258
1259 if (ceiling) {
1260 ceiling &= mask;
1261 if (!ceiling)
1262 return false;
1263 }
1264
1265 if (end - 1 > ceiling - 1)
1266 return false;
1267 return true;
1268 }
1269
unmap_hotplug_pte_range(pmd_t * pmdp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1270 static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
1271 unsigned long end, bool free_mapped,
1272 struct vmem_altmap *altmap)
1273 {
1274 pte_t *ptep, pte;
1275
1276 do {
1277 ptep = pte_offset_kernel(pmdp, addr);
1278 pte = __ptep_get(ptep);
1279 if (pte_none(pte))
1280 continue;
1281
1282 WARN_ON(!pte_present(pte));
1283 __pte_clear(&init_mm, addr, ptep);
1284 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
1285 if (free_mapped)
1286 free_hotplug_page_range(pte_page(pte),
1287 PAGE_SIZE, altmap);
1288 } while (addr += PAGE_SIZE, addr < end);
1289 }
1290
unmap_hotplug_pmd_range(pud_t * pudp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1291 static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
1292 unsigned long end, bool free_mapped,
1293 struct vmem_altmap *altmap)
1294 {
1295 unsigned long next;
1296 pmd_t *pmdp, pmd;
1297
1298 do {
1299 next = pmd_addr_end(addr, end);
1300 pmdp = pmd_offset(pudp, addr);
1301 pmd = READ_ONCE(*pmdp);
1302 if (pmd_none(pmd))
1303 continue;
1304
1305 WARN_ON(!pmd_present(pmd));
1306 if (pmd_sect(pmd)) {
1307 pmd_clear(pmdp);
1308
1309 /*
1310 * One TLBI should be sufficient here as the PMD_SIZE
1311 * range is mapped with a single block entry.
1312 */
1313 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
1314 if (free_mapped)
1315 free_hotplug_page_range(pmd_page(pmd),
1316 PMD_SIZE, altmap);
1317 continue;
1318 }
1319 WARN_ON(!pmd_table(pmd));
1320 unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap);
1321 } while (addr = next, addr < end);
1322 }
1323
unmap_hotplug_pud_range(p4d_t * p4dp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1324 static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
1325 unsigned long end, bool free_mapped,
1326 struct vmem_altmap *altmap)
1327 {
1328 unsigned long next;
1329 pud_t *pudp, pud;
1330
1331 do {
1332 next = pud_addr_end(addr, end);
1333 pudp = pud_offset(p4dp, addr);
1334 pud = READ_ONCE(*pudp);
1335 if (pud_none(pud))
1336 continue;
1337
1338 WARN_ON(!pud_present(pud));
1339 if (pud_sect(pud)) {
1340 pud_clear(pudp);
1341
1342 /*
1343 * One TLBI should be sufficient here as the PUD_SIZE
1344 * range is mapped with a single block entry.
1345 */
1346 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
1347 if (free_mapped)
1348 free_hotplug_page_range(pud_page(pud),
1349 PUD_SIZE, altmap);
1350 continue;
1351 }
1352 WARN_ON(!pud_table(pud));
1353 unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap);
1354 } while (addr = next, addr < end);
1355 }
1356
unmap_hotplug_p4d_range(pgd_t * pgdp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1357 static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
1358 unsigned long end, bool free_mapped,
1359 struct vmem_altmap *altmap)
1360 {
1361 unsigned long next;
1362 p4d_t *p4dp, p4d;
1363
1364 do {
1365 next = p4d_addr_end(addr, end);
1366 p4dp = p4d_offset(pgdp, addr);
1367 p4d = READ_ONCE(*p4dp);
1368 if (p4d_none(p4d))
1369 continue;
1370
1371 WARN_ON(!p4d_present(p4d));
1372 unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap);
1373 } while (addr = next, addr < end);
1374 }
1375
unmap_hotplug_range(unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1376 static void unmap_hotplug_range(unsigned long addr, unsigned long end,
1377 bool free_mapped, struct vmem_altmap *altmap)
1378 {
1379 unsigned long next;
1380 pgd_t *pgdp, pgd;
1381
1382 /*
1383 * altmap can only be used as vmemmap mapping backing memory.
1384 * In case the backing memory itself is not being freed, then
1385 * altmap is irrelevant. Warn about this inconsistency when
1386 * encountered.
1387 */
1388 WARN_ON(!free_mapped && altmap);
1389
1390 do {
1391 next = pgd_addr_end(addr, end);
1392 pgdp = pgd_offset_k(addr);
1393 pgd = READ_ONCE(*pgdp);
1394 if (pgd_none(pgd))
1395 continue;
1396
1397 WARN_ON(!pgd_present(pgd));
1398 unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
1399 } while (addr = next, addr < end);
1400 }
1401
free_empty_pte_table(pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1402 static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr,
1403 unsigned long end, unsigned long floor,
1404 unsigned long ceiling)
1405 {
1406 pte_t *ptep, pte;
1407 unsigned long i, start = addr;
1408
1409 do {
1410 ptep = pte_offset_kernel(pmdp, addr);
1411 pte = __ptep_get(ptep);
1412
1413 /*
1414 * This is just a sanity check here which verifies that
1415 * pte clearing has been done by earlier unmap loops.
1416 */
1417 WARN_ON(!pte_none(pte));
1418 } while (addr += PAGE_SIZE, addr < end);
1419
1420 if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK))
1421 return;
1422
1423 /*
1424 * Check whether we can free the pte page if the rest of the
1425 * entries are empty. Overlap with other regions have been
1426 * handled by the floor/ceiling check.
1427 */
1428 ptep = pte_offset_kernel(pmdp, 0UL);
1429 for (i = 0; i < PTRS_PER_PTE; i++) {
1430 if (!pte_none(__ptep_get(&ptep[i])))
1431 return;
1432 }
1433
1434 pmd_clear(pmdp);
1435 __flush_tlb_kernel_pgtable(start);
1436 free_hotplug_pgtable_page(virt_to_page(ptep));
1437 }
1438
free_empty_pmd_table(pud_t * pudp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1439 static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
1440 unsigned long end, unsigned long floor,
1441 unsigned long ceiling)
1442 {
1443 pmd_t *pmdp, pmd;
1444 unsigned long i, next, start = addr;
1445
1446 do {
1447 next = pmd_addr_end(addr, end);
1448 pmdp = pmd_offset(pudp, addr);
1449 pmd = READ_ONCE(*pmdp);
1450 if (pmd_none(pmd))
1451 continue;
1452
1453 WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd));
1454 free_empty_pte_table(pmdp, addr, next, floor, ceiling);
1455 } while (addr = next, addr < end);
1456
1457 if (CONFIG_PGTABLE_LEVELS <= 2)
1458 return;
1459
1460 if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
1461 return;
1462
1463 /*
1464 * Check whether we can free the pmd page if the rest of the
1465 * entries are empty. Overlap with other regions have been
1466 * handled by the floor/ceiling check.
1467 */
1468 pmdp = pmd_offset(pudp, 0UL);
1469 for (i = 0; i < PTRS_PER_PMD; i++) {
1470 if (!pmd_none(READ_ONCE(pmdp[i])))
1471 return;
1472 }
1473
1474 pud_clear(pudp);
1475 __flush_tlb_kernel_pgtable(start);
1476 free_hotplug_pgtable_page(virt_to_page(pmdp));
1477 }
1478
free_empty_pud_table(p4d_t * p4dp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1479 static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,
1480 unsigned long end, unsigned long floor,
1481 unsigned long ceiling)
1482 {
1483 pud_t *pudp, pud;
1484 unsigned long i, next, start = addr;
1485
1486 do {
1487 next = pud_addr_end(addr, end);
1488 pudp = pud_offset(p4dp, addr);
1489 pud = READ_ONCE(*pudp);
1490 if (pud_none(pud))
1491 continue;
1492
1493 WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud));
1494 free_empty_pmd_table(pudp, addr, next, floor, ceiling);
1495 } while (addr = next, addr < end);
1496
1497 if (!pgtable_l4_enabled())
1498 return;
1499
1500 if (!pgtable_range_aligned(start, end, floor, ceiling, P4D_MASK))
1501 return;
1502
1503 /*
1504 * Check whether we can free the pud page if the rest of the
1505 * entries are empty. Overlap with other regions have been
1506 * handled by the floor/ceiling check.
1507 */
1508 pudp = pud_offset(p4dp, 0UL);
1509 for (i = 0; i < PTRS_PER_PUD; i++) {
1510 if (!pud_none(READ_ONCE(pudp[i])))
1511 return;
1512 }
1513
1514 p4d_clear(p4dp);
1515 __flush_tlb_kernel_pgtable(start);
1516 free_hotplug_pgtable_page(virt_to_page(pudp));
1517 }
1518
free_empty_p4d_table(pgd_t * pgdp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1519 static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
1520 unsigned long end, unsigned long floor,
1521 unsigned long ceiling)
1522 {
1523 p4d_t *p4dp, p4d;
1524 unsigned long i, next, start = addr;
1525
1526 do {
1527 next = p4d_addr_end(addr, end);
1528 p4dp = p4d_offset(pgdp, addr);
1529 p4d = READ_ONCE(*p4dp);
1530 if (p4d_none(p4d))
1531 continue;
1532
1533 WARN_ON(!p4d_present(p4d));
1534 free_empty_pud_table(p4dp, addr, next, floor, ceiling);
1535 } while (addr = next, addr < end);
1536
1537 if (!pgtable_l5_enabled())
1538 return;
1539
1540 if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
1541 return;
1542
1543 /*
1544 * Check whether we can free the p4d page if the rest of the
1545 * entries are empty. Overlap with other regions have been
1546 * handled by the floor/ceiling check.
1547 */
1548 p4dp = p4d_offset(pgdp, 0UL);
1549 for (i = 0; i < PTRS_PER_P4D; i++) {
1550 if (!p4d_none(READ_ONCE(p4dp[i])))
1551 return;
1552 }
1553
1554 pgd_clear(pgdp);
1555 __flush_tlb_kernel_pgtable(start);
1556 free_hotplug_pgtable_page(virt_to_page(p4dp));
1557 }
1558
free_empty_tables(unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1559 static void free_empty_tables(unsigned long addr, unsigned long end,
1560 unsigned long floor, unsigned long ceiling)
1561 {
1562 unsigned long next;
1563 pgd_t *pgdp, pgd;
1564
1565 do {
1566 next = pgd_addr_end(addr, end);
1567 pgdp = pgd_offset_k(addr);
1568 pgd = READ_ONCE(*pgdp);
1569 if (pgd_none(pgd))
1570 continue;
1571
1572 WARN_ON(!pgd_present(pgd));
1573 free_empty_p4d_table(pgdp, addr, next, floor, ceiling);
1574 } while (addr = next, addr < end);
1575 }
1576 #endif
1577
vmemmap_set_pmd(pmd_t * pmdp,void * p,int node,unsigned long addr,unsigned long next)1578 void __meminit vmemmap_set_pmd(pmd_t *pmdp, void *p, int node,
1579 unsigned long addr, unsigned long next)
1580 {
1581 pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
1582 }
1583
vmemmap_check_pmd(pmd_t * pmdp,int node,unsigned long addr,unsigned long next)1584 int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node,
1585 unsigned long addr, unsigned long next)
1586 {
1587 vmemmap_verify((pte_t *)pmdp, node, addr, next);
1588
1589 return pmd_sect(READ_ONCE(*pmdp));
1590 }
1591
vmemmap_populate(unsigned long start,unsigned long end,int node,struct vmem_altmap * altmap)1592 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1593 struct vmem_altmap *altmap)
1594 {
1595 WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1596 /* [start, end] should be within one section */
1597 WARN_ON_ONCE(end - start > PAGES_PER_SECTION * sizeof(struct page));
1598
1599 if (!IS_ENABLED(CONFIG_ARM64_4K_PAGES) ||
1600 (end - start < PAGES_PER_SECTION * sizeof(struct page)))
1601 return vmemmap_populate_basepages(start, end, node, altmap);
1602 else
1603 return vmemmap_populate_hugepages(start, end, node, altmap);
1604 }
1605
1606 #ifdef CONFIG_MEMORY_HOTPLUG
vmemmap_free(unsigned long start,unsigned long end,struct vmem_altmap * altmap)1607 void vmemmap_free(unsigned long start, unsigned long end,
1608 struct vmem_altmap *altmap)
1609 {
1610 WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1611
1612 unmap_hotplug_range(start, end, true, altmap);
1613 free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END);
1614 }
1615 #endif /* CONFIG_MEMORY_HOTPLUG */
1616
pud_set_huge(pud_t * pudp,phys_addr_t phys,pgprot_t prot)1617 int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
1618 {
1619 pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot));
1620
1621 /* Only allow permission changes for now */
1622 if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
1623 pud_val(new_pud)))
1624 return 0;
1625
1626 VM_BUG_ON(phys & ~PUD_MASK);
1627 set_pud(pudp, new_pud);
1628 return 1;
1629 }
1630
pmd_set_huge(pmd_t * pmdp,phys_addr_t phys,pgprot_t prot)1631 int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
1632 {
1633 pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot));
1634
1635 /* Only allow permission changes for now */
1636 if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
1637 pmd_val(new_pmd)))
1638 return 0;
1639
1640 VM_BUG_ON(phys & ~PMD_MASK);
1641 set_pmd(pmdp, new_pmd);
1642 return 1;
1643 }
1644
1645 #ifndef __PAGETABLE_P4D_FOLDED
p4d_clear_huge(p4d_t * p4dp)1646 void p4d_clear_huge(p4d_t *p4dp)
1647 {
1648 }
1649 #endif
1650
pud_clear_huge(pud_t * pudp)1651 int pud_clear_huge(pud_t *pudp)
1652 {
1653 if (!pud_sect(READ_ONCE(*pudp)))
1654 return 0;
1655 pud_clear(pudp);
1656 return 1;
1657 }
1658
pmd_clear_huge(pmd_t * pmdp)1659 int pmd_clear_huge(pmd_t *pmdp)
1660 {
1661 if (!pmd_sect(READ_ONCE(*pmdp)))
1662 return 0;
1663 pmd_clear(pmdp);
1664 return 1;
1665 }
1666
__pmd_free_pte_page(pmd_t * pmdp,unsigned long addr,bool acquire_mmap_lock)1667 static int __pmd_free_pte_page(pmd_t *pmdp, unsigned long addr,
1668 bool acquire_mmap_lock)
1669 {
1670 pte_t *table;
1671 pmd_t pmd;
1672
1673 pmd = READ_ONCE(*pmdp);
1674
1675 if (!pmd_table(pmd)) {
1676 VM_WARN_ON(1);
1677 return 1;
1678 }
1679
1680 /* See comment in pud_free_pmd_page for static key logic */
1681 table = pte_offset_kernel(pmdp, addr);
1682 pmd_clear(pmdp);
1683 __flush_tlb_kernel_pgtable(addr);
1684 if (static_branch_unlikely(&arm64_ptdump_lock_key) && acquire_mmap_lock) {
1685 mmap_read_lock(&init_mm);
1686 mmap_read_unlock(&init_mm);
1687 }
1688
1689 pte_free_kernel(NULL, table);
1690 return 1;
1691 }
1692
pmd_free_pte_page(pmd_t * pmdp,unsigned long addr)1693 int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
1694 {
1695 /* If ptdump is walking the pagetables, acquire init_mm.mmap_lock */
1696 return __pmd_free_pte_page(pmdp, addr, /* acquire_mmap_lock = */ true);
1697 }
1698
pud_free_pmd_page(pud_t * pudp,unsigned long addr)1699 int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
1700 {
1701 pmd_t *table;
1702 pmd_t *pmdp;
1703 pud_t pud;
1704 unsigned long next, end;
1705
1706 pud = READ_ONCE(*pudp);
1707
1708 if (!pud_table(pud)) {
1709 VM_WARN_ON(1);
1710 return 1;
1711 }
1712
1713 table = pmd_offset(pudp, addr);
1714
1715 /*
1716 * Our objective is to prevent ptdump from reading a PMD table which has
1717 * been freed. In this race, if pud_free_pmd_page observes the key on
1718 * (which got flipped by ptdump) then the mmap lock sequence here will,
1719 * as a result of the mmap write lock/unlock sequence in ptdump, give
1720 * us the correct synchronization. If not, this means that ptdump has
1721 * yet not started walking the pagetables - the sequence of barriers
1722 * issued by __flush_tlb_kernel_pgtable() guarantees that ptdump will
1723 * observe an empty PUD.
1724 */
1725 pud_clear(pudp);
1726 __flush_tlb_kernel_pgtable(addr);
1727 if (static_branch_unlikely(&arm64_ptdump_lock_key)) {
1728 mmap_read_lock(&init_mm);
1729 mmap_read_unlock(&init_mm);
1730 }
1731
1732 pmdp = table;
1733 next = addr;
1734 end = addr + PUD_SIZE;
1735 do {
1736 if (pmd_present(pmdp_get(pmdp)))
1737 /*
1738 * PMD has been isolated, so ptdump won't see it. No
1739 * need to acquire init_mm.mmap_lock.
1740 */
1741 __pmd_free_pte_page(pmdp, next, /* acquire_mmap_lock = */ false);
1742 } while (pmdp++, next += PMD_SIZE, next != end);
1743
1744 pmd_free(NULL, table);
1745 return 1;
1746 }
1747
1748 #ifdef CONFIG_MEMORY_HOTPLUG
__remove_pgd_mapping(pgd_t * pgdir,unsigned long start,u64 size)1749 static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
1750 {
1751 unsigned long end = start + size;
1752
1753 WARN_ON(pgdir != init_mm.pgd);
1754 WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END));
1755
1756 unmap_hotplug_range(start, end, false, NULL);
1757 free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
1758 }
1759
arch_get_mappable_range(void)1760 struct range arch_get_mappable_range(void)
1761 {
1762 struct range mhp_range;
1763 phys_addr_t start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual));
1764 phys_addr_t end_linear_pa = __pa(PAGE_END - 1);
1765
1766 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
1767 /*
1768 * Check for a wrap, it is possible because of randomized linear
1769 * mapping the start physical address is actually bigger than
1770 * the end physical address. In this case set start to zero
1771 * because [0, end_linear_pa] range must still be able to cover
1772 * all addressable physical addresses.
1773 */
1774 if (start_linear_pa > end_linear_pa)
1775 start_linear_pa = 0;
1776 }
1777
1778 WARN_ON(start_linear_pa > end_linear_pa);
1779
1780 /*
1781 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
1782 * accommodating both its ends but excluding PAGE_END. Max physical
1783 * range which can be mapped inside this linear mapping range, must
1784 * also be derived from its end points.
1785 */
1786 mhp_range.start = start_linear_pa;
1787 mhp_range.end = end_linear_pa;
1788
1789 return mhp_range;
1790 }
1791
arch_add_memory(int nid,u64 start,u64 size,struct mhp_params * params)1792 int arch_add_memory(int nid, u64 start, u64 size,
1793 struct mhp_params *params)
1794 {
1795 int ret, flags = NO_EXEC_MAPPINGS;
1796
1797 VM_BUG_ON(!mhp_range_allowed(start, size, true));
1798
1799 if (force_pte_mapping())
1800 flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
1801
1802 __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
1803 size, params->pgprot, pgd_pgtable_alloc_init_mm,
1804 flags);
1805
1806 memblock_clear_nomap(start, size);
1807
1808 ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
1809 params);
1810 if (ret)
1811 __remove_pgd_mapping(swapper_pg_dir,
1812 __phys_to_virt(start), size);
1813 else {
1814 /* Address of hotplugged memory can be smaller */
1815 max_pfn = max(max_pfn, PFN_UP(start + size));
1816 max_low_pfn = max_pfn;
1817 }
1818
1819 return ret;
1820 }
1821
arch_remove_memory(u64 start,u64 size,struct vmem_altmap * altmap)1822 void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
1823 {
1824 unsigned long start_pfn = start >> PAGE_SHIFT;
1825 unsigned long nr_pages = size >> PAGE_SHIFT;
1826
1827 __remove_pages(start_pfn, nr_pages, altmap);
1828 __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
1829 }
1830
1831 /*
1832 * This memory hotplug notifier helps prevent boot memory from being
1833 * inadvertently removed as it blocks pfn range offlining process in
1834 * __offline_pages(). Hence this prevents both offlining as well as
1835 * removal process for boot memory which is initially always online.
1836 * In future if and when boot memory could be removed, this notifier
1837 * should be dropped and free_hotplug_page_range() should handle any
1838 * reserved pages allocated during boot.
1839 */
prevent_bootmem_remove_notifier(struct notifier_block * nb,unsigned long action,void * data)1840 static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
1841 unsigned long action, void *data)
1842 {
1843 struct mem_section *ms;
1844 struct memory_notify *arg = data;
1845 unsigned long end_pfn = arg->start_pfn + arg->nr_pages;
1846 unsigned long pfn = arg->start_pfn;
1847
1848 if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE))
1849 return NOTIFY_OK;
1850
1851 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1852 unsigned long start = PFN_PHYS(pfn);
1853 unsigned long end = start + (1UL << PA_SECTION_SHIFT);
1854
1855 ms = __pfn_to_section(pfn);
1856 if (!early_section(ms))
1857 continue;
1858
1859 if (action == MEM_GOING_OFFLINE) {
1860 /*
1861 * Boot memory removal is not supported. Prevent
1862 * it via blocking any attempted offline request
1863 * for the boot memory and just report it.
1864 */
1865 pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end);
1866 return NOTIFY_BAD;
1867 } else if (action == MEM_OFFLINE) {
1868 /*
1869 * This should have never happened. Boot memory
1870 * offlining should have been prevented by this
1871 * very notifier. Probably some memory removal
1872 * procedure might have changed which would then
1873 * require further debug.
1874 */
1875 pr_err("Boot memory [%lx %lx] offlined\n", start, end);
1876
1877 /*
1878 * Core memory hotplug does not process a return
1879 * code from the notifier for MEM_OFFLINE events.
1880 * The error condition has been reported. Return
1881 * from here as if ignored.
1882 */
1883 return NOTIFY_DONE;
1884 }
1885 }
1886 return NOTIFY_OK;
1887 }
1888
1889 static struct notifier_block prevent_bootmem_remove_nb = {
1890 .notifier_call = prevent_bootmem_remove_notifier,
1891 };
1892
1893 /*
1894 * This ensures that boot memory sections on the platform are online
1895 * from early boot. Memory sections could not be prevented from being
1896 * offlined, unless for some reason they are not online to begin with.
1897 * This helps validate the basic assumption on which the above memory
1898 * event notifier works to prevent boot memory section offlining and
1899 * its possible removal.
1900 */
validate_bootmem_online(void)1901 static void validate_bootmem_online(void)
1902 {
1903 phys_addr_t start, end, addr;
1904 struct mem_section *ms;
1905 u64 i;
1906
1907 /*
1908 * Scanning across all memblock might be expensive
1909 * on some big memory systems. Hence enable this
1910 * validation only with DEBUG_VM.
1911 */
1912 if (!IS_ENABLED(CONFIG_DEBUG_VM))
1913 return;
1914
1915 for_each_mem_range(i, &start, &end) {
1916 for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) {
1917 ms = __pfn_to_section(PHYS_PFN(addr));
1918
1919 /*
1920 * All memory ranges in the system at this point
1921 * should have been marked as early sections.
1922 */
1923 WARN_ON(!early_section(ms));
1924
1925 /*
1926 * Memory notifier mechanism here to prevent boot
1927 * memory offlining depends on the fact that each
1928 * early section memory on the system is initially
1929 * online. Otherwise a given memory section which
1930 * is already offline will be overlooked and can
1931 * be removed completely. Call out such sections.
1932 */
1933 if (!online_section(ms))
1934 pr_err("Boot memory [%llx %llx] is offline, can be removed\n",
1935 addr, addr + (1UL << PA_SECTION_SHIFT));
1936 }
1937 }
1938 }
1939
prevent_bootmem_remove_init(void)1940 static int __init prevent_bootmem_remove_init(void)
1941 {
1942 int ret = 0;
1943
1944 if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE))
1945 return ret;
1946
1947 validate_bootmem_online();
1948 ret = register_memory_notifier(&prevent_bootmem_remove_nb);
1949 if (ret)
1950 pr_err("%s: Notifier registration failed %d\n", __func__, ret);
1951
1952 return ret;
1953 }
1954 early_initcall(prevent_bootmem_remove_init);
1955 #endif
1956
modify_prot_start_ptes(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,unsigned int nr)1957 pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,
1958 pte_t *ptep, unsigned int nr)
1959 {
1960 pte_t pte = get_and_clear_ptes(vma->vm_mm, addr, ptep, nr);
1961
1962 if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) {
1963 /*
1964 * Break-before-make (BBM) is required for all user space mappings
1965 * when the permission changes from executable to non-executable
1966 * in cases where cpu is affected with errata #2645198.
1967 */
1968 if (pte_accessible(vma->vm_mm, pte) && pte_user_exec(pte))
1969 __flush_tlb_range(vma, addr, nr * PAGE_SIZE,
1970 PAGE_SIZE, true, 3);
1971 }
1972
1973 return pte;
1974 }
1975
ptep_modify_prot_start(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)1976 pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
1977 {
1978 return modify_prot_start_ptes(vma, addr, ptep, 1);
1979 }
1980
modify_prot_commit_ptes(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t old_pte,pte_t pte,unsigned int nr)1981 void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
1982 pte_t *ptep, pte_t old_pte, pte_t pte,
1983 unsigned int nr)
1984 {
1985 set_ptes(vma->vm_mm, addr, ptep, pte, nr);
1986 }
1987
ptep_modify_prot_commit(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t old_pte,pte_t pte)1988 void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
1989 pte_t old_pte, pte_t pte)
1990 {
1991 modify_prot_commit_ptes(vma, addr, ptep, old_pte, pte, 1);
1992 }
1993
1994 /*
1995 * Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD,
1996 * avoiding the possibility of conflicting TLB entries being allocated.
1997 */
__cpu_replace_ttbr1(pgd_t * pgdp,bool cnp)1998 void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp)
1999 {
2000 typedef void (ttbr_replace_func)(phys_addr_t);
2001 extern ttbr_replace_func idmap_cpu_replace_ttbr1;
2002 ttbr_replace_func *replace_phys;
2003 unsigned long daif;
2004
2005 /* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */
2006 phys_addr_t ttbr1 = phys_to_ttbr(virt_to_phys(pgdp));
2007
2008 if (cnp)
2009 ttbr1 |= TTBR_CNP_BIT;
2010
2011 replace_phys = (void *)__pa_symbol(idmap_cpu_replace_ttbr1);
2012
2013 cpu_install_idmap();
2014
2015 /*
2016 * We really don't want to take *any* exceptions while TTBR1 is
2017 * in the process of being replaced so mask everything.
2018 */
2019 daif = local_daif_save();
2020 replace_phys(ttbr1);
2021 local_daif_restore(daif);
2022
2023 cpu_uninstall_idmap();
2024 }
2025
2026 #ifdef CONFIG_ARCH_HAS_PKEYS
arch_set_user_pkey_access(struct task_struct * tsk,int pkey,unsigned long init_val)2027 int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, unsigned long init_val)
2028 {
2029 u64 new_por;
2030 u64 old_por;
2031
2032 if (!system_supports_poe())
2033 return -ENOSPC;
2034
2035 /*
2036 * This code should only be called with valid 'pkey'
2037 * values originating from in-kernel users. Complain
2038 * if a bad value is observed.
2039 */
2040 if (WARN_ON_ONCE(pkey >= arch_max_pkey()))
2041 return -EINVAL;
2042
2043 /* Set the bits we need in POR: */
2044 new_por = POE_RWX;
2045 if (init_val & PKEY_DISABLE_WRITE)
2046 new_por &= ~POE_W;
2047 if (init_val & PKEY_DISABLE_ACCESS)
2048 new_por &= ~POE_RW;
2049 if (init_val & PKEY_DISABLE_READ)
2050 new_por &= ~POE_R;
2051 if (init_val & PKEY_DISABLE_EXECUTE)
2052 new_por &= ~POE_X;
2053
2054 /* Shift the bits in to the correct place in POR for pkey: */
2055 new_por = POR_ELx_PERM_PREP(pkey, new_por);
2056
2057 /* Get old POR and mask off any old bits in place: */
2058 old_por = read_sysreg_s(SYS_POR_EL0);
2059 old_por &= ~(POE_MASK << POR_ELx_PERM_SHIFT(pkey));
2060
2061 /* Write old part along with new part: */
2062 write_sysreg_s(old_por | new_por, SYS_POR_EL0);
2063
2064 return 0;
2065 }
2066 #endif
2067