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/swap.h> 10 #include <linux/slab.h> 11 #include <linux/init.h> 12 #include <asm/sections.h> 13 #include <asm/page.h> 14 #include <asm/pgalloc.h> 15 #include <as-layout.h> 16 #include <init.h> 17 #include <kern.h> 18 #include <kern_util.h> 19 #include <mem_user.h> 20 #include <os.h> 21 #include <um_malloc.h> 22 #include <linux/sched/task.h> 23 #include <linux/kasan.h> 24 25 #ifdef CONFIG_KASAN 26 void __init kasan_init(void) 27 { 28 /* 29 * kasan_map_memory will map all of the required address space and 30 * the host machine will allocate physical memory as necessary. 31 */ 32 kasan_map_memory((void *)KASAN_SHADOW_START, KASAN_SHADOW_SIZE); 33 init_task.kasan_depth = 0; 34 /* 35 * Since kasan_init() is called before main(), 36 * KASAN is initialized but the enablement is deferred after 37 * jump_label_init(). See arch_mm_preinit(). 38 */ 39 } 40 41 static void (*kasan_init_ptr)(void) 42 __section(".kasan_init") __used 43 = kasan_init; 44 #endif 45 46 /* 47 * Initialized during boot, and readonly for initializing page tables 48 * afterwards 49 */ 50 pgd_t swapper_pg_dir[PTRS_PER_PGD]; 51 52 /* Initialized at boot time, and readonly after that */ 53 int kmalloc_ok = 0; 54 55 /* Used during early boot */ 56 static unsigned long brk_end; 57 58 void __init arch_mm_preinit(void) 59 { 60 /* Safe to call after jump_label_init(). Enables KASAN. */ 61 kasan_init_generic(); 62 63 /* Map in the area just after the brk now that kmalloc is about 64 * to be turned on. 65 */ 66 brk_end = PAGE_ALIGN((unsigned long) sbrk(0)); 67 map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 68 !IS_ENABLED(CONFIG_MMU)); 69 memblock_free((void *)brk_end, uml_reserved - brk_end); 70 uml_reserved = brk_end; 71 min_low_pfn = PFN_UP(__pa(uml_reserved)); 72 max_pfn = max_low_pfn; 73 } 74 75 void __init mem_init(void) 76 { 77 kmalloc_ok = 1; 78 } 79 80 void __init arch_zone_limits_init(unsigned long *max_zone_pfns) 81 { 82 max_zone_pfns[ZONE_NORMAL] = high_physmem >> PAGE_SHIFT; 83 } 84 85 /* 86 * We could munmap() this instead, but then libc allocations could 87 * land in this area and stray initdata access could erroneously 88 * succeed - just mprotect() it to reliably catch bad accesses. 89 */ 90 void free_initmem(void) 91 { 92 unsigned long start = PAGE_ALIGN((unsigned long)__init_begin); 93 unsigned long end = round_down((unsigned long)__init_end, PAGE_SIZE); 94 95 if (end > start) 96 os_protect_memory((void *)start, end - start, 0, 0, 0); 97 } 98 99 void *uml_kmalloc(int size, int flags) 100 { 101 return kmalloc(size, flags); 102 } 103 104 void mark_rodata_ro(void) 105 { 106 unsigned long rodata_start = PFN_ALIGN(__start_rodata); 107 unsigned long rodata_end = PFN_ALIGN(__end_rodata); 108 109 os_protect_memory((void *)rodata_start, rodata_end - rodata_start, 1, 0, 0); 110 } 111