entry_64.S 41.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 *  linux/arch/x86_64/entry.S
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *  Copyright (C) 2000, 2001, 2002  Andi Kleen SuSE Labs
 *  Copyright (C) 2000  Pavel Machek <pavel@suse.cz>
 */

/*
 * entry.S contains the system-call and fault low-level handling routines.
 *
12 13
 * Some of this is documented in Documentation/x86/entry_64.txt
 *
L
Linus Torvalds 已提交
14 15
 * NOTE: This code handles signal-recognition, which happens every time
 * after an interrupt and after each system call.
16 17
 *
 * A note on terminology:
18
 * - iret frame: Architecture defined interrupt frame from SS to RIP
19
 * at the top of the kernel process stack.
20 21 22 23 24 25
 *
 * Some macro usage:
 * - CFI macros are used to generate dwarf2 unwind information for better
 * backtraces. They don't change any code.
 * - ENTRY/END Define functions in the symbol table.
 * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
26
 * - idtentry - Define exception entry points.
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34
 */

#include <linux/linkage.h>
#include <asm/segment.h>
#include <asm/cache.h>
#include <asm/errno.h>
#include <asm/dwarf2.h>
#include <asm/calling.h>
35
#include <asm/asm-offsets.h>
L
Linus Torvalds 已提交
36 37 38 39
#include <asm/msr.h>
#include <asm/unistd.h>
#include <asm/thread_info.h>
#include <asm/hw_irq.h>
40
#include <asm/page_types.h>
41
#include <asm/irqflags.h>
42
#include <asm/paravirt.h>
43
#include <asm/percpu.h>
44
#include <asm/asm.h>
45
#include <asm/context_tracking.h>
46
#include <asm/smap.h>
47
#include <asm/pgtable_types.h>
48
#include <linux/err.h>
L
Linus Torvalds 已提交
49

R
Roland McGrath 已提交
50 51 52 53 54 55
/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this.  */
#include <linux/elf-em.h>
#define AUDIT_ARCH_X86_64	(EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
#define __AUDIT_ARCH_64BIT 0x80000000
#define __AUDIT_ARCH_LE	   0x40000000

L
Linus Torvalds 已提交
56
	.code64
J
Jiri Olsa 已提交
57 58
	.section .entry.text, "ax"

59

60
#ifndef CONFIG_PREEMPT
L
Linus Torvalds 已提交
61
#define retint_kernel retint_restore_args
62
#endif
63

64
#ifdef CONFIG_PARAVIRT
65
ENTRY(native_usergs_sysret64)
66 67
	swapgs
	sysretq
68
ENDPROC(native_usergs_sysret64)
69 70
#endif /* CONFIG_PARAVIRT */

71

72
.macro TRACE_IRQS_IRETQ
73
#ifdef CONFIG_TRACE_IRQFLAGS
74
	bt   $9,EFLAGS(%rsp)	/* interrupts off? */
75 76 77 78 79 80
	jnc  1f
	TRACE_IRQS_ON
1:
#endif
.endm

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/*
 * When dynamic function tracer is enabled it will add a breakpoint
 * to all locations that it is about to modify, sync CPUs, update
 * all the code, sync CPUs, then remove the breakpoints. In this time
 * if lockdep is enabled, it might jump back into the debug handler
 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
 *
 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
 * make sure the stack pointer does not get reset back to the top
 * of the debug stack, and instead just reuses the current stack.
 */
#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)

.macro TRACE_IRQS_OFF_DEBUG
	call debug_stack_set_zero
	TRACE_IRQS_OFF
	call debug_stack_reset
.endm

.macro TRACE_IRQS_ON_DEBUG
	call debug_stack_set_zero
	TRACE_IRQS_ON
	call debug_stack_reset
.endm

106 107
.macro TRACE_IRQS_IRETQ_DEBUG
	bt   $9,EFLAGS(%rsp)	/* interrupts off? */
108 109 110 111 112 113 114 115 116 117 118
	jnc  1f
	TRACE_IRQS_ON_DEBUG
1:
.endm

#else
# define TRACE_IRQS_OFF_DEBUG		TRACE_IRQS_OFF
# define TRACE_IRQS_ON_DEBUG		TRACE_IRQS_ON
# define TRACE_IRQS_IRETQ_DEBUG		TRACE_IRQS_IRETQ
#endif

119
/*
D
Denys Vlasenko 已提交
120
 * empty frame
121 122
 */
	.macro EMPTY_FRAME start=1 offset=0
123
	.if \start
124
	CFI_STARTPROC simple
125
	CFI_SIGNAL_FRAME
126
	CFI_DEF_CFA rsp,8+\offset
127
	.else
128
	CFI_DEF_CFA_OFFSET 8+\offset
129
	.endif
L
Linus Torvalds 已提交
130
	.endm
131 132

/*
133
 * initial frame state for interrupts (and exceptions without error code)
134
 */
135
	.macro INTR_FRAME start=1 offset=0
136 137 138 139 140 141
	EMPTY_FRAME \start, 5*8+\offset
	/*CFI_REL_OFFSET ss, 4*8+\offset*/
	CFI_REL_OFFSET rsp, 3*8+\offset
	/*CFI_REL_OFFSET rflags, 2*8+\offset*/
	/*CFI_REL_OFFSET cs, 1*8+\offset*/
	CFI_REL_OFFSET rip, 0*8+\offset
142 143 144 145 146 147
	.endm

/*
 * initial frame state for exceptions with error code (and interrupts
 * with vector already pushed)
 */
148
	.macro XCPT_FRAME start=1 offset=0
149
	INTR_FRAME \start, 1*8+\offset
150 151 152
	.endm

/*
153
 * frame that enables passing a complete pt_regs to a C function.
154
 */
155
	.macro DEFAULT_FRAME start=1 offset=0
156 157 158 159 160 161 162 163 164 165
	XCPT_FRAME \start, ORIG_RAX+\offset
	CFI_REL_OFFSET rdi, RDI+\offset
	CFI_REL_OFFSET rsi, RSI+\offset
	CFI_REL_OFFSET rdx, RDX+\offset
	CFI_REL_OFFSET rcx, RCX+\offset
	CFI_REL_OFFSET rax, RAX+\offset
	CFI_REL_OFFSET r8, R8+\offset
	CFI_REL_OFFSET r9, R9+\offset
	CFI_REL_OFFSET r10, R10+\offset
	CFI_REL_OFFSET r11, R11+\offset
166 167 168 169 170 171 172
	CFI_REL_OFFSET rbx, RBX+\offset
	CFI_REL_OFFSET rbp, RBP+\offset
	CFI_REL_OFFSET r12, R12+\offset
	CFI_REL_OFFSET r13, R13+\offset
	CFI_REL_OFFSET r14, R14+\offset
	CFI_REL_OFFSET r15, R15+\offset
	.endm
173

L
Linus Torvalds 已提交
174
/*
175
 * 64bit SYSCALL instruction entry. Up to 6 arguments in registers.
L
Linus Torvalds 已提交
176
 *
177 178 179 180 181 182 183
 * 64bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
 * then loads new ss, cs, and rip from previously programmed MSRs.
 * rflags gets masked by a value from another MSR (so CLD and CLAC
 * are not needed). SYSCALL does not save anything on the stack
 * and does not change rsp.
 *
 * Registers on entry:
L
Linus Torvalds 已提交
184
 * rax  system call number
185 186
 * rcx  return address
 * r11  saved rflags (note: r11 is callee-clobbered register in C ABI)
L
Linus Torvalds 已提交
187 188
 * rdi  arg0
 * rsi  arg1
189
 * rdx  arg2
190
 * r10  arg3 (needs to be moved to rcx to conform to C ABI)
L
Linus Torvalds 已提交
191 192
 * r8   arg4
 * r9   arg5
193
 * (note: r12-r15,rbp,rbx are callee-preserved in C ABI)
194
 *
L
Linus Torvalds 已提交
195 196
 * Only called from user space.
 *
197
 * When user can change pt_regs->foo always force IRET. That is because
198 199
 * it deals with uncanonical addresses better. SYSRET has trouble
 * with them due to bugs in both AMD and Intel CPUs.
200
 */
L
Linus Torvalds 已提交
201 202

ENTRY(system_call)
203
	CFI_STARTPROC	simple
204
	CFI_SIGNAL_FRAME
205
	CFI_DEF_CFA	rsp,0
