xref: /linux/arch/um/kernel/mem.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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_pfn = max_low_pfn;
77 	kmalloc_ok = 1;
78 }
79 
80 /*
81  * Create a page table and place a pointer to it in a middle page
82  * directory entry.
83  */
84 static void __init one_page_table_init(pmd_t *pmd)
85 {
86 	if (pmd_none(*pmd)) {
87 		pte_t *pte = (pte_t *) memblock_alloc_low(PAGE_SIZE,
88 							  PAGE_SIZE);
89 		if (!pte)
90 			panic("%s: Failed to allocate %lu bytes align=%lx\n",
91 			      __func__, PAGE_SIZE, PAGE_SIZE);
92 
93 		set_pmd(pmd, __pmd(_KERNPG_TABLE +
94 					   (unsigned long) __pa(pte)));
95 		BUG_ON(pte != pte_offset_kernel(pmd, 0));
96 	}
97 }
98 
99 static void __init one_md_table_init(pud_t *pud)
100 {
101 #ifdef CONFIG_3_LEVEL_PGTABLES
102 	pmd_t *pmd_table = (pmd_t *) memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
103 	if (!pmd_table)
104 		panic("%s: Failed to allocate %lu bytes align=%lx\n",
105 		      __func__, PAGE_SIZE, PAGE_SIZE);
106 
107 	set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table)));
108 	BUG_ON(pmd_table != pmd_offset(pud, 0));
109 #endif
110 }
111 
112 static void __init fixrange_init(unsigned long start, unsigned long end,
113 				 pgd_t *pgd_base)
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 < end); pgd++, i++) {
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) && (vaddr < end); pmd++, j++) {
134 			one_page_table_init(pmd);
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_USER_START;
145 	pte_t *pte;
146 	phys_t p;
147 	unsigned long v, vaddr = FIXADDR_USER_START;
148 
149 	if (!size)
150 		return;
151 
152 	fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
153 	v = (unsigned long) memblock_alloc_low(size, PAGE_SIZE);
154 	if (!v)
155 		panic("%s: Failed to allocate %lu bytes align=%lx\n",
156 		      __func__, size, PAGE_SIZE);
157 
158 	memcpy((void *) v , (void *) FIXADDR_USER_START, size);
159 	p = __pa(v);
160 	for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE,
161 		      p += PAGE_SIZE) {
162 		pte = virt_to_kpte(vaddr);
163 		pte_set_val(*pte, p, PAGE_READONLY);
164 	}
165 #endif
166 }
167 
168 void __init paging_init(void)
169 {
170 	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
171 	unsigned long vaddr;
172 
173 	empty_zero_page = (unsigned long *) memblock_alloc_low(PAGE_SIZE,
174 							       PAGE_SIZE);
175 	if (!empty_zero_page)
176 		panic("%s: Failed to allocate %lu bytes align=%lx\n",
177 		      __func__, PAGE_SIZE, PAGE_SIZE);
178 
179 	max_zone_pfn[ZONE_NORMAL] = end_iomem >> PAGE_SHIFT;
180 	free_area_init(max_zone_pfn);
181 
182 	/*
183 	 * Fixed mappings, only the page table structure has to be
184 	 * created - mappings will be set by set_fixmap():
185 	 */
186 	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
187 	fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
188 
189 	fixaddr_user_init();
190 }
191 
192 /*
193  * This can't do anything because nothing in the kernel image can be freed
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(GFP_KERNEL);
206 
207 	if (pgd) {
208 		memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
209 		memcpy(pgd + USER_PTRS_PER_PGD,
210 		       swapper_pg_dir + USER_PTRS_PER_PGD,
211 		       (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
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]					= PAGE_NONE,
223 	[VM_READ]					= PAGE_READONLY,
224 	[VM_WRITE]					= PAGE_COPY,
225 	[VM_WRITE | VM_READ]				= PAGE_COPY,
226 	[VM_EXEC]					= PAGE_READONLY,
227 	[VM_EXEC | VM_READ]				= PAGE_READONLY,
228 	[VM_EXEC | VM_WRITE]				= PAGE_COPY,
229 	[VM_EXEC | VM_WRITE | VM_READ]			= PAGE_COPY,
230 	[VM_SHARED]					= PAGE_NONE,
231 	[VM_SHARED | VM_READ]				= PAGE_READONLY,
232 	[VM_SHARED | VM_WRITE]				= PAGE_SHARED,
233 	[VM_SHARED | VM_WRITE | VM_READ]		= PAGE_SHARED,
234 	[VM_SHARED | VM_EXEC]				= PAGE_READONLY,
235 	[VM_SHARED | VM_EXEC | VM_READ]			= PAGE_READONLY,
236 	[VM_SHARED | VM_EXEC | VM_WRITE]		= PAGE_SHARED,
237 	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= PAGE_SHARED
238 };
239 DECLARE_VM_GET_PAGE_PROT
240