Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / entry_64-xen.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  *  Jun Nakajima <jun.nakajima@intel.com>
8  *  Asit Mallick <asit.k.mallick@intel.com>
9  *      Modified for Xen
10  */
11
12 /*
13  * entry.S contains the system-call and fault low-level handling routines.
14  *
15  * Some of this is documented in Documentation/x86/entry_64.txt
16  *
17  * NOTE: This code handles signal-recognition, which happens every time
18  * after an interrupt and after each system call.
19  *
20  * Normal syscalls and interrupts don't save a full stack frame, this is
21  * only done for syscall tracing, signals or fork/exec et.al.
22  *
23  * A note on terminology:
24  * - top of stack: Architecture defined interrupt frame from SS to RIP
25  * at the top of the kernel process stack.
26  * - partial stack frame: partially saved registers up to R11.
27  * - full stack frame: Like partial stack frame, but all register saved.
28  *
29  * Some macro usage:
30  * - CFI macros are used to generate dwarf2 unwind information for better
31  * backtraces. They don't change any code.
32  * - SAVE_ALL/RESTORE_ALL - Save/restore all registers
33  * - SAVE_ARGS/RESTORE_ARGS - Save/restore registers that C functions modify.
34  * There are unfortunately lots of special cases where some registers
35  * not touched. The macro is a big mess that should be cleaned up.
36  * - SAVE_REST/RESTORE_REST - Handle the registers not saved by SAVE_ARGS.
37  * Gives a full stack frame.
38  * - ENTRY/END Define functions in the symbol table.
39  * - FIXUP_TOP_OF_STACK/RESTORE_TOP_OF_STACK - Fix up the hardware stack
40  * frame that is otherwise undefined after a SYSCALL
41  * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
42  * - errorentry/paranoidentry/zeroentry - Define exception entry points.
43  */
44
45 #include <linux/linkage.h>
46 #include <asm/segment.h>
47 #include <asm/cache.h>
48 #include <asm/errno.h>
49 #include <asm/dwarf2.h>
50 #include <asm/calling.h>
51 #include <asm/asm-offsets.h>
52 #include <asm/msr.h>
53 #include <asm/unistd.h>
54 #include <asm/thread_info.h>
55 #include <asm/hw_irq.h>
56 #include <asm/page_types.h>
57 #include <asm/irqflags.h>
58 #include <asm/processor-flags.h>
59 #include <asm/ftrace.h>
60 #include <asm/percpu.h>
61 #include <linux/err.h>
62 #include <xen/interface/xen.h>
63 #include <xen/interface/features.h>
64
65 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this.  */
66 #include <linux/elf-em.h>
67 #define AUDIT_ARCH_X86_64       (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
68 #define __AUDIT_ARCH_64BIT 0x80000000
69 #define __AUDIT_ARCH_LE    0x40000000
70
71         .code64
72         .section .entry.text, "ax"
73
74 #ifdef CONFIG_FUNCTION_TRACER
75 #ifdef CONFIG_DYNAMIC_FTRACE
76 ENTRY(mcount)
77         retq
78 END(mcount)
79
80 ENTRY(ftrace_caller)
81         cmpl $0, function_trace_stop
82         jne  ftrace_stub
83
84         MCOUNT_SAVE_FRAME
85
86         movq 0x38(%rsp), %rdi
87         movq 8(%rbp), %rsi
88         subq $MCOUNT_INSN_SIZE, %rdi
89
90 GLOBAL(ftrace_call)
91         call ftrace_stub
92
93         MCOUNT_RESTORE_FRAME
94
95 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
96 GLOBAL(ftrace_graph_call)
97         jmp ftrace_stub
98 #endif
99
100 GLOBAL(ftrace_stub)
101         retq
102 END(ftrace_caller)
103
104 #else /* ! CONFIG_DYNAMIC_FTRACE */
105 ENTRY(mcount)
106         cmpl $0, function_trace_stop
107         jne  ftrace_stub
108
109         cmpq $ftrace_stub, ftrace_trace_function
110         jnz trace
111
112 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
113         cmpq $ftrace_stub, ftrace_graph_return
114         jnz ftrace_graph_caller
115
116         cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
117         jnz ftrace_graph_caller
118 #endif
119
120 GLOBAL(ftrace_stub)
121         retq
122
123 trace:
124         MCOUNT_SAVE_FRAME
125
126         movq 0x38(%rsp), %rdi
127         movq 8(%rbp), %rsi
128         subq $MCOUNT_INSN_SIZE, %rdi
129
130         call   *ftrace_trace_function
131
132         MCOUNT_RESTORE_FRAME
133
134         jmp ftrace_stub
135 END(mcount)
136 #endif /* CONFIG_DYNAMIC_FTRACE */
137 #endif /* CONFIG_FUNCTION_TRACER */
138
139 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
140 ENTRY(ftrace_graph_caller)
141         cmpl $0, function_trace_stop
142         jne ftrace_stub
143
144         MCOUNT_SAVE_FRAME
145
146         leaq 8(%rbp), %rdi
147         movq 0x38(%rsp), %rsi
148         movq (%rbp), %rdx
149         subq $MCOUNT_INSN_SIZE, %rsi
150
151         call    prepare_ftrace_return
152
153         MCOUNT_RESTORE_FRAME
154
155         retq
156 END(ftrace_graph_caller)
157
158 GLOBAL(return_to_handler)
159         subq  $24, %rsp
160
161         /* Save the return values */
162         movq %rax, (%rsp)
163         movq %rdx, 8(%rsp)
164         movq %rbp, %rdi
165
166         call ftrace_return_to_handler
167
168         movq %rax, %rdi
169         movq 8(%rsp), %rdx
170         movq (%rsp), %rax
171         addq $24, %rsp
172         jmp *%rdi
173 #endif
174
175
176 #ifndef CONFIG_PREEMPT
177 #define retint_kernel retint_restore_args
178 #endif
179
180
181 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
182 #ifdef CONFIG_TRACE_IRQFLAGS
183         bt   $9,EFLAGS-\offset(%rsp)    /* interrupts off? */
184         jnc  1f
185         TRACE_IRQS_ON
186 1:
187 #endif
188 .endm
189
190 NMI_MASK = 0x80000000
191         
192 /*
193  * C code is not supposed to know about undefined top of stack. Every time
194  * a C function with an pt_regs argument is called from the SYSCALL based
195  * fast path FIXUP_TOP_OF_STACK is needed.
196  * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
197  * manipulation.
198  */
199
200         /* %rsp:at FRAMEEND */
201         .macro FIXUP_TOP_OF_STACK tmp offset=0
202         movq $__USER_CS,CS+\offset(%rsp)
203         movq $-1,RCX+\offset(%rsp)
204         .endm
205
206         .macro RESTORE_TOP_OF_STACK tmp offset=0
207         .endm
208
209         .macro FAKE_STACK_FRAME child_rip
210         /* push in order ss, rsp, eflags, cs, rip */
211         xorl %eax, %eax
212         pushq_cfi $__KERNEL_DS /* ss */
213         /*CFI_REL_OFFSET        ss,0*/
214         pushq_cfi %rax /* rsp */
215         CFI_REL_OFFSET  rsp,0
216         pushq_cfi $(X86_EFLAGS_IF|X86_EFLAGS_BIT1) /* eflags - interrupts on */
217         /*CFI_REL_OFFSET        rflags,0*/
218         pushq_cfi $__KERNEL_CS /* cs */
219         /*CFI_REL_OFFSET        cs,0*/
220         pushq_cfi \child_rip /* rip */
221         CFI_REL_OFFSET  rip,0
222         pushq_cfi %rax /* orig rax */
223         .endm
224
225         .macro UNFAKE_STACK_FRAME
226         addq $8*6, %rsp
227         CFI_ADJUST_CFA_OFFSET   -(6*8)
228         .endm
229
230 /*
231  * initial frame state for syscall
232  */
233         .macro BASIC_FRAME start=1 offset=0
234         .if \start
235         CFI_STARTPROC simple
236         CFI_SIGNAL_FRAME
237         CFI_DEF_CFA rsp, SS+8+\offset-RIP
238         .else
239         CFI_DEF_CFA_OFFSET SS+8+\offset-RIP
240         .endif
241         /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
242         CFI_REL_OFFSET rsp, RSP+\offset-RIP
243         /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
244         /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
245         CFI_REL_OFFSET rip, RIP+\offset-RIP
246         .endm
247
248 /*
249  * initial frame state for interrupts (and exceptions without error code)
250  */
251         .macro INTR_FRAME start=1 offset=0
252         .if \start == 1
253         BASIC_FRAME 1, \offset+2*8
254         CFI_REL_OFFSET rcx, 0+\offset
255         CFI_REL_OFFSET r11, 8+\offset
256         .else
257         BASIC_FRAME \start, \offset
258         .endif
259         .endm
260
261 /*
262  * initial frame state for exceptions with error code (and interrupts
263  * with vector already pushed)
264  */
265         .macro XCPT_FRAME start=1 offset=0
266         INTR_FRAME \start, RIP+\offset-ORIG_RAX
267         .endm
268
269 /*
270  * frame that enables calling into C.
271  */
272         .macro PARTIAL_FRAME start=1 offset=0
273         .if \start >= 0
274         XCPT_FRAME 2*\start, ORIG_RAX+\offset-ARGOFFSET
275         .endif
276         CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
277         CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
278         CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
279         CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
280         CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
281         CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
282         CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
283         CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
284         CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
285         .endm
286
287 /*
288  * frame that enables passing a complete pt_regs to a C function.
289  */
290         .macro DEFAULT_FRAME start=1 offset=0
291         .if \start >= -1
292         PARTIAL_FRAME \start, R11+\offset-R15
293         .endif
294         CFI_REL_OFFSET rbx, RBX+\offset
295         CFI_REL_OFFSET rbp, RBP+\offset
296         CFI_REL_OFFSET r12, R12+\offset
297         CFI_REL_OFFSET r13, R13+\offset
298         CFI_REL_OFFSET r14, R14+\offset
299         CFI_REL_OFFSET r15, R15+\offset
300         .endm
301
302         /*
303          * Must be consistent with the definition in arch-x86/xen-x86_64.h:
304          *     struct iret_context {
305          *        u64 rax, r11, rcx, flags, rip, cs, rflags, rsp, ss;
306          *     };
307          * with rax, r11, and rcx being taken care of in the hypercall stub.
308          */
309         .macro HYPERVISOR_IRET flag
310         .if \flag == 0  # return from syscall always uses the hypercall
311         testb $3,1*8(%rsp)
312         jnz   2f
313         testl $NMI_MASK,2*8(%rsp)
314         jnz   2f
315
316         cmpb  $0,(xen_features+XENFEAT_supervisor_mode_kernel)(%rip)
317         jne   1f
318
319         /* Direct iret to kernel space. Correct CS and SS. */
320         orl   $3,1*8(%rsp)
321         orl   $3,4*8(%rsp)
322 1:      iretq
323         .endif
324
325 2:      /* Slow iret via hypervisor. */
326         andl  $~NMI_MASK, 2*8(%rsp)
327         pushq $\flag & VGCF_in_syscall
328         jmp  hypercall_page + (__HYPERVISOR_iret * 32)
329         .endm
330
331 #ifndef CONFIG_XEN
332 /* save partial stack frame */
333         .macro SAVE_ARGS_IRQ
334         cld
335         /* start from rbp in pt_regs and jump over */
336         movq_cfi rdi, RDI-RBP
337         movq_cfi rsi, RSI-RBP
338         movq_cfi rdx, RDX-RBP
339         movq_cfi rcx, RCX-RBP
340         movq_cfi rax, RAX-RBP
341         movq_cfi  r8,  R8-RBP
342         movq_cfi  r9,  R9-RBP
343         movq_cfi r10, R10-RBP
344         movq_cfi r11, R11-RBP
345
346         /* Save rbp so that we can unwind from get_irq_regs() */
347         movq_cfi rbp, 0
348
349         /* Save previous stack value */
350         movq %rsp, %rsi
351
352         leaq -RBP(%rsp),%rdi    /* arg1 for handler */
353         testl $3, CS-RBP(%rsi)
354         je 1f
355         SWAPGS
356         /*
357          * irq_count is used to check if a CPU is already on an interrupt stack
358          * or not. While this is essentially redundant with preempt_count it is
359          * a little cheaper to use a separate counter in the PDA (short of
360          * moving irq_enter into assembly, which would be too much work)
361          */
362 1:      incl PER_CPU_VAR(irq_count)
363         cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
364         CFI_DEF_CFA_REGISTER    rsi
365
366         /* Store previous stack value */
367         pushq %rsi
368         CFI_ESCAPE      0x0f /* DW_CFA_def_cfa_expression */, 6, \
369                         0x77 /* DW_OP_breg7 */, 0, \
370                         0x06 /* DW_OP_deref */, \
371                         0x08 /* DW_OP_const1u */, SS+8-RBP, \
372                         0x22 /* DW_OP_plus */
373         /* We entered an interrupt context - irqs are off: */
374         TRACE_IRQS_OFF
375         .endm
376 #endif
377
378 ENTRY(save_rest)
379         CFI_STARTPROC
380         movq 5*8+16(%rsp), %r11 /* save return address */
381         movq %rbx, RBX+16(%rsp)
382         movq %rbp, RBP+16(%rsp)
383         movq %r12, R12+16(%rsp)
384         movq %r13, R13+16(%rsp)
385         movq %r14, R14+16(%rsp)
386         movq %r15, R15+16(%rsp)
387         movq %r11, 8(%rsp)      /* return address */
388         FIXUP_TOP_OF_STACK %r11, 16
389         ret
390         CFI_ENDPROC
391 END(save_rest)
392
393 #ifndef CONFIG_XEN
394 /* save complete stack frame */
395         .pushsection .kprobes.text, "ax"
396 ENTRY(save_paranoid)
397         XCPT_FRAME offset=ORIG_RAX-R15+8
398         cld
399         movq %rdi, RDI+8(%rsp)
400         movq %rsi, RSI+8(%rsp)
401         movq_cfi rdx, RDX+8
402         movq_cfi rcx, RCX+8
403         movq_cfi rax, RAX+8
404         movq %r8, R8+8(%rsp)
405         movq %r9, R9+8(%rsp)
406         movq %r10, R10+8(%rsp)
407         movq %r11, R11+8(%rsp)
408         movq_cfi rbx, RBX+8
409         movq %rbp, RBP+8(%rsp)
410         movq %r12, R12+8(%rsp)
411         movq %r13, R13+8(%rsp)
412         movq %r14, R14+8(%rsp)
413         movq %r15, R15+8(%rsp)
414         movl $1,%ebx
415         movl $MSR_GS_BASE,%ecx
416         rdmsr
417         testl %edx,%edx
418         js 1f   /* negative -> in kernel */
419         SWAPGS
420         xorl %ebx,%ebx
421 1:      ret
422         CFI_ENDPROC
423 END(save_paranoid)
424         .popsection
425 #endif
426
427 /*
428  * A newly forked process directly context switches into this address.
429  *
430  * rdi: prev task we switched from
431  */
432 ENTRY(ret_from_fork)
433         DEFAULT_FRAME
434
435         LOCK ; btr $TIF_FORK,TI_flags(%r8)
436
437         pushq_cfi kernel_eflags(%rip)
438         popfq_cfi                               # reset kernel eflags
439
440         call schedule_tail                      # rdi: 'prev' task parameter
441
442         GET_THREAD_INFO(%rcx)
443
444         RESTORE_REST
445
446         testl $3, CS-ARGOFFSET(%rsp)            # from kernel_thread?
447         jnz  1f
448         /* Need to set the proper %ss (not NULL) for ring 3 iretq */
449         movl $__KERNEL_DS,SS-ARGOFFSET(%rsp)
450         jmp  retint_restore_args
451 1:
452         testl $_TIF_IA32, TI_flags(%rcx)        # 32-bit compat task needs IRET
453         jnz  int_ret_from_sys_call
454
455         RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
456         jmp ret_from_sys_call                   # go to the SYSRET fastpath
457
458         CFI_ENDPROC
459 END(ret_from_fork)
460
461 /*
462  * System call entry. Up to 6 arguments in registers are supported.
463  *
464  * SYSCALL does not save anything on the stack and does not change the
465  * stack pointer.
466  */
467
468 /*
469  * Register setup:
470  * rax  system call number
471  * rdi  arg0
472  * rcx  return address for syscall/sysret, C arg3
473  * rsi  arg1
474  * rdx  arg2
475  * r10  arg3    (--> moved to rcx for C)
476  * r8   arg4
477  * r9   arg5
478  * r11  eflags for syscall/sysret, temporary for C
479  * r12-r15,rbp,rbx saved by C code, not touched.
480  *
481  * Interrupts are enabled on entry.
482  * Only called from user space.
483  *
484  * XXX  if we had a free scratch register we could save the RSP into the stack frame
485  *      and report it properly in ps. Unfortunately we haven't.
486  *
487  * When user can change the frames always force IRET. That is because
488  * it deals with uncanonical addresses better. SYSRET has trouble
489  * with them due to bugs in both AMD and Intel CPUs.
490  */
491
492 ENTRY(system_call)
493         INTR_FRAME start=2 offset=2*8
494         SAVE_ARGS -8,0
495         movq  %rax,ORIG_RAX-ARGOFFSET(%rsp)
496         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
497         jnz tracesys
498 system_call_fastpath:
499 #if __SYSCALL_MASK == ~0
500         cmpq $__NR_syscall_max,%rax
501 #else
502         andl $__SYSCALL_MASK,%eax
503         cmpl $__NR_syscall_max,%eax
504 #endif
505         ja badsys
506         movq %r10,%rcx
507         call *sys_call_table(,%rax,8)  # XXX:    rip relative
508         movq %rax,RAX-ARGOFFSET(%rsp)
509 /*
510  * Syscall return path ending with SYSRET (fast path)
511  * Has incomplete stack frame and undefined top of stack.
512  */
513 ret_from_sys_call:
514         movl $_TIF_ALLWORK_MASK,%edi
515         /* edi: flagmask */
516 sysret_check:
517         LOCKDEP_SYS_EXIT
518         DISABLE_INTERRUPTS(CLBR_NONE)
519         TRACE_IRQS_OFF
520         movl TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET),%edx
521         andl %edi,%edx
522         jnz  sysret_careful
523         CFI_REMEMBER_STATE
524         /*
525          * sysretq will re-enable interrupts:
526          */
527         TRACE_IRQS_ON
528         RESTORE_ARGS 1,8,0,0
529         xor %ecx,%ecx
530         xor %r11,%r11
531         HYPERVISOR_IRET VGCF_IN_SYSCALL
532
533         CFI_RESTORE_STATE
534         /* Handle reschedules */
535         /* edx: work, edi: workmask */
536 sysret_careful:
537         bt $TIF_NEED_RESCHED,%edx
538         jnc sysret_signal
539         TRACE_IRQS_ON
540         ENABLE_INTERRUPTS(CLBR_NONE)
541         pushq_cfi %rdi
542         call schedule
543         popq_cfi %rdi
544         jmp sysret_check
545
546         /* Handle a signal */
547 sysret_signal:
548         TRACE_IRQS_ON
549         ENABLE_INTERRUPTS(CLBR_NONE)
550 #ifdef CONFIG_AUDITSYSCALL
551         bt $TIF_SYSCALL_AUDIT,%edx
552         jc sysret_audit
553 #endif
554         /*
555          * We have a signal, or exit tracing or single-step.
556          * These all wind up with the iret return path anyway,
557          * so just join that path right now.
558          */
559         FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
560         jmp int_check_syscall_exit_work
561
562 badsys:
563         movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
564         jmp ret_from_sys_call
565
566 #ifdef CONFIG_AUDITSYSCALL
567         /*
568          * Fast path for syscall audit without full syscall trace.
569          * We just call __audit_syscall_entry() directly, and then
570          * jump back to the normal fast path.
571          */
572 auditsys:
573         movq %r10,%r9                   /* 6th arg: 4th syscall arg */
574         movq %rdx,%r8                   /* 5th arg: 3rd syscall arg */
575         movq %rsi,%rcx                  /* 4th arg: 2nd syscall arg */
576         movq %rdi,%rdx                  /* 3rd arg: 1st syscall arg */
577         movq %rax,%rsi                  /* 2nd arg: syscall number */
578         movl $AUDIT_ARCH_X86_64,%edi    /* 1st arg: audit arch */
579         call __audit_syscall_entry
580         LOAD_ARGS 0             /* reload call-clobbered registers */
581         jmp system_call_fastpath
582
583         /*
584          * Return fast path for syscall audit.  Call __audit_syscall_exit()
585          * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
586          * masked off.
587          */
588 sysret_audit:
589         movq RAX-ARGOFFSET(%rsp),%rsi   /* second arg, syscall return value */
590         cmpq $-MAX_ERRNO,%rsi   /* is it < -MAX_ERRNO? */
591         setbe %al               /* 1 if so, 0 if not */
592         movzbl %al,%edi         /* zero-extend that into %edi */
593         call __audit_syscall_exit
594         movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
595         jmp sysret_check
596 #endif  /* CONFIG_AUDITSYSCALL */
597
598         /* Do syscall tracing */
599 tracesys:
600 #ifdef CONFIG_AUDITSYSCALL
601         testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
602         jz auditsys
603 #endif
604         SAVE_REST
605         movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
606         FIXUP_TOP_OF_STACK %rdi
607         movq %rsp,%rdi
608         call syscall_trace_enter
609         /*
610          * Reload arg registers from stack in case ptrace changed them.
611          * We don't reload %rax because syscall_trace_enter() returned
612          * the value it wants us to use in the table lookup.
613          */
614         LOAD_ARGS ARGOFFSET, 1
615         RESTORE_REST
616 #if __SYSCALL_MASK == ~0
617         cmpq $__NR_syscall_max,%rax
618 #else
619         andl $__SYSCALL_MASK,%eax
620         cmpl $__NR_syscall_max,%eax
621 #endif
622         ja   int_ret_from_sys_call      /* RAX(%rsp) set to -ENOSYS above */
623         movq %r10,%rcx  /* fixup for C */
624         call *sys_call_table(,%rax,8)
625         movq %rax,RAX-ARGOFFSET(%rsp)
626         /* Use IRET because user could have changed frame */
627
628 /*
629  * Syscall return path ending with IRET.
630  * Has correct top of stack, but partial stack frame.
631  */
632 GLOBAL(int_ret_from_sys_call)
633         DISABLE_INTERRUPTS(CLBR_NONE)
634         TRACE_IRQS_OFF
635         movl $_TIF_ALLWORK_MASK,%edi
636         /* edi: mask to check */
637 GLOBAL(int_with_check)
638         LOCKDEP_SYS_EXIT_IRQ
639         GET_THREAD_INFO(%rcx)
640         movl TI_flags(%rcx),%edx
641         andl %edi,%edx
642         jnz   int_careful
643         andl    $~TS_COMPAT,TI_status(%rcx)
644         jmp   retint_restore_args
645
646         /* Either reschedule or signal or syscall exit tracking needed. */
647         /* First do a reschedule test. */
648         /* edx: work, edi: workmask */
649 int_careful:
650         bt $TIF_NEED_RESCHED,%edx
651         jnc  int_very_careful
652         TRACE_IRQS_ON
653         ENABLE_INTERRUPTS(CLBR_NONE)
654         pushq_cfi %rdi
655         call schedule
656         popq_cfi %rdi
657         DISABLE_INTERRUPTS(CLBR_NONE)
658         TRACE_IRQS_OFF
659         jmp int_with_check
660
661         /* handle signals and tracing -- both require a full stack frame */
662 int_very_careful:
663         TRACE_IRQS_ON
664         ENABLE_INTERRUPTS(CLBR_NONE)
665 int_check_syscall_exit_work:
666         SAVE_REST
667         /* Check for syscall exit trace */
668         testl $_TIF_WORK_SYSCALL_EXIT,%edx
669         jz int_signal
670         pushq_cfi %rdi
671         leaq 8(%rsp),%rdi       # &ptregs -> arg1
672         call syscall_trace_leave
673         popq_cfi %rdi
674         andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
675         jmp int_restore_rest
676
677 int_signal:
678         testl $_TIF_DO_NOTIFY_MASK,%edx
679         jz 1f
680         movq %rsp,%rdi          # &ptregs -> arg1
681         xorl %esi,%esi          # oldset -> arg2
682         call do_notify_resume
683 1:      movl $_TIF_WORK_MASK,%edi
684 int_restore_rest:
685         RESTORE_REST
686         DISABLE_INTERRUPTS(CLBR_NONE)
687         TRACE_IRQS_OFF
688         jmp int_with_check
689         CFI_ENDPROC
690 END(system_call)
691
692 /*
693  * Certain special system calls that need to save a complete full stack frame.
694  */
695         .macro PTREGSCALL label,func,arg
696 ENTRY(\label)
697         PARTIAL_FRAME 1 8               /* offset 8: return address */
698         subq $REST_SKIP, %rsp
699         CFI_ADJUST_CFA_OFFSET REST_SKIP
700         call save_rest
701         DEFAULT_FRAME -2 8              /* offset 8: return address */
702         leaq 8(%rsp), \arg      /* pt_regs pointer */
703         call \func
704         jmp ptregscall_common
705         CFI_ENDPROC
706 END(\label)
707         .endm
708
709         PTREGSCALL stub_clone, sys_clone, %r8
710         PTREGSCALL stub_fork, sys_fork, %rdi
711         PTREGSCALL stub_vfork, sys_vfork, %rdi
712         PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
713         PTREGSCALL stub_iopl, sys_iopl, %rsi
714
715 ENTRY(ptregscall_common)
716         DEFAULT_FRAME 1 8       /* offset 8: return address */
717         RESTORE_TOP_OF_STACK %r11, 8
718         movq_cfi_restore R15+8, r15
719         movq_cfi_restore R14+8, r14
720         movq_cfi_restore R13+8, r13
721         movq_cfi_restore R12+8, r12
722         movq_cfi_restore RBP+8, rbp
723         movq_cfi_restore RBX+8, rbx
724         ret $REST_SKIP          /* pop extended registers */
725         CFI_ENDPROC
726 END(ptregscall_common)
727
728 ENTRY(stub_execve)
729         CFI_STARTPROC
730         addq $8, %rsp
731         PARTIAL_FRAME 0
732         SAVE_REST
733         FIXUP_TOP_OF_STACK %r11
734         movq %rsp, %rcx
735         call sys_execve
736         RESTORE_TOP_OF_STACK %r11
737         movq %rax,RAX(%rsp)
738         RESTORE_REST
739         jmp int_ret_from_sys_call
740         CFI_ENDPROC
741 END(stub_execve)
742
743 /*
744  * sigreturn is special because it needs to restore all registers on return.
745  * This cannot be done with SYSRET, so use the IRET return path instead.
746  */
747 ENTRY(stub_rt_sigreturn)
748         CFI_STARTPROC
749         addq $8, %rsp
750         PARTIAL_FRAME 0
751         SAVE_REST
752         movq %rsp,%rdi
753         FIXUP_TOP_OF_STACK %r11
754         call sys_rt_sigreturn
755         movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
756         RESTORE_REST
757         jmp int_ret_from_sys_call
758         CFI_ENDPROC
759 END(stub_rt_sigreturn)
760
761 #ifdef CONFIG_X86_X32_ABI
762         PTREGSCALL stub_x32_sigaltstack, sys32_sigaltstack, %rdx
763
764 ENTRY(stub_x32_rt_sigreturn)
765         CFI_STARTPROC
766         addq $8, %rsp
767         PARTIAL_FRAME 0
768         SAVE_REST
769         movq %rsp,%rdi
770         FIXUP_TOP_OF_STACK %r11
771         call sys32_x32_rt_sigreturn
772         movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
773         RESTORE_REST
774         jmp int_ret_from_sys_call
775         CFI_ENDPROC
776 END(stub_x32_rt_sigreturn)
777
778 ENTRY(stub_x32_execve)
779         CFI_STARTPROC
780         addq $8, %rsp
781         PARTIAL_FRAME 0
782         SAVE_REST
783         FIXUP_TOP_OF_STACK %r11
784         movq %rsp, %rcx
785         call sys32_execve
786         RESTORE_TOP_OF_STACK %r11
787         movq %rax,RAX(%rsp)
788         RESTORE_REST
789         jmp int_ret_from_sys_call
790         CFI_ENDPROC
791 END(stub_x32_execve)
792
793 #endif
794
795 /*
796  * Interrupt exit.
797  */ 
798
799 retint_with_reschedule:
800         PARTIAL_FRAME
801         movl $_TIF_WORK_MASK,%edi
802 retint_check:
803         LOCKDEP_SYS_EXIT_IRQ
804         movl TI_flags(%rcx),%edx
805         andl %edi,%edx
806         CFI_REMEMBER_STATE
807         jnz  retint_careful
808 retint_restore_args:    /* return to kernel space */
809         movl EFLAGS-REST_SKIP(%rsp), %eax
810         shr $9, %eax                    # EAX[0] == IRET_EFLAGS.IF
811         GET_VCPU_INFO
812         andb evtchn_upcall_mask(%rsi),%al
813         andb $1,%al                     # EAX[0] == IRET_EFLAGS.IF & event_mask
814         jnz restore_all_enable_events   #        != 0 => enable event delivery
815                 
816         RESTORE_ARGS 1,8,1
817         HYPERVISOR_IRET 0
818         
819         /* edi: workmask, edx: work */
820 retint_careful:
821         CFI_RESTORE_STATE
822         bt    $TIF_NEED_RESCHED,%edx
823         jnc   retint_signal
824         TRACE_IRQS_ON
825         ENABLE_INTERRUPTS(CLBR_NONE)
826         pushq_cfi %rdi
827         call  schedule
828         popq_cfi %rdi
829         GET_THREAD_INFO(%rcx)
830         DISABLE_INTERRUPTS(CLBR_NONE)
831         TRACE_IRQS_OFF
832         jmp retint_check
833
834 retint_signal:
835         testl $_TIF_DO_NOTIFY_MASK,%edx
836         jz    retint_restore_args
837         TRACE_IRQS_ON
838         ENABLE_INTERRUPTS(CLBR_NONE)
839         SAVE_REST
840         movq $-1,ORIG_RAX(%rsp)
841         xorl %esi,%esi          # oldset
842         movq %rsp,%rdi          # &pt_regs
843         call do_notify_resume
844         RESTORE_REST
845         DISABLE_INTERRUPTS(CLBR_NONE)
846         TRACE_IRQS_OFF
847         GET_THREAD_INFO(%rcx)
848         jmp retint_with_reschedule
849
850 #ifdef CONFIG_PREEMPT
851         /* Returning to kernel space. Check if we need preemption */
852         /* rcx:  threadinfo. interrupts off. */
853 ENTRY(retint_kernel)
854         cmpl $0,TI_preempt_count(%rcx)
855         jnz  retint_restore_args
856         bt  $TIF_NEED_RESCHED,TI_flags(%rcx)
857         jnc  retint_restore_args
858         bt   $9,EFLAGS-ARGOFFSET(%rsp)  /* interrupts off? */
859         jnc  retint_restore_args
860         call preempt_schedule_irq
861         jmp retint_kernel       /* check again */
862 #endif
863
864         CFI_ENDPROC
865 END(retint_check)
866
867 #ifndef CONFIG_XEN
868 /*
869  * APIC interrupts.
870  */
871 .macro apicinterrupt num sym do_sym
872 ENTRY(\sym)
873         INTR_FRAME
874         pushq_cfi $~(\num)
875         interrupt \do_sym
876         jmp error_entry
877         CFI_ENDPROC
878 END(\sym)
879 .endm
880
881 #ifdef CONFIG_SMP
882 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
883         irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
884 apicinterrupt REBOOT_VECTOR \
885         reboot_interrupt smp_reboot_interrupt
886 #endif
887
888 #ifdef CONFIG_X86_UV
889 apicinterrupt UV_BAU_MESSAGE \
890         uv_bau_message_intr1 uv_bau_message_interrupt
891 #endif
892 apicinterrupt LOCAL_TIMER_VECTOR \
893         apic_timer_interrupt smp_apic_timer_interrupt
894 apicinterrupt X86_PLATFORM_IPI_VECTOR \
895         x86_platform_ipi smp_x86_platform_ipi
896
897 #ifdef CONFIG_SMP
898         ALIGN
899         INTR_FRAME
900 .irp idx,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, \
901         16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
902 .if NUM_INVALIDATE_TLB_VECTORS > \idx
903 ENTRY(invalidate_interrupt\idx)
904         pushq_cfi $~(INVALIDATE_TLB_VECTOR_START+\idx)
905         jmp .Lcommon_invalidate_interrupt0
906         CFI_ADJUST_CFA_OFFSET -8
907 END(invalidate_interrupt\idx)
908 .endif
909 .endr
910         CFI_ENDPROC
911 apicinterrupt INVALIDATE_TLB_VECTOR_START, \
912         invalidate_interrupt0, smp_invalidate_interrupt
913 #endif
914
915 apicinterrupt THRESHOLD_APIC_VECTOR \
916         threshold_interrupt smp_threshold_interrupt
917 apicinterrupt THERMAL_APIC_VECTOR \
918         thermal_interrupt smp_thermal_interrupt
919
920 #ifdef CONFIG_SMP
921 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
922         call_function_single_interrupt smp_call_function_single_interrupt
923 apicinterrupt CALL_FUNCTION_VECTOR \
924         call_function_interrupt smp_call_function_interrupt
925 apicinterrupt RESCHEDULE_VECTOR \
926         reschedule_interrupt smp_reschedule_interrupt
927 #endif
928
929 apicinterrupt ERROR_APIC_VECTOR \
930         error_interrupt smp_error_interrupt
931 apicinterrupt SPURIOUS_APIC_VECTOR \
932         spurious_interrupt smp_spurious_interrupt
933
934 #ifdef CONFIG_IRQ_WORK
935 apicinterrupt IRQ_WORK_VECTOR \
936         irq_work_interrupt smp_irq_work_interrupt
937 #endif
938 #endif /* !CONFIG_XEN */
939
940 /*
941  * Exception entry points.
942  */
943 .macro zeroentry sym do_sym
944 ENTRY(\sym)
945         INTR_FRAME
946         movq (%rsp),%rcx
947         CFI_RESTORE rcx
948         movq 8(%rsp),%r11
949         CFI_RESTORE r11
950         movq $-1,8(%rsp)        /* ORIG_RAX: no syscall to restart */
951         subq $ORIG_RAX-R15-1*8,%rsp
952         CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15-1*8
953         call error_entry
954         DEFAULT_FRAME -1
955         movq %rsp,%rdi          /* pt_regs pointer */
956         xorl %esi,%esi          /* no error code */
957         call \do_sym
958         jmp error_exit          /* %ebx: no swapgs flag */
959         CFI_ENDPROC
960 END(\sym)
961 .endm
962
963 .macro paranoidzeroentry sym do_sym
964         zeroentry \sym \do_sym
965 .endm
966
967 .macro paranoidzeroentry_ist sym do_sym ist
968         zeroentry \sym \do_sym
969 .endm
970
971 .macro errorentry sym do_sym
972 ENTRY(\sym)
973         XCPT_FRAME
974         movq (%rsp),%rcx
975         CFI_RESTORE rcx
976         movq 8(%rsp),%r11
977         CFI_RESTORE r11
978         subq $ORIG_RAX-R15-2*8,%rsp
979         CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15-2*8
980         call error_entry
981         DEFAULT_FRAME -1
982         movq %rsp,%rdi                  /* pt_regs pointer */
983         movq ORIG_RAX(%rsp),%rsi        /* get error code */
984         movq $-1,ORIG_RAX(%rsp)         /* no syscall to restart */
985         call \do_sym
986         jmp error_exit                  /* %ebx: no swapgs flag */
987         CFI_ENDPROC
988 END(\sym)
989 .endm
990
991         /* error code is on the stack already */
992 .macro paranoiderrorentry sym do_sym
993         errorentry \sym \do_sym
994 .endm
995
996 /*
997  * Copied from arch/xen/i386/kernel/entry.S
998  */               
999 # A note on the "critical region" in our callback handler.
1000 # We want to avoid stacking callback handlers due to events occurring
1001 # during handling of the last event. To do this, we keep events disabled
1002 # until we've done all processing. HOWEVER, we must enable events before
1003 # popping the stack frame (can't be done atomically) and so it would still
1004 # be possible to get enough handler activations to overflow the stack.
1005 # Although unlikely, bugs of that kind are hard to track down, so we'd
1006 # like to avoid the possibility.
1007 # So, on entry to the handler we detect whether we interrupted an
1008 # existing activation in its critical region -- if so, we pop the current
1009 # activation and restart the handler using the previous one.
1010 ENTRY(do_hypervisor_callback)   # do_hypervisor_callback(struct *pt_regs)
1011         CFI_STARTPROC
1012 # Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1013 # see the correct pointer to the pt_regs
1014         movq %rdi, %rsp            # we don't return, adjust the stack frame
1015         CFI_ENDPROC
1016         DEFAULT_FRAME
1017 11:     incl PER_CPU_VAR(irq_count)
1018         movq %rsp,%rbp
1019         CFI_DEF_CFA_REGISTER rbp
1020         cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1021         pushq %rbp                      # backlink for old unwinder
1022         call evtchn_do_upcall
1023         popq %rsp
1024         CFI_DEF_CFA_REGISTER rsp
1025         decl PER_CPU_VAR(irq_count)
1026         jmp  error_exit
1027         CFI_ENDPROC
1028 END(do_hypervisor_callback)
1029
1030         ALIGN
1031 restore_all_enable_events:  
1032         PARTIAL_FRAME
1033         TRACE_IRQS_ON
1034         __ENABLE_INTERRUPTS
1035
1036 scrit:  /**** START OF CRITICAL REGION ****/
1037         __TEST_PENDING
1038         CFI_REMEMBER_STATE
1039         jnz  14f                        # process more events if necessary...
1040         RESTORE_ARGS 1,8,1
1041         HYPERVISOR_IRET 0
1042         
1043         CFI_RESTORE_STATE
1044 14:     __DISABLE_INTERRUPTS
1045         SAVE_REST
1046         movq %rsp,%rdi                  # set the argument again
1047         jmp  11b
1048         CFI_ENDPROC
1049 ecrit:  /**** END OF CRITICAL REGION ****/
1050 # At this point, unlike on x86-32, we don't do the fixup to simplify the 
1051 # code and the stack frame is more complex on x86-64.
1052 # When the kernel is interrupted in the critical section, the kernel 
1053 # will do IRET in that case, and everything will be restored at that point, 
1054 # i.e. it just resumes from the next instruction interrupted with the same context. 
1055
1056 # Hypervisor uses this for application faults while it executes.
1057 # We get here for two reasons:
1058 #  1. Fault while reloading DS, ES, FS or GS
1059 #  2. Fault while executing IRET
1060 # Category 1 we do not need to fix up as Xen has already reloaded all segment
1061 # registers that could be reloaded and zeroed the others.
1062 # Category 2 we fix up by killing the current process. We cannot use the
1063 # normal Linux return path in this case because if we use the IRET hypercall
1064 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1065 # We distinguish between categories by comparing each saved segment register
1066 # with its current contents: any discrepancy means we in category 1.
1067 ENTRY(failsafe_callback)
1068         INTR_FRAME offset=4*8
1069         movw %ds,%cx
1070         cmpw %cx,0x10(%rsp)
1071         CFI_REMEMBER_STATE
1072         jne 1f
1073         movw %es,%cx
1074         cmpw %cx,0x18(%rsp)
1075         jne 1f
1076         movw %fs,%cx
1077         cmpw %cx,0x20(%rsp)
1078         jne 1f
1079         movw %gs,%cx
1080         cmpw %cx,0x28(%rsp)
1081         jne 1f
1082         /* All segments match their saved values => Category 2 (Bad IRET). */
1083         movq (%rsp),%rcx
1084         CFI_RESTORE rcx
1085         movq 8(%rsp),%r11
1086         CFI_RESTORE r11
1087         addq $0x30,%rsp
1088         CFI_ADJUST_CFA_OFFSET -0x30
1089         movq $11,%rdi   /* SIGSEGV */
1090         jmp do_exit                     
1091         CFI_RESTORE_STATE
1092 1:      /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1093         movq (%rsp),%rcx
1094         CFI_RESTORE rcx
1095         movq 8(%rsp),%r11
1096         CFI_RESTORE r11
1097         addq $0x30,%rsp
1098         CFI_ADJUST_CFA_OFFSET -0x30
1099         pushq_cfi $0
1100         SAVE_ALL
1101         jmp error_exit
1102         CFI_ENDPROC
1103
1104 zeroentry divide_error do_divide_error
1105 zeroentry overflow do_overflow
1106 zeroentry bounds do_bounds
1107 zeroentry invalid_op do_invalid_op
1108 zeroentry device_not_available do_device_not_available
1109 zeroentry hypervisor_callback do_hypervisor_callback
1110 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1111 errorentry invalid_TSS do_invalid_TSS
1112 errorentry segment_not_present do_segment_not_present
1113 zeroentry coprocessor_error do_coprocessor_error
1114 errorentry alignment_check do_alignment_check
1115 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1116         
1117 ENTRY(kernel_thread_helper)
1118         pushq $0                # fake return address
1119         CFI_STARTPROC
1120         /*
1121          * Here we are in the child and the registers are set as they were
1122          * at kernel_thread() invocation in the parent.
1123          */
1124         call *%rsi
1125         # exit
1126         mov %eax, %edi
1127         call do_exit
1128         ud2                     # padding for call trace
1129         CFI_ENDPROC
1130 END(kernel_thread_helper)
1131
1132 /*
1133  * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1134  *
1135  * C extern interface:
1136  *       extern long execve(const char *name, char **argv, char **envp)
1137  *
1138  * asm input arguments:
1139  *      rdi: name, rsi: argv, rdx: envp
1140  *
1141  * We want to fallback into:
1142  *      extern long sys_execve(const char *name, char **argv,char **envp, struct pt_regs *regs)
1143  *
1144  * do_sys_execve asm fallback arguments:
1145  *      rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1146  */
1147 ENTRY(kernel_execve)
1148         CFI_STARTPROC
1149         FAKE_STACK_FRAME $0
1150         SAVE_ALL
1151         movq %rsp,%rcx
1152         call sys_execve
1153         movq %rax, RAX(%rsp)
1154         RESTORE_REST
1155         testq %rax,%rax
1156         jne 1f
1157         jmp int_ret_from_sys_call
1158 1:      RESTORE_ARGS
1159         UNFAKE_STACK_FRAME
1160         ret
1161         CFI_ENDPROC
1162 END(kernel_execve)
1163
1164 /* Call softirq on interrupt stack. Interrupts are off. */
1165 ENTRY(call_softirq)
1166         CFI_STARTPROC
1167         pushq_cfi %rbp
1168         CFI_REL_OFFSET rbp,0
1169         mov  %rsp,%rbp
1170         CFI_DEF_CFA_REGISTER rbp
1171         incl PER_CPU_VAR(irq_count)
1172         cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1173         push  %rbp                      # backlink for old unwinder
1174         call __do_softirq
1175         leaveq
1176         CFI_RESTORE             rbp
1177         CFI_DEF_CFA_REGISTER    rsp
1178         CFI_ADJUST_CFA_OFFSET   -8
1179         decl PER_CPU_VAR(irq_count)
1180         ret
1181         CFI_ENDPROC
1182 END(call_softirq)
1183
1184 #ifdef CONFIG_STACK_UNWIND
1185 ENTRY(arch_unwind_init_running)
1186         CFI_STARTPROC
1187         movq    %r15, R15(%rdi)
1188         movq    %r14, R14(%rdi)
1189         xchgq   %rsi, %rdx
1190         movq    %r13, R13(%rdi)
1191         movq    %r12, R12(%rdi)
1192         xorl    %eax, %eax
1193         movq    %rbp, RBP(%rdi)
1194         movq    %rbx, RBX(%rdi)
1195         movq    (%rsp), %r9
1196         xchgq   %rdx, %rcx
1197         movq    %rax, R11(%rdi)
1198         movq    %rax, R10(%rdi)
1199         movq    %rax, R9(%rdi)
1200         movq    %rax, R8(%rdi)
1201         movq    %rax, RAX(%rdi)
1202         movq    %rax, RCX(%rdi)
1203         movq    %rax, RDX(%rdi)
1204         movq    %rax, RSI(%rdi)
1205         movq    %rax, RDI(%rdi)
1206         movq    %rax, ORIG_RAX(%rdi)
1207         movq    %r9, RIP(%rdi)
1208         leaq    8(%rsp), %r9
1209         movq    $__KERNEL_CS, CS(%rdi)
1210         movq    %rax, EFLAGS(%rdi)
1211         movq    %r9, RSP(%rdi)
1212         movq    $__KERNEL_DS, SS(%rdi)
1213         jmpq    *%rcx
1214         CFI_ENDPROC
1215 END(arch_unwind_init_running)
1216 #endif
1217
1218 /*
1219  * Some functions should be protected against kprobes
1220  */
1221         .pushsection .kprobes.text, "ax"
1222
1223 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1224 zeroentry nmi do_nmi_callback
1225 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1226 paranoiderrorentry stack_segment do_stack_segment
1227 errorentry general_protection do_general_protection
1228 errorentry page_fault do_page_fault
1229 #ifdef CONFIG_KVM_GUEST
1230 errorentry async_page_fault do_async_page_fault
1231 #endif
1232 #ifdef CONFIG_X86_MCE
1233 paranoidzeroentry machine_check *machine_check_vector(%rip)
1234 #endif
1235
1236 #ifndef CONFIG_XEN
1237         /*
1238          * "Paranoid" exit path from exception stack.
1239          * Paranoid because this is used by NMIs and cannot take
1240          * any kernel state for granted.
1241          * We don't do kernel preemption checks here, because only
1242          * NMI should be common and it does not enable IRQs and
1243          * cannot get reschedule ticks.
1244          *
1245          * "trace" is 0 for the NMI handler only, because irq-tracing
1246          * is fundamentally NMI-unsafe. (we cannot change the soft and
1247          * hard flags at once, atomically)
1248          */
1249
1250         /* ebx: no swapgs flag */
1251 ENTRY(paranoid_exit)
1252         DEFAULT_FRAME
1253         DISABLE_INTERRUPTS(CLBR_NONE)
1254         TRACE_IRQS_OFF
1255         testl %ebx,%ebx                         /* swapgs needed? */
1256         jnz paranoid_restore
1257         testl $3,CS(%rsp)
1258         jnz   paranoid_userspace
1259 paranoid_swapgs:
1260         TRACE_IRQS_IRETQ 0
1261         SWAPGS_UNSAFE_STACK
1262         RESTORE_ALL 8
1263         jmp irq_return
1264 paranoid_restore:
1265         TRACE_IRQS_IRETQ 0
1266         RESTORE_ALL 8
1267         jmp irq_return
1268 paranoid_userspace:
1269         GET_THREAD_INFO(%rcx)
1270         movl TI_flags(%rcx),%ebx
1271         andl $_TIF_WORK_MASK,%ebx
1272         jz paranoid_swapgs
1273         movq %rsp,%rdi                  /* &pt_regs */
1274         call sync_regs
1275         movq %rax,%rsp                  /* switch stack for scheduling */
1276         testl $_TIF_NEED_RESCHED,%ebx
1277         jnz paranoid_schedule
1278         movl %ebx,%edx                  /* arg3: thread flags */
1279         TRACE_IRQS_ON
1280         ENABLE_INTERRUPTS(CLBR_NONE)
1281         xorl %esi,%esi                  /* arg2: oldset */
1282         movq %rsp,%rdi                  /* arg1: &pt_regs */
1283         call do_notify_resume
1284         DISABLE_INTERRUPTS(CLBR_NONE)
1285         TRACE_IRQS_OFF
1286         jmp paranoid_userspace
1287 paranoid_schedule:
1288         TRACE_IRQS_ON
1289         ENABLE_INTERRUPTS(CLBR_ANY)
1290         call schedule
1291         DISABLE_INTERRUPTS(CLBR_ANY)
1292         TRACE_IRQS_OFF
1293         jmp paranoid_userspace
1294         CFI_ENDPROC
1295 END(paranoid_exit)
1296 #endif
1297
1298 /*
1299  * Exception entry point. This expects an error code/orig_rax on the stack.
1300  * returns in "no swapgs flag" in %ebx.
1301  */
1302 ENTRY(error_entry)
1303         XCPT_FRAME start=2 offset=ORIG_RAX-R15+8
1304         /* oldrax contains error code */
1305         cld
1306         movq %rdi, RDI+8(%rsp)
1307         movq %rsi, RSI+8(%rsp)
1308         movq %rdx, RDX+8(%rsp)
1309         movq %rcx, RCX+8(%rsp)
1310         movq %rax, RAX+8(%rsp)
1311         movq  %r8,  R8+8(%rsp)
1312         movq  %r9,  R9+8(%rsp)
1313         movq %r10, R10+8(%rsp)
1314         movq %r11, R11+8(%rsp)
1315         movq_cfi rbx, RBX+8
1316         movq %rbp, RBP+8(%rsp)
1317         movq %r12, R12+8(%rsp)
1318         movq %r13, R13+8(%rsp)
1319         movq %r14, R14+8(%rsp)
1320         movq %r15, R15+8(%rsp)
1321 #ifndef CONFIG_XEN
1322         xorl %ebx,%ebx
1323         testl $3,CS+8(%rsp)
1324         je error_kernelspace
1325 error_swapgs:
1326         SWAPGS
1327 error_sti:
1328 #endif
1329         TRACE_IRQS_OFF
1330         ret
1331
1332 #ifndef CONFIG_XEN
1333 /*
1334  * There are two places in the kernel that can potentially fault with
1335  * usergs. Handle them here. The exception handlers after iret run with
1336  * kernel gs again, so don't set the user space flag. B stepping K8s
1337  * sometimes report an truncated RIP for IRET exceptions returning to
1338  * compat mode. Check for these here too.
1339  */
1340 error_kernelspace:
1341         CFI_REL_OFFSET rcx, RCX+8
1342         incl %ebx
1343         leaq irq_return(%rip),%rcx
1344         cmpq %rcx,RIP+8(%rsp)
1345         je error_swapgs
1346         movl %ecx,%eax  /* zero extend */
1347         cmpq %rax,RIP+8(%rsp)
1348         je bstep_iret
1349         cmpq $gs_change,RIP+8(%rsp)
1350         je error_swapgs
1351         jmp error_sti
1352
1353 bstep_iret:
1354         /* Fix truncated RIP */
1355         movq %rcx,RIP+8(%rsp)
1356         jmp error_swapgs
1357 #endif
1358         CFI_ENDPROC
1359 END(error_entry)
1360
1361
1362 ENTRY(error_exit)
1363         DEFAULT_FRAME
1364         RESTORE_REST
1365         DISABLE_INTERRUPTS(CLBR_NONE)
1366         TRACE_IRQS_OFF
1367         GET_THREAD_INFO(%rcx)
1368         testb $3,CS-ARGOFFSET(%rsp)
1369         jz retint_kernel
1370         LOCKDEP_SYS_EXIT_IRQ
1371         movl TI_flags(%rcx),%edx
1372         movl $_TIF_WORK_MASK,%edi
1373         andl %edi,%edx
1374         jnz retint_careful
1375         jmp retint_restore_args
1376         CFI_ENDPROC
1377 END(error_exit)
1378
1379
1380 #define extern #
1381 #include <asm-generic/percpu.h>
1382
1383 .pushsection PER_CPU_BASE_SECTION, "aw", @progbits
1384 in_NMI: .byte   0
1385 .popsection
1386
1387 do_nmi_callback:
1388         CFI_STARTPROC
1389         addq $8, %rsp
1390         CFI_ENDPROC
1391         DEFAULT_FRAME
1392         orb  $1, PER_CPU_VAR(in_NMI)
1393         js   1f
1394 0:
1395         movb $0x80, PER_CPU_VAR(in_NMI)
1396         call do_nmi
1397         movl $0x80, %eax
1398         cmpxchgb %ah, PER_CPU_VAR(in_NMI)
1399         jne  0b
1400         orl  $NMI_MASK,EFLAGS(%rsp)
1401 1:
1402         RESTORE_REST
1403         DISABLE_INTERRUPTS(CLBR_NONE)
1404         TRACE_IRQS_OFF
1405         GET_THREAD_INFO(%rcx)
1406         jmp  retint_restore_args
1407         CFI_ENDPROC
1408 END(do_nmi_callback)
1409
1410
1411 #ifndef CONFIG_IA32_EMULATION
1412 ENTRY(ignore_sysret)
1413         INTR_FRAME
1414         popq_cfi %rcx
1415         CFI_RESTORE rcx
1416         popq_cfi %r11
1417         CFI_RESTORE r11
1418         mov $-ENOSYS,%eax
1419         # any non-zero value not having VGCF_in_syscall set will do:
1420         HYPERVISOR_IRET VGCF_i387_valid
1421         CFI_ENDPROC
1422 END(ignore_sysret)
1423 #endif
1424
1425 /*
1426  * End of kprobes section
1427  */
1428         .popsection