Skip to content

Commit f10ee3d

Browse files
committed
x86/pti: Fix !PCID and sanitize defines
The switch to the user space page tables in the low level ASM code sets unconditionally bit 12 and bit 11 of CR3. Bit 12 is switching the base address of the page directory to the user part, bit 11 is switching the PCID to the PCID associated with the user page tables. This fails on a machine which lacks PCID support because bit 11 is set in CR3. Bit 11 is reserved when PCID is inactive. While the Intel SDM claims that the reserved bits are ignored when PCID is disabled, the AMD APM states that they should be cleared. This went unnoticed as the AMD APM was not checked when the code was developed and reviewed and test systems with Intel CPUs never failed to boot. The report is against a Centos 6 host where the guest fails to boot, so it's not yet clear whether this is a virt issue or can happen on real hardware too, but thats irrelevant as the AMD APM clearly ask for clearing the reserved bits. Make sure that on non PCID machines bit 11 is not set by the page table switching code. Andy suggested to rename the related bits and masks so they are clearly describing what they should be used for, which is done as well for clarity. That split could have been done with alternatives but the macro hell is horrible and ugly. This can be done on top if someone cares to remove the extra orq. For now it's a straight forward fix. Fixes: 6fd166a ("x86/mm: Use/Fix PCID to optimize user/kernel switches") Reported-by: Laura Abbott <labbott@redhat.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: stable <stable@vger.kernel.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Andy Lutomirski <luto@kernel.org> Cc: Willy Tarreau <w@1wt.eu> Cc: David Woodhouse <dwmw@amazon.co.uk> Link: https://lkml.kernel.org/r/alpine.DEB.2.20.1801140009150.2371@nanos
1 parent 352909b commit f10ee3d

3 files changed

Lines changed: 23 additions & 21 deletions

File tree

‎arch/x86/entry/calling.h‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,11 @@ For 32-bit we have the following conventions - kernel is built with
198198
* PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two
199199
* halves:
200200
*/
201-
#define PTI_SWITCH_PGTABLES_MASK (1<<PAGE_SHIFT)
202-
#define PTI_SWITCH_MASK (PTI_SWITCH_PGTABLES_MASK|(1<<X86_CR3_PTI_SWITCH_BIT))
201+
#define PTI_USER_PGTABLE_BIT PAGE_SHIFT
202+
#define PTI_USER_PGTABLE_MASK (1 << PTI_USER_PGTABLE_BIT)
203+
#define PTI_USER_PCID_BIT X86_CR3_PTI_PCID_USER_BIT
204+
#define PTI_USER_PCID_MASK (1 << PTI_USER_PCID_BIT)
205+
#define PTI_USER_PGTABLE_AND_PCID_MASK (PTI_USER_PCID_MASK | PTI_USER_PGTABLE_MASK)
203206

204207
.macro SET_NOFLUSH_BIT reg:req
205208
bts $X86_CR3_PCID_NOFLUSH_BIT, \reg
@@ -208,7 +211,7 @@ For 32-bit we have the following conventions - kernel is built with
208211
.macro ADJUST_KERNEL_CR3 reg:req
209212
ALTERNATIVE "", "SET_NOFLUSH_BIT \reg", X86_FEATURE_PCID
210213
/* Clear PCID and "PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */
211-
andq $(~PTI_SWITCH_MASK), \reg
214+
andq $(~PTI_USER_PGTABLE_AND_PCID_MASK), \reg
212215
.endm
213216

214217
.macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
@@ -239,15 +242,19 @@ For 32-bit we have the following conventions - kernel is built with
239242
/* Flush needed, clear the bit */
240243
btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
241244
movq \scratch_reg2, \scratch_reg
242-
jmp .Lwrcr3_\@
245+
jmp .Lwrcr3_pcid_\@
243246

244247
.Lnoflush_\@:
245248
movq \scratch_reg2, \scratch_reg
246249
SET_NOFLUSH_BIT \scratch_reg
247250

