1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Regents of the University of California
4 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
5 * Copyright (C) 2020 FORTH-ICS/CARV
6 * Nick Kossifidis <mick@ics.forth.gr>
9 #include <linux/init.h>
11 #include <linux/memblock.h>
12 #include <linux/initrd.h>
13 #include <linux/swap.h>
14 #include <linux/swiotlb.h>
15 #include <linux/sizes.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_reserved_mem.h>
18 #include <linux/libfdt.h>
19 #include <linux/set_memory.h>
20 #include <linux/dma-map-ops.h>
21 #include <linux/crash_dump.h>
22 #include <linux/hugetlb.h>
23 #ifdef CONFIG_RELOCATABLE
24 #include <linux/elf.h>
26 #include <linux/kfence.h>
27 #include <linux/execmem.h>
29 #include <asm/fixmap.h>
31 #include <asm/kasan.h>
33 #include <asm/pgtable.h>
34 #include <asm/sections.h>
36 #include <asm/tlbflush.h>
38 #include "../kernel/head.h"
40 u64 new_vmalloc
[NR_CPUS
/ sizeof(u64
) + 1];
42 struct kernel_mapping kernel_map __ro_after_init
;
43 EXPORT_SYMBOL(kernel_map
);
44 #ifdef CONFIG_XIP_KERNEL
45 #define kernel_map (*(struct kernel_mapping *)XIP_FIXUP(&kernel_map))
49 u64 satp_mode __ro_after_init
= !IS_ENABLED(CONFIG_XIP_KERNEL
) ? SATP_MODE_57
: SATP_MODE_39
;
51 u64 satp_mode __ro_after_init
= SATP_MODE_32
;
53 EXPORT_SYMBOL(satp_mode
);
56 bool pgtable_l4_enabled __ro_after_init
= !IS_ENABLED(CONFIG_XIP_KERNEL
);
57 bool pgtable_l5_enabled __ro_after_init
= !IS_ENABLED(CONFIG_XIP_KERNEL
);
58 EXPORT_SYMBOL(pgtable_l4_enabled
);
59 EXPORT_SYMBOL(pgtable_l5_enabled
);
62 phys_addr_t phys_ram_base __ro_after_init
;
63 EXPORT_SYMBOL(phys_ram_base
);
65 unsigned long empty_zero_page
[PAGE_SIZE
/ sizeof(unsigned long)]
67 EXPORT_SYMBOL(empty_zero_page
);
70 void *_dtb_early_va __initdata
;
71 uintptr_t _dtb_early_pa __initdata
;
73 phys_addr_t dma32_phys_limit __initdata
;
75 static void __init
zone_sizes_init(void)
77 unsigned long max_zone_pfns
[MAX_NR_ZONES
] = { 0, };
79 #ifdef CONFIG_ZONE_DMA32
80 max_zone_pfns
[ZONE_DMA32
] = PFN_DOWN(dma32_phys_limit
);
82 max_zone_pfns
[ZONE_NORMAL
] = max_low_pfn
;
84 free_area_init(max_zone_pfns
);
87 #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
89 #define LOG2_SZ_1K ilog2(SZ_1K)
90 #define LOG2_SZ_1M ilog2(SZ_1M)
91 #define LOG2_SZ_1G ilog2(SZ_1G)
92 #define LOG2_SZ_1T ilog2(SZ_1T)
94 static inline void print_mlk(char *name
, unsigned long b
, unsigned long t
)
96 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name
, b
, t
,
97 (((t
) - (b
)) >> LOG2_SZ_1K
));
100 static inline void print_mlm(char *name
, unsigned long b
, unsigned long t
)
102 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name
, b
, t
,
103 (((t
) - (b
)) >> LOG2_SZ_1M
));
106 static inline void print_mlg(char *name
, unsigned long b
, unsigned long t
)
108 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld GB)\n", name
, b
, t
,
109 (((t
) - (b
)) >> LOG2_SZ_1G
));
113 static inline void print_mlt(char *name
, unsigned long b
, unsigned long t
)
115 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld TB)\n", name
, b
, t
,
116 (((t
) - (b
)) >> LOG2_SZ_1T
));
119 #define print_mlt(n, b, t) do {} while (0)
122 static inline void print_ml(char *name
, unsigned long b
, unsigned long t
)
124 unsigned long diff
= t
- b
;
126 if (IS_ENABLED(CONFIG_64BIT
) && (diff
>> LOG2_SZ_1T
) >= 10)
127 print_mlt(name
, b
, t
);
128 else if ((diff
>> LOG2_SZ_1G
) >= 10)
129 print_mlg(name
, b
, t
);
130 else if ((diff
>> LOG2_SZ_1M
) >= 10)
131 print_mlm(name
, b
, t
);
133 print_mlk(name
, b
, t
);
136 static void __init
print_vm_layout(void)
138 pr_notice("Virtual kernel memory layout:\n");
139 print_ml("fixmap", (unsigned long)FIXADDR_START
,
140 (unsigned long)FIXADDR_TOP
);
141 print_ml("pci io", (unsigned long)PCI_IO_START
,
142 (unsigned long)PCI_IO_END
);
143 print_ml("vmemmap", (unsigned long)VMEMMAP_START
,
144 (unsigned long)VMEMMAP_END
);
145 print_ml("vmalloc", (unsigned long)VMALLOC_START
,
146 (unsigned long)VMALLOC_END
);
148 print_ml("modules", (unsigned long)MODULES_VADDR
,
149 (unsigned long)MODULES_END
);
151 print_ml("lowmem", (unsigned long)PAGE_OFFSET
,
152 (unsigned long)high_memory
);
153 if (IS_ENABLED(CONFIG_64BIT
)) {
155 print_ml("kasan", KASAN_SHADOW_START
, KASAN_SHADOW_END
);
158 print_ml("kernel", (unsigned long)kernel_map
.virt_addr
,
159 (unsigned long)ADDRESS_SPACE_END
);
163 static void print_vm_layout(void) { }
164 #endif /* CONFIG_DEBUG_VM */
166 void __init
mem_init(void)
168 bool swiotlb
= max_pfn
> PFN_DOWN(dma32_phys_limit
);
169 #ifdef CONFIG_FLATMEM
171 #endif /* CONFIG_FLATMEM */
173 if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC
) && !swiotlb
&&
174 dma_cache_alignment
!= 1) {
176 * If no bouncing needed for ZONE_DMA, allocate 1MB swiotlb
177 * buffer per 1GB of RAM for kmalloc() bouncing on
178 * non-coherent platforms.
181 DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
182 swiotlb_adjust_size(min(swiotlb_size_or_default(), size
));
186 swiotlb_init(swiotlb
, SWIOTLB_VERBOSE
);
192 /* Limit the memory size via mem. */
193 static phys_addr_t memory_limit
;
194 #ifdef CONFIG_XIP_KERNEL
195 #define memory_limit (*(phys_addr_t *)XIP_FIXUP(&memory_limit))
196 #endif /* CONFIG_XIP_KERNEL */
198 static int __init
early_mem(char *p
)
205 size
= memparse(p
, &p
) & PAGE_MASK
;
206 memory_limit
= min_t(u64
, size
, memory_limit
);
208 pr_notice("Memory limited to %lldMB\n", (u64
)memory_limit
>> 20);
212 early_param("mem", early_mem
);
214 static void __init
setup_bootmem(void)
216 phys_addr_t vmlinux_end
= __pa_symbol(&_end
);
217 phys_addr_t max_mapped_addr
;
218 phys_addr_t phys_ram_end
, vmlinux_start
;
220 if (IS_ENABLED(CONFIG_XIP_KERNEL
))
221 vmlinux_start
= __pa_symbol(&_sdata
);
223 vmlinux_start
= __pa_symbol(&_start
);
225 memblock_enforce_memory_limit(memory_limit
);
228 * Make sure we align the reservation on PMD_SIZE since we will
229 * map the kernel in the linear mapping as read-only: we do not want
230 * any allocation to happen between _end and the next pmd aligned page.
232 if (IS_ENABLED(CONFIG_64BIT
) && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX
))
233 vmlinux_end
= (vmlinux_end
+ PMD_SIZE
- 1) & PMD_MASK
;
235 * Reserve from the start of the kernel to the end of the kernel
237 memblock_reserve(vmlinux_start
, vmlinux_end
- vmlinux_start
);
240 * Make sure we align the start of the memory on a PMD boundary so that
241 * at worst, we map the linear mapping with PMD mappings.
243 if (!IS_ENABLED(CONFIG_XIP_KERNEL
))
244 phys_ram_base
= memblock_start_of_DRAM() & PMD_MASK
;
247 * In 64-bit, any use of __va/__pa before this point is wrong as we
248 * did not know the start of DRAM before.
250 if (IS_ENABLED(CONFIG_64BIT
) && IS_ENABLED(CONFIG_MMU
))
251 kernel_map
.va_pa_offset
= PAGE_OFFSET
- phys_ram_base
;
254 * The size of the linear page mapping may restrict the amount of
257 if (IS_ENABLED(CONFIG_64BIT
) && IS_ENABLED(CONFIG_MMU
)) {
258 max_mapped_addr
= __pa(PAGE_OFFSET
) + KERN_VIRT_SIZE
;
259 memblock_cap_memory_range(phys_ram_base
,
260 max_mapped_addr
- phys_ram_base
);
264 * Reserve physical address space that would be mapped to virtual
265 * addresses greater than (void *)(-PAGE_SIZE) because:
266 * - This memory would overlap with ERR_PTR
267 * - This memory belongs to high memory, which is not supported
269 * This is not applicable to 64-bit kernel, because virtual addresses
270 * after (void *)(-PAGE_SIZE) are not linearly mapped: they are
271 * occupied by kernel mapping. Also it is unrealistic for high memory
272 * to exist on 64-bit platforms.
274 if (!IS_ENABLED(CONFIG_64BIT
)) {
275 max_mapped_addr
= __va_to_pa_nodebug(-PAGE_SIZE
);
276 memblock_reserve(max_mapped_addr
, (phys_addr_t
)-max_mapped_addr
);
279 phys_ram_end
= memblock_end_of_DRAM();
280 min_low_pfn
= PFN_UP(phys_ram_base
);
281 max_low_pfn
= max_pfn
= PFN_DOWN(phys_ram_end
);
282 high_memory
= (void *)(__va(PFN_PHYS(max_low_pfn
)));
284 dma32_phys_limit
= min(4UL * SZ_1G
, (unsigned long)PFN_PHYS(max_low_pfn
));
285 set_max_mapnr(max_low_pfn
- ARCH_PFN_OFFSET
);
287 reserve_initrd_mem();
290 * No allocation should be done before reserving the memory as defined
291 * in the device tree, otherwise the allocation could end up in a
294 early_init_fdt_scan_reserved_mem();
297 * If DTB is built in, no need to reserve its memblock.
298 * Otherwise, do reserve it but avoid using
299 * early_init_fdt_reserve_self() since __pa() does
300 * not work for DTB pointers that are fixmap addresses
302 if (!IS_ENABLED(CONFIG_BUILTIN_DTB
))
303 memblock_reserve(dtb_early_pa
, fdt_totalsize(dtb_early_va
));
305 dma_contiguous_reserve(dma32_phys_limit
);
306 if (IS_ENABLED(CONFIG_64BIT
))
307 hugetlb_cma_reserve(PUD_SHIFT
- PAGE_SHIFT
);
311 struct pt_alloc_ops pt_ops __meminitdata
;
313 pgd_t swapper_pg_dir
[PTRS_PER_PGD
] __page_aligned_bss
;
314 pgd_t trampoline_pg_dir
[PTRS_PER_PGD
] __page_aligned_bss
;
315 static pte_t fixmap_pte
[PTRS_PER_PTE
] __page_aligned_bss
;
317 pgd_t early_pg_dir
[PTRS_PER_PGD
] __initdata
__aligned(PAGE_SIZE
);
319 #ifdef CONFIG_XIP_KERNEL
320 #define pt_ops (*(struct pt_alloc_ops *)XIP_FIXUP(&pt_ops))
321 #define trampoline_pg_dir ((pgd_t *)XIP_FIXUP(trampoline_pg_dir))
322 #define fixmap_pte ((pte_t *)XIP_FIXUP(fixmap_pte))
323 #define early_pg_dir ((pgd_t *)XIP_FIXUP(early_pg_dir))
324 #endif /* CONFIG_XIP_KERNEL */
326 static const pgprot_t protection_map
[16] = {
327 [VM_NONE
] = PAGE_NONE
,
328 [VM_READ
] = PAGE_READ
,
329 [VM_WRITE
] = PAGE_COPY
,
330 [VM_WRITE
| VM_READ
] = PAGE_COPY
,
331 [VM_EXEC
] = PAGE_EXEC
,
332 [VM_EXEC
| VM_READ
] = PAGE_READ_EXEC
,
333 [VM_EXEC
| VM_WRITE
] = PAGE_COPY_EXEC
,
334 [VM_EXEC
| VM_WRITE
| VM_READ
] = PAGE_COPY_EXEC
,
335 [VM_SHARED
] = PAGE_NONE
,
336 [VM_SHARED
| VM_READ
] = PAGE_READ
,
337 [VM_SHARED
| VM_WRITE
] = PAGE_SHARED
,
338 [VM_SHARED
| VM_WRITE
| VM_READ
] = PAGE_SHARED
,
339 [VM_SHARED
| VM_EXEC
] = PAGE_EXEC
,
340 [VM_SHARED
| VM_EXEC
| VM_READ
] = PAGE_READ_EXEC
,
341 [VM_SHARED
| VM_EXEC
| VM_WRITE
] = PAGE_SHARED_EXEC
,
342 [VM_SHARED
| VM_EXEC
| VM_WRITE
| VM_READ
] = PAGE_SHARED_EXEC
344 DECLARE_VM_GET_PAGE_PROT
346 void __set_fixmap(enum fixed_addresses idx
, phys_addr_t phys
, pgprot_t prot
)
348 unsigned long addr
= __fix_to_virt(idx
);
351 BUG_ON(idx
<= FIX_HOLE
|| idx
>= __end_of_fixed_addresses
);
353 ptep
= &fixmap_pte
[pte_index(addr
)];
355 if (pgprot_val(prot
))
356 set_pte(ptep
, pfn_pte(phys
>> PAGE_SHIFT
, prot
));
358 pte_clear(&init_mm
, addr
, ptep
);
359 local_flush_tlb_page(addr
);
362 static inline pte_t
*__init
get_pte_virt_early(phys_addr_t pa
)
364 return (pte_t
*)((uintptr_t)pa
);
367 static inline pte_t
*__init
get_pte_virt_fixmap(phys_addr_t pa
)
369 clear_fixmap(FIX_PTE
);
370 return (pte_t
*)set_fixmap_offset(FIX_PTE
, pa
);
373 static inline pte_t
*__meminit
get_pte_virt_late(phys_addr_t pa
)
375 return (pte_t
*) __va(pa
);
378 static inline phys_addr_t __init
alloc_pte_early(uintptr_t va
)
381 * We only create PMD or PGD early mappings so we
382 * should never reach here with MMU disabled.
387 static inline phys_addr_t __init
alloc_pte_fixmap(uintptr_t va
)
389 return memblock_phys_alloc(PAGE_SIZE
, PAGE_SIZE
);
392 static phys_addr_t __meminit
alloc_pte_late(uintptr_t va
)
394 struct ptdesc
*ptdesc
= pagetable_alloc(GFP_KERNEL
& ~__GFP_HIGHMEM
, 0);
396 BUG_ON(!ptdesc
|| !pagetable_pte_ctor(ptdesc
));
397 return __pa((pte_t
*)ptdesc_address(ptdesc
));
400 static void __meminit
create_pte_mapping(pte_t
*ptep
, uintptr_t va
, phys_addr_t pa
, phys_addr_t sz
,
403 uintptr_t pte_idx
= pte_index(va
);
405 BUG_ON(sz
!= PAGE_SIZE
);
407 if (pte_none(ptep
[pte_idx
]))
408 ptep
[pte_idx
] = pfn_pte(PFN_DOWN(pa
), prot
);
411 #ifndef __PAGETABLE_PMD_FOLDED
413 static pmd_t trampoline_pmd
[PTRS_PER_PMD
] __page_aligned_bss
;
414 static pmd_t fixmap_pmd
[PTRS_PER_PMD
] __page_aligned_bss
;
415 static pmd_t early_pmd
[PTRS_PER_PMD
] __initdata
__aligned(PAGE_SIZE
);
417 #ifdef CONFIG_XIP_KERNEL
418 #define trampoline_pmd ((pmd_t *)XIP_FIXUP(trampoline_pmd))
419 #define fixmap_pmd ((pmd_t *)XIP_FIXUP(fixmap_pmd))
420 #define early_pmd ((pmd_t *)XIP_FIXUP(early_pmd))
421 #endif /* CONFIG_XIP_KERNEL */
423 static p4d_t trampoline_p4d
[PTRS_PER_P4D
] __page_aligned_bss
;
424 static p4d_t fixmap_p4d
[PTRS_PER_P4D
] __page_aligned_bss
;
425 static p4d_t early_p4d
[PTRS_PER_P4D
] __initdata
__aligned(PAGE_SIZE
);
427 #ifdef CONFIG_XIP_KERNEL
428 #define trampoline_p4d ((p4d_t *)XIP_FIXUP(trampoline_p4d))
429 #define fixmap_p4d ((p4d_t *)XIP_FIXUP(fixmap_p4d))
430 #define early_p4d ((p4d_t *)XIP_FIXUP(early_p4d))
431 #endif /* CONFIG_XIP_KERNEL */
433 static pud_t trampoline_pud
[PTRS_PER_PUD
] __page_aligned_bss
;
434 static pud_t fixmap_pud
[PTRS_PER_PUD
] __page_aligned_bss
;
435 static pud_t early_pud
[PTRS_PER_PUD
] __initdata
__aligned(PAGE_SIZE
);
437 #ifdef CONFIG_XIP_KERNEL
438 #define trampoline_pud ((pud_t *)XIP_FIXUP(trampoline_pud))
439 #define fixmap_pud ((pud_t *)XIP_FIXUP(fixmap_pud))
440 #define early_pud ((pud_t *)XIP_FIXUP(early_pud))
441 #endif /* CONFIG_XIP_KERNEL */
443 static pmd_t
*__init
get_pmd_virt_early(phys_addr_t pa
)
445 /* Before MMU is enabled */
446 return (pmd_t
*)((uintptr_t)pa
);
449 static pmd_t
*__init
get_pmd_virt_fixmap(phys_addr_t pa
)
451 clear_fixmap(FIX_PMD
);
452 return (pmd_t
*)set_fixmap_offset(FIX_PMD
, pa
);
455 static pmd_t
*__meminit
get_pmd_virt_late(phys_addr_t pa
)
457 return (pmd_t
*) __va(pa
);
460 static phys_addr_t __init
alloc_pmd_early(uintptr_t va
)
462 BUG_ON((va
- kernel_map
.virt_addr
) >> PUD_SHIFT
);
464 return (uintptr_t)early_pmd
;
467 static phys_addr_t __init
alloc_pmd_fixmap(uintptr_t va
)
469 return memblock_phys_alloc(PAGE_SIZE
, PAGE_SIZE
);
472 static phys_addr_t __meminit
alloc_pmd_late(uintptr_t va
)
474 struct ptdesc
*ptdesc
= pagetable_alloc(GFP_KERNEL
& ~__GFP_HIGHMEM
, 0);
476 BUG_ON(!ptdesc
|| !pagetable_pmd_ctor(ptdesc
));
477 return __pa((pmd_t
*)ptdesc_address(ptdesc
));
480 static void __meminit
create_pmd_mapping(pmd_t
*pmdp
,
481 uintptr_t va
, phys_addr_t pa
,
482 phys_addr_t sz
, pgprot_t prot
)
485 phys_addr_t pte_phys
;
486 uintptr_t pmd_idx
= pmd_index(va
);
488 if (sz
== PMD_SIZE
) {
489 if (pmd_none(pmdp
[pmd_idx
]))
490 pmdp
[pmd_idx
] = pfn_pmd(PFN_DOWN(pa
), prot
);
494 if (pmd_none(pmdp
[pmd_idx
])) {
495 pte_phys
= pt_ops
.alloc_pte(va
);
496 pmdp
[pmd_idx
] = pfn_pmd(PFN_DOWN(pte_phys
), PAGE_TABLE
);
497 ptep
= pt_ops
.get_pte_virt(pte_phys
);
498 memset(ptep
, 0, PAGE_SIZE
);
500 pte_phys
= PFN_PHYS(_pmd_pfn(pmdp
[pmd_idx
]));
501 ptep
= pt_ops
.get_pte_virt(pte_phys
);
504 create_pte_mapping(ptep
, va
, pa
, sz
, prot
);
507 static pud_t
*__init
get_pud_virt_early(phys_addr_t pa
)
509 return (pud_t
*)((uintptr_t)pa
);
512 static pud_t
*__init
get_pud_virt_fixmap(phys_addr_t pa
)
514 clear_fixmap(FIX_PUD
);
515 return (pud_t
*)set_fixmap_offset(FIX_PUD
, pa
);
518 static pud_t
*__meminit
get_pud_virt_late(phys_addr_t pa
)
520 return (pud_t
*)__va(pa
);
523 static phys_addr_t __init
alloc_pud_early(uintptr_t va
)
525 /* Only one PUD is available for early mapping */
526 BUG_ON((va
- kernel_map
.virt_addr
) >> PGDIR_SHIFT
);
528 return (uintptr_t)early_pud
;
531 static phys_addr_t __init
alloc_pud_fixmap(uintptr_t va
)
533 return memblock_phys_alloc(PAGE_SIZE
, PAGE_SIZE
);
536 static phys_addr_t __meminit
alloc_pud_late(uintptr_t va
)
540 vaddr
= __get_free_page(GFP_KERNEL
);
545 static p4d_t
*__init
get_p4d_virt_early(phys_addr_t pa
)
547 return (p4d_t
*)((uintptr_t)pa
);
550 static p4d_t
*__init
get_p4d_virt_fixmap(phys_addr_t pa
)
552 clear_fixmap(FIX_P4D
);
553 return (p4d_t
*)set_fixmap_offset(FIX_P4D
, pa
);
556 static p4d_t
*__meminit
get_p4d_virt_late(phys_addr_t pa
)
558 return (p4d_t
*)__va(pa
);
561 static phys_addr_t __init
alloc_p4d_early(uintptr_t va
)
563 /* Only one P4D is available for early mapping */
564 BUG_ON((va
- kernel_map
.virt_addr
) >> PGDIR_SHIFT
);
566 return (uintptr_t)early_p4d
;
569 static phys_addr_t __init
alloc_p4d_fixmap(uintptr_t va
)
571 return memblock_phys_alloc(PAGE_SIZE
, PAGE_SIZE
);
574 static phys_addr_t __meminit
alloc_p4d_late(uintptr_t va
)
578 vaddr
= __get_free_page(GFP_KERNEL
);
583 static void __meminit
create_pud_mapping(pud_t
*pudp
, uintptr_t va
, phys_addr_t pa
, phys_addr_t sz
,
587 phys_addr_t next_phys
;
588 uintptr_t pud_index
= pud_index(va
);
590 if (sz
== PUD_SIZE
) {
591 if (pud_val(pudp
[pud_index
]) == 0)
592 pudp
[pud_index
] = pfn_pud(PFN_DOWN(pa
), prot
);
596 if (pud_val(pudp
[pud_index
]) == 0) {
597 next_phys
= pt_ops
.alloc_pmd(va
);
598 pudp
[pud_index
] = pfn_pud(PFN_DOWN(next_phys
), PAGE_TABLE
);
599 nextp
= pt_ops
.get_pmd_virt(next_phys
);
600 memset(nextp
, 0, PAGE_SIZE
);
602 next_phys
= PFN_PHYS(_pud_pfn(pudp
[pud_index
]));
603 nextp
= pt_ops
.get_pmd_virt(next_phys
);
606 create_pmd_mapping(nextp
, va
, pa
, sz
, prot
);
609 static void __meminit
create_p4d_mapping(p4d_t
*p4dp
, uintptr_t va
, phys_addr_t pa
, phys_addr_t sz
,
613 phys_addr_t next_phys
;
614 uintptr_t p4d_index
= p4d_index(va
);
616 if (sz
== P4D_SIZE
) {
617 if (p4d_val(p4dp
[p4d_index
]) == 0)
618 p4dp
[p4d_index
] = pfn_p4d(PFN_DOWN(pa
), prot
);
622 if (p4d_val(p4dp
[p4d_index
]) == 0) {
623 next_phys
= pt_ops
.alloc_pud(va
);
624 p4dp
[p4d_index
] = pfn_p4d(PFN_DOWN(next_phys
), PAGE_TABLE
);
625 nextp
= pt_ops
.get_pud_virt(next_phys
);
626 memset(nextp
, 0, PAGE_SIZE
);
628 next_phys
= PFN_PHYS(_p4d_pfn(p4dp
[p4d_index
]));
629 nextp
= pt_ops
.get_pud_virt(next_phys
);
632 create_pud_mapping(nextp
, va
, pa
, sz
, prot
);
635 #define pgd_next_t p4d_t
636 #define alloc_pgd_next(__va) (pgtable_l5_enabled ? \
637 pt_ops.alloc_p4d(__va) : (pgtable_l4_enabled ? \
638 pt_ops.alloc_pud(__va) : pt_ops.alloc_pmd(__va)))
639 #define get_pgd_next_virt(__pa) (pgtable_l5_enabled ? \
640 pt_ops.get_p4d_virt(__pa) : (pgd_next_t *)(pgtable_l4_enabled ? \
641 pt_ops.get_pud_virt(__pa) : (pud_t *)pt_ops.get_pmd_virt(__pa)))
642 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
643 (pgtable_l5_enabled ? \
644 create_p4d_mapping(__nextp, __va, __pa, __sz, __prot) : \
645 (pgtable_l4_enabled ? \
646 create_pud_mapping((pud_t *)__nextp, __va, __pa, __sz, __prot) : \
647 create_pmd_mapping((pmd_t *)__nextp, __va, __pa, __sz, __prot)))
648 #define fixmap_pgd_next (pgtable_l5_enabled ? \
649 (uintptr_t)fixmap_p4d : (pgtable_l4_enabled ? \
650 (uintptr_t)fixmap_pud : (uintptr_t)fixmap_pmd))
651 #define trampoline_pgd_next (pgtable_l5_enabled ? \
652 (uintptr_t)trampoline_p4d : (pgtable_l4_enabled ? \
653 (uintptr_t)trampoline_pud : (uintptr_t)trampoline_pmd))
655 #define pgd_next_t pte_t
656 #define alloc_pgd_next(__va) pt_ops.alloc_pte(__va)
657 #define get_pgd_next_virt(__pa) pt_ops.get_pte_virt(__pa)
658 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
659 create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
660 #define fixmap_pgd_next ((uintptr_t)fixmap_pte)
661 #define create_p4d_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
662 #define create_pud_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
663 #define create_pmd_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
664 #endif /* __PAGETABLE_PMD_FOLDED */
666 void __meminit
create_pgd_mapping(pgd_t
*pgdp
, uintptr_t va
, phys_addr_t pa
, phys_addr_t sz
,
670 phys_addr_t next_phys
;
671 uintptr_t pgd_idx
= pgd_index(va
);
673 if (sz
== PGDIR_SIZE
) {
674 if (pgd_val(pgdp
[pgd_idx
]) == 0)
675 pgdp
[pgd_idx
] = pfn_pgd(PFN_DOWN(pa
), prot
);
679 if (pgd_val(pgdp
[pgd_idx
]) == 0) {
680 next_phys
= alloc_pgd_next(va
);
681 pgdp
[pgd_idx
] = pfn_pgd(PFN_DOWN(next_phys
), PAGE_TABLE
);
682 nextp
= get_pgd_next_virt(next_phys
);
683 memset(nextp
, 0, PAGE_SIZE
);
685 next_phys
= PFN_PHYS(_pgd_pfn(pgdp
[pgd_idx
]));
686 nextp
= get_pgd_next_virt(next_phys
);
689 create_pgd_next_mapping(nextp
, va
, pa
, sz
, prot
);
692 static uintptr_t __meminit
best_map_size(phys_addr_t pa
, uintptr_t va
, phys_addr_t size
)
694 if (debug_pagealloc_enabled())
697 if (pgtable_l5_enabled
&&
698 !(pa
& (P4D_SIZE
- 1)) && !(va
& (P4D_SIZE
- 1)) && size
>= P4D_SIZE
)
701 if (pgtable_l4_enabled
&&
702 !(pa
& (PUD_SIZE
- 1)) && !(va
& (PUD_SIZE
- 1)) && size
>= PUD_SIZE
)
705 if (IS_ENABLED(CONFIG_64BIT
) &&
706 !(pa
& (PMD_SIZE
- 1)) && !(va
& (PMD_SIZE
- 1)) && size
>= PMD_SIZE
)
712 #ifdef CONFIG_XIP_KERNEL
713 #define phys_ram_base (*(phys_addr_t *)XIP_FIXUP(&phys_ram_base))
714 extern char _xiprom
[], _exiprom
[], __data_loc
;
716 /* called from head.S with MMU off */
717 asmlinkage
void __init
__copy_data(void)
719 void *from
= (void *)(&__data_loc
);
720 void *to
= (void *)CONFIG_PHYS_RAM_BASE
;
721 size_t sz
= (size_t)((uintptr_t)(&_end
) - (uintptr_t)(&_sdata
));
723 memcpy(to
, from
, sz
);
727 #ifdef CONFIG_STRICT_KERNEL_RWX
728 static __meminit pgprot_t
pgprot_from_va(uintptr_t va
)
730 if (is_va_kernel_text(va
))
731 return PAGE_KERNEL_READ_EXEC
;
734 * In 64-bit kernel, the kernel mapping is outside the linear mapping so
735 * we must protect its linear mapping alias from being executed and
737 * And rodata section is marked readonly in mark_rodata_ro.
739 if (IS_ENABLED(CONFIG_64BIT
) && is_va_kernel_lm_alias_text(va
))
740 return PAGE_KERNEL_READ
;
745 void mark_rodata_ro(void)
747 set_kernel_memory(__start_rodata
, _data
, set_memory_ro
);
748 if (IS_ENABLED(CONFIG_64BIT
))
749 set_kernel_memory(lm_alias(__start_rodata
), lm_alias(_data
),
753 static __meminit pgprot_t
pgprot_from_va(uintptr_t va
)
755 if (IS_ENABLED(CONFIG_64BIT
) && !is_kernel_mapping(va
))
758 return PAGE_KERNEL_EXEC
;
760 #endif /* CONFIG_STRICT_KERNEL_RWX */
762 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
763 u64
__pi_set_satp_mode_from_cmdline(uintptr_t dtb_pa
);
765 static void __init
disable_pgtable_l5(void)
767 pgtable_l5_enabled
= false;
768 kernel_map
.page_offset
= PAGE_OFFSET_L4
;
769 satp_mode
= SATP_MODE_48
;
772 static void __init
disable_pgtable_l4(void)
774 pgtable_l4_enabled
= false;
775 kernel_map
.page_offset
= PAGE_OFFSET_L3
;
776 satp_mode
= SATP_MODE_39
;
779 static int __init
print_no4lvl(char *p
)
781 pr_info("Disabled 4-level and 5-level paging");
784 early_param("no4lvl", print_no4lvl
);
786 static int __init
print_no5lvl(char *p
)
788 pr_info("Disabled 5-level paging");
791 early_param("no5lvl", print_no5lvl
);
793 static void __init
set_mmap_rnd_bits_max(void)
795 mmap_rnd_bits_max
= MMAP_VA_BITS
- PAGE_SHIFT
- 3;
799 * There is a simple way to determine if 4-level is supported by the
800 * underlying hardware: establish 1:1 mapping in 4-level page table mode
801 * then read SATP to see if the configuration was taken into account
802 * meaning sv48 is supported.
804 static __init
void set_satp_mode(uintptr_t dtb_pa
)
806 u64 identity_satp
, hw_satp
;
807 uintptr_t set_satp_mode_pmd
= ((unsigned long)set_satp_mode
) & PMD_MASK
;
808 u64 satp_mode_cmdline
= __pi_set_satp_mode_from_cmdline(dtb_pa
);
810 if (satp_mode_cmdline
== SATP_MODE_57
) {
811 disable_pgtable_l5();
812 } else if (satp_mode_cmdline
== SATP_MODE_48
) {
813 disable_pgtable_l5();
814 disable_pgtable_l4();
818 create_p4d_mapping(early_p4d
,
819 set_satp_mode_pmd
, (uintptr_t)early_pud
,
820 P4D_SIZE
, PAGE_TABLE
);
821 create_pud_mapping(early_pud
,
822 set_satp_mode_pmd
, (uintptr_t)early_pmd
,
823 PUD_SIZE
, PAGE_TABLE
);
824 /* Handle the case where set_satp_mode straddles 2 PMDs */
825 create_pmd_mapping(early_pmd
,
826 set_satp_mode_pmd
, set_satp_mode_pmd
,
827 PMD_SIZE
, PAGE_KERNEL_EXEC
);
828 create_pmd_mapping(early_pmd
,
829 set_satp_mode_pmd
+ PMD_SIZE
,
830 set_satp_mode_pmd
+ PMD_SIZE
,
831 PMD_SIZE
, PAGE_KERNEL_EXEC
);
833 create_pgd_mapping(early_pg_dir
,
836 (uintptr_t)early_p4d
: (uintptr_t)early_pud
,
837 PGDIR_SIZE
, PAGE_TABLE
);
839 identity_satp
= PFN_DOWN((uintptr_t)&early_pg_dir
) | satp_mode
;
841 local_flush_tlb_all();
842 csr_write(CSR_SATP
, identity_satp
);
843 hw_satp
= csr_swap(CSR_SATP
, 0ULL);
844 local_flush_tlb_all();
846 if (hw_satp
!= identity_satp
) {
847 if (pgtable_l5_enabled
) {
848 disable_pgtable_l5();
849 memset(early_pg_dir
, 0, PAGE_SIZE
);
852 disable_pgtable_l4();
855 memset(early_pg_dir
, 0, PAGE_SIZE
);
856 memset(early_p4d
, 0, PAGE_SIZE
);
857 memset(early_pud
, 0, PAGE_SIZE
);
858 memset(early_pmd
, 0, PAGE_SIZE
);
863 * setup_vm() is called from head.S with MMU-off.
865 * Following requirements should be honoured for setup_vm() to work
867 * 1) It should use PC-relative addressing for accessing kernel symbols.
868 * To achieve this we always use GCC cmodel=medany.
869 * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
870 * so disable compiler instrumentation when FTRACE is enabled.
872 * Currently, the above requirements are honoured by using custom CFLAGS
873 * for init.o in mm/Makefile.
876 #ifndef __riscv_cmodel_medany
877 #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
880 #ifdef CONFIG_RELOCATABLE
881 extern unsigned long __rela_dyn_start
, __rela_dyn_end
;
883 static void __init
relocate_kernel(void)
885 Elf64_Rela
*rela
= (Elf64_Rela
*)&__rela_dyn_start
;
887 * This holds the offset between the linked virtual address and the
888 * relocated virtual address.
890 uintptr_t reloc_offset
= kernel_map
.virt_addr
- KERNEL_LINK_ADDR
;
892 * This holds the offset between kernel linked virtual address and
895 uintptr_t va_kernel_link_pa_offset
= KERNEL_LINK_ADDR
- kernel_map
.phys_addr
;
897 for ( ; rela
< (Elf64_Rela
*)&__rela_dyn_end
; rela
++) {
898 Elf64_Addr addr
= (rela
->r_offset
- va_kernel_link_pa_offset
);
899 Elf64_Addr relocated_addr
= rela
->r_addend
;
901 if (rela
->r_info
!= R_RISCV_RELATIVE
)
905 * Make sure to not relocate vdso symbols like rt_sigreturn
906 * which are linked from the address 0 in vmlinux since
907 * vdso symbol addresses are actually used as an offset from
908 * mm->context.vdso in VDSO_OFFSET macro.
910 if (relocated_addr
>= KERNEL_LINK_ADDR
)
911 relocated_addr
+= reloc_offset
;
913 *(Elf64_Addr
*)addr
= relocated_addr
;
916 #endif /* CONFIG_RELOCATABLE */
918 #ifdef CONFIG_XIP_KERNEL
919 static void __init
create_kernel_page_table(pgd_t
*pgdir
,
920 __always_unused
bool early
)
922 uintptr_t va
, start_va
, end_va
;
924 /* Map the flash resident part */
925 end_va
= kernel_map
.virt_addr
+ kernel_map
.xiprom_sz
;
926 for (va
= kernel_map
.virt_addr
; va
< end_va
; va
+= PMD_SIZE
)
927 create_pgd_mapping(pgdir
, va
,
928 kernel_map
.xiprom
+ (va
- kernel_map
.virt_addr
),
929 PMD_SIZE
, PAGE_KERNEL_EXEC
);
931 /* Map the data in RAM */
932 start_va
= kernel_map
.virt_addr
+ (uintptr_t)&_sdata
- (uintptr_t)&_start
;
933 end_va
= kernel_map
.virt_addr
+ kernel_map
.size
;
934 for (va
= start_va
; va
< end_va
; va
+= PMD_SIZE
)
935 create_pgd_mapping(pgdir
, va
,
936 kernel_map
.phys_addr
+ (va
- start_va
),
937 PMD_SIZE
, PAGE_KERNEL
);
940 static void __init
create_kernel_page_table(pgd_t
*pgdir
, bool early
)
942 uintptr_t va
, end_va
;
944 end_va
= kernel_map
.virt_addr
+ kernel_map
.size
;
945 for (va
= kernel_map
.virt_addr
; va
< end_va
; va
+= PMD_SIZE
)
946 create_pgd_mapping(pgdir
, va
,
947 kernel_map
.phys_addr
+ (va
- kernel_map
.virt_addr
),
950 PAGE_KERNEL_EXEC
: pgprot_from_va(va
));
955 * Setup a 4MB mapping that encompasses the device tree: for 64-bit kernel,
956 * this means 2 PMD entries whereas for 32-bit kernel, this is only 1 PGDIR
959 static void __init
create_fdt_early_page_table(uintptr_t fix_fdt_va
,
962 #ifndef CONFIG_BUILTIN_DTB
963 uintptr_t pa
= dtb_pa
& ~(PMD_SIZE
- 1);
965 /* Make sure the fdt fixmap address is always aligned on PMD size */
966 BUILD_BUG_ON(FIX_FDT
% (PMD_SIZE
/ PAGE_SIZE
));
968 /* In 32-bit only, the fdt lies in its own PGD */
969 if (!IS_ENABLED(CONFIG_64BIT
)) {
970 create_pgd_mapping(early_pg_dir
, fix_fdt_va
,
971 pa
, MAX_FDT_SIZE
, PAGE_KERNEL
);
973 create_pmd_mapping(fixmap_pmd
, fix_fdt_va
,
974 pa
, PMD_SIZE
, PAGE_KERNEL
);
975 create_pmd_mapping(fixmap_pmd
, fix_fdt_va
+ PMD_SIZE
,
976 pa
+ PMD_SIZE
, PMD_SIZE
, PAGE_KERNEL
);
979 dtb_early_va
= (void *)fix_fdt_va
+ (dtb_pa
& (PMD_SIZE
- 1));
982 * For 64-bit kernel, __va can't be used since it would return a linear
983 * mapping address whereas dtb_early_va will be used before
984 * setup_vm_final installs the linear mapping. For 32-bit kernel, as the
985 * kernel is mapped in the linear mapping, that makes no difference.
987 dtb_early_va
= kernel_mapping_pa_to_va(dtb_pa
);
990 dtb_early_pa
= dtb_pa
;
994 * MMU is not enabled, the page tables are allocated directly using
995 * early_pmd/pud/p4d and the address returned is the physical one.
997 static void __init
pt_ops_set_early(void)
999 pt_ops
.alloc_pte
= alloc_pte_early
;
1000 pt_ops
.get_pte_virt
= get_pte_virt_early
;
1001 #ifndef __PAGETABLE_PMD_FOLDED
1002 pt_ops
.alloc_pmd
= alloc_pmd_early
;
1003 pt_ops
.get_pmd_virt
= get_pmd_virt_early
;
1004 pt_ops
.alloc_pud
= alloc_pud_early
;
1005 pt_ops
.get_pud_virt
= get_pud_virt_early
;
1006 pt_ops
.alloc_p4d
= alloc_p4d_early
;
1007 pt_ops
.get_p4d_virt
= get_p4d_virt_early
;
1012 * MMU is enabled but page table setup is not complete yet.
1013 * fixmap page table alloc functions must be used as a means to temporarily
1014 * map the allocated physical pages since the linear mapping does not exist yet.
1016 * Note that this is called with MMU disabled, hence kernel_mapping_pa_to_va,
1017 * but it will be used as described above.
1019 static void __init
pt_ops_set_fixmap(void)
1021 pt_ops
.alloc_pte
= kernel_mapping_pa_to_va(alloc_pte_fixmap
);
1022 pt_ops
.get_pte_virt
= kernel_mapping_pa_to_va(get_pte_virt_fixmap
);
1023 #ifndef __PAGETABLE_PMD_FOLDED
1024 pt_ops
.alloc_pmd
= kernel_mapping_pa_to_va(alloc_pmd_fixmap
);
1025 pt_ops
.get_pmd_virt
= kernel_mapping_pa_to_va(get_pmd_virt_fixmap
);
1026 pt_ops
.alloc_pud
= kernel_mapping_pa_to_va(alloc_pud_fixmap
);
1027 pt_ops
.get_pud_virt
= kernel_mapping_pa_to_va(get_pud_virt_fixmap
);
1028 pt_ops
.alloc_p4d
= kernel_mapping_pa_to_va(alloc_p4d_fixmap
);
1029 pt_ops
.get_p4d_virt
= kernel_mapping_pa_to_va(get_p4d_virt_fixmap
);
1034 * MMU is enabled and page table setup is complete, so from now, we can use
1035 * generic page allocation functions to setup page table.
1037 static void __init
pt_ops_set_late(void)
1039 pt_ops
.alloc_pte
= alloc_pte_late
;
1040 pt_ops
.get_pte_virt
= get_pte_virt_late
;
1041 #ifndef __PAGETABLE_PMD_FOLDED
1042 pt_ops
.alloc_pmd
= alloc_pmd_late
;
1043 pt_ops
.get_pmd_virt
= get_pmd_virt_late
;
1044 pt_ops
.alloc_pud
= alloc_pud_late
;
1045 pt_ops
.get_pud_virt
= get_pud_virt_late
;
1046 pt_ops
.alloc_p4d
= alloc_p4d_late
;
1047 pt_ops
.get_p4d_virt
= get_p4d_virt_late
;
1051 #ifdef CONFIG_RANDOMIZE_BASE
1052 extern bool __init
__pi_set_nokaslr_from_cmdline(uintptr_t dtb_pa
);
1053 extern u64 __init
__pi_get_kaslr_seed(uintptr_t dtb_pa
);
1054 extern u64 __init
__pi_get_kaslr_seed_zkr(const uintptr_t dtb_pa
);
1056 static int __init
print_nokaslr(char *p
)
1058 pr_info("Disabled KASLR");
1061 early_param("nokaslr", print_nokaslr
);
1063 unsigned long kaslr_offset(void)
1065 return kernel_map
.virt_offset
;
1069 asmlinkage
void __init
setup_vm(uintptr_t dtb_pa
)
1071 pmd_t __maybe_unused fix_bmap_spmd
, fix_bmap_epmd
;
1073 #ifdef CONFIG_RANDOMIZE_BASE
1074 if (!__pi_set_nokaslr_from_cmdline(dtb_pa
)) {
1075 u64 kaslr_seed
= __pi_get_kaslr_seed_zkr(dtb_pa
);
1076 u32 kernel_size
= (uintptr_t)(&_end
) - (uintptr_t)(&_start
);
1079 if (kaslr_seed
== 0)
1080 kaslr_seed
= __pi_get_kaslr_seed(dtb_pa
);
1082 * Compute the number of positions available: we are limited
1083 * by the early page table that only has one PUD and we must
1084 * be aligned on PMD_SIZE.
1086 nr_pos
= (PUD_SIZE
- kernel_size
) / PMD_SIZE
;
1088 kernel_map
.virt_offset
= (kaslr_seed
% nr_pos
) * PMD_SIZE
;
1092 kernel_map
.virt_addr
= KERNEL_LINK_ADDR
+ kernel_map
.virt_offset
;
1094 #ifdef CONFIG_XIP_KERNEL
1096 kernel_map
.page_offset
= PAGE_OFFSET_L3
;
1098 kernel_map
.page_offset
= _AC(CONFIG_PAGE_OFFSET
, UL
);
1100 kernel_map
.xiprom
= (uintptr_t)CONFIG_XIP_PHYS_ADDR
;
1101 kernel_map
.xiprom_sz
= (uintptr_t)(&_exiprom
) - (uintptr_t)(&_xiprom
);
1103 phys_ram_base
= CONFIG_PHYS_RAM_BASE
;
1104 kernel_map
.phys_addr
= (uintptr_t)CONFIG_PHYS_RAM_BASE
;
1105 kernel_map
.size
= (uintptr_t)(&_end
) - (uintptr_t)(&_start
);
1107 kernel_map
.va_kernel_xip_text_pa_offset
= kernel_map
.virt_addr
- kernel_map
.xiprom
;
1108 kernel_map
.va_kernel_xip_data_pa_offset
= kernel_map
.virt_addr
- kernel_map
.phys_addr
1109 + (uintptr_t)&_sdata
- (uintptr_t)&_start
;
1111 kernel_map
.page_offset
= _AC(CONFIG_PAGE_OFFSET
, UL
);
1112 kernel_map
.phys_addr
= (uintptr_t)(&_start
);
1113 kernel_map
.size
= (uintptr_t)(&_end
) - kernel_map
.phys_addr
;
1114 kernel_map
.va_kernel_pa_offset
= kernel_map
.virt_addr
- kernel_map
.phys_addr
;
1117 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
1118 set_satp_mode(dtb_pa
);
1119 set_mmap_rnd_bits_max();
1123 * In 64-bit, we defer the setup of va_pa_offset to setup_bootmem,
1124 * where we have the system memory layout: this allows us to align
1125 * the physical and virtual mappings and then make use of PUD/P4D/PGD
1126 * for the linear mapping. This is only possible because the kernel
1127 * mapping lies outside the linear mapping.
1128 * In 32-bit however, as the kernel resides in the linear mapping,
1129 * setup_vm_final can not change the mapping established here,
1130 * otherwise the same kernel addresses would get mapped to different
1131 * physical addresses (if the start of dram is different from the
1132 * kernel physical address start).
1134 kernel_map
.va_pa_offset
= IS_ENABLED(CONFIG_64BIT
) ?
1135 0UL : PAGE_OFFSET
- kernel_map
.phys_addr
;
1137 memory_limit
= KERN_VIRT_SIZE
;
1139 /* Sanity check alignment and size */
1140 BUG_ON((PAGE_OFFSET
% PGDIR_SIZE
) != 0);
1141 BUG_ON((kernel_map
.phys_addr
% PMD_SIZE
) != 0);
1145 * The last 4K bytes of the addressable memory can not be mapped because
1146 * of IS_ERR_VALUE macro.
1148 BUG_ON((kernel_map
.virt_addr
+ kernel_map
.size
) > ADDRESS_SPACE_END
- SZ_4K
);
1151 #ifdef CONFIG_RELOCATABLE
1153 * Early page table uses only one PUD, which makes it possible
1154 * to map PUD_SIZE aligned on PUD_SIZE: if the relocation offset
1155 * makes the kernel cross over a PUD_SIZE boundary, raise a bug
1156 * since a part of the kernel would not get mapped.
1158 BUG_ON(PUD_SIZE
- (kernel_map
.virt_addr
& (PUD_SIZE
- 1)) < kernel_map
.size
);
1162 apply_early_boot_alternatives();
1165 /* Setup early PGD for fixmap */
1166 create_pgd_mapping(early_pg_dir
, FIXADDR_START
,
1167 fixmap_pgd_next
, PGDIR_SIZE
, PAGE_TABLE
);
1169 #ifndef __PAGETABLE_PMD_FOLDED
1170 /* Setup fixmap P4D and PUD */
1171 if (pgtable_l5_enabled
)
1172 create_p4d_mapping(fixmap_p4d
, FIXADDR_START
,
1173 (uintptr_t)fixmap_pud
, P4D_SIZE
, PAGE_TABLE
);
1174 /* Setup fixmap PUD and PMD */
1175 if (pgtable_l4_enabled
)
1176 create_pud_mapping(fixmap_pud
, FIXADDR_START
,
1177 (uintptr_t)fixmap_pmd
, PUD_SIZE
, PAGE_TABLE
);
1178 create_pmd_mapping(fixmap_pmd
, FIXADDR_START
,
1179 (uintptr_t)fixmap_pte
, PMD_SIZE
, PAGE_TABLE
);
1180 /* Setup trampoline PGD and PMD */
1181 create_pgd_mapping(trampoline_pg_dir
, kernel_map
.virt_addr
,
1182 trampoline_pgd_next
, PGDIR_SIZE
, PAGE_TABLE
);
1183 if (pgtable_l5_enabled
)
1184 create_p4d_mapping(trampoline_p4d
, kernel_map
.virt_addr
,
1185 (uintptr_t)trampoline_pud
, P4D_SIZE
, PAGE_TABLE
);
1186 if (pgtable_l4_enabled
)
1187 create_pud_mapping(trampoline_pud
, kernel_map
.virt_addr
,
1188 (uintptr_t)trampoline_pmd
, PUD_SIZE
, PAGE_TABLE
);
1189 #ifdef CONFIG_XIP_KERNEL
1190 create_pmd_mapping(trampoline_pmd
, kernel_map
.virt_addr
,
1191 kernel_map
.xiprom
, PMD_SIZE
, PAGE_KERNEL_EXEC
);
1193 create_pmd_mapping(trampoline_pmd
, kernel_map
.virt_addr
,
1194 kernel_map
.phys_addr
, PMD_SIZE
, PAGE_KERNEL_EXEC
);
1197 /* Setup trampoline PGD */
1198 create_pgd_mapping(trampoline_pg_dir
, kernel_map
.virt_addr
,
1199 kernel_map
.phys_addr
, PGDIR_SIZE
, PAGE_KERNEL_EXEC
);
1203 * Setup early PGD covering entire kernel which will allow
1204 * us to reach paging_init(). We map all memory banks later
1205 * in setup_vm_final() below.
1207 create_kernel_page_table(early_pg_dir
, true);
1209 /* Setup early mapping for FDT early scan */
1210 create_fdt_early_page_table(__fix_to_virt(FIX_FDT
), dtb_pa
);
1213 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
1214 * range can not span multiple pmds.
1216 BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN
) >> PMD_SHIFT
)
1217 != (__fix_to_virt(FIX_BTMAP_END
) >> PMD_SHIFT
));
1219 #ifndef __PAGETABLE_PMD_FOLDED
1221 * Early ioremap fixmap is already created as it lies within first 2MB
1222 * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END
1223 * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn
1226 fix_bmap_spmd
= fixmap_pmd
[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN
))];
1227 fix_bmap_epmd
= fixmap_pmd
[pmd_index(__fix_to_virt(FIX_BTMAP_END
))];
1228 if (pmd_val(fix_bmap_spmd
) != pmd_val(fix_bmap_epmd
)) {
1230 pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
1231 pmd_val(fix_bmap_spmd
), pmd_val(fix_bmap_epmd
));
1232 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
1233 fix_to_virt(FIX_BTMAP_BEGIN
));
1234 pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
1235 fix_to_virt(FIX_BTMAP_END
));
1237 pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END
);
1238 pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN
);
1242 pt_ops_set_fixmap();
1245 static void __meminit
create_linear_mapping_range(phys_addr_t start
, phys_addr_t end
,
1246 uintptr_t fixed_map_size
, const pgprot_t
*pgprot
)
1249 uintptr_t va
, map_size
;
1251 for (pa
= start
; pa
< end
; pa
+= map_size
) {
1252 va
= (uintptr_t)__va(pa
);
1253 map_size
= fixed_map_size
? fixed_map_size
:
1254 best_map_size(pa
, va
, end
- pa
);
1256 create_pgd_mapping(swapper_pg_dir
, va
, pa
, map_size
,
1257 pgprot
? *pgprot
: pgprot_from_va(va
));
1261 static void __init
create_linear_mapping_page_table(void)
1263 phys_addr_t start
, end
;
1264 phys_addr_t kfence_pool __maybe_unused
;
1267 #ifdef CONFIG_STRICT_KERNEL_RWX
1268 phys_addr_t ktext_start
= __pa_symbol(_start
);
1269 phys_addr_t ktext_size
= __init_data_begin
- _start
;
1270 phys_addr_t krodata_start
= __pa_symbol(__start_rodata
);
1271 phys_addr_t krodata_size
= _data
- __start_rodata
;
1273 /* Isolate kernel text and rodata so they don't get mapped with a PUD */
1274 memblock_mark_nomap(ktext_start
, ktext_size
);
1275 memblock_mark_nomap(krodata_start
, krodata_size
);
1278 #ifdef CONFIG_KFENCE
1280 * kfence pool must be backed by PAGE_SIZE mappings, so allocate it
1281 * before we setup the linear mapping so that we avoid using hugepages
1284 kfence_pool
= memblock_phys_alloc(KFENCE_POOL_SIZE
, PAGE_SIZE
);
1285 BUG_ON(!kfence_pool
);
1287 memblock_mark_nomap(kfence_pool
, KFENCE_POOL_SIZE
);
1288 __kfence_pool
= __va(kfence_pool
);
1291 /* Map all memory banks in the linear mapping */
1292 for_each_mem_range(i
, &start
, &end
) {
1295 if (start
<= __pa(PAGE_OFFSET
) &&
1296 __pa(PAGE_OFFSET
) < end
)
1297 start
= __pa(PAGE_OFFSET
);
1299 create_linear_mapping_range(start
, end
, 0, NULL
);
1302 #ifdef CONFIG_STRICT_KERNEL_RWX
1303 create_linear_mapping_range(ktext_start
, ktext_start
+ ktext_size
, 0, NULL
);
1304 create_linear_mapping_range(krodata_start
, krodata_start
+ krodata_size
, 0, NULL
);
1306 memblock_clear_nomap(ktext_start
, ktext_size
);
1307 memblock_clear_nomap(krodata_start
, krodata_size
);
1310 #ifdef CONFIG_KFENCE
1311 create_linear_mapping_range(kfence_pool
, kfence_pool
+ KFENCE_POOL_SIZE
, PAGE_SIZE
, NULL
);
1313 memblock_clear_nomap(kfence_pool
, KFENCE_POOL_SIZE
);
1317 static void __init
setup_vm_final(void)
1319 /* Setup swapper PGD for fixmap */
1320 #if !defined(CONFIG_64BIT)
1322 * In 32-bit, the device tree lies in a pgd entry, so it must be copied
1323 * directly in swapper_pg_dir in addition to the pgd entry that points
1326 unsigned long idx
= pgd_index(__fix_to_virt(FIX_FDT
));
1328 set_pgd(&swapper_pg_dir
[idx
], early_pg_dir
[idx
]);
1330 create_pgd_mapping(swapper_pg_dir
, FIXADDR_START
,
1331 __pa_symbol(fixmap_pgd_next
),
1332 PGDIR_SIZE
, PAGE_TABLE
);
1334 /* Map the linear mapping */
1335 create_linear_mapping_page_table();
1337 /* Map the kernel */
1338 if (IS_ENABLED(CONFIG_64BIT
))
1339 create_kernel_page_table(swapper_pg_dir
, false);
1342 kasan_swapper_init();
1345 /* Clear fixmap PTE and PMD mappings */
1346 clear_fixmap(FIX_PTE
);
1347 clear_fixmap(FIX_PMD
);
1348 clear_fixmap(FIX_PUD
);
1349 clear_fixmap(FIX_P4D
);
1351 /* Move to swapper page table */
1352 csr_write(CSR_SATP
, PFN_DOWN(__pa_symbol(swapper_pg_dir
)) | satp_mode
);
1353 local_flush_tlb_all();
1358 asmlinkage
void __init
setup_vm(uintptr_t dtb_pa
)
1360 dtb_early_va
= (void *)dtb_pa
;
1361 dtb_early_pa
= dtb_pa
;
1364 static inline void setup_vm_final(void)
1367 #endif /* CONFIG_MMU */
1370 * reserve_crashkernel() - reserves memory for crash kernel
1372 * This function reserves memory area given in "crashkernel=" kernel command
1373 * line parameter. The memory reserved is used by dump capture kernel when
1374 * primary kernel is crashing.
1376 static void __init
arch_reserve_crashkernel(void)
1378 unsigned long long low_size
= 0;
1379 unsigned long long crash_base
, crash_size
;
1380 char *cmdline
= boot_command_line
;
1384 if (!IS_ENABLED(CONFIG_CRASH_RESERVE
))
1387 ret
= parse_crashkernel(cmdline
, memblock_phys_mem_size(),
1388 &crash_size
, &crash_base
,
1393 reserve_crashkernel_generic(cmdline
, crash_size
, crash_base
,
1397 void __init
paging_init(void)
1402 /* Depend on that Linear Mapping is ready */
1403 memblock_allow_resize();
1406 void __init
misc_mem_init(void)
1408 early_memtest(min_low_pfn
<< PAGE_SHIFT
, max_low_pfn
<< PAGE_SHIFT
);
1411 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1412 /* The entire VMEMMAP region has been populated. Flush TLB for this region */
1413 local_flush_tlb_kernel_range(VMEMMAP_START
, VMEMMAP_END
);
1416 arch_reserve_crashkernel();
1417 memblock_dump_all();
1420 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1421 void __meminit
vmemmap_set_pmd(pmd_t
*pmd
, void *p
, int node
,
1422 unsigned long addr
, unsigned long next
)
1424 pmd_set_huge(pmd
, virt_to_phys(p
), PAGE_KERNEL
);
1427 int __meminit
vmemmap_check_pmd(pmd_t
*pmdp
, int node
,
1428 unsigned long addr
, unsigned long next
)
1430 vmemmap_verify((pte_t
*)pmdp
, node
, addr
, next
);
1434 int __meminit
vmemmap_populate(unsigned long start
, unsigned long end
, int node
,
1435 struct vmem_altmap
*altmap
)
1438 * Note that SPARSEMEM_VMEMMAP is only selected for rv64 and that we
1439 * can't use hugepage mappings for 2-level page table because in case of
1440 * memory hotplug, we are not able to update all the page tables with
1443 return vmemmap_populate_hugepages(start
, end
, node
, altmap
);
1447 #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
1449 * Pre-allocates page-table pages for a specific area in the kernel
1450 * page-table. Only the level which needs to be synchronized between
1451 * all page-tables is allocated because the synchronization can be
1454 static void __init
preallocate_pgd_pages_range(unsigned long start
, unsigned long end
,
1460 for (addr
= start
; addr
< end
&& addr
>= start
; addr
= ALIGN(addr
+ 1, PGDIR_SIZE
)) {
1461 pgd_t
*pgd
= pgd_offset_k(addr
);
1467 p4d
= p4d_alloc(&init_mm
, pgd
, addr
);
1471 if (pgtable_l5_enabled
)
1475 pud
= pud_alloc(&init_mm
, p4d
, addr
);
1479 if (pgtable_l4_enabled
)
1483 pmd
= pmd_alloc(&init_mm
, pud
, addr
);
1491 * The pages have to be there now or they will be missing in
1492 * process page-tables later.
1494 panic("Failed to pre-allocate %s pages for %s area\n", lvl
, area
);
1497 #define PAGE_END KASAN_SHADOW_START
1499 void __init
pgtable_cache_init(void)
1501 preallocate_pgd_pages_range(VMALLOC_START
, VMALLOC_END
, "vmalloc");
1502 if (IS_ENABLED(CONFIG_MODULES
))
1503 preallocate_pgd_pages_range(MODULES_VADDR
, MODULES_END
, "bpf/modules");
1504 if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG
)) {
1505 preallocate_pgd_pages_range(VMEMMAP_START
, VMEMMAP_END
, "vmemmap");
1506 preallocate_pgd_pages_range(PAGE_OFFSET
, PAGE_END
, "direct map");
1507 if (IS_ENABLED(CONFIG_KASAN
))
1508 preallocate_pgd_pages_range(KASAN_SHADOW_START
, KASAN_SHADOW_END
, "kasan");
1513 #ifdef CONFIG_EXECMEM
1515 static struct execmem_info execmem_info __ro_after_init
;
1517 struct execmem_info __init
*execmem_arch_setup(void)
1519 execmem_info
= (struct execmem_info
){
1521 [EXECMEM_DEFAULT
] = {
1522 .start
= MODULES_VADDR
,
1524 .pgprot
= PAGE_KERNEL
,
1527 [EXECMEM_KPROBES
] = {
1528 .start
= VMALLOC_START
,
1530 .pgprot
= PAGE_KERNEL_READ_EXEC
,
1534 .start
= BPF_JIT_REGION_START
,
1535 .end
= BPF_JIT_REGION_END
,
1536 .pgprot
= PAGE_KERNEL
,
1537 .alignment
= PAGE_SIZE
,
1542 return &execmem_info
;
1544 #endif /* CONFIG_MMU */
1545 #endif /* CONFIG_EXECMEM */
1547 #ifdef CONFIG_MEMORY_HOTPLUG
1548 static void __meminit
free_pte_table(pte_t
*pte_start
, pmd_t
*pmd
)
1550 struct page
*page
= pmd_page(*pmd
);
1551 struct ptdesc
*ptdesc
= page_ptdesc(page
);
1555 for (i
= 0; i
< PTRS_PER_PTE
; i
++) {
1556 pte
= pte_start
+ i
;
1557 if (!pte_none(*pte
))
1561 pagetable_pte_dtor(ptdesc
);
1562 if (PageReserved(page
))
1563 free_reserved_page(page
);
1565 pagetable_free(ptdesc
);
1569 static void __meminit
free_pmd_table(pmd_t
*pmd_start
, pud_t
*pud
)
1571 struct page
*page
= pud_page(*pud
);
1572 struct ptdesc
*ptdesc
= page_ptdesc(page
);
1576 for (i
= 0; i
< PTRS_PER_PMD
; i
++) {
1577 pmd
= pmd_start
+ i
;
1578 if (!pmd_none(*pmd
))
1582 pagetable_pmd_dtor(ptdesc
);
1583 if (PageReserved(page
))
1584 free_reserved_page(page
);
1586 pagetable_free(ptdesc
);
1590 static void __meminit
free_pud_table(pud_t
*pud_start
, p4d_t
*p4d
)
1592 struct page
*page
= p4d_page(*p4d
);
1596 for (i
= 0; i
< PTRS_PER_PUD
; i
++) {
1597 pud
= pud_start
+ i
;
1598 if (!pud_none(*pud
))
1602 if (PageReserved(page
))
1603 free_reserved_page(page
);
1605 free_pages((unsigned long)page_address(page
), 0);
1609 static void __meminit
free_vmemmap_storage(struct page
*page
, size_t size
,
1610 struct vmem_altmap
*altmap
)
1612 int order
= get_order(size
);
1615 vmem_altmap_free(altmap
, size
>> PAGE_SHIFT
);
1619 if (PageReserved(page
)) {
1620 unsigned int nr_pages
= 1 << order
;
1623 free_reserved_page(page
++);
1627 free_pages((unsigned long)page_address(page
), order
);
1630 static void __meminit
remove_pte_mapping(pte_t
*pte_base
, unsigned long addr
, unsigned long end
,
1631 bool is_vmemmap
, struct vmem_altmap
*altmap
)
1636 for (; addr
< end
; addr
= next
) {
1637 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
1641 ptep
= pte_base
+ pte_index(addr
);
1642 pte
= ptep_get(ptep
);
1643 if (!pte_present(*ptep
))
1646 pte_clear(&init_mm
, addr
, ptep
);
1648 free_vmemmap_storage(pte_page(pte
), PAGE_SIZE
, altmap
);
1652 static void __meminit
remove_pmd_mapping(pmd_t
*pmd_base
, unsigned long addr
, unsigned long end
,
1653 bool is_vmemmap
, struct vmem_altmap
*altmap
)
1659 for (; addr
< end
; addr
= next
) {
1660 next
= pmd_addr_end(addr
, end
);
1661 pmdp
= pmd_base
+ pmd_index(addr
);
1662 pmd
= pmdp_get(pmdp
);
1663 if (!pmd_present(pmd
))
1666 if (pmd_leaf(pmd
)) {
1669 free_vmemmap_storage(pmd_page(pmd
), PMD_SIZE
, altmap
);
1673 pte_base
= (pte_t
*)pmd_page_vaddr(*pmdp
);
1674 remove_pte_mapping(pte_base
, addr
, next
, is_vmemmap
, altmap
);
1675 free_pte_table(pte_base
, pmdp
);
1679 static void __meminit
remove_pud_mapping(pud_t
*pud_base
, unsigned long addr
, unsigned long end
,
1680 bool is_vmemmap
, struct vmem_altmap
*altmap
)
1686 for (; addr
< end
; addr
= next
) {
1687 next
= pud_addr_end(addr
, end
);
1688 pudp
= pud_base
+ pud_index(addr
);
1689 pud
= pudp_get(pudp
);
1690 if (!pud_present(pud
))
1693 if (pud_leaf(pud
)) {
1694 if (pgtable_l4_enabled
) {
1697 free_vmemmap_storage(pud_page(pud
), PUD_SIZE
, altmap
);
1702 pmd_base
= pmd_offset(pudp
, 0);
1703 remove_pmd_mapping(pmd_base
, addr
, next
, is_vmemmap
, altmap
);
1705 if (pgtable_l4_enabled
)
1706 free_pmd_table(pmd_base
, pudp
);
1710 static void __meminit
remove_p4d_mapping(p4d_t
*p4d_base
, unsigned long addr
, unsigned long end
,
1711 bool is_vmemmap
, struct vmem_altmap
*altmap
)
1717 for (; addr
< end
; addr
= next
) {
1718 next
= p4d_addr_end(addr
, end
);
1719 p4dp
= p4d_base
+ p4d_index(addr
);
1720 p4d
= p4dp_get(p4dp
);
1721 if (!p4d_present(p4d
))
1724 if (p4d_leaf(p4d
)) {
1725 if (pgtable_l5_enabled
) {
1728 free_vmemmap_storage(p4d_page(p4d
), P4D_SIZE
, altmap
);
1733 pud_base
= pud_offset(p4dp
, 0);
1734 remove_pud_mapping(pud_base
, addr
, next
, is_vmemmap
, altmap
);
1736 if (pgtable_l5_enabled
)
1737 free_pud_table(pud_base
, p4dp
);
1741 static void __meminit
remove_pgd_mapping(unsigned long va
, unsigned long end
, bool is_vmemmap
,
1742 struct vmem_altmap
*altmap
)
1744 unsigned long addr
, next
;
1748 for (addr
= va
; addr
< end
; addr
= next
) {
1749 next
= pgd_addr_end(addr
, end
);
1750 pgd
= pgd_offset_k(addr
);
1752 if (!pgd_present(*pgd
))
1758 p4d_base
= p4d_offset(pgd
, 0);
1759 remove_p4d_mapping(p4d_base
, addr
, next
, is_vmemmap
, altmap
);
1765 static void __meminit
remove_linear_mapping(phys_addr_t start
, u64 size
)
1767 unsigned long va
= (unsigned long)__va(start
);
1768 unsigned long end
= (unsigned long)__va(start
+ size
);
1770 remove_pgd_mapping(va
, end
, false, NULL
);
1773 struct range
arch_get_mappable_range(void)
1775 struct range mhp_range
;
1777 mhp_range
.start
= __pa(PAGE_OFFSET
);
1778 mhp_range
.end
= __pa(PAGE_END
- 1);
1782 int __ref
arch_add_memory(int nid
, u64 start
, u64 size
, struct mhp_params
*params
)
1786 create_linear_mapping_range(start
, start
+ size
, 0, ¶ms
->pgprot
);
1787 ret
= __add_pages(nid
, start
>> PAGE_SHIFT
, size
>> PAGE_SHIFT
, params
);
1789 remove_linear_mapping(start
, size
);
1793 max_pfn
= PFN_UP(start
+ size
);
1794 max_low_pfn
= max_pfn
;
1801 void __ref
arch_remove_memory(u64 start
, u64 size
, struct vmem_altmap
*altmap
)
1803 __remove_pages(start
>> PAGE_SHIFT
, size
>> PAGE_SHIFT
, altmap
);
1804 remove_linear_mapping(start
, size
);
1808 void __ref
vmemmap_free(unsigned long start
, unsigned long end
, struct vmem_altmap
*altmap
)
1810 remove_pgd_mapping(start
, end
, true, altmap
);
1812 #endif /* CONFIG_MEMORY_HOTPLUG */