]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/x86/entry/entry_64.S
x86/entry/64: Shorten TEST instructions
[mirror_ubuntu-artful-kernel.git] / arch / x86 / entry / entry_64.S
1 /*
2 * linux/arch/x86_64/entry.S
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
6 * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
7 *
8 * entry.S contains the system-call and fault low-level handling routines.
9 *
10 * Some of this is documented in Documentation/x86/entry_64.txt
11 *
12 * A note on terminology:
13 * - iret frame: Architecture defined interrupt frame from SS to RIP
14 * at the top of the kernel process stack.
15 *
16 * Some macro usage:
17 * - ENTRY/END: Define functions in the symbol table.
18 * - TRACE_IRQ_*: Trace hardirq state for lock debugging.
19 * - idtentry: Define exception entry points.
20 */
21 #include <linux/linkage.h>
22 #include <asm/segment.h>
23 #include <asm/cache.h>
24 #include <asm/errno.h>
25 #include "calling.h"
26 #include <asm/asm-offsets.h>
27 #include <asm/msr.h>
28 #include <asm/unistd.h>
29 #include <asm/thread_info.h>
30 #include <asm/hw_irq.h>
31 #include <asm/page_types.h>
32 #include <asm/irqflags.h>
33 #include <asm/paravirt.h>
34 #include <asm/percpu.h>
35 #include <asm/asm.h>
36 #include <asm/smap.h>
37 #include <asm/pgtable_types.h>
38 #include <asm/export.h>
39 #include <asm/frame.h>
40 #include <linux/err.h>
41
42 .code64
43 .section .entry.text, "ax"
44
45 #ifdef CONFIG_PARAVIRT
46 ENTRY(native_usergs_sysret64)
47 UNWIND_HINT_EMPTY
48 swapgs
49 sysretq
50 END(native_usergs_sysret64)
51 #endif /* CONFIG_PARAVIRT */
52
53 .macro TRACE_IRQS_IRETQ
54 #ifdef CONFIG_TRACE_IRQFLAGS
55 bt $9, EFLAGS(%rsp) /* interrupts off? */
56 jnc 1f
57 TRACE_IRQS_ON
58 1:
59 #endif
60 .endm
61
62 /*
63 * When dynamic function tracer is enabled it will add a breakpoint
64 * to all locations that it is about to modify, sync CPUs, update
65 * all the code, sync CPUs, then remove the breakpoints. In this time
66 * if lockdep is enabled, it might jump back into the debug handler
67 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
68 *
69 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
70 * make sure the stack pointer does not get reset back to the top
71 * of the debug stack, and instead just reuses the current stack.
72 */
73 #if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
74
75 .macro TRACE_IRQS_OFF_DEBUG
76 call debug_stack_set_zero
77 TRACE_IRQS_OFF
78 call debug_stack_reset
79 .endm
80
81 .macro TRACE_IRQS_ON_DEBUG
82 call debug_stack_set_zero
83 TRACE_IRQS_ON
84 call debug_stack_reset
85 .endm
86
87 .macro TRACE_IRQS_IRETQ_DEBUG
88 bt $9, EFLAGS(%rsp) /* interrupts off? */
89 jnc 1f
90 TRACE_IRQS_ON_DEBUG
91 1:
92 .endm
93
94 #else
95 # define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
96 # define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
97 # define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
98 #endif
99
100 /*
101 * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
102 *
103 * This is the only entry point used for 64-bit system calls. The
104 * hardware interface is reasonably well designed and the register to
105 * argument mapping Linux uses fits well with the registers that are
106 * available when SYSCALL is used.
107 *
108 * SYSCALL instructions can be found inlined in libc implementations as
109 * well as some other programs and libraries. There are also a handful
110 * of SYSCALL instructions in the vDSO used, for example, as a
111 * clock_gettimeofday fallback.
112 *
113 * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
114 * then loads new ss, cs, and rip from previously programmed MSRs.
115 * rflags gets masked by a value from another MSR (so CLD and CLAC
116 * are not needed). SYSCALL does not save anything on the stack
117 * and does not change rsp.
118 *
119 * Registers on entry:
120 * rax system call number
121 * rcx return address
122 * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
123 * rdi arg0
124 * rsi arg1
125 * rdx arg2
126 * r10 arg3 (needs to be moved to rcx to conform to C ABI)
127 * r8 arg4
128 * r9 arg5
129 * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
130 *
131 * Only called from user space.
132 *
133 * When user can change pt_regs->foo always force IRET. That is because
134 * it deals with uncanonical addresses better. SYSRET has trouble
135 * with them due to bugs in both AMD and Intel CPUs.
136 */
137
138 ENTRY(entry_SYSCALL_64)
139 UNWIND_HINT_EMPTY
140 /*
141 * Interrupts are off on entry.
142 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
143 * it is too small to ever cause noticeable irq latency.
144 */
145
146 swapgs
147 movq %rsp, PER_CPU_VAR(rsp_scratch)
148 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
149
150 TRACE_IRQS_OFF
151
152 /* Construct struct pt_regs on stack */
153 pushq $__USER_DS /* pt_regs->ss */
154 pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */
155 pushq %r11 /* pt_regs->flags */
156 pushq $__USER_CS /* pt_regs->cs */
157 pushq %rcx /* pt_regs->ip */
158 GLOBAL(entry_SYSCALL_64_after_hwframe)
159 pushq %rax /* pt_regs->orig_ax */
160 pushq %rdi /* pt_regs->di */
161 pushq %rsi /* pt_regs->si */
162 pushq %rdx /* pt_regs->dx */
163 pushq %rcx /* pt_regs->cx */
164 pushq $-ENOSYS /* pt_regs->ax */
165 pushq %r8 /* pt_regs->r8 */
166 pushq %r9 /* pt_regs->r9 */
167 pushq %r10 /* pt_regs->r10 */
168 pushq %r11 /* pt_regs->r11 */
169 sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
170 UNWIND_HINT_REGS extra=0
171
172 /*
173 * If we need to do entry work or if we guess we'll need to do
174 * exit work, go straight to the slow path.
175 */
176 movq PER_CPU_VAR(current_task), %r11
177 testl $_TIF_WORK_SYSCALL_ENTRY|_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
178 jnz entry_SYSCALL64_slow_path
179
180 entry_SYSCALL_64_fastpath:
181 /*
182 * Easy case: enable interrupts and issue the syscall. If the syscall
183 * needs pt_regs, we'll call a stub that disables interrupts again
184 * and jumps to the slow path.
185 */
186 TRACE_IRQS_ON
187 ENABLE_INTERRUPTS(CLBR_NONE)
188 #if __SYSCALL_MASK == ~0
189 cmpq $__NR_syscall_max, %rax
190 #else
191 andl $__SYSCALL_MASK, %eax
192 cmpl $__NR_syscall_max, %eax
193 #endif
194 ja 1f /* return -ENOSYS (already in pt_regs->ax) */
195 movq %r10, %rcx
196
197 /*
198 * This call instruction is handled specially in stub_ptregs_64.
199 * It might end up jumping to the slow path. If it jumps, RAX
200 * and all argument registers are clobbered.
201 */
202 call *sys_call_table(, %rax, 8)
203 .Lentry_SYSCALL_64_after_fastpath_call:
204
205 movq %rax, RAX(%rsp)
206 1:
207
208 /*
209 * If we get here, then we know that pt_regs is clean for SYSRET64.
210 * If we see that no exit work is required (which we are required
211 * to check with IRQs off), then we can go straight to SYSRET64.
212 */
213 DISABLE_INTERRUPTS(CLBR_ANY)
214 TRACE_IRQS_OFF
215 movq PER_CPU_VAR(current_task), %r11
216 testl $_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
217 jnz 1f
218
219 LOCKDEP_SYS_EXIT
220 TRACE_IRQS_ON /* user mode is traced as IRQs on */
221 movq RIP(%rsp), %rcx
222 movq EFLAGS(%rsp), %r11
223 addq $6*8, %rsp /* skip extra regs -- they were preserved */
224 UNWIND_HINT_EMPTY
225 jmp .Lpop_c_regs_except_rcx_r11_and_sysret
226
227 1:
228 /*
229 * The fast path looked good when we started, but something changed
230 * along the way and we need to switch to the slow path. Calling
231 * raise(3) will trigger this, for example. IRQs are off.
232 */
233 TRACE_IRQS_ON
234 ENABLE_INTERRUPTS(CLBR_ANY)
235 SAVE_EXTRA_REGS
236 movq %rsp, %rdi
237 call syscall_return_slowpath /* returns with IRQs disabled */
238 jmp return_from_SYSCALL_64
239
240 entry_SYSCALL64_slow_path:
241 /* IRQs are off. */
242 SAVE_EXTRA_REGS
243 movq %rsp, %rdi
244 call do_syscall_64 /* returns with IRQs disabled */
245
246 return_from_SYSCALL_64:
247 TRACE_IRQS_IRETQ /* we're about to change IF */
248
249 /*
250 * Try to use SYSRET instead of IRET if we're returning to
251 * a completely clean 64-bit userspace context. If we're not,
252 * go to the slow exit path.
253 */
254 movq RCX(%rsp), %rcx
255 movq RIP(%rsp), %r11
256
257 cmpq %rcx, %r11 /* SYSRET requires RCX == RIP */
258 jne swapgs_restore_regs_and_return_to_usermode
259
260 /*
261 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
262 * in kernel space. This essentially lets the user take over
263 * the kernel, since userspace controls RSP.
264 *
265 * If width of "canonical tail" ever becomes variable, this will need
266 * to be updated to remain correct on both old and new CPUs.
267 *
268 * Change top bits to match most significant bit (47th or 56th bit
269 * depending on paging mode) in the address.
270 */
271 shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
272 sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
273
274 /* If this changed %rcx, it was not canonical */
275 cmpq %rcx, %r11
276 jne swapgs_restore_regs_and_return_to_usermode
277
278 cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
279 jne swapgs_restore_regs_and_return_to_usermode
280
281 movq R11(%rsp), %r11
282 cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
283 jne swapgs_restore_regs_and_return_to_usermode
284
285 /*
286 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
287 * restore RF properly. If the slowpath sets it for whatever reason, we
288 * need to restore it correctly.
289 *
290 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
291 * trap from userspace immediately after SYSRET. This would cause an
292 * infinite loop whenever #DB happens with register state that satisfies
293 * the opportunistic SYSRET conditions. For example, single-stepping
294 * this user code:
295 *
296 * movq $stuck_here, %rcx
297 * pushfq
298 * popq %r11
299 * stuck_here:
300 *
301 * would never get past 'stuck_here'.
302 */
303 testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
304 jnz swapgs_restore_regs_and_return_to_usermode
305
306 /* nothing to check for RSP */
307
308 cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
309 jne swapgs_restore_regs_and_return_to_usermode
310
311 /*
312 * We win! This label is here just for ease of understanding
313 * perf profiles. Nothing jumps here.
314 */
315 syscall_return_via_sysret:
316 /* rcx and r11 are already restored (see code above) */
317 UNWIND_HINT_EMPTY
318 POP_EXTRA_REGS
319 .Lpop_c_regs_except_rcx_r11_and_sysret:
320 popq %rsi /* skip r11 */
321 popq %r10
322 popq %r9
323 popq %r8
324 popq %rax
325 popq %rsi /* skip rcx */
326 popq %rdx
327 popq %rsi
328 popq %rdi
329 movq RSP-ORIG_RAX(%rsp), %rsp
330 USERGS_SYSRET64
331 END(entry_SYSCALL_64)
332
333 ENTRY(stub_ptregs_64)
334 /*
335 * Syscalls marked as needing ptregs land here.
336 * If we are on the fast path, we need to save the extra regs,
337 * which we achieve by trying again on the slow path. If we are on
338 * the slow path, the extra regs are already saved.
339 *
340 * RAX stores a pointer to the C function implementing the syscall.
341 * IRQs are on.
342 */
343 cmpq $.Lentry_SYSCALL_64_after_fastpath_call, (%rsp)
344 jne 1f
345
346 /*
347 * Called from fast path -- disable IRQs again, pop return address
348 * and jump to slow path
349 */
350 DISABLE_INTERRUPTS(CLBR_ANY)
351 TRACE_IRQS_OFF
352 popq %rax
353 UNWIND_HINT_REGS extra=0
354 jmp entry_SYSCALL64_slow_path
355
356 1:
357 jmp *%rax /* Called from C */
358 END(stub_ptregs_64)
359
360 .macro ptregs_stub func
361 ENTRY(ptregs_\func)
362 UNWIND_HINT_FUNC
363 leaq \func(%rip), %rax
364 jmp stub_ptregs_64
365 END(ptregs_\func)
366 .endm
367
368 /* Instantiate ptregs_stub for each ptregs-using syscall */
369 #define __SYSCALL_64_QUAL_(sym)
370 #define __SYSCALL_64_QUAL_ptregs(sym) ptregs_stub sym
371 #define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym)
372 #include <asm/syscalls_64.h>
373
374 /*
375 * %rdi: prev task
376 * %rsi: next task
377 */
378 ENTRY(__switch_to_asm)
379 UNWIND_HINT_FUNC
380 /*
381 * Save callee-saved registers
382 * This must match the order in inactive_task_frame
383 */
384 pushq %rbp
385 pushq %rbx
386 pushq %r12
387 pushq %r13
388 pushq %r14
389 pushq %r15
390
391 /* switch stack */
392 movq %rsp, TASK_threadsp(%rdi)
393 movq TASK_threadsp(%rsi), %rsp
394
395 #ifdef CONFIG_CC_STACKPROTECTOR
396 movq TASK_stack_canary(%rsi), %rbx
397 movq %rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset
398 #endif
399
400 /* restore callee-saved registers */
401 popq %r15
402 popq %r14
403 popq %r13
404 popq %r12
405 popq %rbx
406 popq %rbp
407
408 jmp __switch_to
409 END(__switch_to_asm)
410
411 /*
412 * A newly forked process directly context switches into this address.
413 *
414 * rax: prev task we switched from
415 * rbx: kernel thread func (NULL for user thread)
416 * r12: kernel thread arg
417 */
418 ENTRY(ret_from_fork)
419 UNWIND_HINT_EMPTY
420 movq %rax, %rdi
421 call schedule_tail /* rdi: 'prev' task parameter */
422
423 testq %rbx, %rbx /* from kernel_thread? */
424 jnz 1f /* kernel threads are uncommon */
425
426 2:
427 UNWIND_HINT_REGS
428 movq %rsp, %rdi
429 call syscall_return_slowpath /* returns with IRQs disabled */
430 TRACE_IRQS_ON /* user mode is traced as IRQS on */
431 jmp swapgs_restore_regs_and_return_to_usermode
432
433 1:
434 /* kernel thread */
435 movq %r12, %rdi
436 call *%rbx
437 /*
438 * A kernel thread is allowed to return here after successfully
439 * calling do_execve(). Exit to userspace to complete the execve()
440 * syscall.
441 */
442 movq $0, RAX(%rsp)
443 jmp 2b
444 END(ret_from_fork)
445
446 /*
447 * Build the entry stubs with some assembler magic.
448 * We pack 1 stub into every 8-byte block.
449 */
450 .align 8
451 ENTRY(irq_entries_start)
452 vector=FIRST_EXTERNAL_VECTOR
453 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
454 UNWIND_HINT_IRET_REGS
455 pushq $(~vector+0x80) /* Note: always in signed byte range */
456 jmp common_interrupt
457 .align 8
458 vector=vector+1
459 .endr
460 END(irq_entries_start)
461
462 .macro DEBUG_ENTRY_ASSERT_IRQS_OFF
463 #ifdef CONFIG_DEBUG_ENTRY
464 pushfq
465 testl $X86_EFLAGS_IF, (%rsp)
466 jz .Lokay_\@
467 ud2
468 .Lokay_\@:
469 addq $8, %rsp
470 #endif
471 .endm
472
473 /*
474 * Enters the IRQ stack if we're not already using it. NMI-safe. Clobbers
475 * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
476 * Requires kernel GSBASE.
477 *
478 * The invariant is that, if irq_count != -1, then the IRQ stack is in use.
479 */
480 .macro ENTER_IRQ_STACK regs=1 old_rsp
481 DEBUG_ENTRY_ASSERT_IRQS_OFF
482 movq %rsp, \old_rsp
483
484 .if \regs
485 UNWIND_HINT_REGS base=\old_rsp
486 .endif
487
488 incl PER_CPU_VAR(irq_count)
489 jnz .Lirq_stack_push_old_rsp_\@
490
491 /*
492 * Right now, if we just incremented irq_count to zero, we've
493 * claimed the IRQ stack but we haven't switched to it yet.
494 *
495 * If anything is added that can interrupt us here without using IST,
496 * it must be *extremely* careful to limit its stack usage. This
497 * could include kprobes and a hypothetical future IST-less #DB
498 * handler.
499 *
500 * The OOPS unwinder relies on the word at the top of the IRQ
501 * stack linking back to the previous RSP for the entire time we're
502 * on the IRQ stack. For this to work reliably, we need to write
503 * it before we actually move ourselves to the IRQ stack.
504 */
505
506 movq \old_rsp, PER_CPU_VAR(irq_stack_union + IRQ_STACK_SIZE - 8)
507 movq PER_CPU_VAR(irq_stack_ptr), %rsp
508
509 #ifdef CONFIG_DEBUG_ENTRY
510 /*
511 * If the first movq above becomes wrong due to IRQ stack layout
512 * changes, the only way we'll notice is if we try to unwind right
513 * here. Assert that we set up the stack right to catch this type
514 * of bug quickly.
515 */
516 cmpq -8(%rsp), \old_rsp
517 je .Lirq_stack_okay\@
518 ud2
519 .Lirq_stack_okay\@:
520 #endif
521
522 .Lirq_stack_push_old_rsp_\@:
523 pushq \old_rsp
524
525 .if \regs
526 UNWIND_HINT_REGS indirect=1
527 .endif
528 .endm
529
530 /*
531 * Undoes ENTER_IRQ_STACK.
532 */
533 .macro LEAVE_IRQ_STACK regs=1
534 DEBUG_ENTRY_ASSERT_IRQS_OFF
535 /* We need to be off the IRQ stack before decrementing irq_count. */
536 popq %rsp
537
538 .if \regs
539 UNWIND_HINT_REGS
540 .endif
541
542 /*
543 * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming
544 * the irq stack but we're not on it.
545 */
546
547 decl PER_CPU_VAR(irq_count)
548 .endm
549
550 /*
551 * Interrupt entry/exit.
552 *
553 * Interrupt entry points save only callee clobbered registers in fast path.
554 *
555 * Entry runs with interrupts off.
556 */
557
558 /* 0(%rsp): ~(interrupt number) */
559 .macro interrupt func
560 cld
561 ALLOC_PT_GPREGS_ON_STACK
562 SAVE_C_REGS
563 SAVE_EXTRA_REGS
564 ENCODE_FRAME_POINTER
565
566 testb $3, CS(%rsp)
567 jz 1f
568
569 /*
570 * IRQ from user mode. Switch to kernel gsbase and inform context
571 * tracking that we're in kernel mode.
572 */
573 SWAPGS
574
575 /*
576 * We need to tell lockdep that IRQs are off. We can't do this until
577 * we fix gsbase, and we should do it before enter_from_user_mode
578 * (which can take locks). Since TRACE_IRQS_OFF idempotent,
579 * the simplest way to handle it is to just call it twice if
580 * we enter from user mode. There's no reason to optimize this since
581 * TRACE_IRQS_OFF is a no-op if lockdep is off.
582 */
583 TRACE_IRQS_OFF
584
585 CALL_enter_from_user_mode
586
587 1:
588 ENTER_IRQ_STACK old_rsp=%rdi
589 /* We entered an interrupt context - irqs are off: */
590 TRACE_IRQS_OFF
591
592 call \func /* rdi points to pt_regs */
593 .endm
594
595 /*
596 * The interrupt stubs push (~vector+0x80) onto the stack and
597 * then jump to common_interrupt.
598 */
599 .p2align CONFIG_X86_L1_CACHE_SHIFT
600 common_interrupt:
601 ASM_CLAC
602 addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
603 interrupt do_IRQ
604 /* 0(%rsp): old RSP */
605 ret_from_intr:
606 DISABLE_INTERRUPTS(CLBR_ANY)
607 TRACE_IRQS_OFF
608
609 LEAVE_IRQ_STACK
610
611 testb $3, CS(%rsp)
612 jz retint_kernel
613
614 /* Interrupt came from user space */
615 GLOBAL(retint_user)
616 mov %rsp,%rdi
617 call prepare_exit_to_usermode
618 TRACE_IRQS_IRETQ
619
620 GLOBAL(swapgs_restore_regs_and_return_to_usermode)
621 #ifdef CONFIG_DEBUG_ENTRY
622 /* Assert that pt_regs indicates user mode. */
623 testb $3, CS(%rsp)
624 jnz 1f
625 ud2
626 1:
627 #endif
628 SWAPGS
629 POP_EXTRA_REGS
630 POP_C_REGS
631 addq $8, %rsp /* skip regs->orig_ax */
632 INTERRUPT_RETURN
633
634
635 /* Returning to kernel space */
636 retint_kernel:
637 #ifdef CONFIG_PREEMPT
638 /* Interrupts are off */
639 /* Check if we need preemption */
640 bt $9, EFLAGS(%rsp) /* were interrupts off? */
641 jnc 1f
642 0: cmpl $0, PER_CPU_VAR(__preempt_count)
643 jnz 1f
644 call preempt_schedule_irq
645 jmp 0b
646 1:
647 #endif
648 /*
649 * The iretq could re-enable interrupts:
650 */
651 TRACE_IRQS_IRETQ
652
653 GLOBAL(restore_regs_and_return_to_kernel)
654 #ifdef CONFIG_DEBUG_ENTRY
655 /* Assert that pt_regs indicates kernel mode. */
656 testb $3, CS(%rsp)
657 jz 1f
658 ud2
659 1:
660 #endif
661 POP_EXTRA_REGS
662 POP_C_REGS
663 addq $8, %rsp /* skip regs->orig_ax */
664 INTERRUPT_RETURN
665
666 ENTRY(native_iret)
667 UNWIND_HINT_IRET_REGS
668 /*
669 * Are we returning to a stack segment from the LDT? Note: in
670 * 64-bit mode SS:RSP on the exception stack is always valid.
671 */
672 #ifdef CONFIG_X86_ESPFIX64
673 testb $4, (SS-RIP)(%rsp)
674 jnz native_irq_return_ldt
675 #endif
676
677 .global native_irq_return_iret
678 native_irq_return_iret:
679 /*
680 * This may fault. Non-paranoid faults on return to userspace are
681 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
682 * Double-faults due to espfix64 are handled in do_double_fault.
683 * Other faults here are fatal.
684 */
685 iretq
686
687 #ifdef CONFIG_X86_ESPFIX64
688 native_irq_return_ldt:
689 /*
690 * We are running with user GSBASE. All GPRs contain their user
691 * values. We have a percpu ESPFIX stack that is eight slots
692 * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom
693 * of the ESPFIX stack.
694 *
695 * We clobber RAX and RDI in this code. We stash RDI on the
696 * normal stack and RAX on the ESPFIX stack.
697 *
698 * The ESPFIX stack layout we set up looks like this:
699 *
700 * --- top of ESPFIX stack ---
701 * SS
702 * RSP
703 * RFLAGS
704 * CS
705 * RIP <-- RSP points here when we're done
706 * RAX <-- espfix_waddr points here
707 * --- bottom of ESPFIX stack ---
708 */
709
710 pushq %rdi /* Stash user RDI */
711 SWAPGS
712 movq PER_CPU_VAR(espfix_waddr), %rdi
713 movq %rax, (0*8)(%rdi) /* user RAX */
714 movq (1*8)(%rsp), %rax /* user RIP */
715 movq %rax, (1*8)(%rdi)
716 movq (2*8)(%rsp), %rax /* user CS */
717 movq %rax, (2*8)(%rdi)
718 movq (3*8)(%rsp), %rax /* user RFLAGS */
719 movq %rax, (3*8)(%rdi)
720 movq (5*8)(%rsp), %rax /* user SS */
721 movq %rax, (5*8)(%rdi)
722 movq (4*8)(%rsp), %rax /* user RSP */
723 movq %rax, (4*8)(%rdi)
724 /* Now RAX == RSP. */
725
726 andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */
727 popq %rdi /* Restore user RDI */
728
729 /*
730 * espfix_stack[31:16] == 0. The page tables are set up such that
731 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
732 * espfix_waddr for any X. That is, there are 65536 RO aliases of
733 * the same page. Set up RSP so that RSP[31:16] contains the
734 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
735 * still points to an RO alias of the ESPFIX stack.
736 */
737 orq PER_CPU_VAR(espfix_stack), %rax
738 SWAPGS
739 movq %rax, %rsp
740 UNWIND_HINT_IRET_REGS offset=8
741
742 /*
743 * At this point, we cannot write to the stack any more, but we can
744 * still read.
745 */
746 popq %rax /* Restore user RAX */
747
748 /*
749 * RSP now points to an ordinary IRET frame, except that the page
750 * is read-only and RSP[31:16] are preloaded with the userspace
751 * values. We can now IRET back to userspace.
752 */
753 jmp native_irq_return_iret
754 #endif
755 END(common_interrupt)
756
757 /*
758 * APIC interrupts.
759 */
760 .macro apicinterrupt3 num sym do_sym
761 ENTRY(\sym)
762 UNWIND_HINT_IRET_REGS
763 ASM_CLAC
764 pushq $~(\num)
765 .Lcommon_\sym:
766 interrupt \do_sym
767 jmp ret_from_intr
768 END(\sym)
769 .endm
770
771 #ifdef CONFIG_TRACING
772 #define trace(sym) trace_##sym
773 #define smp_trace(sym) smp_trace_##sym
774
775 .macro trace_apicinterrupt num sym
776 apicinterrupt3 \num trace(\sym) smp_trace(\sym)
777 .endm
778 #else
779 .macro trace_apicinterrupt num sym do_sym
780 .endm
781 #endif
782
783 /* Make sure APIC interrupt handlers end up in the irqentry section: */
784 #define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
785 #define POP_SECTION_IRQENTRY .popsection
786
787 .macro apicinterrupt num sym do_sym
788 PUSH_SECTION_IRQENTRY
789 apicinterrupt3 \num \sym \do_sym
790 trace_apicinterrupt \num \sym
791 POP_SECTION_IRQENTRY
792 .endm
793
794 #ifdef CONFIG_SMP
795 apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
796 apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
797 #endif
798
799 #ifdef CONFIG_X86_UV
800 apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
801 #endif
802
803 apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
804 apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
805
806 #ifdef CONFIG_HAVE_KVM
807 apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
808 apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
809 apicinterrupt3 POSTED_INTR_NESTED_VECTOR kvm_posted_intr_nested_ipi smp_kvm_posted_intr_nested_ipi
810 #endif
811
812 #ifdef CONFIG_X86_MCE_THRESHOLD
813 apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
814 #endif
815
816 #ifdef CONFIG_X86_MCE_AMD
817 apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
818 #endif
819
820 #ifdef CONFIG_X86_THERMAL_VECTOR
821 apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
822 #endif
823
824 #ifdef CONFIG_SMP
825 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
826 apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
827 apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
828 #endif
829
830 apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
831 apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
832
833 #ifdef CONFIG_IRQ_WORK
834 apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
835 #endif
836
837 /*
838 * Exception entry points.
839 */
840 #define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
841
842 .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
843 ENTRY(\sym)
844 UNWIND_HINT_IRET_REGS offset=\has_error_code*8
845
846 /* Sanity check */
847 .if \shift_ist != -1 && \paranoid == 0
848 .error "using shift_ist requires paranoid=1"
849 .endif
850
851 ASM_CLAC
852
853 .if \has_error_code == 0
854 pushq $-1 /* ORIG_RAX: no syscall to restart */
855 .endif
856
857 ALLOC_PT_GPREGS_ON_STACK
858
859 .if \paranoid
860 .if \paranoid == 1
861 testb $3, CS(%rsp) /* If coming from userspace, switch stacks */
862 jnz 1f
863 .endif
864 call paranoid_entry
865 .else
866 call error_entry
867 .endif
868 UNWIND_HINT_REGS
869 /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
870
871 .if \paranoid
872 .if \shift_ist != -1
873 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
874 .else
875 TRACE_IRQS_OFF
876 .endif
877 .endif
878
879 movq %rsp, %rdi /* pt_regs pointer */
880
881 .if \has_error_code
882 movq ORIG_RAX(%rsp), %rsi /* get error code */
883 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
884 .else
885 xorl %esi, %esi /* no error code */
886 .endif
887
888 .if \shift_ist != -1
889 subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
890 .endif
891
892 call \do_sym
893
894 .if \shift_ist != -1
895 addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
896 .endif
897
898 /* these procedures expect "no swapgs" flag in ebx */
899 .if \paranoid
900 jmp paranoid_exit
901 .else
902 jmp error_exit
903 .endif
904
905 .if \paranoid == 1
906 /*
907 * Paranoid entry from userspace. Switch stacks and treat it
908 * as a normal entry. This means that paranoid handlers
909 * run in real process context if user_mode(regs).
910 */
911 1:
912 call error_entry
913
914
915 movq %rsp, %rdi /* pt_regs pointer */
916 call sync_regs
917 movq %rax, %rsp /* switch stack */
918
919 movq %rsp, %rdi /* pt_regs pointer */
920
921 .if \has_error_code
922 movq ORIG_RAX(%rsp), %rsi /* get error code */
923 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
924 .else
925 xorl %esi, %esi /* no error code */
926 .endif
927
928 call \do_sym
929
930 jmp error_exit /* %ebx: no swapgs flag */
931 .endif
932 END(\sym)
933 .endm
934
935 idtentry divide_error do_divide_error has_error_code=0
936 idtentry overflow do_overflow has_error_code=0
937 idtentry bounds do_bounds has_error_code=0
938 idtentry invalid_op do_invalid_op has_error_code=0
939 idtentry device_not_available do_device_not_available has_error_code=0
940 idtentry double_fault do_double_fault has_error_code=1 paranoid=2
941 idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
942 idtentry invalid_TSS do_invalid_TSS has_error_code=1
943 idtentry segment_not_present do_segment_not_present has_error_code=1
944 idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
945 idtentry coprocessor_error do_coprocessor_error has_error_code=0
946 idtentry alignment_check do_alignment_check has_error_code=1
947 idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
948
949
950 /*
951 * Reload gs selector with exception handling
952 * edi: new selector
953 */
954 ENTRY(native_load_gs_index)
955 FRAME_BEGIN
956 pushfq
957 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
958 SWAPGS
959 .Lgs_change:
960 movl %edi, %gs
961 2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
962 SWAPGS
963 popfq
964 FRAME_END
965 ret
966 ENDPROC(native_load_gs_index)
967 EXPORT_SYMBOL(native_load_gs_index)
968
969 _ASM_EXTABLE(.Lgs_change, bad_gs)
970 .section .fixup, "ax"
971 /* running with kernelgs */
972 bad_gs:
973 SWAPGS /* switch back to user gs */
974 .macro ZAP_GS
975 /* This can't be a string because the preprocessor needs to see it. */
976 movl $__USER_DS, %eax
977 movl %eax, %gs
978 .endm
979 ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
980 xorl %eax, %eax
981 movl %eax, %gs
982 jmp 2b
983 .previous
984
985 /* Call softirq on interrupt stack. Interrupts are off. */
986 ENTRY(do_softirq_own_stack)
987 pushq %rbp
988 mov %rsp, %rbp
989 ENTER_IRQ_STACK regs=0 old_rsp=%r11
990 call __do_softirq
991 LEAVE_IRQ_STACK regs=0
992 leaveq
993 ret
994 ENDPROC(do_softirq_own_stack)
995
996 #ifdef CONFIG_XEN
997 idtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0
998
999 /*
1000 * A note on the "critical region" in our callback handler.
1001 * We want to avoid stacking callback handlers due to events occurring
1002 * during handling of the last event. To do this, we keep events disabled
1003 * until we've done all processing. HOWEVER, we must enable events before
1004 * popping the stack frame (can't be done atomically) and so it would still
1005 * be possible to get enough handler activations to overflow the stack.
1006 * Although unlikely, bugs of that kind are hard to track down, so we'd
1007 * like to avoid the possibility.
1008 * So, on entry to the handler we detect whether we interrupted an
1009 * existing activation in its critical region -- if so, we pop the current
1010 * activation and restart the handler using the previous one.
1011 */
1012 ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */
1013
1014 /*
1015 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1016 * see the correct pointer to the pt_regs
1017 */
1018 UNWIND_HINT_FUNC
1019 movq %rdi, %rsp /* we don't return, adjust the stack frame */
1020 UNWIND_HINT_REGS
1021
1022 ENTER_IRQ_STACK old_rsp=%r10
1023 call xen_evtchn_do_upcall
1024 LEAVE_IRQ_STACK
1025
1026 #ifndef CONFIG_PREEMPT
1027 call xen_maybe_preempt_hcall
1028 #endif
1029 jmp error_exit
1030 END(xen_do_hypervisor_callback)
1031
1032 /*
1033 * Hypervisor uses this for application faults while it executes.
1034 * We get here for two reasons:
1035 * 1. Fault while reloading DS, ES, FS or GS
1036 * 2. Fault while executing IRET
1037 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1038 * registers that could be reloaded and zeroed the others.
1039 * Category 2 we fix up by killing the current process. We cannot use the
1040 * normal Linux return path in this case because if we use the IRET hypercall
1041 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1042 * We distinguish between categories by comparing each saved segment register
1043 * with its current contents: any discrepancy means we in category 1.
1044 */
1045 ENTRY(xen_failsafe_callback)
1046 UNWIND_HINT_EMPTY
1047 movl %ds, %ecx
1048 cmpw %cx, 0x10(%rsp)
1049 jne 1f
1050 movl %es, %ecx
1051 cmpw %cx, 0x18(%rsp)
1052 jne 1f
1053 movl %fs, %ecx
1054 cmpw %cx, 0x20(%rsp)
1055 jne 1f
1056 movl %gs, %ecx
1057 cmpw %cx, 0x28(%rsp)
1058 jne 1f
1059 /* All segments match their saved values => Category 2 (Bad IRET). */
1060 movq (%rsp), %rcx
1061 movq 8(%rsp), %r11
1062 addq $0x30, %rsp
1063 pushq $0 /* RIP */
1064 UNWIND_HINT_IRET_REGS offset=8
1065 jmp general_protection
1066 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1067 movq (%rsp), %rcx
1068 movq 8(%rsp), %r11
1069 addq $0x30, %rsp
1070 UNWIND_HINT_IRET_REGS
1071 pushq $-1 /* orig_ax = -1 => not a system call */
1072 ALLOC_PT_GPREGS_ON_STACK
1073 SAVE_C_REGS
1074 SAVE_EXTRA_REGS
1075 ENCODE_FRAME_POINTER
1076 jmp error_exit
1077 END(xen_failsafe_callback)
1078
1079 apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1080 xen_hvm_callback_vector xen_evtchn_do_upcall
1081
1082 #endif /* CONFIG_XEN */
1083
1084 #if IS_ENABLED(CONFIG_HYPERV)
1085 apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1086 hyperv_callback_vector hyperv_vector_handler
1087 #endif /* CONFIG_HYPERV */
1088
1089 idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1090 idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1091 idtentry stack_segment do_stack_segment has_error_code=1
1092
1093 #ifdef CONFIG_XEN
1094 idtentry xennmi do_nmi has_error_code=0
1095 idtentry xendebug do_debug has_error_code=0
1096 idtentry xenint3 do_int3 has_error_code=0
1097 #endif
1098
1099 idtentry general_protection do_general_protection has_error_code=1
1100 idtentry page_fault do_page_fault has_error_code=1
1101
1102 #ifdef CONFIG_KVM_GUEST
1103 idtentry async_page_fault do_async_page_fault has_error_code=1
1104 #endif
1105
1106 #ifdef CONFIG_X86_MCE
1107 idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
1108 #endif
1109
1110 /*
1111 * Save all registers in pt_regs, and switch gs if needed.
1112 * Use slow, but surefire "are we in kernel?" check.
1113 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1114 */
1115 ENTRY(paranoid_entry)
1116 UNWIND_HINT_FUNC
1117 cld
1118 SAVE_C_REGS 8
1119 SAVE_EXTRA_REGS 8
1120 ENCODE_FRAME_POINTER 8
1121 movl $1, %ebx
1122 movl $MSR_GS_BASE, %ecx
1123 rdmsr
1124 testl %edx, %edx
1125 js 1f /* negative -> in kernel */
1126 SWAPGS
1127 xorl %ebx, %ebx
1128 1: ret
1129 END(paranoid_entry)
1130
1131 /*
1132 * "Paranoid" exit path from exception stack. This is invoked
1133 * only on return from non-NMI IST interrupts that came
1134 * from kernel space.
1135 *
1136 * We may be returning to very strange contexts (e.g. very early
1137 * in syscall entry), so checking for preemption here would
1138 * be complicated. Fortunately, we there's no good reason
1139 * to try to handle preemption here.
1140 *
1141 * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
1142 */
1143 ENTRY(paranoid_exit)
1144 UNWIND_HINT_REGS
1145 DISABLE_INTERRUPTS(CLBR_ANY)
1146 TRACE_IRQS_OFF_DEBUG
1147 testl %ebx, %ebx /* swapgs needed? */
1148 jnz .Lparanoid_exit_no_swapgs
1149 TRACE_IRQS_IRETQ
1150 SWAPGS_UNSAFE_STACK
1151 jmp .Lparanoid_exit_restore
1152 .Lparanoid_exit_no_swapgs:
1153 TRACE_IRQS_IRETQ_DEBUG
1154 .Lparanoid_exit_restore:
1155 jmp restore_regs_and_return_to_kernel
1156 END(paranoid_exit)
1157
1158 /*
1159 * Save all registers in pt_regs, and switch gs if needed.
1160 * Return: EBX=0: came from user mode; EBX=1: otherwise
1161 */
1162 ENTRY(error_entry)
1163 UNWIND_HINT_FUNC
1164 cld
1165 SAVE_C_REGS 8
1166 SAVE_EXTRA_REGS 8
1167 ENCODE_FRAME_POINTER 8
1168 xorl %ebx, %ebx
1169 testb $3, CS+8(%rsp)
1170 jz .Lerror_kernelspace
1171
1172 /*
1173 * We entered from user mode or we're pretending to have entered
1174 * from user mode due to an IRET fault.
1175 */
1176 SWAPGS
1177
1178 .Lerror_entry_from_usermode_after_swapgs:
1179 /*
1180 * We need to tell lockdep that IRQs are off. We can't do this until
1181 * we fix gsbase, and we should do it before enter_from_user_mode
1182 * (which can take locks).
1183 */
1184 TRACE_IRQS_OFF
1185 CALL_enter_from_user_mode
1186 ret
1187
1188 .Lerror_entry_done:
1189 TRACE_IRQS_OFF
1190 ret
1191
1192 /*
1193 * There are two places in the kernel that can potentially fault with
1194 * usergs. Handle them here. B stepping K8s sometimes report a
1195 * truncated RIP for IRET exceptions returning to compat mode. Check
1196 * for these here too.
1197 */
1198 .Lerror_kernelspace:
1199 incl %ebx
1200 leaq native_irq_return_iret(%rip), %rcx
1201 cmpq %rcx, RIP+8(%rsp)
1202 je .Lerror_bad_iret
1203 movl %ecx, %eax /* zero extend */
1204 cmpq %rax, RIP+8(%rsp)
1205 je .Lbstep_iret
1206 cmpq $.Lgs_change, RIP+8(%rsp)
1207 jne .Lerror_entry_done
1208
1209 /*
1210 * hack: .Lgs_change can fail with user gsbase. If this happens, fix up
1211 * gsbase and proceed. We'll fix up the exception and land in
1212 * .Lgs_change's error handler with kernel gsbase.
1213 */
1214 SWAPGS
1215 jmp .Lerror_entry_done
1216
1217 .Lbstep_iret:
1218 /* Fix truncated RIP */
1219 movq %rcx, RIP+8(%rsp)
1220 /* fall through */
1221
1222 .Lerror_bad_iret:
1223 /*
1224 * We came from an IRET to user mode, so we have user gsbase.
1225 * Switch to kernel gsbase:
1226 */
1227 SWAPGS
1228
1229 /*
1230 * Pretend that the exception came from user mode: set up pt_regs
1231 * as if we faulted immediately after IRET and clear EBX so that
1232 * error_exit knows that we will be returning to user mode.
1233 */
1234 mov %rsp, %rdi
1235 call fixup_bad_iret
1236 mov %rax, %rsp
1237 decl %ebx
1238 jmp .Lerror_entry_from_usermode_after_swapgs
1239 END(error_entry)
1240
1241
1242 /*
1243 * On entry, EBX is a "return to kernel mode" flag:
1244 * 1: already in kernel mode, don't need SWAPGS
1245 * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
1246 */
1247 ENTRY(error_exit)
1248 UNWIND_HINT_REGS
1249 DISABLE_INTERRUPTS(CLBR_ANY)
1250 TRACE_IRQS_OFF
1251 testl %ebx, %ebx
1252 jnz retint_kernel
1253 jmp retint_user
1254 END(error_exit)
1255
1256 /*
1257 * Runs on exception stack. Xen PV does not go through this path at all,
1258 * so we can use real assembly here.
1259 */
1260 ENTRY(nmi)
1261 UNWIND_HINT_IRET_REGS
1262
1263 /*
1264 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1265 * the iretq it performs will take us out of NMI context.
1266 * This means that we can have nested NMIs where the next
1267 * NMI is using the top of the stack of the previous NMI. We
1268 * can't let it execute because the nested NMI will corrupt the
1269 * stack of the previous NMI. NMI handlers are not re-entrant
1270 * anyway.
1271 *
1272 * To handle this case we do the following:
1273 * Check the a special location on the stack that contains
1274 * a variable that is set when NMIs are executing.
1275 * The interrupted task's stack is also checked to see if it
1276 * is an NMI stack.
1277 * If the variable is not set and the stack is not the NMI
1278 * stack then:
1279 * o Set the special variable on the stack
1280 * o Copy the interrupt frame into an "outermost" location on the
1281 * stack
1282 * o Copy the interrupt frame into an "iret" location on the stack
1283 * o Continue processing the NMI
1284 * If the variable is set or the previous stack is the NMI stack:
1285 * o Modify the "iret" location to jump to the repeat_nmi
1286 * o return back to the first NMI
1287 *
1288 * Now on exit of the first NMI, we first clear the stack variable
1289 * The NMI stack will tell any nested NMIs at that point that it is
1290 * nested. Then we pop the stack normally with iret, and if there was
1291 * a nested NMI that updated the copy interrupt stack frame, a
1292 * jump will be made to the repeat_nmi code that will handle the second
1293 * NMI.
1294 *
1295 * However, espfix prevents us from directly returning to userspace
1296 * with a single IRET instruction. Similarly, IRET to user mode
1297 * can fault. We therefore handle NMIs from user space like
1298 * other IST entries.
1299 */
1300
1301 ASM_CLAC
1302
1303 /* Use %rdx as our temp variable throughout */
1304 pushq %rdx
1305
1306 testb $3, CS-RIP+8(%rsp)
1307 jz .Lnmi_from_kernel
1308
1309 /*
1310 * NMI from user mode. We need to run on the thread stack, but we
1311 * can't go through the normal entry paths: NMIs are masked, and
1312 * we don't want to enable interrupts, because then we'll end
1313 * up in an awkward situation in which IRQs are on but NMIs
1314 * are off.
1315 *
1316 * We also must not push anything to the stack before switching
1317 * stacks lest we corrupt the "NMI executing" variable.
1318 */
1319
1320 swapgs
1321 cld
1322 movq %rsp, %rdx
1323 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
1324 UNWIND_HINT_IRET_REGS base=%rdx offset=8
1325 pushq 5*8(%rdx) /* pt_regs->ss */
1326 pushq 4*8(%rdx) /* pt_regs->rsp */
1327 pushq 3*8(%rdx) /* pt_regs->flags */
1328 pushq 2*8(%rdx) /* pt_regs->cs */
1329 pushq 1*8(%rdx) /* pt_regs->rip */
1330 UNWIND_HINT_IRET_REGS
1331 pushq $-1 /* pt_regs->orig_ax */
1332 pushq %rdi /* pt_regs->di */
1333 pushq %rsi /* pt_regs->si */
1334 pushq (%rdx) /* pt_regs->dx */
1335 pushq %rcx /* pt_regs->cx */
1336 pushq %rax /* pt_regs->ax */
1337 pushq %r8 /* pt_regs->r8 */
1338 pushq %r9 /* pt_regs->r9 */
1339 pushq %r10 /* pt_regs->r10 */
1340 pushq %r11 /* pt_regs->r11 */
1341 pushq %rbx /* pt_regs->rbx */
1342 pushq %rbp /* pt_regs->rbp */
1343 pushq %r12 /* pt_regs->r12 */
1344 pushq %r13 /* pt_regs->r13 */
1345 pushq %r14 /* pt_regs->r14 */
1346 pushq %r15 /* pt_regs->r15 */
1347 UNWIND_HINT_REGS
1348 ENCODE_FRAME_POINTER
1349
1350 /*
1351 * At this point we no longer need to worry about stack damage
1352 * due to nesting -- we're on the normal thread stack and we're
1353 * done with the NMI stack.
1354 */
1355
1356 movq %rsp, %rdi
1357 movq $-1, %rsi
1358 call do_nmi
1359
1360 /*
1361 * Return back to user mode. We must *not* do the normal exit
1362 * work, because we don't want to enable interrupts.
1363 */
1364 jmp swapgs_restore_regs_and_return_to_usermode
1365
1366 .Lnmi_from_kernel:
1367 /*
1368 * Here's what our stack frame will look like:
1369 * +---------------------------------------------------------+
1370 * | original SS |
1371 * | original Return RSP |
1372 * | original RFLAGS |
1373 * | original CS |
1374 * | original RIP |
1375 * +---------------------------------------------------------+
1376 * | temp storage for rdx |
1377 * +---------------------------------------------------------+
1378 * | "NMI executing" variable |
1379 * +---------------------------------------------------------+
1380 * | iret SS } Copied from "outermost" frame |
1381 * | iret Return RSP } on each loop iteration; overwritten |
1382 * | iret RFLAGS } by a nested NMI to force another |
1383 * | iret CS } iteration if needed. |
1384 * | iret RIP } |
1385 * +---------------------------------------------------------+
1386 * | outermost SS } initialized in first_nmi; |
1387 * | outermost Return RSP } will not be changed before |
1388 * | outermost RFLAGS } NMI processing is done. |
1389 * | outermost CS } Copied to "iret" frame on each |
1390 * | outermost RIP } iteration. |
1391 * +---------------------------------------------------------+
1392 * | pt_regs |
1393 * +---------------------------------------------------------+
1394 *
1395 * The "original" frame is used by hardware. Before re-enabling
1396 * NMIs, we need to be done with it, and we need to leave enough
1397 * space for the asm code here.
1398 *
1399 * We return by executing IRET while RSP points to the "iret" frame.
1400 * That will either return for real or it will loop back into NMI
1401 * processing.
1402 *
1403 * The "outermost" frame is copied to the "iret" frame on each
1404 * iteration of the loop, so each iteration starts with the "iret"
1405 * frame pointing to the final return target.
1406 */
1407
1408 /*
1409 * Determine whether we're a nested NMI.
1410 *
1411 * If we interrupted kernel code between repeat_nmi and
1412 * end_repeat_nmi, then we are a nested NMI. We must not
1413 * modify the "iret" frame because it's being written by
1414 * the outer NMI. That's okay; the outer NMI handler is
1415 * about to about to call do_nmi anyway, so we can just
1416 * resume the outer NMI.
1417 */
1418
1419 movq $repeat_nmi, %rdx
1420 cmpq 8(%rsp), %rdx
1421 ja 1f
1422 movq $end_repeat_nmi, %rdx
1423 cmpq 8(%rsp), %rdx
1424 ja nested_nmi_out
1425 1:
1426
1427 /*
1428 * Now check "NMI executing". If it's set, then we're nested.
1429 * This will not detect if we interrupted an outer NMI just
1430 * before IRET.
1431 */
1432 cmpl $1, -8(%rsp)
1433 je nested_nmi
1434
1435 /*
1436 * Now test if the previous stack was an NMI stack. This covers
1437 * the case where we interrupt an outer NMI after it clears
1438 * "NMI executing" but before IRET. We need to be careful, though:
1439 * there is one case in which RSP could point to the NMI stack
1440 * despite there being no NMI active: naughty userspace controls
1441 * RSP at the very beginning of the SYSCALL targets. We can
1442 * pull a fast one on naughty userspace, though: we program
1443 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1444 * if it controls the kernel's RSP. We set DF before we clear
1445 * "NMI executing".
1446 */
1447 lea 6*8(%rsp), %rdx
1448 /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1449 cmpq %rdx, 4*8(%rsp)
1450 /* If the stack pointer is above the NMI stack, this is a normal NMI */
1451 ja first_nmi
1452
1453 subq $EXCEPTION_STKSZ, %rdx
1454 cmpq %rdx, 4*8(%rsp)
1455 /* If it is below the NMI stack, it is a normal NMI */
1456 jb first_nmi
1457
1458 /* Ah, it is within the NMI stack. */
1459
1460 testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1461 jz first_nmi /* RSP was user controlled. */
1462
1463 /* This is a nested NMI. */
1464
1465 nested_nmi:
1466 /*
1467 * Modify the "iret" frame to point to repeat_nmi, forcing another
1468 * iteration of NMI handling.
1469 */
1470 subq $8, %rsp
1471 leaq -10*8(%rsp), %rdx
1472 pushq $__KERNEL_DS
1473 pushq %rdx
1474 pushfq
1475 pushq $__KERNEL_CS
1476 pushq $repeat_nmi
1477
1478 /* Put stack back */
1479 addq $(6*8), %rsp
1480
1481 nested_nmi_out:
1482 popq %rdx
1483
1484 /* We are returning to kernel mode, so this cannot result in a fault. */
1485 iretq
1486
1487 first_nmi:
1488 /* Restore rdx. */
1489 movq (%rsp), %rdx
1490
1491 /* Make room for "NMI executing". */
1492 pushq $0
1493
1494 /* Leave room for the "iret" frame */
1495 subq $(5*8), %rsp
1496
1497 /* Copy the "original" frame to the "outermost" frame */
1498 .rept 5
1499 pushq 11*8(%rsp)
1500 .endr
1501 UNWIND_HINT_IRET_REGS
1502
1503 /* Everything up to here is safe from nested NMIs */
1504
1505 #ifdef CONFIG_DEBUG_ENTRY
1506 /*
1507 * For ease of testing, unmask NMIs right away. Disabled by
1508 * default because IRET is very expensive.
1509 */
1510 pushq $0 /* SS */
1511 pushq %rsp /* RSP (minus 8 because of the previous push) */
1512 addq $8, (%rsp) /* Fix up RSP */
1513 pushfq /* RFLAGS */
1514 pushq $__KERNEL_CS /* CS */
1515 pushq $1f /* RIP */
1516 iretq /* continues at repeat_nmi below */
1517 UNWIND_HINT_IRET_REGS
1518 1:
1519 #endif
1520
1521 repeat_nmi:
1522 /*
1523 * If there was a nested NMI, the first NMI's iret will return
1524 * here. But NMIs are still enabled and we can take another
1525 * nested NMI. The nested NMI checks the interrupted RIP to see
1526 * if it is between repeat_nmi and end_repeat_nmi, and if so
1527 * it will just return, as we are about to repeat an NMI anyway.
1528 * This makes it safe to copy to the stack frame that a nested
1529 * NMI will update.
1530 *
1531 * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
1532 * we're repeating an NMI, gsbase has the same value that it had on
1533 * the first iteration. paranoid_entry will load the kernel
1534 * gsbase if needed before we call do_nmi. "NMI executing"
1535 * is zero.
1536 */
1537 movq $1, 10*8(%rsp) /* Set "NMI executing". */
1538
1539 /*
1540 * Copy the "outermost" frame to the "iret" frame. NMIs that nest
1541 * here must not modify the "iret" frame while we're writing to
1542 * it or it will end up containing garbage.
1543 */
1544 addq $(10*8), %rsp
1545 .rept 5
1546 pushq -6*8(%rsp)
1547 .endr
1548 subq $(5*8), %rsp
1549 end_repeat_nmi:
1550
1551 /*
1552 * Everything below this point can be preempted by a nested NMI.
1553 * If this happens, then the inner NMI will change the "iret"
1554 * frame to point back to repeat_nmi.
1555 */
1556 pushq $-1 /* ORIG_RAX: no syscall to restart */
1557 ALLOC_PT_GPREGS_ON_STACK
1558
1559 /*
1560 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1561 * as we should not be calling schedule in NMI context.
1562 * Even with normal interrupts enabled. An NMI should not be
1563 * setting NEED_RESCHED or anything that normal interrupts and
1564 * exceptions might do.
1565 */
1566 call paranoid_entry
1567 UNWIND_HINT_REGS
1568
1569 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1570 movq %rsp, %rdi
1571 movq $-1, %rsi
1572 call do_nmi
1573
1574 testl %ebx, %ebx /* swapgs needed? */
1575 jnz nmi_restore
1576 nmi_swapgs:
1577 SWAPGS_UNSAFE_STACK
1578 nmi_restore:
1579 POP_EXTRA_REGS
1580 POP_C_REGS
1581
1582 /*
1583 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1584 * at the "iret" frame.
1585 */
1586 addq $6*8, %rsp
1587
1588 /*
1589 * Clear "NMI executing". Set DF first so that we can easily
1590 * distinguish the remaining code between here and IRET from
1591 * the SYSCALL entry and exit paths.
1592 *
1593 * We arguably should just inspect RIP instead, but I (Andy) wrote
1594 * this code when I had the misapprehension that Xen PV supported
1595 * NMIs, and Xen PV would break that approach.
1596 */
1597 std
1598 movq $0, 5*8(%rsp) /* clear "NMI executing" */
1599
1600 /*
1601 * iretq reads the "iret" frame and exits the NMI stack in a
1602 * single instruction. We are returning to kernel mode, so this
1603 * cannot result in a fault. Similarly, we don't need to worry
1604 * about espfix64 on the way back to kernel mode.
1605 */
1606 iretq
1607 END(nmi)
1608
1609 ENTRY(ignore_sysret)
1610 UNWIND_HINT_EMPTY
1611 mov $-ENOSYS, %eax
1612 sysret
1613 END(ignore_sysret)
1614
1615 ENTRY(rewind_stack_do_exit)
1616 UNWIND_HINT_FUNC
1617 /* Prevent any naive code from trying to unwind to our caller. */
1618 xorl %ebp, %ebp
1619
1620 movq PER_CPU_VAR(cpu_current_top_of_stack), %rax
1621 leaq -PTREGS_SIZE(%rax), %rsp
1622 UNWIND_HINT_FUNC sp_offset=PTREGS_SIZE
1623
1624 call do_exit
1625 END(rewind_stack_do_exit)