]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/x86/entry/entry_32.S
x86/entry/64: Make cpu_entry_area.tss read-only
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / entry / entry_32.S
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4 2/*
a49976d1 3 * Copyright (C) 1991,1992 Linus Torvalds
1da177e4 4 *
a49976d1 5 * entry_32.S contains the system-call and low-level fault and trap handling routines.
1da177e4 6 *
39e8701f 7 * Stack layout while running C code:
a49976d1
IM
8 * 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(),
1da177e4
LT
11 * ptrace.c and ptrace.h
12 *
13 * 0(%esp) - %ebx
14 * 4(%esp) - %ecx
15 * 8(%esp) - %edx
9b47feb7 16 * C(%esp) - %esi
1da177e4
LT
17 * 10(%esp) - %edi
18 * 14(%esp) - %ebp
19 * 18(%esp) - %eax
20 * 1C(%esp) - %ds
21 * 20(%esp) - %es
464d1a78 22 * 24(%esp) - %fs
ccbeed3a
TH
23 * 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
1da177e4
LT
30 */
31
1da177e4 32#include <linux/linkage.h>
d7e7528b 33#include <linux/err.h>
1da177e4 34#include <asm/thread_info.h>
55f327fa 35#include <asm/irqflags.h>
1da177e4
LT
36#include <asm/errno.h>
37#include <asm/segment.h>
38#include <asm/smp.h>
be44d2aa 39#include <asm/percpu.h>
ab68ed98 40#include <asm/processor-flags.h>
9b7dc567 41#include <asm/irq_vectors.h>
cd4d09ec 42#include <asm/cpufeatures.h>
b4ca46e4 43#include <asm/alternative-asm.h>
6837a54d 44#include <asm/asm.h>
e59d1b0a 45#include <asm/smap.h>
4d516f41 46#include <asm/frame.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
5c99b692 180 * is just clearing the MSB, which makes it an invalid stack address and is also
946c1911
JP
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
5c99b692 189 andl $0x7fffffff, %ebp
946c1911
JP
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
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
ebd57499
JP
255/*
256 * 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)
0100301b
BG
272/*
273 * A newly forked process directly context switches into this address.
274 *
275 * eax: prev task we switched from
616d2483
BG
276 * ebx: kernel thread func (NULL for user thread)
277 * edi: kernel thread arg
0100301b 278 */
1da177e4 279ENTRY(ret_from_fork)
ebd57499 280 call schedule_tail_wrapper
39e8701f 281
616d2483
BG
282 testl %ebx, %ebx
283 jnz 1f /* kernel threads are uncommon */
284
2852:
39e8701f 286 /* When we fork, we trace the syscall return in the child, too. */
ebd57499 287 movl %esp, %eax
39e8701f
AL
288 call syscall_return_slowpath
289 jmp restore_all
39e8701f 290
616d2483
BG
291 /* kernel thread */
2921: movl %edi, %eax
293 call *%ebx
39e8701f 294 /*
616d2483
BG
295 * A kernel thread is allowed to return here after successfully
296 * calling do_execve(). Exit to userspace to complete the execve()
297 * syscall.
39e8701f 298 */
616d2483
BG
299 movl $0, PT_EAX(%esp)
300 jmp 2b
301END(ret_from_fork)
6783eaa2 302
1da177e4
LT
303/*
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:
139ec7c4 313 preempt_stop(CLBR_ANY)
1da177e4 314ret_from_intr:
29a2e283 315#ifdef CONFIG_VM86
a49976d1
IM
316 movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
317 movb PT_CS(%esp), %al
318 andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
29a2e283
DA
319#else
320 /*
6783eaa2 321 * We can be coming here from child spawned by kernel_thread().
29a2e283 322 */
a49976d1
IM
323 movl PT_CS(%esp), %eax
324 andl $SEGMENT_RPL_MASK, %eax
29a2e283 325#endif
a49976d1
IM
326 cmpl $USER_RPL, %eax
327 jb resume_kernel # not returning to v8086 or userspace
f95d47ca 328
1da177e4 329ENTRY(resume_userspace)
5d73fc70 330 DISABLE_INTERRUPTS(CLBR_ANY)
e32e58a9 331 TRACE_IRQS_OFF
5d73fc70
AL
332 movl %esp, %eax
333 call prepare_exit_to_usermode
a49976d1 334 jmp restore_all
47a55cd7 335END(ret_from_exception)
1da177e4
LT
336
337#ifdef CONFIG_PREEMPT
338ENTRY(resume_kernel)
139ec7c4 339 DISABLE_INTERRUPTS(CLBR_ANY)
1b00255f 340.Lneed_resched:
a49976d1
IM
341 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
1b00255f 346 jmp .Lneed_resched
47a55cd7 347END(resume_kernel)
1da177e4
LT
348#endif
349
f2b37575
AL
350GLOBAL(__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 */
1b00255f 367 jmp .Lsysenter_past_esp
f2b37575
AL
368#endif
369
fda57b22
AL
370/*
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 */
4c8cd0c5 402ENTRY(entry_SYSENTER_32)
a49976d1 403 movl TSS_sysenter_sp0(%esp), %esp
1b00255f 404.Lsysenter_past_esp:
5f310f73 405 pushl $__USER_DS /* pt_regs->ss */
30bfa7b3 406 pushl %ebp /* pt_regs->sp (stashed in bp) */
5f310f73
AL
407 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
67f590e8 414 /*
f2b37575
AL
415 * 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
67f590e8
AL
417 * 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 *
f2b37575
AL
421 * 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 *
67f590e8
AL
427 * 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 */
f2b37575 433 testl $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, PT_EFLAGS(%esp)
67f590e8
AL
434 jnz .Lsysenter_fix_flags
435.Lsysenter_flags_fixed:
436
55f327fa 437 /*
5f310f73
AL
438 * User mode is traced as though IRQs are on, and SYSENTER
439 * turned them off.
e6e5494c 440 */
55f327fa 441 TRACE_IRQS_OFF
5f310f73
AL
442
443 movl %esp, %eax
444 call do_fast_syscall_32
91e2eea9
BO
445 /* XEN PV guests always use IRET path */
446 ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
447 "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
5f310f73
AL
448
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 */
3bd29515
AL
4531: mov PT_FS(%esp), %fs
454 PTGS_TO_GS
5f310f73
AL
455 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 */
5f310f73 461
c2c9b52f
AL
462 /*
463 * 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
5f310f73
AL
471 /*
472 * 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 */
88c15ec9
BO
475 sti
476 sysexit
af0575bb 477
a49976d1
IM
478.pushsection .fixup, "ax"
4792: movl $0, PT_FS(%esp)
480 jmp 1b
f95d47ca 481.popsection
a49976d1 482 _ASM_EXTABLE(1b, 2b)
ccbeed3a 483 PTGS_TO_GS_EX
67f590e8
AL
484
485.Lsysenter_fix_flags:
486 pushl $X86_EFLAGS_FIXED
487 popfl
488 jmp .Lsysenter_flags_fixed
f2b37575 489GLOBAL(__end_SYSENTER_singlestep_region)
4c8cd0c5 490ENDPROC(entry_SYSENTER_32)
1da177e4 491
fda57b22
AL
492/*
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 */
b2502b41 520ENTRY(entry_INT80_32)
e59d1b0a 521 ASM_CLAC
150ac78d 522 pushl %eax /* pt_regs->orig_ax */
5f310f73 523 SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
150ac78d
AL
524
525 /*
a798f091
AL
526 * User mode is traced as though IRQs are on, and the interrupt gate
527 * turned them off.
150ac78d 528 */
a798f091 529 TRACE_IRQS_OFF
150ac78d
AL
530
531 movl %esp, %eax
a798f091 532 call do_int80_syscall_32
5f310f73 533.Lsyscall_32_done:
1da177e4
LT
534
535restore_all:
2e04bc76 536 TRACE_IRQS_IRET
1b00255f 537.Lrestore_all_notrace:
34273f41 538#ifdef CONFIG_X86_ESPFIX32
1b00255f 539 ALTERNATIVE "jmp .Lrestore_nocheck", "", X86_BUG_ESPFIX
58a5aac5 540
a49976d1
IM
541 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
1b00255f 551 je .Lldt_ss # returning to user-space with LDT SS
34273f41 552#endif
1b00255f 553.Lrestore_nocheck:
a49976d1 554 RESTORE_REGS 4 # skip orig_eax/error_code
1b00255f 555.Lirq_return:
3701d863 556 INTERRUPT_RETURN
1b00255f 557
a49976d1
IM
558.section .fixup, "ax"
559ENTRY(iret_exc )
560 pushl $0 # no error code
561 pushl $do_iret_error
7252c4c3 562 jmp common_exception
1da177e4 563.previous
1b00255f 564 _ASM_EXTABLE(.Lirq_return, iret_exc)
1da177e4 565
34273f41 566#ifdef CONFIG_X86_ESPFIX32
1b00255f 567.Lldt_ss:
dc4c2a0a
AH
568/*
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 */
72c511dd 579#define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
a49976d1
IM
580 mov %esp, %edx /* load kernel esp */
581 mov PT_OLDESP(%esp), %eax /* load userspace esp */
582 mov %dx, %ax /* eax: new kernel esp */
9b47feb7
DV
583 sub %eax, %edx /* offset (low word is 0) */
584 shr $16, %edx
a49976d1
IM
585 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
2e04bc76 591 * will soon execute iret and the tracer was already set to
a49976d1
IM
592 * the irqstate after the IRET:
593 */
fdbd518a 594 DISABLE_INTERRUPTS(CLBR_ANY)
a49976d1 595 lss (%esp), %esp /* switch to espfix segment */
1b00255f 596 jmp .Lrestore_nocheck
34273f41 597#endif
b2502b41 598ENDPROC(entry_INT80_32)
1da177e4 599
f0d96110 600.macro FIXUP_ESPFIX_STACK
dc4c2a0a
AH
601/*
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 */
34273f41 608#ifdef CONFIG_X86_ESPFIX32
dc4c2a0a 609 /* fixup the stack */
a49976d1
IM
610 mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
611 mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
9b47feb7 612 shl $16, %eax
a49976d1
IM
613 addl %esp, %eax /* the adjusted stack pointer */
614 pushl $__KERNEL_DS
615 pushl %eax
616 lss (%esp), %esp /* switch to the normal stack segment */
34273f41 617#endif
f0d96110
TH
618.endm
619.macro UNWIND_ESPFIX_STACK
34273f41 620#ifdef CONFIG_X86_ESPFIX32
a49976d1 621 movl %ss, %eax
f0d96110 622 /* see if on espfix stack */
a49976d1
IM
623 cmpw $__ESPFIX_SS, %ax
624 jne 27f
625 movl $__KERNEL_DS, %eax
626 movl %eax, %ds
627 movl %eax, %es
f0d96110
TH
628 /* switch to normal stack */
629 FIXUP_ESPFIX_STACK
63027:
34273f41 631#endif
f0d96110 632.endm
1da177e4
LT
633
634/*
3304c9c3
DV
635 * Build the entry stubs with some assembler magic.
636 * We pack 1 stub into every 8-byte block.
1da177e4 637 */
3304c9c3 638 .align 8
1da177e4 639ENTRY(irq_entries_start)
3304c9c3
DV
640 vector=FIRST_EXTERNAL_VECTOR
641 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
a49976d1 642 pushl $(~vector+0x80) /* Note: always in signed byte range */
3304c9c3
DV
643 vector=vector+1
644 jmp common_interrupt
3304c9c3
DV
645 .align 8
646 .endr
47a55cd7
JB
647END(irq_entries_start)
648
55f327fa
IM
649/*
650 * the CPU automatically disables interrupts when executing an IRQ vector,
651 * so IRQ-flags tracing has to follow that:
652 */
b7c6244f 653 .p2align CONFIG_X86_L1_CACHE_SHIFT
1da177e4 654common_interrupt:
e59d1b0a 655 ASM_CLAC
a49976d1 656 addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
1da177e4 657 SAVE_ALL
946c1911 658 ENCODE_FRAME_POINTER
55f327fa 659 TRACE_IRQS_OFF
a49976d1
IM
660 movl %esp, %eax
661 call do_IRQ
662 jmp ret_from_intr
47a55cd7 663ENDPROC(common_interrupt)
1da177e4 664
02cf94c3 665#define BUILD_INTERRUPT3(name, nr, fn) \
1da177e4 666ENTRY(name) \
e59d1b0a 667 ASM_CLAC; \
a49976d1 668 pushl $~(nr); \
fe7cacc1 669 SAVE_ALL; \
946c1911 670 ENCODE_FRAME_POINTER; \
55f327fa 671 TRACE_IRQS_OFF \
a49976d1
IM
672 movl %esp, %eax; \
673 call fn; \
674 jmp ret_from_intr; \
47a55cd7 675ENDPROC(name)
1da177e4 676
a49976d1
IM
677#define BUILD_INTERRUPT(name, nr) \
678 BUILD_INTERRUPT3(name, nr, smp_##name); \
02cf94c3 679
1da177e4 680/* The include is where all of the SMP etc. interrupts come from */
1164dd00 681#include <asm/entry_arch.h>
1da177e4 682
1da177e4 683ENTRY(coprocessor_error)
e59d1b0a 684 ASM_CLAC
a49976d1
IM
685 pushl $0
686 pushl $do_coprocessor_error
7252c4c3 687 jmp common_exception
47a55cd7 688END(coprocessor_error)
1da177e4
LT
689
690ENTRY(simd_coprocessor_error)
e59d1b0a 691 ASM_CLAC
a49976d1 692 pushl $0
40d2e763
BG
693#ifdef CONFIG_X86_INVD_BUG
694 /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
a49976d1
IM
695 ALTERNATIVE "pushl $do_general_protection", \
696 "pushl $do_simd_coprocessor_error", \
8e65f6e0 697 X86_FEATURE_XMM
40d2e763 698#else
a49976d1 699 pushl $do_simd_coprocessor_error
40d2e763 700#endif
7252c4c3 701 jmp common_exception
47a55cd7 702END(simd_coprocessor_error)
1da177e4
LT
703
704ENTRY(device_not_available)
e59d1b0a 705 ASM_CLAC
a49976d1
IM
706 pushl $-1 # mark this as an int
707 pushl $do_device_not_available
7252c4c3 708 jmp common_exception
47a55cd7 709END(device_not_available)
1da177e4 710
d3561b7f
RR
711#ifdef CONFIG_PARAVIRT
712ENTRY(native_iret)
3701d863 713 iret
6837a54d 714 _ASM_EXTABLE(native_iret, iret_exc)
47a55cd7 715END(native_iret)
d3561b7f
RR
716#endif
717
1da177e4 718ENTRY(overflow)
e59d1b0a 719 ASM_CLAC
a49976d1
IM
720 pushl $0
721 pushl $do_overflow
7252c4c3 722 jmp common_exception
47a55cd7 723END(overflow)
1da177e4
LT
724
725ENTRY(bounds)
e59d1b0a 726 ASM_CLAC
a49976d1
IM
727 pushl $0
728 pushl $do_bounds
7252c4c3 729 jmp common_exception
47a55cd7 730END(bounds)
1da177e4
LT
731
732ENTRY(invalid_op)
e59d1b0a 733 ASM_CLAC
a49976d1
IM
734 pushl $0
735 pushl $do_invalid_op
7252c4c3 736 jmp common_exception
47a55cd7 737END(invalid_op)
1da177e4
LT
738
739ENTRY(coprocessor_segment_overrun)
e59d1b0a 740 ASM_CLAC
a49976d1
IM
741 pushl $0
742 pushl $do_coprocessor_segment_overrun
7252c4c3 743 jmp common_exception
47a55cd7 744END(coprocessor_segment_overrun)
1da177e4
LT
745
746ENTRY(invalid_TSS)
e59d1b0a 747 ASM_CLAC
a49976d1 748 pushl $do_invalid_TSS
7252c4c3 749 jmp common_exception
47a55cd7 750END(invalid_TSS)
1da177e4
LT
751
752ENTRY(segment_not_present)
e59d1b0a 753 ASM_CLAC
a49976d1 754 pushl $do_segment_not_present
7252c4c3 755 jmp common_exception
47a55cd7 756END(segment_not_present)
1da177e4
LT
757
758ENTRY(stack_segment)
e59d1b0a 759 ASM_CLAC
a49976d1 760 pushl $do_stack_segment
7252c4c3 761 jmp common_exception
47a55cd7 762END(stack_segment)
1da177e4 763
1da177e4 764ENTRY(alignment_check)
e59d1b0a 765 ASM_CLAC
a49976d1 766 pushl $do_alignment_check
7252c4c3 767 jmp common_exception
47a55cd7 768END(alignment_check)
1da177e4 769
d28c4393 770ENTRY(divide_error)
e59d1b0a 771 ASM_CLAC
a49976d1
IM
772 pushl $0 # no error code
773 pushl $do_divide_error
7252c4c3 774 jmp common_exception
47a55cd7 775END(divide_error)
1da177e4
LT
776
777#ifdef CONFIG_X86_MCE
778ENTRY(machine_check)
e59d1b0a 779 ASM_CLAC
a49976d1
IM
780 pushl $0
781 pushl machine_check_vector
7252c4c3 782 jmp common_exception
47a55cd7 783END(machine_check)
1da177e4
LT
784#endif
785
786ENTRY(spurious_interrupt_bug)
e59d1b0a 787 ASM_CLAC
a49976d1
IM
788 pushl $0
789 pushl $do_spurious_interrupt_bug
7252c4c3 790 jmp common_exception
47a55cd7 791END(spurious_interrupt_bug)
1da177e4 792
5ead97c8
JF
793#ifdef CONFIG_XEN
794ENTRY(xen_hypervisor_callback)
a49976d1 795 pushl $-1 /* orig_ax = -1 => not a system call */
5ead97c8 796 SAVE_ALL
946c1911 797 ENCODE_FRAME_POINTER
5ead97c8 798 TRACE_IRQS_OFF
9ec2b804 799
a49976d1
IM
800 /*
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
9ec2b804 812
a49976d1 813 jmp xen_iret_crit_fixup
e2a81baf 814
e2a81baf 815ENTRY(xen_do_upcall)
a49976d1
IM
8161: mov %esp, %eax
817 call xen_evtchn_do_upcall
fdfd811d 818#ifndef CONFIG_PREEMPT
a49976d1 819 call xen_maybe_preempt_hcall
fdfd811d 820#endif
a49976d1 821 jmp ret_from_intr
5ead97c8
JF
822ENDPROC(xen_hypervisor_callback)
823
a49976d1
IM
824/*
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 */
5ead97c8 836ENTRY(xen_failsafe_callback)
a49976d1
IM
837 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
a349e23d
DV
843 /* EAX == 0 => Category 1 (Bad segment)
844 EAX != 0 => Category 2 (Bad IRET) */
a49976d1
IM
845 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 */
5ead97c8 851 SAVE_ALL
946c1911 852 ENCODE_FRAME_POINTER
a49976d1
IM
853 jmp ret_from_exception
854
855.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
5ead97c8 868.previous
a49976d1
IM
869 _ASM_EXTABLE(1b, 6b)
870 _ASM_EXTABLE(2b, 7b)
871 _ASM_EXTABLE(3b, 8b)
872 _ASM_EXTABLE(4b, 9b)
5ead97c8
JF
873ENDPROC(xen_failsafe_callback)
874
bc2b0331 875BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
4b9a8dca 876 xen_evtchn_do_upcall)
38e20b07 877
a49976d1 878#endif /* CONFIG_XEN */
bc2b0331
S
879
880#if IS_ENABLED(CONFIG_HYPERV)
881
882BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
4b9a8dca 883 hyperv_vector_handler)
bc2b0331
S
884
885#endif /* CONFIG_HYPERV */
5ead97c8 886
d211af05 887ENTRY(page_fault)
e59d1b0a 888 ASM_CLAC
a49976d1 889 pushl $do_page_fault
d211af05 890 ALIGN
7252c4c3
JP
891 jmp common_exception
892END(page_fault)
893
894common_exception:
ccbeed3a 895 /* the function address is in %gs's slot on the stack */
a49976d1
IM
896 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
946c1911 906 ENCODE_FRAME_POINTER
d211af05 907 cld
a49976d1
IM
908 movl $(__KERNEL_PERCPU), %ecx
909 movl %ecx, %fs
d211af05 910 UNWIND_ESPFIX_STACK
ccbeed3a 911 GS_TO_REG %ecx
a49976d1
IM
912 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
ccbeed3a
TH
915 REG_TO_PTGS %ecx
916 SET_KERNEL_GS %ecx
a49976d1
IM
917 movl $(__USER_DS), %ecx
918 movl %ecx, %ds
919 movl %ecx, %es
d211af05 920 TRACE_IRQS_OFF
a49976d1
IM
921 movl %esp, %eax # pt_regs pointer
922 call *%edi
923 jmp ret_from_exception
7252c4c3 924END(common_exception)
d211af05 925
d211af05 926ENTRY(debug)
7536656f
AL
927 /*
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 */
e59d1b0a 936 ASM_CLAC
a49976d1 937 pushl $-1 # mark this as an int
d211af05 938 SAVE_ALL
946c1911 939 ENCODE_FRAME_POINTER
a49976d1
IM
940 xorl %edx, %edx # error code 0
941 movl %esp, %eax # pt_regs pointer
7536656f
AL
942
943 /* Are we currently on the SYSENTER stack? */
72f5e08d 944 movl PER_CPU_VAR(cpu_entry_area), %ecx
c482feef 945 addl $CPU_ENTRY_AREA_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx
7536656f
AL
946 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
951 call do_debug
952 jmp ret_from_exception
953
954.Ldebug_from_sysenter_stack:
955 /* We're on the SYSENTER stack. Switch off. */
946c1911 956 movl %esp, %ebx
7536656f
AL
957 movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
958 TRACE_IRQS_OFF
a49976d1 959 call do_debug
946c1911 960 movl %ebx, %esp
a49976d1 961 jmp ret_from_exception
d211af05
AH
962END(debug)
963
964/*
7536656f
AL
965 * 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.
d211af05
AH
970 */
971ENTRY(nmi)
e59d1b0a 972 ASM_CLAC
34273f41 973#ifdef CONFIG_X86_ESPFIX32
a49976d1
IM
974 pushl %eax
975 movl %ss, %eax
976 cmpw $__ESPFIX_SS, %ax
977 popl %eax
1b00255f 978 je .Lnmi_espfix_stack
34273f41 979#endif
7536656f
AL
980
981 pushl %eax # pt_regs->orig_ax
d211af05 982 SAVE_ALL
946c1911 983 ENCODE_FRAME_POINTER
a49976d1
IM
984 xorl %edx, %edx # zero error code
985 movl %esp, %eax # pt_regs pointer
7536656f
AL
986
987 /* Are we currently on the SYSENTER stack? */
72f5e08d 988 movl PER_CPU_VAR(cpu_entry_area), %ecx
c482feef 989 addl $CPU_ENTRY_AREA_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx
7536656f
AL
990 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. */
a49976d1 995 call do_nmi
1b00255f 996 jmp .Lrestore_all_notrace
d211af05 997
7536656f
AL
998.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 */
946c1911 1003 movl %esp, %ebx
7536656f
AL
1004 movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
1005 call do_nmi
946c1911 1006 movl %ebx, %esp
1b00255f 1007 jmp .Lrestore_all_notrace
d211af05 1008
34273f41 1009#ifdef CONFIG_X86_ESPFIX32
1b00255f 1010.Lnmi_espfix_stack:
131484c8 1011 /*
d211af05
AH
1012 * create the pointer to lss back
1013 */
a49976d1
IM
1014 pushl %ss
1015 pushl %esp
1016 addl $4, (%esp)
d211af05
AH
1017 /* copy the iret frame of 12 bytes */
1018 .rept 3
a49976d1 1019 pushl 16(%esp)
d211af05 1020 .endr
a49976d1 1021 pushl %eax
d211af05 1022 SAVE_ALL
946c1911 1023 ENCODE_FRAME_POINTER
a49976d1
IM
1024 FIXUP_ESPFIX_STACK # %eax == %esp
1025 xorl %edx, %edx # zero error code
1026 call do_nmi
d211af05 1027 RESTORE_REGS
a49976d1 1028 lss 12+4(%esp), %esp # back to espfix stack
1b00255f 1029 jmp .Lirq_return
34273f41 1030#endif
d211af05
AH
1031END(nmi)
1032
1033ENTRY(int3)
e59d1b0a 1034 ASM_CLAC
a49976d1 1035 pushl $-1 # mark this as an int
d211af05 1036 SAVE_ALL
946c1911 1037 ENCODE_FRAME_POINTER
d211af05 1038 TRACE_IRQS_OFF
a49976d1
IM
1039 xorl %edx, %edx # zero error code
1040 movl %esp, %eax # pt_regs pointer
1041 call do_int3
1042 jmp ret_from_exception
d211af05
AH
1043END(int3)
1044
1045ENTRY(general_protection)
a49976d1 1046 pushl $do_general_protection
7252c4c3 1047 jmp common_exception
d211af05
AH
1048END(general_protection)
1049
631bc487
GN
1050#ifdef CONFIG_KVM_GUEST
1051ENTRY(async_page_fault)
e59d1b0a 1052 ASM_CLAC
a49976d1 1053 pushl $do_async_page_fault
7252c4c3 1054 jmp common_exception
2ae9d293 1055END(async_page_fault)
631bc487 1056#endif
2deb4be2
AL
1057
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)