blob: 2babb393915e76dbeb8a1b757305b819fddd324b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
10 * entry.S contains the system-call and fault low-level handling routines.
11 *
Andy Lutomirski8b4777a2011-06-05 13:50:18 -040012 * Some of this is documented in Documentation/x86/entry_64.txt
13 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * NOTE: This code handles signal-recognition, which happens every time
15 * after an interrupt and after each system call.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +010016 *
17 * Normal syscalls and interrupts don't save a full stack frame, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * only done for syscall tracing, signals or fork/exec et.al.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +010019 *
20 * A note on terminology:
21 * - top of stack: Architecture defined interrupt frame from SS to RIP
22 * at the top of the kernel process stack.
Lucas De Marchi0d2eb442011-03-17 16:24:16 -030023 * - partial stack frame: partially saved registers up to R11.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +010024 * - full stack frame: Like partial stack frame, but all register saved.
Andi Kleen2e91a172006-09-26 10:52:29 +020025 *
26 * Some macro usage:
27 * - CFI macros are used to generate dwarf2 unwind information for better
28 * backtraces. They don't change any code.
29 * - SAVE_ALL/RESTORE_ALL - Save/restore all registers
30 * - SAVE_ARGS/RESTORE_ARGS - Save/restore registers that C functions modify.
31 * There are unfortunately lots of special cases where some registers
32 * not touched. The macro is a big mess that should be cleaned up.
33 * - SAVE_REST/RESTORE_REST - Handle the registers not saved by SAVE_ARGS.
34 * Gives a full stack frame.
35 * - ENTRY/END Define functions in the symbol table.
36 * - FIXUP_TOP_OF_STACK/RESTORE_TOP_OF_STACK - Fix up the hardware stack
37 * frame that is otherwise undefined after a SYSCALL
38 * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -070039 * - idtentry - Define exception entry points.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/linkage.h>
43#include <asm/segment.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/cache.h>
45#include <asm/errno.h>
46#include <asm/dwarf2.h>
47#include <asm/calling.h>
Sam Ravnborge2d5df92005-09-09 21:28:48 +020048#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/msr.h>
50#include <asm/unistd.h>
51#include <asm/thread_info.h>
52#include <asm/hw_irq.h>
Jeremy Fitzhardinge0341c142009-02-13 11:14:01 -080053#include <asm/page_types.h>
Ingo Molnar2601e642006-07-03 00:24:45 -070054#include <asm/irqflags.h>
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010055#include <asm/paravirt.h>
Tejun Heo9939dda2009-01-13 20:41:35 +090056#include <asm/percpu.h>
H. Peter Anvind7abc0f2012-04-20 12:19:50 -070057#include <asm/asm.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010058#include <asm/context_tracking.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070059#include <asm/smap.h>
H. Peter Anvin3891a042014-04-29 16:46:09 -070060#include <asm/pgtable_types.h>
Eric Parisd7e75282012-01-03 14:23:06 -050061#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Roland McGrath86a1c342008-06-23 15:37:04 -070063/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
64#include <linux/elf-em.h>
65#define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
66#define __AUDIT_ARCH_64BIT 0x80000000
67#define __AUDIT_ARCH_LE 0x40000000
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .code64
Jiri Olsaea714542011-03-07 19:10:39 +010070 .section .entry.text, "ax"
71
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020072
Andi Kleendc37db42005-04-16 15:25:05 -070073#ifndef CONFIG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define retint_kernel retint_restore_args
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +010075#endif
Ingo Molnar2601e642006-07-03 00:24:45 -070076
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010077#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040078ENTRY(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010079 swapgs
80 sysretq
Cyrill Gorcunovb3baaa12009-02-23 22:57:00 +030081ENDPROC(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010082#endif /* CONFIG_PARAVIRT */
83
Ingo Molnar2601e642006-07-03 00:24:45 -070084
85.macro TRACE_IRQS_IRETQ offset=ARGOFFSET
86#ifdef CONFIG_TRACE_IRQFLAGS
87 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
88 jnc 1f
89 TRACE_IRQS_ON
901:
91#endif
92.endm
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
Steven Rostedt5963e312012-05-30 11:54:53 -040095 * When dynamic function tracer is enabled it will add a breakpoint
96 * to all locations that it is about to modify, sync CPUs, update
97 * all the code, sync CPUs, then remove the breakpoints. In this time
98 * if lockdep is enabled, it might jump back into the debug handler
99 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
100 *
101 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
102 * make sure the stack pointer does not get reset back to the top
103 * of the debug stack, and instead just reuses the current stack.
104 */
105#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
106
107.macro TRACE_IRQS_OFF_DEBUG
108 call debug_stack_set_zero
109 TRACE_IRQS_OFF
110 call debug_stack_reset
111.endm
112
113.macro TRACE_IRQS_ON_DEBUG
114 call debug_stack_set_zero
115 TRACE_IRQS_ON
116 call debug_stack_reset
117.endm
118
119.macro TRACE_IRQS_IRETQ_DEBUG offset=ARGOFFSET
120 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
121 jnc 1f
122 TRACE_IRQS_ON_DEBUG
1231:
124.endm
125
126#else
127# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
128# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
129# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
130#endif
131
132/*
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100133 * C code is not supposed to know about undefined top of stack. Every time
134 * a C function with an pt_regs argument is called from the SYSCALL based
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * fast path FIXUP_TOP_OF_STACK is needed.
136 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
137 * manipulation.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100138 */
139
140 /* %rsp:at FRAMEEND */
Alexander van Heukelumc002a1e2008-11-21 16:41:55 +0100141 .macro FIXUP_TOP_OF_STACK tmp offset=0
Brian Gerst3d1e42a2009-01-19 00:38:58 +0900142 movq PER_CPU_VAR(old_rsp),\tmp
Alexander van Heukelumc002a1e2008-11-21 16:41:55 +0100143 movq \tmp,RSP+\offset(%rsp)
144 movq $__USER_DS,SS+\offset(%rsp)
145 movq $__USER_CS,CS+\offset(%rsp)
Andy Lutomirski0fcedc82015-01-16 16:19:27 -0800146 movq RIP+\offset(%rsp),\tmp /* get rip */
147 movq \tmp,RCX+\offset(%rsp) /* copy it to rcx as sysret would do */
Alexander van Heukelumc002a1e2008-11-21 16:41:55 +0100148 movq R11+\offset(%rsp),\tmp /* get eflags */
149 movq \tmp,EFLAGS+\offset(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .endm
151
Alexander van Heukelumc002a1e2008-11-21 16:41:55 +0100152 .macro RESTORE_TOP_OF_STACK tmp offset=0
153 movq RSP+\offset(%rsp),\tmp
Brian Gerst3d1e42a2009-01-19 00:38:58 +0900154 movq \tmp,PER_CPU_VAR(old_rsp)
Alexander van Heukelumc002a1e2008-11-21 16:41:55 +0100155 movq EFLAGS+\offset(%rsp),\tmp
156 movq \tmp,R11+\offset(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .endm
158
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100159/*
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100160 * initial frame state for interrupts (and exceptions without error code)
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100161 */
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100162 .macro EMPTY_FRAME start=1 offset=0
163 .if \start
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100164 CFI_STARTPROC simple
165 CFI_SIGNAL_FRAME
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100166 CFI_DEF_CFA rsp,8+\offset
167 .else
168 CFI_DEF_CFA_OFFSET 8+\offset
169 .endif
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100170 .endm
171
172/*
173 * initial frame state for interrupts (and exceptions without error code)
174 */
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100175 .macro INTR_FRAME start=1 offset=0
Ingo Molnare8a0e272008-11-21 15:11:32 +0100176 EMPTY_FRAME \start, SS+8+\offset-RIP
177 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
178 CFI_REL_OFFSET rsp, RSP+\offset-RIP
179 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
180 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
181 CFI_REL_OFFSET rip, RIP+\offset-RIP
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100182 .endm
183
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100184/*
185 * initial frame state for exceptions with error code (and interrupts
186 * with vector already pushed)
187 */
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100188 .macro XCPT_FRAME start=1 offset=0
Ingo Molnare8a0e272008-11-21 15:11:32 +0100189 INTR_FRAME \start, RIP+\offset-ORIG_RAX
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100190 .endm
191
192/*
193 * frame that enables calling into C.
194 */
195 .macro PARTIAL_FRAME start=1 offset=0
Ingo Molnare8a0e272008-11-21 15:11:32 +0100196 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
197 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
198 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
199 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
200 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
201 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
202 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
203 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
204 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
205 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100206 .endm
207
208/*
209 * frame that enables passing a complete pt_regs to a C function.
210 */
211 .macro DEFAULT_FRAME start=1 offset=0
Ingo Molnare8a0e272008-11-21 15:11:32 +0100212 PARTIAL_FRAME \start, R11+\offset-R15
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100213 CFI_REL_OFFSET rbx, RBX+\offset
214 CFI_REL_OFFSET rbp, RBP+\offset
215 CFI_REL_OFFSET r12, R12+\offset
216 CFI_REL_OFFSET r13, R13+\offset
217 CFI_REL_OFFSET r14, R14+\offset
218 CFI_REL_OFFSET r15, R15+\offset
219 .endm
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100220
Alexander van Heukelume2f6bc22008-11-21 16:43:18 +0100221ENTRY(save_paranoid)
222 XCPT_FRAME 1 RDI+8
223 cld
Jan Beulich3bab13b2014-06-25 14:11:22 +0100224 movq %rdi, RDI+8(%rsp)
225 movq %rsi, RSI+8(%rsp)
Alexander van Heukelume2f6bc22008-11-21 16:43:18 +0100226 movq_cfi rdx, RDX+8
227 movq_cfi rcx, RCX+8
228 movq_cfi rax, RAX+8
Jan Beulich3bab13b2014-06-25 14:11:22 +0100229 movq %r8, R8+8(%rsp)
230 movq %r9, R9+8(%rsp)
231 movq %r10, R10+8(%rsp)
232 movq %r11, R11+8(%rsp)
Alexander van Heukelume2f6bc22008-11-21 16:43:18 +0100233 movq_cfi rbx, RBX+8
Jan Beulich3bab13b2014-06-25 14:11:22 +0100234 movq %rbp, RBP+8(%rsp)
235 movq %r12, R12+8(%rsp)
236 movq %r13, R13+8(%rsp)
237 movq %r14, R14+8(%rsp)
238 movq %r15, R15+8(%rsp)
Alexander van Heukelume2f6bc22008-11-21 16:43:18 +0100239 movl $1,%ebx
240 movl $MSR_GS_BASE,%ecx
241 rdmsr
242 testl %edx,%edx
243 js 1f /* negative -> in kernel */
244 SWAPGS
245 xorl %ebx,%ebx
2461: ret
247 CFI_ENDPROC
248END(save_paranoid)
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/*
Ingo Molnar5b3eec02008-11-27 14:41:21 +0100251 * A newly forked process directly context switches into this address.
252 *
253 * rdi: prev task we switched from
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255ENTRY(ret_from_fork)
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100256 DEFAULT_FRAME
Ingo Molnar5b3eec02008-11-27 14:41:21 +0100257
Benjamin LaHaise7106a5a2009-01-10 23:00:22 -0500258 LOCK ; btr $TIF_FORK,TI_flags(%r8)
259
Ian Campbell6eebdda2012-08-24 23:58:47 +0400260 pushq_cfi $0x0002
Jan Beulichdf5d1872010-09-02 14:07:16 +0100261 popfq_cfi # reset kernel eflags
Ingo Molnar5b3eec02008-11-27 14:41:21 +0100262
263 call schedule_tail # rdi: 'prev' task parameter
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 GET_THREAD_INFO(%rcx)
Ingo Molnar5b3eec02008-11-27 14:41:21 +0100266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 RESTORE_REST
Ingo Molnar5b3eec02008-11-27 14:41:21 +0100268
269 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
Al Viro7076aad2012-09-10 16:44:54 -0400270 jz 1f
Ingo Molnar5b3eec02008-11-27 14:41:21 +0100271
Andy Lutomirski956421f2015-03-05 01:09:44 +0100272 /*
273 * By the time we get here, we have no idea whether our pt_regs,
274 * ti flags, and ti status came from the 64-bit SYSCALL fast path,
275 * the slow path, or one of the ia32entry paths.
276 * Use int_ret_from_sys_call to return, since it can safely handle
277 * all of the above.
278 */
279 jmp int_ret_from_sys_call
Ingo Molnar5b3eec02008-11-27 14:41:21 +0100280
Al Viro7076aad2012-09-10 16:44:54 -04002811:
Al Viro22e2430d2012-10-10 21:35:42 -0400282 subq $REST_SKIP, %rsp # leave space for volatiles
Al Viro7076aad2012-09-10 16:44:54 -0400283 CFI_ADJUST_CFA_OFFSET REST_SKIP
284 movq %rbp, %rdi
285 call *%rbx
Al Viro22e2430d2012-10-10 21:35:42 -0400286 movl $0, RAX(%rsp)
287 RESTORE_REST
288 jmp int_ret_from_sys_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200290END(ret_from_fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/*
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300293 * System call entry. Up to 6 arguments in registers are supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 *
295 * SYSCALL does not save anything on the stack and does not change the
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700296 * stack pointer. However, it does mask the flags register for us, so
297 * CLD and CLAC are not needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100301 * Register setup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * rax system call number
303 * rdi arg0
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100304 * rcx return address for syscall/sysret, C arg3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * rsi arg1
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100306 * rdx arg2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * r10 arg3 (--> moved to rcx for C)
308 * r8 arg4
309 * r9 arg5
310 * r11 eflags for syscall/sysret, temporary for C
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100311 * r12-r15,rbp,rbx saved by C code, not touched.
312 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * Interrupts are off on entry.
314 * Only called from user space.
315 *
316 * XXX if we had a free scratch register we could save the RSP into the stack frame
317 * and report it properly in ps. Unfortunately we haven't.
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200318 *
319 * When user can change the frames always force IRET. That is because
320 * it deals with uncanonical addresses better. SYSRET has trouble
321 * with them due to bugs in both AMD and Intel CPUs.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324ENTRY(system_call)
Jan Beulich7effaa82005-09-12 18:49:24 +0200325 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200326 CFI_SIGNAL_FRAME
Brian Gerst9af45652009-01-19 00:38:58 +0900327 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
Jan Beulich7effaa82005-09-12 18:49:24 +0200328 CFI_REGISTER rip,rcx
329 /*CFI_REGISTER rflags,r11*/
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100330 SWAPGS_UNSAFE_STACK
331 /*
332 * A hypervisor implementation might want to use a label
333 * after the swapgs, so that it can do the swapgs
334 * for the guest and jump here on syscall.
335 */
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000336GLOBAL(system_call_after_swapgs)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100337
Brian Gerst3d1e42a2009-01-19 00:38:58 +0900338 movq %rsp,PER_CPU_VAR(old_rsp)
Brian Gerst9af45652009-01-19 00:38:58 +0900339 movq PER_CPU_VAR(kernel_stack),%rsp
Ingo Molnar2601e642006-07-03 00:24:45 -0700340 /*
341 * No need to follow this irqs off/on section - it's straight
342 * and short:
343 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100344 ENABLE_INTERRUPTS(CLBR_NONE)
Andy Lutomirski54eea992014-09-05 15:13:55 -0700345 SAVE_ARGS 8, 0, rax_enosys=1
346 movq_cfi rax,(ORIG_RAX-ARGOFFSET)
Jan Beulich7effaa82005-09-12 18:49:24 +0200347 movq %rcx,RIP-ARGOFFSET(%rsp)
348 CFI_REL_OFFSET rip,RIP-ARGOFFSET
Jan Beulich46db09d2011-11-29 11:17:45 +0000349 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 jnz tracesys
Roland McGrath86a1c342008-06-23 15:37:04 -0700351system_call_fastpath:
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800352#if __SYSCALL_MASK == ~0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 cmpq $__NR_syscall_max,%rax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800354#else
355 andl $__SYSCALL_MASK,%eax
356 cmpl $__NR_syscall_max,%eax
357#endif
Andy Lutomirski54eea992014-09-05 15:13:55 -0700358 ja ret_from_sys_call /* and return regs->ax */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 movq %r10,%rcx
360 call *sys_call_table(,%rax,8) # XXX: rip relative
361 movq %rax,RAX-ARGOFFSET(%rsp)
362/*
363 * Syscall return path ending with SYSRET (fast path)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100364 * Has incomplete stack frame and undefined top of stack.
365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366ret_from_sys_call:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200367 LOCKDEP_SYS_EXIT
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100368 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700369 TRACE_IRQS_OFF
Andy Lutomirskib3494a42015-03-23 12:32:54 -0700370
371 /*
372 * We must check ti flags with interrupts (or at least preemption)
373 * off because we must *never* return to userspace without
374 * processing exit work that is enqueued if we're preempted here.
375 * In particular, returning to userspace with any of the one-shot
376 * flags (TIF_NOTIFY_RESUME, TIF_USER_RETURN_NOTIFY, etc) set is
377 * very bad.
378 */
379 testl $_TIF_ALLWORK_MASK,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
380 jnz int_ret_from_sys_call_fixup /* Go the the slow path */
381
Jan Beulichbcddc012006-12-07 02:14:02 +0100382 CFI_REMEMBER_STATE
Ingo Molnar2601e642006-07-03 00:24:45 -0700383 /*
384 * sysretq will re-enable interrupts:
385 */
386 TRACE_IRQS_ON
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 movq RIP-ARGOFFSET(%rsp),%rcx
Jan Beulich7effaa82005-09-12 18:49:24 +0200388 CFI_REGISTER rip,rcx
Borislav Petkov838feb42011-05-31 22:21:53 +0200389 RESTORE_ARGS 1,-ARG_SKIP,0
Jan Beulich7effaa82005-09-12 18:49:24 +0200390 /*CFI_REGISTER rflags,r11*/
Brian Gerst3d1e42a2009-01-19 00:38:58 +0900391 movq PER_CPU_VAR(old_rsp), %rsp
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400392 USERGS_SYSRET64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jan Beulichbcddc012006-12-07 02:14:02 +0100394 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Andy Lutomirski96b63522014-07-07 11:37:17 -0700396int_ret_from_sys_call_fixup:
Roland McGrathb60e7142009-09-22 16:46:34 -0700397 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
Andy Lutomirskib3494a42015-03-23 12:32:54 -0700398 jmp int_ret_from_sys_call_irqs_off
Roland McGrath86a1c342008-06-23 15:37:04 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* Do syscall tracing */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100401tracesys:
Andy Lutomirski1dcf74f2014-09-05 15:13:56 -0700402 leaq -REST_SKIP(%rsp), %rdi
403 movq $AUDIT_ARCH_X86_64, %rsi
404 call syscall_trace_enter_phase1
405 test %rax, %rax
406 jnz tracesys_phase2 /* if needed, run the slow path */
407 LOAD_ARGS 0 /* else restore clobbered regs */
408 jmp system_call_fastpath /* and return to the fast path */
409
410tracesys_phase2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 SAVE_REST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 FIXUP_TOP_OF_STACK %rdi
Andy Lutomirski1dcf74f2014-09-05 15:13:56 -0700413 movq %rsp, %rdi
414 movq $AUDIT_ARCH_X86_64, %rsi
415 movq %rax,%rdx
416 call syscall_trace_enter_phase2
417
Roland McGrathd4d67152008-07-09 02:38:07 -0700418 /*
419 * Reload arg registers from stack in case ptrace changed them.
Andy Lutomirski1dcf74f2014-09-05 15:13:56 -0700420 * We don't reload %rax because syscall_trace_entry_phase2() returned
Roland McGrathd4d67152008-07-09 02:38:07 -0700421 * the value it wants us to use in the table lookup.
422 */
423 LOAD_ARGS ARGOFFSET, 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 RESTORE_REST
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800425#if __SYSCALL_MASK == ~0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 cmpq $__NR_syscall_max,%rax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800427#else
428 andl $__SYSCALL_MASK,%eax
429 cmpl $__NR_syscall_max,%eax
430#endif
Andy Lutomirski54eea992014-09-05 15:13:55 -0700431 ja int_ret_from_sys_call /* RAX(%rsp) is already set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 movq %r10,%rcx /* fixup for C */
433 call *sys_call_table(,%rax,8)
Roland McGratha31f8dd2008-03-16 21:59:11 -0700434 movq %rax,RAX-ARGOFFSET(%rsp)
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200435 /* Use IRET because user could have changed frame */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100436
437/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * Syscall return path ending with IRET.
439 * Has correct top of stack, but partial stack frame.
Jan Beulichbcddc012006-12-07 02:14:02 +0100440 */
Cyrill Gorcunovbc8b2b92009-02-23 22:57:01 +0300441GLOBAL(int_ret_from_sys_call)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100442 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700443 TRACE_IRQS_OFF
Andy Lutomirskib3494a42015-03-23 12:32:54 -0700444int_ret_from_sys_call_irqs_off:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 movl $_TIF_ALLWORK_MASK,%edi
446 /* edi: mask to check */
Cyrill Gorcunovbc8b2b92009-02-23 22:57:01 +0300447GLOBAL(int_with_check)
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200448 LOCKDEP_SYS_EXIT_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300450 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 andl %edi,%edx
452 jnz int_careful
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300453 andl $~TS_COMPAT,TI_status(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 jmp retint_swapgs
455
456 /* Either reschedule or signal or syscall exit tracking needed. */
457 /* First do a reschedule test. */
458 /* edx: work, edi: workmask */
459int_careful:
460 bt $TIF_NEED_RESCHED,%edx
461 jnc int_very_careful
Ingo Molnar2601e642006-07-03 00:24:45 -0700462 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100463 ENABLE_INTERRUPTS(CLBR_NONE)
Jan Beulichdf5d1872010-09-02 14:07:16 +0100464 pushq_cfi %rdi
Frederic Weisbecker04304992012-07-11 20:26:38 +0200465 SCHEDULE_USER
Jan Beulichdf5d1872010-09-02 14:07:16 +0100466 popq_cfi %rdi
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100467 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700468 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 jmp int_with_check
470
471 /* handle signals and tracing -- both require a full stack frame */
472int_very_careful:
Ingo Molnar2601e642006-07-03 00:24:45 -0700473 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100474 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrathb60e7142009-09-22 16:46:34 -0700475int_check_syscall_exit_work:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 SAVE_REST
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100477 /* Check for syscall exit trace */
Roland McGrathd4d67152008-07-09 02:38:07 -0700478 testl $_TIF_WORK_SYSCALL_EXIT,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 jz int_signal
Jan Beulichdf5d1872010-09-02 14:07:16 +0100480 pushq_cfi %rdi
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100481 leaq 8(%rsp),%rdi # &ptregs -> arg1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 call syscall_trace_leave
Jan Beulichdf5d1872010-09-02 14:07:16 +0100483 popq_cfi %rdi
Roland McGrathd4d67152008-07-09 02:38:07 -0700484 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 jmp int_restore_rest
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487int_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100488 testl $_TIF_DO_NOTIFY_MASK,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 jz 1f
490 movq %rsp,%rdi # &ptregs -> arg1
491 xorl %esi,%esi # oldset -> arg2
492 call do_notify_resume
Roland McGratheca91e72008-07-10 14:50:39 -07004931: movl $_TIF_WORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494int_restore_rest:
495 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100496 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700497 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 jmp int_with_check
499 CFI_ENDPROC
Jan Beulichbcddc012006-12-07 02:14:02 +0100500END(system_call)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100501
Al Viro1d4b4b22012-10-22 22:34:11 -0400502 .macro FORK_LIKE func
503ENTRY(stub_\func)
504 CFI_STARTPROC
505 popq %r11 /* save return address */
506 PARTIAL_FRAME 0
507 SAVE_REST
508 pushq %r11 /* put it back on stack */
509 FIXUP_TOP_OF_STACK %r11, 8
510 DEFAULT_FRAME 0 8 /* offset 8: return address */
511 call sys_\func
512 RESTORE_TOP_OF_STACK %r11, 8
513 ret $REST_SKIP /* pop extended registers */
514 CFI_ENDPROC
515END(stub_\func)
516 .endm
517
Al Virob3af11a2012-11-19 22:00:52 -0500518 .macro FIXED_FRAME label,func
519ENTRY(\label)
520 CFI_STARTPROC
521 PARTIAL_FRAME 0 8 /* offset 8: return address */
522 FIXUP_TOP_OF_STACK %r11, 8-ARGOFFSET
523 call \func
524 RESTORE_TOP_OF_STACK %r11, 8-ARGOFFSET
525 ret
526 CFI_ENDPROC
527END(\label)
528 .endm
529
Al Viro1d4b4b22012-10-22 22:34:11 -0400530 FORK_LIKE clone
531 FORK_LIKE fork
532 FORK_LIKE vfork
Al Virob3af11a2012-11-19 22:00:52 -0500533 FIXED_FRAME stub_iopl, sys_iopl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535ENTRY(stub_execve)
536 CFI_STARTPROC
Jan Beuliche6b04b62010-09-02 13:52:45 +0100537 addq $8, %rsp
538 PARTIAL_FRAME 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 SAVE_REST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 FIXUP_TOP_OF_STACK %r11
541 call sys_execve
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 movq %rax,RAX(%rsp)
543 RESTORE_REST
544 jmp int_ret_from_sys_call
545 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200546END(stub_execve)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100547
David Drysdale27d6ec72014-12-12 16:57:33 -0800548ENTRY(stub_execveat)
549 CFI_STARTPROC
550 addq $8, %rsp
551 PARTIAL_FRAME 0
552 SAVE_REST
553 FIXUP_TOP_OF_STACK %r11
554 call sys_execveat
555 RESTORE_TOP_OF_STACK %r11
556 movq %rax,RAX(%rsp)
557 RESTORE_REST
558 jmp int_ret_from_sys_call
559 CFI_ENDPROC
560END(stub_execveat)
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/*
563 * sigreturn is special because it needs to restore all registers on return.
564 * This cannot be done with SYSRET, so use the IRET return path instead.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566ENTRY(stub_rt_sigreturn)
567 CFI_STARTPROC
Jan Beulich7effaa82005-09-12 18:49:24 +0200568 addq $8, %rsp
Jan Beuliche6b04b62010-09-02 13:52:45 +0100569 PARTIAL_FRAME 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 SAVE_REST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 FIXUP_TOP_OF_STACK %r11
572 call sys_rt_sigreturn
573 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
574 RESTORE_REST
575 jmp int_ret_from_sys_call
576 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200577END(stub_rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800579#ifdef CONFIG_X86_X32_ABI
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800580ENTRY(stub_x32_rt_sigreturn)
581 CFI_STARTPROC
582 addq $8, %rsp
583 PARTIAL_FRAME 0
584 SAVE_REST
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800585 FIXUP_TOP_OF_STACK %r11
586 call sys32_x32_rt_sigreturn
587 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
588 RESTORE_REST
589 jmp int_ret_from_sys_call
590 CFI_ENDPROC
591END(stub_x32_rt_sigreturn)
592
H. Peter Anvind1a797f2012-02-19 10:06:34 -0800593ENTRY(stub_x32_execve)
594 CFI_STARTPROC
595 addq $8, %rsp
596 PARTIAL_FRAME 0
597 SAVE_REST
598 FIXUP_TOP_OF_STACK %r11
Al Viro6783eaa22012-08-02 23:05:11 +0400599 call compat_sys_execve
H. Peter Anvind1a797f2012-02-19 10:06:34 -0800600 RESTORE_TOP_OF_STACK %r11
601 movq %rax,RAX(%rsp)
602 RESTORE_REST
603 jmp int_ret_from_sys_call
604 CFI_ENDPROC
605END(stub_x32_execve)
606
David Drysdale27d6ec72014-12-12 16:57:33 -0800607ENTRY(stub_x32_execveat)
608 CFI_STARTPROC
609 addq $8, %rsp
610 PARTIAL_FRAME 0
611 SAVE_REST
612 FIXUP_TOP_OF_STACK %r11
613 call compat_sys_execveat
614 RESTORE_TOP_OF_STACK %r11
615 movq %rax,RAX(%rsp)
616 RESTORE_REST
617 jmp int_ret_from_sys_call
618 CFI_ENDPROC
619END(stub_x32_execveat)
620
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800621#endif
622
Jan Beulich7effaa82005-09-12 18:49:24 +0200623/*
H. Peter Anvin939b7872008-11-11 13:51:52 -0800624 * Build the entry stubs and pointer table with some assembler magic.
625 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
626 * single cache line on all modern x86 implementations.
627 */
628 .section .init.rodata,"a"
629ENTRY(interrupt)
Jiri Olsaea714542011-03-07 19:10:39 +0100630 .section .entry.text
H. Peter Anvin939b7872008-11-11 13:51:52 -0800631 .p2align 5
632 .p2align CONFIG_X86_L1_CACHE_SHIFT
633ENTRY(irq_entries_start)
634 INTR_FRAME
635vector=FIRST_EXTERNAL_VECTOR
Jan Beulich2414e022014-11-03 08:39:43 +0000636.rept (FIRST_SYSTEM_VECTOR-FIRST_EXTERNAL_VECTOR+6)/7
H. Peter Anvin939b7872008-11-11 13:51:52 -0800637 .balign 32
638 .rept 7
Jan Beulich2414e022014-11-03 08:39:43 +0000639 .if vector < FIRST_SYSTEM_VECTOR
H. Peter Anvin86655962008-11-12 10:27:35 -0800640 .if vector <> FIRST_EXTERNAL_VECTOR
H. Peter Anvin939b7872008-11-11 13:51:52 -0800641 CFI_ADJUST_CFA_OFFSET -8
642 .endif
Jan Beulichdf5d1872010-09-02 14:07:16 +01006431: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
H. Peter Anvin86655962008-11-12 10:27:35 -0800644 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
H. Peter Anvin939b7872008-11-11 13:51:52 -0800645 jmp 2f
646 .endif
647 .previous
648 .quad 1b
Jiri Olsaea714542011-03-07 19:10:39 +0100649 .section .entry.text
H. Peter Anvin939b7872008-11-11 13:51:52 -0800650vector=vector+1
651 .endif
652 .endr
6532: jmp common_interrupt
654.endr
655 CFI_ENDPROC
656END(irq_entries_start)
657
658.previous
659END(interrupt)
660.previous
661
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100662/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * Interrupt entry/exit.
664 *
665 * Interrupt entry points save only callee clobbered registers in fast path.
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100666 *
667 * Entry runs with interrupts off.
668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100670/* 0(%rsp): ~(interrupt number) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 .macro interrupt func
Frederic Weisbecker625dbc3b2011-01-06 15:22:47 +0100672 /* reserve pt_regs for scratch regs and rbp */
673 subq $ORIG_RAX-RBP, %rsp
674 CFI_ADJUST_CFA_OFFSET ORIG_RAX-RBP
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100675 cld
676 /* start from rbp in pt_regs and jump over */
677 movq_cfi rdi, (RDI-RBP)
678 movq_cfi rsi, (RSI-RBP)
679 movq_cfi rdx, (RDX-RBP)
680 movq_cfi rcx, (RCX-RBP)
681 movq_cfi rax, (RAX-RBP)
682 movq_cfi r8, (R8-RBP)
683 movq_cfi r9, (R9-RBP)
684 movq_cfi r10, (R10-RBP)
685 movq_cfi r11, (R11-RBP)
686
687 /* Save rbp so that we can unwind from get_irq_regs() */
688 movq_cfi rbp, 0
689
690 /* Save previous stack value */
691 movq %rsp, %rsi
692
693 leaq -RBP(%rsp),%rdi /* arg1 for handler */
694 testl $3, CS-RBP(%rsi)
695 je 1f
696 SWAPGS
697 /*
698 * irq_count is used to check if a CPU is already on an interrupt stack
699 * or not. While this is essentially redundant with preempt_count it is
700 * a little cheaper to use a separate counter in the PDA (short of
701 * moving irq_enter into assembly, which would be too much work)
702 */
7031: incl PER_CPU_VAR(irq_count)
704 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
705 CFI_DEF_CFA_REGISTER rsi
706
707 /* Store previous stack value */
708 pushq %rsi
709 CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
710 0x77 /* DW_OP_breg7 */, 0, \
711 0x06 /* DW_OP_deref */, \
712 0x08 /* DW_OP_const1u */, SS+8-RBP, \
713 0x22 /* DW_OP_plus */
714 /* We entered an interrupt context - irqs are off: */
715 TRACE_IRQS_OFF
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 call \func
718 .endm
719
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100720 /*
721 * The interrupt stubs push (~vector+0x80) onto the stack and
722 * then jump to common_interrupt.
723 */
H. Peter Anvin939b7872008-11-11 13:51:52 -0800724 .p2align CONFIG_X86_L1_CACHE_SHIFT
725common_interrupt:
Jan Beulich7effaa82005-09-12 18:49:24 +0200726 XCPT_FRAME
Jan Beulichee4eb872012-11-02 11:18:39 +0000727 ASM_CLAC
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100728 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 interrupt do_IRQ
Brian Gerst3d1e42a2009-01-19 00:38:58 +0900730 /* 0(%rsp): old_rsp-ARGOFFSET */
Jan Beulich7effaa82005-09-12 18:49:24 +0200731ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100732 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700733 TRACE_IRQS_OFF
Brian Gerst56895532009-01-19 00:38:58 +0900734 decl PER_CPU_VAR(irq_count)
Frederic Weisbecker625dbc3b2011-01-06 15:22:47 +0100735
Frederic Weisbeckera2bbe752011-07-02 16:52:45 +0200736 /* Restore saved previous stack */
737 popq %rsi
Mark Wielaard928282e2012-02-24 11:32:05 +0100738 CFI_DEF_CFA rsi,SS+8-RBP /* reg/off reset after def_cfa_expr */
Jan Beulicheab9e612011-09-28 16:57:52 +0100739 leaq ARGOFFSET-RBP(%rsi), %rsp
Jan Beulich7effaa82005-09-12 18:49:24 +0200740 CFI_DEF_CFA_REGISTER rsp
Jan Beulicheab9e612011-09-28 16:57:52 +0100741 CFI_ADJUST_CFA_OFFSET RBP-ARGOFFSET
Frederic Weisbecker625dbc3b2011-01-06 15:22:47 +0100742
Jan Beulich7effaa82005-09-12 18:49:24 +0200743exit_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 GET_THREAD_INFO(%rcx)
745 testl $3,CS-ARGOFFSET(%rsp)
746 je retint_kernel
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Interrupt came from user space */
749 /*
750 * Has a correct top of stack, but a partial stack frame
751 * %rcx: thread info. Interrupts off.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100752 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753retint_with_reschedule:
754 movl $_TIF_WORK_MASK,%edi
Jan Beulich7effaa82005-09-12 18:49:24 +0200755retint_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200756 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300757 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 andl %edi,%edx
Jan Beulich7effaa82005-09-12 18:49:24 +0200759 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200761
762retint_swapgs: /* return to user-space */
Ingo Molnar2601e642006-07-03 00:24:45 -0700763 /*
764 * The iretq could re-enable interrupts:
765 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100766 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700767 TRACE_IRQS_IRETQ
Andy Lutomirski2a23c6b2014-07-22 12:46:50 -0700768
769 /*
770 * Try to use SYSRET instead of IRET if we're returning to
771 * a completely clean 64-bit userspace context.
772 */
773 movq (RCX-R11)(%rsp), %rcx
774 cmpq %rcx,(RIP-R11)(%rsp) /* RCX == RIP */
775 jne opportunistic_sysret_failed
776
777 /*
778 * On Intel CPUs, sysret with non-canonical RCX/RIP will #GP
779 * in kernel space. This essentially lets the user take over
780 * the kernel, since userspace controls RSP. It's not worth
781 * testing for canonicalness exactly -- this check detects any
782 * of the 17 high bits set, which is true for non-canonical
783 * or kernel addresses. (This will pessimize vsyscall=native.
784 * Big deal.)
785 *
786 * If virtual addresses ever become wider, this will need
787 * to be updated to remain correct on both old and new CPUs.
788 */
789 .ifne __VIRTUAL_MASK_SHIFT - 47
790 .error "virtual address width changed -- sysret checks need update"
791 .endif
792 shr $__VIRTUAL_MASK_SHIFT, %rcx
793 jnz opportunistic_sysret_failed
794
795 cmpq $__USER_CS,(CS-R11)(%rsp) /* CS must match SYSRET */
796 jne opportunistic_sysret_failed
797
798 movq (R11-ARGOFFSET)(%rsp), %r11
799 cmpq %r11,(EFLAGS-ARGOFFSET)(%rsp) /* R11 == RFLAGS */
800 jne opportunistic_sysret_failed
801
802 testq $X86_EFLAGS_RF,%r11 /* sysret can't restore RF */
803 jnz opportunistic_sysret_failed
804
805 /* nothing to check for RSP */
806
807 cmpq $__USER_DS,(SS-ARGOFFSET)(%rsp) /* SS must match SYSRET */
808 jne opportunistic_sysret_failed
809
810 /*
811 * We win! This label is here just for ease of understanding
812 * perf profiles. Nothing jumps here.
813 */
814irq_return_via_sysret:
815 CFI_REMEMBER_STATE
816 RESTORE_ARGS 1,8,1
817 movq (RSP-RIP)(%rsp),%rsp
818 USERGS_SYSRET64
819 CFI_RESTORE_STATE
820
821opportunistic_sysret_failed:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100822 SWAPGS
Ingo Molnar2601e642006-07-03 00:24:45 -0700823 jmp restore_args
824
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200825retint_restore_args: /* return to kernel space */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100826 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700827 /*
828 * The iretq could re-enable interrupts:
829 */
830 TRACE_IRQS_IRETQ
831restore_args:
Borislav Petkov838feb42011-05-31 22:21:53 +0200832 RESTORE_ARGS 1,8,1
Ingo Molnar3701d8632008-02-09 23:24:08 +0100833
Adrian Bunkf7f3d792008-02-13 23:29:53 +0200834irq_return:
Andy Lutomirski7209a752014-07-23 08:34:11 -0700835 INTERRUPT_RETURN
836
837ENTRY(native_iret)
H. Peter Anvin3891a042014-04-29 16:46:09 -0700838 /*
839 * Are we returning to a stack segment from the LDT? Note: in
840 * 64-bit mode SS:RSP on the exception stack is always valid.
841 */
H. Peter Anvin34273f42014-05-04 10:36:22 -0700842#ifdef CONFIG_X86_ESPFIX64
H. Peter Anvin3891a042014-04-29 16:46:09 -0700843 testb $4,(SS-RIP)(%rsp)
Andy Lutomirski7209a752014-07-23 08:34:11 -0700844 jnz native_irq_return_ldt
H. Peter Anvin34273f42014-05-04 10:36:22 -0700845#endif
H. Peter Anvin3891a042014-04-29 16:46:09 -0700846
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800847.global native_irq_return_iret
Andy Lutomirski7209a752014-07-23 08:34:11 -0700848native_irq_return_iret:
Andy Lutomirskib645af22014-11-22 18:00:33 -0800849 /*
850 * This may fault. Non-paranoid faults on return to userspace are
851 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
852 * Double-faults due to espfix64 are handled in do_double_fault.
853 * Other faults here are fatal.
854 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 iretq
Ingo Molnar3701d8632008-02-09 23:24:08 +0100856
H. Peter Anvin34273f42014-05-04 10:36:22 -0700857#ifdef CONFIG_X86_ESPFIX64
Andy Lutomirski7209a752014-07-23 08:34:11 -0700858native_irq_return_ldt:
H. Peter Anvin3891a042014-04-29 16:46:09 -0700859 pushq_cfi %rax
860 pushq_cfi %rdi
861 SWAPGS
862 movq PER_CPU_VAR(espfix_waddr),%rdi
863 movq %rax,(0*8)(%rdi) /* RAX */
864 movq (2*8)(%rsp),%rax /* RIP */
865 movq %rax,(1*8)(%rdi)
866 movq (3*8)(%rsp),%rax /* CS */
867 movq %rax,(2*8)(%rdi)
868 movq (4*8)(%rsp),%rax /* RFLAGS */
869 movq %rax,(3*8)(%rdi)
870 movq (6*8)(%rsp),%rax /* SS */
871 movq %rax,(5*8)(%rdi)
872 movq (5*8)(%rsp),%rax /* RSP */
873 movq %rax,(4*8)(%rdi)
874 andl $0xffff0000,%eax
875 popq_cfi %rdi
876 orq PER_CPU_VAR(espfix_stack),%rax
877 SWAPGS
878 movq %rax,%rsp
879 popq_cfi %rax
Andy Lutomirski7209a752014-07-23 08:34:11 -0700880 jmp native_irq_return_iret
H. Peter Anvin34273f42014-05-04 10:36:22 -0700881#endif
H. Peter Anvin3891a042014-04-29 16:46:09 -0700882
Jan Beulich7effaa82005-09-12 18:49:24 +0200883 /* edi: workmask, edx: work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884retint_careful:
Jan Beulich7effaa82005-09-12 18:49:24 +0200885 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 bt $TIF_NEED_RESCHED,%edx
887 jnc retint_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700888 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100889 ENABLE_INTERRUPTS(CLBR_NONE)
Jan Beulichdf5d1872010-09-02 14:07:16 +0100890 pushq_cfi %rdi
Frederic Weisbecker04304992012-07-11 20:26:38 +0200891 SCHEDULE_USER
Jan Beulichdf5d1872010-09-02 14:07:16 +0100892 popq_cfi %rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100894 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700895 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 jmp retint_check
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898retint_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100899 testl $_TIF_DO_NOTIFY_MASK,%edx
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700900 jz retint_swapgs
Ingo Molnar2601e642006-07-03 00:24:45 -0700901 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100902 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 SAVE_REST
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100904 movq $-1,ORIG_RAX(%rsp)
Andi Kleen3829ee62005-07-28 21:15:48 -0700905 xorl %esi,%esi # oldset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 movq %rsp,%rdi # &pt_regs
907 call do_notify_resume
908 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100909 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700910 TRACE_IRQS_OFF
Andi Kleenbe9e6872005-05-01 08:58:51 -0700911 GET_THREAD_INFO(%rcx)
Roland McGratheca91e72008-07-10 14:50:39 -0700912 jmp retint_with_reschedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914#ifdef CONFIG_PREEMPT
915 /* Returning to kernel space. Check if we need preemption */
916 /* rcx: threadinfo. interrupts off. */
Andi Kleenb06baba2006-09-26 10:52:29 +0200917ENTRY(retint_kernel)
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +0200918 cmpl $0,PER_CPU_VAR(__preempt_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 jnz retint_restore_args
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
921 jnc retint_restore_args
922 call preempt_schedule_irq
923 jmp exit_intr
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100924#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200926END(common_interrupt)
H. Peter Anvin3891a042014-04-29 16:46:09 -0700927
Masami Hiramatsu8222d712009-08-27 13:23:25 -0400928/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 * APIC interrupts.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100930 */
Seiji Aguchicf910e82013-06-20 11:46:53 -0400931.macro apicinterrupt3 num sym do_sym
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100932ENTRY(\sym)
Jan Beulich7effaa82005-09-12 18:49:24 +0200933 INTR_FRAME
Jan Beulichee4eb872012-11-02 11:18:39 +0000934 ASM_CLAC
Jan Beulichdf5d1872010-09-02 14:07:16 +0100935 pushq_cfi $~(\num)
Jan Beulich39e95432011-11-29 11:03:46 +0000936.Lcommon_\sym:
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100937 interrupt \do_sym
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 jmp ret_from_intr
939 CFI_ENDPROC
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100940END(\sym)
941.endm
Jacob Shin89b831e2005-11-05 17:25:53 +0100942
Seiji Aguchicf910e82013-06-20 11:46:53 -0400943#ifdef CONFIG_TRACING
944#define trace(sym) trace_##sym
945#define smp_trace(sym) smp_trace_##sym
946
947.macro trace_apicinterrupt num sym
948apicinterrupt3 \num trace(\sym) smp_trace(\sym)
949.endm
950#else
951.macro trace_apicinterrupt num sym do_sym
952.endm
953#endif
954
955.macro apicinterrupt num sym do_sym
956apicinterrupt3 \num \sym \do_sym
957trace_apicinterrupt \num \sym
958.endm
959
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100960#ifdef CONFIG_SMP
Seiji Aguchicf910e82013-06-20 11:46:53 -0400961apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR \
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100962 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
Seiji Aguchicf910e82013-06-20 11:46:53 -0400963apicinterrupt3 REBOOT_VECTOR \
Andi Kleen4ef702c2009-05-27 21:56:52 +0200964 reboot_interrupt smp_reboot_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965#endif
966
Nick Piggin03b48632009-01-20 04:36:04 +0100967#ifdef CONFIG_X86_UV
Seiji Aguchicf910e82013-06-20 11:46:53 -0400968apicinterrupt3 UV_BAU_MESSAGE \
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100969 uv_bau_message_intr1 uv_bau_message_interrupt
Nick Piggin03b48632009-01-20 04:36:04 +0100970#endif
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100971apicinterrupt LOCAL_TIMER_VECTOR \
972 apic_timer_interrupt smp_apic_timer_interrupt
Dimitri Sivanich4a4de9c2009-10-14 09:22:57 -0500973apicinterrupt X86_PLATFORM_IPI_VECTOR \
974 x86_platform_ipi smp_x86_platform_ipi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Yang Zhangd78f2662013-04-11 19:25:11 +0800976#ifdef CONFIG_HAVE_KVM
Seiji Aguchicf910e82013-06-20 11:46:53 -0400977apicinterrupt3 POSTED_INTR_VECTOR \
Yang Zhangd78f2662013-04-11 19:25:11 +0800978 kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
979#endif
980
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400981#ifdef CONFIG_X86_MCE_THRESHOLD
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100982apicinterrupt THRESHOLD_APIC_VECTOR \
Andi Kleen7856f6c2009-04-28 23:32:56 +0200983 threshold_interrupt smp_threshold_interrupt
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400984#endif
985
986#ifdef CONFIG_X86_THERMAL_VECTOR
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100987apicinterrupt THERMAL_APIC_VECTOR \
988 thermal_interrupt smp_thermal_interrupt
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400989#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100991#ifdef CONFIG_SMP
992apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
993 call_function_single_interrupt smp_call_function_single_interrupt
994apicinterrupt CALL_FUNCTION_VECTOR \
995 call_function_interrupt smp_call_function_interrupt
996apicinterrupt RESCHEDULE_VECTOR \
997 reschedule_interrupt smp_reschedule_interrupt
998#endif
999
1000apicinterrupt ERROR_APIC_VECTOR \
1001 error_interrupt smp_error_interrupt
1002apicinterrupt SPURIOUS_APIC_VECTOR \
1003 spurious_interrupt smp_spurious_interrupt
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001004
Peter Zijlstrae360adb2010-10-14 14:01:34 +08001005#ifdef CONFIG_IRQ_WORK
1006apicinterrupt IRQ_WORK_VECTOR \
1007 irq_work_interrupt smp_irq_work_interrupt
Ingo Molnar241771e2008-12-03 10:39:53 +01001008#endif
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010/*
1011 * Exception entry points.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001012 */
Andy Lutomirski577ed452014-05-21 15:07:09 -07001013#define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
1014
1015.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
Alexander van Heukelum322648d2008-11-23 10:08:28 +01001016ENTRY(\sym)
Andy Lutomirski577ed452014-05-21 15:07:09 -07001017 /* Sanity check */
1018 .if \shift_ist != -1 && \paranoid == 0
1019 .error "using shift_ist requires paranoid=1"
1020 .endif
1021
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001022 .if \has_error_code
1023 XCPT_FRAME
1024 .else
Jan Beulich7effaa82005-09-12 18:49:24 +02001025 INTR_FRAME
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001026 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Jan Beulichee4eb872012-11-02 11:18:39 +00001028 ASM_CLAC
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +01001029 PARAVIRT_ADJUST_EXCEPTION_FRAME
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001030
1031 .ifeq \has_error_code
1032 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1033 .endif
1034
Jan Beulichb1cccb12010-09-02 13:55:11 +01001035 subq $ORIG_RAX-R15, %rsp
1036 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001037
1038 .if \paranoid
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001039 .if \paranoid == 1
1040 CFI_REMEMBER_STATE
1041 testl $3, CS(%rsp) /* If coming from userspace, switch */
1042 jnz 1f /* stacks. */
1043 .endif
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +01001044 call save_paranoid
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001045 .else
1046 call error_entry
1047 .endif
1048
Andy Lutomirski1bd24ef2014-05-21 15:07:07 -07001049 DEFAULT_FRAME 0
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001050
1051 .if \paranoid
Andy Lutomirski577ed452014-05-21 15:07:09 -07001052 .if \shift_ist != -1
1053 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
1054 .else
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +01001055 TRACE_IRQS_OFF
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001056 .endif
Andy Lutomirski577ed452014-05-21 15:07:09 -07001057 .endif
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001058
1059 movq %rsp,%rdi /* pt_regs pointer */
1060
1061 .if \has_error_code
1062 movq ORIG_RAX(%rsp),%rsi /* get error code */
1063 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1064 .else
1065 xorl %esi,%esi /* no error code */
1066 .endif
1067
Andy Lutomirski577ed452014-05-21 15:07:09 -07001068 .if \shift_ist != -1
1069 subq $EXCEPTION_STKSZ, INIT_TSS_IST(\shift_ist)
1070 .endif
1071
Alexander van Heukelum322648d2008-11-23 10:08:28 +01001072 call \do_sym
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001073
Andy Lutomirski577ed452014-05-21 15:07:09 -07001074 .if \shift_ist != -1
1075 addq $EXCEPTION_STKSZ, INIT_TSS_IST(\shift_ist)
1076 .endif
1077
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001078 .if \paranoid
1079 jmp paranoid_exit /* %ebx: no swapgs flag */
1080 .else
1081 jmp error_exit /* %ebx: no swapgs flag */
1082 .endif
1083
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001084 .if \paranoid == 1
1085 CFI_RESTORE_STATE
1086 /*
1087 * Paranoid entry from userspace. Switch stacks and treat it
1088 * as a normal entry. This means that paranoid handlers
1089 * run in real process context if user_mode(regs).
1090 */
10911:
1092 call error_entry
1093
1094 DEFAULT_FRAME 0
1095
1096 movq %rsp,%rdi /* pt_regs pointer */
1097 call sync_regs
1098 movq %rax,%rsp /* switch stack */
1099
1100 movq %rsp,%rdi /* pt_regs pointer */
1101
1102 .if \has_error_code
1103 movq ORIG_RAX(%rsp),%rsi /* get error code */
1104 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1105 .else
1106 xorl %esi,%esi /* no error code */
1107 .endif
1108
1109 call \do_sym
1110
1111 jmp error_exit /* %ebx: no swapgs flag */
1112 .endif
1113
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +01001114 CFI_ENDPROC
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001115END(\sym)
Alexander van Heukelum322648d2008-11-23 10:08:28 +01001116.endm
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +01001117
Seiji Aguchi25c74b12013-10-30 16:37:00 -04001118#ifdef CONFIG_TRACING
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001119.macro trace_idtentry sym do_sym has_error_code:req
1120idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
1121idtentry \sym \do_sym has_error_code=\has_error_code
Seiji Aguchi25c74b12013-10-30 16:37:00 -04001122.endm
1123#else
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001124.macro trace_idtentry sym do_sym has_error_code:req
1125idtentry \sym \do_sym has_error_code=\has_error_code
Seiji Aguchi25c74b12013-10-30 16:37:00 -04001126.endm
1127#endif
1128
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001129idtentry divide_error do_divide_error has_error_code=0
1130idtentry overflow do_overflow has_error_code=0
1131idtentry bounds do_bounds has_error_code=0
1132idtentry invalid_op do_invalid_op has_error_code=0
1133idtentry device_not_available do_device_not_available has_error_code=0
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001134idtentry double_fault do_double_fault has_error_code=1 paranoid=2
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001135idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
1136idtentry invalid_TSS do_invalid_TSS has_error_code=1
1137idtentry segment_not_present do_segment_not_present has_error_code=1
1138idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
1139idtentry coprocessor_error do_coprocessor_error has_error_code=0
1140idtentry alignment_check do_alignment_check has_error_code=1
1141idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
Andy Lutomirski5cec93c2011-06-05 13:50:24 -04001142
Ingo Molnar2601e642006-07-03 00:24:45 -07001143
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001144 /* Reload gs selector with exception handling */
1145 /* edi: new selector */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001146ENTRY(native_load_gs_index)
Jan Beulich7effaa82005-09-12 18:49:24 +02001147 CFI_STARTPROC
Jan Beulichdf5d1872010-09-02 14:07:16 +01001148 pushfq_cfi
Jeremy Fitzhardingeb8aa2872009-01-28 14:35:03 -08001149 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001150 SWAPGS
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001151gs_change:
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001152 movl %edi,%gs
Linus Torvalds1da177e2005-04-16 15:20:36 -070011532: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001154 SWAPGS
Jan Beulichdf5d1872010-09-02 14:07:16 +01001155 popfq_cfi
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001156 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001157 CFI_ENDPROC
Alexander van Heukelum6efdcfa2008-11-23 10:15:32 +01001158END(native_load_gs_index)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001159
H. Peter Anvind7abc0f2012-04-20 12:19:50 -07001160 _ASM_EXTABLE(gs_change,bad_gs)
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001161 .section .fixup,"ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* running with kernelgs */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001163bad_gs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001164 SWAPGS /* switch back to user gs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 xorl %eax,%eax
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001166 movl %eax,%gs
1167 jmp 2b
1168 .previous
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001169
Andi Kleen26995002006-08-02 22:37:28 +02001170/* Call softirq on interrupt stack. Interrupts are off. */
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +02001171ENTRY(do_softirq_own_stack)
Jan Beulich7effaa82005-09-12 18:49:24 +02001172 CFI_STARTPROC
Jan Beulichdf5d1872010-09-02 14:07:16 +01001173 pushq_cfi %rbp
Andi Kleen26995002006-08-02 22:37:28 +02001174 CFI_REL_OFFSET rbp,0
1175 mov %rsp,%rbp
1176 CFI_DEF_CFA_REGISTER rbp
Brian Gerst56895532009-01-19 00:38:58 +09001177 incl PER_CPU_VAR(irq_count)
Brian Gerst26f80bd2009-01-19 00:38:58 +09001178 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
Andi Kleen26995002006-08-02 22:37:28 +02001179 push %rbp # backlink for old unwinder
Andi Kleened6b6762005-07-28 21:15:49 -07001180 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +02001181 leaveq
Jan Beulichdf5d1872010-09-02 14:07:16 +01001182 CFI_RESTORE rbp
Jan Beulich7effaa82005-09-12 18:49:24 +02001183 CFI_DEF_CFA_REGISTER rsp
Andi Kleen26995002006-08-02 22:37:28 +02001184 CFI_ADJUST_CFA_OFFSET -8
Brian Gerst56895532009-01-19 00:38:58 +09001185 decl PER_CPU_VAR(irq_count)
Andi Kleened6b6762005-07-28 21:15:49 -07001186 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001187 CFI_ENDPROC
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +02001188END(do_softirq_own_stack)
Andi Kleen75154f42007-06-23 02:29:25 +02001189
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001190#ifdef CONFIG_XEN
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001191idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001192
1193/*
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001194 * A note on the "critical region" in our callback handler.
1195 * We want to avoid stacking callback handlers due to events occurring
1196 * during handling of the last event. To do this, we keep events disabled
1197 * until we've done all processing. HOWEVER, we must enable events before
1198 * popping the stack frame (can't be done atomically) and so it would still
1199 * be possible to get enough handler activations to overflow the stack.
1200 * Although unlikely, bugs of that kind are hard to track down, so we'd
1201 * like to avoid the possibility.
1202 * So, on entry to the handler we detect whether we interrupted an
1203 * existing activation in its critical region -- if so, we pop the current
1204 * activation and restart the handler using the previous one.
1205 */
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001206ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1207 CFI_STARTPROC
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001208/*
1209 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1210 * see the correct pointer to the pt_regs
1211 */
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001212 movq %rdi, %rsp # we don't return, adjust the stack frame
1213 CFI_ENDPROC
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +01001214 DEFAULT_FRAME
Brian Gerst56895532009-01-19 00:38:58 +0900121511: incl PER_CPU_VAR(irq_count)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001216 movq %rsp,%rbp
1217 CFI_DEF_CFA_REGISTER rbp
Brian Gerst26f80bd2009-01-19 00:38:58 +09001218 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001219 pushq %rbp # backlink for old unwinder
1220 call xen_evtchn_do_upcall
1221 popq %rsp
1222 CFI_DEF_CFA_REGISTER rsp
Brian Gerst56895532009-01-19 00:38:58 +09001223 decl PER_CPU_VAR(irq_count)
David Vrabelfdfd8112015-02-19 15:23:17 +00001224#ifndef CONFIG_PREEMPT
1225 call xen_maybe_preempt_hcall
1226#endif
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001227 jmp error_exit
1228 CFI_ENDPROC
Alexander van Heukelum371c3942011-03-11 21:59:38 +01001229END(xen_do_hypervisor_callback)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001230
1231/*
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001232 * Hypervisor uses this for application faults while it executes.
1233 * We get here for two reasons:
1234 * 1. Fault while reloading DS, ES, FS or GS
1235 * 2. Fault while executing IRET
1236 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1237 * registers that could be reloaded and zeroed the others.
1238 * Category 2 we fix up by killing the current process. We cannot use the
1239 * normal Linux return path in this case because if we use the IRET hypercall
1240 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1241 * We distinguish between categories by comparing each saved segment register
1242 * with its current contents: any discrepancy means we in category 1.
1243 */
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001244ENTRY(xen_failsafe_callback)
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +01001245 INTR_FRAME 1 (6*8)
1246 /*CFI_REL_OFFSET gs,GS*/
1247 /*CFI_REL_OFFSET fs,FS*/
1248 /*CFI_REL_OFFSET es,ES*/
1249 /*CFI_REL_OFFSET ds,DS*/
1250 CFI_REL_OFFSET r11,8
1251 CFI_REL_OFFSET rcx,0
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001252 movw %ds,%cx
1253 cmpw %cx,0x10(%rsp)
1254 CFI_REMEMBER_STATE
1255 jne 1f
1256 movw %es,%cx
1257 cmpw %cx,0x18(%rsp)
1258 jne 1f
1259 movw %fs,%cx
1260 cmpw %cx,0x20(%rsp)
1261 jne 1f
1262 movw %gs,%cx
1263 cmpw %cx,0x28(%rsp)
1264 jne 1f
1265 /* All segments match their saved values => Category 2 (Bad IRET). */
1266 movq (%rsp),%rcx
1267 CFI_RESTORE rcx
1268 movq 8(%rsp),%r11
1269 CFI_RESTORE r11
1270 addq $0x30,%rsp
1271 CFI_ADJUST_CFA_OFFSET -0x30
Ingo Molnar14ae22b2008-11-21 15:20:47 +01001272 pushq_cfi $0 /* RIP */
1273 pushq_cfi %r11
1274 pushq_cfi %rcx
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001275 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001276 CFI_RESTORE_STATE
12771: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1278 movq (%rsp),%rcx
1279 CFI_RESTORE rcx
1280 movq 8(%rsp),%r11
1281 CFI_RESTORE r11
1282 addq $0x30,%rsp
1283 CFI_ADJUST_CFA_OFFSET -0x30
David Vrabela349e23d12012-10-19 17:29:07 +01001284 pushq_cfi $-1 /* orig_ax = -1 => not a system call */
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001285 SAVE_ALL
1286 jmp error_exit
1287 CFI_ENDPROC
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001288END(xen_failsafe_callback)
1289
Seiji Aguchicf910e82013-06-20 11:46:53 -04001290apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
Sheng Yang38e20b02010-05-14 12:40:51 +01001291 xen_hvm_callback_vector xen_evtchn_do_upcall
1292
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001293#endif /* CONFIG_XEN */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001294
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001295#if IS_ENABLED(CONFIG_HYPERV)
Seiji Aguchicf910e82013-06-20 11:46:53 -04001296apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001297 hyperv_callback_vector hyperv_vector_handler
1298#endif /* CONFIG_HYPERV */
1299
Andy Lutomirski577ed452014-05-21 15:07:09 -07001300idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1301idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
Andy Lutomirski6f442be2014-11-22 18:00:32 -08001302idtentry stack_segment do_stack_segment has_error_code=1
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -07001303#ifdef CONFIG_XEN
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001304idtentry xen_debug do_debug has_error_code=0
1305idtentry xen_int3 do_int3 has_error_code=0
1306idtentry xen_stack_segment do_stack_segment has_error_code=1
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -07001307#endif
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001308idtentry general_protection do_general_protection has_error_code=1
1309trace_idtentry page_fault do_page_fault has_error_code=1
Gleb Natapov631bc482010-10-14 11:22:52 +02001310#ifdef CONFIG_KVM_GUEST
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001311idtentry async_page_fault do_async_page_fault has_error_code=1
Gleb Natapov631bc482010-10-14 11:22:52 +02001312#endif
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001313#ifdef CONFIG_X86_MCE
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -07001314idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001315#endif
1316
1317 /*
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001318 * "Paranoid" exit path from exception stack. This is invoked
1319 * only on return from non-NMI IST interrupts that came
1320 * from kernel space.
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001321 *
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001322 * We may be returning to very strange contexts (e.g. very early
1323 * in syscall entry), so checking for preemption here would
1324 * be complicated. Fortunately, we there's no good reason
1325 * to try to handle preemption here.
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001326 */
1327
1328 /* ebx: no swapgs flag */
1329ENTRY(paranoid_exit)
Jan Beulich1f130a72010-09-02 13:54:32 +01001330 DEFAULT_FRAME
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001331 DISABLE_INTERRUPTS(CLBR_NONE)
Steven Rostedt5963e312012-05-30 11:54:53 -04001332 TRACE_IRQS_OFF_DEBUG
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001333 testl %ebx,%ebx /* swapgs needed? */
1334 jnz paranoid_restore
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001335 TRACE_IRQS_IRETQ 0
1336 SWAPGS_UNSAFE_STACK
Steven Rostedt0300e7f12009-04-17 08:33:52 -04001337 RESTORE_ALL 8
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001338 INTERRUPT_RETURN
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001339paranoid_restore:
Steven Rostedt5963e312012-05-30 11:54:53 -04001340 TRACE_IRQS_IRETQ_DEBUG 0
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001341 RESTORE_ALL 8
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001342 INTERRUPT_RETURN
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001343 CFI_ENDPROC
1344END(paranoid_exit)
1345
1346/*
1347 * Exception entry point. This expects an error code/orig_rax on the stack.
1348 * returns in "no swapgs flag" in %ebx.
1349 */
1350ENTRY(error_entry)
1351 XCPT_FRAME
1352 CFI_ADJUST_CFA_OFFSET 15*8
1353 /* oldrax contains error code */
1354 cld
Jan Beulich3bab13b2014-06-25 14:11:22 +01001355 movq %rdi, RDI+8(%rsp)
1356 movq %rsi, RSI+8(%rsp)
1357 movq %rdx, RDX+8(%rsp)
1358 movq %rcx, RCX+8(%rsp)
1359 movq %rax, RAX+8(%rsp)
1360 movq %r8, R8+8(%rsp)
1361 movq %r9, R9+8(%rsp)
1362 movq %r10, R10+8(%rsp)
1363 movq %r11, R11+8(%rsp)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001364 movq_cfi rbx, RBX+8
Jan Beulich3bab13b2014-06-25 14:11:22 +01001365 movq %rbp, RBP+8(%rsp)
1366 movq %r12, R12+8(%rsp)
1367 movq %r13, R13+8(%rsp)
1368 movq %r14, R14+8(%rsp)
1369 movq %r15, R15+8(%rsp)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001370 xorl %ebx,%ebx
1371 testl $3,CS+8(%rsp)
1372 je error_kernelspace
1373error_swapgs:
1374 SWAPGS
1375error_sti:
1376 TRACE_IRQS_OFF
1377 ret
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001378
1379/*
1380 * There are two places in the kernel that can potentially fault with
Andy Lutomirskib645af22014-11-22 18:00:33 -08001381 * usergs. Handle them here. B stepping K8s sometimes report a
1382 * truncated RIP for IRET exceptions returning to compat mode. Check
1383 * for these here too.
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001384 */
1385error_kernelspace:
Jan Beulich3bab13b2014-06-25 14:11:22 +01001386 CFI_REL_OFFSET rcx, RCX+8
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001387 incl %ebx
Andy Lutomirski7209a752014-07-23 08:34:11 -07001388 leaq native_irq_return_iret(%rip),%rcx
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001389 cmpq %rcx,RIP+8(%rsp)
Andy Lutomirskib645af22014-11-22 18:00:33 -08001390 je error_bad_iret
Brian Gerstae24ffe2009-10-12 10:18:23 -04001391 movl %ecx,%eax /* zero extend */
1392 cmpq %rax,RIP+8(%rsp)
1393 je bstep_iret
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001394 cmpq $gs_change,RIP+8(%rsp)
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +03001395 je error_swapgs
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001396 jmp error_sti
Brian Gerstae24ffe2009-10-12 10:18:23 -04001397
1398bstep_iret:
1399 /* Fix truncated RIP */
1400 movq %rcx,RIP+8(%rsp)
Andy Lutomirskib645af22014-11-22 18:00:33 -08001401 /* fall through */
1402
1403error_bad_iret:
1404 SWAPGS
1405 mov %rsp,%rdi
1406 call fixup_bad_iret
1407 mov %rax,%rsp
1408 decl %ebx /* Return to usergs */
1409 jmp error_sti
Jan Beuliche6b04b62010-09-02 13:52:45 +01001410 CFI_ENDPROC
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001411END(error_entry)
1412
1413
1414/* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1415ENTRY(error_exit)
1416 DEFAULT_FRAME
1417 movl %ebx,%eax
1418 RESTORE_REST
1419 DISABLE_INTERRUPTS(CLBR_NONE)
1420 TRACE_IRQS_OFF
1421 GET_THREAD_INFO(%rcx)
1422 testl %eax,%eax
1423 jne retint_kernel
1424 LOCKDEP_SYS_EXIT_IRQ
1425 movl TI_flags(%rcx),%edx
1426 movl $_TIF_WORK_MASK,%edi
1427 andl %edi,%edx
1428 jnz retint_careful
1429 jmp retint_swapgs
1430 CFI_ENDPROC
1431END(error_exit)
1432
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001433/*
1434 * Test if a given stack is an NMI stack or not.
1435 */
1436 .macro test_in_nmi reg stack nmi_ret normal_ret
1437 cmpq %\reg, \stack
1438 ja \normal_ret
1439 subq $EXCEPTION_STKSZ, %\reg
1440 cmpq %\reg, \stack
1441 jb \normal_ret
1442 jmp \nmi_ret
1443 .endm
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001444
1445 /* runs on exception stack */
1446ENTRY(nmi)
1447 INTR_FRAME
1448 PARAVIRT_ADJUST_EXCEPTION_FRAME
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001449 /*
1450 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1451 * the iretq it performs will take us out of NMI context.
1452 * This means that we can have nested NMIs where the next
1453 * NMI is using the top of the stack of the previous NMI. We
1454 * can't let it execute because the nested NMI will corrupt the
1455 * stack of the previous NMI. NMI handlers are not re-entrant
1456 * anyway.
1457 *
1458 * To handle this case we do the following:
1459 * Check the a special location on the stack that contains
1460 * a variable that is set when NMIs are executing.
1461 * The interrupted task's stack is also checked to see if it
1462 * is an NMI stack.
1463 * If the variable is not set and the stack is not the NMI
1464 * stack then:
1465 * o Set the special variable on the stack
1466 * o Copy the interrupt frame into a "saved" location on the stack
1467 * o Copy the interrupt frame into a "copy" location on the stack
1468 * o Continue processing the NMI
1469 * If the variable is set or the previous stack is the NMI stack:
1470 * o Modify the "copy" location to jump to the repeate_nmi
1471 * o return back to the first NMI
1472 *
1473 * Now on exit of the first NMI, we first clear the stack variable
1474 * The NMI stack will tell any nested NMIs at that point that it is
1475 * nested. Then we pop the stack normally with iret, and if there was
1476 * a nested NMI that updated the copy interrupt stack frame, a
1477 * jump will be made to the repeat_nmi code that will handle the second
1478 * NMI.
1479 */
1480
1481 /* Use %rdx as out temp variable throughout */
1482 pushq_cfi %rdx
Jan Beulich62610912012-02-24 14:54:37 +00001483 CFI_REL_OFFSET rdx, 0
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001484
1485 /*
Steven Rostedt45d5a162012-02-19 16:43:37 -05001486 * If %cs was not the kernel segment, then the NMI triggered in user
1487 * space, which means it is definitely not nested.
1488 */
Steven Rostedta38449ef2012-02-20 15:29:34 -05001489 cmpl $__KERNEL_CS, 16(%rsp)
Steven Rostedt45d5a162012-02-19 16:43:37 -05001490 jne first_nmi
1491
1492 /*
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001493 * Check the special variable on the stack to see if NMIs are
1494 * executing.
1495 */
Steven Rostedta38449ef2012-02-20 15:29:34 -05001496 cmpl $1, -8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001497 je nested_nmi
1498
1499 /*
1500 * Now test if the previous stack was an NMI stack.
1501 * We need the double check. We check the NMI stack to satisfy the
1502 * race when the first NMI clears the variable before returning.
1503 * We check the variable because the first NMI could be in a
1504 * breakpoint routine using a breakpoint stack.
1505 */
1506 lea 6*8(%rsp), %rdx
1507 test_in_nmi rdx, 4*8(%rsp), nested_nmi, first_nmi
Jan Beulich62610912012-02-24 14:54:37 +00001508 CFI_REMEMBER_STATE
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001509
1510nested_nmi:
1511 /*
1512 * Do nothing if we interrupted the fixup in repeat_nmi.
1513 * It's about to repeat the NMI handler, so we are fine
1514 * with ignoring this one.
1515 */
1516 movq $repeat_nmi, %rdx
1517 cmpq 8(%rsp), %rdx
1518 ja 1f
1519 movq $end_repeat_nmi, %rdx
1520 cmpq 8(%rsp), %rdx
1521 ja nested_nmi_out
1522
15231:
1524 /* Set up the interrupted NMIs stack to jump to repeat_nmi */
Salman Qazi28696f42012-10-01 17:29:25 -07001525 leaq -1*8(%rsp), %rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001526 movq %rdx, %rsp
Salman Qazi28696f42012-10-01 17:29:25 -07001527 CFI_ADJUST_CFA_OFFSET 1*8
1528 leaq -10*8(%rsp), %rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001529 pushq_cfi $__KERNEL_DS
1530 pushq_cfi %rdx
1531 pushfq_cfi
1532 pushq_cfi $__KERNEL_CS
1533 pushq_cfi $repeat_nmi
1534
1535 /* Put stack back */
Salman Qazi28696f42012-10-01 17:29:25 -07001536 addq $(6*8), %rsp
1537 CFI_ADJUST_CFA_OFFSET -6*8
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001538
1539nested_nmi_out:
1540 popq_cfi %rdx
Jan Beulich62610912012-02-24 14:54:37 +00001541 CFI_RESTORE rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001542
1543 /* No need to check faults here */
1544 INTERRUPT_RETURN
1545
Jan Beulich62610912012-02-24 14:54:37 +00001546 CFI_RESTORE_STATE
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001547first_nmi:
1548 /*
1549 * Because nested NMIs will use the pushed location that we
1550 * stored in rdx, we must keep that space available.
1551 * Here's what our stack frame will look like:
1552 * +-------------------------+
1553 * | original SS |
1554 * | original Return RSP |
1555 * | original RFLAGS |
1556 * | original CS |
1557 * | original RIP |
1558 * +-------------------------+
1559 * | temp storage for rdx |
1560 * +-------------------------+
1561 * | NMI executing variable |
1562 * +-------------------------+
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001563 * | copied SS |
1564 * | copied Return RSP |
1565 * | copied RFLAGS |
1566 * | copied CS |
1567 * | copied RIP |
1568 * +-------------------------+
Salman Qazi28696f42012-10-01 17:29:25 -07001569 * | Saved SS |
1570 * | Saved Return RSP |
1571 * | Saved RFLAGS |
1572 * | Saved CS |
1573 * | Saved RIP |
1574 * +-------------------------+
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001575 * | pt_regs |
1576 * +-------------------------+
1577 *
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001578 * The saved stack frame is used to fix up the copied stack frame
1579 * that a nested NMI may change to make the interrupted NMI iret jump
1580 * to the repeat_nmi. The original stack frame and the temp storage
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001581 * is also used by nested NMIs and can not be trusted on exit.
1582 */
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001583 /* Do not pop rdx, nested NMIs will corrupt that part of the stack */
Jan Beulich62610912012-02-24 14:54:37 +00001584 movq (%rsp), %rdx
1585 CFI_RESTORE rdx
1586
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001587 /* Set the NMI executing variable on the stack. */
1588 pushq_cfi $1
1589
Salman Qazi28696f42012-10-01 17:29:25 -07001590 /*
1591 * Leave room for the "copied" frame
1592 */
1593 subq $(5*8), %rsp
Jan Beulich444723d2013-01-24 09:27:31 +00001594 CFI_ADJUST_CFA_OFFSET 5*8
Salman Qazi28696f42012-10-01 17:29:25 -07001595
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001596 /* Copy the stack frame to the Saved frame */
1597 .rept 5
Salman Qazi28696f42012-10-01 17:29:25 -07001598 pushq_cfi 11*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001599 .endr
Jan Beulich62610912012-02-24 14:54:37 +00001600 CFI_DEF_CFA_OFFSET SS+8-RIP
1601
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001602 /* Everything up to here is safe from nested NMIs */
1603
Jan Beulich62610912012-02-24 14:54:37 +00001604 /*
1605 * If there was a nested NMI, the first NMI's iret will return
1606 * here. But NMIs are still enabled and we can take another
1607 * nested NMI. The nested NMI checks the interrupted RIP to see
1608 * if it is between repeat_nmi and end_repeat_nmi, and if so
1609 * it will just return, as we are about to repeat an NMI anyway.
1610 * This makes it safe to copy to the stack frame that a nested
1611 * NMI will update.
1612 */
1613repeat_nmi:
1614 /*
1615 * Update the stack variable to say we are still in NMI (the update
1616 * is benign for the non-repeat case, where 1 was pushed just above
1617 * to this very stack slot).
1618 */
Salman Qazi28696f42012-10-01 17:29:25 -07001619 movq $1, 10*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001620
1621 /* Make another copy, this one may be modified by nested NMIs */
Salman Qazi28696f42012-10-01 17:29:25 -07001622 addq $(10*8), %rsp
1623 CFI_ADJUST_CFA_OFFSET -10*8
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001624 .rept 5
Salman Qazi28696f42012-10-01 17:29:25 -07001625 pushq_cfi -6*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001626 .endr
Salman Qazi28696f42012-10-01 17:29:25 -07001627 subq $(5*8), %rsp
Jan Beulich62610912012-02-24 14:54:37 +00001628 CFI_DEF_CFA_OFFSET SS+8-RIP
1629end_repeat_nmi:
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001630
1631 /*
1632 * Everything below this point can be preempted by a nested
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001633 * NMI if the first NMI took an exception and reset our iret stack
1634 * so that we repeat another NMI.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001635 */
Steven Rostedt1fd466e2011-12-08 12:32:27 -05001636 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
Jan Beulichb1cccb12010-09-02 13:55:11 +01001637 subq $ORIG_RAX-R15, %rsp
1638 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
Steven Rostedt1fd466e2011-12-08 12:32:27 -05001639 /*
1640 * Use save_paranoid to handle SWAPGS, but no need to use paranoid_exit
1641 * as we should not be calling schedule in NMI context.
1642 * Even with normal interrupts enabled. An NMI should not be
1643 * setting NEED_RESCHED or anything that normal interrupts and
1644 * exceptions might do.
1645 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001646 call save_paranoid
1647 DEFAULT_FRAME 0
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001648
1649 /*
1650 * Save off the CR2 register. If we take a page fault in the NMI then
1651 * it could corrupt the CR2 value. If the NMI preempts a page fault
1652 * handler before it was able to read the CR2 register, and then the
1653 * NMI itself takes a page fault, the page fault that was preempted
1654 * will read the information from the NMI page fault and not the
1655 * origin fault. Save it off and restore it if it changes.
1656 * Use the r12 callee-saved register.
1657 */
1658 movq %cr2, %r12
1659
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001660 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1661 movq %rsp,%rdi
1662 movq $-1,%rsi
1663 call do_nmi
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001664
1665 /* Did the NMI take a page fault? Restore cr2 if it did */
1666 movq %cr2, %rcx
1667 cmpq %rcx, %r12
1668 je 1f
1669 movq %r12, %cr2
16701:
1671
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001672 testl %ebx,%ebx /* swapgs needed? */
1673 jnz nmi_restore
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001674nmi_swapgs:
1675 SWAPGS_UNSAFE_STACK
1676nmi_restore:
Jan Beulich444723d2013-01-24 09:27:31 +00001677 /* Pop the extra iret frame at once */
1678 RESTORE_ALL 6*8
Salman Qazi28696f42012-10-01 17:29:25 -07001679
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001680 /* Clear the NMI executing stack variable */
Salman Qazi28696f42012-10-01 17:29:25 -07001681 movq $0, 5*8(%rsp)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001682 jmp irq_return
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001683 CFI_ENDPROC
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001684END(nmi)
1685
1686ENTRY(ignore_sysret)
1687 CFI_STARTPROC
1688 mov $-ENOSYS,%eax
1689 sysret
1690 CFI_ENDPROC
1691END(ignore_sysret)
1692