1 // SPDX-License-Identifier: GPL-2.0 1 2 /* 3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@ 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 th 31 * the host machine will allocate phys 32 */ 33 kasan_map_memory((void *)KASAN_SHADOW_ 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_ini 44 unsigned long *empty_zero_page = NULL; 45 EXPORT_SYMBOL(empty_zero_page); 46 47 /* 48 * Initialized during boot, and readonly for i 49 * afterwards 50 */ 51 pgd_t swapper_pg_dir[PTRS_PER_PGD]; 52 53 /* Initialized at boot time, and readonly afte 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 67 * to be turned on. 68 */ 69 brk_end = (unsigned long) UML_ROUND_UP 70 map_memory(brk_end, __pa(brk_end), uml 71 memblock_free((void *)brk_end, uml_res 72 uml_reserved = brk_end; 73 74 /* this will put all low memory onto t 75 memblock_free_all(); 76 max_pfn = max_low_pfn; 77 kmalloc_ok = 1; 78 } 79 80 /* 81 * Create a page table and place a pointer to 82 * directory entry. 83 */ 84 static void __init one_page_table_init(pmd_t * 85 { 86 if (pmd_none(*pmd)) { 87 pte_t *pte = (pte_t *) membloc 88 89 if (!pte) 90 panic("%s: Failed to a 91 __func__, PAGE_S 92 93 set_pmd(pmd, __pmd(_KERNPG_TAB 94 (un 95 BUG_ON(pte != pte_offset_kerne 96 } 97 } 98 99 static void __init one_md_table_init(pud_t *pu 100 { 101 #ifdef CONFIG_3_LEVEL_PGTABLES 102 pmd_t *pmd_table = (pmd_t *) memblock_ 103 if (!pmd_table) 104 panic("%s: Failed to allocate 105 __func__, PAGE_SIZE, PAG 106 107 set_pud(pud, __pud(_KERNPG_TABLE + (un 108 BUG_ON(pmd_table != pmd_offset(pud, 0) 109 #endif 110 } 111 112 static void __init fixrange_init(unsigned long 113 pgd_t *pgd_ba 114 { 115 pgd_t *pgd; 116 p4d_t *p4d; 117 pud_t *pud; 118 pmd_t *pmd; 119 int i, j; 120 unsigned long vaddr; 121 122 vaddr = start; 123 i = pgd_index(vaddr); 124 j = pmd_index(vaddr); 125 pgd = pgd_base + i; 126 127 for ( ; (i < PTRS_PER_PGD) && (vaddr < 128 p4d = p4d_offset(pgd, vaddr); 129 pud = pud_offset(p4d, vaddr); 130 if (pud_none(*pud)) 131 one_md_table_init(pud) 132 pmd = pmd_offset(pud, vaddr); 133 for (; (j < PTRS_PER_PMD) && ( 134 one_page_table_init(pm 135 vaddr += PMD_SIZE; 136 } 137 j = 0; 138 } 139 } 140 141 static void __init fixaddr_user_init( void) 142 { 143 #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA 144 long size = FIXADDR_USER_END - FIXADDR 145 pte_t *pte; 146 phys_t p; 147 unsigned long v, vaddr = FIXADDR_USER_ 148 149 if (!size) 150 return; 151 152 fixrange_init( FIXADDR_USER_START, FIX 153 v = (unsigned long) memblock_alloc_low 154 if (!v) 155 panic("%s: Failed to allocate 156 __func__, size, PAGE_SIZ 157 158 memcpy((void *) v , (void *) FIXADDR_U 159 p = __pa(v); 160 for ( ; size > 0; size -= PAGE_SIZE, v 161 p += PAGE_SIZE) { 162 pte = virt_to_kpte(vaddr); 163 pte_set_val(*pte, p, PAGE_READ 164 } 165 #endif 166 } 167 168 void __init paging_init(void) 169 { 170 unsigned long max_zone_pfn[MAX_NR_ZONE 171 unsigned long vaddr; 172 173 empty_zero_page = (unsigned long *) me 174 175 if (!empty_zero_page) 176 panic("%s: Failed to allocate 177 __func__, PAGE_SIZE, PAG 178 179 max_zone_pfn[ZONE_NORMAL] = end_iomem 180 free_area_init(max_zone_pfn); 181 182 /* 183 * Fixed mappings, only the page table 184 * created - mappings will be set by s 185 */ 186 vaddr = __fix_to_virt(__end_of_fixed_a 187 fixrange_init(vaddr, FIXADDR_TOP, swap 188 189 fixaddr_user_init(); 190 } 191 192 /* 193 * This can't do anything because nothing in t 194 * since it's not in kernel physical memory. 195 */ 196 197 void free_initmem(void) 198 { 199 } 200 201 /* Allocate and free page tables. */ 202 203 pgd_t *pgd_alloc(struct mm_struct *mm) 204 { 205 pgd_t *pgd = (pgd_t *)__get_free_page( 206 207 if (pgd) { 208 memset(pgd, 0, USER_PTRS_PER_P 209 memcpy(pgd + USER_PTRS_PER_PGD 210 swapper_pg_dir + USER_P 211 (PTRS_PER_PGD - USER_PT 212 } 213 return pgd; 214 } 215 216 void *uml_kmalloc(int size, int flags) 217 { 218 return kmalloc(size, flags); 219 } 220 221 static const pgprot_t protection_map[16] = { 222 [VM_NONE] 223 [VM_READ] 224 [VM_WRITE] 225 [VM_WRITE | VM_READ] 226 [VM_EXEC] 227 [VM_EXEC | VM_READ] 228 [VM_EXEC | VM_WRITE] 229 [VM_EXEC | VM_WRITE | VM_READ] 230 [VM_SHARED] 231 [VM_SHARED | VM_READ] 232 [VM_SHARED | VM_WRITE] 233 [VM_SHARED | VM_WRITE | VM_READ] 234 [VM_SHARED | VM_EXEC] 235 [VM_SHARED | VM_EXEC | VM_READ] 236 [VM_SHARED | VM_EXEC | VM_WRITE] 237 [VM_SHARED | VM_EXEC | VM_WRITE | VM_R 238 }; 239 DECLARE_VM_GET_PAGE_PROT 240
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.