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