206 207
	CFI_REGISTER	rip,rcx
	/*CFI_REGISTER	rflags,r11*/
208 209 210 211 212 213

	/*
	 * Interrupts are off on entry.
	 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
	 * it is too small to ever cause noticeable irq latency.
	 */
214 215 216 217 218 219
	SWAPGS_UNSAFE_STACK
	/*
	 * A hypervisor implementation might want to use a label
	 * after the swapgs, so that it can do the swapgs
	 * for the guest and jump here on syscall.
	 */
220
GLOBAL(system_call_after_swapgs)
221

222
	movq	%rsp,PER_CPU_VAR(rsp_scratch)
223
	movq	PER_CPU_VAR(kernel_stack),%rsp
224 225 226 227

	/* Construct struct pt_regs on stack */
	pushq_cfi $__USER_DS			/* pt_regs->ss */
	pushq_cfi PER_CPU_VAR(rsp_scratch)	/* pt_regs->sp */
228
	/*
229 230 231 232 233
	 * Re-enable interrupts.
	 * We use 'rsp_scratch' as a scratch space, hence irq-off block above
	 * must execute atomically in the face of possible interrupt-driven
	 * task preemption. We must enable interrupts only after we're done
	 * with using rsp_scratch:
234 235
	 */
	ENABLE_INTERRUPTS(CLBR_NONE)
236 237 238 239 240 241 242 243 244 245 246 247 248
	pushq_cfi	%r11			/* pt_regs->flags */
	pushq_cfi	$__USER_CS		/* pt_regs->cs */
	pushq_cfi	%rcx			/* pt_regs->ip */
	CFI_REL_OFFSET rip,0
	pushq_cfi_reg	rax			/* pt_regs->orig_ax */
	pushq_cfi_reg	rdi			/* pt_regs->di */
	pushq_cfi_reg	rsi			/* pt_regs->si */
	pushq_cfi_reg	rdx			/* pt_regs->dx */
	pushq_cfi_reg	rcx			/* pt_regs->cx */
	pushq_cfi	$-ENOSYS		/* pt_regs->ax */
	pushq_cfi_reg	r8			/* pt_regs->r8 */
	pushq_cfi_reg	r9			/* pt_regs->r9 */
	pushq_cfi_reg	r10			/* pt_regs->r10 */
249 250
	pushq_cfi_reg	r11			/* pt_regs->r11 */
	sub	$(6*8),%rsp /* pt_regs->bp,bx,r12-15 not saved */
251

252
	testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
L
Linus Torvalds 已提交
253
	jnz tracesys
R
Roland McGrath 已提交
254
system_call_fastpath:
255
#if __SYSCALL_MASK == ~0
L
Linus Torvalds 已提交
256
	cmpq $__NR_syscall_max,%rax
257 258 259 260
#else
	andl $__SYSCALL_MASK,%eax
	cmpl $__NR_syscall_max,%eax
#endif
261
	ja ret_from_sys_call  /* and return regs->ax */
L
Linus Torvalds 已提交
262 263
	movq %r10,%rcx
	call *sys_call_table(,%rax,8)  # XXX:	 rip relative
264
	movq %rax,RAX(%rsp)
L
Linus Torvalds 已提交
265 266
/*
 * Syscall return path ending with SYSRET (fast path)
267
 * Has incompletely filled pt_regs, iret frame is also incomplete.
268
 */
L
Linus Torvalds 已提交
269
ret_from_sys_call:
270

271
	LOCKDEP_SYS_EXIT
272
	DISABLE_INTERRUPTS(CLBR_NONE)
273
	TRACE_IRQS_OFF
274 275 276 277 278 279 280 281 282

	/*
	 * We must check ti flags with interrupts (or at least preemption)
	 * off because we must *never* return to userspace without
	 * processing exit work that is enqueued if we're preempted here.
	 * In particular, returning to userspace with any of the one-shot
	 * flags (TIF_NOTIFY_RESUME, TIF_USER_RETURN_NOTIFY, etc) set is
	 * very bad.
	 */
283 284
	testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
	jnz int_ret_from_sys_call_irqs_off	/* Go to the slow path */
285

286
	CFI_REMEMBER_STATE
287 288 289 290
	/*
	 * sysretq will re-enable interrupts:
	 */
	TRACE_IRQS_ON
291 292
	RESTORE_C_REGS_EXCEPT_RCX_R11
	movq	RIP(%rsp),%rcx
293
	CFI_REGISTER	rip,rcx
294
	movq	EFLAGS(%rsp),%r11
295
	/*CFI_REGISTER	rflags,r11*/
296
	movq	RSP(%rsp),%rsp
297 298 299 300 301
	/*
	 * 64bit SYSRET restores rip from rcx,
	 * rflags from r11 (but RF and VM bits are forced to 0),
	 * cs and ss are loaded from MSRs.
	 */
302
	USERGS_SYSRET64
L
Linus Torvalds 已提交
303

304
	CFI_RESTORE_STATE
L
Linus Torvalds 已提交
305

306
	/* Do syscall entry tracing */
307
tracesys:
308
	movq %rsp, %rdi
309 310 311 312
	movq $AUDIT_ARCH_X86_64, %rsi
	call syscall_trace_enter_phase1
	test %rax, %rax
	jnz tracesys_phase2		/* if needed, run the slow path */
313
	RESTORE_C_REGS_EXCEPT_RAX	/* else restore clobbered regs */
314
	movq ORIG_RAX(%rsp), %rax
315 316 317
	jmp system_call_fastpath	/*      and return to the fast path */

tracesys_phase2:
318
	SAVE_EXTRA_REGS
319 320 321 322 323
	movq %rsp, %rdi
	movq $AUDIT_ARCH_X86_64, %rsi
	movq %rax,%rdx
	call syscall_trace_enter_phase2

324
	/*
D
Denys Vlasenko 已提交
325
	 * Reload registers from stack in case ptrace changed them.
326
	 * We don't reload %rax because syscall_trace_entry_phase2() returned
327 328
	 * the value it wants us to use in the table lookup.
	 */
329 330
	RESTORE_C_REGS_EXCEPT_RAX
	RESTORE_EXTRA_REGS
331
#if __SYSCALL_MASK == ~0
L
Linus Torvalds 已提交
332
	cmpq $__NR_syscall_max,%rax
333 334 335 336
#else
	andl $__SYSCALL_MASK,%eax
	cmpl $__NR_syscall_max,%eax
#endif
337
	ja   int_ret_from_sys_call	/* RAX(%rsp) is already set */
L
Linus Torvalds 已提交
338 339
	movq %r10,%rcx	/* fixup for C */
	call *sys_call_table(,%rax,8)
340
	movq %rax,RAX(%rsp)
341
	/* Use IRET because user could have changed pt_regs->foo */
342 343

/*
L
Linus Torvalds 已提交
344
 * Syscall return path ending with IRET.
345
 * Has correct iret frame.
346
 */
347
GLOBAL(int_ret_from_sys_call)
348
	DISABLE_INTERRUPTS(CLBR_NONE)
349
	TRACE_IRQS_OFF
350
int_ret_from_sys_call_irqs_off:
L
Linus Torvalds 已提交
351 352
	movl $_TIF_ALLWORK_MASK,%edi
	/* edi:	mask to check */
353
GLOBAL(int_with_check)
354
	LOCKDEP_SYS_EXIT_IRQ
L
Linus Torvalds 已提交
355
	GET_THREAD_INFO(%rcx)
G
Glauber Costa 已提交
356
	movl TI_flags(%rcx),%edx
L
Linus Torvalds 已提交
357 358
	andl %edi,%edx
	jnz   int_careful
G
Glauber Costa 已提交
359
	andl    $~TS_COMPAT,TI_status(%rcx)
L
Linus Torvalds 已提交
360 361 362 363 364 365 366 367
	jmp   retint_swapgs

	/* Either reschedule or signal or syscall exit tracking needed. */
	/* First do a reschedule test. */
	/* edx:	work, edi: workmask */
int_careful:
	bt $TIF_NEED_RESCHED,%edx
	jnc  int_very_careful
368
	TRACE_IRQS_ON
369
	ENABLE_INTERRUPTS(CLBR_NONE)
370
	pushq_cfi %rdi
371
	SCHEDULE_USER
372
	popq_cfi %rdi
373
	DISABLE_INTERRUPTS(CLBR_NONE)
