// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2012 Regents of the University of California * Copyright (C) 2019 Western Digital Corporation or its affiliates. * Copyright (C) 2020 FORTH-ICS/CARV * Nick Kossifidis <mick@ics.forth.gr>
*/
if (IS_ENABLED(CONFIG_XIP_KERNEL))
vmlinux_start = __pa_symbol(&_sdata); else
vmlinux_start = __pa_symbol(&_start);
memblock_enforce_memory_limit(memory_limit);
/* * Make sure we align the reservation on PMD_SIZE since we will * map the kernel in the linear mapping as read-only: we do not want * any allocation to happen between _end and the next pmd aligned page.
*/ if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK; /* * Reserve from the start of the kernel to the end of the kernel
*/
memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
/* * Make sure we align the start of the memory on a PMD boundary so that * at worst, we map the linear mapping with PMD mappings.
*/ if (!IS_ENABLED(CONFIG_XIP_KERNEL)) {
phys_ram_base = memblock_start_of_DRAM() & PMD_MASK; #ifdef CONFIG_SPARSEMEM_VMEMMAP
vmemmap_start_pfn = round_down(phys_ram_base, VMEMMAP_ADDR_ALIGN) >> PAGE_SHIFT; #endif
}
/* * In 64-bit, any use of __va/__pa before this point is wrong as we * did not know the start of DRAM before.
*/ if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU))
kernel_map.va_pa_offset = PAGE_OFFSET - phys_ram_base;
/* * The size of the linear page mapping may restrict the amount of * usable RAM.
*/ if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU)) {
max_mapped_addr = __pa(PAGE_OFFSET) + KERN_VIRT_SIZE; if (memblock_end_of_DRAM() > max_mapped_addr) {
memblock_cap_memory_range(phys_ram_base,
max_mapped_addr - phys_ram_base);
pr_warn("Physical memory overflows the linear mapping size: region above %pa removed",
&max_mapped_addr);
}
}
/* * Reserve physical address space that would be mapped to virtual * addresses greater than (void *)(-PAGE_SIZE) because: * - This memory would overlap with ERR_PTR * - This memory belongs to high memory, which is not supported * * This is not applicable to 64-bit kernel, because virtual addresses * after (void *)(-PAGE_SIZE) are not linearly mapped: they are * occupied by kernel mapping. Also it is unrealistic for high memory * to exist on 64-bit platforms.
*/ if (!IS_ENABLED(CONFIG_64BIT)) {
max_mapped_addr = __va_to_pa_nodebug(-PAGE_SIZE);
memblock_reserve(max_mapped_addr, (phys_addr_t)-max_mapped_addr);
}
/* * No allocation should be done before reserving the memory as defined * in the device tree, otherwise the allocation could end up in a * reserved region.
*/
early_init_fdt_scan_reserved_mem();
/* * If DTB is built in, no need to reserve its memblock. * Otherwise, do reserve it but avoid using * early_init_fdt_reserve_self() since __pa() does * not work for DTB pointers that are fixmap addresses
*/ if (!IS_ENABLED(CONFIG_BUILTIN_DTB))
memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
dma_contiguous_reserve(dma32_phys_limit); if (IS_ENABLED(CONFIG_64BIT))
hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
}
/* * Make sure to not relocate vdso symbols like rt_sigreturn * which are linked from the address 0 in vmlinux since * vdso symbol addresses are actually used as an offset from * mm->context.vdso in VDSO_OFFSET macro.
*/ if (relocated_addr >= KERNEL_LINK_ADDR)
relocated_addr += reloc_offset;
staticinline phys_addr_t __init alloc_pte_early(uintptr_t va)
{ /* * We only create PMD or PGD early mappings so we * should never reach here with MMU disabled.
*/
BUG();
}
/* * We do not know which mm the PTE page is associated to at this point. * Passing NULL to the ctor is the safe option, though it may result * in unnecessary work (e.g. initialising the ptlock for init_mm).
*/
BUG_ON(!ptdesc || !pagetable_pte_ctor(NULL, ptdesc)); return __pa((pte_t *)ptdesc_address(ptdesc));
}
static phys_addr_t __init alloc_pud_early(uintptr_t va)
{ /* Only one PUD is available for early mapping */
BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
static phys_addr_t __init alloc_p4d_early(uintptr_t va)
{ /* Only one P4D is available for early mapping */
BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
/* * In 64-bit kernel, the kernel mapping is outside the linear mapping so * we must protect its linear mapping alias from being executed and * written. * And rodata section is marked readonly in mark_rodata_ro.
*/ if (IS_ENABLED(CONFIG_64BIT) && is_va_kernel_lm_alias_text(va)) return PAGE_KERNEL_READ;
/* * There is a simple way to determine if 4-level is supported by the * underlying hardware: establish 1:1 mapping in 4-level page table mode * then read SATP to see if the configuration was taken into account * meaning sv48 is supported. * The maximum SATP mode is limited by both the command line and the "mmu-type" * property in the device tree, since some platforms may hang if an unsupported * SATP mode is attempted.
*/ static __init void set_satp_mode(uintptr_t dtb_pa)
{
u64 identity_satp, hw_satp;
uintptr_t set_satp_mode_pmd = ((unsignedlong)set_satp_mode) & PMD_MASK;
u64 satp_mode_limit = min_not_zero(__pi_set_satp_mode_from_cmdline(dtb_pa),
__pi_set_satp_mode_from_fdt(dtb_pa));
/* * setup_vm() is called from head.S with MMU-off. * * Following requirements should be honoured for setup_vm() to work * correctly: * 1) It should use PC-relative addressing for accessing kernel symbols. * To achieve this we always use GCC cmodel=medany. * 2) The compiler instrumentation for FTRACE will not work for setup_vm() * so disable compiler instrumentation when FTRACE is enabled. * * Currently, the above requirements are honoured by using custom CFLAGS * for init.o in mm/Makefile.
*/
#ifndef __riscv_cmodel_medany #error"setup_vm() is called from head.S before relocate so it should not use absolute addressing." #endif
/* Map the flash resident part */
end_va = kernel_map.virt_addr + kernel_map.xiprom_sz; for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE)
create_pgd_mapping(pgdir, va,
kernel_map.xiprom + (va - kernel_map.virt_addr),
PMD_SIZE, PAGE_KERNEL_EXEC);
/* Map the data in RAM */
start_va = kernel_map.virt_addr + (uintptr_t)&_sdata - (uintptr_t)&_start;
end_va = kernel_map.virt_addr + kernel_map.size; for (va = start_va; va < end_va; va += PMD_SIZE)
create_pgd_mapping(pgdir, va,
kernel_map.phys_addr + (va - start_va),
PMD_SIZE, PAGE_KERNEL);
} #else staticvoid __init create_kernel_page_table(pgd_t *pgdir, bool early)
{
uintptr_t va, end_va;
end_va = kernel_map.virt_addr + kernel_map.size; for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE)
create_pgd_mapping(pgdir, va,
kernel_map.phys_addr + (va - kernel_map.virt_addr),
PMD_SIZE,
early ?
PAGE_KERNEL_EXEC : pgprot_from_va(va));
} #endif
/* * Setup a 4MB mapping that encompasses the device tree: for 64-bit kernel, * this means 2 PMD entries whereas for 32-bit kernel, this is only 1 PGDIR * entry.
*/ staticvoid __init create_fdt_early_page_table(uintptr_t fix_fdt_va,
uintptr_t dtb_pa)
{ #ifndef CONFIG_BUILTIN_DTB
uintptr_t pa = dtb_pa & ~(PMD_SIZE - 1);
/* Make sure the fdt fixmap address is always aligned on PMD size */
BUILD_BUG_ON(FIX_FDT % (PMD_SIZE / PAGE_SIZE));
/* In 32-bit only, the fdt lies in its own PGD */ if (!IS_ENABLED(CONFIG_64BIT)) {
create_pgd_mapping(early_pg_dir, fix_fdt_va,
pa, MAX_FDT_SIZE, PAGE_KERNEL);
} else {
create_pmd_mapping(fixmap_pmd, fix_fdt_va,
pa, PMD_SIZE, PAGE_KERNEL);
create_pmd_mapping(fixmap_pmd, fix_fdt_va + PMD_SIZE,
pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL);
}
dtb_early_va = (void *)fix_fdt_va + (dtb_pa & (PMD_SIZE - 1)); #else /* * For 64-bit kernel, __va can't be used since it would return a linear * mapping address whereas dtb_early_va will be used before * setup_vm_final installs the linear mapping. For 32-bit kernel, as the * kernel is mapped in the linear mapping, that makes no difference.
*/
dtb_early_va = kernel_mapping_pa_to_va(dtb_pa); #endif
dtb_early_pa = dtb_pa;
}
/* * MMU is not enabled, the page tables are allocated directly using * early_pmd/pud/p4d and the address returned is the physical one.
*/ staticvoid __init pt_ops_set_early(void)
{
pt_ops.alloc_pte = alloc_pte_early;
pt_ops.get_pte_virt = get_pte_virt_early; #ifndef __PAGETABLE_PMD_FOLDED
pt_ops.alloc_pmd = alloc_pmd_early;
pt_ops.get_pmd_virt = get_pmd_virt_early;
pt_ops.alloc_pud = alloc_pud_early;
pt_ops.get_pud_virt = get_pud_virt_early;
pt_ops.alloc_p4d = alloc_p4d_early;
pt_ops.get_p4d_virt = get_p4d_virt_early; #endif
}
/* * MMU is enabled but page table setup is not complete yet. * fixmap page table alloc functions must be used as a means to temporarily * map the allocated physical pages since the linear mapping does not exist yet. * * Note that this is called with MMU disabled, hence kernel_mapping_pa_to_va, * but it will be used as described above.
*/ staticvoid __init pt_ops_set_fixmap(void)
{
pt_ops.alloc_pte = kernel_mapping_pa_to_va(alloc_pte_fixmap);
pt_ops.get_pte_virt = kernel_mapping_pa_to_va(get_pte_virt_fixmap); #ifndef __PAGETABLE_PMD_FOLDED
pt_ops.alloc_pmd = kernel_mapping_pa_to_va(alloc_pmd_fixmap);
pt_ops.get_pmd_virt = kernel_mapping_pa_to_va(get_pmd_virt_fixmap);
pt_ops.alloc_pud = kernel_mapping_pa_to_va(alloc_pud_fixmap);
pt_ops.get_pud_virt = kernel_mapping_pa_to_va(get_pud_virt_fixmap);
pt_ops.alloc_p4d = kernel_mapping_pa_to_va(alloc_p4d_fixmap);
pt_ops.get_p4d_virt = kernel_mapping_pa_to_va(get_p4d_virt_fixmap); #endif
}
/* * MMU is enabled and page table setup is complete, so from now, we can use * generic page allocation functions to setup page table.
*/ staticvoid __init pt_ops_set_late(void)
{
pt_ops.alloc_pte = alloc_pte_late;
pt_ops.get_pte_virt = get_pte_virt_late; #ifndef __PAGETABLE_PMD_FOLDED
pt_ops.alloc_pmd = alloc_pmd_late;
pt_ops.get_pmd_virt = get_pmd_virt_late;
pt_ops.alloc_pud = alloc_pud_late;
pt_ops.get_pud_virt = get_pud_virt_late;
pt_ops.alloc_p4d = alloc_p4d_late;
pt_ops.get_p4d_virt = get_p4d_virt_late; #endif
}
if (kaslr_seed == 0)
kaslr_seed = __pi_get_kaslr_seed(dtb_pa); /* * Compute the number of positions available: we are limited * by the early page table that only has one PUD and we must * be aligned on PMD_SIZE.
*/
nr_pos = (PUD_SIZE - kernel_size) / PMD_SIZE;
/* * In 64-bit, we defer the setup of va_pa_offset to setup_bootmem, * where we have the system memory layout: this allows us to align * the physical and virtual mappings and then make use of PUD/P4D/PGD * for the linear mapping. This is only possible because the kernel * mapping lies outside the linear mapping. * In 32-bit however, as the kernel resides in the linear mapping, * setup_vm_final can not change the mapping established here, * otherwise the same kernel addresses would get mapped to different * physical addresses (if the start of dram is different from the * kernel physical address start).
*/
kernel_map.va_pa_offset = IS_ENABLED(CONFIG_64BIT) ?
0UL : PAGE_OFFSET - kernel_map.phys_addr;
#ifdef CONFIG_64BIT /* * The last 4K bytes of the addressable memory can not be mapped because * of IS_ERR_VALUE macro.
*/
BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K); #endif
#ifdef CONFIG_RELOCATABLE /* * Early page table uses only one PUD, which makes it possible * to map PUD_SIZE aligned on PUD_SIZE: if the relocation offset * makes the kernel cross over a PUD_SIZE boundary, raise a bug * since a part of the kernel would not get mapped.
*/ if (IS_ENABLED(CONFIG_64BIT))
BUG_ON(PUD_SIZE - (kernel_map.virt_addr & (PUD_SIZE - 1)) < kernel_map.size);
relocate_kernel(); #endif
/* * Setup early PGD covering entire kernel which will allow * us to reach paging_init(). We map all memory banks later * in setup_vm_final() below.
*/
create_kernel_page_table(early_pg_dir, true);
/* Setup early mapping for FDT early scan */
create_fdt_early_page_table(__fix_to_virt(FIX_FDT), dtb_pa);
/* * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap * range can not span multiple pmds.
*/
BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
!= (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
#ifndef __PAGETABLE_PMD_FOLDED /* * Early ioremap fixmap is already created as it lies within first 2MB * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn * the user if not.
*/
fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];
fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))]; if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {
WARN_ON(1);
pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));
pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
fix_to_virt(FIX_BTMAP_BEGIN));
pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
fix_to_virt(FIX_BTMAP_END));
/* Isolate kernel text and rodata so they don't get mapped with a PUD */
memblock_mark_nomap(ktext_start, ktext_size);
memblock_mark_nomap(krodata_start, krodata_size); #endif
#ifdef CONFIG_KFENCE /* * kfence pool must be backed by PAGE_SIZE mappings, so allocate it * before we setup the linear mapping so that we avoid using hugepages * for this region.
*/
kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
BUG_ON(!kfence_pool);
/* Map all memory banks in the linear mapping */
for_each_mem_range(i, &start, &end) { if (start >= end) break; if (start <= __pa(PAGE_OFFSET) &&
__pa(PAGE_OFFSET) < end)
start = __pa(PAGE_OFFSET);
staticvoid __init setup_vm_final(void)
{ /* Setup swapper PGD for fixmap */ #if !defined(CONFIG_64BIT) /* * In 32-bit, the device tree lies in a pgd entry, so it must be copied * directly in swapper_pg_dir in addition to the pgd entry that points * to fixmap_pte.
*/ unsignedlong idx = pgd_index(__fix_to_virt(FIX_FDT));
/* * reserve_crashkernel() - reserves memory for crash kernel * * This function reserves memory area given in "crashkernel=" kernel command * line parameter. The memory reserved is used by dump capture kernel when * primary kernel is crashing.
*/ staticvoid __init arch_reserve_crashkernel(void)
{ unsignedlonglong low_size = 0; unsignedlonglong crash_base, crash_size; bool high = false; int ret;
if (!IS_ENABLED(CONFIG_CRASH_RESERVE)) return;
ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
&crash_size, &crash_base,
&low_size, NULL, &high); if (ret) return;
/* Depend on that Linear Mapping is ready */
memblock_allow_resize();
}
void __init misc_mem_init(void)
{
early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
arch_numa_init();
sparse_init(); #ifdef CONFIG_SPARSEMEM_VMEMMAP /* The entire VMEMMAP region has been populated. Flush TLB for this region */
local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END); #endif
zone_sizes_init();
arch_reserve_crashkernel();
memblock_dump_all();
}
int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node, unsignedlong addr, unsignedlong next)
{
vmemmap_verify((pte_t *)pmdp, node, addr, next); return 1;
}
int __meminit vmemmap_populate(unsignedlong start, unsignedlong end, int node, struct vmem_altmap *altmap)
{ /* * Note that SPARSEMEM_VMEMMAP is only selected for rv64 and that we * can't use hugepage mappings for 2-level page table because in case of * memory hotplug, we are not able to update all the page tables with * the new PMDs.
*/ return vmemmap_populate_hugepages(start, end, node, altmap);
} #endif
#ifdefined(CONFIG_MMU) && defined(CONFIG_64BIT) /* * Pre-allocates page-table pages for a specific area in the kernel * page-table. Only the level which needs to be synchronized between * all page-tables is allocated because the synchronization can be * expensive.
*/ staticvoid __init preallocate_pgd_pages_range(unsignedlong start, unsignedlong end, constchar *area)
{ unsignedlong addr; constchar *lvl;
failed: /* * The pages have to be there now or they will be missing in * process page-tables later.
*/
panic("Failed to pre-allocate %s pages for %s area\n", lvl, area);
}
#define PAGE_END KASAN_SHADOW_START
void __init pgtable_cache_init(void)
{
preallocate_pgd_pages_range(VMALLOC_START, VMALLOC_END, "vmalloc"); if (IS_ENABLED(CONFIG_MODULES))
preallocate_pgd_pages_range(MODULES_VADDR, MODULES_END, "bpf/modules"); if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) {
preallocate_pgd_pages_range(VMEMMAP_START, VMEMMAP_END, "vmemmap");
preallocate_pgd_pages_range(PAGE_OFFSET, PAGE_END, "direct map"); if (IS_ENABLED(CONFIG_KASAN))
preallocate_pgd_pages_range(KASAN_SHADOW_START, KASAN_SHADOW_END, "kasan");
}
} #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.