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
65 #ifdef CONFIG_DYNAMIC_FTRACE
78 subq $MCOUNT_INSN_SIZE, %rdi
98 /* taken from glibc */
108 movq 0x38(%rsp), %rdi
110 subq $MCOUNT_INSN_SIZE, %rdi
130 #else /* ! CONFIG_DYNAMIC_FTRACE */
132 cmpq $ftrace_stub, ftrace_trace_function
139 /* taken from glibc */
149 movq 0x38(%rsp), %rdi
151 subq $MCOUNT_INSN_SIZE, %rdi
153 call *ftrace_trace_function
166 #endif /* CONFIG_DYNAMIC_FTRACE */
167 #endif /* CONFIG_FTRACE */
169 #ifndef CONFIG_PREEMPT
170 #define retint_kernel retint_restore_args
173 #ifdef CONFIG_PARAVIRT
174 ENTRY(native_usergs_sysret64)
177 #endif /* CONFIG_PARAVIRT */
180 .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
181 #ifdef CONFIG_TRACE_IRQFLAGS
182 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
190 * C code is not supposed to know about undefined top of stack. Every time
191 * a C function with an pt_regs argument is called from the SYSCALL based
192 * fast path FIXUP_TOP_OF_STACK is needed.
193 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
197 /* %rsp:at FRAMEEND */
198 .macro FIXUP_TOP_OF_STACK tmp
199 movq %gs:pda_oldrsp,\tmp
201 movq $__USER_DS,SS(%rsp)
202 movq $__USER_CS,CS(%rsp)
204 movq R11(%rsp),\tmp /* get eflags */
205 movq \tmp,EFLAGS(%rsp)
208 .macro RESTORE_TOP_OF_STACK tmp,offset=0
209 movq RSP-\offset(%rsp),\tmp
210 movq \tmp,%gs:pda_oldrsp
211 movq EFLAGS-\offset(%rsp),\tmp
212 movq \tmp,R11-\offset(%rsp)
215 .macro FAKE_STACK_FRAME child_rip
216 /* push in order ss, rsp, eflags, cs, rip */
218 pushq $__KERNEL_DS /* ss */
219 CFI_ADJUST_CFA_OFFSET 8
220 /*CFI_REL_OFFSET ss,0*/
222 CFI_ADJUST_CFA_OFFSET 8
224 pushq $(1<<9) /* eflags - interrupts on */
225 CFI_ADJUST_CFA_OFFSET 8
226 /*CFI_REL_OFFSET rflags,0*/
227 pushq $__KERNEL_CS /* cs */
228 CFI_ADJUST_CFA_OFFSET 8
229 /*CFI_REL_OFFSET cs,0*/
230 pushq \child_rip /* rip */
231 CFI_ADJUST_CFA_OFFSET 8
233 pushq %rax /* orig rax */
234 CFI_ADJUST_CFA_OFFSET 8
237 .macro UNFAKE_STACK_FRAME
239 CFI_ADJUST_CFA_OFFSET -(6*8)
242 .macro CFI_DEFAULT_STACK start=1
248 CFI_DEF_CFA_OFFSET SS+8
250 CFI_REL_OFFSET r15,R15
251 CFI_REL_OFFSET r14,R14
252 CFI_REL_OFFSET r13,R13
253 CFI_REL_OFFSET r12,R12
254 CFI_REL_OFFSET rbp,RBP
255 CFI_REL_OFFSET rbx,RBX
256 CFI_REL_OFFSET r11,R11
257 CFI_REL_OFFSET r10,R10
260 CFI_REL_OFFSET rax,RAX
261 CFI_REL_OFFSET rcx,RCX
262 CFI_REL_OFFSET rdx,RDX
263 CFI_REL_OFFSET rsi,RSI
264 CFI_REL_OFFSET rdi,RDI
265 CFI_REL_OFFSET rip,RIP
266 /*CFI_REL_OFFSET cs,CS*/
267 /*CFI_REL_OFFSET rflags,EFLAGS*/
268 CFI_REL_OFFSET rsp,RSP
269 /*CFI_REL_OFFSET ss,SS*/
272 * A newly forked process directly context switches into this.
277 push kernel_eflags(%rip)
278 CFI_ADJUST_CFA_OFFSET 4
279 popf # reset kernel eflags
280 CFI_ADJUST_CFA_OFFSET -4
282 GET_THREAD_INFO(%rcx)
283 testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
287 testl $3,CS-ARGOFFSET(%rsp) # from kernel_thread?
288 je int_ret_from_sys_call
289 testl $_TIF_IA32,TI_flags(%rcx)
290 jnz int_ret_from_sys_call
291 RESTORE_TOP_OF_STACK %rdi,ARGOFFSET
292 jmp ret_from_sys_call
295 call syscall_trace_leave
296 GET_THREAD_INFO(%rcx)
302 * System call entry. Upto 6 arguments in registers are supported.
304 * SYSCALL does not save anything on the stack and does not change the
310 * rax system call number
312 * rcx return address for syscall/sysret, C arg3
315 * r10 arg3 (--> moved to rcx for C)
318 * r11 eflags for syscall/sysret, temporary for C
319 * r12-r15,rbp,rbx saved by C code, not touched.
321 * Interrupts are off on entry.
322 * Only called from user space.
324 * XXX if we had a free scratch register we could save the RSP into the stack frame
325 * and report it properly in ps. Unfortunately we haven't.
327 * When user can change the frames always force IRET. That is because
328 * it deals with uncanonical addresses better. SYSRET has trouble
329 * with them due to bugs in both AMD and Intel CPUs.
335 CFI_DEF_CFA rsp,PDA_STACKOFFSET
337 /*CFI_REGISTER rflags,r11*/
340 * A hypervisor implementation might want to use a label
341 * after the swapgs, so that it can do the swapgs
342 * for the guest and jump here on syscall.
344 ENTRY(system_call_after_swapgs)
346 movq %rsp,%gs:pda_oldrsp
347 movq %gs:pda_kernelstack,%rsp
349 * No need to follow this irqs off/on section - it's straight
352 ENABLE_INTERRUPTS(CLBR_NONE)
354 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
355 movq %rcx,RIP-ARGOFFSET(%rsp)
356 CFI_REL_OFFSET rip,RIP-ARGOFFSET
357 GET_THREAD_INFO(%rcx)
358 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
360 system_call_fastpath:
361 cmpq $__NR_syscall_max,%rax
364 call *sys_call_table(,%rax,8) # XXX: rip relative
365 movq %rax,RAX-ARGOFFSET(%rsp)
367 * Syscall return path ending with SYSRET (fast path)
368 * Has incomplete stack frame and undefined top of stack.
371 movl $_TIF_ALLWORK_MASK,%edi
375 GET_THREAD_INFO(%rcx)
376 DISABLE_INTERRUPTS(CLBR_NONE)
378 movl TI_flags(%rcx),%edx
383 * sysretq will re-enable interrupts:
386 movq RIP-ARGOFFSET(%rsp),%rcx
388 RESTORE_ARGS 0,-ARG_SKIP,1
389 /*CFI_REGISTER rflags,r11*/
390 movq %gs:pda_oldrsp, %rsp
394 /* Handle reschedules */
395 /* edx: work, edi: workmask */
397 bt $TIF_NEED_RESCHED,%edx
400 ENABLE_INTERRUPTS(CLBR_NONE)
402 CFI_ADJUST_CFA_OFFSET 8
405 CFI_ADJUST_CFA_OFFSET -8
408 /* Handle a signal */
411 ENABLE_INTERRUPTS(CLBR_NONE)
412 #ifdef CONFIG_AUDITSYSCALL
413 bt $TIF_SYSCALL_AUDIT,%edx
416 /* edx: work flags (arg3) */
417 leaq do_notify_resume(%rip),%rax
418 leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1
419 xorl %esi,%esi # oldset -> arg2
420 call ptregscall_common
421 movl $_TIF_WORK_MASK,%edi
422 /* Use IRET because user could have changed frame. This
423 works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
424 DISABLE_INTERRUPTS(CLBR_NONE)
429 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
430 jmp ret_from_sys_call
432 #ifdef CONFIG_AUDITSYSCALL
434 * Fast path for syscall audit without full syscall trace.
435 * We just call audit_syscall_entry() directly, and then
436 * jump back to the normal fast path.
439 movq %r10,%r9 /* 6th arg: 4th syscall arg */
440 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
441 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
442 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
443 movq %rax,%rsi /* 2nd arg: syscall number */
444 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
445 call audit_syscall_entry
446 LOAD_ARGS 0 /* reload call-clobbered registers */
447 jmp system_call_fastpath
450 * Return fast path for syscall audit. Call audit_syscall_exit()
451 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
455 movq %rax,%rsi /* second arg, syscall return value */
456 cmpq $0,%rax /* is it < 0? */
457 setl %al /* 1 if so, 0 if not */
458 movzbl %al,%edi /* zero-extend that into %edi */
459 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
460 call audit_syscall_exit
461 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
463 #endif /* CONFIG_AUDITSYSCALL */
465 /* Do syscall tracing */
467 #ifdef CONFIG_AUDITSYSCALL
468 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
472 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
473 FIXUP_TOP_OF_STACK %rdi
475 call syscall_trace_enter
477 * Reload arg registers from stack in case ptrace changed them.
478 * We don't reload %rax because syscall_trace_enter() returned
479 * the value it wants us to use in the table lookup.
481 LOAD_ARGS ARGOFFSET, 1
483 cmpq $__NR_syscall_max,%rax
484 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
485 movq %r10,%rcx /* fixup for C */
486 call *sys_call_table(,%rax,8)
487 movq %rax,RAX-ARGOFFSET(%rsp)
488 /* Use IRET because user could have changed frame */
491 * Syscall return path ending with IRET.
492 * Has correct top of stack, but partial stack frame.
494 .globl int_ret_from_sys_call
495 int_ret_from_sys_call:
496 DISABLE_INTERRUPTS(CLBR_NONE)
498 testl $3,CS-ARGOFFSET(%rsp)
499 je retint_restore_args
500 movl $_TIF_ALLWORK_MASK,%edi
501 /* edi: mask to check */
504 GET_THREAD_INFO(%rcx)
505 movl TI_flags(%rcx),%edx
508 andl $~TS_COMPAT,TI_status(%rcx)
511 /* Either reschedule or signal or syscall exit tracking needed. */
512 /* First do a reschedule test. */
513 /* edx: work, edi: workmask */
515 bt $TIF_NEED_RESCHED,%edx
518 ENABLE_INTERRUPTS(CLBR_NONE)
520 CFI_ADJUST_CFA_OFFSET 8
523 CFI_ADJUST_CFA_OFFSET -8
524 DISABLE_INTERRUPTS(CLBR_NONE)
528 /* handle signals and tracing -- both require a full stack frame */
531 ENABLE_INTERRUPTS(CLBR_NONE)
533 /* Check for syscall exit trace */
534 testl $_TIF_WORK_SYSCALL_EXIT,%edx
537 CFI_ADJUST_CFA_OFFSET 8
538 leaq 8(%rsp),%rdi # &ptregs -> arg1
539 call syscall_trace_leave
541 CFI_ADJUST_CFA_OFFSET -8
542 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
546 testl $_TIF_DO_NOTIFY_MASK,%edx
548 movq %rsp,%rdi # &ptregs -> arg1
549 xorl %esi,%esi # oldset -> arg2
550 call do_notify_resume
551 1: movl $_TIF_WORK_MASK,%edi
554 DISABLE_INTERRUPTS(CLBR_NONE)
561 * Certain special system calls that need to save a complete full stack frame.
564 .macro PTREGSCALL label,func,arg
567 leaq \func(%rip),%rax
568 leaq -ARGOFFSET+8(%rsp),\arg /* 8 for return address */
569 jmp ptregscall_common
575 PTREGSCALL stub_clone, sys_clone, %r8
576 PTREGSCALL stub_fork, sys_fork, %rdi
577 PTREGSCALL stub_vfork, sys_vfork, %rdi
578 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
579 PTREGSCALL stub_iopl, sys_iopl, %rsi
581 ENTRY(ptregscall_common)
583 CFI_ADJUST_CFA_OFFSET -8
584 CFI_REGISTER rip, r11
587 CFI_REGISTER rip, r15
588 FIXUP_TOP_OF_STACK %r11
590 RESTORE_TOP_OF_STACK %r11
592 CFI_REGISTER rip, r11
595 CFI_ADJUST_CFA_OFFSET 8
596 CFI_REL_OFFSET rip, 0
599 END(ptregscall_common)
604 CFI_ADJUST_CFA_OFFSET -8
605 CFI_REGISTER rip, r11
607 FIXUP_TOP_OF_STACK %r11
610 RESTORE_TOP_OF_STACK %r11
613 jmp int_ret_from_sys_call
618 * sigreturn is special because it needs to restore all registers on return.
619 * This cannot be done with SYSRET, so use the IRET return path instead.
621 ENTRY(stub_rt_sigreturn)
624 CFI_ADJUST_CFA_OFFSET -8
627 FIXUP_TOP_OF_STACK %r11
628 call sys_rt_sigreturn
629 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
631 jmp int_ret_from_sys_call
633 END(stub_rt_sigreturn)
636 * initial frame state for interrupts and exceptions
641 CFI_DEF_CFA rsp,SS+8-\ref
642 /*CFI_REL_OFFSET ss,SS-\ref*/
643 CFI_REL_OFFSET rsp,RSP-\ref
644 /*CFI_REL_OFFSET rflags,EFLAGS-\ref*/
645 /*CFI_REL_OFFSET cs,CS-\ref*/
646 CFI_REL_OFFSET rip,RIP-\ref
649 /* initial frame state for interrupts (and exceptions without error code) */
650 #define INTR_FRAME _frame RIP
651 /* initial frame state for exceptions with error code (and interrupts with
652 vector already pushed) */
653 #define XCPT_FRAME _frame ORIG_RAX
656 * Interrupt entry/exit.
658 * Interrupt entry points save only callee clobbered registers in fast path.
660 * Entry runs with interrupts off.
663 /* 0(%rsp): interrupt number */
664 .macro interrupt func
667 leaq -ARGOFFSET(%rsp),%rdi # arg1 for handler
669 CFI_ADJUST_CFA_OFFSET 8
670 CFI_REL_OFFSET rbp, 0
672 CFI_DEF_CFA_REGISTER rbp
676 /* irqcount is used to check if a CPU is already on an interrupt
677 stack or not. While this is essentially redundant with preempt_count
678 it is a little cheaper to use a separate counter in the PDA
679 (short of moving irq_enter into assembly, which would be too
681 1: incl %gs:pda_irqcount
682 cmoveq %gs:pda_irqstackptr,%rsp
683 push %rbp # backlink for old unwinder
685 * We entered an interrupt context - irqs are off:
691 ENTRY(common_interrupt)
694 /* 0(%rsp): oldrsp-ARGOFFSET */
696 DISABLE_INTERRUPTS(CLBR_NONE)
698 decl %gs:pda_irqcount
700 CFI_DEF_CFA_REGISTER rsp
701 CFI_ADJUST_CFA_OFFSET -8
703 GET_THREAD_INFO(%rcx)
704 testl $3,CS-ARGOFFSET(%rsp)
707 /* Interrupt came from user space */
709 * Has a correct top of stack, but a partial stack frame
710 * %rcx: thread info. Interrupts off.
712 retint_with_reschedule:
713 movl $_TIF_WORK_MASK,%edi
716 movl TI_flags(%rcx),%edx
721 retint_swapgs: /* return to user-space */
723 * The iretq could re-enable interrupts:
725 DISABLE_INTERRUPTS(CLBR_ANY)
730 retint_restore_args: /* return to kernel space */
731 DISABLE_INTERRUPTS(CLBR_ANY)
733 * The iretq could re-enable interrupts:
742 .section __ex_table, "a"
743 .quad irq_return, bad_iret
746 #ifdef CONFIG_PARAVIRT
750 .section __ex_table,"a"
751 .quad native_iret, bad_iret
758 * The iret traps when the %cs or %ss being restored is bogus.
759 * We've lost the original trap vector and error code.
760 * #GPF is the most likely one to get for an invalid selector.
761 * So pretend we completed the iret and took the #GPF in user mode.
763 * We are now running with the kernel GS after exception recovery.
764 * But error_entry expects us to have user GS to match the user %cs,
770 jmp general_protection
774 /* edi: workmask, edx: work */
777 bt $TIF_NEED_RESCHED,%edx
780 ENABLE_INTERRUPTS(CLBR_NONE)
782 CFI_ADJUST_CFA_OFFSET 8
785 CFI_ADJUST_CFA_OFFSET -8
786 GET_THREAD_INFO(%rcx)
787 DISABLE_INTERRUPTS(CLBR_NONE)
792 testl $_TIF_DO_NOTIFY_MASK,%edx
795 ENABLE_INTERRUPTS(CLBR_NONE)
797 movq $-1,ORIG_RAX(%rsp)
798 xorl %esi,%esi # oldset
799 movq %rsp,%rdi # &pt_regs
800 call do_notify_resume
802 DISABLE_INTERRUPTS(CLBR_NONE)
804 GET_THREAD_INFO(%rcx)
805 jmp retint_with_reschedule
807 #ifdef CONFIG_PREEMPT
808 /* Returning to kernel space. Check if we need preemption */
809 /* rcx: threadinfo. interrupts off. */
811 cmpl $0,TI_preempt_count(%rcx)
812 jnz retint_restore_args
813 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
814 jnc retint_restore_args
815 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
816 jnc retint_restore_args
817 call preempt_schedule_irq
822 END(common_interrupt)
827 .macro apicinterrupt num,func
830 CFI_ADJUST_CFA_OFFSET 8
836 ENTRY(thermal_interrupt)
837 apicinterrupt THERMAL_APIC_VECTOR,smp_thermal_interrupt
838 END(thermal_interrupt)
840 ENTRY(threshold_interrupt)
841 apicinterrupt THRESHOLD_APIC_VECTOR,mce_threshold_interrupt
842 END(threshold_interrupt)
845 ENTRY(reschedule_interrupt)
846 apicinterrupt RESCHEDULE_VECTOR,smp_reschedule_interrupt
847 END(reschedule_interrupt)
849 .macro INVALIDATE_ENTRY num
850 ENTRY(invalidate_interrupt\num)
851 apicinterrupt INVALIDATE_TLB_VECTOR_START+\num,smp_invalidate_interrupt
852 END(invalidate_interrupt\num)
864 ENTRY(call_function_interrupt)
865 apicinterrupt CALL_FUNCTION_VECTOR,smp_call_function_interrupt
866 END(call_function_interrupt)
867 ENTRY(call_function_single_interrupt)
868 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR,smp_call_function_single_interrupt
869 END(call_function_single_interrupt)
870 ENTRY(irq_move_cleanup_interrupt)
871 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR,smp_irq_move_cleanup_interrupt
872 END(irq_move_cleanup_interrupt)
875 ENTRY(apic_timer_interrupt)
876 apicinterrupt LOCAL_TIMER_VECTOR,smp_apic_timer_interrupt
877 END(apic_timer_interrupt)
879 ENTRY(uv_bau_message_intr1)
880 apicinterrupt 220,uv_bau_message_interrupt
881 END(uv_bau_message_intr1)
883 ENTRY(error_interrupt)
884 apicinterrupt ERROR_APIC_VECTOR,smp_error_interrupt
887 ENTRY(spurious_interrupt)
888 apicinterrupt SPURIOUS_APIC_VECTOR,smp_spurious_interrupt
889 END(spurious_interrupt)
892 * Exception entry points.
896 PARAVIRT_ADJUST_EXCEPTION_FRAME
897 pushq $0 /* push error code/oldrax */
898 CFI_ADJUST_CFA_OFFSET 8
899 pushq %rax /* push real oldrax to the rdi slot */
900 CFI_ADJUST_CFA_OFFSET 8
907 .macro errorentry sym
909 PARAVIRT_ADJUST_EXCEPTION_FRAME
911 CFI_ADJUST_CFA_OFFSET 8
918 /* error code is on the stack already */
919 /* handle NMI like exceptions that can happen everywhere */
920 .macro paranoidentry sym, ist=0, irqtrace=1
924 movl $MSR_GS_BASE,%ecx
932 movq %gs:pda_data_offset, %rbp
935 movq ORIG_RAX(%rsp),%rsi
936 movq $-1,ORIG_RAX(%rsp)
938 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
942 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
944 DISABLE_INTERRUPTS(CLBR_NONE)
951 * "Paranoid" exit path from exception stack.
952 * Paranoid because this is used by NMIs and cannot take
953 * any kernel state for granted.
954 * We don't do kernel preemption checks here, because only
955 * NMI should be common and it does not enable IRQs and
956 * cannot get reschedule ticks.
958 * "trace" is 0 for the NMI handler only, because irq-tracing
959 * is fundamentally NMI-unsafe. (we cannot change the soft and
960 * hard flags at once, atomically)
962 .macro paranoidexit trace=1
963 /* ebx: no swapgs flag */
965 testl %ebx,%ebx /* swapgs needed? */
966 jnz paranoid_restore\trace
968 jnz paranoid_userspace\trace
969 paranoid_swapgs\trace:
974 paranoid_restore\trace:
977 paranoid_userspace\trace:
978 GET_THREAD_INFO(%rcx)
979 movl TI_flags(%rcx),%ebx
980 andl $_TIF_WORK_MASK,%ebx
981 jz paranoid_swapgs\trace
982 movq %rsp,%rdi /* &pt_regs */
984 movq %rax,%rsp /* switch stack for scheduling */
985 testl $_TIF_NEED_RESCHED,%ebx
986 jnz paranoid_schedule\trace
987 movl %ebx,%edx /* arg3: thread flags */
991 ENABLE_INTERRUPTS(CLBR_NONE)
992 xorl %esi,%esi /* arg2: oldset */
993 movq %rsp,%rdi /* arg1: &pt_regs */
994 call do_notify_resume
995 DISABLE_INTERRUPTS(CLBR_NONE)
999 jmp paranoid_userspace\trace
1000 paranoid_schedule\trace:
1004 ENABLE_INTERRUPTS(CLBR_ANY)
1006 DISABLE_INTERRUPTS(CLBR_ANY)
1010 jmp paranoid_userspace\trace
1015 * Exception entry point. This expects an error code/orig_rax on the stack
1016 * and the exception handler in %rax.
1018 KPROBE_ENTRY(error_entry)
1020 CFI_REL_OFFSET rax,0
1021 /* rdi slot contains rax, oldrax contains error code */
1024 CFI_ADJUST_CFA_OFFSET (14*8)
1025 movq %rsi,13*8(%rsp)
1026 CFI_REL_OFFSET rsi,RSI
1027 movq 14*8(%rsp),%rsi /* load rax from rdi slot */
1028 CFI_REGISTER rax,rsi
1029 movq %rdx,12*8(%rsp)
1030 CFI_REL_OFFSET rdx,RDX
1031 movq %rcx,11*8(%rsp)
1032 CFI_REL_OFFSET rcx,RCX
1033 movq %rsi,10*8(%rsp) /* store rax */
1034 CFI_REL_OFFSET rax,RAX
1036 CFI_REL_OFFSET r8,R8
1038 CFI_REL_OFFSET r9,R9
1040 CFI_REL_OFFSET r10,R10
1042 CFI_REL_OFFSET r11,R11
1044 CFI_REL_OFFSET rbx,RBX
1046 CFI_REL_OFFSET rbp,RBP
1048 CFI_REL_OFFSET r12,R12
1050 CFI_REL_OFFSET r13,R13
1052 CFI_REL_OFFSET r14,R14
1054 CFI_REL_OFFSET r15,R15
1057 je error_kernelspace
1062 CFI_REL_OFFSET rdi,RDI
1064 movq ORIG_RAX(%rsp),%rsi /* get error code */
1065 movq $-1,ORIG_RAX(%rsp)
1067 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1071 DISABLE_INTERRUPTS(CLBR_NONE)
1073 GET_THREAD_INFO(%rcx)
1076 LOCKDEP_SYS_EXIT_IRQ
1077 movl TI_flags(%rcx),%edx
1078 movl $_TIF_WORK_MASK,%edi
1086 /* There are two places in the kernel that can potentially fault with
1087 usergs. Handle them here. The exception handlers after
1088 iret run with kernel gs again, so don't set the user space flag.
1089 B stepping K8s sometimes report an truncated RIP for IRET
1090 exceptions returning to compat mode. Check for these here too. */
1091 leaq irq_return(%rip),%rcx
1094 movl %ecx,%ecx /* zero extend */
1097 cmpq $gs_change,RIP(%rsp)
1100 KPROBE_END(error_entry)
1102 /* Reload gs selector with exception handling */
1103 /* edi: new selector */
1104 ENTRY(native_load_gs_index)
1107 CFI_ADJUST_CFA_OFFSET 8
1108 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1112 2: mfence /* workaround */
1115 CFI_ADJUST_CFA_OFFSET -8
1118 ENDPROC(native_load_gs_index)
1120 .section __ex_table,"a"
1122 .quad gs_change,bad_gs
1124 .section .fixup,"ax"
1125 /* running with kernelgs */
1127 SWAPGS /* switch back to user gs */
1134 * Create a kernel thread.
1136 * C extern interface:
1137 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1139 * asm input arguments:
1140 * rdi: fn, rsi: arg, rdx: flags
1142 ENTRY(kernel_thread)
1144 FAKE_STACK_FRAME $child_rip
1147 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1149 orq kernel_thread_flags(%rip),%rdi
1162 * It isn't worth to check for reschedule here,
1163 * so internally to the x86_64 port you can rely on kernel_thread()
1164 * not to reschedule the child before returning, this avoids the need
1165 * of hacks for example to fork off the per-CPU idle tasks.
1166 * [Hopefully no generic code relies on the reschedule -AK]
1172 ENDPROC(kernel_thread)
1175 pushq $0 # fake return address
1178 * Here we are in the child and the registers are set as they were
1179 * at kernel_thread() invocation in the parent.
1191 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1193 * C extern interface:
1194 * extern long execve(char *name, char **argv, char **envp)
1196 * asm input arguments:
1197 * rdi: name, rsi: argv, rdx: envp
1199 * We want to fallback into:
1200 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
1202 * do_sys_execve asm fallback arguments:
1203 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1205 ENTRY(kernel_execve)
1211 movq %rax, RAX(%rsp)
1214 je int_ret_from_sys_call
1219 ENDPROC(kernel_execve)
1221 KPROBE_ENTRY(page_fault)
1222 errorentry do_page_fault
1223 KPROBE_END(page_fault)
1225 ENTRY(coprocessor_error)
1226 zeroentry do_coprocessor_error
1227 END(coprocessor_error)
1229 ENTRY(simd_coprocessor_error)
1230 zeroentry do_simd_coprocessor_error
1231 END(simd_coprocessor_error)
1233 ENTRY(device_not_available)
1234 zeroentry math_state_restore
1235 END(device_not_available)
1237 /* runs on exception stack */
1240 PARAVIRT_ADJUST_EXCEPTION_FRAME
1242 CFI_ADJUST_CFA_OFFSET 8
1243 paranoidentry do_debug, DEBUG_STACK
1247 /* runs on exception stack */
1250 PARAVIRT_ADJUST_EXCEPTION_FRAME
1252 CFI_ADJUST_CFA_OFFSET 8
1253 paranoidentry do_nmi, 0, 0
1254 #ifdef CONFIG_TRACE_IRQFLAGS
1264 PARAVIRT_ADJUST_EXCEPTION_FRAME
1266 CFI_ADJUST_CFA_OFFSET 8
1267 paranoidentry do_int3, DEBUG_STACK
1273 zeroentry do_overflow
1281 zeroentry do_invalid_op
1284 ENTRY(coprocessor_segment_overrun)
1285 zeroentry do_coprocessor_segment_overrun
1286 END(coprocessor_segment_overrun)
1288 /* runs on exception stack */
1291 PARAVIRT_ADJUST_EXCEPTION_FRAME
1292 paranoidentry do_double_fault
1298 errorentry do_invalid_TSS
1301 ENTRY(segment_not_present)
1302 errorentry do_segment_not_present
1303 END(segment_not_present)
1305 /* runs on exception stack */
1306 ENTRY(stack_segment)
1308 PARAVIRT_ADJUST_EXCEPTION_FRAME
1309 paranoidentry do_stack_segment
1314 KPROBE_ENTRY(general_protection)
1315 errorentry do_general_protection
1316 KPROBE_END(general_protection)
1318 ENTRY(alignment_check)
1319 errorentry do_alignment_check
1320 END(alignment_check)
1323 zeroentry do_divide_error
1326 ENTRY(spurious_interrupt_bug)
1327 zeroentry do_spurious_interrupt_bug
1328 END(spurious_interrupt_bug)
1330 #ifdef CONFIG_X86_MCE
1331 /* runs on exception stack */
1332 ENTRY(machine_check)
1334 PARAVIRT_ADJUST_EXCEPTION_FRAME
1336 CFI_ADJUST_CFA_OFFSET 8
1337 paranoidentry do_machine_check
1343 /* Call softirq on interrupt stack. Interrupts are off. */
1347 CFI_ADJUST_CFA_OFFSET 8
1348 CFI_REL_OFFSET rbp,0
1350 CFI_DEF_CFA_REGISTER rbp
1351 incl %gs:pda_irqcount
1352 cmove %gs:pda_irqstackptr,%rsp
1353 push %rbp # backlink for old unwinder
1356 CFI_DEF_CFA_REGISTER rsp
1357 CFI_ADJUST_CFA_OFFSET -8
1358 decl %gs:pda_irqcount
1361 ENDPROC(call_softirq)
1363 KPROBE_ENTRY(ignore_sysret)
1368 ENDPROC(ignore_sysret)
1371 ENTRY(xen_hypervisor_callback)
1372 zeroentry xen_do_hypervisor_callback
1373 END(xen_hypervisor_callback)
1376 # A note on the "critical region" in our callback handler.
1377 # We want to avoid stacking callback handlers due to events occurring
1378 # during handling of the last event. To do this, we keep events disabled
1379 # until we've done all processing. HOWEVER, we must enable events before
1380 # popping the stack frame (can't be done atomically) and so it would still
1381 # be possible to get enough handler activations to overflow the stack.
1382 # Although unlikely, bugs of that kind are hard to track down, so we'd
1383 # like to avoid the possibility.
1384 # So, on entry to the handler we detect whether we interrupted an
1385 # existing activation in its critical region -- if so, we pop the current
1386 # activation and restart the handler using the previous one.
1388 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1390 /* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1391 see the correct pointer to the pt_regs */
1392 movq %rdi, %rsp # we don't return, adjust the stack frame
1395 11: incl %gs:pda_irqcount
1397 CFI_DEF_CFA_REGISTER rbp
1398 cmovzq %gs:pda_irqstackptr,%rsp
1399 pushq %rbp # backlink for old unwinder
1400 call xen_evtchn_do_upcall
1402 CFI_DEF_CFA_REGISTER rsp
1403 decl %gs:pda_irqcount
1406 END(do_hypervisor_callback)
1409 # Hypervisor uses this for application faults while it executes.
1410 # We get here for two reasons:
1411 # 1. Fault while reloading DS, ES, FS or GS
1412 # 2. Fault while executing IRET
1413 # Category 1 we do not need to fix up as Xen has already reloaded all segment
1414 # registers that could be reloaded and zeroed the others.
1415 # Category 2 we fix up by killing the current process. We cannot use the
1416 # normal Linux return path in this case because if we use the IRET hypercall
1417 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1418 # We distinguish between categories by comparing each saved segment register
1419 # with its current contents: any discrepancy means we in category 1.
1421 ENTRY(xen_failsafe_callback)
1422 framesz = (RIP-0x30) /* workaround buggy gas */
1424 CFI_REL_OFFSET rcx, 0
1425 CFI_REL_OFFSET r11, 8
1439 /* All segments match their saved values => Category 2 (Bad IRET). */
1445 CFI_ADJUST_CFA_OFFSET -0x30
1447 CFI_ADJUST_CFA_OFFSET 8
1449 CFI_ADJUST_CFA_OFFSET 8
1451 CFI_ADJUST_CFA_OFFSET 8
1452 jmp general_protection
1454 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1460 CFI_ADJUST_CFA_OFFSET -0x30
1462 CFI_ADJUST_CFA_OFFSET 8
1466 END(xen_failsafe_callback)
1468 #endif /* CONFIG_XEN */