pti.c 17 KB

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