x86/entry: Switch page fault exception to IDTENTRY_RAW
[linux-block.git] / arch / x86 / entry / entry_64.S
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2/*
3 * linux/arch/x86_64/entry.S
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
7 * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
4d732138 8 *
1da177e4
LT
9 * entry.S contains the system-call and fault low-level handling routines.
10 *
cb1aaebe 11 * Some of this is documented in Documentation/x86/entry_64.rst
8b4777a4 12 *
0bd7b798 13 * A note on terminology:
4d732138
IM
14 * - iret frame: Architecture defined interrupt frame from SS to RIP
15 * at the top of the kernel process stack.
2e91a17b
AK
16 *
17 * Some macro usage:
6dcc5627 18 * - SYM_FUNC_START/END:Define functions in the symbol table.
4d732138
IM
19 * - TRACE_IRQ_*: Trace hardirq state for lock debugging.
20 * - idtentry: Define exception entry points.
1da177e4 21 */
1da177e4
LT
22#include <linux/linkage.h>
23#include <asm/segment.h>
1da177e4
LT
24#include <asm/cache.h>
25#include <asm/errno.h>
e2d5df93 26#include <asm/asm-offsets.h>
1da177e4
LT
27#include <asm/msr.h>
28#include <asm/unistd.h>
29#include <asm/thread_info.h>
30#include <asm/hw_irq.h>
0341c14d 31#include <asm/page_types.h>
2601e64d 32#include <asm/irqflags.h>
72fe4858 33#include <asm/paravirt.h>
9939ddaf 34#include <asm/percpu.h>
d7abc0fa 35#include <asm/asm.h>
63bcff2a 36#include <asm/smap.h>
3891a04a 37#include <asm/pgtable_types.h>
784d5699 38#include <asm/export.h>
8c1f7558 39#include <asm/frame.h>
cfa82a00 40#include <asm/trapnr.h>
2641f08b 41#include <asm/nospec-branch.h>
d7e7528b 42#include <linux/err.h>
1da177e4 43
6fd166aa
PZ
44#include "calling.h"
45
4d732138
IM
46.code64
47.section .entry.text, "ax"
16444a8a 48
72fe4858 49#ifdef CONFIG_PARAVIRT
bc7b11c0 50SYM_CODE_START(native_usergs_sysret64)
8c1f7558 51 UNWIND_HINT_EMPTY
72fe4858
GOC
52 swapgs
53 sysretq
bc7b11c0 54SYM_CODE_END(native_usergs_sysret64)
72fe4858
GOC
55#endif /* CONFIG_PARAVIRT */
56
ca37e57b 57.macro TRACE_IRQS_FLAGS flags:req
2601e64d 58#ifdef CONFIG_TRACE_IRQFLAGS
a368d7fd 59 btl $9, \flags /* interrupts off? */
4d732138 60 jnc 1f
2601e64d
IM
61 TRACE_IRQS_ON
621:
63#endif
64.endm
65
ca37e57b
AL
66.macro TRACE_IRQS_IRETQ
67 TRACE_IRQS_FLAGS EFLAGS(%rsp)
68.endm
69
5963e317
SR
70/*
71 * When dynamic function tracer is enabled it will add a breakpoint
72 * to all locations that it is about to modify, sync CPUs, update
73 * all the code, sync CPUs, then remove the breakpoints. In this time
74 * if lockdep is enabled, it might jump back into the debug handler
75 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
76 *
77 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
78 * make sure the stack pointer does not get reset back to the top
79 * of the debug stack, and instead just reuses the current stack.
80 */
81#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
82
83.macro TRACE_IRQS_OFF_DEBUG
4d732138 84 call debug_stack_set_zero
5963e317 85 TRACE_IRQS_OFF
4d732138 86 call debug_stack_reset
5963e317
SR
87.endm
88
89.macro TRACE_IRQS_ON_DEBUG
4d732138 90 call debug_stack_set_zero
5963e317 91 TRACE_IRQS_ON
4d732138 92 call debug_stack_reset
5963e317
SR
93.endm
94
f2db9382 95.macro TRACE_IRQS_IRETQ_DEBUG
6709812f 96 btl $9, EFLAGS(%rsp) /* interrupts off? */
4d732138 97 jnc 1f
5963e317
SR
98 TRACE_IRQS_ON_DEBUG
991:
100.endm
101
102#else
4d732138
IM
103# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
104# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
105# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
5963e317
SR
106#endif
107
1da177e4 108/*
4d732138 109 * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
1da177e4 110 *
fda57b22
AL
111 * This is the only entry point used for 64-bit system calls. The
112 * hardware interface is reasonably well designed and the register to
113 * argument mapping Linux uses fits well with the registers that are
114 * available when SYSCALL is used.
115 *
116 * SYSCALL instructions can be found inlined in libc implementations as
117 * well as some other programs and libraries. There are also a handful
118 * of SYSCALL instructions in the vDSO used, for example, as a
119 * clock_gettimeofday fallback.
120 *
4d732138 121 * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
b87cf63e
DV
122 * then loads new ss, cs, and rip from previously programmed MSRs.
123 * rflags gets masked by a value from another MSR (so CLD and CLAC
124 * are not needed). SYSCALL does not save anything on the stack
125 * and does not change rsp.
126 *
127 * Registers on entry:
1da177e4 128 * rax system call number
b87cf63e
DV
129 * rcx return address
130 * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
1da177e4 131 * rdi arg0
1da177e4 132 * rsi arg1
0bd7b798 133 * rdx arg2
b87cf63e 134 * r10 arg3 (needs to be moved to rcx to conform to C ABI)
1da177e4
LT
135 * r8 arg4
136 * r9 arg5
4d732138 137 * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
0bd7b798 138 *
1da177e4
LT
139 * Only called from user space.
140 *
7fcb3bc3 141 * When user can change pt_regs->foo always force IRET. That is because
7bf36bbc
AK
142 * it deals with uncanonical addresses better. SYSRET has trouble
143 * with them due to bugs in both AMD and Intel CPUs.
0bd7b798 144 */
1da177e4 145
bc7b11c0 146SYM_CODE_START(entry_SYSCALL_64)
8c1f7558 147 UNWIND_HINT_EMPTY
9ed8e7d8
DV
148 /*
149 * Interrupts are off on entry.
150 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
151 * it is too small to ever cause noticeable irq latency.
152 */
72fe4858 153
8a9949bc 154 swapgs
bf904d27 155 /* tss.sp2 is scratch space. */
98f05b51 156 movq %rsp, PER_CPU_VAR(cpu_tss_rw + TSS_sp2)
bf904d27 157 SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
4d732138 158 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
9ed8e7d8
DV
159
160 /* Construct struct pt_regs on stack */
98f05b51
AL
161 pushq $__USER_DS /* pt_regs->ss */
162 pushq PER_CPU_VAR(cpu_tss_rw + TSS_sp2) /* pt_regs->sp */
163 pushq %r11 /* pt_regs->flags */
164 pushq $__USER_CS /* pt_regs->cs */
165 pushq %rcx /* pt_regs->ip */
26ba4e57 166SYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL)
98f05b51 167 pushq %rax /* pt_regs->orig_ax */
30907fd1
DB
168
169 PUSH_AND_CLEAR_REGS rax=$-ENOSYS
4d732138 170
1e423bff 171 /* IRQs are off. */
dfe64506
LT
172 movq %rax, %rdi
173 movq %rsp, %rsi
1e423bff
AL
174 call do_syscall_64 /* returns with IRQs disabled */
175
fffbb5dc
DV
176 /*
177 * Try to use SYSRET instead of IRET if we're returning to
8a055d7f
AL
178 * a completely clean 64-bit userspace context. If we're not,
179 * go to the slow exit path.
fffbb5dc 180 */
4d732138
IM
181 movq RCX(%rsp), %rcx
182 movq RIP(%rsp), %r11
8a055d7f
AL
183
184 cmpq %rcx, %r11 /* SYSRET requires RCX == RIP */
185 jne swapgs_restore_regs_and_return_to_usermode
fffbb5dc
DV
186
187 /*
188 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
189 * in kernel space. This essentially lets the user take over
17be0aec 190 * the kernel, since userspace controls RSP.
fffbb5dc 191 *
17be0aec 192 * If width of "canonical tail" ever becomes variable, this will need
fffbb5dc 193 * to be updated to remain correct on both old and new CPUs.
361b4b58 194 *
cbe0317b
KS
195 * Change top bits to match most significant bit (47th or 56th bit
196 * depending on paging mode) in the address.
fffbb5dc 197 */
09e61a77 198#ifdef CONFIG_X86_5LEVEL
39b95522
KS
199 ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \
200 "shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57
09e61a77 201#else
17be0aec
DV
202 shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
203 sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
09e61a77 204#endif
4d732138 205
17be0aec
DV
206 /* If this changed %rcx, it was not canonical */
207 cmpq %rcx, %r11
8a055d7f 208 jne swapgs_restore_regs_and_return_to_usermode
fffbb5dc 209
4d732138 210 cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
8a055d7f 211 jne swapgs_restore_regs_and_return_to_usermode
fffbb5dc 212
4d732138
IM
213 movq R11(%rsp), %r11
214 cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
8a055d7f 215 jne swapgs_restore_regs_and_return_to_usermode
fffbb5dc
DV
216
217 /*
3e035305
BP
218 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
219 * restore RF properly. If the slowpath sets it for whatever reason, we
220 * need to restore it correctly.
221 *
222 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
223 * trap from userspace immediately after SYSRET. This would cause an
224 * infinite loop whenever #DB happens with register state that satisfies
225 * the opportunistic SYSRET conditions. For example, single-stepping
226 * this user code:
fffbb5dc 227 *
4d732138 228 * movq $stuck_here, %rcx
fffbb5dc
DV
229 * pushfq
230 * popq %r11
231 * stuck_here:
232 *
233 * would never get past 'stuck_here'.
234 */
4d732138 235 testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
8a055d7f 236 jnz swapgs_restore_regs_and_return_to_usermode
fffbb5dc
DV
237
238 /* nothing to check for RSP */
239
4d732138 240 cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
8a055d7f 241 jne swapgs_restore_regs_and_return_to_usermode
fffbb5dc
DV
242
243 /*
4d732138
IM
244 * We win! This label is here just for ease of understanding
245 * perf profiles. Nothing jumps here.
fffbb5dc
DV
246 */
247syscall_return_via_sysret:
17be0aec 248 /* rcx and r11 are already restored (see code above) */
502af0d7 249 POP_REGS pop_rdi=0 skip_r11rcx=1
3e3b9293
AL
250
251 /*
252 * Now all regs are restored except RSP and RDI.
253 * Save old stack pointer and switch to trampoline stack.
254 */
255 movq %rsp, %rdi
c482feef 256 movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
1fb14363 257 UNWIND_HINT_EMPTY
3e3b9293
AL
258
259 pushq RSP-RDI(%rdi) /* RSP */
260 pushq (%rdi) /* RDI */
261
262 /*
263 * We are on the trampoline stack. All regs except RDI are live.
264 * We can do future final exit work right here.
265 */
afaef01c
AP
266 STACKLEAK_ERASE_NOCLOBBER
267
6fd166aa 268 SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
3e3b9293 269
4fbb3910 270 popq %rdi
3e3b9293 271 popq %rsp
fffbb5dc 272 USERGS_SYSRET64
bc7b11c0 273SYM_CODE_END(entry_SYSCALL_64)
0bd7b798 274
0100301b
BG
275/*
276 * %rdi: prev task
277 * %rsi: next task
278 */
b9f6976b 279.pushsection .text, "ax"
96c64806 280SYM_FUNC_START(__switch_to_asm)
0100301b
BG
281 /*
282 * Save callee-saved registers
283 * This must match the order in inactive_task_frame
284 */
285 pushq %rbp
286 pushq %rbx
287 pushq %r12
288 pushq %r13
289 pushq %r14
290 pushq %r15
291
292 /* switch stack */
293 movq %rsp, TASK_threadsp(%rdi)
294 movq TASK_threadsp(%rsi), %rsp
295
050e9baa 296#ifdef CONFIG_STACKPROTECTOR
0100301b 297 movq TASK_stack_canary(%rsi), %rbx
e6401c13 298 movq %rbx, PER_CPU_VAR(fixed_percpu_data) + stack_canary_offset
0100301b
BG
299#endif
300
c995efd5
DW
301#ifdef CONFIG_RETPOLINE
302 /*
303 * When switching from a shallower to a deeper call stack
304 * the RSB may either underflow or use entries populated
305 * with userspace addresses. On CPUs where those concerns
306 * exist, overwrite the RSB with entries which capture
307 * speculative execution to prevent attack.
308 */
d1c99108 309 FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
c995efd5
DW
310#endif
311
0100301b
BG
312 /* restore callee-saved registers */
313 popq %r15
314 popq %r14
315 popq %r13
316 popq %r12
317 popq %rbx
318 popq %rbp
319
320 jmp __switch_to
96c64806 321SYM_FUNC_END(__switch_to_asm)
b9f6976b 322.popsection
0100301b 323
1eeb207f
DV
324/*
325 * A newly forked process directly context switches into this address.
326 *
0100301b 327 * rax: prev task we switched from
616d2483
BG
328 * rbx: kernel thread func (NULL for user thread)
329 * r12: kernel thread arg
1eeb207f 330 */
b9f6976b 331.pushsection .text, "ax"
bc7b11c0 332SYM_CODE_START(ret_from_fork)
8c1f7558 333 UNWIND_HINT_EMPTY
0100301b 334 movq %rax, %rdi
ebd57499 335 call schedule_tail /* rdi: 'prev' task parameter */
1eeb207f 336
ebd57499
JP
337 testq %rbx, %rbx /* from kernel_thread? */
338 jnz 1f /* kernel threads are uncommon */
24d978b7 339
616d2483 3402:
8c1f7558 341 UNWIND_HINT_REGS
ebd57499 342 movq %rsp, %rdi
24d978b7 343 call syscall_return_slowpath /* returns with IRQs disabled */
8a055d7f 344 jmp swapgs_restore_regs_and_return_to_usermode
616d2483
BG
345
3461:
347 /* kernel thread */
d31a5802 348 UNWIND_HINT_EMPTY
616d2483 349 movq %r12, %rdi
34fdce69 350 CALL_NOSPEC rbx
616d2483
BG
351 /*
352 * A kernel thread is allowed to return here after successfully
353 * calling do_execve(). Exit to userspace to complete the execve()
354 * syscall.
355 */
356 movq $0, RAX(%rsp)
357 jmp 2b
bc7b11c0 358SYM_CODE_END(ret_from_fork)
b9f6976b 359.popsection
1eeb207f 360
939b7871 361/*
3304c9c3
DV
362 * Build the entry stubs with some assembler magic.
363 * We pack 1 stub into every 8-byte block.
939b7871 364 */
3304c9c3 365 .align 8
bc7b11c0 366SYM_CODE_START(irq_entries_start)
3304c9c3
DV
367 vector=FIRST_EXTERNAL_VECTOR
368 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
8c1f7558 369 UNWIND_HINT_IRET_REGS
4d732138 370 pushq $(~vector+0x80) /* Note: always in signed byte range */
3304c9c3 371 jmp common_interrupt
3304c9c3 372 .align 8
8c1f7558 373 vector=vector+1
3304c9c3 374 .endr
bc7b11c0 375SYM_CODE_END(irq_entries_start)
939b7871 376
f8a8fe61 377 .align 8
bc7b11c0 378SYM_CODE_START(spurious_entries_start)
f8a8fe61
TG
379 vector=FIRST_SYSTEM_VECTOR
380 .rept (NR_VECTORS - FIRST_SYSTEM_VECTOR)
381 UNWIND_HINT_IRET_REGS
382 pushq $(~vector+0x80) /* Note: always in signed byte range */
383 jmp common_spurious
384 .align 8
385 vector=vector+1
386 .endr
bc7b11c0 387SYM_CODE_END(spurious_entries_start)
f8a8fe61 388
1d3e53e8
AL
389.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
390#ifdef CONFIG_DEBUG_ENTRY
e17f8234
BO
391 pushq %rax
392 SAVE_FLAGS(CLBR_RAX)
393 testl $X86_EFLAGS_IF, %eax
1d3e53e8
AL
394 jz .Lokay_\@
395 ud2
396.Lokay_\@:
e17f8234 397 popq %rax
1d3e53e8
AL
398#endif
399.endm
400
401/*
402 * Enters the IRQ stack if we're not already using it. NMI-safe. Clobbers
403 * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
404 * Requires kernel GSBASE.
405 *
406 * The invariant is that, if irq_count != -1, then the IRQ stack is in use.
407 */
2ba64741 408.macro ENTER_IRQ_STACK regs=1 old_rsp save_ret=0
1d3e53e8 409 DEBUG_ENTRY_ASSERT_IRQS_OFF
2ba64741
DB
410
411 .if \save_ret
412 /*
413 * If save_ret is set, the original stack contains one additional
414 * entry -- the return address. Therefore, move the address one
415 * entry below %rsp to \old_rsp.
416 */
417 leaq 8(%rsp), \old_rsp
418 .else
1d3e53e8 419 movq %rsp, \old_rsp
2ba64741 420 .endif
8c1f7558
JP
421
422 .if \regs
423 UNWIND_HINT_REGS base=\old_rsp
424 .endif
425
1d3e53e8 426 incl PER_CPU_VAR(irq_count)
29955909 427 jnz .Lirq_stack_push_old_rsp_\@
1d3e53e8
AL
428
429 /*
430 * Right now, if we just incremented irq_count to zero, we've
431 * claimed the IRQ stack but we haven't switched to it yet.
432 *
433 * If anything is added that can interrupt us here without using IST,
434 * it must be *extremely* careful to limit its stack usage. This
435 * could include kprobes and a hypothetical future IST-less #DB
436 * handler.
29955909
AL
437 *
438 * The OOPS unwinder relies on the word at the top of the IRQ
439 * stack linking back to the previous RSP for the entire time we're
440 * on the IRQ stack. For this to work reliably, we need to write
441 * it before we actually move ourselves to the IRQ stack.
442 */
443
e6401c13 444 movq \old_rsp, PER_CPU_VAR(irq_stack_backing_store + IRQ_STACK_SIZE - 8)
758a2e31 445 movq PER_CPU_VAR(hardirq_stack_ptr), %rsp
29955909
AL
446
447#ifdef CONFIG_DEBUG_ENTRY
448 /*
449 * If the first movq above becomes wrong due to IRQ stack layout
450 * changes, the only way we'll notice is if we try to unwind right
451 * here. Assert that we set up the stack right to catch this type
452 * of bug quickly.
1d3e53e8 453 */
29955909
AL
454 cmpq -8(%rsp), \old_rsp
455 je .Lirq_stack_okay\@
456 ud2
457 .Lirq_stack_okay\@:
458#endif
1d3e53e8 459
29955909 460.Lirq_stack_push_old_rsp_\@:
1d3e53e8 461 pushq \old_rsp
8c1f7558
JP
462
463 .if \regs
464 UNWIND_HINT_REGS indirect=1
465 .endif
2ba64741
DB
466
467 .if \save_ret
468 /*
469 * Push the return address to the stack. This return address can
470 * be found at the "real" original RSP, which was offset by 8 at
471 * the beginning of this macro.
472 */
473 pushq -8(\old_rsp)
474 .endif
1d3e53e8
AL
475.endm
476
477/*
478 * Undoes ENTER_IRQ_STACK.
479 */
8c1f7558 480.macro LEAVE_IRQ_STACK regs=1
1d3e53e8
AL
481 DEBUG_ENTRY_ASSERT_IRQS_OFF
482 /* We need to be off the IRQ stack before decrementing irq_count. */
483 popq %rsp
484
8c1f7558
JP
485 .if \regs
486 UNWIND_HINT_REGS
487 .endif
488
1d3e53e8
AL
489 /*
490 * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming
491 * the irq stack but we're not on it.
492 */
493
494 decl PER_CPU_VAR(irq_count)
495.endm
496
cfa82a00
TG
497/**
498 * idtentry_body - Macro to emit code calling the C function
499 * @vector: Vector number
500 * @cfunc: C function to be called
501 * @has_error_code: Hardware pushed error code on stack
424c7d0a 502 * @sane: Sane variant which handles irq tracing, context tracking in C
cfa82a00 503 */
424c7d0a 504.macro idtentry_body vector cfunc has_error_code:req sane=0
cfa82a00
TG
505
506 call error_entry
507 UNWIND_HINT_REGS
508
424c7d0a 509 .if \sane == 0
cfa82a00
TG
510 TRACE_IRQS_OFF
511
512#ifdef CONFIG_CONTEXT_TRACKING
513 testb $3, CS(%rsp)
514 jz .Lfrom_kernel_no_ctxt_tracking_\@
515 CALL_enter_from_user_mode
516.Lfrom_kernel_no_ctxt_tracking_\@:
517#endif
424c7d0a 518 .endif
cfa82a00
TG
519
520 movq %rsp, %rdi /* pt_regs pointer into 1st argument*/
521
522 .if \has_error_code == 1
523 movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
524 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
cfa82a00
TG
525 .endif
526
cfa82a00
TG
527 call \cfunc
528
424c7d0a 529 .if \sane == 0
cfa82a00 530 jmp error_exit
424c7d0a
TG
531 .else
532 jmp error_return
533 .endif
cfa82a00
TG
534.endm
535
536/**
537 * idtentry - Macro to generate entry stubs for simple IDT entries
538 * @vector: Vector number
539 * @asmsym: ASM symbol for the entry point
540 * @cfunc: C function to be called
541 * @has_error_code: Hardware pushed error code on stack
424c7d0a 542 * @sane: Sane variant which handles irq tracing, context tracking in C
cfa82a00
TG
543 *
544 * The macro emits code to set up the kernel context for straight forward
545 * and simple IDT entries. No IST stack, no paranoid entry checks.
546 */
424c7d0a 547.macro idtentry vector asmsym cfunc has_error_code:req sane=0
cfa82a00
TG
548SYM_CODE_START(\asmsym)
549 UNWIND_HINT_IRET_REGS offset=\has_error_code*8
550 ASM_CLAC
551
552 .if \has_error_code == 0
553 pushq $-1 /* ORIG_RAX: no syscall to restart */
554 .endif
555
556 .if \vector == X86_TRAP_BP
557 /*
558 * If coming from kernel space, create a 6-word gap to allow the
559 * int3 handler to emulate a call instruction.
560 */
561 testb $3, CS-ORIG_RAX(%rsp)
562 jnz .Lfrom_usermode_no_gap_\@
563 .rept 6
564 pushq 5*8(%rsp)
565 .endr
566 UNWIND_HINT_IRET_REGS offset=8
567.Lfrom_usermode_no_gap_\@:
568 .endif
569
424c7d0a 570 idtentry_body \vector \cfunc \has_error_code \sane
cfa82a00
TG
571
572_ASM_NOKPROBE(\asmsym)
573SYM_CODE_END(\asmsym)
574.endm
575
576/*
577 * MCE and DB exceptions
578 */
579#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss_rw) + (TSS_ist + (x) * 8)
580
581/**
582 * idtentry_mce_db - Macro to generate entry stubs for #MC and #DB
583 * @vector: Vector number
584 * @asmsym: ASM symbol for the entry point
585 * @cfunc: C function to be called
586 *
587 * The macro emits code to set up the kernel context for #MC and #DB
588 *
589 * If the entry comes from user space it uses the normal entry path
590 * including the return to user space work and preemption checks on
591 * exit.
592 *
593 * If hits in kernel mode then it needs to go through the paranoid
594 * entry as the exception can hit any random state. No preemption
595 * check on exit to keep the paranoid path simple.
596 *
597 * If the trap is #DB then the interrupt stack entry in the IST is
598 * moved to the second stack, so a potential recursion will have a
599 * fresh IST.
600 */
601.macro idtentry_mce_db vector asmsym cfunc
602SYM_CODE_START(\asmsym)
603 UNWIND_HINT_IRET_REGS
604 ASM_CLAC
605
606 pushq $-1 /* ORIG_RAX: no syscall to restart */
607
608 /*
609 * If the entry is from userspace, switch stacks and treat it as
610 * a normal entry.
611 */
612 testb $3, CS-ORIG_RAX(%rsp)
613 jnz .Lfrom_usermode_switch_stack_\@
614
615 /*
616 * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX.
617 * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS
618 */
619 call paranoid_entry
620
621 UNWIND_HINT_REGS
622
623 .if \vector == X86_TRAP_DB
624 TRACE_IRQS_OFF_DEBUG
625 .else
626 TRACE_IRQS_OFF
627 .endif
628
629 movq %rsp, %rdi /* pt_regs pointer */
cfa82a00
TG
630
631 .if \vector == X86_TRAP_DB
632 subq $DB_STACK_OFFSET, CPU_TSS_IST(IST_INDEX_DB)
633 .endif
634
635 call \cfunc
636
637 .if \vector == X86_TRAP_DB
638 addq $DB_STACK_OFFSET, CPU_TSS_IST(IST_INDEX_DB)
639 .endif
640
641 jmp paranoid_exit
642
643 /* Switch to the regular task stack and use the noist entry point */
644.Lfrom_usermode_switch_stack_\@:
4c0dcd83 645 idtentry_body vector noist_\cfunc, has_error_code=0 sane=1
cfa82a00
TG
646
647_ASM_NOKPROBE(\asmsym)
648SYM_CODE_END(\asmsym)
649.endm
650
651/*
652 * Double fault entry. Straight paranoid. No checks from which context
653 * this comes because for the espfix induced #DF this would do the wrong
654 * thing.
655 */
656.macro idtentry_df vector asmsym cfunc
657SYM_CODE_START(\asmsym)
658 UNWIND_HINT_IRET_REGS offset=8
659 ASM_CLAC
660
661 /*
662 * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX.
663 * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS
664 */
665 call paranoid_entry
666 UNWIND_HINT_REGS
667
cfa82a00
TG
668 movq %rsp, %rdi /* pt_regs pointer into first argument */
669 movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
670 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
cfa82a00
TG
671 call \cfunc
672
673 jmp paranoid_exit
674
675_ASM_NOKPROBE(\asmsym)
676SYM_CODE_END(\asmsym)
677.endm
678
53aaf262
TG
679/*
680 * Include the defines which emit the idt entries which are shared
681 * shared between 32 and 64 bit.
682 */
683#include <asm/idtentry.h>
684
d99015b1 685/*
f3d415ea 686 * Interrupt entry helper function.
d99015b1 687 *
f3d415ea
DB
688 * Entry runs with interrupts off. Stack layout at entry:
689 * +----------------------------------------------------+
690 * | regs->ss |
691 * | regs->rsp |
692 * | regs->eflags |
693 * | regs->cs |
694 * | regs->ip |
695 * +----------------------------------------------------+
696 * | regs->orig_ax = ~(interrupt number) |
697 * +----------------------------------------------------+
698 * | return address |
699 * +----------------------------------------------------+
d99015b1 700 */
bc7b11c0 701SYM_CODE_START(interrupt_entry)
81b67439 702 UNWIND_HINT_IRET_REGS offset=16
f3d415ea 703 ASM_CLAC
f6f64681 704 cld
7f2590a1 705
f3d415ea 706 testb $3, CS-ORIG_RAX+8(%rsp)
7f2590a1
AL
707 jz 1f
708 SWAPGS
18ec54fd 709 FENCE_SWAPGS_USER_ENTRY
f3d415ea
DB
710 /*
711 * Switch to the thread stack. The IRET frame and orig_ax are
712 * on the stack, as well as the return address. RDI..R12 are
713 * not (yet) on the stack and space has not (yet) been
714 * allocated for them.
715 */
90a6acc4 716 pushq %rdi
f3d415ea 717
90a6acc4
DB
718 /* Need to switch before accessing the thread stack. */
719 SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi
720 movq %rsp, %rdi
721 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
f3d415ea
DB
722
723 /*
724 * We have RDI, return address, and orig_ax on the stack on
725 * top of the IRET frame. That means offset=24
726 */
727 UNWIND_HINT_IRET_REGS base=%rdi offset=24
90a6acc4
DB
728
729 pushq 7*8(%rdi) /* regs->ss */
730 pushq 6*8(%rdi) /* regs->rsp */
731 pushq 5*8(%rdi) /* regs->eflags */
732 pushq 4*8(%rdi) /* regs->cs */
733 pushq 3*8(%rdi) /* regs->ip */
81b67439 734 UNWIND_HINT_IRET_REGS
90a6acc4
DB
735 pushq 2*8(%rdi) /* regs->orig_ax */
736 pushq 8(%rdi) /* return address */
90a6acc4
DB
737
738 movq (%rdi), %rdi
64dbc122 739 jmp 2f
7f2590a1 7401:
18ec54fd
JP
741 FENCE_SWAPGS_KERNEL_ENTRY
7422:
0e34d226
DB
743 PUSH_AND_CLEAR_REGS save_ret=1
744 ENCODE_FRAME_POINTER 8
76f5df43 745
2ba64741 746 testb $3, CS+8(%rsp)
dde74f2e 747 jz 1f
02bc7768
AL
748
749 /*
7f2590a1
AL
750 * IRQ from user mode.
751 *
f1075053
AL
752 * We need to tell lockdep that IRQs are off. We can't do this until
753 * we fix gsbase, and we should do it before enter_from_user_mode
f3d415ea 754 * (which can take locks). Since TRACE_IRQS_OFF is idempotent,
f1075053
AL
755 * the simplest way to handle it is to just call it twice if
756 * we enter from user mode. There's no reason to optimize this since
757 * TRACE_IRQS_OFF is a no-op if lockdep is off.
758 */
759 TRACE_IRQS_OFF
760
478dc89c 761 CALL_enter_from_user_mode
02bc7768 762
76f5df43 7631:
2ba64741 764 ENTER_IRQ_STACK old_rsp=%rdi save_ret=1
f6f64681
DV
765 /* We entered an interrupt context - irqs are off: */
766 TRACE_IRQS_OFF
767
2ba64741 768 ret
bc7b11c0 769SYM_CODE_END(interrupt_entry)
a50480cb 770_ASM_NOKPROBE(interrupt_entry)
2ba64741 771
f3d415ea
DB
772
773/* Interrupt entry/exit. */
1da177e4 774
f8a8fe61
TG
775/*
776 * The interrupt stubs push (~vector+0x80) onto the stack and
777 * then jump to common_spurious/interrupt.
778 */
cc66936e 779SYM_CODE_START_LOCAL(common_spurious)
f8a8fe61
TG
780 addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
781 call interrupt_entry
782 UNWIND_HINT_REGS indirect=1
783 call smp_spurious_interrupt /* rdi points to pt_regs */
784 jmp ret_from_intr
cc66936e 785SYM_CODE_END(common_spurious)
f8a8fe61
TG
786_ASM_NOKPROBE(common_spurious)
787
788/* common_interrupt is a hotpath. Align it */
939b7871 789 .p2align CONFIG_X86_L1_CACHE_SHIFT
cc66936e 790SYM_CODE_START_LOCAL(common_interrupt)
4d732138 791 addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
3aa99fc3
DB
792 call interrupt_entry
793 UNWIND_HINT_REGS indirect=1
794 call do_IRQ /* rdi points to pt_regs */
34061f13 795 /* 0(%rsp): old RSP */
7effaa88 796ret_from_intr:
2140a994 797 DISABLE_INTERRUPTS(CLBR_ANY)
2601e64d 798 TRACE_IRQS_OFF
625dbc3b 799
1d3e53e8 800 LEAVE_IRQ_STACK
625dbc3b 801
03335e95 802 testb $3, CS(%rsp)
dde74f2e 803 jz retint_kernel
4d732138 804
02bc7768 805 /* Interrupt came from user space */
30a2441c 806.Lretint_user:
02bc7768
AL
807 mov %rsp,%rdi
808 call prepare_exit_to_usermode
26c4ef9c 809
26ba4e57 810SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
26c4ef9c
AL
811#ifdef CONFIG_DEBUG_ENTRY
812 /* Assert that pt_regs indicates user mode. */
1e4c4f61 813 testb $3, CS(%rsp)
26c4ef9c
AL
814 jnz 1f
815 ud2
8161:
817#endif
502af0d7 818 POP_REGS pop_rdi=0
3e3b9293
AL
819
820 /*
821 * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS.
822 * Save old stack pointer and switch to trampoline stack.
823 */
824 movq %rsp, %rdi
c482feef 825 movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
1fb14363 826 UNWIND_HINT_EMPTY
3e3b9293
AL
827
828 /* Copy the IRET frame to the trampoline stack. */
829 pushq 6*8(%rdi) /* SS */
830 pushq 5*8(%rdi) /* RSP */
831 pushq 4*8(%rdi) /* EFLAGS */
832 pushq 3*8(%rdi) /* CS */
833 pushq 2*8(%rdi) /* RIP */
834
835 /* Push user RDI on the trampoline stack. */
836 pushq (%rdi)
837
838 /*
839 * We are on the trampoline stack. All regs except RDI are live.
840 * We can do future final exit work right here.
841 */
afaef01c 842 STACKLEAK_ERASE_NOCLOBBER
3e3b9293 843
6fd166aa 844 SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
8a09317b 845
3e3b9293
AL
846 /* Restore RDI. */
847 popq %rdi
848 SWAPGS
26c4ef9c
AL
849 INTERRUPT_RETURN
850
2601e64d 851
627276cb 852/* Returning to kernel space */
6ba71b76 853retint_kernel:
48593975 854#ifdef CONFIG_PREEMPTION
627276cb
DV
855 /* Interrupts are off */
856 /* Check if we need preemption */
6709812f 857 btl $9, EFLAGS(%rsp) /* were interrupts off? */
6ba71b76 858 jnc 1f
b5b447b6 859 cmpl $0, PER_CPU_VAR(__preempt_count)
36acef25 860 jnz 1f
627276cb 861 call preempt_schedule_irq
6ba71b76 8621:
627276cb 863#endif
2601e64d
IM
864 /*
865 * The iretq could re-enable interrupts:
866 */
867 TRACE_IRQS_IRETQ
fffbb5dc 868
26ba4e57 869SYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)
26c4ef9c
AL
870#ifdef CONFIG_DEBUG_ENTRY
871 /* Assert that pt_regs indicates kernel mode. */
1e4c4f61 872 testb $3, CS(%rsp)
26c4ef9c
AL
873 jz 1f
874 ud2
8751:
876#endif
502af0d7 877 POP_REGS
e872045b 878 addq $8, %rsp /* skip regs->orig_ax */
10bcc80e
MD
879 /*
880 * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
881 * when returning from IPI handler.
882 */
7209a75d
AL
883 INTERRUPT_RETURN
884
cc66936e 885SYM_INNER_LABEL_ALIGN(native_iret, SYM_L_GLOBAL)
8c1f7558 886 UNWIND_HINT_IRET_REGS
3891a04a
PA
887 /*
888 * Are we returning to a stack segment from the LDT? Note: in
889 * 64-bit mode SS:RSP on the exception stack is always valid.
890 */
34273f41 891#ifdef CONFIG_X86_ESPFIX64
4d732138
IM
892 testb $4, (SS-RIP)(%rsp)
893 jnz native_irq_return_ldt
34273f41 894#endif
3891a04a 895
cc66936e 896SYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL)
b645af2d
AL
897 /*
898 * This may fault. Non-paranoid faults on return to userspace are
899 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
c29c775a 900 * Double-faults due to espfix64 are handled in exc_double_fault.
b645af2d
AL
901 * Other faults here are fatal.
902 */
1da177e4 903 iretq
3701d863 904
34273f41 905#ifdef CONFIG_X86_ESPFIX64
7209a75d 906native_irq_return_ldt:
85063fac
AL
907 /*
908 * We are running with user GSBASE. All GPRs contain their user
909 * values. We have a percpu ESPFIX stack that is eight slots
910 * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom
911 * of the ESPFIX stack.
912 *
913 * We clobber RAX and RDI in this code. We stash RDI on the
914 * normal stack and RAX on the ESPFIX stack.
915 *
916 * The ESPFIX stack layout we set up looks like this:
917 *
918 * --- top of ESPFIX stack ---
919 * SS
920 * RSP
921 * RFLAGS
922 * CS
923 * RIP <-- RSP points here when we're done
924 * RAX <-- espfix_waddr points here
925 * --- bottom of ESPFIX stack ---
926 */
927
928 pushq %rdi /* Stash user RDI */
8a09317b
DH
929 SWAPGS /* to kernel GS */
930 SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi /* to kernel CR3 */
931
4d732138 932 movq PER_CPU_VAR(espfix_waddr), %rdi
85063fac
AL
933 movq %rax, (0*8)(%rdi) /* user RAX */
934 movq (1*8)(%rsp), %rax /* user RIP */
4d732138 935 movq %rax, (1*8)(%rdi)
85063fac 936 movq (2*8)(%rsp), %rax /* user CS */
4d732138 937 movq %rax, (2*8)(%rdi)
85063fac 938 movq (3*8)(%rsp), %rax /* user RFLAGS */
4d732138 939 movq %rax, (3*8)(%rdi)
85063fac 940 movq (5*8)(%rsp), %rax /* user SS */
4d732138 941 movq %rax, (5*8)(%rdi)
85063fac 942 movq (4*8)(%rsp), %rax /* user RSP */
4d732138 943 movq %rax, (4*8)(%rdi)
85063fac
AL
944 /* Now RAX == RSP. */
945
946 andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */
85063fac
AL
947
948 /*
949 * espfix_stack[31:16] == 0. The page tables are set up such that
950 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
951 * espfix_waddr for any X. That is, there are 65536 RO aliases of
952 * the same page. Set up RSP so that RSP[31:16] contains the
953 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
954 * still points to an RO alias of the ESPFIX stack.
955 */
4d732138 956 orq PER_CPU_VAR(espfix_stack), %rax
8a09317b 957
6fd166aa 958 SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
8a09317b
DH
959 SWAPGS /* to user GS */
960 popq %rdi /* Restore user RDI */
961
4d732138 962 movq %rax, %rsp
8c1f7558 963 UNWIND_HINT_IRET_REGS offset=8
85063fac
AL
964
965 /*
966 * At this point, we cannot write to the stack any more, but we can
967 * still read.
968 */
969 popq %rax /* Restore user RAX */
970
971 /*
972 * RSP now points to an ordinary IRET frame, except that the page
973 * is read-only and RSP[31:16] are preloaded with the userspace
974 * values. We can now IRET back to userspace.
975 */
4d732138 976 jmp native_irq_return_iret
34273f41 977#endif
cc66936e 978SYM_CODE_END(common_interrupt)
a50480cb 979_ASM_NOKPROBE(common_interrupt)
3891a04a 980
1da177e4
LT
981/*
982 * APIC interrupts.
0bd7b798 983 */
cf910e83 984.macro apicinterrupt3 num sym do_sym
bc7b11c0 985SYM_CODE_START(\sym)
8c1f7558 986 UNWIND_HINT_IRET_REGS
4d732138 987 pushq $~(\num)
3aa99fc3
DB
988 call interrupt_entry
989 UNWIND_HINT_REGS indirect=1
990 call \do_sym /* rdi points to pt_regs */
4d732138 991 jmp ret_from_intr
bc7b11c0 992SYM_CODE_END(\sym)
a50480cb 993_ASM_NOKPROBE(\sym)
322648d1 994.endm
1da177e4 995
469f0023 996/* Make sure APIC interrupt handlers end up in the irqentry section: */
229a7186
MH
997#define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
998#define POP_SECTION_IRQENTRY .popsection
469f0023 999
cf910e83 1000.macro apicinterrupt num sym do_sym
469f0023 1001PUSH_SECTION_IRQENTRY
cf910e83 1002apicinterrupt3 \num \sym \do_sym
469f0023 1003POP_SECTION_IRQENTRY
cf910e83
SA
1004.endm
1005
322648d1 1006#ifdef CONFIG_SMP
4d732138
IM
1007apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
1008apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
322648d1 1009#endif
1da177e4 1010
03b48632 1011#ifdef CONFIG_X86_UV
4d732138 1012apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
03b48632 1013#endif
4d732138
IM
1014
1015apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
1016apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
89b831ef 1017
d78f2664 1018#ifdef CONFIG_HAVE_KVM
4d732138
IM
1019apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
1020apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
210f84b0 1021apicinterrupt3 POSTED_INTR_NESTED_VECTOR kvm_posted_intr_nested_ipi smp_kvm_posted_intr_nested_ipi
d78f2664
YZ
1022#endif
1023
33e5ff63 1024#ifdef CONFIG_X86_MCE_THRESHOLD
4d732138 1025apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
33e5ff63
SA
1026#endif
1027
24fd78a8 1028#ifdef CONFIG_X86_MCE_AMD
4d732138 1029apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
24fd78a8
AG
1030#endif
1031
33e5ff63 1032#ifdef CONFIG_X86_THERMAL_VECTOR
4d732138 1033apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
33e5ff63 1034#endif
1812924b 1035
322648d1 1036#ifdef CONFIG_SMP
4d732138
IM
1037apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
1038apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
1039apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
322648d1 1040#endif
1da177e4 1041
4d732138
IM
1042apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
1043apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
0bd7b798 1044
e360adbe 1045#ifdef CONFIG_IRQ_WORK
4d732138 1046apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
241771ef
IM
1047#endif
1048
b9f6976b
TG
1049/*
1050 * Reload gs selector with exception handling
1051 * edi: new selector
1052 *
1053 * Is in entry.text as it shouldn't be instrumented.
1054 */
410367e3 1055SYM_FUNC_START(asm_load_gs_index)
8c1f7558 1056 FRAME_BEGIN
c9317202 1057 swapgs
42c748bb 1058.Lgs_change:
4d732138 1059 movl %edi, %gs
96e5d28a 10602: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
c9317202 1061 swapgs
8c1f7558 1062 FRAME_END
9f1e87ea 1063 ret
410367e3
TG
1064SYM_FUNC_END(asm_load_gs_index)
1065EXPORT_SYMBOL(asm_load_gs_index)
0bd7b798 1066
98ededb6 1067 _ASM_EXTABLE(.Lgs_change, .Lbad_gs)
4d732138 1068 .section .fixup, "ax"
1da177e4 1069 /* running with kernelgs */
ef77e688 1070SYM_CODE_START_LOCAL_NOALIGN(.Lbad_gs)
c9317202 1071 swapgs /* switch back to user gs */
b038c842
AL
1072.macro ZAP_GS
1073 /* This can't be a string because the preprocessor needs to see it. */
1074 movl $__USER_DS, %eax
1075 movl %eax, %gs
1076.endm
1077 ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
4d732138
IM
1078 xorl %eax, %eax
1079 movl %eax, %gs
1080 jmp 2b
ef77e688 1081SYM_CODE_END(.Lbad_gs)
9f1e87ea 1082 .previous
0bd7b798 1083
931b9414
TG
1084/*
1085 * rdi: New stack pointer points to the top word of the stack
1086 * rsi: Function pointer
1087 * rdx: Function argument (can be NULL if none)
1088 */
1089SYM_FUNC_START(asm_call_on_stack)
1090 /*
1091 * Save the frame pointer unconditionally. This allows the ORC
1092 * unwinder to handle the stack switch.
1093 */
1094 pushq %rbp
1095 mov %rsp, %rbp
1096
1097 /*
1098 * The unwinder relies on the word at the top of the new stack
1099 * page linking back to the previous RSP.
1100 */
1101 mov %rsp, (%rdi)
1102 mov %rdi, %rsp
1103 /* Move the argument to the right place */
1104 mov %rdx, %rdi
1105
11061:
1107 .pushsection .discard.instr_begin
1108 .long 1b - .
1109 .popsection
1110
1111 CALL_NOSPEC rsi
1112
11132:
1114 .pushsection .discard.instr_end
1115 .long 2b - .
1116 .popsection
1117
1118 /* Restore the previous stack pointer from RBP. */
1119 leaveq
1120 ret
1121SYM_FUNC_END(asm_call_on_stack)
1122
28c11b0f 1123#ifdef CONFIG_XEN_PV
3d75e1b8 1124/*
9f1e87ea
CG
1125 * A note on the "critical region" in our callback handler.
1126 * We want to avoid stacking callback handlers due to events occurring
1127 * during handling of the last event. To do this, we keep events disabled
1128 * until we've done all processing. HOWEVER, we must enable events before
1129 * popping the stack frame (can't be done atomically) and so it would still
1130 * be possible to get enough handler activations to overflow the stack.
1131 * Although unlikely, bugs of that kind are hard to track down, so we'd
1132 * like to avoid the possibility.
1133 * So, on entry to the handler we detect whether we interrupted an
1134 * existing activation in its critical region -- if so, we pop the current
1135 * activation and restart the handler using the previous one.
2f6474e4
TG
1136 *
1137 * C calling convention: exc_xen_hypervisor_callback(struct *pt_regs)
9f1e87ea 1138 */
2f6474e4 1139SYM_CODE_START_LOCAL(exc_xen_hypervisor_callback)
4d732138 1140
9f1e87ea
CG
1141/*
1142 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1143 * see the correct pointer to the pt_regs
1144 */
8c1f7558 1145 UNWIND_HINT_FUNC
4d732138 1146 movq %rdi, %rsp /* we don't return, adjust the stack frame */
8c1f7558 1147 UNWIND_HINT_REGS
1d3e53e8 1148
2f6474e4 1149 call xen_pv_evtchn_do_upcall
1d3e53e8 1150
2f6474e4
TG
1151 jmp error_return
1152SYM_CODE_END(exc_xen_hypervisor_callback)
3d75e1b8
JF
1153
1154/*
9f1e87ea
CG
1155 * Hypervisor uses this for application faults while it executes.
1156 * We get here for two reasons:
1157 * 1. Fault while reloading DS, ES, FS or GS
1158 * 2. Fault while executing IRET
1159 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1160 * registers that could be reloaded and zeroed the others.
1161 * Category 2 we fix up by killing the current process. We cannot use the
1162 * normal Linux return path in this case because if we use the IRET hypercall
1163 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1164 * We distinguish between categories by comparing each saved segment register
1165 * with its current contents: any discrepancy means we in category 1.
1166 */
bc7b11c0 1167SYM_CODE_START(xen_failsafe_callback)
8c1f7558 1168 UNWIND_HINT_EMPTY
4d732138
IM
1169 movl %ds, %ecx
1170 cmpw %cx, 0x10(%rsp)
1171 jne 1f
1172 movl %es, %ecx
1173 cmpw %cx, 0x18(%rsp)
1174 jne 1f
1175 movl %fs, %ecx
1176 cmpw %cx, 0x20(%rsp)
1177 jne 1f
1178 movl %gs, %ecx
1179 cmpw %cx, 0x28(%rsp)
1180 jne 1f
3d75e1b8 1181 /* All segments match their saved values => Category 2 (Bad IRET). */
4d732138
IM
1182 movq (%rsp), %rcx
1183 movq 8(%rsp), %r11
1184 addq $0x30, %rsp
1185 pushq $0 /* RIP */
8c1f7558 1186 UNWIND_HINT_IRET_REGS offset=8
be4c11af 1187 jmp asm_exc_general_protection
3d75e1b8 11881: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
4d732138
IM
1189 movq (%rsp), %rcx
1190 movq 8(%rsp), %r11
1191 addq $0x30, %rsp
8c1f7558 1192 UNWIND_HINT_IRET_REGS
4d732138 1193 pushq $-1 /* orig_ax = -1 => not a system call */
3f01daec 1194 PUSH_AND_CLEAR_REGS
946c1911 1195 ENCODE_FRAME_POINTER
4d732138 1196 jmp error_exit
bc7b11c0 1197SYM_CODE_END(xen_failsafe_callback)
28c11b0f 1198#endif /* CONFIG_XEN_PV */
3d75e1b8 1199
28c11b0f 1200#ifdef CONFIG_XEN_PVHVM
cf910e83 1201apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
38e20b07 1202 xen_hvm_callback_vector xen_evtchn_do_upcall
28c11b0f 1203#endif
38e20b07 1204
ddeb8f21 1205
bc2b0331 1206#if IS_ENABLED(CONFIG_HYPERV)
cf910e83 1207apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
bc2b0331 1208 hyperv_callback_vector hyperv_vector_handler
93286261
VK
1209
1210apicinterrupt3 HYPERV_REENLIGHTENMENT_VECTOR \
1211 hyperv_reenlightenment_vector hyperv_reenlightenment_intr
248e742a
MK
1212
1213apicinterrupt3 HYPERV_STIMER0_VECTOR \
1214 hv_stimer0_callback_vector hv_stimer0_vector_handler
bc2b0331
S
1215#endif /* CONFIG_HYPERV */
1216
498ad393
ZY
1217#if IS_ENABLED(CONFIG_ACRN_GUEST)
1218apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1219 acrn_hv_callback_vector acrn_hv_vector_handler
1220#endif
1221
ebfc453e 1222/*
9e809d15 1223 * Save all registers in pt_regs, and switch gs if needed.
ebfc453e
DV
1224 * Use slow, but surefire "are we in kernel?" check.
1225 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1226 */
ef1e0315 1227SYM_CODE_START_LOCAL(paranoid_entry)
8c1f7558 1228 UNWIND_HINT_FUNC
1eeb207f 1229 cld
9e809d15
DB
1230 PUSH_AND_CLEAR_REGS save_ret=1
1231 ENCODE_FRAME_POINTER 8
4d732138
IM
1232 movl $1, %ebx
1233 movl $MSR_GS_BASE, %ecx
1eeb207f 1234 rdmsr
4d732138
IM
1235 testl %edx, %edx
1236 js 1f /* negative -> in kernel */
1eeb207f 1237 SWAPGS
4d732138 1238 xorl %ebx, %ebx
8a09317b
DH
1239
12401:
16561f27
DH
1241 /*
1242 * Always stash CR3 in %r14. This value will be restored,
ae852495
AL
1243 * verbatim, at exit. Needed if paranoid_entry interrupted
1244 * another entry that already switched to the user CR3 value
1245 * but has not yet returned to userspace.
16561f27
DH
1246 *
1247 * This is also why CS (stashed in the "iret frame" by the
1248 * hardware at entry) can not be used: this may be a return
ae852495 1249 * to kernel code, but with a user CR3 value.
16561f27 1250 */
8a09317b
DH
1251 SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
1252
18ec54fd
JP
1253 /*
1254 * The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an
1255 * unconditional CR3 write, even in the PTI case. So do an lfence
1256 * to prevent GS speculation, regardless of whether PTI is enabled.
1257 */
1258 FENCE_SWAPGS_KERNEL_ENTRY
1259
8a09317b 1260 ret
ef1e0315 1261SYM_CODE_END(paranoid_entry)
ddeb8f21 1262
ebfc453e
DV
1263/*
1264 * "Paranoid" exit path from exception stack. This is invoked
1265 * only on return from non-NMI IST interrupts that came
1266 * from kernel space.
1267 *
1268 * We may be returning to very strange contexts (e.g. very early
1269 * in syscall entry), so checking for preemption here would
1270 * be complicated. Fortunately, we there's no good reason
1271 * to try to handle preemption here.
4d732138
IM
1272 *
1273 * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
ebfc453e 1274 */
ef1e0315 1275SYM_CODE_START_LOCAL(paranoid_exit)
8c1f7558 1276 UNWIND_HINT_REGS
2140a994 1277 DISABLE_INTERRUPTS(CLBR_ANY)
5963e317 1278 TRACE_IRQS_OFF_DEBUG
4d732138 1279 testl %ebx, %ebx /* swapgs needed? */
e5317832 1280 jnz .Lparanoid_exit_no_swapgs
f2db9382 1281 TRACE_IRQS_IRETQ
16561f27 1282 /* Always restore stashed CR3 value (see paranoid_entry) */
21e94459 1283 RESTORE_CR3 scratch_reg=%rbx save_reg=%r14
ddeb8f21 1284 SWAPGS_UNSAFE_STACK
45c08383 1285 jmp restore_regs_and_return_to_kernel
e5317832 1286.Lparanoid_exit_no_swapgs:
f2db9382 1287 TRACE_IRQS_IRETQ_DEBUG
16561f27 1288 /* Always restore stashed CR3 value (see paranoid_entry) */
e4865757 1289 RESTORE_CR3 scratch_reg=%rbx save_reg=%r14
e5317832 1290 jmp restore_regs_and_return_to_kernel
ef1e0315 1291SYM_CODE_END(paranoid_exit)
ddeb8f21
AH
1292
1293/*
9e809d15 1294 * Save all registers in pt_regs, and switch GS if needed.
ddeb8f21 1295 */
ef1e0315 1296SYM_CODE_START_LOCAL(error_entry)
9e809d15 1297 UNWIND_HINT_FUNC
ddeb8f21 1298 cld
9e809d15
DB
1299 PUSH_AND_CLEAR_REGS save_ret=1
1300 ENCODE_FRAME_POINTER 8
03335e95 1301 testb $3, CS+8(%rsp)
cb6f64ed 1302 jz .Lerror_kernelspace
539f5113 1303
cb6f64ed
AL
1304 /*
1305 * We entered from user mode or we're pretending to have entered
1306 * from user mode due to an IRET fault.
1307 */
ddeb8f21 1308 SWAPGS
18ec54fd 1309 FENCE_SWAPGS_USER_ENTRY
8a09317b
DH
1310 /* We have user CR3. Change to kernel CR3. */
1311 SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
539f5113 1312
cb6f64ed 1313.Lerror_entry_from_usermode_after_swapgs:
7f2590a1
AL
1314 /* Put us onto the real thread stack. */
1315 popq %r12 /* save return addr in %12 */
1316 movq %rsp, %rdi /* arg0 = pt_regs pointer */
1317 call sync_regs
1318 movq %rax, %rsp /* switch stack */
1319 ENCODE_FRAME_POINTER
1320 pushq %r12
f1075053 1321 ret
02bc7768 1322
18ec54fd
JP
1323.Lerror_entry_done_lfence:
1324 FENCE_SWAPGS_KERNEL_ENTRY
cb6f64ed 1325.Lerror_entry_done:
ddeb8f21 1326 ret
ddeb8f21 1327
ebfc453e
DV
1328 /*
1329 * There are two places in the kernel that can potentially fault with
1330 * usergs. Handle them here. B stepping K8s sometimes report a
1331 * truncated RIP for IRET exceptions returning to compat mode. Check
1332 * for these here too.
1333 */
cb6f64ed 1334.Lerror_kernelspace:
4d732138
IM
1335 leaq native_irq_return_iret(%rip), %rcx
1336 cmpq %rcx, RIP+8(%rsp)
cb6f64ed 1337 je .Lerror_bad_iret
4d732138
IM
1338 movl %ecx, %eax /* zero extend */
1339 cmpq %rax, RIP+8(%rsp)
cb6f64ed 1340 je .Lbstep_iret
42c748bb 1341 cmpq $.Lgs_change, RIP+8(%rsp)
18ec54fd 1342 jne .Lerror_entry_done_lfence
539f5113
AL
1343
1344 /*
42c748bb 1345 * hack: .Lgs_change can fail with user gsbase. If this happens, fix up
539f5113 1346 * gsbase and proceed. We'll fix up the exception and land in
42c748bb 1347 * .Lgs_change's error handler with kernel gsbase.
539f5113 1348 */
2fa5f04f 1349 SWAPGS
18ec54fd 1350 FENCE_SWAPGS_USER_ENTRY
2fa5f04f 1351 jmp .Lerror_entry_done
ae24ffe5 1352
cb6f64ed 1353.Lbstep_iret:
ae24ffe5 1354 /* Fix truncated RIP */
4d732138 1355 movq %rcx, RIP+8(%rsp)
b645af2d
AL
1356 /* fall through */
1357
cb6f64ed 1358.Lerror_bad_iret:
539f5113 1359 /*
8a09317b
DH
1360 * We came from an IRET to user mode, so we have user
1361 * gsbase and CR3. Switch to kernel gsbase and CR3:
539f5113 1362 */
b645af2d 1363 SWAPGS
18ec54fd 1364 FENCE_SWAPGS_USER_ENTRY
8a09317b 1365 SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
539f5113
AL
1366
1367 /*
1368 * Pretend that the exception came from user mode: set up pt_regs
b3681dd5 1369 * as if we faulted immediately after IRET.
539f5113 1370 */
4d732138
IM
1371 mov %rsp, %rdi
1372 call fixup_bad_iret
1373 mov %rax, %rsp
cb6f64ed 1374 jmp .Lerror_entry_from_usermode_after_swapgs
ef1e0315 1375SYM_CODE_END(error_entry)
ddeb8f21 1376
ef1e0315 1377SYM_CODE_START_LOCAL(error_exit)
8c1f7558 1378 UNWIND_HINT_REGS
2140a994 1379 DISABLE_INTERRUPTS(CLBR_ANY)
ddeb8f21 1380 TRACE_IRQS_OFF
b3681dd5
AL
1381 testb $3, CS(%rsp)
1382 jz retint_kernel
30a2441c 1383 jmp .Lretint_user
ef1e0315 1384SYM_CODE_END(error_exit)
ddeb8f21 1385
424c7d0a
TG
1386SYM_CODE_START_LOCAL(error_return)
1387 UNWIND_HINT_REGS
1388 DEBUG_ENTRY_ASSERT_IRQS_OFF
1389 testb $3, CS(%rsp)
1390 jz restore_regs_and_return_to_kernel
1391 jmp swapgs_restore_regs_and_return_to_usermode
1392SYM_CODE_END(error_return)
1393
929bacec
AL
1394/*
1395 * Runs on exception stack. Xen PV does not go through this path at all,
1396 * so we can use real assembly here.
8a09317b
DH
1397 *
1398 * Registers:
1399 * %r14: Used to save/restore the CR3 of the interrupted context
1400 * when PAGE_TABLE_ISOLATION is in use. Do not clobber.
929bacec 1401 */
6271fef0 1402SYM_CODE_START(asm_exc_nmi)
8c1f7558 1403 UNWIND_HINT_IRET_REGS
929bacec 1404
3f3c8b8c
SR
1405 /*
1406 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1407 * the iretq it performs will take us out of NMI context.
1408 * This means that we can have nested NMIs where the next
1409 * NMI is using the top of the stack of the previous NMI. We
1410 * can't let it execute because the nested NMI will corrupt the
1411 * stack of the previous NMI. NMI handlers are not re-entrant
1412 * anyway.
1413 *
1414 * To handle this case we do the following:
1415 * Check the a special location on the stack that contains
1416 * a variable that is set when NMIs are executing.
1417 * The interrupted task's stack is also checked to see if it
1418 * is an NMI stack.
1419 * If the variable is not set and the stack is not the NMI
1420 * stack then:
1421 * o Set the special variable on the stack
0b22930e
AL
1422 * o Copy the interrupt frame into an "outermost" location on the
1423 * stack
1424 * o Copy the interrupt frame into an "iret" location on the stack
3f3c8b8c
SR
1425 * o Continue processing the NMI
1426 * If the variable is set or the previous stack is the NMI stack:
0b22930e 1427 * o Modify the "iret" location to jump to the repeat_nmi
3f3c8b8c
SR
1428 * o return back to the first NMI
1429 *
1430 * Now on exit of the first NMI, we first clear the stack variable
1431 * The NMI stack will tell any nested NMIs at that point that it is
1432 * nested. Then we pop the stack normally with iret, and if there was
1433 * a nested NMI that updated the copy interrupt stack frame, a
1434 * jump will be made to the repeat_nmi code that will handle the second
1435 * NMI.
9b6e6a83
AL
1436 *
1437 * However, espfix prevents us from directly returning to userspace
1438 * with a single IRET instruction. Similarly, IRET to user mode
1439 * can fault. We therefore handle NMIs from user space like
1440 * other IST entries.
3f3c8b8c
SR
1441 */
1442
e93c1730
AL
1443 ASM_CLAC
1444
146b2b09 1445 /* Use %rdx as our temp variable throughout */
4d732138 1446 pushq %rdx
3f3c8b8c 1447
9b6e6a83
AL
1448 testb $3, CS-RIP+8(%rsp)
1449 jz .Lnmi_from_kernel
1450
1451 /*
1452 * NMI from user mode. We need to run on the thread stack, but we
1453 * can't go through the normal entry paths: NMIs are masked, and
1454 * we don't want to enable interrupts, because then we'll end
1455 * up in an awkward situation in which IRQs are on but NMIs
1456 * are off.
83c133cf
AL
1457 *
1458 * We also must not push anything to the stack before switching
1459 * stacks lest we corrupt the "NMI executing" variable.
9b6e6a83
AL
1460 */
1461
929bacec 1462 swapgs
9b6e6a83 1463 cld
18ec54fd 1464 FENCE_SWAPGS_USER_ENTRY
8a09317b 1465 SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
9b6e6a83
AL
1466 movq %rsp, %rdx
1467 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
8c1f7558 1468 UNWIND_HINT_IRET_REGS base=%rdx offset=8
9b6e6a83
AL
1469 pushq 5*8(%rdx) /* pt_regs->ss */
1470 pushq 4*8(%rdx) /* pt_regs->rsp */
1471 pushq 3*8(%rdx) /* pt_regs->flags */
1472 pushq 2*8(%rdx) /* pt_regs->cs */
1473 pushq 1*8(%rdx) /* pt_regs->rip */
8c1f7558 1474 UNWIND_HINT_IRET_REGS
9b6e6a83 1475 pushq $-1 /* pt_regs->orig_ax */
30907fd1 1476 PUSH_AND_CLEAR_REGS rdx=(%rdx)
946c1911 1477 ENCODE_FRAME_POINTER
9b6e6a83
AL
1478
1479 /*
1480 * At this point we no longer need to worry about stack damage
1481 * due to nesting -- we're on the normal thread stack and we're
1482 * done with the NMI stack.
1483 */
1484
1485 movq %rsp, %rdi
1486 movq $-1, %rsi
6271fef0 1487 call exc_nmi
9b6e6a83 1488
45d5a168 1489 /*
9b6e6a83 1490 * Return back to user mode. We must *not* do the normal exit
946c1911 1491 * work, because we don't want to enable interrupts.
45d5a168 1492 */
8a055d7f 1493 jmp swapgs_restore_regs_and_return_to_usermode
45d5a168 1494
9b6e6a83 1495.Lnmi_from_kernel:
3f3c8b8c 1496 /*
0b22930e
AL
1497 * Here's what our stack frame will look like:
1498 * +---------------------------------------------------------+
1499 * | original SS |
1500 * | original Return RSP |
1501 * | original RFLAGS |
1502 * | original CS |
1503 * | original RIP |
1504 * +---------------------------------------------------------+
1505 * | temp storage for rdx |
1506 * +---------------------------------------------------------+
1507 * | "NMI executing" variable |
1508 * +---------------------------------------------------------+
1509 * | iret SS } Copied from "outermost" frame |
1510 * | iret Return RSP } on each loop iteration; overwritten |
1511 * | iret RFLAGS } by a nested NMI to force another |
1512 * | iret CS } iteration if needed. |
1513 * | iret RIP } |
1514 * +---------------------------------------------------------+
1515 * | outermost SS } initialized in first_nmi; |
1516 * | outermost Return RSP } will not be changed before |
1517 * | outermost RFLAGS } NMI processing is done. |
1518 * | outermost CS } Copied to "iret" frame on each |
1519 * | outermost RIP } iteration. |
1520 * +---------------------------------------------------------+
1521 * | pt_regs |
1522 * +---------------------------------------------------------+
1523 *
1524 * The "original" frame is used by hardware. Before re-enabling
1525 * NMIs, we need to be done with it, and we need to leave enough
1526 * space for the asm code here.
1527 *
1528 * We return by executing IRET while RSP points to the "iret" frame.
1529 * That will either return for real or it will loop back into NMI
1530 * processing.
1531 *
1532 * The "outermost" frame is copied to the "iret" frame on each
1533 * iteration of the loop, so each iteration starts with the "iret"
1534 * frame pointing to the final return target.
1535 */
1536
45d5a168 1537 /*
0b22930e
AL
1538 * Determine whether we're a nested NMI.
1539 *
a27507ca
AL
1540 * If we interrupted kernel code between repeat_nmi and
1541 * end_repeat_nmi, then we are a nested NMI. We must not
1542 * modify the "iret" frame because it's being written by
1543 * the outer NMI. That's okay; the outer NMI handler is
6271fef0 1544 * about to about to call exc_nmi() anyway, so we can just
a27507ca 1545 * resume the outer NMI.
45d5a168 1546 */
a27507ca
AL
1547
1548 movq $repeat_nmi, %rdx
1549 cmpq 8(%rsp), %rdx
1550 ja 1f
1551 movq $end_repeat_nmi, %rdx
1552 cmpq 8(%rsp), %rdx
1553 ja nested_nmi_out
15541:
45d5a168 1555
3f3c8b8c 1556 /*
a27507ca 1557 * Now check "NMI executing". If it's set, then we're nested.
0b22930e
AL
1558 * This will not detect if we interrupted an outer NMI just
1559 * before IRET.
3f3c8b8c 1560 */
4d732138
IM
1561 cmpl $1, -8(%rsp)
1562 je nested_nmi
3f3c8b8c
SR
1563
1564 /*
0b22930e
AL
1565 * Now test if the previous stack was an NMI stack. This covers
1566 * the case where we interrupt an outer NMI after it clears
810bc075
AL
1567 * "NMI executing" but before IRET. We need to be careful, though:
1568 * there is one case in which RSP could point to the NMI stack
1569 * despite there being no NMI active: naughty userspace controls
1570 * RSP at the very beginning of the SYSCALL targets. We can
1571 * pull a fast one on naughty userspace, though: we program
1572 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1573 * if it controls the kernel's RSP. We set DF before we clear
1574 * "NMI executing".
3f3c8b8c 1575 */
0784b364
DV
1576 lea 6*8(%rsp), %rdx
1577 /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1578 cmpq %rdx, 4*8(%rsp)
1579 /* If the stack pointer is above the NMI stack, this is a normal NMI */
1580 ja first_nmi
4d732138 1581
0784b364
DV
1582 subq $EXCEPTION_STKSZ, %rdx
1583 cmpq %rdx, 4*8(%rsp)
1584 /* If it is below the NMI stack, it is a normal NMI */
1585 jb first_nmi
810bc075
AL
1586
1587 /* Ah, it is within the NMI stack. */
1588
1589 testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1590 jz first_nmi /* RSP was user controlled. */
1591
1592 /* This is a nested NMI. */
0784b364 1593
3f3c8b8c
SR
1594nested_nmi:
1595 /*
0b22930e
AL
1596 * Modify the "iret" frame to point to repeat_nmi, forcing another
1597 * iteration of NMI handling.
3f3c8b8c 1598 */
23a781e9 1599 subq $8, %rsp
4d732138
IM
1600 leaq -10*8(%rsp), %rdx
1601 pushq $__KERNEL_DS
1602 pushq %rdx
131484c8 1603 pushfq
4d732138
IM
1604 pushq $__KERNEL_CS
1605 pushq $repeat_nmi
3f3c8b8c
SR
1606
1607 /* Put stack back */
4d732138 1608 addq $(6*8), %rsp
3f3c8b8c
SR
1609
1610nested_nmi_out:
4d732138 1611 popq %rdx
3f3c8b8c 1612
0b22930e 1613 /* We are returning to kernel mode, so this cannot result in a fault. */
929bacec 1614 iretq
3f3c8b8c
SR
1615
1616first_nmi:
0b22930e 1617 /* Restore rdx. */
4d732138 1618 movq (%rsp), %rdx
62610913 1619
36f1a77b
AL
1620 /* Make room for "NMI executing". */
1621 pushq $0
3f3c8b8c 1622
0b22930e 1623 /* Leave room for the "iret" frame */
4d732138 1624 subq $(5*8), %rsp
28696f43 1625
0b22930e 1626 /* Copy the "original" frame to the "outermost" frame */
3f3c8b8c 1627 .rept 5
4d732138 1628 pushq 11*8(%rsp)
3f3c8b8c 1629 .endr
8c1f7558 1630 UNWIND_HINT_IRET_REGS
62610913 1631
79fb4ad6
SR
1632 /* Everything up to here is safe from nested NMIs */
1633
a97439aa
AL
1634#ifdef CONFIG_DEBUG_ENTRY
1635 /*
1636 * For ease of testing, unmask NMIs right away. Disabled by
1637 * default because IRET is very expensive.
1638 */
1639 pushq $0 /* SS */
1640 pushq %rsp /* RSP (minus 8 because of the previous push) */
1641 addq $8, (%rsp) /* Fix up RSP */
1642 pushfq /* RFLAGS */
1643 pushq $__KERNEL_CS /* CS */
1644 pushq $1f /* RIP */
929bacec 1645 iretq /* continues at repeat_nmi below */
8c1f7558 1646 UNWIND_HINT_IRET_REGS
a97439aa
AL
16471:
1648#endif
1649
0b22930e 1650repeat_nmi:
62610913
JB
1651 /*
1652 * If there was a nested NMI, the first NMI's iret will return
1653 * here. But NMIs are still enabled and we can take another
1654 * nested NMI. The nested NMI checks the interrupted RIP to see
1655 * if it is between repeat_nmi and end_repeat_nmi, and if so
1656 * it will just return, as we are about to repeat an NMI anyway.
1657 * This makes it safe to copy to the stack frame that a nested
1658 * NMI will update.
0b22930e
AL
1659 *
1660 * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
1661 * we're repeating an NMI, gsbase has the same value that it had on
1662 * the first iteration. paranoid_entry will load the kernel
6271fef0 1663 * gsbase if needed before we call exc_nmi(). "NMI executing"
36f1a77b 1664 * is zero.
62610913 1665 */
36f1a77b 1666 movq $1, 10*8(%rsp) /* Set "NMI executing". */
3f3c8b8c 1667
62610913 1668 /*
0b22930e
AL
1669 * Copy the "outermost" frame to the "iret" frame. NMIs that nest
1670 * here must not modify the "iret" frame while we're writing to
1671 * it or it will end up containing garbage.
62610913 1672 */
4d732138 1673 addq $(10*8), %rsp
3f3c8b8c 1674 .rept 5
4d732138 1675 pushq -6*8(%rsp)
3f3c8b8c 1676 .endr
4d732138 1677 subq $(5*8), %rsp
62610913 1678end_repeat_nmi:
3f3c8b8c
SR
1679
1680 /*
0b22930e
AL
1681 * Everything below this point can be preempted by a nested NMI.
1682 * If this happens, then the inner NMI will change the "iret"
1683 * frame to point back to repeat_nmi.
3f3c8b8c 1684 */
4d732138 1685 pushq $-1 /* ORIG_RAX: no syscall to restart */
76f5df43 1686
1fd466ef 1687 /*
ebfc453e 1688 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1fd466ef
SR
1689 * as we should not be calling schedule in NMI context.
1690 * Even with normal interrupts enabled. An NMI should not be
1691 * setting NEED_RESCHED or anything that normal interrupts and
1692 * exceptions might do.
1693 */
4d732138 1694 call paranoid_entry
8c1f7558 1695 UNWIND_HINT_REGS
7fbb98c5 1696
6271fef0 1697 /* paranoidentry exc_nmi(), 0; without TRACE_IRQS_OFF */
4d732138
IM
1698 movq %rsp, %rdi
1699 movq $-1, %rsi
6271fef0 1700 call exc_nmi
7fbb98c5 1701
16561f27 1702 /* Always restore stashed CR3 value (see paranoid_entry) */
21e94459 1703 RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
8a09317b 1704
4d732138
IM
1705 testl %ebx, %ebx /* swapgs needed? */
1706 jnz nmi_restore
ddeb8f21
AH
1707nmi_swapgs:
1708 SWAPGS_UNSAFE_STACK
1709nmi_restore:
502af0d7 1710 POP_REGS
0b22930e 1711
471ee483
AL
1712 /*
1713 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1714 * at the "iret" frame.
1715 */
1716 addq $6*8, %rsp
28696f43 1717
810bc075
AL
1718 /*
1719 * Clear "NMI executing". Set DF first so that we can easily
1720 * distinguish the remaining code between here and IRET from
929bacec
AL
1721 * the SYSCALL entry and exit paths.
1722 *
1723 * We arguably should just inspect RIP instead, but I (Andy) wrote
1724 * this code when I had the misapprehension that Xen PV supported
1725 * NMIs, and Xen PV would break that approach.
810bc075
AL
1726 */
1727 std
1728 movq $0, 5*8(%rsp) /* clear "NMI executing" */
0b22930e
AL
1729
1730 /*
929bacec
AL
1731 * iretq reads the "iret" frame and exits the NMI stack in a
1732 * single instruction. We are returning to kernel mode, so this
1733 * cannot result in a fault. Similarly, we don't need to worry
1734 * about espfix64 on the way back to kernel mode.
0b22930e 1735 */
929bacec 1736 iretq
6271fef0 1737SYM_CODE_END(asm_exc_nmi)
ddeb8f21 1738
dffb3f9d
AL
1739#ifndef CONFIG_IA32_EMULATION
1740/*
1741 * This handles SYSCALL from 32-bit code. There is no way to program
1742 * MSRs to fully disable 32-bit SYSCALL.
1743 */
bc7b11c0 1744SYM_CODE_START(ignore_sysret)
8c1f7558 1745 UNWIND_HINT_EMPTY
4d732138 1746 mov $-ENOSYS, %eax
b2b1d94c 1747 sysretl
bc7b11c0 1748SYM_CODE_END(ignore_sysret)
dffb3f9d 1749#endif
2deb4be2 1750
b9f6976b 1751.pushsection .text, "ax"
bc7b11c0 1752SYM_CODE_START(rewind_stack_do_exit)
8c1f7558 1753 UNWIND_HINT_FUNC
2deb4be2
AL
1754 /* Prevent any naive code from trying to unwind to our caller. */
1755 xorl %ebp, %ebp
1756
1757 movq PER_CPU_VAR(cpu_current_top_of_stack), %rax
8c1f7558 1758 leaq -PTREGS_SIZE(%rax), %rsp
f977df7b 1759 UNWIND_HINT_REGS
2deb4be2
AL
1760
1761 call do_exit
bc7b11c0 1762SYM_CODE_END(rewind_stack_do_exit)
b9f6976b 1763.popsection