1 From 271bc7d0577bef9f344187eb45ba8682eed242f9 Mon Sep 17 00:00:00 2001
2 From: Andy Lutomirski <luto@kernel.org>
3 Date: Thu, 2 Nov 2017 00:59:00 -0700
4 Subject: [PATCH 088/233] x86/entry/64: Move SWAPGS into the common
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
12 All of the code paths that ended up doing IRET to usermode did
13 SWAPGS immediately beforehand. Move the SWAPGS into the common
16 Signed-off-by: Andy Lutomirski <luto@kernel.org>
17 Cc: Borislav Petkov <bpetkov@suse.de>
18 Cc: Brian Gerst <brgerst@gmail.com>
19 Cc: Dave Hansen <dave.hansen@intel.com>
20 Cc: Linus Torvalds <torvalds@linux-foundation.org>
21 Cc: Peter Zijlstra <peterz@infradead.org>
22 Cc: Thomas Gleixner <tglx@linutronix.de>
23 Link: http://lkml.kernel.org/r/27fd6f45b7cd640de38fb9066fd0349bcd11f8e1.1509609304.git.luto@kernel.org
24 Signed-off-by: Ingo Molnar <mingo@kernel.org>
25 (cherry picked from commit 8a055d7f411d41755ce30db5bb65b154777c4b78)
26 Signed-off-by: Andy Whitcroft <apw@canonical.com>
27 Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
28 (cherry picked from commit 62a85594f9be3baeb2495089f1c2980bc497d03b)
29 Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
31 arch/x86/entry/entry_64.S | 32 ++++++++++++++------------------
32 arch/x86/entry/entry_64_compat.S | 3 +--
33 2 files changed, 15 insertions(+), 20 deletions(-)
35 diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
36 index e546441fbec3..7c8258e3ad2d 100644
37 --- a/arch/x86/entry/entry_64.S
38 +++ b/arch/x86/entry/entry_64.S
39 @@ -249,12 +249,14 @@ return_from_SYSCALL_64:
42 * Try to use SYSRET instead of IRET if we're returning to
43 - * a completely clean 64-bit userspace context.
44 + * a completely clean 64-bit userspace context. If we're not,
45 + * go to the slow exit path.
49 - cmpq %rcx, %r11 /* RCX == RIP */
50 - jne opportunistic_sysret_failed
52 + cmpq %rcx, %r11 /* SYSRET requires RCX == RIP */
53 + jne swapgs_restore_regs_and_return_to_usermode
56 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
57 @@ -272,14 +274,14 @@ return_from_SYSCALL_64:
59 /* If this changed %rcx, it was not canonical */
61 - jne opportunistic_sysret_failed
62 + jne swapgs_restore_regs_and_return_to_usermode
64 cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
65 - jne opportunistic_sysret_failed
66 + jne swapgs_restore_regs_and_return_to_usermode
69 cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
70 - jne opportunistic_sysret_failed
71 + jne swapgs_restore_regs_and_return_to_usermode
74 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
75 @@ -300,12 +302,12 @@ return_from_SYSCALL_64:
76 * would never get past 'stuck_here'.
78 testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
79 - jnz opportunistic_sysret_failed
80 + jnz swapgs_restore_regs_and_return_to_usermode
82 /* nothing to check for RSP */
84 cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
85 - jne opportunistic_sysret_failed
86 + jne swapgs_restore_regs_and_return_to_usermode
89 * We win! This label is here just for ease of understanding
90 @@ -318,10 +320,6 @@ syscall_return_via_sysret:
95 -opportunistic_sysret_failed:
97 - jmp restore_regs_and_return_to_usermode
100 ENTRY(stub_ptregs_64)
101 @@ -422,8 +420,7 @@ ENTRY(ret_from_fork)
103 call syscall_return_slowpath /* returns with IRQs disabled */
104 TRACE_IRQS_ON /* user mode is traced as IRQS on */
106 - jmp restore_regs_and_return_to_usermode
107 + jmp swapgs_restore_regs_and_return_to_usermode
111 @@ -611,9 +608,8 @@ GLOBAL(retint_user)
113 call prepare_exit_to_usermode
117 -GLOBAL(restore_regs_and_return_to_usermode)
118 +GLOBAL(swapgs_restore_regs_and_return_to_usermode)
119 #ifdef CONFIG_DEBUG_ENTRY
120 /* Assert that pt_regs indicates user mode. */
122 @@ -621,6 +617,7 @@ GLOBAL(restore_regs_and_return_to_usermode)
129 REMOVE_PT_GPREGS_FROM_STACK 8
130 @@ -1355,8 +1352,7 @@ ENTRY(nmi)
131 * Return back to user mode. We must *not* do the normal exit
132 * work, because we don't want to enable interrupts.
135 - jmp restore_regs_and_return_to_usermode
136 + jmp swapgs_restore_regs_and_return_to_usermode
140 diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S
141 index 2b3a88feaa2b..be745b7a3e3e 100644
142 --- a/arch/x86/entry/entry_64_compat.S
143 +++ b/arch/x86/entry/entry_64_compat.S
144 @@ -336,8 +336,7 @@ ENTRY(entry_INT80_compat)
146 /* Go back to user mode. */
149 - jmp restore_regs_and_return_to_usermode
150 + jmp swapgs_restore_regs_and_return_to_usermode
151 END(entry_INT80_compat)