374
	TRACE_IRQS_OFF
L
Linus Torvalds 已提交
375 376
	jmp int_with_check

377
	/* handle signals and tracing -- both require a full pt_regs */
L
Linus Torvalds 已提交
378
int_very_careful:
379
	TRACE_IRQS_ON
380
	ENABLE_INTERRUPTS(CLBR_NONE)
381
	SAVE_EXTRA_REGS
382
	/* Check for syscall exit trace */
383
	testl $_TIF_WORK_SYSCALL_EXIT,%edx
L
Linus Torvalds 已提交
384
	jz int_signal
385
	pushq_cfi %rdi
386
	leaq 8(%rsp),%rdi	# &ptregs -> arg1
L
Linus Torvalds 已提交
387
	call syscall_trace_leave
388
	popq_cfi %rdi
389
	andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
L
Linus Torvalds 已提交
390
	jmp int_restore_rest
391

L
Linus Torvalds 已提交
392
int_signal:
P
Peter Zijlstra 已提交
393
	testl $_TIF_DO_NOTIFY_MASK,%edx
L
Linus Torvalds 已提交
394 395 396 397
	jz 1f
	movq %rsp,%rdi		# &ptregs -> arg1
	xorl %esi,%esi		# oldset -> arg2
	call do_notify_resume
R
Roland McGrath 已提交
398
1:	movl $_TIF_WORK_MASK,%edi
L
Linus Torvalds 已提交
399
int_restore_rest:
400
	RESTORE_EXTRA_REGS
401
	DISABLE_INTERRUPTS(CLBR_NONE)
402
	TRACE_IRQS_OFF
L
Linus Torvalds 已提交
403 404
	jmp int_with_check
	CFI_ENDPROC
405
END(system_call)
406

407 408 409
	.macro FORK_LIKE func
ENTRY(stub_\func)
	CFI_STARTPROC
410 411
	DEFAULT_FRAME 0, 8		/* offset 8: return address */
	SAVE_EXTRA_REGS 8
412
	call sys_\func
413
	ret
414 415 416 417 418 419 420
	CFI_ENDPROC
END(stub_\func)
	.endm

	FORK_LIKE  clone
	FORK_LIKE  fork
	FORK_LIKE  vfork
L
Linus Torvalds 已提交
421 422 423

ENTRY(stub_execve)
	CFI_STARTPROC
424
	addq $8, %rsp
425 426
	DEFAULT_FRAME 0
	SAVE_EXTRA_REGS
L
Linus Torvalds 已提交
427 428
	call sys_execve
	movq %rax,RAX(%rsp)
429
	RESTORE_EXTRA_REGS
L
Linus Torvalds 已提交
430 431
	jmp int_ret_from_sys_call
	CFI_ENDPROC
432
END(stub_execve)
433

D
David Drysdale 已提交
434 435 436
ENTRY(stub_execveat)
	CFI_STARTPROC
	addq $8, %rsp
437 438
	DEFAULT_FRAME 0
	SAVE_EXTRA_REGS
D
David Drysdale 已提交
439 440
	call sys_execveat
	movq %rax,RAX(%rsp)
441
	RESTORE_EXTRA_REGS
D
David Drysdale 已提交
442 443 444 445
	jmp int_ret_from_sys_call
	CFI_ENDPROC
END(stub_execveat)

L
Linus Torvalds 已提交
446 447 448
/*
 * sigreturn is special because it needs to restore all registers on return.
 * This cannot be done with SYSRET, so use the IRET return path instead.
449
 */
L
Linus Torvalds 已提交
450 451
ENTRY(stub_rt_sigreturn)
	CFI_STARTPROC
452
	addq $8, %rsp
453 454
	DEFAULT_FRAME 0
	SAVE_EXTRA_REGS
L
Linus Torvalds 已提交
455 456
	call sys_rt_sigreturn
	movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
457
	RESTORE_EXTRA_REGS
L
Linus Torvalds 已提交
458 459
	jmp int_ret_from_sys_call
	CFI_ENDPROC
460
END(stub_rt_sigreturn)
L
Linus Torvalds 已提交
461

462 463 464 465
#ifdef CONFIG_X86_X32_ABI
ENTRY(stub_x32_rt_sigreturn)
	CFI_STARTPROC
	addq $8, %rsp
466 467
	DEFAULT_FRAME 0
	SAVE_EXTRA_REGS
468 469
	call sys32_x32_rt_sigreturn
	movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
470
	RESTORE_EXTRA_REGS
471 472 473 474
	jmp int_ret_from_sys_call
	CFI_ENDPROC
END(stub_x32_rt_sigreturn)

H
H. Peter Anvin 已提交
475 476 477
ENTRY(stub_x32_execve)
	CFI_STARTPROC
	addq $8, %rsp
478 479
	DEFAULT_FRAME 0
	SAVE_EXTRA_REGS
480
	call compat_sys_execve
H
H. Peter Anvin 已提交
481
	movq %rax,RAX(%rsp)
482
	RESTORE_EXTRA_REGS
H
H. Peter Anvin 已提交
483 484 485 486
	jmp int_ret_from_sys_call
	CFI_ENDPROC
END(stub_x32_execve)

D
David Drysdale 已提交
487 488 489
ENTRY(stub_x32_execveat)
	CFI_STARTPROC
	addq $8, %rsp
490 491
	DEFAULT_FRAME 0
	SAVE_EXTRA_REGS
D
David Drysdale 已提交
492 493
	call compat_sys_execveat
	movq %rax,RAX(%rsp)
494
	RESTORE_EXTRA_REGS
D
David Drysdale 已提交
495 496 497 498
	jmp int_ret_from_sys_call
	CFI_ENDPROC
END(stub_x32_execveat)

499 500
#endif

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
/*
 * A newly forked process directly context switches into this address.
 *
 * rdi: prev task we switched from
 */
ENTRY(ret_from_fork)
	DEFAULT_FRAME

	LOCK ; btr $TIF_FORK,TI_flags(%r8)

	pushq_cfi $0x0002
	popfq_cfi				# reset kernel eflags

	call schedule_tail			# rdi: 'prev' task parameter

	GET_THREAD_INFO(%rcx)

	RESTORE_EXTRA_REGS

	testl $3,CS(%rsp)			# from kernel_thread?
	jz   1f

523 524 525 526 527 528 529 530
	/*
	 * By the time we get here, we have no idea whether our pt_regs,
	 * ti flags, and ti status came from the 64-bit SYSCALL fast path,
	 * the slow path, or one of the ia32entry paths.
	 * Use int_ret_from_sys_call to return, since it can safely handle
	 * all of the above.
	 */
	jmp  int_ret_from_sys_call
531 532 533 534 535 536 537 538 539 540

1:
	movq %rbp, %rdi
	call *%rbx
	movl $0, RAX(%rsp)
	RESTORE_EXTRA_REGS
	jmp int_ret_from_sys_call
	CFI_ENDPROC
END(ret_from_fork)

541 542 543 544 545 546 547
/*
 * Build the entry stubs and pointer table with some assembler magic.
 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
 * single cache line on all modern x86 implementations.
 */
	.section .init.rodata,"a"
ENTRY(interrupt)
J
Jiri Olsa 已提交
548
	.section .entry.text
549 550 551 552 553
	.p2align 5
	.p2align CONFIG_X86_L1_CACHE_SHIFT
ENTRY(irq_entries_start)
	INTR_FRAME
vector=FIRST_EXTERNAL_VECTOR
554
.rept (FIRST_SYSTEM_VECTOR-FIRST_EXTERNAL_VECTOR+6)/7
555 556
	.balign 32
  .rept	7
557
    .if vector < FIRST_SYSTEM_VECTOR
558
      .if vector <> FIRST_EXTERNAL_VECTOR
559 560
	CFI_ADJUST_CFA_OFFSET -8
      .endif
561
1:	pushq_cfi $(~vector+0x80)	/* Note: always in signed byte range */
562
      .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
563 564 565 566
	jmp 2f
      .endif
      .previous
	.quad 1b
J
Jiri Olsa 已提交
567
      .section .entry.text
568 569 570 571 572 573 574 575 576 577 578 579
vector=vector+1
    .endif
  .endr
2:	jmp common_interrupt
.endr
	CFI_ENDPROC
END(irq_entries_start)

.previous
END(interrupt)
.previous

