xref: /openbmc/linux/arch/x86/entry/entry_64.S (revision b9f6976bfb949121bb6e1e6f4fd9909735729148)
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 * - TRACE_IRQ_*:	Trace hardirq state for lock debugging.
204d732138SIngo Molnar * - idtentry:		Define exception entry points.
21905a36a2SIngo Molnar */
22905a36a2SIngo Molnar#include <linux/linkage.h>
23905a36a2SIngo Molnar#include <asm/segment.h>
24905a36a2SIngo Molnar#include <asm/cache.h>
25905a36a2SIngo Molnar#include <asm/errno.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>
402641f08bSDavid Woodhouse#include <asm/nospec-branch.h>
41905a36a2SIngo Molnar#include <linux/err.h>
42905a36a2SIngo Molnar
436fd166aaSPeter Zijlstra#include "calling.h"
446fd166aaSPeter Zijlstra
45905a36a2SIngo Molnar.code64
46905a36a2SIngo Molnar.section .entry.text, "ax"
47905a36a2SIngo Molnar
48905a36a2SIngo Molnar#ifdef CONFIG_PARAVIRT
49bc7b11c0SJiri SlabySYM_CODE_START(native_usergs_sysret64)
508c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
51905a36a2SIngo Molnar	swapgs
52905a36a2SIngo Molnar	sysretq
53bc7b11c0SJiri SlabySYM_CODE_END(native_usergs_sysret64)
54905a36a2SIngo Molnar#endif /* CONFIG_PARAVIRT */
55905a36a2SIngo Molnar
56ca37e57bSAndy Lutomirski.macro TRACE_IRQS_FLAGS flags:req
57905a36a2SIngo Molnar#ifdef CONFIG_TRACE_IRQFLAGS
58a368d7fdSJan Beulich	btl	$9, \flags		/* interrupts off? */
59905a36a2SIngo Molnar	jnc	1f
60905a36a2SIngo Molnar	TRACE_IRQS_ON
61905a36a2SIngo Molnar1:
62905a36a2SIngo Molnar#endif
63905a36a2SIngo Molnar.endm
64905a36a2SIngo Molnar
65ca37e57bSAndy Lutomirski.macro TRACE_IRQS_IRETQ
66ca37e57bSAndy Lutomirski	TRACE_IRQS_FLAGS EFLAGS(%rsp)
67ca37e57bSAndy Lutomirski.endm
68ca37e57bSAndy Lutomirski
69905a36a2SIngo Molnar/*
70905a36a2SIngo Molnar * When dynamic function tracer is enabled it will add a breakpoint
71905a36a2SIngo Molnar * to all locations that it is about to modify, sync CPUs, update
72905a36a2SIngo Molnar * all the code, sync CPUs, then remove the breakpoints. In this time
73905a36a2SIngo Molnar * if lockdep is enabled, it might jump back into the debug handler
74905a36a2SIngo Molnar * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
75905a36a2SIngo Molnar *
76905a36a2SIngo Molnar * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
77905a36a2SIngo Molnar * make sure the stack pointer does not get reset back to the top
78905a36a2SIngo Molnar * of the debug stack, and instead just reuses the current stack.
79905a36a2SIngo Molnar */
80905a36a2SIngo Molnar#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
81905a36a2SIngo Molnar
82905a36a2SIngo Molnar.macro TRACE_IRQS_OFF_DEBUG
83905a36a2SIngo Molnar	call	debug_stack_set_zero
84905a36a2SIngo Molnar	TRACE_IRQS_OFF
85905a36a2SIngo Molnar	call	debug_stack_reset
86905a36a2SIngo Molnar.endm
87905a36a2SIngo Molnar
88905a36a2SIngo Molnar.macro TRACE_IRQS_ON_DEBUG
89905a36a2SIngo Molnar	call	debug_stack_set_zero
90905a36a2SIngo Molnar	TRACE_IRQS_ON
91905a36a2SIngo Molnar	call	debug_stack_reset
92905a36a2SIngo Molnar.endm
93905a36a2SIngo Molnar
94905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ_DEBUG
956709812fSJan Beulich	btl	$9, EFLAGS(%rsp)		/* interrupts off? */
96905a36a2SIngo Molnar	jnc	1f
97905a36a2SIngo Molnar	TRACE_IRQS_ON_DEBUG
98905a36a2SIngo Molnar1:
99905a36a2SIngo Molnar.endm
100905a36a2SIngo Molnar
101905a36a2SIngo Molnar#else
102905a36a2SIngo Molnar# define TRACE_IRQS_OFF_DEBUG			TRACE_IRQS_OFF
103905a36a2SIngo Molnar# define TRACE_IRQS_ON_DEBUG			TRACE_IRQS_ON
104905a36a2SIngo Molnar# define TRACE_IRQS_IRETQ_DEBUG			TRACE_IRQS_IRETQ
105905a36a2SIngo Molnar#endif
106905a36a2SIngo Molnar
107905a36a2SIngo Molnar/*
1084d732138SIngo Molnar * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
109905a36a2SIngo Molnar *
110fda57b22SAndy Lutomirski * This is the only entry point used for 64-bit system calls.  The
111fda57b22SAndy Lutomirski * hardware interface is reasonably well designed and the register to
112fda57b22SAndy Lutomirski * argument mapping Linux uses fits well with the registers that are
113fda57b22SAndy Lutomirski * available when SYSCALL is used.
114fda57b22SAndy Lutomirski *
115fda57b22SAndy Lutomirski * SYSCALL instructions can be found inlined in libc implementations as
116fda57b22SAndy Lutomirski * well as some other programs and libraries.  There are also a handful
117fda57b22SAndy Lutomirski * of SYSCALL instructions in the vDSO used, for example, as a
118fda57b22SAndy Lutomirski * clock_gettimeofday fallback.
119fda57b22SAndy Lutomirski *
1204d732138SIngo Molnar * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
121905a36a2SIngo Molnar * then loads new ss, cs, and rip from previously programmed MSRs.
122905a36a2SIngo Molnar * rflags gets masked by a value from another MSR (so CLD and CLAC
123905a36a2SIngo Molnar * are not needed). SYSCALL does not save anything on the stack
124905a36a2SIngo Molnar * and does not change rsp.
125905a36a2SIngo Molnar *
126905a36a2SIngo Molnar * Registers on entry:
127905a36a2SIngo Molnar * rax  system call number
128905a36a2SIngo Molnar * rcx  return address
129905a36a2SIngo Molnar * r11  saved rflags (note: r11 is callee-clobbered register in C ABI)
130905a36a2SIngo Molnar * rdi  arg0
131905a36a2SIngo Molnar * rsi  arg1
132905a36a2SIngo Molnar * rdx  arg2
133905a36a2SIngo Molnar * r10  arg3 (needs to be moved to rcx to conform to C ABI)
134905a36a2SIngo Molnar * r8   arg4
135905a36a2SIngo Molnar * r9   arg5
136905a36a2SIngo Molnar * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
137905a36a2SIngo Molnar *
138905a36a2SIngo Molnar * Only called from user space.
139905a36a2SIngo Molnar *
140905a36a2SIngo Molnar * When user can change pt_regs->foo always force IRET. That is because
141905a36a2SIngo Molnar * it deals with uncanonical addresses better. SYSRET has trouble
142905a36a2SIngo Molnar * with them due to bugs in both AMD and Intel CPUs.
143905a36a2SIngo Molnar */
144905a36a2SIngo Molnar
145bc7b11c0SJiri SlabySYM_CODE_START(entry_SYSCALL_64)
1468c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
147905a36a2SIngo Molnar	/*
148905a36a2SIngo Molnar	 * Interrupts are off on entry.
149905a36a2SIngo Molnar	 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
150905a36a2SIngo Molnar	 * it is too small to ever cause noticeable irq latency.
151905a36a2SIngo Molnar	 */
152905a36a2SIngo Molnar
1538a9949bcSAndy Lutomirski	swapgs
154bf904d27SAndy Lutomirski	/* tss.sp2 is scratch space. */
15598f05b51SAndy Lutomirski	movq	%rsp, PER_CPU_VAR(cpu_tss_rw + TSS_sp2)
156bf904d27SAndy Lutomirski	SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
157905a36a2SIngo Molnar	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
158905a36a2SIngo Molnar
159905a36a2SIngo Molnar	/* Construct struct pt_regs on stack */
160905a36a2SIngo Molnar	pushq	$__USER_DS				/* pt_regs->ss */
16198f05b51SAndy Lutomirski	pushq	PER_CPU_VAR(cpu_tss_rw + TSS_sp2)	/* pt_regs->sp */
162905a36a2SIngo Molnar	pushq	%r11					/* pt_regs->flags */
163905a36a2SIngo Molnar	pushq	$__USER_CS				/* pt_regs->cs */
164905a36a2SIngo Molnar	pushq	%rcx					/* pt_regs->ip */
16526ba4e57SJiri SlabySYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL)
166905a36a2SIngo Molnar	pushq	%rax					/* pt_regs->orig_ax */
16730907fd1SDominik Brodowski
16830907fd1SDominik Brodowski	PUSH_AND_CLEAR_REGS rax=$-ENOSYS
169905a36a2SIngo Molnar
170548c3050SAndy Lutomirski	TRACE_IRQS_OFF
171548c3050SAndy Lutomirski
1721e423bffSAndy Lutomirski	/* IRQs are off. */
173dfe64506SLinus Torvalds	movq	%rax, %rdi
174dfe64506SLinus Torvalds	movq	%rsp, %rsi
1751e423bffSAndy Lutomirski	call	do_syscall_64		/* returns with IRQs disabled */
1761e423bffSAndy Lutomirski
177810f80a6SThomas Gleixner	TRACE_IRQS_ON			/* return enables interrupts */
178905a36a2SIngo Molnar
179905a36a2SIngo Molnar	/*
180905a36a2SIngo Molnar	 * Try to use SYSRET instead of IRET if we're returning to
1818a055d7fSAndy Lutomirski	 * a completely clean 64-bit userspace context.  If we're not,
1828a055d7fSAndy Lutomirski	 * go to the slow exit path.
183905a36a2SIngo Molnar	 */
184905a36a2SIngo Molnar	movq	RCX(%rsp), %rcx
185905a36a2SIngo Molnar	movq	RIP(%rsp), %r11
1868a055d7fSAndy Lutomirski
1878a055d7fSAndy Lutomirski	cmpq	%rcx, %r11	/* SYSRET requires RCX == RIP */
1888a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
189905a36a2SIngo Molnar
190905a36a2SIngo Molnar	/*
191905a36a2SIngo Molnar	 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
192905a36a2SIngo Molnar	 * in kernel space.  This essentially lets the user take over
193905a36a2SIngo Molnar	 * the kernel, since userspace controls RSP.
194905a36a2SIngo Molnar	 *
195905a36a2SIngo Molnar	 * If width of "canonical tail" ever becomes variable, this will need
196905a36a2SIngo Molnar	 * to be updated to remain correct on both old and new CPUs.
197361b4b58SKirill A. Shutemov	 *
198cbe0317bSKirill A. Shutemov	 * Change top bits to match most significant bit (47th or 56th bit
199cbe0317bSKirill A. Shutemov	 * depending on paging mode) in the address.
200905a36a2SIngo Molnar	 */
20109e61a77SKirill A. Shutemov#ifdef CONFIG_X86_5LEVEL
20239b95522SKirill A. Shutemov	ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \
20339b95522SKirill A. Shutemov		"shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57
20409e61a77SKirill A. Shutemov#else
205905a36a2SIngo Molnar	shl	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
206905a36a2SIngo Molnar	sar	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
20709e61a77SKirill A. Shutemov#endif
2084d732138SIngo Molnar
209905a36a2SIngo Molnar	/* If this changed %rcx, it was not canonical */
210905a36a2SIngo Molnar	cmpq	%rcx, %r11
2118a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
212905a36a2SIngo Molnar
213905a36a2SIngo Molnar	cmpq	$__USER_CS, CS(%rsp)		/* CS must match SYSRET */
2148a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
215905a36a2SIngo Molnar
216905a36a2SIngo Molnar	movq	R11(%rsp), %r11
217905a36a2SIngo Molnar	cmpq	%r11, EFLAGS(%rsp)		/* R11 == RFLAGS */
2188a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
219905a36a2SIngo Molnar
220905a36a2SIngo Molnar	/*
2213e035305SBorislav Petkov	 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
2223e035305SBorislav Petkov	 * restore RF properly. If the slowpath sets it for whatever reason, we
2233e035305SBorislav Petkov	 * need to restore it correctly.
2243e035305SBorislav Petkov	 *
2253e035305SBorislav Petkov	 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
2263e035305SBorislav Petkov	 * trap from userspace immediately after SYSRET.  This would cause an
2273e035305SBorislav Petkov	 * infinite loop whenever #DB happens with register state that satisfies
2283e035305SBorislav Petkov	 * the opportunistic SYSRET conditions.  For example, single-stepping
2293e035305SBorislav Petkov	 * this user code:
230905a36a2SIngo Molnar	 *
231905a36a2SIngo Molnar	 *           movq	$stuck_here, %rcx
232905a36a2SIngo Molnar	 *           pushfq
233905a36a2SIngo Molnar	 *           popq %r11
234905a36a2SIngo Molnar	 *   stuck_here:
235905a36a2SIngo Molnar	 *
236905a36a2SIngo Molnar	 * would never get past 'stuck_here'.
237905a36a2SIngo Molnar	 */
238905a36a2SIngo Molnar	testq	$(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
2398a055d7fSAndy Lutomirski	jnz	swapgs_restore_regs_and_return_to_usermode
240905a36a2SIngo Molnar
241905a36a2SIngo Molnar	/* nothing to check for RSP */
242905a36a2SIngo Molnar
243905a36a2SIngo Molnar	cmpq	$__USER_DS, SS(%rsp)		/* SS must match SYSRET */
2448a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
245905a36a2SIngo Molnar
246905a36a2SIngo Molnar	/*
247905a36a2SIngo Molnar	 * We win! This label is here just for ease of understanding
248905a36a2SIngo Molnar	 * perf profiles. Nothing jumps here.
249905a36a2SIngo Molnar	 */
250905a36a2SIngo Molnarsyscall_return_via_sysret:
251905a36a2SIngo Molnar	/* rcx and r11 are already restored (see code above) */
252502af0d7SDominik Brodowski	POP_REGS pop_rdi=0 skip_r11rcx=1
2533e3b9293SAndy Lutomirski
2543e3b9293SAndy Lutomirski	/*
2553e3b9293SAndy Lutomirski	 * Now all regs are restored except RSP and RDI.
2563e3b9293SAndy Lutomirski	 * Save old stack pointer and switch to trampoline stack.
2573e3b9293SAndy Lutomirski	 */
2583e3b9293SAndy Lutomirski	movq	%rsp, %rdi
259c482feefSAndy Lutomirski	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
2601fb14363SJosh Poimboeuf	UNWIND_HINT_EMPTY
2613e3b9293SAndy Lutomirski
2623e3b9293SAndy Lutomirski	pushq	RSP-RDI(%rdi)	/* RSP */
2633e3b9293SAndy Lutomirski	pushq	(%rdi)		/* RDI */
2643e3b9293SAndy Lutomirski
2653e3b9293SAndy Lutomirski	/*
2663e3b9293SAndy Lutomirski	 * We are on the trampoline stack.  All regs except RDI are live.
2673e3b9293SAndy Lutomirski	 * We can do future final exit work right here.
2683e3b9293SAndy Lutomirski	 */
269afaef01cSAlexander Popov	STACKLEAK_ERASE_NOCLOBBER
270afaef01cSAlexander Popov
2716fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
2723e3b9293SAndy Lutomirski
2734fbb3910SAndy Lutomirski	popq	%rdi
2743e3b9293SAndy Lutomirski	popq	%rsp
275905a36a2SIngo Molnar	USERGS_SYSRET64
276bc7b11c0SJiri SlabySYM_CODE_END(entry_SYSCALL_64)
277905a36a2SIngo Molnar
278905a36a2SIngo Molnar/*
2790100301bSBrian Gerst * %rdi: prev task
2800100301bSBrian Gerst * %rsi: next task
2810100301bSBrian Gerst */
282*b9f6976bSThomas Gleixner.pushsection .text, "ax"
28396c64806SJosh PoimboeufSYM_FUNC_START(__switch_to_asm)
2840100301bSBrian Gerst	/*
2850100301bSBrian Gerst	 * Save callee-saved registers
2860100301bSBrian Gerst	 * This must match the order in inactive_task_frame
2870100301bSBrian Gerst	 */
2880100301bSBrian Gerst	pushq	%rbp
2890100301bSBrian Gerst	pushq	%rbx
2900100301bSBrian Gerst	pushq	%r12
2910100301bSBrian Gerst	pushq	%r13
2920100301bSBrian Gerst	pushq	%r14
2930100301bSBrian Gerst	pushq	%r15
2940100301bSBrian Gerst
2950100301bSBrian Gerst	/* switch stack */
2960100301bSBrian Gerst	movq	%rsp, TASK_threadsp(%rdi)
2970100301bSBrian Gerst	movq	TASK_threadsp(%rsi), %rsp
2980100301bSBrian Gerst
299050e9baaSLinus Torvalds#ifdef CONFIG_STACKPROTECTOR
3000100301bSBrian Gerst	movq	TASK_stack_canary(%rsi), %rbx
301e6401c13SAndy Lutomirski	movq	%rbx, PER_CPU_VAR(fixed_percpu_data) + stack_canary_offset
3020100301bSBrian Gerst#endif
3030100301bSBrian Gerst
304c995efd5SDavid Woodhouse#ifdef CONFIG_RETPOLINE
305c995efd5SDavid Woodhouse	/*
306c995efd5SDavid Woodhouse	 * When switching from a shallower to a deeper call stack
307c995efd5SDavid Woodhouse	 * the RSB may either underflow or use entries populated
308c995efd5SDavid Woodhouse	 * with userspace addresses. On CPUs where those concerns
309c995efd5SDavid Woodhouse	 * exist, overwrite the RSB with entries which capture
310c995efd5SDavid Woodhouse	 * speculative execution to prevent attack.
311c995efd5SDavid Woodhouse	 */
312d1c99108SDavid Woodhouse	FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
313c995efd5SDavid Woodhouse#endif
314c995efd5SDavid Woodhouse
3150100301bSBrian Gerst	/* restore callee-saved registers */
3160100301bSBrian Gerst	popq	%r15
3170100301bSBrian Gerst	popq	%r14
3180100301bSBrian Gerst	popq	%r13
3190100301bSBrian Gerst	popq	%r12
3200100301bSBrian Gerst	popq	%rbx
3210100301bSBrian Gerst	popq	%rbp
3220100301bSBrian Gerst
3230100301bSBrian Gerst	jmp	__switch_to
32496c64806SJosh PoimboeufSYM_FUNC_END(__switch_to_asm)
325*b9f6976bSThomas Gleixner.popsection
3260100301bSBrian Gerst
3270100301bSBrian Gerst/*
328905a36a2SIngo Molnar * A newly forked process directly context switches into this address.
329905a36a2SIngo Molnar *
3300100301bSBrian Gerst * rax: prev task we switched from
331616d2483SBrian Gerst * rbx: kernel thread func (NULL for user thread)
332616d2483SBrian Gerst * r12: kernel thread arg
333905a36a2SIngo Molnar */
334*b9f6976bSThomas Gleixner.pushsection .text, "ax"
335bc7b11c0SJiri SlabySYM_CODE_START(ret_from_fork)
3368c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
3370100301bSBrian Gerst	movq	%rax, %rdi
3384d732138SIngo Molnar	call	schedule_tail			/* rdi: 'prev' task parameter */
339905a36a2SIngo Molnar
340616d2483SBrian Gerst	testq	%rbx, %rbx			/* from kernel_thread? */
341616d2483SBrian Gerst	jnz	1f				/* kernel threads are uncommon */
342905a36a2SIngo Molnar
343616d2483SBrian Gerst2:
3448c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
345ebd57499SJosh Poimboeuf	movq	%rsp, %rdi
34624d978b7SAndy Lutomirski	call	syscall_return_slowpath	/* returns with IRQs disabled */
34724d978b7SAndy Lutomirski	TRACE_IRQS_ON			/* user mode is traced as IRQS on */
3488a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
349616d2483SBrian Gerst
350616d2483SBrian Gerst1:
351616d2483SBrian Gerst	/* kernel thread */
352d31a5802SJosh Poimboeuf	UNWIND_HINT_EMPTY
353616d2483SBrian Gerst	movq	%r12, %rdi
35434fdce69SPeter Zijlstra	CALL_NOSPEC rbx
355616d2483SBrian Gerst	/*
356616d2483SBrian Gerst	 * A kernel thread is allowed to return here after successfully
357616d2483SBrian Gerst	 * calling do_execve().  Exit to userspace to complete the execve()
358616d2483SBrian Gerst	 * syscall.
359616d2483SBrian Gerst	 */
360616d2483SBrian Gerst	movq	$0, RAX(%rsp)
361616d2483SBrian Gerst	jmp	2b
362bc7b11c0SJiri SlabySYM_CODE_END(ret_from_fork)
363*b9f6976bSThomas Gleixner.popsection
364905a36a2SIngo Molnar
365905a36a2SIngo Molnar/*
366905a36a2SIngo Molnar * Build the entry stubs with some assembler magic.
367905a36a2SIngo Molnar * We pack 1 stub into every 8-byte block.
368905a36a2SIngo Molnar */
369905a36a2SIngo Molnar	.align 8
370bc7b11c0SJiri SlabySYM_CODE_START(irq_entries_start)
371905a36a2SIngo Molnar    vector=FIRST_EXTERNAL_VECTOR
372905a36a2SIngo Molnar    .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
3738c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
374905a36a2SIngo Molnar	pushq	$(~vector+0x80)			/* Note: always in signed byte range */
375905a36a2SIngo Molnar	jmp	common_interrupt
376905a36a2SIngo Molnar	.align	8
3778c1f7558SJosh Poimboeuf	vector=vector+1
378905a36a2SIngo Molnar    .endr
379bc7b11c0SJiri SlabySYM_CODE_END(irq_entries_start)
380905a36a2SIngo Molnar
381f8a8fe61SThomas Gleixner	.align 8
382bc7b11c0SJiri SlabySYM_CODE_START(spurious_entries_start)
383f8a8fe61SThomas Gleixner    vector=FIRST_SYSTEM_VECTOR
384f8a8fe61SThomas Gleixner    .rept (NR_VECTORS - FIRST_SYSTEM_VECTOR)
385f8a8fe61SThomas Gleixner	UNWIND_HINT_IRET_REGS
386f8a8fe61SThomas Gleixner	pushq	$(~vector+0x80)			/* Note: always in signed byte range */
387f8a8fe61SThomas Gleixner	jmp	common_spurious
388f8a8fe61SThomas Gleixner	.align	8
389f8a8fe61SThomas Gleixner	vector=vector+1
390f8a8fe61SThomas Gleixner    .endr
391bc7b11c0SJiri SlabySYM_CODE_END(spurious_entries_start)
392f8a8fe61SThomas Gleixner
3931d3e53e8SAndy Lutomirski.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
3941d3e53e8SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
395e17f8234SBoris Ostrovsky	pushq %rax
396e17f8234SBoris Ostrovsky	SAVE_FLAGS(CLBR_RAX)
397e17f8234SBoris Ostrovsky	testl $X86_EFLAGS_IF, %eax
3981d3e53e8SAndy Lutomirski	jz .Lokay_\@
3991d3e53e8SAndy Lutomirski	ud2
4001d3e53e8SAndy Lutomirski.Lokay_\@:
401e17f8234SBoris Ostrovsky	popq %rax
4021d3e53e8SAndy Lutomirski#endif
4031d3e53e8SAndy Lutomirski.endm
4041d3e53e8SAndy Lutomirski
4051d3e53e8SAndy Lutomirski/*
4061d3e53e8SAndy Lutomirski * Enters the IRQ stack if we're not already using it.  NMI-safe.  Clobbers
4071d3e53e8SAndy Lutomirski * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
4081d3e53e8SAndy Lutomirski * Requires kernel GSBASE.
4091d3e53e8SAndy Lutomirski *
4101d3e53e8SAndy Lutomirski * The invariant is that, if irq_count != -1, then the IRQ stack is in use.
4111d3e53e8SAndy Lutomirski */
4122ba64741SDominik Brodowski.macro ENTER_IRQ_STACK regs=1 old_rsp save_ret=0
4131d3e53e8SAndy Lutomirski	DEBUG_ENTRY_ASSERT_IRQS_OFF
4142ba64741SDominik Brodowski
4152ba64741SDominik Brodowski	.if \save_ret
4162ba64741SDominik Brodowski	/*
4172ba64741SDominik Brodowski	 * If save_ret is set, the original stack contains one additional
4182ba64741SDominik Brodowski	 * entry -- the return address. Therefore, move the address one
4192ba64741SDominik Brodowski	 * entry below %rsp to \old_rsp.
4202ba64741SDominik Brodowski	 */
4212ba64741SDominik Brodowski	leaq	8(%rsp), \old_rsp
4222ba64741SDominik Brodowski	.else
4231d3e53e8SAndy Lutomirski	movq	%rsp, \old_rsp
4242ba64741SDominik Brodowski	.endif
4258c1f7558SJosh Poimboeuf
4268c1f7558SJosh Poimboeuf	.if \regs
4278c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS base=\old_rsp
4288c1f7558SJosh Poimboeuf	.endif
4298c1f7558SJosh Poimboeuf
4301d3e53e8SAndy Lutomirski	incl	PER_CPU_VAR(irq_count)
43129955909SAndy Lutomirski	jnz	.Lirq_stack_push_old_rsp_\@
4321d3e53e8SAndy Lutomirski
4331d3e53e8SAndy Lutomirski	/*
4341d3e53e8SAndy Lutomirski	 * Right now, if we just incremented irq_count to zero, we've
4351d3e53e8SAndy Lutomirski	 * claimed the IRQ stack but we haven't switched to it yet.
4361d3e53e8SAndy Lutomirski	 *
4371d3e53e8SAndy Lutomirski	 * If anything is added that can interrupt us here without using IST,
4381d3e53e8SAndy Lutomirski	 * it must be *extremely* careful to limit its stack usage.  This
4391d3e53e8SAndy Lutomirski	 * could include kprobes and a hypothetical future IST-less #DB
4401d3e53e8SAndy Lutomirski	 * handler.
44129955909SAndy Lutomirski	 *
44229955909SAndy Lutomirski	 * The OOPS unwinder relies on the word at the top of the IRQ
44329955909SAndy Lutomirski	 * stack linking back to the previous RSP for the entire time we're
44429955909SAndy Lutomirski	 * on the IRQ stack.  For this to work reliably, we need to write
44529955909SAndy Lutomirski	 * it before we actually move ourselves to the IRQ stack.
4461d3e53e8SAndy Lutomirski	 */
4471d3e53e8SAndy Lutomirski
448e6401c13SAndy Lutomirski	movq	\old_rsp, PER_CPU_VAR(irq_stack_backing_store + IRQ_STACK_SIZE - 8)
449758a2e31SThomas Gleixner	movq	PER_CPU_VAR(hardirq_stack_ptr), %rsp
45029955909SAndy Lutomirski
45129955909SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
45229955909SAndy Lutomirski	/*
45329955909SAndy Lutomirski	 * If the first movq above becomes wrong due to IRQ stack layout
45429955909SAndy Lutomirski	 * changes, the only way we'll notice is if we try to unwind right
45529955909SAndy Lutomirski	 * here.  Assert that we set up the stack right to catch this type
45629955909SAndy Lutomirski	 * of bug quickly.
45729955909SAndy Lutomirski	 */
45829955909SAndy Lutomirski	cmpq	-8(%rsp), \old_rsp
45929955909SAndy Lutomirski	je	.Lirq_stack_okay\@
46029955909SAndy Lutomirski	ud2
46129955909SAndy Lutomirski	.Lirq_stack_okay\@:
46229955909SAndy Lutomirski#endif
46329955909SAndy Lutomirski
46429955909SAndy Lutomirski.Lirq_stack_push_old_rsp_\@:
4651d3e53e8SAndy Lutomirski	pushq	\old_rsp
4668c1f7558SJosh Poimboeuf
4678c1f7558SJosh Poimboeuf	.if \regs
4688c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS indirect=1
4698c1f7558SJosh Poimboeuf	.endif
4702ba64741SDominik Brodowski
4712ba64741SDominik Brodowski	.if \save_ret
4722ba64741SDominik Brodowski	/*
4732ba64741SDominik Brodowski	 * Push the return address to the stack. This return address can
4742ba64741SDominik Brodowski	 * be found at the "real" original RSP, which was offset by 8 at
4752ba64741SDominik Brodowski	 * the beginning of this macro.
4762ba64741SDominik Brodowski	 */
4772ba64741SDominik Brodowski	pushq	-8(\old_rsp)
4782ba64741SDominik Brodowski	.endif
4791d3e53e8SAndy Lutomirski.endm
4801d3e53e8SAndy Lutomirski
4811d3e53e8SAndy Lutomirski/*
4821d3e53e8SAndy Lutomirski * Undoes ENTER_IRQ_STACK.
4831d3e53e8SAndy Lutomirski */
4848c1f7558SJosh Poimboeuf.macro LEAVE_IRQ_STACK regs=1
4851d3e53e8SAndy Lutomirski	DEBUG_ENTRY_ASSERT_IRQS_OFF
4861d3e53e8SAndy Lutomirski	/* We need to be off the IRQ stack before decrementing irq_count. */
4871d3e53e8SAndy Lutomirski	popq	%rsp
4881d3e53e8SAndy Lutomirski
4898c1f7558SJosh Poimboeuf	.if \regs
4908c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
4918c1f7558SJosh Poimboeuf	.endif
4928c1f7558SJosh Poimboeuf
4931d3e53e8SAndy Lutomirski	/*
4941d3e53e8SAndy Lutomirski	 * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming
4951d3e53e8SAndy Lutomirski	 * the irq stack but we're not on it.
4961d3e53e8SAndy Lutomirski	 */
4971d3e53e8SAndy Lutomirski
4981d3e53e8SAndy Lutomirski	decl	PER_CPU_VAR(irq_count)
4991d3e53e8SAndy Lutomirski.endm
5001d3e53e8SAndy Lutomirski
501905a36a2SIngo Molnar/*
502f3d415eaSDominik Brodowski * Interrupt entry helper function.
503905a36a2SIngo Molnar *
504f3d415eaSDominik Brodowski * Entry runs with interrupts off. Stack layout at entry:
505f3d415eaSDominik Brodowski * +----------------------------------------------------+
506f3d415eaSDominik Brodowski * | regs->ss						|
507f3d415eaSDominik Brodowski * | regs->rsp						|
508f3d415eaSDominik Brodowski * | regs->eflags					|
509f3d415eaSDominik Brodowski * | regs->cs						|
510f3d415eaSDominik Brodowski * | regs->ip						|
511f3d415eaSDominik Brodowski * +----------------------------------------------------+
512f3d415eaSDominik Brodowski * | regs->orig_ax = ~(interrupt number)		|
513f3d415eaSDominik Brodowski * +----------------------------------------------------+
514f3d415eaSDominik Brodowski * | return address					|
515f3d415eaSDominik Brodowski * +----------------------------------------------------+
516905a36a2SIngo Molnar */
517bc7b11c0SJiri SlabySYM_CODE_START(interrupt_entry)
51881b67439SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=16
519f3d415eaSDominik Brodowski	ASM_CLAC
520905a36a2SIngo Molnar	cld
5217f2590a1SAndy Lutomirski
522f3d415eaSDominik Brodowski	testb	$3, CS-ORIG_RAX+8(%rsp)
5237f2590a1SAndy Lutomirski	jz	1f
5247f2590a1SAndy Lutomirski	SWAPGS
52518ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
526f3d415eaSDominik Brodowski	/*
527f3d415eaSDominik Brodowski	 * Switch to the thread stack. The IRET frame and orig_ax are
528f3d415eaSDominik Brodowski	 * on the stack, as well as the return address. RDI..R12 are
529f3d415eaSDominik Brodowski	 * not (yet) on the stack and space has not (yet) been
530f3d415eaSDominik Brodowski	 * allocated for them.
531f3d415eaSDominik Brodowski	 */
53290a6acc4SDominik Brodowski	pushq	%rdi
533f3d415eaSDominik Brodowski
53490a6acc4SDominik Brodowski	/* Need to switch before accessing the thread stack. */
53590a6acc4SDominik Brodowski	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi
53690a6acc4SDominik Brodowski	movq	%rsp, %rdi
53790a6acc4SDominik Brodowski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
538f3d415eaSDominik Brodowski
539f3d415eaSDominik Brodowski	 /*
540f3d415eaSDominik Brodowski	  * We have RDI, return address, and orig_ax on the stack on
541f3d415eaSDominik Brodowski	  * top of the IRET frame. That means offset=24
542f3d415eaSDominik Brodowski	  */
543f3d415eaSDominik Brodowski	UNWIND_HINT_IRET_REGS base=%rdi offset=24
54490a6acc4SDominik Brodowski
54590a6acc4SDominik Brodowski	pushq	7*8(%rdi)		/* regs->ss */
54690a6acc4SDominik Brodowski	pushq	6*8(%rdi)		/* regs->rsp */
54790a6acc4SDominik Brodowski	pushq	5*8(%rdi)		/* regs->eflags */
54890a6acc4SDominik Brodowski	pushq	4*8(%rdi)		/* regs->cs */
54990a6acc4SDominik Brodowski	pushq	3*8(%rdi)		/* regs->ip */
55081b67439SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
55190a6acc4SDominik Brodowski	pushq	2*8(%rdi)		/* regs->orig_ax */
55290a6acc4SDominik Brodowski	pushq	8(%rdi)			/* return address */
55390a6acc4SDominik Brodowski
55490a6acc4SDominik Brodowski	movq	(%rdi), %rdi
55564dbc122SJosh Poimboeuf	jmp	2f
5567f2590a1SAndy Lutomirski1:
55718ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
55818ec54fdSJosh Poimboeuf2:
5590e34d226SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
5600e34d226SDominik Brodowski	ENCODE_FRAME_POINTER 8
561905a36a2SIngo Molnar
5622ba64741SDominik Brodowski	testb	$3, CS+8(%rsp)
563905a36a2SIngo Molnar	jz	1f
56402bc7768SAndy Lutomirski
56502bc7768SAndy Lutomirski	/*
5667f2590a1SAndy Lutomirski	 * IRQ from user mode.
5677f2590a1SAndy Lutomirski	 *
568f1075053SAndy Lutomirski	 * We need to tell lockdep that IRQs are off.  We can't do this until
569f1075053SAndy Lutomirski	 * we fix gsbase, and we should do it before enter_from_user_mode
570f3d415eaSDominik Brodowski	 * (which can take locks).  Since TRACE_IRQS_OFF is idempotent,
571f1075053SAndy Lutomirski	 * the simplest way to handle it is to just call it twice if
572f1075053SAndy Lutomirski	 * we enter from user mode.  There's no reason to optimize this since
573f1075053SAndy Lutomirski	 * TRACE_IRQS_OFF is a no-op if lockdep is off.
574f1075053SAndy Lutomirski	 */
575f1075053SAndy Lutomirski	TRACE_IRQS_OFF
576f1075053SAndy Lutomirski
577478dc89cSAndy Lutomirski	CALL_enter_from_user_mode
57802bc7768SAndy Lutomirski
579905a36a2SIngo Molnar1:
5802ba64741SDominik Brodowski	ENTER_IRQ_STACK old_rsp=%rdi save_ret=1
581905a36a2SIngo Molnar	/* We entered an interrupt context - irqs are off: */
582905a36a2SIngo Molnar	TRACE_IRQS_OFF
583905a36a2SIngo Molnar
5842ba64741SDominik Brodowski	ret
585bc7b11c0SJiri SlabySYM_CODE_END(interrupt_entry)
586a50480cbSAndrea Righi_ASM_NOKPROBE(interrupt_entry)
5872ba64741SDominik Brodowski
588f3d415eaSDominik Brodowski
589f3d415eaSDominik Brodowski/* Interrupt entry/exit. */
590905a36a2SIngo Molnar
591905a36a2SIngo Molnar/*
592905a36a2SIngo Molnar * The interrupt stubs push (~vector+0x80) onto the stack and
593f8a8fe61SThomas Gleixner * then jump to common_spurious/interrupt.
594905a36a2SIngo Molnar */
595cc66936eSJiri SlabySYM_CODE_START_LOCAL(common_spurious)
596f8a8fe61SThomas Gleixner	addq	$-0x80, (%rsp)			/* Adjust vector to [-256, -1] range */
597f8a8fe61SThomas Gleixner	call	interrupt_entry
598f8a8fe61SThomas Gleixner	UNWIND_HINT_REGS indirect=1
599f8a8fe61SThomas Gleixner	call	smp_spurious_interrupt		/* rdi points to pt_regs */
600f8a8fe61SThomas Gleixner	jmp	ret_from_intr
601cc66936eSJiri SlabySYM_CODE_END(common_spurious)
602f8a8fe61SThomas Gleixner_ASM_NOKPROBE(common_spurious)
603f8a8fe61SThomas Gleixner
604f8a8fe61SThomas Gleixner/* common_interrupt is a hotpath. Align it */
605905a36a2SIngo Molnar	.p2align CONFIG_X86_L1_CACHE_SHIFT
606cc66936eSJiri SlabySYM_CODE_START_LOCAL(common_interrupt)
607905a36a2SIngo Molnar	addq	$-0x80, (%rsp)			/* Adjust vector to [-256, -1] range */
6083aa99fc3SDominik Brodowski	call	interrupt_entry
6093aa99fc3SDominik Brodowski	UNWIND_HINT_REGS indirect=1
6103aa99fc3SDominik Brodowski	call	do_IRQ	/* rdi points to pt_regs */
611905a36a2SIngo Molnar	/* 0(%rsp): old RSP */
612905a36a2SIngo Molnarret_from_intr:
6132140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
614905a36a2SIngo Molnar	TRACE_IRQS_OFF
615905a36a2SIngo Molnar
6161d3e53e8SAndy Lutomirski	LEAVE_IRQ_STACK
617905a36a2SIngo Molnar
618905a36a2SIngo Molnar	testb	$3, CS(%rsp)
619905a36a2SIngo Molnar	jz	retint_kernel
62002bc7768SAndy Lutomirski
621905a36a2SIngo Molnar	/* Interrupt came from user space */
62230a2441cSJiri Slaby.Lretint_user:
62302bc7768SAndy Lutomirski	mov	%rsp,%rdi
62402bc7768SAndy Lutomirski	call	prepare_exit_to_usermode
625810f80a6SThomas Gleixner	TRACE_IRQS_ON
62626c4ef9cSAndy Lutomirski
62726ba4e57SJiri SlabySYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
62826c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
62926c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates user mode. */
6301e4c4f61SBorislav Petkov	testb	$3, CS(%rsp)
63126c4ef9cSAndy Lutomirski	jnz	1f
63226c4ef9cSAndy Lutomirski	ud2
63326c4ef9cSAndy Lutomirski1:
63426c4ef9cSAndy Lutomirski#endif
635502af0d7SDominik Brodowski	POP_REGS pop_rdi=0
6363e3b9293SAndy Lutomirski
6373e3b9293SAndy Lutomirski	/*
6383e3b9293SAndy Lutomirski	 * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS.
6393e3b9293SAndy Lutomirski	 * Save old stack pointer and switch to trampoline stack.
6403e3b9293SAndy Lutomirski	 */
6413e3b9293SAndy Lutomirski	movq	%rsp, %rdi
642c482feefSAndy Lutomirski	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
6431fb14363SJosh Poimboeuf	UNWIND_HINT_EMPTY
6443e3b9293SAndy Lutomirski
6453e3b9293SAndy Lutomirski	/* Copy the IRET frame to the trampoline stack. */
6463e3b9293SAndy Lutomirski	pushq	6*8(%rdi)	/* SS */
6473e3b9293SAndy Lutomirski	pushq	5*8(%rdi)	/* RSP */
6483e3b9293SAndy Lutomirski	pushq	4*8(%rdi)	/* EFLAGS */
6493e3b9293SAndy Lutomirski	pushq	3*8(%rdi)	/* CS */
6503e3b9293SAndy Lutomirski	pushq	2*8(%rdi)	/* RIP */
6513e3b9293SAndy Lutomirski
6523e3b9293SAndy Lutomirski	/* Push user RDI on the trampoline stack. */
6533e3b9293SAndy Lutomirski	pushq	(%rdi)
6543e3b9293SAndy Lutomirski
6553e3b9293SAndy Lutomirski	/*
6563e3b9293SAndy Lutomirski	 * We are on the trampoline stack.  All regs except RDI are live.
6573e3b9293SAndy Lutomirski	 * We can do future final exit work right here.
6583e3b9293SAndy Lutomirski	 */
659afaef01cSAlexander Popov	STACKLEAK_ERASE_NOCLOBBER
6603e3b9293SAndy Lutomirski
6616fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
6628a09317bSDave Hansen
6633e3b9293SAndy Lutomirski	/* Restore RDI. */
6643e3b9293SAndy Lutomirski	popq	%rdi
6653e3b9293SAndy Lutomirski	SWAPGS
66626c4ef9cSAndy Lutomirski	INTERRUPT_RETURN
66726c4ef9cSAndy Lutomirski
668905a36a2SIngo Molnar
669905a36a2SIngo Molnar/* Returning to kernel space */
670905a36a2SIngo Molnarretint_kernel:
67148593975SThomas Gleixner#ifdef CONFIG_PREEMPTION
672905a36a2SIngo Molnar	/* Interrupts are off */
673905a36a2SIngo Molnar	/* Check if we need preemption */
6746709812fSJan Beulich	btl	$9, EFLAGS(%rsp)		/* were interrupts off? */
675905a36a2SIngo Molnar	jnc	1f
676b5b447b6SValentin Schneider	cmpl	$0, PER_CPU_VAR(__preempt_count)
677905a36a2SIngo Molnar	jnz	1f
678905a36a2SIngo Molnar	call	preempt_schedule_irq
679905a36a2SIngo Molnar1:
680905a36a2SIngo Molnar#endif
681905a36a2SIngo Molnar	/*
682905a36a2SIngo Molnar	 * The iretq could re-enable interrupts:
683905a36a2SIngo Molnar	 */
684905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
685905a36a2SIngo Molnar
68626ba4e57SJiri SlabySYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)
68726c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
68826c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates kernel mode. */
6891e4c4f61SBorislav Petkov	testb	$3, CS(%rsp)
69026c4ef9cSAndy Lutomirski	jz	1f
69126c4ef9cSAndy Lutomirski	ud2
69226c4ef9cSAndy Lutomirski1:
69326c4ef9cSAndy Lutomirski#endif
694502af0d7SDominik Brodowski	POP_REGS
695e872045bSAndy Lutomirski	addq	$8, %rsp	/* skip regs->orig_ax */
69610bcc80eSMathieu Desnoyers	/*
69710bcc80eSMathieu Desnoyers	 * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
69810bcc80eSMathieu Desnoyers	 * when returning from IPI handler.
69910bcc80eSMathieu Desnoyers	 */
700905a36a2SIngo Molnar	INTERRUPT_RETURN
701905a36a2SIngo Molnar
702cc66936eSJiri SlabySYM_INNER_LABEL_ALIGN(native_iret, SYM_L_GLOBAL)
7038c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
704905a36a2SIngo Molnar	/*
705905a36a2SIngo Molnar	 * Are we returning to a stack segment from the LDT?  Note: in
706905a36a2SIngo Molnar	 * 64-bit mode SS:RSP on the exception stack is always valid.
707905a36a2SIngo Molnar	 */
708905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
709905a36a2SIngo Molnar	testb	$4, (SS-RIP)(%rsp)
710905a36a2SIngo Molnar	jnz	native_irq_return_ldt
711905a36a2SIngo Molnar#endif
712905a36a2SIngo Molnar
713cc66936eSJiri SlabySYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL)
714905a36a2SIngo Molnar	/*
715905a36a2SIngo Molnar	 * This may fault.  Non-paranoid faults on return to userspace are
716905a36a2SIngo Molnar	 * handled by fixup_bad_iret.  These include #SS, #GP, and #NP.
717905a36a2SIngo Molnar	 * Double-faults due to espfix64 are handled in do_double_fault.
718905a36a2SIngo Molnar	 * Other faults here are fatal.
719905a36a2SIngo Molnar	 */
720905a36a2SIngo Molnar	iretq
721905a36a2SIngo Molnar
722905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
723905a36a2SIngo Molnarnative_irq_return_ldt:
72485063facSAndy Lutomirski	/*
72585063facSAndy Lutomirski	 * We are running with user GSBASE.  All GPRs contain their user
72685063facSAndy Lutomirski	 * values.  We have a percpu ESPFIX stack that is eight slots
72785063facSAndy Lutomirski	 * long (see ESPFIX_STACK_SIZE).  espfix_waddr points to the bottom
72885063facSAndy Lutomirski	 * of the ESPFIX stack.
72985063facSAndy Lutomirski	 *
73085063facSAndy Lutomirski	 * We clobber RAX and RDI in this code.  We stash RDI on the
73185063facSAndy Lutomirski	 * normal stack and RAX on the ESPFIX stack.
73285063facSAndy Lutomirski	 *
73385063facSAndy Lutomirski	 * The ESPFIX stack layout we set up looks like this:
73485063facSAndy Lutomirski	 *
73585063facSAndy Lutomirski	 * --- top of ESPFIX stack ---
73685063facSAndy Lutomirski	 * SS
73785063facSAndy Lutomirski	 * RSP
73885063facSAndy Lutomirski	 * RFLAGS
73985063facSAndy Lutomirski	 * CS
74085063facSAndy Lutomirski	 * RIP  <-- RSP points here when we're done
74185063facSAndy Lutomirski	 * RAX  <-- espfix_waddr points here
74285063facSAndy Lutomirski	 * --- bottom of ESPFIX stack ---
74385063facSAndy Lutomirski	 */
74485063facSAndy Lutomirski
74585063facSAndy Lutomirski	pushq	%rdi				/* Stash user RDI */
7468a09317bSDave Hansen	SWAPGS					/* to kernel GS */
7478a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi	/* to kernel CR3 */
7488a09317bSDave Hansen
749905a36a2SIngo Molnar	movq	PER_CPU_VAR(espfix_waddr), %rdi
75085063facSAndy Lutomirski	movq	%rax, (0*8)(%rdi)		/* user RAX */
75185063facSAndy Lutomirski	movq	(1*8)(%rsp), %rax		/* user RIP */
752905a36a2SIngo Molnar	movq	%rax, (1*8)(%rdi)
75385063facSAndy Lutomirski	movq	(2*8)(%rsp), %rax		/* user CS */
754905a36a2SIngo Molnar	movq	%rax, (2*8)(%rdi)
75585063facSAndy Lutomirski	movq	(3*8)(%rsp), %rax		/* user RFLAGS */
756905a36a2SIngo Molnar	movq	%rax, (3*8)(%rdi)
75785063facSAndy Lutomirski	movq	(5*8)(%rsp), %rax		/* user SS */
758905a36a2SIngo Molnar	movq	%rax, (5*8)(%rdi)
75985063facSAndy Lutomirski	movq	(4*8)(%rsp), %rax		/* user RSP */
760905a36a2SIngo Molnar	movq	%rax, (4*8)(%rdi)
76185063facSAndy Lutomirski	/* Now RAX == RSP. */
76285063facSAndy Lutomirski
76385063facSAndy Lutomirski	andl	$0xffff0000, %eax		/* RAX = (RSP & 0xffff0000) */
76485063facSAndy Lutomirski
76585063facSAndy Lutomirski	/*
76685063facSAndy Lutomirski	 * espfix_stack[31:16] == 0.  The page tables are set up such that
76785063facSAndy Lutomirski	 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
76885063facSAndy Lutomirski	 * espfix_waddr for any X.  That is, there are 65536 RO aliases of
76985063facSAndy Lutomirski	 * the same page.  Set up RSP so that RSP[31:16] contains the
77085063facSAndy Lutomirski	 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
77185063facSAndy Lutomirski	 * still points to an RO alias of the ESPFIX stack.
77285063facSAndy Lutomirski	 */
773905a36a2SIngo Molnar	orq	PER_CPU_VAR(espfix_stack), %rax
7748a09317bSDave Hansen
7756fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
7768a09317bSDave Hansen	SWAPGS					/* to user GS */
7778a09317bSDave Hansen	popq	%rdi				/* Restore user RDI */
7788a09317bSDave Hansen
779905a36a2SIngo Molnar	movq	%rax, %rsp
7808c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
78185063facSAndy Lutomirski
78285063facSAndy Lutomirski	/*
78385063facSAndy Lutomirski	 * At this point, we cannot write to the stack any more, but we can
78485063facSAndy Lutomirski	 * still read.
78585063facSAndy Lutomirski	 */
78685063facSAndy Lutomirski	popq	%rax				/* Restore user RAX */
78785063facSAndy Lutomirski
78885063facSAndy Lutomirski	/*
78985063facSAndy Lutomirski	 * RSP now points to an ordinary IRET frame, except that the page
79085063facSAndy Lutomirski	 * is read-only and RSP[31:16] are preloaded with the userspace
79185063facSAndy Lutomirski	 * values.  We can now IRET back to userspace.
79285063facSAndy Lutomirski	 */
793905a36a2SIngo Molnar	jmp	native_irq_return_iret
794905a36a2SIngo Molnar#endif
795cc66936eSJiri SlabySYM_CODE_END(common_interrupt)
796a50480cbSAndrea Righi_ASM_NOKPROBE(common_interrupt)
797905a36a2SIngo Molnar
798905a36a2SIngo Molnar/*
799905a36a2SIngo Molnar * APIC interrupts.
800905a36a2SIngo Molnar */
801905a36a2SIngo Molnar.macro apicinterrupt3 num sym do_sym
802bc7b11c0SJiri SlabySYM_CODE_START(\sym)
8038c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
804905a36a2SIngo Molnar	pushq	$~(\num)
8053aa99fc3SDominik Brodowski	call	interrupt_entry
8063aa99fc3SDominik Brodowski	UNWIND_HINT_REGS indirect=1
8073aa99fc3SDominik Brodowski	call	\do_sym	/* rdi points to pt_regs */
808905a36a2SIngo Molnar	jmp	ret_from_intr
809bc7b11c0SJiri SlabySYM_CODE_END(\sym)
810a50480cbSAndrea Righi_ASM_NOKPROBE(\sym)
811905a36a2SIngo Molnar.endm
812905a36a2SIngo Molnar
813469f0023SAlexander Potapenko/* Make sure APIC interrupt handlers end up in the irqentry section: */
814469f0023SAlexander Potapenko#define PUSH_SECTION_IRQENTRY	.pushsection .irqentry.text, "ax"
815469f0023SAlexander Potapenko#define POP_SECTION_IRQENTRY	.popsection
816469f0023SAlexander Potapenko
817905a36a2SIngo Molnar.macro apicinterrupt num sym do_sym
818469f0023SAlexander PotapenkoPUSH_SECTION_IRQENTRY
819905a36a2SIngo Molnarapicinterrupt3 \num \sym \do_sym
820469f0023SAlexander PotapenkoPOP_SECTION_IRQENTRY
821905a36a2SIngo Molnar.endm
822905a36a2SIngo Molnar
823905a36a2SIngo Molnar#ifdef CONFIG_SMP
8244d732138SIngo Molnarapicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR		irq_move_cleanup_interrupt	smp_irq_move_cleanup_interrupt
8254d732138SIngo Molnarapicinterrupt3 REBOOT_VECTOR			reboot_interrupt		smp_reboot_interrupt
826905a36a2SIngo Molnar#endif
827905a36a2SIngo Molnar
828905a36a2SIngo Molnar#ifdef CONFIG_X86_UV
8294d732138SIngo Molnarapicinterrupt3 UV_BAU_MESSAGE			uv_bau_message_intr1		uv_bau_message_interrupt
830905a36a2SIngo Molnar#endif
8314d732138SIngo Molnar
8324d732138SIngo Molnarapicinterrupt LOCAL_TIMER_VECTOR		apic_timer_interrupt		smp_apic_timer_interrupt
8334d732138SIngo Molnarapicinterrupt X86_PLATFORM_IPI_VECTOR		x86_platform_ipi		smp_x86_platform_ipi
834905a36a2SIngo Molnar
835905a36a2SIngo Molnar#ifdef CONFIG_HAVE_KVM
8364d732138SIngo Molnarapicinterrupt3 POSTED_INTR_VECTOR		kvm_posted_intr_ipi		smp_kvm_posted_intr_ipi
8374d732138SIngo Molnarapicinterrupt3 POSTED_INTR_WAKEUP_VECTOR	kvm_posted_intr_wakeup_ipi	smp_kvm_posted_intr_wakeup_ipi
838210f84b0SWincy Vanapicinterrupt3 POSTED_INTR_NESTED_VECTOR	kvm_posted_intr_nested_ipi	smp_kvm_posted_intr_nested_ipi
839905a36a2SIngo Molnar#endif
840905a36a2SIngo Molnar
841905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE_THRESHOLD
8424d732138SIngo Molnarapicinterrupt THRESHOLD_APIC_VECTOR		threshold_interrupt		smp_threshold_interrupt
843905a36a2SIngo Molnar#endif
844905a36a2SIngo Molnar
8459dda1658SIngo Molnar#ifdef CONFIG_X86_MCE_AMD
8464d732138SIngo Molnarapicinterrupt DEFERRED_ERROR_VECTOR		deferred_error_interrupt	smp_deferred_error_interrupt
8479dda1658SIngo Molnar#endif
8489dda1658SIngo Molnar
849905a36a2SIngo Molnar#ifdef CONFIG_X86_THERMAL_VECTOR
8504d732138SIngo Molnarapicinterrupt THERMAL_APIC_VECTOR		thermal_interrupt		smp_thermal_interrupt
851905a36a2SIngo Molnar#endif
852905a36a2SIngo Molnar
853905a36a2SIngo Molnar#ifdef CONFIG_SMP
8544d732138SIngo Molnarapicinterrupt CALL_FUNCTION_SINGLE_VECTOR	call_function_single_interrupt	smp_call_function_single_interrupt
8554d732138SIngo Molnarapicinterrupt CALL_FUNCTION_VECTOR		call_function_interrupt		smp_call_function_interrupt
8564d732138SIngo Molnarapicinterrupt RESCHEDULE_VECTOR			reschedule_interrupt		smp_reschedule_interrupt
857905a36a2SIngo Molnar#endif
858905a36a2SIngo Molnar
8594d732138SIngo Molnarapicinterrupt ERROR_APIC_VECTOR			error_interrupt			smp_error_interrupt
8604d732138SIngo Molnarapicinterrupt SPURIOUS_APIC_VECTOR		spurious_interrupt		smp_spurious_interrupt
861905a36a2SIngo Molnar
862905a36a2SIngo Molnar#ifdef CONFIG_IRQ_WORK
8634d732138SIngo Molnarapicinterrupt IRQ_WORK_VECTOR			irq_work_interrupt		smp_irq_work_interrupt
864905a36a2SIngo Molnar#endif
865905a36a2SIngo Molnar
866905a36a2SIngo Molnar/*
867905a36a2SIngo Molnar * Exception entry points.
868905a36a2SIngo Molnar */
8698f34c5b5SThomas Gleixner#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss_rw) + (TSS_ist + (x) * 8)
870905a36a2SIngo Molnar
871a0d14b89SPeter Zijlstra.macro idtentry_part do_sym, has_error_code:req, read_cr2:req, paranoid:req, shift_ist=-1, ist_offset=0
8722fd37912SPeter Zijlstra
8732fd37912SPeter Zijlstra	.if \paranoid
8742fd37912SPeter Zijlstra	call	paranoid_entry
8752fd37912SPeter Zijlstra	/* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
8762fd37912SPeter Zijlstra	.else
8772fd37912SPeter Zijlstra	call	error_entry
8782fd37912SPeter Zijlstra	.endif
8792fd37912SPeter Zijlstra	UNWIND_HINT_REGS
8802fd37912SPeter Zijlstra
881a0d14b89SPeter Zijlstra	.if \read_cr2
8826879298bSThomas Gleixner	/*
8836879298bSThomas Gleixner	 * Store CR2 early so subsequent faults cannot clobber it. Use R12 as
8846879298bSThomas Gleixner	 * intermediate storage as RDX can be clobbered in enter_from_user_mode().
8856879298bSThomas Gleixner	 * GET_CR2_INTO can clobber RAX.
8866879298bSThomas Gleixner	 */
8876879298bSThomas Gleixner	GET_CR2_INTO(%r12);
888a0d14b89SPeter Zijlstra	.endif
889a0d14b89SPeter Zijlstra
8902fd37912SPeter Zijlstra	.if \shift_ist != -1
8912fd37912SPeter Zijlstra	TRACE_IRQS_OFF_DEBUG			/* reload IDT in case of recursion */
8922fd37912SPeter Zijlstra	.else
8932fd37912SPeter Zijlstra	TRACE_IRQS_OFF
8942fd37912SPeter Zijlstra	.endif
895a0d14b89SPeter Zijlstra
89672500589SThomas Gleixner#ifdef CONFIG_CONTEXT_TRACKING
897a0d14b89SPeter Zijlstra	.if \paranoid == 0
898a0d14b89SPeter Zijlstra	testb	$3, CS(%rsp)
899a0d14b89SPeter Zijlstra	jz	.Lfrom_kernel_no_context_tracking_\@
900a0d14b89SPeter Zijlstra	CALL_enter_from_user_mode
901a0d14b89SPeter Zijlstra.Lfrom_kernel_no_context_tracking_\@:
9022fd37912SPeter Zijlstra	.endif
90372500589SThomas Gleixner#endif
9042fd37912SPeter Zijlstra
9052fd37912SPeter Zijlstra	movq	%rsp, %rdi			/* pt_regs pointer */
9062fd37912SPeter Zijlstra
9072fd37912SPeter Zijlstra	.if \has_error_code
9082fd37912SPeter Zijlstra	movq	ORIG_RAX(%rsp), %rsi		/* get error code */
9092fd37912SPeter Zijlstra	movq	$-1, ORIG_RAX(%rsp)		/* no syscall to restart */
9102fd37912SPeter Zijlstra	.else
9112fd37912SPeter Zijlstra	xorl	%esi, %esi			/* no error code */
9122fd37912SPeter Zijlstra	.endif
9132fd37912SPeter Zijlstra
9142fd37912SPeter Zijlstra	.if \shift_ist != -1
9152fd37912SPeter Zijlstra	subq	$\ist_offset, CPU_TSS_IST(\shift_ist)
9162fd37912SPeter Zijlstra	.endif
9172fd37912SPeter Zijlstra
9186879298bSThomas Gleixner	.if \read_cr2
9196879298bSThomas Gleixner	movq	%r12, %rdx			/* Move CR2 into 3rd argument */
9206879298bSThomas Gleixner	.endif
9216879298bSThomas Gleixner
9222fd37912SPeter Zijlstra	call	\do_sym
9232fd37912SPeter Zijlstra
9242fd37912SPeter Zijlstra	.if \shift_ist != -1
9252fd37912SPeter Zijlstra	addq	$\ist_offset, CPU_TSS_IST(\shift_ist)
9262fd37912SPeter Zijlstra	.endif
9272fd37912SPeter Zijlstra
9282fd37912SPeter Zijlstra	.if \paranoid
9292fd37912SPeter Zijlstra	/* this procedure expect "no swapgs" flag in ebx */
9302fd37912SPeter Zijlstra	jmp	paranoid_exit
9312fd37912SPeter Zijlstra	.else
9322fd37912SPeter Zijlstra	jmp	error_exit
9332fd37912SPeter Zijlstra	.endif
9342fd37912SPeter Zijlstra
9352fd37912SPeter Zijlstra.endm
9362fd37912SPeter Zijlstra
937bd7b1f7cSAndy Lutomirski/**
938bd7b1f7cSAndy Lutomirski * idtentry - Generate an IDT entry stub
939bd7b1f7cSAndy Lutomirski * @sym:		Name of the generated entry point
940bd7b1f7cSAndy Lutomirski * @do_sym:		C function to be called
941bd7b1f7cSAndy Lutomirski * @has_error_code:	True if this IDT vector has an error code on the stack
942bd7b1f7cSAndy Lutomirski * @paranoid:		non-zero means that this vector may be invoked from
943bd7b1f7cSAndy Lutomirski *			kernel mode with user GSBASE and/or user CR3.
944bd7b1f7cSAndy Lutomirski *			2 is special -- see below.
945bd7b1f7cSAndy Lutomirski * @shift_ist:		Set to an IST index if entries from kernel mode should
946bd7b1f7cSAndy Lutomirski *			decrement the IST stack so that nested entries get a
947bd7b1f7cSAndy Lutomirski *			fresh stack.  (This is for #DB, which has a nasty habit
948bd7b1f7cSAndy Lutomirski *			of recursing.)
9494234653eSPeter Zijlstra * @create_gap:		create a 6-word stack gap when coming from kernel mode.
950a0d14b89SPeter Zijlstra * @read_cr2:		load CR2 into the 3rd argument; done before calling any C code
951bd7b1f7cSAndy Lutomirski *
952bd7b1f7cSAndy Lutomirski * idtentry generates an IDT stub that sets up a usable kernel context,
953bd7b1f7cSAndy Lutomirski * creates struct pt_regs, and calls @do_sym.  The stub has the following
954bd7b1f7cSAndy Lutomirski * special behaviors:
955bd7b1f7cSAndy Lutomirski *
956bd7b1f7cSAndy Lutomirski * On an entry from user mode, the stub switches from the trampoline or
957bd7b1f7cSAndy Lutomirski * IST stack to the normal thread stack.  On an exit to user mode, the
958bd7b1f7cSAndy Lutomirski * normal exit-to-usermode path is invoked.
959bd7b1f7cSAndy Lutomirski *
960bd7b1f7cSAndy Lutomirski * On an exit to kernel mode, if @paranoid == 0, we check for preemption,
961bd7b1f7cSAndy Lutomirski * whereas we omit the preemption check if @paranoid != 0.  This is purely
962bd7b1f7cSAndy Lutomirski * because the implementation is simpler this way.  The kernel only needs
963bd7b1f7cSAndy Lutomirski * to check for asynchronous kernel preemption when IRQ handlers return.
964bd7b1f7cSAndy Lutomirski *
965bd7b1f7cSAndy Lutomirski * If @paranoid == 0, then the stub will handle IRET faults by pretending
966bd7b1f7cSAndy Lutomirski * that the fault came from user mode.  It will handle gs_change faults by
967bd7b1f7cSAndy Lutomirski * pretending that the fault happened with kernel GSBASE.  Since this handling
968bd7b1f7cSAndy Lutomirski * is omitted for @paranoid != 0, the #GP, #SS, and #NP stubs must have
969bd7b1f7cSAndy Lutomirski * @paranoid == 0.  This special handling will do the wrong thing for
970bd7b1f7cSAndy Lutomirski * espfix-induced #DF on IRET, so #DF must not use @paranoid == 0.
971bd7b1f7cSAndy Lutomirski *
972bd7b1f7cSAndy Lutomirski * @paranoid == 2 is special: the stub will never switch stacks.  This is for
973bd7b1f7cSAndy Lutomirski * #DF: if the thread stack is somehow unusable, we'll still get a useful OOPS.
974bd7b1f7cSAndy Lutomirski */
975a0d14b89SPeter Zijlstra.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 ist_offset=0 create_gap=0 read_cr2=0
976bc7b11c0SJiri SlabySYM_CODE_START(\sym)
97798990a33SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=\has_error_code*8
9788c1f7558SJosh Poimboeuf
979905a36a2SIngo Molnar	/* Sanity check */
9804234653eSPeter Zijlstra	.if \shift_ist != -1 && \paranoid != 1
981905a36a2SIngo Molnar	.error "using shift_ist requires paranoid=1"
982905a36a2SIngo Molnar	.endif
983905a36a2SIngo Molnar
9844234653eSPeter Zijlstra	.if \create_gap && \paranoid
9854234653eSPeter Zijlstra	.error "using create_gap requires paranoid=0"
9864234653eSPeter Zijlstra	.endif
9874234653eSPeter Zijlstra
988905a36a2SIngo Molnar	ASM_CLAC
989905a36a2SIngo Molnar
99082c62fa0SJosh Poimboeuf	.if \has_error_code == 0
991905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
992905a36a2SIngo Molnar	.endif
993905a36a2SIngo Molnar
994071ccc96SAndy Lutomirski	.if \paranoid == 1
9959e809d15SDominik Brodowski	testb	$3, CS-ORIG_RAX(%rsp)		/* If coming from userspace, switch stacks */
9967f2590a1SAndy Lutomirski	jnz	.Lfrom_usermode_switch_stack_\@
997905a36a2SIngo Molnar	.endif
9987f2590a1SAndy Lutomirski
9992700fefdSJosh Poimboeuf	.if \create_gap == 1
10002700fefdSJosh Poimboeuf	/*
10012700fefdSJosh Poimboeuf	 * If coming from kernel space, create a 6-word gap to allow the
10022700fefdSJosh Poimboeuf	 * int3 handler to emulate a call instruction.
10032700fefdSJosh Poimboeuf	 */
10042700fefdSJosh Poimboeuf	testb	$3, CS-ORIG_RAX(%rsp)
10052700fefdSJosh Poimboeuf	jnz	.Lfrom_usermode_no_gap_\@
10062700fefdSJosh Poimboeuf	.rept	6
10072700fefdSJosh Poimboeuf	pushq	5*8(%rsp)
10082700fefdSJosh Poimboeuf	.endr
10092700fefdSJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
10102700fefdSJosh Poimboeuf.Lfrom_usermode_no_gap_\@:
10112700fefdSJosh Poimboeuf	.endif
10122700fefdSJosh Poimboeuf
1013a0d14b89SPeter Zijlstra	idtentry_part \do_sym, \has_error_code, \read_cr2, \paranoid, \shift_ist, \ist_offset
1014905a36a2SIngo Molnar
1015071ccc96SAndy Lutomirski	.if \paranoid == 1
1016905a36a2SIngo Molnar	/*
10177f2590a1SAndy Lutomirski	 * Entry from userspace.  Switch stacks and treat it
1018905a36a2SIngo Molnar	 * as a normal entry.  This means that paranoid handlers
1019905a36a2SIngo Molnar	 * run in real process context if user_mode(regs).
1020905a36a2SIngo Molnar	 */
10217f2590a1SAndy Lutomirski.Lfrom_usermode_switch_stack_\@:
1022a0d14b89SPeter Zijlstra	idtentry_part \do_sym, \has_error_code, \read_cr2, paranoid=0
1023905a36a2SIngo Molnar	.endif
1024905a36a2SIngo Molnar
1025a50480cbSAndrea Righi_ASM_NOKPROBE(\sym)
1026bc7b11c0SJiri SlabySYM_CODE_END(\sym)
1027905a36a2SIngo Molnar.endm
1028905a36a2SIngo Molnar
1029905a36a2SIngo Molnaridtentry divide_error			do_divide_error			has_error_code=0
1030905a36a2SIngo Molnaridtentry overflow			do_overflow			has_error_code=0
1031905a36a2SIngo Molnaridtentry bounds				do_bounds			has_error_code=0
1032905a36a2SIngo Molnaridtentry invalid_op			do_invalid_op			has_error_code=0
1033905a36a2SIngo Molnaridtentry device_not_available		do_device_not_available		has_error_code=0
1034a0d14b89SPeter Zijlstraidtentry double_fault			do_double_fault			has_error_code=1 paranoid=2 read_cr2=1
1035905a36a2SIngo Molnaridtentry coprocessor_segment_overrun	do_coprocessor_segment_overrun	has_error_code=0
1036905a36a2SIngo Molnaridtentry invalid_TSS			do_invalid_TSS			has_error_code=1
1037905a36a2SIngo Molnaridtentry segment_not_present		do_segment_not_present		has_error_code=1
1038905a36a2SIngo Molnaridtentry spurious_interrupt_bug		do_spurious_interrupt_bug	has_error_code=0
1039905a36a2SIngo Molnaridtentry coprocessor_error		do_coprocessor_error		has_error_code=0
1040905a36a2SIngo Molnaridtentry alignment_check		do_alignment_check		has_error_code=1
1041905a36a2SIngo Molnaridtentry simd_coprocessor_error		do_simd_coprocessor_error	has_error_code=0
1042905a36a2SIngo Molnar
1043905a36a2SIngo Molnar
10444d732138SIngo Molnar/*
10454d732138SIngo Molnar * Reload gs selector with exception handling
10464d732138SIngo Molnar * edi:  new selector
1047*b9f6976bSThomas Gleixner *
1048*b9f6976bSThomas Gleixner * Is in entry.text as it shouldn't be instrumented.
10494d732138SIngo Molnar */
10506dcc5627SJiri SlabySYM_FUNC_START(native_load_gs_index)
10518c1f7558SJosh Poimboeuf	FRAME_BEGIN
1052905a36a2SIngo Molnar	pushfq
1053905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1054ca37e57bSAndy Lutomirski	TRACE_IRQS_OFF
1055905a36a2SIngo Molnar	SWAPGS
105642c748bbSBorislav Petkov.Lgs_change:
1057905a36a2SIngo Molnar	movl	%edi, %gs
105896e5d28aSBorislav Petkov2:	ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
1059905a36a2SIngo Molnar	SWAPGS
1060ca37e57bSAndy Lutomirski	TRACE_IRQS_FLAGS (%rsp)
1061905a36a2SIngo Molnar	popfq
10628c1f7558SJosh Poimboeuf	FRAME_END
1063905a36a2SIngo Molnar	ret
10646dcc5627SJiri SlabySYM_FUNC_END(native_load_gs_index)
1065784d5699SAl ViroEXPORT_SYMBOL(native_load_gs_index)
1066905a36a2SIngo Molnar
106798ededb6SJiri Slaby	_ASM_EXTABLE(.Lgs_change, .Lbad_gs)
1068905a36a2SIngo Molnar	.section .fixup, "ax"
1069905a36a2SIngo Molnar	/* running with kernelgs */
1070ef77e688SJiri SlabySYM_CODE_START_LOCAL_NOALIGN(.Lbad_gs)
1071905a36a2SIngo Molnar	SWAPGS					/* switch back to user gs */
1072b038c842SAndy Lutomirski.macro ZAP_GS
1073b038c842SAndy Lutomirski	/* This can't be a string because the preprocessor needs to see it. */
1074b038c842SAndy Lutomirski	movl $__USER_DS, %eax
1075b038c842SAndy Lutomirski	movl %eax, %gs
1076b038c842SAndy Lutomirski.endm
1077b038c842SAndy Lutomirski	ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
1078905a36a2SIngo Molnar	xorl	%eax, %eax
1079905a36a2SIngo Molnar	movl	%eax, %gs
1080905a36a2SIngo Molnar	jmp	2b
1081ef77e688SJiri SlabySYM_CODE_END(.Lbad_gs)
1082905a36a2SIngo Molnar	.previous
1083905a36a2SIngo Molnar
1084905a36a2SIngo Molnar/* Call softirq on interrupt stack. Interrupts are off. */
1085*b9f6976bSThomas Gleixner.pushsection .text, "ax"
10866dcc5627SJiri SlabySYM_FUNC_START(do_softirq_own_stack)
1087905a36a2SIngo Molnar	pushq	%rbp
1088905a36a2SIngo Molnar	mov	%rsp, %rbp
10898c1f7558SJosh Poimboeuf	ENTER_IRQ_STACK regs=0 old_rsp=%r11
1090905a36a2SIngo Molnar	call	__do_softirq
10918c1f7558SJosh Poimboeuf	LEAVE_IRQ_STACK regs=0
1092905a36a2SIngo Molnar	leaveq
1093905a36a2SIngo Molnar	ret
10946dcc5627SJiri SlabySYM_FUNC_END(do_softirq_own_stack)
1095*b9f6976bSThomas Gleixner.popsection
1096905a36a2SIngo Molnar
109728c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV
10985878d5d6SJuergen Grossidtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0
1099905a36a2SIngo Molnar
1100905a36a2SIngo Molnar/*
1101905a36a2SIngo Molnar * A note on the "critical region" in our callback handler.
1102905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring
1103905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled
1104905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before
1105905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still
1106905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack.
1107905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd
1108905a36a2SIngo Molnar * like to avoid the possibility.
1109905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an
1110905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current
1111905a36a2SIngo Molnar * activation and restart the handler using the previous one.
1112905a36a2SIngo Molnar */
1113ef1e0315SJiri Slaby/* do_hypervisor_callback(struct *pt_regs) */
1114ef1e0315SJiri SlabySYM_CODE_START_LOCAL(xen_do_hypervisor_callback)
11154d732138SIngo Molnar
1116905a36a2SIngo Molnar/*
1117905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1118905a36a2SIngo Molnar * see the correct pointer to the pt_regs
1119905a36a2SIngo Molnar */
11208c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
11214d732138SIngo Molnar	movq	%rdi, %rsp			/* we don't return, adjust the stack frame */
11228c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
11231d3e53e8SAndy Lutomirski
11241d3e53e8SAndy Lutomirski	ENTER_IRQ_STACK old_rsp=%r10
1125905a36a2SIngo Molnar	call	xen_evtchn_do_upcall
11261d3e53e8SAndy Lutomirski	LEAVE_IRQ_STACK
11271d3e53e8SAndy Lutomirski
112848593975SThomas Gleixner#ifndef CONFIG_PREEMPTION
1129905a36a2SIngo Molnar	call	xen_maybe_preempt_hcall
1130905a36a2SIngo Molnar#endif
1131905a36a2SIngo Molnar	jmp	error_exit
1132ef1e0315SJiri SlabySYM_CODE_END(xen_do_hypervisor_callback)
1133905a36a2SIngo Molnar
1134905a36a2SIngo Molnar/*
1135905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes.
1136905a36a2SIngo Molnar * We get here for two reasons:
1137905a36a2SIngo Molnar *  1. Fault while reloading DS, ES, FS or GS
1138905a36a2SIngo Molnar *  2. Fault while executing IRET
1139905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment
1140905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others.
1141905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the
1142905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall
1143905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1144905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register
1145905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1.
1146905a36a2SIngo Molnar */
1147bc7b11c0SJiri SlabySYM_CODE_START(xen_failsafe_callback)
11488c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
1149905a36a2SIngo Molnar	movl	%ds, %ecx
1150905a36a2SIngo Molnar	cmpw	%cx, 0x10(%rsp)
1151905a36a2SIngo Molnar	jne	1f
1152905a36a2SIngo Molnar	movl	%es, %ecx
1153905a36a2SIngo Molnar	cmpw	%cx, 0x18(%rsp)
1154905a36a2SIngo Molnar	jne	1f
1155905a36a2SIngo Molnar	movl	%fs, %ecx
1156905a36a2SIngo Molnar	cmpw	%cx, 0x20(%rsp)
1157905a36a2SIngo Molnar	jne	1f
1158905a36a2SIngo Molnar	movl	%gs, %ecx
1159905a36a2SIngo Molnar	cmpw	%cx, 0x28(%rsp)
1160905a36a2SIngo Molnar	jne	1f
1161905a36a2SIngo Molnar	/* All segments match their saved values => Category 2 (Bad IRET). */
1162905a36a2SIngo Molnar	movq	(%rsp), %rcx
1163905a36a2SIngo Molnar	movq	8(%rsp), %r11
1164905a36a2SIngo Molnar	addq	$0x30, %rsp
1165905a36a2SIngo Molnar	pushq	$0				/* RIP */
11668c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
1167905a36a2SIngo Molnar	jmp	general_protection
1168905a36a2SIngo Molnar1:	/* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1169905a36a2SIngo Molnar	movq	(%rsp), %rcx
1170905a36a2SIngo Molnar	movq	8(%rsp), %r11
1171905a36a2SIngo Molnar	addq	$0x30, %rsp
11728c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1173905a36a2SIngo Molnar	pushq	$-1 /* orig_ax = -1 => not a system call */
11743f01daecSDominik Brodowski	PUSH_AND_CLEAR_REGS
1175946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
1176905a36a2SIngo Molnar	jmp	error_exit
1177bc7b11c0SJiri SlabySYM_CODE_END(xen_failsafe_callback)
117828c11b0fSJuergen Gross#endif /* CONFIG_XEN_PV */
1179905a36a2SIngo Molnar
118028c11b0fSJuergen Gross#ifdef CONFIG_XEN_PVHVM
1181905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1182905a36a2SIngo Molnar	xen_hvm_callback_vector xen_evtchn_do_upcall
118328c11b0fSJuergen Gross#endif
1184905a36a2SIngo Molnar
1185905a36a2SIngo Molnar
1186905a36a2SIngo Molnar#if IS_ENABLED(CONFIG_HYPERV)
1187905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1188905a36a2SIngo Molnar	hyperv_callback_vector hyperv_vector_handler
118993286261SVitaly Kuznetsov
119093286261SVitaly Kuznetsovapicinterrupt3 HYPERV_REENLIGHTENMENT_VECTOR \
119193286261SVitaly Kuznetsov	hyperv_reenlightenment_vector hyperv_reenlightenment_intr
1192248e742aSMichael Kelley
1193248e742aSMichael Kelleyapicinterrupt3 HYPERV_STIMER0_VECTOR \
1194248e742aSMichael Kelley	hv_stimer0_callback_vector hv_stimer0_vector_handler
1195905a36a2SIngo Molnar#endif /* CONFIG_HYPERV */
1196905a36a2SIngo Molnar
1197498ad393SZhao Yakui#if IS_ENABLED(CONFIG_ACRN_GUEST)
1198498ad393SZhao Yakuiapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1199498ad393SZhao Yakui	acrn_hv_callback_vector acrn_hv_vector_handler
1200498ad393SZhao Yakui#endif
1201498ad393SZhao Yakui
12022a594d4cSThomas Gleixneridtentry debug			do_debug		has_error_code=0	paranoid=1 shift_ist=IST_INDEX_DB ist_offset=DB_STACK_OFFSET
12032700fefdSJosh Poimboeufidtentry int3			do_int3			has_error_code=0	create_gap=1
1204905a36a2SIngo Molnaridtentry stack_segment		do_stack_segment	has_error_code=1
12054d732138SIngo Molnar
120628c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV
120743e41110SJuergen Grossidtentry xennmi			do_nmi			has_error_code=0
12085878d5d6SJuergen Grossidtentry xendebug		do_debug		has_error_code=0
1209905a36a2SIngo Molnar#endif
12104d732138SIngo Molnar
1211905a36a2SIngo Molnaridtentry general_protection	do_general_protection	has_error_code=1
1212a0d14b89SPeter Zijlstraidtentry page_fault		do_page_fault		has_error_code=1	read_cr2=1
12134d732138SIngo Molnar
1214905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE
12156f41c34dSThomas Gleixneridtentry machine_check		do_mce			has_error_code=0	paranoid=1
1216905a36a2SIngo Molnar#endif
1217905a36a2SIngo Molnar
1218905a36a2SIngo Molnar/*
12199e809d15SDominik Brodowski * Save all registers in pt_regs, and switch gs if needed.
1220905a36a2SIngo Molnar * Use slow, but surefire "are we in kernel?" check.
1221905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1222905a36a2SIngo Molnar */
1223ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_entry)
12248c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
1225905a36a2SIngo Molnar	cld
12269e809d15SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
12279e809d15SDominik Brodowski	ENCODE_FRAME_POINTER 8
1228905a36a2SIngo Molnar	movl	$1, %ebx
1229905a36a2SIngo Molnar	movl	$MSR_GS_BASE, %ecx
1230905a36a2SIngo Molnar	rdmsr
1231905a36a2SIngo Molnar	testl	%edx, %edx
1232905a36a2SIngo Molnar	js	1f				/* negative -> in kernel */
1233905a36a2SIngo Molnar	SWAPGS
1234905a36a2SIngo Molnar	xorl	%ebx, %ebx
12358a09317bSDave Hansen
12368a09317bSDave Hansen1:
123716561f27SDave Hansen	/*
123816561f27SDave Hansen	 * Always stash CR3 in %r14.  This value will be restored,
1239ae852495SAndy Lutomirski	 * verbatim, at exit.  Needed if paranoid_entry interrupted
1240ae852495SAndy Lutomirski	 * another entry that already switched to the user CR3 value
1241ae852495SAndy Lutomirski	 * but has not yet returned to userspace.
124216561f27SDave Hansen	 *
124316561f27SDave Hansen	 * This is also why CS (stashed in the "iret frame" by the
124416561f27SDave Hansen	 * hardware at entry) can not be used: this may be a return
1245ae852495SAndy Lutomirski	 * to kernel code, but with a user CR3 value.
124616561f27SDave Hansen	 */
12478a09317bSDave Hansen	SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
12488a09317bSDave Hansen
124918ec54fdSJosh Poimboeuf	/*
125018ec54fdSJosh Poimboeuf	 * The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an
125118ec54fdSJosh Poimboeuf	 * unconditional CR3 write, even in the PTI case.  So do an lfence
125218ec54fdSJosh Poimboeuf	 * to prevent GS speculation, regardless of whether PTI is enabled.
125318ec54fdSJosh Poimboeuf	 */
125418ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
125518ec54fdSJosh Poimboeuf
12568a09317bSDave Hansen	ret
1257ef1e0315SJiri SlabySYM_CODE_END(paranoid_entry)
1258905a36a2SIngo Molnar
1259905a36a2SIngo Molnar/*
1260905a36a2SIngo Molnar * "Paranoid" exit path from exception stack.  This is invoked
1261905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came
1262905a36a2SIngo Molnar * from kernel space.
1263905a36a2SIngo Molnar *
1264905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early
1265905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would
1266905a36a2SIngo Molnar * be complicated.  Fortunately, we there's no good reason
1267905a36a2SIngo Molnar * to try to handle preemption here.
12684d732138SIngo Molnar *
12694d732138SIngo Molnar * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
1270905a36a2SIngo Molnar */
1271ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_exit)
12728c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
12732140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
1274905a36a2SIngo Molnar	TRACE_IRQS_OFF_DEBUG
1275905a36a2SIngo Molnar	testl	%ebx, %ebx			/* swapgs needed? */
1276e5317832SAndy Lutomirski	jnz	.Lparanoid_exit_no_swapgs
1277905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
127816561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
127921e94459SPeter Zijlstra	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
1280905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
128145c08383SThomas Gleixner	jmp	restore_regs_and_return_to_kernel
1282e5317832SAndy Lutomirski.Lparanoid_exit_no_swapgs:
1283905a36a2SIngo Molnar	TRACE_IRQS_IRETQ_DEBUG
128416561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
1285e4865757SIngo Molnar	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
1286e5317832SAndy Lutomirski	jmp restore_regs_and_return_to_kernel
1287ef1e0315SJiri SlabySYM_CODE_END(paranoid_exit)
1288905a36a2SIngo Molnar
1289905a36a2SIngo Molnar/*
12909e809d15SDominik Brodowski * Save all registers in pt_regs, and switch GS if needed.
1291905a36a2SIngo Molnar */
1292ef1e0315SJiri SlabySYM_CODE_START_LOCAL(error_entry)
12939e809d15SDominik Brodowski	UNWIND_HINT_FUNC
1294905a36a2SIngo Molnar	cld
12959e809d15SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
12969e809d15SDominik Brodowski	ENCODE_FRAME_POINTER 8
1297905a36a2SIngo Molnar	testb	$3, CS+8(%rsp)
1298cb6f64edSAndy Lutomirski	jz	.Lerror_kernelspace
1299539f5113SAndy Lutomirski
1300cb6f64edSAndy Lutomirski	/*
1301cb6f64edSAndy Lutomirski	 * We entered from user mode or we're pretending to have entered
1302cb6f64edSAndy Lutomirski	 * from user mode due to an IRET fault.
1303cb6f64edSAndy Lutomirski	 */
1304905a36a2SIngo Molnar	SWAPGS
130518ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
13068a09317bSDave Hansen	/* We have user CR3.  Change to kernel CR3. */
13078a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
1308539f5113SAndy Lutomirski
1309cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_after_swapgs:
13107f2590a1SAndy Lutomirski	/* Put us onto the real thread stack. */
13117f2590a1SAndy Lutomirski	popq	%r12				/* save return addr in %12 */
13127f2590a1SAndy Lutomirski	movq	%rsp, %rdi			/* arg0 = pt_regs pointer */
13137f2590a1SAndy Lutomirski	call	sync_regs
13147f2590a1SAndy Lutomirski	movq	%rax, %rsp			/* switch stack */
13157f2590a1SAndy Lutomirski	ENCODE_FRAME_POINTER
13167f2590a1SAndy Lutomirski	pushq	%r12
1317f1075053SAndy Lutomirski	ret
131802bc7768SAndy Lutomirski
131918ec54fdSJosh Poimboeuf.Lerror_entry_done_lfence:
132018ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
1321cb6f64edSAndy Lutomirski.Lerror_entry_done:
1322905a36a2SIngo Molnar	ret
1323905a36a2SIngo Molnar
1324905a36a2SIngo Molnar	/*
1325905a36a2SIngo Molnar	 * There are two places in the kernel that can potentially fault with
1326905a36a2SIngo Molnar	 * usergs. Handle them here.  B stepping K8s sometimes report a
1327905a36a2SIngo Molnar	 * truncated RIP for IRET exceptions returning to compat mode. Check
1328905a36a2SIngo Molnar	 * for these here too.
1329905a36a2SIngo Molnar	 */
1330cb6f64edSAndy Lutomirski.Lerror_kernelspace:
1331905a36a2SIngo Molnar	leaq	native_irq_return_iret(%rip), %rcx
1332905a36a2SIngo Molnar	cmpq	%rcx, RIP+8(%rsp)
1333cb6f64edSAndy Lutomirski	je	.Lerror_bad_iret
1334905a36a2SIngo Molnar	movl	%ecx, %eax			/* zero extend */
1335905a36a2SIngo Molnar	cmpq	%rax, RIP+8(%rsp)
1336cb6f64edSAndy Lutomirski	je	.Lbstep_iret
133742c748bbSBorislav Petkov	cmpq	$.Lgs_change, RIP+8(%rsp)
133818ec54fdSJosh Poimboeuf	jne	.Lerror_entry_done_lfence
1339539f5113SAndy Lutomirski
1340539f5113SAndy Lutomirski	/*
134142c748bbSBorislav Petkov	 * hack: .Lgs_change can fail with user gsbase.  If this happens, fix up
1342539f5113SAndy Lutomirski	 * gsbase and proceed.  We'll fix up the exception and land in
134342c748bbSBorislav Petkov	 * .Lgs_change's error handler with kernel gsbase.
1344539f5113SAndy Lutomirski	 */
13452fa5f04fSWanpeng Li	SWAPGS
134618ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
13472fa5f04fSWanpeng Li	jmp .Lerror_entry_done
1348905a36a2SIngo Molnar
1349cb6f64edSAndy Lutomirski.Lbstep_iret:
1350905a36a2SIngo Molnar	/* Fix truncated RIP */
1351905a36a2SIngo Molnar	movq	%rcx, RIP+8(%rsp)
1352905a36a2SIngo Molnar	/* fall through */
1353905a36a2SIngo Molnar
1354cb6f64edSAndy Lutomirski.Lerror_bad_iret:
1355539f5113SAndy Lutomirski	/*
13568a09317bSDave Hansen	 * We came from an IRET to user mode, so we have user
13578a09317bSDave Hansen	 * gsbase and CR3.  Switch to kernel gsbase and CR3:
1358539f5113SAndy Lutomirski	 */
1359905a36a2SIngo Molnar	SWAPGS
136018ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
13618a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
1362539f5113SAndy Lutomirski
1363539f5113SAndy Lutomirski	/*
1364539f5113SAndy Lutomirski	 * Pretend that the exception came from user mode: set up pt_regs
1365b3681dd5SAndy Lutomirski	 * as if we faulted immediately after IRET.
1366539f5113SAndy Lutomirski	 */
1367905a36a2SIngo Molnar	mov	%rsp, %rdi
1368905a36a2SIngo Molnar	call	fixup_bad_iret
1369905a36a2SIngo Molnar	mov	%rax, %rsp
1370cb6f64edSAndy Lutomirski	jmp	.Lerror_entry_from_usermode_after_swapgs
1371ef1e0315SJiri SlabySYM_CODE_END(error_entry)
1372905a36a2SIngo Molnar
1373ef1e0315SJiri SlabySYM_CODE_START_LOCAL(error_exit)
13748c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
13752140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
1376905a36a2SIngo Molnar	TRACE_IRQS_OFF
1377b3681dd5SAndy Lutomirski	testb	$3, CS(%rsp)
1378b3681dd5SAndy Lutomirski	jz	retint_kernel
137930a2441cSJiri Slaby	jmp	.Lretint_user
1380ef1e0315SJiri SlabySYM_CODE_END(error_exit)
1381905a36a2SIngo Molnar
1382929bacecSAndy Lutomirski/*
1383929bacecSAndy Lutomirski * Runs on exception stack.  Xen PV does not go through this path at all,
1384929bacecSAndy Lutomirski * so we can use real assembly here.
13858a09317bSDave Hansen *
13868a09317bSDave Hansen * Registers:
13878a09317bSDave Hansen *	%r14: Used to save/restore the CR3 of the interrupted context
13888a09317bSDave Hansen *	      when PAGE_TABLE_ISOLATION is in use.  Do not clobber.
1389929bacecSAndy Lutomirski */
1390bc7b11c0SJiri SlabySYM_CODE_START(nmi)
13918c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1392929bacecSAndy Lutomirski
1393fc57a7c6SAndy Lutomirski	/*
1394905a36a2SIngo Molnar	 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1395905a36a2SIngo Molnar	 * the iretq it performs will take us out of NMI context.
1396905a36a2SIngo Molnar	 * This means that we can have nested NMIs where the next
1397905a36a2SIngo Molnar	 * NMI is using the top of the stack of the previous NMI. We
1398905a36a2SIngo Molnar	 * can't let it execute because the nested NMI will corrupt the
1399905a36a2SIngo Molnar	 * stack of the previous NMI. NMI handlers are not re-entrant
1400905a36a2SIngo Molnar	 * anyway.
1401905a36a2SIngo Molnar	 *
1402905a36a2SIngo Molnar	 * To handle this case we do the following:
1403905a36a2SIngo Molnar	 *  Check the a special location on the stack that contains
1404905a36a2SIngo Molnar	 *  a variable that is set when NMIs are executing.
1405905a36a2SIngo Molnar	 *  The interrupted task's stack is also checked to see if it
1406905a36a2SIngo Molnar	 *  is an NMI stack.
1407905a36a2SIngo Molnar	 *  If the variable is not set and the stack is not the NMI
1408905a36a2SIngo Molnar	 *  stack then:
1409905a36a2SIngo Molnar	 *    o Set the special variable on the stack
14100b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "outermost" location on the
14110b22930eSAndy Lutomirski	 *      stack
14120b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "iret" location on the stack
1413905a36a2SIngo Molnar	 *    o Continue processing the NMI
1414905a36a2SIngo Molnar	 *  If the variable is set or the previous stack is the NMI stack:
14150b22930eSAndy Lutomirski	 *    o Modify the "iret" location to jump to the repeat_nmi
1416905a36a2SIngo Molnar	 *    o return back to the first NMI
1417905a36a2SIngo Molnar	 *
1418905a36a2SIngo Molnar	 * Now on exit of the first NMI, we first clear the stack variable
1419905a36a2SIngo Molnar	 * The NMI stack will tell any nested NMIs at that point that it is
1420905a36a2SIngo Molnar	 * nested. Then we pop the stack normally with iret, and if there was
1421905a36a2SIngo Molnar	 * a nested NMI that updated the copy interrupt stack frame, a
1422905a36a2SIngo Molnar	 * jump will be made to the repeat_nmi code that will handle the second
1423905a36a2SIngo Molnar	 * NMI.
14249b6e6a83SAndy Lutomirski	 *
14259b6e6a83SAndy Lutomirski	 * However, espfix prevents us from directly returning to userspace
14269b6e6a83SAndy Lutomirski	 * with a single IRET instruction.  Similarly, IRET to user mode
14279b6e6a83SAndy Lutomirski	 * can fault.  We therefore handle NMIs from user space like
14289b6e6a83SAndy Lutomirski	 * other IST entries.
1429905a36a2SIngo Molnar	 */
1430905a36a2SIngo Molnar
1431e93c1730SAndy Lutomirski	ASM_CLAC
1432e93c1730SAndy Lutomirski
1433905a36a2SIngo Molnar	/* Use %rdx as our temp variable throughout */
1434905a36a2SIngo Molnar	pushq	%rdx
1435905a36a2SIngo Molnar
14369b6e6a83SAndy Lutomirski	testb	$3, CS-RIP+8(%rsp)
14379b6e6a83SAndy Lutomirski	jz	.Lnmi_from_kernel
1438905a36a2SIngo Molnar
1439905a36a2SIngo Molnar	/*
14409b6e6a83SAndy Lutomirski	 * NMI from user mode.  We need to run on the thread stack, but we
14419b6e6a83SAndy Lutomirski	 * can't go through the normal entry paths: NMIs are masked, and
14429b6e6a83SAndy Lutomirski	 * we don't want to enable interrupts, because then we'll end
14439b6e6a83SAndy Lutomirski	 * up in an awkward situation in which IRQs are on but NMIs
14449b6e6a83SAndy Lutomirski	 * are off.
144583c133cfSAndy Lutomirski	 *
144683c133cfSAndy Lutomirski	 * We also must not push anything to the stack before switching
144783c133cfSAndy Lutomirski	 * stacks lest we corrupt the "NMI executing" variable.
14489b6e6a83SAndy Lutomirski	 */
14499b6e6a83SAndy Lutomirski
1450929bacecSAndy Lutomirski	swapgs
14519b6e6a83SAndy Lutomirski	cld
145218ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
14538a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
14549b6e6a83SAndy Lutomirski	movq	%rsp, %rdx
14559b6e6a83SAndy Lutomirski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
14568c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS base=%rdx offset=8
14579b6e6a83SAndy Lutomirski	pushq	5*8(%rdx)	/* pt_regs->ss */
14589b6e6a83SAndy Lutomirski	pushq	4*8(%rdx)	/* pt_regs->rsp */
14599b6e6a83SAndy Lutomirski	pushq	3*8(%rdx)	/* pt_regs->flags */
14609b6e6a83SAndy Lutomirski	pushq	2*8(%rdx)	/* pt_regs->cs */
14619b6e6a83SAndy Lutomirski	pushq	1*8(%rdx)	/* pt_regs->rip */
14628c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
14639b6e6a83SAndy Lutomirski	pushq   $-1		/* pt_regs->orig_ax */
146430907fd1SDominik Brodowski	PUSH_AND_CLEAR_REGS rdx=(%rdx)
1465946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
14669b6e6a83SAndy Lutomirski
14679b6e6a83SAndy Lutomirski	/*
14689b6e6a83SAndy Lutomirski	 * At this point we no longer need to worry about stack damage
14699b6e6a83SAndy Lutomirski	 * due to nesting -- we're on the normal thread stack and we're
14709b6e6a83SAndy Lutomirski	 * done with the NMI stack.
14719b6e6a83SAndy Lutomirski	 */
14729b6e6a83SAndy Lutomirski
14739b6e6a83SAndy Lutomirski	movq	%rsp, %rdi
14749b6e6a83SAndy Lutomirski	movq	$-1, %rsi
14759b6e6a83SAndy Lutomirski	call	do_nmi
14769b6e6a83SAndy Lutomirski
14779b6e6a83SAndy Lutomirski	/*
14789b6e6a83SAndy Lutomirski	 * Return back to user mode.  We must *not* do the normal exit
1479946c1911SJosh Poimboeuf	 * work, because we don't want to enable interrupts.
14809b6e6a83SAndy Lutomirski	 */
14818a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
14829b6e6a83SAndy Lutomirski
14839b6e6a83SAndy Lutomirski.Lnmi_from_kernel:
14849b6e6a83SAndy Lutomirski	/*
14850b22930eSAndy Lutomirski	 * Here's what our stack frame will look like:
14860b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14870b22930eSAndy Lutomirski	 * | original SS                                             |
14880b22930eSAndy Lutomirski	 * | original Return RSP                                     |
14890b22930eSAndy Lutomirski	 * | original RFLAGS                                         |
14900b22930eSAndy Lutomirski	 * | original CS                                             |
14910b22930eSAndy Lutomirski	 * | original RIP                                            |
14920b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14930b22930eSAndy Lutomirski	 * | temp storage for rdx                                    |
14940b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14950b22930eSAndy Lutomirski	 * | "NMI executing" variable                                |
14960b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14970b22930eSAndy Lutomirski	 * | iret SS          } Copied from "outermost" frame        |
14980b22930eSAndy Lutomirski	 * | iret Return RSP  } on each loop iteration; overwritten  |
14990b22930eSAndy Lutomirski	 * | iret RFLAGS      } by a nested NMI to force another     |
15000b22930eSAndy Lutomirski	 * | iret CS          } iteration if needed.                 |
15010b22930eSAndy Lutomirski	 * | iret RIP         }                                      |
15020b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
15030b22930eSAndy Lutomirski	 * | outermost SS          } initialized in first_nmi;       |
15040b22930eSAndy Lutomirski	 * | outermost Return RSP  } will not be changed before      |
15050b22930eSAndy Lutomirski	 * | outermost RFLAGS      } NMI processing is done.         |
15060b22930eSAndy Lutomirski	 * | outermost CS          } Copied to "iret" frame on each  |
15070b22930eSAndy Lutomirski	 * | outermost RIP         } iteration.                      |
15080b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
15090b22930eSAndy Lutomirski	 * | pt_regs                                                 |
15100b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
15110b22930eSAndy Lutomirski	 *
15120b22930eSAndy Lutomirski	 * The "original" frame is used by hardware.  Before re-enabling
15130b22930eSAndy Lutomirski	 * NMIs, we need to be done with it, and we need to leave enough
15140b22930eSAndy Lutomirski	 * space for the asm code here.
15150b22930eSAndy Lutomirski	 *
15160b22930eSAndy Lutomirski	 * We return by executing IRET while RSP points to the "iret" frame.
15170b22930eSAndy Lutomirski	 * That will either return for real or it will loop back into NMI
15180b22930eSAndy Lutomirski	 * processing.
15190b22930eSAndy Lutomirski	 *
15200b22930eSAndy Lutomirski	 * The "outermost" frame is copied to the "iret" frame on each
15210b22930eSAndy Lutomirski	 * iteration of the loop, so each iteration starts with the "iret"
15220b22930eSAndy Lutomirski	 * frame pointing to the final return target.
15230b22930eSAndy Lutomirski	 */
15240b22930eSAndy Lutomirski
15250b22930eSAndy Lutomirski	/*
15260b22930eSAndy Lutomirski	 * Determine whether we're a nested NMI.
15270b22930eSAndy Lutomirski	 *
1528a27507caSAndy Lutomirski	 * If we interrupted kernel code between repeat_nmi and
1529a27507caSAndy Lutomirski	 * end_repeat_nmi, then we are a nested NMI.  We must not
1530a27507caSAndy Lutomirski	 * modify the "iret" frame because it's being written by
1531a27507caSAndy Lutomirski	 * the outer NMI.  That's okay; the outer NMI handler is
1532a27507caSAndy Lutomirski	 * about to about to call do_nmi anyway, so we can just
1533a27507caSAndy Lutomirski	 * resume the outer NMI.
1534a27507caSAndy Lutomirski	 */
1535a27507caSAndy Lutomirski
1536a27507caSAndy Lutomirski	movq	$repeat_nmi, %rdx
1537a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1538a27507caSAndy Lutomirski	ja	1f
1539a27507caSAndy Lutomirski	movq	$end_repeat_nmi, %rdx
1540a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1541a27507caSAndy Lutomirski	ja	nested_nmi_out
1542a27507caSAndy Lutomirski1:
1543a27507caSAndy Lutomirski
1544a27507caSAndy Lutomirski	/*
1545a27507caSAndy Lutomirski	 * Now check "NMI executing".  If it's set, then we're nested.
15460b22930eSAndy Lutomirski	 * This will not detect if we interrupted an outer NMI just
15470b22930eSAndy Lutomirski	 * before IRET.
1548905a36a2SIngo Molnar	 */
1549905a36a2SIngo Molnar	cmpl	$1, -8(%rsp)
1550905a36a2SIngo Molnar	je	nested_nmi
1551905a36a2SIngo Molnar
1552905a36a2SIngo Molnar	/*
15530b22930eSAndy Lutomirski	 * Now test if the previous stack was an NMI stack.  This covers
15540b22930eSAndy Lutomirski	 * the case where we interrupt an outer NMI after it clears
1555810bc075SAndy Lutomirski	 * "NMI executing" but before IRET.  We need to be careful, though:
1556810bc075SAndy Lutomirski	 * there is one case in which RSP could point to the NMI stack
1557810bc075SAndy Lutomirski	 * despite there being no NMI active: naughty userspace controls
1558810bc075SAndy Lutomirski	 * RSP at the very beginning of the SYSCALL targets.  We can
1559810bc075SAndy Lutomirski	 * pull a fast one on naughty userspace, though: we program
1560810bc075SAndy Lutomirski	 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1561810bc075SAndy Lutomirski	 * if it controls the kernel's RSP.  We set DF before we clear
1562810bc075SAndy Lutomirski	 * "NMI executing".
1563905a36a2SIngo Molnar	 */
1564905a36a2SIngo Molnar	lea	6*8(%rsp), %rdx
1565905a36a2SIngo Molnar	/* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1566905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1567905a36a2SIngo Molnar	/* If the stack pointer is above the NMI stack, this is a normal NMI */
1568905a36a2SIngo Molnar	ja	first_nmi
15694d732138SIngo Molnar
1570905a36a2SIngo Molnar	subq	$EXCEPTION_STKSZ, %rdx
1571905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1572905a36a2SIngo Molnar	/* If it is below the NMI stack, it is a normal NMI */
1573905a36a2SIngo Molnar	jb	first_nmi
1574810bc075SAndy Lutomirski
1575810bc075SAndy Lutomirski	/* Ah, it is within the NMI stack. */
1576810bc075SAndy Lutomirski
1577810bc075SAndy Lutomirski	testb	$(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1578810bc075SAndy Lutomirski	jz	first_nmi	/* RSP was user controlled. */
1579810bc075SAndy Lutomirski
1580810bc075SAndy Lutomirski	/* This is a nested NMI. */
1581905a36a2SIngo Molnar
1582905a36a2SIngo Molnarnested_nmi:
1583905a36a2SIngo Molnar	/*
15840b22930eSAndy Lutomirski	 * Modify the "iret" frame to point to repeat_nmi, forcing another
15850b22930eSAndy Lutomirski	 * iteration of NMI handling.
1586905a36a2SIngo Molnar	 */
158723a781e9SAndy Lutomirski	subq	$8, %rsp
1588905a36a2SIngo Molnar	leaq	-10*8(%rsp), %rdx
1589905a36a2SIngo Molnar	pushq	$__KERNEL_DS
1590905a36a2SIngo Molnar	pushq	%rdx
1591905a36a2SIngo Molnar	pushfq
1592905a36a2SIngo Molnar	pushq	$__KERNEL_CS
1593905a36a2SIngo Molnar	pushq	$repeat_nmi
1594905a36a2SIngo Molnar
1595905a36a2SIngo Molnar	/* Put stack back */
1596905a36a2SIngo Molnar	addq	$(6*8), %rsp
1597905a36a2SIngo Molnar
1598905a36a2SIngo Molnarnested_nmi_out:
1599905a36a2SIngo Molnar	popq	%rdx
1600905a36a2SIngo Molnar
16010b22930eSAndy Lutomirski	/* We are returning to kernel mode, so this cannot result in a fault. */
1602929bacecSAndy Lutomirski	iretq
1603905a36a2SIngo Molnar
1604905a36a2SIngo Molnarfirst_nmi:
16050b22930eSAndy Lutomirski	/* Restore rdx. */
1606905a36a2SIngo Molnar	movq	(%rsp), %rdx
1607905a36a2SIngo Molnar
160836f1a77bSAndy Lutomirski	/* Make room for "NMI executing". */
160936f1a77bSAndy Lutomirski	pushq	$0
1610905a36a2SIngo Molnar
16110b22930eSAndy Lutomirski	/* Leave room for the "iret" frame */
1612905a36a2SIngo Molnar	subq	$(5*8), %rsp
1613905a36a2SIngo Molnar
16140b22930eSAndy Lutomirski	/* Copy the "original" frame to the "outermost" frame */
1615905a36a2SIngo Molnar	.rept 5
1616905a36a2SIngo Molnar	pushq	11*8(%rsp)
1617905a36a2SIngo Molnar	.endr
16188c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1619905a36a2SIngo Molnar
1620905a36a2SIngo Molnar	/* Everything up to here is safe from nested NMIs */
1621905a36a2SIngo Molnar
1622a97439aaSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
1623a97439aaSAndy Lutomirski	/*
1624a97439aaSAndy Lutomirski	 * For ease of testing, unmask NMIs right away.  Disabled by
1625a97439aaSAndy Lutomirski	 * default because IRET is very expensive.
1626a97439aaSAndy Lutomirski	 */
1627a97439aaSAndy Lutomirski	pushq	$0		/* SS */
1628a97439aaSAndy Lutomirski	pushq	%rsp		/* RSP (minus 8 because of the previous push) */
1629a97439aaSAndy Lutomirski	addq	$8, (%rsp)	/* Fix up RSP */
1630a97439aaSAndy Lutomirski	pushfq			/* RFLAGS */
1631a97439aaSAndy Lutomirski	pushq	$__KERNEL_CS	/* CS */
1632a97439aaSAndy Lutomirski	pushq	$1f		/* RIP */
1633929bacecSAndy Lutomirski	iretq			/* continues at repeat_nmi below */
16348c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1635a97439aaSAndy Lutomirski1:
1636a97439aaSAndy Lutomirski#endif
1637a97439aaSAndy Lutomirski
16380b22930eSAndy Lutomirskirepeat_nmi:
1639905a36a2SIngo Molnar	/*
1640905a36a2SIngo Molnar	 * If there was a nested NMI, the first NMI's iret will return
1641905a36a2SIngo Molnar	 * here. But NMIs are still enabled and we can take another
1642905a36a2SIngo Molnar	 * nested NMI. The nested NMI checks the interrupted RIP to see
1643905a36a2SIngo Molnar	 * if it is between repeat_nmi and end_repeat_nmi, and if so
1644905a36a2SIngo Molnar	 * it will just return, as we are about to repeat an NMI anyway.
1645905a36a2SIngo Molnar	 * This makes it safe to copy to the stack frame that a nested
1646905a36a2SIngo Molnar	 * NMI will update.
16470b22930eSAndy Lutomirski	 *
16480b22930eSAndy Lutomirski	 * RSP is pointing to "outermost RIP".  gsbase is unknown, but, if
16490b22930eSAndy Lutomirski	 * we're repeating an NMI, gsbase has the same value that it had on
16500b22930eSAndy Lutomirski	 * the first iteration.  paranoid_entry will load the kernel
165136f1a77bSAndy Lutomirski	 * gsbase if needed before we call do_nmi.  "NMI executing"
165236f1a77bSAndy Lutomirski	 * is zero.
1653905a36a2SIngo Molnar	 */
165436f1a77bSAndy Lutomirski	movq	$1, 10*8(%rsp)		/* Set "NMI executing". */
1655905a36a2SIngo Molnar
16560b22930eSAndy Lutomirski	/*
16570b22930eSAndy Lutomirski	 * Copy the "outermost" frame to the "iret" frame.  NMIs that nest
16580b22930eSAndy Lutomirski	 * here must not modify the "iret" frame while we're writing to
16590b22930eSAndy Lutomirski	 * it or it will end up containing garbage.
16600b22930eSAndy Lutomirski	 */
1661905a36a2SIngo Molnar	addq	$(10*8), %rsp
1662905a36a2SIngo Molnar	.rept 5
1663905a36a2SIngo Molnar	pushq	-6*8(%rsp)
1664905a36a2SIngo Molnar	.endr
1665905a36a2SIngo Molnar	subq	$(5*8), %rsp
1666905a36a2SIngo Molnarend_repeat_nmi:
1667905a36a2SIngo Molnar
1668905a36a2SIngo Molnar	/*
16690b22930eSAndy Lutomirski	 * Everything below this point can be preempted by a nested NMI.
16700b22930eSAndy Lutomirski	 * If this happens, then the inner NMI will change the "iret"
16710b22930eSAndy Lutomirski	 * frame to point back to repeat_nmi.
1672905a36a2SIngo Molnar	 */
1673905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
1674905a36a2SIngo Molnar
1675905a36a2SIngo Molnar	/*
1676905a36a2SIngo Molnar	 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1677905a36a2SIngo Molnar	 * as we should not be calling schedule in NMI context.
1678905a36a2SIngo Molnar	 * Even with normal interrupts enabled. An NMI should not be
1679905a36a2SIngo Molnar	 * setting NEED_RESCHED or anything that normal interrupts and
1680905a36a2SIngo Molnar	 * exceptions might do.
1681905a36a2SIngo Molnar	 */
1682905a36a2SIngo Molnar	call	paranoid_entry
16838c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
1684905a36a2SIngo Molnar
1685905a36a2SIngo Molnar	/* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1686905a36a2SIngo Molnar	movq	%rsp, %rdi
1687905a36a2SIngo Molnar	movq	$-1, %rsi
1688905a36a2SIngo Molnar	call	do_nmi
1689905a36a2SIngo Molnar
169016561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
169121e94459SPeter Zijlstra	RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
16928a09317bSDave Hansen
1693905a36a2SIngo Molnar	testl	%ebx, %ebx			/* swapgs needed? */
1694905a36a2SIngo Molnar	jnz	nmi_restore
1695905a36a2SIngo Molnarnmi_swapgs:
1696905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
1697905a36a2SIngo Molnarnmi_restore:
1698502af0d7SDominik Brodowski	POP_REGS
16990b22930eSAndy Lutomirski
1700471ee483SAndy Lutomirski	/*
1701471ee483SAndy Lutomirski	 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1702471ee483SAndy Lutomirski	 * at the "iret" frame.
1703471ee483SAndy Lutomirski	 */
1704471ee483SAndy Lutomirski	addq	$6*8, %rsp
1705905a36a2SIngo Molnar
1706810bc075SAndy Lutomirski	/*
1707810bc075SAndy Lutomirski	 * Clear "NMI executing".  Set DF first so that we can easily
1708810bc075SAndy Lutomirski	 * distinguish the remaining code between here and IRET from
1709929bacecSAndy Lutomirski	 * the SYSCALL entry and exit paths.
1710929bacecSAndy Lutomirski	 *
1711929bacecSAndy Lutomirski	 * We arguably should just inspect RIP instead, but I (Andy) wrote
1712929bacecSAndy Lutomirski	 * this code when I had the misapprehension that Xen PV supported
1713929bacecSAndy Lutomirski	 * NMIs, and Xen PV would break that approach.
1714810bc075SAndy Lutomirski	 */
1715810bc075SAndy Lutomirski	std
1716810bc075SAndy Lutomirski	movq	$0, 5*8(%rsp)		/* clear "NMI executing" */
17170b22930eSAndy Lutomirski
17180b22930eSAndy Lutomirski	/*
1719929bacecSAndy Lutomirski	 * iretq reads the "iret" frame and exits the NMI stack in a
1720929bacecSAndy Lutomirski	 * single instruction.  We are returning to kernel mode, so this
1721929bacecSAndy Lutomirski	 * cannot result in a fault.  Similarly, we don't need to worry
1722929bacecSAndy Lutomirski	 * about espfix64 on the way back to kernel mode.
17230b22930eSAndy Lutomirski	 */
1724929bacecSAndy Lutomirski	iretq
1725bc7b11c0SJiri SlabySYM_CODE_END(nmi)
1726905a36a2SIngo Molnar
1727dffb3f9dSAndy Lutomirski#ifndef CONFIG_IA32_EMULATION
1728dffb3f9dSAndy Lutomirski/*
1729dffb3f9dSAndy Lutomirski * This handles SYSCALL from 32-bit code.  There is no way to program
1730dffb3f9dSAndy Lutomirski * MSRs to fully disable 32-bit SYSCALL.
1731dffb3f9dSAndy Lutomirski */
1732bc7b11c0SJiri SlabySYM_CODE_START(ignore_sysret)
17338c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
1734905a36a2SIngo Molnar	mov	$-ENOSYS, %eax
1735b2b1d94cSJan Beulich	sysretl
1736bc7b11c0SJiri SlabySYM_CODE_END(ignore_sysret)
1737dffb3f9dSAndy Lutomirski#endif
17382deb4be2SAndy Lutomirski
1739*b9f6976bSThomas Gleixner.pushsection .text, "ax"
1740bc7b11c0SJiri SlabySYM_CODE_START(rewind_stack_do_exit)
17418c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
17422deb4be2SAndy Lutomirski	/* Prevent any naive code from trying to unwind to our caller. */
17432deb4be2SAndy Lutomirski	xorl	%ebp, %ebp
17442deb4be2SAndy Lutomirski
17452deb4be2SAndy Lutomirski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rax
17468c1f7558SJosh Poimboeuf	leaq	-PTREGS_SIZE(%rax), %rsp
1747f977df7bSJann Horn	UNWIND_HINT_REGS
17482deb4be2SAndy Lutomirski
17492deb4be2SAndy Lutomirski	call	do_exit
1750bc7b11c0SJiri SlabySYM_CODE_END(rewind_stack_do_exit)
1751*b9f6976bSThomas Gleixner.popsection
1752