Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[firefly-linux-kernel-4.4.55.git] / arch / x86 / kernel / entry_32.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 <asm/asm.h>
60 #include <asm/smap.h>
61
62 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this.  */
63 #include <linux/elf-em.h>
64 #define AUDIT_ARCH_I386         (EM_386|__AUDIT_ARCH_LE)
65 #define __AUDIT_ARCH_LE    0x40000000
66
67 #ifndef CONFIG_AUDITSYSCALL
68 #define sysenter_audit  syscall_trace_entry
69 #define sysexit_audit   syscall_exit_work
70 #endif
71
72         .section .entry.text, "ax"
73
74 /*
75  * We use macros for low-level operations which need to be overridden
76  * for paravirtualization.  The following will never clobber any registers:
77  *   INTERRUPT_RETURN (aka. "iret")
78  *   GET_CR0_INTO_EAX (aka. "movl %cr0, %eax")
79  *   ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
80  *
81  * For DISABLE_INTERRUPTS/ENABLE_INTERRUPTS (aka "cli"/"sti"), you must
82  * specify what registers can be overwritten (CLBR_NONE, CLBR_EAX/EDX/ECX/ANY).
83  * Allowing a register to be clobbered can shrink the paravirt replacement
84  * enough to patch inline, increasing performance.
85  */
86
87 #ifdef CONFIG_PREEMPT
88 #define preempt_stop(clobbers)  DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
89 #else
90 #define preempt_stop(clobbers)
91 #define resume_kernel           restore_all
92 #endif
93
94 .macro TRACE_IRQS_IRET
95 #ifdef CONFIG_TRACE_IRQFLAGS
96         testl $X86_EFLAGS_IF,PT_EFLAGS(%esp)     # interrupts off?
97         jz 1f
98         TRACE_IRQS_ON
99 1:
100 #endif
101 .endm
102
103 /*
104  * User gs save/restore
105  *
106  * %gs is used for userland TLS and kernel only uses it for stack
107  * canary which is required to be at %gs:20 by gcc.  Read the comment
108  * at the top of stackprotector.h for more info.
109  *
110  * Local labels 98 and 99 are used.
111  */
112 #ifdef CONFIG_X86_32_LAZY_GS
113
114  /* unfortunately push/pop can't be no-op */
115 .macro PUSH_GS
116         pushl_cfi $0
117 .endm
118 .macro POP_GS pop=0
119         addl $(4 + \pop), %esp
120         CFI_ADJUST_CFA_OFFSET -(4 + \pop)
121 .endm
122 .macro POP_GS_EX
123 .endm
124
125  /* all the rest are no-op */
126 .macro PTGS_TO_GS
127 .endm
128 .macro PTGS_TO_GS_EX
129 .endm
130 .macro GS_TO_REG reg
131 .endm
132 .macro REG_TO_PTGS reg
133 .endm
134 .macro SET_KERNEL_GS reg
135 .endm
136
137 #else   /* CONFIG_X86_32_LAZY_GS */
138
139 .macro PUSH_GS
140         pushl_cfi %gs
141         /*CFI_REL_OFFSET gs, 0*/
142 .endm
143
144 .macro POP_GS pop=0
145 98:     popl_cfi %gs
146         /*CFI_RESTORE gs*/
147   .if \pop <> 0
148         add $\pop, %esp
149         CFI_ADJUST_CFA_OFFSET -\pop
150   .endif
151 .endm
152 .macro POP_GS_EX
153 .pushsection .fixup, "ax"
154 99:     movl $0, (%esp)
155         jmp 98b
156 .popsection
157         _ASM_EXTABLE(98b,99b)
158 .endm
159
160 .macro PTGS_TO_GS
161 98:     mov PT_GS(%esp), %gs
162 .endm
163 .macro PTGS_TO_GS_EX
164 .pushsection .fixup, "ax"
165 99:     movl $0, PT_GS(%esp)
166         jmp 98b
167 .popsection
168         _ASM_EXTABLE(98b,99b)
169 .endm
170
171 .macro GS_TO_REG reg
172         movl %gs, \reg
173         /*CFI_REGISTER gs, \reg*/
174 .endm
175 .macro REG_TO_PTGS reg
176         movl \reg, PT_GS(%esp)
177         /*CFI_REL_OFFSET gs, PT_GS*/
178 .endm
179 .macro SET_KERNEL_GS reg
180         movl $(__KERNEL_STACK_CANARY), \reg
181         movl \reg, %gs
182 .endm
183
184 #endif  /* CONFIG_X86_32_LAZY_GS */
185
186 .macro SAVE_ALL
187         cld
188         PUSH_GS
189         pushl_cfi %fs
190         /*CFI_REL_OFFSET fs, 0;*/
191         pushl_cfi %es
192         /*CFI_REL_OFFSET es, 0;*/
193         pushl_cfi %ds
194         /*CFI_REL_OFFSET ds, 0;*/
195         pushl_cfi %eax
196         CFI_REL_OFFSET eax, 0
197         pushl_cfi %ebp
198         CFI_REL_OFFSET ebp, 0
199         pushl_cfi %edi
200         CFI_REL_OFFSET edi, 0
201         pushl_cfi %esi
202         CFI_REL_OFFSET esi, 0
203         pushl_cfi %edx
204         CFI_REL_OFFSET edx, 0
205         pushl_cfi %ecx
206         CFI_REL_OFFSET ecx, 0
207         pushl_cfi %ebx
208         CFI_REL_OFFSET ebx, 0
209         movl $(__USER_DS), %edx
210         movl %edx, %ds
211         movl %edx, %es
212         movl $(__KERNEL_PERCPU), %edx
213         movl %edx, %fs
214         SET_KERNEL_GS %edx
215 .endm
216
217 .macro RESTORE_INT_REGS
218         popl_cfi %ebx
219         CFI_RESTORE ebx
220         popl_cfi %ecx
221         CFI_RESTORE ecx
222         popl_cfi %edx
223         CFI_RESTORE edx
224         popl_cfi %esi
225         CFI_RESTORE esi
226         popl_cfi %edi
227         CFI_RESTORE edi
228         popl_cfi %ebp
229         CFI_RESTORE ebp
230         popl_cfi %eax
231         CFI_RESTORE eax
232 .endm
233
234 .macro RESTORE_REGS pop=0
235         RESTORE_INT_REGS
236 1:      popl_cfi %ds
237         /*CFI_RESTORE ds;*/
238 2:      popl_cfi %es
239         /*CFI_RESTORE es;*/
240 3:      popl_cfi %fs
241         /*CFI_RESTORE fs;*/
242         POP_GS \pop
243 .pushsection .fixup, "ax"
244 4:      movl $0, (%esp)
245         jmp 1b
246 5:      movl $0, (%esp)
247         jmp 2b
248 6:      movl $0, (%esp)
249         jmp 3b
250 .popsection
251         _ASM_EXTABLE(1b,4b)
252         _ASM_EXTABLE(2b,5b)
253         _ASM_EXTABLE(3b,6b)
254         POP_GS_EX
255 .endm
256
257 .macro RING0_INT_FRAME
258         CFI_STARTPROC simple
259         CFI_SIGNAL_FRAME
260         CFI_DEF_CFA esp, 3*4
261         /*CFI_OFFSET cs, -2*4;*/
262         CFI_OFFSET eip, -3*4
263 .endm
264
265 .macro RING0_EC_FRAME
266         CFI_STARTPROC simple
267         CFI_SIGNAL_FRAME
268         CFI_DEF_CFA esp, 4*4
269         /*CFI_OFFSET cs, -2*4;*/
270         CFI_OFFSET eip, -3*4
271 .endm
272
273 .macro RING0_PTREGS_FRAME
274         CFI_STARTPROC simple
275         CFI_SIGNAL_FRAME
276         CFI_DEF_CFA esp, PT_OLDESP-PT_EBX
277         /*CFI_OFFSET cs, PT_CS-PT_OLDESP;*/
278         CFI_OFFSET eip, PT_EIP-PT_OLDESP
279         /*CFI_OFFSET es, PT_ES-PT_OLDESP;*/
280         /*CFI_OFFSET ds, PT_DS-PT_OLDESP;*/
281         CFI_OFFSET eax, PT_EAX-PT_OLDESP
282         CFI_OFFSET ebp, PT_EBP-PT_OLDESP
283         CFI_OFFSET edi, PT_EDI-PT_OLDESP
284         CFI_OFFSET esi, PT_ESI-PT_OLDESP
285         CFI_OFFSET edx, PT_EDX-PT_OLDESP
286         CFI_OFFSET ecx, PT_ECX-PT_OLDESP
287         CFI_OFFSET ebx, PT_EBX-PT_OLDESP
288 .endm
289
290 ENTRY(ret_from_fork)
291         CFI_STARTPROC
292         pushl_cfi %eax
293         call schedule_tail
294         GET_THREAD_INFO(%ebp)
295         popl_cfi %eax
296         pushl_cfi $0x0202               # Reset kernel eflags
297         popfl_cfi
298         jmp syscall_exit
299         CFI_ENDPROC
300 END(ret_from_fork)
301
302 ENTRY(ret_from_kernel_execve)
303         movl %eax, %esp
304         movl $0,PT_EAX(%esp)
305         GET_THREAD_INFO(%ebp)
306         jmp syscall_exit
307 END(ret_from_kernel_execve)
308
309 /*
310  * Interrupt exit functions should be protected against kprobes
311  */
312         .pushsection .kprobes.text, "ax"
313 /*
314  * Return to user mode is not as complex as all this looks,
315  * but we want the default path for a system call return to
316  * go as quickly as possible which is why some of this is
317  * less clear than it otherwise should be.
318  */
319
320         # userspace resumption stub bypassing syscall exit tracing
321         ALIGN
322         RING0_PTREGS_FRAME
323 ret_from_exception:
324         preempt_stop(CLBR_ANY)
325 ret_from_intr:
326         GET_THREAD_INFO(%ebp)
327 #ifdef CONFIG_VM86
328         movl PT_EFLAGS(%esp), %eax      # mix EFLAGS and CS
329         movb PT_CS(%esp), %al
330         andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
331 #else
332         /*
333          * We can be coming here from child spawned by kernel_thread().
334          */
335         movl PT_CS(%esp), %eax
336         andl $SEGMENT_RPL_MASK, %eax
337 #endif
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 TSS_sysenter_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         ASM_STAC
418 1:      movl (%ebp),%ebp
419         ASM_CLAC
420         movl %ebp,PT_EBP(%esp)
421         _ASM_EXTABLE(1b,syscall_fault)
422
423         GET_THREAD_INFO(%ebp)
424
425         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
426         jnz sysenter_audit
427 sysenter_do_call:
428         cmpl $(NR_syscalls), %eax
429         jae syscall_badsys
430         call *sys_call_table(,%eax,4)
431         movl %eax,PT_EAX(%esp)
432         LOCKDEP_SYS_EXIT
433         DISABLE_INTERRUPTS(CLBR_ANY)
434         TRACE_IRQS_OFF
435         movl TI_flags(%ebp), %ecx
436         testl $_TIF_ALLWORK_MASK, %ecx
437         jne sysexit_audit
438 sysenter_exit:
439 /* if something modifies registers it must also disable sysexit */
440         movl PT_EIP(%esp), %edx
441         movl PT_OLDESP(%esp), %ecx
442         xorl %ebp,%ebp
443         TRACE_IRQS_ON
444 1:      mov  PT_FS(%esp), %fs
445         PTGS_TO_GS
446         ENABLE_INTERRUPTS_SYSEXIT
447
448 #ifdef CONFIG_AUDITSYSCALL
449 sysenter_audit:
450         testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
451         jnz syscall_trace_entry
452         addl $4,%esp
453         CFI_ADJUST_CFA_OFFSET -4
454         /* %esi already in 8(%esp)         6th arg: 4th syscall arg */
455         /* %edx already in 4(%esp)         5th arg: 3rd syscall arg */
456         /* %ecx already in 0(%esp)         4th arg: 2nd syscall arg */
457         movl %ebx,%ecx                  /* 3rd arg: 1st syscall arg */
458         movl %eax,%edx                  /* 2nd arg: syscall number */
459         movl $AUDIT_ARCH_I386,%eax      /* 1st arg: audit arch */
460         call __audit_syscall_entry
461         pushl_cfi %ebx
462         movl PT_EAX(%esp),%eax          /* reload syscall number */
463         jmp sysenter_do_call
464
465 sysexit_audit:
466         testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
467         jne syscall_exit_work
468         TRACE_IRQS_ON
469         ENABLE_INTERRUPTS(CLBR_ANY)
470         movl %eax,%edx          /* second arg, syscall return value */
471         cmpl $-MAX_ERRNO,%eax   /* is it an error ? */
472         setbe %al               /* 1 if so, 0 if not */
473         movzbl %al,%eax         /* zero-extend that */
474         call __audit_syscall_exit
475         DISABLE_INTERRUPTS(CLBR_ANY)
476         TRACE_IRQS_OFF
477         movl TI_flags(%ebp), %ecx
478         testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
479         jne syscall_exit_work
480         movl PT_EAX(%esp),%eax  /* reload syscall return value */
481         jmp sysenter_exit
482 #endif
483
484         CFI_ENDPROC
485 .pushsection .fixup,"ax"
486 2:      movl $0,PT_FS(%esp)
487         jmp 1b
488 .popsection
489         _ASM_EXTABLE(1b,2b)
490         PTGS_TO_GS_EX
491 ENDPROC(ia32_sysenter_target)
492
493 /*
494  * syscall stub including irq exit should be protected against kprobes
495  */
496         .pushsection .kprobes.text, "ax"
497         # system call handler stub
498 ENTRY(system_call)
499         RING0_INT_FRAME                 # can't unwind into user space anyway
500         ASM_CLAC
501         pushl_cfi %eax                  # save orig_eax
502         SAVE_ALL
503         GET_THREAD_INFO(%ebp)
504                                         # system call tracing in operation / emulation
505         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
506         jnz syscall_trace_entry
507         cmpl $(NR_syscalls), %eax
508         jae syscall_badsys
509 syscall_call:
510         call *sys_call_table(,%eax,4)
511         movl %eax,PT_EAX(%esp)          # store the return value
512 syscall_exit:
513         LOCKDEP_SYS_EXIT
514         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
515                                         # setting need_resched or sigpending
516                                         # between sampling and the iret
517         TRACE_IRQS_OFF
518         movl TI_flags(%ebp), %ecx
519         testl $_TIF_ALLWORK_MASK, %ecx  # current->work
520         jne syscall_exit_work
521
522 restore_all:
523         TRACE_IRQS_IRET
524 restore_all_notrace:
525         movl PT_EFLAGS(%esp), %eax      # mix EFLAGS, SS and CS
526         # Warning: PT_OLDSS(%esp) contains the wrong/random values if we
527         # are returning to the kernel.
528         # See comments in process.c:copy_thread() for details.
529         movb PT_OLDSS(%esp), %ah
530         movb PT_CS(%esp), %al
531         andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
532         cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
533         CFI_REMEMBER_STATE
534         je ldt_ss                       # returning to user-space with LDT SS
535 restore_nocheck:
536         RESTORE_REGS 4                  # skip orig_eax/error_code
537 irq_return:
538         INTERRUPT_RETURN
539 .section .fixup,"ax"
540 ENTRY(iret_exc)
541         pushl $0                        # no error code
542         pushl $do_iret_error
543         jmp error_code
544 .previous
545         _ASM_EXTABLE(irq_return,iret_exc)
546
547         CFI_RESTORE_STATE
548 ldt_ss:
549         larl PT_OLDSS(%esp), %eax
550         jnz restore_nocheck
551         testl $0x00400000, %eax         # returning to 32bit stack?
552         jnz restore_nocheck             # allright, normal return
553
554 #ifdef CONFIG_PARAVIRT
555         /*
556          * The kernel can't run on a non-flat stack if paravirt mode
557          * is active.  Rather than try to fixup the high bits of
558          * ESP, bypass this code entirely.  This may break DOSemu
559          * and/or Wine support in a paravirt VM, although the option
560          * is still available to implement the setting of the high
561          * 16-bits in the INTERRUPT_RETURN paravirt-op.
562          */
563         cmpl $0, pv_info+PARAVIRT_enabled
564         jne restore_nocheck
565 #endif
566
567 /*
568  * Setup and switch to ESPFIX stack
569  *
570  * We're returning to userspace with a 16 bit stack. The CPU will not
571  * restore the high word of ESP for us on executing iret... This is an
572  * "official" bug of all the x86-compatible CPUs, which we can work
573  * around to make dosemu and wine happy. We do this by preloading the
574  * high word of ESP with the high word of the userspace ESP while
575  * compensating for the offset by changing to the ESPFIX segment with
576  * a base address that matches for the difference.
577  */
578 #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
579         mov %esp, %edx                  /* load kernel esp */
580         mov PT_OLDESP(%esp), %eax       /* load userspace esp */
581         mov %dx, %ax                    /* eax: new kernel esp */
582         sub %eax, %edx                  /* offset (low word is 0) */
583         shr $16, %edx
584         mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
585         mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
586         pushl_cfi $__ESPFIX_SS
587         pushl_cfi %eax                  /* new kernel esp */
588         /* Disable interrupts, but do not irqtrace this section: we
589          * will soon execute iret and the tracer was already set to
590          * the irqstate after the iret */
591         DISABLE_INTERRUPTS(CLBR_EAX)
592         lss (%esp), %esp                /* switch to espfix segment */
593         CFI_ADJUST_CFA_OFFSET -8
594         jmp restore_nocheck
595         CFI_ENDPROC
596 ENDPROC(system_call)
597
598         # perform work that needs to be done immediately before resumption
599         ALIGN
600         RING0_PTREGS_FRAME              # can't unwind into user space anyway
601 work_pending:
602         testb $_TIF_NEED_RESCHED, %cl
603         jz work_notifysig
604 work_resched:
605         call schedule
606         LOCKDEP_SYS_EXIT
607         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
608                                         # setting need_resched or sigpending
609                                         # between sampling and the iret
610         TRACE_IRQS_OFF
611         movl TI_flags(%ebp), %ecx
612         andl $_TIF_WORK_MASK, %ecx      # is there any work to be done other
613                                         # than syscall tracing?
614         jz restore_all
615         testb $_TIF_NEED_RESCHED, %cl
616         jnz work_resched
617
618 work_notifysig:                         # deal with pending signals and
619                                         # notify-resume requests
620 #ifdef CONFIG_VM86
621         testl $X86_EFLAGS_VM, PT_EFLAGS(%esp)
622         movl %esp, %eax
623         jne work_notifysig_v86          # returning to kernel-space or
624                                         # vm86-space
625 1:
626 #else
627         movl %esp, %eax
628 #endif
629         TRACE_IRQS_ON
630         ENABLE_INTERRUPTS(CLBR_NONE)
631         movb PT_CS(%esp), %bl
632         andb $SEGMENT_RPL_MASK, %bl
633         cmpb $USER_RPL, %bl
634         jb resume_kernel
635         xorl %edx, %edx
636         call do_notify_resume
637         jmp resume_userspace
638
639 #ifdef CONFIG_VM86
640         ALIGN
641 work_notifysig_v86:
642         pushl_cfi %ecx                  # save ti_flags for do_notify_resume
643         call save_v86_state             # %eax contains pt_regs pointer
644         popl_cfi %ecx
645         movl %eax, %esp
646         jmp 1b
647 #endif
648 END(work_pending)
649
650         # perform syscall exit tracing
651         ALIGN
652 syscall_trace_entry:
653         movl $-ENOSYS,PT_EAX(%esp)
654         movl %esp, %eax
655         call syscall_trace_enter
656         /* What it returned is what we'll actually use.  */
657         cmpl $(NR_syscalls), %eax
658         jnae syscall_call
659         jmp syscall_exit
660 END(syscall_trace_entry)
661
662         # perform syscall exit tracing
663         ALIGN
664 syscall_exit_work:
665         testl $_TIF_WORK_SYSCALL_EXIT, %ecx
666         jz work_pending
667         TRACE_IRQS_ON
668         ENABLE_INTERRUPTS(CLBR_ANY)     # could let syscall_trace_leave() call
669                                         # schedule() instead
670         movl %esp, %eax
671         call syscall_trace_leave
672         jmp resume_userspace
673 END(syscall_exit_work)
674         CFI_ENDPROC
675
676         RING0_INT_FRAME                 # can't unwind into user space anyway
677 syscall_fault:
678         ASM_CLAC
679         GET_THREAD_INFO(%ebp)
680         movl $-EFAULT,PT_EAX(%esp)
681         jmp resume_userspace
682 END(syscall_fault)
683
684 syscall_badsys:
685         movl $-ENOSYS,PT_EAX(%esp)
686         jmp resume_userspace
687 END(syscall_badsys)
688         CFI_ENDPROC
689 /*
690  * End of kprobes section
691  */
692         .popsection
693
694 /*
695  * System calls that need a pt_regs pointer.
696  */
697 #define PTREGSCALL0(name) \
698 ENTRY(ptregs_##name) ;  \
699         leal 4(%esp),%eax; \
700         jmp sys_##name; \
701 ENDPROC(ptregs_##name)
702
703 #define PTREGSCALL1(name) \
704 ENTRY(ptregs_##name) ; \
705         leal 4(%esp),%edx; \
706         movl (PT_EBX+4)(%esp),%eax; \
707         jmp sys_##name; \
708 ENDPROC(ptregs_##name)
709
710 #define PTREGSCALL2(name) \
711 ENTRY(ptregs_##name) ; \
712         leal 4(%esp),%ecx; \
713         movl (PT_ECX+4)(%esp),%edx; \
714         movl (PT_EBX+4)(%esp),%eax; \
715         jmp sys_##name; \
716 ENDPROC(ptregs_##name)
717
718 #define PTREGSCALL3(name) \
719 ENTRY(ptregs_##name) ; \
720         CFI_STARTPROC; \
721         leal 4(%esp),%eax; \
722         pushl_cfi %eax; \
723         movl PT_EDX(%eax),%ecx; \
724         movl PT_ECX(%eax),%edx; \
725         movl PT_EBX(%eax),%eax; \
726         call sys_##name; \
727         addl $4,%esp; \
728         CFI_ADJUST_CFA_OFFSET -4; \
729         ret; \
730         CFI_ENDPROC; \
731 ENDPROC(ptregs_##name)
732
733 PTREGSCALL1(iopl)
734 PTREGSCALL0(fork)
735 PTREGSCALL0(vfork)
736 PTREGSCALL2(sigaltstack)
737 PTREGSCALL0(sigreturn)
738 PTREGSCALL0(rt_sigreturn)
739 PTREGSCALL2(vm86)
740 PTREGSCALL1(vm86old)
741
742 /* Clone is an oddball.  The 4th arg is in %edi */
743 ENTRY(ptregs_clone)
744         CFI_STARTPROC
745         leal 4(%esp),%eax
746         pushl_cfi %eax
747         pushl_cfi PT_EDI(%eax)
748         movl PT_EDX(%eax),%ecx
749         movl PT_ECX(%eax),%edx
750         movl PT_EBX(%eax),%eax
751         call sys_clone
752         addl $8,%esp
753         CFI_ADJUST_CFA_OFFSET -8
754         ret
755         CFI_ENDPROC
756 ENDPROC(ptregs_clone)
757
758 .macro FIXUP_ESPFIX_STACK
759 /*
760  * Switch back for ESPFIX stack to the normal zerobased stack
761  *
762  * We can't call C functions using the ESPFIX stack. This code reads
763  * the high word of the segment base from the GDT and swiches to the
764  * normal stack and adjusts ESP with the matching offset.
765  */
766         /* fixup the stack */
767         mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
768         mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
769         shl $16, %eax
770         addl %esp, %eax                 /* the adjusted stack pointer */
771         pushl_cfi $__KERNEL_DS
772         pushl_cfi %eax
773         lss (%esp), %esp                /* switch to the normal stack segment */
774         CFI_ADJUST_CFA_OFFSET -8
775 .endm
776 .macro UNWIND_ESPFIX_STACK
777         movl %ss, %eax
778         /* see if on espfix stack */
779         cmpw $__ESPFIX_SS, %ax
780         jne 27f
781         movl $__KERNEL_DS, %eax
782         movl %eax, %ds
783         movl %eax, %es
784         /* switch to normal stack */
785         FIXUP_ESPFIX_STACK
786 27:
787 .endm
788
789 /*
790  * Build the entry stubs and pointer table with some assembler magic.
791  * We pack 7 stubs into a single 32-byte chunk, which will fit in a
792  * single cache line on all modern x86 implementations.
793  */
794 .section .init.rodata,"a"
795 ENTRY(interrupt)
796 .section .entry.text, "ax"
797         .p2align 5
798         .p2align CONFIG_X86_L1_CACHE_SHIFT
799 ENTRY(irq_entries_start)
800         RING0_INT_FRAME
801 vector=FIRST_EXTERNAL_VECTOR
802 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
803         .balign 32
804   .rept 7
805     .if vector < NR_VECTORS
806       .if vector <> FIRST_EXTERNAL_VECTOR
807         CFI_ADJUST_CFA_OFFSET -4
808       .endif
809 1:      pushl_cfi $(~vector+0x80)       /* Note: always in signed byte range */
810       .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
811         jmp 2f
812       .endif
813       .previous
814         .long 1b
815       .section .entry.text, "ax"
816 vector=vector+1
817     .endif
818   .endr
819 2:      jmp common_interrupt
820 .endr
821 END(irq_entries_start)
822
823 .previous
824 END(interrupt)
825 .previous
826
827 /*
828  * the CPU automatically disables interrupts when executing an IRQ vector,
829  * so IRQ-flags tracing has to follow that:
830  */
831         .p2align CONFIG_X86_L1_CACHE_SHIFT
832 common_interrupt:
833         ASM_CLAC
834         addl $-0x80,(%esp)      /* Adjust vector into the [-256,-1] range */
835         SAVE_ALL
836         TRACE_IRQS_OFF
837         movl %esp,%eax
838         call do_IRQ
839         jmp ret_from_intr
840 ENDPROC(common_interrupt)
841         CFI_ENDPROC
842
843 /*
844  *  Irq entries should be protected against kprobes
845  */
846         .pushsection .kprobes.text, "ax"
847 #define BUILD_INTERRUPT3(name, nr, fn)  \
848 ENTRY(name)                             \
849         RING0_INT_FRAME;                \
850         ASM_CLAC;                       \
851         pushl_cfi $~(nr);               \
852         SAVE_ALL;                       \
853         TRACE_IRQS_OFF                  \
854         movl %esp,%eax;                 \
855         call fn;                        \
856         jmp ret_from_intr;              \
857         CFI_ENDPROC;                    \
858 ENDPROC(name)
859
860 #define BUILD_INTERRUPT(name, nr)       BUILD_INTERRUPT3(name, nr, smp_##name)
861
862 /* The include is where all of the SMP etc. interrupts come from */
863 #include <asm/entry_arch.h>
864
865 ENTRY(coprocessor_error)
866         RING0_INT_FRAME
867         ASM_CLAC
868         pushl_cfi $0
869         pushl_cfi $do_coprocessor_error
870         jmp error_code
871         CFI_ENDPROC
872 END(coprocessor_error)
873
874 ENTRY(simd_coprocessor_error)
875         RING0_INT_FRAME
876         ASM_CLAC
877         pushl_cfi $0
878 #ifdef CONFIG_X86_INVD_BUG
879         /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
880 661:    pushl_cfi $do_general_protection
881 662:
882 .section .altinstructions,"a"
883         altinstruction_entry 661b, 663f, X86_FEATURE_XMM, 662b-661b, 664f-663f
884 .previous
885 .section .altinstr_replacement,"ax"
886 663:    pushl $do_simd_coprocessor_error
887 664:
888 .previous
889 #else
890         pushl_cfi $do_simd_coprocessor_error
891 #endif
892         jmp error_code
893         CFI_ENDPROC
894 END(simd_coprocessor_error)
895
896 ENTRY(device_not_available)
897         RING0_INT_FRAME
898         ASM_CLAC
899         pushl_cfi $-1                   # mark this as an int
900         pushl_cfi $do_device_not_available
901         jmp error_code
902         CFI_ENDPROC
903 END(device_not_available)
904
905 #ifdef CONFIG_PARAVIRT
906 ENTRY(native_iret)
907         iret
908         _ASM_EXTABLE(native_iret, iret_exc)
909 END(native_iret)
910
911 ENTRY(native_irq_enable_sysexit)
912         sti
913         sysexit
914 END(native_irq_enable_sysexit)
915 #endif
916
917 ENTRY(overflow)
918         RING0_INT_FRAME
919         ASM_CLAC
920         pushl_cfi $0
921         pushl_cfi $do_overflow
922         jmp error_code
923         CFI_ENDPROC
924 END(overflow)
925
926 ENTRY(bounds)
927         RING0_INT_FRAME
928         ASM_CLAC
929         pushl_cfi $0
930         pushl_cfi $do_bounds
931         jmp error_code
932         CFI_ENDPROC
933 END(bounds)
934
935 ENTRY(invalid_op)
936         RING0_INT_FRAME
937         ASM_CLAC
938         pushl_cfi $0
939         pushl_cfi $do_invalid_op
940         jmp error_code
941         CFI_ENDPROC
942 END(invalid_op)
943
944 ENTRY(coprocessor_segment_overrun)
945         RING0_INT_FRAME
946         ASM_CLAC
947         pushl_cfi $0
948         pushl_cfi $do_coprocessor_segment_overrun
949         jmp error_code
950         CFI_ENDPROC
951 END(coprocessor_segment_overrun)
952
953 ENTRY(invalid_TSS)
954         RING0_EC_FRAME
955         ASM_CLAC
956         pushl_cfi $do_invalid_TSS
957         jmp error_code
958         CFI_ENDPROC
959 END(invalid_TSS)
960
961 ENTRY(segment_not_present)
962         RING0_EC_FRAME
963         ASM_CLAC
964         pushl_cfi $do_segment_not_present
965         jmp error_code
966         CFI_ENDPROC
967 END(segment_not_present)
968
969 ENTRY(stack_segment)
970         RING0_EC_FRAME
971         ASM_CLAC
972         pushl_cfi $do_stack_segment
973         jmp error_code
974         CFI_ENDPROC
975 END(stack_segment)
976
977 ENTRY(alignment_check)
978         RING0_EC_FRAME
979         ASM_CLAC
980         pushl_cfi $do_alignment_check
981         jmp error_code
982         CFI_ENDPROC
983 END(alignment_check)
984
985 ENTRY(divide_error)
986         RING0_INT_FRAME
987         ASM_CLAC
988         pushl_cfi $0                    # no error code
989         pushl_cfi $do_divide_error
990         jmp error_code
991         CFI_ENDPROC
992 END(divide_error)
993
994 #ifdef CONFIG_X86_MCE
995 ENTRY(machine_check)
996         RING0_INT_FRAME
997         ASM_CLAC
998         pushl_cfi $0
999         pushl_cfi machine_check_vector
1000         jmp error_code
1001         CFI_ENDPROC
1002 END(machine_check)
1003 #endif
1004
1005 ENTRY(spurious_interrupt_bug)
1006         RING0_INT_FRAME
1007         ASM_CLAC
1008         pushl_cfi $0
1009         pushl_cfi $do_spurious_interrupt_bug
1010         jmp error_code
1011         CFI_ENDPROC
1012 END(spurious_interrupt_bug)
1013 /*
1014  * End of kprobes section
1015  */
1016         .popsection
1017
1018 ENTRY(ret_from_kernel_thread)
1019         CFI_STARTPROC
1020         pushl_cfi %eax
1021         call schedule_tail
1022         GET_THREAD_INFO(%ebp)
1023         popl_cfi %eax
1024         pushl_cfi $0x0202               # Reset kernel eflags
1025         popfl_cfi
1026         movl PT_EBP(%esp),%eax
1027         call *PT_EBX(%esp)
1028         call do_exit
1029         ud2                     # padding for call trace
1030         CFI_ENDPROC
1031 ENDPROC(ret_from_kernel_thread)
1032
1033 #ifdef CONFIG_XEN
1034 /* Xen doesn't set %esp to be precisely what the normal sysenter
1035    entrypoint expects, so fix it up before using the normal path. */
1036 ENTRY(xen_sysenter_target)
1037         RING0_INT_FRAME
1038         addl $5*4, %esp         /* remove xen-provided frame */
1039         CFI_ADJUST_CFA_OFFSET -5*4
1040         jmp sysenter_past_esp
1041         CFI_ENDPROC
1042
1043 ENTRY(xen_hypervisor_callback)
1044         CFI_STARTPROC
1045         pushl_cfi $0
1046         SAVE_ALL
1047         TRACE_IRQS_OFF
1048
1049         /* Check to see if we got the event in the critical
1050            region in xen_iret_direct, after we've reenabled
1051            events and checked for pending events.  This simulates
1052            iret instruction's behaviour where it delivers a
1053            pending interrupt when enabling interrupts. */
1054         movl PT_EIP(%esp),%eax
1055         cmpl $xen_iret_start_crit,%eax
1056         jb   1f
1057         cmpl $xen_iret_end_crit,%eax
1058         jae  1f
1059
1060         jmp  xen_iret_crit_fixup
1061
1062 ENTRY(xen_do_upcall)
1063 1:      mov %esp, %eax
1064         call xen_evtchn_do_upcall
1065         jmp  ret_from_intr
1066         CFI_ENDPROC
1067 ENDPROC(xen_hypervisor_callback)
1068
1069 # Hypervisor uses this for application faults while it executes.
1070 # We get here for two reasons:
1071 #  1. Fault while reloading DS, ES, FS or GS
1072 #  2. Fault while executing IRET
1073 # Category 1 we fix up by reattempting the load, and zeroing the segment
1074 # register if the load fails.
1075 # Category 2 we fix up by jumping to do_iret_error. We cannot use the
1076 # normal Linux return path in this case because if we use the IRET hypercall
1077 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1078 # We distinguish between categories by maintaining a status value in EAX.
1079 ENTRY(xen_failsafe_callback)
1080         CFI_STARTPROC
1081         pushl_cfi %eax
1082         movl $1,%eax
1083 1:      mov 4(%esp),%ds
1084 2:      mov 8(%esp),%es
1085 3:      mov 12(%esp),%fs
1086 4:      mov 16(%esp),%gs
1087         testl %eax,%eax
1088         popl_cfi %eax
1089         lea 16(%esp),%esp
1090         CFI_ADJUST_CFA_OFFSET -16
1091         jz 5f
1092         addl $16,%esp
1093         jmp iret_exc            # EAX != 0 => Category 2 (Bad IRET)
1094 5:      pushl_cfi $0            # EAX == 0 => Category 1 (Bad segment)
1095         SAVE_ALL
1096         jmp ret_from_exception
1097         CFI_ENDPROC
1098
1099 .section .fixup,"ax"
1100 6:      xorl %eax,%eax
1101         movl %eax,4(%esp)
1102         jmp 1b
1103 7:      xorl %eax,%eax
1104         movl %eax,8(%esp)
1105         jmp 2b
1106 8:      xorl %eax,%eax
1107         movl %eax,12(%esp)
1108         jmp 3b
1109 9:      xorl %eax,%eax
1110         movl %eax,16(%esp)
1111         jmp 4b
1112 .previous
1113         _ASM_EXTABLE(1b,6b)
1114         _ASM_EXTABLE(2b,7b)
1115         _ASM_EXTABLE(3b,8b)
1116         _ASM_EXTABLE(4b,9b)
1117 ENDPROC(xen_failsafe_callback)
1118
1119 BUILD_INTERRUPT3(xen_hvm_callback_vector, XEN_HVM_EVTCHN_CALLBACK,
1120                 xen_evtchn_do_upcall)
1121
1122 #endif  /* CONFIG_XEN */
1123
1124 #ifdef CONFIG_FUNCTION_TRACER
1125 #ifdef CONFIG_DYNAMIC_FTRACE
1126
1127 ENTRY(mcount)
1128         ret
1129 END(mcount)
1130
1131 ENTRY(ftrace_caller)
1132         cmpl $0, function_trace_stop
1133         jne  ftrace_stub
1134
1135         pushl %eax
1136         pushl %ecx
1137         pushl %edx
1138         pushl $0        /* Pass NULL as regs pointer */
1139         movl 4*4(%esp), %eax
1140         movl 0x4(%ebp), %edx
1141         leal function_trace_op, %ecx
1142         subl $MCOUNT_INSN_SIZE, %eax
1143
1144 .globl ftrace_call
1145 ftrace_call:
1146         call ftrace_stub
1147
1148         addl $4,%esp    /* skip NULL pointer */
1149         popl %edx
1150         popl %ecx
1151         popl %eax
1152 ftrace_ret:
1153 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1154 .globl ftrace_graph_call
1155 ftrace_graph_call:
1156         jmp ftrace_stub
1157 #endif
1158
1159 .globl ftrace_stub
1160 ftrace_stub:
1161         ret
1162 END(ftrace_caller)
1163
1164 ENTRY(ftrace_regs_caller)
1165         pushf   /* push flags before compare (in cs location) */
1166         cmpl $0, function_trace_stop
1167         jne ftrace_restore_flags
1168
1169         /*
1170          * i386 does not save SS and ESP when coming from kernel.
1171          * Instead, to get sp, &regs->sp is used (see ptrace.h).
1172          * Unfortunately, that means eflags must be at the same location
1173          * as the current return ip is. We move the return ip into the
1174          * ip location, and move flags into the return ip location.
1175          */
1176         pushl 4(%esp)   /* save return ip into ip slot */
1177
1178         pushl $0        /* Load 0 into orig_ax */
1179         pushl %gs
1180         pushl %fs
1181         pushl %es
1182         pushl %ds
1183         pushl %eax
1184         pushl %ebp
1185         pushl %edi
1186         pushl %esi
1187         pushl %edx
1188         pushl %ecx
1189         pushl %ebx
1190
1191         movl 13*4(%esp), %eax   /* Get the saved flags */
1192         movl %eax, 14*4(%esp)   /* Move saved flags into regs->flags location */
1193                                 /* clobbering return ip */
1194         movl $__KERNEL_CS,13*4(%esp)
1195
1196         movl 12*4(%esp), %eax   /* Load ip (1st parameter) */
1197         subl $MCOUNT_INSN_SIZE, %eax    /* Adjust ip */
1198         movl 0x4(%ebp), %edx    /* Load parent ip (2nd parameter) */
1199         leal function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
1200         pushl %esp              /* Save pt_regs as 4th parameter */
1201
1202 GLOBAL(ftrace_regs_call)
1203         call ftrace_stub
1204
1205         addl $4, %esp           /* Skip pt_regs */
1206         movl 14*4(%esp), %eax   /* Move flags back into cs */
1207         movl %eax, 13*4(%esp)   /* Needed to keep addl from modifying flags */
1208         movl 12*4(%esp), %eax   /* Get return ip from regs->ip */
1209         movl %eax, 14*4(%esp)   /* Put return ip back for ret */
1210
1211         popl %ebx
1212         popl %ecx
1213         popl %edx
1214         popl %esi
1215         popl %edi
1216         popl %ebp
1217         popl %eax
1218         popl %ds
1219         popl %es
1220         popl %fs
1221         popl %gs
1222         addl $8, %esp           /* Skip orig_ax and ip */
1223         popf                    /* Pop flags at end (no addl to corrupt flags) */
1224         jmp ftrace_ret
1225
1226 ftrace_restore_flags:
1227         popf
1228         jmp  ftrace_stub
1229 #else /* ! CONFIG_DYNAMIC_FTRACE */
1230
1231 ENTRY(mcount)
1232         cmpl $0, function_trace_stop
1233         jne  ftrace_stub
1234
1235         cmpl $ftrace_stub, ftrace_trace_function
1236         jnz trace
1237 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1238         cmpl $ftrace_stub, ftrace_graph_return
1239         jnz ftrace_graph_caller
1240
1241         cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
1242         jnz ftrace_graph_caller
1243 #endif
1244 .globl ftrace_stub
1245 ftrace_stub:
1246         ret
1247
1248         /* taken from glibc */
1249 trace:
1250         pushl %eax
1251         pushl %ecx
1252         pushl %edx
1253         movl 0xc(%esp), %eax
1254         movl 0x4(%ebp), %edx
1255         subl $MCOUNT_INSN_SIZE, %eax
1256
1257         call *ftrace_trace_function
1258
1259         popl %edx
1260         popl %ecx
1261         popl %eax
1262         jmp ftrace_stub
1263 END(mcount)
1264 #endif /* CONFIG_DYNAMIC_FTRACE */
1265 #endif /* CONFIG_FUNCTION_TRACER */
1266
1267 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1268 ENTRY(ftrace_graph_caller)
1269         pushl %eax
1270         pushl %ecx
1271         pushl %edx
1272         movl 0xc(%esp), %edx
1273         lea 0x4(%ebp), %eax
1274         movl (%ebp), %ecx
1275         subl $MCOUNT_INSN_SIZE, %edx
1276         call prepare_ftrace_return
1277         popl %edx
1278         popl %ecx
1279         popl %eax
1280         ret
1281 END(ftrace_graph_caller)
1282
1283 .globl return_to_handler
1284 return_to_handler:
1285         pushl %eax
1286         pushl %edx
1287         movl %ebp, %eax
1288         call ftrace_return_to_handler
1289         movl %eax, %ecx
1290         popl %edx
1291         popl %eax
1292         jmp *%ecx
1293 #endif
1294
1295 /*
1296  * Some functions should be protected against kprobes
1297  */
1298         .pushsection .kprobes.text, "ax"
1299
1300 ENTRY(page_fault)
1301         RING0_EC_FRAME
1302         ASM_CLAC
1303         pushl_cfi $do_page_fault
1304         ALIGN
1305 error_code:
1306         /* the function address is in %gs's slot on the stack */
1307         pushl_cfi %fs
1308         /*CFI_REL_OFFSET fs, 0*/
1309         pushl_cfi %es
1310         /*CFI_REL_OFFSET es, 0*/
1311         pushl_cfi %ds
1312         /*CFI_REL_OFFSET ds, 0*/
1313         pushl_cfi %eax
1314         CFI_REL_OFFSET eax, 0
1315         pushl_cfi %ebp
1316         CFI_REL_OFFSET ebp, 0
1317         pushl_cfi %edi
1318         CFI_REL_OFFSET edi, 0
1319         pushl_cfi %esi
1320         CFI_REL_OFFSET esi, 0
1321         pushl_cfi %edx
1322         CFI_REL_OFFSET edx, 0
1323         pushl_cfi %ecx
1324         CFI_REL_OFFSET ecx, 0
1325         pushl_cfi %ebx
1326         CFI_REL_OFFSET ebx, 0
1327         cld
1328         movl $(__KERNEL_PERCPU), %ecx
1329         movl %ecx, %fs
1330         UNWIND_ESPFIX_STACK
1331         GS_TO_REG %ecx
1332         movl PT_GS(%esp), %edi          # get the function address
1333         movl PT_ORIG_EAX(%esp), %edx    # get the error code
1334         movl $-1, PT_ORIG_EAX(%esp)     # no syscall to restart
1335         REG_TO_PTGS %ecx
1336         SET_KERNEL_GS %ecx
1337         movl $(__USER_DS), %ecx
1338         movl %ecx, %ds
1339         movl %ecx, %es
1340         TRACE_IRQS_OFF
1341         movl %esp,%eax                  # pt_regs pointer
1342         call *%edi
1343         jmp ret_from_exception
1344         CFI_ENDPROC
1345 END(page_fault)
1346
1347 /*
1348  * Debug traps and NMI can happen at the one SYSENTER instruction
1349  * that sets up the real kernel stack. Check here, since we can't
1350  * allow the wrong stack to be used.
1351  *
1352  * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
1353  * already pushed 3 words if it hits on the sysenter instruction:
1354  * eflags, cs and eip.
1355  *
1356  * We just load the right stack, and push the three (known) values
1357  * by hand onto the new stack - while updating the return eip past
1358  * the instruction that would have done it for sysenter.
1359  */
1360 .macro FIX_STACK offset ok label
1361         cmpw $__KERNEL_CS, 4(%esp)
1362         jne \ok
1363 \label:
1364         movl TSS_sysenter_sp0 + \offset(%esp), %esp
1365         CFI_DEF_CFA esp, 0
1366         CFI_UNDEFINED eip
1367         pushfl_cfi
1368         pushl_cfi $__KERNEL_CS
1369         pushl_cfi $sysenter_past_esp
1370         CFI_REL_OFFSET eip, 0
1371 .endm
1372
1373 ENTRY(debug)
1374         RING0_INT_FRAME
1375         ASM_CLAC
1376         cmpl $ia32_sysenter_target,(%esp)
1377         jne debug_stack_correct
1378         FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
1379 debug_stack_correct:
1380         pushl_cfi $-1                   # mark this as an int
1381         SAVE_ALL
1382         TRACE_IRQS_OFF
1383         xorl %edx,%edx                  # error code 0
1384         movl %esp,%eax                  # pt_regs pointer
1385         call do_debug
1386         jmp ret_from_exception
1387         CFI_ENDPROC
1388 END(debug)
1389
1390 /*
1391  * NMI is doubly nasty. It can happen _while_ we're handling
1392  * a debug fault, and the debug fault hasn't yet been able to
1393  * clear up the stack. So we first check whether we got  an
1394  * NMI on the sysenter entry path, but after that we need to
1395  * check whether we got an NMI on the debug path where the debug
1396  * fault happened on the sysenter path.
1397  */
1398 ENTRY(nmi)
1399         RING0_INT_FRAME
1400         ASM_CLAC
1401         pushl_cfi %eax
1402         movl %ss, %eax
1403         cmpw $__ESPFIX_SS, %ax
1404         popl_cfi %eax
1405         je nmi_espfix_stack
1406         cmpl $ia32_sysenter_target,(%esp)
1407         je nmi_stack_fixup
1408         pushl_cfi %eax
1409         movl %esp,%eax
1410         /* Do not access memory above the end of our stack page,
1411          * it might not exist.
1412          */
1413         andl $(THREAD_SIZE-1),%eax
1414         cmpl $(THREAD_SIZE-20),%eax
1415         popl_cfi %eax
1416         jae nmi_stack_correct
1417         cmpl $ia32_sysenter_target,12(%esp)
1418         je nmi_debug_stack_check
1419 nmi_stack_correct:
1420         /* We have a RING0_INT_FRAME here */
1421         pushl_cfi %eax
1422         SAVE_ALL
1423         xorl %edx,%edx          # zero error code
1424         movl %esp,%eax          # pt_regs pointer
1425         call do_nmi
1426         jmp restore_all_notrace
1427         CFI_ENDPROC
1428
1429 nmi_stack_fixup:
1430         RING0_INT_FRAME
1431         FIX_STACK 12, nmi_stack_correct, 1
1432         jmp nmi_stack_correct
1433
1434 nmi_debug_stack_check:
1435         /* We have a RING0_INT_FRAME here */
1436         cmpw $__KERNEL_CS,16(%esp)
1437         jne nmi_stack_correct
1438         cmpl $debug,(%esp)
1439         jb nmi_stack_correct
1440         cmpl $debug_esp_fix_insn,(%esp)
1441         ja nmi_stack_correct
1442         FIX_STACK 24, nmi_stack_correct, 1
1443         jmp nmi_stack_correct
1444
1445 nmi_espfix_stack:
1446         /* We have a RING0_INT_FRAME here.
1447          *
1448          * create the pointer to lss back
1449          */
1450         pushl_cfi %ss
1451         pushl_cfi %esp
1452         addl $4, (%esp)
1453         /* copy the iret frame of 12 bytes */
1454         .rept 3
1455         pushl_cfi 16(%esp)
1456         .endr
1457         pushl_cfi %eax
1458         SAVE_ALL
1459         FIXUP_ESPFIX_STACK              # %eax == %esp
1460         xorl %edx,%edx                  # zero error code
1461         call do_nmi
1462         RESTORE_REGS
1463         lss 12+4(%esp), %esp            # back to espfix stack
1464         CFI_ADJUST_CFA_OFFSET -24
1465         jmp irq_return
1466         CFI_ENDPROC
1467 END(nmi)
1468
1469 ENTRY(int3)
1470         RING0_INT_FRAME
1471         ASM_CLAC
1472         pushl_cfi $-1                   # mark this as an int
1473         SAVE_ALL
1474         TRACE_IRQS_OFF
1475         xorl %edx,%edx          # zero error code
1476         movl %esp,%eax          # pt_regs pointer
1477         call do_int3
1478         jmp ret_from_exception
1479         CFI_ENDPROC
1480 END(int3)
1481
1482 ENTRY(general_protection)
1483         RING0_EC_FRAME
1484         pushl_cfi $do_general_protection
1485         jmp error_code
1486         CFI_ENDPROC
1487 END(general_protection)
1488
1489 #ifdef CONFIG_KVM_GUEST
1490 ENTRY(async_page_fault)
1491         RING0_EC_FRAME
1492         ASM_CLAC
1493         pushl_cfi $do_async_page_fault
1494         jmp error_code
1495         CFI_ENDPROC
1496 END(async_page_fault)
1497 #endif
1498
1499 /*
1500  * End of kprobes section
1501  */
1502         .popsection