xref: /openbmc/linux/arch/x86/entry/entry_64.S (revision 410367e321b5cbd4a616161142a7d162cf55885e)
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
1701e423bffSAndy Lutomirski	/* IRQs are off. */
171dfe64506SLinus Torvalds	movq	%rax, %rdi
172dfe64506SLinus Torvalds	movq	%rsp, %rsi
1731e423bffSAndy Lutomirski	call	do_syscall_64		/* returns with IRQs disabled */
1741e423bffSAndy Lutomirski
175905a36a2SIngo Molnar	/*
176905a36a2SIngo Molnar	 * Try to use SYSRET instead of IRET if we're returning to
1778a055d7fSAndy Lutomirski	 * a completely clean 64-bit userspace context.  If we're not,
1788a055d7fSAndy Lutomirski	 * go to the slow exit path.
179905a36a2SIngo Molnar	 */
180905a36a2SIngo Molnar	movq	RCX(%rsp), %rcx
181905a36a2SIngo Molnar	movq	RIP(%rsp), %r11
1828a055d7fSAndy Lutomirski
1838a055d7fSAndy Lutomirski	cmpq	%rcx, %r11	/* SYSRET requires RCX == RIP */
1848a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
185905a36a2SIngo Molnar
186905a36a2SIngo Molnar	/*
187905a36a2SIngo Molnar	 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
188905a36a2SIngo Molnar	 * in kernel space.  This essentially lets the user take over
189905a36a2SIngo Molnar	 * the kernel, since userspace controls RSP.
190905a36a2SIngo Molnar	 *
191905a36a2SIngo Molnar	 * If width of "canonical tail" ever becomes variable, this will need
192905a36a2SIngo Molnar	 * to be updated to remain correct on both old and new CPUs.
193361b4b58SKirill A. Shutemov	 *
194cbe0317bSKirill A. Shutemov	 * Change top bits to match most significant bit (47th or 56th bit
195cbe0317bSKirill A. Shutemov	 * depending on paging mode) in the address.
196905a36a2SIngo Molnar	 */
19709e61a77SKirill A. Shutemov#ifdef CONFIG_X86_5LEVEL
19839b95522SKirill A. Shutemov	ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \
19939b95522SKirill A. Shutemov		"shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57
20009e61a77SKirill A. Shutemov#else
201905a36a2SIngo Molnar	shl	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
202905a36a2SIngo Molnar	sar	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
20309e61a77SKirill A. Shutemov#endif
2044d732138SIngo Molnar
205905a36a2SIngo Molnar	/* If this changed %rcx, it was not canonical */
206905a36a2SIngo Molnar	cmpq	%rcx, %r11
2078a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
208905a36a2SIngo Molnar
209905a36a2SIngo Molnar	cmpq	$__USER_CS, CS(%rsp)		/* CS must match SYSRET */
2108a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
211905a36a2SIngo Molnar
212905a36a2SIngo Molnar	movq	R11(%rsp), %r11
213905a36a2SIngo Molnar	cmpq	%r11, EFLAGS(%rsp)		/* R11 == RFLAGS */
2148a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
215905a36a2SIngo Molnar
216905a36a2SIngo Molnar	/*
2173e035305SBorislav Petkov	 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
2183e035305SBorislav Petkov	 * restore RF properly. If the slowpath sets it for whatever reason, we
2193e035305SBorislav Petkov	 * need to restore it correctly.
2203e035305SBorislav Petkov	 *
2213e035305SBorislav Petkov	 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
2223e035305SBorislav Petkov	 * trap from userspace immediately after SYSRET.  This would cause an
2233e035305SBorislav Petkov	 * infinite loop whenever #DB happens with register state that satisfies
2243e035305SBorislav Petkov	 * the opportunistic SYSRET conditions.  For example, single-stepping
2253e035305SBorislav Petkov	 * this user code:
226905a36a2SIngo Molnar	 *
227905a36a2SIngo Molnar	 *           movq	$stuck_here, %rcx
228905a36a2SIngo Molnar	 *           pushfq
229905a36a2SIngo Molnar	 *           popq %r11
230905a36a2SIngo Molnar	 *   stuck_here:
231905a36a2SIngo Molnar	 *
232905a36a2SIngo Molnar	 * would never get past 'stuck_here'.
233905a36a2SIngo Molnar	 */
234905a36a2SIngo Molnar	testq	$(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
2358a055d7fSAndy Lutomirski	jnz	swapgs_restore_regs_and_return_to_usermode
236905a36a2SIngo Molnar
237905a36a2SIngo Molnar	/* nothing to check for RSP */
238905a36a2SIngo Molnar
239905a36a2SIngo Molnar	cmpq	$__USER_DS, SS(%rsp)		/* SS must match SYSRET */
2408a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
241905a36a2SIngo Molnar
242905a36a2SIngo Molnar	/*
243905a36a2SIngo Molnar	 * We win! This label is here just for ease of understanding
244905a36a2SIngo Molnar	 * perf profiles. Nothing jumps here.
245905a36a2SIngo Molnar	 */
246905a36a2SIngo Molnarsyscall_return_via_sysret:
247905a36a2SIngo Molnar	/* rcx and r11 are already restored (see code above) */
248502af0d7SDominik Brodowski	POP_REGS pop_rdi=0 skip_r11rcx=1
2493e3b9293SAndy Lutomirski
2503e3b9293SAndy Lutomirski	/*
2513e3b9293SAndy Lutomirski	 * Now all regs are restored except RSP and RDI.
2523e3b9293SAndy Lutomirski	 * Save old stack pointer and switch to trampoline stack.
2533e3b9293SAndy Lutomirski	 */
2543e3b9293SAndy Lutomirski	movq	%rsp, %rdi
255c482feefSAndy Lutomirski	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
2561fb14363SJosh Poimboeuf	UNWIND_HINT_EMPTY
2573e3b9293SAndy Lutomirski
2583e3b9293SAndy Lutomirski	pushq	RSP-RDI(%rdi)	/* RSP */
2593e3b9293SAndy Lutomirski	pushq	(%rdi)		/* RDI */
2603e3b9293SAndy Lutomirski
2613e3b9293SAndy Lutomirski	/*
2623e3b9293SAndy Lutomirski	 * We are on the trampoline stack.  All regs except RDI are live.
2633e3b9293SAndy Lutomirski	 * We can do future final exit work right here.
2643e3b9293SAndy Lutomirski	 */
265afaef01cSAlexander Popov	STACKLEAK_ERASE_NOCLOBBER
266afaef01cSAlexander Popov
2676fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
2683e3b9293SAndy Lutomirski
2694fbb3910SAndy Lutomirski	popq	%rdi
2703e3b9293SAndy Lutomirski	popq	%rsp
271905a36a2SIngo Molnar	USERGS_SYSRET64
272bc7b11c0SJiri SlabySYM_CODE_END(entry_SYSCALL_64)
273905a36a2SIngo Molnar
274905a36a2SIngo Molnar/*
2750100301bSBrian Gerst * %rdi: prev task
2760100301bSBrian Gerst * %rsi: next task
2770100301bSBrian Gerst */
278b9f6976bSThomas Gleixner.pushsection .text, "ax"
27996c64806SJosh PoimboeufSYM_FUNC_START(__switch_to_asm)
2800100301bSBrian Gerst	/*
2810100301bSBrian Gerst	 * Save callee-saved registers
2820100301bSBrian Gerst	 * This must match the order in inactive_task_frame
2830100301bSBrian Gerst	 */
2840100301bSBrian Gerst	pushq	%rbp
2850100301bSBrian Gerst	pushq	%rbx
2860100301bSBrian Gerst	pushq	%r12
2870100301bSBrian Gerst	pushq	%r13
2880100301bSBrian Gerst	pushq	%r14
2890100301bSBrian Gerst	pushq	%r15
2900100301bSBrian Gerst
2910100301bSBrian Gerst	/* switch stack */
2920100301bSBrian Gerst	movq	%rsp, TASK_threadsp(%rdi)
2930100301bSBrian Gerst	movq	TASK_threadsp(%rsi), %rsp
2940100301bSBrian Gerst
295050e9baaSLinus Torvalds#ifdef CONFIG_STACKPROTECTOR
2960100301bSBrian Gerst	movq	TASK_stack_canary(%rsi), %rbx
297e6401c13SAndy Lutomirski	movq	%rbx, PER_CPU_VAR(fixed_percpu_data) + stack_canary_offset
2980100301bSBrian Gerst#endif
2990100301bSBrian Gerst
300c995efd5SDavid Woodhouse#ifdef CONFIG_RETPOLINE
301c995efd5SDavid Woodhouse	/*
302c995efd5SDavid Woodhouse	 * When switching from a shallower to a deeper call stack
303c995efd5SDavid Woodhouse	 * the RSB may either underflow or use entries populated
304c995efd5SDavid Woodhouse	 * with userspace addresses. On CPUs where those concerns
305c995efd5SDavid Woodhouse	 * exist, overwrite the RSB with entries which capture
306c995efd5SDavid Woodhouse	 * speculative execution to prevent attack.
307c995efd5SDavid Woodhouse	 */
308d1c99108SDavid Woodhouse	FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
309c995efd5SDavid Woodhouse#endif
310c995efd5SDavid Woodhouse
3110100301bSBrian Gerst	/* restore callee-saved registers */
3120100301bSBrian Gerst	popq	%r15
3130100301bSBrian Gerst	popq	%r14
3140100301bSBrian Gerst	popq	%r13
3150100301bSBrian Gerst	popq	%r12
3160100301bSBrian Gerst	popq	%rbx
3170100301bSBrian Gerst	popq	%rbp
3180100301bSBrian Gerst
3190100301bSBrian Gerst	jmp	__switch_to
32096c64806SJosh PoimboeufSYM_FUNC_END(__switch_to_asm)
321b9f6976bSThomas Gleixner.popsection
3220100301bSBrian Gerst
3230100301bSBrian Gerst/*
324905a36a2SIngo Molnar * A newly forked process directly context switches into this address.
325905a36a2SIngo Molnar *
3260100301bSBrian Gerst * rax: prev task we switched from
327616d2483SBrian Gerst * rbx: kernel thread func (NULL for user thread)
328616d2483SBrian Gerst * r12: kernel thread arg
329905a36a2SIngo Molnar */
330b9f6976bSThomas Gleixner.pushsection .text, "ax"
331bc7b11c0SJiri SlabySYM_CODE_START(ret_from_fork)
3328c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
3330100301bSBrian Gerst	movq	%rax, %rdi
3344d732138SIngo Molnar	call	schedule_tail			/* rdi: 'prev' task parameter */
335905a36a2SIngo Molnar
336616d2483SBrian Gerst	testq	%rbx, %rbx			/* from kernel_thread? */
337616d2483SBrian Gerst	jnz	1f				/* kernel threads are uncommon */
338905a36a2SIngo Molnar
339616d2483SBrian Gerst2:
3408c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
341ebd57499SJosh Poimboeuf	movq	%rsp, %rdi
34224d978b7SAndy Lutomirski	call	syscall_return_slowpath	/* returns with IRQs disabled */
3438a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
344616d2483SBrian Gerst
345616d2483SBrian Gerst1:
346616d2483SBrian Gerst	/* kernel thread */
347d31a5802SJosh Poimboeuf	UNWIND_HINT_EMPTY
348616d2483SBrian Gerst	movq	%r12, %rdi
34934fdce69SPeter Zijlstra	CALL_NOSPEC rbx
350616d2483SBrian Gerst	/*
351616d2483SBrian Gerst	 * A kernel thread is allowed to return here after successfully
352616d2483SBrian Gerst	 * calling do_execve().  Exit to userspace to complete the execve()
353616d2483SBrian Gerst	 * syscall.
354616d2483SBrian Gerst	 */
355616d2483SBrian Gerst	movq	$0, RAX(%rsp)
356616d2483SBrian Gerst	jmp	2b
357bc7b11c0SJiri SlabySYM_CODE_END(ret_from_fork)
358b9f6976bSThomas Gleixner.popsection
359905a36a2SIngo Molnar
360905a36a2SIngo Molnar/*
361905a36a2SIngo Molnar * Build the entry stubs with some assembler magic.
362905a36a2SIngo Molnar * We pack 1 stub into every 8-byte block.
363905a36a2SIngo Molnar */
364905a36a2SIngo Molnar	.align 8
365bc7b11c0SJiri SlabySYM_CODE_START(irq_entries_start)
366905a36a2SIngo Molnar    vector=FIRST_EXTERNAL_VECTOR
367905a36a2SIngo Molnar    .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
3688c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
369905a36a2SIngo Molnar	pushq	$(~vector+0x80)			/* Note: always in signed byte range */
370905a36a2SIngo Molnar	jmp	common_interrupt
371905a36a2SIngo Molnar	.align	8
3728c1f7558SJosh Poimboeuf	vector=vector+1
373905a36a2SIngo Molnar    .endr
374bc7b11c0SJiri SlabySYM_CODE_END(irq_entries_start)
375905a36a2SIngo Molnar
376f8a8fe61SThomas Gleixner	.align 8
377bc7b11c0SJiri SlabySYM_CODE_START(spurious_entries_start)
378f8a8fe61SThomas Gleixner    vector=FIRST_SYSTEM_VECTOR
379f8a8fe61SThomas Gleixner    .rept (NR_VECTORS - FIRST_SYSTEM_VECTOR)
380f8a8fe61SThomas Gleixner	UNWIND_HINT_IRET_REGS
381f8a8fe61SThomas Gleixner	pushq	$(~vector+0x80)			/* Note: always in signed byte range */
382f8a8fe61SThomas Gleixner	jmp	common_spurious
383f8a8fe61SThomas Gleixner	.align	8
384f8a8fe61SThomas Gleixner	vector=vector+1
385f8a8fe61SThomas Gleixner    .endr
386bc7b11c0SJiri SlabySYM_CODE_END(spurious_entries_start)
387f8a8fe61SThomas Gleixner
3881d3e53e8SAndy Lutomirski.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
3891d3e53e8SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
390e17f8234SBoris Ostrovsky	pushq %rax
391e17f8234SBoris Ostrovsky	SAVE_FLAGS(CLBR_RAX)
392e17f8234SBoris Ostrovsky	testl $X86_EFLAGS_IF, %eax
3931d3e53e8SAndy Lutomirski	jz .Lokay_\@
3941d3e53e8SAndy Lutomirski	ud2
3951d3e53e8SAndy Lutomirski.Lokay_\@:
396e17f8234SBoris Ostrovsky	popq %rax
3971d3e53e8SAndy Lutomirski#endif
3981d3e53e8SAndy Lutomirski.endm
3991d3e53e8SAndy Lutomirski
4001d3e53e8SAndy Lutomirski/*
4011d3e53e8SAndy Lutomirski * Enters the IRQ stack if we're not already using it.  NMI-safe.  Clobbers
4021d3e53e8SAndy Lutomirski * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
4031d3e53e8SAndy Lutomirski * Requires kernel GSBASE.
4041d3e53e8SAndy Lutomirski *
4051d3e53e8SAndy Lutomirski * The invariant is that, if irq_count != -1, then the IRQ stack is in use.
4061d3e53e8SAndy Lutomirski */
4072ba64741SDominik Brodowski.macro ENTER_IRQ_STACK regs=1 old_rsp save_ret=0
4081d3e53e8SAndy Lutomirski	DEBUG_ENTRY_ASSERT_IRQS_OFF
4092ba64741SDominik Brodowski
4102ba64741SDominik Brodowski	.if \save_ret
4112ba64741SDominik Brodowski	/*
4122ba64741SDominik Brodowski	 * If save_ret is set, the original stack contains one additional
4132ba64741SDominik Brodowski	 * entry -- the return address. Therefore, move the address one
4142ba64741SDominik Brodowski	 * entry below %rsp to \old_rsp.
4152ba64741SDominik Brodowski	 */
4162ba64741SDominik Brodowski	leaq	8(%rsp), \old_rsp
4172ba64741SDominik Brodowski	.else
4181d3e53e8SAndy Lutomirski	movq	%rsp, \old_rsp
4192ba64741SDominik Brodowski	.endif
4208c1f7558SJosh Poimboeuf
4218c1f7558SJosh Poimboeuf	.if \regs
4228c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS base=\old_rsp
4238c1f7558SJosh Poimboeuf	.endif
4248c1f7558SJosh Poimboeuf
4251d3e53e8SAndy Lutomirski	incl	PER_CPU_VAR(irq_count)
42629955909SAndy Lutomirski	jnz	.Lirq_stack_push_old_rsp_\@
4271d3e53e8SAndy Lutomirski
4281d3e53e8SAndy Lutomirski	/*
4291d3e53e8SAndy Lutomirski	 * Right now, if we just incremented irq_count to zero, we've
4301d3e53e8SAndy Lutomirski	 * claimed the IRQ stack but we haven't switched to it yet.
4311d3e53e8SAndy Lutomirski	 *
4321d3e53e8SAndy Lutomirski	 * If anything is added that can interrupt us here without using IST,
4331d3e53e8SAndy Lutomirski	 * it must be *extremely* careful to limit its stack usage.  This
4341d3e53e8SAndy Lutomirski	 * could include kprobes and a hypothetical future IST-less #DB
4351d3e53e8SAndy Lutomirski	 * handler.
43629955909SAndy Lutomirski	 *
43729955909SAndy Lutomirski	 * The OOPS unwinder relies on the word at the top of the IRQ
43829955909SAndy Lutomirski	 * stack linking back to the previous RSP for the entire time we're
43929955909SAndy Lutomirski	 * on the IRQ stack.  For this to work reliably, we need to write
44029955909SAndy Lutomirski	 * it before we actually move ourselves to the IRQ stack.
4411d3e53e8SAndy Lutomirski	 */
4421d3e53e8SAndy Lutomirski
443e6401c13SAndy Lutomirski	movq	\old_rsp, PER_CPU_VAR(irq_stack_backing_store + IRQ_STACK_SIZE - 8)
444758a2e31SThomas Gleixner	movq	PER_CPU_VAR(hardirq_stack_ptr), %rsp
44529955909SAndy Lutomirski
44629955909SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
44729955909SAndy Lutomirski	/*
44829955909SAndy Lutomirski	 * If the first movq above becomes wrong due to IRQ stack layout
44929955909SAndy Lutomirski	 * changes, the only way we'll notice is if we try to unwind right
45029955909SAndy Lutomirski	 * here.  Assert that we set up the stack right to catch this type
45129955909SAndy Lutomirski	 * of bug quickly.
45229955909SAndy Lutomirski	 */
45329955909SAndy Lutomirski	cmpq	-8(%rsp), \old_rsp
45429955909SAndy Lutomirski	je	.Lirq_stack_okay\@
45529955909SAndy Lutomirski	ud2
45629955909SAndy Lutomirski	.Lirq_stack_okay\@:
45729955909SAndy Lutomirski#endif
45829955909SAndy Lutomirski
45929955909SAndy Lutomirski.Lirq_stack_push_old_rsp_\@:
4601d3e53e8SAndy Lutomirski	pushq	\old_rsp
4618c1f7558SJosh Poimboeuf
4628c1f7558SJosh Poimboeuf	.if \regs
4638c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS indirect=1
4648c1f7558SJosh Poimboeuf	.endif
4652ba64741SDominik Brodowski
4662ba64741SDominik Brodowski	.if \save_ret
4672ba64741SDominik Brodowski	/*
4682ba64741SDominik Brodowski	 * Push the return address to the stack. This return address can
4692ba64741SDominik Brodowski	 * be found at the "real" original RSP, which was offset by 8 at
4702ba64741SDominik Brodowski	 * the beginning of this macro.
4712ba64741SDominik Brodowski	 */
4722ba64741SDominik Brodowski	pushq	-8(\old_rsp)
4732ba64741SDominik Brodowski	.endif
4741d3e53e8SAndy Lutomirski.endm
4751d3e53e8SAndy Lutomirski
4761d3e53e8SAndy Lutomirski/*
4771d3e53e8SAndy Lutomirski * Undoes ENTER_IRQ_STACK.
4781d3e53e8SAndy Lutomirski */
4798c1f7558SJosh Poimboeuf.macro LEAVE_IRQ_STACK regs=1
4801d3e53e8SAndy Lutomirski	DEBUG_ENTRY_ASSERT_IRQS_OFF
4811d3e53e8SAndy Lutomirski	/* We need to be off the IRQ stack before decrementing irq_count. */
4821d3e53e8SAndy Lutomirski	popq	%rsp
4831d3e53e8SAndy Lutomirski
4848c1f7558SJosh Poimboeuf	.if \regs
4858c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
4868c1f7558SJosh Poimboeuf	.endif
4878c1f7558SJosh Poimboeuf
4881d3e53e8SAndy Lutomirski	/*
4891d3e53e8SAndy Lutomirski	 * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming
4901d3e53e8SAndy Lutomirski	 * the irq stack but we're not on it.
4911d3e53e8SAndy Lutomirski	 */
4921d3e53e8SAndy Lutomirski
4931d3e53e8SAndy Lutomirski	decl	PER_CPU_VAR(irq_count)
4941d3e53e8SAndy Lutomirski.endm
4951d3e53e8SAndy Lutomirski
496905a36a2SIngo Molnar/*
497f3d415eaSDominik Brodowski * Interrupt entry helper function.
498905a36a2SIngo Molnar *
499f3d415eaSDominik Brodowski * Entry runs with interrupts off. Stack layout at entry:
500f3d415eaSDominik Brodowski * +----------------------------------------------------+
501f3d415eaSDominik Brodowski * | regs->ss						|
502f3d415eaSDominik Brodowski * | regs->rsp						|
503f3d415eaSDominik Brodowski * | regs->eflags					|
504f3d415eaSDominik Brodowski * | regs->cs						|
505f3d415eaSDominik Brodowski * | regs->ip						|
506f3d415eaSDominik Brodowski * +----------------------------------------------------+
507f3d415eaSDominik Brodowski * | regs->orig_ax = ~(interrupt number)		|
508f3d415eaSDominik Brodowski * +----------------------------------------------------+
509f3d415eaSDominik Brodowski * | return address					|
510f3d415eaSDominik Brodowski * +----------------------------------------------------+
511905a36a2SIngo Molnar */
512bc7b11c0SJiri SlabySYM_CODE_START(interrupt_entry)
51381b67439SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=16
514f3d415eaSDominik Brodowski	ASM_CLAC
515905a36a2SIngo Molnar	cld
5167f2590a1SAndy Lutomirski
517f3d415eaSDominik Brodowski	testb	$3, CS-ORIG_RAX+8(%rsp)
5187f2590a1SAndy Lutomirski	jz	1f
5197f2590a1SAndy Lutomirski	SWAPGS
52018ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
521f3d415eaSDominik Brodowski	/*
522f3d415eaSDominik Brodowski	 * Switch to the thread stack. The IRET frame and orig_ax are
523f3d415eaSDominik Brodowski	 * on the stack, as well as the return address. RDI..R12 are
524f3d415eaSDominik Brodowski	 * not (yet) on the stack and space has not (yet) been
525f3d415eaSDominik Brodowski	 * allocated for them.
526f3d415eaSDominik Brodowski	 */
52790a6acc4SDominik Brodowski	pushq	%rdi
528f3d415eaSDominik Brodowski
52990a6acc4SDominik Brodowski	/* Need to switch before accessing the thread stack. */
53090a6acc4SDominik Brodowski	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi
53190a6acc4SDominik Brodowski	movq	%rsp, %rdi
53290a6acc4SDominik Brodowski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
533f3d415eaSDominik Brodowski
534f3d415eaSDominik Brodowski	 /*
535f3d415eaSDominik Brodowski	  * We have RDI, return address, and orig_ax on the stack on
536f3d415eaSDominik Brodowski	  * top of the IRET frame. That means offset=24
537f3d415eaSDominik Brodowski	  */
538f3d415eaSDominik Brodowski	UNWIND_HINT_IRET_REGS base=%rdi offset=24
53990a6acc4SDominik Brodowski
54090a6acc4SDominik Brodowski	pushq	7*8(%rdi)		/* regs->ss */
54190a6acc4SDominik Brodowski	pushq	6*8(%rdi)		/* regs->rsp */
54290a6acc4SDominik Brodowski	pushq	5*8(%rdi)		/* regs->eflags */
54390a6acc4SDominik Brodowski	pushq	4*8(%rdi)		/* regs->cs */
54490a6acc4SDominik Brodowski	pushq	3*8(%rdi)		/* regs->ip */
54581b67439SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
54690a6acc4SDominik Brodowski	pushq	2*8(%rdi)		/* regs->orig_ax */
54790a6acc4SDominik Brodowski	pushq	8(%rdi)			/* return address */
54890a6acc4SDominik Brodowski
54990a6acc4SDominik Brodowski	movq	(%rdi), %rdi
55064dbc122SJosh Poimboeuf	jmp	2f
5517f2590a1SAndy Lutomirski1:
55218ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
55318ec54fdSJosh Poimboeuf2:
5540e34d226SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
5550e34d226SDominik Brodowski	ENCODE_FRAME_POINTER 8
556905a36a2SIngo Molnar
5572ba64741SDominik Brodowski	testb	$3, CS+8(%rsp)
558905a36a2SIngo Molnar	jz	1f
55902bc7768SAndy Lutomirski
56002bc7768SAndy Lutomirski	/*
5617f2590a1SAndy Lutomirski	 * IRQ from user mode.
5627f2590a1SAndy Lutomirski	 *
563f1075053SAndy Lutomirski	 * We need to tell lockdep that IRQs are off.  We can't do this until
564f1075053SAndy Lutomirski	 * we fix gsbase, and we should do it before enter_from_user_mode
565f3d415eaSDominik Brodowski	 * (which can take locks).  Since TRACE_IRQS_OFF is idempotent,
566f1075053SAndy Lutomirski	 * the simplest way to handle it is to just call it twice if
567f1075053SAndy Lutomirski	 * we enter from user mode.  There's no reason to optimize this since
568f1075053SAndy Lutomirski	 * TRACE_IRQS_OFF is a no-op if lockdep is off.
569f1075053SAndy Lutomirski	 */
570f1075053SAndy Lutomirski	TRACE_IRQS_OFF
571f1075053SAndy Lutomirski
572478dc89cSAndy Lutomirski	CALL_enter_from_user_mode
57302bc7768SAndy Lutomirski
574905a36a2SIngo Molnar1:
5752ba64741SDominik Brodowski	ENTER_IRQ_STACK old_rsp=%rdi save_ret=1
576905a36a2SIngo Molnar	/* We entered an interrupt context - irqs are off: */
577905a36a2SIngo Molnar	TRACE_IRQS_OFF
578905a36a2SIngo Molnar
5792ba64741SDominik Brodowski	ret
580bc7b11c0SJiri SlabySYM_CODE_END(interrupt_entry)
581a50480cbSAndrea Righi_ASM_NOKPROBE(interrupt_entry)
5822ba64741SDominik Brodowski
583f3d415eaSDominik Brodowski
584f3d415eaSDominik Brodowski/* Interrupt entry/exit. */
585905a36a2SIngo Molnar
586905a36a2SIngo Molnar/*
587905a36a2SIngo Molnar * The interrupt stubs push (~vector+0x80) onto the stack and
588f8a8fe61SThomas Gleixner * then jump to common_spurious/interrupt.
589905a36a2SIngo Molnar */
590cc66936eSJiri SlabySYM_CODE_START_LOCAL(common_spurious)
591f8a8fe61SThomas Gleixner	addq	$-0x80, (%rsp)			/* Adjust vector to [-256, -1] range */
592f8a8fe61SThomas Gleixner	call	interrupt_entry
593f8a8fe61SThomas Gleixner	UNWIND_HINT_REGS indirect=1
594f8a8fe61SThomas Gleixner	call	smp_spurious_interrupt		/* rdi points to pt_regs */
595f8a8fe61SThomas Gleixner	jmp	ret_from_intr
596cc66936eSJiri SlabySYM_CODE_END(common_spurious)
597f8a8fe61SThomas Gleixner_ASM_NOKPROBE(common_spurious)
598f8a8fe61SThomas Gleixner
599f8a8fe61SThomas Gleixner/* common_interrupt is a hotpath. Align it */
600905a36a2SIngo Molnar	.p2align CONFIG_X86_L1_CACHE_SHIFT
601cc66936eSJiri SlabySYM_CODE_START_LOCAL(common_interrupt)
602905a36a2SIngo Molnar	addq	$-0x80, (%rsp)			/* Adjust vector to [-256, -1] range */
6033aa99fc3SDominik Brodowski	call	interrupt_entry
6043aa99fc3SDominik Brodowski	UNWIND_HINT_REGS indirect=1
6053aa99fc3SDominik Brodowski	call	do_IRQ	/* rdi points to pt_regs */
606905a36a2SIngo Molnar	/* 0(%rsp): old RSP */
607905a36a2SIngo Molnarret_from_intr:
6082140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
609905a36a2SIngo Molnar	TRACE_IRQS_OFF
610905a36a2SIngo Molnar
6111d3e53e8SAndy Lutomirski	LEAVE_IRQ_STACK
612905a36a2SIngo Molnar
613905a36a2SIngo Molnar	testb	$3, CS(%rsp)
614905a36a2SIngo Molnar	jz	retint_kernel
61502bc7768SAndy Lutomirski
616905a36a2SIngo Molnar	/* Interrupt came from user space */
61730a2441cSJiri Slaby.Lretint_user:
61802bc7768SAndy Lutomirski	mov	%rsp,%rdi
61902bc7768SAndy Lutomirski	call	prepare_exit_to_usermode
62026c4ef9cSAndy Lutomirski
62126ba4e57SJiri SlabySYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
62226c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
62326c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates user mode. */
6241e4c4f61SBorislav Petkov	testb	$3, CS(%rsp)
62526c4ef9cSAndy Lutomirski	jnz	1f
62626c4ef9cSAndy Lutomirski	ud2
62726c4ef9cSAndy Lutomirski1:
62826c4ef9cSAndy Lutomirski#endif
629502af0d7SDominik Brodowski	POP_REGS pop_rdi=0
6303e3b9293SAndy Lutomirski
6313e3b9293SAndy Lutomirski	/*
6323e3b9293SAndy Lutomirski	 * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS.
6333e3b9293SAndy Lutomirski	 * Save old stack pointer and switch to trampoline stack.
6343e3b9293SAndy Lutomirski	 */
6353e3b9293SAndy Lutomirski	movq	%rsp, %rdi
636c482feefSAndy Lutomirski	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
6371fb14363SJosh Poimboeuf	UNWIND_HINT_EMPTY
6383e3b9293SAndy Lutomirski
6393e3b9293SAndy Lutomirski	/* Copy the IRET frame to the trampoline stack. */
6403e3b9293SAndy Lutomirski	pushq	6*8(%rdi)	/* SS */
6413e3b9293SAndy Lutomirski	pushq	5*8(%rdi)	/* RSP */
6423e3b9293SAndy Lutomirski	pushq	4*8(%rdi)	/* EFLAGS */
6433e3b9293SAndy Lutomirski	pushq	3*8(%rdi)	/* CS */
6443e3b9293SAndy Lutomirski	pushq	2*8(%rdi)	/* RIP */
6453e3b9293SAndy Lutomirski
6463e3b9293SAndy Lutomirski	/* Push user RDI on the trampoline stack. */
6473e3b9293SAndy Lutomirski	pushq	(%rdi)
6483e3b9293SAndy Lutomirski
6493e3b9293SAndy Lutomirski	/*
6503e3b9293SAndy Lutomirski	 * We are on the trampoline stack.  All regs except RDI are live.
6513e3b9293SAndy Lutomirski	 * We can do future final exit work right here.
6523e3b9293SAndy Lutomirski	 */
653afaef01cSAlexander Popov	STACKLEAK_ERASE_NOCLOBBER
6543e3b9293SAndy Lutomirski
6556fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
6568a09317bSDave Hansen
6573e3b9293SAndy Lutomirski	/* Restore RDI. */
6583e3b9293SAndy Lutomirski	popq	%rdi
6593e3b9293SAndy Lutomirski	SWAPGS
66026c4ef9cSAndy Lutomirski	INTERRUPT_RETURN
66126c4ef9cSAndy Lutomirski
662905a36a2SIngo Molnar
663905a36a2SIngo Molnar/* Returning to kernel space */
664905a36a2SIngo Molnarretint_kernel:
66548593975SThomas Gleixner#ifdef CONFIG_PREEMPTION
666905a36a2SIngo Molnar	/* Interrupts are off */
667905a36a2SIngo Molnar	/* Check if we need preemption */
6686709812fSJan Beulich	btl	$9, EFLAGS(%rsp)		/* were interrupts off? */
669905a36a2SIngo Molnar	jnc	1f
670b5b447b6SValentin Schneider	cmpl	$0, PER_CPU_VAR(__preempt_count)
671905a36a2SIngo Molnar	jnz	1f
672905a36a2SIngo Molnar	call	preempt_schedule_irq
673905a36a2SIngo Molnar1:
674905a36a2SIngo Molnar#endif
675905a36a2SIngo Molnar	/*
676905a36a2SIngo Molnar	 * The iretq could re-enable interrupts:
677905a36a2SIngo Molnar	 */
678905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
679905a36a2SIngo Molnar
68026ba4e57SJiri SlabySYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)
68126c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
68226c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates kernel mode. */
6831e4c4f61SBorislav Petkov	testb	$3, CS(%rsp)
68426c4ef9cSAndy Lutomirski	jz	1f
68526c4ef9cSAndy Lutomirski	ud2
68626c4ef9cSAndy Lutomirski1:
68726c4ef9cSAndy Lutomirski#endif
688502af0d7SDominik Brodowski	POP_REGS
689e872045bSAndy Lutomirski	addq	$8, %rsp	/* skip regs->orig_ax */
69010bcc80eSMathieu Desnoyers	/*
69110bcc80eSMathieu Desnoyers	 * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
69210bcc80eSMathieu Desnoyers	 * when returning from IPI handler.
69310bcc80eSMathieu Desnoyers	 */
694905a36a2SIngo Molnar	INTERRUPT_RETURN
695905a36a2SIngo Molnar
696cc66936eSJiri SlabySYM_INNER_LABEL_ALIGN(native_iret, SYM_L_GLOBAL)
6978c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
698905a36a2SIngo Molnar	/*
699905a36a2SIngo Molnar	 * Are we returning to a stack segment from the LDT?  Note: in
700905a36a2SIngo Molnar	 * 64-bit mode SS:RSP on the exception stack is always valid.
701905a36a2SIngo Molnar	 */
702905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
703905a36a2SIngo Molnar	testb	$4, (SS-RIP)(%rsp)
704905a36a2SIngo Molnar	jnz	native_irq_return_ldt
705905a36a2SIngo Molnar#endif
706905a36a2SIngo Molnar
707cc66936eSJiri SlabySYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL)
708905a36a2SIngo Molnar	/*
709905a36a2SIngo Molnar	 * This may fault.  Non-paranoid faults on return to userspace are
710905a36a2SIngo Molnar	 * handled by fixup_bad_iret.  These include #SS, #GP, and #NP.
711905a36a2SIngo Molnar	 * Double-faults due to espfix64 are handled in do_double_fault.
712905a36a2SIngo Molnar	 * Other faults here are fatal.
713905a36a2SIngo Molnar	 */
714905a36a2SIngo Molnar	iretq
715905a36a2SIngo Molnar
716905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
717905a36a2SIngo Molnarnative_irq_return_ldt:
71885063facSAndy Lutomirski	/*
71985063facSAndy Lutomirski	 * We are running with user GSBASE.  All GPRs contain their user
72085063facSAndy Lutomirski	 * values.  We have a percpu ESPFIX stack that is eight slots
72185063facSAndy Lutomirski	 * long (see ESPFIX_STACK_SIZE).  espfix_waddr points to the bottom
72285063facSAndy Lutomirski	 * of the ESPFIX stack.
72385063facSAndy Lutomirski	 *
72485063facSAndy Lutomirski	 * We clobber RAX and RDI in this code.  We stash RDI on the
72585063facSAndy Lutomirski	 * normal stack and RAX on the ESPFIX stack.
72685063facSAndy Lutomirski	 *
72785063facSAndy Lutomirski	 * The ESPFIX stack layout we set up looks like this:
72885063facSAndy Lutomirski	 *
72985063facSAndy Lutomirski	 * --- top of ESPFIX stack ---
73085063facSAndy Lutomirski	 * SS
73185063facSAndy Lutomirski	 * RSP
73285063facSAndy Lutomirski	 * RFLAGS
73385063facSAndy Lutomirski	 * CS
73485063facSAndy Lutomirski	 * RIP  <-- RSP points here when we're done
73585063facSAndy Lutomirski	 * RAX  <-- espfix_waddr points here
73685063facSAndy Lutomirski	 * --- bottom of ESPFIX stack ---
73785063facSAndy Lutomirski	 */
73885063facSAndy Lutomirski
73985063facSAndy Lutomirski	pushq	%rdi				/* Stash user RDI */
7408a09317bSDave Hansen	SWAPGS					/* to kernel GS */
7418a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi	/* to kernel CR3 */
7428a09317bSDave Hansen
743905a36a2SIngo Molnar	movq	PER_CPU_VAR(espfix_waddr), %rdi
74485063facSAndy Lutomirski	movq	%rax, (0*8)(%rdi)		/* user RAX */
74585063facSAndy Lutomirski	movq	(1*8)(%rsp), %rax		/* user RIP */
746905a36a2SIngo Molnar	movq	%rax, (1*8)(%rdi)
74785063facSAndy Lutomirski	movq	(2*8)(%rsp), %rax		/* user CS */
748905a36a2SIngo Molnar	movq	%rax, (2*8)(%rdi)
74985063facSAndy Lutomirski	movq	(3*8)(%rsp), %rax		/* user RFLAGS */
750905a36a2SIngo Molnar	movq	%rax, (3*8)(%rdi)
75185063facSAndy Lutomirski	movq	(5*8)(%rsp), %rax		/* user SS */
752905a36a2SIngo Molnar	movq	%rax, (5*8)(%rdi)
75385063facSAndy Lutomirski	movq	(4*8)(%rsp), %rax		/* user RSP */
754905a36a2SIngo Molnar	movq	%rax, (4*8)(%rdi)
75585063facSAndy Lutomirski	/* Now RAX == RSP. */
75685063facSAndy Lutomirski
75785063facSAndy Lutomirski	andl	$0xffff0000, %eax		/* RAX = (RSP & 0xffff0000) */
75885063facSAndy Lutomirski
75985063facSAndy Lutomirski	/*
76085063facSAndy Lutomirski	 * espfix_stack[31:16] == 0.  The page tables are set up such that
76185063facSAndy Lutomirski	 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
76285063facSAndy Lutomirski	 * espfix_waddr for any X.  That is, there are 65536 RO aliases of
76385063facSAndy Lutomirski	 * the same page.  Set up RSP so that RSP[31:16] contains the
76485063facSAndy Lutomirski	 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
76585063facSAndy Lutomirski	 * still points to an RO alias of the ESPFIX stack.
76685063facSAndy Lutomirski	 */
767905a36a2SIngo Molnar	orq	PER_CPU_VAR(espfix_stack), %rax
7688a09317bSDave Hansen
7696fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
7708a09317bSDave Hansen	SWAPGS					/* to user GS */
7718a09317bSDave Hansen	popq	%rdi				/* Restore user RDI */
7728a09317bSDave Hansen
773905a36a2SIngo Molnar	movq	%rax, %rsp
7748c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
77585063facSAndy Lutomirski
77685063facSAndy Lutomirski	/*
77785063facSAndy Lutomirski	 * At this point, we cannot write to the stack any more, but we can
77885063facSAndy Lutomirski	 * still read.
77985063facSAndy Lutomirski	 */
78085063facSAndy Lutomirski	popq	%rax				/* Restore user RAX */
78185063facSAndy Lutomirski
78285063facSAndy Lutomirski	/*
78385063facSAndy Lutomirski	 * RSP now points to an ordinary IRET frame, except that the page
78485063facSAndy Lutomirski	 * is read-only and RSP[31:16] are preloaded with the userspace
78585063facSAndy Lutomirski	 * values.  We can now IRET back to userspace.
78685063facSAndy Lutomirski	 */
787905a36a2SIngo Molnar	jmp	native_irq_return_iret
788905a36a2SIngo Molnar#endif
789cc66936eSJiri SlabySYM_CODE_END(common_interrupt)
790a50480cbSAndrea Righi_ASM_NOKPROBE(common_interrupt)
791905a36a2SIngo Molnar
792905a36a2SIngo Molnar/*
793905a36a2SIngo Molnar * APIC interrupts.
794905a36a2SIngo Molnar */
795905a36a2SIngo Molnar.macro apicinterrupt3 num sym do_sym
796bc7b11c0SJiri SlabySYM_CODE_START(\sym)
7978c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
798905a36a2SIngo Molnar	pushq	$~(\num)
7993aa99fc3SDominik Brodowski	call	interrupt_entry
8003aa99fc3SDominik Brodowski	UNWIND_HINT_REGS indirect=1
8013aa99fc3SDominik Brodowski	call	\do_sym	/* rdi points to pt_regs */
802905a36a2SIngo Molnar	jmp	ret_from_intr
803bc7b11c0SJiri SlabySYM_CODE_END(\sym)
804a50480cbSAndrea Righi_ASM_NOKPROBE(\sym)
805905a36a2SIngo Molnar.endm
806905a36a2SIngo Molnar
807469f0023SAlexander Potapenko/* Make sure APIC interrupt handlers end up in the irqentry section: */
808469f0023SAlexander Potapenko#define PUSH_SECTION_IRQENTRY	.pushsection .irqentry.text, "ax"
809469f0023SAlexander Potapenko#define POP_SECTION_IRQENTRY	.popsection
810469f0023SAlexander Potapenko
811905a36a2SIngo Molnar.macro apicinterrupt num sym do_sym
812469f0023SAlexander PotapenkoPUSH_SECTION_IRQENTRY
813905a36a2SIngo Molnarapicinterrupt3 \num \sym \do_sym
814469f0023SAlexander PotapenkoPOP_SECTION_IRQENTRY
815905a36a2SIngo Molnar.endm
816905a36a2SIngo Molnar
817905a36a2SIngo Molnar#ifdef CONFIG_SMP
8184d732138SIngo Molnarapicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR		irq_move_cleanup_interrupt	smp_irq_move_cleanup_interrupt
8194d732138SIngo Molnarapicinterrupt3 REBOOT_VECTOR			reboot_interrupt		smp_reboot_interrupt
820905a36a2SIngo Molnar#endif
821905a36a2SIngo Molnar
822905a36a2SIngo Molnar#ifdef CONFIG_X86_UV
8234d732138SIngo Molnarapicinterrupt3 UV_BAU_MESSAGE			uv_bau_message_intr1		uv_bau_message_interrupt
824905a36a2SIngo Molnar#endif
8254d732138SIngo Molnar
8264d732138SIngo Molnarapicinterrupt LOCAL_TIMER_VECTOR		apic_timer_interrupt		smp_apic_timer_interrupt
8274d732138SIngo Molnarapicinterrupt X86_PLATFORM_IPI_VECTOR		x86_platform_ipi		smp_x86_platform_ipi
828905a36a2SIngo Molnar
829905a36a2SIngo Molnar#ifdef CONFIG_HAVE_KVM
8304d732138SIngo Molnarapicinterrupt3 POSTED_INTR_VECTOR		kvm_posted_intr_ipi		smp_kvm_posted_intr_ipi
8314d732138SIngo Molnarapicinterrupt3 POSTED_INTR_WAKEUP_VECTOR	kvm_posted_intr_wakeup_ipi	smp_kvm_posted_intr_wakeup_ipi
832210f84b0SWincy Vanapicinterrupt3 POSTED_INTR_NESTED_VECTOR	kvm_posted_intr_nested_ipi	smp_kvm_posted_intr_nested_ipi
833905a36a2SIngo Molnar#endif
834905a36a2SIngo Molnar
835905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE_THRESHOLD
8364d732138SIngo Molnarapicinterrupt THRESHOLD_APIC_VECTOR		threshold_interrupt		smp_threshold_interrupt
837905a36a2SIngo Molnar#endif
838905a36a2SIngo Molnar
8399dda1658SIngo Molnar#ifdef CONFIG_X86_MCE_AMD
8404d732138SIngo Molnarapicinterrupt DEFERRED_ERROR_VECTOR		deferred_error_interrupt	smp_deferred_error_interrupt
8419dda1658SIngo Molnar#endif
8429dda1658SIngo Molnar
843905a36a2SIngo Molnar#ifdef CONFIG_X86_THERMAL_VECTOR
8444d732138SIngo Molnarapicinterrupt THERMAL_APIC_VECTOR		thermal_interrupt		smp_thermal_interrupt
845905a36a2SIngo Molnar#endif
846905a36a2SIngo Molnar
847905a36a2SIngo Molnar#ifdef CONFIG_SMP
8484d732138SIngo Molnarapicinterrupt CALL_FUNCTION_SINGLE_VECTOR	call_function_single_interrupt	smp_call_function_single_interrupt
8494d732138SIngo Molnarapicinterrupt CALL_FUNCTION_VECTOR		call_function_interrupt		smp_call_function_interrupt
8504d732138SIngo Molnarapicinterrupt RESCHEDULE_VECTOR			reschedule_interrupt		smp_reschedule_interrupt
851905a36a2SIngo Molnar#endif
852905a36a2SIngo Molnar
8534d732138SIngo Molnarapicinterrupt ERROR_APIC_VECTOR			error_interrupt			smp_error_interrupt
8544d732138SIngo Molnarapicinterrupt SPURIOUS_APIC_VECTOR		spurious_interrupt		smp_spurious_interrupt
855905a36a2SIngo Molnar
856905a36a2SIngo Molnar#ifdef CONFIG_IRQ_WORK
8574d732138SIngo Molnarapicinterrupt IRQ_WORK_VECTOR			irq_work_interrupt		smp_irq_work_interrupt
858905a36a2SIngo Molnar#endif
859905a36a2SIngo Molnar
860905a36a2SIngo Molnar/*
861905a36a2SIngo Molnar * Exception entry points.
862905a36a2SIngo Molnar */
8638f34c5b5SThomas Gleixner#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss_rw) + (TSS_ist + (x) * 8)
864905a36a2SIngo Molnar
865a0d14b89SPeter Zijlstra.macro idtentry_part do_sym, has_error_code:req, read_cr2:req, paranoid:req, shift_ist=-1, ist_offset=0
8662fd37912SPeter Zijlstra
8672fd37912SPeter Zijlstra	.if \paranoid
8682fd37912SPeter Zijlstra	call	paranoid_entry
8692fd37912SPeter Zijlstra	/* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
8702fd37912SPeter Zijlstra	.else
8712fd37912SPeter Zijlstra	call	error_entry
8722fd37912SPeter Zijlstra	.endif
8732fd37912SPeter Zijlstra	UNWIND_HINT_REGS
8742fd37912SPeter Zijlstra
875a0d14b89SPeter Zijlstra	.if \read_cr2
8766879298bSThomas Gleixner	/*
8776879298bSThomas Gleixner	 * Store CR2 early so subsequent faults cannot clobber it. Use R12 as
8786879298bSThomas Gleixner	 * intermediate storage as RDX can be clobbered in enter_from_user_mode().
8796879298bSThomas Gleixner	 * GET_CR2_INTO can clobber RAX.
8806879298bSThomas Gleixner	 */
8816879298bSThomas Gleixner	GET_CR2_INTO(%r12);
882a0d14b89SPeter Zijlstra	.endif
883a0d14b89SPeter Zijlstra
8842fd37912SPeter Zijlstra	.if \shift_ist != -1
8852fd37912SPeter Zijlstra	TRACE_IRQS_OFF_DEBUG			/* reload IDT in case of recursion */
8862fd37912SPeter Zijlstra	.else
8872fd37912SPeter Zijlstra	TRACE_IRQS_OFF
8882fd37912SPeter Zijlstra	.endif
889a0d14b89SPeter Zijlstra
89072500589SThomas Gleixner#ifdef CONFIG_CONTEXT_TRACKING
891a0d14b89SPeter Zijlstra	.if \paranoid == 0
892a0d14b89SPeter Zijlstra	testb	$3, CS(%rsp)
893a0d14b89SPeter Zijlstra	jz	.Lfrom_kernel_no_context_tracking_\@
894a0d14b89SPeter Zijlstra	CALL_enter_from_user_mode
895a0d14b89SPeter Zijlstra.Lfrom_kernel_no_context_tracking_\@:
8962fd37912SPeter Zijlstra	.endif
89772500589SThomas Gleixner#endif
8982fd37912SPeter Zijlstra
8992fd37912SPeter Zijlstra	movq	%rsp, %rdi			/* pt_regs pointer */
9002fd37912SPeter Zijlstra
9012fd37912SPeter Zijlstra	.if \has_error_code
9022fd37912SPeter Zijlstra	movq	ORIG_RAX(%rsp), %rsi		/* get error code */
9032fd37912SPeter Zijlstra	movq	$-1, ORIG_RAX(%rsp)		/* no syscall to restart */
9042fd37912SPeter Zijlstra	.else
9052fd37912SPeter Zijlstra	xorl	%esi, %esi			/* no error code */
9062fd37912SPeter Zijlstra	.endif
9072fd37912SPeter Zijlstra
9082fd37912SPeter Zijlstra	.if \shift_ist != -1
9092fd37912SPeter Zijlstra	subq	$\ist_offset, CPU_TSS_IST(\shift_ist)
9102fd37912SPeter Zijlstra	.endif
9112fd37912SPeter Zijlstra
9126879298bSThomas Gleixner	.if \read_cr2
9136879298bSThomas Gleixner	movq	%r12, %rdx			/* Move CR2 into 3rd argument */
9146879298bSThomas Gleixner	.endif
9156879298bSThomas Gleixner
9162fd37912SPeter Zijlstra	call	\do_sym
9172fd37912SPeter Zijlstra
9182fd37912SPeter Zijlstra	.if \shift_ist != -1
9192fd37912SPeter Zijlstra	addq	$\ist_offset, CPU_TSS_IST(\shift_ist)
9202fd37912SPeter Zijlstra	.endif
9212fd37912SPeter Zijlstra
9222fd37912SPeter Zijlstra	.if \paranoid
9232fd37912SPeter Zijlstra	/* this procedure expect "no swapgs" flag in ebx */
9242fd37912SPeter Zijlstra	jmp	paranoid_exit
9252fd37912SPeter Zijlstra	.else
9262fd37912SPeter Zijlstra	jmp	error_exit
9272fd37912SPeter Zijlstra	.endif
9282fd37912SPeter Zijlstra
9292fd37912SPeter Zijlstra.endm
9302fd37912SPeter Zijlstra
931bd7b1f7cSAndy Lutomirski/**
932bd7b1f7cSAndy Lutomirski * idtentry - Generate an IDT entry stub
933bd7b1f7cSAndy Lutomirski * @sym:		Name of the generated entry point
934bd7b1f7cSAndy Lutomirski * @do_sym:		C function to be called
935bd7b1f7cSAndy Lutomirski * @has_error_code:	True if this IDT vector has an error code on the stack
936bd7b1f7cSAndy Lutomirski * @paranoid:		non-zero means that this vector may be invoked from
937bd7b1f7cSAndy Lutomirski *			kernel mode with user GSBASE and/or user CR3.
938bd7b1f7cSAndy Lutomirski *			2 is special -- see below.
939bd7b1f7cSAndy Lutomirski * @shift_ist:		Set to an IST index if entries from kernel mode should
940bd7b1f7cSAndy Lutomirski *			decrement the IST stack so that nested entries get a
941bd7b1f7cSAndy Lutomirski *			fresh stack.  (This is for #DB, which has a nasty habit
942bd7b1f7cSAndy Lutomirski *			of recursing.)
9434234653eSPeter Zijlstra * @create_gap:		create a 6-word stack gap when coming from kernel mode.
944a0d14b89SPeter Zijlstra * @read_cr2:		load CR2 into the 3rd argument; done before calling any C code
945bd7b1f7cSAndy Lutomirski *
946bd7b1f7cSAndy Lutomirski * idtentry generates an IDT stub that sets up a usable kernel context,
947bd7b1f7cSAndy Lutomirski * creates struct pt_regs, and calls @do_sym.  The stub has the following
948bd7b1f7cSAndy Lutomirski * special behaviors:
949bd7b1f7cSAndy Lutomirski *
950bd7b1f7cSAndy Lutomirski * On an entry from user mode, the stub switches from the trampoline or
951bd7b1f7cSAndy Lutomirski * IST stack to the normal thread stack.  On an exit to user mode, the
952bd7b1f7cSAndy Lutomirski * normal exit-to-usermode path is invoked.
953bd7b1f7cSAndy Lutomirski *
954bd7b1f7cSAndy Lutomirski * On an exit to kernel mode, if @paranoid == 0, we check for preemption,
955bd7b1f7cSAndy Lutomirski * whereas we omit the preemption check if @paranoid != 0.  This is purely
956bd7b1f7cSAndy Lutomirski * because the implementation is simpler this way.  The kernel only needs
957bd7b1f7cSAndy Lutomirski * to check for asynchronous kernel preemption when IRQ handlers return.
958bd7b1f7cSAndy Lutomirski *
959bd7b1f7cSAndy Lutomirski * If @paranoid == 0, then the stub will handle IRET faults by pretending
960bd7b1f7cSAndy Lutomirski * that the fault came from user mode.  It will handle gs_change faults by
961bd7b1f7cSAndy Lutomirski * pretending that the fault happened with kernel GSBASE.  Since this handling
962bd7b1f7cSAndy Lutomirski * is omitted for @paranoid != 0, the #GP, #SS, and #NP stubs must have
963bd7b1f7cSAndy Lutomirski * @paranoid == 0.  This special handling will do the wrong thing for
964bd7b1f7cSAndy Lutomirski * espfix-induced #DF on IRET, so #DF must not use @paranoid == 0.
965bd7b1f7cSAndy Lutomirski *
966bd7b1f7cSAndy Lutomirski * @paranoid == 2 is special: the stub will never switch stacks.  This is for
967bd7b1f7cSAndy Lutomirski * #DF: if the thread stack is somehow unusable, we'll still get a useful OOPS.
968bd7b1f7cSAndy Lutomirski */
969a0d14b89SPeter Zijlstra.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 ist_offset=0 create_gap=0 read_cr2=0
970bc7b11c0SJiri SlabySYM_CODE_START(\sym)
97198990a33SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=\has_error_code*8
9728c1f7558SJosh Poimboeuf
973905a36a2SIngo Molnar	/* Sanity check */
9744234653eSPeter Zijlstra	.if \shift_ist != -1 && \paranoid != 1
975905a36a2SIngo Molnar	.error "using shift_ist requires paranoid=1"
976905a36a2SIngo Molnar	.endif
977905a36a2SIngo Molnar
9784234653eSPeter Zijlstra	.if \create_gap && \paranoid
9794234653eSPeter Zijlstra	.error "using create_gap requires paranoid=0"
9804234653eSPeter Zijlstra	.endif
9814234653eSPeter Zijlstra
982905a36a2SIngo Molnar	ASM_CLAC
983905a36a2SIngo Molnar
98482c62fa0SJosh Poimboeuf	.if \has_error_code == 0
985905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
986905a36a2SIngo Molnar	.endif
987905a36a2SIngo Molnar
988071ccc96SAndy Lutomirski	.if \paranoid == 1
9899e809d15SDominik Brodowski	testb	$3, CS-ORIG_RAX(%rsp)		/* If coming from userspace, switch stacks */
9907f2590a1SAndy Lutomirski	jnz	.Lfrom_usermode_switch_stack_\@
991905a36a2SIngo Molnar	.endif
9927f2590a1SAndy Lutomirski
9932700fefdSJosh Poimboeuf	.if \create_gap == 1
9942700fefdSJosh Poimboeuf	/*
9952700fefdSJosh Poimboeuf	 * If coming from kernel space, create a 6-word gap to allow the
9962700fefdSJosh Poimboeuf	 * int3 handler to emulate a call instruction.
9972700fefdSJosh Poimboeuf	 */
9982700fefdSJosh Poimboeuf	testb	$3, CS-ORIG_RAX(%rsp)
9992700fefdSJosh Poimboeuf	jnz	.Lfrom_usermode_no_gap_\@
10002700fefdSJosh Poimboeuf	.rept	6
10012700fefdSJosh Poimboeuf	pushq	5*8(%rsp)
10022700fefdSJosh Poimboeuf	.endr
10032700fefdSJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
10042700fefdSJosh Poimboeuf.Lfrom_usermode_no_gap_\@:
10052700fefdSJosh Poimboeuf	.endif
10062700fefdSJosh Poimboeuf
1007a0d14b89SPeter Zijlstra	idtentry_part \do_sym, \has_error_code, \read_cr2, \paranoid, \shift_ist, \ist_offset
1008905a36a2SIngo Molnar
1009071ccc96SAndy Lutomirski	.if \paranoid == 1
1010905a36a2SIngo Molnar	/*
10117f2590a1SAndy Lutomirski	 * Entry from userspace.  Switch stacks and treat it
1012905a36a2SIngo Molnar	 * as a normal entry.  This means that paranoid handlers
1013905a36a2SIngo Molnar	 * run in real process context if user_mode(regs).
1014905a36a2SIngo Molnar	 */
10157f2590a1SAndy Lutomirski.Lfrom_usermode_switch_stack_\@:
1016a0d14b89SPeter Zijlstra	idtentry_part \do_sym, \has_error_code, \read_cr2, paranoid=0
1017905a36a2SIngo Molnar	.endif
1018905a36a2SIngo Molnar
1019a50480cbSAndrea Righi_ASM_NOKPROBE(\sym)
1020bc7b11c0SJiri SlabySYM_CODE_END(\sym)
1021905a36a2SIngo Molnar.endm
1022905a36a2SIngo Molnar
1023905a36a2SIngo Molnaridtentry divide_error			do_divide_error			has_error_code=0
1024905a36a2SIngo Molnaridtentry overflow			do_overflow			has_error_code=0
1025905a36a2SIngo Molnaridtentry bounds				do_bounds			has_error_code=0
1026905a36a2SIngo Molnaridtentry invalid_op			do_invalid_op			has_error_code=0
1027905a36a2SIngo Molnaridtentry device_not_available		do_device_not_available		has_error_code=0
1028a0d14b89SPeter Zijlstraidtentry double_fault			do_double_fault			has_error_code=1 paranoid=2 read_cr2=1
1029905a36a2SIngo Molnaridtentry coprocessor_segment_overrun	do_coprocessor_segment_overrun	has_error_code=0
1030905a36a2SIngo Molnaridtentry invalid_TSS			do_invalid_TSS			has_error_code=1
1031905a36a2SIngo Molnaridtentry segment_not_present		do_segment_not_present		has_error_code=1
1032905a36a2SIngo Molnaridtentry spurious_interrupt_bug		do_spurious_interrupt_bug	has_error_code=0
1033905a36a2SIngo Molnaridtentry coprocessor_error		do_coprocessor_error		has_error_code=0
1034905a36a2SIngo Molnaridtentry alignment_check		do_alignment_check		has_error_code=1
1035905a36a2SIngo Molnaridtentry simd_coprocessor_error		do_simd_coprocessor_error	has_error_code=0
1036905a36a2SIngo Molnar
1037905a36a2SIngo Molnar
10384d732138SIngo Molnar/*
10394d732138SIngo Molnar * Reload gs selector with exception handling
10404d732138SIngo Molnar * edi:  new selector
1041b9f6976bSThomas Gleixner *
1042b9f6976bSThomas Gleixner * Is in entry.text as it shouldn't be instrumented.
10434d732138SIngo Molnar */
1044*410367e3SThomas GleixnerSYM_FUNC_START(asm_load_gs_index)
10458c1f7558SJosh Poimboeuf	FRAME_BEGIN
1046905a36a2SIngo Molnar	SWAPGS
104742c748bbSBorislav Petkov.Lgs_change:
1048905a36a2SIngo Molnar	movl	%edi, %gs
104996e5d28aSBorislav Petkov2:	ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
1050905a36a2SIngo Molnar	SWAPGS
10518c1f7558SJosh Poimboeuf	FRAME_END
1052905a36a2SIngo Molnar	ret
1053*410367e3SThomas GleixnerSYM_FUNC_END(asm_load_gs_index)
1054*410367e3SThomas GleixnerEXPORT_SYMBOL(asm_load_gs_index)
1055905a36a2SIngo Molnar
105698ededb6SJiri Slaby	_ASM_EXTABLE(.Lgs_change, .Lbad_gs)
1057905a36a2SIngo Molnar	.section .fixup, "ax"
1058905a36a2SIngo Molnar	/* running with kernelgs */
1059ef77e688SJiri SlabySYM_CODE_START_LOCAL_NOALIGN(.Lbad_gs)
1060905a36a2SIngo Molnar	SWAPGS					/* switch back to user gs */
1061b038c842SAndy Lutomirski.macro ZAP_GS
1062b038c842SAndy Lutomirski	/* This can't be a string because the preprocessor needs to see it. */
1063b038c842SAndy Lutomirski	movl $__USER_DS, %eax
1064b038c842SAndy Lutomirski	movl %eax, %gs
1065b038c842SAndy Lutomirski.endm
1066b038c842SAndy Lutomirski	ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
1067905a36a2SIngo Molnar	xorl	%eax, %eax
1068905a36a2SIngo Molnar	movl	%eax, %gs
1069905a36a2SIngo Molnar	jmp	2b
1070ef77e688SJiri SlabySYM_CODE_END(.Lbad_gs)
1071905a36a2SIngo Molnar	.previous
1072905a36a2SIngo Molnar
1073905a36a2SIngo Molnar/* Call softirq on interrupt stack. Interrupts are off. */
1074b9f6976bSThomas Gleixner.pushsection .text, "ax"
10756dcc5627SJiri SlabySYM_FUNC_START(do_softirq_own_stack)
1076905a36a2SIngo Molnar	pushq	%rbp
1077905a36a2SIngo Molnar	mov	%rsp, %rbp
10788c1f7558SJosh Poimboeuf	ENTER_IRQ_STACK regs=0 old_rsp=%r11
1079905a36a2SIngo Molnar	call	__do_softirq
10808c1f7558SJosh Poimboeuf	LEAVE_IRQ_STACK regs=0
1081905a36a2SIngo Molnar	leaveq
1082905a36a2SIngo Molnar	ret
10836dcc5627SJiri SlabySYM_FUNC_END(do_softirq_own_stack)
1084b9f6976bSThomas Gleixner.popsection
1085905a36a2SIngo Molnar
108628c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV
10875878d5d6SJuergen Grossidtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0
1088905a36a2SIngo Molnar
1089905a36a2SIngo Molnar/*
1090905a36a2SIngo Molnar * A note on the "critical region" in our callback handler.
1091905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring
1092905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled
1093905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before
1094905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still
1095905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack.
1096905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd
1097905a36a2SIngo Molnar * like to avoid the possibility.
1098905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an
1099905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current
1100905a36a2SIngo Molnar * activation and restart the handler using the previous one.
1101905a36a2SIngo Molnar */
1102ef1e0315SJiri Slaby/* do_hypervisor_callback(struct *pt_regs) */
1103ef1e0315SJiri SlabySYM_CODE_START_LOCAL(xen_do_hypervisor_callback)
11044d732138SIngo Molnar
1105905a36a2SIngo Molnar/*
1106905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1107905a36a2SIngo Molnar * see the correct pointer to the pt_regs
1108905a36a2SIngo Molnar */
11098c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
11104d732138SIngo Molnar	movq	%rdi, %rsp			/* we don't return, adjust the stack frame */
11118c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
11121d3e53e8SAndy Lutomirski
11131d3e53e8SAndy Lutomirski	ENTER_IRQ_STACK old_rsp=%r10
1114905a36a2SIngo Molnar	call	xen_evtchn_do_upcall
11151d3e53e8SAndy Lutomirski	LEAVE_IRQ_STACK
11161d3e53e8SAndy Lutomirski
111748593975SThomas Gleixner#ifndef CONFIG_PREEMPTION
1118905a36a2SIngo Molnar	call	xen_maybe_preempt_hcall
1119905a36a2SIngo Molnar#endif
1120905a36a2SIngo Molnar	jmp	error_exit
1121ef1e0315SJiri SlabySYM_CODE_END(xen_do_hypervisor_callback)
1122905a36a2SIngo Molnar
1123905a36a2SIngo Molnar/*
1124905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes.
1125905a36a2SIngo Molnar * We get here for two reasons:
1126905a36a2SIngo Molnar *  1. Fault while reloading DS, ES, FS or GS
1127905a36a2SIngo Molnar *  2. Fault while executing IRET
1128905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment
1129905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others.
1130905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the
1131905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall
1132905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1133905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register
1134905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1.
1135905a36a2SIngo Molnar */
1136bc7b11c0SJiri SlabySYM_CODE_START(xen_failsafe_callback)
11378c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
1138905a36a2SIngo Molnar	movl	%ds, %ecx
1139905a36a2SIngo Molnar	cmpw	%cx, 0x10(%rsp)
1140905a36a2SIngo Molnar	jne	1f
1141905a36a2SIngo Molnar	movl	%es, %ecx
1142905a36a2SIngo Molnar	cmpw	%cx, 0x18(%rsp)
1143905a36a2SIngo Molnar	jne	1f
1144905a36a2SIngo Molnar	movl	%fs, %ecx
1145905a36a2SIngo Molnar	cmpw	%cx, 0x20(%rsp)
1146905a36a2SIngo Molnar	jne	1f
1147905a36a2SIngo Molnar	movl	%gs, %ecx
1148905a36a2SIngo Molnar	cmpw	%cx, 0x28(%rsp)
1149905a36a2SIngo Molnar	jne	1f
1150905a36a2SIngo Molnar	/* All segments match their saved values => Category 2 (Bad IRET). */
1151905a36a2SIngo Molnar	movq	(%rsp), %rcx
1152905a36a2SIngo Molnar	movq	8(%rsp), %r11
1153905a36a2SIngo Molnar	addq	$0x30, %rsp
1154905a36a2SIngo Molnar	pushq	$0				/* RIP */
11558c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
1156905a36a2SIngo Molnar	jmp	general_protection
1157905a36a2SIngo Molnar1:	/* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1158905a36a2SIngo Molnar	movq	(%rsp), %rcx
1159905a36a2SIngo Molnar	movq	8(%rsp), %r11
1160905a36a2SIngo Molnar	addq	$0x30, %rsp
11618c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1162905a36a2SIngo Molnar	pushq	$-1 /* orig_ax = -1 => not a system call */
11633f01daecSDominik Brodowski	PUSH_AND_CLEAR_REGS
1164946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
1165905a36a2SIngo Molnar	jmp	error_exit
1166bc7b11c0SJiri SlabySYM_CODE_END(xen_failsafe_callback)
116728c11b0fSJuergen Gross#endif /* CONFIG_XEN_PV */
1168905a36a2SIngo Molnar
116928c11b0fSJuergen Gross#ifdef CONFIG_XEN_PVHVM
1170905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1171905a36a2SIngo Molnar	xen_hvm_callback_vector xen_evtchn_do_upcall
117228c11b0fSJuergen Gross#endif
1173905a36a2SIngo Molnar
1174905a36a2SIngo Molnar
1175905a36a2SIngo Molnar#if IS_ENABLED(CONFIG_HYPERV)
1176905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1177905a36a2SIngo Molnar	hyperv_callback_vector hyperv_vector_handler
117893286261SVitaly Kuznetsov
117993286261SVitaly Kuznetsovapicinterrupt3 HYPERV_REENLIGHTENMENT_VECTOR \
118093286261SVitaly Kuznetsov	hyperv_reenlightenment_vector hyperv_reenlightenment_intr
1181248e742aSMichael Kelley
1182248e742aSMichael Kelleyapicinterrupt3 HYPERV_STIMER0_VECTOR \
1183248e742aSMichael Kelley	hv_stimer0_callback_vector hv_stimer0_vector_handler
1184905a36a2SIngo Molnar#endif /* CONFIG_HYPERV */
1185905a36a2SIngo Molnar
1186498ad393SZhao Yakui#if IS_ENABLED(CONFIG_ACRN_GUEST)
1187498ad393SZhao Yakuiapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1188498ad393SZhao Yakui	acrn_hv_callback_vector acrn_hv_vector_handler
1189498ad393SZhao Yakui#endif
1190498ad393SZhao Yakui
11912a594d4cSThomas Gleixneridtentry debug			do_debug		has_error_code=0	paranoid=1 shift_ist=IST_INDEX_DB ist_offset=DB_STACK_OFFSET
11922700fefdSJosh Poimboeufidtentry int3			do_int3			has_error_code=0	create_gap=1
1193905a36a2SIngo Molnaridtentry stack_segment		do_stack_segment	has_error_code=1
11944d732138SIngo Molnar
119528c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV
119643e41110SJuergen Grossidtentry xennmi			do_nmi			has_error_code=0
11975878d5d6SJuergen Grossidtentry xendebug		do_debug		has_error_code=0
1198905a36a2SIngo Molnar#endif
11994d732138SIngo Molnar
1200905a36a2SIngo Molnaridtentry general_protection	do_general_protection	has_error_code=1
1201a0d14b89SPeter Zijlstraidtentry page_fault		do_page_fault		has_error_code=1	read_cr2=1
12024d732138SIngo Molnar
1203905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE
12046f41c34dSThomas Gleixneridtentry machine_check		do_mce			has_error_code=0	paranoid=1
1205905a36a2SIngo Molnar#endif
1206905a36a2SIngo Molnar
1207905a36a2SIngo Molnar/*
12089e809d15SDominik Brodowski * Save all registers in pt_regs, and switch gs if needed.
1209905a36a2SIngo Molnar * Use slow, but surefire "are we in kernel?" check.
1210905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1211905a36a2SIngo Molnar */
1212ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_entry)
12138c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
1214905a36a2SIngo Molnar	cld
12159e809d15SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
12169e809d15SDominik Brodowski	ENCODE_FRAME_POINTER 8
1217905a36a2SIngo Molnar	movl	$1, %ebx
1218905a36a2SIngo Molnar	movl	$MSR_GS_BASE, %ecx
1219905a36a2SIngo Molnar	rdmsr
1220905a36a2SIngo Molnar	testl	%edx, %edx
1221905a36a2SIngo Molnar	js	1f				/* negative -> in kernel */
1222905a36a2SIngo Molnar	SWAPGS
1223905a36a2SIngo Molnar	xorl	%ebx, %ebx
12248a09317bSDave Hansen
12258a09317bSDave Hansen1:
122616561f27SDave Hansen	/*
122716561f27SDave Hansen	 * Always stash CR3 in %r14.  This value will be restored,
1228ae852495SAndy Lutomirski	 * verbatim, at exit.  Needed if paranoid_entry interrupted
1229ae852495SAndy Lutomirski	 * another entry that already switched to the user CR3 value
1230ae852495SAndy Lutomirski	 * but has not yet returned to userspace.
123116561f27SDave Hansen	 *
123216561f27SDave Hansen	 * This is also why CS (stashed in the "iret frame" by the
123316561f27SDave Hansen	 * hardware at entry) can not be used: this may be a return
1234ae852495SAndy Lutomirski	 * to kernel code, but with a user CR3 value.
123516561f27SDave Hansen	 */
12368a09317bSDave Hansen	SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
12378a09317bSDave Hansen
123818ec54fdSJosh Poimboeuf	/*
123918ec54fdSJosh Poimboeuf	 * The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an
124018ec54fdSJosh Poimboeuf	 * unconditional CR3 write, even in the PTI case.  So do an lfence
124118ec54fdSJosh Poimboeuf	 * to prevent GS speculation, regardless of whether PTI is enabled.
124218ec54fdSJosh Poimboeuf	 */
124318ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
124418ec54fdSJosh Poimboeuf
12458a09317bSDave Hansen	ret
1246ef1e0315SJiri SlabySYM_CODE_END(paranoid_entry)
1247905a36a2SIngo Molnar
1248905a36a2SIngo Molnar/*
1249905a36a2SIngo Molnar * "Paranoid" exit path from exception stack.  This is invoked
1250905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came
1251905a36a2SIngo Molnar * from kernel space.
1252905a36a2SIngo Molnar *
1253905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early
1254905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would
1255905a36a2SIngo Molnar * be complicated.  Fortunately, we there's no good reason
1256905a36a2SIngo Molnar * to try to handle preemption here.
12574d732138SIngo Molnar *
12584d732138SIngo Molnar * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
1259905a36a2SIngo Molnar */
1260ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_exit)
12618c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
12622140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
1263905a36a2SIngo Molnar	TRACE_IRQS_OFF_DEBUG
1264905a36a2SIngo Molnar	testl	%ebx, %ebx			/* swapgs needed? */
1265e5317832SAndy Lutomirski	jnz	.Lparanoid_exit_no_swapgs
1266905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
126716561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
126821e94459SPeter Zijlstra	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
1269905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
127045c08383SThomas Gleixner	jmp	restore_regs_and_return_to_kernel
1271e5317832SAndy Lutomirski.Lparanoid_exit_no_swapgs:
1272905a36a2SIngo Molnar	TRACE_IRQS_IRETQ_DEBUG
127316561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
1274e4865757SIngo Molnar	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
1275e5317832SAndy Lutomirski	jmp restore_regs_and_return_to_kernel
1276ef1e0315SJiri SlabySYM_CODE_END(paranoid_exit)
1277905a36a2SIngo Molnar
1278905a36a2SIngo Molnar/*
12799e809d15SDominik Brodowski * Save all registers in pt_regs, and switch GS if needed.
1280905a36a2SIngo Molnar */
1281ef1e0315SJiri SlabySYM_CODE_START_LOCAL(error_entry)
12829e809d15SDominik Brodowski	UNWIND_HINT_FUNC
1283905a36a2SIngo Molnar	cld
12849e809d15SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
12859e809d15SDominik Brodowski	ENCODE_FRAME_POINTER 8
1286905a36a2SIngo Molnar	testb	$3, CS+8(%rsp)
1287cb6f64edSAndy Lutomirski	jz	.Lerror_kernelspace
1288539f5113SAndy Lutomirski
1289cb6f64edSAndy Lutomirski	/*
1290cb6f64edSAndy Lutomirski	 * We entered from user mode or we're pretending to have entered
1291cb6f64edSAndy Lutomirski	 * from user mode due to an IRET fault.
1292cb6f64edSAndy Lutomirski	 */
1293905a36a2SIngo Molnar	SWAPGS
129418ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
12958a09317bSDave Hansen	/* We have user CR3.  Change to kernel CR3. */
12968a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
1297539f5113SAndy Lutomirski
1298cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_after_swapgs:
12997f2590a1SAndy Lutomirski	/* Put us onto the real thread stack. */
13007f2590a1SAndy Lutomirski	popq	%r12				/* save return addr in %12 */
13017f2590a1SAndy Lutomirski	movq	%rsp, %rdi			/* arg0 = pt_regs pointer */
13027f2590a1SAndy Lutomirski	call	sync_regs
13037f2590a1SAndy Lutomirski	movq	%rax, %rsp			/* switch stack */
13047f2590a1SAndy Lutomirski	ENCODE_FRAME_POINTER
13057f2590a1SAndy Lutomirski	pushq	%r12
1306f1075053SAndy Lutomirski	ret
130702bc7768SAndy Lutomirski
130818ec54fdSJosh Poimboeuf.Lerror_entry_done_lfence:
130918ec54fdSJosh Poimboeuf	FENCE_SWAPGS_KERNEL_ENTRY
1310cb6f64edSAndy Lutomirski.Lerror_entry_done:
1311905a36a2SIngo Molnar	ret
1312905a36a2SIngo Molnar
1313905a36a2SIngo Molnar	/*
1314905a36a2SIngo Molnar	 * There are two places in the kernel that can potentially fault with
1315905a36a2SIngo Molnar	 * usergs. Handle them here.  B stepping K8s sometimes report a
1316905a36a2SIngo Molnar	 * truncated RIP for IRET exceptions returning to compat mode. Check
1317905a36a2SIngo Molnar	 * for these here too.
1318905a36a2SIngo Molnar	 */
1319cb6f64edSAndy Lutomirski.Lerror_kernelspace:
1320905a36a2SIngo Molnar	leaq	native_irq_return_iret(%rip), %rcx
1321905a36a2SIngo Molnar	cmpq	%rcx, RIP+8(%rsp)
1322cb6f64edSAndy Lutomirski	je	.Lerror_bad_iret
1323905a36a2SIngo Molnar	movl	%ecx, %eax			/* zero extend */
1324905a36a2SIngo Molnar	cmpq	%rax, RIP+8(%rsp)
1325cb6f64edSAndy Lutomirski	je	.Lbstep_iret
132642c748bbSBorislav Petkov	cmpq	$.Lgs_change, RIP+8(%rsp)
132718ec54fdSJosh Poimboeuf	jne	.Lerror_entry_done_lfence
1328539f5113SAndy Lutomirski
1329539f5113SAndy Lutomirski	/*
133042c748bbSBorislav Petkov	 * hack: .Lgs_change can fail with user gsbase.  If this happens, fix up
1331539f5113SAndy Lutomirski	 * gsbase and proceed.  We'll fix up the exception and land in
133242c748bbSBorislav Petkov	 * .Lgs_change's error handler with kernel gsbase.
1333539f5113SAndy Lutomirski	 */
13342fa5f04fSWanpeng Li	SWAPGS
133518ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
13362fa5f04fSWanpeng Li	jmp .Lerror_entry_done
1337905a36a2SIngo Molnar
1338cb6f64edSAndy Lutomirski.Lbstep_iret:
1339905a36a2SIngo Molnar	/* Fix truncated RIP */
1340905a36a2SIngo Molnar	movq	%rcx, RIP+8(%rsp)
1341905a36a2SIngo Molnar	/* fall through */
1342905a36a2SIngo Molnar
1343cb6f64edSAndy Lutomirski.Lerror_bad_iret:
1344539f5113SAndy Lutomirski	/*
13458a09317bSDave Hansen	 * We came from an IRET to user mode, so we have user
13468a09317bSDave Hansen	 * gsbase and CR3.  Switch to kernel gsbase and CR3:
1347539f5113SAndy Lutomirski	 */
1348905a36a2SIngo Molnar	SWAPGS
134918ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
13508a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
1351539f5113SAndy Lutomirski
1352539f5113SAndy Lutomirski	/*
1353539f5113SAndy Lutomirski	 * Pretend that the exception came from user mode: set up pt_regs
1354b3681dd5SAndy Lutomirski	 * as if we faulted immediately after IRET.
1355539f5113SAndy Lutomirski	 */
1356905a36a2SIngo Molnar	mov	%rsp, %rdi
1357905a36a2SIngo Molnar	call	fixup_bad_iret
1358905a36a2SIngo Molnar	mov	%rax, %rsp
1359cb6f64edSAndy Lutomirski	jmp	.Lerror_entry_from_usermode_after_swapgs
1360ef1e0315SJiri SlabySYM_CODE_END(error_entry)
1361905a36a2SIngo Molnar
1362ef1e0315SJiri SlabySYM_CODE_START_LOCAL(error_exit)
13638c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
13642140a994SJan Beulich	DISABLE_INTERRUPTS(CLBR_ANY)
1365905a36a2SIngo Molnar	TRACE_IRQS_OFF
1366b3681dd5SAndy Lutomirski	testb	$3, CS(%rsp)
1367b3681dd5SAndy Lutomirski	jz	retint_kernel
136830a2441cSJiri Slaby	jmp	.Lretint_user
1369ef1e0315SJiri SlabySYM_CODE_END(error_exit)
1370905a36a2SIngo Molnar
1371929bacecSAndy Lutomirski/*
1372929bacecSAndy Lutomirski * Runs on exception stack.  Xen PV does not go through this path at all,
1373929bacecSAndy Lutomirski * so we can use real assembly here.
13748a09317bSDave Hansen *
13758a09317bSDave Hansen * Registers:
13768a09317bSDave Hansen *	%r14: Used to save/restore the CR3 of the interrupted context
13778a09317bSDave Hansen *	      when PAGE_TABLE_ISOLATION is in use.  Do not clobber.
1378929bacecSAndy Lutomirski */
1379bc7b11c0SJiri SlabySYM_CODE_START(nmi)
13808c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1381929bacecSAndy Lutomirski
1382fc57a7c6SAndy Lutomirski	/*
1383905a36a2SIngo Molnar	 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1384905a36a2SIngo Molnar	 * the iretq it performs will take us out of NMI context.
1385905a36a2SIngo Molnar	 * This means that we can have nested NMIs where the next
1386905a36a2SIngo Molnar	 * NMI is using the top of the stack of the previous NMI. We
1387905a36a2SIngo Molnar	 * can't let it execute because the nested NMI will corrupt the
1388905a36a2SIngo Molnar	 * stack of the previous NMI. NMI handlers are not re-entrant
1389905a36a2SIngo Molnar	 * anyway.
1390905a36a2SIngo Molnar	 *
1391905a36a2SIngo Molnar	 * To handle this case we do the following:
1392905a36a2SIngo Molnar	 *  Check the a special location on the stack that contains
1393905a36a2SIngo Molnar	 *  a variable that is set when NMIs are executing.
1394905a36a2SIngo Molnar	 *  The interrupted task's stack is also checked to see if it
1395905a36a2SIngo Molnar	 *  is an NMI stack.
1396905a36a2SIngo Molnar	 *  If the variable is not set and the stack is not the NMI
1397905a36a2SIngo Molnar	 *  stack then:
1398905a36a2SIngo Molnar	 *    o Set the special variable on the stack
13990b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "outermost" location on the
14000b22930eSAndy Lutomirski	 *      stack
14010b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "iret" location on the stack
1402905a36a2SIngo Molnar	 *    o Continue processing the NMI
1403905a36a2SIngo Molnar	 *  If the variable is set or the previous stack is the NMI stack:
14040b22930eSAndy Lutomirski	 *    o Modify the "iret" location to jump to the repeat_nmi
1405905a36a2SIngo Molnar	 *    o return back to the first NMI
1406905a36a2SIngo Molnar	 *
1407905a36a2SIngo Molnar	 * Now on exit of the first NMI, we first clear the stack variable
1408905a36a2SIngo Molnar	 * The NMI stack will tell any nested NMIs at that point that it is
1409905a36a2SIngo Molnar	 * nested. Then we pop the stack normally with iret, and if there was
1410905a36a2SIngo Molnar	 * a nested NMI that updated the copy interrupt stack frame, a
1411905a36a2SIngo Molnar	 * jump will be made to the repeat_nmi code that will handle the second
1412905a36a2SIngo Molnar	 * NMI.
14139b6e6a83SAndy Lutomirski	 *
14149b6e6a83SAndy Lutomirski	 * However, espfix prevents us from directly returning to userspace
14159b6e6a83SAndy Lutomirski	 * with a single IRET instruction.  Similarly, IRET to user mode
14169b6e6a83SAndy Lutomirski	 * can fault.  We therefore handle NMIs from user space like
14179b6e6a83SAndy Lutomirski	 * other IST entries.
1418905a36a2SIngo Molnar	 */
1419905a36a2SIngo Molnar
1420e93c1730SAndy Lutomirski	ASM_CLAC
1421e93c1730SAndy Lutomirski
1422905a36a2SIngo Molnar	/* Use %rdx as our temp variable throughout */
1423905a36a2SIngo Molnar	pushq	%rdx
1424905a36a2SIngo Molnar
14259b6e6a83SAndy Lutomirski	testb	$3, CS-RIP+8(%rsp)
14269b6e6a83SAndy Lutomirski	jz	.Lnmi_from_kernel
1427905a36a2SIngo Molnar
1428905a36a2SIngo Molnar	/*
14299b6e6a83SAndy Lutomirski	 * NMI from user mode.  We need to run on the thread stack, but we
14309b6e6a83SAndy Lutomirski	 * can't go through the normal entry paths: NMIs are masked, and
14319b6e6a83SAndy Lutomirski	 * we don't want to enable interrupts, because then we'll end
14329b6e6a83SAndy Lutomirski	 * up in an awkward situation in which IRQs are on but NMIs
14339b6e6a83SAndy Lutomirski	 * are off.
143483c133cfSAndy Lutomirski	 *
143583c133cfSAndy Lutomirski	 * We also must not push anything to the stack before switching
143683c133cfSAndy Lutomirski	 * stacks lest we corrupt the "NMI executing" variable.
14379b6e6a83SAndy Lutomirski	 */
14389b6e6a83SAndy Lutomirski
1439929bacecSAndy Lutomirski	swapgs
14409b6e6a83SAndy Lutomirski	cld
144118ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
14428a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
14439b6e6a83SAndy Lutomirski	movq	%rsp, %rdx
14449b6e6a83SAndy Lutomirski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
14458c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS base=%rdx offset=8
14469b6e6a83SAndy Lutomirski	pushq	5*8(%rdx)	/* pt_regs->ss */
14479b6e6a83SAndy Lutomirski	pushq	4*8(%rdx)	/* pt_regs->rsp */
14489b6e6a83SAndy Lutomirski	pushq	3*8(%rdx)	/* pt_regs->flags */
14499b6e6a83SAndy Lutomirski	pushq	2*8(%rdx)	/* pt_regs->cs */
14509b6e6a83SAndy Lutomirski	pushq	1*8(%rdx)	/* pt_regs->rip */
14518c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
14529b6e6a83SAndy Lutomirski	pushq   $-1		/* pt_regs->orig_ax */
145330907fd1SDominik Brodowski	PUSH_AND_CLEAR_REGS rdx=(%rdx)
1454946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
14559b6e6a83SAndy Lutomirski
14569b6e6a83SAndy Lutomirski	/*
14579b6e6a83SAndy Lutomirski	 * At this point we no longer need to worry about stack damage
14589b6e6a83SAndy Lutomirski	 * due to nesting -- we're on the normal thread stack and we're
14599b6e6a83SAndy Lutomirski	 * done with the NMI stack.
14609b6e6a83SAndy Lutomirski	 */
14619b6e6a83SAndy Lutomirski
14629b6e6a83SAndy Lutomirski	movq	%rsp, %rdi
14639b6e6a83SAndy Lutomirski	movq	$-1, %rsi
14649b6e6a83SAndy Lutomirski	call	do_nmi
14659b6e6a83SAndy Lutomirski
14669b6e6a83SAndy Lutomirski	/*
14679b6e6a83SAndy Lutomirski	 * Return back to user mode.  We must *not* do the normal exit
1468946c1911SJosh Poimboeuf	 * work, because we don't want to enable interrupts.
14699b6e6a83SAndy Lutomirski	 */
14708a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
14719b6e6a83SAndy Lutomirski
14729b6e6a83SAndy Lutomirski.Lnmi_from_kernel:
14739b6e6a83SAndy Lutomirski	/*
14740b22930eSAndy Lutomirski	 * Here's what our stack frame will look like:
14750b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14760b22930eSAndy Lutomirski	 * | original SS                                             |
14770b22930eSAndy Lutomirski	 * | original Return RSP                                     |
14780b22930eSAndy Lutomirski	 * | original RFLAGS                                         |
14790b22930eSAndy Lutomirski	 * | original CS                                             |
14800b22930eSAndy Lutomirski	 * | original RIP                                            |
14810b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14820b22930eSAndy Lutomirski	 * | temp storage for rdx                                    |
14830b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14840b22930eSAndy Lutomirski	 * | "NMI executing" variable                                |
14850b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14860b22930eSAndy Lutomirski	 * | iret SS          } Copied from "outermost" frame        |
14870b22930eSAndy Lutomirski	 * | iret Return RSP  } on each loop iteration; overwritten  |
14880b22930eSAndy Lutomirski	 * | iret RFLAGS      } by a nested NMI to force another     |
14890b22930eSAndy Lutomirski	 * | iret CS          } iteration if needed.                 |
14900b22930eSAndy Lutomirski	 * | iret RIP         }                                      |
14910b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14920b22930eSAndy Lutomirski	 * | outermost SS          } initialized in first_nmi;       |
14930b22930eSAndy Lutomirski	 * | outermost Return RSP  } will not be changed before      |
14940b22930eSAndy Lutomirski	 * | outermost RFLAGS      } NMI processing is done.         |
14950b22930eSAndy Lutomirski	 * | outermost CS          } Copied to "iret" frame on each  |
14960b22930eSAndy Lutomirski	 * | outermost RIP         } iteration.                      |
14970b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
14980b22930eSAndy Lutomirski	 * | pt_regs                                                 |
14990b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
15000b22930eSAndy Lutomirski	 *
15010b22930eSAndy Lutomirski	 * The "original" frame is used by hardware.  Before re-enabling
15020b22930eSAndy Lutomirski	 * NMIs, we need to be done with it, and we need to leave enough
15030b22930eSAndy Lutomirski	 * space for the asm code here.
15040b22930eSAndy Lutomirski	 *
15050b22930eSAndy Lutomirski	 * We return by executing IRET while RSP points to the "iret" frame.
15060b22930eSAndy Lutomirski	 * That will either return for real or it will loop back into NMI
15070b22930eSAndy Lutomirski	 * processing.
15080b22930eSAndy Lutomirski	 *
15090b22930eSAndy Lutomirski	 * The "outermost" frame is copied to the "iret" frame on each
15100b22930eSAndy Lutomirski	 * iteration of the loop, so each iteration starts with the "iret"
15110b22930eSAndy Lutomirski	 * frame pointing to the final return target.
15120b22930eSAndy Lutomirski	 */
15130b22930eSAndy Lutomirski
15140b22930eSAndy Lutomirski	/*
15150b22930eSAndy Lutomirski	 * Determine whether we're a nested NMI.
15160b22930eSAndy Lutomirski	 *
1517a27507caSAndy Lutomirski	 * If we interrupted kernel code between repeat_nmi and
1518a27507caSAndy Lutomirski	 * end_repeat_nmi, then we are a nested NMI.  We must not
1519a27507caSAndy Lutomirski	 * modify the "iret" frame because it's being written by
1520a27507caSAndy Lutomirski	 * the outer NMI.  That's okay; the outer NMI handler is
1521a27507caSAndy Lutomirski	 * about to about to call do_nmi anyway, so we can just
1522a27507caSAndy Lutomirski	 * resume the outer NMI.
1523a27507caSAndy Lutomirski	 */
1524a27507caSAndy Lutomirski
1525a27507caSAndy Lutomirski	movq	$repeat_nmi, %rdx
1526a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1527a27507caSAndy Lutomirski	ja	1f
1528a27507caSAndy Lutomirski	movq	$end_repeat_nmi, %rdx
1529a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1530a27507caSAndy Lutomirski	ja	nested_nmi_out
1531a27507caSAndy Lutomirski1:
1532a27507caSAndy Lutomirski
1533a27507caSAndy Lutomirski	/*
1534a27507caSAndy Lutomirski	 * Now check "NMI executing".  If it's set, then we're nested.
15350b22930eSAndy Lutomirski	 * This will not detect if we interrupted an outer NMI just
15360b22930eSAndy Lutomirski	 * before IRET.
1537905a36a2SIngo Molnar	 */
1538905a36a2SIngo Molnar	cmpl	$1, -8(%rsp)
1539905a36a2SIngo Molnar	je	nested_nmi
1540905a36a2SIngo Molnar
1541905a36a2SIngo Molnar	/*
15420b22930eSAndy Lutomirski	 * Now test if the previous stack was an NMI stack.  This covers
15430b22930eSAndy Lutomirski	 * the case where we interrupt an outer NMI after it clears
1544810bc075SAndy Lutomirski	 * "NMI executing" but before IRET.  We need to be careful, though:
1545810bc075SAndy Lutomirski	 * there is one case in which RSP could point to the NMI stack
1546810bc075SAndy Lutomirski	 * despite there being no NMI active: naughty userspace controls
1547810bc075SAndy Lutomirski	 * RSP at the very beginning of the SYSCALL targets.  We can
1548810bc075SAndy Lutomirski	 * pull a fast one on naughty userspace, though: we program
1549810bc075SAndy Lutomirski	 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1550810bc075SAndy Lutomirski	 * if it controls the kernel's RSP.  We set DF before we clear
1551810bc075SAndy Lutomirski	 * "NMI executing".
1552905a36a2SIngo Molnar	 */
1553905a36a2SIngo Molnar	lea	6*8(%rsp), %rdx
1554905a36a2SIngo Molnar	/* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1555905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1556905a36a2SIngo Molnar	/* If the stack pointer is above the NMI stack, this is a normal NMI */
1557905a36a2SIngo Molnar	ja	first_nmi
15584d732138SIngo Molnar
1559905a36a2SIngo Molnar	subq	$EXCEPTION_STKSZ, %rdx
1560905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1561905a36a2SIngo Molnar	/* If it is below the NMI stack, it is a normal NMI */
1562905a36a2SIngo Molnar	jb	first_nmi
1563810bc075SAndy Lutomirski
1564810bc075SAndy Lutomirski	/* Ah, it is within the NMI stack. */
1565810bc075SAndy Lutomirski
1566810bc075SAndy Lutomirski	testb	$(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1567810bc075SAndy Lutomirski	jz	first_nmi	/* RSP was user controlled. */
1568810bc075SAndy Lutomirski
1569810bc075SAndy Lutomirski	/* This is a nested NMI. */
1570905a36a2SIngo Molnar
1571905a36a2SIngo Molnarnested_nmi:
1572905a36a2SIngo Molnar	/*
15730b22930eSAndy Lutomirski	 * Modify the "iret" frame to point to repeat_nmi, forcing another
15740b22930eSAndy Lutomirski	 * iteration of NMI handling.
1575905a36a2SIngo Molnar	 */
157623a781e9SAndy Lutomirski	subq	$8, %rsp
1577905a36a2SIngo Molnar	leaq	-10*8(%rsp), %rdx
1578905a36a2SIngo Molnar	pushq	$__KERNEL_DS
1579905a36a2SIngo Molnar	pushq	%rdx
1580905a36a2SIngo Molnar	pushfq
1581905a36a2SIngo Molnar	pushq	$__KERNEL_CS
1582905a36a2SIngo Molnar	pushq	$repeat_nmi
1583905a36a2SIngo Molnar
1584905a36a2SIngo Molnar	/* Put stack back */
1585905a36a2SIngo Molnar	addq	$(6*8), %rsp
1586905a36a2SIngo Molnar
1587905a36a2SIngo Molnarnested_nmi_out:
1588905a36a2SIngo Molnar	popq	%rdx
1589905a36a2SIngo Molnar
15900b22930eSAndy Lutomirski	/* We are returning to kernel mode, so this cannot result in a fault. */
1591929bacecSAndy Lutomirski	iretq
1592905a36a2SIngo Molnar
1593905a36a2SIngo Molnarfirst_nmi:
15940b22930eSAndy Lutomirski	/* Restore rdx. */
1595905a36a2SIngo Molnar	movq	(%rsp), %rdx
1596905a36a2SIngo Molnar
159736f1a77bSAndy Lutomirski	/* Make room for "NMI executing". */
159836f1a77bSAndy Lutomirski	pushq	$0
1599905a36a2SIngo Molnar
16000b22930eSAndy Lutomirski	/* Leave room for the "iret" frame */
1601905a36a2SIngo Molnar	subq	$(5*8), %rsp
1602905a36a2SIngo Molnar
16030b22930eSAndy Lutomirski	/* Copy the "original" frame to the "outermost" frame */
1604905a36a2SIngo Molnar	.rept 5
1605905a36a2SIngo Molnar	pushq	11*8(%rsp)
1606905a36a2SIngo Molnar	.endr
16078c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1608905a36a2SIngo Molnar
1609905a36a2SIngo Molnar	/* Everything up to here is safe from nested NMIs */
1610905a36a2SIngo Molnar
1611a97439aaSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
1612a97439aaSAndy Lutomirski	/*
1613a97439aaSAndy Lutomirski	 * For ease of testing, unmask NMIs right away.  Disabled by
1614a97439aaSAndy Lutomirski	 * default because IRET is very expensive.
1615a97439aaSAndy Lutomirski	 */
1616a97439aaSAndy Lutomirski	pushq	$0		/* SS */
1617a97439aaSAndy Lutomirski	pushq	%rsp		/* RSP (minus 8 because of the previous push) */
1618a97439aaSAndy Lutomirski	addq	$8, (%rsp)	/* Fix up RSP */
1619a97439aaSAndy Lutomirski	pushfq			/* RFLAGS */
1620a97439aaSAndy Lutomirski	pushq	$__KERNEL_CS	/* CS */
1621a97439aaSAndy Lutomirski	pushq	$1f		/* RIP */
1622929bacecSAndy Lutomirski	iretq			/* continues at repeat_nmi below */
16238c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1624a97439aaSAndy Lutomirski1:
1625a97439aaSAndy Lutomirski#endif
1626a97439aaSAndy Lutomirski
16270b22930eSAndy Lutomirskirepeat_nmi:
1628905a36a2SIngo Molnar	/*
1629905a36a2SIngo Molnar	 * If there was a nested NMI, the first NMI's iret will return
1630905a36a2SIngo Molnar	 * here. But NMIs are still enabled and we can take another
1631905a36a2SIngo Molnar	 * nested NMI. The nested NMI checks the interrupted RIP to see
1632905a36a2SIngo Molnar	 * if it is between repeat_nmi and end_repeat_nmi, and if so
1633905a36a2SIngo Molnar	 * it will just return, as we are about to repeat an NMI anyway.
1634905a36a2SIngo Molnar	 * This makes it safe to copy to the stack frame that a nested
1635905a36a2SIngo Molnar	 * NMI will update.
16360b22930eSAndy Lutomirski	 *
16370b22930eSAndy Lutomirski	 * RSP is pointing to "outermost RIP".  gsbase is unknown, but, if
16380b22930eSAndy Lutomirski	 * we're repeating an NMI, gsbase has the same value that it had on
16390b22930eSAndy Lutomirski	 * the first iteration.  paranoid_entry will load the kernel
164036f1a77bSAndy Lutomirski	 * gsbase if needed before we call do_nmi.  "NMI executing"
164136f1a77bSAndy Lutomirski	 * is zero.
1642905a36a2SIngo Molnar	 */
164336f1a77bSAndy Lutomirski	movq	$1, 10*8(%rsp)		/* Set "NMI executing". */
1644905a36a2SIngo Molnar
16450b22930eSAndy Lutomirski	/*
16460b22930eSAndy Lutomirski	 * Copy the "outermost" frame to the "iret" frame.  NMIs that nest
16470b22930eSAndy Lutomirski	 * here must not modify the "iret" frame while we're writing to
16480b22930eSAndy Lutomirski	 * it or it will end up containing garbage.
16490b22930eSAndy Lutomirski	 */
1650905a36a2SIngo Molnar	addq	$(10*8), %rsp
1651905a36a2SIngo Molnar	.rept 5
1652905a36a2SIngo Molnar	pushq	-6*8(%rsp)
1653905a36a2SIngo Molnar	.endr
1654905a36a2SIngo Molnar	subq	$(5*8), %rsp
1655905a36a2SIngo Molnarend_repeat_nmi:
1656905a36a2SIngo Molnar
1657905a36a2SIngo Molnar	/*
16580b22930eSAndy Lutomirski	 * Everything below this point can be preempted by a nested NMI.
16590b22930eSAndy Lutomirski	 * If this happens, then the inner NMI will change the "iret"
16600b22930eSAndy Lutomirski	 * frame to point back to repeat_nmi.
1661905a36a2SIngo Molnar	 */
1662905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
1663905a36a2SIngo Molnar
1664905a36a2SIngo Molnar	/*
1665905a36a2SIngo Molnar	 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1666905a36a2SIngo Molnar	 * as we should not be calling schedule in NMI context.
1667905a36a2SIngo Molnar	 * Even with normal interrupts enabled. An NMI should not be
1668905a36a2SIngo Molnar	 * setting NEED_RESCHED or anything that normal interrupts and
1669905a36a2SIngo Molnar	 * exceptions might do.
1670905a36a2SIngo Molnar	 */
1671905a36a2SIngo Molnar	call	paranoid_entry
16728c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
1673905a36a2SIngo Molnar
1674905a36a2SIngo Molnar	/* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1675905a36a2SIngo Molnar	movq	%rsp, %rdi
1676905a36a2SIngo Molnar	movq	$-1, %rsi
1677905a36a2SIngo Molnar	call	do_nmi
1678905a36a2SIngo Molnar
167916561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
168021e94459SPeter Zijlstra	RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
16818a09317bSDave Hansen
1682905a36a2SIngo Molnar	testl	%ebx, %ebx			/* swapgs needed? */
1683905a36a2SIngo Molnar	jnz	nmi_restore
1684905a36a2SIngo Molnarnmi_swapgs:
1685905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
1686905a36a2SIngo Molnarnmi_restore:
1687502af0d7SDominik Brodowski	POP_REGS
16880b22930eSAndy Lutomirski
1689471ee483SAndy Lutomirski	/*
1690471ee483SAndy Lutomirski	 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1691471ee483SAndy Lutomirski	 * at the "iret" frame.
1692471ee483SAndy Lutomirski	 */
1693471ee483SAndy Lutomirski	addq	$6*8, %rsp
1694905a36a2SIngo Molnar
1695810bc075SAndy Lutomirski	/*
1696810bc075SAndy Lutomirski	 * Clear "NMI executing".  Set DF first so that we can easily
1697810bc075SAndy Lutomirski	 * distinguish the remaining code between here and IRET from
1698929bacecSAndy Lutomirski	 * the SYSCALL entry and exit paths.
1699929bacecSAndy Lutomirski	 *
1700929bacecSAndy Lutomirski	 * We arguably should just inspect RIP instead, but I (Andy) wrote
1701929bacecSAndy Lutomirski	 * this code when I had the misapprehension that Xen PV supported
1702929bacecSAndy Lutomirski	 * NMIs, and Xen PV would break that approach.
1703810bc075SAndy Lutomirski	 */
1704810bc075SAndy Lutomirski	std
1705810bc075SAndy Lutomirski	movq	$0, 5*8(%rsp)		/* clear "NMI executing" */
17060b22930eSAndy Lutomirski
17070b22930eSAndy Lutomirski	/*
1708929bacecSAndy Lutomirski	 * iretq reads the "iret" frame and exits the NMI stack in a
1709929bacecSAndy Lutomirski	 * single instruction.  We are returning to kernel mode, so this
1710929bacecSAndy Lutomirski	 * cannot result in a fault.  Similarly, we don't need to worry
1711929bacecSAndy Lutomirski	 * about espfix64 on the way back to kernel mode.
17120b22930eSAndy Lutomirski	 */
1713929bacecSAndy Lutomirski	iretq
1714bc7b11c0SJiri SlabySYM_CODE_END(nmi)
1715905a36a2SIngo Molnar
1716dffb3f9dSAndy Lutomirski#ifndef CONFIG_IA32_EMULATION
1717dffb3f9dSAndy Lutomirski/*
1718dffb3f9dSAndy Lutomirski * This handles SYSCALL from 32-bit code.  There is no way to program
1719dffb3f9dSAndy Lutomirski * MSRs to fully disable 32-bit SYSCALL.
1720dffb3f9dSAndy Lutomirski */
1721bc7b11c0SJiri SlabySYM_CODE_START(ignore_sysret)
17228c1f7558SJosh Poimboeuf	UNWIND_HINT_EMPTY
1723905a36a2SIngo Molnar	mov	$-ENOSYS, %eax
1724b2b1d94cSJan Beulich	sysretl
1725bc7b11c0SJiri SlabySYM_CODE_END(ignore_sysret)
1726dffb3f9dSAndy Lutomirski#endif
17272deb4be2SAndy Lutomirski
1728b9f6976bSThomas Gleixner.pushsection .text, "ax"
1729bc7b11c0SJiri SlabySYM_CODE_START(rewind_stack_do_exit)
17308c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
17312deb4be2SAndy Lutomirski	/* Prevent any naive code from trying to unwind to our caller. */
17322deb4be2SAndy Lutomirski	xorl	%ebp, %ebp
17332deb4be2SAndy Lutomirski
17342deb4be2SAndy Lutomirski	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rax
17358c1f7558SJosh Poimboeuf	leaq	-PTREGS_SIZE(%rax), %rsp
1736f977df7bSJann Horn	UNWIND_HINT_REGS
17372deb4be2SAndy Lutomirski
17382deb4be2SAndy Lutomirski	call	do_exit
1739bc7b11c0SJiri SlabySYM_CODE_END(rewind_stack_do_exit)
1740b9f6976bSThomas Gleixner.popsection
1741