xref: /openbmc/linux/arch/x86/entry/entry_64.S (revision eb36b0dce2138581bc6b5e39d0273cb4c96ded81)
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 *
11ff61f079SJonathan Corbet * Some of this is documented in Documentation/arch/x86/entry_64.rst
12905a36a2SIngo Molnar *
13905a36a2SIngo Molnar * A note on terminology:
14905a36a2SIngo Molnar * - iret frame:	Architecture defined interrupt frame from SS to RIP
15905a36a2SIngo Molnar *			at the top of the kernel process stack.
16905a36a2SIngo Molnar *
17905a36a2SIngo Molnar * Some macro usage:
186dcc5627SJiri Slaby * - SYM_FUNC_START/END:Define functions in the symbol table.
194d732138SIngo Molnar * - idtentry:		Define exception entry points.
20905a36a2SIngo Molnar */
21905a36a2SIngo Molnar#include <linux/linkage.h>
22905a36a2SIngo Molnar#include <asm/segment.h>
23905a36a2SIngo Molnar#include <asm/cache.h>
24905a36a2SIngo Molnar#include <asm/errno.h>
25905a36a2SIngo Molnar#include <asm/asm-offsets.h>
26905a36a2SIngo Molnar#include <asm/msr.h>
27905a36a2SIngo Molnar#include <asm/unistd.h>
28905a36a2SIngo Molnar#include <asm/thread_info.h>
29905a36a2SIngo Molnar#include <asm/hw_irq.h>
30905a36a2SIngo Molnar#include <asm/page_types.h>
31905a36a2SIngo Molnar#include <asm/irqflags.h>
32905a36a2SIngo Molnar#include <asm/paravirt.h>
33905a36a2SIngo Molnar#include <asm/percpu.h>
34905a36a2SIngo Molnar#include <asm/asm.h>
35905a36a2SIngo Molnar#include <asm/smap.h>
36905a36a2SIngo Molnar#include <asm/pgtable_types.h>
37784d5699SAl Viro#include <asm/export.h>
388c1f7558SJosh Poimboeuf#include <asm/frame.h>
39cfa82a00SThomas Gleixner#include <asm/trapnr.h>
402641f08bSDavid Woodhouse#include <asm/nospec-branch.h>
41c82965f9SChang S. Bae#include <asm/fsgsbase.h>
42905a36a2SIngo Molnar#include <linux/err.h>
43905a36a2SIngo Molnar
446fd166aaSPeter Zijlstra#include "calling.h"
456fd166aaSPeter Zijlstra
46905a36a2SIngo Molnar.code64
47905a36a2SIngo Molnar.section .entry.text, "ax"
48905a36a2SIngo Molnar
49905a36a2SIngo Molnar/*
504d732138SIngo Molnar * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
51905a36a2SIngo Molnar *
52fda57b22SAndy Lutomirski * This is the only entry point used for 64-bit system calls.  The
53fda57b22SAndy Lutomirski * hardware interface is reasonably well designed and the register to
54fda57b22SAndy Lutomirski * argument mapping Linux uses fits well with the registers that are
55fda57b22SAndy Lutomirski * available when SYSCALL is used.
56fda57b22SAndy Lutomirski *
57fda57b22SAndy Lutomirski * SYSCALL instructions can be found inlined in libc implementations as
58fda57b22SAndy Lutomirski * well as some other programs and libraries.  There are also a handful
59fda57b22SAndy Lutomirski * of SYSCALL instructions in the vDSO used, for example, as a
60fda57b22SAndy Lutomirski * clock_gettimeofday fallback.
61fda57b22SAndy Lutomirski *
624d732138SIngo Molnar * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
63905a36a2SIngo Molnar * then loads new ss, cs, and rip from previously programmed MSRs.
64905a36a2SIngo Molnar * rflags gets masked by a value from another MSR (so CLD and CLAC
65905a36a2SIngo Molnar * are not needed). SYSCALL does not save anything on the stack
66905a36a2SIngo Molnar * and does not change rsp.
67905a36a2SIngo Molnar *
68905a36a2SIngo Molnar * Registers on entry:
69905a36a2SIngo Molnar * rax  system call number
70905a36a2SIngo Molnar * rcx  return address
71905a36a2SIngo Molnar * r11  saved rflags (note: r11 is callee-clobbered register in C ABI)
72905a36a2SIngo Molnar * rdi  arg0
73905a36a2SIngo Molnar * rsi  arg1
74905a36a2SIngo Molnar * rdx  arg2
75905a36a2SIngo Molnar * r10  arg3 (needs to be moved to rcx to conform to C ABI)
76905a36a2SIngo Molnar * r8   arg4
77905a36a2SIngo Molnar * r9   arg5
78905a36a2SIngo Molnar * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
79905a36a2SIngo Molnar *
80905a36a2SIngo Molnar * Only called from user space.
81905a36a2SIngo Molnar *
82905a36a2SIngo Molnar * When user can change pt_regs->foo always force IRET. That is because
83905a36a2SIngo Molnar * it deals with uncanonical addresses better. SYSRET has trouble
84905a36a2SIngo Molnar * with them due to bugs in both AMD and Intel CPUs.
85905a36a2SIngo Molnar */
86905a36a2SIngo Molnar
87bc7b11c0SJiri SlabySYM_CODE_START(entry_SYSCALL_64)
88a09a6e23SPeter Zijlstra	UNWIND_HINT_ENTRY
898f93402bSPeter Zijlstra	ENDBR
90905a36a2SIngo Molnar
918a9949bcSAndy Lutomirski	swapgs
92bf904d27SAndy Lutomirski	/* tss.sp2 is scratch space. */
9398f05b51SAndy Lutomirski	movq	%rsp, PER_CPU_VAR(cpu_tss_rw + TSS_sp2)
94bf904d27SAndy Lutomirski	SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
95c063a217SThomas Gleixner	movq	PER_CPU_VAR(pcpu_hot + X86_top_of_stack), %rsp
96905a36a2SIngo Molnar
97a13644f3SJoerg RoedelSYM_INNER_LABEL(entry_SYSCALL_64_safe_stack, SYM_L_GLOBAL)
98e8d61bdfSPeter Zijlstra	ANNOTATE_NOENDBR
99a13644f3SJoerg Roedel
100905a36a2SIngo Molnar	/* Construct struct pt_regs on stack */
101905a36a2SIngo Molnar	pushq	$__USER_DS				/* pt_regs->ss */
10298f05b51SAndy Lutomirski	pushq	PER_CPU_VAR(cpu_tss_rw + TSS_sp2)	/* pt_regs->sp */
103905a36a2SIngo Molnar	pushq	%r11					/* pt_regs->flags */
104905a36a2SIngo Molnar	pushq	$__USER_CS				/* pt_regs->cs */
105905a36a2SIngo Molnar	pushq	%rcx					/* pt_regs->ip */
10626ba4e57SJiri SlabySYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL)
107905a36a2SIngo Molnar	pushq	%rax					/* pt_regs->orig_ax */
10830907fd1SDominik Brodowski
10930907fd1SDominik Brodowski	PUSH_AND_CLEAR_REGS rax=$-ENOSYS
110905a36a2SIngo Molnar
1111e423bffSAndy Lutomirski	/* IRQs are off. */
1123e5e7f77SH. Peter Anvin (Intel)	movq	%rsp, %rdi
11305954948SH. Peter Anvin (Intel)	/* Sign extend the lower 32bit as syscall numbers are treated as int */
11405954948SH. Peter Anvin (Intel)	movslq	%eax, %rsi
1152dbb887eSPeter Zijlstra
1162dbb887eSPeter Zijlstra	/* clobbers %rax, make sure it is after saving the syscall nr */
1172dbb887eSPeter Zijlstra	IBRS_ENTER
1182dbb887eSPeter Zijlstra	UNTRAIN_RET
119*eb36b0dcSPawan Gupta	CLEAR_BRANCH_HISTORY
1202dbb887eSPeter Zijlstra
1211e423bffSAndy Lutomirski	call	do_syscall_64		/* returns with IRQs disabled */
1221e423bffSAndy Lutomirski
123905a36a2SIngo Molnar	/*
124905a36a2SIngo Molnar	 * Try to use SYSRET instead of IRET if we're returning to
1258a055d7fSAndy Lutomirski	 * a completely clean 64-bit userspace context.  If we're not,
1268a055d7fSAndy Lutomirski	 * go to the slow exit path.
127afd30525SJuergen Gross	 * In the Xen PV case we must use iret anyway.
128905a36a2SIngo Molnar	 */
129afd30525SJuergen Gross
130afd30525SJuergen Gross	ALTERNATIVE "", "jmp	swapgs_restore_regs_and_return_to_usermode", \
131afd30525SJuergen Gross		X86_FEATURE_XENPV
132afd30525SJuergen Gross
133905a36a2SIngo Molnar	movq	RCX(%rsp), %rcx
134905a36a2SIngo Molnar	movq	RIP(%rsp), %r11
1358a055d7fSAndy Lutomirski
1368a055d7fSAndy Lutomirski	cmpq	%rcx, %r11	/* SYSRET requires RCX == RIP */
1378a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
138905a36a2SIngo Molnar
139905a36a2SIngo Molnar	/*
140905a36a2SIngo Molnar	 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
141905a36a2SIngo Molnar	 * in kernel space.  This essentially lets the user take over
142905a36a2SIngo Molnar	 * the kernel, since userspace controls RSP.
143905a36a2SIngo Molnar	 *
144905a36a2SIngo Molnar	 * If width of "canonical tail" ever becomes variable, this will need
145905a36a2SIngo Molnar	 * to be updated to remain correct on both old and new CPUs.
146361b4b58SKirill A. Shutemov	 *
147cbe0317bSKirill A. Shutemov	 * Change top bits to match most significant bit (47th or 56th bit
148cbe0317bSKirill A. Shutemov	 * depending on paging mode) in the address.
149905a36a2SIngo Molnar	 */
15009e61a77SKirill A. Shutemov#ifdef CONFIG_X86_5LEVEL
15139b95522SKirill A. Shutemov	ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \
15239b95522SKirill A. Shutemov		"shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57
15309e61a77SKirill A. Shutemov#else
154905a36a2SIngo Molnar	shl	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
155905a36a2SIngo Molnar	sar	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
15609e61a77SKirill A. Shutemov#endif
1574d732138SIngo Molnar
158905a36a2SIngo Molnar	/* If this changed %rcx, it was not canonical */
159905a36a2SIngo Molnar	cmpq	%rcx, %r11
1608a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
161905a36a2SIngo Molnar
162905a36a2SIngo Molnar	cmpq	$__USER_CS, CS(%rsp)		/* CS must match SYSRET */
1638a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
164905a36a2SIngo Molnar
165905a36a2SIngo Molnar	movq	R11(%rsp), %r11
166905a36a2SIngo Molnar	cmpq	%r11, EFLAGS(%rsp)		/* R11 == RFLAGS */
1678a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
168905a36a2SIngo Molnar
169905a36a2SIngo Molnar	/*
1703e035305SBorislav Petkov	 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
1713e035305SBorislav Petkov	 * restore RF properly. If the slowpath sets it for whatever reason, we
1723e035305SBorislav Petkov	 * need to restore it correctly.
1733e035305SBorislav Petkov	 *
1743e035305SBorislav Petkov	 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
1753e035305SBorislav Petkov	 * trap from userspace immediately after SYSRET.  This would cause an
1763e035305SBorislav Petkov	 * infinite loop whenever #DB happens with register state that satisfies
1773e035305SBorislav Petkov	 * the opportunistic SYSRET conditions.  For example, single-stepping
1783e035305SBorislav Petkov	 * this user code:
179905a36a2SIngo Molnar	 *
180905a36a2SIngo Molnar	 *           movq	$stuck_here, %rcx
181905a36a2SIngo Molnar	 *           pushfq
182905a36a2SIngo Molnar	 *           popq %r11
183905a36a2SIngo Molnar	 *   stuck_here:
184905a36a2SIngo Molnar	 *
185905a36a2SIngo Molnar	 * would never get past 'stuck_here'.
186905a36a2SIngo Molnar	 */
187905a36a2SIngo Molnar	testq	$(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
1888a055d7fSAndy Lutomirski	jnz	swapgs_restore_regs_and_return_to_usermode
189905a36a2SIngo Molnar
190905a36a2SIngo Molnar	/* nothing to check for RSP */
191905a36a2SIngo Molnar
192905a36a2SIngo Molnar	cmpq	$__USER_DS, SS(%rsp)		/* SS must match SYSRET */
1938a055d7fSAndy Lutomirski	jne	swapgs_restore_regs_and_return_to_usermode
194905a36a2SIngo Molnar
195905a36a2SIngo Molnar	/*
196905a36a2SIngo Molnar	 * We win! This label is here just for ease of understanding
197905a36a2SIngo Molnar	 * perf profiles. Nothing jumps here.
198905a36a2SIngo Molnar	 */
199905a36a2SIngo Molnarsyscall_return_via_sysret:
2002dbb887eSPeter Zijlstra	IBRS_EXIT
2011b331eeeSPeter Zijlstra	POP_REGS pop_rdi=0
2023e3b9293SAndy Lutomirski
2033e3b9293SAndy Lutomirski	/*
2043e3b9293SAndy Lutomirski	 * Now all regs are restored except RSP and RDI.
2053e3b9293SAndy Lutomirski	 * Save old stack pointer and switch to trampoline stack.
2063e3b9293SAndy Lutomirski	 */
2073e3b9293SAndy Lutomirski	movq	%rsp, %rdi
208c482feefSAndy Lutomirski	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
209fb799447SJosh Poimboeuf	UNWIND_HINT_END_OF_STACK
2103e3b9293SAndy Lutomirski
2113e3b9293SAndy Lutomirski	pushq	RSP-RDI(%rdi)	/* RSP */
2123e3b9293SAndy Lutomirski	pushq	(%rdi)		/* RDI */
2133e3b9293SAndy Lutomirski
2143e3b9293SAndy Lutomirski	/*
2153e3b9293SAndy Lutomirski	 * We are on the trampoline stack.  All regs except RDI are live.
2163e3b9293SAndy Lutomirski	 * We can do future final exit work right here.
2173e3b9293SAndy Lutomirski	 */
218afaef01cSAlexander Popov	STACKLEAK_ERASE_NOCLOBBER
219afaef01cSAlexander Popov
2206fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
2213e3b9293SAndy Lutomirski
2224fbb3910SAndy Lutomirski	popq	%rdi
2233e3b9293SAndy Lutomirski	popq	%rsp
22447f33de4SLai JiangshanSYM_INNER_LABEL(entry_SYSRETQ_unsafe_stack, SYM_L_GLOBAL)
225ce656528SPeter Zijlstra	ANNOTATE_NOENDBR
226afd30525SJuergen Gross	swapgs
2277caf330fSPawan Gupta	CLEAR_CPU_BUFFERS
228afd30525SJuergen Gross	sysretq
22947f33de4SLai JiangshanSYM_INNER_LABEL(entry_SYSRETQ_end, SYM_L_GLOBAL)
230ce656528SPeter Zijlstra	ANNOTATE_NOENDBR
231ce656528SPeter Zijlstra	int3
232bc7b11c0SJiri SlabySYM_CODE_END(entry_SYSCALL_64)
233905a36a2SIngo Molnar
234905a36a2SIngo Molnar/*
2350100301bSBrian Gerst * %rdi: prev task
2360100301bSBrian Gerst * %rsi: next task
2370100301bSBrian Gerst */
238b9f6976bSThomas Gleixner.pushsection .text, "ax"
23996c64806SJosh PoimboeufSYM_FUNC_START(__switch_to_asm)
2400100301bSBrian Gerst	/*
2410100301bSBrian Gerst	 * Save callee-saved registers
2420100301bSBrian Gerst	 * This must match the order in inactive_task_frame
2430100301bSBrian Gerst	 */
2440100301bSBrian Gerst	pushq	%rbp
2450100301bSBrian Gerst	pushq	%rbx
2460100301bSBrian Gerst	pushq	%r12
2470100301bSBrian Gerst	pushq	%r13
2480100301bSBrian Gerst	pushq	%r14
2490100301bSBrian Gerst	pushq	%r15
2500100301bSBrian Gerst
2510100301bSBrian Gerst	/* switch stack */
2520100301bSBrian Gerst	movq	%rsp, TASK_threadsp(%rdi)
2530100301bSBrian Gerst	movq	TASK_threadsp(%rsi), %rsp
2540100301bSBrian Gerst
255050e9baaSLinus Torvalds#ifdef CONFIG_STACKPROTECTOR
2560100301bSBrian Gerst	movq	TASK_stack_canary(%rsi), %rbx
2575b71ac8aSPeter Zijlstra (Intel)	movq	%rbx, PER_CPU_VAR(fixed_percpu_data) + FIXED_stack_canary
2580100301bSBrian Gerst#endif
2590100301bSBrian Gerst
260c995efd5SDavid Woodhouse	/*
261c995efd5SDavid Woodhouse	 * When switching from a shallower to a deeper call stack
262c995efd5SDavid Woodhouse	 * the RSB may either underflow or use entries populated
263c995efd5SDavid Woodhouse	 * with userspace addresses. On CPUs where those concerns
264c995efd5SDavid Woodhouse	 * exist, overwrite the RSB with entries which capture
265c995efd5SDavid Woodhouse	 * speculative execution to prevent attack.
266c995efd5SDavid Woodhouse	 */
267d1c99108SDavid Woodhouse	FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
268c995efd5SDavid Woodhouse
2690100301bSBrian Gerst	/* restore callee-saved registers */
2700100301bSBrian Gerst	popq	%r15
2710100301bSBrian Gerst	popq	%r14
2720100301bSBrian Gerst	popq	%r13
2730100301bSBrian Gerst	popq	%r12
2740100301bSBrian Gerst	popq	%rbx
2750100301bSBrian Gerst	popq	%rbp
2760100301bSBrian Gerst
2770100301bSBrian Gerst	jmp	__switch_to
27896c64806SJosh PoimboeufSYM_FUNC_END(__switch_to_asm)
279b9f6976bSThomas Gleixner.popsection
2800100301bSBrian Gerst
2810100301bSBrian Gerst/*
282905a36a2SIngo Molnar * A newly forked process directly context switches into this address.
283905a36a2SIngo Molnar *
2840100301bSBrian Gerst * rax: prev task we switched from
285616d2483SBrian Gerst * rbx: kernel thread func (NULL for user thread)
286616d2483SBrian Gerst * r12: kernel thread arg
287905a36a2SIngo Molnar */
288b9f6976bSThomas Gleixner.pushsection .text, "ax"
2893aec4ecbSBrian GerstSYM_CODE_START(ret_from_fork_asm)
2902e7e5bbbSPeter Zijlstra	/*
2912e7e5bbbSPeter Zijlstra	 * This is the start of the kernel stack; even through there's a
2922e7e5bbbSPeter Zijlstra	 * register set at the top, the regset isn't necessarily coherent
2932e7e5bbbSPeter Zijlstra	 * (consider kthreads) and one cannot unwind further.
2942e7e5bbbSPeter Zijlstra	 *
2952e7e5bbbSPeter Zijlstra	 * This ensures stack unwinds of kernel threads terminate in a known
2962e7e5bbbSPeter Zijlstra	 * good state.
2972e7e5bbbSPeter Zijlstra	 */
2982e7e5bbbSPeter Zijlstra	UNWIND_HINT_END_OF_STACK
2993e3f0695SPeter Zijlstra	ANNOTATE_NOENDBR // copy_thread
3005d821386SThomas Gleixner	CALL_DEPTH_ACCOUNT
301905a36a2SIngo Molnar
3023aec4ecbSBrian Gerst	movq	%rax, %rdi		/* prev */
3033aec4ecbSBrian Gerst	movq	%rsp, %rsi		/* regs */
3043aec4ecbSBrian Gerst	movq	%rbx, %rdx		/* fn */
3053aec4ecbSBrian Gerst	movq	%r12, %rcx		/* fn_arg */
3063aec4ecbSBrian Gerst	call	ret_from_fork
307905a36a2SIngo Molnar
3082e7e5bbbSPeter Zijlstra	/*
3092e7e5bbbSPeter Zijlstra	 * Set the stack state to what is expected for the target function
3102e7e5bbbSPeter Zijlstra	 * -- at this point the register set should be a valid user set
3112e7e5bbbSPeter Zijlstra	 * and unwind should work normally.
3122e7e5bbbSPeter Zijlstra	 */
3132e7e5bbbSPeter Zijlstra	UNWIND_HINT_REGS
3148a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
3153aec4ecbSBrian GerstSYM_CODE_END(ret_from_fork_asm)
316b9f6976bSThomas Gleixner.popsection
317905a36a2SIngo Molnar
3181d3e53e8SAndy Lutomirski.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
3191d3e53e8SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
320e17f8234SBoris Ostrovsky	pushq %rax
321fafe5e74SJuergen Gross	SAVE_FLAGS
322e17f8234SBoris Ostrovsky	testl $X86_EFLAGS_IF, %eax
3231d3e53e8SAndy Lutomirski	jz .Lokay_\@
3241d3e53e8SAndy Lutomirski	ud2
3251d3e53e8SAndy Lutomirski.Lokay_\@:
326e17f8234SBoris Ostrovsky	popq %rax
3271d3e53e8SAndy Lutomirski#endif
3281d3e53e8SAndy Lutomirski.endm
3291d3e53e8SAndy Lutomirski
330c22cf380SThomas GleixnerSYM_CODE_START(xen_error_entry)
331c22cf380SThomas Gleixner	ANNOTATE_NOENDBR
332d147553bSPeter Zijlstra	UNWIND_HINT_FUNC
3332c08b9b3SPeter Zijlstra	PUSH_AND_CLEAR_REGS save_ret=1
3342c08b9b3SPeter Zijlstra	ENCODE_FRAME_POINTER 8
3355d821386SThomas Gleixner	UNTRAIN_RET_FROM_CALL
336d147553bSPeter Zijlstra	RET
337d147553bSPeter ZijlstraSYM_CODE_END(xen_error_entry)
338d147553bSPeter Zijlstra
339cfa82a00SThomas Gleixner/**
340cfa82a00SThomas Gleixner * idtentry_body - Macro to emit code calling the C function
341cfa82a00SThomas Gleixner * @cfunc:		C function to be called
342cfa82a00SThomas Gleixner * @has_error_code:	Hardware pushed error code on stack
343cfa82a00SThomas Gleixner */
344e2dcb5f1SThomas Gleixner.macro idtentry_body cfunc has_error_code:req
345cfa82a00SThomas Gleixner
34664cbd0acSLai Jiangshan	/*
34764cbd0acSLai Jiangshan	 * Call error_entry() and switch to the task stack if from userspace.
34864cbd0acSLai Jiangshan	 *
34964cbd0acSLai Jiangshan	 * When in XENPV, it is already in the task stack, and it can't fault
35064cbd0acSLai Jiangshan	 * for native_iret() nor native_load_gs_index() since XENPV uses its
35164cbd0acSLai Jiangshan	 * own pvops for IRET and load_gs_index().  And it doesn't need to
35264cbd0acSLai Jiangshan	 * switch the CR3.  So it can skip invoking error_entry().
35364cbd0acSLai Jiangshan	 */
35464cbd0acSLai Jiangshan	ALTERNATIVE "call error_entry; movq %rax, %rsp", \
355d147553bSPeter Zijlstra		    "call xen_error_entry", X86_FEATURE_XENPV
35664cbd0acSLai Jiangshan
357520a7e80SLai Jiangshan	ENCODE_FRAME_POINTER
358cfa82a00SThomas Gleixner	UNWIND_HINT_REGS
359cfa82a00SThomas Gleixner
360cfa82a00SThomas Gleixner	movq	%rsp, %rdi			/* pt_regs pointer into 1st argument*/
361cfa82a00SThomas Gleixner
362cfa82a00SThomas Gleixner	.if \has_error_code == 1
363cfa82a00SThomas Gleixner		movq	ORIG_RAX(%rsp), %rsi	/* get error code into 2nd argument*/
364cfa82a00SThomas Gleixner		movq	$-1, ORIG_RAX(%rsp)	/* no syscall to restart */
365cfa82a00SThomas Gleixner	.endif
366cfa82a00SThomas Gleixner
367cfa82a00SThomas Gleixner	call	\cfunc
368cfa82a00SThomas Gleixner
369d66e9d50SPeter Zijlstra	/* For some configurations \cfunc ends up being a noreturn. */
370d66e9d50SPeter Zijlstra	REACHABLE
371d66e9d50SPeter Zijlstra
372424c7d0aSThomas Gleixner	jmp	error_return
373cfa82a00SThomas Gleixner.endm
374cfa82a00SThomas Gleixner
375cfa82a00SThomas Gleixner/**
376cfa82a00SThomas Gleixner * idtentry - Macro to generate entry stubs for simple IDT entries
377cfa82a00SThomas Gleixner * @vector:		Vector number
378cfa82a00SThomas Gleixner * @asmsym:		ASM symbol for the entry point
379cfa82a00SThomas Gleixner * @cfunc:		C function to be called
380cfa82a00SThomas Gleixner * @has_error_code:	Hardware pushed error code on stack
381cfa82a00SThomas Gleixner *
382cfa82a00SThomas Gleixner * The macro emits code to set up the kernel context for straight forward
383cfa82a00SThomas Gleixner * and simple IDT entries. No IST stack, no paranoid entry checks.
384cfa82a00SThomas Gleixner */
385e2dcb5f1SThomas Gleixner.macro idtentry vector asmsym cfunc has_error_code:req
386cfa82a00SThomas GleixnerSYM_CODE_START(\asmsym)
38737064583SJosh Poimboeuf
38837064583SJosh Poimboeuf	.if \vector == X86_TRAP_BP
38937064583SJosh Poimboeuf		/* #BP advances %rip to the next instruction */
3904708ea14SJosh Poimboeuf		UNWIND_HINT_IRET_ENTRY offset=\has_error_code*8 signal=0
39137064583SJosh Poimboeuf	.else
3924708ea14SJosh Poimboeuf		UNWIND_HINT_IRET_ENTRY offset=\has_error_code*8
39337064583SJosh Poimboeuf	.endif
39437064583SJosh Poimboeuf
3958f93402bSPeter Zijlstra	ENDBR
396cfa82a00SThomas Gleixner	ASM_CLAC
397c64cc280SLai Jiangshan	cld
398cfa82a00SThomas Gleixner
399cfa82a00SThomas Gleixner	.if \has_error_code == 0
400cfa82a00SThomas Gleixner		pushq	$-1			/* ORIG_RAX: no syscall to restart */
401cfa82a00SThomas Gleixner	.endif
402cfa82a00SThomas Gleixner
403cfa82a00SThomas Gleixner	.if \vector == X86_TRAP_BP
404cfa82a00SThomas Gleixner		/*
405cfa82a00SThomas Gleixner		 * If coming from kernel space, create a 6-word gap to allow the
406cfa82a00SThomas Gleixner		 * int3 handler to emulate a call instruction.
407cfa82a00SThomas Gleixner		 */
408cfa82a00SThomas Gleixner		testb	$3, CS-ORIG_RAX(%rsp)
409cfa82a00SThomas Gleixner		jnz	.Lfrom_usermode_no_gap_\@
410cfa82a00SThomas Gleixner		.rept	6
411cfa82a00SThomas Gleixner		pushq	5*8(%rsp)
412cfa82a00SThomas Gleixner		.endr
413cfa82a00SThomas Gleixner		UNWIND_HINT_IRET_REGS offset=8
414cfa82a00SThomas Gleixner.Lfrom_usermode_no_gap_\@:
415cfa82a00SThomas Gleixner	.endif
416cfa82a00SThomas Gleixner
417e2dcb5f1SThomas Gleixner	idtentry_body \cfunc \has_error_code
418cfa82a00SThomas Gleixner
419cfa82a00SThomas Gleixner_ASM_NOKPROBE(\asmsym)
420cfa82a00SThomas GleixnerSYM_CODE_END(\asmsym)
421cfa82a00SThomas Gleixner.endm
422cfa82a00SThomas Gleixner
423cfa82a00SThomas Gleixner/*
4240bf7c314SThomas Gleixner * Interrupt entry/exit.
4250bf7c314SThomas Gleixner *
4260bf7c314SThomas Gleixner + The interrupt stubs push (vector) onto the stack, which is the error_code
4270bf7c314SThomas Gleixner * position of idtentry exceptions, and jump to one of the two idtentry points
4280bf7c314SThomas Gleixner * (common/spurious).
4290bf7c314SThomas Gleixner *
4300bf7c314SThomas Gleixner * common_interrupt is a hotpath, align it to a cache line
4310bf7c314SThomas Gleixner */
4320bf7c314SThomas Gleixner.macro idtentry_irq vector cfunc
4330bf7c314SThomas Gleixner	.p2align CONFIG_X86_L1_CACHE_SHIFT
4340bf7c314SThomas Gleixner	idtentry \vector asm_\cfunc \cfunc has_error_code=1
4350bf7c314SThomas Gleixner.endm
4360bf7c314SThomas Gleixner
4370bf7c314SThomas Gleixner/*
4386368558cSThomas Gleixner * System vectors which invoke their handlers directly and are not
4396368558cSThomas Gleixner * going through the regular common device interrupt handling code.
4406368558cSThomas Gleixner */
4416368558cSThomas Gleixner.macro idtentry_sysvec vector cfunc
4426368558cSThomas Gleixner	idtentry \vector asm_\cfunc \cfunc has_error_code=0
4436368558cSThomas Gleixner.endm
4446368558cSThomas Gleixner
445cfa82a00SThomas Gleixner/**
446cfa82a00SThomas Gleixner * idtentry_mce_db - Macro to generate entry stubs for #MC and #DB
447cfa82a00SThomas Gleixner * @vector:		Vector number
448cfa82a00SThomas Gleixner * @asmsym:		ASM symbol for the entry point
449cfa82a00SThomas Gleixner * @cfunc:		C function to be called
450cfa82a00SThomas Gleixner *
451cfa82a00SThomas Gleixner * The macro emits code to set up the kernel context for #MC and #DB
452cfa82a00SThomas Gleixner *
453cfa82a00SThomas Gleixner * If the entry comes from user space it uses the normal entry path
454cfa82a00SThomas Gleixner * including the return to user space work and preemption checks on
455cfa82a00SThomas Gleixner * exit.
456cfa82a00SThomas Gleixner *
457cfa82a00SThomas Gleixner * If hits in kernel mode then it needs to go through the paranoid
458cfa82a00SThomas Gleixner * entry as the exception can hit any random state. No preemption
459cfa82a00SThomas Gleixner * check on exit to keep the paranoid path simple.
460cfa82a00SThomas Gleixner */
461cfa82a00SThomas Gleixner.macro idtentry_mce_db vector asmsym cfunc
462cfa82a00SThomas GleixnerSYM_CODE_START(\asmsym)
4634708ea14SJosh Poimboeuf	UNWIND_HINT_IRET_ENTRY
4648f93402bSPeter Zijlstra	ENDBR
465cfa82a00SThomas Gleixner	ASM_CLAC
466c64cc280SLai Jiangshan	cld
467cfa82a00SThomas Gleixner
468cfa82a00SThomas Gleixner	pushq	$-1			/* ORIG_RAX: no syscall to restart */
469cfa82a00SThomas Gleixner
470cfa82a00SThomas Gleixner	/*
471cfa82a00SThomas Gleixner	 * If the entry is from userspace, switch stacks and treat it as
472cfa82a00SThomas Gleixner	 * a normal entry.
473cfa82a00SThomas Gleixner	 */
474cfa82a00SThomas Gleixner	testb	$3, CS-ORIG_RAX(%rsp)
475cfa82a00SThomas Gleixner	jnz	.Lfrom_usermode_switch_stack_\@
476cfa82a00SThomas Gleixner
477c82965f9SChang S. Bae	/* paranoid_entry returns GS information for paranoid_exit in EBX. */
478cfa82a00SThomas Gleixner	call	paranoid_entry
479cfa82a00SThomas Gleixner
480cfa82a00SThomas Gleixner	UNWIND_HINT_REGS
481cfa82a00SThomas Gleixner
482cfa82a00SThomas Gleixner	movq	%rsp, %rdi		/* pt_regs pointer */
483cfa82a00SThomas Gleixner
484cfa82a00SThomas Gleixner	call	\cfunc
485cfa82a00SThomas Gleixner
486cfa82a00SThomas Gleixner	jmp	paranoid_exit
487cfa82a00SThomas Gleixner
488cfa82a00SThomas Gleixner	/* Switch to the regular task stack and use the noist entry point */
489cfa82a00SThomas Gleixner.Lfrom_usermode_switch_stack_\@:
490e2dcb5f1SThomas Gleixner	idtentry_body noist_\cfunc, has_error_code=0
491cfa82a00SThomas Gleixner
492cfa82a00SThomas Gleixner_ASM_NOKPROBE(\asmsym)
493cfa82a00SThomas GleixnerSYM_CODE_END(\asmsym)
494cfa82a00SThomas Gleixner.endm
495cfa82a00SThomas Gleixner
496a13644f3SJoerg Roedel#ifdef CONFIG_AMD_MEM_ENCRYPT
497a13644f3SJoerg Roedel/**
498a13644f3SJoerg Roedel * idtentry_vc - Macro to generate entry stub for #VC
499a13644f3SJoerg Roedel * @vector:		Vector number
500a13644f3SJoerg Roedel * @asmsym:		ASM symbol for the entry point
501a13644f3SJoerg Roedel * @cfunc:		C function to be called
502a13644f3SJoerg Roedel *
503a13644f3SJoerg Roedel * The macro emits code to set up the kernel context for #VC. The #VC handler
504a13644f3SJoerg Roedel * runs on an IST stack and needs to be able to cause nested #VC exceptions.
505a13644f3SJoerg Roedel *
506a13644f3SJoerg Roedel * To make this work the #VC entry code tries its best to pretend it doesn't use
507a13644f3SJoerg Roedel * an IST stack by switching to the task stack if coming from user-space (which
508a13644f3SJoerg Roedel * includes early SYSCALL entry path) or back to the stack in the IRET frame if
509a13644f3SJoerg Roedel * entered from kernel-mode.
510a13644f3SJoerg Roedel *
511a13644f3SJoerg Roedel * If entered from kernel-mode the return stack is validated first, and if it is
512a13644f3SJoerg Roedel * not safe to use (e.g. because it points to the entry stack) the #VC handler
513a13644f3SJoerg Roedel * will switch to a fall-back stack (VC2) and call a special handler function.
514a13644f3SJoerg Roedel *
515a13644f3SJoerg Roedel * The macro is only used for one vector, but it is planned to be extended in
516a13644f3SJoerg Roedel * the future for the #HV exception.
517a13644f3SJoerg Roedel */
518a13644f3SJoerg Roedel.macro idtentry_vc vector asmsym cfunc
519a13644f3SJoerg RoedelSYM_CODE_START(\asmsym)
5204708ea14SJosh Poimboeuf	UNWIND_HINT_IRET_ENTRY
5218f93402bSPeter Zijlstra	ENDBR
522a13644f3SJoerg Roedel	ASM_CLAC
523c64cc280SLai Jiangshan	cld
524a13644f3SJoerg Roedel
525a13644f3SJoerg Roedel	/*
526a13644f3SJoerg Roedel	 * If the entry is from userspace, switch stacks and treat it as
527a13644f3SJoerg Roedel	 * a normal entry.
528a13644f3SJoerg Roedel	 */
529a13644f3SJoerg Roedel	testb	$3, CS-ORIG_RAX(%rsp)
530a13644f3SJoerg Roedel	jnz	.Lfrom_usermode_switch_stack_\@
531a13644f3SJoerg Roedel
532a13644f3SJoerg Roedel	/*
533a13644f3SJoerg Roedel	 * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX.
534a13644f3SJoerg Roedel	 * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS
535a13644f3SJoerg Roedel	 */
536a13644f3SJoerg Roedel	call	paranoid_entry
537a13644f3SJoerg Roedel
538a13644f3SJoerg Roedel	UNWIND_HINT_REGS
539a13644f3SJoerg Roedel
540a13644f3SJoerg Roedel	/*
541a13644f3SJoerg Roedel	 * Switch off the IST stack to make it free for nested exceptions. The
542a13644f3SJoerg Roedel	 * vc_switch_off_ist() function will switch back to the interrupted
543a13644f3SJoerg Roedel	 * stack if it is safe to do so. If not it switches to the VC fall-back
544a13644f3SJoerg Roedel	 * stack.
545a13644f3SJoerg Roedel	 */
546a13644f3SJoerg Roedel	movq	%rsp, %rdi		/* pt_regs pointer */
547a13644f3SJoerg Roedel	call	vc_switch_off_ist
548a13644f3SJoerg Roedel	movq	%rax, %rsp		/* Switch to new stack */
549a13644f3SJoerg Roedel
550c42b1451SLai Jiangshan	ENCODE_FRAME_POINTER
551a13644f3SJoerg Roedel	UNWIND_HINT_REGS
552a13644f3SJoerg Roedel
553a13644f3SJoerg Roedel	/* Update pt_regs */
554a13644f3SJoerg Roedel	movq	ORIG_RAX(%rsp), %rsi	/* get error code into 2nd argument*/
555a13644f3SJoerg Roedel	movq	$-1, ORIG_RAX(%rsp)	/* no syscall to restart */
556a13644f3SJoerg Roedel
557a13644f3SJoerg Roedel	movq	%rsp, %rdi		/* pt_regs pointer */
558a13644f3SJoerg Roedel
559be1a5408SJoerg Roedel	call	kernel_\cfunc
560a13644f3SJoerg Roedel
561a13644f3SJoerg Roedel	/*
562a13644f3SJoerg Roedel	 * No need to switch back to the IST stack. The current stack is either
563a13644f3SJoerg Roedel	 * identical to the stack in the IRET frame or the VC fall-back stack,
564163b0991SIngo Molnar	 * so it is definitely mapped even with PTI enabled.
565a13644f3SJoerg Roedel	 */
566a13644f3SJoerg Roedel	jmp	paranoid_exit
567a13644f3SJoerg Roedel
568a13644f3SJoerg Roedel	/* Switch to the regular task stack */
569a13644f3SJoerg Roedel.Lfrom_usermode_switch_stack_\@:
570be1a5408SJoerg Roedel	idtentry_body user_\cfunc, has_error_code=1
571a13644f3SJoerg Roedel
572a13644f3SJoerg Roedel_ASM_NOKPROBE(\asmsym)
573a13644f3SJoerg RoedelSYM_CODE_END(\asmsym)
574a13644f3SJoerg Roedel.endm
575a13644f3SJoerg Roedel#endif
576a13644f3SJoerg Roedel
577cfa82a00SThomas Gleixner/*
578cfa82a00SThomas Gleixner * Double fault entry. Straight paranoid. No checks from which context
579cfa82a00SThomas Gleixner * this comes because for the espfix induced #DF this would do the wrong
580cfa82a00SThomas Gleixner * thing.
581cfa82a00SThomas Gleixner */
582cfa82a00SThomas Gleixner.macro idtentry_df vector asmsym cfunc
583cfa82a00SThomas GleixnerSYM_CODE_START(\asmsym)
5844708ea14SJosh Poimboeuf	UNWIND_HINT_IRET_ENTRY offset=8
5858f93402bSPeter Zijlstra	ENDBR
586cfa82a00SThomas Gleixner	ASM_CLAC
587c64cc280SLai Jiangshan	cld
588cfa82a00SThomas Gleixner
589c82965f9SChang S. Bae	/* paranoid_entry returns GS information for paranoid_exit in EBX. */
590cfa82a00SThomas Gleixner	call	paranoid_entry
591cfa82a00SThomas Gleixner	UNWIND_HINT_REGS
592cfa82a00SThomas Gleixner
593cfa82a00SThomas Gleixner	movq	%rsp, %rdi		/* pt_regs pointer into first argument */
594cfa82a00SThomas Gleixner	movq	ORIG_RAX(%rsp), %rsi	/* get error code into 2nd argument*/
595cfa82a00SThomas Gleixner	movq	$-1, ORIG_RAX(%rsp)	/* no syscall to restart */
596cfa82a00SThomas Gleixner	call	\cfunc
597cfa82a00SThomas Gleixner
5983515899bSPeter Zijlstra	/* For some configurations \cfunc ends up being a noreturn. */
5993515899bSPeter Zijlstra	REACHABLE
6003515899bSPeter Zijlstra
601cfa82a00SThomas Gleixner	jmp	paranoid_exit
602cfa82a00SThomas Gleixner
603cfa82a00SThomas Gleixner_ASM_NOKPROBE(\asmsym)
604cfa82a00SThomas GleixnerSYM_CODE_END(\asmsym)
605cfa82a00SThomas Gleixner.endm
606cfa82a00SThomas Gleixner
607905a36a2SIngo Molnar/*
60853aaf262SThomas Gleixner * Include the defines which emit the idt entries which are shared
609f0178fc0SThomas Gleixner * shared between 32 and 64 bit and emit the __irqentry_text_* markers
610f0178fc0SThomas Gleixner * so the stacktrace boundary checks work.
61153aaf262SThomas Gleixner */
61267e93dddSThomas Gleixner	__ALIGN
613f0178fc0SThomas Gleixner	.globl __irqentry_text_start
614f0178fc0SThomas Gleixner__irqentry_text_start:
615f0178fc0SThomas Gleixner
61653aaf262SThomas Gleixner#include <asm/idtentry.h>
61753aaf262SThomas Gleixner
61867e93dddSThomas Gleixner	__ALIGN
619f0178fc0SThomas Gleixner	.globl __irqentry_text_end
620f0178fc0SThomas Gleixner__irqentry_text_end:
6213e3f0695SPeter Zijlstra	ANNOTATE_NOENDBR
622f0178fc0SThomas Gleixner
623fa5e5c40SThomas GleixnerSYM_CODE_START_LOCAL(common_interrupt_return)
62426ba4e57SJiri SlabySYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
6252dbb887eSPeter Zijlstra	IBRS_EXIT
62626c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
62726c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates user mode. */
6281e4c4f61SBorislav Petkov	testb	$3, CS(%rsp)
62926c4ef9cSAndy Lutomirski	jnz	1f
63026c4ef9cSAndy Lutomirski	ud2
63126c4ef9cSAndy Lutomirski1:
63226c4ef9cSAndy Lutomirski#endif
6335c8f6a2eSLai Jiangshan#ifdef CONFIG_XEN_PV
6345c8f6a2eSLai Jiangshan	ALTERNATIVE "", "jmp xenpv_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV
6355c8f6a2eSLai Jiangshan#endif
6365c8f6a2eSLai Jiangshan
637502af0d7SDominik Brodowski	POP_REGS pop_rdi=0
6383e3b9293SAndy Lutomirski
6393e3b9293SAndy Lutomirski	/*
6403e3b9293SAndy Lutomirski	 * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS.
6413e3b9293SAndy Lutomirski	 * Save old stack pointer and switch to trampoline stack.
6423e3b9293SAndy Lutomirski	 */
6433e3b9293SAndy Lutomirski	movq	%rsp, %rdi
644c482feefSAndy Lutomirski	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
645fb799447SJosh Poimboeuf	UNWIND_HINT_END_OF_STACK
6463e3b9293SAndy Lutomirski
6473e3b9293SAndy Lutomirski	/* Copy the IRET frame to the trampoline stack. */
6483e3b9293SAndy Lutomirski	pushq	6*8(%rdi)	/* SS */
6493e3b9293SAndy Lutomirski	pushq	5*8(%rdi)	/* RSP */
6503e3b9293SAndy Lutomirski	pushq	4*8(%rdi)	/* EFLAGS */
6513e3b9293SAndy Lutomirski	pushq	3*8(%rdi)	/* CS */
6523e3b9293SAndy Lutomirski	pushq	2*8(%rdi)	/* RIP */
6533e3b9293SAndy Lutomirski
6543e3b9293SAndy Lutomirski	/* Push user RDI on the trampoline stack. */
6553e3b9293SAndy Lutomirski	pushq	(%rdi)
6563e3b9293SAndy Lutomirski
6573e3b9293SAndy Lutomirski	/*
6583e3b9293SAndy Lutomirski	 * We are on the trampoline stack.  All regs except RDI are live.
6593e3b9293SAndy Lutomirski	 * We can do future final exit work right here.
6603e3b9293SAndy Lutomirski	 */
661afaef01cSAlexander Popov	STACKLEAK_ERASE_NOCLOBBER
6623e3b9293SAndy Lutomirski
6636fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
6648a09317bSDave Hansen
6653e3b9293SAndy Lutomirski	/* Restore RDI. */
6663e3b9293SAndy Lutomirski	popq	%rdi
6676cf3e4c0SPeter Zijlstra	swapgs
6687caf330fSPawan Gupta	CLEAR_CPU_BUFFERS
6698b87d8ceSPeter Zijlstra	jmp	.Lnative_iret
67026c4ef9cSAndy Lutomirski
671905a36a2SIngo Molnar
67226ba4e57SJiri SlabySYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)
67326c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
67426c4ef9cSAndy Lutomirski	/* Assert that pt_regs indicates kernel mode. */
6751e4c4f61SBorislav Petkov	testb	$3, CS(%rsp)
67626c4ef9cSAndy Lutomirski	jz	1f
67726c4ef9cSAndy Lutomirski	ud2
67826c4ef9cSAndy Lutomirski1:
67926c4ef9cSAndy Lutomirski#endif
680502af0d7SDominik Brodowski	POP_REGS
681e872045bSAndy Lutomirski	addq	$8, %rsp	/* skip regs->orig_ax */
68210bcc80eSMathieu Desnoyers	/*
68310bcc80eSMathieu Desnoyers	 * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
68410bcc80eSMathieu Desnoyers	 * when returning from IPI handler.
68510bcc80eSMathieu Desnoyers	 */
6868b87d8ceSPeter Zijlstra#ifdef CONFIG_XEN_PV
6878b87d8ceSPeter ZijlstraSYM_INNER_LABEL(early_xen_iret_patch, SYM_L_GLOBAL)
6888b87d8ceSPeter Zijlstra	ANNOTATE_NOENDBR
6898b87d8ceSPeter Zijlstra	.byte 0xe9
6908b87d8ceSPeter Zijlstra	.long .Lnative_iret - (. + 4)
6918b87d8ceSPeter Zijlstra#endif
692905a36a2SIngo Molnar
6938b87d8ceSPeter Zijlstra.Lnative_iret:
6948c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
695905a36a2SIngo Molnar	/*
696905a36a2SIngo Molnar	 * Are we returning to a stack segment from the LDT?  Note: in
697905a36a2SIngo Molnar	 * 64-bit mode SS:RSP on the exception stack is always valid.
698905a36a2SIngo Molnar	 */
699905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
700905a36a2SIngo Molnar	testb	$4, (SS-RIP)(%rsp)
701905a36a2SIngo Molnar	jnz	native_irq_return_ldt
702905a36a2SIngo Molnar#endif
703905a36a2SIngo Molnar
704cc66936eSJiri SlabySYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL)
7053e3f0695SPeter Zijlstra	ANNOTATE_NOENDBR // exc_double_fault
706905a36a2SIngo Molnar	/*
707905a36a2SIngo Molnar	 * This may fault.  Non-paranoid faults on return to userspace are
708905a36a2SIngo Molnar	 * handled by fixup_bad_iret.  These include #SS, #GP, and #NP.
709c29c775aSThomas Gleixner	 * Double-faults due to espfix64 are handled in exc_double_fault.
710905a36a2SIngo Molnar	 * Other faults here are fatal.
711905a36a2SIngo Molnar	 */
712905a36a2SIngo Molnar	iretq
713905a36a2SIngo Molnar
714905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
715905a36a2SIngo Molnarnative_irq_return_ldt:
71685063facSAndy Lutomirski	/*
71785063facSAndy Lutomirski	 * We are running with user GSBASE.  All GPRs contain their user
71885063facSAndy Lutomirski	 * values.  We have a percpu ESPFIX stack that is eight slots
71985063facSAndy Lutomirski	 * long (see ESPFIX_STACK_SIZE).  espfix_waddr points to the bottom
72085063facSAndy Lutomirski	 * of the ESPFIX stack.
72185063facSAndy Lutomirski	 *
72285063facSAndy Lutomirski	 * We clobber RAX and RDI in this code.  We stash RDI on the
72385063facSAndy Lutomirski	 * normal stack and RAX on the ESPFIX stack.
72485063facSAndy Lutomirski	 *
72585063facSAndy Lutomirski	 * The ESPFIX stack layout we set up looks like this:
72685063facSAndy Lutomirski	 *
72785063facSAndy Lutomirski	 * --- top of ESPFIX stack ---
72885063facSAndy Lutomirski	 * SS
72985063facSAndy Lutomirski	 * RSP
73085063facSAndy Lutomirski	 * RFLAGS
73185063facSAndy Lutomirski	 * CS
73285063facSAndy Lutomirski	 * RIP  <-- RSP points here when we're done
73385063facSAndy Lutomirski	 * RAX  <-- espfix_waddr points here
73485063facSAndy Lutomirski	 * --- bottom of ESPFIX stack ---
73585063facSAndy Lutomirski	 */
73685063facSAndy Lutomirski
73785063facSAndy Lutomirski	pushq	%rdi				/* Stash user RDI */
73853c9d924SJuergen Gross	swapgs					/* to kernel GS */
7398a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi	/* to kernel CR3 */
7408a09317bSDave Hansen
741905a36a2SIngo Molnar	movq	PER_CPU_VAR(espfix_waddr), %rdi
74285063facSAndy Lutomirski	movq	%rax, (0*8)(%rdi)		/* user RAX */
74385063facSAndy Lutomirski	movq	(1*8)(%rsp), %rax		/* user RIP */
744905a36a2SIngo Molnar	movq	%rax, (1*8)(%rdi)
74585063facSAndy Lutomirski	movq	(2*8)(%rsp), %rax		/* user CS */
746905a36a2SIngo Molnar	movq	%rax, (2*8)(%rdi)
74785063facSAndy Lutomirski	movq	(3*8)(%rsp), %rax		/* user RFLAGS */
748905a36a2SIngo Molnar	movq	%rax, (3*8)(%rdi)
74985063facSAndy Lutomirski	movq	(5*8)(%rsp), %rax		/* user SS */
750905a36a2SIngo Molnar	movq	%rax, (5*8)(%rdi)
75185063facSAndy Lutomirski	movq	(4*8)(%rsp), %rax		/* user RSP */
752905a36a2SIngo Molnar	movq	%rax, (4*8)(%rdi)
75385063facSAndy Lutomirski	/* Now RAX == RSP. */
75485063facSAndy Lutomirski
75585063facSAndy Lutomirski	andl	$0xffff0000, %eax		/* RAX = (RSP & 0xffff0000) */
75685063facSAndy Lutomirski
75785063facSAndy Lutomirski	/*
75885063facSAndy Lutomirski	 * espfix_stack[31:16] == 0.  The page tables are set up such that
75985063facSAndy Lutomirski	 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
76085063facSAndy Lutomirski	 * espfix_waddr for any X.  That is, there are 65536 RO aliases of
76185063facSAndy Lutomirski	 * the same page.  Set up RSP so that RSP[31:16] contains the
76285063facSAndy Lutomirski	 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
76385063facSAndy Lutomirski	 * still points to an RO alias of the ESPFIX stack.
76485063facSAndy Lutomirski	 */
765905a36a2SIngo Molnar	orq	PER_CPU_VAR(espfix_stack), %rax
7668a09317bSDave Hansen
7676fd166aaSPeter Zijlstra	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
76853c9d924SJuergen Gross	swapgs					/* to user GS */
7698a09317bSDave Hansen	popq	%rdi				/* Restore user RDI */
7708a09317bSDave Hansen
771905a36a2SIngo Molnar	movq	%rax, %rsp
7728c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
77385063facSAndy Lutomirski
77485063facSAndy Lutomirski	/*
77585063facSAndy Lutomirski	 * At this point, we cannot write to the stack any more, but we can
77685063facSAndy Lutomirski	 * still read.
77785063facSAndy Lutomirski	 */
77885063facSAndy Lutomirski	popq	%rax				/* Restore user RAX */
77985063facSAndy Lutomirski
7807caf330fSPawan Gupta	CLEAR_CPU_BUFFERS
7817caf330fSPawan Gupta
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
789fa5e5c40SThomas GleixnerSYM_CODE_END(common_interrupt_return)
790fa5e5c40SThomas Gleixner_ASM_NOKPROBE(common_interrupt_return)
791905a36a2SIngo Molnar
792905a36a2SIngo Molnar/*
7934d732138SIngo Molnar * Reload gs selector with exception handling
794df729fb0SH. Peter Anvin (Intel) *  di:  new selector
795b9f6976bSThomas Gleixner *
796b9f6976bSThomas Gleixner * Is in entry.text as it shouldn't be instrumented.
7974d732138SIngo Molnar */
798410367e3SThomas GleixnerSYM_FUNC_START(asm_load_gs_index)
7998c1f7558SJosh Poimboeuf	FRAME_BEGIN
800c9317202SThomas Gleixner	swapgs
80142c748bbSBorislav Petkov.Lgs_change:
8023e3f0695SPeter Zijlstra	ANNOTATE_NOENDBR // error_entry
803905a36a2SIngo Molnar	movl	%edi, %gs
80496e5d28aSBorislav Petkov2:	ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
805c9317202SThomas Gleixner	swapgs
8068c1f7558SJosh Poimboeuf	FRAME_END
807f94909ceSPeter Zijlstra	RET
808905a36a2SIngo Molnar
809905a36a2SIngo Molnar	/* running with kernelgs */
81016e617d0SPeter Zijlstra.Lbad_gs:
811c9317202SThomas Gleixner	swapgs					/* switch back to user gs */
812b038c842SAndy Lutomirski.macro ZAP_GS
813b038c842SAndy Lutomirski	/* This can't be a string because the preprocessor needs to see it. */
814b038c842SAndy Lutomirski	movl $__USER_DS, %eax
815b038c842SAndy Lutomirski	movl %eax, %gs
816b038c842SAndy Lutomirski.endm
817b038c842SAndy Lutomirski	ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
818905a36a2SIngo Molnar	xorl	%eax, %eax
819905a36a2SIngo Molnar	movl	%eax, %gs
820905a36a2SIngo Molnar	jmp	2b
82116e617d0SPeter Zijlstra
82216e617d0SPeter Zijlstra	_ASM_EXTABLE(.Lgs_change, .Lbad_gs)
82316e617d0SPeter Zijlstra
82416e617d0SPeter ZijlstraSYM_FUNC_END(asm_load_gs_index)
82516e617d0SPeter ZijlstraEXPORT_SYMBOL(asm_load_gs_index)
826905a36a2SIngo Molnar
82728c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV
828905a36a2SIngo Molnar/*
829905a36a2SIngo Molnar * A note on the "critical region" in our callback handler.
830905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring
831905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled
832905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before
833905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still
834905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack.
835905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd
836905a36a2SIngo Molnar * like to avoid the possibility.
837905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an
838905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current
839905a36a2SIngo Molnar * activation and restart the handler using the previous one.
8402f6474e4SThomas Gleixner *
8412f6474e4SThomas Gleixner * C calling convention: exc_xen_hypervisor_callback(struct *pt_regs)
842905a36a2SIngo Molnar */
84367e93dddSThomas Gleixner	__FUNC_ALIGN
84467e93dddSThomas GleixnerSYM_CODE_START_LOCAL_NOALIGN(exc_xen_hypervisor_callback)
8454d732138SIngo Molnar
846905a36a2SIngo Molnar/*
847905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
848905a36a2SIngo Molnar * see the correct pointer to the pt_regs
849905a36a2SIngo Molnar */
8508c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
8514d732138SIngo Molnar	movq	%rdi, %rsp			/* we don't return, adjust the stack frame */
8528c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
8531d3e53e8SAndy Lutomirski
8542f6474e4SThomas Gleixner	call	xen_pv_evtchn_do_upcall
8551d3e53e8SAndy Lutomirski
8562f6474e4SThomas Gleixner	jmp	error_return
8572f6474e4SThomas GleixnerSYM_CODE_END(exc_xen_hypervisor_callback)
858905a36a2SIngo Molnar
859905a36a2SIngo Molnar/*
860905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes.
861905a36a2SIngo Molnar * We get here for two reasons:
862905a36a2SIngo Molnar *  1. Fault while reloading DS, ES, FS or GS
863905a36a2SIngo Molnar *  2. Fault while executing IRET
864905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment
865905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others.
866905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the
867905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall
868905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
869905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register
870905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1.
871905a36a2SIngo Molnar */
87267e93dddSThomas Gleixner	__FUNC_ALIGN
87367e93dddSThomas GleixnerSYM_CODE_START_NOALIGN(xen_failsafe_callback)
874fb799447SJosh Poimboeuf	UNWIND_HINT_UNDEFINED
8755b2fc515SPeter Zijlstra	ENDBR
876905a36a2SIngo Molnar	movl	%ds, %ecx
877905a36a2SIngo Molnar	cmpw	%cx, 0x10(%rsp)
878905a36a2SIngo Molnar	jne	1f
879905a36a2SIngo Molnar	movl	%es, %ecx
880905a36a2SIngo Molnar	cmpw	%cx, 0x18(%rsp)
881905a36a2SIngo Molnar	jne	1f
882905a36a2SIngo Molnar	movl	%fs, %ecx
883905a36a2SIngo Molnar	cmpw	%cx, 0x20(%rsp)
884905a36a2SIngo Molnar	jne	1f
885905a36a2SIngo Molnar	movl	%gs, %ecx
886905a36a2SIngo Molnar	cmpw	%cx, 0x28(%rsp)
887905a36a2SIngo Molnar	jne	1f
888905a36a2SIngo Molnar	/* All segments match their saved values => Category 2 (Bad IRET). */
889905a36a2SIngo Molnar	movq	(%rsp), %rcx
890905a36a2SIngo Molnar	movq	8(%rsp), %r11
891905a36a2SIngo Molnar	addq	$0x30, %rsp
892905a36a2SIngo Molnar	pushq	$0				/* RIP */
8938c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=8
894be4c11afSThomas Gleixner	jmp	asm_exc_general_protection
895905a36a2SIngo Molnar1:	/* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
896905a36a2SIngo Molnar	movq	(%rsp), %rcx
897905a36a2SIngo Molnar	movq	8(%rsp), %r11
898905a36a2SIngo Molnar	addq	$0x30, %rsp
8998c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
900905a36a2SIngo Molnar	pushq	$-1 /* orig_ax = -1 => not a system call */
9013f01daecSDominik Brodowski	PUSH_AND_CLEAR_REGS
902946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
903e88d9741SThomas Gleixner	jmp	error_return
904bc7b11c0SJiri SlabySYM_CODE_END(xen_failsafe_callback)
90528c11b0fSJuergen Gross#endif /* CONFIG_XEN_PV */
906905a36a2SIngo Molnar
907905a36a2SIngo Molnar/*
908c82965f9SChang S. Bae * Save all registers in pt_regs. Return GSBASE related information
909c82965f9SChang S. Bae * in EBX depending on the availability of the FSGSBASE instructions:
910c82965f9SChang S. Bae *
911c82965f9SChang S. Bae * FSGSBASE	R/EBX
912c82965f9SChang S. Bae *     N        0 -> SWAPGS on exit
913c82965f9SChang S. Bae *              1 -> no SWAPGS on exit
914c82965f9SChang S. Bae *
915c82965f9SChang S. Bae *     Y        GSBASE value at entry, must be restored in paranoid_exit
9162dbb887eSPeter Zijlstra *
9172dbb887eSPeter Zijlstra * R14 - old CR3
9182dbb887eSPeter Zijlstra * R15 - old SPEC_CTRL
919905a36a2SIngo Molnar */
920c22cf380SThomas GleixnerSYM_CODE_START(paranoid_entry)
921c22cf380SThomas Gleixner	ANNOTATE_NOENDBR
9228c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
9239e809d15SDominik Brodowski	PUSH_AND_CLEAR_REGS save_ret=1
9249e809d15SDominik Brodowski	ENCODE_FRAME_POINTER 8
9258a09317bSDave Hansen
92616561f27SDave Hansen	/*
92716561f27SDave Hansen	 * Always stash CR3 in %r14.  This value will be restored,
928ae852495SAndy Lutomirski	 * verbatim, at exit.  Needed if paranoid_entry interrupted
929ae852495SAndy Lutomirski	 * another entry that already switched to the user CR3 value
930ae852495SAndy Lutomirski	 * but has not yet returned to userspace.
93116561f27SDave Hansen	 *
93216561f27SDave Hansen	 * This is also why CS (stashed in the "iret frame" by the
93316561f27SDave Hansen	 * hardware at entry) can not be used: this may be a return
934ae852495SAndy Lutomirski	 * to kernel code, but with a user CR3 value.
93596b23714SChang S. Bae	 *
93696b23714SChang S. Bae	 * Switching CR3 does not depend on kernel GSBASE so it can
93796b23714SChang S. Bae	 * be done before switching to the kernel GSBASE. This is
93896b23714SChang S. Bae	 * required for FSGSBASE because the kernel GSBASE has to
93996b23714SChang S. Bae	 * be retrieved from a kernel internal table.
94016561f27SDave Hansen	 */
9418a09317bSDave Hansen	SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
9428a09317bSDave Hansen
94318ec54fdSJosh Poimboeuf	/*
944c82965f9SChang S. Bae	 * Handling GSBASE depends on the availability of FSGSBASE.
945c82965f9SChang S. Bae	 *
946c82965f9SChang S. Bae	 * Without FSGSBASE the kernel enforces that negative GSBASE
947c82965f9SChang S. Bae	 * values indicate kernel GSBASE. With FSGSBASE no assumptions
948c82965f9SChang S. Bae	 * can be made about the GSBASE value when entering from user
949c82965f9SChang S. Bae	 * space.
950c82965f9SChang S. Bae	 */
951c82965f9SChang S. Bae	ALTERNATIVE "jmp .Lparanoid_entry_checkgs", "", X86_FEATURE_FSGSBASE
952c82965f9SChang S. Bae
953c82965f9SChang S. Bae	/*
954c82965f9SChang S. Bae	 * Read the current GSBASE and store it in %rbx unconditionally,
955c82965f9SChang S. Bae	 * retrieve and set the current CPUs kernel GSBASE. The stored value
956c82965f9SChang S. Bae	 * has to be restored in paranoid_exit unconditionally.
957c82965f9SChang S. Bae	 *
9580b2c605fSBorislav Petkov	 * The unconditional write to GS base below ensures that no subsequent
9590b2c605fSBorislav Petkov	 * loads based on a mispredicted GS base can happen, therefore no LFENCE
9600b2c605fSBorislav Petkov	 * is needed here.
961c82965f9SChang S. Bae	 */
962c82965f9SChang S. Bae	SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx
9632dbb887eSPeter Zijlstra	jmp .Lparanoid_gsbase_done
964c82965f9SChang S. Bae
965c82965f9SChang S. Bae.Lparanoid_entry_checkgs:
96696b23714SChang S. Bae	/* EBX = 1 -> kernel GSBASE active, no restore required */
96796b23714SChang S. Bae	movl	$1, %ebx
968c07e4555SLai Jiangshan
96996b23714SChang S. Bae	/*
97096b23714SChang S. Bae	 * The kernel-enforced convention is a negative GSBASE indicates
97196b23714SChang S. Bae	 * a kernel value. No SWAPGS needed on entry and exit.
97296b23714SChang S. Bae	 */
97396b23714SChang S. Bae	movl	$MSR_GS_BASE, %ecx
97496b23714SChang S. Bae	rdmsr
97596b23714SChang S. Bae	testl	%edx, %edx
976c07e4555SLai Jiangshan	js	.Lparanoid_kernel_gsbase
97718ec54fdSJosh Poimboeuf
97896b23714SChang S. Bae	/* EBX = 0 -> SWAPGS required on exit */
97996b23714SChang S. Bae	xorl	%ebx, %ebx
980c07e4555SLai Jiangshan	swapgs
981c07e4555SLai Jiangshan.Lparanoid_kernel_gsbase:
982c07e4555SLai Jiangshan	FENCE_SWAPGS_KERNEL_ENTRY
9832dbb887eSPeter Zijlstra.Lparanoid_gsbase_done:
9842dbb887eSPeter Zijlstra
9852dbb887eSPeter Zijlstra	/*
9862dbb887eSPeter Zijlstra	 * Once we have CR3 and %GS setup save and set SPEC_CTRL. Just like
9872dbb887eSPeter Zijlstra	 * CR3 above, keep the old value in a callee saved register.
9882dbb887eSPeter Zijlstra	 */
9892dbb887eSPeter Zijlstra	IBRS_ENTER save_reg=%r15
9905d821386SThomas Gleixner	UNTRAIN_RET_FROM_CALL
9912dbb887eSPeter Zijlstra
992f94909ceSPeter Zijlstra	RET
993ef1e0315SJiri SlabySYM_CODE_END(paranoid_entry)
994905a36a2SIngo Molnar
995905a36a2SIngo Molnar/*
996905a36a2SIngo Molnar * "Paranoid" exit path from exception stack.  This is invoked
997905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came
998905a36a2SIngo Molnar * from kernel space.
999905a36a2SIngo Molnar *
1000905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early
1001905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would
1002c82965f9SChang S. Bae * be complicated.  Fortunately, there's no good reason to try
1003c82965f9SChang S. Bae * to handle preemption here.
10044d732138SIngo Molnar *
1005c82965f9SChang S. Bae * R/EBX contains the GSBASE related information depending on the
1006c82965f9SChang S. Bae * availability of the FSGSBASE instructions:
1007c82965f9SChang S. Bae *
1008c82965f9SChang S. Bae * FSGSBASE	R/EBX
1009c82965f9SChang S. Bae *     N        0 -> SWAPGS on exit
1010c82965f9SChang S. Bae *              1 -> no SWAPGS on exit
1011c82965f9SChang S. Bae *
1012c82965f9SChang S. Bae *     Y        User space GSBASE, must be restored unconditionally
10132dbb887eSPeter Zijlstra *
10142dbb887eSPeter Zijlstra * R14 - old CR3
10152dbb887eSPeter Zijlstra * R15 - old SPEC_CTRL
1016905a36a2SIngo Molnar */
1017ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_exit)
10188c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
10192dbb887eSPeter Zijlstra
10202dbb887eSPeter Zijlstra	/*
10212dbb887eSPeter Zijlstra	 * Must restore IBRS state before both CR3 and %GS since we need access
10222dbb887eSPeter Zijlstra	 * to the per-CPU x86_spec_ctrl_shadow variable.
10232dbb887eSPeter Zijlstra	 */
10242dbb887eSPeter Zijlstra	IBRS_EXIT save_reg=%r15
10252dbb887eSPeter Zijlstra
1026c82965f9SChang S. Bae	/*
1027c82965f9SChang S. Bae	 * The order of operations is important. RESTORE_CR3 requires
1028c82965f9SChang S. Bae	 * kernel GSBASE.
1029c82965f9SChang S. Bae	 *
1030c82965f9SChang S. Bae	 * NB to anyone to try to optimize this code: this code does
1031c82965f9SChang S. Bae	 * not execute at all for exceptions from user mode. Those
10328c3223a5SJingyu Wang	 * exceptions go through error_return instead.
1033c82965f9SChang S. Bae	 */
1034c82965f9SChang S. Bae	RESTORE_CR3	scratch_reg=%rax save_reg=%r14
1035c82965f9SChang S. Bae
1036c82965f9SChang S. Bae	/* Handle the three GSBASE cases */
1037c82965f9SChang S. Bae	ALTERNATIVE "jmp .Lparanoid_exit_checkgs", "", X86_FEATURE_FSGSBASE
1038c82965f9SChang S. Bae
1039c82965f9SChang S. Bae	/* With FSGSBASE enabled, unconditionally restore GSBASE */
1040c82965f9SChang S. Bae	wrgsbase	%rbx
104145c08383SThomas Gleixner	jmp		restore_regs_and_return_to_kernel
1042c82965f9SChang S. Bae
1043c82965f9SChang S. Bae.Lparanoid_exit_checkgs:
1044c82965f9SChang S. Bae	/* On non-FSGSBASE systems, conditionally do SWAPGS */
1045c82965f9SChang S. Bae	testl		%ebx, %ebx
1046c82965f9SChang S. Bae	jnz		restore_regs_and_return_to_kernel
1047c82965f9SChang S. Bae
1048c82965f9SChang S. Bae	/* We are returning to a context with user GSBASE */
104953c9d924SJuergen Gross	swapgs
1050e5317832SAndy Lutomirski	jmp		restore_regs_and_return_to_kernel
1051ef1e0315SJiri SlabySYM_CODE_END(paranoid_exit)
1052905a36a2SIngo Molnar
1053905a36a2SIngo Molnar/*
1054ee774dacSLai Jiangshan * Switch GS and CR3 if needed.
1055905a36a2SIngo Molnar */
1056c22cf380SThomas GleixnerSYM_CODE_START(error_entry)
1057c22cf380SThomas Gleixner	ANNOTATE_NOENDBR
10589e809d15SDominik Brodowski	UNWIND_HINT_FUNC
10592c08b9b3SPeter Zijlstra
10602c08b9b3SPeter Zijlstra	PUSH_AND_CLEAR_REGS save_ret=1
10612c08b9b3SPeter Zijlstra	ENCODE_FRAME_POINTER 8
10622c08b9b3SPeter Zijlstra
1063905a36a2SIngo Molnar	testb	$3, CS+8(%rsp)
1064cb6f64edSAndy Lutomirski	jz	.Lerror_kernelspace
1065539f5113SAndy Lutomirski
1066cb6f64edSAndy Lutomirski	/*
1067cb6f64edSAndy Lutomirski	 * We entered from user mode or we're pretending to have entered
1068cb6f64edSAndy Lutomirski	 * from user mode due to an IRET fault.
1069cb6f64edSAndy Lutomirski	 */
1070c89191ceSLai Jiangshan	swapgs
107118ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
10728a09317bSDave Hansen	/* We have user CR3.  Change to kernel CR3. */
10738a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
10742dbb887eSPeter Zijlstra	IBRS_ENTER
10755d821386SThomas Gleixner	UNTRAIN_RET_FROM_CALL
1076539f5113SAndy Lutomirski
1077520a7e80SLai Jiangshan	leaq	8(%rsp), %rdi			/* arg0 = pt_regs pointer */
10787f2590a1SAndy Lutomirski	/* Put us onto the real thread stack. */
1079ef79ed20SPeter Zijlstra	jmp	sync_regs
108002bc7768SAndy Lutomirski
1081905a36a2SIngo Molnar	/*
1082905a36a2SIngo Molnar	 * There are two places in the kernel that can potentially fault with
1083905a36a2SIngo Molnar	 * usergs. Handle them here.  B stepping K8s sometimes report a
1084905a36a2SIngo Molnar	 * truncated RIP for IRET exceptions returning to compat mode. Check
1085905a36a2SIngo Molnar	 * for these here too.
1086905a36a2SIngo Molnar	 */
1087cb6f64edSAndy Lutomirski.Lerror_kernelspace:
1088905a36a2SIngo Molnar	leaq	native_irq_return_iret(%rip), %rcx
1089905a36a2SIngo Molnar	cmpq	%rcx, RIP+8(%rsp)
1090cb6f64edSAndy Lutomirski	je	.Lerror_bad_iret
1091905a36a2SIngo Molnar	movl	%ecx, %eax			/* zero extend */
1092905a36a2SIngo Molnar	cmpq	%rax, RIP+8(%rsp)
1093cb6f64edSAndy Lutomirski	je	.Lbstep_iret
109442c748bbSBorislav Petkov	cmpq	$.Lgs_change, RIP+8(%rsp)
109518ec54fdSJosh Poimboeuf	jne	.Lerror_entry_done_lfence
1096539f5113SAndy Lutomirski
1097539f5113SAndy Lutomirski	/*
109842c748bbSBorislav Petkov	 * hack: .Lgs_change can fail with user gsbase.  If this happens, fix up
1099539f5113SAndy Lutomirski	 * gsbase and proceed.  We'll fix up the exception and land in
110042c748bbSBorislav Petkov	 * .Lgs_change's error handler with kernel gsbase.
1101539f5113SAndy Lutomirski	 */
1102c89191ceSLai Jiangshan	swapgs
11031367afaaSLai Jiangshan
11041367afaaSLai Jiangshan	/*
11051367afaaSLai Jiangshan	 * Issue an LFENCE to prevent GS speculation, regardless of whether it is a
11061367afaaSLai Jiangshan	 * kernel or user gsbase.
11071367afaaSLai Jiangshan	 */
11081367afaaSLai Jiangshan.Lerror_entry_done_lfence:
11091367afaaSLai Jiangshan	FENCE_SWAPGS_KERNEL_ENTRY
11105d821386SThomas Gleixner	CALL_DEPTH_ACCOUNT
1111520a7e80SLai Jiangshan	leaq	8(%rsp), %rax			/* return pt_regs pointer */
11124708ea14SJosh Poimboeuf	VALIDATE_UNRET_END
1113f94909ceSPeter Zijlstra	RET
1114905a36a2SIngo Molnar
1115cb6f64edSAndy Lutomirski.Lbstep_iret:
1116905a36a2SIngo Molnar	/* Fix truncated RIP */
1117905a36a2SIngo Molnar	movq	%rcx, RIP+8(%rsp)
1118905a36a2SIngo Molnar	/* fall through */
1119905a36a2SIngo Molnar
1120cb6f64edSAndy Lutomirski.Lerror_bad_iret:
1121539f5113SAndy Lutomirski	/*
11228a09317bSDave Hansen	 * We came from an IRET to user mode, so we have user
11238a09317bSDave Hansen	 * gsbase and CR3.  Switch to kernel gsbase and CR3:
1124539f5113SAndy Lutomirski	 */
1125c89191ceSLai Jiangshan	swapgs
112618ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
11278a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
11282dbb887eSPeter Zijlstra	IBRS_ENTER
11295d821386SThomas Gleixner	UNTRAIN_RET_FROM_CALL
1130539f5113SAndy Lutomirski
1131539f5113SAndy Lutomirski	/*
1132539f5113SAndy Lutomirski	 * Pretend that the exception came from user mode: set up pt_regs
1133b3681dd5SAndy Lutomirski	 * as if we faulted immediately after IRET.
1134539f5113SAndy Lutomirski	 */
1135520a7e80SLai Jiangshan	leaq	8(%rsp), %rdi			/* arg0 = pt_regs pointer */
1136905a36a2SIngo Molnar	call	fixup_bad_iret
1137520a7e80SLai Jiangshan	mov	%rax, %rdi
1138ef79ed20SPeter Zijlstra	jmp	sync_regs
1139ef1e0315SJiri SlabySYM_CODE_END(error_entry)
1140905a36a2SIngo Molnar
1141424c7d0aSThomas GleixnerSYM_CODE_START_LOCAL(error_return)
1142424c7d0aSThomas Gleixner	UNWIND_HINT_REGS
1143424c7d0aSThomas Gleixner	DEBUG_ENTRY_ASSERT_IRQS_OFF
1144424c7d0aSThomas Gleixner	testb	$3, CS(%rsp)
1145424c7d0aSThomas Gleixner	jz	restore_regs_and_return_to_kernel
1146424c7d0aSThomas Gleixner	jmp	swapgs_restore_regs_and_return_to_usermode
1147424c7d0aSThomas GleixnerSYM_CODE_END(error_return)
1148424c7d0aSThomas Gleixner
1149929bacecSAndy Lutomirski/*
1150929bacecSAndy Lutomirski * Runs on exception stack.  Xen PV does not go through this path at all,
1151929bacecSAndy Lutomirski * so we can use real assembly here.
11528a09317bSDave Hansen *
11538a09317bSDave Hansen * Registers:
11548a09317bSDave Hansen *	%r14: Used to save/restore the CR3 of the interrupted context
11558a09317bSDave Hansen *	      when PAGE_TABLE_ISOLATION is in use.  Do not clobber.
1156929bacecSAndy Lutomirski */
11576271fef0SThomas GleixnerSYM_CODE_START(asm_exc_nmi)
11584708ea14SJosh Poimboeuf	UNWIND_HINT_IRET_ENTRY
11598f93402bSPeter Zijlstra	ENDBR
1160929bacecSAndy Lutomirski
1161fc57a7c6SAndy Lutomirski	/*
1162905a36a2SIngo Molnar	 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1163905a36a2SIngo Molnar	 * the iretq it performs will take us out of NMI context.
1164905a36a2SIngo Molnar	 * This means that we can have nested NMIs where the next
1165905a36a2SIngo Molnar	 * NMI is using the top of the stack of the previous NMI. We
1166905a36a2SIngo Molnar	 * can't let it execute because the nested NMI will corrupt the
1167905a36a2SIngo Molnar	 * stack of the previous NMI. NMI handlers are not re-entrant
1168905a36a2SIngo Molnar	 * anyway.
1169905a36a2SIngo Molnar	 *
1170905a36a2SIngo Molnar	 * To handle this case we do the following:
1171905a36a2SIngo Molnar	 *  Check the a special location on the stack that contains
1172905a36a2SIngo Molnar	 *  a variable that is set when NMIs are executing.
1173905a36a2SIngo Molnar	 *  The interrupted task's stack is also checked to see if it
1174905a36a2SIngo Molnar	 *  is an NMI stack.
1175905a36a2SIngo Molnar	 *  If the variable is not set and the stack is not the NMI
1176905a36a2SIngo Molnar	 *  stack then:
1177905a36a2SIngo Molnar	 *    o Set the special variable on the stack
11780b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "outermost" location on the
11790b22930eSAndy Lutomirski	 *      stack
11800b22930eSAndy Lutomirski	 *    o Copy the interrupt frame into an "iret" location on the stack
1181905a36a2SIngo Molnar	 *    o Continue processing the NMI
1182905a36a2SIngo Molnar	 *  If the variable is set or the previous stack is the NMI stack:
11830b22930eSAndy Lutomirski	 *    o Modify the "iret" location to jump to the repeat_nmi
1184905a36a2SIngo Molnar	 *    o return back to the first NMI
1185905a36a2SIngo Molnar	 *
1186905a36a2SIngo Molnar	 * Now on exit of the first NMI, we first clear the stack variable
1187905a36a2SIngo Molnar	 * The NMI stack will tell any nested NMIs at that point that it is
1188905a36a2SIngo Molnar	 * nested. Then we pop the stack normally with iret, and if there was
1189905a36a2SIngo Molnar	 * a nested NMI that updated the copy interrupt stack frame, a
1190905a36a2SIngo Molnar	 * jump will be made to the repeat_nmi code that will handle the second
1191905a36a2SIngo Molnar	 * NMI.
11929b6e6a83SAndy Lutomirski	 *
11939b6e6a83SAndy Lutomirski	 * However, espfix prevents us from directly returning to userspace
11949b6e6a83SAndy Lutomirski	 * with a single IRET instruction.  Similarly, IRET to user mode
11959b6e6a83SAndy Lutomirski	 * can fault.  We therefore handle NMIs from user space like
11969b6e6a83SAndy Lutomirski	 * other IST entries.
1197905a36a2SIngo Molnar	 */
1198905a36a2SIngo Molnar
1199e93c1730SAndy Lutomirski	ASM_CLAC
1200c64cc280SLai Jiangshan	cld
1201e93c1730SAndy Lutomirski
1202905a36a2SIngo Molnar	/* Use %rdx as our temp variable throughout */
1203905a36a2SIngo Molnar	pushq	%rdx
1204905a36a2SIngo Molnar
12059b6e6a83SAndy Lutomirski	testb	$3, CS-RIP+8(%rsp)
12069b6e6a83SAndy Lutomirski	jz	.Lnmi_from_kernel
1207905a36a2SIngo Molnar
1208905a36a2SIngo Molnar	/*
12099b6e6a83SAndy Lutomirski	 * NMI from user mode.  We need to run on the thread stack, but we
12109b6e6a83SAndy Lutomirski	 * can't go through the normal entry paths: NMIs are masked, and
12119b6e6a83SAndy Lutomirski	 * we don't want to enable interrupts, because then we'll end
12129b6e6a83SAndy Lutomirski	 * up in an awkward situation in which IRQs are on but NMIs
12139b6e6a83SAndy Lutomirski	 * are off.
121483c133cfSAndy Lutomirski	 *
121583c133cfSAndy Lutomirski	 * We also must not push anything to the stack before switching
121683c133cfSAndy Lutomirski	 * stacks lest we corrupt the "NMI executing" variable.
12179b6e6a83SAndy Lutomirski	 */
12189b6e6a83SAndy Lutomirski
1219929bacecSAndy Lutomirski	swapgs
122018ec54fdSJosh Poimboeuf	FENCE_SWAPGS_USER_ENTRY
12218a09317bSDave Hansen	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
12229b6e6a83SAndy Lutomirski	movq	%rsp, %rdx
1223c063a217SThomas Gleixner	movq	PER_CPU_VAR(pcpu_hot + X86_top_of_stack), %rsp
12248c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS base=%rdx offset=8
12259b6e6a83SAndy Lutomirski	pushq	5*8(%rdx)	/* pt_regs->ss */
12269b6e6a83SAndy Lutomirski	pushq	4*8(%rdx)	/* pt_regs->rsp */
12279b6e6a83SAndy Lutomirski	pushq	3*8(%rdx)	/* pt_regs->flags */
12289b6e6a83SAndy Lutomirski	pushq	2*8(%rdx)	/* pt_regs->cs */
12299b6e6a83SAndy Lutomirski	pushq	1*8(%rdx)	/* pt_regs->rip */
12308c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
12319b6e6a83SAndy Lutomirski	pushq   $-1		/* pt_regs->orig_ax */
123230907fd1SDominik Brodowski	PUSH_AND_CLEAR_REGS rdx=(%rdx)
1233946c1911SJosh Poimboeuf	ENCODE_FRAME_POINTER
12349b6e6a83SAndy Lutomirski
12352dbb887eSPeter Zijlstra	IBRS_ENTER
12362dbb887eSPeter Zijlstra	UNTRAIN_RET
12372dbb887eSPeter Zijlstra
12389b6e6a83SAndy Lutomirski	/*
12399b6e6a83SAndy Lutomirski	 * At this point we no longer need to worry about stack damage
12409b6e6a83SAndy Lutomirski	 * due to nesting -- we're on the normal thread stack and we're
12419b6e6a83SAndy Lutomirski	 * done with the NMI stack.
12429b6e6a83SAndy Lutomirski	 */
12439b6e6a83SAndy Lutomirski
12449b6e6a83SAndy Lutomirski	movq	%rsp, %rdi
12459b6e6a83SAndy Lutomirski	movq	$-1, %rsi
12466271fef0SThomas Gleixner	call	exc_nmi
12479b6e6a83SAndy Lutomirski
12489b6e6a83SAndy Lutomirski	/*
12499b6e6a83SAndy Lutomirski	 * Return back to user mode.  We must *not* do the normal exit
1250946c1911SJosh Poimboeuf	 * work, because we don't want to enable interrupts.
12519b6e6a83SAndy Lutomirski	 */
12528a055d7fSAndy Lutomirski	jmp	swapgs_restore_regs_and_return_to_usermode
12539b6e6a83SAndy Lutomirski
12549b6e6a83SAndy Lutomirski.Lnmi_from_kernel:
12559b6e6a83SAndy Lutomirski	/*
12560b22930eSAndy Lutomirski	 * Here's what our stack frame will look like:
12570b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
12580b22930eSAndy Lutomirski	 * | original SS                                             |
12590b22930eSAndy Lutomirski	 * | original Return RSP                                     |
12600b22930eSAndy Lutomirski	 * | original RFLAGS                                         |
12610b22930eSAndy Lutomirski	 * | original CS                                             |
12620b22930eSAndy Lutomirski	 * | original RIP                                            |
12630b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
12640b22930eSAndy Lutomirski	 * | temp storage for rdx                                    |
12650b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
12660b22930eSAndy Lutomirski	 * | "NMI executing" variable                                |
12670b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
12680b22930eSAndy Lutomirski	 * | iret SS          } Copied from "outermost" frame        |
12690b22930eSAndy Lutomirski	 * | iret Return RSP  } on each loop iteration; overwritten  |
12700b22930eSAndy Lutomirski	 * | iret RFLAGS      } by a nested NMI to force another     |
12710b22930eSAndy Lutomirski	 * | iret CS          } iteration if needed.                 |
12720b22930eSAndy Lutomirski	 * | iret RIP         }                                      |
12730b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
12740b22930eSAndy Lutomirski	 * | outermost SS          } initialized in first_nmi;       |
12750b22930eSAndy Lutomirski	 * | outermost Return RSP  } will not be changed before      |
12760b22930eSAndy Lutomirski	 * | outermost RFLAGS      } NMI processing is done.         |
12770b22930eSAndy Lutomirski	 * | outermost CS          } Copied to "iret" frame on each  |
12780b22930eSAndy Lutomirski	 * | outermost RIP         } iteration.                      |
12790b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
12800b22930eSAndy Lutomirski	 * | pt_regs                                                 |
12810b22930eSAndy Lutomirski	 * +---------------------------------------------------------+
12820b22930eSAndy Lutomirski	 *
12830b22930eSAndy Lutomirski	 * The "original" frame is used by hardware.  Before re-enabling
12840b22930eSAndy Lutomirski	 * NMIs, we need to be done with it, and we need to leave enough
12850b22930eSAndy Lutomirski	 * space for the asm code here.
12860b22930eSAndy Lutomirski	 *
12870b22930eSAndy Lutomirski	 * We return by executing IRET while RSP points to the "iret" frame.
12880b22930eSAndy Lutomirski	 * That will either return for real or it will loop back into NMI
12890b22930eSAndy Lutomirski	 * processing.
12900b22930eSAndy Lutomirski	 *
12910b22930eSAndy Lutomirski	 * The "outermost" frame is copied to the "iret" frame on each
12920b22930eSAndy Lutomirski	 * iteration of the loop, so each iteration starts with the "iret"
12930b22930eSAndy Lutomirski	 * frame pointing to the final return target.
12940b22930eSAndy Lutomirski	 */
12950b22930eSAndy Lutomirski
12960b22930eSAndy Lutomirski	/*
12970b22930eSAndy Lutomirski	 * Determine whether we're a nested NMI.
12980b22930eSAndy Lutomirski	 *
1299a27507caSAndy Lutomirski	 * If we interrupted kernel code between repeat_nmi and
1300a27507caSAndy Lutomirski	 * end_repeat_nmi, then we are a nested NMI.  We must not
1301a27507caSAndy Lutomirski	 * modify the "iret" frame because it's being written by
1302a27507caSAndy Lutomirski	 * the outer NMI.  That's okay; the outer NMI handler is
13036271fef0SThomas Gleixner	 * about to about to call exc_nmi() anyway, so we can just
1304a27507caSAndy Lutomirski	 * resume the outer NMI.
1305a27507caSAndy Lutomirski	 */
1306a27507caSAndy Lutomirski
1307a27507caSAndy Lutomirski	movq	$repeat_nmi, %rdx
1308a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1309a27507caSAndy Lutomirski	ja	1f
1310a27507caSAndy Lutomirski	movq	$end_repeat_nmi, %rdx
1311a27507caSAndy Lutomirski	cmpq	8(%rsp), %rdx
1312a27507caSAndy Lutomirski	ja	nested_nmi_out
1313a27507caSAndy Lutomirski1:
1314a27507caSAndy Lutomirski
1315a27507caSAndy Lutomirski	/*
1316a27507caSAndy Lutomirski	 * Now check "NMI executing".  If it's set, then we're nested.
13170b22930eSAndy Lutomirski	 * This will not detect if we interrupted an outer NMI just
13180b22930eSAndy Lutomirski	 * before IRET.
1319905a36a2SIngo Molnar	 */
1320905a36a2SIngo Molnar	cmpl	$1, -8(%rsp)
1321905a36a2SIngo Molnar	je	nested_nmi
1322905a36a2SIngo Molnar
1323905a36a2SIngo Molnar	/*
13240b22930eSAndy Lutomirski	 * Now test if the previous stack was an NMI stack.  This covers
13250b22930eSAndy Lutomirski	 * the case where we interrupt an outer NMI after it clears
1326810bc075SAndy Lutomirski	 * "NMI executing" but before IRET.  We need to be careful, though:
1327810bc075SAndy Lutomirski	 * there is one case in which RSP could point to the NMI stack
1328810bc075SAndy Lutomirski	 * despite there being no NMI active: naughty userspace controls
1329810bc075SAndy Lutomirski	 * RSP at the very beginning of the SYSCALL targets.  We can
1330810bc075SAndy Lutomirski	 * pull a fast one on naughty userspace, though: we program
1331810bc075SAndy Lutomirski	 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1332810bc075SAndy Lutomirski	 * if it controls the kernel's RSP.  We set DF before we clear
1333810bc075SAndy Lutomirski	 * "NMI executing".
1334905a36a2SIngo Molnar	 */
1335905a36a2SIngo Molnar	lea	6*8(%rsp), %rdx
1336905a36a2SIngo Molnar	/* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1337905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1338905a36a2SIngo Molnar	/* If the stack pointer is above the NMI stack, this is a normal NMI */
1339905a36a2SIngo Molnar	ja	first_nmi
13404d732138SIngo Molnar
1341905a36a2SIngo Molnar	subq	$EXCEPTION_STKSZ, %rdx
1342905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1343905a36a2SIngo Molnar	/* If it is below the NMI stack, it is a normal NMI */
1344905a36a2SIngo Molnar	jb	first_nmi
1345810bc075SAndy Lutomirski
1346810bc075SAndy Lutomirski	/* Ah, it is within the NMI stack. */
1347810bc075SAndy Lutomirski
1348810bc075SAndy Lutomirski	testb	$(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1349810bc075SAndy Lutomirski	jz	first_nmi	/* RSP was user controlled. */
1350810bc075SAndy Lutomirski
1351810bc075SAndy Lutomirski	/* This is a nested NMI. */
1352905a36a2SIngo Molnar
1353905a36a2SIngo Molnarnested_nmi:
1354905a36a2SIngo Molnar	/*
13550b22930eSAndy Lutomirski	 * Modify the "iret" frame to point to repeat_nmi, forcing another
13560b22930eSAndy Lutomirski	 * iteration of NMI handling.
1357905a36a2SIngo Molnar	 */
135823a781e9SAndy Lutomirski	subq	$8, %rsp
1359905a36a2SIngo Molnar	leaq	-10*8(%rsp), %rdx
1360905a36a2SIngo Molnar	pushq	$__KERNEL_DS
1361905a36a2SIngo Molnar	pushq	%rdx
1362905a36a2SIngo Molnar	pushfq
1363905a36a2SIngo Molnar	pushq	$__KERNEL_CS
1364905a36a2SIngo Molnar	pushq	$repeat_nmi
1365905a36a2SIngo Molnar
1366905a36a2SIngo Molnar	/* Put stack back */
1367905a36a2SIngo Molnar	addq	$(6*8), %rsp
1368905a36a2SIngo Molnar
1369905a36a2SIngo Molnarnested_nmi_out:
1370905a36a2SIngo Molnar	popq	%rdx
1371905a36a2SIngo Molnar
13720b22930eSAndy Lutomirski	/* We are returning to kernel mode, so this cannot result in a fault. */
1373929bacecSAndy Lutomirski	iretq
1374905a36a2SIngo Molnar
1375905a36a2SIngo Molnarfirst_nmi:
13760b22930eSAndy Lutomirski	/* Restore rdx. */
1377905a36a2SIngo Molnar	movq	(%rsp), %rdx
1378905a36a2SIngo Molnar
137936f1a77bSAndy Lutomirski	/* Make room for "NMI executing". */
138036f1a77bSAndy Lutomirski	pushq	$0
1381905a36a2SIngo Molnar
13820b22930eSAndy Lutomirski	/* Leave room for the "iret" frame */
1383905a36a2SIngo Molnar	subq	$(5*8), %rsp
1384905a36a2SIngo Molnar
13850b22930eSAndy Lutomirski	/* Copy the "original" frame to the "outermost" frame */
1386905a36a2SIngo Molnar	.rept 5
1387905a36a2SIngo Molnar	pushq	11*8(%rsp)
1388905a36a2SIngo Molnar	.endr
13898c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1390905a36a2SIngo Molnar
1391905a36a2SIngo Molnar	/* Everything up to here is safe from nested NMIs */
1392905a36a2SIngo Molnar
1393a97439aaSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY
1394a97439aaSAndy Lutomirski	/*
1395a97439aaSAndy Lutomirski	 * For ease of testing, unmask NMIs right away.  Disabled by
1396a97439aaSAndy Lutomirski	 * default because IRET is very expensive.
1397a97439aaSAndy Lutomirski	 */
1398a97439aaSAndy Lutomirski	pushq	$0		/* SS */
1399a97439aaSAndy Lutomirski	pushq	%rsp		/* RSP (minus 8 because of the previous push) */
1400a97439aaSAndy Lutomirski	addq	$8, (%rsp)	/* Fix up RSP */
1401a97439aaSAndy Lutomirski	pushfq			/* RFLAGS */
1402a97439aaSAndy Lutomirski	pushq	$__KERNEL_CS	/* CS */
1403a97439aaSAndy Lutomirski	pushq	$1f		/* RIP */
1404929bacecSAndy Lutomirski	iretq			/* continues at repeat_nmi below */
14058c1f7558SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
1406a97439aaSAndy Lutomirski1:
1407a97439aaSAndy Lutomirski#endif
1408a97439aaSAndy Lutomirski
14090b22930eSAndy Lutomirskirepeat_nmi:
14103e3f0695SPeter Zijlstra	ANNOTATE_NOENDBR // this code
1411905a36a2SIngo Molnar	/*
1412905a36a2SIngo Molnar	 * If there was a nested NMI, the first NMI's iret will return
1413905a36a2SIngo Molnar	 * here. But NMIs are still enabled and we can take another
1414905a36a2SIngo Molnar	 * nested NMI. The nested NMI checks the interrupted RIP to see
1415905a36a2SIngo Molnar	 * if it is between repeat_nmi and end_repeat_nmi, and if so
1416905a36a2SIngo Molnar	 * it will just return, as we are about to repeat an NMI anyway.
1417905a36a2SIngo Molnar	 * This makes it safe to copy to the stack frame that a nested
1418905a36a2SIngo Molnar	 * NMI will update.
14190b22930eSAndy Lutomirski	 *
14200b22930eSAndy Lutomirski	 * RSP is pointing to "outermost RIP".  gsbase is unknown, but, if
14210b22930eSAndy Lutomirski	 * we're repeating an NMI, gsbase has the same value that it had on
14220b22930eSAndy Lutomirski	 * the first iteration.  paranoid_entry will load the kernel
14236271fef0SThomas Gleixner	 * gsbase if needed before we call exc_nmi().  "NMI executing"
142436f1a77bSAndy Lutomirski	 * is zero.
1425905a36a2SIngo Molnar	 */
142636f1a77bSAndy Lutomirski	movq	$1, 10*8(%rsp)		/* Set "NMI executing". */
1427905a36a2SIngo Molnar
14280b22930eSAndy Lutomirski	/*
14290b22930eSAndy Lutomirski	 * Copy the "outermost" frame to the "iret" frame.  NMIs that nest
14300b22930eSAndy Lutomirski	 * here must not modify the "iret" frame while we're writing to
14310b22930eSAndy Lutomirski	 * it or it will end up containing garbage.
14320b22930eSAndy Lutomirski	 */
1433905a36a2SIngo Molnar	addq	$(10*8), %rsp
1434905a36a2SIngo Molnar	.rept 5
1435905a36a2SIngo Molnar	pushq	-6*8(%rsp)
1436905a36a2SIngo Molnar	.endr
1437905a36a2SIngo Molnar	subq	$(5*8), %rsp
1438905a36a2SIngo Molnarend_repeat_nmi:
14393e3f0695SPeter Zijlstra	ANNOTATE_NOENDBR // this code
1440905a36a2SIngo Molnar
1441905a36a2SIngo Molnar	/*
14420b22930eSAndy Lutomirski	 * Everything below this point can be preempted by a nested NMI.
14430b22930eSAndy Lutomirski	 * If this happens, then the inner NMI will change the "iret"
14440b22930eSAndy Lutomirski	 * frame to point back to repeat_nmi.
1445905a36a2SIngo Molnar	 */
1446905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
1447905a36a2SIngo Molnar
1448905a36a2SIngo Molnar	/*
1449905a36a2SIngo Molnar	 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1450905a36a2SIngo Molnar	 * as we should not be calling schedule in NMI context.
1451905a36a2SIngo Molnar	 * Even with normal interrupts enabled. An NMI should not be
1452905a36a2SIngo Molnar	 * setting NEED_RESCHED or anything that normal interrupts and
1453905a36a2SIngo Molnar	 * exceptions might do.
1454905a36a2SIngo Molnar	 */
1455905a36a2SIngo Molnar	call	paranoid_entry
14568c1f7558SJosh Poimboeuf	UNWIND_HINT_REGS
1457905a36a2SIngo Molnar
1458905a36a2SIngo Molnar	movq	%rsp, %rdi
1459905a36a2SIngo Molnar	movq	$-1, %rsi
14606271fef0SThomas Gleixner	call	exc_nmi
1461905a36a2SIngo Molnar
14622dbb887eSPeter Zijlstra	/* Always restore stashed SPEC_CTRL value (see paranoid_entry) */
14632dbb887eSPeter Zijlstra	IBRS_EXIT save_reg=%r15
14642dbb887eSPeter Zijlstra
146516561f27SDave Hansen	/* Always restore stashed CR3 value (see paranoid_entry) */
146621e94459SPeter Zijlstra	RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
14678a09317bSDave Hansen
1468c82965f9SChang S. Bae	/*
1469c82965f9SChang S. Bae	 * The above invocation of paranoid_entry stored the GSBASE
1470c82965f9SChang S. Bae	 * related information in R/EBX depending on the availability
1471c82965f9SChang S. Bae	 * of FSGSBASE.
1472c82965f9SChang S. Bae	 *
1473c82965f9SChang S. Bae	 * If FSGSBASE is enabled, restore the saved GSBASE value
1474c82965f9SChang S. Bae	 * unconditionally, otherwise take the conditional SWAPGS path.
1475c82965f9SChang S. Bae	 */
1476c82965f9SChang S. Bae	ALTERNATIVE "jmp nmi_no_fsgsbase", "", X86_FEATURE_FSGSBASE
1477c82965f9SChang S. Bae
1478c82965f9SChang S. Bae	wrgsbase	%rbx
1479c82965f9SChang S. Bae	jmp	nmi_restore
1480c82965f9SChang S. Bae
1481c82965f9SChang S. Baenmi_no_fsgsbase:
1482c82965f9SChang S. Bae	/* EBX == 0 -> invoke SWAPGS */
1483c82965f9SChang S. Bae	testl	%ebx, %ebx
1484905a36a2SIngo Molnar	jnz	nmi_restore
1485c82965f9SChang S. Bae
1486905a36a2SIngo Molnarnmi_swapgs:
148753c9d924SJuergen Gross	swapgs
1488c82965f9SChang S. Bae
1489905a36a2SIngo Molnarnmi_restore:
1490502af0d7SDominik Brodowski	POP_REGS
14910b22930eSAndy Lutomirski
1492471ee483SAndy Lutomirski	/*
1493471ee483SAndy Lutomirski	 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1494471ee483SAndy Lutomirski	 * at the "iret" frame.
1495471ee483SAndy Lutomirski	 */
1496471ee483SAndy Lutomirski	addq	$6*8, %rsp
1497905a36a2SIngo Molnar
1498810bc075SAndy Lutomirski	/*
1499810bc075SAndy Lutomirski	 * Clear "NMI executing".  Set DF first so that we can easily
1500810bc075SAndy Lutomirski	 * distinguish the remaining code between here and IRET from
1501929bacecSAndy Lutomirski	 * the SYSCALL entry and exit paths.
1502929bacecSAndy Lutomirski	 *
1503929bacecSAndy Lutomirski	 * We arguably should just inspect RIP instead, but I (Andy) wrote
1504929bacecSAndy Lutomirski	 * this code when I had the misapprehension that Xen PV supported
1505929bacecSAndy Lutomirski	 * NMIs, and Xen PV would break that approach.
1506810bc075SAndy Lutomirski	 */
1507810bc075SAndy Lutomirski	std
1508810bc075SAndy Lutomirski	movq	$0, 5*8(%rsp)		/* clear "NMI executing" */
15090b22930eSAndy Lutomirski
15100b22930eSAndy Lutomirski	/*
15117caf330fSPawan Gupta	 * Skip CLEAR_CPU_BUFFERS here, since it only helps in rare cases like
15127caf330fSPawan Gupta	 * NMI in kernel after user state is restored. For an unprivileged user
15137caf330fSPawan Gupta	 * these conditions are hard to meet.
15147caf330fSPawan Gupta	 */
15157caf330fSPawan Gupta
15167caf330fSPawan Gupta	/*
1517929bacecSAndy Lutomirski	 * iretq reads the "iret" frame and exits the NMI stack in a
1518929bacecSAndy Lutomirski	 * single instruction.  We are returning to kernel mode, so this
1519929bacecSAndy Lutomirski	 * cannot result in a fault.  Similarly, we don't need to worry
1520929bacecSAndy Lutomirski	 * about espfix64 on the way back to kernel mode.
15210b22930eSAndy Lutomirski	 */
1522929bacecSAndy Lutomirski	iretq
15236271fef0SThomas GleixnerSYM_CODE_END(asm_exc_nmi)
1524905a36a2SIngo Molnar
1525dffb3f9dSAndy Lutomirski#ifndef CONFIG_IA32_EMULATION
1526dffb3f9dSAndy Lutomirski/*
1527dffb3f9dSAndy Lutomirski * This handles SYSCALL from 32-bit code.  There is no way to program
1528dffb3f9dSAndy Lutomirski * MSRs to fully disable 32-bit SYSCALL.
1529dffb3f9dSAndy Lutomirski */
1530bc7b11c0SJiri SlabySYM_CODE_START(ignore_sysret)
1531fb799447SJosh Poimboeuf	UNWIND_HINT_END_OF_STACK
15328f93402bSPeter Zijlstra	ENDBR
1533905a36a2SIngo Molnar	mov	$-ENOSYS, %eax
15347caf330fSPawan Gupta	CLEAR_CPU_BUFFERS
1535b2b1d94cSJan Beulich	sysretl
1536bc7b11c0SJiri SlabySYM_CODE_END(ignore_sysret)
1537dffb3f9dSAndy Lutomirski#endif
15382deb4be2SAndy Lutomirski
1539b9f6976bSThomas Gleixner.pushsection .text, "ax"
154067e93dddSThomas Gleixner	__FUNC_ALIGN
154167e93dddSThomas GleixnerSYM_CODE_START_NOALIGN(rewind_stack_and_make_dead)
15428c1f7558SJosh Poimboeuf	UNWIND_HINT_FUNC
15432deb4be2SAndy Lutomirski	/* Prevent any naive code from trying to unwind to our caller. */
15442deb4be2SAndy Lutomirski	xorl	%ebp, %ebp
15452deb4be2SAndy Lutomirski
1546c063a217SThomas Gleixner	movq	PER_CPU_VAR(pcpu_hot + X86_top_of_stack), %rax
15478c1f7558SJosh Poimboeuf	leaq	-PTREGS_SIZE(%rax), %rsp
1548f977df7bSJann Horn	UNWIND_HINT_REGS
15492deb4be2SAndy Lutomirski
15500e25498fSEric W. Biederman	call	make_task_dead
15510e25498fSEric W. BiedermanSYM_CODE_END(rewind_stack_and_make_dead)
1552b9f6976bSThomas Gleixner.popsection
1553*eb36b0dcSPawan Gupta
1554*eb36b0dcSPawan Gupta/*
1555*eb36b0dcSPawan Gupta * This sequence executes branches in order to remove user branch information
1556*eb36b0dcSPawan Gupta * from the branch history tracker in the Branch Predictor, therefore removing
1557*eb36b0dcSPawan Gupta * user influence on subsequent BTB lookups.
1558*eb36b0dcSPawan Gupta *
1559*eb36b0dcSPawan Gupta * It should be used on parts prior to Alder Lake. Newer parts should use the
1560*eb36b0dcSPawan Gupta * BHI_DIS_S hardware control instead. If a pre-Alder Lake part is being
1561*eb36b0dcSPawan Gupta * virtualized on newer hardware the VMM should protect against BHI attacks by
1562*eb36b0dcSPawan Gupta * setting BHI_DIS_S for the guests.
1563*eb36b0dcSPawan Gupta *
1564*eb36b0dcSPawan Gupta * CALLs/RETs are necessary to prevent Loop Stream Detector(LSD) from engaging
1565*eb36b0dcSPawan Gupta * and not clearing the branch history. The call tree looks like:
1566*eb36b0dcSPawan Gupta *
1567*eb36b0dcSPawan Gupta * call 1
1568*eb36b0dcSPawan Gupta *    call 2
1569*eb36b0dcSPawan Gupta *      call 2
1570*eb36b0dcSPawan Gupta *        call 2
1571*eb36b0dcSPawan Gupta *          call 2
1572*eb36b0dcSPawan Gupta * 	      call 2
1573*eb36b0dcSPawan Gupta * 	      ret
1574*eb36b0dcSPawan Gupta * 	    ret
1575*eb36b0dcSPawan Gupta *        ret
1576*eb36b0dcSPawan Gupta *      ret
1577*eb36b0dcSPawan Gupta *    ret
1578*eb36b0dcSPawan Gupta * ret
1579*eb36b0dcSPawan Gupta *
1580*eb36b0dcSPawan Gupta * This means that the stack is non-constant and ORC can't unwind it with %rsp
1581*eb36b0dcSPawan Gupta * alone.  Therefore we unconditionally set up the frame pointer, which allows
1582*eb36b0dcSPawan Gupta * ORC to unwind properly.
1583*eb36b0dcSPawan Gupta *
1584*eb36b0dcSPawan Gupta * The alignment is for performance and not for safety, and may be safely
1585*eb36b0dcSPawan Gupta * refactored in the future if needed.
1586*eb36b0dcSPawan Gupta */
1587*eb36b0dcSPawan GuptaSYM_FUNC_START(clear_bhb_loop)
1588*eb36b0dcSPawan Gupta	push	%rbp
1589*eb36b0dcSPawan Gupta	mov	%rsp, %rbp
1590*eb36b0dcSPawan Gupta	movl	$5, %ecx
1591*eb36b0dcSPawan Gupta	ANNOTATE_INTRA_FUNCTION_CALL
1592*eb36b0dcSPawan Gupta	call	1f
1593*eb36b0dcSPawan Gupta	jmp	5f
1594*eb36b0dcSPawan Gupta	.align 64, 0xcc
1595*eb36b0dcSPawan Gupta	ANNOTATE_INTRA_FUNCTION_CALL
1596*eb36b0dcSPawan Gupta1:	call	2f
1597*eb36b0dcSPawan Gupta	RET
1598*eb36b0dcSPawan Gupta	.align 64, 0xcc
1599*eb36b0dcSPawan Gupta2:	movl	$5, %eax
1600*eb36b0dcSPawan Gupta3:	jmp	4f
1601*eb36b0dcSPawan Gupta	nop
1602*eb36b0dcSPawan Gupta4:	sub	$1, %eax
1603*eb36b0dcSPawan Gupta	jnz	3b
1604*eb36b0dcSPawan Gupta	sub	$1, %ecx
1605*eb36b0dcSPawan Gupta	jnz	1b
1606*eb36b0dcSPawan Gupta	RET
1607*eb36b0dcSPawan Gupta5:	lfence
1608*eb36b0dcSPawan Gupta	pop	%rbp
1609*eb36b0dcSPawan Gupta	RET
1610*eb36b0dcSPawan GuptaSYM_FUNC_END(clear_bhb_loop)
1611*eb36b0dcSPawan GuptaEXPORT_SYMBOL_GPL(clear_bhb_loop)
1612*eb36b0dcSPawan GuptaSTACK_FRAME_NON_STANDARD(clear_bhb_loop)
1613