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