251+
.Lwrcr3_pcid_\@:
252+
/* Flip the ASID to the user version */
253+
orq $(PTI_USER_PCID_MASK), \scratch_reg
254+
248255
.Lwrcr3_\@:
249-
/* Flip the PGD and ASID to the user version */
250-
orq $(PTI_SWITCH_MASK), \scratch_reg
256+
/* Flip the PGD to the user version */
257+
orq $(PTI_USER_PGTABLE_MASK), \scratch_reg
251258
mov \scratch_reg, %cr3
252259
.Lend_\@:
253260
.endm
@@ -263,17 +270,12 @@ For 32-bit we have the following conventions - kernel is built with
263270
movq %cr3, \scratch_reg
264271
movq \scratch_reg, \save_reg
265272
/*
266-
* Is the "switch mask" all zero? That means that both of
267-
* these are zero:
268-
*
269-
* 1. The user/kernel PCID bit, and
270-
* 2. The user/kernel "bit" that points CR3 to the
271-
* bottom half of the 8k PGD
272-
*
273-
* That indicates a kernel CR3 value, not a user CR3.
273+
* Test the user pagetable bit. If set, then the user page tables
274+
* are active. If clear CR3 already has the kernel page table
275+
* active.
274276
*/
275-
testq $(PTI_SWITCH_MASK), \scratch_reg
276-
jz .Ldone_\@
277+
bt $PTI_USER_PGTABLE_BIT, \scratch_reg
278+
jnc .Ldone_\@
277279

278280
ADJUST_KERNEL_CR3 \scratch_reg
279281
movq \scratch_reg, %cr3
@@ -290,7 +292,7 @@ For 32-bit we have the following conventions - kernel is built with
290292
* KERNEL pages can always resume with NOFLUSH as we do
291293
* explicit flushes.
292294
*/
293-
bt $X86_CR3_PTI_SWITCH_BIT, \save_reg
295+
bt $PTI_USER_PGTABLE_BIT, \save_reg
294296
jnc .Lnoflush_\@
295297

296298
/*

‎arch/x86/include/asm/processor-flags.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#define CR3_NOFLUSH BIT_ULL(63)
4141

4242
#ifdef CONFIG_PAGE_TABLE_ISOLATION
43-
# define X86_CR3_PTI_SWITCH_BIT 11
43+
# define X86_CR3_PTI_PCID_USER_BIT 11
4444
#endif
4545

4646
#else

‎arch/x86/include/asm/tlbflush.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ static inline u16 kern_pcid(u16 asid)
8181
* Make sure that the dynamic ASID space does not confict with the
8282
* bit we are using to switch between user and kernel ASIDs.
8383
*/
84-
BUILD_BUG_ON(TLB_NR_DYN_ASIDS >= (1 << X86_CR3_PTI_SWITCH_BIT));
84+
BUILD_BUG_ON(TLB_NR_DYN_ASIDS >= (1 << X86_CR3_PTI_PCID_USER_BIT));
8585

8686
/*
8787
* The ASID being passed in here should have respected the
8888
* MAX_ASID_AVAILABLE and thus never have the switch bit set.
8989
*/
90-
VM_WARN_ON_ONCE(asid & (1 << X86_CR3_PTI_SWITCH_BIT));
90+
VM_WARN_ON_ONCE(asid & (1 << X86_CR3_PTI_PCID_USER_BIT));
9191
#endif
9292
/*
9393
* The dynamically-assigned ASIDs that get passed in are small
@@ -112,7 +112,7 @@ static inline u16 user_pcid(u16 asid)
112112
{
113113
u16 ret = kern_pcid(asid);
114114
#ifdef CONFIG_PAGE_TABLE_ISOLATION
115-
ret |= 1 << X86_CR3_PTI_SWITCH_BIT;
115+
ret |= 1 << X86_CR3_PTI_PCID_USER_BIT;
116116
#endif
117117
return ret;
118118
}

0 commit comments

Comments
 (0)