1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on arch/arm/mm/mmu.c
5 * Copyright (C) 1995-2005 Russell King
6 * Copyright (C) 2012 ARM Ltd.
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>
25 #include <linux/vmalloc.h>
26 #include <linux/set_memory.h>
27 #include <linux/kfence.h>
28 #include <linux/pkeys.h>
30 #include <asm/barrier.h>
31 #include <asm/cputype.h>
32 #include <asm/fixmap.h>
33 #include <asm/kasan.h>
34 #include <asm/kernel-pgtable.h>
35 #include <asm/sections.h>
36 #include <asm/setup.h>
37 #include <linux/sizes.h>
39 #include <asm/mmu_context.h>
40 #include <asm/ptdump.h>
41 #include <asm/tlbflush.h>
42 #include <asm/pgalloc.h>
43 #include <asm/kfence.h>
45 #define NO_BLOCK_MAPPINGS BIT(0)
46 #define NO_CONT_MAPPINGS BIT(1)
47 #define NO_EXEC_MAPPINGS BIT(2) /* assumes FEAT_HPDS is not used */
49 u64 kimage_voffset __ro_after_init
;
50 EXPORT_SYMBOL(kimage_voffset
);
52 u32 __boot_cpu_mode
[] = { BOOT_CPU_MODE_EL2
, BOOT_CPU_MODE_EL1
};
54 static bool rodata_is_rw __ro_after_init
= true;
57 * The booting CPU updates the failed status @__early_cpu_boot_status,
58 * with MMU turned off.
60 long __section(".mmuoff.data.write") __early_cpu_boot_status
;
63 * Empty_zero_page is a special page that is used for zero-initialized data
66 unsigned long empty_zero_page
[PAGE_SIZE
/ sizeof(unsigned long)] __page_aligned_bss
;
67 EXPORT_SYMBOL(empty_zero_page
);
69 static DEFINE_SPINLOCK(swapper_pgdir_lock
);
70 static DEFINE_MUTEX(fixmap_lock
);
72 void noinstr
set_swapper_pgd(pgd_t
*pgdp
, pgd_t pgd
)
77 * Don't bother with the fixmap if swapper_pg_dir is still mapped
78 * writable in the kernel mapping.
81 WRITE_ONCE(*pgdp
, pgd
);
87 spin_lock(&swapper_pgdir_lock
);
88 fixmap_pgdp
= pgd_set_fixmap(__pa_symbol(pgdp
));
89 WRITE_ONCE(*fixmap_pgdp
, pgd
);
91 * We need dsb(ishst) here to ensure the page-table-walker sees
92 * our new entry before set_p?d() returns. The fixmap's
93 * flush_tlb_kernel_range() via clear_fixmap() does this for us.
96 spin_unlock(&swapper_pgdir_lock
);
99 pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
100 unsigned long size
, pgprot_t vma_prot
)
102 if (!pfn_is_map_memory(pfn
))
103 return pgprot_noncached(vma_prot
);
104 else if (file
->f_flags
& O_SYNC
)
105 return pgprot_writecombine(vma_prot
);
108 EXPORT_SYMBOL(phys_mem_access_prot
);
110 static phys_addr_t __init
early_pgtable_alloc(int shift
)
114 phys
= memblock_phys_alloc_range(PAGE_SIZE
, PAGE_SIZE
, 0,
115 MEMBLOCK_ALLOC_NOLEAKTRACE
);
117 panic("Failed to allocate page table page\n");
122 bool pgattr_change_is_safe(pteval_t old
, pteval_t
new)
125 * The following mapping attributes may be updated in live
126 * kernel mappings without the need for break-before-make.
128 pteval_t mask
= PTE_PXN
| PTE_RDONLY
| PTE_WRITE
| PTE_NG
|
131 /* creating or taking down mappings is always safe */
132 if (!pte_valid(__pte(old
)) || !pte_valid(__pte(new)))
135 /* A live entry's pfn should not change */
136 if (pte_pfn(__pte(old
)) != pte_pfn(__pte(new)))
139 /* live contiguous mappings may not be manipulated at all */
140 if ((old
| new) & PTE_CONT
)
143 /* Transitioning from Non-Global to Global is unsafe */
144 if (old
& ~new & PTE_NG
)
148 * Changing the memory type between Normal and Normal-Tagged is safe
149 * since Tagged is considered a permission attribute from the
150 * mismatched attribute aliases perspective.
152 if (((old
& PTE_ATTRINDX_MASK
) == PTE_ATTRINDX(MT_NORMAL
) ||
153 (old
& PTE_ATTRINDX_MASK
) == PTE_ATTRINDX(MT_NORMAL_TAGGED
)) &&
154 ((new & PTE_ATTRINDX_MASK
) == PTE_ATTRINDX(MT_NORMAL
) ||
155 (new & PTE_ATTRINDX_MASK
) == PTE_ATTRINDX(MT_NORMAL_TAGGED
)))
156 mask
|= PTE_ATTRINDX_MASK
;
158 return ((old
^ new) & ~mask
) == 0;
161 static void init_clear_pgtable(void *table
)
165 /* Ensure the zeroing is observed by page table walks. */
169 static void init_pte(pte_t
*ptep
, unsigned long addr
, unsigned long end
,
170 phys_addr_t phys
, pgprot_t prot
)
173 pte_t old_pte
= __ptep_get(ptep
);
176 * Required barriers to make this visible to the table walker
177 * are deferred to the end of alloc_init_cont_pte().
179 __set_pte_nosync(ptep
, pfn_pte(__phys_to_pfn(phys
), prot
));
182 * After the PTE entry has been populated once, we
183 * only allow updates to the permission attributes.
185 BUG_ON(!pgattr_change_is_safe(pte_val(old_pte
),
186 pte_val(__ptep_get(ptep
))));
189 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
192 static void alloc_init_cont_pte(pmd_t
*pmdp
, unsigned long addr
,
193 unsigned long end
, phys_addr_t phys
,
195 phys_addr_t (*pgtable_alloc
)(int),
199 pmd_t pmd
= READ_ONCE(*pmdp
);
202 BUG_ON(pmd_sect(pmd
));
204 pmdval_t pmdval
= PMD_TYPE_TABLE
| PMD_TABLE_UXN
| PMD_TABLE_AF
;
205 phys_addr_t pte_phys
;
207 if (flags
& NO_EXEC_MAPPINGS
)
208 pmdval
|= PMD_TABLE_PXN
;
209 BUG_ON(!pgtable_alloc
);
210 pte_phys
= pgtable_alloc(PAGE_SHIFT
);
211 ptep
= pte_set_fixmap(pte_phys
);
212 init_clear_pgtable(ptep
);
213 ptep
+= pte_index(addr
);
214 __pmd_populate(pmdp
, pte_phys
, pmdval
);
216 BUG_ON(pmd_bad(pmd
));
217 ptep
= pte_set_fixmap_offset(pmdp
, addr
);
221 pgprot_t __prot
= prot
;
223 next
= pte_cont_addr_end(addr
, end
);
225 /* use a contiguous mapping if the range is suitably aligned */
226 if ((((addr
| next
| phys
) & ~CONT_PTE_MASK
) == 0) &&
227 (flags
& NO_CONT_MAPPINGS
) == 0)
228 __prot
= __pgprot(pgprot_val(prot
) | PTE_CONT
);
230 init_pte(ptep
, addr
, next
, phys
, __prot
);
232 ptep
+= pte_index(next
) - pte_index(addr
);
234 } while (addr
= next
, addr
!= end
);
237 * Note: barriers and maintenance necessary to clear the fixmap slot
238 * ensure that all previous pgtable writes are visible to the table
244 static void init_pmd(pmd_t
*pmdp
, unsigned long addr
, unsigned long end
,
245 phys_addr_t phys
, pgprot_t prot
,
246 phys_addr_t (*pgtable_alloc
)(int), int flags
)
251 pmd_t old_pmd
= READ_ONCE(*pmdp
);
253 next
= pmd_addr_end(addr
, end
);
255 /* try section mapping first */
256 if (((addr
| next
| phys
) & ~PMD_MASK
) == 0 &&
257 (flags
& NO_BLOCK_MAPPINGS
) == 0) {
258 pmd_set_huge(pmdp
, phys
, prot
);
261 * After the PMD entry has been populated once, we
262 * only allow updates to the permission attributes.
264 BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd
),
265 READ_ONCE(pmd_val(*pmdp
))));
267 alloc_init_cont_pte(pmdp
, addr
, next
, phys
, prot
,
268 pgtable_alloc
, flags
);
270 BUG_ON(pmd_val(old_pmd
) != 0 &&
271 pmd_val(old_pmd
) != READ_ONCE(pmd_val(*pmdp
)));
274 } while (pmdp
++, addr
= next
, addr
!= end
);
277 static void alloc_init_cont_pmd(pud_t
*pudp
, unsigned long addr
,
278 unsigned long end
, phys_addr_t phys
,
280 phys_addr_t (*pgtable_alloc
)(int), int flags
)
283 pud_t pud
= READ_ONCE(*pudp
);
287 * Check for initial section mappings in the pgd/pud.
289 BUG_ON(pud_sect(pud
));
291 pudval_t pudval
= PUD_TYPE_TABLE
| PUD_TABLE_UXN
| PUD_TABLE_AF
;
292 phys_addr_t pmd_phys
;
294 if (flags
& NO_EXEC_MAPPINGS
)
295 pudval
|= PUD_TABLE_PXN
;
296 BUG_ON(!pgtable_alloc
);
297 pmd_phys
= pgtable_alloc(PMD_SHIFT
);
298 pmdp
= pmd_set_fixmap(pmd_phys
);
299 init_clear_pgtable(pmdp
);
300 pmdp
+= pmd_index(addr
);
301 __pud_populate(pudp
, pmd_phys
, pudval
);
303 BUG_ON(pud_bad(pud
));
304 pmdp
= pmd_set_fixmap_offset(pudp
, addr
);
308 pgprot_t __prot
= prot
;
310 next
= pmd_cont_addr_end(addr
, end
);
312 /* use a contiguous mapping if the range is suitably aligned */
313 if ((((addr
| next
| phys
) & ~CONT_PMD_MASK
) == 0) &&
314 (flags
& NO_CONT_MAPPINGS
) == 0)
315 __prot
= __pgprot(pgprot_val(prot
) | PTE_CONT
);
317 init_pmd(pmdp
, addr
, next
, phys
, __prot
, pgtable_alloc
, flags
);
319 pmdp
+= pmd_index(next
) - pmd_index(addr
);
321 } while (addr
= next
, addr
!= end
);
326 static void alloc_init_pud(p4d_t
*p4dp
, unsigned long addr
, unsigned long end
,
327 phys_addr_t phys
, pgprot_t prot
,
328 phys_addr_t (*pgtable_alloc
)(int),
332 p4d_t p4d
= READ_ONCE(*p4dp
);
336 p4dval_t p4dval
= P4D_TYPE_TABLE
| P4D_TABLE_UXN
| P4D_TABLE_AF
;
337 phys_addr_t pud_phys
;
339 if (flags
& NO_EXEC_MAPPINGS
)
340 p4dval
|= P4D_TABLE_PXN
;
341 BUG_ON(!pgtable_alloc
);
342 pud_phys
= pgtable_alloc(PUD_SHIFT
);
343 pudp
= pud_set_fixmap(pud_phys
);
344 init_clear_pgtable(pudp
);
345 pudp
+= pud_index(addr
);
346 __p4d_populate(p4dp
, pud_phys
, p4dval
);
348 BUG_ON(p4d_bad(p4d
));
349 pudp
= pud_set_fixmap_offset(p4dp
, addr
);
353 pud_t old_pud
= READ_ONCE(*pudp
);
355 next
= pud_addr_end(addr
, end
);
358 * For 4K granule only, attempt to put down a 1GB block
360 if (pud_sect_supported() &&
361 ((addr
| next
| phys
) & ~PUD_MASK
) == 0 &&
362 (flags
& NO_BLOCK_MAPPINGS
) == 0) {
363 pud_set_huge(pudp
, phys
, prot
);
366 * After the PUD entry has been populated once, we
367 * only allow updates to the permission attributes.
369 BUG_ON(!pgattr_change_is_safe(pud_val(old_pud
),
370 READ_ONCE(pud_val(*pudp
))));
372 alloc_init_cont_pmd(pudp
, addr
, next
, phys
, prot
,
373 pgtable_alloc
, flags
);
375 BUG_ON(pud_val(old_pud
) != 0 &&
376 pud_val(old_pud
) != READ_ONCE(pud_val(*pudp
)));
379 } while (pudp
++, addr
= next
, addr
!= end
);
384 static void alloc_init_p4d(pgd_t
*pgdp
, unsigned long addr
, unsigned long end
,
385 phys_addr_t phys
, pgprot_t prot
,
386 phys_addr_t (*pgtable_alloc
)(int),
390 pgd_t pgd
= READ_ONCE(*pgdp
);
394 pgdval_t pgdval
= PGD_TYPE_TABLE
| PGD_TABLE_UXN
| PGD_TABLE_AF
;
395 phys_addr_t p4d_phys
;
397 if (flags
& NO_EXEC_MAPPINGS
)
398 pgdval
|= PGD_TABLE_PXN
;
399 BUG_ON(!pgtable_alloc
);
400 p4d_phys
= pgtable_alloc(P4D_SHIFT
);
401 p4dp
= p4d_set_fixmap(p4d_phys
);
402 init_clear_pgtable(p4dp
);
403 p4dp
+= p4d_index(addr
);
404 __pgd_populate(pgdp
, p4d_phys
, pgdval
);
406 BUG_ON(pgd_bad(pgd
));
407 p4dp
= p4d_set_fixmap_offset(pgdp
, addr
);
411 p4d_t old_p4d
= READ_ONCE(*p4dp
);
413 next
= p4d_addr_end(addr
, end
);
415 alloc_init_pud(p4dp
, addr
, next
, phys
, prot
,
416 pgtable_alloc
, flags
);
418 BUG_ON(p4d_val(old_p4d
) != 0 &&
419 p4d_val(old_p4d
) != READ_ONCE(p4d_val(*p4dp
)));
422 } while (p4dp
++, addr
= next
, addr
!= end
);
427 static void __create_pgd_mapping_locked(pgd_t
*pgdir
, phys_addr_t phys
,
428 unsigned long virt
, phys_addr_t size
,
430 phys_addr_t (*pgtable_alloc
)(int),
433 unsigned long addr
, end
, next
;
434 pgd_t
*pgdp
= pgd_offset_pgd(pgdir
, virt
);
437 * If the virtual and physical address don't have the same offset
438 * within a page, we cannot map the region as the caller expects.
440 if (WARN_ON((phys
^ virt
) & ~PAGE_MASK
))
444 addr
= virt
& PAGE_MASK
;
445 end
= PAGE_ALIGN(virt
+ size
);
448 next
= pgd_addr_end(addr
, end
);
449 alloc_init_p4d(pgdp
, addr
, next
, phys
, prot
, pgtable_alloc
,
452 } while (pgdp
++, addr
= next
, addr
!= end
);
455 static void __create_pgd_mapping(pgd_t
*pgdir
, phys_addr_t phys
,
456 unsigned long virt
, phys_addr_t size
,
458 phys_addr_t (*pgtable_alloc
)(int),
461 mutex_lock(&fixmap_lock
);
462 __create_pgd_mapping_locked(pgdir
, phys
, virt
, size
, prot
,
463 pgtable_alloc
, flags
);
464 mutex_unlock(&fixmap_lock
);
467 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
468 extern __alias(__create_pgd_mapping_locked
)
469 void create_kpti_ng_temp_pgd(pgd_t
*pgdir
, phys_addr_t phys
, unsigned long virt
,
470 phys_addr_t size
, pgprot_t prot
,
471 phys_addr_t (*pgtable_alloc
)(int), int flags
);
474 static phys_addr_t
__pgd_pgtable_alloc(int shift
)
476 /* Page is zeroed by init_clear_pgtable() so don't duplicate effort. */
477 void *ptr
= (void *)__get_free_page(GFP_PGTABLE_KERNEL
& ~__GFP_ZERO
);
483 static phys_addr_t
pgd_pgtable_alloc(int shift
)
485 phys_addr_t pa
= __pgd_pgtable_alloc(shift
);
486 struct ptdesc
*ptdesc
= page_ptdesc(phys_to_page(pa
));
489 * Call proper page table ctor in case later we need to
490 * call core mm functions like apply_to_page_range() on
491 * this pre-allocated page table.
493 * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is
494 * folded, and if so pagetable_pte_ctor() becomes nop.
496 if (shift
== PAGE_SHIFT
)
497 BUG_ON(!pagetable_pte_ctor(ptdesc
));
498 else if (shift
== PMD_SHIFT
)
499 BUG_ON(!pagetable_pmd_ctor(ptdesc
));
505 * This function can only be used to modify existing table entries,
506 * without allocating new levels of table. Note that this permits the
507 * creation of new section or page entries.
509 void __init
create_mapping_noalloc(phys_addr_t phys
, unsigned long virt
,
510 phys_addr_t size
, pgprot_t prot
)
512 if (virt
< PAGE_OFFSET
) {
513 pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
517 __create_pgd_mapping(init_mm
.pgd
, phys
, virt
, size
, prot
, NULL
,
521 void __init
create_pgd_mapping(struct mm_struct
*mm
, phys_addr_t phys
,
522 unsigned long virt
, phys_addr_t size
,
523 pgprot_t prot
, bool page_mappings_only
)
527 BUG_ON(mm
== &init_mm
);
529 if (page_mappings_only
)
530 flags
= NO_BLOCK_MAPPINGS
| NO_CONT_MAPPINGS
;
532 __create_pgd_mapping(mm
->pgd
, phys
, virt
, size
, prot
,
533 pgd_pgtable_alloc
, flags
);
536 static void update_mapping_prot(phys_addr_t phys
, unsigned long virt
,
537 phys_addr_t size
, pgprot_t prot
)
539 if (virt
< PAGE_OFFSET
) {
540 pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
545 __create_pgd_mapping(init_mm
.pgd
, phys
, virt
, size
, prot
, NULL
,
548 /* flush the TLBs after updating live kernel mappings */
549 flush_tlb_kernel_range(virt
, virt
+ size
);
552 static void __init
__map_memblock(pgd_t
*pgdp
, phys_addr_t start
,
553 phys_addr_t end
, pgprot_t prot
, int flags
)
555 __create_pgd_mapping(pgdp
, start
, __phys_to_virt(start
), end
- start
,
556 prot
, early_pgtable_alloc
, flags
);
559 void __init
mark_linear_text_alias_ro(void)
562 * Remove the write permissions from the linear alias of .text/.rodata
564 update_mapping_prot(__pa_symbol(_stext
), (unsigned long)lm_alias(_stext
),
565 (unsigned long)__init_begin
- (unsigned long)_stext
,
571 bool __ro_after_init kfence_early_init
= !!CONFIG_KFENCE_SAMPLE_INTERVAL
;
573 /* early_param() will be parsed before map_mem() below. */
574 static int __init
parse_kfence_early_init(char *arg
)
578 if (get_option(&arg
, &val
))
579 kfence_early_init
= !!val
;
582 early_param("kfence.sample_interval", parse_kfence_early_init
);
584 static phys_addr_t __init
arm64_kfence_alloc_pool(void)
586 phys_addr_t kfence_pool
;
588 if (!kfence_early_init
)
591 kfence_pool
= memblock_phys_alloc(KFENCE_POOL_SIZE
, PAGE_SIZE
);
593 pr_err("failed to allocate kfence pool\n");
594 kfence_early_init
= false;
598 /* Temporarily mark as NOMAP. */
599 memblock_mark_nomap(kfence_pool
, KFENCE_POOL_SIZE
);
604 static void __init
arm64_kfence_map_pool(phys_addr_t kfence_pool
, pgd_t
*pgdp
)
609 /* KFENCE pool needs page-level mapping. */
610 __map_memblock(pgdp
, kfence_pool
, kfence_pool
+ KFENCE_POOL_SIZE
,
611 pgprot_tagged(PAGE_KERNEL
),
612 NO_BLOCK_MAPPINGS
| NO_CONT_MAPPINGS
);
613 memblock_clear_nomap(kfence_pool
, KFENCE_POOL_SIZE
);
614 __kfence_pool
= phys_to_virt(kfence_pool
);
616 #else /* CONFIG_KFENCE */
618 static inline phys_addr_t
arm64_kfence_alloc_pool(void) { return 0; }
619 static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool
, pgd_t
*pgdp
) { }
621 #endif /* CONFIG_KFENCE */
623 static void __init
map_mem(pgd_t
*pgdp
)
625 static const u64 direct_map_end
= _PAGE_END(VA_BITS_MIN
);
626 phys_addr_t kernel_start
= __pa_symbol(_stext
);
627 phys_addr_t kernel_end
= __pa_symbol(__init_begin
);
628 phys_addr_t start
, end
;
629 phys_addr_t early_kfence_pool
;
630 int flags
= NO_EXEC_MAPPINGS
;
634 * Setting hierarchical PXNTable attributes on table entries covering
635 * the linear region is only possible if it is guaranteed that no table
636 * entries at any level are being shared between the linear region and
637 * the vmalloc region. Check whether this is true for the PGD level, in
638 * which case it is guaranteed to be true for all other levels as well.
639 * (Unless we are running with support for LPA2, in which case the
640 * entire reduced VA space is covered by a single pgd_t which will have
641 * been populated without the PXNTable attribute by the time we get here.)
643 BUILD_BUG_ON(pgd_index(direct_map_end
- 1) == pgd_index(direct_map_end
) &&
644 pgd_index(_PAGE_OFFSET(VA_BITS_MIN
)) != PTRS_PER_PGD
- 1);
646 early_kfence_pool
= arm64_kfence_alloc_pool();
648 if (can_set_direct_map())
649 flags
|= NO_BLOCK_MAPPINGS
| NO_CONT_MAPPINGS
;
652 * Take care not to create a writable alias for the
653 * read-only text and rodata sections of the kernel image.
654 * So temporarily mark them as NOMAP to skip mappings in
655 * the following for-loop
657 memblock_mark_nomap(kernel_start
, kernel_end
- kernel_start
);
659 /* map all the memory banks */
660 for_each_mem_range(i
, &start
, &end
) {
664 * The linear map must allow allocation tags reading/writing
665 * if MTE is present. Otherwise, it has the same attributes as
668 __map_memblock(pgdp
, start
, end
, pgprot_tagged(PAGE_KERNEL
),
673 * Map the linear alias of the [_stext, __init_begin) interval
674 * as non-executable now, and remove the write permission in
675 * mark_linear_text_alias_ro() below (which will be called after
676 * alternative patching has completed). This makes the contents
677 * of the region accessible to subsystems such as hibernate,
678 * but protects it from inadvertent modification or execution.
679 * Note that contiguous mappings cannot be remapped in this way,
680 * so we should avoid them here.
682 __map_memblock(pgdp
, kernel_start
, kernel_end
,
683 PAGE_KERNEL
, NO_CONT_MAPPINGS
);
684 memblock_clear_nomap(kernel_start
, kernel_end
- kernel_start
);
685 arm64_kfence_map_pool(early_kfence_pool
, pgdp
);
688 void mark_rodata_ro(void)
690 unsigned long section_size
;
693 * mark .rodata as read only. Use __init_begin rather than __end_rodata
694 * to cover NOTES and EXCEPTION_TABLE.
696 section_size
= (unsigned long)__init_begin
- (unsigned long)__start_rodata
;
697 WRITE_ONCE(rodata_is_rw
, false);
698 update_mapping_prot(__pa_symbol(__start_rodata
), (unsigned long)__start_rodata
,
699 section_size
, PAGE_KERNEL_RO
);
702 static void __init
declare_vma(struct vm_struct
*vma
,
703 void *va_start
, void *va_end
,
704 unsigned long vm_flags
)
706 phys_addr_t pa_start
= __pa_symbol(va_start
);
707 unsigned long size
= va_end
- va_start
;
709 BUG_ON(!PAGE_ALIGNED(pa_start
));
710 BUG_ON(!PAGE_ALIGNED(size
));
712 if (!(vm_flags
& VM_NO_GUARD
))
715 vma
->addr
= va_start
;
716 vma
->phys_addr
= pa_start
;
718 vma
->flags
= VM_MAP
| vm_flags
;
719 vma
->caller
= __builtin_return_address(0);
721 vm_area_add_early(vma
);
724 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
725 static pgprot_t
kernel_exec_prot(void)
727 return rodata_enabled
? PAGE_KERNEL_ROX
: PAGE_KERNEL_EXEC
;
730 static int __init
map_entry_trampoline(void)
734 if (!arm64_kernel_unmapped_at_el0())
737 pgprot_t prot
= kernel_exec_prot();
738 phys_addr_t pa_start
= __pa_symbol(__entry_tramp_text_start
);
740 /* The trampoline is always mapped and can therefore be global */
741 pgprot_val(prot
) &= ~PTE_NG
;
743 /* Map only the text into the trampoline page table */
744 memset(tramp_pg_dir
, 0, PGD_SIZE
);
745 __create_pgd_mapping(tramp_pg_dir
, pa_start
, TRAMP_VALIAS
,
746 entry_tramp_text_size(), prot
,
747 __pgd_pgtable_alloc
, NO_BLOCK_MAPPINGS
);
749 /* Map both the text and data into the kernel page table */
750 for (i
= 0; i
< DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE
); i
++)
751 __set_fixmap(FIX_ENTRY_TRAMP_TEXT1
- i
,
752 pa_start
+ i
* PAGE_SIZE
, prot
);
754 if (IS_ENABLED(CONFIG_RELOCATABLE
))
755 __set_fixmap(FIX_ENTRY_TRAMP_TEXT1
- i
,
756 pa_start
+ i
* PAGE_SIZE
, PAGE_KERNEL_RO
);
760 core_initcall(map_entry_trampoline
);
764 * Declare the VMA areas for the kernel
766 static void __init
declare_kernel_vmas(void)
768 static struct vm_struct vmlinux_seg
[KERNEL_SEGMENT_COUNT
];
770 declare_vma(&vmlinux_seg
[0], _stext
, _etext
, VM_NO_GUARD
);
771 declare_vma(&vmlinux_seg
[1], __start_rodata
, __inittext_begin
, VM_NO_GUARD
);
772 declare_vma(&vmlinux_seg
[2], __inittext_begin
, __inittext_end
, VM_NO_GUARD
);
773 declare_vma(&vmlinux_seg
[3], __initdata_begin
, __initdata_end
, VM_NO_GUARD
);
774 declare_vma(&vmlinux_seg
[4], _data
, _end
, 0);
777 void __pi_map_range(u64
*pgd
, u64 start
, u64 end
, u64 pa
, pgprot_t prot
,
778 int level
, pte_t
*tbl
, bool may_use_cont
, u64 va_offset
);
780 static u8 idmap_ptes
[IDMAP_LEVELS
- 1][PAGE_SIZE
] __aligned(PAGE_SIZE
) __ro_after_init
,
781 kpti_ptes
[IDMAP_LEVELS
- 1][PAGE_SIZE
] __aligned(PAGE_SIZE
) __ro_after_init
;
783 static void __init
create_idmap(void)
785 u64 start
= __pa_symbol(__idmap_text_start
);
786 u64 end
= __pa_symbol(__idmap_text_end
);
787 u64 ptep
= __pa_symbol(idmap_ptes
);
789 __pi_map_range(&ptep
, start
, end
, start
, PAGE_KERNEL_ROX
,
790 IDMAP_ROOT_LEVEL
, (pte_t
*)idmap_pg_dir
, false,
791 __phys_to_virt(ptep
) - ptep
);
793 if (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0
) && !arm64_use_ng_mappings
) {
794 extern u32 __idmap_kpti_flag
;
795 u64 pa
= __pa_symbol(&__idmap_kpti_flag
);
798 * The KPTI G-to-nG conversion code needs a read-write mapping
799 * of its synchronization flag in the ID map.
801 ptep
= __pa_symbol(kpti_ptes
);
802 __pi_map_range(&ptep
, pa
, pa
+ sizeof(u32
), pa
, PAGE_KERNEL
,
803 IDMAP_ROOT_LEVEL
, (pte_t
*)idmap_pg_dir
, false,
804 __phys_to_virt(ptep
) - ptep
);
808 void __init
paging_init(void)
810 map_mem(swapper_pg_dir
);
812 memblock_allow_resize();
815 declare_kernel_vmas();
818 #ifdef CONFIG_MEMORY_HOTPLUG
819 static void free_hotplug_page_range(struct page
*page
, size_t size
,
820 struct vmem_altmap
*altmap
)
823 vmem_altmap_free(altmap
, size
>> PAGE_SHIFT
);
825 WARN_ON(PageReserved(page
));
826 free_pages((unsigned long)page_address(page
), get_order(size
));
830 static void free_hotplug_pgtable_page(struct page
*page
)
832 free_hotplug_page_range(page
, PAGE_SIZE
, NULL
);
835 static bool pgtable_range_aligned(unsigned long start
, unsigned long end
,
836 unsigned long floor
, unsigned long ceiling
,
849 if (end
- 1 > ceiling
- 1)
854 static void unmap_hotplug_pte_range(pmd_t
*pmdp
, unsigned long addr
,
855 unsigned long end
, bool free_mapped
,
856 struct vmem_altmap
*altmap
)
861 ptep
= pte_offset_kernel(pmdp
, addr
);
862 pte
= __ptep_get(ptep
);
866 WARN_ON(!pte_present(pte
));
867 __pte_clear(&init_mm
, addr
, ptep
);
868 flush_tlb_kernel_range(addr
, addr
+ PAGE_SIZE
);
870 free_hotplug_page_range(pte_page(pte
),
872 } while (addr
+= PAGE_SIZE
, addr
< end
);
875 static void unmap_hotplug_pmd_range(pud_t
*pudp
, unsigned long addr
,
876 unsigned long end
, bool free_mapped
,
877 struct vmem_altmap
*altmap
)
883 next
= pmd_addr_end(addr
, end
);
884 pmdp
= pmd_offset(pudp
, addr
);
885 pmd
= READ_ONCE(*pmdp
);
889 WARN_ON(!pmd_present(pmd
));
894 * One TLBI should be sufficient here as the PMD_SIZE
895 * range is mapped with a single block entry.
897 flush_tlb_kernel_range(addr
, addr
+ PAGE_SIZE
);
899 free_hotplug_page_range(pmd_page(pmd
),
903 WARN_ON(!pmd_table(pmd
));
904 unmap_hotplug_pte_range(pmdp
, addr
, next
, free_mapped
, altmap
);
905 } while (addr
= next
, addr
< end
);
908 static void unmap_hotplug_pud_range(p4d_t
*p4dp
, unsigned long addr
,
909 unsigned long end
, bool free_mapped
,
910 struct vmem_altmap
*altmap
)
916 next
= pud_addr_end(addr
, end
);
917 pudp
= pud_offset(p4dp
, addr
);
918 pud
= READ_ONCE(*pudp
);
922 WARN_ON(!pud_present(pud
));
927 * One TLBI should be sufficient here as the PUD_SIZE
928 * range is mapped with a single block entry.
930 flush_tlb_kernel_range(addr
, addr
+ PAGE_SIZE
);
932 free_hotplug_page_range(pud_page(pud
),
936 WARN_ON(!pud_table(pud
));
937 unmap_hotplug_pmd_range(pudp
, addr
, next
, free_mapped
, altmap
);
938 } while (addr
= next
, addr
< end
);
941 static void unmap_hotplug_p4d_range(pgd_t
*pgdp
, unsigned long addr
,
942 unsigned long end
, bool free_mapped
,
943 struct vmem_altmap
*altmap
)
949 next
= p4d_addr_end(addr
, end
);
950 p4dp
= p4d_offset(pgdp
, addr
);
951 p4d
= READ_ONCE(*p4dp
);
955 WARN_ON(!p4d_present(p4d
));
956 unmap_hotplug_pud_range(p4dp
, addr
, next
, free_mapped
, altmap
);
957 } while (addr
= next
, addr
< end
);
960 static void unmap_hotplug_range(unsigned long addr
, unsigned long end
,
961 bool free_mapped
, struct vmem_altmap
*altmap
)
967 * altmap can only be used as vmemmap mapping backing memory.
968 * In case the backing memory itself is not being freed, then
969 * altmap is irrelevant. Warn about this inconsistency when
972 WARN_ON(!free_mapped
&& altmap
);
975 next
= pgd_addr_end(addr
, end
);
976 pgdp
= pgd_offset_k(addr
);
977 pgd
= READ_ONCE(*pgdp
);
981 WARN_ON(!pgd_present(pgd
));
982 unmap_hotplug_p4d_range(pgdp
, addr
, next
, free_mapped
, altmap
);
983 } while (addr
= next
, addr
< end
);
986 static void free_empty_pte_table(pmd_t
*pmdp
, unsigned long addr
,
987 unsigned long end
, unsigned long floor
,
988 unsigned long ceiling
)
991 unsigned long i
, start
= addr
;
994 ptep
= pte_offset_kernel(pmdp
, addr
);
995 pte
= __ptep_get(ptep
);
998 * This is just a sanity check here which verifies that
999 * pte clearing has been done by earlier unmap loops.
1001 WARN_ON(!pte_none(pte
));
1002 } while (addr
+= PAGE_SIZE
, addr
< end
);
1004 if (!pgtable_range_aligned(start
, end
, floor
, ceiling
, PMD_MASK
))
1008 * Check whether we can free the pte page if the rest of the
1009 * entries are empty. Overlap with other regions have been
1010 * handled by the floor/ceiling check.
1012 ptep
= pte_offset_kernel(pmdp
, 0UL);
1013 for (i
= 0; i
< PTRS_PER_PTE
; i
++) {
1014 if (!pte_none(__ptep_get(&ptep
[i
])))
1019 __flush_tlb_kernel_pgtable(start
);
1020 free_hotplug_pgtable_page(virt_to_page(ptep
));
1023 static void free_empty_pmd_table(pud_t
*pudp
, unsigned long addr
,
1024 unsigned long end
, unsigned long floor
,
1025 unsigned long ceiling
)
1028 unsigned long i
, next
, start
= addr
;
1031 next
= pmd_addr_end(addr
, end
);
1032 pmdp
= pmd_offset(pudp
, addr
);
1033 pmd
= READ_ONCE(*pmdp
);
1037 WARN_ON(!pmd_present(pmd
) || !pmd_table(pmd
) || pmd_sect(pmd
));
1038 free_empty_pte_table(pmdp
, addr
, next
, floor
, ceiling
);
1039 } while (addr
= next
, addr
< end
);
1041 if (CONFIG_PGTABLE_LEVELS
<= 2)
1044 if (!pgtable_range_aligned(start
, end
, floor
, ceiling
, PUD_MASK
))
1048 * Check whether we can free the pmd page if the rest of the
1049 * entries are empty. Overlap with other regions have been
1050 * handled by the floor/ceiling check.
1052 pmdp
= pmd_offset(pudp
, 0UL);
1053 for (i
= 0; i
< PTRS_PER_PMD
; i
++) {
1054 if (!pmd_none(READ_ONCE(pmdp
[i
])))
1059 __flush_tlb_kernel_pgtable(start
);
1060 free_hotplug_pgtable_page(virt_to_page(pmdp
));
1063 static void free_empty_pud_table(p4d_t
*p4dp
, unsigned long addr
,
1064 unsigned long end
, unsigned long floor
,
1065 unsigned long ceiling
)
1068 unsigned long i
, next
, start
= addr
;
1071 next
= pud_addr_end(addr
, end
);
1072 pudp
= pud_offset(p4dp
, addr
);
1073 pud
= READ_ONCE(*pudp
);
1077 WARN_ON(!pud_present(pud
) || !pud_table(pud
) || pud_sect(pud
));
1078 free_empty_pmd_table(pudp
, addr
, next
, floor
, ceiling
);
1079 } while (addr
= next
, addr
< end
);
1081 if (!pgtable_l4_enabled())
1084 if (!pgtable_range_aligned(start
, end
, floor
, ceiling
, P4D_MASK
))
1088 * Check whether we can free the pud page if the rest of the
1089 * entries are empty. Overlap with other regions have been
1090 * handled by the floor/ceiling check.
1092 pudp
= pud_offset(p4dp
, 0UL);
1093 for (i
= 0; i
< PTRS_PER_PUD
; i
++) {
1094 if (!pud_none(READ_ONCE(pudp
[i
])))
1099 __flush_tlb_kernel_pgtable(start
);
1100 free_hotplug_pgtable_page(virt_to_page(pudp
));
1103 static void free_empty_p4d_table(pgd_t
*pgdp
, unsigned long addr
,
1104 unsigned long end
, unsigned long floor
,
1105 unsigned long ceiling
)
1108 unsigned long i
, next
, start
= addr
;
1111 next
= p4d_addr_end(addr
, end
);
1112 p4dp
= p4d_offset(pgdp
, addr
);
1113 p4d
= READ_ONCE(*p4dp
);
1117 WARN_ON(!p4d_present(p4d
));
1118 free_empty_pud_table(p4dp
, addr
, next
, floor
, ceiling
);
1119 } while (addr
= next
, addr
< end
);
1121 if (!pgtable_l5_enabled())
1124 if (!pgtable_range_aligned(start
, end
, floor
, ceiling
, PGDIR_MASK
))
1128 * Check whether we can free the p4d page if the rest of the
1129 * entries are empty. Overlap with other regions have been
1130 * handled by the floor/ceiling check.
1132 p4dp
= p4d_offset(pgdp
, 0UL);
1133 for (i
= 0; i
< PTRS_PER_P4D
; i
++) {
1134 if (!p4d_none(READ_ONCE(p4dp
[i
])))
1139 __flush_tlb_kernel_pgtable(start
);
1140 free_hotplug_pgtable_page(virt_to_page(p4dp
));
1143 static void free_empty_tables(unsigned long addr
, unsigned long end
,
1144 unsigned long floor
, unsigned long ceiling
)
1150 next
= pgd_addr_end(addr
, end
);
1151 pgdp
= pgd_offset_k(addr
);
1152 pgd
= READ_ONCE(*pgdp
);
1156 WARN_ON(!pgd_present(pgd
));
1157 free_empty_p4d_table(pgdp
, addr
, next
, floor
, ceiling
);
1158 } while (addr
= next
, addr
< end
);
1162 void __meminit
vmemmap_set_pmd(pmd_t
*pmdp
, void *p
, int node
,
1163 unsigned long addr
, unsigned long next
)
1165 pmd_set_huge(pmdp
, __pa(p
), __pgprot(PROT_SECT_NORMAL
));
1168 int __meminit
vmemmap_check_pmd(pmd_t
*pmdp
, int node
,
1169 unsigned long addr
, unsigned long next
)
1171 vmemmap_verify((pte_t
*)pmdp
, node
, addr
, next
);
1175 int __meminit
vmemmap_populate(unsigned long start
, unsigned long end
, int node
,
1176 struct vmem_altmap
*altmap
)
1178 WARN_ON((start
< VMEMMAP_START
) || (end
> VMEMMAP_END
));
1180 if (!IS_ENABLED(CONFIG_ARM64_4K_PAGES
))
1181 return vmemmap_populate_basepages(start
, end
, node
, altmap
);
1183 return vmemmap_populate_hugepages(start
, end
, node
, altmap
);
1186 #ifdef CONFIG_MEMORY_HOTPLUG
1187 void vmemmap_free(unsigned long start
, unsigned long end
,
1188 struct vmem_altmap
*altmap
)
1190 WARN_ON((start
< VMEMMAP_START
) || (end
> VMEMMAP_END
));
1192 unmap_hotplug_range(start
, end
, true, altmap
);
1193 free_empty_tables(start
, end
, VMEMMAP_START
, VMEMMAP_END
);
1195 #endif /* CONFIG_MEMORY_HOTPLUG */
1197 int pud_set_huge(pud_t
*pudp
, phys_addr_t phys
, pgprot_t prot
)
1199 pud_t new_pud
= pfn_pud(__phys_to_pfn(phys
), mk_pud_sect_prot(prot
));
1201 /* Only allow permission changes for now */
1202 if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp
)),
1206 VM_BUG_ON(phys
& ~PUD_MASK
);
1207 set_pud(pudp
, new_pud
);
1211 int pmd_set_huge(pmd_t
*pmdp
, phys_addr_t phys
, pgprot_t prot
)
1213 pmd_t new_pmd
= pfn_pmd(__phys_to_pfn(phys
), mk_pmd_sect_prot(prot
));
1215 /* Only allow permission changes for now */
1216 if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp
)),
1220 VM_BUG_ON(phys
& ~PMD_MASK
);
1221 set_pmd(pmdp
, new_pmd
);
1225 #ifndef __PAGETABLE_P4D_FOLDED
1226 void p4d_clear_huge(p4d_t
*p4dp
)
1231 int pud_clear_huge(pud_t
*pudp
)
1233 if (!pud_sect(READ_ONCE(*pudp
)))
1239 int pmd_clear_huge(pmd_t
*pmdp
)
1241 if (!pmd_sect(READ_ONCE(*pmdp
)))
1247 int pmd_free_pte_page(pmd_t
*pmdp
, unsigned long addr
)
1252 pmd
= READ_ONCE(*pmdp
);
1254 if (!pmd_table(pmd
)) {
1259 table
= pte_offset_kernel(pmdp
, addr
);
1261 __flush_tlb_kernel_pgtable(addr
);
1262 pte_free_kernel(NULL
, table
);
1266 int pud_free_pmd_page(pud_t
*pudp
, unsigned long addr
)
1271 unsigned long next
, end
;
1273 pud
= READ_ONCE(*pudp
);
1275 if (!pud_table(pud
)) {
1280 table
= pmd_offset(pudp
, addr
);
1283 end
= addr
+ PUD_SIZE
;
1285 pmd_free_pte_page(pmdp
, next
);
1286 } while (pmdp
++, next
+= PMD_SIZE
, next
!= end
);
1289 __flush_tlb_kernel_pgtable(addr
);
1290 pmd_free(NULL
, table
);
1294 #ifdef CONFIG_MEMORY_HOTPLUG
1295 static void __remove_pgd_mapping(pgd_t
*pgdir
, unsigned long start
, u64 size
)
1297 unsigned long end
= start
+ size
;
1299 WARN_ON(pgdir
!= init_mm
.pgd
);
1300 WARN_ON((start
< PAGE_OFFSET
) || (end
> PAGE_END
));
1302 unmap_hotplug_range(start
, end
, false, NULL
);
1303 free_empty_tables(start
, end
, PAGE_OFFSET
, PAGE_END
);
1306 struct range
arch_get_mappable_range(void)
1308 struct range mhp_range
;
1309 u64 start_linear_pa
= __pa(_PAGE_OFFSET(vabits_actual
));
1310 u64 end_linear_pa
= __pa(PAGE_END
- 1);
1312 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE
)) {
1314 * Check for a wrap, it is possible because of randomized linear
1315 * mapping the start physical address is actually bigger than
1316 * the end physical address. In this case set start to zero
1317 * because [0, end_linear_pa] range must still be able to cover
1318 * all addressable physical addresses.
1320 if (start_linear_pa
> end_linear_pa
)
1321 start_linear_pa
= 0;
1324 WARN_ON(start_linear_pa
> end_linear_pa
);
1327 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
1328 * accommodating both its ends but excluding PAGE_END. Max physical
1329 * range which can be mapped inside this linear mapping range, must
1330 * also be derived from its end points.
1332 mhp_range
.start
= start_linear_pa
;
1333 mhp_range
.end
= end_linear_pa
;
1338 int arch_add_memory(int nid
, u64 start
, u64 size
,
1339 struct mhp_params
*params
)
1341 int ret
, flags
= NO_EXEC_MAPPINGS
;
1343 VM_BUG_ON(!mhp_range_allowed(start
, size
, true));
1345 if (can_set_direct_map())
1346 flags
|= NO_BLOCK_MAPPINGS
| NO_CONT_MAPPINGS
;
1348 __create_pgd_mapping(swapper_pg_dir
, start
, __phys_to_virt(start
),
1349 size
, params
->pgprot
, __pgd_pgtable_alloc
,
1352 memblock_clear_nomap(start
, size
);
1354 ret
= __add_pages(nid
, start
>> PAGE_SHIFT
, size
>> PAGE_SHIFT
,
1357 __remove_pgd_mapping(swapper_pg_dir
,
1358 __phys_to_virt(start
), size
);
1360 max_pfn
= PFN_UP(start
+ size
);
1361 max_low_pfn
= max_pfn
;
1367 void arch_remove_memory(u64 start
, u64 size
, struct vmem_altmap
*altmap
)
1369 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1370 unsigned long nr_pages
= size
>> PAGE_SHIFT
;
1372 __remove_pages(start_pfn
, nr_pages
, altmap
);
1373 __remove_pgd_mapping(swapper_pg_dir
, __phys_to_virt(start
), size
);
1377 * This memory hotplug notifier helps prevent boot memory from being
1378 * inadvertently removed as it blocks pfn range offlining process in
1379 * __offline_pages(). Hence this prevents both offlining as well as
1380 * removal process for boot memory which is initially always online.
1381 * In future if and when boot memory could be removed, this notifier
1382 * should be dropped and free_hotplug_page_range() should handle any
1383 * reserved pages allocated during boot.
1385 static int prevent_bootmem_remove_notifier(struct notifier_block
*nb
,
1386 unsigned long action
, void *data
)
1388 struct mem_section
*ms
;
1389 struct memory_notify
*arg
= data
;
1390 unsigned long end_pfn
= arg
->start_pfn
+ arg
->nr_pages
;
1391 unsigned long pfn
= arg
->start_pfn
;
1393 if ((action
!= MEM_GOING_OFFLINE
) && (action
!= MEM_OFFLINE
))
1396 for (; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
1397 unsigned long start
= PFN_PHYS(pfn
);
1398 unsigned long end
= start
+ (1UL << PA_SECTION_SHIFT
);
1400 ms
= __pfn_to_section(pfn
);
1401 if (!early_section(ms
))
1404 if (action
== MEM_GOING_OFFLINE
) {
1406 * Boot memory removal is not supported. Prevent
1407 * it via blocking any attempted offline request
1408 * for the boot memory and just report it.
1410 pr_warn("Boot memory [%lx %lx] offlining attempted\n", start
, end
);
1412 } else if (action
== MEM_OFFLINE
) {
1414 * This should have never happened. Boot memory
1415 * offlining should have been prevented by this
1416 * very notifier. Probably some memory removal
1417 * procedure might have changed which would then
1418 * require further debug.
1420 pr_err("Boot memory [%lx %lx] offlined\n", start
, end
);
1423 * Core memory hotplug does not process a return
1424 * code from the notifier for MEM_OFFLINE events.
1425 * The error condition has been reported. Return
1426 * from here as if ignored.
1434 static struct notifier_block prevent_bootmem_remove_nb
= {
1435 .notifier_call
= prevent_bootmem_remove_notifier
,
1439 * This ensures that boot memory sections on the platform are online
1440 * from early boot. Memory sections could not be prevented from being
1441 * offlined, unless for some reason they are not online to begin with.
1442 * This helps validate the basic assumption on which the above memory
1443 * event notifier works to prevent boot memory section offlining and
1444 * its possible removal.
1446 static void validate_bootmem_online(void)
1448 phys_addr_t start
, end
, addr
;
1449 struct mem_section
*ms
;
1453 * Scanning across all memblock might be expensive
1454 * on some big memory systems. Hence enable this
1455 * validation only with DEBUG_VM.
1457 if (!IS_ENABLED(CONFIG_DEBUG_VM
))
1460 for_each_mem_range(i
, &start
, &end
) {
1461 for (addr
= start
; addr
< end
; addr
+= (1UL << PA_SECTION_SHIFT
)) {
1462 ms
= __pfn_to_section(PHYS_PFN(addr
));
1465 * All memory ranges in the system at this point
1466 * should have been marked as early sections.
1468 WARN_ON(!early_section(ms
));
1471 * Memory notifier mechanism here to prevent boot
1472 * memory offlining depends on the fact that each
1473 * early section memory on the system is initially
1474 * online. Otherwise a given memory section which
1475 * is already offline will be overlooked and can
1476 * be removed completely. Call out such sections.
1478 if (!online_section(ms
))
1479 pr_err("Boot memory [%llx %llx] is offline, can be removed\n",
1480 addr
, addr
+ (1UL << PA_SECTION_SHIFT
));
1485 static int __init
prevent_bootmem_remove_init(void)
1489 if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE
))
1492 validate_bootmem_online();
1493 ret
= register_memory_notifier(&prevent_bootmem_remove_nb
);
1495 pr_err("%s: Notifier registration failed %d\n", __func__
, ret
);
1499 early_initcall(prevent_bootmem_remove_init
);
1502 pte_t
ptep_modify_prot_start(struct vm_area_struct
*vma
, unsigned long addr
, pte_t
*ptep
)
1504 if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198
)) {
1506 * Break-before-make (BBM) is required for all user space mappings
1507 * when the permission changes from executable to non-executable
1508 * in cases where cpu is affected with errata #2645198.
1510 if (pte_user_exec(ptep_get(ptep
)))
1511 return ptep_clear_flush(vma
, addr
, ptep
);
1513 return ptep_get_and_clear(vma
->vm_mm
, addr
, ptep
);
1516 void ptep_modify_prot_commit(struct vm_area_struct
*vma
, unsigned long addr
, pte_t
*ptep
,
1517 pte_t old_pte
, pte_t pte
)
1519 set_pte_at(vma
->vm_mm
, addr
, ptep
, pte
);
1523 * Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD,
1524 * avoiding the possibility of conflicting TLB entries being allocated.
1526 void __cpu_replace_ttbr1(pgd_t
*pgdp
, bool cnp
)
1528 typedef void (ttbr_replace_func
)(phys_addr_t
);
1529 extern ttbr_replace_func idmap_cpu_replace_ttbr1
;
1530 ttbr_replace_func
*replace_phys
;
1533 /* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */
1534 phys_addr_t ttbr1
= phys_to_ttbr(virt_to_phys(pgdp
));
1537 ttbr1
|= TTBR_CNP_BIT
;
1539 replace_phys
= (void *)__pa_symbol(idmap_cpu_replace_ttbr1
);
1541 cpu_install_idmap();
1544 * We really don't want to take *any* exceptions while TTBR1 is
1545 * in the process of being replaced so mask everything.
1547 daif
= local_daif_save();
1548 replace_phys(ttbr1
);
1549 local_daif_restore(daif
);
1551 cpu_uninstall_idmap();
1554 #ifdef CONFIG_ARCH_HAS_PKEYS
1555 int arch_set_user_pkey_access(struct task_struct
*tsk
, int pkey
, unsigned long init_val
)
1557 u64 new_por
= POE_RXW
;
1561 if (!system_supports_poe())
1565 * This code should only be called with valid 'pkey'
1566 * values originating from in-kernel users. Complain
1567 * if a bad value is observed.
1569 if (WARN_ON_ONCE(pkey
>= arch_max_pkey()))
1572 /* Set the bits we need in POR: */
1574 if (init_val
& PKEY_DISABLE_WRITE
)
1576 if (init_val
& PKEY_DISABLE_ACCESS
)
1578 if (init_val
& PKEY_DISABLE_READ
)
1580 if (init_val
& PKEY_DISABLE_EXECUTE
)
1583 /* Shift the bits in to the correct place in POR for pkey: */
1584 pkey_shift
= pkey
* POR_BITS_PER_PKEY
;
1585 new_por
<<= pkey_shift
;
1587 /* Get old POR and mask off any old bits in place: */
1588 old_por
= read_sysreg_s(SYS_POR_EL0
);
1589 old_por
&= ~(POE_MASK
<< pkey_shift
);
1591 /* Write old part along with new part: */
1592 write_sysreg_s(old_por
| new_por
, SYS_POR_EL0
);