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