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