]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - arch/x86/kernel/entry_32.S
x86-preempt-lazy.patch
[zynq/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_thread)
303         CFI_STARTPROC
304         pushl_cfi %eax
305         call schedule_tail
306         GET_THREAD_INFO(%ebp)
307         popl_cfi %eax
308         pushl_cfi $0x0202               # Reset kernel eflags
309         popfl_cfi
310         movl PT_EBP(%esp),%eax
311         call *PT_EBX(%esp)
312         movl $0,PT_EAX(%esp)
313         jmp syscall_exit
314         CFI_ENDPROC
315 ENDPROC(ret_from_kernel_thread)
316
317 /*
318  * Return to user mode is not as complex as all this looks,
319  * but we want the default path for a system call return to
320  * go as quickly as possible which is why some of this is
321  * less clear than it otherwise should be.
322  */
323
324         # userspace resumption stub bypassing syscall exit tracing
325         ALIGN
326         RING0_PTREGS_FRAME
327 ret_from_exception:
328         preempt_stop(CLBR_ANY)
329 ret_from_intr:
330         GET_THREAD_INFO(%ebp)
331 #ifdef CONFIG_VM86
332         movl PT_EFLAGS(%esp), %eax      # mix EFLAGS and CS
333         movb PT_CS(%esp), %al
334         andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
335 #else
336         /*
337          * We can be coming here from child spawned by kernel_thread().
338          */
339         movl PT_CS(%esp), %eax
340         andl $SEGMENT_RPL_MASK, %eax
341 #endif
342         cmpl $USER_RPL, %eax
343         jb resume_kernel                # not returning to v8086 or userspace
344
345 ENTRY(resume_userspace)
346         LOCKDEP_SYS_EXIT
347         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
348                                         # setting need_resched or sigpending
349                                         # between sampling and the iret
350         TRACE_IRQS_OFF
351         movl TI_flags(%ebp), %ecx
352         andl $_TIF_WORK_MASK, %ecx      # is there any work to be done on
353                                         # int/exception return?
354         jne work_pending
355         jmp restore_all
356 END(ret_from_exception)
357
358 #ifdef CONFIG_PREEMPT
359 ENTRY(resume_kernel)
360         DISABLE_INTERRUPTS(CLBR_ANY)
361 need_resched:
362         # preempt count == 0 + NEED_RS set?
363         cmpl $0,PER_CPU_VAR(__preempt_count)
364 #ifndef CONFIG_PREEMPT_LAZY
365         jnz restore_all
366 #else
367         jz test_int_off
368
369         # atleast preempt count == 0 ?
370         cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
371         jne restore_all
372
373         cmpl $0,TI_preempt_lazy_count(%ebp)     # non-zero preempt_lazy_count ?
374         jnz restore_all
375
376         testl $_TIF_NEED_RESCHED_LAZY, TI_flags(%ebp)
377         jz restore_all
378 test_int_off:
379 #endif
380         testl $X86_EFLAGS_IF,PT_EFLAGS(%esp)    # interrupts off (exception path) ?
381         jz restore_all
382         call preempt_schedule_irq
383         jmp need_resched
384 END(resume_kernel)
385 #endif
386         CFI_ENDPROC
387
388 /* SYSENTER_RETURN points to after the "sysenter" instruction in
389    the vsyscall page.  See vsyscall-sysentry.S, which defines the symbol.  */
390
391         # sysenter call handler stub
392 ENTRY(ia32_sysenter_target)
393         CFI_STARTPROC simple
394         CFI_SIGNAL_FRAME
395         CFI_DEF_CFA esp, 0
396         CFI_REGISTER esp, ebp
397         movl TSS_sysenter_sp0(%esp),%esp
398 sysenter_past_esp:
399         /*
400          * Interrupts are disabled here, but we can't trace it until
401          * enough kernel state to call TRACE_IRQS_OFF can be called - but
402          * we immediately enable interrupts at that point anyway.
403          */
404         pushl_cfi $__USER_DS
405         /*CFI_REL_OFFSET ss, 0*/
406         pushl_cfi %ebp
407         CFI_REL_OFFSET esp, 0
408         pushfl_cfi
409         orl $X86_EFLAGS_IF, (%esp)
410         pushl_cfi $__USER_CS
411         /*CFI_REL_OFFSET cs, 0*/
412         /*
413          * Push current_thread_info()->sysenter_return to the stack.
414          * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
415          * pushed above; +8 corresponds to copy_thread's esp0 setting.
416          */
417         pushl_cfi ((TI_sysenter_return)-THREAD_SIZE+8+4*4)(%esp)
418         CFI_REL_OFFSET eip, 0
419
420         pushl_cfi %eax
421         SAVE_ALL
422         ENABLE_INTERRUPTS(CLBR_NONE)
423
424 /*
425  * Load the potential sixth argument from user stack.
426  * Careful about security.
427  */
428         cmpl $__PAGE_OFFSET-3,%ebp
429         jae syscall_fault
430         ASM_STAC
431 1:      movl (%ebp),%ebp
432         ASM_CLAC
433         movl %ebp,PT_EBP(%esp)
434         _ASM_EXTABLE(1b,syscall_fault)
435
436         GET_THREAD_INFO(%ebp)
437
438         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
439         jnz sysenter_audit
440 sysenter_do_call:
441         cmpl $(NR_syscalls), %eax
442         jae sysenter_badsys
443         call *sys_call_table(,%eax,4)
444 sysenter_after_call:
445         movl %eax,PT_EAX(%esp)
446         LOCKDEP_SYS_EXIT
447         DISABLE_INTERRUPTS(CLBR_ANY)
448         TRACE_IRQS_OFF
449         movl TI_flags(%ebp), %ecx
450         testl $_TIF_ALLWORK_MASK, %ecx
451         jne sysexit_audit
452 sysenter_exit:
453 /* if something modifies registers it must also disable sysexit */
454         movl PT_EIP(%esp), %edx
455         movl PT_OLDESP(%esp), %ecx
456         xorl %ebp,%ebp
457         TRACE_IRQS_ON
458 1:      mov  PT_FS(%esp), %fs
459         PTGS_TO_GS
460         ENABLE_INTERRUPTS_SYSEXIT
461
462 #ifdef CONFIG_AUDITSYSCALL
463 sysenter_audit:
464         testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
465         jnz syscall_trace_entry
466         /* movl PT_EAX(%esp), %eax      already set, syscall number: 1st arg to audit */
467         movl PT_EBX(%esp), %edx         /* ebx/a0: 2nd arg to audit */
468         /* movl PT_ECX(%esp), %ecx      already set, a1: 3nd arg to audit */
469         pushl_cfi PT_ESI(%esp)          /* a3: 5th arg */
470         pushl_cfi PT_EDX+4(%esp)        /* a2: 4th arg */
471         call __audit_syscall_entry
472         popl_cfi %ecx /* get that remapped edx off the stack */
473         popl_cfi %ecx /* get that remapped esi off the stack */
474         movl PT_EAX(%esp),%eax          /* reload syscall number */
475         jmp sysenter_do_call
476
477 sysexit_audit:
478         testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
479         jne syscall_exit_work
480         TRACE_IRQS_ON
481         ENABLE_INTERRUPTS(CLBR_ANY)
482         movl %eax,%edx          /* second arg, syscall return value */
483         cmpl $-MAX_ERRNO,%eax   /* is it an error ? */
484         setbe %al               /* 1 if so, 0 if not */
485         movzbl %al,%eax         /* zero-extend that */
486         call __audit_syscall_exit
487         DISABLE_INTERRUPTS(CLBR_ANY)
488         TRACE_IRQS_OFF
489         movl TI_flags(%ebp), %ecx
490         testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
491         jne syscall_exit_work
492         movl PT_EAX(%esp),%eax  /* reload syscall return value */
493         jmp sysenter_exit
494 #endif
495
496         CFI_ENDPROC
497 .pushsection .fixup,"ax"
498 2:      movl $0,PT_FS(%esp)
499         jmp 1b
500 .popsection
501         _ASM_EXTABLE(1b,2b)
502         PTGS_TO_GS_EX
503 ENDPROC(ia32_sysenter_target)
504
505         # system call handler stub
506 ENTRY(system_call)
507         RING0_INT_FRAME                 # can't unwind into user space anyway
508         ASM_CLAC
509         pushl_cfi %eax                  # save orig_eax
510         SAVE_ALL
511         GET_THREAD_INFO(%ebp)
512                                         # system call tracing in operation / emulation
513         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
514         jnz syscall_trace_entry
515         cmpl $(NR_syscalls), %eax
516         jae syscall_badsys
517 syscall_call:
518         call *sys_call_table(,%eax,4)
519 syscall_after_call:
520         movl %eax,PT_EAX(%esp)          # store the return value
521 syscall_exit:
522         LOCKDEP_SYS_EXIT
523         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
524                                         # setting need_resched or sigpending
525                                         # between sampling and the iret
526         TRACE_IRQS_OFF
527         movl TI_flags(%ebp), %ecx
528         testl $_TIF_ALLWORK_MASK, %ecx  # current->work
529         jne syscall_exit_work
530
531 restore_all:
532         TRACE_IRQS_IRET
533 restore_all_notrace:
534 #ifdef CONFIG_X86_ESPFIX32
535         movl PT_EFLAGS(%esp), %eax      # mix EFLAGS, SS and CS
536         # Warning: PT_OLDSS(%esp) contains the wrong/random values if we
537         # are returning to the kernel.
538         # See comments in process.c:copy_thread() for details.
539         movb PT_OLDSS(%esp), %ah
540         movb PT_CS(%esp), %al
541         andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
542         cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
543         CFI_REMEMBER_STATE
544         je ldt_ss                       # returning to user-space with LDT SS
545 #endif
546 restore_nocheck:
547         RESTORE_REGS 4                  # skip orig_eax/error_code
548 irq_return:
549         INTERRUPT_RETURN
550 .section .fixup,"ax"
551 ENTRY(iret_exc)
552         pushl $0                        # no error code
553         pushl $do_iret_error
554         jmp error_code
555 .previous
556         _ASM_EXTABLE(irq_return,iret_exc)
557
558 #ifdef CONFIG_X86_ESPFIX32
559         CFI_RESTORE_STATE
560 ldt_ss:
561 #ifdef CONFIG_PARAVIRT
562         /*
563          * The kernel can't run on a non-flat stack if paravirt mode
564          * is active.  Rather than try to fixup the high bits of
565          * ESP, bypass this code entirely.  This may break DOSemu
566          * and/or Wine support in a paravirt VM, although the option
567          * is still available to implement the setting of the high
568          * 16-bits in the INTERRUPT_RETURN paravirt-op.
569          */
570         cmpl $0, pv_info+PARAVIRT_enabled
571         jne restore_nocheck
572 #endif
573
574 /*
575  * Setup and switch to ESPFIX stack
576  *
577  * We're returning to userspace with a 16 bit stack. The CPU will not
578  * restore the high word of ESP for us on executing iret... This is an
579  * "official" bug of all the x86-compatible CPUs, which we can work
580  * around to make dosemu and wine happy. We do this by preloading the
581  * high word of ESP with the high word of the userspace ESP while
582  * compensating for the offset by changing to the ESPFIX segment with
583  * a base address that matches for the difference.
584  */
585 #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
586         mov %esp, %edx                  /* load kernel esp */
587         mov PT_OLDESP(%esp), %eax       /* load userspace esp */
588         mov %dx, %ax                    /* eax: new kernel esp */
589         sub %eax, %edx                  /* offset (low word is 0) */
590         shr $16, %edx
591         mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
592         mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
593         pushl_cfi $__ESPFIX_SS
594         pushl_cfi %eax                  /* new kernel esp */
595         /* Disable interrupts, but do not irqtrace this section: we
596          * will soon execute iret and the tracer was already set to
597          * the irqstate after the iret */
598         DISABLE_INTERRUPTS(CLBR_EAX)
599         lss (%esp), %esp                /* switch to espfix segment */
600         CFI_ADJUST_CFA_OFFSET -8
601         jmp restore_nocheck
602 #endif
603         CFI_ENDPROC
604 ENDPROC(system_call)
605
606         # perform work that needs to be done immediately before resumption
607         ALIGN
608         RING0_PTREGS_FRAME              # can't unwind into user space anyway
609 work_pending:
610         testl $_TIF_NEED_RESCHED_MASK, %ecx
611         jz work_notifysig
612 work_resched:
613         call schedule
614         LOCKDEP_SYS_EXIT
615         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
616                                         # setting need_resched or sigpending
617                                         # between sampling and the iret
618         TRACE_IRQS_OFF
619         movl TI_flags(%ebp), %ecx
620         andl $_TIF_WORK_MASK, %ecx      # is there any work to be done other
621                                         # than syscall tracing?
622         jz restore_all
623         testl $_TIF_NEED_RESCHED_MASK, %ecx
624         jnz work_resched
625
626 work_notifysig:                         # deal with pending signals and
627                                         # notify-resume requests
628 #ifdef CONFIG_VM86
629         testl $X86_EFLAGS_VM, PT_EFLAGS(%esp)
630         movl %esp, %eax
631         jne work_notifysig_v86          # returning to kernel-space or
632                                         # vm86-space
633 1:
634 #else
635         movl %esp, %eax
636 #endif
637         TRACE_IRQS_ON
638         ENABLE_INTERRUPTS(CLBR_NONE)
639         movb PT_CS(%esp), %bl
640         andb $SEGMENT_RPL_MASK, %bl
641         cmpb $USER_RPL, %bl
642         jb resume_kernel
643         xorl %edx, %edx
644         call do_notify_resume
645         jmp resume_userspace
646
647 #ifdef CONFIG_VM86
648         ALIGN
649 work_notifysig_v86:
650         pushl_cfi %ecx                  # save ti_flags for do_notify_resume
651         call save_v86_state             # %eax contains pt_regs pointer
652         popl_cfi %ecx
653         movl %eax, %esp
654         jmp 1b
655 #endif
656 END(work_pending)
657
658         # perform syscall exit tracing
659         ALIGN
660 syscall_trace_entry:
661         movl $-ENOSYS,PT_EAX(%esp)
662         movl %esp, %eax
663         call syscall_trace_enter
664         /* What it returned is what we'll actually use.  */
665         cmpl $(NR_syscalls), %eax
666         jnae syscall_call
667         jmp syscall_exit
668 END(syscall_trace_entry)
669
670         # perform syscall exit tracing
671         ALIGN
672 syscall_exit_work:
673         testl $_TIF_WORK_SYSCALL_EXIT, %ecx
674         jz work_pending
675         TRACE_IRQS_ON
676         ENABLE_INTERRUPTS(CLBR_ANY)     # could let syscall_trace_leave() call
677                                         # schedule() instead
678         movl %esp, %eax
679         call syscall_trace_leave
680         jmp resume_userspace
681 END(syscall_exit_work)
682         CFI_ENDPROC
683
684         RING0_INT_FRAME                 # can't unwind into user space anyway
685 syscall_fault:
686         ASM_CLAC
687         GET_THREAD_INFO(%ebp)
688         movl $-EFAULT,PT_EAX(%esp)
689         jmp resume_userspace
690 END(syscall_fault)
691
692 syscall_badsys:
693         movl $-ENOSYS,%eax
694         jmp syscall_after_call
695 END(syscall_badsys)
696
697 sysenter_badsys:
698         movl $-ENOSYS,%eax
699         jmp sysenter_after_call
700 END(sysenter_badsys)
701         CFI_ENDPROC
702
703 .macro FIXUP_ESPFIX_STACK
704 /*
705  * Switch back for ESPFIX stack to the normal zerobased stack
706  *
707  * We can't call C functions using the ESPFIX stack. This code reads
708  * the high word of the segment base from the GDT and swiches to the
709  * normal stack and adjusts ESP with the matching offset.
710  */
711 #ifdef CONFIG_X86_ESPFIX32
712         /* fixup the stack */
713         mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
714         mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
715         shl $16, %eax
716         addl %esp, %eax                 /* the adjusted stack pointer */
717         pushl_cfi $__KERNEL_DS
718         pushl_cfi %eax
719         lss (%esp), %esp                /* switch to the normal stack segment */
720         CFI_ADJUST_CFA_OFFSET -8
721 #endif
722 .endm
723 .macro UNWIND_ESPFIX_STACK
724 #ifdef CONFIG_X86_ESPFIX32
725         movl %ss, %eax
726         /* see if on espfix stack */
727         cmpw $__ESPFIX_SS, %ax
728         jne 27f
729         movl $__KERNEL_DS, %eax
730         movl %eax, %ds
731         movl %eax, %es
732         /* switch to normal stack */
733         FIXUP_ESPFIX_STACK
734 27:
735 #endif
736 .endm
737
738 /*
739  * Build the entry stubs and pointer table with some assembler magic.
740  * We pack 7 stubs into a single 32-byte chunk, which will fit in a
741  * single cache line on all modern x86 implementations.
742  */
743 .section .init.rodata,"a"
744 ENTRY(interrupt)
745 .section .entry.text, "ax"
746         .p2align 5
747         .p2align CONFIG_X86_L1_CACHE_SHIFT
748 ENTRY(irq_entries_start)
749         RING0_INT_FRAME
750 vector=FIRST_EXTERNAL_VECTOR
751 .rept (FIRST_SYSTEM_VECTOR-FIRST_EXTERNAL_VECTOR+6)/7
752         .balign 32
753   .rept 7
754     .if vector < FIRST_SYSTEM_VECTOR
755       .if vector <> FIRST_EXTERNAL_VECTOR
756         CFI_ADJUST_CFA_OFFSET -4
757       .endif
758 1:      pushl_cfi $(~vector+0x80)       /* Note: always in signed byte range */
759       .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
760         jmp 2f
761       .endif
762       .previous
763         .long 1b
764       .section .entry.text, "ax"
765 vector=vector+1
766     .endif
767   .endr
768 2:      jmp common_interrupt
769 .endr
770 END(irq_entries_start)
771
772 .previous
773 END(interrupt)
774 .previous
775
776 /*
777  * the CPU automatically disables interrupts when executing an IRQ vector,
778  * so IRQ-flags tracing has to follow that:
779  */
780         .p2align CONFIG_X86_L1_CACHE_SHIFT
781 common_interrupt:
782         ASM_CLAC
783         addl $-0x80,(%esp)      /* Adjust vector into the [-256,-1] range */
784         SAVE_ALL
785         TRACE_IRQS_OFF
786         movl %esp,%eax
787         call do_IRQ
788         jmp ret_from_intr
789 ENDPROC(common_interrupt)
790         CFI_ENDPROC
791
792 #define BUILD_INTERRUPT3(name, nr, fn)  \
793 ENTRY(name)                             \
794         RING0_INT_FRAME;                \
795         ASM_CLAC;                       \
796         pushl_cfi $~(nr);               \
797         SAVE_ALL;                       \
798         TRACE_IRQS_OFF                  \
799         movl %esp,%eax;                 \
800         call fn;                        \
801         jmp ret_from_intr;              \
802         CFI_ENDPROC;                    \
803 ENDPROC(name)
804
805
806 #ifdef CONFIG_TRACING
807 #define TRACE_BUILD_INTERRUPT(name, nr)         \
808         BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
809 #else
810 #define TRACE_BUILD_INTERRUPT(name, nr)
811 #endif
812
813 #define BUILD_INTERRUPT(name, nr) \
814         BUILD_INTERRUPT3(name, nr, smp_##name); \
815         TRACE_BUILD_INTERRUPT(name, nr)
816
817 /* The include is where all of the SMP etc. interrupts come from */
818 #include <asm/entry_arch.h>
819
820 ENTRY(coprocessor_error)
821         RING0_INT_FRAME
822         ASM_CLAC
823         pushl_cfi $0
824         pushl_cfi $do_coprocessor_error
825         jmp error_code
826         CFI_ENDPROC
827 END(coprocessor_error)
828
829 ENTRY(simd_coprocessor_error)
830         RING0_INT_FRAME
831         ASM_CLAC
832         pushl_cfi $0
833 #ifdef CONFIG_X86_INVD_BUG
834         /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
835 661:    pushl_cfi $do_general_protection
836 662:
837 .section .altinstructions,"a"
838         altinstruction_entry 661b, 663f, X86_FEATURE_XMM, 662b-661b, 664f-663f
839 .previous
840 .section .altinstr_replacement,"ax"
841 663:    pushl $do_simd_coprocessor_error
842 664:
843 .previous
844 #else
845         pushl_cfi $do_simd_coprocessor_error
846 #endif
847         jmp error_code
848         CFI_ENDPROC
849 END(simd_coprocessor_error)
850
851 ENTRY(device_not_available)
852         RING0_INT_FRAME
853         ASM_CLAC
854         pushl_cfi $-1                   # mark this as an int
855         pushl_cfi $do_device_not_available
856         jmp error_code
857         CFI_ENDPROC
858 END(device_not_available)
859
860 #ifdef CONFIG_PARAVIRT
861 ENTRY(native_iret)
862         iret
863         _ASM_EXTABLE(native_iret, iret_exc)
864 END(native_iret)
865
866 ENTRY(native_irq_enable_sysexit)
867         sti
868         sysexit
869 END(native_irq_enable_sysexit)
870 #endif
871
872 ENTRY(overflow)
873         RING0_INT_FRAME
874         ASM_CLAC
875         pushl_cfi $0
876         pushl_cfi $do_overflow
877         jmp error_code
878         CFI_ENDPROC
879 END(overflow)
880
881 ENTRY(bounds)
882         RING0_INT_FRAME
883         ASM_CLAC
884         pushl_cfi $0
885         pushl_cfi $do_bounds
886         jmp error_code
887         CFI_ENDPROC
888 END(bounds)
889
890 ENTRY(invalid_op)
891         RING0_INT_FRAME
892         ASM_CLAC
893         pushl_cfi $0
894         pushl_cfi $do_invalid_op
895         jmp error_code
896         CFI_ENDPROC
897 END(invalid_op)
898
899 ENTRY(coprocessor_segment_overrun)
900         RING0_INT_FRAME
901         ASM_CLAC
902         pushl_cfi $0
903         pushl_cfi $do_coprocessor_segment_overrun
904         jmp error_code
905         CFI_ENDPROC
906 END(coprocessor_segment_overrun)
907
908 ENTRY(invalid_TSS)
909         RING0_EC_FRAME
910         ASM_CLAC
911         pushl_cfi $do_invalid_TSS
912         jmp error_code
913         CFI_ENDPROC
914 END(invalid_TSS)
915
916 ENTRY(segment_not_present)
917         RING0_EC_FRAME
918         ASM_CLAC
919         pushl_cfi $do_segment_not_present
920         jmp error_code
921         CFI_ENDPROC
922 END(segment_not_present)
923
924 ENTRY(stack_segment)
925         RING0_EC_FRAME
926         ASM_CLAC
927         pushl_cfi $do_stack_segment
928         jmp error_code
929         CFI_ENDPROC
930 END(stack_segment)
931
932 ENTRY(alignment_check)
933         RING0_EC_FRAME
934         ASM_CLAC
935         pushl_cfi $do_alignment_check
936         jmp error_code
937         CFI_ENDPROC
938 END(alignment_check)
939
940 ENTRY(divide_error)
941         RING0_INT_FRAME
942         ASM_CLAC
943         pushl_cfi $0                    # no error code
944         pushl_cfi $do_divide_error
945         jmp error_code
946         CFI_ENDPROC
947 END(divide_error)
948
949 #ifdef CONFIG_X86_MCE
950 ENTRY(machine_check)
951         RING0_INT_FRAME
952         ASM_CLAC
953         pushl_cfi $0
954         pushl_cfi machine_check_vector
955         jmp error_code
956         CFI_ENDPROC
957 END(machine_check)
958 #endif
959
960 ENTRY(spurious_interrupt_bug)
961         RING0_INT_FRAME
962         ASM_CLAC
963         pushl_cfi $0
964         pushl_cfi $do_spurious_interrupt_bug
965         jmp error_code
966         CFI_ENDPROC
967 END(spurious_interrupt_bug)
968
969 #ifdef CONFIG_XEN
970 /* Xen doesn't set %esp to be precisely what the normal sysenter
971    entrypoint expects, so fix it up before using the normal path. */
972 ENTRY(xen_sysenter_target)
973         RING0_INT_FRAME
974         addl $5*4, %esp         /* remove xen-provided frame */
975         CFI_ADJUST_CFA_OFFSET -5*4
976         jmp sysenter_past_esp
977         CFI_ENDPROC
978
979 ENTRY(xen_hypervisor_callback)
980         CFI_STARTPROC
981         pushl_cfi $-1 /* orig_ax = -1 => not a system call */
982         SAVE_ALL
983         TRACE_IRQS_OFF
984
985         /* Check to see if we got the event in the critical
986            region in xen_iret_direct, after we've reenabled
987            events and checked for pending events.  This simulates
988            iret instruction's behaviour where it delivers a
989            pending interrupt when enabling interrupts. */
990         movl PT_EIP(%esp),%eax
991         cmpl $xen_iret_start_crit,%eax
992         jb   1f
993         cmpl $xen_iret_end_crit,%eax
994         jae  1f
995
996         jmp  xen_iret_crit_fixup
997
998 ENTRY(xen_do_upcall)
999 1:      mov %esp, %eax
1000         call xen_evtchn_do_upcall
1001 #ifndef CONFIG_PREEMPT
1002         call xen_maybe_preempt_hcall
1003 #endif
1004         jmp  ret_from_intr
1005         CFI_ENDPROC
1006 ENDPROC(xen_hypervisor_callback)
1007
1008 # Hypervisor uses this for application faults while it executes.
1009 # We get here for two reasons:
1010 #  1. Fault while reloading DS, ES, FS or GS
1011 #  2. Fault while executing IRET
1012 # Category 1 we fix up by reattempting the load, and zeroing the segment
1013 # register if the load fails.
1014 # Category 2 we fix up by jumping to do_iret_error. We cannot use the
1015 # normal Linux return path in this case because if we use the IRET hypercall
1016 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1017 # We distinguish between categories by maintaining a status value in EAX.
1018 ENTRY(xen_failsafe_callback)
1019         CFI_STARTPROC
1020         pushl_cfi %eax
1021         movl $1,%eax
1022 1:      mov 4(%esp),%ds
1023 2:      mov 8(%esp),%es
1024 3:      mov 12(%esp),%fs
1025 4:      mov 16(%esp),%gs
1026         /* EAX == 0 => Category 1 (Bad segment)
1027            EAX != 0 => Category 2 (Bad IRET) */
1028         testl %eax,%eax
1029         popl_cfi %eax
1030         lea 16(%esp),%esp
1031         CFI_ADJUST_CFA_OFFSET -16
1032         jz 5f
1033         jmp iret_exc
1034 5:      pushl_cfi $-1 /* orig_ax = -1 => not a system call */
1035         SAVE_ALL
1036         jmp ret_from_exception
1037         CFI_ENDPROC
1038
1039 .section .fixup,"ax"
1040 6:      xorl %eax,%eax
1041         movl %eax,4(%esp)
1042         jmp 1b
1043 7:      xorl %eax,%eax
1044         movl %eax,8(%esp)
1045         jmp 2b
1046 8:      xorl %eax,%eax
1047         movl %eax,12(%esp)
1048         jmp 3b
1049 9:      xorl %eax,%eax
1050         movl %eax,16(%esp)
1051         jmp 4b
1052 .previous
1053         _ASM_EXTABLE(1b,6b)
1054         _ASM_EXTABLE(2b,7b)
1055         _ASM_EXTABLE(3b,8b)
1056         _ASM_EXTABLE(4b,9b)
1057 ENDPROC(xen_failsafe_callback)
1058
1059 BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
1060                 xen_evtchn_do_upcall)
1061
1062 #endif  /* CONFIG_XEN */
1063
1064 #if IS_ENABLED(CONFIG_HYPERV)
1065
1066 BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
1067         hyperv_vector_handler)
1068
1069 #endif /* CONFIG_HYPERV */
1070
1071 #ifdef CONFIG_FUNCTION_TRACER
1072 #ifdef CONFIG_DYNAMIC_FTRACE
1073
1074 ENTRY(mcount)
1075         ret
1076 END(mcount)
1077
1078 ENTRY(ftrace_caller)
1079         pushl %eax
1080         pushl %ecx
1081         pushl %edx
1082         pushl $0        /* Pass NULL as regs pointer */
1083         movl 4*4(%esp), %eax
1084         movl 0x4(%ebp), %edx
1085         movl function_trace_op, %ecx
1086         subl $MCOUNT_INSN_SIZE, %eax
1087
1088 .globl ftrace_call
1089 ftrace_call:
1090         call ftrace_stub
1091
1092         addl $4,%esp    /* skip NULL pointer */
1093         popl %edx
1094         popl %ecx
1095         popl %eax
1096 ftrace_ret:
1097 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1098 .globl ftrace_graph_call
1099 ftrace_graph_call:
1100         jmp ftrace_stub
1101 #endif
1102
1103 .globl ftrace_stub
1104 ftrace_stub:
1105         ret
1106 END(ftrace_caller)
1107
1108 ENTRY(ftrace_regs_caller)
1109         pushf   /* push flags before compare (in cs location) */
1110
1111         /*
1112          * i386 does not save SS and ESP when coming from kernel.
1113          * Instead, to get sp, &regs->sp is used (see ptrace.h).
1114          * Unfortunately, that means eflags must be at the same location
1115          * as the current return ip is. We move the return ip into the
1116          * ip location, and move flags into the return ip location.
1117          */
1118         pushl 4(%esp)   /* save return ip into ip slot */
1119
1120         pushl $0        /* Load 0 into orig_ax */
1121         pushl %gs
1122         pushl %fs
1123         pushl %es
1124         pushl %ds
1125         pushl %eax
1126         pushl %ebp
1127         pushl %edi
1128         pushl %esi
1129         pushl %edx
1130         pushl %ecx
1131         pushl %ebx
1132
1133         movl 13*4(%esp), %eax   /* Get the saved flags */
1134         movl %eax, 14*4(%esp)   /* Move saved flags into regs->flags location */
1135                                 /* clobbering return ip */
1136         movl $__KERNEL_CS,13*4(%esp)
1137
1138         movl 12*4(%esp), %eax   /* Load ip (1st parameter) */
1139         subl $MCOUNT_INSN_SIZE, %eax    /* Adjust ip */
1140         movl 0x4(%ebp), %edx    /* Load parent ip (2nd parameter) */
1141         movl function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
1142         pushl %esp              /* Save pt_regs as 4th parameter */
1143
1144 GLOBAL(ftrace_regs_call)
1145         call ftrace_stub
1146
1147         addl $4, %esp           /* Skip pt_regs */
1148         movl 14*4(%esp), %eax   /* Move flags back into cs */
1149         movl %eax, 13*4(%esp)   /* Needed to keep addl from modifying flags */
1150         movl 12*4(%esp), %eax   /* Get return ip from regs->ip */
1151         movl %eax, 14*4(%esp)   /* Put return ip back for ret */
1152
1153         popl %ebx
1154         popl %ecx
1155         popl %edx
1156         popl %esi
1157         popl %edi
1158         popl %ebp
1159         popl %eax
1160         popl %ds
1161         popl %es
1162         popl %fs
1163         popl %gs
1164         addl $8, %esp           /* Skip orig_ax and ip */
1165         popf                    /* Pop flags at end (no addl to corrupt flags) */
1166         jmp ftrace_ret
1167
1168         popf
1169         jmp  ftrace_stub
1170 #else /* ! CONFIG_DYNAMIC_FTRACE */
1171
1172 ENTRY(mcount)
1173         cmpl $__PAGE_OFFSET, %esp
1174         jb ftrace_stub          /* Paging not enabled yet? */
1175
1176         cmpl $ftrace_stub, ftrace_trace_function
1177         jnz trace
1178 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1179         cmpl $ftrace_stub, ftrace_graph_return
1180         jnz ftrace_graph_caller
1181
1182         cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
1183         jnz ftrace_graph_caller
1184 #endif
1185 .globl ftrace_stub
1186 ftrace_stub:
1187         ret
1188
1189         /* taken from glibc */
1190 trace:
1191         pushl %eax
1192         pushl %ecx
1193         pushl %edx
1194         movl 0xc(%esp), %eax
1195         movl 0x4(%ebp), %edx
1196         subl $MCOUNT_INSN_SIZE, %eax
1197
1198         call *ftrace_trace_function
1199
1200         popl %edx
1201         popl %ecx
1202         popl %eax
1203         jmp ftrace_stub
1204 END(mcount)
1205 #endif /* CONFIG_DYNAMIC_FTRACE */
1206 #endif /* CONFIG_FUNCTION_TRACER */
1207
1208 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1209 ENTRY(ftrace_graph_caller)
1210         pushl %eax
1211         pushl %ecx
1212         pushl %edx
1213         movl 0xc(%esp), %eax
1214         lea 0x4(%ebp), %edx
1215         movl (%ebp), %ecx
1216         subl $MCOUNT_INSN_SIZE, %eax
1217         call prepare_ftrace_return
1218         popl %edx
1219         popl %ecx
1220         popl %eax
1221         ret
1222 END(ftrace_graph_caller)
1223
1224 .globl return_to_handler
1225 return_to_handler:
1226         pushl %eax
1227         pushl %edx
1228         movl %ebp, %eax
1229         call ftrace_return_to_handler
1230         movl %eax, %ecx
1231         popl %edx
1232         popl %eax
1233         jmp *%ecx
1234 #endif
1235
1236 #ifdef CONFIG_TRACING
1237 ENTRY(trace_page_fault)
1238         RING0_EC_FRAME
1239         ASM_CLAC
1240         pushl_cfi $trace_do_page_fault
1241         jmp error_code
1242         CFI_ENDPROC
1243 END(trace_page_fault)
1244 #endif
1245
1246 ENTRY(page_fault)
1247         RING0_EC_FRAME
1248         ASM_CLAC
1249         pushl_cfi $do_page_fault
1250         ALIGN
1251 error_code:
1252         /* the function address is in %gs's slot on the stack */
1253         pushl_cfi %fs
1254         /*CFI_REL_OFFSET fs, 0*/
1255         pushl_cfi %es
1256         /*CFI_REL_OFFSET es, 0*/
1257         pushl_cfi %ds
1258         /*CFI_REL_OFFSET ds, 0*/
1259         pushl_cfi %eax
1260         CFI_REL_OFFSET eax, 0
1261         pushl_cfi %ebp
1262         CFI_REL_OFFSET ebp, 0
1263         pushl_cfi %edi
1264         CFI_REL_OFFSET edi, 0
1265         pushl_cfi %esi
1266         CFI_REL_OFFSET esi, 0
1267         pushl_cfi %edx
1268         CFI_REL_OFFSET edx, 0
1269         pushl_cfi %ecx
1270         CFI_REL_OFFSET ecx, 0
1271         pushl_cfi %ebx
1272         CFI_REL_OFFSET ebx, 0
1273         cld
1274         movl $(__KERNEL_PERCPU), %ecx
1275         movl %ecx, %fs
1276         UNWIND_ESPFIX_STACK
1277         GS_TO_REG %ecx
1278         movl PT_GS(%esp), %edi          # get the function address
1279         movl PT_ORIG_EAX(%esp), %edx    # get the error code
1280         movl $-1, PT_ORIG_EAX(%esp)     # no syscall to restart
1281         REG_TO_PTGS %ecx
1282         SET_KERNEL_GS %ecx
1283         movl $(__USER_DS), %ecx
1284         movl %ecx, %ds
1285         movl %ecx, %es
1286         TRACE_IRQS_OFF
1287         movl %esp,%eax                  # pt_regs pointer
1288         call *%edi
1289         jmp ret_from_exception
1290         CFI_ENDPROC
1291 END(page_fault)
1292
1293 /*
1294  * Debug traps and NMI can happen at the one SYSENTER instruction
1295  * that sets up the real kernel stack. Check here, since we can't
1296  * allow the wrong stack to be used.
1297  *
1298  * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
1299  * already pushed 3 words if it hits on the sysenter instruction:
1300  * eflags, cs and eip.
1301  *
1302  * We just load the right stack, and push the three (known) values
1303  * by hand onto the new stack - while updating the return eip past
1304  * the instruction that would have done it for sysenter.
1305  */
1306 .macro FIX_STACK offset ok label
1307         cmpw $__KERNEL_CS, 4(%esp)
1308         jne \ok
1309 \label:
1310         movl TSS_sysenter_sp0 + \offset(%esp), %esp
1311         CFI_DEF_CFA esp, 0
1312         CFI_UNDEFINED eip
1313         pushfl_cfi
1314         pushl_cfi $__KERNEL_CS
1315         pushl_cfi $sysenter_past_esp
1316         CFI_REL_OFFSET eip, 0
1317 .endm
1318
1319 ENTRY(debug)
1320         RING0_INT_FRAME
1321         ASM_CLAC
1322         cmpl $ia32_sysenter_target,(%esp)
1323         jne debug_stack_correct
1324         FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
1325 debug_stack_correct:
1326         pushl_cfi $-1                   # mark this as an int
1327         SAVE_ALL
1328         TRACE_IRQS_OFF
1329         xorl %edx,%edx                  # error code 0
1330         movl %esp,%eax                  # pt_regs pointer
1331         call do_debug
1332         jmp ret_from_exception
1333         CFI_ENDPROC
1334 END(debug)
1335
1336 /*
1337  * NMI is doubly nasty. It can happen _while_ we're handling
1338  * a debug fault, and the debug fault hasn't yet been able to
1339  * clear up the stack. So we first check whether we got  an
1340  * NMI on the sysenter entry path, but after that we need to
1341  * check whether we got an NMI on the debug path where the debug
1342  * fault happened on the sysenter path.
1343  */
1344 ENTRY(nmi)
1345         RING0_INT_FRAME
1346         ASM_CLAC
1347 #ifdef CONFIG_X86_ESPFIX32
1348         pushl_cfi %eax
1349         movl %ss, %eax
1350         cmpw $__ESPFIX_SS, %ax
1351         popl_cfi %eax
1352         je nmi_espfix_stack
1353 #endif
1354         cmpl $ia32_sysenter_target,(%esp)
1355         je nmi_stack_fixup
1356         pushl_cfi %eax
1357         movl %esp,%eax
1358         /* Do not access memory above the end of our stack page,
1359          * it might not exist.
1360          */
1361         andl $(THREAD_SIZE-1),%eax
1362         cmpl $(THREAD_SIZE-20),%eax
1363         popl_cfi %eax
1364         jae nmi_stack_correct
1365         cmpl $ia32_sysenter_target,12(%esp)
1366         je nmi_debug_stack_check
1367 nmi_stack_correct:
1368         /* We have a RING0_INT_FRAME here */
1369         pushl_cfi %eax
1370         SAVE_ALL
1371         xorl %edx,%edx          # zero error code
1372         movl %esp,%eax          # pt_regs pointer
1373         call do_nmi
1374         jmp restore_all_notrace
1375         CFI_ENDPROC
1376
1377 nmi_stack_fixup:
1378         RING0_INT_FRAME
1379         FIX_STACK 12, nmi_stack_correct, 1
1380         jmp nmi_stack_correct
1381
1382 nmi_debug_stack_check:
1383         /* We have a RING0_INT_FRAME here */
1384         cmpw $__KERNEL_CS,16(%esp)
1385         jne nmi_stack_correct
1386         cmpl $debug,(%esp)
1387         jb nmi_stack_correct
1388         cmpl $debug_esp_fix_insn,(%esp)
1389         ja nmi_stack_correct
1390         FIX_STACK 24, nmi_stack_correct, 1
1391         jmp nmi_stack_correct
1392
1393 #ifdef CONFIG_X86_ESPFIX32
1394 nmi_espfix_stack:
1395         /* We have a RING0_INT_FRAME here.
1396          *
1397          * create the pointer to lss back
1398          */
1399         pushl_cfi %ss
1400         pushl_cfi %esp
1401         addl $4, (%esp)
1402         /* copy the iret frame of 12 bytes */
1403         .rept 3
1404         pushl_cfi 16(%esp)
1405         .endr
1406         pushl_cfi %eax
1407         SAVE_ALL
1408         FIXUP_ESPFIX_STACK              # %eax == %esp
1409         xorl %edx,%edx                  # zero error code
1410         call do_nmi
1411         RESTORE_REGS
1412         lss 12+4(%esp), %esp            # back to espfix stack
1413         CFI_ADJUST_CFA_OFFSET -24
1414         jmp irq_return
1415 #endif
1416         CFI_ENDPROC
1417 END(nmi)
1418
1419 ENTRY(int3)
1420         RING0_INT_FRAME
1421         ASM_CLAC
1422         pushl_cfi $-1                   # mark this as an int
1423         SAVE_ALL
1424         TRACE_IRQS_OFF
1425         xorl %edx,%edx          # zero error code
1426         movl %esp,%eax          # pt_regs pointer
1427         call do_int3
1428         jmp ret_from_exception
1429         CFI_ENDPROC
1430 END(int3)
1431
1432 ENTRY(general_protection)
1433         RING0_EC_FRAME
1434         pushl_cfi $do_general_protection
1435         jmp error_code
1436         CFI_ENDPROC
1437 END(general_protection)
1438
1439 #ifdef CONFIG_KVM_GUEST
1440 ENTRY(async_page_fault)
1441         RING0_EC_FRAME
1442         ASM_CLAC
1443         pushl_cfi $do_async_page_fault
1444         jmp error_code
1445         CFI_ENDPROC
1446 END(async_page_fault)
1447 #endif
1448