blob: 48ef7bb32c4269331ceb2332b8e6bf5faffcb3de [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ingo Molnara49976d2015-06-08 09:49:11 +02002 * Copyright (C) 1991,1992 Linus Torvalds
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Ingo Molnara49976d2015-06-08 09:49:11 +02004 * entry_32.S contains the system-call and low-level fault and trap handling routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Andy Lutomirski39e87012015-10-05 17:48:13 -07006 * Stack layout while running C code:
Ingo Molnara49976d2015-06-08 09:49:11 +02007 * 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(),
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * ptrace.c and ptrace.h
11 *
12 * 0(%esp) - %ebx
13 * 4(%esp) - %ecx
14 * 8(%esp) - %edx
Denys Vlasenko9b47feb2015-06-08 22:35:33 +020015 * C(%esp) - %esi
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * 10(%esp) - %edi
17 * 14(%esp) - %ebp
18 * 18(%esp) - %eax
19 * 1C(%esp) - %ds
20 * 20(%esp) - %es
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +010021 * 24(%esp) - %fs
Tejun Heoccbeed32009-02-09 22:17:40 +090022 * 28(%esp) - %gs saved iff !CONFIG_X86_32_LAZY_GS
23 * 2C(%esp) - orig_eax
24 * 30(%esp) - %eip
25 * 34(%esp) - %cs
26 * 38(%esp) - %eflags
27 * 3C(%esp) - %oldesp
28 * 40(%esp) - %oldss
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/linkage.h>
Eric Parisd7e75282012-01-03 14:23:06 -050032#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/thread_info.h>
Ingo Molnar55f327f2006-07-03 00:24:43 -070034#include <asm/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/errno.h>
36#include <asm/segment.h>
37#include <asm/smp.h>
Stas Sergeevbe44d2a2006-12-07 02:14:01 +010038#include <asm/percpu.h>
Cyrill Gorcunovab68ed92008-03-25 22:16:32 +030039#include <asm/processor-flags.h>
Thomas Gleixner9b7dc562008-05-02 20:10:09 +020040#include <asm/irq_vectors.h>
Borislav Petkovcd4d09e2016-01-26 22:12:04 +010041#include <asm/cpufeatures.h>
Andy Lutomirskib4ca46e2011-08-25 16:10:33 -040042#include <asm/alternative-asm.h>
H. Peter Anvin6837a542012-04-20 12:19:50 -070043#include <asm/asm.h>
H. Peter Anvine59d1b02012-09-21 13:58:10 -070044#include <asm/smap.h>
Josh Poimboeuf4d516f42016-09-21 16:04:01 -050045#include <asm/frame.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jiri Olsaea714542011-03-07 19:10:39 +010047 .section .entry.text, "ax"
48
Rusty Russell139ec7c2006-12-07 02:14:08 +010049/*
50 * We use macros for low-level operations which need to be overridden
51 * for paravirtualization. The following will never clobber any registers:
52 * INTERRUPT_RETURN (aka. "iret")
53 * GET_CR0_INTO_EAX (aka. "movl %cr0, %eax")
Jeremy Fitzhardinged75cd222008-06-25 00:19:26 -040054 * ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
Rusty Russell139ec7c2006-12-07 02:14:08 +010055 *
56 * For DISABLE_INTERRUPTS/ENABLE_INTERRUPTS (aka "cli"/"sti"), you must
57 * specify what registers can be overwritten (CLBR_NONE, CLBR_EAX/EDX/ECX/ANY).
58 * Allowing a register to be clobbered can shrink the paravirt replacement
59 * enough to patch inline, increasing performance.
60 */
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#ifdef CONFIG_PREEMPT
Ingo Molnara49976d2015-06-08 09:49:11 +020063# define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#else
Ingo Molnara49976d2015-06-08 09:49:11 +020065# define preempt_stop(clobbers)
66# define resume_kernel restore_all
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#endif
68
Ingo Molnar55f327f2006-07-03 00:24:43 -070069.macro TRACE_IRQS_IRET
70#ifdef CONFIG_TRACE_IRQFLAGS
Ingo Molnara49976d2015-06-08 09:49:11 +020071 testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off?
72 jz 1f
Ingo Molnar55f327f2006-07-03 00:24:43 -070073 TRACE_IRQS_ON
741:
75#endif
76.endm
77
Tejun Heoccbeed32009-02-09 22:17:40 +090078/*
79 * User gs save/restore
80 *
81 * %gs is used for userland TLS and kernel only uses it for stack
82 * canary which is required to be at %gs:20 by gcc. Read the comment
83 * at the top of stackprotector.h for more info.
84 *
85 * Local labels 98 and 99 are used.
86 */
87#ifdef CONFIG_X86_32_LAZY_GS
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Tejun Heoccbeed32009-02-09 22:17:40 +090089 /* unfortunately push/pop can't be no-op */
90.macro PUSH_GS
Ingo Molnara49976d2015-06-08 09:49:11 +020091 pushl $0
Tejun Heoccbeed32009-02-09 22:17:40 +090092.endm
93.macro POP_GS pop=0
Ingo Molnara49976d2015-06-08 09:49:11 +020094 addl $(4 + \pop), %esp
Tejun Heoccbeed32009-02-09 22:17:40 +090095.endm
96.macro POP_GS_EX
97.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Tejun Heoccbeed32009-02-09 22:17:40 +090099 /* all the rest are no-op */
100.macro PTGS_TO_GS
101.endm
102.macro PTGS_TO_GS_EX
103.endm
104.macro GS_TO_REG reg
105.endm
106.macro REG_TO_PTGS reg
107.endm
108.macro SET_KERNEL_GS reg
109.endm
110
111#else /* CONFIG_X86_32_LAZY_GS */
112
113.macro PUSH_GS
Ingo Molnara49976d2015-06-08 09:49:11 +0200114 pushl %gs
Tejun Heoccbeed32009-02-09 22:17:40 +0900115.endm
116
117.macro POP_GS pop=0
Ingo Molnara49976d2015-06-08 09:49:11 +020011898: popl %gs
Tejun Heoccbeed32009-02-09 22:17:40 +0900119 .if \pop <> 0
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200120 add $\pop, %esp
Tejun Heoccbeed32009-02-09 22:17:40 +0900121 .endif
122.endm
123.macro POP_GS_EX
124.pushsection .fixup, "ax"
Ingo Molnara49976d2015-06-08 09:49:11 +020012599: movl $0, (%esp)
126 jmp 98b
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100127.popsection
Ingo Molnara49976d2015-06-08 09:49:11 +0200128 _ASM_EXTABLE(98b, 99b)
Tejun Heoccbeed32009-02-09 22:17:40 +0900129.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Tejun Heoccbeed32009-02-09 22:17:40 +0900131.macro PTGS_TO_GS
Ingo Molnara49976d2015-06-08 09:49:11 +020013298: mov PT_GS(%esp), %gs
Tejun Heoccbeed32009-02-09 22:17:40 +0900133.endm
134.macro PTGS_TO_GS_EX
135.pushsection .fixup, "ax"
Ingo Molnara49976d2015-06-08 09:49:11 +020013699: movl $0, PT_GS(%esp)
137 jmp 98b
Tejun Heoccbeed32009-02-09 22:17:40 +0900138.popsection
Ingo Molnara49976d2015-06-08 09:49:11 +0200139 _ASM_EXTABLE(98b, 99b)
Tejun Heoccbeed32009-02-09 22:17:40 +0900140.endm
141
142.macro GS_TO_REG reg
Ingo Molnara49976d2015-06-08 09:49:11 +0200143 movl %gs, \reg
Tejun Heoccbeed32009-02-09 22:17:40 +0900144.endm
145.macro REG_TO_PTGS reg
Ingo Molnara49976d2015-06-08 09:49:11 +0200146 movl \reg, PT_GS(%esp)
Tejun Heoccbeed32009-02-09 22:17:40 +0900147.endm
148.macro SET_KERNEL_GS reg
Ingo Molnara49976d2015-06-08 09:49:11 +0200149 movl $(__KERNEL_STACK_CANARY), \reg
150 movl \reg, %gs
Tejun Heoccbeed32009-02-09 22:17:40 +0900151.endm
152
Ingo Molnara49976d2015-06-08 09:49:11 +0200153#endif /* CONFIG_X86_32_LAZY_GS */
Tejun Heoccbeed32009-02-09 22:17:40 +0900154
Andy Lutomirski150ac782015-10-05 17:48:14 -0700155.macro SAVE_ALL pt_regs_ax=%eax
Tejun Heof0d96112009-02-09 22:17:40 +0900156 cld
Tejun Heoccbeed32009-02-09 22:17:40 +0900157 PUSH_GS
Ingo Molnara49976d2015-06-08 09:49:11 +0200158 pushl %fs
159 pushl %es
160 pushl %ds
Andy Lutomirski150ac782015-10-05 17:48:14 -0700161 pushl \pt_regs_ax
Ingo Molnara49976d2015-06-08 09:49:11 +0200162 pushl %ebp
163 pushl %edi
164 pushl %esi
165 pushl %edx
166 pushl %ecx
167 pushl %ebx
168 movl $(__USER_DS), %edx
169 movl %edx, %ds
170 movl %edx, %es
171 movl $(__KERNEL_PERCPU), %edx
172 movl %edx, %fs
Tejun Heoccbeed32009-02-09 22:17:40 +0900173 SET_KERNEL_GS %edx
Tejun Heof0d96112009-02-09 22:17:40 +0900174.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500176/*
177 * This is a sneaky trick to help the unwinder find pt_regs on the stack. The
178 * frame pointer is replaced with an encoded pointer to pt_regs. The encoding
179 * is just setting the LSB, which makes it an invalid stack address and is also
180 * a signal to the unwinder that it's a pt_regs pointer in disguise.
181 *
182 * NOTE: This macro must be used *after* SAVE_ALL because it corrupts the
183 * original rbp.
184 */
185.macro ENCODE_FRAME_POINTER
186#ifdef CONFIG_FRAME_POINTER
187 mov %esp, %ebp
188 orl $0x1, %ebp
189#endif
190.endm
191
Tejun Heof0d96112009-02-09 22:17:40 +0900192.macro RESTORE_INT_REGS
Ingo Molnara49976d2015-06-08 09:49:11 +0200193 popl %ebx
194 popl %ecx
195 popl %edx
196 popl %esi
197 popl %edi
198 popl %ebp
199 popl %eax
Tejun Heof0d96112009-02-09 22:17:40 +0900200.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Tejun Heoccbeed32009-02-09 22:17:40 +0900202.macro RESTORE_REGS pop=0
Tejun Heof0d96112009-02-09 22:17:40 +0900203 RESTORE_INT_REGS
Ingo Molnara49976d2015-06-08 09:49:11 +02002041: popl %ds
2052: popl %es
2063: popl %fs
Tejun Heoccbeed32009-02-09 22:17:40 +0900207 POP_GS \pop
Tejun Heof0d96112009-02-09 22:17:40 +0900208.pushsection .fixup, "ax"
Ingo Molnara49976d2015-06-08 09:49:11 +02002094: movl $0, (%esp)
210 jmp 1b
2115: movl $0, (%esp)
212 jmp 2b
2136: movl $0, (%esp)
214 jmp 3b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215.popsection
Ingo Molnara49976d2015-06-08 09:49:11 +0200216 _ASM_EXTABLE(1b, 4b)
217 _ASM_EXTABLE(2b, 5b)
218 _ASM_EXTABLE(3b, 6b)
Tejun Heoccbeed32009-02-09 22:17:40 +0900219 POP_GS_EX
Tejun Heof0d96112009-02-09 22:17:40 +0900220.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Brian Gerst01003012016-08-13 12:38:19 -0400222/*
223 * %eax: prev task
224 * %edx: next task
225 */
226ENTRY(__switch_to_asm)
227 /*
228 * Save callee-saved registers
229 * This must match the order in struct inactive_task_frame
230 */
231 pushl %ebp
232 pushl %ebx
233 pushl %edi
234 pushl %esi
235
236 /* switch stack */
237 movl %esp, TASK_threadsp(%eax)
238 movl TASK_threadsp(%edx), %esp
239
240#ifdef CONFIG_CC_STACKPROTECTOR
241 movl TASK_stack_canary(%edx), %ebx
242 movl %ebx, PER_CPU_VAR(stack_canary)+stack_canary_offset
243#endif
244
245 /* restore callee-saved registers */
246 popl %esi
247 popl %edi
248 popl %ebx
249 popl %ebp
250
251 jmp __switch_to
252END(__switch_to_asm)
253
254/*
Josh Poimboeufebd57492017-05-23 10:37:29 -0500255 * The unwinder expects the last frame on the stack to always be at the same
256 * offset from the end of the page, which allows it to validate the stack.
257 * Calling schedule_tail() directly would break that convention because its an
258 * asmlinkage function so its argument has to be pushed on the stack. This
259 * wrapper creates a proper "end of stack" frame header before the call.
260 */
261ENTRY(schedule_tail_wrapper)
262 FRAME_BEGIN
263
264 pushl %eax
265 call schedule_tail
266 popl %eax
267
268 FRAME_END
269 ret
270ENDPROC(schedule_tail_wrapper)
271/*
Brian Gerst01003012016-08-13 12:38:19 -0400272 * A newly forked process directly context switches into this address.
273 *
274 * eax: prev task we switched from
Brian Gerst616d2482016-08-13 12:38:20 -0400275 * ebx: kernel thread func (NULL for user thread)
276 * edi: kernel thread arg
Brian Gerst01003012016-08-13 12:38:19 -0400277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278ENTRY(ret_from_fork)
Josh Poimboeufebd57492017-05-23 10:37:29 -0500279 call schedule_tail_wrapper
Andy Lutomirski39e87012015-10-05 17:48:13 -0700280
Brian Gerst616d2482016-08-13 12:38:20 -0400281 testl %ebx, %ebx
282 jnz 1f /* kernel threads are uncommon */
283
2842:
Andy Lutomirski39e87012015-10-05 17:48:13 -0700285 /* When we fork, we trace the syscall return in the child, too. */
Josh Poimboeufebd57492017-05-23 10:37:29 -0500286 movl %esp, %eax
Andy Lutomirski39e87012015-10-05 17:48:13 -0700287 call syscall_return_slowpath
288 jmp restore_all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Brian Gerst616d2482016-08-13 12:38:20 -0400290 /* kernel thread */
2911: movl %edi, %eax
292 call *%ebx
Andy Lutomirski39e87012015-10-05 17:48:13 -0700293 /*
Brian Gerst616d2482016-08-13 12:38:20 -0400294 * A kernel thread is allowed to return here after successfully
295 * calling do_execve(). Exit to userspace to complete the execve()
296 * syscall.
Andy Lutomirski39e87012015-10-05 17:48:13 -0700297 */
Brian Gerst616d2482016-08-13 12:38:20 -0400298 movl $0, PT_EAX(%esp)
299 jmp 2b
300END(ret_from_fork)
Al Viro6783eaa22012-08-02 23:05:11 +0400301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
303 * Return to user mode is not as complex as all this looks,
304 * but we want the default path for a system call return to
305 * go as quickly as possible which is why some of this is
306 * less clear than it otherwise should be.
307 */
308
309 # userspace resumption stub bypassing syscall exit tracing
310 ALIGN
311ret_from_exception:
Rusty Russell139ec7c2006-12-07 02:14:08 +0100312 preempt_stop(CLBR_ANY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313ret_from_intr:
Dmitry Adamushko29a2e282012-03-22 21:39:25 +0100314#ifdef CONFIG_VM86
Ingo Molnara49976d2015-06-08 09:49:11 +0200315 movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
316 movb PT_CS(%esp), %al
317 andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
Dmitry Adamushko29a2e282012-03-22 21:39:25 +0100318#else
319 /*
Al Viro6783eaa22012-08-02 23:05:11 +0400320 * We can be coming here from child spawned by kernel_thread().
Dmitry Adamushko29a2e282012-03-22 21:39:25 +0100321 */
Ingo Molnara49976d2015-06-08 09:49:11 +0200322 movl PT_CS(%esp), %eax
323 andl $SEGMENT_RPL_MASK, %eax
Dmitry Adamushko29a2e282012-03-22 21:39:25 +0100324#endif
Ingo Molnara49976d2015-06-08 09:49:11 +0200325 cmpl $USER_RPL, %eax
326 jb resume_kernel # not returning to v8086 or userspace
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328ENTRY(resume_userspace)
Andy Lutomirski5d73fc72015-07-31 14:41:09 -0700329 DISABLE_INTERRUPTS(CLBR_ANY)
Peter Zijlstrae32e58a2008-06-06 10:14:08 +0200330 TRACE_IRQS_OFF
Andy Lutomirski5d73fc72015-07-31 14:41:09 -0700331 movl %esp, %eax
332 call prepare_exit_to_usermode
Ingo Molnara49976d2015-06-08 09:49:11 +0200333 jmp restore_all
Jan Beulich47a55cd2007-02-13 13:26:24 +0100334END(ret_from_exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336#ifdef CONFIG_PREEMPT
337ENTRY(resume_kernel)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100338 DISABLE_INTERRUPTS(CLBR_ANY)
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500339.Lneed_resched:
Ingo Molnara49976d2015-06-08 09:49:11 +0200340 cmpl $0, PER_CPU_VAR(__preempt_count)
341 jnz restore_all
342 testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
343 jz restore_all
344 call preempt_schedule_irq
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500345 jmp .Lneed_resched
Jan Beulich47a55cd2007-02-13 13:26:24 +0100346END(resume_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#endif
348
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800349GLOBAL(__begin_SYSENTER_singlestep_region)
350/*
351 * All code from here through __end_SYSENTER_singlestep_region is subject
352 * to being single-stepped if a user program sets TF and executes SYSENTER.
353 * There is absolutely nothing that we can do to prevent this from happening
354 * (thanks Intel!). To keep our handling of this situation as simple as
355 * possible, we handle TF just like AC and NT, except that our #DB handler
356 * will ignore all of the single-step traps generated in this range.
357 */
358
359#ifdef CONFIG_XEN
360/*
361 * Xen doesn't set %esp to be precisely what the normal SYSENTER
362 * entry point expects, so fix it up before using the normal path.
363 */
364ENTRY(xen_sysenter_target)
365 addl $5*4, %esp /* remove xen-provided frame */
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500366 jmp .Lsysenter_past_esp
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800367#endif
368
Andy Lutomirskifda57b22016-03-09 19:00:35 -0800369/*
370 * 32-bit SYSENTER entry.
371 *
372 * 32-bit system calls through the vDSO's __kernel_vsyscall enter here
373 * if X86_FEATURE_SEP is available. This is the preferred system call
374 * entry on 32-bit systems.
375 *
376 * The SYSENTER instruction, in principle, should *only* occur in the
377 * vDSO. In practice, a small number of Android devices were shipped
378 * with a copy of Bionic that inlined a SYSENTER instruction. This
379 * never happened in any of Google's Bionic versions -- it only happened
380 * in a narrow range of Intel-provided versions.
381 *
382 * SYSENTER loads SS, ESP, CS, and EIP from previously programmed MSRs.
383 * IF and VM in RFLAGS are cleared (IOW: interrupts are off).
384 * SYSENTER does not save anything on the stack,
385 * and does not save old EIP (!!!), ESP, or EFLAGS.
386 *
387 * To avoid losing track of EFLAGS.VM (and thus potentially corrupting
388 * user and/or vm86 state), we explicitly disable the SYSENTER
389 * instruction in vm86 mode by reprogramming the MSRs.
390 *
391 * Arguments:
392 * eax system call number
393 * ebx arg1
394 * ecx arg2
395 * edx arg3
396 * esi arg4
397 * edi arg5
398 * ebp user stack
399 * 0(%ebp) arg6
400 */
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +0200401ENTRY(entry_SYSENTER_32)
Ingo Molnara49976d2015-06-08 09:49:11 +0200402 movl TSS_sysenter_sp0(%esp), %esp
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500403.Lsysenter_past_esp:
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700404 pushl $__USER_DS /* pt_regs->ss */
Andy Lutomirski30bfa7b2015-12-16 23:18:48 -0800405 pushl %ebp /* pt_regs->sp (stashed in bp) */
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700406 pushfl /* pt_regs->flags (except IF = 0) */
407 orl $X86_EFLAGS_IF, (%esp) /* Fix IF */
408 pushl $__USER_CS /* pt_regs->cs */
409 pushl $0 /* pt_regs->ip = 0 (placeholder) */
410 pushl %eax /* pt_regs->orig_ax */
411 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
412
Ingo Molnar55f327f2006-07-03 00:24:43 -0700413 /*
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800414 * SYSENTER doesn't filter flags, so we need to clear NT, AC
415 * and TF ourselves. To save a few cycles, we can check whether
Andy Lutomirski67f590e2016-03-09 19:00:26 -0800416 * either was set instead of doing an unconditional popfq.
417 * This needs to happen before enabling interrupts so that
418 * we don't get preempted with NT set.
419 *
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800420 * If TF is set, we will single-step all the way to here -- do_debug
421 * will ignore all the traps. (Yes, this is slow, but so is
422 * single-stepping in general. This allows us to avoid having
423 * a more complicated code to handle the case where a user program
424 * forces us to single-step through the SYSENTER entry code.)
425 *
Andy Lutomirski67f590e2016-03-09 19:00:26 -0800426 * NB.: .Lsysenter_fix_flags is a label with the code under it moved
427 * out-of-line as an optimization: NT is unlikely to be set in the
428 * majority of the cases and instead of polluting the I$ unnecessarily,
429 * we're keeping that code behind a branch which will predict as
430 * not-taken and therefore its instructions won't be fetched.
431 */
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800432 testl $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, PT_EFLAGS(%esp)
Andy Lutomirski67f590e2016-03-09 19:00:26 -0800433 jnz .Lsysenter_fix_flags
434.Lsysenter_flags_fixed:
435
436 /*
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700437 * User mode is traced as though IRQs are on, and SYSENTER
438 * turned them off.
Ingo Molnar55f327f2006-07-03 00:24:43 -0700439 */
Ingo Molnar55f327f2006-07-03 00:24:43 -0700440 TRACE_IRQS_OFF
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700441
442 movl %esp, %eax
443 call do_fast_syscall_32
Boris Ostrovsky91e2eea2015-11-19 16:55:45 -0500444 /* XEN PV guests always use IRET path */
445 ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
446 "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700447
448/* Opportunistic SYSEXIT */
449 TRACE_IRQS_ON /* User mode traces as IRQs on. */
450 movl PT_EIP(%esp), %edx /* pt_regs->ip */
451 movl PT_OLDESP(%esp), %ecx /* pt_regs->sp */
Andy Lutomirski3bd29512015-10-16 15:42:55 -07004521: mov PT_FS(%esp), %fs
453 PTGS_TO_GS
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700454 popl %ebx /* pt_regs->bx */
455 addl $2*4, %esp /* skip pt_regs->cx and pt_regs->dx */
456 popl %esi /* pt_regs->si */
457 popl %edi /* pt_regs->di */
458 popl %ebp /* pt_regs->bp */
459 popl %eax /* pt_regs->ax */
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700460
461 /*
Andy Lutomirskic2c9b522016-03-09 19:00:27 -0800462 * Restore all flags except IF. (We restore IF separately because
463 * STI gives a one-instruction window in which we won't be interrupted,
464 * whereas POPF does not.)
465 */
466 addl $PT_EFLAGS-PT_DS, %esp /* point esp at pt_regs->flags */
467 btr $X86_EFLAGS_IF_BIT, (%esp)
468 popfl
469
470 /*
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700471 * Return back to the vDSO, which will pop ecx and edx.
472 * Don't bother with DS and ES (they already contain __USER_DS).
473 */
Boris Ostrovsky88c15ec2015-11-19 16:55:46 -0500474 sti
475 sysexit
Roland McGrathaf0575b2008-06-24 04:16:52 -0700476
Ingo Molnara49976d2015-06-08 09:49:11 +0200477.pushsection .fixup, "ax"
4782: movl $0, PT_FS(%esp)
479 jmp 1b
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100480.popsection
Ingo Molnara49976d2015-06-08 09:49:11 +0200481 _ASM_EXTABLE(1b, 2b)
Tejun Heoccbeed32009-02-09 22:17:40 +0900482 PTGS_TO_GS_EX
Andy Lutomirski67f590e2016-03-09 19:00:26 -0800483
484.Lsysenter_fix_flags:
485 pushl $X86_EFLAGS_FIXED
486 popfl
487 jmp .Lsysenter_flags_fixed
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800488GLOBAL(__end_SYSENTER_singlestep_region)
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +0200489ENDPROC(entry_SYSENTER_32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Andy Lutomirskifda57b22016-03-09 19:00:35 -0800491/*
492 * 32-bit legacy system call entry.
493 *
494 * 32-bit x86 Linux system calls traditionally used the INT $0x80
495 * instruction. INT $0x80 lands here.
496 *
497 * This entry point can be used by any 32-bit perform system calls.
498 * Instances of INT $0x80 can be found inline in various programs and
499 * libraries. It is also used by the vDSO's __kernel_vsyscall
500 * fallback for hardware that doesn't support a faster entry method.
501 * Restarted 32-bit system calls also fall back to INT $0x80
502 * regardless of what instruction was originally used to do the system
503 * call. (64-bit programs can use INT $0x80 as well, but they can
504 * only run on 64-bit kernels and therefore land in
505 * entry_INT80_compat.)
506 *
507 * This is considered a slow path. It is not used by most libc
508 * implementations on modern hardware except during process startup.
509 *
510 * Arguments:
511 * eax system call number
512 * ebx arg1
513 * ecx arg2
514 * edx arg3
515 * esi arg4
516 * edi arg5
517 * ebp arg6
518 */
Ingo Molnarb2502b42015-06-08 08:42:03 +0200519ENTRY(entry_INT80_32)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700520 ASM_CLAC
Andy Lutomirski150ac782015-10-05 17:48:14 -0700521 pushl %eax /* pt_regs->orig_ax */
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700522 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
Andy Lutomirski150ac782015-10-05 17:48:14 -0700523
524 /*
Andy Lutomirskia798f092016-03-09 13:24:32 -0800525 * User mode is traced as though IRQs are on, and the interrupt gate
526 * turned them off.
Andy Lutomirski150ac782015-10-05 17:48:14 -0700527 */
Andy Lutomirskia798f092016-03-09 13:24:32 -0800528 TRACE_IRQS_OFF
Andy Lutomirski150ac782015-10-05 17:48:14 -0700529
530 movl %esp, %eax
Andy Lutomirskia798f092016-03-09 13:24:32 -0800531 call do_int80_syscall_32
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700532.Lsyscall_32_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534restore_all:
Alexander van Heukelum2e04bc72009-06-18 00:35:57 +0200535 TRACE_IRQS_IRET
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500536.Lrestore_all_notrace:
H. Peter Anvin34273f42014-05-04 10:36:22 -0700537#ifdef CONFIG_X86_ESPFIX32
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500538 ALTERNATIVE "jmp .Lrestore_nocheck", "", X86_BUG_ESPFIX
Andy Lutomirski58a5aac2016-02-29 15:50:19 -0800539
Ingo Molnara49976d2015-06-08 09:49:11 +0200540 movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
541 /*
542 * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
543 * are returning to the kernel.
544 * See comments in process.c:copy_thread() for details.
545 */
546 movb PT_OLDSS(%esp), %ah
547 movb PT_CS(%esp), %al
548 andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
549 cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500550 je .Lldt_ss # returning to user-space with LDT SS
H. Peter Anvin34273f42014-05-04 10:36:22 -0700551#endif
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500552.Lrestore_nocheck:
Ingo Molnara49976d2015-06-08 09:49:11 +0200553 RESTORE_REGS 4 # skip orig_eax/error_code
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500554.Lirq_return:
Ingo Molnar3701d8632008-02-09 23:24:08 +0100555 INTERRUPT_RETURN
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500556
Ingo Molnara49976d2015-06-08 09:49:11 +0200557.section .fixup, "ax"
558ENTRY(iret_exc )
559 pushl $0 # no error code
560 pushl $do_iret_error
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500561 jmp common_exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562.previous
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500563 _ASM_EXTABLE(.Lirq_return, iret_exc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
H. Peter Anvin34273f42014-05-04 10:36:22 -0700565#ifdef CONFIG_X86_ESPFIX32
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500566.Lldt_ss:
Alexander van Heukelumdc4c2a02009-06-18 00:35:58 +0200567/*
568 * Setup and switch to ESPFIX stack
569 *
570 * We're returning to userspace with a 16 bit stack. The CPU will not
571 * restore the high word of ESP for us on executing iret... This is an
572 * "official" bug of all the x86-compatible CPUs, which we can work
573 * around to make dosemu and wine happy. We do this by preloading the
574 * high word of ESP with the high word of the userspace ESP while
575 * compensating for the offset by changing to the ESPFIX segment with
576 * a base address that matches for the difference.
577 */
Brian Gerst72c511d2010-07-31 12:48:23 -0400578#define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
Ingo Molnara49976d2015-06-08 09:49:11 +0200579 mov %esp, %edx /* load kernel esp */
580 mov PT_OLDESP(%esp), %eax /* load userspace esp */
581 mov %dx, %ax /* eax: new kernel esp */
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200582 sub %eax, %edx /* offset (low word is 0) */
583 shr $16, %edx
Ingo Molnara49976d2015-06-08 09:49:11 +0200584 mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
585 mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
586 pushl $__ESPFIX_SS
587 pushl %eax /* new kernel esp */
588 /*
589 * Disable interrupts, but do not irqtrace this section: we
Alexander van Heukelum2e04bc72009-06-18 00:35:57 +0200590 * will soon execute iret and the tracer was already set to
Ingo Molnara49976d2015-06-08 09:49:11 +0200591 * the irqstate after the IRET:
592 */
Jan Beulichfdbd5182017-02-03 01:58:03 -0700593 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnara49976d2015-06-08 09:49:11 +0200594 lss (%esp), %esp /* switch to espfix segment */
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500595 jmp .Lrestore_nocheck
H. Peter Anvin34273f42014-05-04 10:36:22 -0700596#endif
Ingo Molnarb2502b42015-06-08 08:42:03 +0200597ENDPROC(entry_INT80_32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Tejun Heof0d96112009-02-09 22:17:40 +0900599.macro FIXUP_ESPFIX_STACK
Alexander van Heukelumdc4c2a02009-06-18 00:35:58 +0200600/*
601 * Switch back for ESPFIX stack to the normal zerobased stack
602 *
603 * We can't call C functions using the ESPFIX stack. This code reads
604 * the high word of the segment base from the GDT and swiches to the
605 * normal stack and adjusts ESP with the matching offset.
606 */
H. Peter Anvin34273f42014-05-04 10:36:22 -0700607#ifdef CONFIG_X86_ESPFIX32
Alexander van Heukelumdc4c2a02009-06-18 00:35:58 +0200608 /* fixup the stack */
Ingo Molnara49976d2015-06-08 09:49:11 +0200609 mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
610 mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200611 shl $16, %eax
Ingo Molnara49976d2015-06-08 09:49:11 +0200612 addl %esp, %eax /* the adjusted stack pointer */
613 pushl $__KERNEL_DS
614 pushl %eax
615 lss (%esp), %esp /* switch to the normal stack segment */
H. Peter Anvin34273f42014-05-04 10:36:22 -0700616#endif
Tejun Heof0d96112009-02-09 22:17:40 +0900617.endm
618.macro UNWIND_ESPFIX_STACK
H. Peter Anvin34273f42014-05-04 10:36:22 -0700619#ifdef CONFIG_X86_ESPFIX32
Ingo Molnara49976d2015-06-08 09:49:11 +0200620 movl %ss, %eax
Tejun Heof0d96112009-02-09 22:17:40 +0900621 /* see if on espfix stack */
Ingo Molnara49976d2015-06-08 09:49:11 +0200622 cmpw $__ESPFIX_SS, %ax
623 jne 27f
624 movl $__KERNEL_DS, %eax
625 movl %eax, %ds
626 movl %eax, %es
Tejun Heof0d96112009-02-09 22:17:40 +0900627 /* switch to normal stack */
628 FIXUP_ESPFIX_STACK
62927:
H. Peter Anvin34273f42014-05-04 10:36:22 -0700630#endif
Tejun Heof0d96112009-02-09 22:17:40 +0900631.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633/*
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200634 * Build the entry stubs with some assembler magic.
635 * We pack 1 stub into every 8-byte block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200637 .align 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638ENTRY(irq_entries_start)
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200639 vector=FIRST_EXTERNAL_VECTOR
640 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
Ingo Molnara49976d2015-06-08 09:49:11 +0200641 pushl $(~vector+0x80) /* Note: always in signed byte range */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200642 vector=vector+1
643 jmp common_interrupt
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200644 .align 8
645 .endr
Jan Beulich47a55cd2007-02-13 13:26:24 +0100646END(irq_entries_start)
647
Ingo Molnar55f327f2006-07-03 00:24:43 -0700648/*
649 * the CPU automatically disables interrupts when executing an IRQ vector,
650 * so IRQ-flags tracing has to follow that:
651 */
H. Peter Anvinb7c62442008-11-11 13:24:58 -0800652 .p2align CONFIG_X86_L1_CACHE_SHIFT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653common_interrupt:
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700654 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200655 addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500657 ENCODE_FRAME_POINTER
Ingo Molnar55f327f2006-07-03 00:24:43 -0700658 TRACE_IRQS_OFF
Ingo Molnara49976d2015-06-08 09:49:11 +0200659 movl %esp, %eax
660 call do_IRQ
661 jmp ret_from_intr
Jan Beulich47a55cd2007-02-13 13:26:24 +0100662ENDPROC(common_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Tejun Heo02cf94c2009-01-21 17:26:06 +0900664#define BUILD_INTERRUPT3(name, nr, fn) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665ENTRY(name) \
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700666 ASM_CLAC; \
Ingo Molnara49976d2015-06-08 09:49:11 +0200667 pushl $~(nr); \
Jan Beulichfe7cacc2006-06-26 13:57:44 +0200668 SAVE_ALL; \
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500669 ENCODE_FRAME_POINTER; \
Ingo Molnar55f327f2006-07-03 00:24:43 -0700670 TRACE_IRQS_OFF \
Ingo Molnara49976d2015-06-08 09:49:11 +0200671 movl %esp, %eax; \
672 call fn; \
673 jmp ret_from_intr; \
Jan Beulich47a55cd2007-02-13 13:26:24 +0100674ENDPROC(name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Seiji Aguchicf910e82013-06-20 11:46:53 -0400676
677#ifdef CONFIG_TRACING
Ingo Molnara49976d2015-06-08 09:49:11 +0200678# define TRACE_BUILD_INTERRUPT(name, nr) BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
Seiji Aguchicf910e82013-06-20 11:46:53 -0400679#else
Ingo Molnara49976d2015-06-08 09:49:11 +0200680# define TRACE_BUILD_INTERRUPT(name, nr)
Seiji Aguchicf910e82013-06-20 11:46:53 -0400681#endif
682
Ingo Molnara49976d2015-06-08 09:49:11 +0200683#define BUILD_INTERRUPT(name, nr) \
684 BUILD_INTERRUPT3(name, nr, smp_##name); \
Seiji Aguchicf910e82013-06-20 11:46:53 -0400685 TRACE_BUILD_INTERRUPT(name, nr)
Tejun Heo02cf94c2009-01-21 17:26:06 +0900686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/* The include is where all of the SMP etc. interrupts come from */
Ingo Molnar1164dd02009-01-28 19:34:09 +0100688#include <asm/entry_arch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690ENTRY(coprocessor_error)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700691 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200692 pushl $0
693 pushl $do_coprocessor_error
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500694 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100695END(coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697ENTRY(simd_coprocessor_error)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700698 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200699 pushl $0
Brian Gerst40d2e762010-03-21 09:00:43 -0400700#ifdef CONFIG_X86_INVD_BUG
701 /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
Ingo Molnara49976d2015-06-08 09:49:11 +0200702 ALTERNATIVE "pushl $do_general_protection", \
703 "pushl $do_simd_coprocessor_error", \
Borislav Petkov8e65f6e2015-01-18 12:35:55 +0100704 X86_FEATURE_XMM
Brian Gerst40d2e762010-03-21 09:00:43 -0400705#else
Ingo Molnara49976d2015-06-08 09:49:11 +0200706 pushl $do_simd_coprocessor_error
Brian Gerst40d2e762010-03-21 09:00:43 -0400707#endif
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500708 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100709END(simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711ENTRY(device_not_available)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700712 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200713 pushl $-1 # mark this as an int
714 pushl $do_device_not_available
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500715 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100716END(device_not_available)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Rusty Russelld3561b72006-12-07 02:14:07 +0100718#ifdef CONFIG_PARAVIRT
719ENTRY(native_iret)
Ingo Molnar3701d8632008-02-09 23:24:08 +0100720 iret
H. Peter Anvin6837a542012-04-20 12:19:50 -0700721 _ASM_EXTABLE(native_iret, iret_exc)
Jan Beulich47a55cd2007-02-13 13:26:24 +0100722END(native_iret)
Rusty Russelld3561b72006-12-07 02:14:07 +0100723#endif
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725ENTRY(overflow)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700726 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200727 pushl $0
728 pushl $do_overflow
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500729 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100730END(overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732ENTRY(bounds)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700733 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200734 pushl $0
735 pushl $do_bounds
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500736 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100737END(bounds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739ENTRY(invalid_op)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700740 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200741 pushl $0
742 pushl $do_invalid_op
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500743 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100744END(invalid_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746ENTRY(coprocessor_segment_overrun)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700747 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200748 pushl $0
749 pushl $do_coprocessor_segment_overrun
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500750 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100751END(coprocessor_segment_overrun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753ENTRY(invalid_TSS)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700754 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200755 pushl $do_invalid_TSS
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500756 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100757END(invalid_TSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759ENTRY(segment_not_present)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700760 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200761 pushl $do_segment_not_present
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500762 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100763END(segment_not_present)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765ENTRY(stack_segment)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700766 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200767 pushl $do_stack_segment
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500768 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100769END(stack_segment)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771ENTRY(alignment_check)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700772 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200773 pushl $do_alignment_check
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500774 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100775END(alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Prasanna S.Pd28c4392006-09-26 10:52:34 +0200777ENTRY(divide_error)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700778 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200779 pushl $0 # no error code
780 pushl $do_divide_error
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500781 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100782END(divide_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784#ifdef CONFIG_X86_MCE
785ENTRY(machine_check)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700786 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200787 pushl $0
788 pushl machine_check_vector
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500789 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100790END(machine_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791#endif
792
793ENTRY(spurious_interrupt_bug)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700794 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200795 pushl $0
796 pushl $do_spurious_interrupt_bug
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500797 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100798END(spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700800#ifdef CONFIG_XEN
801ENTRY(xen_hypervisor_callback)
Ingo Molnara49976d2015-06-08 09:49:11 +0200802 pushl $-1 /* orig_ax = -1 => not a system call */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700803 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500804 ENCODE_FRAME_POINTER
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700805 TRACE_IRQS_OFF
Jeremy Fitzhardinge9ec2b802007-07-17 18:37:07 -0700806
Ingo Molnara49976d2015-06-08 09:49:11 +0200807 /*
808 * Check to see if we got the event in the critical
809 * region in xen_iret_direct, after we've reenabled
810 * events and checked for pending events. This simulates
811 * iret instruction's behaviour where it delivers a
812 * pending interrupt when enabling interrupts:
813 */
814 movl PT_EIP(%esp), %eax
815 cmpl $xen_iret_start_crit, %eax
816 jb 1f
817 cmpl $xen_iret_end_crit, %eax
818 jae 1f
Jeremy Fitzhardinge9ec2b802007-07-17 18:37:07 -0700819
Ingo Molnara49976d2015-06-08 09:49:11 +0200820 jmp xen_iret_crit_fixup
Jeremy Fitzhardinge9ec2b802007-07-17 18:37:07 -0700821
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700822ENTRY(xen_do_upcall)
Ingo Molnara49976d2015-06-08 09:49:11 +02008231: mov %esp, %eax
824 call xen_evtchn_do_upcall
David Vrabelfdfd8112015-02-19 15:23:17 +0000825#ifndef CONFIG_PREEMPT
Ingo Molnara49976d2015-06-08 09:49:11 +0200826 call xen_maybe_preempt_hcall
David Vrabelfdfd8112015-02-19 15:23:17 +0000827#endif
Ingo Molnara49976d2015-06-08 09:49:11 +0200828 jmp ret_from_intr
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700829ENDPROC(xen_hypervisor_callback)
830
Ingo Molnara49976d2015-06-08 09:49:11 +0200831/*
832 * Hypervisor uses this for application faults while it executes.
833 * We get here for two reasons:
834 * 1. Fault while reloading DS, ES, FS or GS
835 * 2. Fault while executing IRET
836 * Category 1 we fix up by reattempting the load, and zeroing the segment
837 * register if the load fails.
838 * Category 2 we fix up by jumping to do_iret_error. We cannot use the
839 * normal Linux return path in this case because if we use the IRET hypercall
840 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
841 * We distinguish between categories by maintaining a status value in EAX.
842 */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700843ENTRY(xen_failsafe_callback)
Ingo Molnara49976d2015-06-08 09:49:11 +0200844 pushl %eax
845 movl $1, %eax
8461: mov 4(%esp), %ds
8472: mov 8(%esp), %es
8483: mov 12(%esp), %fs
8494: mov 16(%esp), %gs
David Vrabela349e23d12012-10-19 17:29:07 +0100850 /* EAX == 0 => Category 1 (Bad segment)
851 EAX != 0 => Category 2 (Bad IRET) */
Ingo Molnara49976d2015-06-08 09:49:11 +0200852 testl %eax, %eax
853 popl %eax
854 lea 16(%esp), %esp
855 jz 5f
856 jmp iret_exc
8575: pushl $-1 /* orig_ax = -1 => not a system call */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700858 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500859 ENCODE_FRAME_POINTER
Ingo Molnara49976d2015-06-08 09:49:11 +0200860 jmp ret_from_exception
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700861
Ingo Molnara49976d2015-06-08 09:49:11 +0200862.section .fixup, "ax"
8636: xorl %eax, %eax
864 movl %eax, 4(%esp)
865 jmp 1b
8667: xorl %eax, %eax
867 movl %eax, 8(%esp)
868 jmp 2b
8698: xorl %eax, %eax
870 movl %eax, 12(%esp)
871 jmp 3b
8729: xorl %eax, %eax
873 movl %eax, 16(%esp)
874 jmp 4b
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700875.previous
Ingo Molnara49976d2015-06-08 09:49:11 +0200876 _ASM_EXTABLE(1b, 6b)
877 _ASM_EXTABLE(2b, 7b)
878 _ASM_EXTABLE(3b, 8b)
879 _ASM_EXTABLE(4b, 9b)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700880ENDPROC(xen_failsafe_callback)
881
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800882BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
Sheng Yang38e20b02010-05-14 12:40:51 +0100883 xen_evtchn_do_upcall)
884
Ingo Molnara49976d2015-06-08 09:49:11 +0200885#endif /* CONFIG_XEN */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700886
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800887#if IS_ENABLED(CONFIG_HYPERV)
888
889BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
890 hyperv_vector_handler)
891
892#endif /* CONFIG_HYPERV */
893
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400894#ifdef CONFIG_TRACING
895ENTRY(trace_page_fault)
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400896 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200897 pushl $trace_do_page_fault
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500898 jmp common_exception
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400899END(trace_page_fault)
900#endif
901
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100902ENTRY(page_fault)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700903 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200904 pushl $do_page_fault
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100905 ALIGN
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500906 jmp common_exception
907END(page_fault)
908
909common_exception:
Tejun Heoccbeed32009-02-09 22:17:40 +0900910 /* the function address is in %gs's slot on the stack */
Ingo Molnara49976d2015-06-08 09:49:11 +0200911 pushl %fs
912 pushl %es
913 pushl %ds
914 pushl %eax
915 pushl %ebp
916 pushl %edi
917 pushl %esi
918 pushl %edx
919 pushl %ecx
920 pushl %ebx
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500921 ENCODE_FRAME_POINTER
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100922 cld
Ingo Molnara49976d2015-06-08 09:49:11 +0200923 movl $(__KERNEL_PERCPU), %ecx
924 movl %ecx, %fs
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100925 UNWIND_ESPFIX_STACK
Tejun Heoccbeed32009-02-09 22:17:40 +0900926 GS_TO_REG %ecx
Ingo Molnara49976d2015-06-08 09:49:11 +0200927 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
Tejun Heoccbeed32009-02-09 22:17:40 +0900930 REG_TO_PTGS %ecx
931 SET_KERNEL_GS %ecx
Ingo Molnara49976d2015-06-08 09:49:11 +0200932 movl $(__USER_DS), %ecx
933 movl %ecx, %ds
934 movl %ecx, %es
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100935 TRACE_IRQS_OFF
Ingo Molnara49976d2015-06-08 09:49:11 +0200936 movl %esp, %eax # pt_regs pointer
937 call *%edi
938 jmp ret_from_exception
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500939END(common_exception)
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100940
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100941ENTRY(debug)
Andy Lutomirski75366562016-03-09 19:00:32 -0800942 /*
943 * #DB can happen at the first instruction of
944 * entry_SYSENTER_32 or in Xen's SYSENTER prologue. If this
945 * happens, then we will be running on a very small stack. We
946 * need to detect this condition and switch to the thread
947 * stack before calling any C code at all.
948 *
949 * If you edit this code, keep in mind that NMIs can happen in here.
950 */
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700951 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200952 pushl $-1 # mark this as an int
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100953 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500954 ENCODE_FRAME_POINTER
Ingo Molnara49976d2015-06-08 09:49:11 +0200955 xorl %edx, %edx # error code 0
956 movl %esp, %eax # pt_regs pointer
Andy Lutomirski75366562016-03-09 19:00:32 -0800957
958 /* Are we currently on the SYSENTER stack? */
959 PER_CPU(cpu_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx)
960 subl %eax, %ecx /* ecx = (end of SYSENTER_stack) - esp */
961 cmpl $SIZEOF_SYSENTER_stack, %ecx
962 jb .Ldebug_from_sysenter_stack
963
964 TRACE_IRQS_OFF
Ingo Molnara49976d2015-06-08 09:49:11 +0200965 call do_debug
966 jmp ret_from_exception
Andy Lutomirski75366562016-03-09 19:00:32 -0800967
968.Ldebug_from_sysenter_stack:
969 /* We're on the SYSENTER stack. Switch off. */
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500970 movl %esp, %ebx
Andy Lutomirski75366562016-03-09 19:00:32 -0800971 movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
972 TRACE_IRQS_OFF
973 call do_debug
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500974 movl %ebx, %esp
Andy Lutomirski75366562016-03-09 19:00:32 -0800975 jmp ret_from_exception
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100976END(debug)
977
978/*
Andy Lutomirski75366562016-03-09 19:00:32 -0800979 * NMI is doubly nasty. It can happen on the first instruction of
980 * entry_SYSENTER_32 (just like #DB), but it can also interrupt the beginning
981 * of the #DB handler even if that #DB in turn hit before entry_SYSENTER_32
982 * switched stacks. We handle both conditions by simply checking whether we
983 * interrupted kernel code running on the SYSENTER stack.
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100984 */
985ENTRY(nmi)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700986 ASM_CLAC
H. Peter Anvin34273f42014-05-04 10:36:22 -0700987#ifdef CONFIG_X86_ESPFIX32
Ingo Molnara49976d2015-06-08 09:49:11 +0200988 pushl %eax
989 movl %ss, %eax
990 cmpw $__ESPFIX_SS, %ax
991 popl %eax
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500992 je .Lnmi_espfix_stack
H. Peter Anvin34273f42014-05-04 10:36:22 -0700993#endif
Andy Lutomirski75366562016-03-09 19:00:32 -0800994
995 pushl %eax # pt_regs->orig_ax
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100996 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500997 ENCODE_FRAME_POINTER
Ingo Molnara49976d2015-06-08 09:49:11 +0200998 xorl %edx, %edx # zero error code
999 movl %esp, %eax # pt_regs pointer
Andy Lutomirski75366562016-03-09 19:00:32 -08001000
1001 /* Are we currently on the SYSENTER stack? */
1002 PER_CPU(cpu_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx)
1003 subl %eax, %ecx /* ecx = (end of SYSENTER_stack) - esp */
1004 cmpl $SIZEOF_SYSENTER_stack, %ecx
1005 jb .Lnmi_from_sysenter_stack
1006
1007 /* Not on SYSENTER stack. */
Ingo Molnara49976d2015-06-08 09:49:11 +02001008 call do_nmi
Josh Poimboeuf1b002552016-09-21 16:03:59 -05001009 jmp .Lrestore_all_notrace
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001010
Andy Lutomirski75366562016-03-09 19:00:32 -08001011.Lnmi_from_sysenter_stack:
1012 /*
1013 * We're on the SYSENTER stack. Switch off. No one (not even debug)
1014 * is using the thread stack right now, so it's safe for us to use it.
1015 */
Josh Poimboeuf946c1912016-10-20 11:34:40 -05001016 movl %esp, %ebx
Andy Lutomirski75366562016-03-09 19:00:32 -08001017 movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
1018 call do_nmi
Josh Poimboeuf946c1912016-10-20 11:34:40 -05001019 movl %ebx, %esp
Josh Poimboeuf1b002552016-09-21 16:03:59 -05001020 jmp .Lrestore_all_notrace
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001021
H. Peter Anvin34273f42014-05-04 10:36:22 -07001022#ifdef CONFIG_X86_ESPFIX32
Josh Poimboeuf1b002552016-09-21 16:03:59 -05001023.Lnmi_espfix_stack:
Ingo Molnar131484c2015-05-28 12:21:47 +02001024 /*
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001025 * create the pointer to lss back
1026 */
Ingo Molnara49976d2015-06-08 09:49:11 +02001027 pushl %ss
1028 pushl %esp
1029 addl $4, (%esp)
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001030 /* copy the iret frame of 12 bytes */
1031 .rept 3
Ingo Molnara49976d2015-06-08 09:49:11 +02001032 pushl 16(%esp)
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001033 .endr
Ingo Molnara49976d2015-06-08 09:49:11 +02001034 pushl %eax
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001035 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -05001036 ENCODE_FRAME_POINTER
Ingo Molnara49976d2015-06-08 09:49:11 +02001037 FIXUP_ESPFIX_STACK # %eax == %esp
1038 xorl %edx, %edx # zero error code
1039 call do_nmi
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001040 RESTORE_REGS
Ingo Molnara49976d2015-06-08 09:49:11 +02001041 lss 12+4(%esp), %esp # back to espfix stack
Josh Poimboeuf1b002552016-09-21 16:03:59 -05001042 jmp .Lirq_return
H. Peter Anvin34273f42014-05-04 10:36:22 -07001043#endif
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001044END(nmi)
1045
1046ENTRY(int3)
H. Peter Anvine59d1b02012-09-21 13:58:10 -07001047 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +02001048 pushl $-1 # mark this as an int
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001049 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -05001050 ENCODE_FRAME_POINTER
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001051 TRACE_IRQS_OFF
Ingo Molnara49976d2015-06-08 09:49:11 +02001052 xorl %edx, %edx # zero error code
1053 movl %esp, %eax # pt_regs pointer
1054 call do_int3
1055 jmp ret_from_exception
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001056END(int3)
1057
1058ENTRY(general_protection)
Ingo Molnara49976d2015-06-08 09:49:11 +02001059 pushl $do_general_protection
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -05001060 jmp common_exception
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001061END(general_protection)
1062
Gleb Natapov631bc482010-10-14 11:22:52 +02001063#ifdef CONFIG_KVM_GUEST
1064ENTRY(async_page_fault)
H. Peter Anvine59d1b02012-09-21 13:58:10 -07001065 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +02001066 pushl $do_async_page_fault
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -05001067 jmp common_exception
Sedat Dilek2ae9d292011-03-08 22:39:24 +01001068END(async_page_fault)
Gleb Natapov631bc482010-10-14 11:22:52 +02001069#endif
Andy Lutomirski2deb4be2016-07-14 13:22:55 -07001070
1071ENTRY(rewind_stack_do_exit)
1072 /* Prevent any naive code from trying to unwind to our caller. */
1073 xorl %ebp, %ebp
1074
1075 movl PER_CPU_VAR(cpu_current_top_of_stack), %esi
1076 leal -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%esi), %esp
1077
1078 call do_exit
10791: jmp 1b
1080END(rewind_stack_do_exit)