x86/asm/entry/irq: Simplify interrupt dispatch table (IDT) layout
[deliverable/linux.git] / arch / x86 / kernel / 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
9 /*
10 * entry.S contains the system-call and fault low-level handling routines.
11 *
12 * Some of this is documented in Documentation/x86/entry_64.txt
13 *
14 * NOTE: This code handles signal-recognition, which happens every time
15 * after an interrupt and after each system call.
16 *
17 * A note on terminology:
18 * - iret frame: Architecture defined interrupt frame from SS to RIP
19 * at the top of the kernel process stack.
20 *
21 * Some macro usage:
22 * - CFI macros are used to generate dwarf2 unwind information for better
23 * backtraces. They don't change any code.
24 * - ENTRY/END Define functions in the symbol table.
25 * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
26 * - idtentry - Define exception entry points.
27 */
28
29 #include <linux/linkage.h>
30 #include <asm/segment.h>
31 #include <asm/cache.h>
32 #include <asm/errno.h>
33 #include <asm/dwarf2.h>
34 #include <asm/calling.h>
35 #include <asm/asm-offsets.h>
36 #include <asm/msr.h>
37 #include <asm/unistd.h>
38 #include <asm/thread_info.h>
39 #include <asm/hw_irq.h>
40 #include <asm/page_types.h>
41 #include <asm/irqflags.h>
42 #include <asm/paravirt.h>
43 #include <asm/percpu.h>
44 #include <asm/asm.h>
45 #include <asm/context_tracking.h>
46 #include <asm/smap.h>
47 #include <asm/pgtable_types.h>
48 #include <linux/err.h>
49
50 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
51 #include <linux/elf-em.h>
52 #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
53 #define __AUDIT_ARCH_64BIT 0x80000000
54 #define __AUDIT_ARCH_LE 0x40000000
55
56 .code64
57 .section .entry.text, "ax"
58
59
60 #ifdef CONFIG_PARAVIRT
61 ENTRY(native_usergs_sysret64)
62 swapgs
63 sysretq
64 ENDPROC(native_usergs_sysret64)
65 #endif /* CONFIG_PARAVIRT */
66
67
68 .macro TRACE_IRQS_IRETQ
69 #ifdef CONFIG_TRACE_IRQFLAGS
70 bt $9,EFLAGS(%rsp) /* interrupts off? */
71 jnc 1f
72 TRACE_IRQS_ON
73 1:
74 #endif
75 .endm
76
77 /*
78 * When dynamic function tracer is enabled it will add a breakpoint
79 * to all locations that it is about to modify, sync CPUs, update
80 * all the code, sync CPUs, then remove the breakpoints. In this time
81 * if lockdep is enabled, it might jump back into the debug handler
82 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
83 *
84 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
85 * make sure the stack pointer does not get reset back to the top
86 * of the debug stack, and instead just reuses the current stack.
87 */
88 #if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
89
90 .macro TRACE_IRQS_OFF_DEBUG
91 call debug_stack_set_zero
92 TRACE_IRQS_OFF
93 call debug_stack_reset
94 .endm
95
96 .macro TRACE_IRQS_ON_DEBUG
97 call debug_stack_set_zero
98 TRACE_IRQS_ON
99 call debug_stack_reset
100 .endm
101
102 .macro TRACE_IRQS_IRETQ_DEBUG
103 bt $9,EFLAGS(%rsp) /* interrupts off? */
104 jnc 1f
105 TRACE_IRQS_ON_DEBUG
106 1:
107 .endm
108
109 #else
110 # define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
111 # define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
112 # define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
113 #endif
114
115 /*
116 * empty frame
117 */
118 .macro EMPTY_FRAME start=1 offset=0
119 .if \start
120 CFI_STARTPROC simple
121 CFI_SIGNAL_FRAME
122 CFI_DEF_CFA rsp,8+\offset
123 .else
124 CFI_DEF_CFA_OFFSET 8+\offset
125 .endif
126 .endm
127
128 /*
129 * initial frame state for interrupts (and exceptions without error code)
130 */
131 .macro INTR_FRAME start=1 offset=0
132 EMPTY_FRAME \start, 5*8+\offset
133 /*CFI_REL_OFFSET ss, 4*8+\offset*/
134 CFI_REL_OFFSET rsp, 3*8+\offset
135 /*CFI_REL_OFFSET rflags, 2*8+\offset*/
136 /*CFI_REL_OFFSET cs, 1*8+\offset*/
137 CFI_REL_OFFSET rip, 0*8+\offset
138 .endm
139
140 /*
141 * initial frame state for exceptions with error code (and interrupts
142 * with vector already pushed)
143 */
144 .macro XCPT_FRAME start=1 offset=0
145 INTR_FRAME \start, 1*8+\offset
146 .endm
147
148 /*
149 * frame that enables passing a complete pt_regs to a C function.
150 */
151 .macro DEFAULT_FRAME start=1 offset=0
152 XCPT_FRAME \start, ORIG_RAX+\offset
153 CFI_REL_OFFSET rdi, RDI+\offset
154 CFI_REL_OFFSET rsi, RSI+\offset
155 CFI_REL_OFFSET rdx, RDX+\offset
156 CFI_REL_OFFSET rcx, RCX+\offset
157 CFI_REL_OFFSET rax, RAX+\offset
158 CFI_REL_OFFSET r8, R8+\offset
159 CFI_REL_OFFSET r9, R9+\offset
160 CFI_REL_OFFSET r10, R10+\offset
161 CFI_REL_OFFSET r11, R11+\offset
162 CFI_REL_OFFSET rbx, RBX+\offset
163 CFI_REL_OFFSET rbp, RBP+\offset
164 CFI_REL_OFFSET r12, R12+\offset
165 CFI_REL_OFFSET r13, R13+\offset
166 CFI_REL_OFFSET r14, R14+\offset
167 CFI_REL_OFFSET r15, R15+\offset
168 .endm
169
170 /*
171 * 64bit SYSCALL instruction entry. Up to 6 arguments in registers.
172 *
173 * 64bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
174 * then loads new ss, cs, and rip from previously programmed MSRs.
175 * rflags gets masked by a value from another MSR (so CLD and CLAC
176 * are not needed). SYSCALL does not save anything on the stack
177 * and does not change rsp.
178 *
179 * Registers on entry:
180 * rax system call number
181 * rcx return address
182 * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
183 * rdi arg0
184 * rsi arg1
185 * rdx arg2
186 * r10 arg3 (needs to be moved to rcx to conform to C ABI)
187 * r8 arg4
188 * r9 arg5
189 * (note: r12-r15,rbp,rbx are callee-preserved in C ABI)
190 *
191 * Only called from user space.
192 *
193 * When user can change pt_regs->foo always force IRET. That is because
194 * it deals with uncanonical addresses better. SYSRET has trouble
195 * with them due to bugs in both AMD and Intel CPUs.
196 */
197
198 ENTRY(system_call)
199 CFI_STARTPROC simple
200 CFI_SIGNAL_FRAME
201 CFI_DEF_CFA rsp,0
202 CFI_REGISTER rip,rcx
203 /*CFI_REGISTER rflags,r11*/
204
205 /*
206 * Interrupts are off on entry.
207 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
208 * it is too small to ever cause noticeable irq latency.
209 */
210 SWAPGS_UNSAFE_STACK
211 /*
212 * A hypervisor implementation might want to use a label
213 * after the swapgs, so that it can do the swapgs
214 * for the guest and jump here on syscall.
215 */
216 GLOBAL(system_call_after_swapgs)
217
218 movq %rsp,PER_CPU_VAR(rsp_scratch)
219 movq PER_CPU_VAR(kernel_stack),%rsp
220
221 /* Construct struct pt_regs on stack */
222 pushq_cfi $__USER_DS /* pt_regs->ss */
223 pushq_cfi PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */
224 /*
225 * Re-enable interrupts.
226 * We use 'rsp_scratch' as a scratch space, hence irq-off block above
227 * must execute atomically in the face of possible interrupt-driven
228 * task preemption. We must enable interrupts only after we're done
229 * with using rsp_scratch:
230 */
231 ENABLE_INTERRUPTS(CLBR_NONE)
232 pushq_cfi %r11 /* pt_regs->flags */
233 pushq_cfi $__USER_CS /* pt_regs->cs */
234 pushq_cfi %rcx /* pt_regs->ip */
235 CFI_REL_OFFSET rip,0
236 pushq_cfi_reg rax /* pt_regs->orig_ax */
237 pushq_cfi_reg rdi /* pt_regs->di */
238 pushq_cfi_reg rsi /* pt_regs->si */
239 pushq_cfi_reg rdx /* pt_regs->dx */
240 pushq_cfi_reg rcx /* pt_regs->cx */
241 pushq_cfi $-ENOSYS /* pt_regs->ax */
242 pushq_cfi_reg r8 /* pt_regs->r8 */
243 pushq_cfi_reg r9 /* pt_regs->r9 */
244 pushq_cfi_reg r10 /* pt_regs->r10 */
245 pushq_cfi_reg r11 /* pt_regs->r11 */
246 sub $(6*8),%rsp /* pt_regs->bp,bx,r12-15 not saved */
247 CFI_ADJUST_CFA_OFFSET 6*8
248
249 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
250 jnz tracesys
251 system_call_fastpath:
252 #if __SYSCALL_MASK == ~0
253 cmpq $__NR_syscall_max,%rax
254 #else
255 andl $__SYSCALL_MASK,%eax
256 cmpl $__NR_syscall_max,%eax
257 #endif
258 ja 1f /* return -ENOSYS (already in pt_regs->ax) */
259 movq %r10,%rcx
260 call *sys_call_table(,%rax,8)
261 movq %rax,RAX(%rsp)
262 1:
263 /*
264 * Syscall return path ending with SYSRET (fast path).
265 * Has incompletely filled pt_regs.
266 */
267 LOCKDEP_SYS_EXIT
268 /*
269 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
270 * it is too small to ever cause noticeable irq latency.
271 */
272 DISABLE_INTERRUPTS(CLBR_NONE)
273
274 /*
275 * We must check ti flags with interrupts (or at least preemption)
276 * off because we must *never* return to userspace without
277 * processing exit work that is enqueued if we're preempted here.
278 * In particular, returning to userspace with any of the one-shot
279 * flags (TIF_NOTIFY_RESUME, TIF_USER_RETURN_NOTIFY, etc) set is
280 * very bad.
281 */
282 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
283 jnz int_ret_from_sys_call_irqs_off /* Go to the slow path */
284
285 CFI_REMEMBER_STATE
286
287 RESTORE_C_REGS_EXCEPT_RCX_R11
288 movq RIP(%rsp),%rcx
289 CFI_REGISTER rip,rcx
290 movq EFLAGS(%rsp),%r11
291 /*CFI_REGISTER rflags,r11*/
292 movq RSP(%rsp),%rsp
293 /*
294 * 64bit SYSRET restores rip from rcx,
295 * rflags from r11 (but RF and VM bits are forced to 0),
296 * cs and ss are loaded from MSRs.
297 * Restoration of rflags re-enables interrupts.
298 */
299 USERGS_SYSRET64
300
301 CFI_RESTORE_STATE
302
303 /* Do syscall entry tracing */
304 tracesys:
305 movq %rsp, %rdi
306 movl $AUDIT_ARCH_X86_64, %esi
307 call syscall_trace_enter_phase1
308 test %rax, %rax
309 jnz tracesys_phase2 /* if needed, run the slow path */
310 RESTORE_C_REGS_EXCEPT_RAX /* else restore clobbered regs */
311 movq ORIG_RAX(%rsp), %rax
312 jmp system_call_fastpath /* and return to the fast path */
313
314 tracesys_phase2:
315 SAVE_EXTRA_REGS
316 movq %rsp, %rdi
317 movl $AUDIT_ARCH_X86_64, %esi
318 movq %rax,%rdx
319 call syscall_trace_enter_phase2
320
321 /*
322 * Reload registers from stack in case ptrace changed them.
323 * We don't reload %rax because syscall_trace_entry_phase2() returned
324 * the value it wants us to use in the table lookup.
325 */
326 RESTORE_C_REGS_EXCEPT_RAX
327 RESTORE_EXTRA_REGS
328 #if __SYSCALL_MASK == ~0
329 cmpq $__NR_syscall_max,%rax
330 #else
331 andl $__SYSCALL_MASK,%eax
332 cmpl $__NR_syscall_max,%eax
333 #endif
334 ja 1f /* return -ENOSYS (already in pt_regs->ax) */
335 movq %r10,%rcx /* fixup for C */
336 call *sys_call_table(,%rax,8)
337 movq %rax,RAX(%rsp)
338 1:
339 /* Use IRET because user could have changed pt_regs->foo */
340
341 /*
342 * Syscall return path ending with IRET.
343 * Has correct iret frame.
344 */
345 GLOBAL(int_ret_from_sys_call)
346 DISABLE_INTERRUPTS(CLBR_NONE)
347 int_ret_from_sys_call_irqs_off: /* jumps come here from the irqs-off SYSRET path */
348 TRACE_IRQS_OFF
349 movl $_TIF_ALLWORK_MASK,%edi
350 /* edi: mask to check */
351 GLOBAL(int_with_check)
352 LOCKDEP_SYS_EXIT_IRQ
353 GET_THREAD_INFO(%rcx)
354 movl TI_flags(%rcx),%edx
355 andl %edi,%edx
356 jnz int_careful
357 andl $~TS_COMPAT,TI_status(%rcx)
358 jmp syscall_return
359
360 /* Either reschedule or signal or syscall exit tracking needed. */
361 /* First do a reschedule test. */
362 /* edx: work, edi: workmask */
363 int_careful:
364 bt $TIF_NEED_RESCHED,%edx
365 jnc int_very_careful
366 TRACE_IRQS_ON
367 ENABLE_INTERRUPTS(CLBR_NONE)
368 pushq_cfi %rdi
369 SCHEDULE_USER
370 popq_cfi %rdi
371 DISABLE_INTERRUPTS(CLBR_NONE)
372 TRACE_IRQS_OFF
373 jmp int_with_check
374
375 /* handle signals and tracing -- both require a full pt_regs */
376 int_very_careful:
377 TRACE_IRQS_ON
378 ENABLE_INTERRUPTS(CLBR_NONE)
379 SAVE_EXTRA_REGS
380 /* Check for syscall exit trace */
381 testl $_TIF_WORK_SYSCALL_EXIT,%edx
382 jz int_signal
383 pushq_cfi %rdi
384 leaq 8(%rsp),%rdi # &ptregs -> arg1
385 call syscall_trace_leave
386 popq_cfi %rdi
387 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
388 jmp int_restore_rest
389
390 int_signal:
391 testl $_TIF_DO_NOTIFY_MASK,%edx
392 jz 1f
393 movq %rsp,%rdi # &ptregs -> arg1
394 xorl %esi,%esi # oldset -> arg2
395 call do_notify_resume
396 1: movl $_TIF_WORK_MASK,%edi
397 int_restore_rest:
398 RESTORE_EXTRA_REGS
399 DISABLE_INTERRUPTS(CLBR_NONE)
400 TRACE_IRQS_OFF
401 jmp int_with_check
402
403 syscall_return:
404 /* The IRETQ could re-enable interrupts: */
405 DISABLE_INTERRUPTS(CLBR_ANY)
406 TRACE_IRQS_IRETQ
407
408 /*
409 * Try to use SYSRET instead of IRET if we're returning to
410 * a completely clean 64-bit userspace context.
411 */
412 movq RCX(%rsp),%rcx
413 cmpq %rcx,RIP(%rsp) /* RCX == RIP */
414 jne opportunistic_sysret_failed
415
416 /*
417 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
418 * in kernel space. This essentially lets the user take over
419 * the kernel, since userspace controls RSP. It's not worth
420 * testing for canonicalness exactly -- this check detects any
421 * of the 17 high bits set, which is true for non-canonical
422 * or kernel addresses. (This will pessimize vsyscall=native.
423 * Big deal.)
424 *
425 * If virtual addresses ever become wider, this will need
426 * to be updated to remain correct on both old and new CPUs.
427 */
428 .ifne __VIRTUAL_MASK_SHIFT - 47
429 .error "virtual address width changed -- SYSRET checks need update"
430 .endif
431 shr $__VIRTUAL_MASK_SHIFT, %rcx
432 jnz opportunistic_sysret_failed
433
434 cmpq $__USER_CS,CS(%rsp) /* CS must match SYSRET */
435 jne opportunistic_sysret_failed
436
437 movq R11(%rsp),%r11
438 cmpq %r11,EFLAGS(%rsp) /* R11 == RFLAGS */
439 jne opportunistic_sysret_failed
440
441 /*
442 * SYSRET can't restore RF. SYSRET can restore TF, but unlike IRET,
443 * restoring TF results in a trap from userspace immediately after
444 * SYSRET. This would cause an infinite loop whenever #DB happens
445 * with register state that satisfies the opportunistic SYSRET
446 * conditions. For example, single-stepping this user code:
447 *
448 * movq $stuck_here,%rcx
449 * pushfq
450 * popq %r11
451 * stuck_here:
452 *
453 * would never get past 'stuck_here'.
454 */
455 testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
456 jnz opportunistic_sysret_failed
457
458 /* nothing to check for RSP */
459
460 cmpq $__USER_DS,SS(%rsp) /* SS must match SYSRET */
461 jne opportunistic_sysret_failed
462
463 /*
464 * We win! This label is here just for ease of understanding
465 * perf profiles. Nothing jumps here.
466 */
467 syscall_return_via_sysret:
468 CFI_REMEMBER_STATE
469 /* r11 is already restored (see code above) */
470 RESTORE_C_REGS_EXCEPT_R11
471 movq RSP(%rsp),%rsp
472 USERGS_SYSRET64
473 CFI_RESTORE_STATE
474
475 opportunistic_sysret_failed:
476 SWAPGS
477 jmp restore_c_regs_and_iret
478 CFI_ENDPROC
479 END(system_call)
480
481
482 .macro FORK_LIKE func
483 ENTRY(stub_\func)
484 CFI_STARTPROC
485 DEFAULT_FRAME 0, 8 /* offset 8: return address */
486 SAVE_EXTRA_REGS 8
487 call sys_\func
488 ret
489 CFI_ENDPROC
490 END(stub_\func)
491 .endm
492
493 FORK_LIKE clone
494 FORK_LIKE fork
495 FORK_LIKE vfork
496
497 ENTRY(stub_execve)
498 CFI_STARTPROC
499 DEFAULT_FRAME 0, 8
500 call sys_execve
501 return_from_execve:
502 testl %eax, %eax
503 jz 1f
504 /* exec failed, can use fast SYSRET code path in this case */
505 ret
506 1:
507 /* must use IRET code path (pt_regs->cs may have changed) */
508 addq $8, %rsp
509 ZERO_EXTRA_REGS
510 movq %rax,RAX(%rsp)
511 jmp int_ret_from_sys_call
512 CFI_ENDPROC
513 END(stub_execve)
514
515 ENTRY(stub_execveat)
516 CFI_STARTPROC
517 DEFAULT_FRAME 0, 8
518 call sys_execveat
519 jmp return_from_execve
520 CFI_ENDPROC
521 END(stub_execveat)
522
523 /*
524 * sigreturn is special because it needs to restore all registers on return.
525 * This cannot be done with SYSRET, so use the IRET return path instead.
526 */
527 ENTRY(stub_rt_sigreturn)
528 CFI_STARTPROC
529 addq $8, %rsp
530 DEFAULT_FRAME 0
531 SAVE_EXTRA_REGS
532 call sys_rt_sigreturn
533 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
534 RESTORE_EXTRA_REGS
535 jmp int_ret_from_sys_call
536 CFI_ENDPROC
537 END(stub_rt_sigreturn)
538
539 #ifdef CONFIG_X86_X32_ABI
540 ENTRY(stub_x32_rt_sigreturn)
541 CFI_STARTPROC
542 addq $8, %rsp
543 DEFAULT_FRAME 0
544 SAVE_EXTRA_REGS
545 call sys32_x32_rt_sigreturn
546 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
547 RESTORE_EXTRA_REGS
548 jmp int_ret_from_sys_call
549 CFI_ENDPROC
550 END(stub_x32_rt_sigreturn)
551
552 ENTRY(stub_x32_execve)
553 CFI_STARTPROC
554 DEFAULT_FRAME 0, 8
555 call compat_sys_execve
556 jmp return_from_execve
557 CFI_ENDPROC
558 END(stub_x32_execve)
559
560 ENTRY(stub_x32_execveat)
561 CFI_STARTPROC
562 DEFAULT_FRAME 0, 8
563 call compat_sys_execveat
564 jmp return_from_execve
565 CFI_ENDPROC
566 END(stub_x32_execveat)
567
568 #endif
569
570 /*
571 * A newly forked process directly context switches into this address.
572 *
573 * rdi: prev task we switched from
574 */
575 ENTRY(ret_from_fork)
576 DEFAULT_FRAME
577
578 LOCK ; btr $TIF_FORK,TI_flags(%r8)
579
580 pushq_cfi $0x0002
581 popfq_cfi # reset kernel eflags
582
583 call schedule_tail # rdi: 'prev' task parameter
584
585 GET_THREAD_INFO(%rcx)
586
587 RESTORE_EXTRA_REGS
588
589 testl $3,CS(%rsp) # from kernel_thread?
590 jz 1f
591
592 /*
593 * By the time we get here, we have no idea whether our pt_regs,
594 * ti flags, and ti status came from the 64-bit SYSCALL fast path,
595 * the slow path, or one of the ia32entry paths.
596 * Use int_ret_from_sys_call to return, since it can safely handle
597 * all of the above.
598 */
599 jmp int_ret_from_sys_call
600
601 1:
602 movq %rbp, %rdi
603 call *%rbx
604 movl $0, RAX(%rsp)
605 RESTORE_EXTRA_REGS
606 jmp int_ret_from_sys_call
607 CFI_ENDPROC
608 END(ret_from_fork)
609
610 /*
611 * Build the entry stubs with some assembler magic.
612 * We pack 1 stub into every 8-byte block.
613 */
614 .align 8
615 ENTRY(irq_entries_start)
616 INTR_FRAME
617 vector=FIRST_EXTERNAL_VECTOR
618 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
619 pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
620 vector=vector+1
621 jmp common_interrupt
622 CFI_ADJUST_CFA_OFFSET -8
623 .align 8
624 .endr
625 CFI_ENDPROC
626 END(irq_entries_start)
627
628 /*
629 * Interrupt entry/exit.
630 *
631 * Interrupt entry points save only callee clobbered registers in fast path.
632 *
633 * Entry runs with interrupts off.
634 */
635
636 /* 0(%rsp): ~(interrupt number) */
637 .macro interrupt func
638 cld
639 /*
640 * Since nothing in interrupt handling code touches r12...r15 members
641 * of "struct pt_regs", and since interrupts can nest, we can save
642 * four stack slots and simultaneously provide
643 * an unwind-friendly stack layout by saving "truncated" pt_regs
644 * exactly up to rbp slot, without these members.
645 */
646 ALLOC_PT_GPREGS_ON_STACK -RBP
647 SAVE_C_REGS -RBP
648 /* this goes to 0(%rsp) for unwinder, not for saving the value: */
649 SAVE_EXTRA_REGS_RBP -RBP
650
651 leaq -RBP(%rsp),%rdi /* arg1 for \func (pointer to pt_regs) */
652
653 testl $3, CS-RBP(%rsp)
654 je 1f
655 SWAPGS
656 1:
657 /*
658 * Save previous stack pointer, optionally switch to interrupt stack.
659 * irq_count is used to check if a CPU is already on an interrupt stack
660 * or not. While this is essentially redundant with preempt_count it is
661 * a little cheaper to use a separate counter in the PDA (short of
662 * moving irq_enter into assembly, which would be too much work)
663 */
664 movq %rsp, %rsi
665 incl PER_CPU_VAR(irq_count)
666 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
667 CFI_DEF_CFA_REGISTER rsi
668 pushq %rsi
669 /*
670 * For debugger:
671 * "CFA (Current Frame Address) is the value on stack + offset"
672 */
673 CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
674 0x77 /* DW_OP_breg7 (rsp) */, 0, \
675 0x06 /* DW_OP_deref */, \
676 0x08 /* DW_OP_const1u */, SIZEOF_PTREGS-RBP, \
677 0x22 /* DW_OP_plus */
678 /* We entered an interrupt context - irqs are off: */
679 TRACE_IRQS_OFF
680
681 call \func
682 .endm
683
684 /*
685 * The interrupt stubs push (~vector+0x80) onto the stack and
686 * then jump to common_interrupt.
687 */
688 .p2align CONFIG_X86_L1_CACHE_SHIFT
689 common_interrupt:
690 XCPT_FRAME
691 ASM_CLAC
692 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
693 interrupt do_IRQ
694 /* 0(%rsp): old RSP */
695 ret_from_intr:
696 DISABLE_INTERRUPTS(CLBR_NONE)
697 TRACE_IRQS_OFF
698 decl PER_CPU_VAR(irq_count)
699
700 /* Restore saved previous stack */
701 popq %rsi
702 CFI_DEF_CFA rsi,SIZEOF_PTREGS-RBP /* reg/off reset after def_cfa_expr */
703 /* return code expects complete pt_regs - adjust rsp accordingly: */
704 leaq -RBP(%rsi),%rsp
705 CFI_DEF_CFA_REGISTER rsp
706 CFI_ADJUST_CFA_OFFSET RBP
707
708 testl $3,CS(%rsp)
709 je retint_kernel
710 /* Interrupt came from user space */
711
712 GET_THREAD_INFO(%rcx)
713 /*
714 * %rcx: thread info. Interrupts off.
715 */
716 retint_with_reschedule:
717 movl $_TIF_WORK_MASK,%edi
718 retint_check:
719 LOCKDEP_SYS_EXIT_IRQ
720 movl TI_flags(%rcx),%edx
721 andl %edi,%edx
722 CFI_REMEMBER_STATE
723 jnz retint_careful
724
725 retint_swapgs: /* return to user-space */
726 /*
727 * The iretq could re-enable interrupts:
728 */
729 DISABLE_INTERRUPTS(CLBR_ANY)
730 TRACE_IRQS_IRETQ
731
732 SWAPGS
733 jmp restore_c_regs_and_iret
734
735 /* Returning to kernel space */
736 retint_kernel:
737 #ifdef CONFIG_PREEMPT
738 /* Interrupts are off */
739 /* Check if we need preemption */
740 bt $9,EFLAGS(%rsp) /* interrupts were off? */
741 jnc 1f
742 0: cmpl $0,PER_CPU_VAR(__preempt_count)
743 jnz 1f
744 call preempt_schedule_irq
745 jmp 0b
746 1:
747 #endif
748 /*
749 * The iretq could re-enable interrupts:
750 */
751 TRACE_IRQS_IRETQ
752
753 /*
754 * At this label, code paths which return to kernel and to user,
755 * which come from interrupts/exception and from syscalls, merge.
756 */
757 restore_c_regs_and_iret:
758 RESTORE_C_REGS
759 REMOVE_PT_GPREGS_FROM_STACK 8
760
761 irq_return:
762 INTERRUPT_RETURN
763
764 ENTRY(native_iret)
765 /*
766 * Are we returning to a stack segment from the LDT? Note: in
767 * 64-bit mode SS:RSP on the exception stack is always valid.
768 */
769 #ifdef CONFIG_X86_ESPFIX64
770 testb $4,(SS-RIP)(%rsp)
771 jnz native_irq_return_ldt
772 #endif
773
774 .global native_irq_return_iret
775 native_irq_return_iret:
776 /*
777 * This may fault. Non-paranoid faults on return to userspace are
778 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
779 * Double-faults due to espfix64 are handled in do_double_fault.
780 * Other faults here are fatal.
781 */
782 iretq
783
784 #ifdef CONFIG_X86_ESPFIX64
785 native_irq_return_ldt:
786 pushq_cfi %rax
787 pushq_cfi %rdi
788 SWAPGS
789 movq PER_CPU_VAR(espfix_waddr),%rdi
790 movq %rax,(0*8)(%rdi) /* RAX */
791 movq (2*8)(%rsp),%rax /* RIP */
792 movq %rax,(1*8)(%rdi)
793 movq (3*8)(%rsp),%rax /* CS */
794 movq %rax,(2*8)(%rdi)
795 movq (4*8)(%rsp),%rax /* RFLAGS */
796 movq %rax,(3*8)(%rdi)
797 movq (6*8)(%rsp),%rax /* SS */
798 movq %rax,(5*8)(%rdi)
799 movq (5*8)(%rsp),%rax /* RSP */
800 movq %rax,(4*8)(%rdi)
801 andl $0xffff0000,%eax
802 popq_cfi %rdi
803 orq PER_CPU_VAR(espfix_stack),%rax
804 SWAPGS
805 movq %rax,%rsp
806 popq_cfi %rax
807 jmp native_irq_return_iret
808 #endif
809
810 /* edi: workmask, edx: work */
811 retint_careful:
812 CFI_RESTORE_STATE
813 bt $TIF_NEED_RESCHED,%edx
814 jnc retint_signal
815 TRACE_IRQS_ON
816 ENABLE_INTERRUPTS(CLBR_NONE)
817 pushq_cfi %rdi
818 SCHEDULE_USER
819 popq_cfi %rdi
820 GET_THREAD_INFO(%rcx)
821 DISABLE_INTERRUPTS(CLBR_NONE)
822 TRACE_IRQS_OFF
823 jmp retint_check
824
825 retint_signal:
826 testl $_TIF_DO_NOTIFY_MASK,%edx
827 jz retint_swapgs
828 TRACE_IRQS_ON
829 ENABLE_INTERRUPTS(CLBR_NONE)
830 SAVE_EXTRA_REGS
831 movq $-1,ORIG_RAX(%rsp)
832 xorl %esi,%esi # oldset
833 movq %rsp,%rdi # &pt_regs
834 call do_notify_resume
835 RESTORE_EXTRA_REGS
836 DISABLE_INTERRUPTS(CLBR_NONE)
837 TRACE_IRQS_OFF
838 GET_THREAD_INFO(%rcx)
839 jmp retint_with_reschedule
840
841 CFI_ENDPROC
842 END(common_interrupt)
843
844 /*
845 * APIC interrupts.
846 */
847 .macro apicinterrupt3 num sym do_sym
848 ENTRY(\sym)
849 INTR_FRAME
850 ASM_CLAC
851 pushq_cfi $~(\num)
852 .Lcommon_\sym:
853 interrupt \do_sym
854 jmp ret_from_intr
855 CFI_ENDPROC
856 END(\sym)
857 .endm
858
859 #ifdef CONFIG_TRACING
860 #define trace(sym) trace_##sym
861 #define smp_trace(sym) smp_trace_##sym
862
863 .macro trace_apicinterrupt num sym
864 apicinterrupt3 \num trace(\sym) smp_trace(\sym)
865 .endm
866 #else
867 .macro trace_apicinterrupt num sym do_sym
868 .endm
869 #endif
870
871 .macro apicinterrupt num sym do_sym
872 apicinterrupt3 \num \sym \do_sym
873 trace_apicinterrupt \num \sym
874 .endm
875
876 #ifdef CONFIG_SMP
877 apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR \
878 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
879 apicinterrupt3 REBOOT_VECTOR \
880 reboot_interrupt smp_reboot_interrupt
881 #endif
882
883 #ifdef CONFIG_X86_UV
884 apicinterrupt3 UV_BAU_MESSAGE \
885 uv_bau_message_intr1 uv_bau_message_interrupt
886 #endif
887 apicinterrupt LOCAL_TIMER_VECTOR \
888 apic_timer_interrupt smp_apic_timer_interrupt
889 apicinterrupt X86_PLATFORM_IPI_VECTOR \
890 x86_platform_ipi smp_x86_platform_ipi
891
892 #ifdef CONFIG_HAVE_KVM
893 apicinterrupt3 POSTED_INTR_VECTOR \
894 kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
895 #endif
896
897 #ifdef CONFIG_X86_MCE_THRESHOLD
898 apicinterrupt THRESHOLD_APIC_VECTOR \
899 threshold_interrupt smp_threshold_interrupt
900 #endif
901
902 #ifdef CONFIG_X86_THERMAL_VECTOR
903 apicinterrupt THERMAL_APIC_VECTOR \
904 thermal_interrupt smp_thermal_interrupt
905 #endif
906
907 #ifdef CONFIG_SMP
908 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
909 call_function_single_interrupt smp_call_function_single_interrupt
910 apicinterrupt CALL_FUNCTION_VECTOR \
911 call_function_interrupt smp_call_function_interrupt
912 apicinterrupt RESCHEDULE_VECTOR \
913 reschedule_interrupt smp_reschedule_interrupt
914 #endif
915
916 apicinterrupt ERROR_APIC_VECTOR \
917 error_interrupt smp_error_interrupt
918 apicinterrupt SPURIOUS_APIC_VECTOR \
919 spurious_interrupt smp_spurious_interrupt
920
921 #ifdef CONFIG_IRQ_WORK
922 apicinterrupt IRQ_WORK_VECTOR \
923 irq_work_interrupt smp_irq_work_interrupt
924 #endif
925
926 /*
927 * Exception entry points.
928 */
929 #define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
930
931 .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
932 ENTRY(\sym)
933 /* Sanity check */
934 .if \shift_ist != -1 && \paranoid == 0
935 .error "using shift_ist requires paranoid=1"
936 .endif
937
938 .if \has_error_code
939 XCPT_FRAME
940 .else
941 INTR_FRAME
942 .endif
943
944 ASM_CLAC
945 PARAVIRT_ADJUST_EXCEPTION_FRAME
946
947 .ifeq \has_error_code
948 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
949 .endif
950
951 ALLOC_PT_GPREGS_ON_STACK
952
953 .if \paranoid
954 .if \paranoid == 1
955 CFI_REMEMBER_STATE
956 testl $3, CS(%rsp) /* If coming from userspace, switch */
957 jnz 1f /* stacks. */
958 .endif
959 call paranoid_entry
960 .else
961 call error_entry
962 .endif
963 /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
964
965 DEFAULT_FRAME 0
966
967 .if \paranoid
968 .if \shift_ist != -1
969 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
970 .else
971 TRACE_IRQS_OFF
972 .endif
973 .endif
974
975 movq %rsp,%rdi /* pt_regs pointer */
976
977 .if \has_error_code
978 movq ORIG_RAX(%rsp),%rsi /* get error code */
979 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
980 .else
981 xorl %esi,%esi /* no error code */
982 .endif
983
984 .if \shift_ist != -1
985 subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
986 .endif
987
988 call \do_sym
989
990 .if \shift_ist != -1
991 addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
992 .endif
993
994 /* these procedures expect "no swapgs" flag in ebx */
995 .if \paranoid
996 jmp paranoid_exit
997 .else
998 jmp error_exit
999 .endif
1000
1001 .if \paranoid == 1
1002 CFI_RESTORE_STATE
1003 /*
1004 * Paranoid entry from userspace. Switch stacks and treat it
1005 * as a normal entry. This means that paranoid handlers
1006 * run in real process context if user_mode(regs).
1007 */
1008 1:
1009 call error_entry
1010
1011 DEFAULT_FRAME 0
1012
1013 movq %rsp,%rdi /* pt_regs pointer */
1014 call sync_regs
1015 movq %rax,%rsp /* switch stack */
1016
1017 movq %rsp,%rdi /* pt_regs pointer */
1018
1019 .if \has_error_code
1020 movq ORIG_RAX(%rsp),%rsi /* get error code */
1021 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
1022 .else
1023 xorl %esi,%esi /* no error code */
1024 .endif
1025
1026 call \do_sym
1027
1028 jmp error_exit /* %ebx: no swapgs flag */
1029 .endif
1030
1031 CFI_ENDPROC
1032 END(\sym)
1033 .endm
1034
1035 #ifdef CONFIG_TRACING
1036 .macro trace_idtentry sym do_sym has_error_code:req
1037 idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
1038 idtentry \sym \do_sym has_error_code=\has_error_code
1039 .endm
1040 #else
1041 .macro trace_idtentry sym do_sym has_error_code:req
1042 idtentry \sym \do_sym has_error_code=\has_error_code
1043 .endm
1044 #endif
1045
1046 idtentry divide_error do_divide_error has_error_code=0
1047 idtentry overflow do_overflow has_error_code=0
1048 idtentry bounds do_bounds has_error_code=0
1049 idtentry invalid_op do_invalid_op has_error_code=0
1050 idtentry device_not_available do_device_not_available has_error_code=0
1051 idtentry double_fault do_double_fault has_error_code=1 paranoid=2
1052 idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
1053 idtentry invalid_TSS do_invalid_TSS has_error_code=1
1054 idtentry segment_not_present do_segment_not_present has_error_code=1
1055 idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
1056 idtentry coprocessor_error do_coprocessor_error has_error_code=0
1057 idtentry alignment_check do_alignment_check has_error_code=1
1058 idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
1059
1060
1061 /* Reload gs selector with exception handling */
1062 /* edi: new selector */
1063 ENTRY(native_load_gs_index)
1064 CFI_STARTPROC
1065 pushfq_cfi
1066 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1067 SWAPGS
1068 gs_change:
1069 movl %edi,%gs
1070 2: mfence /* workaround */
1071 SWAPGS
1072 popfq_cfi
1073 ret
1074 CFI_ENDPROC
1075 END(native_load_gs_index)
1076
1077 _ASM_EXTABLE(gs_change,bad_gs)
1078 .section .fixup,"ax"
1079 /* running with kernelgs */
1080 bad_gs:
1081 SWAPGS /* switch back to user gs */
1082 xorl %eax,%eax
1083 movl %eax,%gs
1084 jmp 2b
1085 .previous
1086
1087 /* Call softirq on interrupt stack. Interrupts are off. */
1088 ENTRY(do_softirq_own_stack)
1089 CFI_STARTPROC
1090 pushq_cfi %rbp
1091 CFI_REL_OFFSET rbp,0
1092 mov %rsp,%rbp
1093 CFI_DEF_CFA_REGISTER rbp
1094 incl PER_CPU_VAR(irq_count)
1095 cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1096 push %rbp # backlink for old unwinder
1097 call __do_softirq
1098 leaveq
1099 CFI_RESTORE rbp
1100 CFI_DEF_CFA_REGISTER rsp
1101 CFI_ADJUST_CFA_OFFSET -8
1102 decl PER_CPU_VAR(irq_count)
1103 ret
1104 CFI_ENDPROC
1105 END(do_softirq_own_stack)
1106
1107 #ifdef CONFIG_XEN
1108 idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
1109
1110 /*
1111 * A note on the "critical region" in our callback handler.
1112 * We want to avoid stacking callback handlers due to events occurring
1113 * during handling of the last event. To do this, we keep events disabled
1114 * until we've done all processing. HOWEVER, we must enable events before
1115 * popping the stack frame (can't be done atomically) and so it would still
1116 * be possible to get enough handler activations to overflow the stack.
1117 * Although unlikely, bugs of that kind are hard to track down, so we'd
1118 * like to avoid the possibility.
1119 * So, on entry to the handler we detect whether we interrupted an
1120 * existing activation in its critical region -- if so, we pop the current
1121 * activation and restart the handler using the previous one.
1122 */
1123 ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1124 CFI_STARTPROC
1125 /*
1126 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1127 * see the correct pointer to the pt_regs
1128 */
1129 movq %rdi, %rsp # we don't return, adjust the stack frame
1130 CFI_ENDPROC
1131 DEFAULT_FRAME
1132 11: incl PER_CPU_VAR(irq_count)
1133 movq %rsp,%rbp
1134 CFI_DEF_CFA_REGISTER rbp
1135 cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1136 pushq %rbp # backlink for old unwinder
1137 call xen_evtchn_do_upcall
1138 popq %rsp
1139 CFI_DEF_CFA_REGISTER rsp
1140 decl PER_CPU_VAR(irq_count)
1141 #ifndef CONFIG_PREEMPT
1142 call xen_maybe_preempt_hcall
1143 #endif
1144 jmp error_exit
1145 CFI_ENDPROC
1146 END(xen_do_hypervisor_callback)
1147
1148 /*
1149 * Hypervisor uses this for application faults while it executes.
1150 * We get here for two reasons:
1151 * 1. Fault while reloading DS, ES, FS or GS
1152 * 2. Fault while executing IRET
1153 * Category 1 we do not need to fix up as Xen has already reloaded all segment
1154 * registers that could be reloaded and zeroed the others.
1155 * Category 2 we fix up by killing the current process. We cannot use the
1156 * normal Linux return path in this case because if we use the IRET hypercall
1157 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1158 * We distinguish between categories by comparing each saved segment register
1159 * with its current contents: any discrepancy means we in category 1.
1160 */
1161 ENTRY(xen_failsafe_callback)
1162 INTR_FRAME 1 (6*8)
1163 /*CFI_REL_OFFSET gs,GS*/
1164 /*CFI_REL_OFFSET fs,FS*/
1165 /*CFI_REL_OFFSET es,ES*/
1166 /*CFI_REL_OFFSET ds,DS*/
1167 CFI_REL_OFFSET r11,8
1168 CFI_REL_OFFSET rcx,0
1169 movw %ds,%cx
1170 cmpw %cx,0x10(%rsp)
1171 CFI_REMEMBER_STATE
1172 jne 1f
1173 movw %es,%cx
1174 cmpw %cx,0x18(%rsp)
1175 jne 1f
1176 movw %fs,%cx
1177 cmpw %cx,0x20(%rsp)
1178 jne 1f
1179 movw %gs,%cx
1180 cmpw %cx,0x28(%rsp)
1181 jne 1f
1182 /* All segments match their saved values => Category 2 (Bad IRET). */
1183 movq (%rsp),%rcx
1184 CFI_RESTORE rcx
1185 movq 8(%rsp),%r11
1186 CFI_RESTORE r11
1187 addq $0x30,%rsp
1188 CFI_ADJUST_CFA_OFFSET -0x30
1189 pushq_cfi $0 /* RIP */
1190 pushq_cfi %r11
1191 pushq_cfi %rcx
1192 jmp general_protection
1193 CFI_RESTORE_STATE
1194 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1195 movq (%rsp),%rcx
1196 CFI_RESTORE rcx
1197 movq 8(%rsp),%r11
1198 CFI_RESTORE r11
1199 addq $0x30,%rsp
1200 CFI_ADJUST_CFA_OFFSET -0x30
1201 pushq_cfi $-1 /* orig_ax = -1 => not a system call */
1202 ALLOC_PT_GPREGS_ON_STACK
1203 SAVE_C_REGS
1204 SAVE_EXTRA_REGS
1205 jmp error_exit
1206 CFI_ENDPROC
1207 END(xen_failsafe_callback)
1208
1209 apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1210 xen_hvm_callback_vector xen_evtchn_do_upcall
1211
1212 #endif /* CONFIG_XEN */
1213
1214 #if IS_ENABLED(CONFIG_HYPERV)
1215 apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1216 hyperv_callback_vector hyperv_vector_handler
1217 #endif /* CONFIG_HYPERV */
1218
1219 idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1220 idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1221 idtentry stack_segment do_stack_segment has_error_code=1
1222 #ifdef CONFIG_XEN
1223 idtentry xen_debug do_debug has_error_code=0
1224 idtentry xen_int3 do_int3 has_error_code=0
1225 idtentry xen_stack_segment do_stack_segment has_error_code=1
1226 #endif
1227 idtentry general_protection do_general_protection has_error_code=1
1228 trace_idtentry page_fault do_page_fault has_error_code=1
1229 #ifdef CONFIG_KVM_GUEST
1230 idtentry async_page_fault do_async_page_fault has_error_code=1
1231 #endif
1232 #ifdef CONFIG_X86_MCE
1233 idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
1234 #endif
1235
1236 /*
1237 * Save all registers in pt_regs, and switch gs if needed.
1238 * Use slow, but surefire "are we in kernel?" check.
1239 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1240 */
1241 ENTRY(paranoid_entry)
1242 XCPT_FRAME 1 15*8
1243 cld
1244 SAVE_C_REGS 8
1245 SAVE_EXTRA_REGS 8
1246 movl $1,%ebx
1247 movl $MSR_GS_BASE,%ecx
1248 rdmsr
1249 testl %edx,%edx
1250 js 1f /* negative -> in kernel */
1251 SWAPGS
1252 xorl %ebx,%ebx
1253 1: ret
1254 CFI_ENDPROC
1255 END(paranoid_entry)
1256
1257 /*
1258 * "Paranoid" exit path from exception stack. This is invoked
1259 * only on return from non-NMI IST interrupts that came
1260 * from kernel space.
1261 *
1262 * We may be returning to very strange contexts (e.g. very early
1263 * in syscall entry), so checking for preemption here would
1264 * be complicated. Fortunately, we there's no good reason
1265 * to try to handle preemption here.
1266 */
1267 /* On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) */
1268 ENTRY(paranoid_exit)
1269 DEFAULT_FRAME
1270 DISABLE_INTERRUPTS(CLBR_NONE)
1271 TRACE_IRQS_OFF_DEBUG
1272 testl %ebx,%ebx /* swapgs needed? */
1273 jnz paranoid_exit_no_swapgs
1274 TRACE_IRQS_IRETQ
1275 SWAPGS_UNSAFE_STACK
1276 jmp paranoid_exit_restore
1277 paranoid_exit_no_swapgs:
1278 TRACE_IRQS_IRETQ_DEBUG
1279 paranoid_exit_restore:
1280 RESTORE_EXTRA_REGS
1281 RESTORE_C_REGS
1282 REMOVE_PT_GPREGS_FROM_STACK 8
1283 INTERRUPT_RETURN
1284 CFI_ENDPROC
1285 END(paranoid_exit)
1286
1287 /*
1288 * Save all registers in pt_regs, and switch gs if needed.
1289 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1290 */
1291 ENTRY(error_entry)
1292 XCPT_FRAME 1 15*8
1293 cld
1294 SAVE_C_REGS 8
1295 SAVE_EXTRA_REGS 8
1296 xorl %ebx,%ebx
1297 testl $3,CS+8(%rsp)
1298 je error_kernelspace
1299 error_swapgs:
1300 SWAPGS
1301 error_sti:
1302 TRACE_IRQS_OFF
1303 ret
1304
1305 /*
1306 * There are two places in the kernel that can potentially fault with
1307 * usergs. Handle them here. B stepping K8s sometimes report a
1308 * truncated RIP for IRET exceptions returning to compat mode. Check
1309 * for these here too.
1310 */
1311 error_kernelspace:
1312 CFI_REL_OFFSET rcx, RCX+8
1313 incl %ebx
1314 leaq native_irq_return_iret(%rip),%rcx
1315 cmpq %rcx,RIP+8(%rsp)
1316 je error_bad_iret
1317 movl %ecx,%eax /* zero extend */
1318 cmpq %rax,RIP+8(%rsp)
1319 je bstep_iret
1320 cmpq $gs_change,RIP+8(%rsp)
1321 je error_swapgs
1322 jmp error_sti
1323
1324 bstep_iret:
1325 /* Fix truncated RIP */
1326 movq %rcx,RIP+8(%rsp)
1327 /* fall through */
1328
1329 error_bad_iret:
1330 SWAPGS
1331 mov %rsp,%rdi
1332 call fixup_bad_iret
1333 mov %rax,%rsp
1334 decl %ebx /* Return to usergs */
1335 jmp error_sti
1336 CFI_ENDPROC
1337 END(error_entry)
1338
1339
1340 /* On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) */
1341 ENTRY(error_exit)
1342 DEFAULT_FRAME
1343 movl %ebx,%eax
1344 RESTORE_EXTRA_REGS
1345 DISABLE_INTERRUPTS(CLBR_NONE)
1346 TRACE_IRQS_OFF
1347 GET_THREAD_INFO(%rcx)
1348 testl %eax,%eax
1349 jne retint_kernel
1350 LOCKDEP_SYS_EXIT_IRQ
1351 movl TI_flags(%rcx),%edx
1352 movl $_TIF_WORK_MASK,%edi
1353 andl %edi,%edx
1354 jnz retint_careful
1355 jmp retint_swapgs
1356 CFI_ENDPROC
1357 END(error_exit)
1358
1359 /* Runs on exception stack */
1360 ENTRY(nmi)
1361 INTR_FRAME
1362 PARAVIRT_ADJUST_EXCEPTION_FRAME
1363 /*
1364 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1365 * the iretq it performs will take us out of NMI context.
1366 * This means that we can have nested NMIs where the next
1367 * NMI is using the top of the stack of the previous NMI. We
1368 * can't let it execute because the nested NMI will corrupt the
1369 * stack of the previous NMI. NMI handlers are not re-entrant
1370 * anyway.
1371 *
1372 * To handle this case we do the following:
1373 * Check the a special location on the stack that contains
1374 * a variable that is set when NMIs are executing.
1375 * The interrupted task's stack is also checked to see if it
1376 * is an NMI stack.
1377 * If the variable is not set and the stack is not the NMI
1378 * stack then:
1379 * o Set the special variable on the stack
1380 * o Copy the interrupt frame into a "saved" location on the stack
1381 * o Copy the interrupt frame into a "copy" location on the stack
1382 * o Continue processing the NMI
1383 * If the variable is set or the previous stack is the NMI stack:
1384 * o Modify the "copy" location to jump to the repeate_nmi
1385 * o return back to the first NMI
1386 *
1387 * Now on exit of the first NMI, we first clear the stack variable
1388 * The NMI stack will tell any nested NMIs at that point that it is
1389 * nested. Then we pop the stack normally with iret, and if there was
1390 * a nested NMI that updated the copy interrupt stack frame, a
1391 * jump will be made to the repeat_nmi code that will handle the second
1392 * NMI.
1393 */
1394
1395 /* Use %rdx as our temp variable throughout */
1396 pushq_cfi %rdx
1397 CFI_REL_OFFSET rdx, 0
1398
1399 /*
1400 * If %cs was not the kernel segment, then the NMI triggered in user
1401 * space, which means it is definitely not nested.
1402 */
1403 cmpl $__KERNEL_CS, 16(%rsp)
1404 jne first_nmi
1405
1406 /*
1407 * Check the special variable on the stack to see if NMIs are
1408 * executing.
1409 */
1410 cmpl $1, -8(%rsp)
1411 je nested_nmi
1412
1413 /*
1414 * Now test if the previous stack was an NMI stack.
1415 * We need the double check. We check the NMI stack to satisfy the
1416 * race when the first NMI clears the variable before returning.
1417 * We check the variable because the first NMI could be in a
1418 * breakpoint routine using a breakpoint stack.
1419 */
1420 lea 6*8(%rsp), %rdx
1421 /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1422 cmpq %rdx, 4*8(%rsp)
1423 /* If the stack pointer is above the NMI stack, this is a normal NMI */
1424 ja first_nmi
1425 subq $EXCEPTION_STKSZ, %rdx
1426 cmpq %rdx, 4*8(%rsp)
1427 /* If it is below the NMI stack, it is a normal NMI */
1428 jb first_nmi
1429 /* Ah, it is within the NMI stack, treat it as nested */
1430 jmp nested_nmi
1431
1432 CFI_REMEMBER_STATE
1433
1434 nested_nmi:
1435 /*
1436 * Do nothing if we interrupted the fixup in repeat_nmi.
1437 * It's about to repeat the NMI handler, so we are fine
1438 * with ignoring this one.
1439 */
1440 movq $repeat_nmi, %rdx
1441 cmpq 8(%rsp), %rdx
1442 ja 1f
1443 movq $end_repeat_nmi, %rdx
1444 cmpq 8(%rsp), %rdx
1445 ja nested_nmi_out
1446
1447 1:
1448 /* Set up the interrupted NMIs stack to jump to repeat_nmi */
1449 leaq -1*8(%rsp), %rdx
1450 movq %rdx, %rsp
1451 CFI_ADJUST_CFA_OFFSET 1*8
1452 leaq -10*8(%rsp), %rdx
1453 pushq_cfi $__KERNEL_DS
1454 pushq_cfi %rdx
1455 pushfq_cfi
1456 pushq_cfi $__KERNEL_CS
1457 pushq_cfi $repeat_nmi
1458
1459 /* Put stack back */
1460 addq $(6*8), %rsp
1461 CFI_ADJUST_CFA_OFFSET -6*8
1462
1463 nested_nmi_out:
1464 popq_cfi %rdx
1465 CFI_RESTORE rdx
1466
1467 /* No need to check faults here */
1468 INTERRUPT_RETURN
1469
1470 CFI_RESTORE_STATE
1471 first_nmi:
1472 /*
1473 * Because nested NMIs will use the pushed location that we
1474 * stored in rdx, we must keep that space available.
1475 * Here's what our stack frame will look like:
1476 * +-------------------------+
1477 * | original SS |
1478 * | original Return RSP |
1479 * | original RFLAGS |
1480 * | original CS |
1481 * | original RIP |
1482 * +-------------------------+
1483 * | temp storage for rdx |
1484 * +-------------------------+
1485 * | NMI executing variable |
1486 * +-------------------------+
1487 * | copied SS |
1488 * | copied Return RSP |
1489 * | copied RFLAGS |
1490 * | copied CS |
1491 * | copied RIP |
1492 * +-------------------------+
1493 * | Saved SS |
1494 * | Saved Return RSP |
1495 * | Saved RFLAGS |
1496 * | Saved CS |
1497 * | Saved RIP |
1498 * +-------------------------+
1499 * | pt_regs |
1500 * +-------------------------+
1501 *
1502 * The saved stack frame is used to fix up the copied stack frame
1503 * that a nested NMI may change to make the interrupted NMI iret jump
1504 * to the repeat_nmi. The original stack frame and the temp storage
1505 * is also used by nested NMIs and can not be trusted on exit.
1506 */
1507 /* Do not pop rdx, nested NMIs will corrupt that part of the stack */
1508 movq (%rsp), %rdx
1509 CFI_RESTORE rdx
1510
1511 /* Set the NMI executing variable on the stack. */
1512 pushq_cfi $1
1513
1514 /*
1515 * Leave room for the "copied" frame
1516 */
1517 subq $(5*8), %rsp
1518 CFI_ADJUST_CFA_OFFSET 5*8
1519
1520 /* Copy the stack frame to the Saved frame */
1521 .rept 5
1522 pushq_cfi 11*8(%rsp)
1523 .endr
1524 CFI_DEF_CFA_OFFSET 5*8
1525
1526 /* Everything up to here is safe from nested NMIs */
1527
1528 /*
1529 * If there was a nested NMI, the first NMI's iret will return
1530 * here. But NMIs are still enabled and we can take another
1531 * nested NMI. The nested NMI checks the interrupted RIP to see
1532 * if it is between repeat_nmi and end_repeat_nmi, and if so
1533 * it will just return, as we are about to repeat an NMI anyway.
1534 * This makes it safe to copy to the stack frame that a nested
1535 * NMI will update.
1536 */
1537 repeat_nmi:
1538 /*
1539 * Update the stack variable to say we are still in NMI (the update
1540 * is benign for the non-repeat case, where 1 was pushed just above
1541 * to this very stack slot).
1542 */
1543 movq $1, 10*8(%rsp)
1544
1545 /* Make another copy, this one may be modified by nested NMIs */
1546 addq $(10*8), %rsp
1547 CFI_ADJUST_CFA_OFFSET -10*8
1548 .rept 5
1549 pushq_cfi -6*8(%rsp)
1550 .endr
1551 subq $(5*8), %rsp
1552 CFI_DEF_CFA_OFFSET 5*8
1553 end_repeat_nmi:
1554
1555 /*
1556 * Everything below this point can be preempted by a nested
1557 * NMI if the first NMI took an exception and reset our iret stack
1558 * so that we repeat another NMI.
1559 */
1560 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
1561 ALLOC_PT_GPREGS_ON_STACK
1562
1563 /*
1564 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1565 * as we should not be calling schedule in NMI context.
1566 * Even with normal interrupts enabled. An NMI should not be
1567 * setting NEED_RESCHED or anything that normal interrupts and
1568 * exceptions might do.
1569 */
1570 call paranoid_entry
1571 DEFAULT_FRAME 0
1572
1573 /*
1574 * Save off the CR2 register. If we take a page fault in the NMI then
1575 * it could corrupt the CR2 value. If the NMI preempts a page fault
1576 * handler before it was able to read the CR2 register, and then the
1577 * NMI itself takes a page fault, the page fault that was preempted
1578 * will read the information from the NMI page fault and not the
1579 * origin fault. Save it off and restore it if it changes.
1580 * Use the r12 callee-saved register.
1581 */
1582 movq %cr2, %r12
1583
1584 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1585 movq %rsp,%rdi
1586 movq $-1,%rsi
1587 call do_nmi
1588
1589 /* Did the NMI take a page fault? Restore cr2 if it did */
1590 movq %cr2, %rcx
1591 cmpq %rcx, %r12
1592 je 1f
1593 movq %r12, %cr2
1594 1:
1595
1596 testl %ebx,%ebx /* swapgs needed? */
1597 jnz nmi_restore
1598 nmi_swapgs:
1599 SWAPGS_UNSAFE_STACK
1600 nmi_restore:
1601 RESTORE_EXTRA_REGS
1602 RESTORE_C_REGS
1603 /* Pop the extra iret frame at once */
1604 REMOVE_PT_GPREGS_FROM_STACK 6*8
1605
1606 /* Clear the NMI executing stack variable */
1607 movq $0, 5*8(%rsp)
1608 jmp irq_return
1609 CFI_ENDPROC
1610 END(nmi)
1611
1612 ENTRY(ignore_sysret)
1613 CFI_STARTPROC
1614 mov $-ENOSYS,%eax
1615 sysret
1616 CFI_ENDPROC
1617 END(ignore_sysret)
1618
This page took 0.064577 seconds and 5 git commands to generate.