These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / 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         # preempt count == 0 + NEED_RS set?
282         cmpl    $0, PER_CPU_VAR(__preempt_count)
283 #ifndef CONFIG_PREEMPT_LAZY
284         jnz     restore_all
285 #else
286         jz test_int_off
287
288         # atleast preempt count == 0 ?
289         cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
290         jne restore_all
291
292         cmpl $0,TI_preempt_lazy_count(%ebp)     # non-zero preempt_lazy_count ?
293         jnz restore_all
294
295         testl $_TIF_NEED_RESCHED_LAZY, TI_flags(%ebp)
296         jz restore_all
297 test_int_off:
298 #endif
299         testl   $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
300         jz      restore_all
301         call    preempt_schedule_irq
302         jmp     need_resched
303 END(resume_kernel)
304 #endif
305
306         # SYSENTER  call handler stub
307 ENTRY(entry_SYSENTER_32)
308         movl    TSS_sysenter_sp0(%esp), %esp
309 sysenter_past_esp:
310         pushl   $__USER_DS              /* pt_regs->ss */
311         pushl   %ebp                    /* pt_regs->sp (stashed in bp) */
312         pushfl                          /* pt_regs->flags (except IF = 0) */
313         orl     $X86_EFLAGS_IF, (%esp)  /* Fix IF */
314         pushl   $__USER_CS              /* pt_regs->cs */
315         pushl   $0                      /* pt_regs->ip = 0 (placeholder) */
316         pushl   %eax                    /* pt_regs->orig_ax */
317         SAVE_ALL pt_regs_ax=$-ENOSYS    /* save rest */
318
319         /*
320          * User mode is traced as though IRQs are on, and SYSENTER
321          * turned them off.
322          */
323         TRACE_IRQS_OFF
324
325         movl    %esp, %eax
326         call    do_fast_syscall_32
327         /* XEN PV guests always use IRET path */
328         ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
329                     "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
330
331 /* Opportunistic SYSEXIT */
332         TRACE_IRQS_ON                   /* User mode traces as IRQs on. */
333         movl    PT_EIP(%esp), %edx      /* pt_regs->ip */
334         movl    PT_OLDESP(%esp), %ecx   /* pt_regs->sp */
335 1:      mov     PT_FS(%esp), %fs
336         PTGS_TO_GS
337         popl    %ebx                    /* pt_regs->bx */
338         addl    $2*4, %esp              /* skip pt_regs->cx and pt_regs->dx */
339         popl    %esi                    /* pt_regs->si */
340         popl    %edi                    /* pt_regs->di */
341         popl    %ebp                    /* pt_regs->bp */
342         popl    %eax                    /* pt_regs->ax */
343
344         /*
345          * Return back to the vDSO, which will pop ecx and edx.
346          * Don't bother with DS and ES (they already contain __USER_DS).
347          */
348         ENABLE_INTERRUPTS_SYSEXIT
349
350 .pushsection .fixup, "ax"
351 2:      movl    $0, PT_FS(%esp)
352         jmp     1b
353 .popsection
354         _ASM_EXTABLE(1b, 2b)
355         PTGS_TO_GS_EX
356 ENDPROC(entry_SYSENTER_32)
357
358         # system call handler stub
359 ENTRY(entry_INT80_32)
360         ASM_CLAC
361         pushl   %eax                    /* pt_regs->orig_ax */
362         SAVE_ALL pt_regs_ax=$-ENOSYS    /* save rest */
363
364         /*
365          * User mode is traced as though IRQs are on.  Unlike the 64-bit
366          * case, INT80 is a trap gate on 32-bit kernels, so interrupts
367          * are already on (unless user code is messing around with iopl).
368          */
369
370         movl    %esp, %eax
371         call    do_syscall_32_irqs_on
372 .Lsyscall_32_done:
373
374 restore_all:
375         TRACE_IRQS_IRET
376 restore_all_notrace:
377 #ifdef CONFIG_X86_ESPFIX32
378         movl    PT_EFLAGS(%esp), %eax           # mix EFLAGS, SS and CS
379         /*
380          * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
381          * are returning to the kernel.
382          * See comments in process.c:copy_thread() for details.
383          */
384         movb    PT_OLDSS(%esp), %ah
385         movb    PT_CS(%esp), %al
386         andl    $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
387         cmpl    $((SEGMENT_LDT << 8) | USER_RPL), %eax
388         je ldt_ss                               # returning to user-space with LDT SS
389 #endif
390 restore_nocheck:
391         RESTORE_REGS 4                          # skip orig_eax/error_code
392 irq_return:
393         INTERRUPT_RETURN
394 .section .fixup, "ax"
395 ENTRY(iret_exc  )
396         pushl   $0                              # no error code
397         pushl   $do_iret_error
398         jmp     error_code
399 .previous
400         _ASM_EXTABLE(irq_return, iret_exc)
401
402 #ifdef CONFIG_X86_ESPFIX32
403 ldt_ss:
404 #ifdef CONFIG_PARAVIRT
405         /*
406          * The kernel can't run on a non-flat stack if paravirt mode
407          * is active.  Rather than try to fixup the high bits of
408          * ESP, bypass this code entirely.  This may break DOSemu
409          * and/or Wine support in a paravirt VM, although the option
410          * is still available to implement the setting of the high
411          * 16-bits in the INTERRUPT_RETURN paravirt-op.
412          */
413         cmpl    $0, pv_info+PARAVIRT_enabled
414         jne     restore_nocheck
415 #endif
416
417 /*
418  * Setup and switch to ESPFIX stack
419  *
420  * We're returning to userspace with a 16 bit stack. The CPU will not
421  * restore the high word of ESP for us on executing iret... This is an
422  * "official" bug of all the x86-compatible CPUs, which we can work
423  * around to make dosemu and wine happy. We do this by preloading the
424  * high word of ESP with the high word of the userspace ESP while
425  * compensating for the offset by changing to the ESPFIX segment with
426  * a base address that matches for the difference.
427  */
428 #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
429         mov     %esp, %edx                      /* load kernel esp */
430         mov     PT_OLDESP(%esp), %eax           /* load userspace esp */
431         mov     %dx, %ax                        /* eax: new kernel esp */
432         sub     %eax, %edx                      /* offset (low word is 0) */
433         shr     $16, %edx
434         mov     %dl, GDT_ESPFIX_SS + 4          /* bits 16..23 */
435         mov     %dh, GDT_ESPFIX_SS + 7          /* bits 24..31 */
436         pushl   $__ESPFIX_SS
437         pushl   %eax                            /* new kernel esp */
438         /*
439          * Disable interrupts, but do not irqtrace this section: we
440          * will soon execute iret and the tracer was already set to
441          * the irqstate after the IRET:
442          */
443         DISABLE_INTERRUPTS(CLBR_EAX)
444         lss     (%esp), %esp                    /* switch to espfix segment */
445         jmp     restore_nocheck
446 #endif
447 ENDPROC(entry_INT80_32)
448
449 .macro FIXUP_ESPFIX_STACK
450 /*
451  * Switch back for ESPFIX stack to the normal zerobased stack
452  *
453  * We can't call C functions using the ESPFIX stack. This code reads
454  * the high word of the segment base from the GDT and swiches to the
455  * normal stack and adjusts ESP with the matching offset.
456  */
457 #ifdef CONFIG_X86_ESPFIX32
458         /* fixup the stack */
459         mov     GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
460         mov     GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
461         shl     $16, %eax
462         addl    %esp, %eax                      /* the adjusted stack pointer */
463         pushl   $__KERNEL_DS
464         pushl   %eax
465         lss     (%esp), %esp                    /* switch to the normal stack segment */
466 #endif
467 .endm
468 .macro UNWIND_ESPFIX_STACK
469 #ifdef CONFIG_X86_ESPFIX32
470         movl    %ss, %eax
471         /* see if on espfix stack */
472         cmpw    $__ESPFIX_SS, %ax
473         jne     27f
474         movl    $__KERNEL_DS, %eax
475         movl    %eax, %ds
476         movl    %eax, %es
477         /* switch to normal stack */
478         FIXUP_ESPFIX_STACK
479 27:
480 #endif
481 .endm
482
483 /*
484  * Build the entry stubs with some assembler magic.
485  * We pack 1 stub into every 8-byte block.
486  */
487         .align 8
488 ENTRY(irq_entries_start)
489     vector=FIRST_EXTERNAL_VECTOR
490     .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
491         pushl   $(~vector+0x80)                 /* Note: always in signed byte range */
492     vector=vector+1
493         jmp     common_interrupt
494         .align  8
495     .endr
496 END(irq_entries_start)
497
498 /*
499  * the CPU automatically disables interrupts when executing an IRQ vector,
500  * so IRQ-flags tracing has to follow that:
501  */
502         .p2align CONFIG_X86_L1_CACHE_SHIFT
503 common_interrupt:
504         ASM_CLAC
505         addl    $-0x80, (%esp)                  /* Adjust vector into the [-256, -1] range */
506         SAVE_ALL
507         TRACE_IRQS_OFF
508         movl    %esp, %eax
509         call    do_IRQ
510         jmp     ret_from_intr
511 ENDPROC(common_interrupt)
512
513 #define BUILD_INTERRUPT3(name, nr, fn)  \
514 ENTRY(name)                             \
515         ASM_CLAC;                       \
516         pushl   $~(nr);                 \
517         SAVE_ALL;                       \
518         TRACE_IRQS_OFF                  \
519         movl    %esp, %eax;             \
520         call    fn;                     \
521         jmp     ret_from_intr;          \
522 ENDPROC(name)
523
524
525 #ifdef CONFIG_TRACING
526 # define TRACE_BUILD_INTERRUPT(name, nr)        BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
527 #else
528 # define TRACE_BUILD_INTERRUPT(name, nr)
529 #endif
530
531 #define BUILD_INTERRUPT(name, nr)               \
532         BUILD_INTERRUPT3(name, nr, smp_##name); \
533         TRACE_BUILD_INTERRUPT(name, nr)
534
535 /* The include is where all of the SMP etc. interrupts come from */
536 #include <asm/entry_arch.h>
537
538 ENTRY(coprocessor_error)
539         ASM_CLAC
540         pushl   $0
541         pushl   $do_coprocessor_error
542         jmp     error_code
543 END(coprocessor_error)
544
545 ENTRY(simd_coprocessor_error)
546         ASM_CLAC
547         pushl   $0
548 #ifdef CONFIG_X86_INVD_BUG
549         /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
550         ALTERNATIVE "pushl      $do_general_protection",        \
551                     "pushl      $do_simd_coprocessor_error",    \
552                     X86_FEATURE_XMM
553 #else
554         pushl   $do_simd_coprocessor_error
555 #endif
556         jmp     error_code
557 END(simd_coprocessor_error)
558
559 ENTRY(device_not_available)
560         ASM_CLAC
561         pushl   $-1                             # mark this as an int
562         pushl   $do_device_not_available
563         jmp     error_code
564 END(device_not_available)
565
566 #ifdef CONFIG_PARAVIRT
567 ENTRY(native_iret)
568         iret
569         _ASM_EXTABLE(native_iret, iret_exc)
570 END(native_iret)
571
572 ENTRY(native_irq_enable_sysexit)
573         sti
574         sysexit
575 END(native_irq_enable_sysexit)
576 #endif
577
578 ENTRY(overflow)
579         ASM_CLAC
580         pushl   $0
581         pushl   $do_overflow
582         jmp     error_code
583 END(overflow)
584
585 ENTRY(bounds)
586         ASM_CLAC
587         pushl   $0
588         pushl   $do_bounds
589         jmp     error_code
590 END(bounds)
591
592 ENTRY(invalid_op)
593         ASM_CLAC
594         pushl   $0
595         pushl   $do_invalid_op
596         jmp     error_code
597 END(invalid_op)
598
599 ENTRY(coprocessor_segment_overrun)
600         ASM_CLAC
601         pushl   $0
602         pushl   $do_coprocessor_segment_overrun
603         jmp     error_code
604 END(coprocessor_segment_overrun)
605
606 ENTRY(invalid_TSS)
607         ASM_CLAC
608         pushl   $do_invalid_TSS
609         jmp     error_code
610 END(invalid_TSS)
611
612 ENTRY(segment_not_present)
613         ASM_CLAC
614         pushl   $do_segment_not_present
615         jmp     error_code
616 END(segment_not_present)
617
618 ENTRY(stack_segment)
619         ASM_CLAC
620         pushl   $do_stack_segment
621         jmp     error_code
622 END(stack_segment)
623
624 ENTRY(alignment_check)
625         ASM_CLAC
626         pushl   $do_alignment_check
627         jmp     error_code
628 END(alignment_check)
629
630 ENTRY(divide_error)
631         ASM_CLAC
632         pushl   $0                              # no error code
633         pushl   $do_divide_error
634         jmp     error_code
635 END(divide_error)
636
637 #ifdef CONFIG_X86_MCE
638 ENTRY(machine_check)
639         ASM_CLAC
640         pushl   $0
641         pushl   machine_check_vector
642         jmp     error_code
643 END(machine_check)
644 #endif
645
646 ENTRY(spurious_interrupt_bug)
647         ASM_CLAC
648         pushl   $0
649         pushl   $do_spurious_interrupt_bug
650         jmp     error_code
651 END(spurious_interrupt_bug)
652
653 #ifdef CONFIG_XEN
654 /*
655  * Xen doesn't set %esp to be precisely what the normal SYSENTER
656  * entry point expects, so fix it up before using the normal path.
657  */
658 ENTRY(xen_sysenter_target)
659         addl    $5*4, %esp                      /* remove xen-provided frame */
660         jmp     sysenter_past_esp
661
662 ENTRY(xen_hypervisor_callback)
663         pushl   $-1                             /* orig_ax = -1 => not a system call */
664         SAVE_ALL
665         TRACE_IRQS_OFF
666
667         /*
668          * Check to see if we got the event in the critical
669          * region in xen_iret_direct, after we've reenabled
670          * events and checked for pending events.  This simulates
671          * iret instruction's behaviour where it delivers a
672          * pending interrupt when enabling interrupts:
673          */
674         movl    PT_EIP(%esp), %eax
675         cmpl    $xen_iret_start_crit, %eax
676         jb      1f
677         cmpl    $xen_iret_end_crit, %eax
678         jae     1f
679
680         jmp     xen_iret_crit_fixup
681
682 ENTRY(xen_do_upcall)
683 1:      mov     %esp, %eax
684         call    xen_evtchn_do_upcall
685 #ifndef CONFIG_PREEMPT
686         call    xen_maybe_preempt_hcall
687 #endif
688         jmp     ret_from_intr
689 ENDPROC(xen_hypervisor_callback)
690
691 /*
692  * Hypervisor uses this for application faults while it executes.
693  * We get here for two reasons:
694  *  1. Fault while reloading DS, ES, FS or GS
695  *  2. Fault while executing IRET
696  * Category 1 we fix up by reattempting the load, and zeroing the segment
697  * register if the load fails.
698  * Category 2 we fix up by jumping to do_iret_error. We cannot use the
699  * normal Linux return path in this case because if we use the IRET hypercall
700  * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
701  * We distinguish between categories by maintaining a status value in EAX.
702  */
703 ENTRY(xen_failsafe_callback)
704         pushl   %eax
705         movl    $1, %eax
706 1:      mov     4(%esp), %ds
707 2:      mov     8(%esp), %es
708 3:      mov     12(%esp), %fs
709 4:      mov     16(%esp), %gs
710         /* EAX == 0 => Category 1 (Bad segment)
711            EAX != 0 => Category 2 (Bad IRET) */
712         testl   %eax, %eax
713         popl    %eax
714         lea     16(%esp), %esp
715         jz      5f
716         jmp     iret_exc
717 5:      pushl   $-1                             /* orig_ax = -1 => not a system call */
718         SAVE_ALL
719         jmp     ret_from_exception
720
721 .section .fixup, "ax"
722 6:      xorl    %eax, %eax
723         movl    %eax, 4(%esp)
724         jmp     1b
725 7:      xorl    %eax, %eax
726         movl    %eax, 8(%esp)
727         jmp     2b
728 8:      xorl    %eax, %eax
729         movl    %eax, 12(%esp)
730         jmp     3b
731 9:      xorl    %eax, %eax
732         movl    %eax, 16(%esp)
733         jmp     4b
734 .previous
735         _ASM_EXTABLE(1b, 6b)
736         _ASM_EXTABLE(2b, 7b)
737         _ASM_EXTABLE(3b, 8b)
738         _ASM_EXTABLE(4b, 9b)
739 ENDPROC(xen_failsafe_callback)
740
741 BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
742                 xen_evtchn_do_upcall)
743
744 #endif /* CONFIG_XEN */
745
746 #if IS_ENABLED(CONFIG_HYPERV)
747
748 BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
749         hyperv_vector_handler)
750
751 #endif /* CONFIG_HYPERV */
752
753 #ifdef CONFIG_FUNCTION_TRACER
754 #ifdef CONFIG_DYNAMIC_FTRACE
755
756 ENTRY(mcount)
757         ret
758 END(mcount)
759
760 ENTRY(ftrace_caller)
761         pushl   %eax
762         pushl   %ecx
763         pushl   %edx
764         pushl   $0                              /* Pass NULL as regs pointer */
765         movl    4*4(%esp), %eax
766         movl    0x4(%ebp), %edx
767         movl    function_trace_op, %ecx
768         subl    $MCOUNT_INSN_SIZE, %eax
769
770 .globl ftrace_call
771 ftrace_call:
772         call    ftrace_stub
773
774         addl    $4, %esp                        /* skip NULL pointer */
775         popl    %edx
776         popl    %ecx
777         popl    %eax
778 ftrace_ret:
779 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
780 .globl ftrace_graph_call
781 ftrace_graph_call:
782         jmp     ftrace_stub
783 #endif
784
785 .globl ftrace_stub
786 ftrace_stub:
787         ret
788 END(ftrace_caller)
789
790 ENTRY(ftrace_regs_caller)
791         pushf   /* push flags before compare (in cs location) */
792
793         /*
794          * i386 does not save SS and ESP when coming from kernel.
795          * Instead, to get sp, &regs->sp is used (see ptrace.h).
796          * Unfortunately, that means eflags must be at the same location
797          * as the current return ip is. We move the return ip into the
798          * ip location, and move flags into the return ip location.
799          */
800         pushl   4(%esp)                         /* save return ip into ip slot */
801
802         pushl   $0                              /* Load 0 into orig_ax */
803         pushl   %gs
804         pushl   %fs
805         pushl   %es
806         pushl   %ds
807         pushl   %eax
808         pushl   %ebp
809         pushl   %edi
810         pushl   %esi
811         pushl   %edx
812         pushl   %ecx
813         pushl   %ebx
814
815         movl    13*4(%esp), %eax                /* Get the saved flags */
816         movl    %eax, 14*4(%esp)                /* Move saved flags into regs->flags location */
817                                                 /* clobbering return ip */
818         movl    $__KERNEL_CS, 13*4(%esp)
819
820         movl    12*4(%esp), %eax                /* Load ip (1st parameter) */
821         subl    $MCOUNT_INSN_SIZE, %eax         /* Adjust ip */
822         movl    0x4(%ebp), %edx                 /* Load parent ip (2nd parameter) */
823         movl    function_trace_op, %ecx         /* Save ftrace_pos in 3rd parameter */
824         pushl   %esp                            /* Save pt_regs as 4th parameter */
825
826 GLOBAL(ftrace_regs_call)
827         call    ftrace_stub
828
829         addl    $4, %esp                        /* Skip pt_regs */
830         movl    14*4(%esp), %eax                /* Move flags back into cs */
831         movl    %eax, 13*4(%esp)                /* Needed to keep addl  from modifying flags */
832         movl    12*4(%esp), %eax                /* Get return ip from regs->ip */
833         movl    %eax, 14*4(%esp)                /* Put return ip back for ret */
834
835         popl    %ebx
836         popl    %ecx
837         popl    %edx
838         popl    %esi
839         popl    %edi
840         popl    %ebp
841         popl    %eax
842         popl    %ds
843         popl    %es
844         popl    %fs
845         popl    %gs
846         addl    $8, %esp                        /* Skip orig_ax and ip */
847         popf                                    /* Pop flags at end (no addl to corrupt flags) */
848         jmp     ftrace_ret
849
850         popf
851         jmp     ftrace_stub
852 #else /* ! CONFIG_DYNAMIC_FTRACE */
853
854 ENTRY(mcount)
855         cmpl    $__PAGE_OFFSET, %esp
856         jb      ftrace_stub                     /* Paging not enabled yet? */
857
858         cmpl    $ftrace_stub, ftrace_trace_function
859         jnz     trace
860 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
861         cmpl    $ftrace_stub, ftrace_graph_return
862         jnz     ftrace_graph_caller
863
864         cmpl    $ftrace_graph_entry_stub, ftrace_graph_entry
865         jnz     ftrace_graph_caller
866 #endif
867 .globl ftrace_stub
868 ftrace_stub:
869         ret
870
871         /* taken from glibc */
872 trace:
873         pushl   %eax
874         pushl   %ecx
875         pushl   %edx
876         movl    0xc(%esp), %eax
877         movl    0x4(%ebp), %edx
878         subl    $MCOUNT_INSN_SIZE, %eax
879
880         call    *ftrace_trace_function
881
882         popl    %edx
883         popl    %ecx
884         popl    %eax
885         jmp     ftrace_stub
886 END(mcount)
887 #endif /* CONFIG_DYNAMIC_FTRACE */
888 #endif /* CONFIG_FUNCTION_TRACER */
889
890 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
891 ENTRY(ftrace_graph_caller)
892         pushl   %eax
893         pushl   %ecx
894         pushl   %edx
895         movl    0xc(%esp), %eax
896         lea     0x4(%ebp), %edx
897         movl    (%ebp), %ecx
898         subl    $MCOUNT_INSN_SIZE, %eax
899         call    prepare_ftrace_return
900         popl    %edx
901         popl    %ecx
902         popl    %eax
903         ret
904 END(ftrace_graph_caller)
905
906 .globl return_to_handler
907 return_to_handler:
908         pushl   %eax
909         pushl   %edx
910         movl    %ebp, %eax
911         call    ftrace_return_to_handler
912         movl    %eax, %ecx
913         popl    %edx
914         popl    %eax
915         jmp     *%ecx
916 #endif
917
918 #ifdef CONFIG_TRACING
919 ENTRY(trace_page_fault)
920         ASM_CLAC
921         pushl   $trace_do_page_fault
922         jmp     error_code
923 END(trace_page_fault)
924 #endif
925
926 ENTRY(page_fault)
927         ASM_CLAC
928         pushl   $do_page_fault
929         ALIGN
930 error_code:
931         /* the function address is in %gs's slot on the stack */
932         pushl   %fs
933         pushl   %es
934         pushl   %ds
935         pushl   %eax
936         pushl   %ebp
937         pushl   %edi
938         pushl   %esi
939         pushl   %edx
940         pushl   %ecx
941         pushl   %ebx
942         cld
943         movl    $(__KERNEL_PERCPU), %ecx
944         movl    %ecx, %fs
945         UNWIND_ESPFIX_STACK
946         GS_TO_REG %ecx
947         movl    PT_GS(%esp), %edi               # get the function address
948         movl    PT_ORIG_EAX(%esp), %edx         # get the error code
949         movl    $-1, PT_ORIG_EAX(%esp)          # no syscall to restart
950         REG_TO_PTGS %ecx
951         SET_KERNEL_GS %ecx
952         movl    $(__USER_DS), %ecx
953         movl    %ecx, %ds
954         movl    %ecx, %es
955         TRACE_IRQS_OFF
956         movl    %esp, %eax                      # pt_regs pointer
957         call    *%edi
958         jmp     ret_from_exception
959 END(page_fault)
960
961 /*
962  * Debug traps and NMI can happen at the one SYSENTER instruction
963  * that sets up the real kernel stack. Check here, since we can't
964  * allow the wrong stack to be used.
965  *
966  * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
967  * already pushed 3 words if it hits on the sysenter instruction:
968  * eflags, cs and eip.
969  *
970  * We just load the right stack, and push the three (known) values
971  * by hand onto the new stack - while updating the return eip past
972  * the instruction that would have done it for sysenter.
973  */
974 .macro FIX_STACK offset ok label
975         cmpw    $__KERNEL_CS, 4(%esp)
976         jne     \ok
977 \label:
978         movl    TSS_sysenter_sp0 + \offset(%esp), %esp
979         pushfl
980         pushl   $__KERNEL_CS
981         pushl   $sysenter_past_esp
982 .endm
983
984 ENTRY(debug)
985         ASM_CLAC
986         cmpl    $entry_SYSENTER_32, (%esp)
987         jne     debug_stack_correct
988         FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
989 debug_stack_correct:
990         pushl   $-1                             # mark this as an int
991         SAVE_ALL
992         TRACE_IRQS_OFF
993         xorl    %edx, %edx                      # error code 0
994         movl    %esp, %eax                      # pt_regs pointer
995         call    do_debug
996         jmp     ret_from_exception
997 END(debug)
998
999 /*
1000  * NMI is doubly nasty. It can happen _while_ we're handling
1001  * a debug fault, and the debug fault hasn't yet been able to
1002  * clear up the stack. So we first check whether we got  an
1003  * NMI on the sysenter entry path, but after that we need to
1004  * check whether we got an NMI on the debug path where the debug
1005  * fault happened on the sysenter path.
1006  */
1007 ENTRY(nmi)
1008         ASM_CLAC
1009 #ifdef CONFIG_X86_ESPFIX32
1010         pushl   %eax
1011         movl    %ss, %eax
1012         cmpw    $__ESPFIX_SS, %ax
1013         popl    %eax
1014         je      nmi_espfix_stack
1015 #endif
1016         cmpl    $entry_SYSENTER_32, (%esp)
1017         je      nmi_stack_fixup
1018         pushl   %eax
1019         movl    %esp, %eax
1020         /*
1021          * Do not access memory above the end of our stack page,
1022          * it might not exist.
1023          */
1024         andl    $(THREAD_SIZE-1), %eax
1025         cmpl    $(THREAD_SIZE-20), %eax
1026         popl    %eax
1027         jae     nmi_stack_correct
1028         cmpl    $entry_SYSENTER_32, 12(%esp)
1029         je      nmi_debug_stack_check
1030 nmi_stack_correct:
1031         pushl   %eax
1032         SAVE_ALL
1033         xorl    %edx, %edx                      # zero error code
1034         movl    %esp, %eax                      # pt_regs pointer
1035         call    do_nmi
1036         jmp     restore_all_notrace
1037
1038 nmi_stack_fixup:
1039         FIX_STACK 12, nmi_stack_correct, 1
1040         jmp     nmi_stack_correct
1041
1042 nmi_debug_stack_check:
1043         cmpw    $__KERNEL_CS, 16(%esp)
1044         jne     nmi_stack_correct
1045         cmpl    $debug, (%esp)
1046         jb      nmi_stack_correct
1047         cmpl    $debug_esp_fix_insn, (%esp)
1048         ja      nmi_stack_correct
1049         FIX_STACK 24, nmi_stack_correct, 1
1050         jmp     nmi_stack_correct
1051
1052 #ifdef CONFIG_X86_ESPFIX32
1053 nmi_espfix_stack:
1054         /*
1055          * create the pointer to lss back
1056          */
1057         pushl   %ss
1058         pushl   %esp
1059         addl    $4, (%esp)
1060         /* copy the iret frame of 12 bytes */
1061         .rept 3
1062         pushl   16(%esp)
1063         .endr
1064         pushl   %eax
1065         SAVE_ALL
1066         FIXUP_ESPFIX_STACK                      # %eax == %esp
1067         xorl    %edx, %edx                      # zero error code
1068         call    do_nmi
1069         RESTORE_REGS
1070         lss     12+4(%esp), %esp                # back to espfix stack
1071         jmp     irq_return
1072 #endif
1073 END(nmi)
1074
1075 ENTRY(int3)
1076         ASM_CLAC
1077         pushl   $-1                             # mark this as an int
1078         SAVE_ALL
1079         TRACE_IRQS_OFF
1080         xorl    %edx, %edx                      # zero error code
1081         movl    %esp, %eax                      # pt_regs pointer
1082         call    do_int3
1083         jmp     ret_from_exception
1084 END(int3)
1085
1086 ENTRY(general_protection)
1087         pushl   $do_general_protection
1088         jmp     error_code
1089 END(general_protection)
1090
1091 #ifdef CONFIG_KVM_GUEST
1092 ENTRY(async_page_fault)
1093         ASM_CLAC
1094         pushl   $do_async_page_fault
1095         jmp     error_code
1096 END(async_page_fault)
1097 #endif