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