blob: 0ab316c468067357663529ce5c53c31a32b35f46 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Ingo Molnara49976d2015-06-08 09:49:11 +02003 * Copyright (C) 1991,1992 Linus Torvalds
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Ingo Molnara49976d2015-06-08 09:49:11 +02005 * entry_32.S contains the system-call and low-level fault and trap handling routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Andy Lutomirski39e87012015-10-05 17:48:13 -07007 * Stack layout while running C code:
Ingo Molnara49976d2015-06-08 09:49:11 +02008 * ptrace needs to have all registers on the stack.
9 * If the order here is changed, it needs to be
10 * updated in fork.c:copy_process(), signal.c:do_signal(),
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * ptrace.c and ptrace.h
12 *
13 * 0(%esp) - %ebx
14 * 4(%esp) - %ecx
15 * 8(%esp) - %edx
Denys Vlasenko9b47feb2015-06-08 22:35:33 +020016 * C(%esp) - %esi
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * 10(%esp) - %edi
18 * 14(%esp) - %ebp
19 * 18(%esp) - %eax
20 * 1C(%esp) - %ds
21 * 20(%esp) - %es
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +010022 * 24(%esp) - %fs
Tejun Heoccbeed32009-02-09 22:17:40 +090023 * 28(%esp) - %gs saved iff !CONFIG_X86_32_LAZY_GS
24 * 2C(%esp) - orig_eax
25 * 30(%esp) - %eip
26 * 34(%esp) - %cs
27 * 38(%esp) - %eflags
28 * 3C(%esp) - %oldesp
29 * 40(%esp) - %oldss
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/linkage.h>
Eric Parisd7e75282012-01-03 14:23:06 -050033#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/thread_info.h>
Ingo Molnar55f327f2006-07-03 00:24:43 -070035#include <asm/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/errno.h>
37#include <asm/segment.h>
38#include <asm/smp.h>
Stas Sergeevbe44d2a2006-12-07 02:14:01 +010039#include <asm/percpu.h>
Cyrill Gorcunovab68ed92008-03-25 22:16:32 +030040#include <asm/processor-flags.h>
Thomas Gleixner9b7dc562008-05-02 20:10:09 +020041#include <asm/irq_vectors.h>
Borislav Petkovcd4d09e2016-01-26 22:12:04 +010042#include <asm/cpufeatures.h>
Andy Lutomirskib4ca46e2011-08-25 16:10:33 -040043#include <asm/alternative-asm.h>
H. Peter Anvin6837a542012-04-20 12:19:50 -070044#include <asm/asm.h>
H. Peter Anvine59d1b02012-09-21 13:58:10 -070045#include <asm/smap.h>
Josh Poimboeuf4d516f42016-09-21 16:04:01 -050046#include <asm/frame.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jiri Olsaea714542011-03-07 19:10:39 +010048 .section .entry.text, "ax"
49
Rusty Russell139ec7c2006-12-07 02:14:08 +010050/*
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")
Jeremy Fitzhardinged75cd222008-06-25 00:19:26 -040055 * ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
Rusty Russell139ec7c2006-12-07 02:14:08 +010056 *
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.
61 */
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#ifdef CONFIG_PREEMPT
Ingo Molnara49976d2015-06-08 09:49:11 +020064# define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#else
Ingo Molnara49976d2015-06-08 09:49:11 +020066# define preempt_stop(clobbers)
67# define resume_kernel restore_all
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#endif
69
Ingo Molnar55f327f2006-07-03 00:24:43 -070070.macro TRACE_IRQS_IRET
71#ifdef CONFIG_TRACE_IRQFLAGS
Ingo Molnara49976d2015-06-08 09:49:11 +020072 testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off?
73 jz 1f
Ingo Molnar55f327f2006-07-03 00:24:43 -070074 TRACE_IRQS_ON
751:
76#endif
77.endm
78
Tejun Heoccbeed32009-02-09 22:17:40 +090079/*
80 * User gs save/restore
81 *
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.
85 *
86 * Local labels 98 and 99 are used.
87 */
88#ifdef CONFIG_X86_32_LAZY_GS
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Tejun Heoccbeed32009-02-09 22:17:40 +090090 /* unfortunately push/pop can't be no-op */
91.macro PUSH_GS
Ingo Molnara49976d2015-06-08 09:49:11 +020092 pushl $0
Tejun Heoccbeed32009-02-09 22:17:40 +090093.endm
94.macro POP_GS pop=0
Ingo Molnara49976d2015-06-08 09:49:11 +020095 addl $(4 + \pop), %esp
Tejun Heoccbeed32009-02-09 22:17:40 +090096.endm
97.macro POP_GS_EX
98.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Tejun Heoccbeed32009-02-09 22:17:40 +0900100 /* all the rest are no-op */
101.macro PTGS_TO_GS
102.endm
103.macro PTGS_TO_GS_EX
104.endm
105.macro GS_TO_REG reg
106.endm
107.macro REG_TO_PTGS reg
108.endm
109.macro SET_KERNEL_GS reg
110.endm
111
112#else /* CONFIG_X86_32_LAZY_GS */
113
114.macro PUSH_GS
Ingo Molnara49976d2015-06-08 09:49:11 +0200115 pushl %gs
Tejun Heoccbeed32009-02-09 22:17:40 +0900116.endm
117
118.macro POP_GS pop=0
Ingo Molnara49976d2015-06-08 09:49:11 +020011998: popl %gs
Tejun Heoccbeed32009-02-09 22:17:40 +0900120 .if \pop <> 0
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200121 add $\pop, %esp
Tejun Heoccbeed32009-02-09 22:17:40 +0900122 .endif
123.endm
124.macro POP_GS_EX
125.pushsection .fixup, "ax"
Ingo Molnara49976d2015-06-08 09:49:11 +020012699: movl $0, (%esp)
127 jmp 98b
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100128.popsection
Ingo Molnara49976d2015-06-08 09:49:11 +0200129 _ASM_EXTABLE(98b, 99b)
Tejun Heoccbeed32009-02-09 22:17:40 +0900130.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Tejun Heoccbeed32009-02-09 22:17:40 +0900132.macro PTGS_TO_GS
Ingo Molnara49976d2015-06-08 09:49:11 +020013398: mov PT_GS(%esp), %gs
Tejun Heoccbeed32009-02-09 22:17:40 +0900134.endm
135.macro PTGS_TO_GS_EX
136.pushsection .fixup, "ax"
Ingo Molnara49976d2015-06-08 09:49:11 +020013799: movl $0, PT_GS(%esp)
138 jmp 98b
Tejun Heoccbeed32009-02-09 22:17:40 +0900139.popsection
Ingo Molnara49976d2015-06-08 09:49:11 +0200140 _ASM_EXTABLE(98b, 99b)
Tejun Heoccbeed32009-02-09 22:17:40 +0900141.endm
142
143.macro GS_TO_REG reg
Ingo Molnara49976d2015-06-08 09:49:11 +0200144 movl %gs, \reg
Tejun Heoccbeed32009-02-09 22:17:40 +0900145.endm
146.macro REG_TO_PTGS reg
Ingo Molnara49976d2015-06-08 09:49:11 +0200147 movl \reg, PT_GS(%esp)
Tejun Heoccbeed32009-02-09 22:17:40 +0900148.endm
149.macro SET_KERNEL_GS reg
Ingo Molnara49976d2015-06-08 09:49:11 +0200150 movl $(__KERNEL_STACK_CANARY), \reg
151 movl \reg, %gs
Tejun Heoccbeed32009-02-09 22:17:40 +0900152.endm
153
Ingo Molnara49976d2015-06-08 09:49:11 +0200154#endif /* CONFIG_X86_32_LAZY_GS */
Tejun Heoccbeed32009-02-09 22:17:40 +0900155
Andy Lutomirski150ac782015-10-05 17:48:14 -0700156.macro SAVE_ALL pt_regs_ax=%eax
Tejun Heof0d96112009-02-09 22:17:40 +0900157 cld
Tejun Heoccbeed32009-02-09 22:17:40 +0900158 PUSH_GS
Ingo Molnara49976d2015-06-08 09:49:11 +0200159 pushl %fs
160 pushl %es
161 pushl %ds
Andy Lutomirski150ac782015-10-05 17:48:14 -0700162 pushl \pt_regs_ax
Ingo Molnara49976d2015-06-08 09:49:11 +0200163 pushl %ebp
164 pushl %edi
165 pushl %esi
166 pushl %edx
167 pushl %ecx
168 pushl %ebx
169 movl $(__USER_DS), %edx
170 movl %edx, %ds
171 movl %edx, %es
172 movl $(__KERNEL_PERCPU), %edx
173 movl %edx, %fs
Tejun Heoccbeed32009-02-09 22:17:40 +0900174 SET_KERNEL_GS %edx
Tejun Heof0d96112009-02-09 22:17:40 +0900175.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500177/*
178 * This is a sneaky trick to help the unwinder find pt_regs on the stack. The
179 * frame pointer is replaced with an encoded pointer to pt_regs. The encoding
Josh Poimboeuf5c99b692017-10-09 20:20:03 -0500180 * is just clearing the MSB, which makes it an invalid stack address and is also
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500181 * a signal to the unwinder that it's a pt_regs pointer in disguise.
182 *
183 * NOTE: This macro must be used *after* SAVE_ALL because it corrupts the
184 * original rbp.
185 */
186.macro ENCODE_FRAME_POINTER
187#ifdef CONFIG_FRAME_POINTER
188 mov %esp, %ebp
Josh Poimboeuf5c99b692017-10-09 20:20:03 -0500189 andl $0x7fffffff, %ebp
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500190#endif
191.endm
192
Tejun Heof0d96112009-02-09 22:17:40 +0900193.macro RESTORE_INT_REGS
Ingo Molnara49976d2015-06-08 09:49:11 +0200194 popl %ebx
195 popl %ecx
196 popl %edx
197 popl %esi
198 popl %edi
199 popl %ebp
200 popl %eax
Tejun Heof0d96112009-02-09 22:17:40 +0900201.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Tejun Heoccbeed32009-02-09 22:17:40 +0900203.macro RESTORE_REGS pop=0
Tejun Heof0d96112009-02-09 22:17:40 +0900204 RESTORE_INT_REGS
Ingo Molnara49976d2015-06-08 09:49:11 +02002051: popl %ds
2062: popl %es
2073: popl %fs
Tejun Heoccbeed32009-02-09 22:17:40 +0900208 POP_GS \pop
Tejun Heof0d96112009-02-09 22:17:40 +0900209.pushsection .fixup, "ax"
Ingo Molnara49976d2015-06-08 09:49:11 +02002104: movl $0, (%esp)
211 jmp 1b
2125: movl $0, (%esp)
213 jmp 2b
2146: movl $0, (%esp)
215 jmp 3b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216.popsection
Ingo Molnara49976d2015-06-08 09:49:11 +0200217 _ASM_EXTABLE(1b, 4b)
218 _ASM_EXTABLE(2b, 5b)
219 _ASM_EXTABLE(3b, 6b)
Tejun Heoccbeed32009-02-09 22:17:40 +0900220 POP_GS_EX
Tejun Heof0d96112009-02-09 22:17:40 +0900221.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Brian Gerst01003012016-08-13 12:38:19 -0400223/*
224 * %eax: prev task
225 * %edx: next task
226 */
227ENTRY(__switch_to_asm)
228 /*
229 * Save callee-saved registers
230 * This must match the order in struct inactive_task_frame
231 */
232 pushl %ebp
233 pushl %ebx
234 pushl %edi
235 pushl %esi
236
237 /* switch stack */
238 movl %esp, TASK_threadsp(%eax)
239 movl TASK_threadsp(%edx), %esp
240
241#ifdef CONFIG_CC_STACKPROTECTOR
242 movl TASK_stack_canary(%edx), %ebx
243 movl %ebx, PER_CPU_VAR(stack_canary)+stack_canary_offset
244#endif
245
246 /* restore callee-saved registers */
247 popl %esi
248 popl %edi
249 popl %ebx
250 popl %ebp
251
252 jmp __switch_to
253END(__switch_to_asm)
254
255/*
Josh Poimboeufebd57492017-05-23 10:37:29 -0500256 * The unwinder expects the last frame on the stack to always be at the same
257 * offset from the end of the page, which allows it to validate the stack.
258 * Calling schedule_tail() directly would break that convention because its an
259 * asmlinkage function so its argument has to be pushed on the stack. This
260 * wrapper creates a proper "end of stack" frame header before the call.
261 */
262ENTRY(schedule_tail_wrapper)
263 FRAME_BEGIN
264
265 pushl %eax
266 call schedule_tail
267 popl %eax
268
269 FRAME_END
270 ret
271ENDPROC(schedule_tail_wrapper)
272/*
Brian Gerst01003012016-08-13 12:38:19 -0400273 * A newly forked process directly context switches into this address.
274 *
275 * eax: prev task we switched from
Brian Gerst616d2482016-08-13 12:38:20 -0400276 * ebx: kernel thread func (NULL for user thread)
277 * edi: kernel thread arg
Brian Gerst01003012016-08-13 12:38:19 -0400278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279ENTRY(ret_from_fork)
Josh Poimboeufebd57492017-05-23 10:37:29 -0500280 call schedule_tail_wrapper
Andy Lutomirski39e87012015-10-05 17:48:13 -0700281
Brian Gerst616d2482016-08-13 12:38:20 -0400282 testl %ebx, %ebx
283 jnz 1f /* kernel threads are uncommon */
284
2852:
Andy Lutomirski39e87012015-10-05 17:48:13 -0700286 /* When we fork, we trace the syscall return in the child, too. */
Josh Poimboeufebd57492017-05-23 10:37:29 -0500287 movl %esp, %eax
Andy Lutomirski39e87012015-10-05 17:48:13 -0700288 call syscall_return_slowpath
289 jmp restore_all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Brian Gerst616d2482016-08-13 12:38:20 -0400291 /* kernel thread */
2921: movl %edi, %eax
293 call *%ebx
Andy Lutomirski39e87012015-10-05 17:48:13 -0700294 /*
Brian Gerst616d2482016-08-13 12:38:20 -0400295 * A kernel thread is allowed to return here after successfully
296 * calling do_execve(). Exit to userspace to complete the execve()
297 * syscall.
Andy Lutomirski39e87012015-10-05 17:48:13 -0700298 */
Brian Gerst616d2482016-08-13 12:38:20 -0400299 movl $0, PT_EAX(%esp)
300 jmp 2b
301END(ret_from_fork)
Al Viro6783eaa22012-08-02 23:05:11 +0400302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
304 * Return to user mode is not as complex as all this looks,
305 * but we want the default path for a system call return to
306 * go as quickly as possible which is why some of this is
307 * less clear than it otherwise should be.
308 */
309
310 # userspace resumption stub bypassing syscall exit tracing
311 ALIGN
312ret_from_exception:
Rusty Russell139ec7c2006-12-07 02:14:08 +0100313 preempt_stop(CLBR_ANY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314ret_from_intr:
Dmitry Adamushko29a2e282012-03-22 21:39:25 +0100315#ifdef CONFIG_VM86
Ingo Molnara49976d2015-06-08 09:49:11 +0200316 movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
317 movb PT_CS(%esp), %al
318 andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
Dmitry Adamushko29a2e282012-03-22 21:39:25 +0100319#else
320 /*
Al Viro6783eaa22012-08-02 23:05:11 +0400321 * We can be coming here from child spawned by kernel_thread().
Dmitry Adamushko29a2e282012-03-22 21:39:25 +0100322 */
Ingo Molnara49976d2015-06-08 09:49:11 +0200323 movl PT_CS(%esp), %eax
324 andl $SEGMENT_RPL_MASK, %eax
Dmitry Adamushko29a2e282012-03-22 21:39:25 +0100325#endif
Ingo Molnara49976d2015-06-08 09:49:11 +0200326 cmpl $USER_RPL, %eax
327 jb resume_kernel # not returning to v8086 or userspace
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329ENTRY(resume_userspace)
Andy Lutomirski5d73fc72015-07-31 14:41:09 -0700330 DISABLE_INTERRUPTS(CLBR_ANY)
Peter Zijlstrae32e58a2008-06-06 10:14:08 +0200331 TRACE_IRQS_OFF
Andy Lutomirski5d73fc72015-07-31 14:41:09 -0700332 movl %esp, %eax
333 call prepare_exit_to_usermode
Ingo Molnara49976d2015-06-08 09:49:11 +0200334 jmp restore_all
Jan Beulich47a55cd2007-02-13 13:26:24 +0100335END(ret_from_exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337#ifdef CONFIG_PREEMPT
338ENTRY(resume_kernel)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100339 DISABLE_INTERRUPTS(CLBR_ANY)
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500340.Lneed_resched:
Ingo Molnara49976d2015-06-08 09:49:11 +0200341 cmpl $0, PER_CPU_VAR(__preempt_count)
342 jnz restore_all
343 testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
344 jz restore_all
345 call preempt_schedule_irq
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500346 jmp .Lneed_resched
Jan Beulich47a55cd2007-02-13 13:26:24 +0100347END(resume_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#endif
349
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800350GLOBAL(__begin_SYSENTER_singlestep_region)
351/*
352 * All code from here through __end_SYSENTER_singlestep_region is subject
353 * to being single-stepped if a user program sets TF and executes SYSENTER.
354 * There is absolutely nothing that we can do to prevent this from happening
355 * (thanks Intel!). To keep our handling of this situation as simple as
356 * possible, we handle TF just like AC and NT, except that our #DB handler
357 * will ignore all of the single-step traps generated in this range.
358 */
359
360#ifdef CONFIG_XEN
361/*
362 * Xen doesn't set %esp to be precisely what the normal SYSENTER
363 * entry point expects, so fix it up before using the normal path.
364 */
365ENTRY(xen_sysenter_target)
366 addl $5*4, %esp /* remove xen-provided frame */
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500367 jmp .Lsysenter_past_esp
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800368#endif
369
Andy Lutomirskifda57b22016-03-09 19:00:35 -0800370/*
371 * 32-bit SYSENTER entry.
372 *
373 * 32-bit system calls through the vDSO's __kernel_vsyscall enter here
374 * if X86_FEATURE_SEP is available. This is the preferred system call
375 * entry on 32-bit systems.
376 *
377 * The SYSENTER instruction, in principle, should *only* occur in the
378 * vDSO. In practice, a small number of Android devices were shipped
379 * with a copy of Bionic that inlined a SYSENTER instruction. This
380 * never happened in any of Google's Bionic versions -- it only happened
381 * in a narrow range of Intel-provided versions.
382 *
383 * SYSENTER loads SS, ESP, CS, and EIP from previously programmed MSRs.
384 * IF and VM in RFLAGS are cleared (IOW: interrupts are off).
385 * SYSENTER does not save anything on the stack,
386 * and does not save old EIP (!!!), ESP, or EFLAGS.
387 *
388 * To avoid losing track of EFLAGS.VM (and thus potentially corrupting
389 * user and/or vm86 state), we explicitly disable the SYSENTER
390 * instruction in vm86 mode by reprogramming the MSRs.
391 *
392 * Arguments:
393 * eax system call number
394 * ebx arg1
395 * ecx arg2
396 * edx arg3
397 * esi arg4
398 * edi arg5
399 * ebp user stack
400 * 0(%ebp) arg6
401 */
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +0200402ENTRY(entry_SYSENTER_32)
Ingo Molnara49976d2015-06-08 09:49:11 +0200403 movl TSS_sysenter_sp0(%esp), %esp
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500404.Lsysenter_past_esp:
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700405 pushl $__USER_DS /* pt_regs->ss */
Andy Lutomirski30bfa7b2015-12-16 23:18:48 -0800406 pushl %ebp /* pt_regs->sp (stashed in bp) */
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700407 pushfl /* pt_regs->flags (except IF = 0) */
408 orl $X86_EFLAGS_IF, (%esp) /* Fix IF */
409 pushl $__USER_CS /* pt_regs->cs */
410 pushl $0 /* pt_regs->ip = 0 (placeholder) */
411 pushl %eax /* pt_regs->orig_ax */
412 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
413
Ingo Molnar55f327f2006-07-03 00:24:43 -0700414 /*
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800415 * SYSENTER doesn't filter flags, so we need to clear NT, AC
416 * and TF ourselves. To save a few cycles, we can check whether
Andy Lutomirski67f590e2016-03-09 19:00:26 -0800417 * either was set instead of doing an unconditional popfq.
418 * This needs to happen before enabling interrupts so that
419 * we don't get preempted with NT set.
420 *
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800421 * If TF is set, we will single-step all the way to here -- do_debug
422 * will ignore all the traps. (Yes, this is slow, but so is
423 * single-stepping in general. This allows us to avoid having
424 * a more complicated code to handle the case where a user program
425 * forces us to single-step through the SYSENTER entry code.)
426 *
Andy Lutomirski67f590e2016-03-09 19:00:26 -0800427 * NB.: .Lsysenter_fix_flags is a label with the code under it moved
428 * out-of-line as an optimization: NT is unlikely to be set in the
429 * majority of the cases and instead of polluting the I$ unnecessarily,
430 * we're keeping that code behind a branch which will predict as
431 * not-taken and therefore its instructions won't be fetched.
432 */
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800433 testl $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, PT_EFLAGS(%esp)
Andy Lutomirski67f590e2016-03-09 19:00:26 -0800434 jnz .Lsysenter_fix_flags
435.Lsysenter_flags_fixed:
436
437 /*
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700438 * User mode is traced as though IRQs are on, and SYSENTER
439 * turned them off.
Ingo Molnar55f327f2006-07-03 00:24:43 -0700440 */
Ingo Molnar55f327f2006-07-03 00:24:43 -0700441 TRACE_IRQS_OFF
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700442
443 movl %esp, %eax
444 call do_fast_syscall_32
Boris Ostrovsky91e2eea2015-11-19 16:55:45 -0500445 /* XEN PV guests always use IRET path */
446 ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
447 "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700448
449/* Opportunistic SYSEXIT */
450 TRACE_IRQS_ON /* User mode traces as IRQs on. */
451 movl PT_EIP(%esp), %edx /* pt_regs->ip */
452 movl PT_OLDESP(%esp), %ecx /* pt_regs->sp */
Andy Lutomirski3bd29512015-10-16 15:42:55 -07004531: mov PT_FS(%esp), %fs
454 PTGS_TO_GS
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700455 popl %ebx /* pt_regs->bx */
456 addl $2*4, %esp /* skip pt_regs->cx and pt_regs->dx */
457 popl %esi /* pt_regs->si */
458 popl %edi /* pt_regs->di */
459 popl %ebp /* pt_regs->bp */
460 popl %eax /* pt_regs->ax */
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700461
462 /*
Andy Lutomirskic2c9b522016-03-09 19:00:27 -0800463 * Restore all flags except IF. (We restore IF separately because
464 * STI gives a one-instruction window in which we won't be interrupted,
465 * whereas POPF does not.)
466 */
467 addl $PT_EFLAGS-PT_DS, %esp /* point esp at pt_regs->flags */
468 btr $X86_EFLAGS_IF_BIT, (%esp)
469 popfl
470
471 /*
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700472 * Return back to the vDSO, which will pop ecx and edx.
473 * Don't bother with DS and ES (they already contain __USER_DS).
474 */
Boris Ostrovsky88c15ec2015-11-19 16:55:46 -0500475 sti
476 sysexit
Roland McGrathaf0575b2008-06-24 04:16:52 -0700477
Ingo Molnara49976d2015-06-08 09:49:11 +0200478.pushsection .fixup, "ax"
4792: movl $0, PT_FS(%esp)
480 jmp 1b
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100481.popsection
Ingo Molnara49976d2015-06-08 09:49:11 +0200482 _ASM_EXTABLE(1b, 2b)
Tejun Heoccbeed32009-02-09 22:17:40 +0900483 PTGS_TO_GS_EX
Andy Lutomirski67f590e2016-03-09 19:00:26 -0800484
485.Lsysenter_fix_flags:
486 pushl $X86_EFLAGS_FIXED
487 popfl
488 jmp .Lsysenter_flags_fixed
Andy Lutomirskif2b37572016-03-09 19:00:30 -0800489GLOBAL(__end_SYSENTER_singlestep_region)
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +0200490ENDPROC(entry_SYSENTER_32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Andy Lutomirskifda57b22016-03-09 19:00:35 -0800492/*
493 * 32-bit legacy system call entry.
494 *
495 * 32-bit x86 Linux system calls traditionally used the INT $0x80
496 * instruction. INT $0x80 lands here.
497 *
498 * This entry point can be used by any 32-bit perform system calls.
499 * Instances of INT $0x80 can be found inline in various programs and
500 * libraries. It is also used by the vDSO's __kernel_vsyscall
501 * fallback for hardware that doesn't support a faster entry method.
502 * Restarted 32-bit system calls also fall back to INT $0x80
503 * regardless of what instruction was originally used to do the system
504 * call. (64-bit programs can use INT $0x80 as well, but they can
505 * only run on 64-bit kernels and therefore land in
506 * entry_INT80_compat.)
507 *
508 * This is considered a slow path. It is not used by most libc
509 * implementations on modern hardware except during process startup.
510 *
511 * Arguments:
512 * eax system call number
513 * ebx arg1
514 * ecx arg2
515 * edx arg3
516 * esi arg4
517 * edi arg5
518 * ebp arg6
519 */
Ingo Molnarb2502b42015-06-08 08:42:03 +0200520ENTRY(entry_INT80_32)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700521 ASM_CLAC
Andy Lutomirski150ac782015-10-05 17:48:14 -0700522 pushl %eax /* pt_regs->orig_ax */
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700523 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
Andy Lutomirski150ac782015-10-05 17:48:14 -0700524
525 /*
Andy Lutomirskia798f092016-03-09 13:24:32 -0800526 * User mode is traced as though IRQs are on, and the interrupt gate
527 * turned them off.
Andy Lutomirski150ac782015-10-05 17:48:14 -0700528 */
Andy Lutomirskia798f092016-03-09 13:24:32 -0800529 TRACE_IRQS_OFF
Andy Lutomirski150ac782015-10-05 17:48:14 -0700530
531 movl %esp, %eax
Andy Lutomirskia798f092016-03-09 13:24:32 -0800532 call do_int80_syscall_32
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700533.Lsyscall_32_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535restore_all:
Alexander van Heukelum2e04bc72009-06-18 00:35:57 +0200536 TRACE_IRQS_IRET
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500537.Lrestore_all_notrace:
H. Peter Anvin34273f42014-05-04 10:36:22 -0700538#ifdef CONFIG_X86_ESPFIX32
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500539 ALTERNATIVE "jmp .Lrestore_nocheck", "", X86_BUG_ESPFIX
Andy Lutomirski58a5aac2016-02-29 15:50:19 -0800540
Ingo Molnara49976d2015-06-08 09:49:11 +0200541 movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
542 /*
543 * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
544 * are returning to the kernel.
545 * See comments in process.c:copy_thread() for details.
546 */
547 movb PT_OLDSS(%esp), %ah
548 movb PT_CS(%esp), %al
549 andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
550 cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500551 je .Lldt_ss # returning to user-space with LDT SS
H. Peter Anvin34273f42014-05-04 10:36:22 -0700552#endif
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500553.Lrestore_nocheck:
Ingo Molnara49976d2015-06-08 09:49:11 +0200554 RESTORE_REGS 4 # skip orig_eax/error_code
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500555.Lirq_return:
Ingo Molnar3701d8632008-02-09 23:24:08 +0100556 INTERRUPT_RETURN
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500557
Ingo Molnara49976d2015-06-08 09:49:11 +0200558.section .fixup, "ax"
559ENTRY(iret_exc )
560 pushl $0 # no error code
561 pushl $do_iret_error
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500562 jmp common_exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563.previous
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500564 _ASM_EXTABLE(.Lirq_return, iret_exc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
H. Peter Anvin34273f42014-05-04 10:36:22 -0700566#ifdef CONFIG_X86_ESPFIX32
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500567.Lldt_ss:
Alexander van Heukelumdc4c2a02009-06-18 00:35:58 +0200568/*
569 * Setup and switch to ESPFIX stack
570 *
571 * We're returning to userspace with a 16 bit stack. The CPU will not
572 * restore the high word of ESP for us on executing iret... This is an
573 * "official" bug of all the x86-compatible CPUs, which we can work
574 * around to make dosemu and wine happy. We do this by preloading the
575 * high word of ESP with the high word of the userspace ESP while
576 * compensating for the offset by changing to the ESPFIX segment with
577 * a base address that matches for the difference.
578 */
Brian Gerst72c511d2010-07-31 12:48:23 -0400579#define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
Ingo Molnara49976d2015-06-08 09:49:11 +0200580 mov %esp, %edx /* load kernel esp */
581 mov PT_OLDESP(%esp), %eax /* load userspace esp */
582 mov %dx, %ax /* eax: new kernel esp */
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200583 sub %eax, %edx /* offset (low word is 0) */
584 shr $16, %edx
Ingo Molnara49976d2015-06-08 09:49:11 +0200585 mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
586 mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
587 pushl $__ESPFIX_SS
588 pushl %eax /* new kernel esp */
589 /*
590 * Disable interrupts, but do not irqtrace this section: we
Alexander van Heukelum2e04bc72009-06-18 00:35:57 +0200591 * will soon execute iret and the tracer was already set to
Ingo Molnara49976d2015-06-08 09:49:11 +0200592 * the irqstate after the IRET:
593 */
Jan Beulichfdbd5182017-02-03 01:58:03 -0700594 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnara49976d2015-06-08 09:49:11 +0200595 lss (%esp), %esp /* switch to espfix segment */
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500596 jmp .Lrestore_nocheck
H. Peter Anvin34273f42014-05-04 10:36:22 -0700597#endif
Ingo Molnarb2502b42015-06-08 08:42:03 +0200598ENDPROC(entry_INT80_32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Tejun Heof0d96112009-02-09 22:17:40 +0900600.macro FIXUP_ESPFIX_STACK
Alexander van Heukelumdc4c2a02009-06-18 00:35:58 +0200601/*
602 * Switch back for ESPFIX stack to the normal zerobased stack
603 *
604 * We can't call C functions using the ESPFIX stack. This code reads
605 * the high word of the segment base from the GDT and swiches to the
606 * normal stack and adjusts ESP with the matching offset.
607 */
H. Peter Anvin34273f42014-05-04 10:36:22 -0700608#ifdef CONFIG_X86_ESPFIX32
Alexander van Heukelumdc4c2a02009-06-18 00:35:58 +0200609 /* fixup the stack */
Ingo Molnara49976d2015-06-08 09:49:11 +0200610 mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
611 mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200612 shl $16, %eax
Ingo Molnara49976d2015-06-08 09:49:11 +0200613 addl %esp, %eax /* the adjusted stack pointer */
614 pushl $__KERNEL_DS
615 pushl %eax
616 lss (%esp), %esp /* switch to the normal stack segment */
H. Peter Anvin34273f42014-05-04 10:36:22 -0700617#endif
Tejun Heof0d96112009-02-09 22:17:40 +0900618.endm
619.macro UNWIND_ESPFIX_STACK
H. Peter Anvin34273f42014-05-04 10:36:22 -0700620#ifdef CONFIG_X86_ESPFIX32
Ingo Molnara49976d2015-06-08 09:49:11 +0200621 movl %ss, %eax
Tejun Heof0d96112009-02-09 22:17:40 +0900622 /* see if on espfix stack */
Ingo Molnara49976d2015-06-08 09:49:11 +0200623 cmpw $__ESPFIX_SS, %ax
624 jne 27f
625 movl $__KERNEL_DS, %eax
626 movl %eax, %ds
627 movl %eax, %es
Tejun Heof0d96112009-02-09 22:17:40 +0900628 /* switch to normal stack */
629 FIXUP_ESPFIX_STACK
63027:
H. Peter Anvin34273f42014-05-04 10:36:22 -0700631#endif
Tejun Heof0d96112009-02-09 22:17:40 +0900632.endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634/*
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200635 * Build the entry stubs with some assembler magic.
636 * We pack 1 stub into every 8-byte block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200638 .align 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639ENTRY(irq_entries_start)
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200640 vector=FIRST_EXTERNAL_VECTOR
641 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
Ingo Molnara49976d2015-06-08 09:49:11 +0200642 pushl $(~vector+0x80) /* Note: always in signed byte range */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200643 vector=vector+1
644 jmp common_interrupt
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200645 .align 8
646 .endr
Jan Beulich47a55cd2007-02-13 13:26:24 +0100647END(irq_entries_start)
648
Ingo Molnar55f327f2006-07-03 00:24:43 -0700649/*
650 * the CPU automatically disables interrupts when executing an IRQ vector,
651 * so IRQ-flags tracing has to follow that:
652 */
H. Peter Anvinb7c62442008-11-11 13:24:58 -0800653 .p2align CONFIG_X86_L1_CACHE_SHIFT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654common_interrupt:
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700655 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200656 addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500658 ENCODE_FRAME_POINTER
Ingo Molnar55f327f2006-07-03 00:24:43 -0700659 TRACE_IRQS_OFF
Ingo Molnara49976d2015-06-08 09:49:11 +0200660 movl %esp, %eax
661 call do_IRQ
662 jmp ret_from_intr
Jan Beulich47a55cd2007-02-13 13:26:24 +0100663ENDPROC(common_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Tejun Heo02cf94c2009-01-21 17:26:06 +0900665#define BUILD_INTERRUPT3(name, nr, fn) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666ENTRY(name) \
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700667 ASM_CLAC; \
Ingo Molnara49976d2015-06-08 09:49:11 +0200668 pushl $~(nr); \
Jan Beulichfe7cacc2006-06-26 13:57:44 +0200669 SAVE_ALL; \
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500670 ENCODE_FRAME_POINTER; \
Ingo Molnar55f327f2006-07-03 00:24:43 -0700671 TRACE_IRQS_OFF \
Ingo Molnara49976d2015-06-08 09:49:11 +0200672 movl %esp, %eax; \
673 call fn; \
674 jmp ret_from_intr; \
Jan Beulich47a55cd2007-02-13 13:26:24 +0100675ENDPROC(name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Ingo Molnara49976d2015-06-08 09:49:11 +0200677#define BUILD_INTERRUPT(name, nr) \
678 BUILD_INTERRUPT3(name, nr, smp_##name); \
Tejun Heo02cf94c2009-01-21 17:26:06 +0900679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/* The include is where all of the SMP etc. interrupts come from */
Ingo Molnar1164dd02009-01-28 19:34:09 +0100681#include <asm/entry_arch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683ENTRY(coprocessor_error)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700684 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200685 pushl $0
686 pushl $do_coprocessor_error
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500687 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100688END(coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690ENTRY(simd_coprocessor_error)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700691 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200692 pushl $0
Brian Gerst40d2e762010-03-21 09:00:43 -0400693#ifdef CONFIG_X86_INVD_BUG
694 /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
Ingo Molnara49976d2015-06-08 09:49:11 +0200695 ALTERNATIVE "pushl $do_general_protection", \
696 "pushl $do_simd_coprocessor_error", \
Borislav Petkov8e65f6e2015-01-18 12:35:55 +0100697 X86_FEATURE_XMM
Brian Gerst40d2e762010-03-21 09:00:43 -0400698#else
Ingo Molnara49976d2015-06-08 09:49:11 +0200699 pushl $do_simd_coprocessor_error
Brian Gerst40d2e762010-03-21 09:00:43 -0400700#endif
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500701 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100702END(simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704ENTRY(device_not_available)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700705 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200706 pushl $-1 # mark this as an int
707 pushl $do_device_not_available
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500708 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100709END(device_not_available)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Rusty Russelld3561b72006-12-07 02:14:07 +0100711#ifdef CONFIG_PARAVIRT
712ENTRY(native_iret)
Ingo Molnar3701d8632008-02-09 23:24:08 +0100713 iret
H. Peter Anvin6837a542012-04-20 12:19:50 -0700714 _ASM_EXTABLE(native_iret, iret_exc)
Jan Beulich47a55cd2007-02-13 13:26:24 +0100715END(native_iret)
Rusty Russelld3561b72006-12-07 02:14:07 +0100716#endif
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718ENTRY(overflow)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700719 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200720 pushl $0
721 pushl $do_overflow
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500722 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100723END(overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725ENTRY(bounds)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700726 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200727 pushl $0
728 pushl $do_bounds
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500729 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100730END(bounds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732ENTRY(invalid_op)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700733 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200734 pushl $0
735 pushl $do_invalid_op
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500736 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100737END(invalid_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739ENTRY(coprocessor_segment_overrun)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700740 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200741 pushl $0
742 pushl $do_coprocessor_segment_overrun
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500743 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100744END(coprocessor_segment_overrun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746ENTRY(invalid_TSS)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700747 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200748 pushl $do_invalid_TSS
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500749 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100750END(invalid_TSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752ENTRY(segment_not_present)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700753 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200754 pushl $do_segment_not_present
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500755 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100756END(segment_not_present)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758ENTRY(stack_segment)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700759 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200760 pushl $do_stack_segment
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500761 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100762END(stack_segment)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764ENTRY(alignment_check)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700765 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200766 pushl $do_alignment_check
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500767 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100768END(alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Prasanna S.Pd28c4392006-09-26 10:52:34 +0200770ENTRY(divide_error)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700771 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200772 pushl $0 # no error code
773 pushl $do_divide_error
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500774 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100775END(divide_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777#ifdef CONFIG_X86_MCE
778ENTRY(machine_check)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700779 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200780 pushl $0
781 pushl machine_check_vector
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500782 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100783END(machine_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784#endif
785
786ENTRY(spurious_interrupt_bug)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700787 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200788 pushl $0
789 pushl $do_spurious_interrupt_bug
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500790 jmp common_exception
Jan Beulich47a55cd2007-02-13 13:26:24 +0100791END(spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700793#ifdef CONFIG_XEN
794ENTRY(xen_hypervisor_callback)
Ingo Molnara49976d2015-06-08 09:49:11 +0200795 pushl $-1 /* orig_ax = -1 => not a system call */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700796 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500797 ENCODE_FRAME_POINTER
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700798 TRACE_IRQS_OFF
Jeremy Fitzhardinge9ec2b802007-07-17 18:37:07 -0700799
Ingo Molnara49976d2015-06-08 09:49:11 +0200800 /*
801 * Check to see if we got the event in the critical
802 * region in xen_iret_direct, after we've reenabled
803 * events and checked for pending events. This simulates
804 * iret instruction's behaviour where it delivers a
805 * pending interrupt when enabling interrupts:
806 */
807 movl PT_EIP(%esp), %eax
808 cmpl $xen_iret_start_crit, %eax
809 jb 1f
810 cmpl $xen_iret_end_crit, %eax
811 jae 1f
Jeremy Fitzhardinge9ec2b802007-07-17 18:37:07 -0700812
Ingo Molnara49976d2015-06-08 09:49:11 +0200813 jmp xen_iret_crit_fixup
Jeremy Fitzhardinge9ec2b802007-07-17 18:37:07 -0700814
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -0700815ENTRY(xen_do_upcall)
Ingo Molnara49976d2015-06-08 09:49:11 +02008161: mov %esp, %eax
817 call xen_evtchn_do_upcall
David Vrabelfdfd8112015-02-19 15:23:17 +0000818#ifndef CONFIG_PREEMPT
Ingo Molnara49976d2015-06-08 09:49:11 +0200819 call xen_maybe_preempt_hcall
David Vrabelfdfd8112015-02-19 15:23:17 +0000820#endif
Ingo Molnara49976d2015-06-08 09:49:11 +0200821 jmp ret_from_intr
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700822ENDPROC(xen_hypervisor_callback)
823
Ingo Molnara49976d2015-06-08 09:49:11 +0200824/*
825 * Hypervisor uses this for application faults while it executes.
826 * We get here for two reasons:
827 * 1. Fault while reloading DS, ES, FS or GS
828 * 2. Fault while executing IRET
829 * Category 1 we fix up by reattempting the load, and zeroing the segment
830 * register if the load fails.
831 * Category 2 we fix up by jumping to do_iret_error. We cannot use the
832 * normal Linux return path in this case because if we use the IRET hypercall
833 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
834 * We distinguish between categories by maintaining a status value in EAX.
835 */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700836ENTRY(xen_failsafe_callback)
Ingo Molnara49976d2015-06-08 09:49:11 +0200837 pushl %eax
838 movl $1, %eax
8391: mov 4(%esp), %ds
8402: mov 8(%esp), %es
8413: mov 12(%esp), %fs
8424: mov 16(%esp), %gs
David Vrabela349e23d12012-10-19 17:29:07 +0100843 /* EAX == 0 => Category 1 (Bad segment)
844 EAX != 0 => Category 2 (Bad IRET) */
Ingo Molnara49976d2015-06-08 09:49:11 +0200845 testl %eax, %eax
846 popl %eax
847 lea 16(%esp), %esp
848 jz 5f
849 jmp iret_exc
8505: pushl $-1 /* orig_ax = -1 => not a system call */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700851 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500852 ENCODE_FRAME_POINTER
Ingo Molnara49976d2015-06-08 09:49:11 +0200853 jmp ret_from_exception
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700854
Ingo Molnara49976d2015-06-08 09:49:11 +0200855.section .fixup, "ax"
8566: xorl %eax, %eax
857 movl %eax, 4(%esp)
858 jmp 1b
8597: xorl %eax, %eax
860 movl %eax, 8(%esp)
861 jmp 2b
8628: xorl %eax, %eax
863 movl %eax, 12(%esp)
864 jmp 3b
8659: xorl %eax, %eax
866 movl %eax, 16(%esp)
867 jmp 4b
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700868.previous
Ingo Molnara49976d2015-06-08 09:49:11 +0200869 _ASM_EXTABLE(1b, 6b)
870 _ASM_EXTABLE(2b, 7b)
871 _ASM_EXTABLE(3b, 8b)
872 _ASM_EXTABLE(4b, 9b)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700873ENDPROC(xen_failsafe_callback)
874
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800875BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
Thomas Gleixner4b9a8dc2017-08-28 08:47:31 +0200876 xen_evtchn_do_upcall)
Sheng Yang38e20b02010-05-14 12:40:51 +0100877
Ingo Molnara49976d2015-06-08 09:49:11 +0200878#endif /* CONFIG_XEN */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700879
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800880#if IS_ENABLED(CONFIG_HYPERV)
881
882BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
Thomas Gleixner4b9a8dc2017-08-28 08:47:31 +0200883 hyperv_vector_handler)
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800884
885#endif /* CONFIG_HYPERV */
886
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100887ENTRY(page_fault)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700888 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200889 pushl $do_page_fault
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100890 ALIGN
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500891 jmp common_exception
892END(page_fault)
893
894common_exception:
Tejun Heoccbeed32009-02-09 22:17:40 +0900895 /* the function address is in %gs's slot on the stack */
Ingo Molnara49976d2015-06-08 09:49:11 +0200896 pushl %fs
897 pushl %es
898 pushl %ds
899 pushl %eax
900 pushl %ebp
901 pushl %edi
902 pushl %esi
903 pushl %edx
904 pushl %ecx
905 pushl %ebx
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500906 ENCODE_FRAME_POINTER
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100907 cld
Ingo Molnara49976d2015-06-08 09:49:11 +0200908 movl $(__KERNEL_PERCPU), %ecx
909 movl %ecx, %fs
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100910 UNWIND_ESPFIX_STACK
Tejun Heoccbeed32009-02-09 22:17:40 +0900911 GS_TO_REG %ecx
Ingo Molnara49976d2015-06-08 09:49:11 +0200912 movl PT_GS(%esp), %edi # get the function address
913 movl PT_ORIG_EAX(%esp), %edx # get the error code
914 movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart
Tejun Heoccbeed32009-02-09 22:17:40 +0900915 REG_TO_PTGS %ecx
916 SET_KERNEL_GS %ecx
Ingo Molnara49976d2015-06-08 09:49:11 +0200917 movl $(__USER_DS), %ecx
918 movl %ecx, %ds
919 movl %ecx, %es
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100920 TRACE_IRQS_OFF
Ingo Molnara49976d2015-06-08 09:49:11 +0200921 movl %esp, %eax # pt_regs pointer
922 call *%edi
923 jmp ret_from_exception
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -0500924END(common_exception)
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100925
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100926ENTRY(debug)
Andy Lutomirski75366562016-03-09 19:00:32 -0800927 /*
928 * #DB can happen at the first instruction of
929 * entry_SYSENTER_32 or in Xen's SYSENTER prologue. If this
930 * happens, then we will be running on a very small stack. We
931 * need to detect this condition and switch to the thread
932 * stack before calling any C code at all.
933 *
934 * If you edit this code, keep in mind that NMIs can happen in here.
935 */
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700936 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +0200937 pushl $-1 # mark this as an int
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100938 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500939 ENCODE_FRAME_POINTER
Ingo Molnara49976d2015-06-08 09:49:11 +0200940 xorl %edx, %edx # error code 0
941 movl %esp, %eax # pt_regs pointer
Andy Lutomirski75366562016-03-09 19:00:32 -0800942
943 /* Are we currently on the SYSENTER stack? */
Andy Lutomirski72f5e082017-12-04 15:07:20 +0100944 movl PER_CPU_VAR(cpu_entry_area), %ecx
945 addl $CPU_ENTRY_AREA_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx
Andy Lutomirski75366562016-03-09 19:00:32 -0800946 subl %eax, %ecx /* ecx = (end of SYSENTER_stack) - esp */
947 cmpl $SIZEOF_SYSENTER_stack, %ecx
948 jb .Ldebug_from_sysenter_stack
949
950 TRACE_IRQS_OFF
Ingo Molnara49976d2015-06-08 09:49:11 +0200951 call do_debug
952 jmp ret_from_exception
Andy Lutomirski75366562016-03-09 19:00:32 -0800953
954.Ldebug_from_sysenter_stack:
955 /* We're on the SYSENTER stack. Switch off. */
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500956 movl %esp, %ebx
Andy Lutomirski75366562016-03-09 19:00:32 -0800957 movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
958 TRACE_IRQS_OFF
959 call do_debug
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500960 movl %ebx, %esp
Andy Lutomirski75366562016-03-09 19:00:32 -0800961 jmp ret_from_exception
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100962END(debug)
963
964/*
Andy Lutomirski75366562016-03-09 19:00:32 -0800965 * NMI is doubly nasty. It can happen on the first instruction of
966 * entry_SYSENTER_32 (just like #DB), but it can also interrupt the beginning
967 * of the #DB handler even if that #DB in turn hit before entry_SYSENTER_32
968 * switched stacks. We handle both conditions by simply checking whether we
969 * interrupted kernel code running on the SYSENTER stack.
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100970 */
971ENTRY(nmi)
H. Peter Anvine59d1b02012-09-21 13:58:10 -0700972 ASM_CLAC
H. Peter Anvin34273f42014-05-04 10:36:22 -0700973#ifdef CONFIG_X86_ESPFIX32
Ingo Molnara49976d2015-06-08 09:49:11 +0200974 pushl %eax
975 movl %ss, %eax
976 cmpw $__ESPFIX_SS, %ax
977 popl %eax
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500978 je .Lnmi_espfix_stack
H. Peter Anvin34273f42014-05-04 10:36:22 -0700979#endif
Andy Lutomirski75366562016-03-09 19:00:32 -0800980
981 pushl %eax # pt_regs->orig_ax
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100982 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -0500983 ENCODE_FRAME_POINTER
Ingo Molnara49976d2015-06-08 09:49:11 +0200984 xorl %edx, %edx # zero error code
985 movl %esp, %eax # pt_regs pointer
Andy Lutomirski75366562016-03-09 19:00:32 -0800986
987 /* Are we currently on the SYSENTER stack? */
Andy Lutomirski72f5e082017-12-04 15:07:20 +0100988 movl PER_CPU_VAR(cpu_entry_area), %ecx
989 addl $CPU_ENTRY_AREA_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx
Andy Lutomirski75366562016-03-09 19:00:32 -0800990 subl %eax, %ecx /* ecx = (end of SYSENTER_stack) - esp */
991 cmpl $SIZEOF_SYSENTER_stack, %ecx
992 jb .Lnmi_from_sysenter_stack
993
994 /* Not on SYSENTER stack. */
Ingo Molnara49976d2015-06-08 09:49:11 +0200995 call do_nmi
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500996 jmp .Lrestore_all_notrace
Alexander van Heukelumd211af02008-11-24 15:38:45 +0100997
Andy Lutomirski75366562016-03-09 19:00:32 -0800998.Lnmi_from_sysenter_stack:
999 /*
1000 * We're on the SYSENTER stack. Switch off. No one (not even debug)
1001 * is using the thread stack right now, so it's safe for us to use it.
1002 */
Josh Poimboeuf946c1912016-10-20 11:34:40 -05001003 movl %esp, %ebx
Andy Lutomirski75366562016-03-09 19:00:32 -08001004 movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
1005 call do_nmi
Josh Poimboeuf946c1912016-10-20 11:34:40 -05001006 movl %ebx, %esp
Josh Poimboeuf1b002552016-09-21 16:03:59 -05001007 jmp .Lrestore_all_notrace
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001008
H. Peter Anvin34273f42014-05-04 10:36:22 -07001009#ifdef CONFIG_X86_ESPFIX32
Josh Poimboeuf1b002552016-09-21 16:03:59 -05001010.Lnmi_espfix_stack:
Ingo Molnar131484c2015-05-28 12:21:47 +02001011 /*
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001012 * create the pointer to lss back
1013 */
Ingo Molnara49976d2015-06-08 09:49:11 +02001014 pushl %ss
1015 pushl %esp
1016 addl $4, (%esp)
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001017 /* copy the iret frame of 12 bytes */
1018 .rept 3
Ingo Molnara49976d2015-06-08 09:49:11 +02001019 pushl 16(%esp)
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001020 .endr
Ingo Molnara49976d2015-06-08 09:49:11 +02001021 pushl %eax
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001022 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -05001023 ENCODE_FRAME_POINTER
Ingo Molnara49976d2015-06-08 09:49:11 +02001024 FIXUP_ESPFIX_STACK # %eax == %esp
1025 xorl %edx, %edx # zero error code
1026 call do_nmi
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001027 RESTORE_REGS
Ingo Molnara49976d2015-06-08 09:49:11 +02001028 lss 12+4(%esp), %esp # back to espfix stack
Josh Poimboeuf1b002552016-09-21 16:03:59 -05001029 jmp .Lirq_return
H. Peter Anvin34273f42014-05-04 10:36:22 -07001030#endif
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001031END(nmi)
1032
1033ENTRY(int3)
H. Peter Anvine59d1b02012-09-21 13:58:10 -07001034 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +02001035 pushl $-1 # mark this as an int
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001036 SAVE_ALL
Josh Poimboeuf946c1912016-10-20 11:34:40 -05001037 ENCODE_FRAME_POINTER
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001038 TRACE_IRQS_OFF
Ingo Molnara49976d2015-06-08 09:49:11 +02001039 xorl %edx, %edx # zero error code
1040 movl %esp, %eax # pt_regs pointer
1041 call do_int3
1042 jmp ret_from_exception
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001043END(int3)
1044
1045ENTRY(general_protection)
Ingo Molnara49976d2015-06-08 09:49:11 +02001046 pushl $do_general_protection
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -05001047 jmp common_exception
Alexander van Heukelumd211af02008-11-24 15:38:45 +01001048END(general_protection)
1049
Gleb Natapov631bc482010-10-14 11:22:52 +02001050#ifdef CONFIG_KVM_GUEST
1051ENTRY(async_page_fault)
H. Peter Anvine59d1b02012-09-21 13:58:10 -07001052 ASM_CLAC
Ingo Molnara49976d2015-06-08 09:49:11 +02001053 pushl $do_async_page_fault
Josh Poimboeuf7252c4c2016-09-21 16:04:00 -05001054 jmp common_exception
Sedat Dilek2ae9d292011-03-08 22:39:24 +01001055END(async_page_fault)
Gleb Natapov631bc482010-10-14 11:22:52 +02001056#endif
Andy Lutomirski2deb4be2016-07-14 13:22:55 -07001057
1058ENTRY(rewind_stack_do_exit)
1059 /* Prevent any naive code from trying to unwind to our caller. */
1060 xorl %ebp, %ebp
1061
1062 movl PER_CPU_VAR(cpu_current_top_of_stack), %esi
1063 leal -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%esi), %esp
1064
1065 call do_exit
10661: jmp 1b
1067END(rewind_stack_do_exit)