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 * - idtentry - 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/percpu.h>
58 #include <asm/context_tracking.h>
60 #include <asm/pgtable_types.h>
61 #include <linux/err.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"
73 #ifndef CONFIG_PREEMPT
74 #define retint_kernel retint_restore_args
77 #ifdef CONFIG_PARAVIRT
78 ENTRY(native_usergs_sysret64)
81 ENDPROC(native_usergs_sysret64)
82 #endif /* CONFIG_PARAVIRT */
85 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
86 #ifdef CONFIG_TRACE_IRQFLAGS
87 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
95 * When dynamic function tracer is enabled it will add a breakpoint
96 * to all locations that it is about to modify, sync CPUs, update
97 * all the code, sync CPUs, then remove the breakpoints. In this time
98 * if lockdep is enabled, it might jump back into the debug handler
99 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
101 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
102 * make sure the stack pointer does not get reset back to the top
103 * of the debug stack, and instead just reuses the current stack.
105 #if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
107 .macro TRACE_IRQS_OFF_DEBUG
108 call debug_stack_set_zero
110 call debug_stack_reset
113 .macro TRACE_IRQS_ON_DEBUG
114 call debug_stack_set_zero
116 call debug_stack_reset
119 .macro TRACE_IRQS_IRETQ_DEBUG offset=ARGOFFSET
120 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
127 # define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
128 # define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
129 # define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
133 * C code is not supposed to know about undefined top of stack. Every time
134 * a C function with an pt_regs argument is called from the SYSCALL based
135 * fast path FIXUP_TOP_OF_STACK is needed.
136 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
140 /* %rsp:at FRAMEEND */
141 .macro FIXUP_TOP_OF_STACK tmp offset=0
142 movq PER_CPU_VAR(old_rsp),\tmp
143 movq \tmp,RSP+\offset(%rsp)
144 movq $__USER_DS,SS+\offset(%rsp)
145 movq $__USER_CS,CS+\offset(%rsp)
146 movq $-1,RCX+\offset(%rsp)
147 movq R11+\offset(%rsp),\tmp /* get eflags */
148 movq \tmp,EFLAGS+\offset(%rsp)
151 .macro RESTORE_TOP_OF_STACK tmp offset=0
152 movq RSP+\offset(%rsp),\tmp
153 movq \tmp,PER_CPU_VAR(old_rsp)
154 movq EFLAGS+\offset(%rsp),\tmp
155 movq \tmp,R11+\offset(%rsp)
159 * initial frame state for interrupts (and exceptions without error code)
161 .macro EMPTY_FRAME start=1 offset=0
165 CFI_DEF_CFA rsp,8+\offset
167 CFI_DEF_CFA_OFFSET 8+\offset
172 * initial frame state for interrupts (and exceptions without error code)
174 .macro INTR_FRAME start=1 offset=0
175 EMPTY_FRAME \start, SS+8+\offset-RIP
176 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
177 CFI_REL_OFFSET rsp, RSP+\offset-RIP
178 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
179 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
180 CFI_REL_OFFSET rip, RIP+\offset-RIP
184 * initial frame state for exceptions with error code (and interrupts
185 * with vector already pushed)
187 .macro XCPT_FRAME start=1 offset=0
188 INTR_FRAME \start, RIP+\offset-ORIG_RAX
192 * frame that enables calling into C.
194 .macro PARTIAL_FRAME start=1 offset=0
195 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
196 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
197 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
198 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
199 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
200 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
201 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
202 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
203 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
204 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
208 * frame that enables passing a complete pt_regs to a C function.
210 .macro DEFAULT_FRAME start=1 offset=0
211 PARTIAL_FRAME \start, R11+\offset-R15
212 CFI_REL_OFFSET rbx, RBX+\offset
213 CFI_REL_OFFSET rbp, RBP+\offset
214 CFI_REL_OFFSET r12, R12+\offset
215 CFI_REL_OFFSET r13, R13+\offset
216 CFI_REL_OFFSET r14, R14+\offset
217 CFI_REL_OFFSET r15, R15+\offset
220 /* save partial stack frame */
223 /* start from rbp in pt_regs and jump over */
224 movq_cfi rdi, (RDI-RBP)
225 movq_cfi rsi, (RSI-RBP)
226 movq_cfi rdx, (RDX-RBP)
227 movq_cfi rcx, (RCX-RBP)
228 movq_cfi rax, (RAX-RBP)
229 movq_cfi r8, (R8-RBP)
230 movq_cfi r9, (R9-RBP)
231 movq_cfi r10, (R10-RBP)
232 movq_cfi r11, (R11-RBP)
234 /* Save rbp so that we can unwind from get_irq_regs() */
237 /* Save previous stack value */
240 leaq -RBP(%rsp),%rdi /* arg1 for handler */
241 testl $3, CS-RBP(%rsi)
245 * irq_count is used to check if a CPU is already on an interrupt stack
246 * or not. While this is essentially redundant with preempt_count it is
247 * a little cheaper to use a separate counter in the PDA (short of
248 * moving irq_enter into assembly, which would be too much work)
250 1: incl PER_CPU_VAR(irq_count)
251 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
252 CFI_DEF_CFA_REGISTER rsi
254 /* Store previous stack value */
256 CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
257 0x77 /* DW_OP_breg7 */, 0, \
258 0x06 /* DW_OP_deref */, \
259 0x08 /* DW_OP_const1u */, SS+8-RBP, \
260 0x22 /* DW_OP_plus */
261 /* We entered an interrupt context - irqs are off: */
268 movq %rdi, RDI+8(%rsp)
269 movq %rsi, RSI+8(%rsp)
275 movq %r10, R10+8(%rsp)
276 movq %r11, R11+8(%rsp)
278 movq %rbp, RBP+8(%rsp)
279 movq %r12, R12+8(%rsp)
280 movq %r13, R13+8(%rsp)
281 movq %r14, R14+8(%rsp)
282 movq %r15, R15+8(%rsp)
284 movl $MSR_GS_BASE,%ecx
287 js 1f /* negative -> in kernel */
295 * A newly forked process directly context switches into this address.
297 * rdi: prev task we switched from
302 LOCK ; btr $TIF_FORK,TI_flags(%r8)
305 popfq_cfi # reset kernel eflags
307 call schedule_tail # rdi: 'prev' task parameter
309 GET_THREAD_INFO(%rcx)
313 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
316 testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
317 jnz int_ret_from_sys_call
319 RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
320 jmp ret_from_sys_call # go to the SYSRET fastpath
323 subq $REST_SKIP, %rsp # leave space for volatiles
324 CFI_ADJUST_CFA_OFFSET REST_SKIP
329 jmp int_ret_from_sys_call
334 * System call entry. Up to 6 arguments in registers are supported.
336 * SYSCALL does not save anything on the stack and does not change the
337 * stack pointer. However, it does mask the flags register for us, so
338 * CLD and CLAC are not needed.
343 * rax system call number
345 * rcx return address for syscall/sysret, C arg3
348 * r10 arg3 (--> moved to rcx for C)
351 * r11 eflags for syscall/sysret, temporary for C
352 * r12-r15,rbp,rbx saved by C code, not touched.
354 * Interrupts are off on entry.
355 * Only called from user space.
357 * XXX if we had a free scratch register we could save the RSP into the stack frame
358 * and report it properly in ps. Unfortunately we haven't.
360 * When user can change the frames always force IRET. That is because
361 * it deals with uncanonical addresses better. SYSRET has trouble
362 * with them due to bugs in both AMD and Intel CPUs.
368 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
370 /*CFI_REGISTER rflags,r11*/
373 * A hypervisor implementation might want to use a label
374 * after the swapgs, so that it can do the swapgs
375 * for the guest and jump here on syscall.
377 GLOBAL(system_call_after_swapgs)
379 movq %rsp,PER_CPU_VAR(old_rsp)
380 movq PER_CPU_VAR(kernel_stack),%rsp
382 * No need to follow this irqs off/on section - it's straight
385 ENABLE_INTERRUPTS(CLBR_NONE)
386 SAVE_ARGS 8, 0, rax_enosys=1
387 movq_cfi rax,(ORIG_RAX-ARGOFFSET)
388 movq %rcx,RIP-ARGOFFSET(%rsp)
389 CFI_REL_OFFSET rip,RIP-ARGOFFSET
390 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
392 system_call_fastpath:
393 #if __SYSCALL_MASK == ~0
394 cmpq $__NR_syscall_max,%rax
396 andl $__SYSCALL_MASK,%eax
397 cmpl $__NR_syscall_max,%eax
399 ja ret_from_sys_call /* and return regs->ax */
401 call *sys_call_table(,%rax,8) # XXX: rip relative
402 movq %rax,RAX-ARGOFFSET(%rsp)
404 * Syscall return path ending with SYSRET (fast path)
405 * Has incomplete stack frame and undefined top of stack.
408 movl $_TIF_ALLWORK_MASK,%edi
412 DISABLE_INTERRUPTS(CLBR_NONE)
414 movl TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET),%edx
419 * sysretq will re-enable interrupts:
422 movq RIP-ARGOFFSET(%rsp),%rcx
424 RESTORE_ARGS 1,-ARG_SKIP,0
425 /*CFI_REGISTER rflags,r11*/
426 movq PER_CPU_VAR(old_rsp), %rsp
430 /* Handle reschedules */
431 /* edx: work, edi: workmask */
433 bt $TIF_NEED_RESCHED,%edx
436 ENABLE_INTERRUPTS(CLBR_NONE)
442 /* Handle a signal */
445 ENABLE_INTERRUPTS(CLBR_NONE)
446 #ifdef CONFIG_AUDITSYSCALL
447 bt $TIF_SYSCALL_AUDIT,%edx
451 * We have a signal, or exit tracing or single-step.
452 * These all wind up with the iret return path anyway,
453 * so just join that path right now.
455 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
456 jmp int_check_syscall_exit_work
458 #ifdef CONFIG_AUDITSYSCALL
460 * Return fast path for syscall audit. Call __audit_syscall_exit()
461 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
465 movq RAX-ARGOFFSET(%rsp),%rsi /* second arg, syscall return value */
466 cmpq $-MAX_ERRNO,%rsi /* is it < -MAX_ERRNO? */
467 setbe %al /* 1 if so, 0 if not */
468 movzbl %al,%edi /* zero-extend that into %edi */
469 call __audit_syscall_exit
470 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
472 #endif /* CONFIG_AUDITSYSCALL */
474 /* Do syscall tracing */
476 leaq -REST_SKIP(%rsp), %rdi
477 movq $AUDIT_ARCH_X86_64, %rsi
478 call syscall_trace_enter_phase1
480 jnz tracesys_phase2 /* if needed, run the slow path */
481 LOAD_ARGS 0 /* else restore clobbered regs */
482 jmp system_call_fastpath /* and return to the fast path */
486 FIXUP_TOP_OF_STACK %rdi
488 movq $AUDIT_ARCH_X86_64, %rsi
490 call syscall_trace_enter_phase2
493 * Reload arg registers from stack in case ptrace changed them.
494 * We don't reload %rax because syscall_trace_entry_phase2() returned
495 * the value it wants us to use in the table lookup.
497 LOAD_ARGS ARGOFFSET, 1
499 #if __SYSCALL_MASK == ~0
500 cmpq $__NR_syscall_max,%rax
502 andl $__SYSCALL_MASK,%eax
503 cmpl $__NR_syscall_max,%eax
505 ja int_ret_from_sys_call /* RAX(%rsp) is already set */
506 movq %r10,%rcx /* fixup for C */
507 call *sys_call_table(,%rax,8)
508 movq %rax,RAX-ARGOFFSET(%rsp)
509 /* Use IRET because user could have changed frame */
512 * Syscall return path ending with IRET.
513 * Has correct top of stack, but partial stack frame.
515 GLOBAL(int_ret_from_sys_call)
516 DISABLE_INTERRUPTS(CLBR_NONE)
518 movl $_TIF_ALLWORK_MASK,%edi
519 /* edi: mask to check */
520 GLOBAL(int_with_check)
522 GET_THREAD_INFO(%rcx)
523 movl TI_flags(%rcx),%edx
526 andl $~TS_COMPAT,TI_status(%rcx)
529 /* Either reschedule or signal or syscall exit tracking needed. */
530 /* First do a reschedule test. */
531 /* edx: work, edi: workmask */
533 bt $TIF_NEED_RESCHED,%edx
536 ENABLE_INTERRUPTS(CLBR_NONE)
540 DISABLE_INTERRUPTS(CLBR_NONE)
544 /* handle signals and tracing -- both require a full stack frame */
547 ENABLE_INTERRUPTS(CLBR_NONE)
548 int_check_syscall_exit_work:
550 /* Check for syscall exit trace */
551 testl $_TIF_WORK_SYSCALL_EXIT,%edx
554 leaq 8(%rsp),%rdi # &ptregs -> arg1
555 call syscall_trace_leave
557 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
561 testl $_TIF_DO_NOTIFY_MASK,%edx
563 movq %rsp,%rdi # &ptregs -> arg1
564 xorl %esi,%esi # oldset -> arg2
565 call do_notify_resume
566 1: movl $_TIF_WORK_MASK,%edi
569 DISABLE_INTERRUPTS(CLBR_NONE)
575 .macro FORK_LIKE func
578 popq %r11 /* save return address */
581 pushq %r11 /* put it back on stack */
582 FIXUP_TOP_OF_STACK %r11, 8
583 DEFAULT_FRAME 0 8 /* offset 8: return address */
585 RESTORE_TOP_OF_STACK %r11, 8
586 ret $REST_SKIP /* pop extended registers */
591 .macro FIXED_FRAME label,func
594 PARTIAL_FRAME 0 8 /* offset 8: return address */
595 FIXUP_TOP_OF_STACK %r11, 8-ARGOFFSET
597 RESTORE_TOP_OF_STACK %r11, 8-ARGOFFSET
606 FIXED_FRAME stub_iopl, sys_iopl
613 FIXUP_TOP_OF_STACK %r11
617 jmp int_ret_from_sys_call
626 FIXUP_TOP_OF_STACK %r11
628 RESTORE_TOP_OF_STACK %r11
631 jmp int_ret_from_sys_call
636 * sigreturn is special because it needs to restore all registers on return.
637 * This cannot be done with SYSRET, so use the IRET return path instead.
639 ENTRY(stub_rt_sigreturn)
644 FIXUP_TOP_OF_STACK %r11
645 call sys_rt_sigreturn
646 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
648 jmp int_ret_from_sys_call
650 END(stub_rt_sigreturn)
652 #ifdef CONFIG_X86_X32_ABI
653 ENTRY(stub_x32_rt_sigreturn)
658 FIXUP_TOP_OF_STACK %r11
659 call sys32_x32_rt_sigreturn
660 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
662 jmp int_ret_from_sys_call
664 END(stub_x32_rt_sigreturn)
666 ENTRY(stub_x32_execve)
671 FIXUP_TOP_OF_STACK %r11
672 call compat_sys_execve
673 RESTORE_TOP_OF_STACK %r11
676 jmp int_ret_from_sys_call
680 ENTRY(stub_x32_execveat)
685 FIXUP_TOP_OF_STACK %r11
686 call compat_sys_execveat
687 RESTORE_TOP_OF_STACK %r11
690 jmp int_ret_from_sys_call
692 END(stub_x32_execveat)
697 * Build the entry stubs and pointer table with some assembler magic.
698 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
699 * single cache line on all modern x86 implementations.
701 .section .init.rodata,"a"
705 .p2align CONFIG_X86_L1_CACHE_SHIFT
706 ENTRY(irq_entries_start)
708 vector=FIRST_EXTERNAL_VECTOR
709 .rept (FIRST_SYSTEM_VECTOR-FIRST_EXTERNAL_VECTOR+6)/7
712 .if vector < FIRST_SYSTEM_VECTOR
713 .if vector <> FIRST_EXTERNAL_VECTOR
714 CFI_ADJUST_CFA_OFFSET -8
716 1: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
717 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
726 2: jmp common_interrupt
729 END(irq_entries_start)
736 * Interrupt entry/exit.
738 * Interrupt entry points save only callee clobbered registers in fast path.
740 * Entry runs with interrupts off.
743 /* 0(%rsp): ~(interrupt number) */
744 .macro interrupt func
745 /* reserve pt_regs for scratch regs and rbp */
746 subq $ORIG_RAX-RBP, %rsp
747 CFI_ADJUST_CFA_OFFSET ORIG_RAX-RBP
753 * The interrupt stubs push (~vector+0x80) onto the stack and
754 * then jump to common_interrupt.
756 .p2align CONFIG_X86_L1_CACHE_SHIFT
760 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
762 /* 0(%rsp): old_rsp-ARGOFFSET */
764 DISABLE_INTERRUPTS(CLBR_NONE)
766 decl PER_CPU_VAR(irq_count)
768 /* Restore saved previous stack */
770 CFI_DEF_CFA rsi,SS+8-RBP /* reg/off reset after def_cfa_expr */
771 leaq ARGOFFSET-RBP(%rsi), %rsp
772 CFI_DEF_CFA_REGISTER rsp
773 CFI_ADJUST_CFA_OFFSET RBP-ARGOFFSET
776 GET_THREAD_INFO(%rcx)
777 testl $3,CS-ARGOFFSET(%rsp)
780 /* Interrupt came from user space */
782 * Has a correct top of stack, but a partial stack frame
783 * %rcx: thread info. Interrupts off.
785 retint_with_reschedule:
786 movl $_TIF_WORK_MASK,%edi
789 movl TI_flags(%rcx),%edx
794 retint_swapgs: /* return to user-space */
796 * The iretq could re-enable interrupts:
798 DISABLE_INTERRUPTS(CLBR_ANY)
803 retint_restore_args: /* return to kernel space */
804 DISABLE_INTERRUPTS(CLBR_ANY)
806 * The iretq could re-enable interrupts:
817 * Are we returning to a stack segment from the LDT? Note: in
818 * 64-bit mode SS:RSP on the exception stack is always valid.
820 #ifdef CONFIG_X86_ESPFIX64
821 testb $4,(SS-RIP)(%rsp)
822 jnz native_irq_return_ldt
825 .global native_irq_return_iret
826 native_irq_return_iret:
828 * This may fault. Non-paranoid faults on return to userspace are
829 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
830 * Double-faults due to espfix64 are handled in do_double_fault.
831 * Other faults here are fatal.
835 #ifdef CONFIG_X86_ESPFIX64
836 native_irq_return_ldt:
840 movq PER_CPU_VAR(espfix_waddr),%rdi
841 movq %rax,(0*8)(%rdi) /* RAX */
842 movq (2*8)(%rsp),%rax /* RIP */
843 movq %rax,(1*8)(%rdi)
844 movq (3*8)(%rsp),%rax /* CS */
845 movq %rax,(2*8)(%rdi)
846 movq (4*8)(%rsp),%rax /* RFLAGS */
847 movq %rax,(3*8)(%rdi)
848 movq (6*8)(%rsp),%rax /* SS */
849 movq %rax,(5*8)(%rdi)
850 movq (5*8)(%rsp),%rax /* RSP */
851 movq %rax,(4*8)(%rdi)
852 andl $0xffff0000,%eax
854 orq PER_CPU_VAR(espfix_stack),%rax
858 jmp native_irq_return_iret
861 /* edi: workmask, edx: work */
864 bt $TIF_NEED_RESCHED,%edx
867 ENABLE_INTERRUPTS(CLBR_NONE)
871 GET_THREAD_INFO(%rcx)
872 DISABLE_INTERRUPTS(CLBR_NONE)
877 testl $_TIF_DO_NOTIFY_MASK,%edx
880 ENABLE_INTERRUPTS(CLBR_NONE)
882 movq $-1,ORIG_RAX(%rsp)
883 xorl %esi,%esi # oldset
884 movq %rsp,%rdi # &pt_regs
885 call do_notify_resume
887 DISABLE_INTERRUPTS(CLBR_NONE)
889 GET_THREAD_INFO(%rcx)
890 jmp retint_with_reschedule
892 #ifdef CONFIG_PREEMPT
893 /* Returning to kernel space. Check if we need preemption */
894 /* rcx: threadinfo. interrupts off. */
896 cmpl $0,PER_CPU_VAR(__preempt_count)
897 jnz retint_restore_args
898 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
899 jnc retint_restore_args
900 call preempt_schedule_irq
904 END(common_interrupt)
909 .macro apicinterrupt3 num sym do_sym
921 #ifdef CONFIG_TRACING
922 #define trace(sym) trace_##sym
923 #define smp_trace(sym) smp_trace_##sym
925 .macro trace_apicinterrupt num sym
926 apicinterrupt3 \num trace(\sym) smp_trace(\sym)
929 .macro trace_apicinterrupt num sym do_sym
933 .macro apicinterrupt num sym do_sym
934 apicinterrupt3 \num \sym \do_sym
935 trace_apicinterrupt \num \sym
939 apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR \
940 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
941 apicinterrupt3 REBOOT_VECTOR \
942 reboot_interrupt smp_reboot_interrupt
946 apicinterrupt3 UV_BAU_MESSAGE \
947 uv_bau_message_intr1 uv_bau_message_interrupt
949 apicinterrupt LOCAL_TIMER_VECTOR \
950 apic_timer_interrupt smp_apic_timer_interrupt
951 apicinterrupt X86_PLATFORM_IPI_VECTOR \
952 x86_platform_ipi smp_x86_platform_ipi
954 #ifdef CONFIG_HAVE_KVM
955 apicinterrupt3 POSTED_INTR_VECTOR \
956 kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
959 #ifdef CONFIG_X86_MCE_THRESHOLD
960 apicinterrupt THRESHOLD_APIC_VECTOR \
961 threshold_interrupt smp_threshold_interrupt
964 #ifdef CONFIG_X86_THERMAL_VECTOR
965 apicinterrupt THERMAL_APIC_VECTOR \
966 thermal_interrupt smp_thermal_interrupt
970 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
971 call_function_single_interrupt smp_call_function_single_interrupt
972 apicinterrupt CALL_FUNCTION_VECTOR \
973 call_function_interrupt smp_call_function_interrupt
974 apicinterrupt RESCHEDULE_VECTOR \
975 reschedule_interrupt smp_reschedule_interrupt
978 apicinterrupt ERROR_APIC_VECTOR \
979 error_interrupt smp_error_interrupt
980 apicinterrupt SPURIOUS_APIC_VECTOR \
981 spurious_interrupt smp_spurious_interrupt
983 #ifdef CONFIG_IRQ_WORK
984 apicinterrupt IRQ_WORK_VECTOR \
985 irq_work_interrupt smp_irq_work_interrupt
989 * Exception entry points.
991 #define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
993 .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
996 .if \shift_ist != -1 && \paranoid == 0
997 .error "using shift_ist requires paranoid=1"
1007 PARAVIRT_ADJUST_EXCEPTION_FRAME
1009 .ifeq \has_error_code
1010 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1013 subq $ORIG_RAX-R15, %rsp
1014 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1019 testl $3, CS(%rsp) /* If coming from userspace, switch */
1020 jnz 1f /* stacks. */
1030 .if \shift_ist != -1
1031 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
1037 movq %rsp,%rdi /* pt_regs pointer */
1040 movq ORIG_RAX(%rsp),%rsi /* get error code */
1041 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1043 xorl %esi,%esi /* no error code */
1046 .if \shift_ist != -1
1047 subq $EXCEPTION_STKSZ, INIT_TSS_IST(\shift_ist)
1052 .if \shift_ist != -1
1053 addq $EXCEPTION_STKSZ, INIT_TSS_IST(\shift_ist)
1057 jmp paranoid_exit /* %ebx: no swapgs flag */
1059 jmp error_exit /* %ebx: no swapgs flag */
1065 * Paranoid entry from userspace. Switch stacks and treat it
1066 * as a normal entry. This means that paranoid handlers
1067 * run in real process context if user_mode(regs).
1074 movq %rsp,%rdi /* pt_regs pointer */
1076 movq %rax,%rsp /* switch stack */
1078 movq %rsp,%rdi /* pt_regs pointer */
1081 movq ORIG_RAX(%rsp),%rsi /* get error code */
1082 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1084 xorl %esi,%esi /* no error code */
1089 jmp error_exit /* %ebx: no swapgs flag */
1096 #ifdef CONFIG_TRACING
1097 .macro trace_idtentry sym do_sym has_error_code:req
1098 idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
1099 idtentry \sym \do_sym has_error_code=\has_error_code
1102 .macro trace_idtentry sym do_sym has_error_code:req
1103 idtentry \sym \do_sym has_error_code=\has_error_code
1107 idtentry divide_error do_divide_error has_error_code=0
1108 idtentry overflow do_overflow has_error_code=0
1109 idtentry bounds do_bounds has_error_code=0
1110 idtentry invalid_op do_invalid_op has_error_code=0
1111 idtentry device_not_available do_device_not_available has_error_code=0
1112 idtentry double_fault do_double_fault has_error_code=1 paranoid=2
1113 idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
1114 idtentry invalid_TSS do_invalid_TSS has_error_code=1
1115 idtentry segment_not_present do_segment_not_present has_error_code=1
1116 idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
1117 idtentry coprocessor_error do_coprocessor_error has_error_code=0
1118 idtentry alignment_check do_alignment_check has_error_code=1
1119 idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
1122 /* Reload gs selector with exception handling */
1123 /* edi: new selector */
1124 ENTRY(native_load_gs_index)
1127 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1131 2: mfence /* workaround */
1136 END(native_load_gs_index)
1138 _ASM_EXTABLE(gs_change,bad_gs)
1139 .section .fixup,"ax"
1140 /* running with kernelgs */
1142 SWAPGS /* switch back to user gs */
1148 /* Call softirq on interrupt stack. Interrupts are off. */
1149 ENTRY(do_softirq_own_stack)
1152 CFI_REL_OFFSET rbp,0
1154 CFI_DEF_CFA_REGISTER rbp
1155 incl PER_CPU_VAR(irq_count)
1156 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1157 push %rbp # backlink for old unwinder
1161 CFI_DEF_CFA_REGISTER rsp
1162 CFI_ADJUST_CFA_OFFSET -8
1163 decl PER_CPU_VAR(irq_count)
1166 END(do_softirq_own_stack)
1169 idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
1172 * A note on the "critical region" in our callback handler.
1173 * We want to avoid stacking callback handlers due to events occurring
1174 * during handling of the last event. To do this, we keep events disabled
1175 * until we've done all processing. HOWEVER, we must enable events before
1176 * popping the stack frame (can't be done atomically) and so it would still
1177 * be possible to get enough handler activations to overflow the stack.
1178 * Although unlikely, bugs of that kind are hard to track down, so we'd
1179 * like to avoid the possibility.
1180 * So, on entry to the handler we detect whether we interrupted an
1181 * existing activation in its critical region -- if so, we pop the current
1182 * activation and restart the handler using the previous one.
1184 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1187 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1188 * see the correct pointer to the pt_regs
1190 movq %rdi, %rsp # we don't return, adjust the stack frame
1193 11: incl PER_CPU_VAR(irq_count)
1195 CFI_DEF_CFA_REGISTER rbp
1196 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1197 pushq %rbp # backlink for old unwinder
1198 call xen_evtchn_do_upcall
1200 CFI_DEF_CFA_REGISTER rsp
1201 decl PER_CPU_VAR(irq_count)
1204 END(xen_do_hypervisor_callback)
1207 * Hypervisor uses this for application faults while it executes.
1208 * We get here for two reasons:
1209 * 1. Fault while reloading DS, ES, FS or GS
1210 * 2. Fault while executing IRET
1211 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1212 * registers that could be reloaded and zeroed the others.
1213 * Category 2 we fix up by killing the current process. We cannot use the
1214 * normal Linux return path in this case because if we use the IRET hypercall
1215 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1216 * We distinguish between categories by comparing each saved segment register
1217 * with its current contents: any discrepancy means we in category 1.
1219 ENTRY(xen_failsafe_callback)
1221 /*CFI_REL_OFFSET gs,GS*/
1222 /*CFI_REL_OFFSET fs,FS*/
1223 /*CFI_REL_OFFSET es,ES*/
1224 /*CFI_REL_OFFSET ds,DS*/
1225 CFI_REL_OFFSET r11,8
1226 CFI_REL_OFFSET rcx,0
1240 /* All segments match their saved values => Category 2 (Bad IRET). */
1246 CFI_ADJUST_CFA_OFFSET -0x30
1247 pushq_cfi $0 /* RIP */
1250 jmp general_protection
1252 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1258 CFI_ADJUST_CFA_OFFSET -0x30
1259 pushq_cfi $-1 /* orig_ax = -1 => not a system call */
1263 END(xen_failsafe_callback)
1265 apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1266 xen_hvm_callback_vector xen_evtchn_do_upcall
1268 #endif /* CONFIG_XEN */
1270 #if IS_ENABLED(CONFIG_HYPERV)
1271 apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1272 hyperv_callback_vector hyperv_vector_handler
1273 #endif /* CONFIG_HYPERV */
1275 idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1276 idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1277 idtentry stack_segment do_stack_segment has_error_code=1
1279 idtentry xen_debug do_debug has_error_code=0
1280 idtentry xen_int3 do_int3 has_error_code=0
1281 idtentry xen_stack_segment do_stack_segment has_error_code=1
1283 idtentry general_protection do_general_protection has_error_code=1
1284 trace_idtentry page_fault do_page_fault has_error_code=1
1285 #ifdef CONFIG_KVM_GUEST
1286 idtentry async_page_fault do_async_page_fault has_error_code=1
1288 #ifdef CONFIG_X86_MCE
1289 idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
1293 * "Paranoid" exit path from exception stack. This is invoked
1294 * only on return from non-NMI IST interrupts that came
1295 * from kernel space.
1297 * We may be returning to very strange contexts (e.g. very early
1298 * in syscall entry), so checking for preemption here would
1299 * be complicated. Fortunately, we there's no good reason
1300 * to try to handle preemption here.
1303 /* ebx: no swapgs flag */
1304 ENTRY(paranoid_exit)
1306 DISABLE_INTERRUPTS(CLBR_NONE)
1307 TRACE_IRQS_OFF_DEBUG
1308 testl %ebx,%ebx /* swapgs needed? */
1309 jnz paranoid_restore
1315 TRACE_IRQS_IRETQ_DEBUG 0
1322 * Exception entry point. This expects an error code/orig_rax on the stack.
1323 * returns in "no swapgs flag" in %ebx.
1327 CFI_ADJUST_CFA_OFFSET 15*8
1328 /* oldrax contains error code */
1330 movq %rdi, RDI+8(%rsp)
1331 movq %rsi, RSI+8(%rsp)
1332 movq %rdx, RDX+8(%rsp)
1333 movq %rcx, RCX+8(%rsp)
1334 movq %rax, RAX+8(%rsp)
1335 movq %r8, R8+8(%rsp)
1336 movq %r9, R9+8(%rsp)
1337 movq %r10, R10+8(%rsp)
1338 movq %r11, R11+8(%rsp)
1340 movq %rbp, RBP+8(%rsp)
1341 movq %r12, R12+8(%rsp)
1342 movq %r13, R13+8(%rsp)
1343 movq %r14, R14+8(%rsp)
1344 movq %r15, R15+8(%rsp)
1347 je error_kernelspace
1355 * There are two places in the kernel that can potentially fault with
1356 * usergs. Handle them here. B stepping K8s sometimes report a
1357 * truncated RIP for IRET exceptions returning to compat mode. Check
1358 * for these here too.
1361 CFI_REL_OFFSET rcx, RCX+8
1363 leaq native_irq_return_iret(%rip),%rcx
1364 cmpq %rcx,RIP+8(%rsp)
1366 movl %ecx,%eax /* zero extend */
1367 cmpq %rax,RIP+8(%rsp)
1369 cmpq $gs_change,RIP+8(%rsp)
1374 /* Fix truncated RIP */
1375 movq %rcx,RIP+8(%rsp)
1383 decl %ebx /* Return to usergs */
1389 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1394 DISABLE_INTERRUPTS(CLBR_NONE)
1396 GET_THREAD_INFO(%rcx)
1399 LOCKDEP_SYS_EXIT_IRQ
1400 movl TI_flags(%rcx),%edx
1401 movl $_TIF_WORK_MASK,%edi
1409 * Test if a given stack is an NMI stack or not.
1411 .macro test_in_nmi reg stack nmi_ret normal_ret
1414 subq $EXCEPTION_STKSZ, %\reg
1420 /* runs on exception stack */
1423 PARAVIRT_ADJUST_EXCEPTION_FRAME
1425 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1426 * the iretq it performs will take us out of NMI context.
1427 * This means that we can have nested NMIs where the next
1428 * NMI is using the top of the stack of the previous NMI. We
1429 * can't let it execute because the nested NMI will corrupt the
1430 * stack of the previous NMI. NMI handlers are not re-entrant
1433 * To handle this case we do the following:
1434 * Check the a special location on the stack that contains
1435 * a variable that is set when NMIs are executing.
1436 * The interrupted task's stack is also checked to see if it
1438 * If the variable is not set and the stack is not the NMI
1440 * o Set the special variable on the stack
1441 * o Copy the interrupt frame into a "saved" location on the stack
1442 * o Copy the interrupt frame into a "copy" location on the stack
1443 * o Continue processing the NMI
1444 * If the variable is set or the previous stack is the NMI stack:
1445 * o Modify the "copy" location to jump to the repeate_nmi
1446 * o return back to the first NMI
1448 * Now on exit of the first NMI, we first clear the stack variable
1449 * The NMI stack will tell any nested NMIs at that point that it is
1450 * nested. Then we pop the stack normally with iret, and if there was
1451 * a nested NMI that updated the copy interrupt stack frame, a
1452 * jump will be made to the repeat_nmi code that will handle the second
1456 /* Use %rdx as out temp variable throughout */
1458 CFI_REL_OFFSET rdx, 0
1461 * If %cs was not the kernel segment, then the NMI triggered in user
1462 * space, which means it is definitely not nested.
1464 cmpl $__KERNEL_CS, 16(%rsp)
1468 * Check the special variable on the stack to see if NMIs are
1475 * Now test if the previous stack was an NMI stack.
1476 * We need the double check. We check the NMI stack to satisfy the
1477 * race when the first NMI clears the variable before returning.
1478 * We check the variable because the first NMI could be in a
1479 * breakpoint routine using a breakpoint stack.
1482 test_in_nmi rdx, 4*8(%rsp), nested_nmi, first_nmi
1487 * Do nothing if we interrupted the fixup in repeat_nmi.
1488 * It's about to repeat the NMI handler, so we are fine
1489 * with ignoring this one.
1491 movq $repeat_nmi, %rdx
1494 movq $end_repeat_nmi, %rdx
1499 /* Set up the interrupted NMIs stack to jump to repeat_nmi */
1500 leaq -1*8(%rsp), %rdx
1502 CFI_ADJUST_CFA_OFFSET 1*8
1503 leaq -10*8(%rsp), %rdx
1504 pushq_cfi $__KERNEL_DS
1507 pushq_cfi $__KERNEL_CS
1508 pushq_cfi $repeat_nmi
1510 /* Put stack back */
1512 CFI_ADJUST_CFA_OFFSET -6*8
1518 /* No need to check faults here */
1524 * Because nested NMIs will use the pushed location that we
1525 * stored in rdx, we must keep that space available.
1526 * Here's what our stack frame will look like:
1527 * +-------------------------+
1529 * | original Return RSP |
1530 * | original RFLAGS |
1533 * +-------------------------+
1534 * | temp storage for rdx |
1535 * +-------------------------+
1536 * | NMI executing variable |
1537 * +-------------------------+
1539 * | copied Return RSP |
1543 * +-------------------------+
1545 * | Saved Return RSP |
1549 * +-------------------------+
1551 * +-------------------------+
1553 * The saved stack frame is used to fix up the copied stack frame
1554 * that a nested NMI may change to make the interrupted NMI iret jump
1555 * to the repeat_nmi. The original stack frame and the temp storage
1556 * is also used by nested NMIs and can not be trusted on exit.
1558 /* Do not pop rdx, nested NMIs will corrupt that part of the stack */
1562 /* Set the NMI executing variable on the stack. */
1566 * Leave room for the "copied" frame
1569 CFI_ADJUST_CFA_OFFSET 5*8
1571 /* Copy the stack frame to the Saved frame */
1573 pushq_cfi 11*8(%rsp)
1575 CFI_DEF_CFA_OFFSET SS+8-RIP
1577 /* Everything up to here is safe from nested NMIs */
1580 * If there was a nested NMI, the first NMI's iret will return
1581 * here. But NMIs are still enabled and we can take another
1582 * nested NMI. The nested NMI checks the interrupted RIP to see
1583 * if it is between repeat_nmi and end_repeat_nmi, and if so
1584 * it will just return, as we are about to repeat an NMI anyway.
1585 * This makes it safe to copy to the stack frame that a nested
1590 * Update the stack variable to say we are still in NMI (the update
1591 * is benign for the non-repeat case, where 1 was pushed just above
1592 * to this very stack slot).
1596 /* Make another copy, this one may be modified by nested NMIs */
1598 CFI_ADJUST_CFA_OFFSET -10*8
1600 pushq_cfi -6*8(%rsp)
1603 CFI_DEF_CFA_OFFSET SS+8-RIP
1607 * Everything below this point can be preempted by a nested
1608 * NMI if the first NMI took an exception and reset our iret stack
1609 * so that we repeat another NMI.
1611 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1612 subq $ORIG_RAX-R15, %rsp
1613 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1615 * Use save_paranoid to handle SWAPGS, but no need to use paranoid_exit
1616 * as we should not be calling schedule in NMI context.
1617 * Even with normal interrupts enabled. An NMI should not be
1618 * setting NEED_RESCHED or anything that normal interrupts and
1619 * exceptions might do.
1625 * Save off the CR2 register. If we take a page fault in the NMI then
1626 * it could corrupt the CR2 value. If the NMI preempts a page fault
1627 * handler before it was able to read the CR2 register, and then the
1628 * NMI itself takes a page fault, the page fault that was preempted
1629 * will read the information from the NMI page fault and not the
1630 * origin fault. Save it off and restore it if it changes.
1631 * Use the r12 callee-saved register.
1635 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1640 /* Did the NMI take a page fault? Restore cr2 if it did */
1647 testl %ebx,%ebx /* swapgs needed? */
1652 /* Pop the extra iret frame at once */
1655 /* Clear the NMI executing stack variable */
1661 ENTRY(ignore_sysret)