580
/*
L
Linus Torvalds 已提交
581 582 583
 * Interrupt entry/exit.
 *
 * Interrupt entry points save only callee clobbered registers in fast path.
584 585 586
 *
 * Entry runs with interrupts off.
 */
L
Linus Torvalds 已提交
587

588
/* 0(%rsp): ~(interrupt number) */
L
Linus Torvalds 已提交
589
	.macro interrupt func
590
	cld
D
Denys Vlasenko 已提交
591 592 593 594 595 596 597
	/*
	 * Since nothing in interrupt handling code touches r12...r15 members
	 * of "struct pt_regs", and since interrupts can nest, we can save
	 * four stack slots and simultaneously provide
	 * an unwind-friendly stack layout by saving "truncated" pt_regs
	 * exactly up to rbp slot, without these members.
	 */
598 599 600 601 602 603
	ALLOC_PT_GPREGS_ON_STACK -RBP
	SAVE_C_REGS -RBP
	/* this goes to 0(%rsp) for unwinder, not for saving the value: */
	SAVE_EXTRA_REGS_RBP -RBP

	leaq -RBP(%rsp),%rdi	/* arg1 for \func (pointer to pt_regs) */
604

605
	testl $3, CS-RBP(%rsp)
606 607
	je 1f
	SWAPGS
608
1:
609
	/*
D
Denys Vlasenko 已提交
610
	 * Save previous stack pointer, optionally switch to interrupt stack.
611 612 613 614 615
	 * irq_count is used to check if a CPU is already on an interrupt stack
	 * or not. While this is essentially redundant with preempt_count it is
	 * a little cheaper to use a separate counter in the PDA (short of
	 * moving irq_enter into assembly, which would be too much work)
	 */
616 617
	movq %rsp, %rsi
	incl PER_CPU_VAR(irq_count)
618 619 620
	cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
	CFI_DEF_CFA_REGISTER	rsi
	pushq %rsi
621 622 623 624
	/*
	 * For debugger:
	 * "CFA (Current Frame Address) is the value on stack + offset"
	 */
625
	CFI_ESCAPE	0x0f /* DW_CFA_def_cfa_expression */, 6, \
626
			0x77 /* DW_OP_breg7 (rsp) */, 0, \
627
			0x06 /* DW_OP_deref */, \
628
			0x08 /* DW_OP_const1u */, SIZEOF_PTREGS-RBP, \
629 630 631 632
			0x22 /* DW_OP_plus */
	/* We entered an interrupt context - irqs are off: */
	TRACE_IRQS_OFF

L
Linus Torvalds 已提交
633 634 635
	call \func
	.endm

636 637 638 639
	/*
	 * The interrupt stubs push (~vector+0x80) onto the stack and
	 * then jump to common_interrupt.
	 */
640 641
	.p2align CONFIG_X86_L1_CACHE_SHIFT
common_interrupt:
642
	XCPT_FRAME
643
	ASM_CLAC
644
	addq $-0x80,(%rsp)		/* Adjust vector to [-256,-1] range */
L
Linus Torvalds 已提交
645
	interrupt do_IRQ
646
	/* 0(%rsp): old RSP */
647
ret_from_intr:
648
	DISABLE_INTERRUPTS(CLBR_NONE)
649
	TRACE_IRQS_OFF
650
	decl PER_CPU_VAR(irq_count)
651

652 653
	/* Restore saved previous stack */
	popq %rsi
654
	CFI_DEF_CFA rsi,SIZEOF_PTREGS-RBP /* reg/off reset after def_cfa_expr */
D
Denys Vlasenko 已提交
655
	/* return code expects complete pt_regs - adjust rsp accordingly: */
656
	leaq -RBP(%rsi),%rsp
657
	CFI_DEF_CFA_REGISTER	rsp
658
	CFI_ADJUST_CFA_OFFSET	RBP
659

660
exit_intr:
L
Linus Torvalds 已提交
661
	GET_THREAD_INFO(%rcx)
662
	testl $3,CS(%rsp)
L
Linus Torvalds 已提交
663
	je retint_kernel
664

L
Linus Torvalds 已提交
665 666
	/* Interrupt came from user space */
	/*
D
Denys Vlasenko 已提交
667
	 * Has a correct top of stack.
L
Linus Torvalds 已提交
668
	 * %rcx: thread info. Interrupts off.
669
	 */
L
Linus Torvalds 已提交
670 671
retint_with_reschedule:
	movl $_TIF_WORK_MASK,%edi
672
retint_check:
673
	LOCKDEP_SYS_EXIT_IRQ
G
Glauber Costa 已提交
674
	movl TI_flags(%rcx),%edx
L
Linus Torvalds 已提交
675
	andl %edi,%edx
676
	CFI_REMEMBER_STATE
L
Linus Torvalds 已提交
677
	jnz  retint_careful
678 679

retint_swapgs:		/* return to user-space */
680 681 682
	/*
	 * The iretq could re-enable interrupts:
	 */
683
	DISABLE_INTERRUPTS(CLBR_ANY)
684
	TRACE_IRQS_IRETQ
685 686 687 688 689

	/*
	 * Try to use SYSRET instead of IRET if we're returning to
	 * a completely clean 64-bit userspace context.
	 */
690 691
	movq RCX(%rsp),%rcx
	cmpq %rcx,RIP(%rsp)		/* RCX == RIP */
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	jne opportunistic_sysret_failed

	/*
	 * On Intel CPUs, sysret with non-canonical RCX/RIP will #GP
	 * in kernel space.  This essentially lets the user take over
	 * the kernel, since userspace controls RSP.  It's not worth
	 * testing for canonicalness exactly -- this check detects any
	 * of the 17 high bits set, which is true for non-canonical
	 * or kernel addresses.  (This will pessimize vsyscall=native.
	 * Big deal.)
	 *
	 * If virtual addresses ever become wider, this will need
	 * to be updated to remain correct on both old and new CPUs.
	 */
	.ifne __VIRTUAL_MASK_SHIFT - 47
	.error "virtual address width changed -- sysret checks need update"
	.endif
	shr $__VIRTUAL_MASK_SHIFT, %rcx
	jnz opportunistic_sysret_failed

712
	cmpq $__USER_CS,CS(%rsp)	/* CS must match SYSRET */
713 714
	jne opportunistic_sysret_failed

715 716
	movq R11(%rsp),%r11
	cmpq %r11,EFLAGS(%rsp)		/* R11 == RFLAGS */
717 718
	jne opportunistic_sysret_failed

719
	testq $X86_EFLAGS_RF,%r11	/* sysret can't restore RF */
720 721 722 723
	jnz opportunistic_sysret_failed

	/* nothing to check for RSP */

724
	cmpq $__USER_DS,SS(%rsp)	/* SS must match SYSRET */
725 726 727 728 729 730 731 732
	jne opportunistic_sysret_failed

	/*
	 * We win!  This label is here just for ease of understanding
	 * perf profiles.  Nothing jumps here.
	 */
irq_return_via_sysret:
	CFI_REMEMBER_STATE
733 734 735
	/* r11 is already restored (see code above) */
	RESTORE_C_REGS_EXCEPT_R11
	movq RSP(%rsp),%rsp
736 737 738 739
	USERGS_SYSRET64
	CFI_RESTORE_STATE

opportunistic_sysret_failed:
740
	SWAPGS
741 742
	jmp restore_args

743
retint_restore_args:	/* return to kernel space */
744
	DISABLE_INTERRUPTS(CLBR_ANY)
745 746 747 748 749
	/*
	 * The iretq could re-enable interrupts:
	 */
	TRACE_IRQS_IRETQ
restore_args:
750 751
	RESTORE_C_REGS
	REMOVE_PT_GPREGS_FROM_STACK 8
I
Ingo Molnar 已提交
752

A
Adrian Bunk 已提交
753
irq_return:
754 755 756
	INTERRUPT_RETURN

ENTRY(native_iret)
757 758 759 760
	/*
	 * Are we returning to a stack segment from the LDT?  Note: in
	 * 64-bit mode SS:RSP on the exception stack is always valid.
	 */
761
#ifdef CONFIG_X86_ESPFIX64
762
	testb $4,(SS-RIP)(%rsp)
763
	jnz native_irq_return_ldt
764
#endif
765

