x86/asm/entry/64: Shrink code in 'paranoid_exit'
[linux-block.git] / arch / x86 / kernel / entry_64.S
CommitLineData
1da177e4
LT
1/*
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>
1da177e4
LT
7 */
8
9/*
10 * entry.S contains the system-call and fault low-level handling routines.
11 *
8b4777a4
AL
12 * Some of this is documented in Documentation/x86/entry_64.txt
13 *
1da177e4
LT
14 * NOTE: This code handles signal-recognition, which happens every time
15 * after an interrupt and after each system call.
0bd7b798 16 *
0bd7b798
AH
17 * A note on terminology:
18 * - top of stack: Architecture defined interrupt frame from SS to RIP
19 * at the top of the kernel process stack.
0d2eb44f 20 * - partial stack frame: partially saved registers up to R11.
0bd7b798 21 * - full stack frame: Like partial stack frame, but all register saved.
2e91a17b
AK
22 *
23 * Some macro usage:
24 * - CFI macros are used to generate dwarf2 unwind information for better
25 * backtraces. They don't change any code.
2e91a17b
AK
26 * - ENTRY/END Define functions in the symbol table.
27 * - FIXUP_TOP_OF_STACK/RESTORE_TOP_OF_STACK - Fix up the hardware stack
28 * frame that is otherwise undefined after a SYSCALL
29 * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
cb5dd2c5 30 * - idtentry - Define exception entry points.
1da177e4
LT
31 */
32
1da177e4
LT
33#include <linux/linkage.h>
34#include <asm/segment.h>
1da177e4
LT
35#include <asm/cache.h>
36#include <asm/errno.h>
37#include <asm/dwarf2.h>
38#include <asm/calling.h>
e2d5df93 39#include <asm/asm-offsets.h>
1da177e4
LT
40#include <asm/msr.h>
41#include <asm/unistd.h>
42#include <asm/thread_info.h>
43#include <asm/hw_irq.h>
0341c14d 44#include <asm/page_types.h>
2601e64d 45#include <asm/irqflags.h>
72fe4858 46#include <asm/paravirt.h>
9939ddaf 47#include <asm/percpu.h>
d7abc0fa 48#include <asm/asm.h>
91d1aa43 49#include <asm/context_tracking.h>
63bcff2a 50#include <asm/smap.h>
3891a04a 51#include <asm/pgtable_types.h>
d7e7528b 52#include <linux/err.h>
1da177e4 53
86a1c34a
RM
54/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
55#include <linux/elf-em.h>
56#define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
57#define __AUDIT_ARCH_64BIT 0x80000000
58#define __AUDIT_ARCH_LE 0x40000000
59
1da177e4 60 .code64
ea714547
JO
61 .section .entry.text, "ax"
62
16444a8a 63
dc37db4d 64#ifndef CONFIG_PREEMPT
1da177e4 65#define retint_kernel retint_restore_args
0bd7b798 66#endif
2601e64d 67
72fe4858 68#ifdef CONFIG_PARAVIRT
2be29982 69ENTRY(native_usergs_sysret64)
72fe4858
GOC
70 swapgs
71 sysretq
b3baaa13 72ENDPROC(native_usergs_sysret64)
72fe4858
GOC
73#endif /* CONFIG_PARAVIRT */
74
2601e64d
IM
75
76.macro TRACE_IRQS_IRETQ offset=ARGOFFSET
77#ifdef CONFIG_TRACE_IRQFLAGS
78 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
79 jnc 1f
80 TRACE_IRQS_ON
811:
82#endif
83.endm
84
5963e317
SR
85/*
86 * When dynamic function tracer is enabled it will add a breakpoint
87 * to all locations that it is about to modify, sync CPUs, update
88 * all the code, sync CPUs, then remove the breakpoints. In this time
89 * if lockdep is enabled, it might jump back into the debug handler
90 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
91 *
92 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
93 * make sure the stack pointer does not get reset back to the top
94 * of the debug stack, and instead just reuses the current stack.
95 */
96#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
97
98.macro TRACE_IRQS_OFF_DEBUG
99 call debug_stack_set_zero
100 TRACE_IRQS_OFF
101 call debug_stack_reset
102.endm
103
104.macro TRACE_IRQS_ON_DEBUG
105 call debug_stack_set_zero
106 TRACE_IRQS_ON
107 call debug_stack_reset
108.endm
109
110.macro TRACE_IRQS_IRETQ_DEBUG offset=ARGOFFSET
111 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
112 jnc 1f
113 TRACE_IRQS_ON_DEBUG
1141:
115.endm
116
117#else
118# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
119# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
120# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
121#endif
122
1da177e4 123/*
0bd7b798
AH
124 * C code is not supposed to know about undefined top of stack. Every time
125 * a C function with an pt_regs argument is called from the SYSCALL based
1da177e4
LT
126 * fast path FIXUP_TOP_OF_STACK is needed.
127 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
128 * manipulation.
0bd7b798
AH
129 */
130
131 /* %rsp:at FRAMEEND */
c002a1e6 132 .macro FIXUP_TOP_OF_STACK tmp offset=0
3d1e42a7 133 movq PER_CPU_VAR(old_rsp),\tmp
c002a1e6
AH
134 movq \tmp,RSP+\offset(%rsp)
135 movq $__USER_DS,SS+\offset(%rsp)
136 movq $__USER_CS,CS+\offset(%rsp)
0fcedc86
AL
137 movq RIP+\offset(%rsp),\tmp /* get rip */
138 movq \tmp,RCX+\offset(%rsp) /* copy it to rcx as sysret would do */
c002a1e6
AH
139 movq R11+\offset(%rsp),\tmp /* get eflags */
140 movq \tmp,EFLAGS+\offset(%rsp)
1da177e4
LT
141 .endm
142
c002a1e6
AH
143 .macro RESTORE_TOP_OF_STACK tmp offset=0
144 movq RSP+\offset(%rsp),\tmp
3d1e42a7 145 movq \tmp,PER_CPU_VAR(old_rsp)
c002a1e6
AH
146 movq EFLAGS+\offset(%rsp),\tmp
147 movq \tmp,R11+\offset(%rsp)
1da177e4
LT
148 .endm
149
dcd072e2 150/*
e90e147c 151 * empty frame
dcd072e2
AH
152 */
153 .macro EMPTY_FRAME start=1 offset=0
7effaa88 154 .if \start
dcd072e2 155 CFI_STARTPROC simple
adf14236 156 CFI_SIGNAL_FRAME
dcd072e2 157 CFI_DEF_CFA rsp,8+\offset
7effaa88 158 .else
dcd072e2 159 CFI_DEF_CFA_OFFSET 8+\offset
7effaa88 160 .endif
1da177e4 161 .endm
d99015b1
AH
162
163/*
dcd072e2 164 * initial frame state for interrupts (and exceptions without error code)
d99015b1 165 */
dcd072e2 166 .macro INTR_FRAME start=1 offset=0
e8a0e276
IM
167 EMPTY_FRAME \start, SS+8+\offset-RIP
168 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
169 CFI_REL_OFFSET rsp, RSP+\offset-RIP
170 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
171 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
172 CFI_REL_OFFSET rip, RIP+\offset-RIP
d99015b1
AH
173 .endm
174
d99015b1
AH
175/*
176 * initial frame state for exceptions with error code (and interrupts
177 * with vector already pushed)
178 */
dcd072e2 179 .macro XCPT_FRAME start=1 offset=0
e8a0e276 180 INTR_FRAME \start, RIP+\offset-ORIG_RAX
dcd072e2
AH
181 .endm
182
183/*
76f5df43 184 * frame that enables passing a complete pt_regs to a C function.
dcd072e2 185 */
76f5df43 186 .macro DEFAULT_FRAME start=1 offset=0
e8a0e276
IM
187 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
188 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
189 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
190 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
191 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
192 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
193 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
194 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
195 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
196 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
dcd072e2
AH
197 CFI_REL_OFFSET rbx, RBX+\offset
198 CFI_REL_OFFSET rbp, RBP+\offset
199 CFI_REL_OFFSET r12, R12+\offset
200 CFI_REL_OFFSET r13, R13+\offset
201 CFI_REL_OFFSET r14, R14+\offset
202 CFI_REL_OFFSET r15, R15+\offset
203 .endm
d99015b1 204
e2f6bc25
AH
205ENTRY(save_paranoid)
206 XCPT_FRAME 1 RDI+8
207 cld
76f5df43
DV
208 SAVE_C_REGS 8
209 SAVE_EXTRA_REGS 8
e2f6bc25
AH
210 movl $1,%ebx
211 movl $MSR_GS_BASE,%ecx
212 rdmsr
213 testl %edx,%edx
214 js 1f /* negative -> in kernel */
215 SWAPGS
216 xorl %ebx,%ebx
2171: ret
218 CFI_ENDPROC
219END(save_paranoid)
220
1da177e4 221/*
5b3eec0c
IM
222 * A newly forked process directly context switches into this address.
223 *
224 * rdi: prev task we switched from
0bd7b798 225 */
1da177e4 226ENTRY(ret_from_fork)
dcd072e2 227 DEFAULT_FRAME
5b3eec0c 228
7106a5ab
BL
229 LOCK ; btr $TIF_FORK,TI_flags(%r8)
230
6eebdda3 231 pushq_cfi $0x0002
df5d1874 232 popfq_cfi # reset kernel eflags
5b3eec0c
IM
233
234 call schedule_tail # rdi: 'prev' task parameter
235
1da177e4 236 GET_THREAD_INFO(%rcx)
5b3eec0c 237
76f5df43 238 RESTORE_EXTRA_REGS
5b3eec0c
IM
239
240 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
7076aada 241 jz 1f
5b3eec0c
IM
242
243 testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
1da177e4 244 jnz int_ret_from_sys_call
5b3eec0c 245
c002a1e6 246 RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
5b3eec0c
IM
247 jmp ret_from_sys_call # go to the SYSRET fastpath
248
7076aada 2491:
7076aada
AV
250 movq %rbp, %rdi
251 call *%rbx
22e2430d 252 movl $0, RAX(%rsp)
76f5df43 253 RESTORE_EXTRA_REGS
22e2430d 254 jmp int_ret_from_sys_call
1da177e4 255 CFI_ENDPROC
4b787e0b 256END(ret_from_fork)
1da177e4
LT
257
258/*
0d2eb44f 259 * System call entry. Up to 6 arguments in registers are supported.
1da177e4
LT
260 *
261 * SYSCALL does not save anything on the stack and does not change the
63bcff2a
PA
262 * stack pointer. However, it does mask the flags register for us, so
263 * CLD and CLAC are not needed.
1da177e4 264 */
0bd7b798 265
1da177e4 266/*
0bd7b798 267 * Register setup:
1da177e4
LT
268 * rax system call number
269 * rdi arg0
0bd7b798 270 * rcx return address for syscall/sysret, C arg3
1da177e4 271 * rsi arg1
0bd7b798 272 * rdx arg2
1da177e4
LT
273 * r10 arg3 (--> moved to rcx for C)
274 * r8 arg4
275 * r9 arg5
276 * r11 eflags for syscall/sysret, temporary for C
0bd7b798
AH
277 * r12-r15,rbp,rbx saved by C code, not touched.
278 *
1da177e4
LT
279 * Interrupts are off on entry.
280 * Only called from user space.
281 *
282 * XXX if we had a free scratch register we could save the RSP into the stack frame
283 * and report it properly in ps. Unfortunately we haven't.
7bf36bbc
AK
284 *
285 * When user can change the frames always force IRET. That is because
286 * it deals with uncanonical addresses better. SYSRET has trouble
287 * with them due to bugs in both AMD and Intel CPUs.
0bd7b798 288 */
1da177e4
LT
289
290ENTRY(system_call)
7effaa88 291 CFI_STARTPROC simple
adf14236 292 CFI_SIGNAL_FRAME
9af45651 293 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
7effaa88
JB
294 CFI_REGISTER rip,rcx
295 /*CFI_REGISTER rflags,r11*/
72fe4858
GOC
296 SWAPGS_UNSAFE_STACK
297 /*
298 * A hypervisor implementation might want to use a label
299 * after the swapgs, so that it can do the swapgs
300 * for the guest and jump here on syscall.
301 */
f6b2bc84 302GLOBAL(system_call_after_swapgs)
72fe4858 303
3d1e42a7 304 movq %rsp,PER_CPU_VAR(old_rsp)
9af45651 305 movq PER_CPU_VAR(kernel_stack),%rsp
2601e64d
IM
306 /*
307 * No need to follow this irqs off/on section - it's straight
308 * and short:
309 */
72fe4858 310 ENABLE_INTERRUPTS(CLBR_NONE)
76f5df43
DV
311 ALLOC_PT_GPREGS_ON_STACK 8
312 SAVE_C_REGS_EXCEPT_RAX_RCX
313 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
54eea995 314 movq_cfi rax,(ORIG_RAX-ARGOFFSET)
76f5df43 315 movq %rcx,RIP-ARGOFFSET(%rsp)
7effaa88 316 CFI_REL_OFFSET rip,RIP-ARGOFFSET
46db09d3 317 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
1da177e4 318 jnz tracesys
86a1c34a 319system_call_fastpath:
fca460f9 320#if __SYSCALL_MASK == ~0
1da177e4 321 cmpq $__NR_syscall_max,%rax
fca460f9
PA
322#else
323 andl $__SYSCALL_MASK,%eax
324 cmpl $__NR_syscall_max,%eax
325#endif
54eea995 326 ja ret_from_sys_call /* and return regs->ax */
1da177e4
LT
327 movq %r10,%rcx
328 call *sys_call_table(,%rax,8) # XXX: rip relative
329 movq %rax,RAX-ARGOFFSET(%rsp)
330/*
331 * Syscall return path ending with SYSRET (fast path)
0bd7b798
AH
332 * Has incomplete stack frame and undefined top of stack.
333 */
1da177e4 334ret_from_sys_call:
96b6352c
AL
335 testl $_TIF_ALLWORK_MASK,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
336 jnz int_ret_from_sys_call_fixup /* Go the the slow path */
337
10cd706d 338 LOCKDEP_SYS_EXIT
72fe4858 339 DISABLE_INTERRUPTS(CLBR_NONE)
2601e64d 340 TRACE_IRQS_OFF
bcddc015 341 CFI_REMEMBER_STATE
2601e64d
IM
342 /*
343 * sysretq will re-enable interrupts:
344 */
345 TRACE_IRQS_ON
76f5df43 346 RESTORE_C_REGS_EXCEPT_RCX
1da177e4 347 movq RIP-ARGOFFSET(%rsp),%rcx
7effaa88 348 CFI_REGISTER rip,rcx
7effaa88 349 /*CFI_REGISTER rflags,r11*/
3d1e42a7 350 movq PER_CPU_VAR(old_rsp), %rsp
2be29982 351 USERGS_SYSRET64
1da177e4 352
bcddc015 353 CFI_RESTORE_STATE
1da177e4 354
96b6352c 355int_ret_from_sys_call_fixup:
b60e714d 356 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
96b6352c 357 jmp int_ret_from_sys_call
86a1c34a 358
1da177e4 359 /* Do syscall tracing */
0bd7b798 360tracesys:
76f5df43 361 movq %rsp, %rdi
1dcf74f6
AL
362 movq $AUDIT_ARCH_X86_64, %rsi
363 call syscall_trace_enter_phase1
364 test %rax, %rax
365 jnz tracesys_phase2 /* if needed, run the slow path */
76f5df43
DV
366 RESTORE_C_REGS_EXCEPT_RAX /* else restore clobbered regs */
367 movq ORIG_RAX-ARGOFFSET(%rsp), %rax
1dcf74f6
AL
368 jmp system_call_fastpath /* and return to the fast path */
369
370tracesys_phase2:
76f5df43 371 SAVE_EXTRA_REGS
1da177e4 372 FIXUP_TOP_OF_STACK %rdi
1dcf74f6
AL
373 movq %rsp, %rdi
374 movq $AUDIT_ARCH_X86_64, %rsi
375 movq %rax,%rdx
376 call syscall_trace_enter_phase2
377
d4d67150 378 /*
e90e147c 379 * Reload registers from stack in case ptrace changed them.
1dcf74f6 380 * We don't reload %rax because syscall_trace_entry_phase2() returned
d4d67150
RM
381 * the value it wants us to use in the table lookup.
382 */
76f5df43
DV
383 RESTORE_C_REGS_EXCEPT_RAX
384 RESTORE_EXTRA_REGS
fca460f9 385#if __SYSCALL_MASK == ~0
1da177e4 386 cmpq $__NR_syscall_max,%rax
fca460f9
PA
387#else
388 andl $__SYSCALL_MASK,%eax
389 cmpl $__NR_syscall_max,%eax
390#endif
54eea995 391 ja int_ret_from_sys_call /* RAX(%rsp) is already set */
1da177e4
LT
392 movq %r10,%rcx /* fixup for C */
393 call *sys_call_table(,%rax,8)
a31f8dd7 394 movq %rax,RAX-ARGOFFSET(%rsp)
7bf36bbc 395 /* Use IRET because user could have changed frame */
0bd7b798
AH
396
397/*
1da177e4
LT
398 * Syscall return path ending with IRET.
399 * Has correct top of stack, but partial stack frame.
bcddc015 400 */
bc8b2b92 401GLOBAL(int_ret_from_sys_call)
72fe4858 402 DISABLE_INTERRUPTS(CLBR_NONE)
2601e64d 403 TRACE_IRQS_OFF
1da177e4
LT
404 movl $_TIF_ALLWORK_MASK,%edi
405 /* edi: mask to check */
bc8b2b92 406GLOBAL(int_with_check)
10cd706d 407 LOCKDEP_SYS_EXIT_IRQ
1da177e4 408 GET_THREAD_INFO(%rcx)
26ccb8a7 409 movl TI_flags(%rcx),%edx
1da177e4
LT
410 andl %edi,%edx
411 jnz int_careful
26ccb8a7 412 andl $~TS_COMPAT,TI_status(%rcx)
1da177e4
LT
413 jmp retint_swapgs
414
415 /* Either reschedule or signal or syscall exit tracking needed. */
416 /* First do a reschedule test. */
417 /* edx: work, edi: workmask */
418int_careful:
419 bt $TIF_NEED_RESCHED,%edx
420 jnc int_very_careful
2601e64d 421 TRACE_IRQS_ON
72fe4858 422 ENABLE_INTERRUPTS(CLBR_NONE)
df5d1874 423 pushq_cfi %rdi
0430499c 424 SCHEDULE_USER
df5d1874 425 popq_cfi %rdi
72fe4858 426 DISABLE_INTERRUPTS(CLBR_NONE)
2601e64d 427 TRACE_IRQS_OFF
1da177e4
LT
428 jmp int_with_check
429
430 /* handle signals and tracing -- both require a full stack frame */
431int_very_careful:
2601e64d 432 TRACE_IRQS_ON
72fe4858 433 ENABLE_INTERRUPTS(CLBR_NONE)
b60e714d 434int_check_syscall_exit_work:
76f5df43 435 SAVE_EXTRA_REGS
0bd7b798 436 /* Check for syscall exit trace */
d4d67150 437 testl $_TIF_WORK_SYSCALL_EXIT,%edx
1da177e4 438 jz int_signal
df5d1874 439 pushq_cfi %rdi
0bd7b798 440 leaq 8(%rsp),%rdi # &ptregs -> arg1
1da177e4 441 call syscall_trace_leave
df5d1874 442 popq_cfi %rdi
d4d67150 443 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
1da177e4 444 jmp int_restore_rest
0bd7b798 445
1da177e4 446int_signal:
8f4d37ec 447 testl $_TIF_DO_NOTIFY_MASK,%edx
1da177e4
LT
448 jz 1f
449 movq %rsp,%rdi # &ptregs -> arg1
450 xorl %esi,%esi # oldset -> arg2
451 call do_notify_resume
eca91e78 4521: movl $_TIF_WORK_MASK,%edi
1da177e4 453int_restore_rest:
76f5df43 454 RESTORE_EXTRA_REGS
72fe4858 455 DISABLE_INTERRUPTS(CLBR_NONE)
2601e64d 456 TRACE_IRQS_OFF
1da177e4
LT
457 jmp int_with_check
458 CFI_ENDPROC
bcddc015 459END(system_call)
0bd7b798 460
1d4b4b29
AV
461 .macro FORK_LIKE func
462ENTRY(stub_\func)
463 CFI_STARTPROC
76f5df43
DV
464 DEFAULT_FRAME 0, 8 /* offset 8: return address */
465 SAVE_EXTRA_REGS 8
1d4b4b29 466 FIXUP_TOP_OF_STACK %r11, 8
1d4b4b29
AV
467 call sys_\func
468 RESTORE_TOP_OF_STACK %r11, 8
76f5df43 469 ret
1d4b4b29
AV
470 CFI_ENDPROC
471END(stub_\func)
472 .endm
473
b3af11af
AV
474 .macro FIXED_FRAME label,func
475ENTRY(\label)
476 CFI_STARTPROC
76f5df43 477 DEFAULT_FRAME 0, 8 /* offset 8: return address */
b3af11af
AV
478 FIXUP_TOP_OF_STACK %r11, 8-ARGOFFSET
479 call \func
480 RESTORE_TOP_OF_STACK %r11, 8-ARGOFFSET
481 ret
482 CFI_ENDPROC
483END(\label)
484 .endm
485
1d4b4b29
AV
486 FORK_LIKE clone
487 FORK_LIKE fork
488 FORK_LIKE vfork
b3af11af 489 FIXED_FRAME stub_iopl, sys_iopl
1da177e4 490
1da177e4
LT
491ENTRY(stub_execve)
492 CFI_STARTPROC
e6b04b6b 493 addq $8, %rsp
76f5df43
DV
494 DEFAULT_FRAME 0
495 SAVE_EXTRA_REGS
1da177e4
LT
496 FIXUP_TOP_OF_STACK %r11
497 call sys_execve
1da177e4 498 movq %rax,RAX(%rsp)
76f5df43 499 RESTORE_EXTRA_REGS
1da177e4
LT
500 jmp int_ret_from_sys_call
501 CFI_ENDPROC
4b787e0b 502END(stub_execve)
0bd7b798 503
27d6ec7a
DD
504ENTRY(stub_execveat)
505 CFI_STARTPROC
506 addq $8, %rsp
76f5df43
DV
507 DEFAULT_FRAME 0
508 SAVE_EXTRA_REGS
27d6ec7a
DD
509 FIXUP_TOP_OF_STACK %r11
510 call sys_execveat
511 RESTORE_TOP_OF_STACK %r11
512 movq %rax,RAX(%rsp)
76f5df43 513 RESTORE_EXTRA_REGS
27d6ec7a
DD
514 jmp int_ret_from_sys_call
515 CFI_ENDPROC
516END(stub_execveat)
517
1da177e4
LT
518/*
519 * sigreturn is special because it needs to restore all registers on return.
520 * This cannot be done with SYSRET, so use the IRET return path instead.
0bd7b798 521 */
1da177e4
LT
522ENTRY(stub_rt_sigreturn)
523 CFI_STARTPROC
7effaa88 524 addq $8, %rsp
76f5df43
DV
525 DEFAULT_FRAME 0
526 SAVE_EXTRA_REGS
1da177e4
LT
527 FIXUP_TOP_OF_STACK %r11
528 call sys_rt_sigreturn
529 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
76f5df43 530 RESTORE_EXTRA_REGS
1da177e4
LT
531 jmp int_ret_from_sys_call
532 CFI_ENDPROC
4b787e0b 533END(stub_rt_sigreturn)
1da177e4 534
c5a37394 535#ifdef CONFIG_X86_X32_ABI
c5a37394
PA
536ENTRY(stub_x32_rt_sigreturn)
537 CFI_STARTPROC
538 addq $8, %rsp
76f5df43
DV
539 DEFAULT_FRAME 0
540 SAVE_EXTRA_REGS
c5a37394
PA
541 FIXUP_TOP_OF_STACK %r11
542 call sys32_x32_rt_sigreturn
543 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
76f5df43 544 RESTORE_EXTRA_REGS
c5a37394
PA
545 jmp int_ret_from_sys_call
546 CFI_ENDPROC
547END(stub_x32_rt_sigreturn)
548
d1a797f3
PA
549ENTRY(stub_x32_execve)
550 CFI_STARTPROC
551 addq $8, %rsp
76f5df43
DV
552 DEFAULT_FRAME 0
553 SAVE_EXTRA_REGS
d1a797f3 554 FIXUP_TOP_OF_STACK %r11
6783eaa2 555 call compat_sys_execve
d1a797f3
PA
556 RESTORE_TOP_OF_STACK %r11
557 movq %rax,RAX(%rsp)
76f5df43 558 RESTORE_EXTRA_REGS
d1a797f3
PA
559 jmp int_ret_from_sys_call
560 CFI_ENDPROC
561END(stub_x32_execve)
562
27d6ec7a
DD
563ENTRY(stub_x32_execveat)
564 CFI_STARTPROC
565 addq $8, %rsp
76f5df43
DV
566 DEFAULT_FRAME 0
567 SAVE_EXTRA_REGS
27d6ec7a
DD
568 FIXUP_TOP_OF_STACK %r11
569 call compat_sys_execveat
570 RESTORE_TOP_OF_STACK %r11
571 movq %rax,RAX(%rsp)
76f5df43 572 RESTORE_EXTRA_REGS
27d6ec7a
DD
573 jmp int_ret_from_sys_call
574 CFI_ENDPROC
575END(stub_x32_execveat)
576
c5a37394
PA
577#endif
578
939b7871
PA
579/*
580 * Build the entry stubs and pointer table with some assembler magic.
581 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
582 * single cache line on all modern x86 implementations.
583 */
584 .section .init.rodata,"a"
585ENTRY(interrupt)
ea714547 586 .section .entry.text
939b7871
PA
587 .p2align 5
588 .p2align CONFIG_X86_L1_CACHE_SHIFT
589ENTRY(irq_entries_start)
590 INTR_FRAME
591vector=FIRST_EXTERNAL_VECTOR
2414e021 592.rept (FIRST_SYSTEM_VECTOR-FIRST_EXTERNAL_VECTOR+6)/7
939b7871
PA
593 .balign 32
594 .rept 7
2414e021 595 .if vector < FIRST_SYSTEM_VECTOR
8665596e 596 .if vector <> FIRST_EXTERNAL_VECTOR
939b7871
PA
597 CFI_ADJUST_CFA_OFFSET -8
598 .endif
df5d1874 5991: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
8665596e 600 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
939b7871
PA
601 jmp 2f
602 .endif
603 .previous
604 .quad 1b
ea714547 605 .section .entry.text
939b7871
PA
606vector=vector+1
607 .endif
608 .endr
6092: jmp common_interrupt
610.endr
611 CFI_ENDPROC
612END(irq_entries_start)
613
614.previous
615END(interrupt)
616.previous
617
d99015b1 618/*
1da177e4
LT
619 * Interrupt entry/exit.
620 *
621 * Interrupt entry points save only callee clobbered registers in fast path.
d99015b1
AH
622 *
623 * Entry runs with interrupts off.
624 */
1da177e4 625
722024db 626/* 0(%rsp): ~(interrupt number) */
1da177e4 627 .macro interrupt func
f6f64681 628 cld
e90e147c
DV
629 /*
630 * Since nothing in interrupt handling code touches r12...r15 members
631 * of "struct pt_regs", and since interrupts can nest, we can save
632 * four stack slots and simultaneously provide
633 * an unwind-friendly stack layout by saving "truncated" pt_regs
634 * exactly up to rbp slot, without these members.
635 */
76f5df43
DV
636 ALLOC_PT_GPREGS_ON_STACK -RBP
637 SAVE_C_REGS -RBP
638 /* this goes to 0(%rsp) for unwinder, not for saving the value: */
639 SAVE_EXTRA_REGS_RBP -RBP
640
641 leaq -RBP(%rsp),%rdi /* arg1 for \func (pointer to pt_regs) */
f6f64681 642
76f5df43 643 testl $3, CS-RBP(%rsp)
f6f64681
DV
644 je 1f
645 SWAPGS
76f5df43 6461:
f6f64681 647 /*
e90e147c 648 * Save previous stack pointer, optionally switch to interrupt stack.
f6f64681
DV
649 * irq_count is used to check if a CPU is already on an interrupt stack
650 * or not. While this is essentially redundant with preempt_count it is
651 * a little cheaper to use a separate counter in the PDA (short of
652 * moving irq_enter into assembly, which would be too much work)
653 */
76f5df43
DV
654 movq %rsp, %rsi
655 incl PER_CPU_VAR(irq_count)
f6f64681
DV
656 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
657 CFI_DEF_CFA_REGISTER rsi
f6f64681
DV
658 pushq %rsi
659 CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
660 0x77 /* DW_OP_breg7 */, 0, \
661 0x06 /* DW_OP_deref */, \
662 0x08 /* DW_OP_const1u */, SS+8-RBP, \
663 0x22 /* DW_OP_plus */
664 /* We entered an interrupt context - irqs are off: */
665 TRACE_IRQS_OFF
666
1da177e4
LT
667 call \func
668 .endm
669
722024db
AH
670 /*
671 * The interrupt stubs push (~vector+0x80) onto the stack and
672 * then jump to common_interrupt.
673 */
939b7871
PA
674 .p2align CONFIG_X86_L1_CACHE_SHIFT
675common_interrupt:
7effaa88 676 XCPT_FRAME
ee4eb87b 677 ASM_CLAC
722024db 678 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
1da177e4 679 interrupt do_IRQ
3d1e42a7 680 /* 0(%rsp): old_rsp-ARGOFFSET */
7effaa88 681ret_from_intr:
72fe4858 682 DISABLE_INTERRUPTS(CLBR_NONE)
2601e64d 683 TRACE_IRQS_OFF
56895530 684 decl PER_CPU_VAR(irq_count)
625dbc3b 685
a2bbe750
FW
686 /* Restore saved previous stack */
687 popq %rsi
928282e4 688 CFI_DEF_CFA rsi,SS+8-RBP /* reg/off reset after def_cfa_expr */
e90e147c 689 /* return code expects complete pt_regs - adjust rsp accordingly: */
eab9e613 690 leaq ARGOFFSET-RBP(%rsi), %rsp
7effaa88 691 CFI_DEF_CFA_REGISTER rsp
eab9e613 692 CFI_ADJUST_CFA_OFFSET RBP-ARGOFFSET
625dbc3b 693
7effaa88 694exit_intr:
1da177e4
LT
695 GET_THREAD_INFO(%rcx)
696 testl $3,CS-ARGOFFSET(%rsp)
697 je retint_kernel
0bd7b798 698
1da177e4
LT
699 /* Interrupt came from user space */
700 /*
e90e147c 701 * Has a correct top of stack.
1da177e4 702 * %rcx: thread info. Interrupts off.
0bd7b798 703 */
1da177e4
LT
704retint_with_reschedule:
705 movl $_TIF_WORK_MASK,%edi
7effaa88 706retint_check:
10cd706d 707 LOCKDEP_SYS_EXIT_IRQ
26ccb8a7 708 movl TI_flags(%rcx),%edx
1da177e4 709 andl %edi,%edx
7effaa88 710 CFI_REMEMBER_STATE
1da177e4 711 jnz retint_careful
10cd706d
PZ
712
713retint_swapgs: /* return to user-space */
2601e64d
IM
714 /*
715 * The iretq could re-enable interrupts:
716 */
72fe4858 717 DISABLE_INTERRUPTS(CLBR_ANY)
2601e64d 718 TRACE_IRQS_IRETQ
2a23c6b8
AL
719
720 /*
721 * Try to use SYSRET instead of IRET if we're returning to
722 * a completely clean 64-bit userspace context.
723 */
6e1327bd
DV
724 movq (RCX-ARGOFFSET)(%rsp), %rcx
725 cmpq %rcx,(RIP-ARGOFFSET)(%rsp) /* RCX == RIP */
2a23c6b8
AL
726 jne opportunistic_sysret_failed
727
728 /*
729 * On Intel CPUs, sysret with non-canonical RCX/RIP will #GP
730 * in kernel space. This essentially lets the user take over
731 * the kernel, since userspace controls RSP. It's not worth
732 * testing for canonicalness exactly -- this check detects any
733 * of the 17 high bits set, which is true for non-canonical
734 * or kernel addresses. (This will pessimize vsyscall=native.
735 * Big deal.)
736 *
737 * If virtual addresses ever become wider, this will need
738 * to be updated to remain correct on both old and new CPUs.
739 */
740 .ifne __VIRTUAL_MASK_SHIFT - 47
741 .error "virtual address width changed -- sysret checks need update"
742 .endif
743 shr $__VIRTUAL_MASK_SHIFT, %rcx
744 jnz opportunistic_sysret_failed
745
6e1327bd 746 cmpq $__USER_CS,(CS-ARGOFFSET)(%rsp) /* CS must match SYSRET */
2a23c6b8
AL
747 jne opportunistic_sysret_failed
748
749 movq (R11-ARGOFFSET)(%rsp), %r11
750 cmpq %r11,(EFLAGS-ARGOFFSET)(%rsp) /* R11 == RFLAGS */
751 jne opportunistic_sysret_failed
752
753 testq $X86_EFLAGS_RF,%r11 /* sysret can't restore RF */
754 jnz opportunistic_sysret_failed
755
756 /* nothing to check for RSP */
757
758 cmpq $__USER_DS,(SS-ARGOFFSET)(%rsp) /* SS must match SYSRET */
759 jne opportunistic_sysret_failed
760
761 /*
762 * We win! This label is here just for ease of understanding
763 * perf profiles. Nothing jumps here.
764 */
765irq_return_via_sysret:
766 CFI_REMEMBER_STATE
76f5df43
DV
767 RESTORE_C_REGS
768 REMOVE_PT_GPREGS_FROM_STACK 8
2a23c6b8
AL
769 movq (RSP-RIP)(%rsp),%rsp
770 USERGS_SYSRET64
771 CFI_RESTORE_STATE
772
773opportunistic_sysret_failed:
72fe4858 774 SWAPGS
2601e64d
IM
775 jmp restore_args
776
10cd706d 777retint_restore_args: /* return to kernel space */
72fe4858 778 DISABLE_INTERRUPTS(CLBR_ANY)
2601e64d
IM
779 /*
780 * The iretq could re-enable interrupts:
781 */
782 TRACE_IRQS_IRETQ
783restore_args:
76f5df43
DV
784 RESTORE_C_REGS
785 REMOVE_PT_GPREGS_FROM_STACK 8
3701d863 786
f7f3d791 787irq_return:
7209a75d
AL
788 INTERRUPT_RETURN
789
790ENTRY(native_iret)
3891a04a
PA
791 /*
792 * Are we returning to a stack segment from the LDT? Note: in
793 * 64-bit mode SS:RSP on the exception stack is always valid.
794 */
34273f41 795#ifdef CONFIG_X86_ESPFIX64
3891a04a 796 testb $4,(SS-RIP)(%rsp)
7209a75d 797 jnz native_irq_return_ldt
34273f41 798#endif
3891a04a 799
af726f21 800.global native_irq_return_iret
7209a75d 801native_irq_return_iret:
b645af2d
AL
802 /*
803 * This may fault. Non-paranoid faults on return to userspace are
804 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
805 * Double-faults due to espfix64 are handled in do_double_fault.
806 * Other faults here are fatal.
807 */
1da177e4 808 iretq
3701d863 809
34273f41 810#ifdef CONFIG_X86_ESPFIX64
7209a75d 811native_irq_return_ldt:
3891a04a
PA
812 pushq_cfi %rax
813 pushq_cfi %rdi
814 SWAPGS
815 movq PER_CPU_VAR(espfix_waddr),%rdi
816 movq %rax,(0*8)(%rdi) /* RAX */
817 movq (2*8)(%rsp),%rax /* RIP */
818 movq %rax,(1*8)(%rdi)
819 movq (3*8)(%rsp),%rax /* CS */
820 movq %rax,(2*8)(%rdi)
821 movq (4*8)(%rsp),%rax /* RFLAGS */
822 movq %rax,(3*8)(%rdi)
823 movq (6*8)(%rsp),%rax /* SS */
824 movq %rax,(5*8)(%rdi)
825 movq (5*8)(%rsp),%rax /* RSP */
826 movq %rax,(4*8)(%rdi)
827 andl $0xffff0000,%eax
828 popq_cfi %rdi
829 orq PER_CPU_VAR(espfix_stack),%rax
830 SWAPGS
831 movq %rax,%rsp
832 popq_cfi %rax
7209a75d 833 jmp native_irq_return_iret
34273f41 834#endif
3891a04a 835
7effaa88 836 /* edi: workmask, edx: work */
1da177e4 837retint_careful:
7effaa88 838 CFI_RESTORE_STATE
1da177e4
LT
839 bt $TIF_NEED_RESCHED,%edx
840 jnc retint_signal
2601e64d 841 TRACE_IRQS_ON
72fe4858 842 ENABLE_INTERRUPTS(CLBR_NONE)
df5d1874 843 pushq_cfi %rdi
0430499c 844 SCHEDULE_USER
df5d1874 845 popq_cfi %rdi
1da177e4 846 GET_THREAD_INFO(%rcx)
72fe4858 847 DISABLE_INTERRUPTS(CLBR_NONE)
2601e64d 848 TRACE_IRQS_OFF
1da177e4 849 jmp retint_check
0bd7b798 850
1da177e4 851retint_signal:
8f4d37ec 852 testl $_TIF_DO_NOTIFY_MASK,%edx
10ffdbb8 853 jz retint_swapgs
2601e64d 854 TRACE_IRQS_ON
72fe4858 855 ENABLE_INTERRUPTS(CLBR_NONE)
76f5df43 856 SAVE_EXTRA_REGS
0bd7b798 857 movq $-1,ORIG_RAX(%rsp)
3829ee6b 858 xorl %esi,%esi # oldset
1da177e4
LT
859 movq %rsp,%rdi # &pt_regs
860 call do_notify_resume
76f5df43 861 RESTORE_EXTRA_REGS
72fe4858 862 DISABLE_INTERRUPTS(CLBR_NONE)
2601e64d 863 TRACE_IRQS_OFF
be9e6870 864 GET_THREAD_INFO(%rcx)
eca91e78 865 jmp retint_with_reschedule
1da177e4
LT
866
867#ifdef CONFIG_PREEMPT
868 /* Returning to kernel space. Check if we need preemption */
869 /* rcx: threadinfo. interrupts off. */
b06babac 870ENTRY(retint_kernel)
c2daa3be 871 cmpl $0,PER_CPU_VAR(__preempt_count)
1da177e4 872 jnz retint_restore_args
1da177e4
LT
873 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
874 jnc retint_restore_args
875 call preempt_schedule_irq
876 jmp exit_intr
0bd7b798 877#endif
1da177e4 878 CFI_ENDPROC
4b787e0b 879END(common_interrupt)
3891a04a 880
1da177e4
LT
881/*
882 * APIC interrupts.
0bd7b798 883 */
cf910e83 884.macro apicinterrupt3 num sym do_sym
322648d1 885ENTRY(\sym)
7effaa88 886 INTR_FRAME
ee4eb87b 887 ASM_CLAC
df5d1874 888 pushq_cfi $~(\num)
39e95433 889.Lcommon_\sym:
322648d1 890 interrupt \do_sym
1da177e4
LT
891 jmp ret_from_intr
892 CFI_ENDPROC
322648d1
AH
893END(\sym)
894.endm
1da177e4 895
cf910e83
SA
896#ifdef CONFIG_TRACING
897#define trace(sym) trace_##sym
898#define smp_trace(sym) smp_trace_##sym
899
900.macro trace_apicinterrupt num sym
901apicinterrupt3 \num trace(\sym) smp_trace(\sym)
902.endm
903#else
904.macro trace_apicinterrupt num sym do_sym
905.endm
906#endif
907
908.macro apicinterrupt num sym do_sym
909apicinterrupt3 \num \sym \do_sym
910trace_apicinterrupt \num \sym
911.endm
912
322648d1 913#ifdef CONFIG_SMP
cf910e83 914apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR \
322648d1 915 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
cf910e83 916apicinterrupt3 REBOOT_VECTOR \
4ef702c1 917 reboot_interrupt smp_reboot_interrupt
322648d1 918#endif
1da177e4 919
03b48632 920#ifdef CONFIG_X86_UV
cf910e83 921apicinterrupt3 UV_BAU_MESSAGE \
322648d1 922 uv_bau_message_intr1 uv_bau_message_interrupt
03b48632 923#endif
322648d1
AH
924apicinterrupt LOCAL_TIMER_VECTOR \
925 apic_timer_interrupt smp_apic_timer_interrupt
4a4de9c7
DS
926apicinterrupt X86_PLATFORM_IPI_VECTOR \
927 x86_platform_ipi smp_x86_platform_ipi
89b831ef 928
d78f2664 929#ifdef CONFIG_HAVE_KVM
cf910e83 930apicinterrupt3 POSTED_INTR_VECTOR \
d78f2664
YZ
931 kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
932#endif
933
33e5ff63 934#ifdef CONFIG_X86_MCE_THRESHOLD
322648d1 935apicinterrupt THRESHOLD_APIC_VECTOR \
7856f6cc 936 threshold_interrupt smp_threshold_interrupt
33e5ff63
SA
937#endif
938
939#ifdef CONFIG_X86_THERMAL_VECTOR
322648d1
AH
940apicinterrupt THERMAL_APIC_VECTOR \
941 thermal_interrupt smp_thermal_interrupt
33e5ff63 942#endif
1812924b 943
322648d1
AH
944#ifdef CONFIG_SMP
945apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
946 call_function_single_interrupt smp_call_function_single_interrupt
947apicinterrupt CALL_FUNCTION_VECTOR \
948 call_function_interrupt smp_call_function_interrupt
949apicinterrupt RESCHEDULE_VECTOR \
950 reschedule_interrupt smp_reschedule_interrupt
951#endif
1da177e4 952
322648d1
AH
953apicinterrupt ERROR_APIC_VECTOR \
954 error_interrupt smp_error_interrupt
955apicinterrupt SPURIOUS_APIC_VECTOR \
956 spurious_interrupt smp_spurious_interrupt
0bd7b798 957
e360adbe
PZ
958#ifdef CONFIG_IRQ_WORK
959apicinterrupt IRQ_WORK_VECTOR \
960 irq_work_interrupt smp_irq_work_interrupt
241771ef
IM
961#endif
962
1da177e4
LT
963/*
964 * Exception entry points.
0bd7b798 965 */
577ed45e
AL
966#define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
967
968.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
322648d1 969ENTRY(\sym)
577ed45e
AL
970 /* Sanity check */
971 .if \shift_ist != -1 && \paranoid == 0
972 .error "using shift_ist requires paranoid=1"
973 .endif
974
cb5dd2c5
AL
975 .if \has_error_code
976 XCPT_FRAME
977 .else
7effaa88 978 INTR_FRAME
cb5dd2c5 979 .endif
1da177e4 980
ee4eb87b 981 ASM_CLAC
b8b1d08b 982 PARAVIRT_ADJUST_EXCEPTION_FRAME
cb5dd2c5
AL
983
984 .ifeq \has_error_code
985 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
986 .endif
987
76f5df43 988 ALLOC_PT_GPREGS_ON_STACK
cb5dd2c5
AL
989
990 .if \paranoid
48e08d0f
AL
991 .if \paranoid == 1
992 CFI_REMEMBER_STATE
993 testl $3, CS(%rsp) /* If coming from userspace, switch */
994 jnz 1f /* stacks. */
995 .endif
b8b1d08b 996 call save_paranoid
cb5dd2c5
AL
997 .else
998 call error_entry
999 .endif
1000
1bd24efc 1001 DEFAULT_FRAME 0
cb5dd2c5
AL
1002
1003 .if \paranoid
577ed45e
AL
1004 .if \shift_ist != -1
1005 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
1006 .else
b8b1d08b 1007 TRACE_IRQS_OFF
cb5dd2c5 1008 .endif
577ed45e 1009 .endif
cb5dd2c5
AL
1010
1011 movq %rsp,%rdi /* pt_regs pointer */
1012
1013 .if \has_error_code
1014 movq ORIG_RAX(%rsp),%rsi /* get error code */
1015 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1016 .else
1017 xorl %esi,%esi /* no error code */
1018 .endif
1019
577ed45e
AL
1020 .if \shift_ist != -1
1021 subq $EXCEPTION_STKSZ, INIT_TSS_IST(\shift_ist)
1022 .endif
1023
322648d1 1024 call \do_sym
cb5dd2c5 1025
577ed45e
AL
1026 .if \shift_ist != -1
1027 addq $EXCEPTION_STKSZ, INIT_TSS_IST(\shift_ist)
1028 .endif
1029
cb5dd2c5
AL
1030 .if \paranoid
1031 jmp paranoid_exit /* %ebx: no swapgs flag */
1032 .else
1033 jmp error_exit /* %ebx: no swapgs flag */
1034 .endif
1035
48e08d0f
AL
1036 .if \paranoid == 1
1037 CFI_RESTORE_STATE
1038 /*
1039 * Paranoid entry from userspace. Switch stacks and treat it
1040 * as a normal entry. This means that paranoid handlers
1041 * run in real process context if user_mode(regs).
1042 */
10431:
1044 call error_entry
1045
1046 DEFAULT_FRAME 0
1047
1048 movq %rsp,%rdi /* pt_regs pointer */
1049 call sync_regs
1050 movq %rax,%rsp /* switch stack */
1051
1052 movq %rsp,%rdi /* pt_regs pointer */
1053
1054 .if \has_error_code
1055 movq ORIG_RAX(%rsp),%rsi /* get error code */
1056 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1057 .else
1058 xorl %esi,%esi /* no error code */
1059 .endif
1060
1061 call \do_sym
1062
1063 jmp error_exit /* %ebx: no swapgs flag */
1064 .endif
1065
b8b1d08b 1066 CFI_ENDPROC
ddeb8f21 1067END(\sym)
322648d1 1068.endm
b8b1d08b 1069
25c74b10 1070#ifdef CONFIG_TRACING
cb5dd2c5
AL
1071.macro trace_idtentry sym do_sym has_error_code:req
1072idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
1073idtentry \sym \do_sym has_error_code=\has_error_code
25c74b10
SA
1074.endm
1075#else
cb5dd2c5
AL
1076.macro trace_idtentry sym do_sym has_error_code:req
1077idtentry \sym \do_sym has_error_code=\has_error_code
25c74b10
SA
1078.endm
1079#endif
1080
cb5dd2c5
AL
1081idtentry divide_error do_divide_error has_error_code=0
1082idtentry overflow do_overflow has_error_code=0
1083idtentry bounds do_bounds has_error_code=0
1084idtentry invalid_op do_invalid_op has_error_code=0
1085idtentry device_not_available do_device_not_available has_error_code=0
48e08d0f 1086idtentry double_fault do_double_fault has_error_code=1 paranoid=2
cb5dd2c5
AL
1087idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
1088idtentry invalid_TSS do_invalid_TSS has_error_code=1
1089idtentry segment_not_present do_segment_not_present has_error_code=1
1090idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
1091idtentry coprocessor_error do_coprocessor_error has_error_code=0
1092idtentry alignment_check do_alignment_check has_error_code=1
1093idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
5cec93c2 1094
2601e64d 1095
9f1e87ea
CG
1096 /* Reload gs selector with exception handling */
1097 /* edi: new selector */
9f9d489a 1098ENTRY(native_load_gs_index)
7effaa88 1099 CFI_STARTPROC
df5d1874 1100 pushfq_cfi
b8aa287f 1101 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
9f1e87ea 1102 SWAPGS
0bd7b798 1103gs_change:
9f1e87ea 1104 movl %edi,%gs
1da177e4 11052: mfence /* workaround */
72fe4858 1106 SWAPGS
df5d1874 1107 popfq_cfi
9f1e87ea 1108 ret
7effaa88 1109 CFI_ENDPROC
6efdcfaf 1110END(native_load_gs_index)
0bd7b798 1111
d7abc0fa 1112 _ASM_EXTABLE(gs_change,bad_gs)
9f1e87ea 1113 .section .fixup,"ax"
1da177e4 1114 /* running with kernelgs */
0bd7b798 1115bad_gs:
72fe4858 1116 SWAPGS /* switch back to user gs */
1da177e4 1117 xorl %eax,%eax
9f1e87ea
CG
1118 movl %eax,%gs
1119 jmp 2b
1120 .previous
0bd7b798 1121
2699500b 1122/* Call softirq on interrupt stack. Interrupts are off. */
7d65f4a6 1123ENTRY(do_softirq_own_stack)
7effaa88 1124 CFI_STARTPROC
df5d1874 1125 pushq_cfi %rbp
2699500b
AK
1126 CFI_REL_OFFSET rbp,0
1127 mov %rsp,%rbp
1128 CFI_DEF_CFA_REGISTER rbp
56895530 1129 incl PER_CPU_VAR(irq_count)
26f80bd6 1130 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
2699500b 1131 push %rbp # backlink for old unwinder
ed6b676c 1132 call __do_softirq
2699500b 1133 leaveq
df5d1874 1134 CFI_RESTORE rbp
7effaa88 1135 CFI_DEF_CFA_REGISTER rsp
2699500b 1136 CFI_ADJUST_CFA_OFFSET -8
56895530 1137 decl PER_CPU_VAR(irq_count)
ed6b676c 1138 ret
7effaa88 1139 CFI_ENDPROC
7d65f4a6 1140END(do_softirq_own_stack)
75154f40 1141
3d75e1b8 1142#ifdef CONFIG_XEN
cb5dd2c5 1143idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
3d75e1b8
JF
1144
1145/*
9f1e87ea
CG
1146 * A note on the "critical region" in our callback handler.
1147 * We want to avoid stacking callback handlers due to events occurring
1148 * during handling of the last event. To do this, we keep events disabled
1149 * until we've done all processing. HOWEVER, we must enable events before
1150 * popping the stack frame (can't be done atomically) and so it would still
1151 * be possible to get enough handler activations to overflow the stack.
1152 * Although unlikely, bugs of that kind are hard to track down, so we'd
1153 * like to avoid the possibility.
1154 * So, on entry to the handler we detect whether we interrupted an
1155 * existing activation in its critical region -- if so, we pop the current
1156 * activation and restart the handler using the previous one.
1157 */
3d75e1b8
JF
1158ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1159 CFI_STARTPROC
9f1e87ea
CG
1160/*
1161 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1162 * see the correct pointer to the pt_regs
1163 */
3d75e1b8
JF
1164 movq %rdi, %rsp # we don't return, adjust the stack frame
1165 CFI_ENDPROC
dcd072e2 1166 DEFAULT_FRAME
56895530 116711: incl PER_CPU_VAR(irq_count)
3d75e1b8
JF
1168 movq %rsp,%rbp
1169 CFI_DEF_CFA_REGISTER rbp
26f80bd6 1170 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
3d75e1b8
JF
1171 pushq %rbp # backlink for old unwinder
1172 call xen_evtchn_do_upcall
1173 popq %rsp
1174 CFI_DEF_CFA_REGISTER rsp
56895530 1175 decl PER_CPU_VAR(irq_count)
fdfd811d
DV
1176#ifndef CONFIG_PREEMPT
1177 call xen_maybe_preempt_hcall
1178#endif
3d75e1b8
JF
1179 jmp error_exit
1180 CFI_ENDPROC
371c394a 1181END(xen_do_hypervisor_callback)
3d75e1b8
JF
1182
1183/*
9f1e87ea
CG
1184 * Hypervisor uses this for application faults while it executes.
1185 * We get here for two reasons:
1186 * 1. Fault while reloading DS, ES, FS or GS
1187 * 2. Fault while executing IRET
1188 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1189 * registers that could be reloaded and zeroed the others.
1190 * Category 2 we fix up by killing the current process. We cannot use the
1191 * normal Linux return path in this case because if we use the IRET hypercall
1192 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1193 * We distinguish between categories by comparing each saved segment register
1194 * with its current contents: any discrepancy means we in category 1.
1195 */
3d75e1b8 1196ENTRY(xen_failsafe_callback)
dcd072e2
AH
1197 INTR_FRAME 1 (6*8)
1198 /*CFI_REL_OFFSET gs,GS*/
1199 /*CFI_REL_OFFSET fs,FS*/
1200 /*CFI_REL_OFFSET es,ES*/
1201 /*CFI_REL_OFFSET ds,DS*/
1202 CFI_REL_OFFSET r11,8
1203 CFI_REL_OFFSET rcx,0
3d75e1b8
JF
1204 movw %ds,%cx
1205 cmpw %cx,0x10(%rsp)
1206 CFI_REMEMBER_STATE
1207 jne 1f
1208 movw %es,%cx
1209 cmpw %cx,0x18(%rsp)
1210 jne 1f
1211 movw %fs,%cx
1212 cmpw %cx,0x20(%rsp)
1213 jne 1f
1214 movw %gs,%cx
1215 cmpw %cx,0x28(%rsp)
1216 jne 1f
1217 /* All segments match their saved values => Category 2 (Bad IRET). */
1218 movq (%rsp),%rcx
1219 CFI_RESTORE rcx
1220 movq 8(%rsp),%r11
1221 CFI_RESTORE r11
1222 addq $0x30,%rsp
1223 CFI_ADJUST_CFA_OFFSET -0x30
14ae22ba
IM
1224 pushq_cfi $0 /* RIP */
1225 pushq_cfi %r11
1226 pushq_cfi %rcx
4a5c3e77 1227 jmp general_protection
3d75e1b8
JF
1228 CFI_RESTORE_STATE
12291: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1230 movq (%rsp),%rcx
1231 CFI_RESTORE rcx
1232 movq 8(%rsp),%r11
1233 CFI_RESTORE r11
1234 addq $0x30,%rsp
1235 CFI_ADJUST_CFA_OFFSET -0x30
a349e23d 1236 pushq_cfi $-1 /* orig_ax = -1 => not a system call */
76f5df43
DV
1237 ALLOC_PT_GPREGS_ON_STACK
1238 SAVE_C_REGS
1239 SAVE_EXTRA_REGS
3d75e1b8
JF
1240 jmp error_exit
1241 CFI_ENDPROC
3d75e1b8
JF
1242END(xen_failsafe_callback)
1243
cf910e83 1244apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
38e20b07
SY
1245 xen_hvm_callback_vector xen_evtchn_do_upcall
1246
3d75e1b8 1247#endif /* CONFIG_XEN */
ddeb8f21 1248
bc2b0331 1249#if IS_ENABLED(CONFIG_HYPERV)
cf910e83 1250apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
bc2b0331
S
1251 hyperv_callback_vector hyperv_vector_handler
1252#endif /* CONFIG_HYPERV */
1253
577ed45e
AL
1254idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1255idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
6f442be2 1256idtentry stack_segment do_stack_segment has_error_code=1
6cac5a92 1257#ifdef CONFIG_XEN
cb5dd2c5
AL
1258idtentry xen_debug do_debug has_error_code=0
1259idtentry xen_int3 do_int3 has_error_code=0
1260idtentry xen_stack_segment do_stack_segment has_error_code=1
6cac5a92 1261#endif
cb5dd2c5
AL
1262idtentry general_protection do_general_protection has_error_code=1
1263trace_idtentry page_fault do_page_fault has_error_code=1
631bc487 1264#ifdef CONFIG_KVM_GUEST
cb5dd2c5 1265idtentry async_page_fault do_async_page_fault has_error_code=1
631bc487 1266#endif
ddeb8f21 1267#ifdef CONFIG_X86_MCE
cb5dd2c5 1268idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
ddeb8f21
AH
1269#endif
1270
1271 /*
48e08d0f
AL
1272 * "Paranoid" exit path from exception stack. This is invoked
1273 * only on return from non-NMI IST interrupts that came
1274 * from kernel space.
ddeb8f21 1275 *
48e08d0f
AL
1276 * We may be returning to very strange contexts (e.g. very early
1277 * in syscall entry), so checking for preemption here would
1278 * be complicated. Fortunately, we there's no good reason
1279 * to try to handle preemption here.
ddeb8f21
AH
1280 */
1281
1282 /* ebx: no swapgs flag */
1283ENTRY(paranoid_exit)
1f130a78 1284 DEFAULT_FRAME
ddeb8f21 1285 DISABLE_INTERRUPTS(CLBR_NONE)
5963e317 1286 TRACE_IRQS_OFF_DEBUG
ddeb8f21 1287 testl %ebx,%ebx /* swapgs needed? */
0d550836 1288 jnz paranoid_exit_no_swapgs
ddeb8f21
AH
1289 TRACE_IRQS_IRETQ 0
1290 SWAPGS_UNSAFE_STACK
0d550836
DV
1291 jmp paranoid_exit_restore
1292paranoid_exit_no_swapgs:
5963e317 1293 TRACE_IRQS_IRETQ_DEBUG 0
0d550836 1294paranoid_exit_restore:
76f5df43
DV
1295 RESTORE_EXTRA_REGS
1296 RESTORE_C_REGS
1297 REMOVE_PT_GPREGS_FROM_STACK 8
48e08d0f 1298 INTERRUPT_RETURN
ddeb8f21
AH
1299 CFI_ENDPROC
1300END(paranoid_exit)
1301
1302/*
1303 * Exception entry point. This expects an error code/orig_rax on the stack.
1304 * returns in "no swapgs flag" in %ebx.
1305 */
1306ENTRY(error_entry)
1307 XCPT_FRAME
1308 CFI_ADJUST_CFA_OFFSET 15*8
1309 /* oldrax contains error code */
1310 cld
76f5df43
DV
1311 SAVE_C_REGS 8
1312 SAVE_EXTRA_REGS 8
ddeb8f21
AH
1313 xorl %ebx,%ebx
1314 testl $3,CS+8(%rsp)
1315 je error_kernelspace
1316error_swapgs:
1317 SWAPGS
1318error_sti:
1319 TRACE_IRQS_OFF
1320 ret
ddeb8f21
AH
1321
1322/*
1323 * There are two places in the kernel that can potentially fault with
b645af2d
AL
1324 * usergs. Handle them here. B stepping K8s sometimes report a
1325 * truncated RIP for IRET exceptions returning to compat mode. Check
1326 * for these here too.
ddeb8f21
AH
1327 */
1328error_kernelspace:
3bab13b0 1329 CFI_REL_OFFSET rcx, RCX+8
ddeb8f21 1330 incl %ebx
7209a75d 1331 leaq native_irq_return_iret(%rip),%rcx
ddeb8f21 1332 cmpq %rcx,RIP+8(%rsp)
b645af2d 1333 je error_bad_iret
ae24ffe5
BG
1334 movl %ecx,%eax /* zero extend */
1335 cmpq %rax,RIP+8(%rsp)
1336 je bstep_iret
ddeb8f21 1337 cmpq $gs_change,RIP+8(%rsp)
9f1e87ea 1338 je error_swapgs
ddeb8f21 1339 jmp error_sti
ae24ffe5
BG
1340
1341bstep_iret:
1342 /* Fix truncated RIP */
1343 movq %rcx,RIP+8(%rsp)
b645af2d
AL
1344 /* fall through */
1345
1346error_bad_iret:
1347 SWAPGS
1348 mov %rsp,%rdi
1349 call fixup_bad_iret
1350 mov %rax,%rsp
1351 decl %ebx /* Return to usergs */
1352 jmp error_sti
e6b04b6b 1353 CFI_ENDPROC
ddeb8f21
AH
1354END(error_entry)
1355
1356
1357/* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1358ENTRY(error_exit)
1359 DEFAULT_FRAME
1360 movl %ebx,%eax
76f5df43 1361 RESTORE_EXTRA_REGS
ddeb8f21
AH
1362 DISABLE_INTERRUPTS(CLBR_NONE)
1363 TRACE_IRQS_OFF
1364 GET_THREAD_INFO(%rcx)
1365 testl %eax,%eax
1366 jne retint_kernel
1367 LOCKDEP_SYS_EXIT_IRQ
1368 movl TI_flags(%rcx),%edx
1369 movl $_TIF_WORK_MASK,%edi
1370 andl %edi,%edx
1371 jnz retint_careful
1372 jmp retint_swapgs
1373 CFI_ENDPROC
1374END(error_exit)
1375
3f3c8b8c
SR
1376/*
1377 * Test if a given stack is an NMI stack or not.
1378 */
1379 .macro test_in_nmi reg stack nmi_ret normal_ret
1380 cmpq %\reg, \stack
1381 ja \normal_ret
1382 subq $EXCEPTION_STKSZ, %\reg
1383 cmpq %\reg, \stack
1384 jb \normal_ret
1385 jmp \nmi_ret
1386 .endm
ddeb8f21
AH
1387
1388 /* runs on exception stack */
1389ENTRY(nmi)
1390 INTR_FRAME
1391 PARAVIRT_ADJUST_EXCEPTION_FRAME
3f3c8b8c
SR
1392 /*
1393 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1394 * the iretq it performs will take us out of NMI context.
1395 * This means that we can have nested NMIs where the next
1396 * NMI is using the top of the stack of the previous NMI. We
1397 * can't let it execute because the nested NMI will corrupt the
1398 * stack of the previous NMI. NMI handlers are not re-entrant
1399 * anyway.
1400 *
1401 * To handle this case we do the following:
1402 * Check the a special location on the stack that contains
1403 * a variable that is set when NMIs are executing.
1404 * The interrupted task's stack is also checked to see if it
1405 * is an NMI stack.
1406 * If the variable is not set and the stack is not the NMI
1407 * stack then:
1408 * o Set the special variable on the stack
1409 * o Copy the interrupt frame into a "saved" location on the stack
1410 * o Copy the interrupt frame into a "copy" location on the stack
1411 * o Continue processing the NMI
1412 * If the variable is set or the previous stack is the NMI stack:
1413 * o Modify the "copy" location to jump to the repeate_nmi
1414 * o return back to the first NMI
1415 *
1416 * Now on exit of the first NMI, we first clear the stack variable
1417 * The NMI stack will tell any nested NMIs at that point that it is
1418 * nested. Then we pop the stack normally with iret, and if there was
1419 * a nested NMI that updated the copy interrupt stack frame, a
1420 * jump will be made to the repeat_nmi code that will handle the second
1421 * NMI.
1422 */
1423
1424 /* Use %rdx as out temp variable throughout */
1425 pushq_cfi %rdx
62610913 1426 CFI_REL_OFFSET rdx, 0
3f3c8b8c 1427
45d5a168
SR
1428 /*
1429 * If %cs was not the kernel segment, then the NMI triggered in user
1430 * space, which means it is definitely not nested.
1431 */
a38449ef 1432 cmpl $__KERNEL_CS, 16(%rsp)
45d5a168
SR
1433 jne first_nmi
1434
3f3c8b8c
SR
1435 /*
1436 * Check the special variable on the stack to see if NMIs are
1437 * executing.
1438 */
a38449ef 1439 cmpl $1, -8(%rsp)
3f3c8b8c
SR
1440 je nested_nmi
1441
1442 /*
1443 * Now test if the previous stack was an NMI stack.
1444 * We need the double check. We check the NMI stack to satisfy the
1445 * race when the first NMI clears the variable before returning.
1446 * We check the variable because the first NMI could be in a
1447 * breakpoint routine using a breakpoint stack.
1448 */
1449 lea 6*8(%rsp), %rdx
1450 test_in_nmi rdx, 4*8(%rsp), nested_nmi, first_nmi
62610913 1451 CFI_REMEMBER_STATE
3f3c8b8c
SR
1452
1453nested_nmi:
1454 /*
1455 * Do nothing if we interrupted the fixup in repeat_nmi.
1456 * It's about to repeat the NMI handler, so we are fine
1457 * with ignoring this one.
1458 */
1459 movq $repeat_nmi, %rdx
1460 cmpq 8(%rsp), %rdx
1461 ja 1f
1462 movq $end_repeat_nmi, %rdx
1463 cmpq 8(%rsp), %rdx
1464 ja nested_nmi_out
1465
14661:
1467 /* Set up the interrupted NMIs stack to jump to repeat_nmi */
28696f43 1468 leaq -1*8(%rsp), %rdx
3f3c8b8c 1469 movq %rdx, %rsp
28696f43
SQ
1470 CFI_ADJUST_CFA_OFFSET 1*8
1471 leaq -10*8(%rsp), %rdx
3f3c8b8c
SR
1472 pushq_cfi $__KERNEL_DS
1473 pushq_cfi %rdx
1474 pushfq_cfi
1475 pushq_cfi $__KERNEL_CS
1476 pushq_cfi $repeat_nmi
1477
1478 /* Put stack back */
28696f43
SQ
1479 addq $(6*8), %rsp
1480 CFI_ADJUST_CFA_OFFSET -6*8
3f3c8b8c
SR
1481
1482nested_nmi_out:
1483 popq_cfi %rdx
62610913 1484 CFI_RESTORE rdx
3f3c8b8c
SR
1485
1486 /* No need to check faults here */
1487 INTERRUPT_RETURN
1488
62610913 1489 CFI_RESTORE_STATE
3f3c8b8c
SR
1490first_nmi:
1491 /*
1492 * Because nested NMIs will use the pushed location that we
1493 * stored in rdx, we must keep that space available.
1494 * Here's what our stack frame will look like:
1495 * +-------------------------+
1496 * | original SS |
1497 * | original Return RSP |
1498 * | original RFLAGS |
1499 * | original CS |
1500 * | original RIP |
1501 * +-------------------------+
1502 * | temp storage for rdx |
1503 * +-------------------------+
1504 * | NMI executing variable |
1505 * +-------------------------+
3f3c8b8c
SR
1506 * | copied SS |
1507 * | copied Return RSP |
1508 * | copied RFLAGS |
1509 * | copied CS |
1510 * | copied RIP |
1511 * +-------------------------+
28696f43
SQ
1512 * | Saved SS |
1513 * | Saved Return RSP |
1514 * | Saved RFLAGS |
1515 * | Saved CS |
1516 * | Saved RIP |
1517 * +-------------------------+
3f3c8b8c
SR
1518 * | pt_regs |
1519 * +-------------------------+
1520 *
79fb4ad6
SR
1521 * The saved stack frame is used to fix up the copied stack frame
1522 * that a nested NMI may change to make the interrupted NMI iret jump
1523 * to the repeat_nmi. The original stack frame and the temp storage
3f3c8b8c
SR
1524 * is also used by nested NMIs and can not be trusted on exit.
1525 */
79fb4ad6 1526 /* Do not pop rdx, nested NMIs will corrupt that part of the stack */
62610913
JB
1527 movq (%rsp), %rdx
1528 CFI_RESTORE rdx
1529
3f3c8b8c
SR
1530 /* Set the NMI executing variable on the stack. */
1531 pushq_cfi $1
1532
28696f43
SQ
1533 /*
1534 * Leave room for the "copied" frame
1535 */
1536 subq $(5*8), %rsp
444723dc 1537 CFI_ADJUST_CFA_OFFSET 5*8
28696f43 1538
3f3c8b8c
SR
1539 /* Copy the stack frame to the Saved frame */
1540 .rept 5
28696f43 1541 pushq_cfi 11*8(%rsp)
3f3c8b8c 1542 .endr
62610913
JB
1543 CFI_DEF_CFA_OFFSET SS+8-RIP
1544
79fb4ad6
SR
1545 /* Everything up to here is safe from nested NMIs */
1546
62610913
JB
1547 /*
1548 * If there was a nested NMI, the first NMI's iret will return
1549 * here. But NMIs are still enabled and we can take another
1550 * nested NMI. The nested NMI checks the interrupted RIP to see
1551 * if it is between repeat_nmi and end_repeat_nmi, and if so
1552 * it will just return, as we are about to repeat an NMI anyway.
1553 * This makes it safe to copy to the stack frame that a nested
1554 * NMI will update.
1555 */
1556repeat_nmi:
1557 /*
1558 * Update the stack variable to say we are still in NMI (the update
1559 * is benign for the non-repeat case, where 1 was pushed just above
1560 * to this very stack slot).
1561 */
28696f43 1562 movq $1, 10*8(%rsp)
3f3c8b8c
SR
1563
1564 /* Make another copy, this one may be modified by nested NMIs */
28696f43
SQ
1565 addq $(10*8), %rsp
1566 CFI_ADJUST_CFA_OFFSET -10*8
3f3c8b8c 1567 .rept 5
28696f43 1568 pushq_cfi -6*8(%rsp)
3f3c8b8c 1569 .endr
28696f43 1570 subq $(5*8), %rsp
62610913
JB
1571 CFI_DEF_CFA_OFFSET SS+8-RIP
1572end_repeat_nmi:
3f3c8b8c
SR
1573
1574 /*
1575 * Everything below this point can be preempted by a nested
79fb4ad6
SR
1576 * NMI if the first NMI took an exception and reset our iret stack
1577 * so that we repeat another NMI.
3f3c8b8c 1578 */
1fd466ef 1579 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
76f5df43
DV
1580 ALLOC_PT_GPREGS_ON_STACK
1581
1fd466ef
SR
1582 /*
1583 * Use save_paranoid to handle SWAPGS, but no need to use paranoid_exit
1584 * as we should not be calling schedule in NMI context.
1585 * Even with normal interrupts enabled. An NMI should not be
1586 * setting NEED_RESCHED or anything that normal interrupts and
1587 * exceptions might do.
1588 */
ddeb8f21
AH
1589 call save_paranoid
1590 DEFAULT_FRAME 0
7fbb98c5
SR
1591
1592 /*
1593 * Save off the CR2 register. If we take a page fault in the NMI then
1594 * it could corrupt the CR2 value. If the NMI preempts a page fault
1595 * handler before it was able to read the CR2 register, and then the
1596 * NMI itself takes a page fault, the page fault that was preempted
1597 * will read the information from the NMI page fault and not the
1598 * origin fault. Save it off and restore it if it changes.
1599 * Use the r12 callee-saved register.
1600 */
1601 movq %cr2, %r12
1602
ddeb8f21
AH
1603 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1604 movq %rsp,%rdi
1605 movq $-1,%rsi
1606 call do_nmi
7fbb98c5
SR
1607
1608 /* Did the NMI take a page fault? Restore cr2 if it did */
1609 movq %cr2, %rcx
1610 cmpq %rcx, %r12
1611 je 1f
1612 movq %r12, %cr2
16131:
1614
ddeb8f21
AH
1615 testl %ebx,%ebx /* swapgs needed? */
1616 jnz nmi_restore
ddeb8f21
AH
1617nmi_swapgs:
1618 SWAPGS_UNSAFE_STACK
1619nmi_restore:
76f5df43
DV
1620 RESTORE_EXTRA_REGS
1621 RESTORE_C_REGS
444723dc 1622 /* Pop the extra iret frame at once */
76f5df43 1623 REMOVE_PT_GPREGS_FROM_STACK 6*8
28696f43 1624
3f3c8b8c 1625 /* Clear the NMI executing stack variable */
28696f43 1626 movq $0, 5*8(%rsp)
ddeb8f21 1627 jmp irq_return
9f1e87ea 1628 CFI_ENDPROC
ddeb8f21
AH
1629END(nmi)
1630
1631ENTRY(ignore_sysret)
1632 CFI_STARTPROC
1633 mov $-ENOSYS,%eax
1634 sysret
1635 CFI_ENDPROC
1636END(ignore_sysret)
1637