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>
59 #include <asm/context_tracking.h>
61 #include <asm/pgtable_types.h>
62 #include <linux/err.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
75 #ifdef CC_USING_FENTRY
76 # define function_hook __fentry__
78 # define function_hook mcount
81 #ifdef CONFIG_DYNAMIC_FTRACE
87 /* skip is set if stack has been adjusted */
88 .macro ftrace_caller_setup skip=0
89 MCOUNT_SAVE_FRAME \skip
91 /* Load the ftrace_ops into the 3rd parameter */
92 movq function_trace_op(%rip), %rdx
94 /* Load ip into the first parameter */
96 subq $MCOUNT_INSN_SIZE, %rdi
97 /* Load the parent_ip into the second parameter */
98 #ifdef CC_USING_FENTRY
99 movq SS+16(%rsp), %rsi
106 /* Check if tracing was disabled (quick check) */
107 cmpl $0, function_trace_stop
111 /* regs go into 4th parameter (but make it NULL) */
120 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
121 GLOBAL(ftrace_graph_call)
125 /* This is weak to keep gas from relaxing the jumps */
130 ENTRY(ftrace_regs_caller)
131 /* Save the current flags before compare (in SS location)*/
134 /* Check if tracing was disabled (quick check) */
135 cmpl $0, function_trace_stop
136 jne ftrace_restore_flags
138 /* skip=8 to skip flags saved in SS */
139 ftrace_caller_setup 8
141 /* Save the rest of pt_regs */
150 /* Copy saved flags */
152 movq %rcx, EFLAGS(%rsp)
153 /* Kernel segments */
154 movq $__KERNEL_DS, %rcx
156 movq $__KERNEL_CS, %rcx
158 /* Stack - skipping return address */
159 leaq SS+16(%rsp), %rcx
162 /* regs go into 4th parameter */
165 GLOBAL(ftrace_regs_call)
168 /* Copy flags back to SS, to restore them */
169 movq EFLAGS(%rsp), %rax
172 /* Handlers can change the RIP */
174 movq %rax, SS+8(%rsp)
176 /* restore the rest of pt_regs */
185 /* skip=8 to skip flags saved in SS */
186 MCOUNT_RESTORE_FRAME 8
192 ftrace_restore_flags:
196 END(ftrace_regs_caller)
199 #else /* ! CONFIG_DYNAMIC_FTRACE */
202 cmpl $0, function_trace_stop
205 cmpq $ftrace_stub, ftrace_trace_function
208 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
209 cmpq $ftrace_stub, ftrace_graph_return
210 jnz ftrace_graph_caller
212 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
213 jnz ftrace_graph_caller
223 #ifdef CC_USING_FENTRY
224 movq SS+16(%rsp), %rsi
228 subq $MCOUNT_INSN_SIZE, %rdi
230 call *ftrace_trace_function
236 #endif /* CONFIG_DYNAMIC_FTRACE */
237 #endif /* CONFIG_FUNCTION_TRACER */
239 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
240 ENTRY(ftrace_graph_caller)
243 #ifdef CC_USING_FENTRY
244 leaq SS+16(%rsp), %rdi
245 movq $0, %rdx /* No framepointers needed */
251 subq $MCOUNT_INSN_SIZE, %rsi
253 call prepare_ftrace_return
258 END(ftrace_graph_caller)
260 GLOBAL(return_to_handler)
263 /* Save the return values */
268 call ftrace_return_to_handler
278 #ifndef CONFIG_PREEMPT
279 #define retint_kernel retint_restore_args
282 #ifdef CONFIG_PARAVIRT
283 ENTRY(native_usergs_sysret64)
286 ENDPROC(native_usergs_sysret64)
287 #endif /* CONFIG_PARAVIRT */
290 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
291 #ifdef CONFIG_TRACE_IRQFLAGS
292 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
300 * When dynamic function tracer is enabled it will add a breakpoint
301 * to all locations that it is about to modify, sync CPUs, update
302 * all the code, sync CPUs, then remove the breakpoints. In this time
303 * if lockdep is enabled, it might jump back into the debug handler
304 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
306 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
307 * make sure the stack pointer does not get reset back to the top
308 * of the debug stack, and instead just reuses the current stack.
310 #if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
312 .macro TRACE_IRQS_OFF_DEBUG
313 call debug_stack_set_zero
315 call debug_stack_reset
318 .macro TRACE_IRQS_ON_DEBUG
319 call debug_stack_set_zero
321 call debug_stack_reset
324 .macro TRACE_IRQS_IRETQ_DEBUG offset=ARGOFFSET
325 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
332 # define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
333 # define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
334 # define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
338 * C code is not supposed to know about undefined top of stack. Every time
339 * a C function with an pt_regs argument is called from the SYSCALL based
340 * fast path FIXUP_TOP_OF_STACK is needed.
341 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
345 /* %rsp:at FRAMEEND */
346 .macro FIXUP_TOP_OF_STACK tmp offset=0
347 movq PER_CPU_VAR(old_rsp),\tmp
348 movq \tmp,RSP+\offset(%rsp)
349 movq $__USER_DS,SS+\offset(%rsp)
350 movq $__USER_CS,CS+\offset(%rsp)
351 movq $-1,RCX+\offset(%rsp)
352 movq R11+\offset(%rsp),\tmp /* get eflags */
353 movq \tmp,EFLAGS+\offset(%rsp)
356 .macro RESTORE_TOP_OF_STACK tmp offset=0
357 movq RSP+\offset(%rsp),\tmp
358 movq \tmp,PER_CPU_VAR(old_rsp)
359 movq EFLAGS+\offset(%rsp),\tmp
360 movq \tmp,R11+\offset(%rsp)
363 .macro FAKE_STACK_FRAME child_rip
364 /* push in order ss, rsp, eflags, cs, rip */
366 pushq_cfi $__KERNEL_DS /* ss */
367 /*CFI_REL_OFFSET ss,0*/
368 pushq_cfi %rax /* rsp */
370 pushq_cfi $(X86_EFLAGS_IF|X86_EFLAGS_FIXED) /* eflags - interrupts on */
371 /*CFI_REL_OFFSET rflags,0*/
372 pushq_cfi $__KERNEL_CS /* cs */
373 /*CFI_REL_OFFSET cs,0*/
374 pushq_cfi \child_rip /* rip */
376 pushq_cfi %rax /* orig rax */
379 .macro UNFAKE_STACK_FRAME
381 CFI_ADJUST_CFA_OFFSET -(6*8)
385 * initial frame state for interrupts (and exceptions without error code)
387 .macro EMPTY_FRAME start=1 offset=0
391 CFI_DEF_CFA rsp,8+\offset
393 CFI_DEF_CFA_OFFSET 8+\offset
398 * initial frame state for interrupts (and exceptions without error code)
400 .macro INTR_FRAME start=1 offset=0
401 EMPTY_FRAME \start, SS+8+\offset-RIP
402 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
403 CFI_REL_OFFSET rsp, RSP+\offset-RIP
404 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
405 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
406 CFI_REL_OFFSET rip, RIP+\offset-RIP
410 * initial frame state for exceptions with error code (and interrupts
411 * with vector already pushed)
413 .macro XCPT_FRAME start=1 offset=0
414 INTR_FRAME \start, RIP+\offset-ORIG_RAX
415 /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
419 * frame that enables calling into C.
421 .macro PARTIAL_FRAME start=1 offset=0
422 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
423 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
424 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
425 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
426 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
427 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
428 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
429 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
430 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
431 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
435 * frame that enables passing a complete pt_regs to a C function.
437 .macro DEFAULT_FRAME start=1 offset=0
438 PARTIAL_FRAME \start, R11+\offset-R15
439 CFI_REL_OFFSET rbx, RBX+\offset
440 CFI_REL_OFFSET rbp, RBP+\offset
441 CFI_REL_OFFSET r12, R12+\offset
442 CFI_REL_OFFSET r13, R13+\offset
443 CFI_REL_OFFSET r14, R14+\offset
444 CFI_REL_OFFSET r15, R15+\offset
447 /* save partial stack frame */
450 /* start from rbp in pt_regs and jump over */
451 movq_cfi rdi, (RDI-RBP)
452 movq_cfi rsi, (RSI-RBP)
453 movq_cfi rdx, (RDX-RBP)
454 movq_cfi rcx, (RCX-RBP)
455 movq_cfi rax, (RAX-RBP)
456 movq_cfi r8, (R8-RBP)
457 movq_cfi r9, (R9-RBP)
458 movq_cfi r10, (R10-RBP)
459 movq_cfi r11, (R11-RBP)
461 /* Save rbp so that we can unwind from get_irq_regs() */
464 /* Save previous stack value */
467 leaq -RBP(%rsp),%rdi /* arg1 for handler */
468 testl $3, CS-RBP(%rsi)
472 * irq_count is used to check if a CPU is already on an interrupt stack
473 * or not. While this is essentially redundant with preempt_count it is
474 * a little cheaper to use a separate counter in the PDA (short of
475 * moving irq_enter into assembly, which would be too much work)
477 1: incl PER_CPU_VAR(irq_count)
478 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
479 CFI_DEF_CFA_REGISTER rsi
481 /* Store previous stack value */
483 CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
484 0x77 /* DW_OP_breg7 */, 0, \
485 0x06 /* DW_OP_deref */, \
486 0x08 /* DW_OP_const1u */, SS+8-RBP, \
487 0x22 /* DW_OP_plus */
488 /* We entered an interrupt context - irqs are off: */
493 PARTIAL_FRAME 1 (REST_SKIP+8)
494 movq 5*8+16(%rsp), %r11 /* save return address */
501 movq %r11, 8(%rsp) /* return address */
502 FIXUP_TOP_OF_STACK %r11, 16
507 /* save complete stack frame */
508 .pushsection .kprobes.text, "ax"
528 movl $MSR_GS_BASE,%ecx
531 js 1f /* negative -> in kernel */
540 * A newly forked process directly context switches into this address.
542 * rdi: prev task we switched from
547 LOCK ; btr $TIF_FORK,TI_flags(%r8)
550 popfq_cfi # reset kernel eflags
552 call schedule_tail # rdi: 'prev' task parameter
554 GET_THREAD_INFO(%rcx)
558 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
562 * By the time we get here, we have no idea whether our pt_regs,
563 * ti flags, and ti status came from the 64-bit SYSCALL fast path,
564 * the slow path, or one of the ia32entry paths.
565 * Use int_ret_from_sys_call to return, since it can safely handle
568 jmp int_ret_from_sys_call
571 subq $REST_SKIP, %rsp # leave space for volatiles
572 CFI_ADJUST_CFA_OFFSET REST_SKIP
577 jmp int_ret_from_sys_call
582 * System call entry. Up to 6 arguments in registers are supported.
584 * SYSCALL does not save anything on the stack and does not change the
585 * stack pointer. However, it does mask the flags register for us, so
586 * CLD and CLAC are not needed.
591 * rax system call number
593 * rcx return address for syscall/sysret, C arg3
596 * r10 arg3 (--> moved to rcx for C)
599 * r11 eflags for syscall/sysret, temporary for C
600 * r12-r15,rbp,rbx saved by C code, not touched.
602 * Interrupts are off on entry.
603 * Only called from user space.
605 * XXX if we had a free scratch register we could save the RSP into the stack frame
606 * and report it properly in ps. Unfortunately we haven't.
608 * When user can change the frames always force IRET. That is because
609 * it deals with uncanonical addresses better. SYSRET has trouble
610 * with them due to bugs in both AMD and Intel CPUs.
616 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
618 /*CFI_REGISTER rflags,r11*/
621 * A hypervisor implementation might want to use a label
622 * after the swapgs, so that it can do the swapgs
623 * for the guest and jump here on syscall.
625 GLOBAL(system_call_after_swapgs)
627 movq %rsp,PER_CPU_VAR(old_rsp)
628 movq PER_CPU_VAR(kernel_stack),%rsp
630 * No need to follow this irqs off/on section - it's straight
633 ENABLE_INTERRUPTS(CLBR_NONE)
635 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
636 movq %rcx,RIP-ARGOFFSET(%rsp)
637 CFI_REL_OFFSET rip,RIP-ARGOFFSET
638 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
640 system_call_fastpath:
641 #if __SYSCALL_MASK == ~0
642 cmpq $__NR_syscall_max,%rax
644 andl $__SYSCALL_MASK,%eax
645 cmpl $__NR_syscall_max,%eax
649 call *sys_call_table(,%rax,8) # XXX: rip relative
650 movq %rax,RAX-ARGOFFSET(%rsp)
652 * Syscall return path ending with SYSRET (fast path)
653 * Has incomplete stack frame and undefined top of stack.
656 movl $_TIF_ALLWORK_MASK,%edi
660 DISABLE_INTERRUPTS(CLBR_NONE)
662 movl TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET),%edx
667 * sysretq will re-enable interrupts:
670 movq RIP-ARGOFFSET(%rsp),%rcx
672 RESTORE_ARGS 1,-ARG_SKIP,0
673 /*CFI_REGISTER rflags,r11*/
674 movq PER_CPU_VAR(old_rsp), %rsp
678 /* Handle reschedules */
679 /* edx: work, edi: workmask */
681 bt $TIF_NEED_RESCHED,%edx
684 ENABLE_INTERRUPTS(CLBR_NONE)
690 /* Handle a signal */
693 ENABLE_INTERRUPTS(CLBR_NONE)
694 #ifdef CONFIG_AUDITSYSCALL
695 bt $TIF_SYSCALL_AUDIT,%edx
699 * We have a signal, or exit tracing or single-step.
700 * These all wind up with the iret return path anyway,
701 * so just join that path right now.
703 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
704 jmp int_check_syscall_exit_work
707 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
708 jmp ret_from_sys_call
710 #ifdef CONFIG_AUDITSYSCALL
712 * Fast path for syscall audit without full syscall trace.
713 * We just call __audit_syscall_entry() directly, and then
714 * jump back to the normal fast path.
717 movq %r10,%r9 /* 6th arg: 4th syscall arg */
718 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
719 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
720 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
721 movq %rax,%rsi /* 2nd arg: syscall number */
722 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
723 call __audit_syscall_entry
724 LOAD_ARGS 0 /* reload call-clobbered registers */
725 jmp system_call_fastpath
728 * Return fast path for syscall audit. Call __audit_syscall_exit()
729 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
733 movq RAX-ARGOFFSET(%rsp),%rsi /* second arg, syscall return value */
734 cmpq $-MAX_ERRNO,%rsi /* is it < -MAX_ERRNO? */
735 setbe %al /* 1 if so, 0 if not */
736 movzbl %al,%edi /* zero-extend that into %edi */
737 call __audit_syscall_exit
738 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
740 #endif /* CONFIG_AUDITSYSCALL */
742 /* Do syscall tracing */
744 #ifdef CONFIG_AUDITSYSCALL
745 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
749 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
750 FIXUP_TOP_OF_STACK %rdi
752 call syscall_trace_enter
754 * Reload arg registers from stack in case ptrace changed them.
755 * We don't reload %rax because syscall_trace_enter() returned
756 * the value it wants us to use in the table lookup.
758 LOAD_ARGS ARGOFFSET, 1
760 #if __SYSCALL_MASK == ~0
761 cmpq $__NR_syscall_max,%rax
763 andl $__SYSCALL_MASK,%eax
764 cmpl $__NR_syscall_max,%eax
766 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
767 movq %r10,%rcx /* fixup for C */
768 call *sys_call_table(,%rax,8)
769 movq %rax,RAX-ARGOFFSET(%rsp)
770 /* Use IRET because user could have changed frame */
773 * Syscall return path ending with IRET.
774 * Has correct top of stack, but partial stack frame.
776 GLOBAL(int_ret_from_sys_call)
777 DISABLE_INTERRUPTS(CLBR_NONE)
779 movl $_TIF_ALLWORK_MASK,%edi
780 /* edi: mask to check */
781 GLOBAL(int_with_check)
783 GET_THREAD_INFO(%rcx)
784 movl TI_flags(%rcx),%edx
787 andl $~TS_COMPAT,TI_status(%rcx)
790 /* Either reschedule or signal or syscall exit tracking needed. */
791 /* First do a reschedule test. */
792 /* edx: work, edi: workmask */
794 bt $TIF_NEED_RESCHED,%edx
797 ENABLE_INTERRUPTS(CLBR_NONE)
801 DISABLE_INTERRUPTS(CLBR_NONE)
805 /* handle signals and tracing -- both require a full stack frame */
808 ENABLE_INTERRUPTS(CLBR_NONE)
809 int_check_syscall_exit_work:
811 /* Check for syscall exit trace */
812 testl $_TIF_WORK_SYSCALL_EXIT,%edx
815 leaq 8(%rsp),%rdi # &ptregs -> arg1
816 call syscall_trace_leave
818 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
822 testl $_TIF_DO_NOTIFY_MASK,%edx
824 movq %rsp,%rdi # &ptregs -> arg1
825 xorl %esi,%esi # oldset -> arg2
826 call do_notify_resume
827 1: movl $_TIF_WORK_MASK,%edi
830 DISABLE_INTERRUPTS(CLBR_NONE)
836 .macro FORK_LIKE func
839 popq %r11 /* save return address */
842 pushq %r11 /* put it back on stack */
843 FIXUP_TOP_OF_STACK %r11, 8
844 DEFAULT_FRAME 0 8 /* offset 8: return address */
846 RESTORE_TOP_OF_STACK %r11, 8
847 ret $REST_SKIP /* pop extended registers */
852 .macro FIXED_FRAME label,func
855 PARTIAL_FRAME 0 8 /* offset 8: return address */
856 FIXUP_TOP_OF_STACK %r11, 8-ARGOFFSET
858 RESTORE_TOP_OF_STACK %r11, 8-ARGOFFSET
867 FIXED_FRAME stub_iopl, sys_iopl
869 ENTRY(ptregscall_common)
870 DEFAULT_FRAME 1 8 /* offset 8: return address */
871 RESTORE_TOP_OF_STACK %r11, 8
872 movq_cfi_restore R15+8, r15
873 movq_cfi_restore R14+8, r14
874 movq_cfi_restore R13+8, r13
875 movq_cfi_restore R12+8, r12
876 movq_cfi_restore RBP+8, rbp
877 movq_cfi_restore RBX+8, rbx
878 ret $REST_SKIP /* pop extended registers */
880 END(ptregscall_common)
887 FIXUP_TOP_OF_STACK %r11
891 jmp int_ret_from_sys_call
896 * sigreturn is special because it needs to restore all registers on return.
897 * This cannot be done with SYSRET, so use the IRET return path instead.
899 ENTRY(stub_rt_sigreturn)
904 FIXUP_TOP_OF_STACK %r11
905 call sys_rt_sigreturn
906 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
908 jmp int_ret_from_sys_call
910 END(stub_rt_sigreturn)
912 #ifdef CONFIG_X86_X32_ABI
913 ENTRY(stub_x32_rt_sigreturn)
918 FIXUP_TOP_OF_STACK %r11
919 call sys32_x32_rt_sigreturn
920 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
922 jmp int_ret_from_sys_call
924 END(stub_x32_rt_sigreturn)
926 ENTRY(stub_x32_execve)
931 FIXUP_TOP_OF_STACK %r11
932 call compat_sys_execve
933 RESTORE_TOP_OF_STACK %r11
936 jmp int_ret_from_sys_call
943 * Build the entry stubs and pointer table with some assembler magic.
944 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
945 * single cache line on all modern x86 implementations.
947 .section .init.rodata,"a"
951 .p2align CONFIG_X86_L1_CACHE_SHIFT
952 ENTRY(irq_entries_start)
954 vector=FIRST_EXTERNAL_VECTOR
955 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
958 .if vector < NR_VECTORS
959 .if vector <> FIRST_EXTERNAL_VECTOR
960 CFI_ADJUST_CFA_OFFSET -8
962 1: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
963 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
972 2: jmp common_interrupt
975 END(irq_entries_start)
982 * Interrupt entry/exit.
984 * Interrupt entry points save only callee clobbered registers in fast path.
986 * Entry runs with interrupts off.
989 /* 0(%rsp): ~(interrupt number) */
990 .macro interrupt func
991 /* reserve pt_regs for scratch regs and rbp */
992 subq $ORIG_RAX-RBP, %rsp
993 CFI_ADJUST_CFA_OFFSET ORIG_RAX-RBP
999 * Interrupt entry/exit should be protected against kprobes
1001 .pushsection .kprobes.text, "ax"
1003 * The interrupt stubs push (~vector+0x80) onto the stack and
1004 * then jump to common_interrupt.
1006 .p2align CONFIG_X86_L1_CACHE_SHIFT
1010 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
1012 /* 0(%rsp): old_rsp-ARGOFFSET */
1014 DISABLE_INTERRUPTS(CLBR_NONE)
1016 decl PER_CPU_VAR(irq_count)
1018 /* Restore saved previous stack */
1020 CFI_DEF_CFA rsi,SS+8-RBP /* reg/off reset after def_cfa_expr */
1021 leaq ARGOFFSET-RBP(%rsi), %rsp
1022 CFI_DEF_CFA_REGISTER rsp
1023 CFI_ADJUST_CFA_OFFSET RBP-ARGOFFSET
1026 GET_THREAD_INFO(%rcx)
1027 testl $3,CS-ARGOFFSET(%rsp)
1030 /* Interrupt came from user space */
1032 * Has a correct top of stack, but a partial stack frame
1033 * %rcx: thread info. Interrupts off.
1035 retint_with_reschedule:
1036 movl $_TIF_WORK_MASK,%edi
1038 LOCKDEP_SYS_EXIT_IRQ
1039 movl TI_flags(%rcx),%edx
1044 retint_swapgs: /* return to user-space */
1046 * The iretq could re-enable interrupts:
1048 DISABLE_INTERRUPTS(CLBR_ANY)
1053 retint_restore_args: /* return to kernel space */
1054 DISABLE_INTERRUPTS(CLBR_ANY)
1056 * The iretq could re-enable interrupts:
1067 * Are we returning to a stack segment from the LDT? Note: in
1068 * 64-bit mode SS:RSP on the exception stack is always valid.
1070 #ifdef CONFIG_X86_ESPFIX64
1071 testb $4,(SS-RIP)(%rsp)
1072 jnz native_irq_return_ldt
1075 .global native_irq_return_iret
1076 native_irq_return_iret:
1078 * This may fault. Non-paranoid faults on return to userspace are
1079 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
1080 * Double-faults due to espfix64 are handled in do_double_fault.
1081 * Other faults here are fatal.
1085 #ifdef CONFIG_X86_ESPFIX64
1086 native_irq_return_ldt:
1090 movq PER_CPU_VAR(espfix_waddr),%rdi
1091 movq %rax,(0*8)(%rdi) /* RAX */
1092 movq (2*8)(%rsp),%rax /* RIP */
1093 movq %rax,(1*8)(%rdi)
1094 movq (3*8)(%rsp),%rax /* CS */
1095 movq %rax,(2*8)(%rdi)
1096 movq (4*8)(%rsp),%rax /* RFLAGS */
1097 movq %rax,(3*8)(%rdi)
1098 movq (6*8)(%rsp),%rax /* SS */
1099 movq %rax,(5*8)(%rdi)
1100 movq (5*8)(%rsp),%rax /* RSP */
1101 movq %rax,(4*8)(%rdi)
1102 andl $0xffff0000,%eax
1104 orq PER_CPU_VAR(espfix_stack),%rax
1108 jmp native_irq_return_iret
1111 /* edi: workmask, edx: work */
1114 bt $TIF_NEED_RESCHED,%edx
1117 ENABLE_INTERRUPTS(CLBR_NONE)
1121 GET_THREAD_INFO(%rcx)
1122 DISABLE_INTERRUPTS(CLBR_NONE)
1127 testl $_TIF_DO_NOTIFY_MASK,%edx
1130 ENABLE_INTERRUPTS(CLBR_NONE)
1132 movq $-1,ORIG_RAX(%rsp)
1133 xorl %esi,%esi # oldset
1134 movq %rsp,%rdi # &pt_regs
1135 call do_notify_resume
1137 DISABLE_INTERRUPTS(CLBR_NONE)
1139 GET_THREAD_INFO(%rcx)
1140 jmp retint_with_reschedule
1142 #ifdef CONFIG_PREEMPT
1143 /* Returning to kernel space. Check if we need preemption */
1144 /* rcx: threadinfo. interrupts off. */
1145 ENTRY(retint_kernel)
1146 cmpl $0,TI_preempt_count(%rcx)
1147 jnz retint_restore_args
1148 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
1149 jnc retint_restore_args
1150 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
1151 jnc retint_restore_args
1152 call preempt_schedule_irq
1156 END(common_interrupt)
1159 * End of kprobes section
1166 .macro apicinterrupt num sym do_sym
1179 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
1180 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
1181 apicinterrupt REBOOT_VECTOR \
1182 reboot_interrupt smp_reboot_interrupt
1185 #ifdef CONFIG_X86_UV
1186 apicinterrupt UV_BAU_MESSAGE \
1187 uv_bau_message_intr1 uv_bau_message_interrupt
1189 apicinterrupt LOCAL_TIMER_VECTOR \
1190 apic_timer_interrupt smp_apic_timer_interrupt
1191 apicinterrupt X86_PLATFORM_IPI_VECTOR \
1192 x86_platform_ipi smp_x86_platform_ipi
1194 #ifdef CONFIG_HAVE_KVM
1195 apicinterrupt POSTED_INTR_VECTOR \
1196 kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
1199 apicinterrupt THRESHOLD_APIC_VECTOR \
1200 threshold_interrupt smp_threshold_interrupt
1201 apicinterrupt THERMAL_APIC_VECTOR \
1202 thermal_interrupt smp_thermal_interrupt
1205 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
1206 call_function_single_interrupt smp_call_function_single_interrupt
1207 apicinterrupt CALL_FUNCTION_VECTOR \
1208 call_function_interrupt smp_call_function_interrupt
1209 apicinterrupt RESCHEDULE_VECTOR \
1210 reschedule_interrupt smp_reschedule_interrupt
1213 apicinterrupt ERROR_APIC_VECTOR \
1214 error_interrupt smp_error_interrupt
1215 apicinterrupt SPURIOUS_APIC_VECTOR \
1216 spurious_interrupt smp_spurious_interrupt
1218 #ifdef CONFIG_IRQ_WORK
1219 apicinterrupt IRQ_WORK_VECTOR \
1220 irq_work_interrupt smp_irq_work_interrupt
1224 * Exception entry points.
1226 .macro zeroentry sym do_sym
1230 PARAVIRT_ADJUST_EXCEPTION_FRAME
1231 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1232 subq $ORIG_RAX-R15, %rsp
1233 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1236 movq %rsp,%rdi /* pt_regs pointer */
1237 xorl %esi,%esi /* no error code */
1239 jmp error_exit /* %ebx: no swapgs flag */
1244 .macro paranoidzeroentry sym do_sym
1248 PARAVIRT_ADJUST_EXCEPTION_FRAME
1249 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1250 subq $ORIG_RAX-R15, %rsp
1251 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1254 movq %rsp,%rdi /* pt_regs pointer */
1255 xorl %esi,%esi /* no error code */
1257 jmp paranoid_exit /* %ebx: no swapgs flag */
1262 #define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
1263 .macro paranoidzeroentry_ist sym do_sym ist
1267 PARAVIRT_ADJUST_EXCEPTION_FRAME
1268 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1269 subq $ORIG_RAX-R15, %rsp
1270 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1272 TRACE_IRQS_OFF_DEBUG
1273 movq %rsp,%rdi /* pt_regs pointer */
1274 xorl %esi,%esi /* no error code */
1275 subq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1277 addq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
1278 jmp paranoid_exit /* %ebx: no swapgs flag */
1283 .macro errorentry sym do_sym
1287 PARAVIRT_ADJUST_EXCEPTION_FRAME
1288 subq $ORIG_RAX-R15, %rsp
1289 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1292 movq %rsp,%rdi /* pt_regs pointer */
1293 movq ORIG_RAX(%rsp),%rsi /* get error code */
1294 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1296 jmp error_exit /* %ebx: no swapgs flag */
1301 /* error code is on the stack already */
1302 .macro paranoiderrorentry sym do_sym
1306 PARAVIRT_ADJUST_EXCEPTION_FRAME
1307 subq $ORIG_RAX-R15, %rsp
1308 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1312 movq %rsp,%rdi /* pt_regs pointer */
1313 movq ORIG_RAX(%rsp),%rsi /* get error code */
1314 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1316 jmp paranoid_exit /* %ebx: no swapgs flag */
1321 zeroentry divide_error do_divide_error
1322 zeroentry overflow do_overflow
1323 zeroentry bounds do_bounds
1324 zeroentry invalid_op do_invalid_op
1325 zeroentry device_not_available do_device_not_available
1326 paranoiderrorentry double_fault do_double_fault
1327 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1328 errorentry invalid_TSS do_invalid_TSS
1329 errorentry segment_not_present do_segment_not_present
1330 zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1331 zeroentry coprocessor_error do_coprocessor_error
1332 errorentry alignment_check do_alignment_check
1333 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1336 /* Reload gs selector with exception handling */
1337 /* edi: new selector */
1338 ENTRY(native_load_gs_index)
1341 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1345 2: mfence /* workaround */
1350 END(native_load_gs_index)
1352 _ASM_EXTABLE(gs_change,bad_gs)
1353 .section .fixup,"ax"
1354 /* running with kernelgs */
1356 SWAPGS /* switch back to user gs */
1362 /* Call softirq on interrupt stack. Interrupts are off. */
1366 CFI_REL_OFFSET rbp,0
1368 CFI_DEF_CFA_REGISTER rbp
1369 incl PER_CPU_VAR(irq_count)
1370 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1371 push %rbp # backlink for old unwinder
1375 CFI_DEF_CFA_REGISTER rsp
1376 CFI_ADJUST_CFA_OFFSET -8
1377 decl PER_CPU_VAR(irq_count)
1383 zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
1386 * A note on the "critical region" in our callback handler.
1387 * We want to avoid stacking callback handlers due to events occurring
1388 * during handling of the last event. To do this, we keep events disabled
1389 * until we've done all processing. HOWEVER, we must enable events before
1390 * popping the stack frame (can't be done atomically) and so it would still
1391 * be possible to get enough handler activations to overflow the stack.
1392 * Although unlikely, bugs of that kind are hard to track down, so we'd
1393 * like to avoid the possibility.
1394 * So, on entry to the handler we detect whether we interrupted an
1395 * existing activation in its critical region -- if so, we pop the current
1396 * activation and restart the handler using the previous one.
1398 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1401 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1402 * see the correct pointer to the pt_regs
1404 movq %rdi, %rsp # we don't return, adjust the stack frame
1407 11: incl PER_CPU_VAR(irq_count)
1409 CFI_DEF_CFA_REGISTER rbp
1410 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1411 pushq %rbp # backlink for old unwinder
1412 call xen_evtchn_do_upcall
1414 CFI_DEF_CFA_REGISTER rsp
1415 decl PER_CPU_VAR(irq_count)
1418 END(xen_do_hypervisor_callback)
1421 * Hypervisor uses this for application faults while it executes.
1422 * We get here for two reasons:
1423 * 1. Fault while reloading DS, ES, FS or GS
1424 * 2. Fault while executing IRET
1425 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1426 * registers that could be reloaded and zeroed the others.
1427 * Category 2 we fix up by killing the current process. We cannot use the
1428 * normal Linux return path in this case because if we use the IRET hypercall
1429 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1430 * We distinguish between categories by comparing each saved segment register
1431 * with its current contents: any discrepancy means we in category 1.
1433 ENTRY(xen_failsafe_callback)
1435 /*CFI_REL_OFFSET gs,GS*/
1436 /*CFI_REL_OFFSET fs,FS*/
1437 /*CFI_REL_OFFSET es,ES*/
1438 /*CFI_REL_OFFSET ds,DS*/
1439 CFI_REL_OFFSET r11,8
1440 CFI_REL_OFFSET rcx,0
1454 /* All segments match their saved values => Category 2 (Bad IRET). */
1460 CFI_ADJUST_CFA_OFFSET -0x30
1461 pushq_cfi $0 /* RIP */
1464 jmp general_protection
1466 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1472 CFI_ADJUST_CFA_OFFSET -0x30
1473 pushq_cfi $-1 /* orig_ax = -1 => not a system call */
1477 END(xen_failsafe_callback)
1479 apicinterrupt HYPERVISOR_CALLBACK_VECTOR \
1480 xen_hvm_callback_vector xen_evtchn_do_upcall
1482 #endif /* CONFIG_XEN */
1484 #if IS_ENABLED(CONFIG_HYPERV)
1485 apicinterrupt HYPERVISOR_CALLBACK_VECTOR \
1486 hyperv_callback_vector hyperv_vector_handler
1487 #endif /* CONFIG_HYPERV */
1490 * Some functions should be protected against kprobes
1492 .pushsection .kprobes.text, "ax"
1494 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1495 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1496 errorentry stack_segment do_stack_segment
1498 zeroentry xen_debug do_debug
1499 zeroentry xen_int3 do_int3
1500 errorentry xen_stack_segment do_stack_segment
1502 errorentry general_protection do_general_protection
1503 errorentry page_fault do_page_fault
1504 #ifdef CONFIG_KVM_GUEST
1505 errorentry async_page_fault do_async_page_fault
1507 #ifdef CONFIG_X86_MCE
1508 paranoidzeroentry machine_check *machine_check_vector(%rip)
1512 * "Paranoid" exit path from exception stack.
1513 * Paranoid because this is used by NMIs and cannot take
1514 * any kernel state for granted.
1515 * We don't do kernel preemption checks here, because only
1516 * NMI should be common and it does not enable IRQs and
1517 * cannot get reschedule ticks.
1519 * "trace" is 0 for the NMI handler only, because irq-tracing
1520 * is fundamentally NMI-unsafe. (we cannot change the soft and
1521 * hard flags at once, atomically)
1524 /* ebx: no swapgs flag */
1525 ENTRY(paranoid_exit)
1527 DISABLE_INTERRUPTS(CLBR_NONE)
1528 TRACE_IRQS_OFF_DEBUG
1529 testl %ebx,%ebx /* swapgs needed? */
1530 jnz paranoid_restore
1532 jnz paranoid_userspace
1539 TRACE_IRQS_IRETQ_DEBUG 0
1543 GET_THREAD_INFO(%rcx)
1544 movl TI_flags(%rcx),%ebx
1545 andl $_TIF_WORK_MASK,%ebx
1547 movq %rsp,%rdi /* &pt_regs */
1549 movq %rax,%rsp /* switch stack for scheduling */
1550 testl $_TIF_NEED_RESCHED,%ebx
1551 jnz paranoid_schedule
1552 movl %ebx,%edx /* arg3: thread flags */
1554 ENABLE_INTERRUPTS(CLBR_NONE)
1555 xorl %esi,%esi /* arg2: oldset */
1556 movq %rsp,%rdi /* arg1: &pt_regs */
1557 call do_notify_resume
1558 DISABLE_INTERRUPTS(CLBR_NONE)
1560 jmp paranoid_userspace
1563 ENABLE_INTERRUPTS(CLBR_ANY)
1565 DISABLE_INTERRUPTS(CLBR_ANY)
1567 jmp paranoid_userspace
1572 * Exception entry point. This expects an error code/orig_rax on the stack.
1573 * returns in "no swapgs flag" in %ebx.
1577 CFI_ADJUST_CFA_OFFSET 15*8
1578 /* oldrax contains error code */
1597 je error_kernelspace
1605 * There are two places in the kernel that can potentially fault with
1606 * usergs. Handle them here. B stepping K8s sometimes report a
1607 * truncated RIP for IRET exceptions returning to compat mode. Check
1608 * for these here too.
1612 leaq native_irq_return_iret(%rip),%rcx
1613 cmpq %rcx,RIP+8(%rsp)
1615 movl %ecx,%eax /* zero extend */
1616 cmpq %rax,RIP+8(%rsp)
1618 cmpq $gs_change,RIP+8(%rsp)
1623 /* Fix truncated RIP */
1624 movq %rcx,RIP+8(%rsp)
1632 decl %ebx /* Return to usergs */
1638 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1643 DISABLE_INTERRUPTS(CLBR_NONE)
1645 GET_THREAD_INFO(%rcx)
1648 LOCKDEP_SYS_EXIT_IRQ
1649 movl TI_flags(%rcx),%edx
1650 movl $_TIF_WORK_MASK,%edi
1658 * Test if a given stack is an NMI stack or not.
1660 .macro test_in_nmi reg stack nmi_ret normal_ret
1663 subq $EXCEPTION_STKSZ, %\reg
1669 /* runs on exception stack */
1672 PARAVIRT_ADJUST_EXCEPTION_FRAME
1674 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1675 * the iretq it performs will take us out of NMI context.
1676 * This means that we can have nested NMIs where the next
1677 * NMI is using the top of the stack of the previous NMI. We
1678 * can't let it execute because the nested NMI will corrupt the
1679 * stack of the previous NMI. NMI handlers are not re-entrant
1682 * To handle this case we do the following:
1683 * Check the a special location on the stack that contains
1684 * a variable that is set when NMIs are executing.
1685 * The interrupted task's stack is also checked to see if it
1687 * If the variable is not set and the stack is not the NMI
1689 * o Set the special variable on the stack
1690 * o Copy the interrupt frame into a "saved" location on the stack
1691 * o Copy the interrupt frame into a "copy" location on the stack
1692 * o Continue processing the NMI
1693 * If the variable is set or the previous stack is the NMI stack:
1694 * o Modify the "copy" location to jump to the repeate_nmi
1695 * o return back to the first NMI
1697 * Now on exit of the first NMI, we first clear the stack variable
1698 * The NMI stack will tell any nested NMIs at that point that it is
1699 * nested. Then we pop the stack normally with iret, and if there was
1700 * a nested NMI that updated the copy interrupt stack frame, a
1701 * jump will be made to the repeat_nmi code that will handle the second
1705 /* Use %rdx as out temp variable throughout */
1707 CFI_REL_OFFSET rdx, 0
1710 * If %cs was not the kernel segment, then the NMI triggered in user
1711 * space, which means it is definitely not nested.
1713 cmpl $__KERNEL_CS, 16(%rsp)
1717 * Check the special variable on the stack to see if NMIs are
1724 * Now test if the previous stack was an NMI stack.
1725 * We need the double check. We check the NMI stack to satisfy the
1726 * race when the first NMI clears the variable before returning.
1727 * We check the variable because the first NMI could be in a
1728 * breakpoint routine using a breakpoint stack.
1731 test_in_nmi rdx, 4*8(%rsp), nested_nmi, first_nmi
1736 * Do nothing if we interrupted the fixup in repeat_nmi.
1737 * It's about to repeat the NMI handler, so we are fine
1738 * with ignoring this one.
1740 movq $repeat_nmi, %rdx
1743 movq $end_repeat_nmi, %rdx
1748 /* Set up the interrupted NMIs stack to jump to repeat_nmi */
1749 leaq -1*8(%rsp), %rdx
1751 CFI_ADJUST_CFA_OFFSET 1*8
1752 leaq -10*8(%rsp), %rdx
1753 pushq_cfi $__KERNEL_DS
1756 pushq_cfi $__KERNEL_CS
1757 pushq_cfi $repeat_nmi
1759 /* Put stack back */
1761 CFI_ADJUST_CFA_OFFSET -6*8
1767 /* No need to check faults here */
1773 * Because nested NMIs will use the pushed location that we
1774 * stored in rdx, we must keep that space available.
1775 * Here's what our stack frame will look like:
1776 * +-------------------------+
1778 * | original Return RSP |
1779 * | original RFLAGS |
1782 * +-------------------------+
1783 * | temp storage for rdx |
1784 * +-------------------------+
1785 * | NMI executing variable |
1786 * +-------------------------+
1788 * | copied Return RSP |
1792 * +-------------------------+
1794 * | Saved Return RSP |
1798 * +-------------------------+
1800 * +-------------------------+
1802 * The saved stack frame is used to fix up the copied stack frame
1803 * that a nested NMI may change to make the interrupted NMI iret jump
1804 * to the repeat_nmi. The original stack frame and the temp storage
1805 * is also used by nested NMIs and can not be trusted on exit.
1807 /* Do not pop rdx, nested NMIs will corrupt that part of the stack */
1811 /* Set the NMI executing variable on the stack. */
1815 * Leave room for the "copied" frame
1818 CFI_ADJUST_CFA_OFFSET 5*8
1820 /* Copy the stack frame to the Saved frame */
1822 pushq_cfi 11*8(%rsp)
1824 CFI_DEF_CFA_OFFSET SS+8-RIP
1826 /* Everything up to here is safe from nested NMIs */
1829 * If there was a nested NMI, the first NMI's iret will return
1830 * here. But NMIs are still enabled and we can take another
1831 * nested NMI. The nested NMI checks the interrupted RIP to see
1832 * if it is between repeat_nmi and end_repeat_nmi, and if so
1833 * it will just return, as we are about to repeat an NMI anyway.
1834 * This makes it safe to copy to the stack frame that a nested
1839 * Update the stack variable to say we are still in NMI (the update
1840 * is benign for the non-repeat case, where 1 was pushed just above
1841 * to this very stack slot).
1845 /* Make another copy, this one may be modified by nested NMIs */
1847 CFI_ADJUST_CFA_OFFSET -10*8
1849 pushq_cfi -6*8(%rsp)
1852 CFI_DEF_CFA_OFFSET SS+8-RIP
1856 * Everything below this point can be preempted by a nested
1857 * NMI if the first NMI took an exception and reset our iret stack
1858 * so that we repeat another NMI.
1860 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1861 subq $ORIG_RAX-R15, %rsp
1862 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
1864 * Use save_paranoid to handle SWAPGS, but no need to use paranoid_exit
1865 * as we should not be calling schedule in NMI context.
1866 * Even with normal interrupts enabled. An NMI should not be
1867 * setting NEED_RESCHED or anything that normal interrupts and
1868 * exceptions might do.
1874 * Save off the CR2 register. If we take a page fault in the NMI then
1875 * it could corrupt the CR2 value. If the NMI preempts a page fault
1876 * handler before it was able to read the CR2 register, and then the
1877 * NMI itself takes a page fault, the page fault that was preempted
1878 * will read the information from the NMI page fault and not the
1879 * origin fault. Save it off and restore it if it changes.
1880 * Use the r12 callee-saved register.
1884 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1889 /* Did the NMI take a page fault? Restore cr2 if it did */
1896 testl %ebx,%ebx /* swapgs needed? */
1901 /* Pop the extra iret frame at once */
1904 /* Clear the NMI executing stack variable */
1910 ENTRY(ignore_sysret)
1918 * End of kprobes section