]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/x86/entry/entry_64.S
Merge branch 'x86/asm' into x86/core, to prepare for new patch
[karo-tx-linux.git] / arch / x86 / entry / entry_64.S
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>
7  */
8
9 /*
10  * entry.S contains the system-call and fault low-level handling routines.
11  *
12  * Some of this is documented in Documentation/x86/entry_64.txt
13  *
14  * NOTE: This code handles signal-recognition, which happens every time
15  * after an interrupt and after each system call.
16  *
17  * A note on terminology:
18  * - iret frame: Architecture defined interrupt frame from SS to RIP
19  * at the top of the kernel process stack.
20  *
21  * Some macro usage:
22  * - ENTRY/END Define functions in the symbol table.
23  * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
24  * - idtentry - Define exception entry points.
25  */
26
27 #include <linux/linkage.h>
28 #include <asm/segment.h>
29 #include <asm/cache.h>
30 #include <asm/errno.h>
31 #include "calling.h"
32 #include <asm/asm-offsets.h>
33 #include <asm/msr.h>
34 #include <asm/unistd.h>
35 #include <asm/thread_info.h>
36 #include <asm/hw_irq.h>
37 #include <asm/page_types.h>
38 #include <asm/irqflags.h>
39 #include <asm/paravirt.h>
40 #include <asm/percpu.h>
41 #include <asm/asm.h>
42 #include <asm/context_tracking.h>
43 #include <asm/smap.h>
44 #include <asm/pgtable_types.h>
45 #include <linux/err.h>
46
47 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this.  */
48 #include <linux/elf-em.h>
49 #define AUDIT_ARCH_X86_64       (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
50 #define __AUDIT_ARCH_64BIT 0x80000000
51 #define __AUDIT_ARCH_LE    0x40000000
52
53         .code64
54         .section .entry.text, "ax"
55
56
57 #ifdef CONFIG_PARAVIRT
58 ENTRY(native_usergs_sysret64)
59         swapgs
60         sysretq
61 ENDPROC(native_usergs_sysret64)
62 #endif /* CONFIG_PARAVIRT */
63
64
65 .macro TRACE_IRQS_IRETQ
66 #ifdef CONFIG_TRACE_IRQFLAGS
67         bt   $9,EFLAGS(%rsp)    /* interrupts off? */
68         jnc  1f
69         TRACE_IRQS_ON
70 1:
71 #endif
72 .endm
73
74 /*
75  * When dynamic function tracer is enabled it will add a breakpoint
76  * to all locations that it is about to modify, sync CPUs, update
77  * all the code, sync CPUs, then remove the breakpoints. In this time
78  * if lockdep is enabled, it might jump back into the debug handler
79  * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
80  *
81  * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
82  * make sure the stack pointer does not get reset back to the top
83  * of the debug stack, and instead just reuses the current stack.
84  */
85 #if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
86
87 .macro TRACE_IRQS_OFF_DEBUG
88         call debug_stack_set_zero
89         TRACE_IRQS_OFF
90         call debug_stack_reset
91 .endm
92
93 .macro TRACE_IRQS_ON_DEBUG
94         call debug_stack_set_zero
95         TRACE_IRQS_ON
96         call debug_stack_reset
97 .endm
98
99 .macro TRACE_IRQS_IRETQ_DEBUG
100         bt   $9,EFLAGS(%rsp)    /* interrupts off? */
101         jnc  1f
102         TRACE_IRQS_ON_DEBUG
103 1:
104 .endm
105
106 #else
107 # define TRACE_IRQS_OFF_DEBUG           TRACE_IRQS_OFF
108 # define TRACE_IRQS_ON_DEBUG            TRACE_IRQS_ON
109 # define TRACE_IRQS_IRETQ_DEBUG         TRACE_IRQS_IRETQ
110 #endif
111
112 /*
113  * 64bit SYSCALL instruction entry. Up to 6 arguments in registers.
114  *
115  * 64bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
116  * then loads new ss, cs, and rip from previously programmed MSRs.
117  * rflags gets masked by a value from another MSR (so CLD and CLAC
118  * are not needed). SYSCALL does not save anything on the stack
119  * and does not change rsp.
120  *
121  * Registers on entry:
122  * rax  system call number
123  * rcx  return address
124  * r11  saved rflags (note: r11 is callee-clobbered register in C ABI)
125  * rdi  arg0
126  * rsi  arg1
127  * rdx  arg2
128  * r10  arg3 (needs to be moved to rcx to conform to C ABI)
129  * r8   arg4
130  * r9   arg5
131  * (note: r12-r15,rbp,rbx are callee-preserved in C ABI)
132  *
133  * Only called from user space.
134  *
135  * When user can change pt_regs->foo always force IRET. That is because
136  * it deals with uncanonical addresses better. SYSRET has trouble
137  * with them due to bugs in both AMD and Intel CPUs.
138  */
139
140 ENTRY(entry_SYSCALL_64)
141         /*
142          * Interrupts are off on entry.
143          * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
144          * it is too small to ever cause noticeable irq latency.
145          */
146         SWAPGS_UNSAFE_STACK
147         /*
148          * A hypervisor implementation might want to use a label
149          * after the swapgs, so that it can do the swapgs
150          * for the guest and jump here on syscall.
151          */
152 GLOBAL(entry_SYSCALL_64_after_swapgs)
153
154         movq    %rsp,PER_CPU_VAR(rsp_scratch)
155         movq    PER_CPU_VAR(cpu_current_top_of_stack),%rsp
156
157         /* Construct struct pt_regs on stack */
158         pushq $__USER_DS                        /* pt_regs->ss */
159         pushq PER_CPU_VAR(rsp_scratch)  /* pt_regs->sp */
160         /*
161          * Re-enable interrupts.
162          * We use 'rsp_scratch' as a scratch space, hence irq-off block above
163          * must execute atomically in the face of possible interrupt-driven
164          * task preemption. We must enable interrupts only after we're done
165          * with using rsp_scratch:
166          */
167         ENABLE_INTERRUPTS(CLBR_NONE)
168         pushq   %r11                    /* pt_regs->flags */
169         pushq   $__USER_CS              /* pt_regs->cs */
170         pushq   %rcx                    /* pt_regs->ip */
171         pushq   %rax                    /* pt_regs->orig_ax */
172         pushq   %rdi                    /* pt_regs->di */
173         pushq   %rsi                    /* pt_regs->si */
174         pushq   %rdx                    /* pt_regs->dx */
175         pushq   %rcx                    /* pt_regs->cx */
176         pushq   $-ENOSYS                /* pt_regs->ax */
177         pushq   %r8                     /* pt_regs->r8 */
178         pushq   %r9                     /* pt_regs->r9 */
179         pushq   %r10                    /* pt_regs->r10 */
180         pushq   %r11                    /* pt_regs->r11 */
181         sub     $(6*8),%rsp /* pt_regs->bp,bx,r12-15 not saved */
182
183         testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
184         jnz tracesys
185 entry_SYSCALL_64_fastpath:
186 #if __SYSCALL_MASK == ~0
187         cmpq $__NR_syscall_max,%rax
188 #else
189         andl $__SYSCALL_MASK,%eax
190         cmpl $__NR_syscall_max,%eax
191 #endif
192         ja      1f      /* return -ENOSYS (already in pt_regs->ax) */
193         movq %r10,%rcx
194         call *sys_call_table(,%rax,8)
195         movq %rax,RAX(%rsp)
196 1:
197 /*
198  * Syscall return path ending with SYSRET (fast path).
199  * Has incompletely filled pt_regs.
200  */
201         LOCKDEP_SYS_EXIT
202         /*
203          * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
204          * it is too small to ever cause noticeable irq latency.
205          */
206         DISABLE_INTERRUPTS(CLBR_NONE)
207
208         /*
209          * We must check ti flags with interrupts (or at least preemption)
210          * off because we must *never* return to userspace without
211          * processing exit work that is enqueued if we're preempted here.
212          * In particular, returning to userspace with any of the one-shot
213          * flags (TIF_NOTIFY_RESUME, TIF_USER_RETURN_NOTIFY, etc) set is
214          * very bad.
215          */
216         testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
217         jnz int_ret_from_sys_call_irqs_off      /* Go to the slow path */
218
219         RESTORE_C_REGS_EXCEPT_RCX_R11
220         movq    RIP(%rsp),%rcx
221         movq    EFLAGS(%rsp),%r11
222         movq    RSP(%rsp),%rsp
223         /*
224          * 64bit SYSRET restores rip from rcx,
225          * rflags from r11 (but RF and VM bits are forced to 0),
226          * cs and ss are loaded from MSRs.
227          * Restoration of rflags re-enables interrupts.
228          *
229          * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss
230          * descriptor is not reinitialized.  This means that we should
231          * avoid SYSRET with SS == NULL, which could happen if we schedule,
232          * exit the kernel, and re-enter using an interrupt vector.  (All
233          * interrupt entries on x86_64 set SS to NULL.)  We prevent that
234          * from happening by reloading SS in __switch_to.  (Actually
235          * detecting the failure in 64-bit userspace is tricky but can be
236          * done.)
237          */
238         USERGS_SYSRET64
239
240         /* Do syscall entry tracing */
241 tracesys:
242         movq %rsp, %rdi
243         movl $AUDIT_ARCH_X86_64, %esi
244         call syscall_trace_enter_phase1
245         test %rax, %rax
246         jnz tracesys_phase2             /* if needed, run the slow path */
247         RESTORE_C_REGS_EXCEPT_RAX       /* else restore clobbered regs */
248         movq ORIG_RAX(%rsp), %rax
249         jmp entry_SYSCALL_64_fastpath   /*      and return to the fast path */
250
251 tracesys_phase2:
252         SAVE_EXTRA_REGS
253         movq %rsp, %rdi
254         movl $AUDIT_ARCH_X86_64, %esi
255         movq %rax,%rdx
256         call syscall_trace_enter_phase2
257
258         /*
259          * Reload registers from stack in case ptrace changed them.
260          * We don't reload %rax because syscall_trace_entry_phase2() returned
261          * the value it wants us to use in the table lookup.
262          */
263         RESTORE_C_REGS_EXCEPT_RAX
264         RESTORE_EXTRA_REGS
265 #if __SYSCALL_MASK == ~0
266         cmpq $__NR_syscall_max,%rax
267 #else
268         andl $__SYSCALL_MASK,%eax
269         cmpl $__NR_syscall_max,%eax
270 #endif
271         ja      1f      /* return -ENOSYS (already in pt_regs->ax) */
272         movq %r10,%rcx  /* fixup for C */
273         call *sys_call_table(,%rax,8)
274         movq %rax,RAX(%rsp)
275 1:
276         /* Use IRET because user could have changed pt_regs->foo */
277
278 /*
279  * Syscall return path ending with IRET.
280  * Has correct iret frame.
281  */
282 GLOBAL(int_ret_from_sys_call)
283         DISABLE_INTERRUPTS(CLBR_NONE)
284 int_ret_from_sys_call_irqs_off: /* jumps come here from the irqs-off SYSRET path */
285         TRACE_IRQS_OFF
286         movl $_TIF_ALLWORK_MASK,%edi
287         /* edi: mask to check */
288 GLOBAL(int_with_check)
289         LOCKDEP_SYS_EXIT_IRQ
290         GET_THREAD_INFO(%rcx)
291         movl TI_flags(%rcx),%edx
292         andl %edi,%edx
293         jnz   int_careful
294         andl    $~TS_COMPAT,TI_status(%rcx)
295         jmp     syscall_return
296
297         /* Either reschedule or signal or syscall exit tracking needed. */
298         /* First do a reschedule test. */
299         /* edx: work, edi: workmask */
300 int_careful:
301         bt $TIF_NEED_RESCHED,%edx
302         jnc  int_very_careful
303         TRACE_IRQS_ON
304         ENABLE_INTERRUPTS(CLBR_NONE)
305         pushq %rdi
306         SCHEDULE_USER
307         popq %rdi
308         DISABLE_INTERRUPTS(CLBR_NONE)
309         TRACE_IRQS_OFF
310         jmp int_with_check
311
312         /* handle signals and tracing -- both require a full pt_regs */
313 int_very_careful:
314         TRACE_IRQS_ON
315         ENABLE_INTERRUPTS(CLBR_NONE)
316         SAVE_EXTRA_REGS
317         /* Check for syscall exit trace */
318         testl $_TIF_WORK_SYSCALL_EXIT,%edx
319         jz int_signal
320         pushq %rdi
321         leaq 8(%rsp),%rdi       # &ptregs -> arg1
322         call syscall_trace_leave
323         popq %rdi
324         andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
325         jmp int_restore_rest
326
327 int_signal:
328         testl $_TIF_DO_NOTIFY_MASK,%edx
329         jz 1f
330         movq %rsp,%rdi          # &ptregs -> arg1
331         xorl %esi,%esi          # oldset -> arg2
332         call do_notify_resume
333 1:      movl $_TIF_WORK_MASK,%edi
334 int_restore_rest:
335         RESTORE_EXTRA_REGS
336         DISABLE_INTERRUPTS(CLBR_NONE)
337         TRACE_IRQS_OFF
338         jmp int_with_check
339
340 syscall_return:
341         /* The IRETQ could re-enable interrupts: */
342         DISABLE_INTERRUPTS(CLBR_ANY)
343         TRACE_IRQS_IRETQ
344
345         /*
346          * Try to use SYSRET instead of IRET if we're returning to
347          * a completely clean 64-bit userspace context.
348          */
349         movq RCX(%rsp),%rcx
350         movq RIP(%rsp),%r11
351         cmpq %rcx,%r11                  /* RCX == RIP */
352         jne opportunistic_sysret_failed
353
354         /*
355          * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
356          * in kernel space.  This essentially lets the user take over
357          * the kernel, since userspace controls RSP.
358          *
359          * If width of "canonical tail" ever becomes variable, this will need
360          * to be updated to remain correct on both old and new CPUs.
361          */
362         .ifne __VIRTUAL_MASK_SHIFT - 47
363         .error "virtual address width changed -- SYSRET checks need update"
364         .endif
365         /* Change top 16 bits to be the sign-extension of 47th bit */
366         shl     $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
367         sar     $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
368         /* If this changed %rcx, it was not canonical */
369         cmpq    %rcx, %r11
370         jne     opportunistic_sysret_failed
371
372         cmpq $__USER_CS,CS(%rsp)        /* CS must match SYSRET */
373         jne opportunistic_sysret_failed
374
375         movq R11(%rsp),%r11
376         cmpq %r11,EFLAGS(%rsp)          /* R11 == RFLAGS */
377         jne opportunistic_sysret_failed
378
379         /*
380          * SYSRET can't restore RF.  SYSRET can restore TF, but unlike IRET,
381          * restoring TF results in a trap from userspace immediately after
382          * SYSRET.  This would cause an infinite loop whenever #DB happens
383          * with register state that satisfies the opportunistic SYSRET
384          * conditions.  For example, single-stepping this user code:
385          *
386          *           movq $stuck_here,%rcx
387          *           pushfq
388          *           popq %r11
389          *   stuck_here:
390          *
391          * would never get past 'stuck_here'.
392          */
393         testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
394         jnz opportunistic_sysret_failed
395
396         /* nothing to check for RSP */
397
398         cmpq $__USER_DS,SS(%rsp)        /* SS must match SYSRET */
399         jne opportunistic_sysret_failed
400
401         /*
402          * We win!  This label is here just for ease of understanding
403          * perf profiles.  Nothing jumps here.
404          */
405 syscall_return_via_sysret:
406         /* rcx and r11 are already restored (see code above) */
407         RESTORE_C_REGS_EXCEPT_RCX_R11
408         movq RSP(%rsp),%rsp
409         USERGS_SYSRET64
410
411 opportunistic_sysret_failed:
412         SWAPGS
413         jmp     restore_c_regs_and_iret
414 END(entry_SYSCALL_64)
415
416
417         .macro FORK_LIKE func
418 ENTRY(stub_\func)
419         SAVE_EXTRA_REGS 8
420         jmp sys_\func
421 END(stub_\func)
422         .endm
423
424         FORK_LIKE  clone
425         FORK_LIKE  fork
426         FORK_LIKE  vfork
427
428 ENTRY(stub_execve)
429         call    sys_execve
430 return_from_execve:
431         testl   %eax, %eax
432         jz      1f
433         /* exec failed, can use fast SYSRET code path in this case */
434         ret
435 1:
436         /* must use IRET code path (pt_regs->cs may have changed) */
437         addq    $8, %rsp
438         ZERO_EXTRA_REGS
439         movq    %rax,RAX(%rsp)
440         jmp     int_ret_from_sys_call
441 END(stub_execve)
442 /*
443  * Remaining execve stubs are only 7 bytes long.
444  * ENTRY() often aligns to 16 bytes, which in this case has no benefits.
445  */
446         .align  8
447 GLOBAL(stub_execveat)
448         call    sys_execveat
449         jmp     return_from_execve
450 END(stub_execveat)
451
452 #if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION)
453         .align  8
454 GLOBAL(stub_x32_execve)
455 GLOBAL(stub32_execve)
456         call    compat_sys_execve
457         jmp     return_from_execve
458 END(stub32_execve)
459 END(stub_x32_execve)
460         .align  8
461 GLOBAL(stub_x32_execveat)
462 GLOBAL(stub32_execveat)
463         call    compat_sys_execveat
464         jmp     return_from_execve
465 END(stub32_execveat)
466 END(stub_x32_execveat)
467 #endif
468
469 /*
470  * sigreturn is special because it needs to restore all registers on return.
471  * This cannot be done with SYSRET, so use the IRET return path instead.
472  */
473 ENTRY(stub_rt_sigreturn)
474         /*
475          * SAVE_EXTRA_REGS result is not normally needed:
476          * sigreturn overwrites all pt_regs->GPREGS.
477          * But sigreturn can fail (!), and there is no easy way to detect that.
478          * To make sure RESTORE_EXTRA_REGS doesn't restore garbage on error,
479          * we SAVE_EXTRA_REGS here.
480          */
481         SAVE_EXTRA_REGS 8
482         call sys_rt_sigreturn
483 return_from_stub:
484         addq    $8, %rsp
485         RESTORE_EXTRA_REGS
486         movq %rax,RAX(%rsp)
487         jmp int_ret_from_sys_call
488 END(stub_rt_sigreturn)
489
490 #ifdef CONFIG_X86_X32_ABI
491 ENTRY(stub_x32_rt_sigreturn)
492         SAVE_EXTRA_REGS 8
493         call sys32_x32_rt_sigreturn
494         jmp  return_from_stub
495 END(stub_x32_rt_sigreturn)
496 #endif
497
498 /*
499  * A newly forked process directly context switches into this address.
500  *
501  * rdi: prev task we switched from
502  */
503 ENTRY(ret_from_fork)
504
505         LOCK ; btr $TIF_FORK,TI_flags(%r8)
506
507         pushq $0x0002
508         popfq                           # reset kernel eflags
509
510         call schedule_tail                      # rdi: 'prev' task parameter
511
512         RESTORE_EXTRA_REGS
513
514         testb   $3, CS(%rsp)                    # from kernel_thread?
515
516         /*
517          * By the time we get here, we have no idea whether our pt_regs,
518          * ti flags, and ti status came from the 64-bit SYSCALL fast path,
519          * the slow path, or one of the 32-bit compat paths.
520          * Use IRET code path to return, since it can safely handle
521          * all of the above.
522          */
523         jnz     int_ret_from_sys_call
524
525         /* We came from kernel_thread */
526         /* nb: we depend on RESTORE_EXTRA_REGS above */
527         movq %rbp, %rdi
528         call *%rbx
529         movl $0, RAX(%rsp)
530         RESTORE_EXTRA_REGS
531         jmp int_ret_from_sys_call
532 END(ret_from_fork)
533
534 /*
535  * Build the entry stubs with some assembler magic.
536  * We pack 1 stub into every 8-byte block.
537  */
538         .align 8
539 ENTRY(irq_entries_start)
540     vector=FIRST_EXTERNAL_VECTOR
541     .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
542         pushq $(~vector+0x80)   /* Note: always in signed byte range */
543     vector=vector+1
544         jmp     common_interrupt
545         .align  8
546     .endr
547 END(irq_entries_start)
548
549 /*
550  * Interrupt entry/exit.
551  *
552  * Interrupt entry points save only callee clobbered registers in fast path.
553  *
554  * Entry runs with interrupts off.
555  */
556
557 /* 0(%rsp): ~(interrupt number) */
558         .macro interrupt func
559         cld
560         /*
561          * Since nothing in interrupt handling code touches r12...r15 members
562          * of "struct pt_regs", and since interrupts can nest, we can save
563          * four stack slots and simultaneously provide
564          * an unwind-friendly stack layout by saving "truncated" pt_regs
565          * exactly up to rbp slot, without these members.
566          */
567         ALLOC_PT_GPREGS_ON_STACK -RBP
568         SAVE_C_REGS -RBP
569         /* this goes to 0(%rsp) for unwinder, not for saving the value: */
570         SAVE_EXTRA_REGS_RBP -RBP
571
572         leaq -RBP(%rsp),%rdi    /* arg1 for \func (pointer to pt_regs) */
573
574         testb   $3, CS-RBP(%rsp)
575         jz      1f
576         SWAPGS
577 1:
578         /*
579          * Save previous stack pointer, optionally switch to interrupt stack.
580          * irq_count is used to check if a CPU is already on an interrupt stack
581          * or not. While this is essentially redundant with preempt_count it is
582          * a little cheaper to use a separate counter in the PDA (short of
583          * moving irq_enter into assembly, which would be too much work)
584          */
585         movq %rsp, %rsi
586         incl PER_CPU_VAR(irq_count)
587         cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
588         pushq %rsi
589         /* We entered an interrupt context - irqs are off: */
590         TRACE_IRQS_OFF
591
592         call \func
593         .endm
594
595         /*
596          * The interrupt stubs push (~vector+0x80) onto the stack and
597          * then jump to common_interrupt.
598          */
599         .p2align CONFIG_X86_L1_CACHE_SHIFT
600 common_interrupt:
601         ASM_CLAC
602         addq $-0x80,(%rsp)              /* Adjust vector to [-256,-1] range */
603         interrupt do_IRQ
604         /* 0(%rsp): old RSP */
605 ret_from_intr:
606         DISABLE_INTERRUPTS(CLBR_NONE)
607         TRACE_IRQS_OFF
608         decl PER_CPU_VAR(irq_count)
609
610         /* Restore saved previous stack */
611         popq %rsi
612         /* return code expects complete pt_regs - adjust rsp accordingly: */
613         leaq -RBP(%rsi),%rsp
614
615         testb   $3, CS(%rsp)
616         jz      retint_kernel
617         /* Interrupt came from user space */
618 retint_user:
619         GET_THREAD_INFO(%rcx)
620         /*
621          * %rcx: thread info. Interrupts off.
622          */
623 retint_with_reschedule:
624         movl $_TIF_WORK_MASK,%edi
625 retint_check:
626         LOCKDEP_SYS_EXIT_IRQ
627         movl TI_flags(%rcx),%edx
628         andl %edi,%edx
629         jnz  retint_careful
630
631 retint_swapgs:          /* return to user-space */
632         /*
633          * The iretq could re-enable interrupts:
634          */
635         DISABLE_INTERRUPTS(CLBR_ANY)
636         TRACE_IRQS_IRETQ
637
638         SWAPGS
639         jmp     restore_c_regs_and_iret
640
641 /* Returning to kernel space */
642 retint_kernel:
643 #ifdef CONFIG_PREEMPT
644         /* Interrupts are off */
645         /* Check if we need preemption */
646         bt      $9,EFLAGS(%rsp) /* interrupts were off? */
647         jnc     1f
648 0:      cmpl    $0,PER_CPU_VAR(__preempt_count)
649         jnz     1f
650         call    preempt_schedule_irq
651         jmp     0b
652 1:
653 #endif
654         /*
655          * The iretq could re-enable interrupts:
656          */
657         TRACE_IRQS_IRETQ
658
659 /*
660  * At this label, code paths which return to kernel and to user,
661  * which come from interrupts/exception and from syscalls, merge.
662  */
663 restore_c_regs_and_iret:
664         RESTORE_C_REGS
665         REMOVE_PT_GPREGS_FROM_STACK 8
666         INTERRUPT_RETURN
667
668 ENTRY(native_iret)
669         /*
670          * Are we returning to a stack segment from the LDT?  Note: in
671          * 64-bit mode SS:RSP on the exception stack is always valid.
672          */
673 #ifdef CONFIG_X86_ESPFIX64
674         testb $4,(SS-RIP)(%rsp)
675         jnz native_irq_return_ldt
676 #endif
677
678 .global native_irq_return_iret
679 native_irq_return_iret:
680         /*
681          * This may fault.  Non-paranoid faults on return to userspace are
682          * handled by fixup_bad_iret.  These include #SS, #GP, and #NP.
683          * Double-faults due to espfix64 are handled in do_double_fault.
684          * Other faults here are fatal.
685          */
686         iretq
687
688 #ifdef CONFIG_X86_ESPFIX64
689 native_irq_return_ldt:
690         pushq %rax
691         pushq %rdi
692         SWAPGS
693         movq PER_CPU_VAR(espfix_waddr),%rdi
694         movq %rax,(0*8)(%rdi)   /* RAX */
695         movq (2*8)(%rsp),%rax   /* RIP */
696         movq %rax,(1*8)(%rdi)
697         movq (3*8)(%rsp),%rax   /* CS */
698         movq %rax,(2*8)(%rdi)
699         movq (4*8)(%rsp),%rax   /* RFLAGS */
700         movq %rax,(3*8)(%rdi)
701         movq (6*8)(%rsp),%rax   /* SS */
702         movq %rax,(5*8)(%rdi)
703         movq (5*8)(%rsp),%rax   /* RSP */
704         movq %rax,(4*8)(%rdi)
705         andl $0xffff0000,%eax
706         popq %rdi
707         orq PER_CPU_VAR(espfix_stack),%rax
708         SWAPGS
709         movq %rax,%rsp
710         popq %rax
711         jmp native_irq_return_iret
712 #endif
713
714         /* edi: workmask, edx: work */
715 retint_careful:
716         bt    $TIF_NEED_RESCHED,%edx
717         jnc   retint_signal
718         TRACE_IRQS_ON
719         ENABLE_INTERRUPTS(CLBR_NONE)
720         pushq %rdi
721         SCHEDULE_USER
722         popq %rdi
723         GET_THREAD_INFO(%rcx)
724         DISABLE_INTERRUPTS(CLBR_NONE)
725         TRACE_IRQS_OFF
726         jmp retint_check
727
728 retint_signal:
729         testl $_TIF_DO_NOTIFY_MASK,%edx
730         jz    retint_swapgs
731         TRACE_IRQS_ON
732         ENABLE_INTERRUPTS(CLBR_NONE)
733         SAVE_EXTRA_REGS
734         movq $-1,ORIG_RAX(%rsp)
735         xorl %esi,%esi          # oldset
736         movq %rsp,%rdi          # &pt_regs
737         call do_notify_resume
738         RESTORE_EXTRA_REGS
739         DISABLE_INTERRUPTS(CLBR_NONE)
740         TRACE_IRQS_OFF
741         GET_THREAD_INFO(%rcx)
742         jmp retint_with_reschedule
743
744 END(common_interrupt)
745
746 /*
747  * APIC interrupts.
748  */
749 .macro apicinterrupt3 num sym do_sym
750 ENTRY(\sym)
751         ASM_CLAC
752         pushq $~(\num)
753 .Lcommon_\sym:
754         interrupt \do_sym
755         jmp ret_from_intr
756 END(\sym)
757 .endm
758
759 #ifdef CONFIG_TRACING
760 #define trace(sym) trace_##sym
761 #define smp_trace(sym) smp_trace_##sym
762
763 .macro trace_apicinterrupt num sym
764 apicinterrupt3 \num trace(\sym) smp_trace(\sym)
765 .endm
766 #else
767 .macro trace_apicinterrupt num sym do_sym
768 .endm
769 #endif
770
771 .macro apicinterrupt num sym do_sym
772 apicinterrupt3 \num \sym \do_sym
773 trace_apicinterrupt \num \sym
774 .endm
775
776 #ifdef CONFIG_SMP
777 apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR \
778         irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
779 apicinterrupt3 REBOOT_VECTOR \
780         reboot_interrupt smp_reboot_interrupt
781 #endif
782
783 #ifdef CONFIG_X86_UV
784 apicinterrupt3 UV_BAU_MESSAGE \
785         uv_bau_message_intr1 uv_bau_message_interrupt
786 #endif
787 apicinterrupt LOCAL_TIMER_VECTOR \
788         apic_timer_interrupt smp_apic_timer_interrupt
789 apicinterrupt X86_PLATFORM_IPI_VECTOR \
790         x86_platform_ipi smp_x86_platform_ipi
791
792 #ifdef CONFIG_HAVE_KVM
793 apicinterrupt3 POSTED_INTR_VECTOR \
794         kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
795 apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR \
796         kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
797 #endif
798
799 #ifdef CONFIG_X86_MCE_THRESHOLD
800 apicinterrupt THRESHOLD_APIC_VECTOR \
801         threshold_interrupt smp_threshold_interrupt
802 #endif
803
804 #ifdef CONFIG_X86_MCE_AMD
805 apicinterrupt DEFERRED_ERROR_VECTOR \
806         deferred_error_interrupt smp_deferred_error_interrupt
807 #endif
808
809 #ifdef CONFIG_X86_THERMAL_VECTOR
810 apicinterrupt THERMAL_APIC_VECTOR \
811         thermal_interrupt smp_thermal_interrupt
812 #endif
813
814 #ifdef CONFIG_SMP
815 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
816         call_function_single_interrupt smp_call_function_single_interrupt
817 apicinterrupt CALL_FUNCTION_VECTOR \
818         call_function_interrupt smp_call_function_interrupt
819 apicinterrupt RESCHEDULE_VECTOR \
820         reschedule_interrupt smp_reschedule_interrupt
821 #endif
822
823 apicinterrupt ERROR_APIC_VECTOR \
824         error_interrupt smp_error_interrupt
825 apicinterrupt SPURIOUS_APIC_VECTOR \
826         spurious_interrupt smp_spurious_interrupt
827
828 #ifdef CONFIG_IRQ_WORK
829 apicinterrupt IRQ_WORK_VECTOR \
830         irq_work_interrupt smp_irq_work_interrupt
831 #endif
832
833 /*
834  * Exception entry points.
835  */
836 #define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
837
838 .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
839 ENTRY(\sym)
840         /* Sanity check */
841         .if \shift_ist != -1 && \paranoid == 0
842         .error "using shift_ist requires paranoid=1"
843         .endif
844
845         ASM_CLAC
846         PARAVIRT_ADJUST_EXCEPTION_FRAME
847
848         .ifeq \has_error_code
849         pushq $-1                       /* ORIG_RAX: no syscall to restart */
850         .endif
851
852         ALLOC_PT_GPREGS_ON_STACK
853
854         .if \paranoid
855         .if \paranoid == 1
856         testb   $3, CS(%rsp)            /* If coming from userspace, switch */
857         jnz 1f                          /* stacks. */
858         .endif
859         call paranoid_entry
860         .else
861         call error_entry
862         .endif
863         /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
864
865         .if \paranoid
866         .if \shift_ist != -1
867         TRACE_IRQS_OFF_DEBUG            /* reload IDT in case of recursion */
868         .else
869         TRACE_IRQS_OFF
870         .endif
871         .endif
872
873         movq %rsp,%rdi                  /* pt_regs pointer */
874
875         .if \has_error_code
876         movq ORIG_RAX(%rsp),%rsi        /* get error code */
877         movq $-1,ORIG_RAX(%rsp)         /* no syscall to restart */
878         .else
879         xorl %esi,%esi                  /* no error code */
880         .endif
881
882         .if \shift_ist != -1
883         subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
884         .endif
885
886         call \do_sym
887
888         .if \shift_ist != -1
889         addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
890         .endif
891
892         /* these procedures expect "no swapgs" flag in ebx */
893         .if \paranoid
894         jmp paranoid_exit
895         .else
896         jmp error_exit
897         .endif
898
899         .if \paranoid == 1
900         /*
901          * Paranoid entry from userspace.  Switch stacks and treat it
902          * as a normal entry.  This means that paranoid handlers
903          * run in real process context if user_mode(regs).
904          */
905 1:
906         call error_entry
907
908
909         movq %rsp,%rdi                  /* pt_regs pointer */
910         call sync_regs
911         movq %rax,%rsp                  /* switch stack */
912
913         movq %rsp,%rdi                  /* pt_regs pointer */
914
915         .if \has_error_code
916         movq ORIG_RAX(%rsp),%rsi        /* get error code */
917         movq $-1,ORIG_RAX(%rsp)         /* no syscall to restart */
918         .else
919         xorl %esi,%esi                  /* no error code */
920         .endif
921
922         call \do_sym
923
924         jmp error_exit                  /* %ebx: no swapgs flag */
925         .endif
926 END(\sym)
927 .endm
928
929 #ifdef CONFIG_TRACING
930 .macro trace_idtentry sym do_sym has_error_code:req
931 idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
932 idtentry \sym \do_sym has_error_code=\has_error_code
933 .endm
934 #else
935 .macro trace_idtentry sym do_sym has_error_code:req
936 idtentry \sym \do_sym has_error_code=\has_error_code
937 .endm
938 #endif
939
940 idtentry divide_error do_divide_error has_error_code=0
941 idtentry overflow do_overflow has_error_code=0
942 idtentry bounds do_bounds has_error_code=0
943 idtentry invalid_op do_invalid_op has_error_code=0
944 idtentry device_not_available do_device_not_available has_error_code=0
945 idtentry double_fault do_double_fault has_error_code=1 paranoid=2
946 idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
947 idtentry invalid_TSS do_invalid_TSS has_error_code=1
948 idtentry segment_not_present do_segment_not_present has_error_code=1
949 idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
950 idtentry coprocessor_error do_coprocessor_error has_error_code=0
951 idtentry alignment_check do_alignment_check has_error_code=1
952 idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
953
954
955         /* Reload gs selector with exception handling */
956         /* edi:  new selector */
957 ENTRY(native_load_gs_index)
958         pushfq
959         DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
960         SWAPGS
961 gs_change:
962         movl %edi,%gs
963 2:      mfence          /* workaround */
964         SWAPGS
965         popfq
966         ret
967 END(native_load_gs_index)
968
969         _ASM_EXTABLE(gs_change,bad_gs)
970         .section .fixup,"ax"
971         /* running with kernelgs */
972 bad_gs:
973         SWAPGS                  /* switch back to user gs */
974         xorl %eax,%eax
975         movl %eax,%gs
976         jmp  2b
977         .previous
978
979 /* Call softirq on interrupt stack. Interrupts are off. */
980 ENTRY(do_softirq_own_stack)
981         pushq %rbp
982         mov  %rsp,%rbp
983         incl PER_CPU_VAR(irq_count)
984         cmove PER_CPU_VAR(irq_stack_ptr),%rsp
985         push  %rbp                      # backlink for old unwinder
986         call __do_softirq
987         leaveq
988         decl PER_CPU_VAR(irq_count)
989         ret
990 END(do_softirq_own_stack)
991
992 #ifdef CONFIG_XEN
993 idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
994
995 /*
996  * A note on the "critical region" in our callback handler.
997  * We want to avoid stacking callback handlers due to events occurring
998  * during handling of the last event. To do this, we keep events disabled
999  * until we've done all processing. HOWEVER, we must enable events before
1000  * popping the stack frame (can't be done atomically) and so it would still
1001  * be possible to get enough handler activations to overflow the stack.
1002  * Although unlikely, bugs of that kind are hard to track down, so we'd
1003  * like to avoid the possibility.
1004  * So, on entry to the handler we detect whether we interrupted an
1005  * existing activation in its critical region -- if so, we pop the current
1006  * activation and restart the handler using the previous one.
1007  */
1008 ENTRY(xen_do_hypervisor_callback)   # do_hypervisor_callback(struct *pt_regs)
1009 /*
1010  * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1011  * see the correct pointer to the pt_regs
1012  */
1013         movq %rdi, %rsp            # we don't return, adjust the stack frame
1014 11:     incl PER_CPU_VAR(irq_count)
1015         movq %rsp,%rbp
1016         cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1017         pushq %rbp                      # backlink for old unwinder
1018         call xen_evtchn_do_upcall
1019         popq %rsp
1020         decl PER_CPU_VAR(irq_count)
1021 #ifndef CONFIG_PREEMPT
1022         call xen_maybe_preempt_hcall
1023 #endif
1024         jmp  error_exit
1025 END(xen_do_hypervisor_callback)
1026
1027 /*
1028  * Hypervisor uses this for application faults while it executes.
1029  * We get here for two reasons:
1030  *  1. Fault while reloading DS, ES, FS or GS
1031  *  2. Fault while executing IRET
1032  * Category 1 we do not need to fix up as Xen has already reloaded all segment
1033  * registers that could be reloaded and zeroed the others.
1034  * Category 2 we fix up by killing the current process. We cannot use the
1035  * normal Linux return path in this case because if we use the IRET hypercall
1036  * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1037  * We distinguish between categories by comparing each saved segment register
1038  * with its current contents: any discrepancy means we in category 1.
1039  */
1040 ENTRY(xen_failsafe_callback)
1041         movl %ds,%ecx
1042         cmpw %cx,0x10(%rsp)
1043         jne 1f
1044         movl %es,%ecx
1045         cmpw %cx,0x18(%rsp)
1046         jne 1f
1047         movl %fs,%ecx
1048         cmpw %cx,0x20(%rsp)
1049         jne 1f
1050         movl %gs,%ecx
1051         cmpw %cx,0x28(%rsp)
1052         jne 1f
1053         /* All segments match their saved values => Category 2 (Bad IRET). */
1054         movq (%rsp),%rcx
1055         movq 8(%rsp),%r11
1056         addq $0x30,%rsp
1057         pushq $0        /* RIP */
1058         pushq %r11
1059         pushq %rcx
1060         jmp general_protection
1061 1:      /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1062         movq (%rsp),%rcx
1063         movq 8(%rsp),%r11
1064         addq $0x30,%rsp
1065         pushq $-1 /* orig_ax = -1 => not a system call */
1066         ALLOC_PT_GPREGS_ON_STACK
1067         SAVE_C_REGS
1068         SAVE_EXTRA_REGS
1069         jmp error_exit
1070 END(xen_failsafe_callback)
1071
1072 apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1073         xen_hvm_callback_vector xen_evtchn_do_upcall
1074
1075 #endif /* CONFIG_XEN */
1076
1077 #if IS_ENABLED(CONFIG_HYPERV)
1078 apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1079         hyperv_callback_vector hyperv_vector_handler
1080 #endif /* CONFIG_HYPERV */
1081
1082 idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1083 idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1084 idtentry stack_segment do_stack_segment has_error_code=1
1085 #ifdef CONFIG_XEN
1086 idtentry xen_debug do_debug has_error_code=0
1087 idtentry xen_int3 do_int3 has_error_code=0
1088 idtentry xen_stack_segment do_stack_segment has_error_code=1
1089 #endif
1090 idtentry general_protection do_general_protection has_error_code=1
1091 trace_idtentry page_fault do_page_fault has_error_code=1
1092 #ifdef CONFIG_KVM_GUEST
1093 idtentry async_page_fault do_async_page_fault has_error_code=1
1094 #endif
1095 #ifdef CONFIG_X86_MCE
1096 idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
1097 #endif
1098
1099 /*
1100  * Save all registers in pt_regs, and switch gs if needed.
1101  * Use slow, but surefire "are we in kernel?" check.
1102  * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1103  */
1104 ENTRY(paranoid_entry)
1105         cld
1106         SAVE_C_REGS 8
1107         SAVE_EXTRA_REGS 8
1108         movl $1,%ebx
1109         movl $MSR_GS_BASE,%ecx
1110         rdmsr
1111         testl %edx,%edx
1112         js 1f   /* negative -> in kernel */
1113         SWAPGS
1114         xorl %ebx,%ebx
1115 1:      ret
1116 END(paranoid_entry)
1117
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.
1127  */
1128 /* On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) */
1129 ENTRY(paranoid_exit)
1130         DISABLE_INTERRUPTS(CLBR_NONE)
1131         TRACE_IRQS_OFF_DEBUG
1132         testl %ebx,%ebx                         /* swapgs needed? */
1133         jnz paranoid_exit_no_swapgs
1134         TRACE_IRQS_IRETQ
1135         SWAPGS_UNSAFE_STACK
1136         jmp paranoid_exit_restore
1137 paranoid_exit_no_swapgs:
1138         TRACE_IRQS_IRETQ_DEBUG
1139 paranoid_exit_restore:
1140         RESTORE_EXTRA_REGS
1141         RESTORE_C_REGS
1142         REMOVE_PT_GPREGS_FROM_STACK 8
1143         INTERRUPT_RETURN
1144 END(paranoid_exit)
1145
1146 /*
1147  * Save all registers in pt_regs, and switch gs if needed.
1148  * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1149  */
1150 ENTRY(error_entry)
1151         cld
1152         SAVE_C_REGS 8
1153         SAVE_EXTRA_REGS 8
1154         xorl %ebx,%ebx
1155         testb   $3, CS+8(%rsp)
1156         jz      error_kernelspace
1157 error_swapgs:
1158         SWAPGS
1159 error_sti:
1160         TRACE_IRQS_OFF
1161         ret
1162
1163         /*
1164          * There are two places in the kernel that can potentially fault with
1165          * usergs. Handle them here.  B stepping K8s sometimes report a
1166          * truncated RIP for IRET exceptions returning to compat mode. Check
1167          * for these here too.
1168          */
1169 error_kernelspace:
1170         incl %ebx
1171         leaq native_irq_return_iret(%rip),%rcx
1172         cmpq %rcx,RIP+8(%rsp)
1173         je error_bad_iret
1174         movl %ecx,%eax  /* zero extend */
1175         cmpq %rax,RIP+8(%rsp)
1176         je bstep_iret
1177         cmpq $gs_change,RIP+8(%rsp)
1178         je error_swapgs
1179         jmp error_sti
1180
1181 bstep_iret:
1182         /* Fix truncated RIP */
1183         movq %rcx,RIP+8(%rsp)
1184         /* fall through */
1185
1186 error_bad_iret:
1187         SWAPGS
1188         mov %rsp,%rdi
1189         call fixup_bad_iret
1190         mov %rax,%rsp
1191         decl %ebx       /* Return to usergs */
1192         jmp error_sti
1193 END(error_entry)
1194
1195
1196 /* On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) */
1197 ENTRY(error_exit)
1198         movl %ebx,%eax
1199         RESTORE_EXTRA_REGS
1200         DISABLE_INTERRUPTS(CLBR_NONE)
1201         TRACE_IRQS_OFF
1202         testl %eax,%eax
1203         jnz retint_kernel
1204         jmp retint_user
1205 END(error_exit)
1206
1207 /* Runs on exception stack */
1208 ENTRY(nmi)
1209         PARAVIRT_ADJUST_EXCEPTION_FRAME
1210         /*
1211          * We allow breakpoints in NMIs. If a breakpoint occurs, then
1212          * the iretq it performs will take us out of NMI context.
1213          * This means that we can have nested NMIs where the next
1214          * NMI is using the top of the stack of the previous NMI. We
1215          * can't let it execute because the nested NMI will corrupt the
1216          * stack of the previous NMI. NMI handlers are not re-entrant
1217          * anyway.
1218          *
1219          * To handle this case we do the following:
1220          *  Check the a special location on the stack that contains
1221          *  a variable that is set when NMIs are executing.
1222          *  The interrupted task's stack is also checked to see if it
1223          *  is an NMI stack.
1224          *  If the variable is not set and the stack is not the NMI
1225          *  stack then:
1226          *    o Set the special variable on the stack
1227          *    o Copy the interrupt frame into a "saved" location on the stack
1228          *    o Copy the interrupt frame into a "copy" location on the stack
1229          *    o Continue processing the NMI
1230          *  If the variable is set or the previous stack is the NMI stack:
1231          *    o Modify the "copy" location to jump to the repeate_nmi
1232          *    o return back to the first NMI
1233          *
1234          * Now on exit of the first NMI, we first clear the stack variable
1235          * The NMI stack will tell any nested NMIs at that point that it is
1236          * nested. Then we pop the stack normally with iret, and if there was
1237          * a nested NMI that updated the copy interrupt stack frame, a
1238          * jump will be made to the repeat_nmi code that will handle the second
1239          * NMI.
1240          */
1241
1242         /* Use %rdx as our temp variable throughout */
1243         pushq %rdx
1244
1245         /*
1246          * If %cs was not the kernel segment, then the NMI triggered in user
1247          * space, which means it is definitely not nested.
1248          */
1249         cmpl $__KERNEL_CS, 16(%rsp)
1250         jne first_nmi
1251
1252         /*
1253          * Check the special variable on the stack to see if NMIs are
1254          * executing.
1255          */
1256         cmpl $1, -8(%rsp)
1257         je nested_nmi
1258
1259         /*
1260          * Now test if the previous stack was an NMI stack.
1261          * We need the double check. We check the NMI stack to satisfy the
1262          * race when the first NMI clears the variable before returning.
1263          * We check the variable because the first NMI could be in a
1264          * breakpoint routine using a breakpoint stack.
1265          */
1266         lea     6*8(%rsp), %rdx
1267         /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1268         cmpq    %rdx, 4*8(%rsp)
1269         /* If the stack pointer is above the NMI stack, this is a normal NMI */
1270         ja      first_nmi
1271         subq    $EXCEPTION_STKSZ, %rdx
1272         cmpq    %rdx, 4*8(%rsp)
1273         /* If it is below the NMI stack, it is a normal NMI */
1274         jb      first_nmi
1275         /* Ah, it is within the NMI stack, treat it as nested */
1276
1277 nested_nmi:
1278         /*
1279          * Do nothing if we interrupted the fixup in repeat_nmi.
1280          * It's about to repeat the NMI handler, so we are fine
1281          * with ignoring this one.
1282          */
1283         movq $repeat_nmi, %rdx
1284         cmpq 8(%rsp), %rdx
1285         ja 1f
1286         movq $end_repeat_nmi, %rdx
1287         cmpq 8(%rsp), %rdx
1288         ja nested_nmi_out
1289
1290 1:
1291         /* Set up the interrupted NMIs stack to jump to repeat_nmi */
1292         leaq -1*8(%rsp), %rdx
1293         movq %rdx, %rsp
1294         leaq -10*8(%rsp), %rdx
1295         pushq $__KERNEL_DS
1296         pushq %rdx
1297         pushfq
1298         pushq $__KERNEL_CS
1299         pushq $repeat_nmi
1300
1301         /* Put stack back */
1302         addq $(6*8), %rsp
1303
1304 nested_nmi_out:
1305         popq %rdx
1306
1307         /* No need to check faults here */
1308         INTERRUPT_RETURN
1309
1310 first_nmi:
1311         /*
1312          * Because nested NMIs will use the pushed location that we
1313          * stored in rdx, we must keep that space available.
1314          * Here's what our stack frame will look like:
1315          * +-------------------------+
1316          * | original SS             |
1317          * | original Return RSP     |
1318          * | original RFLAGS         |
1319          * | original CS             |
1320          * | original RIP            |
1321          * +-------------------------+
1322          * | temp storage for rdx    |
1323          * +-------------------------+
1324          * | NMI executing variable  |
1325          * +-------------------------+
1326          * | copied SS               |
1327          * | copied Return RSP       |
1328          * | copied RFLAGS           |
1329          * | copied CS               |
1330          * | copied RIP              |
1331          * +-------------------------+
1332          * | Saved SS                |
1333          * | Saved Return RSP        |
1334          * | Saved RFLAGS            |
1335          * | Saved CS                |
1336          * | Saved RIP               |
1337          * +-------------------------+
1338          * | pt_regs                 |
1339          * +-------------------------+
1340          *
1341          * The saved stack frame is used to fix up the copied stack frame
1342          * that a nested NMI may change to make the interrupted NMI iret jump
1343          * to the repeat_nmi. The original stack frame and the temp storage
1344          * is also used by nested NMIs and can not be trusted on exit.
1345          */
1346         /* Do not pop rdx, nested NMIs will corrupt that part of the stack */
1347         movq (%rsp), %rdx
1348
1349         /* Set the NMI executing variable on the stack. */
1350         pushq $1
1351
1352         /*
1353          * Leave room for the "copied" frame
1354          */
1355         subq $(5*8), %rsp
1356
1357         /* Copy the stack frame to the Saved frame */
1358         .rept 5
1359         pushq 11*8(%rsp)
1360         .endr
1361
1362         /* Everything up to here is safe from nested NMIs */
1363
1364         /*
1365          * If there was a nested NMI, the first NMI's iret will return
1366          * here. But NMIs are still enabled and we can take another
1367          * nested NMI. The nested NMI checks the interrupted RIP to see
1368          * if it is between repeat_nmi and end_repeat_nmi, and if so
1369          * it will just return, as we are about to repeat an NMI anyway.
1370          * This makes it safe to copy to the stack frame that a nested
1371          * NMI will update.
1372          */
1373 repeat_nmi:
1374         /*
1375          * Update the stack variable to say we are still in NMI (the update
1376          * is benign for the non-repeat case, where 1 was pushed just above
1377          * to this very stack slot).
1378          */
1379         movq $1, 10*8(%rsp)
1380
1381         /* Make another copy, this one may be modified by nested NMIs */
1382         addq $(10*8), %rsp
1383         .rept 5
1384         pushq -6*8(%rsp)
1385         .endr
1386         subq $(5*8), %rsp
1387 end_repeat_nmi:
1388
1389         /*
1390          * Everything below this point can be preempted by a nested
1391          * NMI if the first NMI took an exception and reset our iret stack
1392          * so that we repeat another NMI.
1393          */
1394         pushq $-1               /* ORIG_RAX: no syscall to restart */
1395         ALLOC_PT_GPREGS_ON_STACK
1396
1397         /*
1398          * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1399          * as we should not be calling schedule in NMI context.
1400          * Even with normal interrupts enabled. An NMI should not be
1401          * setting NEED_RESCHED or anything that normal interrupts and
1402          * exceptions might do.
1403          */
1404         call paranoid_entry
1405
1406         /*
1407          * Save off the CR2 register. If we take a page fault in the NMI then
1408          * it could corrupt the CR2 value. If the NMI preempts a page fault
1409          * handler before it was able to read the CR2 register, and then the
1410          * NMI itself takes a page fault, the page fault that was preempted
1411          * will read the information from the NMI page fault and not the
1412          * origin fault. Save it off and restore it if it changes.
1413          * Use the r12 callee-saved register.
1414          */
1415         movq %cr2, %r12
1416
1417         /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1418         movq %rsp,%rdi
1419         movq $-1,%rsi
1420         call do_nmi
1421
1422         /* Did the NMI take a page fault? Restore cr2 if it did */
1423         movq %cr2, %rcx
1424         cmpq %rcx, %r12
1425         je 1f
1426         movq %r12, %cr2
1427 1:
1428         testl %ebx,%ebx                         /* swapgs needed? */
1429         jnz nmi_restore
1430 nmi_swapgs:
1431         SWAPGS_UNSAFE_STACK
1432 nmi_restore:
1433         RESTORE_EXTRA_REGS
1434         RESTORE_C_REGS
1435         /* Pop the extra iret frame at once */
1436         REMOVE_PT_GPREGS_FROM_STACK 6*8
1437
1438         /* Clear the NMI executing stack variable */
1439         movq $0, 5*8(%rsp)
1440         INTERRUPT_RETURN
1441 END(nmi)
1442
1443 ENTRY(ignore_sysret)
1444         mov $-ENOSYS,%eax
1445         sysret
1446 END(ignore_sysret)
1447