xref: /linux/arch/um/kernel/mem.c (revision e2683c8868d03382da7e1ce8453b543a043066d1)
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
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 /*
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 int kmalloc_ok = 0;
55 
56 /* Used during early boot */
57 static unsigned long brk_end;
58 
59 void __init arch_mm_preinit(void)
60 {
61 	/* Safe to call after jump_label_init(). Enables KASAN. */
62 	kasan_init_generic();
63 
64 	/* Map in the area just after the brk now that kmalloc is about
65 	 * to be turned on.
66 	 */
67 	brk_end = PAGE_ALIGN((unsigned long) sbrk(0));
68 	map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
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  * This can't do anything because nothing in the kernel image can be freed
87  * since it's not in kernel physical memory.
88  */
89 
90 void free_initmem(void)
91 {
92 }
93 
94 /* Allocate and free page tables. */
95 
96 pgd_t *pgd_alloc(struct mm_struct *mm)
97 {
98 	pgd_t *pgd = __pgd_alloc(mm, 0);
99 
100 	if (pgd)
101 		memcpy(pgd + USER_PTRS_PER_PGD,
102 		       swapper_pg_dir + USER_PTRS_PER_PGD,
103 		       (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
104 
105 	return pgd;
106 }
107 
108 void *uml_kmalloc(int size, int flags)
109 {
110 	return kmalloc(size, flags);
111 }
112 
113 static const pgprot_t protection_map[16] = {
114 	[VM_NONE]					= PAGE_NONE,
115 	[VM_READ]					= PAGE_READONLY,
116 	[VM_WRITE]					= PAGE_COPY,
117 	[VM_WRITE | VM_READ]				= PAGE_COPY,
118 	[VM_EXEC]					= PAGE_READONLY,
119 	[VM_EXEC | VM_READ]				= PAGE_READONLY,
120 	[VM_EXEC | VM_WRITE]				= PAGE_COPY,
121 	[VM_EXEC | VM_WRITE | VM_READ]			= PAGE_COPY,
122 	[VM_SHARED]					= PAGE_NONE,
123 	[VM_SHARED | VM_READ]				= PAGE_READONLY,
124 	[VM_SHARED | VM_WRITE]				= PAGE_SHARED,
125 	[VM_SHARED | VM_WRITE | VM_READ]		= PAGE_SHARED,
126 	[VM_SHARED | VM_EXEC]				= PAGE_READONLY,
127 	[VM_SHARED | VM_EXEC | VM_READ]			= PAGE_READONLY,
128 	[VM_SHARED | VM_EXEC | VM_WRITE]		= PAGE_SHARED,
129 	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= PAGE_SHARED
130 };
131 DECLARE_VM_GET_PAGE_PROT
132 
133 void mark_rodata_ro(void)
134 {
135 	unsigned long rodata_start = PFN_ALIGN(__start_rodata);
136 	unsigned long rodata_end = PFN_ALIGN(__end_rodata);
137 
138 	os_protect_memory((void *)rodata_start, rodata_end - rodata_start, 1, 0, 0);
139 }
140