[patch 35/60] x86/espfix: Ensure that ESPFIX is visible in user PGD

From: Thomas Gleixner
Date: Mon Dec 04 2017 - 11:52:00 EST


From: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>

Clone the ESPFIX alias mapping area so the entry/exit code has access to it
even with the user space page tables.

[ tglx: Remove the per cpu user mapped oddity ]

Signed-off-by: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>

---
arch/x86/kernel/espfix_64.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)

--- a/arch/x86/kernel/espfix_64.c
+++ b/arch/x86/kernel/espfix_64.c
@@ -129,6 +129,22 @@ void __init init_espfix_bsp(void)
p4d = p4d_alloc(&init_mm, pgd, ESPFIX_BASE_ADDR);
p4d_populate(&init_mm, p4d, espfix_pud_page);

+ /*
+ * Just copy the top-level PGD that is mapping the espfix area to
+ * ensure it is mapped into the user page tables.
+ *
+ * For 5-level paging, the espfix pgd was populated when
+ * kpti_init() pre-populated all the pgd entries. The above
+ * p4d_alloc() would never do anything and the p4d_populate() would
+ * be done to a p4d already mapped in the userspace pgd.
+ */
+#ifdef CONFIG_KERNEL_PAGE_TABLE_ISOLATION
+ if (CONFIG_PGTABLE_LEVELS <= 4) {
+ set_pgd(kernel_to_user_pgdp(pgd),
+ __pgd(_KERNPG_TABLE | (p4d_pfn(*p4d) << PAGE_SHIFT)));
+ }
+#endif
+
/* Randomize the locations */
init_espfix_random();