2 * linux/arch/x86_64/entry.S
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
6 * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
10 * entry.S contains the system-call and fault low-level handling routines.
12 * NOTE: This code handles signal-recognition, which happens every time
13 * after an interrupt and after each system call.
15 * Normal syscalls and interrupts don't save a full stack frame, this is
16 * only done for syscall tracing, signals or fork/exec et.al.
18 * A note on terminology:
19 * - top of stack: Architecture defined interrupt frame from SS to RIP
20 * at the top of the kernel process stack.
21 * - partial stack frame: partially saved registers upto R11.
22 * - full stack frame: Like partial stack frame, but all register saved.
25 * - CFI macros are used to generate dwarf2 unwind information for better
26 * backtraces. They don't change any code.
27 * - SAVE_ALL/RESTORE_ALL - Save/restore all registers
28 * - SAVE_ARGS/RESTORE_ARGS - Save/restore registers that C functions modify.
29 * There are unfortunately lots of special cases where some registers
30 * not touched. The macro is a big mess that should be cleaned up.
31 * - SAVE_REST/RESTORE_REST - Handle the registers not saved by SAVE_ARGS.
32 * Gives a full stack frame.
33 * - ENTRY/END Define functions in the symbol table.
34 * - FIXUP_TOP_OF_STACK/RESTORE_TOP_OF_STACK - Fix up the hardware stack
35 * frame that is otherwise undefined after a SYSCALL
36 * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
37 * - errorentry/paranoidentry/zeroentry - Define exception entry points.
40 #include <linux/linkage.h>
41 #include <asm/segment.h>
42 #include <asm/cache.h>
43 #include <asm/errno.h>
44 #include <asm/dwarf2.h>
45 #include <asm/calling.h>
46 #include <asm/asm-offsets.h>
48 #include <asm/unistd.h>
49 #include <asm/thread_info.h>
50 #include <asm/hw_irq.h>
52 #include <asm/irqflags.h>
53 #include <asm/paravirt.h>
54 #include <asm/ftrace.h>
56 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
57 #include <linux/elf-em.h>
58 #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
59 #define __AUDIT_ARCH_64BIT 0x80000000
60 #define __AUDIT_ARCH_LE 0x40000000
63 #ifdef CONFIG_FUNCTION_TRACER
64 #ifdef CONFIG_DYNAMIC_FTRACE
70 cmpl $0, function_trace_stop
77 subq $MCOUNT_INSN_SIZE, %rdi
85 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
86 .globl ftrace_graph_call
96 #else /* ! CONFIG_DYNAMIC_FTRACE */
98 cmpl $0, function_trace_stop
101 cmpq $ftrace_stub, ftrace_trace_function
104 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
105 cmpq $ftrace_stub, ftrace_graph_return
106 jnz ftrace_graph_caller
108 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
109 jnz ftrace_graph_caller
119 movq 0x38(%rsp), %rdi
121 subq $MCOUNT_INSN_SIZE, %rdi
123 call *ftrace_trace_function
129 #endif /* CONFIG_DYNAMIC_FTRACE */
130 #endif /* CONFIG_FUNCTION_TRACER */
132 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
133 ENTRY(ftrace_graph_caller)
134 cmpl $0, function_trace_stop
140 movq 0x38(%rsp), %rsi
141 subq $MCOUNT_INSN_SIZE, %rsi
143 call prepare_ftrace_return
148 END(ftrace_graph_caller)
151 .globl return_to_handler
165 call ftrace_return_to_handler
182 #ifndef CONFIG_PREEMPT
183 #define retint_kernel retint_restore_args
186 #ifdef CONFIG_PARAVIRT
187 ENTRY(native_usergs_sysret64)
190 #endif /* CONFIG_PARAVIRT */
193 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
194 #ifdef CONFIG_TRACE_IRQFLAGS
195 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
203 * C code is not supposed to know about undefined top of stack. Every time
204 * a C function with an pt_regs argument is called from the SYSCALL based
205 * fast path FIXUP_TOP_OF_STACK is needed.
206 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
210 /* %rsp:at FRAMEEND */
211 .macro FIXUP_TOP_OF_STACK tmp offset=0
212 movq %gs:pda_oldrsp,\tmp
213 movq \tmp,RSP+\offset(%rsp)
214 movq $__USER_DS,SS+\offset(%rsp)
215 movq $__USER_CS,CS+\offset(%rsp)
216 movq $-1,RCX+\offset(%rsp)
217 movq R11+\offset(%rsp),\tmp /* get eflags */
218 movq \tmp,EFLAGS+\offset(%rsp)
221 .macro RESTORE_TOP_OF_STACK tmp offset=0
222 movq RSP+\offset(%rsp),\tmp
223 movq \tmp,%gs:pda_oldrsp
224 movq EFLAGS+\offset(%rsp),\tmp
225 movq \tmp,R11+\offset(%rsp)
228 .macro FAKE_STACK_FRAME child_rip
229 /* push in order ss, rsp, eflags, cs, rip */
231 pushq $__KERNEL_DS /* ss */
232 CFI_ADJUST_CFA_OFFSET 8
233 /*CFI_REL_OFFSET ss,0*/
235 CFI_ADJUST_CFA_OFFSET 8
237 pushq $X86_EFLAGS_IF /* eflags - interrupts on */
238 CFI_ADJUST_CFA_OFFSET 8
239 /*CFI_REL_OFFSET rflags,0*/
240 pushq $__KERNEL_CS /* cs */
241 CFI_ADJUST_CFA_OFFSET 8
242 /*CFI_REL_OFFSET cs,0*/
243 pushq \child_rip /* rip */
244 CFI_ADJUST_CFA_OFFSET 8
246 pushq %rax /* orig rax */
247 CFI_ADJUST_CFA_OFFSET 8
250 .macro UNFAKE_STACK_FRAME
252 CFI_ADJUST_CFA_OFFSET -(6*8)
256 * initial frame state for interrupts (and exceptions without error code)
258 .macro EMPTY_FRAME start=1 offset=0
262 CFI_DEF_CFA rsp,8+\offset
264 CFI_DEF_CFA_OFFSET 8+\offset
269 * initial frame state for interrupts (and exceptions without error code)
271 .macro INTR_FRAME start=1 offset=0
272 EMPTY_FRAME \start, SS+8+\offset-RIP
273 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
274 CFI_REL_OFFSET rsp, RSP+\offset-RIP
275 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
276 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
277 CFI_REL_OFFSET rip, RIP+\offset-RIP
281 * initial frame state for exceptions with error code (and interrupts
282 * with vector already pushed)
284 .macro XCPT_FRAME start=1 offset=0
285 INTR_FRAME \start, RIP+\offset-ORIG_RAX
286 /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
290 * frame that enables calling into C.
292 .macro PARTIAL_FRAME start=1 offset=0
293 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
294 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
295 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
296 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
297 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
298 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
299 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
300 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
301 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
302 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
306 * frame that enables passing a complete pt_regs to a C function.
308 .macro DEFAULT_FRAME start=1 offset=0
309 PARTIAL_FRAME \start, R11+\offset-R15
310 CFI_REL_OFFSET rbx, RBX+\offset
311 CFI_REL_OFFSET rbp, RBP+\offset
312 CFI_REL_OFFSET r12, R12+\offset
313 CFI_REL_OFFSET r13, R13+\offset
314 CFI_REL_OFFSET r14, R14+\offset
315 CFI_REL_OFFSET r15, R15+\offset
318 /* save partial stack frame */
322 movq_cfi rdi, RDI+16-ARGOFFSET
323 movq_cfi rsi, RSI+16-ARGOFFSET
324 movq_cfi rdx, RDX+16-ARGOFFSET
325 movq_cfi rcx, RCX+16-ARGOFFSET
326 movq_cfi rax, RAX+16-ARGOFFSET
327 movq_cfi r8, R8+16-ARGOFFSET
328 movq_cfi r9, R9+16-ARGOFFSET
329 movq_cfi r10, R10+16-ARGOFFSET
330 movq_cfi r11, R11+16-ARGOFFSET
332 leaq -ARGOFFSET+16(%rsp),%rdi /* arg1 for handler */
333 movq_cfi rbp, 8 /* push %rbp */
334 leaq 8(%rsp), %rbp /* mov %rsp, %ebp */
339 * irqcount is used to check if a CPU is already on an interrupt stack
340 * or not. While this is essentially redundant with preempt_count it is
341 * a little cheaper to use a separate counter in the PDA (short of
342 * moving irq_enter into assembly, which would be too much work)
344 1: incl %gs:pda_irqcount
346 popq_cfi %rax /* move return address... */
347 mov %gs:pda_irqstackptr,%rsp
349 pushq_cfi %rax /* ... to the new stack */
351 * We entered an interrupt context - irqs are off:
359 PARTIAL_FRAME 1 REST_SKIP+8
360 movq 5*8+16(%rsp), %r11 /* save return address */
367 movq %r11, 8(%rsp) /* return address */
368 FIXUP_TOP_OF_STACK %r11, 16
373 /* save complete stack frame */
393 movl $MSR_GS_BASE,%ecx
396 js 1f /* negative -> in kernel */
404 * A newly forked process directly context switches into this address.
406 * rdi: prev task we switched from
411 push kernel_eflags(%rip)
412 CFI_ADJUST_CFA_OFFSET 8
413 popf # reset kernel eflags
414 CFI_ADJUST_CFA_OFFSET -8
416 call schedule_tail # rdi: 'prev' task parameter
418 GET_THREAD_INFO(%rcx)
423 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
424 je int_ret_from_sys_call
426 testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
427 jnz int_ret_from_sys_call
429 RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
430 jmp ret_from_sys_call # go to the SYSRET fastpath
437 * System call entry. Upto 6 arguments in registers are supported.
439 * SYSCALL does not save anything on the stack and does not change the
445 * rax system call number
447 * rcx return address for syscall/sysret, C arg3
450 * r10 arg3 (--> moved to rcx for C)
453 * r11 eflags for syscall/sysret, temporary for C
454 * r12-r15,rbp,rbx saved by C code, not touched.
456 * Interrupts are off on entry.
457 * Only called from user space.
459 * XXX if we had a free scratch register we could save the RSP into the stack frame
460 * and report it properly in ps. Unfortunately we haven't.
462 * When user can change the frames always force IRET. That is because
463 * it deals with uncanonical addresses better. SYSRET has trouble
464 * with them due to bugs in both AMD and Intel CPUs.
470 CFI_DEF_CFA rsp,PDA_STACKOFFSET
472 /*CFI_REGISTER rflags,r11*/
475 * A hypervisor implementation might want to use a label
476 * after the swapgs, so that it can do the swapgs
477 * for the guest and jump here on syscall.
479 ENTRY(system_call_after_swapgs)
481 movq %rsp,%gs:pda_oldrsp
482 movq %gs:pda_kernelstack,%rsp
484 * No need to follow this irqs off/on section - it's straight
487 ENABLE_INTERRUPTS(CLBR_NONE)
489 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
490 movq %rcx,RIP-ARGOFFSET(%rsp)
491 CFI_REL_OFFSET rip,RIP-ARGOFFSET
492 GET_THREAD_INFO(%rcx)
493 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
495 system_call_fastpath:
496 cmpq $__NR_syscall_max,%rax
499 call *sys_call_table(,%rax,8) # XXX: rip relative
500 movq %rax,RAX-ARGOFFSET(%rsp)
502 * Syscall return path ending with SYSRET (fast path)
503 * Has incomplete stack frame and undefined top of stack.
506 movl $_TIF_ALLWORK_MASK,%edi
510 GET_THREAD_INFO(%rcx)
511 DISABLE_INTERRUPTS(CLBR_NONE)
513 movl TI_flags(%rcx),%edx
518 * sysretq will re-enable interrupts:
521 movq RIP-ARGOFFSET(%rsp),%rcx
523 RESTORE_ARGS 0,-ARG_SKIP,1
524 /*CFI_REGISTER rflags,r11*/
525 movq %gs:pda_oldrsp, %rsp
529 /* Handle reschedules */
530 /* edx: work, edi: workmask */
532 bt $TIF_NEED_RESCHED,%edx
535 ENABLE_INTERRUPTS(CLBR_NONE)
537 CFI_ADJUST_CFA_OFFSET 8
540 CFI_ADJUST_CFA_OFFSET -8
543 /* Handle a signal */
546 ENABLE_INTERRUPTS(CLBR_NONE)
547 #ifdef CONFIG_AUDITSYSCALL
548 bt $TIF_SYSCALL_AUDIT,%edx
551 /* edx: work flags (arg3) */
552 leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1
553 xorl %esi,%esi # oldset -> arg2
555 FIXUP_TOP_OF_STACK %r11
556 call do_notify_resume
557 RESTORE_TOP_OF_STACK %r11
559 movl $_TIF_WORK_MASK,%edi
560 /* Use IRET because user could have changed frame. This
561 works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
562 DISABLE_INTERRUPTS(CLBR_NONE)
567 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
568 jmp ret_from_sys_call
570 #ifdef CONFIG_AUDITSYSCALL
572 * Fast path for syscall audit without full syscall trace.
573 * We just call audit_syscall_entry() directly, and then
574 * jump back to the normal fast path.
577 movq %r10,%r9 /* 6th arg: 4th syscall arg */
578 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
579 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
580 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
581 movq %rax,%rsi /* 2nd arg: syscall number */
582 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
583 call audit_syscall_entry
584 LOAD_ARGS 0 /* reload call-clobbered registers */
585 jmp system_call_fastpath
588 * Return fast path for syscall audit. Call audit_syscall_exit()
589 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
593 movq %rax,%rsi /* second arg, syscall return value */
594 cmpq $0,%rax /* is it < 0? */
595 setl %al /* 1 if so, 0 if not */
596 movzbl %al,%edi /* zero-extend that into %edi */
597 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
598 call audit_syscall_exit
599 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
601 #endif /* CONFIG_AUDITSYSCALL */
603 /* Do syscall tracing */
605 #ifdef CONFIG_AUDITSYSCALL
606 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
610 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
611 FIXUP_TOP_OF_STACK %rdi
613 call syscall_trace_enter
615 * Reload arg registers from stack in case ptrace changed them.
616 * We don't reload %rax because syscall_trace_enter() returned
617 * the value it wants us to use in the table lookup.
619 LOAD_ARGS ARGOFFSET, 1
621 cmpq $__NR_syscall_max,%rax
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 .globl int_ret_from_sys_call
633 .globl int_with_check
634 int_ret_from_sys_call:
635 DISABLE_INTERRUPTS(CLBR_NONE)
637 testl $3,CS-ARGOFFSET(%rsp)
638 je retint_restore_args
639 movl $_TIF_ALLWORK_MASK,%edi
640 /* edi: mask to check */
643 GET_THREAD_INFO(%rcx)
644 movl TI_flags(%rcx),%edx
647 andl $~TS_COMPAT,TI_status(%rcx)
650 /* Either reschedule or signal or syscall exit tracking needed. */
651 /* First do a reschedule test. */
652 /* edx: work, edi: workmask */
654 bt $TIF_NEED_RESCHED,%edx
657 ENABLE_INTERRUPTS(CLBR_NONE)
659 CFI_ADJUST_CFA_OFFSET 8
662 CFI_ADJUST_CFA_OFFSET -8
663 DISABLE_INTERRUPTS(CLBR_NONE)
667 /* handle signals and tracing -- both require a full stack frame */
670 ENABLE_INTERRUPTS(CLBR_NONE)
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 0 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 R15+8, r15
726 movq_cfi_restore R14+8, r14
727 movq_cfi_restore R13+8, r13
728 movq_cfi_restore R12+8, r12
729 movq_cfi_restore RBP+8, rbp
730 movq_cfi_restore RBX+8, rbx
731 ret $REST_SKIP /* pop extended registers */
733 END(ptregscall_common)
738 CFI_ADJUST_CFA_OFFSET -8
739 CFI_REGISTER rip, r11
741 FIXUP_TOP_OF_STACK %r11
744 RESTORE_TOP_OF_STACK %r11
747 jmp int_ret_from_sys_call
752 * sigreturn is special because it needs to restore all registers on return.
753 * This cannot be done with SYSRET, so use the IRET return path instead.
755 ENTRY(stub_rt_sigreturn)
758 CFI_ADJUST_CFA_OFFSET -8
761 FIXUP_TOP_OF_STACK %r11
762 call sys_rt_sigreturn
763 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
765 jmp int_ret_from_sys_call
767 END(stub_rt_sigreturn)
770 * Build the entry stubs and pointer table with some assembler magic.
771 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
772 * single cache line on all modern x86 implementations.
774 .section .init.rodata,"a"
778 .p2align CONFIG_X86_L1_CACHE_SHIFT
779 ENTRY(irq_entries_start)
781 vector=FIRST_EXTERNAL_VECTOR
782 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
785 .if vector < NR_VECTORS
786 .if vector <> FIRST_EXTERNAL_VECTOR
787 CFI_ADJUST_CFA_OFFSET -8
789 1: pushq $(~vector+0x80) /* Note: always in signed byte range */
790 CFI_ADJUST_CFA_OFFSET 8
791 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
800 2: jmp common_interrupt
803 END(irq_entries_start)
810 * Interrupt entry/exit.
812 * Interrupt entry points save only callee clobbered registers in fast path.
814 * Entry runs with interrupts off.
817 /* 0(%rsp): ~(interrupt number) */
818 .macro interrupt func
820 CFI_ADJUST_CFA_OFFSET 10*8
827 * The interrupt stubs push (~vector+0x80) onto the stack and
828 * then jump to common_interrupt.
830 .p2align CONFIG_X86_L1_CACHE_SHIFT
833 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
835 /* 0(%rsp): oldrsp-ARGOFFSET */
837 DISABLE_INTERRUPTS(CLBR_NONE)
839 decl %gs:pda_irqcount
841 CFI_DEF_CFA_REGISTER rsp
842 CFI_ADJUST_CFA_OFFSET -8
844 GET_THREAD_INFO(%rcx)
845 testl $3,CS-ARGOFFSET(%rsp)
848 /* Interrupt came from user space */
850 * Has a correct top of stack, but a partial stack frame
851 * %rcx: thread info. Interrupts off.
853 retint_with_reschedule:
854 movl $_TIF_WORK_MASK,%edi
857 movl TI_flags(%rcx),%edx
862 retint_swapgs: /* return to user-space */
864 * The iretq could re-enable interrupts:
866 DISABLE_INTERRUPTS(CLBR_ANY)
871 retint_restore_args: /* return to kernel space */
872 DISABLE_INTERRUPTS(CLBR_ANY)
874 * The iretq could re-enable interrupts:
883 .section __ex_table, "a"
884 .quad irq_return, bad_iret
887 #ifdef CONFIG_PARAVIRT
891 .section __ex_table,"a"
892 .quad native_iret, bad_iret
899 * The iret traps when the %cs or %ss being restored is bogus.
900 * We've lost the original trap vector and error code.
901 * #GPF is the most likely one to get for an invalid selector.
902 * So pretend we completed the iret and took the #GPF in user mode.
904 * We are now running with the kernel GS after exception recovery.
905 * But error_entry expects us to have user GS to match the user %cs,
911 jmp general_protection
915 /* edi: workmask, edx: work */
918 bt $TIF_NEED_RESCHED,%edx
921 ENABLE_INTERRUPTS(CLBR_NONE)
923 CFI_ADJUST_CFA_OFFSET 8
926 CFI_ADJUST_CFA_OFFSET -8
927 GET_THREAD_INFO(%rcx)
928 DISABLE_INTERRUPTS(CLBR_NONE)
933 testl $_TIF_DO_NOTIFY_MASK,%edx
936 ENABLE_INTERRUPTS(CLBR_NONE)
938 movq $-1,ORIG_RAX(%rsp)
939 xorl %esi,%esi # oldset
940 movq %rsp,%rdi # &pt_regs
941 call do_notify_resume
943 DISABLE_INTERRUPTS(CLBR_NONE)
945 GET_THREAD_INFO(%rcx)
946 jmp retint_with_reschedule
948 #ifdef CONFIG_PREEMPT
949 /* Returning to kernel space. Check if we need preemption */
950 /* rcx: threadinfo. interrupts off. */
952 cmpl $0,TI_preempt_count(%rcx)
953 jnz retint_restore_args
954 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
955 jnc retint_restore_args
956 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
957 jnc retint_restore_args
958 call preempt_schedule_irq
963 END(common_interrupt)
968 .macro apicinterrupt num sym do_sym
972 CFI_ADJUST_CFA_OFFSET 8
980 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
981 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
984 apicinterrupt UV_BAU_MESSAGE \
985 uv_bau_message_intr1 uv_bau_message_interrupt
986 apicinterrupt LOCAL_TIMER_VECTOR \
987 apic_timer_interrupt smp_apic_timer_interrupt
990 apicinterrupt INVALIDATE_TLB_VECTOR_START+0 \
991 invalidate_interrupt0 smp_invalidate_interrupt
992 apicinterrupt INVALIDATE_TLB_VECTOR_START+1 \
993 invalidate_interrupt1 smp_invalidate_interrupt
994 apicinterrupt INVALIDATE_TLB_VECTOR_START+2 \
995 invalidate_interrupt2 smp_invalidate_interrupt
996 apicinterrupt INVALIDATE_TLB_VECTOR_START+3 \
997 invalidate_interrupt3 smp_invalidate_interrupt
998 apicinterrupt INVALIDATE_TLB_VECTOR_START+4 \
999 invalidate_interrupt4 smp_invalidate_interrupt
1000 apicinterrupt INVALIDATE_TLB_VECTOR_START+5 \
1001 invalidate_interrupt5 smp_invalidate_interrupt
1002 apicinterrupt INVALIDATE_TLB_VECTOR_START+6 \
1003 invalidate_interrupt6 smp_invalidate_interrupt
1004 apicinterrupt INVALIDATE_TLB_VECTOR_START+7 \
1005 invalidate_interrupt7 smp_invalidate_interrupt
1008 apicinterrupt THRESHOLD_APIC_VECTOR \
1009 threshold_interrupt mce_threshold_interrupt
1010 apicinterrupt THERMAL_APIC_VECTOR \
1011 thermal_interrupt smp_thermal_interrupt
1014 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
1015 call_function_single_interrupt smp_call_function_single_interrupt
1016 apicinterrupt CALL_FUNCTION_VECTOR \
1017 call_function_interrupt smp_call_function_interrupt
1018 apicinterrupt RESCHEDULE_VECTOR \
1019 reschedule_interrupt smp_reschedule_interrupt
1022 apicinterrupt ERROR_APIC_VECTOR \
1023 error_interrupt smp_error_interrupt
1024 apicinterrupt SPURIOUS_APIC_VECTOR \
1025 spurious_interrupt smp_spurious_interrupt
1028 * Exception entry points.
1030 .macro zeroentry sym do_sym
1033 PARAVIRT_ADJUST_EXCEPTION_FRAME
1034 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1036 CFI_ADJUST_CFA_OFFSET 15*8
1039 movq %rsp,%rdi /* pt_regs pointer */
1040 xorl %esi,%esi /* no error code */
1042 jmp error_exit /* %ebx: no swapgs flag */
1047 .macro paranoidzeroentry sym do_sym
1050 PARAVIRT_ADJUST_EXCEPTION_FRAME
1051 pushq $-1 /* ORIG_RAX: no syscall to restart */
1052 CFI_ADJUST_CFA_OFFSET 8
1056 movq %rsp,%rdi /* pt_regs pointer */
1057 xorl %esi,%esi /* no error code */
1059 jmp paranoid_exit /* %ebx: no swapgs flag */
1064 .macro paranoidzeroentry_ist sym do_sym ist
1067 PARAVIRT_ADJUST_EXCEPTION_FRAME
1068 pushq $-1 /* ORIG_RAX: no syscall to restart */
1069 CFI_ADJUST_CFA_OFFSET 8
1073 movq %rsp,%rdi /* pt_regs pointer */
1074 xorl %esi,%esi /* no error code */
1075 movq %gs:pda_data_offset, %rbp
1076 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
1078 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
1079 jmp paranoid_exit /* %ebx: no swapgs flag */
1084 .macro errorentry sym do_sym
1087 PARAVIRT_ADJUST_EXCEPTION_FRAME
1089 CFI_ADJUST_CFA_OFFSET 15*8
1092 movq %rsp,%rdi /* pt_regs pointer */
1093 movq ORIG_RAX(%rsp),%rsi /* get error code */
1094 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1096 jmp error_exit /* %ebx: no swapgs flag */
1101 /* error code is on the stack already */
1102 .macro paranoiderrorentry sym do_sym
1105 PARAVIRT_ADJUST_EXCEPTION_FRAME
1107 CFI_ADJUST_CFA_OFFSET 15*8
1111 movq %rsp,%rdi /* pt_regs pointer */
1112 movq ORIG_RAX(%rsp),%rsi /* get error code */
1113 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1115 jmp paranoid_exit /* %ebx: no swapgs flag */
1120 zeroentry divide_error do_divide_error
1121 zeroentry overflow do_overflow
1122 zeroentry bounds do_bounds
1123 zeroentry invalid_op do_invalid_op
1124 zeroentry device_not_available do_device_not_available
1125 paranoiderrorentry double_fault do_double_fault
1126 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1127 errorentry invalid_TSS do_invalid_TSS
1128 errorentry segment_not_present do_segment_not_present
1129 zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1130 zeroentry coprocessor_error do_coprocessor_error
1131 errorentry alignment_check do_alignment_check
1132 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1134 /* Reload gs selector with exception handling */
1135 /* edi: new selector */
1136 ENTRY(native_load_gs_index)
1139 CFI_ADJUST_CFA_OFFSET 8
1140 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1144 2: mfence /* workaround */
1147 CFI_ADJUST_CFA_OFFSET -8
1150 END(native_load_gs_index)
1152 .section __ex_table,"a"
1154 .quad gs_change,bad_gs
1156 .section .fixup,"ax"
1157 /* running with kernelgs */
1159 SWAPGS /* switch back to user gs */
1166 * Create a kernel thread.
1168 * C extern interface:
1169 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1171 * asm input arguments:
1172 * rdi: fn, rsi: arg, rdx: flags
1174 ENTRY(kernel_thread)
1176 FAKE_STACK_FRAME $child_rip
1179 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1181 orq kernel_thread_flags(%rip),%rdi
1194 * It isn't worth to check for reschedule here,
1195 * so internally to the x86_64 port you can rely on kernel_thread()
1196 * not to reschedule the child before returning, this avoids the need
1197 * of hacks for example to fork off the per-CPU idle tasks.
1198 * [Hopefully no generic code relies on the reschedule -AK]
1207 pushq $0 # fake return address
1210 * Here we are in the child and the registers are set as they were
1211 * at kernel_thread() invocation in the parent.
1219 ud2 # padding for call trace
1224 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1226 * C extern interface:
1227 * extern long execve(char *name, char **argv, char **envp)
1229 * asm input arguments:
1230 * rdi: name, rsi: argv, rdx: envp
1232 * We want to fallback into:
1233 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
1235 * do_sys_execve asm fallback arguments:
1236 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1238 ENTRY(kernel_execve)
1244 movq %rax, RAX(%rsp)
1247 je int_ret_from_sys_call
1254 /* Call softirq on interrupt stack. Interrupts are off. */
1258 CFI_ADJUST_CFA_OFFSET 8
1259 CFI_REL_OFFSET rbp,0
1261 CFI_DEF_CFA_REGISTER rbp
1262 incl %gs:pda_irqcount
1263 cmove %gs:pda_irqstackptr,%rsp
1264 push %rbp # backlink for old unwinder
1267 CFI_DEF_CFA_REGISTER rsp
1268 CFI_ADJUST_CFA_OFFSET -8
1269 decl %gs:pda_irqcount
1275 zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
1278 * A note on the "critical region" in our callback handler.
1279 * We want to avoid stacking callback handlers due to events occurring
1280 * during handling of the last event. To do this, we keep events disabled
1281 * until we've done all processing. HOWEVER, we must enable events before
1282 * popping the stack frame (can't be done atomically) and so it would still
1283 * be possible to get enough handler activations to overflow the stack.
1284 * Although unlikely, bugs of that kind are hard to track down, so we'd
1285 * like to avoid the possibility.
1286 * So, on entry to the handler we detect whether we interrupted an
1287 * existing activation in its critical region -- if so, we pop the current
1288 * activation and restart the handler using the previous one.
1290 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1293 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1294 * see the correct pointer to the pt_regs
1296 movq %rdi, %rsp # we don't return, adjust the stack frame
1299 11: incl %gs:pda_irqcount
1301 CFI_DEF_CFA_REGISTER rbp
1302 cmovzq %gs:pda_irqstackptr,%rsp
1303 pushq %rbp # backlink for old unwinder
1304 call xen_evtchn_do_upcall
1306 CFI_DEF_CFA_REGISTER rsp
1307 decl %gs:pda_irqcount
1310 END(do_hypervisor_callback)
1313 * Hypervisor uses this for application faults while it executes.
1314 * We get here for two reasons:
1315 * 1. Fault while reloading DS, ES, FS or GS
1316 * 2. Fault while executing IRET
1317 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1318 * registers that could be reloaded and zeroed the others.
1319 * Category 2 we fix up by killing the current process. We cannot use the
1320 * normal Linux return path in this case because if we use the IRET hypercall
1321 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1322 * We distinguish between categories by comparing each saved segment register
1323 * with its current contents: any discrepancy means we in category 1.
1325 ENTRY(xen_failsafe_callback)
1327 /*CFI_REL_OFFSET gs,GS*/
1328 /*CFI_REL_OFFSET fs,FS*/
1329 /*CFI_REL_OFFSET es,ES*/
1330 /*CFI_REL_OFFSET ds,DS*/
1331 CFI_REL_OFFSET r11,8
1332 CFI_REL_OFFSET rcx,0
1346 /* All segments match their saved values => Category 2 (Bad IRET). */
1352 CFI_ADJUST_CFA_OFFSET -0x30
1353 pushq_cfi $0 /* RIP */
1356 jmp general_protection
1358 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1364 CFI_ADJUST_CFA_OFFSET -0x30
1369 END(xen_failsafe_callback)
1371 #endif /* CONFIG_XEN */
1374 * Some functions should be protected against kprobes
1376 .pushsection .kprobes.text, "ax"
1378 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1379 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1380 paranoiderrorentry stack_segment do_stack_segment
1381 errorentry general_protection do_general_protection
1382 errorentry page_fault do_page_fault
1383 #ifdef CONFIG_X86_MCE
1384 paranoidzeroentry machine_check do_machine_check
1388 * "Paranoid" exit path from exception stack.
1389 * Paranoid because this is used by NMIs and cannot take
1390 * any kernel state for granted.
1391 * We don't do kernel preemption checks here, because only
1392 * NMI should be common and it does not enable IRQs and
1393 * cannot get reschedule ticks.
1395 * "trace" is 0 for the NMI handler only, because irq-tracing
1396 * is fundamentally NMI-unsafe. (we cannot change the soft and
1397 * hard flags at once, atomically)
1400 /* ebx: no swapgs flag */
1401 ENTRY(paranoid_exit)
1403 DISABLE_INTERRUPTS(CLBR_NONE)
1405 testl %ebx,%ebx /* swapgs needed? */
1406 jnz paranoid_restore
1408 jnz paranoid_userspace
1416 GET_THREAD_INFO(%rcx)
1417 movl TI_flags(%rcx),%ebx
1418 andl $_TIF_WORK_MASK,%ebx
1420 movq %rsp,%rdi /* &pt_regs */
1422 movq %rax,%rsp /* switch stack for scheduling */
1423 testl $_TIF_NEED_RESCHED,%ebx
1424 jnz paranoid_schedule
1425 movl %ebx,%edx /* arg3: thread flags */
1427 ENABLE_INTERRUPTS(CLBR_NONE)
1428 xorl %esi,%esi /* arg2: oldset */
1429 movq %rsp,%rdi /* arg1: &pt_regs */
1430 call do_notify_resume
1431 DISABLE_INTERRUPTS(CLBR_NONE)
1433 jmp paranoid_userspace
1436 ENABLE_INTERRUPTS(CLBR_ANY)
1438 DISABLE_INTERRUPTS(CLBR_ANY)
1440 jmp paranoid_userspace
1445 * Exception entry point. This expects an error code/orig_rax on the stack.
1446 * returns in "no swapgs flag" in %ebx.
1450 CFI_ADJUST_CFA_OFFSET 15*8
1451 /* oldrax contains error code */
1470 je error_kernelspace
1479 * There are two places in the kernel that can potentially fault with
1480 * usergs. Handle them here. The exception handlers after iret run with
1481 * kernel gs again, so don't set the user space flag. B stepping K8s
1482 * sometimes report an truncated RIP for IRET exceptions returning to
1483 * compat mode. Check for these here too.
1487 leaq irq_return(%rip),%rcx
1488 cmpq %rcx,RIP+8(%rsp)
1490 movl %ecx,%ecx /* zero extend */
1491 cmpq %rcx,RIP+8(%rsp)
1493 cmpq $gs_change,RIP+8(%rsp)
1499 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1504 DISABLE_INTERRUPTS(CLBR_NONE)
1506 GET_THREAD_INFO(%rcx)
1509 LOCKDEP_SYS_EXIT_IRQ
1510 movl TI_flags(%rcx),%edx
1511 movl $_TIF_WORK_MASK,%edi
1519 /* runs on exception stack */
1522 PARAVIRT_ADJUST_EXCEPTION_FRAME
1525 CFI_ADJUST_CFA_OFFSET 15*8
1528 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1532 #ifdef CONFIG_TRACE_IRQFLAGS
1533 /* paranoidexit; without TRACE_IRQS_OFF */
1534 /* ebx: no swapgs flag */
1535 DISABLE_INTERRUPTS(CLBR_NONE)
1536 testl %ebx,%ebx /* swapgs needed? */
1546 GET_THREAD_INFO(%rcx)
1547 movl TI_flags(%rcx),%ebx
1548 andl $_TIF_WORK_MASK,%ebx
1550 movq %rsp,%rdi /* &pt_regs */
1552 movq %rax,%rsp /* switch stack for scheduling */
1553 testl $_TIF_NEED_RESCHED,%ebx
1555 movl %ebx,%edx /* arg3: thread flags */
1556 ENABLE_INTERRUPTS(CLBR_NONE)
1557 xorl %esi,%esi /* arg2: oldset */
1558 movq %rsp,%rdi /* arg1: &pt_regs */
1559 call do_notify_resume
1560 DISABLE_INTERRUPTS(CLBR_NONE)
1563 ENABLE_INTERRUPTS(CLBR_ANY)
1565 DISABLE_INTERRUPTS(CLBR_ANY)
1574 ENTRY(ignore_sysret)
1582 * End of kprobes section