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/alternative-asm.h>
60 #include <asm/cpufeature.h>
61 #include <asm/kaiser.h>
63 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
64 #include <linux/elf-em.h>
65 #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
66 #define __AUDIT_ARCH_64BIT 0x80000000
67 #define __AUDIT_ARCH_LE 0x40000000
70 .section .entry.text, "ax"
72 #ifdef CONFIG_FUNCTION_TRACER
73 #ifdef CONFIG_DYNAMIC_FTRACE
79 cmpl $0, function_trace_stop
86 subq $MCOUNT_INSN_SIZE, %rdi
93 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
94 GLOBAL(ftrace_graph_call)
102 #else /* ! CONFIG_DYNAMIC_FTRACE */
104 cmpl $0, function_trace_stop
107 cmpq $ftrace_stub, ftrace_trace_function
110 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
111 cmpq $ftrace_stub, ftrace_graph_return
112 jnz ftrace_graph_caller
114 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
115 jnz ftrace_graph_caller
124 movq 0x38(%rsp), %rdi
126 subq $MCOUNT_INSN_SIZE, %rdi
128 call *ftrace_trace_function
134 #endif /* CONFIG_DYNAMIC_FTRACE */
135 #endif /* CONFIG_FUNCTION_TRACER */
137 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
138 ENTRY(ftrace_graph_caller)
139 cmpl $0, function_trace_stop
145 movq 0x38(%rsp), %rsi
147 subq $MCOUNT_INSN_SIZE, %rsi
149 call prepare_ftrace_return
154 END(ftrace_graph_caller)
156 GLOBAL(return_to_handler)
159 /* Save the return values */
164 call ftrace_return_to_handler
174 #ifndef CONFIG_PREEMPT
175 #define retint_kernel retint_restore_args
178 #ifdef CONFIG_PARAVIRT
179 ENTRY(native_usergs_sysret64)
182 ENDPROC(native_usergs_sysret64)
183 #endif /* CONFIG_PARAVIRT */
186 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
187 #ifdef CONFIG_TRACE_IRQFLAGS
188 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
196 * C code is not supposed to know about undefined top of stack. Every time
197 * a C function with an pt_regs argument is called from the SYSCALL based
198 * fast path FIXUP_TOP_OF_STACK is needed.
199 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
203 /* %rsp:at FRAMEEND */
204 .macro FIXUP_TOP_OF_STACK tmp offset=0
205 movq PER_CPU_VAR(old_rsp),\tmp
206 movq \tmp,RSP+\offset(%rsp)
207 movq $__USER_DS,SS+\offset(%rsp)
208 movq $__USER_CS,CS+\offset(%rsp)
209 movq $-1,RCX+\offset(%rsp)
210 movq R11+\offset(%rsp),\tmp /* get eflags */
211 movq \tmp,EFLAGS+\offset(%rsp)
214 .macro RESTORE_TOP_OF_STACK tmp offset=0
215 movq RSP+\offset(%rsp),\tmp
216 movq \tmp,PER_CPU_VAR(old_rsp)
217 movq EFLAGS+\offset(%rsp),\tmp
218 movq \tmp,R11+\offset(%rsp)
221 .macro FAKE_STACK_FRAME child_rip
222 /* push in order ss, rsp, eflags, cs, rip */
224 pushq_cfi $__KERNEL_DS /* ss */
225 /*CFI_REL_OFFSET ss,0*/
226 pushq_cfi %rax /* rsp */
228 pushq_cfi $X86_EFLAGS_IF /* eflags - interrupts on */
229 /*CFI_REL_OFFSET rflags,0*/
230 pushq_cfi $__KERNEL_CS /* cs */
231 /*CFI_REL_OFFSET cs,0*/
232 pushq_cfi \child_rip /* rip */
234 pushq_cfi %rax /* orig rax */
237 .macro UNFAKE_STACK_FRAME
239 CFI_ADJUST_CFA_OFFSET -(6*8)
243 * initial frame state for interrupts (and exceptions without error code)
245 .macro EMPTY_FRAME start=1 offset=0
249 CFI_DEF_CFA rsp,8+\offset
251 CFI_DEF_CFA_OFFSET 8+\offset
256 * initial frame state for interrupts (and exceptions without error code)
258 .macro INTR_FRAME start=1 offset=0
259 EMPTY_FRAME \start, SS+8+\offset-RIP
260 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
261 CFI_REL_OFFSET rsp, RSP+\offset-RIP
262 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
263 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
264 CFI_REL_OFFSET rip, RIP+\offset-RIP
268 * initial frame state for exceptions with error code (and interrupts
269 * with vector already pushed)
271 .macro XCPT_FRAME start=1 offset=0
272 INTR_FRAME \start, RIP+\offset-ORIG_RAX
273 /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
277 * frame that enables calling into C.
279 .macro PARTIAL_FRAME start=1 offset=0
280 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
281 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
282 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
283 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
284 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
285 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
286 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
287 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
288 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
289 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
293 * frame that enables passing a complete pt_regs to a C function.
295 .macro DEFAULT_FRAME start=1 offset=0
296 PARTIAL_FRAME \start, R11+\offset-R15
297 CFI_REL_OFFSET rbx, RBX+\offset
298 CFI_REL_OFFSET rbp, RBP+\offset
299 CFI_REL_OFFSET r12, R12+\offset
300 CFI_REL_OFFSET r13, R13+\offset
301 CFI_REL_OFFSET r14, R14+\offset
302 CFI_REL_OFFSET r15, R15+\offset
305 /* save partial stack frame */
308 /* start from rbp in pt_regs and jump over */
309 movq_cfi rdi, RDI-RBP
310 movq_cfi rsi, RSI-RBP
311 movq_cfi rdx, RDX-RBP
312 movq_cfi rcx, RCX-RBP
313 movq_cfi rax, RAX-RBP
316 movq_cfi r10, R10-RBP
317 movq_cfi r11, R11-RBP
319 /* Save rbp so that we can unwind from get_irq_regs() */
322 /* Save previous stack value */
325 leaq -RBP(%rsp),%rdi /* arg1 for handler */
331 * irq_count is used to check if a CPU is already on an interrupt stack
332 * or not. While this is essentially redundant with preempt_count it is
333 * a little cheaper to use a separate counter in the PDA (short of
334 * moving irq_enter into assembly, which would be too much work)
336 1: incl PER_CPU_VAR(irq_count)
338 mov PER_CPU_VAR(irq_stack_ptr),%rsp
339 CFI_DEF_CFA_REGISTER rsi
341 2: /* Store previous stack value */
343 CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
344 0x77 /* DW_OP_breg7 */, 0, \
345 0x06 /* DW_OP_deref */, \
346 0x08 /* DW_OP_const1u */, SS+8-RBP, \
347 0x22 /* DW_OP_plus */
348 /* We entered an interrupt context - irqs are off: */
353 PARTIAL_FRAME 1 REST_SKIP+8
354 movq 5*8+16(%rsp), %r11 /* save return address */
361 movq %r11, 8(%rsp) /* return address */
362 FIXUP_TOP_OF_STACK %r11, 16
367 /* save complete stack frame */
368 .pushsection .kprobes.text, "ax"
370 * Return: ebx=0: needs swapgs but not SWITCH_USER_CR3 in paranoid_exit
371 * ebx=1: needs neither swapgs nor SWITCH_USER_CR3 in paranoid_exit
372 * ebx=2: needs both swapgs and SWITCH_USER_CR3 in paranoid_exit
373 * ebx=3: needs SWITCH_USER_CR3 but not swapgs in paranoid_exit
394 movl $MSR_GS_BASE,%ecx
397 js 1f /* negative -> in kernel */
401 #ifdef CONFIG_PAGE_TABLE_ISOLATION
403 * We might have come in between a swapgs and a SWITCH_KERNEL_CR3
404 * on entry, or between a SWITCH_USER_CR3 and a swapgs on exit.
405 * Do a conditional SWITCH_KERNEL_CR3: this could safely be done
406 * unconditionally, but we need to find out whether the reverse
407 * should be done on return (conveyed to paranoid_exit in %ebx).
409 ALTERNATIVE "jmp 2f", "movq %cr3, %rax", X86_FEATURE_KAISER
410 testl $KAISER_SHADOW_PGD_OFFSET, %eax
413 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax
414 /* If PCID enabled, set X86_CR3_PCID_NOFLUSH_BIT */
415 ALTERNATIVE "", "bts $63, %rax", X86_FEATURE_PCID
425 * A newly forked process directly context switches into this address.
427 * rdi: prev task we switched from
432 LOCK ; btr $TIF_FORK,TI_flags(%r8)
434 pushq_cfi kernel_eflags(%rip)
435 popfq_cfi # reset kernel eflags
437 call schedule_tail # rdi: 'prev' task parameter
439 GET_THREAD_INFO(%rcx)
443 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
444 je int_ret_from_sys_call
447 * By the time we get here, we have no idea whether our pt_regs,
448 * ti flags, and ti status came from the 64-bit SYSCALL fast path,
449 * the slow path, or one of the ia32entry paths.
450 * Use int_ret_from_sys_call to return, since it can safely handle
453 jmp int_ret_from_sys_call
459 * System call entry. Up to 6 arguments in registers are supported.
461 * SYSCALL does not save anything on the stack and does not change the
467 * rax system call number
469 * rcx return address for syscall/sysret, C arg3
472 * r10 arg3 (--> moved to rcx for C)
475 * r11 eflags for syscall/sysret, temporary for C
476 * r12-r15,rbp,rbx saved by C code, not touched.
478 * Interrupts are off on entry.
479 * Only called from user space.
481 * XXX if we had a free scratch register we could save the RSP into the stack frame
482 * and report it properly in ps. Unfortunately we haven't.
484 * When user can change the frames always force IRET. That is because
485 * it deals with uncanonical addresses better. SYSRET has trouble
486 * with them due to bugs in both AMD and Intel CPUs.
492 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
494 /*CFI_REGISTER rflags,r11*/
496 SWITCH_KERNEL_CR3_NO_STACK
498 * A hypervisor implementation might want to use a label
499 * after the swapgs, so that it can do the swapgs
500 * for the guest and jump here on syscall.
502 ENTRY(system_call_after_swapgs)
504 movq %rsp,PER_CPU_VAR(old_rsp)
505 movq PER_CPU_VAR(kernel_stack),%rsp
507 * No need to follow this irqs off/on section - it's straight
510 ENABLE_INTERRUPTS(CLBR_NONE)
512 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
513 movq %rcx,RIP-ARGOFFSET(%rsp)
514 CFI_REL_OFFSET rip,RIP-ARGOFFSET
515 GET_THREAD_INFO(%rcx)
516 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
518 system_call_fastpath:
519 cmpq $__NR_syscall_max,%rax
522 call *sys_call_table(,%rax,8) # XXX: rip relative
523 movq %rax,RAX-ARGOFFSET(%rsp)
525 * Syscall return path ending with SYSRET (fast path)
526 * Has incomplete stack frame and undefined top of stack.
529 movl $_TIF_ALLWORK_MASK,%edi
533 GET_THREAD_INFO(%rcx)
534 DISABLE_INTERRUPTS(CLBR_NONE)
536 movl TI_flags(%rcx),%edx
541 * sysretq will re-enable interrupts:
544 movq RIP-ARGOFFSET(%rsp),%rcx
546 RESTORE_ARGS 1,-ARG_SKIP,0
547 /*CFI_REGISTER rflags,r11*/
549 * This opens a window where we have a user CR3, but are
550 * running in the kernel. This makes using the CS
551 * register useless for telling whether or not we need to
552 * switch CR3 in NMIs. Normal interrupts are OK because
556 movq PER_CPU_VAR(old_rsp), %rsp
560 /* Handle reschedules */
561 /* edx: work, edi: workmask */
563 bt $TIF_NEED_RESCHED,%edx
566 ENABLE_INTERRUPTS(CLBR_NONE)
572 /* Handle a signal */
575 ENABLE_INTERRUPTS(CLBR_NONE)
576 #ifdef CONFIG_AUDITSYSCALL
577 bt $TIF_SYSCALL_AUDIT,%edx
581 * We have a signal, or exit tracing or single-step.
582 * These all wind up with the iret return path anyway,
583 * so just join that path right now.
585 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
586 jmp int_check_syscall_exit_work
589 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
590 jmp ret_from_sys_call
592 #ifdef CONFIG_AUDITSYSCALL
594 * Fast path for syscall audit without full syscall trace.
595 * We just call audit_syscall_entry() directly, and then
596 * jump back to the normal fast path.
599 movq %r10,%r9 /* 6th arg: 4th syscall arg */
600 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
601 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
602 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
603 movq %rax,%rsi /* 2nd arg: syscall number */
604 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
605 call audit_syscall_entry
606 LOAD_ARGS 0 /* reload call-clobbered registers */
607 jmp system_call_fastpath
610 * Return fast path for syscall audit. Call audit_syscall_exit()
611 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
615 movq RAX-ARGOFFSET(%rsp),%rsi /* second arg, syscall return value */
616 cmpq $0,%rsi /* is it < 0? */
617 setl %al /* 1 if so, 0 if not */
618 movzbl %al,%edi /* zero-extend that into %edi */
619 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
620 call audit_syscall_exit
621 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
623 #endif /* CONFIG_AUDITSYSCALL */
625 /* Do syscall tracing */
627 #ifdef CONFIG_AUDITSYSCALL
628 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
632 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
633 FIXUP_TOP_OF_STACK %rdi
635 call syscall_trace_enter
637 * Reload arg registers from stack in case ptrace changed them.
638 * We don't reload %rax because syscall_trace_enter() returned
639 * the value it wants us to use in the table lookup.
641 LOAD_ARGS ARGOFFSET, 1
643 cmpq $__NR_syscall_max,%rax
644 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
645 movq %r10,%rcx /* fixup for C */
646 call *sys_call_table(,%rax,8)
647 movq %rax,RAX-ARGOFFSET(%rsp)
648 /* Use IRET because user could have changed frame */
651 * Syscall return path ending with IRET.
652 * Has correct top of stack, but partial stack frame.
654 GLOBAL(int_ret_from_sys_call)
655 DISABLE_INTERRUPTS(CLBR_NONE)
657 testl $3,CS-ARGOFFSET(%rsp)
658 je retint_restore_args
659 movl $_TIF_ALLWORK_MASK,%edi
660 /* edi: mask to check */
661 GLOBAL(int_with_check)
663 GET_THREAD_INFO(%rcx)
664 movl TI_flags(%rcx),%edx
667 andl $~TS_COMPAT,TI_status(%rcx)
670 /* Either reschedule or signal or syscall exit tracking needed. */
671 /* First do a reschedule test. */
672 /* edx: work, edi: workmask */
674 bt $TIF_NEED_RESCHED,%edx
677 ENABLE_INTERRUPTS(CLBR_NONE)
681 DISABLE_INTERRUPTS(CLBR_NONE)
685 /* handle signals and tracing -- both require a full stack frame */
688 ENABLE_INTERRUPTS(CLBR_NONE)
689 int_check_syscall_exit_work:
691 /* Check for syscall exit trace */
692 testl $_TIF_WORK_SYSCALL_EXIT,%edx
695 leaq 8(%rsp),%rdi # &ptregs -> arg1
696 call syscall_trace_leave
698 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
702 testl $_TIF_DO_NOTIFY_MASK,%edx
704 movq %rsp,%rdi # &ptregs -> arg1
705 xorl %esi,%esi # oldset -> arg2
706 call do_notify_resume
707 1: movl $_TIF_WORK_MASK,%edi
710 DISABLE_INTERRUPTS(CLBR_NONE)
717 * Certain special system calls that need to save a complete full stack frame.
719 .macro PTREGSCALL label,func,arg
721 PARTIAL_FRAME 1 8 /* offset 8: return address */
722 subq $REST_SKIP, %rsp
723 CFI_ADJUST_CFA_OFFSET REST_SKIP
725 DEFAULT_FRAME 0 8 /* offset 8: return address */
726 leaq 8(%rsp), \arg /* pt_regs pointer */
728 jmp ptregscall_common
733 PTREGSCALL stub_clone, sys_clone, %r8
734 PTREGSCALL stub_fork, sys_fork, %rdi
735 PTREGSCALL stub_vfork, sys_vfork, %rdi
736 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
737 PTREGSCALL stub_iopl, sys_iopl, %rsi
739 ENTRY(ptregscall_common)
740 DEFAULT_FRAME 1 8 /* offset 8: return address */
741 RESTORE_TOP_OF_STACK %r11, 8
742 movq_cfi_restore R15+8, r15
743 movq_cfi_restore R14+8, r14
744 movq_cfi_restore R13+8, r13
745 movq_cfi_restore R12+8, r12
746 movq_cfi_restore RBP+8, rbp
747 movq_cfi_restore RBX+8, rbx
748 ret $REST_SKIP /* pop extended registers */
750 END(ptregscall_common)
757 FIXUP_TOP_OF_STACK %r11
760 RESTORE_TOP_OF_STACK %r11
763 jmp int_ret_from_sys_call
768 * sigreturn is special because it needs to restore all registers on return.
769 * This cannot be done with SYSRET, so use the IRET return path instead.
771 ENTRY(stub_rt_sigreturn)
777 FIXUP_TOP_OF_STACK %r11
778 call sys_rt_sigreturn
779 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
781 jmp int_ret_from_sys_call
783 END(stub_rt_sigreturn)
786 * Build the entry stubs and pointer table with some assembler magic.
787 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
788 * single cache line on all modern x86 implementations.
790 .section .init.rodata,"a"
794 .p2align CONFIG_X86_L1_CACHE_SHIFT
795 ENTRY(irq_entries_start)
797 vector=FIRST_EXTERNAL_VECTOR
798 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
801 .if vector < NR_VECTORS
802 .if vector <> FIRST_EXTERNAL_VECTOR
803 CFI_ADJUST_CFA_OFFSET -8
805 1: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
806 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
815 2: jmp common_interrupt
818 END(irq_entries_start)
825 * Interrupt entry/exit.
827 * Interrupt entry points save only callee clobbered registers in fast path.
829 * Entry runs with interrupts off.
832 /* 0(%rsp): ~(interrupt number) */
833 .macro interrupt func
834 /* reserve pt_regs for scratch regs and rbp */
835 subq $ORIG_RAX-RBP, %rsp
836 CFI_ADJUST_CFA_OFFSET ORIG_RAX-RBP
842 * Interrupt entry/exit should be protected against kprobes
844 .pushsection .kprobes.text, "ax"
846 * The interrupt stubs push (~vector+0x80) onto the stack and
847 * then jump to common_interrupt.
849 .p2align CONFIG_X86_L1_CACHE_SHIFT
852 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
854 /* 0(%rsp): old_rsp-ARGOFFSET */
856 DISABLE_INTERRUPTS(CLBR_NONE)
858 decl PER_CPU_VAR(irq_count)
860 /* Restore saved previous stack */
862 CFI_DEF_CFA_REGISTER rsi
863 leaq ARGOFFSET-RBP(%rsi), %rsp
864 CFI_DEF_CFA_REGISTER rsp
865 CFI_ADJUST_CFA_OFFSET RBP-ARGOFFSET
868 GET_THREAD_INFO(%rcx)
869 testl $3,CS-ARGOFFSET(%rsp)
872 /* Interrupt came from user space */
874 * Has a correct top of stack, but a partial stack frame
875 * %rcx: thread info. Interrupts off.
877 retint_with_reschedule:
878 movl $_TIF_WORK_MASK,%edi
881 movl TI_flags(%rcx),%edx
886 retint_swapgs: /* return to user-space */
888 * The iretq could re-enable interrupts:
890 DISABLE_INTERRUPTS(CLBR_ANY)
893 * This opens a window where we have a user CR3, but are
894 * running in the kernel. This makes using the CS
895 * register useless for telling whether or not we need to
896 * switch CR3 in NMIs. Normal interrupts are OK because
903 retint_restore_args: /* return to kernel space */
904 DISABLE_INTERRUPTS(CLBR_ANY)
906 * The iretq could re-enable interrupts:
917 * Are we returning to a stack segment from the LDT? Note: in
918 * 64-bit mode SS:RSP on the exception stack is always valid.
920 #ifdef CONFIG_X86_ESPFIX64
921 testb $4,(SS-RIP)(%rsp)
922 jnz native_irq_return_ldt
925 .global native_irq_return_iret
926 native_irq_return_iret:
928 * This may fault. Non-paranoid faults on return to userspace are
929 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
930 * Double-faults due to espfix64 are handled in do_double_fault.
931 * Other faults here are fatal.
935 #ifdef CONFIG_X86_ESPFIX64
936 native_irq_return_ldt:
941 movq PER_CPU_VAR(espfix_waddr),%rdi
942 movq %rax,(0*8)(%rdi) /* RAX */
943 movq (2*8)(%rsp),%rax /* RIP */
944 movq %rax,(1*8)(%rdi)
945 movq (3*8)(%rsp),%rax /* CS */
946 movq %rax,(2*8)(%rdi)
947 movq (4*8)(%rsp),%rax /* RFLAGS */
948 movq %rax,(3*8)(%rdi)
949 movq (6*8)(%rsp),%rax /* SS */
950 movq %rax,(5*8)(%rdi)
951 movq (5*8)(%rsp),%rax /* RSP */
952 movq %rax,(4*8)(%rdi)
953 andl $0xffff0000,%eax
955 orq PER_CPU_VAR(espfix_stack),%rax
960 jmp native_irq_return_iret
963 /* edi: workmask, edx: work */
966 bt $TIF_NEED_RESCHED,%edx
969 ENABLE_INTERRUPTS(CLBR_NONE)
973 GET_THREAD_INFO(%rcx)
974 DISABLE_INTERRUPTS(CLBR_NONE)
979 testl $_TIF_DO_NOTIFY_MASK,%edx
982 ENABLE_INTERRUPTS(CLBR_NONE)
984 movq $-1,ORIG_RAX(%rsp)
985 xorl %esi,%esi # oldset
986 movq %rsp,%rdi # &pt_regs
987 call do_notify_resume
989 DISABLE_INTERRUPTS(CLBR_NONE)
991 GET_THREAD_INFO(%rcx)
992 jmp retint_with_reschedule
994 #ifdef CONFIG_PREEMPT
995 /* Returning to kernel space. Check if we need preemption */
996 /* rcx: threadinfo. interrupts off. */
998 cmpl $0,TI_preempt_count(%rcx)
999 jnz retint_restore_args
1000 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
1001 jnc retint_restore_args
1002 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
1003 jnc retint_restore_args
1004 call preempt_schedule_irq
1008 END(common_interrupt)
1011 * End of kprobes section
1018 .macro apicinterrupt num sym do_sym
1029 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
1030 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
1031 apicinterrupt REBOOT_VECTOR \
1032 reboot_interrupt smp_reboot_interrupt
1035 #ifdef CONFIG_X86_UV
1036 apicinterrupt UV_BAU_MESSAGE \
1037 uv_bau_message_intr1 uv_bau_message_interrupt
1039 apicinterrupt LOCAL_TIMER_VECTOR \
1040 apic_timer_interrupt smp_apic_timer_interrupt
1041 apicinterrupt X86_PLATFORM_IPI_VECTOR \
1042 x86_platform_ipi smp_x86_platform_ipi
1045 .irp idx,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, \
1046 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
1047 .if NUM_INVALIDATE_TLB_VECTORS > \idx
1048 apicinterrupt (INVALIDATE_TLB_VECTOR_START)+\idx \
1049 invalidate_interrupt\idx smp_invalidate_interrupt
1054 apicinterrupt THRESHOLD_APIC_VECTOR \
1055 threshold_interrupt smp_threshold_interrupt
1056 apicinterrupt THERMAL_APIC_VECTOR \
1057 thermal_interrupt smp_thermal_interrupt
1060 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
1061 call_function_single_interrupt smp_call_function_single_interrupt
1062 apicinterrupt CALL_FUNCTION_VECTOR \
1063 call_function_interrupt smp_call_function_interrupt
1064 apicinterrupt RESCHEDULE_VECTOR \
1065 reschedule_interrupt smp_reschedule_interrupt
1068 apicinterrupt ERROR_APIC_VECTOR \
1069 error_interrupt smp_error_interrupt
1070 apicinterrupt SPURIOUS_APIC_VECTOR \
1071 spurious_interrupt smp_spurious_interrupt
1073 #ifdef CONFIG_IRQ_WORK
1074 apicinterrupt IRQ_WORK_VECTOR \
1075 irq_work_interrupt smp_irq_work_interrupt
1079 * Exception entry points.
1081 .macro zeroentry sym do_sym
1084 PARAVIRT_ADJUST_EXCEPTION_FRAME
1085 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1086 subq $ORIG_RAX-R15, %rsp
1087 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1090 movq %rsp,%rdi /* pt_regs pointer */
1091 xorl %esi,%esi /* no error code */
1093 jmp error_exit /* %ebx: no swapgs flag */
1098 .macro paranoidzeroentry sym do_sym
1101 PARAVIRT_ADJUST_EXCEPTION_FRAME
1102 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1103 subq $ORIG_RAX-R15, %rsp
1104 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1107 movq %rsp,%rdi /* pt_regs pointer */
1108 xorl %esi,%esi /* no error code */
1110 jmp paranoid_exit /* %ebx: no swapgs flag */
1115 #define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
1116 .macro paranoidzeroentry_ist sym do_sym ist
1119 PARAVIRT_ADJUST_EXCEPTION_FRAME
1120 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1121 subq $ORIG_RAX-R15, %rsp
1122 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1125 movq %rsp,%rdi /* pt_regs pointer */
1126 xorl %esi,%esi /* no error code */
1127 subq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1129 addq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1130 jmp paranoid_exit /* %ebx: no swapgs flag */
1135 .macro errorentry sym do_sym
1138 PARAVIRT_ADJUST_EXCEPTION_FRAME
1139 subq $ORIG_RAX-R15, %rsp
1140 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1143 movq %rsp,%rdi /* pt_regs pointer */
1144 movq ORIG_RAX(%rsp),%rsi /* get error code */
1145 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1147 jmp error_exit /* %ebx: no swapgs flag */
1152 /* error code is on the stack already */
1153 .macro paranoiderrorentry sym do_sym
1156 PARAVIRT_ADJUST_EXCEPTION_FRAME
1157 subq $ORIG_RAX-R15, %rsp
1158 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1162 movq %rsp,%rdi /* pt_regs pointer */
1163 movq ORIG_RAX(%rsp),%rsi /* get error code */
1164 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1166 jmp paranoid_exit /* %ebx: no swapgs flag */
1171 zeroentry divide_error do_divide_error
1172 zeroentry overflow do_overflow
1173 zeroentry bounds do_bounds
1174 zeroentry invalid_op do_invalid_op
1175 zeroentry device_not_available do_device_not_available
1176 paranoiderrorentry double_fault do_double_fault
1177 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1178 errorentry invalid_TSS do_invalid_TSS
1179 errorentry segment_not_present do_segment_not_present
1180 zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1181 zeroentry coprocessor_error do_coprocessor_error
1182 errorentry alignment_check do_alignment_check
1183 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1186 /* Reload gs selector with exception handling */
1187 /* edi: new selector */
1188 ENTRY(native_load_gs_index)
1191 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1195 2: mfence /* workaround */
1200 END(native_load_gs_index)
1202 .section __ex_table,"a"
1204 .quad gs_change,bad_gs
1206 .section .fixup,"ax"
1207 /* running with kernelgs */
1209 SWAPGS /* switch back to user gs */
1215 ENTRY(kernel_thread_helper)
1216 pushq $0 # fake return address
1219 * Here we are in the child and the registers are set as they were
1220 * at kernel_thread() invocation in the parent.
1226 ud2 # padding for call trace
1228 END(kernel_thread_helper)
1231 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1233 * C extern interface:
1234 * extern long execve(const char *name, char **argv, char **envp)
1236 * asm input arguments:
1237 * rdi: name, rsi: argv, rdx: envp
1239 * We want to fallback into:
1240 * extern long sys_execve(const char *name, char **argv,char **envp, struct pt_regs *regs)
1242 * do_sys_execve asm fallback arguments:
1243 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1245 ENTRY(kernel_execve)
1251 movq %rax, RAX(%rsp)
1254 je int_ret_from_sys_call
1261 /* Call softirq on interrupt stack. Interrupts are off. */
1265 CFI_REL_OFFSET rbp,0
1267 CFI_DEF_CFA_REGISTER rbp
1268 incl PER_CPU_VAR(irq_count)
1269 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1270 push %rbp # backlink for old unwinder
1274 CFI_DEF_CFA_REGISTER rsp
1275 CFI_ADJUST_CFA_OFFSET -8
1276 decl PER_CPU_VAR(irq_count)
1282 zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
1285 * A note on the "critical region" in our callback handler.
1286 * We want to avoid stacking callback handlers due to events occurring
1287 * during handling of the last event. To do this, we keep events disabled
1288 * until we've done all processing. HOWEVER, we must enable events before
1289 * popping the stack frame (can't be done atomically) and so it would still
1290 * be possible to get enough handler activations to overflow the stack.
1291 * Although unlikely, bugs of that kind are hard to track down, so we'd
1292 * like to avoid the possibility.
1293 * So, on entry to the handler we detect whether we interrupted an
1294 * existing activation in its critical region -- if so, we pop the current
1295 * activation and restart the handler using the previous one.
1297 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1300 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1301 * see the correct pointer to the pt_regs
1303 movq %rdi, %rsp # we don't return, adjust the stack frame
1306 11: incl PER_CPU_VAR(irq_count)
1308 CFI_DEF_CFA_REGISTER rbp
1309 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1310 pushq %rbp # backlink for old unwinder
1311 call xen_evtchn_do_upcall
1313 CFI_DEF_CFA_REGISTER rsp
1314 decl PER_CPU_VAR(irq_count)
1317 END(xen_do_hypervisor_callback)
1320 * Hypervisor uses this for application faults while it executes.
1321 * We get here for two reasons:
1322 * 1. Fault while reloading DS, ES, FS or GS
1323 * 2. Fault while executing IRET
1324 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1325 * registers that could be reloaded and zeroed the others.
1326 * Category 2 we fix up by killing the current process. We cannot use the
1327 * normal Linux return path in this case because if we use the IRET hypercall
1328 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1329 * We distinguish between categories by comparing each saved segment register
1330 * with its current contents: any discrepancy means we in category 1.
1332 ENTRY(xen_failsafe_callback)
1334 /*CFI_REL_OFFSET gs,GS*/
1335 /*CFI_REL_OFFSET fs,FS*/
1336 /*CFI_REL_OFFSET es,ES*/
1337 /*CFI_REL_OFFSET ds,DS*/
1338 CFI_REL_OFFSET r11,8
1339 CFI_REL_OFFSET rcx,0
1353 /* All segments match their saved values => Category 2 (Bad IRET). */
1359 CFI_ADJUST_CFA_OFFSET -0x30
1360 pushq_cfi $0 /* RIP */
1363 jmp general_protection
1365 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1371 CFI_ADJUST_CFA_OFFSET -0x30
1372 pushq_cfi $-1 /* orig_ax = -1 => not a system call */
1376 END(xen_failsafe_callback)
1378 apicinterrupt XEN_HVM_EVTCHN_CALLBACK \
1379 xen_hvm_callback_vector xen_evtchn_do_upcall
1381 #endif /* CONFIG_XEN */
1384 * Some functions should be protected against kprobes
1386 .pushsection .kprobes.text, "ax"
1388 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1389 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1390 errorentry stack_segment do_stack_segment
1392 zeroentry xen_debug do_debug
1393 zeroentry xen_int3 do_int3
1394 errorentry xen_stack_segment do_stack_segment
1396 errorentry general_protection do_general_protection
1397 errorentry page_fault do_page_fault
1398 #ifdef CONFIG_KVM_GUEST
1399 errorentry async_page_fault do_async_page_fault
1401 #ifdef CONFIG_X86_MCE
1402 paranoidzeroentry machine_check *machine_check_vector(%rip)
1406 * "Paranoid" exit path from exception stack.
1407 * Paranoid because this is used by NMIs and cannot take
1408 * any kernel state for granted.
1409 * We don't do kernel preemption checks here, because only
1410 * NMI should be common and it does not enable IRQs and
1411 * cannot get reschedule ticks.
1413 * "trace" is 0 for the NMI handler only, because irq-tracing
1414 * is fundamentally NMI-unsafe. (we cannot change the soft and
1415 * hard flags at once, atomically)
1418 * On entry: ebx=0: needs swapgs but not SWITCH_USER_CR3
1419 * ebx=1: needs neither swapgs nor SWITCH_USER_CR3
1420 * ebx=2: needs both swapgs and SWITCH_USER_CR3
1421 * ebx=3: needs SWITCH_USER_CR3 but not swapgs
1423 ENTRY(paranoid_exit)
1425 DISABLE_INTERRUPTS(CLBR_NONE)
1427 movq %rbx, %r12 /* paranoid_userspace uses %ebx */
1429 jnz paranoid_userspace
1431 movq %r12, %rbx /* restore after paranoid_userspace */
1433 #ifdef CONFIG_PAGE_TABLE_ISOLATION
1434 /* No ALTERNATIVE for X86_FEATURE_KAISER: save_paranoid sets %ebx */
1435 testl $2, %ebx /* SWITCH_USER_CR3 needed? */
1436 jz paranoid_exit_no_switch
1438 paranoid_exit_no_switch:
1440 testl $1, %ebx /* swapgs needed? */
1441 jnz paranoid_exit_no_swapgs
1443 paranoid_exit_no_swapgs:
1448 GET_THREAD_INFO(%rcx)
1449 movl TI_flags(%rcx),%ebx
1450 andl $_TIF_WORK_MASK,%ebx
1452 movq %rsp,%rdi /* &pt_regs */
1454 movq %rax,%rsp /* switch stack for scheduling */
1455 testl $_TIF_NEED_RESCHED,%ebx
1456 jnz paranoid_schedule
1457 movl %ebx,%edx /* arg3: thread flags */
1459 ENABLE_INTERRUPTS(CLBR_NONE)
1460 xorl %esi,%esi /* arg2: oldset */
1461 movq %rsp,%rdi /* arg1: &pt_regs */
1462 call do_notify_resume
1463 DISABLE_INTERRUPTS(CLBR_NONE)
1465 jmp paranoid_userspace
1468 ENABLE_INTERRUPTS(CLBR_ANY)
1470 DISABLE_INTERRUPTS(CLBR_ANY)
1472 jmp paranoid_userspace
1477 * Exception entry point. This expects an error code/orig_rax on the stack.
1478 * returns in "no swapgs flag" in %ebx.
1482 CFI_ADJUST_CFA_OFFSET 15*8
1483 /* oldrax contains error code */
1501 * error_entry() always returns with a kernel gsbase and
1502 * CR3. We must also have a kernel CR3/gsbase before
1503 * calling TRACE_IRQS_*. Just unconditionally switch to
1504 * the kernel CR3 here.
1509 je error_kernelspace
1517 * There are two places in the kernel that can potentially fault with
1518 * usergs. Handle them here. B stepping K8s sometimes report a
1519 * truncated RIP for IRET exceptions returning to compat mode. Check
1520 * for these here too.
1524 leaq native_irq_return_iret(%rip),%rcx
1525 cmpq %rcx,RIP+8(%rsp)
1527 movl %ecx,%eax /* zero extend */
1528 cmpq %rax,RIP+8(%rsp)
1530 cmpq $gs_change,RIP+8(%rsp)
1535 /* Fix truncated RIP */
1536 movq %rcx,RIP+8(%rsp)
1544 decl %ebx /* Return to usergs */
1550 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1555 DISABLE_INTERRUPTS(CLBR_NONE)
1557 GET_THREAD_INFO(%rcx)
1560 LOCKDEP_SYS_EXIT_IRQ
1561 movl TI_flags(%rcx),%edx
1562 movl $_TIF_WORK_MASK,%edi
1570 /* runs on exception stack */
1574 * Fix up the exception frame if we're on Xen.
1575 * PARAVIRT_ADJUST_EXCEPTION_FRAME is guaranteed to push at most
1576 * one value to the stack on native, so it may clobber the rdx
1577 * scratch slot, but it won't clobber any of the important
1580 * Xen is a different story, because the Xen frame itself overlaps
1581 * the "NMI executing" variable.
1583 PARAVIRT_ADJUST_EXCEPTION_FRAME
1586 subq $ORIG_RAX-R15, %rsp
1587 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1590 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1594 #ifdef CONFIG_TRACE_IRQFLAGS
1595 /* paranoidexit; without TRACE_IRQS_OFF */
1596 /* ebx: no-swapgs and kaiser-switch-cr3 flag */
1597 DISABLE_INTERRUPTS(CLBR_NONE)
1598 movq %rbx, %r12 /* nmi_userspace uses %ebx */
1602 movq %r12, %rbx /* restore after nmi_userspace */
1603 #ifdef CONFIG_PAGE_TABLE_ISOLATION
1604 /* No ALTERNATIVE for X86_FEATURE_KAISER: save_paranoid sets %ebx */
1605 testl $2, %ebx /* SWITCH_USER_CR3 needed? */
1606 jz nmi_exit_no_switch
1610 testl $1, %ebx /* swapgs needed? */
1611 jnz nmi_exit_no_swapgs
1618 GET_THREAD_INFO(%rcx)
1619 movl TI_flags(%rcx),%ebx
1620 andl $_TIF_WORK_MASK,%ebx
1622 movq %rsp,%rdi /* &pt_regs */
1624 movq %rax,%rsp /* switch stack for scheduling */
1625 testl $_TIF_NEED_RESCHED,%ebx
1627 movl %ebx,%edx /* arg3: thread flags */
1628 ENABLE_INTERRUPTS(CLBR_NONE)
1629 xorl %esi,%esi /* arg2: oldset */
1630 movq %rsp,%rdi /* arg1: &pt_regs */
1631 call do_notify_resume
1632 DISABLE_INTERRUPTS(CLBR_NONE)
1635 ENABLE_INTERRUPTS(CLBR_ANY)
1637 DISABLE_INTERRUPTS(CLBR_ANY)
1646 ENTRY(ignore_sysret)
1654 * End of kprobes section