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/mm.h>
10 #include <linux/swap.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <asm/sections.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 #include <linux/kasan.h>
25
26 #ifdef CONFIG_KASAN
kasan_init(void)27 void __init 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 /*
36 * Since kasan_init() is called before main(),
37 * KASAN is initialized but the enablement is deferred after
38 * jump_label_init(). See arch_mm_preinit().
39 */
40 }
41
42 static void (*kasan_init_ptr)(void)
43 __section(".kasan_init") __used
44 = kasan_init;
45 #endif
46
47 /* allocated in paging_init, zeroed in mem_init, and unchanged thereafter */
48 unsigned long *empty_zero_page = NULL;
49 EXPORT_SYMBOL(empty_zero_page);
50
51 /*
52 * Initialized during boot, and readonly for initializing page tables
53 * afterwards
54 */
55 pgd_t swapper_pg_dir[PTRS_PER_PGD];
56
57 /* Initialized at boot time, and readonly after that */
58 int kmalloc_ok = 0;
59
60 /* Used during early boot */
61 static unsigned long brk_end;
62
arch_mm_preinit(void)63 void __init arch_mm_preinit(void)
64 {
65 /* Safe to call after jump_label_init(). Enables KASAN. */
66 kasan_init_generic();
67
68 /* clear the zero-page */
69 memset(empty_zero_page, 0, PAGE_SIZE);
70
71 /* Map in the area just after the brk now that kmalloc is about
72 * to be turned on.
73 */
74 brk_end = PAGE_ALIGN((unsigned long) sbrk(0));
75 map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
76 memblock_free((void *)brk_end, uml_reserved - brk_end);
77 uml_reserved = brk_end;
78 min_low_pfn = PFN_UP(__pa(uml_reserved));
79 max_pfn = max_low_pfn;
80 }
81
mem_init(void)82 void __init mem_init(void)
83 {
84 kmalloc_ok = 1;
85 }
86
paging_init(void)87 void __init paging_init(void)
88 {
89 unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
90
91 empty_zero_page = (unsigned long *) memblock_alloc_low(PAGE_SIZE,
92 PAGE_SIZE);
93 if (!empty_zero_page)
94 panic("%s: Failed to allocate %lu bytes align=%lx\n",
95 __func__, PAGE_SIZE, PAGE_SIZE);
96
97 max_zone_pfn[ZONE_NORMAL] = high_physmem >> PAGE_SHIFT;
98 free_area_init(max_zone_pfn);
99 }
100
101 /*
102 * This can't do anything because nothing in the kernel image can be freed
103 * since it's not in kernel physical memory.
104 */
105
free_initmem(void)106 void free_initmem(void)
107 {
108 }
109
110 /* Allocate and free page tables. */
111
pgd_alloc(struct mm_struct * mm)112 pgd_t *pgd_alloc(struct mm_struct *mm)
113 {
114 pgd_t *pgd = __pgd_alloc(mm, 0);
115
116 if (pgd)
117 memcpy(pgd + USER_PTRS_PER_PGD,
118 swapper_pg_dir + USER_PTRS_PER_PGD,
119 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
120
121 return pgd;
122 }
123
uml_kmalloc(int size,int flags)124 void *uml_kmalloc(int size, int flags)
125 {
126 return kmalloc(size, flags);
127 }
128
129 static const pgprot_t protection_map[16] = {
130 [VM_NONE] = PAGE_NONE,
131 [VM_READ] = PAGE_READONLY,
132 [VM_WRITE] = PAGE_COPY,
133 [VM_WRITE | VM_READ] = PAGE_COPY,
134 [VM_EXEC] = PAGE_READONLY,
135 [VM_EXEC | VM_READ] = PAGE_READONLY,
136 [VM_EXEC | VM_WRITE] = PAGE_COPY,
137 [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY,
138 [VM_SHARED] = PAGE_NONE,
139 [VM_SHARED | VM_READ] = PAGE_READONLY,
140 [VM_SHARED | VM_WRITE] = PAGE_SHARED,
141 [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
142 [VM_SHARED | VM_EXEC] = PAGE_READONLY,
143 [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY,
144 [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED,
145 [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED
146 };
147 DECLARE_VM_GET_PAGE_PROT
148
mark_rodata_ro(void)149 void mark_rodata_ro(void)
150 {
151 unsigned long rodata_start = PFN_ALIGN(__start_rodata);
152 unsigned long rodata_end = PFN_ALIGN(__end_rodata);
153
154 os_protect_memory((void *)rodata_start, rodata_end - rodata_start, 1, 0, 0);
155 }
156