766
.global native_irq_return_iret
767
native_irq_return_iret:
A
Andy Lutomirski 已提交
768 769 770 771 772 773
	/*
	 * This may fault.  Non-paranoid faults on return to userspace are
	 * handled by fixup_bad_iret.  These include #SS, #GP, and #NP.
	 * Double-faults due to espfix64 are handled in do_double_fault.
	 * Other faults here are fatal.
	 */
L
Linus Torvalds 已提交
774
	iretq
I
Ingo Molnar 已提交
775

776
#ifdef CONFIG_X86_ESPFIX64
777
native_irq_return_ldt:
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	pushq_cfi %rax
	pushq_cfi %rdi
	SWAPGS
	movq PER_CPU_VAR(espfix_waddr),%rdi
	movq %rax,(0*8)(%rdi)	/* RAX */
	movq (2*8)(%rsp),%rax	/* RIP */
	movq %rax,(1*8)(%rdi)
	movq (3*8)(%rsp),%rax	/* CS */
	movq %rax,(2*8)(%rdi)
	movq (4*8)(%rsp),%rax	/* RFLAGS */
	movq %rax,(3*8)(%rdi)
	movq (6*8)(%rsp),%rax	/* SS */
	movq %rax,(5*8)(%rdi)
	movq (5*8)(%rsp),%rax	/* RSP */
	movq %rax,(4*8)(%rdi)
	andl $0xffff0000,%eax
	popq_cfi %rdi
	orq PER_CPU_VAR(espfix_stack),%rax
	SWAPGS
	movq %rax,%rsp
	popq_cfi %rax
799
	jmp native_irq_return_iret
800
#endif
801

802
	/* edi: workmask, edx: work */
L
Linus Torvalds 已提交
803
retint_careful:
804
	CFI_RESTORE_STATE
L
Linus Torvalds 已提交
805 806
	bt    $TIF_NEED_RESCHED,%edx
	jnc   retint_signal
807
	TRACE_IRQS_ON
808
	ENABLE_INTERRUPTS(CLBR_NONE)
809
	pushq_cfi %rdi
810
	SCHEDULE_USER
811
	popq_cfi %rdi
L
Linus Torvalds 已提交
812
	GET_THREAD_INFO(%rcx)
813
	DISABLE_INTERRUPTS(CLBR_NONE)
814
	TRACE_IRQS_OFF
L
Linus Torvalds 已提交
815
	jmp retint_check
816

L
Linus Torvalds 已提交
817
retint_signal:
P
Peter Zijlstra 已提交
818
	testl $_TIF_DO_NOTIFY_MASK,%edx
819
	jz    retint_swapgs
820
	TRACE_IRQS_ON
821
	ENABLE_INTERRUPTS(CLBR_NONE)
822
	SAVE_EXTRA_REGS
823
	movq $-1,ORIG_RAX(%rsp)
824
	xorl %esi,%esi		# oldset
L
Linus Torvalds 已提交
825 826
	movq %rsp,%rdi		# &pt_regs
	call do_notify_resume
827
	RESTORE_EXTRA_REGS
828
	DISABLE_INTERRUPTS(CLBR_NONE)
829
	TRACE_IRQS_OFF
830
	GET_THREAD_INFO(%rcx)
R
Roland McGrath 已提交
831
	jmp retint_with_reschedule
L
Linus Torvalds 已提交
832 833 834 835

#ifdef CONFIG_PREEMPT
	/* Returning to kernel space. Check if we need preemption */
	/* rcx:	 threadinfo. interrupts off. */
836
ENTRY(retint_kernel)
837
	cmpl $0,PER_CPU_VAR(__preempt_count)
L
Linus Torvalds 已提交
838
	jnz  retint_restore_args
839
	bt   $9,EFLAGS(%rsp)	/* interrupts off? */
L
Linus Torvalds 已提交
840 841 842
	jnc  retint_restore_args
	call preempt_schedule_irq
	jmp exit_intr
843
#endif
L
Linus Torvalds 已提交
844
	CFI_ENDPROC
845
END(common_interrupt)
846

L
Linus Torvalds 已提交
847 848
/*
 * APIC interrupts.
849
 */
850
.macro apicinterrupt3 num sym do_sym
851
ENTRY(\sym)
852
	INTR_FRAME
853
	ASM_CLAC
854
	pushq_cfi $~(\num)
855
.Lcommon_\sym:
856
	interrupt \do_sym
L
Linus Torvalds 已提交
857 858
	jmp ret_from_intr
	CFI_ENDPROC
859 860
END(\sym)
.endm
L
Linus Torvalds 已提交
861

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
#ifdef CONFIG_TRACING
#define trace(sym) trace_##sym
#define smp_trace(sym) smp_trace_##sym

.macro trace_apicinterrupt num sym
apicinterrupt3 \num trace(\sym) smp_trace(\sym)
.endm
#else
.macro trace_apicinterrupt num sym do_sym
.endm
#endif

.macro apicinterrupt num sym do_sym
apicinterrupt3 \num \sym \do_sym
trace_apicinterrupt \num \sym
.endm

879
#ifdef CONFIG_SMP
880
apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR \
881
	irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
882
apicinterrupt3 REBOOT_VECTOR \
883
	reboot_interrupt smp_reboot_interrupt
884
#endif
L
Linus Torvalds 已提交
885

N
Nick Piggin 已提交
886
#ifdef CONFIG_X86_UV
887
apicinterrupt3 UV_BAU_MESSAGE \
888
	uv_bau_message_intr1 uv_bau_message_interrupt
N
Nick Piggin 已提交
889
#endif
890 891
apicinterrupt LOCAL_TIMER_VECTOR \
	apic_timer_interrupt smp_apic_timer_interrupt
892 893
apicinterrupt X86_PLATFORM_IPI_VECTOR \
	x86_platform_ipi smp_x86_platform_ipi
894

895
#ifdef CONFIG_HAVE_KVM
896
apicinterrupt3 POSTED_INTR_VECTOR \
897 898 899
	kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
#endif

900
#ifdef CONFIG_X86_MCE_THRESHOLD
901
apicinterrupt THRESHOLD_APIC_VECTOR \
902
	threshold_interrupt smp_threshold_interrupt
903 904 905
#endif

#ifdef CONFIG_X86_THERMAL_VECTOR
906 907
apicinterrupt THERMAL_APIC_VECTOR \
	thermal_interrupt smp_thermal_interrupt
908
#endif
909

910 911 912 913 914 915 916 917
#ifdef CONFIG_SMP
apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
	call_function_single_interrupt smp_call_function_single_interrupt
apicinterrupt CALL_FUNCTION_VECTOR \
	call_function_interrupt smp_call_function_interrupt
apicinterrupt RESCHEDULE_VECTOR \
	reschedule_interrupt smp_reschedule_interrupt
#endif
L
Linus Torvalds 已提交
918

919 920 921 922
apicinterrupt ERROR_APIC_VECTOR \
	error_interrupt smp_error_interrupt
apicinterrupt SPURIOUS_APIC_VECTOR \
	spurious_interrupt smp_spurious_interrupt
923

924 925 926
#ifdef CONFIG_IRQ_WORK
apicinterrupt IRQ_WORK_VECTOR \
	irq_work_interrupt smp_irq_work_interrupt
I
Ingo Molnar 已提交
927 928
#endif

L
Linus Torvalds 已提交
929 930
/*
 * Exception entry points.
931
 */
932
#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
933 934

.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
935
ENTRY(\sym)
936 937 938 939 940
	/* Sanity check */
	.if \shift_ist != -1 && \paranoid == 0
	.error "using shift_ist requires paranoid=1"
	.endif

941 942 943
	.if \has_error_code
	XCPT_FRAME
	.else
944
	INTR_FRAME
945
	.endif
L
Linus Torvalds 已提交
946

947
	ASM_CLAC
948
	PARAVIRT_ADJUST_EXCEPTION_FRAME
949 950 951 952 953

	.ifeq \has_error_code
	pushq_cfi $-1			/* ORIG_RAX: no syscall to restart */
	.endif

954
	ALLOC_PT_GPREGS_ON_STACK
955 956

	.if \paranoid
957 958 959 960 961
	.if \paranoid == 1
	CFI_REMEMBER_STATE
	testl $3, CS(%rsp)		/* If coming from userspace, switch */
	jnz 1f				/* stacks. */
	.endif
962
	call paranoid_entry
963 964 965
	.else
	call error_entry
	.endif
