]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/entry/entry_32.S
UBUNTU: Ubuntu-4.13.0-45.50
[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>
b9908a4d 46#include <asm/nospec-branch.h>
1da177e4 47
ea714547
JO
48 .section .entry.text, "ax"
49
139ec7c4
RR
50/*
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")
d75cd22f 55 * ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
139ec7c4
RR
56 *
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
1da177e4 63#ifdef CONFIG_PREEMPT
a49976d1 64# define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
1da177e4 65#else
a49976d1
IM
66# define preempt_stop(clobbers)
67# define resume_kernel restore_all
1da177e4
LT
68#endif
69
55f327fa
IM
70.macro TRACE_IRQS_IRET
71#ifdef CONFIG_TRACE_IRQFLAGS
a49976d1
IM
72 testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off?
73 jz 1f
55f327fa
IM
74 TRACE_IRQS_ON
751:
76#endif
77.endm
78
ccbeed3a
TH
79/*
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
89
90 /* unfortunately push/pop can't be no-op */
91.macro PUSH_GS
a49976d1 92 pushl $0
ccbeed3a
TH
93.endm
94.macro POP_GS pop=0
a49976d1 95 addl $(4 + \pop), %esp
ccbeed3a
TH
96.endm
97.macro POP_GS_EX
98.endm
99
100 /* 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
a49976d1 115 pushl %gs
ccbeed3a
TH
116.endm
117
118.macro POP_GS pop=0
a49976d1 11998: popl %gs
ccbeed3a 120 .if \pop <> 0
9b47feb7 121 add $\pop, %esp
ccbeed3a
TH
122 .endif
123.endm
124.macro POP_GS_EX
125.pushsection .fixup, "ax"
a49976d1
IM
12699: movl $0, (%esp)
127 jmp 98b
ccbeed3a 128.popsection
a49976d1 129 _ASM_EXTABLE(98b, 99b)
ccbeed3a
TH
130.endm
131
132.macro PTGS_TO_GS
a49976d1 13398: mov PT_GS(%esp), %gs
ccbeed3a
TH
134.endm
135.macro PTGS_TO_GS_EX
136.pushsection .fixup, "ax"
a49976d1
IM
13799: movl $0, PT_GS(%esp)
138 jmp 98b
ccbeed3a 139.popsection
a49976d1 140 _ASM_EXTABLE(98b, 99b)
ccbeed3a
TH
141.endm
142
143.macro GS_TO_REG reg
a49976d1 144 movl %gs, \reg
ccbeed3a
TH
145.endm
146.macro REG_TO_PTGS reg
a49976d1 147 movl \reg, PT_GS(%esp)
ccbeed3a
TH
148.endm
149.macro SET_KERNEL_GS reg
a49976d1
IM
150 movl $(__KERNEL_STACK_CANARY), \reg
151 movl \reg, %gs
ccbeed3a
TH
152.endm
153
a49976d1 154#endif /* CONFIG_X86_32_LAZY_GS */
ccbeed3a 155
150ac78d 156.macro SAVE_ALL pt_regs_ax=%eax
f0d96110 157 cld
ccbeed3a 158 PUSH_GS
a49976d1
IM
159 pushl %fs
160 pushl %es
161 pushl %ds
150ac78d 162 pushl \pt_regs_ax
a49976d1
IM
163 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
ccbeed3a 174 SET_KERNEL_GS %edx
f0d96110 175.endm
1da177e4 176
946c1911
JP
177/*
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
180 * is just setting the LSB, which makes it an invalid stack address and is also
181 * 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
189 orl $0x1, %ebp
190#endif
191.endm
192
f0d96110 193.macro RESTORE_INT_REGS
a49976d1
IM
194 popl %ebx
195 popl %ecx
196 popl %edx
197 popl %esi
198 popl %edi
199 popl %ebp
200 popl %eax
f0d96110 201.endm
1da177e4 202
ccbeed3a 203.macro RESTORE_REGS pop=0
f0d96110 204 RESTORE_INT_REGS
a49976d1
IM
2051: popl %ds
2062: popl %es
2073: popl %fs
ccbeed3a 208 POP_GS \pop
f0d96110 209.pushsection .fixup, "ax"
a49976d1
IM
2104: movl $0, (%esp)
211 jmp 1b
2125: movl $0, (%esp)
213 jmp 2b
2146: movl $0, (%esp)
215 jmp 3b
f95d47ca 216.popsection
a49976d1
IM
217 _ASM_EXTABLE(1b, 4b)
218 _ASM_EXTABLE(2b, 5b)
219 _ASM_EXTABLE(3b, 6b)
ccbeed3a 220 POP_GS_EX
f0d96110 221.endm
1da177e4 222
0100301b
BG
223/*
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
12aa317c
DW
246#ifdef CONFIG_RETPOLINE
247 /*
248 * When switching from a shallower to a deeper call stack
249 * the RSB may either underflow or use entries populated
250 * with userspace addresses. On CPUs where those concerns
251 * exist, overwrite the RSB with entries which capture
252 * speculative execution to prevent attack.
253 */
81fd59c4
BP
254 /* Clobbers %ebx */
255 FILL_RETURN_BUFFER RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
12aa317c
DW
256#endif
257
0100301b
BG
258 /* restore callee-saved registers */
259 popl %esi
260 popl %edi
261 popl %ebx
262 popl %ebp
263
264 jmp __switch_to
265END(__switch_to_asm)
266
ebd57499
JP
267/*
268 * The unwinder expects the last frame on the stack to always be at the same
269 * offset from the end of the page, which allows it to validate the stack.
270 * Calling schedule_tail() directly would break that convention because its an
271 * asmlinkage function so its argument has to be pushed on the stack. This
272 * wrapper creates a proper "end of stack" frame header before the call.
273 */
274ENTRY(schedule_tail_wrapper)
275 FRAME_BEGIN
276
277 pushl %eax
278 call schedule_tail
279 popl %eax
280
281 FRAME_END
282 ret
283ENDPROC(schedule_tail_wrapper)
0100301b
BG
284/*
285 * A newly forked process directly context switches into this address.
286 *
287 * eax: prev task we switched from
616d2483
BG
288 * ebx: kernel thread func (NULL for user thread)
289 * edi: kernel thread arg
0100301b 290 */
1da177e4 291ENTRY(ret_from_fork)
ebd57499 292 call schedule_tail_wrapper
39e8701f 293
616d2483
BG
294 testl %ebx, %ebx
295 jnz 1f /* kernel threads are uncommon */
296
2972:
39e8701f 298 /* When we fork, we trace the syscall return in the child, too. */
ebd57499 299 movl %esp, %eax
39e8701f
AL
300 call syscall_return_slowpath
301 jmp restore_all
39e8701f 302
616d2483
BG
303 /* kernel thread */
3041: movl %edi, %eax
b9908a4d 305 CALL_NOSPEC %ebx
39e8701f 306 /*
616d2483
BG
307 * A kernel thread is allowed to return here after successfully
308 * calling do_execve(). Exit to userspace to complete the execve()
309 * syscall.
39e8701f 310 */
616d2483
BG
311 movl $0, PT_EAX(%esp)
312 jmp 2b
313END(ret_from_fork)
6783eaa2 314
1da177e4
LT
315/*
316 * Return to user mode is not as complex as all this looks,
317 * but we want the default path for a system call return to
318 * go as quickly as possible which is why some of this is
319 * less clear than it otherwise should be.
320 */
321
322 # userspace resumption stub bypassing syscall exit tracing
323 ALIGN
324ret_from_exception:
139ec7c4 325 preempt_stop(CLBR_ANY)
1da177e4 326ret_from_intr:
29a2e283 327#ifdef CONFIG_VM86
a49976d1
IM
328 movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
329 movb PT_CS(%esp), %al
330 andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
29a2e283
DA
331#else
332 /*
6783eaa2 333 * We can be coming here from child spawned by kernel_thread().
29a2e283 334 */
a49976d1
IM
335 movl PT_CS(%esp), %eax
336 andl $SEGMENT_RPL_MASK, %eax
29a2e283 337#endif
a49976d1
IM
338 cmpl $USER_RPL, %eax
339 jb resume_kernel # not returning to v8086 or userspace
f95d47ca 340
1da177e4 341ENTRY(resume_userspace)
5d73fc70 342 DISABLE_INTERRUPTS(CLBR_ANY)
e32e58a9 343 TRACE_IRQS_OFF
5d73fc70
AL
344 movl %esp, %eax
345 call prepare_exit_to_usermode
a49976d1 346 jmp restore_all
47a55cd7 347END(ret_from_exception)
1da177e4
LT
348
349#ifdef CONFIG_PREEMPT
350ENTRY(resume_kernel)
139ec7c4 351 DISABLE_INTERRUPTS(CLBR_ANY)
1b00255f 352.Lneed_resched:
a49976d1
IM
353 cmpl $0, PER_CPU_VAR(__preempt_count)
354 jnz restore_all
355 testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
356 jz restore_all
357 call preempt_schedule_irq
1b00255f 358 jmp .Lneed_resched
47a55cd7 359END(resume_kernel)
1da177e4
LT
360#endif
361
f2b37575
AL
362GLOBAL(__begin_SYSENTER_singlestep_region)
363/*
364 * All code from here through __end_SYSENTER_singlestep_region is subject
365 * to being single-stepped if a user program sets TF and executes SYSENTER.
366 * There is absolutely nothing that we can do to prevent this from happening
367 * (thanks Intel!). To keep our handling of this situation as simple as
368 * possible, we handle TF just like AC and NT, except that our #DB handler
369 * will ignore all of the single-step traps generated in this range.
370 */
371
372#ifdef CONFIG_XEN
373/*
374 * Xen doesn't set %esp to be precisely what the normal SYSENTER
375 * entry point expects, so fix it up before using the normal path.
376 */
377ENTRY(xen_sysenter_target)
378 addl $5*4, %esp /* remove xen-provided frame */
1b00255f 379 jmp .Lsysenter_past_esp
f2b37575
AL
380#endif
381
fda57b22
AL
382/*
383 * 32-bit SYSENTER entry.
384 *
385 * 32-bit system calls through the vDSO's __kernel_vsyscall enter here
386 * if X86_FEATURE_SEP is available. This is the preferred system call
387 * entry on 32-bit systems.
388 *
389 * The SYSENTER instruction, in principle, should *only* occur in the
390 * vDSO. In practice, a small number of Android devices were shipped
391 * with a copy of Bionic that inlined a SYSENTER instruction. This
392 * never happened in any of Google's Bionic versions -- it only happened
393 * in a narrow range of Intel-provided versions.
394 *
395 * SYSENTER loads SS, ESP, CS, and EIP from previously programmed MSRs.
396 * IF and VM in RFLAGS are cleared (IOW: interrupts are off).
397 * SYSENTER does not save anything on the stack,
398 * and does not save old EIP (!!!), ESP, or EFLAGS.
399 *
400 * To avoid losing track of EFLAGS.VM (and thus potentially corrupting
401 * user and/or vm86 state), we explicitly disable the SYSENTER
402 * instruction in vm86 mode by reprogramming the MSRs.
403 *
404 * Arguments:
405 * eax system call number
406 * ebx arg1
407 * ecx arg2
408 * edx arg3
409 * esi arg4
410 * edi arg5
411 * ebp user stack
412 * 0(%ebp) arg6
413 */
4c8cd0c5 414ENTRY(entry_SYSENTER_32)
a49976d1 415 movl TSS_sysenter_sp0(%esp), %esp
1b00255f 416.Lsysenter_past_esp:
5f310f73 417 pushl $__USER_DS /* pt_regs->ss */
30bfa7b3 418 pushl %ebp /* pt_regs->sp (stashed in bp) */
5f310f73
AL
419 pushfl /* pt_regs->flags (except IF = 0) */
420 orl $X86_EFLAGS_IF, (%esp) /* Fix IF */
421 pushl $__USER_CS /* pt_regs->cs */
422 pushl $0 /* pt_regs->ip = 0 (placeholder) */
423 pushl %eax /* pt_regs->orig_ax */
424 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
425
67f590e8 426 /*
f2b37575
AL
427 * SYSENTER doesn't filter flags, so we need to clear NT, AC
428 * and TF ourselves. To save a few cycles, we can check whether
67f590e8
AL
429 * either was set instead of doing an unconditional popfq.
430 * This needs to happen before enabling interrupts so that
431 * we don't get preempted with NT set.
432 *
f2b37575
AL
433 * If TF is set, we will single-step all the way to here -- do_debug
434 * will ignore all the traps. (Yes, this is slow, but so is
435 * single-stepping in general. This allows us to avoid having
436 * a more complicated code to handle the case where a user program
437 * forces us to single-step through the SYSENTER entry code.)
438 *
67f590e8
AL
439 * NB.: .Lsysenter_fix_flags is a label with the code under it moved
440 * out-of-line as an optimization: NT is unlikely to be set in the
441 * majority of the cases and instead of polluting the I$ unnecessarily,
442 * we're keeping that code behind a branch which will predict as
443 * not-taken and therefore its instructions won't be fetched.
444 */
f2b37575 445 testl $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, PT_EFLAGS(%esp)
67f590e8
AL
446 jnz .Lsysenter_fix_flags
447.Lsysenter_flags_fixed:
448
55f327fa 449 /*
5f310f73
AL
450 * User mode is traced as though IRQs are on, and SYSENTER
451 * turned them off.
e6e5494c 452 */
55f327fa 453 TRACE_IRQS_OFF
5f310f73
AL
454
455 movl %esp, %eax
456 call do_fast_syscall_32
91e2eea9
BO
457 /* XEN PV guests always use IRET path */
458 ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
459 "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
5f310f73
AL
460
461/* Opportunistic SYSEXIT */
462 TRACE_IRQS_ON /* User mode traces as IRQs on. */
463 movl PT_EIP(%esp), %edx /* pt_regs->ip */
464 movl PT_OLDESP(%esp), %ecx /* pt_regs->sp */
3bd29515
AL
4651: mov PT_FS(%esp), %fs
466 PTGS_TO_GS
5f310f73
AL
467 popl %ebx /* pt_regs->bx */
468 addl $2*4, %esp /* skip pt_regs->cx and pt_regs->dx */
469 popl %esi /* pt_regs->si */
470 popl %edi /* pt_regs->di */
471 popl %ebp /* pt_regs->bp */
472 popl %eax /* pt_regs->ax */
5f310f73 473
c2c9b52f
AL
474 /*
475 * Restore all flags except IF. (We restore IF separately because
476 * STI gives a one-instruction window in which we won't be interrupted,
477 * whereas POPF does not.)
478 */
479 addl $PT_EFLAGS-PT_DS, %esp /* point esp at pt_regs->flags */
480 btr $X86_EFLAGS_IF_BIT, (%esp)
481 popfl
482
5f310f73
AL
483 /*
484 * Return back to the vDSO, which will pop ecx and edx.
485 * Don't bother with DS and ES (they already contain __USER_DS).
486 */
88c15ec9
BO
487 sti
488 sysexit
af0575bb 489
a49976d1
IM
490.pushsection .fixup, "ax"
4912: movl $0, PT_FS(%esp)
492 jmp 1b
f95d47ca 493.popsection
a49976d1 494 _ASM_EXTABLE(1b, 2b)
ccbeed3a 495 PTGS_TO_GS_EX
67f590e8
AL
496
497.Lsysenter_fix_flags:
498 pushl $X86_EFLAGS_FIXED
499 popfl
500 jmp .Lsysenter_flags_fixed
f2b37575 501GLOBAL(__end_SYSENTER_singlestep_region)
4c8cd0c5 502ENDPROC(entry_SYSENTER_32)
1da177e4 503
fda57b22
AL
504/*
505 * 32-bit legacy system call entry.
506 *
507 * 32-bit x86 Linux system calls traditionally used the INT $0x80
508 * instruction. INT $0x80 lands here.
509 *
510 * This entry point can be used by any 32-bit perform system calls.
511 * Instances of INT $0x80 can be found inline in various programs and
512 * libraries. It is also used by the vDSO's __kernel_vsyscall
513 * fallback for hardware that doesn't support a faster entry method.
514 * Restarted 32-bit system calls also fall back to INT $0x80
515 * regardless of what instruction was originally used to do the system
516 * call. (64-bit programs can use INT $0x80 as well, but they can
517 * only run on 64-bit kernels and therefore land in
518 * entry_INT80_compat.)
519 *
520 * This is considered a slow path. It is not used by most libc
521 * implementations on modern hardware except during process startup.
522 *
523 * Arguments:
524 * eax system call number
525 * ebx arg1
526 * ecx arg2
527 * edx arg3
528 * esi arg4
529 * edi arg5
530 * ebp arg6
531 */
b2502b41 532ENTRY(entry_INT80_32)
e59d1b0a 533 ASM_CLAC
150ac78d 534 pushl %eax /* pt_regs->orig_ax */
5f310f73 535 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
150ac78d
AL
536
537 /*
a798f091
AL
538 * User mode is traced as though IRQs are on, and the interrupt gate
539 * turned them off.
150ac78d 540 */
a798f091 541 TRACE_IRQS_OFF
150ac78d
AL
542
543 movl %esp, %eax
a798f091 544 call do_int80_syscall_32
5f310f73 545.Lsyscall_32_done:
1da177e4
LT
546
547restore_all:
2e04bc76 548 TRACE_IRQS_IRET
1b00255f 549.Lrestore_all_notrace:
34273f41 550#ifdef CONFIG_X86_ESPFIX32
1b00255f 551 ALTERNATIVE "jmp .Lrestore_nocheck", "", X86_BUG_ESPFIX
58a5aac5 552
a49976d1
IM
553 movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
554 /*
555 * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
556 * are returning to the kernel.
557 * See comments in process.c:copy_thread() for details.
558 */
559 movb PT_OLDSS(%esp), %ah
560 movb PT_CS(%esp), %al
561 andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
562 cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
1b00255f 563 je .Lldt_ss # returning to user-space with LDT SS
34273f41 564#endif
1b00255f 565.Lrestore_nocheck:
a49976d1 566 RESTORE_REGS 4 # skip orig_eax/error_code
1b00255f 567.Lirq_return:
3701d863 568 INTERRUPT_RETURN
1b00255f 569
a49976d1
IM
570.section .fixup, "ax"
571ENTRY(iret_exc )
572 pushl $0 # no error code
573 pushl $do_iret_error
7252c4c3 574 jmp common_exception
1da177e4 575.previous
1b00255f 576 _ASM_EXTABLE(.Lirq_return, iret_exc)
1da177e4 577
34273f41 578#ifdef CONFIG_X86_ESPFIX32
1b00255f 579.Lldt_ss:
dc4c2a0a
AH
580/*
581 * Setup and switch to ESPFIX stack
582 *
583 * We're returning to userspace with a 16 bit stack. The CPU will not
584 * restore the high word of ESP for us on executing iret... This is an
585 * "official" bug of all the x86-compatible CPUs, which we can work
586 * around to make dosemu and wine happy. We do this by preloading the
587 * high word of ESP with the high word of the userspace ESP while
588 * compensating for the offset by changing to the ESPFIX segment with
589 * a base address that matches for the difference.
590 */
72c511dd 591#define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
a49976d1
IM
592 mov %esp, %edx /* load kernel esp */
593 mov PT_OLDESP(%esp), %eax /* load userspace esp */
594 mov %dx, %ax /* eax: new kernel esp */
9b47feb7
DV
595 sub %eax, %edx /* offset (low word is 0) */
596 shr $16, %edx
a49976d1
IM
597 mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
598 mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
599 pushl $__ESPFIX_SS
600 pushl %eax /* new kernel esp */
601 /*
602 * Disable interrupts, but do not irqtrace this section: we
2e04bc76 603 * will soon execute iret and the tracer was already set to
a49976d1
IM
604 * the irqstate after the IRET:
605 */
fdbd518a 606 DISABLE_INTERRUPTS(CLBR_ANY)
a49976d1 607 lss (%esp), %esp /* switch to espfix segment */
1b00255f 608 jmp .Lrestore_nocheck
34273f41 609#endif
b2502b41 610ENDPROC(entry_INT80_32)
1da177e4 611
f0d96110 612.macro FIXUP_ESPFIX_STACK
dc4c2a0a
AH
613/*
614 * Switch back for ESPFIX stack to the normal zerobased stack
615 *
616 * We can't call C functions using the ESPFIX stack. This code reads
617 * the high word of the segment base from the GDT and swiches to the
618 * normal stack and adjusts ESP with the matching offset.
619 */
34273f41 620#ifdef CONFIG_X86_ESPFIX32
dc4c2a0a 621 /* fixup the stack */
a49976d1
IM
622 mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
623 mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
9b47feb7 624 shl $16, %eax
a49976d1
IM
625 addl %esp, %eax /* the adjusted stack pointer */
626 pushl $__KERNEL_DS
627 pushl %eax
628 lss (%esp), %esp /* switch to the normal stack segment */
34273f41 629#endif
f0d96110
TH
630.endm
631.macro UNWIND_ESPFIX_STACK
34273f41 632#ifdef CONFIG_X86_ESPFIX32
a49976d1 633 movl %ss, %eax
f0d96110 634 /* see if on espfix stack */
a49976d1
IM
635 cmpw $__ESPFIX_SS, %ax
636 jne 27f
637 movl $__KERNEL_DS, %eax
638 movl %eax, %ds
639 movl %eax, %es
f0d96110
TH
640 /* switch to normal stack */
641 FIXUP_ESPFIX_STACK
64227:
34273f41 643#endif
f0d96110 644.endm
1da177e4
LT
645
646/*
3304c9c3
DV
647 * Build the entry stubs with some assembler magic.
648 * We pack 1 stub into every 8-byte block.
1da177e4 649 */
3304c9c3 650 .align 8
1da177e4 651ENTRY(irq_entries_start)
3304c9c3
DV
652 vector=FIRST_EXTERNAL_VECTOR
653 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
a49976d1 654 pushl $(~vector+0x80) /* Note: always in signed byte range */
3304c9c3
DV
655 vector=vector+1
656 jmp common_interrupt
3304c9c3
DV
657 .align 8
658 .endr
47a55cd7
JB
659END(irq_entries_start)
660
55f327fa
IM
661/*
662 * the CPU automatically disables interrupts when executing an IRQ vector,
663 * so IRQ-flags tracing has to follow that:
664 */
b7c6244f 665 .p2align CONFIG_X86_L1_CACHE_SHIFT
1da177e4 666common_interrupt:
e59d1b0a 667 ASM_CLAC
a49976d1 668 addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
1da177e4 669 SAVE_ALL
946c1911 670 ENCODE_FRAME_POINTER
55f327fa 671 TRACE_IRQS_OFF
a49976d1
IM
672 movl %esp, %eax
673 call do_IRQ
674 jmp ret_from_intr
47a55cd7 675ENDPROC(common_interrupt)
1da177e4 676
02cf94c3 677#define BUILD_INTERRUPT3(name, nr, fn) \
1da177e4 678ENTRY(name) \
e59d1b0a 679 ASM_CLAC; \
a49976d1 680 pushl $~(nr); \
fe7cacc1 681 SAVE_ALL; \
946c1911 682 ENCODE_FRAME_POINTER; \
55f327fa 683 TRACE_IRQS_OFF \
a49976d1
IM
684 movl %esp, %eax; \
685 call fn; \
686 jmp ret_from_intr; \
47a55cd7 687ENDPROC(name)
1da177e4 688
cf910e83
SA
689
690#ifdef CONFIG_TRACING
a49976d1 691# define TRACE_BUILD_INTERRUPT(name, nr) BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
cf910e83 692#else
a49976d1 693# define TRACE_BUILD_INTERRUPT(name, nr)
cf910e83
SA
694#endif
695
a49976d1
IM
696#define BUILD_INTERRUPT(name, nr) \
697 BUILD_INTERRUPT3(name, nr, smp_##name); \
cf910e83 698 TRACE_BUILD_INTERRUPT(name, nr)
02cf94c3 699
1da177e4 700/* The include is where all of the SMP etc. interrupts come from */
1164dd00 701#include <asm/entry_arch.h>
1da177e4 702
1da177e4 703ENTRY(coprocessor_error)
e59d1b0a 704 ASM_CLAC
a49976d1
IM
705 pushl $0
706 pushl $do_coprocessor_error
7252c4c3 707 jmp common_exception
47a55cd7 708END(coprocessor_error)
1da177e4
LT
709
710ENTRY(simd_coprocessor_error)
e59d1b0a 711 ASM_CLAC
a49976d1 712 pushl $0
40d2e763
BG
713#ifdef CONFIG_X86_INVD_BUG
714 /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
a49976d1
IM
715 ALTERNATIVE "pushl $do_general_protection", \
716 "pushl $do_simd_coprocessor_error", \
8e65f6e0 717 X86_FEATURE_XMM
40d2e763 718#else
a49976d1 719 pushl $do_simd_coprocessor_error
40d2e763 720#endif
7252c4c3 721 jmp common_exception
47a55cd7 722END(simd_coprocessor_error)
1da177e4
LT
723
724ENTRY(device_not_available)
e59d1b0a 725 ASM_CLAC
a49976d1
IM
726 pushl $-1 # mark this as an int
727 pushl $do_device_not_available
7252c4c3 728 jmp common_exception
47a55cd7 729END(device_not_available)
1da177e4 730
d3561b7f
RR
731#ifdef CONFIG_PARAVIRT
732ENTRY(native_iret)
3701d863 733 iret
6837a54d 734 _ASM_EXTABLE(native_iret, iret_exc)
47a55cd7 735END(native_iret)
d3561b7f
RR
736#endif
737
1da177e4 738ENTRY(overflow)
e59d1b0a 739 ASM_CLAC
a49976d1
IM
740 pushl $0
741 pushl $do_overflow
7252c4c3 742 jmp common_exception
47a55cd7 743END(overflow)
1da177e4
LT
744
745ENTRY(bounds)
e59d1b0a 746 ASM_CLAC
a49976d1
IM
747 pushl $0
748 pushl $do_bounds
7252c4c3 749 jmp common_exception
47a55cd7 750END(bounds)
1da177e4
LT
751
752ENTRY(invalid_op)
e59d1b0a 753 ASM_CLAC
a49976d1
IM
754 pushl $0
755 pushl $do_invalid_op
7252c4c3 756 jmp common_exception
47a55cd7 757END(invalid_op)
1da177e4
LT
758
759ENTRY(coprocessor_segment_overrun)
e59d1b0a 760 ASM_CLAC
a49976d1
IM
761 pushl $0
762 pushl $do_coprocessor_segment_overrun
7252c4c3 763 jmp common_exception
47a55cd7 764END(coprocessor_segment_overrun)
1da177e4
LT
765
766ENTRY(invalid_TSS)
e59d1b0a 767 ASM_CLAC
a49976d1 768 pushl $do_invalid_TSS
7252c4c3 769 jmp common_exception
47a55cd7 770END(invalid_TSS)
1da177e4
LT
771
772ENTRY(segment_not_present)
e59d1b0a 773 ASM_CLAC
a49976d1 774 pushl $do_segment_not_present
7252c4c3 775 jmp common_exception
47a55cd7 776END(segment_not_present)
1da177e4
LT
777
778ENTRY(stack_segment)
e59d1b0a 779 ASM_CLAC
a49976d1 780 pushl $do_stack_segment
7252c4c3 781 jmp common_exception
47a55cd7 782END(stack_segment)
1da177e4 783
1da177e4 784ENTRY(alignment_check)
e59d1b0a 785 ASM_CLAC
a49976d1 786 pushl $do_alignment_check
7252c4c3 787 jmp common_exception
47a55cd7 788END(alignment_check)
1da177e4 789
d28c4393 790ENTRY(divide_error)
e59d1b0a 791 ASM_CLAC
a49976d1
IM
792 pushl $0 # no error code
793 pushl $do_divide_error
7252c4c3 794 jmp common_exception
47a55cd7 795END(divide_error)
1da177e4
LT
796
797#ifdef CONFIG_X86_MCE
798ENTRY(machine_check)
e59d1b0a 799 ASM_CLAC
a49976d1
IM
800 pushl $0
801 pushl machine_check_vector
7252c4c3 802 jmp common_exception
47a55cd7 803END(machine_check)
1da177e4
LT
804#endif
805
806ENTRY(spurious_interrupt_bug)
e59d1b0a 807 ASM_CLAC
a49976d1
IM
808 pushl $0
809 pushl $do_spurious_interrupt_bug
7252c4c3 810 jmp common_exception
47a55cd7 811END(spurious_interrupt_bug)
1da177e4 812
5ead97c8
JF
813#ifdef CONFIG_XEN
814ENTRY(xen_hypervisor_callback)
a49976d1 815 pushl $-1 /* orig_ax = -1 => not a system call */
5ead97c8 816 SAVE_ALL
946c1911 817 ENCODE_FRAME_POINTER
5ead97c8 818 TRACE_IRQS_OFF
9ec2b804 819
a49976d1
IM
820 /*
821 * Check to see if we got the event in the critical
822 * region in xen_iret_direct, after we've reenabled
823 * events and checked for pending events. This simulates
824 * iret instruction's behaviour where it delivers a
825 * pending interrupt when enabling interrupts:
826 */
827 movl PT_EIP(%esp), %eax
828 cmpl $xen_iret_start_crit, %eax
829 jb 1f
830 cmpl $xen_iret_end_crit, %eax
831 jae 1f
9ec2b804 832
a49976d1 833 jmp xen_iret_crit_fixup
e2a81baf 834
e2a81baf 835ENTRY(xen_do_upcall)
a49976d1
IM
8361: mov %esp, %eax
837 call xen_evtchn_do_upcall
fdfd811d 838#ifndef CONFIG_PREEMPT
a49976d1 839 call xen_maybe_preempt_hcall
fdfd811d 840#endif
a49976d1 841 jmp ret_from_intr
5ead97c8
JF
842ENDPROC(xen_hypervisor_callback)
843
a49976d1
IM
844/*
845 * Hypervisor uses this for application faults while it executes.
846 * We get here for two reasons:
847 * 1. Fault while reloading DS, ES, FS or GS
848 * 2. Fault while executing IRET
849 * Category 1 we fix up by reattempting the load, and zeroing the segment
850 * register if the load fails.
851 * Category 2 we fix up by jumping to do_iret_error. We cannot use the
852 * normal Linux return path in this case because if we use the IRET hypercall
853 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
854 * We distinguish between categories by maintaining a status value in EAX.
855 */
5ead97c8 856ENTRY(xen_failsafe_callback)
a49976d1
IM
857 pushl %eax
858 movl $1, %eax
8591: mov 4(%esp), %ds
8602: mov 8(%esp), %es
8613: mov 12(%esp), %fs
8624: mov 16(%esp), %gs
a349e23d
DV
863 /* EAX == 0 => Category 1 (Bad segment)
864 EAX != 0 => Category 2 (Bad IRET) */
a49976d1
IM
865 testl %eax, %eax
866 popl %eax
867 lea 16(%esp), %esp
868 jz 5f
869 jmp iret_exc
8705: pushl $-1 /* orig_ax = -1 => not a system call */
5ead97c8 871 SAVE_ALL
946c1911 872 ENCODE_FRAME_POINTER
a49976d1
IM
873 jmp ret_from_exception
874
875.section .fixup, "ax"
8766: xorl %eax, %eax
877 movl %eax, 4(%esp)
878 jmp 1b
8797: xorl %eax, %eax
880 movl %eax, 8(%esp)
881 jmp 2b
8828: xorl %eax, %eax
883 movl %eax, 12(%esp)
884 jmp 3b
8859: xorl %eax, %eax
886 movl %eax, 16(%esp)
887 jmp 4b
5ead97c8 888.previous
a49976d1
IM
889 _ASM_EXTABLE(1b, 6b)
890 _ASM_EXTABLE(2b, 7b)
891 _ASM_EXTABLE(3b, 8b)
892 _ASM_EXTABLE(4b, 9b)
5ead97c8
JF
893ENDPROC(xen_failsafe_callback)
894
bc2b0331 895BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
38e20b07
SY
896 xen_evtchn_do_upcall)
897
a49976d1 898#endif /* CONFIG_XEN */
bc2b0331
S
899
900#if IS_ENABLED(CONFIG_HYPERV)
901
902BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
903 hyperv_vector_handler)
904
905#endif /* CONFIG_HYPERV */
5ead97c8 906
d211af05 907ENTRY(page_fault)
e59d1b0a 908 ASM_CLAC
a49976d1 909 pushl $do_page_fault
d211af05 910 ALIGN
7252c4c3
JP
911 jmp common_exception
912END(page_fault)
913
914common_exception:
ccbeed3a 915 /* the function address is in %gs's slot on the stack */
a49976d1
IM
916 pushl %fs
917 pushl %es
918 pushl %ds
919 pushl %eax
920 pushl %ebp
921 pushl %edi
922 pushl %esi
923 pushl %edx
924 pushl %ecx
925 pushl %ebx
946c1911 926 ENCODE_FRAME_POINTER
d211af05 927 cld
a49976d1
IM
928 movl $(__KERNEL_PERCPU), %ecx
929 movl %ecx, %fs
d211af05 930 UNWIND_ESPFIX_STACK
ccbeed3a 931 GS_TO_REG %ecx
a49976d1
IM
932 movl PT_GS(%esp), %edi # get the function address
933 movl PT_ORIG_EAX(%esp), %edx # get the error code
934 movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart
ccbeed3a
TH
935 REG_TO_PTGS %ecx
936 SET_KERNEL_GS %ecx
a49976d1
IM
937 movl $(__USER_DS), %ecx
938 movl %ecx, %ds
939 movl %ecx, %es
d211af05 940 TRACE_IRQS_OFF
a49976d1 941 movl %esp, %eax # pt_regs pointer
b9908a4d 942 CALL_NOSPEC %edi
a49976d1 943 jmp ret_from_exception
7252c4c3 944END(common_exception)
d211af05 945
d211af05 946ENTRY(debug)
7536656f
AL
947 /*
948 * #DB can happen at the first instruction of
949 * entry_SYSENTER_32 or in Xen's SYSENTER prologue. If this
950 * happens, then we will be running on a very small stack. We
951 * need to detect this condition and switch to the thread
952 * stack before calling any C code at all.
953 *
954 * If you edit this code, keep in mind that NMIs can happen in here.
955 */
e59d1b0a 956 ASM_CLAC
a49976d1 957 pushl $-1 # mark this as an int
d211af05 958 SAVE_ALL
946c1911 959 ENCODE_FRAME_POINTER
a49976d1
IM
960 xorl %edx, %edx # error code 0
961 movl %esp, %eax # pt_regs pointer
7536656f
AL
962
963 /* Are we currently on the SYSENTER stack? */
475b37e7 964 movl PER_CPU_VAR(cpu_entry_area), %ecx
e0437c47
DH
965 addl $CPU_ENTRY_AREA_entry_stack + SIZEOF_entry_stack, %ecx
966 subl %eax, %ecx /* ecx = (end of entry_stack) - esp */
967 cmpl $SIZEOF_entry_stack, %ecx
7536656f
AL
968 jb .Ldebug_from_sysenter_stack
969
970 TRACE_IRQS_OFF
971 call do_debug
972 jmp ret_from_exception
973
974.Ldebug_from_sysenter_stack:
975 /* We're on the SYSENTER stack. Switch off. */
946c1911 976 movl %esp, %ebx
7536656f
AL
977 movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
978 TRACE_IRQS_OFF
a49976d1 979 call do_debug
946c1911 980 movl %ebx, %esp
a49976d1 981 jmp ret_from_exception
d211af05
AH
982END(debug)
983
984/*
7536656f
AL
985 * NMI is doubly nasty. It can happen on the first instruction of
986 * entry_SYSENTER_32 (just like #DB), but it can also interrupt the beginning
987 * of the #DB handler even if that #DB in turn hit before entry_SYSENTER_32
988 * switched stacks. We handle both conditions by simply checking whether we
989 * interrupted kernel code running on the SYSENTER stack.
d211af05
AH
990 */
991ENTRY(nmi)
e59d1b0a 992 ASM_CLAC
34273f41 993#ifdef CONFIG_X86_ESPFIX32
a49976d1
IM
994 pushl %eax
995 movl %ss, %eax
996 cmpw $__ESPFIX_SS, %ax
997 popl %eax
1b00255f 998 je .Lnmi_espfix_stack
34273f41 999#endif
7536656f
AL
1000
1001 pushl %eax # pt_regs->orig_ax
d211af05 1002 SAVE_ALL
946c1911 1003 ENCODE_FRAME_POINTER
a49976d1
IM
1004 xorl %edx, %edx # zero error code
1005 movl %esp, %eax # pt_regs pointer
7536656f
AL
1006
1007 /* Are we currently on the SYSENTER stack? */
475b37e7 1008 movl PER_CPU_VAR(cpu_entry_area), %ecx
e0437c47
DH
1009 addl $CPU_ENTRY_AREA_entry_stack + SIZEOF_entry_stack, %ecx
1010 subl %eax, %ecx /* ecx = (end of entry_stack) - esp */
1011 cmpl $SIZEOF_entry_stack, %ecx
7536656f
AL
1012 jb .Lnmi_from_sysenter_stack
1013
1014 /* Not on SYSENTER stack. */
a49976d1 1015 call do_nmi
1b00255f 1016 jmp .Lrestore_all_notrace
d211af05 1017
7536656f
AL
1018.Lnmi_from_sysenter_stack:
1019 /*
1020 * We're on the SYSENTER stack. Switch off. No one (not even debug)
1021 * is using the thread stack right now, so it's safe for us to use it.
1022 */
946c1911 1023 movl %esp, %ebx
7536656f
AL
1024 movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
1025 call do_nmi
946c1911 1026 movl %ebx, %esp
1b00255f 1027 jmp .Lrestore_all_notrace
d211af05 1028
34273f41 1029#ifdef CONFIG_X86_ESPFIX32
1b00255f 1030.Lnmi_espfix_stack:
131484c8 1031 /*
d211af05
AH
1032 * create the pointer to lss back
1033 */
a49976d1
IM
1034 pushl %ss
1035 pushl %esp
1036 addl $4, (%esp)
d211af05
AH
1037 /* copy the iret frame of 12 bytes */
1038 .rept 3
a49976d1 1039 pushl 16(%esp)
d211af05 1040 .endr
a49976d1 1041 pushl %eax
d211af05 1042 SAVE_ALL
946c1911 1043 ENCODE_FRAME_POINTER
a49976d1
IM
1044 FIXUP_ESPFIX_STACK # %eax == %esp
1045 xorl %edx, %edx # zero error code
1046 call do_nmi
d211af05 1047 RESTORE_REGS
a49976d1 1048 lss 12+4(%esp), %esp # back to espfix stack
1b00255f 1049 jmp .Lirq_return
34273f41 1050#endif
d211af05
AH
1051END(nmi)
1052
1053ENTRY(int3)
e59d1b0a 1054 ASM_CLAC
a49976d1 1055 pushl $-1 # mark this as an int
d211af05 1056 SAVE_ALL
946c1911 1057 ENCODE_FRAME_POINTER
d211af05 1058 TRACE_IRQS_OFF
a49976d1
IM
1059 xorl %edx, %edx # zero error code
1060 movl %esp, %eax # pt_regs pointer
1061 call do_int3
1062 jmp ret_from_exception
d211af05
AH
1063END(int3)
1064
1065ENTRY(general_protection)
a49976d1 1066 pushl $do_general_protection
7252c4c3 1067 jmp common_exception
d211af05
AH
1068END(general_protection)
1069
631bc487
GN
1070#ifdef CONFIG_KVM_GUEST
1071ENTRY(async_page_fault)
e59d1b0a 1072 ASM_CLAC
a49976d1 1073 pushl $do_async_page_fault
7252c4c3 1074 jmp common_exception
2ae9d293 1075END(async_page_fault)
631bc487 1076#endif
2deb4be2
AL
1077
1078ENTRY(rewind_stack_do_exit)
1079 /* Prevent any naive code from trying to unwind to our caller. */
1080 xorl %ebp, %ebp
1081
1082 movl PER_CPU_VAR(cpu_current_top_of_stack), %esi
1083 leal -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%esi), %esp
1084
1085 call do_exit
10861: jmp 1b
1087END(rewind_stack_do_exit)