This source file includes following definitions.
- get_padding
- kaslr_memory_enabled
- kernel_randomize_memory
- init_trampoline_pud
- init_trampoline
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/random.h>
26 #include <linux/memblock.h>
27
28 #include <asm/pgalloc.h>
29 #include <asm/pgtable.h>
30 #include <asm/setup.h>
31 #include <asm/kaslr.h>
32
33 #include "mm_internal.h"
34
35 #define TB_SHIFT 40
36
37
38
39
40
41
42 static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;
43
44
45
46
47
48
49 static __initdata struct kaslr_memory_region {
50 unsigned long *base;
51 unsigned long size_tb;
52 } kaslr_regions[] = {
53 { &page_offset_base, 0 },
54 { &vmalloc_base, 0 },
55 { &vmemmap_base, 0 },
56 };
57
58
59 static inline unsigned long get_padding(struct kaslr_memory_region *region)
60 {
61 return (region->size_tb << TB_SHIFT);
62 }
63
64
65
66
67
68 static inline bool kaslr_memory_enabled(void)
69 {
70 return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN);
71 }
72
73
74 void __init kernel_randomize_memory(void)
75 {
76 size_t i;
77 unsigned long vaddr_start, vaddr;
78 unsigned long rand, memory_tb;
79 struct rnd_state rand_state;
80 unsigned long remain_entropy;
81 unsigned long vmemmap_size;
82
83 vaddr_start = pgtable_l5_enabled() ? __PAGE_OFFSET_BASE_L5 : __PAGE_OFFSET_BASE_L4;
84 vaddr = vaddr_start;
85
86
87
88
89
90
91 BUILD_BUG_ON(vaddr_start >= vaddr_end);
92 BUILD_BUG_ON(vaddr_end != CPU_ENTRY_AREA_BASE);
93 BUILD_BUG_ON(vaddr_end > __START_KERNEL_map);
94
95 if (!kaslr_memory_enabled())
96 return;
97
98 kaslr_regions[0].size_tb = 1 << (MAX_PHYSMEM_BITS - TB_SHIFT);
99 kaslr_regions[1].size_tb = VMALLOC_SIZE_TB;
100
101
102
103
104
105 BUG_ON(kaslr_regions[0].base != &page_offset_base);
106 memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
107 CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
108
109
110 if (memory_tb < kaslr_regions[0].size_tb)
111 kaslr_regions[0].size_tb = memory_tb;
112
113
114
115
116
117 vmemmap_size = (kaslr_regions[0].size_tb << (TB_SHIFT - PAGE_SHIFT)) *
118 sizeof(struct page);
119 kaslr_regions[2].size_tb = DIV_ROUND_UP(vmemmap_size, 1UL << TB_SHIFT);
120
121
122 remain_entropy = vaddr_end - vaddr_start;
123 for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++)
124 remain_entropy -= get_padding(&kaslr_regions[i]);
125
126 prandom_seed_state(&rand_state, kaslr_get_random_long("Memory"));
127
128 for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++) {
129 unsigned long entropy;
130
131
132
133
134
135 entropy = remain_entropy / (ARRAY_SIZE(kaslr_regions) - i);
136 prandom_bytes_state(&rand_state, &rand, sizeof(rand));
137 entropy = (rand % (entropy + 1)) & PUD_MASK;
138 vaddr += entropy;
139 *kaslr_regions[i].base = vaddr;
140
141
142
143
144
145 vaddr += get_padding(&kaslr_regions[i]);
146 vaddr = round_up(vaddr + 1, PUD_SIZE);
147 remain_entropy -= entropy;
148 }
149 }
150
151 static void __meminit init_trampoline_pud(void)
152 {
153 pud_t *pud_page_tramp, *pud, *pud_tramp;
154 p4d_t *p4d_page_tramp, *p4d, *p4d_tramp;
155 unsigned long paddr, vaddr;
156 pgd_t *pgd;
157
158 pud_page_tramp = alloc_low_page();
159
160
161
162
163
164
165
166
167 paddr = 0;
168 vaddr = (unsigned long)__va(paddr);
169 pgd = pgd_offset_k(vaddr);
170
171 p4d = p4d_offset(pgd, vaddr);
172 pud = pud_offset(p4d, vaddr);
173
174 pud_tramp = pud_page_tramp + pud_index(paddr);
175 *pud_tramp = *pud;
176
177 if (pgtable_l5_enabled()) {
178 p4d_page_tramp = alloc_low_page();
179
180 p4d_tramp = p4d_page_tramp + p4d_index(paddr);
181
182 set_p4d(p4d_tramp,
183 __p4d(_KERNPG_TABLE | __pa(pud_page_tramp)));
184
185 set_pgd(&trampoline_pgd_entry,
186 __pgd(_KERNPG_TABLE | __pa(p4d_page_tramp)));
187 } else {
188 set_pgd(&trampoline_pgd_entry,
189 __pgd(_KERNPG_TABLE | __pa(pud_page_tramp)));
190 }
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204
205 void __meminit init_trampoline(void)
206 {
207 if (!kaslr_memory_enabled()) {
208 init_trampoline_default();
209 return;
210 }
211
212 init_trampoline_pud();
213 }