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 %ecx /* pt_regs->cx */
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
314 /* Opportunistic SYSEXIT */
315 TRACE_IRQS_ON /* User mode traces as IRQs on. */
316 movl PT_EIP(%esp), %edx /* pt_regs->ip */
317 movl PT_OLDESP(%esp), %ecx /* pt_regs->sp */
318 1: mov PT_FS(%esp), %fs
320 popl %ebx /* pt_regs->bx */
321 addl $2*4, %esp /* skip pt_regs->cx and pt_regs->dx */
322 popl %esi /* pt_regs->si */
323 popl %edi /* pt_regs->di */
324 popl %ebp /* pt_regs->bp */
325 popl %eax /* pt_regs->ax */
328 * Return back to the vDSO, which will pop ecx and edx.
329 * Don't bother with DS and ES (they already contain __USER_DS).
331 ENABLE_INTERRUPTS_SYSEXIT
333 .pushsection .fixup, "ax"
334 2: movl $0, PT_FS(%esp)
339 ENDPROC(entry_SYSENTER_32)
341 # system call handler stub
342 ENTRY(entry_INT80_32)
344 pushl %eax /* pt_regs->orig_ax */
345 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
348 * User mode is traced as though IRQs are on. Unlike the 64-bit
349 * case, INT80 is a trap gate on 32-bit kernels, so interrupts
350 * are already on (unless user code is messing around with iopl).
354 call do_syscall_32_irqs_on
360 #ifdef CONFIG_X86_ESPFIX32
361 movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
363 * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
364 * are returning to the kernel.
365 * See comments in process.c:copy_thread() for details.
367 movb PT_OLDSS(%esp), %ah
368 movb PT_CS(%esp), %al
369 andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
370 cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
371 je ldt_ss # returning to user-space with LDT SS
374 RESTORE_REGS 4 # skip orig_eax/error_code
377 .section .fixup, "ax"
379 pushl $0 # no error code
383 _ASM_EXTABLE(irq_return, iret_exc)
385 #ifdef CONFIG_X86_ESPFIX32
387 #ifdef CONFIG_PARAVIRT
389 * The kernel can't run on a non-flat stack if paravirt mode
390 * is active. Rather than try to fixup the high bits of
391 * ESP, bypass this code entirely. This may break DOSemu
392 * and/or Wine support in a paravirt VM, although the option
393 * is still available to implement the setting of the high
394 * 16-bits in the INTERRUPT_RETURN paravirt-op.
396 cmpl $0, pv_info+PARAVIRT_enabled
401 * Setup and switch to ESPFIX stack
403 * We're returning to userspace with a 16 bit stack. The CPU will not
404 * restore the high word of ESP for us on executing iret... This is an
405 * "official" bug of all the x86-compatible CPUs, which we can work
406 * around to make dosemu and wine happy. We do this by preloading the
407 * high word of ESP with the high word of the userspace ESP while
408 * compensating for the offset by changing to the ESPFIX segment with
409 * a base address that matches for the difference.
411 #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
412 mov %esp, %edx /* load kernel esp */
413 mov PT_OLDESP(%esp), %eax /* load userspace esp */
414 mov %dx, %ax /* eax: new kernel esp */
415 sub %eax, %edx /* offset (low word is 0) */
417 mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
418 mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
420 pushl %eax /* new kernel esp */
422 * Disable interrupts, but do not irqtrace this section: we
423 * will soon execute iret and the tracer was already set to
424 * the irqstate after the IRET:
426 DISABLE_INTERRUPTS(CLBR_EAX)
427 lss (%esp), %esp /* switch to espfix segment */
430 ENDPROC(entry_INT80_32)
432 .macro FIXUP_ESPFIX_STACK
434 * Switch back for ESPFIX stack to the normal zerobased stack
436 * We can't call C functions using the ESPFIX stack. This code reads
437 * the high word of the segment base from the GDT and swiches to the
438 * normal stack and adjusts ESP with the matching offset.
440 #ifdef CONFIG_X86_ESPFIX32
441 /* fixup the stack */
442 mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
443 mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
445 addl %esp, %eax /* the adjusted stack pointer */
448 lss (%esp), %esp /* switch to the normal stack segment */
451 .macro UNWIND_ESPFIX_STACK
452 #ifdef CONFIG_X86_ESPFIX32
454 /* see if on espfix stack */
455 cmpw $__ESPFIX_SS, %ax
457 movl $__KERNEL_DS, %eax
460 /* switch to normal stack */
467 * Build the entry stubs with some assembler magic.
468 * We pack 1 stub into every 8-byte block.
471 ENTRY(irq_entries_start)
472 vector=FIRST_EXTERNAL_VECTOR
473 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
474 pushl $(~vector+0x80) /* Note: always in signed byte range */
479 END(irq_entries_start)
482 * the CPU automatically disables interrupts when executing an IRQ vector,
483 * so IRQ-flags tracing has to follow that:
485 .p2align CONFIG_X86_L1_CACHE_SHIFT
488 addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
494 ENDPROC(common_interrupt)
496 #define BUILD_INTERRUPT3(name, nr, fn) \
508 #ifdef CONFIG_TRACING
509 # define TRACE_BUILD_INTERRUPT(name, nr) BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
511 # define TRACE_BUILD_INTERRUPT(name, nr)
514 #define BUILD_INTERRUPT(name, nr) \
515 BUILD_INTERRUPT3(name, nr, smp_##name); \
516 TRACE_BUILD_INTERRUPT(name, nr)
518 /* The include is where all of the SMP etc. interrupts come from */
519 #include <asm/entry_arch.h>
521 ENTRY(coprocessor_error)
524 pushl $do_coprocessor_error
526 END(coprocessor_error)
528 ENTRY(simd_coprocessor_error)
531 #ifdef CONFIG_X86_INVD_BUG
532 /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
533 ALTERNATIVE "pushl $do_general_protection", \
534 "pushl $do_simd_coprocessor_error", \
537 pushl $do_simd_coprocessor_error
540 END(simd_coprocessor_error)
542 ENTRY(device_not_available)
544 pushl $-1 # mark this as an int
545 pushl $do_device_not_available
547 END(device_not_available)
549 #ifdef CONFIG_PARAVIRT
552 _ASM_EXTABLE(native_iret, iret_exc)
555 ENTRY(native_irq_enable_sysexit)
558 END(native_irq_enable_sysexit)
582 ENTRY(coprocessor_segment_overrun)
585 pushl $do_coprocessor_segment_overrun
587 END(coprocessor_segment_overrun)
591 pushl $do_invalid_TSS
595 ENTRY(segment_not_present)
597 pushl $do_segment_not_present
599 END(segment_not_present)
603 pushl $do_stack_segment
607 ENTRY(alignment_check)
609 pushl $do_alignment_check
615 pushl $0 # no error code
616 pushl $do_divide_error
620 #ifdef CONFIG_X86_MCE
624 pushl machine_check_vector
629 ENTRY(spurious_interrupt_bug)
632 pushl $do_spurious_interrupt_bug
634 END(spurious_interrupt_bug)
638 * Xen doesn't set %esp to be precisely what the normal SYSENTER
639 * entry point expects, so fix it up before using the normal path.
641 ENTRY(xen_sysenter_target)
642 addl $5*4, %esp /* remove xen-provided frame */
643 jmp sysenter_past_esp
645 ENTRY(xen_hypervisor_callback)
646 pushl $-1 /* orig_ax = -1 => not a system call */
651 * Check to see if we got the event in the critical
652 * region in xen_iret_direct, after we've reenabled
653 * events and checked for pending events. This simulates
654 * iret instruction's behaviour where it delivers a
655 * pending interrupt when enabling interrupts:
657 movl PT_EIP(%esp), %eax
658 cmpl $xen_iret_start_crit, %eax
660 cmpl $xen_iret_end_crit, %eax
663 jmp xen_iret_crit_fixup
667 call xen_evtchn_do_upcall
668 #ifndef CONFIG_PREEMPT
669 call xen_maybe_preempt_hcall
672 ENDPROC(xen_hypervisor_callback)
675 * Hypervisor uses this for application faults while it executes.
676 * We get here for two reasons:
677 * 1. Fault while reloading DS, ES, FS or GS
678 * 2. Fault while executing IRET
679 * Category 1 we fix up by reattempting the load, and zeroing the segment
680 * register if the load fails.
681 * Category 2 we fix up by jumping to do_iret_error. We cannot use the
682 * normal Linux return path in this case because if we use the IRET hypercall
683 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
684 * We distinguish between categories by maintaining a status value in EAX.
686 ENTRY(xen_failsafe_callback)
693 /* EAX == 0 => Category 1 (Bad segment)
694 EAX != 0 => Category 2 (Bad IRET) */
700 5: pushl $-1 /* orig_ax = -1 => not a system call */
702 jmp ret_from_exception
704 .section .fixup, "ax"
722 ENDPROC(xen_failsafe_callback)
724 BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
725 xen_evtchn_do_upcall)
727 #endif /* CONFIG_XEN */
729 #if IS_ENABLED(CONFIG_HYPERV)
731 BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
732 hyperv_vector_handler)
734 #endif /* CONFIG_HYPERV */
736 #ifdef CONFIG_FUNCTION_TRACER
737 #ifdef CONFIG_DYNAMIC_FTRACE
747 pushl $0 /* Pass NULL as regs pointer */
750 movl function_trace_op, %ecx
751 subl $MCOUNT_INSN_SIZE, %eax
757 addl $4, %esp /* skip NULL pointer */
762 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
763 .globl ftrace_graph_call
773 ENTRY(ftrace_regs_caller)
774 pushf /* push flags before compare (in cs location) */
777 * i386 does not save SS and ESP when coming from kernel.
778 * Instead, to get sp, ®s->sp is used (see ptrace.h).
779 * Unfortunately, that means eflags must be at the same location
780 * as the current return ip is. We move the return ip into the
781 * ip location, and move flags into the return ip location.
783 pushl 4(%esp) /* save return ip into ip slot */
785 pushl $0 /* Load 0 into orig_ax */
798 movl 13*4(%esp), %eax /* Get the saved flags */
799 movl %eax, 14*4(%esp) /* Move saved flags into regs->flags location */
800 /* clobbering return ip */
801 movl $__KERNEL_CS, 13*4(%esp)
803 movl 12*4(%esp), %eax /* Load ip (1st parameter) */
804 subl $MCOUNT_INSN_SIZE, %eax /* Adjust ip */
805 movl 0x4(%ebp), %edx /* Load parent ip (2nd parameter) */
806 movl function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
807 pushl %esp /* Save pt_regs as 4th parameter */
809 GLOBAL(ftrace_regs_call)
812 addl $4, %esp /* Skip pt_regs */
813 movl 14*4(%esp), %eax /* Move flags back into cs */
814 movl %eax, 13*4(%esp) /* Needed to keep addl from modifying flags */
815 movl 12*4(%esp), %eax /* Get return ip from regs->ip */
816 movl %eax, 14*4(%esp) /* Put return ip back for ret */
829 addl $8, %esp /* Skip orig_ax and ip */
830 popf /* Pop flags at end (no addl to corrupt flags) */
835 #else /* ! CONFIG_DYNAMIC_FTRACE */
838 cmpl $__PAGE_OFFSET, %esp
839 jb ftrace_stub /* Paging not enabled yet? */
841 cmpl $ftrace_stub, ftrace_trace_function
843 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
844 cmpl $ftrace_stub, ftrace_graph_return
845 jnz ftrace_graph_caller
847 cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
848 jnz ftrace_graph_caller
854 /* taken from glibc */
861 subl $MCOUNT_INSN_SIZE, %eax
863 call *ftrace_trace_function
870 #endif /* CONFIG_DYNAMIC_FTRACE */
871 #endif /* CONFIG_FUNCTION_TRACER */
873 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
874 ENTRY(ftrace_graph_caller)
881 subl $MCOUNT_INSN_SIZE, %eax
882 call prepare_ftrace_return
887 END(ftrace_graph_caller)
889 .globl return_to_handler
894 call ftrace_return_to_handler
901 #ifdef CONFIG_TRACING
902 ENTRY(trace_page_fault)
904 pushl $trace_do_page_fault
906 END(trace_page_fault)
914 /* the function address is in %gs's slot on the stack */
926 movl $(__KERNEL_PERCPU), %ecx
930 movl PT_GS(%esp), %edi # get the function address
931 movl PT_ORIG_EAX(%esp), %edx # get the error code
932 movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart
935 movl $(__USER_DS), %ecx
939 movl %esp, %eax # pt_regs pointer
941 jmp ret_from_exception
945 * Debug traps and NMI can happen at the one SYSENTER instruction
946 * that sets up the real kernel stack. Check here, since we can't
947 * allow the wrong stack to be used.
949 * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
950 * already pushed 3 words if it hits on the sysenter instruction:
951 * eflags, cs and eip.
953 * We just load the right stack, and push the three (known) values
954 * by hand onto the new stack - while updating the return eip past
955 * the instruction that would have done it for sysenter.
957 .macro FIX_STACK offset ok label
958 cmpw $__KERNEL_CS, 4(%esp)
961 movl TSS_sysenter_sp0 + \offset(%esp), %esp
964 pushl $sysenter_past_esp
969 cmpl $entry_SYSENTER_32, (%esp)
970 jne debug_stack_correct
971 FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
973 pushl $-1 # mark this as an int
976 xorl %edx, %edx # error code 0
977 movl %esp, %eax # pt_regs pointer
979 jmp ret_from_exception
983 * NMI is doubly nasty. It can happen _while_ we're handling
984 * a debug fault, and the debug fault hasn't yet been able to
985 * clear up the stack. So we first check whether we got an
986 * NMI on the sysenter entry path, but after that we need to
987 * check whether we got an NMI on the debug path where the debug
988 * fault happened on the sysenter path.
992 #ifdef CONFIG_X86_ESPFIX32
995 cmpw $__ESPFIX_SS, %ax
999 cmpl $entry_SYSENTER_32, (%esp)
1004 * Do not access memory above the end of our stack page,
1005 * it might not exist.
1007 andl $(THREAD_SIZE-1), %eax
1008 cmpl $(THREAD_SIZE-20), %eax
1010 jae nmi_stack_correct
1011 cmpl $entry_SYSENTER_32, 12(%esp)
1012 je nmi_debug_stack_check
1016 xorl %edx, %edx # zero error code
1017 movl %esp, %eax # pt_regs pointer
1019 jmp restore_all_notrace
1022 FIX_STACK 12, nmi_stack_correct, 1
1023 jmp nmi_stack_correct
1025 nmi_debug_stack_check:
1026 cmpw $__KERNEL_CS, 16(%esp)
1027 jne nmi_stack_correct
1029 jb nmi_stack_correct
1030 cmpl $debug_esp_fix_insn, (%esp)
1031 ja nmi_stack_correct
1032 FIX_STACK 24, nmi_stack_correct, 1
1033 jmp nmi_stack_correct
1035 #ifdef CONFIG_X86_ESPFIX32
1038 * create the pointer to lss back
1043 /* copy the iret frame of 12 bytes */
1049 FIXUP_ESPFIX_STACK # %eax == %esp
1050 xorl %edx, %edx # zero error code
1053 lss 12+4(%esp), %esp # back to espfix stack
1060 pushl $-1 # mark this as an int
1063 xorl %edx, %edx # zero error code
1064 movl %esp, %eax # pt_regs pointer
1066 jmp ret_from_exception
1069 ENTRY(general_protection)
1070 pushl $do_general_protection
1072 END(general_protection)
1074 #ifdef CONFIG_KVM_GUEST
1075 ENTRY(async_page_fault)
1077 pushl $do_async_page_fault
1079 END(async_page_fault)