xref: /openbmc/linux/arch/x86/entry/entry_64.S (revision 471ee4832209e986029b9fabdaad57b1eecb856b)
1905a36a2SIngo Molnar/*
2905a36a2SIngo Molnar *  linux/arch/x86_64/entry.S
3905a36a2SIngo Molnar *
4905a36a2SIngo Molnar *  Copyright (C) 1991, 1992  Linus Torvalds
5905a36a2SIngo Molnar *  Copyright (C) 2000, 2001, 2002  Andi Kleen SuSE Labs
6905a36a2SIngo Molnar *  Copyright (C) 2000  Pavel Machek <pavel@suse.cz>
74d732138SIngo Molnar *
8905a36a2SIngo Molnar * entry.S contains the system-call and fault low-level handling routines.
9905a36a2SIngo Molnar *
10905a36a2SIngo Molnar * Some of this is documented in Documentation/x86/entry_64.txt
11905a36a2SIngo Molnar *
12905a36a2SIngo Molnar * A note on terminology:
13905a36a2SIngo Molnar * - iret frame:	Architecture defined interrupt frame from SS to RIP
14905a36a2SIngo Molnar *			at the top of the kernel process stack.
15905a36a2SIngo Molnar *
16905a36a2SIngo Molnar * Some macro usage:
174d732138SIngo Molnar * - ENTRY/END:		Define functions in the symbol table.
184d732138SIngo Molnar * - TRACE_IRQ_*:	Trace hardirq state for lock debugging.
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>
25d36f9479SIngo Molnar#include "calling.h"
26905a36a2SIngo Molnar#include <asm/asm-offsets.h>
27905a36a2SIngo Molnar#include <asm/msr.h>
28905a36a2SIngo Molnar#include <asm/unistd.h>
29905a36a2SIngo Molnar#include <asm/thread_info.h>
30905a36a2SIngo Molnar#include <asm/hw_irq.h>
31905a36a2SIngo Molnar#include <asm/page_types.h>
32905a36a2SIngo Molnar#include <asm/irqflags.h>
33905a36a2SIngo Molnar#include <asm/paravirt.h>
34905a36a2SIngo Molnar#include <asm/percpu.h>
35905a36a2SIngo Molnar#include <asm/asm.h>
36905a36a2SIngo Molnar#include <asm/smap.h>
37905a36a2SIngo Molnar#include <asm/pgtable_types.h>
38784d5699SAl Viro#include <asm/export.h>
398c1f7558SJosh Poimboeuf#include <asm/frame.h>
40905a36a2SIngo Molnar#include <linux/err.h>
41905a36a2SIngo Molnar
42905a36a2SIngo Molnar.code64
43905a36a2SIngo Molnar.section .entry.text, "ax"
44905a36a2SIngo Molnar
45905a36a2SIngo Molnar#ifdef CONFIG_PARAVIRT
46905a36a2SIngo MolnarENTRY(native_usergs_sysret64)
478c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
48905a36a2SIngo Molnar	swapgs
49905a36a2SIngo Molnar	sysretq
508c1f7558SJosh PoimboeufEND(native_usergs_sysret64)
51905a36a2SIngo Molnar#endif /* CONFIG_PARAVIRT */
52905a36a2SIngo Molnar
53905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ
54905a36a2SIngo Molnar#ifdef CONFIG_TRACE_IRQFLAGS
55905a36a2SIngo Molnar	bt	$9, EFLAGS(%rsp)		/* interrupts off? */
56905a36a2SIngo Molnar	jnc	1f
57905a36a2SIngo Molnar	TRACE_IRQS_ON
58905a36a2SIngo Molnar1:
59905a36a2SIngo Molnar#endif
60905a36a2SIngo Molnar.endm
61905a36a2SIngo Molnar
62905a36a2SIngo Molnar/*
63905a36a2SIngo Molnar * When dynamic function tracer is enabled it will add a breakpoint
64905a36a2SIngo Molnar * to all locations that it is about to modify, sync CPUs, update
65905a36a2SIngo Molnar * all the code, sync CPUs, then remove the breakpoints. In this time
66905a36a2SIngo Molnar * if lockdep is enabled, it might jump back into the debug handler
67905a36a2SIngo Molnar * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
68905a36a2SIngo Molnar *
69905a36a2SIngo Molnar * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
70905a36a2SIngo Molnar * make sure the stack pointer does not get reset back to the top
71905a36a2SIngo Molnar * of the debug stack, and instead just reuses the current stack.
72905a36a2SIngo Molnar */
73905a36a2SIngo Molnar#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
74905a36a2SIngo Molnar
75905a36a2SIngo Molnar.macro TRACE_IRQS_OFF_DEBUG
76905a36a2SIngo Molnar	call	debug_stack_set_zero
77905a36a2SIngo Molnar	TRACE_IRQS_OFF
78905a36a2SIngo Molnar	call	debug_stack_reset
79905a36a2SIngo Molnar.endm
80905a36a2SIngo Molnar
81905a36a2SIngo Molnar.macro TRACE_IRQS_ON_DEBUG
82905a36a2SIngo Molnar	call	debug_stack_set_zero
83905a36a2SIngo Molnar	TRACE_IRQS_ON
84905a36a2SIngo Molnar	call	debug_stack_reset
85905a36a2SIngo Molnar.endm
86905a36a2SIngo Molnar
87905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ_DEBUG
88905a36a2SIngo Molnar	bt	$9, EFLAGS(%rsp)		/* interrupts off? */
89905a36a2SIngo Molnar	jnc	1f
90905a36a2SIngo Molnar	TRACE_IRQS_ON_DEBUG
91905a36a2SIngo Molnar1:
92905a36a2SIngo Molnar.endm
93905a36a2SIngo Molnar
94905a36a2SIngo Molnar#else
95905a36a2SIngo Molnar# define TRACE_IRQS_OFF_DEBUG			TRACE_IRQS_OFF
96905a36a2SIngo Molnar# define TRACE_IRQS_ON_DEBUG			TRACE_IRQS_ON
97905a36a2SIngo Molnar# define TRACE_IRQS_IRETQ_DEBUG			TRACE_IRQS_IRETQ
98905a36a2SIngo Molnar#endif
99905a36a2SIngo Molnar
100905a36a2SIngo Molnar/*
1014d732138SIngo Molnar * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
102905a36a2SIngo Molnar *
103fda57b22SAndy Lutomirski * This is the only entry point used for 64-bit system calls.  The
104fda57b22SAndy Lutomirski * hardware interface is reasonably well designed and the register to
105fda57b22SAndy Lutomirski * argument mapping Linux uses fits well with the registers that are
106fda57b22SAndy Lutomirski * available when SYSCALL is used.
107fda57b22SAndy Lutomirski *
108fda57b22SAndy Lutomirski * SYSCALL instructions can be found inlined in libc implementations as
109fda57b22SAndy Lutomirski * well as some other programs and libraries.  There are also a handful
110fda57b22SAndy Lutomirski * of SYSCALL instructions in the vDSO used, for example, as a
111fda57b22SAndy Lutomirski * clock_gettimeofday fallback.
112fda57b22SAndy Lutomirski *
1134d732138SIngo Molnar * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
114905a36a2SIngo Molnar * then loads new ss, cs, and rip from previously programmed MSRs.
115905a36a2SIngo Molnar * rflags gets masked by a value from another MSR (so CLD and CLAC
116905a36a2SIngo Molnar * are not needed). SYSCALL does not save anything on the stack
117905a36a2SIngo Molnar * and does not change rsp.
118905a36a2SIngo Molnar *
119905a36a2SIngo Molnar * Registers on entry:
120905a36a2SIngo Molnar * rax  system call number
121905a36a2SIngo Molnar * rcx  return address
122905a36a2SIngo Molnar * r11  saved rflags (note: r11 is callee-clobbered register in C ABI)
123905a36a2SIngo Molnar * rdi  arg0
124905a36a2SIngo Molnar * rsi  arg1
125905a36a2SIngo Molnar * rdx  arg2
126905a36a2SIngo Molnar * r10  arg3 (needs to be moved to rcx to conform to C ABI)
127905a36a2SIngo Molnar * r8   arg4
128905a36a2SIngo Molnar * r9   arg5
129905a36a2SIngo Molnar * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
130905a36a2SIngo Molnar *
131905a36a2SIngo Molnar * Only called from user space.
132905a36a2SIngo Molnar *
133905a36a2SIngo Molnar * When user can change pt_regs->foo always force IRET. That is because
134905a36a2SIngo Molnar * it deals with uncanonical addresses better. SYSRET has trouble
135905a36a2SIngo Molnar * with them due to bugs in both AMD and Intel CPUs.
136905a36a2SIngo Molnar */
137905a36a2SIngo Molnar
138b2502b41SIngo MolnarENTRY(entry_SYSCALL_64)
1398c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
140905a36a2SIngo Molnar	/*
141905a36a2SIngo Molnar	 * Interrupts are off on entry.
142905a36a2SIngo Molnar	 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
143905a36a2SIngo Molnar	 * it is too small to ever cause noticeable irq latency.
144905a36a2SIngo Molnar	 */
145905a36a2SIngo Molnar
1468a9949bcSAndy Lutomirski	swapgs
147905a36a2SIngo Molnar	movq	%rsp, PER_CPU_VAR(rsp_scratch)
148905a36a2SIngo Molnar	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
149905a36a2SIngo Molnar
1501e423bffSAndy Lutomirski	TRACE_IRQS_OFF
1511e423bffSAndy Lutomirski
152905a36a2SIngo Molnar	/* Construct struct pt_regs on stack */
153905a36a2SIngo Molnar	pushq	$__USER_DS			/* pt_regs->ss */
154905a36a2SIngo Molnar	pushq	PER_CPU_VAR(rsp_scratch)	/* pt_regs->sp */
155905a36a2SIngo Molnar	pushq	%r11				/* pt_regs->flags */
156905a36a2SIngo Molnar	pushq	$__USER_CS			/* pt_regs->cs */
157905a36a2SIngo Molnar	pushq	%rcx				/* pt_regs->ip */
1588a9949bcSAndy LutomirskiGLOBAL(entry_SYSCALL_64_after_hwframe)
159905a36a2SIngo Molnar	pushq	%rax				/* pt_regs->orig_ax */
160905a36a2SIngo Molnar	pushq	%rdi				/* pt_regs->di */
161905a36a2SIngo Molnar	pushq	%rsi				/* pt_regs->si */
162905a36a2SIngo Molnar	pushq	%rdx				/* pt_regs->dx */
163905a36a2SIngo Molnar	pushq	%rcx				/* pt_regs->cx */
164905a36a2SIngo Molnar	pushq	$-ENOSYS			/* pt_regs->ax */
165905a36a2SIngo Molnar	pushq	%r8				/* pt_regs->r8 */
166905a36a2SIngo Molnar	pushq	%r9				/* pt_regs->r9 */
167905a36a2SIngo Molnar	pushq	%r10				/* pt_regs->r10 */
168905a36a2SIngo Molnar	pushq	%r11				/* pt_regs->r11 */
169905a36a2SIngo Molnar	sub	$(6*8), %rsp			/* pt_regs->bp, bx, r12-15 not saved */
1708c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS extra=0
171905a36a2SIngo Molnar
1721e423bffSAndy Lutomirski	/*
1731e423bffSAndy Lutomirski	 * If we need to do entry work or if we guess we'll need to do
1741e423bffSAndy Lutomirski	 * exit work, go straight to the slow path.
1751e423bffSAndy Lutomirski	 */
17615f4eae7SAndy Lutomirski	movq	PER_CPU_VAR(current_task), %r11
17715f4eae7SAndy Lutomirski	testl	$_TIF_WORK_SYSCALL_ENTRY|_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
1781e423bffSAndy Lutomirski	jnz	entry_SYSCALL64_slow_path
1791e423bffSAndy Lutomirski
180b2502b41SIngo Molnarentry_SYSCALL_64_fastpath:
1811e423bffSAndy Lutomirski	/*
1821e423bffSAndy Lutomirski	 * Easy case: enable interrupts and issue the syscall.  If the syscall
1831e423bffSAndy Lutomirski	 * needs pt_regs, we'll call a stub that disables interrupts again
1841e423bffSAndy Lutomirski	 * and jumps to the slow path.
1851e423bffSAndy Lutomirski	 */
1861e423bffSAndy Lutomirski	TRACE_IRQS_ON
1871e423bffSAndy Lutomirski	ENABLE_INTERRUPTS(CLBR_NONE)
188905a36a2SIngo Molnar#if __SYSCALL_MASK == ~0
189905a36a2SIngo Molnar	cmpq	$__NR_syscall_max, %rax
190905a36a2SIngo Molnar#else
191905a36a2SIngo Molnar	andl	$__SYSCALL_MASK, %eax
192905a36a2SIngo Molnar	cmpl	$__NR_syscall_max, %eax
193905a36a2SIngo Molnar#endif
194905a36a2SIngo Molnar	ja	1f				/* return -ENOSYS (already in pt_regs->ax) */
195905a36a2SIngo Molnar	movq	%r10, %rcx
196302f5b26SAndy Lutomirski
197302f5b26SAndy Lutomirski	/*
198302f5b26SAndy Lutomirski	 * This call instruction is handled specially in stub_ptregs_64.
199b7765086SAndy Lutomirski	 * It might end up jumping to the slow path.  If it jumps, RAX
200b7765086SAndy Lutomirski	 * and all argument registers are clobbered.
201302f5b26SAndy Lutomirski	 */
202905a36a2SIngo Molnar	call	*sys_call_table(, %rax, 8)
203302f5b26SAndy Lutomirski.Lentry_SYSCALL_64_after_fastpath_call:
204302f5b26SAndy Lutomirski
205905a36a2SIngo Molnar	movq	%rax, RAX(%rsp)
206905a36a2SIngo Molnar1:
2071e423bffSAndy Lutomirski
208905a36a2SIngo Molnar	/*
2091e423bffSAndy Lutomirski	 * If we get here, then we know that pt_regs is clean for SYSRET64.
2101e423bffSAndy Lutomirski	 * If we see that no exit work is required (which we are required
2111e423bffSAndy Lutomirski	 * to check with IRQs off), then we can go straight to SYSRET64.
212905a36a2SIngo Molnar	 */
2132140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
2141e423bffSAndy Lutomirski	TRACE_IRQS_OFF
21515f4eae7SAndy Lutomirski	movq	PER_CPU_VAR(current_task), %r11
21615f4eae7SAndy Lutomirski	testl	$_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
2171e423bffSAndy Lutomirski	jnz	1f
218905a36a2SIngo Molnar
2191e423bffSAndy Lutomirski	LOCKDEP_SYS_EXIT
2201e423bffSAndy Lutomirski	TRACE_IRQS_ON		/* user mode is traced as IRQs on */
221eb2a54c3SAndy Lutomirski	movq	RIP(%rsp), %rcx
222eb2a54c3SAndy Lutomirski	movq	EFLAGS(%rsp), %r11
223a5122106SAndy Lutomirski	addq	$6*8, %rsp	/* skip extra regs -- they were preserved */
2248c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
225a5122106SAndy Lutomirski	jmp	.Lpop_c_regs_except_rcx_r11_and_sysret
226905a36a2SIngo Molnar
2271e423bffSAndy Lutomirski1:
2281e423bffSAndy Lutomirski	/*
2291e423bffSAndy Lutomirski	 * The fast path looked good when we started, but something changed
2301e423bffSAndy Lutomirski	 * along the way and we need to switch to the slow path.  Calling
2311e423bffSAndy Lutomirski	 * raise(3) will trigger this, for example.  IRQs are off.
2321e423bffSAndy Lutomirski	 */
23329ea1b25SAndy Lutomirski	TRACE_IRQS_ON
2342140a994SJan Beulich	ENABLE_INTERRUPTS(CLBR_ANY)
235905a36a2SIngo Molnar	SAVE_EXTRA_REGS
23629ea1b25SAndy Lutomirski	movq	%rsp, %rdi
23729ea1b25SAndy Lutomirski	call	syscall_return_slowpath	/* returns with IRQs disabled */
2381e423bffSAndy Lutomirski	jmp	return_from_SYSCALL_64
2391e423bffSAndy Lutomirski
2401e423bffSAndy Lutomirskientry_SYSCALL64_slow_path:
2411e423bffSAndy Lutomirski	/* IRQs are off. */
2421e423bffSAndy Lutomirski	SAVE_EXTRA_REGS
2431e423bffSAndy Lutomirski	movq	%rsp, %rdi
2441e423bffSAndy Lutomirski	call	do_syscall_64		/* returns with IRQs disabled */
2451e423bffSAndy Lutomirski
2461e423bffSAndy Lutomirskireturn_from_SYSCALL_64:
24729ea1b25SAndy Lutomirski	TRACE_IRQS_IRETQ		/* we're about to change IF */
248905a36a2SIngo Molnar
249905a36a2SIngo Molnar	/*
250905a36a2SIngo Molnar	 * Try to use SYSRET instead of IRET if we're returning to
2518a055d7fSAndy Lutomirski	 * a completely clean 64-bit userspace context.  If we're not,
2528a055d7fSAndy Lutomirski	 * go to the slow exit path.
253905a36a2SIngo Molnar	 */
254905a36a2SIngo Molnar	movq	RCX(%rsp), %rcx
255905a36a2SIngo Molnar	movq	RIP(%rsp), %r11
2568a055d7fSAndy Lutomirski
2578a055d7fSAndy Lutomirski	cmpq	%rcx, %r11	/* SYSRET requires RCX == RIP */
2588a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
259905a36a2SIngo Molnar
260905a36a2SIngo Molnar	/*
261905a36a2SIngo Molnar	 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
262905a36a2SIngo Molnar	 * in kernel space.  This essentially lets the user take over
263905a36a2SIngo Molnar	 * the kernel, since userspace controls RSP.
264905a36a2SIngo Molnar	 *
265905a36a2SIngo Molnar	 * If width of "canonical tail" ever becomes variable, this will need
266905a36a2SIngo Molnar	 * to be updated to remain correct on both old and new CPUs.
267361b4b58SKirill A. Shutemov	 *
268cbe0317bSKirill A. Shutemov	 * Change top bits to match most significant bit (47th or 56th bit
269cbe0317bSKirill A. Shutemov	 * depending on paging mode) in the address.
270905a36a2SIngo Molnar	 */
271905a36a2SIngo Molnar	shl	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
272905a36a2SIngo Molnar	sar	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
2734d732138SIngo Molnar
274905a36a2SIngo Molnar	/* If this changed %rcx, it was not canonical */
275905a36a2SIngo Molnar	cmpq	%rcx, %r11
2768a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
277905a36a2SIngo Molnar
278905a36a2SIngo Molnar	cmpq	$__USER_CS, CS(%rsp)		/* CS must match SYSRET */
2798a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
280905a36a2SIngo Molnar
281905a36a2SIngo Molnar	movq	R11(%rsp), %r11
282905a36a2SIngo Molnar	cmpq	%r11, EFLAGS(%rsp)		/* R11 == RFLAGS */
2838a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
284905a36a2SIngo Molnar
285905a36a2SIngo Molnar	/*
2863e035305SBorislav Petkov	 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
2873e035305SBorislav Petkov	 * restore RF properly. If the slowpath sets it for whatever reason, we
2883e035305SBorislav Petkov	 * need to restore it correctly.
2893e035305SBorislav Petkov	 *
2903e035305SBorislav Petkov	 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
2913e035305SBorislav Petkov	 * trap from userspace immediately after SYSRET.  This would cause an
2923e035305SBorislav Petkov	 * infinite loop whenever #DB happens with register state that satisfies
2933e035305SBorislav Petkov	 * the opportunistic SYSRET conditions.  For example, single-stepping
2943e035305SBorislav Petkov	 * this user code:
295905a36a2SIngo Molnar	 *
296905a36a2SIngo Molnar	 *           movq	$stuck_here, %rcx
297905a36a2SIngo Molnar	 *           pushfq
298905a36a2SIngo Molnar	 *           popq %r11
299905a36a2SIngo Molnar	 *   stuck_here:
300905a36a2SIngo Molnar	 *
301905a36a2SIngo Molnar	 * would never get past 'stuck_here'.
302905a36a2SIngo Molnar	 */
303905a36a2SIngo Molnar	testq	$(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
3048a055d7fSAndy Lutomirski	jnz	swapgs_restore_regs_and_return_to_usermode
305905a36a2SIngo Molnar
306905a36a2SIngo Molnar	/* nothing to check for RSP */
307905a36a2SIngo Molnar
308905a36a2SIngo Molnar	cmpq	$__USER_DS, SS(%rsp)		/* SS must match SYSRET */
3098a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
310905a36a2SIngo Molnar
311905a36a2SIngo Molnar	/*
312905a36a2SIngo Molnar	 * We win! This label is here just for ease of understanding
313905a36a2SIngo Molnar	 * perf profiles. Nothing jumps here.
314905a36a2SIngo Molnar	 */
315905a36a2SIngo Molnarsyscall_return_via_sysret:
316905a36a2SIngo Molnar	/* rcx and r11 are already restored (see code above) */
3178c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
3184fbb3910SAndy Lutomirski	POP_EXTRA_REGS
319a5122106SAndy Lutomirski.Lpop_c_regs_except_rcx_r11_and_sysret:
3204fbb3910SAndy Lutomirski	popq	%rsi	/* skip r11 */
3214fbb3910SAndy Lutomirski	popq	%r10
3224fbb3910SAndy Lutomirski	popq	%r9
3234fbb3910SAndy Lutomirski	popq	%r8
3244fbb3910SAndy Lutomirski	popq	%rax
3254fbb3910SAndy Lutomirski	popq	%rsi	/* skip rcx */
3264fbb3910SAndy Lutomirski	popq	%rdx
3274fbb3910SAndy Lutomirski	popq	%rsi
3284fbb3910SAndy Lutomirski	popq	%rdi
3294fbb3910SAndy Lutomirski	movq	RSP-ORIG_RAX(%rsp), %rsp
330905a36a2SIngo Molnar	USERGS_SYSRET64
331b2502b41SIngo MolnarEND(entry_SYSCALL_64)
332905a36a2SIngo Molnar
333302f5b26SAndy LutomirskiENTRY(stub_ptregs_64)
334302f5b26SAndy Lutomirski	/*
335302f5b26SAndy Lutomirski	 * Syscalls marked as needing ptregs land here.
336b7765086SAndy Lutomirski	 * If we are on the fast path, we need to save the extra regs,
337b7765086SAndy Lutomirski	 * which we achieve by trying again on the slow path.  If we are on
338b7765086SAndy Lutomirski	 * the slow path, the extra regs are already saved.
339302f5b26SAndy Lutomirski	 *
340302f5b26SAndy Lutomirski	 * RAX stores a pointer to the C function implementing the syscall.
341b7765086SAndy Lutomirski	 * IRQs are on.
342302f5b26SAndy Lutomirski	 */
343302f5b26SAndy Lutomirski	cmpq	$.Lentry_SYSCALL_64_after_fastpath_call, (%rsp)
344302f5b26SAndy Lutomirski	jne	1f
345302f5b26SAndy Lutomirski
346b7765086SAndy Lutomirski	/*
347b7765086SAndy Lutomirski	 * Called from fast path -- disable IRQs again, pop return address
348b7765086SAndy Lutomirski	 * and jump to slow path
349b7765086SAndy Lutomirski	 */
3502140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
351b7765086SAndy Lutomirski	TRACE_IRQS_OFF
352302f5b26SAndy Lutomirski	popq	%rax
3538c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS extra=0
354b7765086SAndy Lutomirski	jmp	entry_SYSCALL64_slow_path
355302f5b26SAndy Lutomirski
356302f5b26SAndy Lutomirski1:
357b3830e8dSBorislav Petkov	jmp	*%rax				/* Called from C */
358302f5b26SAndy LutomirskiEND(stub_ptregs_64)
359302f5b26SAndy Lutomirski
360302f5b26SAndy Lutomirski.macro ptregs_stub func
361302f5b26SAndy LutomirskiENTRY(ptregs_\func)
3628c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
363302f5b26SAndy Lutomirski	leaq	\func(%rip), %rax
364302f5b26SAndy Lutomirski	jmp	stub_ptregs_64
365302f5b26SAndy LutomirskiEND(ptregs_\func)
366302f5b26SAndy Lutomirski.endm
367302f5b26SAndy Lutomirski
368302f5b26SAndy Lutomirski/* Instantiate ptregs_stub for each ptregs-using syscall */
369302f5b26SAndy Lutomirski#define __SYSCALL_64_QUAL_(sym)
370302f5b26SAndy Lutomirski#define __SYSCALL_64_QUAL_ptregs(sym) ptregs_stub sym
371302f5b26SAndy Lutomirski#define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym)
372302f5b26SAndy Lutomirski#include <asm/syscalls_64.h>
373905a36a2SIngo Molnar
374905a36a2SIngo Molnar/*
3750100301bSBrian Gerst * %rdi: prev task
3760100301bSBrian Gerst * %rsi: next task
3770100301bSBrian Gerst */
3780100301bSBrian GerstENTRY(__switch_to_asm)
3798c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
3800100301bSBrian Gerst	/*
3810100301bSBrian Gerst	 * Save callee-saved registers
3820100301bSBrian Gerst	 * This must match the order in inactive_task_frame
3830100301bSBrian Gerst	 */
3840100301bSBrian Gerst	pushq	%rbp
3850100301bSBrian Gerst	pushq	%rbx
3860100301bSBrian Gerst	pushq	%r12
3870100301bSBrian Gerst	pushq	%r13
3880100301bSBrian Gerst	pushq	%r14
3890100301bSBrian Gerst	pushq	%r15
3900100301bSBrian Gerst
3910100301bSBrian Gerst	/* switch stack */
3920100301bSBrian Gerst	movq	%rsp, TASK_threadsp(%rdi)
3930100301bSBrian Gerst	movq	TASK_threadsp(%rsi), %rsp
3940100301bSBrian Gerst
3950100301bSBrian Gerst#ifdef CONFIG_CC_STACKPROTECTOR
3960100301bSBrian Gerst	movq	TASK_stack_canary(%rsi), %rbx
3970100301bSBrian Gerst	movq	%rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset
3980100301bSBrian Gerst#endif
3990100301bSBrian Gerst
4000100301bSBrian Gerst	/* restore callee-saved registers */
4010100301bSBrian Gerst	popq	%r15
4020100301bSBrian Gerst	popq	%r14
4030100301bSBrian Gerst	popq	%r13
4040100301bSBrian Gerst	popq	%r12
4050100301bSBrian Gerst	popq	%rbx
4060100301bSBrian Gerst	popq	%rbp
4070100301bSBrian Gerst
4080100301bSBrian Gerst	jmp	__switch_to
4090100301bSBrian GerstEND(__switch_to_asm)
4100100301bSBrian Gerst
4110100301bSBrian Gerst/*
412905a36a2SIngo Molnar * A newly forked process directly context switches into this address.
413905a36a2SIngo Molnar *
4140100301bSBrian Gerst * rax: prev task we switched from
415616d2483SBrian Gerst * rbx: kernel thread func (NULL for user thread)
416616d2483SBrian Gerst * r12: kernel thread arg
417905a36a2SIngo Molnar */
418905a36a2SIngo MolnarENTRY(ret_from_fork)
4198c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
4200100301bSBrian Gerst	movq	%rax, %rdi
4214d732138SIngo Molnar	call	schedule_tail			/* rdi: 'prev' task parameter */
422905a36a2SIngo Molnar
423616d2483SBrian Gerst	testq	%rbx, %rbx			/* from kernel_thread? */
424616d2483SBrian Gerst	jnz	1f				/* kernel threads are uncommon */
425905a36a2SIngo Molnar
426616d2483SBrian Gerst2:
4278c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
428ebd57499SJosh Poimboeuf	movq	%rsp, %rdi
42924d978b7SAndy Lutomirski	call	syscall_return_slowpath	/* returns with IRQs disabled */
43024d978b7SAndy Lutomirski	TRACE_IRQS_ON			/* user mode is traced as IRQS on */
4318a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
432616d2483SBrian Gerst
433616d2483SBrian Gerst1:
434616d2483SBrian Gerst	/* kernel thread */
435616d2483SBrian Gerst	movq	%r12, %rdi
436616d2483SBrian Gerst	call	*%rbx
437616d2483SBrian Gerst	/*
438616d2483SBrian Gerst	 * A kernel thread is allowed to return here after successfully
439616d2483SBrian Gerst	 * calling do_execve().  Exit to userspace to complete the execve()
440616d2483SBrian Gerst	 * syscall.
441616d2483SBrian Gerst	 */
442616d2483SBrian Gerst	movq	$0, RAX(%rsp)
443616d2483SBrian Gerst	jmp	2b
444905a36a2SIngo MolnarEND(ret_from_fork)
445905a36a2SIngo Molnar
446905a36a2SIngo Molnar/*
447905a36a2SIngo Molnar * Build the entry stubs with some assembler magic.
448905a36a2SIngo Molnar * We pack 1 stub into every 8-byte block.
449905a36a2SIngo Molnar */
450905a36a2SIngo Molnar	.align 8
451905a36a2SIngo MolnarENTRY(irq_entries_start)
452905a36a2SIngo Molnar    vector=FIRST_EXTERNAL_VECTOR
453905a36a2SIngo Molnar    .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
4548c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
455905a36a2SIngo Molnar	pushq	$(~vector+0x80)			/* Note: always in signed byte range */
456905a36a2SIngo Molnar	jmp	common_interrupt
457905a36a2SIngo Molnar	.align	8
4588c1f7558SJosh Poimboeuf	vector=vector+1
459905a36a2SIngo Molnar    .endr
460905a36a2SIngo MolnarEND(irq_entries_start)
461905a36a2SIngo Molnar
4621d3e53e8SAndy Lutomirski.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
4631d3e53e8SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
4641d3e53e8SAndy Lutomirski	pushfq
4651d3e53e8SAndy Lutomirski	testl $X86_EFLAGS_IF, (%rsp)
4661d3e53e8SAndy Lutomirski	jz .Lokay_\@
4671d3e53e8SAndy Lutomirski	ud2
4681d3e53e8SAndy Lutomirski.Lokay_\@:
4691d3e53e8SAndy Lutomirski	addq $8, %rsp
4701d3e53e8SAndy Lutomirski#endif
4711d3e53e8SAndy Lutomirski.endm
4721d3e53e8SAndy Lutomirski
4731d3e53e8SAndy Lutomirski/*
4741d3e53e8SAndy Lutomirski * Enters the IRQ stack if we're not already using it.  NMI-safe.  Clobbers
4751d3e53e8SAndy Lutomirski * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
4761d3e53e8SAndy Lutomirski * Requires kernel GSBASE.
4771d3e53e8SAndy Lutomirski *
4781d3e53e8SAndy Lutomirski * The invariant is that, if irq_count != -1, then the IRQ stack is in use.
4791d3e53e8SAndy Lutomirski */
4808c1f7558SJosh Poimboeuf.macro ENTER_IRQ_STACK regs=1 old_rsp
4811d3e53e8SAndy Lutomirski	DEBUG_ENTRY_ASSERT_IRQS_OFF
4821d3e53e8SAndy Lutomirski	movq	%rsp, \old_rsp
4838c1f7558SJosh Poimboeuf
4848c1f7558SJosh Poimboeuf	.if \regs
4858c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS base=\old_rsp
4868c1f7558SJosh Poimboeuf	.endif
4878c1f7558SJosh Poimboeuf
4881d3e53e8SAndy Lutomirski	incl	PER_CPU_VAR(irq_count)
48929955909SAndy Lutomirski	jnz	.Lirq_stack_push_old_rsp_\@
4901d3e53e8SAndy Lutomirski
4911d3e53e8SAndy Lutomirski	/*
4921d3e53e8SAndy Lutomirski	 * Right now, if we just incremented irq_count to zero, we've
4931d3e53e8SAndy Lutomirski	 * claimed the IRQ stack but we haven't switched to it yet.
4941d3e53e8SAndy Lutomirski	 *
4951d3e53e8SAndy Lutomirski	 * If anything is added that can interrupt us here without using IST,
4961d3e53e8SAndy Lutomirski	 * it must be *extremely* careful to limit its stack usage.  This
4971d3e53e8SAndy Lutomirski	 * could include kprobes and a hypothetical future IST-less #DB
4981d3e53e8SAndy Lutomirski	 * handler.
49929955909SAndy Lutomirski	 *
50029955909SAndy Lutomirski	 * The OOPS unwinder relies on the word at the top of the IRQ
50129955909SAndy Lutomirski	 * stack linking back to the previous RSP for the entire time we're
50229955909SAndy Lutomirski	 * on the IRQ stack.  For this to work reliably, we need to write
50329955909SAndy Lutomirski	 * it before we actually move ourselves to the IRQ stack.
5041d3e53e8SAndy Lutomirski	 */
5051d3e53e8SAndy Lutomirski
50629955909SAndy Lutomirski	movq	\old_rsp, PER_CPU_VAR(irq_stack_union + IRQ_STACK_SIZE - 8)
50729955909SAndy Lutomirski	movq	PER_CPU_VAR(irq_stack_ptr), %rsp
50829955909SAndy Lutomirski
50929955909SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
51029955909SAndy Lutomirski	/*
51129955909SAndy Lutomirski	 * If the first movq above becomes wrong due to IRQ stack layout
51229955909SAndy Lutomirski	 * changes, the only way we'll notice is if we try to unwind right
51329955909SAndy Lutomirski	 * here.  Assert that we set up the stack right to catch this type
51429955909SAndy Lutomirski	 * of bug quickly.
51529955909SAndy Lutomirski	 */
51629955909SAndy Lutomirski	cmpq	-8(%rsp), \old_rsp
51729955909SAndy Lutomirski	je	.Lirq_stack_okay\@
51829955909SAndy Lutomirski	ud2
51929955909SAndy Lutomirski	.Lirq_stack_okay\@:
52029955909SAndy Lutomirski#endif
52129955909SAndy Lutomirski
52229955909SAndy Lutomirski.Lirq_stack_push_old_rsp_\@:
5231d3e53e8SAndy Lutomirski	pushq	\old_rsp
5248c1f7558SJosh Poimboeuf
5258c1f7558SJosh Poimboeuf	.if \regs
5268c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS indirect=1
5278c1f7558SJosh Poimboeuf	.endif
5281d3e53e8SAndy Lutomirski.endm
5291d3e53e8SAndy Lutomirski
5301d3e53e8SAndy Lutomirski/*
5311d3e53e8SAndy Lutomirski * Undoes ENTER_IRQ_STACK.
5321d3e53e8SAndy Lutomirski */
5338c1f7558SJosh Poimboeuf.macro LEAVE_IRQ_STACK regs=1
5341d3e53e8SAndy Lutomirski	DEBUG_ENTRY_ASSERT_IRQS_OFF
5351d3e53e8SAndy Lutomirski	/* We need to be off the IRQ stack before decrementing irq_count. */
5361d3e53e8SAndy Lutomirski	popq	%rsp
5371d3e53e8SAndy Lutomirski
5388c1f7558SJosh Poimboeuf	.if \regs
5398c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
5408c1f7558SJosh Poimboeuf	.endif
5418c1f7558SJosh Poimboeuf
5421d3e53e8SAndy Lutomirski	/*
5431d3e53e8SAndy Lutomirski	 * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming
5441d3e53e8SAndy Lutomirski	 * the irq stack but we're not on it.
5451d3e53e8SAndy Lutomirski	 */
5461d3e53e8SAndy Lutomirski
5471d3e53e8SAndy Lutomirski	decl	PER_CPU_VAR(irq_count)
5481d3e53e8SAndy Lutomirski.endm
5491d3e53e8SAndy Lutomirski
550905a36a2SIngo Molnar/*
551905a36a2SIngo Molnar * Interrupt entry/exit.
552905a36a2SIngo Molnar *
553905a36a2SIngo Molnar * Interrupt entry points save only callee clobbered registers in fast path.
554905a36a2SIngo Molnar *
555905a36a2SIngo Molnar * Entry runs with interrupts off.
556905a36a2SIngo Molnar */
557905a36a2SIngo Molnar
558905a36a2SIngo Molnar/* 0(%rsp): ~(interrupt number) */
559905a36a2SIngo Molnar	.macro interrupt func
560905a36a2SIngo Molnar	cld
561ff467594SAndy Lutomirski	ALLOC_PT_GPREGS_ON_STACK
562ff467594SAndy Lutomirski	SAVE_C_REGS
563ff467594SAndy Lutomirski	SAVE_EXTRA_REGS
564946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
565905a36a2SIngo Molnar
566ff467594SAndy Lutomirski	testb	$3, CS(%rsp)
567905a36a2SIngo Molnar	jz	1f
56802bc7768SAndy Lutomirski
56902bc7768SAndy Lutomirski	/*
57002bc7768SAndy Lutomirski	 * IRQ from user mode.  Switch to kernel gsbase and inform context
57102bc7768SAndy Lutomirski	 * tracking that we're in kernel mode.
57202bc7768SAndy Lutomirski	 */
573905a36a2SIngo Molnar	SWAPGS
574f1075053SAndy Lutomirski
575f1075053SAndy Lutomirski	/*
576f1075053SAndy Lutomirski	 * We need to tell lockdep that IRQs are off.  We can't do this until
577f1075053SAndy Lutomirski	 * we fix gsbase, and we should do it before enter_from_user_mode
578f1075053SAndy Lutomirski	 * (which can take locks).  Since TRACE_IRQS_OFF idempotent,
579f1075053SAndy Lutomirski	 * the simplest way to handle it is to just call it twice if
580f1075053SAndy Lutomirski	 * we enter from user mode.  There's no reason to optimize this since
581f1075053SAndy Lutomirski	 * TRACE_IRQS_OFF is a no-op if lockdep is off.
582f1075053SAndy Lutomirski	 */
583f1075053SAndy Lutomirski	TRACE_IRQS_OFF
584f1075053SAndy Lutomirski
585478dc89cSAndy Lutomirski	CALL_enter_from_user_mode
58602bc7768SAndy Lutomirski
587905a36a2SIngo Molnar1:
5881d3e53e8SAndy Lutomirski	ENTER_IRQ_STACK old_rsp=%rdi
589905a36a2SIngo Molnar	/* We entered an interrupt context - irqs are off: */
590905a36a2SIngo Molnar	TRACE_IRQS_OFF
591905a36a2SIngo Molnar
592a586f98eSAndy Lutomirski	call	\func	/* rdi points to pt_regs */
593905a36a2SIngo Molnar	.endm
594905a36a2SIngo Molnar
595905a36a2SIngo Molnar	/*
596905a36a2SIngo Molnar	 * The interrupt stubs push (~vector+0x80) onto the stack and
597905a36a2SIngo Molnar	 * then jump to common_interrupt.
598905a36a2SIngo Molnar	 */
599905a36a2SIngo Molnar	.p2align CONFIG_X86_L1_CACHE_SHIFT
600905a36a2SIngo Molnarcommon_interrupt:
601905a36a2SIngo Molnar	ASM_CLAC
602905a36a2SIngo Molnar	addq	$-0x80, (%rsp)			/* Adjust vector to [-256, -1] range */
603905a36a2SIngo Molnar	interrupt do_IRQ
604905a36a2SIngo Molnar	/* 0(%rsp): old RSP */
605905a36a2SIngo Molnarret_from_intr:
6062140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
607905a36a2SIngo Molnar	TRACE_IRQS_OFF
608905a36a2SIngo Molnar
6091d3e53e8SAndy Lutomirski	LEAVE_IRQ_STACK
610905a36a2SIngo Molnar
611905a36a2SIngo Molnar	testb	$3, CS(%rsp)
612905a36a2SIngo Molnar	jz	retint_kernel
61302bc7768SAndy Lutomirski
614905a36a2SIngo Molnar	/* Interrupt came from user space */
61502bc7768SAndy LutomirskiGLOBAL(retint_user)
61602bc7768SAndy Lutomirski	mov	%rsp,%rdi
61702bc7768SAndy Lutomirski	call	prepare_exit_to_usermode
618905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
61926c4ef9cSAndy Lutomirski
6208a055d7fSAndy LutomirskiGLOBAL(swapgs_restore_regs_and_return_to_usermode)
62126c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
62226c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates user mode. */
62326c4ef9cSAndy Lutomirski	testl	$3, CS(%rsp)
62426c4ef9cSAndy Lutomirski	jnz	1f
62526c4ef9cSAndy Lutomirski	ud2
62626c4ef9cSAndy Lutomirski1:
62726c4ef9cSAndy Lutomirski#endif
6288a055d7fSAndy Lutomirski	SWAPGS
629e872045bSAndy Lutomirski	POP_EXTRA_REGS
630e872045bSAndy Lutomirski	POP_C_REGS
631e872045bSAndy Lutomirski	addq	$8, %rsp	/* skip regs->orig_ax */
63226c4ef9cSAndy Lutomirski	INTERRUPT_RETURN
63326c4ef9cSAndy Lutomirski
634905a36a2SIngo Molnar
635905a36a2SIngo Molnar/* Returning to kernel space */
636905a36a2SIngo Molnarretint_kernel:
637905a36a2SIngo Molnar#ifdef CONFIG_PREEMPT
638905a36a2SIngo Molnar	/* Interrupts are off */
639905a36a2SIngo Molnar	/* Check if we need preemption */
6404d732138SIngo Molnar	bt	$9, EFLAGS(%rsp)		/* were interrupts off? */
641905a36a2SIngo Molnar	jnc	1f
642905a36a2SIngo Molnar0:	cmpl	$0, PER_CPU_VAR(__preempt_count)
643905a36a2SIngo Molnar	jnz	1f
644905a36a2SIngo Molnar	call	preempt_schedule_irq
645905a36a2SIngo Molnar	jmp	0b
646905a36a2SIngo Molnar1:
647905a36a2SIngo Molnar#endif
648905a36a2SIngo Molnar	/*
649905a36a2SIngo Molnar	 * The iretq could re-enable interrupts:
650905a36a2SIngo Molnar	 */
651905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
652905a36a2SIngo Molnar
65326c4ef9cSAndy LutomirskiGLOBAL(restore_regs_and_return_to_kernel)
65426c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
65526c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates kernel mode. */
65626c4ef9cSAndy Lutomirski	testl	$3, CS(%rsp)
65726c4ef9cSAndy Lutomirski	jz	1f
65826c4ef9cSAndy Lutomirski	ud2
65926c4ef9cSAndy Lutomirski1:
66026c4ef9cSAndy Lutomirski#endif
661e872045bSAndy Lutomirski	POP_EXTRA_REGS
662e872045bSAndy Lutomirski	POP_C_REGS
663e872045bSAndy Lutomirski	addq	$8, %rsp	/* skip regs->orig_ax */
664905a36a2SIngo Molnar	INTERRUPT_RETURN
665905a36a2SIngo Molnar
666905a36a2SIngo MolnarENTRY(native_iret)
6678c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
668905a36a2SIngo Molnar	/*
669905a36a2SIngo Molnar	 * Are we returning to a stack segment from the LDT?  Note: in
670905a36a2SIngo Molnar	 * 64-bit mode SS:RSP on the exception stack is always valid.
671905a36a2SIngo Molnar	 */
672905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
673905a36a2SIngo Molnar	testb	$4, (SS-RIP)(%rsp)
674905a36a2SIngo Molnar	jnz	native_irq_return_ldt
675905a36a2SIngo Molnar#endif
676905a36a2SIngo Molnar
677905a36a2SIngo Molnar.global native_irq_return_iret
678905a36a2SIngo Molnarnative_irq_return_iret:
679905a36a2SIngo Molnar	/*
680905a36a2SIngo Molnar	 * This may fault.  Non-paranoid faults on return to userspace are
681905a36a2SIngo Molnar	 * handled by fixup_bad_iret.  These include #SS, #GP, and #NP.
682905a36a2SIngo Molnar	 * Double-faults due to espfix64 are handled in do_double_fault.
683905a36a2SIngo Molnar	 * Other faults here are fatal.
684905a36a2SIngo Molnar	 */
685905a36a2SIngo Molnar	iretq
686905a36a2SIngo Molnar
687905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
688905a36a2SIngo Molnarnative_irq_return_ldt:
68985063facSAndy Lutomirski	/*
69085063facSAndy Lutomirski	 * We are running with user GSBASE.  All GPRs contain their user
69185063facSAndy Lutomirski	 * values.  We have a percpu ESPFIX stack that is eight slots
69285063facSAndy Lutomirski	 * long (see ESPFIX_STACK_SIZE).  espfix_waddr points to the bottom
69385063facSAndy Lutomirski	 * of the ESPFIX stack.
69485063facSAndy Lutomirski	 *
69585063facSAndy Lutomirski	 * We clobber RAX and RDI in this code.  We stash RDI on the
69685063facSAndy Lutomirski	 * normal stack and RAX on the ESPFIX stack.
69785063facSAndy Lutomirski	 *
69885063facSAndy Lutomirski	 * The ESPFIX stack layout we set up looks like this:
69985063facSAndy Lutomirski	 *
70085063facSAndy Lutomirski	 * --- top of ESPFIX stack ---
70185063facSAndy Lutomirski	 * SS
70285063facSAndy Lutomirski	 * RSP
70385063facSAndy Lutomirski	 * RFLAGS
70485063facSAndy Lutomirski	 * CS
70585063facSAndy Lutomirski	 * RIP  <-- RSP points here when we're done
70685063facSAndy Lutomirski	 * RAX  <-- espfix_waddr points here
70785063facSAndy Lutomirski	 * --- bottom of ESPFIX stack ---
70885063facSAndy Lutomirski	 */
70985063facSAndy Lutomirski
71085063facSAndy Lutomirski	pushq	%rdi				/* Stash user RDI */
711905a36a2SIngo Molnar	SWAPGS
712905a36a2SIngo Molnar	movq	PER_CPU_VAR(espfix_waddr), %rdi
71385063facSAndy Lutomirski	movq	%rax, (0*8)(%rdi)		/* user RAX */
71485063facSAndy Lutomirski	movq	(1*8)(%rsp), %rax		/* user RIP */
715905a36a2SIngo Molnar	movq	%rax, (1*8)(%rdi)
71685063facSAndy Lutomirski	movq	(2*8)(%rsp), %rax		/* user CS */
717905a36a2SIngo Molnar	movq	%rax, (2*8)(%rdi)
71885063facSAndy Lutomirski	movq	(3*8)(%rsp), %rax		/* user RFLAGS */
719905a36a2SIngo Molnar	movq	%rax, (3*8)(%rdi)
72085063facSAndy Lutomirski	movq	(5*8)(%rsp), %rax		/* user SS */
721905a36a2SIngo Molnar	movq	%rax, (5*8)(%rdi)
72285063facSAndy Lutomirski	movq	(4*8)(%rsp), %rax		/* user RSP */
723905a36a2SIngo Molnar	movq	%rax, (4*8)(%rdi)
72485063facSAndy Lutomirski	/* Now RAX == RSP. */
72585063facSAndy Lutomirski
72685063facSAndy Lutomirski	andl	$0xffff0000, %eax		/* RAX = (RSP & 0xffff0000) */
72785063facSAndy Lutomirski	popq	%rdi				/* Restore user RDI */
72885063facSAndy Lutomirski
72985063facSAndy Lutomirski	/*
73085063facSAndy Lutomirski	 * espfix_stack[31:16] == 0.  The page tables are set up such that
73185063facSAndy Lutomirski	 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
73285063facSAndy Lutomirski	 * espfix_waddr for any X.  That is, there are 65536 RO aliases of
73385063facSAndy Lutomirski	 * the same page.  Set up RSP so that RSP[31:16] contains the
73485063facSAndy Lutomirski	 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
73585063facSAndy Lutomirski	 * still points to an RO alias of the ESPFIX stack.
73685063facSAndy Lutomirski	 */
737905a36a2SIngo Molnar	orq	PER_CPU_VAR(espfix_stack), %rax
738905a36a2SIngo Molnar	SWAPGS
739905a36a2SIngo Molnar	movq	%rax, %rsp
7408c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
74185063facSAndy Lutomirski
74285063facSAndy Lutomirski	/*
74385063facSAndy Lutomirski	 * At this point, we cannot write to the stack any more, but we can
74485063facSAndy Lutomirski	 * still read.
74585063facSAndy Lutomirski	 */
74685063facSAndy Lutomirski	popq	%rax				/* Restore user RAX */
74785063facSAndy Lutomirski
74885063facSAndy Lutomirski	/*
74985063facSAndy Lutomirski	 * RSP now points to an ordinary IRET frame, except that the page
75085063facSAndy Lutomirski	 * is read-only and RSP[31:16] are preloaded with the userspace
75185063facSAndy Lutomirski	 * values.  We can now IRET back to userspace.
75285063facSAndy Lutomirski	 */
753905a36a2SIngo Molnar	jmp	native_irq_return_iret
754905a36a2SIngo Molnar#endif
755905a36a2SIngo MolnarEND(common_interrupt)
756905a36a2SIngo Molnar
757905a36a2SIngo Molnar/*
758905a36a2SIngo Molnar * APIC interrupts.
759905a36a2SIngo Molnar */
760905a36a2SIngo Molnar.macro apicinterrupt3 num sym do_sym
761905a36a2SIngo MolnarENTRY(\sym)
7628c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
763905a36a2SIngo Molnar	ASM_CLAC
764905a36a2SIngo Molnar	pushq	$~(\num)
765905a36a2SIngo Molnar.Lcommon_\sym:
766905a36a2SIngo Molnar	interrupt \do_sym
767905a36a2SIngo Molnar	jmp	ret_from_intr
768905a36a2SIngo MolnarEND(\sym)
769905a36a2SIngo Molnar.endm
770905a36a2SIngo Molnar
771469f0023SAlexander Potapenko/* Make sure APIC interrupt handlers end up in the irqentry section: */
772469f0023SAlexander Potapenko#define PUSH_SECTION_IRQENTRY	.pushsection .irqentry.text, "ax"
773469f0023SAlexander Potapenko#define POP_SECTION_IRQENTRY	.popsection
774469f0023SAlexander Potapenko
775905a36a2SIngo Molnar.macro apicinterrupt num sym do_sym
776469f0023SAlexander PotapenkoPUSH_SECTION_IRQENTRY
777905a36a2SIngo Molnarapicinterrupt3 \num \sym \do_sym
778469f0023SAlexander PotapenkoPOP_SECTION_IRQENTRY
779905a36a2SIngo Molnar.endm
780905a36a2SIngo Molnar
781905a36a2SIngo Molnar#ifdef CONFIG_SMP
7824d732138SIngo Molnarapicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR		irq_move_cleanup_interrupt	smp_irq_move_cleanup_interrupt
7834d732138SIngo Molnarapicinterrupt3 REBOOT_VECTOR			reboot_interrupt		smp_reboot_interrupt
784905a36a2SIngo Molnar#endif
785905a36a2SIngo Molnar
786905a36a2SIngo Molnar#ifdef CONFIG_X86_UV
7874d732138SIngo Molnarapicinterrupt3 UV_BAU_MESSAGE			uv_bau_message_intr1		uv_bau_message_interrupt
788905a36a2SIngo Molnar#endif
7894d732138SIngo Molnar
7904d732138SIngo Molnarapicinterrupt LOCAL_TIMER_VECTOR		apic_timer_interrupt		smp_apic_timer_interrupt
7914d732138SIngo Molnarapicinterrupt X86_PLATFORM_IPI_VECTOR		x86_platform_ipi		smp_x86_platform_ipi
792905a36a2SIngo Molnar
793905a36a2SIngo Molnar#ifdef CONFIG_HAVE_KVM
7944d732138SIngo Molnarapicinterrupt3 POSTED_INTR_VECTOR		kvm_posted_intr_ipi		smp_kvm_posted_intr_ipi
7954d732138SIngo Molnarapicinterrupt3 POSTED_INTR_WAKEUP_VECTOR	kvm_posted_intr_wakeup_ipi	smp_kvm_posted_intr_wakeup_ipi
796210f84b0SWincy Vanapicinterrupt3 POSTED_INTR_NESTED_VECTOR	kvm_posted_intr_nested_ipi	smp_kvm_posted_intr_nested_ipi
797905a36a2SIngo Molnar#endif
798905a36a2SIngo Molnar
799905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE_THRESHOLD
8004d732138SIngo Molnarapicinterrupt THRESHOLD_APIC_VECTOR		threshold_interrupt		smp_threshold_interrupt
801905a36a2SIngo Molnar#endif
802905a36a2SIngo Molnar
8039dda1658SIngo Molnar#ifdef CONFIG_X86_MCE_AMD
8044d732138SIngo Molnarapicinterrupt DEFERRED_ERROR_VECTOR		deferred_error_interrupt	smp_deferred_error_interrupt
8059dda1658SIngo Molnar#endif
8069dda1658SIngo Molnar
807905a36a2SIngo Molnar#ifdef CONFIG_X86_THERMAL_VECTOR
8084d732138SIngo Molnarapicinterrupt THERMAL_APIC_VECTOR		thermal_interrupt		smp_thermal_interrupt
809905a36a2SIngo Molnar#endif
810905a36a2SIngo Molnar
811905a36a2SIngo Molnar#ifdef CONFIG_SMP
8124d732138SIngo Molnarapicinterrupt CALL_FUNCTION_SINGLE_VECTOR	call_function_single_interrupt	smp_call_function_single_interrupt
8134d732138SIngo Molnarapicinterrupt CALL_FUNCTION_VECTOR		call_function_interrupt		smp_call_function_interrupt
8144d732138SIngo Molnarapicinterrupt RESCHEDULE_VECTOR			reschedule_interrupt		smp_reschedule_interrupt
815905a36a2SIngo Molnar#endif
816905a36a2SIngo Molnar
8174d732138SIngo Molnarapicinterrupt ERROR_APIC_VECTOR			error_interrupt			smp_error_interrupt
8184d732138SIngo Molnarapicinterrupt SPURIOUS_APIC_VECTOR		spurious_interrupt		smp_spurious_interrupt
819905a36a2SIngo Molnar
820905a36a2SIngo Molnar#ifdef CONFIG_IRQ_WORK
8214d732138SIngo Molnarapicinterrupt IRQ_WORK_VECTOR			irq_work_interrupt		smp_irq_work_interrupt
822905a36a2SIngo Molnar#endif
823905a36a2SIngo Molnar
824905a36a2SIngo Molnar/*
825905a36a2SIngo Molnar * Exception entry points.
826905a36a2SIngo Molnar */
827905a36a2SIngo Molnar#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
828905a36a2SIngo Molnar
829905a36a2SIngo Molnar.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
830905a36a2SIngo MolnarENTRY(\sym)
83198990a33SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=\has_error_code*8
8328c1f7558SJosh Poimboeuf
833905a36a2SIngo Molnar	/* Sanity check */
834905a36a2SIngo Molnar	.if \shift_ist != -1 && \paranoid == 0
835905a36a2SIngo Molnar	.error "using shift_ist requires paranoid=1"
836905a36a2SIngo Molnar	.endif
837905a36a2SIngo Molnar
838905a36a2SIngo Molnar	ASM_CLAC
839905a36a2SIngo Molnar
84082c62fa0SJosh Poimboeuf	.if \has_error_code == 0
841905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
842905a36a2SIngo Molnar	.endif
843905a36a2SIngo Molnar
844905a36a2SIngo Molnar	ALLOC_PT_GPREGS_ON_STACK
845905a36a2SIngo Molnar
846905a36a2SIngo Molnar	.if \paranoid
847905a36a2SIngo Molnar	.if \paranoid == 1
8484d732138SIngo Molnar	testb	$3, CS(%rsp)			/* If coming from userspace, switch stacks */
8494d732138SIngo Molnar	jnz	1f
850905a36a2SIngo Molnar	.endif
851905a36a2SIngo Molnar	call	paranoid_entry
852905a36a2SIngo Molnar	.else
853905a36a2SIngo Molnar	call	error_entry
854905a36a2SIngo Molnar	.endif
8558c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
856905a36a2SIngo Molnar	/* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
857905a36a2SIngo Molnar
858905a36a2SIngo Molnar	.if \paranoid
859905a36a2SIngo Molnar	.if \shift_ist != -1
860905a36a2SIngo Molnar	TRACE_IRQS_OFF_DEBUG			/* reload IDT in case of recursion */
861905a36a2SIngo Molnar	.else
862905a36a2SIngo Molnar	TRACE_IRQS_OFF
863905a36a2SIngo Molnar	.endif
864905a36a2SIngo Molnar	.endif
865905a36a2SIngo Molnar
866905a36a2SIngo Molnar	movq	%rsp, %rdi			/* pt_regs pointer */
867905a36a2SIngo Molnar
868905a36a2SIngo Molnar	.if \has_error_code
869905a36a2SIngo Molnar	movq	ORIG_RAX(%rsp), %rsi		/* get error code */
870905a36a2SIngo Molnar	movq	$-1, ORIG_RAX(%rsp)		/* no syscall to restart */
871905a36a2SIngo Molnar	.else
872905a36a2SIngo Molnar	xorl	%esi, %esi			/* no error code */
873905a36a2SIngo Molnar	.endif
874905a36a2SIngo Molnar
875905a36a2SIngo Molnar	.if \shift_ist != -1
876905a36a2SIngo Molnar	subq	$EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
877905a36a2SIngo Molnar	.endif
878905a36a2SIngo Molnar
879905a36a2SIngo Molnar	call	\do_sym
880905a36a2SIngo Molnar
881905a36a2SIngo Molnar	.if \shift_ist != -1
882905a36a2SIngo Molnar	addq	$EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
883905a36a2SIngo Molnar	.endif
884905a36a2SIngo Molnar
885905a36a2SIngo Molnar	/* these procedures expect "no swapgs" flag in ebx */
886905a36a2SIngo Molnar	.if \paranoid
887905a36a2SIngo Molnar	jmp	paranoid_exit
888905a36a2SIngo Molnar	.else
889905a36a2SIngo Molnar	jmp	error_exit
890905a36a2SIngo Molnar	.endif
891905a36a2SIngo Molnar
892905a36a2SIngo Molnar	.if \paranoid == 1
893905a36a2SIngo Molnar	/*
894905a36a2SIngo Molnar	 * Paranoid entry from userspace.  Switch stacks and treat it
895905a36a2SIngo Molnar	 * as a normal entry.  This means that paranoid handlers
896905a36a2SIngo Molnar	 * run in real process context if user_mode(regs).
897905a36a2SIngo Molnar	 */
898905a36a2SIngo Molnar1:
899905a36a2SIngo Molnar	call	error_entry
900905a36a2SIngo Molnar
901905a36a2SIngo Molnar
902905a36a2SIngo Molnar	movq	%rsp, %rdi			/* pt_regs pointer */
903905a36a2SIngo Molnar	call	sync_regs
904905a36a2SIngo Molnar	movq	%rax, %rsp			/* switch stack */
905905a36a2SIngo Molnar
906905a36a2SIngo Molnar	movq	%rsp, %rdi			/* pt_regs pointer */
907905a36a2SIngo Molnar
908905a36a2SIngo Molnar	.if \has_error_code
909905a36a2SIngo Molnar	movq	ORIG_RAX(%rsp), %rsi		/* get error code */
910905a36a2SIngo Molnar	movq	$-1, ORIG_RAX(%rsp)		/* no syscall to restart */
911905a36a2SIngo Molnar	.else
912905a36a2SIngo Molnar	xorl	%esi, %esi			/* no error code */
913905a36a2SIngo Molnar	.endif
914905a36a2SIngo Molnar
915905a36a2SIngo Molnar	call	\do_sym
916905a36a2SIngo Molnar
917905a36a2SIngo Molnar	jmp	error_exit			/* %ebx: no swapgs flag */
918905a36a2SIngo Molnar	.endif
919905a36a2SIngo MolnarEND(\sym)
920905a36a2SIngo Molnar.endm
921905a36a2SIngo Molnar
922905a36a2SIngo Molnaridtentry divide_error			do_divide_error			has_error_code=0
923905a36a2SIngo Molnaridtentry overflow			do_overflow			has_error_code=0
924905a36a2SIngo Molnaridtentry bounds				do_bounds			has_error_code=0
925905a36a2SIngo Molnaridtentry invalid_op			do_invalid_op			has_error_code=0
926905a36a2SIngo Molnaridtentry device_not_available		do_device_not_available		has_error_code=0
927905a36a2SIngo Molnaridtentry double_fault			do_double_fault			has_error_code=1 paranoid=2
928905a36a2SIngo Molnaridtentry coprocessor_segment_overrun	do_coprocessor_segment_overrun	has_error_code=0
929905a36a2SIngo Molnaridtentry invalid_TSS			do_invalid_TSS			has_error_code=1
930905a36a2SIngo Molnaridtentry segment_not_present		do_segment_not_present		has_error_code=1
931905a36a2SIngo Molnaridtentry spurious_interrupt_bug		do_spurious_interrupt_bug	has_error_code=0
932905a36a2SIngo Molnaridtentry coprocessor_error		do_coprocessor_error		has_error_code=0
933905a36a2SIngo Molnaridtentry alignment_check		do_alignment_check		has_error_code=1
934905a36a2SIngo Molnaridtentry simd_coprocessor_error		do_simd_coprocessor_error	has_error_code=0
935905a36a2SIngo Molnar
936905a36a2SIngo Molnar
9374d732138SIngo Molnar	/*
9384d732138SIngo Molnar	 * Reload gs selector with exception handling
9394d732138SIngo Molnar	 * edi:  new selector
9404d732138SIngo Molnar	 */
941905a36a2SIngo MolnarENTRY(native_load_gs_index)
9428c1f7558SJosh Poimboeuf	FRAME_BEGIN
943905a36a2SIngo Molnar	pushfq
944905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
945905a36a2SIngo Molnar	SWAPGS
94642c748bbSBorislav Petkov.Lgs_change:
947905a36a2SIngo Molnar	movl	%edi, %gs
94896e5d28aSBorislav Petkov2:	ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
949905a36a2SIngo Molnar	SWAPGS
950905a36a2SIngo Molnar	popfq
9518c1f7558SJosh Poimboeuf	FRAME_END
952905a36a2SIngo Molnar	ret
9538c1f7558SJosh PoimboeufENDPROC(native_load_gs_index)
954784d5699SAl ViroEXPORT_SYMBOL(native_load_gs_index)
955905a36a2SIngo Molnar
95642c748bbSBorislav Petkov	_ASM_EXTABLE(.Lgs_change, bad_gs)
957905a36a2SIngo Molnar	.section .fixup, "ax"
958905a36a2SIngo Molnar	/* running with kernelgs */
959905a36a2SIngo Molnarbad_gs:
960905a36a2SIngo Molnar	SWAPGS					/* switch back to user gs */
961b038c842SAndy Lutomirski.macro ZAP_GS
962b038c842SAndy Lutomirski	/* This can't be a string because the preprocessor needs to see it. */
963b038c842SAndy Lutomirski	movl $__USER_DS, %eax
964b038c842SAndy Lutomirski	movl %eax, %gs
965b038c842SAndy Lutomirski.endm
966b038c842SAndy Lutomirski	ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
967905a36a2SIngo Molnar	xorl	%eax, %eax
968905a36a2SIngo Molnar	movl	%eax, %gs
969905a36a2SIngo Molnar	jmp	2b
970905a36a2SIngo Molnar	.previous
971905a36a2SIngo Molnar
972905a36a2SIngo Molnar/* Call softirq on interrupt stack. Interrupts are off. */
973905a36a2SIngo MolnarENTRY(do_softirq_own_stack)
974905a36a2SIngo Molnar	pushq	%rbp
975905a36a2SIngo Molnar	mov	%rsp, %rbp
9768c1f7558SJosh Poimboeuf	ENTER_IRQ_STACK regs=0 old_rsp=%r11
977905a36a2SIngo Molnar	call	__do_softirq
9788c1f7558SJosh Poimboeuf	LEAVE_IRQ_STACK regs=0
979905a36a2SIngo Molnar	leaveq
980905a36a2SIngo Molnar	ret
9818c1f7558SJosh PoimboeufENDPROC(do_softirq_own_stack)
982905a36a2SIngo Molnar
983905a36a2SIngo Molnar#ifdef CONFIG_XEN
9845878d5d6SJuergen Grossidtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0
985905a36a2SIngo Molnar
986905a36a2SIngo Molnar/*
987905a36a2SIngo Molnar * A note on the "critical region" in our callback handler.
988905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring
989905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled
990905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before
991905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still
992905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack.
993905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd
994905a36a2SIngo Molnar * like to avoid the possibility.
995905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an
996905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current
997905a36a2SIngo Molnar * activation and restart the handler using the previous one.
998905a36a2SIngo Molnar */
9994d732138SIngo MolnarENTRY(xen_do_hypervisor_callback)		/* do_hypervisor_callback(struct *pt_regs) */
10004d732138SIngo Molnar
1001905a36a2SIngo Molnar/*
1002905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1003905a36a2SIngo Molnar * see the correct pointer to the pt_regs
1004905a36a2SIngo Molnar */
10058c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
10064d732138SIngo Molnar	movq	%rdi, %rsp			/* we don't return, adjust the stack frame */
10078c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
10081d3e53e8SAndy Lutomirski
10091d3e53e8SAndy Lutomirski	ENTER_IRQ_STACK old_rsp=%r10
1010905a36a2SIngo Molnar	call	xen_evtchn_do_upcall
10111d3e53e8SAndy Lutomirski	LEAVE_IRQ_STACK
10121d3e53e8SAndy Lutomirski
1013905a36a2SIngo Molnar#ifndef CONFIG_PREEMPT
1014905a36a2SIngo Molnar	call	xen_maybe_preempt_hcall
1015905a36a2SIngo Molnar#endif
1016905a36a2SIngo Molnar	jmp	error_exit
1017905a36a2SIngo MolnarEND(xen_do_hypervisor_callback)
1018905a36a2SIngo Molnar
1019905a36a2SIngo Molnar/*
1020905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes.
1021905a36a2SIngo Molnar * We get here for two reasons:
1022905a36a2SIngo Molnar *  1. Fault while reloading DS, ES, FS or GS
1023905a36a2SIngo Molnar *  2. Fault while executing IRET
1024905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment
1025905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others.
1026905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the
1027905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall
1028905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1029905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register
1030905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1.
1031905a36a2SIngo Molnar */
1032905a36a2SIngo MolnarENTRY(xen_failsafe_callback)
10338c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
1034905a36a2SIngo Molnar	movl	%ds, %ecx
1035905a36a2SIngo Molnar	cmpw	%cx, 0x10(%rsp)
1036905a36a2SIngo Molnar	jne	1f
1037905a36a2SIngo Molnar	movl	%es, %ecx
1038905a36a2SIngo Molnar	cmpw	%cx, 0x18(%rsp)
1039905a36a2SIngo Molnar	jne	1f
1040905a36a2SIngo Molnar	movl	%fs, %ecx
1041905a36a2SIngo Molnar	cmpw	%cx, 0x20(%rsp)
1042905a36a2SIngo Molnar	jne	1f
1043905a36a2SIngo Molnar	movl	%gs, %ecx
1044905a36a2SIngo Molnar	cmpw	%cx, 0x28(%rsp)
1045905a36a2SIngo Molnar	jne	1f
1046905a36a2SIngo Molnar	/* All segments match their saved values => Category 2 (Bad IRET). */
1047905a36a2SIngo Molnar	movq	(%rsp), %rcx
1048905a36a2SIngo Molnar	movq	8(%rsp), %r11
1049905a36a2SIngo Molnar	addq	$0x30, %rsp
1050905a36a2SIngo Molnar	pushq	$0				/* RIP */
10518c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
1052905a36a2SIngo Molnar	jmp	general_protection
1053905a36a2SIngo Molnar1:	/* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1054905a36a2SIngo Molnar	movq	(%rsp), %rcx
1055905a36a2SIngo Molnar	movq	8(%rsp), %r11
1056905a36a2SIngo Molnar	addq	$0x30, %rsp
10578c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1058905a36a2SIngo Molnar	pushq	$-1 /* orig_ax = -1 => not a system call */
1059905a36a2SIngo Molnar	ALLOC_PT_GPREGS_ON_STACK
1060905a36a2SIngo Molnar	SAVE_C_REGS
1061905a36a2SIngo Molnar	SAVE_EXTRA_REGS
1062946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
1063905a36a2SIngo Molnar	jmp	error_exit
1064905a36a2SIngo MolnarEND(xen_failsafe_callback)
1065905a36a2SIngo Molnar
1066905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1067905a36a2SIngo Molnar	xen_hvm_callback_vector xen_evtchn_do_upcall
1068905a36a2SIngo Molnar
1069905a36a2SIngo Molnar#endif /* CONFIG_XEN */
1070905a36a2SIngo Molnar
1071905a36a2SIngo Molnar#if IS_ENABLED(CONFIG_HYPERV)
1072905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1073905a36a2SIngo Molnar	hyperv_callback_vector hyperv_vector_handler
1074905a36a2SIngo Molnar#endif /* CONFIG_HYPERV */
1075905a36a2SIngo Molnar
1076905a36a2SIngo Molnaridtentry debug			do_debug		has_error_code=0	paranoid=1 shift_ist=DEBUG_STACK
1077905a36a2SIngo Molnaridtentry int3			do_int3			has_error_code=0	paranoid=1 shift_ist=DEBUG_STACK
1078905a36a2SIngo Molnaridtentry stack_segment		do_stack_segment	has_error_code=1
10794d732138SIngo Molnar
1080905a36a2SIngo Molnar#ifdef CONFIG_XEN
10815878d5d6SJuergen Grossidtentry xendebug		do_debug		has_error_code=0
10825878d5d6SJuergen Grossidtentry xenint3		do_int3			has_error_code=0
1083905a36a2SIngo Molnar#endif
10844d732138SIngo Molnar
1085905a36a2SIngo Molnaridtentry general_protection	do_general_protection	has_error_code=1
108611a7ffb0SThomas Gleixneridtentry page_fault		do_page_fault		has_error_code=1
10874d732138SIngo Molnar
1088905a36a2SIngo Molnar#ifdef CONFIG_KVM_GUEST
1089905a36a2SIngo Molnaridtentry async_page_fault	do_async_page_fault	has_error_code=1
1090905a36a2SIngo Molnar#endif
10914d732138SIngo Molnar
1092905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE
1093905a36a2SIngo Molnaridtentry machine_check					has_error_code=0	paranoid=1 do_sym=*machine_check_vector(%rip)
1094905a36a2SIngo Molnar#endif
1095905a36a2SIngo Molnar
1096905a36a2SIngo Molnar/*
1097905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed.
1098905a36a2SIngo Molnar * Use slow, but surefire "are we in kernel?" check.
1099905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1100905a36a2SIngo Molnar */
1101905a36a2SIngo MolnarENTRY(paranoid_entry)
11028c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
1103905a36a2SIngo Molnar	cld
1104905a36a2SIngo Molnar	SAVE_C_REGS 8
1105905a36a2SIngo Molnar	SAVE_EXTRA_REGS 8
1106946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER 8
1107905a36a2SIngo Molnar	movl	$1, %ebx
1108905a36a2SIngo Molnar	movl	$MSR_GS_BASE, %ecx
1109905a36a2SIngo Molnar	rdmsr
1110905a36a2SIngo Molnar	testl	%edx, %edx
1111905a36a2SIngo Molnar	js	1f				/* negative -> in kernel */
1112905a36a2SIngo Molnar	SWAPGS
1113905a36a2SIngo Molnar	xorl	%ebx, %ebx
1114905a36a2SIngo Molnar1:	ret
1115905a36a2SIngo MolnarEND(paranoid_entry)
1116905a36a2SIngo Molnar
1117905a36a2SIngo Molnar/*
1118905a36a2SIngo Molnar * "Paranoid" exit path from exception stack.  This is invoked
1119905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came
1120905a36a2SIngo Molnar * from kernel space.
1121905a36a2SIngo Molnar *
1122905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early
1123905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would
1124905a36a2SIngo Molnar * be complicated.  Fortunately, we there's no good reason
1125905a36a2SIngo Molnar * to try to handle preemption here.
11264d732138SIngo Molnar *
11274d732138SIngo Molnar * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
1128905a36a2SIngo Molnar */
1129905a36a2SIngo MolnarENTRY(paranoid_exit)
11308c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
11312140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
1132905a36a2SIngo Molnar	TRACE_IRQS_OFF_DEBUG
1133905a36a2SIngo Molnar	testl	%ebx, %ebx			/* swapgs needed? */
1134e5317832SAndy Lutomirski	jnz	.Lparanoid_exit_no_swapgs
1135905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
1136905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
1137e5317832SAndy Lutomirski	jmp	.Lparanoid_exit_restore
1138e5317832SAndy Lutomirski.Lparanoid_exit_no_swapgs:
1139905a36a2SIngo Molnar	TRACE_IRQS_IRETQ_DEBUG
1140e5317832SAndy Lutomirski.Lparanoid_exit_restore:
1141e5317832SAndy Lutomirski	jmp restore_regs_and_return_to_kernel
1142905a36a2SIngo MolnarEND(paranoid_exit)
1143905a36a2SIngo Molnar
1144905a36a2SIngo Molnar/*
1145905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed.
1146539f5113SAndy Lutomirski * Return: EBX=0: came from user mode; EBX=1: otherwise
1147905a36a2SIngo Molnar */
1148905a36a2SIngo MolnarENTRY(error_entry)
11498c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
1150905a36a2SIngo Molnar	cld
1151905a36a2SIngo Molnar	SAVE_C_REGS 8
1152905a36a2SIngo Molnar	SAVE_EXTRA_REGS 8
1153946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER 8
1154905a36a2SIngo Molnar	xorl	%ebx, %ebx
1155905a36a2SIngo Molnar	testb	$3, CS+8(%rsp)
1156cb6f64edSAndy Lutomirski	jz	.Lerror_kernelspace
1157539f5113SAndy Lutomirski
1158cb6f64edSAndy Lutomirski	/*
1159cb6f64edSAndy Lutomirski	 * We entered from user mode or we're pretending to have entered
1160cb6f64edSAndy Lutomirski	 * from user mode due to an IRET fault.
1161cb6f64edSAndy Lutomirski	 */
1162905a36a2SIngo Molnar	SWAPGS
1163539f5113SAndy Lutomirski
1164cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_after_swapgs:
1165f1075053SAndy Lutomirski	/*
1166f1075053SAndy Lutomirski	 * We need to tell lockdep that IRQs are off.  We can't do this until
1167f1075053SAndy Lutomirski	 * we fix gsbase, and we should do it before enter_from_user_mode
1168f1075053SAndy Lutomirski	 * (which can take locks).
1169f1075053SAndy Lutomirski	 */
1170f1075053SAndy Lutomirski	TRACE_IRQS_OFF
1171478dc89cSAndy Lutomirski	CALL_enter_from_user_mode
1172f1075053SAndy Lutomirski	ret
117302bc7768SAndy Lutomirski
1174cb6f64edSAndy Lutomirski.Lerror_entry_done:
1175905a36a2SIngo Molnar	TRACE_IRQS_OFF
1176905a36a2SIngo Molnar	ret
1177905a36a2SIngo Molnar
1178905a36a2SIngo Molnar	/*
1179905a36a2SIngo Molnar	 * There are two places in the kernel that can potentially fault with
1180905a36a2SIngo Molnar	 * usergs. Handle them here.  B stepping K8s sometimes report a
1181905a36a2SIngo Molnar	 * truncated RIP for IRET exceptions returning to compat mode. Check
1182905a36a2SIngo Molnar	 * for these here too.
1183905a36a2SIngo Molnar	 */
1184cb6f64edSAndy Lutomirski.Lerror_kernelspace:
1185905a36a2SIngo Molnar	incl	%ebx
1186905a36a2SIngo Molnar	leaq	native_irq_return_iret(%rip), %rcx
1187905a36a2SIngo Molnar	cmpq	%rcx, RIP+8(%rsp)
1188cb6f64edSAndy Lutomirski	je	.Lerror_bad_iret
1189905a36a2SIngo Molnar	movl	%ecx, %eax			/* zero extend */
1190905a36a2SIngo Molnar	cmpq	%rax, RIP+8(%rsp)
1191cb6f64edSAndy Lutomirski	je	.Lbstep_iret
119242c748bbSBorislav Petkov	cmpq	$.Lgs_change, RIP+8(%rsp)
1193cb6f64edSAndy Lutomirski	jne	.Lerror_entry_done
1194539f5113SAndy Lutomirski
1195539f5113SAndy Lutomirski	/*
119642c748bbSBorislav Petkov	 * hack: .Lgs_change can fail with user gsbase.  If this happens, fix up
1197539f5113SAndy Lutomirski	 * gsbase and proceed.  We'll fix up the exception and land in
119842c748bbSBorislav Petkov	 * .Lgs_change's error handler with kernel gsbase.
1199539f5113SAndy Lutomirski	 */
12002fa5f04fSWanpeng Li	SWAPGS
12012fa5f04fSWanpeng Li	jmp .Lerror_entry_done
1202905a36a2SIngo Molnar
1203cb6f64edSAndy Lutomirski.Lbstep_iret:
1204905a36a2SIngo Molnar	/* Fix truncated RIP */
1205905a36a2SIngo Molnar	movq	%rcx, RIP+8(%rsp)
1206905a36a2SIngo Molnar	/* fall through */
1207905a36a2SIngo Molnar
1208cb6f64edSAndy Lutomirski.Lerror_bad_iret:
1209539f5113SAndy Lutomirski	/*
1210539f5113SAndy Lutomirski	 * We came from an IRET to user mode, so we have user gsbase.
1211539f5113SAndy Lutomirski	 * Switch to kernel gsbase:
1212539f5113SAndy Lutomirski	 */
1213905a36a2SIngo Molnar	SWAPGS
1214539f5113SAndy Lutomirski
1215539f5113SAndy Lutomirski	/*
1216539f5113SAndy Lutomirski	 * Pretend that the exception came from user mode: set up pt_regs
1217539f5113SAndy Lutomirski	 * as if we faulted immediately after IRET and clear EBX so that
1218539f5113SAndy Lutomirski	 * error_exit knows that we will be returning to user mode.
1219539f5113SAndy Lutomirski	 */
1220905a36a2SIngo Molnar	mov	%rsp, %rdi
1221905a36a2SIngo Molnar	call	fixup_bad_iret
1222905a36a2SIngo Molnar	mov	%rax, %rsp
1223539f5113SAndy Lutomirski	decl	%ebx
1224cb6f64edSAndy Lutomirski	jmp	.Lerror_entry_from_usermode_after_swapgs
1225905a36a2SIngo MolnarEND(error_entry)
1226905a36a2SIngo Molnar
1227905a36a2SIngo Molnar
1228539f5113SAndy Lutomirski/*
122975ca5b22SNicolas Iooss * On entry, EBX is a "return to kernel mode" flag:
1230539f5113SAndy Lutomirski *   1: already in kernel mode, don't need SWAPGS
1231539f5113SAndy Lutomirski *   0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
1232539f5113SAndy Lutomirski */
1233905a36a2SIngo MolnarENTRY(error_exit)
12348c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
12352140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
1236905a36a2SIngo Molnar	TRACE_IRQS_OFF
12372140a994SJan Beulich	testl	%ebx, %ebx
1238905a36a2SIngo Molnar	jnz	retint_kernel
1239905a36a2SIngo Molnar	jmp	retint_user
1240905a36a2SIngo MolnarEND(error_exit)
1241905a36a2SIngo Molnar
1242905a36a2SIngo Molnar/* Runs on exception stack */
12435878d5d6SJuergen Gross/* XXX: broken on Xen PV */
1244905a36a2SIngo MolnarENTRY(nmi)
12458c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1246fc57a7c6SAndy Lutomirski	/*
1247905a36a2SIngo Molnar	 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1248905a36a2SIngo Molnar	 * the iretq it performs will take us out of NMI context.
1249905a36a2SIngo Molnar	 * This means that we can have nested NMIs where the next
1250905a36a2SIngo Molnar	 * NMI is using the top of the stack of the previous NMI. We
1251905a36a2SIngo Molnar	 * can't let it execute because the nested NMI will corrupt the
1252905a36a2SIngo Molnar	 * stack of the previous NMI. NMI handlers are not re-entrant
1253905a36a2SIngo Molnar	 * anyway.
1254905a36a2SIngo Molnar	 *
1255905a36a2SIngo Molnar	 * To handle this case we do the following:
1256905a36a2SIngo Molnar	 *  Check the a special location on the stack that contains
1257905a36a2SIngo Molnar	 *  a variable that is set when NMIs are executing.
1258905a36a2SIngo Molnar	 *  The interrupted task's stack is also checked to see if it
1259905a36a2SIngo Molnar	 *  is an NMI stack.
1260905a36a2SIngo Molnar	 *  If the variable is not set and the stack is not the NMI
1261905a36a2SIngo Molnar	 *  stack then:
1262905a36a2SIngo Molnar	 *    o Set the special variable on the stack
12630b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "outermost" location on the
12640b22930eSAndy Lutomirski	 *      stack
12650b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "iret" location on the stack
1266905a36a2SIngo Molnar	 *    o Continue processing the NMI
1267905a36a2SIngo Molnar	 *  If the variable is set or the previous stack is the NMI stack:
12680b22930eSAndy Lutomirski	 *    o Modify the "iret" location to jump to the repeat_nmi
1269905a36a2SIngo Molnar	 *    o return back to the first NMI
1270905a36a2SIngo Molnar	 *
1271905a36a2SIngo Molnar	 * Now on exit of the first NMI, we first clear the stack variable
1272905a36a2SIngo Molnar	 * The NMI stack will tell any nested NMIs at that point that it is
1273905a36a2SIngo Molnar	 * nested. Then we pop the stack normally with iret, and if there was
1274905a36a2SIngo Molnar	 * a nested NMI that updated the copy interrupt stack frame, a
1275905a36a2SIngo Molnar	 * jump will be made to the repeat_nmi code that will handle the second
1276905a36a2SIngo Molnar	 * NMI.
12779b6e6a83SAndy Lutomirski	 *
12789b6e6a83SAndy Lutomirski	 * However, espfix prevents us from directly returning to userspace
12799b6e6a83SAndy Lutomirski	 * with a single IRET instruction.  Similarly, IRET to user mode
12809b6e6a83SAndy Lutomirski	 * can fault.  We therefore handle NMIs from user space like
12819b6e6a83SAndy Lutomirski	 * other IST entries.
1282905a36a2SIngo Molnar	 */
1283905a36a2SIngo Molnar
1284e93c1730SAndy Lutomirski	ASM_CLAC
1285e93c1730SAndy Lutomirski
1286905a36a2SIngo Molnar	/* Use %rdx as our temp variable throughout */
1287905a36a2SIngo Molnar	pushq	%rdx
1288905a36a2SIngo Molnar
12899b6e6a83SAndy Lutomirski	testb	$3, CS-RIP+8(%rsp)
12909b6e6a83SAndy Lutomirski	jz	.Lnmi_from_kernel
1291905a36a2SIngo Molnar
1292905a36a2SIngo Molnar	/*
12939b6e6a83SAndy Lutomirski	 * NMI from user mode.  We need to run on the thread stack, but we
12949b6e6a83SAndy Lutomirski	 * can't go through the normal entry paths: NMIs are masked, and
12959b6e6a83SAndy Lutomirski	 * we don't want to enable interrupts, because then we'll end
12969b6e6a83SAndy Lutomirski	 * up in an awkward situation in which IRQs are on but NMIs
12979b6e6a83SAndy Lutomirski	 * are off.
129883c133cfSAndy Lutomirski	 *
129983c133cfSAndy Lutomirski	 * We also must not push anything to the stack before switching
130083c133cfSAndy Lutomirski	 * stacks lest we corrupt the "NMI executing" variable.
13019b6e6a83SAndy Lutomirski	 */
13029b6e6a83SAndy Lutomirski
130383c133cfSAndy Lutomirski	SWAPGS_UNSAFE_STACK
13049b6e6a83SAndy Lutomirski	cld
13059b6e6a83SAndy Lutomirski	movq	%rsp, %rdx
13069b6e6a83SAndy Lutomirski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
13078c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS base=%rdx offset=8
13089b6e6a83SAndy Lutomirski	pushq	5*8(%rdx)	/* pt_regs->ss */
13099b6e6a83SAndy Lutomirski	pushq	4*8(%rdx)	/* pt_regs->rsp */
13109b6e6a83SAndy Lutomirski	pushq	3*8(%rdx)	/* pt_regs->flags */
13119b6e6a83SAndy Lutomirski	pushq	2*8(%rdx)	/* pt_regs->cs */
13129b6e6a83SAndy Lutomirski	pushq	1*8(%rdx)	/* pt_regs->rip */
13138c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
13149b6e6a83SAndy Lutomirski	pushq   $-1		/* pt_regs->orig_ax */
13159b6e6a83SAndy Lutomirski	pushq   %rdi		/* pt_regs->di */
13169b6e6a83SAndy Lutomirski	pushq   %rsi		/* pt_regs->si */
13179b6e6a83SAndy Lutomirski	pushq   (%rdx)		/* pt_regs->dx */
13189b6e6a83SAndy Lutomirski	pushq   %rcx		/* pt_regs->cx */
13199b6e6a83SAndy Lutomirski	pushq   %rax		/* pt_regs->ax */
13209b6e6a83SAndy Lutomirski	pushq   %r8		/* pt_regs->r8 */
13219b6e6a83SAndy Lutomirski	pushq   %r9		/* pt_regs->r9 */
13229b6e6a83SAndy Lutomirski	pushq   %r10		/* pt_regs->r10 */
13239b6e6a83SAndy Lutomirski	pushq   %r11		/* pt_regs->r11 */
13249b6e6a83SAndy Lutomirski	pushq	%rbx		/* pt_regs->rbx */
13259b6e6a83SAndy Lutomirski	pushq	%rbp		/* pt_regs->rbp */
13269b6e6a83SAndy Lutomirski	pushq	%r12		/* pt_regs->r12 */
13279b6e6a83SAndy Lutomirski	pushq	%r13		/* pt_regs->r13 */
13289b6e6a83SAndy Lutomirski	pushq	%r14		/* pt_regs->r14 */
13299b6e6a83SAndy Lutomirski	pushq	%r15		/* pt_regs->r15 */
13308c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
1331946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
13329b6e6a83SAndy Lutomirski
13339b6e6a83SAndy Lutomirski	/*
13349b6e6a83SAndy Lutomirski	 * At this point we no longer need to worry about stack damage
13359b6e6a83SAndy Lutomirski	 * due to nesting -- we're on the normal thread stack and we're
13369b6e6a83SAndy Lutomirski	 * done with the NMI stack.
13379b6e6a83SAndy Lutomirski	 */
13389b6e6a83SAndy Lutomirski
13399b6e6a83SAndy Lutomirski	movq	%rsp, %rdi
13409b6e6a83SAndy Lutomirski	movq	$-1, %rsi
13419b6e6a83SAndy Lutomirski	call	do_nmi
13429b6e6a83SAndy Lutomirski
13439b6e6a83SAndy Lutomirski	/*
13449b6e6a83SAndy Lutomirski	 * Return back to user mode.  We must *not* do the normal exit
1345946c1911SJosh Poimboeuf	 * work, because we don't want to enable interrupts.
13469b6e6a83SAndy Lutomirski	 */
13478a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
13489b6e6a83SAndy Lutomirski
13499b6e6a83SAndy Lutomirski.Lnmi_from_kernel:
13509b6e6a83SAndy Lutomirski	/*
13510b22930eSAndy Lutomirski	 * Here's what our stack frame will look like:
13520b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
13530b22930eSAndy Lutomirski	 * | original SS                                             |
13540b22930eSAndy Lutomirski	 * | original Return RSP                                     |
13550b22930eSAndy Lutomirski	 * | original RFLAGS                                         |
13560b22930eSAndy Lutomirski	 * | original CS                                             |
13570b22930eSAndy Lutomirski	 * | original RIP                                            |
13580b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
13590b22930eSAndy Lutomirski	 * | temp storage for rdx                                    |
13600b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
13610b22930eSAndy Lutomirski	 * | "NMI executing" variable                                |
13620b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
13630b22930eSAndy Lutomirski	 * | iret SS          } Copied from "outermost" frame        |
13640b22930eSAndy Lutomirski	 * | iret Return RSP  } on each loop iteration; overwritten  |
13650b22930eSAndy Lutomirski	 * | iret RFLAGS      } by a nested NMI to force another     |
13660b22930eSAndy Lutomirski	 * | iret CS          } iteration if needed.                 |
13670b22930eSAndy Lutomirski	 * | iret RIP         }                                      |
13680b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
13690b22930eSAndy Lutomirski	 * | outermost SS          } initialized in first_nmi;       |
13700b22930eSAndy Lutomirski	 * | outermost Return RSP  } will not be changed before      |
13710b22930eSAndy Lutomirski	 * | outermost RFLAGS      } NMI processing is done.         |
13720b22930eSAndy Lutomirski	 * | outermost CS          } Copied to "iret" frame on each  |
13730b22930eSAndy Lutomirski	 * | outermost RIP         } iteration.                      |
13740b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
13750b22930eSAndy Lutomirski	 * | pt_regs                                                 |
13760b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
13770b22930eSAndy Lutomirski	 *
13780b22930eSAndy Lutomirski	 * The "original" frame is used by hardware.  Before re-enabling
13790b22930eSAndy Lutomirski	 * NMIs, we need to be done with it, and we need to leave enough
13800b22930eSAndy Lutomirski	 * space for the asm code here.
13810b22930eSAndy Lutomirski	 *
13820b22930eSAndy Lutomirski	 * We return by executing IRET while RSP points to the "iret" frame.
13830b22930eSAndy Lutomirski	 * That will either return for real or it will loop back into NMI
13840b22930eSAndy Lutomirski	 * processing.
13850b22930eSAndy Lutomirski	 *
13860b22930eSAndy Lutomirski	 * The "outermost" frame is copied to the "iret" frame on each
13870b22930eSAndy Lutomirski	 * iteration of the loop, so each iteration starts with the "iret"
13880b22930eSAndy Lutomirski	 * frame pointing to the final return target.
13890b22930eSAndy Lutomirski	 */
13900b22930eSAndy Lutomirski
13910b22930eSAndy Lutomirski	/*
13920b22930eSAndy Lutomirski	 * Determine whether we're a nested NMI.
13930b22930eSAndy Lutomirski	 *
1394a27507caSAndy Lutomirski	 * If we interrupted kernel code between repeat_nmi and
1395a27507caSAndy Lutomirski	 * end_repeat_nmi, then we are a nested NMI.  We must not
1396a27507caSAndy Lutomirski	 * modify the "iret" frame because it's being written by
1397a27507caSAndy Lutomirski	 * the outer NMI.  That's okay; the outer NMI handler is
1398a27507caSAndy Lutomirski	 * about to about to call do_nmi anyway, so we can just
1399a27507caSAndy Lutomirski	 * resume the outer NMI.
1400a27507caSAndy Lutomirski	 */
1401a27507caSAndy Lutomirski
1402a27507caSAndy Lutomirski	movq	$repeat_nmi, %rdx
1403a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1404a27507caSAndy Lutomirski	ja	1f
1405a27507caSAndy Lutomirski	movq	$end_repeat_nmi, %rdx
1406a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1407a27507caSAndy Lutomirski	ja	nested_nmi_out
1408a27507caSAndy Lutomirski1:
1409a27507caSAndy Lutomirski
1410a27507caSAndy Lutomirski	/*
1411a27507caSAndy Lutomirski	 * Now check "NMI executing".  If it's set, then we're nested.
14120b22930eSAndy Lutomirski	 * This will not detect if we interrupted an outer NMI just
14130b22930eSAndy Lutomirski	 * before IRET.
1414905a36a2SIngo Molnar	 */
1415905a36a2SIngo Molnar	cmpl	$1, -8(%rsp)
1416905a36a2SIngo Molnar	je	nested_nmi
1417905a36a2SIngo Molnar
1418905a36a2SIngo Molnar	/*
14190b22930eSAndy Lutomirski	 * Now test if the previous stack was an NMI stack.  This covers
14200b22930eSAndy Lutomirski	 * the case where we interrupt an outer NMI after it clears
1421810bc075SAndy Lutomirski	 * "NMI executing" but before IRET.  We need to be careful, though:
1422810bc075SAndy Lutomirski	 * there is one case in which RSP could point to the NMI stack
1423810bc075SAndy Lutomirski	 * despite there being no NMI active: naughty userspace controls
1424810bc075SAndy Lutomirski	 * RSP at the very beginning of the SYSCALL targets.  We can
1425810bc075SAndy Lutomirski	 * pull a fast one on naughty userspace, though: we program
1426810bc075SAndy Lutomirski	 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1427810bc075SAndy Lutomirski	 * if it controls the kernel's RSP.  We set DF before we clear
1428810bc075SAndy Lutomirski	 * "NMI executing".
1429905a36a2SIngo Molnar	 */
1430905a36a2SIngo Molnar	lea	6*8(%rsp), %rdx
1431905a36a2SIngo Molnar	/* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1432905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1433905a36a2SIngo Molnar	/* If the stack pointer is above the NMI stack, this is a normal NMI */
1434905a36a2SIngo Molnar	ja	first_nmi
14354d732138SIngo Molnar
1436905a36a2SIngo Molnar	subq	$EXCEPTION_STKSZ, %rdx
1437905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1438905a36a2SIngo Molnar	/* If it is below the NMI stack, it is a normal NMI */
1439905a36a2SIngo Molnar	jb	first_nmi
1440810bc075SAndy Lutomirski
1441810bc075SAndy Lutomirski	/* Ah, it is within the NMI stack. */
1442810bc075SAndy Lutomirski
1443810bc075SAndy Lutomirski	testb	$(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1444810bc075SAndy Lutomirski	jz	first_nmi	/* RSP was user controlled. */
1445810bc075SAndy Lutomirski
1446810bc075SAndy Lutomirski	/* This is a nested NMI. */
1447905a36a2SIngo Molnar
1448905a36a2SIngo Molnarnested_nmi:
1449905a36a2SIngo Molnar	/*
14500b22930eSAndy Lutomirski	 * Modify the "iret" frame to point to repeat_nmi, forcing another
14510b22930eSAndy Lutomirski	 * iteration of NMI handling.
1452905a36a2SIngo Molnar	 */
145323a781e9SAndy Lutomirski	subq	$8, %rsp
1454905a36a2SIngo Molnar	leaq	-10*8(%rsp), %rdx
1455905a36a2SIngo Molnar	pushq	$__KERNEL_DS
1456905a36a2SIngo Molnar	pushq	%rdx
1457905a36a2SIngo Molnar	pushfq
1458905a36a2SIngo Molnar	pushq	$__KERNEL_CS
1459905a36a2SIngo Molnar	pushq	$repeat_nmi
1460905a36a2SIngo Molnar
1461905a36a2SIngo Molnar	/* Put stack back */
1462905a36a2SIngo Molnar	addq	$(6*8), %rsp
1463905a36a2SIngo Molnar
1464905a36a2SIngo Molnarnested_nmi_out:
1465905a36a2SIngo Molnar	popq	%rdx
1466905a36a2SIngo Molnar
14670b22930eSAndy Lutomirski	/* We are returning to kernel mode, so this cannot result in a fault. */
1468905a36a2SIngo Molnar	INTERRUPT_RETURN
1469905a36a2SIngo Molnar
1470905a36a2SIngo Molnarfirst_nmi:
14710b22930eSAndy Lutomirski	/* Restore rdx. */
1472905a36a2SIngo Molnar	movq	(%rsp), %rdx
1473905a36a2SIngo Molnar
147436f1a77bSAndy Lutomirski	/* Make room for "NMI executing". */
147536f1a77bSAndy Lutomirski	pushq	$0
1476905a36a2SIngo Molnar
14770b22930eSAndy Lutomirski	/* Leave room for the "iret" frame */
1478905a36a2SIngo Molnar	subq	$(5*8), %rsp
1479905a36a2SIngo Molnar
14800b22930eSAndy Lutomirski	/* Copy the "original" frame to the "outermost" frame */
1481905a36a2SIngo Molnar	.rept 5
1482905a36a2SIngo Molnar	pushq	11*8(%rsp)
1483905a36a2SIngo Molnar	.endr
14848c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1485905a36a2SIngo Molnar
1486905a36a2SIngo Molnar	/* Everything up to here is safe from nested NMIs */
1487905a36a2SIngo Molnar
1488a97439aaSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
1489a97439aaSAndy Lutomirski	/*
1490a97439aaSAndy Lutomirski	 * For ease of testing, unmask NMIs right away.  Disabled by
1491a97439aaSAndy Lutomirski	 * default because IRET is very expensive.
1492a97439aaSAndy Lutomirski	 */
1493a97439aaSAndy Lutomirski	pushq	$0		/* SS */
1494a97439aaSAndy Lutomirski	pushq	%rsp		/* RSP (minus 8 because of the previous push) */
1495a97439aaSAndy Lutomirski	addq	$8, (%rsp)	/* Fix up RSP */
1496a97439aaSAndy Lutomirski	pushfq			/* RFLAGS */
1497a97439aaSAndy Lutomirski	pushq	$__KERNEL_CS	/* CS */
1498a97439aaSAndy Lutomirski	pushq	$1f		/* RIP */
1499a97439aaSAndy Lutomirski	INTERRUPT_RETURN	/* continues at repeat_nmi below */
15008c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1501a97439aaSAndy Lutomirski1:
1502a97439aaSAndy Lutomirski#endif
1503a97439aaSAndy Lutomirski
15040b22930eSAndy Lutomirskirepeat_nmi:
1505905a36a2SIngo Molnar	/*
1506905a36a2SIngo Molnar	 * If there was a nested NMI, the first NMI's iret will return
1507905a36a2SIngo Molnar	 * here. But NMIs are still enabled and we can take another
1508905a36a2SIngo Molnar	 * nested NMI. The nested NMI checks the interrupted RIP to see
1509905a36a2SIngo Molnar	 * if it is between repeat_nmi and end_repeat_nmi, and if so
1510905a36a2SIngo Molnar	 * it will just return, as we are about to repeat an NMI anyway.
1511905a36a2SIngo Molnar	 * This makes it safe to copy to the stack frame that a nested
1512905a36a2SIngo Molnar	 * NMI will update.
15130b22930eSAndy Lutomirski	 *
15140b22930eSAndy Lutomirski	 * RSP is pointing to "outermost RIP".  gsbase is unknown, but, if
15150b22930eSAndy Lutomirski	 * we're repeating an NMI, gsbase has the same value that it had on
15160b22930eSAndy Lutomirski	 * the first iteration.  paranoid_entry will load the kernel
151736f1a77bSAndy Lutomirski	 * gsbase if needed before we call do_nmi.  "NMI executing"
151836f1a77bSAndy Lutomirski	 * is zero.
1519905a36a2SIngo Molnar	 */
152036f1a77bSAndy Lutomirski	movq	$1, 10*8(%rsp)		/* Set "NMI executing". */
1521905a36a2SIngo Molnar
15220b22930eSAndy Lutomirski	/*
15230b22930eSAndy Lutomirski	 * Copy the "outermost" frame to the "iret" frame.  NMIs that nest
15240b22930eSAndy Lutomirski	 * here must not modify the "iret" frame while we're writing to
15250b22930eSAndy Lutomirski	 * it or it will end up containing garbage.
15260b22930eSAndy Lutomirski	 */
1527905a36a2SIngo Molnar	addq	$(10*8), %rsp
1528905a36a2SIngo Molnar	.rept 5
1529905a36a2SIngo Molnar	pushq	-6*8(%rsp)
1530905a36a2SIngo Molnar	.endr
1531905a36a2SIngo Molnar	subq	$(5*8), %rsp
1532905a36a2SIngo Molnarend_repeat_nmi:
1533905a36a2SIngo Molnar
1534905a36a2SIngo Molnar	/*
15350b22930eSAndy Lutomirski	 * Everything below this point can be preempted by a nested NMI.
15360b22930eSAndy Lutomirski	 * If this happens, then the inner NMI will change the "iret"
15370b22930eSAndy Lutomirski	 * frame to point back to repeat_nmi.
1538905a36a2SIngo Molnar	 */
1539905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
1540905a36a2SIngo Molnar	ALLOC_PT_GPREGS_ON_STACK
1541905a36a2SIngo Molnar
1542905a36a2SIngo Molnar	/*
1543905a36a2SIngo Molnar	 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1544905a36a2SIngo Molnar	 * as we should not be calling schedule in NMI context.
1545905a36a2SIngo Molnar	 * Even with normal interrupts enabled. An NMI should not be
1546905a36a2SIngo Molnar	 * setting NEED_RESCHED or anything that normal interrupts and
1547905a36a2SIngo Molnar	 * exceptions might do.
1548905a36a2SIngo Molnar	 */
1549905a36a2SIngo Molnar	call	paranoid_entry
15508c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
1551905a36a2SIngo Molnar
1552905a36a2SIngo Molnar	/* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1553905a36a2SIngo Molnar	movq	%rsp, %rdi
1554905a36a2SIngo Molnar	movq	$-1, %rsi
1555905a36a2SIngo Molnar	call	do_nmi
1556905a36a2SIngo Molnar
1557905a36a2SIngo Molnar	testl	%ebx, %ebx			/* swapgs needed? */
1558905a36a2SIngo Molnar	jnz	nmi_restore
1559905a36a2SIngo Molnarnmi_swapgs:
1560905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
1561905a36a2SIngo Molnarnmi_restore:
1562*471ee483SAndy Lutomirski	POP_EXTRA_REGS
1563*471ee483SAndy Lutomirski	POP_C_REGS
15640b22930eSAndy Lutomirski
1565*471ee483SAndy Lutomirski	/*
1566*471ee483SAndy Lutomirski	 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1567*471ee483SAndy Lutomirski	 * at the "iret" frame.
1568*471ee483SAndy Lutomirski	 */
1569*471ee483SAndy Lutomirski	addq	$6*8, %rsp
1570905a36a2SIngo Molnar
1571810bc075SAndy Lutomirski	/*
1572810bc075SAndy Lutomirski	 * Clear "NMI executing".  Set DF first so that we can easily
1573810bc075SAndy Lutomirski	 * distinguish the remaining code between here and IRET from
1574810bc075SAndy Lutomirski	 * the SYSCALL entry and exit paths.  On a native kernel, we
1575810bc075SAndy Lutomirski	 * could just inspect RIP, but, on paravirt kernels,
1576810bc075SAndy Lutomirski	 * INTERRUPT_RETURN can translate into a jump into a
1577810bc075SAndy Lutomirski	 * hypercall page.
1578810bc075SAndy Lutomirski	 */
1579810bc075SAndy Lutomirski	std
1580810bc075SAndy Lutomirski	movq	$0, 5*8(%rsp)		/* clear "NMI executing" */
15810b22930eSAndy Lutomirski
15820b22930eSAndy Lutomirski	/*
15830b22930eSAndy Lutomirski	 * INTERRUPT_RETURN reads the "iret" frame and exits the NMI
15840b22930eSAndy Lutomirski	 * stack in a single instruction.  We are returning to kernel
15850b22930eSAndy Lutomirski	 * mode, so this cannot result in a fault.
15860b22930eSAndy Lutomirski	 */
15875ca6f70fSAndy Lutomirski	INTERRUPT_RETURN
1588905a36a2SIngo MolnarEND(nmi)
1589905a36a2SIngo Molnar
1590905a36a2SIngo MolnarENTRY(ignore_sysret)
15918c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
1592905a36a2SIngo Molnar	mov	$-ENOSYS, %eax
1593905a36a2SIngo Molnar	sysret
1594905a36a2SIngo MolnarEND(ignore_sysret)
15952deb4be2SAndy Lutomirski
15962deb4be2SAndy LutomirskiENTRY(rewind_stack_do_exit)
15978c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
15982deb4be2SAndy Lutomirski	/* Prevent any naive code from trying to unwind to our caller. */
15992deb4be2SAndy Lutomirski	xorl	%ebp, %ebp
16002deb4be2SAndy Lutomirski
16012deb4be2SAndy Lutomirski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rax
16028c1f7558SJosh Poimboeuf	leaq	-PTREGS_SIZE(%rax), %rsp
16038c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC sp_offset=PTREGS_SIZE
16042deb4be2SAndy Lutomirski
16052deb4be2SAndy Lutomirski	call	do_exit
16062deb4be2SAndy LutomirskiEND(rewind_stack_do_exit)
1607