pti.c 17 KB

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