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