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