2 * linux/arch/x86_64/entry.S
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
6 * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
10 * entry.S contains the system-call and fault low-level handling routines.
12 * NOTE: This code handles signal-recognition, which happens every time
13 * after an interrupt and after each system call.
15 * Normal syscalls and interrupts don't save a full stack frame, this is
16 * only done for syscall tracing, signals or fork/exec et.al.
18 * A note on terminology:
19 * - top of stack: Architecture defined interrupt frame from SS to RIP
20 * at the top of the kernel process stack.
21 * - partial stack frame: partially saved registers upto R11.
22 * - full stack frame: Like partial stack frame, but all register saved.
25 * - CFI macros are used to generate dwarf2 unwind information for better
26 * backtraces. They don't change any code.
27 * - SAVE_ALL/RESTORE_ALL - Save/restore all registers
28 * - SAVE_ARGS/RESTORE_ARGS - Save/restore registers that C functions modify.
29 * There are unfortunately lots of special cases where some registers
30 * not touched. The macro is a big mess that should be cleaned up.
31 * - SAVE_REST/RESTORE_REST - Handle the registers not saved by SAVE_ARGS.
32 * Gives a full stack frame.
33 * - ENTRY/END Define functions in the symbol table.
34 * - FIXUP_TOP_OF_STACK/RESTORE_TOP_OF_STACK - Fix up the hardware stack
35 * frame that is otherwise undefined after a SYSCALL
36 * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
37 * - errorentry/paranoidentry/zeroentry - Define exception entry points.
40 #include <linux/linkage.h>
41 #include <asm/segment.h>
42 #include <asm/cache.h>
43 #include <asm/errno.h>
44 #include <asm/dwarf2.h>
45 #include <asm/calling.h>
46 #include <asm/asm-offsets.h>
48 #include <asm/unistd.h>
49 #include <asm/thread_info.h>
50 #include <asm/hw_irq.h>
52 #include <asm/irqflags.h>
53 #include <asm/paravirt.h>
54 #include <asm/ftrace.h>
56 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
57 #include <linux/elf-em.h>
58 #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
59 #define __AUDIT_ARCH_64BIT 0x80000000
60 #define __AUDIT_ARCH_LE 0x40000000
63 #ifdef CONFIG_FUNCTION_TRACER
64 #ifdef CONFIG_DYNAMIC_FTRACE
71 /* taken from glibc */
83 subq $MCOUNT_INSN_SIZE, %rdi
103 #else /* ! CONFIG_DYNAMIC_FTRACE */
105 cmpq $ftrace_stub, ftrace_trace_function
112 /* taken from glibc */
122 movq 0x38(%rsp), %rdi
124 subq $MCOUNT_INSN_SIZE, %rdi
126 call *ftrace_trace_function
139 #endif /* CONFIG_DYNAMIC_FTRACE */
140 #endif /* CONFIG_FUNCTION_TRACER */
142 #ifndef CONFIG_PREEMPT
143 #define retint_kernel retint_restore_args
146 #ifdef CONFIG_PARAVIRT
147 ENTRY(native_usergs_sysret64)
150 #endif /* CONFIG_PARAVIRT */
153 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
154 #ifdef CONFIG_TRACE_IRQFLAGS
155 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
163 * C code is not supposed to know about undefined top of stack. Every time
164 * a C function with an pt_regs argument is called from the SYSCALL based
165 * fast path FIXUP_TOP_OF_STACK is needed.
166 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
170 /* %rsp:at FRAMEEND */
171 .macro FIXUP_TOP_OF_STACK tmp offset=0
172 movq %gs:pda_oldrsp,\tmp
173 movq \tmp,RSP+\offset(%rsp)
174 movq $__USER_DS,SS+\offset(%rsp)
175 movq $__USER_CS,CS+\offset(%rsp)
176 movq $-1,RCX+\offset(%rsp)
177 movq R11+\offset(%rsp),\tmp /* get eflags */
178 movq \tmp,EFLAGS+\offset(%rsp)
181 .macro RESTORE_TOP_OF_STACK tmp offset=0
182 movq RSP+\offset(%rsp),\tmp
183 movq \tmp,%gs:pda_oldrsp
184 movq EFLAGS+\offset(%rsp),\tmp
185 movq \tmp,R11+\offset(%rsp)
188 .macro FAKE_STACK_FRAME child_rip
189 /* push in order ss, rsp, eflags, cs, rip */
191 pushq $__KERNEL_DS /* ss */
192 CFI_ADJUST_CFA_OFFSET 8
193 /*CFI_REL_OFFSET ss,0*/
195 CFI_ADJUST_CFA_OFFSET 8
197 pushq $X86_EFLAGS_IF /* eflags - interrupts on */
198 CFI_ADJUST_CFA_OFFSET 8
199 /*CFI_REL_OFFSET rflags,0*/
200 pushq $__KERNEL_CS /* cs */
201 CFI_ADJUST_CFA_OFFSET 8
202 /*CFI_REL_OFFSET cs,0*/
203 pushq \child_rip /* rip */
204 CFI_ADJUST_CFA_OFFSET 8
206 pushq %rax /* orig rax */
207 CFI_ADJUST_CFA_OFFSET 8
210 .macro UNFAKE_STACK_FRAME
212 CFI_ADJUST_CFA_OFFSET -(6*8)
216 * initial frame state for interrupts (and exceptions without error code)
218 .macro EMPTY_FRAME start=1 offset=0
222 CFI_DEF_CFA rsp,8+\offset
224 CFI_DEF_CFA_OFFSET 8+\offset
229 * initial frame state for interrupts (and exceptions without error code)
231 .macro INTR_FRAME start=1 offset=0
232 EMPTY_FRAME \start, SS+8+\offset-RIP
233 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
234 CFI_REL_OFFSET rsp, RSP+\offset-RIP
235 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
236 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
237 CFI_REL_OFFSET rip, RIP+\offset-RIP
241 * initial frame state for exceptions with error code (and interrupts
242 * with vector already pushed)
244 .macro XCPT_FRAME start=1 offset=0
245 INTR_FRAME \start, RIP+\offset-ORIG_RAX
246 /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
250 * frame that enables calling into C.
252 .macro PARTIAL_FRAME start=1 offset=0
253 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
254 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
255 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
256 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
257 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
258 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
259 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
260 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
261 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
262 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
266 * frame that enables passing a complete pt_regs to a C function.
268 .macro DEFAULT_FRAME start=1 offset=0
269 PARTIAL_FRAME \start, R11+\offset-R15
270 CFI_REL_OFFSET rbx, RBX+\offset
271 CFI_REL_OFFSET rbp, RBP+\offset
272 CFI_REL_OFFSET r12, R12+\offset
273 CFI_REL_OFFSET r13, R13+\offset
274 CFI_REL_OFFSET r14, R14+\offset
275 CFI_REL_OFFSET r15, R15+\offset
278 /* save partial stack frame */
282 movq_cfi rdi, RDI+16-ARGOFFSET
283 movq_cfi rsi, RSI+16-ARGOFFSET
284 movq_cfi rdx, RDX+16-ARGOFFSET
285 movq_cfi rcx, RCX+16-ARGOFFSET
286 movq_cfi rax, RAX+16-ARGOFFSET
287 movq_cfi r8, R8+16-ARGOFFSET
288 movq_cfi r9, R9+16-ARGOFFSET
289 movq_cfi r10, R10+16-ARGOFFSET
290 movq_cfi r11, R11+16-ARGOFFSET
292 leaq -ARGOFFSET+16(%rsp),%rdi /* arg1 for handler */
293 movq_cfi rbp, 8 /* push %rbp */
294 leaq 8(%rsp), %rbp /* mov %rsp, %ebp */
299 * irqcount is used to check if a CPU is already on an interrupt stack
300 * or not. While this is essentially redundant with preempt_count it is
301 * a little cheaper to use a separate counter in the PDA (short of
302 * moving irq_enter into assembly, which would be too much work)
304 1: incl %gs:pda_irqcount
306 popq_cfi %rax /* move return address... */
307 mov %gs:pda_irqstackptr,%rsp
309 pushq_cfi %rax /* ... to the new stack */
311 * We entered an interrupt context - irqs are off:
319 PARTIAL_FRAME 1 REST_SKIP+8
320 movq 5*8+16(%rsp), %r11 /* save return address */
327 movq %r11, 8(%rsp) /* return address */
328 FIXUP_TOP_OF_STACK %r11, 16
333 /* save complete stack frame */
353 movl $MSR_GS_BASE,%ecx
356 js 1f /* negative -> in kernel */
364 * A newly forked process directly context switches into this address.
366 * rdi: prev task we switched from
371 push kernel_eflags(%rip)
372 CFI_ADJUST_CFA_OFFSET 8
373 popf # reset kernel eflags
374 CFI_ADJUST_CFA_OFFSET -8
376 call schedule_tail # rdi: 'prev' task parameter
378 GET_THREAD_INFO(%rcx)
383 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
384 je int_ret_from_sys_call
386 testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
387 jnz int_ret_from_sys_call
389 RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
390 jmp ret_from_sys_call # go to the SYSRET fastpath
397 * System call entry. Upto 6 arguments in registers are supported.
399 * SYSCALL does not save anything on the stack and does not change the
405 * rax system call number
407 * rcx return address for syscall/sysret, C arg3
410 * r10 arg3 (--> moved to rcx for C)
413 * r11 eflags for syscall/sysret, temporary for C
414 * r12-r15,rbp,rbx saved by C code, not touched.
416 * Interrupts are off on entry.
417 * Only called from user space.
419 * XXX if we had a free scratch register we could save the RSP into the stack frame
420 * and report it properly in ps. Unfortunately we haven't.
422 * When user can change the frames always force IRET. That is because
423 * it deals with uncanonical addresses better. SYSRET has trouble
424 * with them due to bugs in both AMD and Intel CPUs.
430 CFI_DEF_CFA rsp,PDA_STACKOFFSET
432 /*CFI_REGISTER rflags,r11*/
435 * A hypervisor implementation might want to use a label
436 * after the swapgs, so that it can do the swapgs
437 * for the guest and jump here on syscall.
439 ENTRY(system_call_after_swapgs)
441 movq %rsp,%gs:pda_oldrsp
442 movq %gs:pda_kernelstack,%rsp
444 * No need to follow this irqs off/on section - it's straight
447 ENABLE_INTERRUPTS(CLBR_NONE)
449 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
450 movq %rcx,RIP-ARGOFFSET(%rsp)
451 CFI_REL_OFFSET rip,RIP-ARGOFFSET
452 GET_THREAD_INFO(%rcx)
453 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
455 system_call_fastpath:
456 cmpq $__NR_syscall_max,%rax
459 call *sys_call_table(,%rax,8) # XXX: rip relative
460 movq %rax,RAX-ARGOFFSET(%rsp)
462 * Syscall return path ending with SYSRET (fast path)
463 * Has incomplete stack frame and undefined top of stack.
466 movl $_TIF_ALLWORK_MASK,%edi
470 GET_THREAD_INFO(%rcx)
471 DISABLE_INTERRUPTS(CLBR_NONE)
473 movl TI_flags(%rcx),%edx
478 * sysretq will re-enable interrupts:
481 movq RIP-ARGOFFSET(%rsp),%rcx
483 RESTORE_ARGS 0,-ARG_SKIP,1
484 /*CFI_REGISTER rflags,r11*/
485 movq %gs:pda_oldrsp, %rsp
489 /* Handle reschedules */
490 /* edx: work, edi: workmask */
492 bt $TIF_NEED_RESCHED,%edx
495 ENABLE_INTERRUPTS(CLBR_NONE)
497 CFI_ADJUST_CFA_OFFSET 8
500 CFI_ADJUST_CFA_OFFSET -8
503 /* Handle a signal */
506 ENABLE_INTERRUPTS(CLBR_NONE)
507 #ifdef CONFIG_AUDITSYSCALL
508 bt $TIF_SYSCALL_AUDIT,%edx
511 /* edx: work flags (arg3) */
512 leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1
513 xorl %esi,%esi # oldset -> arg2
515 FIXUP_TOP_OF_STACK %r11
516 call do_notify_resume
517 RESTORE_TOP_OF_STACK %r11
519 movl $_TIF_WORK_MASK,%edi
520 /* Use IRET because user could have changed frame. This
521 works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
522 DISABLE_INTERRUPTS(CLBR_NONE)
527 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
528 jmp ret_from_sys_call
530 #ifdef CONFIG_AUDITSYSCALL
532 * Fast path for syscall audit without full syscall trace.
533 * We just call audit_syscall_entry() directly, and then
534 * jump back to the normal fast path.
537 movq %r10,%r9 /* 6th arg: 4th syscall arg */
538 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
539 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
540 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
541 movq %rax,%rsi /* 2nd arg: syscall number */
542 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
543 call audit_syscall_entry
544 LOAD_ARGS 0 /* reload call-clobbered registers */
545 jmp system_call_fastpath
548 * Return fast path for syscall audit. Call audit_syscall_exit()
549 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
553 movq %rax,%rsi /* second arg, syscall return value */
554 cmpq $0,%rax /* is it < 0? */
555 setl %al /* 1 if so, 0 if not */
556 movzbl %al,%edi /* zero-extend that into %edi */
557 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
558 call audit_syscall_exit
559 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
561 #endif /* CONFIG_AUDITSYSCALL */
563 /* Do syscall tracing */
565 #ifdef CONFIG_AUDITSYSCALL
566 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
570 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
571 FIXUP_TOP_OF_STACK %rdi
573 call syscall_trace_enter
575 * Reload arg registers from stack in case ptrace changed them.
576 * We don't reload %rax because syscall_trace_enter() returned
577 * the value it wants us to use in the table lookup.
579 LOAD_ARGS ARGOFFSET, 1
581 cmpq $__NR_syscall_max,%rax
582 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
583 movq %r10,%rcx /* fixup for C */
584 call *sys_call_table(,%rax,8)
585 movq %rax,RAX-ARGOFFSET(%rsp)
586 /* Use IRET because user could have changed frame */
589 * Syscall return path ending with IRET.
590 * Has correct top of stack, but partial stack frame.
592 .globl int_ret_from_sys_call
593 .globl int_with_check
594 int_ret_from_sys_call:
595 DISABLE_INTERRUPTS(CLBR_NONE)
597 testl $3,CS-ARGOFFSET(%rsp)
598 je retint_restore_args
599 movl $_TIF_ALLWORK_MASK,%edi
600 /* edi: mask to check */
603 GET_THREAD_INFO(%rcx)
604 movl TI_flags(%rcx),%edx
607 andl $~TS_COMPAT,TI_status(%rcx)
610 /* Either reschedule or signal or syscall exit tracking needed. */
611 /* First do a reschedule test. */
612 /* edx: work, edi: workmask */
614 bt $TIF_NEED_RESCHED,%edx
617 ENABLE_INTERRUPTS(CLBR_NONE)
619 CFI_ADJUST_CFA_OFFSET 8
622 CFI_ADJUST_CFA_OFFSET -8
623 DISABLE_INTERRUPTS(CLBR_NONE)
627 /* handle signals and tracing -- both require a full stack frame */
630 ENABLE_INTERRUPTS(CLBR_NONE)
632 /* Check for syscall exit trace */
633 testl $_TIF_WORK_SYSCALL_EXIT,%edx
636 CFI_ADJUST_CFA_OFFSET 8
637 leaq 8(%rsp),%rdi # &ptregs -> arg1
638 call syscall_trace_leave
640 CFI_ADJUST_CFA_OFFSET -8
641 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
645 testl $_TIF_DO_NOTIFY_MASK,%edx
647 movq %rsp,%rdi # &ptregs -> arg1
648 xorl %esi,%esi # oldset -> arg2
649 call do_notify_resume
650 1: movl $_TIF_WORK_MASK,%edi
653 DISABLE_INTERRUPTS(CLBR_NONE)
660 * Certain special system calls that need to save a complete full stack frame.
662 .macro PTREGSCALL label,func,arg
664 PARTIAL_FRAME 1 8 /* offset 8: return address */
665 subq $REST_SKIP, %rsp
666 CFI_ADJUST_CFA_OFFSET REST_SKIP
668 DEFAULT_FRAME 0 8 /* offset 8: return address */
669 leaq 8(%rsp), \arg /* pt_regs pointer */
671 jmp ptregscall_common
676 PTREGSCALL stub_clone, sys_clone, %r8
677 PTREGSCALL stub_fork, sys_fork, %rdi
678 PTREGSCALL stub_vfork, sys_vfork, %rdi
679 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
680 PTREGSCALL stub_iopl, sys_iopl, %rsi
682 ENTRY(ptregscall_common)
683 DEFAULT_FRAME 1 8 /* offset 8: return address */
684 RESTORE_TOP_OF_STACK %r11, 8
685 movq_cfi_restore R15+8, r15
686 movq_cfi_restore R14+8, r14
687 movq_cfi_restore R13+8, r13
688 movq_cfi_restore R12+8, r12
689 movq_cfi_restore RBP+8, rbp
690 movq_cfi_restore RBX+8, rbx
691 ret $REST_SKIP /* pop extended registers */
693 END(ptregscall_common)
698 CFI_ADJUST_CFA_OFFSET -8
699 CFI_REGISTER rip, r11
701 FIXUP_TOP_OF_STACK %r11
704 RESTORE_TOP_OF_STACK %r11
707 jmp int_ret_from_sys_call
712 * sigreturn is special because it needs to restore all registers on return.
713 * This cannot be done with SYSRET, so use the IRET return path instead.
715 ENTRY(stub_rt_sigreturn)
718 CFI_ADJUST_CFA_OFFSET -8
721 FIXUP_TOP_OF_STACK %r11
722 call sys_rt_sigreturn
723 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
725 jmp int_ret_from_sys_call
727 END(stub_rt_sigreturn)
730 * Build the entry stubs and pointer table with some assembler magic.
731 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
732 * single cache line on all modern x86 implementations.
734 .section .init.rodata,"a"
738 .p2align CONFIG_X86_L1_CACHE_SHIFT
739 ENTRY(irq_entries_start)
741 vector=FIRST_EXTERNAL_VECTOR
742 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
745 .if vector < NR_VECTORS
746 .if vector <> FIRST_EXTERNAL_VECTOR
747 CFI_ADJUST_CFA_OFFSET -8
749 1: pushq $(~vector+0x80) /* Note: always in signed byte range */
750 CFI_ADJUST_CFA_OFFSET 8
751 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
760 2: jmp common_interrupt
763 END(irq_entries_start)
770 * Interrupt entry/exit.
772 * Interrupt entry points save only callee clobbered registers in fast path.
774 * Entry runs with interrupts off.
777 /* 0(%rsp): ~(interrupt number) */
778 .macro interrupt func
780 CFI_ADJUST_CFA_OFFSET 10*8
787 * The interrupt stubs push (~vector+0x80) onto the stack and
788 * then jump to common_interrupt.
790 .p2align CONFIG_X86_L1_CACHE_SHIFT
793 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
795 /* 0(%rsp): oldrsp-ARGOFFSET */
797 DISABLE_INTERRUPTS(CLBR_NONE)
799 decl %gs:pda_irqcount
801 CFI_DEF_CFA_REGISTER rsp
802 CFI_ADJUST_CFA_OFFSET -8
804 GET_THREAD_INFO(%rcx)
805 testl $3,CS-ARGOFFSET(%rsp)
808 /* Interrupt came from user space */
810 * Has a correct top of stack, but a partial stack frame
811 * %rcx: thread info. Interrupts off.
813 retint_with_reschedule:
814 movl $_TIF_WORK_MASK,%edi
817 movl TI_flags(%rcx),%edx
822 retint_swapgs: /* return to user-space */
824 * The iretq could re-enable interrupts:
826 DISABLE_INTERRUPTS(CLBR_ANY)
831 retint_restore_args: /* return to kernel space */
832 DISABLE_INTERRUPTS(CLBR_ANY)
834 * The iretq could re-enable interrupts:
843 .section __ex_table, "a"
844 .quad irq_return, bad_iret
847 #ifdef CONFIG_PARAVIRT
851 .section __ex_table,"a"
852 .quad native_iret, bad_iret
859 * The iret traps when the %cs or %ss being restored is bogus.
860 * We've lost the original trap vector and error code.
861 * #GPF is the most likely one to get for an invalid selector.
862 * So pretend we completed the iret and took the #GPF in user mode.
864 * We are now running with the kernel GS after exception recovery.
865 * But error_entry expects us to have user GS to match the user %cs,
871 jmp general_protection
875 /* edi: workmask, edx: work */
878 bt $TIF_NEED_RESCHED,%edx
881 ENABLE_INTERRUPTS(CLBR_NONE)
883 CFI_ADJUST_CFA_OFFSET 8
886 CFI_ADJUST_CFA_OFFSET -8
887 GET_THREAD_INFO(%rcx)
888 DISABLE_INTERRUPTS(CLBR_NONE)
893 testl $_TIF_DO_NOTIFY_MASK,%edx
896 ENABLE_INTERRUPTS(CLBR_NONE)
898 movq $-1,ORIG_RAX(%rsp)
899 xorl %esi,%esi # oldset
900 movq %rsp,%rdi # &pt_regs
901 call do_notify_resume
903 DISABLE_INTERRUPTS(CLBR_NONE)
905 GET_THREAD_INFO(%rcx)
906 jmp retint_with_reschedule
908 #ifdef CONFIG_PREEMPT
909 /* Returning to kernel space. Check if we need preemption */
910 /* rcx: threadinfo. interrupts off. */
912 cmpl $0,TI_preempt_count(%rcx)
913 jnz retint_restore_args
914 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
915 jnc retint_restore_args
916 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
917 jnc retint_restore_args
918 call preempt_schedule_irq
923 END(common_interrupt)
928 .macro apicinterrupt num sym do_sym
932 CFI_ADJUST_CFA_OFFSET 8
940 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
941 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
944 apicinterrupt UV_BAU_MESSAGE \
945 uv_bau_message_intr1 uv_bau_message_interrupt
946 apicinterrupt LOCAL_TIMER_VECTOR \
947 apic_timer_interrupt smp_apic_timer_interrupt
950 apicinterrupt INVALIDATE_TLB_VECTOR_START+0 \
951 invalidate_interrupt0 smp_invalidate_interrupt
952 apicinterrupt INVALIDATE_TLB_VECTOR_START+1 \
953 invalidate_interrupt1 smp_invalidate_interrupt
954 apicinterrupt INVALIDATE_TLB_VECTOR_START+2 \
955 invalidate_interrupt2 smp_invalidate_interrupt
956 apicinterrupt INVALIDATE_TLB_VECTOR_START+3 \
957 invalidate_interrupt3 smp_invalidate_interrupt
958 apicinterrupt INVALIDATE_TLB_VECTOR_START+4 \
959 invalidate_interrupt4 smp_invalidate_interrupt
960 apicinterrupt INVALIDATE_TLB_VECTOR_START+5 \
961 invalidate_interrupt5 smp_invalidate_interrupt
962 apicinterrupt INVALIDATE_TLB_VECTOR_START+6 \
963 invalidate_interrupt6 smp_invalidate_interrupt
964 apicinterrupt INVALIDATE_TLB_VECTOR_START+7 \
965 invalidate_interrupt7 smp_invalidate_interrupt
968 apicinterrupt THRESHOLD_APIC_VECTOR \
969 threshold_interrupt mce_threshold_interrupt
970 apicinterrupt THERMAL_APIC_VECTOR \
971 thermal_interrupt smp_thermal_interrupt
974 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
975 call_function_single_interrupt smp_call_function_single_interrupt
976 apicinterrupt CALL_FUNCTION_VECTOR \
977 call_function_interrupt smp_call_function_interrupt
978 apicinterrupt RESCHEDULE_VECTOR \
979 reschedule_interrupt smp_reschedule_interrupt
982 apicinterrupt ERROR_APIC_VECTOR \
983 error_interrupt smp_error_interrupt
984 apicinterrupt SPURIOUS_APIC_VECTOR \
985 spurious_interrupt smp_spurious_interrupt
988 * Exception entry points.
990 .macro zeroentry sym do_sym
993 PARAVIRT_ADJUST_EXCEPTION_FRAME
994 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
996 CFI_ADJUST_CFA_OFFSET 15*8
999 movq %rsp,%rdi /* pt_regs pointer */
1000 xorl %esi,%esi /* no error code */
1002 jmp error_exit /* %ebx: no swapgs flag */
1007 .macro paranoidzeroentry sym do_sym
1010 PARAVIRT_ADJUST_EXCEPTION_FRAME
1011 pushq $-1 /* ORIG_RAX: no syscall to restart */
1012 CFI_ADJUST_CFA_OFFSET 8
1016 movq %rsp,%rdi /* pt_regs pointer */
1017 xorl %esi,%esi /* no error code */
1019 jmp paranoid_exit /* %ebx: no swapgs flag */
1024 .macro paranoidzeroentry_ist sym do_sym ist
1027 PARAVIRT_ADJUST_EXCEPTION_FRAME
1028 pushq $-1 /* ORIG_RAX: no syscall to restart */
1029 CFI_ADJUST_CFA_OFFSET 8
1033 movq %rsp,%rdi /* pt_regs pointer */
1034 xorl %esi,%esi /* no error code */
1035 movq %gs:pda_data_offset, %rbp
1036 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
1038 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
1039 jmp paranoid_exit /* %ebx: no swapgs flag */
1044 .macro errorentry sym do_sym
1047 PARAVIRT_ADJUST_EXCEPTION_FRAME
1049 CFI_ADJUST_CFA_OFFSET 15*8
1052 movq %rsp,%rdi /* pt_regs pointer */
1053 movq ORIG_RAX(%rsp),%rsi /* get error code */
1054 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1056 jmp error_exit /* %ebx: no swapgs flag */
1061 /* error code is on the stack already */
1062 .macro paranoiderrorentry sym do_sym
1065 PARAVIRT_ADJUST_EXCEPTION_FRAME
1067 CFI_ADJUST_CFA_OFFSET 15*8
1071 movq %rsp,%rdi /* pt_regs pointer */
1072 movq ORIG_RAX(%rsp),%rsi /* get error code */
1073 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1075 jmp paranoid_exit /* %ebx: no swapgs flag */
1080 zeroentry divide_error do_divide_error
1081 zeroentry overflow do_overflow
1082 zeroentry bounds do_bounds
1083 zeroentry invalid_op do_invalid_op
1084 zeroentry device_not_available do_device_not_available
1085 paranoiderrorentry double_fault do_double_fault
1086 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1087 errorentry invalid_TSS do_invalid_TSS
1088 errorentry segment_not_present do_segment_not_present
1089 zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1090 zeroentry coprocessor_error do_coprocessor_error
1091 errorentry alignment_check do_alignment_check
1092 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1094 /* Reload gs selector with exception handling */
1095 /* edi: new selector */
1096 ENTRY(native_load_gs_index)
1099 CFI_ADJUST_CFA_OFFSET 8
1100 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1104 2: mfence /* workaround */
1107 CFI_ADJUST_CFA_OFFSET -8
1110 END(native_load_gs_index)
1112 .section __ex_table,"a"
1114 .quad gs_change,bad_gs
1116 .section .fixup,"ax"
1117 /* running with kernelgs */
1119 SWAPGS /* switch back to user gs */
1126 * Create a kernel thread.
1128 * C extern interface:
1129 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1131 * asm input arguments:
1132 * rdi: fn, rsi: arg, rdx: flags
1134 ENTRY(kernel_thread)
1136 FAKE_STACK_FRAME $child_rip
1139 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1141 orq kernel_thread_flags(%rip),%rdi
1154 * It isn't worth to check for reschedule here,
1155 * so internally to the x86_64 port you can rely on kernel_thread()
1156 * not to reschedule the child before returning, this avoids the need
1157 * of hacks for example to fork off the per-CPU idle tasks.
1158 * [Hopefully no generic code relies on the reschedule -AK]
1167 pushq $0 # fake return address
1170 * Here we are in the child and the registers are set as they were
1171 * at kernel_thread() invocation in the parent.
1179 ud2 # padding for call trace
1184 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1186 * C extern interface:
1187 * extern long execve(char *name, char **argv, char **envp)
1189 * asm input arguments:
1190 * rdi: name, rsi: argv, rdx: envp
1192 * We want to fallback into:
1193 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
1195 * do_sys_execve asm fallback arguments:
1196 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1198 ENTRY(kernel_execve)
1204 movq %rax, RAX(%rsp)
1207 je int_ret_from_sys_call
1214 /* Call softirq on interrupt stack. Interrupts are off. */
1218 CFI_ADJUST_CFA_OFFSET 8
1219 CFI_REL_OFFSET rbp,0
1221 CFI_DEF_CFA_REGISTER rbp
1222 incl %gs:pda_irqcount
1223 cmove %gs:pda_irqstackptr,%rsp
1224 push %rbp # backlink for old unwinder
1227 CFI_DEF_CFA_REGISTER rsp
1228 CFI_ADJUST_CFA_OFFSET -8
1229 decl %gs:pda_irqcount
1235 zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
1238 * A note on the "critical region" in our callback handler.
1239 * We want to avoid stacking callback handlers due to events occurring
1240 * during handling of the last event. To do this, we keep events disabled
1241 * until we've done all processing. HOWEVER, we must enable events before
1242 * popping the stack frame (can't be done atomically) and so it would still
1243 * be possible to get enough handler activations to overflow the stack.
1244 * Although unlikely, bugs of that kind are hard to track down, so we'd
1245 * like to avoid the possibility.
1246 * So, on entry to the handler we detect whether we interrupted an
1247 * existing activation in its critical region -- if so, we pop the current
1248 * activation and restart the handler using the previous one.
1250 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1253 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1254 * see the correct pointer to the pt_regs
1256 movq %rdi, %rsp # we don't return, adjust the stack frame
1259 11: incl %gs:pda_irqcount
1261 CFI_DEF_CFA_REGISTER rbp
1262 cmovzq %gs:pda_irqstackptr,%rsp
1263 pushq %rbp # backlink for old unwinder
1264 call xen_evtchn_do_upcall
1266 CFI_DEF_CFA_REGISTER rsp
1267 decl %gs:pda_irqcount
1270 END(do_hypervisor_callback)
1273 * Hypervisor uses this for application faults while it executes.
1274 * We get here for two reasons:
1275 * 1. Fault while reloading DS, ES, FS or GS
1276 * 2. Fault while executing IRET
1277 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1278 * registers that could be reloaded and zeroed the others.
1279 * Category 2 we fix up by killing the current process. We cannot use the
1280 * normal Linux return path in this case because if we use the IRET hypercall
1281 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1282 * We distinguish between categories by comparing each saved segment register
1283 * with its current contents: any discrepancy means we in category 1.
1285 ENTRY(xen_failsafe_callback)
1287 /*CFI_REL_OFFSET gs,GS*/
1288 /*CFI_REL_OFFSET fs,FS*/
1289 /*CFI_REL_OFFSET es,ES*/
1290 /*CFI_REL_OFFSET ds,DS*/
1291 CFI_REL_OFFSET r11,8
1292 CFI_REL_OFFSET rcx,0
1306 /* All segments match their saved values => Category 2 (Bad IRET). */
1312 CFI_ADJUST_CFA_OFFSET -0x30
1313 pushq_cfi $0 /* RIP */
1316 jmp general_protection
1318 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1324 CFI_ADJUST_CFA_OFFSET -0x30
1329 END(xen_failsafe_callback)
1331 #endif /* CONFIG_XEN */
1334 * Some functions should be protected against kprobes
1336 .pushsection .kprobes.text, "ax"
1338 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1339 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1340 paranoiderrorentry stack_segment do_stack_segment
1341 errorentry general_protection do_general_protection
1342 errorentry page_fault do_page_fault
1343 #ifdef CONFIG_X86_MCE
1344 paranoidzeroentry machine_check do_machine_check
1348 * "Paranoid" exit path from exception stack.
1349 * Paranoid because this is used by NMIs and cannot take
1350 * any kernel state for granted.
1351 * We don't do kernel preemption checks here, because only
1352 * NMI should be common and it does not enable IRQs and
1353 * cannot get reschedule ticks.
1355 * "trace" is 0 for the NMI handler only, because irq-tracing
1356 * is fundamentally NMI-unsafe. (we cannot change the soft and
1357 * hard flags at once, atomically)
1360 /* ebx: no swapgs flag */
1361 ENTRY(paranoid_exit)
1363 DISABLE_INTERRUPTS(CLBR_NONE)
1365 testl %ebx,%ebx /* swapgs needed? */
1366 jnz paranoid_restore
1368 jnz paranoid_userspace
1376 GET_THREAD_INFO(%rcx)
1377 movl TI_flags(%rcx),%ebx
1378 andl $_TIF_WORK_MASK,%ebx
1380 movq %rsp,%rdi /* &pt_regs */
1382 movq %rax,%rsp /* switch stack for scheduling */
1383 testl $_TIF_NEED_RESCHED,%ebx
1384 jnz paranoid_schedule
1385 movl %ebx,%edx /* arg3: thread flags */
1387 ENABLE_INTERRUPTS(CLBR_NONE)
1388 xorl %esi,%esi /* arg2: oldset */
1389 movq %rsp,%rdi /* arg1: &pt_regs */
1390 call do_notify_resume
1391 DISABLE_INTERRUPTS(CLBR_NONE)
1393 jmp paranoid_userspace
1396 ENABLE_INTERRUPTS(CLBR_ANY)
1398 DISABLE_INTERRUPTS(CLBR_ANY)
1400 jmp paranoid_userspace
1405 * Exception entry point. This expects an error code/orig_rax on the stack.
1406 * returns in "no swapgs flag" in %ebx.
1410 CFI_ADJUST_CFA_OFFSET 15*8
1411 /* oldrax contains error code */
1430 je error_kernelspace
1439 * There are two places in the kernel that can potentially fault with
1440 * usergs. Handle them here. The exception handlers after iret run with
1441 * kernel gs again, so don't set the user space flag. B stepping K8s
1442 * sometimes report an truncated RIP for IRET exceptions returning to
1443 * compat mode. Check for these here too.
1447 leaq irq_return(%rip),%rcx
1448 cmpq %rcx,RIP+8(%rsp)
1450 movl %ecx,%ecx /* zero extend */
1451 cmpq %rcx,RIP+8(%rsp)
1453 cmpq $gs_change,RIP+8(%rsp)
1459 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1464 DISABLE_INTERRUPTS(CLBR_NONE)
1466 GET_THREAD_INFO(%rcx)
1469 LOCKDEP_SYS_EXIT_IRQ
1470 movl TI_flags(%rcx),%edx
1471 movl $_TIF_WORK_MASK,%edi
1479 /* runs on exception stack */
1482 PARAVIRT_ADJUST_EXCEPTION_FRAME
1485 CFI_ADJUST_CFA_OFFSET 15*8
1488 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1492 #ifdef CONFIG_TRACE_IRQFLAGS
1493 /* paranoidexit; without TRACE_IRQS_OFF */
1494 /* ebx: no swapgs flag */
1495 DISABLE_INTERRUPTS(CLBR_NONE)
1496 testl %ebx,%ebx /* swapgs needed? */
1506 GET_THREAD_INFO(%rcx)
1507 movl TI_flags(%rcx),%ebx
1508 andl $_TIF_WORK_MASK,%ebx
1510 movq %rsp,%rdi /* &pt_regs */
1512 movq %rax,%rsp /* switch stack for scheduling */
1513 testl $_TIF_NEED_RESCHED,%ebx
1515 movl %ebx,%edx /* arg3: thread flags */
1516 ENABLE_INTERRUPTS(CLBR_NONE)
1517 xorl %esi,%esi /* arg2: oldset */
1518 movq %rsp,%rdi /* arg1: &pt_regs */
1519 call do_notify_resume
1520 DISABLE_INTERRUPTS(CLBR_NONE)
1523 ENABLE_INTERRUPTS(CLBR_ANY)
1525 DISABLE_INTERRUPTS(CLBR_ANY)
1534 ENTRY(ignore_sysret)
1542 * End of kprobes section