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 * Some of this is documented in Documentation/x86/entry_64.txt
14 * NOTE: This code handles signal-recognition, which happens every time
15 * after an interrupt and after each system call.
17 * Normal syscalls and interrupts don't save a full stack frame, this is
18 * only done for syscall tracing, signals or fork/exec et.al.
20 * A note on terminology:
21 * - top of stack: Architecture defined interrupt frame from SS to RIP
22 * at the top of the kernel process stack.
23 * - partial stack frame: partially saved registers up to R11.
24 * - full stack frame: Like partial stack frame, but all register saved.
27 * - CFI macros are used to generate dwarf2 unwind information for better
28 * backtraces. They don't change any code.
29 * - SAVE_ALL/RESTORE_ALL - Save/restore all registers
30 * - SAVE_ARGS/RESTORE_ARGS - Save/restore registers that C functions modify.
31 * There are unfortunately lots of special cases where some registers
32 * not touched. The macro is a big mess that should be cleaned up.
33 * - SAVE_REST/RESTORE_REST - Handle the registers not saved by SAVE_ARGS.
34 * Gives a full stack frame.
35 * - ENTRY/END Define functions in the symbol table.
36 * - FIXUP_TOP_OF_STACK/RESTORE_TOP_OF_STACK - Fix up the hardware stack
37 * frame that is otherwise undefined after a SYSCALL
38 * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
39 * - errorentry/paranoidentry/zeroentry - Define exception entry points.
42 #include <linux/linkage.h>
43 #include <asm/segment.h>
44 #include <asm/cache.h>
45 #include <asm/errno.h>
46 #include <asm/dwarf2.h>
47 #include <asm/calling.h>
48 #include <asm/asm-offsets.h>
50 #include <asm/unistd.h>
51 #include <asm/thread_info.h>
52 #include <asm/hw_irq.h>
53 #include <asm/page_types.h>
54 #include <asm/irqflags.h>
55 #include <asm/paravirt.h>
56 #include <asm/ftrace.h>
57 #include <asm/percpu.h>
58 #include <asm/pgtable_types.h>
59 #include <asm/kaiser.h>
61 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
62 #include <linux/elf-em.h>
63 #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
64 #define __AUDIT_ARCH_64BIT 0x80000000
65 #define __AUDIT_ARCH_LE 0x40000000
68 .section .entry.text, "ax"
70 #ifdef CONFIG_FUNCTION_TRACER
71 #ifdef CONFIG_DYNAMIC_FTRACE
77 cmpl $0, function_trace_stop
84 subq $MCOUNT_INSN_SIZE, %rdi
91 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
92 GLOBAL(ftrace_graph_call)
100 #else /* ! CONFIG_DYNAMIC_FTRACE */
102 cmpl $0, function_trace_stop
105 cmpq $ftrace_stub, ftrace_trace_function
108 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
109 cmpq $ftrace_stub, ftrace_graph_return
110 jnz ftrace_graph_caller
112 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
113 jnz ftrace_graph_caller
122 movq 0x38(%rsp), %rdi
124 subq $MCOUNT_INSN_SIZE, %rdi
126 call *ftrace_trace_function
132 #endif /* CONFIG_DYNAMIC_FTRACE */
133 #endif /* CONFIG_FUNCTION_TRACER */
135 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
136 ENTRY(ftrace_graph_caller)
137 cmpl $0, function_trace_stop
143 movq 0x38(%rsp), %rsi
145 subq $MCOUNT_INSN_SIZE, %rsi
147 call prepare_ftrace_return
152 END(ftrace_graph_caller)
154 GLOBAL(return_to_handler)
157 /* Save the return values */
162 call ftrace_return_to_handler
172 #ifndef CONFIG_PREEMPT
173 #define retint_kernel retint_restore_args
176 #ifdef CONFIG_PARAVIRT
177 ENTRY(native_usergs_sysret64)
180 ENDPROC(native_usergs_sysret64)
181 #endif /* CONFIG_PARAVIRT */
184 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
185 #ifdef CONFIG_TRACE_IRQFLAGS
186 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
194 * C code is not supposed to know about undefined top of stack. Every time
195 * a C function with an pt_regs argument is called from the SYSCALL based
196 * fast path FIXUP_TOP_OF_STACK is needed.
197 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
201 /* %rsp:at FRAMEEND */
202 .macro FIXUP_TOP_OF_STACK tmp offset=0
203 movq PER_CPU_VAR(old_rsp),\tmp
204 movq \tmp,RSP+\offset(%rsp)
205 movq $__USER_DS,SS+\offset(%rsp)
206 movq $__USER_CS,CS+\offset(%rsp)
207 movq $-1,RCX+\offset(%rsp)
208 movq R11+\offset(%rsp),\tmp /* get eflags */
209 movq \tmp,EFLAGS+\offset(%rsp)
212 .macro RESTORE_TOP_OF_STACK tmp offset=0
213 movq RSP+\offset(%rsp),\tmp
214 movq \tmp,PER_CPU_VAR(old_rsp)
215 movq EFLAGS+\offset(%rsp),\tmp
216 movq \tmp,R11+\offset(%rsp)
219 .macro FAKE_STACK_FRAME child_rip
220 /* push in order ss, rsp, eflags, cs, rip */
222 pushq_cfi $__KERNEL_DS /* ss */
223 /*CFI_REL_OFFSET ss,0*/
224 pushq_cfi %rax /* rsp */
226 pushq_cfi $X86_EFLAGS_IF /* eflags - interrupts on */
227 /*CFI_REL_OFFSET rflags,0*/
228 pushq_cfi $__KERNEL_CS /* cs */
229 /*CFI_REL_OFFSET cs,0*/
230 pushq_cfi \child_rip /* rip */
232 pushq_cfi %rax /* orig rax */
235 .macro UNFAKE_STACK_FRAME
237 CFI_ADJUST_CFA_OFFSET -(6*8)
241 * initial frame state for interrupts (and exceptions without error code)
243 .macro EMPTY_FRAME start=1 offset=0
247 CFI_DEF_CFA rsp,8+\offset
249 CFI_DEF_CFA_OFFSET 8+\offset
254 * initial frame state for interrupts (and exceptions without error code)
256 .macro INTR_FRAME start=1 offset=0
257 EMPTY_FRAME \start, SS+8+\offset-RIP
258 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
259 CFI_REL_OFFSET rsp, RSP+\offset-RIP
260 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
261 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
262 CFI_REL_OFFSET rip, RIP+\offset-RIP
266 * initial frame state for exceptions with error code (and interrupts
267 * with vector already pushed)
269 .macro XCPT_FRAME start=1 offset=0
270 INTR_FRAME \start, RIP+\offset-ORIG_RAX
271 /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
275 * frame that enables calling into C.
277 .macro PARTIAL_FRAME start=1 offset=0
278 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
279 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
280 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
281 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
282 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
283 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
284 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
285 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
286 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
287 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
291 * frame that enables passing a complete pt_regs to a C function.
293 .macro DEFAULT_FRAME start=1 offset=0
294 PARTIAL_FRAME \start, R11+\offset-R15
295 CFI_REL_OFFSET rbx, RBX+\offset
296 CFI_REL_OFFSET rbp, RBP+\offset
297 CFI_REL_OFFSET r12, R12+\offset
298 CFI_REL_OFFSET r13, R13+\offset
299 CFI_REL_OFFSET r14, R14+\offset
300 CFI_REL_OFFSET r15, R15+\offset
303 /* save partial stack frame */
306 /* start from rbp in pt_regs and jump over */
307 movq_cfi rdi, RDI-RBP
308 movq_cfi rsi, RSI-RBP
309 movq_cfi rdx, RDX-RBP
310 movq_cfi rcx, RCX-RBP
311 movq_cfi rax, RAX-RBP
314 movq_cfi r10, R10-RBP
315 movq_cfi r11, R11-RBP
317 /* Save rbp so that we can unwind from get_irq_regs() */
320 /* Save previous stack value */
323 leaq -RBP(%rsp),%rdi /* arg1 for handler */
329 * irq_count is used to check if a CPU is already on an interrupt stack
330 * or not. While this is essentially redundant with preempt_count it is
331 * a little cheaper to use a separate counter in the PDA (short of
332 * moving irq_enter into assembly, which would be too much work)
334 1: incl PER_CPU_VAR(irq_count)
336 mov PER_CPU_VAR(irq_stack_ptr),%rsp
337 CFI_DEF_CFA_REGISTER rsi
339 2: /* Store previous stack value */
341 CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
342 0x77 /* DW_OP_breg7 */, 0, \
343 0x06 /* DW_OP_deref */, \
344 0x08 /* DW_OP_const1u */, SS+8-RBP, \
345 0x22 /* DW_OP_plus */
346 /* We entered an interrupt context - irqs are off: */
351 PARTIAL_FRAME 1 REST_SKIP+8
352 movq 5*8+16(%rsp), %r11 /* save return address */
359 movq %r11, 8(%rsp) /* return address */
360 FIXUP_TOP_OF_STACK %r11, 16
365 /* save complete stack frame */
366 .pushsection .kprobes.text, "ax"
368 * Return: ebx=0: needs swapgs but not SWITCH_USER_CR3 in paranoid_exit
369 * ebx=1: needs neither swapgs nor SWITCH_USER_CR3 in paranoid_exit
370 * ebx=2: needs both swapgs and SWITCH_USER_CR3 in paranoid_exit
371 * ebx=3: needs SWITCH_USER_CR3 but not swapgs in paranoid_exit
392 movl $MSR_GS_BASE,%ecx
395 js 1f /* negative -> in kernel */
401 * We might have come in between a swapgs and a SWITCH_KERNEL_CR3
402 * on entry, or between a SWITCH_USER_CR3 and a swapgs on exit.
403 * Do a conditional SWITCH_KERNEL_CR3: this could safely be done
404 * unconditionally, but we need to find out whether the reverse
405 * should be done on return (conveyed to paranoid_exit in %ebx).
408 testl $KAISER_SHADOW_PGD_OFFSET, %eax
411 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax
412 orq x86_cr3_pcid_noflush, %rax
422 * A newly forked process directly context switches into this address.
424 * rdi: prev task we switched from
429 LOCK ; btr $TIF_FORK,TI_flags(%r8)
431 pushq_cfi kernel_eflags(%rip)
432 popfq_cfi # reset kernel eflags
434 call schedule_tail # rdi: 'prev' task parameter
436 GET_THREAD_INFO(%rcx)
440 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
441 je int_ret_from_sys_call
444 * By the time we get here, we have no idea whether our pt_regs,
445 * ti flags, and ti status came from the 64-bit SYSCALL fast path,
446 * the slow path, or one of the ia32entry paths.
447 * Use int_ret_from_sys_call to return, since it can safely handle
450 jmp int_ret_from_sys_call
456 * System call entry. Up to 6 arguments in registers are supported.
458 * SYSCALL does not save anything on the stack and does not change the
464 * rax system call number
466 * rcx return address for syscall/sysret, C arg3
469 * r10 arg3 (--> moved to rcx for C)
472 * r11 eflags for syscall/sysret, temporary for C
473 * r12-r15,rbp,rbx saved by C code, not touched.
475 * Interrupts are off on entry.
476 * Only called from user space.
478 * XXX if we had a free scratch register we could save the RSP into the stack frame
479 * and report it properly in ps. Unfortunately we haven't.
481 * When user can change the frames always force IRET. That is because
482 * it deals with uncanonical addresses better. SYSRET has trouble
483 * with them due to bugs in both AMD and Intel CPUs.
489 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
491 /*CFI_REGISTER rflags,r11*/
493 SWITCH_KERNEL_CR3_NO_STACK
495 * A hypervisor implementation might want to use a label
496 * after the swapgs, so that it can do the swapgs
497 * for the guest and jump here on syscall.
499 ENTRY(system_call_after_swapgs)
501 movq %rsp,PER_CPU_VAR(old_rsp)
502 movq PER_CPU_VAR(kernel_stack),%rsp
504 * No need to follow this irqs off/on section - it's straight
507 ENABLE_INTERRUPTS(CLBR_NONE)
509 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
510 movq %rcx,RIP-ARGOFFSET(%rsp)
511 CFI_REL_OFFSET rip,RIP-ARGOFFSET
512 GET_THREAD_INFO(%rcx)
513 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
515 system_call_fastpath:
516 cmpq $__NR_syscall_max,%rax
519 call *sys_call_table(,%rax,8) # XXX: rip relative
520 movq %rax,RAX-ARGOFFSET(%rsp)
522 * Syscall return path ending with SYSRET (fast path)
523 * Has incomplete stack frame and undefined top of stack.
526 movl $_TIF_ALLWORK_MASK,%edi
530 GET_THREAD_INFO(%rcx)
531 DISABLE_INTERRUPTS(CLBR_NONE)
533 movl TI_flags(%rcx),%edx
538 * sysretq will re-enable interrupts:
541 movq RIP-ARGOFFSET(%rsp),%rcx
543 RESTORE_ARGS 1,-ARG_SKIP,0
544 /*CFI_REGISTER rflags,r11*/
546 * This opens a window where we have a user CR3, but are
547 * running in the kernel. This makes using the CS
548 * register useless for telling whether or not we need to
549 * switch CR3 in NMIs. Normal interrupts are OK because
553 movq PER_CPU_VAR(old_rsp), %rsp
557 /* Handle reschedules */
558 /* edx: work, edi: workmask */
560 bt $TIF_NEED_RESCHED,%edx
563 ENABLE_INTERRUPTS(CLBR_NONE)
569 /* Handle a signal */
572 ENABLE_INTERRUPTS(CLBR_NONE)
573 #ifdef CONFIG_AUDITSYSCALL
574 bt $TIF_SYSCALL_AUDIT,%edx
578 * We have a signal, or exit tracing or single-step.
579 * These all wind up with the iret return path anyway,
580 * so just join that path right now.
582 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
583 jmp int_check_syscall_exit_work
586 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
587 jmp ret_from_sys_call
589 #ifdef CONFIG_AUDITSYSCALL
591 * Fast path for syscall audit without full syscall trace.
592 * We just call audit_syscall_entry() directly, and then
593 * jump back to the normal fast path.
596 movq %r10,%r9 /* 6th arg: 4th syscall arg */
597 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
598 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
599 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
600 movq %rax,%rsi /* 2nd arg: syscall number */
601 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
602 call audit_syscall_entry
603 LOAD_ARGS 0 /* reload call-clobbered registers */
604 jmp system_call_fastpath
607 * Return fast path for syscall audit. Call audit_syscall_exit()
608 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
612 movq RAX-ARGOFFSET(%rsp),%rsi /* second arg, syscall return value */
613 cmpq $0,%rsi /* is it < 0? */
614 setl %al /* 1 if so, 0 if not */
615 movzbl %al,%edi /* zero-extend that into %edi */
616 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
617 call audit_syscall_exit
618 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
620 #endif /* CONFIG_AUDITSYSCALL */
622 /* Do syscall tracing */
624 #ifdef CONFIG_AUDITSYSCALL
625 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
629 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
630 FIXUP_TOP_OF_STACK %rdi
632 call syscall_trace_enter
634 * Reload arg registers from stack in case ptrace changed them.
635 * We don't reload %rax because syscall_trace_enter() returned
636 * the value it wants us to use in the table lookup.
638 LOAD_ARGS ARGOFFSET, 1
640 cmpq $__NR_syscall_max,%rax
641 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
642 movq %r10,%rcx /* fixup for C */
643 call *sys_call_table(,%rax,8)
644 movq %rax,RAX-ARGOFFSET(%rsp)
645 /* Use IRET because user could have changed frame */
648 * Syscall return path ending with IRET.
649 * Has correct top of stack, but partial stack frame.
651 GLOBAL(int_ret_from_sys_call)
652 DISABLE_INTERRUPTS(CLBR_NONE)
654 testl $3,CS-ARGOFFSET(%rsp)
655 je retint_restore_args
656 movl $_TIF_ALLWORK_MASK,%edi
657 /* edi: mask to check */
658 GLOBAL(int_with_check)
660 GET_THREAD_INFO(%rcx)
661 movl TI_flags(%rcx),%edx
664 andl $~TS_COMPAT,TI_status(%rcx)
667 /* Either reschedule or signal or syscall exit tracking needed. */
668 /* First do a reschedule test. */
669 /* edx: work, edi: workmask */
671 bt $TIF_NEED_RESCHED,%edx
674 ENABLE_INTERRUPTS(CLBR_NONE)
678 DISABLE_INTERRUPTS(CLBR_NONE)
682 /* handle signals and tracing -- both require a full stack frame */
685 ENABLE_INTERRUPTS(CLBR_NONE)
686 int_check_syscall_exit_work:
688 /* Check for syscall exit trace */
689 testl $_TIF_WORK_SYSCALL_EXIT,%edx
692 leaq 8(%rsp),%rdi # &ptregs -> arg1
693 call syscall_trace_leave
695 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
699 testl $_TIF_DO_NOTIFY_MASK,%edx
701 movq %rsp,%rdi # &ptregs -> arg1
702 xorl %esi,%esi # oldset -> arg2
703 call do_notify_resume
704 1: movl $_TIF_WORK_MASK,%edi
707 DISABLE_INTERRUPTS(CLBR_NONE)
714 * Certain special system calls that need to save a complete full stack frame.
716 .macro PTREGSCALL label,func,arg
718 PARTIAL_FRAME 1 8 /* offset 8: return address */
719 subq $REST_SKIP, %rsp
720 CFI_ADJUST_CFA_OFFSET REST_SKIP
722 DEFAULT_FRAME 0 8 /* offset 8: return address */
723 leaq 8(%rsp), \arg /* pt_regs pointer */
725 jmp ptregscall_common
730 PTREGSCALL stub_clone, sys_clone, %r8
731 PTREGSCALL stub_fork, sys_fork, %rdi
732 PTREGSCALL stub_vfork, sys_vfork, %rdi
733 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
734 PTREGSCALL stub_iopl, sys_iopl, %rsi
736 ENTRY(ptregscall_common)
737 DEFAULT_FRAME 1 8 /* offset 8: return address */
738 RESTORE_TOP_OF_STACK %r11, 8
739 movq_cfi_restore R15+8, r15
740 movq_cfi_restore R14+8, r14
741 movq_cfi_restore R13+8, r13
742 movq_cfi_restore R12+8, r12
743 movq_cfi_restore RBP+8, rbp
744 movq_cfi_restore RBX+8, rbx
745 ret $REST_SKIP /* pop extended registers */
747 END(ptregscall_common)
754 FIXUP_TOP_OF_STACK %r11
757 RESTORE_TOP_OF_STACK %r11
760 jmp int_ret_from_sys_call
765 * sigreturn is special because it needs to restore all registers on return.
766 * This cannot be done with SYSRET, so use the IRET return path instead.
768 ENTRY(stub_rt_sigreturn)
774 FIXUP_TOP_OF_STACK %r11
775 call sys_rt_sigreturn
776 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
778 jmp int_ret_from_sys_call
780 END(stub_rt_sigreturn)
783 * Build the entry stubs and pointer table with some assembler magic.
784 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
785 * single cache line on all modern x86 implementations.
787 .section .init.rodata,"a"
791 .p2align CONFIG_X86_L1_CACHE_SHIFT
792 ENTRY(irq_entries_start)
794 vector=FIRST_EXTERNAL_VECTOR
795 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
798 .if vector < NR_VECTORS
799 .if vector <> FIRST_EXTERNAL_VECTOR
800 CFI_ADJUST_CFA_OFFSET -8
802 1: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
803 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
812 2: jmp common_interrupt
815 END(irq_entries_start)
822 * Interrupt entry/exit.
824 * Interrupt entry points save only callee clobbered registers in fast path.
826 * Entry runs with interrupts off.
829 /* 0(%rsp): ~(interrupt number) */
830 .macro interrupt func
831 /* reserve pt_regs for scratch regs and rbp */
832 subq $ORIG_RAX-RBP, %rsp
833 CFI_ADJUST_CFA_OFFSET ORIG_RAX-RBP
839 * Interrupt entry/exit should be protected against kprobes
841 .pushsection .kprobes.text, "ax"
843 * The interrupt stubs push (~vector+0x80) onto the stack and
844 * then jump to common_interrupt.
846 .p2align CONFIG_X86_L1_CACHE_SHIFT
849 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
851 /* 0(%rsp): old_rsp-ARGOFFSET */
853 DISABLE_INTERRUPTS(CLBR_NONE)
855 decl PER_CPU_VAR(irq_count)
857 /* Restore saved previous stack */
859 CFI_DEF_CFA_REGISTER rsi
860 leaq ARGOFFSET-RBP(%rsi), %rsp
861 CFI_DEF_CFA_REGISTER rsp
862 CFI_ADJUST_CFA_OFFSET RBP-ARGOFFSET
865 GET_THREAD_INFO(%rcx)
866 testl $3,CS-ARGOFFSET(%rsp)
869 /* Interrupt came from user space */
871 * Has a correct top of stack, but a partial stack frame
872 * %rcx: thread info. Interrupts off.
874 retint_with_reschedule:
875 movl $_TIF_WORK_MASK,%edi
878 movl TI_flags(%rcx),%edx
883 retint_swapgs: /* return to user-space */
885 * The iretq could re-enable interrupts:
887 DISABLE_INTERRUPTS(CLBR_ANY)
890 * This opens a window where we have a user CR3, but are
891 * running in the kernel. This makes using the CS
892 * register useless for telling whether or not we need to
893 * switch CR3 in NMIs. Normal interrupts are OK because
900 retint_restore_args: /* return to kernel space */
901 DISABLE_INTERRUPTS(CLBR_ANY)
903 * The iretq could re-enable interrupts:
914 * Are we returning to a stack segment from the LDT? Note: in
915 * 64-bit mode SS:RSP on the exception stack is always valid.
917 #ifdef CONFIG_X86_ESPFIX64
918 testb $4,(SS-RIP)(%rsp)
919 jnz native_irq_return_ldt
922 .global native_irq_return_iret
923 native_irq_return_iret:
925 * This may fault. Non-paranoid faults on return to userspace are
926 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
927 * Double-faults due to espfix64 are handled in do_double_fault.
928 * Other faults here are fatal.
932 #ifdef CONFIG_X86_ESPFIX64
933 native_irq_return_ldt:
938 movq PER_CPU_VAR(espfix_waddr),%rdi
939 movq %rax,(0*8)(%rdi) /* RAX */
940 movq (2*8)(%rsp),%rax /* RIP */
941 movq %rax,(1*8)(%rdi)
942 movq (3*8)(%rsp),%rax /* CS */
943 movq %rax,(2*8)(%rdi)
944 movq (4*8)(%rsp),%rax /* RFLAGS */
945 movq %rax,(3*8)(%rdi)
946 movq (6*8)(%rsp),%rax /* SS */
947 movq %rax,(5*8)(%rdi)
948 movq (5*8)(%rsp),%rax /* RSP */
949 movq %rax,(4*8)(%rdi)
950 andl $0xffff0000,%eax
952 orq PER_CPU_VAR(espfix_stack),%rax
957 jmp native_irq_return_iret
960 /* edi: workmask, edx: work */
963 bt $TIF_NEED_RESCHED,%edx
966 ENABLE_INTERRUPTS(CLBR_NONE)
970 GET_THREAD_INFO(%rcx)
971 DISABLE_INTERRUPTS(CLBR_NONE)
976 testl $_TIF_DO_NOTIFY_MASK,%edx
979 ENABLE_INTERRUPTS(CLBR_NONE)
981 movq $-1,ORIG_RAX(%rsp)
982 xorl %esi,%esi # oldset
983 movq %rsp,%rdi # &pt_regs
984 call do_notify_resume
986 DISABLE_INTERRUPTS(CLBR_NONE)
988 GET_THREAD_INFO(%rcx)
989 jmp retint_with_reschedule
991 #ifdef CONFIG_PREEMPT
992 /* Returning to kernel space. Check if we need preemption */
993 /* rcx: threadinfo. interrupts off. */
995 cmpl $0,TI_preempt_count(%rcx)
996 jnz retint_restore_args
997 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
998 jnc retint_restore_args
999 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
1000 jnc retint_restore_args
1001 call preempt_schedule_irq
1005 END(common_interrupt)
1008 * End of kprobes section
1015 .macro apicinterrupt num sym do_sym
1026 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
1027 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
1028 apicinterrupt REBOOT_VECTOR \
1029 reboot_interrupt smp_reboot_interrupt
1032 #ifdef CONFIG_X86_UV
1033 apicinterrupt UV_BAU_MESSAGE \
1034 uv_bau_message_intr1 uv_bau_message_interrupt
1036 apicinterrupt LOCAL_TIMER_VECTOR \
1037 apic_timer_interrupt smp_apic_timer_interrupt
1038 apicinterrupt X86_PLATFORM_IPI_VECTOR \
1039 x86_platform_ipi smp_x86_platform_ipi
1042 .irp idx,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, \
1043 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
1044 .if NUM_INVALIDATE_TLB_VECTORS > \idx
1045 apicinterrupt (INVALIDATE_TLB_VECTOR_START)+\idx \
1046 invalidate_interrupt\idx smp_invalidate_interrupt
1051 apicinterrupt THRESHOLD_APIC_VECTOR \
1052 threshold_interrupt smp_threshold_interrupt
1053 apicinterrupt THERMAL_APIC_VECTOR \
1054 thermal_interrupt smp_thermal_interrupt
1057 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
1058 call_function_single_interrupt smp_call_function_single_interrupt
1059 apicinterrupt CALL_FUNCTION_VECTOR \
1060 call_function_interrupt smp_call_function_interrupt
1061 apicinterrupt RESCHEDULE_VECTOR \
1062 reschedule_interrupt smp_reschedule_interrupt
1065 apicinterrupt ERROR_APIC_VECTOR \
1066 error_interrupt smp_error_interrupt
1067 apicinterrupt SPURIOUS_APIC_VECTOR \
1068 spurious_interrupt smp_spurious_interrupt
1070 #ifdef CONFIG_IRQ_WORK
1071 apicinterrupt IRQ_WORK_VECTOR \
1072 irq_work_interrupt smp_irq_work_interrupt
1076 * Exception entry points.
1078 .macro zeroentry sym do_sym
1081 PARAVIRT_ADJUST_EXCEPTION_FRAME
1082 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1083 subq $ORIG_RAX-R15, %rsp
1084 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1087 movq %rsp,%rdi /* pt_regs pointer */
1088 xorl %esi,%esi /* no error code */
1090 jmp error_exit /* %ebx: no swapgs flag */
1095 .macro paranoidzeroentry sym do_sym
1098 PARAVIRT_ADJUST_EXCEPTION_FRAME
1099 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1100 subq $ORIG_RAX-R15, %rsp
1101 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1104 movq %rsp,%rdi /* pt_regs pointer */
1105 xorl %esi,%esi /* no error code */
1107 jmp paranoid_exit /* %ebx: no swapgs flag */
1112 #define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
1113 .macro paranoidzeroentry_ist sym do_sym ist
1116 PARAVIRT_ADJUST_EXCEPTION_FRAME
1117 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1118 subq $ORIG_RAX-R15, %rsp
1119 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1122 movq %rsp,%rdi /* pt_regs pointer */
1123 xorl %esi,%esi /* no error code */
1124 subq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1126 addq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1127 jmp paranoid_exit /* %ebx: no swapgs flag */
1132 .macro errorentry sym do_sym
1135 PARAVIRT_ADJUST_EXCEPTION_FRAME
1136 subq $ORIG_RAX-R15, %rsp
1137 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1140 movq %rsp,%rdi /* pt_regs pointer */
1141 movq ORIG_RAX(%rsp),%rsi /* get error code */
1142 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1144 jmp error_exit /* %ebx: no swapgs flag */
1149 /* error code is on the stack already */
1150 .macro paranoiderrorentry sym do_sym
1153 PARAVIRT_ADJUST_EXCEPTION_FRAME
1154 subq $ORIG_RAX-R15, %rsp
1155 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1159 movq %rsp,%rdi /* pt_regs pointer */
1160 movq ORIG_RAX(%rsp),%rsi /* get error code */
1161 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1163 jmp paranoid_exit /* %ebx: no swapgs flag */
1168 zeroentry divide_error do_divide_error
1169 zeroentry overflow do_overflow
1170 zeroentry bounds do_bounds
1171 zeroentry invalid_op do_invalid_op
1172 zeroentry device_not_available do_device_not_available
1173 paranoiderrorentry double_fault do_double_fault
1174 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1175 errorentry invalid_TSS do_invalid_TSS
1176 errorentry segment_not_present do_segment_not_present
1177 zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1178 zeroentry coprocessor_error do_coprocessor_error
1179 errorentry alignment_check do_alignment_check
1180 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1183 /* Reload gs selector with exception handling */
1184 /* edi: new selector */
1185 ENTRY(native_load_gs_index)
1188 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1192 2: mfence /* workaround */
1197 END(native_load_gs_index)
1199 .section __ex_table,"a"
1201 .quad gs_change,bad_gs
1203 .section .fixup,"ax"
1204 /* running with kernelgs */
1206 SWAPGS /* switch back to user gs */
1212 ENTRY(kernel_thread_helper)
1213 pushq $0 # fake return address
1216 * Here we are in the child and the registers are set as they were
1217 * at kernel_thread() invocation in the parent.
1223 ud2 # padding for call trace
1225 END(kernel_thread_helper)
1228 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1230 * C extern interface:
1231 * extern long execve(const char *name, char **argv, char **envp)
1233 * asm input arguments:
1234 * rdi: name, rsi: argv, rdx: envp
1236 * We want to fallback into:
1237 * extern long sys_execve(const char *name, char **argv,char **envp, struct pt_regs *regs)
1239 * do_sys_execve asm fallback arguments:
1240 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1242 ENTRY(kernel_execve)
1248 movq %rax, RAX(%rsp)
1251 je int_ret_from_sys_call
1258 /* Call softirq on interrupt stack. Interrupts are off. */
1262 CFI_REL_OFFSET rbp,0
1264 CFI_DEF_CFA_REGISTER rbp
1265 incl PER_CPU_VAR(irq_count)
1266 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1267 push %rbp # backlink for old unwinder
1271 CFI_DEF_CFA_REGISTER rsp
1272 CFI_ADJUST_CFA_OFFSET -8
1273 decl PER_CPU_VAR(irq_count)
1279 zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
1282 * A note on the "critical region" in our callback handler.
1283 * We want to avoid stacking callback handlers due to events occurring
1284 * during handling of the last event. To do this, we keep events disabled
1285 * until we've done all processing. HOWEVER, we must enable events before
1286 * popping the stack frame (can't be done atomically) and so it would still
1287 * be possible to get enough handler activations to overflow the stack.
1288 * Although unlikely, bugs of that kind are hard to track down, so we'd
1289 * like to avoid the possibility.
1290 * So, on entry to the handler we detect whether we interrupted an
1291 * existing activation in its critical region -- if so, we pop the current
1292 * activation and restart the handler using the previous one.
1294 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1297 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1298 * see the correct pointer to the pt_regs
1300 movq %rdi, %rsp # we don't return, adjust the stack frame
1303 11: incl PER_CPU_VAR(irq_count)
1305 CFI_DEF_CFA_REGISTER rbp
1306 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1307 pushq %rbp # backlink for old unwinder
1308 call xen_evtchn_do_upcall
1310 CFI_DEF_CFA_REGISTER rsp
1311 decl PER_CPU_VAR(irq_count)
1314 END(xen_do_hypervisor_callback)
1317 * Hypervisor uses this for application faults while it executes.
1318 * We get here for two reasons:
1319 * 1. Fault while reloading DS, ES, FS or GS
1320 * 2. Fault while executing IRET
1321 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1322 * registers that could be reloaded and zeroed the others.
1323 * Category 2 we fix up by killing the current process. We cannot use the
1324 * normal Linux return path in this case because if we use the IRET hypercall
1325 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1326 * We distinguish between categories by comparing each saved segment register
1327 * with its current contents: any discrepancy means we in category 1.
1329 ENTRY(xen_failsafe_callback)
1331 /*CFI_REL_OFFSET gs,GS*/
1332 /*CFI_REL_OFFSET fs,FS*/
1333 /*CFI_REL_OFFSET es,ES*/
1334 /*CFI_REL_OFFSET ds,DS*/
1335 CFI_REL_OFFSET r11,8
1336 CFI_REL_OFFSET rcx,0
1350 /* All segments match their saved values => Category 2 (Bad IRET). */
1356 CFI_ADJUST_CFA_OFFSET -0x30
1357 pushq_cfi $0 /* RIP */
1360 jmp general_protection
1362 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1368 CFI_ADJUST_CFA_OFFSET -0x30
1369 pushq_cfi $-1 /* orig_ax = -1 => not a system call */
1373 END(xen_failsafe_callback)
1375 apicinterrupt XEN_HVM_EVTCHN_CALLBACK \
1376 xen_hvm_callback_vector xen_evtchn_do_upcall
1378 #endif /* CONFIG_XEN */
1381 * Some functions should be protected against kprobes
1383 .pushsection .kprobes.text, "ax"
1385 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1386 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1387 errorentry stack_segment do_stack_segment
1389 zeroentry xen_debug do_debug
1390 zeroentry xen_int3 do_int3
1391 errorentry xen_stack_segment do_stack_segment
1393 errorentry general_protection do_general_protection
1394 errorentry page_fault do_page_fault
1395 #ifdef CONFIG_KVM_GUEST
1396 errorentry async_page_fault do_async_page_fault
1398 #ifdef CONFIG_X86_MCE
1399 paranoidzeroentry machine_check *machine_check_vector(%rip)
1403 * "Paranoid" exit path from exception stack.
1404 * Paranoid because this is used by NMIs and cannot take
1405 * any kernel state for granted.
1406 * We don't do kernel preemption checks here, because only
1407 * NMI should be common and it does not enable IRQs and
1408 * cannot get reschedule ticks.
1410 * "trace" is 0 for the NMI handler only, because irq-tracing
1411 * is fundamentally NMI-unsafe. (we cannot change the soft and
1412 * hard flags at once, atomically)
1415 * On entry: ebx=0: needs swapgs but not SWITCH_USER_CR3
1416 * ebx=1: needs neither swapgs nor SWITCH_USER_CR3
1417 * ebx=2: needs both swapgs and SWITCH_USER_CR3
1418 * ebx=3: needs SWITCH_USER_CR3 but not swapgs
1420 ENTRY(paranoid_exit)
1422 DISABLE_INTERRUPTS(CLBR_NONE)
1424 movq %rbx, %r12 /* paranoid_userspace uses %ebx */
1426 jnz paranoid_userspace
1428 movq %r12, %rbx /* restore after paranoid_userspace */
1430 #ifdef CONFIG_KAISER
1431 testl $2, %ebx /* SWITCH_USER_CR3 needed? */
1432 jz paranoid_exit_no_switch
1434 paranoid_exit_no_switch:
1436 testl $1, %ebx /* swapgs needed? */
1437 jnz paranoid_exit_no_swapgs
1439 paranoid_exit_no_swapgs:
1444 GET_THREAD_INFO(%rcx)
1445 movl TI_flags(%rcx),%ebx
1446 andl $_TIF_WORK_MASK,%ebx
1448 movq %rsp,%rdi /* &pt_regs */
1450 movq %rax,%rsp /* switch stack for scheduling */
1451 testl $_TIF_NEED_RESCHED,%ebx
1452 jnz paranoid_schedule
1453 movl %ebx,%edx /* arg3: thread flags */
1455 ENABLE_INTERRUPTS(CLBR_NONE)
1456 xorl %esi,%esi /* arg2: oldset */
1457 movq %rsp,%rdi /* arg1: &pt_regs */
1458 call do_notify_resume
1459 DISABLE_INTERRUPTS(CLBR_NONE)
1461 jmp paranoid_userspace
1464 ENABLE_INTERRUPTS(CLBR_ANY)
1466 DISABLE_INTERRUPTS(CLBR_ANY)
1468 jmp paranoid_userspace
1473 * Exception entry point. This expects an error code/orig_rax on the stack.
1474 * returns in "no swapgs flag" in %ebx.
1478 CFI_ADJUST_CFA_OFFSET 15*8
1479 /* oldrax contains error code */
1497 * error_entry() always returns with a kernel gsbase and
1498 * CR3. We must also have a kernel CR3/gsbase before
1499 * calling TRACE_IRQS_*. Just unconditionally switch to
1500 * the kernel CR3 here.
1505 je error_kernelspace
1513 * There are two places in the kernel that can potentially fault with
1514 * usergs. Handle them here. B stepping K8s sometimes report a
1515 * truncated RIP for IRET exceptions returning to compat mode. Check
1516 * for these here too.
1520 leaq native_irq_return_iret(%rip),%rcx
1521 cmpq %rcx,RIP+8(%rsp)
1523 movl %ecx,%eax /* zero extend */
1524 cmpq %rax,RIP+8(%rsp)
1526 cmpq $gs_change,RIP+8(%rsp)
1531 /* Fix truncated RIP */
1532 movq %rcx,RIP+8(%rsp)
1540 decl %ebx /* Return to usergs */
1546 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1551 DISABLE_INTERRUPTS(CLBR_NONE)
1553 GET_THREAD_INFO(%rcx)
1556 LOCKDEP_SYS_EXIT_IRQ
1557 movl TI_flags(%rcx),%edx
1558 movl $_TIF_WORK_MASK,%edi
1566 /* runs on exception stack */
1570 * Fix up the exception frame if we're on Xen.
1571 * PARAVIRT_ADJUST_EXCEPTION_FRAME is guaranteed to push at most
1572 * one value to the stack on native, so it may clobber the rdx
1573 * scratch slot, but it won't clobber any of the important
1576 * Xen is a different story, because the Xen frame itself overlaps
1577 * the "NMI executing" variable.
1579 PARAVIRT_ADJUST_EXCEPTION_FRAME
1582 subq $ORIG_RAX-R15, %rsp
1583 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1586 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1590 #ifdef CONFIG_TRACE_IRQFLAGS
1591 /* paranoidexit; without TRACE_IRQS_OFF */
1592 /* ebx: no-swapgs and kaiser-switch-cr3 flag */
1593 DISABLE_INTERRUPTS(CLBR_NONE)
1594 movq %rbx, %r12 /* nmi_userspace uses %ebx */
1598 movq %r12, %rbx /* restore after nmi_userspace */
1599 #ifdef CONFIG_KAISER
1600 testl $2, %ebx /* SWITCH_USER_CR3 needed? */
1601 jz nmi_exit_no_switch
1605 testl $1, %ebx /* swapgs needed? */
1606 jnz nmi_exit_no_swapgs
1613 GET_THREAD_INFO(%rcx)
1614 movl TI_flags(%rcx),%ebx
1615 andl $_TIF_WORK_MASK,%ebx
1617 movq %rsp,%rdi /* &pt_regs */
1619 movq %rax,%rsp /* switch stack for scheduling */
1620 testl $_TIF_NEED_RESCHED,%ebx
1622 movl %ebx,%edx /* arg3: thread flags */
1623 ENABLE_INTERRUPTS(CLBR_NONE)
1624 xorl %esi,%esi /* arg2: oldset */
1625 movq %rsp,%rdi /* arg1: &pt_regs */
1626 call do_notify_resume
1627 DISABLE_INTERRUPTS(CLBR_NONE)
1630 ENABLE_INTERRUPTS(CLBR_ANY)
1632 DISABLE_INTERRUPTS(CLBR_ANY)
1641 ENTRY(ignore_sysret)
1649 * End of kprobes section