pti.c 18 KB

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