xref: /openbmc/linux/arch/x86/entry/entry_64.S (revision b2b1d94cdfd4e906d3936dab2850096a4a0c2017)
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
17729ea1b25SAndy Lutomirski	TRACE_IRQS_IRETQ		/* we're about to change IF */
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) */
2528c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
253502af0d7SDominik Brodowski	POP_REGS pop_rdi=0 skip_r11rcx=1
2543e3b9293SAndy Lutomirski
2553e3b9293SAndy Lutomirski	/*
2563e3b9293SAndy Lutomirski	 * Now all regs are restored except RSP and RDI.
2573e3b9293SAndy Lutomirski	 * Save old stack pointer and switch to trampoline stack.
2583e3b9293SAndy Lutomirski	 */
2593e3b9293SAndy Lutomirski	movq	%rsp, %rdi
260c482feefSAndy Lutomirski	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
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 */
282bc7b11c0SJiri SlabySYM_CODE_START(__switch_to_asm)
2838c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
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
324bc7b11c0SJiri SlabySYM_CODE_END(__switch_to_asm)
3250100301bSBrian Gerst
3260100301bSBrian Gerst/*
327905a36a2SIngo Molnar * A newly forked process directly context switches into this address.
328905a36a2SIngo Molnar *
3290100301bSBrian Gerst * rax: prev task we switched from
330616d2483SBrian Gerst * rbx: kernel thread func (NULL for user thread)
331616d2483SBrian Gerst * r12: kernel thread arg
332905a36a2SIngo Molnar */
333bc7b11c0SJiri SlabySYM_CODE_START(ret_from_fork)
3348c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
3350100301bSBrian Gerst	movq	%rax, %rdi
3364d732138SIngo Molnar	call	schedule_tail			/* rdi: 'prev' task parameter */
337905a36a2SIngo Molnar
338616d2483SBrian Gerst	testq	%rbx, %rbx			/* from kernel_thread? */
339616d2483SBrian Gerst	jnz	1f				/* kernel threads are uncommon */
340905a36a2SIngo Molnar
341616d2483SBrian Gerst2:
3428c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
343ebd57499SJosh Poimboeuf	movq	%rsp, %rdi
34424d978b7SAndy Lutomirski	call	syscall_return_slowpath	/* returns with IRQs disabled */
34524d978b7SAndy Lutomirski	TRACE_IRQS_ON			/* user mode is traced as IRQS on */
3468a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
347616d2483SBrian Gerst
348616d2483SBrian Gerst1:
349616d2483SBrian Gerst	/* kernel thread */
350d31a5802SJosh Poimboeuf	UNWIND_HINT_EMPTY
351616d2483SBrian Gerst	movq	%r12, %rdi
3522641f08bSDavid Woodhouse	CALL_NOSPEC %rbx
353616d2483SBrian Gerst	/*
354616d2483SBrian Gerst	 * A kernel thread is allowed to return here after successfully
355616d2483SBrian Gerst	 * calling do_execve().  Exit to userspace to complete the execve()
356616d2483SBrian Gerst	 * syscall.
357616d2483SBrian Gerst	 */
358616d2483SBrian Gerst	movq	$0, RAX(%rsp)
359616d2483SBrian Gerst	jmp	2b
360bc7b11c0SJiri SlabySYM_CODE_END(ret_from_fork)
361905a36a2SIngo Molnar
362905a36a2SIngo Molnar/*
363905a36a2SIngo Molnar * Build the entry stubs with some assembler magic.
364905a36a2SIngo Molnar * We pack 1 stub into every 8-byte block.
365905a36a2SIngo Molnar */
366905a36a2SIngo Molnar	.align 8
367bc7b11c0SJiri SlabySYM_CODE_START(irq_entries_start)
368905a36a2SIngo Molnar    vector=FIRST_EXTERNAL_VECTOR
369905a36a2SIngo Molnar    .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
3708c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
371905a36a2SIngo Molnar	pushq	$(~vector+0x80)			/* Note: always in signed byte range */
372905a36a2SIngo Molnar	jmp	common_interrupt
373905a36a2SIngo Molnar	.align	8
3748c1f7558SJosh Poimboeuf	vector=vector+1
375905a36a2SIngo Molnar    .endr
376bc7b11c0SJiri SlabySYM_CODE_END(irq_entries_start)
377905a36a2SIngo Molnar
378f8a8fe61SThomas Gleixner	.align 8
379bc7b11c0SJiri SlabySYM_CODE_START(spurious_entries_start)
380f8a8fe61SThomas Gleixner    vector=FIRST_SYSTEM_VECTOR
381f8a8fe61SThomas Gleixner    .rept (NR_VECTORS - FIRST_SYSTEM_VECTOR)
382f8a8fe61SThomas Gleixner	UNWIND_HINT_IRET_REGS
383f8a8fe61SThomas Gleixner	pushq	$(~vector+0x80)			/* Note: always in signed byte range */
384f8a8fe61SThomas Gleixner	jmp	common_spurious
385f8a8fe61SThomas Gleixner	.align	8
386f8a8fe61SThomas Gleixner	vector=vector+1
387f8a8fe61SThomas Gleixner    .endr
388bc7b11c0SJiri SlabySYM_CODE_END(spurious_entries_start)
389f8a8fe61SThomas Gleixner
3901d3e53e8SAndy Lutomirski.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
3911d3e53e8SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
392e17f8234SBoris Ostrovsky	pushq %rax
393e17f8234SBoris Ostrovsky	SAVE_FLAGS(CLBR_RAX)
394e17f8234SBoris Ostrovsky	testl $X86_EFLAGS_IF, %eax
3951d3e53e8SAndy Lutomirski	jz .Lokay_\@
3961d3e53e8SAndy Lutomirski	ud2
3971d3e53e8SAndy Lutomirski.Lokay_\@:
398e17f8234SBoris Ostrovsky	popq %rax
3991d3e53e8SAndy Lutomirski#endif
4001d3e53e8SAndy Lutomirski.endm
4011d3e53e8SAndy Lutomirski
4021d3e53e8SAndy Lutomirski/*
4031d3e53e8SAndy Lutomirski * Enters the IRQ stack if we're not already using it.  NMI-safe.  Clobbers
4041d3e53e8SAndy Lutomirski * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
4051d3e53e8SAndy Lutomirski * Requires kernel GSBASE.
4061d3e53e8SAndy Lutomirski *
4071d3e53e8SAndy Lutomirski * The invariant is that, if irq_count != -1, then the IRQ stack is in use.
4081d3e53e8SAndy Lutomirski */
4092ba64741SDominik Brodowski.macro ENTER_IRQ_STACK regs=1 old_rsp save_ret=0
4101d3e53e8SAndy Lutomirski	DEBUG_ENTRY_ASSERT_IRQS_OFF
4112ba64741SDominik Brodowski
4122ba64741SDominik Brodowski	.if \save_ret
4132ba64741SDominik Brodowski	/*
4142ba64741SDominik Brodowski	 * If save_ret is set, the original stack contains one additional
4152ba64741SDominik Brodowski	 * entry -- the return address. Therefore, move the address one
4162ba64741SDominik Brodowski	 * entry below %rsp to \old_rsp.
4172ba64741SDominik Brodowski	 */
4182ba64741SDominik Brodowski	leaq	8(%rsp), \old_rsp
4192ba64741SDominik Brodowski	.else
4201d3e53e8SAndy Lutomirski	movq	%rsp, \old_rsp
4212ba64741SDominik Brodowski	.endif
4228c1f7558SJosh Poimboeuf
4238c1f7558SJosh Poimboeuf	.if \regs
4248c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS base=\old_rsp
4258c1f7558SJosh Poimboeuf	.endif
4268c1f7558SJosh Poimboeuf
4271d3e53e8SAndy Lutomirski	incl	PER_CPU_VAR(irq_count)
42829955909SAndy Lutomirski	jnz	.Lirq_stack_push_old_rsp_\@
4291d3e53e8SAndy Lutomirski
4301d3e53e8SAndy Lutomirski	/*
4311d3e53e8SAndy Lutomirski	 * Right now, if we just incremented irq_count to zero, we've
4321d3e53e8SAndy Lutomirski	 * claimed the IRQ stack but we haven't switched to it yet.
4331d3e53e8SAndy Lutomirski	 *
4341d3e53e8SAndy Lutomirski	 * If anything is added that can interrupt us here without using IST,
4351d3e53e8SAndy Lutomirski	 * it must be *extremely* careful to limit its stack usage.  This
4361d3e53e8SAndy Lutomirski	 * could include kprobes and a hypothetical future IST-less #DB
4371d3e53e8SAndy Lutomirski	 * handler.
43829955909SAndy Lutomirski	 *
43929955909SAndy Lutomirski	 * The OOPS unwinder relies on the word at the top of the IRQ
44029955909SAndy Lutomirski	 * stack linking back to the previous RSP for the entire time we're
44129955909SAndy Lutomirski	 * on the IRQ stack.  For this to work reliably, we need to write
44229955909SAndy Lutomirski	 * it before we actually move ourselves to the IRQ stack.
4431d3e53e8SAndy Lutomirski	 */
4441d3e53e8SAndy Lutomirski
445e6401c13SAndy Lutomirski	movq	\old_rsp, PER_CPU_VAR(irq_stack_backing_store + IRQ_STACK_SIZE - 8)
446758a2e31SThomas Gleixner	movq	PER_CPU_VAR(hardirq_stack_ptr), %rsp
44729955909SAndy Lutomirski
44829955909SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
44929955909SAndy Lutomirski	/*
45029955909SAndy Lutomirski	 * If the first movq above becomes wrong due to IRQ stack layout
45129955909SAndy Lutomirski	 * changes, the only way we'll notice is if we try to unwind right
45229955909SAndy Lutomirski	 * here.  Assert that we set up the stack right to catch this type
45329955909SAndy Lutomirski	 * of bug quickly.
45429955909SAndy Lutomirski	 */
45529955909SAndy Lutomirski	cmpq	-8(%rsp), \old_rsp
45629955909SAndy Lutomirski	je	.Lirq_stack_okay\@
45729955909SAndy Lutomirski	ud2
45829955909SAndy Lutomirski	.Lirq_stack_okay\@:
45929955909SAndy Lutomirski#endif
46029955909SAndy Lutomirski
46129955909SAndy Lutomirski.Lirq_stack_push_old_rsp_\@:
4621d3e53e8SAndy Lutomirski	pushq	\old_rsp
4638c1f7558SJosh Poimboeuf
4648c1f7558SJosh Poimboeuf	.if \regs
4658c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS indirect=1
4668c1f7558SJosh Poimboeuf	.endif
4672ba64741SDominik Brodowski
4682ba64741SDominik Brodowski	.if \save_ret
4692ba64741SDominik Brodowski	/*
4702ba64741SDominik Brodowski	 * Push the return address to the stack. This return address can
4712ba64741SDominik Brodowski	 * be found at the "real" original RSP, which was offset by 8 at
4722ba64741SDominik Brodowski	 * the beginning of this macro.
4732ba64741SDominik Brodowski	 */
4742ba64741SDominik Brodowski	pushq	-8(\old_rsp)
4752ba64741SDominik Brodowski	.endif
4761d3e53e8SAndy Lutomirski.endm
4771d3e53e8SAndy Lutomirski
4781d3e53e8SAndy Lutomirski/*
4791d3e53e8SAndy Lutomirski * Undoes ENTER_IRQ_STACK.
4801d3e53e8SAndy Lutomirski */
4818c1f7558SJosh Poimboeuf.macro LEAVE_IRQ_STACK regs=1
4821d3e53e8SAndy Lutomirski	DEBUG_ENTRY_ASSERT_IRQS_OFF
4831d3e53e8SAndy Lutomirski	/* We need to be off the IRQ stack before decrementing irq_count. */
4841d3e53e8SAndy Lutomirski	popq	%rsp
4851d3e53e8SAndy Lutomirski
4868c1f7558SJosh Poimboeuf	.if \regs
4878c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
4888c1f7558SJosh Poimboeuf	.endif
4898c1f7558SJosh Poimboeuf
4901d3e53e8SAndy Lutomirski	/*
4911d3e53e8SAndy Lutomirski	 * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming
4921d3e53e8SAndy Lutomirski	 * the irq stack but we're not on it.
4931d3e53e8SAndy Lutomirski	 */
4941d3e53e8SAndy Lutomirski
4951d3e53e8SAndy Lutomirski	decl	PER_CPU_VAR(irq_count)
4961d3e53e8SAndy Lutomirski.endm
4971d3e53e8SAndy Lutomirski
498905a36a2SIngo Molnar/*
499f3d415eaSDominik Brodowski * Interrupt entry helper function.
500905a36a2SIngo Molnar *
501f3d415eaSDominik Brodowski * Entry runs with interrupts off. Stack layout at entry:
502f3d415eaSDominik Brodowski * +----------------------------------------------------+
503f3d415eaSDominik Brodowski * | regs->ss						|
504f3d415eaSDominik Brodowski * | regs->rsp						|
505f3d415eaSDominik Brodowski * | regs->eflags					|
506f3d415eaSDominik Brodowski * | regs->cs						|
507f3d415eaSDominik Brodowski * | regs->ip						|
508f3d415eaSDominik Brodowski * +----------------------------------------------------+
509f3d415eaSDominik Brodowski * | regs->orig_ax = ~(interrupt number)		|
510f3d415eaSDominik Brodowski * +----------------------------------------------------+
511f3d415eaSDominik Brodowski * | return address					|
512f3d415eaSDominik Brodowski * +----------------------------------------------------+
513905a36a2SIngo Molnar */
514bc7b11c0SJiri SlabySYM_CODE_START(interrupt_entry)
515f3d415eaSDominik Brodowski	UNWIND_HINT_FUNC
516f3d415eaSDominik Brodowski	ASM_CLAC
517905a36a2SIngo Molnar	cld
5187f2590a1SAndy Lutomirski
519f3d415eaSDominik Brodowski	testb	$3, CS-ORIG_RAX+8(%rsp)
5207f2590a1SAndy Lutomirski	jz	1f
5217f2590a1SAndy Lutomirski	SWAPGS
52218ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
523f3d415eaSDominik Brodowski	/*
524f3d415eaSDominik Brodowski	 * Switch to the thread stack. The IRET frame and orig_ax are
525f3d415eaSDominik Brodowski	 * on the stack, as well as the return address. RDI..R12 are
526f3d415eaSDominik Brodowski	 * not (yet) on the stack and space has not (yet) been
527f3d415eaSDominik Brodowski	 * allocated for them.
528f3d415eaSDominik Brodowski	 */
52990a6acc4SDominik Brodowski	pushq	%rdi
530f3d415eaSDominik Brodowski
53190a6acc4SDominik Brodowski	/* Need to switch before accessing the thread stack. */
53290a6acc4SDominik Brodowski	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi
53390a6acc4SDominik Brodowski	movq	%rsp, %rdi
53490a6acc4SDominik Brodowski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
535f3d415eaSDominik Brodowski
536f3d415eaSDominik Brodowski	 /*
537f3d415eaSDominik Brodowski	  * We have RDI, return address, and orig_ax on the stack on
538f3d415eaSDominik Brodowski	  * top of the IRET frame. That means offset=24
539f3d415eaSDominik Brodowski	  */
540f3d415eaSDominik Brodowski	UNWIND_HINT_IRET_REGS base=%rdi offset=24
54190a6acc4SDominik Brodowski
54290a6acc4SDominik Brodowski	pushq	7*8(%rdi)		/* regs->ss */
54390a6acc4SDominik Brodowski	pushq	6*8(%rdi)		/* regs->rsp */
54490a6acc4SDominik Brodowski	pushq	5*8(%rdi)		/* regs->eflags */
54590a6acc4SDominik Brodowski	pushq	4*8(%rdi)		/* regs->cs */
54690a6acc4SDominik Brodowski	pushq	3*8(%rdi)		/* regs->ip */
54790a6acc4SDominik Brodowski	pushq	2*8(%rdi)		/* regs->orig_ax */
54890a6acc4SDominik Brodowski	pushq	8(%rdi)			/* return address */
54990a6acc4SDominik Brodowski	UNWIND_HINT_FUNC
55090a6acc4SDominik Brodowski
55190a6acc4SDominik Brodowski	movq	(%rdi), %rdi
55264dbc122SJosh Poimboeuf	jmp	2f
5537f2590a1SAndy Lutomirski1:
55418ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
55518ec54fdSJosh Poimboeuf2:
5560e34d226SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
5570e34d226SDominik Brodowski	ENCODE_FRAME_POINTER 8
558905a36a2SIngo Molnar
5592ba64741SDominik Brodowski	testb	$3, CS+8(%rsp)
560905a36a2SIngo Molnar	jz	1f
56102bc7768SAndy Lutomirski
56202bc7768SAndy Lutomirski	/*
5637f2590a1SAndy Lutomirski	 * IRQ from user mode.
5647f2590a1SAndy Lutomirski	 *
565f1075053SAndy Lutomirski	 * We need to tell lockdep that IRQs are off.  We can't do this until
566f1075053SAndy Lutomirski	 * we fix gsbase, and we should do it before enter_from_user_mode
567f3d415eaSDominik Brodowski	 * (which can take locks).  Since TRACE_IRQS_OFF is idempotent,
568f1075053SAndy Lutomirski	 * the simplest way to handle it is to just call it twice if
569f1075053SAndy Lutomirski	 * we enter from user mode.  There's no reason to optimize this since
570f1075053SAndy Lutomirski	 * TRACE_IRQS_OFF is a no-op if lockdep is off.
571f1075053SAndy Lutomirski	 */
572f1075053SAndy Lutomirski	TRACE_IRQS_OFF
573f1075053SAndy Lutomirski
574478dc89cSAndy Lutomirski	CALL_enter_from_user_mode
57502bc7768SAndy Lutomirski
576905a36a2SIngo Molnar1:
5772ba64741SDominik Brodowski	ENTER_IRQ_STACK old_rsp=%rdi save_ret=1
578905a36a2SIngo Molnar	/* We entered an interrupt context - irqs are off: */
579905a36a2SIngo Molnar	TRACE_IRQS_OFF
580905a36a2SIngo Molnar
5812ba64741SDominik Brodowski	ret
582bc7b11c0SJiri SlabySYM_CODE_END(interrupt_entry)
583a50480cbSAndrea Righi_ASM_NOKPROBE(interrupt_entry)
5842ba64741SDominik Brodowski
585f3d415eaSDominik Brodowski
586f3d415eaSDominik Brodowski/* Interrupt entry/exit. */
587905a36a2SIngo Molnar
588905a36a2SIngo Molnar/*
589905a36a2SIngo Molnar * The interrupt stubs push (~vector+0x80) onto the stack and
590f8a8fe61SThomas Gleixner * then jump to common_spurious/interrupt.
591905a36a2SIngo Molnar */
592cc66936eSJiri SlabySYM_CODE_START_LOCAL(common_spurious)
593f8a8fe61SThomas Gleixner	addq	$-0x80, (%rsp)			/* Adjust vector to [-256, -1] range */
594f8a8fe61SThomas Gleixner	call	interrupt_entry
595f8a8fe61SThomas Gleixner	UNWIND_HINT_REGS indirect=1
596f8a8fe61SThomas Gleixner	call	smp_spurious_interrupt		/* rdi points to pt_regs */
597f8a8fe61SThomas Gleixner	jmp	ret_from_intr
598cc66936eSJiri SlabySYM_CODE_END(common_spurious)
599f8a8fe61SThomas Gleixner_ASM_NOKPROBE(common_spurious)
600f8a8fe61SThomas Gleixner
601f8a8fe61SThomas Gleixner/* common_interrupt is a hotpath. Align it */
602905a36a2SIngo Molnar	.p2align CONFIG_X86_L1_CACHE_SHIFT
603cc66936eSJiri SlabySYM_CODE_START_LOCAL(common_interrupt)
604905a36a2SIngo Molnar	addq	$-0x80, (%rsp)			/* Adjust vector to [-256, -1] range */
6053aa99fc3SDominik Brodowski	call	interrupt_entry
6063aa99fc3SDominik Brodowski	UNWIND_HINT_REGS indirect=1
6073aa99fc3SDominik Brodowski	call	do_IRQ	/* rdi points to pt_regs */
608905a36a2SIngo Molnar	/* 0(%rsp): old RSP */
609905a36a2SIngo Molnarret_from_intr:
6102140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
611905a36a2SIngo Molnar	TRACE_IRQS_OFF
612905a36a2SIngo Molnar
6131d3e53e8SAndy Lutomirski	LEAVE_IRQ_STACK
614905a36a2SIngo Molnar
615905a36a2SIngo Molnar	testb	$3, CS(%rsp)
616905a36a2SIngo Molnar	jz	retint_kernel
61702bc7768SAndy Lutomirski
618905a36a2SIngo Molnar	/* Interrupt came from user space */
61930a2441cSJiri Slaby.Lretint_user:
62002bc7768SAndy Lutomirski	mov	%rsp,%rdi
62102bc7768SAndy Lutomirski	call	prepare_exit_to_usermode
622905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
62326c4ef9cSAndy Lutomirski
62426ba4e57SJiri SlabySYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
62526c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
62626c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates user mode. */
6271e4c4f61SBorislav Petkov	testb	$3, CS(%rsp)
62826c4ef9cSAndy Lutomirski	jnz	1f
62926c4ef9cSAndy Lutomirski	ud2
63026c4ef9cSAndy Lutomirski1:
63126c4ef9cSAndy Lutomirski#endif
632502af0d7SDominik Brodowski	POP_REGS pop_rdi=0
6333e3b9293SAndy Lutomirski
6343e3b9293SAndy Lutomirski	/*
6353e3b9293SAndy Lutomirski	 * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS.
6363e3b9293SAndy Lutomirski	 * Save old stack pointer and switch to trampoline stack.
6373e3b9293SAndy Lutomirski	 */
6383e3b9293SAndy Lutomirski	movq	%rsp, %rdi
639c482feefSAndy Lutomirski	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
6403e3b9293SAndy Lutomirski
6413e3b9293SAndy Lutomirski	/* Copy the IRET frame to the trampoline stack. */
6423e3b9293SAndy Lutomirski	pushq	6*8(%rdi)	/* SS */
6433e3b9293SAndy Lutomirski	pushq	5*8(%rdi)	/* RSP */
6443e3b9293SAndy Lutomirski	pushq	4*8(%rdi)	/* EFLAGS */
6453e3b9293SAndy Lutomirski	pushq	3*8(%rdi)	/* CS */
6463e3b9293SAndy Lutomirski	pushq	2*8(%rdi)	/* RIP */
6473e3b9293SAndy Lutomirski
6483e3b9293SAndy Lutomirski	/* Push user RDI on the trampoline stack. */
6493e3b9293SAndy Lutomirski	pushq	(%rdi)
6503e3b9293SAndy Lutomirski
6513e3b9293SAndy Lutomirski	/*
6523e3b9293SAndy Lutomirski	 * We are on the trampoline stack.  All regs except RDI are live.
6533e3b9293SAndy Lutomirski	 * We can do future final exit work right here.
6543e3b9293SAndy Lutomirski	 */
655afaef01cSAlexander Popov	STACKLEAK_ERASE_NOCLOBBER
6563e3b9293SAndy Lutomirski
6576fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
6588a09317bSDave Hansen
6593e3b9293SAndy Lutomirski	/* Restore RDI. */
6603e3b9293SAndy Lutomirski	popq	%rdi
6613e3b9293SAndy Lutomirski	SWAPGS
66226c4ef9cSAndy Lutomirski	INTERRUPT_RETURN
66326c4ef9cSAndy Lutomirski
664905a36a2SIngo Molnar
665905a36a2SIngo Molnar/* Returning to kernel space */
666905a36a2SIngo Molnarretint_kernel:
66748593975SThomas Gleixner#ifdef CONFIG_PREEMPTION
668905a36a2SIngo Molnar	/* Interrupts are off */
669905a36a2SIngo Molnar	/* Check if we need preemption */
6706709812fSJan Beulich	btl	$9, EFLAGS(%rsp)		/* were interrupts off? */
671905a36a2SIngo Molnar	jnc	1f
672b5b447b6SValentin Schneider	cmpl	$0, PER_CPU_VAR(__preempt_count)
673905a36a2SIngo Molnar	jnz	1f
674905a36a2SIngo Molnar	call	preempt_schedule_irq
675905a36a2SIngo Molnar1:
676905a36a2SIngo Molnar#endif
677905a36a2SIngo Molnar	/*
678905a36a2SIngo Molnar	 * The iretq could re-enable interrupts:
679905a36a2SIngo Molnar	 */
680905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
681905a36a2SIngo Molnar
68226ba4e57SJiri SlabySYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)
68326c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
68426c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates kernel mode. */
6851e4c4f61SBorislav Petkov	testb	$3, CS(%rsp)
68626c4ef9cSAndy Lutomirski	jz	1f
68726c4ef9cSAndy Lutomirski	ud2
68826c4ef9cSAndy Lutomirski1:
68926c4ef9cSAndy Lutomirski#endif
690502af0d7SDominik Brodowski	POP_REGS
691e872045bSAndy Lutomirski	addq	$8, %rsp	/* skip regs->orig_ax */
69210bcc80eSMathieu Desnoyers	/*
69310bcc80eSMathieu Desnoyers	 * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
69410bcc80eSMathieu Desnoyers	 * when returning from IPI handler.
69510bcc80eSMathieu Desnoyers	 */
696905a36a2SIngo Molnar	INTERRUPT_RETURN
697905a36a2SIngo Molnar
698cc66936eSJiri SlabySYM_INNER_LABEL_ALIGN(native_iret, SYM_L_GLOBAL)
6998c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
700905a36a2SIngo Molnar	/*
701905a36a2SIngo Molnar	 * Are we returning to a stack segment from the LDT?  Note: in
702905a36a2SIngo Molnar	 * 64-bit mode SS:RSP on the exception stack is always valid.
703905a36a2SIngo Molnar	 */
704905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
705905a36a2SIngo Molnar	testb	$4, (SS-RIP)(%rsp)
706905a36a2SIngo Molnar	jnz	native_irq_return_ldt
707905a36a2SIngo Molnar#endif
708905a36a2SIngo Molnar
709cc66936eSJiri SlabySYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL)
710905a36a2SIngo Molnar	/*
711905a36a2SIngo Molnar	 * This may fault.  Non-paranoid faults on return to userspace are
712905a36a2SIngo Molnar	 * handled by fixup_bad_iret.  These include #SS, #GP, and #NP.
713905a36a2SIngo Molnar	 * Double-faults due to espfix64 are handled in do_double_fault.
714905a36a2SIngo Molnar	 * Other faults here are fatal.
715905a36a2SIngo Molnar	 */
716905a36a2SIngo Molnar	iretq
717905a36a2SIngo Molnar
718905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
719905a36a2SIngo Molnarnative_irq_return_ldt:
72085063facSAndy Lutomirski	/*
72185063facSAndy Lutomirski	 * We are running with user GSBASE.  All GPRs contain their user
72285063facSAndy Lutomirski	 * values.  We have a percpu ESPFIX stack that is eight slots
72385063facSAndy Lutomirski	 * long (see ESPFIX_STACK_SIZE).  espfix_waddr points to the bottom
72485063facSAndy Lutomirski	 * of the ESPFIX stack.
72585063facSAndy Lutomirski	 *
72685063facSAndy Lutomirski	 * We clobber RAX and RDI in this code.  We stash RDI on the
72785063facSAndy Lutomirski	 * normal stack and RAX on the ESPFIX stack.
72885063facSAndy Lutomirski	 *
72985063facSAndy Lutomirski	 * The ESPFIX stack layout we set up looks like this:
73085063facSAndy Lutomirski	 *
73185063facSAndy Lutomirski	 * --- top of ESPFIX stack ---
73285063facSAndy Lutomirski	 * SS
73385063facSAndy Lutomirski	 * RSP
73485063facSAndy Lutomirski	 * RFLAGS
73585063facSAndy Lutomirski	 * CS
73685063facSAndy Lutomirski	 * RIP  <-- RSP points here when we're done
73785063facSAndy Lutomirski	 * RAX  <-- espfix_waddr points here
73885063facSAndy Lutomirski	 * --- bottom of ESPFIX stack ---
73985063facSAndy Lutomirski	 */
74085063facSAndy Lutomirski
74185063facSAndy Lutomirski	pushq	%rdi				/* Stash user RDI */
7428a09317bSDave Hansen	SWAPGS					/* to kernel GS */
7438a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi	/* to kernel CR3 */
7448a09317bSDave Hansen
745905a36a2SIngo Molnar	movq	PER_CPU_VAR(espfix_waddr), %rdi
74685063facSAndy Lutomirski	movq	%rax, (0*8)(%rdi)		/* user RAX */
74785063facSAndy Lutomirski	movq	(1*8)(%rsp), %rax		/* user RIP */
748905a36a2SIngo Molnar	movq	%rax, (1*8)(%rdi)
74985063facSAndy Lutomirski	movq	(2*8)(%rsp), %rax		/* user CS */
750905a36a2SIngo Molnar	movq	%rax, (2*8)(%rdi)
75185063facSAndy Lutomirski	movq	(3*8)(%rsp), %rax		/* user RFLAGS */
752905a36a2SIngo Molnar	movq	%rax, (3*8)(%rdi)
75385063facSAndy Lutomirski	movq	(5*8)(%rsp), %rax		/* user SS */
754905a36a2SIngo Molnar	movq	%rax, (5*8)(%rdi)
75585063facSAndy Lutomirski	movq	(4*8)(%rsp), %rax		/* user RSP */
756905a36a2SIngo Molnar	movq	%rax, (4*8)(%rdi)
75785063facSAndy Lutomirski	/* Now RAX == RSP. */
75885063facSAndy Lutomirski
75985063facSAndy Lutomirski	andl	$0xffff0000, %eax		/* RAX = (RSP & 0xffff0000) */
76085063facSAndy Lutomirski
76185063facSAndy Lutomirski	/*
76285063facSAndy Lutomirski	 * espfix_stack[31:16] == 0.  The page tables are set up such that
76385063facSAndy Lutomirski	 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
76485063facSAndy Lutomirski	 * espfix_waddr for any X.  That is, there are 65536 RO aliases of
76585063facSAndy Lutomirski	 * the same page.  Set up RSP so that RSP[31:16] contains the
76685063facSAndy Lutomirski	 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
76785063facSAndy Lutomirski	 * still points to an RO alias of the ESPFIX stack.
76885063facSAndy Lutomirski	 */
769905a36a2SIngo Molnar	orq	PER_CPU_VAR(espfix_stack), %rax
7708a09317bSDave Hansen
7716fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
7728a09317bSDave Hansen	SWAPGS					/* to user GS */
7738a09317bSDave Hansen	popq	%rdi				/* Restore user RDI */
7748a09317bSDave Hansen
775905a36a2SIngo Molnar	movq	%rax, %rsp
7768c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
77785063facSAndy Lutomirski
77885063facSAndy Lutomirski	/*
77985063facSAndy Lutomirski	 * At this point, we cannot write to the stack any more, but we can
78085063facSAndy Lutomirski	 * still read.
78185063facSAndy Lutomirski	 */
78285063facSAndy Lutomirski	popq	%rax				/* Restore user RAX */
78385063facSAndy Lutomirski
78485063facSAndy Lutomirski	/*
78585063facSAndy Lutomirski	 * RSP now points to an ordinary IRET frame, except that the page
78685063facSAndy Lutomirski	 * is read-only and RSP[31:16] are preloaded with the userspace
78785063facSAndy Lutomirski	 * values.  We can now IRET back to userspace.
78885063facSAndy Lutomirski	 */
789905a36a2SIngo Molnar	jmp	native_irq_return_iret
790905a36a2SIngo Molnar#endif
791cc66936eSJiri SlabySYM_CODE_END(common_interrupt)
792a50480cbSAndrea Righi_ASM_NOKPROBE(common_interrupt)
793905a36a2SIngo Molnar
794905a36a2SIngo Molnar/*
795905a36a2SIngo Molnar * APIC interrupts.
796905a36a2SIngo Molnar */
797905a36a2SIngo Molnar.macro apicinterrupt3 num sym do_sym
798bc7b11c0SJiri SlabySYM_CODE_START(\sym)
7998c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
800905a36a2SIngo Molnar	pushq	$~(\num)
801905a36a2SIngo Molnar.Lcommon_\sym:
8023aa99fc3SDominik Brodowski	call	interrupt_entry
8033aa99fc3SDominik Brodowski	UNWIND_HINT_REGS indirect=1
8043aa99fc3SDominik Brodowski	call	\do_sym	/* rdi points to pt_regs */
805905a36a2SIngo Molnar	jmp	ret_from_intr
806bc7b11c0SJiri SlabySYM_CODE_END(\sym)
807a50480cbSAndrea Righi_ASM_NOKPROBE(\sym)
808905a36a2SIngo Molnar.endm
809905a36a2SIngo Molnar
810469f0023SAlexander Potapenko/* Make sure APIC interrupt handlers end up in the irqentry section: */
811469f0023SAlexander Potapenko#define PUSH_SECTION_IRQENTRY	.pushsection .irqentry.text, "ax"
812469f0023SAlexander Potapenko#define POP_SECTION_IRQENTRY	.popsection
813469f0023SAlexander Potapenko
814905a36a2SIngo Molnar.macro apicinterrupt num sym do_sym
815469f0023SAlexander PotapenkoPUSH_SECTION_IRQENTRY
816905a36a2SIngo Molnarapicinterrupt3 \num \sym \do_sym
817469f0023SAlexander PotapenkoPOP_SECTION_IRQENTRY
818905a36a2SIngo Molnar.endm
819905a36a2SIngo Molnar
820905a36a2SIngo Molnar#ifdef CONFIG_SMP
8214d732138SIngo Molnarapicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR		irq_move_cleanup_interrupt	smp_irq_move_cleanup_interrupt
8224d732138SIngo Molnarapicinterrupt3 REBOOT_VECTOR			reboot_interrupt		smp_reboot_interrupt
823905a36a2SIngo Molnar#endif
824905a36a2SIngo Molnar
825905a36a2SIngo Molnar#ifdef CONFIG_X86_UV
8264d732138SIngo Molnarapicinterrupt3 UV_BAU_MESSAGE			uv_bau_message_intr1		uv_bau_message_interrupt
827905a36a2SIngo Molnar#endif
8284d732138SIngo Molnar
8294d732138SIngo Molnarapicinterrupt LOCAL_TIMER_VECTOR		apic_timer_interrupt		smp_apic_timer_interrupt
8304d732138SIngo Molnarapicinterrupt X86_PLATFORM_IPI_VECTOR		x86_platform_ipi		smp_x86_platform_ipi
831905a36a2SIngo Molnar
832905a36a2SIngo Molnar#ifdef CONFIG_HAVE_KVM
8334d732138SIngo Molnarapicinterrupt3 POSTED_INTR_VECTOR		kvm_posted_intr_ipi		smp_kvm_posted_intr_ipi
8344d732138SIngo Molnarapicinterrupt3 POSTED_INTR_WAKEUP_VECTOR	kvm_posted_intr_wakeup_ipi	smp_kvm_posted_intr_wakeup_ipi
835210f84b0SWincy Vanapicinterrupt3 POSTED_INTR_NESTED_VECTOR	kvm_posted_intr_nested_ipi	smp_kvm_posted_intr_nested_ipi
836905a36a2SIngo Molnar#endif
837905a36a2SIngo Molnar
838905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE_THRESHOLD
8394d732138SIngo Molnarapicinterrupt THRESHOLD_APIC_VECTOR		threshold_interrupt		smp_threshold_interrupt
840905a36a2SIngo Molnar#endif
841905a36a2SIngo Molnar
8429dda1658SIngo Molnar#ifdef CONFIG_X86_MCE_AMD
8434d732138SIngo Molnarapicinterrupt DEFERRED_ERROR_VECTOR		deferred_error_interrupt	smp_deferred_error_interrupt
8449dda1658SIngo Molnar#endif
8459dda1658SIngo Molnar
846905a36a2SIngo Molnar#ifdef CONFIG_X86_THERMAL_VECTOR
8474d732138SIngo Molnarapicinterrupt THERMAL_APIC_VECTOR		thermal_interrupt		smp_thermal_interrupt
848905a36a2SIngo Molnar#endif
849905a36a2SIngo Molnar
850905a36a2SIngo Molnar#ifdef CONFIG_SMP
8514d732138SIngo Molnarapicinterrupt CALL_FUNCTION_SINGLE_VECTOR	call_function_single_interrupt	smp_call_function_single_interrupt
8524d732138SIngo Molnarapicinterrupt CALL_FUNCTION_VECTOR		call_function_interrupt		smp_call_function_interrupt
8534d732138SIngo Molnarapicinterrupt RESCHEDULE_VECTOR			reschedule_interrupt		smp_reschedule_interrupt
854905a36a2SIngo Molnar#endif
855905a36a2SIngo Molnar
8564d732138SIngo Molnarapicinterrupt ERROR_APIC_VECTOR			error_interrupt			smp_error_interrupt
8574d732138SIngo Molnarapicinterrupt SPURIOUS_APIC_VECTOR		spurious_interrupt		smp_spurious_interrupt
858905a36a2SIngo Molnar
859905a36a2SIngo Molnar#ifdef CONFIG_IRQ_WORK
8604d732138SIngo Molnarapicinterrupt IRQ_WORK_VECTOR			irq_work_interrupt		smp_irq_work_interrupt
861905a36a2SIngo Molnar#endif
862905a36a2SIngo Molnar
863905a36a2SIngo Molnar/*
864905a36a2SIngo Molnar * Exception entry points.
865905a36a2SIngo Molnar */
8668f34c5b5SThomas Gleixner#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss_rw) + (TSS_ist + (x) * 8)
867905a36a2SIngo Molnar
868a0d14b89SPeter Zijlstra.macro idtentry_part do_sym, has_error_code:req, read_cr2:req, paranoid:req, shift_ist=-1, ist_offset=0
8692fd37912SPeter Zijlstra
8702fd37912SPeter Zijlstra	.if \paranoid
8712fd37912SPeter Zijlstra	call	paranoid_entry
8722fd37912SPeter Zijlstra	/* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
8732fd37912SPeter Zijlstra	.else
8742fd37912SPeter Zijlstra	call	error_entry
8752fd37912SPeter Zijlstra	.endif
8762fd37912SPeter Zijlstra	UNWIND_HINT_REGS
8772fd37912SPeter Zijlstra
878a0d14b89SPeter Zijlstra	.if \read_cr2
8796879298bSThomas Gleixner	/*
8806879298bSThomas Gleixner	 * Store CR2 early so subsequent faults cannot clobber it. Use R12 as
8816879298bSThomas Gleixner	 * intermediate storage as RDX can be clobbered in enter_from_user_mode().
8826879298bSThomas Gleixner	 * GET_CR2_INTO can clobber RAX.
8836879298bSThomas Gleixner	 */
8846879298bSThomas Gleixner	GET_CR2_INTO(%r12);
885a0d14b89SPeter Zijlstra	.endif
886a0d14b89SPeter Zijlstra
8872fd37912SPeter Zijlstra	.if \shift_ist != -1
8882fd37912SPeter Zijlstra	TRACE_IRQS_OFF_DEBUG			/* reload IDT in case of recursion */
8892fd37912SPeter Zijlstra	.else
8902fd37912SPeter Zijlstra	TRACE_IRQS_OFF
8912fd37912SPeter Zijlstra	.endif
892a0d14b89SPeter Zijlstra
893a0d14b89SPeter Zijlstra	.if \paranoid == 0
894a0d14b89SPeter Zijlstra	testb	$3, CS(%rsp)
895a0d14b89SPeter Zijlstra	jz	.Lfrom_kernel_no_context_tracking_\@
896a0d14b89SPeter Zijlstra	CALL_enter_from_user_mode
897a0d14b89SPeter Zijlstra.Lfrom_kernel_no_context_tracking_\@:
8982fd37912SPeter Zijlstra	.endif
8992fd37912SPeter Zijlstra
9002fd37912SPeter Zijlstra	movq	%rsp, %rdi			/* pt_regs pointer */
9012fd37912SPeter Zijlstra
9022fd37912SPeter Zijlstra	.if \has_error_code
9032fd37912SPeter Zijlstra	movq	ORIG_RAX(%rsp), %rsi		/* get error code */
9042fd37912SPeter Zijlstra	movq	$-1, ORIG_RAX(%rsp)		/* no syscall to restart */
9052fd37912SPeter Zijlstra	.else
9062fd37912SPeter Zijlstra	xorl	%esi, %esi			/* no error code */
9072fd37912SPeter Zijlstra	.endif
9082fd37912SPeter Zijlstra
9092fd37912SPeter Zijlstra	.if \shift_ist != -1
9102fd37912SPeter Zijlstra	subq	$\ist_offset, CPU_TSS_IST(\shift_ist)
9112fd37912SPeter Zijlstra	.endif
9122fd37912SPeter Zijlstra
9136879298bSThomas Gleixner	.if \read_cr2
9146879298bSThomas Gleixner	movq	%r12, %rdx			/* Move CR2 into 3rd argument */
9156879298bSThomas Gleixner	.endif
9166879298bSThomas Gleixner
9172fd37912SPeter Zijlstra	call	\do_sym
9182fd37912SPeter Zijlstra
9192fd37912SPeter Zijlstra	.if \shift_ist != -1
9202fd37912SPeter Zijlstra	addq	$\ist_offset, CPU_TSS_IST(\shift_ist)
9212fd37912SPeter Zijlstra	.endif
9222fd37912SPeter Zijlstra
9232fd37912SPeter Zijlstra	.if \paranoid
9242fd37912SPeter Zijlstra	/* this procedure expect "no swapgs" flag in ebx */
9252fd37912SPeter Zijlstra	jmp	paranoid_exit
9262fd37912SPeter Zijlstra	.else
9272fd37912SPeter Zijlstra	jmp	error_exit
9282fd37912SPeter Zijlstra	.endif
9292fd37912SPeter Zijlstra
9302fd37912SPeter Zijlstra.endm
9312fd37912SPeter Zijlstra
932bd7b1f7cSAndy Lutomirski/**
933bd7b1f7cSAndy Lutomirski * idtentry - Generate an IDT entry stub
934bd7b1f7cSAndy Lutomirski * @sym:		Name of the generated entry point
935bd7b1f7cSAndy Lutomirski * @do_sym:		C function to be called
936bd7b1f7cSAndy Lutomirski * @has_error_code:	True if this IDT vector has an error code on the stack
937bd7b1f7cSAndy Lutomirski * @paranoid:		non-zero means that this vector may be invoked from
938bd7b1f7cSAndy Lutomirski *			kernel mode with user GSBASE and/or user CR3.
939bd7b1f7cSAndy Lutomirski *			2 is special -- see below.
940bd7b1f7cSAndy Lutomirski * @shift_ist:		Set to an IST index if entries from kernel mode should
941bd7b1f7cSAndy Lutomirski *			decrement the IST stack so that nested entries get a
942bd7b1f7cSAndy Lutomirski *			fresh stack.  (This is for #DB, which has a nasty habit
943bd7b1f7cSAndy Lutomirski *			of recursing.)
9444234653eSPeter Zijlstra * @create_gap:		create a 6-word stack gap when coming from kernel mode.
945a0d14b89SPeter Zijlstra * @read_cr2:		load CR2 into the 3rd argument; done before calling any C code
946bd7b1f7cSAndy Lutomirski *
947bd7b1f7cSAndy Lutomirski * idtentry generates an IDT stub that sets up a usable kernel context,
948bd7b1f7cSAndy Lutomirski * creates struct pt_regs, and calls @do_sym.  The stub has the following
949bd7b1f7cSAndy Lutomirski * special behaviors:
950bd7b1f7cSAndy Lutomirski *
951bd7b1f7cSAndy Lutomirski * On an entry from user mode, the stub switches from the trampoline or
952bd7b1f7cSAndy Lutomirski * IST stack to the normal thread stack.  On an exit to user mode, the
953bd7b1f7cSAndy Lutomirski * normal exit-to-usermode path is invoked.
954bd7b1f7cSAndy Lutomirski *
955bd7b1f7cSAndy Lutomirski * On an exit to kernel mode, if @paranoid == 0, we check for preemption,
956bd7b1f7cSAndy Lutomirski * whereas we omit the preemption check if @paranoid != 0.  This is purely
957bd7b1f7cSAndy Lutomirski * because the implementation is simpler this way.  The kernel only needs
958bd7b1f7cSAndy Lutomirski * to check for asynchronous kernel preemption when IRQ handlers return.
959bd7b1f7cSAndy Lutomirski *
960bd7b1f7cSAndy Lutomirski * If @paranoid == 0, then the stub will handle IRET faults by pretending
961bd7b1f7cSAndy Lutomirski * that the fault came from user mode.  It will handle gs_change faults by
962bd7b1f7cSAndy Lutomirski * pretending that the fault happened with kernel GSBASE.  Since this handling
963bd7b1f7cSAndy Lutomirski * is omitted for @paranoid != 0, the #GP, #SS, and #NP stubs must have
964bd7b1f7cSAndy Lutomirski * @paranoid == 0.  This special handling will do the wrong thing for
965bd7b1f7cSAndy Lutomirski * espfix-induced #DF on IRET, so #DF must not use @paranoid == 0.
966bd7b1f7cSAndy Lutomirski *
967bd7b1f7cSAndy Lutomirski * @paranoid == 2 is special: the stub will never switch stacks.  This is for
968bd7b1f7cSAndy Lutomirski * #DF: if the thread stack is somehow unusable, we'll still get a useful OOPS.
969bd7b1f7cSAndy Lutomirski */
970a0d14b89SPeter Zijlstra.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 ist_offset=0 create_gap=0 read_cr2=0
971bc7b11c0SJiri SlabySYM_CODE_START(\sym)
97298990a33SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=\has_error_code*8
9738c1f7558SJosh Poimboeuf
974905a36a2SIngo Molnar	/* Sanity check */
9754234653eSPeter Zijlstra	.if \shift_ist != -1 && \paranoid != 1
976905a36a2SIngo Molnar	.error "using shift_ist requires paranoid=1"
977905a36a2SIngo Molnar	.endif
978905a36a2SIngo Molnar
9794234653eSPeter Zijlstra	.if \create_gap && \paranoid
9804234653eSPeter Zijlstra	.error "using create_gap requires paranoid=0"
9814234653eSPeter Zijlstra	.endif
9824234653eSPeter Zijlstra
983905a36a2SIngo Molnar	ASM_CLAC
984905a36a2SIngo Molnar
98582c62fa0SJosh Poimboeuf	.if \has_error_code == 0
986905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
987905a36a2SIngo Molnar	.endif
988905a36a2SIngo Molnar
989071ccc96SAndy Lutomirski	.if \paranoid == 1
9909e809d15SDominik Brodowski	testb	$3, CS-ORIG_RAX(%rsp)		/* If coming from userspace, switch stacks */
9917f2590a1SAndy Lutomirski	jnz	.Lfrom_usermode_switch_stack_\@
992905a36a2SIngo Molnar	.endif
9937f2590a1SAndy Lutomirski
9942700fefdSJosh Poimboeuf	.if \create_gap == 1
9952700fefdSJosh Poimboeuf	/*
9962700fefdSJosh Poimboeuf	 * If coming from kernel space, create a 6-word gap to allow the
9972700fefdSJosh Poimboeuf	 * int3 handler to emulate a call instruction.
9982700fefdSJosh Poimboeuf	 */
9992700fefdSJosh Poimboeuf	testb	$3, CS-ORIG_RAX(%rsp)
10002700fefdSJosh Poimboeuf	jnz	.Lfrom_usermode_no_gap_\@
10012700fefdSJosh Poimboeuf	.rept	6
10022700fefdSJosh Poimboeuf	pushq	5*8(%rsp)
10032700fefdSJosh Poimboeuf	.endr
10042700fefdSJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
10052700fefdSJosh Poimboeuf.Lfrom_usermode_no_gap_\@:
10062700fefdSJosh Poimboeuf	.endif
10072700fefdSJosh Poimboeuf
1008a0d14b89SPeter Zijlstra	idtentry_part \do_sym, \has_error_code, \read_cr2, \paranoid, \shift_ist, \ist_offset
1009905a36a2SIngo Molnar
1010071ccc96SAndy Lutomirski	.if \paranoid == 1
1011905a36a2SIngo Molnar	/*
10127f2590a1SAndy Lutomirski	 * Entry from userspace.  Switch stacks and treat it
1013905a36a2SIngo Molnar	 * as a normal entry.  This means that paranoid handlers
1014905a36a2SIngo Molnar	 * run in real process context if user_mode(regs).
1015905a36a2SIngo Molnar	 */
10167f2590a1SAndy Lutomirski.Lfrom_usermode_switch_stack_\@:
1017a0d14b89SPeter Zijlstra	idtentry_part \do_sym, \has_error_code, \read_cr2, paranoid=0
1018905a36a2SIngo Molnar	.endif
1019905a36a2SIngo Molnar
1020a50480cbSAndrea Righi_ASM_NOKPROBE(\sym)
1021bc7b11c0SJiri SlabySYM_CODE_END(\sym)
1022905a36a2SIngo Molnar.endm
1023905a36a2SIngo Molnar
1024905a36a2SIngo Molnaridtentry divide_error			do_divide_error			has_error_code=0
1025905a36a2SIngo Molnaridtentry overflow			do_overflow			has_error_code=0
1026905a36a2SIngo Molnaridtentry bounds				do_bounds			has_error_code=0
1027905a36a2SIngo Molnaridtentry invalid_op			do_invalid_op			has_error_code=0
1028905a36a2SIngo Molnaridtentry device_not_available		do_device_not_available		has_error_code=0
1029a0d14b89SPeter Zijlstraidtentry double_fault			do_double_fault			has_error_code=1 paranoid=2 read_cr2=1
1030905a36a2SIngo Molnaridtentry coprocessor_segment_overrun	do_coprocessor_segment_overrun	has_error_code=0
1031905a36a2SIngo Molnaridtentry invalid_TSS			do_invalid_TSS			has_error_code=1
1032905a36a2SIngo Molnaridtentry segment_not_present		do_segment_not_present		has_error_code=1
1033905a36a2SIngo Molnaridtentry spurious_interrupt_bug		do_spurious_interrupt_bug	has_error_code=0
1034905a36a2SIngo Molnaridtentry coprocessor_error		do_coprocessor_error		has_error_code=0
1035905a36a2SIngo Molnaridtentry alignment_check		do_alignment_check		has_error_code=1
1036905a36a2SIngo Molnaridtentry simd_coprocessor_error		do_simd_coprocessor_error	has_error_code=0
1037905a36a2SIngo Molnar
1038905a36a2SIngo Molnar
10394d732138SIngo Molnar	/*
10404d732138SIngo Molnar	 * Reload gs selector with exception handling
10414d732138SIngo Molnar	 * edi:  new selector
10424d732138SIngo Molnar	 */
10436dcc5627SJiri SlabySYM_FUNC_START(native_load_gs_index)
10448c1f7558SJosh Poimboeuf	FRAME_BEGIN
1045905a36a2SIngo Molnar	pushfq
1046905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1047ca37e57bSAndy Lutomirski	TRACE_IRQS_OFF
1048905a36a2SIngo Molnar	SWAPGS
104942c748bbSBorislav Petkov.Lgs_change:
1050905a36a2SIngo Molnar	movl	%edi, %gs
105196e5d28aSBorislav Petkov2:	ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
1052905a36a2SIngo Molnar	SWAPGS
1053ca37e57bSAndy Lutomirski	TRACE_IRQS_FLAGS (%rsp)
1054905a36a2SIngo Molnar	popfq
10558c1f7558SJosh Poimboeuf	FRAME_END
1056905a36a2SIngo Molnar	ret
10576dcc5627SJiri SlabySYM_FUNC_END(native_load_gs_index)
1058784d5699SAl ViroEXPORT_SYMBOL(native_load_gs_index)
1059905a36a2SIngo Molnar
106098ededb6SJiri Slaby	_ASM_EXTABLE(.Lgs_change, .Lbad_gs)
1061905a36a2SIngo Molnar	.section .fixup, "ax"
1062905a36a2SIngo Molnar	/* running with kernelgs */
1063ef77e688SJiri SlabySYM_CODE_START_LOCAL_NOALIGN(.Lbad_gs)
1064905a36a2SIngo Molnar	SWAPGS					/* switch back to user gs */
1065b038c842SAndy Lutomirski.macro ZAP_GS
1066b038c842SAndy Lutomirski	/* This can't be a string because the preprocessor needs to see it. */
1067b038c842SAndy Lutomirski	movl $__USER_DS, %eax
1068b038c842SAndy Lutomirski	movl %eax, %gs
1069b038c842SAndy Lutomirski.endm
1070b038c842SAndy Lutomirski	ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
1071905a36a2SIngo Molnar	xorl	%eax, %eax
1072905a36a2SIngo Molnar	movl	%eax, %gs
1073905a36a2SIngo Molnar	jmp	2b
1074ef77e688SJiri SlabySYM_CODE_END(.Lbad_gs)
1075905a36a2SIngo Molnar	.previous
1076905a36a2SIngo Molnar
1077905a36a2SIngo Molnar/* Call softirq on interrupt stack. Interrupts are off. */
10786dcc5627SJiri SlabySYM_FUNC_START(do_softirq_own_stack)
1079905a36a2SIngo Molnar	pushq	%rbp
1080905a36a2SIngo Molnar	mov	%rsp, %rbp
10818c1f7558SJosh Poimboeuf	ENTER_IRQ_STACK regs=0 old_rsp=%r11
1082905a36a2SIngo Molnar	call	__do_softirq
10838c1f7558SJosh Poimboeuf	LEAVE_IRQ_STACK regs=0
1084905a36a2SIngo Molnar	leaveq
1085905a36a2SIngo Molnar	ret
10866dcc5627SJiri SlabySYM_FUNC_END(do_softirq_own_stack)
1087905a36a2SIngo Molnar
108828c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV
10895878d5d6SJuergen Grossidtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0
1090905a36a2SIngo Molnar
1091905a36a2SIngo Molnar/*
1092905a36a2SIngo Molnar * A note on the "critical region" in our callback handler.
1093905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring
1094905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled
1095905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before
1096905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still
1097905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack.
1098905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd
1099905a36a2SIngo Molnar * like to avoid the possibility.
1100905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an
1101905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current
1102905a36a2SIngo Molnar * activation and restart the handler using the previous one.
1103905a36a2SIngo Molnar */
1104ef1e0315SJiri Slaby/* do_hypervisor_callback(struct *pt_regs) */
1105ef1e0315SJiri SlabySYM_CODE_START_LOCAL(xen_do_hypervisor_callback)
11064d732138SIngo Molnar
1107905a36a2SIngo Molnar/*
1108905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1109905a36a2SIngo Molnar * see the correct pointer to the pt_regs
1110905a36a2SIngo Molnar */
11118c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
11124d732138SIngo Molnar	movq	%rdi, %rsp			/* we don't return, adjust the stack frame */
11138c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
11141d3e53e8SAndy Lutomirski
11151d3e53e8SAndy Lutomirski	ENTER_IRQ_STACK old_rsp=%r10
1116905a36a2SIngo Molnar	call	xen_evtchn_do_upcall
11171d3e53e8SAndy Lutomirski	LEAVE_IRQ_STACK
11181d3e53e8SAndy Lutomirski
111948593975SThomas Gleixner#ifndef CONFIG_PREEMPTION
1120905a36a2SIngo Molnar	call	xen_maybe_preempt_hcall
1121905a36a2SIngo Molnar#endif
1122905a36a2SIngo Molnar	jmp	error_exit
1123ef1e0315SJiri SlabySYM_CODE_END(xen_do_hypervisor_callback)
1124905a36a2SIngo Molnar
1125905a36a2SIngo Molnar/*
1126905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes.
1127905a36a2SIngo Molnar * We get here for two reasons:
1128905a36a2SIngo Molnar *  1. Fault while reloading DS, ES, FS or GS
1129905a36a2SIngo Molnar *  2. Fault while executing IRET
1130905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment
1131905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others.
1132905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the
1133905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall
1134905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1135905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register
1136905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1.
1137905a36a2SIngo Molnar */
1138bc7b11c0SJiri SlabySYM_CODE_START(xen_failsafe_callback)
11398c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
1140905a36a2SIngo Molnar	movl	%ds, %ecx
1141905a36a2SIngo Molnar	cmpw	%cx, 0x10(%rsp)
1142905a36a2SIngo Molnar	jne	1f
1143905a36a2SIngo Molnar	movl	%es, %ecx
1144905a36a2SIngo Molnar	cmpw	%cx, 0x18(%rsp)
1145905a36a2SIngo Molnar	jne	1f
1146905a36a2SIngo Molnar	movl	%fs, %ecx
1147905a36a2SIngo Molnar	cmpw	%cx, 0x20(%rsp)
1148905a36a2SIngo Molnar	jne	1f
1149905a36a2SIngo Molnar	movl	%gs, %ecx
1150905a36a2SIngo Molnar	cmpw	%cx, 0x28(%rsp)
1151905a36a2SIngo Molnar	jne	1f
1152905a36a2SIngo Molnar	/* All segments match their saved values => Category 2 (Bad IRET). */
1153905a36a2SIngo Molnar	movq	(%rsp), %rcx
1154905a36a2SIngo Molnar	movq	8(%rsp), %r11
1155905a36a2SIngo Molnar	addq	$0x30, %rsp
1156905a36a2SIngo Molnar	pushq	$0				/* RIP */
11578c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
1158905a36a2SIngo Molnar	jmp	general_protection
1159905a36a2SIngo Molnar1:	/* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1160905a36a2SIngo Molnar	movq	(%rsp), %rcx
1161905a36a2SIngo Molnar	movq	8(%rsp), %r11
1162905a36a2SIngo Molnar	addq	$0x30, %rsp
11638c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1164905a36a2SIngo Molnar	pushq	$-1 /* orig_ax = -1 => not a system call */
11653f01daecSDominik Brodowski	PUSH_AND_CLEAR_REGS
1166946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
1167905a36a2SIngo Molnar	jmp	error_exit
1168bc7b11c0SJiri SlabySYM_CODE_END(xen_failsafe_callback)
116928c11b0fSJuergen Gross#endif /* CONFIG_XEN_PV */
1170905a36a2SIngo Molnar
117128c11b0fSJuergen Gross#ifdef CONFIG_XEN_PVHVM
1172905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1173905a36a2SIngo Molnar	xen_hvm_callback_vector xen_evtchn_do_upcall
117428c11b0fSJuergen Gross#endif
1175905a36a2SIngo Molnar
1176905a36a2SIngo Molnar
1177905a36a2SIngo Molnar#if IS_ENABLED(CONFIG_HYPERV)
1178905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1179905a36a2SIngo Molnar	hyperv_callback_vector hyperv_vector_handler
118093286261SVitaly Kuznetsov
118193286261SVitaly Kuznetsovapicinterrupt3 HYPERV_REENLIGHTENMENT_VECTOR \
118293286261SVitaly Kuznetsov	hyperv_reenlightenment_vector hyperv_reenlightenment_intr
1183248e742aSMichael Kelley
1184248e742aSMichael Kelleyapicinterrupt3 HYPERV_STIMER0_VECTOR \
1185248e742aSMichael Kelley	hv_stimer0_callback_vector hv_stimer0_vector_handler
1186905a36a2SIngo Molnar#endif /* CONFIG_HYPERV */
1187905a36a2SIngo Molnar
1188498ad393SZhao Yakui#if IS_ENABLED(CONFIG_ACRN_GUEST)
1189498ad393SZhao Yakuiapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1190498ad393SZhao Yakui	acrn_hv_callback_vector acrn_hv_vector_handler
1191498ad393SZhao Yakui#endif
1192498ad393SZhao Yakui
11932a594d4cSThomas Gleixneridtentry debug			do_debug		has_error_code=0	paranoid=1 shift_ist=IST_INDEX_DB ist_offset=DB_STACK_OFFSET
11942700fefdSJosh Poimboeufidtentry int3			do_int3			has_error_code=0	create_gap=1
1195905a36a2SIngo Molnaridtentry stack_segment		do_stack_segment	has_error_code=1
11964d732138SIngo Molnar
119728c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV
119843e41110SJuergen Grossidtentry xennmi			do_nmi			has_error_code=0
11995878d5d6SJuergen Grossidtentry xendebug		do_debug		has_error_code=0
1200905a36a2SIngo Molnar#endif
12014d732138SIngo Molnar
1202905a36a2SIngo Molnaridtentry general_protection	do_general_protection	has_error_code=1
1203a0d14b89SPeter Zijlstraidtentry page_fault		do_page_fault		has_error_code=1	read_cr2=1
12044d732138SIngo Molnar
1205905a36a2SIngo Molnar#ifdef CONFIG_KVM_GUEST
1206a0d14b89SPeter Zijlstraidtentry async_page_fault	do_async_page_fault	has_error_code=1	read_cr2=1
1207905a36a2SIngo Molnar#endif
12084d732138SIngo Molnar
1209905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE
12106f41c34dSThomas Gleixneridtentry machine_check		do_mce			has_error_code=0	paranoid=1
1211905a36a2SIngo Molnar#endif
1212905a36a2SIngo Molnar
1213905a36a2SIngo Molnar/*
12149e809d15SDominik Brodowski * Save all registers in pt_regs, and switch gs if needed.
1215905a36a2SIngo Molnar * Use slow, but surefire "are we in kernel?" check.
1216905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1217905a36a2SIngo Molnar */
1218ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_entry)
12198c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
1220905a36a2SIngo Molnar	cld
12219e809d15SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
12229e809d15SDominik Brodowski	ENCODE_FRAME_POINTER 8
1223905a36a2SIngo Molnar	movl	$1, %ebx
1224905a36a2SIngo Molnar	movl	$MSR_GS_BASE, %ecx
1225905a36a2SIngo Molnar	rdmsr
1226905a36a2SIngo Molnar	testl	%edx, %edx
1227905a36a2SIngo Molnar	js	1f				/* negative -> in kernel */
1228905a36a2SIngo Molnar	SWAPGS
1229905a36a2SIngo Molnar	xorl	%ebx, %ebx
12308a09317bSDave Hansen
12318a09317bSDave Hansen1:
123216561f27SDave Hansen	/*
123316561f27SDave Hansen	 * Always stash CR3 in %r14.  This value will be restored,
1234ae852495SAndy Lutomirski	 * verbatim, at exit.  Needed if paranoid_entry interrupted
1235ae852495SAndy Lutomirski	 * another entry that already switched to the user CR3 value
1236ae852495SAndy Lutomirski	 * but has not yet returned to userspace.
123716561f27SDave Hansen	 *
123816561f27SDave Hansen	 * This is also why CS (stashed in the "iret frame" by the
123916561f27SDave Hansen	 * hardware at entry) can not be used: this may be a return
1240ae852495SAndy Lutomirski	 * to kernel code, but with a user CR3 value.
124116561f27SDave Hansen	 */
12428a09317bSDave Hansen	SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
12438a09317bSDave Hansen
124418ec54fdSJosh Poimboeuf	/*
124518ec54fdSJosh Poimboeuf	 * The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an
124618ec54fdSJosh Poimboeuf	 * unconditional CR3 write, even in the PTI case.  So do an lfence
124718ec54fdSJosh Poimboeuf	 * to prevent GS speculation, regardless of whether PTI is enabled.
124818ec54fdSJosh Poimboeuf	 */
124918ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
125018ec54fdSJosh Poimboeuf
12518a09317bSDave Hansen	ret
1252ef1e0315SJiri SlabySYM_CODE_END(paranoid_entry)
1253905a36a2SIngo Molnar
1254905a36a2SIngo Molnar/*
1255905a36a2SIngo Molnar * "Paranoid" exit path from exception stack.  This is invoked
1256905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came
1257905a36a2SIngo Molnar * from kernel space.
1258905a36a2SIngo Molnar *
1259905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early
1260905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would
1261905a36a2SIngo Molnar * be complicated.  Fortunately, we there's no good reason
1262905a36a2SIngo Molnar * to try to handle preemption here.
12634d732138SIngo Molnar *
12644d732138SIngo Molnar * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
1265905a36a2SIngo Molnar */
1266ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_exit)
12678c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
12682140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
1269905a36a2SIngo Molnar	TRACE_IRQS_OFF_DEBUG
1270905a36a2SIngo Molnar	testl	%ebx, %ebx			/* swapgs needed? */
1271e5317832SAndy Lutomirski	jnz	.Lparanoid_exit_no_swapgs
1272905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
127316561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
127421e94459SPeter Zijlstra	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
1275905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
127645c08383SThomas Gleixner	jmp	restore_regs_and_return_to_kernel
1277e5317832SAndy Lutomirski.Lparanoid_exit_no_swapgs:
1278905a36a2SIngo Molnar	TRACE_IRQS_IRETQ_DEBUG
127916561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
1280e4865757SIngo Molnar	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
1281e5317832SAndy Lutomirski	jmp restore_regs_and_return_to_kernel
1282ef1e0315SJiri SlabySYM_CODE_END(paranoid_exit)
1283905a36a2SIngo Molnar
1284905a36a2SIngo Molnar/*
12859e809d15SDominik Brodowski * Save all registers in pt_regs, and switch GS if needed.
1286905a36a2SIngo Molnar */
1287ef1e0315SJiri SlabySYM_CODE_START_LOCAL(error_entry)
12889e809d15SDominik Brodowski	UNWIND_HINT_FUNC
1289905a36a2SIngo Molnar	cld
12909e809d15SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
12919e809d15SDominik Brodowski	ENCODE_FRAME_POINTER 8
1292905a36a2SIngo Molnar	testb	$3, CS+8(%rsp)
1293cb6f64edSAndy Lutomirski	jz	.Lerror_kernelspace
1294539f5113SAndy Lutomirski
1295cb6f64edSAndy Lutomirski	/*
1296cb6f64edSAndy Lutomirski	 * We entered from user mode or we're pretending to have entered
1297cb6f64edSAndy Lutomirski	 * from user mode due to an IRET fault.
1298cb6f64edSAndy Lutomirski	 */
1299905a36a2SIngo Molnar	SWAPGS
130018ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
13018a09317bSDave Hansen	/* We have user CR3.  Change to kernel CR3. */
13028a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
1303539f5113SAndy Lutomirski
1304cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_after_swapgs:
13057f2590a1SAndy Lutomirski	/* Put us onto the real thread stack. */
13067f2590a1SAndy Lutomirski	popq	%r12				/* save return addr in %12 */
13077f2590a1SAndy Lutomirski	movq	%rsp, %rdi			/* arg0 = pt_regs pointer */
13087f2590a1SAndy Lutomirski	call	sync_regs
13097f2590a1SAndy Lutomirski	movq	%rax, %rsp			/* switch stack */
13107f2590a1SAndy Lutomirski	ENCODE_FRAME_POINTER
13117f2590a1SAndy Lutomirski	pushq	%r12
1312f1075053SAndy Lutomirski	ret
131302bc7768SAndy Lutomirski
131418ec54fdSJosh Poimboeuf.Lerror_entry_done_lfence:
131518ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
1316cb6f64edSAndy Lutomirski.Lerror_entry_done:
1317905a36a2SIngo Molnar	ret
1318905a36a2SIngo Molnar
1319905a36a2SIngo Molnar	/*
1320905a36a2SIngo Molnar	 * There are two places in the kernel that can potentially fault with
1321905a36a2SIngo Molnar	 * usergs. Handle them here.  B stepping K8s sometimes report a
1322905a36a2SIngo Molnar	 * truncated RIP for IRET exceptions returning to compat mode. Check
1323905a36a2SIngo Molnar	 * for these here too.
1324905a36a2SIngo Molnar	 */
1325cb6f64edSAndy Lutomirski.Lerror_kernelspace:
1326905a36a2SIngo Molnar	leaq	native_irq_return_iret(%rip), %rcx
1327905a36a2SIngo Molnar	cmpq	%rcx, RIP+8(%rsp)
1328cb6f64edSAndy Lutomirski	je	.Lerror_bad_iret
1329905a36a2SIngo Molnar	movl	%ecx, %eax			/* zero extend */
1330905a36a2SIngo Molnar	cmpq	%rax, RIP+8(%rsp)
1331cb6f64edSAndy Lutomirski	je	.Lbstep_iret
133242c748bbSBorislav Petkov	cmpq	$.Lgs_change, RIP+8(%rsp)
133318ec54fdSJosh Poimboeuf	jne	.Lerror_entry_done_lfence
1334539f5113SAndy Lutomirski
1335539f5113SAndy Lutomirski	/*
133642c748bbSBorislav Petkov	 * hack: .Lgs_change can fail with user gsbase.  If this happens, fix up
1337539f5113SAndy Lutomirski	 * gsbase and proceed.  We'll fix up the exception and land in
133842c748bbSBorislav Petkov	 * .Lgs_change's error handler with kernel gsbase.
1339539f5113SAndy Lutomirski	 */
13402fa5f04fSWanpeng Li	SWAPGS
134118ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
13428a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
13432fa5f04fSWanpeng Li	jmp .Lerror_entry_done
1344905a36a2SIngo Molnar
1345cb6f64edSAndy Lutomirski.Lbstep_iret:
1346905a36a2SIngo Molnar	/* Fix truncated RIP */
1347905a36a2SIngo Molnar	movq	%rcx, RIP+8(%rsp)
1348905a36a2SIngo Molnar	/* fall through */
1349905a36a2SIngo Molnar
1350cb6f64edSAndy Lutomirski.Lerror_bad_iret:
1351539f5113SAndy Lutomirski	/*
13528a09317bSDave Hansen	 * We came from an IRET to user mode, so we have user
13538a09317bSDave Hansen	 * gsbase and CR3.  Switch to kernel gsbase and CR3:
1354539f5113SAndy Lutomirski	 */
1355905a36a2SIngo Molnar	SWAPGS
135618ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
13578a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
1358539f5113SAndy Lutomirski
1359539f5113SAndy Lutomirski	/*
1360539f5113SAndy Lutomirski	 * Pretend that the exception came from user mode: set up pt_regs
1361b3681dd5SAndy Lutomirski	 * as if we faulted immediately after IRET.
1362539f5113SAndy Lutomirski	 */
1363905a36a2SIngo Molnar	mov	%rsp, %rdi
1364905a36a2SIngo Molnar	call	fixup_bad_iret
1365905a36a2SIngo Molnar	mov	%rax, %rsp
1366cb6f64edSAndy Lutomirski	jmp	.Lerror_entry_from_usermode_after_swapgs
1367ef1e0315SJiri SlabySYM_CODE_END(error_entry)
1368905a36a2SIngo Molnar
1369ef1e0315SJiri SlabySYM_CODE_START_LOCAL(error_exit)
13708c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
13712140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
1372905a36a2SIngo Molnar	TRACE_IRQS_OFF
1373b3681dd5SAndy Lutomirski	testb	$3, CS(%rsp)
1374b3681dd5SAndy Lutomirski	jz	retint_kernel
137530a2441cSJiri Slaby	jmp	.Lretint_user
1376ef1e0315SJiri SlabySYM_CODE_END(error_exit)
1377905a36a2SIngo Molnar
1378929bacecSAndy Lutomirski/*
1379929bacecSAndy Lutomirski * Runs on exception stack.  Xen PV does not go through this path at all,
1380929bacecSAndy Lutomirski * so we can use real assembly here.
13818a09317bSDave Hansen *
13828a09317bSDave Hansen * Registers:
13838a09317bSDave Hansen *	%r14: Used to save/restore the CR3 of the interrupted context
13848a09317bSDave Hansen *	      when PAGE_TABLE_ISOLATION is in use.  Do not clobber.
1385929bacecSAndy Lutomirski */
1386bc7b11c0SJiri SlabySYM_CODE_START(nmi)
13878c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1388929bacecSAndy Lutomirski
1389fc57a7c6SAndy Lutomirski	/*
1390905a36a2SIngo Molnar	 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1391905a36a2SIngo Molnar	 * the iretq it performs will take us out of NMI context.
1392905a36a2SIngo Molnar	 * This means that we can have nested NMIs where the next
1393905a36a2SIngo Molnar	 * NMI is using the top of the stack of the previous NMI. We
1394905a36a2SIngo Molnar	 * can't let it execute because the nested NMI will corrupt the
1395905a36a2SIngo Molnar	 * stack of the previous NMI. NMI handlers are not re-entrant
1396905a36a2SIngo Molnar	 * anyway.
1397905a36a2SIngo Molnar	 *
1398905a36a2SIngo Molnar	 * To handle this case we do the following:
1399905a36a2SIngo Molnar	 *  Check the a special location on the stack that contains
1400905a36a2SIngo Molnar	 *  a variable that is set when NMIs are executing.
1401905a36a2SIngo Molnar	 *  The interrupted task's stack is also checked to see if it
1402905a36a2SIngo Molnar	 *  is an NMI stack.
1403905a36a2SIngo Molnar	 *  If the variable is not set and the stack is not the NMI
1404905a36a2SIngo Molnar	 *  stack then:
1405905a36a2SIngo Molnar	 *    o Set the special variable on the stack
14060b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "outermost" location on the
14070b22930eSAndy Lutomirski	 *      stack
14080b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "iret" location on the stack
1409905a36a2SIngo Molnar	 *    o Continue processing the NMI
1410905a36a2SIngo Molnar	 *  If the variable is set or the previous stack is the NMI stack:
14110b22930eSAndy Lutomirski	 *    o Modify the "iret" location to jump to the repeat_nmi
1412905a36a2SIngo Molnar	 *    o return back to the first NMI
1413905a36a2SIngo Molnar	 *
1414905a36a2SIngo Molnar	 * Now on exit of the first NMI, we first clear the stack variable
1415905a36a2SIngo Molnar	 * The NMI stack will tell any nested NMIs at that point that it is
1416905a36a2SIngo Molnar	 * nested. Then we pop the stack normally with iret, and if there was
1417905a36a2SIngo Molnar	 * a nested NMI that updated the copy interrupt stack frame, a
1418905a36a2SIngo Molnar	 * jump will be made to the repeat_nmi code that will handle the second
1419905a36a2SIngo Molnar	 * NMI.
14209b6e6a83SAndy Lutomirski	 *
14219b6e6a83SAndy Lutomirski	 * However, espfix prevents us from directly returning to userspace
14229b6e6a83SAndy Lutomirski	 * with a single IRET instruction.  Similarly, IRET to user mode
14239b6e6a83SAndy Lutomirski	 * can fault.  We therefore handle NMIs from user space like
14249b6e6a83SAndy Lutomirski	 * other IST entries.
1425905a36a2SIngo Molnar	 */
1426905a36a2SIngo Molnar
1427e93c1730SAndy Lutomirski	ASM_CLAC
1428e93c1730SAndy Lutomirski
1429905a36a2SIngo Molnar	/* Use %rdx as our temp variable throughout */
1430905a36a2SIngo Molnar	pushq	%rdx
1431905a36a2SIngo Molnar
14329b6e6a83SAndy Lutomirski	testb	$3, CS-RIP+8(%rsp)
14339b6e6a83SAndy Lutomirski	jz	.Lnmi_from_kernel
1434905a36a2SIngo Molnar
1435905a36a2SIngo Molnar	/*
14369b6e6a83SAndy Lutomirski	 * NMI from user mode.  We need to run on the thread stack, but we
14379b6e6a83SAndy Lutomirski	 * can't go through the normal entry paths: NMIs are masked, and
14389b6e6a83SAndy Lutomirski	 * we don't want to enable interrupts, because then we'll end
14399b6e6a83SAndy Lutomirski	 * up in an awkward situation in which IRQs are on but NMIs
14409b6e6a83SAndy Lutomirski	 * are off.
144183c133cfSAndy Lutomirski	 *
144283c133cfSAndy Lutomirski	 * We also must not push anything to the stack before switching
144383c133cfSAndy Lutomirski	 * stacks lest we corrupt the "NMI executing" variable.
14449b6e6a83SAndy Lutomirski	 */
14459b6e6a83SAndy Lutomirski
1446929bacecSAndy Lutomirski	swapgs
14479b6e6a83SAndy Lutomirski	cld
144818ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
14498a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
14509b6e6a83SAndy Lutomirski	movq	%rsp, %rdx
14519b6e6a83SAndy Lutomirski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
14528c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS base=%rdx offset=8
14539b6e6a83SAndy Lutomirski	pushq	5*8(%rdx)	/* pt_regs->ss */
14549b6e6a83SAndy Lutomirski	pushq	4*8(%rdx)	/* pt_regs->rsp */
14559b6e6a83SAndy Lutomirski	pushq	3*8(%rdx)	/* pt_regs->flags */
14569b6e6a83SAndy Lutomirski	pushq	2*8(%rdx)	/* pt_regs->cs */
14579b6e6a83SAndy Lutomirski	pushq	1*8(%rdx)	/* pt_regs->rip */
14588c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
14599b6e6a83SAndy Lutomirski	pushq   $-1		/* pt_regs->orig_ax */
146030907fd1SDominik Brodowski	PUSH_AND_CLEAR_REGS rdx=(%rdx)
1461946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
14629b6e6a83SAndy Lutomirski
14639b6e6a83SAndy Lutomirski	/*
14649b6e6a83SAndy Lutomirski	 * At this point we no longer need to worry about stack damage
14659b6e6a83SAndy Lutomirski	 * due to nesting -- we're on the normal thread stack and we're
14669b6e6a83SAndy Lutomirski	 * done with the NMI stack.
14679b6e6a83SAndy Lutomirski	 */
14689b6e6a83SAndy Lutomirski
14699b6e6a83SAndy Lutomirski	movq	%rsp, %rdi
14709b6e6a83SAndy Lutomirski	movq	$-1, %rsi
14719b6e6a83SAndy Lutomirski	call	do_nmi
14729b6e6a83SAndy Lutomirski
14739b6e6a83SAndy Lutomirski	/*
14749b6e6a83SAndy Lutomirski	 * Return back to user mode.  We must *not* do the normal exit
1475946c1911SJosh Poimboeuf	 * work, because we don't want to enable interrupts.
14769b6e6a83SAndy Lutomirski	 */
14778a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
14789b6e6a83SAndy Lutomirski
14799b6e6a83SAndy Lutomirski.Lnmi_from_kernel:
14809b6e6a83SAndy Lutomirski	/*
14810b22930eSAndy Lutomirski	 * Here's what our stack frame will look like:
14820b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14830b22930eSAndy Lutomirski	 * | original SS                                             |
14840b22930eSAndy Lutomirski	 * | original Return RSP                                     |
14850b22930eSAndy Lutomirski	 * | original RFLAGS                                         |
14860b22930eSAndy Lutomirski	 * | original CS                                             |
14870b22930eSAndy Lutomirski	 * | original RIP                                            |
14880b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14890b22930eSAndy Lutomirski	 * | temp storage for rdx                                    |
14900b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14910b22930eSAndy Lutomirski	 * | "NMI executing" variable                                |
14920b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14930b22930eSAndy Lutomirski	 * | iret SS          } Copied from "outermost" frame        |
14940b22930eSAndy Lutomirski	 * | iret Return RSP  } on each loop iteration; overwritten  |
14950b22930eSAndy Lutomirski	 * | iret RFLAGS      } by a nested NMI to force another     |
14960b22930eSAndy Lutomirski	 * | iret CS          } iteration if needed.                 |
14970b22930eSAndy Lutomirski	 * | iret RIP         }                                      |
14980b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14990b22930eSAndy Lutomirski	 * | outermost SS          } initialized in first_nmi;       |
15000b22930eSAndy Lutomirski	 * | outermost Return RSP  } will not be changed before      |
15010b22930eSAndy Lutomirski	 * | outermost RFLAGS      } NMI processing is done.         |
15020b22930eSAndy Lutomirski	 * | outermost CS          } Copied to "iret" frame on each  |
15030b22930eSAndy Lutomirski	 * | outermost RIP         } iteration.                      |
15040b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
15050b22930eSAndy Lutomirski	 * | pt_regs                                                 |
15060b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
15070b22930eSAndy Lutomirski	 *
15080b22930eSAndy Lutomirski	 * The "original" frame is used by hardware.  Before re-enabling
15090b22930eSAndy Lutomirski	 * NMIs, we need to be done with it, and we need to leave enough
15100b22930eSAndy Lutomirski	 * space for the asm code here.
15110b22930eSAndy Lutomirski	 *
15120b22930eSAndy Lutomirski	 * We return by executing IRET while RSP points to the "iret" frame.
15130b22930eSAndy Lutomirski	 * That will either return for real or it will loop back into NMI
15140b22930eSAndy Lutomirski	 * processing.
15150b22930eSAndy Lutomirski	 *
15160b22930eSAndy Lutomirski	 * The "outermost" frame is copied to the "iret" frame on each
15170b22930eSAndy Lutomirski	 * iteration of the loop, so each iteration starts with the "iret"
15180b22930eSAndy Lutomirski	 * frame pointing to the final return target.
15190b22930eSAndy Lutomirski	 */
15200b22930eSAndy Lutomirski
15210b22930eSAndy Lutomirski	/*
15220b22930eSAndy Lutomirski	 * Determine whether we're a nested NMI.
15230b22930eSAndy Lutomirski	 *
1524a27507caSAndy Lutomirski	 * If we interrupted kernel code between repeat_nmi and
1525a27507caSAndy Lutomirski	 * end_repeat_nmi, then we are a nested NMI.  We must not
1526a27507caSAndy Lutomirski	 * modify the "iret" frame because it's being written by
1527a27507caSAndy Lutomirski	 * the outer NMI.  That's okay; the outer NMI handler is
1528a27507caSAndy Lutomirski	 * about to about to call do_nmi anyway, so we can just
1529a27507caSAndy Lutomirski	 * resume the outer NMI.
1530a27507caSAndy Lutomirski	 */
1531a27507caSAndy Lutomirski
1532a27507caSAndy Lutomirski	movq	$repeat_nmi, %rdx
1533a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1534a27507caSAndy Lutomirski	ja	1f
1535a27507caSAndy Lutomirski	movq	$end_repeat_nmi, %rdx
1536a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1537a27507caSAndy Lutomirski	ja	nested_nmi_out
1538a27507caSAndy Lutomirski1:
1539a27507caSAndy Lutomirski
1540a27507caSAndy Lutomirski	/*
1541a27507caSAndy Lutomirski	 * Now check "NMI executing".  If it's set, then we're nested.
15420b22930eSAndy Lutomirski	 * This will not detect if we interrupted an outer NMI just
15430b22930eSAndy Lutomirski	 * before IRET.
1544905a36a2SIngo Molnar	 */
1545905a36a2SIngo Molnar	cmpl	$1, -8(%rsp)
1546905a36a2SIngo Molnar	je	nested_nmi
1547905a36a2SIngo Molnar
1548905a36a2SIngo Molnar	/*
15490b22930eSAndy Lutomirski	 * Now test if the previous stack was an NMI stack.  This covers
15500b22930eSAndy Lutomirski	 * the case where we interrupt an outer NMI after it clears
1551810bc075SAndy Lutomirski	 * "NMI executing" but before IRET.  We need to be careful, though:
1552810bc075SAndy Lutomirski	 * there is one case in which RSP could point to the NMI stack
1553810bc075SAndy Lutomirski	 * despite there being no NMI active: naughty userspace controls
1554810bc075SAndy Lutomirski	 * RSP at the very beginning of the SYSCALL targets.  We can
1555810bc075SAndy Lutomirski	 * pull a fast one on naughty userspace, though: we program
1556810bc075SAndy Lutomirski	 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1557810bc075SAndy Lutomirski	 * if it controls the kernel's RSP.  We set DF before we clear
1558810bc075SAndy Lutomirski	 * "NMI executing".
1559905a36a2SIngo Molnar	 */
1560905a36a2SIngo Molnar	lea	6*8(%rsp), %rdx
1561905a36a2SIngo Molnar	/* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1562905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1563905a36a2SIngo Molnar	/* If the stack pointer is above the NMI stack, this is a normal NMI */
1564905a36a2SIngo Molnar	ja	first_nmi
15654d732138SIngo Molnar
1566905a36a2SIngo Molnar	subq	$EXCEPTION_STKSZ, %rdx
1567905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1568905a36a2SIngo Molnar	/* If it is below the NMI stack, it is a normal NMI */
1569905a36a2SIngo Molnar	jb	first_nmi
1570810bc075SAndy Lutomirski
1571810bc075SAndy Lutomirski	/* Ah, it is within the NMI stack. */
1572810bc075SAndy Lutomirski
1573810bc075SAndy Lutomirski	testb	$(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1574810bc075SAndy Lutomirski	jz	first_nmi	/* RSP was user controlled. */
1575810bc075SAndy Lutomirski
1576810bc075SAndy Lutomirski	/* This is a nested NMI. */
1577905a36a2SIngo Molnar
1578905a36a2SIngo Molnarnested_nmi:
1579905a36a2SIngo Molnar	/*
15800b22930eSAndy Lutomirski	 * Modify the "iret" frame to point to repeat_nmi, forcing another
15810b22930eSAndy Lutomirski	 * iteration of NMI handling.
1582905a36a2SIngo Molnar	 */
158323a781e9SAndy Lutomirski	subq	$8, %rsp
1584905a36a2SIngo Molnar	leaq	-10*8(%rsp), %rdx
1585905a36a2SIngo Molnar	pushq	$__KERNEL_DS
1586905a36a2SIngo Molnar	pushq	%rdx
1587905a36a2SIngo Molnar	pushfq
1588905a36a2SIngo Molnar	pushq	$__KERNEL_CS
1589905a36a2SIngo Molnar	pushq	$repeat_nmi
1590905a36a2SIngo Molnar
1591905a36a2SIngo Molnar	/* Put stack back */
1592905a36a2SIngo Molnar	addq	$(6*8), %rsp
1593905a36a2SIngo Molnar
1594905a36a2SIngo Molnarnested_nmi_out:
1595905a36a2SIngo Molnar	popq	%rdx
1596905a36a2SIngo Molnar
15970b22930eSAndy Lutomirski	/* We are returning to kernel mode, so this cannot result in a fault. */
1598929bacecSAndy Lutomirski	iretq
1599905a36a2SIngo Molnar
1600905a36a2SIngo Molnarfirst_nmi:
16010b22930eSAndy Lutomirski	/* Restore rdx. */
1602905a36a2SIngo Molnar	movq	(%rsp), %rdx
1603905a36a2SIngo Molnar
160436f1a77bSAndy Lutomirski	/* Make room for "NMI executing". */
160536f1a77bSAndy Lutomirski	pushq	$0
1606905a36a2SIngo Molnar
16070b22930eSAndy Lutomirski	/* Leave room for the "iret" frame */
1608905a36a2SIngo Molnar	subq	$(5*8), %rsp
1609905a36a2SIngo Molnar
16100b22930eSAndy Lutomirski	/* Copy the "original" frame to the "outermost" frame */
1611905a36a2SIngo Molnar	.rept 5
1612905a36a2SIngo Molnar	pushq	11*8(%rsp)
1613905a36a2SIngo Molnar	.endr
16148c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1615905a36a2SIngo Molnar
1616905a36a2SIngo Molnar	/* Everything up to here is safe from nested NMIs */
1617905a36a2SIngo Molnar
1618a97439aaSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
1619a97439aaSAndy Lutomirski	/*
1620a97439aaSAndy Lutomirski	 * For ease of testing, unmask NMIs right away.  Disabled by
1621a97439aaSAndy Lutomirski	 * default because IRET is very expensive.
1622a97439aaSAndy Lutomirski	 */
1623a97439aaSAndy Lutomirski	pushq	$0		/* SS */
1624a97439aaSAndy Lutomirski	pushq	%rsp		/* RSP (minus 8 because of the previous push) */
1625a97439aaSAndy Lutomirski	addq	$8, (%rsp)	/* Fix up RSP */
1626a97439aaSAndy Lutomirski	pushfq			/* RFLAGS */
1627a97439aaSAndy Lutomirski	pushq	$__KERNEL_CS	/* CS */
1628a97439aaSAndy Lutomirski	pushq	$1f		/* RIP */
1629929bacecSAndy Lutomirski	iretq			/* continues at repeat_nmi below */
16308c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1631a97439aaSAndy Lutomirski1:
1632a97439aaSAndy Lutomirski#endif
1633a97439aaSAndy Lutomirski
16340b22930eSAndy Lutomirskirepeat_nmi:
1635905a36a2SIngo Molnar	/*
1636905a36a2SIngo Molnar	 * If there was a nested NMI, the first NMI's iret will return
1637905a36a2SIngo Molnar	 * here. But NMIs are still enabled and we can take another
1638905a36a2SIngo Molnar	 * nested NMI. The nested NMI checks the interrupted RIP to see
1639905a36a2SIngo Molnar	 * if it is between repeat_nmi and end_repeat_nmi, and if so
1640905a36a2SIngo Molnar	 * it will just return, as we are about to repeat an NMI anyway.
1641905a36a2SIngo Molnar	 * This makes it safe to copy to the stack frame that a nested
1642905a36a2SIngo Molnar	 * NMI will update.
16430b22930eSAndy Lutomirski	 *
16440b22930eSAndy Lutomirski	 * RSP is pointing to "outermost RIP".  gsbase is unknown, but, if
16450b22930eSAndy Lutomirski	 * we're repeating an NMI, gsbase has the same value that it had on
16460b22930eSAndy Lutomirski	 * the first iteration.  paranoid_entry will load the kernel
164736f1a77bSAndy Lutomirski	 * gsbase if needed before we call do_nmi.  "NMI executing"
164836f1a77bSAndy Lutomirski	 * is zero.
1649905a36a2SIngo Molnar	 */
165036f1a77bSAndy Lutomirski	movq	$1, 10*8(%rsp)		/* Set "NMI executing". */
1651905a36a2SIngo Molnar
16520b22930eSAndy Lutomirski	/*
16530b22930eSAndy Lutomirski	 * Copy the "outermost" frame to the "iret" frame.  NMIs that nest
16540b22930eSAndy Lutomirski	 * here must not modify the "iret" frame while we're writing to
16550b22930eSAndy Lutomirski	 * it or it will end up containing garbage.
16560b22930eSAndy Lutomirski	 */
1657905a36a2SIngo Molnar	addq	$(10*8), %rsp
1658905a36a2SIngo Molnar	.rept 5
1659905a36a2SIngo Molnar	pushq	-6*8(%rsp)
1660905a36a2SIngo Molnar	.endr
1661905a36a2SIngo Molnar	subq	$(5*8), %rsp
1662905a36a2SIngo Molnarend_repeat_nmi:
1663905a36a2SIngo Molnar
1664905a36a2SIngo Molnar	/*
16650b22930eSAndy Lutomirski	 * Everything below this point can be preempted by a nested NMI.
16660b22930eSAndy Lutomirski	 * If this happens, then the inner NMI will change the "iret"
16670b22930eSAndy Lutomirski	 * frame to point back to repeat_nmi.
1668905a36a2SIngo Molnar	 */
1669905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
1670905a36a2SIngo Molnar
1671905a36a2SIngo Molnar	/*
1672905a36a2SIngo Molnar	 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1673905a36a2SIngo Molnar	 * as we should not be calling schedule in NMI context.
1674905a36a2SIngo Molnar	 * Even with normal interrupts enabled. An NMI should not be
1675905a36a2SIngo Molnar	 * setting NEED_RESCHED or anything that normal interrupts and
1676905a36a2SIngo Molnar	 * exceptions might do.
1677905a36a2SIngo Molnar	 */
1678905a36a2SIngo Molnar	call	paranoid_entry
16798c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
1680905a36a2SIngo Molnar
1681905a36a2SIngo Molnar	/* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1682905a36a2SIngo Molnar	movq	%rsp, %rdi
1683905a36a2SIngo Molnar	movq	$-1, %rsi
1684905a36a2SIngo Molnar	call	do_nmi
1685905a36a2SIngo Molnar
168616561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
168721e94459SPeter Zijlstra	RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
16888a09317bSDave Hansen
1689905a36a2SIngo Molnar	testl	%ebx, %ebx			/* swapgs needed? */
1690905a36a2SIngo Molnar	jnz	nmi_restore
1691905a36a2SIngo Molnarnmi_swapgs:
1692905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
1693905a36a2SIngo Molnarnmi_restore:
1694502af0d7SDominik Brodowski	POP_REGS
16950b22930eSAndy Lutomirski
1696471ee483SAndy Lutomirski	/*
1697471ee483SAndy Lutomirski	 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1698471ee483SAndy Lutomirski	 * at the "iret" frame.
1699471ee483SAndy Lutomirski	 */
1700471ee483SAndy Lutomirski	addq	$6*8, %rsp
1701905a36a2SIngo Molnar
1702810bc075SAndy Lutomirski	/*
1703810bc075SAndy Lutomirski	 * Clear "NMI executing".  Set DF first so that we can easily
1704810bc075SAndy Lutomirski	 * distinguish the remaining code between here and IRET from
1705929bacecSAndy Lutomirski	 * the SYSCALL entry and exit paths.
1706929bacecSAndy Lutomirski	 *
1707929bacecSAndy Lutomirski	 * We arguably should just inspect RIP instead, but I (Andy) wrote
1708929bacecSAndy Lutomirski	 * this code when I had the misapprehension that Xen PV supported
1709929bacecSAndy Lutomirski	 * NMIs, and Xen PV would break that approach.
1710810bc075SAndy Lutomirski	 */
1711810bc075SAndy Lutomirski	std
1712810bc075SAndy Lutomirski	movq	$0, 5*8(%rsp)		/* clear "NMI executing" */
17130b22930eSAndy Lutomirski
17140b22930eSAndy Lutomirski	/*
1715929bacecSAndy Lutomirski	 * iretq reads the "iret" frame and exits the NMI stack in a
1716929bacecSAndy Lutomirski	 * single instruction.  We are returning to kernel mode, so this
1717929bacecSAndy Lutomirski	 * cannot result in a fault.  Similarly, we don't need to worry
1718929bacecSAndy Lutomirski	 * about espfix64 on the way back to kernel mode.
17190b22930eSAndy Lutomirski	 */
1720929bacecSAndy Lutomirski	iretq
1721bc7b11c0SJiri SlabySYM_CODE_END(nmi)
1722905a36a2SIngo Molnar
1723dffb3f9dSAndy Lutomirski#ifndef CONFIG_IA32_EMULATION
1724dffb3f9dSAndy Lutomirski/*
1725dffb3f9dSAndy Lutomirski * This handles SYSCALL from 32-bit code.  There is no way to program
1726dffb3f9dSAndy Lutomirski * MSRs to fully disable 32-bit SYSCALL.
1727dffb3f9dSAndy Lutomirski */
1728bc7b11c0SJiri SlabySYM_CODE_START(ignore_sysret)
17298c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
1730905a36a2SIngo Molnar	mov	$-ENOSYS, %eax
1731*b2b1d94cSJan Beulich	sysretl
1732bc7b11c0SJiri SlabySYM_CODE_END(ignore_sysret)
1733dffb3f9dSAndy Lutomirski#endif
17342deb4be2SAndy Lutomirski
1735bc7b11c0SJiri SlabySYM_CODE_START(rewind_stack_do_exit)
17368c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
17372deb4be2SAndy Lutomirski	/* Prevent any naive code from trying to unwind to our caller. */
17382deb4be2SAndy Lutomirski	xorl	%ebp, %ebp
17392deb4be2SAndy Lutomirski
17402deb4be2SAndy Lutomirski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rax
17418c1f7558SJosh Poimboeuf	leaq	-PTREGS_SIZE(%rax), %rsp
17428c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC sp_offset=PTREGS_SIZE
17432deb4be2SAndy Lutomirski
17442deb4be2SAndy Lutomirski	call	do_exit
1745bc7b11c0SJiri SlabySYM_CODE_END(rewind_stack_do_exit)
1746