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