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 .globl int_with_check
496 int_ret_from_sys_call:
497 DISABLE_INTERRUPTS(CLBR_NONE)
499 testl $3,CS-ARGOFFSET(%rsp)
500 je retint_restore_args
501 movl $_TIF_ALLWORK_MASK,%edi
502 /* edi: mask to check */
505 GET_THREAD_INFO(%rcx)
506 movl TI_flags(%rcx),%edx
509 andl $~TS_COMPAT,TI_status(%rcx)
512 /* Either reschedule or signal or syscall exit tracking needed. */
513 /* First do a reschedule test. */
514 /* edx: work, edi: workmask */
516 bt $TIF_NEED_RESCHED,%edx
519 ENABLE_INTERRUPTS(CLBR_NONE)
521 CFI_ADJUST_CFA_OFFSET 8
524 CFI_ADJUST_CFA_OFFSET -8
525 DISABLE_INTERRUPTS(CLBR_NONE)
529 /* handle signals and tracing -- both require a full stack frame */
532 ENABLE_INTERRUPTS(CLBR_NONE)
534 /* Check for syscall exit trace */
535 testl $_TIF_WORK_SYSCALL_EXIT,%edx
538 CFI_ADJUST_CFA_OFFSET 8
539 leaq 8(%rsp),%rdi # &ptregs -> arg1
540 call syscall_trace_leave
542 CFI_ADJUST_CFA_OFFSET -8
543 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
547 testl $_TIF_DO_NOTIFY_MASK,%edx
549 movq %rsp,%rdi # &ptregs -> arg1
550 xorl %esi,%esi # oldset -> arg2
551 call do_notify_resume
552 1: movl $_TIF_WORK_MASK,%edi
555 DISABLE_INTERRUPTS(CLBR_NONE)
562 * Certain special system calls that need to save a complete full stack frame.
565 .macro PTREGSCALL label,func,arg
568 leaq \func(%rip),%rax
569 leaq -ARGOFFSET+8(%rsp),\arg /* 8 for return address */
570 jmp ptregscall_common
576 PTREGSCALL stub_clone, sys_clone, %r8
577 PTREGSCALL stub_fork, sys_fork, %rdi
578 PTREGSCALL stub_vfork, sys_vfork, %rdi
579 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
580 PTREGSCALL stub_iopl, sys_iopl, %rsi
582 ENTRY(ptregscall_common)
584 CFI_ADJUST_CFA_OFFSET -8
585 CFI_REGISTER rip, r11
588 CFI_REGISTER rip, r15
589 FIXUP_TOP_OF_STACK %r11
591 RESTORE_TOP_OF_STACK %r11
593 CFI_REGISTER rip, r11
596 CFI_ADJUST_CFA_OFFSET 8
597 CFI_REL_OFFSET rip, 0
600 END(ptregscall_common)
605 CFI_ADJUST_CFA_OFFSET -8
606 CFI_REGISTER rip, r11
608 FIXUP_TOP_OF_STACK %r11
611 RESTORE_TOP_OF_STACK %r11
614 jmp int_ret_from_sys_call
619 * sigreturn is special because it needs to restore all registers on return.
620 * This cannot be done with SYSRET, so use the IRET return path instead.
622 ENTRY(stub_rt_sigreturn)
625 CFI_ADJUST_CFA_OFFSET -8
628 FIXUP_TOP_OF_STACK %r11
629 call sys_rt_sigreturn
630 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
632 jmp int_ret_from_sys_call
634 END(stub_rt_sigreturn)
637 * initial frame state for interrupts and exceptions
642 CFI_DEF_CFA rsp,SS+8-\ref
643 /*CFI_REL_OFFSET ss,SS-\ref*/
644 CFI_REL_OFFSET rsp,RSP-\ref
645 /*CFI_REL_OFFSET rflags,EFLAGS-\ref*/
646 /*CFI_REL_OFFSET cs,CS-\ref*/
647 CFI_REL_OFFSET rip,RIP-\ref
650 /* initial frame state for interrupts (and exceptions without error code) */
651 #define INTR_FRAME _frame RIP
652 /* initial frame state for exceptions with error code (and interrupts with
653 vector already pushed) */
654 #define XCPT_FRAME _frame ORIG_RAX
657 * Interrupt entry/exit.
659 * Interrupt entry points save only callee clobbered registers in fast path.
661 * Entry runs with interrupts off.
664 /* 0(%rsp): interrupt number */
665 .macro interrupt func
668 leaq -ARGOFFSET(%rsp),%rdi # arg1 for handler
670 CFI_ADJUST_CFA_OFFSET 8
671 CFI_REL_OFFSET rbp, 0
673 CFI_DEF_CFA_REGISTER rbp
677 /* irqcount is used to check if a CPU is already on an interrupt
678 stack or not. While this is essentially redundant with preempt_count
679 it is a little cheaper to use a separate counter in the PDA
680 (short of moving irq_enter into assembly, which would be too
682 1: incl %gs:pda_irqcount
683 cmoveq %gs:pda_irqstackptr,%rsp
684 push %rbp # backlink for old unwinder
686 * We entered an interrupt context - irqs are off:
692 ENTRY(common_interrupt)
695 /* 0(%rsp): oldrsp-ARGOFFSET */
697 DISABLE_INTERRUPTS(CLBR_NONE)
699 decl %gs:pda_irqcount
701 CFI_DEF_CFA_REGISTER rsp
702 CFI_ADJUST_CFA_OFFSET -8
704 GET_THREAD_INFO(%rcx)
705 testl $3,CS-ARGOFFSET(%rsp)
708 /* Interrupt came from user space */
710 * Has a correct top of stack, but a partial stack frame
711 * %rcx: thread info. Interrupts off.
713 retint_with_reschedule:
714 movl $_TIF_WORK_MASK,%edi
717 movl TI_flags(%rcx),%edx
722 retint_swapgs: /* return to user-space */
724 * The iretq could re-enable interrupts:
726 DISABLE_INTERRUPTS(CLBR_ANY)
731 retint_restore_args: /* return to kernel space */
732 DISABLE_INTERRUPTS(CLBR_ANY)
734 * The iretq could re-enable interrupts:
743 .section __ex_table, "a"
744 .quad irq_return, bad_iret
747 #ifdef CONFIG_PARAVIRT
751 .section __ex_table,"a"
752 .quad native_iret, bad_iret
759 * The iret traps when the %cs or %ss being restored is bogus.
760 * We've lost the original trap vector and error code.
761 * #GPF is the most likely one to get for an invalid selector.
762 * So pretend we completed the iret and took the #GPF in user mode.
764 * We are now running with the kernel GS after exception recovery.
765 * But error_entry expects us to have user GS to match the user %cs,
771 jmp general_protection
775 /* edi: workmask, edx: work */
778 bt $TIF_NEED_RESCHED,%edx
781 ENABLE_INTERRUPTS(CLBR_NONE)
783 CFI_ADJUST_CFA_OFFSET 8
786 CFI_ADJUST_CFA_OFFSET -8
787 GET_THREAD_INFO(%rcx)
788 DISABLE_INTERRUPTS(CLBR_NONE)
793 testl $_TIF_DO_NOTIFY_MASK,%edx
796 ENABLE_INTERRUPTS(CLBR_NONE)
798 movq $-1,ORIG_RAX(%rsp)
799 xorl %esi,%esi # oldset
800 movq %rsp,%rdi # &pt_regs
801 call do_notify_resume
803 DISABLE_INTERRUPTS(CLBR_NONE)
805 GET_THREAD_INFO(%rcx)
806 jmp retint_with_reschedule
808 #ifdef CONFIG_PREEMPT
809 /* Returning to kernel space. Check if we need preemption */
810 /* rcx: threadinfo. interrupts off. */
812 cmpl $0,TI_preempt_count(%rcx)
813 jnz retint_restore_args
814 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
815 jnc retint_restore_args
816 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
817 jnc retint_restore_args
818 call preempt_schedule_irq
823 END(common_interrupt)
828 .macro apicinterrupt num,func
831 CFI_ADJUST_CFA_OFFSET 8
837 ENTRY(thermal_interrupt)
838 apicinterrupt THERMAL_APIC_VECTOR,smp_thermal_interrupt
839 END(thermal_interrupt)
841 ENTRY(threshold_interrupt)
842 apicinterrupt THRESHOLD_APIC_VECTOR,mce_threshold_interrupt
843 END(threshold_interrupt)
846 ENTRY(reschedule_interrupt)
847 apicinterrupt RESCHEDULE_VECTOR,smp_reschedule_interrupt
848 END(reschedule_interrupt)
850 .macro INVALIDATE_ENTRY num
851 ENTRY(invalidate_interrupt\num)
852 apicinterrupt INVALIDATE_TLB_VECTOR_START+\num,smp_invalidate_interrupt
853 END(invalidate_interrupt\num)
865 ENTRY(call_function_interrupt)
866 apicinterrupt CALL_FUNCTION_VECTOR,smp_call_function_interrupt
867 END(call_function_interrupt)
868 ENTRY(call_function_single_interrupt)
869 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR,smp_call_function_single_interrupt
870 END(call_function_single_interrupt)
871 ENTRY(irq_move_cleanup_interrupt)
872 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR,smp_irq_move_cleanup_interrupt
873 END(irq_move_cleanup_interrupt)
876 ENTRY(apic_timer_interrupt)
877 apicinterrupt LOCAL_TIMER_VECTOR,smp_apic_timer_interrupt
878 END(apic_timer_interrupt)
880 ENTRY(uv_bau_message_intr1)
881 apicinterrupt 220,uv_bau_message_interrupt
882 END(uv_bau_message_intr1)
884 ENTRY(error_interrupt)
885 apicinterrupt ERROR_APIC_VECTOR,smp_error_interrupt
888 ENTRY(spurious_interrupt)
889 apicinterrupt SPURIOUS_APIC_VECTOR,smp_spurious_interrupt
890 END(spurious_interrupt)
893 * Exception entry points.
897 PARAVIRT_ADJUST_EXCEPTION_FRAME
898 pushq $0 /* push error code/oldrax */
899 CFI_ADJUST_CFA_OFFSET 8
900 pushq %rax /* push real oldrax to the rdi slot */
901 CFI_ADJUST_CFA_OFFSET 8
908 .macro errorentry sym
910 PARAVIRT_ADJUST_EXCEPTION_FRAME
912 CFI_ADJUST_CFA_OFFSET 8
919 /* error code is on the stack already */
920 /* handle NMI like exceptions that can happen everywhere */
921 .macro paranoidentry sym, ist=0, irqtrace=1
925 movl $MSR_GS_BASE,%ecx
933 movq %gs:pda_data_offset, %rbp
936 movq ORIG_RAX(%rsp),%rsi
937 movq $-1,ORIG_RAX(%rsp)
939 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
943 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
945 DISABLE_INTERRUPTS(CLBR_NONE)
952 * "Paranoid" exit path from exception stack.
953 * Paranoid because this is used by NMIs and cannot take
954 * any kernel state for granted.
955 * We don't do kernel preemption checks here, because only
956 * NMI should be common and it does not enable IRQs and
957 * cannot get reschedule ticks.
959 * "trace" is 0 for the NMI handler only, because irq-tracing
960 * is fundamentally NMI-unsafe. (we cannot change the soft and
961 * hard flags at once, atomically)
963 .macro paranoidexit trace=1
964 /* ebx: no swapgs flag */
966 testl %ebx,%ebx /* swapgs needed? */
967 jnz paranoid_restore\trace
969 jnz paranoid_userspace\trace
970 paranoid_swapgs\trace:
975 paranoid_restore\trace:
978 paranoid_userspace\trace:
979 GET_THREAD_INFO(%rcx)
980 movl TI_flags(%rcx),%ebx
981 andl $_TIF_WORK_MASK,%ebx
982 jz paranoid_swapgs\trace
983 movq %rsp,%rdi /* &pt_regs */
985 movq %rax,%rsp /* switch stack for scheduling */
986 testl $_TIF_NEED_RESCHED,%ebx
987 jnz paranoid_schedule\trace
988 movl %ebx,%edx /* arg3: thread flags */
992 ENABLE_INTERRUPTS(CLBR_NONE)
993 xorl %esi,%esi /* arg2: oldset */
994 movq %rsp,%rdi /* arg1: &pt_regs */
995 call do_notify_resume
996 DISABLE_INTERRUPTS(CLBR_NONE)
1000 jmp paranoid_userspace\trace
1001 paranoid_schedule\trace:
1005 ENABLE_INTERRUPTS(CLBR_ANY)
1007 DISABLE_INTERRUPTS(CLBR_ANY)
1011 jmp paranoid_userspace\trace
1016 * Exception entry point. This expects an error code/orig_rax on the stack
1017 * and the exception handler in %rax.
1019 KPROBE_ENTRY(error_entry)
1021 CFI_REL_OFFSET rax,0
1022 /* rdi slot contains rax, oldrax contains error code */
1025 CFI_ADJUST_CFA_OFFSET (14*8)
1026 movq %rsi,13*8(%rsp)
1027 CFI_REL_OFFSET rsi,RSI
1028 movq 14*8(%rsp),%rsi /* load rax from rdi slot */
1029 CFI_REGISTER rax,rsi
1030 movq %rdx,12*8(%rsp)
1031 CFI_REL_OFFSET rdx,RDX
1032 movq %rcx,11*8(%rsp)
1033 CFI_REL_OFFSET rcx,RCX
1034 movq %rsi,10*8(%rsp) /* store rax */
1035 CFI_REL_OFFSET rax,RAX
1037 CFI_REL_OFFSET r8,R8
1039 CFI_REL_OFFSET r9,R9
1041 CFI_REL_OFFSET r10,R10
1043 CFI_REL_OFFSET r11,R11
1045 CFI_REL_OFFSET rbx,RBX
1047 CFI_REL_OFFSET rbp,RBP
1049 CFI_REL_OFFSET r12,R12
1051 CFI_REL_OFFSET r13,R13
1053 CFI_REL_OFFSET r14,R14
1055 CFI_REL_OFFSET r15,R15
1058 je error_kernelspace
1063 CFI_REL_OFFSET rdi,RDI
1065 movq ORIG_RAX(%rsp),%rsi /* get error code */
1066 movq $-1,ORIG_RAX(%rsp)
1068 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1072 DISABLE_INTERRUPTS(CLBR_NONE)
1074 GET_THREAD_INFO(%rcx)
1077 LOCKDEP_SYS_EXIT_IRQ
1078 movl TI_flags(%rcx),%edx
1079 movl $_TIF_WORK_MASK,%edi
1087 /* There are two places in the kernel that can potentially fault with
1088 usergs. Handle them here. The exception handlers after
1089 iret run with kernel gs again, so don't set the user space flag.
1090 B stepping K8s sometimes report an truncated RIP for IRET
1091 exceptions returning to compat mode. Check for these here too. */
1092 leaq irq_return(%rip),%rcx
1095 movl %ecx,%ecx /* zero extend */
1098 cmpq $gs_change,RIP(%rsp)
1101 KPROBE_END(error_entry)
1103 /* Reload gs selector with exception handling */
1104 /* edi: new selector */
1105 ENTRY(native_load_gs_index)
1108 CFI_ADJUST_CFA_OFFSET 8
1109 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1113 2: mfence /* workaround */
1116 CFI_ADJUST_CFA_OFFSET -8
1119 ENDPROC(native_load_gs_index)
1121 .section __ex_table,"a"
1123 .quad gs_change,bad_gs
1125 .section .fixup,"ax"
1126 /* running with kernelgs */
1128 SWAPGS /* switch back to user gs */
1135 * Create a kernel thread.
1137 * C extern interface:
1138 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1140 * asm input arguments:
1141 * rdi: fn, rsi: arg, rdx: flags
1143 ENTRY(kernel_thread)
1145 FAKE_STACK_FRAME $child_rip
1148 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1150 orq kernel_thread_flags(%rip),%rdi
1163 * It isn't worth to check for reschedule here,
1164 * so internally to the x86_64 port you can rely on kernel_thread()
1165 * not to reschedule the child before returning, this avoids the need
1166 * of hacks for example to fork off the per-CPU idle tasks.
1167 * [Hopefully no generic code relies on the reschedule -AK]
1173 ENDPROC(kernel_thread)
1176 pushq $0 # fake return address
1179 * Here we are in the child and the registers are set as they were
1180 * at kernel_thread() invocation in the parent.
1192 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1194 * C extern interface:
1195 * extern long execve(char *name, char **argv, char **envp)
1197 * asm input arguments:
1198 * rdi: name, rsi: argv, rdx: envp
1200 * We want to fallback into:
1201 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
1203 * do_sys_execve asm fallback arguments:
1204 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1206 ENTRY(kernel_execve)
1212 movq %rax, RAX(%rsp)
1215 je int_ret_from_sys_call
1220 ENDPROC(kernel_execve)
1222 KPROBE_ENTRY(page_fault)
1223 errorentry do_page_fault
1224 KPROBE_END(page_fault)
1226 ENTRY(coprocessor_error)
1227 zeroentry do_coprocessor_error
1228 END(coprocessor_error)
1230 ENTRY(simd_coprocessor_error)
1231 zeroentry do_simd_coprocessor_error
1232 END(simd_coprocessor_error)
1234 ENTRY(device_not_available)
1235 zeroentry math_state_restore
1236 END(device_not_available)
1238 /* runs on exception stack */
1241 PARAVIRT_ADJUST_EXCEPTION_FRAME
1243 CFI_ADJUST_CFA_OFFSET 8
1244 paranoidentry do_debug, DEBUG_STACK
1248 /* runs on exception stack */
1251 PARAVIRT_ADJUST_EXCEPTION_FRAME
1253 CFI_ADJUST_CFA_OFFSET 8
1254 paranoidentry do_nmi, 0, 0
1255 #ifdef CONFIG_TRACE_IRQFLAGS
1265 PARAVIRT_ADJUST_EXCEPTION_FRAME
1267 CFI_ADJUST_CFA_OFFSET 8
1268 paranoidentry do_int3, DEBUG_STACK
1274 zeroentry do_overflow
1282 zeroentry do_invalid_op
1285 ENTRY(coprocessor_segment_overrun)
1286 zeroentry do_coprocessor_segment_overrun
1287 END(coprocessor_segment_overrun)
1289 /* runs on exception stack */
1292 PARAVIRT_ADJUST_EXCEPTION_FRAME
1293 paranoidentry do_double_fault
1299 errorentry do_invalid_TSS
1302 ENTRY(segment_not_present)
1303 errorentry do_segment_not_present
1304 END(segment_not_present)
1306 /* runs on exception stack */
1307 ENTRY(stack_segment)
1309 PARAVIRT_ADJUST_EXCEPTION_FRAME
1310 paranoidentry do_stack_segment
1315 KPROBE_ENTRY(general_protection)
1316 errorentry do_general_protection
1317 KPROBE_END(general_protection)
1319 ENTRY(alignment_check)
1320 errorentry do_alignment_check
1321 END(alignment_check)
1324 zeroentry do_divide_error
1327 ENTRY(spurious_interrupt_bug)
1328 zeroentry do_spurious_interrupt_bug
1329 END(spurious_interrupt_bug)
1331 #ifdef CONFIG_X86_MCE
1332 /* runs on exception stack */
1333 ENTRY(machine_check)
1335 PARAVIRT_ADJUST_EXCEPTION_FRAME
1337 CFI_ADJUST_CFA_OFFSET 8
1338 paranoidentry do_machine_check
1344 /* Call softirq on interrupt stack. Interrupts are off. */
1348 CFI_ADJUST_CFA_OFFSET 8
1349 CFI_REL_OFFSET rbp,0
1351 CFI_DEF_CFA_REGISTER rbp
1352 incl %gs:pda_irqcount
1353 cmove %gs:pda_irqstackptr,%rsp
1354 push %rbp # backlink for old unwinder
1357 CFI_DEF_CFA_REGISTER rsp
1358 CFI_ADJUST_CFA_OFFSET -8
1359 decl %gs:pda_irqcount
1362 ENDPROC(call_softirq)
1364 KPROBE_ENTRY(ignore_sysret)
1369 ENDPROC(ignore_sysret)
1372 ENTRY(xen_hypervisor_callback)
1373 zeroentry xen_do_hypervisor_callback
1374 END(xen_hypervisor_callback)
1377 # A note on the "critical region" in our callback handler.
1378 # We want to avoid stacking callback handlers due to events occurring
1379 # during handling of the last event. To do this, we keep events disabled
1380 # until we've done all processing. HOWEVER, we must enable events before
1381 # popping the stack frame (can't be done atomically) and so it would still
1382 # be possible to get enough handler activations to overflow the stack.
1383 # Although unlikely, bugs of that kind are hard to track down, so we'd
1384 # like to avoid the possibility.
1385 # So, on entry to the handler we detect whether we interrupted an
1386 # existing activation in its critical region -- if so, we pop the current
1387 # activation and restart the handler using the previous one.
1389 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1391 /* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1392 see the correct pointer to the pt_regs */
1393 movq %rdi, %rsp # we don't return, adjust the stack frame
1396 11: incl %gs:pda_irqcount
1398 CFI_DEF_CFA_REGISTER rbp
1399 cmovzq %gs:pda_irqstackptr,%rsp
1400 pushq %rbp # backlink for old unwinder
1401 call xen_evtchn_do_upcall
1403 CFI_DEF_CFA_REGISTER rsp
1404 decl %gs:pda_irqcount
1407 END(do_hypervisor_callback)
1410 # Hypervisor uses this for application faults while it executes.
1411 # We get here for two reasons:
1412 # 1. Fault while reloading DS, ES, FS or GS
1413 # 2. Fault while executing IRET
1414 # Category 1 we do not need to fix up as Xen has already reloaded all segment
1415 # registers that could be reloaded and zeroed the others.
1416 # Category 2 we fix up by killing the current process. We cannot use the
1417 # normal Linux return path in this case because if we use the IRET hypercall
1418 # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1419 # We distinguish between categories by comparing each saved segment register
1420 # with its current contents: any discrepancy means we in category 1.
1422 ENTRY(xen_failsafe_callback)
1423 framesz = (RIP-0x30) /* workaround buggy gas */
1425 CFI_REL_OFFSET rcx, 0
1426 CFI_REL_OFFSET r11, 8
1440 /* All segments match their saved values => Category 2 (Bad IRET). */
1446 CFI_ADJUST_CFA_OFFSET -0x30
1448 CFI_ADJUST_CFA_OFFSET 8
1450 CFI_ADJUST_CFA_OFFSET 8
1452 CFI_ADJUST_CFA_OFFSET 8
1453 jmp general_protection
1455 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1461 CFI_ADJUST_CFA_OFFSET -0x30
1463 CFI_ADJUST_CFA_OFFSET 8
1467 END(xen_failsafe_callback)
1469 #endif /* CONFIG_XEN */