966
	/* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
967

968
	DEFAULT_FRAME 0
969 970

	.if \paranoid
971 972 973
	.if \shift_ist != -1
	TRACE_IRQS_OFF_DEBUG		/* reload IDT in case of recursion */
	.else
974
	TRACE_IRQS_OFF
975
	.endif
976
	.endif
977 978 979 980 981 982 983 984 985 986

	movq %rsp,%rdi			/* pt_regs pointer */

	.if \has_error_code
	movq ORIG_RAX(%rsp),%rsi	/* get error code */
	movq $-1,ORIG_RAX(%rsp)		/* no syscall to restart */
	.else
	xorl %esi,%esi			/* no error code */
	.endif

987
	.if \shift_ist != -1
988
	subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
989 990
	.endif

991
	call \do_sym
992

993
	.if \shift_ist != -1
994
	addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
995 996
	.endif

997
	/* these procedures expect "no swapgs" flag in ebx */
998
	.if \paranoid
999
	jmp paranoid_exit
1000
	.else
1001
	jmp error_exit
1002 1003
	.endif

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	.if \paranoid == 1
	CFI_RESTORE_STATE
	/*
	 * Paranoid entry from userspace.  Switch stacks and treat it
	 * as a normal entry.  This means that paranoid handlers
	 * run in real process context if user_mode(regs).
	 */
1:
	call error_entry

	DEFAULT_FRAME 0

	movq %rsp,%rdi			/* pt_regs pointer */
	call sync_regs
	movq %rax,%rsp			/* switch stack */

	movq %rsp,%rdi			/* pt_regs pointer */

	.if \has_error_code
	movq ORIG_RAX(%rsp),%rsi	/* get error code */
	movq $-1,ORIG_RAX(%rsp)		/* no syscall to restart */
	.else
	xorl %esi,%esi			/* no error code */
	.endif

	call \do_sym

	jmp error_exit			/* %ebx: no swapgs flag */
	.endif

1034
	CFI_ENDPROC
1035
END(\sym)
1036
.endm
1037

1038
#ifdef CONFIG_TRACING
1039 1040 1041
.macro trace_idtentry sym do_sym has_error_code:req
idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
idtentry \sym \do_sym has_error_code=\has_error_code
1042 1043
.endm
#else
1044 1045
.macro trace_idtentry sym do_sym has_error_code:req
idtentry \sym \do_sym has_error_code=\has_error_code
1046 1047 1048
.endm
#endif

1049 1050 1051 1052 1053
idtentry divide_error do_divide_error has_error_code=0
idtentry overflow do_overflow has_error_code=0
idtentry bounds do_bounds has_error_code=0
idtentry invalid_op do_invalid_op has_error_code=0
idtentry device_not_available do_device_not_available has_error_code=0
1054
idtentry double_fault do_double_fault has_error_code=1 paranoid=2
1055 1056 1057 1058 1059 1060 1061
idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
idtentry invalid_TSS do_invalid_TSS has_error_code=1
idtentry segment_not_present do_segment_not_present has_error_code=1
idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
idtentry coprocessor_error do_coprocessor_error has_error_code=0
idtentry alignment_check do_alignment_check has_error_code=1
idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
1062

1063

1064 1065
	/* Reload gs selector with exception handling */
	/* edi:  new selector */
1066
ENTRY(native_load_gs_index)
1067
	CFI_STARTPROC
1068
	pushfq_cfi
1069
	DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
1070
	SWAPGS
1071
gs_change:
1072
	movl %edi,%gs
L
Linus Torvalds 已提交
1073
2:	mfence		/* workaround */
1074
	SWAPGS
1075
	popfq_cfi
1076
	ret
1077
	CFI_ENDPROC
1078
END(native_load_gs_index)
1079

1080
	_ASM_EXTABLE(gs_change,bad_gs)
1081
	.section .fixup,"ax"
L
Linus Torvalds 已提交
1082
	/* running with kernelgs */
1083
bad_gs:
1084
	SWAPGS			/* switch back to user gs */
L
Linus Torvalds 已提交
1085
	xorl %eax,%eax
1086 1087 1088
	movl %eax,%gs
	jmp  2b
	.previous
1089

1090
/* Call softirq on interrupt stack. Interrupts are off. */
1091
ENTRY(do_softirq_own_stack)
1092
	CFI_STARTPROC
1093
	pushq_cfi %rbp
1094 1095 1096
	CFI_REL_OFFSET rbp,0
	mov  %rsp,%rbp
	CFI_DEF_CFA_REGISTER rbp
1097
	incl PER_CPU_VAR(irq_count)
1098
	cmove PER_CPU_VAR(irq_stack_ptr),%rsp
1099
	push  %rbp			# backlink for old unwinder
1100
	call __do_softirq
1101
	leaveq
1102
	CFI_RESTORE		rbp
1103
	CFI_DEF_CFA_REGISTER	rsp
1104
	CFI_ADJUST_CFA_OFFSET   -8
1105
	decl PER_CPU_VAR(irq_count)
1106
	ret
1107
	CFI_ENDPROC
1108
END(do_softirq_own_stack)
1109

1110
#ifdef CONFIG_XEN
1111
idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
1112 1113

/*
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
 * A note on the "critical region" in our callback handler.
 * We want to avoid stacking callback handlers due to events occurring
 * during handling of the last event. To do this, we keep events disabled
 * until we've done all processing. HOWEVER, we must enable events before
 * popping the stack frame (can't be done atomically) and so it would still
 * be possible to get enough handler activations to overflow the stack.
 * Although unlikely, bugs of that kind are hard to track down, so we'd
 * like to avoid the possibility.
 * So, on entry to the handler we detect whether we interrupted an
 * existing activation in its critical region -- if so, we pop the current
 * activation and restart the handler using the previous one.
 */
1126 1127
ENTRY(xen_do_hypervisor_callback)   # do_hypervisor_callback(struct *pt_regs)
	CFI_STARTPROC
1128 1129 1130 1131
/*
 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
 * see the correct pointer to the pt_regs
 */
1132 1133
	movq %rdi, %rsp            # we don't return, adjust the stack frame
	CFI_ENDPROC
1134
	DEFAULT_FRAME
1135
11:	incl PER_CPU_VAR(irq_count)
1136 1137
	movq %rsp,%rbp
	CFI_DEF_CFA_REGISTER rbp
1138
	cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
1139 1140 1141 1142
	pushq %rbp			# backlink for old unwinder
	call xen_evtchn_do_upcall
	popq %rsp
	CFI_DEF_CFA_REGISTER rsp
1143
	decl PER_CPU_VAR(irq_count)
1144 1145 1146
#ifndef CONFIG_PREEMPT
	call xen_maybe_preempt_hcall
#endif
1147 1148
	jmp  error_exit
	CFI_ENDPROC
1149
END(xen_do_hypervisor_callback)
1150 1151

/*
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
 * Hypervisor uses this for application faults while it executes.
 * We get here for two reasons:
 *  1. Fault while reloading DS, ES, FS or GS
 *  2. Fault while executing IRET
 * Category 1 we do not need to fix up as Xen has already reloaded all segment
 * registers that could be reloaded and zeroed the others.
 * Category 2 we fix up by killing the current process. We cannot use the
 * normal Linux return path in this case because if we use the IRET hypercall
 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
 * We distinguish between categories by comparing each saved segment register
 * with its current contents: any discrepancy means we in category 1.
 */
1164
ENTRY(xen_failsafe_callback)
1165 1166 1167 1168 1169 1170 1171
	INTR_FRAME 1 (6*8)
	/*CFI_REL_OFFSET gs,GS*/
	/*CFI_REL_OFFSET fs,FS*/
	/*CFI_REL_OFFSET es,ES*/
	/*CFI_REL_OFFSET ds,DS*/
	CFI_REL_OFFSET r11,8
	CFI_REL_OFFSET rcx,0
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
	movw %ds,%cx
	cmpw %cx,0x10(%rsp)
	CFI_REMEMBER_STATE
	jne 1f
	movw %es,%cx
	cmpw %cx,0x18(%rsp)
	jne 1f
	movw %fs,%cx
	cmpw %cx,0x20(%rsp)
	jne 1f
	movw %gs,%cx
	cmpw %cx,0x28(%rsp)
	jne 1f
	/* All segments match their saved values => Category 2 (Bad IRET). */
	movq (%rsp),%rcx
	CFI_RESTORE rcx
	movq 8(%rsp),%r11
	CFI_RESTORE r11
	addq $0x30,%rsp
	CFI_ADJUST_CFA_OFFSET -0x30
