- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / entry_32-xen.S
1 /*
2  *
3  *  Copyright (C) 1991, 1992  Linus Torvalds
4  */
5
6 /*
7  * entry.S contains the system-call and fault low-level handling routines.
8  * This also contains the timer-interrupt handler, as well as all interrupts
9  * and faults that can result in a task-switch.
10  *
11  * NOTE: This code handles signal-recognition, which happens every time
12  * after a timer-interrupt and after each system call.
13  *
14  * I changed all the .align's to 4 (16 byte alignment), as that's faster
15  * on a 486.
16  *
17  * Stack layout in 'syscall_exit':
18  *      ptrace needs to have all regs on the stack.
19  *      if the order here is changed, it needs to be
20  *      updated in fork.c:copy_process, signal.c:do_signal,
21  *      ptrace.c and ptrace.h
22  *
23  *       0(%esp) - %ebx
24  *       4(%esp) - %ecx
25  *       8(%esp) - %edx
26  *       C(%esp) - %esi
27  *      10(%esp) - %edi
28  *      14(%esp) - %ebp
29  *      18(%esp) - %eax
30  *      1C(%esp) - %ds
31  *      20(%esp) - %es
32  *      24(%esp) - %fs
33  *      28(%esp) - %gs          saved iff !CONFIG_X86_32_LAZY_GS
34  *      2C(%esp) - orig_eax
35  *      30(%esp) - %eip
36  *      34(%esp) - %cs
37  *      38(%esp) - %eflags
38  *      3C(%esp) - %oldesp
39  *      40(%esp) - %oldss
40  *
41  * "current" is in register %ebx during any slow entries.
42  */
43
44 #include <linux/linkage.h>
45 #include <linux/err.h>
46 #include <asm/thread_info.h>
47 #include <asm/irqflags.h>
48 #include <asm/errno.h>
49 #include <asm/segment.h>
50 #include <asm/smp.h>
51 #include <asm/page_types.h>
52 #include <asm/percpu.h>
53 #include <asm/dwarf2.h>
54 #include <asm/processor-flags.h>
55 #include <asm/ftrace.h>
56 #include <asm/irq_vectors.h>
57 #include <asm/cpufeature.h>
58 #include <asm/alternative-asm.h>
59 #include <xen/interface/xen.h>
60
61 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this.  */
62 #include <linux/elf-em.h>
63 #define AUDIT_ARCH_I386         (EM_386|__AUDIT_ARCH_LE)
64 #define __AUDIT_ARCH_LE    0x40000000
65
66 #ifndef CONFIG_AUDITSYSCALL
67 #define sysenter_audit  syscall_trace_entry
68 #define sysexit_audit   syscall_exit_work
69 #endif
70
71         .section .entry.text, "ax"
72
73 /*
74  * We use macros for low-level operations which need to be overridden
75  * for paravirtualization.  The following will never clobber any registers:
76  *   INTERRUPT_RETURN (aka. "iret")
77  *   GET_CR0_INTO_EAX (aka. "movl %cr0, %eax")
78  *   ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
79  *
80  * For DISABLE_INTERRUPTS/ENABLE_INTERRUPTS (aka "cli"/"sti"), you must
81  * specify what registers can be overwritten (CLBR_NONE, CLBR_EAX/EDX/ECX/ANY).
82  * Allowing a register to be clobbered can shrink the paravirt replacement
83  * enough to patch inline, increasing performance.
84  */
85
86 /* Pseudo-eflags. */
87 NMI_MASK        = 0x80000000
88
89 #ifdef CONFIG_PREEMPT
90 #define preempt_stop(clobbers)  DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
91 #else
92 #define preempt_stop(clobbers)
93 #define resume_kernel           restore_all
94 #endif
95
96 .macro TRACE_IRQS_IRET
97 #ifdef CONFIG_TRACE_IRQFLAGS
98         testl $X86_EFLAGS_IF,PT_EFLAGS(%esp)     # interrupts off?
99         jz 1f
100         TRACE_IRQS_ON
101 1:
102 #endif
103 .endm
104
105 #ifdef CONFIG_VM86
106 #define resume_userspace_sig    check_userspace
107 #else
108 #define resume_userspace_sig    resume_userspace
109 #endif
110
111 /*
112  * User gs save/restore
113  *
114  * %gs is used for userland TLS and kernel only uses it for stack
115  * canary which is required to be at %gs:20 by gcc.  Read the comment
116  * at the top of stackprotector.h for more info.
117  *
118  * Local labels 98 and 99 are used.
119  */
120 #ifdef CONFIG_X86_32_LAZY_GS
121
122  /* unfortunately push/pop can't be no-op */
123 .macro PUSH_GS
124         pushl_cfi $0
125 .endm
126 .macro POP_GS pop=0
127         addl $(4 + \pop), %esp
128         CFI_ADJUST_CFA_OFFSET -(4 + \pop)
129 .endm
130 .macro POP_GS_EX
131 .endm
132
133  /* all the rest are no-op */
134 .macro PTGS_TO_GS
135 .endm
136 .macro PTGS_TO_GS_EX
137 .endm
138 .macro GS_TO_REG reg
139 .endm
140 .macro REG_TO_PTGS reg
141 .endm
142 .macro SET_KERNEL_GS reg
143 .endm
144
145 #else   /* CONFIG_X86_32_LAZY_GS */
146
147 .macro PUSH_GS
148         pushl_cfi %gs
149         /*CFI_REL_OFFSET gs, 0*/
150 .endm
151
152 .macro POP_GS pop=0
153 98:     popl_cfi %gs
154         /*CFI_RESTORE gs*/
155   .if \pop <> 0
156         add $\pop, %esp
157         CFI_ADJUST_CFA_OFFSET -\pop
158   .endif
159 .endm
160 .macro POP_GS_EX
161 .pushsection .fixup, "ax"
162 99:     movl $0, (%esp)
163         jmp 98b
164 .section __ex_table, "a"
165         .align 4
166         .long 98b, 99b
167 .popsection
168 .endm
169
170 .macro PTGS_TO_GS
171 98:     mov PT_GS(%esp), %gs
172 .endm
173 .macro PTGS_TO_GS_EX
174 .pushsection .fixup, "ax"
175 99:     movl $0, PT_GS(%esp)
176         jmp 98b
177 .section __ex_table, "a"
178         .align 4
179         .long 98b, 99b
180 .popsection
181 .endm
182
183 .macro GS_TO_REG reg
184         movl %gs, \reg
185         /*CFI_REGISTER gs, \reg*/
186 .endm
187 .macro REG_TO_PTGS reg
188         movl \reg, PT_GS(%esp)
189         /*CFI_REL_OFFSET gs, PT_GS*/
190 .endm
191 .macro SET_KERNEL_GS reg
192         movl $(__KERNEL_STACK_CANARY), \reg
193         movl \reg, %gs
194 .endm
195
196 #endif  /* CONFIG_X86_32_LAZY_GS */
197
198 .macro SAVE_ALL
199         cld
200         PUSH_GS
201         pushl_cfi %fs
202         /*CFI_REL_OFFSET fs, 0;*/
203         pushl_cfi %es
204         /*CFI_REL_OFFSET es, 0;*/
205         pushl_cfi %ds
206         /*CFI_REL_OFFSET ds, 0;*/
207         pushl_cfi %eax
208         CFI_REL_OFFSET eax, 0
209         pushl_cfi %ebp
210         CFI_REL_OFFSET ebp, 0
211         pushl_cfi %edi
212         CFI_REL_OFFSET edi, 0
213         pushl_cfi %esi
214         CFI_REL_OFFSET esi, 0
215         pushl_cfi %edx
216         CFI_REL_OFFSET edx, 0
217         pushl_cfi %ecx
218         CFI_REL_OFFSET ecx, 0
219         pushl_cfi %ebx
220         CFI_REL_OFFSET ebx, 0
221         movl $(__USER_DS), %edx
222         movl %edx, %ds
223         movl %edx, %es
224         movl $(__KERNEL_PERCPU), %edx
225         movl %edx, %fs
226         SET_KERNEL_GS %edx
227 .endm
228
229 .macro RESTORE_INT_REGS
230         popl_cfi %ebx
231         CFI_RESTORE ebx
232         popl_cfi %ecx
233         CFI_RESTORE ecx
234         popl_cfi %edx
235         CFI_RESTORE edx
236         popl_cfi %esi
237         CFI_RESTORE esi
238         popl_cfi %edi
239         CFI_RESTORE edi
240         popl_cfi %ebp
241         CFI_RESTORE ebp
242         popl_cfi %eax
243         CFI_RESTORE eax
244 .endm
245
246 .macro RESTORE_REGS pop=0
247         RESTORE_INT_REGS
248 1:      popl_cfi %ds
249         /*CFI_RESTORE ds;*/
250 2:      popl_cfi %es
251         /*CFI_RESTORE es;*/
252 3:      popl_cfi %fs
253         /*CFI_RESTORE fs;*/
254         POP_GS \pop
255 .pushsection .fixup, "ax"
256 4:      movl $0, (%esp)
257         jmp 1b
258 5:      movl $0, (%esp)
259         jmp 2b
260 6:      movl $0, (%esp)
261         jmp 3b
262 .section __ex_table, "a"
263         .align 4
264         .long 1b, 4b
265         .long 2b, 5b
266         .long 3b, 6b
267 .popsection
268         POP_GS_EX
269 .endm
270
271 .macro RING0_INT_FRAME
272         CFI_STARTPROC simple
273         CFI_SIGNAL_FRAME
274         CFI_DEF_CFA esp, 3*4
275         /*CFI_OFFSET cs, -2*4;*/
276         CFI_OFFSET eip, -3*4
277 .endm
278
279 .macro RING0_EC_FRAME
280         CFI_STARTPROC simple
281         CFI_SIGNAL_FRAME
282         CFI_DEF_CFA esp, 4*4
283         /*CFI_OFFSET cs, -2*4;*/
284         CFI_OFFSET eip, -3*4
285 .endm
286
287 .macro RING0_PTREGS_FRAME
288         CFI_STARTPROC simple
289         CFI_SIGNAL_FRAME
290         CFI_DEF_CFA esp, PT_OLDESP-PT_EBX
291         /*CFI_OFFSET cs, PT_CS-PT_OLDESP;*/
292         CFI_OFFSET eip, PT_EIP-PT_OLDESP
293         /*CFI_OFFSET es, PT_ES-PT_OLDESP;*/
294         /*CFI_OFFSET ds, PT_DS-PT_OLDESP;*/
295         CFI_OFFSET eax, PT_EAX-PT_OLDESP
296         CFI_OFFSET ebp, PT_EBP-PT_OLDESP
297         CFI_OFFSET edi, PT_EDI-PT_OLDESP
298         CFI_OFFSET esi, PT_ESI-PT_OLDESP
299         CFI_OFFSET edx, PT_EDX-PT_OLDESP
300         CFI_OFFSET ecx, PT_ECX-PT_OLDESP
301         CFI_OFFSET ebx, PT_EBX-PT_OLDESP
302 .endm
303
304 ENTRY(ret_from_fork)
305         CFI_STARTPROC
306         pushl_cfi %eax
307         call schedule_tail
308         GET_THREAD_INFO(%ebp)
309         popl_cfi %eax
310         pushl_cfi $0x0202               # Reset kernel eflags
311         popfl_cfi
312         jmp syscall_exit
313         CFI_ENDPROC
314 END(ret_from_fork)
315
316 /*
317  * Interrupt exit functions should be protected against kprobes
318  */
319         .pushsection .kprobes.text, "ax"
320 /*
321  * Return to user mode is not as complex as all this looks,
322  * but we want the default path for a system call return to
323  * go as quickly as possible which is why some of this is
324  * less clear than it otherwise should be.
325  */
326
327         # userspace resumption stub bypassing syscall exit tracing
328         ALIGN
329         RING0_PTREGS_FRAME
330 ret_from_exception:
331         preempt_stop(CLBR_ANY)
332 ret_from_intr:
333         GET_THREAD_INFO(%ebp)
334 check_userspace:
335         movl PT_EFLAGS(%esp), %eax      # mix EFLAGS and CS
336         movb PT_CS(%esp), %al
337         andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
338         cmpl $USER_RPL, %eax
339         jb resume_kernel                # not returning to v8086 or userspace
340
341 ENTRY(resume_userspace)
342         LOCKDEP_SYS_EXIT
343         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
344                                         # setting need_resched or sigpending
345                                         # between sampling and the iret
346         TRACE_IRQS_OFF
347         movl TI_flags(%ebp), %ecx
348         andl $_TIF_WORK_MASK, %ecx      # is there any work to be done on
349                                         # int/exception return?
350         jne work_pending
351         jmp restore_all
352 END(ret_from_exception)
353
354 #ifdef CONFIG_PREEMPT
355 ENTRY(resume_kernel)
356         DISABLE_INTERRUPTS(CLBR_ANY)
357         cmpl $0,TI_preempt_count(%ebp)  # non-zero preempt_count ?
358         jnz restore_all
359 need_resched:
360         movl TI_flags(%ebp), %ecx       # need_resched set ?
361         testb $_TIF_NEED_RESCHED, %cl
362         jz restore_all
363         testl $X86_EFLAGS_IF,PT_EFLAGS(%esp)    # interrupts off (exception path) ?
364         jz restore_all
365         call preempt_schedule_irq
366         jmp need_resched
367 END(resume_kernel)
368 #endif
369         CFI_ENDPROC
370 /*
371  * End of kprobes section
372  */
373         .popsection
374
375 /* SYSENTER_RETURN points to after the "sysenter" instruction in
376    the vsyscall page.  See vsyscall-sysentry.S, which defines the symbol.  */
377
378         # sysenter call handler stub
379 ENTRY(ia32_sysenter_target)
380         CFI_STARTPROC simple
381         CFI_SIGNAL_FRAME
382         CFI_DEF_CFA esp, 0
383         CFI_REGISTER esp, ebp
384         movl SYSENTER_stack_sp0(%esp),%esp
385 sysenter_past_esp:
386         /*
387          * Interrupts are disabled here, but we can't trace it until
388          * enough kernel state to call TRACE_IRQS_OFF can be called - but
389          * we immediately enable interrupts at that point anyway.
390          */
391         pushl_cfi $__USER_DS
392         /*CFI_REL_OFFSET ss, 0*/
393         pushl_cfi %ebp
394         CFI_REL_OFFSET esp, 0
395         pushfl_cfi
396         orl $X86_EFLAGS_IF, (%esp)
397         pushl_cfi $__USER_CS
398         /*CFI_REL_OFFSET cs, 0*/
399         /*
400          * Push current_thread_info()->sysenter_return to the stack.
401          * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
402          * pushed above; +8 corresponds to copy_thread's esp0 setting.
403          */
404         pushl_cfi ((TI_sysenter_return)-THREAD_SIZE+8+4*4)(%esp)
405         CFI_REL_OFFSET eip, 0
406
407         pushl_cfi %eax
408         SAVE_ALL
409         ENABLE_INTERRUPTS(CLBR_NONE)
410
411 /*
412  * Load the potential sixth argument from user stack.
413  * Careful about security.
414  */
415         cmpl $__PAGE_OFFSET-3,%ebp
416         jae syscall_fault
417 1:      movl (%ebp),%ebp
418         movl %ebp,PT_EBP(%esp)
419 .section __ex_table,"a"
420         .align 4
421         .long 1b,syscall_fault
422 .previous
423
424         GET_THREAD_INFO(%ebp)
425
426         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
427         jnz sysenter_audit
428 sysenter_do_call:
429         cmpl $(NR_syscalls), %eax
430         jae syscall_badsys
431         call *sys_call_table(,%eax,4)
432         movl %eax,PT_EAX(%esp)
433         LOCKDEP_SYS_EXIT
434         DISABLE_INTERRUPTS(CLBR_ANY)
435         TRACE_IRQS_OFF
436         movl TI_flags(%ebp), %ecx
437         testl $_TIF_ALLWORK_MASK, %ecx
438         jne sysexit_audit
439 sysenter_exit:
440 /* if something modifies registers it must also disable sysexit */
441         movl PT_EIP(%esp), %edx
442         movl PT_OLDESP(%esp), %ecx
443         xorl %ebp,%ebp
444 #ifdef CONFIG_XEN_VCPU_INFO_PLACEMENT
445         GET_VCPU_INFO
446 #endif
447         TRACE_IRQS_ON
448 1:      mov  PT_FS(%esp), %fs
449         PTGS_TO_GS
450         ENABLE_INTERRUPTS_SYSEXIT
451
452 #ifdef CONFIG_AUDITSYSCALL
453 sysenter_audit:
454         testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
455         jnz syscall_trace_entry
456         addl $4,%esp
457         CFI_ADJUST_CFA_OFFSET -4
458         /* %esi already in 8(%esp)         6th arg: 4th syscall arg */
459         /* %edx already in 4(%esp)         5th arg: 3rd syscall arg */
460         /* %ecx already in 0(%esp)         4th arg: 2nd syscall arg */
461         movl %ebx,%ecx                  /* 3rd arg: 1st syscall arg */
462         movl %eax,%edx                  /* 2nd arg: syscall number */
463         movl $AUDIT_ARCH_I386,%eax      /* 1st arg: audit arch */
464         call __audit_syscall_entry
465         pushl_cfi %ebx
466         movl PT_EAX(%esp),%eax          /* reload syscall number */
467         jmp sysenter_do_call
468
469 sysexit_audit:
470         testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
471         jne syscall_exit_work
472         TRACE_IRQS_ON
473         ENABLE_INTERRUPTS(CLBR_ANY)
474         movl %eax,%edx          /* second arg, syscall return value */
475         cmpl $-MAX_ERRNO,%eax   /* is it an error ? */
476         setbe %al               /* 1 if so, 0 if not */
477         movzbl %al,%eax         /* zero-extend that */
478         call __audit_syscall_exit
479         DISABLE_INTERRUPTS(CLBR_ANY)
480         TRACE_IRQS_OFF
481         movl TI_flags(%ebp), %ecx
482         testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
483         jne syscall_exit_work
484         movl PT_EAX(%esp),%eax  /* reload syscall return value */
485         jmp sysenter_exit
486 #endif
487
488         CFI_ENDPROC
489 .pushsection .fixup,"ax"
490 2:      movl $0,PT_FS(%esp)
491         jmp 1b
492 .section __ex_table,"a"
493         .align 4
494         .long 1b,2b
495 .popsection
496         PTGS_TO_GS_EX
497 ENDPROC(ia32_sysenter_target)
498
499         # pv sysenter call handler stub
500 ENTRY(ia32pv_sysenter_target)
501         RING0_INT_FRAME
502         movl $__USER_DS,16(%esp)
503         movl %ebp,12(%esp)
504         movl $__USER_CS,4(%esp)
505         addl $4,%esp
506         CFI_ADJUST_CFA_OFFSET -4
507         /* +5*4 is SS:ESP,EFLAGS,CS:EIP. +8 is esp0 setting. */
508         pushl_cfi (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
509 /*
510  * Load the potential sixth argument from user stack.
511  * Careful about security.
512  */
513         cmpl $__PAGE_OFFSET-3,%ebp
514         jae syscall_fault
515 1:      movl (%ebp),%ebp
516 .section __ex_table,"a"
517         .align 4
518         .long 1b,syscall_fault
519 .previous
520         jmp system_call
521         CFI_ENDPROC
522 ENDPROC(ia32pv_sysenter_target)
523
524 /*
525  * syscall stub including irq exit should be protected against kprobes
526  */
527         .pushsection .kprobes.text, "ax"
528         # system call handler stub
529 ENTRY(system_call)
530         RING0_INT_FRAME                 # can't unwind into user space anyway
531         pushl_cfi %eax                  # save orig_eax
532         SAVE_ALL
533         GET_THREAD_INFO(%ebp)
534                                         # system call tracing in operation / emulation
535         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
536         jnz syscall_trace_entry
537         cmpl $(NR_syscalls), %eax
538         jae syscall_badsys
539 syscall_call:
540         call *sys_call_table(,%eax,4)
541         movl %eax,PT_EAX(%esp)          # store the return value
542 syscall_exit:
543         LOCKDEP_SYS_EXIT
544         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
545                                         # setting need_resched or sigpending
546                                         # between sampling and the iret
547         TRACE_IRQS_OFF
548         movl TI_flags(%ebp), %ecx
549         testl $_TIF_ALLWORK_MASK, %ecx  # current->work
550         jne syscall_exit_work
551
552 restore_all:
553         TRACE_IRQS_IRET
554 restore_all_notrace:
555 #ifndef CONFIG_XEN
556         movl PT_EFLAGS(%esp), %eax      # mix EFLAGS, SS and CS
557         # Warning: PT_OLDSS(%esp) contains the wrong/random values if we
558         # are returning to the kernel.
559         # See comments in process.c:copy_thread() for details.
560         movb PT_OLDSS(%esp), %ah
561         movb PT_CS(%esp), %al
562         andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
563         cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
564         CFI_REMEMBER_STATE
565         je ldt_ss                       # returning to user-space with LDT SS
566 restore_nocheck:
567 #else
568 restore_nocheck:
569         movl PT_EFLAGS(%esp), %eax
570         testl $(X86_EFLAGS_VM|NMI_MASK), %eax
571         CFI_REMEMBER_STATE
572         jnz hypervisor_iret
573         shr $9, %eax                    # EAX[0] == IRET_EFLAGS.IF
574         GET_VCPU_INFO
575         andb evtchn_upcall_mask(%esi),%al
576         andb $1,%al                     # EAX[0] == IRET_EFLAGS.IF & event_mask
577         CFI_REMEMBER_STATE
578         jnz restore_all_enable_events   #        != 0 => enable event delivery
579 #endif
580         RESTORE_REGS 4                  # skip orig_eax/error_code
581 irq_return:
582         INTERRUPT_RETURN
583 .section .fixup,"ax"
584 ENTRY(iret_exc)
585         pushl $0                        # no error code
586         pushl $do_iret_error
587         jmp error_code
588 .previous
589 .section __ex_table,"a"
590         .align 4
591         .long irq_return,iret_exc
592 .previous
593
594         CFI_RESTORE_STATE
595 #ifndef CONFIG_XEN
596 ldt_ss:
597         larl PT_OLDSS(%esp), %eax
598         jnz restore_nocheck
599         testl $0x00400000, %eax         # returning to 32bit stack?
600         jnz restore_nocheck             # allright, normal return
601
602 #ifdef CONFIG_PARAVIRT
603         /*
604          * The kernel can't run on a non-flat stack if paravirt mode
605          * is active.  Rather than try to fixup the high bits of
606          * ESP, bypass this code entirely.  This may break DOSemu
607          * and/or Wine support in a paravirt VM, although the option
608          * is still available to implement the setting of the high
609          * 16-bits in the INTERRUPT_RETURN paravirt-op.
610          */
611         cmpl $0, pv_info+PARAVIRT_enabled
612         jne restore_nocheck
613 #endif
614
615 /*
616  * Setup and switch to ESPFIX stack
617  *
618  * We're returning to userspace with a 16 bit stack. The CPU will not
619  * restore the high word of ESP for us on executing iret... This is an
620  * "official" bug of all the x86-compatible CPUs, which we can work
621  * around to make dosemu and wine happy. We do this by preloading the
622  * high word of ESP with the high word of the userspace ESP while
623  * compensating for the offset by changing to the ESPFIX segment with
624  * a base address that matches for the difference.
625  */
626 #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
627         mov %esp, %edx                  /* load kernel esp */
628         mov PT_OLDESP(%esp), %eax       /* load userspace esp */
629         mov %dx, %ax                    /* eax: new kernel esp */
630         sub %eax, %edx                  /* offset (low word is 0) */
631         shr $16, %edx
632         mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
633         mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
634         pushl_cfi $__ESPFIX_SS
635         pushl_cfi %eax                  /* new kernel esp */
636         /* Disable interrupts, but do not irqtrace this section: we
637          * will soon execute iret and the tracer was already set to
638          * the irqstate after the iret */
639         DISABLE_INTERRUPTS(CLBR_EAX)
640         lss (%esp), %esp                /* switch to espfix segment */
641         CFI_ADJUST_CFA_OFFSET -8
642         jmp restore_nocheck
643 #else
644         ALIGN
645 restore_all_enable_events:
646         TRACE_IRQS_ON
647         __ENABLE_INTERRUPTS
648 scrit:  /**** START OF CRITICAL REGION ****/
649         __TEST_PENDING
650         jnz  14f                        # process more events if necessary...
651         RESTORE_REGS 4
652 1:      INTERRUPT_RETURN
653 .section __ex_table,"a"
654         .align 4
655         .long 1b,iret_exc
656 .previous
657 14:     __DISABLE_INTERRUPTS
658         TRACE_IRQS_OFF
659 ecrit:  /**** END OF CRITICAL REGION ****/
660         jmp  .Ldo_upcall
661
662         CFI_RESTORE_STATE
663 hypervisor_iret:
664         andl $~NMI_MASK, PT_EFLAGS(%esp)
665         RESTORE_REGS 4
666         jmp  hypercall_page + (__HYPERVISOR_iret * 32)
667 #endif
668         CFI_ENDPROC
669 ENDPROC(system_call)
670
671         # perform work that needs to be done immediately before resumption
672         ALIGN
673         RING0_PTREGS_FRAME              # can't unwind into user space anyway
674 work_pending:
675         testb $_TIF_NEED_RESCHED, %cl
676         jz work_notifysig
677 work_resched:
678         call schedule
679         LOCKDEP_SYS_EXIT
680         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
681                                         # setting need_resched or sigpending
682                                         # between sampling and the iret
683         TRACE_IRQS_OFF
684         movl TI_flags(%ebp), %ecx
685         andl $_TIF_WORK_MASK, %ecx      # is there any work to be done other
686                                         # than syscall tracing?
687         jz restore_all
688         testb $_TIF_NEED_RESCHED, %cl
689         jnz work_resched
690
691 work_notifysig:                         # deal with pending signals and
692                                         # notify-resume requests
693 #ifdef CONFIG_VM86
694         testl $X86_EFLAGS_VM, PT_EFLAGS(%esp)
695         movl %esp, %eax
696         jne work_notifysig_v86          # returning to kernel-space or
697                                         # vm86-space
698         TRACE_IRQS_ON
699         ENABLE_INTERRUPTS(CLBR_NONE)
700         xorl %edx, %edx
701         call do_notify_resume
702         jmp resume_userspace_sig
703
704         ALIGN
705 work_notifysig_v86:
706         pushl_cfi %ecx                  # save ti_flags for do_notify_resume
707         call save_v86_state             # %eax contains pt_regs pointer
708         popl_cfi %ecx
709         movl %eax, %esp
710 #else
711         movl %esp, %eax
712 #endif
713         TRACE_IRQS_ON
714         ENABLE_INTERRUPTS(CLBR_NONE)
715         xorl %edx, %edx
716         call do_notify_resume
717         jmp resume_userspace_sig
718 END(work_pending)
719
720         # perform syscall exit tracing
721         ALIGN
722 syscall_trace_entry:
723         movl $-ENOSYS,PT_EAX(%esp)
724         movl %esp, %eax
725         call syscall_trace_enter
726         /* What it returned is what we'll actually use.  */
727         cmpl $(NR_syscalls), %eax
728         jnae syscall_call
729         jmp syscall_exit
730 END(syscall_trace_entry)
731
732         # perform syscall exit tracing
733         ALIGN
734 syscall_exit_work:
735         testl $_TIF_WORK_SYSCALL_EXIT, %ecx
736         jz work_pending
737         TRACE_IRQS_ON
738         ENABLE_INTERRUPTS(CLBR_ANY)     # could let syscall_trace_leave() call
739                                         # schedule() instead
740         movl %esp, %eax
741         call syscall_trace_leave
742         jmp resume_userspace
743 END(syscall_exit_work)
744         CFI_ENDPROC
745
746         RING0_INT_FRAME                 # can't unwind into user space anyway
747 syscall_fault:
748         GET_THREAD_INFO(%ebp)
749         movl $-EFAULT,PT_EAX(%esp)
750         jmp resume_userspace
751 END(syscall_fault)
752
753 syscall_badsys:
754         movl $-ENOSYS,PT_EAX(%esp)
755         jmp resume_userspace
756 END(syscall_badsys)
757         CFI_ENDPROC
758 /*
759  * End of kprobes section
760  */
761         .popsection
762
763 /*
764  * System calls that need a pt_regs pointer.
765  */
766 #define PTREGSCALL0(name) \
767 ENTRY(ptregs_##name) ;  \
768         leal 4(%esp),%eax; \
769         jmp sys_##name; \
770 ENDPROC(ptregs_##name)
771
772 #define PTREGSCALL1(name) \
773 ENTRY(ptregs_##name) ; \
774         leal 4(%esp),%edx; \
775         movl (PT_EBX+4)(%esp),%eax; \
776         jmp sys_##name; \
777 ENDPROC(ptregs_##name)
778
779 #define PTREGSCALL2(name) \
780 ENTRY(ptregs_##name) ; \
781         leal 4(%esp),%ecx; \
782         movl (PT_ECX+4)(%esp),%edx; \
783         movl (PT_EBX+4)(%esp),%eax; \
784         jmp sys_##name; \
785 ENDPROC(ptregs_##name)
786
787 #define PTREGSCALL3(name) \
788 ENTRY(ptregs_##name) ; \
789         CFI_STARTPROC; \
790         leal 4(%esp),%eax; \
791         pushl_cfi %eax; \
792         movl PT_EDX(%eax),%ecx; \
793         movl PT_ECX(%eax),%edx; \
794         movl PT_EBX(%eax),%eax; \
795         call sys_##name; \
796         addl $4,%esp; \
797         CFI_ADJUST_CFA_OFFSET -4; \
798         ret; \
799         CFI_ENDPROC; \
800 ENDPROC(ptregs_##name)
801
802 PTREGSCALL1(iopl)
803 PTREGSCALL0(fork)
804 PTREGSCALL0(vfork)
805 PTREGSCALL3(execve)
806 PTREGSCALL2(sigaltstack)
807 PTREGSCALL0(sigreturn)
808 PTREGSCALL0(rt_sigreturn)
809 PTREGSCALL2(vm86)
810 PTREGSCALL1(vm86old)
811
812 /* Clone is an oddball.  The 4th arg is in %edi */
813 ENTRY(ptregs_clone)
814         CFI_STARTPROC
815         leal 4(%esp),%eax
816         pushl_cfi %eax
817         pushl_cfi PT_EDI(%eax)
818         movl PT_EDX(%eax),%ecx
819         movl PT_ECX(%eax),%edx
820         movl PT_EBX(%eax),%eax
821         call sys_clone
822         addl $8,%esp
823         CFI_ADJUST_CFA_OFFSET -8
824         ret
825         CFI_ENDPROC
826 ENDPROC(ptregs_clone)
827
828 #ifndef CONFIG_XEN
829 .macro FIXUP_ESPFIX_STACK
830 /*
831  * Switch back for ESPFIX stack to the normal zerobased stack
832  *
833  * We can't call C functions using the ESPFIX stack. This code reads
834  * the high word of the segment base from the GDT and swiches to the
835  * normal stack and adjusts ESP with the matching offset.
836  */
837         /* fixup the stack */
838         mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
839         mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
840         shl $16, %eax
841         addl %esp, %eax                 /* the adjusted stack pointer */
842         pushl_cfi $__KERNEL_DS
843         pushl_cfi %eax
844         lss (%esp), %esp                /* switch to the normal stack segment */
845         CFI_ADJUST_CFA_OFFSET -8
846 .endm
847 .macro UNWIND_ESPFIX_STACK
848         movl %ss, %eax
849         /* see if on espfix stack */
850         cmpw $__ESPFIX_SS, %ax
851         jne 27f
852         movl $__KERNEL_DS, %eax
853         movl %eax, %ds
854         movl %eax, %es
855         /* switch to normal stack */
856         FIXUP_ESPFIX_STACK
857 27:
858 .endm
859
860 /*
861  * Build the entry stubs and pointer table with some assembler magic.
862  * We pack 7 stubs into a single 32-byte chunk, which will fit in a
863  * single cache line on all modern x86 implementations.
864  */
865 .section .init.rodata,"a"
866 ENTRY(interrupt)
867 .section .entry.text, "ax"
868         .p2align 5
869         .p2align CONFIG_X86_L1_CACHE_SHIFT
870 ENTRY(irq_entries_start)
871         RING0_INT_FRAME
872 vector=FIRST_EXTERNAL_VECTOR
873 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
874         .balign 32
875   .rept 7
876     .if vector < NR_VECTORS
877       .if vector <> FIRST_EXTERNAL_VECTOR
878         CFI_ADJUST_CFA_OFFSET -4
879       .endif
880 1:      pushl_cfi $(~vector+0x80)       /* Note: always in signed byte range */
881       .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
882         jmp 2f
883       .endif
884       .previous
885         .long 1b
886       .section .entry.text, "ax"
887 vector=vector+1
888     .endif
889   .endr
890 2:      jmp common_interrupt
891 .endr
892 END(irq_entries_start)
893
894 .previous
895 END(interrupt)
896 .previous
897
898 /*
899  * the CPU automatically disables interrupts when executing an IRQ vector,
900  * so IRQ-flags tracing has to follow that:
901  */
902         .p2align CONFIG_X86_L1_CACHE_SHIFT
903 common_interrupt:
904         addl $-0x80,(%esp)      /* Adjust vector into the [-256,-1] range */
905         SAVE_ALL
906         TRACE_IRQS_OFF
907         movl %esp,%eax
908         call do_IRQ
909         jmp ret_from_intr
910 ENDPROC(common_interrupt)
911         CFI_ENDPROC
912
913 /*
914  *  Irq entries should be protected against kprobes
915  */
916         .pushsection .kprobes.text, "ax"
917 #define BUILD_INTERRUPT3(name, nr, fn)  \
918 ENTRY(name)                             \
919         RING0_INT_FRAME;                \
920         pushl_cfi $~(nr);               \
921         SAVE_ALL;                       \
922         TRACE_IRQS_OFF                  \
923         movl %esp,%eax;                 \
924         call fn;                        \
925         jmp ret_from_intr;              \
926         CFI_ENDPROC;                    \
927 ENDPROC(name)
928
929 #define BUILD_INTERRUPT(name, nr)       BUILD_INTERRUPT3(name, nr, smp_##name)
930
931 /* The include is where all of the SMP etc. interrupts come from */
932 #include <asm/entry_arch.h>
933
934 #else
935 #define UNWIND_ESPFIX_STACK
936
937         .pushsection .kprobes.text, "ax"
938
939 # A note on the "critical region" in our callback handler.
940 # We want to avoid stacking callback handlers due to events occurring
941 # during handling of the last event. To do this, we keep events disabled
942 # until we've done all processing. HOWEVER, we must enable events before
943 # popping the stack frame (can't be done atomically) and so it would still
944 # be possible to get enough handler activations to overflow the stack.
945 # Although unlikely, bugs of that kind are hard to track down, so we'd
946 # like to avoid the possibility.
947 # So, on entry to the handler we detect whether we interrupted an
948 # existing activation in its critical region -- if so, we pop the current
949 # activation and restart the handler using the previous one.
950 #
951 # The sysexit critical region is slightly different. sysexit
952 # atomically removes the entire stack frame. If we interrupt in the
953 # critical region we know that the entire frame is present and correct
954 # so we can simply throw away the new one.
955 ENTRY(hypervisor_callback)
956         RING0_INT_FRAME
957         pushl_cfi %eax
958         SAVE_ALL
959         movl PT_CS(%esp),%ecx
960         movl PT_EIP(%esp),%eax
961         andl $SEGMENT_RPL_MASK,%ecx
962         cmpl $USER_RPL,%ecx
963         jae  .Ldo_upcall
964         cmpl $scrit,%eax
965         jb   0f
966         cmpl $ecrit,%eax
967         jb   critical_region_fixup
968 0:
969 #ifdef CONFIG_XEN_SUPERVISOR_MODE_KERNEL
970         cmpl $sysexit_scrit,%eax
971         jb   .Ldo_upcall
972         cmpl $sysexit_ecrit,%eax
973         ja   .Ldo_upcall
974         addl $PT_OLDESP,%esp            # Remove eflags...ebx from stack frame.
975 #endif
976 .Ldo_upcall:
977         pushl_cfi %esp
978         call evtchn_do_upcall
979         add  $4,%esp
980         CFI_ADJUST_CFA_OFFSET -4
981         jmp  ret_from_intr
982         CFI_ENDPROC
983
984 # [How we do the fixup]. We want to merge the current stack frame with the
985 # just-interrupted frame. How we do this depends on where in the critical
986 # region the interrupted handler was executing, and so how many saved
987 # registers are in each frame. We do this quickly using the lookup table
988 # 'critical_fixup_table'. For each byte offset in the critical region, it
989 # provides the number of bytes which have already been popped from the
990 # interrupted stack frame.
991 critical_region_fixup:
992         movsbl critical_fixup_table-scrit(%eax),%ecx # %ecx contains num slots popped
993         testl %ecx,%ecx
994         leal (%esp,%ecx,4),%esi         # %esi points at end of src region
995         leal PT_OLDESP(%esp),%edi       # %edi points at end of dst region
996         jle   17f                       # skip loop if nothing to copy
997 16:     subl $4,%esi                    # pre-decrementing copy loop
998         subl $4,%edi
999         movl (%esi),%eax
1000         movl %eax,(%edi)
1001         loop 16b
1002 17:     movl %edi,%esp                  # final %edi is top of merged stack
1003         jmp  .Ldo_upcall
1004
1005 .section .rodata,"a"
1006 critical_fixup_table:
1007         .rept __SIZEOF_TEST_PENDING
1008         .byte -1
1009         .endr
1010         .byte -1,-1                     # jnz  14f
1011         .byte 0                         # pop  %ebx
1012         .byte 1                         # pop  %ecx
1013         .byte 2                         # pop  %edx
1014         .byte 3                         # pop  %esi
1015         .byte 4                         # pop  %edi
1016         .byte 5                         # pop  %ebp
1017         .byte 6                         # pop  %eax
1018         .byte 7                         # pop  %ds
1019         .byte 8                         # pop  %es
1020         .byte 9,9                       # pop  %fs
1021 #ifndef CONFIG_X86_32_LAZY_GS
1022         .byte 10,10                     # pop  %gs
1023         .byte 11,11,11                  # add  $4,%esp
1024 #else
1025         .byte 10,10,10                  # add  $8,%esp
1026 #endif
1027         .byte 12                        # iret
1028         .rept __SIZEOF_DISABLE_INTERRUPTS
1029         .byte -1
1030         .endr
1031 .previous
1032
1033 # Hypervisor uses this for application faults while it executes.
1034 # We get here for two reasons:
1035 #  1. Fault while reloading DS, ES, FS or GS
1036 #  2. Fault while executing IRET
1037 # Category 1 we fix up by reattempting the load, and zeroing the segment
1038 # register if the load fails.
1039 # Category 2 we fix up by jumping to do_iret_error. We cannot use the
1040 # normal Linux return path in this case because if we use the IRET hypercall
1041 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1042 # We distinguish between categories by maintaining a status value in EAX.
1043 ENTRY(failsafe_callback)
1044         pushl %eax
1045         movl $1,%eax
1046 1:      mov 4(%esp),%ds
1047 2:      mov 8(%esp),%es
1048 3:      mov 12(%esp),%fs
1049 4:      mov 16(%esp),%gs
1050         testl %eax,%eax
1051         popl %eax
1052         jz 5f
1053         addl $16,%esp           # EAX != 0 => Category 2 (Bad IRET)
1054         jmp iret_exc
1055 5:      addl $16,%esp           # EAX == 0 => Category 1 (Bad segment)
1056         RING0_INT_FRAME
1057         pushl $0
1058         SAVE_ALL
1059         jmp ret_from_exception
1060 .section .fixup,"ax";           \
1061 6:      xorl %eax,%eax;         \
1062         movl %eax,4(%esp);      \
1063         jmp 1b;                 \
1064 7:      xorl %eax,%eax;         \
1065         movl %eax,8(%esp);      \
1066         jmp 2b;                 \
1067 8:      xorl %eax,%eax;         \
1068         movl %eax,12(%esp);     \
1069         jmp 3b;                 \
1070 9:      xorl %eax,%eax;         \
1071         movl %eax,16(%esp);     \
1072         jmp 4b;                 \
1073 .previous;                      \
1074 .section __ex_table,"a";        \
1075         .align 4;               \
1076         .long 1b,6b;            \
1077         .long 2b,7b;            \
1078         .long 3b,8b;            \
1079         .long 4b,9b;            \
1080 .previous
1081 #endif
1082         CFI_ENDPROC
1083
1084 ENTRY(coprocessor_error)
1085         RING0_INT_FRAME
1086         pushl_cfi $0
1087         pushl_cfi $do_coprocessor_error
1088         jmp error_code
1089         CFI_ENDPROC
1090 END(coprocessor_error)
1091
1092 ENTRY(simd_coprocessor_error)
1093         RING0_INT_FRAME
1094         pushl_cfi $0
1095 #ifdef CONFIG_X86_INVD_BUG
1096         /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
1097 661:    pushl_cfi $do_general_protection
1098 662:
1099 .section .altinstructions,"a"
1100         altinstruction_entry 661b, 663f, X86_FEATURE_XMM, 662b-661b, 664f-663f
1101 .previous
1102 .section .altinstr_replacement,"ax"
1103 663:    pushl $do_simd_coprocessor_error
1104 664:
1105 .previous
1106 #else
1107         pushl_cfi $do_simd_coprocessor_error
1108 #endif
1109         jmp error_code
1110         CFI_ENDPROC
1111 END(simd_coprocessor_error)
1112
1113 ENTRY(device_not_available)
1114         RING0_INT_FRAME
1115         pushl_cfi $-1                   # mark this as an int
1116         pushl_cfi $do_device_not_available
1117         jmp error_code
1118         CFI_ENDPROC
1119 END(device_not_available)
1120
1121 #ifdef CONFIG_PARAVIRT
1122 ENTRY(native_iret)
1123         iret
1124 .section __ex_table,"a"
1125         .align 4
1126         .long native_iret, iret_exc
1127 .previous
1128 END(native_iret)
1129
1130 ENTRY(native_irq_enable_sysexit)
1131         sti
1132         sysexit
1133 END(native_irq_enable_sysexit)
1134 #endif
1135
1136 ENTRY(overflow)
1137         RING0_INT_FRAME
1138         pushl_cfi $0
1139         pushl_cfi $do_overflow
1140         jmp error_code
1141         CFI_ENDPROC
1142 END(overflow)
1143
1144 ENTRY(bounds)
1145         RING0_INT_FRAME
1146         pushl_cfi $0
1147         pushl_cfi $do_bounds
1148         jmp error_code
1149         CFI_ENDPROC
1150 END(bounds)
1151
1152 ENTRY(invalid_op)
1153         RING0_INT_FRAME
1154         pushl_cfi $0
1155         pushl_cfi $do_invalid_op
1156         jmp error_code
1157         CFI_ENDPROC
1158 END(invalid_op)
1159
1160 ENTRY(coprocessor_segment_overrun)
1161         RING0_INT_FRAME
1162         pushl_cfi $0
1163         pushl_cfi $do_coprocessor_segment_overrun
1164         jmp error_code
1165         CFI_ENDPROC
1166 END(coprocessor_segment_overrun)
1167
1168 ENTRY(invalid_TSS)
1169         RING0_EC_FRAME
1170         pushl_cfi $do_invalid_TSS
1171         jmp error_code
1172         CFI_ENDPROC
1173 END(invalid_TSS)
1174
1175 ENTRY(segment_not_present)
1176         RING0_EC_FRAME
1177         pushl_cfi $do_segment_not_present
1178         jmp error_code
1179         CFI_ENDPROC
1180 END(segment_not_present)
1181
1182 ENTRY(stack_segment)
1183         RING0_EC_FRAME
1184         pushl_cfi $do_stack_segment
1185         jmp error_code
1186         CFI_ENDPROC
1187 END(stack_segment)
1188
1189 ENTRY(alignment_check)
1190         RING0_EC_FRAME
1191         pushl_cfi $do_alignment_check
1192         jmp error_code
1193         CFI_ENDPROC
1194 END(alignment_check)
1195
1196 ENTRY(divide_error)
1197         RING0_INT_FRAME
1198         pushl_cfi $0                    # no error code
1199         pushl_cfi $do_divide_error
1200         jmp error_code
1201         CFI_ENDPROC
1202 END(divide_error)
1203
1204 #ifdef CONFIG_X86_MCE
1205 ENTRY(machine_check)
1206         RING0_INT_FRAME
1207         pushl_cfi $0
1208         pushl_cfi machine_check_vector
1209         jmp error_code
1210         CFI_ENDPROC
1211 END(machine_check)
1212 #endif
1213
1214 #ifndef CONFIG_XEN
1215 ENTRY(spurious_interrupt_bug)
1216         RING0_INT_FRAME
1217         pushl_cfi $0
1218         pushl_cfi $do_spurious_interrupt_bug
1219         jmp error_code
1220         CFI_ENDPROC
1221 END(spurious_interrupt_bug)
1222 #endif /* !CONFIG_XEN */
1223
1224 ENTRY(fixup_4gb_segment)
1225         RING0_EC_FRAME
1226         pushl_cfi $do_fixup_4gb_segment
1227         jmp error_code
1228         CFI_ENDPROC
1229 END(fixup_4gb_segment)
1230 /*
1231  * End of kprobes section
1232  */
1233         .popsection
1234
1235 #ifdef CONFIG_STACK_UNWIND
1236 ENTRY(arch_unwind_init_running)
1237         CFI_STARTPROC
1238         movl    4(%esp), %edx
1239         movl    (%esp), %ecx
1240         leal    4(%esp), %eax
1241         movl    %ebx, PT_EBX(%edx)
1242         xorl    %ebx, %ebx
1243         movl    %ebx, PT_ECX(%edx)
1244         movl    %ebx, PT_EDX(%edx)
1245         movl    %esi, PT_ESI(%edx)
1246         movl    %edi, PT_EDI(%edx)
1247         movl    %ebp, PT_EBP(%edx)
1248         movl    %ebx, PT_EAX(%edx)
1249         movl    $__USER_DS, PT_DS(%edx)
1250         movl    $__USER_DS, PT_ES(%edx)
1251         movl    $__KERNEL_PERCPU, PT_FS(%edx)
1252         movl    $__KERNEL_STACK_CANARY, PT_GS(%edx)
1253         movl    %eax, PT_OLDESP(%edx)
1254         movl    16(%esp), %eax
1255         movl    %ebx, PT_ORIG_EAX(%edx)
1256         movl    %ecx, PT_EIP(%edx)
1257         movl    12(%esp), %ecx
1258         movl    $__KERNEL_CS, PT_CS(%edx)
1259         movl    %eax, 12(%esp)
1260         movl    8(%esp), %eax
1261         movl    %ecx, 8(%esp)
1262         movl    %ebx, PT_EFLAGS(%edx)
1263         movl    PT_EBX(%edx), %ebx
1264         movl    $__KERNEL_DS, PT_OLDSS(%edx)
1265         jmpl    *%eax
1266         CFI_ENDPROC
1267 ENDPROC(arch_unwind_init_running)
1268 #endif
1269
1270 ENTRY(kernel_thread_helper)
1271         pushl $0                # fake return address for unwinder
1272         CFI_STARTPROC
1273         movl %edi,%eax
1274         call *%esi
1275         call do_exit
1276         ud2                     # padding for call trace
1277         CFI_ENDPROC
1278 ENDPROC(kernel_thread_helper)
1279
1280 #ifdef CONFIG_FUNCTION_TRACER
1281 #ifdef CONFIG_DYNAMIC_FTRACE
1282
1283 ENTRY(mcount)
1284         ret
1285 END(mcount)
1286
1287 ENTRY(ftrace_caller)
1288         cmpl $0, function_trace_stop
1289         jne  ftrace_stub
1290
1291         pushl %eax
1292         pushl %ecx
1293         pushl %edx
1294         movl 0xc(%esp), %eax
1295         movl 0x4(%ebp), %edx
1296         subl $MCOUNT_INSN_SIZE, %eax
1297
1298 .globl ftrace_call
1299 ftrace_call:
1300         call ftrace_stub
1301
1302         popl %edx
1303         popl %ecx
1304         popl %eax
1305 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1306 .globl ftrace_graph_call
1307 ftrace_graph_call:
1308         jmp ftrace_stub
1309 #endif
1310
1311 .globl ftrace_stub
1312 ftrace_stub:
1313         ret
1314 END(ftrace_caller)
1315
1316 #else /* ! CONFIG_DYNAMIC_FTRACE */
1317
1318 ENTRY(mcount)
1319         cmpl $0, function_trace_stop
1320         jne  ftrace_stub
1321
1322         cmpl $ftrace_stub, ftrace_trace_function
1323         jnz trace
1324 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1325         cmpl $ftrace_stub, ftrace_graph_return
1326         jnz ftrace_graph_caller
1327
1328         cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
1329         jnz ftrace_graph_caller
1330 #endif
1331 .globl ftrace_stub
1332 ftrace_stub:
1333         ret
1334
1335         /* taken from glibc */
1336 trace:
1337         pushl %eax
1338         pushl %ecx
1339         pushl %edx
1340         movl 0xc(%esp), %eax
1341         movl 0x4(%ebp), %edx
1342         subl $MCOUNT_INSN_SIZE, %eax
1343
1344         call *ftrace_trace_function
1345
1346         popl %edx
1347         popl %ecx
1348         popl %eax
1349         jmp ftrace_stub
1350 END(mcount)
1351 #endif /* CONFIG_DYNAMIC_FTRACE */
1352 #endif /* CONFIG_FUNCTION_TRACER */
1353
1354 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1355 ENTRY(ftrace_graph_caller)
1356         cmpl $0, function_trace_stop
1357         jne ftrace_stub
1358
1359         pushl %eax
1360         pushl %ecx
1361         pushl %edx
1362         movl 0xc(%esp), %edx
1363         lea 0x4(%ebp), %eax
1364         movl (%ebp), %ecx
1365         subl $MCOUNT_INSN_SIZE, %edx
1366         call prepare_ftrace_return
1367         popl %edx
1368         popl %ecx
1369         popl %eax
1370         ret
1371 END(ftrace_graph_caller)
1372
1373 .globl return_to_handler
1374 return_to_handler:
1375         pushl %eax
1376         pushl %edx
1377         movl %ebp, %eax
1378         call ftrace_return_to_handler
1379         movl %eax, %ecx
1380         popl %edx
1381         popl %eax
1382         jmp *%ecx
1383 #endif
1384
1385 #ifdef TIF_CSTAR
1386         # pv syscall call handler stub
1387 ENTRY(ia32pv_cstar_target)
1388         RING0_INT_FRAME
1389         movl $__USER_DS,16(%esp)
1390         movl %ebp,%ecx
1391         movl $__USER_CS,4(%esp)
1392         movl 12(%esp),%ebp
1393         pushl_cfi %eax                  # save orig_eax
1394 /*
1395  * Load the potential sixth argument from user stack.
1396  * Careful about security.
1397  */
1398         cmpl $__PAGE_OFFSET-4,%ebp
1399         CFI_REMEMBER_STATE
1400         ja cstar_fault
1401 1:      movl (%ebp),%ebp
1402 .section __ex_table,"a"
1403         .align 4
1404         .long 1b,cstar_fault
1405 .previous
1406         SAVE_ALL
1407         GET_THREAD_INFO(%ebp)
1408         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
1409         jnz cstar_trace_entry
1410         cmpl $NR_syscalls,%eax
1411         jae cstar_badsys
1412 .Lcstar_call:
1413         btl %eax,cstar_special
1414         jc .Lcstar_special
1415         call *cstar_call_table(,%eax,4)
1416         movl %eax,PT_EAX(%esp)          # store the return value
1417 .Lcstar_exit:
1418         movl PT_ECX(%esp),%ecx
1419         movl %ecx,PT_EBP(%esp)          # put user EBP back in place
1420         jmp syscall_exit
1421 .Lcstar_special:
1422         movl PT_ECX(%esp),%ecx
1423         movl %ecx,PT_EBP(%esp)          # put user EBP back in place
1424         jmp syscall_call
1425 GLOBAL(cstar_set_tif)
1426         movl $cstar_clear_tif,(%esp)    # replace return address
1427         LOCK_PREFIX
1428         orl $_TIF_CSTAR,TI_flags(%ebp)
1429         jmp *sys_call_table(,%eax,4)
1430 cstar_clear_tif:
1431         movl %eax,PT_EAX(%esp)          # store the return value
1432         LOCK_PREFIX
1433         andl $~_TIF_CSTAR,TI_flags(%ebp)
1434         jmp .Lcstar_exit
1435 cstar_trace_entry:
1436         movl $-ENOSYS,PT_EAX(%esp)
1437         cmpl $NR_syscalls,%eax
1438         jae 1f
1439         btl %eax,cstar_special
1440         jc .Lcstar_trace_special
1441 1:      movl %esp,%eax
1442         LOCK_PREFIX
1443         orl $_TIF_CSTAR,TI_flags(%ebp)
1444         call syscall_trace_enter
1445         LOCK_PREFIX
1446         andl $~_TIF_CSTAR,TI_flags(%ebp)
1447         /* What it returned is what we'll actually use.  */
1448         cmpl $NR_syscalls,%eax
1449         jb .Lcstar_call
1450         jmp .Lcstar_exit
1451 .Lcstar_trace_special:
1452         movl PT_ECX(%esp),%ecx
1453         movl %esp,%eax
1454         movl %ecx,PT_EBP(%esp)          # put user EBP back in place
1455         call syscall_trace_enter
1456         /* What it returned is what we'll actually use.  */
1457         cmpl $NR_syscalls,%eax
1458         jb syscall_call
1459         jmp syscall_exit
1460 cstar_badsys:
1461         movl $-ENOSYS,PT_EAX(%esp)
1462 .Lcstar_resume:
1463         movl PT_ECX(%esp),%ecx
1464         movl %ecx,PT_EBP(%esp)          # put user EBP back in place
1465         jmp resume_userspace
1466         CFI_RESTORE_STATE
1467 cstar_fault:
1468         movl $-EFAULT,%eax
1469         SAVE_ALL
1470         GET_THREAD_INFO(%ebp)
1471         jmp .Lcstar_resume
1472         CFI_ENDPROC
1473 ENDPROC(ia32pv_cstar_target)
1474
1475 ENTRY(cstar_ret_from_fork)
1476         CFI_STARTPROC
1477         movl PT_ECX(%esp),%ecx
1478         GET_THREAD_INFO(%ebp)
1479         movl %ecx,PT_EBP(%esp)          # put user EBP back in place
1480         LOCK_PREFIX
1481         andl $~_TIF_CSTAR,TI_flags(%ebp)
1482         jmp ret_from_fork
1483         CFI_ENDPROC
1484 END(cstar_ret_from_fork)
1485
1486 #include <asm/unistd.h>
1487 .pushsection .rodata,"a"
1488 .balign 4
1489 cstar_special:
1490 nr=0
1491 mask=0
1492 .rept NR_syscalls+31
1493  .irp n, __NR_sigreturn, __NR_rt_sigreturn
1494   .if nr == \n
1495    mask = mask | (1 << (\n & 31))
1496   .endif
1497  .endr
1498  nr = nr + 1
1499  .if (nr & 31) == 0
1500   .long mask
1501   mask = 0
1502  .endif
1503 .endr
1504 .popsection
1505 #endif /* TIF_CSTAR */
1506
1507 /*
1508  * Some functions should be protected against kprobes
1509  */
1510         .pushsection .kprobes.text, "ax"
1511
1512 ENTRY(page_fault)
1513         RING0_EC_FRAME
1514         pushl_cfi $do_page_fault
1515         ALIGN
1516 error_code:
1517         /* the function address is in %gs's slot on the stack */
1518         pushl_cfi %fs
1519         /*CFI_REL_OFFSET fs, 0*/
1520         pushl_cfi %es
1521         /*CFI_REL_OFFSET es, 0*/
1522         pushl_cfi %ds
1523         /*CFI_REL_OFFSET ds, 0*/
1524         pushl_cfi %eax
1525         CFI_REL_OFFSET eax, 0
1526         pushl_cfi %ebp
1527         CFI_REL_OFFSET ebp, 0
1528         pushl_cfi %edi
1529         CFI_REL_OFFSET edi, 0
1530         pushl_cfi %esi
1531         CFI_REL_OFFSET esi, 0
1532         pushl_cfi %edx
1533         CFI_REL_OFFSET edx, 0
1534         pushl_cfi %ecx
1535         CFI_REL_OFFSET ecx, 0
1536         pushl_cfi %ebx
1537         CFI_REL_OFFSET ebx, 0
1538         cld
1539         movl $(__KERNEL_PERCPU), %ecx
1540         movl %ecx, %fs
1541         UNWIND_ESPFIX_STACK
1542         GS_TO_REG %ecx
1543         movl PT_GS(%esp), %edi          # get the function address
1544         movl PT_ORIG_EAX(%esp), %edx    # get the error code
1545         movl $-1, PT_ORIG_EAX(%esp)     # no syscall to restart
1546         REG_TO_PTGS %ecx
1547         SET_KERNEL_GS %ecx
1548         movl $(__USER_DS), %ecx
1549         movl %ecx, %ds
1550         movl %ecx, %es
1551         TRACE_IRQS_OFF
1552         movl %esp,%eax                  # pt_regs pointer
1553         call *%edi
1554         jmp ret_from_exception
1555         CFI_ENDPROC
1556 END(page_fault)
1557
1558 #ifndef CONFIG_XEN
1559 /*
1560  * Debug traps and NMI can happen at the one SYSENTER instruction
1561  * that sets up the real kernel stack. Check here, since we can't
1562  * allow the wrong stack to be used.
1563  *
1564  * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
1565  * already pushed 3 words if it hits on the sysenter instruction:
1566  * eflags, cs and eip.
1567  *
1568  * We just load the right stack, and push the three (known) values
1569  * by hand onto the new stack - while updating the return eip past
1570  * the instruction that would have done it for sysenter.
1571  */
1572 .macro FIX_STACK offset ok label
1573         cmpw $__KERNEL_CS, 4(%esp)
1574         jne \ok
1575 \label:
1576         movl TSS_sysenter_sp0 + \offset(%esp), %esp
1577         CFI_DEF_CFA esp, 0
1578         CFI_UNDEFINED eip
1579         pushfl_cfi
1580         pushl_cfi $__KERNEL_CS
1581         pushl_cfi $sysenter_past_esp
1582         CFI_REL_OFFSET eip, 0
1583 .endm
1584 #endif /* CONFIG_XEN */
1585
1586 ENTRY(debug)
1587         RING0_INT_FRAME
1588 #ifndef CONFIG_XEN
1589         cmpl $ia32_sysenter_target,(%esp)
1590         jne debug_stack_correct
1591         FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
1592 debug_stack_correct:
1593 #endif /* !CONFIG_XEN */
1594         pushl_cfi $-1                   # mark this as an int
1595         SAVE_ALL
1596         TRACE_IRQS_OFF
1597         xorl %edx,%edx                  # error code 0
1598         movl %esp,%eax                  # pt_regs pointer
1599         call do_debug
1600         jmp ret_from_exception
1601         CFI_ENDPROC
1602 END(debug)
1603
1604 /*
1605  * NMI is doubly nasty. It can happen _while_ we're handling
1606  * a debug fault, and the debug fault hasn't yet been able to
1607  * clear up the stack. So we first check whether we got  an
1608  * NMI on the sysenter entry path, but after that we need to
1609  * check whether we got an NMI on the debug path where the debug
1610  * fault happened on the sysenter path.
1611  */
1612 ENTRY(nmi)
1613         RING0_INT_FRAME
1614         pushl_cfi %eax
1615 #ifndef CONFIG_XEN
1616         movl %ss, %eax
1617         cmpw $__ESPFIX_SS, %ax
1618         popl_cfi %eax
1619         je nmi_espfix_stack
1620         cmpl $ia32_sysenter_target,(%esp)
1621         je nmi_stack_fixup
1622         pushl_cfi %eax
1623         movl %esp,%eax
1624         /* Do not access memory above the end of our stack page,
1625          * it might not exist.
1626          */
1627         andl $(THREAD_SIZE-1),%eax
1628         cmpl $(THREAD_SIZE-20),%eax
1629         popl_cfi %eax
1630         jae nmi_stack_correct
1631         cmpl $ia32_sysenter_target,12(%esp)
1632         je nmi_debug_stack_check
1633 nmi_stack_correct:
1634         /* We have a RING0_INT_FRAME here */
1635         pushl_cfi %eax
1636         SAVE_ALL
1637         xorl %edx,%edx          # zero error code
1638         movl %esp,%eax          # pt_regs pointer
1639         call do_nmi
1640         jmp restore_all_notrace
1641         CFI_ENDPROC
1642
1643 nmi_stack_fixup:
1644         RING0_INT_FRAME
1645         FIX_STACK 12, nmi_stack_correct, 1
1646         jmp nmi_stack_correct
1647
1648 nmi_debug_stack_check:
1649         /* We have a RING0_INT_FRAME here */
1650         cmpw $__KERNEL_CS,16(%esp)
1651         jne nmi_stack_correct
1652         cmpl $debug,(%esp)
1653         jb nmi_stack_correct
1654         cmpl $debug_esp_fix_insn,(%esp)
1655         ja nmi_stack_correct
1656         FIX_STACK 24, nmi_stack_correct, 1
1657         jmp nmi_stack_correct
1658
1659 nmi_espfix_stack:
1660         /* We have a RING0_INT_FRAME here.
1661          *
1662          * create the pointer to lss back
1663          */
1664         pushl_cfi %ss
1665         pushl_cfi %esp
1666         addl $4, (%esp)
1667         /* copy the iret frame of 12 bytes */
1668         .rept 3
1669         pushl_cfi 16(%esp)
1670         .endr
1671         pushl_cfi %eax
1672         SAVE_ALL
1673         FIXUP_ESPFIX_STACK              # %eax == %esp
1674         xorl %edx,%edx                  # zero error code
1675         call do_nmi
1676         RESTORE_REGS
1677         lss 12+4(%esp), %esp            # back to espfix stack
1678         CFI_ADJUST_CFA_OFFSET -24
1679         jmp irq_return
1680 #else
1681         SAVE_ALL
1682         xorl %edx,%edx          # zero error code
1683         movl %esp,%eax          # pt_regs pointer
1684         call do_nmi
1685         orl  $NMI_MASK, PT_EFLAGS(%esp)
1686         jmp restore_all
1687 #endif
1688         CFI_ENDPROC
1689 END(nmi)
1690
1691 ENTRY(int3)
1692         RING0_INT_FRAME
1693         pushl_cfi $-1                   # mark this as an int
1694         SAVE_ALL
1695         TRACE_IRQS_OFF
1696         xorl %edx,%edx          # zero error code
1697         movl %esp,%eax          # pt_regs pointer
1698         call do_int3
1699         jmp ret_from_exception
1700         CFI_ENDPROC
1701 END(int3)
1702
1703 ENTRY(general_protection)
1704         RING0_EC_FRAME
1705         pushl_cfi $do_general_protection
1706         jmp error_code
1707         CFI_ENDPROC
1708 END(general_protection)
1709
1710 #ifdef CONFIG_KVM_GUEST
1711 ENTRY(async_page_fault)
1712         RING0_EC_FRAME
1713         pushl_cfi $do_async_page_fault
1714         jmp error_code
1715         CFI_ENDPROC
1716 END(async_page_fault)
1717 #endif
1718
1719 /*
1720  * End of kprobes section
1721  */
1722         .popsection