2 * Copyright (C) 1991,1992 Linus Torvalds
4 * entry_32.S contains the system-call and low-level fault and trap handling routines.
6 * Stack layout while running C code:
7 * ptrace needs to have all registers on the stack.
8 * If the order here is changed, it needs to be
9 * updated in fork.c:copy_process(), signal.c:do_signal(),
10 * ptrace.c and ptrace.h
22 * 28(%esp) - %gs saved iff !CONFIG_X86_32_LAZY_GS
31 #include <linux/linkage.h>
32 #include <linux/err.h>
33 #include <asm/thread_info.h>
34 #include <asm/irqflags.h>
35 #include <asm/errno.h>
36 #include <asm/segment.h>
38 #include <asm/page_types.h>
39 #include <asm/percpu.h>
40 #include <asm/processor-flags.h>
41 #include <asm/ftrace.h>
42 #include <asm/irq_vectors.h>
43 #include <asm/cpufeature.h>
44 #include <asm/alternative-asm.h>
48 .section .entry.text, "ax"
51 * We use macros for low-level operations which need to be overridden
52 * for paravirtualization. The following will never clobber any registers:
53 * INTERRUPT_RETURN (aka. "iret")
54 * GET_CR0_INTO_EAX (aka. "movl %cr0, %eax")
55 * ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
57 * For DISABLE_INTERRUPTS/ENABLE_INTERRUPTS (aka "cli"/"sti"), you must
58 * specify what registers can be overwritten (CLBR_NONE, CLBR_EAX/EDX/ECX/ANY).
59 * Allowing a register to be clobbered can shrink the paravirt replacement
60 * enough to patch inline, increasing performance.
64 # define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
66 # define preempt_stop(clobbers)
67 # define resume_kernel restore_all
70 .macro TRACE_IRQS_IRET
71 #ifdef CONFIG_TRACE_IRQFLAGS
72 testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off?
80 * User gs save/restore
82 * %gs is used for userland TLS and kernel only uses it for stack
83 * canary which is required to be at %gs:20 by gcc. Read the comment
84 * at the top of stackprotector.h for more info.
86 * Local labels 98 and 99 are used.
88 #ifdef CONFIG_X86_32_LAZY_GS
90 /* unfortunately push/pop can't be no-op */
95 addl $(4 + \pop), %esp
100 /* all the rest are no-op */
107 .macro REG_TO_PTGS reg
109 .macro SET_KERNEL_GS reg
112 #else /* CONFIG_X86_32_LAZY_GS */
125 .pushsection .fixup, "ax"
129 _ASM_EXTABLE(98b, 99b)
133 98: mov PT_GS(%esp), %gs
136 .pushsection .fixup, "ax"
137 99: movl $0, PT_GS(%esp)
140 _ASM_EXTABLE(98b, 99b)
146 .macro REG_TO_PTGS reg
147 movl \reg, PT_GS(%esp)
149 .macro SET_KERNEL_GS reg
150 movl $(__KERNEL_STACK_CANARY), \reg
154 #endif /* CONFIG_X86_32_LAZY_GS */
156 .macro SAVE_ALL pt_regs_ax=%eax
169 movl $(__USER_DS), %edx
172 movl $(__KERNEL_PERCPU), %edx
177 .macro RESTORE_INT_REGS
187 .macro RESTORE_REGS pop=0
193 .pushsection .fixup, "ax"
210 GET_THREAD_INFO(%ebp)
212 pushl $0x0202 # Reset kernel eflags
215 /* When we fork, we trace the syscall return in the child, too. */
217 call syscall_return_slowpath
221 ENTRY(ret_from_kernel_thread)
224 GET_THREAD_INFO(%ebp)
226 pushl $0x0202 # Reset kernel eflags
228 movl PT_EBP(%esp), %eax
230 movl $0, PT_EAX(%esp)
233 * Kernel threads return to userspace as if returning from a syscall.
234 * We should check whether anything actually uses this path and, if so,
235 * consider switching it over to ret_from_fork.
238 call syscall_return_slowpath
240 ENDPROC(ret_from_kernel_thread)
243 * Return to user mode is not as complex as all this looks,
244 * but we want the default path for a system call return to
245 * go as quickly as possible which is why some of this is
246 * less clear than it otherwise should be.
249 # userspace resumption stub bypassing syscall exit tracing
252 preempt_stop(CLBR_ANY)
254 GET_THREAD_INFO(%ebp)
256 movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
257 movb PT_CS(%esp), %al
258 andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
261 * We can be coming here from child spawned by kernel_thread().
263 movl PT_CS(%esp), %eax
264 andl $SEGMENT_RPL_MASK, %eax
267 jb resume_kernel # not returning to v8086 or userspace
269 ENTRY(resume_userspace)
270 DISABLE_INTERRUPTS(CLBR_ANY)
273 call prepare_exit_to_usermode
275 END(ret_from_exception)
277 #ifdef CONFIG_PREEMPT
279 DISABLE_INTERRUPTS(CLBR_ANY)
281 cmpl $0, PER_CPU_VAR(__preempt_count)
283 testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
285 call preempt_schedule_irq
290 # SYSENTER call handler stub
291 ENTRY(entry_SYSENTER_32)
292 movl TSS_sysenter_sp0(%esp), %esp
294 pushl $__USER_DS /* pt_regs->ss */
295 pushl %ebp /* pt_regs->sp (stashed in bp) */
296 pushfl /* pt_regs->flags (except IF = 0) */
297 orl $X86_EFLAGS_IF, (%esp) /* Fix IF */
298 pushl $__USER_CS /* pt_regs->cs */
299 pushl $0 /* pt_regs->ip = 0 (placeholder) */
300 pushl %eax /* pt_regs->orig_ax */
301 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
304 * User mode is traced as though IRQs are on, and SYSENTER
310 call do_fast_syscall_32
311 /* XEN PV guests always use IRET path */
312 ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
313 "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
315 /* Opportunistic SYSEXIT */
316 TRACE_IRQS_ON /* User mode traces as IRQs on. */
317 movl PT_EIP(%esp), %edx /* pt_regs->ip */
318 movl PT_OLDESP(%esp), %ecx /* pt_regs->sp */
319 1: mov PT_FS(%esp), %fs
321 popl %ebx /* pt_regs->bx */
322 addl $2*4, %esp /* skip pt_regs->cx and pt_regs->dx */
323 popl %esi /* pt_regs->si */
324 popl %edi /* pt_regs->di */
325 popl %ebp /* pt_regs->bp */
326 popl %eax /* pt_regs->ax */
329 * Return back to the vDSO, which will pop ecx and edx.
330 * Don't bother with DS and ES (they already contain __USER_DS).
335 .pushsection .fixup, "ax"
336 2: movl $0, PT_FS(%esp)
341 ENDPROC(entry_SYSENTER_32)
343 # system call handler stub
344 ENTRY(entry_INT80_32)
346 pushl %eax /* pt_regs->orig_ax */
347 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
350 * User mode is traced as though IRQs are on. Unlike the 64-bit
351 * case, INT80 is a trap gate on 32-bit kernels, so interrupts
352 * are already on (unless user code is messing around with iopl).
356 call do_syscall_32_irqs_on
362 #ifdef CONFIG_X86_ESPFIX32
363 movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
365 * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
366 * are returning to the kernel.
367 * See comments in process.c:copy_thread() for details.
369 movb PT_OLDSS(%esp), %ah
370 movb PT_CS(%esp), %al
371 andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
372 cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
373 je ldt_ss # returning to user-space with LDT SS
376 RESTORE_REGS 4 # skip orig_eax/error_code
379 .section .fixup, "ax"
381 pushl $0 # no error code
385 _ASM_EXTABLE(irq_return, iret_exc)
387 #ifdef CONFIG_X86_ESPFIX32
389 #ifdef CONFIG_PARAVIRT
391 * The kernel can't run on a non-flat stack if paravirt mode
392 * is active. Rather than try to fixup the high bits of
393 * ESP, bypass this code entirely. This may break DOSemu
394 * and/or Wine support in a paravirt VM, although the option
395 * is still available to implement the setting of the high
396 * 16-bits in the INTERRUPT_RETURN paravirt-op.
398 cmpl $0, pv_info+PARAVIRT_enabled
403 * Setup and switch to ESPFIX stack
405 * We're returning to userspace with a 16 bit stack. The CPU will not
406 * restore the high word of ESP for us on executing iret... This is an
407 * "official" bug of all the x86-compatible CPUs, which we can work
408 * around to make dosemu and wine happy. We do this by preloading the
409 * high word of ESP with the high word of the userspace ESP while
410 * compensating for the offset by changing to the ESPFIX segment with
411 * a base address that matches for the difference.
413 #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
414 mov %esp, %edx /* load kernel esp */
415 mov PT_OLDESP(%esp), %eax /* load userspace esp */
416 mov %dx, %ax /* eax: new kernel esp */
417 sub %eax, %edx /* offset (low word is 0) */
419 mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
420 mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
422 pushl %eax /* new kernel esp */
424 * Disable interrupts, but do not irqtrace this section: we
425 * will soon execute iret and the tracer was already set to
426 * the irqstate after the IRET:
428 DISABLE_INTERRUPTS(CLBR_EAX)
429 lss (%esp), %esp /* switch to espfix segment */
432 ENDPROC(entry_INT80_32)
434 .macro FIXUP_ESPFIX_STACK
436 * Switch back for ESPFIX stack to the normal zerobased stack
438 * We can't call C functions using the ESPFIX stack. This code reads
439 * the high word of the segment base from the GDT and swiches to the
440 * normal stack and adjusts ESP with the matching offset.
442 #ifdef CONFIG_X86_ESPFIX32
443 /* fixup the stack */
444 mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
445 mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
447 addl %esp, %eax /* the adjusted stack pointer */
450 lss (%esp), %esp /* switch to the normal stack segment */
453 .macro UNWIND_ESPFIX_STACK
454 #ifdef CONFIG_X86_ESPFIX32
456 /* see if on espfix stack */
457 cmpw $__ESPFIX_SS, %ax
459 movl $__KERNEL_DS, %eax
462 /* switch to normal stack */
469 * Build the entry stubs with some assembler magic.
470 * We pack 1 stub into every 8-byte block.
473 ENTRY(irq_entries_start)
474 vector=FIRST_EXTERNAL_VECTOR
475 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
476 pushl $(~vector+0x80) /* Note: always in signed byte range */
481 END(irq_entries_start)
484 * the CPU automatically disables interrupts when executing an IRQ vector,
485 * so IRQ-flags tracing has to follow that:
487 .p2align CONFIG_X86_L1_CACHE_SHIFT
490 addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
496 ENDPROC(common_interrupt)
498 #define BUILD_INTERRUPT3(name, nr, fn) \
510 #ifdef CONFIG_TRACING
511 # define TRACE_BUILD_INTERRUPT(name, nr) BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
513 # define TRACE_BUILD_INTERRUPT(name, nr)
516 #define BUILD_INTERRUPT(name, nr) \
517 BUILD_INTERRUPT3(name, nr, smp_##name); \
518 TRACE_BUILD_INTERRUPT(name, nr)
520 /* The include is where all of the SMP etc. interrupts come from */
521 #include <asm/entry_arch.h>
523 ENTRY(coprocessor_error)
526 pushl $do_coprocessor_error
528 END(coprocessor_error)
530 ENTRY(simd_coprocessor_error)
533 #ifdef CONFIG_X86_INVD_BUG
534 /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
535 ALTERNATIVE "pushl $do_general_protection", \
536 "pushl $do_simd_coprocessor_error", \
539 pushl $do_simd_coprocessor_error
542 END(simd_coprocessor_error)
544 ENTRY(device_not_available)
546 pushl $-1 # mark this as an int
547 pushl $do_device_not_available
549 END(device_not_available)
551 #ifdef CONFIG_PARAVIRT
554 _ASM_EXTABLE(native_iret, iret_exc)
579 ENTRY(coprocessor_segment_overrun)
582 pushl $do_coprocessor_segment_overrun
584 END(coprocessor_segment_overrun)
588 pushl $do_invalid_TSS
592 ENTRY(segment_not_present)
594 pushl $do_segment_not_present
596 END(segment_not_present)
600 pushl $do_stack_segment
604 ENTRY(alignment_check)
606 pushl $do_alignment_check
612 pushl $0 # no error code
613 pushl $do_divide_error
617 #ifdef CONFIG_X86_MCE
621 pushl machine_check_vector
626 ENTRY(spurious_interrupt_bug)
629 pushl $do_spurious_interrupt_bug
631 END(spurious_interrupt_bug)
635 * Xen doesn't set %esp to be precisely what the normal SYSENTER
636 * entry point expects, so fix it up before using the normal path.
638 ENTRY(xen_sysenter_target)
639 addl $5*4, %esp /* remove xen-provided frame */
640 jmp sysenter_past_esp
642 ENTRY(xen_hypervisor_callback)
643 pushl $-1 /* orig_ax = -1 => not a system call */
648 * Check to see if we got the event in the critical
649 * region in xen_iret_direct, after we've reenabled
650 * events and checked for pending events. This simulates
651 * iret instruction's behaviour where it delivers a
652 * pending interrupt when enabling interrupts:
654 movl PT_EIP(%esp), %eax
655 cmpl $xen_iret_start_crit, %eax
657 cmpl $xen_iret_end_crit, %eax
660 jmp xen_iret_crit_fixup
664 call xen_evtchn_do_upcall
665 #ifndef CONFIG_PREEMPT
666 call xen_maybe_preempt_hcall
669 ENDPROC(xen_hypervisor_callback)
672 * Hypervisor uses this for application faults while it executes.
673 * We get here for two reasons:
674 * 1. Fault while reloading DS, ES, FS or GS
675 * 2. Fault while executing IRET
676 * Category 1 we fix up by reattempting the load, and zeroing the segment
677 * register if the load fails.
678 * Category 2 we fix up by jumping to do_iret_error. We cannot use the
679 * normal Linux return path in this case because if we use the IRET hypercall
680 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
681 * We distinguish between categories by maintaining a status value in EAX.
683 ENTRY(xen_failsafe_callback)
690 /* EAX == 0 => Category 1 (Bad segment)
691 EAX != 0 => Category 2 (Bad IRET) */
697 5: pushl $-1 /* orig_ax = -1 => not a system call */
699 jmp ret_from_exception
701 .section .fixup, "ax"
719 ENDPROC(xen_failsafe_callback)
721 BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
722 xen_evtchn_do_upcall)
724 #endif /* CONFIG_XEN */
726 #if IS_ENABLED(CONFIG_HYPERV)
728 BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
729 hyperv_vector_handler)
731 #endif /* CONFIG_HYPERV */
733 #ifdef CONFIG_FUNCTION_TRACER
734 #ifdef CONFIG_DYNAMIC_FTRACE
744 pushl $0 /* Pass NULL as regs pointer */
747 movl function_trace_op, %ecx
748 subl $MCOUNT_INSN_SIZE, %eax
754 addl $4, %esp /* skip NULL pointer */
759 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
760 .globl ftrace_graph_call
770 ENTRY(ftrace_regs_caller)
771 pushf /* push flags before compare (in cs location) */
774 * i386 does not save SS and ESP when coming from kernel.
775 * Instead, to get sp, ®s->sp is used (see ptrace.h).
776 * Unfortunately, that means eflags must be at the same location
777 * as the current return ip is. We move the return ip into the
778 * ip location, and move flags into the return ip location.
780 pushl 4(%esp) /* save return ip into ip slot */
782 pushl $0 /* Load 0 into orig_ax */
795 movl 13*4(%esp), %eax /* Get the saved flags */
796 movl %eax, 14*4(%esp) /* Move saved flags into regs->flags location */
797 /* clobbering return ip */
798 movl $__KERNEL_CS, 13*4(%esp)
800 movl 12*4(%esp), %eax /* Load ip (1st parameter) */
801 subl $MCOUNT_INSN_SIZE, %eax /* Adjust ip */
802 movl 0x4(%ebp), %edx /* Load parent ip (2nd parameter) */
803 movl function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
804 pushl %esp /* Save pt_regs as 4th parameter */
806 GLOBAL(ftrace_regs_call)
809 addl $4, %esp /* Skip pt_regs */
810 movl 14*4(%esp), %eax /* Move flags back into cs */
811 movl %eax, 13*4(%esp) /* Needed to keep addl from modifying flags */
812 movl 12*4(%esp), %eax /* Get return ip from regs->ip */
813 movl %eax, 14*4(%esp) /* Put return ip back for ret */
826 addl $8, %esp /* Skip orig_ax and ip */
827 popf /* Pop flags at end (no addl to corrupt flags) */
832 #else /* ! CONFIG_DYNAMIC_FTRACE */
835 cmpl $__PAGE_OFFSET, %esp
836 jb ftrace_stub /* Paging not enabled yet? */
838 cmpl $ftrace_stub, ftrace_trace_function
840 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
841 cmpl $ftrace_stub, ftrace_graph_return
842 jnz ftrace_graph_caller
844 cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
845 jnz ftrace_graph_caller
851 /* taken from glibc */
858 subl $MCOUNT_INSN_SIZE, %eax
860 call *ftrace_trace_function
867 #endif /* CONFIG_DYNAMIC_FTRACE */
868 #endif /* CONFIG_FUNCTION_TRACER */
870 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
871 ENTRY(ftrace_graph_caller)
878 subl $MCOUNT_INSN_SIZE, %eax
879 call prepare_ftrace_return
884 END(ftrace_graph_caller)
886 .globl return_to_handler
891 call ftrace_return_to_handler
898 #ifdef CONFIG_TRACING
899 ENTRY(trace_page_fault)
901 pushl $trace_do_page_fault
903 END(trace_page_fault)
911 /* the function address is in %gs's slot on the stack */
923 movl $(__KERNEL_PERCPU), %ecx
927 movl PT_GS(%esp), %edi # get the function address
928 movl PT_ORIG_EAX(%esp), %edx # get the error code
929 movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart
932 movl $(__USER_DS), %ecx
936 movl %esp, %eax # pt_regs pointer
938 jmp ret_from_exception
942 * Debug traps and NMI can happen at the one SYSENTER instruction
943 * that sets up the real kernel stack. Check here, since we can't
944 * allow the wrong stack to be used.
946 * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
947 * already pushed 3 words if it hits on the sysenter instruction:
948 * eflags, cs and eip.
950 * We just load the right stack, and push the three (known) values
951 * by hand onto the new stack - while updating the return eip past
952 * the instruction that would have done it for sysenter.
954 .macro FIX_STACK offset ok label
955 cmpw $__KERNEL_CS, 4(%esp)
958 movl TSS_sysenter_sp0 + \offset(%esp), %esp
961 pushl $sysenter_past_esp
966 cmpl $entry_SYSENTER_32, (%esp)
967 jne debug_stack_correct
968 FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
970 pushl $-1 # mark this as an int
973 xorl %edx, %edx # error code 0
974 movl %esp, %eax # pt_regs pointer
976 jmp ret_from_exception
980 * NMI is doubly nasty. It can happen _while_ we're handling
981 * a debug fault, and the debug fault hasn't yet been able to
982 * clear up the stack. So we first check whether we got an
983 * NMI on the sysenter entry path, but after that we need to
984 * check whether we got an NMI on the debug path where the debug
985 * fault happened on the sysenter path.
989 #ifdef CONFIG_X86_ESPFIX32
992 cmpw $__ESPFIX_SS, %ax
996 cmpl $entry_SYSENTER_32, (%esp)
1001 * Do not access memory above the end of our stack page,
1002 * it might not exist.
1004 andl $(THREAD_SIZE-1), %eax
1005 cmpl $(THREAD_SIZE-20), %eax
1007 jae nmi_stack_correct
1008 cmpl $entry_SYSENTER_32, 12(%esp)
1009 je nmi_debug_stack_check
1013 xorl %edx, %edx # zero error code
1014 movl %esp, %eax # pt_regs pointer
1016 jmp restore_all_notrace
1019 FIX_STACK 12, nmi_stack_correct, 1
1020 jmp nmi_stack_correct
1022 nmi_debug_stack_check:
1023 cmpw $__KERNEL_CS, 16(%esp)
1024 jne nmi_stack_correct
1026 jb nmi_stack_correct
1027 cmpl $debug_esp_fix_insn, (%esp)
1028 ja nmi_stack_correct
1029 FIX_STACK 24, nmi_stack_correct, 1
1030 jmp nmi_stack_correct
1032 #ifdef CONFIG_X86_ESPFIX32
1035 * create the pointer to lss back
1040 /* copy the iret frame of 12 bytes */
1046 FIXUP_ESPFIX_STACK # %eax == %esp
1047 xorl %edx, %edx # zero error code
1050 lss 12+4(%esp), %esp # back to espfix stack
1057 pushl $-1 # mark this as an int
1060 xorl %edx, %edx # zero error code
1061 movl %esp, %eax # pt_regs pointer
1063 jmp ret_from_exception
1066 ENTRY(general_protection)
1067 pushl $do_general_protection
1069 END(general_protection)
1071 #ifdef CONFIG_KVM_GUEST
1072 ENTRY(async_page_fault)
1074 pushl $do_async_page_fault
1076 END(async_page_fault)