Skip to content

Commit f3d2b76

Browse files
hansendcgregkh
authored andcommitted
x86/mm/pti: Prepare the x86/entry assembly code for entry/exit CR3 switching
commit 8a09317 upstream. PAGE_TABLE_ISOLATION needs to switch to a different CR3 value when it enters the kernel and switch back when it exits. This essentially needs to be done before leaving assembly code. This is extra challenging because the switching context is tricky: the registers that can be clobbered can vary. It is also hard to store things on the stack because there is an established ABI (ptregs) or the stack is entirely unsafe to use. Establish a set of macros that allow changing to the user and kernel CR3 values. Interactions with SWAPGS: Previous versions of the PAGE_TABLE_ISOLATION code relied on having per-CPU scratch space to save/restore a register that can be used for the CR3 MOV. The %GS register is used to index into our per-CPU space, so SWAPGS *had* to be done before the CR3 switch. That scratch space is gone now, but the semantic that SWAPGS must be done before the CR3 MOV is retained. This is good to keep because it is not that hard to do and it allows to do things like add per-CPU debugging information. What this does in the NMI code is worth pointing out. NMIs can interrupt *any* context and they can also be nested with NMIs interrupting other NMIs. The comments below ".Lnmi_from_kernel" explain the format of the stack during this situation. Changing the format of this stack is hard. Instead of storing the old CR3 value on the stack, this depends on the *regular* register save/restore mechanism and then uses %r14 to keep CR3 during the NMI. It is callee-saved and will not be clobbered by the C NMI handlers that get called. [ PeterZ: ESPFIX optimization ] Based-on-code-from: Andy Lutomirski <luto@kernel.org> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Borislav Petkov <bp@suse.de> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Cc: Andy Lutomirski <luto@kernel.org> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: David Laight <David.Laight@aculab.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Eduardo Valentin <eduval@amazon.com> Cc: Greg KH <gregkh@linuxfoundation.org> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Juergen Gross <jgross@suse.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Will Deacon <will.deacon@arm.com> Cc: aliguori@amazon.com Cc: daniel.gruss@iaik.tugraz.at Cc: hughd@google.com Cc: keescook@google.com Cc: linux-mm@kvack.org Signed-off-by: Ingo Molnar <mingo@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent acfee9b commit f3d2b76

3 files changed

Lines changed: 128 additions & 7 deletions

File tree

‎arch/x86/entry/calling.h‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#include <linux/jump_label.h>
33
#include <asm/unwind_hints.h>
4+
#include <asm/cpufeatures.h>
5+
#include <asm/page_types.h>
46

57
/*
68
@@ -187,6 +189,70 @@ For 32-bit we have the following conventions - kernel is built with
187189
#endif
188190
.endm
189191

192+
#ifdef CONFIG_PAGE_TABLE_ISOLATION
193+
194+
/* PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two halves: */
195+
#define PTI_SWITCH_MASK (1<<PAGE_SHIFT)
196+
197+
.macro ADJUST_KERNEL_CR3 reg:req
198+
/* Clear "PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */
199+
andq $(~PTI_SWITCH_MASK), \reg
200+
.endm
201+
202+
.macro ADJUST_USER_CR3 reg:req
203+
/* Move CR3 up a page to the user page tables: */
204+
orq $(PTI_SWITCH_MASK), \reg
205+
.endm
206+
207+
.macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
208+
mov %cr3, \scratch_reg
209+
ADJUST_KERNEL_CR3 \scratch_reg
210+
mov \scratch_reg, %cr3
211+
.endm
212+
213+
.macro SWITCH_TO_USER_CR3 scratch_reg:req
214+
mov %cr3, \scratch_reg
215+
ADJUST_USER_CR3 \scratch_reg
216+
mov \scratch_reg, %cr3
217+
.endm
218+
219+
.macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
220+
movq %cr3, \scratch_reg
221+
movq \scratch_reg, \save_reg
222+
/*
223+
* Is the switch bit zero? This means the address is
224+
* up in real PAGE_TABLE_ISOLATION patches in a moment.
225+
*/
226+
testq $(PTI_SWITCH_MASK), \scratch_reg
227+
jz .Ldone_\@
228+
229+
ADJUST_KERNEL_CR3 \scratch_reg
230+
movq \scratch_reg, %cr3
231+
232+
.Ldone_\@:
233+
.endm
234+
235+
.macro RESTORE_CR3 save_reg:req
236+
/*
237+
* The CR3 write could be avoided when not changing its value,
238+
* but would require a CR3 read *and* a scratch register.
239+
*/
240+
movq \save_reg, %cr3
241+
.endm
242+
243+
#else /* CONFIG_PAGE_TABLE_ISOLATION=n: */
244+
245+
.macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
246+
.endm
247+
.macro SWITCH_TO_USER_CR3 scratch_reg:req
248+
.endm
249+
.macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
250+
.endm
251+
.macro RESTORE_CR3 save_reg:req
252+
.endm
253+
254+
#endif
255+
190256
#endif /* CONFIG_X86_64 */
191257

