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
330 pushq_cfi %rbp /* backlink for unwinder */
332 * We entered an interrupt context - irqs are off:
338 PARTIAL_FRAME 1 REST_SKIP+8
339 movq 5*8+16(%rsp), %r11 /* save return address */
346 movq %r11, 8(%rsp) /* return address */
347 FIXUP_TOP_OF_STACK %r11, 16
352 /* save complete stack frame */
353 .pushsection .kprobes.text, "ax"
373 movl $MSR_GS_BASE,%ecx
376 js 1f /* negative -> in kernel */
385 * A newly forked process directly context switches into this address.
387 * rdi: prev task we switched from
392 LOCK ; btr $TIF_FORK,TI_flags(%r8)
394 pushq_cfi kernel_eflags(%rip)
395 popfq_cfi # reset kernel eflags
397 call schedule_tail # rdi: 'prev' task parameter
399 GET_THREAD_INFO(%rcx)
403 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
404 je int_ret_from_sys_call
406 testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
407 jnz int_ret_from_sys_call
409 RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
410 jmp ret_from_sys_call # go to the SYSRET fastpath
416 * System call entry. Up to 6 arguments in registers are supported.
418 * SYSCALL does not save anything on the stack and does not change the
424 * rax system call number
426 * rcx return address for syscall/sysret, C arg3
429 * r10 arg3 (--> moved to rcx for C)
432 * r11 eflags for syscall/sysret, temporary for C
433 * r12-r15,rbp,rbx saved by C code, not touched.
435 * Interrupts are off on entry.
436 * Only called from user space.
438 * XXX if we had a free scratch register we could save the RSP into the stack frame
439 * and report it properly in ps. Unfortunately we haven't.
441 * When user can change the frames always force IRET. That is because
442 * it deals with uncanonical addresses better. SYSRET has trouble
443 * with them due to bugs in both AMD and Intel CPUs.
449 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
451 /*CFI_REGISTER rflags,r11*/
454 * A hypervisor implementation might want to use a label
455 * after the swapgs, so that it can do the swapgs
456 * for the guest and jump here on syscall.
458 ENTRY(system_call_after_swapgs)
460 movq %rsp,PER_CPU_VAR(old_rsp)
461 movq PER_CPU_VAR(kernel_stack),%rsp
463 * No need to follow this irqs off/on section - it's straight
466 ENABLE_INTERRUPTS(CLBR_NONE)
468 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
469 movq %rcx,RIP-ARGOFFSET(%rsp)
470 CFI_REL_OFFSET rip,RIP-ARGOFFSET
471 GET_THREAD_INFO(%rcx)
472 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
474 system_call_fastpath:
475 cmpq $__NR_syscall_max,%rax
478 call *sys_call_table(,%rax,8) # XXX: rip relative
479 movq %rax,RAX-ARGOFFSET(%rsp)
481 * Syscall return path ending with SYSRET (fast path)
482 * Has incomplete stack frame and undefined top of stack.
485 movl $_TIF_ALLWORK_MASK,%edi
489 GET_THREAD_INFO(%rcx)
490 DISABLE_INTERRUPTS(CLBR_NONE)
492 movl TI_flags(%rcx),%edx
497 * sysretq will re-enable interrupts:
500 movq RIP-ARGOFFSET(%rsp),%rcx
502 RESTORE_ARGS 0,-ARG_SKIP,1
503 /*CFI_REGISTER rflags,r11*/
504 movq PER_CPU_VAR(old_rsp), %rsp
508 /* Handle reschedules */
509 /* edx: work, edi: workmask */
511 bt $TIF_NEED_RESCHED,%edx
514 ENABLE_INTERRUPTS(CLBR_NONE)
520 /* Handle a signal */
523 ENABLE_INTERRUPTS(CLBR_NONE)
524 #ifdef CONFIG_AUDITSYSCALL
525 bt $TIF_SYSCALL_AUDIT,%edx
529 * We have a signal, or exit tracing or single-step.
530 * These all wind up with the iret return path anyway,
531 * so just join that path right now.
533 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
534 jmp int_check_syscall_exit_work
537 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
538 jmp ret_from_sys_call
540 #ifdef CONFIG_AUDITSYSCALL
542 * Fast path for syscall audit without full syscall trace.
543 * We just call audit_syscall_entry() directly, and then
544 * jump back to the normal fast path.
547 movq %r10,%r9 /* 6th arg: 4th syscall arg */
548 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
549 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
550 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
551 movq %rax,%rsi /* 2nd arg: syscall number */
552 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
553 call audit_syscall_entry
554 LOAD_ARGS 0 /* reload call-clobbered registers */
555 jmp system_call_fastpath
558 * Return fast path for syscall audit. Call audit_syscall_exit()
559 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
563 movq RAX-ARGOFFSET(%rsp),%rsi /* second arg, syscall return value */
564 cmpq $0,%rsi /* is it < 0? */
565 setl %al /* 1 if so, 0 if not */
566 movzbl %al,%edi /* zero-extend that into %edi */
567 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
568 call audit_syscall_exit
569 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
571 #endif /* CONFIG_AUDITSYSCALL */
573 /* Do syscall tracing */
575 #ifdef CONFIG_AUDITSYSCALL
576 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
580 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
581 FIXUP_TOP_OF_STACK %rdi
583 call syscall_trace_enter
585 * Reload arg registers from stack in case ptrace changed them.
586 * We don't reload %rax because syscall_trace_enter() returned
587 * the value it wants us to use in the table lookup.
589 LOAD_ARGS ARGOFFSET, 1
591 cmpq $__NR_syscall_max,%rax
592 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
593 movq %r10,%rcx /* fixup for C */
594 call *sys_call_table(,%rax,8)
595 movq %rax,RAX-ARGOFFSET(%rsp)
596 /* Use IRET because user could have changed frame */
599 * Syscall return path ending with IRET.
600 * Has correct top of stack, but partial stack frame.
602 GLOBAL(int_ret_from_sys_call)
603 DISABLE_INTERRUPTS(CLBR_NONE)
605 testl $3,CS-ARGOFFSET(%rsp)
606 je retint_restore_args
607 movl $_TIF_ALLWORK_MASK,%edi
608 /* edi: mask to check */
609 GLOBAL(int_with_check)
611 GET_THREAD_INFO(%rcx)
612 movl TI_flags(%rcx),%edx
615 andl $~TS_COMPAT,TI_status(%rcx)
618 /* Either reschedule or signal or syscall exit tracking needed. */
619 /* First do a reschedule test. */
620 /* edx: work, edi: workmask */
622 bt $TIF_NEED_RESCHED,%edx
625 ENABLE_INTERRUPTS(CLBR_NONE)
629 DISABLE_INTERRUPTS(CLBR_NONE)
633 /* handle signals and tracing -- both require a full stack frame */
636 ENABLE_INTERRUPTS(CLBR_NONE)
637 int_check_syscall_exit_work:
639 /* Check for syscall exit trace */
640 testl $_TIF_WORK_SYSCALL_EXIT,%edx
643 leaq 8(%rsp),%rdi # &ptregs -> arg1
644 call syscall_trace_leave
646 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
650 testl $_TIF_DO_NOTIFY_MASK,%edx
652 movq %rsp,%rdi # &ptregs -> arg1
653 xorl %esi,%esi # oldset -> arg2
654 call do_notify_resume
655 1: movl $_TIF_WORK_MASK,%edi
658 DISABLE_INTERRUPTS(CLBR_NONE)
665 * Certain special system calls that need to save a complete full stack frame.
667 .macro PTREGSCALL label,func,arg
669 PARTIAL_FRAME 1 8 /* offset 8: return address */
670 subq $REST_SKIP, %rsp
671 CFI_ADJUST_CFA_OFFSET REST_SKIP
673 DEFAULT_FRAME 0 8 /* offset 8: return address */
674 leaq 8(%rsp), \arg /* pt_regs pointer */
676 jmp ptregscall_common
681 PTREGSCALL stub_clone, sys_clone, %r8
682 PTREGSCALL stub_fork, sys_fork, %rdi
683 PTREGSCALL stub_vfork, sys_vfork, %rdi
684 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
685 PTREGSCALL stub_iopl, sys_iopl, %rsi
687 ENTRY(ptregscall_common)
688 DEFAULT_FRAME 1 8 /* offset 8: return address */
689 RESTORE_TOP_OF_STACK %r11, 8
690 movq_cfi_restore R15+8, r15
691 movq_cfi_restore R14+8, r14
692 movq_cfi_restore R13+8, r13
693 movq_cfi_restore R12+8, r12
694 movq_cfi_restore RBP+8, rbp
695 movq_cfi_restore RBX+8, rbx
696 ret $REST_SKIP /* pop extended registers */
698 END(ptregscall_common)
705 FIXUP_TOP_OF_STACK %r11
708 RESTORE_TOP_OF_STACK %r11
711 jmp int_ret_from_sys_call
716 * sigreturn is special because it needs to restore all registers on return.
717 * This cannot be done with SYSRET, so use the IRET return path instead.
719 ENTRY(stub_rt_sigreturn)
725 FIXUP_TOP_OF_STACK %r11
726 call sys_rt_sigreturn
727 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
729 jmp int_ret_from_sys_call
731 END(stub_rt_sigreturn)
734 * Build the entry stubs and pointer table with some assembler magic.
735 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
736 * single cache line on all modern x86 implementations.
738 .section .init.rodata,"a"
742 .p2align CONFIG_X86_L1_CACHE_SHIFT
743 ENTRY(irq_entries_start)
745 vector=FIRST_EXTERNAL_VECTOR
746 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
749 .if vector < NR_VECTORS
750 .if vector <> FIRST_EXTERNAL_VECTOR
751 CFI_ADJUST_CFA_OFFSET -8
753 1: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
754 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
763 2: jmp common_interrupt
766 END(irq_entries_start)
773 * Interrupt entry/exit.
775 * Interrupt entry points save only callee clobbered registers in fast path.
777 * Entry runs with interrupts off.
780 /* 0(%rsp): ~(interrupt number) */
781 .macro interrupt func
782 /* reserve pt_regs for scratch regs and rbp */
783 subq $ORIG_RAX-RBP, %rsp
784 CFI_ADJUST_CFA_OFFSET ORIG_RAX-RBP
791 * Interrupt entry/exit should be protected against kprobes
793 .pushsection .kprobes.text, "ax"
795 * The interrupt stubs push (~vector+0x80) onto the stack and
796 * then jump to common_interrupt.
798 .p2align CONFIG_X86_L1_CACHE_SHIFT
801 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
803 /* 0(%rsp): old_rsp-ARGOFFSET */
805 DISABLE_INTERRUPTS(CLBR_NONE)
807 decl PER_CPU_VAR(irq_count)
811 CFI_DEF_CFA_REGISTER rsp
812 CFI_ADJUST_CFA_OFFSET -8
814 /* we did not save rbx, restore only from ARGOFFSET */
816 CFI_ADJUST_CFA_OFFSET -8
818 GET_THREAD_INFO(%rcx)
819 testl $3,CS-ARGOFFSET(%rsp)
822 /* Interrupt came from user space */
824 * Has a correct top of stack, but a partial stack frame
825 * %rcx: thread info. Interrupts off.
827 retint_with_reschedule:
828 movl $_TIF_WORK_MASK,%edi
831 movl TI_flags(%rcx),%edx
836 retint_swapgs: /* return to user-space */
838 * The iretq could re-enable interrupts:
840 DISABLE_INTERRUPTS(CLBR_ANY)
845 retint_restore_args: /* return to kernel space */
846 DISABLE_INTERRUPTS(CLBR_ANY)
848 * The iretq could re-enable interrupts:
857 .section __ex_table, "a"
858 .quad irq_return, bad_iret
861 #ifdef CONFIG_PARAVIRT
865 .section __ex_table,"a"
866 .quad native_iret, bad_iret
873 * The iret traps when the %cs or %ss being restored is bogus.
874 * We've lost the original trap vector and error code.
875 * #GPF is the most likely one to get for an invalid selector.
876 * So pretend we completed the iret and took the #GPF in user mode.
878 * We are now running with the kernel GS after exception recovery.
879 * But error_entry expects us to have user GS to match the user %cs,
885 jmp general_protection
889 /* edi: workmask, edx: work */
892 bt $TIF_NEED_RESCHED,%edx
895 ENABLE_INTERRUPTS(CLBR_NONE)
899 GET_THREAD_INFO(%rcx)
900 DISABLE_INTERRUPTS(CLBR_NONE)
905 testl $_TIF_DO_NOTIFY_MASK,%edx
908 ENABLE_INTERRUPTS(CLBR_NONE)
910 movq $-1,ORIG_RAX(%rsp)
911 xorl %esi,%esi # oldset
912 movq %rsp,%rdi # &pt_regs
913 call do_notify_resume
915 DISABLE_INTERRUPTS(CLBR_NONE)
917 GET_THREAD_INFO(%rcx)
918 jmp retint_with_reschedule
920 #ifdef CONFIG_PREEMPT
921 /* Returning to kernel space. Check if we need preemption */
922 /* rcx: threadinfo. interrupts off. */
924 cmpl $0,TI_preempt_count(%rcx)
925 jnz retint_restore_args
926 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
927 jnc retint_restore_args
928 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
929 jnc retint_restore_args
930 call preempt_schedule_irq
935 END(common_interrupt)
937 * End of kprobes section
944 .macro apicinterrupt num sym do_sym
955 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
956 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
957 apicinterrupt REBOOT_VECTOR \
958 reboot_interrupt smp_reboot_interrupt
962 apicinterrupt UV_BAU_MESSAGE \
963 uv_bau_message_intr1 uv_bau_message_interrupt
965 apicinterrupt LOCAL_TIMER_VECTOR \
966 apic_timer_interrupt smp_apic_timer_interrupt
967 apicinterrupt X86_PLATFORM_IPI_VECTOR \
968 x86_platform_ipi smp_x86_platform_ipi
971 .irp idx,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, \
972 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
973 .if NUM_INVALIDATE_TLB_VECTORS > \idx
974 apicinterrupt (INVALIDATE_TLB_VECTOR_START)+\idx \
975 invalidate_interrupt\idx smp_invalidate_interrupt
980 apicinterrupt THRESHOLD_APIC_VECTOR \
981 threshold_interrupt smp_threshold_interrupt
982 apicinterrupt THERMAL_APIC_VECTOR \
983 thermal_interrupt smp_thermal_interrupt
985 #ifdef CONFIG_X86_MCE
986 apicinterrupt MCE_SELF_VECTOR \
987 mce_self_interrupt smp_mce_self_interrupt
991 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
992 call_function_single_interrupt smp_call_function_single_interrupt
993 apicinterrupt CALL_FUNCTION_VECTOR \
994 call_function_interrupt smp_call_function_interrupt
995 apicinterrupt RESCHEDULE_VECTOR \
996 reschedule_interrupt smp_reschedule_interrupt
999 apicinterrupt ERROR_APIC_VECTOR \
1000 error_interrupt smp_error_interrupt
1001 apicinterrupt SPURIOUS_APIC_VECTOR \
1002 spurious_interrupt smp_spurious_interrupt
1004 #ifdef CONFIG_IRQ_WORK
1005 apicinterrupt IRQ_WORK_VECTOR \
1006 irq_work_interrupt smp_irq_work_interrupt
1010 * Exception entry points.
1012 .macro zeroentry sym do_sym
1015 PARAVIRT_ADJUST_EXCEPTION_FRAME
1016 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1017 subq $ORIG_RAX-R15, %rsp
1018 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1021 movq %rsp,%rdi /* pt_regs pointer */
1022 xorl %esi,%esi /* no error code */
1024 jmp error_exit /* %ebx: no swapgs flag */
1029 .macro paranoidzeroentry sym do_sym
1032 PARAVIRT_ADJUST_EXCEPTION_FRAME
1033 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1034 subq $ORIG_RAX-R15, %rsp
1035 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1038 movq %rsp,%rdi /* pt_regs pointer */
1039 xorl %esi,%esi /* no error code */
1041 jmp paranoid_exit /* %ebx: no swapgs flag */
1046 #define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
1047 .macro paranoidzeroentry_ist sym do_sym ist
1050 PARAVIRT_ADJUST_EXCEPTION_FRAME
1051 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1052 subq $ORIG_RAX-R15, %rsp
1053 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1056 movq %rsp,%rdi /* pt_regs pointer */
1057 xorl %esi,%esi /* no error code */
1058 subq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1060 addq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1061 jmp paranoid_exit /* %ebx: no swapgs flag */
1066 .macro errorentry sym do_sym
1069 PARAVIRT_ADJUST_EXCEPTION_FRAME
1070 subq $ORIG_RAX-R15, %rsp
1071 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1074 movq %rsp,%rdi /* pt_regs pointer */
1075 movq ORIG_RAX(%rsp),%rsi /* get error code */
1076 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1078 jmp error_exit /* %ebx: no swapgs flag */
1083 /* error code is on the stack already */
1084 .macro paranoiderrorentry sym do_sym
1087 PARAVIRT_ADJUST_EXCEPTION_FRAME
1088 subq $ORIG_RAX-R15, %rsp
1089 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1093 movq %rsp,%rdi /* pt_regs pointer */
1094 movq ORIG_RAX(%rsp),%rsi /* get error code */
1095 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1097 jmp paranoid_exit /* %ebx: no swapgs flag */
1102 zeroentry divide_error do_divide_error
1103 zeroentry overflow do_overflow
1104 zeroentry bounds do_bounds
1105 zeroentry invalid_op do_invalid_op
1106 zeroentry device_not_available do_device_not_available
1107 paranoiderrorentry double_fault do_double_fault
1108 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1109 errorentry invalid_TSS do_invalid_TSS
1110 errorentry segment_not_present do_segment_not_present
1111 zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1112 zeroentry coprocessor_error do_coprocessor_error
1113 errorentry alignment_check do_alignment_check
1114 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1116 /* Reload gs selector with exception handling */
1117 /* edi: new selector */
1118 ENTRY(native_load_gs_index)
1121 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1125 2: mfence /* workaround */
1130 END(native_load_gs_index)
1132 .section __ex_table,"a"
1134 .quad gs_change,bad_gs
1136 .section .fixup,"ax"
1137 /* running with kernelgs */
1139 SWAPGS /* switch back to user gs */
1145 ENTRY(kernel_thread_helper)
1146 pushq $0 # fake return address
1149 * Here we are in the child and the registers are set as they were
1150 * at kernel_thread() invocation in the parent.
1156 ud2 # padding for call trace
1158 END(kernel_thread_helper)
1161 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1163 * C extern interface:
1164 * extern long execve(const char *name, char **argv, char **envp)
1166 * asm input arguments:
1167 * rdi: name, rsi: argv, rdx: envp
1169 * We want to fallback into:
1170 * extern long sys_execve(const char *name, char **argv,char **envp, struct pt_regs *regs)
1172 * do_sys_execve asm fallback arguments:
1173 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1175 ENTRY(kernel_execve)
1181 movq %rax, RAX(%rsp)
1184 je int_ret_from_sys_call
1191 /* Call softirq on interrupt stack. Interrupts are off. */
1195 CFI_REL_OFFSET rbp,0
1197 CFI_DEF_CFA_REGISTER rbp
1198 incl PER_CPU_VAR(irq_count)
1199 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1200 push %rbp # backlink for old unwinder
1204 CFI_DEF_CFA_REGISTER rsp
1205 CFI_ADJUST_CFA_OFFSET -8
1206 decl PER_CPU_VAR(irq_count)
1212 zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
1215 * A note on the "critical region" in our callback handler.
1216 * We want to avoid stacking callback handlers due to events occurring
1217 * during handling of the last event. To do this, we keep events disabled
1218 * until we've done all processing. HOWEVER, we must enable events before
1219 * popping the stack frame (can't be done atomically) and so it would still
1220 * be possible to get enough handler activations to overflow the stack.
1221 * Although unlikely, bugs of that kind are hard to track down, so we'd
1222 * like to avoid the possibility.
1223 * So, on entry to the handler we detect whether we interrupted an
1224 * existing activation in its critical region -- if so, we pop the current
1225 * activation and restart the handler using the previous one.
1227 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1230 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1231 * see the correct pointer to the pt_regs
1233 movq %rdi, %rsp # we don't return, adjust the stack frame
1236 11: incl PER_CPU_VAR(irq_count)
1238 CFI_DEF_CFA_REGISTER rbp
1239 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1240 pushq %rbp # backlink for old unwinder
1241 call xen_evtchn_do_upcall
1243 CFI_DEF_CFA_REGISTER rsp
1244 decl PER_CPU_VAR(irq_count)
1247 END(xen_do_hypervisor_callback)
1250 * Hypervisor uses this for application faults while it executes.
1251 * We get here for two reasons:
1252 * 1. Fault while reloading DS, ES, FS or GS
1253 * 2. Fault while executing IRET
1254 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1255 * registers that could be reloaded and zeroed the others.
1256 * Category 2 we fix up by killing the current process. We cannot use the
1257 * normal Linux return path in this case because if we use the IRET hypercall
1258 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1259 * We distinguish between categories by comparing each saved segment register
1260 * with its current contents: any discrepancy means we in category 1.
1262 ENTRY(xen_failsafe_callback)
1264 /*CFI_REL_OFFSET gs,GS*/
1265 /*CFI_REL_OFFSET fs,FS*/
1266 /*CFI_REL_OFFSET es,ES*/
1267 /*CFI_REL_OFFSET ds,DS*/
1268 CFI_REL_OFFSET r11,8
1269 CFI_REL_OFFSET rcx,0
1283 /* All segments match their saved values => Category 2 (Bad IRET). */
1289 CFI_ADJUST_CFA_OFFSET -0x30
1290 pushq_cfi $0 /* RIP */
1293 jmp general_protection
1295 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1301 CFI_ADJUST_CFA_OFFSET -0x30
1306 END(xen_failsafe_callback)
1308 apicinterrupt XEN_HVM_EVTCHN_CALLBACK \
1309 xen_hvm_callback_vector xen_evtchn_do_upcall
1311 #endif /* CONFIG_XEN */
1314 * Some functions should be protected against kprobes
1316 .pushsection .kprobes.text, "ax"
1318 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1319 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1320 paranoiderrorentry stack_segment do_stack_segment
1322 zeroentry xen_debug do_debug
1323 zeroentry xen_int3 do_int3
1324 errorentry xen_stack_segment do_stack_segment
1326 errorentry general_protection do_general_protection
1327 errorentry page_fault do_page_fault
1328 #ifdef CONFIG_KVM_GUEST
1329 errorentry async_page_fault do_async_page_fault
1331 #ifdef CONFIG_X86_MCE
1332 paranoidzeroentry machine_check *machine_check_vector(%rip)
1336 * "Paranoid" exit path from exception stack.
1337 * Paranoid because this is used by NMIs and cannot take
1338 * any kernel state for granted.
1339 * We don't do kernel preemption checks here, because only
1340 * NMI should be common and it does not enable IRQs and
1341 * cannot get reschedule ticks.
1343 * "trace" is 0 for the NMI handler only, because irq-tracing
1344 * is fundamentally NMI-unsafe. (we cannot change the soft and
1345 * hard flags at once, atomically)
1348 /* ebx: no swapgs flag */
1349 ENTRY(paranoid_exit)
1351 DISABLE_INTERRUPTS(CLBR_NONE)
1353 testl %ebx,%ebx /* swapgs needed? */
1354 jnz paranoid_restore
1356 jnz paranoid_userspace
1367 GET_THREAD_INFO(%rcx)
1368 movl TI_flags(%rcx),%ebx
1369 andl $_TIF_WORK_MASK,%ebx
1371 movq %rsp,%rdi /* &pt_regs */
1373 movq %rax,%rsp /* switch stack for scheduling */
1374 testl $_TIF_NEED_RESCHED,%ebx
1375 jnz paranoid_schedule
1376 movl %ebx,%edx /* arg3: thread flags */
1378 ENABLE_INTERRUPTS(CLBR_NONE)
1379 xorl %esi,%esi /* arg2: oldset */
1380 movq %rsp,%rdi /* arg1: &pt_regs */
1381 call do_notify_resume
1382 DISABLE_INTERRUPTS(CLBR_NONE)
1384 jmp paranoid_userspace
1387 ENABLE_INTERRUPTS(CLBR_ANY)
1389 DISABLE_INTERRUPTS(CLBR_ANY)
1391 jmp paranoid_userspace
1396 * Exception entry point. This expects an error code/orig_rax on the stack.
1397 * returns in "no swapgs flag" in %ebx.
1401 CFI_ADJUST_CFA_OFFSET 15*8
1402 /* oldrax contains error code */
1421 je error_kernelspace
1429 * There are two places in the kernel that can potentially fault with
1430 * usergs. Handle them here. The exception handlers after iret run with
1431 * kernel gs again, so don't set the user space flag. B stepping K8s
1432 * sometimes report an truncated RIP for IRET exceptions returning to
1433 * compat mode. Check for these here too.
1437 leaq irq_return(%rip),%rcx
1438 cmpq %rcx,RIP+8(%rsp)
1440 movl %ecx,%eax /* zero extend */
1441 cmpq %rax,RIP+8(%rsp)
1443 cmpq $gs_change,RIP+8(%rsp)
1448 /* Fix truncated RIP */
1449 movq %rcx,RIP+8(%rsp)
1455 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1460 DISABLE_INTERRUPTS(CLBR_NONE)
1462 GET_THREAD_INFO(%rcx)
1465 LOCKDEP_SYS_EXIT_IRQ
1466 movl TI_flags(%rcx),%edx
1467 movl $_TIF_WORK_MASK,%edi
1475 /* runs on exception stack */
1478 PARAVIRT_ADJUST_EXCEPTION_FRAME
1480 subq $ORIG_RAX-R15, %rsp
1481 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1484 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1488 #ifdef CONFIG_TRACE_IRQFLAGS
1489 /* paranoidexit; without TRACE_IRQS_OFF */
1490 /* ebx: no swapgs flag */
1491 DISABLE_INTERRUPTS(CLBR_NONE)
1492 testl %ebx,%ebx /* swapgs needed? */
1502 GET_THREAD_INFO(%rcx)
1503 movl TI_flags(%rcx),%ebx
1504 andl $_TIF_WORK_MASK,%ebx
1506 movq %rsp,%rdi /* &pt_regs */
1508 movq %rax,%rsp /* switch stack for scheduling */
1509 testl $_TIF_NEED_RESCHED,%ebx
1511 movl %ebx,%edx /* arg3: thread flags */
1512 ENABLE_INTERRUPTS(CLBR_NONE)
1513 xorl %esi,%esi /* arg2: oldset */
1514 movq %rsp,%rdi /* arg1: &pt_regs */
1515 call do_notify_resume
1516 DISABLE_INTERRUPTS(CLBR_NONE)
1519 ENABLE_INTERRUPTS(CLBR_ANY)
1521 DISABLE_INTERRUPTS(CLBR_ANY)
1530 ENTRY(ignore_sysret)
1538 * End of kprobes section