]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/x86/kernel/entry_32.S
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
[karo-tx-linux.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         TRACE_IRQS_ON
626         ENABLE_INTERRUPTS(CLBR_NONE)
627         movb PT_CS(%esp), %bl
628         andb $SEGMENT_RPL_MASK, %bl
629         cmpb $USER_RPL, %bl
630         jb resume_kernel
631         xorl %edx, %edx
632         call do_notify_resume
633         jmp resume_userspace
634
635         ALIGN
636 work_notifysig_v86:
637         pushl_cfi %ecx                  # save ti_flags for do_notify_resume
638         call save_v86_state             # %eax contains pt_regs pointer
639         popl_cfi %ecx
640         movl %eax, %esp
641 #else
642         movl %esp, %eax
643 #endif
644         TRACE_IRQS_ON
645         ENABLE_INTERRUPTS(CLBR_NONE)
646         movb PT_CS(%esp), %bl
647         andb $SEGMENT_RPL_MASK, %bl
648         cmpb $USER_RPL, %bl
649         jb resume_kernel
650         xorl %edx, %edx
651         call do_notify_resume
652         jmp resume_userspace
653 END(work_pending)
654
655         # perform syscall exit tracing
656         ALIGN
657 syscall_trace_entry:
658         movl $-ENOSYS,PT_EAX(%esp)
659         movl %esp, %eax
660         call syscall_trace_enter
661         /* What it returned is what we'll actually use.  */
662         cmpl $(NR_syscalls), %eax
663         jnae syscall_call
664         jmp syscall_exit
665 END(syscall_trace_entry)
666
667         # perform syscall exit tracing
668         ALIGN
669 syscall_exit_work:
670         testl $_TIF_WORK_SYSCALL_EXIT, %ecx
671         jz work_pending
672         TRACE_IRQS_ON
673         ENABLE_INTERRUPTS(CLBR_ANY)     # could let syscall_trace_leave() call
674                                         # schedule() instead
675         movl %esp, %eax
676         call syscall_trace_leave
677         jmp resume_userspace
678 END(syscall_exit_work)
679         CFI_ENDPROC
680
681         RING0_INT_FRAME                 # can't unwind into user space anyway
682 syscall_fault:
683         ASM_CLAC
684         GET_THREAD_INFO(%ebp)
685         movl $-EFAULT,PT_EAX(%esp)
686         jmp resume_userspace
687 END(syscall_fault)
688
689 syscall_badsys:
690         movl $-ENOSYS,PT_EAX(%esp)
691         jmp resume_userspace
692 END(syscall_badsys)
693         CFI_ENDPROC
694 /*
695  * End of kprobes section
696  */
697         .popsection
698
699 /*
700  * System calls that need a pt_regs pointer.
701  */
702 #define PTREGSCALL0(name) \
703 ENTRY(ptregs_##name) ;  \
704         leal 4(%esp),%eax; \
705         jmp sys_##name; \
706 ENDPROC(ptregs_##name)
707
708 #define PTREGSCALL1(name) \
709 ENTRY(ptregs_##name) ; \
710         leal 4(%esp),%edx; \
711         movl (PT_EBX+4)(%esp),%eax; \
712         jmp sys_##name; \
713 ENDPROC(ptregs_##name)
714
715 #define PTREGSCALL2(name) \
716 ENTRY(ptregs_##name) ; \
717         leal 4(%esp),%ecx; \
718         movl (PT_ECX+4)(%esp),%edx; \
719         movl (PT_EBX+4)(%esp),%eax; \
720         jmp sys_##name; \
721 ENDPROC(ptregs_##name)
722
723 #define PTREGSCALL3(name) \
724 ENTRY(ptregs_##name) ; \
725         CFI_STARTPROC; \
726         leal 4(%esp),%eax; \
727         pushl_cfi %eax; \
728         movl PT_EDX(%eax),%ecx; \
729         movl PT_ECX(%eax),%edx; \
730         movl PT_EBX(%eax),%eax; \
731         call sys_##name; \
732         addl $4,%esp; \
733         CFI_ADJUST_CFA_OFFSET -4; \
734         ret; \
735         CFI_ENDPROC; \
736 ENDPROC(ptregs_##name)
737
738 PTREGSCALL1(iopl)
739 PTREGSCALL0(fork)
740 PTREGSCALL0(vfork)
741 PTREGSCALL2(sigaltstack)
742 PTREGSCALL0(sigreturn)
743 PTREGSCALL0(rt_sigreturn)
744 PTREGSCALL2(vm86)
745 PTREGSCALL1(vm86old)
746
747 /* Clone is an oddball.  The 4th arg is in %edi */
748 ENTRY(ptregs_clone)
749         CFI_STARTPROC
750         leal 4(%esp),%eax
751         pushl_cfi %eax
752         pushl_cfi PT_EDI(%eax)
753         movl PT_EDX(%eax),%ecx
754         movl PT_ECX(%eax),%edx
755         movl PT_EBX(%eax),%eax
756         call sys_clone
757         addl $8,%esp
758         CFI_ADJUST_CFA_OFFSET -8
759         ret
760         CFI_ENDPROC
761 ENDPROC(ptregs_clone)
762
763 .macro FIXUP_ESPFIX_STACK
764 /*
765  * Switch back for ESPFIX stack to the normal zerobased stack
766  *
767  * We can't call C functions using the ESPFIX stack. This code reads
768  * the high word of the segment base from the GDT and swiches to the
769  * normal stack and adjusts ESP with the matching offset.
770  */
771         /* fixup the stack */
772         mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
773         mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
774         shl $16, %eax
775         addl %esp, %eax                 /* the adjusted stack pointer */
776         pushl_cfi $__KERNEL_DS
777         pushl_cfi %eax
778         lss (%esp), %esp                /* switch to the normal stack segment */
779         CFI_ADJUST_CFA_OFFSET -8
780 .endm
781 .macro UNWIND_ESPFIX_STACK
782         movl %ss, %eax
783         /* see if on espfix stack */
784         cmpw $__ESPFIX_SS, %ax
785         jne 27f
786         movl $__KERNEL_DS, %eax
787         movl %eax, %ds
788         movl %eax, %es
789         /* switch to normal stack */
790         FIXUP_ESPFIX_STACK
791 27:
792 .endm
793
794 /*
795  * Build the entry stubs and pointer table with some assembler magic.
796  * We pack 7 stubs into a single 32-byte chunk, which will fit in a
797  * single cache line on all modern x86 implementations.
798  */
799 .section .init.rodata,"a"
800 ENTRY(interrupt)
801 .section .entry.text, "ax"
802         .p2align 5
803         .p2align CONFIG_X86_L1_CACHE_SHIFT
804 ENTRY(irq_entries_start)
805         RING0_INT_FRAME
806 vector=FIRST_EXTERNAL_VECTOR
807 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
808         .balign 32
809   .rept 7
810     .if vector < NR_VECTORS
811       .if vector <> FIRST_EXTERNAL_VECTOR
812         CFI_ADJUST_CFA_OFFSET -4
813       .endif
814 1:      pushl_cfi $(~vector+0x80)       /* Note: always in signed byte range */
815       .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
816         jmp 2f
817       .endif
818       .previous
819         .long 1b
820       .section .entry.text, "ax"
821 vector=vector+1
822     .endif
823   .endr
824 2:      jmp common_interrupt
825 .endr
826 END(irq_entries_start)
827
828 .previous
829 END(interrupt)
830 .previous
831
832 /*
833  * the CPU automatically disables interrupts when executing an IRQ vector,
834  * so IRQ-flags tracing has to follow that:
835  */
836         .p2align CONFIG_X86_L1_CACHE_SHIFT
837 common_interrupt:
838         ASM_CLAC
839         addl $-0x80,(%esp)      /* Adjust vector into the [-256,-1] range */
840         SAVE_ALL
841         TRACE_IRQS_OFF
842         movl %esp,%eax
843         call do_IRQ
844         jmp ret_from_intr
845 ENDPROC(common_interrupt)
846         CFI_ENDPROC
847
848 /*
849  *  Irq entries should be protected against kprobes
850  */
851         .pushsection .kprobes.text, "ax"
852 #define BUILD_INTERRUPT3(name, nr, fn)  \
853 ENTRY(name)                             \
854         RING0_INT_FRAME;                \
855         ASM_CLAC;                       \
856         pushl_cfi $~(nr);               \
857         SAVE_ALL;                       \
858         TRACE_IRQS_OFF                  \
859         movl %esp,%eax;                 \
860         call fn;                        \
861         jmp ret_from_intr;              \
862         CFI_ENDPROC;                    \
863 ENDPROC(name)
864
865 #define BUILD_INTERRUPT(name, nr)       BUILD_INTERRUPT3(name, nr, smp_##name)
866
867 /* The include is where all of the SMP etc. interrupts come from */
868 #include <asm/entry_arch.h>
869
870 ENTRY(coprocessor_error)
871         RING0_INT_FRAME
872         ASM_CLAC
873         pushl_cfi $0
874         pushl_cfi $do_coprocessor_error
875         jmp error_code
876         CFI_ENDPROC
877 END(coprocessor_error)
878
879 ENTRY(simd_coprocessor_error)
880         RING0_INT_FRAME
881         ASM_CLAC
882         pushl_cfi $0
883 #ifdef CONFIG_X86_INVD_BUG
884         /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
885 661:    pushl_cfi $do_general_protection
886 662:
887 .section .altinstructions,"a"
888         altinstruction_entry 661b, 663f, X86_FEATURE_XMM, 662b-661b, 664f-663f
889 .previous
890 .section .altinstr_replacement,"ax"
891 663:    pushl $do_simd_coprocessor_error
892 664:
893 .previous
894 #else
895         pushl_cfi $do_simd_coprocessor_error
896 #endif
897         jmp error_code
898         CFI_ENDPROC
899 END(simd_coprocessor_error)
900
901 ENTRY(device_not_available)
902         RING0_INT_FRAME
903         ASM_CLAC
904         pushl_cfi $-1                   # mark this as an int
905         pushl_cfi $do_device_not_available
906         jmp error_code
907         CFI_ENDPROC
908 END(device_not_available)
909
910 #ifdef CONFIG_PARAVIRT
911 ENTRY(native_iret)
912         iret
913         _ASM_EXTABLE(native_iret, iret_exc)
914 END(native_iret)
915
916 ENTRY(native_irq_enable_sysexit)
917         sti
918         sysexit
919 END(native_irq_enable_sysexit)
920 #endif
921
922 ENTRY(overflow)
923         RING0_INT_FRAME
924         ASM_CLAC
925         pushl_cfi $0
926         pushl_cfi $do_overflow
927         jmp error_code
928         CFI_ENDPROC
929 END(overflow)
930
931 ENTRY(bounds)
932         RING0_INT_FRAME
933         ASM_CLAC
934         pushl_cfi $0
935         pushl_cfi $do_bounds
936         jmp error_code
937         CFI_ENDPROC
938 END(bounds)
939
940 ENTRY(invalid_op)
941         RING0_INT_FRAME
942         ASM_CLAC
943         pushl_cfi $0
944         pushl_cfi $do_invalid_op
945         jmp error_code
946         CFI_ENDPROC
947 END(invalid_op)
948
949 ENTRY(coprocessor_segment_overrun)
950         RING0_INT_FRAME
951         ASM_CLAC
952         pushl_cfi $0
953         pushl_cfi $do_coprocessor_segment_overrun
954         jmp error_code
955         CFI_ENDPROC
956 END(coprocessor_segment_overrun)
957
958 ENTRY(invalid_TSS)
959         RING0_EC_FRAME
960         ASM_CLAC
961         pushl_cfi $do_invalid_TSS
962         jmp error_code
963         CFI_ENDPROC
964 END(invalid_TSS)
965
966 ENTRY(segment_not_present)
967         RING0_EC_FRAME
968         ASM_CLAC
969         pushl_cfi $do_segment_not_present
970         jmp error_code
971         CFI_ENDPROC
972 END(segment_not_present)
973
974 ENTRY(stack_segment)
975         RING0_EC_FRAME
976         ASM_CLAC
977         pushl_cfi $do_stack_segment
978         jmp error_code
979         CFI_ENDPROC
980 END(stack_segment)
981
982 ENTRY(alignment_check)
983         RING0_EC_FRAME
984         ASM_CLAC
985         pushl_cfi $do_alignment_check
986         jmp error_code
987         CFI_ENDPROC
988 END(alignment_check)
989
990 ENTRY(divide_error)
991         RING0_INT_FRAME
992         ASM_CLAC
993         pushl_cfi $0                    # no error code
994         pushl_cfi $do_divide_error
995         jmp error_code
996         CFI_ENDPROC
997 END(divide_error)
998
999 #ifdef CONFIG_X86_MCE
1000 ENTRY(machine_check)
1001         RING0_INT_FRAME
1002         ASM_CLAC
1003         pushl_cfi $0
1004         pushl_cfi machine_check_vector
1005         jmp error_code
1006         CFI_ENDPROC
1007 END(machine_check)
1008 #endif
1009
1010 ENTRY(spurious_interrupt_bug)
1011         RING0_INT_FRAME
1012         ASM_CLAC
1013         pushl_cfi $0
1014         pushl_cfi $do_spurious_interrupt_bug
1015         jmp error_code
1016         CFI_ENDPROC
1017 END(spurious_interrupt_bug)
1018 /*
1019  * End of kprobes section
1020  */
1021         .popsection
1022
1023 ENTRY(ret_from_kernel_thread)
1024         CFI_STARTPROC
1025         pushl_cfi %eax
1026         call schedule_tail
1027         GET_THREAD_INFO(%ebp)
1028         popl_cfi %eax
1029         pushl_cfi $0x0202               # Reset kernel eflags
1030         popfl_cfi
1031         movl PT_EBP(%esp),%eax
1032         call *PT_EBX(%esp)
1033         call do_exit
1034         ud2                     # padding for call trace
1035         CFI_ENDPROC
1036 ENDPROC(ret_from_kernel_thread)
1037
1038 #ifdef CONFIG_XEN
1039 /* Xen doesn't set %esp to be precisely what the normal sysenter
1040    entrypoint expects, so fix it up before using the normal path. */
1041 ENTRY(xen_sysenter_target)
1042         RING0_INT_FRAME
1043         addl $5*4, %esp         /* remove xen-provided frame */
1044         CFI_ADJUST_CFA_OFFSET -5*4
1045         jmp sysenter_past_esp
1046         CFI_ENDPROC
1047
1048 ENTRY(xen_hypervisor_callback)
1049         CFI_STARTPROC
1050         pushl_cfi $0
1051         SAVE_ALL
1052         TRACE_IRQS_OFF
1053
1054         /* Check to see if we got the event in the critical
1055            region in xen_iret_direct, after we've reenabled
1056            events and checked for pending events.  This simulates
1057            iret instruction's behaviour where it delivers a
1058            pending interrupt when enabling interrupts. */
1059         movl PT_EIP(%esp),%eax
1060         cmpl $xen_iret_start_crit,%eax
1061         jb   1f
1062         cmpl $xen_iret_end_crit,%eax
1063         jae  1f
1064
1065         jmp  xen_iret_crit_fixup
1066
1067 ENTRY(xen_do_upcall)
1068 1:      mov %esp, %eax
1069         call xen_evtchn_do_upcall
1070         jmp  ret_from_intr
1071         CFI_ENDPROC
1072 ENDPROC(xen_hypervisor_callback)
1073
1074 # Hypervisor uses this for application faults while it executes.
1075 # We get here for two reasons:
1076 #  1. Fault while reloading DS, ES, FS or GS
1077 #  2. Fault while executing IRET
1078 # Category 1 we fix up by reattempting the load, and zeroing the segment
1079 # register if the load fails.
1080 # Category 2 we fix up by jumping to do_iret_error. We cannot use the
1081 # normal Linux return path in this case because if we use the IRET hypercall
1082 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1083 # We distinguish between categories by maintaining a status value in EAX.
1084 ENTRY(xen_failsafe_callback)
1085         CFI_STARTPROC
1086         pushl_cfi %eax
1087         movl $1,%eax
1088 1:      mov 4(%esp),%ds
1089 2:      mov 8(%esp),%es
1090 3:      mov 12(%esp),%fs
1091 4:      mov 16(%esp),%gs
1092         testl %eax,%eax
1093         popl_cfi %eax
1094         lea 16(%esp),%esp
1095         CFI_ADJUST_CFA_OFFSET -16
1096         jz 5f
1097         addl $16,%esp
1098         jmp iret_exc            # EAX != 0 => Category 2 (Bad IRET)
1099 5:      pushl_cfi $0            # EAX == 0 => Category 1 (Bad segment)
1100         SAVE_ALL
1101         jmp ret_from_exception
1102         CFI_ENDPROC
1103
1104 .section .fixup,"ax"
1105 6:      xorl %eax,%eax
1106         movl %eax,4(%esp)
1107         jmp 1b
1108 7:      xorl %eax,%eax
1109         movl %eax,8(%esp)
1110         jmp 2b
1111 8:      xorl %eax,%eax
1112         movl %eax,12(%esp)
1113         jmp 3b
1114 9:      xorl %eax,%eax
1115         movl %eax,16(%esp)
1116         jmp 4b
1117 .previous
1118         _ASM_EXTABLE(1b,6b)
1119         _ASM_EXTABLE(2b,7b)
1120         _ASM_EXTABLE(3b,8b)
1121         _ASM_EXTABLE(4b,9b)
1122 ENDPROC(xen_failsafe_callback)
1123
1124 BUILD_INTERRUPT3(xen_hvm_callback_vector, XEN_HVM_EVTCHN_CALLBACK,
1125                 xen_evtchn_do_upcall)
1126
1127 #endif  /* CONFIG_XEN */
1128
1129 #ifdef CONFIG_FUNCTION_TRACER
1130 #ifdef CONFIG_DYNAMIC_FTRACE
1131
1132 ENTRY(mcount)
1133         ret
1134 END(mcount)
1135
1136 ENTRY(ftrace_caller)
1137         cmpl $0, function_trace_stop
1138         jne  ftrace_stub
1139
1140         pushl %eax
1141         pushl %ecx
1142         pushl %edx
1143         pushl $0        /* Pass NULL as regs pointer */
1144         movl 4*4(%esp), %eax
1145         movl 0x4(%ebp), %edx
1146         leal function_trace_op, %ecx
1147         subl $MCOUNT_INSN_SIZE, %eax
1148
1149 .globl ftrace_call
1150 ftrace_call:
1151         call ftrace_stub
1152
1153         addl $4,%esp    /* skip NULL pointer */
1154         popl %edx
1155         popl %ecx
1156         popl %eax
1157 ftrace_ret:
1158 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1159 .globl ftrace_graph_call
1160 ftrace_graph_call:
1161         jmp ftrace_stub
1162 #endif
1163
1164 .globl ftrace_stub
1165 ftrace_stub:
1166         ret
1167 END(ftrace_caller)
1168
1169 ENTRY(ftrace_regs_caller)
1170         pushf   /* push flags before compare (in cs location) */
1171         cmpl $0, function_trace_stop
1172         jne ftrace_restore_flags
1173
1174         /*
1175          * i386 does not save SS and ESP when coming from kernel.
1176          * Instead, to get sp, &regs->sp is used (see ptrace.h).
1177          * Unfortunately, that means eflags must be at the same location
1178          * as the current return ip is. We move the return ip into the
1179          * ip location, and move flags into the return ip location.
1180          */
1181         pushl 4(%esp)   /* save return ip into ip slot */
1182
1183         pushl $0        /* Load 0 into orig_ax */
1184         pushl %gs
1185         pushl %fs
1186         pushl %es
1187         pushl %ds
1188         pushl %eax
1189         pushl %ebp
1190         pushl %edi
1191         pushl %esi
1192         pushl %edx
1193         pushl %ecx
1194         pushl %ebx
1195
1196         movl 13*4(%esp), %eax   /* Get the saved flags */
1197         movl %eax, 14*4(%esp)   /* Move saved flags into regs->flags location */
1198                                 /* clobbering return ip */
1199         movl $__KERNEL_CS,13*4(%esp)
1200
1201         movl 12*4(%esp), %eax   /* Load ip (1st parameter) */
1202         subl $MCOUNT_INSN_SIZE, %eax    /* Adjust ip */
1203         movl 0x4(%ebp), %edx    /* Load parent ip (2nd parameter) */
1204         leal function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
1205         pushl %esp              /* Save pt_regs as 4th parameter */
1206
1207 GLOBAL(ftrace_regs_call)
1208         call ftrace_stub
1209
1210         addl $4, %esp           /* Skip pt_regs */
1211         movl 14*4(%esp), %eax   /* Move flags back into cs */
1212         movl %eax, 13*4(%esp)   /* Needed to keep addl from modifying flags */
1213         movl 12*4(%esp), %eax   /* Get return ip from regs->ip */
1214         movl %eax, 14*4(%esp)   /* Put return ip back for ret */
1215
1216         popl %ebx
1217         popl %ecx
1218         popl %edx
1219         popl %esi
1220         popl %edi
1221         popl %ebp
1222         popl %eax
1223         popl %ds
1224         popl %es
1225         popl %fs
1226         popl %gs
1227         addl $8, %esp           /* Skip orig_ax and ip */
1228         popf                    /* Pop flags at end (no addl to corrupt flags) */
1229         jmp ftrace_ret
1230
1231 ftrace_restore_flags:
1232         popf
1233         jmp  ftrace_stub
1234 #else /* ! CONFIG_DYNAMIC_FTRACE */
1235
1236 ENTRY(mcount)
1237         cmpl $0, function_trace_stop
1238         jne  ftrace_stub
1239
1240         cmpl $ftrace_stub, ftrace_trace_function
1241         jnz trace
1242 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1243         cmpl $ftrace_stub, ftrace_graph_return
1244         jnz ftrace_graph_caller
1245
1246         cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
1247         jnz ftrace_graph_caller
1248 #endif
1249 .globl ftrace_stub
1250 ftrace_stub:
1251         ret
1252
1253         /* taken from glibc */
1254 trace:
1255         pushl %eax
1256         pushl %ecx
1257         pushl %edx
1258         movl 0xc(%esp), %eax
1259         movl 0x4(%ebp), %edx
1260         subl $MCOUNT_INSN_SIZE, %eax
1261
1262         call *ftrace_trace_function
1263
1264         popl %edx
1265         popl %ecx
1266         popl %eax
1267         jmp ftrace_stub
1268 END(mcount)
1269 #endif /* CONFIG_DYNAMIC_FTRACE */
1270 #endif /* CONFIG_FUNCTION_TRACER */
1271
1272 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1273 ENTRY(ftrace_graph_caller)
1274         pushl %eax
1275         pushl %ecx
1276         pushl %edx
1277         movl 0xc(%esp), %edx
1278         lea 0x4(%ebp), %eax
1279         movl (%ebp), %ecx
1280         subl $MCOUNT_INSN_SIZE, %edx
1281         call prepare_ftrace_return
1282         popl %edx
1283         popl %ecx
1284         popl %eax
1285         ret
1286 END(ftrace_graph_caller)
1287
1288 .globl return_to_handler
1289 return_to_handler:
1290         pushl %eax
1291         pushl %edx
1292         movl %ebp, %eax
1293         call ftrace_return_to_handler
1294         movl %eax, %ecx
1295         popl %edx
1296         popl %eax
1297         jmp *%ecx
1298 #endif
1299
1300 /*
1301  * Some functions should be protected against kprobes
1302  */
1303         .pushsection .kprobes.text, "ax"
1304
1305 ENTRY(page_fault)
1306         RING0_EC_FRAME
1307         ASM_CLAC
1308         pushl_cfi $do_page_fault
1309         ALIGN
1310 error_code:
1311         /* the function address is in %gs's slot on the stack */
1312         pushl_cfi %fs
1313         /*CFI_REL_OFFSET fs, 0*/
1314         pushl_cfi %es
1315         /*CFI_REL_OFFSET es, 0*/
1316         pushl_cfi %ds
1317         /*CFI_REL_OFFSET ds, 0*/
1318         pushl_cfi %eax
1319         CFI_REL_OFFSET eax, 0
1320         pushl_cfi %ebp
1321         CFI_REL_OFFSET ebp, 0
1322         pushl_cfi %edi
1323         CFI_REL_OFFSET edi, 0
1324         pushl_cfi %esi
1325         CFI_REL_OFFSET esi, 0
1326         pushl_cfi %edx
1327         CFI_REL_OFFSET edx, 0
1328         pushl_cfi %ecx
1329         CFI_REL_OFFSET ecx, 0
1330         pushl_cfi %ebx
1331         CFI_REL_OFFSET ebx, 0
1332         cld
1333         movl $(__KERNEL_PERCPU), %ecx
1334         movl %ecx, %fs
1335         UNWIND_ESPFIX_STACK
1336         GS_TO_REG %ecx
1337         movl PT_GS(%esp), %edi          # get the function address
1338         movl PT_ORIG_EAX(%esp), %edx    # get the error code
1339         movl $-1, PT_ORIG_EAX(%esp)     # no syscall to restart
1340         REG_TO_PTGS %ecx
1341         SET_KERNEL_GS %ecx
1342         movl $(__USER_DS), %ecx
1343         movl %ecx, %ds
1344         movl %ecx, %es
1345         TRACE_IRQS_OFF
1346         movl %esp,%eax                  # pt_regs pointer
1347         call *%edi
1348         jmp ret_from_exception
1349         CFI_ENDPROC
1350 END(page_fault)
1351
1352 /*
1353  * Debug traps and NMI can happen at the one SYSENTER instruction
1354  * that sets up the real kernel stack. Check here, since we can't
1355  * allow the wrong stack to be used.
1356  *
1357  * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
1358  * already pushed 3 words if it hits on the sysenter instruction:
1359  * eflags, cs and eip.
1360  *
1361  * We just load the right stack, and push the three (known) values
1362  * by hand onto the new stack - while updating the return eip past
1363  * the instruction that would have done it for sysenter.
1364  */
1365 .macro FIX_STACK offset ok label
1366         cmpw $__KERNEL_CS, 4(%esp)
1367         jne \ok
1368 \label:
1369         movl TSS_sysenter_sp0 + \offset(%esp), %esp
1370         CFI_DEF_CFA esp, 0
1371         CFI_UNDEFINED eip
1372         pushfl_cfi
1373         pushl_cfi $__KERNEL_CS
1374         pushl_cfi $sysenter_past_esp
1375         CFI_REL_OFFSET eip, 0
1376 .endm
1377
1378 ENTRY(debug)
1379         RING0_INT_FRAME
1380         ASM_CLAC
1381         cmpl $ia32_sysenter_target,(%esp)
1382         jne debug_stack_correct
1383         FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
1384 debug_stack_correct:
1385         pushl_cfi $-1                   # mark this as an int
1386         SAVE_ALL
1387         TRACE_IRQS_OFF
1388         xorl %edx,%edx                  # error code 0
1389         movl %esp,%eax                  # pt_regs pointer
1390         call do_debug
1391         jmp ret_from_exception
1392         CFI_ENDPROC
1393 END(debug)
1394
1395 /*
1396  * NMI is doubly nasty. It can happen _while_ we're handling
1397  * a debug fault, and the debug fault hasn't yet been able to
1398  * clear up the stack. So we first check whether we got  an
1399  * NMI on the sysenter entry path, but after that we need to
1400  * check whether we got an NMI on the debug path where the debug
1401  * fault happened on the sysenter path.
1402  */
1403 ENTRY(nmi)
1404         RING0_INT_FRAME
1405         ASM_CLAC
1406         pushl_cfi %eax
1407         movl %ss, %eax
1408         cmpw $__ESPFIX_SS, %ax
1409         popl_cfi %eax
1410         je nmi_espfix_stack
1411         cmpl $ia32_sysenter_target,(%esp)
1412         je nmi_stack_fixup
1413         pushl_cfi %eax
1414         movl %esp,%eax
1415         /* Do not access memory above the end of our stack page,
1416          * it might not exist.
1417          */
1418         andl $(THREAD_SIZE-1),%eax
1419         cmpl $(THREAD_SIZE-20),%eax
1420         popl_cfi %eax
1421         jae nmi_stack_correct
1422         cmpl $ia32_sysenter_target,12(%esp)
1423         je nmi_debug_stack_check
1424 nmi_stack_correct:
1425         /* We have a RING0_INT_FRAME here */
1426         pushl_cfi %eax
1427         SAVE_ALL
1428         xorl %edx,%edx          # zero error code
1429         movl %esp,%eax          # pt_regs pointer
1430         call do_nmi
1431         jmp restore_all_notrace
1432         CFI_ENDPROC
1433
1434 nmi_stack_fixup:
1435         RING0_INT_FRAME
1436         FIX_STACK 12, nmi_stack_correct, 1
1437         jmp nmi_stack_correct
1438
1439 nmi_debug_stack_check:
1440         /* We have a RING0_INT_FRAME here */
1441         cmpw $__KERNEL_CS,16(%esp)
1442         jne nmi_stack_correct
1443         cmpl $debug,(%esp)
1444         jb nmi_stack_correct
1445         cmpl $debug_esp_fix_insn,(%esp)
1446         ja nmi_stack_correct
1447         FIX_STACK 24, nmi_stack_correct, 1
1448         jmp nmi_stack_correct
1449
1450 nmi_espfix_stack:
1451         /* We have a RING0_INT_FRAME here.
1452          *
1453          * create the pointer to lss back
1454          */
1455         pushl_cfi %ss
1456         pushl_cfi %esp
1457         addl $4, (%esp)
1458         /* copy the iret frame of 12 bytes */
1459         .rept 3
1460         pushl_cfi 16(%esp)
1461         .endr
1462         pushl_cfi %eax
1463         SAVE_ALL
1464         FIXUP_ESPFIX_STACK              # %eax == %esp
1465         xorl %edx,%edx                  # zero error code
1466         call do_nmi
1467         RESTORE_REGS
1468         lss 12+4(%esp), %esp            # back to espfix stack
1469         CFI_ADJUST_CFA_OFFSET -24
1470         jmp irq_return
1471         CFI_ENDPROC
1472 END(nmi)
1473
1474 ENTRY(int3)
1475         RING0_INT_FRAME
1476         ASM_CLAC
1477         pushl_cfi $-1                   # mark this as an int
1478         SAVE_ALL
1479         TRACE_IRQS_OFF
1480         xorl %edx,%edx          # zero error code
1481         movl %esp,%eax          # pt_regs pointer
1482         call do_int3
1483         jmp ret_from_exception
1484         CFI_ENDPROC
1485 END(int3)
1486
1487 ENTRY(general_protection)
1488         RING0_EC_FRAME
1489         pushl_cfi $do_general_protection
1490         jmp error_code
1491         CFI_ENDPROC
1492 END(general_protection)
1493
1494 #ifdef CONFIG_KVM_GUEST
1495 ENTRY(async_page_fault)
1496         RING0_EC_FRAME
1497         ASM_CLAC
1498         pushl_cfi $do_async_page_fault
1499         jmp error_code
1500         CFI_ENDPROC
1501 END(async_page_fault)
1502 #endif
1503
1504 /*
1505  * End of kprobes section
1506  */
1507         .popsection