192258
/*

‎arch/x86/entry/entry_64.S‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ ENTRY(entry_SYSCALL_64_trampoline)
164164
/* Stash the user RSP. */
165165
movq %rsp, RSP_SCRATCH
166166

167+
/* Note: using %rsp as a scratch reg. */
168+
SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
169+
167170
/* Load the top of the task stack into RSP */
168171
movq CPU_ENTRY_AREA_tss + TSS_sp1 + CPU_ENTRY_AREA, %rsp
169172

@@ -203,6 +206,10 @@ ENTRY(entry_SYSCALL_64)
203206
*/
204207

205208
swapgs
209+
/*
210+
* This path is not taken when PAGE_TABLE_ISOLATION is disabled so it
211+
* is not required to switch CR3.
212+
*/
206213
movq %rsp, PER_CPU_VAR(rsp_scratch)
207214
movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
208215

@@ -399,6 +406,7 @@ syscall_return_via_sysret:
399406
* We are on the trampoline stack. All regs except RDI are live.
400407
* We can do future final exit work right here.
401408
*/
409+
SWITCH_TO_USER_CR3 scratch_reg=%rdi
402410

403411
popq %rdi
404412
popq %rsp
@@ -736,6 +744,8 @@ GLOBAL(swapgs_restore_regs_and_return_to_usermode)
736744
* We can do future final exit work right here.
737745
*/
738746

747+
SWITCH_TO_USER_CR3 scratch_reg=%rdi
748+
739749
/* Restore RDI. */
740750
popq %rdi
741751
SWAPGS
@@ -818,7 +828,9 @@ native_irq_return_ldt:
818828
*/
819829

820830
pushq %rdi /* Stash user RDI */
821-
SWAPGS
831+
SWAPGS /* to kernel GS */
832+
SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi /* to kernel CR3 */
833+
822834
movq PER_CPU_VAR(espfix_waddr), %rdi
823835
movq %rax, (0*8)(%rdi) /* user RAX */
824836
movq (1*8)(%rsp), %rax /* user RIP */
@@ -834,7 +846,6 @@ native_irq_return_ldt:
834846
/* Now RAX == RSP. */
835847

836848
andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */
837-
popq %rdi /* Restore user RDI */
838849

839850
/*
840851
* espfix_stack[31:16] == 0. The page tables are set up such that
@@ -845,7 +856,11 @@ native_irq_return_ldt:
845856
* still points to an RO alias of the ESPFIX stack.
846857
*/
847858
orq PER_CPU_VAR(espfix_stack), %rax
848-
SWAPGS
859+
860+
SWITCH_TO_USER_CR3 scratch_reg=%rdi /* to user CR3 */
861+
SWAPGS /* to user GS */
862+
popq %rdi /* Restore user RDI */
863+
849864
movq %rax, %rsp
850865
UNWIND_HINT_IRET_REGS offset=8
851866

@@ -945,6 +960,8 @@ ENTRY(switch_to_thread_stack)
945960
UNWIND_HINT_FUNC
946961

947962
pushq %rdi
963+
/* Need to switch before accessing the thread stack. */
964+
SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi
948965
movq %rsp, %rdi
949966
movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
950967
UNWIND_HINT sp_offset=16 sp_reg=ORC_REG_DI
@@ -1244,7 +1261,11 @@ ENTRY(paranoid_entry)
12441261
js 1f /* negative -> in kernel */
12451262
SWAPGS
12461263
xorl %ebx, %ebx
1247-
1: ret
1264+
1265+
1:
1266+
SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
1267+
1268+
ret
12481269
END(paranoid_entry)
12491270

