Added patch headers.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / entry_32-xen.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 <asm/thread_info.h>
46 #include <asm/irqflags.h>
47 #include <asm/errno.h>
48 #include <asm/segment.h>
49 #include <asm/smp.h>
50 #include <asm/page_types.h>
51 #include <asm/percpu.h>
52 #include <asm/dwarf2.h>
53 #include <asm/processor-flags.h>
54 #include <asm/ftrace.h>
55 #include <asm/irq_vectors.h>
56 #include <xen/interface/xen.h>
57
58 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this.  */
59 #include <linux/elf-em.h>
60 #define AUDIT_ARCH_I386         (EM_386|__AUDIT_ARCH_LE)
61 #define __AUDIT_ARCH_LE    0x40000000
62
63 #ifndef CONFIG_AUDITSYSCALL
64 #define sysenter_audit  syscall_trace_entry
65 #define sysexit_audit   syscall_exit_work
66 #endif
67
68 /*
69  * We use macros for low-level operations which need to be overridden
70  * for paravirtualization.  The following will never clobber any registers:
71  *   INTERRUPT_RETURN (aka. "iret")
72  *   GET_CR0_INTO_EAX (aka. "movl %cr0, %eax")
73  *   ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
74  *
75  * For DISABLE_INTERRUPTS/ENABLE_INTERRUPTS (aka "cli"/"sti"), you must
76  * specify what registers can be overwritten (CLBR_NONE, CLBR_EAX/EDX/ECX/ANY).
77  * Allowing a register to be clobbered can shrink the paravirt replacement
78  * enough to patch inline, increasing performance.
79  */
80
81 #define nr_syscalls ((syscall_table_size)/4)
82
83 /* Pseudo-eflags. */
84 NMI_MASK        = 0x80000000
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 #ifdef CONFIG_VM86
103 #define resume_userspace_sig    check_userspace
104 #else
105 #define resume_userspace_sig    resume_userspace
106 #endif
107
108 /*
109  * User gs save/restore
110  *
111  * %gs is used for userland TLS and kernel only uses it for stack
112  * canary which is required to be at %gs:20 by gcc.  Read the comment
113  * at the top of stackprotector.h for more info.
114  *
115  * Local labels 98 and 99 are used.
116  */
117 #ifdef CONFIG_X86_32_LAZY_GS
118
119  /* unfortunately push/pop can't be no-op */
120 .macro PUSH_GS
121         pushl $0
122         CFI_ADJUST_CFA_OFFSET 4
123 .endm
124 .macro POP_GS pop=0
125         addl $(4 + \pop), %esp
126         CFI_ADJUST_CFA_OFFSET -(4 + \pop)
127 .endm
128 .macro POP_GS_EX
129 .endm
130
131  /* all the rest are no-op */
132 .macro PTGS_TO_GS
133 .endm
134 .macro PTGS_TO_GS_EX
135 .endm
136 .macro GS_TO_REG reg
137 .endm
138 .macro REG_TO_PTGS reg
139 .endm
140 .macro SET_KERNEL_GS reg
141 .endm
142
143 #else   /* CONFIG_X86_32_LAZY_GS */
144
145 .macro PUSH_GS
146         pushl %gs
147         CFI_ADJUST_CFA_OFFSET 4
148         /*CFI_REL_OFFSET gs, 0*/
149 .endm
150
151 .macro POP_GS pop=0
152 98:     popl %gs
153         CFI_ADJUST_CFA_OFFSET -4
154         /*CFI_RESTORE gs*/
155   .if \pop <> 0
156         add $\pop, %esp
157         CFI_ADJUST_CFA_OFFSET -\pop
158   .endif
159 .endm
160 .macro POP_GS_EX
161 .pushsection .fixup, "ax"
162 99:     movl $0, (%esp)
163         jmp 98b
164 .section __ex_table, "a"
165         .align 4
166         .long 98b, 99b
167 .popsection
168 .endm
169
170 .macro PTGS_TO_GS
171 98:     mov PT_GS(%esp), %gs
172 .endm
173 .macro PTGS_TO_GS_EX
174 .pushsection .fixup, "ax"
175 99:     movl $0, PT_GS(%esp)
176         jmp 98b
177 .section __ex_table, "a"
178         .align 4
179         .long 98b, 99b
180 .popsection
181 .endm
182
183 .macro GS_TO_REG reg
184         movl %gs, \reg
185         /*CFI_REGISTER gs, \reg*/
186 .endm
187 .macro REG_TO_PTGS reg
188         movl \reg, PT_GS(%esp)
189         /*CFI_REL_OFFSET gs, PT_GS*/
190 .endm
191 .macro SET_KERNEL_GS reg
192         movl $(__KERNEL_STACK_CANARY), \reg
193         movl \reg, %gs
194 .endm
195
196 #endif  /* CONFIG_X86_32_LAZY_GS */
197
198 .macro SAVE_ALL
199         cld
200         PUSH_GS
201         pushl %fs
202         CFI_ADJUST_CFA_OFFSET 4
203         /*CFI_REL_OFFSET fs, 0;*/
204         pushl %es
205         CFI_ADJUST_CFA_OFFSET 4
206         /*CFI_REL_OFFSET es, 0;*/
207         pushl %ds
208         CFI_ADJUST_CFA_OFFSET 4
209         /*CFI_REL_OFFSET ds, 0;*/
210         pushl %eax
211         CFI_ADJUST_CFA_OFFSET 4
212         CFI_REL_OFFSET eax, 0
213         pushl %ebp
214         CFI_ADJUST_CFA_OFFSET 4
215         CFI_REL_OFFSET ebp, 0
216         pushl %edi
217         CFI_ADJUST_CFA_OFFSET 4
218         CFI_REL_OFFSET edi, 0
219         pushl %esi
220         CFI_ADJUST_CFA_OFFSET 4
221         CFI_REL_OFFSET esi, 0
222         pushl %edx
223         CFI_ADJUST_CFA_OFFSET 4
224         CFI_REL_OFFSET edx, 0
225         pushl %ecx
226         CFI_ADJUST_CFA_OFFSET 4
227         CFI_REL_OFFSET ecx, 0
228         pushl %ebx
229         CFI_ADJUST_CFA_OFFSET 4
230         CFI_REL_OFFSET ebx, 0
231         movl $(__USER_DS), %edx
232         movl %edx, %ds
233         movl %edx, %es
234         movl $(__KERNEL_PERCPU), %edx
235         movl %edx, %fs
236         SET_KERNEL_GS %edx
237 .endm
238
239 .macro RESTORE_INT_REGS
240         popl %ebx
241         CFI_ADJUST_CFA_OFFSET -4
242         CFI_RESTORE ebx
243         popl %ecx
244         CFI_ADJUST_CFA_OFFSET -4
245         CFI_RESTORE ecx
246         popl %edx
247         CFI_ADJUST_CFA_OFFSET -4
248         CFI_RESTORE edx
249         popl %esi
250         CFI_ADJUST_CFA_OFFSET -4
251         CFI_RESTORE esi
252         popl %edi
253         CFI_ADJUST_CFA_OFFSET -4
254         CFI_RESTORE edi
255         popl %ebp
256         CFI_ADJUST_CFA_OFFSET -4
257         CFI_RESTORE ebp
258         popl %eax
259         CFI_ADJUST_CFA_OFFSET -4
260         CFI_RESTORE eax
261 .endm
262
263 .macro RESTORE_REGS pop=0
264         RESTORE_INT_REGS
265 1:      popl %ds
266         CFI_ADJUST_CFA_OFFSET -4
267         /*CFI_RESTORE ds;*/
268 2:      popl %es
269         CFI_ADJUST_CFA_OFFSET -4
270         /*CFI_RESTORE es;*/
271 3:      popl %fs
272         CFI_ADJUST_CFA_OFFSET -4
273         /*CFI_RESTORE fs;*/
274         POP_GS \pop
275 .pushsection .fixup, "ax"
276 4:      movl $0, (%esp)
277         jmp 1b
278 5:      movl $0, (%esp)
279         jmp 2b
280 6:      movl $0, (%esp)
281         jmp 3b
282 .section __ex_table, "a"
283         .align 4
284         .long 1b, 4b
285         .long 2b, 5b
286         .long 3b, 6b
287 .popsection
288         POP_GS_EX
289 .endm
290
291 .macro RING0_INT_FRAME
292         CFI_STARTPROC simple
293         CFI_SIGNAL_FRAME
294         CFI_DEF_CFA esp, 3*4
295         /*CFI_OFFSET cs, -2*4;*/
296         CFI_OFFSET eip, -3*4
297 .endm
298
299 .macro RING0_EC_FRAME
300         CFI_STARTPROC simple
301         CFI_SIGNAL_FRAME
302         CFI_DEF_CFA esp, 4*4
303         /*CFI_OFFSET cs, -2*4;*/
304         CFI_OFFSET eip, -3*4
305 .endm
306
307 .macro RING0_PTREGS_FRAME
308         CFI_STARTPROC simple
309         CFI_SIGNAL_FRAME
310         CFI_DEF_CFA esp, PT_OLDESP-PT_EBX
311         /*CFI_OFFSET cs, PT_CS-PT_OLDESP;*/
312         CFI_OFFSET eip, PT_EIP-PT_OLDESP
313         /*CFI_OFFSET es, PT_ES-PT_OLDESP;*/
314         /*CFI_OFFSET ds, PT_DS-PT_OLDESP;*/
315         CFI_OFFSET eax, PT_EAX-PT_OLDESP
316         CFI_OFFSET ebp, PT_EBP-PT_OLDESP
317         CFI_OFFSET edi, PT_EDI-PT_OLDESP
318         CFI_OFFSET esi, PT_ESI-PT_OLDESP
319         CFI_OFFSET edx, PT_EDX-PT_OLDESP
320         CFI_OFFSET ecx, PT_ECX-PT_OLDESP
321         CFI_OFFSET ebx, PT_EBX-PT_OLDESP
322 .endm
323
324 ENTRY(ret_from_fork)
325         CFI_STARTPROC
326         pushl %eax
327         CFI_ADJUST_CFA_OFFSET 4
328         call schedule_tail
329         GET_THREAD_INFO(%ebp)
330         popl %eax
331         CFI_ADJUST_CFA_OFFSET -4
332         pushl $0x0202                   # Reset kernel eflags
333         CFI_ADJUST_CFA_OFFSET 4
334         popfl
335         CFI_ADJUST_CFA_OFFSET -4
336         jmp syscall_exit
337         CFI_ENDPROC
338 END(ret_from_fork)
339
340 /*
341  * Interrupt exit functions should be protected against kprobes
342  */
343         .pushsection .kprobes.text, "ax"
344 /*
345  * Return to user mode is not as complex as all this looks,
346  * but we want the default path for a system call return to
347  * go as quickly as possible which is why some of this is
348  * less clear than it otherwise should be.
349  */
350
351         # userspace resumption stub bypassing syscall exit tracing
352         ALIGN
353         RING0_PTREGS_FRAME
354 ret_from_exception:
355         preempt_stop(CLBR_ANY)
356 ret_from_intr:
357         GET_THREAD_INFO(%ebp)
358 check_userspace:
359         movl PT_EFLAGS(%esp), %eax      # mix EFLAGS and CS
360         movb PT_CS(%esp), %al
361         andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
362         cmpl $USER_RPL, %eax
363         jb resume_kernel                # not returning to v8086 or userspace
364
365 ENTRY(resume_userspace)
366         LOCKDEP_SYS_EXIT
367         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
368                                         # setting need_resched or sigpending
369                                         # between sampling and the iret
370         TRACE_IRQS_OFF
371         movl TI_flags(%ebp), %ecx
372         andl $_TIF_WORK_MASK, %ecx      # is there any work to be done on
373                                         # int/exception return?
374         jne work_pending
375         jmp restore_all
376 END(ret_from_exception)
377
378 #ifdef CONFIG_PREEMPT
379 ENTRY(resume_kernel)
380         DISABLE_INTERRUPTS(CLBR_ANY)
381         cmpl $0,TI_preempt_count(%ebp)  # non-zero preempt_count ?
382         jnz restore_all
383 need_resched:
384         movl TI_flags(%ebp), %ecx       # need_resched set ?
385         testb $_TIF_NEED_RESCHED, %cl
386         jz restore_all
387         testl $X86_EFLAGS_IF,PT_EFLAGS(%esp)    # interrupts off (exception path) ?
388         jz restore_all
389         call preempt_schedule_irq
390         jmp need_resched
391 END(resume_kernel)
392 #endif
393         CFI_ENDPROC
394 /*
395  * End of kprobes section
396  */
397         .popsection
398
399 /* SYSENTER_RETURN points to after the "sysenter" instruction in
400    the vsyscall page.  See vsyscall-sysentry.S, which defines the symbol.  */
401
402         # sysenter call handler stub
403 ENTRY(ia32_sysenter_target)
404         CFI_STARTPROC simple
405         CFI_SIGNAL_FRAME
406         CFI_DEF_CFA esp, 0
407         CFI_REGISTER esp, ebp
408         movl SYSENTER_stack_sp0(%esp),%esp
409 sysenter_past_esp:
410         /*
411          * Interrupts are disabled here, but we can't trace it until
412          * enough kernel state to call TRACE_IRQS_OFF can be called - but
413          * we immediately enable interrupts at that point anyway.
414          */
415         pushl $(__USER_DS)
416         CFI_ADJUST_CFA_OFFSET 4
417         /*CFI_REL_OFFSET ss, 0*/
418         pushl %ebp
419         CFI_ADJUST_CFA_OFFSET 4
420         CFI_REL_OFFSET esp, 0
421         pushfl
422         orl $X86_EFLAGS_IF, (%esp)
423         CFI_ADJUST_CFA_OFFSET 4
424         pushl $(__USER_CS)
425         CFI_ADJUST_CFA_OFFSET 4
426         /*CFI_REL_OFFSET cs, 0*/
427         /*
428          * Push current_thread_info()->sysenter_return to the stack.
429          * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
430          * pushed above; +8 corresponds to copy_thread's esp0 setting.
431          */
432         pushl (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
433         CFI_ADJUST_CFA_OFFSET 4
434         CFI_REL_OFFSET eip, 0
435
436         pushl %eax
437         CFI_ADJUST_CFA_OFFSET 4
438         SAVE_ALL
439         ENABLE_INTERRUPTS(CLBR_NONE)
440
441 /*
442  * Load the potential sixth argument from user stack.
443  * Careful about security.
444  */
445         cmpl $__PAGE_OFFSET-3,%ebp
446         jae syscall_fault
447 1:      movl (%ebp),%ebp
448         movl %ebp,PT_EBP(%esp)
449 .section __ex_table,"a"
450         .align 4
451         .long 1b,syscall_fault
452 .previous
453
454         GET_THREAD_INFO(%ebp)
455
456         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
457         jnz sysenter_audit
458 sysenter_do_call:
459         cmpl $(nr_syscalls), %eax
460         jae syscall_badsys
461         call *sys_call_table(,%eax,4)
462         movl %eax,PT_EAX(%esp)
463         LOCKDEP_SYS_EXIT
464         DISABLE_INTERRUPTS(CLBR_ANY)
465         TRACE_IRQS_OFF
466         movl TI_flags(%ebp), %ecx
467         testl $_TIF_ALLWORK_MASK, %ecx
468         jne sysexit_audit
469 sysenter_exit:
470 /* if something modifies registers it must also disable sysexit */
471         movl PT_EIP(%esp), %edx
472         movl PT_OLDESP(%esp), %ecx
473         xorl %ebp,%ebp
474 #ifdef CONFIG_XEN_VCPU_INFO_PLACEMENT
475         GET_VCPU_INFO
476 #endif
477         TRACE_IRQS_ON
478 1:      mov  PT_FS(%esp), %fs
479         PTGS_TO_GS
480         ENABLE_INTERRUPTS_SYSEXIT
481
482 #ifdef CONFIG_AUDITSYSCALL
483 sysenter_audit:
484         testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
485         jnz syscall_trace_entry
486         addl $4,%esp
487         CFI_ADJUST_CFA_OFFSET -4
488         /* %esi already in 8(%esp)         6th arg: 4th syscall arg */
489         /* %edx already in 4(%esp)         5th arg: 3rd syscall arg */
490         /* %ecx already in 0(%esp)         4th arg: 2nd syscall arg */
491         movl %ebx,%ecx                  /* 3rd arg: 1st syscall arg */
492         movl %eax,%edx                  /* 2nd arg: syscall number */
493         movl $AUDIT_ARCH_I386,%eax      /* 1st arg: audit arch */
494         call audit_syscall_entry
495         pushl %ebx
496         CFI_ADJUST_CFA_OFFSET 4
497         movl PT_EAX(%esp),%eax          /* reload syscall number */
498         jmp sysenter_do_call
499
500 sysexit_audit:
501         testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
502         jne syscall_exit_work
503         TRACE_IRQS_ON
504         ENABLE_INTERRUPTS(CLBR_ANY)
505         movl %eax,%edx          /* second arg, syscall return value */
506         cmpl $0,%eax            /* is it < 0? */
507         setl %al                /* 1 if so, 0 if not */
508         movzbl %al,%eax         /* zero-extend that */
509         inc %eax /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
510         call audit_syscall_exit
511         DISABLE_INTERRUPTS(CLBR_ANY)
512         TRACE_IRQS_OFF
513         movl TI_flags(%ebp), %ecx
514         testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
515         jne syscall_exit_work
516         movl PT_EAX(%esp),%eax  /* reload syscall return value */
517         jmp sysenter_exit
518 #endif
519
520         CFI_ENDPROC
521 .pushsection .fixup,"ax"
522 2:      movl $0,PT_FS(%esp)
523         jmp 1b
524 .section __ex_table,"a"
525         .align 4
526         .long 1b,2b
527 .popsection
528         PTGS_TO_GS_EX
529 ENDPROC(ia32_sysenter_target)
530
531         # pv sysenter call handler stub
532 ENTRY(ia32pv_sysenter_target)
533         RING0_INT_FRAME
534         movl $__USER_DS,16(%esp)
535         movl %ebp,12(%esp)
536         movl $__USER_CS,4(%esp)
537         addl $4,%esp
538         CFI_ADJUST_CFA_OFFSET -4
539         /* +5*4 is SS:ESP,EFLAGS,CS:EIP. +8 is esp0 setting. */
540         pushl (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
541         CFI_ADJUST_CFA_OFFSET 4
542 /*
543  * Load the potential sixth argument from user stack.
544  * Careful about security.
545  */
546         cmpl $__PAGE_OFFSET-3,%ebp
547         jae syscall_fault
548 1:      movl (%ebp),%ebp
549 .section __ex_table,"a"
550         .align 4
551         .long 1b,syscall_fault
552 .previous
553         jmp system_call
554         CFI_ENDPROC
555 ENDPROC(ia32pv_sysenter_target)
556
557 /*
558  * syscall stub including irq exit should be protected against kprobes
559  */
560         .pushsection .kprobes.text, "ax"
561         # system call handler stub
562 ENTRY(system_call)
563         RING0_INT_FRAME                 # can't unwind into user space anyway
564         pushl %eax                      # save orig_eax
565         CFI_ADJUST_CFA_OFFSET 4
566         SAVE_ALL
567         GET_THREAD_INFO(%ebp)
568         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
569         jnz syscall_trace_entry
570         cmpl $(nr_syscalls), %eax
571         jae syscall_badsys
572 syscall_call:
573         call *sys_call_table(,%eax,4)
574         movl %eax,PT_EAX(%esp)          # store the return value
575 syscall_exit:
576         LOCKDEP_SYS_EXIT
577         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
578                                         # setting need_resched or sigpending
579                                         # between sampling and the iret
580         TRACE_IRQS_OFF
581         movl TI_flags(%ebp), %ecx
582         testl $_TIF_ALLWORK_MASK, %ecx  # current->work
583         jne syscall_exit_work
584
585 restore_all:
586         TRACE_IRQS_IRET
587 restore_all_notrace:
588 #ifndef CONFIG_XEN
589         movl PT_EFLAGS(%esp), %eax      # mix EFLAGS, SS and CS
590         # Warning: PT_OLDSS(%esp) contains the wrong/random values if we
591         # are returning to the kernel.
592         # See comments in process.c:copy_thread() for details.
593         movb PT_OLDSS(%esp), %ah
594         movb PT_CS(%esp), %al
595         andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
596         cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
597         CFI_REMEMBER_STATE
598         je ldt_ss                       # returning to user-space with LDT SS
599 restore_nocheck:
600 #else
601 restore_nocheck:
602         movl PT_EFLAGS(%esp), %eax
603         testl $(X86_EFLAGS_VM|NMI_MASK), %eax
604         CFI_REMEMBER_STATE
605         jnz hypervisor_iret
606         shr $9, %eax                    # EAX[0] == IRET_EFLAGS.IF
607         GET_VCPU_INFO
608         andb evtchn_upcall_mask(%esi),%al
609         andb $1,%al                     # EAX[0] == IRET_EFLAGS.IF & event_mask
610         CFI_REMEMBER_STATE
611         jnz restore_all_enable_events   #        != 0 => enable event delivery
612 #endif
613         RESTORE_REGS 4                  # skip orig_eax/error_code
614         CFI_ADJUST_CFA_OFFSET -4
615 irq_return:
616         INTERRUPT_RETURN
617 .section .fixup,"ax"
618 ENTRY(iret_exc)
619         pushl $0                        # no error code
620         pushl $do_iret_error
621         jmp error_code
622 .previous
623 .section __ex_table,"a"
624         .align 4
625         .long irq_return,iret_exc
626 .previous
627
628         CFI_RESTORE_STATE
629 #ifndef CONFIG_XEN
630 ldt_ss:
631         larl PT_OLDSS(%esp), %eax
632         jnz restore_nocheck
633         testl $0x00400000, %eax         # returning to 32bit stack?
634         jnz restore_nocheck             # allright, normal return
635
636 #ifdef CONFIG_PARAVIRT
637         /*
638          * The kernel can't run on a non-flat stack if paravirt mode
639          * is active.  Rather than try to fixup the high bits of
640          * ESP, bypass this code entirely.  This may break DOSemu
641          * and/or Wine support in a paravirt VM, although the option
642          * is still available to implement the setting of the high
643          * 16-bits in the INTERRUPT_RETURN paravirt-op.
644          */
645         cmpl $0, pv_info+PARAVIRT_enabled
646         jne restore_nocheck
647 #endif
648
649 /*
650  * Setup and switch to ESPFIX stack
651  *
652  * We're returning to userspace with a 16 bit stack. The CPU will not
653  * restore the high word of ESP for us on executing iret... This is an
654  * "official" bug of all the x86-compatible CPUs, which we can work
655  * around to make dosemu and wine happy. We do this by preloading the
656  * high word of ESP with the high word of the userspace ESP while
657  * compensating for the offset by changing to the ESPFIX segment with
658  * a base address that matches for the difference.
659  */
660         mov %esp, %edx                  /* load kernel esp */
661         mov PT_OLDESP(%esp), %eax       /* load userspace esp */
662         mov %dx, %ax                    /* eax: new kernel esp */
663         sub %eax, %edx                  /* offset (low word is 0) */
664         PER_CPU(gdt_page, %ebx)
665         shr $16, %edx
666         mov %dl, GDT_ENTRY_ESPFIX_SS * 8 + 4(%ebx) /* bits 16..23 */
667         mov %dh, GDT_ENTRY_ESPFIX_SS * 8 + 7(%ebx) /* bits 24..31 */
668         pushl $__ESPFIX_SS
669         CFI_ADJUST_CFA_OFFSET 4
670         push %eax                       /* new kernel esp */
671         CFI_ADJUST_CFA_OFFSET 4
672         /* Disable interrupts, but do not irqtrace this section: we
673          * will soon execute iret and the tracer was already set to
674          * the irqstate after the iret */
675         DISABLE_INTERRUPTS(CLBR_EAX)
676         lss (%esp), %esp                /* switch to espfix segment */
677         CFI_ADJUST_CFA_OFFSET -8
678         jmp restore_nocheck
679 #else
680         ALIGN
681 restore_all_enable_events:
682         TRACE_IRQS_ON
683         __ENABLE_INTERRUPTS
684 scrit:  /**** START OF CRITICAL REGION ****/
685         __TEST_PENDING
686         jnz  14f                        # process more events if necessary...
687         RESTORE_REGS 4
688 1:      INTERRUPT_RETURN
689 .section __ex_table,"a"
690         .align 4
691         .long 1b,iret_exc
692 .previous
693 14:     __DISABLE_INTERRUPTS
694         TRACE_IRQS_OFF
695 ecrit:  /**** END OF CRITICAL REGION ****/
696         jmp  .Ldo_upcall
697
698         CFI_RESTORE_STATE
699 hypervisor_iret:
700         andl $~NMI_MASK, PT_EFLAGS(%esp)
701         RESTORE_REGS 4
702         jmp  hypercall_page + (__HYPERVISOR_iret * 32)
703 #endif
704         CFI_ENDPROC
705 ENDPROC(system_call)
706
707         # perform work that needs to be done immediately before resumption
708         ALIGN
709         RING0_PTREGS_FRAME              # can't unwind into user space anyway
710 work_pending:
711         testb $_TIF_NEED_RESCHED, %cl
712         jz work_notifysig
713 work_resched:
714         call schedule
715         LOCKDEP_SYS_EXIT
716         DISABLE_INTERRUPTS(CLBR_ANY)    # make sure we don't miss an interrupt
717                                         # setting need_resched or sigpending
718                                         # between sampling and the iret
719         TRACE_IRQS_OFF
720         movl TI_flags(%ebp), %ecx
721         andl $_TIF_WORK_MASK, %ecx      # is there any work to be done other
722                                         # than syscall tracing?
723         jz restore_all
724         testb $_TIF_NEED_RESCHED, %cl
725         jnz work_resched
726
727 work_notifysig:                         # deal with pending signals and
728                                         # notify-resume requests
729 #ifdef CONFIG_VM86
730         testl $X86_EFLAGS_VM, PT_EFLAGS(%esp)
731         movl %esp, %eax
732         jne work_notifysig_v86          # returning to kernel-space or
733                                         # vm86-space
734         xorl %edx, %edx
735         call do_notify_resume
736         jmp resume_userspace_sig
737
738         ALIGN
739 work_notifysig_v86:
740         pushl %ecx                      # save ti_flags for do_notify_resume
741         CFI_ADJUST_CFA_OFFSET 4
742         call save_v86_state             # %eax contains pt_regs pointer
743         popl %ecx
744         CFI_ADJUST_CFA_OFFSET -4
745         movl %eax, %esp
746 #else
747         movl %esp, %eax
748 #endif
749         xorl %edx, %edx
750         call do_notify_resume
751         jmp resume_userspace_sig
752 END(work_pending)
753
754         # perform syscall exit tracing
755         ALIGN
756 syscall_trace_entry:
757         movl $-ENOSYS,PT_EAX(%esp)
758         movl %esp, %eax
759         call syscall_trace_enter
760         /* What it returned is what we'll actually use.  */
761         cmpl $(nr_syscalls), %eax
762         jnae syscall_call
763         jmp syscall_exit
764 END(syscall_trace_entry)
765
766         # perform syscall exit tracing
767         ALIGN
768 syscall_exit_work:
769         testl $_TIF_WORK_SYSCALL_EXIT, %ecx
770         jz work_pending
771         TRACE_IRQS_ON
772         ENABLE_INTERRUPTS(CLBR_ANY)     # could let syscall_trace_leave() call
773                                         # schedule() instead
774         movl %esp, %eax
775         call syscall_trace_leave
776         jmp resume_userspace
777 END(syscall_exit_work)
778         CFI_ENDPROC
779
780         RING0_INT_FRAME                 # can't unwind into user space anyway
781 syscall_fault:
782         GET_THREAD_INFO(%ebp)
783         movl $-EFAULT,PT_EAX(%esp)
784         jmp resume_userspace
785 END(syscall_fault)
786
787 syscall_badsys:
788         movl $-ENOSYS,PT_EAX(%esp)
789         jmp resume_userspace
790 END(syscall_badsys)
791         CFI_ENDPROC
792 /*
793  * End of kprobes section
794  */
795         .popsection
796
797 /*
798  * System calls that need a pt_regs pointer.
799  */
800 #define PTREGSCALL0(name) \
801         ALIGN; \
802 ptregs_##name: \
803         leal 4(%esp),%eax; \
804         jmp sys_##name;
805
806 #define PTREGSCALL1(name) \
807         ALIGN; \
808 ptregs_##name: \
809         leal 4(%esp),%edx; \
810         movl (PT_EBX+4)(%esp),%eax; \
811         jmp sys_##name;
812
813 #define PTREGSCALL2(name) \
814         ALIGN; \
815 ptregs_##name: \
816         leal 4(%esp),%ecx; \
817         movl (PT_ECX+4)(%esp),%edx; \
818         movl (PT_EBX+4)(%esp),%eax; \
819         jmp sys_##name;
820
821 #define PTREGSCALL3(name) \
822         ALIGN; \
823 ptregs_##name: \
824         leal 4(%esp),%eax; \
825         pushl %eax; \
826         movl PT_EDX(%eax),%ecx; \
827         movl PT_ECX(%eax),%edx; \
828         movl PT_EBX(%eax),%eax; \
829         call sys_##name; \
830         addl $4,%esp; \
831         ret
832
833 PTREGSCALL1(iopl)
834 PTREGSCALL0(fork)
835 PTREGSCALL0(vfork)
836 PTREGSCALL3(execve)
837 PTREGSCALL2(sigaltstack)
838 PTREGSCALL0(sigreturn)
839 PTREGSCALL0(rt_sigreturn)
840 PTREGSCALL2(vm86)
841 PTREGSCALL1(vm86old)
842
843 /* Clone is an oddball.  The 4th arg is in %edi */
844         ALIGN;
845 ptregs_clone:
846         leal 4(%esp),%eax
847         pushl %eax
848         pushl PT_EDI(%eax)
849         movl PT_EDX(%eax),%ecx
850         movl PT_ECX(%eax),%edx
851         movl PT_EBX(%eax),%eax
852         call sys_clone
853         addl $8,%esp
854         ret
855
856 #ifndef CONFIG_XEN
857 .macro FIXUP_ESPFIX_STACK
858 /*
859  * Switch back for ESPFIX stack to the normal zerobased stack
860  *
861  * We can't call C functions using the ESPFIX stack. This code reads
862  * the high word of the segment base from the GDT and swiches to the
863  * normal stack and adjusts ESP with the matching offset.
864  */
865         /* fixup the stack */
866         PER_CPU(gdt_page, %ebx)
867         mov GDT_ENTRY_ESPFIX_SS * 8 + 4(%ebx), %al /* bits 16..23 */
868         mov GDT_ENTRY_ESPFIX_SS * 8 + 7(%ebx), %ah /* bits 24..31 */
869         shl $16, %eax
870         addl %esp, %eax                 /* the adjusted stack pointer */
871         pushl $__KERNEL_DS
872         CFI_ADJUST_CFA_OFFSET 4
873         pushl %eax
874         CFI_ADJUST_CFA_OFFSET 4
875         lss (%esp), %esp                /* switch to the normal stack segment */
876         CFI_ADJUST_CFA_OFFSET -8
877 .endm
878 .macro UNWIND_ESPFIX_STACK
879         movl %ss, %eax
880         /* see if on espfix stack */
881         cmpw $__ESPFIX_SS, %ax
882         jne 27f
883         movl $__KERNEL_DS, %eax
884         movl %eax, %ds
885         movl %eax, %es
886         /* switch to normal stack */
887         FIXUP_ESPFIX_STACK
888 27:
889 .endm
890
891 /*
892  * Build the entry stubs and pointer table with some assembler magic.
893  * We pack 7 stubs into a single 32-byte chunk, which will fit in a
894  * single cache line on all modern x86 implementations.
895  */
896 .section .init.rodata,"a"
897 ENTRY(interrupt)
898 .text
899         .p2align 5
900         .p2align CONFIG_X86_L1_CACHE_SHIFT
901 ENTRY(irq_entries_start)
902         RING0_INT_FRAME
903 vector=FIRST_EXTERNAL_VECTOR
904 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
905         .balign 32
906   .rept 7
907     .if vector < NR_VECTORS
908       .if vector <> FIRST_EXTERNAL_VECTOR
909         CFI_ADJUST_CFA_OFFSET -4
910       .endif
911 1:      pushl $(~vector+0x80)   /* Note: always in signed byte range */
912         CFI_ADJUST_CFA_OFFSET 4
913       .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
914         jmp 2f
915       .endif
916       .previous
917         .long 1b
918       .text
919 vector=vector+1
920     .endif
921   .endr
922 2:      jmp common_interrupt
923 .endr
924 END(irq_entries_start)
925
926 .previous
927 END(interrupt)
928 .previous
929
930 /*
931  * the CPU automatically disables interrupts when executing an IRQ vector,
932  * so IRQ-flags tracing has to follow that:
933  */
934         .p2align CONFIG_X86_L1_CACHE_SHIFT
935 common_interrupt:
936         addl $-0x80,(%esp)      /* Adjust vector into the [-256,-1] range */
937         SAVE_ALL
938         TRACE_IRQS_OFF
939         movl %esp,%eax
940         call do_IRQ
941         jmp ret_from_intr
942 ENDPROC(common_interrupt)
943         CFI_ENDPROC
944
945 /*
946  *  Irq entries should be protected against kprobes
947  */
948         .pushsection .kprobes.text, "ax"
949 #define BUILD_INTERRUPT3(name, nr, fn)  \
950 ENTRY(name)                             \
951         RING0_INT_FRAME;                \
952         pushl $~(nr);                   \
953         CFI_ADJUST_CFA_OFFSET 4;        \
954         SAVE_ALL;                       \
955         TRACE_IRQS_OFF                  \
956         movl %esp,%eax;                 \
957         call fn;                        \
958         jmp ret_from_intr;              \
959         CFI_ENDPROC;                    \
960 ENDPROC(name)
961
962 #define BUILD_INTERRUPT(name, nr)       BUILD_INTERRUPT3(name, nr, smp_##name)
963
964 /* The include is where all of the SMP etc. interrupts come from */
965 #include <asm/entry_arch.h>
966
967 #else
968 #define UNWIND_ESPFIX_STACK
969
970         .pushsection .kprobes.text, "ax"
971
972 # A note on the "critical region" in our callback handler.
973 # We want to avoid stacking callback handlers due to events occurring
974 # during handling of the last event. To do this, we keep events disabled
975 # until we've done all processing. HOWEVER, we must enable events before
976 # popping the stack frame (can't be done atomically) and so it would still
977 # be possible to get enough handler activations to overflow the stack.
978 # Although unlikely, bugs of that kind are hard to track down, so we'd
979 # like to avoid the possibility.
980 # So, on entry to the handler we detect whether we interrupted an
981 # existing activation in its critical region -- if so, we pop the current
982 # activation and restart the handler using the previous one.
983 #
984 # The sysexit critical region is slightly different. sysexit
985 # atomically removes the entire stack frame. If we interrupt in the
986 # critical region we know that the entire frame is present and correct
987 # so we can simply throw away the new one.
988 ENTRY(hypervisor_callback)
989         RING0_INT_FRAME
990         pushl %eax
991         CFI_ADJUST_CFA_OFFSET 4
992         SAVE_ALL
993         movl PT_CS(%esp),%ecx
994         movl PT_EIP(%esp),%eax
995         andl $SEGMENT_RPL_MASK,%ecx
996         cmpl $USER_RPL,%ecx
997         jae  .Ldo_upcall
998         cmpl $scrit,%eax
999         jb   0f
1000         cmpl $ecrit,%eax
1001         jb   critical_region_fixup
1002 0:
1003 #ifdef CONFIG_XEN_SUPERVISOR_MODE_KERNEL
1004         cmpl $sysexit_scrit,%eax
1005         jb   .Ldo_upcall
1006         cmpl $sysexit_ecrit,%eax
1007         ja   .Ldo_upcall
1008         addl $PT_OLDESP,%esp            # Remove eflags...ebx from stack frame.
1009 #endif
1010 .Ldo_upcall:
1011         push %esp
1012         CFI_ADJUST_CFA_OFFSET 4
1013         call evtchn_do_upcall
1014         add  $4,%esp
1015         CFI_ADJUST_CFA_OFFSET -4
1016         jmp  ret_from_intr
1017         CFI_ENDPROC
1018
1019 # [How we do the fixup]. We want to merge the current stack frame with the
1020 # just-interrupted frame. How we do this depends on where in the critical
1021 # region the interrupted handler was executing, and so how many saved
1022 # registers are in each frame. We do this quickly using the lookup table
1023 # 'critical_fixup_table'. For each byte offset in the critical region, it
1024 # provides the number of bytes which have already been popped from the
1025 # interrupted stack frame.
1026 critical_region_fixup:
1027         movsbl critical_fixup_table-scrit(%eax),%ecx # %ecx contains num slots popped
1028         testl %ecx,%ecx
1029         leal (%esp,%ecx,4),%esi         # %esi points at end of src region
1030         leal PT_OLDESP(%esp),%edi       # %edi points at end of dst region
1031         jle   17f                       # skip loop if nothing to copy
1032 16:     subl $4,%esi                    # pre-decrementing copy loop
1033         subl $4,%edi
1034         movl (%esi),%eax
1035         movl %eax,(%edi)
1036         loop 16b
1037 17:     movl %edi,%esp                  # final %edi is top of merged stack
1038         jmp  .Ldo_upcall
1039
1040 .section .rodata,"a"
1041 critical_fixup_table:
1042         .rept __SIZEOF_TEST_PENDING
1043         .byte -1
1044         .endr
1045         .byte -1,-1                     # jnz  14f
1046         .byte 0                         # pop  %ebx
1047         .byte 1                         # pop  %ecx
1048         .byte 2                         # pop  %edx
1049         .byte 3                         # pop  %esi
1050         .byte 4                         # pop  %edi
1051         .byte 5                         # pop  %ebp
1052         .byte 6                         # pop  %eax
1053         .byte 7                         # pop  %ds
1054         .byte 8                         # pop  %es
1055         .byte 9,9                       # pop  %fs
1056 #ifndef CONFIG_X86_32_LAZY_GS
1057         .byte 10,10                     # pop  %gs
1058         .byte 11,11,11                  # add  $4,%esp
1059 #else
1060         .byte 10,10,10                  # add  $8,%esp
1061 #endif
1062         .byte 12                        # iret
1063         .rept __SIZEOF_DISABLE_INTERRUPTS
1064         .byte -1
1065         .endr
1066 .previous
1067
1068 # Hypervisor uses this for application faults while it executes.
1069 # We get here for two reasons:
1070 #  1. Fault while reloading DS, ES, FS or GS
1071 #  2. Fault while executing IRET
1072 # Category 1 we fix up by reattempting the load, and zeroing the segment
1073 # register if the load fails.
1074 # Category 2 we fix up by jumping to do_iret_error. We cannot use the
1075 # normal Linux return path in this case because if we use the IRET hypercall
1076 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1077 # We distinguish between categories by maintaining a status value in EAX.
1078 ENTRY(failsafe_callback)
1079         pushl %eax
1080         movl $1,%eax
1081 1:      mov 4(%esp),%ds
1082 2:      mov 8(%esp),%es
1083 3:      mov 12(%esp),%fs
1084 4:      mov 16(%esp),%gs
1085         testl %eax,%eax
1086         popl %eax
1087         jz 5f
1088         addl $16,%esp           # EAX != 0 => Category 2 (Bad IRET)
1089         jmp iret_exc
1090 5:      addl $16,%esp           # EAX == 0 => Category 1 (Bad segment)
1091         RING0_INT_FRAME
1092         pushl $0
1093         SAVE_ALL
1094         jmp ret_from_exception
1095 .section .fixup,"ax";           \
1096 6:      xorl %eax,%eax;         \
1097         movl %eax,4(%esp);      \
1098         jmp 1b;                 \
1099 7:      xorl %eax,%eax;         \
1100         movl %eax,8(%esp);      \
1101         jmp 2b;                 \
1102 8:      xorl %eax,%eax;         \
1103         movl %eax,12(%esp);     \
1104         jmp 3b;                 \
1105 9:      xorl %eax,%eax;         \
1106         movl %eax,16(%esp);     \
1107         jmp 4b;                 \
1108 .previous;                      \
1109 .section __ex_table,"a";        \
1110         .align 4;               \
1111         .long 1b,6b;            \
1112         .long 2b,7b;            \
1113         .long 3b,8b;            \
1114         .long 4b,9b;            \
1115 .previous
1116 #endif
1117         CFI_ENDPROC
1118
1119 ENTRY(coprocessor_error)
1120         RING0_INT_FRAME
1121         pushl $0
1122         CFI_ADJUST_CFA_OFFSET 4
1123         pushl $do_coprocessor_error
1124         CFI_ADJUST_CFA_OFFSET 4
1125         jmp error_code
1126         CFI_ENDPROC
1127 END(coprocessor_error)
1128
1129 ENTRY(simd_coprocessor_error)
1130         RING0_INT_FRAME
1131         pushl $0
1132         CFI_ADJUST_CFA_OFFSET 4
1133         pushl $do_simd_coprocessor_error
1134         CFI_ADJUST_CFA_OFFSET 4
1135         jmp error_code
1136         CFI_ENDPROC
1137 END(simd_coprocessor_error)
1138
1139 ENTRY(device_not_available)
1140         RING0_INT_FRAME
1141         pushl $-1                       # mark this as an int
1142         CFI_ADJUST_CFA_OFFSET 4
1143         pushl $do_device_not_available
1144         CFI_ADJUST_CFA_OFFSET 4
1145         jmp error_code
1146         CFI_ENDPROC
1147 END(device_not_available)
1148
1149 #ifdef CONFIG_PARAVIRT
1150 ENTRY(native_iret)
1151         iret
1152 .section __ex_table,"a"
1153         .align 4
1154         .long native_iret, iret_exc
1155 .previous
1156 END(native_iret)
1157
1158 ENTRY(native_irq_enable_sysexit)
1159         sti
1160         sysexit
1161 END(native_irq_enable_sysexit)
1162 #endif
1163
1164 ENTRY(overflow)
1165         RING0_INT_FRAME
1166         pushl $0
1167         CFI_ADJUST_CFA_OFFSET 4
1168         pushl $do_overflow
1169         CFI_ADJUST_CFA_OFFSET 4
1170         jmp error_code
1171         CFI_ENDPROC
1172 END(overflow)
1173
1174 ENTRY(bounds)
1175         RING0_INT_FRAME
1176         pushl $0
1177         CFI_ADJUST_CFA_OFFSET 4
1178         pushl $do_bounds
1179         CFI_ADJUST_CFA_OFFSET 4
1180         jmp error_code
1181         CFI_ENDPROC
1182 END(bounds)
1183
1184 ENTRY(invalid_op)
1185         RING0_INT_FRAME
1186         pushl $0
1187         CFI_ADJUST_CFA_OFFSET 4
1188         pushl $do_invalid_op
1189         CFI_ADJUST_CFA_OFFSET 4
1190         jmp error_code
1191         CFI_ENDPROC
1192 END(invalid_op)
1193
1194 ENTRY(coprocessor_segment_overrun)
1195         RING0_INT_FRAME
1196         pushl $0
1197         CFI_ADJUST_CFA_OFFSET 4
1198         pushl $do_coprocessor_segment_overrun
1199         CFI_ADJUST_CFA_OFFSET 4
1200         jmp error_code
1201         CFI_ENDPROC
1202 END(coprocessor_segment_overrun)
1203
1204 ENTRY(invalid_TSS)
1205         RING0_EC_FRAME
1206         pushl $do_invalid_TSS
1207         CFI_ADJUST_CFA_OFFSET 4
1208         jmp error_code
1209         CFI_ENDPROC
1210 END(invalid_TSS)
1211
1212 ENTRY(segment_not_present)
1213         RING0_EC_FRAME
1214         pushl $do_segment_not_present
1215         CFI_ADJUST_CFA_OFFSET 4
1216         jmp error_code
1217         CFI_ENDPROC
1218 END(segment_not_present)
1219
1220 ENTRY(stack_segment)
1221         RING0_EC_FRAME
1222         pushl $do_stack_segment
1223         CFI_ADJUST_CFA_OFFSET 4
1224         jmp error_code
1225         CFI_ENDPROC
1226 END(stack_segment)
1227
1228 ENTRY(alignment_check)
1229         RING0_EC_FRAME
1230         pushl $do_alignment_check
1231         CFI_ADJUST_CFA_OFFSET 4
1232         jmp error_code
1233         CFI_ENDPROC
1234 END(alignment_check)
1235
1236 ENTRY(divide_error)
1237         RING0_INT_FRAME
1238         pushl $0                        # no error code
1239         CFI_ADJUST_CFA_OFFSET 4
1240         pushl $do_divide_error
1241         CFI_ADJUST_CFA_OFFSET 4
1242         jmp error_code
1243         CFI_ENDPROC
1244 END(divide_error)
1245
1246 #ifdef CONFIG_X86_MCE
1247 ENTRY(machine_check)
1248         RING0_INT_FRAME
1249         pushl $0
1250         CFI_ADJUST_CFA_OFFSET 4
1251         pushl machine_check_vector
1252         CFI_ADJUST_CFA_OFFSET 4
1253         jmp error_code
1254         CFI_ENDPROC
1255 END(machine_check)
1256 #endif
1257
1258 #ifndef CONFIG_XEN
1259 ENTRY(spurious_interrupt_bug)
1260         RING0_INT_FRAME
1261         pushl $0
1262         CFI_ADJUST_CFA_OFFSET 4
1263         pushl $do_spurious_interrupt_bug
1264         CFI_ADJUST_CFA_OFFSET 4
1265         jmp error_code
1266         CFI_ENDPROC
1267 #endif /* !CONFIG_XEN */
1268
1269 ENTRY(fixup_4gb_segment)
1270         RING0_EC_FRAME
1271         pushl $do_fixup_4gb_segment
1272         CFI_ADJUST_CFA_OFFSET 4
1273         jmp error_code
1274         CFI_ENDPROC
1275 END(spurious_interrupt_bug)
1276 /*
1277  * End of kprobes section
1278  */
1279         .popsection
1280
1281 #ifdef CONFIG_STACK_UNWIND
1282 ENTRY(arch_unwind_init_running)
1283         CFI_STARTPROC
1284         movl    4(%esp), %edx
1285         movl    (%esp), %ecx
1286         leal    4(%esp), %eax
1287         movl    %ebx, PT_EBX(%edx)
1288         xorl    %ebx, %ebx
1289         movl    %ebx, PT_ECX(%edx)
1290         movl    %ebx, PT_EDX(%edx)
1291         movl    %esi, PT_ESI(%edx)
1292         movl    %edi, PT_EDI(%edx)
1293         movl    %ebp, PT_EBP(%edx)
1294         movl    %ebx, PT_EAX(%edx)
1295         movl    $__USER_DS, PT_DS(%edx)
1296         movl    $__USER_DS, PT_ES(%edx)
1297         movl    $__KERNEL_PERCPU, PT_FS(%edx)
1298         movl    $__KERNEL_STACK_CANARY, PT_GS(%edx)
1299         movl    %eax, PT_OLDESP(%edx)
1300         movl    16(%esp), %eax
1301         movl    %ebx, PT_ORIG_EAX(%edx)
1302         movl    %ecx, PT_EIP(%edx)
1303         movl    12(%esp), %ecx
1304         movl    $__KERNEL_CS, PT_CS(%edx)
1305         movl    %eax, 12(%esp)
1306         movl    8(%esp), %eax
1307         movl    %ecx, 8(%esp)
1308         movl    %ebx, PT_EFLAGS(%edx)
1309         movl    PT_EBX(%edx), %ebx
1310         movl    $__KERNEL_DS, PT_OLDSS(%edx)
1311         jmpl    *%eax
1312         CFI_ENDPROC
1313 ENDPROC(arch_unwind_init_running)
1314 #endif
1315
1316 ENTRY(kernel_thread_helper)
1317         pushl $0                # fake return address for unwinder
1318         CFI_STARTPROC
1319         movl %edi,%eax
1320         call *%esi
1321         call do_exit
1322         ud2                     # padding for call trace
1323         CFI_ENDPROC
1324 ENDPROC(kernel_thread_helper)
1325
1326 #ifdef CONFIG_FUNCTION_TRACER
1327 #ifdef CONFIG_DYNAMIC_FTRACE
1328
1329 ENTRY(mcount)
1330         ret
1331 END(mcount)
1332
1333 ENTRY(ftrace_caller)
1334         cmpl $0, function_trace_stop
1335         jne  ftrace_stub
1336
1337         pushl %eax
1338         pushl %ecx
1339         pushl %edx
1340         movl 0xc(%esp), %eax
1341         movl 0x4(%ebp), %edx
1342         subl $MCOUNT_INSN_SIZE, %eax
1343
1344 .globl ftrace_call
1345 ftrace_call:
1346         call ftrace_stub
1347
1348         popl %edx
1349         popl %ecx
1350         popl %eax
1351 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1352 .globl ftrace_graph_call
1353 ftrace_graph_call:
1354         jmp ftrace_stub
1355 #endif
1356
1357 .globl ftrace_stub
1358 ftrace_stub:
1359         ret
1360 END(ftrace_caller)
1361
1362 #else /* ! CONFIG_DYNAMIC_FTRACE */
1363
1364 ENTRY(mcount)
1365         cmpl $0, function_trace_stop
1366         jne  ftrace_stub
1367
1368         cmpl $ftrace_stub, ftrace_trace_function
1369         jnz trace
1370 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1371         cmpl $ftrace_stub, ftrace_graph_return
1372         jnz ftrace_graph_caller
1373
1374         cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
1375         jnz ftrace_graph_caller
1376 #endif
1377 .globl ftrace_stub
1378 ftrace_stub:
1379         ret
1380
1381         /* taken from glibc */
1382 trace:
1383         pushl %eax
1384         pushl %ecx
1385         pushl %edx
1386         movl 0xc(%esp), %eax
1387         movl 0x4(%ebp), %edx
1388         subl $MCOUNT_INSN_SIZE, %eax
1389
1390         call *ftrace_trace_function
1391
1392         popl %edx
1393         popl %ecx
1394         popl %eax
1395         jmp ftrace_stub
1396 END(mcount)
1397 #endif /* CONFIG_DYNAMIC_FTRACE */
1398 #endif /* CONFIG_FUNCTION_TRACER */
1399
1400 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1401 ENTRY(ftrace_graph_caller)
1402         cmpl $0, function_trace_stop
1403         jne ftrace_stub
1404
1405         pushl %eax
1406         pushl %ecx
1407         pushl %edx
1408         movl 0xc(%esp), %edx
1409         lea 0x4(%ebp), %eax
1410         movl (%ebp), %ecx
1411         subl $MCOUNT_INSN_SIZE, %edx
1412         call prepare_ftrace_return
1413         popl %edx
1414         popl %ecx
1415         popl %eax
1416         ret
1417 END(ftrace_graph_caller)
1418
1419 .globl return_to_handler
1420 return_to_handler:
1421         pushl %eax
1422         pushl %edx
1423         movl %ebp, %eax
1424         call ftrace_return_to_handler
1425         movl %eax, %ecx
1426         popl %edx
1427         popl %eax
1428         jmp *%ecx
1429 #endif
1430
1431 #include <asm/alternative-asm.h>
1432
1433         # pv syscall call handler stub
1434 ENTRY(ia32pv_cstar_target)
1435         RING0_INT_FRAME
1436         movl $__USER_DS,16(%esp)
1437         movl %ebp,%ecx
1438         movl $__USER_CS,4(%esp)
1439         movl 12(%esp),%ebp
1440         pushl %eax                      # save orig_eax
1441         CFI_ADJUST_CFA_OFFSET 4
1442 /*
1443  * Load the potential sixth argument from user stack.
1444  * Careful about security.
1445  */
1446         cmpl $__PAGE_OFFSET-4,%ebp
1447         CFI_REMEMBER_STATE
1448         ja cstar_fault
1449 1:      movl (%ebp),%ebp
1450 .section __ex_table,"a"
1451         .align 4
1452         .long 1b,cstar_fault
1453 .previous
1454         SAVE_ALL
1455         GET_THREAD_INFO(%ebp)
1456         testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
1457         jnz cstar_trace_entry
1458         cmpl $nr_syscalls,%eax
1459         jae cstar_badsys
1460 .Lcstar_call:
1461         btl %eax,cstar_special
1462         jc .Lcstar_special
1463         call *cstar_call_table(,%eax,4)
1464         movl %eax,PT_EAX(%esp)          # store the return value
1465 .Lcstar_exit:
1466         movl PT_ECX(%esp),%ecx
1467         movl %ecx,PT_EBP(%esp)          # put user EBP back in place
1468         jmp syscall_exit
1469 .Lcstar_special:
1470         movl PT_ECX(%esp),%ecx
1471         movl %ecx,PT_EBP(%esp)          # put user EBP back in place
1472         jmp syscall_call
1473 cstar_set_tif:
1474         movl $cstar_clear_tif,(%esp)    # replace return address
1475         LOCK_PREFIX
1476         orl $_TIF_CSTAR,TI_flags(%ebp)
1477         jmp *sys_call_table(,%eax,4)
1478 cstar_clear_tif:
1479         movl %eax,PT_EAX(%esp)          # store the return value
1480         LOCK_PREFIX
1481         andl $~_TIF_CSTAR,TI_flags(%ebp)
1482         jmp .Lcstar_exit
1483 cstar_trace_entry:
1484         movl $-ENOSYS,PT_EAX(%esp)
1485         cmpl $nr_syscalls,%eax
1486         jae 1f
1487         btl %eax,cstar_special
1488         jc .Lcstar_trace_special
1489 1:      movl %esp,%eax
1490         LOCK_PREFIX
1491         orl $_TIF_CSTAR,TI_flags(%ebp)
1492         call syscall_trace_enter
1493         LOCK_PREFIX
1494         andl $~_TIF_CSTAR,TI_flags(%ebp)
1495         /* What it returned is what we'll actually use.  */
1496         cmpl $nr_syscalls,%eax
1497         jb .Lcstar_call
1498         jmp .Lcstar_exit
1499 .Lcstar_trace_special:
1500         movl PT_ECX(%esp),%ecx
1501         movl %esp,%eax
1502         movl %ecx,PT_EBP(%esp)          # put user EBP back in place
1503         call syscall_trace_enter
1504         /* What it returned is what we'll actually use.  */
1505         cmpl $nr_syscalls,%eax
1506         jb syscall_call
1507         jmp syscall_exit
1508 cstar_badsys:
1509         movl $-ENOSYS,PT_EAX(%esp)
1510 .Lcstar_resume:
1511         movl PT_ECX(%esp),%ecx
1512         movl %ecx,PT_EBP(%esp)          # put user EBP back in place
1513         jmp resume_userspace
1514         CFI_RESTORE_STATE
1515 cstar_fault:
1516         movl $-EFAULT,%eax
1517         SAVE_ALL
1518         GET_THREAD_INFO(%ebp)
1519         jmp .Lcstar_resume
1520         CFI_ENDPROC
1521 ENDPROC(ia32pv_cstar_target)
1522
1523 ENTRY(cstar_ret_from_fork)
1524         CFI_STARTPROC
1525         movl PT_ECX(%esp),%ecx
1526         GET_THREAD_INFO(%ebp)
1527         movl %ecx,PT_EBP(%esp)          # put user EBP back in place
1528         LOCK_PREFIX
1529         andl $~_TIF_CSTAR,TI_flags(%ebp)
1530         jmp ret_from_fork
1531         CFI_ENDPROC
1532 END(ret_from_fork)
1533
1534 .section .rodata,"a"
1535 #include "syscall_table_32.S"
1536
1537 syscall_table_size=(.-sys_call_table)
1538
1539 #include <asm/unistd.h>
1540 cstar_special:
1541 nr=0
1542 mask=0
1543 .rept nr_syscalls+31
1544  .irp n, __NR_sigreturn, __NR_rt_sigreturn
1545   .if nr == \n
1546    mask = mask | (1 << (\n & 31))
1547   .endif
1548  .endr
1549  nr = nr + 1
1550  .if (nr & 31) == 0
1551   .long mask
1552   mask = 0
1553  .endif
1554 .endr
1555 #define sys_call_table cstar_call_table
1556 #define sys_fork cstar_set_tif
1557 #define sys_clone cstar_set_tif
1558 #define sys_vfork cstar_set_tif
1559 #include "syscall_table_32.S"
1560 #undef  sys_call_table
1561 #undef  sys_fork
1562 #undef  sys_clone
1563 #undef  sys_vfork
1564
1565 /*
1566  * Some functions should be protected against kprobes
1567  */
1568         .pushsection .kprobes.text, "ax"
1569
1570 ENTRY(page_fault)
1571         RING0_EC_FRAME
1572         pushl $do_page_fault
1573         CFI_ADJUST_CFA_OFFSET 4
1574         ALIGN
1575 error_code:
1576         /* the function address is in %gs's slot on the stack */
1577         pushl %fs
1578         CFI_ADJUST_CFA_OFFSET 4
1579         /*CFI_REL_OFFSET fs, 0*/
1580         pushl %es
1581         CFI_ADJUST_CFA_OFFSET 4
1582         /*CFI_REL_OFFSET es, 0*/
1583         pushl %ds
1584         CFI_ADJUST_CFA_OFFSET 4
1585         /*CFI_REL_OFFSET ds, 0*/
1586         pushl %eax
1587         CFI_ADJUST_CFA_OFFSET 4
1588         CFI_REL_OFFSET eax, 0
1589         pushl %ebp
1590         CFI_ADJUST_CFA_OFFSET 4
1591         CFI_REL_OFFSET ebp, 0
1592         pushl %edi
1593         CFI_ADJUST_CFA_OFFSET 4
1594         CFI_REL_OFFSET edi, 0
1595         pushl %esi
1596         CFI_ADJUST_CFA_OFFSET 4
1597         CFI_REL_OFFSET esi, 0
1598         pushl %edx
1599         CFI_ADJUST_CFA_OFFSET 4
1600         CFI_REL_OFFSET edx, 0
1601         pushl %ecx
1602         CFI_ADJUST_CFA_OFFSET 4
1603         CFI_REL_OFFSET ecx, 0
1604         pushl %ebx
1605         CFI_ADJUST_CFA_OFFSET 4
1606         CFI_REL_OFFSET ebx, 0
1607         cld
1608         movl $(__KERNEL_PERCPU), %ecx
1609         movl %ecx, %fs
1610         UNWIND_ESPFIX_STACK
1611         GS_TO_REG %ecx
1612         movl PT_GS(%esp), %edi          # get the function address
1613         movl PT_ORIG_EAX(%esp), %edx    # get the error code
1614         movl $-1, PT_ORIG_EAX(%esp)     # no syscall to restart
1615         REG_TO_PTGS %ecx
1616         SET_KERNEL_GS %ecx
1617         movl $(__USER_DS), %ecx
1618         movl %ecx, %ds
1619         movl %ecx, %es
1620         TRACE_IRQS_OFF
1621         movl %esp,%eax                  # pt_regs pointer
1622         call *%edi
1623         jmp ret_from_exception
1624         CFI_ENDPROC
1625 END(page_fault)
1626
1627 #ifndef CONFIG_XEN
1628 /*
1629  * Debug traps and NMI can happen at the one SYSENTER instruction
1630  * that sets up the real kernel stack. Check here, since we can't
1631  * allow the wrong stack to be used.
1632  *
1633  * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
1634  * already pushed 3 words if it hits on the sysenter instruction:
1635  * eflags, cs and eip.
1636  *
1637  * We just load the right stack, and push the three (known) values
1638  * by hand onto the new stack - while updating the return eip past
1639  * the instruction that would have done it for sysenter.
1640  */
1641 .macro FIX_STACK offset ok label
1642         cmpw $__KERNEL_CS, 4(%esp)
1643         jne \ok
1644 \label:
1645         movl TSS_sysenter_sp0 + \offset(%esp), %esp
1646         CFI_DEF_CFA esp, 0
1647         CFI_UNDEFINED eip
1648         pushfl
1649         CFI_ADJUST_CFA_OFFSET 4
1650         pushl $__KERNEL_CS
1651         CFI_ADJUST_CFA_OFFSET 4
1652         pushl $sysenter_past_esp
1653         CFI_ADJUST_CFA_OFFSET 4
1654         CFI_REL_OFFSET eip, 0
1655 .endm
1656 #endif /* CONFIG_XEN */
1657
1658 ENTRY(debug)
1659         RING0_INT_FRAME
1660 #ifndef CONFIG_XEN
1661         cmpl $ia32_sysenter_target,(%esp)
1662         jne debug_stack_correct
1663         FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
1664 debug_stack_correct:
1665 #endif /* !CONFIG_XEN */
1666         pushl $-1                       # mark this as an int
1667         CFI_ADJUST_CFA_OFFSET 4
1668         SAVE_ALL
1669         TRACE_IRQS_OFF
1670         xorl %edx,%edx                  # error code 0
1671         movl %esp,%eax                  # pt_regs pointer
1672         call do_debug
1673         jmp ret_from_exception
1674         CFI_ENDPROC
1675 END(debug)
1676
1677 /*
1678  * NMI is doubly nasty. It can happen _while_ we're handling
1679  * a debug fault, and the debug fault hasn't yet been able to
1680  * clear up the stack. So we first check whether we got  an
1681  * NMI on the sysenter entry path, but after that we need to
1682  * check whether we got an NMI on the debug path where the debug
1683  * fault happened on the sysenter path.
1684  */
1685 ENTRY(nmi)
1686         RING0_INT_FRAME
1687         pushl %eax
1688         CFI_ADJUST_CFA_OFFSET 4
1689 #ifndef CONFIG_XEN
1690         movl %ss, %eax
1691         cmpw $__ESPFIX_SS, %ax
1692         popl %eax
1693         CFI_ADJUST_CFA_OFFSET -4
1694         je nmi_espfix_stack
1695         cmpl $ia32_sysenter_target,(%esp)
1696         je nmi_stack_fixup
1697         pushl %eax
1698         CFI_ADJUST_CFA_OFFSET 4
1699         movl %esp,%eax
1700         /* Do not access memory above the end of our stack page,
1701          * it might not exist.
1702          */
1703         andl $(THREAD_SIZE-1),%eax
1704         cmpl $(THREAD_SIZE-20),%eax
1705         popl %eax
1706         CFI_ADJUST_CFA_OFFSET -4
1707         jae nmi_stack_correct
1708         cmpl $ia32_sysenter_target,12(%esp)
1709         je nmi_debug_stack_check
1710 nmi_stack_correct:
1711         /* We have a RING0_INT_FRAME here */
1712         pushl %eax
1713         CFI_ADJUST_CFA_OFFSET 4
1714         SAVE_ALL
1715         xorl %edx,%edx          # zero error code
1716         movl %esp,%eax          # pt_regs pointer
1717         call do_nmi
1718         jmp restore_all_notrace
1719         CFI_ENDPROC
1720
1721 nmi_stack_fixup:
1722         RING0_INT_FRAME
1723         FIX_STACK 12, nmi_stack_correct, 1
1724         jmp nmi_stack_correct
1725
1726 nmi_debug_stack_check:
1727         /* We have a RING0_INT_FRAME here */
1728         cmpw $__KERNEL_CS,16(%esp)
1729         jne nmi_stack_correct
1730         cmpl $debug,(%esp)
1731         jb nmi_stack_correct
1732         cmpl $debug_esp_fix_insn,(%esp)
1733         ja nmi_stack_correct
1734         FIX_STACK 24, nmi_stack_correct, 1
1735         jmp nmi_stack_correct
1736
1737 nmi_espfix_stack:
1738         /* We have a RING0_INT_FRAME here.
1739          *
1740          * create the pointer to lss back
1741          */
1742         pushl %ss
1743         CFI_ADJUST_CFA_OFFSET 4
1744         pushl %esp
1745         CFI_ADJUST_CFA_OFFSET 4
1746         addl $4, (%esp)
1747         /* copy the iret frame of 12 bytes */
1748         .rept 3
1749         pushl 16(%esp)
1750         CFI_ADJUST_CFA_OFFSET 4
1751         .endr
1752         pushl %eax
1753         CFI_ADJUST_CFA_OFFSET 4
1754         SAVE_ALL
1755         FIXUP_ESPFIX_STACK              # %eax == %esp
1756         xorl %edx,%edx                  # zero error code
1757         call do_nmi
1758         RESTORE_REGS
1759         lss 12+4(%esp), %esp            # back to espfix stack
1760         CFI_ADJUST_CFA_OFFSET -24
1761         jmp irq_return
1762 #else
1763         SAVE_ALL
1764         xorl %edx,%edx          # zero error code
1765         movl %esp,%eax          # pt_regs pointer
1766         call do_nmi
1767         orl  $NMI_MASK, PT_EFLAGS(%esp)
1768         jmp restore_all
1769 #endif
1770         CFI_ENDPROC
1771 END(nmi)
1772
1773 ENTRY(int3)
1774         RING0_INT_FRAME
1775         pushl $-1                       # mark this as an int
1776         CFI_ADJUST_CFA_OFFSET 4
1777         SAVE_ALL
1778         TRACE_IRQS_OFF
1779         xorl %edx,%edx          # zero error code
1780         movl %esp,%eax          # pt_regs pointer
1781         call do_int3
1782         jmp ret_from_exception
1783         CFI_ENDPROC
1784 END(int3)
1785
1786 ENTRY(general_protection)
1787         RING0_EC_FRAME
1788         pushl $do_general_protection
1789         CFI_ADJUST_CFA_OFFSET 4
1790         jmp error_code
1791         CFI_ENDPROC
1792 END(general_protection)
1793
1794 /*
1795  * End of kprobes section
1796  */
1797         .popsection