x86/entry/32: Switch INT80 to the new C syscall path
[linux-2.6-microblaze.git] / arch / x86 / entry / entry_32.S
1 /*
2  *  Copyright (C) 1991,1992  Linus Torvalds
3  *
4  * entry_32.S contains the system-call and low-level fault and trap handling routines.
5  *
6  * Stack layout while running C code:
7  *      ptrace needs to have all registers on the stack.
8  *      If the order here is changed, it needs to be
9  *      updated in fork.c:copy_process(), signal.c:do_signal(),
10  *      ptrace.c and ptrace.h
11  *
12  *       0(%esp) - %ebx
13  *       4(%esp) - %ecx
14  *       8(%esp) - %edx
15  *       C(%esp) - %esi
16  *      10(%esp) - %edi
17  *      14(%esp) - %ebp
18  *      18(%esp) - %eax
19  *      1C(%esp) - %ds
20  *      20(%esp) - %es
21  *      24(%esp) - %fs
22  *      28(%esp) - %gs          saved iff !CONFIG_X86_32_LAZY_GS
23  *      2C(%esp) - orig_eax
24  *      30(%esp) - %eip
25  *      34(%esp) - %cs
26  *      38(%esp) - %eflags
27  *      3C(%esp) - %oldesp
28  *      40(%esp) - %oldss
29  */
30
31 #include <linux/linkage.h>
32 #include <linux/err.h>
33 #include <asm/thread_info.h>
34 #include <asm/irqflags.h>
35 #include <asm/errno.h>
36 #include <asm/segment.h>
37 #include <asm/smp.h>
38 #include <asm/page_types.h>
39 #include <asm/percpu.h>
40 #include <asm/processor-flags.h>
41 #include <asm/ftrace.h>
42 #include <asm/irq_vectors.h>
43 #include <asm/cpufeature.h>
44 #include <asm/alternative-asm.h>
45 #include <asm/asm.h>
46 #include <asm/smap.h>
47
48         .section .entry.text, "ax"
49
50 /*
51  * We use macros for low-level operations which need to be overridden
52  * for paravirtualization.  The following will never clobber any registers:
53  *   INTERRUPT_RETURN (aka. "iret")
54  *   GET_CR0_INTO_EAX (aka. "movl %cr0, %eax")
55  *   ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
56  *
57  * For DISABLE_INTERRUPTS/ENABLE_INTERRUPTS (aka "cli"/"sti"), you must
58  * specify what registers can be overwritten (CLBR_NONE, CLBR_EAX/EDX/ECX/ANY).
59  * Allowing a register to be clobbered can shrink the paravirt replacement
60  * enough to patch inline, increasing performance.
61  */
62
63 #ifdef CONFIG_PREEMPT
64 # define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
65 #else
66 # define preempt_stop(clobbers)
67 # define resume_kernel          restore_all
68 #endif
69
70 .macro TRACE_IRQS_IRET
71 #ifdef CONFIG_TRACE_IRQFLAGS
72         testl   $X86_EFLAGS_IF, PT_EFLAGS(%esp)     # interrupts off?
73         jz      1f
74         TRACE_IRQS_ON
75 1:
76 #endif
77 .endm
78
79 /*
80  * User gs save/restore
81  *
82  * %gs is used for userland TLS and kernel only uses it for stack
83  * canary which is required to be at %gs:20 by gcc.  Read the comment
84  * at the top of stackprotector.h for more info.
85  *
86  * Local labels 98 and 99 are used.
87  */
88 #ifdef CONFIG_X86_32_LAZY_GS
89
90  /* unfortunately push/pop can't be no-op */
91 .macro PUSH_GS
92         pushl   $0
93 .endm
94 .macro POP_GS pop=0
95         addl    $(4 + \pop), %esp
96 .endm
97 .macro POP_GS_EX
98 .endm
99
100  /* all the rest are no-op */
101 .macro PTGS_TO_GS
102 .endm
103 .macro PTGS_TO_GS_EX
104 .endm
105 .macro GS_TO_REG reg
106 .endm
107 .macro REG_TO_PTGS reg
108 .endm
109 .macro SET_KERNEL_GS reg
110 .endm
111
112 #else   /* CONFIG_X86_32_LAZY_GS */
113
114 .macro PUSH_GS
115         pushl   %gs
116 .endm
117
118 .macro POP_GS pop=0
119 98:     popl    %gs
120   .if \pop <> 0
121         add     $\pop, %esp
122   .endif
123 .endm
124 .macro POP_GS_EX
125 .pushsection .fixup, "ax"
126 99:     movl    $0, (%esp)
127         jmp     98b
128 .popsection
129         _ASM_EXTABLE(98b, 99b)
130 .endm
131
132 .macro PTGS_TO_GS
133 98:     mov     PT_GS(%esp), %gs
134 .endm
135 .macro PTGS_TO_GS_EX
136 .pushsection .fixup, "ax"
137 99:     movl    $0, PT_GS(%esp)
138         jmp     98b
139 .popsection
140         _ASM_EXTABLE(98b, 99b)
141 .endm
142
143 .macro GS_TO_REG reg
144         movl    %gs, \reg
145 .endm
146 .macro REG_TO_PTGS reg
147         movl    \reg, PT_GS(%esp)
148 .endm
149 .macro SET_KERNEL_GS reg
150         movl    $(__KERNEL_STACK_CANARY), \reg
151         movl    \reg, %gs
152 .endm
153
154 #endif /* CONFIG_X86_32_LAZY_GS */
155
156 .macro SAVE_ALL pt_regs_ax=%eax
157         cld
158         PUSH_GS
159         pushl   %fs
160         pushl   %es
161         pushl   %ds
162         pushl   \pt_regs_ax
163         pushl   %ebp
164         pushl   %edi
165         pushl   %esi
166         pushl   %edx
167         pushl   %ecx
168         pushl   %ebx
169         movl    $(__USER_DS), %edx
170         movl    %edx, %ds
171         movl    %edx, %es
172         movl    $(__KERNEL_PERCPU), %edx
173         movl    %edx, %fs
174         SET_KERNEL_GS %edx
175 .endm
176
177 .macro RESTORE_INT_REGS
178         popl    %ebx
179         popl    %ecx
180         popl    %edx
181         popl    %esi
182         popl    %edi
183         popl    %ebp
184         popl    %eax
185 .endm
186
187 .macro RESTORE_REGS pop=0
188         RESTORE_INT_REGS
189 1:      popl    %ds
190 2:      popl    %es
191 3:      popl    %fs
192         POP_GS \pop
193 .pushsection .fixup, "ax"
194 4:      movl    $0, (%esp)
195         jmp     1b
196 5:      movl    $0, (%esp)
197         jmp     2b
198 6:      movl    $0, (%esp)
199         jmp     3b
200 .popsection
201         _ASM_EXTABLE(1b, 4b)
202         _ASM_EXTABLE(2b, 5b)
203         _ASM_EXTABLE(3b, 6b)
204         POP_GS_EX
205 .endm
206
207 ENTRY(ret_from_fork)
208         pushl   %eax
209         call    schedule_tail
210         GET_THREAD_INFO(%ebp)
211         popl    %eax
212         pushl   $0x0202                         # Reset kernel eflags
213         popfl
214
215         /* When we fork, we trace the syscall return in the child, too. */
216         movl    %esp, %eax
217         call    syscall_return_slowpath
218         jmp     restore_all
219 END(ret_from_fork)
220
221 ENTRY(ret_from_kernel_thread)
222         pushl   %eax
223         call    schedule_tail
224         GET_THREAD_INFO(%ebp)
225         popl    %eax
226         pushl   $0x0202                         # Reset kernel eflags
227         popfl
228         movl    PT_EBP(%esp), %eax
229         call    *PT_EBX(%esp)
230         movl    $0, PT_EAX(%esp)
231
232         /*
233          * Kernel threads return to userspace as if returning from a syscall.
234          * We should check whether anything actually uses this path and, if so,
235          * consider switching it over to ret_from_fork.
236          */
237         movl    %esp, %eax
238         call    syscall_return_slowpath
239         jmp     restore_all
240 ENDPROC(ret_from_kernel_thread)
241
242 /*
243  * Return to user mode is not as complex as all this looks,
244  * but we want the default path for a system call return to
245  * go as quickly as possible which is why some of this is
246  * less clear than it otherwise should be.
247  */
248
249         # userspace resumption stub bypassing syscall exit tracing
250         ALIGN
251 ret_from_exception:
252         preempt_stop(CLBR_ANY)
253 ret_from_intr:
254         GET_THREAD_INFO(%ebp)
255 #ifdef CONFIG_VM86
256         movl    PT_EFLAGS(%esp), %eax           # mix EFLAGS and CS
257         movb    PT_CS(%esp), %al
258         andl    $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
259 #else
260         /*
261          * We can be coming here from child spawned by kernel_thread().
262          */
263         movl    PT_CS(%esp), %eax
264         andl    $SEGMENT_RPL_MASK, %eax
265 #endif
266         cmpl    $USER_RPL, %eax
267         jb      resume_kernel                   # not returning to v8086 or userspace
268
269 ENTRY(resume_userspace)
270         DISABLE_INTERRUPTS(CLBR_ANY)
271         TRACE_IRQS_OFF
272         movl    %esp, %eax
273         call    prepare_exit_to_usermode
274         jmp     restore_all
275 END(ret_from_exception)
276
277 #ifdef CONFIG_PREEMPT
278 ENTRY(resume_kernel)
279         DISABLE_INTERRUPTS(CLBR_ANY)
280 need_resched:
281         cmpl    $0, PER_CPU_VAR(__preempt_count)
282         jnz     restore_all
283         testl   $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
284         jz      restore_all
285         call    preempt_schedule_irq
286         jmp     need_resched
287 END(resume_kernel)
288 #endif
289
290 /*
291  * SYSENTER_RETURN points to after the SYSENTER instruction
292  * in the vsyscall page.  See vsyscall-sysentry.S, which defines
293  * the symbol.
294  */
295
296         # SYSENTER  call handler stub
297 ENTRY(entry_SYSENTER_32)
298         movl    TSS_sysenter_sp0(%esp), %esp
299 sysenter_past_esp:
300         /*
301          * Interrupts are disabled here, but we can't trace it until
302          * enough kernel state to call TRACE_IRQS_OFF can be called - but
303          * we immediately enable interrupts at that point anyway.
304          */
305         pushl   $__USER_DS
306         pushl   %ebp
307         pushfl
308         orl     $X86_EFLAGS_IF, (%esp)
309         pushl   $__USER_CS
310         /*
311          * Push current_thread_info()->sysenter_return to the stack.
312          * A tiny bit of offset fixup is necessary: TI_sysenter_return
313          * is relative to thread_info, which is at the bottom of the
314          * kernel stack page.  4*4 means the 4 words pushed above;
315          * TOP_OF_KERNEL_STACK_PADDING takes us to the top of the stack;
316          * and THREAD_SIZE takes us to the bottom.
317          */
318         pushl   ((TI_sysenter_return) - THREAD_SIZE + TOP_OF_KERNEL_STACK_PADDING + 4*4)(%esp)
319
320         pushl   %eax
321         SAVE_ALL
322         ENABLE_INTERRUPTS(CLBR_NONE)
323
324 /*
325  * Load the potential sixth argument from user stack.
326  * Careful about security.
327  */
328         cmpl    $__PAGE_OFFSET-3, %ebp
329         jae     syscall_fault
330         ASM_STAC
331 1:      movl    (%ebp), %ebp
332         ASM_CLAC
333         movl    %ebp, PT_EBP(%esp)
334         _ASM_EXTABLE(1b, syscall_fault)
335
336         GET_THREAD_INFO(%ebp)
337
338         testl   $_TIF_WORK_SYSCALL_ENTRY, TI_flags(%ebp)
339         jnz     syscall_trace_entry
340 sysenter_do_call:
341         cmpl    $(NR_syscalls), %eax
342         jae     sysenter_badsys
343         call    *sys_call_table(, %eax, 4)
344 sysenter_after_call:
345         movl    %eax, PT_EAX(%esp)
346         LOCKDEP_SYS_EXIT
347         DISABLE_INTERRUPTS(CLBR_ANY)
348         TRACE_IRQS_OFF
349         movl    TI_flags(%ebp), %ecx
350         testl   $_TIF_ALLWORK_MASK, %ecx
351         jnz     syscall_exit_work_irqs_off
352 sysenter_exit:
353 /* if something modifies registers it must also disable sysexit */
354         movl    PT_EIP(%esp), %edx
355         movl    PT_OLDESP(%esp), %ecx
356         xorl    %ebp, %ebp
357         TRACE_IRQS_ON
358 1:      mov     PT_FS(%esp), %fs
359         PTGS_TO_GS
360         ENABLE_INTERRUPTS_SYSEXIT
361
362 .pushsection .fixup, "ax"
363 2:      movl    $0, PT_FS(%esp)
364         jmp     1b
365 .popsection
366         _ASM_EXTABLE(1b, 2b)
367         PTGS_TO_GS_EX
368 ENDPROC(entry_SYSENTER_32)
369
370         # system call handler stub
371 ENTRY(entry_INT80_32)
372         ASM_CLAC
373         pushl   %eax                    /* pt_regs->orig_ax */
374         SAVE_ALL pt_regs_ax=$-ENOSYS    /* save rest, load -ENOSYS into ax */
375
376         /*
377          * User mode is traced as though IRQs are on, and the interrupt gate
378          * turned them off.
379          */
380         TRACE_IRQS_OFF
381
382         movl    %esp, %eax
383         call    do_int80_syscall_32
384
385 restore_all:
386         TRACE_IRQS_IRET
387 restore_all_notrace:
388 #ifdef CONFIG_X86_ESPFIX32
389         movl    PT_EFLAGS(%esp), %eax           # mix EFLAGS, SS and CS
390         /*
391          * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
392          * are returning to the kernel.
393          * See comments in process.c:copy_thread() for details.
394          */
395         movb    PT_OLDSS(%esp), %ah
396         movb    PT_CS(%esp), %al
397         andl    $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
398         cmpl    $((SEGMENT_LDT << 8) | USER_RPL), %eax
399         je ldt_ss                               # returning to user-space with LDT SS
400 #endif
401 restore_nocheck:
402         RESTORE_REGS 4                          # skip orig_eax/error_code
403 irq_return:
404         INTERRUPT_RETURN
405 .section .fixup, "ax"
406 ENTRY(iret_exc  )
407         pushl   $0                              # no error code
408         pushl   $do_iret_error
409         jmp     error_code
410 .previous
411         _ASM_EXTABLE(irq_return, iret_exc)
412
413 #ifdef CONFIG_X86_ESPFIX32
414 ldt_ss:
415 #ifdef CONFIG_PARAVIRT
416         /*
417          * The kernel can't run on a non-flat stack if paravirt mode
418          * is active.  Rather than try to fixup the high bits of
419          * ESP, bypass this code entirely.  This may break DOSemu
420          * and/or Wine support in a paravirt VM, although the option
421          * is still available to implement the setting of the high
422          * 16-bits in the INTERRUPT_RETURN paravirt-op.
423          */
424         cmpl    $0, pv_info+PARAVIRT_enabled
425         jne     restore_nocheck
426 #endif
427
428 /*
429  * Setup and switch to ESPFIX stack
430  *
431  * We're returning to userspace with a 16 bit stack. The CPU will not
432  * restore the high word of ESP for us on executing iret... This is an
433  * "official" bug of all the x86-compatible CPUs, which we can work
434  * around to make dosemu and wine happy. We do this by preloading the
435  * high word of ESP with the high word of the userspace ESP while
436  * compensating for the offset by changing to the ESPFIX segment with
437  * a base address that matches for the difference.
438  */
439 #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
440         mov     %esp, %edx                      /* load kernel esp */
441         mov     PT_OLDESP(%esp), %eax           /* load userspace esp */
442         mov     %dx, %ax                        /* eax: new kernel esp */
443         sub     %eax, %edx                      /* offset (low word is 0) */
444         shr     $16, %edx
445         mov     %dl, GDT_ESPFIX_SS + 4          /* bits 16..23 */
446         mov     %dh, GDT_ESPFIX_SS + 7          /* bits 24..31 */
447         pushl   $__ESPFIX_SS
448         pushl   %eax                            /* new kernel esp */
449         /*
450          * Disable interrupts, but do not irqtrace this section: we
451          * will soon execute iret and the tracer was already set to
452          * the irqstate after the IRET:
453          */
454         DISABLE_INTERRUPTS(CLBR_EAX)
455         lss     (%esp), %esp                    /* switch to espfix segment */
456         jmp     restore_nocheck
457 #endif
458 ENDPROC(entry_INT80_32)
459
460         # perform syscall exit tracing
461         ALIGN
462 syscall_trace_entry:
463         movl    $-ENOSYS, PT_EAX(%esp)
464         movl    %esp, %eax
465         call    syscall_trace_enter
466         /* What it returned is what we'll actually use.  */
467         cmpl    $(NR_syscalls), %eax
468         jnae    syscall_call
469         jmp     syscall_exit
470 END(syscall_trace_entry)
471
472         # perform syscall exit tracing
473         ALIGN
474 syscall_exit_work_irqs_off:
475         TRACE_IRQS_ON
476         ENABLE_INTERRUPTS(CLBR_ANY)
477
478 syscall_exit_work:
479         movl    %esp, %eax
480         call    syscall_return_slowpath
481         jmp     restore_all
482 END(syscall_exit_work)
483
484 syscall_fault:
485         ASM_CLAC
486         GET_THREAD_INFO(%ebp)
487         movl    $-EFAULT, PT_EAX(%esp)
488         jmp     resume_userspace
489 END(syscall_fault)
490
491 sysenter_badsys:
492         movl    $-ENOSYS, %eax
493         jmp     sysenter_after_call
494 END(sysenter_badsys)
495
496 .macro FIXUP_ESPFIX_STACK
497 /*
498  * Switch back for ESPFIX stack to the normal zerobased stack
499  *
500  * We can't call C functions using the ESPFIX stack. This code reads
501  * the high word of the segment base from the GDT and swiches to the
502  * normal stack and adjusts ESP with the matching offset.
503  */
504 #ifdef CONFIG_X86_ESPFIX32
505         /* fixup the stack */
506         mov     GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
507         mov     GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
508         shl     $16, %eax
509         addl    %esp, %eax                      /* the adjusted stack pointer */
510         pushl   $__KERNEL_DS
511         pushl   %eax
512         lss     (%esp), %esp                    /* switch to the normal stack segment */
513 #endif
514 .endm
515 .macro UNWIND_ESPFIX_STACK
516 #ifdef CONFIG_X86_ESPFIX32
517         movl    %ss, %eax
518         /* see if on espfix stack */
519         cmpw    $__ESPFIX_SS, %ax
520         jne     27f
521         movl    $__KERNEL_DS, %eax
522         movl    %eax, %ds
523         movl    %eax, %es
524         /* switch to normal stack */
525         FIXUP_ESPFIX_STACK
526 27:
527 #endif
528 .endm
529
530 /*
531  * Build the entry stubs with some assembler magic.
532  * We pack 1 stub into every 8-byte block.
533  */
534         .align 8
535 ENTRY(irq_entries_start)
536     vector=FIRST_EXTERNAL_VECTOR
537     .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
538         pushl   $(~vector+0x80)                 /* Note: always in signed byte range */
539     vector=vector+1
540         jmp     common_interrupt
541         .align  8
542     .endr
543 END(irq_entries_start)
544
545 /*
546  * the CPU automatically disables interrupts when executing an IRQ vector,
547  * so IRQ-flags tracing has to follow that:
548  */
549         .p2align CONFIG_X86_L1_CACHE_SHIFT
550 common_interrupt:
551         ASM_CLAC
552         addl    $-0x80, (%esp)                  /* Adjust vector into the [-256, -1] range */
553         SAVE_ALL
554         TRACE_IRQS_OFF
555         movl    %esp, %eax
556         call    do_IRQ
557         jmp     ret_from_intr
558 ENDPROC(common_interrupt)
559
560 #define BUILD_INTERRUPT3(name, nr, fn)  \
561 ENTRY(name)                             \
562         ASM_CLAC;                       \
563         pushl   $~(nr);                 \
564         SAVE_ALL;                       \
565         TRACE_IRQS_OFF                  \
566         movl    %esp, %eax;             \
567         call    fn;                     \
568         jmp     ret_from_intr;          \
569 ENDPROC(name)
570
571
572 #ifdef CONFIG_TRACING
573 # define TRACE_BUILD_INTERRUPT(name, nr)        BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
574 #else
575 # define TRACE_BUILD_INTERRUPT(name, nr)
576 #endif
577
578 #define BUILD_INTERRUPT(name, nr)               \
579         BUILD_INTERRUPT3(name, nr, smp_##name); \
580         TRACE_BUILD_INTERRUPT(name, nr)
581
582 /* The include is where all of the SMP etc. interrupts come from */
583 #include <asm/entry_arch.h>
584
585 ENTRY(coprocessor_error)
586         ASM_CLAC
587         pushl   $0
588         pushl   $do_coprocessor_error
589         jmp     error_code
590 END(coprocessor_error)
591
592 ENTRY(simd_coprocessor_error)
593         ASM_CLAC
594         pushl   $0
595 #ifdef CONFIG_X86_INVD_BUG
596         /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
597         ALTERNATIVE "pushl      $do_general_protection",        \
598                     "pushl      $do_simd_coprocessor_error",    \
599                     X86_FEATURE_XMM
600 #else
601         pushl   $do_simd_coprocessor_error
602 #endif
603         jmp     error_code
604 END(simd_coprocessor_error)
605
606 ENTRY(device_not_available)
607         ASM_CLAC
608         pushl   $-1                             # mark this as an int
609         pushl   $do_device_not_available
610         jmp     error_code
611 END(device_not_available)
612
613 #ifdef CONFIG_PARAVIRT
614 ENTRY(native_iret)
615         iret
616         _ASM_EXTABLE(native_iret, iret_exc)
617 END(native_iret)
618
619 ENTRY(native_irq_enable_sysexit)
620         sti
621         sysexit
622 END(native_irq_enable_sysexit)
623 #endif
624
625 ENTRY(overflow)
626         ASM_CLAC
627         pushl   $0
628         pushl   $do_overflow
629         jmp     error_code
630 END(overflow)
631
632 ENTRY(bounds)
633         ASM_CLAC
634         pushl   $0
635         pushl   $do_bounds
636         jmp     error_code
637 END(bounds)
638
639 ENTRY(invalid_op)
640         ASM_CLAC
641         pushl   $0
642         pushl   $do_invalid_op
643         jmp     error_code
644 END(invalid_op)
645
646 ENTRY(coprocessor_segment_overrun)
647         ASM_CLAC
648         pushl   $0
649         pushl   $do_coprocessor_segment_overrun
650         jmp     error_code
651 END(coprocessor_segment_overrun)
652
653 ENTRY(invalid_TSS)
654         ASM_CLAC
655         pushl   $do_invalid_TSS
656         jmp     error_code
657 END(invalid_TSS)
658
659 ENTRY(segment_not_present)
660         ASM_CLAC
661         pushl   $do_segment_not_present
662         jmp     error_code
663 END(segment_not_present)
664
665 ENTRY(stack_segment)
666         ASM_CLAC
667         pushl   $do_stack_segment
668         jmp     error_code
669 END(stack_segment)
670
671 ENTRY(alignment_check)
672         ASM_CLAC
673         pushl   $do_alignment_check
674         jmp     error_code
675 END(alignment_check)
676
677 ENTRY(divide_error)
678         ASM_CLAC
679         pushl   $0                              # no error code
680         pushl   $do_divide_error
681         jmp     error_code
682 END(divide_error)
683
684 #ifdef CONFIG_X86_MCE
685 ENTRY(machine_check)
686         ASM_CLAC
687         pushl   $0
688         pushl   machine_check_vector
689         jmp     error_code
690 END(machine_check)
691 #endif
692
693 ENTRY(spurious_interrupt_bug)
694         ASM_CLAC
695         pushl   $0
696         pushl   $do_spurious_interrupt_bug
697         jmp     error_code
698 END(spurious_interrupt_bug)
699
700 #ifdef CONFIG_XEN
701 /*
702  * Xen doesn't set %esp to be precisely what the normal SYSENTER
703  * entry point expects, so fix it up before using the normal path.
704  */
705 ENTRY(xen_sysenter_target)
706         addl    $5*4, %esp                      /* remove xen-provided frame */
707         jmp     sysenter_past_esp
708
709 ENTRY(xen_hypervisor_callback)
710         pushl   $-1                             /* orig_ax = -1 => not a system call */
711         SAVE_ALL
712         TRACE_IRQS_OFF
713
714         /*
715          * Check to see if we got the event in the critical
716          * region in xen_iret_direct, after we've reenabled
717          * events and checked for pending events.  This simulates
718          * iret instruction's behaviour where it delivers a
719          * pending interrupt when enabling interrupts:
720          */
721         movl    PT_EIP(%esp), %eax
722         cmpl    $xen_iret_start_crit, %eax
723         jb      1f
724         cmpl    $xen_iret_end_crit, %eax
725         jae     1f
726
727         jmp     xen_iret_crit_fixup
728
729 ENTRY(xen_do_upcall)
730 1:      mov     %esp, %eax
731         call    xen_evtchn_do_upcall
732 #ifndef CONFIG_PREEMPT
733         call    xen_maybe_preempt_hcall
734 #endif
735         jmp     ret_from_intr
736 ENDPROC(xen_hypervisor_callback)
737
738 /*
739  * Hypervisor uses this for application faults while it executes.
740  * We get here for two reasons:
741  *  1. Fault while reloading DS, ES, FS or GS
742  *  2. Fault while executing IRET
743  * Category 1 we fix up by reattempting the load, and zeroing the segment
744  * register if the load fails.
745  * Category 2 we fix up by jumping to do_iret_error. We cannot use the
746  * normal Linux return path in this case because if we use the IRET hypercall
747  * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
748  * We distinguish between categories by maintaining a status value in EAX.
749  */
750 ENTRY(xen_failsafe_callback)
751         pushl   %eax
752         movl    $1, %eax
753 1:      mov     4(%esp), %ds
754 2:      mov     8(%esp), %es
755 3:      mov     12(%esp), %fs
756 4:      mov     16(%esp), %gs
757         /* EAX == 0 => Category 1 (Bad segment)
758            EAX != 0 => Category 2 (Bad IRET) */
759         testl   %eax, %eax
760         popl    %eax
761         lea     16(%esp), %esp
762         jz      5f
763         jmp     iret_exc
764 5:      pushl   $-1                             /* orig_ax = -1 => not a system call */
765         SAVE_ALL
766         jmp     ret_from_exception
767
768 .section .fixup, "ax"
769 6:      xorl    %eax, %eax
770         movl    %eax, 4(%esp)
771         jmp     1b
772 7:      xorl    %eax, %eax
773         movl    %eax, 8(%esp)
774         jmp     2b
775 8:      xorl    %eax, %eax
776         movl    %eax, 12(%esp)
777         jmp     3b
778 9:      xorl    %eax, %eax
779         movl    %eax, 16(%esp)
780         jmp     4b
781 .previous
782         _ASM_EXTABLE(1b, 6b)
783         _ASM_EXTABLE(2b, 7b)
784         _ASM_EXTABLE(3b, 8b)
785         _ASM_EXTABLE(4b, 9b)
786 ENDPROC(xen_failsafe_callback)
787
788 BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
789                 xen_evtchn_do_upcall)
790
791 #endif /* CONFIG_XEN */
792
793 #if IS_ENABLED(CONFIG_HYPERV)
794
795 BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
796         hyperv_vector_handler)
797
798 #endif /* CONFIG_HYPERV */
799
800 #ifdef CONFIG_FUNCTION_TRACER
801 #ifdef CONFIG_DYNAMIC_FTRACE
802
803 ENTRY(mcount)
804         ret
805 END(mcount)
806
807 ENTRY(ftrace_caller)
808         pushl   %eax
809         pushl   %ecx
810         pushl   %edx
811         pushl   $0                              /* Pass NULL as regs pointer */
812         movl    4*4(%esp), %eax
813         movl    0x4(%ebp), %edx
814         movl    function_trace_op, %ecx
815         subl    $MCOUNT_INSN_SIZE, %eax
816
817 .globl ftrace_call
818 ftrace_call:
819         call    ftrace_stub
820
821         addl    $4, %esp                        /* skip NULL pointer */
822         popl    %edx
823         popl    %ecx
824         popl    %eax
825 ftrace_ret:
826 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
827 .globl ftrace_graph_call
828 ftrace_graph_call:
829         jmp     ftrace_stub
830 #endif
831
832 .globl ftrace_stub
833 ftrace_stub:
834         ret
835 END(ftrace_caller)
836
837 ENTRY(ftrace_regs_caller)
838         pushf   /* push flags before compare (in cs location) */
839
840         /*
841          * i386 does not save SS and ESP when coming from kernel.
842          * Instead, to get sp, &regs->sp is used (see ptrace.h).
843          * Unfortunately, that means eflags must be at the same location
844          * as the current return ip is. We move the return ip into the
845          * ip location, and move flags into the return ip location.
846          */
847         pushl   4(%esp)                         /* save return ip into ip slot */
848
849         pushl   $0                              /* Load 0 into orig_ax */
850         pushl   %gs
851         pushl   %fs
852         pushl   %es
853         pushl   %ds
854         pushl   %eax
855         pushl   %ebp
856         pushl   %edi
857         pushl   %esi
858         pushl   %edx
859         pushl   %ecx
860         pushl   %ebx
861
862         movl    13*4(%esp), %eax                /* Get the saved flags */
863         movl    %eax, 14*4(%esp)                /* Move saved flags into regs->flags location */
864                                                 /* clobbering return ip */
865         movl    $__KERNEL_CS, 13*4(%esp)
866
867         movl    12*4(%esp), %eax                /* Load ip (1st parameter) */
868         subl    $MCOUNT_INSN_SIZE, %eax         /* Adjust ip */
869         movl    0x4(%ebp), %edx                 /* Load parent ip (2nd parameter) */
870         movl    function_trace_op, %ecx         /* Save ftrace_pos in 3rd parameter */
871         pushl   %esp                            /* Save pt_regs as 4th parameter */
872
873 GLOBAL(ftrace_regs_call)
874         call    ftrace_stub
875
876         addl    $4, %esp                        /* Skip pt_regs */
877         movl    14*4(%esp), %eax                /* Move flags back into cs */
878         movl    %eax, 13*4(%esp)                /* Needed to keep addl  from modifying flags */
879         movl    12*4(%esp), %eax                /* Get return ip from regs->ip */
880         movl    %eax, 14*4(%esp)                /* Put return ip back for ret */
881
882         popl    %ebx
883         popl    %ecx
884         popl    %edx
885         popl    %esi
886         popl    %edi
887         popl    %ebp
888         popl    %eax
889         popl    %ds
890         popl    %es
891         popl    %fs
892         popl    %gs
893         addl    $8, %esp                        /* Skip orig_ax and ip */
894         popf                                    /* Pop flags at end (no addl to corrupt flags) */
895         jmp     ftrace_ret
896
897         popf
898         jmp     ftrace_stub
899 #else /* ! CONFIG_DYNAMIC_FTRACE */
900
901 ENTRY(mcount)
902         cmpl    $__PAGE_OFFSET, %esp
903         jb      ftrace_stub                     /* Paging not enabled yet? */
904
905         cmpl    $ftrace_stub, ftrace_trace_function
906         jnz     trace
907 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
908         cmpl    $ftrace_stub, ftrace_graph_return
909         jnz     ftrace_graph_caller
910
911         cmpl    $ftrace_graph_entry_stub, ftrace_graph_entry
912         jnz     ftrace_graph_caller
913 #endif
914 .globl ftrace_stub
915 ftrace_stub:
916         ret
917
918         /* taken from glibc */
919 trace:
920         pushl   %eax
921         pushl   %ecx
922         pushl   %edx
923         movl    0xc(%esp), %eax
924         movl    0x4(%ebp), %edx
925         subl    $MCOUNT_INSN_SIZE, %eax
926
927         call    *ftrace_trace_function
928
929         popl    %edx
930         popl    %ecx
931         popl    %eax
932         jmp     ftrace_stub
933 END(mcount)
934 #endif /* CONFIG_DYNAMIC_FTRACE */
935 #endif /* CONFIG_FUNCTION_TRACER */
936
937 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
938 ENTRY(ftrace_graph_caller)
939         pushl   %eax
940         pushl   %ecx
941         pushl   %edx
942         movl    0xc(%esp), %eax
943         lea     0x4(%ebp), %edx
944         movl    (%ebp), %ecx
945         subl    $MCOUNT_INSN_SIZE, %eax
946         call    prepare_ftrace_return
947         popl    %edx
948         popl    %ecx
949         popl    %eax
950         ret
951 END(ftrace_graph_caller)
952
953 .globl return_to_handler
954 return_to_handler:
955         pushl   %eax
956         pushl   %edx
957         movl    %ebp, %eax
958         call    ftrace_return_to_handler
959         movl    %eax, %ecx
960         popl    %edx
961         popl    %eax
962         jmp     *%ecx
963 #endif
964
965 #ifdef CONFIG_TRACING
966 ENTRY(trace_page_fault)
967         ASM_CLAC
968         pushl   $trace_do_page_fault
969         jmp     error_code
970 END(trace_page_fault)
971 #endif
972
973 ENTRY(page_fault)
974         ASM_CLAC
975         pushl   $do_page_fault
976         ALIGN
977 error_code:
978         /* the function address is in %gs's slot on the stack */
979         pushl   %fs
980         pushl   %es
981         pushl   %ds
982         pushl   %eax
983         pushl   %ebp
984         pushl   %edi
985         pushl   %esi
986         pushl   %edx
987         pushl   %ecx
988         pushl   %ebx
989         cld
990         movl    $(__KERNEL_PERCPU), %ecx
991         movl    %ecx, %fs
992         UNWIND_ESPFIX_STACK
993         GS_TO_REG %ecx
994         movl    PT_GS(%esp), %edi               # get the function address
995         movl    PT_ORIG_EAX(%esp), %edx         # get the error code
996         movl    $-1, PT_ORIG_EAX(%esp)          # no syscall to restart
997         REG_TO_PTGS %ecx
998         SET_KERNEL_GS %ecx
999         movl    $(__USER_DS), %ecx
1000         movl    %ecx, %ds
1001         movl    %ecx, %es
1002         TRACE_IRQS_OFF
1003         movl    %esp, %eax                      # pt_regs pointer
1004         call    *%edi
1005         jmp     ret_from_exception
1006 END(page_fault)
1007
1008 /*
1009  * Debug traps and NMI can happen at the one SYSENTER instruction
1010  * that sets up the real kernel stack. Check here, since we can't
1011  * allow the wrong stack to be used.
1012  *
1013  * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
1014  * already pushed 3 words if it hits on the sysenter instruction:
1015  * eflags, cs and eip.
1016  *
1017  * We just load the right stack, and push the three (known) values
1018  * by hand onto the new stack - while updating the return eip past
1019  * the instruction that would have done it for sysenter.
1020  */
1021 .macro FIX_STACK offset ok label
1022         cmpw    $__KERNEL_CS, 4(%esp)
1023         jne     \ok
1024 \label:
1025         movl    TSS_sysenter_sp0 + \offset(%esp), %esp
1026         pushfl
1027         pushl   $__KERNEL_CS
1028         pushl   $sysenter_past_esp
1029 .endm
1030
1031 ENTRY(debug)
1032         ASM_CLAC
1033         cmpl    $entry_SYSENTER_32, (%esp)
1034         jne     debug_stack_correct
1035         FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
1036 debug_stack_correct:
1037         pushl   $-1                             # mark this as an int
1038         SAVE_ALL
1039         TRACE_IRQS_OFF
1040         xorl    %edx, %edx                      # error code 0
1041         movl    %esp, %eax                      # pt_regs pointer
1042         call    do_debug
1043         jmp     ret_from_exception
1044 END(debug)
1045
1046 /*
1047  * NMI is doubly nasty. It can happen _while_ we're handling
1048  * a debug fault, and the debug fault hasn't yet been able to
1049  * clear up the stack. So we first check whether we got  an
1050  * NMI on the sysenter entry path, but after that we need to
1051  * check whether we got an NMI on the debug path where the debug
1052  * fault happened on the sysenter path.
1053  */
1054 ENTRY(nmi)
1055         ASM_CLAC
1056 #ifdef CONFIG_X86_ESPFIX32
1057         pushl   %eax
1058         movl    %ss, %eax
1059         cmpw    $__ESPFIX_SS, %ax
1060         popl    %eax
1061         je      nmi_espfix_stack
1062 #endif
1063         cmpl    $entry_SYSENTER_32, (%esp)
1064         je      nmi_stack_fixup
1065         pushl   %eax
1066         movl    %esp, %eax
1067         /*
1068          * Do not access memory above the end of our stack page,
1069          * it might not exist.
1070          */
1071         andl    $(THREAD_SIZE-1), %eax
1072         cmpl    $(THREAD_SIZE-20), %eax
1073         popl    %eax
1074         jae     nmi_stack_correct
1075         cmpl    $entry_SYSENTER_32, 12(%esp)
1076         je      nmi_debug_stack_check
1077 nmi_stack_correct:
1078         pushl   %eax
1079         SAVE_ALL
1080         xorl    %edx, %edx                      # zero error code
1081         movl    %esp, %eax                      # pt_regs pointer
1082         call    do_nmi
1083         jmp     restore_all_notrace
1084
1085 nmi_stack_fixup:
1086         FIX_STACK 12, nmi_stack_correct, 1
1087         jmp     nmi_stack_correct
1088
1089 nmi_debug_stack_check:
1090         cmpw    $__KERNEL_CS, 16(%esp)
1091         jne     nmi_stack_correct
1092         cmpl    $debug, (%esp)
1093         jb      nmi_stack_correct
1094         cmpl    $debug_esp_fix_insn, (%esp)
1095         ja      nmi_stack_correct
1096         FIX_STACK 24, nmi_stack_correct, 1
1097         jmp     nmi_stack_correct
1098
1099 #ifdef CONFIG_X86_ESPFIX32
1100 nmi_espfix_stack:
1101         /*
1102          * create the pointer to lss back
1103          */
1104         pushl   %ss
1105         pushl   %esp
1106         addl    $4, (%esp)
1107         /* copy the iret frame of 12 bytes */
1108         .rept 3
1109         pushl   16(%esp)
1110         .endr
1111         pushl   %eax
1112         SAVE_ALL
1113         FIXUP_ESPFIX_STACK                      # %eax == %esp
1114         xorl    %edx, %edx                      # zero error code
1115         call    do_nmi
1116         RESTORE_REGS
1117         lss     12+4(%esp), %esp                # back to espfix stack
1118         jmp     irq_return
1119 #endif
1120 END(nmi)
1121
1122 ENTRY(int3)
1123         ASM_CLAC
1124         pushl   $-1                             # mark this as an int
1125         SAVE_ALL
1126         TRACE_IRQS_OFF
1127         xorl    %edx, %edx                      # zero error code
1128         movl    %esp, %eax                      # pt_regs pointer
1129         call    do_int3
1130         jmp     ret_from_exception
1131 END(int3)
1132
1133 ENTRY(general_protection)
1134         pushl   $do_general_protection
1135         jmp     error_code
1136 END(general_protection)
1137
1138 #ifdef CONFIG_KVM_GUEST
1139 ENTRY(async_page_fault)
1140         ASM_CLAC
1141         pushl   $do_async_page_fault
1142         jmp     error_code
1143 END(async_page_fault)
1144 #endif