xref: /openbmc/linux/arch/x86/entry/entry_64.S (revision 0595494891723a1dcca5eaa8eeca8ab54ad953b9)
1b2441318SGreg Kroah-Hartman/* SPDX-License-Identifier: GPL-2.0 */
2905a36a2SIngo Molnar/*
3905a36a2SIngo Molnar *  linux/arch/x86_64/entry.S
4905a36a2SIngo Molnar *
5905a36a2SIngo Molnar *  Copyright (C) 1991, 1992  Linus Torvalds
6905a36a2SIngo Molnar *  Copyright (C) 2000, 2001, 2002  Andi Kleen SuSE Labs
7905a36a2SIngo Molnar *  Copyright (C) 2000  Pavel Machek <pavel@suse.cz>
84d732138SIngo Molnar *
9905a36a2SIngo Molnar * entry.S contains the system-call and fault low-level handling routines.
10905a36a2SIngo Molnar *
11cb1aaebeSMauro Carvalho Chehab * Some of this is documented in Documentation/x86/entry_64.rst
12905a36a2SIngo Molnar *
13905a36a2SIngo Molnar * A note on terminology:
14905a36a2SIngo Molnar * - iret frame:	Architecture defined interrupt frame from SS to RIP
15905a36a2SIngo Molnar *			at the top of the kernel process stack.
16905a36a2SIngo Molnar *
17905a36a2SIngo Molnar * Some macro usage:
186dcc5627SJiri Slaby * - SYM_FUNC_START/END:Define functions in the symbol table.
194d732138SIngo Molnar * - idtentry:		Define exception entry points.
20905a36a2SIngo Molnar */
21905a36a2SIngo Molnar#include <linux/linkage.h>
22905a36a2SIngo Molnar#include <asm/segment.h>
23905a36a2SIngo Molnar#include <asm/cache.h>
24905a36a2SIngo Molnar#include <asm/errno.h>
25905a36a2SIngo Molnar#include <asm/asm-offsets.h>
26905a36a2SIngo Molnar#include <asm/msr.h>
27905a36a2SIngo Molnar#include <asm/unistd.h>
28905a36a2SIngo Molnar#include <asm/thread_info.h>
29905a36a2SIngo Molnar#include <asm/hw_irq.h>
30905a36a2SIngo Molnar#include <asm/page_types.h>
31905a36a2SIngo Molnar#include <asm/irqflags.h>
32905a36a2SIngo Molnar#include <asm/paravirt.h>
33905a36a2SIngo Molnar#include <asm/percpu.h>
34905a36a2SIngo Molnar#include <asm/asm.h>
35905a36a2SIngo Molnar#include <asm/smap.h>
36905a36a2SIngo Molnar#include <asm/pgtable_types.h>
37784d5699SAl Viro#include <asm/export.h>
388c1f7558SJosh Poimboeuf#include <asm/frame.h>
39cfa82a00SThomas Gleixner#include <asm/trapnr.h>
402641f08bSDavid Woodhouse#include <asm/nospec-branch.h>
41c82965f9SChang S. Bae#include <asm/fsgsbase.h>
42905a36a2SIngo Molnar#include <linux/err.h>
43905a36a2SIngo Molnar
446fd166aaSPeter Zijlstra#include "calling.h"
456fd166aaSPeter Zijlstra
46905a36a2SIngo Molnar.code64
47905a36a2SIngo Molnar.section .entry.text, "ax"
48905a36a2SIngo Molnar
49905a36a2SIngo Molnar/*
504d732138SIngo Molnar * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
51905a36a2SIngo Molnar *
52fda57b22SAndy Lutomirski * This is the only entry point used for 64-bit system calls.  The
53fda57b22SAndy Lutomirski * hardware interface is reasonably well designed and the register to
54fda57b22SAndy Lutomirski * argument mapping Linux uses fits well with the registers that are
55fda57b22SAndy Lutomirski * available when SYSCALL is used.
56fda57b22SAndy Lutomirski *
57fda57b22SAndy Lutomirski * SYSCALL instructions can be found inlined in libc implementations as
58fda57b22SAndy Lutomirski * well as some other programs and libraries.  There are also a handful
59fda57b22SAndy Lutomirski * of SYSCALL instructions in the vDSO used, for example, as a
60fda57b22SAndy Lutomirski * clock_gettimeofday fallback.
61fda57b22SAndy Lutomirski *
624d732138SIngo Molnar * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
63905a36a2SIngo Molnar * then loads new ss, cs, and rip from previously programmed MSRs.
64905a36a2SIngo Molnar * rflags gets masked by a value from another MSR (so CLD and CLAC
65905a36a2SIngo Molnar * are not needed). SYSCALL does not save anything on the stack
66905a36a2SIngo Molnar * and does not change rsp.
67905a36a2SIngo Molnar *
68905a36a2SIngo Molnar * Registers on entry:
69905a36a2SIngo Molnar * rax  system call number
70905a36a2SIngo Molnar * rcx  return address
71905a36a2SIngo Molnar * r11  saved rflags (note: r11 is callee-clobbered register in C ABI)
72905a36a2SIngo Molnar * rdi  arg0
73905a36a2SIngo Molnar * rsi  arg1
74905a36a2SIngo Molnar * rdx  arg2
75905a36a2SIngo Molnar * r10  arg3 (needs to be moved to rcx to conform to C ABI)
76905a36a2SIngo Molnar * r8   arg4
77905a36a2SIngo Molnar * r9   arg5
78905a36a2SIngo Molnar * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
79905a36a2SIngo Molnar *
80905a36a2SIngo Molnar * Only called from user space.
81905a36a2SIngo Molnar *
82905a36a2SIngo Molnar * When user can change pt_regs->foo always force IRET. That is because
83905a36a2SIngo Molnar * it deals with uncanonical addresses better. SYSRET has trouble
84905a36a2SIngo Molnar * with them due to bugs in both AMD and Intel CPUs.
85905a36a2SIngo Molnar */
86905a36a2SIngo Molnar
87bc7b11c0SJiri SlabySYM_CODE_START(entry_SYSCALL_64)
888c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
89905a36a2SIngo Molnar
908a9949bcSAndy Lutomirski	swapgs
91bf904d27SAndy Lutomirski	/* tss.sp2 is scratch space. */
9298f05b51SAndy Lutomirski	movq	%rsp, PER_CPU_VAR(cpu_tss_rw + TSS_sp2)
93bf904d27SAndy Lutomirski	SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
94905a36a2SIngo Molnar	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
95905a36a2SIngo Molnar
96a13644f3SJoerg RoedelSYM_INNER_LABEL(entry_SYSCALL_64_safe_stack, SYM_L_GLOBAL)
97a13644f3SJoerg Roedel
98905a36a2SIngo Molnar	/* Construct struct pt_regs on stack */
99905a36a2SIngo Molnar	pushq	$__USER_DS				/* pt_regs->ss */
10098f05b51SAndy Lutomirski	pushq	PER_CPU_VAR(cpu_tss_rw + TSS_sp2)	/* pt_regs->sp */
101905a36a2SIngo Molnar	pushq	%r11					/* pt_regs->flags */
102905a36a2SIngo Molnar	pushq	$__USER_CS				/* pt_regs->cs */
103905a36a2SIngo Molnar	pushq	%rcx					/* pt_regs->ip */
10426ba4e57SJiri SlabySYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL)
105905a36a2SIngo Molnar	pushq	%rax					/* pt_regs->orig_ax */
10630907fd1SDominik Brodowski
10730907fd1SDominik Brodowski	PUSH_AND_CLEAR_REGS rax=$-ENOSYS
108905a36a2SIngo Molnar
1091e423bffSAndy Lutomirski	/* IRQs are off. */
1103e5e7f77SH. Peter Anvin (Intel)	movq	%rsp, %rdi
111*05954948SH. Peter Anvin (Intel)	/* Sign extend the lower 32bit as syscall numbers are treated as int */
112*05954948SH. Peter Anvin (Intel)	movslq	%eax, %rsi
1131e423bffSAndy Lutomirski	call	do_syscall_64		/* returns with IRQs disabled */
1141e423bffSAndy Lutomirski
115905a36a2SIngo Molnar	/*
116905a36a2SIngo Molnar	 * Try to use SYSRET instead of IRET if we're returning to
1178a055d7fSAndy Lutomirski	 * a completely clean 64-bit userspace context.  If we're not,
1188a055d7fSAndy Lutomirski	 * go to the slow exit path.
119afd30525SJuergen Gross	 * In the Xen PV case we must use iret anyway.
120905a36a2SIngo Molnar	 */
121afd30525SJuergen Gross
122afd30525SJuergen Gross	ALTERNATIVE "", "jmp	swapgs_restore_regs_and_return_to_usermode", \
123afd30525SJuergen Gross		X86_FEATURE_XENPV
124afd30525SJuergen Gross
125905a36a2SIngo Molnar	movq	RCX(%rsp), %rcx
126905a36a2SIngo Molnar	movq	RIP(%rsp), %r11
1278a055d7fSAndy Lutomirski
1288a055d7fSAndy Lutomirski	cmpq	%rcx, %r11	/* SYSRET requires RCX == RIP */
1298a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
130905a36a2SIngo Molnar
131905a36a2SIngo Molnar	/*
132905a36a2SIngo Molnar	 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
133905a36a2SIngo Molnar	 * in kernel space.  This essentially lets the user take over
134905a36a2SIngo Molnar	 * the kernel, since userspace controls RSP.
135905a36a2SIngo Molnar	 *
136905a36a2SIngo Molnar	 * If width of "canonical tail" ever becomes variable, this will need
137905a36a2SIngo Molnar	 * to be updated to remain correct on both old and new CPUs.
138361b4b58SKirill A. Shutemov	 *
139cbe0317bSKirill A. Shutemov	 * Change top bits to match most significant bit (47th or 56th bit
140cbe0317bSKirill A. Shutemov	 * depending on paging mode) in the address.
141905a36a2SIngo Molnar	 */
14209e61a77SKirill A. Shutemov#ifdef CONFIG_X86_5LEVEL
14339b95522SKirill A. Shutemov	ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \
14439b95522SKirill A. Shutemov		"shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57
14509e61a77SKirill A. Shutemov#else
146905a36a2SIngo Molnar	shl	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
147905a36a2SIngo Molnar	sar	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
14809e61a77SKirill A. Shutemov#endif
1494d732138SIngo Molnar
150905a36a2SIngo Molnar	/* If this changed %rcx, it was not canonical */
151905a36a2SIngo Molnar	cmpq	%rcx, %r11
1528a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
153905a36a2SIngo Molnar
154905a36a2SIngo Molnar	cmpq	$__USER_CS, CS(%rsp)		/* CS must match SYSRET */
1558a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
156905a36a2SIngo Molnar
157905a36a2SIngo Molnar	movq	R11(%rsp), %r11
158905a36a2SIngo Molnar	cmpq	%r11, EFLAGS(%rsp)		/* R11 == RFLAGS */
1598a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
160905a36a2SIngo Molnar
161905a36a2SIngo Molnar	/*
1623e035305SBorislav Petkov	 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
1633e035305SBorislav Petkov	 * restore RF properly. If the slowpath sets it for whatever reason, we
1643e035305SBorislav Petkov	 * need to restore it correctly.
1653e035305SBorislav Petkov	 *
1663e035305SBorislav Petkov	 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
1673e035305SBorislav Petkov	 * trap from userspace immediately after SYSRET.  This would cause an
1683e035305SBorislav Petkov	 * infinite loop whenever #DB happens with register state that satisfies
1693e035305SBorislav Petkov	 * the opportunistic SYSRET conditions.  For example, single-stepping
1703e035305SBorislav Petkov	 * this user code:
171905a36a2SIngo Molnar	 *
172905a36a2SIngo Molnar	 *           movq	$stuck_here, %rcx
173905a36a2SIngo Molnar	 *           pushfq
174905a36a2SIngo Molnar	 *           popq %r11
175905a36a2SIngo Molnar	 *   stuck_here:
176905a36a2SIngo Molnar	 *
177905a36a2SIngo Molnar	 * would never get past 'stuck_here'.
178905a36a2SIngo Molnar	 */
179905a36a2SIngo Molnar	testq	$(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
1808a055d7fSAndy Lutomirski	jnz	swapgs_restore_regs_and_return_to_usermode
181905a36a2SIngo Molnar
182905a36a2SIngo Molnar	/* nothing to check for RSP */
183905a36a2SIngo Molnar
184905a36a2SIngo Molnar	cmpq	$__USER_DS, SS(%rsp)		/* SS must match SYSRET */
1858a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
186905a36a2SIngo Molnar
187905a36a2SIngo Molnar	/*
188905a36a2SIngo Molnar	 * We win! This label is here just for ease of understanding
189905a36a2SIngo Molnar	 * perf profiles. Nothing jumps here.
190905a36a2SIngo Molnar	 */
191905a36a2SIngo Molnarsyscall_return_via_sysret:
192905a36a2SIngo Molnar	/* rcx and r11 are already restored (see code above) */
193502af0d7SDominik Brodowski	POP_REGS pop_rdi=0 skip_r11rcx=1
1943e3b9293SAndy Lutomirski
1953e3b9293SAndy Lutomirski	/*
1963e3b9293SAndy Lutomirski	 * Now all regs are restored except RSP and RDI.
1973e3b9293SAndy Lutomirski	 * Save old stack pointer and switch to trampoline stack.
1983e3b9293SAndy Lutomirski	 */
1993e3b9293SAndy Lutomirski	movq	%rsp, %rdi
200c482feefSAndy Lutomirski	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
2011fb14363SJosh Poimboeuf	UNWIND_HINT_EMPTY
2023e3b9293SAndy Lutomirski
2033e3b9293SAndy Lutomirski	pushq	RSP-RDI(%rdi)	/* RSP */
2043e3b9293SAndy Lutomirski	pushq	(%rdi)		/* RDI */
2053e3b9293SAndy Lutomirski
2063e3b9293SAndy Lutomirski	/*
2073e3b9293SAndy Lutomirski	 * We are on the trampoline stack.  All regs except RDI are live.
2083e3b9293SAndy Lutomirski	 * We can do future final exit work right here.
2093e3b9293SAndy Lutomirski	 */
210afaef01cSAlexander Popov	STACKLEAK_ERASE_NOCLOBBER
211afaef01cSAlexander Popov
2126fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
2133e3b9293SAndy Lutomirski
2144fbb3910SAndy Lutomirski	popq	%rdi
2153e3b9293SAndy Lutomirski	popq	%rsp
216afd30525SJuergen Gross	swapgs
217afd30525SJuergen Gross	sysretq
218bc7b11c0SJiri SlabySYM_CODE_END(entry_SYSCALL_64)
219905a36a2SIngo Molnar
220905a36a2SIngo Molnar/*
2210100301bSBrian Gerst * %rdi: prev task
2220100301bSBrian Gerst * %rsi: next task
2230100301bSBrian Gerst */
224b9f6976bSThomas Gleixner.pushsection .text, "ax"
22596c64806SJosh PoimboeufSYM_FUNC_START(__switch_to_asm)
2260100301bSBrian Gerst	/*
2270100301bSBrian Gerst	 * Save callee-saved registers
2280100301bSBrian Gerst	 * This must match the order in inactive_task_frame
2290100301bSBrian Gerst	 */
2300100301bSBrian Gerst	pushq	%rbp
2310100301bSBrian Gerst	pushq	%rbx
2320100301bSBrian Gerst	pushq	%r12
2330100301bSBrian Gerst	pushq	%r13
2340100301bSBrian Gerst	pushq	%r14
2350100301bSBrian Gerst	pushq	%r15
2360100301bSBrian Gerst
2370100301bSBrian Gerst	/* switch stack */
2380100301bSBrian Gerst	movq	%rsp, TASK_threadsp(%rdi)
2390100301bSBrian Gerst	movq	TASK_threadsp(%rsi), %rsp
2400100301bSBrian Gerst
241050e9baaSLinus Torvalds#ifdef CONFIG_STACKPROTECTOR
2420100301bSBrian Gerst	movq	TASK_stack_canary(%rsi), %rbx
243e6401c13SAndy Lutomirski	movq	%rbx, PER_CPU_VAR(fixed_percpu_data) + stack_canary_offset
2440100301bSBrian Gerst#endif
2450100301bSBrian Gerst
246c995efd5SDavid Woodhouse#ifdef CONFIG_RETPOLINE
247c995efd5SDavid Woodhouse	/*
248c995efd5SDavid Woodhouse	 * When switching from a shallower to a deeper call stack
249c995efd5SDavid Woodhouse	 * the RSB may either underflow or use entries populated
250c995efd5SDavid Woodhouse	 * with userspace addresses. On CPUs where those concerns
251c995efd5SDavid Woodhouse	 * exist, overwrite the RSB with entries which capture
252c995efd5SDavid Woodhouse	 * speculative execution to prevent attack.
253c995efd5SDavid Woodhouse	 */
254d1c99108SDavid Woodhouse	FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
255c995efd5SDavid Woodhouse#endif
256c995efd5SDavid Woodhouse
2570100301bSBrian Gerst	/* restore callee-saved registers */
2580100301bSBrian Gerst	popq	%r15
2590100301bSBrian Gerst	popq	%r14
2600100301bSBrian Gerst	popq	%r13
2610100301bSBrian Gerst	popq	%r12
2620100301bSBrian Gerst	popq	%rbx
2630100301bSBrian Gerst	popq	%rbp
2640100301bSBrian Gerst
2650100301bSBrian Gerst	jmp	__switch_to
26696c64806SJosh PoimboeufSYM_FUNC_END(__switch_to_asm)
267b9f6976bSThomas Gleixner.popsection
2680100301bSBrian Gerst
2690100301bSBrian Gerst/*
270905a36a2SIngo Molnar * A newly forked process directly context switches into this address.
271905a36a2SIngo Molnar *
2720100301bSBrian Gerst * rax: prev task we switched from
273616d2483SBrian Gerst * rbx: kernel thread func (NULL for user thread)
274616d2483SBrian Gerst * r12: kernel thread arg
275905a36a2SIngo Molnar */
276b9f6976bSThomas Gleixner.pushsection .text, "ax"
277bc7b11c0SJiri SlabySYM_CODE_START(ret_from_fork)
2788c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
2790100301bSBrian Gerst	movq	%rax, %rdi
2804d732138SIngo Molnar	call	schedule_tail			/* rdi: 'prev' task parameter */
281905a36a2SIngo Molnar
282616d2483SBrian Gerst	testq	%rbx, %rbx			/* from kernel_thread? */
283616d2483SBrian Gerst	jnz	1f				/* kernel threads are uncommon */
284905a36a2SIngo Molnar
285616d2483SBrian Gerst2:
2868c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
287ebd57499SJosh Poimboeuf	movq	%rsp, %rdi
288167fd210SThomas Gleixner	call	syscall_exit_to_user_mode	/* returns with IRQs disabled */
2898a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
290616d2483SBrian Gerst
291616d2483SBrian Gerst1:
292616d2483SBrian Gerst	/* kernel thread */
293d31a5802SJosh Poimboeuf	UNWIND_HINT_EMPTY
294616d2483SBrian Gerst	movq	%r12, %rdi
29534fdce69SPeter Zijlstra	CALL_NOSPEC rbx
296616d2483SBrian Gerst	/*
297616d2483SBrian Gerst	 * A kernel thread is allowed to return here after successfully
298be619f7fSEric W. Biederman	 * calling kernel_execve().  Exit to userspace to complete the execve()
299616d2483SBrian Gerst	 * syscall.
300616d2483SBrian Gerst	 */
301616d2483SBrian Gerst	movq	$0, RAX(%rsp)
302616d2483SBrian Gerst	jmp	2b
303bc7b11c0SJiri SlabySYM_CODE_END(ret_from_fork)
304b9f6976bSThomas Gleixner.popsection
305905a36a2SIngo Molnar
3061d3e53e8SAndy Lutomirski.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
3071d3e53e8SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
308e17f8234SBoris Ostrovsky	pushq %rax
309fafe5e74SJuergen Gross	SAVE_FLAGS
310e17f8234SBoris Ostrovsky	testl $X86_EFLAGS_IF, %eax
3111d3e53e8SAndy Lutomirski	jz .Lokay_\@
3121d3e53e8SAndy Lutomirski	ud2
3131d3e53e8SAndy Lutomirski.Lokay_\@:
314e17f8234SBoris Ostrovsky	popq %rax
3151d3e53e8SAndy Lutomirski#endif
3161d3e53e8SAndy Lutomirski.endm
3171d3e53e8SAndy Lutomirski
318cfa82a00SThomas Gleixner/**
319cfa82a00SThomas Gleixner * idtentry_body - Macro to emit code calling the C function
320cfa82a00SThomas Gleixner * @cfunc:		C function to be called
321cfa82a00SThomas Gleixner * @has_error_code:	Hardware pushed error code on stack
322cfa82a00SThomas Gleixner */
323e2dcb5f1SThomas Gleixner.macro idtentry_body cfunc has_error_code:req
324cfa82a00SThomas Gleixner
325cfa82a00SThomas Gleixner	call	error_entry
326cfa82a00SThomas Gleixner	UNWIND_HINT_REGS
327cfa82a00SThomas Gleixner
328cfa82a00SThomas Gleixner	movq	%rsp, %rdi			/* pt_regs pointer into 1st argument*/
329cfa82a00SThomas Gleixner
330cfa82a00SThomas Gleixner	.if \has_error_code == 1
331cfa82a00SThomas Gleixner		movq	ORIG_RAX(%rsp), %rsi	/* get error code into 2nd argument*/
332cfa82a00SThomas Gleixner		movq	$-1, ORIG_RAX(%rsp)	/* no syscall to restart */
333cfa82a00SThomas Gleixner	.endif
334cfa82a00SThomas Gleixner
335cfa82a00SThomas Gleixner	call	\cfunc
336cfa82a00SThomas Gleixner
337424c7d0aSThomas Gleixner	jmp	error_return
338cfa82a00SThomas Gleixner.endm
339cfa82a00SThomas Gleixner
340cfa82a00SThomas Gleixner/**
341cfa82a00SThomas Gleixner * idtentry - Macro to generate entry stubs for simple IDT entries
342cfa82a00SThomas Gleixner * @vector:		Vector number
343cfa82a00SThomas Gleixner * @asmsym:		ASM symbol for the entry point
344cfa82a00SThomas Gleixner * @cfunc:		C function to be called
345cfa82a00SThomas Gleixner * @has_error_code:	Hardware pushed error code on stack
346cfa82a00SThomas Gleixner *
347cfa82a00SThomas Gleixner * The macro emits code to set up the kernel context for straight forward
348cfa82a00SThomas Gleixner * and simple IDT entries. No IST stack, no paranoid entry checks.
349cfa82a00SThomas Gleixner */
350e2dcb5f1SThomas Gleixner.macro idtentry vector asmsym cfunc has_error_code:req
351cfa82a00SThomas GleixnerSYM_CODE_START(\asmsym)
352cfa82a00SThomas Gleixner	UNWIND_HINT_IRET_REGS offset=\has_error_code*8
353cfa82a00SThomas Gleixner	ASM_CLAC
354cfa82a00SThomas Gleixner
355cfa82a00SThomas Gleixner	.if \has_error_code == 0
356cfa82a00SThomas Gleixner		pushq	$-1			/* ORIG_RAX: no syscall to restart */
357cfa82a00SThomas Gleixner	.endif
358cfa82a00SThomas Gleixner
359cfa82a00SThomas Gleixner	.if \vector == X86_TRAP_BP
360cfa82a00SThomas Gleixner		/*
361cfa82a00SThomas Gleixner		 * If coming from kernel space, create a 6-word gap to allow the
362cfa82a00SThomas Gleixner		 * int3 handler to emulate a call instruction.
363cfa82a00SThomas Gleixner		 */
364cfa82a00SThomas Gleixner		testb	$3, CS-ORIG_RAX(%rsp)
365cfa82a00SThomas Gleixner		jnz	.Lfrom_usermode_no_gap_\@
366cfa82a00SThomas Gleixner		.rept	6
367cfa82a00SThomas Gleixner		pushq	5*8(%rsp)
368cfa82a00SThomas Gleixner		.endr
369cfa82a00SThomas Gleixner		UNWIND_HINT_IRET_REGS offset=8
370cfa82a00SThomas Gleixner.Lfrom_usermode_no_gap_\@:
371cfa82a00SThomas Gleixner	.endif
372cfa82a00SThomas Gleixner
373e2dcb5f1SThomas Gleixner	idtentry_body \cfunc \has_error_code
374cfa82a00SThomas Gleixner
375cfa82a00SThomas Gleixner_ASM_NOKPROBE(\asmsym)
376cfa82a00SThomas GleixnerSYM_CODE_END(\asmsym)
377cfa82a00SThomas Gleixner.endm
378cfa82a00SThomas Gleixner
379cfa82a00SThomas Gleixner/*
3800bf7c314SThomas Gleixner * Interrupt entry/exit.
3810bf7c314SThomas Gleixner *
3820bf7c314SThomas Gleixner + The interrupt stubs push (vector) onto the stack, which is the error_code
3830bf7c314SThomas Gleixner * position of idtentry exceptions, and jump to one of the two idtentry points
3840bf7c314SThomas Gleixner * (common/spurious).
3850bf7c314SThomas Gleixner *
3860bf7c314SThomas Gleixner * common_interrupt is a hotpath, align it to a cache line
3870bf7c314SThomas Gleixner */
3880bf7c314SThomas Gleixner.macro idtentry_irq vector cfunc
3890bf7c314SThomas Gleixner	.p2align CONFIG_X86_L1_CACHE_SHIFT
3900bf7c314SThomas Gleixner	idtentry \vector asm_\cfunc \cfunc has_error_code=1
3910bf7c314SThomas Gleixner.endm
3920bf7c314SThomas Gleixner
3930bf7c314SThomas Gleixner/*
3946368558cSThomas Gleixner * System vectors which invoke their handlers directly and are not
3956368558cSThomas Gleixner * going through the regular common device interrupt handling code.
3966368558cSThomas Gleixner */
3976368558cSThomas Gleixner.macro idtentry_sysvec vector cfunc
3986368558cSThomas Gleixner	idtentry \vector asm_\cfunc \cfunc has_error_code=0
3996368558cSThomas Gleixner.endm
4006368558cSThomas Gleixner
401cfa82a00SThomas Gleixner/**
402cfa82a00SThomas Gleixner * idtentry_mce_db - Macro to generate entry stubs for #MC and #DB
403cfa82a00SThomas Gleixner * @vector:		Vector number
404cfa82a00SThomas Gleixner * @asmsym:		ASM symbol for the entry point
405cfa82a00SThomas Gleixner * @cfunc:		C function to be called
406cfa82a00SThomas Gleixner *
407cfa82a00SThomas Gleixner * The macro emits code to set up the kernel context for #MC and #DB
408cfa82a00SThomas Gleixner *
409cfa82a00SThomas Gleixner * If the entry comes from user space it uses the normal entry path
410cfa82a00SThomas Gleixner * including the return to user space work and preemption checks on
411cfa82a00SThomas Gleixner * exit.
412cfa82a00SThomas Gleixner *
413cfa82a00SThomas Gleixner * If hits in kernel mode then it needs to go through the paranoid
414cfa82a00SThomas Gleixner * entry as the exception can hit any random state. No preemption
415cfa82a00SThomas Gleixner * check on exit to keep the paranoid path simple.
416cfa82a00SThomas Gleixner */
417cfa82a00SThomas Gleixner.macro idtentry_mce_db vector asmsym cfunc
418cfa82a00SThomas GleixnerSYM_CODE_START(\asmsym)
419cfa82a00SThomas Gleixner	UNWIND_HINT_IRET_REGS
420cfa82a00SThomas Gleixner	ASM_CLAC
421cfa82a00SThomas Gleixner
422cfa82a00SThomas Gleixner	pushq	$-1			/* ORIG_RAX: no syscall to restart */
423cfa82a00SThomas Gleixner
424cfa82a00SThomas Gleixner	/*
425cfa82a00SThomas Gleixner	 * If the entry is from userspace, switch stacks and treat it as
426cfa82a00SThomas Gleixner	 * a normal entry.
427cfa82a00SThomas Gleixner	 */
428cfa82a00SThomas Gleixner	testb	$3, CS-ORIG_RAX(%rsp)
429cfa82a00SThomas Gleixner	jnz	.Lfrom_usermode_switch_stack_\@
430cfa82a00SThomas Gleixner
431c82965f9SChang S. Bae	/* paranoid_entry returns GS information for paranoid_exit in EBX. */
432cfa82a00SThomas Gleixner	call	paranoid_entry
433cfa82a00SThomas Gleixner
434cfa82a00SThomas Gleixner	UNWIND_HINT_REGS
435cfa82a00SThomas Gleixner
436cfa82a00SThomas Gleixner	movq	%rsp, %rdi		/* pt_regs pointer */
437cfa82a00SThomas Gleixner
438cfa82a00SThomas Gleixner	call	\cfunc
439cfa82a00SThomas Gleixner
440cfa82a00SThomas Gleixner	jmp	paranoid_exit
441cfa82a00SThomas Gleixner
442cfa82a00SThomas Gleixner	/* Switch to the regular task stack and use the noist entry point */
443cfa82a00SThomas Gleixner.Lfrom_usermode_switch_stack_\@:
444e2dcb5f1SThomas Gleixner	idtentry_body noist_\cfunc, has_error_code=0
445cfa82a00SThomas Gleixner
446cfa82a00SThomas Gleixner_ASM_NOKPROBE(\asmsym)
447cfa82a00SThomas GleixnerSYM_CODE_END(\asmsym)
448cfa82a00SThomas Gleixner.endm
449cfa82a00SThomas Gleixner
450a13644f3SJoerg Roedel#ifdef CONFIG_AMD_MEM_ENCRYPT
451a13644f3SJoerg Roedel/**
452a13644f3SJoerg Roedel * idtentry_vc - Macro to generate entry stub for #VC
453a13644f3SJoerg Roedel * @vector:		Vector number
454a13644f3SJoerg Roedel * @asmsym:		ASM symbol for the entry point
455a13644f3SJoerg Roedel * @cfunc:		C function to be called
456a13644f3SJoerg Roedel *
457a13644f3SJoerg Roedel * The macro emits code to set up the kernel context for #VC. The #VC handler
458a13644f3SJoerg Roedel * runs on an IST stack and needs to be able to cause nested #VC exceptions.
459a13644f3SJoerg Roedel *
460a13644f3SJoerg Roedel * To make this work the #VC entry code tries its best to pretend it doesn't use
461a13644f3SJoerg Roedel * an IST stack by switching to the task stack if coming from user-space (which
462a13644f3SJoerg Roedel * includes early SYSCALL entry path) or back to the stack in the IRET frame if
463a13644f3SJoerg Roedel * entered from kernel-mode.
464a13644f3SJoerg Roedel *
465a13644f3SJoerg Roedel * If entered from kernel-mode the return stack is validated first, and if it is
466a13644f3SJoerg Roedel * not safe to use (e.g. because it points to the entry stack) the #VC handler
467a13644f3SJoerg Roedel * will switch to a fall-back stack (VC2) and call a special handler function.
468a13644f3SJoerg Roedel *
469a13644f3SJoerg Roedel * The macro is only used for one vector, but it is planned to be extended in
470a13644f3SJoerg Roedel * the future for the #HV exception.
471a13644f3SJoerg Roedel */
472a13644f3SJoerg Roedel.macro idtentry_vc vector asmsym cfunc
473a13644f3SJoerg RoedelSYM_CODE_START(\asmsym)
474a13644f3SJoerg Roedel	UNWIND_HINT_IRET_REGS
475a13644f3SJoerg Roedel	ASM_CLAC
476a13644f3SJoerg Roedel
477a13644f3SJoerg Roedel	/*
478a13644f3SJoerg Roedel	 * If the entry is from userspace, switch stacks and treat it as
479a13644f3SJoerg Roedel	 * a normal entry.
480a13644f3SJoerg Roedel	 */
481a13644f3SJoerg Roedel	testb	$3, CS-ORIG_RAX(%rsp)
482a13644f3SJoerg Roedel	jnz	.Lfrom_usermode_switch_stack_\@
483a13644f3SJoerg Roedel
484a13644f3SJoerg Roedel	/*
485a13644f3SJoerg Roedel	 * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX.
486a13644f3SJoerg Roedel	 * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS
487a13644f3SJoerg Roedel	 */
488a13644f3SJoerg Roedel	call	paranoid_entry
489a13644f3SJoerg Roedel
490a13644f3SJoerg Roedel	UNWIND_HINT_REGS
491a13644f3SJoerg Roedel
492a13644f3SJoerg Roedel	/*
493a13644f3SJoerg Roedel	 * Switch off the IST stack to make it free for nested exceptions. The
494a13644f3SJoerg Roedel	 * vc_switch_off_ist() function will switch back to the interrupted
495a13644f3SJoerg Roedel	 * stack if it is safe to do so. If not it switches to the VC fall-back
496a13644f3SJoerg Roedel	 * stack.
497a13644f3SJoerg Roedel	 */
498a13644f3SJoerg Roedel	movq	%rsp, %rdi		/* pt_regs pointer */
499a13644f3SJoerg Roedel	call	vc_switch_off_ist
500a13644f3SJoerg Roedel	movq	%rax, %rsp		/* Switch to new stack */
501a13644f3SJoerg Roedel
502a13644f3SJoerg Roedel	UNWIND_HINT_REGS
503a13644f3SJoerg Roedel
504a13644f3SJoerg Roedel	/* Update pt_regs */
505a13644f3SJoerg Roedel	movq	ORIG_RAX(%rsp), %rsi	/* get error code into 2nd argument*/
506a13644f3SJoerg Roedel	movq	$-1, ORIG_RAX(%rsp)	/* no syscall to restart */
507a13644f3SJoerg Roedel
508a13644f3SJoerg Roedel	movq	%rsp, %rdi		/* pt_regs pointer */
509a13644f3SJoerg Roedel
510a13644f3SJoerg Roedel	call	\cfunc
511a13644f3SJoerg Roedel
512a13644f3SJoerg Roedel	/*
513a13644f3SJoerg Roedel	 * No need to switch back to the IST stack. The current stack is either
514a13644f3SJoerg Roedel	 * identical to the stack in the IRET frame or the VC fall-back stack,
515163b0991SIngo Molnar	 * so it is definitely mapped even with PTI enabled.
516a13644f3SJoerg Roedel	 */
517a13644f3SJoerg Roedel	jmp	paranoid_exit
518a13644f3SJoerg Roedel
519a13644f3SJoerg Roedel	/* Switch to the regular task stack */
520a13644f3SJoerg Roedel.Lfrom_usermode_switch_stack_\@:
521a13644f3SJoerg Roedel	idtentry_body safe_stack_\cfunc, has_error_code=1
522a13644f3SJoerg Roedel
523a13644f3SJoerg Roedel_ASM_NOKPROBE(\asmsym)
524a13644f3SJoerg RoedelSYM_CODE_END(\asmsym)
525a13644f3SJoerg Roedel.endm
526a13644f3SJoerg Roedel#endif
527a13644f3SJoerg Roedel
528cfa82a00SThomas Gleixner/*
529cfa82a00SThomas Gleixner * Double fault entry. Straight paranoid. No checks from which context
530cfa82a00SThomas Gleixner * this comes because for the espfix induced #DF this would do the wrong
531cfa82a00SThomas Gleixner * thing.
532cfa82a00SThomas Gleixner */
533cfa82a00SThomas Gleixner.macro idtentry_df vector asmsym cfunc
534cfa82a00SThomas GleixnerSYM_CODE_START(\asmsym)
535cfa82a00SThomas Gleixner	UNWIND_HINT_IRET_REGS offset=8
536cfa82a00SThomas Gleixner	ASM_CLAC
537cfa82a00SThomas Gleixner
538c82965f9SChang S. Bae	/* paranoid_entry returns GS information for paranoid_exit in EBX. */
539cfa82a00SThomas Gleixner	call	paranoid_entry
540cfa82a00SThomas Gleixner	UNWIND_HINT_REGS
541cfa82a00SThomas Gleixner
542cfa82a00SThomas Gleixner	movq	%rsp, %rdi		/* pt_regs pointer into first argument */
543cfa82a00SThomas Gleixner	movq	ORIG_RAX(%rsp), %rsi	/* get error code into 2nd argument*/
544cfa82a00SThomas Gleixner	movq	$-1, ORIG_RAX(%rsp)	/* no syscall to restart */
545cfa82a00SThomas Gleixner	call	\cfunc
546cfa82a00SThomas Gleixner
547cfa82a00SThomas Gleixner	jmp	paranoid_exit
548cfa82a00SThomas Gleixner
549cfa82a00SThomas Gleixner_ASM_NOKPROBE(\asmsym)
550cfa82a00SThomas GleixnerSYM_CODE_END(\asmsym)
551cfa82a00SThomas Gleixner.endm
552cfa82a00SThomas Gleixner
553905a36a2SIngo Molnar/*
55453aaf262SThomas Gleixner * Include the defines which emit the idt entries which are shared
555f0178fc0SThomas Gleixner * shared between 32 and 64 bit and emit the __irqentry_text_* markers
556f0178fc0SThomas Gleixner * so the stacktrace boundary checks work.
55753aaf262SThomas Gleixner */
558f0178fc0SThomas Gleixner	.align 16
559f0178fc0SThomas Gleixner	.globl __irqentry_text_start
560f0178fc0SThomas Gleixner__irqentry_text_start:
561f0178fc0SThomas Gleixner
56253aaf262SThomas Gleixner#include <asm/idtentry.h>
56353aaf262SThomas Gleixner
564f0178fc0SThomas Gleixner	.align 16
565f0178fc0SThomas Gleixner	.globl __irqentry_text_end
566f0178fc0SThomas Gleixner__irqentry_text_end:
567f0178fc0SThomas Gleixner
568fa5e5c40SThomas GleixnerSYM_CODE_START_LOCAL(common_interrupt_return)
56926ba4e57SJiri SlabySYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
57026c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
57126c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates user mode. */
5721e4c4f61SBorislav Petkov	testb	$3, CS(%rsp)
57326c4ef9cSAndy Lutomirski	jnz	1f
57426c4ef9cSAndy Lutomirski	ud2
57526c4ef9cSAndy Lutomirski1:
57626c4ef9cSAndy Lutomirski#endif
577502af0d7SDominik Brodowski	POP_REGS pop_rdi=0
5783e3b9293SAndy Lutomirski
5793e3b9293SAndy Lutomirski	/*
5803e3b9293SAndy Lutomirski	 * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS.
5813e3b9293SAndy Lutomirski	 * Save old stack pointer and switch to trampoline stack.
5823e3b9293SAndy Lutomirski	 */
5833e3b9293SAndy Lutomirski	movq	%rsp, %rdi
584c482feefSAndy Lutomirski	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
5851fb14363SJosh Poimboeuf	UNWIND_HINT_EMPTY
5863e3b9293SAndy Lutomirski
5873e3b9293SAndy Lutomirski	/* Copy the IRET frame to the trampoline stack. */
5883e3b9293SAndy Lutomirski	pushq	6*8(%rdi)	/* SS */
5893e3b9293SAndy Lutomirski	pushq	5*8(%rdi)	/* RSP */
5903e3b9293SAndy Lutomirski	pushq	4*8(%rdi)	/* EFLAGS */
5913e3b9293SAndy Lutomirski	pushq	3*8(%rdi)	/* CS */
5923e3b9293SAndy Lutomirski	pushq	2*8(%rdi)	/* RIP */
5933e3b9293SAndy Lutomirski
5943e3b9293SAndy Lutomirski	/* Push user RDI on the trampoline stack. */
5953e3b9293SAndy Lutomirski	pushq	(%rdi)
5963e3b9293SAndy Lutomirski
5973e3b9293SAndy Lutomirski	/*
5983e3b9293SAndy Lutomirski	 * We are on the trampoline stack.  All regs except RDI are live.
5993e3b9293SAndy Lutomirski	 * We can do future final exit work right here.
6003e3b9293SAndy Lutomirski	 */
601afaef01cSAlexander Popov	STACKLEAK_ERASE_NOCLOBBER
6023e3b9293SAndy Lutomirski
6036fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
6048a09317bSDave Hansen
6053e3b9293SAndy Lutomirski	/* Restore RDI. */
6063e3b9293SAndy Lutomirski	popq	%rdi
6073e3b9293SAndy Lutomirski	SWAPGS
60826c4ef9cSAndy Lutomirski	INTERRUPT_RETURN
60926c4ef9cSAndy Lutomirski
610905a36a2SIngo Molnar
61126ba4e57SJiri SlabySYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)
61226c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
61326c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates kernel mode. */
6141e4c4f61SBorislav Petkov	testb	$3, CS(%rsp)
61526c4ef9cSAndy Lutomirski	jz	1f
61626c4ef9cSAndy Lutomirski	ud2
61726c4ef9cSAndy Lutomirski1:
61826c4ef9cSAndy Lutomirski#endif
619502af0d7SDominik Brodowski	POP_REGS
620e872045bSAndy Lutomirski	addq	$8, %rsp	/* skip regs->orig_ax */
62110bcc80eSMathieu Desnoyers	/*
62210bcc80eSMathieu Desnoyers	 * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
62310bcc80eSMathieu Desnoyers	 * when returning from IPI handler.
62410bcc80eSMathieu Desnoyers	 */
625905a36a2SIngo Molnar	INTERRUPT_RETURN
626905a36a2SIngo Molnar
627cc66936eSJiri SlabySYM_INNER_LABEL_ALIGN(native_iret, SYM_L_GLOBAL)
6288c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
629905a36a2SIngo Molnar	/*
630905a36a2SIngo Molnar	 * Are we returning to a stack segment from the LDT?  Note: in
631905a36a2SIngo Molnar	 * 64-bit mode SS:RSP on the exception stack is always valid.
632905a36a2SIngo Molnar	 */
633905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
634905a36a2SIngo Molnar	testb	$4, (SS-RIP)(%rsp)
635905a36a2SIngo Molnar	jnz	native_irq_return_ldt
636905a36a2SIngo Molnar#endif
637905a36a2SIngo Molnar
638cc66936eSJiri SlabySYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL)
639905a36a2SIngo Molnar	/*
640905a36a2SIngo Molnar	 * This may fault.  Non-paranoid faults on return to userspace are
641905a36a2SIngo Molnar	 * handled by fixup_bad_iret.  These include #SS, #GP, and #NP.
642c29c775aSThomas Gleixner	 * Double-faults due to espfix64 are handled in exc_double_fault.
643905a36a2SIngo Molnar	 * Other faults here are fatal.
644905a36a2SIngo Molnar	 */
645905a36a2SIngo Molnar	iretq
646905a36a2SIngo Molnar
647905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
648905a36a2SIngo Molnarnative_irq_return_ldt:
64985063facSAndy Lutomirski	/*
65085063facSAndy Lutomirski	 * We are running with user GSBASE.  All GPRs contain their user
65185063facSAndy Lutomirski	 * values.  We have a percpu ESPFIX stack that is eight slots
65285063facSAndy Lutomirski	 * long (see ESPFIX_STACK_SIZE).  espfix_waddr points to the bottom
65385063facSAndy Lutomirski	 * of the ESPFIX stack.
65485063facSAndy Lutomirski	 *
65585063facSAndy Lutomirski	 * We clobber RAX and RDI in this code.  We stash RDI on the
65685063facSAndy Lutomirski	 * normal stack and RAX on the ESPFIX stack.
65785063facSAndy Lutomirski	 *
65885063facSAndy Lutomirski	 * The ESPFIX stack layout we set up looks like this:
65985063facSAndy Lutomirski	 *
66085063facSAndy Lutomirski	 * --- top of ESPFIX stack ---
66185063facSAndy Lutomirski	 * SS
66285063facSAndy Lutomirski	 * RSP
66385063facSAndy Lutomirski	 * RFLAGS
66485063facSAndy Lutomirski	 * CS
66585063facSAndy Lutomirski	 * RIP  <-- RSP points here when we're done
66685063facSAndy Lutomirski	 * RAX  <-- espfix_waddr points here
66785063facSAndy Lutomirski	 * --- bottom of ESPFIX stack ---
66885063facSAndy Lutomirski	 */
66985063facSAndy Lutomirski
67085063facSAndy Lutomirski	pushq	%rdi				/* Stash user RDI */
67153c9d924SJuergen Gross	swapgs					/* to kernel GS */
6728a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi	/* to kernel CR3 */
6738a09317bSDave Hansen
674905a36a2SIngo Molnar	movq	PER_CPU_VAR(espfix_waddr), %rdi
67585063facSAndy Lutomirski	movq	%rax, (0*8)(%rdi)		/* user RAX */
67685063facSAndy Lutomirski	movq	(1*8)(%rsp), %rax		/* user RIP */
677905a36a2SIngo Molnar	movq	%rax, (1*8)(%rdi)
67885063facSAndy Lutomirski	movq	(2*8)(%rsp), %rax		/* user CS */
679905a36a2SIngo Molnar	movq	%rax, (2*8)(%rdi)
68085063facSAndy Lutomirski	movq	(3*8)(%rsp), %rax		/* user RFLAGS */
681905a36a2SIngo Molnar	movq	%rax, (3*8)(%rdi)
68285063facSAndy Lutomirski	movq	(5*8)(%rsp), %rax		/* user SS */
683905a36a2SIngo Molnar	movq	%rax, (5*8)(%rdi)
68485063facSAndy Lutomirski	movq	(4*8)(%rsp), %rax		/* user RSP */
685905a36a2SIngo Molnar	movq	%rax, (4*8)(%rdi)
68685063facSAndy Lutomirski	/* Now RAX == RSP. */
68785063facSAndy Lutomirski
68885063facSAndy Lutomirski	andl	$0xffff0000, %eax		/* RAX = (RSP & 0xffff0000) */
68985063facSAndy Lutomirski
69085063facSAndy Lutomirski	/*
69185063facSAndy Lutomirski	 * espfix_stack[31:16] == 0.  The page tables are set up such that
69285063facSAndy Lutomirski	 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
69385063facSAndy Lutomirski	 * espfix_waddr for any X.  That is, there are 65536 RO aliases of
69485063facSAndy Lutomirski	 * the same page.  Set up RSP so that RSP[31:16] contains the
69585063facSAndy Lutomirski	 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
69685063facSAndy Lutomirski	 * still points to an RO alias of the ESPFIX stack.
69785063facSAndy Lutomirski	 */
698905a36a2SIngo Molnar	orq	PER_CPU_VAR(espfix_stack), %rax
6998a09317bSDave Hansen
7006fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
70153c9d924SJuergen Gross	swapgs					/* to user GS */
7028a09317bSDave Hansen	popq	%rdi				/* Restore user RDI */
7038a09317bSDave Hansen
704905a36a2SIngo Molnar	movq	%rax, %rsp
7058c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
70685063facSAndy Lutomirski
70785063facSAndy Lutomirski	/*
70885063facSAndy Lutomirski	 * At this point, we cannot write to the stack any more, but we can
70985063facSAndy Lutomirski	 * still read.
71085063facSAndy Lutomirski	 */
71185063facSAndy Lutomirski	popq	%rax				/* Restore user RAX */
71285063facSAndy Lutomirski
71385063facSAndy Lutomirski	/*
71485063facSAndy Lutomirski	 * RSP now points to an ordinary IRET frame, except that the page
71585063facSAndy Lutomirski	 * is read-only and RSP[31:16] are preloaded with the userspace
71685063facSAndy Lutomirski	 * values.  We can now IRET back to userspace.
71785063facSAndy Lutomirski	 */
718905a36a2SIngo Molnar	jmp	native_irq_return_iret
719905a36a2SIngo Molnar#endif
720fa5e5c40SThomas GleixnerSYM_CODE_END(common_interrupt_return)
721fa5e5c40SThomas Gleixner_ASM_NOKPROBE(common_interrupt_return)
722905a36a2SIngo Molnar
723905a36a2SIngo Molnar/*
7244d732138SIngo Molnar * Reload gs selector with exception handling
7254d732138SIngo Molnar * edi:  new selector
726b9f6976bSThomas Gleixner *
727b9f6976bSThomas Gleixner * Is in entry.text as it shouldn't be instrumented.
7284d732138SIngo Molnar */
729410367e3SThomas GleixnerSYM_FUNC_START(asm_load_gs_index)
7308c1f7558SJosh Poimboeuf	FRAME_BEGIN
731c9317202SThomas Gleixner	swapgs
73242c748bbSBorislav Petkov.Lgs_change:
733905a36a2SIngo Molnar	movl	%edi, %gs
73496e5d28aSBorislav Petkov2:	ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
735c9317202SThomas Gleixner	swapgs
7368c1f7558SJosh Poimboeuf	FRAME_END
737905a36a2SIngo Molnar	ret
738410367e3SThomas GleixnerSYM_FUNC_END(asm_load_gs_index)
739410367e3SThomas GleixnerEXPORT_SYMBOL(asm_load_gs_index)
740905a36a2SIngo Molnar
74198ededb6SJiri Slaby	_ASM_EXTABLE(.Lgs_change, .Lbad_gs)
742905a36a2SIngo Molnar	.section .fixup, "ax"
743905a36a2SIngo Molnar	/* running with kernelgs */
744ef77e688SJiri SlabySYM_CODE_START_LOCAL_NOALIGN(.Lbad_gs)
745c9317202SThomas Gleixner	swapgs					/* switch back to user gs */
746b038c842SAndy Lutomirski.macro ZAP_GS
747b038c842SAndy Lutomirski	/* This can't be a string because the preprocessor needs to see it. */
748b038c842SAndy Lutomirski	movl $__USER_DS, %eax
749b038c842SAndy Lutomirski	movl %eax, %gs
750b038c842SAndy Lutomirski.endm
751b038c842SAndy Lutomirski	ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
752905a36a2SIngo Molnar	xorl	%eax, %eax
753905a36a2SIngo Molnar	movl	%eax, %gs
754905a36a2SIngo Molnar	jmp	2b
755ef77e688SJiri SlabySYM_CODE_END(.Lbad_gs)
756905a36a2SIngo Molnar	.previous
757905a36a2SIngo Molnar
75828c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV
759905a36a2SIngo Molnar/*
760905a36a2SIngo Molnar * A note on the "critical region" in our callback handler.
761905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring
762905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled
763905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before
764905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still
765905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack.
766905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd
767905a36a2SIngo Molnar * like to avoid the possibility.
768905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an
769905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current
770905a36a2SIngo Molnar * activation and restart the handler using the previous one.
7712f6474e4SThomas Gleixner *
7722f6474e4SThomas Gleixner * C calling convention: exc_xen_hypervisor_callback(struct *pt_regs)
773905a36a2SIngo Molnar */
7742f6474e4SThomas GleixnerSYM_CODE_START_LOCAL(exc_xen_hypervisor_callback)
7754d732138SIngo Molnar
776905a36a2SIngo Molnar/*
777905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
778905a36a2SIngo Molnar * see the correct pointer to the pt_regs
779905a36a2SIngo Molnar */
7808c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
7814d732138SIngo Molnar	movq	%rdi, %rsp			/* we don't return, adjust the stack frame */
7828c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
7831d3e53e8SAndy Lutomirski
7842f6474e4SThomas Gleixner	call	xen_pv_evtchn_do_upcall
7851d3e53e8SAndy Lutomirski
7862f6474e4SThomas Gleixner	jmp	error_return
7872f6474e4SThomas GleixnerSYM_CODE_END(exc_xen_hypervisor_callback)
788905a36a2SIngo Molnar
789905a36a2SIngo Molnar/*
790905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes.
791905a36a2SIngo Molnar * We get here for two reasons:
792905a36a2SIngo Molnar *  1. Fault while reloading DS, ES, FS or GS
793905a36a2SIngo Molnar *  2. Fault while executing IRET
794905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment
795905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others.
796905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the
797905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall
798905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
799905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register
800905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1.
801905a36a2SIngo Molnar */
802bc7b11c0SJiri SlabySYM_CODE_START(xen_failsafe_callback)
8038c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
804905a36a2SIngo Molnar	movl	%ds, %ecx
805905a36a2SIngo Molnar	cmpw	%cx, 0x10(%rsp)
806905a36a2SIngo Molnar	jne	1f
807905a36a2SIngo Molnar	movl	%es, %ecx
808905a36a2SIngo Molnar	cmpw	%cx, 0x18(%rsp)
809905a36a2SIngo Molnar	jne	1f
810905a36a2SIngo Molnar	movl	%fs, %ecx
811905a36a2SIngo Molnar	cmpw	%cx, 0x20(%rsp)
812905a36a2SIngo Molnar	jne	1f
813905a36a2SIngo Molnar	movl	%gs, %ecx
814905a36a2SIngo Molnar	cmpw	%cx, 0x28(%rsp)
815905a36a2SIngo Molnar	jne	1f
816905a36a2SIngo Molnar	/* All segments match their saved values => Category 2 (Bad IRET). */
817905a36a2SIngo Molnar	movq	(%rsp), %rcx
818905a36a2SIngo Molnar	movq	8(%rsp), %r11
819905a36a2SIngo Molnar	addq	$0x30, %rsp
820905a36a2SIngo Molnar	pushq	$0				/* RIP */
8218c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
822be4c11afSThomas Gleixner	jmp	asm_exc_general_protection
823905a36a2SIngo Molnar1:	/* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
824905a36a2SIngo Molnar	movq	(%rsp), %rcx
825905a36a2SIngo Molnar	movq	8(%rsp), %r11
826905a36a2SIngo Molnar	addq	$0x30, %rsp
8278c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
828905a36a2SIngo Molnar	pushq	$-1 /* orig_ax = -1 => not a system call */
8293f01daecSDominik Brodowski	PUSH_AND_CLEAR_REGS
830946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
831e88d9741SThomas Gleixner	jmp	error_return
832bc7b11c0SJiri SlabySYM_CODE_END(xen_failsafe_callback)
83328c11b0fSJuergen Gross#endif /* CONFIG_XEN_PV */
834905a36a2SIngo Molnar
835905a36a2SIngo Molnar/*
836c82965f9SChang S. Bae * Save all registers in pt_regs. Return GSBASE related information
837c82965f9SChang S. Bae * in EBX depending on the availability of the FSGSBASE instructions:
838c82965f9SChang S. Bae *
839c82965f9SChang S. Bae * FSGSBASE	R/EBX
840c82965f9SChang S. Bae *     N        0 -> SWAPGS on exit
841c82965f9SChang S. Bae *              1 -> no SWAPGS on exit
842c82965f9SChang S. Bae *
843c82965f9SChang S. Bae *     Y        GSBASE value at entry, must be restored in paranoid_exit
844905a36a2SIngo Molnar */
845ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_entry)
8468c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
847905a36a2SIngo Molnar	cld
8489e809d15SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
8499e809d15SDominik Brodowski	ENCODE_FRAME_POINTER 8
8508a09317bSDave Hansen
85116561f27SDave Hansen	/*
85216561f27SDave Hansen	 * Always stash CR3 in %r14.  This value will be restored,
853ae852495SAndy Lutomirski	 * verbatim, at exit.  Needed if paranoid_entry interrupted
854ae852495SAndy Lutomirski	 * another entry that already switched to the user CR3 value
855ae852495SAndy Lutomirski	 * but has not yet returned to userspace.
85616561f27SDave Hansen	 *
85716561f27SDave Hansen	 * This is also why CS (stashed in the "iret frame" by the
85816561f27SDave Hansen	 * hardware at entry) can not be used: this may be a return
859ae852495SAndy Lutomirski	 * to kernel code, but with a user CR3 value.
86096b23714SChang S. Bae	 *
86196b23714SChang S. Bae	 * Switching CR3 does not depend on kernel GSBASE so it can
86296b23714SChang S. Bae	 * be done before switching to the kernel GSBASE. This is
86396b23714SChang S. Bae	 * required for FSGSBASE because the kernel GSBASE has to
86496b23714SChang S. Bae	 * be retrieved from a kernel internal table.
86516561f27SDave Hansen	 */
8668a09317bSDave Hansen	SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
8678a09317bSDave Hansen
86818ec54fdSJosh Poimboeuf	/*
869c82965f9SChang S. Bae	 * Handling GSBASE depends on the availability of FSGSBASE.
870c82965f9SChang S. Bae	 *
871c82965f9SChang S. Bae	 * Without FSGSBASE the kernel enforces that negative GSBASE
872c82965f9SChang S. Bae	 * values indicate kernel GSBASE. With FSGSBASE no assumptions
873c82965f9SChang S. Bae	 * can be made about the GSBASE value when entering from user
874c82965f9SChang S. Bae	 * space.
875c82965f9SChang S. Bae	 */
876c82965f9SChang S. Bae	ALTERNATIVE "jmp .Lparanoid_entry_checkgs", "", X86_FEATURE_FSGSBASE
877c82965f9SChang S. Bae
878c82965f9SChang S. Bae	/*
879c82965f9SChang S. Bae	 * Read the current GSBASE and store it in %rbx unconditionally,
880c82965f9SChang S. Bae	 * retrieve and set the current CPUs kernel GSBASE. The stored value
881c82965f9SChang S. Bae	 * has to be restored in paranoid_exit unconditionally.
882c82965f9SChang S. Bae	 *
8830b2c605fSBorislav Petkov	 * The unconditional write to GS base below ensures that no subsequent
8840b2c605fSBorislav Petkov	 * loads based on a mispredicted GS base can happen, therefore no LFENCE
8850b2c605fSBorislav Petkov	 * is needed here.
886c82965f9SChang S. Bae	 */
887c82965f9SChang S. Bae	SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx
888c82965f9SChang S. Bae	ret
889c82965f9SChang S. Bae
890c82965f9SChang S. Bae.Lparanoid_entry_checkgs:
89196b23714SChang S. Bae	/* EBX = 1 -> kernel GSBASE active, no restore required */
89296b23714SChang S. Bae	movl	$1, %ebx
89396b23714SChang S. Bae	/*
89496b23714SChang S. Bae	 * The kernel-enforced convention is a negative GSBASE indicates
89596b23714SChang S. Bae	 * a kernel value. No SWAPGS needed on entry and exit.
89696b23714SChang S. Bae	 */
89796b23714SChang S. Bae	movl	$MSR_GS_BASE, %ecx
89896b23714SChang S. Bae	rdmsr
89996b23714SChang S. Bae	testl	%edx, %edx
90096b23714SChang S. Bae	jns	.Lparanoid_entry_swapgs
90196b23714SChang S. Bae	ret
90296b23714SChang S. Bae
90396b23714SChang S. Bae.Lparanoid_entry_swapgs:
90453c9d924SJuergen Gross	swapgs
90596b23714SChang S. Bae
90618ec54fdSJosh Poimboeuf	/*
90718ec54fdSJosh Poimboeuf	 * The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an
90818ec54fdSJosh Poimboeuf	 * unconditional CR3 write, even in the PTI case.  So do an lfence
90918ec54fdSJosh Poimboeuf	 * to prevent GS speculation, regardless of whether PTI is enabled.
91018ec54fdSJosh Poimboeuf	 */
91118ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
91218ec54fdSJosh Poimboeuf
91396b23714SChang S. Bae	/* EBX = 0 -> SWAPGS required on exit */
91496b23714SChang S. Bae	xorl	%ebx, %ebx
9158a09317bSDave Hansen	ret
916ef1e0315SJiri SlabySYM_CODE_END(paranoid_entry)
917905a36a2SIngo Molnar
918905a36a2SIngo Molnar/*
919905a36a2SIngo Molnar * "Paranoid" exit path from exception stack.  This is invoked
920905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came
921905a36a2SIngo Molnar * from kernel space.
922905a36a2SIngo Molnar *
923905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early
924905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would
925c82965f9SChang S. Bae * be complicated.  Fortunately, there's no good reason to try
926c82965f9SChang S. Bae * to handle preemption here.
9274d732138SIngo Molnar *
928c82965f9SChang S. Bae * R/EBX contains the GSBASE related information depending on the
929c82965f9SChang S. Bae * availability of the FSGSBASE instructions:
930c82965f9SChang S. Bae *
931c82965f9SChang S. Bae * FSGSBASE	R/EBX
932c82965f9SChang S. Bae *     N        0 -> SWAPGS on exit
933c82965f9SChang S. Bae *              1 -> no SWAPGS on exit
934c82965f9SChang S. Bae *
935c82965f9SChang S. Bae *     Y        User space GSBASE, must be restored unconditionally
936905a36a2SIngo Molnar */
937ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_exit)
9388c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
939c82965f9SChang S. Bae	/*
940c82965f9SChang S. Bae	 * The order of operations is important. RESTORE_CR3 requires
941c82965f9SChang S. Bae	 * kernel GSBASE.
942c82965f9SChang S. Bae	 *
943c82965f9SChang S. Bae	 * NB to anyone to try to optimize this code: this code does
944c82965f9SChang S. Bae	 * not execute at all for exceptions from user mode. Those
945c82965f9SChang S. Bae	 * exceptions go through error_exit instead.
946c82965f9SChang S. Bae	 */
947c82965f9SChang S. Bae	RESTORE_CR3	scratch_reg=%rax save_reg=%r14
948c82965f9SChang S. Bae
949c82965f9SChang S. Bae	/* Handle the three GSBASE cases */
950c82965f9SChang S. Bae	ALTERNATIVE "jmp .Lparanoid_exit_checkgs", "", X86_FEATURE_FSGSBASE
951c82965f9SChang S. Bae
952c82965f9SChang S. Bae	/* With FSGSBASE enabled, unconditionally restore GSBASE */
953c82965f9SChang S. Bae	wrgsbase	%rbx
95445c08383SThomas Gleixner	jmp		restore_regs_and_return_to_kernel
955c82965f9SChang S. Bae
956c82965f9SChang S. Bae.Lparanoid_exit_checkgs:
957c82965f9SChang S. Bae	/* On non-FSGSBASE systems, conditionally do SWAPGS */
958c82965f9SChang S. Bae	testl		%ebx, %ebx
959c82965f9SChang S. Bae	jnz		restore_regs_and_return_to_kernel
960c82965f9SChang S. Bae
961c82965f9SChang S. Bae	/* We are returning to a context with user GSBASE */
96253c9d924SJuergen Gross	swapgs
963e5317832SAndy Lutomirski	jmp		restore_regs_and_return_to_kernel
964ef1e0315SJiri SlabySYM_CODE_END(paranoid_exit)
965905a36a2SIngo Molnar
966905a36a2SIngo Molnar/*
9679e809d15SDominik Brodowski * Save all registers in pt_regs, and switch GS if needed.
968905a36a2SIngo Molnar */
969ef1e0315SJiri SlabySYM_CODE_START_LOCAL(error_entry)
9709e809d15SDominik Brodowski	UNWIND_HINT_FUNC
971905a36a2SIngo Molnar	cld
9729e809d15SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
9739e809d15SDominik Brodowski	ENCODE_FRAME_POINTER 8
974905a36a2SIngo Molnar	testb	$3, CS+8(%rsp)
975cb6f64edSAndy Lutomirski	jz	.Lerror_kernelspace
976539f5113SAndy Lutomirski
977cb6f64edSAndy Lutomirski	/*
978cb6f64edSAndy Lutomirski	 * We entered from user mode or we're pretending to have entered
979cb6f64edSAndy Lutomirski	 * from user mode due to an IRET fault.
980cb6f64edSAndy Lutomirski	 */
981905a36a2SIngo Molnar	SWAPGS
98218ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
9838a09317bSDave Hansen	/* We have user CR3.  Change to kernel CR3. */
9848a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
985539f5113SAndy Lutomirski
986cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_after_swapgs:
9877f2590a1SAndy Lutomirski	/* Put us onto the real thread stack. */
9887f2590a1SAndy Lutomirski	popq	%r12				/* save return addr in %12 */
9897f2590a1SAndy Lutomirski	movq	%rsp, %rdi			/* arg0 = pt_regs pointer */
9907f2590a1SAndy Lutomirski	call	sync_regs
9917f2590a1SAndy Lutomirski	movq	%rax, %rsp			/* switch stack */
9927f2590a1SAndy Lutomirski	ENCODE_FRAME_POINTER
9937f2590a1SAndy Lutomirski	pushq	%r12
994f1075053SAndy Lutomirski	ret
99502bc7768SAndy Lutomirski
99618ec54fdSJosh Poimboeuf.Lerror_entry_done_lfence:
99718ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
998cb6f64edSAndy Lutomirski.Lerror_entry_done:
999905a36a2SIngo Molnar	ret
1000905a36a2SIngo Molnar
1001905a36a2SIngo Molnar	/*
1002905a36a2SIngo Molnar	 * There are two places in the kernel that can potentially fault with
1003905a36a2SIngo Molnar	 * usergs. Handle them here.  B stepping K8s sometimes report a
1004905a36a2SIngo Molnar	 * truncated RIP for IRET exceptions returning to compat mode. Check
1005905a36a2SIngo Molnar	 * for these here too.
1006905a36a2SIngo Molnar	 */
1007cb6f64edSAndy Lutomirski.Lerror_kernelspace:
1008905a36a2SIngo Molnar	leaq	native_irq_return_iret(%rip), %rcx
1009905a36a2SIngo Molnar	cmpq	%rcx, RIP+8(%rsp)
1010cb6f64edSAndy Lutomirski	je	.Lerror_bad_iret
1011905a36a2SIngo Molnar	movl	%ecx, %eax			/* zero extend */
1012905a36a2SIngo Molnar	cmpq	%rax, RIP+8(%rsp)
1013cb6f64edSAndy Lutomirski	je	.Lbstep_iret
101442c748bbSBorislav Petkov	cmpq	$.Lgs_change, RIP+8(%rsp)
101518ec54fdSJosh Poimboeuf	jne	.Lerror_entry_done_lfence
1016539f5113SAndy Lutomirski
1017539f5113SAndy Lutomirski	/*
101842c748bbSBorislav Petkov	 * hack: .Lgs_change can fail with user gsbase.  If this happens, fix up
1019539f5113SAndy Lutomirski	 * gsbase and proceed.  We'll fix up the exception and land in
102042c748bbSBorislav Petkov	 * .Lgs_change's error handler with kernel gsbase.
1021539f5113SAndy Lutomirski	 */
10222fa5f04fSWanpeng Li	SWAPGS
102318ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
10242fa5f04fSWanpeng Li	jmp .Lerror_entry_done
1025905a36a2SIngo Molnar
1026cb6f64edSAndy Lutomirski.Lbstep_iret:
1027905a36a2SIngo Molnar	/* Fix truncated RIP */
1028905a36a2SIngo Molnar	movq	%rcx, RIP+8(%rsp)
1029905a36a2SIngo Molnar	/* fall through */
1030905a36a2SIngo Molnar
1031cb6f64edSAndy Lutomirski.Lerror_bad_iret:
1032539f5113SAndy Lutomirski	/*
10338a09317bSDave Hansen	 * We came from an IRET to user mode, so we have user
10348a09317bSDave Hansen	 * gsbase and CR3.  Switch to kernel gsbase and CR3:
1035539f5113SAndy Lutomirski	 */
1036905a36a2SIngo Molnar	SWAPGS
103718ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
10388a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
1039539f5113SAndy Lutomirski
1040539f5113SAndy Lutomirski	/*
1041539f5113SAndy Lutomirski	 * Pretend that the exception came from user mode: set up pt_regs
1042b3681dd5SAndy Lutomirski	 * as if we faulted immediately after IRET.
1043539f5113SAndy Lutomirski	 */
1044905a36a2SIngo Molnar	mov	%rsp, %rdi
1045905a36a2SIngo Molnar	call	fixup_bad_iret
1046905a36a2SIngo Molnar	mov	%rax, %rsp
1047cb6f64edSAndy Lutomirski	jmp	.Lerror_entry_from_usermode_after_swapgs
1048ef1e0315SJiri SlabySYM_CODE_END(error_entry)
1049905a36a2SIngo Molnar
1050424c7d0aSThomas GleixnerSYM_CODE_START_LOCAL(error_return)
1051424c7d0aSThomas Gleixner	UNWIND_HINT_REGS
1052424c7d0aSThomas Gleixner	DEBUG_ENTRY_ASSERT_IRQS_OFF
1053424c7d0aSThomas Gleixner	testb	$3, CS(%rsp)
1054424c7d0aSThomas Gleixner	jz	restore_regs_and_return_to_kernel
1055424c7d0aSThomas Gleixner	jmp	swapgs_restore_regs_and_return_to_usermode
1056424c7d0aSThomas GleixnerSYM_CODE_END(error_return)
1057424c7d0aSThomas Gleixner
1058929bacecSAndy Lutomirski/*
1059929bacecSAndy Lutomirski * Runs on exception stack.  Xen PV does not go through this path at all,
1060929bacecSAndy Lutomirski * so we can use real assembly here.
10618a09317bSDave Hansen *
10628a09317bSDave Hansen * Registers:
10638a09317bSDave Hansen *	%r14: Used to save/restore the CR3 of the interrupted context
10648a09317bSDave Hansen *	      when PAGE_TABLE_ISOLATION is in use.  Do not clobber.
1065929bacecSAndy Lutomirski */
10666271fef0SThomas GleixnerSYM_CODE_START(asm_exc_nmi)
10678c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1068929bacecSAndy Lutomirski
1069fc57a7c6SAndy Lutomirski	/*
1070905a36a2SIngo Molnar	 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1071905a36a2SIngo Molnar	 * the iretq it performs will take us out of NMI context.
1072905a36a2SIngo Molnar	 * This means that we can have nested NMIs where the next
1073905a36a2SIngo Molnar	 * NMI is using the top of the stack of the previous NMI. We
1074905a36a2SIngo Molnar	 * can't let it execute because the nested NMI will corrupt the
1075905a36a2SIngo Molnar	 * stack of the previous NMI. NMI handlers are not re-entrant
1076905a36a2SIngo Molnar	 * anyway.
1077905a36a2SIngo Molnar	 *
1078905a36a2SIngo Molnar	 * To handle this case we do the following:
1079905a36a2SIngo Molnar	 *  Check the a special location on the stack that contains
1080905a36a2SIngo Molnar	 *  a variable that is set when NMIs are executing.
1081905a36a2SIngo Molnar	 *  The interrupted task's stack is also checked to see if it
1082905a36a2SIngo Molnar	 *  is an NMI stack.
1083905a36a2SIngo Molnar	 *  If the variable is not set and the stack is not the NMI
1084905a36a2SIngo Molnar	 *  stack then:
1085905a36a2SIngo Molnar	 *    o Set the special variable on the stack
10860b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "outermost" location on the
10870b22930eSAndy Lutomirski	 *      stack
10880b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "iret" location on the stack
1089905a36a2SIngo Molnar	 *    o Continue processing the NMI
1090905a36a2SIngo Molnar	 *  If the variable is set or the previous stack is the NMI stack:
10910b22930eSAndy Lutomirski	 *    o Modify the "iret" location to jump to the repeat_nmi
1092905a36a2SIngo Molnar	 *    o return back to the first NMI
1093905a36a2SIngo Molnar	 *
1094905a36a2SIngo Molnar	 * Now on exit of the first NMI, we first clear the stack variable
1095905a36a2SIngo Molnar	 * The NMI stack will tell any nested NMIs at that point that it is
1096905a36a2SIngo Molnar	 * nested. Then we pop the stack normally with iret, and if there was
1097905a36a2SIngo Molnar	 * a nested NMI that updated the copy interrupt stack frame, a
1098905a36a2SIngo Molnar	 * jump will be made to the repeat_nmi code that will handle the second
1099905a36a2SIngo Molnar	 * NMI.
11009b6e6a83SAndy Lutomirski	 *
11019b6e6a83SAndy Lutomirski	 * However, espfix prevents us from directly returning to userspace
11029b6e6a83SAndy Lutomirski	 * with a single IRET instruction.  Similarly, IRET to user mode
11039b6e6a83SAndy Lutomirski	 * can fault.  We therefore handle NMIs from user space like
11049b6e6a83SAndy Lutomirski	 * other IST entries.
1105905a36a2SIngo Molnar	 */
1106905a36a2SIngo Molnar
1107e93c1730SAndy Lutomirski	ASM_CLAC
1108e93c1730SAndy Lutomirski
1109905a36a2SIngo Molnar	/* Use %rdx as our temp variable throughout */
1110905a36a2SIngo Molnar	pushq	%rdx
1111905a36a2SIngo Molnar
11129b6e6a83SAndy Lutomirski	testb	$3, CS-RIP+8(%rsp)
11139b6e6a83SAndy Lutomirski	jz	.Lnmi_from_kernel
1114905a36a2SIngo Molnar
1115905a36a2SIngo Molnar	/*
11169b6e6a83SAndy Lutomirski	 * NMI from user mode.  We need to run on the thread stack, but we
11179b6e6a83SAndy Lutomirski	 * can't go through the normal entry paths: NMIs are masked, and
11189b6e6a83SAndy Lutomirski	 * we don't want to enable interrupts, because then we'll end
11199b6e6a83SAndy Lutomirski	 * up in an awkward situation in which IRQs are on but NMIs
11209b6e6a83SAndy Lutomirski	 * are off.
112183c133cfSAndy Lutomirski	 *
112283c133cfSAndy Lutomirski	 * We also must not push anything to the stack before switching
112383c133cfSAndy Lutomirski	 * stacks lest we corrupt the "NMI executing" variable.
11249b6e6a83SAndy Lutomirski	 */
11259b6e6a83SAndy Lutomirski
1126929bacecSAndy Lutomirski	swapgs
11279b6e6a83SAndy Lutomirski	cld
112818ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
11298a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
11309b6e6a83SAndy Lutomirski	movq	%rsp, %rdx
11319b6e6a83SAndy Lutomirski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
11328c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS base=%rdx offset=8
11339b6e6a83SAndy Lutomirski	pushq	5*8(%rdx)	/* pt_regs->ss */
11349b6e6a83SAndy Lutomirski	pushq	4*8(%rdx)	/* pt_regs->rsp */
11359b6e6a83SAndy Lutomirski	pushq	3*8(%rdx)	/* pt_regs->flags */
11369b6e6a83SAndy Lutomirski	pushq	2*8(%rdx)	/* pt_regs->cs */
11379b6e6a83SAndy Lutomirski	pushq	1*8(%rdx)	/* pt_regs->rip */
11388c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
11399b6e6a83SAndy Lutomirski	pushq   $-1		/* pt_regs->orig_ax */
114030907fd1SDominik Brodowski	PUSH_AND_CLEAR_REGS rdx=(%rdx)
1141946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
11429b6e6a83SAndy Lutomirski
11439b6e6a83SAndy Lutomirski	/*
11449b6e6a83SAndy Lutomirski	 * At this point we no longer need to worry about stack damage
11459b6e6a83SAndy Lutomirski	 * due to nesting -- we're on the normal thread stack and we're
11469b6e6a83SAndy Lutomirski	 * done with the NMI stack.
11479b6e6a83SAndy Lutomirski	 */
11489b6e6a83SAndy Lutomirski
11499b6e6a83SAndy Lutomirski	movq	%rsp, %rdi
11509b6e6a83SAndy Lutomirski	movq	$-1, %rsi
11516271fef0SThomas Gleixner	call	exc_nmi
11529b6e6a83SAndy Lutomirski
11539b6e6a83SAndy Lutomirski	/*
11549b6e6a83SAndy Lutomirski	 * Return back to user mode.  We must *not* do the normal exit
1155946c1911SJosh Poimboeuf	 * work, because we don't want to enable interrupts.
11569b6e6a83SAndy Lutomirski	 */
11578a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
11589b6e6a83SAndy Lutomirski
11599b6e6a83SAndy Lutomirski.Lnmi_from_kernel:
11609b6e6a83SAndy Lutomirski	/*
11610b22930eSAndy Lutomirski	 * Here's what our stack frame will look like:
11620b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
11630b22930eSAndy Lutomirski	 * | original SS                                             |
11640b22930eSAndy Lutomirski	 * | original Return RSP                                     |
11650b22930eSAndy Lutomirski	 * | original RFLAGS                                         |
11660b22930eSAndy Lutomirski	 * | original CS                                             |
11670b22930eSAndy Lutomirski	 * | original RIP                                            |
11680b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
11690b22930eSAndy Lutomirski	 * | temp storage for rdx                                    |
11700b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
11710b22930eSAndy Lutomirski	 * | "NMI executing" variable                                |
11720b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
11730b22930eSAndy Lutomirski	 * | iret SS          } Copied from "outermost" frame        |
11740b22930eSAndy Lutomirski	 * | iret Return RSP  } on each loop iteration; overwritten  |
11750b22930eSAndy Lutomirski	 * | iret RFLAGS      } by a nested NMI to force another     |
11760b22930eSAndy Lutomirski	 * | iret CS          } iteration if needed.                 |
11770b22930eSAndy Lutomirski	 * | iret RIP         }                                      |
11780b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
11790b22930eSAndy Lutomirski	 * | outermost SS          } initialized in first_nmi;       |
11800b22930eSAndy Lutomirski	 * | outermost Return RSP  } will not be changed before      |
11810b22930eSAndy Lutomirski	 * | outermost RFLAGS      } NMI processing is done.         |
11820b22930eSAndy Lutomirski	 * | outermost CS          } Copied to "iret" frame on each  |
11830b22930eSAndy Lutomirski	 * | outermost RIP         } iteration.                      |
11840b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
11850b22930eSAndy Lutomirski	 * | pt_regs                                                 |
11860b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
11870b22930eSAndy Lutomirski	 *
11880b22930eSAndy Lutomirski	 * The "original" frame is used by hardware.  Before re-enabling
11890b22930eSAndy Lutomirski	 * NMIs, we need to be done with it, and we need to leave enough
11900b22930eSAndy Lutomirski	 * space for the asm code here.
11910b22930eSAndy Lutomirski	 *
11920b22930eSAndy Lutomirski	 * We return by executing IRET while RSP points to the "iret" frame.
11930b22930eSAndy Lutomirski	 * That will either return for real or it will loop back into NMI
11940b22930eSAndy Lutomirski	 * processing.
11950b22930eSAndy Lutomirski	 *
11960b22930eSAndy Lutomirski	 * The "outermost" frame is copied to the "iret" frame on each
11970b22930eSAndy Lutomirski	 * iteration of the loop, so each iteration starts with the "iret"
11980b22930eSAndy Lutomirski	 * frame pointing to the final return target.
11990b22930eSAndy Lutomirski	 */
12000b22930eSAndy Lutomirski
12010b22930eSAndy Lutomirski	/*
12020b22930eSAndy Lutomirski	 * Determine whether we're a nested NMI.
12030b22930eSAndy Lutomirski	 *
1204a27507caSAndy Lutomirski	 * If we interrupted kernel code between repeat_nmi and
1205a27507caSAndy Lutomirski	 * end_repeat_nmi, then we are a nested NMI.  We must not
1206a27507caSAndy Lutomirski	 * modify the "iret" frame because it's being written by
1207a27507caSAndy Lutomirski	 * the outer NMI.  That's okay; the outer NMI handler is
12086271fef0SThomas Gleixner	 * about to about to call exc_nmi() anyway, so we can just
1209a27507caSAndy Lutomirski	 * resume the outer NMI.
1210a27507caSAndy Lutomirski	 */
1211a27507caSAndy Lutomirski
1212a27507caSAndy Lutomirski	movq	$repeat_nmi, %rdx
1213a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1214a27507caSAndy Lutomirski	ja	1f
1215a27507caSAndy Lutomirski	movq	$end_repeat_nmi, %rdx
1216a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1217a27507caSAndy Lutomirski	ja	nested_nmi_out
1218a27507caSAndy Lutomirski1:
1219a27507caSAndy Lutomirski
1220a27507caSAndy Lutomirski	/*
1221a27507caSAndy Lutomirski	 * Now check "NMI executing".  If it's set, then we're nested.
12220b22930eSAndy Lutomirski	 * This will not detect if we interrupted an outer NMI just
12230b22930eSAndy Lutomirski	 * before IRET.
1224905a36a2SIngo Molnar	 */
1225905a36a2SIngo Molnar	cmpl	$1, -8(%rsp)
1226905a36a2SIngo Molnar	je	nested_nmi
1227905a36a2SIngo Molnar
1228905a36a2SIngo Molnar	/*
12290b22930eSAndy Lutomirski	 * Now test if the previous stack was an NMI stack.  This covers
12300b22930eSAndy Lutomirski	 * the case where we interrupt an outer NMI after it clears
1231810bc075SAndy Lutomirski	 * "NMI executing" but before IRET.  We need to be careful, though:
1232810bc075SAndy Lutomirski	 * there is one case in which RSP could point to the NMI stack
1233810bc075SAndy Lutomirski	 * despite there being no NMI active: naughty userspace controls
1234810bc075SAndy Lutomirski	 * RSP at the very beginning of the SYSCALL targets.  We can
1235810bc075SAndy Lutomirski	 * pull a fast one on naughty userspace, though: we program
1236810bc075SAndy Lutomirski	 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1237810bc075SAndy Lutomirski	 * if it controls the kernel's RSP.  We set DF before we clear
1238810bc075SAndy Lutomirski	 * "NMI executing".
1239905a36a2SIngo Molnar	 */
1240905a36a2SIngo Molnar	lea	6*8(%rsp), %rdx
1241905a36a2SIngo Molnar	/* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1242905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1243905a36a2SIngo Molnar	/* If the stack pointer is above the NMI stack, this is a normal NMI */
1244905a36a2SIngo Molnar	ja	first_nmi
12454d732138SIngo Molnar
1246905a36a2SIngo Molnar	subq	$EXCEPTION_STKSZ, %rdx
1247905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1248905a36a2SIngo Molnar	/* If it is below the NMI stack, it is a normal NMI */
1249905a36a2SIngo Molnar	jb	first_nmi
1250810bc075SAndy Lutomirski
1251810bc075SAndy Lutomirski	/* Ah, it is within the NMI stack. */
1252810bc075SAndy Lutomirski
1253810bc075SAndy Lutomirski	testb	$(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1254810bc075SAndy Lutomirski	jz	first_nmi	/* RSP was user controlled. */
1255810bc075SAndy Lutomirski
1256810bc075SAndy Lutomirski	/* This is a nested NMI. */
1257905a36a2SIngo Molnar
1258905a36a2SIngo Molnarnested_nmi:
1259905a36a2SIngo Molnar	/*
12600b22930eSAndy Lutomirski	 * Modify the "iret" frame to point to repeat_nmi, forcing another
12610b22930eSAndy Lutomirski	 * iteration of NMI handling.
1262905a36a2SIngo Molnar	 */
126323a781e9SAndy Lutomirski	subq	$8, %rsp
1264905a36a2SIngo Molnar	leaq	-10*8(%rsp), %rdx
1265905a36a2SIngo Molnar	pushq	$__KERNEL_DS
1266905a36a2SIngo Molnar	pushq	%rdx
1267905a36a2SIngo Molnar	pushfq
1268905a36a2SIngo Molnar	pushq	$__KERNEL_CS
1269905a36a2SIngo Molnar	pushq	$repeat_nmi
1270905a36a2SIngo Molnar
1271905a36a2SIngo Molnar	/* Put stack back */
1272905a36a2SIngo Molnar	addq	$(6*8), %rsp
1273905a36a2SIngo Molnar
1274905a36a2SIngo Molnarnested_nmi_out:
1275905a36a2SIngo Molnar	popq	%rdx
1276905a36a2SIngo Molnar
12770b22930eSAndy Lutomirski	/* We are returning to kernel mode, so this cannot result in a fault. */
1278929bacecSAndy Lutomirski	iretq
1279905a36a2SIngo Molnar
1280905a36a2SIngo Molnarfirst_nmi:
12810b22930eSAndy Lutomirski	/* Restore rdx. */
1282905a36a2SIngo Molnar	movq	(%rsp), %rdx
1283905a36a2SIngo Molnar
128436f1a77bSAndy Lutomirski	/* Make room for "NMI executing". */
128536f1a77bSAndy Lutomirski	pushq	$0
1286905a36a2SIngo Molnar
12870b22930eSAndy Lutomirski	/* Leave room for the "iret" frame */
1288905a36a2SIngo Molnar	subq	$(5*8), %rsp
1289905a36a2SIngo Molnar
12900b22930eSAndy Lutomirski	/* Copy the "original" frame to the "outermost" frame */
1291905a36a2SIngo Molnar	.rept 5
1292905a36a2SIngo Molnar	pushq	11*8(%rsp)
1293905a36a2SIngo Molnar	.endr
12948c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1295905a36a2SIngo Molnar
1296905a36a2SIngo Molnar	/* Everything up to here is safe from nested NMIs */
1297905a36a2SIngo Molnar
1298a97439aaSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
1299a97439aaSAndy Lutomirski	/*
1300a97439aaSAndy Lutomirski	 * For ease of testing, unmask NMIs right away.  Disabled by
1301a97439aaSAndy Lutomirski	 * default because IRET is very expensive.
1302a97439aaSAndy Lutomirski	 */
1303a97439aaSAndy Lutomirski	pushq	$0		/* SS */
1304a97439aaSAndy Lutomirski	pushq	%rsp		/* RSP (minus 8 because of the previous push) */
1305a97439aaSAndy Lutomirski	addq	$8, (%rsp)	/* Fix up RSP */
1306a97439aaSAndy Lutomirski	pushfq			/* RFLAGS */
1307a97439aaSAndy Lutomirski	pushq	$__KERNEL_CS	/* CS */
1308a97439aaSAndy Lutomirski	pushq	$1f		/* RIP */
1309929bacecSAndy Lutomirski	iretq			/* continues at repeat_nmi below */
13108c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1311a97439aaSAndy Lutomirski1:
1312a97439aaSAndy Lutomirski#endif
1313a97439aaSAndy Lutomirski
13140b22930eSAndy Lutomirskirepeat_nmi:
1315905a36a2SIngo Molnar	/*
1316905a36a2SIngo Molnar	 * If there was a nested NMI, the first NMI's iret will return
1317905a36a2SIngo Molnar	 * here. But NMIs are still enabled and we can take another
1318905a36a2SIngo Molnar	 * nested NMI. The nested NMI checks the interrupted RIP to see
1319905a36a2SIngo Molnar	 * if it is between repeat_nmi and end_repeat_nmi, and if so
1320905a36a2SIngo Molnar	 * it will just return, as we are about to repeat an NMI anyway.
1321905a36a2SIngo Molnar	 * This makes it safe to copy to the stack frame that a nested
1322905a36a2SIngo Molnar	 * NMI will update.
13230b22930eSAndy Lutomirski	 *
13240b22930eSAndy Lutomirski	 * RSP is pointing to "outermost RIP".  gsbase is unknown, but, if
13250b22930eSAndy Lutomirski	 * we're repeating an NMI, gsbase has the same value that it had on
13260b22930eSAndy Lutomirski	 * the first iteration.  paranoid_entry will load the kernel
13276271fef0SThomas Gleixner	 * gsbase if needed before we call exc_nmi().  "NMI executing"
132836f1a77bSAndy Lutomirski	 * is zero.
1329905a36a2SIngo Molnar	 */
133036f1a77bSAndy Lutomirski	movq	$1, 10*8(%rsp)		/* Set "NMI executing". */
1331905a36a2SIngo Molnar
13320b22930eSAndy Lutomirski	/*
13330b22930eSAndy Lutomirski	 * Copy the "outermost" frame to the "iret" frame.  NMIs that nest
13340b22930eSAndy Lutomirski	 * here must not modify the "iret" frame while we're writing to
13350b22930eSAndy Lutomirski	 * it or it will end up containing garbage.
13360b22930eSAndy Lutomirski	 */
1337905a36a2SIngo Molnar	addq	$(10*8), %rsp
1338905a36a2SIngo Molnar	.rept 5
1339905a36a2SIngo Molnar	pushq	-6*8(%rsp)
1340905a36a2SIngo Molnar	.endr
1341905a36a2SIngo Molnar	subq	$(5*8), %rsp
1342905a36a2SIngo Molnarend_repeat_nmi:
1343905a36a2SIngo Molnar
1344905a36a2SIngo Molnar	/*
13450b22930eSAndy Lutomirski	 * Everything below this point can be preempted by a nested NMI.
13460b22930eSAndy Lutomirski	 * If this happens, then the inner NMI will change the "iret"
13470b22930eSAndy Lutomirski	 * frame to point back to repeat_nmi.
1348905a36a2SIngo Molnar	 */
1349905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
1350905a36a2SIngo Molnar
1351905a36a2SIngo Molnar	/*
1352905a36a2SIngo Molnar	 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1353905a36a2SIngo Molnar	 * as we should not be calling schedule in NMI context.
1354905a36a2SIngo Molnar	 * Even with normal interrupts enabled. An NMI should not be
1355905a36a2SIngo Molnar	 * setting NEED_RESCHED or anything that normal interrupts and
1356905a36a2SIngo Molnar	 * exceptions might do.
1357905a36a2SIngo Molnar	 */
1358905a36a2SIngo Molnar	call	paranoid_entry
13598c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
1360905a36a2SIngo Molnar
1361905a36a2SIngo Molnar	movq	%rsp, %rdi
1362905a36a2SIngo Molnar	movq	$-1, %rsi
13636271fef0SThomas Gleixner	call	exc_nmi
1364905a36a2SIngo Molnar
136516561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
136621e94459SPeter Zijlstra	RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
13678a09317bSDave Hansen
1368c82965f9SChang S. Bae	/*
1369c82965f9SChang S. Bae	 * The above invocation of paranoid_entry stored the GSBASE
1370c82965f9SChang S. Bae	 * related information in R/EBX depending on the availability
1371c82965f9SChang S. Bae	 * of FSGSBASE.
1372c82965f9SChang S. Bae	 *
1373c82965f9SChang S. Bae	 * If FSGSBASE is enabled, restore the saved GSBASE value
1374c82965f9SChang S. Bae	 * unconditionally, otherwise take the conditional SWAPGS path.
1375c82965f9SChang S. Bae	 */
1376c82965f9SChang S. Bae	ALTERNATIVE "jmp nmi_no_fsgsbase", "", X86_FEATURE_FSGSBASE
1377c82965f9SChang S. Bae
1378c82965f9SChang S. Bae	wrgsbase	%rbx
1379c82965f9SChang S. Bae	jmp	nmi_restore
1380c82965f9SChang S. Bae
1381c82965f9SChang S. Baenmi_no_fsgsbase:
1382c82965f9SChang S. Bae	/* EBX == 0 -> invoke SWAPGS */
1383c82965f9SChang S. Bae	testl	%ebx, %ebx
1384905a36a2SIngo Molnar	jnz	nmi_restore
1385c82965f9SChang S. Bae
1386905a36a2SIngo Molnarnmi_swapgs:
138753c9d924SJuergen Gross	swapgs
1388c82965f9SChang S. Bae
1389905a36a2SIngo Molnarnmi_restore:
1390502af0d7SDominik Brodowski	POP_REGS
13910b22930eSAndy Lutomirski
1392471ee483SAndy Lutomirski	/*
1393471ee483SAndy Lutomirski	 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1394471ee483SAndy Lutomirski	 * at the "iret" frame.
1395471ee483SAndy Lutomirski	 */
1396471ee483SAndy Lutomirski	addq	$6*8, %rsp
1397905a36a2SIngo Molnar
1398810bc075SAndy Lutomirski	/*
1399810bc075SAndy Lutomirski	 * Clear "NMI executing".  Set DF first so that we can easily
1400810bc075SAndy Lutomirski	 * distinguish the remaining code between here and IRET from
1401929bacecSAndy Lutomirski	 * the SYSCALL entry and exit paths.
1402929bacecSAndy Lutomirski	 *
1403929bacecSAndy Lutomirski	 * We arguably should just inspect RIP instead, but I (Andy) wrote
1404929bacecSAndy Lutomirski	 * this code when I had the misapprehension that Xen PV supported
1405929bacecSAndy Lutomirski	 * NMIs, and Xen PV would break that approach.
1406810bc075SAndy Lutomirski	 */
1407810bc075SAndy Lutomirski	std
1408810bc075SAndy Lutomirski	movq	$0, 5*8(%rsp)		/* clear "NMI executing" */
14090b22930eSAndy Lutomirski
14100b22930eSAndy Lutomirski	/*
1411929bacecSAndy Lutomirski	 * iretq reads the "iret" frame and exits the NMI stack in a
1412929bacecSAndy Lutomirski	 * single instruction.  We are returning to kernel mode, so this
1413929bacecSAndy Lutomirski	 * cannot result in a fault.  Similarly, we don't need to worry
1414929bacecSAndy Lutomirski	 * about espfix64 on the way back to kernel mode.
14150b22930eSAndy Lutomirski	 */
1416929bacecSAndy Lutomirski	iretq
14176271fef0SThomas GleixnerSYM_CODE_END(asm_exc_nmi)
1418905a36a2SIngo Molnar
1419dffb3f9dSAndy Lutomirski#ifndef CONFIG_IA32_EMULATION
1420dffb3f9dSAndy Lutomirski/*
1421dffb3f9dSAndy Lutomirski * This handles SYSCALL from 32-bit code.  There is no way to program
1422dffb3f9dSAndy Lutomirski * MSRs to fully disable 32-bit SYSCALL.
1423dffb3f9dSAndy Lutomirski */
1424bc7b11c0SJiri SlabySYM_CODE_START(ignore_sysret)
14258c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
1426905a36a2SIngo Molnar	mov	$-ENOSYS, %eax
1427b2b1d94cSJan Beulich	sysretl
1428bc7b11c0SJiri SlabySYM_CODE_END(ignore_sysret)
1429dffb3f9dSAndy Lutomirski#endif
14302deb4be2SAndy Lutomirski
1431b9f6976bSThomas Gleixner.pushsection .text, "ax"
1432bc7b11c0SJiri SlabySYM_CODE_START(rewind_stack_do_exit)
14338c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
14342deb4be2SAndy Lutomirski	/* Prevent any naive code from trying to unwind to our caller. */
14352deb4be2SAndy Lutomirski	xorl	%ebp, %ebp
14362deb4be2SAndy Lutomirski
14372deb4be2SAndy Lutomirski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rax
14388c1f7558SJosh Poimboeuf	leaq	-PTREGS_SIZE(%rax), %rsp
1439f977df7bSJann Horn	UNWIND_HINT_REGS
14402deb4be2SAndy Lutomirski
14412deb4be2SAndy Lutomirski	call	do_exit
1442bc7b11c0SJiri SlabySYM_CODE_END(rewind_stack_do_exit)
1443b9f6976bSThomas Gleixner.popsection
1444