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 * NOTE: This code handles signal-recognition, which happens every time
16 * after an interrupt and after each system call.
18 * Normal syscalls and interrupts don't save a full stack frame, this is
19 * only done for syscall tracing, signals or fork/exec et.al.
21 * A note on terminology:
22 * - top of stack: Architecture defined interrupt frame from SS to RIP
23 * at the top of the kernel process stack.
24 * - partial stack frame: partially saved registers upto R11.
25 * - full stack frame: Like partial stack frame, but all register saved.
28 * - CFI macros are used to generate dwarf2 unwind information for better
29 * backtraces. They don't change any code.
30 * - SAVE_ALL/RESTORE_ALL - Save/restore all registers
31 * - SAVE_ARGS/RESTORE_ARGS - Save/restore registers that C functions modify.
32 * There are unfortunately lots of special cases where some registers
33 * not touched. The macro is a big mess that should be cleaned up.
34 * - SAVE_REST/RESTORE_REST - Handle the registers not saved by SAVE_ARGS.
35 * Gives a full stack frame.
36 * - ENTRY/END Define functions in the symbol table.
37 * - FIXUP_TOP_OF_STACK/RESTORE_TOP_OF_STACK - Fix up the hardware stack
38 * frame that is otherwise undefined after a SYSCALL
39 * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
40 * - errorentry/paranoidentry/zeroentry - Define exception entry points.
43 #include <linux/linkage.h>
44 #include <linux/stringify.h>
45 #include <asm/segment.h>
46 #include <asm/cache.h>
47 #include <asm/errno.h>
48 #include <asm/dwarf2.h>
49 #include <asm/calling.h>
50 #include <asm/asm-offsets.h>
52 #include <asm/unistd.h>
53 #include <asm/thread_info.h>
54 #include <asm/hw_irq.h>
55 #include <asm/page_types.h>
56 #include <asm/irqflags.h>
57 #include <asm/processor-flags.h>
58 #include <asm/ftrace.h>
59 #include <asm/percpu.h>
60 #include <xen/interface/xen.h>
61 #include <xen/interface/features.h>
63 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
64 #include <linux/elf-em.h>
65 #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
66 #define __AUDIT_ARCH_64BIT 0x80000000
67 #define __AUDIT_ARCH_LE 0x40000000
70 #ifdef CONFIG_FUNCTION_TRACER
71 #ifdef CONFIG_DYNAMIC_FTRACE
77 cmpl $0, function_trace_stop
84 subq $MCOUNT_INSN_SIZE, %rdi
91 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
92 GLOBAL(ftrace_graph_call)
100 #else /* ! CONFIG_DYNAMIC_FTRACE */
102 cmpl $0, function_trace_stop
105 cmpq $ftrace_stub, ftrace_trace_function
108 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
109 cmpq $ftrace_stub, ftrace_graph_return
110 jnz ftrace_graph_caller
112 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
113 jnz ftrace_graph_caller
122 movq 0x38(%rsp), %rdi
124 subq $MCOUNT_INSN_SIZE, %rdi
126 call *ftrace_trace_function
132 #endif /* CONFIG_DYNAMIC_FTRACE */
133 #endif /* CONFIG_FUNCTION_TRACER */
135 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
136 ENTRY(ftrace_graph_caller)
137 cmpl $0, function_trace_stop
143 movq 0x38(%rsp), %rsi
145 subq $MCOUNT_INSN_SIZE, %rsi
147 call prepare_ftrace_return
152 END(ftrace_graph_caller)
154 GLOBAL(return_to_handler)
157 /* Save the return values */
162 call ftrace_return_to_handler
172 #ifndef CONFIG_PREEMPT
173 #define retint_kernel retint_restore_args
177 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
178 #ifdef CONFIG_TRACE_IRQFLAGS
179 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
186 NMI_MASK = 0x80000000
189 * C code is not supposed to know about undefined top of stack. Every time
190 * a C function with an pt_regs argument is called from the SYSCALL based
191 * fast path FIXUP_TOP_OF_STACK is needed.
192 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
196 /* %rsp:at FRAMEEND */
197 .macro FIXUP_TOP_OF_STACK tmp offset=0
198 movq $__USER_CS,CS+\offset(%rsp)
199 movq $-1,RCX+\offset(%rsp)
202 .macro RESTORE_TOP_OF_STACK tmp offset=0
205 .macro FAKE_STACK_FRAME child_rip
206 /* push in order ss, rsp, eflags, cs, rip */
208 pushq $__KERNEL_DS /* ss */
209 CFI_ADJUST_CFA_OFFSET 8
210 /*CFI_REL_OFFSET ss,0*/
212 CFI_ADJUST_CFA_OFFSET 8
214 pushq $X86_EFLAGS_IF /* eflags - interrupts on */
215 CFI_ADJUST_CFA_OFFSET 8
216 /*CFI_REL_OFFSET rflags,0*/
217 pushq $__KERNEL_CS /* cs */
218 CFI_ADJUST_CFA_OFFSET 8
219 /*CFI_REL_OFFSET cs,0*/
220 pushq \child_rip /* rip */
221 CFI_ADJUST_CFA_OFFSET 8
223 pushq %rax /* orig rax */
224 CFI_ADJUST_CFA_OFFSET 8
227 .macro UNFAKE_STACK_FRAME
229 CFI_ADJUST_CFA_OFFSET -(6*8)
233 * initial frame state for interrupts (and exceptions without error code)
235 .macro EMPTY_FRAME offset=0
238 CFI_DEF_CFA rsp,\offset
242 * initial frame state for syscall
244 .macro BASIC_FRAME start=1 offset=0
246 EMPTY_FRAME __stringify(SS+8+\offset-RIP)
248 CFI_DEF_CFA_OFFSET SS+8+\offset-RIP
250 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
251 CFI_REL_OFFSET rsp, RSP+\offset-RIP
252 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
253 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
254 CFI_REL_OFFSET rip, RIP+\offset-RIP
258 * initial frame state for interrupts (and exceptions without error code)
260 .macro INTR_FRAME start=1 offset=0
262 BASIC_FRAME 1, \offset+2*8
263 CFI_REL_OFFSET rcx, 0+\offset
264 CFI_REL_OFFSET r11, 8+\offset
266 BASIC_FRAME \start, \offset
271 * initial frame state for exceptions with error code (and interrupts
272 * with vector already pushed)
274 .macro XCPT_FRAME start=1 offset=0
275 INTR_FRAME \start, __stringify(RIP+\offset-ORIG_RAX)
279 * frame that enables calling into C.
281 .macro PARTIAL_FRAME start=1 offset=0
283 XCPT_FRAME 2*\start, __stringify(ORIG_RAX+\offset-ARGOFFSET)
285 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
286 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
287 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
288 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
289 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
290 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
291 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
292 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
293 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
297 * frame that enables passing a complete pt_regs to a C function.
299 .macro DEFAULT_FRAME start=1 offset=0
301 PARTIAL_FRAME \start, __stringify(R11+\offset-R15)
303 CFI_REL_OFFSET rbx, RBX+\offset
304 CFI_REL_OFFSET rbp, RBP+\offset
305 CFI_REL_OFFSET r12, R12+\offset
306 CFI_REL_OFFSET r13, R13+\offset
307 CFI_REL_OFFSET r14, R14+\offset
308 CFI_REL_OFFSET r15, R15+\offset
312 * Must be consistent with the definition in arch-x86/xen-x86_64.h:
313 * struct iret_context {
314 * u64 rax, r11, rcx, flags, rip, cs, rflags, rsp, ss;
316 * with rax, r11, and rcx being taken care of in the hypercall stub.
318 .macro HYPERVISOR_IRET flag
321 testl $NMI_MASK,2*8(%rsp)
324 cmpb $0,(xen_features+XENFEAT_supervisor_mode_kernel)(%rip)
327 /* Direct iret to kernel space. Correct CS and SS. */
332 2: /* Slow iret via hypervisor. */
333 andl $~NMI_MASK, 2*8(%rsp)
335 jmp hypercall_page + (__HYPERVISOR_iret * 32)
339 /* save partial stack frame */
341 XCPT_FRAME offset=__stringify(ORIG_RAX-ARGOFFSET+16)
343 movq %rdi, RDI+16-ARGOFFSET(%rsp)
344 movq %rsi, RSI+16-ARGOFFSET(%rsp)
345 movq %rdx, RDX+16-ARGOFFSET(%rsp)
346 movq %rcx, RCX+16-ARGOFFSET(%rsp)
347 movq_cfi rax, __stringify(RAX+16-ARGOFFSET)
348 movq %r8, R8+16-ARGOFFSET(%rsp)
349 movq %r9, R9+16-ARGOFFSET(%rsp)
350 movq %r10, R10+16-ARGOFFSET(%rsp)
351 movq_cfi r11, __stringify(R11+16-ARGOFFSET)
353 leaq -ARGOFFSET+16(%rsp),%rdi /* arg1 for handler */
354 movq_cfi rbp, 8 /* push %rbp */
355 leaq 8(%rsp), %rbp /* mov %rsp, %ebp */
356 CFI_DEF_CFA_REGISTER rbp
357 CFI_ADJUST_CFA_OFFSET -8
362 * irq_count is used to check if a CPU is already on an interrupt stack
363 * or not. While this is essentially redundant with preempt_count it is
364 * a little cheaper to use a separate counter in the PDA (short of
365 * moving irq_enter into assembly, which would be too much work)
367 1: incl PER_CPU_VAR(irq_count)
369 popq %rax /* move return address... */
370 mov PER_CPU_VAR(irq_stack_ptr),%rsp
371 pushq %rbp /* backlink for unwinder */
372 pushq %rax /* ... to the new stack */
374 * We entered an interrupt context - irqs are off:
384 movq 5*8+16(%rsp), %r11 /* save return address */
385 movq %rbx, RBX+16(%rsp)
386 movq %rbp, RBP+16(%rsp)
387 movq %r12, R12+16(%rsp)
388 movq %r13, R13+16(%rsp)
389 movq %r14, R14+16(%rsp)
390 movq %r15, R15+16(%rsp)
391 movq %r11, 8(%rsp) /* return address */
392 FIXUP_TOP_OF_STACK %r11, 16
398 /* save complete stack frame */
399 .pushsection .kprobes.text, "ax"
401 XCPT_FRAME offset=__stringify(ORIG_RAX-R15+8)
403 movq %rdi, RDI+8(%rsp)
404 movq %rsi, RSI+8(%rsp)
405 movq_cfi rdx, __stringify(RDX+8)
406 movq_cfi rcx, __stringify(RCX+8)
407 movq_cfi rax, __stringify(RAX+8)
410 movq %r10, R10+8(%rsp)
411 movq %r11, R11+8(%rsp)
412 movq_cfi rbx, __stringify(RBX+8)
413 movq %rbp, RBP+8(%rsp)
414 movq %r12, R12+8(%rsp)
415 movq %r13, R13+8(%rsp)
416 movq %r14, R14+8(%rsp)
417 movq %r15, R15+8(%rsp)
419 movl $MSR_GS_BASE,%ecx
422 js 1f /* negative -> in kernel */
432 * A newly forked process directly context switches into this address.
434 * rdi: prev task we switched from
439 LOCK ; btr $TIF_FORK,TI_flags(%r8)
441 push kernel_eflags(%rip)
442 CFI_ADJUST_CFA_OFFSET 8
443 popf # reset kernel eflags
444 CFI_ADJUST_CFA_OFFSET -8
446 call schedule_tail # rdi: 'prev' task parameter
448 GET_THREAD_INFO(%rcx)
452 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
453 je int_ret_from_sys_call
455 testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
456 jnz int_ret_from_sys_call
458 RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
459 jmp ret_from_sys_call # go to the SYSRET fastpath
465 * System call entry. Upto 6 arguments in registers are supported.
467 * SYSCALL does not save anything on the stack and does not change the
473 * rax system call number
475 * rcx return address for syscall/sysret, C arg3
478 * r10 arg3 (--> moved to rcx for C)
481 * r11 eflags for syscall/sysret, temporary for C
482 * r12-r15,rbp,rbx saved by C code, not touched.
484 * Interrupts are enabled on entry.
485 * Only called from user space.
487 * XXX if we had a free scratch register we could save the RSP into the stack frame
488 * and report it properly in ps. Unfortunately we haven't.
490 * When user can change the frames always force IRET. That is because
491 * it deals with uncanonical addresses better. SYSRET has trouble
492 * with them due to bugs in both AMD and Intel CPUs.
496 INTR_FRAME start=2 offset=2*8
498 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
499 GET_THREAD_INFO(%rcx)
500 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
502 system_call_fastpath:
503 cmpq $__NR_syscall_max,%rax
506 call *sys_call_table(,%rax,8) # XXX: rip relative
507 movq %rax,RAX-ARGOFFSET(%rsp)
509 * Syscall return path ending with SYSRET (fast path)
510 * Has incomplete stack frame and undefined top of stack.
513 movl $_TIF_ALLWORK_MASK,%edi
517 GET_THREAD_INFO(%rcx)
518 DISABLE_INTERRUPTS(CLBR_NONE)
520 movl TI_flags(%rcx),%edx
525 * sysretq will re-enable interrupts:
528 ENABLE_INTERRUPTS(CLBR_NONE)
530 HYPERVISOR_IRET VGCF_IN_SYSCALL
533 /* Handle reschedules */
534 /* edx: work, edi: workmask */
536 bt $TIF_NEED_RESCHED,%edx
539 ENABLE_INTERRUPTS(CLBR_NONE)
541 CFI_ADJUST_CFA_OFFSET 8
544 CFI_ADJUST_CFA_OFFSET -8
547 /* Handle a signal */
550 ENABLE_INTERRUPTS(CLBR_NONE)
551 #ifdef CONFIG_AUDITSYSCALL
552 bt $TIF_SYSCALL_AUDIT,%edx
556 * We have a signal, or exit tracing or single-step.
557 * These all wind up with the iret return path anyway,
558 * so just join that path right now.
560 FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
561 jmp int_check_syscall_exit_work
564 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
565 jmp ret_from_sys_call
567 #ifdef CONFIG_AUDITSYSCALL
569 * Fast path for syscall audit without full syscall trace.
570 * We just call audit_syscall_entry() directly, and then
571 * jump back to the normal fast path.
574 movq %r10,%r9 /* 6th arg: 4th syscall arg */
575 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
576 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
577 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
578 movq %rax,%rsi /* 2nd arg: syscall number */
579 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
580 call audit_syscall_entry
581 LOAD_ARGS 0 /* reload call-clobbered registers */
582 jmp system_call_fastpath
585 * Return fast path for syscall audit. Call audit_syscall_exit()
586 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
590 movq %rax,%rsi /* second arg, syscall return value */
591 cmpq $0,%rax /* is it < 0? */
592 setl %al /* 1 if so, 0 if not */
593 movzbl %al,%edi /* zero-extend that into %edi */
594 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
595 call audit_syscall_exit
596 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
598 #endif /* CONFIG_AUDITSYSCALL */
600 /* Do syscall tracing */
602 #ifdef CONFIG_AUDITSYSCALL
603 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
607 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
608 FIXUP_TOP_OF_STACK %rdi
610 call syscall_trace_enter
612 * Reload arg registers from stack in case ptrace changed them.
613 * We don't reload %rax because syscall_trace_enter() returned
614 * the value it wants us to use in the table lookup.
616 LOAD_ARGS ARGOFFSET, 1
618 cmpq $__NR_syscall_max,%rax
619 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
620 movq %r10,%rcx /* fixup for C */
621 call *sys_call_table(,%rax,8)
622 movq %rax,RAX-ARGOFFSET(%rsp)
623 /* Use IRET because user could have changed frame */
626 * Syscall return path ending with IRET.
627 * Has correct top of stack, but partial stack frame.
629 GLOBAL(int_ret_from_sys_call)
630 DISABLE_INTERRUPTS(CLBR_NONE)
632 testb $3,CS-ARGOFFSET(%rsp)
634 /* Need to set the proper %ss (not NULL) for ring 3 iretq */
635 movl $__KERNEL_DS,SS-ARGOFFSET(%rsp)
636 jmp retint_restore_args # retrun from ring3 kernel
638 movl $_TIF_ALLWORK_MASK,%edi
639 /* edi: mask to check */
640 GLOBAL(int_with_check)
642 GET_THREAD_INFO(%rcx)
643 movl TI_flags(%rcx),%edx
646 andl $~TS_COMPAT,TI_status(%rcx)
647 jmp retint_restore_args
649 /* Either reschedule or signal or syscall exit tracking needed. */
650 /* First do a reschedule test. */
651 /* edx: work, edi: workmask */
653 bt $TIF_NEED_RESCHED,%edx
656 ENABLE_INTERRUPTS(CLBR_NONE)
658 CFI_ADJUST_CFA_OFFSET 8
661 CFI_ADJUST_CFA_OFFSET -8
662 DISABLE_INTERRUPTS(CLBR_NONE)
666 /* handle signals and tracing -- both require a full stack frame */
669 ENABLE_INTERRUPTS(CLBR_NONE)
670 int_check_syscall_exit_work:
672 /* Check for syscall exit trace */
673 testl $_TIF_WORK_SYSCALL_EXIT,%edx
676 CFI_ADJUST_CFA_OFFSET 8
677 leaq 8(%rsp),%rdi # &ptregs -> arg1
678 call syscall_trace_leave
680 CFI_ADJUST_CFA_OFFSET -8
681 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
685 testl $_TIF_DO_NOTIFY_MASK,%edx
687 movq %rsp,%rdi # &ptregs -> arg1
688 xorl %esi,%esi # oldset -> arg2
689 call do_notify_resume
690 1: movl $_TIF_WORK_MASK,%edi
693 DISABLE_INTERRUPTS(CLBR_NONE)
700 * Certain special system calls that need to save a complete full stack frame.
702 .macro PTREGSCALL label,func,arg
704 PARTIAL_FRAME 1 8 /* offset 8: return address */
705 subq $REST_SKIP, %rsp
706 CFI_ADJUST_CFA_OFFSET REST_SKIP
708 DEFAULT_FRAME -2 8 /* offset 8: return address */
709 leaq 8(%rsp), \arg /* pt_regs pointer */
711 jmp ptregscall_common
716 PTREGSCALL stub_clone, sys_clone, %r8
717 PTREGSCALL stub_fork, sys_fork, %rdi
718 PTREGSCALL stub_vfork, sys_vfork, %rdi
719 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
720 PTREGSCALL stub_iopl, sys_iopl, %rsi
722 ENTRY(ptregscall_common)
723 DEFAULT_FRAME 1 8 /* offset 8: return address */
724 RESTORE_TOP_OF_STACK %r11, 8
725 movq_cfi_restore __stringify(R15+8), r15
726 movq_cfi_restore __stringify(R14+8), r14
727 movq_cfi_restore __stringify(R13+8), r13
728 movq_cfi_restore __stringify(R12+8), r12
729 movq_cfi_restore __stringify(RBP+8), rbp
730 movq_cfi_restore __stringify(RBX+8), rbx
731 ret $REST_SKIP /* pop extended registers */
733 END(ptregscall_common)
740 FIXUP_TOP_OF_STACK %r11
743 RESTORE_TOP_OF_STACK %r11
746 jmp int_ret_from_sys_call
751 * sigreturn is special because it needs to restore all registers on return.
752 * This cannot be done with SYSRET, so use the IRET return path instead.
754 ENTRY(stub_rt_sigreturn)
760 FIXUP_TOP_OF_STACK %r11
761 call sys_rt_sigreturn
762 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
764 jmp int_ret_from_sys_call
766 END(stub_rt_sigreturn)
772 retint_with_reschedule:
774 movl $_TIF_WORK_MASK,%edi
777 movl TI_flags(%rcx),%edx
781 retint_restore_args: /* return to kernel space */
782 movl EFLAGS-REST_SKIP(%rsp), %eax
783 shr $9, %eax # EAX[0] == IRET_EFLAGS.IF
785 andb evtchn_upcall_mask(%rsi),%al
786 andb $1,%al # EAX[0] == IRET_EFLAGS.IF & event_mask
787 jnz restore_all_enable_events # != 0 => enable event delivery
792 /* edi: workmask, edx: work */
795 bt $TIF_NEED_RESCHED,%edx
798 ENABLE_INTERRUPTS(CLBR_NONE)
800 CFI_ADJUST_CFA_OFFSET 8
803 CFI_ADJUST_CFA_OFFSET -8
804 GET_THREAD_INFO(%rcx)
805 DISABLE_INTERRUPTS(CLBR_NONE)
810 testl $_TIF_DO_NOTIFY_MASK,%edx
811 jz retint_restore_args
813 ENABLE_INTERRUPTS(CLBR_NONE)
815 movq $-1,ORIG_RAX(%rsp)
816 xorl %esi,%esi # oldset
817 movq %rsp,%rdi # &pt_regs
818 call do_notify_resume
820 DISABLE_INTERRUPTS(CLBR_NONE)
822 GET_THREAD_INFO(%rcx)
823 jmp retint_with_reschedule
825 #ifdef CONFIG_PREEMPT
826 /* Returning to kernel space. Check if we need preemption */
827 /* rcx: threadinfo. interrupts off. */
829 cmpl $0,TI_preempt_count(%rcx)
830 jnz retint_restore_args
831 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
832 jnc retint_restore_args
833 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
834 jnc retint_restore_args
835 call preempt_schedule_irq
836 jmp retint_kernel /* check again */
846 .macro apicinterrupt num sym do_sym
850 CFI_ADJUST_CFA_OFFSET 8
858 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
859 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
860 apicinterrupt REBOOT_VECTOR \
861 reboot_interrupt smp_reboot_interrupt
865 apicinterrupt UV_BAU_MESSAGE \
866 uv_bau_message_intr1 uv_bau_message_interrupt
868 apicinterrupt LOCAL_TIMER_VECTOR \
869 apic_timer_interrupt smp_apic_timer_interrupt
870 apicinterrupt X86_PLATFORM_IPI_VECTOR \
871 x86_platform_ipi smp_x86_platform_ipi
874 apicinterrupt INVALIDATE_TLB_VECTOR_START+0 \
875 invalidate_interrupt0 smp_invalidate_interrupt
876 apicinterrupt INVALIDATE_TLB_VECTOR_START+1 \
877 invalidate_interrupt1 smp_invalidate_interrupt
878 apicinterrupt INVALIDATE_TLB_VECTOR_START+2 \
879 invalidate_interrupt2 smp_invalidate_interrupt
880 apicinterrupt INVALIDATE_TLB_VECTOR_START+3 \
881 invalidate_interrupt3 smp_invalidate_interrupt
882 apicinterrupt INVALIDATE_TLB_VECTOR_START+4 \
883 invalidate_interrupt4 smp_invalidate_interrupt
884 apicinterrupt INVALIDATE_TLB_VECTOR_START+5 \
885 invalidate_interrupt5 smp_invalidate_interrupt
886 apicinterrupt INVALIDATE_TLB_VECTOR_START+6 \
887 invalidate_interrupt6 smp_invalidate_interrupt
888 apicinterrupt INVALIDATE_TLB_VECTOR_START+7 \
889 invalidate_interrupt7 smp_invalidate_interrupt
892 apicinterrupt THRESHOLD_APIC_VECTOR \
893 threshold_interrupt smp_threshold_interrupt
894 apicinterrupt THERMAL_APIC_VECTOR \
895 thermal_interrupt smp_thermal_interrupt
897 #ifdef CONFIG_X86_MCE
898 apicinterrupt MCE_SELF_VECTOR \
899 mce_self_interrupt smp_mce_self_interrupt
903 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
904 call_function_single_interrupt smp_call_function_single_interrupt
905 apicinterrupt CALL_FUNCTION_VECTOR \
906 call_function_interrupt smp_call_function_interrupt
907 apicinterrupt RESCHEDULE_VECTOR \
908 reschedule_interrupt smp_reschedule_interrupt
911 apicinterrupt ERROR_APIC_VECTOR \
912 error_interrupt smp_error_interrupt
913 apicinterrupt SPURIOUS_APIC_VECTOR \
914 spurious_interrupt smp_spurious_interrupt
916 #ifdef CONFIG_PERF_EVENTS
917 apicinterrupt LOCAL_PENDING_VECTOR \
918 perf_pending_interrupt smp_perf_pending_interrupt
920 #endif /* !CONFIG_XEN */
923 * Exception entry points.
925 .macro zeroentry sym do_sym
932 movq $-1,8(%rsp) /* ORIG_RAX: no syscall to restart */
933 subq $ORIG_RAX-R15-8, %rsp
934 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15-8
937 movq %rsp,%rdi /* pt_regs pointer */
938 xorl %esi,%esi /* no error code */
940 jmp error_exit /* %ebx: no swapgs flag */
945 .macro paranoidzeroentry sym do_sym
946 zeroentry \sym \do_sym
949 .macro paranoidzeroentry_ist sym do_sym ist
950 zeroentry \sym \do_sym
953 .macro errorentry sym do_sym
960 subq $ORIG_RAX-R15-2*8, %rsp
961 CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15-2*8
964 movq %rsp,%rdi /* pt_regs pointer */
965 movq ORIG_RAX(%rsp),%rsi /* get error code */
966 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
968 jmp error_exit /* %ebx: no swapgs flag */
973 /* error code is on the stack already */
974 .macro paranoiderrorentry sym do_sym
975 errorentry \sym \do_sym
979 * Copied from arch/xen/i386/kernel/entry.S
981 # A note on the "critical region" in our callback handler.
982 # We want to avoid stacking callback handlers due to events occurring
983 # during handling of the last event. To do this, we keep events disabled
984 # until we've done all processing. HOWEVER, we must enable events before
985 # popping the stack frame (can't be done atomically) and so it would still
986 # be possible to get enough handler activations to overflow the stack.
987 # Although unlikely, bugs of that kind are hard to track down, so we'd
988 # like to avoid the possibility.
989 # So, on entry to the handler we detect whether we interrupted an
990 # existing activation in its critical region -- if so, we pop the current
991 # activation and restart the handler using the previous one.
992 ENTRY(do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
994 # Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
995 # see the correct pointer to the pt_regs
996 movq %rdi, %rsp # we don't return, adjust the stack frame
999 11: incl PER_CPU_VAR(irq_count)
1001 CFI_DEF_CFA_REGISTER rbp
1002 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1003 pushq %rbp # backlink for old unwinder
1004 call evtchn_do_upcall
1006 CFI_DEF_CFA_REGISTER rsp
1007 decl PER_CPU_VAR(irq_count)
1010 END(do_hypervisor_callback)
1013 restore_all_enable_events:
1018 scrit: /**** START OF CRITICAL REGION ****/
1021 jnz 14f # process more events if necessary...
1026 14: __DISABLE_INTERRUPTS
1028 movq %rsp,%rdi # set the argument again
1031 ecrit: /**** END OF CRITICAL REGION ****/
1032 # At this point, unlike on x86-32, we don't do the fixup to simplify the
1033 # code and the stack frame is more complex on x86-64.
1034 # When the kernel is interrupted in the critical section, the kernel
1035 # will do IRET in that case, and everything will be restored at that point,
1036 # i.e. it just resumes from the next instruction interrupted with the same context.
1038 # Hypervisor uses this for application faults while it executes.
1039 # We get here for two reasons:
1040 # 1. Fault while reloading DS, ES, FS or GS
1041 # 2. Fault while executing IRET
1042 # Category 1 we do not need to fix up as Xen has already reloaded all segment
1043 # registers that could be reloaded and zeroed the others.
1044 # Category 2 we fix up by killing the current process. We cannot use the
1045 # normal Linux return path in this case because if we use the IRET hypercall
1046 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1047 # We distinguish between categories by comparing each saved segment register
1048 # with its current contents: any discrepancy means we in category 1.
1049 ENTRY(failsafe_callback)
1050 INTR_FRAME offset=4*8
1064 /* All segments match their saved values => Category 2 (Bad IRET). */
1070 CFI_ADJUST_CFA_OFFSET -0x30
1071 movq $11,%rdi /* SIGSEGV */
1074 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1080 CFI_ADJUST_CFA_OFFSET -0x30
1082 CFI_ADJUST_CFA_OFFSET 8
1087 zeroentry divide_error do_divide_error
1088 zeroentry overflow do_overflow
1089 zeroentry bounds do_bounds
1090 zeroentry invalid_op do_invalid_op
1091 zeroentry device_not_available do_device_not_available
1092 zeroentry hypervisor_callback do_hypervisor_callback
1093 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1094 errorentry invalid_TSS do_invalid_TSS
1095 errorentry segment_not_present do_segment_not_present
1096 zeroentry coprocessor_error do_coprocessor_error
1097 errorentry alignment_check do_alignment_check
1098 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1100 ENTRY(kernel_thread_helper)
1101 pushq $0 # fake return address
1104 * Here we are in the child and the registers are set as they were
1105 * at kernel_thread() invocation in the parent.
1111 ud2 # padding for call trace
1113 END(kernel_thread_helper)
1116 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1118 * C extern interface:
1119 * extern long execve(char *name, char **argv, char **envp)
1121 * asm input arguments:
1122 * rdi: name, rsi: argv, rdx: envp
1124 * We want to fallback into:
1125 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
1127 * do_sys_execve asm fallback arguments:
1128 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1130 ENTRY(kernel_execve)
1136 movq %rax, RAX(%rsp)
1140 jmp int_ret_from_sys_call
1147 /* Call softirq on interrupt stack. Interrupts are off. */
1151 CFI_ADJUST_CFA_OFFSET 8
1152 CFI_REL_OFFSET rbp,0
1154 CFI_DEF_CFA_REGISTER rbp
1155 incl PER_CPU_VAR(irq_count)
1156 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1157 push %rbp # backlink for old unwinder
1160 CFI_DEF_CFA_REGISTER rsp
1161 CFI_ADJUST_CFA_OFFSET -8
1162 decl PER_CPU_VAR(irq_count)
1167 #ifdef CONFIG_STACK_UNWIND
1168 ENTRY(arch_unwind_init_running)
1170 movq %r15, R15(%rdi)
1171 movq %r14, R14(%rdi)
1173 movq %r13, R13(%rdi)
1174 movq %r12, R12(%rdi)
1176 movq %rbp, RBP(%rdi)
1177 movq %rbx, RBX(%rdi)
1180 movq %rax, R11(%rdi)
1181 movq %rax, R10(%rdi)
1184 movq %rax, RAX(%rdi)
1185 movq %rax, RCX(%rdi)
1186 movq %rax, RDX(%rdi)
1187 movq %rax, RSI(%rdi)
1188 movq %rax, RDI(%rdi)
1189 movq %rax, ORIG_RAX(%rdi)
1192 movq $__KERNEL_CS, CS(%rdi)
1193 movq %rax, EFLAGS(%rdi)
1195 movq $__KERNEL_DS, SS(%rdi)
1198 END(arch_unwind_init_running)
1202 * Some functions should be protected against kprobes
1204 .pushsection .kprobes.text, "ax"
1206 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1207 zeroentry nmi do_nmi_callback
1208 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1209 paranoiderrorentry stack_segment do_stack_segment
1210 errorentry general_protection do_general_protection
1211 errorentry page_fault do_page_fault
1212 #ifdef CONFIG_X86_MCE
1213 paranoidzeroentry machine_check *machine_check_vector(%rip)
1218 * "Paranoid" exit path from exception stack.
1219 * Paranoid because this is used by NMIs and cannot take
1220 * any kernel state for granted.
1221 * We don't do kernel preemption checks here, because only
1222 * NMI should be common and it does not enable IRQs and
1223 * cannot get reschedule ticks.
1225 * "trace" is 0 for the NMI handler only, because irq-tracing
1226 * is fundamentally NMI-unsafe. (we cannot change the soft and
1227 * hard flags at once, atomically)
1230 /* ebx: no swapgs flag */
1231 ENTRY(paranoid_exit)
1233 DISABLE_INTERRUPTS(CLBR_NONE)
1235 testl %ebx,%ebx /* swapgs needed? */
1236 jnz paranoid_restore
1238 jnz paranoid_userspace
1249 GET_THREAD_INFO(%rcx)
1250 movl TI_flags(%rcx),%ebx
1251 andl $_TIF_WORK_MASK,%ebx
1253 movq %rsp,%rdi /* &pt_regs */
1255 movq %rax,%rsp /* switch stack for scheduling */
1256 testl $_TIF_NEED_RESCHED,%ebx
1257 jnz paranoid_schedule
1258 movl %ebx,%edx /* arg3: thread flags */
1260 ENABLE_INTERRUPTS(CLBR_NONE)
1261 xorl %esi,%esi /* arg2: oldset */
1262 movq %rsp,%rdi /* arg1: &pt_regs */
1263 call do_notify_resume
1264 DISABLE_INTERRUPTS(CLBR_NONE)
1266 jmp paranoid_userspace
1269 ENABLE_INTERRUPTS(CLBR_ANY)
1271 DISABLE_INTERRUPTS(CLBR_ANY)
1273 jmp paranoid_userspace
1279 * Exception entry point. This expects an error code/orig_rax on the stack.
1280 * returns in "no swapgs flag" in %ebx.
1283 XCPT_FRAME start=2 offset=__stringify(ORIG_RAX-R15+8)
1284 /* oldrax contains error code */
1286 movq %rdi, RDI+8(%rsp)
1287 movq %rsi, RSI+8(%rsp)
1288 movq %rdx, RDX+8(%rsp)
1289 movq %rcx, RCX+8(%rsp)
1290 movq %rax, RAX+8(%rsp)
1291 movq %r8, R8+8(%rsp)
1292 movq %r9, R9+8(%rsp)
1293 movq %r10, R10+8(%rsp)
1294 movq %r11, R11+8(%rsp)
1295 movq_cfi rbx, __stringify(RBX+8)
1296 movq %rbp, RBP+8(%rsp)
1297 movq %r12, R12+8(%rsp)
1298 movq %r13, R13+8(%rsp)
1299 movq %r14, R14+8(%rsp)
1300 movq %r15, R15+8(%rsp)
1304 je error_kernelspace
1314 * There are two places in the kernel that can potentially fault with
1315 * usergs. Handle them here. The exception handlers after iret run with
1316 * kernel gs again, so don't set the user space flag. B stepping K8s
1317 * sometimes report an truncated RIP for IRET exceptions returning to
1318 * compat mode. Check for these here too.
1321 CFI_REL_OFFSET rcx, RCX+8
1323 leaq irq_return(%rip),%rcx
1324 cmpq %rcx,RIP+8(%rsp)
1326 movl %ecx,%eax /* zero extend */
1327 cmpq %rax,RIP+8(%rsp)
1329 cmpq $gs_change,RIP+8(%rsp)
1334 /* Fix truncated RIP */
1335 movq %rcx,RIP+8(%rsp)
1345 DISABLE_INTERRUPTS(CLBR_NONE)
1347 GET_THREAD_INFO(%rcx)
1348 testb $3,CS-ARGOFFSET(%rsp)
1350 LOCKDEP_SYS_EXIT_IRQ
1351 movl TI_flags(%rcx),%edx
1352 movl $_TIF_WORK_MASK,%edi
1355 jmp retint_restore_args
1366 orl $NMI_MASK,EFLAGS(%rsp)
1368 DISABLE_INTERRUPTS(CLBR_NONE)
1370 GET_THREAD_INFO(%rcx)
1371 jmp retint_restore_args
1373 END(do_nmi_callback)
1376 #ifndef CONFIG_IA32_EMULATION
1377 ENTRY(ignore_sysret)
1380 CFI_ADJUST_CFA_OFFSET -8
1383 CFI_ADJUST_CFA_OFFSET -8
1392 * End of kprobes section