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
28 #include <asm/fixmap.h>
29 #include <asm/io.h>
30 #include <asm/numa.h>
31 #include <asm/pgtable.h>
32 #include <asm/ptdump.h>
33 #include <asm/sections.h>
34 #include <asm/soc.h>
35 #include <asm/sparsemem.h>
36 #include <asm/tlbflush.h>
37
38 #include "../kernel/head.h"
39
40 struct kernel_mapping kernel_map __ro_after_init;
41 EXPORT_SYMBOL(kernel_map);
42 #ifdef CONFIG_XIP_KERNEL
43 #define kernel_map (*(struct kernel_mapping *)XIP_FIXUP(&kernel_map))
44 #endif
45
46 #ifdef CONFIG_64BIT
47 u64 satp_mode __ro_after_init = !IS_ENABLED(CONFIG_XIP_KERNEL) ? SATP_MODE_57 : SATP_MODE_39;
48 #else
49 u64 satp_mode __ro_after_init = SATP_MODE_32;
50 #endif
51 EXPORT_SYMBOL(satp_mode);
52
53 bool pgtable_l4_enabled = IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_XIP_KERNEL);
54 bool pgtable_l5_enabled = IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_XIP_KERNEL);
55 EXPORT_SYMBOL(pgtable_l4_enabled);
56 EXPORT_SYMBOL(pgtable_l5_enabled);
57
58 phys_addr_t phys_ram_base __ro_after_init;
59 EXPORT_SYMBOL(phys_ram_base);
60
61 #ifdef CONFIG_SPARSEMEM_VMEMMAP
62 #define VMEMMAP_ADDR_ALIGN (1ULL << SECTION_SIZE_BITS)
63
64 unsigned long vmemmap_start_pfn __ro_after_init;
65 EXPORT_SYMBOL(vmemmap_start_pfn);
66 #endif
67
68 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
69 __page_aligned_bss;
70 EXPORT_SYMBOL(empty_zero_page);
71
72 extern char _start[];
73 void *_dtb_early_va __initdata;
74 uintptr_t _dtb_early_pa __initdata;
75
76 static phys_addr_t dma32_phys_limit __initdata;
77
zone_sizes_init(void)78 static void __init zone_sizes_init(void)
79 {
80 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
81
82 #ifdef CONFIG_ZONE_DMA32
83 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
84 #endif
85 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
86
87 free_area_init(max_zone_pfns);
88 }
89
90 #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
91
92 #define LOG2_SZ_1K ilog2(SZ_1K)
93 #define LOG2_SZ_1M ilog2(SZ_1M)
94 #define LOG2_SZ_1G ilog2(SZ_1G)
95 #define LOG2_SZ_1T ilog2(SZ_1T)
96
print_mlk(char * name,unsigned long b,unsigned long t)97 static inline void print_mlk(char *name, unsigned long b, unsigned long t)
98 {
99 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t,
100 (((t) - (b)) >> LOG2_SZ_1K));
101 }
102
print_mlm(char * name,unsigned long b,unsigned long t)103 static inline void print_mlm(char *name, unsigned long b, unsigned long t)
104 {
105 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t,
106 (((t) - (b)) >> LOG2_SZ_1M));
107 }
108
print_mlg(char * name,unsigned long b,unsigned long t)109 static inline void print_mlg(char *name, unsigned long b, unsigned long t)
110 {
111 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld GB)\n", name, b, t,
112 (((t) - (b)) >> LOG2_SZ_1G));
113 }
114
115 #ifdef CONFIG_64BIT
print_mlt(char * name,unsigned long b,unsigned long t)116 static inline void print_mlt(char *name, unsigned long b, unsigned long t)
117 {
118 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld TB)\n", name, b, t,
119 (((t) - (b)) >> LOG2_SZ_1T));
120 }
121 #else
122 #define print_mlt(n, b, t) do {} while (0)
123 #endif
124
print_ml(char * name,unsigned long b,unsigned long t)125 static inline void print_ml(char *name, unsigned long b, unsigned long t)
126 {
127 unsigned long diff = t - b;
128
129 if (IS_ENABLED(CONFIG_64BIT) && (diff >> LOG2_SZ_1T) >= 10)
130 print_mlt(name, b, t);
131 else if ((diff >> LOG2_SZ_1G) >= 10)
132 print_mlg(name, b, t);
133 else if ((diff >> LOG2_SZ_1M) >= 10)
134 print_mlm(name, b, t);
135 else
136 print_mlk(name, b, t);
137 }
138
print_vm_layout(void)139 static void __init print_vm_layout(void)
140 {
141 pr_notice("Virtual kernel memory layout:\n");
142 print_ml("fixmap", (unsigned long)FIXADDR_START,
143 (unsigned long)FIXADDR_TOP);
144 print_ml("pci io", (unsigned long)PCI_IO_START,
145 (unsigned long)PCI_IO_END);
146 print_ml("vmemmap", (unsigned long)VMEMMAP_START,
147 (unsigned long)VMEMMAP_END);
148 print_ml("vmalloc", (unsigned long)VMALLOC_START,
149 (unsigned long)VMALLOC_END);
150 #ifdef CONFIG_64BIT
151 print_ml("modules", (unsigned long)MODULES_VADDR,
152 (unsigned long)MODULES_END);
153 #endif
154 print_ml("lowmem", (unsigned long)PAGE_OFFSET,
155 (unsigned long)high_memory);
156 if (IS_ENABLED(CONFIG_64BIT)) {
157 #ifdef CONFIG_KASAN
158 print_ml("kasan", KASAN_SHADOW_START, KASAN_SHADOW_END);
159 #endif
160
161 print_ml("kernel", (unsigned long)kernel_map.virt_addr,
162 (unsigned long)ADDRESS_SPACE_END);
163 }
164 }
165 #else
print_vm_layout(void)166 static void print_vm_layout(void) { }
167 #endif /* CONFIG_DEBUG_VM */
168
mem_init(void)169 void __init mem_init(void)
170 {
171 #ifdef CONFIG_FLATMEM
172 BUG_ON(!mem_map);
173 #endif /* CONFIG_FLATMEM */
174
175 swiotlb_init(max_pfn > PFN_DOWN(dma32_phys_limit), SWIOTLB_VERBOSE);
176 memblock_free_all();
177
178 print_vm_layout();
179 }
180
181 /* Limit the memory size via mem. */
182 static phys_addr_t memory_limit;
183 #ifdef CONFIG_XIP_KERNEL
184 #define memory_limit (*(phys_addr_t *)XIP_FIXUP(&memory_limit))
185 #endif /* CONFIG_XIP_KERNEL */
186
early_mem(char * p)187 static int __init early_mem(char *p)
188 {
189 u64 size;
190
191 if (!p)
192 return 1;
193
194 size = memparse(p, &p) & PAGE_MASK;
195 memory_limit = min_t(u64, size, memory_limit);
196
197 pr_notice("Memory limited to %lldMB\n", (u64)memory_limit >> 20);
198
199 return 0;
200 }
201 early_param("mem", early_mem);
202
setup_bootmem(void)203 static void __init setup_bootmem(void)
204 {
205 phys_addr_t vmlinux_end = __pa_symbol(&_end);
206 phys_addr_t max_mapped_addr;
207 phys_addr_t phys_ram_end, vmlinux_start;
208
209 if (IS_ENABLED(CONFIG_XIP_KERNEL))
210 vmlinux_start = __pa_symbol(&_sdata);
211 else
212 vmlinux_start = __pa_symbol(&_start);
213
214 memblock_enforce_memory_limit(memory_limit);
215
216 /*
217 * Make sure we align the reservation on PMD_SIZE since we will
218 * map the kernel in the linear mapping as read-only: we do not want
219 * any allocation to happen between _end and the next pmd aligned page.
220 */
221 if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
222 vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK;
223 /*
224 * Reserve from the start of the kernel to the end of the kernel
225 */
226 memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
227
228 /*
229 * Make sure we align the start of the memory on a PMD boundary so that
230 * at worst, we map the linear mapping with PMD mappings.
231 */
232 if (!IS_ENABLED(CONFIG_XIP_KERNEL)) {
233 phys_ram_base = memblock_start_of_DRAM() & PMD_MASK;
234 #ifdef CONFIG_SPARSEMEM_VMEMMAP
235 vmemmap_start_pfn = round_down(phys_ram_base, VMEMMAP_ADDR_ALIGN) >> PAGE_SHIFT;
236 #endif
237 }
238
239 /*
240 * In 64-bit, any use of __va/__pa before this point is wrong as we
241 * did not know the start of DRAM before.
242 */
243 if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU))
244 kernel_map.va_pa_offset = PAGE_OFFSET - phys_ram_base;
245
246 /*
247 * The size of the linear page mapping may restrict the amount of
248 * usable RAM.
249 */
250 if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU)) {
251 max_mapped_addr = __pa(PAGE_OFFSET) + KERN_VIRT_SIZE;
252 memblock_cap_memory_range(phys_ram_base,
253 max_mapped_addr - phys_ram_base);
254 }
255
256 /*
257 * Reserve physical address space that would be mapped to virtual
258 * addresses greater than (void *)(-PAGE_SIZE) because:
259 * - This memory would overlap with ERR_PTR
260 * - This memory belongs to high memory, which is not supported
261 *
262 * This is not applicable to 64-bit kernel, because virtual addresses
263 * after (void *)(-PAGE_SIZE) are not linearly mapped: they are
264 * occupied by kernel mapping. Also it is unrealistic for high memory
265 * to exist on 64-bit platforms.
266 */
267 if (!IS_ENABLED(CONFIG_64BIT)) {
268 max_mapped_addr = __va_to_pa_nodebug(-PAGE_SIZE);
269 memblock_reserve(max_mapped_addr, (phys_addr_t)-max_mapped_addr);
270 }
271
272 phys_ram_end = memblock_end_of_DRAM();
273 min_low_pfn = PFN_UP(phys_ram_base);
274 max_low_pfn = max_pfn = PFN_DOWN(phys_ram_end);
275 high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
276
277 dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn));
278 set_max_mapnr(max_low_pfn - ARCH_PFN_OFFSET);
279
280 reserve_initrd_mem();
281
282 /*
283 * No allocation should be done before reserving the memory as defined
284 * in the device tree, otherwise the allocation could end up in a
285 * reserved region.
286 */
287 early_init_fdt_scan_reserved_mem();
288
289 /*
290 * If DTB is built in, no need to reserve its memblock.
291 * Otherwise, do reserve it but avoid using
292 * early_init_fdt_reserve_self() since __pa() does
293 * not work for DTB pointers that are fixmap addresses
294 */
295 if (!IS_ENABLED(CONFIG_BUILTIN_DTB))
296 memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
297
298 dma_contiguous_reserve(dma32_phys_limit);
299 if (IS_ENABLED(CONFIG_64BIT))
300 hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
301 }
302
303 #ifdef CONFIG_MMU
304 struct pt_alloc_ops pt_ops __initdata;
305
306 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
307 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
308 static pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
309
310 pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
311
312 #ifdef CONFIG_XIP_KERNEL
313 #define pt_ops (*(struct pt_alloc_ops *)XIP_FIXUP(&pt_ops))
314 #define trampoline_pg_dir ((pgd_t *)XIP_FIXUP(trampoline_pg_dir))
315 #define fixmap_pte ((pte_t *)XIP_FIXUP(fixmap_pte))
316 #define early_pg_dir ((pgd_t *)XIP_FIXUP(early_pg_dir))
317 #endif /* CONFIG_XIP_KERNEL */
318
319 static const pgprot_t protection_map[16] = {
320 [VM_NONE] = PAGE_NONE,
321 [VM_READ] = PAGE_READ,
322 [VM_WRITE] = PAGE_COPY,
323 [VM_WRITE | VM_READ] = PAGE_COPY,
324 [VM_EXEC] = PAGE_EXEC,
325 [VM_EXEC | VM_READ] = PAGE_READ_EXEC,
326 [VM_EXEC | VM_WRITE] = PAGE_COPY_EXEC,
327 [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_EXEC,
328 [VM_SHARED] = PAGE_NONE,
329 [VM_SHARED | VM_READ] = PAGE_READ,
330 [VM_SHARED | VM_WRITE] = PAGE_SHARED,
331 [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
332 [VM_SHARED | VM_EXEC] = PAGE_EXEC,
333 [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READ_EXEC,
334 [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED_EXEC,
335 [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_EXEC
336 };
337 DECLARE_VM_GET_PAGE_PROT
338
__set_fixmap(enum fixed_addresses idx,phys_addr_t phys,pgprot_t prot)339 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
340 {
341 unsigned long addr = __fix_to_virt(idx);
342 pte_t *ptep;
343
344 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
345
346 ptep = &fixmap_pte[pte_index(addr)];
347
348 if (pgprot_val(prot))
349 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
350 else
351 pte_clear(&init_mm, addr, ptep);
352 local_flush_tlb_page(addr);
353 }
354
get_pte_virt_early(phys_addr_t pa)355 static inline pte_t *__init get_pte_virt_early(phys_addr_t pa)
356 {
357 return (pte_t *)((uintptr_t)pa);
358 }
359
get_pte_virt_fixmap(phys_addr_t pa)360 static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa)
361 {
362 clear_fixmap(FIX_PTE);
363 return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
364 }
365
get_pte_virt_late(phys_addr_t pa)366 static inline pte_t *__init get_pte_virt_late(phys_addr_t pa)
367 {
368 return (pte_t *) __va(pa);
369 }
370
alloc_pte_early(uintptr_t va)371 static inline phys_addr_t __init alloc_pte_early(uintptr_t va)
372 {
373 /*
374 * We only create PMD or PGD early mappings so we
375 * should never reach here with MMU disabled.
376 */
377 BUG();
378 }
379
alloc_pte_fixmap(uintptr_t va)380 static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va)
381 {
382 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
383 }
384
alloc_pte_late(uintptr_t va)385 static phys_addr_t __init alloc_pte_late(uintptr_t va)
386 {
387 struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL & ~__GFP_HIGHMEM, 0);
388
389 BUG_ON(!ptdesc || !pagetable_pte_ctor(ptdesc));
390 return __pa((pte_t *)ptdesc_address(ptdesc));
391 }
392
create_pte_mapping(pte_t * ptep,uintptr_t va,phys_addr_t pa,phys_addr_t sz,pgprot_t prot)393 static void __init create_pte_mapping(pte_t *ptep,
394 uintptr_t va, phys_addr_t pa,
395 phys_addr_t sz, pgprot_t prot)
396 {
397 uintptr_t pte_idx = pte_index(va);
398
399 BUG_ON(sz != PAGE_SIZE);
400
401 if (pte_none(ptep[pte_idx]))
402 ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot);
403 }
404
405 #ifndef __PAGETABLE_PMD_FOLDED
406
407 static pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
408 static pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
409 static pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
410
411 #ifdef CONFIG_XIP_KERNEL
412 #define trampoline_pmd ((pmd_t *)XIP_FIXUP(trampoline_pmd))
413 #define fixmap_pmd ((pmd_t *)XIP_FIXUP(fixmap_pmd))
414 #define early_pmd ((pmd_t *)XIP_FIXUP(early_pmd))
415 #endif /* CONFIG_XIP_KERNEL */
416
417 static p4d_t trampoline_p4d[PTRS_PER_P4D] __page_aligned_bss;
418 static p4d_t fixmap_p4d[PTRS_PER_P4D] __page_aligned_bss;
419 static p4d_t early_p4d[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE);
420
421 #ifdef CONFIG_XIP_KERNEL
422 #define trampoline_p4d ((p4d_t *)XIP_FIXUP(trampoline_p4d))
423 #define fixmap_p4d ((p4d_t *)XIP_FIXUP(fixmap_p4d))
424 #define early_p4d ((p4d_t *)XIP_FIXUP(early_p4d))
425 #endif /* CONFIG_XIP_KERNEL */
426
427 static pud_t trampoline_pud[PTRS_PER_PUD] __page_aligned_bss;
428 static pud_t fixmap_pud[PTRS_PER_PUD] __page_aligned_bss;
429 static pud_t early_pud[PTRS_PER_PUD] __initdata __aligned(PAGE_SIZE);
430
431 #ifdef CONFIG_XIP_KERNEL
432 #define trampoline_pud ((pud_t *)XIP_FIXUP(trampoline_pud))
433 #define fixmap_pud ((pud_t *)XIP_FIXUP(fixmap_pud))
434 #define early_pud ((pud_t *)XIP_FIXUP(early_pud))
435 #endif /* CONFIG_XIP_KERNEL */
436
get_pmd_virt_early(phys_addr_t pa)437 static pmd_t *__init get_pmd_virt_early(phys_addr_t pa)
438 {
439 /* Before MMU is enabled */
440 return (pmd_t *)((uintptr_t)pa);
441 }
442
get_pmd_virt_fixmap(phys_addr_t pa)443 static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa)
444 {
445 clear_fixmap(FIX_PMD);
446 return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
447 }
448
get_pmd_virt_late(phys_addr_t pa)449 static pmd_t *__init get_pmd_virt_late(phys_addr_t pa)
450 {
451 return (pmd_t *) __va(pa);
452 }
453
alloc_pmd_early(uintptr_t va)454 static phys_addr_t __init alloc_pmd_early(uintptr_t va)
455 {
456 BUG_ON((va - kernel_map.virt_addr) >> PUD_SHIFT);
457
458 return (uintptr_t)early_pmd;
459 }
460
alloc_pmd_fixmap(uintptr_t va)461 static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va)
462 {
463 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
464 }
465
alloc_pmd_late(uintptr_t va)466 static phys_addr_t __init alloc_pmd_late(uintptr_t va)
467 {
468 struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL & ~__GFP_HIGHMEM, 0);
469
470 BUG_ON(!ptdesc || !pagetable_pmd_ctor(ptdesc));
471 return __pa((pmd_t *)ptdesc_address(ptdesc));
472 }
473
create_pmd_mapping(pmd_t * pmdp,uintptr_t va,phys_addr_t pa,phys_addr_t sz,pgprot_t prot)474 static void __init create_pmd_mapping(pmd_t *pmdp,
475 uintptr_t va, phys_addr_t pa,
476 phys_addr_t sz, pgprot_t prot)
477 {
478 pte_t *ptep;
479 phys_addr_t pte_phys;
480 uintptr_t pmd_idx = pmd_index(va);
481
482 if (sz == PMD_SIZE) {
483 if (pmd_none(pmdp[pmd_idx]))
484 pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot);
485 return;
486 }
487
488 if (pmd_none(pmdp[pmd_idx])) {
489 pte_phys = pt_ops.alloc_pte(va);
490 pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
491 ptep = pt_ops.get_pte_virt(pte_phys);
492 memset(ptep, 0, PAGE_SIZE);
493 } else {
494 pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx]));
495 ptep = pt_ops.get_pte_virt(pte_phys);
496 }
497
498 create_pte_mapping(ptep, va, pa, sz, prot);
499 }
500
get_pud_virt_early(phys_addr_t pa)501 static pud_t *__init get_pud_virt_early(phys_addr_t pa)
502 {
503 return (pud_t *)((uintptr_t)pa);
504 }
505
get_pud_virt_fixmap(phys_addr_t pa)506 static pud_t *__init get_pud_virt_fixmap(phys_addr_t pa)
507 {
508 clear_fixmap(FIX_PUD);
509 return (pud_t *)set_fixmap_offset(FIX_PUD, pa);
510 }
511
get_pud_virt_late(phys_addr_t pa)512 static pud_t *__init get_pud_virt_late(phys_addr_t pa)
513 {
514 return (pud_t *)__va(pa);
515 }
516
alloc_pud_early(uintptr_t va)517 static phys_addr_t __init alloc_pud_early(uintptr_t va)
518 {
519 /* Only one PUD is available for early mapping */
520 BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
521
522 return (uintptr_t)early_pud;
523 }
524
alloc_pud_fixmap(uintptr_t va)525 static phys_addr_t __init alloc_pud_fixmap(uintptr_t va)
526 {
527 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
528 }
529
alloc_pud_late(uintptr_t va)530 static phys_addr_t alloc_pud_late(uintptr_t va)
531 {
532 unsigned long vaddr;
533
534 vaddr = __get_free_page(GFP_KERNEL);
535 BUG_ON(!vaddr);
536 return __pa(vaddr);
537 }
538
get_p4d_virt_early(phys_addr_t pa)539 static p4d_t *__init get_p4d_virt_early(phys_addr_t pa)
540 {
541 return (p4d_t *)((uintptr_t)pa);
542 }
543
get_p4d_virt_fixmap(phys_addr_t pa)544 static p4d_t *__init get_p4d_virt_fixmap(phys_addr_t pa)
545 {
546 clear_fixmap(FIX_P4D);
547 return (p4d_t *)set_fixmap_offset(FIX_P4D, pa);
548 }
549
get_p4d_virt_late(phys_addr_t pa)550 static p4d_t *__init get_p4d_virt_late(phys_addr_t pa)
551 {
552 return (p4d_t *)__va(pa);
553 }
554
alloc_p4d_early(uintptr_t va)555 static phys_addr_t __init alloc_p4d_early(uintptr_t va)
556 {
557 /* Only one P4D is available for early mapping */
558 BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
559
560 return (uintptr_t)early_p4d;
561 }
562
alloc_p4d_fixmap(uintptr_t va)563 static phys_addr_t __init alloc_p4d_fixmap(uintptr_t va)
564 {
565 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
566 }
567
alloc_p4d_late(uintptr_t va)568 static phys_addr_t alloc_p4d_late(uintptr_t va)
569 {
570 unsigned long vaddr;
571
572 vaddr = __get_free_page(GFP_KERNEL);
573 BUG_ON(!vaddr);
574 return __pa(vaddr);
575 }
576
create_pud_mapping(pud_t * pudp,uintptr_t va,phys_addr_t pa,phys_addr_t sz,pgprot_t prot)577 static void __init create_pud_mapping(pud_t *pudp,
578 uintptr_t va, phys_addr_t pa,
579 phys_addr_t sz, pgprot_t prot)
580 {
581 pmd_t *nextp;
582 phys_addr_t next_phys;
583 uintptr_t pud_index = pud_index(va);
584
585 if (sz == PUD_SIZE) {
586 if (pud_val(pudp[pud_index]) == 0)
587 pudp[pud_index] = pfn_pud(PFN_DOWN(pa), prot);
588 return;
589 }
590
591 if (pud_val(pudp[pud_index]) == 0) {
592 next_phys = pt_ops.alloc_pmd(va);
593 pudp[pud_index] = pfn_pud(PFN_DOWN(next_phys), PAGE_TABLE);
594 nextp = pt_ops.get_pmd_virt(next_phys);
595 memset(nextp, 0, PAGE_SIZE);
596 } else {
597 next_phys = PFN_PHYS(_pud_pfn(pudp[pud_index]));
598 nextp = pt_ops.get_pmd_virt(next_phys);
599 }
600
601 create_pmd_mapping(nextp, va, pa, sz, prot);
602 }
603
create_p4d_mapping(p4d_t * p4dp,uintptr_t va,phys_addr_t pa,phys_addr_t sz,pgprot_t prot)604 static void __init create_p4d_mapping(p4d_t *p4dp,
605 uintptr_t va, phys_addr_t pa,
606 phys_addr_t sz, pgprot_t prot)
607 {
608 pud_t *nextp;
609 phys_addr_t next_phys;
610 uintptr_t p4d_index = p4d_index(va);
611
612 if (sz == P4D_SIZE) {
613 if (p4d_val(p4dp[p4d_index]) == 0)
614 p4dp[p4d_index] = pfn_p4d(PFN_DOWN(pa), prot);
615 return;
616 }
617
618 if (p4d_val(p4dp[p4d_index]) == 0) {
619 next_phys = pt_ops.alloc_pud(va);
620 p4dp[p4d_index] = pfn_p4d(PFN_DOWN(next_phys), PAGE_TABLE);
621 nextp = pt_ops.get_pud_virt(next_phys);
622 memset(nextp, 0, PAGE_SIZE);
623 } else {
624 next_phys = PFN_PHYS(_p4d_pfn(p4dp[p4d_index]));
625 nextp = pt_ops.get_pud_virt(next_phys);
626 }
627
628 create_pud_mapping(nextp, va, pa, sz, prot);
629 }
630
631 #define pgd_next_t p4d_t
632 #define alloc_pgd_next(__va) (pgtable_l5_enabled ? \
633 pt_ops.alloc_p4d(__va) : (pgtable_l4_enabled ? \
634 pt_ops.alloc_pud(__va) : pt_ops.alloc_pmd(__va)))
635 #define get_pgd_next_virt(__pa) (pgtable_l5_enabled ? \
636 pt_ops.get_p4d_virt(__pa) : (pgd_next_t *)(pgtable_l4_enabled ? \
637 pt_ops.get_pud_virt(__pa) : (pud_t *)pt_ops.get_pmd_virt(__pa)))
638 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
639 (pgtable_l5_enabled ? \
640 create_p4d_mapping(__nextp, __va, __pa, __sz, __prot) : \
641 (pgtable_l4_enabled ? \
642 create_pud_mapping((pud_t *)__nextp, __va, __pa, __sz, __prot) : \
643 create_pmd_mapping((pmd_t *)__nextp, __va, __pa, __sz, __prot)))
644 #define fixmap_pgd_next (pgtable_l5_enabled ? \
645 (uintptr_t)fixmap_p4d : (pgtable_l4_enabled ? \
646 (uintptr_t)fixmap_pud : (uintptr_t)fixmap_pmd))
647 #define trampoline_pgd_next (pgtable_l5_enabled ? \
648 (uintptr_t)trampoline_p4d : (pgtable_l4_enabled ? \
649 (uintptr_t)trampoline_pud : (uintptr_t)trampoline_pmd))
650 #else
651 #define pgd_next_t pte_t
652 #define alloc_pgd_next(__va) pt_ops.alloc_pte(__va)
653 #define get_pgd_next_virt(__pa) pt_ops.get_pte_virt(__pa)
654 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
655 create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
656 #define fixmap_pgd_next ((uintptr_t)fixmap_pte)
657 #define create_p4d_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
658 #define create_pud_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
659 #define create_pmd_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
660 #endif /* __PAGETABLE_PMD_FOLDED */
661
create_pgd_mapping(pgd_t * pgdp,uintptr_t va,phys_addr_t pa,phys_addr_t sz,pgprot_t prot)662 void __init create_pgd_mapping(pgd_t *pgdp,
663 uintptr_t va, phys_addr_t pa,
664 phys_addr_t sz, pgprot_t prot)
665 {
666 pgd_next_t *nextp;
667 phys_addr_t next_phys;
668 uintptr_t pgd_idx = pgd_index(va);
669
670 if (sz == PGDIR_SIZE) {
671 if (pgd_val(pgdp[pgd_idx]) == 0)
672 pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot);
673 return;
674 }
675
676 if (pgd_val(pgdp[pgd_idx]) == 0) {
677 next_phys = alloc_pgd_next(va);
678 pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
679 nextp = get_pgd_next_virt(next_phys);
680 memset(nextp, 0, PAGE_SIZE);
681 } else {
682 next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx]));
683 nextp = get_pgd_next_virt(next_phys);
684 }
685
686 create_pgd_next_mapping(nextp, va, pa, sz, prot);
687 }
688
best_map_size(phys_addr_t pa,uintptr_t va,phys_addr_t size)689 static uintptr_t __init best_map_size(phys_addr_t pa, uintptr_t va,
690 phys_addr_t size)
691 {
692 if (debug_pagealloc_enabled())
693 return PAGE_SIZE;
694
695 if (pgtable_l5_enabled &&
696 !(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >= P4D_SIZE)
697 return P4D_SIZE;
698
699 if (pgtable_l4_enabled &&
700 !(pa & (PUD_SIZE - 1)) && !(va & (PUD_SIZE - 1)) && size >= PUD_SIZE)
701 return PUD_SIZE;
702
703 if (IS_ENABLED(CONFIG_64BIT) &&
704 !(pa & (PMD_SIZE - 1)) && !(va & (PMD_SIZE - 1)) && size >= PMD_SIZE)
705 return PMD_SIZE;
706
707 return PAGE_SIZE;
708 }
709
710 #ifdef CONFIG_XIP_KERNEL
711 #define phys_ram_base (*(phys_addr_t *)XIP_FIXUP(&phys_ram_base))
712 extern char _xiprom[], _exiprom[], __data_loc;
713
714 /* called from head.S with MMU off */
__copy_data(void)715 asmlinkage void __init __copy_data(void)
716 {
717 void *from = (void *)(&__data_loc);
718 void *to = (void *)CONFIG_PHYS_RAM_BASE;
719 size_t sz = (size_t)((uintptr_t)(&_end) - (uintptr_t)(&_sdata));
720
721 memcpy(to, from, sz);
722 }
723 #endif
724
725 #ifdef CONFIG_STRICT_KERNEL_RWX
pgprot_from_va(uintptr_t va)726 static __init pgprot_t pgprot_from_va(uintptr_t va)
727 {
728 if (is_va_kernel_text(va))
729 return PAGE_KERNEL_READ_EXEC;
730
731 /*
732 * In 64-bit kernel, the kernel mapping is outside the linear mapping so
733 * we must protect its linear mapping alias from being executed and
734 * written.
735 * And rodata section is marked readonly in mark_rodata_ro.
736 */
737 if (IS_ENABLED(CONFIG_64BIT) && is_va_kernel_lm_alias_text(va))
738 return PAGE_KERNEL_READ;
739
740 return PAGE_KERNEL;
741 }
742
mark_rodata_ro(void)743 void mark_rodata_ro(void)
744 {
745 set_kernel_memory(__start_rodata, _data, set_memory_ro);
746 if (IS_ENABLED(CONFIG_64BIT))
747 set_kernel_memory(lm_alias(__start_rodata), lm_alias(_data),
748 set_memory_ro);
749
750 debug_checkwx();
751 }
752 #else
pgprot_from_va(uintptr_t va)753 static __init 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
793 /*
794 * There is a simple way to determine if 4-level is supported by the
795 * underlying hardware: establish 1:1 mapping in 4-level page table mode
796 * then read SATP to see if the configuration was taken into account
797 * meaning sv48 is supported.
798 */
set_satp_mode(uintptr_t dtb_pa)799 static __init void set_satp_mode(uintptr_t dtb_pa)
800 {
801 u64 identity_satp, hw_satp;
802 uintptr_t set_satp_mode_pmd = ((unsigned long)set_satp_mode) & PMD_MASK;
803 u64 satp_mode_cmdline = __pi_set_satp_mode_from_cmdline(dtb_pa);
804
805 if (satp_mode_cmdline == SATP_MODE_57) {
806 disable_pgtable_l5();
807 } else if (satp_mode_cmdline == SATP_MODE_48) {
808 disable_pgtable_l5();
809 disable_pgtable_l4();
810 return;
811 }
812
813 create_p4d_mapping(early_p4d,
814 set_satp_mode_pmd, (uintptr_t)early_pud,
815 P4D_SIZE, PAGE_TABLE);
816 create_pud_mapping(early_pud,
817 set_satp_mode_pmd, (uintptr_t)early_pmd,
818 PUD_SIZE, PAGE_TABLE);
819 /* Handle the case where set_satp_mode straddles 2 PMDs */
820 create_pmd_mapping(early_pmd,
821 set_satp_mode_pmd, set_satp_mode_pmd,
822 PMD_SIZE, PAGE_KERNEL_EXEC);
823 create_pmd_mapping(early_pmd,
824 set_satp_mode_pmd + PMD_SIZE,
825 set_satp_mode_pmd + PMD_SIZE,
826 PMD_SIZE, PAGE_KERNEL_EXEC);
827 retry:
828 create_pgd_mapping(early_pg_dir,
829 set_satp_mode_pmd,
830 pgtable_l5_enabled ?
831 (uintptr_t)early_p4d : (uintptr_t)early_pud,
832 PGDIR_SIZE, PAGE_TABLE);
833
834 identity_satp = PFN_DOWN((uintptr_t)&early_pg_dir) | satp_mode;
835
836 local_flush_tlb_all();
837 csr_write(CSR_SATP, identity_satp);
838 hw_satp = csr_swap(CSR_SATP, 0ULL);
839 local_flush_tlb_all();
840
841 if (hw_satp != identity_satp) {
842 if (pgtable_l5_enabled) {
843 disable_pgtable_l5();
844 memset(early_pg_dir, 0, PAGE_SIZE);
845 goto retry;
846 }
847 disable_pgtable_l4();
848 }
849
850 memset(early_pg_dir, 0, PAGE_SIZE);
851 memset(early_p4d, 0, PAGE_SIZE);
852 memset(early_pud, 0, PAGE_SIZE);
853 memset(early_pmd, 0, PAGE_SIZE);
854 }
855 #endif
856
857 /*
858 * setup_vm() is called from head.S with MMU-off.
859 *
860 * Following requirements should be honoured for setup_vm() to work
861 * correctly:
862 * 1) It should use PC-relative addressing for accessing kernel symbols.
863 * To achieve this we always use GCC cmodel=medany.
864 * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
865 * so disable compiler instrumentation when FTRACE is enabled.
866 *
867 * Currently, the above requirements are honoured by using custom CFLAGS
868 * for init.o in mm/Makefile.
869 */
870
871 #ifndef __riscv_cmodel_medany
872 #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
873 #endif
874
875 #ifdef CONFIG_RELOCATABLE
876 extern unsigned long __rela_dyn_start, __rela_dyn_end;
877
relocate_kernel(void)878 static void __init relocate_kernel(void)
879 {
880 Elf64_Rela *rela = (Elf64_Rela *)&__rela_dyn_start;
881 /*
882 * This holds the offset between the linked virtual address and the
883 * relocated virtual address.
884 */
885 uintptr_t reloc_offset = kernel_map.virt_addr - KERNEL_LINK_ADDR;
886 /*
887 * This holds the offset between kernel linked virtual address and
888 * physical address.
889 */
890 uintptr_t va_kernel_link_pa_offset = KERNEL_LINK_ADDR - kernel_map.phys_addr;
891
892 for ( ; rela < (Elf64_Rela *)&__rela_dyn_end; rela++) {
893 Elf64_Addr addr = (rela->r_offset - va_kernel_link_pa_offset);
894 Elf64_Addr relocated_addr = rela->r_addend;
895
896 if (rela->r_info != R_RISCV_RELATIVE)
897 continue;
898
899 /*
900 * Make sure to not relocate vdso symbols like rt_sigreturn
901 * which are linked from the address 0 in vmlinux since
902 * vdso symbol addresses are actually used as an offset from
903 * mm->context.vdso in VDSO_OFFSET macro.
904 */
905 if (relocated_addr >= KERNEL_LINK_ADDR)
906 relocated_addr += reloc_offset;
907
908 *(Elf64_Addr *)addr = relocated_addr;
909 }
910 }
911 #endif /* CONFIG_RELOCATABLE */
912
913 #ifdef CONFIG_XIP_KERNEL
create_kernel_page_table(pgd_t * pgdir,__always_unused bool early)914 static void __init create_kernel_page_table(pgd_t *pgdir,
915 __always_unused bool early)
916 {
917 uintptr_t va, end_va;
918
919 /* Map the flash resident part */
920 end_va = kernel_map.virt_addr + kernel_map.xiprom_sz;
921 for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE)
922 create_pgd_mapping(pgdir, va,
923 kernel_map.xiprom + (va - kernel_map.virt_addr),
924 PMD_SIZE, PAGE_KERNEL_EXEC);
925
926 /* Map the data in RAM */
927 end_va = kernel_map.virt_addr + kernel_map.size;
928 for (va = kernel_map.virt_addr + XIP_OFFSET; va < end_va; va += PMD_SIZE)
929 create_pgd_mapping(pgdir, va,
930 kernel_map.phys_addr + (va - (kernel_map.virt_addr + XIP_OFFSET)),
931 PMD_SIZE, PAGE_KERNEL);
932 }
933 #else
create_kernel_page_table(pgd_t * pgdir,bool early)934 static void __init create_kernel_page_table(pgd_t *pgdir, bool early)
935 {
936 uintptr_t va, end_va;
937
938 end_va = kernel_map.virt_addr + kernel_map.size;
939 for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE)
940 create_pgd_mapping(pgdir, va,
941 kernel_map.phys_addr + (va - kernel_map.virt_addr),
942 PMD_SIZE,
943 early ?
944 PAGE_KERNEL_EXEC : pgprot_from_va(va));
945 }
946 #endif
947
948 /*
949 * Setup a 4MB mapping that encompasses the device tree: for 64-bit kernel,
950 * this means 2 PMD entries whereas for 32-bit kernel, this is only 1 PGDIR
951 * entry.
952 */
create_fdt_early_page_table(uintptr_t fix_fdt_va,uintptr_t dtb_pa)953 static void __init create_fdt_early_page_table(uintptr_t fix_fdt_va,
954 uintptr_t dtb_pa)
955 {
956 #ifndef CONFIG_BUILTIN_DTB
957 uintptr_t pa = dtb_pa & ~(PMD_SIZE - 1);
958
959 /* Make sure the fdt fixmap address is always aligned on PMD size */
960 BUILD_BUG_ON(FIX_FDT % (PMD_SIZE / PAGE_SIZE));
961
962 /* In 32-bit only, the fdt lies in its own PGD */
963 if (!IS_ENABLED(CONFIG_64BIT)) {
964 create_pgd_mapping(early_pg_dir, fix_fdt_va,
965 pa, MAX_FDT_SIZE, PAGE_KERNEL);
966 } else {
967 create_pmd_mapping(fixmap_pmd, fix_fdt_va,
968 pa, PMD_SIZE, PAGE_KERNEL);
969 create_pmd_mapping(fixmap_pmd, fix_fdt_va + PMD_SIZE,
970 pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL);
971 }
972
973 dtb_early_va = (void *)fix_fdt_va + (dtb_pa & (PMD_SIZE - 1));
974 #else
975 /*
976 * For 64-bit kernel, __va can't be used since it would return a linear
977 * mapping address whereas dtb_early_va will be used before
978 * setup_vm_final installs the linear mapping. For 32-bit kernel, as the
979 * kernel is mapped in the linear mapping, that makes no difference.
980 */
981 dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
982 #endif
983
984 dtb_early_pa = dtb_pa;
985 }
986
987 /*
988 * MMU is not enabled, the page tables are allocated directly using
989 * early_pmd/pud/p4d and the address returned is the physical one.
990 */
pt_ops_set_early(void)991 static void __init pt_ops_set_early(void)
992 {
993 pt_ops.alloc_pte = alloc_pte_early;
994 pt_ops.get_pte_virt = get_pte_virt_early;
995 #ifndef __PAGETABLE_PMD_FOLDED
996 pt_ops.alloc_pmd = alloc_pmd_early;
997 pt_ops.get_pmd_virt = get_pmd_virt_early;
998 pt_ops.alloc_pud = alloc_pud_early;
999 pt_ops.get_pud_virt = get_pud_virt_early;
1000 pt_ops.alloc_p4d = alloc_p4d_early;
1001 pt_ops.get_p4d_virt = get_p4d_virt_early;
1002 #endif
1003 }
1004
1005 /*
1006 * MMU is enabled but page table setup is not complete yet.
1007 * fixmap page table alloc functions must be used as a means to temporarily
1008 * map the allocated physical pages since the linear mapping does not exist yet.
1009 *
1010 * Note that this is called with MMU disabled, hence kernel_mapping_pa_to_va,
1011 * but it will be used as described above.
1012 */
pt_ops_set_fixmap(void)1013 static void __init pt_ops_set_fixmap(void)
1014 {
1015 pt_ops.alloc_pte = kernel_mapping_pa_to_va(alloc_pte_fixmap);
1016 pt_ops.get_pte_virt = kernel_mapping_pa_to_va(get_pte_virt_fixmap);
1017 #ifndef __PAGETABLE_PMD_FOLDED
1018 pt_ops.alloc_pmd = kernel_mapping_pa_to_va(alloc_pmd_fixmap);
1019 pt_ops.get_pmd_virt = kernel_mapping_pa_to_va(get_pmd_virt_fixmap);
1020 pt_ops.alloc_pud = kernel_mapping_pa_to_va(alloc_pud_fixmap);
1021 pt_ops.get_pud_virt = kernel_mapping_pa_to_va(get_pud_virt_fixmap);
1022 pt_ops.alloc_p4d = kernel_mapping_pa_to_va(alloc_p4d_fixmap);
1023 pt_ops.get_p4d_virt = kernel_mapping_pa_to_va(get_p4d_virt_fixmap);
1024 #endif
1025 }
1026
1027 /*
1028 * MMU is enabled and page table setup is complete, so from now, we can use
1029 * generic page allocation functions to setup page table.
1030 */
pt_ops_set_late(void)1031 static void __init pt_ops_set_late(void)
1032 {
1033 pt_ops.alloc_pte = alloc_pte_late;
1034 pt_ops.get_pte_virt = get_pte_virt_late;
1035 #ifndef __PAGETABLE_PMD_FOLDED
1036 pt_ops.alloc_pmd = alloc_pmd_late;
1037 pt_ops.get_pmd_virt = get_pmd_virt_late;
1038 pt_ops.alloc_pud = alloc_pud_late;
1039 pt_ops.get_pud_virt = get_pud_virt_late;
1040 pt_ops.alloc_p4d = alloc_p4d_late;
1041 pt_ops.get_p4d_virt = get_p4d_virt_late;
1042 #endif
1043 }
1044
1045 #ifdef CONFIG_RANDOMIZE_BASE
1046 extern bool __init __pi_set_nokaslr_from_cmdline(uintptr_t dtb_pa);
1047 extern u64 __init __pi_get_kaslr_seed(uintptr_t dtb_pa);
1048
print_nokaslr(char * p)1049 static int __init print_nokaslr(char *p)
1050 {
1051 pr_info("Disabled KASLR");
1052 return 0;
1053 }
1054 early_param("nokaslr", print_nokaslr);
1055
kaslr_offset(void)1056 unsigned long kaslr_offset(void)
1057 {
1058 return kernel_map.virt_offset;
1059 }
1060 #endif
1061
setup_vm(uintptr_t dtb_pa)1062 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
1063 {
1064 pmd_t __maybe_unused fix_bmap_spmd, fix_bmap_epmd;
1065
1066 #ifdef CONFIG_RANDOMIZE_BASE
1067 if (!__pi_set_nokaslr_from_cmdline(dtb_pa)) {
1068 u64 kaslr_seed = __pi_get_kaslr_seed(dtb_pa);
1069 u32 kernel_size = (uintptr_t)(&_end) - (uintptr_t)(&_start);
1070 u32 nr_pos;
1071
1072 /*
1073 * Compute the number of positions available: we are limited
1074 * by the early page table that only has one PUD and we must
1075 * be aligned on PMD_SIZE.
1076 */
1077 nr_pos = (PUD_SIZE - kernel_size) / PMD_SIZE;
1078
1079 kernel_map.virt_offset = (kaslr_seed % nr_pos) * PMD_SIZE;
1080 }
1081 #endif
1082
1083 kernel_map.virt_addr = KERNEL_LINK_ADDR + kernel_map.virt_offset;
1084
1085 #ifdef CONFIG_XIP_KERNEL
1086 #ifdef CONFIG_64BIT
1087 kernel_map.page_offset = PAGE_OFFSET_L3;
1088 #else
1089 kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL);
1090 #endif
1091 kernel_map.xiprom = (uintptr_t)CONFIG_XIP_PHYS_ADDR;
1092 kernel_map.xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom);
1093
1094 phys_ram_base = CONFIG_PHYS_RAM_BASE;
1095 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1096 vmemmap_start_pfn = round_down(phys_ram_base, VMEMMAP_ADDR_ALIGN) >> PAGE_SHIFT;
1097 #endif
1098 kernel_map.phys_addr = (uintptr_t)CONFIG_PHYS_RAM_BASE;
1099 kernel_map.size = (uintptr_t)(&_end) - (uintptr_t)(&_start);
1100
1101 kernel_map.va_kernel_xip_pa_offset = kernel_map.virt_addr - kernel_map.xiprom;
1102 #else
1103 kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL);
1104 kernel_map.phys_addr = (uintptr_t)(&_start);
1105 kernel_map.size = (uintptr_t)(&_end) - kernel_map.phys_addr;
1106 #endif
1107
1108 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
1109 set_satp_mode(dtb_pa);
1110 #endif
1111
1112 /*
1113 * In 64-bit, we defer the setup of va_pa_offset to setup_bootmem,
1114 * where we have the system memory layout: this allows us to align
1115 * the physical and virtual mappings and then make use of PUD/P4D/PGD
1116 * for the linear mapping. This is only possible because the kernel
1117 * mapping lies outside the linear mapping.
1118 * In 32-bit however, as the kernel resides in the linear mapping,
1119 * setup_vm_final can not change the mapping established here,
1120 * otherwise the same kernel addresses would get mapped to different
1121 * physical addresses (if the start of dram is different from the
1122 * kernel physical address start).
1123 */
1124 kernel_map.va_pa_offset = IS_ENABLED(CONFIG_64BIT) ?
1125 0UL : PAGE_OFFSET - kernel_map.phys_addr;
1126 kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr;
1127
1128 /*
1129 * The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit
1130 * kernel, whereas for 64-bit kernel, the end of the virtual address
1131 * space is occupied by the modules/BPF/kernel mappings which reduces
1132 * the available size of the linear mapping.
1133 */
1134 memory_limit = KERN_VIRT_SIZE - (IS_ENABLED(CONFIG_64BIT) ? SZ_4G : 0);
1135
1136 /* Sanity check alignment and size */
1137 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
1138 BUG_ON((kernel_map.phys_addr % PMD_SIZE) != 0);
1139
1140 #ifdef CONFIG_64BIT
1141 /*
1142 * The last 4K bytes of the addressable memory can not be mapped because
1143 * of IS_ERR_VALUE macro.
1144 */
1145 BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K);
1146 #endif
1147
1148 #ifdef CONFIG_RELOCATABLE
1149 /*
1150 * Early page table uses only one PUD, which makes it possible
1151 * to map PUD_SIZE aligned on PUD_SIZE: if the relocation offset
1152 * makes the kernel cross over a PUD_SIZE boundary, raise a bug
1153 * since a part of the kernel would not get mapped.
1154 */
1155 BUG_ON(PUD_SIZE - (kernel_map.virt_addr & (PUD_SIZE - 1)) < kernel_map.size);
1156 relocate_kernel();
1157 #endif
1158
1159 apply_early_boot_alternatives();
1160 pt_ops_set_early();
1161
1162 /* Setup early PGD for fixmap */
1163 create_pgd_mapping(early_pg_dir, FIXADDR_START,
1164 fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
1165
1166 #ifndef __PAGETABLE_PMD_FOLDED
1167 /* Setup fixmap P4D and PUD */
1168 if (pgtable_l5_enabled)
1169 create_p4d_mapping(fixmap_p4d, FIXADDR_START,
1170 (uintptr_t)fixmap_pud, P4D_SIZE, PAGE_TABLE);
1171 /* Setup fixmap PUD and PMD */
1172 if (pgtable_l4_enabled)
1173 create_pud_mapping(fixmap_pud, FIXADDR_START,
1174 (uintptr_t)fixmap_pmd, PUD_SIZE, PAGE_TABLE);
1175 create_pmd_mapping(fixmap_pmd, FIXADDR_START,
1176 (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
1177 /* Setup trampoline PGD and PMD */
1178 create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr,
1179 trampoline_pgd_next, PGDIR_SIZE, PAGE_TABLE);
1180 if (pgtable_l5_enabled)
1181 create_p4d_mapping(trampoline_p4d, kernel_map.virt_addr,
1182 (uintptr_t)trampoline_pud, P4D_SIZE, PAGE_TABLE);
1183 if (pgtable_l4_enabled)
1184 create_pud_mapping(trampoline_pud, kernel_map.virt_addr,
1185 (uintptr_t)trampoline_pmd, PUD_SIZE, PAGE_TABLE);
1186 #ifdef CONFIG_XIP_KERNEL
1187 create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr,
1188 kernel_map.xiprom, PMD_SIZE, PAGE_KERNEL_EXEC);
1189 #else
1190 create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr,
1191 kernel_map.phys_addr, PMD_SIZE, PAGE_KERNEL_EXEC);
1192 #endif
1193 #else
1194 /* Setup trampoline PGD */
1195 create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr,
1196 kernel_map.phys_addr, PGDIR_SIZE, PAGE_KERNEL_EXEC);
1197 #endif
1198
1199 /*
1200 * Setup early PGD covering entire kernel which will allow
1201 * us to reach paging_init(). We map all memory banks later
1202 * in setup_vm_final() below.
1203 */
1204 create_kernel_page_table(early_pg_dir, true);
1205
1206 /* Setup early mapping for FDT early scan */
1207 create_fdt_early_page_table(__fix_to_virt(FIX_FDT), dtb_pa);
1208
1209 /*
1210 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
1211 * range can not span multiple pmds.
1212 */
1213 BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
1214 != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
1215
1216 #ifndef __PAGETABLE_PMD_FOLDED
1217 /*
1218 * Early ioremap fixmap is already created as it lies within first 2MB
1219 * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END
1220 * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn
1221 * the user if not.
1222 */
1223 fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];
1224 fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];
1225 if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {
1226 WARN_ON(1);
1227 pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
1228 pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));
1229 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
1230 fix_to_virt(FIX_BTMAP_BEGIN));
1231 pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
1232 fix_to_virt(FIX_BTMAP_END));
1233
1234 pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
1235 pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
1236 }
1237 #endif
1238
1239 pt_ops_set_fixmap();
1240 }
1241
create_linear_mapping_range(phys_addr_t start,phys_addr_t end,uintptr_t fixed_map_size)1242 static void __init create_linear_mapping_range(phys_addr_t start,
1243 phys_addr_t end,
1244 uintptr_t fixed_map_size)
1245 {
1246 phys_addr_t pa;
1247 uintptr_t va, map_size;
1248
1249 for (pa = start; pa < end; pa += map_size) {
1250 va = (uintptr_t)__va(pa);
1251 map_size = fixed_map_size ? fixed_map_size :
1252 best_map_size(pa, va, end - pa);
1253
1254 create_pgd_mapping(swapper_pg_dir, va, pa, map_size,
1255 pgprot_from_va(va));
1256 }
1257 }
1258
create_linear_mapping_page_table(void)1259 static void __init create_linear_mapping_page_table(void)
1260 {
1261 phys_addr_t start, end;
1262 phys_addr_t kfence_pool __maybe_unused;
1263 u64 i;
1264
1265 #ifdef CONFIG_STRICT_KERNEL_RWX
1266 phys_addr_t ktext_start = __pa_symbol(_start);
1267 phys_addr_t ktext_size = __init_data_begin - _start;
1268 phys_addr_t krodata_start = __pa_symbol(__start_rodata);
1269 phys_addr_t krodata_size = _data - __start_rodata;
1270
1271 /* Isolate kernel text and rodata so they don't get mapped with a PUD */
1272 memblock_mark_nomap(ktext_start, ktext_size);
1273 memblock_mark_nomap(krodata_start, krodata_size);
1274 #endif
1275
1276 #ifdef CONFIG_KFENCE
1277 /*
1278 * kfence pool must be backed by PAGE_SIZE mappings, so allocate it
1279 * before we setup the linear mapping so that we avoid using hugepages
1280 * for this region.
1281 */
1282 kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
1283 BUG_ON(!kfence_pool);
1284
1285 memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE);
1286 __kfence_pool = __va(kfence_pool);
1287 #endif
1288
1289 /* Map all memory banks in the linear mapping */
1290 for_each_mem_range(i, &start, &end) {
1291 if (start >= end)
1292 break;
1293 if (start <= __pa(PAGE_OFFSET) &&
1294 __pa(PAGE_OFFSET) < end)
1295 start = __pa(PAGE_OFFSET);
1296
1297 create_linear_mapping_range(start, end, 0);
1298 }
1299
1300 #ifdef CONFIG_STRICT_KERNEL_RWX
1301 create_linear_mapping_range(ktext_start, ktext_start + ktext_size, 0);
1302 create_linear_mapping_range(krodata_start,
1303 krodata_start + krodata_size, 0);
1304
1305 memblock_clear_nomap(ktext_start, ktext_size);
1306 memblock_clear_nomap(krodata_start, krodata_size);
1307 #endif
1308
1309 #ifdef CONFIG_KFENCE
1310 create_linear_mapping_range(kfence_pool,
1311 kfence_pool + KFENCE_POOL_SIZE,
1312 PAGE_SIZE);
1313
1314 memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE);
1315 #endif
1316 }
1317
setup_vm_final(void)1318 static void __init setup_vm_final(void)
1319 {
1320 /* Setup swapper PGD for fixmap */
1321 #if !defined(CONFIG_64BIT)
1322 /*
1323 * In 32-bit, the device tree lies in a pgd entry, so it must be copied
1324 * directly in swapper_pg_dir in addition to the pgd entry that points
1325 * to fixmap_pte.
1326 */
1327 unsigned long idx = pgd_index(__fix_to_virt(FIX_FDT));
1328
1329 set_pgd(&swapper_pg_dir[idx], early_pg_dir[idx]);
1330 #endif
1331 create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
1332 __pa_symbol(fixmap_pgd_next),
1333 PGDIR_SIZE, PAGE_TABLE);
1334
1335 /* Map the linear mapping */
1336 create_linear_mapping_page_table();
1337
1338 /* Map the kernel */
1339 if (IS_ENABLED(CONFIG_64BIT))
1340 create_kernel_page_table(swapper_pg_dir, false);
1341
1342 #ifdef CONFIG_KASAN
1343 kasan_swapper_init();
1344 #endif
1345
1346 /* Clear fixmap PTE and PMD mappings */
1347 clear_fixmap(FIX_PTE);
1348 clear_fixmap(FIX_PMD);
1349 clear_fixmap(FIX_PUD);
1350 clear_fixmap(FIX_P4D);
1351
1352 /* Move to swapper page table */
1353 csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode);
1354 local_flush_tlb_all();
1355
1356 pt_ops_set_late();
1357 }
1358 #else
setup_vm(uintptr_t dtb_pa)1359 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
1360 {
1361 dtb_early_va = (void *)dtb_pa;
1362 dtb_early_pa = dtb_pa;
1363 }
1364
setup_vm_final(void)1365 static inline void setup_vm_final(void)
1366 {
1367 }
1368 #endif /* CONFIG_MMU */
1369
1370 /* Reserve 128M low memory by default for swiotlb buffer */
1371 #define DEFAULT_CRASH_KERNEL_LOW_SIZE (128UL << 20)
1372
reserve_crashkernel_low(unsigned long long low_size)1373 static int __init reserve_crashkernel_low(unsigned long long low_size)
1374 {
1375 unsigned long long low_base;
1376
1377 low_base = memblock_phys_alloc_range(low_size, PMD_SIZE, 0, dma32_phys_limit);
1378 if (!low_base) {
1379 pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size);
1380 return -ENOMEM;
1381 }
1382
1383 pr_info("crashkernel low memory reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
1384 low_base, low_base + low_size, low_size >> 20);
1385
1386 crashk_low_res.start = low_base;
1387 crashk_low_res.end = low_base + low_size - 1;
1388
1389 return 0;
1390 }
1391
1392 /*
1393 * reserve_crashkernel() - reserves memory for crash kernel
1394 *
1395 * This function reserves memory area given in "crashkernel=" kernel command
1396 * line parameter. The memory reserved is used by dump capture kernel when
1397 * primary kernel is crashing.
1398 */
reserve_crashkernel(void)1399 static void __init reserve_crashkernel(void)
1400 {
1401 unsigned long long crash_base = 0;
1402 unsigned long long crash_size = 0;
1403 unsigned long long crash_low_size = 0;
1404 unsigned long search_start = memblock_start_of_DRAM();
1405 unsigned long search_end = (unsigned long)dma32_phys_limit;
1406 char *cmdline = boot_command_line;
1407 bool fixed_base = false;
1408 bool high = false;
1409
1410 int ret = 0;
1411
1412 if (!IS_ENABLED(CONFIG_KEXEC_CORE))
1413 return;
1414 /*
1415 * Don't reserve a region for a crash kernel on a crash kernel
1416 * since it doesn't make much sense and we have limited memory
1417 * resources.
1418 */
1419 if (is_kdump_kernel()) {
1420 pr_info("crashkernel: ignoring reservation request\n");
1421 return;
1422 }
1423
1424 ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
1425 &crash_size, &crash_base);
1426 if (ret == -ENOENT) {
1427 /* Fallback to crashkernel=X,[high,low] */
1428 ret = parse_crashkernel_high(cmdline, 0, &crash_size, &crash_base);
1429 if (ret || !crash_size)
1430 return;
1431
1432 /*
1433 * crashkernel=Y,low is valid only when crashkernel=X,high
1434 * is passed.
1435 */
1436 ret = parse_crashkernel_low(cmdline, 0, &crash_low_size, &crash_base);
1437 if (ret == -ENOENT)
1438 crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
1439 else if (ret)
1440 return;
1441
1442 search_start = (unsigned long)dma32_phys_limit;
1443 search_end = memblock_end_of_DRAM();
1444 high = true;
1445 } else if (ret || !crash_size) {
1446 /* Invalid argument value specified */
1447 return;
1448 }
1449
1450 crash_size = PAGE_ALIGN(crash_size);
1451
1452 if (crash_base) {
1453 fixed_base = true;
1454 search_start = crash_base;
1455 search_end = crash_base + crash_size;
1456 }
1457
1458 /*
1459 * Current riscv boot protocol requires 2MB alignment for
1460 * RV64 and 4MB alignment for RV32 (hugepage size)
1461 *
1462 * Try to alloc from 32bit addressible physical memory so that
1463 * swiotlb can work on the crash kernel.
1464 */
1465 crash_base = memblock_phys_alloc_range(crash_size, PMD_SIZE,
1466 search_start, search_end);
1467 if (crash_base == 0) {
1468 /*
1469 * For crashkernel=size[KMG]@offset[KMG], print out failure
1470 * message if can't reserve the specified region.
1471 */
1472 if (fixed_base) {
1473 pr_warn("crashkernel: allocating failed with given size@offset\n");
1474 return;
1475 }
1476
1477 if (high) {
1478 /*
1479 * For crashkernel=size[KMG],high, if the first attempt was
1480 * for high memory, fall back to low memory.
1481 */
1482 search_start = memblock_start_of_DRAM();
1483 search_end = (unsigned long)dma32_phys_limit;
1484 } else {
1485 /*
1486 * For crashkernel=size[KMG], if the first attempt was for
1487 * low memory, fall back to high memory, the minimum required
1488 * low memory will be reserved later.
1489 */
1490 search_start = (unsigned long)dma32_phys_limit;
1491 search_end = memblock_end_of_DRAM();
1492 crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
1493 }
1494
1495 crash_base = memblock_phys_alloc_range(crash_size, PMD_SIZE,
1496 search_start, search_end);
1497 if (crash_base == 0) {
1498 pr_warn("crashkernel: couldn't allocate %lldKB\n",
1499 crash_size >> 10);
1500 return;
1501 }
1502 }
1503
1504 if ((crash_base >= dma32_phys_limit) && crash_low_size &&
1505 reserve_crashkernel_low(crash_low_size)) {
1506 memblock_phys_free(crash_base, crash_size);
1507 return;
1508 }
1509
1510 pr_info("crashkernel: reserved 0x%016llx - 0x%016llx (%lld MB)\n",
1511 crash_base, crash_base + crash_size, crash_size >> 20);
1512
1513 crashk_res.start = crash_base;
1514 crashk_res.end = crash_base + crash_size - 1;
1515 }
1516
paging_init(void)1517 void __init paging_init(void)
1518 {
1519 setup_bootmem();
1520 setup_vm_final();
1521
1522 /* Depend on that Linear Mapping is ready */
1523 memblock_allow_resize();
1524 }
1525
misc_mem_init(void)1526 void __init misc_mem_init(void)
1527 {
1528 early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
1529 arch_numa_init();
1530 sparse_init();
1531 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1532 /* The entire VMEMMAP region has been populated. Flush TLB for this region */
1533 local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END);
1534 #endif
1535 zone_sizes_init();
1536 reserve_crashkernel();
1537 memblock_dump_all();
1538 }
1539
1540 #ifdef CONFIG_SPARSEMEM_VMEMMAP
vmemmap_populate(unsigned long start,unsigned long end,int node,struct vmem_altmap * altmap)1541 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1542 struct vmem_altmap *altmap)
1543 {
1544 return vmemmap_populate_basepages(start, end, node, NULL);
1545 }
1546 #endif
1547
1548 #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
1549 /*
1550 * Pre-allocates page-table pages for a specific area in the kernel
1551 * page-table. Only the level which needs to be synchronized between
1552 * all page-tables is allocated because the synchronization can be
1553 * expensive.
1554 */
preallocate_pgd_pages_range(unsigned long start,unsigned long end,const char * area)1555 static void __init preallocate_pgd_pages_range(unsigned long start, unsigned long end,
1556 const char *area)
1557 {
1558 unsigned long addr;
1559 const char *lvl;
1560
1561 for (addr = start; addr < end && addr >= start; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
1562 pgd_t *pgd = pgd_offset_k(addr);
1563 p4d_t *p4d;
1564 pud_t *pud;
1565 pmd_t *pmd;
1566
1567 lvl = "p4d";
1568 p4d = p4d_alloc(&init_mm, pgd, addr);
1569 if (!p4d)
1570 goto failed;
1571
1572 if (pgtable_l5_enabled)
1573 continue;
1574
1575 lvl = "pud";
1576 pud = pud_alloc(&init_mm, p4d, addr);
1577 if (!pud)
1578 goto failed;
1579
1580 if (pgtable_l4_enabled)
1581 continue;
1582
1583 lvl = "pmd";
1584 pmd = pmd_alloc(&init_mm, pud, addr);
1585 if (!pmd)
1586 goto failed;
1587 }
1588 return;
1589
1590 failed:
1591 /*
1592 * The pages have to be there now or they will be missing in
1593 * process page-tables later.
1594 */
1595 panic("Failed to pre-allocate %s pages for %s area\n", lvl, area);
1596 }
1597
pgtable_cache_init(void)1598 void __init pgtable_cache_init(void)
1599 {
1600 preallocate_pgd_pages_range(VMALLOC_START, VMALLOC_END, "vmalloc");
1601 if (IS_ENABLED(CONFIG_MODULES))
1602 preallocate_pgd_pages_range(MODULES_VADDR, MODULES_END, "bpf/modules");
1603 }
1604 #endif
1605