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>
62 #include <asm/nospec-branch.h>
64 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
65 #include <linux/elf-em.h>
66 #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
67 #define __AUDIT_ARCH_64BIT 0x80000000
68 #define __AUDIT_ARCH_LE 0x40000000
71 .section .entry.text, "ax"
73 #ifdef CONFIG_FUNCTION_TRACER
74 #ifdef CONFIG_DYNAMIC_FTRACE
80 cmpl $0, function_trace_stop
87 subq $MCOUNT_INSN_SIZE, %rdi
94 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
95 GLOBAL(ftrace_graph_call)
103 #else /* ! CONFIG_DYNAMIC_FTRACE */
105 cmpl $0, function_trace_stop
108 cmpq $ftrace_stub, ftrace_trace_function
111 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
112 cmpq $ftrace_stub, ftrace_graph_return
113 jnz ftrace_graph_caller
115 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
116 jnz ftrace_graph_caller
125 movq 0x38(%rsp), %rdi
127 subq $MCOUNT_INSN_SIZE, %rdi
129 movq ftrace_trace_function, %r8
135 #endif /* CONFIG_DYNAMIC_FTRACE */
136 #endif /* CONFIG_FUNCTION_TRACER */
138 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
139 ENTRY(ftrace_graph_caller)
140 cmpl $0, function_trace_stop
146 movq 0x38(%rsp), %rsi
148 subq $MCOUNT_INSN_SIZE, %rsi
150 call prepare_ftrace_return
155 END(ftrace_graph_caller)
157 GLOBAL(return_to_handler)
160 /* Save the return values */
165 call ftrace_return_to_handler
175 #ifndef CONFIG_PREEMPT
176 #define retint_kernel retint_restore_args
179 #ifdef CONFIG_PARAVIRT
180 ENTRY(native_usergs_sysret64)
183 ENDPROC(native_usergs_sysret64)
184 #endif /* CONFIG_PARAVIRT */
187 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
188 #ifdef CONFIG_TRACE_IRQFLAGS
189 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
197 * C code is not supposed to know about undefined top of stack. Every time
198 * a C function with an pt_regs argument is called from the SYSCALL based
199 * fast path FIXUP_TOP_OF_STACK is needed.
200 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
204 /* %rsp:at FRAMEEND */
205 .macro FIXUP_TOP_OF_STACK tmp offset=0
206 movq PER_CPU_VAR(old_rsp),\tmp
207 movq \tmp,RSP+\offset(%rsp)
208 movq $__USER_DS,SS+\offset(%rsp)
209 movq $__USER_CS,CS+\offset(%rsp)
210 movq $-1,RCX+\offset(%rsp)
211 movq R11+\offset(%rsp),\tmp /* get eflags */
212 movq \tmp,EFLAGS+\offset(%rsp)
215 .macro RESTORE_TOP_OF_STACK tmp offset=0
216 movq RSP+\offset(%rsp),\tmp
217 movq \tmp,PER_CPU_VAR(old_rsp)
218 movq EFLAGS+\offset(%rsp),\tmp
219 movq \tmp,R11+\offset(%rsp)
222 .macro FAKE_STACK_FRAME child_rip
223 /* push in order ss, rsp, eflags, cs, rip */
225 pushq_cfi $__KERNEL_DS /* ss */
226 /*CFI_REL_OFFSET ss,0*/
227 pushq_cfi %rax /* rsp */
229 pushq_cfi $X86_EFLAGS_IF /* eflags - interrupts on */
230 /*CFI_REL_OFFSET rflags,0*/
231 pushq_cfi $__KERNEL_CS /* cs */
232 /*CFI_REL_OFFSET cs,0*/
233 pushq_cfi \child_rip /* rip */
235 pushq_cfi %rax /* orig rax */
238 .macro UNFAKE_STACK_FRAME
240 CFI_ADJUST_CFA_OFFSET -(6*8)
244 * initial frame state for interrupts (and exceptions without error code)
246 .macro EMPTY_FRAME start=1 offset=0
250 CFI_DEF_CFA rsp,8+\offset
252 CFI_DEF_CFA_OFFSET 8+\offset
257 * initial frame state for interrupts (and exceptions without error code)
259 .macro INTR_FRAME start=1 offset=0
260 EMPTY_FRAME \start, SS+8+\offset-RIP
261 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
262 CFI_REL_OFFSET rsp, RSP+\offset-RIP
263 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
264 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
265 CFI_REL_OFFSET rip, RIP+\offset-RIP
269 * initial frame state for exceptions with error code (and interrupts
270 * with vector already pushed)
272 .macro XCPT_FRAME start=1 offset=0
273 INTR_FRAME \start, RIP+\offset-ORIG_RAX
274 /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
278 * frame that enables calling into C.
280 .macro PARTIAL_FRAME start=1 offset=0
281 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
282 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
283 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
284 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
285 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
286 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
287 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
288 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
289 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
290 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
294 * frame that enables passing a complete pt_regs to a C function.
296 .macro DEFAULT_FRAME start=1 offset=0
297 PARTIAL_FRAME \start, R11+\offset-R15
298 CFI_REL_OFFSET rbx, RBX+\offset
299 CFI_REL_OFFSET rbp, RBP+\offset
300 CFI_REL_OFFSET r12, R12+\offset
301 CFI_REL_OFFSET r13, R13+\offset
302 CFI_REL_OFFSET r14, R14+\offset
303 CFI_REL_OFFSET r15, R15+\offset
306 /* save partial stack frame */
309 /* start from rbp in pt_regs and jump over */
310 movq_cfi rdi, RDI-RBP
311 movq_cfi rsi, RSI-RBP
312 movq_cfi rdx, RDX-RBP
313 movq_cfi rcx, RCX-RBP
314 movq_cfi rax, RAX-RBP
317 movq_cfi r10, R10-RBP
318 movq_cfi r11, R11-RBP
320 /* Save rbp so that we can unwind from get_irq_regs() */
323 /* Save previous stack value */
326 leaq -RBP(%rsp),%rdi /* arg1 for handler */
332 * irq_count is used to check if a CPU is already on an interrupt stack
333 * or not. While this is essentially redundant with preempt_count it is
334 * a little cheaper to use a separate counter in the PDA (short of
335 * moving irq_enter into assembly, which would be too much work)
337 1: incl PER_CPU_VAR(irq_count)
339 mov PER_CPU_VAR(irq_stack_ptr),%rsp
340 CFI_DEF_CFA_REGISTER rsi
342 2: /* Store previous stack value */
344 CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
345 0x77 /* DW_OP_breg7 */, 0, \
346 0x06 /* DW_OP_deref */, \
347 0x08 /* DW_OP_const1u */, SS+8-RBP, \
348 0x22 /* DW_OP_plus */
349 /* We entered an interrupt context - irqs are off: */
354 PARTIAL_FRAME 1 REST_SKIP+8
355 movq 5*8+16(%rsp), %r11 /* save return address */
362 movq %r11, 8(%rsp) /* return address */
363 FIXUP_TOP_OF_STACK %r11, 16
368 /* save complete stack frame */
369 .pushsection .kprobes.text, "ax"
371 * Return: ebx=0: needs swapgs but not SWITCH_USER_CR3 in paranoid_exit
372 * ebx=1: needs neither swapgs nor SWITCH_USER_CR3 in paranoid_exit
373 * ebx=2: needs both swapgs and SWITCH_USER_CR3 in paranoid_exit
374 * ebx=3: needs SWITCH_USER_CR3 but not swapgs in paranoid_exit
395 movl $MSR_GS_BASE,%ecx
398 js 1f /* negative -> in kernel */
402 #ifdef CONFIG_PAGE_TABLE_ISOLATION
404 * We might have come in between a swapgs and a SWITCH_KERNEL_CR3
405 * on entry, or between a SWITCH_USER_CR3 and a swapgs on exit.
406 * Do a conditional SWITCH_KERNEL_CR3: this could safely be done
407 * unconditionally, but we need to find out whether the reverse
408 * should be done on return (conveyed to paranoid_exit in %ebx).
410 ALTERNATIVE "jmp 2f", "movq %cr3, %rax", X86_FEATURE_KAISER
411 testl $KAISER_SHADOW_PGD_OFFSET, %eax
414 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax
415 /* If PCID enabled, set X86_CR3_PCID_NOFLUSH_BIT */
416 ALTERNATIVE "", "bts $63, %rax", X86_FEATURE_PCID
426 * A newly forked process directly context switches into this address.
428 * rdi: prev task we switched from
433 LOCK ; btr $TIF_FORK,TI_flags(%r8)
435 pushq_cfi kernel_eflags(%rip)
436 popfq_cfi # reset kernel eflags
438 call schedule_tail # rdi: 'prev' task parameter
440 GET_THREAD_INFO(%rcx)
444 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
445 je int_ret_from_sys_call
448 * By the time we get here, we have no idea whether our pt_regs,
449 * ti flags, and ti status came from the 64-bit SYSCALL fast path,
450 * the slow path, or one of the ia32entry paths.
451 * Use int_ret_from_sys_call to return, since it can safely handle
454 jmp int_ret_from_sys_call
460 * System call entry. Up to 6 arguments in registers are supported.
462 * SYSCALL does not save anything on the stack and does not change the
468 * rax system call number
470 * rcx return address for syscall/sysret, C arg3
473 * r10 arg3 (--> moved to rcx for C)
476 * r11 eflags for syscall/sysret, temporary for C
477 * r12-r15,rbp,rbx saved by C code, not touched.
479 * Interrupts are off on entry.
480 * Only called from user space.
482 * XXX if we had a free scratch register we could save the RSP into the stack frame
483 * and report it properly in ps. Unfortunately we haven't.
485 * When user can change the frames always force IRET. That is because
486 * it deals with uncanonical addresses better. SYSRET has trouble
487 * with them due to bugs in both AMD and Intel CPUs.
493 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
495 /*CFI_REGISTER rflags,r11*/
497 SWITCH_KERNEL_CR3_NO_STACK
499 * A hypervisor implementation might want to use a label
500 * after the swapgs, so that it can do the swapgs
501 * for the guest and jump here on syscall.
503 ENTRY(system_call_after_swapgs)
505 movq %rsp,PER_CPU_VAR(old_rsp)
506 movq PER_CPU_VAR(kernel_stack),%rsp
508 * No need to follow this irqs off/on section - it's straight
511 ENABLE_INTERRUPTS(CLBR_NONE)
513 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
514 movq %rcx,RIP-ARGOFFSET(%rsp)
515 CFI_REL_OFFSET rip,RIP-ARGOFFSET
516 GET_THREAD_INFO(%rcx)
517 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
519 system_call_fastpath:
520 cmpq $NR_syscalls, %rax
522 sbb %rcx, %rcx /* array_index_mask_nospec() */
525 #ifdef CONFIG_RETPOLINE
526 movq sys_call_table(, %rax, 8), %rax
527 call __x86_indirect_thunk_rax
529 call *sys_call_table(,%rax,8) # XXX: rip relative
531 movq %rax,RAX-ARGOFFSET(%rsp)
533 * Syscall return path ending with SYSRET (fast path)
534 * Has incomplete stack frame and undefined top of stack.
537 movl $_TIF_ALLWORK_MASK,%edi
541 GET_THREAD_INFO(%rcx)
542 DISABLE_INTERRUPTS(CLBR_NONE)
544 movl TI_flags(%rcx),%edx
549 * sysretq will re-enable interrupts:
552 movq RIP-ARGOFFSET(%rsp),%rcx
554 RESTORE_ARGS 1,-ARG_SKIP,0
555 /*CFI_REGISTER rflags,r11*/
557 * This opens a window where we have a user CR3, but are
558 * running in the kernel. This makes using the CS
559 * register useless for telling whether or not we need to
560 * switch CR3 in NMIs. Normal interrupts are OK because
564 movq PER_CPU_VAR(old_rsp), %rsp
568 /* Handle reschedules */
569 /* edx: work, edi: workmask */
571 bt $TIF_NEED_RESCHED,%edx
574 ENABLE_INTERRUPTS(CLBR_NONE)
580 /* Handle a signal */
583 ENABLE_INTERRUPTS(CLBR_NONE)
584 #ifdef CONFIG_AUDITSYSCALL
585 bt $TIF_SYSCALL_AUDIT,%edx
589 * We have a signal, or exit tracing or single-step.
590 * These all wind up with the iret return path anyway,
591 * so just join that path right now.
593 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
594 jmp int_check_syscall_exit_work
597 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
598 jmp ret_from_sys_call
600 #ifdef CONFIG_AUDITSYSCALL
602 * Fast path for syscall audit without full syscall trace.
603 * We just call audit_syscall_entry() directly, and then
604 * jump back to the normal fast path.
607 movq %r10,%r9 /* 6th arg: 4th syscall arg */
608 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
609 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
610 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
611 movq %rax,%rsi /* 2nd arg: syscall number */
612 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
613 call audit_syscall_entry
614 LOAD_ARGS 0 /* reload call-clobbered registers */
615 jmp system_call_fastpath
618 * Return fast path for syscall audit. Call audit_syscall_exit()
619 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
623 movq RAX-ARGOFFSET(%rsp),%rsi /* second arg, syscall return value */
624 cmpq $0,%rsi /* is it < 0? */
625 setl %al /* 1 if so, 0 if not */
626 movzbl %al,%edi /* zero-extend that into %edi */
627 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
628 call audit_syscall_exit
629 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
631 #endif /* CONFIG_AUDITSYSCALL */
633 /* Do syscall tracing */
635 #ifdef CONFIG_AUDITSYSCALL
636 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
640 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
641 FIXUP_TOP_OF_STACK %rdi
643 call syscall_trace_enter
645 * Reload arg registers from stack in case ptrace changed them.
646 * We don't reload %rax because syscall_trace_enter() returned
647 * the value it wants us to use in the table lookup.
649 LOAD_ARGS ARGOFFSET, 1
651 cmpq $NR_syscalls, %rax
652 jae int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
653 sbb %rcx, %rcx /* array_index_mask_nospec() */
655 movq %r10,%rcx /* fixup for C */
656 #ifdef CONFIG_RETPOLINE
657 movq sys_call_table(, %rax, 8), %rax
658 call __x86_indirect_thunk_rax
660 call *sys_call_table(,%rax,8)
662 movq %rax,RAX-ARGOFFSET(%rsp)
663 /* Use IRET because user could have changed frame */
666 * Syscall return path ending with IRET.
667 * Has correct top of stack, but partial stack frame.
669 GLOBAL(int_ret_from_sys_call)
670 DISABLE_INTERRUPTS(CLBR_NONE)
672 testl $3,CS-ARGOFFSET(%rsp)
673 je retint_restore_args
674 movl $_TIF_ALLWORK_MASK,%edi
675 /* edi: mask to check */
676 GLOBAL(int_with_check)
678 GET_THREAD_INFO(%rcx)
679 movl TI_flags(%rcx),%edx
682 andl $~TS_COMPAT,TI_status(%rcx)
685 /* Either reschedule or signal or syscall exit tracking needed. */
686 /* First do a reschedule test. */
687 /* edx: work, edi: workmask */
689 bt $TIF_NEED_RESCHED,%edx
692 ENABLE_INTERRUPTS(CLBR_NONE)
696 DISABLE_INTERRUPTS(CLBR_NONE)
700 /* handle signals and tracing -- both require a full stack frame */
703 ENABLE_INTERRUPTS(CLBR_NONE)
704 int_check_syscall_exit_work:
706 /* Check for syscall exit trace */
707 testl $_TIF_WORK_SYSCALL_EXIT,%edx
710 leaq 8(%rsp),%rdi # &ptregs -> arg1
711 call syscall_trace_leave
713 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
717 testl $_TIF_DO_NOTIFY_MASK,%edx
719 movq %rsp,%rdi # &ptregs -> arg1
720 xorl %esi,%esi # oldset -> arg2
721 call do_notify_resume
722 1: movl $_TIF_WORK_MASK,%edi
725 DISABLE_INTERRUPTS(CLBR_NONE)
732 * Certain special system calls that need to save a complete full stack frame.
734 .macro PTREGSCALL label,func,arg
736 PARTIAL_FRAME 1 8 /* offset 8: return address */
737 subq $REST_SKIP, %rsp
738 CFI_ADJUST_CFA_OFFSET REST_SKIP
740 DEFAULT_FRAME 0 8 /* offset 8: return address */
741 leaq 8(%rsp), \arg /* pt_regs pointer */
743 jmp ptregscall_common
748 PTREGSCALL stub_clone, sys_clone, %r8
749 PTREGSCALL stub_fork, sys_fork, %rdi
750 PTREGSCALL stub_vfork, sys_vfork, %rdi
751 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
752 PTREGSCALL stub_iopl, sys_iopl, %rsi
754 ENTRY(ptregscall_common)
755 DEFAULT_FRAME 1 8 /* offset 8: return address */
756 RESTORE_TOP_OF_STACK %r11, 8
757 movq_cfi_restore R15+8, r15
758 movq_cfi_restore R14+8, r14
759 movq_cfi_restore R13+8, r13
760 movq_cfi_restore R12+8, r12
761 movq_cfi_restore RBP+8, rbp
762 movq_cfi_restore RBX+8, rbx
763 ret $REST_SKIP /* pop extended registers */
765 END(ptregscall_common)
772 FIXUP_TOP_OF_STACK %r11
775 RESTORE_TOP_OF_STACK %r11
778 jmp int_ret_from_sys_call
783 * sigreturn is special because it needs to restore all registers on return.
784 * This cannot be done with SYSRET, so use the IRET return path instead.
786 ENTRY(stub_rt_sigreturn)
792 FIXUP_TOP_OF_STACK %r11
793 call sys_rt_sigreturn
794 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
796 jmp int_ret_from_sys_call
798 END(stub_rt_sigreturn)
801 * Build the entry stubs and pointer table with some assembler magic.
802 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
803 * single cache line on all modern x86 implementations.
805 .section .init.rodata,"a"
809 .p2align CONFIG_X86_L1_CACHE_SHIFT
810 ENTRY(irq_entries_start)
812 vector=FIRST_EXTERNAL_VECTOR
813 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
816 .if vector < NR_VECTORS
817 .if vector <> FIRST_EXTERNAL_VECTOR
818 CFI_ADJUST_CFA_OFFSET -8
820 1: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
821 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
830 2: jmp common_interrupt
833 END(irq_entries_start)
840 * Interrupt entry/exit.
842 * Interrupt entry points save only callee clobbered registers in fast path.
844 * Entry runs with interrupts off.
847 /* 0(%rsp): ~(interrupt number) */
848 .macro interrupt func
849 /* reserve pt_regs for scratch regs and rbp */
850 subq $ORIG_RAX-RBP, %rsp
851 CFI_ADJUST_CFA_OFFSET ORIG_RAX-RBP
857 * Interrupt entry/exit should be protected against kprobes
859 .pushsection .kprobes.text, "ax"
861 * The interrupt stubs push (~vector+0x80) onto the stack and
862 * then jump to common_interrupt.
864 .p2align CONFIG_X86_L1_CACHE_SHIFT
867 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
869 /* 0(%rsp): old_rsp-ARGOFFSET */
871 DISABLE_INTERRUPTS(CLBR_NONE)
873 decl PER_CPU_VAR(irq_count)
875 /* Restore saved previous stack */
877 CFI_DEF_CFA_REGISTER rsi
878 leaq ARGOFFSET-RBP(%rsi), %rsp
879 CFI_DEF_CFA_REGISTER rsp
880 CFI_ADJUST_CFA_OFFSET RBP-ARGOFFSET
883 GET_THREAD_INFO(%rcx)
884 testl $3,CS-ARGOFFSET(%rsp)
887 /* Interrupt came from user space */
889 * Has a correct top of stack, but a partial stack frame
890 * %rcx: thread info. Interrupts off.
892 retint_with_reschedule:
893 movl $_TIF_WORK_MASK,%edi
896 movl TI_flags(%rcx),%edx
901 retint_swapgs: /* return to user-space */
903 * The iretq could re-enable interrupts:
905 DISABLE_INTERRUPTS(CLBR_ANY)
908 * This opens a window where we have a user CR3, but are
909 * running in the kernel. This makes using the CS
910 * register useless for telling whether or not we need to
911 * switch CR3 in NMIs. Normal interrupts are OK because
918 retint_restore_args: /* return to kernel space */
919 DISABLE_INTERRUPTS(CLBR_ANY)
921 * The iretq could re-enable interrupts:
932 * Are we returning to a stack segment from the LDT? Note: in
933 * 64-bit mode SS:RSP on the exception stack is always valid.
935 #ifdef CONFIG_X86_ESPFIX64
936 testb $4,(SS-RIP)(%rsp)
937 jnz native_irq_return_ldt
940 .global native_irq_return_iret
941 native_irq_return_iret:
943 * This may fault. Non-paranoid faults on return to userspace are
944 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
945 * Double-faults due to espfix64 are handled in do_double_fault.
946 * Other faults here are fatal.
950 #ifdef CONFIG_X86_ESPFIX64
951 native_irq_return_ldt:
956 movq PER_CPU_VAR(espfix_waddr),%rdi
957 movq %rax,(0*8)(%rdi) /* RAX */
958 movq (2*8)(%rsp),%rax /* RIP */
959 movq %rax,(1*8)(%rdi)
960 movq (3*8)(%rsp),%rax /* CS */
961 movq %rax,(2*8)(%rdi)
962 movq (4*8)(%rsp),%rax /* RFLAGS */
963 movq %rax,(3*8)(%rdi)
964 movq (6*8)(%rsp),%rax /* SS */
965 movq %rax,(5*8)(%rdi)
966 movq (5*8)(%rsp),%rax /* RSP */
967 movq %rax,(4*8)(%rdi)
968 andl $0xffff0000,%eax
970 orq PER_CPU_VAR(espfix_stack),%rax
975 jmp native_irq_return_iret
978 /* edi: workmask, edx: work */
981 bt $TIF_NEED_RESCHED,%edx
984 ENABLE_INTERRUPTS(CLBR_NONE)
988 GET_THREAD_INFO(%rcx)
989 DISABLE_INTERRUPTS(CLBR_NONE)
994 testl $_TIF_DO_NOTIFY_MASK,%edx
997 ENABLE_INTERRUPTS(CLBR_NONE)
999 movq $-1,ORIG_RAX(%rsp)
1000 xorl %esi,%esi # oldset
1001 movq %rsp,%rdi # &pt_regs
1002 call do_notify_resume
1004 DISABLE_INTERRUPTS(CLBR_NONE)
1006 GET_THREAD_INFO(%rcx)
1007 jmp retint_with_reschedule
1009 #ifdef CONFIG_PREEMPT
1010 /* Returning to kernel space. Check if we need preemption */
1011 /* rcx: threadinfo. interrupts off. */
1012 ENTRY(retint_kernel)
1013 cmpl $0,TI_preempt_count(%rcx)
1014 jnz retint_restore_args
1015 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
1016 jnc retint_restore_args
1017 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
1018 jnc retint_restore_args
1019 call preempt_schedule_irq
1023 END(common_interrupt)
1026 * End of kprobes section
1033 .macro apicinterrupt num sym do_sym
1044 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
1045 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
1046 apicinterrupt REBOOT_VECTOR \
1047 reboot_interrupt smp_reboot_interrupt
1050 #ifdef CONFIG_X86_UV
1051 apicinterrupt UV_BAU_MESSAGE \
1052 uv_bau_message_intr1 uv_bau_message_interrupt
1054 apicinterrupt LOCAL_TIMER_VECTOR \
1055 apic_timer_interrupt smp_apic_timer_interrupt
1056 apicinterrupt X86_PLATFORM_IPI_VECTOR \
1057 x86_platform_ipi smp_x86_platform_ipi
1060 .irp idx,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, \
1061 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
1062 .if NUM_INVALIDATE_TLB_VECTORS > \idx
1063 apicinterrupt (INVALIDATE_TLB_VECTOR_START)+\idx \
1064 invalidate_interrupt\idx smp_invalidate_interrupt
1069 apicinterrupt THRESHOLD_APIC_VECTOR \
1070 threshold_interrupt smp_threshold_interrupt
1071 apicinterrupt THERMAL_APIC_VECTOR \
1072 thermal_interrupt smp_thermal_interrupt
1075 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
1076 call_function_single_interrupt smp_call_function_single_interrupt
1077 apicinterrupt CALL_FUNCTION_VECTOR \
1078 call_function_interrupt smp_call_function_interrupt
1079 apicinterrupt RESCHEDULE_VECTOR \
1080 reschedule_interrupt smp_reschedule_interrupt
1083 apicinterrupt ERROR_APIC_VECTOR \
1084 error_interrupt smp_error_interrupt
1085 apicinterrupt SPURIOUS_APIC_VECTOR \
1086 spurious_interrupt smp_spurious_interrupt
1088 #ifdef CONFIG_IRQ_WORK
1089 apicinterrupt IRQ_WORK_VECTOR \
1090 irq_work_interrupt smp_irq_work_interrupt
1094 * Exception entry points.
1096 .macro zeroentry sym do_sym
1099 PARAVIRT_ADJUST_EXCEPTION_FRAME
1100 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1101 subq $ORIG_RAX-R15, %rsp
1102 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1105 movq %rsp,%rdi /* pt_regs pointer */
1106 xorl %esi,%esi /* no error code */
1108 jmp error_exit /* %ebx: no swapgs flag */
1113 .macro paranoidzeroentry sym do_sym
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 */
1125 jmp paranoid_exit /* %ebx: no swapgs flag */
1130 #define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
1131 .macro paranoidzeroentry_ist sym do_sym ist
1134 PARAVIRT_ADJUST_EXCEPTION_FRAME
1135 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1136 subq $ORIG_RAX-R15, %rsp
1137 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1140 movq %rsp,%rdi /* pt_regs pointer */
1141 xorl %esi,%esi /* no error code */
1142 subq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1144 addq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1145 jmp paranoid_exit /* %ebx: no swapgs flag */
1150 .macro errorentry sym do_sym
1153 PARAVIRT_ADJUST_EXCEPTION_FRAME
1154 subq $ORIG_RAX-R15, %rsp
1155 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1158 movq %rsp,%rdi /* pt_regs pointer */
1159 movq ORIG_RAX(%rsp),%rsi /* get error code */
1160 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1162 jmp error_exit /* %ebx: no swapgs flag */
1167 /* error code is on the stack already */
1168 .macro paranoiderrorentry sym do_sym
1171 PARAVIRT_ADJUST_EXCEPTION_FRAME
1172 subq $ORIG_RAX-R15, %rsp
1173 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1177 movq %rsp,%rdi /* pt_regs pointer */
1178 movq ORIG_RAX(%rsp),%rsi /* get error code */
1179 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1181 jmp paranoid_exit /* %ebx: no swapgs flag */
1186 zeroentry divide_error do_divide_error
1187 zeroentry overflow do_overflow
1188 zeroentry bounds do_bounds
1189 zeroentry invalid_op do_invalid_op
1190 zeroentry device_not_available do_device_not_available
1191 paranoiderrorentry double_fault do_double_fault
1192 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1193 errorentry invalid_TSS do_invalid_TSS
1194 errorentry segment_not_present do_segment_not_present
1195 zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1196 zeroentry coprocessor_error do_coprocessor_error
1197 errorentry alignment_check do_alignment_check
1198 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1201 /* Reload gs selector with exception handling */
1202 /* edi: new selector */
1203 ENTRY(native_load_gs_index)
1206 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1210 2: mfence /* workaround */
1215 END(native_load_gs_index)
1217 .section __ex_table,"a"
1219 .quad gs_change,bad_gs
1221 .section .fixup,"ax"
1222 /* running with kernelgs */
1224 SWAPGS /* switch back to user gs */
1230 ENTRY(kernel_thread_helper)
1231 pushq $0 # fake return address
1234 * Here we are in the child and the registers are set as they were
1235 * at kernel_thread() invocation in the parent.
1241 ud2 # padding for call trace
1243 END(kernel_thread_helper)
1246 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1248 * C extern interface:
1249 * extern long execve(const char *name, char **argv, char **envp)
1251 * asm input arguments:
1252 * rdi: name, rsi: argv, rdx: envp
1254 * We want to fallback into:
1255 * extern long sys_execve(const char *name, char **argv,char **envp, struct pt_regs *regs)
1257 * do_sys_execve asm fallback arguments:
1258 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1260 ENTRY(kernel_execve)
1266 movq %rax, RAX(%rsp)
1269 je int_ret_from_sys_call
1276 /* Call softirq on interrupt stack. Interrupts are off. */
1280 CFI_REL_OFFSET rbp,0
1282 CFI_DEF_CFA_REGISTER rbp
1283 incl PER_CPU_VAR(irq_count)
1284 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1285 push %rbp # backlink for old unwinder
1289 CFI_DEF_CFA_REGISTER rsp
1290 CFI_ADJUST_CFA_OFFSET -8
1291 decl PER_CPU_VAR(irq_count)
1297 zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
1300 * A note on the "critical region" in our callback handler.
1301 * We want to avoid stacking callback handlers due to events occurring
1302 * during handling of the last event. To do this, we keep events disabled
1303 * until we've done all processing. HOWEVER, we must enable events before
1304 * popping the stack frame (can't be done atomically) and so it would still
1305 * be possible to get enough handler activations to overflow the stack.
1306 * Although unlikely, bugs of that kind are hard to track down, so we'd
1307 * like to avoid the possibility.
1308 * So, on entry to the handler we detect whether we interrupted an
1309 * existing activation in its critical region -- if so, we pop the current
1310 * activation and restart the handler using the previous one.
1312 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1315 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1316 * see the correct pointer to the pt_regs
1318 movq %rdi, %rsp # we don't return, adjust the stack frame
1321 11: incl PER_CPU_VAR(irq_count)
1323 CFI_DEF_CFA_REGISTER rbp
1324 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1325 pushq %rbp # backlink for old unwinder
1326 call xen_evtchn_do_upcall
1328 CFI_DEF_CFA_REGISTER rsp
1329 decl PER_CPU_VAR(irq_count)
1332 END(xen_do_hypervisor_callback)
1335 * Hypervisor uses this for application faults while it executes.
1336 * We get here for two reasons:
1337 * 1. Fault while reloading DS, ES, FS or GS
1338 * 2. Fault while executing IRET
1339 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1340 * registers that could be reloaded and zeroed the others.
1341 * Category 2 we fix up by killing the current process. We cannot use the
1342 * normal Linux return path in this case because if we use the IRET hypercall
1343 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1344 * We distinguish between categories by comparing each saved segment register
1345 * with its current contents: any discrepancy means we in category 1.
1347 ENTRY(xen_failsafe_callback)
1349 /*CFI_REL_OFFSET gs,GS*/
1350 /*CFI_REL_OFFSET fs,FS*/
1351 /*CFI_REL_OFFSET es,ES*/
1352 /*CFI_REL_OFFSET ds,DS*/
1353 CFI_REL_OFFSET r11,8
1354 CFI_REL_OFFSET rcx,0
1368 /* All segments match their saved values => Category 2 (Bad IRET). */
1374 CFI_ADJUST_CFA_OFFSET -0x30
1375 pushq_cfi $0 /* RIP */
1378 jmp general_protection
1380 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1386 CFI_ADJUST_CFA_OFFSET -0x30
1387 pushq_cfi $-1 /* orig_ax = -1 => not a system call */
1391 END(xen_failsafe_callback)
1393 apicinterrupt XEN_HVM_EVTCHN_CALLBACK \
1394 xen_hvm_callback_vector xen_evtchn_do_upcall
1396 #endif /* CONFIG_XEN */
1399 * Some functions should be protected against kprobes
1401 .pushsection .kprobes.text, "ax"
1403 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1404 zeroentry int3 do_int3
1405 errorentry stack_segment do_stack_segment
1407 zeroentry xen_debug do_debug
1408 zeroentry xen_int3 do_int3
1409 errorentry xen_stack_segment do_stack_segment
1411 errorentry general_protection do_general_protection
1412 errorentry page_fault do_page_fault
1413 #ifdef CONFIG_KVM_GUEST
1414 errorentry async_page_fault do_async_page_fault
1416 #ifdef CONFIG_X86_MCE
1417 paranoidzeroentry machine_check do_mce
1421 * "Paranoid" exit path from exception stack.
1422 * Paranoid because this is used by NMIs and cannot take
1423 * any kernel state for granted.
1424 * We don't do kernel preemption checks here, because only
1425 * NMI should be common and it does not enable IRQs and
1426 * cannot get reschedule ticks.
1428 * "trace" is 0 for the NMI handler only, because irq-tracing
1429 * is fundamentally NMI-unsafe. (we cannot change the soft and
1430 * hard flags at once, atomically)
1433 * On entry: ebx=0: needs swapgs but not SWITCH_USER_CR3
1434 * ebx=1: needs neither swapgs nor SWITCH_USER_CR3
1435 * ebx=2: needs both swapgs and SWITCH_USER_CR3
1436 * ebx=3: needs SWITCH_USER_CR3 but not swapgs
1438 ENTRY(paranoid_exit)
1440 DISABLE_INTERRUPTS(CLBR_NONE)
1442 movq %rbx, %r12 /* paranoid_userspace uses %ebx */
1444 jnz paranoid_userspace
1446 movq %r12, %rbx /* restore after paranoid_userspace */
1448 #ifdef CONFIG_PAGE_TABLE_ISOLATION
1449 /* No ALTERNATIVE for X86_FEATURE_KAISER: save_paranoid sets %ebx */
1450 testl $2, %ebx /* SWITCH_USER_CR3 needed? */
1451 jz paranoid_exit_no_switch
1453 paranoid_exit_no_switch:
1455 testl $1, %ebx /* swapgs needed? */
1456 jnz paranoid_exit_no_swapgs
1458 paranoid_exit_no_swapgs:
1463 GET_THREAD_INFO(%rcx)
1464 movl TI_flags(%rcx),%ebx
1465 andl $_TIF_WORK_MASK,%ebx
1467 movq %rsp,%rdi /* &pt_regs */
1469 movq %rax,%rsp /* switch stack for scheduling */
1470 testl $_TIF_NEED_RESCHED,%ebx
1471 jnz paranoid_schedule
1472 movl %ebx,%edx /* arg3: thread flags */
1474 ENABLE_INTERRUPTS(CLBR_NONE)
1475 xorl %esi,%esi /* arg2: oldset */
1476 movq %rsp,%rdi /* arg1: &pt_regs */
1477 call do_notify_resume
1478 DISABLE_INTERRUPTS(CLBR_NONE)
1480 jmp paranoid_userspace
1483 ENABLE_INTERRUPTS(CLBR_ANY)
1485 DISABLE_INTERRUPTS(CLBR_ANY)
1487 jmp paranoid_userspace
1492 * Exception entry point. This expects an error code/orig_rax on the stack.
1493 * returns in "no swapgs flag" in %ebx.
1497 CFI_ADJUST_CFA_OFFSET 15*8
1498 /* oldrax contains error code */
1516 * error_entry() always returns with a kernel gsbase and
1517 * CR3. We must also have a kernel CR3/gsbase before
1518 * calling TRACE_IRQS_*. Just unconditionally switch to
1519 * the kernel CR3 here.
1524 je error_kernelspace
1532 * There are two places in the kernel that can potentially fault with
1533 * usergs. Handle them here. B stepping K8s sometimes report a
1534 * truncated RIP for IRET exceptions returning to compat mode. Check
1535 * for these here too.
1539 leaq native_irq_return_iret(%rip),%rcx
1540 cmpq %rcx,RIP+8(%rsp)
1542 movl %ecx,%eax /* zero extend */
1543 cmpq %rax,RIP+8(%rsp)
1545 cmpq $gs_change,RIP+8(%rsp)
1550 /* Fix truncated RIP */
1551 movq %rcx,RIP+8(%rsp)
1559 decl %ebx /* Return to usergs */
1565 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1570 DISABLE_INTERRUPTS(CLBR_NONE)
1572 GET_THREAD_INFO(%rcx)
1575 LOCKDEP_SYS_EXIT_IRQ
1576 movl TI_flags(%rcx),%edx
1577 movl $_TIF_WORK_MASK,%edi
1585 /* runs on exception stack */
1589 * Fix up the exception frame if we're on Xen.
1590 * PARAVIRT_ADJUST_EXCEPTION_FRAME is guaranteed to push at most
1591 * one value to the stack on native, so it may clobber the rdx
1592 * scratch slot, but it won't clobber any of the important
1595 * Xen is a different story, because the Xen frame itself overlaps
1596 * the "NMI executing" variable.
1598 PARAVIRT_ADJUST_EXCEPTION_FRAME
1601 subq $ORIG_RAX-R15, %rsp
1602 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1605 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1609 #ifdef CONFIG_TRACE_IRQFLAGS
1610 /* paranoidexit; without TRACE_IRQS_OFF */
1611 /* ebx: no-swapgs and kaiser-switch-cr3 flag */
1612 DISABLE_INTERRUPTS(CLBR_NONE)
1613 movq %rbx, %r12 /* nmi_userspace uses %ebx */
1617 movq %r12, %rbx /* restore after nmi_userspace */
1618 #ifdef CONFIG_PAGE_TABLE_ISOLATION
1619 /* No ALTERNATIVE for X86_FEATURE_KAISER: save_paranoid sets %ebx */
1620 testl $2, %ebx /* SWITCH_USER_CR3 needed? */
1621 jz nmi_exit_no_switch
1625 testl $1, %ebx /* swapgs needed? */
1626 jnz nmi_exit_no_swapgs
1633 GET_THREAD_INFO(%rcx)
1634 movl TI_flags(%rcx),%ebx
1635 andl $_TIF_WORK_MASK,%ebx
1637 movq %rsp,%rdi /* &pt_regs */
1639 movq %rax,%rsp /* switch stack for scheduling */
1640 testl $_TIF_NEED_RESCHED,%ebx
1642 movl %ebx,%edx /* arg3: thread flags */
1643 ENABLE_INTERRUPTS(CLBR_NONE)
1644 xorl %esi,%esi /* arg2: oldset */
1645 movq %rsp,%rdi /* arg1: &pt_regs */
1646 call do_notify_resume
1647 DISABLE_INTERRUPTS(CLBR_NONE)
1650 ENABLE_INTERRUPTS(CLBR_ANY)
1652 DISABLE_INTERRUPTS(CLBR_ANY)
1661 ENTRY(ignore_sysret)
1669 * End of kprobes section