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