at commit c03c1a14
| 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 | #include <linux/kfence.h> |
| 24 | #include <linux/execmem.h> |
| 25 | |
| 26 | #include <asm/alternative.h> |
| 27 | #include <asm/fixmap.h> |
| 28 | #include <asm/io.h> |
| 29 | #include <asm/kasan.h> |
| 30 | #include <asm/module.h> |
| 31 | #include <asm/numa.h> |
| 32 | #include <asm/pgtable.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 | #if defined(CONFIG_64BIT) && defined(CONFIG_MMU) |
| 41 | DECLARE_BITMAP(new_valid_map_cpus, NR_CPUS); |
| 42 | #endif |
| 43 | |
| 44 | struct kernel_mapping kernel_map __ro_after_init; |
| 45 | EXPORT_SYMBOL(kernel_map); |
| 46 | |
| 47 | #ifdef CONFIG_64BIT |
| 48 | u64 satp_mode __ro_after_init = SATP_MODE_57; |
| 49 | #else |
| 50 | u64 satp_mode __ro_after_init = SATP_MODE_32; |
| 51 | #endif |
| 52 | EXPORT_SYMBOL(satp_mode); |
| 53 | |
| 54 | #ifdef CONFIG_64BIT |
| 55 | bool pgtable_l4_enabled __ro_after_init = true; |
| 56 | bool pgtable_l5_enabled __ro_after_init = true; |
| 57 | EXPORT_SYMBOL(pgtable_l4_enabled); |
| 58 | EXPORT_SYMBOL(pgtable_l5_enabled); |
| 59 | #endif |
| 60 | |
| 61 | phys_addr_t phys_ram_base __ro_after_init; |
| 62 | EXPORT_SYMBOL(phys_ram_base); |
| 63 | |
| 64 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 65 | #define VMEMMAP_ADDR_ALIGN max(1ULL << SECTION_SIZE_BITS, \ |
| 66 | PFN_PHYS(MAX_FOLIO_VMEMMAP_ALIGN / \ |
| 67 | sizeof(struct page))) |
| 68 | |
| 69 | unsigned long vmemmap_start_pfn __ro_after_init; |
| 70 | EXPORT_SYMBOL(vmemmap_start_pfn); |
| 71 | #endif |
| 72 | |
| 73 | extern char _start[]; |
| 74 | void *_dtb_early_va __initdata; |
| 75 | uintptr_t _dtb_early_pa __initdata; |
| 76 | |
| 77 | phys_addr_t dma32_phys_limit __initdata; |
| 78 | |
| 79 | void __init arch_zone_limits_init(unsigned long *max_zone_pfns) |
| 80 | { |
| 81 | #ifdef CONFIG_ZONE_DMA32 |
| 82 | max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit); |
| 83 | #endif |
| 84 | max_zone_pfns[ZONE_NORMAL] = max_low_pfn; |
| 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 | |
| 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 | |
| 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 | |
| 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 |
| 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 | |
| 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 | |
| 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 |
| 163 | static void print_vm_layout(void) { } |
| 164 | #endif /* CONFIG_DEBUG_VM */ |
| 165 | |
| 166 | void __init arch_mm_preinit(void) |
| 167 | { |
| 168 | bool swiotlb = max_pfn > PFN_DOWN(dma32_phys_limit) && |
| 169 | memblock_start_of_DRAM() < dma32_phys_limit; |
| 170 | unsigned int swiotlb_flags = SWIOTLB_VERBOSE; |
| 171 | #ifdef CONFIG_FLATMEM |
| 172 | BUG_ON(!mem_map); |
| 173 | #endif /* CONFIG_FLATMEM */ |
| 174 | |
| 175 | if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb && |
| 176 | dma_cache_alignment != 1) { |
| 177 | /* |
| 178 | * No 32-bit DMA bouncing needed (either all DRAM is within |
| 179 | * the 32-bit limit, or it all starts above it), but |
| 180 | * kmalloc() buffers whose sizes are not cache-line-aligned |
| 181 | * still require bouncing for non-coherent DMA. Use |
| 182 | * SWIOTLB_ANY so that the buffer can be allocated from high |
| 183 | * memory when DRAM starts above dma32_phys_limit. Allocate |
| 184 | * ~1 MB per 1 GB of RAM. |
| 185 | */ |
| 186 | unsigned long size = |
| 187 | DIV_ROUND_UP(memblock_phys_mem_size(), 1024); |
| 188 | swiotlb_adjust_size(min(swiotlb_size_or_default(), size)); |
| 189 | swiotlb = true; |
| 190 | swiotlb_flags |= SWIOTLB_ANY; |
| 191 | } |
| 192 | |
| 193 | swiotlb_init(swiotlb, swiotlb_flags); |
| 194 | |
| 195 | print_vm_layout(); |
| 196 | } |
| 197 | |
| 198 | /* Limit the memory size via mem. */ |
| 199 | static phys_addr_t memory_limit; |
| 200 | |
| 201 | static int __init early_mem(char *p) |
| 202 | { |
| 203 | u64 size; |
| 204 | |
| 205 | if (!p) |
| 206 | return 1; |
| 207 | |
| 208 | size = memparse(p, &p) & PAGE_MASK; |
| 209 | memory_limit = min_t(u64, size, memory_limit); |
| 210 | |
| 211 | pr_notice("Memory limited to %lldMB\n", (u64)memory_limit >> 20); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | early_param("mem", early_mem); |
| 216 | |
| 217 | static void __init setup_bootmem(void) |
| 218 | { |
| 219 | phys_addr_t vmlinux_end = __pa_symbol(&_end); |
| 220 | phys_addr_t max_mapped_addr; |
| 221 | phys_addr_t phys_ram_end, vmlinux_start; |
| 222 | |
| 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 | phys_ram_base = memblock_start_of_DRAM() & PMD_MASK; |
| 244 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 245 | vmemmap_start_pfn = round_down(phys_ram_base, VMEMMAP_ADDR_ALIGN) >> PAGE_SHIFT; |
| 246 | #endif |
| 247 | |
| 248 | /* |
| 249 | * In 64-bit, any use of __va/__pa before this point is wrong as we |
| 250 | * did not know the start of DRAM before. |
| 251 | */ |
| 252 | if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU)) |
| 253 | kernel_map.va_pa_offset = PAGE_OFFSET - phys_ram_base; |
| 254 | |
| 255 | /* |
| 256 | * The size of the linear page mapping may restrict the amount of |
| 257 | * usable RAM. |
| 258 | */ |
| 259 | if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU)) { |
| 260 | max_mapped_addr = __pa(PAGE_OFFSET) + KERN_VIRT_SIZE; |
| 261 | if (memblock_end_of_DRAM() > max_mapped_addr) { |
| 262 | memblock_cap_memory_range(phys_ram_base, |
| 263 | max_mapped_addr - phys_ram_base); |
| 264 | pr_warn("Physical memory overflows the linear mapping size: region above %pa removed", |
| 265 | &max_mapped_addr); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Reserve physical address space that would be mapped to virtual |
| 271 | * addresses greater than (void *)(-PAGE_SIZE) because: |
| 272 | * - This memory would overlap with ERR_PTR |
| 273 | * - This memory belongs to high memory, which is not supported |
| 274 | * |
| 275 | * This is not applicable to 64-bit kernel, because virtual addresses |
| 276 | * after (void *)(-PAGE_SIZE) are not linearly mapped: they are |
| 277 | * occupied by kernel mapping. Also it is unrealistic for high memory |
| 278 | * to exist on 64-bit platforms. |
| 279 | */ |
| 280 | if (!IS_ENABLED(CONFIG_64BIT)) { |
| 281 | max_mapped_addr = __va_to_pa_nodebug(-PAGE_SIZE); |
| 282 | memblock_reserve(max_mapped_addr, (phys_addr_t)-max_mapped_addr); |
| 283 | } |
| 284 | |
| 285 | phys_ram_end = memblock_end_of_DRAM(); |
| 286 | min_low_pfn = PFN_UP(phys_ram_base); |
| 287 | max_low_pfn = max_pfn = PFN_DOWN(phys_ram_end); |
| 288 | |
| 289 | dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn)); |
| 290 | |
| 291 | reserve_initrd_mem(); |
| 292 | |
| 293 | /* |
| 294 | * No allocation should be done before reserving the memory as defined |
| 295 | * in the device tree, otherwise the allocation could end up in a |
| 296 | * reserved region. |
| 297 | */ |
| 298 | early_init_fdt_scan_reserved_mem(); |
| 299 | |
| 300 | /* |
| 301 | * If DTB is built in, no need to reserve its memblock. |
| 302 | * Otherwise, do reserve it but avoid using |
| 303 | * early_init_fdt_reserve_self() since __pa() does |
| 304 | * not work for DTB pointers that are fixmap addresses |
| 305 | */ |
| 306 | if (!IS_ENABLED(CONFIG_BUILTIN_DTB)) |
| 307 | memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va)); |
| 308 | } |
| 309 | |
| 310 | #ifdef CONFIG_RELOCATABLE |
| 311 | extern unsigned long __rela_dyn_start, __rela_dyn_end; |
| 312 | |
| 313 | static void __init relocate_kernel(void) |
| 314 | { |
| 315 | Elf_Rela *rela = (Elf_Rela *)&__rela_dyn_start; |
| 316 | /* |
| 317 | * This holds the offset between the linked virtual address and the |
| 318 | * relocated virtual address. |
| 319 | */ |
| 320 | uintptr_t reloc_offset = kernel_map.virt_addr - KERNEL_LINK_ADDR; |
| 321 | /* |
| 322 | * This holds the offset between kernel linked virtual address and |
| 323 | * physical address. |
| 324 | */ |
| 325 | uintptr_t va_kernel_link_pa_offset = KERNEL_LINK_ADDR - kernel_map.phys_addr; |
| 326 | |
| 327 | for ( ; rela < (Elf_Rela *)&__rela_dyn_end; rela++) { |
| 328 | Elf_Addr addr = (rela->r_offset - va_kernel_link_pa_offset); |
| 329 | Elf_Addr relocated_addr = rela->r_addend; |
| 330 | |
| 331 | if (rela->r_info != R_RISCV_RELATIVE) |
| 332 | continue; |
| 333 | |
| 334 | /* |
| 335 | * Make sure to not relocate vdso symbols like rt_sigreturn |
| 336 | * which are linked from the address 0 in vmlinux since |
| 337 | * vdso symbol addresses are actually used as an offset from |
| 338 | * mm->context.vdso in VDSO_OFFSET macro. |
| 339 | */ |
| 340 | if (relocated_addr >= KERNEL_LINK_ADDR) |
| 341 | relocated_addr += reloc_offset; |
| 342 | |
| 343 | *(Elf_Addr *)addr = relocated_addr; |
| 344 | } |
| 345 | } |
| 346 | #endif /* CONFIG_RELOCATABLE */ |
| 347 | |
| 348 | #ifdef CONFIG_MMU |
| 349 | struct pt_alloc_ops pt_ops __meminitdata; |
| 350 | |
| 351 | pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; |
| 352 | pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; |
| 353 | static pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; |
| 354 | |
| 355 | pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); |
| 356 | |
| 357 | static const pgprot_t protection_map[16] = { |
| 358 | [VM_NONE] = PAGE_NONE, |
| 359 | [VM_READ] = PAGE_READ, |
| 360 | [VM_WRITE] = PAGE_SHADOWSTACK, |
| 361 | [VM_WRITE | VM_READ] = PAGE_COPY, |
| 362 | [VM_EXEC] = PAGE_EXEC, |
| 363 | [VM_EXEC | VM_READ] = PAGE_READ_EXEC, |
| 364 | [VM_EXEC | VM_WRITE] = PAGE_COPY_EXEC, |
| 365 | [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_EXEC, |
| 366 | [VM_SHARED] = PAGE_NONE, |
| 367 | [VM_SHARED | VM_READ] = PAGE_READ, |
| 368 | [VM_SHARED | VM_WRITE] = PAGE_SHARED, |
| 369 | [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED, |
| 370 | [VM_SHARED | VM_EXEC] = PAGE_EXEC, |
| 371 | [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READ_EXEC, |
| 372 | [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED_EXEC, |
| 373 | [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_EXEC |
| 374 | }; |
| 375 | DECLARE_VM_GET_PAGE_PROT |
| 376 | |
| 377 | void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot) |
| 378 | { |
| 379 | unsigned long addr = __fix_to_virt(idx); |
| 380 | pte_t *ptep; |
| 381 | |
| 382 | BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); |
| 383 | |
| 384 | ptep = &fixmap_pte[pte_index(addr)]; |
| 385 | |
| 386 | if (pgprot_val(prot)) |
| 387 | set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot)); |
| 388 | else |
| 389 | pte_clear(&init_mm, addr, ptep); |
| 390 | local_flush_tlb_page(addr); |
| 391 | } |
| 392 | |
| 393 | static inline pte_t *__init get_pte_virt_early(phys_addr_t pa) |
| 394 | { |
| 395 | return (pte_t *)((uintptr_t)pa); |
| 396 | } |
| 397 | |
| 398 | static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa) |
| 399 | { |
| 400 | clear_fixmap(FIX_PTE); |
| 401 | return (pte_t *)set_fixmap_offset(FIX_PTE, pa); |
| 402 | } |
| 403 | |
| 404 | static inline pte_t *__meminit get_pte_virt_late(phys_addr_t pa) |
| 405 | { |
| 406 | return (pte_t *) __va(pa); |
| 407 | } |
| 408 | |
| 409 | static inline phys_addr_t __init alloc_pte_early(uintptr_t va) |
| 410 | { |
| 411 | /* |
| 412 | * We only create PMD or PGD early mappings so we |
| 413 | * should never reach here with MMU disabled. |
| 414 | */ |
| 415 | BUG(); |
| 416 | } |
| 417 | |
| 418 | static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va) |
| 419 | { |
| 420 | return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); |
| 421 | } |
| 422 | |
| 423 | static phys_addr_t __meminit alloc_pte_late(uintptr_t va) |
| 424 | { |
| 425 | struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL, 0); |
| 426 | |
| 427 | /* |
| 428 | * We do not know which mm the PTE page is associated to at this point. |
| 429 | * Passing NULL to the ctor is the safe option, though it may result |
| 430 | * in unnecessary work (e.g. initialising the ptlock for init_mm). |
| 431 | */ |
| 432 | BUG_ON(!ptdesc || !pagetable_pte_ctor(NULL, ptdesc)); |
| 433 | return __pa((pte_t *)ptdesc_address(ptdesc)); |
| 434 | } |
| 435 | |
| 436 | static void __meminit create_pte_mapping(pte_t *ptep, uintptr_t va, phys_addr_t pa, phys_addr_t sz, |
| 437 | pgprot_t prot) |
| 438 | { |
| 439 | uintptr_t pte_idx = pte_index(va); |
| 440 | |
| 441 | BUG_ON(sz != PAGE_SIZE); |
| 442 | |
| 443 | if (pte_none(ptep[pte_idx])) |
| 444 | ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot); |
| 445 | } |
| 446 | |
| 447 | #ifndef __PAGETABLE_PMD_FOLDED |
| 448 | |
| 449 | static pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss; |
| 450 | static pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss; |
| 451 | static pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE); |
| 452 | |
| 453 | static p4d_t trampoline_p4d[PTRS_PER_P4D] __page_aligned_bss; |
| 454 | static p4d_t fixmap_p4d[PTRS_PER_P4D] __page_aligned_bss; |
| 455 | static p4d_t early_p4d[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE); |
| 456 | |
| 457 | static pud_t trampoline_pud[PTRS_PER_PUD] __page_aligned_bss; |
| 458 | static pud_t fixmap_pud[PTRS_PER_PUD] __page_aligned_bss; |
| 459 | static pud_t early_pud[PTRS_PER_PUD] __initdata __aligned(PAGE_SIZE); |
| 460 | |
| 461 | static pmd_t *__init get_pmd_virt_early(phys_addr_t pa) |
| 462 | { |
| 463 | /* Before MMU is enabled */ |
| 464 | return (pmd_t *)((uintptr_t)pa); |
| 465 | } |
| 466 | |
| 467 | static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa) |
| 468 | { |
| 469 | clear_fixmap(FIX_PMD); |
| 470 | return (pmd_t *)set_fixmap_offset(FIX_PMD, pa); |
| 471 | } |
| 472 | |
| 473 | static pmd_t *__meminit get_pmd_virt_late(phys_addr_t pa) |
| 474 | { |
| 475 | return (pmd_t *) __va(pa); |
| 476 | } |
| 477 | |
| 478 | static phys_addr_t __init alloc_pmd_early(uintptr_t va) |
| 479 | { |
| 480 | BUG_ON((va - kernel_map.virt_addr) >> PUD_SHIFT); |
| 481 | |
| 482 | return (uintptr_t)early_pmd; |
| 483 | } |
| 484 | |
| 485 | static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va) |
| 486 | { |
| 487 | return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); |
| 488 | } |
| 489 | |
| 490 | static phys_addr_t __meminit alloc_pmd_late(uintptr_t va) |
| 491 | { |
| 492 | struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL, 0); |
| 493 | |
| 494 | /* See comment in alloc_pte_late() regarding NULL passed the ctor */ |
| 495 | BUG_ON(!ptdesc || !pagetable_pmd_ctor(NULL, ptdesc)); |
| 496 | return __pa((pmd_t *)ptdesc_address(ptdesc)); |
| 497 | } |
| 498 | |
| 499 | static void __meminit create_pmd_mapping(pmd_t *pmdp, |
| 500 | uintptr_t va, phys_addr_t pa, |
| 501 | phys_addr_t sz, pgprot_t prot) |
| 502 | { |
| 503 | pte_t *ptep; |
| 504 | phys_addr_t pte_phys; |
| 505 | uintptr_t pmd_idx = pmd_index(va); |
| 506 | |
| 507 | if (sz == PMD_SIZE) { |
| 508 | if (pmd_none(pmdp[pmd_idx])) |
| 509 | pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | if (pmd_none(pmdp[pmd_idx])) { |
| 514 | pte_phys = pt_ops.alloc_pte(va); |
| 515 | pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE); |
| 516 | ptep = pt_ops.get_pte_virt(pte_phys); |
| 517 | memset(ptep, 0, PAGE_SIZE); |
| 518 | } else { |
| 519 | pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx])); |
| 520 | ptep = pt_ops.get_pte_virt(pte_phys); |
| 521 | } |
| 522 | |
| 523 | create_pte_mapping(ptep, va, pa, sz, prot); |
| 524 | } |
| 525 | |
| 526 | static pud_t *__init get_pud_virt_early(phys_addr_t pa) |
| 527 | { |
| 528 | return (pud_t *)((uintptr_t)pa); |
| 529 | } |
| 530 | |
| 531 | static pud_t *__init get_pud_virt_fixmap(phys_addr_t pa) |
| 532 | { |
| 533 | clear_fixmap(FIX_PUD); |
| 534 | return (pud_t *)set_fixmap_offset(FIX_PUD, pa); |
| 535 | } |
| 536 | |
| 537 | static pud_t *__meminit get_pud_virt_late(phys_addr_t pa) |
| 538 | { |
| 539 | return (pud_t *)__va(pa); |
| 540 | } |
| 541 | |
| 542 | static phys_addr_t __init alloc_pud_early(uintptr_t va) |
| 543 | { |
| 544 | /* Only one PUD is available for early mapping */ |
| 545 | BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT); |
| 546 | |
| 547 | return (uintptr_t)early_pud; |
| 548 | } |
| 549 | |
| 550 | static phys_addr_t __init alloc_pud_fixmap(uintptr_t va) |
| 551 | { |
| 552 | return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); |
| 553 | } |
| 554 | |
| 555 | static phys_addr_t __meminit alloc_pud_late(uintptr_t va) |
| 556 | { |
| 557 | struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL, 0); |
| 558 | |
| 559 | BUG_ON(!ptdesc); |
| 560 | pagetable_pud_ctor(ptdesc); |
| 561 | return __pa((pud_t *)ptdesc_address(ptdesc)); |
| 562 | } |
| 563 | |
| 564 | static p4d_t *__init get_p4d_virt_early(phys_addr_t pa) |
| 565 | { |
| 566 | return (p4d_t *)((uintptr_t)pa); |
| 567 | } |
| 568 | |
| 569 | static p4d_t *__init get_p4d_virt_fixmap(phys_addr_t pa) |
| 570 | { |
| 571 | clear_fixmap(FIX_P4D); |
| 572 | return (p4d_t *)set_fixmap_offset(FIX_P4D, pa); |
| 573 | } |
| 574 | |
| 575 | static p4d_t *__meminit get_p4d_virt_late(phys_addr_t pa) |
| 576 | { |
| 577 | return (p4d_t *)__va(pa); |
| 578 | } |
| 579 | |
| 580 | static phys_addr_t __init alloc_p4d_early(uintptr_t va) |
| 581 | { |
| 582 | /* Only one P4D is available for early mapping */ |
| 583 | BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT); |
| 584 | |
| 585 | return (uintptr_t)early_p4d; |
| 586 | } |
| 587 | |
| 588 | static phys_addr_t __init alloc_p4d_fixmap(uintptr_t va) |
| 589 | { |
| 590 | return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); |
| 591 | } |
| 592 | |
| 593 | static phys_addr_t __meminit alloc_p4d_late(uintptr_t va) |
| 594 | { |
| 595 | struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL, 0); |
| 596 | |
| 597 | BUG_ON(!ptdesc); |
| 598 | pagetable_p4d_ctor(ptdesc); |
| 599 | return __pa((p4d_t *)ptdesc_address(ptdesc)); |
| 600 | } |
| 601 | |
| 602 | static void __meminit create_pud_mapping(pud_t *pudp, uintptr_t va, phys_addr_t pa, phys_addr_t sz, |
| 603 | pgprot_t prot) |
| 604 | { |
| 605 | pmd_t *nextp; |
| 606 | phys_addr_t next_phys; |
| 607 | uintptr_t pud_index = pud_index(va); |
| 608 | |
| 609 | if (sz == PUD_SIZE) { |
| 610 | if (pud_val(pudp[pud_index]) == 0) |
| 611 | pudp[pud_index] = pfn_pud(PFN_DOWN(pa), prot); |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | if (pud_val(pudp[pud_index]) == 0) { |
| 616 | next_phys = pt_ops.alloc_pmd(va); |
| 617 | pudp[pud_index] = pfn_pud(PFN_DOWN(next_phys), PAGE_TABLE); |
| 618 | nextp = pt_ops.get_pmd_virt(next_phys); |
| 619 | memset(nextp, 0, PAGE_SIZE); |
| 620 | } else { |
| 621 | next_phys = PFN_PHYS(_pud_pfn(pudp[pud_index])); |
| 622 | nextp = pt_ops.get_pmd_virt(next_phys); |
| 623 | } |
| 624 | |
| 625 | create_pmd_mapping(nextp, va, pa, sz, prot); |
| 626 | } |
| 627 | |
| 628 | static void __meminit create_p4d_mapping(p4d_t *p4dp, uintptr_t va, phys_addr_t pa, phys_addr_t sz, |
| 629 | pgprot_t prot) |
| 630 | { |
| 631 | pud_t *nextp; |
| 632 | phys_addr_t next_phys; |
| 633 | uintptr_t p4d_index = p4d_index(va); |
| 634 | |
| 635 | if (sz == P4D_SIZE) { |
| 636 | if (p4d_val(p4dp[p4d_index]) == 0) |
| 637 | p4dp[p4d_index] = pfn_p4d(PFN_DOWN(pa), prot); |
| 638 | return; |
| 639 | } |
| 640 | |
| 641 | if (p4d_val(p4dp[p4d_index]) == 0) { |
| 642 | next_phys = pt_ops.alloc_pud(va); |
| 643 | p4dp[p4d_index] = pfn_p4d(PFN_DOWN(next_phys), PAGE_TABLE); |
| 644 | nextp = pt_ops.get_pud_virt(next_phys); |
| 645 | memset(nextp, 0, PAGE_SIZE); |
| 646 | } else { |
| 647 | next_phys = PFN_PHYS(_p4d_pfn(p4dp[p4d_index])); |
| 648 | nextp = pt_ops.get_pud_virt(next_phys); |
| 649 | } |
| 650 | |
| 651 | create_pud_mapping(nextp, va, pa, sz, prot); |
| 652 | } |
| 653 | |
| 654 | #define pgd_next_t p4d_t |
| 655 | #define alloc_pgd_next(__va) (pgtable_l5_enabled ? \ |
| 656 | pt_ops.alloc_p4d(__va) : (pgtable_l4_enabled ? \ |
| 657 | pt_ops.alloc_pud(__va) : pt_ops.alloc_pmd(__va))) |
| 658 | #define get_pgd_next_virt(__pa) (pgtable_l5_enabled ? \ |
| 659 | pt_ops.get_p4d_virt(__pa) : (pgd_next_t *)(pgtable_l4_enabled ? \ |
| 660 | pt_ops.get_pud_virt(__pa) : (pud_t *)pt_ops.get_pmd_virt(__pa))) |
| 661 | #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ |
| 662 | (pgtable_l5_enabled ? \ |
| 663 | create_p4d_mapping(__nextp, __va, __pa, __sz, __prot) : \ |
| 664 | (pgtable_l4_enabled ? \ |
| 665 | create_pud_mapping((pud_t *)__nextp, __va, __pa, __sz, __prot) : \ |
| 666 | create_pmd_mapping((pmd_t *)__nextp, __va, __pa, __sz, __prot))) |
| 667 | #define fixmap_pgd_next (pgtable_l5_enabled ? \ |
| 668 | (uintptr_t)fixmap_p4d : (pgtable_l4_enabled ? \ |
| 669 | (uintptr_t)fixmap_pud : (uintptr_t)fixmap_pmd)) |
| 670 | #define trampoline_pgd_next (pgtable_l5_enabled ? \ |
| 671 | (uintptr_t)trampoline_p4d : (pgtable_l4_enabled ? \ |
| 672 | (uintptr_t)trampoline_pud : (uintptr_t)trampoline_pmd)) |
| 673 | #else |
| 674 | #define pgd_next_t pte_t |
| 675 | #define alloc_pgd_next(__va) pt_ops.alloc_pte(__va) |
| 676 | #define get_pgd_next_virt(__pa) pt_ops.get_pte_virt(__pa) |
| 677 | #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ |
| 678 | create_pte_mapping(__nextp, __va, __pa, __sz, __prot) |
| 679 | #define fixmap_pgd_next ((uintptr_t)fixmap_pte) |
| 680 | #define create_p4d_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0) |
| 681 | #define create_pud_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0) |
| 682 | #define create_pmd_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0) |
| 683 | #endif /* __PAGETABLE_PMD_FOLDED */ |
| 684 | |
| 685 | void __meminit create_pgd_mapping(pgd_t *pgdp, uintptr_t va, phys_addr_t pa, phys_addr_t sz, |
| 686 | pgprot_t prot) |
| 687 | { |
| 688 | pgd_next_t *nextp; |
| 689 | phys_addr_t next_phys; |
| 690 | uintptr_t pgd_idx = pgd_index(va); |
| 691 | |
| 692 | if (sz == PGDIR_SIZE) { |
| 693 | if (pgd_val(pgdp[pgd_idx]) == 0) |
| 694 | pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot); |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | if (pgd_val(pgdp[pgd_idx]) == 0) { |
| 699 | next_phys = alloc_pgd_next(va); |
| 700 | pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE); |
| 701 | nextp = get_pgd_next_virt(next_phys); |
| 702 | memset(nextp, 0, PAGE_SIZE); |
| 703 | } else { |
| 704 | next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx])); |
| 705 | nextp = get_pgd_next_virt(next_phys); |
| 706 | } |
| 707 | |
| 708 | create_pgd_next_mapping(nextp, va, pa, sz, prot); |
| 709 | } |
| 710 | |
| 711 | static uintptr_t __meminit best_map_size(phys_addr_t pa, uintptr_t va, phys_addr_t size) |
| 712 | { |
| 713 | if (debug_pagealloc_enabled()) |
| 714 | return PAGE_SIZE; |
| 715 | |
| 716 | if (pgtable_l5_enabled && |
| 717 | !(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >= P4D_SIZE) |
| 718 | return P4D_SIZE; |
| 719 | |
| 720 | if (pgtable_l4_enabled && |
| 721 | !(pa & (PUD_SIZE - 1)) && !(va & (PUD_SIZE - 1)) && size >= PUD_SIZE) |
| 722 | return PUD_SIZE; |
| 723 | |
| 724 | if (IS_ENABLED(CONFIG_64BIT) && |
| 725 | !(pa & (PMD_SIZE - 1)) && !(va & (PMD_SIZE - 1)) && size >= PMD_SIZE) |
| 726 | return PMD_SIZE; |
| 727 | |
| 728 | return PAGE_SIZE; |
| 729 | } |
| 730 | |
| 731 | #ifdef CONFIG_STRICT_KERNEL_RWX |
| 732 | static __meminit pgprot_t pgprot_from_va(uintptr_t va) |
| 733 | { |
| 734 | if (is_va_kernel_text(va)) |
| 735 | return PAGE_KERNEL_READ_EXEC; |
| 736 | |
| 737 | /* |
| 738 | * In 64-bit kernel, the kernel mapping is outside the linear mapping so |
| 739 | * we must protect its linear mapping alias from being executed and |
| 740 | * written. |
| 741 | * And rodata section is marked readonly in mark_rodata_ro. |
| 742 | */ |
| 743 | if (IS_ENABLED(CONFIG_64BIT) && is_va_kernel_lm_alias_text(va)) |
| 744 | return PAGE_KERNEL_READ; |
| 745 | |
| 746 | return PAGE_KERNEL; |
| 747 | } |
| 748 | |
| 749 | void mark_rodata_ro(void) |
| 750 | { |
| 751 | set_kernel_memory(__start_rodata, _data, set_memory_ro); |
| 752 | if (IS_ENABLED(CONFIG_64BIT)) |
| 753 | set_kernel_memory(lm_alias(__start_rodata), lm_alias(_data), |
| 754 | set_memory_ro); |
| 755 | } |
| 756 | #else |
| 757 | static __meminit pgprot_t pgprot_from_va(uintptr_t va) |
| 758 | { |
| 759 | if (IS_ENABLED(CONFIG_64BIT) && !is_kernel_mapping(va)) |
| 760 | return PAGE_KERNEL; |
| 761 | |
| 762 | return PAGE_KERNEL_EXEC; |
| 763 | } |
| 764 | #endif /* CONFIG_STRICT_KERNEL_RWX */ |
| 765 | |
| 766 | #if defined(CONFIG_64BIT) |
| 767 | u64 __pi_set_satp_mode_from_cmdline(uintptr_t dtb_pa); |
| 768 | u64 __pi_set_satp_mode_from_fdt(uintptr_t dtb_pa); |
| 769 | |
| 770 | static void __init disable_pgtable_l5(void) |
| 771 | { |
| 772 | pgtable_l5_enabled = false; |
| 773 | kernel_map.page_offset = PAGE_OFFSET_L4; |
| 774 | satp_mode = SATP_MODE_48; |
| 775 | } |
| 776 | |
| 777 | static void __init disable_pgtable_l4(void) |
| 778 | { |
| 779 | pgtable_l4_enabled = false; |
| 780 | kernel_map.page_offset = PAGE_OFFSET_L3; |
| 781 | satp_mode = SATP_MODE_39; |
| 782 | } |
| 783 | |
| 784 | static int __init print_no4lvl(char *p) |
| 785 | { |
| 786 | pr_info("Disabled 4-level and 5-level paging"); |
| 787 | return 0; |
| 788 | } |
| 789 | early_param("no4lvl", print_no4lvl); |
| 790 | |
| 791 | static int __init print_no5lvl(char *p) |
| 792 | { |
| 793 | pr_info("Disabled 5-level paging"); |
| 794 | return 0; |
| 795 | } |
| 796 | early_param("no5lvl", print_no5lvl); |
| 797 | |
| 798 | static void __init set_mmap_rnd_bits_max(void) |
| 799 | { |
| 800 | mmap_rnd_bits_max = MMAP_VA_BITS - PAGE_SHIFT - 3; |
| 801 | } |
| 802 | |
| 803 | static bool __init is_vaddr_valid(unsigned long va) |
| 804 | { |
| 805 | unsigned long up = 0; |
| 806 | |
| 807 | switch (satp_mode) { |
| 808 | case SATP_MODE_39: |
| 809 | up = 1UL << 38; |
| 810 | break; |
| 811 | case SATP_MODE_48: |
| 812 | up = 1UL << 47; |
| 813 | break; |
| 814 | case SATP_MODE_57: |
| 815 | up = 1UL << 56; |
| 816 | break; |
| 817 | default: |
| 818 | return false; |
| 819 | } |
| 820 | |
| 821 | return (va < up) || (va >= (ULONG_MAX - up + 1)); |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | * There is a simple way to determine if 4-level is supported by the |
| 826 | * underlying hardware: establish 1:1 mapping in 4-level page table mode |
| 827 | * then read SATP to see if the configuration was taken into account |
| 828 | * meaning sv48 is supported. |
| 829 | * The maximum SATP mode is limited by both the command line and the "mmu-type" |
| 830 | * property in the device tree, since some platforms may hang if an unsupported |
| 831 | * SATP mode is attempted. |
| 832 | */ |
| 833 | static __init void set_satp_mode(uintptr_t dtb_pa) |
| 834 | { |
| 835 | u64 identity_satp, hw_satp; |
| 836 | uintptr_t set_satp_mode_pmd = ((unsigned long)set_satp_mode) & PMD_MASK; |
| 837 | u64 satp_mode_limit = min_not_zero(__pi_set_satp_mode_from_cmdline(dtb_pa), |
| 838 | __pi_set_satp_mode_from_fdt(dtb_pa)); |
| 839 | |
| 840 | kernel_map.page_offset = PAGE_OFFSET_L5; |
| 841 | |
| 842 | if (satp_mode_limit == SATP_MODE_48) { |
| 843 | disable_pgtable_l5(); |
| 844 | } else if (satp_mode_limit == SATP_MODE_39) { |
| 845 | disable_pgtable_l5(); |
| 846 | disable_pgtable_l4(); |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | create_p4d_mapping(early_p4d, |
| 851 | set_satp_mode_pmd, (uintptr_t)early_pud, |
| 852 | P4D_SIZE, PAGE_TABLE); |
| 853 | create_pud_mapping(early_pud, |
| 854 | set_satp_mode_pmd, (uintptr_t)early_pmd, |
| 855 | PUD_SIZE, PAGE_TABLE); |
| 856 | /* Handle the case where set_satp_mode straddles 2 PMDs */ |
| 857 | create_pmd_mapping(early_pmd, |
| 858 | set_satp_mode_pmd, set_satp_mode_pmd, |
| 859 | PMD_SIZE, PAGE_KERNEL_EXEC); |
| 860 | create_pmd_mapping(early_pmd, |
| 861 | set_satp_mode_pmd + PMD_SIZE, |
| 862 | set_satp_mode_pmd + PMD_SIZE, |
| 863 | PMD_SIZE, PAGE_KERNEL_EXEC); |
| 864 | retry: |
| 865 | if (!is_vaddr_valid(set_satp_mode_pmd)) |
| 866 | goto out; |
| 867 | |
| 868 | create_pgd_mapping(early_pg_dir, |
| 869 | set_satp_mode_pmd, |
| 870 | pgtable_l5_enabled ? |
| 871 | (uintptr_t)early_p4d : (uintptr_t)early_pud, |
| 872 | PGDIR_SIZE, PAGE_TABLE); |
| 873 | |
| 874 | identity_satp = PFN_DOWN((uintptr_t)&early_pg_dir) | satp_mode; |
| 875 | |
| 876 | local_flush_tlb_all(); |
| 877 | csr_write(CSR_SATP, identity_satp); |
| 878 | hw_satp = csr_swap(CSR_SATP, 0ULL); |
| 879 | local_flush_tlb_all(); |
| 880 | |
| 881 | if (hw_satp != identity_satp) { |
| 882 | if (pgtable_l5_enabled) { |
| 883 | disable_pgtable_l5(); |
| 884 | memset(early_pg_dir, 0, PAGE_SIZE); |
| 885 | goto retry; |
| 886 | } |
| 887 | disable_pgtable_l4(); |
| 888 | } |
| 889 | |
| 890 | out: |
| 891 | memset(early_pg_dir, 0, PAGE_SIZE); |
| 892 | memset(early_p4d, 0, PAGE_SIZE); |
| 893 | memset(early_pud, 0, PAGE_SIZE); |
| 894 | memset(early_pmd, 0, PAGE_SIZE); |
| 895 | } |
| 896 | #endif |
| 897 | |
| 898 | /* |
| 899 | * setup_vm() is called from head.S with MMU-off. |
| 900 | * |
| 901 | * Following requirements should be honoured for setup_vm() to work |
| 902 | * correctly: |
| 903 | * 1) It should use PC-relative addressing for accessing kernel symbols. |
| 904 | * To achieve this we always use GCC cmodel=medany. |
| 905 | * 2) The compiler instrumentation for FTRACE will not work for setup_vm() |
| 906 | * so disable compiler instrumentation when FTRACE is enabled. |
| 907 | * |
| 908 | * Currently, the above requirements are honoured by using custom CFLAGS |
| 909 | * for init.o in mm/Makefile. |
| 910 | */ |
| 911 | |
| 912 | #ifndef __riscv_cmodel_medany |
| 913 | #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing." |
| 914 | #endif |
| 915 | |
| 916 | static void __init create_kernel_page_table(pgd_t *pgdir, bool early) |
| 917 | { |
| 918 | uintptr_t va, end_va; |
| 919 | |
| 920 | end_va = kernel_map.virt_addr + kernel_map.size; |
| 921 | for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE) |
| 922 | create_pgd_mapping(pgdir, va, |
| 923 | kernel_map.phys_addr + (va - kernel_map.virt_addr), |
| 924 | PMD_SIZE, |
| 925 | early ? |
| 926 | PAGE_KERNEL_EXEC : pgprot_from_va(va)); |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | * Setup a 4MB mapping that encompasses the device tree: for 64-bit kernel, |
| 931 | * this means 2 PMD entries whereas for 32-bit kernel, this is only 1 PGDIR |
| 932 | * entry. |
| 933 | */ |
| 934 | static void __init create_fdt_early_page_table(uintptr_t fix_fdt_va, |
| 935 | uintptr_t dtb_pa) |
| 936 | { |
| 937 | #ifndef CONFIG_BUILTIN_DTB |
| 938 | uintptr_t pa = dtb_pa & ~(PMD_SIZE - 1); |
| 939 | |
| 940 | /* Make sure the fdt fixmap address is always aligned on PMD size */ |
| 941 | BUILD_BUG_ON(FIX_FDT % (PMD_SIZE / PAGE_SIZE)); |
| 942 | |
| 943 | /* In 32-bit only, the fdt lies in its own PGD */ |
| 944 | if (!IS_ENABLED(CONFIG_64BIT)) { |
| 945 | create_pgd_mapping(early_pg_dir, fix_fdt_va, |
| 946 | pa, MAX_FDT_SIZE, PAGE_KERNEL); |
| 947 | } else { |
| 948 | create_pmd_mapping(fixmap_pmd, fix_fdt_va, |
| 949 | pa, PMD_SIZE, PAGE_KERNEL); |
| 950 | create_pmd_mapping(fixmap_pmd, fix_fdt_va + PMD_SIZE, |
| 951 | pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL); |
| 952 | } |
| 953 | |
| 954 | dtb_early_va = (void *)fix_fdt_va + (dtb_pa & (PMD_SIZE - 1)); |
| 955 | #else |
| 956 | /* |
| 957 | * For 64-bit kernel, __va can't be used since it would return a linear |
| 958 | * mapping address whereas dtb_early_va will be used before |
| 959 | * setup_vm_final installs the linear mapping. For 32-bit kernel, as the |
| 960 | * kernel is mapped in the linear mapping, that makes no difference. |
| 961 | */ |
| 962 | dtb_early_va = kernel_mapping_pa_to_va(dtb_pa); |
| 963 | #endif |
| 964 | |
| 965 | dtb_early_pa = dtb_pa; |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * MMU is not enabled, the page tables are allocated directly using |
| 970 | * early_pmd/pud/p4d and the address returned is the physical one. |
| 971 | */ |
| 972 | static void __init pt_ops_set_early(void) |
| 973 | { |
| 974 | pt_ops.alloc_pte = alloc_pte_early; |
| 975 | pt_ops.get_pte_virt = get_pte_virt_early; |
| 976 | #ifndef __PAGETABLE_PMD_FOLDED |
| 977 | pt_ops.alloc_pmd = alloc_pmd_early; |
| 978 | pt_ops.get_pmd_virt = get_pmd_virt_early; |
| 979 | pt_ops.alloc_pud = alloc_pud_early; |
| 980 | pt_ops.get_pud_virt = get_pud_virt_early; |
| 981 | pt_ops.alloc_p4d = alloc_p4d_early; |
| 982 | pt_ops.get_p4d_virt = get_p4d_virt_early; |
| 983 | #endif |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * MMU is enabled but page table setup is not complete yet. |
| 988 | * fixmap page table alloc functions must be used as a means to temporarily |
| 989 | * map the allocated physical pages since the linear mapping does not exist yet. |
| 990 | * |
| 991 | * Note that this is called with MMU disabled, hence kernel_mapping_pa_to_va, |
| 992 | * but it will be used as described above. |
| 993 | */ |
| 994 | static void __init pt_ops_set_fixmap(void) |
| 995 | { |
| 996 | pt_ops.alloc_pte = kernel_mapping_pa_to_va(alloc_pte_fixmap); |
| 997 | pt_ops.get_pte_virt = kernel_mapping_pa_to_va(get_pte_virt_fixmap); |
| 998 | #ifndef __PAGETABLE_PMD_FOLDED |
| 999 | pt_ops.alloc_pmd = kernel_mapping_pa_to_va(alloc_pmd_fixmap); |
| 1000 | pt_ops.get_pmd_virt = kernel_mapping_pa_to_va(get_pmd_virt_fixmap); |
| 1001 | pt_ops.alloc_pud = kernel_mapping_pa_to_va(alloc_pud_fixmap); |
| 1002 | pt_ops.get_pud_virt = kernel_mapping_pa_to_va(get_pud_virt_fixmap); |
| 1003 | pt_ops.alloc_p4d = kernel_mapping_pa_to_va(alloc_p4d_fixmap); |
| 1004 | pt_ops.get_p4d_virt = kernel_mapping_pa_to_va(get_p4d_virt_fixmap); |
| 1005 | #endif |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * MMU is enabled and page table setup is complete, so from now, we can use |
| 1010 | * generic page allocation functions to setup page table. |
| 1011 | */ |
| 1012 | static void __init pt_ops_set_late(void) |
| 1013 | { |
| 1014 | pt_ops.alloc_pte = alloc_pte_late; |
| 1015 | pt_ops.get_pte_virt = get_pte_virt_late; |
| 1016 | #ifndef __PAGETABLE_PMD_FOLDED |
| 1017 | pt_ops.alloc_pmd = alloc_pmd_late; |
| 1018 | pt_ops.get_pmd_virt = get_pmd_virt_late; |
| 1019 | pt_ops.alloc_pud = alloc_pud_late; |
| 1020 | pt_ops.get_pud_virt = get_pud_virt_late; |
| 1021 | pt_ops.alloc_p4d = alloc_p4d_late; |
| 1022 | pt_ops.get_p4d_virt = get_p4d_virt_late; |
| 1023 | #endif |
| 1024 | } |
| 1025 | |
| 1026 | #ifdef CONFIG_RANDOMIZE_BASE |
| 1027 | extern bool __init __pi_set_nokaslr_from_cmdline(uintptr_t dtb_pa); |
| 1028 | extern u64 __init __pi_get_kaslr_seed(uintptr_t dtb_pa); |
| 1029 | extern u64 __init __pi_get_kaslr_seed_zkr(const uintptr_t dtb_pa); |
| 1030 | |
| 1031 | static int __init print_nokaslr(char *p) |
| 1032 | { |
| 1033 | pr_info("Disabled KASLR"); |
| 1034 | return 0; |
| 1035 | } |
| 1036 | early_param("nokaslr", print_nokaslr); |
| 1037 | #endif |
| 1038 | |
| 1039 | asmlinkage void __init setup_vm(uintptr_t dtb_pa) |
| 1040 | { |
| 1041 | pmd_t __maybe_unused fix_bmap_spmd, fix_bmap_epmd; |
| 1042 | |
| 1043 | #ifdef CONFIG_RANDOMIZE_BASE |
| 1044 | if (!__pi_set_nokaslr_from_cmdline(dtb_pa)) { |
| 1045 | u64 kaslr_seed = __pi_get_kaslr_seed_zkr(dtb_pa); |
| 1046 | u32 kernel_size = (uintptr_t)(&_end) - (uintptr_t)(&_start); |
| 1047 | u32 nr_pos; |
| 1048 | |
| 1049 | if (kaslr_seed == 0) |
| 1050 | kaslr_seed = __pi_get_kaslr_seed(dtb_pa); |
| 1051 | /* |
| 1052 | * Compute the number of positions available: we are limited |
| 1053 | * by the early page table that only has one PUD and we must |
| 1054 | * be aligned on PMD_SIZE. |
| 1055 | */ |
| 1056 | nr_pos = (PUD_SIZE - kernel_size) / PMD_SIZE; |
| 1057 | |
| 1058 | kernel_map.virt_offset = (kaslr_seed % nr_pos) * PMD_SIZE; |
| 1059 | } |
| 1060 | #endif |
| 1061 | |
| 1062 | kernel_map.virt_addr = KERNEL_LINK_ADDR + kernel_map.virt_offset; |
| 1063 | |
| 1064 | kernel_map.phys_addr = (uintptr_t)(&_start); |
| 1065 | kernel_map.size = (uintptr_t)(&_end) - kernel_map.phys_addr; |
| 1066 | kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr; |
| 1067 | |
| 1068 | #if defined(CONFIG_64BIT) |
| 1069 | set_satp_mode(dtb_pa); |
| 1070 | set_mmap_rnd_bits_max(); |
| 1071 | #endif |
| 1072 | |
| 1073 | /* |
| 1074 | * In 64-bit, we defer the setup of va_pa_offset to setup_bootmem, |
| 1075 | * where we have the system memory layout: this allows us to align |
| 1076 | * the physical and virtual mappings and then make use of PUD/P4D/PGD |
| 1077 | * for the linear mapping. This is only possible because the kernel |
| 1078 | * mapping lies outside the linear mapping. |
| 1079 | * In 32-bit however, as the kernel resides in the linear mapping, |
| 1080 | * setup_vm_final can not change the mapping established here, |
| 1081 | * otherwise the same kernel addresses would get mapped to different |
| 1082 | * physical addresses (if the start of dram is different from the |
| 1083 | * kernel physical address start). |
| 1084 | */ |
| 1085 | kernel_map.va_pa_offset = IS_ENABLED(CONFIG_64BIT) ? |
| 1086 | 0UL : PAGE_OFFSET - kernel_map.phys_addr; |
| 1087 | |
| 1088 | memory_limit = KERN_VIRT_SIZE; |
| 1089 | |
| 1090 | /* Sanity check alignment and size */ |
| 1091 | BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0); |
| 1092 | BUG_ON((kernel_map.phys_addr % PMD_SIZE) != 0); |
| 1093 | |
| 1094 | #ifdef CONFIG_64BIT |
| 1095 | /* |
| 1096 | * The last 4K bytes of the addressable memory can not be mapped because |
| 1097 | * of IS_ERR_VALUE macro. |
| 1098 | */ |
| 1099 | BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K); |
| 1100 | #endif |
| 1101 | |
| 1102 | #ifdef CONFIG_RELOCATABLE |
| 1103 | /* |
| 1104 | * Early page table uses only one PUD, which makes it possible |
| 1105 | * to map PUD_SIZE aligned on PUD_SIZE: if the relocation offset |
| 1106 | * makes the kernel cross over a PUD_SIZE boundary, raise a bug |
| 1107 | * since a part of the kernel would not get mapped. |
| 1108 | */ |
| 1109 | if (IS_ENABLED(CONFIG_64BIT)) |
| 1110 | BUG_ON(PUD_SIZE - (kernel_map.virt_addr & (PUD_SIZE - 1)) < kernel_map.size); |
| 1111 | relocate_kernel(); |
| 1112 | #endif |
| 1113 | |
| 1114 | apply_early_boot_alternatives(); |
| 1115 | pt_ops_set_early(); |
| 1116 | |
| 1117 | /* Setup early PGD for fixmap */ |
| 1118 | create_pgd_mapping(early_pg_dir, FIXADDR_START, |
| 1119 | fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE); |
| 1120 | |
| 1121 | #ifndef __PAGETABLE_PMD_FOLDED |
| 1122 | /* Setup fixmap P4D and PUD */ |
| 1123 | if (pgtable_l5_enabled) |
| 1124 | create_p4d_mapping(fixmap_p4d, FIXADDR_START, |
| 1125 | (uintptr_t)fixmap_pud, P4D_SIZE, PAGE_TABLE); |
| 1126 | /* Setup fixmap PUD and PMD */ |
| 1127 | if (pgtable_l4_enabled) |
| 1128 | create_pud_mapping(fixmap_pud, FIXADDR_START, |
| 1129 | (uintptr_t)fixmap_pmd, PUD_SIZE, PAGE_TABLE); |
| 1130 | create_pmd_mapping(fixmap_pmd, FIXADDR_START, |
| 1131 | (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE); |
| 1132 | /* Setup trampoline PGD and PMD */ |
| 1133 | create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr, |
| 1134 | trampoline_pgd_next, PGDIR_SIZE, PAGE_TABLE); |
| 1135 | if (pgtable_l5_enabled) |
| 1136 | create_p4d_mapping(trampoline_p4d, kernel_map.virt_addr, |
| 1137 | (uintptr_t)trampoline_pud, P4D_SIZE, PAGE_TABLE); |
| 1138 | if (pgtable_l4_enabled) |
| 1139 | create_pud_mapping(trampoline_pud, kernel_map.virt_addr, |
| 1140 | (uintptr_t)trampoline_pmd, PUD_SIZE, PAGE_TABLE); |
| 1141 | create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr, |
| 1142 | kernel_map.phys_addr, PMD_SIZE, PAGE_KERNEL_EXEC); |
| 1143 | #else |
| 1144 | /* Setup trampoline PGD */ |
| 1145 | create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr, |
| 1146 | kernel_map.phys_addr, PGDIR_SIZE, PAGE_KERNEL_EXEC); |
| 1147 | #endif |
| 1148 | |
| 1149 | /* |
| 1150 | * Setup early PGD covering entire kernel which will allow |
| 1151 | * us to reach paging_init(). We map all memory banks later |
| 1152 | * in setup_vm_final() below. |
| 1153 | */ |
| 1154 | create_kernel_page_table(early_pg_dir, true); |
| 1155 | |
| 1156 | /* Setup early mapping for FDT early scan */ |
| 1157 | create_fdt_early_page_table(__fix_to_virt(FIX_FDT), dtb_pa); |
| 1158 | |
| 1159 | /* |
| 1160 | * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap |
| 1161 | * range can not span multiple pmds. |
| 1162 | */ |
| 1163 | BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) |
| 1164 | != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); |
| 1165 | |
| 1166 | #ifndef __PAGETABLE_PMD_FOLDED |
| 1167 | /* |
| 1168 | * Early ioremap fixmap is already created as it lies within first 2MB |
| 1169 | * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END |
| 1170 | * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn |
| 1171 | * the user if not. |
| 1172 | */ |
| 1173 | fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))]; |
| 1174 | fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))]; |
| 1175 | if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) { |
| 1176 | WARN_ON(1); |
| 1177 | pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n", |
| 1178 | pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd)); |
| 1179 | pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", |
| 1180 | fix_to_virt(FIX_BTMAP_BEGIN)); |
| 1181 | pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n", |
| 1182 | fix_to_virt(FIX_BTMAP_END)); |
| 1183 | |
| 1184 | pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END); |
| 1185 | pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN); |
| 1186 | } |
| 1187 | #endif |
| 1188 | |
| 1189 | pt_ops_set_fixmap(); |
| 1190 | } |
| 1191 | |
| 1192 | static void __meminit create_linear_mapping_range(phys_addr_t start, phys_addr_t end, |
| 1193 | uintptr_t fixed_map_size, const pgprot_t *pgprot) |
| 1194 | { |
| 1195 | phys_addr_t pa; |
| 1196 | uintptr_t va, map_size; |
| 1197 | |
| 1198 | for (pa = start; pa < end; pa += map_size) { |
| 1199 | va = (uintptr_t)__va(pa); |
| 1200 | map_size = fixed_map_size ? fixed_map_size : |
| 1201 | best_map_size(pa, va, end - pa); |
| 1202 | |
| 1203 | create_pgd_mapping(swapper_pg_dir, va, pa, map_size, |
| 1204 | pgprot ? *pgprot : pgprot_from_va(va)); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | static void __init create_linear_mapping_page_table(void) |
| 1209 | { |
| 1210 | phys_addr_t start, end; |
| 1211 | phys_addr_t kfence_pool __maybe_unused; |
| 1212 | u64 i; |
| 1213 | |
| 1214 | #ifdef CONFIG_STRICT_KERNEL_RWX |
| 1215 | phys_addr_t ktext_start = __pa_symbol(_start); |
| 1216 | phys_addr_t ktext_size = __init_data_begin - _start; |
| 1217 | phys_addr_t krodata_start = __pa_symbol(__start_rodata); |
| 1218 | phys_addr_t krodata_size = _data - __start_rodata; |
| 1219 | |
| 1220 | /* Isolate kernel text and rodata so they don't get mapped with a PUD */ |
| 1221 | memblock_mark_nomap(ktext_start, ktext_size); |
| 1222 | memblock_mark_nomap(krodata_start, krodata_size); |
| 1223 | #endif |
| 1224 | |
| 1225 | #ifdef CONFIG_KFENCE |
| 1226 | /* |
| 1227 | * kfence pool must be backed by PAGE_SIZE mappings, so allocate it |
| 1228 | * before we setup the linear mapping so that we avoid using hugepages |
| 1229 | * for this region. |
| 1230 | */ |
| 1231 | kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE); |
| 1232 | BUG_ON(!kfence_pool); |
| 1233 | |
| 1234 | memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE); |
| 1235 | __kfence_pool = __va(kfence_pool); |
| 1236 | #endif |
| 1237 | |
| 1238 | /* Map all memory banks in the linear mapping */ |
| 1239 | for_each_mem_range(i, &start, &end) { |
| 1240 | if (start <= __pa(PAGE_OFFSET) && |
| 1241 | __pa(PAGE_OFFSET) < end) |
| 1242 | start = __pa(PAGE_OFFSET); |
| 1243 | |
| 1244 | create_linear_mapping_range(start, end, 0, NULL); |
| 1245 | } |
| 1246 | |
| 1247 | #ifdef CONFIG_STRICT_KERNEL_RWX |
| 1248 | create_linear_mapping_range(ktext_start, ktext_start + ktext_size, 0, NULL); |
| 1249 | create_linear_mapping_range(krodata_start, krodata_start + krodata_size, 0, NULL); |
| 1250 | |
| 1251 | memblock_clear_nomap(ktext_start, ktext_size); |
| 1252 | memblock_clear_nomap(krodata_start, krodata_size); |
| 1253 | #endif |
| 1254 | |
| 1255 | #ifdef CONFIG_KFENCE |
| 1256 | create_linear_mapping_range(kfence_pool, kfence_pool + KFENCE_POOL_SIZE, PAGE_SIZE, NULL); |
| 1257 | |
| 1258 | memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE); |
| 1259 | #endif |
| 1260 | } |
| 1261 | |
| 1262 | static void __init setup_vm_final(void) |
| 1263 | { |
| 1264 | /* Setup swapper PGD for fixmap */ |
| 1265 | #if !defined(CONFIG_64BIT) |
| 1266 | /* |
| 1267 | * In 32-bit, the device tree lies in a pgd entry, so it must be copied |
| 1268 | * directly in swapper_pg_dir in addition to the pgd entry that points |
| 1269 | * to fixmap_pte. |
| 1270 | */ |
| 1271 | unsigned long idx = pgd_index(__fix_to_virt(FIX_FDT)); |
| 1272 | |
| 1273 | set_pgd(&swapper_pg_dir[idx], early_pg_dir[idx]); |
| 1274 | #endif |
| 1275 | create_pgd_mapping(swapper_pg_dir, FIXADDR_START, |
| 1276 | __pa_symbol(fixmap_pgd_next), |
| 1277 | PGDIR_SIZE, PAGE_TABLE); |
| 1278 | |
| 1279 | /* Map the linear mapping */ |
| 1280 | create_linear_mapping_page_table(); |
| 1281 | |
| 1282 | /* Map the kernel */ |
| 1283 | if (IS_ENABLED(CONFIG_64BIT)) |
| 1284 | create_kernel_page_table(swapper_pg_dir, false); |
| 1285 | |
| 1286 | #ifdef CONFIG_KASAN |
| 1287 | kasan_swapper_init(); |
| 1288 | #endif |
| 1289 | |
| 1290 | /* Clear fixmap PTE and PMD mappings */ |
| 1291 | clear_fixmap(FIX_PTE); |
| 1292 | clear_fixmap(FIX_PMD); |
| 1293 | clear_fixmap(FIX_PUD); |
| 1294 | clear_fixmap(FIX_P4D); |
| 1295 | |
| 1296 | /* Move to swapper page table */ |
| 1297 | csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode); |
| 1298 | local_flush_tlb_all(); |
| 1299 | |
| 1300 | pt_ops_set_late(); |
| 1301 | } |
| 1302 | #else |
| 1303 | asmlinkage void __init setup_vm(uintptr_t dtb_pa) |
| 1304 | { |
| 1305 | dtb_early_va = (void *)dtb_pa; |
| 1306 | dtb_early_pa = dtb_pa; |
| 1307 | |
| 1308 | #ifdef CONFIG_RELOCATABLE |
| 1309 | kernel_map.virt_addr = (uintptr_t)_start; |
| 1310 | kernel_map.phys_addr = (uintptr_t)_start; |
| 1311 | relocate_kernel(); |
| 1312 | #endif |
| 1313 | } |
| 1314 | |
| 1315 | static inline void setup_vm_final(void) |
| 1316 | { |
| 1317 | } |
| 1318 | #endif /* CONFIG_MMU */ |
| 1319 | |
| 1320 | /* |
| 1321 | * reserve_crashkernel() - reserves memory for crash kernel |
| 1322 | * |
| 1323 | * This function reserves memory area given in "crashkernel=" kernel command |
| 1324 | * line parameter. The memory reserved is used by dump capture kernel when |
| 1325 | * primary kernel is crashing. |
| 1326 | */ |
| 1327 | static void __init arch_reserve_crashkernel(void) |
| 1328 | { |
| 1329 | unsigned long long low_size = 0, cma_size = 0; |
| 1330 | unsigned long long crash_base, crash_size; |
| 1331 | bool high = false; |
| 1332 | int ret; |
| 1333 | |
| 1334 | if (!IS_ENABLED(CONFIG_CRASH_RESERVE)) |
| 1335 | return; |
| 1336 | |
| 1337 | ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), |
| 1338 | &crash_size, &crash_base, |
| 1339 | &low_size, &cma_size, &high); |
| 1340 | if (ret) |
| 1341 | return; |
| 1342 | |
| 1343 | reserve_crashkernel_generic(crash_size, crash_base, low_size, high); |
| 1344 | reserve_crashkernel_cma(cma_size); |
| 1345 | } |
| 1346 | |
| 1347 | void __init paging_init(void) |
| 1348 | { |
| 1349 | setup_bootmem(); |
| 1350 | setup_vm_final(); |
| 1351 | |
| 1352 | /* Depend on that Linear Mapping is ready */ |
| 1353 | memblock_allow_resize(); |
| 1354 | } |
| 1355 | |
| 1356 | void __init misc_mem_init(void) |
| 1357 | { |
| 1358 | early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT); |
| 1359 | arch_numa_init(); |
| 1360 | dma_contiguous_reserve(dma32_phys_limit); |
| 1361 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 1362 | /* The entire VMEMMAP region has been populated. Flush TLB for this region */ |
| 1363 | local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END); |
| 1364 | #endif |
| 1365 | arch_reserve_crashkernel(); |
| 1366 | memblock_dump_all(); |
| 1367 | } |
| 1368 | |
| 1369 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 1370 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, |
| 1371 | struct vmem_altmap *altmap) |
| 1372 | { |
| 1373 | WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); |
| 1374 | |
| 1375 | /* |
| 1376 | * Note that SPARSEMEM_VMEMMAP is only selected for rv64 and that we |
| 1377 | * can't use hugepage mappings for 2-level page table because in case of |
| 1378 | * memory hotplug, we are not able to update all the page tables with |
| 1379 | * the new PMDs. |
| 1380 | */ |
| 1381 | return vmemmap_populate_hugepages(start, end, node, altmap); |
| 1382 | } |
| 1383 | #endif |
| 1384 | |
| 1385 | #if defined(CONFIG_MMU) && defined(CONFIG_64BIT) |
| 1386 | /* |
| 1387 | * Pre-allocates page-table pages for a specific area in the kernel |
| 1388 | * page-table. Only the level which needs to be synchronized between |
| 1389 | * all page-tables is allocated because the synchronization can be |
| 1390 | * expensive. |
| 1391 | */ |
| 1392 | static void __init preallocate_pgd_pages_range(unsigned long start, unsigned long end, |
| 1393 | const char *area) |
| 1394 | { |
| 1395 | unsigned long addr; |
| 1396 | const char *lvl; |
| 1397 | |
| 1398 | for (addr = start; addr < end && addr >= start; addr = ALIGN(addr + 1, PGDIR_SIZE)) { |
| 1399 | pgd_t *pgd = pgd_offset_k(addr); |
| 1400 | p4d_t *p4d; |
| 1401 | pud_t *pud; |
| 1402 | pmd_t *pmd; |
| 1403 | |
| 1404 | lvl = "p4d"; |
| 1405 | p4d = p4d_alloc(&init_mm, pgd, addr); |
| 1406 | if (!p4d) |
| 1407 | goto failed; |
| 1408 | |
| 1409 | if (pgtable_l5_enabled) |
| 1410 | continue; |
| 1411 | |
| 1412 | lvl = "pud"; |
| 1413 | pud = pud_alloc(&init_mm, p4d, addr); |
| 1414 | if (!pud) |
| 1415 | goto failed; |
| 1416 | |
| 1417 | if (pgtable_l4_enabled) |
| 1418 | continue; |
| 1419 | |
| 1420 | lvl = "pmd"; |
| 1421 | pmd = pmd_alloc(&init_mm, pud, addr); |
| 1422 | if (!pmd) |
| 1423 | goto failed; |
| 1424 | } |
| 1425 | return; |
| 1426 | |
| 1427 | failed: |
| 1428 | /* |
| 1429 | * The pages have to be there now or they will be missing in |
| 1430 | * process page-tables later. |
| 1431 | */ |
| 1432 | panic("Failed to pre-allocate %s pages for %s area\n", lvl, area); |
| 1433 | } |
| 1434 | |
| 1435 | #define PAGE_END KASAN_SHADOW_START |
| 1436 | |
| 1437 | void __init pgtable_cache_init(void) |
| 1438 | { |
| 1439 | preallocate_pgd_pages_range(VMALLOC_START, VMALLOC_END, "vmalloc"); |
| 1440 | if (IS_ENABLED(CONFIG_MODULES)) |
| 1441 | preallocate_pgd_pages_range(MODULES_VADDR, MODULES_END, "bpf/modules"); |
| 1442 | if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) { |
| 1443 | preallocate_pgd_pages_range(VMEMMAP_START, VMEMMAP_END, "vmemmap"); |
| 1444 | preallocate_pgd_pages_range(PAGE_OFFSET, PAGE_END, "direct map"); |
| 1445 | if (IS_ENABLED(CONFIG_KASAN)) |
| 1446 | preallocate_pgd_pages_range(KASAN_SHADOW_START, KASAN_SHADOW_END, "kasan"); |
| 1447 | } |
| 1448 | } |
| 1449 | #endif |
| 1450 | |
| 1451 | #ifdef CONFIG_EXECMEM |
| 1452 | #ifdef CONFIG_MMU |
| 1453 | static struct execmem_info execmem_info __ro_after_init; |
| 1454 | |
| 1455 | struct execmem_info __init *execmem_arch_setup(void) |
| 1456 | { |
| 1457 | execmem_info = (struct execmem_info){ |
| 1458 | .ranges = { |
| 1459 | [EXECMEM_DEFAULT] = { |
| 1460 | .start = MODULES_VADDR, |
| 1461 | .end = MODULES_END, |
| 1462 | .pgprot = PAGE_KERNEL, |
| 1463 | .alignment = 1, |
| 1464 | }, |
| 1465 | [EXECMEM_KPROBES] = { |
| 1466 | .start = VMALLOC_START, |
| 1467 | .end = VMALLOC_END, |
| 1468 | .pgprot = IS_ENABLED(CONFIG_STRICT_MODULE_RWX) ? |
| 1469 | PAGE_KERNEL_READ_EXEC : |
| 1470 | PAGE_KERNEL_EXEC, |
| 1471 | .alignment = 1, |
| 1472 | }, |
| 1473 | [EXECMEM_BPF] = { |
| 1474 | .start = BPF_JIT_REGION_START, |
| 1475 | .end = BPF_JIT_REGION_END, |
| 1476 | .pgprot = PAGE_KERNEL, |
| 1477 | .alignment = PAGE_SIZE, |
| 1478 | }, |
| 1479 | }, |
| 1480 | }; |
| 1481 | |
| 1482 | return &execmem_info; |
| 1483 | } |
| 1484 | #endif /* CONFIG_MMU */ |
| 1485 | #endif /* CONFIG_EXECMEM */ |
| 1486 | |
| 1487 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 1488 | static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd) |
| 1489 | { |
| 1490 | struct page *page = pmd_page(*pmd); |
| 1491 | struct ptdesc *ptdesc = page_ptdesc(page); |
| 1492 | pte_t *pte; |
| 1493 | int i; |
| 1494 | |
| 1495 | for (i = 0; i < PTRS_PER_PTE; i++) { |
| 1496 | pte = pte_start + i; |
| 1497 | if (!pte_none(*pte)) |
| 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | pagetable_dtor(ptdesc); |
| 1502 | if (PageReserved(page)) |
| 1503 | free_reserved_page(page); |
| 1504 | else |
| 1505 | pagetable_free(ptdesc); |
| 1506 | pmd_clear(pmd); |
| 1507 | } |
| 1508 | |
| 1509 | static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud, bool is_vmemmap) |
| 1510 | { |
| 1511 | struct page *page = pud_page(*pud); |
| 1512 | struct ptdesc *ptdesc = page_ptdesc(page); |
| 1513 | pmd_t *pmd; |
| 1514 | int i; |
| 1515 | |
| 1516 | for (i = 0; i < PTRS_PER_PMD; i++) { |
| 1517 | pmd = pmd_start + i; |
| 1518 | if (!pmd_none(*pmd)) |
| 1519 | return; |
| 1520 | } |
| 1521 | |
| 1522 | if (!is_vmemmap) |
| 1523 | pagetable_dtor(ptdesc); |
| 1524 | if (PageReserved(page)) |
| 1525 | free_reserved_page(page); |
| 1526 | else |
| 1527 | pagetable_free(ptdesc); |
| 1528 | pud_clear(pud); |
| 1529 | } |
| 1530 | |
| 1531 | static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d) |
| 1532 | { |
| 1533 | struct page *page = p4d_page(*p4d); |
| 1534 | pud_t *pud; |
| 1535 | int i; |
| 1536 | |
| 1537 | for (i = 0; i < PTRS_PER_PUD; i++) { |
| 1538 | pud = pud_start + i; |
| 1539 | if (!pud_none(*pud)) |
| 1540 | return; |
| 1541 | } |
| 1542 | |
| 1543 | if (PageReserved(page)) |
| 1544 | free_reserved_page(page); |
| 1545 | else |
| 1546 | __free_pages(page, 0); |
| 1547 | p4d_clear(p4d); |
| 1548 | } |
| 1549 | |
| 1550 | static void __meminit free_vmemmap_storage(struct page *page, size_t size, |
| 1551 | struct vmem_altmap *altmap) |
| 1552 | { |
| 1553 | int order = get_order(size); |
| 1554 | |
| 1555 | if (altmap) { |
| 1556 | vmem_altmap_free(altmap, size >> PAGE_SHIFT); |
| 1557 | return; |
| 1558 | } |
| 1559 | |
| 1560 | if (PageReserved(page)) { |
| 1561 | unsigned int nr_pages = 1 << order; |
| 1562 | |
| 1563 | while (nr_pages--) |
| 1564 | free_reserved_page(page++); |
| 1565 | return; |
| 1566 | } |
| 1567 | |
| 1568 | __free_pages(page, order); |
| 1569 | } |
| 1570 | |
| 1571 | static void __meminit remove_pte_mapping(pte_t *pte_base, unsigned long addr, unsigned long end, |
| 1572 | bool is_vmemmap, struct vmem_altmap *altmap) |
| 1573 | { |
| 1574 | unsigned long next; |
| 1575 | pte_t *ptep, pte; |
| 1576 | |
| 1577 | for (; addr < end; addr = next) { |
| 1578 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 1579 | if (next > end) |
| 1580 | next = end; |
| 1581 | |
| 1582 | ptep = pte_base + pte_index(addr); |
| 1583 | pte = ptep_get(ptep); |
| 1584 | if (!pte_present(*ptep)) |
| 1585 | continue; |
| 1586 | |
| 1587 | pte_clear(&init_mm, addr, ptep); |
| 1588 | if (is_vmemmap) |
| 1589 | free_vmemmap_storage(pte_page(pte), PAGE_SIZE, altmap); |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | static void __meminit remove_pmd_mapping(pmd_t *pmd_base, unsigned long addr, unsigned long end, |
| 1594 | bool is_vmemmap, struct vmem_altmap *altmap) |
| 1595 | { |
| 1596 | unsigned long next; |
| 1597 | pte_t *pte_base; |
| 1598 | pmd_t *pmdp, pmd; |
| 1599 | |
| 1600 | for (; addr < end; addr = next) { |
| 1601 | next = pmd_addr_end(addr, end); |
| 1602 | pmdp = pmd_base + pmd_index(addr); |
| 1603 | pmd = pmdp_get(pmdp); |
| 1604 | if (!pmd_present(pmd)) |
| 1605 | continue; |
| 1606 | |
| 1607 | if (pmd_leaf(pmd)) { |
| 1608 | pmd_clear(pmdp); |
| 1609 | if (is_vmemmap) |
| 1610 | free_vmemmap_storage(pmd_page(pmd), PMD_SIZE, altmap); |
| 1611 | continue; |
| 1612 | } |
| 1613 | |
| 1614 | pte_base = (pte_t *)pmd_page_vaddr(*pmdp); |
| 1615 | remove_pte_mapping(pte_base, addr, next, is_vmemmap, altmap); |
| 1616 | free_pte_table(pte_base, pmdp); |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | static void __meminit remove_pud_mapping(pud_t *pud_base, unsigned long addr, unsigned long end, |
| 1621 | bool is_vmemmap, struct vmem_altmap *altmap) |
| 1622 | { |
| 1623 | unsigned long next; |
| 1624 | pud_t *pudp, pud; |
| 1625 | pmd_t *pmd_base; |
| 1626 | |
| 1627 | for (; addr < end; addr = next) { |
| 1628 | next = pud_addr_end(addr, end); |
| 1629 | pudp = pgtable_l4_enabled ? pud_base + pud_index(addr) : pud_base; |
| 1630 | pud = pudp_get(pudp); |
| 1631 | if (!pud_present(pud)) |
| 1632 | continue; |
| 1633 | |
| 1634 | if (pud_leaf(pud)) { |
| 1635 | if (pgtable_l4_enabled) { |
| 1636 | pud_clear(pudp); |
| 1637 | if (is_vmemmap) |
| 1638 | free_vmemmap_storage(pud_page(pud), PUD_SIZE, altmap); |
| 1639 | } |
| 1640 | continue; |
| 1641 | } |
| 1642 | |
| 1643 | pmd_base = pmd_offset(pudp, 0); |
| 1644 | remove_pmd_mapping(pmd_base, addr, next, is_vmemmap, altmap); |
| 1645 | |
| 1646 | if (pgtable_l4_enabled) |
| 1647 | free_pmd_table(pmd_base, pudp, is_vmemmap); |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | static void __meminit remove_p4d_mapping(p4d_t *p4d_base, unsigned long addr, unsigned long end, |
| 1652 | bool is_vmemmap, struct vmem_altmap *altmap) |
| 1653 | { |
| 1654 | unsigned long next; |
| 1655 | p4d_t *p4dp, p4d; |
| 1656 | pud_t *pud_base; |
| 1657 | |
| 1658 | for (; addr < end; addr = next) { |
| 1659 | next = p4d_addr_end(addr, end); |
| 1660 | p4dp = pgtable_l5_enabled ? p4d_base + p4d_index(addr) : p4d_base; |
| 1661 | p4d = p4dp_get(p4dp); |
| 1662 | if (!p4d_present(p4d)) |
| 1663 | continue; |
| 1664 | |
| 1665 | if (p4d_leaf(p4d)) { |
| 1666 | if (pgtable_l5_enabled) { |
| 1667 | p4d_clear(p4dp); |
| 1668 | if (is_vmemmap) |
| 1669 | free_vmemmap_storage(p4d_page(p4d), P4D_SIZE, altmap); |
| 1670 | } |
| 1671 | continue; |
| 1672 | } |
| 1673 | |
| 1674 | pud_base = pud_offset(p4dp, 0); |
| 1675 | remove_pud_mapping(pud_base, addr, next, is_vmemmap, altmap); |
| 1676 | |
| 1677 | if (pgtable_l5_enabled) |
| 1678 | free_pud_table(pud_base, p4dp); |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | static void __meminit remove_pgd_mapping(unsigned long va, unsigned long end, bool is_vmemmap, |
| 1683 | struct vmem_altmap *altmap) |
| 1684 | { |
| 1685 | unsigned long addr, next; |
| 1686 | p4d_t *p4d_base; |
| 1687 | pgd_t *pgd; |
| 1688 | |
| 1689 | for (addr = va; addr < end; addr = next) { |
| 1690 | next = pgd_addr_end(addr, end); |
| 1691 | pgd = pgd_offset_k(addr); |
| 1692 | |
| 1693 | if (!pgd_present(*pgd)) |
| 1694 | continue; |
| 1695 | |
| 1696 | if (pgd_leaf(*pgd)) |
| 1697 | continue; |
| 1698 | |
| 1699 | p4d_base = p4d_offset(pgd, 0); |
| 1700 | remove_p4d_mapping(p4d_base, addr, next, is_vmemmap, altmap); |
| 1701 | } |
| 1702 | |
| 1703 | flush_tlb_all(); |
| 1704 | } |
| 1705 | |
| 1706 | static void __meminit remove_linear_mapping(phys_addr_t start, u64 size) |
| 1707 | { |
| 1708 | unsigned long va = (unsigned long)__va(start); |
| 1709 | unsigned long end = (unsigned long)__va(start + size); |
| 1710 | |
| 1711 | remove_pgd_mapping(va, end, false, NULL); |
| 1712 | } |
| 1713 | |
| 1714 | struct range arch_get_mappable_range(void) |
| 1715 | { |
| 1716 | struct range mhp_range; |
| 1717 | |
| 1718 | mhp_range.start = __pa(PAGE_OFFSET); |
| 1719 | mhp_range.end = __pa(PAGE_END - 1); |
| 1720 | return mhp_range; |
| 1721 | } |
| 1722 | |
| 1723 | int __ref arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params) |
| 1724 | { |
| 1725 | int ret = 0; |
| 1726 | |
| 1727 | create_linear_mapping_range(start, start + size, 0, ¶ms->pgprot); |
| 1728 | ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT, params); |
| 1729 | if (ret) { |
| 1730 | remove_linear_mapping(start, size); |
| 1731 | goto out; |
| 1732 | } |
| 1733 | |
| 1734 | max_pfn = PFN_UP(start + size); |
| 1735 | max_low_pfn = max_pfn; |
| 1736 | |
| 1737 | out: |
| 1738 | flush_tlb_all(); |
| 1739 | return ret; |
| 1740 | } |
| 1741 | |
| 1742 | void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap, |
| 1743 | struct dev_pagemap *pgmap) |
| 1744 | { |
| 1745 | __remove_pages(start >> PAGE_SHIFT, size >> PAGE_SHIFT, altmap, pgmap); |
| 1746 | remove_linear_mapping(start, size); |
| 1747 | flush_tlb_all(); |
| 1748 | } |
| 1749 | |
| 1750 | void __ref vmemmap_free(unsigned long start, unsigned long end, struct vmem_altmap *altmap) |
| 1751 | { |
| 1752 | remove_pgd_mapping(start, end, true, altmap); |
| 1753 | } |
| 1754 | #endif /* CONFIG_MEMORY_HOTPLUG */ |