xref: /openbmc/linux/arch/x86/entry/entry_64.S (revision cb6f64ed5a04036eef07e70b57dd5dd78f2fbcef)
1905a36a2SIngo Molnar/*
2905a36a2SIngo Molnar *  linux/arch/x86_64/entry.S
3905a36a2SIngo Molnar *
4905a36a2SIngo Molnar *  Copyright (C) 1991, 1992  Linus Torvalds
5905a36a2SIngo Molnar *  Copyright (C) 2000, 2001, 2002  Andi Kleen SuSE Labs
6905a36a2SIngo Molnar *  Copyright (C) 2000  Pavel Machek <pavel@suse.cz>
74d732138SIngo Molnar *
8905a36a2SIngo Molnar * entry.S contains the system-call and fault low-level handling routines.
9905a36a2SIngo Molnar *
10905a36a2SIngo Molnar * Some of this is documented in Documentation/x86/entry_64.txt
11905a36a2SIngo Molnar *
12905a36a2SIngo Molnar * A note on terminology:
13905a36a2SIngo Molnar * - iret frame:	Architecture defined interrupt frame from SS to RIP
14905a36a2SIngo Molnar *			at the top of the kernel process stack.
15905a36a2SIngo Molnar *
16905a36a2SIngo Molnar * Some macro usage:
174d732138SIngo Molnar * - ENTRY/END:		Define functions in the symbol table.
184d732138SIngo Molnar * - TRACE_IRQ_*:	Trace hardirq state for lock debugging.
194d732138SIngo Molnar * - idtentry:		Define exception entry points.
20905a36a2SIngo Molnar */
21905a36a2SIngo Molnar#include <linux/linkage.h>
22905a36a2SIngo Molnar#include <asm/segment.h>
23905a36a2SIngo Molnar#include <asm/cache.h>
24905a36a2SIngo Molnar#include <asm/errno.h>
25d36f9479SIngo Molnar#include "calling.h"
26905a36a2SIngo Molnar#include <asm/asm-offsets.h>
27905a36a2SIngo Molnar#include <asm/msr.h>
28905a36a2SIngo Molnar#include <asm/unistd.h>
29905a36a2SIngo Molnar#include <asm/thread_info.h>
30905a36a2SIngo Molnar#include <asm/hw_irq.h>
31905a36a2SIngo Molnar#include <asm/page_types.h>
32905a36a2SIngo Molnar#include <asm/irqflags.h>
33905a36a2SIngo Molnar#include <asm/paravirt.h>
34905a36a2SIngo Molnar#include <asm/percpu.h>
35905a36a2SIngo Molnar#include <asm/asm.h>
36905a36a2SIngo Molnar#include <asm/context_tracking.h>
37905a36a2SIngo Molnar#include <asm/smap.h>
38905a36a2SIngo Molnar#include <asm/pgtable_types.h>
39905a36a2SIngo Molnar#include <linux/err.h>
40905a36a2SIngo Molnar
41905a36a2SIngo Molnar/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this.  */
42905a36a2SIngo Molnar#include <linux/elf-em.h>
43905a36a2SIngo Molnar#define AUDIT_ARCH_X86_64			(EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
44905a36a2SIngo Molnar#define __AUDIT_ARCH_64BIT			0x80000000
45905a36a2SIngo Molnar#define __AUDIT_ARCH_LE				0x40000000
46905a36a2SIngo Molnar
47905a36a2SIngo Molnar.code64
48905a36a2SIngo Molnar.section .entry.text, "ax"
49905a36a2SIngo Molnar
50905a36a2SIngo Molnar#ifdef CONFIG_PARAVIRT
51905a36a2SIngo MolnarENTRY(native_usergs_sysret64)
52905a36a2SIngo Molnar	swapgs
53905a36a2SIngo Molnar	sysretq
54905a36a2SIngo MolnarENDPROC(native_usergs_sysret64)
55905a36a2SIngo Molnar#endif /* CONFIG_PARAVIRT */
56905a36a2SIngo Molnar
57905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ
58905a36a2SIngo Molnar#ifdef CONFIG_TRACE_IRQFLAGS
59905a36a2SIngo Molnar	bt	$9, EFLAGS(%rsp)		/* interrupts off? */
60905a36a2SIngo Molnar	jnc	1f
61905a36a2SIngo Molnar	TRACE_IRQS_ON
62905a36a2SIngo Molnar1:
63905a36a2SIngo Molnar#endif
64905a36a2SIngo Molnar.endm
65905a36a2SIngo Molnar
66905a36a2SIngo Molnar/*
67905a36a2SIngo Molnar * When dynamic function tracer is enabled it will add a breakpoint
68905a36a2SIngo Molnar * to all locations that it is about to modify, sync CPUs, update
69905a36a2SIngo Molnar * all the code, sync CPUs, then remove the breakpoints. In this time
70905a36a2SIngo Molnar * if lockdep is enabled, it might jump back into the debug handler
71905a36a2SIngo Molnar * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
72905a36a2SIngo Molnar *
73905a36a2SIngo Molnar * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
74905a36a2SIngo Molnar * make sure the stack pointer does not get reset back to the top
75905a36a2SIngo Molnar * of the debug stack, and instead just reuses the current stack.
76905a36a2SIngo Molnar */
77905a36a2SIngo Molnar#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
78905a36a2SIngo Molnar
79905a36a2SIngo Molnar.macro TRACE_IRQS_OFF_DEBUG
80905a36a2SIngo Molnar	call	debug_stack_set_zero
81905a36a2SIngo Molnar	TRACE_IRQS_OFF
82905a36a2SIngo Molnar	call	debug_stack_reset
83905a36a2SIngo Molnar.endm
84905a36a2SIngo Molnar
85905a36a2SIngo Molnar.macro TRACE_IRQS_ON_DEBUG
86905a36a2SIngo Molnar	call	debug_stack_set_zero
87905a36a2SIngo Molnar	TRACE_IRQS_ON
88905a36a2SIngo Molnar	call	debug_stack_reset
89905a36a2SIngo Molnar.endm
90905a36a2SIngo Molnar
91905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ_DEBUG
92905a36a2SIngo Molnar	bt	$9, EFLAGS(%rsp)		/* interrupts off? */
93905a36a2SIngo Molnar	jnc	1f
94905a36a2SIngo Molnar	TRACE_IRQS_ON_DEBUG
95905a36a2SIngo Molnar1:
96905a36a2SIngo Molnar.endm
97905a36a2SIngo Molnar
98905a36a2SIngo Molnar#else
99905a36a2SIngo Molnar# define TRACE_IRQS_OFF_DEBUG			TRACE_IRQS_OFF
100905a36a2SIngo Molnar# define TRACE_IRQS_ON_DEBUG			TRACE_IRQS_ON
101905a36a2SIngo Molnar# define TRACE_IRQS_IRETQ_DEBUG			TRACE_IRQS_IRETQ
102905a36a2SIngo Molnar#endif
103905a36a2SIngo Molnar
104905a36a2SIngo Molnar/*
1054d732138SIngo Molnar * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
106905a36a2SIngo Molnar *
1074d732138SIngo Molnar * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
108905a36a2SIngo Molnar * then loads new ss, cs, and rip from previously programmed MSRs.
109905a36a2SIngo Molnar * rflags gets masked by a value from another MSR (so CLD and CLAC
110905a36a2SIngo Molnar * are not needed). SYSCALL does not save anything on the stack
111905a36a2SIngo Molnar * and does not change rsp.
112905a36a2SIngo Molnar *
113905a36a2SIngo Molnar * Registers on entry:
114905a36a2SIngo Molnar * rax  system call number
115905a36a2SIngo Molnar * rcx  return address
116905a36a2SIngo Molnar * r11  saved rflags (note: r11 is callee-clobbered register in C ABI)
117905a36a2SIngo Molnar * rdi  arg0
118905a36a2SIngo Molnar * rsi  arg1
119905a36a2SIngo Molnar * rdx  arg2
120905a36a2SIngo Molnar * r10  arg3 (needs to be moved to rcx to conform to C ABI)
121905a36a2SIngo Molnar * r8   arg4
122905a36a2SIngo Molnar * r9   arg5
123905a36a2SIngo Molnar * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
124905a36a2SIngo Molnar *
125905a36a2SIngo Molnar * Only called from user space.
126905a36a2SIngo Molnar *
127905a36a2SIngo Molnar * When user can change pt_regs->foo always force IRET. That is because
128905a36a2SIngo Molnar * it deals with uncanonical addresses better. SYSRET has trouble
129905a36a2SIngo Molnar * with them due to bugs in both AMD and Intel CPUs.
130905a36a2SIngo Molnar */
131905a36a2SIngo Molnar
132b2502b41SIngo MolnarENTRY(entry_SYSCALL_64)
133905a36a2SIngo Molnar	/*
134905a36a2SIngo Molnar	 * Interrupts are off on entry.
135905a36a2SIngo Molnar	 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
136905a36a2SIngo Molnar	 * it is too small to ever cause noticeable irq latency.
137905a36a2SIngo Molnar	 */
138905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
139905a36a2SIngo Molnar	/*
140905a36a2SIngo Molnar	 * A hypervisor implementation might want to use a label
141905a36a2SIngo Molnar	 * after the swapgs, so that it can do the swapgs
142905a36a2SIngo Molnar	 * for the guest and jump here on syscall.
143905a36a2SIngo Molnar	 */
144b2502b41SIngo MolnarGLOBAL(entry_SYSCALL_64_after_swapgs)
145905a36a2SIngo Molnar
146905a36a2SIngo Molnar	movq	%rsp, PER_CPU_VAR(rsp_scratch)
147905a36a2SIngo Molnar	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
148905a36a2SIngo Molnar
149905a36a2SIngo Molnar	/* Construct struct pt_regs on stack */
150905a36a2SIngo Molnar	pushq	$__USER_DS			/* pt_regs->ss */
151905a36a2SIngo Molnar	pushq	PER_CPU_VAR(rsp_scratch)	/* pt_regs->sp */
152905a36a2SIngo Molnar	/*
153905a36a2SIngo Molnar	 * Re-enable interrupts.
154905a36a2SIngo Molnar	 * We use 'rsp_scratch' as a scratch space, hence irq-off block above
155905a36a2SIngo Molnar	 * must execute atomically in the face of possible interrupt-driven
156905a36a2SIngo Molnar	 * task preemption. We must enable interrupts only after we're done
157905a36a2SIngo Molnar	 * with using rsp_scratch:
158905a36a2SIngo Molnar	 */
159905a36a2SIngo Molnar	ENABLE_INTERRUPTS(CLBR_NONE)
160905a36a2SIngo Molnar	pushq	%r11				/* pt_regs->flags */
161905a36a2SIngo Molnar	pushq	$__USER_CS			/* pt_regs->cs */
162905a36a2SIngo Molnar	pushq	%rcx				/* pt_regs->ip */
163905a36a2SIngo Molnar	pushq	%rax				/* pt_regs->orig_ax */
164905a36a2SIngo Molnar	pushq	%rdi				/* pt_regs->di */
165905a36a2SIngo Molnar	pushq	%rsi				/* pt_regs->si */
166905a36a2SIngo Molnar	pushq	%rdx				/* pt_regs->dx */
167905a36a2SIngo Molnar	pushq	%rcx				/* pt_regs->cx */
168905a36a2SIngo Molnar	pushq	$-ENOSYS			/* pt_regs->ax */
169905a36a2SIngo Molnar	pushq	%r8				/* pt_regs->r8 */
170905a36a2SIngo Molnar	pushq	%r9				/* pt_regs->r9 */
171905a36a2SIngo Molnar	pushq	%r10				/* pt_regs->r10 */
172905a36a2SIngo Molnar	pushq	%r11				/* pt_regs->r11 */
173905a36a2SIngo Molnar	sub	$(6*8), %rsp			/* pt_regs->bp, bx, r12-15 not saved */
174905a36a2SIngo Molnar
175905a36a2SIngo Molnar	testl	$_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
176905a36a2SIngo Molnar	jnz	tracesys
177b2502b41SIngo Molnarentry_SYSCALL_64_fastpath:
178905a36a2SIngo Molnar#if __SYSCALL_MASK == ~0
179905a36a2SIngo Molnar	cmpq	$__NR_syscall_max, %rax
180905a36a2SIngo Molnar#else
181905a36a2SIngo Molnar	andl	$__SYSCALL_MASK, %eax
182905a36a2SIngo Molnar	cmpl	$__NR_syscall_max, %eax
183905a36a2SIngo Molnar#endif
184905a36a2SIngo Molnar	ja	1f				/* return -ENOSYS (already in pt_regs->ax) */
185905a36a2SIngo Molnar	movq	%r10, %rcx
186905a36a2SIngo Molnar	call	*sys_call_table(, %rax, 8)
187905a36a2SIngo Molnar	movq	%rax, RAX(%rsp)
188905a36a2SIngo Molnar1:
189905a36a2SIngo Molnar/*
190905a36a2SIngo Molnar * Syscall return path ending with SYSRET (fast path).
191905a36a2SIngo Molnar * Has incompletely filled pt_regs.
192905a36a2SIngo Molnar */
193905a36a2SIngo Molnar	LOCKDEP_SYS_EXIT
194905a36a2SIngo Molnar	/*
195905a36a2SIngo Molnar	 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
196905a36a2SIngo Molnar	 * it is too small to ever cause noticeable irq latency.
197905a36a2SIngo Molnar	 */
198905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_NONE)
199905a36a2SIngo Molnar
200905a36a2SIngo Molnar	/*
201905a36a2SIngo Molnar	 * We must check ti flags with interrupts (or at least preemption)
202905a36a2SIngo Molnar	 * off because we must *never* return to userspace without
203905a36a2SIngo Molnar	 * processing exit work that is enqueued if we're preempted here.
204905a36a2SIngo Molnar	 * In particular, returning to userspace with any of the one-shot
205905a36a2SIngo Molnar	 * flags (TIF_NOTIFY_RESUME, TIF_USER_RETURN_NOTIFY, etc) set is
206905a36a2SIngo Molnar	 * very bad.
207905a36a2SIngo Molnar	 */
208905a36a2SIngo Molnar	testl	$_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
209905a36a2SIngo Molnar	jnz	int_ret_from_sys_call_irqs_off	/* Go to the slow path */
210905a36a2SIngo Molnar
211905a36a2SIngo Molnar	RESTORE_C_REGS_EXCEPT_RCX_R11
212905a36a2SIngo Molnar	movq	RIP(%rsp), %rcx
213905a36a2SIngo Molnar	movq	EFLAGS(%rsp), %r11
214905a36a2SIngo Molnar	movq	RSP(%rsp), %rsp
215905a36a2SIngo Molnar	/*
2164d732138SIngo Molnar	 * 64-bit SYSRET restores rip from rcx,
217905a36a2SIngo Molnar	 * rflags from r11 (but RF and VM bits are forced to 0),
218905a36a2SIngo Molnar	 * cs and ss are loaded from MSRs.
219905a36a2SIngo Molnar	 * Restoration of rflags re-enables interrupts.
220905a36a2SIngo Molnar	 *
221905a36a2SIngo Molnar	 * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss
222905a36a2SIngo Molnar	 * descriptor is not reinitialized.  This means that we should
223905a36a2SIngo Molnar	 * avoid SYSRET with SS == NULL, which could happen if we schedule,
224905a36a2SIngo Molnar	 * exit the kernel, and re-enter using an interrupt vector.  (All
225905a36a2SIngo Molnar	 * interrupt entries on x86_64 set SS to NULL.)  We prevent that
226905a36a2SIngo Molnar	 * from happening by reloading SS in __switch_to.  (Actually
227905a36a2SIngo Molnar	 * detecting the failure in 64-bit userspace is tricky but can be
228905a36a2SIngo Molnar	 * done.)
229905a36a2SIngo Molnar	 */
230905a36a2SIngo Molnar	USERGS_SYSRET64
231905a36a2SIngo Molnar
232905a36a2SIngo Molnar	/* Do syscall entry tracing */
233905a36a2SIngo Molnartracesys:
234905a36a2SIngo Molnar	movq	%rsp, %rdi
235905a36a2SIngo Molnar	movl	$AUDIT_ARCH_X86_64, %esi
236905a36a2SIngo Molnar	call	syscall_trace_enter_phase1
237905a36a2SIngo Molnar	test	%rax, %rax
238905a36a2SIngo Molnar	jnz	tracesys_phase2			/* if needed, run the slow path */
239905a36a2SIngo Molnar	RESTORE_C_REGS_EXCEPT_RAX		/* else restore clobbered regs */
240905a36a2SIngo Molnar	movq	ORIG_RAX(%rsp), %rax
241b2502b41SIngo Molnar	jmp	entry_SYSCALL_64_fastpath	/* and return to the fast path */
242905a36a2SIngo Molnar
243905a36a2SIngo Molnartracesys_phase2:
244905a36a2SIngo Molnar	SAVE_EXTRA_REGS
245905a36a2SIngo Molnar	movq	%rsp, %rdi
246905a36a2SIngo Molnar	movl	$AUDIT_ARCH_X86_64, %esi
247905a36a2SIngo Molnar	movq	%rax, %rdx
248905a36a2SIngo Molnar	call	syscall_trace_enter_phase2
249905a36a2SIngo Molnar
250905a36a2SIngo Molnar	/*
251905a36a2SIngo Molnar	 * Reload registers from stack in case ptrace changed them.
252905a36a2SIngo Molnar	 * We don't reload %rax because syscall_trace_entry_phase2() returned
253905a36a2SIngo Molnar	 * the value it wants us to use in the table lookup.
254905a36a2SIngo Molnar	 */
255905a36a2SIngo Molnar	RESTORE_C_REGS_EXCEPT_RAX
256905a36a2SIngo Molnar	RESTORE_EXTRA_REGS
257905a36a2SIngo Molnar#if __SYSCALL_MASK == ~0
258905a36a2SIngo Molnar	cmpq	$__NR_syscall_max, %rax
259905a36a2SIngo Molnar#else
260905a36a2SIngo Molnar	andl	$__SYSCALL_MASK, %eax
261905a36a2SIngo Molnar	cmpl	$__NR_syscall_max, %eax
262905a36a2SIngo Molnar#endif
263905a36a2SIngo Molnar	ja	1f				/* return -ENOSYS (already in pt_regs->ax) */
264905a36a2SIngo Molnar	movq	%r10, %rcx			/* fixup for C */
265905a36a2SIngo Molnar	call	*sys_call_table(, %rax, 8)
266905a36a2SIngo Molnar	movq	%rax, RAX(%rsp)
267905a36a2SIngo Molnar1:
268905a36a2SIngo Molnar	/* Use IRET because user could have changed pt_regs->foo */
269905a36a2SIngo Molnar
270905a36a2SIngo Molnar/*
271905a36a2SIngo Molnar * Syscall return path ending with IRET.
272905a36a2SIngo Molnar * Has correct iret frame.
273905a36a2SIngo Molnar */
274905a36a2SIngo MolnarGLOBAL(int_ret_from_sys_call)
275905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_NONE)
276905a36a2SIngo Molnarint_ret_from_sys_call_irqs_off: /* jumps come here from the irqs-off SYSRET path */
277905a36a2SIngo Molnar	TRACE_IRQS_OFF
278905a36a2SIngo Molnar	movl	$_TIF_ALLWORK_MASK, %edi
279905a36a2SIngo Molnar	/* edi:	mask to check */
280905a36a2SIngo MolnarGLOBAL(int_with_check)
281905a36a2SIngo Molnar	LOCKDEP_SYS_EXIT_IRQ
282905a36a2SIngo Molnar	GET_THREAD_INFO(%rcx)
283905a36a2SIngo Molnar	movl	TI_flags(%rcx), %edx
284905a36a2SIngo Molnar	andl	%edi, %edx
285905a36a2SIngo Molnar	jnz	int_careful
286905a36a2SIngo Molnar	andl	$~TS_COMPAT, TI_status(%rcx)
287905a36a2SIngo Molnar	jmp	syscall_return
288905a36a2SIngo Molnar
2894d732138SIngo Molnar	/*
2904d732138SIngo Molnar	 * Either reschedule or signal or syscall exit tracking needed.
2914d732138SIngo Molnar	 * First do a reschedule test.
2924d732138SIngo Molnar	 * edx:	work, edi: workmask
2934d732138SIngo Molnar	 */
294905a36a2SIngo Molnarint_careful:
295905a36a2SIngo Molnar	bt	$TIF_NEED_RESCHED, %edx
296905a36a2SIngo Molnar	jnc	int_very_careful
297905a36a2SIngo Molnar	TRACE_IRQS_ON
298905a36a2SIngo Molnar	ENABLE_INTERRUPTS(CLBR_NONE)
299905a36a2SIngo Molnar	pushq	%rdi
300905a36a2SIngo Molnar	SCHEDULE_USER
301905a36a2SIngo Molnar	popq	%rdi
302905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_NONE)
303905a36a2SIngo Molnar	TRACE_IRQS_OFF
304905a36a2SIngo Molnar	jmp	int_with_check
305905a36a2SIngo Molnar
306905a36a2SIngo Molnar	/* handle signals and tracing -- both require a full pt_regs */
307905a36a2SIngo Molnarint_very_careful:
308905a36a2SIngo Molnar	TRACE_IRQS_ON
309905a36a2SIngo Molnar	ENABLE_INTERRUPTS(CLBR_NONE)
310905a36a2SIngo Molnar	SAVE_EXTRA_REGS
311905a36a2SIngo Molnar	/* Check for syscall exit trace */
312905a36a2SIngo Molnar	testl	$_TIF_WORK_SYSCALL_EXIT, %edx
313905a36a2SIngo Molnar	jz	int_signal
314905a36a2SIngo Molnar	pushq	%rdi
3154d732138SIngo Molnar	leaq	8(%rsp), %rdi			/* &ptregs -> arg1 */
316905a36a2SIngo Molnar	call	syscall_trace_leave
317905a36a2SIngo Molnar	popq	%rdi
318905a36a2SIngo Molnar	andl	$~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU), %edi
319905a36a2SIngo Molnar	jmp	int_restore_rest
320905a36a2SIngo Molnar
321905a36a2SIngo Molnarint_signal:
322905a36a2SIngo Molnar	testl	$_TIF_DO_NOTIFY_MASK, %edx
323905a36a2SIngo Molnar	jz	1f
3244d732138SIngo Molnar	movq	%rsp, %rdi			/* &ptregs -> arg1 */
3254d732138SIngo Molnar	xorl	%esi, %esi			/* oldset -> arg2 */
326905a36a2SIngo Molnar	call	do_notify_resume
327905a36a2SIngo Molnar1:	movl	$_TIF_WORK_MASK, %edi
328905a36a2SIngo Molnarint_restore_rest:
329905a36a2SIngo Molnar	RESTORE_EXTRA_REGS
330905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_NONE)
331905a36a2SIngo Molnar	TRACE_IRQS_OFF
332905a36a2SIngo Molnar	jmp	int_with_check
333905a36a2SIngo Molnar
334905a36a2SIngo Molnarsyscall_return:
335905a36a2SIngo Molnar	/* The IRETQ could re-enable interrupts: */
336905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_ANY)
337905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
338905a36a2SIngo Molnar
339905a36a2SIngo Molnar	/*
340905a36a2SIngo Molnar	 * Try to use SYSRET instead of IRET if we're returning to
341905a36a2SIngo Molnar	 * a completely clean 64-bit userspace context.
342905a36a2SIngo Molnar	 */
343905a36a2SIngo Molnar	movq	RCX(%rsp), %rcx
344905a36a2SIngo Molnar	movq	RIP(%rsp), %r11
345905a36a2SIngo Molnar	cmpq	%rcx, %r11			/* RCX == RIP */
346905a36a2SIngo Molnar	jne	opportunistic_sysret_failed
347905a36a2SIngo Molnar
348905a36a2SIngo Molnar	/*
349905a36a2SIngo Molnar	 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
350905a36a2SIngo Molnar	 * in kernel space.  This essentially lets the user take over
351905a36a2SIngo Molnar	 * the kernel, since userspace controls RSP.
352905a36a2SIngo Molnar	 *
353905a36a2SIngo Molnar	 * If width of "canonical tail" ever becomes variable, this will need
354905a36a2SIngo Molnar	 * to be updated to remain correct on both old and new CPUs.
355905a36a2SIngo Molnar	 */
356905a36a2SIngo Molnar	.ifne __VIRTUAL_MASK_SHIFT - 47
357905a36a2SIngo Molnar	.error "virtual address width changed -- SYSRET checks need update"
358905a36a2SIngo Molnar	.endif
3594d732138SIngo Molnar
360905a36a2SIngo Molnar	/* Change top 16 bits to be the sign-extension of 47th bit */
361905a36a2SIngo Molnar	shl	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
362905a36a2SIngo Molnar	sar	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
3634d732138SIngo Molnar
364905a36a2SIngo Molnar	/* If this changed %rcx, it was not canonical */
365905a36a2SIngo Molnar	cmpq	%rcx, %r11
366905a36a2SIngo Molnar	jne	opportunistic_sysret_failed
367905a36a2SIngo Molnar
368905a36a2SIngo Molnar	cmpq	$__USER_CS, CS(%rsp)		/* CS must match SYSRET */
369905a36a2SIngo Molnar	jne	opportunistic_sysret_failed
370905a36a2SIngo Molnar
371905a36a2SIngo Molnar	movq	R11(%rsp), %r11
372905a36a2SIngo Molnar	cmpq	%r11, EFLAGS(%rsp)		/* R11 == RFLAGS */
373905a36a2SIngo Molnar	jne	opportunistic_sysret_failed
374905a36a2SIngo Molnar
375905a36a2SIngo Molnar	/*
376905a36a2SIngo Molnar	 * SYSRET can't restore RF.  SYSRET can restore TF, but unlike IRET,
377905a36a2SIngo Molnar	 * restoring TF results in a trap from userspace immediately after
378905a36a2SIngo Molnar	 * SYSRET.  This would cause an infinite loop whenever #DB happens
379905a36a2SIngo Molnar	 * with register state that satisfies the opportunistic SYSRET
380905a36a2SIngo Molnar	 * conditions.  For example, single-stepping this user code:
381905a36a2SIngo Molnar	 *
382905a36a2SIngo Molnar	 *           movq	$stuck_here, %rcx
383905a36a2SIngo Molnar	 *           pushfq
384905a36a2SIngo Molnar	 *           popq %r11
385905a36a2SIngo Molnar	 *   stuck_here:
386905a36a2SIngo Molnar	 *
387905a36a2SIngo Molnar	 * would never get past 'stuck_here'.
388905a36a2SIngo Molnar	 */
389905a36a2SIngo Molnar	testq	$(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
390905a36a2SIngo Molnar	jnz	opportunistic_sysret_failed
391905a36a2SIngo Molnar
392905a36a2SIngo Molnar	/* nothing to check for RSP */
393905a36a2SIngo Molnar
394905a36a2SIngo Molnar	cmpq	$__USER_DS, SS(%rsp)		/* SS must match SYSRET */
395905a36a2SIngo Molnar	jne	opportunistic_sysret_failed
396905a36a2SIngo Molnar
397905a36a2SIngo Molnar	/*
398905a36a2SIngo Molnar	 * We win! This label is here just for ease of understanding
399905a36a2SIngo Molnar	 * perf profiles. Nothing jumps here.
400905a36a2SIngo Molnar	 */
401905a36a2SIngo Molnarsyscall_return_via_sysret:
402905a36a2SIngo Molnar	/* rcx and r11 are already restored (see code above) */
403905a36a2SIngo Molnar	RESTORE_C_REGS_EXCEPT_RCX_R11
404905a36a2SIngo Molnar	movq	RSP(%rsp), %rsp
405905a36a2SIngo Molnar	USERGS_SYSRET64
406905a36a2SIngo Molnar
407905a36a2SIngo Molnaropportunistic_sysret_failed:
408905a36a2SIngo Molnar	SWAPGS
409905a36a2SIngo Molnar	jmp	restore_c_regs_and_iret
410b2502b41SIngo MolnarEND(entry_SYSCALL_64)
411905a36a2SIngo Molnar
412905a36a2SIngo Molnar
413905a36a2SIngo Molnar	.macro FORK_LIKE func
414905a36a2SIngo MolnarENTRY(stub_\func)
415905a36a2SIngo Molnar	SAVE_EXTRA_REGS 8
416905a36a2SIngo Molnar	jmp	sys_\func
417905a36a2SIngo MolnarEND(stub_\func)
418905a36a2SIngo Molnar	.endm
419905a36a2SIngo Molnar
420905a36a2SIngo Molnar	FORK_LIKE  clone
421905a36a2SIngo Molnar	FORK_LIKE  fork
422905a36a2SIngo Molnar	FORK_LIKE  vfork
423905a36a2SIngo Molnar
424905a36a2SIngo MolnarENTRY(stub_execve)
425905a36a2SIngo Molnar	call	sys_execve
426905a36a2SIngo Molnarreturn_from_execve:
427905a36a2SIngo Molnar	testl	%eax, %eax
428905a36a2SIngo Molnar	jz	1f
429905a36a2SIngo Molnar	/* exec failed, can use fast SYSRET code path in this case */
430905a36a2SIngo Molnar	ret
431905a36a2SIngo Molnar1:
432905a36a2SIngo Molnar	/* must use IRET code path (pt_regs->cs may have changed) */
433905a36a2SIngo Molnar	addq	$8, %rsp
434905a36a2SIngo Molnar	ZERO_EXTRA_REGS
435905a36a2SIngo Molnar	movq	%rax, RAX(%rsp)
436905a36a2SIngo Molnar	jmp	int_ret_from_sys_call
437905a36a2SIngo MolnarEND(stub_execve)
438905a36a2SIngo Molnar/*
439905a36a2SIngo Molnar * Remaining execve stubs are only 7 bytes long.
440905a36a2SIngo Molnar * ENTRY() often aligns to 16 bytes, which in this case has no benefits.
441905a36a2SIngo Molnar */
442905a36a2SIngo Molnar	.align	8
443905a36a2SIngo MolnarGLOBAL(stub_execveat)
444905a36a2SIngo Molnar	call	sys_execveat
445905a36a2SIngo Molnar	jmp	return_from_execve
446905a36a2SIngo MolnarEND(stub_execveat)
447905a36a2SIngo Molnar
448905a36a2SIngo Molnar#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION)
449905a36a2SIngo Molnar	.align	8
450905a36a2SIngo MolnarGLOBAL(stub_x32_execve)
451905a36a2SIngo MolnarGLOBAL(stub32_execve)
452905a36a2SIngo Molnar	call	compat_sys_execve
453905a36a2SIngo Molnar	jmp	return_from_execve
454905a36a2SIngo MolnarEND(stub32_execve)
455905a36a2SIngo MolnarEND(stub_x32_execve)
456905a36a2SIngo Molnar	.align	8
457905a36a2SIngo MolnarGLOBAL(stub_x32_execveat)
458905a36a2SIngo MolnarGLOBAL(stub32_execveat)
459905a36a2SIngo Molnar	call	compat_sys_execveat
460905a36a2SIngo Molnar	jmp	return_from_execve
461905a36a2SIngo MolnarEND(stub32_execveat)
462905a36a2SIngo MolnarEND(stub_x32_execveat)
463905a36a2SIngo Molnar#endif
464905a36a2SIngo Molnar
465905a36a2SIngo Molnar/*
466905a36a2SIngo Molnar * sigreturn is special because it needs to restore all registers on return.
467905a36a2SIngo Molnar * This cannot be done with SYSRET, so use the IRET return path instead.
468905a36a2SIngo Molnar */
469905a36a2SIngo MolnarENTRY(stub_rt_sigreturn)
470905a36a2SIngo Molnar	/*
471905a36a2SIngo Molnar	 * SAVE_EXTRA_REGS result is not normally needed:
472905a36a2SIngo Molnar	 * sigreturn overwrites all pt_regs->GPREGS.
473905a36a2SIngo Molnar	 * But sigreturn can fail (!), and there is no easy way to detect that.
474905a36a2SIngo Molnar	 * To make sure RESTORE_EXTRA_REGS doesn't restore garbage on error,
475905a36a2SIngo Molnar	 * we SAVE_EXTRA_REGS here.
476905a36a2SIngo Molnar	 */
477905a36a2SIngo Molnar	SAVE_EXTRA_REGS 8
478905a36a2SIngo Molnar	call	sys_rt_sigreturn
479905a36a2SIngo Molnarreturn_from_stub:
480905a36a2SIngo Molnar	addq	$8, %rsp
481905a36a2SIngo Molnar	RESTORE_EXTRA_REGS
482905a36a2SIngo Molnar	movq	%rax, RAX(%rsp)
483905a36a2SIngo Molnar	jmp	int_ret_from_sys_call
484905a36a2SIngo MolnarEND(stub_rt_sigreturn)
485905a36a2SIngo Molnar
486905a36a2SIngo Molnar#ifdef CONFIG_X86_X32_ABI
487905a36a2SIngo MolnarENTRY(stub_x32_rt_sigreturn)
488905a36a2SIngo Molnar	SAVE_EXTRA_REGS 8
489905a36a2SIngo Molnar	call	sys32_x32_rt_sigreturn
490905a36a2SIngo Molnar	jmp	return_from_stub
491905a36a2SIngo MolnarEND(stub_x32_rt_sigreturn)
492905a36a2SIngo Molnar#endif
493905a36a2SIngo Molnar
494905a36a2SIngo Molnar/*
495905a36a2SIngo Molnar * A newly forked process directly context switches into this address.
496905a36a2SIngo Molnar *
497905a36a2SIngo Molnar * rdi: prev task we switched from
498905a36a2SIngo Molnar */
499905a36a2SIngo MolnarENTRY(ret_from_fork)
500905a36a2SIngo Molnar
501905a36a2SIngo Molnar	LOCK ; btr $TIF_FORK, TI_flags(%r8)
502905a36a2SIngo Molnar
503905a36a2SIngo Molnar	pushq	$0x0002
5044d732138SIngo Molnar	popfq					/* reset kernel eflags */
505905a36a2SIngo Molnar
5064d732138SIngo Molnar	call	schedule_tail			/* rdi: 'prev' task parameter */
507905a36a2SIngo Molnar
508905a36a2SIngo Molnar	RESTORE_EXTRA_REGS
509905a36a2SIngo Molnar
5104d732138SIngo Molnar	testb	$3, CS(%rsp)			/* from kernel_thread? */
511905a36a2SIngo Molnar
512905a36a2SIngo Molnar	/*
513905a36a2SIngo Molnar	 * By the time we get here, we have no idea whether our pt_regs,
514905a36a2SIngo Molnar	 * ti flags, and ti status came from the 64-bit SYSCALL fast path,
515138bd56aSIngo Molnar	 * the slow path, or one of the 32-bit compat paths.
516905a36a2SIngo Molnar	 * Use IRET code path to return, since it can safely handle
517905a36a2SIngo Molnar	 * all of the above.
518905a36a2SIngo Molnar	 */
519905a36a2SIngo Molnar	jnz	int_ret_from_sys_call
520905a36a2SIngo Molnar
5214d732138SIngo Molnar	/*
5224d732138SIngo Molnar	 * We came from kernel_thread
5234d732138SIngo Molnar	 * nb: we depend on RESTORE_EXTRA_REGS above
5244d732138SIngo Molnar	 */
525905a36a2SIngo Molnar	movq	%rbp, %rdi
526905a36a2SIngo Molnar	call	*%rbx
527905a36a2SIngo Molnar	movl	$0, RAX(%rsp)
528905a36a2SIngo Molnar	RESTORE_EXTRA_REGS
529905a36a2SIngo Molnar	jmp	int_ret_from_sys_call
530905a36a2SIngo MolnarEND(ret_from_fork)
531905a36a2SIngo Molnar
532905a36a2SIngo Molnar/*
533905a36a2SIngo Molnar * Build the entry stubs with some assembler magic.
534905a36a2SIngo Molnar * We pack 1 stub into every 8-byte block.
535905a36a2SIngo Molnar */
536905a36a2SIngo Molnar	.align 8
537905a36a2SIngo MolnarENTRY(irq_entries_start)
538905a36a2SIngo Molnar    vector=FIRST_EXTERNAL_VECTOR
539905a36a2SIngo Molnar    .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
540905a36a2SIngo Molnar	pushq	$(~vector+0x80)			/* Note: always in signed byte range */
541905a36a2SIngo Molnar    vector=vector+1
542905a36a2SIngo Molnar	jmp	common_interrupt
543905a36a2SIngo Molnar	.align	8
544905a36a2SIngo Molnar    .endr
545905a36a2SIngo MolnarEND(irq_entries_start)
546905a36a2SIngo Molnar
547905a36a2SIngo Molnar/*
548905a36a2SIngo Molnar * Interrupt entry/exit.
549905a36a2SIngo Molnar *
550905a36a2SIngo Molnar * Interrupt entry points save only callee clobbered registers in fast path.
551905a36a2SIngo Molnar *
552905a36a2SIngo Molnar * Entry runs with interrupts off.
553905a36a2SIngo Molnar */
554905a36a2SIngo Molnar
555905a36a2SIngo Molnar/* 0(%rsp): ~(interrupt number) */
556905a36a2SIngo Molnar	.macro interrupt func
557905a36a2SIngo Molnar	cld
558905a36a2SIngo Molnar	/*
559905a36a2SIngo Molnar	 * Since nothing in interrupt handling code touches r12...r15 members
560905a36a2SIngo Molnar	 * of "struct pt_regs", and since interrupts can nest, we can save
561905a36a2SIngo Molnar	 * four stack slots and simultaneously provide
562905a36a2SIngo Molnar	 * an unwind-friendly stack layout by saving "truncated" pt_regs
563905a36a2SIngo Molnar	 * exactly up to rbp slot, without these members.
564905a36a2SIngo Molnar	 */
565905a36a2SIngo Molnar	ALLOC_PT_GPREGS_ON_STACK -RBP
566905a36a2SIngo Molnar	SAVE_C_REGS -RBP
567905a36a2SIngo Molnar	/* this goes to 0(%rsp) for unwinder, not for saving the value: */
568905a36a2SIngo Molnar	SAVE_EXTRA_REGS_RBP -RBP
569905a36a2SIngo Molnar
570905a36a2SIngo Molnar	leaq	-RBP(%rsp), %rdi		/* arg1 for \func (pointer to pt_regs) */
571905a36a2SIngo Molnar
572905a36a2SIngo Molnar	testb	$3, CS-RBP(%rsp)
573905a36a2SIngo Molnar	jz	1f
574905a36a2SIngo Molnar	SWAPGS
575905a36a2SIngo Molnar1:
576905a36a2SIngo Molnar	/*
577905a36a2SIngo Molnar	 * Save previous stack pointer, optionally switch to interrupt stack.
578905a36a2SIngo Molnar	 * irq_count is used to check if a CPU is already on an interrupt stack
579905a36a2SIngo Molnar	 * or not. While this is essentially redundant with preempt_count it is
580905a36a2SIngo Molnar	 * a little cheaper to use a separate counter in the PDA (short of
581905a36a2SIngo Molnar	 * moving irq_enter into assembly, which would be too much work)
582905a36a2SIngo Molnar	 */
583905a36a2SIngo Molnar	movq	%rsp, %rsi
584905a36a2SIngo Molnar	incl	PER_CPU_VAR(irq_count)
585905a36a2SIngo Molnar	cmovzq	PER_CPU_VAR(irq_stack_ptr), %rsp
586905a36a2SIngo Molnar	pushq	%rsi
587905a36a2SIngo Molnar	/* We entered an interrupt context - irqs are off: */
588905a36a2SIngo Molnar	TRACE_IRQS_OFF
589905a36a2SIngo Molnar
590905a36a2SIngo Molnar	call	\func
591905a36a2SIngo Molnar	.endm
592905a36a2SIngo Molnar
593905a36a2SIngo Molnar	/*
594905a36a2SIngo Molnar	 * The interrupt stubs push (~vector+0x80) onto the stack and
595905a36a2SIngo Molnar	 * then jump to common_interrupt.
596905a36a2SIngo Molnar	 */
597905a36a2SIngo Molnar	.p2align CONFIG_X86_L1_CACHE_SHIFT
598905a36a2SIngo Molnarcommon_interrupt:
599905a36a2SIngo Molnar	ASM_CLAC
600905a36a2SIngo Molnar	addq	$-0x80, (%rsp)			/* Adjust vector to [-256, -1] range */
601905a36a2SIngo Molnar	interrupt do_IRQ
602905a36a2SIngo Molnar	/* 0(%rsp): old RSP */
603905a36a2SIngo Molnarret_from_intr:
604905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_NONE)
605905a36a2SIngo Molnar	TRACE_IRQS_OFF
606905a36a2SIngo Molnar	decl	PER_CPU_VAR(irq_count)
607905a36a2SIngo Molnar
608905a36a2SIngo Molnar	/* Restore saved previous stack */
609905a36a2SIngo Molnar	popq	%rsi
610905a36a2SIngo Molnar	/* return code expects complete pt_regs - adjust rsp accordingly: */
611905a36a2SIngo Molnar	leaq	-RBP(%rsi), %rsp
612905a36a2SIngo Molnar
613905a36a2SIngo Molnar	testb	$3, CS(%rsp)
614905a36a2SIngo Molnar	jz	retint_kernel
615905a36a2SIngo Molnar	/* Interrupt came from user space */
6165e99cb7cSAndy LutomirskiGLOBAL(retint_user)
617905a36a2SIngo Molnar	GET_THREAD_INFO(%rcx)
6184d732138SIngo Molnar
6194d732138SIngo Molnar	/* %rcx: thread info. Interrupts are off. */
620905a36a2SIngo Molnarretint_with_reschedule:
621905a36a2SIngo Molnar	movl	$_TIF_WORK_MASK, %edi
622905a36a2SIngo Molnarretint_check:
623905a36a2SIngo Molnar	LOCKDEP_SYS_EXIT_IRQ
624905a36a2SIngo Molnar	movl	TI_flags(%rcx), %edx
625905a36a2SIngo Molnar	andl	%edi, %edx
626905a36a2SIngo Molnar	jnz	retint_careful
627905a36a2SIngo Molnar
628905a36a2SIngo Molnarretint_swapgs:					/* return to user-space */
629905a36a2SIngo Molnar	/*
630905a36a2SIngo Molnar	 * The iretq could re-enable interrupts:
631905a36a2SIngo Molnar	 */
632905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_ANY)
633905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
634905a36a2SIngo Molnar
635905a36a2SIngo Molnar	SWAPGS
636905a36a2SIngo Molnar	jmp	restore_c_regs_and_iret
637905a36a2SIngo Molnar
638905a36a2SIngo Molnar/* Returning to kernel space */
639905a36a2SIngo Molnarretint_kernel:
640905a36a2SIngo Molnar#ifdef CONFIG_PREEMPT
641905a36a2SIngo Molnar	/* Interrupts are off */
642905a36a2SIngo Molnar	/* Check if we need preemption */
6434d732138SIngo Molnar	bt	$9, EFLAGS(%rsp)		/* were interrupts off? */
644905a36a2SIngo Molnar	jnc	1f
645905a36a2SIngo Molnar0:	cmpl	$0, PER_CPU_VAR(__preempt_count)
646905a36a2SIngo Molnar	jnz	1f
647905a36a2SIngo Molnar	call	preempt_schedule_irq
648905a36a2SIngo Molnar	jmp	0b
649905a36a2SIngo Molnar1:
650905a36a2SIngo Molnar#endif
651905a36a2SIngo Molnar	/*
652905a36a2SIngo Molnar	 * The iretq could re-enable interrupts:
653905a36a2SIngo Molnar	 */
654905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
655905a36a2SIngo Molnar
656905a36a2SIngo Molnar/*
657905a36a2SIngo Molnar * At this label, code paths which return to kernel and to user,
658905a36a2SIngo Molnar * which come from interrupts/exception and from syscalls, merge.
659905a36a2SIngo Molnar */
660905a36a2SIngo Molnarrestore_c_regs_and_iret:
661905a36a2SIngo Molnar	RESTORE_C_REGS
662905a36a2SIngo Molnar	REMOVE_PT_GPREGS_FROM_STACK 8
663905a36a2SIngo Molnar	INTERRUPT_RETURN
664905a36a2SIngo Molnar
665905a36a2SIngo MolnarENTRY(native_iret)
666905a36a2SIngo Molnar	/*
667905a36a2SIngo Molnar	 * Are we returning to a stack segment from the LDT?  Note: in
668905a36a2SIngo Molnar	 * 64-bit mode SS:RSP on the exception stack is always valid.
669905a36a2SIngo Molnar	 */
670905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
671905a36a2SIngo Molnar	testb	$4, (SS-RIP)(%rsp)
672905a36a2SIngo Molnar	jnz	native_irq_return_ldt
673905a36a2SIngo Molnar#endif
674905a36a2SIngo Molnar
675905a36a2SIngo Molnar.global native_irq_return_iret
676905a36a2SIngo Molnarnative_irq_return_iret:
677905a36a2SIngo Molnar	/*
678905a36a2SIngo Molnar	 * This may fault.  Non-paranoid faults on return to userspace are
679905a36a2SIngo Molnar	 * handled by fixup_bad_iret.  These include #SS, #GP, and #NP.
680905a36a2SIngo Molnar	 * Double-faults due to espfix64 are handled in do_double_fault.
681905a36a2SIngo Molnar	 * Other faults here are fatal.
682905a36a2SIngo Molnar	 */
683905a36a2SIngo Molnar	iretq
684905a36a2SIngo Molnar
685905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64
686905a36a2SIngo Molnarnative_irq_return_ldt:
687905a36a2SIngo Molnar	pushq	%rax
688905a36a2SIngo Molnar	pushq	%rdi
689905a36a2SIngo Molnar	SWAPGS
690905a36a2SIngo Molnar	movq	PER_CPU_VAR(espfix_waddr), %rdi
691905a36a2SIngo Molnar	movq	%rax, (0*8)(%rdi)		/* RAX */
692905a36a2SIngo Molnar	movq	(2*8)(%rsp), %rax		/* RIP */
693905a36a2SIngo Molnar	movq	%rax, (1*8)(%rdi)
694905a36a2SIngo Molnar	movq	(3*8)(%rsp), %rax		/* CS */
695905a36a2SIngo Molnar	movq	%rax, (2*8)(%rdi)
696905a36a2SIngo Molnar	movq	(4*8)(%rsp), %rax		/* RFLAGS */
697905a36a2SIngo Molnar	movq	%rax, (3*8)(%rdi)
698905a36a2SIngo Molnar	movq	(6*8)(%rsp), %rax		/* SS */
699905a36a2SIngo Molnar	movq	%rax, (5*8)(%rdi)
700905a36a2SIngo Molnar	movq	(5*8)(%rsp), %rax		/* RSP */
701905a36a2SIngo Molnar	movq	%rax, (4*8)(%rdi)
702905a36a2SIngo Molnar	andl	$0xffff0000, %eax
703905a36a2SIngo Molnar	popq	%rdi
704905a36a2SIngo Molnar	orq	PER_CPU_VAR(espfix_stack), %rax
705905a36a2SIngo Molnar	SWAPGS
706905a36a2SIngo Molnar	movq	%rax, %rsp
707905a36a2SIngo Molnar	popq	%rax
708905a36a2SIngo Molnar	jmp	native_irq_return_iret
709905a36a2SIngo Molnar#endif
710905a36a2SIngo Molnar
711905a36a2SIngo Molnar	/* edi: workmask, edx: work */
712905a36a2SIngo Molnarretint_careful:
713905a36a2SIngo Molnar	bt	$TIF_NEED_RESCHED, %edx
714905a36a2SIngo Molnar	jnc	retint_signal
715905a36a2SIngo Molnar	TRACE_IRQS_ON
716905a36a2SIngo Molnar	ENABLE_INTERRUPTS(CLBR_NONE)
717905a36a2SIngo Molnar	pushq	%rdi
718905a36a2SIngo Molnar	SCHEDULE_USER
719905a36a2SIngo Molnar	popq	%rdi
720905a36a2SIngo Molnar	GET_THREAD_INFO(%rcx)
721905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_NONE)
722905a36a2SIngo Molnar	TRACE_IRQS_OFF
723905a36a2SIngo Molnar	jmp	retint_check
724905a36a2SIngo Molnar
725905a36a2SIngo Molnarretint_signal:
726905a36a2SIngo Molnar	testl	$_TIF_DO_NOTIFY_MASK, %edx
727905a36a2SIngo Molnar	jz	retint_swapgs
728905a36a2SIngo Molnar	TRACE_IRQS_ON
729905a36a2SIngo Molnar	ENABLE_INTERRUPTS(CLBR_NONE)
730905a36a2SIngo Molnar	SAVE_EXTRA_REGS
731905a36a2SIngo Molnar	movq	$-1, ORIG_RAX(%rsp)
7324d732138SIngo Molnar	xorl	%esi, %esi			/* oldset */
7334d732138SIngo Molnar	movq	%rsp, %rdi			/* &pt_regs */
734905a36a2SIngo Molnar	call	do_notify_resume
735905a36a2SIngo Molnar	RESTORE_EXTRA_REGS
736905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_NONE)
737905a36a2SIngo Molnar	TRACE_IRQS_OFF
738905a36a2SIngo Molnar	GET_THREAD_INFO(%rcx)
739905a36a2SIngo Molnar	jmp	retint_with_reschedule
740905a36a2SIngo Molnar
741905a36a2SIngo MolnarEND(common_interrupt)
742905a36a2SIngo Molnar
743905a36a2SIngo Molnar/*
744905a36a2SIngo Molnar * APIC interrupts.
745905a36a2SIngo Molnar */
746905a36a2SIngo Molnar.macro apicinterrupt3 num sym do_sym
747905a36a2SIngo MolnarENTRY(\sym)
748905a36a2SIngo Molnar	ASM_CLAC
749905a36a2SIngo Molnar	pushq	$~(\num)
750905a36a2SIngo Molnar.Lcommon_\sym:
751905a36a2SIngo Molnar	interrupt \do_sym
752905a36a2SIngo Molnar	jmp	ret_from_intr
753905a36a2SIngo MolnarEND(\sym)
754905a36a2SIngo Molnar.endm
755905a36a2SIngo Molnar
756905a36a2SIngo Molnar#ifdef CONFIG_TRACING
757905a36a2SIngo Molnar#define trace(sym) trace_##sym
758905a36a2SIngo Molnar#define smp_trace(sym) smp_trace_##sym
759905a36a2SIngo Molnar
760905a36a2SIngo Molnar.macro trace_apicinterrupt num sym
761905a36a2SIngo Molnarapicinterrupt3 \num trace(\sym) smp_trace(\sym)
762905a36a2SIngo Molnar.endm
763905a36a2SIngo Molnar#else
764905a36a2SIngo Molnar.macro trace_apicinterrupt num sym do_sym
765905a36a2SIngo Molnar.endm
766905a36a2SIngo Molnar#endif
767905a36a2SIngo Molnar
768905a36a2SIngo Molnar.macro apicinterrupt num sym do_sym
769905a36a2SIngo Molnarapicinterrupt3 \num \sym \do_sym
770905a36a2SIngo Molnartrace_apicinterrupt \num \sym
771905a36a2SIngo Molnar.endm
772905a36a2SIngo Molnar
773905a36a2SIngo Molnar#ifdef CONFIG_SMP
7744d732138SIngo Molnarapicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR		irq_move_cleanup_interrupt	smp_irq_move_cleanup_interrupt
7754d732138SIngo Molnarapicinterrupt3 REBOOT_VECTOR			reboot_interrupt		smp_reboot_interrupt
776905a36a2SIngo Molnar#endif
777905a36a2SIngo Molnar
778905a36a2SIngo Molnar#ifdef CONFIG_X86_UV
7794d732138SIngo Molnarapicinterrupt3 UV_BAU_MESSAGE			uv_bau_message_intr1		uv_bau_message_interrupt
780905a36a2SIngo Molnar#endif
7814d732138SIngo Molnar
7824d732138SIngo Molnarapicinterrupt LOCAL_TIMER_VECTOR		apic_timer_interrupt		smp_apic_timer_interrupt
7834d732138SIngo Molnarapicinterrupt X86_PLATFORM_IPI_VECTOR		x86_platform_ipi		smp_x86_platform_ipi
784905a36a2SIngo Molnar
785905a36a2SIngo Molnar#ifdef CONFIG_HAVE_KVM
7864d732138SIngo Molnarapicinterrupt3 POSTED_INTR_VECTOR		kvm_posted_intr_ipi		smp_kvm_posted_intr_ipi
7874d732138SIngo Molnarapicinterrupt3 POSTED_INTR_WAKEUP_VECTOR	kvm_posted_intr_wakeup_ipi	smp_kvm_posted_intr_wakeup_ipi
788905a36a2SIngo Molnar#endif
789905a36a2SIngo Molnar
790905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE_THRESHOLD
7914d732138SIngo Molnarapicinterrupt THRESHOLD_APIC_VECTOR		threshold_interrupt		smp_threshold_interrupt
792905a36a2SIngo Molnar#endif
793905a36a2SIngo Molnar
7949dda1658SIngo Molnar#ifdef CONFIG_X86_MCE_AMD
7954d732138SIngo Molnarapicinterrupt DEFERRED_ERROR_VECTOR		deferred_error_interrupt	smp_deferred_error_interrupt
7969dda1658SIngo Molnar#endif
7979dda1658SIngo Molnar
798905a36a2SIngo Molnar#ifdef CONFIG_X86_THERMAL_VECTOR
7994d732138SIngo Molnarapicinterrupt THERMAL_APIC_VECTOR		thermal_interrupt		smp_thermal_interrupt
800905a36a2SIngo Molnar#endif
801905a36a2SIngo Molnar
802905a36a2SIngo Molnar#ifdef CONFIG_SMP
8034d732138SIngo Molnarapicinterrupt CALL_FUNCTION_SINGLE_VECTOR	call_function_single_interrupt	smp_call_function_single_interrupt
8044d732138SIngo Molnarapicinterrupt CALL_FUNCTION_VECTOR		call_function_interrupt		smp_call_function_interrupt
8054d732138SIngo Molnarapicinterrupt RESCHEDULE_VECTOR			reschedule_interrupt		smp_reschedule_interrupt
806905a36a2SIngo Molnar#endif
807905a36a2SIngo Molnar
8084d732138SIngo Molnarapicinterrupt ERROR_APIC_VECTOR			error_interrupt			smp_error_interrupt
8094d732138SIngo Molnarapicinterrupt SPURIOUS_APIC_VECTOR		spurious_interrupt		smp_spurious_interrupt
810905a36a2SIngo Molnar
811905a36a2SIngo Molnar#ifdef CONFIG_IRQ_WORK
8124d732138SIngo Molnarapicinterrupt IRQ_WORK_VECTOR			irq_work_interrupt		smp_irq_work_interrupt
813905a36a2SIngo Molnar#endif
814905a36a2SIngo Molnar
815905a36a2SIngo Molnar/*
816905a36a2SIngo Molnar * Exception entry points.
817905a36a2SIngo Molnar */
818905a36a2SIngo Molnar#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
819905a36a2SIngo Molnar
820905a36a2SIngo Molnar.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
821905a36a2SIngo MolnarENTRY(\sym)
822905a36a2SIngo Molnar	/* Sanity check */
823905a36a2SIngo Molnar	.if \shift_ist != -1 && \paranoid == 0
824905a36a2SIngo Molnar	.error "using shift_ist requires paranoid=1"
825905a36a2SIngo Molnar	.endif
826905a36a2SIngo Molnar
827905a36a2SIngo Molnar	ASM_CLAC
828905a36a2SIngo Molnar	PARAVIRT_ADJUST_EXCEPTION_FRAME
829905a36a2SIngo Molnar
830905a36a2SIngo Molnar	.ifeq \has_error_code
831905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
832905a36a2SIngo Molnar	.endif
833905a36a2SIngo Molnar
834905a36a2SIngo Molnar	ALLOC_PT_GPREGS_ON_STACK
835905a36a2SIngo Molnar
836905a36a2SIngo Molnar	.if \paranoid
837905a36a2SIngo Molnar	.if \paranoid == 1
8384d732138SIngo Molnar	testb	$3, CS(%rsp)			/* If coming from userspace, switch stacks */
8394d732138SIngo Molnar	jnz	1f
840905a36a2SIngo Molnar	.endif
841905a36a2SIngo Molnar	call	paranoid_entry
842905a36a2SIngo Molnar	.else
843905a36a2SIngo Molnar	call	error_entry
844905a36a2SIngo Molnar	.endif
845905a36a2SIngo Molnar	/* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
846905a36a2SIngo Molnar
847905a36a2SIngo Molnar	.if \paranoid
848905a36a2SIngo Molnar	.if \shift_ist != -1
849905a36a2SIngo Molnar	TRACE_IRQS_OFF_DEBUG			/* reload IDT in case of recursion */
850905a36a2SIngo Molnar	.else
851905a36a2SIngo Molnar	TRACE_IRQS_OFF
852905a36a2SIngo Molnar	.endif
853905a36a2SIngo Molnar	.endif
854905a36a2SIngo Molnar
855905a36a2SIngo Molnar	movq	%rsp, %rdi			/* pt_regs pointer */
856905a36a2SIngo Molnar
857905a36a2SIngo Molnar	.if \has_error_code
858905a36a2SIngo Molnar	movq	ORIG_RAX(%rsp), %rsi		/* get error code */
859905a36a2SIngo Molnar	movq	$-1, ORIG_RAX(%rsp)		/* no syscall to restart */
860905a36a2SIngo Molnar	.else
861905a36a2SIngo Molnar	xorl	%esi, %esi			/* no error code */
862905a36a2SIngo Molnar	.endif
863905a36a2SIngo Molnar
864905a36a2SIngo Molnar	.if \shift_ist != -1
865905a36a2SIngo Molnar	subq	$EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
866905a36a2SIngo Molnar	.endif
867905a36a2SIngo Molnar
868905a36a2SIngo Molnar	call	\do_sym
869905a36a2SIngo Molnar
870905a36a2SIngo Molnar	.if \shift_ist != -1
871905a36a2SIngo Molnar	addq	$EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
872905a36a2SIngo Molnar	.endif
873905a36a2SIngo Molnar
874905a36a2SIngo Molnar	/* these procedures expect "no swapgs" flag in ebx */
875905a36a2SIngo Molnar	.if \paranoid
876905a36a2SIngo Molnar	jmp	paranoid_exit
877905a36a2SIngo Molnar	.else
878905a36a2SIngo Molnar	jmp	error_exit
879905a36a2SIngo Molnar	.endif
880905a36a2SIngo Molnar
881905a36a2SIngo Molnar	.if \paranoid == 1
882905a36a2SIngo Molnar	/*
883905a36a2SIngo Molnar	 * Paranoid entry from userspace.  Switch stacks and treat it
884905a36a2SIngo Molnar	 * as a normal entry.  This means that paranoid handlers
885905a36a2SIngo Molnar	 * run in real process context if user_mode(regs).
886905a36a2SIngo Molnar	 */
887905a36a2SIngo Molnar1:
888905a36a2SIngo Molnar	call	error_entry
889905a36a2SIngo Molnar
890905a36a2SIngo Molnar
891905a36a2SIngo Molnar	movq	%rsp, %rdi			/* pt_regs pointer */
892905a36a2SIngo Molnar	call	sync_regs
893905a36a2SIngo Molnar	movq	%rax, %rsp			/* switch stack */
894905a36a2SIngo Molnar
895905a36a2SIngo Molnar	movq	%rsp, %rdi			/* pt_regs pointer */
896905a36a2SIngo Molnar
897905a36a2SIngo Molnar	.if \has_error_code
898905a36a2SIngo Molnar	movq	ORIG_RAX(%rsp), %rsi		/* get error code */
899905a36a2SIngo Molnar	movq	$-1, ORIG_RAX(%rsp)		/* no syscall to restart */
900905a36a2SIngo Molnar	.else
901905a36a2SIngo Molnar	xorl	%esi, %esi			/* no error code */
902905a36a2SIngo Molnar	.endif
903905a36a2SIngo Molnar
904905a36a2SIngo Molnar	call	\do_sym
905905a36a2SIngo Molnar
906905a36a2SIngo Molnar	jmp	error_exit			/* %ebx: no swapgs flag */
907905a36a2SIngo Molnar	.endif
908905a36a2SIngo MolnarEND(\sym)
909905a36a2SIngo Molnar.endm
910905a36a2SIngo Molnar
911905a36a2SIngo Molnar#ifdef CONFIG_TRACING
912905a36a2SIngo Molnar.macro trace_idtentry sym do_sym has_error_code:req
913905a36a2SIngo Molnaridtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
914905a36a2SIngo Molnaridtentry \sym \do_sym has_error_code=\has_error_code
915905a36a2SIngo Molnar.endm
916905a36a2SIngo Molnar#else
917905a36a2SIngo Molnar.macro trace_idtentry sym do_sym has_error_code:req
918905a36a2SIngo Molnaridtentry \sym \do_sym has_error_code=\has_error_code
919905a36a2SIngo Molnar.endm
920905a36a2SIngo Molnar#endif
921905a36a2SIngo Molnar
922905a36a2SIngo Molnaridtentry divide_error			do_divide_error			has_error_code=0
923905a36a2SIngo Molnaridtentry overflow			do_overflow			has_error_code=0
924905a36a2SIngo Molnaridtentry bounds				do_bounds			has_error_code=0
925905a36a2SIngo Molnaridtentry invalid_op			do_invalid_op			has_error_code=0
926905a36a2SIngo Molnaridtentry device_not_available		do_device_not_available		has_error_code=0
927905a36a2SIngo Molnaridtentry double_fault			do_double_fault			has_error_code=1 paranoid=2
928905a36a2SIngo Molnaridtentry coprocessor_segment_overrun	do_coprocessor_segment_overrun	has_error_code=0
929905a36a2SIngo Molnaridtentry invalid_TSS			do_invalid_TSS			has_error_code=1
930905a36a2SIngo Molnaridtentry segment_not_present		do_segment_not_present		has_error_code=1
931905a36a2SIngo Molnaridtentry spurious_interrupt_bug		do_spurious_interrupt_bug	has_error_code=0
932905a36a2SIngo Molnaridtentry coprocessor_error		do_coprocessor_error		has_error_code=0
933905a36a2SIngo Molnaridtentry alignment_check		do_alignment_check		has_error_code=1
934905a36a2SIngo Molnaridtentry simd_coprocessor_error		do_simd_coprocessor_error	has_error_code=0
935905a36a2SIngo Molnar
936905a36a2SIngo Molnar
9374d732138SIngo Molnar	/*
9384d732138SIngo Molnar	 * Reload gs selector with exception handling
9394d732138SIngo Molnar	 * edi:  new selector
9404d732138SIngo Molnar	 */
941905a36a2SIngo MolnarENTRY(native_load_gs_index)
942905a36a2SIngo Molnar	pushfq
943905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
944905a36a2SIngo Molnar	SWAPGS
945905a36a2SIngo Molnargs_change:
946905a36a2SIngo Molnar	movl	%edi, %gs
947905a36a2SIngo Molnar2:	mfence					/* workaround */
948905a36a2SIngo Molnar	SWAPGS
949905a36a2SIngo Molnar	popfq
950905a36a2SIngo Molnar	ret
951905a36a2SIngo MolnarEND(native_load_gs_index)
952905a36a2SIngo Molnar
953905a36a2SIngo Molnar	_ASM_EXTABLE(gs_change, bad_gs)
954905a36a2SIngo Molnar	.section .fixup, "ax"
955905a36a2SIngo Molnar	/* running with kernelgs */
956905a36a2SIngo Molnarbad_gs:
957905a36a2SIngo Molnar	SWAPGS					/* switch back to user gs */
958905a36a2SIngo Molnar	xorl	%eax, %eax
959905a36a2SIngo Molnar	movl	%eax, %gs
960905a36a2SIngo Molnar	jmp	2b
961905a36a2SIngo Molnar	.previous
962905a36a2SIngo Molnar
963905a36a2SIngo Molnar/* Call softirq on interrupt stack. Interrupts are off. */
964905a36a2SIngo MolnarENTRY(do_softirq_own_stack)
965905a36a2SIngo Molnar	pushq	%rbp
966905a36a2SIngo Molnar	mov	%rsp, %rbp
967905a36a2SIngo Molnar	incl	PER_CPU_VAR(irq_count)
968905a36a2SIngo Molnar	cmove	PER_CPU_VAR(irq_stack_ptr), %rsp
9694d732138SIngo Molnar	push	%rbp				/* frame pointer backlink */
970905a36a2SIngo Molnar	call	__do_softirq
971905a36a2SIngo Molnar	leaveq
972905a36a2SIngo Molnar	decl	PER_CPU_VAR(irq_count)
973905a36a2SIngo Molnar	ret
974905a36a2SIngo MolnarEND(do_softirq_own_stack)
975905a36a2SIngo Molnar
976905a36a2SIngo Molnar#ifdef CONFIG_XEN
977905a36a2SIngo Molnaridtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
978905a36a2SIngo Molnar
979905a36a2SIngo Molnar/*
980905a36a2SIngo Molnar * A note on the "critical region" in our callback handler.
981905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring
982905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled
983905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before
984905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still
985905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack.
986905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd
987905a36a2SIngo Molnar * like to avoid the possibility.
988905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an
989905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current
990905a36a2SIngo Molnar * activation and restart the handler using the previous one.
991905a36a2SIngo Molnar */
9924d732138SIngo MolnarENTRY(xen_do_hypervisor_callback)		/* do_hypervisor_callback(struct *pt_regs) */
9934d732138SIngo Molnar
994905a36a2SIngo Molnar/*
995905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
996905a36a2SIngo Molnar * see the correct pointer to the pt_regs
997905a36a2SIngo Molnar */
9984d732138SIngo Molnar	movq	%rdi, %rsp			/* we don't return, adjust the stack frame */
999905a36a2SIngo Molnar11:	incl	PER_CPU_VAR(irq_count)
1000905a36a2SIngo Molnar	movq	%rsp, %rbp
1001905a36a2SIngo Molnar	cmovzq	PER_CPU_VAR(irq_stack_ptr), %rsp
10024d732138SIngo Molnar	pushq	%rbp				/* frame pointer backlink */
1003905a36a2SIngo Molnar	call	xen_evtchn_do_upcall
1004905a36a2SIngo Molnar	popq	%rsp
1005905a36a2SIngo Molnar	decl	PER_CPU_VAR(irq_count)
1006905a36a2SIngo Molnar#ifndef CONFIG_PREEMPT
1007905a36a2SIngo Molnar	call	xen_maybe_preempt_hcall
1008905a36a2SIngo Molnar#endif
1009905a36a2SIngo Molnar	jmp	error_exit
1010905a36a2SIngo MolnarEND(xen_do_hypervisor_callback)
1011905a36a2SIngo Molnar
1012905a36a2SIngo Molnar/*
1013905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes.
1014905a36a2SIngo Molnar * We get here for two reasons:
1015905a36a2SIngo Molnar *  1. Fault while reloading DS, ES, FS or GS
1016905a36a2SIngo Molnar *  2. Fault while executing IRET
1017905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment
1018905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others.
1019905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the
1020905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall
1021905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1022905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register
1023905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1.
1024905a36a2SIngo Molnar */
1025905a36a2SIngo MolnarENTRY(xen_failsafe_callback)
1026905a36a2SIngo Molnar	movl	%ds, %ecx
1027905a36a2SIngo Molnar	cmpw	%cx, 0x10(%rsp)
1028905a36a2SIngo Molnar	jne	1f
1029905a36a2SIngo Molnar	movl	%es, %ecx
1030905a36a2SIngo Molnar	cmpw	%cx, 0x18(%rsp)
1031905a36a2SIngo Molnar	jne	1f
1032905a36a2SIngo Molnar	movl	%fs, %ecx
1033905a36a2SIngo Molnar	cmpw	%cx, 0x20(%rsp)
1034905a36a2SIngo Molnar	jne	1f
1035905a36a2SIngo Molnar	movl	%gs, %ecx
1036905a36a2SIngo Molnar	cmpw	%cx, 0x28(%rsp)
1037905a36a2SIngo Molnar	jne	1f
1038905a36a2SIngo Molnar	/* All segments match their saved values => Category 2 (Bad IRET). */
1039905a36a2SIngo Molnar	movq	(%rsp), %rcx
1040905a36a2SIngo Molnar	movq	8(%rsp), %r11
1041905a36a2SIngo Molnar	addq	$0x30, %rsp
1042905a36a2SIngo Molnar	pushq	$0				/* RIP */
1043905a36a2SIngo Molnar	pushq	%r11
1044905a36a2SIngo Molnar	pushq	%rcx
1045905a36a2SIngo Molnar	jmp	general_protection
1046905a36a2SIngo Molnar1:	/* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1047905a36a2SIngo Molnar	movq	(%rsp), %rcx
1048905a36a2SIngo Molnar	movq	8(%rsp), %r11
1049905a36a2SIngo Molnar	addq	$0x30, %rsp
1050905a36a2SIngo Molnar	pushq	$-1 /* orig_ax = -1 => not a system call */
1051905a36a2SIngo Molnar	ALLOC_PT_GPREGS_ON_STACK
1052905a36a2SIngo Molnar	SAVE_C_REGS
1053905a36a2SIngo Molnar	SAVE_EXTRA_REGS
1054905a36a2SIngo Molnar	jmp	error_exit
1055905a36a2SIngo MolnarEND(xen_failsafe_callback)
1056905a36a2SIngo Molnar
1057905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1058905a36a2SIngo Molnar	xen_hvm_callback_vector xen_evtchn_do_upcall
1059905a36a2SIngo Molnar
1060905a36a2SIngo Molnar#endif /* CONFIG_XEN */
1061905a36a2SIngo Molnar
1062905a36a2SIngo Molnar#if IS_ENABLED(CONFIG_HYPERV)
1063905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1064905a36a2SIngo Molnar	hyperv_callback_vector hyperv_vector_handler
1065905a36a2SIngo Molnar#endif /* CONFIG_HYPERV */
1066905a36a2SIngo Molnar
1067905a36a2SIngo Molnaridtentry debug			do_debug		has_error_code=0	paranoid=1 shift_ist=DEBUG_STACK
1068905a36a2SIngo Molnaridtentry int3			do_int3			has_error_code=0	paranoid=1 shift_ist=DEBUG_STACK
1069905a36a2SIngo Molnaridtentry stack_segment		do_stack_segment	has_error_code=1
10704d732138SIngo Molnar
1071905a36a2SIngo Molnar#ifdef CONFIG_XEN
1072905a36a2SIngo Molnaridtentry xen_debug		do_debug		has_error_code=0
1073905a36a2SIngo Molnaridtentry xen_int3		do_int3			has_error_code=0
1074905a36a2SIngo Molnaridtentry xen_stack_segment	do_stack_segment	has_error_code=1
1075905a36a2SIngo Molnar#endif
10764d732138SIngo Molnar
1077905a36a2SIngo Molnaridtentry general_protection	do_general_protection	has_error_code=1
1078905a36a2SIngo Molnartrace_idtentry page_fault	do_page_fault		has_error_code=1
10794d732138SIngo Molnar
1080905a36a2SIngo Molnar#ifdef CONFIG_KVM_GUEST
1081905a36a2SIngo Molnaridtentry async_page_fault	do_async_page_fault	has_error_code=1
1082905a36a2SIngo Molnar#endif
10834d732138SIngo Molnar
1084905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE
1085905a36a2SIngo Molnaridtentry machine_check					has_error_code=0	paranoid=1 do_sym=*machine_check_vector(%rip)
1086905a36a2SIngo Molnar#endif
1087905a36a2SIngo Molnar
1088905a36a2SIngo Molnar/*
1089905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed.
1090905a36a2SIngo Molnar * Use slow, but surefire "are we in kernel?" check.
1091905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1092905a36a2SIngo Molnar */
1093905a36a2SIngo MolnarENTRY(paranoid_entry)
1094905a36a2SIngo Molnar	cld
1095905a36a2SIngo Molnar	SAVE_C_REGS 8
1096905a36a2SIngo Molnar	SAVE_EXTRA_REGS 8
1097905a36a2SIngo Molnar	movl	$1, %ebx
1098905a36a2SIngo Molnar	movl	$MSR_GS_BASE, %ecx
1099905a36a2SIngo Molnar	rdmsr
1100905a36a2SIngo Molnar	testl	%edx, %edx
1101905a36a2SIngo Molnar	js	1f				/* negative -> in kernel */
1102905a36a2SIngo Molnar	SWAPGS
1103905a36a2SIngo Molnar	xorl	%ebx, %ebx
1104905a36a2SIngo Molnar1:	ret
1105905a36a2SIngo MolnarEND(paranoid_entry)
1106905a36a2SIngo Molnar
1107905a36a2SIngo Molnar/*
1108905a36a2SIngo Molnar * "Paranoid" exit path from exception stack.  This is invoked
1109905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came
1110905a36a2SIngo Molnar * from kernel space.
1111905a36a2SIngo Molnar *
1112905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early
1113905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would
1114905a36a2SIngo Molnar * be complicated.  Fortunately, we there's no good reason
1115905a36a2SIngo Molnar * to try to handle preemption here.
11164d732138SIngo Molnar *
11174d732138SIngo Molnar * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
1118905a36a2SIngo Molnar */
1119905a36a2SIngo MolnarENTRY(paranoid_exit)
1120905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_NONE)
1121905a36a2SIngo Molnar	TRACE_IRQS_OFF_DEBUG
1122905a36a2SIngo Molnar	testl	%ebx, %ebx			/* swapgs needed? */
1123905a36a2SIngo Molnar	jnz	paranoid_exit_no_swapgs
1124905a36a2SIngo Molnar	TRACE_IRQS_IRETQ
1125905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
1126905a36a2SIngo Molnar	jmp	paranoid_exit_restore
1127905a36a2SIngo Molnarparanoid_exit_no_swapgs:
1128905a36a2SIngo Molnar	TRACE_IRQS_IRETQ_DEBUG
1129905a36a2SIngo Molnarparanoid_exit_restore:
1130905a36a2SIngo Molnar	RESTORE_EXTRA_REGS
1131905a36a2SIngo Molnar	RESTORE_C_REGS
1132905a36a2SIngo Molnar	REMOVE_PT_GPREGS_FROM_STACK 8
1133905a36a2SIngo Molnar	INTERRUPT_RETURN
1134905a36a2SIngo MolnarEND(paranoid_exit)
1135905a36a2SIngo Molnar
1136905a36a2SIngo Molnar/*
1137905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed.
1138539f5113SAndy Lutomirski * Return: EBX=0: came from user mode; EBX=1: otherwise
1139905a36a2SIngo Molnar */
1140905a36a2SIngo MolnarENTRY(error_entry)
1141905a36a2SIngo Molnar	cld
1142905a36a2SIngo Molnar	SAVE_C_REGS 8
1143905a36a2SIngo Molnar	SAVE_EXTRA_REGS 8
1144905a36a2SIngo Molnar	xorl	%ebx, %ebx
1145905a36a2SIngo Molnar	testb	$3, CS+8(%rsp)
1146*cb6f64edSAndy Lutomirski	jz	.Lerror_kernelspace
1147539f5113SAndy Lutomirski
1148*cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_swapgs:
1149*cb6f64edSAndy Lutomirski	/*
1150*cb6f64edSAndy Lutomirski	 * We entered from user mode or we're pretending to have entered
1151*cb6f64edSAndy Lutomirski	 * from user mode due to an IRET fault.
1152*cb6f64edSAndy Lutomirski	 */
1153905a36a2SIngo Molnar	SWAPGS
1154539f5113SAndy Lutomirski
1155*cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_after_swapgs:
1156*cb6f64edSAndy Lutomirski.Lerror_entry_done:
1157905a36a2SIngo Molnar	TRACE_IRQS_OFF
1158905a36a2SIngo Molnar	ret
1159905a36a2SIngo Molnar
1160905a36a2SIngo Molnar	/*
1161905a36a2SIngo Molnar	 * There are two places in the kernel that can potentially fault with
1162905a36a2SIngo Molnar	 * usergs. Handle them here.  B stepping K8s sometimes report a
1163905a36a2SIngo Molnar	 * truncated RIP for IRET exceptions returning to compat mode. Check
1164905a36a2SIngo Molnar	 * for these here too.
1165905a36a2SIngo Molnar	 */
1166*cb6f64edSAndy Lutomirski.Lerror_kernelspace:
1167905a36a2SIngo Molnar	incl	%ebx
1168905a36a2SIngo Molnar	leaq	native_irq_return_iret(%rip), %rcx
1169905a36a2SIngo Molnar	cmpq	%rcx, RIP+8(%rsp)
1170*cb6f64edSAndy Lutomirski	je	.Lerror_bad_iret
1171905a36a2SIngo Molnar	movl	%ecx, %eax			/* zero extend */
1172905a36a2SIngo Molnar	cmpq	%rax, RIP+8(%rsp)
1173*cb6f64edSAndy Lutomirski	je	.Lbstep_iret
1174905a36a2SIngo Molnar	cmpq	$gs_change, RIP+8(%rsp)
1175*cb6f64edSAndy Lutomirski	jne	.Lerror_entry_done
1176539f5113SAndy Lutomirski
1177539f5113SAndy Lutomirski	/*
1178539f5113SAndy Lutomirski	 * hack: gs_change can fail with user gsbase.  If this happens, fix up
1179539f5113SAndy Lutomirski	 * gsbase and proceed.  We'll fix up the exception and land in
1180539f5113SAndy Lutomirski	 * gs_change's error handler with kernel gsbase.
1181539f5113SAndy Lutomirski	 */
1182*cb6f64edSAndy Lutomirski	jmp	.Lerror_entry_from_usermode_swapgs
1183905a36a2SIngo Molnar
1184*cb6f64edSAndy Lutomirski.Lbstep_iret:
1185905a36a2SIngo Molnar	/* Fix truncated RIP */
1186905a36a2SIngo Molnar	movq	%rcx, RIP+8(%rsp)
1187905a36a2SIngo Molnar	/* fall through */
1188905a36a2SIngo Molnar
1189*cb6f64edSAndy Lutomirski.Lerror_bad_iret:
1190539f5113SAndy Lutomirski	/*
1191539f5113SAndy Lutomirski	 * We came from an IRET to user mode, so we have user gsbase.
1192539f5113SAndy Lutomirski	 * Switch to kernel gsbase:
1193539f5113SAndy Lutomirski	 */
1194905a36a2SIngo Molnar	SWAPGS
1195539f5113SAndy Lutomirski
1196539f5113SAndy Lutomirski	/*
1197539f5113SAndy Lutomirski	 * Pretend that the exception came from user mode: set up pt_regs
1198539f5113SAndy Lutomirski	 * as if we faulted immediately after IRET and clear EBX so that
1199539f5113SAndy Lutomirski	 * error_exit knows that we will be returning to user mode.
1200539f5113SAndy Lutomirski	 */
1201905a36a2SIngo Molnar	mov	%rsp, %rdi
1202905a36a2SIngo Molnar	call	fixup_bad_iret
1203905a36a2SIngo Molnar	mov	%rax, %rsp
1204539f5113SAndy Lutomirski	decl	%ebx
1205*cb6f64edSAndy Lutomirski	jmp	.Lerror_entry_from_usermode_after_swapgs
1206905a36a2SIngo MolnarEND(error_entry)
1207905a36a2SIngo Molnar
1208905a36a2SIngo Molnar
1209539f5113SAndy Lutomirski/*
1210539f5113SAndy Lutomirski * On entry, EBS is a "return to kernel mode" flag:
1211539f5113SAndy Lutomirski *   1: already in kernel mode, don't need SWAPGS
1212539f5113SAndy Lutomirski *   0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
1213539f5113SAndy Lutomirski */
1214905a36a2SIngo MolnarENTRY(error_exit)
1215905a36a2SIngo Molnar	movl	%ebx, %eax
1216905a36a2SIngo Molnar	RESTORE_EXTRA_REGS
1217905a36a2SIngo Molnar	DISABLE_INTERRUPTS(CLBR_NONE)
1218905a36a2SIngo Molnar	TRACE_IRQS_OFF
1219905a36a2SIngo Molnar	testl	%eax, %eax
1220905a36a2SIngo Molnar	jnz	retint_kernel
1221905a36a2SIngo Molnar	jmp	retint_user
1222905a36a2SIngo MolnarEND(error_exit)
1223905a36a2SIngo Molnar
1224905a36a2SIngo Molnar/* Runs on exception stack */
1225905a36a2SIngo MolnarENTRY(nmi)
1226905a36a2SIngo Molnar	PARAVIRT_ADJUST_EXCEPTION_FRAME
1227905a36a2SIngo Molnar	/*
1228905a36a2SIngo Molnar	 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1229905a36a2SIngo Molnar	 * the iretq it performs will take us out of NMI context.
1230905a36a2SIngo Molnar	 * This means that we can have nested NMIs where the next
1231905a36a2SIngo Molnar	 * NMI is using the top of the stack of the previous NMI. We
1232905a36a2SIngo Molnar	 * can't let it execute because the nested NMI will corrupt the
1233905a36a2SIngo Molnar	 * stack of the previous NMI. NMI handlers are not re-entrant
1234905a36a2SIngo Molnar	 * anyway.
1235905a36a2SIngo Molnar	 *
1236905a36a2SIngo Molnar	 * To handle this case we do the following:
1237905a36a2SIngo Molnar	 *  Check the a special location on the stack that contains
1238905a36a2SIngo Molnar	 *  a variable that is set when NMIs are executing.
1239905a36a2SIngo Molnar	 *  The interrupted task's stack is also checked to see if it
1240905a36a2SIngo Molnar	 *  is an NMI stack.
1241905a36a2SIngo Molnar	 *  If the variable is not set and the stack is not the NMI
1242905a36a2SIngo Molnar	 *  stack then:
1243905a36a2SIngo Molnar	 *    o Set the special variable on the stack
1244905a36a2SIngo Molnar	 *    o Copy the interrupt frame into a "saved" location on the stack
1245905a36a2SIngo Molnar	 *    o Copy the interrupt frame into a "copy" location on the stack
1246905a36a2SIngo Molnar	 *    o Continue processing the NMI
1247905a36a2SIngo Molnar	 *  If the variable is set or the previous stack is the NMI stack:
1248905a36a2SIngo Molnar	 *    o Modify the "copy" location to jump to the repeate_nmi
1249905a36a2SIngo Molnar	 *    o return back to the first NMI
1250905a36a2SIngo Molnar	 *
1251905a36a2SIngo Molnar	 * Now on exit of the first NMI, we first clear the stack variable
1252905a36a2SIngo Molnar	 * The NMI stack will tell any nested NMIs at that point that it is
1253905a36a2SIngo Molnar	 * nested. Then we pop the stack normally with iret, and if there was
1254905a36a2SIngo Molnar	 * a nested NMI that updated the copy interrupt stack frame, a
1255905a36a2SIngo Molnar	 * jump will be made to the repeat_nmi code that will handle the second
1256905a36a2SIngo Molnar	 * NMI.
1257905a36a2SIngo Molnar	 */
1258905a36a2SIngo Molnar
1259905a36a2SIngo Molnar	/* Use %rdx as our temp variable throughout */
1260905a36a2SIngo Molnar	pushq	%rdx
1261905a36a2SIngo Molnar
1262905a36a2SIngo Molnar	/*
1263905a36a2SIngo Molnar	 * If %cs was not the kernel segment, then the NMI triggered in user
1264905a36a2SIngo Molnar	 * space, which means it is definitely not nested.
1265905a36a2SIngo Molnar	 */
1266905a36a2SIngo Molnar	cmpl	$__KERNEL_CS, 16(%rsp)
1267905a36a2SIngo Molnar	jne	first_nmi
1268905a36a2SIngo Molnar
1269905a36a2SIngo Molnar	/*
1270905a36a2SIngo Molnar	 * Check the special variable on the stack to see if NMIs are
1271905a36a2SIngo Molnar	 * executing.
1272905a36a2SIngo Molnar	 */
1273905a36a2SIngo Molnar	cmpl	$1, -8(%rsp)
1274905a36a2SIngo Molnar	je	nested_nmi
1275905a36a2SIngo Molnar
1276905a36a2SIngo Molnar	/*
1277905a36a2SIngo Molnar	 * Now test if the previous stack was an NMI stack.
1278905a36a2SIngo Molnar	 * We need the double check. We check the NMI stack to satisfy the
1279905a36a2SIngo Molnar	 * race when the first NMI clears the variable before returning.
1280905a36a2SIngo Molnar	 * We check the variable because the first NMI could be in a
1281905a36a2SIngo Molnar	 * breakpoint routine using a breakpoint stack.
1282905a36a2SIngo Molnar	 */
1283905a36a2SIngo Molnar	lea	6*8(%rsp), %rdx
1284905a36a2SIngo Molnar	/* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1285905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1286905a36a2SIngo Molnar	/* If the stack pointer is above the NMI stack, this is a normal NMI */
1287905a36a2SIngo Molnar	ja	first_nmi
12884d732138SIngo Molnar
1289905a36a2SIngo Molnar	subq	$EXCEPTION_STKSZ, %rdx
1290905a36a2SIngo Molnar	cmpq	%rdx, 4*8(%rsp)
1291905a36a2SIngo Molnar	/* If it is below the NMI stack, it is a normal NMI */
1292905a36a2SIngo Molnar	jb	first_nmi
1293905a36a2SIngo Molnar	/* Ah, it is within the NMI stack, treat it as nested */
1294905a36a2SIngo Molnar
1295905a36a2SIngo Molnarnested_nmi:
1296905a36a2SIngo Molnar	/*
1297905a36a2SIngo Molnar	 * Do nothing if we interrupted the fixup in repeat_nmi.
1298905a36a2SIngo Molnar	 * It's about to repeat the NMI handler, so we are fine
1299905a36a2SIngo Molnar	 * with ignoring this one.
1300905a36a2SIngo Molnar	 */
1301905a36a2SIngo Molnar	movq	$repeat_nmi, %rdx
1302905a36a2SIngo Molnar	cmpq	8(%rsp), %rdx
1303905a36a2SIngo Molnar	ja	1f
1304905a36a2SIngo Molnar	movq	$end_repeat_nmi, %rdx
1305905a36a2SIngo Molnar	cmpq	8(%rsp), %rdx
1306905a36a2SIngo Molnar	ja	nested_nmi_out
1307905a36a2SIngo Molnar
1308905a36a2SIngo Molnar1:
1309905a36a2SIngo Molnar	/* Set up the interrupted NMIs stack to jump to repeat_nmi */
1310905a36a2SIngo Molnar	leaq	-1*8(%rsp), %rdx
1311905a36a2SIngo Molnar	movq	%rdx, %rsp
1312905a36a2SIngo Molnar	leaq	-10*8(%rsp), %rdx
1313905a36a2SIngo Molnar	pushq	$__KERNEL_DS
1314905a36a2SIngo Molnar	pushq	%rdx
1315905a36a2SIngo Molnar	pushfq
1316905a36a2SIngo Molnar	pushq	$__KERNEL_CS
1317905a36a2SIngo Molnar	pushq	$repeat_nmi
1318905a36a2SIngo Molnar
1319905a36a2SIngo Molnar	/* Put stack back */
1320905a36a2SIngo Molnar	addq	$(6*8), %rsp
1321905a36a2SIngo Molnar
1322905a36a2SIngo Molnarnested_nmi_out:
1323905a36a2SIngo Molnar	popq	%rdx
1324905a36a2SIngo Molnar
1325905a36a2SIngo Molnar	/* No need to check faults here */
1326905a36a2SIngo Molnar	INTERRUPT_RETURN
1327905a36a2SIngo Molnar
1328905a36a2SIngo Molnarfirst_nmi:
1329905a36a2SIngo Molnar	/*
1330905a36a2SIngo Molnar	 * Because nested NMIs will use the pushed location that we
1331905a36a2SIngo Molnar	 * stored in rdx, we must keep that space available.
1332905a36a2SIngo Molnar	 * Here's what our stack frame will look like:
1333905a36a2SIngo Molnar	 * +-------------------------+
1334905a36a2SIngo Molnar	 * | original SS             |
1335905a36a2SIngo Molnar	 * | original Return RSP     |
1336905a36a2SIngo Molnar	 * | original RFLAGS         |
1337905a36a2SIngo Molnar	 * | original CS             |
1338905a36a2SIngo Molnar	 * | original RIP            |
1339905a36a2SIngo Molnar	 * +-------------------------+
1340905a36a2SIngo Molnar	 * | temp storage for rdx    |
1341905a36a2SIngo Molnar	 * +-------------------------+
1342905a36a2SIngo Molnar	 * | NMI executing variable  |
1343905a36a2SIngo Molnar	 * +-------------------------+
1344905a36a2SIngo Molnar	 * | copied SS               |
1345905a36a2SIngo Molnar	 * | copied Return RSP       |
1346905a36a2SIngo Molnar	 * | copied RFLAGS           |
1347905a36a2SIngo Molnar	 * | copied CS               |
1348905a36a2SIngo Molnar	 * | copied RIP              |
1349905a36a2SIngo Molnar	 * +-------------------------+
1350905a36a2SIngo Molnar	 * | Saved SS                |
1351905a36a2SIngo Molnar	 * | Saved Return RSP        |
1352905a36a2SIngo Molnar	 * | Saved RFLAGS            |
1353905a36a2SIngo Molnar	 * | Saved CS                |
1354905a36a2SIngo Molnar	 * | Saved RIP               |
1355905a36a2SIngo Molnar	 * +-------------------------+
1356905a36a2SIngo Molnar	 * | pt_regs                 |
1357905a36a2SIngo Molnar	 * +-------------------------+
1358905a36a2SIngo Molnar	 *
1359905a36a2SIngo Molnar	 * The saved stack frame is used to fix up the copied stack frame
1360905a36a2SIngo Molnar	 * that a nested NMI may change to make the interrupted NMI iret jump
1361905a36a2SIngo Molnar	 * to the repeat_nmi. The original stack frame and the temp storage
1362905a36a2SIngo Molnar	 * is also used by nested NMIs and can not be trusted on exit.
1363905a36a2SIngo Molnar	 */
1364905a36a2SIngo Molnar	/* Do not pop rdx, nested NMIs will corrupt that part of the stack */
1365905a36a2SIngo Molnar	movq	(%rsp), %rdx
1366905a36a2SIngo Molnar
1367905a36a2SIngo Molnar	/* Set the NMI executing variable on the stack. */
1368905a36a2SIngo Molnar	pushq	$1
1369905a36a2SIngo Molnar
13704d732138SIngo Molnar	/* Leave room for the "copied" frame */
1371905a36a2SIngo Molnar	subq	$(5*8), %rsp
1372905a36a2SIngo Molnar
1373905a36a2SIngo Molnar	/* Copy the stack frame to the Saved frame */
1374905a36a2SIngo Molnar	.rept 5
1375905a36a2SIngo Molnar	pushq	11*8(%rsp)
1376905a36a2SIngo Molnar	.endr
1377905a36a2SIngo Molnar
1378905a36a2SIngo Molnar	/* Everything up to here is safe from nested NMIs */
1379905a36a2SIngo Molnar
1380905a36a2SIngo Molnar	/*
1381905a36a2SIngo Molnar	 * If there was a nested NMI, the first NMI's iret will return
1382905a36a2SIngo Molnar	 * here. But NMIs are still enabled and we can take another
1383905a36a2SIngo Molnar	 * nested NMI. The nested NMI checks the interrupted RIP to see
1384905a36a2SIngo Molnar	 * if it is between repeat_nmi and end_repeat_nmi, and if so
1385905a36a2SIngo Molnar	 * it will just return, as we are about to repeat an NMI anyway.
1386905a36a2SIngo Molnar	 * This makes it safe to copy to the stack frame that a nested
1387905a36a2SIngo Molnar	 * NMI will update.
1388905a36a2SIngo Molnar	 */
1389905a36a2SIngo Molnarrepeat_nmi:
1390905a36a2SIngo Molnar	/*
1391905a36a2SIngo Molnar	 * Update the stack variable to say we are still in NMI (the update
1392905a36a2SIngo Molnar	 * is benign for the non-repeat case, where 1 was pushed just above
1393905a36a2SIngo Molnar	 * to this very stack slot).
1394905a36a2SIngo Molnar	 */
1395905a36a2SIngo Molnar	movq	$1, 10*8(%rsp)
1396905a36a2SIngo Molnar
1397905a36a2SIngo Molnar	/* Make another copy, this one may be modified by nested NMIs */
1398905a36a2SIngo Molnar	addq	$(10*8), %rsp
1399905a36a2SIngo Molnar	.rept 5
1400905a36a2SIngo Molnar	pushq	-6*8(%rsp)
1401905a36a2SIngo Molnar	.endr
1402905a36a2SIngo Molnar	subq	$(5*8), %rsp
1403905a36a2SIngo Molnarend_repeat_nmi:
1404905a36a2SIngo Molnar
1405905a36a2SIngo Molnar	/*
1406905a36a2SIngo Molnar	 * Everything below this point can be preempted by a nested
1407905a36a2SIngo Molnar	 * NMI if the first NMI took an exception and reset our iret stack
1408905a36a2SIngo Molnar	 * so that we repeat another NMI.
1409905a36a2SIngo Molnar	 */
1410905a36a2SIngo Molnar	pushq	$-1				/* ORIG_RAX: no syscall to restart */
1411905a36a2SIngo Molnar	ALLOC_PT_GPREGS_ON_STACK
1412905a36a2SIngo Molnar
1413905a36a2SIngo Molnar	/*
1414905a36a2SIngo Molnar	 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1415905a36a2SIngo Molnar	 * as we should not be calling schedule in NMI context.
1416905a36a2SIngo Molnar	 * Even with normal interrupts enabled. An NMI should not be
1417905a36a2SIngo Molnar	 * setting NEED_RESCHED or anything that normal interrupts and
1418905a36a2SIngo Molnar	 * exceptions might do.
1419905a36a2SIngo Molnar	 */
1420905a36a2SIngo Molnar	call	paranoid_entry
1421905a36a2SIngo Molnar
1422905a36a2SIngo Molnar	/*
1423905a36a2SIngo Molnar	 * Save off the CR2 register. If we take a page fault in the NMI then
1424905a36a2SIngo Molnar	 * it could corrupt the CR2 value. If the NMI preempts a page fault
1425905a36a2SIngo Molnar	 * handler before it was able to read the CR2 register, and then the
1426905a36a2SIngo Molnar	 * NMI itself takes a page fault, the page fault that was preempted
1427905a36a2SIngo Molnar	 * will read the information from the NMI page fault and not the
1428905a36a2SIngo Molnar	 * origin fault. Save it off and restore it if it changes.
1429905a36a2SIngo Molnar	 * Use the r12 callee-saved register.
1430905a36a2SIngo Molnar	 */
1431905a36a2SIngo Molnar	movq	%cr2, %r12
1432905a36a2SIngo Molnar
1433905a36a2SIngo Molnar	/* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1434905a36a2SIngo Molnar	movq	%rsp, %rdi
1435905a36a2SIngo Molnar	movq	$-1, %rsi
1436905a36a2SIngo Molnar	call	do_nmi
1437905a36a2SIngo Molnar
1438905a36a2SIngo Molnar	/* Did the NMI take a page fault? Restore cr2 if it did */
1439905a36a2SIngo Molnar	movq	%cr2, %rcx
1440905a36a2SIngo Molnar	cmpq	%rcx, %r12
1441905a36a2SIngo Molnar	je	1f
1442905a36a2SIngo Molnar	movq	%r12, %cr2
1443905a36a2SIngo Molnar1:
1444905a36a2SIngo Molnar	testl	%ebx, %ebx			/* swapgs needed? */
1445905a36a2SIngo Molnar	jnz	nmi_restore
1446905a36a2SIngo Molnarnmi_swapgs:
1447905a36a2SIngo Molnar	SWAPGS_UNSAFE_STACK
1448905a36a2SIngo Molnarnmi_restore:
1449905a36a2SIngo Molnar	RESTORE_EXTRA_REGS
1450905a36a2SIngo Molnar	RESTORE_C_REGS
1451905a36a2SIngo Molnar	/* Pop the extra iret frame at once */
1452905a36a2SIngo Molnar	REMOVE_PT_GPREGS_FROM_STACK 6*8
1453905a36a2SIngo Molnar
1454905a36a2SIngo Molnar	/* Clear the NMI executing stack variable */
1455905a36a2SIngo Molnar	movq	$0, 5*8(%rsp)
14565ca6f70fSAndy Lutomirski	INTERRUPT_RETURN
1457905a36a2SIngo MolnarEND(nmi)
1458905a36a2SIngo Molnar
1459905a36a2SIngo MolnarENTRY(ignore_sysret)
1460905a36a2SIngo Molnar	mov	$-ENOSYS, %eax
1461905a36a2SIngo Molnar	sysret
1462905a36a2SIngo MolnarEND(ignore_sysret)
1463