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