^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/fixmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <as-layout.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <kern.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <kern_util.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <mem_user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* allocated in paging_init, zeroed in mem_init, and unchanged thereafter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned long *empty_zero_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) EXPORT_SYMBOL(empty_zero_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Initialized during boot, and readonly for initializing page tables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * afterwards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) pgd_t swapper_pg_dir[PTRS_PER_PGD];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* Initialized at boot time, and readonly after that */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned long long highmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) EXPORT_SYMBOL(highmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int kmalloc_ok = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Used during early boot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static unsigned long brk_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void __init mem_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* clear the zero-page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) memset(empty_zero_page, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* Map in the area just after the brk now that kmalloc is about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * to be turned on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) brk_end = (unsigned long) UML_ROUND_UP(sbrk(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) memblock_free(__pa(brk_end), uml_reserved - brk_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) uml_reserved = brk_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* this will put all low memory onto the freelists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) memblock_free_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) max_low_pfn = totalram_pages();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) max_pfn = max_low_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) mem_init_print_info(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) kmalloc_ok = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * Create a page table and place a pointer to it in a middle page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * directory entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static void __init one_page_table_init(pmd_t *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (pmd_none(*pmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pte_t *pte = (pte_t *) memblock_alloc_low(PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) panic("%s: Failed to allocate %lu bytes align=%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __func__, PAGE_SIZE, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) set_pmd(pmd, __pmd(_KERNPG_TABLE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) (unsigned long) __pa(pte)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (pte != pte_offset_kernel(pmd, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static void __init one_md_table_init(pud_t *pud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #ifdef CONFIG_3_LEVEL_PGTABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) pmd_t *pmd_table = (pmd_t *) memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!pmd_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) panic("%s: Failed to allocate %lu bytes align=%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) __func__, PAGE_SIZE, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (pmd_table != pmd_offset(pud, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static void __init fixrange_init(unsigned long start, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pgd_t *pgd_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) pgd_t *pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) unsigned long vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) vaddr = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) i = pgd_index(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) j = pmd_index(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pgd = pgd_base + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) p4d = p4d_offset(pgd, vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pud = pud_offset(p4d, vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (pud_none(*pud))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) one_md_table_init(pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pmd = pmd_offset(pud, vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) for (; (j < PTRS_PER_PMD) && (vaddr < end); pmd++, j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) one_page_table_init(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) vaddr += PMD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static void __init fixaddr_user_init( void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) long size = FIXADDR_USER_END - FIXADDR_USER_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) phys_t p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) unsigned long v, vaddr = FIXADDR_USER_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) v = (unsigned long) memblock_alloc_low(size, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) panic("%s: Failed to allocate %lu bytes align=%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) __func__, size, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) memcpy((void *) v , (void *) FIXADDR_USER_START, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) p = __pa(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) p += PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) pte = virt_to_kpte(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) pte_set_val(*pte, p, PAGE_READONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void __init paging_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned long vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) empty_zero_page = (unsigned long *) memblock_alloc_low(PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!empty_zero_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) panic("%s: Failed to allocate %lu bytes align=%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) __func__, PAGE_SIZE, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) max_zone_pfn[ZONE_NORMAL] = end_iomem >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) free_area_init(max_zone_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * Fixed mappings, only the page table structure has to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * created - mappings will be set by set_fixmap():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) fixaddr_user_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * This can't do anything because nothing in the kernel image can be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * since it's not in kernel physical memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) void free_initmem(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /* Allocate and free page tables. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) pgd_t *pgd_alloc(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (pgd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) memcpy(pgd + USER_PTRS_PER_PGD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) swapper_pg_dir + USER_PTRS_PER_PGD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) void *uml_kmalloc(int size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return kmalloc(size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }