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>
7 * Jun Nakajima <jun.nakajima@intel.com>
8 * Asit Mallick <asit.k.mallick@intel.com>
13 * entry.S contains the system-call and fault low-level handling routines.
15 * Some of this is documented in Documentation/x86/entry_64.txt
17 * NOTE: This code handles signal-recognition, which happens every time
18 * after an interrupt and after each system call.
20 * Normal syscalls and interrupts don't save a full stack frame, this is
21 * only done for syscall tracing, signals or fork/exec et.al.
23 * A note on terminology:
24 * - top of stack: Architecture defined interrupt frame from SS to RIP
25 * at the top of the kernel process stack.
26 * - partial stack frame: partially saved registers up to R11.
27 * - full stack frame: Like partial stack frame, but all register saved.
30 * - CFI macros are used to generate dwarf2 unwind information for better
31 * backtraces. They don't change any code.
32 * - SAVE_ALL/RESTORE_ALL - Save/restore all registers
33 * - SAVE_ARGS/RESTORE_ARGS - Save/restore registers that C functions modify.
34 * There are unfortunately lots of special cases where some registers
35 * not touched. The macro is a big mess that should be cleaned up.
36 * - SAVE_REST/RESTORE_REST - Handle the registers not saved by SAVE_ARGS.
37 * Gives a full stack frame.
38 * - ENTRY/END Define functions in the symbol table.
39 * - FIXUP_TOP_OF_STACK/RESTORE_TOP_OF_STACK - Fix up the hardware stack
40 * frame that is otherwise undefined after a SYSCALL
41 * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
42 * - errorentry/paranoidentry/zeroentry - Define exception entry points.
45 #include <linux/linkage.h>
46 #include <asm/segment.h>
47 #include <asm/cache.h>
48 #include <asm/errno.h>
49 #include <asm/dwarf2.h>
50 #include <asm/calling.h>
51 #include <asm/asm-offsets.h>
53 #include <asm/unistd.h>
54 #include <asm/thread_info.h>
55 #include <asm/hw_irq.h>
56 #include <asm/page_types.h>
57 #include <asm/irqflags.h>
58 #include <asm/processor-flags.h>
59 #include <asm/ftrace.h>
60 #include <asm/percpu.h>
61 #include <linux/err.h>
62 #include <xen/interface/xen.h>
63 #include <xen/interface/features.h>
65 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
66 #include <linux/elf-em.h>
67 #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
68 #define __AUDIT_ARCH_64BIT 0x80000000
69 #define __AUDIT_ARCH_LE 0x40000000
72 .section .entry.text, "ax"
74 #ifdef CONFIG_FUNCTION_TRACER
75 #ifdef CONFIG_DYNAMIC_FTRACE
81 cmpl $0, function_trace_stop
88 subq $MCOUNT_INSN_SIZE, %rdi
95 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
96 GLOBAL(ftrace_graph_call)
104 #else /* ! CONFIG_DYNAMIC_FTRACE */
106 cmpl $0, function_trace_stop
109 cmpq $ftrace_stub, ftrace_trace_function
112 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
113 cmpq $ftrace_stub, ftrace_graph_return
114 jnz ftrace_graph_caller
116 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
117 jnz ftrace_graph_caller
126 movq 0x38(%rsp), %rdi
128 subq $MCOUNT_INSN_SIZE, %rdi
130 call *ftrace_trace_function
136 #endif /* CONFIG_DYNAMIC_FTRACE */
137 #endif /* CONFIG_FUNCTION_TRACER */
139 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
140 ENTRY(ftrace_graph_caller)
141 cmpl $0, function_trace_stop
147 movq 0x38(%rsp), %rsi
149 subq $MCOUNT_INSN_SIZE, %rsi
151 call prepare_ftrace_return
156 END(ftrace_graph_caller)
158 GLOBAL(return_to_handler)
161 /* Save the return values */
166 call ftrace_return_to_handler
176 #ifndef CONFIG_PREEMPT
177 #define retint_kernel retint_restore_args
181 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
182 #ifdef CONFIG_TRACE_IRQFLAGS
183 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
190 NMI_MASK = 0x80000000
193 * C code is not supposed to know about undefined top of stack. Every time
194 * a C function with an pt_regs argument is called from the SYSCALL based
195 * fast path FIXUP_TOP_OF_STACK is needed.
196 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
200 /* %rsp:at FRAMEEND */
201 .macro FIXUP_TOP_OF_STACK tmp offset=0
202 movq $__USER_CS,CS+\offset(%rsp)
203 movq $-1,RCX+\offset(%rsp)
206 .macro RESTORE_TOP_OF_STACK tmp offset=0
209 .macro FAKE_STACK_FRAME child_rip
210 /* push in order ss, rsp, eflags, cs, rip */
212 pushq_cfi $__KERNEL_DS /* ss */
213 /*CFI_REL_OFFSET ss,0*/
214 pushq_cfi %rax /* rsp */
216 pushq_cfi $(X86_EFLAGS_IF|X86_EFLAGS_BIT1) /* eflags - interrupts on */
217 /*CFI_REL_OFFSET rflags,0*/
218 pushq_cfi $__KERNEL_CS /* cs */
219 /*CFI_REL_OFFSET cs,0*/
220 pushq_cfi \child_rip /* rip */
222 pushq_cfi %rax /* orig rax */
225 .macro UNFAKE_STACK_FRAME
227 CFI_ADJUST_CFA_OFFSET -(6*8)
231 * initial frame state for syscall
233 .macro BASIC_FRAME start=1 offset=0
237 CFI_DEF_CFA rsp, SS+8+\offset-RIP
239 CFI_DEF_CFA_OFFSET SS+8+\offset-RIP
241 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
242 CFI_REL_OFFSET rsp, RSP+\offset-RIP
243 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
244 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
245 CFI_REL_OFFSET rip, RIP+\offset-RIP
249 * initial frame state for interrupts (and exceptions without error code)
251 .macro INTR_FRAME start=1 offset=0
253 BASIC_FRAME 1, \offset+2*8
254 CFI_REL_OFFSET rcx, 0+\offset
255 CFI_REL_OFFSET r11, 8+\offset
257 BASIC_FRAME \start, \offset
262 * initial frame state for exceptions with error code (and interrupts
263 * with vector already pushed)
265 .macro XCPT_FRAME start=1 offset=0
266 INTR_FRAME \start, RIP+\offset-ORIG_RAX
270 * frame that enables calling into C.
272 .macro PARTIAL_FRAME start=1 offset=0
274 XCPT_FRAME 2*\start, ORIG_RAX+\offset-ARGOFFSET
276 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
277 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
278 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
279 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
280 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
281 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
282 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
283 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
284 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
288 * frame that enables passing a complete pt_regs to a C function.
290 .macro DEFAULT_FRAME start=1 offset=0
292 PARTIAL_FRAME \start, R11+\offset-R15
294 CFI_REL_OFFSET rbx, RBX+\offset
295 CFI_REL_OFFSET rbp, RBP+\offset
296 CFI_REL_OFFSET r12, R12+\offset
297 CFI_REL_OFFSET r13, R13+\offset
298 CFI_REL_OFFSET r14, R14+\offset
299 CFI_REL_OFFSET r15, R15+\offset
303 * Must be consistent with the definition in arch-x86/xen-x86_64.h:
304 * struct iret_context {
305 * u64 rax, r11, rcx, flags, rip, cs, rflags, rsp, ss;
307 * with rax, r11, and rcx being taken care of in the hypercall stub.
309 .macro HYPERVISOR_IRET flag
310 .if \flag == 0 # return from syscall always uses the hypercall
313 testl $NMI_MASK,2*8(%rsp)
316 cmpb $0,(xen_features+XENFEAT_supervisor_mode_kernel)(%rip)
319 /* Direct iret to kernel space. Correct CS and SS. */
325 2: /* Slow iret via hypervisor. */
326 andl $~NMI_MASK, 2*8(%rsp)
327 pushq $\flag & VGCF_in_syscall
328 jmp hypercall_page + (__HYPERVISOR_iret * 32)
332 /* save partial stack frame */
335 /* start from rbp in pt_regs and jump over */
336 movq_cfi rdi, RDI-RBP
337 movq_cfi rsi, RSI-RBP
338 movq_cfi rdx, RDX-RBP
339 movq_cfi rcx, RCX-RBP
340 movq_cfi rax, RAX-RBP
343 movq_cfi r10, R10-RBP
344 movq_cfi r11, R11-RBP
346 /* Save rbp so that we can unwind from get_irq_regs() */
349 /* Save previous stack value */
352 leaq -RBP(%rsp),%rdi /* arg1 for handler */
353 testl $3, CS-RBP(%rsi)
357 * irq_count is used to check if a CPU is already on an interrupt stack
358 * or not. While this is essentially redundant with preempt_count it is
359 * a little cheaper to use a separate counter in the PDA (short of
360 * moving irq_enter into assembly, which would be too much work)
362 1: incl PER_CPU_VAR(irq_count)
363 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
364 CFI_DEF_CFA_REGISTER rsi
366 /* Store previous stack value */
368 CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
369 0x77 /* DW_OP_breg7 */, 0, \
370 0x06 /* DW_OP_deref */, \
371 0x08 /* DW_OP_const1u */, SS+8-RBP, \
372 0x22 /* DW_OP_plus */
373 /* We entered an interrupt context - irqs are off: */
380 movq 5*8+16(%rsp), %r11 /* save return address */
381 movq %rbx, RBX+16(%rsp)
382 movq %rbp, RBP+16(%rsp)
383 movq %r12, R12+16(%rsp)
384 movq %r13, R13+16(%rsp)
385 movq %r14, R14+16(%rsp)
386 movq %r15, R15+16(%rsp)
387 movq %r11, 8(%rsp) /* return address */
388 FIXUP_TOP_OF_STACK %r11, 16
394 /* save complete stack frame */
395 .pushsection .kprobes.text, "ax"
397 XCPT_FRAME offset=ORIG_RAX-R15+8
399 movq %rdi, RDI+8(%rsp)
400 movq %rsi, RSI+8(%rsp)
406 movq %r10, R10+8(%rsp)
407 movq %r11, R11+8(%rsp)
409 movq %rbp, RBP+8(%rsp)
410 movq %r12, R12+8(%rsp)
411 movq %r13, R13+8(%rsp)
412 movq %r14, R14+8(%rsp)
413 movq %r15, R15+8(%rsp)
415 movl $MSR_GS_BASE,%ecx
418 js 1f /* negative -> in kernel */
428 * A newly forked process directly context switches into this address.
430 * rdi: prev task we switched from
435 LOCK ; btr $TIF_FORK,TI_flags(%r8)
437 pushq_cfi kernel_eflags(%rip)
438 popfq_cfi # reset kernel eflags
440 call schedule_tail # rdi: 'prev' task parameter
442 GET_THREAD_INFO(%rcx)
446 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
448 /* Need to set the proper %ss (not NULL) for ring 3 iretq */
449 movl $__KERNEL_DS,SS-ARGOFFSET(%rsp)
450 jmp retint_restore_args
452 testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
453 jnz int_ret_from_sys_call
455 RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
456 jmp ret_from_sys_call # go to the SYSRET fastpath
462 * System call entry. Up to 6 arguments in registers are supported.
464 * SYSCALL does not save anything on the stack and does not change the
470 * rax system call number
472 * rcx return address for syscall/sysret, C arg3
475 * r10 arg3 (--> moved to rcx for C)
478 * r11 eflags for syscall/sysret, temporary for C
479 * r12-r15,rbp,rbx saved by C code, not touched.
481 * Interrupts are enabled on entry.
482 * Only called from user space.
484 * XXX if we had a free scratch register we could save the RSP into the stack frame
485 * and report it properly in ps. Unfortunately we haven't.
487 * When user can change the frames always force IRET. That is because
488 * it deals with uncanonical addresses better. SYSRET has trouble
489 * with them due to bugs in both AMD and Intel CPUs.
493 INTR_FRAME start=2 offset=2*8
495 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
496 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
498 system_call_fastpath:
499 #if __SYSCALL_MASK == ~0
500 cmpq $__NR_syscall_max,%rax
502 andl $__SYSCALL_MASK,%eax
503 cmpl $__NR_syscall_max,%eax
507 call *sys_call_table(,%rax,8) # XXX: rip relative
508 movq %rax,RAX-ARGOFFSET(%rsp)
510 * Syscall return path ending with SYSRET (fast path)
511 * Has incomplete stack frame and undefined top of stack.
514 movl $_TIF_ALLWORK_MASK,%edi
518 DISABLE_INTERRUPTS(CLBR_NONE)
520 movl TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET),%edx
525 * sysretq will re-enable interrupts:
531 HYPERVISOR_IRET VGCF_IN_SYSCALL
534 /* Handle reschedules */
535 /* edx: work, edi: workmask */
537 bt $TIF_NEED_RESCHED,%edx
540 ENABLE_INTERRUPTS(CLBR_NONE)
546 /* Handle a signal */
549 ENABLE_INTERRUPTS(CLBR_NONE)
550 #ifdef CONFIG_AUDITSYSCALL
551 bt $TIF_SYSCALL_AUDIT,%edx
555 * We have a signal, or exit tracing or single-step.
556 * These all wind up with the iret return path anyway,
557 * so just join that path right now.
559 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
560 jmp int_check_syscall_exit_work
563 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
564 jmp ret_from_sys_call
566 #ifdef CONFIG_AUDITSYSCALL
568 * Fast path for syscall audit without full syscall trace.
569 * We just call __audit_syscall_entry() directly, and then
570 * jump back to the normal fast path.
573 movq %r10,%r9 /* 6th arg: 4th syscall arg */
574 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
575 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
576 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
577 movq %rax,%rsi /* 2nd arg: syscall number */
578 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
579 call __audit_syscall_entry
580 LOAD_ARGS 0 /* reload call-clobbered registers */
581 jmp system_call_fastpath
584 * Return fast path for syscall audit. Call __audit_syscall_exit()
585 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
589 movq RAX-ARGOFFSET(%rsp),%rsi /* second arg, syscall return value */
590 cmpq $-MAX_ERRNO,%rsi /* is it < -MAX_ERRNO? */
591 setbe %al /* 1 if so, 0 if not */
592 movzbl %al,%edi /* zero-extend that into %edi */
593 call __audit_syscall_exit
594 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
596 #endif /* CONFIG_AUDITSYSCALL */
598 /* Do syscall tracing */
600 #ifdef CONFIG_AUDITSYSCALL
601 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
605 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
606 FIXUP_TOP_OF_STACK %rdi
608 call syscall_trace_enter
610 * Reload arg registers from stack in case ptrace changed them.
611 * We don't reload %rax because syscall_trace_enter() returned
612 * the value it wants us to use in the table lookup.
614 LOAD_ARGS ARGOFFSET, 1
616 #if __SYSCALL_MASK == ~0
617 cmpq $__NR_syscall_max,%rax
619 andl $__SYSCALL_MASK,%eax
620 cmpl $__NR_syscall_max,%eax
622 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
623 movq %r10,%rcx /* fixup for C */
624 call *sys_call_table(,%rax,8)
625 movq %rax,RAX-ARGOFFSET(%rsp)
626 /* Use IRET because user could have changed frame */
629 * Syscall return path ending with IRET.
630 * Has correct top of stack, but partial stack frame.
632 GLOBAL(int_ret_from_sys_call)
633 DISABLE_INTERRUPTS(CLBR_NONE)
635 movl $_TIF_ALLWORK_MASK,%edi
636 /* edi: mask to check */
637 GLOBAL(int_with_check)
639 GET_THREAD_INFO(%rcx)
640 movl TI_flags(%rcx),%edx
643 andl $~TS_COMPAT,TI_status(%rcx)
644 jmp retint_restore_args
646 /* Either reschedule or signal or syscall exit tracking needed. */
647 /* First do a reschedule test. */
648 /* edx: work, edi: workmask */
650 bt $TIF_NEED_RESCHED,%edx
653 ENABLE_INTERRUPTS(CLBR_NONE)
657 DISABLE_INTERRUPTS(CLBR_NONE)
661 /* handle signals and tracing -- both require a full stack frame */
664 ENABLE_INTERRUPTS(CLBR_NONE)
665 int_check_syscall_exit_work:
667 /* Check for syscall exit trace */
668 testl $_TIF_WORK_SYSCALL_EXIT,%edx
671 leaq 8(%rsp),%rdi # &ptregs -> arg1
672 call syscall_trace_leave
674 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
678 testl $_TIF_DO_NOTIFY_MASK,%edx
680 movq %rsp,%rdi # &ptregs -> arg1
681 xorl %esi,%esi # oldset -> arg2
682 call do_notify_resume
683 1: movl $_TIF_WORK_MASK,%edi
686 DISABLE_INTERRUPTS(CLBR_NONE)
693 * Certain special system calls that need to save a complete full stack frame.
695 .macro PTREGSCALL label,func,arg
697 PARTIAL_FRAME 1 8 /* offset 8: return address */
698 subq $REST_SKIP, %rsp
699 CFI_ADJUST_CFA_OFFSET REST_SKIP
701 DEFAULT_FRAME -2 8 /* offset 8: return address */
702 leaq 8(%rsp), \arg /* pt_regs pointer */
704 jmp ptregscall_common
709 PTREGSCALL stub_clone, sys_clone, %r8
710 PTREGSCALL stub_fork, sys_fork, %rdi
711 PTREGSCALL stub_vfork, sys_vfork, %rdi
712 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
713 PTREGSCALL stub_iopl, sys_iopl, %rsi
715 ENTRY(ptregscall_common)
716 DEFAULT_FRAME 1 8 /* offset 8: return address */
717 RESTORE_TOP_OF_STACK %r11, 8
718 movq_cfi_restore R15+8, r15
719 movq_cfi_restore R14+8, r14
720 movq_cfi_restore R13+8, r13
721 movq_cfi_restore R12+8, r12
722 movq_cfi_restore RBP+8, rbp
723 movq_cfi_restore RBX+8, rbx
724 ret $REST_SKIP /* pop extended registers */
726 END(ptregscall_common)
733 FIXUP_TOP_OF_STACK %r11
736 RESTORE_TOP_OF_STACK %r11
739 jmp int_ret_from_sys_call
744 * sigreturn is special because it needs to restore all registers on return.
745 * This cannot be done with SYSRET, so use the IRET return path instead.
747 ENTRY(stub_rt_sigreturn)
753 FIXUP_TOP_OF_STACK %r11
754 call sys_rt_sigreturn
755 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
757 jmp int_ret_from_sys_call
759 END(stub_rt_sigreturn)
761 #ifdef CONFIG_X86_X32_ABI
762 PTREGSCALL stub_x32_sigaltstack, sys32_sigaltstack, %rdx
764 ENTRY(stub_x32_rt_sigreturn)
770 FIXUP_TOP_OF_STACK %r11
771 call sys32_x32_rt_sigreturn
772 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
774 jmp int_ret_from_sys_call
776 END(stub_x32_rt_sigreturn)
778 ENTRY(stub_x32_execve)
783 FIXUP_TOP_OF_STACK %r11
786 RESTORE_TOP_OF_STACK %r11
789 jmp int_ret_from_sys_call
799 retint_with_reschedule:
801 movl $_TIF_WORK_MASK,%edi
804 movl TI_flags(%rcx),%edx
808 retint_restore_args: /* return to kernel space */
809 movl EFLAGS-REST_SKIP(%rsp), %eax
810 shr $9, %eax # EAX[0] == IRET_EFLAGS.IF
812 andb evtchn_upcall_mask(%rsi),%al
813 andb $1,%al # EAX[0] == IRET_EFLAGS.IF & event_mask
814 jnz restore_all_enable_events # != 0 => enable event delivery
819 /* edi: workmask, edx: work */
822 bt $TIF_NEED_RESCHED,%edx
825 ENABLE_INTERRUPTS(CLBR_NONE)
829 GET_THREAD_INFO(%rcx)
830 DISABLE_INTERRUPTS(CLBR_NONE)
835 testl $_TIF_DO_NOTIFY_MASK,%edx
836 jz retint_restore_args
838 ENABLE_INTERRUPTS(CLBR_NONE)
840 movq $-1,ORIG_RAX(%rsp)
841 xorl %esi,%esi # oldset
842 movq %rsp,%rdi # &pt_regs
843 call do_notify_resume
845 DISABLE_INTERRUPTS(CLBR_NONE)
847 GET_THREAD_INFO(%rcx)
848 jmp retint_with_reschedule
850 #ifdef CONFIG_PREEMPT
851 /* Returning to kernel space. Check if we need preemption */
852 /* rcx: threadinfo. interrupts off. */
854 cmpl $0,TI_preempt_count(%rcx)
855 jnz retint_restore_args
856 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
857 jnc retint_restore_args
858 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
859 jnc retint_restore_args
860 call preempt_schedule_irq
861 jmp retint_kernel /* check again */
871 .macro apicinterrupt num sym do_sym
882 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
883 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
884 apicinterrupt REBOOT_VECTOR \
885 reboot_interrupt smp_reboot_interrupt
889 apicinterrupt UV_BAU_MESSAGE \
890 uv_bau_message_intr1 uv_bau_message_interrupt
892 apicinterrupt LOCAL_TIMER_VECTOR \
893 apic_timer_interrupt smp_apic_timer_interrupt
894 apicinterrupt X86_PLATFORM_IPI_VECTOR \
895 x86_platform_ipi smp_x86_platform_ipi
900 .irp idx,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, \
901 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
902 .if NUM_INVALIDATE_TLB_VECTORS > \idx
903 ENTRY(invalidate_interrupt\idx)
904 pushq_cfi $~(INVALIDATE_TLB_VECTOR_START+\idx)
905 jmp .Lcommon_invalidate_interrupt0
906 CFI_ADJUST_CFA_OFFSET -8
907 END(invalidate_interrupt\idx)
911 apicinterrupt INVALIDATE_TLB_VECTOR_START, \
912 invalidate_interrupt0, smp_invalidate_interrupt
915 apicinterrupt THRESHOLD_APIC_VECTOR \
916 threshold_interrupt smp_threshold_interrupt
917 apicinterrupt THERMAL_APIC_VECTOR \
918 thermal_interrupt smp_thermal_interrupt
921 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
922 call_function_single_interrupt smp_call_function_single_interrupt
923 apicinterrupt CALL_FUNCTION_VECTOR \
924 call_function_interrupt smp_call_function_interrupt
925 apicinterrupt RESCHEDULE_VECTOR \
926 reschedule_interrupt smp_reschedule_interrupt
929 apicinterrupt ERROR_APIC_VECTOR \
930 error_interrupt smp_error_interrupt
931 apicinterrupt SPURIOUS_APIC_VECTOR \
932 spurious_interrupt smp_spurious_interrupt
934 #ifdef CONFIG_IRQ_WORK
935 apicinterrupt IRQ_WORK_VECTOR \
936 irq_work_interrupt smp_irq_work_interrupt
938 #endif /* !CONFIG_XEN */
941 * Exception entry points.
943 .macro zeroentry sym do_sym
950 movq $-1,8(%rsp) /* ORIG_RAX: no syscall to restart */
951 subq $ORIG_RAX-R15-1*8,%rsp
952 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15-1*8
955 movq %rsp,%rdi /* pt_regs pointer */
956 xorl %esi,%esi /* no error code */
958 jmp error_exit /* %ebx: no swapgs flag */
963 .macro paranoidzeroentry sym do_sym
964 zeroentry \sym \do_sym
967 .macro paranoidzeroentry_ist sym do_sym ist
968 zeroentry \sym \do_sym
971 .macro errorentry sym do_sym
978 subq $ORIG_RAX-R15-2*8,%rsp
979 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15-2*8
982 movq %rsp,%rdi /* pt_regs pointer */
983 movq ORIG_RAX(%rsp),%rsi /* get error code */
984 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
986 jmp error_exit /* %ebx: no swapgs flag */
991 /* error code is on the stack already */
992 .macro paranoiderrorentry sym do_sym
993 errorentry \sym \do_sym
997 * Copied from arch/xen/i386/kernel/entry.S
999 # A note on the "critical region" in our callback handler.
1000 # We want to avoid stacking callback handlers due to events occurring
1001 # during handling of the last event. To do this, we keep events disabled
1002 # until we've done all processing. HOWEVER, we must enable events before
1003 # popping the stack frame (can't be done atomically) and so it would still
1004 # be possible to get enough handler activations to overflow the stack.
1005 # Although unlikely, bugs of that kind are hard to track down, so we'd
1006 # like to avoid the possibility.
1007 # So, on entry to the handler we detect whether we interrupted an
1008 # existing activation in its critical region -- if so, we pop the current
1009 # activation and restart the handler using the previous one.
1010 ENTRY(do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1012 # Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1013 # see the correct pointer to the pt_regs
1014 movq %rdi, %rsp # we don't return, adjust the stack frame
1017 11: incl PER_CPU_VAR(irq_count)
1019 CFI_DEF_CFA_REGISTER rbp
1020 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1021 pushq %rbp # backlink for old unwinder
1022 call evtchn_do_upcall
1024 CFI_DEF_CFA_REGISTER rsp
1025 decl PER_CPU_VAR(irq_count)
1028 END(do_hypervisor_callback)
1031 restore_all_enable_events:
1036 scrit: /**** START OF CRITICAL REGION ****/
1039 jnz 14f # process more events if necessary...
1044 14: __DISABLE_INTERRUPTS
1046 movq %rsp,%rdi # set the argument again
1049 ecrit: /**** END OF CRITICAL REGION ****/
1050 # At this point, unlike on x86-32, we don't do the fixup to simplify the
1051 # code and the stack frame is more complex on x86-64.
1052 # When the kernel is interrupted in the critical section, the kernel
1053 # will do IRET in that case, and everything will be restored at that point,
1054 # i.e. it just resumes from the next instruction interrupted with the same context.
1056 # Hypervisor uses this for application faults while it executes.
1057 # We get here for two reasons:
1058 # 1. Fault while reloading DS, ES, FS or GS
1059 # 2. Fault while executing IRET
1060 # Category 1 we do not need to fix up as Xen has already reloaded all segment
1061 # registers that could be reloaded and zeroed the others.
1062 # Category 2 we fix up by killing the current process. We cannot use the
1063 # normal Linux return path in this case because if we use the IRET hypercall
1064 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1065 # We distinguish between categories by comparing each saved segment register
1066 # with its current contents: any discrepancy means we in category 1.
1067 ENTRY(failsafe_callback)
1068 INTR_FRAME offset=4*8
1082 /* All segments match their saved values => Category 2 (Bad IRET). */
1088 CFI_ADJUST_CFA_OFFSET -0x30
1089 movq $11,%rdi /* SIGSEGV */
1092 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1098 CFI_ADJUST_CFA_OFFSET -0x30
1104 zeroentry divide_error do_divide_error
1105 zeroentry overflow do_overflow
1106 zeroentry bounds do_bounds
1107 zeroentry invalid_op do_invalid_op
1108 zeroentry device_not_available do_device_not_available
1109 zeroentry hypervisor_callback do_hypervisor_callback
1110 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1111 errorentry invalid_TSS do_invalid_TSS
1112 errorentry segment_not_present do_segment_not_present
1113 zeroentry coprocessor_error do_coprocessor_error
1114 errorentry alignment_check do_alignment_check
1115 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1117 ENTRY(kernel_thread_helper)
1118 pushq $0 # fake return address
1121 * Here we are in the child and the registers are set as they were
1122 * at kernel_thread() invocation in the parent.
1128 ud2 # padding for call trace
1130 END(kernel_thread_helper)
1133 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1135 * C extern interface:
1136 * extern long execve(const char *name, char **argv, char **envp)
1138 * asm input arguments:
1139 * rdi: name, rsi: argv, rdx: envp
1141 * We want to fallback into:
1142 * extern long sys_execve(const char *name, char **argv,char **envp, struct pt_regs *regs)
1144 * do_sys_execve asm fallback arguments:
1145 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1147 ENTRY(kernel_execve)
1153 movq %rax, RAX(%rsp)
1157 jmp int_ret_from_sys_call
1164 /* Call softirq on interrupt stack. Interrupts are off. */
1168 CFI_REL_OFFSET rbp,0
1170 CFI_DEF_CFA_REGISTER rbp
1171 incl PER_CPU_VAR(irq_count)
1172 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1173 push %rbp # backlink for old unwinder
1177 CFI_DEF_CFA_REGISTER rsp
1178 CFI_ADJUST_CFA_OFFSET -8
1179 decl PER_CPU_VAR(irq_count)
1184 #ifdef CONFIG_STACK_UNWIND
1185 ENTRY(arch_unwind_init_running)
1187 movq %r15, R15(%rdi)
1188 movq %r14, R14(%rdi)
1190 movq %r13, R13(%rdi)
1191 movq %r12, R12(%rdi)
1193 movq %rbp, RBP(%rdi)
1194 movq %rbx, RBX(%rdi)
1197 movq %rax, R11(%rdi)
1198 movq %rax, R10(%rdi)
1201 movq %rax, RAX(%rdi)
1202 movq %rax, RCX(%rdi)
1203 movq %rax, RDX(%rdi)
1204 movq %rax, RSI(%rdi)
1205 movq %rax, RDI(%rdi)
1206 movq %rax, ORIG_RAX(%rdi)
1209 movq $__KERNEL_CS, CS(%rdi)
1210 movq %rax, EFLAGS(%rdi)
1212 movq $__KERNEL_DS, SS(%rdi)
1215 END(arch_unwind_init_running)
1219 * Some functions should be protected against kprobes
1221 .pushsection .kprobes.text, "ax"
1223 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1224 zeroentry nmi do_nmi_callback
1225 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1226 paranoiderrorentry stack_segment do_stack_segment
1227 errorentry general_protection do_general_protection
1228 errorentry page_fault do_page_fault
1229 #ifdef CONFIG_KVM_GUEST
1230 errorentry async_page_fault do_async_page_fault
1232 #ifdef CONFIG_X86_MCE
1233 paranoidzeroentry machine_check *machine_check_vector(%rip)
1238 * "Paranoid" exit path from exception stack.
1239 * Paranoid because this is used by NMIs and cannot take
1240 * any kernel state for granted.
1241 * We don't do kernel preemption checks here, because only
1242 * NMI should be common and it does not enable IRQs and
1243 * cannot get reschedule ticks.
1245 * "trace" is 0 for the NMI handler only, because irq-tracing
1246 * is fundamentally NMI-unsafe. (we cannot change the soft and
1247 * hard flags at once, atomically)
1250 /* ebx: no swapgs flag */
1251 ENTRY(paranoid_exit)
1253 DISABLE_INTERRUPTS(CLBR_NONE)
1255 testl %ebx,%ebx /* swapgs needed? */
1256 jnz paranoid_restore
1258 jnz paranoid_userspace
1269 GET_THREAD_INFO(%rcx)
1270 movl TI_flags(%rcx),%ebx
1271 andl $_TIF_WORK_MASK,%ebx
1273 movq %rsp,%rdi /* &pt_regs */
1275 movq %rax,%rsp /* switch stack for scheduling */
1276 testl $_TIF_NEED_RESCHED,%ebx
1277 jnz paranoid_schedule
1278 movl %ebx,%edx /* arg3: thread flags */
1280 ENABLE_INTERRUPTS(CLBR_NONE)
1281 xorl %esi,%esi /* arg2: oldset */
1282 movq %rsp,%rdi /* arg1: &pt_regs */
1283 call do_notify_resume
1284 DISABLE_INTERRUPTS(CLBR_NONE)
1286 jmp paranoid_userspace
1289 ENABLE_INTERRUPTS(CLBR_ANY)
1291 DISABLE_INTERRUPTS(CLBR_ANY)
1293 jmp paranoid_userspace
1299 * Exception entry point. This expects an error code/orig_rax on the stack.
1300 * returns in "no swapgs flag" in %ebx.
1303 XCPT_FRAME start=2 offset=ORIG_RAX-R15+8
1304 /* oldrax contains error code */
1306 movq %rdi, RDI+8(%rsp)
1307 movq %rsi, RSI+8(%rsp)
1308 movq %rdx, RDX+8(%rsp)
1309 movq %rcx, RCX+8(%rsp)
1310 movq %rax, RAX+8(%rsp)
1311 movq %r8, R8+8(%rsp)
1312 movq %r9, R9+8(%rsp)
1313 movq %r10, R10+8(%rsp)
1314 movq %r11, R11+8(%rsp)
1316 movq %rbp, RBP+8(%rsp)
1317 movq %r12, R12+8(%rsp)
1318 movq %r13, R13+8(%rsp)
1319 movq %r14, R14+8(%rsp)
1320 movq %r15, R15+8(%rsp)
1324 je error_kernelspace
1334 * There are two places in the kernel that can potentially fault with
1335 * usergs. Handle them here. The exception handlers after iret run with
1336 * kernel gs again, so don't set the user space flag. B stepping K8s
1337 * sometimes report an truncated RIP for IRET exceptions returning to
1338 * compat mode. Check for these here too.
1341 CFI_REL_OFFSET rcx, RCX+8
1343 leaq irq_return(%rip),%rcx
1344 cmpq %rcx,RIP+8(%rsp)
1346 movl %ecx,%eax /* zero extend */
1347 cmpq %rax,RIP+8(%rsp)
1349 cmpq $gs_change,RIP+8(%rsp)
1354 /* Fix truncated RIP */
1355 movq %rcx,RIP+8(%rsp)
1365 DISABLE_INTERRUPTS(CLBR_NONE)
1367 GET_THREAD_INFO(%rcx)
1368 testb $3,CS-ARGOFFSET(%rsp)
1370 LOCKDEP_SYS_EXIT_IRQ
1371 movl TI_flags(%rcx),%edx
1372 movl $_TIF_WORK_MASK,%edi
1375 jmp retint_restore_args
1381 #include <asm-generic/percpu.h>
1383 .pushsection PER_CPU_BASE_SECTION, "aw", @progbits
1392 orb $1, PER_CPU_VAR(in_NMI)
1395 movb $0x80, PER_CPU_VAR(in_NMI)
1398 cmpxchgb %ah, PER_CPU_VAR(in_NMI)
1400 orl $NMI_MASK,EFLAGS(%rsp)
1403 DISABLE_INTERRUPTS(CLBR_NONE)
1405 GET_THREAD_INFO(%rcx)
1406 jmp retint_restore_args
1408 END(do_nmi_callback)
1411 #ifndef CONFIG_IA32_EMULATION
1412 ENTRY(ignore_sysret)
1419 # any non-zero value not having VGCF_in_syscall set will do:
1420 HYPERVISOR_IRET VGCF_i387_valid
1426 * End of kprobes section