1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
7 */
8
9 #include <linux/init.h>
10 #include <linux/mm.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>
25 #endif
26 #include <linux/kfence.h>
27 #include <linux/execmem.h>
28
29 #include <asm/fixmap.h>
30 #include <asm/io.h>
31 #include <asm/kasan.h>
32 #include <asm/numa.h>
33 #include <asm/pgtable.h>
34 #include <asm/sections.h>
35 #include <asm/soc.h>
36 #include <asm/tlbflush.h>
37
38 #include "../kernel/head.h"
39
40 u64 new_vmalloc[NR_CPUS / sizeof(u64) + 1];
41
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))
46 #endif
47
48 #ifdef CONFIG_64BIT
49 u64 satp_mode __ro_after_init = !IS_ENABLED(CONFIG_XIP_KERNEL) ? SATP_MODE_57 : SATP_MODE_39;
50 #else
51 u64 satp_mode __ro_after_init = SATP_MODE_32;
52 #endif
53 EXPORT_SYMBOL(satp_mode);
54
55 #ifdef CONFIG_64BIT
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);
60 #endif
61
62 phys_addr_t phys_ram_base __ro_after_init;
63 EXPORT_SYMBOL(phys_ram_base);
64
65 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
66 __page_aligned_bss;
67 EXPORT_SYMBOL(empty_zero_page);
68
69 extern char _start[];
70 void *_dtb_early_va __initdata;
71 uintptr_t _dtb_early_pa __initdata;
72
73 phys_addr_t dma32_phys_limit __initdata;
74
zone_sizes_init(void)75 static void __init zone_sizes_init(void)
76 {
77 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
78
79 #ifdef CONFIG_ZONE_DMA32
80 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
81 #endif
82 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
83
84 free_area_init(max_zone_pfns);
85 }
86
87 #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
88
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)
93
print_mlk(char * name,unsigned long b,unsigned long t)94 static inline void print_mlk(char *name, unsigned long b, unsigned long t)
95 {
96 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t,
97 (((t) - (b)) >> LOG2_SZ_1K));
98 }
99
print_mlm(char * name,unsigned long b,unsigned long t)100 static inline void print_mlm(char *name, unsigned long b, unsigned long t)
101 {
102 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t,
103 (((t) - (b)) >> LOG2_SZ_1M));
104 }
105
print_mlg(char * name,unsigned long b,unsigned long t)106 static inline void print_mlg(char *name, unsigned long b, unsigned long t)
107 {
108 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld GB)\n", name, b, t,
109 (((t) - (b)) >> LOG2_SZ_1G));
110 }
111
112 #ifdef CONFIG_64BIT
print_mlt(char * name,unsigned long b,unsigned long t)113 static inline void print_mlt(char *name, unsigned long b, unsigned long t)
114 {
115 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld TB)\n", name, b, t,
116 (((t) - (b)) >> LOG2_SZ_1T));
117 }
118 #else
119 #define print_mlt(n, b, t) do {} while (0)
120 #endif
121
print_ml(char * name,unsigned long b,unsigned long t)122 static inline void print_ml(char *name, unsigned long b, unsigned long t)
123 {
124 unsigned long diff = t - b;
125
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);
132 else
133 print_mlk(name, b, t);
134 }
135
print_vm_layout(void)136 static void __init print_vm_layout(void)
137 {
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);
147 #ifdef CONFIG_64BIT
148 print_ml("modules", (unsigned long)MODULES_VADDR,
149 (unsigned long)MODULES_END);
150 #endif
151 print_ml("lowmem", (unsigned long)PAGE_OFFSET,
152 (unsigned long)high_memory);
153 if (IS_ENABLED(CONFIG_64BIT)) {
154 #ifdef CONFIG_KASAN
155 print_ml("kasan", KASAN_SHADOW_START, KASAN_SHADOW_END);
156 #endif
157
158 print_ml("kernel", (unsigned long)kernel_map.virt_addr,
159 (unsigned long)ADDRESS_SPACE_END);
160 }
161 }
162 #else
print_vm_layout(void)163 static void print_vm_layout(void) { }
164 #endif /* CONFIG_DEBUG_VM */
165
mem_init(void)166 void __init mem_init(void)
167 {
168 bool swiotlb = max_pfn > PFN_DOWN(dma32_phys_limit);
169 #ifdef CONFIG_FLATMEM
170 BUG_ON(!mem_map);
171 #endif /* CONFIG_FLATMEM */
172
173 if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb &&
174 dma_cache_alignment != 1) {
175 /*
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.
179 */
180 unsigned long size =
181 DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
182 swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
183 swiotlb = true;
184 }
185
186 swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
187 memblock_free_all();
188
189 print_vm_layout();
190 }
191
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 */
197
early_mem(char * p)198 static int __init early_mem(char *p)
199 {
200 u64 size;
201
202 if (!p)
203 return 1;
204
205 size = memparse(p, &p) & PAGE_MASK;
206 memory_limit = min_t(u64, size, memory_limit);
207
208 pr_notice("Memory limited to %lldMB\n", (u64)memory_limit >> 20);
209
210 return 0;
211 }
212 early_param("mem", early_mem);
213
setup_bootmem(void)214 static void __init setup_bootmem(void)
215 {
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;
219
220 if (IS_ENABLED(CONFIG_XIP_KERNEL))
221 vmlinux_start = __pa_symbol(&_sdata);
222 else
223 vmlinux_start = __pa_symbol(&_start);
224
225 memblock_enforce_memory_limit(memory_limit);
226
227 /*
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.
231 */
232 if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
233 vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK;
234 /*
235 * Reserve from the start of the kernel to the end of the kernel
236 */
237 memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
238
239 /*
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.
242 */
243 if (!IS_ENABLED(CONFIG_XIP_KERNEL))
244 phys_ram_base = memblock_start_of_DRAM() & PMD_MASK;
245
246 /*
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.
249 */
250 if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU))
251 kernel_map.va_pa_offset = PAGE_OFFSET - phys_ram_base;
252
253 /*
254 * The size of the linear page mapping may restrict the amount of
255 * usable RAM.
256 */
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);
261 }
262
263 /*
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
268 *
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.
273 */
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);
277 }
278
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)));
283
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);
286
287 reserve_initrd_mem();
288
289 /*
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
292 * reserved region.
293 */
294 early_init_fdt_scan_reserved_mem();
295
296 /*
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
301 */
302 if (!IS_ENABLED(CONFIG_BUILTIN_DTB))
303 memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
304
305 dma_contiguous_reserve(dma32_phys_limit);
306 if (IS_ENABLED(CONFIG_64BIT))
307 hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
308 }
309
310 #ifdef CONFIG_MMU
311 struct pt_alloc_ops pt_ops __meminitdata;
312
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;
316
317 pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
318
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 */
325
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
343 };
344 DECLARE_VM_GET_PAGE_PROT
345
__set_fixmap(enum fixed_addresses idx,phys_addr_t phys,pgprot_t prot)346 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
347 {
348 unsigned long addr = __fix_to_virt(idx);
349 pte_t *ptep;
350
351 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
352
353 ptep = &fixmap_pte[pte_index(addr)];
354
355 if (pgprot_val(prot))
356 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
357 else
358 pte_clear(&init_mm, addr, ptep);
359 local_flush_tlb_page(addr);
360 }
361
get_pte_virt_early(phys_addr_t pa)362 static inline pte_t *__init get_pte_virt_early(phys_addr_t pa)
363 {
364 return (pte_t *)((uintptr_t)pa);
365 }
366
get_pte_virt_fixmap(phys_addr_t pa)367 static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa)
368 {
369 clear_fixmap(FIX_PTE);
370 return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
371 }
372
get_pte_virt_late(phys_addr_t pa)373 static inline pte_t *__meminit get_pte_virt_late(phys_addr_t pa)
374 {
375 return (pte_t *) __va(pa);
376 }
377
alloc_pte_early(uintptr_t va)378 static inline phys_addr_t __init alloc_pte_early(uintptr_t va)
379 {
380 /*
381 * We only create PMD or PGD early mappings so we
382 * should never reach here with MMU disabled.
383 */
384 BUG();
385 }
386
alloc_pte_fixmap(uintptr_t va)387 static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va)
388 {
389 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
390 }
391
alloc_pte_late(uintptr_t va)392 static phys_addr_t __meminit alloc_pte_late(uintptr_t va)
393 {
394 struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL & ~__GFP_HIGHMEM, 0);
395
396 BUG_ON(!ptdesc || !pagetable_pte_ctor(ptdesc));
397 return __pa((pte_t *)ptdesc_address(ptdesc));
398 }
399
create_pte_mapping(pte_t * ptep,uintptr_t va,phys_addr_t pa,phys_addr_t sz,pgprot_t prot)400 static void __meminit create_pte_mapping(pte_t *ptep, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
401 pgprot_t prot)
402 {
403 uintptr_t pte_idx = pte_index(va);
404
405 BUG_ON(sz != PAGE_SIZE);
406
407 if (pte_none(ptep[pte_idx]))
408 ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot);
409 }
410
411 #ifndef __PAGETABLE_PMD_FOLDED
412
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);
416
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 */
422
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);
426
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 */
432
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);
436
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 */
442
get_pmd_virt_early(phys_addr_t pa)443 static pmd_t *__init get_pmd_virt_early(phys_addr_t pa)
444 {
445 /* Before MMU is enabled */
446 return (pmd_t *)((uintptr_t)pa);
447 }
448
get_pmd_virt_fixmap(phys_addr_t pa)449 static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa)
450 {
451 clear_fixmap(FIX_PMD);
452 return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
453 }
454
get_pmd_virt_late(phys_addr_t pa)455 static pmd_t *__meminit get_pmd_virt_late(phys_addr_t pa)
456 {
457 return (pmd_t *) __va(pa);
458 }
459
alloc_pmd_early(uintptr_t va)460 static phys_addr_t __init alloc_pmd_early(uintptr_t va)
461 {
462 BUG_ON((va - kernel_map.virt_addr) >> PUD_SHIFT);
463
464 return (uintptr_t)early_pmd;
465 }
466
alloc_pmd_fixmap(uintptr_t va)467 static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va)
468 {
469 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
470 }
471
alloc_pmd_late(uintptr_t va)472 static phys_addr_t __meminit alloc_pmd_late(uintptr_t va)
473 {
474 struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL & ~__GFP_HIGHMEM, 0);
475
476 BUG_ON(!ptdesc || !pagetable_pmd_ctor(ptdesc));
477 return __pa((pmd_t *)ptdesc_address(ptdesc));
478 }
479
create_pmd_mapping(pmd_t * pmdp,uintptr_t va,phys_addr_t pa,phys_addr_t sz,pgprot_t prot)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)
483 {
484 pte_t *ptep;
485 phys_addr_t pte_phys;
486 uintptr_t pmd_idx = pmd_index(va);
487
488 if (sz == PMD_SIZE) {
489 if (pmd_none(pmdp[pmd_idx]))
490 pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot);
491 return;
492 }
493
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);
499 } else {
500 pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx]));
501 ptep = pt_ops.get_pte_virt(pte_phys);
502 }
503
504 create_pte_mapping(ptep, va, pa, sz, prot);
505 }
506
get_pud_virt_early(phys_addr_t pa)507 static pud_t *__init get_pud_virt_early(phys_addr_t pa)
508 {
509 return (pud_t *)((uintptr_t)pa);
510 }
511
get_pud_virt_fixmap(phys_addr_t pa)512 static pud_t *__init get_pud_virt_fixmap(phys_addr_t pa)
513 {
514 clear_fixmap(FIX_PUD);
515 return (pud_t *)set_fixmap_offset(FIX_PUD, pa);
516 }
517
get_pud_virt_late(phys_addr_t pa)518 static pud_t *__meminit get_pud_virt_late(phys_addr_t pa)
519 {
520 return (pud_t *)__va(pa);
521 }
522
alloc_pud_early(uintptr_t va)523 static phys_addr_t __init alloc_pud_early(uintptr_t va)
524 {
525 /* Only one PUD is available for early mapping */
526 BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
527
528 return (uintptr_t)early_pud;
529 }
530
alloc_pud_fixmap(uintptr_t va)531 static phys_addr_t __init alloc_pud_fixmap(uintptr_t va)
532 {
533 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
534 }
535
alloc_pud_late(uintptr_t va)536 static phys_addr_t __meminit alloc_pud_late(uintptr_t va)
537 {
538 unsigned long vaddr;
539
540 vaddr = __get_free_page(GFP_KERNEL);
541 BUG_ON(!vaddr);
542 return __pa(vaddr);
543 }
544
get_p4d_virt_early(phys_addr_t pa)545 static p4d_t *__init get_p4d_virt_early(phys_addr_t pa)
546 {
547 return (p4d_t *)((uintptr_t)pa);
548 }
549
get_p4d_virt_fixmap(phys_addr_t pa)550 static p4d_t *__init get_p4d_virt_fixmap(phys_addr_t pa)
551 {
552 clear_fixmap(FIX_P4D);
553 return (p4d_t *)set_fixmap_offset(FIX_P4D, pa);
554 }
555
get_p4d_virt_late(phys_addr_t pa)556 static p4d_t *__meminit get_p4d_virt_late(phys_addr_t pa)
557 {
558 return (p4d_t *)__va(pa);
559 }
560
alloc_p4d_early(uintptr_t va)561 static phys_addr_t __init alloc_p4d_early(uintptr_t va)
562 {
563 /* Only one P4D is available for early mapping */
564 BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
565
566 return (uintptr_t)early_p4d;
567 }
568
alloc_p4d_fixmap(uintptr_t va)569 static phys_addr_t __init alloc_p4d_fixmap(uintptr_t va)
570 {
571 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
572 }
573
alloc_p4d_late(uintptr_t va)574 static phys_addr_t __meminit alloc_p4d_late(uintptr_t va)
575 {
576 unsigned long vaddr;
577
578 vaddr = __get_free_page(GFP_KERNEL);
579 BUG_ON(!vaddr);
580 return __pa(vaddr);
581 }
582
create_pud_mapping(pud_t * pudp,uintptr_t va,phys_addr_t pa,phys_addr_t sz,pgprot_t prot)583 static void __meminit create_pud_mapping(pud_t *pudp, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
584 pgprot_t prot)
585 {
586 pmd_t *nextp;
587 phys_addr_t next_phys;
588 uintptr_t pud_index = pud_index(va);
589
590 if (sz == PUD_SIZE) {
591 if (pud_val(pudp[pud_index]) == 0)
592 pudp[pud_index] = pfn_pud(PFN_DOWN(pa), prot);
593 return;
594 }
595
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);
601 } else {
602 next_phys = PFN_PHYS(_pud_pfn(pudp[pud_index]));
603 nextp = pt_ops.get_pmd_virt(next_phys);
604 }
605
606 create_pmd_mapping(nextp, va, pa, sz, prot);
607 }
608
create_p4d_mapping(p4d_t * p4dp,uintptr_t va,phys_addr_t pa,phys_addr_t sz,pgprot_t prot)609 static void __meminit create_p4d_mapping(p4d_t *p4dp, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
610 pgprot_t prot)
611 {
612 pud_t *nextp;
613 phys_addr_t next_phys;
614 uintptr_t p4d_index = p4d_index(va);
615
616 if (sz == P4D_SIZE) {
617 if (p4d_val(p4dp[p4d_index]) == 0)
618 p4dp[p4d_index] = pfn_p4d(PFN_DOWN(pa), prot);
619 return;
620 }
621
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);
627 } else {
628 next_phys = PFN_PHYS(_p4d_pfn(p4dp[p4d_index]));
629 nextp = pt_ops.get_pud_virt(next_phys);
630 }
631
632 create_pud_mapping(nextp, va, pa, sz, prot);
633 }
634
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))
654 #else
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 */
665
create_pgd_mapping(pgd_t * pgdp,uintptr_t va,phys_addr_t pa,phys_addr_t sz,pgprot_t prot)666 void __meminit create_pgd_mapping(pgd_t *pgdp, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
667 pgprot_t prot)
668 {
669 pgd_next_t *nextp;
670 phys_addr_t next_phys;
671 uintptr_t pgd_idx = pgd_index(va);
672
673 if (sz == PGDIR_SIZE) {
674 if (pgd_val(pgdp[pgd_idx]) == 0)
675 pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot);
676 return;
677 }
678
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);
684 } else {
685 next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx]));
686 nextp = get_pgd_next_virt(next_phys);
687 }
688
689 create_pgd_next_mapping(nextp, va, pa, sz, prot);
690 }
691
best_map_size(phys_addr_t pa,uintptr_t va,phys_addr_t size)692 static uintptr_t __meminit best_map_size(phys_addr_t pa, uintptr_t va, phys_addr_t size)
693 {
694 if (debug_pagealloc_enabled())
695 return PAGE_SIZE;
696
697 if (pgtable_l5_enabled &&
698 !(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >= P4D_SIZE)
699 return P4D_SIZE;
700
701 if (pgtable_l4_enabled &&
702 !(pa & (PUD_SIZE - 1)) && !(va & (PUD_SIZE - 1)) && size >= PUD_SIZE)
703 return PUD_SIZE;
704
705 if (IS_ENABLED(CONFIG_64BIT) &&
706 !(pa & (PMD_SIZE - 1)) && !(va & (PMD_SIZE - 1)) && size >= PMD_SIZE)
707 return PMD_SIZE;
708
709 return PAGE_SIZE;
710 }
711
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;
715
716 /* called from head.S with MMU off */
__copy_data(void)717 asmlinkage void __init __copy_data(void)
718 {
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));
722
723 memcpy(to, from, sz);
724 }
725 #endif
726
727 #ifdef CONFIG_STRICT_KERNEL_RWX
pgprot_from_va(uintptr_t va)728 static __meminit pgprot_t pgprot_from_va(uintptr_t va)
729 {
730 if (is_va_kernel_text(va))
731 return PAGE_KERNEL_READ_EXEC;
732
733 /*
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
736 * written.
737 * And rodata section is marked readonly in mark_rodata_ro.
738 */
739 if (IS_ENABLED(CONFIG_64BIT) && is_va_kernel_lm_alias_text(va))
740 return PAGE_KERNEL_READ;
741
742 return PAGE_KERNEL;
743 }
744
mark_rodata_ro(void)745 void mark_rodata_ro(void)
746 {
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),
750 set_memory_ro);
751 }
752 #else
pgprot_from_va(uintptr_t va)753 static __meminit pgprot_t pgprot_from_va(uintptr_t va)
754 {
755 if (IS_ENABLED(CONFIG_64BIT) && !is_kernel_mapping(va))
756 return PAGE_KERNEL;
757
758 return PAGE_KERNEL_EXEC;
759 }
760 #endif /* CONFIG_STRICT_KERNEL_RWX */
761
762 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
763 u64 __pi_set_satp_mode_from_cmdline(uintptr_t dtb_pa);
764
disable_pgtable_l5(void)765 static void __init disable_pgtable_l5(void)
766 {
767 pgtable_l5_enabled = false;
768 kernel_map.page_offset = PAGE_OFFSET_L4;
769 satp_mode = SATP_MODE_48;
770 }
771
disable_pgtable_l4(void)772 static void __init disable_pgtable_l4(void)
773 {
774 pgtable_l4_enabled = false;
775 kernel_map.page_offset = PAGE_OFFSET_L3;
776 satp_mode = SATP_MODE_39;
777 }
778
print_no4lvl(char * p)779 static int __init print_no4lvl(char *p)
780 {
781 pr_info("Disabled 4-level and 5-level paging");
782 return 0;
783 }
784 early_param("no4lvl", print_no4lvl);
785
print_no5lvl(char * p)786 static int __init print_no5lvl(char *p)
787 {
788 pr_info("Disabled 5-level paging");
789 return 0;
790 }
791 early_param("no5lvl", print_no5lvl);
792
set_mmap_rnd_bits_max(void)793 static void __init set_mmap_rnd_bits_max(void)
794 {
795 mmap_rnd_bits_max = MMAP_VA_BITS - PAGE_SHIFT - 3;
796 }
797
798 /*
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.
803 */
set_satp_mode(uintptr_t dtb_pa)804 static __init void set_satp_mode(uintptr_t dtb_pa)
805 {
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);
809
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();
815 return;
816 }
817
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);
832 retry:
833 create_pgd_mapping(early_pg_dir,
834 set_satp_mode_pmd,
835 pgtable_l5_enabled ?
836 (uintptr_t)early_p4d : (uintptr_t)early_pud,
837 PGDIR_SIZE, PAGE_TABLE);
838
839 identity_satp = PFN_DOWN((uintptr_t)&early_pg_dir) | satp_mode;
840
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();
845
846 if (hw_satp != identity_satp) {
847 if (pgtable_l5_enabled) {
848 disable_pgtable_l5();
849 memset(early_pg_dir, 0, PAGE_SIZE);
850 goto retry;
851 }
852 disable_pgtable_l4();
853 }
854
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);
859 }
860 #endif
861
862 /*
863 * setup_vm() is called from head.S with MMU-off.
864 *
865 * Following requirements should be honoured for setup_vm() to work
866 * correctly:
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.
871 *
872 * Currently, the above requirements are honoured by using custom CFLAGS
873 * for init.o in mm/Makefile.
874 */
875
876 #ifndef __riscv_cmodel_medany
877 #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
878 #endif
879
880 #ifdef CONFIG_RELOCATABLE
881 extern unsigned long __rela_dyn_start, __rela_dyn_end;
882
relocate_kernel(void)883 static void __init relocate_kernel(void)
884 {
885 Elf64_Rela *rela = (Elf64_Rela *)&__rela_dyn_start;
886 /*
887 * This holds the offset between the linked virtual address and the
888 * relocated virtual address.
889 */
890 uintptr_t reloc_offset = kernel_map.virt_addr - KERNEL_LINK_ADDR;
891 /*
892 * This holds the offset between kernel linked virtual address and
893 * physical address.
894 */
895 uintptr_t va_kernel_link_pa_offset = KERNEL_LINK_ADDR - kernel_map.phys_addr;
896
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;
900
901 if (rela->r_info != R_RISCV_RELATIVE)
902 continue;
903
904 /*
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.
909 */
910 if (relocated_addr >= KERNEL_LINK_ADDR)
911 relocated_addr += reloc_offset;
912
913 *(Elf64_Addr *)addr = relocated_addr;
914 }
915 }
916 #endif /* CONFIG_RELOCATABLE */
917
918 #ifdef CONFIG_XIP_KERNEL
create_kernel_page_table(pgd_t * pgdir,__always_unused bool early)919 static void __init create_kernel_page_table(pgd_t *pgdir,
920 __always_unused bool early)
921 {
922 uintptr_t va, start_va, end_va;
923
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);
930
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);
938 }
939 #else
create_kernel_page_table(pgd_t * pgdir,bool early)940 static void __init create_kernel_page_table(pgd_t *pgdir, bool early)
941 {
942 uintptr_t va, end_va;
943
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),
948 PMD_SIZE,
949 early ?
950 PAGE_KERNEL_EXEC : pgprot_from_va(va));
951 }
952 #endif
953
954 /*
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
957 * entry.
958 */
create_fdt_early_page_table(uintptr_t fix_fdt_va,uintptr_t dtb_pa)959 static void __init create_fdt_early_page_table(uintptr_t fix_fdt_va,
960 uintptr_t dtb_pa)
961 {
962 #ifndef CONFIG_BUILTIN_DTB
963 uintptr_t pa = dtb_pa & ~(PMD_SIZE - 1);
964
965 /* Make sure the fdt fixmap address is always aligned on PMD size */
966 BUILD_BUG_ON(FIX_FDT % (PMD_SIZE / PAGE_SIZE));
967
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);
972 } else {
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);
977 }
978
979 dtb_early_va = (void *)fix_fdt_va + (dtb_pa & (PMD_SIZE - 1));
980 #else
981 /*
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.
986 */
987 dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
988 #endif
989
990 dtb_early_pa = dtb_pa;
991 }
992
993 /*
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.
996 */
pt_ops_set_early(void)997 static void __init pt_ops_set_early(void)
998 {
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;
1008 #endif
1009 }
1010
1011 /*
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.
1015 *
1016 * Note that this is called with MMU disabled, hence kernel_mapping_pa_to_va,
1017 * but it will be used as described above.
1018 */
pt_ops_set_fixmap(void)1019 static void __init pt_ops_set_fixmap(void)
1020 {
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);
1030 #endif
1031 }
1032
1033 /*
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.
1036 */
pt_ops_set_late(void)1037 static void __init pt_ops_set_late(void)
1038 {
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;
1048 #endif
1049 }
1050
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);
1055
print_nokaslr(char * p)1056 static int __init print_nokaslr(char *p)
1057 {
1058 pr_info("Disabled KASLR");
1059 return 0;
1060 }
1061 early_param("nokaslr", print_nokaslr);
1062
kaslr_offset(void)1063 unsigned long kaslr_offset(void)
1064 {
1065 return kernel_map.virt_offset;
1066 }
1067 #endif
1068
setup_vm(uintptr_t dtb_pa)1069 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
1070 {
1071 pmd_t __maybe_unused fix_bmap_spmd, fix_bmap_epmd;
1072
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);
1077 u32 nr_pos;
1078
1079 if (kaslr_seed == 0)
1080 kaslr_seed = __pi_get_kaslr_seed(dtb_pa);
1081 /*
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.
1085 */
1086 nr_pos = (PUD_SIZE - kernel_size) / PMD_SIZE;
1087
1088 kernel_map.virt_offset = (kaslr_seed % nr_pos) * PMD_SIZE;
1089 }
1090 #endif
1091
1092 kernel_map.virt_addr = KERNEL_LINK_ADDR + kernel_map.virt_offset;
1093
1094 #ifdef CONFIG_XIP_KERNEL
1095 #ifdef CONFIG_64BIT
1096 kernel_map.page_offset = PAGE_OFFSET_L3;
1097 #else
1098 kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL);
1099 #endif
1100 kernel_map.xiprom = (uintptr_t)CONFIG_XIP_PHYS_ADDR;
1101 kernel_map.xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom);
1102
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);
1106
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;
1110 #else
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;
1115 #endif
1116
1117 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
1118 set_satp_mode(dtb_pa);
1119 set_mmap_rnd_bits_max();
1120 #endif
1121
1122 /*
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).
1133 */
1134 kernel_map.va_pa_offset = IS_ENABLED(CONFIG_64BIT) ?
1135 0UL : PAGE_OFFSET - kernel_map.phys_addr;
1136
1137 memory_limit = KERN_VIRT_SIZE;
1138
1139 /* Sanity check alignment and size */
1140 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
1141 BUG_ON((kernel_map.phys_addr % PMD_SIZE) != 0);
1142
1143 #ifdef CONFIG_64BIT
1144 /*
1145 * The last 4K bytes of the addressable memory can not be mapped because
1146 * of IS_ERR_VALUE macro.
1147 */
1148 BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K);
1149 #endif
1150
1151 #ifdef CONFIG_RELOCATABLE
1152 /*
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.
1157 */
1158 BUG_ON(PUD_SIZE - (kernel_map.virt_addr & (PUD_SIZE - 1)) < kernel_map.size);
1159 relocate_kernel();
1160 #endif
1161
1162 apply_early_boot_alternatives();
1163 pt_ops_set_early();
1164
1165 /* Setup early PGD for fixmap */
1166 create_pgd_mapping(early_pg_dir, FIXADDR_START,
1167 fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
1168
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);
1192 #else
1193 create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr,
1194 kernel_map.phys_addr, PMD_SIZE, PAGE_KERNEL_EXEC);
1195 #endif
1196 #else
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);
1200 #endif
1201
1202 /*
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.
1206 */
1207 create_kernel_page_table(early_pg_dir, true);
1208
1209 /* Setup early mapping for FDT early scan */
1210 create_fdt_early_page_table(__fix_to_virt(FIX_FDT), dtb_pa);
1211
1212 /*
1213 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
1214 * range can not span multiple pmds.
1215 */
1216 BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
1217 != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
1218
1219 #ifndef __PAGETABLE_PMD_FOLDED
1220 /*
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
1224 * the user if not.
1225 */
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)) {
1229 WARN_ON(1);
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));
1236
1237 pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
1238 pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
1239 }
1240 #endif
1241
1242 pt_ops_set_fixmap();
1243 }
1244
create_linear_mapping_range(phys_addr_t start,phys_addr_t end,uintptr_t fixed_map_size,const pgprot_t * pgprot)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)
1247 {
1248 phys_addr_t pa;
1249 uintptr_t va, map_size;
1250
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);
1255
1256 create_pgd_mapping(swapper_pg_dir, va, pa, map_size,
1257 pgprot ? *pgprot : pgprot_from_va(va));
1258 }
1259 }
1260
create_linear_mapping_page_table(void)1261 static void __init create_linear_mapping_page_table(void)
1262 {
1263 phys_addr_t start, end;
1264 phys_addr_t kfence_pool __maybe_unused;
1265 u64 i;
1266
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;
1272
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);
1276 #endif
1277
1278 #ifdef CONFIG_KFENCE
1279 /*
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
1282 * for this region.
1283 */
1284 kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
1285 BUG_ON(!kfence_pool);
1286
1287 memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE);
1288 __kfence_pool = __va(kfence_pool);
1289 #endif
1290
1291 /* Map all memory banks in the linear mapping */
1292 for_each_mem_range(i, &start, &end) {
1293 if (start >= end)
1294 break;
1295 if (start <= __pa(PAGE_OFFSET) &&
1296 __pa(PAGE_OFFSET) < end)
1297 start = __pa(PAGE_OFFSET);
1298
1299 create_linear_mapping_range(start, end, 0, NULL);
1300 }
1301
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);
1305
1306 memblock_clear_nomap(ktext_start, ktext_size);
1307 memblock_clear_nomap(krodata_start, krodata_size);
1308 #endif
1309
1310 #ifdef CONFIG_KFENCE
1311 create_linear_mapping_range(kfence_pool, kfence_pool + KFENCE_POOL_SIZE, PAGE_SIZE, NULL);
1312
1313 memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE);
1314 #endif
1315 }
1316
setup_vm_final(void)1317 static void __init setup_vm_final(void)
1318 {
1319 /* Setup swapper PGD for fixmap */
1320 #if !defined(CONFIG_64BIT)
1321 /*
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
1324 * to fixmap_pte.
1325 */
1326 unsigned long idx = pgd_index(__fix_to_virt(FIX_FDT));
1327
1328 set_pgd(&swapper_pg_dir[idx], early_pg_dir[idx]);
1329 #endif
1330 create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
1331 __pa_symbol(fixmap_pgd_next),
1332 PGDIR_SIZE, PAGE_TABLE);
1333
1334 /* Map the linear mapping */
1335 create_linear_mapping_page_table();
1336
1337 /* Map the kernel */
1338 if (IS_ENABLED(CONFIG_64BIT))
1339 create_kernel_page_table(swapper_pg_dir, false);
1340
1341 #ifdef CONFIG_KASAN
1342 kasan_swapper_init();
1343 #endif
1344
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);
1350
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();
1354
1355 pt_ops_set_late();
1356 }
1357 #else
setup_vm(uintptr_t dtb_pa)1358 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
1359 {
1360 dtb_early_va = (void *)dtb_pa;
1361 dtb_early_pa = dtb_pa;
1362 }
1363
setup_vm_final(void)1364 static inline void setup_vm_final(void)
1365 {
1366 }
1367 #endif /* CONFIG_MMU */
1368
1369 /*
1370 * reserve_crashkernel() - reserves memory for crash kernel
1371 *
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.
1375 */
arch_reserve_crashkernel(void)1376 static void __init arch_reserve_crashkernel(void)
1377 {
1378 unsigned long long low_size = 0;
1379 unsigned long long crash_base, crash_size;
1380 char *cmdline = boot_command_line;
1381 bool high = false;
1382 int ret;
1383
1384 if (!IS_ENABLED(CONFIG_CRASH_RESERVE))
1385 return;
1386
1387 ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
1388 &crash_size, &crash_base,
1389 &low_size, &high);
1390 if (ret)
1391 return;
1392
1393 reserve_crashkernel_generic(cmdline, crash_size, crash_base,
1394 low_size, high);
1395 }
1396
paging_init(void)1397 void __init paging_init(void)
1398 {
1399 setup_bootmem();
1400 setup_vm_final();
1401
1402 /* Depend on that Linear Mapping is ready */
1403 memblock_allow_resize();
1404 }
1405
misc_mem_init(void)1406 void __init misc_mem_init(void)
1407 {
1408 early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
1409 arch_numa_init();
1410 sparse_init();
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);
1414 #endif
1415 zone_sizes_init();
1416 arch_reserve_crashkernel();
1417 memblock_dump_all();
1418 }
1419
1420 #ifdef CONFIG_SPARSEMEM_VMEMMAP
vmemmap_set_pmd(pmd_t * pmd,void * p,int node,unsigned long addr,unsigned long next)1421 void __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node,
1422 unsigned long addr, unsigned long next)
1423 {
1424 pmd_set_huge(pmd, virt_to_phys(p), PAGE_KERNEL);
1425 }
1426
vmemmap_check_pmd(pmd_t * pmdp,int node,unsigned long addr,unsigned long next)1427 int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node,
1428 unsigned long addr, unsigned long next)
1429 {
1430 vmemmap_verify((pte_t *)pmdp, node, addr, next);
1431 return 1;
1432 }
1433
vmemmap_populate(unsigned long start,unsigned long end,int node,struct vmem_altmap * altmap)1434 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1435 struct vmem_altmap *altmap)
1436 {
1437 /*
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
1441 * the new PMDs.
1442 */
1443 return vmemmap_populate_hugepages(start, end, node, altmap);
1444 }
1445 #endif
1446
1447 #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
1448 /*
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
1452 * expensive.
1453 */
preallocate_pgd_pages_range(unsigned long start,unsigned long end,const char * area)1454 static void __init preallocate_pgd_pages_range(unsigned long start, unsigned long end,
1455 const char *area)
1456 {
1457 unsigned long addr;
1458 const char *lvl;
1459
1460 for (addr = start; addr < end && addr >= start; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
1461 pgd_t *pgd = pgd_offset_k(addr);
1462 p4d_t *p4d;
1463 pud_t *pud;
1464 pmd_t *pmd;
1465
1466 lvl = "p4d";
1467 p4d = p4d_alloc(&init_mm, pgd, addr);
1468 if (!p4d)
1469 goto failed;
1470
1471 if (pgtable_l5_enabled)
1472 continue;
1473
1474 lvl = "pud";
1475 pud = pud_alloc(&init_mm, p4d, addr);
1476 if (!pud)
1477 goto failed;
1478
1479 if (pgtable_l4_enabled)
1480 continue;
1481
1482 lvl = "pmd";
1483 pmd = pmd_alloc(&init_mm, pud, addr);
1484 if (!pmd)
1485 goto failed;
1486 }
1487 return;
1488
1489 failed:
1490 /*
1491 * The pages have to be there now or they will be missing in
1492 * process page-tables later.
1493 */
1494 panic("Failed to pre-allocate %s pages for %s area\n", lvl, area);
1495 }
1496
1497 #define PAGE_END KASAN_SHADOW_START
1498
pgtable_cache_init(void)1499 void __init pgtable_cache_init(void)
1500 {
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");
1509 }
1510 }
1511 #endif
1512
1513 #ifdef CONFIG_EXECMEM
1514 #ifdef CONFIG_MMU
1515 static struct execmem_info execmem_info __ro_after_init;
1516
execmem_arch_setup(void)1517 struct execmem_info __init *execmem_arch_setup(void)
1518 {
1519 execmem_info = (struct execmem_info){
1520 .ranges = {
1521 [EXECMEM_DEFAULT] = {
1522 .start = MODULES_VADDR,
1523 .end = MODULES_END,
1524 .pgprot = PAGE_KERNEL,
1525 .alignment = 1,
1526 },
1527 [EXECMEM_KPROBES] = {
1528 .start = VMALLOC_START,
1529 .end = VMALLOC_END,
1530 .pgprot = PAGE_KERNEL_READ_EXEC,
1531 .alignment = 1,
1532 },
1533 [EXECMEM_BPF] = {
1534 .start = BPF_JIT_REGION_START,
1535 .end = BPF_JIT_REGION_END,
1536 .pgprot = PAGE_KERNEL,
1537 .alignment = PAGE_SIZE,
1538 },
1539 },
1540 };
1541
1542 return &execmem_info;
1543 }
1544 #endif /* CONFIG_MMU */
1545 #endif /* CONFIG_EXECMEM */
1546
1547 #ifdef CONFIG_MEMORY_HOTPLUG
free_pte_table(pte_t * pte_start,pmd_t * pmd)1548 static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
1549 {
1550 struct page *page = pmd_page(*pmd);
1551 struct ptdesc *ptdesc = page_ptdesc(page);
1552 pte_t *pte;
1553 int i;
1554
1555 for (i = 0; i < PTRS_PER_PTE; i++) {
1556 pte = pte_start + i;
1557 if (!pte_none(*pte))
1558 return;
1559 }
1560
1561 pagetable_pte_dtor(ptdesc);
1562 if (PageReserved(page))
1563 free_reserved_page(page);
1564 else
1565 pagetable_free(ptdesc);
1566 pmd_clear(pmd);
1567 }
1568
free_pmd_table(pmd_t * pmd_start,pud_t * pud)1569 static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
1570 {
1571 struct page *page = pud_page(*pud);
1572 struct ptdesc *ptdesc = page_ptdesc(page);
1573 pmd_t *pmd;
1574 int i;
1575
1576 for (i = 0; i < PTRS_PER_PMD; i++) {
1577 pmd = pmd_start + i;
1578 if (!pmd_none(*pmd))
1579 return;
1580 }
1581
1582 pagetable_pmd_dtor(ptdesc);
1583 if (PageReserved(page))
1584 free_reserved_page(page);
1585 else
1586 pagetable_free(ptdesc);
1587 pud_clear(pud);
1588 }
1589
free_pud_table(pud_t * pud_start,p4d_t * p4d)1590 static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d)
1591 {
1592 struct page *page = p4d_page(*p4d);
1593 pud_t *pud;
1594 int i;
1595
1596 for (i = 0; i < PTRS_PER_PUD; i++) {
1597 pud = pud_start + i;
1598 if (!pud_none(*pud))
1599 return;
1600 }
1601
1602 if (PageReserved(page))
1603 free_reserved_page(page);
1604 else
1605 free_pages((unsigned long)page_address(page), 0);
1606 p4d_clear(p4d);
1607 }
1608
free_vmemmap_storage(struct page * page,size_t size,struct vmem_altmap * altmap)1609 static void __meminit free_vmemmap_storage(struct page *page, size_t size,
1610 struct vmem_altmap *altmap)
1611 {
1612 int order = get_order(size);
1613
1614 if (altmap) {
1615 vmem_altmap_free(altmap, size >> PAGE_SHIFT);
1616 return;
1617 }
1618
1619 if (PageReserved(page)) {
1620 unsigned int nr_pages = 1 << order;
1621
1622 while (nr_pages--)
1623 free_reserved_page(page++);
1624 return;
1625 }
1626
1627 free_pages((unsigned long)page_address(page), order);
1628 }
1629
remove_pte_mapping(pte_t * pte_base,unsigned long addr,unsigned long end,bool is_vmemmap,struct vmem_altmap * altmap)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)
1632 {
1633 unsigned long next;
1634 pte_t *ptep, pte;
1635
1636 for (; addr < end; addr = next) {
1637 next = (addr + PAGE_SIZE) & PAGE_MASK;
1638 if (next > end)
1639 next = end;
1640
1641 ptep = pte_base + pte_index(addr);
1642 pte = ptep_get(ptep);
1643 if (!pte_present(*ptep))
1644 continue;
1645
1646 pte_clear(&init_mm, addr, ptep);
1647 if (is_vmemmap)
1648 free_vmemmap_storage(pte_page(pte), PAGE_SIZE, altmap);
1649 }
1650 }
1651
remove_pmd_mapping(pmd_t * pmd_base,unsigned long addr,unsigned long end,bool is_vmemmap,struct vmem_altmap * 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)
1654 {
1655 unsigned long next;
1656 pte_t *pte_base;
1657 pmd_t *pmdp, pmd;
1658
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))
1664 continue;
1665
1666 if (pmd_leaf(pmd)) {
1667 pmd_clear(pmdp);
1668 if (is_vmemmap)
1669 free_vmemmap_storage(pmd_page(pmd), PMD_SIZE, altmap);
1670 continue;
1671 }
1672
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);
1676 }
1677 }
1678
remove_pud_mapping(pud_t * pud_base,unsigned long addr,unsigned long end,bool is_vmemmap,struct vmem_altmap * altmap)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)
1681 {
1682 unsigned long next;
1683 pud_t *pudp, pud;
1684 pmd_t *pmd_base;
1685
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))
1691 continue;
1692
1693 if (pud_leaf(pud)) {
1694 if (pgtable_l4_enabled) {
1695 pud_clear(pudp);
1696 if (is_vmemmap)
1697 free_vmemmap_storage(pud_page(pud), PUD_SIZE, altmap);
1698 }
1699 continue;
1700 }
1701
1702 pmd_base = pmd_offset(pudp, 0);
1703 remove_pmd_mapping(pmd_base, addr, next, is_vmemmap, altmap);
1704
1705 if (pgtable_l4_enabled)
1706 free_pmd_table(pmd_base, pudp);
1707 }
1708 }
1709
remove_p4d_mapping(p4d_t * p4d_base,unsigned long addr,unsigned long end,bool is_vmemmap,struct vmem_altmap * altmap)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)
1712 {
1713 unsigned long next;
1714 p4d_t *p4dp, p4d;
1715 pud_t *pud_base;
1716
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))
1722 continue;
1723
1724 if (p4d_leaf(p4d)) {
1725 if (pgtable_l5_enabled) {
1726 p4d_clear(p4dp);
1727 if (is_vmemmap)
1728 free_vmemmap_storage(p4d_page(p4d), P4D_SIZE, altmap);
1729 }
1730 continue;
1731 }
1732
1733 pud_base = pud_offset(p4dp, 0);
1734 remove_pud_mapping(pud_base, addr, next, is_vmemmap, altmap);
1735
1736 if (pgtable_l5_enabled)
1737 free_pud_table(pud_base, p4dp);
1738 }
1739 }
1740
remove_pgd_mapping(unsigned long va,unsigned long end,bool is_vmemmap,struct vmem_altmap * altmap)1741 static void __meminit remove_pgd_mapping(unsigned long va, unsigned long end, bool is_vmemmap,
1742 struct vmem_altmap *altmap)
1743 {
1744 unsigned long addr, next;
1745 p4d_t *p4d_base;
1746 pgd_t *pgd;
1747
1748 for (addr = va; addr < end; addr = next) {
1749 next = pgd_addr_end(addr, end);
1750 pgd = pgd_offset_k(addr);
1751
1752 if (!pgd_present(*pgd))
1753 continue;
1754
1755 if (pgd_leaf(*pgd))
1756 continue;
1757
1758 p4d_base = p4d_offset(pgd, 0);
1759 remove_p4d_mapping(p4d_base, addr, next, is_vmemmap, altmap);
1760 }
1761
1762 flush_tlb_all();
1763 }
1764
remove_linear_mapping(phys_addr_t start,u64 size)1765 static void __meminit remove_linear_mapping(phys_addr_t start, u64 size)
1766 {
1767 unsigned long va = (unsigned long)__va(start);
1768 unsigned long end = (unsigned long)__va(start + size);
1769
1770 remove_pgd_mapping(va, end, false, NULL);
1771 }
1772
arch_get_mappable_range(void)1773 struct range arch_get_mappable_range(void)
1774 {
1775 struct range mhp_range;
1776
1777 mhp_range.start = __pa(PAGE_OFFSET);
1778 mhp_range.end = __pa(PAGE_END - 1);
1779 return mhp_range;
1780 }
1781
arch_add_memory(int nid,u64 start,u64 size,struct mhp_params * params)1782 int __ref arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params)
1783 {
1784 int ret = 0;
1785
1786 create_linear_mapping_range(start, start + size, 0, ¶ms->pgprot);
1787 ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT, params);
1788 if (ret) {
1789 remove_linear_mapping(start, size);
1790 goto out;
1791 }
1792
1793 max_pfn = PFN_UP(start + size);
1794 max_low_pfn = max_pfn;
1795
1796 out:
1797 flush_tlb_all();
1798 return ret;
1799 }
1800
arch_remove_memory(u64 start,u64 size,struct vmem_altmap * altmap)1801 void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
1802 {
1803 __remove_pages(start >> PAGE_SHIFT, size >> PAGE_SHIFT, altmap);
1804 remove_linear_mapping(start, size);
1805 flush_tlb_all();
1806 }
1807
vmemmap_free(unsigned long start,unsigned long end,struct vmem_altmap * altmap)1808 void __ref vmemmap_free(unsigned long start, unsigned long end, struct vmem_altmap *altmap)
1809 {
1810 remove_pgd_mapping(start, end, true, altmap);
1811 }
1812 #endif /* CONFIG_MEMORY_HOTPLUG */
1813