x86/entry: Add IRQENTRY_IRQ macro
[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
1d3e53e8
AL
361.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
362#ifdef CONFIG_DEBUG_ENTRY
e17f8234
BO
363 pushq %rax
364 SAVE_FLAGS(CLBR_RAX)
365 testl $X86_EFLAGS_IF, %eax
1d3e53e8
AL
366 jz .Lokay_\@
367 ud2
368.Lokay_\@:
e17f8234 369 popq %rax
1d3e53e8
AL
370#endif
371.endm
372
373/*
374 * Enters the IRQ stack if we're not already using it. NMI-safe. Clobbers
375 * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
376 * Requires kernel GSBASE.
377 *
378 * The invariant is that, if irq_count != -1, then the IRQ stack is in use.
379 */
2ba64741 380.macro ENTER_IRQ_STACK regs=1 old_rsp save_ret=0
1d3e53e8 381 DEBUG_ENTRY_ASSERT_IRQS_OFF
2ba64741
DB
382
383 .if \save_ret
384 /*
385 * If save_ret is set, the original stack contains one additional
386 * entry -- the return address. Therefore, move the address one
387 * entry below %rsp to \old_rsp.
388 */
389 leaq 8(%rsp), \old_rsp
390 .else
1d3e53e8 391 movq %rsp, \old_rsp
2ba64741 392 .endif
8c1f7558
JP
393
394 .if \regs
395 UNWIND_HINT_REGS base=\old_rsp
396 .endif
397
1d3e53e8 398 incl PER_CPU_VAR(irq_count)
29955909 399 jnz .Lirq_stack_push_old_rsp_\@
1d3e53e8
AL
400
401 /*
402 * Right now, if we just incremented irq_count to zero, we've
403 * claimed the IRQ stack but we haven't switched to it yet.
404 *
405 * If anything is added that can interrupt us here without using IST,
406 * it must be *extremely* careful to limit its stack usage. This
407 * could include kprobes and a hypothetical future IST-less #DB
408 * handler.
29955909
AL
409 *
410 * The OOPS unwinder relies on the word at the top of the IRQ
411 * stack linking back to the previous RSP for the entire time we're
412 * on the IRQ stack. For this to work reliably, we need to write
413 * it before we actually move ourselves to the IRQ stack.
414 */
415
e6401c13 416 movq \old_rsp, PER_CPU_VAR(irq_stack_backing_store + IRQ_STACK_SIZE - 8)
758a2e31 417 movq PER_CPU_VAR(hardirq_stack_ptr), %rsp
29955909
AL
418
419#ifdef CONFIG_DEBUG_ENTRY
420 /*
421 * If the first movq above becomes wrong due to IRQ stack layout
422 * changes, the only way we'll notice is if we try to unwind right
423 * here. Assert that we set up the stack right to catch this type
424 * of bug quickly.
1d3e53e8 425 */
29955909
AL
426 cmpq -8(%rsp), \old_rsp
427 je .Lirq_stack_okay\@
428 ud2
429 .Lirq_stack_okay\@:
430#endif
1d3e53e8 431
29955909 432.Lirq_stack_push_old_rsp_\@:
1d3e53e8 433 pushq \old_rsp
8c1f7558
JP
434
435 .if \regs
436 UNWIND_HINT_REGS indirect=1
437 .endif
2ba64741
DB
438
439 .if \save_ret
440 /*
441 * Push the return address to the stack. This return address can
442 * be found at the "real" original RSP, which was offset by 8 at
443 * the beginning of this macro.
444 */
445 pushq -8(\old_rsp)
446 .endif
1d3e53e8
AL
447.endm
448
449/*
450 * Undoes ENTER_IRQ_STACK.
451 */
8c1f7558 452.macro LEAVE_IRQ_STACK regs=1
1d3e53e8
AL
453 DEBUG_ENTRY_ASSERT_IRQS_OFF
454 /* We need to be off the IRQ stack before decrementing irq_count. */
455 popq %rsp
456
8c1f7558
JP
457 .if \regs
458 UNWIND_HINT_REGS
459 .endif
460
1d3e53e8
AL
461 /*
462 * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming
463 * the irq stack but we're not on it.
464 */
465
466 decl PER_CPU_VAR(irq_count)
467.endm
468
cfa82a00
TG
469/**
470 * idtentry_body - Macro to emit code calling the C function
cfa82a00
TG
471 * @cfunc: C function to be called
472 * @has_error_code: Hardware pushed error code on stack
473 */
e2dcb5f1 474.macro idtentry_body cfunc has_error_code:req
cfa82a00
TG
475
476 call error_entry
477 UNWIND_HINT_REGS
478
cfa82a00
TG
479 movq %rsp, %rdi /* pt_regs pointer into 1st argument*/
480
481 .if \has_error_code == 1
482 movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
483 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
cfa82a00
TG
484 .endif
485
cfa82a00
TG
486 call \cfunc
487
424c7d0a 488 jmp error_return
cfa82a00
TG
489.endm
490
491/**
492 * idtentry - Macro to generate entry stubs for simple IDT entries
493 * @vector: Vector number
494 * @asmsym: ASM symbol for the entry point
495 * @cfunc: C function to be called
496 * @has_error_code: Hardware pushed error code on stack
497 *
498 * The macro emits code to set up the kernel context for straight forward
499 * and simple IDT entries. No IST stack, no paranoid entry checks.
500 */
e2dcb5f1 501.macro idtentry vector asmsym cfunc has_error_code:req
cfa82a00
TG
502SYM_CODE_START(\asmsym)
503 UNWIND_HINT_IRET_REGS offset=\has_error_code*8
504 ASM_CLAC
505
506 .if \has_error_code == 0
507 pushq $-1 /* ORIG_RAX: no syscall to restart */
508 .endif
509
510 .if \vector == X86_TRAP_BP
511 /*
512 * If coming from kernel space, create a 6-word gap to allow the
513 * int3 handler to emulate a call instruction.
514 */
515 testb $3, CS-ORIG_RAX(%rsp)
516 jnz .Lfrom_usermode_no_gap_\@
517 .rept 6
518 pushq 5*8(%rsp)
519 .endr
520 UNWIND_HINT_IRET_REGS offset=8
521.Lfrom_usermode_no_gap_\@:
522 .endif
523
e2dcb5f1 524 idtentry_body \cfunc \has_error_code
cfa82a00
TG
525
526_ASM_NOKPROBE(\asmsym)
527SYM_CODE_END(\asmsym)
528.endm
529
0bf7c314
TG
530/*
531 * Interrupt entry/exit.
532 *
533 + The interrupt stubs push (vector) onto the stack, which is the error_code
534 * position of idtentry exceptions, and jump to one of the two idtentry points
535 * (common/spurious).
536 *
537 * common_interrupt is a hotpath, align it to a cache line
538 */
539.macro idtentry_irq vector cfunc
540 .p2align CONFIG_X86_L1_CACHE_SHIFT
541 idtentry \vector asm_\cfunc \cfunc has_error_code=1
542.endm
543
cfa82a00
TG
544/*
545 * MCE and DB exceptions
546 */
547#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss_rw) + (TSS_ist + (x) * 8)
548
549/**
550 * idtentry_mce_db - Macro to generate entry stubs for #MC and #DB
551 * @vector: Vector number
552 * @asmsym: ASM symbol for the entry point
553 * @cfunc: C function to be called
554 *
555 * The macro emits code to set up the kernel context for #MC and #DB
556 *
557 * If the entry comes from user space it uses the normal entry path
558 * including the return to user space work and preemption checks on
559 * exit.
560 *
561 * If hits in kernel mode then it needs to go through the paranoid
562 * entry as the exception can hit any random state. No preemption
563 * check on exit to keep the paranoid path simple.
564 *
565 * If the trap is #DB then the interrupt stack entry in the IST is
566 * moved to the second stack, so a potential recursion will have a
567 * fresh IST.
568 */
569.macro idtentry_mce_db vector asmsym cfunc
570SYM_CODE_START(\asmsym)
571 UNWIND_HINT_IRET_REGS
572 ASM_CLAC
573
574 pushq $-1 /* ORIG_RAX: no syscall to restart */
575
576 /*
577 * If the entry is from userspace, switch stacks and treat it as
578 * a normal entry.
579 */
580 testb $3, CS-ORIG_RAX(%rsp)
581 jnz .Lfrom_usermode_switch_stack_\@
582
583 /*
584 * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX.
585 * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS
586 */
587 call paranoid_entry
588
589 UNWIND_HINT_REGS
590
591 .if \vector == X86_TRAP_DB
592 TRACE_IRQS_OFF_DEBUG
593 .else
594 TRACE_IRQS_OFF
595 .endif
596
597 movq %rsp, %rdi /* pt_regs pointer */
cfa82a00
TG
598
599 .if \vector == X86_TRAP_DB
600 subq $DB_STACK_OFFSET, CPU_TSS_IST(IST_INDEX_DB)
601 .endif
602
603 call \cfunc
604
605 .if \vector == X86_TRAP_DB
606 addq $DB_STACK_OFFSET, CPU_TSS_IST(IST_INDEX_DB)
607 .endif
608
609 jmp paranoid_exit
610
611 /* Switch to the regular task stack and use the noist entry point */
612.Lfrom_usermode_switch_stack_\@:
e2dcb5f1 613 idtentry_body noist_\cfunc, has_error_code=0
cfa82a00
TG
614
615_ASM_NOKPROBE(\asmsym)
616SYM_CODE_END(\asmsym)
617.endm
618
619/*
620 * Double fault entry. Straight paranoid. No checks from which context
621 * this comes because for the espfix induced #DF this would do the wrong
622 * thing.
623 */
624.macro idtentry_df vector asmsym cfunc
625SYM_CODE_START(\asmsym)
626 UNWIND_HINT_IRET_REGS offset=8
627 ASM_CLAC
628
629 /*
630 * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX.
631 * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS
632 */
633 call paranoid_entry
634 UNWIND_HINT_REGS
635
cfa82a00
TG
636 movq %rsp, %rdi /* pt_regs pointer into first argument */
637 movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
638 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
cfa82a00
TG
639 call \cfunc
640
641 jmp paranoid_exit
642
643_ASM_NOKPROBE(\asmsym)
644SYM_CODE_END(\asmsym)
645.endm
646
53aaf262
TG
647/*
648 * Include the defines which emit the idt entries which are shared
649 * shared between 32 and 64 bit.
650 */
651#include <asm/idtentry.h>
652
d99015b1 653/*
f3d415ea 654 * Interrupt entry helper function.
d99015b1 655 *
f3d415ea
DB
656 * Entry runs with interrupts off. Stack layout at entry:
657 * +----------------------------------------------------+
658 * | regs->ss |
659 * | regs->rsp |
660 * | regs->eflags |
661 * | regs->cs |
662 * | regs->ip |
663 * +----------------------------------------------------+
664 * | regs->orig_ax = ~(interrupt number) |
665 * +----------------------------------------------------+
666 * | return address |
667 * +----------------------------------------------------+
d99015b1 668 */
bc7b11c0 669SYM_CODE_START(interrupt_entry)
81b67439 670 UNWIND_HINT_IRET_REGS offset=16
f3d415ea 671 ASM_CLAC
f6f64681 672 cld
7f2590a1 673
f3d415ea 674 testb $3, CS-ORIG_RAX+8(%rsp)
7f2590a1
AL
675 jz 1f
676 SWAPGS
18ec54fd 677 FENCE_SWAPGS_USER_ENTRY
f3d415ea
DB
678 /*
679 * Switch to the thread stack. The IRET frame and orig_ax are
680 * on the stack, as well as the return address. RDI..R12 are
681 * not (yet) on the stack and space has not (yet) been
682 * allocated for them.
683 */
90a6acc4 684 pushq %rdi
f3d415ea 685
90a6acc4
DB
686 /* Need to switch before accessing the thread stack. */
687 SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi
688 movq %rsp, %rdi
689 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
f3d415ea
DB
690
691 /*
692 * We have RDI, return address, and orig_ax on the stack on
693 * top of the IRET frame. That means offset=24
694 */
695 UNWIND_HINT_IRET_REGS base=%rdi offset=24
90a6acc4
DB
696
697 pushq 7*8(%rdi) /* regs->ss */
698 pushq 6*8(%rdi) /* regs->rsp */
699 pushq 5*8(%rdi) /* regs->eflags */
700 pushq 4*8(%rdi) /* regs->cs */
701 pushq 3*8(%rdi) /* regs->ip */
81b67439 702 UNWIND_HINT_IRET_REGS
90a6acc4
DB
703 pushq 2*8(%rdi) /* regs->orig_ax */
704 pushq 8(%rdi) /* return address */
90a6acc4
DB
705
706 movq (%rdi), %rdi
64dbc122 707 jmp 2f
7f2590a1 7081:
18ec54fd
JP
709 FENCE_SWAPGS_KERNEL_ENTRY
7102:
0e34d226
DB
711 PUSH_AND_CLEAR_REGS save_ret=1
712 ENCODE_FRAME_POINTER 8
76f5df43 713
2ba64741 714 testb $3, CS+8(%rsp)
dde74f2e 715 jz 1f
02bc7768
AL
716
717 /*
7f2590a1
AL
718 * IRQ from user mode.
719 *
f1075053
AL
720 * We need to tell lockdep that IRQs are off. We can't do this until
721 * we fix gsbase, and we should do it before enter_from_user_mode
f3d415ea 722 * (which can take locks). Since TRACE_IRQS_OFF is idempotent,
f1075053
AL
723 * the simplest way to handle it is to just call it twice if
724 * we enter from user mode. There's no reason to optimize this since
725 * TRACE_IRQS_OFF is a no-op if lockdep is off.
726 */
727 TRACE_IRQS_OFF
728
478dc89c 729 CALL_enter_from_user_mode
02bc7768 730
76f5df43 7311:
2ba64741 732 ENTER_IRQ_STACK old_rsp=%rdi save_ret=1
f6f64681
DV
733 /* We entered an interrupt context - irqs are off: */
734 TRACE_IRQS_OFF
735
2ba64741 736 ret
bc7b11c0 737SYM_CODE_END(interrupt_entry)
a50480cb 738_ASM_NOKPROBE(interrupt_entry)
2ba64741 739
f3d415ea
DB
740
741/* Interrupt entry/exit. */
1da177e4 742
f8a8fe61 743/*
633260fa 744 * The interrupt stubs push vector onto the stack and
f8a8fe61
TG
745 * then jump to common_spurious/interrupt.
746 */
cc66936e 747SYM_CODE_START_LOCAL(common_spurious)
f8a8fe61
TG
748 call interrupt_entry
749 UNWIND_HINT_REGS indirect=1
633260fa
TG
750 movq ORIG_RAX(%rdi), %rsi /* get vector from stack */
751 movq $-1, ORIG_RAX(%rdi) /* no syscall to restart */
f8a8fe61
TG
752 call smp_spurious_interrupt /* rdi points to pt_regs */
753 jmp ret_from_intr
cc66936e 754SYM_CODE_END(common_spurious)
f8a8fe61
TG
755_ASM_NOKPROBE(common_spurious)
756
757/* common_interrupt is a hotpath. Align it */
939b7871 758 .p2align CONFIG_X86_L1_CACHE_SHIFT
cc66936e 759SYM_CODE_START_LOCAL(common_interrupt)
3aa99fc3
DB
760 call interrupt_entry
761 UNWIND_HINT_REGS indirect=1
633260fa
TG
762 movq ORIG_RAX(%rdi), %rsi /* get vector from stack */
763 movq $-1, ORIG_RAX(%rdi) /* no syscall to restart */
764 call do_IRQ /* rdi points to pt_regs */
34061f13 765 /* 0(%rsp): old RSP */
7effaa88 766ret_from_intr:
2140a994 767 DISABLE_INTERRUPTS(CLBR_ANY)
2601e64d 768 TRACE_IRQS_OFF
625dbc3b 769
1d3e53e8 770 LEAVE_IRQ_STACK
625dbc3b 771
03335e95 772 testb $3, CS(%rsp)
dde74f2e 773 jz retint_kernel
4d732138 774
02bc7768 775 /* Interrupt came from user space */
30a2441c 776.Lretint_user:
02bc7768
AL
777 mov %rsp,%rdi
778 call prepare_exit_to_usermode
26c4ef9c 779
26ba4e57 780SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
26c4ef9c
AL
781#ifdef CONFIG_DEBUG_ENTRY
782 /* Assert that pt_regs indicates user mode. */
1e4c4f61 783 testb $3, CS(%rsp)
26c4ef9c
AL
784 jnz 1f
785 ud2
7861:
787#endif
502af0d7 788 POP_REGS pop_rdi=0
3e3b9293
AL
789
790 /*
791 * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS.
792 * Save old stack pointer and switch to trampoline stack.
793 */
794 movq %rsp, %rdi
c482feef 795 movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
1fb14363 796 UNWIND_HINT_EMPTY
3e3b9293
AL
797
798 /* Copy the IRET frame to the trampoline stack. */
799 pushq 6*8(%rdi) /* SS */
800 pushq 5*8(%rdi) /* RSP */
801 pushq 4*8(%rdi) /* EFLAGS */
802 pushq 3*8(%rdi) /* CS */
803 pushq 2*8(%rdi) /* RIP */
804
805 /* Push user RDI on the trampoline stack. */
806 pushq (%rdi)
807
808 /*
809 * We are on the trampoline stack. All regs except RDI are live.
810 * We can do future final exit work right here.
811 */
afaef01c 812 STACKLEAK_ERASE_NOCLOBBER
3e3b9293 813
6fd166aa 814 SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
8a09317b 815
3e3b9293
AL
816 /* Restore RDI. */
817 popq %rdi
818 SWAPGS
26c4ef9c
AL
819 INTERRUPT_RETURN
820
2601e64d 821
627276cb 822/* Returning to kernel space */
6ba71b76 823retint_kernel:
48593975 824#ifdef CONFIG_PREEMPTION
627276cb
DV
825 /* Interrupts are off */
826 /* Check if we need preemption */
6709812f 827 btl $9, EFLAGS(%rsp) /* were interrupts off? */
6ba71b76 828 jnc 1f
b5b447b6 829 cmpl $0, PER_CPU_VAR(__preempt_count)
36acef25 830 jnz 1f
627276cb 831 call preempt_schedule_irq
6ba71b76 8321:
627276cb 833#endif
2601e64d
IM
834 /*
835 * The iretq could re-enable interrupts:
836 */
837 TRACE_IRQS_IRETQ
fffbb5dc 838
26ba4e57 839SYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)
26c4ef9c
AL
840#ifdef CONFIG_DEBUG_ENTRY
841 /* Assert that pt_regs indicates kernel mode. */
1e4c4f61 842 testb $3, CS(%rsp)
26c4ef9c
AL
843 jz 1f
844 ud2
8451:
846#endif
502af0d7 847 POP_REGS
e872045b 848 addq $8, %rsp /* skip regs->orig_ax */
10bcc80e
MD
849 /*
850 * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
851 * when returning from IPI handler.
852 */
7209a75d
AL
853 INTERRUPT_RETURN
854
cc66936e 855SYM_INNER_LABEL_ALIGN(native_iret, SYM_L_GLOBAL)
8c1f7558 856 UNWIND_HINT_IRET_REGS
3891a04a
PA
857 /*
858 * Are we returning to a stack segment from the LDT? Note: in
859 * 64-bit mode SS:RSP on the exception stack is always valid.
860 */
34273f41 861#ifdef CONFIG_X86_ESPFIX64
4d732138
IM
862 testb $4, (SS-RIP)(%rsp)
863 jnz native_irq_return_ldt
34273f41 864#endif
3891a04a 865
cc66936e 866SYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL)
b645af2d
AL
867 /*
868 * This may fault. Non-paranoid faults on return to userspace are
869 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
c29c775a 870 * Double-faults due to espfix64 are handled in exc_double_fault.
b645af2d
AL
871 * Other faults here are fatal.
872 */
1da177e4 873 iretq
3701d863 874
34273f41 875#ifdef CONFIG_X86_ESPFIX64
7209a75d 876native_irq_return_ldt:
85063fac
AL
877 /*
878 * We are running with user GSBASE. All GPRs contain their user
879 * values. We have a percpu ESPFIX stack that is eight slots
880 * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom
881 * of the ESPFIX stack.
882 *
883 * We clobber RAX and RDI in this code. We stash RDI on the
884 * normal stack and RAX on the ESPFIX stack.
885 *
886 * The ESPFIX stack layout we set up looks like this:
887 *
888 * --- top of ESPFIX stack ---
889 * SS
890 * RSP
891 * RFLAGS
892 * CS
893 * RIP <-- RSP points here when we're done
894 * RAX <-- espfix_waddr points here
895 * --- bottom of ESPFIX stack ---
896 */
897
898 pushq %rdi /* Stash user RDI */
8a09317b
DH
899 SWAPGS /* to kernel GS */
900 SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi /* to kernel CR3 */
901
4d732138 902 movq PER_CPU_VAR(espfix_waddr), %rdi
85063fac
AL
903 movq %rax, (0*8)(%rdi) /* user RAX */
904 movq (1*8)(%rsp), %rax /* user RIP */
4d732138 905 movq %rax, (1*8)(%rdi)
85063fac 906 movq (2*8)(%rsp), %rax /* user CS */
4d732138 907 movq %rax, (2*8)(%rdi)
85063fac 908 movq (3*8)(%rsp), %rax /* user RFLAGS */
4d732138 909 movq %rax, (3*8)(%rdi)
85063fac 910 movq (5*8)(%rsp), %rax /* user SS */
4d732138 911 movq %rax, (5*8)(%rdi)
85063fac 912 movq (4*8)(%rsp), %rax /* user RSP */
4d732138 913 movq %rax, (4*8)(%rdi)
85063fac
AL
914 /* Now RAX == RSP. */
915
916 andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */
85063fac
AL
917
918 /*
919 * espfix_stack[31:16] == 0. The page tables are set up such that
920 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
921 * espfix_waddr for any X. That is, there are 65536 RO aliases of
922 * the same page. Set up RSP so that RSP[31:16] contains the
923 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
924 * still points to an RO alias of the ESPFIX stack.
925 */
4d732138 926 orq PER_CPU_VAR(espfix_stack), %rax
8a09317b 927
6fd166aa 928 SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
8a09317b
DH
929 SWAPGS /* to user GS */
930 popq %rdi /* Restore user RDI */
931
4d732138 932 movq %rax, %rsp
8c1f7558 933 UNWIND_HINT_IRET_REGS offset=8
85063fac
AL
934
935 /*
936 * At this point, we cannot write to the stack any more, but we can
937 * still read.
938 */
939 popq %rax /* Restore user RAX */
940
941 /*
942 * RSP now points to an ordinary IRET frame, except that the page
943 * is read-only and RSP[31:16] are preloaded with the userspace
944 * values. We can now IRET back to userspace.
945 */
4d732138 946 jmp native_irq_return_iret
34273f41 947#endif
cc66936e 948SYM_CODE_END(common_interrupt)
a50480cb 949_ASM_NOKPROBE(common_interrupt)
3891a04a 950
1da177e4
LT
951/*
952 * APIC interrupts.
0bd7b798 953 */
cf910e83 954.macro apicinterrupt3 num sym do_sym
bc7b11c0 955SYM_CODE_START(\sym)
8c1f7558 956 UNWIND_HINT_IRET_REGS
4d732138 957 pushq $~(\num)
3aa99fc3
DB
958 call interrupt_entry
959 UNWIND_HINT_REGS indirect=1
960 call \do_sym /* rdi points to pt_regs */
4d732138 961 jmp ret_from_intr
bc7b11c0 962SYM_CODE_END(\sym)
a50480cb 963_ASM_NOKPROBE(\sym)
322648d1 964.endm
1da177e4 965
469f0023 966/* Make sure APIC interrupt handlers end up in the irqentry section: */
229a7186
MH
967#define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
968#define POP_SECTION_IRQENTRY .popsection
469f0023 969
cf910e83 970.macro apicinterrupt num sym do_sym
469f0023 971PUSH_SECTION_IRQENTRY
cf910e83 972apicinterrupt3 \num \sym \do_sym
469f0023 973POP_SECTION_IRQENTRY
cf910e83
SA
974.endm
975
322648d1 976#ifdef CONFIG_SMP
4d732138
IM
977apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
978apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
322648d1 979#endif
1da177e4 980
03b48632 981#ifdef CONFIG_X86_UV
4d732138 982apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
03b48632 983#endif
4d732138
IM
984
985apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
986apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
89b831ef 987
d78f2664 988#ifdef CONFIG_HAVE_KVM
4d732138
IM
989apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
990apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
210f84b0 991apicinterrupt3 POSTED_INTR_NESTED_VECTOR kvm_posted_intr_nested_ipi smp_kvm_posted_intr_nested_ipi
d78f2664
YZ
992#endif
993
33e5ff63 994#ifdef CONFIG_X86_MCE_THRESHOLD
4d732138 995apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
33e5ff63
SA
996#endif
997
24fd78a8 998#ifdef CONFIG_X86_MCE_AMD
4d732138 999apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
24fd78a8
AG
1000#endif
1001
33e5ff63 1002#ifdef CONFIG_X86_THERMAL_VECTOR
4d732138 1003apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
33e5ff63 1004#endif
1812924b 1005
322648d1 1006#ifdef CONFIG_SMP
4d732138
IM
1007apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
1008apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
1009apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
322648d1 1010#endif
1da177e4 1011
4d732138 1012apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
633260fa 1013apicinterrupt SPURIOUS_APIC_VECTOR spurious_apic_interrupt smp_spurious_apic_interrupt
0bd7b798 1014
e360adbe 1015#ifdef CONFIG_IRQ_WORK
4d732138 1016apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
241771ef
IM
1017#endif
1018
b9f6976b
TG
1019/*
1020 * Reload gs selector with exception handling
1021 * edi: new selector
1022 *
1023 * Is in entry.text as it shouldn't be instrumented.
1024 */
410367e3 1025SYM_FUNC_START(asm_load_gs_index)
8c1f7558 1026 FRAME_BEGIN
c9317202 1027 swapgs
42c748bb 1028.Lgs_change:
4d732138 1029 movl %edi, %gs
96e5d28a 10302: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
c9317202 1031 swapgs
8c1f7558 1032 FRAME_END
9f1e87ea 1033 ret
410367e3
TG
1034SYM_FUNC_END(asm_load_gs_index)
1035EXPORT_SYMBOL(asm_load_gs_index)
0bd7b798 1036
98ededb6 1037 _ASM_EXTABLE(.Lgs_change, .Lbad_gs)
4d732138 1038 .section .fixup, "ax"
1da177e4 1039 /* running with kernelgs */
ef77e688 1040SYM_CODE_START_LOCAL_NOALIGN(.Lbad_gs)
c9317202 1041 swapgs /* switch back to user gs */
b038c842
AL
1042.macro ZAP_GS
1043 /* This can't be a string because the preprocessor needs to see it. */
1044 movl $__USER_DS, %eax
1045 movl %eax, %gs
1046.endm
1047 ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
4d732138
IM
1048 xorl %eax, %eax
1049 movl %eax, %gs
1050 jmp 2b
ef77e688 1051SYM_CODE_END(.Lbad_gs)
9f1e87ea 1052 .previous
0bd7b798 1053
931b9414
TG
1054/*
1055 * rdi: New stack pointer points to the top word of the stack
1056 * rsi: Function pointer
1057 * rdx: Function argument (can be NULL if none)
1058 */
1059SYM_FUNC_START(asm_call_on_stack)
1060 /*
1061 * Save the frame pointer unconditionally. This allows the ORC
1062 * unwinder to handle the stack switch.
1063 */
1064 pushq %rbp
1065 mov %rsp, %rbp
1066
1067 /*
1068 * The unwinder relies on the word at the top of the new stack
1069 * page linking back to the previous RSP.
1070 */
1071 mov %rsp, (%rdi)
1072 mov %rdi, %rsp
1073 /* Move the argument to the right place */
1074 mov %rdx, %rdi
1075
10761:
1077 .pushsection .discard.instr_begin
1078 .long 1b - .
1079 .popsection
1080
1081 CALL_NOSPEC rsi
1082
10832:
1084 .pushsection .discard.instr_end
1085 .long 2b - .
1086 .popsection
1087
1088 /* Restore the previous stack pointer from RBP. */
1089 leaveq
1090 ret
1091SYM_FUNC_END(asm_call_on_stack)
1092
28c11b0f 1093#ifdef CONFIG_XEN_PV
3d75e1b8 1094/*
9f1e87ea
CG
1095 * A note on the "critical region" in our callback handler.
1096 * We want to avoid stacking callback handlers due to events occurring
1097 * during handling of the last event. To do this, we keep events disabled
1098 * until we've done all processing. HOWEVER, we must enable events before
1099 * popping the stack frame (can't be done atomically) and so it would still
1100 * be possible to get enough handler activations to overflow the stack.
1101 * Although unlikely, bugs of that kind are hard to track down, so we'd
1102 * like to avoid the possibility.
1103 * So, on entry to the handler we detect whether we interrupted an
1104 * existing activation in its critical region -- if so, we pop the current
1105 * activation and restart the handler using the previous one.
2f6474e4
TG
1106 *
1107 * C calling convention: exc_xen_hypervisor_callback(struct *pt_regs)
9f1e87ea 1108 */
2f6474e4 1109SYM_CODE_START_LOCAL(exc_xen_hypervisor_callback)
4d732138 1110
9f1e87ea
CG
1111/*
1112 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1113 * see the correct pointer to the pt_regs
1114 */
8c1f7558 1115 UNWIND_HINT_FUNC
4d732138 1116 movq %rdi, %rsp /* we don't return, adjust the stack frame */
8c1f7558 1117 UNWIND_HINT_REGS
1d3e53e8 1118
2f6474e4 1119 call xen_pv_evtchn_do_upcall
1d3e53e8 1120
2f6474e4
TG
1121 jmp error_return
1122SYM_CODE_END(exc_xen_hypervisor_callback)
3d75e1b8
JF
1123
1124/*
9f1e87ea
CG
1125 * Hypervisor uses this for application faults while it executes.
1126 * We get here for two reasons:
1127 * 1. Fault while reloading DS, ES, FS or GS
1128 * 2. Fault while executing IRET
1129 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1130 * registers that could be reloaded and zeroed the others.
1131 * Category 2 we fix up by killing the current process. We cannot use the
1132 * normal Linux return path in this case because if we use the IRET hypercall
1133 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1134 * We distinguish between categories by comparing each saved segment register
1135 * with its current contents: any discrepancy means we in category 1.
1136 */
bc7b11c0 1137SYM_CODE_START(xen_failsafe_callback)
8c1f7558 1138 UNWIND_HINT_EMPTY
4d732138
IM
1139 movl %ds, %ecx
1140 cmpw %cx, 0x10(%rsp)
1141 jne 1f
1142 movl %es, %ecx
1143 cmpw %cx, 0x18(%rsp)
1144 jne 1f
1145 movl %fs, %ecx
1146 cmpw %cx, 0x20(%rsp)
1147 jne 1f
1148 movl %gs, %ecx
1149 cmpw %cx, 0x28(%rsp)
1150 jne 1f
3d75e1b8 1151 /* All segments match their saved values => Category 2 (Bad IRET). */
4d732138
IM
1152 movq (%rsp), %rcx
1153 movq 8(%rsp), %r11
1154 addq $0x30, %rsp
1155 pushq $0 /* RIP */
8c1f7558 1156 UNWIND_HINT_IRET_REGS offset=8
be4c11af 1157 jmp asm_exc_general_protection
3d75e1b8 11581: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
4d732138
IM
1159 movq (%rsp), %rcx
1160 movq 8(%rsp), %r11
1161 addq $0x30, %rsp
8c1f7558 1162 UNWIND_HINT_IRET_REGS
4d732138 1163 pushq $-1 /* orig_ax = -1 => not a system call */
3f01daec 1164 PUSH_AND_CLEAR_REGS
946c1911 1165 ENCODE_FRAME_POINTER
e88d9741 1166 jmp error_return
bc7b11c0 1167SYM_CODE_END(xen_failsafe_callback)
28c11b0f 1168#endif /* CONFIG_XEN_PV */
3d75e1b8 1169
28c11b0f 1170#ifdef CONFIG_XEN_PVHVM
cf910e83 1171apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
38e20b07 1172 xen_hvm_callback_vector xen_evtchn_do_upcall
28c11b0f 1173#endif
38e20b07 1174
ddeb8f21 1175
bc2b0331 1176#if IS_ENABLED(CONFIG_HYPERV)
cf910e83 1177apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
bc2b0331 1178 hyperv_callback_vector hyperv_vector_handler
93286261
VK
1179
1180apicinterrupt3 HYPERV_REENLIGHTENMENT_VECTOR \
1181 hyperv_reenlightenment_vector hyperv_reenlightenment_intr
248e742a
MK
1182
1183apicinterrupt3 HYPERV_STIMER0_VECTOR \
1184 hv_stimer0_callback_vector hv_stimer0_vector_handler
bc2b0331
S
1185#endif /* CONFIG_HYPERV */
1186
498ad393
ZY
1187#if IS_ENABLED(CONFIG_ACRN_GUEST)
1188apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1189 acrn_hv_callback_vector acrn_hv_vector_handler
1190#endif
1191
ebfc453e 1192/*
9e809d15 1193 * Save all registers in pt_regs, and switch gs if needed.
ebfc453e
DV
1194 * Use slow, but surefire "are we in kernel?" check.
1195 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1196 */
ef1e0315 1197SYM_CODE_START_LOCAL(paranoid_entry)
8c1f7558 1198 UNWIND_HINT_FUNC
1eeb207f 1199 cld
9e809d15
DB
1200 PUSH_AND_CLEAR_REGS save_ret=1
1201 ENCODE_FRAME_POINTER 8
4d732138
IM
1202 movl $1, %ebx
1203 movl $MSR_GS_BASE, %ecx
1eeb207f 1204 rdmsr
4d732138
IM
1205 testl %edx, %edx
1206 js 1f /* negative -> in kernel */
1eeb207f 1207 SWAPGS
4d732138 1208 xorl %ebx, %ebx
8a09317b
DH
1209
12101:
16561f27
DH
1211 /*
1212 * Always stash CR3 in %r14. This value will be restored,
ae852495
AL
1213 * verbatim, at exit. Needed if paranoid_entry interrupted
1214 * another entry that already switched to the user CR3 value
1215 * but has not yet returned to userspace.
16561f27
DH
1216 *
1217 * This is also why CS (stashed in the "iret frame" by the
1218 * hardware at entry) can not be used: this may be a return
ae852495 1219 * to kernel code, but with a user CR3 value.
16561f27 1220 */
8a09317b
DH
1221 SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
1222
18ec54fd
JP
1223 /*
1224 * The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an
1225 * unconditional CR3 write, even in the PTI case. So do an lfence
1226 * to prevent GS speculation, regardless of whether PTI is enabled.
1227 */
1228 FENCE_SWAPGS_KERNEL_ENTRY
1229
8a09317b 1230 ret
ef1e0315 1231SYM_CODE_END(paranoid_entry)
ddeb8f21 1232
ebfc453e
DV
1233/*
1234 * "Paranoid" exit path from exception stack. This is invoked
1235 * only on return from non-NMI IST interrupts that came
1236 * from kernel space.
1237 *
1238 * We may be returning to very strange contexts (e.g. very early
1239 * in syscall entry), so checking for preemption here would
1240 * be complicated. Fortunately, we there's no good reason
1241 * to try to handle preemption here.
4d732138
IM
1242 *
1243 * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
ebfc453e 1244 */
ef1e0315 1245SYM_CODE_START_LOCAL(paranoid_exit)
8c1f7558 1246 UNWIND_HINT_REGS
2140a994 1247 DISABLE_INTERRUPTS(CLBR_ANY)
5963e317 1248 TRACE_IRQS_OFF_DEBUG
4d732138 1249 testl %ebx, %ebx /* swapgs needed? */
e5317832 1250 jnz .Lparanoid_exit_no_swapgs
f2db9382 1251 TRACE_IRQS_IRETQ
16561f27 1252 /* Always restore stashed CR3 value (see paranoid_entry) */
21e94459 1253 RESTORE_CR3 scratch_reg=%rbx save_reg=%r14
ddeb8f21 1254 SWAPGS_UNSAFE_STACK
45c08383 1255 jmp restore_regs_and_return_to_kernel
e5317832 1256.Lparanoid_exit_no_swapgs:
f2db9382 1257 TRACE_IRQS_IRETQ_DEBUG
16561f27 1258 /* Always restore stashed CR3 value (see paranoid_entry) */
e4865757 1259 RESTORE_CR3 scratch_reg=%rbx save_reg=%r14
e5317832 1260 jmp restore_regs_and_return_to_kernel
ef1e0315 1261SYM_CODE_END(paranoid_exit)
ddeb8f21
AH
1262
1263/*
9e809d15 1264 * Save all registers in pt_regs, and switch GS if needed.
ddeb8f21 1265 */
ef1e0315 1266SYM_CODE_START_LOCAL(error_entry)
9e809d15 1267 UNWIND_HINT_FUNC
ddeb8f21 1268 cld
9e809d15
DB
1269 PUSH_AND_CLEAR_REGS save_ret=1
1270 ENCODE_FRAME_POINTER 8
03335e95 1271 testb $3, CS+8(%rsp)
cb6f64ed 1272 jz .Lerror_kernelspace
539f5113 1273
cb6f64ed
AL
1274 /*
1275 * We entered from user mode or we're pretending to have entered
1276 * from user mode due to an IRET fault.
1277 */
ddeb8f21 1278 SWAPGS
18ec54fd 1279 FENCE_SWAPGS_USER_ENTRY
8a09317b
DH
1280 /* We have user CR3. Change to kernel CR3. */
1281 SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
539f5113 1282
cb6f64ed 1283.Lerror_entry_from_usermode_after_swapgs:
7f2590a1
AL
1284 /* Put us onto the real thread stack. */
1285 popq %r12 /* save return addr in %12 */
1286 movq %rsp, %rdi /* arg0 = pt_regs pointer */
1287 call sync_regs
1288 movq %rax, %rsp /* switch stack */
1289 ENCODE_FRAME_POINTER
1290 pushq %r12
f1075053 1291 ret
02bc7768 1292
18ec54fd
JP
1293.Lerror_entry_done_lfence:
1294 FENCE_SWAPGS_KERNEL_ENTRY
cb6f64ed 1295.Lerror_entry_done:
ddeb8f21 1296 ret
ddeb8f21 1297
ebfc453e
DV
1298 /*
1299 * There are two places in the kernel that can potentially fault with
1300 * usergs. Handle them here. B stepping K8s sometimes report a
1301 * truncated RIP for IRET exceptions returning to compat mode. Check
1302 * for these here too.
1303 */
cb6f64ed 1304.Lerror_kernelspace:
4d732138
IM
1305 leaq native_irq_return_iret(%rip), %rcx
1306 cmpq %rcx, RIP+8(%rsp)
cb6f64ed 1307 je .Lerror_bad_iret
4d732138
IM
1308 movl %ecx, %eax /* zero extend */
1309 cmpq %rax, RIP+8(%rsp)
cb6f64ed 1310 je .Lbstep_iret
42c748bb 1311 cmpq $.Lgs_change, RIP+8(%rsp)
18ec54fd 1312 jne .Lerror_entry_done_lfence
539f5113
AL
1313
1314 /*
42c748bb 1315 * hack: .Lgs_change can fail with user gsbase. If this happens, fix up
539f5113 1316 * gsbase and proceed. We'll fix up the exception and land in
42c748bb 1317 * .Lgs_change's error handler with kernel gsbase.
539f5113 1318 */
2fa5f04f 1319 SWAPGS
18ec54fd 1320 FENCE_SWAPGS_USER_ENTRY
2fa5f04f 1321 jmp .Lerror_entry_done
ae24ffe5 1322
cb6f64ed 1323.Lbstep_iret:
ae24ffe5 1324 /* Fix truncated RIP */
4d732138 1325 movq %rcx, RIP+8(%rsp)
b645af2d
AL
1326 /* fall through */
1327
cb6f64ed 1328.Lerror_bad_iret:
539f5113 1329 /*
8a09317b
DH
1330 * We came from an IRET to user mode, so we have user
1331 * gsbase and CR3. Switch to kernel gsbase and CR3:
539f5113 1332 */
b645af2d 1333 SWAPGS
18ec54fd 1334 FENCE_SWAPGS_USER_ENTRY
8a09317b 1335 SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
539f5113
AL
1336
1337 /*
1338 * Pretend that the exception came from user mode: set up pt_regs
b3681dd5 1339 * as if we faulted immediately after IRET.
539f5113 1340 */
4d732138
IM
1341 mov %rsp, %rdi
1342 call fixup_bad_iret
1343 mov %rax, %rsp
cb6f64ed 1344 jmp .Lerror_entry_from_usermode_after_swapgs
ef1e0315 1345SYM_CODE_END(error_entry)
ddeb8f21 1346
424c7d0a
TG
1347SYM_CODE_START_LOCAL(error_return)
1348 UNWIND_HINT_REGS
1349 DEBUG_ENTRY_ASSERT_IRQS_OFF
1350 testb $3, CS(%rsp)
1351 jz restore_regs_and_return_to_kernel
1352 jmp swapgs_restore_regs_and_return_to_usermode
1353SYM_CODE_END(error_return)
1354
929bacec
AL
1355/*
1356 * Runs on exception stack. Xen PV does not go through this path at all,
1357 * so we can use real assembly here.
8a09317b
DH
1358 *
1359 * Registers:
1360 * %r14: Used to save/restore the CR3 of the interrupted context
1361 * when PAGE_TABLE_ISOLATION is in use. Do not clobber.
929bacec 1362 */
6271fef0 1363SYM_CODE_START(asm_exc_nmi)
8c1f7558 1364 UNWIND_HINT_IRET_REGS
929bacec 1365
3f3c8b8c
SR
1366 /*
1367 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1368 * the iretq it performs will take us out of NMI context.
1369 * This means that we can have nested NMIs where the next
1370 * NMI is using the top of the stack of the previous NMI. We
1371 * can't let it execute because the nested NMI will corrupt the
1372 * stack of the previous NMI. NMI handlers are not re-entrant
1373 * anyway.
1374 *
1375 * To handle this case we do the following:
1376 * Check the a special location on the stack that contains
1377 * a variable that is set when NMIs are executing.
1378 * The interrupted task's stack is also checked to see if it
1379 * is an NMI stack.
1380 * If the variable is not set and the stack is not the NMI
1381 * stack then:
1382 * o Set the special variable on the stack
0b22930e
AL
1383 * o Copy the interrupt frame into an "outermost" location on the
1384 * stack
1385 * o Copy the interrupt frame into an "iret" location on the stack
3f3c8b8c
SR
1386 * o Continue processing the NMI
1387 * If the variable is set or the previous stack is the NMI stack:
0b22930e 1388 * o Modify the "iret" location to jump to the repeat_nmi
3f3c8b8c
SR
1389 * o return back to the first NMI
1390 *
1391 * Now on exit of the first NMI, we first clear the stack variable
1392 * The NMI stack will tell any nested NMIs at that point that it is
1393 * nested. Then we pop the stack normally with iret, and if there was
1394 * a nested NMI that updated the copy interrupt stack frame, a
1395 * jump will be made to the repeat_nmi code that will handle the second
1396 * NMI.
9b6e6a83
AL
1397 *
1398 * However, espfix prevents us from directly returning to userspace
1399 * with a single IRET instruction. Similarly, IRET to user mode
1400 * can fault. We therefore handle NMIs from user space like
1401 * other IST entries.
3f3c8b8c
SR
1402 */
1403
e93c1730
AL
1404 ASM_CLAC
1405
146b2b09 1406 /* Use %rdx as our temp variable throughout */
4d732138 1407 pushq %rdx
3f3c8b8c 1408
9b6e6a83
AL
1409 testb $3, CS-RIP+8(%rsp)
1410 jz .Lnmi_from_kernel
1411
1412 /*
1413 * NMI from user mode. We need to run on the thread stack, but we
1414 * can't go through the normal entry paths: NMIs are masked, and
1415 * we don't want to enable interrupts, because then we'll end
1416 * up in an awkward situation in which IRQs are on but NMIs
1417 * are off.
83c133cf
AL
1418 *
1419 * We also must not push anything to the stack before switching
1420 * stacks lest we corrupt the "NMI executing" variable.
9b6e6a83
AL
1421 */
1422
929bacec 1423 swapgs
9b6e6a83 1424 cld
18ec54fd 1425 FENCE_SWAPGS_USER_ENTRY
8a09317b 1426 SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
9b6e6a83
AL
1427 movq %rsp, %rdx
1428 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
8c1f7558 1429 UNWIND_HINT_IRET_REGS base=%rdx offset=8
9b6e6a83
AL
1430 pushq 5*8(%rdx) /* pt_regs->ss */
1431 pushq 4*8(%rdx) /* pt_regs->rsp */
1432 pushq 3*8(%rdx) /* pt_regs->flags */
1433 pushq 2*8(%rdx) /* pt_regs->cs */
1434 pushq 1*8(%rdx) /* pt_regs->rip */
8c1f7558 1435 UNWIND_HINT_IRET_REGS
9b6e6a83 1436 pushq $-1 /* pt_regs->orig_ax */
30907fd1 1437 PUSH_AND_CLEAR_REGS rdx=(%rdx)
946c1911 1438 ENCODE_FRAME_POINTER
9b6e6a83
AL
1439
1440 /*
1441 * At this point we no longer need to worry about stack damage
1442 * due to nesting -- we're on the normal thread stack and we're
1443 * done with the NMI stack.
1444 */
1445
1446 movq %rsp, %rdi
1447 movq $-1, %rsi
6271fef0 1448 call exc_nmi
9b6e6a83 1449
45d5a168 1450 /*
9b6e6a83 1451 * Return back to user mode. We must *not* do the normal exit
946c1911 1452 * work, because we don't want to enable interrupts.
45d5a168 1453 */
8a055d7f 1454 jmp swapgs_restore_regs_and_return_to_usermode
45d5a168 1455
9b6e6a83 1456.Lnmi_from_kernel:
3f3c8b8c 1457 /*
0b22930e
AL
1458 * Here's what our stack frame will look like:
1459 * +---------------------------------------------------------+
1460 * | original SS |
1461 * | original Return RSP |
1462 * | original RFLAGS |
1463 * | original CS |
1464 * | original RIP |
1465 * +---------------------------------------------------------+
1466 * | temp storage for rdx |
1467 * +---------------------------------------------------------+
1468 * | "NMI executing" variable |
1469 * +---------------------------------------------------------+
1470 * | iret SS } Copied from "outermost" frame |
1471 * | iret Return RSP } on each loop iteration; overwritten |
1472 * | iret RFLAGS } by a nested NMI to force another |
1473 * | iret CS } iteration if needed. |
1474 * | iret RIP } |
1475 * +---------------------------------------------------------+
1476 * | outermost SS } initialized in first_nmi; |
1477 * | outermost Return RSP } will not be changed before |
1478 * | outermost RFLAGS } NMI processing is done. |
1479 * | outermost CS } Copied to "iret" frame on each |
1480 * | outermost RIP } iteration. |
1481 * +---------------------------------------------------------+
1482 * | pt_regs |
1483 * +---------------------------------------------------------+
1484 *
1485 * The "original" frame is used by hardware. Before re-enabling
1486 * NMIs, we need to be done with it, and we need to leave enough
1487 * space for the asm code here.
1488 *
1489 * We return by executing IRET while RSP points to the "iret" frame.
1490 * That will either return for real or it will loop back into NMI
1491 * processing.
1492 *
1493 * The "outermost" frame is copied to the "iret" frame on each
1494 * iteration of the loop, so each iteration starts with the "iret"
1495 * frame pointing to the final return target.
1496 */
1497
45d5a168 1498 /*
0b22930e
AL
1499 * Determine whether we're a nested NMI.
1500 *
a27507ca
AL
1501 * If we interrupted kernel code between repeat_nmi and
1502 * end_repeat_nmi, then we are a nested NMI. We must not
1503 * modify the "iret" frame because it's being written by
1504 * the outer NMI. That's okay; the outer NMI handler is
6271fef0 1505 * about to about to call exc_nmi() anyway, so we can just
a27507ca 1506 * resume the outer NMI.
45d5a168 1507 */
a27507ca
AL
1508
1509 movq $repeat_nmi, %rdx
1510 cmpq 8(%rsp), %rdx
1511 ja 1f
1512 movq $end_repeat_nmi, %rdx
1513 cmpq 8(%rsp), %rdx
1514 ja nested_nmi_out
15151:
45d5a168 1516
3f3c8b8c 1517 /*
a27507ca 1518 * Now check "NMI executing". If it's set, then we're nested.
0b22930e
AL
1519 * This will not detect if we interrupted an outer NMI just
1520 * before IRET.
3f3c8b8c 1521 */
4d732138
IM
1522 cmpl $1, -8(%rsp)
1523 je nested_nmi
3f3c8b8c
SR
1524
1525 /*
0b22930e
AL
1526 * Now test if the previous stack was an NMI stack. This covers
1527 * the case where we interrupt an outer NMI after it clears
810bc075
AL
1528 * "NMI executing" but before IRET. We need to be careful, though:
1529 * there is one case in which RSP could point to the NMI stack
1530 * despite there being no NMI active: naughty userspace controls
1531 * RSP at the very beginning of the SYSCALL targets. We can
1532 * pull a fast one on naughty userspace, though: we program
1533 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1534 * if it controls the kernel's RSP. We set DF before we clear
1535 * "NMI executing".
3f3c8b8c 1536 */
0784b364
DV
1537 lea 6*8(%rsp), %rdx
1538 /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1539 cmpq %rdx, 4*8(%rsp)
1540 /* If the stack pointer is above the NMI stack, this is a normal NMI */
1541 ja first_nmi
4d732138 1542
0784b364
DV
1543 subq $EXCEPTION_STKSZ, %rdx
1544 cmpq %rdx, 4*8(%rsp)
1545 /* If it is below the NMI stack, it is a normal NMI */
1546 jb first_nmi
810bc075
AL
1547
1548 /* Ah, it is within the NMI stack. */
1549
1550 testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1551 jz first_nmi /* RSP was user controlled. */
1552
1553 /* This is a nested NMI. */
0784b364 1554
3f3c8b8c
SR
1555nested_nmi:
1556 /*
0b22930e
AL
1557 * Modify the "iret" frame to point to repeat_nmi, forcing another
1558 * iteration of NMI handling.
3f3c8b8c 1559 */
23a781e9 1560 subq $8, %rsp
4d732138
IM
1561 leaq -10*8(%rsp), %rdx
1562 pushq $__KERNEL_DS
1563 pushq %rdx
131484c8 1564 pushfq
4d732138
IM
1565 pushq $__KERNEL_CS
1566 pushq $repeat_nmi
3f3c8b8c
SR
1567
1568 /* Put stack back */
4d732138 1569 addq $(6*8), %rsp
3f3c8b8c
SR
1570
1571nested_nmi_out:
4d732138 1572 popq %rdx
3f3c8b8c 1573
0b22930e 1574 /* We are returning to kernel mode, so this cannot result in a fault. */
929bacec 1575 iretq
3f3c8b8c
SR
1576
1577first_nmi:
0b22930e 1578 /* Restore rdx. */
4d732138 1579 movq (%rsp), %rdx
62610913 1580
36f1a77b
AL
1581 /* Make room for "NMI executing". */
1582 pushq $0
3f3c8b8c 1583
0b22930e 1584 /* Leave room for the "iret" frame */
4d732138 1585 subq $(5*8), %rsp
28696f43 1586
0b22930e 1587 /* Copy the "original" frame to the "outermost" frame */
3f3c8b8c 1588 .rept 5
4d732138 1589 pushq 11*8(%rsp)
3f3c8b8c 1590 .endr
8c1f7558 1591 UNWIND_HINT_IRET_REGS
62610913 1592
79fb4ad6
SR
1593 /* Everything up to here is safe from nested NMIs */
1594
a97439aa
AL
1595#ifdef CONFIG_DEBUG_ENTRY
1596 /*
1597 * For ease of testing, unmask NMIs right away. Disabled by
1598 * default because IRET is very expensive.
1599 */
1600 pushq $0 /* SS */
1601 pushq %rsp /* RSP (minus 8 because of the previous push) */
1602 addq $8, (%rsp) /* Fix up RSP */
1603 pushfq /* RFLAGS */
1604 pushq $__KERNEL_CS /* CS */
1605 pushq $1f /* RIP */
929bacec 1606 iretq /* continues at repeat_nmi below */
8c1f7558 1607 UNWIND_HINT_IRET_REGS
a97439aa
AL
16081:
1609#endif
1610
0b22930e 1611repeat_nmi:
62610913
JB
1612 /*
1613 * If there was a nested NMI, the first NMI's iret will return
1614 * here. But NMIs are still enabled and we can take another
1615 * nested NMI. The nested NMI checks the interrupted RIP to see
1616 * if it is between repeat_nmi and end_repeat_nmi, and if so
1617 * it will just return, as we are about to repeat an NMI anyway.
1618 * This makes it safe to copy to the stack frame that a nested
1619 * NMI will update.
0b22930e
AL
1620 *
1621 * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
1622 * we're repeating an NMI, gsbase has the same value that it had on
1623 * the first iteration. paranoid_entry will load the kernel
6271fef0 1624 * gsbase if needed before we call exc_nmi(). "NMI executing"
36f1a77b 1625 * is zero.
62610913 1626 */
36f1a77b 1627 movq $1, 10*8(%rsp) /* Set "NMI executing". */
3f3c8b8c 1628
62610913 1629 /*
0b22930e
AL
1630 * Copy the "outermost" frame to the "iret" frame. NMIs that nest
1631 * here must not modify the "iret" frame while we're writing to
1632 * it or it will end up containing garbage.
62610913 1633 */
4d732138 1634 addq $(10*8), %rsp
3f3c8b8c 1635 .rept 5
4d732138 1636 pushq -6*8(%rsp)
3f3c8b8c 1637 .endr
4d732138 1638 subq $(5*8), %rsp
62610913 1639end_repeat_nmi:
3f3c8b8c
SR
1640
1641 /*
0b22930e
AL
1642 * Everything below this point can be preempted by a nested NMI.
1643 * If this happens, then the inner NMI will change the "iret"
1644 * frame to point back to repeat_nmi.
3f3c8b8c 1645 */
4d732138 1646 pushq $-1 /* ORIG_RAX: no syscall to restart */
76f5df43 1647
1fd466ef 1648 /*
ebfc453e 1649 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1fd466ef
SR
1650 * as we should not be calling schedule in NMI context.
1651 * Even with normal interrupts enabled. An NMI should not be
1652 * setting NEED_RESCHED or anything that normal interrupts and
1653 * exceptions might do.
1654 */
4d732138 1655 call paranoid_entry
8c1f7558 1656 UNWIND_HINT_REGS
7fbb98c5 1657
6271fef0 1658 /* paranoidentry exc_nmi(), 0; without TRACE_IRQS_OFF */
4d732138
IM
1659 movq %rsp, %rdi
1660 movq $-1, %rsi
6271fef0 1661 call exc_nmi
7fbb98c5 1662
16561f27 1663 /* Always restore stashed CR3 value (see paranoid_entry) */
21e94459 1664 RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
8a09317b 1665
4d732138
IM
1666 testl %ebx, %ebx /* swapgs needed? */
1667 jnz nmi_restore
ddeb8f21
AH
1668nmi_swapgs:
1669 SWAPGS_UNSAFE_STACK
1670nmi_restore:
502af0d7 1671 POP_REGS
0b22930e 1672
471ee483
AL
1673 /*
1674 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1675 * at the "iret" frame.
1676 */
1677 addq $6*8, %rsp
28696f43 1678
810bc075
AL
1679 /*
1680 * Clear "NMI executing". Set DF first so that we can easily
1681 * distinguish the remaining code between here and IRET from
929bacec
AL
1682 * the SYSCALL entry and exit paths.
1683 *
1684 * We arguably should just inspect RIP instead, but I (Andy) wrote
1685 * this code when I had the misapprehension that Xen PV supported
1686 * NMIs, and Xen PV would break that approach.
810bc075
AL
1687 */
1688 std
1689 movq $0, 5*8(%rsp) /* clear "NMI executing" */
0b22930e
AL
1690
1691 /*
929bacec
AL
1692 * iretq reads the "iret" frame and exits the NMI stack in a
1693 * single instruction. We are returning to kernel mode, so this
1694 * cannot result in a fault. Similarly, we don't need to worry
1695 * about espfix64 on the way back to kernel mode.
0b22930e 1696 */
929bacec 1697 iretq
6271fef0 1698SYM_CODE_END(asm_exc_nmi)
ddeb8f21 1699
dffb3f9d
AL
1700#ifndef CONFIG_IA32_EMULATION
1701/*
1702 * This handles SYSCALL from 32-bit code. There is no way to program
1703 * MSRs to fully disable 32-bit SYSCALL.
1704 */
bc7b11c0 1705SYM_CODE_START(ignore_sysret)
8c1f7558 1706 UNWIND_HINT_EMPTY
4d732138 1707 mov $-ENOSYS, %eax
b2b1d94c 1708 sysretl
bc7b11c0 1709SYM_CODE_END(ignore_sysret)
dffb3f9d 1710#endif
2deb4be2 1711
b9f6976b 1712.pushsection .text, "ax"
bc7b11c0 1713SYM_CODE_START(rewind_stack_do_exit)
8c1f7558 1714 UNWIND_HINT_FUNC
2deb4be2
AL
1715 /* Prevent any naive code from trying to unwind to our caller. */
1716 xorl %ebp, %ebp
1717
1718 movq PER_CPU_VAR(cpu_current_top_of_stack), %rax
8c1f7558 1719 leaq -PTREGS_SIZE(%rax), %rsp
f977df7b 1720 UNWIND_HINT_REGS
2deb4be2
AL
1721
1722 call do_exit
bc7b11c0 1723SYM_CODE_END(rewind_stack_do_exit)
b9f6976b 1724.popsection