12501271
/*
@@ -1266,6 +1287,7 @@ ENTRY(paranoid_exit)
12661287
testl %ebx, %ebx /* swapgs needed? */
12671288
jnz .Lparanoid_exit_no_swapgs
12681289
TRACE_IRQS_IRETQ
1290+
RESTORE_CR3 save_reg=%r14
12691291
SWAPGS_UNSAFE_STACK
12701292
jmp .Lparanoid_exit_restore
12711293
.Lparanoid_exit_no_swapgs:
@@ -1293,6 +1315,8 @@ ENTRY(error_entry)
12931315
* from user mode due to an IRET fault.
12941316
*/
12951317
SWAPGS
1318+
/* We have user CR3. Change to kernel CR3. */
1319+
SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
12961320

12971321
.Lerror_entry_from_usermode_after_swapgs:
12981322
/* Put us onto the real thread stack. */
@@ -1339,6 +1363,7 @@ ENTRY(error_entry)
13391363
* .Lgs_change's error handler with kernel gsbase.
13401364
*/
13411365
SWAPGS
1366+
SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
13421367
jmp .Lerror_entry_done
13431368

13441369
.Lbstep_iret:
@@ -1348,10 +1373,11 @@ ENTRY(error_entry)
13481373

13491374
.Lerror_bad_iret:
13501375
/*
1351-
* We came from an IRET to user mode, so we have user gsbase.
1352-
* Switch to kernel gsbase:
1376+
* We came from an IRET to user mode, so we have user
1377+
* gsbase and CR3. Switch to kernel gsbase and CR3:
13531378
*/
13541379
SWAPGS
1380+
SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
13551381

13561382
/*
13571383
* Pretend that the exception came from user mode: set up pt_regs
@@ -1383,6 +1409,10 @@ END(error_exit)
13831409
/*
13841410
* Runs on exception stack. Xen PV does not go through this path at all,
13851411
* so we can use real assembly here.
1412+
*
1413+
* Registers:
1414+
* %r14: Used to save/restore the CR3 of the interrupted context
1415+
* when PAGE_TABLE_ISOLATION is in use. Do not clobber.
13861416
*/
13871417
ENTRY(nmi)
13881418
UNWIND_HINT_IRET_REGS
@@ -1446,6 +1476,7 @@ ENTRY(nmi)
14461476

14471477
swapgs
14481478
cld
1479+
SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
14491480
movq %rsp, %rdx
14501481
movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
14511482
UNWIND_HINT_IRET_REGS base=%rdx offset=8
@@ -1698,6 +1729,8 @@ end_repeat_nmi:
16981729
movq $-1, %rsi
16991730
call do_nmi
17001731

1732+
RESTORE_CR3 save_reg=%r14
1733+
17011734
testl %ebx, %ebx /* swapgs needed? */
17021735
jnz nmi_restore
17031736
nmi_swapgs:

‎arch/x86/entry/entry_64_compat.S‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
ENTRY(entry_SYSENTER_compat)
5050
/* Interrupts are off on entry. */
5151
SWAPGS
52+
53+
/* We are about to clobber %rsp anyway, clobbering here is OK */
54+
SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
55+
5256
movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
5357

5458
/*
@@ -215,6 +219,12 @@ GLOBAL(entry_SYSCALL_compat_after_hwframe)
215219
pushq $0 /* pt_regs->r14 = 0 */
216220
pushq $0 /* pt_regs->r15 = 0 */
217221

222+
/*
223+
* We just saved %rdi so it is safe to clobber. It is not
224+
* preserved during the C calls inside TRACE_IRQS_OFF anyway.
225+
*/
226+
SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi
227+
218228
/*
219229
* User mode is traced as though IRQs are on, and SYSENTER
220230
* turned them off.
@@ -256,10 +266,22 @@ sysret32_from_system_call:
256266
* when the system call started, which is already known to user
257267
* code. We zero R8-R10 to avoid info leaks.
258268
*/
269+
movq RSP-ORIG_RAX(%rsp), %rsp
270+
271+
/*
272+
* The original userspace %rsp (RSP-ORIG_RAX(%rsp)) is stored
273+
* on the process stack which is not mapped to userspace and
274+
* not readable after we SWITCH_TO_USER_CR3. Delay the CR3
275+
* switch until after after the last reference to the process
276+
* stack.
277+
*
278+
* %r8 is zeroed before the sysret, thus safe to clobber.
279+
*/
280+
SWITCH_TO_USER_CR3 scratch_reg=%r8
281+
259282
xorq %r8, %r8
260283
xorq %r9, %r9
261284
xorq %r10, %r10
262-
movq RSP-ORIG_RAX(%rsp), %rsp
263285
swapgs
264286
sysretl
265287
END(entry_SYSCALL_compat)

0 commit comments

Comments
 (0)