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>
8 * entry.S contains the system-call and fault low-level handling routines.
10 * Some of this is documented in Documentation/x86/entry_64.txt
12 * A note on terminology:
13 * - iret frame: Architecture defined interrupt frame from SS to RIP
14 * at the top of the kernel process stack.
17 * - ENTRY/END: Define functions in the symbol table.
18 * - TRACE_IRQ_*: Trace hardirq state for lock debugging.
19 * - idtentry: Define exception entry points.
21 #include <linux/linkage.h>
22 #include <asm/segment.h>
23 #include <asm/cache.h>
24 #include <asm/errno.h>
26 #include <asm/asm-offsets.h>
28 #include <asm/unistd.h>
29 #include <asm/thread_info.h>
30 #include <asm/hw_irq.h>
31 #include <asm/page_types.h>
32 #include <asm/irqflags.h>
33 #include <asm/paravirt.h>
34 #include <asm/percpu.h>
37 #include <asm/pgtable_types.h>
38 #include <asm/export.h>
39 #include <asm/frame.h>
40 #include <linux/err.h>
43 .section .entry.text, "ax"
45 #ifdef CONFIG_PARAVIRT
46 ENTRY(native_usergs_sysret64)
50 END(native_usergs_sysret64)
51 #endif /* CONFIG_PARAVIRT */
53 .macro TRACE_IRQS_IRETQ
54 #ifdef CONFIG_TRACE_IRQFLAGS
55 bt $9, EFLAGS(%rsp) /* interrupts off? */
63 * When dynamic function tracer is enabled it will add a breakpoint
64 * to all locations that it is about to modify, sync CPUs, update
65 * all the code, sync CPUs, then remove the breakpoints. In this time
66 * if lockdep is enabled, it might jump back into the debug handler
67 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
69 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
70 * make sure the stack pointer does not get reset back to the top
71 * of the debug stack, and instead just reuses the current stack.
73 #if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
75 .macro TRACE_IRQS_OFF_DEBUG
76 call debug_stack_set_zero
78 call debug_stack_reset
81 .macro TRACE_IRQS_ON_DEBUG
82 call debug_stack_set_zero
84 call debug_stack_reset
87 .macro TRACE_IRQS_IRETQ_DEBUG
88 bt $9, EFLAGS(%rsp) /* interrupts off? */
95 # define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
96 # define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
97 # define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
101 * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
103 * This is the only entry point used for 64-bit system calls. The
104 * hardware interface is reasonably well designed and the register to
105 * argument mapping Linux uses fits well with the registers that are
106 * available when SYSCALL is used.
108 * SYSCALL instructions can be found inlined in libc implementations as
109 * well as some other programs and libraries. There are also a handful
110 * of SYSCALL instructions in the vDSO used, for example, as a
111 * clock_gettimeofday fallback.
113 * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
114 * then loads new ss, cs, and rip from previously programmed MSRs.
115 * rflags gets masked by a value from another MSR (so CLD and CLAC
116 * are not needed). SYSCALL does not save anything on the stack
117 * and does not change rsp.
119 * Registers on entry:
120 * rax system call number
122 * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
126 * r10 arg3 (needs to be moved to rcx to conform to C ABI)
129 * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
131 * Only called from user space.
133 * When user can change pt_regs->foo always force IRET. That is because
134 * it deals with uncanonical addresses better. SYSRET has trouble
135 * with them due to bugs in both AMD and Intel CPUs.
138 ENTRY(entry_SYSCALL_64)
141 * Interrupts are off on entry.
142 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
143 * it is too small to ever cause noticeable irq latency.
147 movq %rsp, PER_CPU_VAR(rsp_scratch)
148 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
152 /* Construct struct pt_regs on stack */
153 pushq $__USER_DS /* pt_regs->ss */
154 pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */
155 pushq %r11 /* pt_regs->flags */
156 pushq $__USER_CS /* pt_regs->cs */
157 pushq %rcx /* pt_regs->ip */
158 GLOBAL(entry_SYSCALL_64_after_hwframe)
159 pushq %rax /* pt_regs->orig_ax */
160 pushq %rdi /* pt_regs->di */
161 pushq %rsi /* pt_regs->si */
162 pushq %rdx /* pt_regs->dx */
163 pushq %rcx /* pt_regs->cx */
164 pushq $-ENOSYS /* pt_regs->ax */
165 pushq %r8 /* pt_regs->r8 */
166 pushq %r9 /* pt_regs->r9 */
167 pushq %r10 /* pt_regs->r10 */
168 pushq %r11 /* pt_regs->r11 */
169 sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
170 UNWIND_HINT_REGS extra=0
173 * If we need to do entry work or if we guess we'll need to do
174 * exit work, go straight to the slow path.
176 movq PER_CPU_VAR(current_task), %r11
177 testl $_TIF_WORK_SYSCALL_ENTRY|_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
178 jnz entry_SYSCALL64_slow_path
180 entry_SYSCALL_64_fastpath:
182 * Easy case: enable interrupts and issue the syscall. If the syscall
183 * needs pt_regs, we'll call a stub that disables interrupts again
184 * and jumps to the slow path.
187 ENABLE_INTERRUPTS(CLBR_NONE)
188 #if __SYSCALL_MASK == ~0
189 cmpq $__NR_syscall_max, %rax
191 andl $__SYSCALL_MASK, %eax
192 cmpl $__NR_syscall_max, %eax
194 ja 1f /* return -ENOSYS (already in pt_regs->ax) */
198 * This call instruction is handled specially in stub_ptregs_64.
199 * It might end up jumping to the slow path. If it jumps, RAX
200 * and all argument registers are clobbered.
202 call *sys_call_table(, %rax, 8)
203 .Lentry_SYSCALL_64_after_fastpath_call:
209 * If we get here, then we know that pt_regs is clean for SYSRET64.
210 * If we see that no exit work is required (which we are required
211 * to check with IRQs off), then we can go straight to SYSRET64.
213 DISABLE_INTERRUPTS(CLBR_ANY)
215 movq PER_CPU_VAR(current_task), %r11
216 testl $_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
220 TRACE_IRQS_ON /* user mode is traced as IRQs on */
222 movq EFLAGS(%rsp), %r11
223 addq $6*8, %rsp /* skip extra regs -- they were preserved */
225 jmp .Lpop_c_regs_except_rcx_r11_and_sysret
229 * The fast path looked good when we started, but something changed
230 * along the way and we need to switch to the slow path. Calling
231 * raise(3) will trigger this, for example. IRQs are off.
234 ENABLE_INTERRUPTS(CLBR_ANY)
237 call syscall_return_slowpath /* returns with IRQs disabled */
238 jmp return_from_SYSCALL_64
240 entry_SYSCALL64_slow_path:
244 call do_syscall_64 /* returns with IRQs disabled */
246 return_from_SYSCALL_64:
247 TRACE_IRQS_IRETQ /* we're about to change IF */
250 * Try to use SYSRET instead of IRET if we're returning to
251 * a completely clean 64-bit userspace context. If we're not,
252 * go to the slow exit path.
257 cmpq %rcx, %r11 /* SYSRET requires RCX == RIP */
258 jne swapgs_restore_regs_and_return_to_usermode
261 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
262 * in kernel space. This essentially lets the user take over
263 * the kernel, since userspace controls RSP.
265 * If width of "canonical tail" ever becomes variable, this will need
266 * to be updated to remain correct on both old and new CPUs.
268 * Change top bits to match most significant bit (47th or 56th bit
269 * depending on paging mode) in the address.
271 shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
272 sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
274 /* If this changed %rcx, it was not canonical */
276 jne swapgs_restore_regs_and_return_to_usermode
278 cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
279 jne swapgs_restore_regs_and_return_to_usermode
282 cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
283 jne swapgs_restore_regs_and_return_to_usermode
286 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
287 * restore RF properly. If the slowpath sets it for whatever reason, we
288 * need to restore it correctly.
290 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
291 * trap from userspace immediately after SYSRET. This would cause an
292 * infinite loop whenever #DB happens with register state that satisfies
293 * the opportunistic SYSRET conditions. For example, single-stepping
296 * movq $stuck_here, %rcx
301 * would never get past 'stuck_here'.
303 testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
304 jnz swapgs_restore_regs_and_return_to_usermode
306 /* nothing to check for RSP */
308 cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
309 jne swapgs_restore_regs_and_return_to_usermode
312 * We win! This label is here just for ease of understanding
313 * perf profiles. Nothing jumps here.
315 syscall_return_via_sysret:
316 /* rcx and r11 are already restored (see code above) */
319 .Lpop_c_regs_except_rcx_r11_and_sysret:
320 popq %rsi /* skip r11 */
325 popq %rsi /* skip rcx */
329 movq RSP-ORIG_RAX(%rsp), %rsp
331 END(entry_SYSCALL_64)
333 ENTRY(stub_ptregs_64)
335 * Syscalls marked as needing ptregs land here.
336 * If we are on the fast path, we need to save the extra regs,
337 * which we achieve by trying again on the slow path. If we are on
338 * the slow path, the extra regs are already saved.
340 * RAX stores a pointer to the C function implementing the syscall.
343 cmpq $.Lentry_SYSCALL_64_after_fastpath_call, (%rsp)
347 * Called from fast path -- disable IRQs again, pop return address
348 * and jump to slow path
350 DISABLE_INTERRUPTS(CLBR_ANY)
353 UNWIND_HINT_REGS extra=0
354 jmp entry_SYSCALL64_slow_path
357 jmp *%rax /* Called from C */
360 .macro ptregs_stub func
363 leaq \func(%rip), %rax
368 /* Instantiate ptregs_stub for each ptregs-using syscall */
369 #define __SYSCALL_64_QUAL_(sym)
370 #define __SYSCALL_64_QUAL_ptregs(sym) ptregs_stub sym
371 #define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym)
372 #include <asm/syscalls_64.h>
378 ENTRY(__switch_to_asm)
381 * Save callee-saved registers
382 * This must match the order in inactive_task_frame
392 movq %rsp, TASK_threadsp(%rdi)
393 movq TASK_threadsp(%rsi), %rsp
395 #ifdef CONFIG_CC_STACKPROTECTOR
396 movq TASK_stack_canary(%rsi), %rbx
397 movq %rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset
400 /* restore callee-saved registers */
412 * A newly forked process directly context switches into this address.
414 * rax: prev task we switched from
415 * rbx: kernel thread func (NULL for user thread)
416 * r12: kernel thread arg
421 call schedule_tail /* rdi: 'prev' task parameter */
423 testq %rbx, %rbx /* from kernel_thread? */
424 jnz 1f /* kernel threads are uncommon */
429 call syscall_return_slowpath /* returns with IRQs disabled */
430 TRACE_IRQS_ON /* user mode is traced as IRQS on */
431 jmp swapgs_restore_regs_and_return_to_usermode
438 * A kernel thread is allowed to return here after successfully
439 * calling do_execve(). Exit to userspace to complete the execve()
447 * Build the entry stubs with some assembler magic.
448 * We pack 1 stub into every 8-byte block.
451 ENTRY(irq_entries_start)
452 vector=FIRST_EXTERNAL_VECTOR
453 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
454 UNWIND_HINT_IRET_REGS
455 pushq $(~vector+0x80) /* Note: always in signed byte range */
460 END(irq_entries_start)
462 .macro DEBUG_ENTRY_ASSERT_IRQS_OFF
463 #ifdef CONFIG_DEBUG_ENTRY
465 testl $X86_EFLAGS_IF, (%rsp)
474 * Enters the IRQ stack if we're not already using it. NMI-safe. Clobbers
475 * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
476 * Requires kernel GSBASE.
478 * The invariant is that, if irq_count != -1, then the IRQ stack is in use.
480 .macro ENTER_IRQ_STACK regs=1 old_rsp
481 DEBUG_ENTRY_ASSERT_IRQS_OFF
485 UNWIND_HINT_REGS base=\old_rsp
488 incl PER_CPU_VAR(irq_count)
489 jnz .Lirq_stack_push_old_rsp_\@
492 * Right now, if we just incremented irq_count to zero, we've
493 * claimed the IRQ stack but we haven't switched to it yet.
495 * If anything is added that can interrupt us here without using IST,
496 * it must be *extremely* careful to limit its stack usage. This
497 * could include kprobes and a hypothetical future IST-less #DB
500 * The OOPS unwinder relies on the word at the top of the IRQ
501 * stack linking back to the previous RSP for the entire time we're
502 * on the IRQ stack. For this to work reliably, we need to write
503 * it before we actually move ourselves to the IRQ stack.
506 movq \old_rsp, PER_CPU_VAR(irq_stack_union + IRQ_STACK_SIZE - 8)
507 movq PER_CPU_VAR(irq_stack_ptr), %rsp
509 #ifdef CONFIG_DEBUG_ENTRY
511 * If the first movq above becomes wrong due to IRQ stack layout
512 * changes, the only way we'll notice is if we try to unwind right
513 * here. Assert that we set up the stack right to catch this type
516 cmpq -8(%rsp), \old_rsp
517 je .Lirq_stack_okay\@
522 .Lirq_stack_push_old_rsp_\@:
526 UNWIND_HINT_REGS indirect=1
531 * Undoes ENTER_IRQ_STACK.
533 .macro LEAVE_IRQ_STACK regs=1
534 DEBUG_ENTRY_ASSERT_IRQS_OFF
535 /* We need to be off the IRQ stack before decrementing irq_count. */
543 * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming
544 * the irq stack but we're not on it.
547 decl PER_CPU_VAR(irq_count)
551 * Interrupt entry/exit.
553 * Interrupt entry points save only callee clobbered registers in fast path.
555 * Entry runs with interrupts off.
558 /* 0(%rsp): ~(interrupt number) */
559 .macro interrupt func
561 ALLOC_PT_GPREGS_ON_STACK
570 * IRQ from user mode. Switch to kernel gsbase and inform context
571 * tracking that we're in kernel mode.
576 * We need to tell lockdep that IRQs are off. We can't do this until
577 * we fix gsbase, and we should do it before enter_from_user_mode
578 * (which can take locks). Since TRACE_IRQS_OFF idempotent,
579 * the simplest way to handle it is to just call it twice if
580 * we enter from user mode. There's no reason to optimize this since
581 * TRACE_IRQS_OFF is a no-op if lockdep is off.
585 CALL_enter_from_user_mode
588 ENTER_IRQ_STACK old_rsp=%rdi
589 /* We entered an interrupt context - irqs are off: */
592 call \func /* rdi points to pt_regs */
596 * The interrupt stubs push (~vector+0x80) onto the stack and
597 * then jump to common_interrupt.
599 .p2align CONFIG_X86_L1_CACHE_SHIFT
602 addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
604 /* 0(%rsp): old RSP */
606 DISABLE_INTERRUPTS(CLBR_ANY)
614 /* Interrupt came from user space */
617 call prepare_exit_to_usermode
620 GLOBAL(swapgs_restore_regs_and_return_to_usermode)
621 #ifdef CONFIG_DEBUG_ENTRY
622 /* Assert that pt_regs indicates user mode. */
631 addq $8, %rsp /* skip regs->orig_ax */
635 /* Returning to kernel space */
637 #ifdef CONFIG_PREEMPT
638 /* Interrupts are off */
639 /* Check if we need preemption */
640 bt $9, EFLAGS(%rsp) /* were interrupts off? */
642 0: cmpl $0, PER_CPU_VAR(__preempt_count)
644 call preempt_schedule_irq
649 * The iretq could re-enable interrupts:
653 GLOBAL(restore_regs_and_return_to_kernel)
654 #ifdef CONFIG_DEBUG_ENTRY
655 /* Assert that pt_regs indicates kernel mode. */
663 addq $8, %rsp /* skip regs->orig_ax */
667 UNWIND_HINT_IRET_REGS
669 * Are we returning to a stack segment from the LDT? Note: in
670 * 64-bit mode SS:RSP on the exception stack is always valid.
672 #ifdef CONFIG_X86_ESPFIX64
673 testb $4, (SS-RIP)(%rsp)
674 jnz native_irq_return_ldt
677 .global native_irq_return_iret
678 native_irq_return_iret:
680 * This may fault. Non-paranoid faults on return to userspace are
681 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
682 * Double-faults due to espfix64 are handled in do_double_fault.
683 * Other faults here are fatal.
687 #ifdef CONFIG_X86_ESPFIX64
688 native_irq_return_ldt:
690 * We are running with user GSBASE. All GPRs contain their user
691 * values. We have a percpu ESPFIX stack that is eight slots
692 * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom
693 * of the ESPFIX stack.
695 * We clobber RAX and RDI in this code. We stash RDI on the
696 * normal stack and RAX on the ESPFIX stack.
698 * The ESPFIX stack layout we set up looks like this:
700 * --- top of ESPFIX stack ---
705 * RIP <-- RSP points here when we're done
706 * RAX <-- espfix_waddr points here
707 * --- bottom of ESPFIX stack ---
710 pushq %rdi /* Stash user RDI */
712 movq PER_CPU_VAR(espfix_waddr), %rdi
713 movq %rax, (0*8)(%rdi) /* user RAX */
714 movq (1*8)(%rsp), %rax /* user RIP */
715 movq %rax, (1*8)(%rdi)
716 movq (2*8)(%rsp), %rax /* user CS */
717 movq %rax, (2*8)(%rdi)
718 movq (3*8)(%rsp), %rax /* user RFLAGS */
719 movq %rax, (3*8)(%rdi)
720 movq (5*8)(%rsp), %rax /* user SS */
721 movq %rax, (5*8)(%rdi)
722 movq (4*8)(%rsp), %rax /* user RSP */
723 movq %rax, (4*8)(%rdi)
724 /* Now RAX == RSP. */
726 andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */
727 popq %rdi /* Restore user RDI */
730 * espfix_stack[31:16] == 0. The page tables are set up such that
731 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
732 * espfix_waddr for any X. That is, there are 65536 RO aliases of
733 * the same page. Set up RSP so that RSP[31:16] contains the
734 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
735 * still points to an RO alias of the ESPFIX stack.
737 orq PER_CPU_VAR(espfix_stack), %rax
740 UNWIND_HINT_IRET_REGS offset=8
743 * At this point, we cannot write to the stack any more, but we can
746 popq %rax /* Restore user RAX */
749 * RSP now points to an ordinary IRET frame, except that the page
750 * is read-only and RSP[31:16] are preloaded with the userspace
751 * values. We can now IRET back to userspace.
753 jmp native_irq_return_iret
755 END(common_interrupt)
760 .macro apicinterrupt3 num sym do_sym
762 UNWIND_HINT_IRET_REGS
771 #ifdef CONFIG_TRACING
772 #define trace(sym) trace_##sym
773 #define smp_trace(sym) smp_trace_##sym
775 .macro trace_apicinterrupt num sym
776 apicinterrupt3 \num trace(\sym) smp_trace(\sym)
779 .macro trace_apicinterrupt num sym do_sym
783 /* Make sure APIC interrupt handlers end up in the irqentry section: */
784 #define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
785 #define POP_SECTION_IRQENTRY .popsection
787 .macro apicinterrupt num sym do_sym
788 PUSH_SECTION_IRQENTRY
789 apicinterrupt3 \num \sym \do_sym
790 trace_apicinterrupt \num \sym
795 apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
796 apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
800 apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
803 apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
804 apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
806 #ifdef CONFIG_HAVE_KVM
807 apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
808 apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
809 apicinterrupt3 POSTED_INTR_NESTED_VECTOR kvm_posted_intr_nested_ipi smp_kvm_posted_intr_nested_ipi
812 #ifdef CONFIG_X86_MCE_THRESHOLD
813 apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
816 #ifdef CONFIG_X86_MCE_AMD
817 apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
820 #ifdef CONFIG_X86_THERMAL_VECTOR
821 apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
825 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
826 apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
827 apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
830 apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
831 apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
833 #ifdef CONFIG_IRQ_WORK
834 apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
838 * Exception entry points.
840 #define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
842 .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
844 UNWIND_HINT_IRET_REGS offset=\has_error_code*8
847 .if \shift_ist != -1 && \paranoid == 0
848 .error "using shift_ist requires paranoid=1"
853 .if \has_error_code == 0
854 pushq $-1 /* ORIG_RAX: no syscall to restart */
857 ALLOC_PT_GPREGS_ON_STACK
861 testb $3, CS(%rsp) /* If coming from userspace, switch stacks */
869 /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
873 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
879 movq %rsp, %rdi /* pt_regs pointer */
882 movq ORIG_RAX(%rsp), %rsi /* get error code */
883 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
885 xorl %esi, %esi /* no error code */
889 subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
895 addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
898 /* these procedures expect "no swapgs" flag in ebx */
907 * Paranoid entry from userspace. Switch stacks and treat it
908 * as a normal entry. This means that paranoid handlers
909 * run in real process context if user_mode(regs).
915 movq %rsp, %rdi /* pt_regs pointer */
917 movq %rax, %rsp /* switch stack */
919 movq %rsp, %rdi /* pt_regs pointer */
922 movq ORIG_RAX(%rsp), %rsi /* get error code */
923 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
925 xorl %esi, %esi /* no error code */
930 jmp error_exit /* %ebx: no swapgs flag */
935 idtentry divide_error do_divide_error has_error_code=0
936 idtentry overflow do_overflow has_error_code=0
937 idtentry bounds do_bounds has_error_code=0
938 idtentry invalid_op do_invalid_op has_error_code=0
939 idtentry device_not_available do_device_not_available has_error_code=0
940 idtentry double_fault do_double_fault has_error_code=1 paranoid=2
941 idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
942 idtentry invalid_TSS do_invalid_TSS has_error_code=1
943 idtentry segment_not_present do_segment_not_present has_error_code=1
944 idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
945 idtentry coprocessor_error do_coprocessor_error has_error_code=0
946 idtentry alignment_check do_alignment_check has_error_code=1
947 idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
951 * Reload gs selector with exception handling
954 ENTRY(native_load_gs_index)
957 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
961 2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
966 ENDPROC(native_load_gs_index)
967 EXPORT_SYMBOL(native_load_gs_index)
969 _ASM_EXTABLE(.Lgs_change, bad_gs)
970 .section .fixup, "ax"
971 /* running with kernelgs */
973 SWAPGS /* switch back to user gs */
975 /* This can't be a string because the preprocessor needs to see it. */
976 movl $__USER_DS, %eax
979 ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
985 /* Call softirq on interrupt stack. Interrupts are off. */
986 ENTRY(do_softirq_own_stack)
989 ENTER_IRQ_STACK regs=0 old_rsp=%r11
991 LEAVE_IRQ_STACK regs=0
994 ENDPROC(do_softirq_own_stack)
997 idtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0
1000 * A note on the "critical region" in our callback handler.
1001 * We want to avoid stacking callback handlers due to events occurring
1002 * during handling of the last event. To do this, we keep events disabled
1003 * until we've done all processing. HOWEVER, we must enable events before
1004 * popping the stack frame (can't be done atomically) and so it would still
1005 * be possible to get enough handler activations to overflow the stack.
1006 * Although unlikely, bugs of that kind are hard to track down, so we'd
1007 * like to avoid the possibility.
1008 * So, on entry to the handler we detect whether we interrupted an
1009 * existing activation in its critical region -- if so, we pop the current
1010 * activation and restart the handler using the previous one.
1012 ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */
1015 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1016 * see the correct pointer to the pt_regs
1019 movq %rdi, %rsp /* we don't return, adjust the stack frame */
1022 ENTER_IRQ_STACK old_rsp=%r10
1023 call xen_evtchn_do_upcall
1026 #ifndef CONFIG_PREEMPT
1027 call xen_maybe_preempt_hcall
1030 END(xen_do_hypervisor_callback)
1033 * Hypervisor uses this for application faults while it executes.
1034 * We get here for two reasons:
1035 * 1. Fault while reloading DS, ES, FS or GS
1036 * 2. Fault while executing IRET
1037 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1038 * registers that could be reloaded and zeroed the others.
1039 * Category 2 we fix up by killing the current process. We cannot use the
1040 * normal Linux return path in this case because if we use the IRET hypercall
1041 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1042 * We distinguish between categories by comparing each saved segment register
1043 * with its current contents: any discrepancy means we in category 1.
1045 ENTRY(xen_failsafe_callback)
1048 cmpw %cx, 0x10(%rsp)
1051 cmpw %cx, 0x18(%rsp)
1054 cmpw %cx, 0x20(%rsp)
1057 cmpw %cx, 0x28(%rsp)
1059 /* All segments match their saved values => Category 2 (Bad IRET). */
1064 UNWIND_HINT_IRET_REGS offset=8
1065 jmp general_protection
1066 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1070 UNWIND_HINT_IRET_REGS
1071 pushq $-1 /* orig_ax = -1 => not a system call */
1072 ALLOC_PT_GPREGS_ON_STACK
1075 ENCODE_FRAME_POINTER
1077 END(xen_failsafe_callback)
1079 apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1080 xen_hvm_callback_vector xen_evtchn_do_upcall
1082 #endif /* CONFIG_XEN */
1084 #if IS_ENABLED(CONFIG_HYPERV)
1085 apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1086 hyperv_callback_vector hyperv_vector_handler
1087 #endif /* CONFIG_HYPERV */
1089 idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1090 idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1091 idtentry stack_segment do_stack_segment has_error_code=1
1094 idtentry xennmi do_nmi has_error_code=0
1095 idtentry xendebug do_debug has_error_code=0
1096 idtentry xenint3 do_int3 has_error_code=0
1099 idtentry general_protection do_general_protection has_error_code=1
1100 idtentry page_fault do_page_fault has_error_code=1
1102 #ifdef CONFIG_KVM_GUEST
1103 idtentry async_page_fault do_async_page_fault has_error_code=1
1106 #ifdef CONFIG_X86_MCE
1107 idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
1111 * Save all registers in pt_regs, and switch gs if needed.
1112 * Use slow, but surefire "are we in kernel?" check.
1113 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1115 ENTRY(paranoid_entry)
1120 ENCODE_FRAME_POINTER 8
1122 movl $MSR_GS_BASE, %ecx
1125 js 1f /* negative -> in kernel */
1132 * "Paranoid" exit path from exception stack. This is invoked
1133 * only on return from non-NMI IST interrupts that came
1134 * from kernel space.
1136 * We may be returning to very strange contexts (e.g. very early
1137 * in syscall entry), so checking for preemption here would
1138 * be complicated. Fortunately, we there's no good reason
1139 * to try to handle preemption here.
1141 * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
1143 ENTRY(paranoid_exit)
1145 DISABLE_INTERRUPTS(CLBR_ANY)
1146 TRACE_IRQS_OFF_DEBUG
1147 testl %ebx, %ebx /* swapgs needed? */
1148 jnz .Lparanoid_exit_no_swapgs
1151 jmp .Lparanoid_exit_restore
1152 .Lparanoid_exit_no_swapgs:
1153 TRACE_IRQS_IRETQ_DEBUG
1154 .Lparanoid_exit_restore:
1155 jmp restore_regs_and_return_to_kernel
1159 * Save all registers in pt_regs, and switch gs if needed.
1160 * Return: EBX=0: came from user mode; EBX=1: otherwise
1167 ENCODE_FRAME_POINTER 8
1169 testb $3, CS+8(%rsp)
1170 jz .Lerror_kernelspace
1173 * We entered from user mode or we're pretending to have entered
1174 * from user mode due to an IRET fault.
1178 .Lerror_entry_from_usermode_after_swapgs:
1180 * We need to tell lockdep that IRQs are off. We can't do this until
1181 * we fix gsbase, and we should do it before enter_from_user_mode
1182 * (which can take locks).
1185 CALL_enter_from_user_mode
1193 * There are two places in the kernel that can potentially fault with
1194 * usergs. Handle them here. B stepping K8s sometimes report a
1195 * truncated RIP for IRET exceptions returning to compat mode. Check
1196 * for these here too.
1198 .Lerror_kernelspace:
1200 leaq native_irq_return_iret(%rip), %rcx
1201 cmpq %rcx, RIP+8(%rsp)
1203 movl %ecx, %eax /* zero extend */
1204 cmpq %rax, RIP+8(%rsp)
1206 cmpq $.Lgs_change, RIP+8(%rsp)
1207 jne .Lerror_entry_done
1210 * hack: .Lgs_change can fail with user gsbase. If this happens, fix up
1211 * gsbase and proceed. We'll fix up the exception and land in
1212 * .Lgs_change's error handler with kernel gsbase.
1215 jmp .Lerror_entry_done
1218 /* Fix truncated RIP */
1219 movq %rcx, RIP+8(%rsp)
1224 * We came from an IRET to user mode, so we have user gsbase.
1225 * Switch to kernel gsbase:
1230 * Pretend that the exception came from user mode: set up pt_regs
1231 * as if we faulted immediately after IRET and clear EBX so that
1232 * error_exit knows that we will be returning to user mode.
1238 jmp .Lerror_entry_from_usermode_after_swapgs
1243 * On entry, EBX is a "return to kernel mode" flag:
1244 * 1: already in kernel mode, don't need SWAPGS
1245 * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
1249 DISABLE_INTERRUPTS(CLBR_ANY)
1257 * Runs on exception stack. Xen PV does not go through this path at all,
1258 * so we can use real assembly here.
1261 UNWIND_HINT_IRET_REGS
1264 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1265 * the iretq it performs will take us out of NMI context.
1266 * This means that we can have nested NMIs where the next
1267 * NMI is using the top of the stack of the previous NMI. We
1268 * can't let it execute because the nested NMI will corrupt the
1269 * stack of the previous NMI. NMI handlers are not re-entrant
1272 * To handle this case we do the following:
1273 * Check the a special location on the stack that contains
1274 * a variable that is set when NMIs are executing.
1275 * The interrupted task's stack is also checked to see if it
1277 * If the variable is not set and the stack is not the NMI
1279 * o Set the special variable on the stack
1280 * o Copy the interrupt frame into an "outermost" location on the
1282 * o Copy the interrupt frame into an "iret" location on the stack
1283 * o Continue processing the NMI
1284 * If the variable is set or the previous stack is the NMI stack:
1285 * o Modify the "iret" location to jump to the repeat_nmi
1286 * o return back to the first NMI
1288 * Now on exit of the first NMI, we first clear the stack variable
1289 * The NMI stack will tell any nested NMIs at that point that it is
1290 * nested. Then we pop the stack normally with iret, and if there was
1291 * a nested NMI that updated the copy interrupt stack frame, a
1292 * jump will be made to the repeat_nmi code that will handle the second
1295 * However, espfix prevents us from directly returning to userspace
1296 * with a single IRET instruction. Similarly, IRET to user mode
1297 * can fault. We therefore handle NMIs from user space like
1298 * other IST entries.
1303 /* Use %rdx as our temp variable throughout */
1306 testb $3, CS-RIP+8(%rsp)
1307 jz .Lnmi_from_kernel
1310 * NMI from user mode. We need to run on the thread stack, but we
1311 * can't go through the normal entry paths: NMIs are masked, and
1312 * we don't want to enable interrupts, because then we'll end
1313 * up in an awkward situation in which IRQs are on but NMIs
1316 * We also must not push anything to the stack before switching
1317 * stacks lest we corrupt the "NMI executing" variable.
1323 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
1324 UNWIND_HINT_IRET_REGS base=%rdx offset=8
1325 pushq 5*8(%rdx) /* pt_regs->ss */
1326 pushq 4*8(%rdx) /* pt_regs->rsp */
1327 pushq 3*8(%rdx) /* pt_regs->flags */
1328 pushq 2*8(%rdx) /* pt_regs->cs */
1329 pushq 1*8(%rdx) /* pt_regs->rip */
1330 UNWIND_HINT_IRET_REGS
1331 pushq $-1 /* pt_regs->orig_ax */
1332 pushq %rdi /* pt_regs->di */
1333 pushq %rsi /* pt_regs->si */
1334 pushq (%rdx) /* pt_regs->dx */
1335 pushq %rcx /* pt_regs->cx */
1336 pushq %rax /* pt_regs->ax */
1337 pushq %r8 /* pt_regs->r8 */
1338 pushq %r9 /* pt_regs->r9 */
1339 pushq %r10 /* pt_regs->r10 */
1340 pushq %r11 /* pt_regs->r11 */
1341 pushq %rbx /* pt_regs->rbx */
1342 pushq %rbp /* pt_regs->rbp */
1343 pushq %r12 /* pt_regs->r12 */
1344 pushq %r13 /* pt_regs->r13 */
1345 pushq %r14 /* pt_regs->r14 */
1346 pushq %r15 /* pt_regs->r15 */
1348 ENCODE_FRAME_POINTER
1351 * At this point we no longer need to worry about stack damage
1352 * due to nesting -- we're on the normal thread stack and we're
1353 * done with the NMI stack.
1361 * Return back to user mode. We must *not* do the normal exit
1362 * work, because we don't want to enable interrupts.
1364 jmp swapgs_restore_regs_and_return_to_usermode
1368 * Here's what our stack frame will look like:
1369 * +---------------------------------------------------------+
1371 * | original Return RSP |
1372 * | original RFLAGS |
1375 * +---------------------------------------------------------+
1376 * | temp storage for rdx |
1377 * +---------------------------------------------------------+
1378 * | "NMI executing" variable |
1379 * +---------------------------------------------------------+
1380 * | iret SS } Copied from "outermost" frame |
1381 * | iret Return RSP } on each loop iteration; overwritten |
1382 * | iret RFLAGS } by a nested NMI to force another |
1383 * | iret CS } iteration if needed. |
1385 * +---------------------------------------------------------+
1386 * | outermost SS } initialized in first_nmi; |
1387 * | outermost Return RSP } will not be changed before |
1388 * | outermost RFLAGS } NMI processing is done. |
1389 * | outermost CS } Copied to "iret" frame on each |
1390 * | outermost RIP } iteration. |
1391 * +---------------------------------------------------------+
1393 * +---------------------------------------------------------+
1395 * The "original" frame is used by hardware. Before re-enabling
1396 * NMIs, we need to be done with it, and we need to leave enough
1397 * space for the asm code here.
1399 * We return by executing IRET while RSP points to the "iret" frame.
1400 * That will either return for real or it will loop back into NMI
1403 * The "outermost" frame is copied to the "iret" frame on each
1404 * iteration of the loop, so each iteration starts with the "iret"
1405 * frame pointing to the final return target.
1409 * Determine whether we're a nested NMI.
1411 * If we interrupted kernel code between repeat_nmi and
1412 * end_repeat_nmi, then we are a nested NMI. We must not
1413 * modify the "iret" frame because it's being written by
1414 * the outer NMI. That's okay; the outer NMI handler is
1415 * about to about to call do_nmi anyway, so we can just
1416 * resume the outer NMI.
1419 movq $repeat_nmi, %rdx
1422 movq $end_repeat_nmi, %rdx
1428 * Now check "NMI executing". If it's set, then we're nested.
1429 * This will not detect if we interrupted an outer NMI just
1436 * Now test if the previous stack was an NMI stack. This covers
1437 * the case where we interrupt an outer NMI after it clears
1438 * "NMI executing" but before IRET. We need to be careful, though:
1439 * there is one case in which RSP could point to the NMI stack
1440 * despite there being no NMI active: naughty userspace controls
1441 * RSP at the very beginning of the SYSCALL targets. We can
1442 * pull a fast one on naughty userspace, though: we program
1443 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1444 * if it controls the kernel's RSP. We set DF before we clear
1448 /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1449 cmpq %rdx, 4*8(%rsp)
1450 /* If the stack pointer is above the NMI stack, this is a normal NMI */
1453 subq $EXCEPTION_STKSZ, %rdx
1454 cmpq %rdx, 4*8(%rsp)
1455 /* If it is below the NMI stack, it is a normal NMI */
1458 /* Ah, it is within the NMI stack. */
1460 testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1461 jz first_nmi /* RSP was user controlled. */
1463 /* This is a nested NMI. */
1467 * Modify the "iret" frame to point to repeat_nmi, forcing another
1468 * iteration of NMI handling.
1471 leaq -10*8(%rsp), %rdx
1478 /* Put stack back */
1484 /* We are returning to kernel mode, so this cannot result in a fault. */
1491 /* Make room for "NMI executing". */
1494 /* Leave room for the "iret" frame */
1497 /* Copy the "original" frame to the "outermost" frame */
1501 UNWIND_HINT_IRET_REGS
1503 /* Everything up to here is safe from nested NMIs */
1505 #ifdef CONFIG_DEBUG_ENTRY
1507 * For ease of testing, unmask NMIs right away. Disabled by
1508 * default because IRET is very expensive.
1511 pushq %rsp /* RSP (minus 8 because of the previous push) */
1512 addq $8, (%rsp) /* Fix up RSP */
1514 pushq $__KERNEL_CS /* CS */
1516 iretq /* continues at repeat_nmi below */
1517 UNWIND_HINT_IRET_REGS
1523 * If there was a nested NMI, the first NMI's iret will return
1524 * here. But NMIs are still enabled and we can take another
1525 * nested NMI. The nested NMI checks the interrupted RIP to see
1526 * if it is between repeat_nmi and end_repeat_nmi, and if so
1527 * it will just return, as we are about to repeat an NMI anyway.
1528 * This makes it safe to copy to the stack frame that a nested
1531 * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
1532 * we're repeating an NMI, gsbase has the same value that it had on
1533 * the first iteration. paranoid_entry will load the kernel
1534 * gsbase if needed before we call do_nmi. "NMI executing"
1537 movq $1, 10*8(%rsp) /* Set "NMI executing". */
1540 * Copy the "outermost" frame to the "iret" frame. NMIs that nest
1541 * here must not modify the "iret" frame while we're writing to
1542 * it or it will end up containing garbage.
1552 * Everything below this point can be preempted by a nested NMI.
1553 * If this happens, then the inner NMI will change the "iret"
1554 * frame to point back to repeat_nmi.
1556 pushq $-1 /* ORIG_RAX: no syscall to restart */
1557 ALLOC_PT_GPREGS_ON_STACK
1560 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1561 * as we should not be calling schedule in NMI context.
1562 * Even with normal interrupts enabled. An NMI should not be
1563 * setting NEED_RESCHED or anything that normal interrupts and
1564 * exceptions might do.
1569 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1574 testl %ebx, %ebx /* swapgs needed? */
1583 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1584 * at the "iret" frame.
1589 * Clear "NMI executing". Set DF first so that we can easily
1590 * distinguish the remaining code between here and IRET from
1591 * the SYSCALL entry and exit paths.
1593 * We arguably should just inspect RIP instead, but I (Andy) wrote
1594 * this code when I had the misapprehension that Xen PV supported
1595 * NMIs, and Xen PV would break that approach.
1598 movq $0, 5*8(%rsp) /* clear "NMI executing" */
1601 * iretq reads the "iret" frame and exits the NMI stack in a
1602 * single instruction. We are returning to kernel mode, so this
1603 * cannot result in a fault. Similarly, we don't need to worry
1604 * about espfix64 on the way back to kernel mode.
1609 ENTRY(ignore_sysret)
1615 ENTRY(rewind_stack_do_exit)
1617 /* Prevent any naive code from trying to unwind to our caller. */
1620 movq PER_CPU_VAR(cpu_current_top_of_stack), %rax
1621 leaq -PTREGS_SIZE(%rax), %rsp
1622 UNWIND_HINT_FUNC sp_offset=PTREGS_SIZE
1625 END(rewind_stack_do_exit)