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>
51 #include <asm/page_types.h>
52 #include <asm/irqflags.h>
53 #include <asm/paravirt.h>
54 #include <asm/ftrace.h>
55 #include <asm/percpu.h>
57 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
58 #include <linux/elf-em.h>
59 #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
60 #define __AUDIT_ARCH_64BIT 0x80000000
61 #define __AUDIT_ARCH_LE 0x40000000
64 #ifdef CONFIG_FUNCTION_TRACER
65 #ifdef CONFIG_DYNAMIC_FTRACE
71 cmpl $0, function_trace_stop
78 subq $MCOUNT_INSN_SIZE, %rdi
85 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
86 GLOBAL(ftrace_graph_call)
94 #else /* ! CONFIG_DYNAMIC_FTRACE */
96 cmpl $0, function_trace_stop
99 cmpq $ftrace_stub, ftrace_trace_function
102 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
103 cmpq $ftrace_stub, ftrace_graph_return
104 jnz ftrace_graph_caller
106 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
107 jnz ftrace_graph_caller
116 movq 0x38(%rsp), %rdi
118 subq $MCOUNT_INSN_SIZE, %rdi
120 call *ftrace_trace_function
126 #endif /* CONFIG_DYNAMIC_FTRACE */
127 #endif /* CONFIG_FUNCTION_TRACER */
129 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
130 ENTRY(ftrace_graph_caller)
131 cmpl $0, function_trace_stop
137 movq 0x38(%rsp), %rsi
138 subq $MCOUNT_INSN_SIZE, %rsi
140 call prepare_ftrace_return
145 END(ftrace_graph_caller)
147 GLOBAL(return_to_handler)
160 call ftrace_return_to_handler
177 #ifndef CONFIG_PREEMPT
178 #define retint_kernel retint_restore_args
181 #ifdef CONFIG_PARAVIRT
182 ENTRY(native_usergs_sysret64)
185 ENDPROC(native_usergs_sysret64)
186 #endif /* CONFIG_PARAVIRT */
189 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
190 #ifdef CONFIG_TRACE_IRQFLAGS
191 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
199 * C code is not supposed to know about undefined top of stack. Every time
200 * a C function with an pt_regs argument is called from the SYSCALL based
201 * fast path FIXUP_TOP_OF_STACK is needed.
202 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
206 /* %rsp:at FRAMEEND */
207 .macro FIXUP_TOP_OF_STACK tmp offset=0
208 movq PER_CPU_VAR(old_rsp),\tmp
209 movq \tmp,RSP+\offset(%rsp)
210 movq $__USER_DS,SS+\offset(%rsp)
211 movq $__USER_CS,CS+\offset(%rsp)
212 movq $-1,RCX+\offset(%rsp)
213 movq R11+\offset(%rsp),\tmp /* get eflags */
214 movq \tmp,EFLAGS+\offset(%rsp)
217 .macro RESTORE_TOP_OF_STACK tmp offset=0
218 movq RSP+\offset(%rsp),\tmp
219 movq \tmp,PER_CPU_VAR(old_rsp)
220 movq EFLAGS+\offset(%rsp),\tmp
221 movq \tmp,R11+\offset(%rsp)
224 .macro FAKE_STACK_FRAME child_rip
225 /* push in order ss, rsp, eflags, cs, rip */
227 pushq $__KERNEL_DS /* ss */
228 CFI_ADJUST_CFA_OFFSET 8
229 /*CFI_REL_OFFSET ss,0*/
231 CFI_ADJUST_CFA_OFFSET 8
233 pushq $X86_EFLAGS_IF /* eflags - interrupts on */
234 CFI_ADJUST_CFA_OFFSET 8
235 /*CFI_REL_OFFSET rflags,0*/
236 pushq $__KERNEL_CS /* cs */
237 CFI_ADJUST_CFA_OFFSET 8
238 /*CFI_REL_OFFSET cs,0*/
239 pushq \child_rip /* rip */
240 CFI_ADJUST_CFA_OFFSET 8
242 pushq %rax /* orig rax */
243 CFI_ADJUST_CFA_OFFSET 8
246 .macro UNFAKE_STACK_FRAME
248 CFI_ADJUST_CFA_OFFSET -(6*8)
252 * initial frame state for interrupts (and exceptions without error code)
254 .macro EMPTY_FRAME start=1 offset=0
258 CFI_DEF_CFA rsp,8+\offset
260 CFI_DEF_CFA_OFFSET 8+\offset
265 * initial frame state for interrupts (and exceptions without error code)
267 .macro INTR_FRAME start=1 offset=0
268 EMPTY_FRAME \start, SS+8+\offset-RIP
269 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
270 CFI_REL_OFFSET rsp, RSP+\offset-RIP
271 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
272 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
273 CFI_REL_OFFSET rip, RIP+\offset-RIP
277 * initial frame state for exceptions with error code (and interrupts
278 * with vector already pushed)
280 .macro XCPT_FRAME start=1 offset=0
281 INTR_FRAME \start, RIP+\offset-ORIG_RAX
282 /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
286 * frame that enables calling into C.
288 .macro PARTIAL_FRAME start=1 offset=0
289 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
290 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
291 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
292 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
293 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
294 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
295 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
296 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
297 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
298 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
302 * frame that enables passing a complete pt_regs to a C function.
304 .macro DEFAULT_FRAME start=1 offset=0
305 PARTIAL_FRAME \start, R11+\offset-R15
306 CFI_REL_OFFSET rbx, RBX+\offset
307 CFI_REL_OFFSET rbp, RBP+\offset
308 CFI_REL_OFFSET r12, R12+\offset
309 CFI_REL_OFFSET r13, R13+\offset
310 CFI_REL_OFFSET r14, R14+\offset
311 CFI_REL_OFFSET r15, R15+\offset
314 /* save partial stack frame */
318 movq_cfi rdi, RDI+16-ARGOFFSET
319 movq_cfi rsi, RSI+16-ARGOFFSET
320 movq_cfi rdx, RDX+16-ARGOFFSET
321 movq_cfi rcx, RCX+16-ARGOFFSET
322 movq_cfi rax, RAX+16-ARGOFFSET
323 movq_cfi r8, R8+16-ARGOFFSET
324 movq_cfi r9, R9+16-ARGOFFSET
325 movq_cfi r10, R10+16-ARGOFFSET
326 movq_cfi r11, R11+16-ARGOFFSET
328 leaq -ARGOFFSET+16(%rsp),%rdi /* arg1 for handler */
329 movq_cfi rbp, 8 /* push %rbp */
330 leaq 8(%rsp), %rbp /* mov %rsp, %ebp */
335 * irq_count is used to check if a CPU is already on an interrupt stack
336 * or not. While this is essentially redundant with preempt_count it is
337 * a little cheaper to use a separate counter in the PDA (short of
338 * moving irq_enter into assembly, which would be too much work)
340 1: incl PER_CPU_VAR(irq_count)
342 popq_cfi %rax /* move return address... */
343 mov PER_CPU_VAR(irq_stack_ptr),%rsp
345 pushq_cfi %rbp /* backlink for unwinder */
346 pushq_cfi %rax /* ... to the new stack */
348 * We entered an interrupt context - irqs are off:
356 PARTIAL_FRAME 1 REST_SKIP+8
357 movq 5*8+16(%rsp), %r11 /* save return address */
364 movq %r11, 8(%rsp) /* return address */
365 FIXUP_TOP_OF_STACK %r11, 16
370 /* save complete stack frame */
371 .pushsection .kprobes.text, "ax"
391 movl $MSR_GS_BASE,%ecx
394 js 1f /* negative -> in kernel */
403 * A newly forked process directly context switches into this address.
405 * rdi: prev task we switched from
410 LOCK ; btr $TIF_FORK,TI_flags(%r8)
412 push kernel_eflags(%rip)
413 CFI_ADJUST_CFA_OFFSET 8
414 popf # reset kernel eflags
415 CFI_ADJUST_CFA_OFFSET -8
417 call schedule_tail # rdi: 'prev' task parameter
419 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
436 * System call entry. Upto 6 arguments in registers are supported.
438 * SYSCALL does not save anything on the stack and does not change the
444 * rax system call number
446 * rcx return address for syscall/sysret, C arg3
449 * r10 arg3 (--> moved to rcx for C)
452 * r11 eflags for syscall/sysret, temporary for C
453 * r12-r15,rbp,rbx saved by C code, not touched.
455 * Interrupts are off on entry.
456 * Only called from user space.
458 * XXX if we had a free scratch register we could save the RSP into the stack frame
459 * and report it properly in ps. Unfortunately we haven't.
461 * When user can change the frames always force IRET. That is because
462 * it deals with uncanonical addresses better. SYSRET has trouble
463 * with them due to bugs in both AMD and Intel CPUs.
469 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
471 /*CFI_REGISTER rflags,r11*/
474 * A hypervisor implementation might want to use a label
475 * after the swapgs, so that it can do the swapgs
476 * for the guest and jump here on syscall.
478 ENTRY(system_call_after_swapgs)
480 movq %rsp,PER_CPU_VAR(old_rsp)
481 movq PER_CPU_VAR(kernel_stack),%rsp
483 * No need to follow this irqs off/on section - it's straight
486 ENABLE_INTERRUPTS(CLBR_NONE)
488 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
489 movq %rcx,RIP-ARGOFFSET(%rsp)
490 CFI_REL_OFFSET rip,RIP-ARGOFFSET
491 GET_THREAD_INFO(%rcx)
492 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
494 system_call_fastpath:
495 cmpq $__NR_syscall_max,%rax
498 call *sys_call_table(,%rax,8) # XXX: rip relative
499 movq %rax,RAX-ARGOFFSET(%rsp)
501 * Syscall return path ending with SYSRET (fast path)
502 * Has incomplete stack frame and undefined top of stack.
505 movl $_TIF_ALLWORK_MASK,%edi
509 GET_THREAD_INFO(%rcx)
510 DISABLE_INTERRUPTS(CLBR_NONE)
512 movl TI_flags(%rcx),%edx
517 * sysretq will re-enable interrupts:
520 movq RIP-ARGOFFSET(%rsp),%rcx
522 RESTORE_ARGS 0,-ARG_SKIP,1
523 /*CFI_REGISTER rflags,r11*/
524 movq PER_CPU_VAR(old_rsp), %rsp
528 /* Handle reschedules */
529 /* edx: work, edi: workmask */
531 bt $TIF_NEED_RESCHED,%edx
534 ENABLE_INTERRUPTS(CLBR_NONE)
536 CFI_ADJUST_CFA_OFFSET 8
539 CFI_ADJUST_CFA_OFFSET -8
542 /* Handle a signal */
545 ENABLE_INTERRUPTS(CLBR_NONE)
546 #ifdef CONFIG_AUDITSYSCALL
547 bt $TIF_SYSCALL_AUDIT,%edx
550 /* edx: work flags (arg3) */
551 leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1
552 xorl %esi,%esi # oldset -> arg2
554 FIXUP_TOP_OF_STACK %r11
555 call do_notify_resume
556 RESTORE_TOP_OF_STACK %r11
558 movl $_TIF_WORK_MASK,%edi
559 /* Use IRET because user could have changed frame. This
560 works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
561 DISABLE_INTERRUPTS(CLBR_NONE)
566 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
567 jmp ret_from_sys_call
569 #ifdef CONFIG_AUDITSYSCALL
571 * Fast path for syscall audit without full syscall trace.
572 * We just call audit_syscall_entry() directly, and then
573 * jump back to the normal fast path.
576 movq %r10,%r9 /* 6th arg: 4th syscall arg */
577 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
578 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
579 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
580 movq %rax,%rsi /* 2nd arg: syscall number */
581 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
582 call audit_syscall_entry
583 LOAD_ARGS 0 /* reload call-clobbered registers */
584 jmp system_call_fastpath
587 * Return fast path for syscall audit. Call audit_syscall_exit()
588 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
592 movq %rax,%rsi /* second arg, syscall return value */
593 cmpq $0,%rax /* is it < 0? */
594 setl %al /* 1 if so, 0 if not */
595 movzbl %al,%edi /* zero-extend that into %edi */
596 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
597 call audit_syscall_exit
598 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
600 #endif /* CONFIG_AUDITSYSCALL */
602 /* Do syscall tracing */
604 #ifdef CONFIG_AUDITSYSCALL
605 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
609 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
610 FIXUP_TOP_OF_STACK %rdi
612 call syscall_trace_enter
614 * Reload arg registers from stack in case ptrace changed them.
615 * We don't reload %rax because syscall_trace_enter() returned
616 * the value it wants us to use in the table lookup.
618 LOAD_ARGS ARGOFFSET, 1
620 cmpq $__NR_syscall_max,%rax
621 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
622 movq %r10,%rcx /* fixup for C */
623 call *sys_call_table(,%rax,8)
624 movq %rax,RAX-ARGOFFSET(%rsp)
625 /* Use IRET because user could have changed frame */
628 * Syscall return path ending with IRET.
629 * Has correct top of stack, but partial stack frame.
631 GLOBAL(int_ret_from_sys_call)
632 DISABLE_INTERRUPTS(CLBR_NONE)
634 testl $3,CS-ARGOFFSET(%rsp)
635 je retint_restore_args
636 movl $_TIF_ALLWORK_MASK,%edi
637 /* edi: mask to check */
638 GLOBAL(int_with_check)
640 GET_THREAD_INFO(%rcx)
641 movl TI_flags(%rcx),%edx
644 andl $~TS_COMPAT,TI_status(%rcx)
647 /* Either reschedule or signal or syscall exit tracking needed. */
648 /* First do a reschedule test. */
649 /* edx: work, edi: workmask */
651 bt $TIF_NEED_RESCHED,%edx
654 ENABLE_INTERRUPTS(CLBR_NONE)
656 CFI_ADJUST_CFA_OFFSET 8
659 CFI_ADJUST_CFA_OFFSET -8
660 DISABLE_INTERRUPTS(CLBR_NONE)
664 /* handle signals and tracing -- both require a full stack frame */
667 ENABLE_INTERRUPTS(CLBR_NONE)
669 /* Check for syscall exit trace */
670 testl $_TIF_WORK_SYSCALL_EXIT,%edx
673 CFI_ADJUST_CFA_OFFSET 8
674 leaq 8(%rsp),%rdi # &ptregs -> arg1
675 call syscall_trace_leave
677 CFI_ADJUST_CFA_OFFSET -8
678 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
682 testl $_TIF_DO_NOTIFY_MASK,%edx
684 movq %rsp,%rdi # &ptregs -> arg1
685 xorl %esi,%esi # oldset -> arg2
686 call do_notify_resume
687 1: movl $_TIF_WORK_MASK,%edi
690 DISABLE_INTERRUPTS(CLBR_NONE)
697 * Certain special system calls that need to save a complete full stack frame.
699 .macro PTREGSCALL label,func,arg
701 PARTIAL_FRAME 1 8 /* offset 8: return address */
702 subq $REST_SKIP, %rsp
703 CFI_ADJUST_CFA_OFFSET REST_SKIP
705 DEFAULT_FRAME 0 8 /* offset 8: return address */
706 leaq 8(%rsp), \arg /* pt_regs pointer */
708 jmp ptregscall_common
713 PTREGSCALL stub_clone, sys_clone, %r8
714 PTREGSCALL stub_fork, sys_fork, %rdi
715 PTREGSCALL stub_vfork, sys_vfork, %rdi
716 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
717 PTREGSCALL stub_iopl, sys_iopl, %rsi
719 ENTRY(ptregscall_common)
720 DEFAULT_FRAME 1 8 /* offset 8: return address */
721 RESTORE_TOP_OF_STACK %r11, 8
722 movq_cfi_restore R15+8, r15
723 movq_cfi_restore R14+8, r14
724 movq_cfi_restore R13+8, r13
725 movq_cfi_restore R12+8, r12
726 movq_cfi_restore RBP+8, rbp
727 movq_cfi_restore RBX+8, rbx
728 ret $REST_SKIP /* pop extended registers */
730 END(ptregscall_common)
735 CFI_ADJUST_CFA_OFFSET -8
736 CFI_REGISTER rip, r11
738 FIXUP_TOP_OF_STACK %r11
741 RESTORE_TOP_OF_STACK %r11
744 jmp int_ret_from_sys_call
749 * sigreturn is special because it needs to restore all registers on return.
750 * This cannot be done with SYSRET, so use the IRET return path instead.
752 ENTRY(stub_rt_sigreturn)
755 CFI_ADJUST_CFA_OFFSET -8
758 FIXUP_TOP_OF_STACK %r11
759 call sys_rt_sigreturn
760 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
762 jmp int_ret_from_sys_call
764 END(stub_rt_sigreturn)
767 * Build the entry stubs and pointer table with some assembler magic.
768 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
769 * single cache line on all modern x86 implementations.
771 .section .init.rodata,"a"
775 .p2align CONFIG_X86_L1_CACHE_SHIFT
776 ENTRY(irq_entries_start)
778 vector=FIRST_EXTERNAL_VECTOR
779 .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
782 .if vector < NR_VECTORS
783 .if vector <> FIRST_EXTERNAL_VECTOR
784 CFI_ADJUST_CFA_OFFSET -8
786 1: pushq $(~vector+0x80) /* Note: always in signed byte range */
787 CFI_ADJUST_CFA_OFFSET 8
788 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
797 2: jmp common_interrupt
800 END(irq_entries_start)
807 * Interrupt entry/exit.
809 * Interrupt entry points save only callee clobbered registers in fast path.
811 * Entry runs with interrupts off.
814 /* 0(%rsp): ~(interrupt number) */
815 .macro interrupt func
817 CFI_ADJUST_CFA_OFFSET 10*8
824 * The interrupt stubs push (~vector+0x80) onto the stack and
825 * then jump to common_interrupt.
827 .p2align CONFIG_X86_L1_CACHE_SHIFT
830 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
832 /* 0(%rsp): old_rsp-ARGOFFSET */
834 DISABLE_INTERRUPTS(CLBR_NONE)
836 decl PER_CPU_VAR(irq_count)
838 CFI_DEF_CFA_REGISTER rsp
839 CFI_ADJUST_CFA_OFFSET -8
841 GET_THREAD_INFO(%rcx)
842 testl $3,CS-ARGOFFSET(%rsp)
845 /* Interrupt came from user space */
847 * Has a correct top of stack, but a partial stack frame
848 * %rcx: thread info. Interrupts off.
850 retint_with_reschedule:
851 movl $_TIF_WORK_MASK,%edi
854 movl TI_flags(%rcx),%edx
859 retint_swapgs: /* return to user-space */
861 * The iretq could re-enable interrupts:
863 DISABLE_INTERRUPTS(CLBR_ANY)
868 retint_restore_args: /* return to kernel space */
869 DISABLE_INTERRUPTS(CLBR_ANY)
871 * The iretq could re-enable interrupts:
880 .section __ex_table, "a"
881 .quad irq_return, bad_iret
884 #ifdef CONFIG_PARAVIRT
888 .section __ex_table,"a"
889 .quad native_iret, bad_iret
896 * The iret traps when the %cs or %ss being restored is bogus.
897 * We've lost the original trap vector and error code.
898 * #GPF is the most likely one to get for an invalid selector.
899 * So pretend we completed the iret and took the #GPF in user mode.
901 * We are now running with the kernel GS after exception recovery.
902 * But error_entry expects us to have user GS to match the user %cs,
908 jmp general_protection
912 /* edi: workmask, edx: work */
915 bt $TIF_NEED_RESCHED,%edx
918 ENABLE_INTERRUPTS(CLBR_NONE)
920 CFI_ADJUST_CFA_OFFSET 8
923 CFI_ADJUST_CFA_OFFSET -8
924 GET_THREAD_INFO(%rcx)
925 DISABLE_INTERRUPTS(CLBR_NONE)
930 testl $_TIF_DO_NOTIFY_MASK,%edx
933 ENABLE_INTERRUPTS(CLBR_NONE)
935 movq $-1,ORIG_RAX(%rsp)
936 xorl %esi,%esi # oldset
937 movq %rsp,%rdi # &pt_regs
938 call do_notify_resume
940 DISABLE_INTERRUPTS(CLBR_NONE)
942 GET_THREAD_INFO(%rcx)
943 jmp retint_with_reschedule
945 #ifdef CONFIG_PREEMPT
946 /* Returning to kernel space. Check if we need preemption */
947 /* rcx: threadinfo. interrupts off. */
949 cmpl $0,TI_preempt_count(%rcx)
950 jnz retint_restore_args
951 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
952 jnc retint_restore_args
953 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
954 jnc retint_restore_args
955 call preempt_schedule_irq
960 END(common_interrupt)
965 .macro apicinterrupt num sym do_sym
969 CFI_ADJUST_CFA_OFFSET 8
977 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
978 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
982 apicinterrupt UV_BAU_MESSAGE \
983 uv_bau_message_intr1 uv_bau_message_interrupt
985 apicinterrupt LOCAL_TIMER_VECTOR \
986 apic_timer_interrupt smp_apic_timer_interrupt
987 apicinterrupt GENERIC_INTERRUPT_VECTOR \
988 generic_interrupt smp_generic_interrupt
991 apicinterrupt INVALIDATE_TLB_VECTOR_START+0 \
992 invalidate_interrupt0 smp_invalidate_interrupt
993 apicinterrupt INVALIDATE_TLB_VECTOR_START+1 \
994 invalidate_interrupt1 smp_invalidate_interrupt
995 apicinterrupt INVALIDATE_TLB_VECTOR_START+2 \
996 invalidate_interrupt2 smp_invalidate_interrupt
997 apicinterrupt INVALIDATE_TLB_VECTOR_START+3 \
998 invalidate_interrupt3 smp_invalidate_interrupt
999 apicinterrupt INVALIDATE_TLB_VECTOR_START+4 \
1000 invalidate_interrupt4 smp_invalidate_interrupt
1001 apicinterrupt INVALIDATE_TLB_VECTOR_START+5 \
1002 invalidate_interrupt5 smp_invalidate_interrupt
1003 apicinterrupt INVALIDATE_TLB_VECTOR_START+6 \
1004 invalidate_interrupt6 smp_invalidate_interrupt
1005 apicinterrupt INVALIDATE_TLB_VECTOR_START+7 \
1006 invalidate_interrupt7 smp_invalidate_interrupt
1009 apicinterrupt THRESHOLD_APIC_VECTOR \
1010 threshold_interrupt mce_threshold_interrupt
1011 apicinterrupt THERMAL_APIC_VECTOR \
1012 thermal_interrupt smp_thermal_interrupt
1015 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
1016 call_function_single_interrupt smp_call_function_single_interrupt
1017 apicinterrupt CALL_FUNCTION_VECTOR \
1018 call_function_interrupt smp_call_function_interrupt
1019 apicinterrupt RESCHEDULE_VECTOR \
1020 reschedule_interrupt smp_reschedule_interrupt
1023 apicinterrupt ERROR_APIC_VECTOR \
1024 error_interrupt smp_error_interrupt
1025 apicinterrupt SPURIOUS_APIC_VECTOR \
1026 spurious_interrupt smp_spurious_interrupt
1029 * Exception entry points.
1031 .macro zeroentry sym do_sym
1034 PARAVIRT_ADJUST_EXCEPTION_FRAME
1035 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1037 CFI_ADJUST_CFA_OFFSET 15*8
1040 movq %rsp,%rdi /* pt_regs pointer */
1041 xorl %esi,%esi /* no error code */
1043 jmp error_exit /* %ebx: no swapgs flag */
1048 .macro paranoidzeroentry sym do_sym
1051 PARAVIRT_ADJUST_EXCEPTION_FRAME
1052 pushq $-1 /* ORIG_RAX: no syscall to restart */
1053 CFI_ADJUST_CFA_OFFSET 8
1057 movq %rsp,%rdi /* pt_regs pointer */
1058 xorl %esi,%esi /* no error code */
1060 jmp paranoid_exit /* %ebx: no swapgs flag */
1065 .macro paranoidzeroentry_ist sym do_sym ist
1068 PARAVIRT_ADJUST_EXCEPTION_FRAME
1069 pushq $-1 /* ORIG_RAX: no syscall to restart */
1070 CFI_ADJUST_CFA_OFFSET 8
1074 movq %rsp,%rdi /* pt_regs pointer */
1075 xorl %esi,%esi /* no error code */
1076 PER_CPU(init_tss, %rbp)
1077 subq $EXCEPTION_STKSZ, TSS_ist + (\ist - 1) * 8(%rbp)
1079 addq $EXCEPTION_STKSZ, TSS_ist + (\ist - 1) * 8(%rbp)
1080 jmp paranoid_exit /* %ebx: no swapgs flag */
1085 .macro errorentry sym do_sym
1088 PARAVIRT_ADJUST_EXCEPTION_FRAME
1090 CFI_ADJUST_CFA_OFFSET 15*8
1093 movq %rsp,%rdi /* pt_regs pointer */
1094 movq ORIG_RAX(%rsp),%rsi /* get error code */
1095 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1097 jmp error_exit /* %ebx: no swapgs flag */
1102 /* error code is on the stack already */
1103 .macro paranoiderrorentry sym do_sym
1106 PARAVIRT_ADJUST_EXCEPTION_FRAME
1108 CFI_ADJUST_CFA_OFFSET 15*8
1112 movq %rsp,%rdi /* pt_regs pointer */
1113 movq ORIG_RAX(%rsp),%rsi /* get error code */
1114 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1116 jmp paranoid_exit /* %ebx: no swapgs flag */
1121 zeroentry divide_error do_divide_error
1122 zeroentry overflow do_overflow
1123 zeroentry bounds do_bounds
1124 zeroentry invalid_op do_invalid_op
1125 zeroentry device_not_available do_device_not_available
1126 paranoiderrorentry double_fault do_double_fault
1127 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1128 errorentry invalid_TSS do_invalid_TSS
1129 errorentry segment_not_present do_segment_not_present
1130 zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1131 zeroentry coprocessor_error do_coprocessor_error
1132 errorentry alignment_check do_alignment_check
1133 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1135 /* Reload gs selector with exception handling */
1136 /* edi: new selector */
1137 ENTRY(native_load_gs_index)
1140 CFI_ADJUST_CFA_OFFSET 8
1141 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1145 2: mfence /* workaround */
1148 CFI_ADJUST_CFA_OFFSET -8
1151 END(native_load_gs_index)
1153 .section __ex_table,"a"
1155 .quad gs_change,bad_gs
1157 .section .fixup,"ax"
1158 /* running with kernelgs */
1160 SWAPGS /* switch back to user gs */
1167 * Create a kernel thread.
1169 * C extern interface:
1170 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1172 * asm input arguments:
1173 * rdi: fn, rsi: arg, rdx: flags
1175 ENTRY(kernel_thread)
1177 FAKE_STACK_FRAME $child_rip
1180 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1182 orq kernel_thread_flags(%rip),%rdi
1195 * It isn't worth to check for reschedule here,
1196 * so internally to the x86_64 port you can rely on kernel_thread()
1197 * not to reschedule the child before returning, this avoids the need
1198 * of hacks for example to fork off the per-CPU idle tasks.
1199 * [Hopefully no generic code relies on the reschedule -AK]
1208 pushq $0 # fake return address
1211 * Here we are in the child and the registers are set as they were
1212 * at kernel_thread() invocation in the parent.
1220 ud2 # padding for call trace
1225 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1227 * C extern interface:
1228 * extern long execve(char *name, char **argv, char **envp)
1230 * asm input arguments:
1231 * rdi: name, rsi: argv, rdx: envp
1233 * We want to fallback into:
1234 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
1236 * do_sys_execve asm fallback arguments:
1237 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1239 ENTRY(kernel_execve)
1245 movq %rax, RAX(%rsp)
1248 je int_ret_from_sys_call
1255 /* Call softirq on interrupt stack. Interrupts are off. */
1259 CFI_ADJUST_CFA_OFFSET 8
1260 CFI_REL_OFFSET rbp,0
1262 CFI_DEF_CFA_REGISTER rbp
1263 incl PER_CPU_VAR(irq_count)
1264 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1265 push %rbp # backlink for old unwinder
1268 CFI_DEF_CFA_REGISTER rsp
1269 CFI_ADJUST_CFA_OFFSET -8
1270 decl PER_CPU_VAR(irq_count)
1276 zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
1279 * A note on the "critical region" in our callback handler.
1280 * We want to avoid stacking callback handlers due to events occurring
1281 * during handling of the last event. To do this, we keep events disabled
1282 * until we've done all processing. HOWEVER, we must enable events before
1283 * popping the stack frame (can't be done atomically) and so it would still
1284 * be possible to get enough handler activations to overflow the stack.
1285 * Although unlikely, bugs of that kind are hard to track down, so we'd
1286 * like to avoid the possibility.
1287 * So, on entry to the handler we detect whether we interrupted an
1288 * existing activation in its critical region -- if so, we pop the current
1289 * activation and restart the handler using the previous one.
1291 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1294 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1295 * see the correct pointer to the pt_regs
1297 movq %rdi, %rsp # we don't return, adjust the stack frame
1300 11: incl PER_CPU_VAR(irq_count)
1302 CFI_DEF_CFA_REGISTER rbp
1303 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1304 pushq %rbp # backlink for old unwinder
1305 call xen_evtchn_do_upcall
1307 CFI_DEF_CFA_REGISTER rsp
1308 decl PER_CPU_VAR(irq_count)
1311 END(do_hypervisor_callback)
1314 * Hypervisor uses this for application faults while it executes.
1315 * We get here for two reasons:
1316 * 1. Fault while reloading DS, ES, FS or GS
1317 * 2. Fault while executing IRET
1318 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1319 * registers that could be reloaded and zeroed the others.
1320 * Category 2 we fix up by killing the current process. We cannot use the
1321 * normal Linux return path in this case because if we use the IRET hypercall
1322 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1323 * We distinguish between categories by comparing each saved segment register
1324 * with its current contents: any discrepancy means we in category 1.
1326 ENTRY(xen_failsafe_callback)
1328 /*CFI_REL_OFFSET gs,GS*/
1329 /*CFI_REL_OFFSET fs,FS*/
1330 /*CFI_REL_OFFSET es,ES*/
1331 /*CFI_REL_OFFSET ds,DS*/
1332 CFI_REL_OFFSET r11,8
1333 CFI_REL_OFFSET rcx,0
1347 /* All segments match their saved values => Category 2 (Bad IRET). */
1353 CFI_ADJUST_CFA_OFFSET -0x30
1354 pushq_cfi $0 /* RIP */
1357 jmp general_protection
1359 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1365 CFI_ADJUST_CFA_OFFSET -0x30
1370 END(xen_failsafe_callback)
1372 #endif /* CONFIG_XEN */
1375 * Some functions should be protected against kprobes
1377 .pushsection .kprobes.text, "ax"
1379 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1380 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1381 paranoiderrorentry stack_segment do_stack_segment
1383 zeroentry xen_debug do_debug
1384 zeroentry xen_int3 do_int3
1385 errorentry xen_stack_segment do_stack_segment
1387 errorentry general_protection do_general_protection
1388 errorentry page_fault do_page_fault
1389 #ifdef CONFIG_X86_MCE
1390 paranoidzeroentry machine_check do_machine_check
1394 * "Paranoid" exit path from exception stack.
1395 * Paranoid because this is used by NMIs and cannot take
1396 * any kernel state for granted.
1397 * We don't do kernel preemption checks here, because only
1398 * NMI should be common and it does not enable IRQs and
1399 * cannot get reschedule ticks.
1401 * "trace" is 0 for the NMI handler only, because irq-tracing
1402 * is fundamentally NMI-unsafe. (we cannot change the soft and
1403 * hard flags at once, atomically)
1406 /* ebx: no swapgs flag */
1407 ENTRY(paranoid_exit)
1409 DISABLE_INTERRUPTS(CLBR_NONE)
1411 testl %ebx,%ebx /* swapgs needed? */
1412 jnz paranoid_restore
1414 jnz paranoid_userspace
1425 GET_THREAD_INFO(%rcx)
1426 movl TI_flags(%rcx),%ebx
1427 andl $_TIF_WORK_MASK,%ebx
1429 movq %rsp,%rdi /* &pt_regs */
1431 movq %rax,%rsp /* switch stack for scheduling */
1432 testl $_TIF_NEED_RESCHED,%ebx
1433 jnz paranoid_schedule
1434 movl %ebx,%edx /* arg3: thread flags */
1436 ENABLE_INTERRUPTS(CLBR_NONE)
1437 xorl %esi,%esi /* arg2: oldset */
1438 movq %rsp,%rdi /* arg1: &pt_regs */
1439 call do_notify_resume
1440 DISABLE_INTERRUPTS(CLBR_NONE)
1442 jmp paranoid_userspace
1445 ENABLE_INTERRUPTS(CLBR_ANY)
1447 DISABLE_INTERRUPTS(CLBR_ANY)
1449 jmp paranoid_userspace
1454 * Exception entry point. This expects an error code/orig_rax on the stack.
1455 * returns in "no swapgs flag" in %ebx.
1459 CFI_ADJUST_CFA_OFFSET 15*8
1460 /* oldrax contains error code */
1479 je error_kernelspace
1488 * There are two places in the kernel that can potentially fault with
1489 * usergs. Handle them here. The exception handlers after iret run with
1490 * kernel gs again, so don't set the user space flag. B stepping K8s
1491 * sometimes report an truncated RIP for IRET exceptions returning to
1492 * compat mode. Check for these here too.
1496 leaq irq_return(%rip),%rcx
1497 cmpq %rcx,RIP+8(%rsp)
1499 movl %ecx,%ecx /* zero extend */
1500 cmpq %rcx,RIP+8(%rsp)
1502 cmpq $gs_change,RIP+8(%rsp)
1508 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1513 DISABLE_INTERRUPTS(CLBR_NONE)
1515 GET_THREAD_INFO(%rcx)
1518 LOCKDEP_SYS_EXIT_IRQ
1519 movl TI_flags(%rcx),%edx
1520 movl $_TIF_WORK_MASK,%edi
1528 /* runs on exception stack */
1531 PARAVIRT_ADJUST_EXCEPTION_FRAME
1534 CFI_ADJUST_CFA_OFFSET 15*8
1537 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1541 #ifdef CONFIG_TRACE_IRQFLAGS
1542 /* paranoidexit; without TRACE_IRQS_OFF */
1543 /* ebx: no swapgs flag */
1544 DISABLE_INTERRUPTS(CLBR_NONE)
1545 testl %ebx,%ebx /* swapgs needed? */
1555 GET_THREAD_INFO(%rcx)
1556 movl TI_flags(%rcx),%ebx
1557 andl $_TIF_WORK_MASK,%ebx
1559 movq %rsp,%rdi /* &pt_regs */
1561 movq %rax,%rsp /* switch stack for scheduling */
1562 testl $_TIF_NEED_RESCHED,%ebx
1564 movl %ebx,%edx /* arg3: thread flags */
1565 ENABLE_INTERRUPTS(CLBR_NONE)
1566 xorl %esi,%esi /* arg2: oldset */
1567 movq %rsp,%rdi /* arg1: &pt_regs */
1568 call do_notify_resume
1569 DISABLE_INTERRUPTS(CLBR_NONE)
1572 ENABLE_INTERRUPTS(CLBR_ANY)
1574 DISABLE_INTERRUPTS(CLBR_ANY)
1583 ENTRY(ignore_sysret)
1591 * End of kprobes section