blob: 3bb2c4302df1f8ba18e21b997fd5b51e8ec55c54 [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
Ingo Molnar7fcb3bc2015-03-17 14:42:59 +0100232 /* Do syscall entry tracing */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100233tracesys:
Ingo Molnar4d732132015-06-08 20:43:07 +0200234 movq %rsp, %rdi
235 movl $AUDIT_ARCH_X86_64, %esi
236 call syscall_trace_enter_phase1
237 test %rax, %rax
238 jnz tracesys_phase2 /* if needed, run the slow path */
239 RESTORE_C_REGS_EXCEPT_RAX /* else restore clobbered regs */
240 movq ORIG_RAX(%rsp), %rax
241 jmp entry_SYSCALL_64_fastpath /* and return to the fast path */
Andy Lutomirski1dcf74f2014-09-05 15:13:56 -0700242
243tracesys_phase2:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800244 SAVE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200245 movq %rsp, %rdi
246 movl $AUDIT_ARCH_X86_64, %esi
247 movq %rax, %rdx
248 call syscall_trace_enter_phase2
Andy Lutomirski1dcf74f2014-09-05 15:13:56 -0700249
Roland McGrathd4d67152008-07-09 02:38:07 -0700250 /*
Denys Vlasenkoe90e1472015-02-26 14:40:28 -0800251 * Reload registers from stack in case ptrace changed them.
Andy Lutomirski1dcf74f2014-09-05 15:13:56 -0700252 * We don't reload %rax because syscall_trace_entry_phase2() returned
Roland McGrathd4d67152008-07-09 02:38:07 -0700253 * the value it wants us to use in the table lookup.
254 */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800255 RESTORE_C_REGS_EXCEPT_RAX
256 RESTORE_EXTRA_REGS
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800257#if __SYSCALL_MASK == ~0
Ingo Molnar4d732132015-06-08 20:43:07 +0200258 cmpq $__NR_syscall_max, %rax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800259#else
Ingo Molnar4d732132015-06-08 20:43:07 +0200260 andl $__SYSCALL_MASK, %eax
261 cmpl $__NR_syscall_max, %eax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800262#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200263 ja 1f /* return -ENOSYS (already in pt_regs->ax) */
264 movq %r10, %rcx /* fixup for C */
265 call *sys_call_table(, %rax, 8)
266 movq %rax, RAX(%rsp)
Denys Vlasenkoa6de5a22015-03-31 19:00:11 +02002671:
Ingo Molnar7fcb3bc2015-03-17 14:42:59 +0100268 /* Use IRET because user could have changed pt_regs->foo */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100269
270/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * Syscall return path ending with IRET.
Ingo Molnar7fcb3bc2015-03-17 14:42:59 +0100272 * Has correct iret frame.
Jan Beulichbcddc012006-12-07 02:14:02 +0100273 */
Cyrill Gorcunovbc8b2b92009-02-23 22:57:01 +0300274GLOBAL(int_ret_from_sys_call)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100275 DISABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenko4416c5a2015-03-31 19:00:03 +0200276int_ret_from_sys_call_irqs_off: /* jumps come here from the irqs-off SYSRET path */
Ingo Molnar2601e642006-07-03 00:24:45 -0700277 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +0200278 movl $_TIF_ALLWORK_MASK, %edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* edi: mask to check */
Cyrill Gorcunovbc8b2b92009-02-23 22:57:01 +0300280GLOBAL(int_with_check)
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200281 LOCKDEP_SYS_EXIT_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 GET_THREAD_INFO(%rcx)
Ingo Molnar4d732132015-06-08 20:43:07 +0200283 movl TI_flags(%rcx), %edx
284 andl %edi, %edx
285 jnz int_careful
286 andl $~TS_COMPAT, TI_status(%rcx)
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200287 jmp syscall_return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Ingo Molnar4d732132015-06-08 20:43:07 +0200289 /*
290 * Either reschedule or signal or syscall exit tracking needed.
291 * First do a reschedule test.
292 * edx: work, edi: workmask
293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294int_careful:
Ingo Molnar4d732132015-06-08 20:43:07 +0200295 bt $TIF_NEED_RESCHED, %edx
296 jnc int_very_careful
Ingo Molnar2601e642006-07-03 00:24:45 -0700297 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100298 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar4d732132015-06-08 20:43:07 +0200299 pushq %rdi
Frederic Weisbecker04304992012-07-11 20:26:38 +0200300 SCHEDULE_USER
Ingo Molnar4d732132015-06-08 20:43:07 +0200301 popq %rdi
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100302 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700303 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +0200304 jmp int_with_check
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Ingo Molnar7fcb3bc2015-03-17 14:42:59 +0100306 /* handle signals and tracing -- both require a full pt_regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307int_very_careful:
Ingo Molnar2601e642006-07-03 00:24:45 -0700308 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100309 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800310 SAVE_EXTRA_REGS
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100311 /* Check for syscall exit trace */
Ingo Molnar4d732132015-06-08 20:43:07 +0200312 testl $_TIF_WORK_SYSCALL_EXIT, %edx
313 jz int_signal
314 pushq %rdi
315 leaq 8(%rsp), %rdi /* &ptregs -> arg1 */
316 call syscall_trace_leave
317 popq %rdi
318 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU), %edi
319 jmp int_restore_rest
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321int_signal:
Ingo Molnar4d732132015-06-08 20:43:07 +0200322 testl $_TIF_DO_NOTIFY_MASK, %edx
323 jz 1f
324 movq %rsp, %rdi /* &ptregs -> arg1 */
325 xorl %esi, %esi /* oldset -> arg2 */
326 call do_notify_resume
3271: movl $_TIF_WORK_MASK, %edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328int_restore_rest:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800329 RESTORE_EXTRA_REGS
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100330 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700331 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +0200332 jmp int_with_check
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200333
334syscall_return:
335 /* The IRETQ could re-enable interrupts: */
336 DISABLE_INTERRUPTS(CLBR_ANY)
337 TRACE_IRQS_IRETQ
338
339 /*
340 * Try to use SYSRET instead of IRET if we're returning to
341 * a completely clean 64-bit userspace context.
342 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200343 movq RCX(%rsp), %rcx
344 movq RIP(%rsp), %r11
345 cmpq %rcx, %r11 /* RCX == RIP */
346 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200347
348 /*
349 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
350 * in kernel space. This essentially lets the user take over
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200351 * the kernel, since userspace controls RSP.
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200352 *
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200353 * If width of "canonical tail" ever becomes variable, this will need
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200354 * to be updated to remain correct on both old and new CPUs.
355 */
356 .ifne __VIRTUAL_MASK_SHIFT - 47
357 .error "virtual address width changed -- SYSRET checks need update"
358 .endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200359
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200360 /* Change top 16 bits to be the sign-extension of 47th bit */
361 shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
362 sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
Ingo Molnar4d732132015-06-08 20:43:07 +0200363
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200364 /* If this changed %rcx, it was not canonical */
365 cmpq %rcx, %r11
366 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200367
Ingo Molnar4d732132015-06-08 20:43:07 +0200368 cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
369 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200370
Ingo Molnar4d732132015-06-08 20:43:07 +0200371 movq R11(%rsp), %r11
372 cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
373 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200374
375 /*
376 * SYSRET can't restore RF. SYSRET can restore TF, but unlike IRET,
377 * restoring TF results in a trap from userspace immediately after
378 * SYSRET. This would cause an infinite loop whenever #DB happens
379 * with register state that satisfies the opportunistic SYSRET
380 * conditions. For example, single-stepping this user code:
381 *
Ingo Molnar4d732132015-06-08 20:43:07 +0200382 * movq $stuck_here, %rcx
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200383 * pushfq
384 * popq %r11
385 * stuck_here:
386 *
387 * would never get past 'stuck_here'.
388 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200389 testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
390 jnz opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200391
392 /* nothing to check for RSP */
393
Ingo Molnar4d732132015-06-08 20:43:07 +0200394 cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
395 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200396
397 /*
Ingo Molnar4d732132015-06-08 20:43:07 +0200398 * We win! This label is here just for ease of understanding
399 * perf profiles. Nothing jumps here.
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200400 */
401syscall_return_via_sysret:
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200402 /* rcx and r11 are already restored (see code above) */
403 RESTORE_C_REGS_EXCEPT_RCX_R11
Ingo Molnar4d732132015-06-08 20:43:07 +0200404 movq RSP(%rsp), %rsp
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200405 USERGS_SYSRET64
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200406
407opportunistic_sysret_failed:
408 SWAPGS
409 jmp restore_c_regs_and_iret
Ingo Molnarb2502b42015-06-08 08:42:03 +0200410END(entry_SYSCALL_64)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100411
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200412
Al Viro1d4b4b22012-10-22 22:34:11 -0400413 .macro FORK_LIKE func
414ENTRY(stub_\func)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800415 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +0200416 jmp sys_\func
Al Viro1d4b4b22012-10-22 22:34:11 -0400417END(stub_\func)
418 .endm
419
420 FORK_LIKE clone
421 FORK_LIKE fork
422 FORK_LIKE vfork
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424ENTRY(stub_execve)
Denys Vlasenkofc3e9582015-04-04 20:55:19 +0200425 call sys_execve
426return_from_execve:
427 testl %eax, %eax
428 jz 1f
429 /* exec failed, can use fast SYSRET code path in this case */
430 ret
4311:
432 /* must use IRET code path (pt_regs->cs may have changed) */
433 addq $8, %rsp
434 ZERO_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200435 movq %rax, RAX(%rsp)
Denys Vlasenkofc3e9582015-04-04 20:55:19 +0200436 jmp int_ret_from_sys_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200437END(stub_execve)
Denys Vlasenkoa37f34a2015-04-07 22:43:44 +0200438/*
439 * Remaining execve stubs are only 7 bytes long.
440 * ENTRY() often aligns to 16 bytes, which in this case has no benefits.
441 */
442 .align 8
443GLOBAL(stub_execveat)
Denys Vlasenkofc3e9582015-04-04 20:55:19 +0200444 call sys_execveat
445 jmp return_from_execve
David Drysdale27d6ec72014-12-12 16:57:33 -0800446END(stub_execveat)
447
Denys Vlasenkoac7f5df2015-04-21 18:03:13 +0200448#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION)
Denys Vlasenkoa37f34a2015-04-07 22:43:44 +0200449 .align 8
450GLOBAL(stub_x32_execve)
Denys Vlasenkoa37f34a2015-04-07 22:43:44 +0200451GLOBAL(stub32_execve)
Denys Vlasenko0f90fb92015-04-07 22:43:39 +0200452 call compat_sys_execve
453 jmp return_from_execve
Denys Vlasenko0f90fb92015-04-07 22:43:39 +0200454END(stub32_execve)
Denys Vlasenkoac7f5df2015-04-21 18:03:13 +0200455END(stub_x32_execve)
Denys Vlasenkoa37f34a2015-04-07 22:43:44 +0200456 .align 8
Denys Vlasenkoac7f5df2015-04-21 18:03:13 +0200457GLOBAL(stub_x32_execveat)
Denys Vlasenkoa37f34a2015-04-07 22:43:44 +0200458GLOBAL(stub32_execveat)
Denys Vlasenko0f90fb92015-04-07 22:43:39 +0200459 call compat_sys_execveat
460 jmp return_from_execve
Denys Vlasenko0f90fb92015-04-07 22:43:39 +0200461END(stub32_execveat)
Denys Vlasenkoac7f5df2015-04-21 18:03:13 +0200462END(stub_x32_execveat)
Denys Vlasenko0f90fb92015-04-07 22:43:39 +0200463#endif
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/*
466 * sigreturn is special because it needs to restore all registers on return.
467 * This cannot be done with SYSRET, so use the IRET return path instead.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469ENTRY(stub_rt_sigreturn)
Denys Vlasenko31f01192015-04-07 22:43:37 +0200470 /*
471 * SAVE_EXTRA_REGS result is not normally needed:
472 * sigreturn overwrites all pt_regs->GPREGS.
473 * But sigreturn can fail (!), and there is no easy way to detect that.
474 * To make sure RESTORE_EXTRA_REGS doesn't restore garbage on error,
475 * we SAVE_EXTRA_REGS here.
476 */
477 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +0200478 call sys_rt_sigreturn
Denys Vlasenko31f01192015-04-07 22:43:37 +0200479return_from_stub:
480 addq $8, %rsp
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800481 RESTORE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200482 movq %rax, RAX(%rsp)
483 jmp int_ret_from_sys_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200484END(stub_rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800486#ifdef CONFIG_X86_X32_ABI
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800487ENTRY(stub_x32_rt_sigreturn)
Denys Vlasenko31f01192015-04-07 22:43:37 +0200488 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +0200489 call sys32_x32_rt_sigreturn
490 jmp return_from_stub
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800491END(stub_x32_rt_sigreturn)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800492#endif
493
Jan Beulich7effaa82005-09-12 18:49:24 +0200494/*
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800495 * A newly forked process directly context switches into this address.
496 *
497 * rdi: prev task we switched from
498 */
499ENTRY(ret_from_fork)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800500
Ingo Molnar4d732132015-06-08 20:43:07 +0200501 LOCK ; btr $TIF_FORK, TI_flags(%r8)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800502
Ingo Molnar4d732132015-06-08 20:43:07 +0200503 pushq $0x0002
504 popfq /* reset kernel eflags */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800505
Ingo Molnar4d732132015-06-08 20:43:07 +0200506 call schedule_tail /* rdi: 'prev' task parameter */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800507
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800508 RESTORE_EXTRA_REGS
509
Ingo Molnar4d732132015-06-08 20:43:07 +0200510 testb $3, CS(%rsp) /* from kernel_thread? */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800511
Andy Lutomirski1e3fbb82015-02-26 14:40:39 -0800512 /*
513 * By the time we get here, we have no idea whether our pt_regs,
514 * ti flags, and ti status came from the 64-bit SYSCALL fast path,
Ingo Molnar138bd562015-06-05 14:11:49 +0200515 * the slow path, or one of the 32-bit compat paths.
Denys Vlasenko66ad4ef2015-04-07 22:43:42 +0200516 * Use IRET code path to return, since it can safely handle
Andy Lutomirski1e3fbb82015-02-26 14:40:39 -0800517 * all of the above.
518 */
Denys Vlasenko66ad4ef2015-04-07 22:43:42 +0200519 jnz int_ret_from_sys_call
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800520
Ingo Molnar4d732132015-06-08 20:43:07 +0200521 /*
522 * We came from kernel_thread
523 * nb: we depend on RESTORE_EXTRA_REGS above
524 */
525 movq %rbp, %rdi
526 call *%rbx
527 movl $0, RAX(%rsp)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800528 RESTORE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200529 jmp int_ret_from_sys_call
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800530END(ret_from_fork)
531
532/*
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200533 * Build the entry stubs with some assembler magic.
534 * We pack 1 stub into every 8-byte block.
H. Peter Anvin939b7872008-11-11 13:51:52 -0800535 */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200536 .align 8
H. Peter Anvin939b7872008-11-11 13:51:52 -0800537ENTRY(irq_entries_start)
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200538 vector=FIRST_EXTERNAL_VECTOR
539 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
Ingo Molnar4d732132015-06-08 20:43:07 +0200540 pushq $(~vector+0x80) /* Note: always in signed byte range */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200541 vector=vector+1
542 jmp common_interrupt
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200543 .align 8
544 .endr
H. Peter Anvin939b7872008-11-11 13:51:52 -0800545END(irq_entries_start)
546
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100547/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * Interrupt entry/exit.
549 *
550 * Interrupt entry points save only callee clobbered registers in fast path.
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100551 *
552 * Entry runs with interrupts off.
553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100555/* 0(%rsp): ~(interrupt number) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 .macro interrupt func
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100557 cld
Denys Vlasenkoe90e1472015-02-26 14:40:28 -0800558 /*
559 * Since nothing in interrupt handling code touches r12...r15 members
560 * of "struct pt_regs", and since interrupts can nest, we can save
561 * four stack slots and simultaneously provide
562 * an unwind-friendly stack layout by saving "truncated" pt_regs
563 * exactly up to rbp slot, without these members.
564 */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800565 ALLOC_PT_GPREGS_ON_STACK -RBP
566 SAVE_C_REGS -RBP
567 /* this goes to 0(%rsp) for unwinder, not for saving the value: */
568 SAVE_EXTRA_REGS_RBP -RBP
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100569
Ingo Molnar4d732132015-06-08 20:43:07 +0200570 leaq -RBP(%rsp), %rdi /* arg1 for \func (pointer to pt_regs) */
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100571
Denys Vlasenko03335e92015-04-27 15:21:52 +0200572 testb $3, CS-RBP(%rsp)
Denys Vlasenkodde74f22015-04-27 15:21:51 +0200573 jz 1f
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100574 SWAPGS
Denys Vlasenko76f5df42015-02-26 14:40:27 -08005751:
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100576 /*
Denys Vlasenkoe90e1472015-02-26 14:40:28 -0800577 * Save previous stack pointer, optionally switch to interrupt stack.
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100578 * irq_count is used to check if a CPU is already on an interrupt stack
579 * or not. While this is essentially redundant with preempt_count it is
580 * a little cheaper to use a separate counter in the PDA (short of
581 * moving irq_enter into assembly, which would be too much work)
582 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200583 movq %rsp, %rsi
584 incl PER_CPU_VAR(irq_count)
585 cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
586 pushq %rsi
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100587 /* We entered an interrupt context - irqs are off: */
588 TRACE_IRQS_OFF
589
Ingo Molnar4d732132015-06-08 20:43:07 +0200590 call \func
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 .endm
592
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100593 /*
594 * The interrupt stubs push (~vector+0x80) onto the stack and
595 * then jump to common_interrupt.
596 */
H. Peter Anvin939b7872008-11-11 13:51:52 -0800597 .p2align CONFIG_X86_L1_CACHE_SHIFT
598common_interrupt:
Jan Beulichee4eb872012-11-02 11:18:39 +0000599 ASM_CLAC
Ingo Molnar4d732132015-06-08 20:43:07 +0200600 addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 interrupt do_IRQ
Denys Vlasenko34061f12015-03-23 14:03:59 +0100602 /* 0(%rsp): old RSP */
Jan Beulich7effaa82005-09-12 18:49:24 +0200603ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100604 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700605 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +0200606 decl PER_CPU_VAR(irq_count)
Frederic Weisbecker625dbc32011-01-06 15:22:47 +0100607
Frederic Weisbeckera2bbe752011-07-02 16:52:45 +0200608 /* Restore saved previous stack */
Ingo Molnar4d732132015-06-08 20:43:07 +0200609 popq %rsi
Denys Vlasenkoe90e1472015-02-26 14:40:28 -0800610 /* return code expects complete pt_regs - adjust rsp accordingly: */
Ingo Molnar4d732132015-06-08 20:43:07 +0200611 leaq -RBP(%rsi), %rsp
Frederic Weisbecker625dbc32011-01-06 15:22:47 +0100612
Denys Vlasenko03335e92015-04-27 15:21:52 +0200613 testb $3, CS(%rsp)
Denys Vlasenkodde74f22015-04-27 15:21:51 +0200614 jz retint_kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* Interrupt came from user space */
Jan Beulich2f63b9d2015-06-01 13:03:59 +0100616retint_user:
Denys Vlasenkoa3675b32015-03-30 20:09:34 +0200617 GET_THREAD_INFO(%rcx)
Ingo Molnar4d732132015-06-08 20:43:07 +0200618
619 /* %rcx: thread info. Interrupts are off. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620retint_with_reschedule:
Ingo Molnar4d732132015-06-08 20:43:07 +0200621 movl $_TIF_WORK_MASK, %edi
Jan Beulich7effaa82005-09-12 18:49:24 +0200622retint_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200623 LOCKDEP_SYS_EXIT_IRQ
Ingo Molnar4d732132015-06-08 20:43:07 +0200624 movl TI_flags(%rcx), %edx
625 andl %edi, %edx
626 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200627
Ingo Molnar4d732132015-06-08 20:43:07 +0200628retint_swapgs: /* return to user-space */
Ingo Molnar2601e642006-07-03 00:24:45 -0700629 /*
630 * The iretq could re-enable interrupts:
631 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100632 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700633 TRACE_IRQS_IRETQ
Andy Lutomirski2a23c6b2014-07-22 12:46:50 -0700634
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100635 SWAPGS
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200636 jmp restore_c_regs_and_iret
Ingo Molnar2601e642006-07-03 00:24:45 -0700637
Denys Vlasenko627276c2015-03-30 20:09:31 +0200638/* Returning to kernel space */
Denys Vlasenko6ba71b72015-03-31 19:00:05 +0200639retint_kernel:
Denys Vlasenko627276c2015-03-30 20:09:31 +0200640#ifdef CONFIG_PREEMPT
641 /* Interrupts are off */
642 /* Check if we need preemption */
Ingo Molnar4d732132015-06-08 20:43:07 +0200643 bt $9, EFLAGS(%rsp) /* were interrupts off? */
Denys Vlasenko6ba71b72015-03-31 19:00:05 +0200644 jnc 1f
Ingo Molnar4d732132015-06-08 20:43:07 +02006450: cmpl $0, PER_CPU_VAR(__preempt_count)
Denys Vlasenko36acef22015-03-31 19:00:07 +0200646 jnz 1f
Denys Vlasenko627276c2015-03-30 20:09:31 +0200647 call preempt_schedule_irq
Denys Vlasenko36acef22015-03-31 19:00:07 +0200648 jmp 0b
Denys Vlasenko6ba71b72015-03-31 19:00:05 +02006491:
Denys Vlasenko627276c2015-03-30 20:09:31 +0200650#endif
Ingo Molnar2601e642006-07-03 00:24:45 -0700651 /*
652 * The iretq could re-enable interrupts:
653 */
654 TRACE_IRQS_IRETQ
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200655
656/*
657 * At this label, code paths which return to kernel and to user,
658 * which come from interrupts/exception and from syscalls, merge.
659 */
660restore_c_regs_and_iret:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800661 RESTORE_C_REGS
662 REMOVE_PT_GPREGS_FROM_STACK 8
Andy Lutomirski7209a752014-07-23 08:34:11 -0700663 INTERRUPT_RETURN
664
665ENTRY(native_iret)
H. Peter Anvin3891a042014-04-29 16:46:09 -0700666 /*
667 * Are we returning to a stack segment from the LDT? Note: in
668 * 64-bit mode SS:RSP on the exception stack is always valid.
669 */
H. Peter Anvin34273f42014-05-04 10:36:22 -0700670#ifdef CONFIG_X86_ESPFIX64
Ingo Molnar4d732132015-06-08 20:43:07 +0200671 testb $4, (SS-RIP)(%rsp)
672 jnz native_irq_return_ldt
H. Peter Anvin34273f42014-05-04 10:36:22 -0700673#endif
H. Peter Anvin3891a042014-04-29 16:46:09 -0700674
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800675.global native_irq_return_iret
Andy Lutomirski7209a752014-07-23 08:34:11 -0700676native_irq_return_iret:
Andy Lutomirskib645af22014-11-22 18:00:33 -0800677 /*
678 * This may fault. Non-paranoid faults on return to userspace are
679 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
680 * Double-faults due to espfix64 are handled in do_double_fault.
681 * Other faults here are fatal.
682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 iretq
Ingo Molnar3701d8632008-02-09 23:24:08 +0100684
H. Peter Anvin34273f42014-05-04 10:36:22 -0700685#ifdef CONFIG_X86_ESPFIX64
Andy Lutomirski7209a752014-07-23 08:34:11 -0700686native_irq_return_ldt:
Ingo Molnar4d732132015-06-08 20:43:07 +0200687 pushq %rax
688 pushq %rdi
H. Peter Anvin3891a042014-04-29 16:46:09 -0700689 SWAPGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200690 movq PER_CPU_VAR(espfix_waddr), %rdi
691 movq %rax, (0*8)(%rdi) /* RAX */
692 movq (2*8)(%rsp), %rax /* RIP */
693 movq %rax, (1*8)(%rdi)
694 movq (3*8)(%rsp), %rax /* CS */
695 movq %rax, (2*8)(%rdi)
696 movq (4*8)(%rsp), %rax /* RFLAGS */
697 movq %rax, (3*8)(%rdi)
698 movq (6*8)(%rsp), %rax /* SS */
699 movq %rax, (5*8)(%rdi)
700 movq (5*8)(%rsp), %rax /* RSP */
701 movq %rax, (4*8)(%rdi)
702 andl $0xffff0000, %eax
703 popq %rdi
704 orq PER_CPU_VAR(espfix_stack), %rax
H. Peter Anvin3891a042014-04-29 16:46:09 -0700705 SWAPGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200706 movq %rax, %rsp
707 popq %rax
708 jmp native_irq_return_iret
H. Peter Anvin34273f42014-05-04 10:36:22 -0700709#endif
H. Peter Anvin3891a042014-04-29 16:46:09 -0700710
Jan Beulich7effaa82005-09-12 18:49:24 +0200711 /* edi: workmask, edx: work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712retint_careful:
Ingo Molnar4d732132015-06-08 20:43:07 +0200713 bt $TIF_NEED_RESCHED, %edx
714 jnc retint_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700715 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100716 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar4d732132015-06-08 20:43:07 +0200717 pushq %rdi
Frederic Weisbecker04304992012-07-11 20:26:38 +0200718 SCHEDULE_USER
Ingo Molnar4d732132015-06-08 20:43:07 +0200719 popq %rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100721 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700722 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +0200723 jmp retint_check
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725retint_signal:
Ingo Molnar4d732132015-06-08 20:43:07 +0200726 testl $_TIF_DO_NOTIFY_MASK, %edx
727 jz retint_swapgs
Ingo Molnar2601e642006-07-03 00:24:45 -0700728 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100729 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800730 SAVE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200731 movq $-1, ORIG_RAX(%rsp)
732 xorl %esi, %esi /* oldset */
733 movq %rsp, %rdi /* &pt_regs */
734 call do_notify_resume
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800735 RESTORE_EXTRA_REGS
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100736 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700737 TRACE_IRQS_OFF
Andi Kleenbe9e6872005-05-01 08:58:51 -0700738 GET_THREAD_INFO(%rcx)
Ingo Molnar4d732132015-06-08 20:43:07 +0200739 jmp retint_with_reschedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jan Beulich4b787e02006-06-26 13:56:55 +0200741END(common_interrupt)
H. Peter Anvin3891a042014-04-29 16:46:09 -0700742
Masami Hiramatsu8222d712009-08-27 13:23:25 -0400743/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * APIC interrupts.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100745 */
Seiji Aguchicf910e82013-06-20 11:46:53 -0400746.macro apicinterrupt3 num sym do_sym
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100747ENTRY(\sym)
Jan Beulichee4eb872012-11-02 11:18:39 +0000748 ASM_CLAC
Ingo Molnar4d732132015-06-08 20:43:07 +0200749 pushq $~(\num)
Jan Beulich39e95432011-11-29 11:03:46 +0000750.Lcommon_\sym:
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100751 interrupt \do_sym
Ingo Molnar4d732132015-06-08 20:43:07 +0200752 jmp ret_from_intr
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100753END(\sym)
754.endm
Jacob Shin89b831e2005-11-05 17:25:53 +0100755
Seiji Aguchicf910e82013-06-20 11:46:53 -0400756#ifdef CONFIG_TRACING
757#define trace(sym) trace_##sym
758#define smp_trace(sym) smp_trace_##sym
759
760.macro trace_apicinterrupt num sym
761apicinterrupt3 \num trace(\sym) smp_trace(\sym)
762.endm
763#else
764.macro trace_apicinterrupt num sym do_sym
765.endm
766#endif
767
768.macro apicinterrupt num sym do_sym
769apicinterrupt3 \num \sym \do_sym
770trace_apicinterrupt \num \sym
771.endm
772
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100773#ifdef CONFIG_SMP
Ingo Molnar4d732132015-06-08 20:43:07 +0200774apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
775apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776#endif
777
Nick Piggin03b48632009-01-20 04:36:04 +0100778#ifdef CONFIG_X86_UV
Ingo Molnar4d732132015-06-08 20:43:07 +0200779apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
Nick Piggin03b48632009-01-20 04:36:04 +0100780#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200781
782apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
783apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Yang Zhangd78f2662013-04-11 19:25:11 +0800785#ifdef CONFIG_HAVE_KVM
Ingo Molnar4d732132015-06-08 20:43:07 +0200786apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
787apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
Yang Zhangd78f2662013-04-11 19:25:11 +0800788#endif
789
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400790#ifdef CONFIG_X86_MCE_THRESHOLD
Ingo Molnar4d732132015-06-08 20:43:07 +0200791apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400792#endif
793
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500794#ifdef CONFIG_X86_MCE_AMD
Ingo Molnar4d732132015-06-08 20:43:07 +0200795apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500796#endif
797
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400798#ifdef CONFIG_X86_THERMAL_VECTOR
Ingo Molnar4d732132015-06-08 20:43:07 +0200799apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400800#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100802#ifdef CONFIG_SMP
Ingo Molnar4d732132015-06-08 20:43:07 +0200803apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
804apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
805apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100806#endif
807
Ingo Molnar4d732132015-06-08 20:43:07 +0200808apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
809apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100810
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800811#ifdef CONFIG_IRQ_WORK
Ingo Molnar4d732132015-06-08 20:43:07 +0200812apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
Ingo Molnar241771e2008-12-03 10:39:53 +0100813#endif
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815/*
816 * Exception entry points.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100817 */
Andy Lutomirski9b476682015-03-05 19:19:07 -0800818#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700819
820.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100821ENTRY(\sym)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700822 /* Sanity check */
823 .if \shift_ist != -1 && \paranoid == 0
824 .error "using shift_ist requires paranoid=1"
825 .endif
826
Jan Beulichee4eb872012-11-02 11:18:39 +0000827 ASM_CLAC
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100828 PARAVIRT_ADJUST_EXCEPTION_FRAME
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700829
830 .ifeq \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200831 pushq $-1 /* ORIG_RAX: no syscall to restart */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700832 .endif
833
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800834 ALLOC_PT_GPREGS_ON_STACK
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700835
836 .if \paranoid
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800837 .if \paranoid == 1
Ingo Molnar4d732132015-06-08 20:43:07 +0200838 testb $3, CS(%rsp) /* If coming from userspace, switch stacks */
839 jnz 1f
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800840 .endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200841 call paranoid_entry
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700842 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200843 call error_entry
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700844 .endif
Denys Vlasenkoebfc4532015-02-26 14:40:34 -0800845 /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700846
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700847 .if \paranoid
Andy Lutomirski577ed452014-05-21 15:07:09 -0700848 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200849 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
Andy Lutomirski577ed452014-05-21 15:07:09 -0700850 .else
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100851 TRACE_IRQS_OFF
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700852 .endif
Andy Lutomirski577ed452014-05-21 15:07:09 -0700853 .endif
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700854
Ingo Molnar4d732132015-06-08 20:43:07 +0200855 movq %rsp, %rdi /* pt_regs pointer */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700856
857 .if \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200858 movq ORIG_RAX(%rsp), %rsi /* get error code */
859 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700860 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200861 xorl %esi, %esi /* no error code */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700862 .endif
863
Andy Lutomirski577ed452014-05-21 15:07:09 -0700864 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200865 subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700866 .endif
867
Ingo Molnar4d732132015-06-08 20:43:07 +0200868 call \do_sym
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700869
Andy Lutomirski577ed452014-05-21 15:07:09 -0700870 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200871 addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700872 .endif
873
Denys Vlasenkoebfc4532015-02-26 14:40:34 -0800874 /* these procedures expect "no swapgs" flag in ebx */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700875 .if \paranoid
Ingo Molnar4d732132015-06-08 20:43:07 +0200876 jmp paranoid_exit
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700877 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200878 jmp error_exit
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700879 .endif
880
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800881 .if \paranoid == 1
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800882 /*
883 * Paranoid entry from userspace. Switch stacks and treat it
884 * as a normal entry. This means that paranoid handlers
885 * run in real process context if user_mode(regs).
886 */
8871:
Ingo Molnar4d732132015-06-08 20:43:07 +0200888 call error_entry
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800889
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800890
Ingo Molnar4d732132015-06-08 20:43:07 +0200891 movq %rsp, %rdi /* pt_regs pointer */
892 call sync_regs
893 movq %rax, %rsp /* switch stack */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800894
Ingo Molnar4d732132015-06-08 20:43:07 +0200895 movq %rsp, %rdi /* pt_regs pointer */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800896
897 .if \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200898 movq ORIG_RAX(%rsp), %rsi /* get error code */
899 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800900 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200901 xorl %esi, %esi /* no error code */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800902 .endif
903
Ingo Molnar4d732132015-06-08 20:43:07 +0200904 call \do_sym
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800905
Ingo Molnar4d732132015-06-08 20:43:07 +0200906 jmp error_exit /* %ebx: no swapgs flag */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800907 .endif
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +0100908END(\sym)
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100909.endm
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100910
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400911#ifdef CONFIG_TRACING
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700912.macro trace_idtentry sym do_sym has_error_code:req
913idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
914idtentry \sym \do_sym has_error_code=\has_error_code
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400915.endm
916#else
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700917.macro trace_idtentry sym do_sym has_error_code:req
918idtentry \sym \do_sym has_error_code=\has_error_code
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400919.endm
920#endif
921
Ingo Molnar4d732132015-06-08 20:43:07 +0200922idtentry divide_error do_divide_error has_error_code=0
923idtentry overflow do_overflow has_error_code=0
924idtentry bounds do_bounds has_error_code=0
925idtentry invalid_op do_invalid_op has_error_code=0
926idtentry device_not_available do_device_not_available has_error_code=0
927idtentry double_fault do_double_fault has_error_code=1 paranoid=2
928idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
929idtentry invalid_TSS do_invalid_TSS has_error_code=1
930idtentry segment_not_present do_segment_not_present has_error_code=1
931idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
932idtentry coprocessor_error do_coprocessor_error has_error_code=0
933idtentry alignment_check do_alignment_check has_error_code=1
934idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400935
Ingo Molnar2601e642006-07-03 00:24:45 -0700936
Ingo Molnar4d732132015-06-08 20:43:07 +0200937 /*
938 * Reload gs selector with exception handling
939 * edi: new selector
940 */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -0400941ENTRY(native_load_gs_index)
Ingo Molnar131484c2015-05-28 12:21:47 +0200942 pushfq
Jeremy Fitzhardingeb8aa2872009-01-28 14:35:03 -0800943 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300944 SWAPGS
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100945gs_change:
Ingo Molnar4d732132015-06-08 20:43:07 +0200946 movl %edi, %gs
9472: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100948 SWAPGS
Ingo Molnar131484c2015-05-28 12:21:47 +0200949 popfq
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300950 ret
Alexander van Heukelum6efdcfa2008-11-23 10:15:32 +0100951END(native_load_gs_index)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100952
Ingo Molnar4d732132015-06-08 20:43:07 +0200953 _ASM_EXTABLE(gs_change, bad_gs)
954 .section .fixup, "ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* running with kernelgs */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100956bad_gs:
Ingo Molnar4d732132015-06-08 20:43:07 +0200957 SWAPGS /* switch back to user gs */
958 xorl %eax, %eax
959 movl %eax, %gs
960 jmp 2b
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300961 .previous
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100962
Andi Kleen26995002006-08-02 22:37:28 +0200963/* Call softirq on interrupt stack. Interrupts are off. */
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200964ENTRY(do_softirq_own_stack)
Ingo Molnar4d732132015-06-08 20:43:07 +0200965 pushq %rbp
966 mov %rsp, %rbp
967 incl PER_CPU_VAR(irq_count)
968 cmove PER_CPU_VAR(irq_stack_ptr), %rsp
969 push %rbp /* frame pointer backlink */
970 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +0200971 leaveq
Ingo Molnar4d732132015-06-08 20:43:07 +0200972 decl PER_CPU_VAR(irq_count)
Andi Kleened6b6762005-07-28 21:15:49 -0700973 ret
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200974END(do_softirq_own_stack)
Andi Kleen75154f42007-06-23 02:29:25 +0200975
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700976#ifdef CONFIG_XEN
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700977idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700978
979/*
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300980 * A note on the "critical region" in our callback handler.
981 * We want to avoid stacking callback handlers due to events occurring
982 * during handling of the last event. To do this, we keep events disabled
983 * until we've done all processing. HOWEVER, we must enable events before
984 * popping the stack frame (can't be done atomically) and so it would still
985 * be possible to get enough handler activations to overflow the stack.
986 * Although unlikely, bugs of that kind are hard to track down, so we'd
987 * like to avoid the possibility.
988 * So, on entry to the handler we detect whether we interrupted an
989 * existing activation in its critical region -- if so, we pop the current
990 * activation and restart the handler using the previous one.
991 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200992ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */
993
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300994/*
995 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
996 * see the correct pointer to the pt_regs
997 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200998 movq %rdi, %rsp /* we don't return, adjust the stack frame */
99911: incl PER_CPU_VAR(irq_count)
1000 movq %rsp, %rbp
1001 cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
1002 pushq %rbp /* frame pointer backlink */
1003 call xen_evtchn_do_upcall
1004 popq %rsp
1005 decl PER_CPU_VAR(irq_count)
David Vrabelfdfd8112015-02-19 15:23:17 +00001006#ifndef CONFIG_PREEMPT
Ingo Molnar4d732132015-06-08 20:43:07 +02001007 call xen_maybe_preempt_hcall
David Vrabelfdfd8112015-02-19 15:23:17 +00001008#endif
Ingo Molnar4d732132015-06-08 20:43:07 +02001009 jmp error_exit
Alexander van Heukelum371c3942011-03-11 21:59:38 +01001010END(xen_do_hypervisor_callback)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001011
1012/*
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001013 * Hypervisor uses this for application faults while it executes.
1014 * We get here for two reasons:
1015 * 1. Fault while reloading DS, ES, FS or GS
1016 * 2. Fault while executing IRET
1017 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1018 * registers that could be reloaded and zeroed the others.
1019 * Category 2 we fix up by killing the current process. We cannot use the
1020 * normal Linux return path in this case because if we use the IRET hypercall
1021 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1022 * We distinguish between categories by comparing each saved segment register
1023 * with its current contents: any discrepancy means we in category 1.
1024 */
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001025ENTRY(xen_failsafe_callback)
Ingo Molnar4d732132015-06-08 20:43:07 +02001026 movl %ds, %ecx
1027 cmpw %cx, 0x10(%rsp)
1028 jne 1f
1029 movl %es, %ecx
1030 cmpw %cx, 0x18(%rsp)
1031 jne 1f
1032 movl %fs, %ecx
1033 cmpw %cx, 0x20(%rsp)
1034 jne 1f
1035 movl %gs, %ecx
1036 cmpw %cx, 0x28(%rsp)
1037 jne 1f
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001038 /* All segments match their saved values => Category 2 (Bad IRET). */
Ingo Molnar4d732132015-06-08 20:43:07 +02001039 movq (%rsp), %rcx
1040 movq 8(%rsp), %r11
1041 addq $0x30, %rsp
1042 pushq $0 /* RIP */
1043 pushq %r11
1044 pushq %rcx
1045 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -070010461: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
Ingo Molnar4d732132015-06-08 20:43:07 +02001047 movq (%rsp), %rcx
1048 movq 8(%rsp), %r11
1049 addq $0x30, %rsp
1050 pushq $-1 /* orig_ax = -1 => not a system call */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001051 ALLOC_PT_GPREGS_ON_STACK
1052 SAVE_C_REGS
1053 SAVE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +02001054 jmp error_exit
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001055END(xen_failsafe_callback)
1056
Seiji Aguchicf910e82013-06-20 11:46:53 -04001057apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
Sheng Yang38e20b02010-05-14 12:40:51 +01001058 xen_hvm_callback_vector xen_evtchn_do_upcall
1059
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001060#endif /* CONFIG_XEN */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001061
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001062#if IS_ENABLED(CONFIG_HYPERV)
Seiji Aguchicf910e82013-06-20 11:46:53 -04001063apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001064 hyperv_callback_vector hyperv_vector_handler
1065#endif /* CONFIG_HYPERV */
1066
Ingo Molnar4d732132015-06-08 20:43:07 +02001067idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1068idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1069idtentry stack_segment do_stack_segment has_error_code=1
1070
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -07001071#ifdef CONFIG_XEN
Ingo Molnar4d732132015-06-08 20:43:07 +02001072idtentry xen_debug do_debug has_error_code=0
1073idtentry xen_int3 do_int3 has_error_code=0
1074idtentry xen_stack_segment do_stack_segment has_error_code=1
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -07001075#endif
Ingo Molnar4d732132015-06-08 20:43:07 +02001076
1077idtentry general_protection do_general_protection has_error_code=1
1078trace_idtentry page_fault do_page_fault has_error_code=1
1079
Gleb Natapov631bc482010-10-14 11:22:52 +02001080#ifdef CONFIG_KVM_GUEST
Ingo Molnar4d732132015-06-08 20:43:07 +02001081idtentry async_page_fault do_async_page_fault has_error_code=1
Gleb Natapov631bc482010-10-14 11:22:52 +02001082#endif
Ingo Molnar4d732132015-06-08 20:43:07 +02001083
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001084#ifdef CONFIG_X86_MCE
Ingo Molnar4d732132015-06-08 20:43:07 +02001085idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001086#endif
1087
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001088/*
1089 * Save all registers in pt_regs, and switch gs if needed.
1090 * Use slow, but surefire "are we in kernel?" check.
1091 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1092 */
1093ENTRY(paranoid_entry)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001094 cld
1095 SAVE_C_REGS 8
1096 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +02001097 movl $1, %ebx
1098 movl $MSR_GS_BASE, %ecx
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001099 rdmsr
Ingo Molnar4d732132015-06-08 20:43:07 +02001100 testl %edx, %edx
1101 js 1f /* negative -> in kernel */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001102 SWAPGS
Ingo Molnar4d732132015-06-08 20:43:07 +02001103 xorl %ebx, %ebx
Denys Vlasenko1eeb2072015-02-26 14:40:33 -080011041: ret
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001105END(paranoid_entry)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001106
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001107/*
1108 * "Paranoid" exit path from exception stack. This is invoked
1109 * only on return from non-NMI IST interrupts that came
1110 * from kernel space.
1111 *
1112 * We may be returning to very strange contexts (e.g. very early
1113 * in syscall entry), so checking for preemption here would
1114 * be complicated. Fortunately, we there's no good reason
1115 * to try to handle preemption here.
Ingo Molnar4d732132015-06-08 20:43:07 +02001116 *
1117 * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001118 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001119ENTRY(paranoid_exit)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001120 DISABLE_INTERRUPTS(CLBR_NONE)
Steven Rostedt5963e312012-05-30 11:54:53 -04001121 TRACE_IRQS_OFF_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +02001122 testl %ebx, %ebx /* swapgs needed? */
1123 jnz paranoid_exit_no_swapgs
Denys Vlasenkof2db9382015-02-26 14:40:30 -08001124 TRACE_IRQS_IRETQ
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001125 SWAPGS_UNSAFE_STACK
Ingo Molnar4d732132015-06-08 20:43:07 +02001126 jmp paranoid_exit_restore
Denys Vlasenko0d550832015-02-26 14:40:29 -08001127paranoid_exit_no_swapgs:
Denys Vlasenkof2db9382015-02-26 14:40:30 -08001128 TRACE_IRQS_IRETQ_DEBUG
Denys Vlasenko0d550832015-02-26 14:40:29 -08001129paranoid_exit_restore:
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001130 RESTORE_EXTRA_REGS
1131 RESTORE_C_REGS
1132 REMOVE_PT_GPREGS_FROM_STACK 8
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001133 INTERRUPT_RETURN
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001134END(paranoid_exit)
1135
1136/*
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001137 * Save all registers in pt_regs, and switch gs if needed.
Andy Lutomirski539f5112015-06-09 12:36:01 -07001138 * Return: EBX=0: came from user mode; EBX=1: otherwise
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001139 */
1140ENTRY(error_entry)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001141 cld
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001142 SAVE_C_REGS 8
1143 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +02001144 xorl %ebx, %ebx
Denys Vlasenko03335e92015-04-27 15:21:52 +02001145 testb $3, CS+8(%rsp)
Denys Vlasenkodde74f22015-04-27 15:21:51 +02001146 jz error_kernelspace
Andy Lutomirski539f5112015-06-09 12:36:01 -07001147
1148 /* We entered from user mode */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001149 SWAPGS
Andy Lutomirski539f5112015-06-09 12:36:01 -07001150
1151error_entry_done:
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001152 TRACE_IRQS_OFF
1153 ret
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001154
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001155 /*
1156 * There are two places in the kernel that can potentially fault with
1157 * usergs. Handle them here. B stepping K8s sometimes report a
1158 * truncated RIP for IRET exceptions returning to compat mode. Check
1159 * for these here too.
1160 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001161error_kernelspace:
Ingo Molnar4d732132015-06-08 20:43:07 +02001162 incl %ebx
1163 leaq native_irq_return_iret(%rip), %rcx
1164 cmpq %rcx, RIP+8(%rsp)
1165 je error_bad_iret
1166 movl %ecx, %eax /* zero extend */
1167 cmpq %rax, RIP+8(%rsp)
1168 je bstep_iret
1169 cmpq $gs_change, RIP+8(%rsp)
Andy Lutomirski539f5112015-06-09 12:36:01 -07001170 jne error_entry_done
1171
1172 /*
1173 * hack: gs_change can fail with user gsbase. If this happens, fix up
1174 * gsbase and proceed. We'll fix up the exception and land in
1175 * gs_change's error handler with kernel gsbase.
1176 */
1177 SWAPGS
1178 jmp error_entry_done
Brian Gerstae24ffe2009-10-12 10:18:23 -04001179
1180bstep_iret:
1181 /* Fix truncated RIP */
Ingo Molnar4d732132015-06-08 20:43:07 +02001182 movq %rcx, RIP+8(%rsp)
Andy Lutomirskib645af22014-11-22 18:00:33 -08001183 /* fall through */
1184
1185error_bad_iret:
Andy Lutomirski539f5112015-06-09 12:36:01 -07001186 /*
1187 * We came from an IRET to user mode, so we have user gsbase.
1188 * Switch to kernel gsbase:
1189 */
Andy Lutomirskib645af22014-11-22 18:00:33 -08001190 SWAPGS
Andy Lutomirski539f5112015-06-09 12:36:01 -07001191
1192 /*
1193 * Pretend that the exception came from user mode: set up pt_regs
1194 * as if we faulted immediately after IRET and clear EBX so that
1195 * error_exit knows that we will be returning to user mode.
1196 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001197 mov %rsp, %rdi
1198 call fixup_bad_iret
1199 mov %rax, %rsp
Andy Lutomirski539f5112015-06-09 12:36:01 -07001200 decl %ebx
1201 jmp error_entry_done
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001202END(error_entry)
1203
1204
Andy Lutomirski539f5112015-06-09 12:36:01 -07001205/*
1206 * On entry, EBS is a "return to kernel mode" flag:
1207 * 1: already in kernel mode, don't need SWAPGS
1208 * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
1209 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001210ENTRY(error_exit)
Ingo Molnar4d732132015-06-08 20:43:07 +02001211 movl %ebx, %eax
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001212 RESTORE_EXTRA_REGS
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001213 DISABLE_INTERRUPTS(CLBR_NONE)
1214 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +02001215 testl %eax, %eax
1216 jnz retint_kernel
1217 jmp retint_user
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001218END(error_exit)
1219
Denys Vlasenko0784b362015-04-01 16:50:57 +02001220/* Runs on exception stack */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001221ENTRY(nmi)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001222 PARAVIRT_ADJUST_EXCEPTION_FRAME
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001223 /*
1224 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1225 * the iretq it performs will take us out of NMI context.
1226 * This means that we can have nested NMIs where the next
1227 * NMI is using the top of the stack of the previous NMI. We
1228 * can't let it execute because the nested NMI will corrupt the
1229 * stack of the previous NMI. NMI handlers are not re-entrant
1230 * anyway.
1231 *
1232 * To handle this case we do the following:
1233 * Check the a special location on the stack that contains
1234 * a variable that is set when NMIs are executing.
1235 * The interrupted task's stack is also checked to see if it
1236 * is an NMI stack.
1237 * If the variable is not set and the stack is not the NMI
1238 * stack then:
1239 * o Set the special variable on the stack
1240 * o Copy the interrupt frame into a "saved" location on the stack
1241 * o Copy the interrupt frame into a "copy" location on the stack
1242 * o Continue processing the NMI
1243 * If the variable is set or the previous stack is the NMI stack:
1244 * o Modify the "copy" location to jump to the repeate_nmi
1245 * o return back to the first NMI
1246 *
1247 * Now on exit of the first NMI, we first clear the stack variable
1248 * The NMI stack will tell any nested NMIs at that point that it is
1249 * nested. Then we pop the stack normally with iret, and if there was
1250 * a nested NMI that updated the copy interrupt stack frame, a
1251 * jump will be made to the repeat_nmi code that will handle the second
1252 * NMI.
1253 */
1254
Denys Vlasenko146b2b02015-03-25 18:18:13 +01001255 /* Use %rdx as our temp variable throughout */
Ingo Molnar4d732132015-06-08 20:43:07 +02001256 pushq %rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001257
1258 /*
Steven Rostedt45d5a162012-02-19 16:43:37 -05001259 * If %cs was not the kernel segment, then the NMI triggered in user
1260 * space, which means it is definitely not nested.
1261 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001262 cmpl $__KERNEL_CS, 16(%rsp)
1263 jne first_nmi
Steven Rostedt45d5a162012-02-19 16:43:37 -05001264
1265 /*
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001266 * Check the special variable on the stack to see if NMIs are
1267 * executing.
1268 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001269 cmpl $1, -8(%rsp)
1270 je nested_nmi
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001271
1272 /*
1273 * Now test if the previous stack was an NMI stack.
1274 * We need the double check. We check the NMI stack to satisfy the
1275 * race when the first NMI clears the variable before returning.
1276 * We check the variable because the first NMI could be in a
1277 * breakpoint routine using a breakpoint stack.
1278 */
Denys Vlasenko0784b362015-04-01 16:50:57 +02001279 lea 6*8(%rsp), %rdx
1280 /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1281 cmpq %rdx, 4*8(%rsp)
1282 /* If the stack pointer is above the NMI stack, this is a normal NMI */
1283 ja first_nmi
Ingo Molnar4d732132015-06-08 20:43:07 +02001284
Denys Vlasenko0784b362015-04-01 16:50:57 +02001285 subq $EXCEPTION_STKSZ, %rdx
1286 cmpq %rdx, 4*8(%rsp)
1287 /* If it is below the NMI stack, it is a normal NMI */
1288 jb first_nmi
1289 /* Ah, it is within the NMI stack, treat it as nested */
Denys Vlasenko0784b362015-04-01 16:50:57 +02001290
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001291nested_nmi:
1292 /*
1293 * Do nothing if we interrupted the fixup in repeat_nmi.
1294 * It's about to repeat the NMI handler, so we are fine
1295 * with ignoring this one.
1296 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001297 movq $repeat_nmi, %rdx
1298 cmpq 8(%rsp), %rdx
1299 ja 1f
1300 movq $end_repeat_nmi, %rdx
1301 cmpq 8(%rsp), %rdx
1302 ja nested_nmi_out
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001303
13041:
1305 /* Set up the interrupted NMIs stack to jump to repeat_nmi */
Ingo Molnar4d732132015-06-08 20:43:07 +02001306 leaq -1*8(%rsp), %rdx
1307 movq %rdx, %rsp
1308 leaq -10*8(%rsp), %rdx
1309 pushq $__KERNEL_DS
1310 pushq %rdx
Ingo Molnar131484c2015-05-28 12:21:47 +02001311 pushfq
Ingo Molnar4d732132015-06-08 20:43:07 +02001312 pushq $__KERNEL_CS
1313 pushq $repeat_nmi
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001314
1315 /* Put stack back */
Ingo Molnar4d732132015-06-08 20:43:07 +02001316 addq $(6*8), %rsp
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001317
1318nested_nmi_out:
Ingo Molnar4d732132015-06-08 20:43:07 +02001319 popq %rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001320
1321 /* No need to check faults here */
1322 INTERRUPT_RETURN
1323
1324first_nmi:
1325 /*
1326 * Because nested NMIs will use the pushed location that we
1327 * stored in rdx, we must keep that space available.
1328 * Here's what our stack frame will look like:
1329 * +-------------------------+
1330 * | original SS |
1331 * | original Return RSP |
1332 * | original RFLAGS |
1333 * | original CS |
1334 * | original RIP |
1335 * +-------------------------+
1336 * | temp storage for rdx |
1337 * +-------------------------+
1338 * | NMI executing variable |
1339 * +-------------------------+
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001340 * | copied SS |
1341 * | copied Return RSP |
1342 * | copied RFLAGS |
1343 * | copied CS |
1344 * | copied RIP |
1345 * +-------------------------+
Salman Qazi28696f42012-10-01 17:29:25 -07001346 * | Saved SS |
1347 * | Saved Return RSP |
1348 * | Saved RFLAGS |
1349 * | Saved CS |
1350 * | Saved RIP |
1351 * +-------------------------+
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001352 * | pt_regs |
1353 * +-------------------------+
1354 *
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001355 * The saved stack frame is used to fix up the copied stack frame
1356 * that a nested NMI may change to make the interrupted NMI iret jump
1357 * to the repeat_nmi. The original stack frame and the temp storage
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001358 * is also used by nested NMIs and can not be trusted on exit.
1359 */
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001360 /* Do not pop rdx, nested NMIs will corrupt that part of the stack */
Ingo Molnar4d732132015-06-08 20:43:07 +02001361 movq (%rsp), %rdx
Jan Beulich62610912012-02-24 14:54:37 +00001362
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001363 /* Set the NMI executing variable on the stack. */
Ingo Molnar4d732132015-06-08 20:43:07 +02001364 pushq $1
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001365
Ingo Molnar4d732132015-06-08 20:43:07 +02001366 /* Leave room for the "copied" frame */
1367 subq $(5*8), %rsp
Salman Qazi28696f42012-10-01 17:29:25 -07001368
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001369 /* Copy the stack frame to the Saved frame */
1370 .rept 5
Ingo Molnar4d732132015-06-08 20:43:07 +02001371 pushq 11*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001372 .endr
Jan Beulich62610912012-02-24 14:54:37 +00001373
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001374 /* Everything up to here is safe from nested NMIs */
1375
Jan Beulich62610912012-02-24 14:54:37 +00001376 /*
1377 * If there was a nested NMI, the first NMI's iret will return
1378 * here. But NMIs are still enabled and we can take another
1379 * nested NMI. The nested NMI checks the interrupted RIP to see
1380 * if it is between repeat_nmi and end_repeat_nmi, and if so
1381 * it will just return, as we are about to repeat an NMI anyway.
1382 * This makes it safe to copy to the stack frame that a nested
1383 * NMI will update.
1384 */
1385repeat_nmi:
1386 /*
1387 * Update the stack variable to say we are still in NMI (the update
1388 * is benign for the non-repeat case, where 1 was pushed just above
1389 * to this very stack slot).
1390 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001391 movq $1, 10*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001392
1393 /* Make another copy, this one may be modified by nested NMIs */
Ingo Molnar4d732132015-06-08 20:43:07 +02001394 addq $(10*8), %rsp
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001395 .rept 5
Ingo Molnar4d732132015-06-08 20:43:07 +02001396 pushq -6*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001397 .endr
Ingo Molnar4d732132015-06-08 20:43:07 +02001398 subq $(5*8), %rsp
Jan Beulich62610912012-02-24 14:54:37 +00001399end_repeat_nmi:
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001400
1401 /*
1402 * Everything below this point can be preempted by a nested
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001403 * NMI if the first NMI took an exception and reset our iret stack
1404 * so that we repeat another NMI.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001405 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001406 pushq $-1 /* ORIG_RAX: no syscall to restart */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001407 ALLOC_PT_GPREGS_ON_STACK
1408
Steven Rostedt1fd466e2011-12-08 12:32:27 -05001409 /*
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001410 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
Steven Rostedt1fd466e2011-12-08 12:32:27 -05001411 * as we should not be calling schedule in NMI context.
1412 * Even with normal interrupts enabled. An NMI should not be
1413 * setting NEED_RESCHED or anything that normal interrupts and
1414 * exceptions might do.
1415 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001416 call paranoid_entry
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001417
1418 /*
1419 * Save off the CR2 register. If we take a page fault in the NMI then
1420 * it could corrupt the CR2 value. If the NMI preempts a page fault
1421 * handler before it was able to read the CR2 register, and then the
1422 * NMI itself takes a page fault, the page fault that was preempted
1423 * will read the information from the NMI page fault and not the
1424 * origin fault. Save it off and restore it if it changes.
1425 * Use the r12 callee-saved register.
1426 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001427 movq %cr2, %r12
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001428
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001429 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
Ingo Molnar4d732132015-06-08 20:43:07 +02001430 movq %rsp, %rdi
1431 movq $-1, %rsi
1432 call do_nmi
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001433
1434 /* Did the NMI take a page fault? Restore cr2 if it did */
Ingo Molnar4d732132015-06-08 20:43:07 +02001435 movq %cr2, %rcx
1436 cmpq %rcx, %r12
1437 je 1f
1438 movq %r12, %cr2
Steven Rostedt7fbb98c2012-06-07 10:21:21 -040014391:
Ingo Molnar4d732132015-06-08 20:43:07 +02001440 testl %ebx, %ebx /* swapgs needed? */
1441 jnz nmi_restore
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001442nmi_swapgs:
1443 SWAPGS_UNSAFE_STACK
1444nmi_restore:
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001445 RESTORE_EXTRA_REGS
1446 RESTORE_C_REGS
Jan Beulich444723d2013-01-24 09:27:31 +00001447 /* Pop the extra iret frame at once */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001448 REMOVE_PT_GPREGS_FROM_STACK 6*8
Salman Qazi28696f42012-10-01 17:29:25 -07001449
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001450 /* Clear the NMI executing stack variable */
Ingo Molnar4d732132015-06-08 20:43:07 +02001451 movq $0, 5*8(%rsp)
Andy Lutomirski5ca6f702015-06-04 13:24:29 -07001452 INTERRUPT_RETURN
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001453END(nmi)
1454
1455ENTRY(ignore_sysret)
Ingo Molnar4d732132015-06-08 20:43:07 +02001456 mov $-ENOSYS, %eax
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001457 sysret
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001458END(ignore_sysret)