I
Ingo Molnar 已提交
1192 1193 1194
	pushq_cfi $0	/* RIP */
	pushq_cfi %r11
	pushq_cfi %rcx
1195
	jmp general_protection
1196 1197 1198 1199 1200 1201 1202 1203
	CFI_RESTORE_STATE
1:	/* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
	movq (%rsp),%rcx
	CFI_RESTORE rcx
	movq 8(%rsp),%r11
	CFI_RESTORE r11
	addq $0x30,%rsp
	CFI_ADJUST_CFA_OFFSET -0x30
1204
	pushq_cfi $-1 /* orig_ax = -1 => not a system call */
1205 1206 1207
	ALLOC_PT_GPREGS_ON_STACK
	SAVE_C_REGS
	SAVE_EXTRA_REGS
1208 1209 1210 1211
	jmp error_exit
	CFI_ENDPROC
END(xen_failsafe_callback)

1212
apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1213 1214
	xen_hvm_callback_vector xen_evtchn_do_upcall

1215
#endif /* CONFIG_XEN */
1216

1217
#if IS_ENABLED(CONFIG_HYPERV)
1218
apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
1219 1220 1221
	hyperv_callback_vector hyperv_vector_handler
#endif /* CONFIG_HYPERV */

1222 1223
idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1224
idtentry stack_segment do_stack_segment has_error_code=1
1225
#ifdef CONFIG_XEN
1226 1227 1228
idtentry xen_debug do_debug has_error_code=0
idtentry xen_int3 do_int3 has_error_code=0
idtentry xen_stack_segment do_stack_segment has_error_code=1
1229
#endif
1230 1231
idtentry general_protection do_general_protection has_error_code=1
trace_idtentry page_fault do_page_fault has_error_code=1
G
Gleb Natapov 已提交
1232
#ifdef CONFIG_KVM_GUEST
1233
idtentry async_page_fault do_async_page_fault has_error_code=1
G
Gleb Natapov 已提交
1234
#endif
1235
#ifdef CONFIG_X86_MCE
1236
idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
1237 1238
#endif

1239 1240 1241 1242 1243 1244 1245
/*
 * Save all registers in pt_regs, and switch gs if needed.
 * Use slow, but surefire "are we in kernel?" check.
 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
 */
ENTRY(paranoid_entry)
	XCPT_FRAME 1 15*8
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
	cld
	SAVE_C_REGS 8
	SAVE_EXTRA_REGS 8
	movl $1,%ebx
	movl $MSR_GS_BASE,%ecx
	rdmsr
	testl %edx,%edx
	js 1f	/* negative -> in kernel */
	SWAPGS
	xorl %ebx,%ebx
1:	ret
	CFI_ENDPROC
1258
END(paranoid_entry)
1259

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
/*
 * "Paranoid" exit path from exception stack.  This is invoked
 * only on return from non-NMI IST interrupts that came
 * from kernel space.
 *
 * We may be returning to very strange contexts (e.g. very early
 * in syscall entry), so checking for preemption here would
 * be complicated.  Fortunately, we there's no good reason
 * to try to handle preemption here.
 */
/* On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) */
1271
ENTRY(paranoid_exit)
1272
	DEFAULT_FRAME
1273
	DISABLE_INTERRUPTS(CLBR_NONE)
1274
	TRACE_IRQS_OFF_DEBUG
1275
	testl %ebx,%ebx				/* swapgs needed? */
1276
	jnz paranoid_exit_no_swapgs
1277
	TRACE_IRQS_IRETQ
1278
	SWAPGS_UNSAFE_STACK
1279 1280
	jmp paranoid_exit_restore
paranoid_exit_no_swapgs:
1281
	TRACE_IRQS_IRETQ_DEBUG
1282
paranoid_exit_restore:
1283 1284 1285
	RESTORE_EXTRA_REGS
	RESTORE_C_REGS
	REMOVE_PT_GPREGS_FROM_STACK 8
1286
	INTERRUPT_RETURN
1287 1288 1289 1290
	CFI_ENDPROC
END(paranoid_exit)

/*
1291 1292
 * Save all registers in pt_regs, and switch gs if needed.
 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
1293 1294
 */
ENTRY(error_entry)
1295
	XCPT_FRAME 1 15*8
1296
	cld
1297 1298
	SAVE_C_REGS 8
	SAVE_EXTRA_REGS 8
1299 1300 1301 1302 1303 1304 1305 1306 1307
	xorl %ebx,%ebx
	testl $3,CS+8(%rsp)
	je error_kernelspace
error_swapgs:
	SWAPGS
error_sti:
	TRACE_IRQS_OFF
	ret

1308 1309 1310 1311 1312 1313
	/*
	 * There are two places in the kernel that can potentially fault with
	 * usergs. Handle them here.  B stepping K8s sometimes report a
	 * truncated RIP for IRET exceptions returning to compat mode. Check
	 * for these here too.
	 */
1314
error_kernelspace:
1315
	CFI_REL_OFFSET rcx, RCX+8
1316
	incl %ebx
1317
	leaq native_irq_return_iret(%rip),%rcx
1318
	cmpq %rcx,RIP+8(%rsp)
A
Andy Lutomirski 已提交
1319
	je error_bad_iret
1320 1321 1322
	movl %ecx,%eax	/* zero extend */
	cmpq %rax,RIP+8(%rsp)
	je bstep_iret
1323
	cmpq $gs_change,RIP+8(%rsp)
1324
	je error_swapgs
1325
	jmp error_sti
1326 1327 1328 1329

bstep_iret:
	/* Fix truncated RIP */
	movq %rcx,RIP+8(%rsp)
A
Andy Lutomirski 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338
	/* fall through */

error_bad_iret:
	SWAPGS
	mov %rsp,%rdi
	call fixup_bad_iret
	mov %rax,%rsp
	decl %ebx	/* Return to usergs */
	jmp error_sti
1339
	CFI_ENDPROC
1340 1341 1342
END(error_entry)


1343
/* On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) */
1344 1345 1346
ENTRY(error_exit)
	DEFAULT_FRAME
	movl %ebx,%eax
1347
	RESTORE_EXTRA_REGS
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
	DISABLE_INTERRUPTS(CLBR_NONE)
	TRACE_IRQS_OFF
	GET_THREAD_INFO(%rcx)
	testl %eax,%eax
	jne retint_kernel
	LOCKDEP_SYS_EXIT_IRQ
	movl TI_flags(%rcx),%edx
	movl $_TIF_WORK_MASK,%edi
	andl %edi,%edx
	jnz retint_careful
	jmp retint_swapgs
	CFI_ENDPROC
END(error_exit)

1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
/*
 * Test if a given stack is an NMI stack or not.
 */
	.macro test_in_nmi reg stack nmi_ret normal_ret
	cmpq %\reg, \stack
	ja \normal_ret
	subq $EXCEPTION_STKSZ, %\reg
	cmpq %\reg, \stack
	jb \normal_ret
	jmp \nmi_ret
	.endm
1373 1374 1375 1376 1377

	/* runs on exception stack */
ENTRY(nmi)
	INTR_FRAME
	PARAVIRT_ADJUST_EXCEPTION_FRAME
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	/*
	 * We allow breakpoints in NMIs. If a breakpoint occurs, then
	 * the iretq it performs will take us out of NMI context.
	 * This means that we can have nested NMIs where the next
	 * NMI is using the top of the stack of the previous NMI. We
	 * can't let it execute because the nested NMI will corrupt the
	 * stack of the previous NMI. NMI handlers are not re-entrant
	 * anyway.
	 *
	 * To handle this case we do the following:
	 *  Check the a special location on the stack that contains
	 *  a variable that is set when NMIs are executing.
	 *  The interrupted task's stack is also checked to see if it
	 *  is an NMI stack.
	 *  If the variable is not set and the stack is not the NMI
	 *  stack then:
	 *    o Set the special variable on the stack
	 *    o Copy the interrupt frame into a "saved" location on the stack
	 *    o Copy the interrupt frame into a "copy" location on the stack
	 *    o Continue processing the NMI
	 *  If the variable is set or the previous stack is the NMI stack:
	 *    o Modify the "copy" location to jump to the repeate_nmi
	 *    o return back to the first NMI
	 *
	 * Now on exit of the first NMI, we first clear the stack variable
	 * The NMI stack will tell any nested NMIs at that point that it is
	 * nested. Then we pop the stack normally with iret, and if there was
	 * a nested NMI that updated the copy interrupt stack frame, a
	 * jump will be made to the repeat_nmi code that will handle the second
	 * NMI.
	 */

	/* Use %rdx as out temp variable throughout */
	pushq_cfi %rdx
