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