]>
Commit | Line | Data |
---|---|---|
aa8c6248 TG |
1 | /* |
2 | * Copyright(c) 2017 Intel Corporation. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of version 2 of the GNU General Public License as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * This code is based in part on work published here: | |
14 | * | |
15 | * https://github.com/IAIK/KAISER | |
16 | * | |
17 | * The original work was written by and and signed off by for the Linux | |
18 | * kernel by: | |
19 | * | |
20 | * Signed-off-by: Richard Fellner <richard.fellner@student.tugraz.at> | |
21 | * Signed-off-by: Moritz Lipp <moritz.lipp@iaik.tugraz.at> | |
22 | * Signed-off-by: Daniel Gruss <daniel.gruss@iaik.tugraz.at> | |
23 | * Signed-off-by: Michael Schwarz <michael.schwarz@iaik.tugraz.at> | |
24 | * | |
25 | * Major changes to the original code by: Dave Hansen <dave.hansen@intel.com> | |
26 | * Mostly rewritten by Thomas Gleixner <tglx@linutronix.de> and | |
27 | * Andy Lutomirsky <luto@amacapital.net> | |
28 | */ | |
29 | #include <linux/kernel.h> | |
30 | #include <linux/errno.h> | |
31 | #include <linux/string.h> | |
32 | #include <linux/types.h> | |
33 | #include <linux/bug.h> | |
34 | #include <linux/init.h> | |
35 | #include <linux/spinlock.h> | |
36 | #include <linux/mm.h> | |
37 | #include <linux/uaccess.h> | |
2a56e210 | 38 | #include <linux/cpu.h> |
aa8c6248 TG |
39 | |
40 | #include <asm/cpufeature.h> | |
41 | #include <asm/hypervisor.h> | |
85900ea5 | 42 | #include <asm/vsyscall.h> |
aa8c6248 TG |
43 | #include <asm/cmdline.h> |
44 | #include <asm/pti.h> | |
45 | #include <asm/pgtable.h> | |
46 | #include <asm/pgalloc.h> | |
47 | #include <asm/tlbflush.h> | |
48 | #include <asm/desc.h> | |
f901f138 | 49 | #include <asm/sections.h> |
aa8c6248 TG |
50 | |
51 | #undef pr_fmt | |
52 | #define pr_fmt(fmt) "Kernel/User page tables isolation: " fmt | |
53 | ||
03f4424f AL |
54 | /* Backporting helper */ |
55 | #ifndef __GFP_NOTRACK | |
56 | #define __GFP_NOTRACK 0 | |
57 | #endif | |
58 | ||
383eff9d JR |
59 | /* |
60 | * Define the page-table levels we clone for user-space on 32 | |
61 | * and 64 bit. | |
62 | */ | |
63 | #ifdef CONFIG_X86_64 | |
64 | #define PTI_LEVEL_KERNEL_IMAGE PTI_CLONE_PMD | |
65 | #else | |
66 | #define PTI_LEVEL_KERNEL_IMAGE PTI_CLONE_PTE | |
67 | #endif | |
68 | ||
aa8c6248 TG |
69 | static void __init pti_print_if_insecure(const char *reason) |
70 | { | |
de791821 | 71 | if (boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) |
aa8c6248 TG |
72 | pr_info("%s\n", reason); |
73 | } | |
74 | ||
41f4c20b BP |
75 | static void __init pti_print_if_secure(const char *reason) |
76 | { | |
de791821 | 77 | if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) |
41f4c20b BP |
78 | pr_info("%s\n", reason); |
79 | } | |
80 | ||
9c64e474 DH |
81 | enum pti_mode { |
82 | PTI_AUTO = 0, | |
83 | PTI_FORCE_OFF, | |
84 | PTI_FORCE_ON | |
85 | } pti_mode; | |
86 | ||
aa8c6248 TG |
87 | void __init pti_check_boottime_disable(void) |
88 | { | |
41f4c20b BP |
89 | char arg[5]; |
90 | int ret; | |
91 | ||
9c64e474 DH |
92 | /* Assume mode is auto unless overridden. */ |
93 | pti_mode = PTI_AUTO; | |
94 | ||
aa8c6248 | 95 | if (hypervisor_is_type(X86_HYPER_XEN_PV)) { |
9c64e474 | 96 | pti_mode = PTI_FORCE_OFF; |
aa8c6248 TG |
97 | pti_print_if_insecure("disabled on XEN PV."); |
98 | return; | |
99 | } | |
100 | ||
41f4c20b BP |
101 | ret = cmdline_find_option(boot_command_line, "pti", arg, sizeof(arg)); |
102 | if (ret > 0) { | |
103 | if (ret == 3 && !strncmp(arg, "off", 3)) { | |
9c64e474 | 104 | pti_mode = PTI_FORCE_OFF; |
41f4c20b BP |
105 | pti_print_if_insecure("disabled on command line."); |
106 | return; | |
107 | } | |
108 | if (ret == 2 && !strncmp(arg, "on", 2)) { | |
9c64e474 | 109 | pti_mode = PTI_FORCE_ON; |
41f4c20b BP |
110 | pti_print_if_secure("force enabled on command line."); |
111 | goto enable; | |
112 | } | |
9c64e474 DH |
113 | if (ret == 4 && !strncmp(arg, "auto", 4)) { |
114 | pti_mode = PTI_AUTO; | |
41f4c20b | 115 | goto autosel; |
9c64e474 | 116 | } |
41f4c20b BP |
117 | } |
118 | ||
2a56e210 JP |
119 | if (cmdline_find_option_bool(boot_command_line, "nopti") || |
120 | cpu_mitigations_off()) { | |
9c64e474 | 121 | pti_mode = PTI_FORCE_OFF; |
aa8c6248 TG |
122 | pti_print_if_insecure("disabled on command line."); |
123 | return; | |
124 | } | |
125 | ||
41f4c20b | 126 | autosel: |
de791821 | 127 | if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) |
aa8c6248 | 128 | return; |
41f4c20b | 129 | enable: |
aa8c6248 TG |
130 | setup_force_cpu_cap(X86_FEATURE_PTI); |
131 | } | |
132 | ||
5aa85693 | 133 | pgd_t __pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd) |
61e9b367 DH |
134 | { |
135 | /* | |
136 | * Changes to the high (kernel) portion of the kernelmode page | |
137 | * tables are not automatically propagated to the usermode tables. | |
138 | * | |
139 | * Users should keep in mind that, unlike the kernelmode tables, | |
140 | * there is no vmalloc_fault equivalent for the usermode tables. | |
141 | * Top-level entries added to init_mm's usermode pgd after boot | |
142 | * will not be automatically propagated to other mms. | |
143 | */ | |
144 | if (!pgdp_maps_userspace(pgdp)) | |
145 | return pgd; | |
146 | ||
147 | /* | |
148 | * The user page tables get the full PGD, accessible from | |
149 | * userspace: | |
150 | */ | |
151 | kernel_to_user_pgdp(pgdp)->pgd = pgd.pgd; | |
152 | ||
153 | /* | |
154 | * If this is normal user memory, make it NX in the kernel | |
155 | * pagetables so that, if we somehow screw up and return to | |
156 | * usermode with the kernel CR3 loaded, we'll get a page fault | |
157 | * instead of allowing user code to execute with the wrong CR3. | |
158 | * | |
159 | * As exceptions, we don't set NX if: | |
160 | * - _PAGE_USER is not set. This could be an executable | |
161 | * EFI runtime mapping or something similar, and the kernel | |
162 | * may execute from it | |
163 | * - we don't have NX support | |
164 | * - we're clearing the PGD (i.e. the new pgd is not present). | |
165 | */ | |
166 | if ((pgd.pgd & (_PAGE_USER|_PAGE_PRESENT)) == (_PAGE_USER|_PAGE_PRESENT) && | |
167 | (__supported_pte_mask & _PAGE_NX)) | |
168 | pgd.pgd |= _PAGE_NX; | |
169 | ||
170 | /* return the copy of the PGD we want the kernel to use: */ | |
171 | return pgd; | |
172 | } | |
173 | ||
03f4424f AL |
174 | /* |
175 | * Walk the user copy of the page tables (optionally) trying to allocate | |
176 | * page table pages on the way down. | |
177 | * | |
178 | * Returns a pointer to a P4D on success, or NULL on failure. | |
179 | */ | |
9c64e474 | 180 | static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address) |
03f4424f AL |
181 | { |
182 | pgd_t *pgd = kernel_to_user_pgdp(pgd_offset_k(address)); | |
183 | gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO); | |
184 | ||
185 | if (address < PAGE_OFFSET) { | |
186 | WARN_ONCE(1, "attempt to walk user address\n"); | |
187 | return NULL; | |
188 | } | |
189 | ||
190 | if (pgd_none(*pgd)) { | |
191 | unsigned long new_p4d_page = __get_free_page(gfp); | |
25e7c8ea | 192 | if (WARN_ON_ONCE(!new_p4d_page)) |
03f4424f AL |
193 | return NULL; |
194 | ||
8d56eff2 | 195 | set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page))); |
03f4424f AL |
196 | } |
197 | BUILD_BUG_ON(pgd_large(*pgd) != 0); | |
198 | ||
199 | return p4d_offset(pgd, address); | |
200 | } | |
201 | ||
202 | /* | |
203 | * Walk the user copy of the page tables (optionally) trying to allocate | |
204 | * page table pages on the way down. | |
205 | * | |
206 | * Returns a pointer to a PMD on success, or NULL on failure. | |
207 | */ | |
9c64e474 | 208 | static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address) |
03f4424f AL |
209 | { |
210 | gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO); | |
25e7c8ea | 211 | p4d_t *p4d; |
03f4424f AL |
212 | pud_t *pud; |
213 | ||
25e7c8ea JB |
214 | p4d = pti_user_pagetable_walk_p4d(address); |
215 | if (!p4d) | |
216 | return NULL; | |
217 | ||
03f4424f AL |
218 | BUILD_BUG_ON(p4d_large(*p4d) != 0); |
219 | if (p4d_none(*p4d)) { | |
220 | unsigned long new_pud_page = __get_free_page(gfp); | |
7df1a151 | 221 | if (WARN_ON_ONCE(!new_pud_page)) |
03f4424f AL |
222 | return NULL; |
223 | ||
8d56eff2 | 224 | set_p4d(p4d, __p4d(_KERNPG_TABLE | __pa(new_pud_page))); |
03f4424f AL |
225 | } |
226 | ||
227 | pud = pud_offset(p4d, address); | |
228 | /* The user page tables do not use large mappings: */ | |
229 | if (pud_large(*pud)) { | |
230 | WARN_ON(1); | |
231 | return NULL; | |
232 | } | |
233 | if (pud_none(*pud)) { | |
234 | unsigned long new_pmd_page = __get_free_page(gfp); | |
7df1a151 | 235 | if (WARN_ON_ONCE(!new_pmd_page)) |
03f4424f AL |
236 | return NULL; |
237 | ||
8d56eff2 | 238 | set_pud(pud, __pud(_KERNPG_TABLE | __pa(new_pmd_page))); |
03f4424f AL |
239 | } |
240 | ||
241 | return pmd_offset(pud, address); | |
242 | } | |
243 | ||
85900ea5 AL |
244 | /* |
245 | * Walk the shadow copy of the page tables (optionally) trying to allocate | |
246 | * page table pages on the way down. Does not support large pages. | |
247 | * | |
248 | * Note: this is only used when mapping *new* kernel data into the | |
249 | * user/shadow page tables. It is never used for userspace data. | |
250 | * | |
251 | * Returns a pointer to a PTE on success, or NULL on failure. | |
252 | */ | |
8f4a1b14 | 253 | static pte_t *pti_user_pagetable_walk_pte(unsigned long address) |
85900ea5 AL |
254 | { |
255 | gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO); | |
7df1a151 | 256 | pmd_t *pmd; |
85900ea5 AL |
257 | pte_t *pte; |
258 | ||
7df1a151 JB |
259 | pmd = pti_user_pagetable_walk_pmd(address); |
260 | if (!pmd) | |
261 | return NULL; | |
262 | ||
85900ea5 AL |
263 | /* We can't do anything sensible if we hit a large mapping. */ |
264 | if (pmd_large(*pmd)) { | |
265 | WARN_ON(1); | |
266 | return NULL; | |
267 | } | |
268 | ||
269 | if (pmd_none(*pmd)) { | |
270 | unsigned long new_pte_page = __get_free_page(gfp); | |
271 | if (!new_pte_page) | |
272 | return NULL; | |
273 | ||
8d56eff2 | 274 | set_pmd(pmd, __pmd(_KERNPG_TABLE | __pa(new_pte_page))); |
85900ea5 AL |
275 | } |
276 | ||
277 | pte = pte_offset_kernel(pmd, address); | |
278 | if (pte_flags(*pte) & _PAGE_USER) { | |
279 | WARN_ONCE(1, "attempt to walk to user pte\n"); | |
280 | return NULL; | |
281 | } | |
282 | return pte; | |
283 | } | |
284 | ||
383eff9d | 285 | #ifdef CONFIG_X86_VSYSCALL_EMULATION |
85900ea5 AL |
286 | static void __init pti_setup_vsyscall(void) |
287 | { | |
288 | pte_t *pte, *target_pte; | |
289 | unsigned int level; | |
290 | ||
291 | pte = lookup_address(VSYSCALL_ADDR, &level); | |
292 | if (!pte || WARN_ON(level != PG_LEVEL_4K) || pte_none(*pte)) | |
293 | return; | |
294 | ||
295 | target_pte = pti_user_pagetable_walk_pte(VSYSCALL_ADDR); | |
296 | if (WARN_ON(!target_pte)) | |
297 | return; | |
298 | ||
299 | *target_pte = *pte; | |
300 | set_vsyscall_pgtable_user_bits(kernel_to_user_pgdp(swapper_pg_dir)); | |
301 | } | |
302 | #else | |
303 | static void __init pti_setup_vsyscall(void) { } | |
304 | #endif | |
305 | ||
383eff9d JR |
306 | enum pti_clone_level { |
307 | PTI_CLONE_PMD, | |
308 | PTI_CLONE_PTE, | |
309 | }; | |
310 | ||
9c64e474 | 311 | static void |
383eff9d JR |
312 | pti_clone_pgtable(unsigned long start, unsigned long end, |
313 | enum pti_clone_level level) | |
03f4424f AL |
314 | { |
315 | unsigned long addr; | |
316 | ||
317 | /* | |
318 | * Clone the populated PMDs which cover start to end. These PMD areas | |
319 | * can have holes. | |
320 | */ | |
383eff9d JR |
321 | for (addr = start; addr < end;) { |
322 | pte_t *pte, *target_pte; | |
03f4424f AL |
323 | pmd_t *pmd, *target_pmd; |
324 | pgd_t *pgd; | |
325 | p4d_t *p4d; | |
326 | pud_t *pud; | |
327 | ||
4348c799 JR |
328 | /* Overflow check */ |
329 | if (addr < start) | |
330 | break; | |
331 | ||
03f4424f AL |
332 | pgd = pgd_offset_k(addr); |
333 | if (WARN_ON(pgd_none(*pgd))) | |
334 | return; | |
335 | p4d = p4d_offset(pgd, addr); | |
336 | if (WARN_ON(p4d_none(*p4d))) | |
337 | return; | |
383eff9d | 338 | |
03f4424f | 339 | pud = pud_offset(p4d, addr); |
383eff9d JR |
340 | if (pud_none(*pud)) { |
341 | addr += PUD_SIZE; | |
03f4424f | 342 | continue; |
383eff9d JR |
343 | } |
344 | ||
03f4424f | 345 | pmd = pmd_offset(pud, addr); |
383eff9d JR |
346 | if (pmd_none(*pmd)) { |
347 | addr += PMD_SIZE; | |
03f4424f | 348 | continue; |
383eff9d | 349 | } |
03f4424f | 350 | |
383eff9d JR |
351 | if (pmd_large(*pmd) || level == PTI_CLONE_PMD) { |
352 | target_pmd = pti_user_pagetable_walk_pmd(addr); | |
353 | if (WARN_ON(!target_pmd)) | |
354 | return; | |
355 | ||
356 | /* | |
357 | * Only clone present PMDs. This ensures only setting | |
358 | * _PAGE_GLOBAL on present PMDs. This should only be | |
359 | * called on well-known addresses anyway, so a non- | |
360 | * present PMD would be a surprise. | |
361 | */ | |
362 | if (WARN_ON(!(pmd_flags(*pmd) & _PAGE_PRESENT))) | |
363 | return; | |
364 | ||
365 | /* | |
366 | * Setting 'target_pmd' below creates a mapping in both | |
367 | * the user and kernel page tables. It is effectively | |
368 | * global, so set it as global in both copies. Note: | |
369 | * the X86_FEATURE_PGE check is not _required_ because | |
370 | * the CPU ignores _PAGE_GLOBAL when PGE is not | |
371 | * supported. The check keeps consistentency with | |
372 | * code that only set this bit when supported. | |
373 | */ | |
374 | if (boot_cpu_has(X86_FEATURE_PGE)) | |
375 | *pmd = pmd_set_flags(*pmd, _PAGE_GLOBAL); | |
376 | ||
377 | /* | |
378 | * Copy the PMD. That is, the kernelmode and usermode | |
379 | * tables will share the last-level page tables of this | |
380 | * address range | |
381 | */ | |
382 | *target_pmd = *pmd; | |
383 | ||
384 | addr += PMD_SIZE; | |
385 | ||
386 | } else if (level == PTI_CLONE_PTE) { | |
387 | ||
388 | /* Walk the page-table down to the pte level */ | |
389 | pte = pte_offset_kernel(pmd, addr); | |
390 | if (pte_none(*pte)) { | |
391 | addr += PAGE_SIZE; | |
392 | continue; | |
393 | } | |
394 | ||
395 | /* Only clone present PTEs */ | |
396 | if (WARN_ON(!(pte_flags(*pte) & _PAGE_PRESENT))) | |
397 | return; | |
398 | ||
399 | /* Allocate PTE in the user page-table */ | |
400 | target_pte = pti_user_pagetable_walk_pte(addr); | |
401 | if (WARN_ON(!target_pte)) | |
402 | return; | |
403 | ||
404 | /* Set GLOBAL bit in both PTEs */ | |
405 | if (boot_cpu_has(X86_FEATURE_PGE)) | |
406 | *pte = pte_set_flags(*pte, _PAGE_GLOBAL); | |
407 | ||
408 | /* Clone the PTE */ | |
409 | *target_pte = *pte; | |
410 | ||
411 | addr += PAGE_SIZE; | |
412 | ||
413 | } else { | |
414 | BUG(); | |
415 | } | |
03f4424f AL |
416 | } |
417 | } | |
418 | ||
032f4025 | 419 | #ifdef CONFIG_X86_64 |
f7cfbee9 AL |
420 | /* |
421 | * Clone a single p4d (i.e. a top-level entry on 4-level systems and a | |
422 | * next-level entry on 5-level systems. | |
423 | */ | |
424 | static void __init pti_clone_p4d(unsigned long addr) | |
425 | { | |
426 | p4d_t *kernel_p4d, *user_p4d; | |
427 | pgd_t *kernel_pgd; | |
428 | ||
429 | user_p4d = pti_user_pagetable_walk_p4d(addr); | |
25e7c8ea JB |
430 | if (!user_p4d) |
431 | return; | |
432 | ||
f7cfbee9 AL |
433 | kernel_pgd = pgd_offset_k(addr); |
434 | kernel_p4d = p4d_offset(kernel_pgd, addr); | |
435 | *user_p4d = *kernel_p4d; | |
436 | } | |
437 | ||
438 | /* | |
439 | * Clone the CPU_ENTRY_AREA into the user space visible page table. | |
440 | */ | |
441 | static void __init pti_clone_user_shared(void) | |
442 | { | |
443 | pti_clone_p4d(CPU_ENTRY_AREA_BASE); | |
444 | } | |
445 | ||
032f4025 JR |
446 | #else /* CONFIG_X86_64 */ |
447 | ||
448 | /* | |
449 | * On 32 bit PAE systems with 1GB of Kernel address space there is only | |
450 | * one pgd/p4d for the whole kernel. Cloning that would map the whole | |
451 | * address space into the user page-tables, making PTI useless. So clone | |
452 | * the page-table on the PMD level to prevent that. | |
453 | */ | |
454 | static void __init pti_clone_user_shared(void) | |
455 | { | |
456 | unsigned long start, end; | |
457 | ||
458 | start = CPU_ENTRY_AREA_BASE; | |
459 | end = start + (PAGE_SIZE * CPU_ENTRY_AREA_PAGES); | |
460 | ||
383eff9d | 461 | pti_clone_pgtable(start, end, PTI_CLONE_PMD); |
032f4025 JR |
462 | } |
463 | #endif /* CONFIG_X86_64 */ | |
464 | ||
4b6bbe95 AL |
465 | /* |
466 | * Clone the ESPFIX P4D into the user space visinble page table | |
467 | */ | |
468 | static void __init pti_setup_espfix64(void) | |
469 | { | |
470 | #ifdef CONFIG_X86_ESPFIX64 | |
471 | pti_clone_p4d(ESPFIX_BASE_ADDR); | |
472 | #endif | |
473 | } | |
474 | ||
6dc72c3c TG |
475 | /* |
476 | * Clone the populated PMDs of the entry and irqentry text and force it RO. | |
477 | */ | |
53db5ced | 478 | static void pti_clone_entry_text(void) |
6dc72c3c | 479 | { |
383eff9d JR |
480 | pti_clone_pgtable((unsigned long) __entry_text_start, |
481 | (unsigned long) __irqentry_text_end, | |
482 | PTI_CLONE_PMD); | |
6dc72c3c TG |
483 | } |
484 | ||
9c64e474 DH |
485 | /* |
486 | * Global pages and PCIDs are both ways to make kernel TLB entries | |
487 | * live longer, reduce TLB misses and improve kernel performance. | |
488 | * But, leaving all kernel text Global makes it potentially accessible | |
489 | * to Meltdown-style attacks which make it trivial to find gadgets or | |
490 | * defeat KASLR. | |
491 | * | |
492 | * Only use global pages when it is really worth it. | |
493 | */ | |
494 | static inline bool pti_kernel_image_global_ok(void) | |
495 | { | |
496 | /* | |
497 | * Systems with PCIDs get litlle benefit from global | |
498 | * kernel text and are not worth the downsides. | |
499 | */ | |
500 | if (cpu_feature_enabled(X86_FEATURE_PCID)) | |
501 | return false; | |
502 | ||
503 | /* | |
504 | * Only do global kernel image for pti=auto. Do the most | |
505 | * secure thing (not global) if pti=on specified. | |
506 | */ | |
507 | if (pti_mode != PTI_AUTO) | |
508 | return false; | |
509 | ||
510 | /* | |
511 | * K8 may not tolerate the cleared _PAGE_RW on the userspace | |
512 | * global kernel image pages. Do the safe thing (disable | |
513 | * global kernel image). This is unlikely to ever be | |
514 | * noticed because PTI is disabled by default on AMD CPUs. | |
515 | */ | |
516 | if (boot_cpu_has(X86_FEATURE_K8)) | |
517 | return false; | |
518 | ||
171b6b40 DH |
519 | /* |
520 | * RANDSTRUCT derives its hardening benefits from the | |
521 | * attacker's lack of knowledge about the layout of kernel | |
522 | * data structures. Keep the kernel image non-global in | |
523 | * cases where RANDSTRUCT is in use to help keep the layout a | |
524 | * secret. | |
525 | */ | |
526 | if (IS_ENABLED(CONFIG_GCC_PLUGIN_RANDSTRUCT)) | |
527 | return false; | |
528 | ||
9c64e474 DH |
529 | return true; |
530 | } | |
531 | ||
c0113907 DH |
532 | /* |
533 | * This is the only user for these and it is not arch-generic | |
534 | * like the other set_memory.h functions. Just extern them. | |
535 | */ | |
536 | extern int set_memory_nonglobal(unsigned long addr, int numpages); | |
537 | extern int set_memory_global(unsigned long addr, int numpages); | |
538 | ||
9c64e474 DH |
539 | /* |
540 | * For some configurations, map all of kernel text into the user page | |
541 | * tables. This reduces TLB misses, especially on non-PCID systems. | |
542 | */ | |
4cec8b0c | 543 | static void pti_clone_kernel_text(void) |
9c64e474 | 544 | { |
8dd9b8a5 DH |
545 | /* |
546 | * rodata is part of the kernel image and is normally | |
547 | * readable on the filesystem or on the web. But, do not | |
548 | * clone the areas past rodata, they might contain secrets. | |
549 | */ | |
9c64e474 | 550 | unsigned long start = PFN_ALIGN(_text); |
c0113907 DH |
551 | unsigned long end_clone = (unsigned long)__end_rodata_aligned; |
552 | unsigned long end_global = PFN_ALIGN((unsigned long)__stop___ex_table); | |
9c64e474 DH |
553 | |
554 | if (!pti_kernel_image_global_ok()) | |
555 | return; | |
556 | ||
8dd9b8a5 DH |
557 | pr_debug("mapping partial kernel image into user address space\n"); |
558 | ||
559 | /* | |
560 | * Note that this will undo _some_ of the work that | |
561 | * pti_set_kernel_image_nonglobal() did to clear the | |
562 | * global bit. | |
563 | */ | |
383eff9d | 564 | pti_clone_pgtable(start, end_clone, PTI_LEVEL_KERNEL_IMAGE); |
c0113907 DH |
565 | |
566 | /* | |
383eff9d | 567 | * pti_clone_pgtable() will set the global bit in any PMDs |
c0113907 DH |
568 | * that it clones, but we also need to get any PTEs in |
569 | * the last level for areas that are not huge-page-aligned. | |
570 | */ | |
571 | ||
572 | /* Set the global bit for normal non-__init kernel text: */ | |
573 | set_memory_global(start, (end_global - start) >> PAGE_SHIFT); | |
9c64e474 DH |
574 | } |
575 | ||
d473bd10 DH |
576 | void pti_set_kernel_image_nonglobal(void) |
577 | { | |
578 | /* | |
579 | * The identity map is created with PMDs, regardless of the | |
580 | * actual length of the kernel. We need to clear | |
581 | * _PAGE_GLOBAL up to a PMD boundary, not just to the end | |
582 | * of the image. | |
583 | */ | |
584 | unsigned long start = PFN_ALIGN(_text); | |
585 | unsigned long end = ALIGN((unsigned long)_end, PMD_PAGE_SIZE); | |
586 | ||
c0113907 DH |
587 | /* |
588 | * This clears _PAGE_GLOBAL from the entire kernel image. | |
589 | * pti_clone_kernel_text() map put _PAGE_GLOBAL back for | |
590 | * areas that are mapped to userspace. | |
591 | */ | |
d473bd10 DH |
592 | set_memory_nonglobal(start, (end - start) >> PAGE_SHIFT); |
593 | } | |
594 | ||
aa8c6248 TG |
595 | /* |
596 | * Initialize kernel page table isolation | |
597 | */ | |
598 | void __init pti_init(void) | |
599 | { | |
600 | if (!static_cpu_has(X86_FEATURE_PTI)) | |
601 | return; | |
602 | ||
603 | pr_info("enabled\n"); | |
f7cfbee9 | 604 | |
c397fbab JR |
605 | #ifdef CONFIG_X86_32 |
606 | /* | |
607 | * We check for X86_FEATURE_PCID here. But the init-code will | |
608 | * clear the feature flag on 32 bit because the feature is not | |
609 | * supported on 32 bit anyway. To print the warning we need to | |
610 | * check with cpuid directly again. | |
611 | */ | |
8b43b48b | 612 | if (cpuid_ecx(0x1) & BIT(17)) { |
c397fbab JR |
613 | /* Use printk to work around pr_fmt() */ |
614 | printk(KERN_WARNING "\n"); | |
615 | printk(KERN_WARNING "************************************************************\n"); | |
616 | printk(KERN_WARNING "** WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! **\n"); | |
617 | printk(KERN_WARNING "** **\n"); | |
618 | printk(KERN_WARNING "** You are using 32-bit PTI on a 64-bit PCID-capable CPU. **\n"); | |
619 | printk(KERN_WARNING "** Your performance will increase dramatically if you **\n"); | |
620 | printk(KERN_WARNING "** switch to a 64-bit kernel! **\n"); | |
621 | printk(KERN_WARNING "** **\n"); | |
622 | printk(KERN_WARNING "** WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! **\n"); | |
623 | printk(KERN_WARNING "************************************************************\n"); | |
624 | } | |
625 | #endif | |
626 | ||
f7cfbee9 | 627 | pti_clone_user_shared(); |
d473bd10 DH |
628 | |
629 | /* Undo all global bits from the init pagetables in head_64.S: */ | |
630 | pti_set_kernel_image_nonglobal(); | |
631 | /* Replace some of the global bits just for shared entry text: */ | |
6dc72c3c | 632 | pti_clone_entry_text(); |
4b6bbe95 | 633 | pti_setup_espfix64(); |
85900ea5 | 634 | pti_setup_vsyscall(); |
aa8c6248 | 635 | } |
4cec8b0c JR |
636 | |
637 | /* | |
53db5ced JR |
638 | * Finalize the kernel mappings in the userspace page-table. Some of the |
639 | * mappings for the kernel image might have changed since pti_init() | |
640 | * cloned them. This is because parts of the kernel image have been | |
641 | * mapped RO and/or NX. These changes need to be cloned again to the | |
642 | * userspace page-table. | |
4cec8b0c JR |
643 | */ |
644 | void pti_finalize(void) | |
645 | { | |
cbd7516a TG |
646 | if (!boot_cpu_has(X86_FEATURE_PTI)) |
647 | return; | |
4cec8b0c | 648 | /* |
53db5ced JR |
649 | * We need to clone everything (again) that maps parts of the |
650 | * kernel image. | |
4cec8b0c | 651 | */ |
53db5ced | 652 | pti_clone_entry_text(); |
4cec8b0c | 653 | pti_clone_kernel_text(); |
3a2cf6d0 JR |
654 | |
655 | debug_checkwx_user(); | |
4cec8b0c | 656 | } |