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 */
390 movl $MSR_GS_BASE,%ecx
393 js 1f /* negative -> in kernel */
401 * A newly forked process directly context switches into this address.
403 * rdi: prev task we switched from
408 LOCK ; btr $TIF_FORK,TI_flags(%r8)
410 push kernel_eflags(%rip)
411 CFI_ADJUST_CFA_OFFSET 8
412 popf # reset kernel eflags
413 CFI_ADJUST_CFA_OFFSET -8
415 call schedule_tail # rdi: 'prev' task parameter
417 GET_THREAD_INFO(%rcx)
422 testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
423 je int_ret_from_sys_call
425 testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
426 jnz int_ret_from_sys_call
428 RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
429 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
989 apicinterrupt INVALIDATE_TLB_VECTOR_START+0 \
990 invalidate_interrupt0 smp_invalidate_interrupt
991 apicinterrupt INVALIDATE_TLB_VECTOR_START+1 \
992 invalidate_interrupt1 smp_invalidate_interrupt
993 apicinterrupt INVALIDATE_TLB_VECTOR_START+2 \
994 invalidate_interrupt2 smp_invalidate_interrupt
995 apicinterrupt INVALIDATE_TLB_VECTOR_START+3 \
996 invalidate_interrupt3 smp_invalidate_interrupt
997 apicinterrupt INVALIDATE_TLB_VECTOR_START+4 \
998 invalidate_interrupt4 smp_invalidate_interrupt
999 apicinterrupt INVALIDATE_TLB_VECTOR_START+5 \
1000 invalidate_interrupt5 smp_invalidate_interrupt
1001 apicinterrupt INVALIDATE_TLB_VECTOR_START+6 \
1002 invalidate_interrupt6 smp_invalidate_interrupt
1003 apicinterrupt INVALIDATE_TLB_VECTOR_START+7 \
1004 invalidate_interrupt7 smp_invalidate_interrupt
1007 apicinterrupt THRESHOLD_APIC_VECTOR \
1008 threshold_interrupt mce_threshold_interrupt
1009 apicinterrupt THERMAL_APIC_VECTOR \
1010 thermal_interrupt smp_thermal_interrupt
1013 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
1014 call_function_single_interrupt smp_call_function_single_interrupt
1015 apicinterrupt CALL_FUNCTION_VECTOR \
1016 call_function_interrupt smp_call_function_interrupt
1017 apicinterrupt RESCHEDULE_VECTOR \
1018 reschedule_interrupt smp_reschedule_interrupt
1021 apicinterrupt ERROR_APIC_VECTOR \
1022 error_interrupt smp_error_interrupt
1023 apicinterrupt SPURIOUS_APIC_VECTOR \
1024 spurious_interrupt smp_spurious_interrupt
1027 * Exception entry points.
1029 .macro zeroentry sym do_sym
1032 PARAVIRT_ADJUST_EXCEPTION_FRAME
1033 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1035 CFI_ADJUST_CFA_OFFSET 15*8
1038 movq %rsp,%rdi /* pt_regs pointer */
1039 xorl %esi,%esi /* no error code */
1041 jmp error_exit /* %ebx: no swapgs flag */
1046 .macro paranoidzeroentry sym do_sym
1049 PARAVIRT_ADJUST_EXCEPTION_FRAME
1050 pushq $-1 /* ORIG_RAX: no syscall to restart */
1051 CFI_ADJUST_CFA_OFFSET 8
1055 movq %rsp,%rdi /* pt_regs pointer */
1056 xorl %esi,%esi /* no error code */
1058 jmp paranoid_exit /* %ebx: no swapgs flag */
1063 .macro paranoidzeroentry_ist sym do_sym ist
1066 PARAVIRT_ADJUST_EXCEPTION_FRAME
1067 pushq $-1 /* ORIG_RAX: no syscall to restart */
1068 CFI_ADJUST_CFA_OFFSET 8
1072 movq %rsp,%rdi /* pt_regs pointer */
1073 xorl %esi,%esi /* no error code */
1074 PER_CPU(init_tss, %rbp)
1075 subq $EXCEPTION_STKSZ, TSS_ist + (\ist - 1) * 8(%rbp)
1077 addq $EXCEPTION_STKSZ, TSS_ist + (\ist - 1) * 8(%rbp)
1078 jmp paranoid_exit /* %ebx: no swapgs flag */
1083 .macro errorentry sym do_sym
1086 PARAVIRT_ADJUST_EXCEPTION_FRAME
1088 CFI_ADJUST_CFA_OFFSET 15*8
1091 movq %rsp,%rdi /* pt_regs pointer */
1092 movq ORIG_RAX(%rsp),%rsi /* get error code */
1093 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1095 jmp error_exit /* %ebx: no swapgs flag */
1100 /* error code is on the stack already */
1101 .macro paranoiderrorentry sym do_sym
1104 PARAVIRT_ADJUST_EXCEPTION_FRAME
1106 CFI_ADJUST_CFA_OFFSET 15*8
1110 movq %rsp,%rdi /* pt_regs pointer */
1111 movq ORIG_RAX(%rsp),%rsi /* get error code */
1112 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1114 jmp paranoid_exit /* %ebx: no swapgs flag */
1119 zeroentry divide_error do_divide_error
1120 zeroentry overflow do_overflow
1121 zeroentry bounds do_bounds
1122 zeroentry invalid_op do_invalid_op
1123 zeroentry device_not_available do_device_not_available
1124 paranoiderrorentry double_fault do_double_fault
1125 zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1126 errorentry invalid_TSS do_invalid_TSS
1127 errorentry segment_not_present do_segment_not_present
1128 zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1129 zeroentry coprocessor_error do_coprocessor_error
1130 errorentry alignment_check do_alignment_check
1131 zeroentry simd_coprocessor_error do_simd_coprocessor_error
1133 /* Reload gs selector with exception handling */
1134 /* edi: new selector */
1135 ENTRY(native_load_gs_index)
1138 CFI_ADJUST_CFA_OFFSET 8
1139 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1143 2: mfence /* workaround */
1146 CFI_ADJUST_CFA_OFFSET -8
1149 END(native_load_gs_index)
1151 .section __ex_table,"a"
1153 .quad gs_change,bad_gs
1155 .section .fixup,"ax"
1156 /* running with kernelgs */
1158 SWAPGS /* switch back to user gs */
1165 * Create a kernel thread.
1167 * C extern interface:
1168 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1170 * asm input arguments:
1171 * rdi: fn, rsi: arg, rdx: flags
1173 ENTRY(kernel_thread)
1175 FAKE_STACK_FRAME $child_rip
1178 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1180 orq kernel_thread_flags(%rip),%rdi
1193 * It isn't worth to check for reschedule here,
1194 * so internally to the x86_64 port you can rely on kernel_thread()
1195 * not to reschedule the child before returning, this avoids the need
1196 * of hacks for example to fork off the per-CPU idle tasks.
1197 * [Hopefully no generic code relies on the reschedule -AK]
1206 pushq $0 # fake return address
1209 * Here we are in the child and the registers are set as they were
1210 * at kernel_thread() invocation in the parent.
1218 ud2 # padding for call trace
1223 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1225 * C extern interface:
1226 * extern long execve(char *name, char **argv, char **envp)
1228 * asm input arguments:
1229 * rdi: name, rsi: argv, rdx: envp
1231 * We want to fallback into:
1232 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
1234 * do_sys_execve asm fallback arguments:
1235 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1237 ENTRY(kernel_execve)
1243 movq %rax, RAX(%rsp)
1246 je int_ret_from_sys_call
1253 /* Call softirq on interrupt stack. Interrupts are off. */
1257 CFI_ADJUST_CFA_OFFSET 8
1258 CFI_REL_OFFSET rbp,0
1260 CFI_DEF_CFA_REGISTER rbp
1261 incl PER_CPU_VAR(irq_count)
1262 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1263 push %rbp # backlink for old unwinder
1266 CFI_DEF_CFA_REGISTER rsp
1267 CFI_ADJUST_CFA_OFFSET -8
1268 decl PER_CPU_VAR(irq_count)
1274 zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
1277 * A note on the "critical region" in our callback handler.
1278 * We want to avoid stacking callback handlers due to events occurring
1279 * during handling of the last event. To do this, we keep events disabled
1280 * until we've done all processing. HOWEVER, we must enable events before
1281 * popping the stack frame (can't be done atomically) and so it would still
1282 * be possible to get enough handler activations to overflow the stack.
1283 * Although unlikely, bugs of that kind are hard to track down, so we'd
1284 * like to avoid the possibility.
1285 * So, on entry to the handler we detect whether we interrupted an
1286 * existing activation in its critical region -- if so, we pop the current
1287 * activation and restart the handler using the previous one.
1289 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1292 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1293 * see the correct pointer to the pt_regs
1295 movq %rdi, %rsp # we don't return, adjust the stack frame
1298 11: incl PER_CPU_VAR(irq_count)
1300 CFI_DEF_CFA_REGISTER rbp
1301 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1302 pushq %rbp # backlink for old unwinder
1303 call xen_evtchn_do_upcall
1305 CFI_DEF_CFA_REGISTER rsp
1306 decl PER_CPU_VAR(irq_count)
1309 END(do_hypervisor_callback)
1312 * Hypervisor uses this for application faults while it executes.
1313 * We get here for two reasons:
1314 * 1. Fault while reloading DS, ES, FS or GS
1315 * 2. Fault while executing IRET
1316 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1317 * registers that could be reloaded and zeroed the others.
1318 * Category 2 we fix up by killing the current process. We cannot use the
1319 * normal Linux return path in this case because if we use the IRET hypercall
1320 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1321 * We distinguish between categories by comparing each saved segment register
1322 * with its current contents: any discrepancy means we in category 1.
1324 ENTRY(xen_failsafe_callback)
1326 /*CFI_REL_OFFSET gs,GS*/
1327 /*CFI_REL_OFFSET fs,FS*/
1328 /*CFI_REL_OFFSET es,ES*/
1329 /*CFI_REL_OFFSET ds,DS*/
1330 CFI_REL_OFFSET r11,8
1331 CFI_REL_OFFSET rcx,0
1345 /* All segments match their saved values => Category 2 (Bad IRET). */
1351 CFI_ADJUST_CFA_OFFSET -0x30
1352 pushq_cfi $0 /* RIP */
1355 jmp general_protection
1357 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1363 CFI_ADJUST_CFA_OFFSET -0x30
1368 END(xen_failsafe_callback)
1370 #endif /* CONFIG_XEN */
1373 * Some functions should be protected against kprobes
1375 .pushsection .kprobes.text, "ax"
1377 paranoidzeroentry_ist debug do_debug DEBUG_STACK
1378 paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1379 paranoiderrorentry stack_segment do_stack_segment
1380 errorentry general_protection do_general_protection
1381 errorentry page_fault do_page_fault
1382 #ifdef CONFIG_X86_MCE
1383 paranoidzeroentry machine_check do_machine_check
1387 * "Paranoid" exit path from exception stack.
1388 * Paranoid because this is used by NMIs and cannot take
1389 * any kernel state for granted.
1390 * We don't do kernel preemption checks here, because only
1391 * NMI should be common and it does not enable IRQs and
1392 * cannot get reschedule ticks.
1394 * "trace" is 0 for the NMI handler only, because irq-tracing
1395 * is fundamentally NMI-unsafe. (we cannot change the soft and
1396 * hard flags at once, atomically)
1399 /* ebx: no swapgs flag */
1400 ENTRY(paranoid_exit)
1402 DISABLE_INTERRUPTS(CLBR_NONE)
1404 testl %ebx,%ebx /* swapgs needed? */
1405 jnz paranoid_restore
1407 jnz paranoid_userspace
1415 GET_THREAD_INFO(%rcx)
1416 movl TI_flags(%rcx),%ebx
1417 andl $_TIF_WORK_MASK,%ebx
1419 movq %rsp,%rdi /* &pt_regs */
1421 movq %rax,%rsp /* switch stack for scheduling */
1422 testl $_TIF_NEED_RESCHED,%ebx
1423 jnz paranoid_schedule
1424 movl %ebx,%edx /* arg3: thread flags */
1426 ENABLE_INTERRUPTS(CLBR_NONE)
1427 xorl %esi,%esi /* arg2: oldset */
1428 movq %rsp,%rdi /* arg1: &pt_regs */
1429 call do_notify_resume
1430 DISABLE_INTERRUPTS(CLBR_NONE)
1432 jmp paranoid_userspace
1435 ENABLE_INTERRUPTS(CLBR_ANY)
1437 DISABLE_INTERRUPTS(CLBR_ANY)
1439 jmp paranoid_userspace
1444 * Exception entry point. This expects an error code/orig_rax on the stack.
1445 * returns in "no swapgs flag" in %ebx.
1449 CFI_ADJUST_CFA_OFFSET 15*8
1450 /* oldrax contains error code */
1469 je error_kernelspace
1478 * There are two places in the kernel that can potentially fault with
1479 * usergs. Handle them here. The exception handlers after iret run with
1480 * kernel gs again, so don't set the user space flag. B stepping K8s
1481 * sometimes report an truncated RIP for IRET exceptions returning to
1482 * compat mode. Check for these here too.
1486 leaq irq_return(%rip),%rcx
1487 cmpq %rcx,RIP+8(%rsp)
1489 movl %ecx,%ecx /* zero extend */
1490 cmpq %rcx,RIP+8(%rsp)
1492 cmpq $gs_change,RIP+8(%rsp)
1498 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1503 DISABLE_INTERRUPTS(CLBR_NONE)
1505 GET_THREAD_INFO(%rcx)
1508 LOCKDEP_SYS_EXIT_IRQ
1509 movl TI_flags(%rcx),%edx
1510 movl $_TIF_WORK_MASK,%edi
1518 /* runs on exception stack */
1521 PARAVIRT_ADJUST_EXCEPTION_FRAME
1524 CFI_ADJUST_CFA_OFFSET 15*8
1527 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1531 #ifdef CONFIG_TRACE_IRQFLAGS
1532 /* paranoidexit; without TRACE_IRQS_OFF */
1533 /* ebx: no swapgs flag */
1534 DISABLE_INTERRUPTS(CLBR_NONE)
1535 testl %ebx,%ebx /* swapgs needed? */
1545 GET_THREAD_INFO(%rcx)
1546 movl TI_flags(%rcx),%ebx
1547 andl $_TIF_WORK_MASK,%ebx
1549 movq %rsp,%rdi /* &pt_regs */
1551 movq %rax,%rsp /* switch stack for scheduling */
1552 testl $_TIF_NEED_RESCHED,%ebx
1554 movl %ebx,%edx /* arg3: thread flags */
1555 ENABLE_INTERRUPTS(CLBR_NONE)
1556 xorl %esi,%esi /* arg2: oldset */
1557 movq %rsp,%rdi /* arg1: &pt_regs */
1558 call do_notify_resume
1559 DISABLE_INTERRUPTS(CLBR_NONE)
1562 ENABLE_INTERRUPTS(CLBR_ANY)
1564 DISABLE_INTERRUPTS(CLBR_ANY)
1573 ENTRY(ignore_sysret)
1581 * End of kprobes section