]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/entry/entry_64.S
x86/entry/64: Shorten TEST instructions
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / entry / 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>
4d732138 7 *
1da177e4
LT
8 * entry.S contains the system-call and fault low-level handling routines.
9 *
8b4777a4
AL
10 * Some of this is documented in Documentation/x86/entry_64.txt
11 *
0bd7b798 12 * A note on terminology:
4d732138
IM
13 * - iret frame: Architecture defined interrupt frame from SS to RIP
14 * at the top of the kernel process stack.
2e91a17b
AK
15 *
16 * Some macro usage:
4d732138
IM
17 * - ENTRY/END: Define functions in the symbol table.
18 * - TRACE_IRQ_*: Trace hardirq state for lock debugging.
19 * - idtentry: Define exception entry points.
1da177e4 20 */
1da177e4
LT
21#include <linux/linkage.h>
22#include <asm/segment.h>
1da177e4
LT
23#include <asm/cache.h>
24#include <asm/errno.h>
d36f9479 25#include "calling.h"
e2d5df93 26#include <asm/asm-offsets.h>
1da177e4
LT
27#include <asm/msr.h>
28#include <asm/unistd.h>
29#include <asm/thread_info.h>
30#include <asm/hw_irq.h>
0341c14d 31#include <asm/page_types.h>
2601e64d 32#include <asm/irqflags.h>
72fe4858 33#include <asm/paravirt.h>
9939ddaf 34#include <asm/percpu.h>
d7abc0fa 35#include <asm/asm.h>
63bcff2a 36#include <asm/smap.h>
3891a04a 37#include <asm/pgtable_types.h>
784d5699 38#include <asm/export.h>
8c1f7558 39#include <asm/frame.h>
d7e7528b 40#include <linux/err.h>
1da177e4 41
4d732138
IM
42.code64
43.section .entry.text, "ax"
16444a8a 44
72fe4858 45#ifdef CONFIG_PARAVIRT
2be29982 46ENTRY(native_usergs_sysret64)
8c1f7558 47 UNWIND_HINT_EMPTY
72fe4858
GOC
48 swapgs
49 sysretq
8c1f7558 50END(native_usergs_sysret64)
72fe4858
GOC
51#endif /* CONFIG_PARAVIRT */
52
f2db9382 53.macro TRACE_IRQS_IRETQ
2601e64d 54#ifdef CONFIG_TRACE_IRQFLAGS
4d732138
IM
55 bt $9, EFLAGS(%rsp) /* interrupts off? */
56 jnc 1f
2601e64d
IM
57 TRACE_IRQS_ON
581:
59#endif
60.endm
61
5963e317
SR
62/*
63 * When dynamic function tracer is enabled it will add a breakpoint
64 * to all locations that it is about to modify, sync CPUs, update
65 * all the code, sync CPUs, then remove the breakpoints. In this time
66 * if lockdep is enabled, it might jump back into the debug handler
67 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
68 *
69 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
70 * make sure the stack pointer does not get reset back to the top
71 * of the debug stack, and instead just reuses the current stack.
72 */
73#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
74
75.macro TRACE_IRQS_OFF_DEBUG
4d732138 76 call debug_stack_set_zero
5963e317 77 TRACE_IRQS_OFF
4d732138 78 call debug_stack_reset
5963e317
SR
79.endm
80
81.macro TRACE_IRQS_ON_DEBUG
4d732138 82 call debug_stack_set_zero
5963e317 83 TRACE_IRQS_ON
4d732138 84 call debug_stack_reset
5963e317
SR
85.endm
86
f2db9382 87.macro TRACE_IRQS_IRETQ_DEBUG
4d732138
IM
88 bt $9, EFLAGS(%rsp) /* interrupts off? */
89 jnc 1f
5963e317
SR
90 TRACE_IRQS_ON_DEBUG
911:
92.endm
93
94#else
4d732138
IM
95# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
96# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
97# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
5963e317
SR
98#endif
99
1da177e4 100/*
4d732138 101 * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
1da177e4 102 *
fda57b22
AL
103 * This is the only entry point used for 64-bit system calls. The
104 * hardware interface is reasonably well designed and the register to
105 * argument mapping Linux uses fits well with the registers that are
106 * available when SYSCALL is used.
107 *
108 * SYSCALL instructions can be found inlined in libc implementations as
109 * well as some other programs and libraries. There are also a handful
110 * of SYSCALL instructions in the vDSO used, for example, as a
111 * clock_gettimeofday fallback.
112 *
4d732138 113 * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
b87cf63e
DV
114 * then loads new ss, cs, and rip from previously programmed MSRs.
115 * rflags gets masked by a value from another MSR (so CLD and CLAC
116 * are not needed). SYSCALL does not save anything on the stack
117 * and does not change rsp.
118 *
119 * Registers on entry:
1da177e4 120 * rax system call number
b87cf63e
DV
121 * rcx return address
122 * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
1da177e4 123 * rdi arg0
1da177e4 124 * rsi arg1
0bd7b798 125 * rdx arg2
b87cf63e 126 * r10 arg3 (needs to be moved to rcx to conform to C ABI)
1da177e4
LT
127 * r8 arg4
128 * r9 arg5
4d732138 129 * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
0bd7b798 130 *
1da177e4
LT
131 * Only called from user space.
132 *
7fcb3bc3 133 * When user can change pt_regs->foo always force IRET. That is because
7bf36bbc
AK
134 * it deals with uncanonical addresses better. SYSRET has trouble
135 * with them due to bugs in both AMD and Intel CPUs.
0bd7b798 136 */
1da177e4 137
b2502b41 138ENTRY(entry_SYSCALL_64)
8c1f7558 139 UNWIND_HINT_EMPTY
9ed8e7d8
DV
140 /*
141 * Interrupts are off on entry.
142 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
143 * it is too small to ever cause noticeable irq latency.
144 */
72fe4858 145
8a9949bc 146 swapgs
4d732138
IM
147 movq %rsp, PER_CPU_VAR(rsp_scratch)
148 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
9ed8e7d8 149
1e423bff
AL
150 TRACE_IRQS_OFF
151
9ed8e7d8 152 /* Construct struct pt_regs on stack */
4d732138
IM
153 pushq $__USER_DS /* pt_regs->ss */
154 pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */
4d732138
IM
155 pushq %r11 /* pt_regs->flags */
156 pushq $__USER_CS /* pt_regs->cs */
157 pushq %rcx /* pt_regs->ip */
8a9949bc 158GLOBAL(entry_SYSCALL_64_after_hwframe)
4d732138
IM
159 pushq %rax /* pt_regs->orig_ax */
160 pushq %rdi /* pt_regs->di */
161 pushq %rsi /* pt_regs->si */
162 pushq %rdx /* pt_regs->dx */
163 pushq %rcx /* pt_regs->cx */
164 pushq $-ENOSYS /* pt_regs->ax */
165 pushq %r8 /* pt_regs->r8 */
166 pushq %r9 /* pt_regs->r9 */
167 pushq %r10 /* pt_regs->r10 */
168 pushq %r11 /* pt_regs->r11 */
169 sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
8c1f7558 170 UNWIND_HINT_REGS extra=0
4d732138 171
1e423bff
AL
172 /*
173 * If we need to do entry work or if we guess we'll need to do
174 * exit work, go straight to the slow path.
175 */
15f4eae7
AL
176 movq PER_CPU_VAR(current_task), %r11
177 testl $_TIF_WORK_SYSCALL_ENTRY|_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
1e423bff
AL
178 jnz entry_SYSCALL64_slow_path
179
b2502b41 180entry_SYSCALL_64_fastpath:
1e423bff
AL
181 /*
182 * Easy case: enable interrupts and issue the syscall. If the syscall
183 * needs pt_regs, we'll call a stub that disables interrupts again
184 * and jumps to the slow path.
185 */
186 TRACE_IRQS_ON
187 ENABLE_INTERRUPTS(CLBR_NONE)
fca460f9 188#if __SYSCALL_MASK == ~0
4d732138 189 cmpq $__NR_syscall_max, %rax
fca460f9 190#else
4d732138
IM
191 andl $__SYSCALL_MASK, %eax
192 cmpl $__NR_syscall_max, %eax
fca460f9 193#endif
4d732138
IM
194 ja 1f /* return -ENOSYS (already in pt_regs->ax) */
195 movq %r10, %rcx
302f5b26
AL
196
197 /*
198 * This call instruction is handled specially in stub_ptregs_64.
b7765086
AL
199 * It might end up jumping to the slow path. If it jumps, RAX
200 * and all argument registers are clobbered.
302f5b26 201 */
4d732138 202 call *sys_call_table(, %rax, 8)
302f5b26
AL
203.Lentry_SYSCALL_64_after_fastpath_call:
204
4d732138 205 movq %rax, RAX(%rsp)
146b2b09 2061:
b3494a4a
AL
207
208 /*
1e423bff
AL
209 * If we get here, then we know that pt_regs is clean for SYSRET64.
210 * If we see that no exit work is required (which we are required
211 * to check with IRQs off), then we can go straight to SYSRET64.
b3494a4a 212 */
2140a994 213 DISABLE_INTERRUPTS(CLBR_ANY)
1e423bff 214 TRACE_IRQS_OFF
15f4eae7
AL
215 movq PER_CPU_VAR(current_task), %r11
216 testl $_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
1e423bff 217 jnz 1f
b3494a4a 218
1e423bff
AL
219 LOCKDEP_SYS_EXIT
220 TRACE_IRQS_ON /* user mode is traced as IRQs on */
eb2a54c3
AL
221 movq RIP(%rsp), %rcx
222 movq EFLAGS(%rsp), %r11
a5122106 223 addq $6*8, %rsp /* skip extra regs -- they were preserved */
8c1f7558 224 UNWIND_HINT_EMPTY
a5122106 225 jmp .Lpop_c_regs_except_rcx_r11_and_sysret
1da177e4 226
1e423bff
AL
2271:
228 /*
229 * The fast path looked good when we started, but something changed
230 * along the way and we need to switch to the slow path. Calling
231 * raise(3) will trigger this, for example. IRQs are off.
232 */
29ea1b25 233 TRACE_IRQS_ON
2140a994 234 ENABLE_INTERRUPTS(CLBR_ANY)
76f5df43 235 SAVE_EXTRA_REGS
4d732138 236 movq %rsp, %rdi
1e423bff
AL
237 call syscall_return_slowpath /* returns with IRQs disabled */
238 jmp return_from_SYSCALL_64
0bd7b798 239
1e423bff
AL
240entry_SYSCALL64_slow_path:
241 /* IRQs are off. */
76f5df43 242 SAVE_EXTRA_REGS
29ea1b25 243 movq %rsp, %rdi
1e423bff
AL
244 call do_syscall_64 /* returns with IRQs disabled */
245
246return_from_SYSCALL_64:
29ea1b25 247 TRACE_IRQS_IRETQ /* we're about to change IF */
fffbb5dc
DV
248
249 /*
250 * Try to use SYSRET instead of IRET if we're returning to
8a055d7f
AL
251 * a completely clean 64-bit userspace context. If we're not,
252 * go to the slow exit path.
fffbb5dc 253 */
4d732138
IM
254 movq RCX(%rsp), %rcx
255 movq RIP(%rsp), %r11
8a055d7f
AL
256
257 cmpq %rcx, %r11 /* SYSRET requires RCX == RIP */
258 jne swapgs_restore_regs_and_return_to_usermode
fffbb5dc
DV
259
260 /*
261 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
262 * in kernel space. This essentially lets the user take over
17be0aec 263 * the kernel, since userspace controls RSP.
fffbb5dc 264 *
17be0aec 265 * If width of "canonical tail" ever becomes variable, this will need
fffbb5dc 266 * to be updated to remain correct on both old and new CPUs.
361b4b58 267 *
cbe0317b
KS
268 * Change top bits to match most significant bit (47th or 56th bit
269 * depending on paging mode) in the address.
fffbb5dc 270 */
17be0aec
DV
271 shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
272 sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
4d732138 273
17be0aec
DV
274 /* If this changed %rcx, it was not canonical */
275 cmpq %rcx, %r11
8a055d7f 276 jne swapgs_restore_regs_and_return_to_usermode
fffbb5dc 277
4d732138 278 cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
8a055d7f 279 jne swapgs_restore_regs_and_return_to_usermode
fffbb5dc 280
4d732138
IM
281 movq R11(%rsp), %r11
282 cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
8a055d7f 283 jne swapgs_restore_regs_and_return_to_usermode
fffbb5dc
DV
284
285 /*
3e035305
BP
286 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
287 * restore RF properly. If the slowpath sets it for whatever reason, we
288 * need to restore it correctly.
289 *
290 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
291 * trap from userspace immediately after SYSRET. This would cause an
292 * infinite loop whenever #DB happens with register state that satisfies
293 * the opportunistic SYSRET conditions. For example, single-stepping
294 * this user code:
fffbb5dc 295 *
4d732138 296 * movq $stuck_here, %rcx
fffbb5dc
DV
297 * pushfq
298 * popq %r11
299 * stuck_here:
300 *
301 * would never get past 'stuck_here'.
302 */
4d732138 303 testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
8a055d7f 304 jnz swapgs_restore_regs_and_return_to_usermode
fffbb5dc
DV
305
306 /* nothing to check for RSP */
307
4d732138 308 cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
8a055d7f 309 jne swapgs_restore_regs_and_return_to_usermode
fffbb5dc
DV
310
311 /*
4d732138
IM
312 * We win! This label is here just for ease of understanding
313 * perf profiles. Nothing jumps here.
fffbb5dc
DV
314 */
315syscall_return_via_sysret:
17be0aec 316 /* rcx and r11 are already restored (see code above) */
8c1f7558 317 UNWIND_HINT_EMPTY
4fbb3910 318 POP_EXTRA_REGS
a5122106 319.Lpop_c_regs_except_rcx_r11_and_sysret:
4fbb3910
AL
320 popq %rsi /* skip r11 */
321 popq %r10
322 popq %r9
323 popq %r8
324 popq %rax
325 popq %rsi /* skip rcx */
326 popq %rdx
327 popq %rsi
328 popq %rdi
329 movq RSP-ORIG_RAX(%rsp), %rsp
fffbb5dc 330 USERGS_SYSRET64
b2502b41 331END(entry_SYSCALL_64)
0bd7b798 332
302f5b26
AL
333ENTRY(stub_ptregs_64)
334 /*
335 * Syscalls marked as needing ptregs land here.
b7765086
AL
336 * If we are on the fast path, we need to save the extra regs,
337 * which we achieve by trying again on the slow path. If we are on
338 * the slow path, the extra regs are already saved.
302f5b26
AL
339 *
340 * RAX stores a pointer to the C function implementing the syscall.
b7765086 341 * IRQs are on.
302f5b26
AL
342 */
343 cmpq $.Lentry_SYSCALL_64_after_fastpath_call, (%rsp)
344 jne 1f
345
b7765086
AL
346 /*
347 * Called from fast path -- disable IRQs again, pop return address
348 * and jump to slow path
349 */
2140a994 350 DISABLE_INTERRUPTS(CLBR_ANY)
b7765086 351 TRACE_IRQS_OFF
302f5b26 352 popq %rax
8c1f7558 353 UNWIND_HINT_REGS extra=0
b7765086 354 jmp entry_SYSCALL64_slow_path
302f5b26
AL
355
3561:
b3830e8d 357 jmp *%rax /* Called from C */
302f5b26
AL
358END(stub_ptregs_64)
359
360.macro ptregs_stub func
361ENTRY(ptregs_\func)
8c1f7558 362 UNWIND_HINT_FUNC
302f5b26
AL
363 leaq \func(%rip), %rax
364 jmp stub_ptregs_64
365END(ptregs_\func)
366.endm
367
368/* Instantiate ptregs_stub for each ptregs-using syscall */
369#define __SYSCALL_64_QUAL_(sym)
370#define __SYSCALL_64_QUAL_ptregs(sym) ptregs_stub sym
371#define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym)
372#include <asm/syscalls_64.h>
fffbb5dc 373
0100301b
BG
374/*
375 * %rdi: prev task
376 * %rsi: next task
377 */
378ENTRY(__switch_to_asm)
8c1f7558 379 UNWIND_HINT_FUNC
0100301b
BG
380 /*
381 * Save callee-saved registers
382 * This must match the order in inactive_task_frame
383 */
384 pushq %rbp
385 pushq %rbx
386 pushq %r12
387 pushq %r13
388 pushq %r14
389 pushq %r15
390
391 /* switch stack */
392 movq %rsp, TASK_threadsp(%rdi)
393 movq TASK_threadsp(%rsi), %rsp
394
395#ifdef CONFIG_CC_STACKPROTECTOR
396 movq TASK_stack_canary(%rsi), %rbx
397 movq %rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset
398#endif
399
400 /* restore callee-saved registers */
401 popq %r15
402 popq %r14
403 popq %r13
404 popq %r12
405 popq %rbx
406 popq %rbp
407
408 jmp __switch_to
409END(__switch_to_asm)
410
1eeb207f
DV
411/*
412 * A newly forked process directly context switches into this address.
413 *
0100301b 414 * rax: prev task we switched from
616d2483
BG
415 * rbx: kernel thread func (NULL for user thread)
416 * r12: kernel thread arg
1eeb207f
DV
417 */
418ENTRY(ret_from_fork)
8c1f7558 419 UNWIND_HINT_EMPTY
0100301b 420 movq %rax, %rdi
ebd57499 421 call schedule_tail /* rdi: 'prev' task parameter */
1eeb207f 422
ebd57499
JP
423 testq %rbx, %rbx /* from kernel_thread? */
424 jnz 1f /* kernel threads are uncommon */
24d978b7 425
616d2483 4262:
8c1f7558 427 UNWIND_HINT_REGS
ebd57499 428 movq %rsp, %rdi
24d978b7
AL
429 call syscall_return_slowpath /* returns with IRQs disabled */
430 TRACE_IRQS_ON /* user mode is traced as IRQS on */
8a055d7f 431 jmp swapgs_restore_regs_and_return_to_usermode
616d2483
BG
432
4331:
434 /* kernel thread */
435 movq %r12, %rdi
436 call *%rbx
437 /*
438 * A kernel thread is allowed to return here after successfully
439 * calling do_execve(). Exit to userspace to complete the execve()
440 * syscall.
441 */
442 movq $0, RAX(%rsp)
443 jmp 2b
1eeb207f
DV
444END(ret_from_fork)
445
939b7871 446/*
3304c9c3
DV
447 * Build the entry stubs with some assembler magic.
448 * We pack 1 stub into every 8-byte block.
939b7871 449 */
3304c9c3 450 .align 8
939b7871 451ENTRY(irq_entries_start)
3304c9c3
DV
452 vector=FIRST_EXTERNAL_VECTOR
453 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
8c1f7558 454 UNWIND_HINT_IRET_REGS
4d732138 455 pushq $(~vector+0x80) /* Note: always in signed byte range */
3304c9c3 456 jmp common_interrupt
3304c9c3 457 .align 8
8c1f7558 458 vector=vector+1
3304c9c3 459 .endr
939b7871
PA
460END(irq_entries_start)
461
1d3e53e8
AL
462.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
463#ifdef CONFIG_DEBUG_ENTRY
464 pushfq
465 testl $X86_EFLAGS_IF, (%rsp)
466 jz .Lokay_\@
467 ud2
468.Lokay_\@:
469 addq $8, %rsp
470#endif
471.endm
472
473/*
474 * Enters the IRQ stack if we're not already using it. NMI-safe. Clobbers
475 * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
476 * Requires kernel GSBASE.
477 *
478 * The invariant is that, if irq_count != -1, then the IRQ stack is in use.
479 */
8c1f7558 480.macro ENTER_IRQ_STACK regs=1 old_rsp
1d3e53e8
AL
481 DEBUG_ENTRY_ASSERT_IRQS_OFF
482 movq %rsp, \old_rsp
8c1f7558
JP
483
484 .if \regs
485 UNWIND_HINT_REGS base=\old_rsp
486 .endif
487
1d3e53e8 488 incl PER_CPU_VAR(irq_count)
29955909 489 jnz .Lirq_stack_push_old_rsp_\@
1d3e53e8
AL
490
491 /*
492 * Right now, if we just incremented irq_count to zero, we've
493 * claimed the IRQ stack but we haven't switched to it yet.
494 *
495 * If anything is added that can interrupt us here without using IST,
496 * it must be *extremely* careful to limit its stack usage. This
497 * could include kprobes and a hypothetical future IST-less #DB
498 * handler.
29955909
AL
499 *
500 * The OOPS unwinder relies on the word at the top of the IRQ
501 * stack linking back to the previous RSP for the entire time we're
502 * on the IRQ stack. For this to work reliably, we need to write
503 * it before we actually move ourselves to the IRQ stack.
504 */
505
506 movq \old_rsp, PER_CPU_VAR(irq_stack_union + IRQ_STACK_SIZE - 8)
507 movq PER_CPU_VAR(irq_stack_ptr), %rsp
508
509#ifdef CONFIG_DEBUG_ENTRY
510 /*
511 * If the first movq above becomes wrong due to IRQ stack layout
512 * changes, the only way we'll notice is if we try to unwind right
513 * here. Assert that we set up the stack right to catch this type
514 * of bug quickly.
1d3e53e8 515 */
29955909
AL
516 cmpq -8(%rsp), \old_rsp
517 je .Lirq_stack_okay\@
518 ud2
519 .Lirq_stack_okay\@:
520#endif
1d3e53e8 521
29955909 522.Lirq_stack_push_old_rsp_\@:
1d3e53e8 523 pushq \old_rsp
8c1f7558
JP
524
525 .if \regs
526 UNWIND_HINT_REGS indirect=1
527 .endif
1d3e53e8
AL
528.endm
529
530/*
531 * Undoes ENTER_IRQ_STACK.
532 */
8c1f7558 533.macro LEAVE_IRQ_STACK regs=1
1d3e53e8
AL
534 DEBUG_ENTRY_ASSERT_IRQS_OFF
535 /* We need to be off the IRQ stack before decrementing irq_count. */
536 popq %rsp
537
8c1f7558
JP
538 .if \regs
539 UNWIND_HINT_REGS
540 .endif
541
1d3e53e8
AL
542 /*
543 * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming
544 * the irq stack but we're not on it.
545 */
546
547 decl PER_CPU_VAR(irq_count)
548.endm
549
d99015b1 550/*
1da177e4
LT
551 * Interrupt entry/exit.
552 *
553 * Interrupt entry points save only callee clobbered registers in fast path.
d99015b1
AH
554 *
555 * Entry runs with interrupts off.
556 */
1da177e4 557
722024db 558/* 0(%rsp): ~(interrupt number) */
1da177e4 559 .macro interrupt func
f6f64681 560 cld
ff467594
AL
561 ALLOC_PT_GPREGS_ON_STACK
562 SAVE_C_REGS
563 SAVE_EXTRA_REGS
946c1911 564 ENCODE_FRAME_POINTER
76f5df43 565
ff467594 566 testb $3, CS(%rsp)
dde74f2e 567 jz 1f
02bc7768
AL
568
569 /*
570 * IRQ from user mode. Switch to kernel gsbase and inform context
571 * tracking that we're in kernel mode.
572 */
f6f64681 573 SWAPGS
f1075053
AL
574
575 /*
576 * We need to tell lockdep that IRQs are off. We can't do this until
577 * we fix gsbase, and we should do it before enter_from_user_mode
578 * (which can take locks). Since TRACE_IRQS_OFF idempotent,
579 * the simplest way to handle it is to just call it twice if
580 * we enter from user mode. There's no reason to optimize this since
581 * TRACE_IRQS_OFF is a no-op if lockdep is off.
582 */
583 TRACE_IRQS_OFF
584
478dc89c 585 CALL_enter_from_user_mode
02bc7768 586
76f5df43 5871:
1d3e53e8 588 ENTER_IRQ_STACK old_rsp=%rdi
f6f64681
DV
589 /* We entered an interrupt context - irqs are off: */
590 TRACE_IRQS_OFF
591
a586f98e 592 call \func /* rdi points to pt_regs */
1da177e4
LT
593 .endm
594
722024db
AH
595 /*
596 * The interrupt stubs push (~vector+0x80) onto the stack and
597 * then jump to common_interrupt.
598 */
939b7871
PA
599 .p2align CONFIG_X86_L1_CACHE_SHIFT
600common_interrupt:
ee4eb87b 601 ASM_CLAC
4d732138 602 addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
1da177e4 603 interrupt do_IRQ
34061f13 604 /* 0(%rsp): old RSP */
7effaa88 605ret_from_intr:
2140a994 606 DISABLE_INTERRUPTS(CLBR_ANY)
2601e64d 607 TRACE_IRQS_OFF
625dbc3b 608
1d3e53e8 609 LEAVE_IRQ_STACK
625dbc3b 610
03335e95 611 testb $3, CS(%rsp)
dde74f2e 612 jz retint_kernel
4d732138 613
02bc7768 614 /* Interrupt came from user space */
02bc7768
AL
615GLOBAL(retint_user)
616 mov %rsp,%rdi
617 call prepare_exit_to_usermode
2601e64d 618 TRACE_IRQS_IRETQ
26c4ef9c 619
8a055d7f 620GLOBAL(swapgs_restore_regs_and_return_to_usermode)
26c4ef9c
AL
621#ifdef CONFIG_DEBUG_ENTRY
622 /* Assert that pt_regs indicates user mode. */
1e4c4f61 623 testb $3, CS(%rsp)
26c4ef9c
AL
624 jnz 1f
625 ud2
6261:
627#endif
8a055d7f 628 SWAPGS
e872045b
AL
629 POP_EXTRA_REGS
630 POP_C_REGS
631 addq $8, %rsp /* skip regs->orig_ax */
26c4ef9c
AL
632 INTERRUPT_RETURN
633
2601e64d 634
627276cb 635/* Returning to kernel space */
6ba71b76 636retint_kernel:
627276cb
DV
637#ifdef CONFIG_PREEMPT
638 /* Interrupts are off */
639 /* Check if we need preemption */
4d732138 640 bt $9, EFLAGS(%rsp) /* were interrupts off? */
6ba71b76 641 jnc 1f
4d732138 6420: cmpl $0, PER_CPU_VAR(__preempt_count)
36acef25 643 jnz 1f
627276cb 644 call preempt_schedule_irq
36acef25 645 jmp 0b
6ba71b76 6461:
627276cb 647#endif
2601e64d
IM
648 /*
649 * The iretq could re-enable interrupts:
650 */
651 TRACE_IRQS_IRETQ
fffbb5dc 652
26c4ef9c
AL
653GLOBAL(restore_regs_and_return_to_kernel)
654#ifdef CONFIG_DEBUG_ENTRY
655 /* Assert that pt_regs indicates kernel mode. */
1e4c4f61 656 testb $3, CS(%rsp)
26c4ef9c
AL
657 jz 1f
658 ud2
6591:
660#endif
e872045b
AL
661 POP_EXTRA_REGS
662 POP_C_REGS
663 addq $8, %rsp /* skip regs->orig_ax */
7209a75d
AL
664 INTERRUPT_RETURN
665
666ENTRY(native_iret)
8c1f7558 667 UNWIND_HINT_IRET_REGS
3891a04a
PA
668 /*
669 * Are we returning to a stack segment from the LDT? Note: in
670 * 64-bit mode SS:RSP on the exception stack is always valid.
671 */
34273f41 672#ifdef CONFIG_X86_ESPFIX64
4d732138
IM
673 testb $4, (SS-RIP)(%rsp)
674 jnz native_irq_return_ldt
34273f41 675#endif
3891a04a 676
af726f21 677.global native_irq_return_iret
7209a75d 678native_irq_return_iret:
b645af2d
AL
679 /*
680 * This may fault. Non-paranoid faults on return to userspace are
681 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
682 * Double-faults due to espfix64 are handled in do_double_fault.
683 * Other faults here are fatal.
684 */
1da177e4 685 iretq
3701d863 686
34273f41 687#ifdef CONFIG_X86_ESPFIX64
7209a75d 688native_irq_return_ldt:
85063fac
AL
689 /*
690 * We are running with user GSBASE. All GPRs contain their user
691 * values. We have a percpu ESPFIX stack that is eight slots
692 * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom
693 * of the ESPFIX stack.
694 *
695 * We clobber RAX and RDI in this code. We stash RDI on the
696 * normal stack and RAX on the ESPFIX stack.
697 *
698 * The ESPFIX stack layout we set up looks like this:
699 *
700 * --- top of ESPFIX stack ---
701 * SS
702 * RSP
703 * RFLAGS
704 * CS
705 * RIP <-- RSP points here when we're done
706 * RAX <-- espfix_waddr points here
707 * --- bottom of ESPFIX stack ---
708 */
709
710 pushq %rdi /* Stash user RDI */
3891a04a 711 SWAPGS
4d732138 712 movq PER_CPU_VAR(espfix_waddr), %rdi
85063fac
AL
713 movq %rax, (0*8)(%rdi) /* user RAX */
714 movq (1*8)(%rsp), %rax /* user RIP */
4d732138 715 movq %rax, (1*8)(%rdi)
85063fac 716 movq (2*8)(%rsp), %rax /* user CS */
4d732138 717 movq %rax, (2*8)(%rdi)
85063fac 718 movq (3*8)(%rsp), %rax /* user RFLAGS */
4d732138 719 movq %rax, (3*8)(%rdi)
85063fac 720 movq (5*8)(%rsp), %rax /* user SS */
4d732138 721 movq %rax, (5*8)(%rdi)
85063fac 722 movq (4*8)(%rsp), %rax /* user RSP */
4d732138 723 movq %rax, (4*8)(%rdi)
85063fac
AL
724 /* Now RAX == RSP. */
725
726 andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */
727 popq %rdi /* Restore user RDI */
728
729 /*
730 * espfix_stack[31:16] == 0. The page tables are set up such that
731 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
732 * espfix_waddr for any X. That is, there are 65536 RO aliases of
733 * the same page. Set up RSP so that RSP[31:16] contains the
734 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
735 * still points to an RO alias of the ESPFIX stack.
736 */
4d732138 737 orq PER_CPU_VAR(espfix_stack), %rax
3891a04a 738 SWAPGS
4d732138 739 movq %rax, %rsp
8c1f7558 740 UNWIND_HINT_IRET_REGS offset=8
85063fac
AL
741
742 /*
743 * At this point, we cannot write to the stack any more, but we can
744 * still read.
745 */
746 popq %rax /* Restore user RAX */
747
748 /*
749 * RSP now points to an ordinary IRET frame, except that the page
750 * is read-only and RSP[31:16] are preloaded with the userspace
751 * values. We can now IRET back to userspace.
752 */
4d732138 753 jmp native_irq_return_iret
34273f41 754#endif
4b787e0b 755END(common_interrupt)
3891a04a 756
1da177e4
LT
757/*
758 * APIC interrupts.
0bd7b798 759 */
cf910e83 760.macro apicinterrupt3 num sym do_sym
322648d1 761ENTRY(\sym)
8c1f7558 762 UNWIND_HINT_IRET_REGS
ee4eb87b 763 ASM_CLAC
4d732138 764 pushq $~(\num)
39e95433 765.Lcommon_\sym:
322648d1 766 interrupt \do_sym
4d732138 767 jmp ret_from_intr
322648d1
AH
768END(\sym)
769.endm
1da177e4 770
469f0023 771/* Make sure APIC interrupt handlers end up in the irqentry section: */
229a7186
MH
772#define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
773#define POP_SECTION_IRQENTRY .popsection
469f0023 774
cf910e83 775.macro apicinterrupt num sym do_sym
469f0023 776PUSH_SECTION_IRQENTRY
cf910e83 777apicinterrupt3 \num \sym \do_sym
469f0023 778POP_SECTION_IRQENTRY
cf910e83
SA
779.endm
780
322648d1 781#ifdef CONFIG_SMP
4d732138
IM
782apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
783apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
322648d1 784#endif
1da177e4 785
03b48632 786#ifdef CONFIG_X86_UV
4d732138 787apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
03b48632 788#endif
4d732138
IM
789
790apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
791apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
89b831ef 792
d78f2664 793#ifdef CONFIG_HAVE_KVM
4d732138
IM
794apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
795apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
210f84b0 796apicinterrupt3 POSTED_INTR_NESTED_VECTOR kvm_posted_intr_nested_ipi smp_kvm_posted_intr_nested_ipi
d78f2664
YZ
797#endif
798
33e5ff63 799#ifdef CONFIG_X86_MCE_THRESHOLD
4d732138 800apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
33e5ff63
SA
801#endif
802
24fd78a8 803#ifdef CONFIG_X86_MCE_AMD
4d732138 804apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
24fd78a8
AG
805#endif
806
33e5ff63 807#ifdef CONFIG_X86_THERMAL_VECTOR
4d732138 808apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
33e5ff63 809#endif
1812924b 810
322648d1 811#ifdef CONFIG_SMP
4d732138
IM
812apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
813apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
814apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
322648d1 815#endif
1da177e4 816
4d732138
IM
817apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
818apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
0bd7b798 819
e360adbe 820#ifdef CONFIG_IRQ_WORK
4d732138 821apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
241771ef
IM
822#endif
823
1da177e4
LT
824/*
825 * Exception entry points.
0bd7b798 826 */
9b476688 827#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
577ed45e
AL
828
829.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
322648d1 830ENTRY(\sym)
98990a33 831 UNWIND_HINT_IRET_REGS offset=\has_error_code*8
8c1f7558 832
577ed45e
AL
833 /* Sanity check */
834 .if \shift_ist != -1 && \paranoid == 0
835 .error "using shift_ist requires paranoid=1"
836 .endif
837
ee4eb87b 838 ASM_CLAC
cb5dd2c5 839
82c62fa0 840 .if \has_error_code == 0
4d732138 841 pushq $-1 /* ORIG_RAX: no syscall to restart */
cb5dd2c5
AL
842 .endif
843
76f5df43 844 ALLOC_PT_GPREGS_ON_STACK
cb5dd2c5
AL
845
846 .if \paranoid
48e08d0f 847 .if \paranoid == 1
4d732138
IM
848 testb $3, CS(%rsp) /* If coming from userspace, switch stacks */
849 jnz 1f
48e08d0f 850 .endif
4d732138 851 call paranoid_entry
cb5dd2c5 852 .else
4d732138 853 call error_entry
cb5dd2c5 854 .endif
8c1f7558 855 UNWIND_HINT_REGS
ebfc453e 856 /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
cb5dd2c5 857
cb5dd2c5 858 .if \paranoid
577ed45e 859 .if \shift_ist != -1
4d732138 860 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
577ed45e 861 .else
b8b1d08b 862 TRACE_IRQS_OFF
cb5dd2c5 863 .endif
577ed45e 864 .endif
cb5dd2c5 865
4d732138 866 movq %rsp, %rdi /* pt_regs pointer */
cb5dd2c5
AL
867
868 .if \has_error_code
4d732138
IM
869 movq ORIG_RAX(%rsp), %rsi /* get error code */
870 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
cb5dd2c5 871 .else
4d732138 872 xorl %esi, %esi /* no error code */
cb5dd2c5
AL
873 .endif
874
577ed45e 875 .if \shift_ist != -1
4d732138 876 subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
577ed45e
AL
877 .endif
878
4d732138 879 call \do_sym
cb5dd2c5 880
577ed45e 881 .if \shift_ist != -1
4d732138 882 addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
577ed45e
AL
883 .endif
884
ebfc453e 885 /* these procedures expect "no swapgs" flag in ebx */
cb5dd2c5 886 .if \paranoid
4d732138 887 jmp paranoid_exit
cb5dd2c5 888 .else
4d732138 889 jmp error_exit
cb5dd2c5
AL
890 .endif
891
48e08d0f 892 .if \paranoid == 1
48e08d0f
AL
893 /*
894 * Paranoid entry from userspace. Switch stacks and treat it
895 * as a normal entry. This means that paranoid handlers
896 * run in real process context if user_mode(regs).
897 */
8981:
4d732138 899 call error_entry
48e08d0f 900
48e08d0f 901
4d732138
IM
902 movq %rsp, %rdi /* pt_regs pointer */
903 call sync_regs
904 movq %rax, %rsp /* switch stack */
48e08d0f 905
4d732138 906 movq %rsp, %rdi /* pt_regs pointer */
48e08d0f
AL
907
908 .if \has_error_code
4d732138
IM
909 movq ORIG_RAX(%rsp), %rsi /* get error code */
910 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
48e08d0f 911 .else
4d732138 912 xorl %esi, %esi /* no error code */
48e08d0f
AL
913 .endif
914
4d732138 915 call \do_sym
48e08d0f 916
4d732138 917 jmp error_exit /* %ebx: no swapgs flag */
48e08d0f 918 .endif
ddeb8f21 919END(\sym)
322648d1 920.endm
b8b1d08b 921
4d732138
IM
922idtentry divide_error do_divide_error has_error_code=0
923idtentry overflow do_overflow has_error_code=0
924idtentry bounds do_bounds has_error_code=0
925idtentry invalid_op do_invalid_op has_error_code=0
926idtentry device_not_available do_device_not_available has_error_code=0
927idtentry double_fault do_double_fault has_error_code=1 paranoid=2
928idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
929idtentry invalid_TSS do_invalid_TSS has_error_code=1
930idtentry segment_not_present do_segment_not_present has_error_code=1
931idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
932idtentry coprocessor_error do_coprocessor_error has_error_code=0
933idtentry alignment_check do_alignment_check has_error_code=1
934idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
935
936
937 /*
938 * Reload gs selector with exception handling
939 * edi: new selector
940 */
9f9d489a 941ENTRY(native_load_gs_index)
8c1f7558 942 FRAME_BEGIN
131484c8 943 pushfq
b8aa287f 944 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
9f1e87ea 945 SWAPGS
42c748bb 946.Lgs_change:
4d732138 947 movl %edi, %gs
96e5d28a 9482: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
72fe4858 949 SWAPGS
131484c8 950 popfq
8c1f7558 951 FRAME_END
9f1e87ea 952 ret
8c1f7558 953ENDPROC(native_load_gs_index)
784d5699 954EXPORT_SYMBOL(native_load_gs_index)
0bd7b798 955
42c748bb 956 _ASM_EXTABLE(.Lgs_change, bad_gs)
4d732138 957 .section .fixup, "ax"
1da177e4 958 /* running with kernelgs */
0bd7b798 959bad_gs:
4d732138 960 SWAPGS /* switch back to user gs */
b038c842
AL
961.macro ZAP_GS
962 /* This can't be a string because the preprocessor needs to see it. */
963 movl $__USER_DS, %eax
964 movl %eax, %gs
965.endm
966 ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
4d732138
IM
967 xorl %eax, %eax
968 movl %eax, %gs
969 jmp 2b
9f1e87ea 970 .previous
0bd7b798 971
2699500b 972/* Call softirq on interrupt stack. Interrupts are off. */
7d65f4a6 973ENTRY(do_softirq_own_stack)
4d732138
IM
974 pushq %rbp
975 mov %rsp, %rbp
8c1f7558 976 ENTER_IRQ_STACK regs=0 old_rsp=%r11
4d732138 977 call __do_softirq
8c1f7558 978 LEAVE_IRQ_STACK regs=0
2699500b 979 leaveq
ed6b676c 980 ret
8c1f7558 981ENDPROC(do_softirq_own_stack)
75154f40 982
3d75e1b8 983#ifdef CONFIG_XEN
5878d5d6 984idtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0
3d75e1b8
JF
985
986/*
9f1e87ea
CG
987 * A note on the "critical region" in our callback handler.
988 * We want to avoid stacking callback handlers due to events occurring
989 * during handling of the last event. To do this, we keep events disabled
990 * until we've done all processing. HOWEVER, we must enable events before
991 * popping the stack frame (can't be done atomically) and so it would still
992 * be possible to get enough handler activations to overflow the stack.
993 * Although unlikely, bugs of that kind are hard to track down, so we'd
994 * like to avoid the possibility.
995 * So, on entry to the handler we detect whether we interrupted an
996 * existing activation in its critical region -- if so, we pop the current
997 * activation and restart the handler using the previous one.
998 */
4d732138
IM
999ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */
1000
9f1e87ea
CG
1001/*
1002 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1003 * see the correct pointer to the pt_regs
1004 */
8c1f7558 1005 UNWIND_HINT_FUNC
4d732138 1006 movq %rdi, %rsp /* we don't return, adjust the stack frame */
8c1f7558 1007 UNWIND_HINT_REGS
1d3e53e8
AL
1008
1009 ENTER_IRQ_STACK old_rsp=%r10
4d732138 1010 call xen_evtchn_do_upcall
1d3e53e8
AL
1011 LEAVE_IRQ_STACK
1012
fdfd811d 1013#ifndef CONFIG_PREEMPT
4d732138 1014 call xen_maybe_preempt_hcall
fdfd811d 1015#endif
4d732138 1016 jmp error_exit
371c394a 1017END(xen_do_hypervisor_callback)
3d75e1b8
JF
1018
1019/*
9f1e87ea
CG
1020 * Hypervisor uses this for application faults while it executes.
1021 * We get here for two reasons:
1022 * 1. Fault while reloading DS, ES, FS or GS
1023 * 2. Fault while executing IRET
1024 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1025 * registers that could be reloaded and zeroed the others.
1026 * Category 2 we fix up by killing the current process. We cannot use the
1027 * normal Linux return path in this case because if we use the IRET hypercall
1028 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1029 * We distinguish between categories by comparing each saved segment register
1030 * with its current contents: any discrepancy means we in category 1.
1031 */
3d75e1b8 1032ENTRY(xen_failsafe_callback)
8c1f7558 1033 UNWIND_HINT_EMPTY
4d732138
IM
1034 movl %ds, %ecx
1035 cmpw %cx, 0x10(%rsp)
1036 jne 1f
1037 movl %es, %ecx
1038 cmpw %cx, 0x18(%rsp)
1039 jne 1f
1040 movl %fs, %ecx
1041 cmpw %cx, 0x20(%rsp)
1042 jne 1f
1043 movl %gs, %ecx
1044 cmpw %cx, 0x28(%rsp)
1045 jne 1f
3d75e1b8 1046 /* All segments match their saved values => Category 2 (Bad IRET). */
4d732138
IM
1047 movq (%rsp), %rcx
1048 movq 8(%rsp), %r11
1049 addq $0x30, %rsp
1050 pushq $0 /* RIP */
8c1f7558 1051 UNWIND_HINT_IRET_REGS offset=8
4d732138 1052 jmp general_protection
3d75e1b8 10531: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
4d732138
IM
1054 movq (%rsp), %rcx
1055 movq 8(%rsp), %r11
1056 addq $0x30, %rsp
8c1f7558 1057 UNWIND_HINT_IRET_REGS
4d732138 1058 pushq $-1 /* orig_ax = -1 => not a system call */
76f5df43
DV
1059 ALLOC_PT_GPREGS_ON_STACK
1060 SAVE_C_REGS
1061 SAVE_EXTRA_REGS
946c1911 1062 ENCODE_FRAME_POINTER
4d732138 1063 jmp error_exit
3d75e1b8
JF
1064END(xen_failsafe_callback)
1065
cf910e83 1066apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
38e20b07
SY
1067 xen_hvm_callback_vector xen_evtchn_do_upcall
1068
3d75e1b8 1069#endif /* CONFIG_XEN */
ddeb8f21 1070
bc2b0331 1071#if IS_ENABLED(CONFIG_HYPERV)
cf910e83 1072apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
bc2b0331
S
1073 hyperv_callback_vector hyperv_vector_handler
1074#endif /* CONFIG_HYPERV */
1075
4d732138
IM
1076idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1077idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1078idtentry stack_segment do_stack_segment has_error_code=1
1079
6cac5a92 1080#ifdef CONFIG_XEN
43e41110 1081idtentry xennmi do_nmi has_error_code=0
5878d5d6
JG
1082idtentry xendebug do_debug has_error_code=0
1083idtentry xenint3 do_int3 has_error_code=0
6cac5a92 1084#endif
4d732138
IM
1085
1086idtentry general_protection do_general_protection has_error_code=1
11a7ffb0 1087idtentry page_fault do_page_fault has_error_code=1
4d732138 1088
631bc487 1089#ifdef CONFIG_KVM_GUEST
4d732138 1090idtentry async_page_fault do_async_page_fault has_error_code=1
631bc487 1091#endif
4d732138 1092
ddeb8f21 1093#ifdef CONFIG_X86_MCE
4d732138 1094idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
ddeb8f21
AH
1095#endif
1096
ebfc453e
DV
1097/*
1098 * Save all registers in pt_regs, and switch gs if needed.
1099 * Use slow, but surefire "are we in kernel?" check.
1100 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1101 */
1102ENTRY(paranoid_entry)
8c1f7558 1103 UNWIND_HINT_FUNC
1eeb207f
DV
1104 cld
1105 SAVE_C_REGS 8
1106 SAVE_EXTRA_REGS 8
946c1911 1107 ENCODE_FRAME_POINTER 8
4d732138
IM
1108 movl $1, %ebx
1109 movl $MSR_GS_BASE, %ecx
1eeb207f 1110 rdmsr
4d732138
IM
1111 testl %edx, %edx
1112 js 1f /* negative -> in kernel */
1eeb207f 1113 SWAPGS
4d732138 1114 xorl %ebx, %ebx
1eeb207f 11151: ret
ebfc453e 1116END(paranoid_entry)
ddeb8f21 1117
ebfc453e
DV
1118/*
1119 * "Paranoid" exit path from exception stack. This is invoked
1120 * only on return from non-NMI IST interrupts that came
1121 * from kernel space.
1122 *
1123 * We may be returning to very strange contexts (e.g. very early
1124 * in syscall entry), so checking for preemption here would
1125 * be complicated. Fortunately, we there's no good reason
1126 * to try to handle preemption here.
4d732138
IM
1127 *
1128 * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
ebfc453e 1129 */
ddeb8f21 1130ENTRY(paranoid_exit)
8c1f7558 1131 UNWIND_HINT_REGS
2140a994 1132 DISABLE_INTERRUPTS(CLBR_ANY)
5963e317 1133 TRACE_IRQS_OFF_DEBUG
4d732138 1134 testl %ebx, %ebx /* swapgs needed? */
e5317832 1135 jnz .Lparanoid_exit_no_swapgs
f2db9382 1136 TRACE_IRQS_IRETQ
ddeb8f21 1137 SWAPGS_UNSAFE_STACK
e5317832
AL
1138 jmp .Lparanoid_exit_restore
1139.Lparanoid_exit_no_swapgs:
f2db9382 1140 TRACE_IRQS_IRETQ_DEBUG
e5317832
AL
1141.Lparanoid_exit_restore:
1142 jmp restore_regs_and_return_to_kernel
ddeb8f21
AH
1143END(paranoid_exit)
1144
1145/*
ebfc453e 1146 * Save all registers in pt_regs, and switch gs if needed.
539f5113 1147 * Return: EBX=0: came from user mode; EBX=1: otherwise
ddeb8f21
AH
1148 */
1149ENTRY(error_entry)
8c1f7558 1150 UNWIND_HINT_FUNC
ddeb8f21 1151 cld
76f5df43
DV
1152 SAVE_C_REGS 8
1153 SAVE_EXTRA_REGS 8
946c1911 1154 ENCODE_FRAME_POINTER 8
4d732138 1155 xorl %ebx, %ebx
03335e95 1156 testb $3, CS+8(%rsp)
cb6f64ed 1157 jz .Lerror_kernelspace
539f5113 1158
cb6f64ed
AL
1159 /*
1160 * We entered from user mode or we're pretending to have entered
1161 * from user mode due to an IRET fault.
1162 */
ddeb8f21 1163 SWAPGS
539f5113 1164
cb6f64ed 1165.Lerror_entry_from_usermode_after_swapgs:
f1075053
AL
1166 /*
1167 * We need to tell lockdep that IRQs are off. We can't do this until
1168 * we fix gsbase, and we should do it before enter_from_user_mode
1169 * (which can take locks).
1170 */
1171 TRACE_IRQS_OFF
478dc89c 1172 CALL_enter_from_user_mode
f1075053 1173 ret
02bc7768 1174
cb6f64ed 1175.Lerror_entry_done:
ddeb8f21
AH
1176 TRACE_IRQS_OFF
1177 ret
ddeb8f21 1178
ebfc453e
DV
1179 /*
1180 * There are two places in the kernel that can potentially fault with
1181 * usergs. Handle them here. B stepping K8s sometimes report a
1182 * truncated RIP for IRET exceptions returning to compat mode. Check
1183 * for these here too.
1184 */
cb6f64ed 1185.Lerror_kernelspace:
4d732138
IM
1186 incl %ebx
1187 leaq native_irq_return_iret(%rip), %rcx
1188 cmpq %rcx, RIP+8(%rsp)
cb6f64ed 1189 je .Lerror_bad_iret
4d732138
IM
1190 movl %ecx, %eax /* zero extend */
1191 cmpq %rax, RIP+8(%rsp)
cb6f64ed 1192 je .Lbstep_iret
42c748bb 1193 cmpq $.Lgs_change, RIP+8(%rsp)
cb6f64ed 1194 jne .Lerror_entry_done
539f5113
AL
1195
1196 /*
42c748bb 1197 * hack: .Lgs_change can fail with user gsbase. If this happens, fix up
539f5113 1198 * gsbase and proceed. We'll fix up the exception and land in
42c748bb 1199 * .Lgs_change's error handler with kernel gsbase.
539f5113 1200 */
2fa5f04f
WL
1201 SWAPGS
1202 jmp .Lerror_entry_done
ae24ffe5 1203
cb6f64ed 1204.Lbstep_iret:
ae24ffe5 1205 /* Fix truncated RIP */
4d732138 1206 movq %rcx, RIP+8(%rsp)
b645af2d
AL
1207 /* fall through */
1208
cb6f64ed 1209.Lerror_bad_iret:
539f5113
AL
1210 /*
1211 * We came from an IRET to user mode, so we have user gsbase.
1212 * Switch to kernel gsbase:
1213 */
b645af2d 1214 SWAPGS
539f5113
AL
1215
1216 /*
1217 * Pretend that the exception came from user mode: set up pt_regs
1218 * as if we faulted immediately after IRET and clear EBX so that
1219 * error_exit knows that we will be returning to user mode.
1220 */
4d732138
IM
1221 mov %rsp, %rdi
1222 call fixup_bad_iret
1223 mov %rax, %rsp
539f5113 1224 decl %ebx
cb6f64ed 1225 jmp .Lerror_entry_from_usermode_after_swapgs
ddeb8f21
AH
1226END(error_entry)
1227
1228
539f5113 1229/*
75ca5b22 1230 * On entry, EBX is a "return to kernel mode" flag:
539f5113
AL
1231 * 1: already in kernel mode, don't need SWAPGS
1232 * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
1233 */
ddeb8f21 1234ENTRY(error_exit)
8c1f7558 1235 UNWIND_HINT_REGS
2140a994 1236 DISABLE_INTERRUPTS(CLBR_ANY)
ddeb8f21 1237 TRACE_IRQS_OFF
2140a994 1238 testl %ebx, %ebx
4d732138
IM
1239 jnz retint_kernel
1240 jmp retint_user
ddeb8f21
AH
1241END(error_exit)
1242
929bacec
AL
1243/*
1244 * Runs on exception stack. Xen PV does not go through this path at all,
1245 * so we can use real assembly here.
1246 */
ddeb8f21 1247ENTRY(nmi)
8c1f7558 1248 UNWIND_HINT_IRET_REGS
929bacec 1249
3f3c8b8c
SR
1250 /*
1251 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1252 * the iretq it performs will take us out of NMI context.
1253 * This means that we can have nested NMIs where the next
1254 * NMI is using the top of the stack of the previous NMI. We
1255 * can't let it execute because the nested NMI will corrupt the
1256 * stack of the previous NMI. NMI handlers are not re-entrant
1257 * anyway.
1258 *
1259 * To handle this case we do the following:
1260 * Check the a special location on the stack that contains
1261 * a variable that is set when NMIs are executing.
1262 * The interrupted task's stack is also checked to see if it
1263 * is an NMI stack.
1264 * If the variable is not set and the stack is not the NMI
1265 * stack then:
1266 * o Set the special variable on the stack
0b22930e
AL
1267 * o Copy the interrupt frame into an "outermost" location on the
1268 * stack
1269 * o Copy the interrupt frame into an "iret" location on the stack
3f3c8b8c
SR
1270 * o Continue processing the NMI
1271 * If the variable is set or the previous stack is the NMI stack:
0b22930e 1272 * o Modify the "iret" location to jump to the repeat_nmi
3f3c8b8c
SR
1273 * o return back to the first NMI
1274 *
1275 * Now on exit of the first NMI, we first clear the stack variable
1276 * The NMI stack will tell any nested NMIs at that point that it is
1277 * nested. Then we pop the stack normally with iret, and if there was
1278 * a nested NMI that updated the copy interrupt stack frame, a
1279 * jump will be made to the repeat_nmi code that will handle the second
1280 * NMI.
9b6e6a83
AL
1281 *
1282 * However, espfix prevents us from directly returning to userspace
1283 * with a single IRET instruction. Similarly, IRET to user mode
1284 * can fault. We therefore handle NMIs from user space like
1285 * other IST entries.
3f3c8b8c
SR
1286 */
1287
e93c1730
AL
1288 ASM_CLAC
1289
146b2b09 1290 /* Use %rdx as our temp variable throughout */
4d732138 1291 pushq %rdx
3f3c8b8c 1292
9b6e6a83
AL
1293 testb $3, CS-RIP+8(%rsp)
1294 jz .Lnmi_from_kernel
1295
1296 /*
1297 * NMI from user mode. We need to run on the thread stack, but we
1298 * can't go through the normal entry paths: NMIs are masked, and
1299 * we don't want to enable interrupts, because then we'll end
1300 * up in an awkward situation in which IRQs are on but NMIs
1301 * are off.
83c133cf
AL
1302 *
1303 * We also must not push anything to the stack before switching
1304 * stacks lest we corrupt the "NMI executing" variable.
9b6e6a83
AL
1305 */
1306
929bacec 1307 swapgs
9b6e6a83
AL
1308 cld
1309 movq %rsp, %rdx
1310 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
8c1f7558 1311 UNWIND_HINT_IRET_REGS base=%rdx offset=8
9b6e6a83
AL
1312 pushq 5*8(%rdx) /* pt_regs->ss */
1313 pushq 4*8(%rdx) /* pt_regs->rsp */
1314 pushq 3*8(%rdx) /* pt_regs->flags */
1315 pushq 2*8(%rdx) /* pt_regs->cs */
1316 pushq 1*8(%rdx) /* pt_regs->rip */
8c1f7558 1317 UNWIND_HINT_IRET_REGS
9b6e6a83
AL
1318 pushq $-1 /* pt_regs->orig_ax */
1319 pushq %rdi /* pt_regs->di */
1320 pushq %rsi /* pt_regs->si */
1321 pushq (%rdx) /* pt_regs->dx */
1322 pushq %rcx /* pt_regs->cx */
1323 pushq %rax /* pt_regs->ax */
1324 pushq %r8 /* pt_regs->r8 */
1325 pushq %r9 /* pt_regs->r9 */
1326 pushq %r10 /* pt_regs->r10 */
1327 pushq %r11 /* pt_regs->r11 */
1328 pushq %rbx /* pt_regs->rbx */
1329 pushq %rbp /* pt_regs->rbp */
1330 pushq %r12 /* pt_regs->r12 */
1331 pushq %r13 /* pt_regs->r13 */
1332 pushq %r14 /* pt_regs->r14 */
1333 pushq %r15 /* pt_regs->r15 */
8c1f7558 1334 UNWIND_HINT_REGS
946c1911 1335 ENCODE_FRAME_POINTER
9b6e6a83
AL
1336
1337 /*
1338 * At this point we no longer need to worry about stack damage
1339 * due to nesting -- we're on the normal thread stack and we're
1340 * done with the NMI stack.
1341 */
1342
1343 movq %rsp, %rdi
1344 movq $-1, %rsi
1345 call do_nmi
1346
45d5a168 1347 /*
9b6e6a83 1348 * Return back to user mode. We must *not* do the normal exit
946c1911 1349 * work, because we don't want to enable interrupts.
45d5a168 1350 */
8a055d7f 1351 jmp swapgs_restore_regs_and_return_to_usermode
45d5a168 1352
9b6e6a83 1353.Lnmi_from_kernel:
3f3c8b8c 1354 /*
0b22930e
AL
1355 * Here's what our stack frame will look like:
1356 * +---------------------------------------------------------+
1357 * | original SS |
1358 * | original Return RSP |
1359 * | original RFLAGS |
1360 * | original CS |
1361 * | original RIP |
1362 * +---------------------------------------------------------+
1363 * | temp storage for rdx |
1364 * +---------------------------------------------------------+
1365 * | "NMI executing" variable |
1366 * +---------------------------------------------------------+
1367 * | iret SS } Copied from "outermost" frame |
1368 * | iret Return RSP } on each loop iteration; overwritten |
1369 * | iret RFLAGS } by a nested NMI to force another |
1370 * | iret CS } iteration if needed. |
1371 * | iret RIP } |
1372 * +---------------------------------------------------------+
1373 * | outermost SS } initialized in first_nmi; |
1374 * | outermost Return RSP } will not be changed before |
1375 * | outermost RFLAGS } NMI processing is done. |
1376 * | outermost CS } Copied to "iret" frame on each |
1377 * | outermost RIP } iteration. |
1378 * +---------------------------------------------------------+
1379 * | pt_regs |
1380 * +---------------------------------------------------------+
1381 *
1382 * The "original" frame is used by hardware. Before re-enabling
1383 * NMIs, we need to be done with it, and we need to leave enough
1384 * space for the asm code here.
1385 *
1386 * We return by executing IRET while RSP points to the "iret" frame.
1387 * That will either return for real or it will loop back into NMI
1388 * processing.
1389 *
1390 * The "outermost" frame is copied to the "iret" frame on each
1391 * iteration of the loop, so each iteration starts with the "iret"
1392 * frame pointing to the final return target.
1393 */
1394
45d5a168 1395 /*
0b22930e
AL
1396 * Determine whether we're a nested NMI.
1397 *
a27507ca
AL
1398 * If we interrupted kernel code between repeat_nmi and
1399 * end_repeat_nmi, then we are a nested NMI. We must not
1400 * modify the "iret" frame because it's being written by
1401 * the outer NMI. That's okay; the outer NMI handler is
1402 * about to about to call do_nmi anyway, so we can just
1403 * resume the outer NMI.
45d5a168 1404 */
a27507ca
AL
1405
1406 movq $repeat_nmi, %rdx
1407 cmpq 8(%rsp), %rdx
1408 ja 1f
1409 movq $end_repeat_nmi, %rdx
1410 cmpq 8(%rsp), %rdx
1411 ja nested_nmi_out
14121:
45d5a168 1413
3f3c8b8c 1414 /*
a27507ca 1415 * Now check "NMI executing". If it's set, then we're nested.
0b22930e
AL
1416 * This will not detect if we interrupted an outer NMI just
1417 * before IRET.
3f3c8b8c 1418 */
4d732138
IM
1419 cmpl $1, -8(%rsp)
1420 je nested_nmi
3f3c8b8c
SR
1421
1422 /*
0b22930e
AL
1423 * Now test if the previous stack was an NMI stack. This covers
1424 * the case where we interrupt an outer NMI after it clears
810bc075
AL
1425 * "NMI executing" but before IRET. We need to be careful, though:
1426 * there is one case in which RSP could point to the NMI stack
1427 * despite there being no NMI active: naughty userspace controls
1428 * RSP at the very beginning of the SYSCALL targets. We can
1429 * pull a fast one on naughty userspace, though: we program
1430 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1431 * if it controls the kernel's RSP. We set DF before we clear
1432 * "NMI executing".
3f3c8b8c 1433 */
0784b364
DV
1434 lea 6*8(%rsp), %rdx
1435 /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1436 cmpq %rdx, 4*8(%rsp)
1437 /* If the stack pointer is above the NMI stack, this is a normal NMI */
1438 ja first_nmi
4d732138 1439
0784b364
DV
1440 subq $EXCEPTION_STKSZ, %rdx
1441 cmpq %rdx, 4*8(%rsp)
1442 /* If it is below the NMI stack, it is a normal NMI */
1443 jb first_nmi
810bc075
AL
1444
1445 /* Ah, it is within the NMI stack. */
1446
1447 testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1448 jz first_nmi /* RSP was user controlled. */
1449
1450 /* This is a nested NMI. */
0784b364 1451
3f3c8b8c
SR
1452nested_nmi:
1453 /*
0b22930e
AL
1454 * Modify the "iret" frame to point to repeat_nmi, forcing another
1455 * iteration of NMI handling.
3f3c8b8c 1456 */
23a781e9 1457 subq $8, %rsp
4d732138
IM
1458 leaq -10*8(%rsp), %rdx
1459 pushq $__KERNEL_DS
1460 pushq %rdx
131484c8 1461 pushfq
4d732138
IM
1462 pushq $__KERNEL_CS
1463 pushq $repeat_nmi
3f3c8b8c
SR
1464
1465 /* Put stack back */
4d732138 1466 addq $(6*8), %rsp
3f3c8b8c
SR
1467
1468nested_nmi_out:
4d732138 1469 popq %rdx
3f3c8b8c 1470
0b22930e 1471 /* We are returning to kernel mode, so this cannot result in a fault. */
929bacec 1472 iretq
3f3c8b8c
SR
1473
1474first_nmi:
0b22930e 1475 /* Restore rdx. */
4d732138 1476 movq (%rsp), %rdx
62610913 1477
36f1a77b
AL
1478 /* Make room for "NMI executing". */
1479 pushq $0
3f3c8b8c 1480
0b22930e 1481 /* Leave room for the "iret" frame */
4d732138 1482 subq $(5*8), %rsp
28696f43 1483
0b22930e 1484 /* Copy the "original" frame to the "outermost" frame */
3f3c8b8c 1485 .rept 5
4d732138 1486 pushq 11*8(%rsp)
3f3c8b8c 1487 .endr
8c1f7558 1488 UNWIND_HINT_IRET_REGS
62610913 1489
79fb4ad6
SR
1490 /* Everything up to here is safe from nested NMIs */
1491
a97439aa
AL
1492#ifdef CONFIG_DEBUG_ENTRY
1493 /*
1494 * For ease of testing, unmask NMIs right away. Disabled by
1495 * default because IRET is very expensive.
1496 */
1497 pushq $0 /* SS */
1498 pushq %rsp /* RSP (minus 8 because of the previous push) */
1499 addq $8, (%rsp) /* Fix up RSP */
1500 pushfq /* RFLAGS */
1501 pushq $__KERNEL_CS /* CS */
1502 pushq $1f /* RIP */
929bacec 1503 iretq /* continues at repeat_nmi below */
8c1f7558 1504 UNWIND_HINT_IRET_REGS
a97439aa
AL
15051:
1506#endif
1507
0b22930e 1508repeat_nmi:
62610913
JB
1509 /*
1510 * If there was a nested NMI, the first NMI's iret will return
1511 * here. But NMIs are still enabled and we can take another
1512 * nested NMI. The nested NMI checks the interrupted RIP to see
1513 * if it is between repeat_nmi and end_repeat_nmi, and if so
1514 * it will just return, as we are about to repeat an NMI anyway.
1515 * This makes it safe to copy to the stack frame that a nested
1516 * NMI will update.
0b22930e
AL
1517 *
1518 * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
1519 * we're repeating an NMI, gsbase has the same value that it had on
1520 * the first iteration. paranoid_entry will load the kernel
36f1a77b
AL
1521 * gsbase if needed before we call do_nmi. "NMI executing"
1522 * is zero.
62610913 1523 */
36f1a77b 1524 movq $1, 10*8(%rsp) /* Set "NMI executing". */
3f3c8b8c 1525
62610913 1526 /*
0b22930e
AL
1527 * Copy the "outermost" frame to the "iret" frame. NMIs that nest
1528 * here must not modify the "iret" frame while we're writing to
1529 * it or it will end up containing garbage.
62610913 1530 */
4d732138 1531 addq $(10*8), %rsp
3f3c8b8c 1532 .rept 5
4d732138 1533 pushq -6*8(%rsp)
3f3c8b8c 1534 .endr
4d732138 1535 subq $(5*8), %rsp
62610913 1536end_repeat_nmi:
3f3c8b8c
SR
1537
1538 /*
0b22930e
AL
1539 * Everything below this point can be preempted by a nested NMI.
1540 * If this happens, then the inner NMI will change the "iret"
1541 * frame to point back to repeat_nmi.
3f3c8b8c 1542 */
4d732138 1543 pushq $-1 /* ORIG_RAX: no syscall to restart */
76f5df43
DV
1544 ALLOC_PT_GPREGS_ON_STACK
1545
1fd466ef 1546 /*
ebfc453e 1547 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1fd466ef
SR
1548 * as we should not be calling schedule in NMI context.
1549 * Even with normal interrupts enabled. An NMI should not be
1550 * setting NEED_RESCHED or anything that normal interrupts and
1551 * exceptions might do.
1552 */
4d732138 1553 call paranoid_entry
8c1f7558 1554 UNWIND_HINT_REGS
7fbb98c5 1555
ddeb8f21 1556 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
4d732138
IM
1557 movq %rsp, %rdi
1558 movq $-1, %rsi
1559 call do_nmi
7fbb98c5 1560
4d732138
IM
1561 testl %ebx, %ebx /* swapgs needed? */
1562 jnz nmi_restore
ddeb8f21
AH
1563nmi_swapgs:
1564 SWAPGS_UNSAFE_STACK
1565nmi_restore:
471ee483
AL
1566 POP_EXTRA_REGS
1567 POP_C_REGS
0b22930e 1568
471ee483
AL
1569 /*
1570 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1571 * at the "iret" frame.
1572 */
1573 addq $6*8, %rsp
28696f43 1574
810bc075
AL
1575 /*
1576 * Clear "NMI executing". Set DF first so that we can easily
1577 * distinguish the remaining code between here and IRET from
929bacec
AL
1578 * the SYSCALL entry and exit paths.
1579 *
1580 * We arguably should just inspect RIP instead, but I (Andy) wrote
1581 * this code when I had the misapprehension that Xen PV supported
1582 * NMIs, and Xen PV would break that approach.
810bc075
AL
1583 */
1584 std
1585 movq $0, 5*8(%rsp) /* clear "NMI executing" */
0b22930e
AL
1586
1587 /*
929bacec
AL
1588 * iretq reads the "iret" frame and exits the NMI stack in a
1589 * single instruction. We are returning to kernel mode, so this
1590 * cannot result in a fault. Similarly, we don't need to worry
1591 * about espfix64 on the way back to kernel mode.
0b22930e 1592 */
929bacec 1593 iretq
ddeb8f21
AH
1594END(nmi)
1595
1596ENTRY(ignore_sysret)
8c1f7558 1597 UNWIND_HINT_EMPTY
4d732138 1598 mov $-ENOSYS, %eax
ddeb8f21 1599 sysret
ddeb8f21 1600END(ignore_sysret)
2deb4be2
AL
1601
1602ENTRY(rewind_stack_do_exit)
8c1f7558 1603 UNWIND_HINT_FUNC
2deb4be2
AL
1604 /* Prevent any naive code from trying to unwind to our caller. */
1605 xorl %ebp, %ebp
1606
1607 movq PER_CPU_VAR(cpu_current_top_of_stack), %rax
8c1f7558
JP
1608 leaq -PTREGS_SIZE(%rax), %rsp
1609 UNWIND_HINT_FUNC sp_offset=PTREGS_SIZE
2deb4be2
AL
1610
1611 call do_exit
2deb4be2 1612END(rewind_stack_do_exit)