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
64 * Some macro's to hide the most frequently occuring CFI annotations.
68 CFI_ADJUST_CFA_OFFSET 8
73 CFI_ADJUST_CFA_OFFSET -8
76 .macro movq_cfi reg offset=0
77 movq %\reg, \offset(%rsp)
78 CFI_REL_OFFSET \reg, \offset
81 .macro movq_cfi_restore offset reg
82 movq \offset(%rsp), %\reg
86 #ifdef CONFIG_FUNCTION_TRACER
87 #ifdef CONFIG_DYNAMIC_FTRACE
94 /* taken from glibc */
104 movq 0x38(%rsp), %rdi
106 subq $MCOUNT_INSN_SIZE, %rdi
126 #else /* ! CONFIG_DYNAMIC_FTRACE */
128 cmpq $ftrace_stub, ftrace_trace_function
135 /* taken from glibc */
145 movq 0x38(%rsp), %rdi
147 subq $MCOUNT_INSN_SIZE, %rdi
149 call *ftrace_trace_function
162 #endif /* CONFIG_DYNAMIC_FTRACE */
163 #endif /* CONFIG_FUNCTION_TRACER */
165 #ifndef CONFIG_PREEMPT
166 #define retint_kernel retint_restore_args
169 #ifdef CONFIG_PARAVIRT
170 ENTRY(native_usergs_sysret64)
173 #endif /* CONFIG_PARAVIRT */
176 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
177 #ifdef CONFIG_TRACE_IRQFLAGS
178 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
186 * C code is not supposed to know about undefined top of stack. Every time
187 * a C function with an pt_regs argument is called from the SYSCALL based
188 * fast path FIXUP_TOP_OF_STACK is needed.
189 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
193 /* %rsp:at FRAMEEND */
194 .macro FIXUP_TOP_OF_STACK tmp offset=0
195 movq %gs:pda_oldrsp,\tmp
196 movq \tmp,RSP+\offset(%rsp)
197 movq $__USER_DS,SS+\offset(%rsp)
198 movq $__USER_CS,CS+\offset(%rsp)
199 movq $-1,RCX+\offset(%rsp)
200 movq R11+\offset(%rsp),\tmp /* get eflags */
201 movq \tmp,EFLAGS+\offset(%rsp)
204 .macro RESTORE_TOP_OF_STACK tmp offset=0
205 movq RSP+\offset(%rsp),\tmp
206 movq \tmp,%gs:pda_oldrsp
207 movq EFLAGS+\offset(%rsp),\tmp
208 movq \tmp,R11+\offset(%rsp)
211 .macro FAKE_STACK_FRAME child_rip
212 /* push in order ss, rsp, eflags, cs, rip */
214 pushq $__KERNEL_DS /* ss */
215 CFI_ADJUST_CFA_OFFSET 8
216 /*CFI_REL_OFFSET ss,0*/
218 CFI_ADJUST_CFA_OFFSET 8
220 pushq $(1<<9) /* eflags - interrupts on */
221 CFI_ADJUST_CFA_OFFSET 8
222 /*CFI_REL_OFFSET rflags,0*/
223 pushq $__KERNEL_CS /* cs */
224 CFI_ADJUST_CFA_OFFSET 8
225 /*CFI_REL_OFFSET cs,0*/
226 pushq \child_rip /* rip */
227 CFI_ADJUST_CFA_OFFSET 8
229 pushq %rax /* orig rax */
230 CFI_ADJUST_CFA_OFFSET 8
233 .macro UNFAKE_STACK_FRAME
235 CFI_ADJUST_CFA_OFFSET -(6*8)
239 * initial frame state for interrupts (and exceptions without error code)
241 .macro EMPTY_FRAME start=1 offset=0
245 CFI_DEF_CFA rsp,8+\offset
247 CFI_DEF_CFA_OFFSET 8+\offset
252 * initial frame state for interrupts (and exceptions without error code)
254 .macro INTR_FRAME start=1 offset=0
255 EMPTY_FRAME \start, SS+8+\offset-RIP
256 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
257 CFI_REL_OFFSET rsp, RSP+\offset-RIP
258 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
259 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
260 CFI_REL_OFFSET rip, RIP+\offset-RIP
264 * initial frame state for exceptions with error code (and interrupts
265 * with vector already pushed)
267 .macro XCPT_FRAME start=1 offset=0
268 INTR_FRAME \start, RIP+\offset-ORIG_RAX
269 /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
273 * frame that enables calling into C.
275 .macro PARTIAL_FRAME start=1 offset=0
276 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
277 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
278 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
279 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
280 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
281 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
282 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
283 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
284 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
285 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
289 * frame that enables passing a complete pt_regs to a C function.
291 .macro DEFAULT_FRAME start=1 offset=0
292 PARTIAL_FRAME \start, R11+\offset-R15
293 CFI_REL_OFFSET rbx, RBX+\offset
294 CFI_REL_OFFSET rbp, RBP+\offset
295 CFI_REL_OFFSET r12, R12+\offset
296 CFI_REL_OFFSET r13, R13+\offset
297 CFI_REL_OFFSET r14, R14+\offset
298 CFI_REL_OFFSET r15, R15+\offset
301 /* save partial stack frame */
305 movq_cfi rdi, RDI+16-ARGOFFSET
306 movq_cfi rsi, RSI+16-ARGOFFSET
307 movq_cfi rdx, RDX+16-ARGOFFSET
308 movq_cfi rcx, RCX+16-ARGOFFSET
309 movq_cfi rax, RAX+16-ARGOFFSET
310 movq_cfi r8, R8+16-ARGOFFSET
311 movq_cfi r9, R9+16-ARGOFFSET
312 movq_cfi r10, R10+16-ARGOFFSET
313 movq_cfi r11, R11+16-ARGOFFSET
315 leaq -ARGOFFSET+16(%rsp),%rdi /* arg1 for handler */
316 movq_cfi rbp, 8 /* push %rbp */
317 leaq 8(%rsp), %rbp /* mov %rsp, %ebp */
322 * irqcount is used to check if a CPU is already on an interrupt stack
323 * or not. While this is essentially redundant with preempt_count it is
324 * a little cheaper to use a separate counter in the PDA (short of
325 * moving irq_enter into assembly, which would be too much work)
327 1: incl %gs:pda_irqcount
329 popq_cfi %rax /* move return address... */
330 mov %gs:pda_irqstackptr,%rsp
332 pushq_cfi %rax /* ... to the new stack */
334 * We entered an interrupt context - irqs are off:
342 PARTIAL_FRAME 1 REST_SKIP+8
343 movq 5*8+16(%rsp), %r11 /* save return address */
350 movq %r11, 8(%rsp) /* return address */
351 FIXUP_TOP_OF_STACK %r11, 16
356 /* save complete stack frame */
376 movl $MSR_GS_BASE,%ecx
379 js 1f /* negative -> in kernel */
387 * A newly forked process directly context switches into this.
392 push kernel_eflags(%rip)
393 CFI_ADJUST_CFA_OFFSET 8
394 popf # reset kernel eflags
395 CFI_ADJUST_CFA_OFFSET -8
397 GET_THREAD_INFO(%rcx)
398 testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
402 testl $3,CS-ARGOFFSET(%rsp) # from kernel_thread?
403 je int_ret_from_sys_call
404 testl $_TIF_IA32,TI_flags(%rcx)
405 jnz int_ret_from_sys_call
406 RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
407 jmp ret_from_sys_call
410 call syscall_trace_leave
411 GET_THREAD_INFO(%rcx)
417 * System call entry. Upto 6 arguments in registers are supported.
419 * SYSCALL does not save anything on the stack and does not change the
425 * rax system call number
427 * rcx return address for syscall/sysret, C arg3
430 * r10 arg3 (--> moved to rcx for C)
433 * r11 eflags for syscall/sysret, temporary for C
434 * r12-r15,rbp,rbx saved by C code, not touched.
436 * Interrupts are off on entry.
437 * Only called from user space.
439 * XXX if we had a free scratch register we could save the RSP into the stack frame
440 * and report it properly in ps. Unfortunately we haven't.
442 * When user can change the frames always force IRET. That is because
443 * it deals with uncanonical addresses better. SYSRET has trouble
444 * with them due to bugs in both AMD and Intel CPUs.
450 CFI_DEF_CFA rsp,PDA_STACKOFFSET
452 /*CFI_REGISTER rflags,r11*/
455 * A hypervisor implementation might want to use a label
456 * after the swapgs, so that it can do the swapgs
457 * for the guest and jump here on syscall.
459 ENTRY(system_call_after_swapgs)
461 movq %rsp,%gs:pda_oldrsp
462 movq %gs:pda_kernelstack,%rsp
464 * No need to follow this irqs off/on section - it's straight
467 ENABLE_INTERRUPTS(CLBR_NONE)
469 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
470 movq %rcx,RIP-ARGOFFSET(%rsp)
471 CFI_REL_OFFSET rip,RIP-ARGOFFSET
472 GET_THREAD_INFO(%rcx)
473 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
475 system_call_fastpath:
476 cmpq $__NR_syscall_max,%rax
479 call *sys_call_table(,%rax,8) # XXX: rip relative
480 movq %rax,RAX-ARGOFFSET(%rsp)
482 * Syscall return path ending with SYSRET (fast path)
483 * Has incomplete stack frame and undefined top of stack.
486 movl $_TIF_ALLWORK_MASK,%edi
490 GET_THREAD_INFO(%rcx)
491 DISABLE_INTERRUPTS(CLBR_NONE)
493 movl TI_flags(%rcx),%edx
498 * sysretq will re-enable interrupts:
501 movq RIP-ARGOFFSET(%rsp),%rcx
503 RESTORE_ARGS 0,-ARG_SKIP,1
504 /*CFI_REGISTER rflags,r11*/
505 movq %gs:pda_oldrsp, %rsp
509 /* Handle reschedules */
510 /* edx: work, edi: workmask */
512 bt $TIF_NEED_RESCHED,%edx
515 ENABLE_INTERRUPTS(CLBR_NONE)
517 CFI_ADJUST_CFA_OFFSET 8
520 CFI_ADJUST_CFA_OFFSET -8
523 /* Handle a signal */
526 ENABLE_INTERRUPTS(CLBR_NONE)
527 #ifdef CONFIG_AUDITSYSCALL
528 bt $TIF_SYSCALL_AUDIT,%edx
531 /* edx: work flags (arg3) */
532 leaq do_notify_resume(%rip),%rax
533 leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1
534 xorl %esi,%esi # oldset -> arg2
535 call ptregscall_common
536 movl $_TIF_WORK_MASK,%edi
537 /* Use IRET because user could have changed frame. This
538 works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
539 DISABLE_INTERRUPTS(CLBR_NONE)
544 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
545 jmp ret_from_sys_call
547 #ifdef CONFIG_AUDITSYSCALL
549 * Fast path for syscall audit without full syscall trace.
550 * We just call audit_syscall_entry() directly, and then
551 * jump back to the normal fast path.
554 movq %r10,%r9 /* 6th arg: 4th syscall arg */
555 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
556 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
557 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
558 movq %rax,%rsi /* 2nd arg: syscall number */
559 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
560 call audit_syscall_entry
561 LOAD_ARGS 0 /* reload call-clobbered registers */
562 jmp system_call_fastpath
565 * Return fast path for syscall audit. Call audit_syscall_exit()
566 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
570 movq %rax,%rsi /* second arg, syscall return value */
571 cmpq $0,%rax /* is it < 0? */
572 setl %al /* 1 if so, 0 if not */
573 movzbl %al,%edi /* zero-extend that into %edi */
574 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
575 call audit_syscall_exit
576 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
578 #endif /* CONFIG_AUDITSYSCALL */
580 /* Do syscall tracing */
582 #ifdef CONFIG_AUDITSYSCALL
583 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
587 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
588 FIXUP_TOP_OF_STACK %rdi
590 call syscall_trace_enter
592 * Reload arg registers from stack in case ptrace changed them.
593 * We don't reload %rax because syscall_trace_enter() returned
594 * the value it wants us to use in the table lookup.
596 LOAD_ARGS ARGOFFSET, 1
598 cmpq $__NR_syscall_max,%rax
599 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
600 movq %r10,%rcx /* fixup for C */
601 call *sys_call_table(,%rax,8)
602 movq %rax,RAX-ARGOFFSET(%rsp)
603 /* Use IRET because user could have changed frame */
606 * Syscall return path ending with IRET.
607 * Has correct top of stack, but partial stack frame.
609 .globl int_ret_from_sys_call
610 .globl int_with_check
611 int_ret_from_sys_call:
612 DISABLE_INTERRUPTS(CLBR_NONE)
614 testl $3,CS-ARGOFFSET(%rsp)
615 je retint_restore_args
616 movl $_TIF_ALLWORK_MASK,%edi
617 /* edi: mask to check */
620 GET_THREAD_INFO(%rcx)
621 movl TI_flags(%rcx),%edx
624 andl $~TS_COMPAT,TI_status(%rcx)
627 /* Either reschedule or signal or syscall exit tracking needed. */
628 /* First do a reschedule test. */
629 /* edx: work, edi: workmask */
631 bt $TIF_NEED_RESCHED,%edx
634 ENABLE_INTERRUPTS(CLBR_NONE)
636 CFI_ADJUST_CFA_OFFSET 8
639 CFI_ADJUST_CFA_OFFSET -8
640 DISABLE_INTERRUPTS(CLBR_NONE)
644 /* handle signals and tracing -- both require a full stack frame */
647 ENABLE_INTERRUPTS(CLBR_NONE)
649 /* Check for syscall exit trace */
650 testl $_TIF_WORK_SYSCALL_EXIT,%edx
653 CFI_ADJUST_CFA_OFFSET 8
654 leaq 8(%rsp),%rdi # &ptregs -> arg1
655 call syscall_trace_leave
657 CFI_ADJUST_CFA_OFFSET -8
658 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
662 testl $_TIF_DO_NOTIFY_MASK,%edx
664 movq %rsp,%rdi # &ptregs -> arg1
665 xorl %esi,%esi # oldset -> arg2
666 call do_notify_resume
667 1: movl $_TIF_WORK_MASK,%edi
670 DISABLE_INTERRUPTS(CLBR_NONE)
677 * Certain special system calls that need to save a complete full stack frame.
679 .macro PTREGSCALL label,func,arg
681 PARTIAL_FRAME 1 8 /* offset 8: return address */
682 subq $REST_SKIP, %rsp
683 CFI_ADJUST_CFA_OFFSET REST_SKIP
685 DEFAULT_FRAME 0 8 /* offset 8: return address */
686 leaq 8(%rsp), \arg /* pt_regs pointer */
688 jmp ptregscall_common
693 PTREGSCALL stub_clone, sys_clone, %r8
694 PTREGSCALL stub_fork, sys_fork, %rdi
695 PTREGSCALL stub_vfork, sys_vfork, %rdi
696 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
697 PTREGSCALL stub_iopl, sys_iopl, %rsi
699 ENTRY(ptregscall_common)
700 DEFAULT_FRAME 1 8 /* offset 8: return address */
701 RESTORE_TOP_OF_STACK %r11, 8
702 movq_cfi_restore R15+8, r15
703 movq_cfi_restore R14+8, r14
704 movq_cfi_restore R13+8, r13
705 movq_cfi_restore R12+8, r12
706 movq_cfi_restore RBP+8, rbp
707 movq_cfi_restore RBX+8, rbx
708 ret $REST_SKIP /* pop extended registers */
710 END(ptregscall_common)
715 CFI_ADJUST_CFA_OFFSET -8
716 CFI_REGISTER rip, r11
718 FIXUP_TOP_OF_STACK %r11
721 RESTORE_TOP_OF_STACK %r11
724 jmp int_ret_from_sys_call
729 * sigreturn is special because it needs to restore all registers on return.
730 * This cannot be done with SYSRET, so use the IRET return path instead.
732 ENTRY(stub_rt_sigreturn)
735 CFI_ADJUST_CFA_OFFSET -8
738 FIXUP_TOP_OF_STACK %r11
739 call sys_rt_sigreturn
740 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
742 jmp int_ret_from_sys_call
744 END(stub_rt_sigreturn)
747 * Build the entry stubs and pointer table with some assembler magic.
748 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
749 * single cache line on all modern x86 implementations.
751 .section .init.rodata,"a"
755 .p2align CONFIG_X86_L1_CACHE_SHIFT
756 ENTRY(irq_entries_start)
758 vector=FIRST_EXTERNAL_VECTOR
759 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
762 .if vector < NR_VECTORS
763 .if vector <> FIRST_EXTERNAL_VECTOR
764 CFI_ADJUST_CFA_OFFSET -8
766 1: pushq $(~vector+0x80) /* Note: always in signed byte range */
767 CFI_ADJUST_CFA_OFFSET 8
768 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
777 2: jmp common_interrupt
780 END(irq_entries_start)
787 * Interrupt entry/exit.
789 * Interrupt entry points save only callee clobbered registers in fast path.
791 * Entry runs with interrupts off.
794 /* 0(%rsp): ~(interrupt number) */
795 .macro interrupt func
797 CFI_ADJUST_CFA_OFFSET 10*8
804 * The interrupt stubs push (~vector+0x80) onto the stack and
805 * then jump to common_interrupt.
807 .p2align CONFIG_X86_L1_CACHE_SHIFT
810 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
812 /* 0(%rsp): oldrsp-ARGOFFSET */
814 DISABLE_INTERRUPTS(CLBR_NONE)
816 decl %gs:pda_irqcount
818 CFI_DEF_CFA_REGISTER rsp
819 CFI_ADJUST_CFA_OFFSET -8
821 GET_THREAD_INFO(%rcx)
822 testl $3,CS-ARGOFFSET(%rsp)
825 /* Interrupt came from user space */
827 * Has a correct top of stack, but a partial stack frame
828 * %rcx: thread info. Interrupts off.
830 retint_with_reschedule:
831 movl $_TIF_WORK_MASK,%edi
834 movl TI_flags(%rcx),%edx
839 retint_swapgs: /* return to user-space */
841 * The iretq could re-enable interrupts:
843 DISABLE_INTERRUPTS(CLBR_ANY)
848 retint_restore_args: /* return to kernel space */
849 DISABLE_INTERRUPTS(CLBR_ANY)
851 * The iretq could re-enable interrupts:
860 .section __ex_table, "a"
861 .quad irq_return, bad_iret
864 #ifdef CONFIG_PARAVIRT
868 .section __ex_table,"a"
869 .quad native_iret, bad_iret
876 * The iret traps when the %cs or %ss being restored is bogus.
877 * We've lost the original trap vector and error code.
878 * #GPF is the most likely one to get for an invalid selector.
879 * So pretend we completed the iret and took the #GPF in user mode.
881 * We are now running with the kernel GS after exception recovery.
882 * But error_entry expects us to have user GS to match the user %cs,
888 jmp general_protection
892 /* edi: workmask, edx: work */
895 bt $TIF_NEED_RESCHED,%edx
898 ENABLE_INTERRUPTS(CLBR_NONE)
900 CFI_ADJUST_CFA_OFFSET 8
903 CFI_ADJUST_CFA_OFFSET -8
904 GET_THREAD_INFO(%rcx)
905 DISABLE_INTERRUPTS(CLBR_NONE)
910 testl $_TIF_DO_NOTIFY_MASK,%edx
913 ENABLE_INTERRUPTS(CLBR_NONE)
915 movq $-1,ORIG_RAX(%rsp)
916 xorl %esi,%esi # oldset
917 movq %rsp,%rdi # &pt_regs
918 call do_notify_resume
920 DISABLE_INTERRUPTS(CLBR_NONE)
922 GET_THREAD_INFO(%rcx)
923 jmp retint_with_reschedule
925 #ifdef CONFIG_PREEMPT
926 /* Returning to kernel space. Check if we need preemption */
927 /* rcx: threadinfo. interrupts off. */
929 cmpl $0,TI_preempt_count(%rcx)
930 jnz retint_restore_args
931 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
932 jnc retint_restore_args
933 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
934 jnc retint_restore_args
935 call preempt_schedule_irq
940 END(common_interrupt)
947 .macro apicinterrupt num,func
950 CFI_ADJUST_CFA_OFFSET 8
956 ENTRY(thermal_interrupt)
957 apicinterrupt THERMAL_APIC_VECTOR,smp_thermal_interrupt
958 END(thermal_interrupt)
960 ENTRY(threshold_interrupt)
961 apicinterrupt THRESHOLD_APIC_VECTOR,mce_threshold_interrupt
962 END(threshold_interrupt)
965 ENTRY(reschedule_interrupt)
966 apicinterrupt RESCHEDULE_VECTOR,smp_reschedule_interrupt
967 END(reschedule_interrupt)
969 .macro INVALIDATE_ENTRY num
970 ENTRY(invalidate_interrupt\num)
971 apicinterrupt INVALIDATE_TLB_VECTOR_START+\num,smp_invalidate_interrupt
972 END(invalidate_interrupt\num)
984 ENTRY(call_function_interrupt)
985 apicinterrupt CALL_FUNCTION_VECTOR,smp_call_function_interrupt
986 END(call_function_interrupt)
987 ENTRY(call_function_single_interrupt)
988 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR,smp_call_function_single_interrupt
989 END(call_function_single_interrupt)
990 ENTRY(irq_move_cleanup_interrupt)
991 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR,smp_irq_move_cleanup_interrupt
992 END(irq_move_cleanup_interrupt)
995 ENTRY(apic_timer_interrupt)
996 apicinterrupt LOCAL_TIMER_VECTOR,smp_apic_timer_interrupt
997 END(apic_timer_interrupt)
999 ENTRY(uv_bau_message_intr1)
1000 apicinterrupt 220,uv_bau_message_interrupt
1001 END(uv_bau_message_intr1)
1003 ENTRY(error_interrupt)
1004 apicinterrupt ERROR_APIC_VECTOR,smp_error_interrupt
1005 END(error_interrupt)
1007 ENTRY(spurious_interrupt)
1008 apicinterrupt SPURIOUS_APIC_VECTOR,smp_spurious_interrupt
1009 END(spurious_interrupt)
1012 * Exception entry points.
1014 .macro zeroentry sym
1016 PARAVIRT_ADJUST_EXCEPTION_FRAME
1017 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1019 CFI_ADJUST_CFA_OFFSET 15*8
1022 movq %rsp,%rdi /* pt_regs pointer */
1023 xorl %esi,%esi /* no error code */
1025 jmp error_exit /* %ebx: no swapgs flag */
1029 .macro paranoidzeroentry sym
1031 PARAVIRT_ADJUST_EXCEPTION_FRAME
1032 pushq $-1 /* ORIG_RAX: no syscall to restart */
1033 CFI_ADJUST_CFA_OFFSET 8
1037 movq %rsp,%rdi /* pt_regs pointer */
1038 xorl %esi,%esi /* no error code */
1040 jmp paranoid_exit /* %ebx: no swapgs flag */
1044 .macro paranoidzeroentry_ist sym ist
1046 PARAVIRT_ADJUST_EXCEPTION_FRAME
1047 pushq $-1 /* ORIG_RAX: no syscall to restart */
1048 CFI_ADJUST_CFA_OFFSET 8
1052 movq %rsp,%rdi /* pt_regs pointer */
1053 xorl %esi,%esi /* no error code */
1054 movq %gs:pda_data_offset, %rbp
1055 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
1057 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
1058 jmp paranoid_exit /* %ebx: no swapgs flag */
1062 .macro errorentry sym
1064 PARAVIRT_ADJUST_EXCEPTION_FRAME
1066 CFI_ADJUST_CFA_OFFSET 15*8
1069 movq %rsp,%rdi /* pt_regs pointer */
1070 movq ORIG_RAX(%rsp),%rsi /* get error code */
1071 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1073 jmp error_exit /* %ebx: no swapgs flag */
1077 /* error code is on the stack already */
1078 .macro paranoiderrorentry sym
1080 PARAVIRT_ADJUST_EXCEPTION_FRAME
1082 CFI_ADJUST_CFA_OFFSET 15*8
1086 movq %rsp,%rdi /* pt_regs pointer */
1087 movq ORIG_RAX(%rsp),%rsi /* get error code */
1088 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1090 jmp paranoid_exit /* %ebx: no swapgs flag */
1095 * "Paranoid" exit path from exception stack.
1096 * Paranoid because this is used by NMIs and cannot take
1097 * any kernel state for granted.
1098 * We don't do kernel preemption checks here, because only
1099 * NMI should be common and it does not enable IRQs and
1100 * cannot get reschedule ticks.
1102 * "trace" is 0 for the NMI handler only, because irq-tracing
1103 * is fundamentally NMI-unsafe. (we cannot change the soft and
1104 * hard flags at once, atomically)
1107 /* ebx: no swapgs flag */
1108 KPROBE_ENTRY(paranoid_exit)
1110 DISABLE_INTERRUPTS(CLBR_NONE)
1112 testl %ebx,%ebx /* swapgs needed? */
1113 jnz paranoid_restore
1115 jnz paranoid_userspace
1123 GET_THREAD_INFO(%rcx)
1124 movl TI_flags(%rcx),%ebx
1125 andl $_TIF_WORK_MASK,%ebx
1127 movq %rsp,%rdi /* &pt_regs */
1129 movq %rax,%rsp /* switch stack for scheduling */
1130 testl $_TIF_NEED_RESCHED,%ebx
1131 jnz paranoid_schedule
1132 movl %ebx,%edx /* arg3: thread flags */
1134 ENABLE_INTERRUPTS(CLBR_NONE)
1135 xorl %esi,%esi /* arg2: oldset */
1136 movq %rsp,%rdi /* arg1: &pt_regs */
1137 call do_notify_resume
1138 DISABLE_INTERRUPTS(CLBR_NONE)
1140 jmp paranoid_userspace
1143 ENABLE_INTERRUPTS(CLBR_ANY)
1145 DISABLE_INTERRUPTS(CLBR_ANY)
1147 jmp paranoid_userspace
1152 * Exception entry point. This expects an error code/orig_rax on the stack.
1153 * returns in "no swapgs flag" in %ebx.
1155 KPROBE_ENTRY(error_entry)
1157 CFI_ADJUST_CFA_OFFSET 15*8
1158 /* oldrax contains error code */
1177 je error_kernelspace
1186 * There are two places in the kernel that can potentially fault with
1187 * usergs. Handle them here. The exception handlers after iret run with
1188 * kernel gs again, so don't set the user space flag. B stepping K8s
1189 * sometimes report an truncated RIP for IRET exceptions returning to
1190 * compat mode. Check for these here too.
1194 leaq irq_return(%rip),%rcx
1195 cmpq %rcx,RIP+8(%rsp)
1197 movl %ecx,%ecx /* zero extend */
1198 cmpq %rcx,RIP+8(%rsp)
1200 cmpq $gs_change,RIP+8(%rsp)
1203 KPROBE_END(error_entry)
1206 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1207 KPROBE_ENTRY(error_exit)
1211 DISABLE_INTERRUPTS(CLBR_NONE)
1213 GET_THREAD_INFO(%rcx)
1216 LOCKDEP_SYS_EXIT_IRQ
1217 movl TI_flags(%rcx),%edx
1218 movl $_TIF_WORK_MASK,%edi
1223 KPROBE_END(error_exit)
1225 /* Reload gs selector with exception handling */
1226 /* edi: new selector */
1227 ENTRY(native_load_gs_index)
1230 CFI_ADJUST_CFA_OFFSET 8
1231 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1235 2: mfence /* workaround */
1238 CFI_ADJUST_CFA_OFFSET -8
1241 ENDPROC(native_load_gs_index)
1243 .section __ex_table,"a"
1245 .quad gs_change,bad_gs
1247 .section .fixup,"ax"
1248 /* running with kernelgs */
1250 SWAPGS /* switch back to user gs */
1257 * Create a kernel thread.
1259 * C extern interface:
1260 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1262 * asm input arguments:
1263 * rdi: fn, rsi: arg, rdx: flags
1265 ENTRY(kernel_thread)
1267 FAKE_STACK_FRAME $child_rip
1270 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1272 orq kernel_thread_flags(%rip),%rdi
1285 * It isn't worth to check for reschedule here,
1286 * so internally to the x86_64 port you can rely on kernel_thread()
1287 * not to reschedule the child before returning, this avoids the need
1288 * of hacks for example to fork off the per-CPU idle tasks.
1289 * [Hopefully no generic code relies on the reschedule -AK]
1295 ENDPROC(kernel_thread)
1298 pushq $0 # fake return address
1301 * Here we are in the child and the registers are set as they were
1302 * at kernel_thread() invocation in the parent.
1314 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1316 * C extern interface:
1317 * extern long execve(char *name, char **argv, char **envp)
1319 * asm input arguments:
1320 * rdi: name, rsi: argv, rdx: envp
1322 * We want to fallback into:
1323 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
1325 * do_sys_execve asm fallback arguments:
1326 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1328 ENTRY(kernel_execve)
1334 movq %rax, RAX(%rsp)
1337 je int_ret_from_sys_call
1342 ENDPROC(kernel_execve)
1344 KPROBE_ENTRY(page_fault)
1345 errorentry do_page_fault
1346 KPROBE_END(page_fault)
1348 ENTRY(coprocessor_error)
1349 zeroentry do_coprocessor_error
1350 END(coprocessor_error)
1352 ENTRY(simd_coprocessor_error)
1353 zeroentry do_simd_coprocessor_error
1354 END(simd_coprocessor_error)
1356 ENTRY(device_not_available)
1357 zeroentry do_device_not_available
1358 END(device_not_available)
1360 /* runs on exception stack */
1362 paranoidzeroentry_ist do_debug, DEBUG_STACK
1365 /* runs on exception stack */
1368 PARAVIRT_ADJUST_EXCEPTION_FRAME
1371 CFI_ADJUST_CFA_OFFSET 15*8
1374 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1378 #ifdef CONFIG_TRACE_IRQFLAGS
1379 /* paranoidexit; without TRACE_IRQS_OFF */
1380 /* ebx: no swapgs flag */
1381 DISABLE_INTERRUPTS(CLBR_NONE)
1382 testl %ebx,%ebx /* swapgs needed? */
1392 GET_THREAD_INFO(%rcx)
1393 movl TI_flags(%rcx),%ebx
1394 andl $_TIF_WORK_MASK,%ebx
1396 movq %rsp,%rdi /* &pt_regs */
1398 movq %rax,%rsp /* switch stack for scheduling */
1399 testl $_TIF_NEED_RESCHED,%ebx
1401 movl %ebx,%edx /* arg3: thread flags */
1402 ENABLE_INTERRUPTS(CLBR_NONE)
1403 xorl %esi,%esi /* arg2: oldset */
1404 movq %rsp,%rdi /* arg1: &pt_regs */
1405 call do_notify_resume
1406 DISABLE_INTERRUPTS(CLBR_NONE)
1409 ENABLE_INTERRUPTS(CLBR_ANY)
1411 DISABLE_INTERRUPTS(CLBR_ANY)
1421 paranoidzeroentry_ist do_int3, DEBUG_STACK
1425 zeroentry do_overflow
1433 zeroentry do_invalid_op
1436 ENTRY(coprocessor_segment_overrun)
1437 zeroentry do_coprocessor_segment_overrun
1438 END(coprocessor_segment_overrun)
1440 /* runs on exception stack */
1442 paranoiderrorentry do_double_fault
1446 errorentry do_invalid_TSS
1449 ENTRY(segment_not_present)
1450 errorentry do_segment_not_present
1451 END(segment_not_present)
1453 /* runs on exception stack */
1454 ENTRY(stack_segment)
1455 paranoiderrorentry do_stack_segment
1458 KPROBE_ENTRY(general_protection)
1459 errorentry do_general_protection
1460 KPROBE_END(general_protection)
1462 ENTRY(alignment_check)
1463 errorentry do_alignment_check
1464 END(alignment_check)
1467 zeroentry do_divide_error
1470 ENTRY(spurious_interrupt_bug)
1471 zeroentry do_spurious_interrupt_bug
1472 END(spurious_interrupt_bug)
1474 #ifdef CONFIG_X86_MCE
1475 /* runs on exception stack */
1476 ENTRY(machine_check)
1477 paranoidzeroentry do_machine_check
1481 /* Call softirq on interrupt stack. Interrupts are off. */
1485 CFI_ADJUST_CFA_OFFSET 8
1486 CFI_REL_OFFSET rbp,0
1488 CFI_DEF_CFA_REGISTER rbp
1489 incl %gs:pda_irqcount
1490 cmove %gs:pda_irqstackptr,%rsp
1491 push %rbp # backlink for old unwinder
1494 CFI_DEF_CFA_REGISTER rsp
1495 CFI_ADJUST_CFA_OFFSET -8
1496 decl %gs:pda_irqcount
1499 ENDPROC(call_softirq)
1501 KPROBE_ENTRY(ignore_sysret)
1506 ENDPROC(ignore_sysret)
1509 ENTRY(xen_hypervisor_callback)
1510 zeroentry xen_do_hypervisor_callback
1511 END(xen_hypervisor_callback)
1514 # A note on the "critical region" in our callback handler.
1515 # We want to avoid stacking callback handlers due to events occurring
1516 # during handling of the last event. To do this, we keep events disabled
1517 # until we've done all processing. HOWEVER, we must enable events before
1518 # popping the stack frame (can't be done atomically) and so it would still
1519 # be possible to get enough handler activations to overflow the stack.
1520 # Although unlikely, bugs of that kind are hard to track down, so we'd
1521 # like to avoid the possibility.
1522 # So, on entry to the handler we detect whether we interrupted an
1523 # existing activation in its critical region -- if so, we pop the current
1524 # activation and restart the handler using the previous one.
1526 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1528 /* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1529 see the correct pointer to the pt_regs */
1530 movq %rdi, %rsp # we don't return, adjust the stack frame
1533 11: incl %gs:pda_irqcount
1535 CFI_DEF_CFA_REGISTER rbp
1536 cmovzq %gs:pda_irqstackptr,%rsp
1537 pushq %rbp # backlink for old unwinder
1538 call xen_evtchn_do_upcall
1540 CFI_DEF_CFA_REGISTER rsp
1541 decl %gs:pda_irqcount
1544 END(do_hypervisor_callback)
1547 # Hypervisor uses this for application faults while it executes.
1548 # We get here for two reasons:
1549 # 1. Fault while reloading DS, ES, FS or GS
1550 # 2. Fault while executing IRET
1551 # Category 1 we do not need to fix up as Xen has already reloaded all segment
1552 # registers that could be reloaded and zeroed the others.
1553 # Category 2 we fix up by killing the current process. We cannot use the
1554 # normal Linux return path in this case because if we use the IRET hypercall
1555 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1556 # We distinguish between categories by comparing each saved segment register
1557 # with its current contents: any discrepancy means we in category 1.
1559 ENTRY(xen_failsafe_callback)
1561 /*CFI_REL_OFFSET gs,GS*/
1562 /*CFI_REL_OFFSET fs,FS*/
1563 /*CFI_REL_OFFSET es,ES*/
1564 /*CFI_REL_OFFSET ds,DS*/
1565 CFI_REL_OFFSET r11,8
1566 CFI_REL_OFFSET rcx,0
1580 /* All segments match their saved values => Category 2 (Bad IRET). */
1586 CFI_ADJUST_CFA_OFFSET -0x30
1587 pushq_cfi $0 /* RIP */
1590 jmp general_protection
1592 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1598 CFI_ADJUST_CFA_OFFSET -0x30
1603 END(xen_failsafe_callback)
1605 #endif /* CONFIG_XEN */