1412
	CFI_REL_OFFSET rdx, 0
1413

1414 1415 1416 1417
	/*
	 * If %cs was not the kernel segment, then the NMI triggered in user
	 * space, which means it is definitely not nested.
	 */
1418
	cmpl $__KERNEL_CS, 16(%rsp)
1419 1420
	jne first_nmi

1421 1422 1423 1424
	/*
	 * Check the special variable on the stack to see if NMIs are
	 * executing.
	 */
1425
	cmpl $1, -8(%rsp)
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
	je nested_nmi

	/*
	 * Now test if the previous stack was an NMI stack.
	 * We need the double check. We check the NMI stack to satisfy the
	 * race when the first NMI clears the variable before returning.
	 * We check the variable because the first NMI could be in a
	 * breakpoint routine using a breakpoint stack.
	 */
	lea 6*8(%rsp), %rdx
	test_in_nmi rdx, 4*8(%rsp), nested_nmi, first_nmi
1437
	CFI_REMEMBER_STATE
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453

nested_nmi:
	/*
	 * Do nothing if we interrupted the fixup in repeat_nmi.
	 * It's about to repeat the NMI handler, so we are fine
	 * with ignoring this one.
	 */
	movq $repeat_nmi, %rdx
	cmpq 8(%rsp), %rdx
	ja 1f
	movq $end_repeat_nmi, %rdx
	cmpq 8(%rsp), %rdx
	ja nested_nmi_out

1:
	/* Set up the interrupted NMIs stack to jump to repeat_nmi */
1454
	leaq -1*8(%rsp), %rdx
1455
	movq %rdx, %rsp
1456 1457
	CFI_ADJUST_CFA_OFFSET 1*8
	leaq -10*8(%rsp), %rdx
1458 1459 1460 1461 1462 1463 1464
	pushq_cfi $__KERNEL_DS
	pushq_cfi %rdx
	pushfq_cfi
	pushq_cfi $__KERNEL_CS
	pushq_cfi $repeat_nmi

	/* Put stack back */
1465 1466
	addq $(6*8), %rsp
	CFI_ADJUST_CFA_OFFSET -6*8
1467 1468 1469

nested_nmi_out:
	popq_cfi %rdx
1470
	CFI_RESTORE rdx
1471 1472 1473 1474

	/* No need to check faults here */
	INTERRUPT_RETURN

1475
	CFI_RESTORE_STATE
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
first_nmi:
	/*
	 * Because nested NMIs will use the pushed location that we
	 * stored in rdx, we must keep that space available.
	 * Here's what our stack frame will look like:
	 * +-------------------------+
	 * | original SS             |
	 * | original Return RSP     |
	 * | original RFLAGS         |
	 * | original CS             |
	 * | original RIP            |
	 * +-------------------------+
	 * | temp storage for rdx    |
	 * +-------------------------+
	 * | NMI executing variable  |
	 * +-------------------------+
	 * | copied SS               |
	 * | copied Return RSP       |
	 * | copied RFLAGS           |
	 * | copied CS               |
	 * | copied RIP              |
	 * +-------------------------+
1498 1499 1500 1501 1502 1503
	 * | Saved SS                |
	 * | Saved Return RSP        |
	 * | Saved RFLAGS            |
	 * | Saved CS                |
	 * | Saved RIP               |
	 * +-------------------------+
1504 1505 1506
	 * | pt_regs                 |
	 * +-------------------------+
	 *
1507 1508 1509
	 * The saved stack frame is used to fix up the copied stack frame
	 * that a nested NMI may change to make the interrupted NMI iret jump
	 * to the repeat_nmi. The original stack frame and the temp storage
1510 1511
	 * is also used by nested NMIs and can not be trusted on exit.
	 */
1512
	/* Do not pop rdx, nested NMIs will corrupt that part of the stack */
1513 1514 1515
	movq (%rsp), %rdx
	CFI_RESTORE rdx

1516 1517 1518
	/* Set the NMI executing variable on the stack. */
	pushq_cfi $1

1519 1520 1521 1522
	/*
	 * Leave room for the "copied" frame
	 */
	subq $(5*8), %rsp
1523
	CFI_ADJUST_CFA_OFFSET 5*8
1524

1525 1526
	/* Copy the stack frame to the Saved frame */
	.rept 5
1527
	pushq_cfi 11*8(%rsp)
1528
	.endr
1529
	CFI_DEF_CFA_OFFSET 5*8
1530

1531 1532
	/* Everything up to here is safe from nested NMIs */

1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
	/*
	 * If there was a nested NMI, the first NMI's iret will return
	 * here. But NMIs are still enabled and we can take another
	 * nested NMI. The nested NMI checks the interrupted RIP to see
	 * if it is between repeat_nmi and end_repeat_nmi, and if so
	 * it will just return, as we are about to repeat an NMI anyway.
	 * This makes it safe to copy to the stack frame that a nested
	 * NMI will update.
	 */
repeat_nmi:
	/*
	 * Update the stack variable to say we are still in NMI (the update
	 * is benign for the non-repeat case, where 1 was pushed just above
	 * to this very stack slot).
	 */
1548
	movq $1, 10*8(%rsp)
1549 1550

	/* Make another copy, this one may be modified by nested NMIs */
1551 1552
	addq $(10*8), %rsp
	CFI_ADJUST_CFA_OFFSET -10*8
1553
	.rept 5
1554
	pushq_cfi -6*8(%rsp)
1555
	.endr
1556
	subq $(5*8), %rsp
1557
	CFI_DEF_CFA_OFFSET 5*8
1558
end_repeat_nmi:
1559 1560 1561

	/*
	 * Everything below this point can be preempted by a nested
1562 1563
	 * NMI if the first NMI took an exception and reset our iret stack
	 * so that we repeat another NMI.
1564
	 */
1565
	pushq_cfi $-1		/* ORIG_RAX: no syscall to restart */
1566 1567
	ALLOC_PT_GPREGS_ON_STACK

1568
	/*
1569
	 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1570 1571 1572 1573 1574
	 * as we should not be calling schedule in NMI context.
	 * Even with normal interrupts enabled. An NMI should not be
	 * setting NEED_RESCHED or anything that normal interrupts and
	 * exceptions might do.
	 */
1575
	call paranoid_entry
1576
	DEFAULT_FRAME 0
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588

	/*
	 * Save off the CR2 register. If we take a page fault in the NMI then
	 * it could corrupt the CR2 value. If the NMI preempts a page fault
	 * handler before it was able to read the CR2 register, and then the
	 * NMI itself takes a page fault, the page fault that was preempted
	 * will read the information from the NMI page fault and not the
	 * origin fault. Save it off and restore it if it changes.
	 * Use the r12 callee-saved register.
	 */
	movq %cr2, %r12

1589 1590 1591 1592
	/* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
	movq %rsp,%rdi
	movq $-1,%rsi
	call do_nmi
1593 1594 1595 1596 1597 1598 1599 1600

	/* Did the NMI take a page fault? Restore cr2 if it did */
	movq %cr2, %rcx
	cmpq %rcx, %r12
	je 1f
	movq %r12, %cr2
1:
	
1601 1602 1603 1604 1605
	testl %ebx,%ebx				/* swapgs needed? */
	jnz nmi_restore
nmi_swapgs:
	SWAPGS_UNSAFE_STACK
nmi_restore:
1606 1607
	RESTORE_EXTRA_REGS
	RESTORE_C_REGS
1608
	/* Pop the extra iret frame at once */
1609
	REMOVE_PT_GPREGS_FROM_STACK 6*8
1610

1611
	/* Clear the NMI executing stack variable */
1612
	movq $0, 5*8(%rsp)
1613
	jmp irq_return
1614
	CFI_ENDPROC
1615 1616 1617 1618 1619 1620 1621 1622 1623
END(nmi)

ENTRY(ignore_sysret)
	CFI_STARTPROC
	mov $-ENOSYS,%eax
	sysret
	CFI_ENDPROC
END(ignore_sysret)