GNU Linux-libre 4.9.314-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/export.h>
48 #include <asm/nospec-branch.h>
49
50         .section .entry.text, "ax"
51
52 /*
53  * We use macros for low-level operations which need to be overridden
54  * for paravirtualization.  The following will never clobber any registers:
55  *   INTERRUPT_RETURN (aka. "iret")
56  *   GET_CR0_INTO_EAX (aka. "movl %cr0, %eax")
57  *   ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
58  *
59  * For DISABLE_INTERRUPTS/ENABLE_INTERRUPTS (aka "cli"/"sti"), you must
60  * specify what registers can be overwritten (CLBR_NONE, CLBR_EAX/EDX/ECX/ANY).
61  * Allowing a register to be clobbered can shrink the paravirt replacement
62  * enough to patch inline, increasing performance.
63  */
64
65 #ifdef CONFIG_PREEMPT
66 # define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
67 #else
68 # define preempt_stop(clobbers)
69 # define resume_kernel          restore_all
70 #endif
71
72 .macro TRACE_IRQS_IRET
73 #ifdef CONFIG_TRACE_IRQFLAGS
74         testl   $X86_EFLAGS_IF, PT_EFLAGS(%esp)     # interrupts off?
75         jz      1f
76         TRACE_IRQS_ON
77 1:
78 #endif
79 .endm
80
81 /*
82  * User gs save/restore
83  *
84  * %gs is used for userland TLS and kernel only uses it for stack
85  * canary which is required to be at %gs:20 by gcc.  Read the comment
86  * at the top of stackprotector.h for more info.
87  *
88  * Local labels 98 and 99 are used.
89  */
90 #ifdef CONFIG_X86_32_LAZY_GS
91
92  /* unfortunately push/pop can't be no-op */
93 .macro PUSH_GS
94         pushl   $0
95 .endm
96 .macro POP_GS pop=0
97         addl    $(4 + \pop), %esp
98 .endm
99 .macro POP_GS_EX
100 .endm
101
102  /* all the rest are no-op */
103 .macro PTGS_TO_GS
104 .endm
105 .macro PTGS_TO_GS_EX
106 .endm
107 .macro GS_TO_REG reg
108 .endm
109 .macro REG_TO_PTGS reg
110 .endm
111 .macro SET_KERNEL_GS reg
112 .endm
113
114 #else   /* CONFIG_X86_32_LAZY_GS */
115
116 .macro PUSH_GS
117         pushl   %gs
118 .endm
119
120 .macro POP_GS pop=0
121 98:     popl    %gs
122   .if \pop <> 0
123         add     $\pop, %esp
124   .endif
125 .endm
126 .macro POP_GS_EX
127 .pushsection .fixup, "ax"
128 99:     movl    $0, (%esp)
129         jmp     98b
130 .popsection
131         _ASM_EXTABLE(98b, 99b)
132 .endm
133
134 .macro PTGS_TO_GS
135 98:     mov     PT_GS(%esp), %gs
136 .endm
137 .macro PTGS_TO_GS_EX
138 .pushsection .fixup, "ax"
139 99:     movl    $0, PT_GS(%esp)
140         jmp     98b
141 .popsection
142         _ASM_EXTABLE(98b, 99b)
143 .endm
144
145 .macro GS_TO_REG reg
146         movl    %gs, \reg
147 .endm
148 .macro REG_TO_PTGS reg
149         movl    \reg, PT_GS(%esp)
150 .endm
151 .macro SET_KERNEL_GS reg
152         movl    $(__KERNEL_STACK_CANARY), \reg
153         movl    \reg, %gs
154 .endm
155
156 #endif /* CONFIG_X86_32_LAZY_GS */
157
158 .macro SAVE_ALL pt_regs_ax=%eax
159         cld
160         PUSH_GS
161         pushl   %fs
162         pushl   %es
163         pushl   %ds
164         pushl   \pt_regs_ax
165         pushl   %ebp
166         pushl   %edi
167         pushl   %esi
168         pushl   %edx
169         pushl   %ecx
170         pushl   %ebx
171         movl    $(__USER_DS), %edx
172         movl    %edx, %ds
173         movl    %edx, %es
174         movl    $(__KERNEL_PERCPU), %edx
175         movl    %edx, %fs
176         SET_KERNEL_GS %edx
177 .endm
178
179 .macro RESTORE_INT_REGS
180         popl    %ebx
181         popl    %ecx
182         popl    %edx
183         popl    %esi
184         popl    %edi
185         popl    %ebp
186         popl    %eax
187 .endm
188
189 .macro RESTORE_REGS pop=0
190         RESTORE_INT_REGS
191 1:      popl    %ds
192 2:      popl    %es
193 3:      popl    %fs
194         POP_GS \pop
195 .pushsection .fixup, "ax"
196 4:      movl    $0, (%esp)
197         jmp     1b
198 5:      movl    $0, (%esp)
199         jmp     2b
200 6:      movl    $0, (%esp)
201         jmp     3b
202 .popsection
203         _ASM_EXTABLE(1b, 4b)
204         _ASM_EXTABLE(2b, 5b)
205         _ASM_EXTABLE(3b, 6b)
206         POP_GS_EX
207 .endm
208
209 /*
210  * %eax: prev task
211  * %edx: next task
212  */
213 ENTRY(__switch_to_asm)
214         /*
215          * Save callee-saved registers
216          * This must match the order in struct inactive_task_frame
217          */
218         pushl   %ebp
219         pushl   %ebx
220         pushl   %edi
221         pushl   %esi
222         pushfl
223
224         /* switch stack */
225         movl    %esp, TASK_threadsp(%eax)
226         movl    TASK_threadsp(%edx), %esp
227
228 #ifdef CONFIG_CC_STACKPROTECTOR
229         movl    TASK_stack_canary(%edx), %ebx
230         movl    %ebx, PER_CPU_VAR(stack_canary)+stack_canary_offset
231 #endif
232
233 #ifdef CONFIG_RETPOLINE
234         /*
235          * When switching from a shallower to a deeper call stack
236          * the RSB may either underflow or use entries populated
237          * with userspace addresses. On CPUs where those concerns
238          * exist, overwrite the RSB with entries which capture
239          * speculative execution to prevent attack.
240          */
241         FILL_RETURN_BUFFER %ebx, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
242 #endif
243
244         /* restore callee-saved registers */
245         popfl
246         popl    %esi
247         popl    %edi
248         popl    %ebx
249         popl    %ebp
250
251         jmp     __switch_to
252 END(__switch_to_asm)
253
254 /*
255  * A newly forked process directly context switches into this address.
256  *
257  * eax: prev task we switched from
258  * ebx: kernel thread func (NULL for user thread)
259  * edi: kernel thread arg
260  */
261 ENTRY(ret_from_fork)
262         pushl   %eax
263         call    schedule_tail
264         popl    %eax
265
266         testl   %ebx, %ebx
267         jnz     1f              /* kernel threads are uncommon */
268
269 2:
270         /* When we fork, we trace the syscall return in the child, too. */
271         movl    %esp, %eax
272         call    syscall_return_slowpath
273         jmp     restore_all
274
275         /* kernel thread */
276 1:      movl    %edi, %eax
277         CALL_NOSPEC %ebx
278         /*
279          * A kernel thread is allowed to return here after successfully
280          * calling do_execve().  Exit to userspace to complete the execve()
281          * syscall.
282          */
283         movl    $0, PT_EAX(%esp)
284         jmp     2b
285 END(ret_from_fork)
286
287 /*
288  * Return to user mode is not as complex as all this looks,
289  * but we want the default path for a system call return to
290  * go as quickly as possible which is why some of this is
291  * less clear than it otherwise should be.
292  */
293
294         # userspace resumption stub bypassing syscall exit tracing
295         ALIGN
296 ret_from_exception:
297         preempt_stop(CLBR_ANY)
298 ret_from_intr:
299 #ifdef CONFIG_VM86
300         movl    PT_EFLAGS(%esp), %eax           # mix EFLAGS and CS
301         movb    PT_CS(%esp), %al
302         andl    $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
303 #else
304         /*
305          * We can be coming here from child spawned by kernel_thread().
306          */
307         movl    PT_CS(%esp), %eax
308         andl    $SEGMENT_RPL_MASK, %eax
309 #endif
310         cmpl    $USER_RPL, %eax
311         jb      resume_kernel                   # not returning to v8086 or userspace
312
313 ENTRY(resume_userspace)
314         DISABLE_INTERRUPTS(CLBR_ANY)
315         TRACE_IRQS_OFF
316         movl    %esp, %eax
317         call    prepare_exit_to_usermode
318         jmp     restore_all
319 END(ret_from_exception)
320
321 #ifdef CONFIG_PREEMPT
322 ENTRY(resume_kernel)
323         DISABLE_INTERRUPTS(CLBR_ANY)
324 need_resched:
325         cmpl    $0, PER_CPU_VAR(__preempt_count)
326         jnz     restore_all
327         testl   $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
328         jz      restore_all
329         call    preempt_schedule_irq
330         jmp     need_resched
331 END(resume_kernel)
332 #endif
333
334 GLOBAL(__begin_SYSENTER_singlestep_region)
335 /*
336  * All code from here through __end_SYSENTER_singlestep_region is subject
337  * to being single-stepped if a user program sets TF and executes SYSENTER.
338  * There is absolutely nothing that we can do to prevent this from happening
339  * (thanks Intel!).  To keep our handling of this situation as simple as
340  * possible, we handle TF just like AC and NT, except that our #DB handler
341  * will ignore all of the single-step traps generated in this range.
342  */
343
344 #ifdef CONFIG_XEN
345 /*
346  * Xen doesn't set %esp to be precisely what the normal SYSENTER
347  * entry point expects, so fix it up before using the normal path.
348  */
349 ENTRY(xen_sysenter_target)
350         addl    $5*4, %esp                      /* remove xen-provided frame */
351         jmp     sysenter_past_esp
352 #endif
353
354 /*
355  * 32-bit SYSENTER entry.
356  *
357  * 32-bit system calls through the vDSO's __kernel_vsyscall enter here
358  * if X86_FEATURE_SEP is available.  This is the preferred system call
359  * entry on 32-bit systems.
360  *
361  * The SYSENTER instruction, in principle, should *only* occur in the
362  * vDSO.  In practice, a small number of Android devices were shipped
363  * with a copy of Bionic that inlined a SYSENTER instruction.  This
364  * never happened in any of Google's Bionic versions -- it only happened
365  * in a narrow range of Intel-provided versions.
366  *
367  * SYSENTER loads SS, ESP, CS, and EIP from previously programmed MSRs.
368  * IF and VM in RFLAGS are cleared (IOW: interrupts are off).
369  * SYSENTER does not save anything on the stack,
370  * and does not save old EIP (!!!), ESP, or EFLAGS.
371  *
372  * To avoid losing track of EFLAGS.VM (and thus potentially corrupting
373  * user and/or vm86 state), we explicitly disable the SYSENTER
374  * instruction in vm86 mode by reprogramming the MSRs.
375  *
376  * Arguments:
377  * eax  system call number
378  * ebx  arg1
379  * ecx  arg2
380  * edx  arg3
381  * esi  arg4
382  * edi  arg5
383  * ebp  user stack
384  * 0(%ebp) arg6
385  */
386 ENTRY(entry_SYSENTER_32)
387         movl    TSS_sysenter_sp0(%esp), %esp
388 sysenter_past_esp:
389         pushl   $__USER_DS              /* pt_regs->ss */
390         pushl   %ebp                    /* pt_regs->sp (stashed in bp) */
391         pushfl                          /* pt_regs->flags (except IF = 0) */
392         orl     $X86_EFLAGS_IF, (%esp)  /* Fix IF */
393         pushl   $__USER_CS              /* pt_regs->cs */
394         pushl   $0                      /* pt_regs->ip = 0 (placeholder) */
395         pushl   %eax                    /* pt_regs->orig_ax */
396         SAVE_ALL pt_regs_ax=$-ENOSYS    /* save rest */
397
398         /*
399          * SYSENTER doesn't filter flags, so we need to clear NT, AC
400          * and TF ourselves.  To save a few cycles, we can check whether
401          * either was set instead of doing an unconditional popfq.
402          * This needs to happen before enabling interrupts so that
403          * we don't get preempted with NT set.
404          *
405          * If TF is set, we will single-step all the way to here -- do_debug
406          * will ignore all the traps.  (Yes, this is slow, but so is
407          * single-stepping in general.  This allows us to avoid having
408          * a more complicated code to handle the case where a user program
409          * forces us to single-step through the SYSENTER entry code.)
410          *
411          * NB.: .Lsysenter_fix_flags is a label with the code under it moved
412          * out-of-line as an optimization: NT is unlikely to be set in the
413          * majority of the cases and instead of polluting the I$ unnecessarily,
414          * we're keeping that code behind a branch which will predict as
415          * not-taken and therefore its instructions won't be fetched.
416          */
417         testl   $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, PT_EFLAGS(%esp)
418         jnz     .Lsysenter_fix_flags
419 .Lsysenter_flags_fixed:
420
421         /*
422          * User mode is traced as though IRQs are on, and SYSENTER
423          * turned them off.
424          */
425         TRACE_IRQS_OFF
426
427         movl    %esp, %eax
428         call    do_fast_syscall_32
429         /* XEN PV guests always use IRET path */
430         ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
431                     "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
432
433 /* Opportunistic SYSEXIT */
434         TRACE_IRQS_ON                   /* User mode traces as IRQs on. */
435         movl    PT_EIP(%esp), %edx      /* pt_regs->ip */
436         movl    PT_OLDESP(%esp), %ecx   /* pt_regs->sp */
437 1:      mov     PT_FS(%esp), %fs
438         PTGS_TO_GS
439         popl    %ebx                    /* pt_regs->bx */
440         addl    $2*4, %esp              /* skip pt_regs->cx and pt_regs->dx */
441         popl    %esi                    /* pt_regs->si */
442         popl    %edi                    /* pt_regs->di */
443         popl    %ebp                    /* pt_regs->bp */
444         popl    %eax                    /* pt_regs->ax */
445
446         /*
447          * Restore all flags except IF. (We restore IF separately because
448          * STI gives a one-instruction window in which we won't be interrupted,
449          * whereas POPF does not.)
450          */
451         addl    $PT_EFLAGS-PT_DS, %esp  /* point esp at pt_regs->flags */
452         btr     $X86_EFLAGS_IF_BIT, (%esp)
453         popfl
454
455         /*
456          * Return back to the vDSO, which will pop ecx and edx.
457          * Don't bother with DS and ES (they already contain __USER_DS).
458          */
459         sti
460         sysexit
461
462 .pushsection .fixup, "ax"
463 2:      movl    $0, PT_FS(%esp)
464         jmp     1b
465 .popsection
466         _ASM_EXTABLE(1b, 2b)
467         PTGS_TO_GS_EX
468
469 .Lsysenter_fix_flags:
470         pushl   $X86_EFLAGS_FIXED
471         popfl
472         jmp     .Lsysenter_flags_fixed
473 GLOBAL(__end_SYSENTER_singlestep_region)
474 ENDPROC(entry_SYSENTER_32)
475
476 /*
477  * 32-bit legacy system call entry.
478  *
479  * 32-bit x86 Linux system calls traditionally used the INT $0x80
480  * instruction.  INT $0x80 lands here.
481  *
482  * This entry point can be used by any 32-bit perform system calls.
483  * Instances of INT $0x80 can be found inline in various programs and
484  * libraries.  It is also used by the vDSO's __kernel_vsyscall
485  * fallback for hardware that doesn't support a faster entry method.
486  * Restarted 32-bit system calls also fall back to INT $0x80
487  * regardless of what instruction was originally used to do the system
488  * call.  (64-bit programs can use INT $0x80 as well, but they can
489  * only run on 64-bit kernels and therefore land in
490  * entry_INT80_compat.)
491  *
492  * This is considered a slow path.  It is not used by most libc
493  * implementations on modern hardware except during process startup.
494  *
495  * Arguments:
496  * eax  system call number
497  * ebx  arg1
498  * ecx  arg2
499  * edx  arg3
500  * esi  arg4
501  * edi  arg5
502  * ebp  arg6
503  */
504 ENTRY(entry_INT80_32)
505         ASM_CLAC
506         pushl   %eax                    /* pt_regs->orig_ax */
507         SAVE_ALL pt_regs_ax=$-ENOSYS    /* save rest */
508
509         /*
510          * User mode is traced as though IRQs are on, and the interrupt gate
511          * turned them off.
512          */
513         TRACE_IRQS_OFF
514
515         movl    %esp, %eax
516         call    do_int80_syscall_32
517 .Lsyscall_32_done:
518
519 restore_all:
520         TRACE_IRQS_IRET
521 restore_all_notrace:
522 #ifdef CONFIG_X86_ESPFIX32
523         ALTERNATIVE     "jmp restore_nocheck", "", X86_BUG_ESPFIX
524
525         movl    PT_EFLAGS(%esp), %eax           # mix EFLAGS, SS and CS
526         /*
527          * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
528          * are returning to the kernel.
529          * See comments in process.c:copy_thread() for details.
530          */
531         movb    PT_OLDSS(%esp), %ah
532         movb    PT_CS(%esp), %al
533         andl    $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
534         cmpl    $((SEGMENT_LDT << 8) | USER_RPL), %eax
535         je ldt_ss                               # returning to user-space with LDT SS
536 #endif
537 restore_nocheck:
538         RESTORE_REGS 4                          # skip orig_eax/error_code
539 irq_return:
540         INTERRUPT_RETURN
541 .section .fixup, "ax"
542 ENTRY(iret_exc  )
543         pushl   $0                              # no error code
544         pushl   $do_iret_error
545         jmp     error_code
546 .previous
547         _ASM_EXTABLE(irq_return, iret_exc)
548
549 #ifdef CONFIG_X86_ESPFIX32
550 ldt_ss:
551 /*
552  * Setup and switch to ESPFIX stack
553  *
554  * We're returning to userspace with a 16 bit stack. The CPU will not
555  * restore the high word of ESP for us on executing iret... This is an
556  * "official" bug of all the x86-compatible CPUs, which we can work
557  * around to make dosemu and wine happy. We do this by preloading the
558  * high word of ESP with the high word of the userspace ESP while
559  * compensating for the offset by changing to the ESPFIX segment with
560  * a base address that matches for the difference.
561  */
562 #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
563         mov     %esp, %edx                      /* load kernel esp */
564         mov     PT_OLDESP(%esp), %eax           /* load userspace esp */
565         mov     %dx, %ax                        /* eax: new kernel esp */
566         sub     %eax, %edx                      /* offset (low word is 0) */
567         shr     $16, %edx
568         mov     %dl, GDT_ESPFIX_SS + 4          /* bits 16..23 */
569         mov     %dh, GDT_ESPFIX_SS + 7          /* bits 24..31 */
570         pushl   $__ESPFIX_SS
571         pushl   %eax                            /* new kernel esp */
572         /*
573          * Disable interrupts, but do not irqtrace this section: we
574          * will soon execute iret and the tracer was already set to
575          * the irqstate after the IRET:
576          */
577         DISABLE_INTERRUPTS(CLBR_EAX)
578         lss     (%esp), %esp                    /* switch to espfix segment */
579         jmp     restore_nocheck
580 #endif
581 ENDPROC(entry_INT80_32)
582
583 .macro FIXUP_ESPFIX_STACK
584 /*
585  * Switch back for ESPFIX stack to the normal zerobased stack
586  *
587  * We can't call C functions using the ESPFIX stack. This code reads
588  * the high word of the segment base from the GDT and swiches to the
589  * normal stack and adjusts ESP with the matching offset.
590  */
591 #ifdef CONFIG_X86_ESPFIX32
592         /* fixup the stack */
593         mov     GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
594         mov     GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
595         shl     $16, %eax
596         addl    %esp, %eax                      /* the adjusted stack pointer */
597         pushl   $__KERNEL_DS
598         pushl   %eax
599         lss     (%esp), %esp                    /* switch to the normal stack segment */
600 #endif
601 .endm
602 .macro UNWIND_ESPFIX_STACK
603 #ifdef CONFIG_X86_ESPFIX32
604         movl    %ss, %eax
605         /* see if on espfix stack */
606         cmpw    $__ESPFIX_SS, %ax
607         jne     27f
608         movl    $__KERNEL_DS, %eax
609         movl    %eax, %ds
610         movl    %eax, %es
611         /* switch to normal stack */
612         FIXUP_ESPFIX_STACK
613 27:
614 #endif
615 .endm
616
617 /*
618  * Build the entry stubs with some assembler magic.
619  * We pack 1 stub into every 8-byte block.
620  */
621         .align 8
622 ENTRY(irq_entries_start)
623     vector=FIRST_EXTERNAL_VECTOR
624     .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
625         pushl   $(~vector+0x80)                 /* Note: always in signed byte range */
626     vector=vector+1
627         jmp     common_interrupt
628         .align  8
629     .endr
630 END(irq_entries_start)
631
632 /*
633  * the CPU automatically disables interrupts when executing an IRQ vector,
634  * so IRQ-flags tracing has to follow that:
635  */
636         .p2align CONFIG_X86_L1_CACHE_SHIFT
637 common_interrupt:
638         ASM_CLAC
639         addl    $-0x80, (%esp)                  /* Adjust vector into the [-256, -1] range */
640         SAVE_ALL
641         TRACE_IRQS_OFF
642         movl    %esp, %eax
643         call    do_IRQ
644         jmp     ret_from_intr
645 ENDPROC(common_interrupt)
646
647 #define BUILD_INTERRUPT3(name, nr, fn)  \
648 ENTRY(name)                             \
649         ASM_CLAC;                       \
650         pushl   $~(nr);                 \
651         SAVE_ALL;                       \
652         TRACE_IRQS_OFF                  \
653         movl    %esp, %eax;             \
654         call    fn;                     \
655         jmp     ret_from_intr;          \
656 ENDPROC(name)
657
658
659 #ifdef CONFIG_TRACING
660 # define TRACE_BUILD_INTERRUPT(name, nr)        BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
661 #else
662 # define TRACE_BUILD_INTERRUPT(name, nr)
663 #endif
664
665 #define BUILD_INTERRUPT(name, nr)               \
666         BUILD_INTERRUPT3(name, nr, smp_##name); \
667         TRACE_BUILD_INTERRUPT(name, nr)
668
669 /* The include is where all of the SMP etc. interrupts come from */
670 #include <asm/entry_arch.h>
671
672 ENTRY(coprocessor_error)
673         ASM_CLAC
674         pushl   $0
675         pushl   $do_coprocessor_error
676         jmp     error_code
677 END(coprocessor_error)
678
679 ENTRY(simd_coprocessor_error)
680         ASM_CLAC
681         pushl   $0
682 #ifdef CONFIG_X86_INVD_BUG
683         /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
684         ALTERNATIVE "pushl      $do_general_protection",        \
685                     "pushl      $do_simd_coprocessor_error",    \
686                     X86_FEATURE_XMM
687 #else
688         pushl   $do_simd_coprocessor_error
689 #endif
690         jmp     error_code
691 END(simd_coprocessor_error)
692
693 ENTRY(device_not_available)
694         ASM_CLAC
695         pushl   $-1                             # mark this as an int
696         pushl   $do_device_not_available
697         jmp     error_code
698 END(device_not_available)
699
700 #ifdef CONFIG_PARAVIRT
701 ENTRY(native_iret)
702         iret
703         _ASM_EXTABLE(native_iret, iret_exc)
704 END(native_iret)
705 #endif
706
707 ENTRY(overflow)
708         ASM_CLAC
709         pushl   $0
710         pushl   $do_overflow
711         jmp     error_code
712 END(overflow)
713
714 ENTRY(bounds)
715         ASM_CLAC
716         pushl   $0
717         pushl   $do_bounds
718         jmp     error_code
719 END(bounds)
720
721 ENTRY(invalid_op)
722         ASM_CLAC
723         pushl   $0
724         pushl   $do_invalid_op
725         jmp     error_code
726 END(invalid_op)
727
728 ENTRY(coprocessor_segment_overrun)
729         ASM_CLAC
730         pushl   $0
731         pushl   $do_coprocessor_segment_overrun
732         jmp     error_code
733 END(coprocessor_segment_overrun)
734
735 ENTRY(invalid_TSS)
736         ASM_CLAC
737         pushl   $do_invalid_TSS
738         jmp     error_code
739 END(invalid_TSS)
740
741 ENTRY(segment_not_present)
742         ASM_CLAC
743         pushl   $do_segment_not_present
744         jmp     error_code
745 END(segment_not_present)
746
747 ENTRY(stack_segment)
748         ASM_CLAC
749         pushl   $do_stack_segment
750         jmp     error_code
751 END(stack_segment)
752
753 ENTRY(alignment_check)
754         ASM_CLAC
755         pushl   $do_alignment_check
756         jmp     error_code
757 END(alignment_check)
758
759 ENTRY(divide_error)
760         ASM_CLAC
761         pushl   $0                              # no error code
762         pushl   $do_divide_error
763         jmp     error_code
764 END(divide_error)
765
766 #ifdef CONFIG_X86_MCE
767 ENTRY(machine_check)
768         ASM_CLAC
769         pushl   $0
770         pushl   machine_check_vector
771         jmp     error_code
772 END(machine_check)
773 #endif
774
775 ENTRY(spurious_interrupt_bug)
776         ASM_CLAC
777         pushl   $0
778         pushl   $do_spurious_interrupt_bug
779         jmp     error_code
780 END(spurious_interrupt_bug)
781
782 #ifdef CONFIG_XEN
783 ENTRY(xen_hypervisor_callback)
784         pushl   $-1                             /* orig_ax = -1 => not a system call */
785         SAVE_ALL
786         TRACE_IRQS_OFF
787
788         /*
789          * Check to see if we got the event in the critical
790          * region in xen_iret_direct, after we've reenabled
791          * events and checked for pending events.  This simulates
792          * iret instruction's behaviour where it delivers a
793          * pending interrupt when enabling interrupts:
794          */
795         movl    PT_EIP(%esp), %eax
796         cmpl    $xen_iret_start_crit, %eax
797         jb      1f
798         cmpl    $xen_iret_end_crit, %eax
799         jae     1f
800
801         jmp     xen_iret_crit_fixup
802
803 ENTRY(xen_do_upcall)
804 1:      mov     %esp, %eax
805         call    xen_evtchn_do_upcall
806 #ifndef CONFIG_PREEMPT
807         call    xen_maybe_preempt_hcall
808 #endif
809         jmp     ret_from_intr
810 ENDPROC(xen_hypervisor_callback)
811
812 /*
813  * Hypervisor uses this for application faults while it executes.
814  * We get here for two reasons:
815  *  1. Fault while reloading DS, ES, FS or GS
816  *  2. Fault while executing IRET
817  * Category 1 we fix up by reattempting the load, and zeroing the segment
818  * register if the load fails.
819  * Category 2 we fix up by jumping to do_iret_error. We cannot use the
820  * normal Linux return path in this case because if we use the IRET hypercall
821  * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
822  * We distinguish between categories by maintaining a status value in EAX.
823  */
824 ENTRY(xen_failsafe_callback)
825         pushl   %eax
826         movl    $1, %eax
827 1:      mov     4(%esp), %ds
828 2:      mov     8(%esp), %es
829 3:      mov     12(%esp), %fs
830 4:      mov     16(%esp), %gs
831         /* EAX == 0 => Category 1 (Bad segment)
832            EAX != 0 => Category 2 (Bad IRET) */
833         testl   %eax, %eax
834         popl    %eax
835         lea     16(%esp), %esp
836         jz      5f
837         jmp     iret_exc
838 5:      pushl   $-1                             /* orig_ax = -1 => not a system call */
839         SAVE_ALL
840         jmp     ret_from_exception
841
842 .section .fixup, "ax"
843 6:      xorl    %eax, %eax
844         movl    %eax, 4(%esp)
845         jmp     1b
846 7:      xorl    %eax, %eax
847         movl    %eax, 8(%esp)
848         jmp     2b
849 8:      xorl    %eax, %eax
850         movl    %eax, 12(%esp)
851         jmp     3b
852 9:      xorl    %eax, %eax
853         movl    %eax, 16(%esp)
854         jmp     4b
855 .previous
856         _ASM_EXTABLE(1b, 6b)
857         _ASM_EXTABLE(2b, 7b)
858         _ASM_EXTABLE(3b, 8b)
859         _ASM_EXTABLE(4b, 9b)
860 ENDPROC(xen_failsafe_callback)
861
862 BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
863                 xen_evtchn_do_upcall)
864
865 #endif /* CONFIG_XEN */
866
867 #if IS_ENABLED(CONFIG_HYPERV)
868
869 BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
870         hyperv_vector_handler)
871
872 #endif /* CONFIG_HYPERV */
873
874 #ifdef CONFIG_FUNCTION_TRACER
875 #ifdef CONFIG_DYNAMIC_FTRACE
876
877 ENTRY(mcount)
878         ret
879 END(mcount)
880
881 ENTRY(ftrace_caller)
882         pushl   %eax
883         pushl   %ecx
884         pushl   %edx
885         pushl   $0                              /* Pass NULL as regs pointer */
886         movl    4*4(%esp), %eax
887         movl    0x4(%ebp), %edx
888         movl    function_trace_op, %ecx
889         subl    $MCOUNT_INSN_SIZE, %eax
890
891 .globl ftrace_call
892 ftrace_call:
893         call    ftrace_stub
894
895         addl    $4, %esp                        /* skip NULL pointer */
896         popl    %edx
897         popl    %ecx
898         popl    %eax
899 ftrace_ret:
900 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
901 .globl ftrace_graph_call
902 ftrace_graph_call:
903         jmp     ftrace_stub
904 #endif
905
906 /* This is weak to keep gas from relaxing the jumps */
907 WEAK(ftrace_stub)
908         ret
909 END(ftrace_caller)
910
911 ENTRY(ftrace_regs_caller)
912         pushf   /* push flags before compare (in cs location) */
913
914         /*
915          * i386 does not save SS and ESP when coming from kernel.
916          * Instead, to get sp, &regs->sp is used (see ptrace.h).
917          * Unfortunately, that means eflags must be at the same location
918          * as the current return ip is. We move the return ip into the
919          * ip location, and move flags into the return ip location.
920          */
921         pushl   4(%esp)                         /* save return ip into ip slot */
922
923         pushl   $0                              /* Load 0 into orig_ax */
924         pushl   %gs
925         pushl   %fs
926         pushl   %es
927         pushl   %ds
928         pushl   %eax
929         pushl   %ebp
930         pushl   %edi
931         pushl   %esi
932         pushl   %edx
933         pushl   %ecx
934         pushl   %ebx
935
936         movl    13*4(%esp), %eax                /* Get the saved flags */
937         movl    %eax, 14*4(%esp)                /* Move saved flags into regs->flags location */
938                                                 /* clobbering return ip */
939         movl    $__KERNEL_CS, 13*4(%esp)
940
941         movl    12*4(%esp), %eax                /* Load ip (1st parameter) */
942         subl    $MCOUNT_INSN_SIZE, %eax         /* Adjust ip */
943         movl    0x4(%ebp), %edx                 /* Load parent ip (2nd parameter) */
944         movl    function_trace_op, %ecx         /* Save ftrace_pos in 3rd parameter */
945         pushl   %esp                            /* Save pt_regs as 4th parameter */
946
947 GLOBAL(ftrace_regs_call)
948         call    ftrace_stub
949
950         addl    $4, %esp                        /* Skip pt_regs */
951         movl    14*4(%esp), %eax                /* Move flags back into cs */
952         movl    %eax, 13*4(%esp)                /* Needed to keep addl  from modifying flags */
953         movl    12*4(%esp), %eax                /* Get return ip from regs->ip */
954         movl    %eax, 14*4(%esp)                /* Put return ip back for ret */
955
956         popl    %ebx
957         popl    %ecx
958         popl    %edx
959         popl    %esi
960         popl    %edi
961         popl    %ebp
962         popl    %eax
963         popl    %ds
964         popl    %es
965         popl    %fs
966         popl    %gs
967         addl    $8, %esp                        /* Skip orig_ax and ip */
968         popf                                    /* Pop flags at end (no addl to corrupt flags) */
969         jmp     ftrace_ret
970
971         popf
972         jmp     ftrace_stub
973 #else /* ! CONFIG_DYNAMIC_FTRACE */
974
975 ENTRY(mcount)
976         cmpl    $__PAGE_OFFSET, %esp
977         jb      ftrace_stub                     /* Paging not enabled yet? */
978
979         cmpl    $ftrace_stub, ftrace_trace_function
980         jnz     trace
981 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
982         cmpl    $ftrace_stub, ftrace_graph_return
983         jnz     ftrace_graph_caller
984
985         cmpl    $ftrace_graph_entry_stub, ftrace_graph_entry
986         jnz     ftrace_graph_caller
987 #endif
988 .globl ftrace_stub
989 ftrace_stub:
990         ret
991
992         /* taken from glibc */
993 trace:
994         pushl   %eax
995         pushl   %ecx
996         pushl   %edx
997         movl    0xc(%esp), %eax
998         movl    0x4(%ebp), %edx
999         subl    $MCOUNT_INSN_SIZE, %eax
1000
1001         movl    ftrace_trace_function, %ecx
1002         CALL_NOSPEC %ecx
1003
1004         popl    %edx
1005         popl    %ecx
1006         popl    %eax
1007         jmp     ftrace_stub
1008 END(mcount)
1009 #endif /* CONFIG_DYNAMIC_FTRACE */
1010 EXPORT_SYMBOL(mcount)
1011 #endif /* CONFIG_FUNCTION_TRACER */
1012
1013 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1014 ENTRY(ftrace_graph_caller)
1015         pushl   %eax
1016         pushl   %ecx
1017         pushl   %edx
1018         movl    0xc(%esp), %eax
1019         lea     0x4(%ebp), %edx
1020         movl    (%ebp), %ecx
1021         subl    $MCOUNT_INSN_SIZE, %eax
1022         call    prepare_ftrace_return
1023         popl    %edx
1024         popl    %ecx
1025         popl    %eax
1026         ret
1027 END(ftrace_graph_caller)
1028
1029 .globl return_to_handler
1030 return_to_handler:
1031         pushl   %eax
1032         pushl   %edx
1033         movl    %ebp, %eax
1034         call    ftrace_return_to_handler
1035         movl    %eax, %ecx
1036         popl    %edx
1037         popl    %eax
1038         JMP_NOSPEC %ecx
1039 #endif
1040
1041 #ifdef CONFIG_TRACING
1042 ENTRY(trace_page_fault)
1043         ASM_CLAC
1044         pushl   $trace_do_page_fault
1045         jmp     error_code
1046 END(trace_page_fault)
1047 #endif
1048
1049 ENTRY(page_fault)
1050         ASM_CLAC
1051         pushl   $do_page_fault
1052         ALIGN
1053 error_code:
1054         /* the function address is in %gs's slot on the stack */
1055         pushl   %fs
1056         pushl   %es
1057         pushl   %ds
1058         pushl   %eax
1059         pushl   %ebp
1060         pushl   %edi
1061         pushl   %esi
1062         pushl   %edx
1063         pushl   %ecx
1064         pushl   %ebx
1065         cld
1066         movl    $(__KERNEL_PERCPU), %ecx
1067         movl    %ecx, %fs
1068         UNWIND_ESPFIX_STACK
1069         GS_TO_REG %ecx
1070         movl    PT_GS(%esp), %edi               # get the function address
1071         movl    PT_ORIG_EAX(%esp), %edx         # get the error code
1072         movl    $-1, PT_ORIG_EAX(%esp)          # no syscall to restart
1073         REG_TO_PTGS %ecx
1074         SET_KERNEL_GS %ecx
1075         movl    $(__USER_DS), %ecx
1076         movl    %ecx, %ds
1077         movl    %ecx, %es
1078         TRACE_IRQS_OFF
1079         movl    %esp, %eax                      # pt_regs pointer
1080         CALL_NOSPEC %edi
1081         jmp     ret_from_exception
1082 END(page_fault)
1083
1084 ENTRY(debug)
1085         /*
1086          * #DB can happen at the first instruction of
1087          * entry_SYSENTER_32 or in Xen's SYSENTER prologue.  If this
1088          * happens, then we will be running on a very small stack.  We
1089          * need to detect this condition and switch to the thread
1090          * stack before calling any C code at all.
1091          *
1092          * If you edit this code, keep in mind that NMIs can happen in here.
1093          */
1094         ASM_CLAC
1095         pushl   $-1                             # mark this as an int
1096         SAVE_ALL
1097         xorl    %edx, %edx                      # error code 0
1098         movl    %esp, %eax                      # pt_regs pointer
1099
1100         /* Are we currently on the SYSENTER stack? */
1101         PER_CPU(cpu_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx)
1102         subl    %eax, %ecx      /* ecx = (end of SYSENTER_stack) - esp */
1103         cmpl    $SIZEOF_SYSENTER_stack, %ecx
1104         jb      .Ldebug_from_sysenter_stack
1105
1106         TRACE_IRQS_OFF
1107         call    do_debug
1108         jmp     ret_from_exception
1109
1110 .Ldebug_from_sysenter_stack:
1111         /* We're on the SYSENTER stack.  Switch off. */
1112         movl    %esp, %ebp
1113         movl    PER_CPU_VAR(cpu_current_top_of_stack), %esp
1114         TRACE_IRQS_OFF
1115         call    do_debug
1116         movl    %ebp, %esp
1117         jmp     ret_from_exception
1118 END(debug)
1119
1120 /*
1121  * NMI is doubly nasty.  It can happen on the first instruction of
1122  * entry_SYSENTER_32 (just like #DB), but it can also interrupt the beginning
1123  * of the #DB handler even if that #DB in turn hit before entry_SYSENTER_32
1124  * switched stacks.  We handle both conditions by simply checking whether we
1125  * interrupted kernel code running on the SYSENTER stack.
1126  */
1127 ENTRY(nmi)
1128         ASM_CLAC
1129 #ifdef CONFIG_X86_ESPFIX32
1130         pushl   %eax
1131         movl    %ss, %eax
1132         cmpw    $__ESPFIX_SS, %ax
1133         popl    %eax
1134         je      nmi_espfix_stack
1135 #endif
1136
1137         pushl   %eax                            # pt_regs->orig_ax
1138         SAVE_ALL
1139         xorl    %edx, %edx                      # zero error code
1140         movl    %esp, %eax                      # pt_regs pointer
1141
1142         /* Are we currently on the SYSENTER stack? */
1143         PER_CPU(cpu_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx)
1144         subl    %eax, %ecx      /* ecx = (end of SYSENTER_stack) - esp */
1145         cmpl    $SIZEOF_SYSENTER_stack, %ecx
1146         jb      .Lnmi_from_sysenter_stack
1147
1148         /* Not on SYSENTER stack. */
1149         call    do_nmi
1150         jmp     restore_all_notrace
1151
1152 .Lnmi_from_sysenter_stack:
1153         /*
1154          * We're on the SYSENTER stack.  Switch off.  No one (not even debug)
1155          * is using the thread stack right now, so it's safe for us to use it.
1156          */
1157         movl    %esp, %ebp
1158         movl    PER_CPU_VAR(cpu_current_top_of_stack), %esp
1159         call    do_nmi
1160         movl    %ebp, %esp
1161         jmp     restore_all_notrace
1162
1163 #ifdef CONFIG_X86_ESPFIX32
1164 nmi_espfix_stack:
1165         /*
1166          * create the pointer to lss back
1167          */
1168         pushl   %ss
1169         pushl   %esp
1170         addl    $4, (%esp)
1171         /* copy the iret frame of 12 bytes */
1172         .rept 3
1173         pushl   16(%esp)
1174         .endr
1175         pushl   %eax
1176         SAVE_ALL
1177         FIXUP_ESPFIX_STACK                      # %eax == %esp
1178         xorl    %edx, %edx                      # zero error code
1179         call    do_nmi
1180         RESTORE_REGS
1181         lss     12+4(%esp), %esp                # back to espfix stack
1182         jmp     irq_return
1183 #endif
1184 END(nmi)
1185
1186 ENTRY(int3)
1187         ASM_CLAC
1188         pushl   $-1                             # mark this as an int
1189         SAVE_ALL
1190         TRACE_IRQS_OFF
1191         xorl    %edx, %edx                      # zero error code
1192         movl    %esp, %eax                      # pt_regs pointer
1193         call    do_int3
1194         jmp     ret_from_exception
1195 END(int3)
1196
1197 ENTRY(general_protection)
1198         ASM_CLAC
1199         pushl   $do_general_protection
1200         jmp     error_code
1201 END(general_protection)
1202
1203 #ifdef CONFIG_KVM_GUEST
1204 ENTRY(async_page_fault)
1205         ASM_CLAC
1206         pushl   $do_async_page_fault
1207         jmp     error_code
1208 END(async_page_fault)
1209 #endif
1210
1211 ENTRY(rewind_stack_do_exit)
1212         /* Prevent any naive code from trying to unwind to our caller. */
1213         xorl    %ebp, %ebp
1214
1215         movl    PER_CPU_VAR(cpu_current_top_of_stack), %esi
1216         leal    -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%esi), %esp
1217
1218         call    do_exit
1219 1:      jmp 1b
1220 END(rewind_stack_do_exit)