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