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