blob: 4ca5b782ed7064414146721ee0200e50e51be365 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86_64/entry.S
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
6 * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
Ingo Molnar4d732132015-06-08 20:43:07 +02007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * entry.S contains the system-call and fault low-level handling routines.
9 *
Andy Lutomirski8b4777a2011-06-05 13:50:18 -040010 * Some of this is documented in Documentation/x86/entry_64.txt
11 *
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +010012 * A note on terminology:
Ingo Molnar4d732132015-06-08 20:43:07 +020013 * - iret frame: Architecture defined interrupt frame from SS to RIP
14 * at the top of the kernel process stack.
Andi Kleen2e91a172006-09-26 10:52:29 +020015 *
16 * Some macro usage:
Ingo Molnar4d732132015-06-08 20:43:07 +020017 * - ENTRY/END: Define functions in the symbol table.
18 * - TRACE_IRQ_*: Trace hardirq state for lock debugging.
19 * - idtentry: Define exception entry points.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/linkage.h>
22#include <asm/segment.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/cache.h>
24#include <asm/errno.h>
Ingo Molnard36f9472015-06-03 18:29:26 +020025#include "calling.h"
Sam Ravnborge2d5df92005-09-09 21:28:48 +020026#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/msr.h>
28#include <asm/unistd.h>
29#include <asm/thread_info.h>
30#include <asm/hw_irq.h>
Jeremy Fitzhardinge0341c142009-02-13 11:14:01 -080031#include <asm/page_types.h>
Ingo Molnar2601e642006-07-03 00:24:45 -070032#include <asm/irqflags.h>
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010033#include <asm/paravirt.h>
Tejun Heo9939dda2009-01-13 20:41:35 +090034#include <asm/percpu.h>
H. Peter Anvind7abc0f2012-04-20 12:19:50 -070035#include <asm/asm.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010036#include <asm/context_tracking.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070037#include <asm/smap.h>
H. Peter Anvin3891a042014-04-29 16:46:09 -070038#include <asm/pgtable_types.h>
Eric Parisd7e75282012-01-03 14:23:06 -050039#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Roland McGrath86a1c342008-06-23 15:37:04 -070041/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
42#include <linux/elf-em.h>
Ingo Molnar4d732132015-06-08 20:43:07 +020043#define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
44#define __AUDIT_ARCH_64BIT 0x80000000
45#define __AUDIT_ARCH_LE 0x40000000
Roland McGrath86a1c342008-06-23 15:37:04 -070046
Ingo Molnar4d732132015-06-08 20:43:07 +020047.code64
48.section .entry.text, "ax"
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020049
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010050#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040051ENTRY(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010052 swapgs
53 sysretq
Cyrill Gorcunovb3baaa12009-02-23 22:57:00 +030054ENDPROC(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010055#endif /* CONFIG_PARAVIRT */
56
Denys Vlasenkof2db9382015-02-26 14:40:30 -080057.macro TRACE_IRQS_IRETQ
Ingo Molnar2601e642006-07-03 00:24:45 -070058#ifdef CONFIG_TRACE_IRQFLAGS
Ingo Molnar4d732132015-06-08 20:43:07 +020059 bt $9, EFLAGS(%rsp) /* interrupts off? */
60 jnc 1f
Ingo Molnar2601e642006-07-03 00:24:45 -070061 TRACE_IRQS_ON
621:
63#endif
64.endm
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
Steven Rostedt5963e312012-05-30 11:54:53 -040067 * When dynamic function tracer is enabled it will add a breakpoint
68 * to all locations that it is about to modify, sync CPUs, update
69 * all the code, sync CPUs, then remove the breakpoints. In this time
70 * if lockdep is enabled, it might jump back into the debug handler
71 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
72 *
73 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
74 * make sure the stack pointer does not get reset back to the top
75 * of the debug stack, and instead just reuses the current stack.
76 */
77#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
78
79.macro TRACE_IRQS_OFF_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +020080 call debug_stack_set_zero
Steven Rostedt5963e312012-05-30 11:54:53 -040081 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +020082 call debug_stack_reset
Steven Rostedt5963e312012-05-30 11:54:53 -040083.endm
84
85.macro TRACE_IRQS_ON_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +020086 call debug_stack_set_zero
Steven Rostedt5963e312012-05-30 11:54:53 -040087 TRACE_IRQS_ON
Ingo Molnar4d732132015-06-08 20:43:07 +020088 call debug_stack_reset
Steven Rostedt5963e312012-05-30 11:54:53 -040089.endm
90
Denys Vlasenkof2db9382015-02-26 14:40:30 -080091.macro TRACE_IRQS_IRETQ_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +020092 bt $9, EFLAGS(%rsp) /* interrupts off? */
93 jnc 1f
Steven Rostedt5963e312012-05-30 11:54:53 -040094 TRACE_IRQS_ON_DEBUG
951:
96.endm
97
98#else
Ingo Molnar4d732132015-06-08 20:43:07 +020099# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
100# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
101# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
Steven Rostedt5963e312012-05-30 11:54:53 -0400102#endif
103
104/*
Ingo Molnar4d732132015-06-08 20:43:07 +0200105 * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
Ingo Molnar4d732132015-06-08 20:43:07 +0200107 * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800108 * then loads new ss, cs, and rip from previously programmed MSRs.
109 * rflags gets masked by a value from another MSR (so CLD and CLAC
110 * are not needed). SYSCALL does not save anything on the stack
111 * and does not change rsp.
112 *
113 * Registers on entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * rax system call number
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800115 * rcx return address
116 * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * rdi arg0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * rsi arg1
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100119 * rdx arg2
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800120 * r10 arg3 (needs to be moved to rcx to conform to C ABI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * r8 arg4
122 * r9 arg5
Ingo Molnar4d732132015-06-08 20:43:07 +0200123 * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100124 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * Only called from user space.
126 *
Ingo Molnar7fcb3bc2015-03-17 14:42:59 +0100127 * When user can change pt_regs->foo always force IRET. That is because
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200128 * it deals with uncanonical addresses better. SYSRET has trouble
129 * with them due to bugs in both AMD and Intel CPUs.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Ingo Molnarb2502b42015-06-08 08:42:03 +0200132ENTRY(entry_SYSCALL_64)
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100133 /*
134 * Interrupts are off on entry.
135 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
136 * it is too small to ever cause noticeable irq latency.
137 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100138 SWAPGS_UNSAFE_STACK
139 /*
140 * A hypervisor implementation might want to use a label
141 * after the swapgs, so that it can do the swapgs
142 * for the guest and jump here on syscall.
143 */
Ingo Molnarb2502b42015-06-08 08:42:03 +0200144GLOBAL(entry_SYSCALL_64_after_swapgs)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100145
Ingo Molnar4d732132015-06-08 20:43:07 +0200146 movq %rsp, PER_CPU_VAR(rsp_scratch)
147 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100148
149 /* Construct struct pt_regs on stack */
Ingo Molnar4d732132015-06-08 20:43:07 +0200150 pushq $__USER_DS /* pt_regs->ss */
151 pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */
Denys Vlasenko33db1fd2015-03-17 14:52:24 +0100152 /*
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100153 * Re-enable interrupts.
154 * We use 'rsp_scratch' as a scratch space, hence irq-off block above
155 * must execute atomically in the face of possible interrupt-driven
156 * task preemption. We must enable interrupts only after we're done
157 * with using rsp_scratch:
Denys Vlasenko33db1fd2015-03-17 14:52:24 +0100158 */
159 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar4d732132015-06-08 20:43:07 +0200160 pushq %r11 /* pt_regs->flags */
161 pushq $__USER_CS /* pt_regs->cs */
162 pushq %rcx /* pt_regs->ip */
163 pushq %rax /* pt_regs->orig_ax */
164 pushq %rdi /* pt_regs->di */
165 pushq %rsi /* pt_regs->si */
166 pushq %rdx /* pt_regs->dx */
167 pushq %rcx /* pt_regs->cx */
168 pushq $-ENOSYS /* pt_regs->ax */
169 pushq %r8 /* pt_regs->r8 */
170 pushq %r9 /* pt_regs->r9 */
171 pushq %r10 /* pt_regs->r10 */
172 pushq %r11 /* pt_regs->r11 */
173 sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100174
Ingo Molnar4d732132015-06-08 20:43:07 +0200175 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
176 jnz tracesys
Ingo Molnarb2502b42015-06-08 08:42:03 +0200177entry_SYSCALL_64_fastpath:
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800178#if __SYSCALL_MASK == ~0
Ingo Molnar4d732132015-06-08 20:43:07 +0200179 cmpq $__NR_syscall_max, %rax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800180#else
Ingo Molnar4d732132015-06-08 20:43:07 +0200181 andl $__SYSCALL_MASK, %eax
182 cmpl $__NR_syscall_max, %eax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800183#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200184 ja 1f /* return -ENOSYS (already in pt_regs->ax) */
185 movq %r10, %rcx
186 call *sys_call_table(, %rax, 8)
187 movq %rax, RAX(%rsp)
Denys Vlasenko146b2b02015-03-25 18:18:13 +01001881:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
Denys Vlasenko146b2b02015-03-25 18:18:13 +0100190 * Syscall return path ending with SYSRET (fast path).
191 * Has incompletely filled pt_regs.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100192 */
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200193 LOCKDEP_SYS_EXIT
Denys Vlasenko4416c5a2015-03-31 19:00:03 +0200194 /*
195 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
196 * it is too small to ever cause noticeable irq latency.
197 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100198 DISABLE_INTERRUPTS(CLBR_NONE)
Andy Lutomirskib3494a42015-03-23 12:32:54 -0700199
200 /*
201 * We must check ti flags with interrupts (or at least preemption)
202 * off because we must *never* return to userspace without
203 * processing exit work that is enqueued if we're preempted here.
204 * In particular, returning to userspace with any of the one-shot
205 * flags (TIF_NOTIFY_RESUME, TIF_USER_RETURN_NOTIFY, etc) set is
206 * very bad.
207 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200208 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
209 jnz int_ret_from_sys_call_irqs_off /* Go to the slow path */
Andy Lutomirskib3494a42015-03-23 12:32:54 -0700210
Denys Vlasenko29722cd2015-03-09 19:39:21 +0100211 RESTORE_C_REGS_EXCEPT_RCX_R11
Ingo Molnar4d732132015-06-08 20:43:07 +0200212 movq RIP(%rsp), %rcx
213 movq EFLAGS(%rsp), %r11
214 movq RSP(%rsp), %rsp
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800215 /*
Ingo Molnar4d732132015-06-08 20:43:07 +0200216 * 64-bit SYSRET restores rip from rcx,
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800217 * rflags from r11 (but RF and VM bits are forced to 0),
218 * cs and ss are loaded from MSRs.
Denys Vlasenko4416c5a2015-03-31 19:00:03 +0200219 * Restoration of rflags re-enables interrupts.
Andy Lutomirski61f01dd2015-04-26 16:47:59 -0700220 *
221 * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss
222 * descriptor is not reinitialized. This means that we should
223 * avoid SYSRET with SS == NULL, which could happen if we schedule,
224 * exit the kernel, and re-enter using an interrupt vector. (All
225 * interrupt entries on x86_64 set SS to NULL.) We prevent that
226 * from happening by reloading SS in __switch_to. (Actually
227 * detecting the failure in 64-bit userspace is tricky but can be
228 * done.)
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800229 */
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400230 USERGS_SYSRET64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700232GLOBAL(int_ret_from_sys_call_irqs_off)
233 TRACE_IRQS_ON
234 ENABLE_INTERRUPTS(CLBR_NONE)
235 jmp int_ret_from_sys_call
236
Ingo Molnar7fcb3bc2015-03-17 14:42:59 +0100237 /* Do syscall entry tracing */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100238tracesys:
Ingo Molnar4d732132015-06-08 20:43:07 +0200239 movq %rsp, %rdi
240 movl $AUDIT_ARCH_X86_64, %esi
241 call syscall_trace_enter_phase1
242 test %rax, %rax
243 jnz tracesys_phase2 /* if needed, run the slow path */
244 RESTORE_C_REGS_EXCEPT_RAX /* else restore clobbered regs */
245 movq ORIG_RAX(%rsp), %rax
246 jmp entry_SYSCALL_64_fastpath /* and return to the fast path */
Andy Lutomirski1dcf74f2014-09-05 15:13:56 -0700247
248tracesys_phase2:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800249 SAVE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200250 movq %rsp, %rdi
251 movl $AUDIT_ARCH_X86_64, %esi
252 movq %rax, %rdx
253 call syscall_trace_enter_phase2
Andy Lutomirski1dcf74f2014-09-05 15:13:56 -0700254
Roland McGrathd4d67152008-07-09 02:38:07 -0700255 /*
Denys Vlasenkoe90e1472015-02-26 14:40:28 -0800256 * Reload registers from stack in case ptrace changed them.
Andy Lutomirski1dcf74f2014-09-05 15:13:56 -0700257 * We don't reload %rax because syscall_trace_entry_phase2() returned
Roland McGrathd4d67152008-07-09 02:38:07 -0700258 * the value it wants us to use in the table lookup.
259 */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800260 RESTORE_C_REGS_EXCEPT_RAX
261 RESTORE_EXTRA_REGS
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800262#if __SYSCALL_MASK == ~0
Ingo Molnar4d732132015-06-08 20:43:07 +0200263 cmpq $__NR_syscall_max, %rax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800264#else
Ingo Molnar4d732132015-06-08 20:43:07 +0200265 andl $__SYSCALL_MASK, %eax
266 cmpl $__NR_syscall_max, %eax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800267#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200268 ja 1f /* return -ENOSYS (already in pt_regs->ax) */
269 movq %r10, %rcx /* fixup for C */
270 call *sys_call_table(, %rax, 8)
271 movq %rax, RAX(%rsp)
Denys Vlasenkoa6de5a22015-03-31 19:00:11 +02002721:
Ingo Molnar7fcb3bc2015-03-17 14:42:59 +0100273 /* Use IRET because user could have changed pt_regs->foo */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100274
275/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * Syscall return path ending with IRET.
Ingo Molnar7fcb3bc2015-03-17 14:42:59 +0100277 * Has correct iret frame.
Jan Beulichbcddc012006-12-07 02:14:02 +0100278 */
Cyrill Gorcunovbc8b2b92009-02-23 22:57:01 +0300279GLOBAL(int_ret_from_sys_call)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800280 SAVE_EXTRA_REGS
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700281 movq %rsp, %rdi
282 call syscall_return_slowpath /* returns with IRQs disabled */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800283 RESTORE_EXTRA_REGS
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700284 TRACE_IRQS_IRETQ /* we're about to change IF */
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200285
286 /*
287 * Try to use SYSRET instead of IRET if we're returning to
288 * a completely clean 64-bit userspace context.
289 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200290 movq RCX(%rsp), %rcx
291 movq RIP(%rsp), %r11
292 cmpq %rcx, %r11 /* RCX == RIP */
293 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200294
295 /*
296 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
297 * in kernel space. This essentially lets the user take over
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200298 * the kernel, since userspace controls RSP.
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200299 *
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200300 * If width of "canonical tail" ever becomes variable, this will need
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200301 * to be updated to remain correct on both old and new CPUs.
302 */
303 .ifne __VIRTUAL_MASK_SHIFT - 47
304 .error "virtual address width changed -- SYSRET checks need update"
305 .endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200306
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200307 /* Change top 16 bits to be the sign-extension of 47th bit */
308 shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
309 sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
Ingo Molnar4d732132015-06-08 20:43:07 +0200310
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200311 /* If this changed %rcx, it was not canonical */
312 cmpq %rcx, %r11
313 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200314
Ingo Molnar4d732132015-06-08 20:43:07 +0200315 cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
316 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200317
Ingo Molnar4d732132015-06-08 20:43:07 +0200318 movq R11(%rsp), %r11
319 cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
320 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200321
322 /*
323 * SYSRET can't restore RF. SYSRET can restore TF, but unlike IRET,
324 * restoring TF results in a trap from userspace immediately after
325 * SYSRET. This would cause an infinite loop whenever #DB happens
326 * with register state that satisfies the opportunistic SYSRET
327 * conditions. For example, single-stepping this user code:
328 *
Ingo Molnar4d732132015-06-08 20:43:07 +0200329 * movq $stuck_here, %rcx
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200330 * pushfq
331 * popq %r11
332 * stuck_here:
333 *
334 * would never get past 'stuck_here'.
335 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200336 testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
337 jnz opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200338
339 /* nothing to check for RSP */
340
Ingo Molnar4d732132015-06-08 20:43:07 +0200341 cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
342 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200343
344 /*
Ingo Molnar4d732132015-06-08 20:43:07 +0200345 * We win! This label is here just for ease of understanding
346 * perf profiles. Nothing jumps here.
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200347 */
348syscall_return_via_sysret:
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200349 /* rcx and r11 are already restored (see code above) */
350 RESTORE_C_REGS_EXCEPT_RCX_R11
Ingo Molnar4d732132015-06-08 20:43:07 +0200351 movq RSP(%rsp), %rsp
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200352 USERGS_SYSRET64
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200353
354opportunistic_sysret_failed:
355 SWAPGS
356 jmp restore_c_regs_and_iret
Ingo Molnarb2502b42015-06-08 08:42:03 +0200357END(entry_SYSCALL_64)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100358
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200359
Al Viro1d4b4b22012-10-22 22:34:11 -0400360 .macro FORK_LIKE func
361ENTRY(stub_\func)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800362 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +0200363 jmp sys_\func
Al Viro1d4b4b22012-10-22 22:34:11 -0400364END(stub_\func)
365 .endm
366
367 FORK_LIKE clone
368 FORK_LIKE fork
369 FORK_LIKE vfork
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371ENTRY(stub_execve)
Denys Vlasenkofc3e9582015-04-04 20:55:19 +0200372 call sys_execve
373return_from_execve:
374 testl %eax, %eax
375 jz 1f
376 /* exec failed, can use fast SYSRET code path in this case */
377 ret
3781:
379 /* must use IRET code path (pt_regs->cs may have changed) */
380 addq $8, %rsp
381 ZERO_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200382 movq %rax, RAX(%rsp)
Denys Vlasenkofc3e9582015-04-04 20:55:19 +0200383 jmp int_ret_from_sys_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200384END(stub_execve)
Denys Vlasenkoa37f34a2015-04-07 22:43:44 +0200385/*
386 * Remaining execve stubs are only 7 bytes long.
387 * ENTRY() often aligns to 16 bytes, which in this case has no benefits.
388 */
389 .align 8
390GLOBAL(stub_execveat)
Denys Vlasenkofc3e9582015-04-04 20:55:19 +0200391 call sys_execveat
392 jmp return_from_execve
David Drysdale27d6ec72014-12-12 16:57:33 -0800393END(stub_execveat)
394
Denys Vlasenkoac7f5df2015-04-21 18:03:13 +0200395#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION)
Denys Vlasenkoa37f34a2015-04-07 22:43:44 +0200396 .align 8
397GLOBAL(stub_x32_execve)
Denys Vlasenkoa37f34a2015-04-07 22:43:44 +0200398GLOBAL(stub32_execve)
Denys Vlasenko0f90fb92015-04-07 22:43:39 +0200399 call compat_sys_execve
400 jmp return_from_execve
Denys Vlasenko0f90fb92015-04-07 22:43:39 +0200401END(stub32_execve)
Denys Vlasenkoac7f5df2015-04-21 18:03:13 +0200402END(stub_x32_execve)
Denys Vlasenkoa37f34a2015-04-07 22:43:44 +0200403 .align 8
Denys Vlasenkoac7f5df2015-04-21 18:03:13 +0200404GLOBAL(stub_x32_execveat)
Denys Vlasenkoa37f34a2015-04-07 22:43:44 +0200405GLOBAL(stub32_execveat)
Denys Vlasenko0f90fb92015-04-07 22:43:39 +0200406 call compat_sys_execveat
407 jmp return_from_execve
Denys Vlasenko0f90fb92015-04-07 22:43:39 +0200408END(stub32_execveat)
Denys Vlasenkoac7f5df2015-04-21 18:03:13 +0200409END(stub_x32_execveat)
Denys Vlasenko0f90fb92015-04-07 22:43:39 +0200410#endif
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*
413 * sigreturn is special because it needs to restore all registers on return.
414 * This cannot be done with SYSRET, so use the IRET return path instead.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100415 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416ENTRY(stub_rt_sigreturn)
Denys Vlasenko31f01192015-04-07 22:43:37 +0200417 /*
418 * SAVE_EXTRA_REGS result is not normally needed:
419 * sigreturn overwrites all pt_regs->GPREGS.
420 * But sigreturn can fail (!), and there is no easy way to detect that.
421 * To make sure RESTORE_EXTRA_REGS doesn't restore garbage on error,
422 * we SAVE_EXTRA_REGS here.
423 */
424 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +0200425 call sys_rt_sigreturn
Denys Vlasenko31f01192015-04-07 22:43:37 +0200426return_from_stub:
427 addq $8, %rsp
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800428 RESTORE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200429 movq %rax, RAX(%rsp)
430 jmp int_ret_from_sys_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200431END(stub_rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800433#ifdef CONFIG_X86_X32_ABI
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800434ENTRY(stub_x32_rt_sigreturn)
Denys Vlasenko31f01192015-04-07 22:43:37 +0200435 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +0200436 call sys32_x32_rt_sigreturn
437 jmp return_from_stub
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800438END(stub_x32_rt_sigreturn)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800439#endif
440
Jan Beulich7effaa82005-09-12 18:49:24 +0200441/*
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800442 * A newly forked process directly context switches into this address.
443 *
444 * rdi: prev task we switched from
445 */
446ENTRY(ret_from_fork)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800447
Ingo Molnar4d732132015-06-08 20:43:07 +0200448 LOCK ; btr $TIF_FORK, TI_flags(%r8)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800449
Ingo Molnar4d732132015-06-08 20:43:07 +0200450 pushq $0x0002
451 popfq /* reset kernel eflags */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800452
Ingo Molnar4d732132015-06-08 20:43:07 +0200453 call schedule_tail /* rdi: 'prev' task parameter */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800454
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800455 RESTORE_EXTRA_REGS
456
Ingo Molnar4d732132015-06-08 20:43:07 +0200457 testb $3, CS(%rsp) /* from kernel_thread? */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800458
Andy Lutomirski1e3fbb82015-02-26 14:40:39 -0800459 /*
460 * By the time we get here, we have no idea whether our pt_regs,
461 * ti flags, and ti status came from the 64-bit SYSCALL fast path,
Ingo Molnar138bd562015-06-05 14:11:49 +0200462 * the slow path, or one of the 32-bit compat paths.
Denys Vlasenko66ad4ef2015-04-07 22:43:42 +0200463 * Use IRET code path to return, since it can safely handle
Andy Lutomirski1e3fbb82015-02-26 14:40:39 -0800464 * all of the above.
465 */
Denys Vlasenko66ad4ef2015-04-07 22:43:42 +0200466 jnz int_ret_from_sys_call
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800467
Ingo Molnar4d732132015-06-08 20:43:07 +0200468 /*
469 * We came from kernel_thread
470 * nb: we depend on RESTORE_EXTRA_REGS above
471 */
472 movq %rbp, %rdi
473 call *%rbx
474 movl $0, RAX(%rsp)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800475 RESTORE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200476 jmp int_ret_from_sys_call
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800477END(ret_from_fork)
478
479/*
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200480 * Build the entry stubs with some assembler magic.
481 * We pack 1 stub into every 8-byte block.
H. Peter Anvin939b7872008-11-11 13:51:52 -0800482 */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200483 .align 8
H. Peter Anvin939b7872008-11-11 13:51:52 -0800484ENTRY(irq_entries_start)
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200485 vector=FIRST_EXTERNAL_VECTOR
486 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
Ingo Molnar4d732132015-06-08 20:43:07 +0200487 pushq $(~vector+0x80) /* Note: always in signed byte range */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200488 vector=vector+1
489 jmp common_interrupt
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200490 .align 8
491 .endr
H. Peter Anvin939b7872008-11-11 13:51:52 -0800492END(irq_entries_start)
493
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100494/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * Interrupt entry/exit.
496 *
497 * Interrupt entry points save only callee clobbered registers in fast path.
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100498 *
499 * Entry runs with interrupts off.
500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100502/* 0(%rsp): ~(interrupt number) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 .macro interrupt func
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100504 cld
Denys Vlasenkoe90e1472015-02-26 14:40:28 -0800505 /*
506 * Since nothing in interrupt handling code touches r12...r15 members
507 * of "struct pt_regs", and since interrupts can nest, we can save
508 * four stack slots and simultaneously provide
509 * an unwind-friendly stack layout by saving "truncated" pt_regs
510 * exactly up to rbp slot, without these members.
511 */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800512 ALLOC_PT_GPREGS_ON_STACK -RBP
513 SAVE_C_REGS -RBP
514 /* this goes to 0(%rsp) for unwinder, not for saving the value: */
515 SAVE_EXTRA_REGS_RBP -RBP
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100516
Ingo Molnar4d732132015-06-08 20:43:07 +0200517 leaq -RBP(%rsp), %rdi /* arg1 for \func (pointer to pt_regs) */
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100518
Denys Vlasenko03335e92015-04-27 15:21:52 +0200519 testb $3, CS-RBP(%rsp)
Denys Vlasenkodde74f22015-04-27 15:21:51 +0200520 jz 1f
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100521 SWAPGS
Denys Vlasenko76f5df42015-02-26 14:40:27 -08005221:
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100523 /*
Denys Vlasenkoe90e1472015-02-26 14:40:28 -0800524 * Save previous stack pointer, optionally switch to interrupt stack.
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100525 * irq_count is used to check if a CPU is already on an interrupt stack
526 * or not. While this is essentially redundant with preempt_count it is
527 * a little cheaper to use a separate counter in the PDA (short of
528 * moving irq_enter into assembly, which would be too much work)
529 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200530 movq %rsp, %rsi
531 incl PER_CPU_VAR(irq_count)
532 cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
533 pushq %rsi
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100534 /* We entered an interrupt context - irqs are off: */
535 TRACE_IRQS_OFF
536
Ingo Molnar4d732132015-06-08 20:43:07 +0200537 call \func
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 .endm
539
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100540 /*
541 * The interrupt stubs push (~vector+0x80) onto the stack and
542 * then jump to common_interrupt.
543 */
H. Peter Anvin939b7872008-11-11 13:51:52 -0800544 .p2align CONFIG_X86_L1_CACHE_SHIFT
545common_interrupt:
Jan Beulichee4eb872012-11-02 11:18:39 +0000546 ASM_CLAC
Ingo Molnar4d732132015-06-08 20:43:07 +0200547 addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 interrupt do_IRQ
Denys Vlasenko34061f12015-03-23 14:03:59 +0100549 /* 0(%rsp): old RSP */
Jan Beulich7effaa82005-09-12 18:49:24 +0200550ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100551 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700552 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +0200553 decl PER_CPU_VAR(irq_count)
Frederic Weisbecker625dbc32011-01-06 15:22:47 +0100554
Frederic Weisbeckera2bbe752011-07-02 16:52:45 +0200555 /* Restore saved previous stack */
Ingo Molnar4d732132015-06-08 20:43:07 +0200556 popq %rsi
Denys Vlasenkoe90e1472015-02-26 14:40:28 -0800557 /* return code expects complete pt_regs - adjust rsp accordingly: */
Ingo Molnar4d732132015-06-08 20:43:07 +0200558 leaq -RBP(%rsi), %rsp
Frederic Weisbecker625dbc32011-01-06 15:22:47 +0100559
Denys Vlasenko03335e92015-04-27 15:21:52 +0200560 testb $3, CS(%rsp)
Denys Vlasenkodde74f22015-04-27 15:21:51 +0200561 jz retint_kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Interrupt came from user space */
Andy Lutomirski5e99cb72015-07-03 12:44:19 -0700563GLOBAL(retint_user)
Denys Vlasenkoa3675b32015-03-30 20:09:34 +0200564 GET_THREAD_INFO(%rcx)
Ingo Molnar4d732132015-06-08 20:43:07 +0200565
566 /* %rcx: thread info. Interrupts are off. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567retint_with_reschedule:
Ingo Molnar4d732132015-06-08 20:43:07 +0200568 movl $_TIF_WORK_MASK, %edi
Jan Beulich7effaa82005-09-12 18:49:24 +0200569retint_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200570 LOCKDEP_SYS_EXIT_IRQ
Ingo Molnar4d732132015-06-08 20:43:07 +0200571 movl TI_flags(%rcx), %edx
572 andl %edi, %edx
573 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200574
Ingo Molnar4d732132015-06-08 20:43:07 +0200575retint_swapgs: /* return to user-space */
Ingo Molnar2601e642006-07-03 00:24:45 -0700576 /*
577 * The iretq could re-enable interrupts:
578 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100579 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700580 TRACE_IRQS_IRETQ
Andy Lutomirski2a23c6b2014-07-22 12:46:50 -0700581
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100582 SWAPGS
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200583 jmp restore_c_regs_and_iret
Ingo Molnar2601e642006-07-03 00:24:45 -0700584
Denys Vlasenko627276c2015-03-30 20:09:31 +0200585/* Returning to kernel space */
Denys Vlasenko6ba71b72015-03-31 19:00:05 +0200586retint_kernel:
Denys Vlasenko627276c2015-03-30 20:09:31 +0200587#ifdef CONFIG_PREEMPT
588 /* Interrupts are off */
589 /* Check if we need preemption */
Ingo Molnar4d732132015-06-08 20:43:07 +0200590 bt $9, EFLAGS(%rsp) /* were interrupts off? */
Denys Vlasenko6ba71b72015-03-31 19:00:05 +0200591 jnc 1f
Ingo Molnar4d732132015-06-08 20:43:07 +02005920: cmpl $0, PER_CPU_VAR(__preempt_count)
Denys Vlasenko36acef22015-03-31 19:00:07 +0200593 jnz 1f
Denys Vlasenko627276c2015-03-30 20:09:31 +0200594 call preempt_schedule_irq
Denys Vlasenko36acef22015-03-31 19:00:07 +0200595 jmp 0b
Denys Vlasenko6ba71b72015-03-31 19:00:05 +02005961:
Denys Vlasenko627276c2015-03-30 20:09:31 +0200597#endif
Ingo Molnar2601e642006-07-03 00:24:45 -0700598 /*
599 * The iretq could re-enable interrupts:
600 */
601 TRACE_IRQS_IRETQ
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200602
603/*
604 * At this label, code paths which return to kernel and to user,
605 * which come from interrupts/exception and from syscalls, merge.
606 */
607restore_c_regs_and_iret:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800608 RESTORE_C_REGS
609 REMOVE_PT_GPREGS_FROM_STACK 8
Andy Lutomirski7209a752014-07-23 08:34:11 -0700610 INTERRUPT_RETURN
611
612ENTRY(native_iret)
H. Peter Anvin3891a042014-04-29 16:46:09 -0700613 /*
614 * Are we returning to a stack segment from the LDT? Note: in
615 * 64-bit mode SS:RSP on the exception stack is always valid.
616 */
H. Peter Anvin34273f42014-05-04 10:36:22 -0700617#ifdef CONFIG_X86_ESPFIX64
Ingo Molnar4d732132015-06-08 20:43:07 +0200618 testb $4, (SS-RIP)(%rsp)
619 jnz native_irq_return_ldt
H. Peter Anvin34273f42014-05-04 10:36:22 -0700620#endif
H. Peter Anvin3891a042014-04-29 16:46:09 -0700621
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800622.global native_irq_return_iret
Andy Lutomirski7209a752014-07-23 08:34:11 -0700623native_irq_return_iret:
Andy Lutomirskib645af22014-11-22 18:00:33 -0800624 /*
625 * This may fault. Non-paranoid faults on return to userspace are
626 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
627 * Double-faults due to espfix64 are handled in do_double_fault.
628 * Other faults here are fatal.
629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 iretq
Ingo Molnar3701d8632008-02-09 23:24:08 +0100631
H. Peter Anvin34273f42014-05-04 10:36:22 -0700632#ifdef CONFIG_X86_ESPFIX64
Andy Lutomirski7209a752014-07-23 08:34:11 -0700633native_irq_return_ldt:
Ingo Molnar4d732132015-06-08 20:43:07 +0200634 pushq %rax
635 pushq %rdi
H. Peter Anvin3891a042014-04-29 16:46:09 -0700636 SWAPGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200637 movq PER_CPU_VAR(espfix_waddr), %rdi
638 movq %rax, (0*8)(%rdi) /* RAX */
639 movq (2*8)(%rsp), %rax /* RIP */
640 movq %rax, (1*8)(%rdi)
641 movq (3*8)(%rsp), %rax /* CS */
642 movq %rax, (2*8)(%rdi)
643 movq (4*8)(%rsp), %rax /* RFLAGS */
644 movq %rax, (3*8)(%rdi)
645 movq (6*8)(%rsp), %rax /* SS */
646 movq %rax, (5*8)(%rdi)
647 movq (5*8)(%rsp), %rax /* RSP */
648 movq %rax, (4*8)(%rdi)
649 andl $0xffff0000, %eax
650 popq %rdi
651 orq PER_CPU_VAR(espfix_stack), %rax
H. Peter Anvin3891a042014-04-29 16:46:09 -0700652 SWAPGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200653 movq %rax, %rsp
654 popq %rax
655 jmp native_irq_return_iret
H. Peter Anvin34273f42014-05-04 10:36:22 -0700656#endif
H. Peter Anvin3891a042014-04-29 16:46:09 -0700657
Jan Beulich7effaa82005-09-12 18:49:24 +0200658 /* edi: workmask, edx: work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659retint_careful:
Ingo Molnar4d732132015-06-08 20:43:07 +0200660 bt $TIF_NEED_RESCHED, %edx
661 jnc retint_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700662 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100663 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar4d732132015-06-08 20:43:07 +0200664 pushq %rdi
Frederic Weisbecker04304992012-07-11 20:26:38 +0200665 SCHEDULE_USER
Ingo Molnar4d732132015-06-08 20:43:07 +0200666 popq %rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100668 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700669 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +0200670 jmp retint_check
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672retint_signal:
Ingo Molnar4d732132015-06-08 20:43:07 +0200673 testl $_TIF_DO_NOTIFY_MASK, %edx
674 jz retint_swapgs
Ingo Molnar2601e642006-07-03 00:24:45 -0700675 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100676 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800677 SAVE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200678 movq $-1, ORIG_RAX(%rsp)
679 xorl %esi, %esi /* oldset */
680 movq %rsp, %rdi /* &pt_regs */
681 call do_notify_resume
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800682 RESTORE_EXTRA_REGS
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100683 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700684 TRACE_IRQS_OFF
Andi Kleenbe9e6872005-05-01 08:58:51 -0700685 GET_THREAD_INFO(%rcx)
Ingo Molnar4d732132015-06-08 20:43:07 +0200686 jmp retint_with_reschedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Jan Beulich4b787e02006-06-26 13:56:55 +0200688END(common_interrupt)
H. Peter Anvin3891a042014-04-29 16:46:09 -0700689
Masami Hiramatsu8222d712009-08-27 13:23:25 -0400690/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * APIC interrupts.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100692 */
Seiji Aguchicf910e82013-06-20 11:46:53 -0400693.macro apicinterrupt3 num sym do_sym
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100694ENTRY(\sym)
Jan Beulichee4eb872012-11-02 11:18:39 +0000695 ASM_CLAC
Ingo Molnar4d732132015-06-08 20:43:07 +0200696 pushq $~(\num)
Jan Beulich39e95432011-11-29 11:03:46 +0000697.Lcommon_\sym:
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100698 interrupt \do_sym
Ingo Molnar4d732132015-06-08 20:43:07 +0200699 jmp ret_from_intr
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100700END(\sym)
701.endm
Jacob Shin89b831e2005-11-05 17:25:53 +0100702
Seiji Aguchicf910e82013-06-20 11:46:53 -0400703#ifdef CONFIG_TRACING
704#define trace(sym) trace_##sym
705#define smp_trace(sym) smp_trace_##sym
706
707.macro trace_apicinterrupt num sym
708apicinterrupt3 \num trace(\sym) smp_trace(\sym)
709.endm
710#else
711.macro trace_apicinterrupt num sym do_sym
712.endm
713#endif
714
715.macro apicinterrupt num sym do_sym
716apicinterrupt3 \num \sym \do_sym
717trace_apicinterrupt \num \sym
718.endm
719
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100720#ifdef CONFIG_SMP
Ingo Molnar4d732132015-06-08 20:43:07 +0200721apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
722apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723#endif
724
Nick Piggin03b48632009-01-20 04:36:04 +0100725#ifdef CONFIG_X86_UV
Ingo Molnar4d732132015-06-08 20:43:07 +0200726apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
Nick Piggin03b48632009-01-20 04:36:04 +0100727#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200728
729apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
730apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Yang Zhangd78f2662013-04-11 19:25:11 +0800732#ifdef CONFIG_HAVE_KVM
Ingo Molnar4d732132015-06-08 20:43:07 +0200733apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
734apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
Yang Zhangd78f2662013-04-11 19:25:11 +0800735#endif
736
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400737#ifdef CONFIG_X86_MCE_THRESHOLD
Ingo Molnar4d732132015-06-08 20:43:07 +0200738apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400739#endif
740
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500741#ifdef CONFIG_X86_MCE_AMD
Ingo Molnar4d732132015-06-08 20:43:07 +0200742apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500743#endif
744
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400745#ifdef CONFIG_X86_THERMAL_VECTOR
Ingo Molnar4d732132015-06-08 20:43:07 +0200746apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400747#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100749#ifdef CONFIG_SMP
Ingo Molnar4d732132015-06-08 20:43:07 +0200750apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
751apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
752apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100753#endif
754
Ingo Molnar4d732132015-06-08 20:43:07 +0200755apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
756apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100757
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800758#ifdef CONFIG_IRQ_WORK
Ingo Molnar4d732132015-06-08 20:43:07 +0200759apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
Ingo Molnar241771e2008-12-03 10:39:53 +0100760#endif
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
763 * Exception entry points.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100764 */
Andy Lutomirski9b476682015-03-05 19:19:07 -0800765#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700766
767.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100768ENTRY(\sym)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700769 /* Sanity check */
770 .if \shift_ist != -1 && \paranoid == 0
771 .error "using shift_ist requires paranoid=1"
772 .endif
773
Jan Beulichee4eb872012-11-02 11:18:39 +0000774 ASM_CLAC
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100775 PARAVIRT_ADJUST_EXCEPTION_FRAME
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700776
777 .ifeq \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200778 pushq $-1 /* ORIG_RAX: no syscall to restart */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700779 .endif
780
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800781 ALLOC_PT_GPREGS_ON_STACK
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700782
783 .if \paranoid
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800784 .if \paranoid == 1
Ingo Molnar4d732132015-06-08 20:43:07 +0200785 testb $3, CS(%rsp) /* If coming from userspace, switch stacks */
786 jnz 1f
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800787 .endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200788 call paranoid_entry
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700789 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200790 call error_entry
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700791 .endif
Denys Vlasenkoebfc4532015-02-26 14:40:34 -0800792 /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700793
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700794 .if \paranoid
Andy Lutomirski577ed452014-05-21 15:07:09 -0700795 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200796 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
Andy Lutomirski577ed452014-05-21 15:07:09 -0700797 .else
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100798 TRACE_IRQS_OFF
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700799 .endif
Andy Lutomirski577ed452014-05-21 15:07:09 -0700800 .endif
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700801
Ingo Molnar4d732132015-06-08 20:43:07 +0200802 movq %rsp, %rdi /* pt_regs pointer */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700803
804 .if \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200805 movq ORIG_RAX(%rsp), %rsi /* get error code */
806 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700807 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200808 xorl %esi, %esi /* no error code */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700809 .endif
810
Andy Lutomirski577ed452014-05-21 15:07:09 -0700811 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200812 subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700813 .endif
814
Ingo Molnar4d732132015-06-08 20:43:07 +0200815 call \do_sym
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700816
Andy Lutomirski577ed452014-05-21 15:07:09 -0700817 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200818 addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700819 .endif
820
Denys Vlasenkoebfc4532015-02-26 14:40:34 -0800821 /* these procedures expect "no swapgs" flag in ebx */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700822 .if \paranoid
Ingo Molnar4d732132015-06-08 20:43:07 +0200823 jmp paranoid_exit
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700824 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200825 jmp error_exit
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700826 .endif
827
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800828 .if \paranoid == 1
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800829 /*
830 * Paranoid entry from userspace. Switch stacks and treat it
831 * as a normal entry. This means that paranoid handlers
832 * run in real process context if user_mode(regs).
833 */
8341:
Ingo Molnar4d732132015-06-08 20:43:07 +0200835 call error_entry
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800836
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800837
Ingo Molnar4d732132015-06-08 20:43:07 +0200838 movq %rsp, %rdi /* pt_regs pointer */
839 call sync_regs
840 movq %rax, %rsp /* switch stack */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800841
Ingo Molnar4d732132015-06-08 20:43:07 +0200842 movq %rsp, %rdi /* pt_regs pointer */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800843
844 .if \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200845 movq ORIG_RAX(%rsp), %rsi /* get error code */
846 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800847 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200848 xorl %esi, %esi /* no error code */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800849 .endif
850
Ingo Molnar4d732132015-06-08 20:43:07 +0200851 call \do_sym
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800852
Ingo Molnar4d732132015-06-08 20:43:07 +0200853 jmp error_exit /* %ebx: no swapgs flag */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800854 .endif
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +0100855END(\sym)
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100856.endm
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100857
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400858#ifdef CONFIG_TRACING
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700859.macro trace_idtentry sym do_sym has_error_code:req
860idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
861idtentry \sym \do_sym has_error_code=\has_error_code
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400862.endm
863#else
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700864.macro trace_idtentry sym do_sym has_error_code:req
865idtentry \sym \do_sym has_error_code=\has_error_code
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400866.endm
867#endif
868
Ingo Molnar4d732132015-06-08 20:43:07 +0200869idtentry divide_error do_divide_error has_error_code=0
870idtentry overflow do_overflow has_error_code=0
871idtentry bounds do_bounds has_error_code=0
872idtentry invalid_op do_invalid_op has_error_code=0
873idtentry device_not_available do_device_not_available has_error_code=0
874idtentry double_fault do_double_fault has_error_code=1 paranoid=2
875idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
876idtentry invalid_TSS do_invalid_TSS has_error_code=1
877idtentry segment_not_present do_segment_not_present has_error_code=1
878idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
879idtentry coprocessor_error do_coprocessor_error has_error_code=0
880idtentry alignment_check do_alignment_check has_error_code=1
881idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400882
Ingo Molnar2601e642006-07-03 00:24:45 -0700883
Ingo Molnar4d732132015-06-08 20:43:07 +0200884 /*
885 * Reload gs selector with exception handling
886 * edi: new selector
887 */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -0400888ENTRY(native_load_gs_index)
Ingo Molnar131484c2015-05-28 12:21:47 +0200889 pushfq
Jeremy Fitzhardingeb8aa2872009-01-28 14:35:03 -0800890 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300891 SWAPGS
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100892gs_change:
Ingo Molnar4d732132015-06-08 20:43:07 +0200893 movl %edi, %gs
8942: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100895 SWAPGS
Ingo Molnar131484c2015-05-28 12:21:47 +0200896 popfq
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300897 ret
Alexander van Heukelum6efdcfa2008-11-23 10:15:32 +0100898END(native_load_gs_index)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100899
Ingo Molnar4d732132015-06-08 20:43:07 +0200900 _ASM_EXTABLE(gs_change, bad_gs)
901 .section .fixup, "ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* running with kernelgs */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100903bad_gs:
Ingo Molnar4d732132015-06-08 20:43:07 +0200904 SWAPGS /* switch back to user gs */
905 xorl %eax, %eax
906 movl %eax, %gs
907 jmp 2b
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300908 .previous
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100909
Andi Kleen26995002006-08-02 22:37:28 +0200910/* Call softirq on interrupt stack. Interrupts are off. */
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200911ENTRY(do_softirq_own_stack)
Ingo Molnar4d732132015-06-08 20:43:07 +0200912 pushq %rbp
913 mov %rsp, %rbp
914 incl PER_CPU_VAR(irq_count)
915 cmove PER_CPU_VAR(irq_stack_ptr), %rsp
916 push %rbp /* frame pointer backlink */
917 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +0200918 leaveq
Ingo Molnar4d732132015-06-08 20:43:07 +0200919 decl PER_CPU_VAR(irq_count)
Andi Kleened6b6762005-07-28 21:15:49 -0700920 ret
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200921END(do_softirq_own_stack)
Andi Kleen75154f42007-06-23 02:29:25 +0200922
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700923#ifdef CONFIG_XEN
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700924idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700925
926/*
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300927 * A note on the "critical region" in our callback handler.
928 * We want to avoid stacking callback handlers due to events occurring
929 * during handling of the last event. To do this, we keep events disabled
930 * until we've done all processing. HOWEVER, we must enable events before
931 * popping the stack frame (can't be done atomically) and so it would still
932 * be possible to get enough handler activations to overflow the stack.
933 * Although unlikely, bugs of that kind are hard to track down, so we'd
934 * like to avoid the possibility.
935 * So, on entry to the handler we detect whether we interrupted an
936 * existing activation in its critical region -- if so, we pop the current
937 * activation and restart the handler using the previous one.
938 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200939ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */
940
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300941/*
942 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
943 * see the correct pointer to the pt_regs
944 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200945 movq %rdi, %rsp /* we don't return, adjust the stack frame */
94611: incl PER_CPU_VAR(irq_count)
947 movq %rsp, %rbp
948 cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
949 pushq %rbp /* frame pointer backlink */
950 call xen_evtchn_do_upcall
951 popq %rsp
952 decl PER_CPU_VAR(irq_count)
David Vrabelfdfd8112015-02-19 15:23:17 +0000953#ifndef CONFIG_PREEMPT
Ingo Molnar4d732132015-06-08 20:43:07 +0200954 call xen_maybe_preempt_hcall
David Vrabelfdfd8112015-02-19 15:23:17 +0000955#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200956 jmp error_exit
Alexander van Heukelum371c3942011-03-11 21:59:38 +0100957END(xen_do_hypervisor_callback)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700958
959/*
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300960 * Hypervisor uses this for application faults while it executes.
961 * We get here for two reasons:
962 * 1. Fault while reloading DS, ES, FS or GS
963 * 2. Fault while executing IRET
964 * Category 1 we do not need to fix up as Xen has already reloaded all segment
965 * registers that could be reloaded and zeroed the others.
966 * Category 2 we fix up by killing the current process. We cannot use the
967 * normal Linux return path in this case because if we use the IRET hypercall
968 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
969 * We distinguish between categories by comparing each saved segment register
970 * with its current contents: any discrepancy means we in category 1.
971 */
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700972ENTRY(xen_failsafe_callback)
Ingo Molnar4d732132015-06-08 20:43:07 +0200973 movl %ds, %ecx
974 cmpw %cx, 0x10(%rsp)
975 jne 1f
976 movl %es, %ecx
977 cmpw %cx, 0x18(%rsp)
978 jne 1f
979 movl %fs, %ecx
980 cmpw %cx, 0x20(%rsp)
981 jne 1f
982 movl %gs, %ecx
983 cmpw %cx, 0x28(%rsp)
984 jne 1f
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700985 /* All segments match their saved values => Category 2 (Bad IRET). */
Ingo Molnar4d732132015-06-08 20:43:07 +0200986 movq (%rsp), %rcx
987 movq 8(%rsp), %r11
988 addq $0x30, %rsp
989 pushq $0 /* RIP */
990 pushq %r11
991 pushq %rcx
992 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07009931: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
Ingo Molnar4d732132015-06-08 20:43:07 +0200994 movq (%rsp), %rcx
995 movq 8(%rsp), %r11
996 addq $0x30, %rsp
997 pushq $-1 /* orig_ax = -1 => not a system call */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800998 ALLOC_PT_GPREGS_ON_STACK
999 SAVE_C_REGS
1000 SAVE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +02001001 jmp error_exit
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001002END(xen_failsafe_callback)
1003
Seiji Aguchicf910e82013-06-20 11:46:53 -04001004apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
Sheng Yang38e20b02010-05-14 12:40:51 +01001005 xen_hvm_callback_vector xen_evtchn_do_upcall
1006
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001007#endif /* CONFIG_XEN */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001008
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001009#if IS_ENABLED(CONFIG_HYPERV)
Seiji Aguchicf910e82013-06-20 11:46:53 -04001010apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001011 hyperv_callback_vector hyperv_vector_handler
1012#endif /* CONFIG_HYPERV */
1013
Ingo Molnar4d732132015-06-08 20:43:07 +02001014idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1015idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1016idtentry stack_segment do_stack_segment has_error_code=1
1017
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -07001018#ifdef CONFIG_XEN
Ingo Molnar4d732132015-06-08 20:43:07 +02001019idtentry xen_debug do_debug has_error_code=0
1020idtentry xen_int3 do_int3 has_error_code=0
1021idtentry xen_stack_segment do_stack_segment has_error_code=1
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -07001022#endif
Ingo Molnar4d732132015-06-08 20:43:07 +02001023
1024idtentry general_protection do_general_protection has_error_code=1
1025trace_idtentry page_fault do_page_fault has_error_code=1
1026
Gleb Natapov631bc482010-10-14 11:22:52 +02001027#ifdef CONFIG_KVM_GUEST
Ingo Molnar4d732132015-06-08 20:43:07 +02001028idtentry async_page_fault do_async_page_fault has_error_code=1
Gleb Natapov631bc482010-10-14 11:22:52 +02001029#endif
Ingo Molnar4d732132015-06-08 20:43:07 +02001030
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001031#ifdef CONFIG_X86_MCE
Ingo Molnar4d732132015-06-08 20:43:07 +02001032idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001033#endif
1034
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001035/*
1036 * Save all registers in pt_regs, and switch gs if needed.
1037 * Use slow, but surefire "are we in kernel?" check.
1038 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1039 */
1040ENTRY(paranoid_entry)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001041 cld
1042 SAVE_C_REGS 8
1043 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +02001044 movl $1, %ebx
1045 movl $MSR_GS_BASE, %ecx
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001046 rdmsr
Ingo Molnar4d732132015-06-08 20:43:07 +02001047 testl %edx, %edx
1048 js 1f /* negative -> in kernel */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001049 SWAPGS
Ingo Molnar4d732132015-06-08 20:43:07 +02001050 xorl %ebx, %ebx
Denys Vlasenko1eeb2072015-02-26 14:40:33 -080010511: ret
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001052END(paranoid_entry)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001053
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001054/*
1055 * "Paranoid" exit path from exception stack. This is invoked
1056 * only on return from non-NMI IST interrupts that came
1057 * from kernel space.
1058 *
1059 * We may be returning to very strange contexts (e.g. very early
1060 * in syscall entry), so checking for preemption here would
1061 * be complicated. Fortunately, we there's no good reason
1062 * to try to handle preemption here.
Ingo Molnar4d732132015-06-08 20:43:07 +02001063 *
1064 * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001065 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001066ENTRY(paranoid_exit)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001067 DISABLE_INTERRUPTS(CLBR_NONE)
Steven Rostedt5963e312012-05-30 11:54:53 -04001068 TRACE_IRQS_OFF_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +02001069 testl %ebx, %ebx /* swapgs needed? */
1070 jnz paranoid_exit_no_swapgs
Denys Vlasenkof2db9382015-02-26 14:40:30 -08001071 TRACE_IRQS_IRETQ
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001072 SWAPGS_UNSAFE_STACK
Ingo Molnar4d732132015-06-08 20:43:07 +02001073 jmp paranoid_exit_restore
Denys Vlasenko0d550832015-02-26 14:40:29 -08001074paranoid_exit_no_swapgs:
Denys Vlasenkof2db9382015-02-26 14:40:30 -08001075 TRACE_IRQS_IRETQ_DEBUG
Denys Vlasenko0d550832015-02-26 14:40:29 -08001076paranoid_exit_restore:
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001077 RESTORE_EXTRA_REGS
1078 RESTORE_C_REGS
1079 REMOVE_PT_GPREGS_FROM_STACK 8
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001080 INTERRUPT_RETURN
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001081END(paranoid_exit)
1082
1083/*
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001084 * Save all registers in pt_regs, and switch gs if needed.
Andy Lutomirski539f5112015-06-09 12:36:01 -07001085 * Return: EBX=0: came from user mode; EBX=1: otherwise
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001086 */
1087ENTRY(error_entry)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001088 cld
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001089 SAVE_C_REGS 8
1090 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +02001091 xorl %ebx, %ebx
Denys Vlasenko03335e92015-04-27 15:21:52 +02001092 testb $3, CS+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001093 jz .Lerror_kernelspace
Andy Lutomirski539f5112015-06-09 12:36:01 -07001094
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001095.Lerror_entry_from_usermode_swapgs:
1096 /*
1097 * We entered from user mode or we're pretending to have entered
1098 * from user mode due to an IRET fault.
1099 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001100 SWAPGS
Andy Lutomirski539f5112015-06-09 12:36:01 -07001101
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001102.Lerror_entry_from_usermode_after_swapgs:
1103.Lerror_entry_done:
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001104 TRACE_IRQS_OFF
1105 ret
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001106
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001107 /*
1108 * There are two places in the kernel that can potentially fault with
1109 * usergs. Handle them here. B stepping K8s sometimes report a
1110 * truncated RIP for IRET exceptions returning to compat mode. Check
1111 * for these here too.
1112 */
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001113.Lerror_kernelspace:
Ingo Molnar4d732132015-06-08 20:43:07 +02001114 incl %ebx
1115 leaq native_irq_return_iret(%rip), %rcx
1116 cmpq %rcx, RIP+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001117 je .Lerror_bad_iret
Ingo Molnar4d732132015-06-08 20:43:07 +02001118 movl %ecx, %eax /* zero extend */
1119 cmpq %rax, RIP+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001120 je .Lbstep_iret
Ingo Molnar4d732132015-06-08 20:43:07 +02001121 cmpq $gs_change, RIP+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001122 jne .Lerror_entry_done
Andy Lutomirski539f5112015-06-09 12:36:01 -07001123
1124 /*
1125 * hack: gs_change can fail with user gsbase. If this happens, fix up
1126 * gsbase and proceed. We'll fix up the exception and land in
1127 * gs_change's error handler with kernel gsbase.
1128 */
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001129 jmp .Lerror_entry_from_usermode_swapgs
Brian Gerstae24ffe2009-10-12 10:18:23 -04001130
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001131.Lbstep_iret:
Brian Gerstae24ffe2009-10-12 10:18:23 -04001132 /* Fix truncated RIP */
Ingo Molnar4d732132015-06-08 20:43:07 +02001133 movq %rcx, RIP+8(%rsp)
Andy Lutomirskib645af22014-11-22 18:00:33 -08001134 /* fall through */
1135
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001136.Lerror_bad_iret:
Andy Lutomirski539f5112015-06-09 12:36:01 -07001137 /*
1138 * We came from an IRET to user mode, so we have user gsbase.
1139 * Switch to kernel gsbase:
1140 */
Andy Lutomirskib645af22014-11-22 18:00:33 -08001141 SWAPGS
Andy Lutomirski539f5112015-06-09 12:36:01 -07001142
1143 /*
1144 * Pretend that the exception came from user mode: set up pt_regs
1145 * as if we faulted immediately after IRET and clear EBX so that
1146 * error_exit knows that we will be returning to user mode.
1147 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001148 mov %rsp, %rdi
1149 call fixup_bad_iret
1150 mov %rax, %rsp
Andy Lutomirski539f5112015-06-09 12:36:01 -07001151 decl %ebx
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001152 jmp .Lerror_entry_from_usermode_after_swapgs
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001153END(error_entry)
1154
1155
Andy Lutomirski539f5112015-06-09 12:36:01 -07001156/*
1157 * On entry, EBS is a "return to kernel mode" flag:
1158 * 1: already in kernel mode, don't need SWAPGS
1159 * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
1160 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001161ENTRY(error_exit)
Ingo Molnar4d732132015-06-08 20:43:07 +02001162 movl %ebx, %eax
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001163 RESTORE_EXTRA_REGS
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001164 DISABLE_INTERRUPTS(CLBR_NONE)
1165 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +02001166 testl %eax, %eax
1167 jnz retint_kernel
1168 jmp retint_user
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001169END(error_exit)
1170
Denys Vlasenko0784b362015-04-01 16:50:57 +02001171/* Runs on exception stack */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001172ENTRY(nmi)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001173 PARAVIRT_ADJUST_EXCEPTION_FRAME
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001174 /*
1175 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1176 * the iretq it performs will take us out of NMI context.
1177 * This means that we can have nested NMIs where the next
1178 * NMI is using the top of the stack of the previous NMI. We
1179 * can't let it execute because the nested NMI will corrupt the
1180 * stack of the previous NMI. NMI handlers are not re-entrant
1181 * anyway.
1182 *
1183 * To handle this case we do the following:
1184 * Check the a special location on the stack that contains
1185 * a variable that is set when NMIs are executing.
1186 * The interrupted task's stack is also checked to see if it
1187 * is an NMI stack.
1188 * If the variable is not set and the stack is not the NMI
1189 * stack then:
1190 * o Set the special variable on the stack
1191 * o Copy the interrupt frame into a "saved" location on the stack
1192 * o Copy the interrupt frame into a "copy" location on the stack
1193 * o Continue processing the NMI
1194 * If the variable is set or the previous stack is the NMI stack:
1195 * o Modify the "copy" location to jump to the repeate_nmi
1196 * o return back to the first NMI
1197 *
1198 * Now on exit of the first NMI, we first clear the stack variable
1199 * The NMI stack will tell any nested NMIs at that point that it is
1200 * nested. Then we pop the stack normally with iret, and if there was
1201 * a nested NMI that updated the copy interrupt stack frame, a
1202 * jump will be made to the repeat_nmi code that will handle the second
1203 * NMI.
1204 */
1205
Denys Vlasenko146b2b02015-03-25 18:18:13 +01001206 /* Use %rdx as our temp variable throughout */
Ingo Molnar4d732132015-06-08 20:43:07 +02001207 pushq %rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001208
1209 /*
Steven Rostedt45d5a162012-02-19 16:43:37 -05001210 * If %cs was not the kernel segment, then the NMI triggered in user
1211 * space, which means it is definitely not nested.
1212 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001213 cmpl $__KERNEL_CS, 16(%rsp)
1214 jne first_nmi
Steven Rostedt45d5a162012-02-19 16:43:37 -05001215
1216 /*
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001217 * Check the special variable on the stack to see if NMIs are
1218 * executing.
1219 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001220 cmpl $1, -8(%rsp)
1221 je nested_nmi
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001222
1223 /*
1224 * Now test if the previous stack was an NMI stack.
1225 * We need the double check. We check the NMI stack to satisfy the
1226 * race when the first NMI clears the variable before returning.
1227 * We check the variable because the first NMI could be in a
1228 * breakpoint routine using a breakpoint stack.
1229 */
Denys Vlasenko0784b362015-04-01 16:50:57 +02001230 lea 6*8(%rsp), %rdx
1231 /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1232 cmpq %rdx, 4*8(%rsp)
1233 /* If the stack pointer is above the NMI stack, this is a normal NMI */
1234 ja first_nmi
Ingo Molnar4d732132015-06-08 20:43:07 +02001235
Denys Vlasenko0784b362015-04-01 16:50:57 +02001236 subq $EXCEPTION_STKSZ, %rdx
1237 cmpq %rdx, 4*8(%rsp)
1238 /* If it is below the NMI stack, it is a normal NMI */
1239 jb first_nmi
1240 /* Ah, it is within the NMI stack, treat it as nested */
Denys Vlasenko0784b362015-04-01 16:50:57 +02001241
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001242nested_nmi:
1243 /*
1244 * Do nothing if we interrupted the fixup in repeat_nmi.
1245 * It's about to repeat the NMI handler, so we are fine
1246 * with ignoring this one.
1247 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001248 movq $repeat_nmi, %rdx
1249 cmpq 8(%rsp), %rdx
1250 ja 1f
1251 movq $end_repeat_nmi, %rdx
1252 cmpq 8(%rsp), %rdx
1253 ja nested_nmi_out
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001254
12551:
1256 /* Set up the interrupted NMIs stack to jump to repeat_nmi */
Ingo Molnar4d732132015-06-08 20:43:07 +02001257 leaq -1*8(%rsp), %rdx
1258 movq %rdx, %rsp
1259 leaq -10*8(%rsp), %rdx
1260 pushq $__KERNEL_DS
1261 pushq %rdx
Ingo Molnar131484c2015-05-28 12:21:47 +02001262 pushfq
Ingo Molnar4d732132015-06-08 20:43:07 +02001263 pushq $__KERNEL_CS
1264 pushq $repeat_nmi
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001265
1266 /* Put stack back */
Ingo Molnar4d732132015-06-08 20:43:07 +02001267 addq $(6*8), %rsp
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001268
1269nested_nmi_out:
Ingo Molnar4d732132015-06-08 20:43:07 +02001270 popq %rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001271
1272 /* No need to check faults here */
1273 INTERRUPT_RETURN
1274
1275first_nmi:
1276 /*
1277 * Because nested NMIs will use the pushed location that we
1278 * stored in rdx, we must keep that space available.
1279 * Here's what our stack frame will look like:
1280 * +-------------------------+
1281 * | original SS |
1282 * | original Return RSP |
1283 * | original RFLAGS |
1284 * | original CS |
1285 * | original RIP |
1286 * +-------------------------+
1287 * | temp storage for rdx |
1288 * +-------------------------+
1289 * | NMI executing variable |
1290 * +-------------------------+
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001291 * | copied SS |
1292 * | copied Return RSP |
1293 * | copied RFLAGS |
1294 * | copied CS |
1295 * | copied RIP |
1296 * +-------------------------+
Salman Qazi28696f42012-10-01 17:29:25 -07001297 * | Saved SS |
1298 * | Saved Return RSP |
1299 * | Saved RFLAGS |
1300 * | Saved CS |
1301 * | Saved RIP |
1302 * +-------------------------+
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001303 * | pt_regs |
1304 * +-------------------------+
1305 *
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001306 * The saved stack frame is used to fix up the copied stack frame
1307 * that a nested NMI may change to make the interrupted NMI iret jump
1308 * to the repeat_nmi. The original stack frame and the temp storage
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001309 * is also used by nested NMIs and can not be trusted on exit.
1310 */
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001311 /* Do not pop rdx, nested NMIs will corrupt that part of the stack */
Ingo Molnar4d732132015-06-08 20:43:07 +02001312 movq (%rsp), %rdx
Jan Beulich62610912012-02-24 14:54:37 +00001313
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001314 /* Set the NMI executing variable on the stack. */
Ingo Molnar4d732132015-06-08 20:43:07 +02001315 pushq $1
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001316
Ingo Molnar4d732132015-06-08 20:43:07 +02001317 /* Leave room for the "copied" frame */
1318 subq $(5*8), %rsp
Salman Qazi28696f42012-10-01 17:29:25 -07001319
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001320 /* Copy the stack frame to the Saved frame */
1321 .rept 5
Ingo Molnar4d732132015-06-08 20:43:07 +02001322 pushq 11*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001323 .endr
Jan Beulich62610912012-02-24 14:54:37 +00001324
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001325 /* Everything up to here is safe from nested NMIs */
1326
Jan Beulich62610912012-02-24 14:54:37 +00001327 /*
1328 * If there was a nested NMI, the first NMI's iret will return
1329 * here. But NMIs are still enabled and we can take another
1330 * nested NMI. The nested NMI checks the interrupted RIP to see
1331 * if it is between repeat_nmi and end_repeat_nmi, and if so
1332 * it will just return, as we are about to repeat an NMI anyway.
1333 * This makes it safe to copy to the stack frame that a nested
1334 * NMI will update.
1335 */
1336repeat_nmi:
1337 /*
1338 * Update the stack variable to say we are still in NMI (the update
1339 * is benign for the non-repeat case, where 1 was pushed just above
1340 * to this very stack slot).
1341 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001342 movq $1, 10*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001343
1344 /* Make another copy, this one may be modified by nested NMIs */
Ingo Molnar4d732132015-06-08 20:43:07 +02001345 addq $(10*8), %rsp
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001346 .rept 5
Ingo Molnar4d732132015-06-08 20:43:07 +02001347 pushq -6*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001348 .endr
Ingo Molnar4d732132015-06-08 20:43:07 +02001349 subq $(5*8), %rsp
Jan Beulich62610912012-02-24 14:54:37 +00001350end_repeat_nmi:
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001351
1352 /*
1353 * Everything below this point can be preempted by a nested
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001354 * NMI if the first NMI took an exception and reset our iret stack
1355 * so that we repeat another NMI.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001356 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001357 pushq $-1 /* ORIG_RAX: no syscall to restart */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001358 ALLOC_PT_GPREGS_ON_STACK
1359
Steven Rostedt1fd466e2011-12-08 12:32:27 -05001360 /*
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001361 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
Steven Rostedt1fd466e2011-12-08 12:32:27 -05001362 * as we should not be calling schedule in NMI context.
1363 * Even with normal interrupts enabled. An NMI should not be
1364 * setting NEED_RESCHED or anything that normal interrupts and
1365 * exceptions might do.
1366 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001367 call paranoid_entry
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001368
1369 /*
1370 * Save off the CR2 register. If we take a page fault in the NMI then
1371 * it could corrupt the CR2 value. If the NMI preempts a page fault
1372 * handler before it was able to read the CR2 register, and then the
1373 * NMI itself takes a page fault, the page fault that was preempted
1374 * will read the information from the NMI page fault and not the
1375 * origin fault. Save it off and restore it if it changes.
1376 * Use the r12 callee-saved register.
1377 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001378 movq %cr2, %r12
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001379
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001380 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
Ingo Molnar4d732132015-06-08 20:43:07 +02001381 movq %rsp, %rdi
1382 movq $-1, %rsi
1383 call do_nmi
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001384
1385 /* Did the NMI take a page fault? Restore cr2 if it did */
Ingo Molnar4d732132015-06-08 20:43:07 +02001386 movq %cr2, %rcx
1387 cmpq %rcx, %r12
1388 je 1f
1389 movq %r12, %cr2
Steven Rostedt7fbb98c2012-06-07 10:21:21 -040013901:
Ingo Molnar4d732132015-06-08 20:43:07 +02001391 testl %ebx, %ebx /* swapgs needed? */
1392 jnz nmi_restore
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001393nmi_swapgs:
1394 SWAPGS_UNSAFE_STACK
1395nmi_restore:
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001396 RESTORE_EXTRA_REGS
1397 RESTORE_C_REGS
Jan Beulich444723d2013-01-24 09:27:31 +00001398 /* Pop the extra iret frame at once */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001399 REMOVE_PT_GPREGS_FROM_STACK 6*8
Salman Qazi28696f42012-10-01 17:29:25 -07001400
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001401 /* Clear the NMI executing stack variable */
Ingo Molnar4d732132015-06-08 20:43:07 +02001402 movq $0, 5*8(%rsp)
Andy Lutomirski5ca6f702015-06-04 13:24:29 -07001403 INTERRUPT_RETURN
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001404END(nmi)
1405
1406ENTRY(ignore_sysret)
Ingo Molnar4d732132015-06-08 20:43:07 +02001407 mov $-ENOSYS, %eax
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001408 sysret
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001409END(ignore_sysret)