]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/entry/entry_32.S
x86/entry: Remap the TSS into the CPU entry area
[mirror_ubuntu-artful-kernel.git] / arch / x86 / entry / entry_32.S
CommitLineData
1da177e4 1/*
a49976d1 2 * Copyright (C) 1991,1992 Linus Torvalds
1da177e4 3 *
a49976d1 4 * entry_32.S contains the system-call and low-level fault and trap handling routines.
1da177e4 5 *
39e8701f 6 * Stack layout while running C code:
a49976d1
IM
7 * ptrace needs to have all registers on the stack.
8 * If the order here is changed, it needs to be
9 * updated in fork.c:copy_process(), signal.c:do_signal(),
1da177e4
LT
10 * ptrace.c and ptrace.h
11 *
12 * 0(%esp) - %ebx
13 * 4(%esp) - %ecx
14 * 8(%esp) - %edx
9b47feb7 15 * C(%esp) - %esi
1da177e4
LT
16 * 10(%esp) - %edi
17 * 14(%esp) - %ebp
18 * 18(%esp) - %eax
19 * 1C(%esp) - %ds
20 * 20(%esp) - %es
464d1a78 21 * 24(%esp) - %fs
ccbeed3a
TH
22 * 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
1da177e4
LT
29 */
30
1da177e4 31#include <linux/linkage.h>
d7e7528b 32#include <linux/err.h>
1da177e4 33#include <asm/thread_info.h>
55f327fa 34#include <asm/irqflags.h>
1da177e4
LT
35#include <asm/errno.h>
36#include <asm/segment.h>
37#include <asm/smp.h>
be44d2aa 38#include <asm/percpu.h>
ab68ed98 39#include <asm/processor-flags.h>
9b7dc567 40#include <asm/irq_vectors.h>
cd4d09ec 41#include <asm/cpufeatures.h>
b4ca46e4 42#include <asm/alternative-asm.h>
6837a54d 43#include <asm/asm.h>
e59d1b0a 44#include <asm/smap.h>
4d516f41 45#include <asm/frame.h>
1da177e4 46
ea714547
JO
47 .section .entry.text, "ax"
48
139ec7c4
RR
49/*
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")
d75cd22f 54 * ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
139ec7c4
RR
55 *
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
1da177e4 62#ifdef CONFIG_PREEMPT
a49976d1 63# define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
1da177e4 64#else
a49976d1
IM
65# define preempt_stop(clobbers)
66# define resume_kernel restore_all
1da177e4
LT
67#endif
68
55f327fa
IM
69.macro TRACE_IRQS_IRET
70#ifdef CONFIG_TRACE_IRQFLAGS
a49976d1
IM
71 testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off?
72 jz 1f
55f327fa
IM
73 TRACE_IRQS_ON
741:
75#endif
76.endm
77
ccbeed3a
TH
78/*
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
88
89 /* unfortunately push/pop can't be no-op */
90.macro PUSH_GS
a49976d1 91 pushl $0
ccbeed3a
TH
92.endm
93.macro POP_GS pop=0
a49976d1 94 addl $(4 + \pop), %esp
ccbeed3a
TH
95.endm
96.macro POP_GS_EX
97.endm
98
99 /* 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
a49976d1 114 pushl %gs
ccbeed3a
TH
115.endm
116
117.macro POP_GS pop=0
a49976d1 11898: popl %gs
ccbeed3a 119 .if \pop <> 0
9b47feb7 120 add $\pop, %esp
ccbeed3a
TH
121 .endif
122.endm
123.macro POP_GS_EX
124.pushsection .fixup, "ax"
a49976d1
IM
12599: movl $0, (%esp)
126 jmp 98b
ccbeed3a 127.popsection
a49976d1 128 _ASM_EXTABLE(98b, 99b)
ccbeed3a
TH
129.endm
130
131.macro PTGS_TO_GS
a49976d1 13298: mov PT_GS(%esp), %gs
ccbeed3a
TH
133.endm
134.macro PTGS_TO_GS_EX
135.pushsection .fixup, "ax"
a49976d1
IM
13699: movl $0, PT_GS(%esp)
137 jmp 98b
ccbeed3a 138.popsection
a49976d1 139 _ASM_EXTABLE(98b, 99b)
ccbeed3a
TH
140.endm
141
142.macro GS_TO_REG reg
a49976d1 143 movl %gs, \reg
ccbeed3a
TH
144.endm
145.macro REG_TO_PTGS reg
a49976d1 146 movl \reg, PT_GS(%esp)
ccbeed3a
TH
147.endm
148.macro SET_KERNEL_GS reg
a49976d1
IM
149 movl $(__KERNEL_STACK_CANARY), \reg
150 movl \reg, %gs
ccbeed3a
TH
151.endm
152
a49976d1 153#endif /* CONFIG_X86_32_LAZY_GS */
ccbeed3a 154
150ac78d 155.macro SAVE_ALL pt_regs_ax=%eax
f0d96110 156 cld
ccbeed3a 157 PUSH_GS
a49976d1
IM
158 pushl %fs
159 pushl %es
160 pushl %ds
150ac78d 161 pushl \pt_regs_ax
a49976d1
IM
162 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
ccbeed3a 173 SET_KERNEL_GS %edx
f0d96110 174.endm
1da177e4 175
946c1911
JP
176/*
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
f0d96110 192.macro RESTORE_INT_REGS
a49976d1
IM
193 popl %ebx
194 popl %ecx
195 popl %edx
196 popl %esi
197 popl %edi
198 popl %ebp
199 popl %eax
f0d96110 200.endm
1da177e4 201
ccbeed3a 202.macro RESTORE_REGS pop=0
f0d96110 203 RESTORE_INT_REGS
a49976d1
IM
2041: popl %ds
2052: popl %es
2063: popl %fs
ccbeed3a 207 POP_GS \pop
f0d96110 208.pushsection .fixup, "ax"
a49976d1
IM
2094: movl $0, (%esp)
210 jmp 1b
2115: movl $0, (%esp)
212 jmp 2b
2136: movl $0, (%esp)
214 jmp 3b
f95d47ca 215.popsection
a49976d1
IM
216 _ASM_EXTABLE(1b, 4b)
217 _ASM_EXTABLE(2b, 5b)
218 _ASM_EXTABLE(3b, 6b)
ccbeed3a 219 POP_GS_EX
f0d96110 220.endm
1da177e4 221
0100301b
BG
222/*
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
ebd57499
JP
254/*
255 * 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)
0100301b
BG
271/*
272 * A newly forked process directly context switches into this address.
273 *
274 * eax: prev task we switched from
616d2483
BG
275 * ebx: kernel thread func (NULL for user thread)
276 * edi: kernel thread arg
0100301b 277 */
1da177e4 278ENTRY(ret_from_fork)
ebd57499 279 call schedule_tail_wrapper
39e8701f 280
616d2483
BG
281 testl %ebx, %ebx
282 jnz 1f /* kernel threads are uncommon */
283
2842:
39e8701f 285 /* When we fork, we trace the syscall return in the child, too. */
ebd57499 286 movl %esp, %eax
39e8701f
AL
287 call syscall_return_slowpath
288 jmp restore_all
39e8701f 289
616d2483
BG
290 /* kernel thread */
2911: movl %edi, %eax
292 call *%ebx
39e8701f 293 /*
616d2483
BG
294 * A kernel thread is allowed to return here after successfully
295 * calling do_execve(). Exit to userspace to complete the execve()
296 * syscall.
39e8701f 297 */
616d2483
BG
298 movl $0, PT_EAX(%esp)
299 jmp 2b
300END(ret_from_fork)
6783eaa2 301
1da177e4
LT
302/*
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:
139ec7c4 312 preempt_stop(CLBR_ANY)
1da177e4 313ret_from_intr:
29a2e283 314#ifdef CONFIG_VM86
a49976d1
IM
315 movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
316 movb PT_CS(%esp), %al
317 andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
29a2e283
DA
318#else
319 /*
6783eaa2 320 * We can be coming here from child spawned by kernel_thread().
29a2e283 321 */
a49976d1
IM
322 movl PT_CS(%esp), %eax
323 andl $SEGMENT_RPL_MASK, %eax
29a2e283 324#endif
a49976d1
IM
325 cmpl $USER_RPL, %eax
326 jb resume_kernel # not returning to v8086 or userspace
f95d47ca 327
1da177e4 328ENTRY(resume_userspace)
5d73fc70 329 DISABLE_INTERRUPTS(CLBR_ANY)
e32e58a9 330 TRACE_IRQS_OFF
5d73fc70
AL
331 movl %esp, %eax
332 call prepare_exit_to_usermode
a49976d1 333 jmp restore_all
47a55cd7 334END(ret_from_exception)
1da177e4
LT
335
336#ifdef CONFIG_PREEMPT
337ENTRY(resume_kernel)
139ec7c4 338 DISABLE_INTERRUPTS(CLBR_ANY)
1b00255f 339.Lneed_resched:
a49976d1
IM
340 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
1b00255f 345 jmp .Lneed_resched
47a55cd7 346END(resume_kernel)
1da177e4
LT
347#endif
348
f2b37575
AL
349GLOBAL(__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 */
1b00255f 366 jmp .Lsysenter_past_esp
f2b37575
AL
367#endif
368
fda57b22
AL
369/*
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 */
4c8cd0c5 401ENTRY(entry_SYSENTER_32)
a49976d1 402 movl TSS_sysenter_sp0(%esp), %esp
1b00255f 403.Lsysenter_past_esp:
5f310f73 404 pushl $__USER_DS /* pt_regs->ss */
30bfa7b3 405 pushl %ebp /* pt_regs->sp (stashed in bp) */
5f310f73
AL
406 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
67f590e8 413 /*
f2b37575
AL
414 * 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
67f590e8
AL
416 * 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 *
f2b37575
AL
420 * 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 *
67f590e8
AL
426 * 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 */
f2b37575 432 testl $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, PT_EFLAGS(%esp)
67f590e8
AL
433 jnz .Lsysenter_fix_flags
434.Lsysenter_flags_fixed:
435
55f327fa 436 /*
5f310f73
AL
437 * User mode is traced as though IRQs are on, and SYSENTER
438 * turned them off.
e6e5494c 439 */
55f327fa 440 TRACE_IRQS_OFF
5f310f73
AL
441
442 movl %esp, %eax
443 call do_fast_syscall_32
91e2eea9
BO
444 /* XEN PV guests always use IRET path */
445 ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
446 "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
5f310f73
AL
447
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 */
3bd29515
AL
4521: mov PT_FS(%esp), %fs
453 PTGS_TO_GS
5f310f73
AL
454 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 */
5f310f73 460
c2c9b52f
AL
461 /*
462 * 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
5f310f73
AL
470 /*
471 * 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 */
88c15ec9
BO
474 sti
475 sysexit
af0575bb 476
a49976d1
IM
477.pushsection .fixup, "ax"
4782: movl $0, PT_FS(%esp)
479 jmp 1b
f95d47ca 480.popsection
a49976d1 481 _ASM_EXTABLE(1b, 2b)
ccbeed3a 482 PTGS_TO_GS_EX
67f590e8
AL
483
484.Lsysenter_fix_flags:
485 pushl $X86_EFLAGS_FIXED
486 popfl
487 jmp .Lsysenter_flags_fixed
f2b37575 488GLOBAL(__end_SYSENTER_singlestep_region)
4c8cd0c5 489ENDPROC(entry_SYSENTER_32)
1da177e4 490
fda57b22
AL
491/*
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 */
b2502b41 519ENTRY(entry_INT80_32)
e59d1b0a 520 ASM_CLAC
150ac78d 521 pushl %eax /* pt_regs->orig_ax */
5f310f73 522 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
150ac78d
AL
523
524 /*
a798f091
AL
525 * User mode is traced as though IRQs are on, and the interrupt gate
526 * turned them off.
150ac78d 527 */
a798f091 528 TRACE_IRQS_OFF
150ac78d
AL
529
530 movl %esp, %eax
a798f091 531 call do_int80_syscall_32
5f310f73 532.Lsyscall_32_done:
1da177e4
LT
533
534restore_all:
2e04bc76 535 TRACE_IRQS_IRET
1b00255f 536.Lrestore_all_notrace:
34273f41 537#ifdef CONFIG_X86_ESPFIX32
1b00255f 538 ALTERNATIVE "jmp .Lrestore_nocheck", "", X86_BUG_ESPFIX
58a5aac5 539
a49976d1
IM
540 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
1b00255f 550 je .Lldt_ss # returning to user-space with LDT SS
34273f41 551#endif
1b00255f 552.Lrestore_nocheck:
a49976d1 553 RESTORE_REGS 4 # skip orig_eax/error_code
1b00255f 554.Lirq_return:
3701d863 555 INTERRUPT_RETURN
1b00255f 556
a49976d1
IM
557.section .fixup, "ax"
558ENTRY(iret_exc )
559 pushl $0 # no error code
560 pushl $do_iret_error
7252c4c3 561 jmp common_exception
1da177e4 562.previous
1b00255f 563 _ASM_EXTABLE(.Lirq_return, iret_exc)
1da177e4 564
34273f41 565#ifdef CONFIG_X86_ESPFIX32
1b00255f 566.Lldt_ss:
dc4c2a0a
AH
567/*
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 */
72c511dd 578#define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
a49976d1
IM
579 mov %esp, %edx /* load kernel esp */
580 mov PT_OLDESP(%esp), %eax /* load userspace esp */
581 mov %dx, %ax /* eax: new kernel esp */
9b47feb7
DV
582 sub %eax, %edx /* offset (low word is 0) */
583 shr $16, %edx
a49976d1
IM
584 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
2e04bc76 590 * will soon execute iret and the tracer was already set to
a49976d1
IM
591 * the irqstate after the IRET:
592 */
fdbd518a 593 DISABLE_INTERRUPTS(CLBR_ANY)
a49976d1 594 lss (%esp), %esp /* switch to espfix segment */
1b00255f 595 jmp .Lrestore_nocheck
34273f41 596#endif
b2502b41 597ENDPROC(entry_INT80_32)
1da177e4 598
f0d96110 599.macro FIXUP_ESPFIX_STACK
dc4c2a0a
AH
600/*
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 */
34273f41 607#ifdef CONFIG_X86_ESPFIX32
dc4c2a0a 608 /* fixup the stack */
a49976d1
IM
609 mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
610 mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
9b47feb7 611 shl $16, %eax
a49976d1
IM
612 addl %esp, %eax /* the adjusted stack pointer */
613 pushl $__KERNEL_DS
614 pushl %eax
615 lss (%esp), %esp /* switch to the normal stack segment */
34273f41 616#endif
f0d96110
TH
617.endm
618.macro UNWIND_ESPFIX_STACK
34273f41 619#ifdef CONFIG_X86_ESPFIX32
a49976d1 620 movl %ss, %eax
f0d96110 621 /* see if on espfix stack */
a49976d1
IM
622 cmpw $__ESPFIX_SS, %ax
623 jne 27f
624 movl $__KERNEL_DS, %eax
625 movl %eax, %ds
626 movl %eax, %es
f0d96110
TH
627 /* switch to normal stack */
628 FIXUP_ESPFIX_STACK
62927:
34273f41 630#endif
f0d96110 631.endm
1da177e4
LT
632
633/*
3304c9c3
DV
634 * Build the entry stubs with some assembler magic.
635 * We pack 1 stub into every 8-byte block.
1da177e4 636 */
3304c9c3 637 .align 8
1da177e4 638ENTRY(irq_entries_start)
3304c9c3
DV
639 vector=FIRST_EXTERNAL_VECTOR
640 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
a49976d1 641 pushl $(~vector+0x80) /* Note: always in signed byte range */
3304c9c3
DV
642 vector=vector+1
643 jmp common_interrupt
3304c9c3
DV
644 .align 8
645 .endr
47a55cd7
JB
646END(irq_entries_start)
647
55f327fa
IM
648/*
649 * the CPU automatically disables interrupts when executing an IRQ vector,
650 * so IRQ-flags tracing has to follow that:
651 */
b7c6244f 652 .p2align CONFIG_X86_L1_CACHE_SHIFT
1da177e4 653common_interrupt:
e59d1b0a 654 ASM_CLAC
a49976d1 655 addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
1da177e4 656 SAVE_ALL
946c1911 657 ENCODE_FRAME_POINTER
55f327fa 658 TRACE_IRQS_OFF
a49976d1
IM
659 movl %esp, %eax
660 call do_IRQ
661 jmp ret_from_intr
47a55cd7 662ENDPROC(common_interrupt)
1da177e4 663
02cf94c3 664#define BUILD_INTERRUPT3(name, nr, fn) \
1da177e4 665ENTRY(name) \
e59d1b0a 666 ASM_CLAC; \
a49976d1 667 pushl $~(nr); \
fe7cacc1 668 SAVE_ALL; \
946c1911 669 ENCODE_FRAME_POINTER; \
55f327fa 670 TRACE_IRQS_OFF \
a49976d1
IM
671 movl %esp, %eax; \
672 call fn; \
673 jmp ret_from_intr; \
47a55cd7 674ENDPROC(name)
1da177e4 675
cf910e83
SA
676
677#ifdef CONFIG_TRACING
a49976d1 678# define TRACE_BUILD_INTERRUPT(name, nr) BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
cf910e83 679#else
a49976d1 680# define TRACE_BUILD_INTERRUPT(name, nr)
cf910e83
SA
681#endif
682
a49976d1
IM
683#define BUILD_INTERRUPT(name, nr) \
684 BUILD_INTERRUPT3(name, nr, smp_##name); \
cf910e83 685 TRACE_BUILD_INTERRUPT(name, nr)
02cf94c3 686
1da177e4 687/* The include is where all of the SMP etc. interrupts come from */
1164dd00 688#include <asm/entry_arch.h>
1da177e4 689
1da177e4 690ENTRY(coprocessor_error)
e59d1b0a 691 ASM_CLAC
a49976d1
IM
692 pushl $0
693 pushl $do_coprocessor_error
7252c4c3 694 jmp common_exception
47a55cd7 695END(coprocessor_error)
1da177e4
LT
696
697ENTRY(simd_coprocessor_error)
e59d1b0a 698 ASM_CLAC
a49976d1 699 pushl $0
40d2e763
BG
700#ifdef CONFIG_X86_INVD_BUG
701 /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
a49976d1
IM
702 ALTERNATIVE "pushl $do_general_protection", \
703 "pushl $do_simd_coprocessor_error", \
8e65f6e0 704 X86_FEATURE_XMM
40d2e763 705#else
a49976d1 706 pushl $do_simd_coprocessor_error
40d2e763 707#endif
7252c4c3 708 jmp common_exception
47a55cd7 709END(simd_coprocessor_error)
1da177e4
LT
710
711ENTRY(device_not_available)
e59d1b0a 712 ASM_CLAC
a49976d1
IM
713 pushl $-1 # mark this as an int
714 pushl $do_device_not_available
7252c4c3 715 jmp common_exception
47a55cd7 716END(device_not_available)
1da177e4 717
d3561b7f
RR
718#ifdef CONFIG_PARAVIRT
719ENTRY(native_iret)
3701d863 720 iret
6837a54d 721 _ASM_EXTABLE(native_iret, iret_exc)
47a55cd7 722END(native_iret)
d3561b7f
RR
723#endif
724
1da177e4 725ENTRY(overflow)
e59d1b0a 726 ASM_CLAC
a49976d1
IM
727 pushl $0
728 pushl $do_overflow
7252c4c3 729 jmp common_exception
47a55cd7 730END(overflow)
1da177e4
LT
731
732ENTRY(bounds)
e59d1b0a 733 ASM_CLAC
a49976d1
IM
734 pushl $0
735 pushl $do_bounds
7252c4c3 736 jmp common_exception
47a55cd7 737END(bounds)
1da177e4
LT
738
739ENTRY(invalid_op)
e59d1b0a 740 ASM_CLAC
a49976d1
IM
741 pushl $0
742 pushl $do_invalid_op
7252c4c3 743 jmp common_exception
47a55cd7 744END(invalid_op)
1da177e4
LT
745
746ENTRY(coprocessor_segment_overrun)
e59d1b0a 747 ASM_CLAC
a49976d1
IM
748 pushl $0
749 pushl $do_coprocessor_segment_overrun
7252c4c3 750 jmp common_exception
47a55cd7 751END(coprocessor_segment_overrun)
1da177e4
LT
752
753ENTRY(invalid_TSS)
e59d1b0a 754 ASM_CLAC
a49976d1 755 pushl $do_invalid_TSS
7252c4c3 756 jmp common_exception
47a55cd7 757END(invalid_TSS)
1da177e4
LT
758
759ENTRY(segment_not_present)
e59d1b0a 760 ASM_CLAC
a49976d1 761 pushl $do_segment_not_present
7252c4c3 762 jmp common_exception
47a55cd7 763END(segment_not_present)
1da177e4
LT
764
765ENTRY(stack_segment)
e59d1b0a 766 ASM_CLAC
a49976d1 767 pushl $do_stack_segment
7252c4c3 768 jmp common_exception
47a55cd7 769END(stack_segment)
1da177e4 770
1da177e4 771ENTRY(alignment_check)
e59d1b0a 772 ASM_CLAC
a49976d1 773 pushl $do_alignment_check
7252c4c3 774 jmp common_exception
47a55cd7 775END(alignment_check)
1da177e4 776
d28c4393 777ENTRY(divide_error)
e59d1b0a 778 ASM_CLAC
a49976d1
IM
779 pushl $0 # no error code
780 pushl $do_divide_error
7252c4c3 781 jmp common_exception
47a55cd7 782END(divide_error)
1da177e4
LT
783
784#ifdef CONFIG_X86_MCE
785ENTRY(machine_check)
e59d1b0a 786 ASM_CLAC
a49976d1
IM
787 pushl $0
788 pushl machine_check_vector
7252c4c3 789 jmp common_exception
47a55cd7 790END(machine_check)
1da177e4
LT
791#endif
792
793ENTRY(spurious_interrupt_bug)
e59d1b0a 794 ASM_CLAC
a49976d1
IM
795 pushl $0
796 pushl $do_spurious_interrupt_bug
7252c4c3 797 jmp common_exception
47a55cd7 798END(spurious_interrupt_bug)
1da177e4 799
5ead97c8
JF
800#ifdef CONFIG_XEN
801ENTRY(xen_hypervisor_callback)
a49976d1 802 pushl $-1 /* orig_ax = -1 => not a system call */
5ead97c8 803 SAVE_ALL
946c1911 804 ENCODE_FRAME_POINTER
5ead97c8 805 TRACE_IRQS_OFF
9ec2b804 806
a49976d1
IM
807 /*
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
9ec2b804 819
a49976d1 820 jmp xen_iret_crit_fixup
e2a81baf 821
e2a81baf 822ENTRY(xen_do_upcall)
a49976d1
IM
8231: mov %esp, %eax
824 call xen_evtchn_do_upcall
fdfd811d 825#ifndef CONFIG_PREEMPT
a49976d1 826 call xen_maybe_preempt_hcall
fdfd811d 827#endif
a49976d1 828 jmp ret_from_intr
5ead97c8
JF
829ENDPROC(xen_hypervisor_callback)
830
a49976d1
IM
831/*
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 */
5ead97c8 843ENTRY(xen_failsafe_callback)
a49976d1
IM
844 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
a349e23d
DV
850 /* EAX == 0 => Category 1 (Bad segment)
851 EAX != 0 => Category 2 (Bad IRET) */
a49976d1
IM
852 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 */
5ead97c8 858 SAVE_ALL
946c1911 859 ENCODE_FRAME_POINTER
a49976d1
IM
860 jmp ret_from_exception
861
862.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
5ead97c8 875.previous
a49976d1
IM
876 _ASM_EXTABLE(1b, 6b)
877 _ASM_EXTABLE(2b, 7b)
878 _ASM_EXTABLE(3b, 8b)
879 _ASM_EXTABLE(4b, 9b)
5ead97c8
JF
880ENDPROC(xen_failsafe_callback)
881
bc2b0331 882BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
38e20b07
SY
883 xen_evtchn_do_upcall)
884
a49976d1 885#endif /* CONFIG_XEN */
bc2b0331
S
886
887#if IS_ENABLED(CONFIG_HYPERV)
888
889BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
890 hyperv_vector_handler)
891
892#endif /* CONFIG_HYPERV */
5ead97c8 893
d211af05 894ENTRY(page_fault)
e59d1b0a 895 ASM_CLAC
a49976d1 896 pushl $do_page_fault
d211af05 897 ALIGN
7252c4c3
JP
898 jmp common_exception
899END(page_fault)
900
901common_exception:
ccbeed3a 902 /* the function address is in %gs's slot on the stack */
a49976d1
IM
903 pushl %fs
904 pushl %es
905 pushl %ds
906 pushl %eax
907 pushl %ebp
908 pushl %edi
909 pushl %esi
910 pushl %edx
911 pushl %ecx
912 pushl %ebx
946c1911 913 ENCODE_FRAME_POINTER
d211af05 914 cld
a49976d1
IM
915 movl $(__KERNEL_PERCPU), %ecx
916 movl %ecx, %fs
d211af05 917 UNWIND_ESPFIX_STACK
ccbeed3a 918 GS_TO_REG %ecx
a49976d1
IM
919 movl PT_GS(%esp), %edi # get the function address
920 movl PT_ORIG_EAX(%esp), %edx # get the error code
921 movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart
ccbeed3a
TH
922 REG_TO_PTGS %ecx
923 SET_KERNEL_GS %ecx
a49976d1
IM
924 movl $(__USER_DS), %ecx
925 movl %ecx, %ds
926 movl %ecx, %es
d211af05 927 TRACE_IRQS_OFF
a49976d1
IM
928 movl %esp, %eax # pt_regs pointer
929 call *%edi
930 jmp ret_from_exception
7252c4c3 931END(common_exception)
d211af05 932
d211af05 933ENTRY(debug)
7536656f
AL
934 /*
935 * #DB can happen at the first instruction of
936 * entry_SYSENTER_32 or in Xen's SYSENTER prologue. If this
937 * happens, then we will be running on a very small stack. We
938 * need to detect this condition and switch to the thread
939 * stack before calling any C code at all.
940 *
941 * If you edit this code, keep in mind that NMIs can happen in here.
942 */
e59d1b0a 943 ASM_CLAC
a49976d1 944 pushl $-1 # mark this as an int
d211af05 945 SAVE_ALL
946c1911 946 ENCODE_FRAME_POINTER
a49976d1
IM
947 xorl %edx, %edx # error code 0
948 movl %esp, %eax # pt_regs pointer
7536656f
AL
949
950 /* Are we currently on the SYSENTER stack? */
475b37e7
AL
951 movl PER_CPU_VAR(cpu_entry_area), %ecx
952 addl $CPU_ENTRY_AREA_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx
7536656f
AL
953 subl %eax, %ecx /* ecx = (end of SYSENTER_stack) - esp */
954 cmpl $SIZEOF_SYSENTER_stack, %ecx
955 jb .Ldebug_from_sysenter_stack
956
957 TRACE_IRQS_OFF
958 call do_debug
959 jmp ret_from_exception
960
961.Ldebug_from_sysenter_stack:
962 /* We're on the SYSENTER stack. Switch off. */
946c1911 963 movl %esp, %ebx
7536656f
AL
964 movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
965 TRACE_IRQS_OFF
a49976d1 966 call do_debug
946c1911 967 movl %ebx, %esp
a49976d1 968 jmp ret_from_exception
d211af05
AH
969END(debug)
970
971/*
7536656f
AL
972 * NMI is doubly nasty. It can happen on the first instruction of
973 * entry_SYSENTER_32 (just like #DB), but it can also interrupt the beginning
974 * of the #DB handler even if that #DB in turn hit before entry_SYSENTER_32
975 * switched stacks. We handle both conditions by simply checking whether we
976 * interrupted kernel code running on the SYSENTER stack.
d211af05
AH
977 */
978ENTRY(nmi)
e59d1b0a 979 ASM_CLAC
34273f41 980#ifdef CONFIG_X86_ESPFIX32
a49976d1
IM
981 pushl %eax
982 movl %ss, %eax
983 cmpw $__ESPFIX_SS, %ax
984 popl %eax
1b00255f 985 je .Lnmi_espfix_stack
34273f41 986#endif
7536656f
AL
987
988 pushl %eax # pt_regs->orig_ax
d211af05 989 SAVE_ALL
946c1911 990 ENCODE_FRAME_POINTER
a49976d1
IM
991 xorl %edx, %edx # zero error code
992 movl %esp, %eax # pt_regs pointer
7536656f
AL
993
994 /* Are we currently on the SYSENTER stack? */
475b37e7
AL
995 movl PER_CPU_VAR(cpu_entry_area), %ecx
996 addl $CPU_ENTRY_AREA_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx
7536656f
AL
997 subl %eax, %ecx /* ecx = (end of SYSENTER_stack) - esp */
998 cmpl $SIZEOF_SYSENTER_stack, %ecx
999 jb .Lnmi_from_sysenter_stack
1000
1001 /* Not on SYSENTER stack. */
a49976d1 1002 call do_nmi
1b00255f 1003 jmp .Lrestore_all_notrace
d211af05 1004
7536656f
AL
1005.Lnmi_from_sysenter_stack:
1006 /*
1007 * We're on the SYSENTER stack. Switch off. No one (not even debug)
1008 * is using the thread stack right now, so it's safe for us to use it.
1009 */
946c1911 1010 movl %esp, %ebx
7536656f
AL
1011 movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
1012 call do_nmi
946c1911 1013 movl %ebx, %esp
1b00255f 1014 jmp .Lrestore_all_notrace
d211af05 1015
34273f41 1016#ifdef CONFIG_X86_ESPFIX32
1b00255f 1017.Lnmi_espfix_stack:
131484c8 1018 /*
d211af05
AH
1019 * create the pointer to lss back
1020 */
a49976d1
IM
1021 pushl %ss
1022 pushl %esp
1023 addl $4, (%esp)
d211af05
AH
1024 /* copy the iret frame of 12 bytes */
1025 .rept 3
a49976d1 1026 pushl 16(%esp)
d211af05 1027 .endr
a49976d1 1028 pushl %eax
d211af05 1029 SAVE_ALL
946c1911 1030 ENCODE_FRAME_POINTER
a49976d1
IM
1031 FIXUP_ESPFIX_STACK # %eax == %esp
1032 xorl %edx, %edx # zero error code
1033 call do_nmi
d211af05 1034 RESTORE_REGS
a49976d1 1035 lss 12+4(%esp), %esp # back to espfix stack
1b00255f 1036 jmp .Lirq_return
34273f41 1037#endif
d211af05
AH
1038END(nmi)
1039
1040ENTRY(int3)
e59d1b0a 1041 ASM_CLAC
a49976d1 1042 pushl $-1 # mark this as an int
d211af05 1043 SAVE_ALL
946c1911 1044 ENCODE_FRAME_POINTER
d211af05 1045 TRACE_IRQS_OFF
a49976d1
IM
1046 xorl %edx, %edx # zero error code
1047 movl %esp, %eax # pt_regs pointer
1048 call do_int3
1049 jmp ret_from_exception
d211af05
AH
1050END(int3)
1051
1052ENTRY(general_protection)
a49976d1 1053 pushl $do_general_protection
7252c4c3 1054 jmp common_exception
d211af05
AH
1055END(general_protection)
1056
631bc487
GN
1057#ifdef CONFIG_KVM_GUEST
1058ENTRY(async_page_fault)
e59d1b0a 1059 ASM_CLAC
a49976d1 1060 pushl $do_async_page_fault
7252c4c3 1061 jmp common_exception
2ae9d293 1062END(async_page_fault)
631bc487 1063#endif
2deb4be2
AL
1064
1065ENTRY(rewind_stack_do_exit)
1066 /* Prevent any naive code from trying to unwind to our caller. */
1067 xorl %ebp, %ebp
1068
1069 movl PER_CPU_VAR(cpu_current_top_of_stack), %esi
1070 leal -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%esi), %esp
1071
1072 call do_exit
10731: jmp 1b
1074END(rewind_stack_do_exit)