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 up to 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>
51 #include <asm/page_types.h>
52 #include <asm/irqflags.h>
53 #include <asm/paravirt.h>
54 #include <asm/ftrace.h>
55 #include <asm/percpu.h>
57 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
58 #include <linux/elf-em.h>
59 #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
60 #define __AUDIT_ARCH_64BIT 0x80000000
61 #define __AUDIT_ARCH_LE 0x40000000
64 .section .entry.text, "ax"
66 #ifdef CONFIG_FUNCTION_TRACER
67 #ifdef CONFIG_DYNAMIC_FTRACE
73 cmpl $0, function_trace_stop
80 subq $MCOUNT_INSN_SIZE, %rdi
87 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
88 GLOBAL(ftrace_graph_call)
96 #else /* ! CONFIG_DYNAMIC_FTRACE */
98 cmpl $0, function_trace_stop
101 cmpq $ftrace_stub, ftrace_trace_function
104 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
105 cmpq $ftrace_stub, ftrace_graph_return
106 jnz ftrace_graph_caller
108 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
109 jnz ftrace_graph_caller
118 movq 0x38(%rsp), %rdi
120 subq $MCOUNT_INSN_SIZE, %rdi
122 call *ftrace_trace_function
128 #endif /* CONFIG_DYNAMIC_FTRACE */
129 #endif /* CONFIG_FUNCTION_TRACER */
131 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
132 ENTRY(ftrace_graph_caller)
133 cmpl $0, function_trace_stop
139 movq 0x38(%rsp), %rsi
141 subq $MCOUNT_INSN_SIZE, %rsi
143 call prepare_ftrace_return
148 END(ftrace_graph_caller)
150 GLOBAL(return_to_handler)
153 /* Save the return values */
158 call ftrace_return_to_handler
168 #ifndef CONFIG_PREEMPT
169 #define retint_kernel retint_restore_args
172 #ifdef CONFIG_PARAVIRT
173 ENTRY(native_usergs_sysret64)
176 ENDPROC(native_usergs_sysret64)
177 #endif /* CONFIG_PARAVIRT */
180 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
181 #ifdef CONFIG_TRACE_IRQFLAGS
182 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
190 * C code is not supposed to know about undefined top of stack. Every time
191 * a C function with an pt_regs argument is called from the SYSCALL based
192 * fast path FIXUP_TOP_OF_STACK is needed.
193 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
197 /* %rsp:at FRAMEEND */
198 .macro FIXUP_TOP_OF_STACK tmp offset=0
199 movq PER_CPU_VAR(old_rsp),\tmp
200 movq \tmp,RSP+\offset(%rsp)
201 movq $__USER_DS,SS+\offset(%rsp)
202 movq $__USER_CS,CS+\offset(%rsp)
203 movq $-1,RCX+\offset(%rsp)
204 movq R11+\offset(%rsp),\tmp /* get eflags */
205 movq \tmp,EFLAGS+\offset(%rsp)
208 .macro RESTORE_TOP_OF_STACK tmp offset=0
209 movq RSP+\offset(%rsp),\tmp
210 movq \tmp,PER_CPU_VAR(old_rsp)
211 movq EFLAGS+\offset(%rsp),\tmp
212 movq \tmp,R11+\offset(%rsp)
215 .macro FAKE_STACK_FRAME child_rip
216 /* push in order ss, rsp, eflags, cs, rip */
218 pushq_cfi $__KERNEL_DS /* ss */
219 /*CFI_REL_OFFSET ss,0*/
220 pushq_cfi %rax /* rsp */
222 pushq_cfi $X86_EFLAGS_IF /* eflags - interrupts on */
223 /*CFI_REL_OFFSET rflags,0*/
224 pushq_cfi $__KERNEL_CS /* cs */
225 /*CFI_REL_OFFSET cs,0*/
226 pushq_cfi \child_rip /* rip */
228 pushq_cfi %rax /* orig rax */
231 .macro UNFAKE_STACK_FRAME
233 CFI_ADJUST_CFA_OFFSET -(6*8)
237 * initial frame state for interrupts (and exceptions without error code)
239 .macro EMPTY_FRAME start=1 offset=0
243 CFI_DEF_CFA rsp,8+\offset
245 CFI_DEF_CFA_OFFSET 8+\offset
250 * initial frame state for interrupts (and exceptions without error code)
252 .macro INTR_FRAME start=1 offset=0
253 EMPTY_FRAME \start, SS+8+\offset-RIP
254 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
255 CFI_REL_OFFSET rsp, RSP+\offset-RIP
256 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
257 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
258 CFI_REL_OFFSET rip, RIP+\offset-RIP
262 * initial frame state for exceptions with error code (and interrupts
263 * with vector already pushed)
265 .macro XCPT_FRAME start=1 offset=0
266 INTR_FRAME \start, RIP+\offset-ORIG_RAX
267 /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
271 * frame that enables calling into C.
273 .macro PARTIAL_FRAME start=1 offset=0
274 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
275 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
276 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
277 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
278 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
279 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
280 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
281 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
282 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
283 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
287 * frame that enables passing a complete pt_regs to a C function.
289 .macro DEFAULT_FRAME start=1 offset=0
290 PARTIAL_FRAME \start, R11+\offset-R15
291 CFI_REL_OFFSET rbx, RBX+\offset
292 CFI_REL_OFFSET rbp, RBP+\offset
293 CFI_REL_OFFSET r12, R12+\offset
294 CFI_REL_OFFSET r13, R13+\offset
295 CFI_REL_OFFSET r14, R14+\offset
296 CFI_REL_OFFSET r15, R15+\offset
299 /* save partial stack frame */
302 /* start from rbp in pt_regs and jump over */
303 movq_cfi rdi, RDI-RBP
304 movq_cfi rsi, RSI-RBP
305 movq_cfi rdx, RDX-RBP
306 movq_cfi rcx, RCX-RBP
307 movq_cfi rax, RAX-RBP
310 movq_cfi r10, R10-RBP
311 movq_cfi r11, R11-RBP
313 movq_cfi rbp, 0 /* push %rbp */
316 leaq -RBP(%rsp),%rdi /* arg1 for handler */
321 * irq_count is used to check if a CPU is already on an interrupt stack
322 * or not. While this is essentially redundant with preempt_count it is
323 * a little cheaper to use a separate counter in the PDA (short of
324 * moving irq_enter into assembly, which would be too much work)
326 1: incl PER_CPU_VAR(irq_count)
328 mov PER_CPU_VAR(irq_stack_ptr),%rsp
331 * We entered an interrupt context - irqs are off:
337 PARTIAL_FRAME 1 REST_SKIP+8
338 movq 5*8+16(%rsp), %r11 /* save return address */
345 movq %r11, 8(%rsp) /* return address */
346 FIXUP_TOP_OF_STACK %r11, 16
351 /* save complete stack frame */
352 .pushsection .kprobes.text, "ax"
372 movl $MSR_GS_BASE,%ecx
375 js 1f /* negative -> in kernel */
384 * A newly forked process directly context switches into this address.
386 * rdi: prev task we switched from
391 LOCK ; btr $TIF_FORK,TI_flags(%r8)
393 pushq_cfi kernel_eflags(%rip)
394 popfq_cfi # reset kernel eflags
396 call schedule_tail # rdi: 'prev' task parameter
398 GET_THREAD_INFO(%rcx)
402 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
403 je int_ret_from_sys_call
405 testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
406 jnz int_ret_from_sys_call
408 RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
409 jmp ret_from_sys_call # go to the SYSRET fastpath
415 * System call entry. Up to 6 arguments in registers are supported.
417 * SYSCALL does not save anything on the stack and does not change the
423 * rax system call number
425 * rcx return address for syscall/sysret, C arg3
428 * r10 arg3 (--> moved to rcx for C)
431 * r11 eflags for syscall/sysret, temporary for C
432 * r12-r15,rbp,rbx saved by C code, not touched.
434 * Interrupts are off on entry.
435 * Only called from user space.
437 * XXX if we had a free scratch register we could save the RSP into the stack frame
438 * and report it properly in ps. Unfortunately we haven't.
440 * When user can change the frames always force IRET. That is because
441 * it deals with uncanonical addresses better. SYSRET has trouble
442 * with them due to bugs in both AMD and Intel CPUs.
448 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
450 /*CFI_REGISTER rflags,r11*/
453 * A hypervisor implementation might want to use a label
454 * after the swapgs, so that it can do the swapgs
455 * for the guest and jump here on syscall.
457 ENTRY(system_call_after_swapgs)
459 movq %rsp,PER_CPU_VAR(old_rsp)
460 movq PER_CPU_VAR(kernel_stack),%rsp
462 * No need to follow this irqs off/on section - it's straight
465 ENABLE_INTERRUPTS(CLBR_NONE)
467 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
468 movq %rcx,RIP-ARGOFFSET(%rsp)
469 CFI_REL_OFFSET rip,RIP-ARGOFFSET
470 GET_THREAD_INFO(%rcx)
471 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
473 system_call_fastpath:
474 cmpq $__NR_syscall_max,%rax
477 call *sys_call_table(,%rax,8) # XXX: rip relative
478 movq %rax,RAX-ARGOFFSET(%rsp)
480 * Syscall return path ending with SYSRET (fast path)
481 * Has incomplete stack frame and undefined top of stack.
484 movl $_TIF_ALLWORK_MASK,%edi
488 GET_THREAD_INFO(%rcx)
489 DISABLE_INTERRUPTS(CLBR_NONE)
491 movl TI_flags(%rcx),%edx
496 * sysretq will re-enable interrupts:
499 movq RIP-ARGOFFSET(%rsp),%rcx
501 RESTORE_ARGS 0,-ARG_SKIP,1
502 /*CFI_REGISTER rflags,r11*/
503 movq PER_CPU_VAR(old_rsp), %rsp
507 /* Handle reschedules */
508 /* edx: work, edi: workmask */
510 bt $TIF_NEED_RESCHED,%edx
513 ENABLE_INTERRUPTS(CLBR_NONE)
519 /* Handle a signal */
522 ENABLE_INTERRUPTS(CLBR_NONE)
523 #ifdef CONFIG_AUDITSYSCALL
524 bt $TIF_SYSCALL_AUDIT,%edx
528 * We have a signal, or exit tracing or single-step.
529 * These all wind up with the iret return path anyway,
530 * so just join that path right now.
532 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
533 jmp int_check_syscall_exit_work
536 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
537 jmp ret_from_sys_call
539 #ifdef CONFIG_AUDITSYSCALL
541 * Fast path for syscall audit without full syscall trace.
542 * We just call audit_syscall_entry() directly, and then
543 * jump back to the normal fast path.
546 movq %r10,%r9 /* 6th arg: 4th syscall arg */
547 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
548 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
549 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
550 movq %rax,%rsi /* 2nd arg: syscall number */
551 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
552 call audit_syscall_entry
553 LOAD_ARGS 0 /* reload call-clobbered registers */
554 jmp system_call_fastpath
557 * Return fast path for syscall audit. Call audit_syscall_exit()
558 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
562 movq RAX-ARGOFFSET(%rsp),%rsi /* second arg, syscall return value */
563 cmpq $0,%rsi /* is it < 0? */
564 setl %al /* 1 if so, 0 if not */
565 movzbl %al,%edi /* zero-extend that into %edi */
566 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
567 call audit_syscall_exit
568 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
570 #endif /* CONFIG_AUDITSYSCALL */
572 /* Do syscall tracing */
574 #ifdef CONFIG_AUDITSYSCALL
575 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
579 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
580 FIXUP_TOP_OF_STACK %rdi
582 call syscall_trace_enter
584 * Reload arg registers from stack in case ptrace changed them.
585 * We don't reload %rax because syscall_trace_enter() returned
586 * the value it wants us to use in the table lookup.
588 LOAD_ARGS ARGOFFSET, 1
590 cmpq $__NR_syscall_max,%rax
591 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
592 movq %r10,%rcx /* fixup for C */
593 call *sys_call_table(,%rax,8)
594 movq %rax,RAX-ARGOFFSET(%rsp)
595 /* Use IRET because user could have changed frame */
598 * Syscall return path ending with IRET.
599 * Has correct top of stack, but partial stack frame.
601 GLOBAL(int_ret_from_sys_call)
602 DISABLE_INTERRUPTS(CLBR_NONE)
604 testl $3,CS-ARGOFFSET(%rsp)
605 je retint_restore_args
606 movl $_TIF_ALLWORK_MASK,%edi
607 /* edi: mask to check */
608 GLOBAL(int_with_check)
610 GET_THREAD_INFO(%rcx)
611 movl TI_flags(%rcx),%edx
614 andl $~TS_COMPAT,TI_status(%rcx)
617 /* Either reschedule or signal or syscall exit tracking needed. */
618 /* First do a reschedule test. */
619 /* edx: work, edi: workmask */
621 bt $TIF_NEED_RESCHED,%edx
624 ENABLE_INTERRUPTS(CLBR_NONE)
628 DISABLE_INTERRUPTS(CLBR_NONE)
632 /* handle signals and tracing -- both require a full stack frame */
635 ENABLE_INTERRUPTS(CLBR_NONE)
636 int_check_syscall_exit_work:
638 /* Check for syscall exit trace */
639 testl $_TIF_WORK_SYSCALL_EXIT,%edx
642 leaq 8(%rsp),%rdi # &ptregs -> arg1
643 call syscall_trace_leave
645 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
649 testl $_TIF_DO_NOTIFY_MASK,%edx
651 movq %rsp,%rdi # &ptregs -> arg1
652 xorl %esi,%esi # oldset -> arg2
653 call do_notify_resume
654 1: movl $_TIF_WORK_MASK,%edi
657 DISABLE_INTERRUPTS(CLBR_NONE)
664 * Certain special system calls that need to save a complete full stack frame.
666 .macro PTREGSCALL label,func,arg
668 PARTIAL_FRAME 1 8 /* offset 8: return address */
669 subq $REST_SKIP, %rsp
670 CFI_ADJUST_CFA_OFFSET REST_SKIP
672 DEFAULT_FRAME 0 8 /* offset 8: return address */
673 leaq 8(%rsp), \arg /* pt_regs pointer */
675 jmp ptregscall_common
680 PTREGSCALL stub_clone, sys_clone, %r8
681 PTREGSCALL stub_fork, sys_fork, %rdi
682 PTREGSCALL stub_vfork, sys_vfork, %rdi
683 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
684 PTREGSCALL stub_iopl, sys_iopl, %rsi
686 ENTRY(ptregscall_common)
687 DEFAULT_FRAME 1 8 /* offset 8: return address */
688 RESTORE_TOP_OF_STACK %r11, 8
689 movq_cfi_restore R15+8, r15
690 movq_cfi_restore R14+8, r14
691 movq_cfi_restore R13+8, r13
692 movq_cfi_restore R12+8, r12
693 movq_cfi_restore RBP+8, rbp
694 movq_cfi_restore RBX+8, rbx
695 ret $REST_SKIP /* pop extended registers */
697 END(ptregscall_common)
704 FIXUP_TOP_OF_STACK %r11
707 RESTORE_TOP_OF_STACK %r11
710 jmp int_ret_from_sys_call
715 * sigreturn is special because it needs to restore all registers on return.
716 * This cannot be done with SYSRET, so use the IRET return path instead.
718 ENTRY(stub_rt_sigreturn)
724 FIXUP_TOP_OF_STACK %r11
725 call sys_rt_sigreturn
726 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
728 jmp int_ret_from_sys_call
730 END(stub_rt_sigreturn)
733 * Build the entry stubs and pointer table with some assembler magic.
734 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
735 * single cache line on all modern x86 implementations.
737 .section .init.rodata,"a"
741 .p2align CONFIG_X86_L1_CACHE_SHIFT
742 ENTRY(irq_entries_start)
744 vector=FIRST_EXTERNAL_VECTOR
745 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
748 .if vector < NR_VECTORS
749 .if vector <> FIRST_EXTERNAL_VECTOR
750 CFI_ADJUST_CFA_OFFSET -8
752 1: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
753 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
762 2: jmp common_interrupt
765 END(irq_entries_start)
772 * Interrupt entry/exit.
774 * Interrupt entry points save only callee clobbered registers in fast path.
776 * Entry runs with interrupts off.
779 /* 0(%rsp): ~(interrupt number) */
780 .macro interrupt func
781 /* reserve pt_regs for scratch regs and rbp */
782 subq $ORIG_RAX-RBP, %rsp
783 CFI_ADJUST_CFA_OFFSET ORIG_RAX-RBP
790 * Interrupt entry/exit should be protected against kprobes
792 .pushsection .kprobes.text, "ax"
794 * The interrupt stubs push (~vector+0x80) onto the stack and
795 * then jump to common_interrupt.
797 .p2align CONFIG_X86_L1_CACHE_SHIFT
800 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
802 /* 0(%rsp): old_rsp-ARGOFFSET */
804 DISABLE_INTERRUPTS(CLBR_NONE)
806 decl PER_CPU_VAR(irq_count)
810 CFI_DEF_CFA_REGISTER rsp
811 CFI_ADJUST_CFA_OFFSET -8
813 /* we did not save rbx, restore only from ARGOFFSET */
815 CFI_ADJUST_CFA_OFFSET -8
817 GET_THREAD_INFO(%rcx)
818 testl $3,CS-ARGOFFSET(%rsp)
821 /* Interrupt came from user space */
823 * Has a correct top of stack, but a partial stack frame
824 * %rcx: thread info. Interrupts off.
826 retint_with_reschedule:
827 movl $_TIF_WORK_MASK,%edi
830 movl TI_flags(%rcx),%edx
835 retint_swapgs: /* return to user-space */
837 * The iretq could re-enable interrupts:
839 DISABLE_INTERRUPTS(CLBR_ANY)
844 retint_restore_args: /* return to kernel space */
845 DISABLE_INTERRUPTS(CLBR_ANY)
847 * The iretq could re-enable interrupts:
856 .section __ex_table, "a"
857 .quad irq_return, bad_iret
860 #ifdef CONFIG_PARAVIRT
864 .section __ex_table,"a"
865 .quad native_iret, bad_iret
872 * The iret traps when the %cs or %ss being restored is bogus.
873 * We've lost the original trap vector and error code.
874 * #GPF is the most likely one to get for an invalid selector.
875 * So pretend we completed the iret and took the #GPF in user mode.
877 * We are now running with the kernel GS after exception recovery.
878 * But error_entry expects us to have user GS to match the user %cs,
884 jmp general_protection
888 /* edi: workmask, edx: work */
891 bt $TIF_NEED_RESCHED,%edx
894 ENABLE_INTERRUPTS(CLBR_NONE)
898 GET_THREAD_INFO(%rcx)
899 DISABLE_INTERRUPTS(CLBR_NONE)
904 testl $_TIF_DO_NOTIFY_MASK,%edx
907 ENABLE_INTERRUPTS(CLBR_NONE)
909 movq $-1,ORIG_RAX(%rsp)
910 xorl %esi,%esi # oldset
911 movq %rsp,%rdi # &pt_regs
912 call do_notify_resume
914 DISABLE_INTERRUPTS(CLBR_NONE)
916 GET_THREAD_INFO(%rcx)
917 jmp retint_with_reschedule
919 #ifdef CONFIG_PREEMPT
920 /* Returning to kernel space. Check if we need preemption */
921 /* rcx: threadinfo. interrupts off. */
923 cmpl $0,TI_preempt_count(%rcx)
924 jnz retint_restore_args
925 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
926 jnc retint_restore_args
927 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
928 jnc retint_restore_args
929 call preempt_schedule_irq
934 END(common_interrupt)
936 * End of kprobes section
943 .macro apicinterrupt num sym do_sym
954 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
955 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
956 apicinterrupt REBOOT_VECTOR \
957 reboot_interrupt smp_reboot_interrupt
961 apicinterrupt UV_BAU_MESSAGE \
962 uv_bau_message_intr1 uv_bau_message_interrupt
964 apicinterrupt LOCAL_TIMER_VECTOR \
965 apic_timer_interrupt smp_apic_timer_interrupt
966 apicinterrupt X86_PLATFORM_IPI_VECTOR \
967 x86_platform_ipi smp_x86_platform_ipi
970 .irp idx,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, \
971 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
972 .if NUM_INVALIDATE_TLB_VECTORS > \idx
973 apicinterrupt (INVALIDATE_TLB_VECTOR_START)+\idx \
974 invalidate_interrupt\idx smp_invalidate_interrupt
979 apicinterrupt THRESHOLD_APIC_VECTOR \
980 threshold_interrupt smp_threshold_interrupt
981 apicinterrupt THERMAL_APIC_VECTOR \
982 thermal_interrupt smp_thermal_interrupt
984 #ifdef CONFIG_X86_MCE
985 apicinterrupt MCE_SELF_VECTOR \
986 mce_self_interrupt smp_mce_self_interrupt
990 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
991 call_function_single_interrupt smp_call_function_single_interrupt
992 apicinterrupt CALL_FUNCTION_VECTOR \
993 call_function_interrupt smp_call_function_interrupt
994 apicinterrupt RESCHEDULE_VECTOR \
995 reschedule_interrupt smp_reschedule_interrupt
998 apicinterrupt ERROR_APIC_VECTOR \
999 error_interrupt smp_error_interrupt
1000 apicinterrupt SPURIOUS_APIC_VECTOR \
1001 spurious_interrupt smp_spurious_interrupt
1003 #ifdef CONFIG_IRQ_WORK
1004 apicinterrupt IRQ_WORK_VECTOR \
1005 irq_work_interrupt smp_irq_work_interrupt
1009 * Exception entry points.
1011 .macro zeroentry sym do_sym
1014 PARAVIRT_ADJUST_EXCEPTION_FRAME
1015 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1016 subq $ORIG_RAX-R15, %rsp
1017 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1020 movq %rsp,%rdi /* pt_regs pointer */
1021 xorl %esi,%esi /* no error code */
1023 jmp error_exit /* %ebx: no swapgs flag */
1028 .macro paranoidzeroentry sym do_sym
1031 PARAVIRT_ADJUST_EXCEPTION_FRAME
1032 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1033 subq $ORIG_RAX-R15, %rsp
1034 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1037 movq %rsp,%rdi /* pt_regs pointer */
1038 xorl %esi,%esi /* no error code */
1040 jmp paranoid_exit /* %ebx: no swapgs flag */
1045 #define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
1046 .macro paranoidzeroentry_ist sym do_sym ist
1049 PARAVIRT_ADJUST_EXCEPTION_FRAME
1050 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1051 subq $ORIG_RAX-R15, %rsp
1052 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1055 movq %rsp,%rdi /* pt_regs pointer */
1056 xorl %esi,%esi /* no error code */
1057 subq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1059 addq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1060 jmp paranoid_exit /* %ebx: no swapgs flag */
1065 .macro errorentry sym do_sym
1068 PARAVIRT_ADJUST_EXCEPTION_FRAME
1069 subq $ORIG_RAX-R15, %rsp
1070 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1073 movq %rsp,%rdi /* pt_regs pointer */
1074 movq ORIG_RAX(%rsp),%rsi /* get error code */
1075 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1077 jmp error_exit /* %ebx: no swapgs flag */
1082 /* error code is on the stack already */
1083 .macro paranoiderrorentry sym do_sym
1086 PARAVIRT_ADJUST_EXCEPTION_FRAME
1087 subq $ORIG_RAX-R15, %rsp
1088 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1092 movq %rsp,%rdi /* pt_regs pointer */
1093 movq ORIG_RAX(%rsp),%rsi /* get error code */
1094 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1096 jmp paranoid_exit /* %ebx: no swapgs flag */
1101 zeroentry divide_error do_divide_error
1102 zeroentry overflow do_overflow
1103 zeroentry bounds do_bounds
1104 zeroentry invalid_op do_invalid_op
1105 zeroentry device_not_available do_device_not_available
1106 paranoiderrorentry double_fault do_double_fault
1107 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1108 errorentry invalid_TSS do_invalid_TSS
1109 errorentry segment_not_present do_segment_not_present
1110 zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1111 zeroentry coprocessor_error do_coprocessor_error
1112 errorentry alignment_check do_alignment_check
1113 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1115 /* Reload gs selector with exception handling */
1116 /* edi: new selector */
1117 ENTRY(native_load_gs_index)
1120 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1124 2: mfence /* workaround */
1129 END(native_load_gs_index)
1131 .section __ex_table,"a"
1133 .quad gs_change,bad_gs
1135 .section .fixup,"ax"
1136 /* running with kernelgs */
1138 SWAPGS /* switch back to user gs */
1144 ENTRY(kernel_thread_helper)
1145 pushq $0 # fake return address
1148 * Here we are in the child and the registers are set as they were
1149 * at kernel_thread() invocation in the parent.
1155 ud2 # padding for call trace
1157 END(kernel_thread_helper)
1160 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1162 * C extern interface:
1163 * extern long execve(const char *name, char **argv, char **envp)
1165 * asm input arguments:
1166 * rdi: name, rsi: argv, rdx: envp
1168 * We want to fallback into:
1169 * extern long sys_execve(const char *name, char **argv,char **envp, struct pt_regs *regs)
1171 * do_sys_execve asm fallback arguments:
1172 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1174 ENTRY(kernel_execve)
1180 movq %rax, RAX(%rsp)
1183 je int_ret_from_sys_call
1190 /* Call softirq on interrupt stack. Interrupts are off. */
1194 CFI_REL_OFFSET rbp,0
1196 CFI_DEF_CFA_REGISTER rbp
1197 incl PER_CPU_VAR(irq_count)
1198 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1199 push %rbp # backlink for old unwinder
1203 CFI_DEF_CFA_REGISTER rsp
1204 CFI_ADJUST_CFA_OFFSET -8
1205 decl PER_CPU_VAR(irq_count)
1211 zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
1214 * A note on the "critical region" in our callback handler.
1215 * We want to avoid stacking callback handlers due to events occurring
1216 * during handling of the last event. To do this, we keep events disabled
1217 * until we've done all processing. HOWEVER, we must enable events before
1218 * popping the stack frame (can't be done atomically) and so it would still
1219 * be possible to get enough handler activations to overflow the stack.
1220 * Although unlikely, bugs of that kind are hard to track down, so we'd
1221 * like to avoid the possibility.
1222 * So, on entry to the handler we detect whether we interrupted an
1223 * existing activation in its critical region -- if so, we pop the current
1224 * activation and restart the handler using the previous one.
1226 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1229 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1230 * see the correct pointer to the pt_regs
1232 movq %rdi, %rsp # we don't return, adjust the stack frame
1235 11: incl PER_CPU_VAR(irq_count)
1237 CFI_DEF_CFA_REGISTER rbp
1238 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1239 pushq %rbp # backlink for old unwinder
1240 call xen_evtchn_do_upcall
1242 CFI_DEF_CFA_REGISTER rsp
1243 decl PER_CPU_VAR(irq_count)
1246 END(xen_do_hypervisor_callback)
1249 * Hypervisor uses this for application faults while it executes.
1250 * We get here for two reasons:
1251 * 1. Fault while reloading DS, ES, FS or GS
1252 * 2. Fault while executing IRET
1253 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1254 * registers that could be reloaded and zeroed the others.
1255 * Category 2 we fix up by killing the current process. We cannot use the
1256 * normal Linux return path in this case because if we use the IRET hypercall
1257 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1258 * We distinguish between categories by comparing each saved segment register
1259 * with its current contents: any discrepancy means we in category 1.
1261 ENTRY(xen_failsafe_callback)
1263 /*CFI_REL_OFFSET gs,GS*/
1264 /*CFI_REL_OFFSET fs,FS*/
1265 /*CFI_REL_OFFSET es,ES*/
1266 /*CFI_REL_OFFSET ds,DS*/
1267 CFI_REL_OFFSET r11,8
1268 CFI_REL_OFFSET rcx,0
1282 /* All segments match their saved values => Category 2 (Bad IRET). */
1288 CFI_ADJUST_CFA_OFFSET -0x30
1289 pushq_cfi $0 /* RIP */
1292 jmp general_protection
1294 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1300 CFI_ADJUST_CFA_OFFSET -0x30
1305 END(xen_failsafe_callback)
1307 apicinterrupt XEN_HVM_EVTCHN_CALLBACK \
1308 xen_hvm_callback_vector xen_evtchn_do_upcall
1310 #endif /* CONFIG_XEN */
1313 * Some functions should be protected against kprobes
1315 .pushsection .kprobes.text, "ax"
1317 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1318 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1319 paranoiderrorentry stack_segment do_stack_segment
1321 zeroentry xen_debug do_debug
1322 zeroentry xen_int3 do_int3
1323 errorentry xen_stack_segment do_stack_segment
1325 errorentry general_protection do_general_protection
1326 errorentry page_fault do_page_fault
1327 #ifdef CONFIG_KVM_GUEST
1328 errorentry async_page_fault do_async_page_fault
1330 #ifdef CONFIG_X86_MCE
1331 paranoidzeroentry machine_check *machine_check_vector(%rip)
1335 * "Paranoid" exit path from exception stack.
1336 * Paranoid because this is used by NMIs and cannot take
1337 * any kernel state for granted.
1338 * We don't do kernel preemption checks here, because only
1339 * NMI should be common and it does not enable IRQs and
1340 * cannot get reschedule ticks.
1342 * "trace" is 0 for the NMI handler only, because irq-tracing
1343 * is fundamentally NMI-unsafe. (we cannot change the soft and
1344 * hard flags at once, atomically)
1347 /* ebx: no swapgs flag */
1348 ENTRY(paranoid_exit)
1350 DISABLE_INTERRUPTS(CLBR_NONE)
1352 testl %ebx,%ebx /* swapgs needed? */
1353 jnz paranoid_restore
1355 jnz paranoid_userspace
1366 GET_THREAD_INFO(%rcx)
1367 movl TI_flags(%rcx),%ebx
1368 andl $_TIF_WORK_MASK,%ebx
1370 movq %rsp,%rdi /* &pt_regs */
1372 movq %rax,%rsp /* switch stack for scheduling */
1373 testl $_TIF_NEED_RESCHED,%ebx
1374 jnz paranoid_schedule
1375 movl %ebx,%edx /* arg3: thread flags */
1377 ENABLE_INTERRUPTS(CLBR_NONE)
1378 xorl %esi,%esi /* arg2: oldset */
1379 movq %rsp,%rdi /* arg1: &pt_regs */
1380 call do_notify_resume
1381 DISABLE_INTERRUPTS(CLBR_NONE)
1383 jmp paranoid_userspace
1386 ENABLE_INTERRUPTS(CLBR_ANY)
1388 DISABLE_INTERRUPTS(CLBR_ANY)
1390 jmp paranoid_userspace
1395 * Exception entry point. This expects an error code/orig_rax on the stack.
1396 * returns in "no swapgs flag" in %ebx.
1400 CFI_ADJUST_CFA_OFFSET 15*8
1401 /* oldrax contains error code */
1420 je error_kernelspace
1428 * There are two places in the kernel that can potentially fault with
1429 * usergs. Handle them here. The exception handlers after iret run with
1430 * kernel gs again, so don't set the user space flag. B stepping K8s
1431 * sometimes report an truncated RIP for IRET exceptions returning to
1432 * compat mode. Check for these here too.
1436 leaq irq_return(%rip),%rcx
1437 cmpq %rcx,RIP+8(%rsp)
1439 movl %ecx,%eax /* zero extend */
1440 cmpq %rax,RIP+8(%rsp)
1442 cmpq $gs_change,RIP+8(%rsp)
1447 /* Fix truncated RIP */
1448 movq %rcx,RIP+8(%rsp)
1454 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1459 DISABLE_INTERRUPTS(CLBR_NONE)
1461 GET_THREAD_INFO(%rcx)
1464 LOCKDEP_SYS_EXIT_IRQ
1465 movl TI_flags(%rcx),%edx
1466 movl $_TIF_WORK_MASK,%edi
1474 /* runs on exception stack */
1477 PARAVIRT_ADJUST_EXCEPTION_FRAME
1479 subq $ORIG_RAX-R15, %rsp
1480 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1483 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1487 #ifdef CONFIG_TRACE_IRQFLAGS
1488 /* paranoidexit; without TRACE_IRQS_OFF */
1489 /* ebx: no swapgs flag */
1490 DISABLE_INTERRUPTS(CLBR_NONE)
1491 testl %ebx,%ebx /* swapgs needed? */
1501 GET_THREAD_INFO(%rcx)
1502 movl TI_flags(%rcx),%ebx
1503 andl $_TIF_WORK_MASK,%ebx
1505 movq %rsp,%rdi /* &pt_regs */
1507 movq %rax,%rsp /* switch stack for scheduling */
1508 testl $_TIF_NEED_RESCHED,%ebx
1510 movl %ebx,%edx /* arg3: thread flags */
1511 ENABLE_INTERRUPTS(CLBR_NONE)
1512 xorl %esi,%esi /* arg2: oldset */
1513 movq %rsp,%rdi /* arg1: &pt_regs */
1514 call do_notify_resume
1515 DISABLE_INTERRUPTS(CLBR_NONE)
1518 ENABLE_INTERRUPTS(CLBR_ANY)
1520 DISABLE_INTERRUPTS(CLBR_ANY)
1529 ENTRY(ignore_sysret)
1537 * End of kprobes section