xref: /linux/arch/x86/entry/entry_64.S (revision fd501d4f0399700011acde486576c7c1eb8e7a61)
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 *  linux/arch/x86_64/entry.S
4 *
5 *  Copyright (C) 1991, 1992  Linus Torvalds
6 *  Copyright (C) 2000, 2001, 2002  Andi Kleen SuSE Labs
7 *  Copyright (C) 2000  Pavel Machek <pavel@suse.cz>
8 *
9 * entry.S contains the system-call and fault low-level handling routines.
10 *
11 * Some of this is documented in Documentation/x86/entry_64.rst
12 *
13 * A note on terminology:
14 * - iret frame:	Architecture defined interrupt frame from SS to RIP
15 *			at the top of the kernel process stack.
16 *
17 * Some macro usage:
18 * - SYM_FUNC_START/END:Define functions in the symbol table.
19 * - idtentry:		Define exception entry points.
20 */
21#include <linux/linkage.h>
22#include <asm/segment.h>
23#include <asm/cache.h>
24#include <asm/errno.h>
25#include <asm/asm-offsets.h>
26#include <asm/msr.h>
27#include <asm/unistd.h>
28#include <asm/thread_info.h>
29#include <asm/hw_irq.h>
30#include <asm/page_types.h>
31#include <asm/irqflags.h>
32#include <asm/paravirt.h>
33#include <asm/percpu.h>
34#include <asm/asm.h>
35#include <asm/smap.h>
36#include <asm/pgtable_types.h>
37#include <asm/export.h>
38#include <asm/frame.h>
39#include <asm/trapnr.h>
40#include <asm/nospec-branch.h>
41#include <linux/err.h>
42
43#include "calling.h"
44
45.code64
46.section .entry.text, "ax"
47
48#ifdef CONFIG_PARAVIRT
49SYM_CODE_START(native_usergs_sysret64)
50	UNWIND_HINT_EMPTY
51	swapgs
52	sysretq
53SYM_CODE_END(native_usergs_sysret64)
54#endif /* CONFIG_PARAVIRT */
55
56/*
57 * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
58 *
59 * This is the only entry point used for 64-bit system calls.  The
60 * hardware interface is reasonably well designed and the register to
61 * argument mapping Linux uses fits well with the registers that are
62 * available when SYSCALL is used.
63 *
64 * SYSCALL instructions can be found inlined in libc implementations as
65 * well as some other programs and libraries.  There are also a handful
66 * of SYSCALL instructions in the vDSO used, for example, as a
67 * clock_gettimeofday fallback.
68 *
69 * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
70 * then loads new ss, cs, and rip from previously programmed MSRs.
71 * rflags gets masked by a value from another MSR (so CLD and CLAC
72 * are not needed). SYSCALL does not save anything on the stack
73 * and does not change rsp.
74 *
75 * Registers on entry:
76 * rax  system call number
77 * rcx  return address
78 * r11  saved rflags (note: r11 is callee-clobbered register in C ABI)
79 * rdi  arg0
80 * rsi  arg1
81 * rdx  arg2
82 * r10  arg3 (needs to be moved to rcx to conform to C ABI)
83 * r8   arg4
84 * r9   arg5
85 * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
86 *
87 * Only called from user space.
88 *
89 * When user can change pt_regs->foo always force IRET. That is because
90 * it deals with uncanonical addresses better. SYSRET has trouble
91 * with them due to bugs in both AMD and Intel CPUs.
92 */
93
94SYM_CODE_START(entry_SYSCALL_64)
95	UNWIND_HINT_EMPTY
96
97	swapgs
98	/* tss.sp2 is scratch space. */
99	movq	%rsp, PER_CPU_VAR(cpu_tss_rw + TSS_sp2)
100	SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
101	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
102
103	/* Construct struct pt_regs on stack */
104	pushq	$__USER_DS				/* pt_regs->ss */
105	pushq	PER_CPU_VAR(cpu_tss_rw + TSS_sp2)	/* pt_regs->sp */
106	pushq	%r11					/* pt_regs->flags */
107	pushq	$__USER_CS				/* pt_regs->cs */
108	pushq	%rcx					/* pt_regs->ip */
109SYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL)
110	pushq	%rax					/* pt_regs->orig_ax */
111
112	PUSH_AND_CLEAR_REGS rax=$-ENOSYS
113
114	/* IRQs are off. */
115	movq	%rax, %rdi
116	movq	%rsp, %rsi
117	call	do_syscall_64		/* returns with IRQs disabled */
118
119	/*
120	 * Try to use SYSRET instead of IRET if we're returning to
121	 * a completely clean 64-bit userspace context.  If we're not,
122	 * go to the slow exit path.
123	 */
124	movq	RCX(%rsp), %rcx
125	movq	RIP(%rsp), %r11
126
127	cmpq	%rcx, %r11	/* SYSRET requires RCX == RIP */
128	jne	swapgs_restore_regs_and_return_to_usermode
129
130	/*
131	 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
132	 * in kernel space.  This essentially lets the user take over
133	 * the kernel, since userspace controls RSP.
134	 *
135	 * If width of "canonical tail" ever becomes variable, this will need
136	 * to be updated to remain correct on both old and new CPUs.
137	 *
138	 * Change top bits to match most significant bit (47th or 56th bit
139	 * depending on paging mode) in the address.
140	 */
141#ifdef CONFIG_X86_5LEVEL
142	ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \
143		"shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57
144#else
145	shl	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
146	sar	$(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
147#endif
148
149	/* If this changed %rcx, it was not canonical */
150	cmpq	%rcx, %r11
151	jne	swapgs_restore_regs_and_return_to_usermode
152
153	cmpq	$__USER_CS, CS(%rsp)		/* CS must match SYSRET */
154	jne	swapgs_restore_regs_and_return_to_usermode
155
156	movq	R11(%rsp), %r11
157	cmpq	%r11, EFLAGS(%rsp)		/* R11 == RFLAGS */
158	jne	swapgs_restore_regs_and_return_to_usermode
159
160	/*
161	 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
162	 * restore RF properly. If the slowpath sets it for whatever reason, we
163	 * need to restore it correctly.
164	 *
165	 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
166	 * trap from userspace immediately after SYSRET.  This would cause an
167	 * infinite loop whenever #DB happens with register state that satisfies
168	 * the opportunistic SYSRET conditions.  For example, single-stepping
169	 * this user code:
170	 *
171	 *           movq	$stuck_here, %rcx
172	 *           pushfq
173	 *           popq %r11
174	 *   stuck_here:
175	 *
176	 * would never get past 'stuck_here'.
177	 */
178	testq	$(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
179	jnz	swapgs_restore_regs_and_return_to_usermode
180
181	/* nothing to check for RSP */
182
183	cmpq	$__USER_DS, SS(%rsp)		/* SS must match SYSRET */
184	jne	swapgs_restore_regs_and_return_to_usermode
185
186	/*
187	 * We win! This label is here just for ease of understanding
188	 * perf profiles. Nothing jumps here.
189	 */
190syscall_return_via_sysret:
191	/* rcx and r11 are already restored (see code above) */
192	POP_REGS pop_rdi=0 skip_r11rcx=1
193
194	/*
195	 * Now all regs are restored except RSP and RDI.
196	 * Save old stack pointer and switch to trampoline stack.
197	 */
198	movq	%rsp, %rdi
199	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
200	UNWIND_HINT_EMPTY
201
202	pushq	RSP-RDI(%rdi)	/* RSP */
203	pushq	(%rdi)		/* RDI */
204
205	/*
206	 * We are on the trampoline stack.  All regs except RDI are live.
207	 * We can do future final exit work right here.
208	 */
209	STACKLEAK_ERASE_NOCLOBBER
210
211	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
212
213	popq	%rdi
214	popq	%rsp
215	USERGS_SYSRET64
216SYM_CODE_END(entry_SYSCALL_64)
217
218/*
219 * %rdi: prev task
220 * %rsi: next task
221 */
222.pushsection .text, "ax"
223SYM_FUNC_START(__switch_to_asm)
224	/*
225	 * Save callee-saved registers
226	 * This must match the order in inactive_task_frame
227	 */
228	pushq	%rbp
229	pushq	%rbx
230	pushq	%r12
231	pushq	%r13
232	pushq	%r14
233	pushq	%r15
234
235	/* switch stack */
236	movq	%rsp, TASK_threadsp(%rdi)
237	movq	TASK_threadsp(%rsi), %rsp
238
239#ifdef CONFIG_STACKPROTECTOR
240	movq	TASK_stack_canary(%rsi), %rbx
241	movq	%rbx, PER_CPU_VAR(fixed_percpu_data) + stack_canary_offset
242#endif
243
244#ifdef CONFIG_RETPOLINE
245	/*
246	 * When switching from a shallower to a deeper call stack
247	 * the RSB may either underflow or use entries populated
248	 * with userspace addresses. On CPUs where those concerns
249	 * exist, overwrite the RSB with entries which capture
250	 * speculative execution to prevent attack.
251	 */
252	FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
253#endif
254
255	/* restore callee-saved registers */
256	popq	%r15
257	popq	%r14
258	popq	%r13
259	popq	%r12
260	popq	%rbx
261	popq	%rbp
262
263	jmp	__switch_to
264SYM_FUNC_END(__switch_to_asm)
265.popsection
266
267/*
268 * A newly forked process directly context switches into this address.
269 *
270 * rax: prev task we switched from
271 * rbx: kernel thread func (NULL for user thread)
272 * r12: kernel thread arg
273 */
274.pushsection .text, "ax"
275SYM_CODE_START(ret_from_fork)
276	UNWIND_HINT_EMPTY
277	movq	%rax, %rdi
278	call	schedule_tail			/* rdi: 'prev' task parameter */
279
280	testq	%rbx, %rbx			/* from kernel_thread? */
281	jnz	1f				/* kernel threads are uncommon */
282
2832:
284	UNWIND_HINT_REGS
285	movq	%rsp, %rdi
286	call	syscall_return_slowpath	/* returns with IRQs disabled */
287	jmp	swapgs_restore_regs_and_return_to_usermode
288
2891:
290	/* kernel thread */
291	UNWIND_HINT_EMPTY
292	movq	%r12, %rdi
293	CALL_NOSPEC rbx
294	/*
295	 * A kernel thread is allowed to return here after successfully
296	 * calling do_execve().  Exit to userspace to complete the execve()
297	 * syscall.
298	 */
299	movq	$0, RAX(%rsp)
300	jmp	2b
301SYM_CODE_END(ret_from_fork)
302.popsection
303
304.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
305#ifdef CONFIG_DEBUG_ENTRY
306	pushq %rax
307	SAVE_FLAGS(CLBR_RAX)
308	testl $X86_EFLAGS_IF, %eax
309	jz .Lokay_\@
310	ud2
311.Lokay_\@:
312	popq %rax
313#endif
314.endm
315
316/**
317 * idtentry_body - Macro to emit code calling the C function
318 * @cfunc:		C function to be called
319 * @has_error_code:	Hardware pushed error code on stack
320 */
321.macro idtentry_body cfunc has_error_code:req
322
323	call	error_entry
324	UNWIND_HINT_REGS
325
326	movq	%rsp, %rdi			/* pt_regs pointer into 1st argument*/
327
328	.if \has_error_code == 1
329		movq	ORIG_RAX(%rsp), %rsi	/* get error code into 2nd argument*/
330		movq	$-1, ORIG_RAX(%rsp)	/* no syscall to restart */
331	.endif
332
333	call	\cfunc
334
335	jmp	error_return
336.endm
337
338/**
339 * idtentry - Macro to generate entry stubs for simple IDT entries
340 * @vector:		Vector number
341 * @asmsym:		ASM symbol for the entry point
342 * @cfunc:		C function to be called
343 * @has_error_code:	Hardware pushed error code on stack
344 *
345 * The macro emits code to set up the kernel context for straight forward
346 * and simple IDT entries. No IST stack, no paranoid entry checks.
347 */
348.macro idtentry vector asmsym cfunc has_error_code:req
349SYM_CODE_START(\asmsym)
350	UNWIND_HINT_IRET_REGS offset=\has_error_code*8
351	ASM_CLAC
352
353	.if \has_error_code == 0
354		pushq	$-1			/* ORIG_RAX: no syscall to restart */
355	.endif
356
357	.if \vector == X86_TRAP_BP
358		/*
359		 * If coming from kernel space, create a 6-word gap to allow the
360		 * int3 handler to emulate a call instruction.
361		 */
362		testb	$3, CS-ORIG_RAX(%rsp)
363		jnz	.Lfrom_usermode_no_gap_\@
364		.rept	6
365		pushq	5*8(%rsp)
366		.endr
367		UNWIND_HINT_IRET_REGS offset=8
368.Lfrom_usermode_no_gap_\@:
369	.endif
370
371	idtentry_body \cfunc \has_error_code
372
373_ASM_NOKPROBE(\asmsym)
374SYM_CODE_END(\asmsym)
375.endm
376
377/*
378 * Interrupt entry/exit.
379 *
380 + The interrupt stubs push (vector) onto the stack, which is the error_code
381 * position of idtentry exceptions, and jump to one of the two idtentry points
382 * (common/spurious).
383 *
384 * common_interrupt is a hotpath, align it to a cache line
385 */
386.macro idtentry_irq vector cfunc
387	.p2align CONFIG_X86_L1_CACHE_SHIFT
388	idtentry \vector asm_\cfunc \cfunc has_error_code=1
389.endm
390
391/*
392 * System vectors which invoke their handlers directly and are not
393 * going through the regular common device interrupt handling code.
394 */
395.macro idtentry_sysvec vector cfunc
396	idtentry \vector asm_\cfunc \cfunc has_error_code=0
397.endm
398
399/**
400 * idtentry_mce_db - Macro to generate entry stubs for #MC and #DB
401 * @vector:		Vector number
402 * @asmsym:		ASM symbol for the entry point
403 * @cfunc:		C function to be called
404 *
405 * The macro emits code to set up the kernel context for #MC and #DB
406 *
407 * If the entry comes from user space it uses the normal entry path
408 * including the return to user space work and preemption checks on
409 * exit.
410 *
411 * If hits in kernel mode then it needs to go through the paranoid
412 * entry as the exception can hit any random state. No preemption
413 * check on exit to keep the paranoid path simple.
414 */
415.macro idtentry_mce_db vector asmsym cfunc
416SYM_CODE_START(\asmsym)
417	UNWIND_HINT_IRET_REGS
418	ASM_CLAC
419
420	pushq	$-1			/* ORIG_RAX: no syscall to restart */
421
422	/*
423	 * If the entry is from userspace, switch stacks and treat it as
424	 * a normal entry.
425	 */
426	testb	$3, CS-ORIG_RAX(%rsp)
427	jnz	.Lfrom_usermode_switch_stack_\@
428
429	/*
430	 * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX.
431	 * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS
432	 */
433	call	paranoid_entry
434
435	UNWIND_HINT_REGS
436
437	movq	%rsp, %rdi		/* pt_regs pointer */
438
439	call	\cfunc
440
441	jmp	paranoid_exit
442
443	/* Switch to the regular task stack and use the noist entry point */
444.Lfrom_usermode_switch_stack_\@:
445	idtentry_body noist_\cfunc, has_error_code=0
446
447_ASM_NOKPROBE(\asmsym)
448SYM_CODE_END(\asmsym)
449.endm
450
451/*
452 * Double fault entry. Straight paranoid. No checks from which context
453 * this comes because for the espfix induced #DF this would do the wrong
454 * thing.
455 */
456.macro idtentry_df vector asmsym cfunc
457SYM_CODE_START(\asmsym)
458	UNWIND_HINT_IRET_REGS offset=8
459	ASM_CLAC
460
461	/*
462	 * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX.
463	 * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS
464	 */
465	call	paranoid_entry
466	UNWIND_HINT_REGS
467
468	movq	%rsp, %rdi		/* pt_regs pointer into first argument */
469	movq	ORIG_RAX(%rsp), %rsi	/* get error code into 2nd argument*/
470	movq	$-1, ORIG_RAX(%rsp)	/* no syscall to restart */
471	call	\cfunc
472
473	jmp	paranoid_exit
474
475_ASM_NOKPROBE(\asmsym)
476SYM_CODE_END(\asmsym)
477.endm
478
479/*
480 * Include the defines which emit the idt entries which are shared
481 * shared between 32 and 64 bit.
482 */
483#include <asm/idtentry.h>
484
485SYM_CODE_START_LOCAL(common_interrupt_return)
486SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
487#ifdef CONFIG_DEBUG_ENTRY
488	/* Assert that pt_regs indicates user mode. */
489	testb	$3, CS(%rsp)
490	jnz	1f
491	ud2
4921:
493#endif
494	POP_REGS pop_rdi=0
495
496	/*
497	 * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS.
498	 * Save old stack pointer and switch to trampoline stack.
499	 */
500	movq	%rsp, %rdi
501	movq	PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
502	UNWIND_HINT_EMPTY
503
504	/* Copy the IRET frame to the trampoline stack. */
505	pushq	6*8(%rdi)	/* SS */
506	pushq	5*8(%rdi)	/* RSP */
507	pushq	4*8(%rdi)	/* EFLAGS */
508	pushq	3*8(%rdi)	/* CS */
509	pushq	2*8(%rdi)	/* RIP */
510
511	/* Push user RDI on the trampoline stack. */
512	pushq	(%rdi)
513
514	/*
515	 * We are on the trampoline stack.  All regs except RDI are live.
516	 * We can do future final exit work right here.
517	 */
518	STACKLEAK_ERASE_NOCLOBBER
519
520	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
521
522	/* Restore RDI. */
523	popq	%rdi
524	SWAPGS
525	INTERRUPT_RETURN
526
527
528SYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)
529#ifdef CONFIG_DEBUG_ENTRY
530	/* Assert that pt_regs indicates kernel mode. */
531	testb	$3, CS(%rsp)
532	jz	1f
533	ud2
5341:
535#endif
536	POP_REGS
537	addq	$8, %rsp	/* skip regs->orig_ax */
538	/*
539	 * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
540	 * when returning from IPI handler.
541	 */
542	INTERRUPT_RETURN
543
544SYM_INNER_LABEL_ALIGN(native_iret, SYM_L_GLOBAL)
545	UNWIND_HINT_IRET_REGS
546	/*
547	 * Are we returning to a stack segment from the LDT?  Note: in
548	 * 64-bit mode SS:RSP on the exception stack is always valid.
549	 */
550#ifdef CONFIG_X86_ESPFIX64
551	testb	$4, (SS-RIP)(%rsp)
552	jnz	native_irq_return_ldt
553#endif
554
555SYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL)
556	/*
557	 * This may fault.  Non-paranoid faults on return to userspace are
558	 * handled by fixup_bad_iret.  These include #SS, #GP, and #NP.
559	 * Double-faults due to espfix64 are handled in exc_double_fault.
560	 * Other faults here are fatal.
561	 */
562	iretq
563
564#ifdef CONFIG_X86_ESPFIX64
565native_irq_return_ldt:
566	/*
567	 * We are running with user GSBASE.  All GPRs contain their user
568	 * values.  We have a percpu ESPFIX stack that is eight slots
569	 * long (see ESPFIX_STACK_SIZE).  espfix_waddr points to the bottom
570	 * of the ESPFIX stack.
571	 *
572	 * We clobber RAX and RDI in this code.  We stash RDI on the
573	 * normal stack and RAX on the ESPFIX stack.
574	 *
575	 * The ESPFIX stack layout we set up looks like this:
576	 *
577	 * --- top of ESPFIX stack ---
578	 * SS
579	 * RSP
580	 * RFLAGS
581	 * CS
582	 * RIP  <-- RSP points here when we're done
583	 * RAX  <-- espfix_waddr points here
584	 * --- bottom of ESPFIX stack ---
585	 */
586
587	pushq	%rdi				/* Stash user RDI */
588	SWAPGS					/* to kernel GS */
589	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi	/* to kernel CR3 */
590
591	movq	PER_CPU_VAR(espfix_waddr), %rdi
592	movq	%rax, (0*8)(%rdi)		/* user RAX */
593	movq	(1*8)(%rsp), %rax		/* user RIP */
594	movq	%rax, (1*8)(%rdi)
595	movq	(2*8)(%rsp), %rax		/* user CS */
596	movq	%rax, (2*8)(%rdi)
597	movq	(3*8)(%rsp), %rax		/* user RFLAGS */
598	movq	%rax, (3*8)(%rdi)
599	movq	(5*8)(%rsp), %rax		/* user SS */
600	movq	%rax, (5*8)(%rdi)
601	movq	(4*8)(%rsp), %rax		/* user RSP */
602	movq	%rax, (4*8)(%rdi)
603	/* Now RAX == RSP. */
604
605	andl	$0xffff0000, %eax		/* RAX = (RSP & 0xffff0000) */
606
607	/*
608	 * espfix_stack[31:16] == 0.  The page tables are set up such that
609	 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
610	 * espfix_waddr for any X.  That is, there are 65536 RO aliases of
611	 * the same page.  Set up RSP so that RSP[31:16] contains the
612	 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
613	 * still points to an RO alias of the ESPFIX stack.
614	 */
615	orq	PER_CPU_VAR(espfix_stack), %rax
616
617	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
618	SWAPGS					/* to user GS */
619	popq	%rdi				/* Restore user RDI */
620
621	movq	%rax, %rsp
622	UNWIND_HINT_IRET_REGS offset=8
623
624	/*
625	 * At this point, we cannot write to the stack any more, but we can
626	 * still read.
627	 */
628	popq	%rax				/* Restore user RAX */
629
630	/*
631	 * RSP now points to an ordinary IRET frame, except that the page
632	 * is read-only and RSP[31:16] are preloaded with the userspace
633	 * values.  We can now IRET back to userspace.
634	 */
635	jmp	native_irq_return_iret
636#endif
637SYM_CODE_END(common_interrupt_return)
638_ASM_NOKPROBE(common_interrupt_return)
639
640/*
641 * Reload gs selector with exception handling
642 * edi:  new selector
643 *
644 * Is in entry.text as it shouldn't be instrumented.
645 */
646SYM_FUNC_START(asm_load_gs_index)
647	FRAME_BEGIN
648	swapgs
649.Lgs_change:
650	movl	%edi, %gs
6512:	ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
652	swapgs
653	FRAME_END
654	ret
655SYM_FUNC_END(asm_load_gs_index)
656EXPORT_SYMBOL(asm_load_gs_index)
657
658	_ASM_EXTABLE(.Lgs_change, .Lbad_gs)
659	.section .fixup, "ax"
660	/* running with kernelgs */
661SYM_CODE_START_LOCAL_NOALIGN(.Lbad_gs)
662	swapgs					/* switch back to user gs */
663.macro ZAP_GS
664	/* This can't be a string because the preprocessor needs to see it. */
665	movl $__USER_DS, %eax
666	movl %eax, %gs
667.endm
668	ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
669	xorl	%eax, %eax
670	movl	%eax, %gs
671	jmp	2b
672SYM_CODE_END(.Lbad_gs)
673	.previous
674
675/*
676 * rdi: New stack pointer points to the top word of the stack
677 * rsi: Function pointer
678 * rdx: Function argument (can be NULL if none)
679 */
680SYM_FUNC_START(asm_call_on_stack)
681	/*
682	 * Save the frame pointer unconditionally. This allows the ORC
683	 * unwinder to handle the stack switch.
684	 */
685	pushq		%rbp
686	mov		%rsp, %rbp
687
688	/*
689	 * The unwinder relies on the word at the top of the new stack
690	 * page linking back to the previous RSP.
691	 */
692	mov		%rsp, (%rdi)
693	mov		%rdi, %rsp
694	/* Move the argument to the right place */
695	mov		%rdx, %rdi
696
6971:
698	.pushsection .discard.instr_begin
699	.long 1b - .
700	.popsection
701
702	CALL_NOSPEC	rsi
703
7042:
705	.pushsection .discard.instr_end
706	.long 2b - .
707	.popsection
708
709	/* Restore the previous stack pointer from RBP. */
710	leaveq
711	ret
712SYM_FUNC_END(asm_call_on_stack)
713
714#ifdef CONFIG_XEN_PV
715/*
716 * A note on the "critical region" in our callback handler.
717 * We want to avoid stacking callback handlers due to events occurring
718 * during handling of the last event. To do this, we keep events disabled
719 * until we've done all processing. HOWEVER, we must enable events before
720 * popping the stack frame (can't be done atomically) and so it would still
721 * be possible to get enough handler activations to overflow the stack.
722 * Although unlikely, bugs of that kind are hard to track down, so we'd
723 * like to avoid the possibility.
724 * So, on entry to the handler we detect whether we interrupted an
725 * existing activation in its critical region -- if so, we pop the current
726 * activation and restart the handler using the previous one.
727 *
728 * C calling convention: exc_xen_hypervisor_callback(struct *pt_regs)
729 */
730SYM_CODE_START_LOCAL(exc_xen_hypervisor_callback)
731
732/*
733 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
734 * see the correct pointer to the pt_regs
735 */
736	UNWIND_HINT_FUNC
737	movq	%rdi, %rsp			/* we don't return, adjust the stack frame */
738	UNWIND_HINT_REGS
739
740	call	xen_pv_evtchn_do_upcall
741
742	jmp	error_return
743SYM_CODE_END(exc_xen_hypervisor_callback)
744
745/*
746 * Hypervisor uses this for application faults while it executes.
747 * We get here for two reasons:
748 *  1. Fault while reloading DS, ES, FS or GS
749 *  2. Fault while executing IRET
750 * Category 1 we do not need to fix up as Xen has already reloaded all segment
751 * registers that could be reloaded and zeroed the others.
752 * Category 2 we fix up by killing the current process. We cannot use the
753 * normal Linux return path in this case because if we use the IRET hypercall
754 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
755 * We distinguish between categories by comparing each saved segment register
756 * with its current contents: any discrepancy means we in category 1.
757 */
758SYM_CODE_START(xen_failsafe_callback)
759	UNWIND_HINT_EMPTY
760	movl	%ds, %ecx
761	cmpw	%cx, 0x10(%rsp)
762	jne	1f
763	movl	%es, %ecx
764	cmpw	%cx, 0x18(%rsp)
765	jne	1f
766	movl	%fs, %ecx
767	cmpw	%cx, 0x20(%rsp)
768	jne	1f
769	movl	%gs, %ecx
770	cmpw	%cx, 0x28(%rsp)
771	jne	1f
772	/* All segments match their saved values => Category 2 (Bad IRET). */
773	movq	(%rsp), %rcx
774	movq	8(%rsp), %r11
775	addq	$0x30, %rsp
776	pushq	$0				/* RIP */
777	UNWIND_HINT_IRET_REGS offset=8
778	jmp	asm_exc_general_protection
7791:	/* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
780	movq	(%rsp), %rcx
781	movq	8(%rsp), %r11
782	addq	$0x30, %rsp
783	UNWIND_HINT_IRET_REGS
784	pushq	$-1 /* orig_ax = -1 => not a system call */
785	PUSH_AND_CLEAR_REGS
786	ENCODE_FRAME_POINTER
787	jmp	error_return
788SYM_CODE_END(xen_failsafe_callback)
789#endif /* CONFIG_XEN_PV */
790
791/*
792 * Save all registers in pt_regs, and switch gs if needed.
793 * Use slow, but surefire "are we in kernel?" check.
794 * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
795 */
796SYM_CODE_START_LOCAL(paranoid_entry)
797	UNWIND_HINT_FUNC
798	cld
799	PUSH_AND_CLEAR_REGS save_ret=1
800	ENCODE_FRAME_POINTER 8
801	movl	$1, %ebx
802	movl	$MSR_GS_BASE, %ecx
803	rdmsr
804	testl	%edx, %edx
805	js	1f				/* negative -> in kernel */
806	SWAPGS
807	xorl	%ebx, %ebx
808
8091:
810	/*
811	 * Always stash CR3 in %r14.  This value will be restored,
812	 * verbatim, at exit.  Needed if paranoid_entry interrupted
813	 * another entry that already switched to the user CR3 value
814	 * but has not yet returned to userspace.
815	 *
816	 * This is also why CS (stashed in the "iret frame" by the
817	 * hardware at entry) can not be used: this may be a return
818	 * to kernel code, but with a user CR3 value.
819	 */
820	SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
821
822	/*
823	 * The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an
824	 * unconditional CR3 write, even in the PTI case.  So do an lfence
825	 * to prevent GS speculation, regardless of whether PTI is enabled.
826	 */
827	FENCE_SWAPGS_KERNEL_ENTRY
828
829	ret
830SYM_CODE_END(paranoid_entry)
831
832/*
833 * "Paranoid" exit path from exception stack.  This is invoked
834 * only on return from non-NMI IST interrupts that came
835 * from kernel space.
836 *
837 * We may be returning to very strange contexts (e.g. very early
838 * in syscall entry), so checking for preemption here would
839 * be complicated.  Fortunately, we there's no good reason
840 * to try to handle preemption here.
841 *
842 * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
843 */
844SYM_CODE_START_LOCAL(paranoid_exit)
845	UNWIND_HINT_REGS
846	testl	%ebx, %ebx			/* swapgs needed? */
847	jnz	.Lparanoid_exit_no_swapgs
848	/* Always restore stashed CR3 value (see paranoid_entry) */
849	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
850	SWAPGS_UNSAFE_STACK
851	jmp	restore_regs_and_return_to_kernel
852.Lparanoid_exit_no_swapgs:
853	/* Always restore stashed CR3 value (see paranoid_entry) */
854	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
855	jmp restore_regs_and_return_to_kernel
856SYM_CODE_END(paranoid_exit)
857
858/*
859 * Save all registers in pt_regs, and switch GS if needed.
860 */
861SYM_CODE_START_LOCAL(error_entry)
862	UNWIND_HINT_FUNC
863	cld
864	PUSH_AND_CLEAR_REGS save_ret=1
865	ENCODE_FRAME_POINTER 8
866	testb	$3, CS+8(%rsp)
867	jz	.Lerror_kernelspace
868
869	/*
870	 * We entered from user mode or we're pretending to have entered
871	 * from user mode due to an IRET fault.
872	 */
873	SWAPGS
874	FENCE_SWAPGS_USER_ENTRY
875	/* We have user CR3.  Change to kernel CR3. */
876	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
877
878.Lerror_entry_from_usermode_after_swapgs:
879	/* Put us onto the real thread stack. */
880	popq	%r12				/* save return addr in %12 */
881	movq	%rsp, %rdi			/* arg0 = pt_regs pointer */
882	call	sync_regs
883	movq	%rax, %rsp			/* switch stack */
884	ENCODE_FRAME_POINTER
885	pushq	%r12
886	ret
887
888.Lerror_entry_done_lfence:
889	FENCE_SWAPGS_KERNEL_ENTRY
890.Lerror_entry_done:
891	ret
892
893	/*
894	 * There are two places in the kernel that can potentially fault with
895	 * usergs. Handle them here.  B stepping K8s sometimes report a
896	 * truncated RIP for IRET exceptions returning to compat mode. Check
897	 * for these here too.
898	 */
899.Lerror_kernelspace:
900	leaq	native_irq_return_iret(%rip), %rcx
901	cmpq	%rcx, RIP+8(%rsp)
902	je	.Lerror_bad_iret
903	movl	%ecx, %eax			/* zero extend */
904	cmpq	%rax, RIP+8(%rsp)
905	je	.Lbstep_iret
906	cmpq	$.Lgs_change, RIP+8(%rsp)
907	jne	.Lerror_entry_done_lfence
908
909	/*
910	 * hack: .Lgs_change can fail with user gsbase.  If this happens, fix up
911	 * gsbase and proceed.  We'll fix up the exception and land in
912	 * .Lgs_change's error handler with kernel gsbase.
913	 */
914	SWAPGS
915	FENCE_SWAPGS_USER_ENTRY
916	jmp .Lerror_entry_done
917
918.Lbstep_iret:
919	/* Fix truncated RIP */
920	movq	%rcx, RIP+8(%rsp)
921	/* fall through */
922
923.Lerror_bad_iret:
924	/*
925	 * We came from an IRET to user mode, so we have user
926	 * gsbase and CR3.  Switch to kernel gsbase and CR3:
927	 */
928	SWAPGS
929	FENCE_SWAPGS_USER_ENTRY
930	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
931
932	/*
933	 * Pretend that the exception came from user mode: set up pt_regs
934	 * as if we faulted immediately after IRET.
935	 */
936	mov	%rsp, %rdi
937	call	fixup_bad_iret
938	mov	%rax, %rsp
939	jmp	.Lerror_entry_from_usermode_after_swapgs
940SYM_CODE_END(error_entry)
941
942SYM_CODE_START_LOCAL(error_return)
943	UNWIND_HINT_REGS
944	DEBUG_ENTRY_ASSERT_IRQS_OFF
945	testb	$3, CS(%rsp)
946	jz	restore_regs_and_return_to_kernel
947	jmp	swapgs_restore_regs_and_return_to_usermode
948SYM_CODE_END(error_return)
949
950/*
951 * Runs on exception stack.  Xen PV does not go through this path at all,
952 * so we can use real assembly here.
953 *
954 * Registers:
955 *	%r14: Used to save/restore the CR3 of the interrupted context
956 *	      when PAGE_TABLE_ISOLATION is in use.  Do not clobber.
957 */
958SYM_CODE_START(asm_exc_nmi)
959	UNWIND_HINT_IRET_REGS
960
961	/*
962	 * We allow breakpoints in NMIs. If a breakpoint occurs, then
963	 * the iretq it performs will take us out of NMI context.
964	 * This means that we can have nested NMIs where the next
965	 * NMI is using the top of the stack of the previous NMI. We
966	 * can't let it execute because the nested NMI will corrupt the
967	 * stack of the previous NMI. NMI handlers are not re-entrant
968	 * anyway.
969	 *
970	 * To handle this case we do the following:
971	 *  Check the a special location on the stack that contains
972	 *  a variable that is set when NMIs are executing.
973	 *  The interrupted task's stack is also checked to see if it
974	 *  is an NMI stack.
975	 *  If the variable is not set and the stack is not the NMI
976	 *  stack then:
977	 *    o Set the special variable on the stack
978	 *    o Copy the interrupt frame into an "outermost" location on the
979	 *      stack
980	 *    o Copy the interrupt frame into an "iret" location on the stack
981	 *    o Continue processing the NMI
982	 *  If the variable is set or the previous stack is the NMI stack:
983	 *    o Modify the "iret" location to jump to the repeat_nmi
984	 *    o return back to the first NMI
985	 *
986	 * Now on exit of the first NMI, we first clear the stack variable
987	 * The NMI stack will tell any nested NMIs at that point that it is
988	 * nested. Then we pop the stack normally with iret, and if there was
989	 * a nested NMI that updated the copy interrupt stack frame, a
990	 * jump will be made to the repeat_nmi code that will handle the second
991	 * NMI.
992	 *
993	 * However, espfix prevents us from directly returning to userspace
994	 * with a single IRET instruction.  Similarly, IRET to user mode
995	 * can fault.  We therefore handle NMIs from user space like
996	 * other IST entries.
997	 */
998
999	ASM_CLAC
1000
1001	/* Use %rdx as our temp variable throughout */
1002	pushq	%rdx
1003
1004	testb	$3, CS-RIP+8(%rsp)
1005	jz	.Lnmi_from_kernel
1006
1007	/*
1008	 * NMI from user mode.  We need to run on the thread stack, but we
1009	 * can't go through the normal entry paths: NMIs are masked, and
1010	 * we don't want to enable interrupts, because then we'll end
1011	 * up in an awkward situation in which IRQs are on but NMIs
1012	 * are off.
1013	 *
1014	 * We also must not push anything to the stack before switching
1015	 * stacks lest we corrupt the "NMI executing" variable.
1016	 */
1017
1018	swapgs
1019	cld
1020	FENCE_SWAPGS_USER_ENTRY
1021	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
1022	movq	%rsp, %rdx
1023	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
1024	UNWIND_HINT_IRET_REGS base=%rdx offset=8
1025	pushq	5*8(%rdx)	/* pt_regs->ss */
1026	pushq	4*8(%rdx)	/* pt_regs->rsp */
1027	pushq	3*8(%rdx)	/* pt_regs->flags */
1028	pushq	2*8(%rdx)	/* pt_regs->cs */
1029	pushq	1*8(%rdx)	/* pt_regs->rip */
1030	UNWIND_HINT_IRET_REGS
1031	pushq   $-1		/* pt_regs->orig_ax */
1032	PUSH_AND_CLEAR_REGS rdx=(%rdx)
1033	ENCODE_FRAME_POINTER
1034
1035	/*
1036	 * At this point we no longer need to worry about stack damage
1037	 * due to nesting -- we're on the normal thread stack and we're
1038	 * done with the NMI stack.
1039	 */
1040
1041	movq	%rsp, %rdi
1042	movq	$-1, %rsi
1043	call	exc_nmi
1044
1045	/*
1046	 * Return back to user mode.  We must *not* do the normal exit
1047	 * work, because we don't want to enable interrupts.
1048	 */
1049	jmp	swapgs_restore_regs_and_return_to_usermode
1050
1051.Lnmi_from_kernel:
1052	/*
1053	 * Here's what our stack frame will look like:
1054	 * +---------------------------------------------------------+
1055	 * | original SS                                             |
1056	 * | original Return RSP                                     |
1057	 * | original RFLAGS                                         |
1058	 * | original CS                                             |
1059	 * | original RIP                                            |
1060	 * +---------------------------------------------------------+
1061	 * | temp storage for rdx                                    |
1062	 * +---------------------------------------------------------+
1063	 * | "NMI executing" variable                                |
1064	 * +---------------------------------------------------------+
1065	 * | iret SS          } Copied from "outermost" frame        |
1066	 * | iret Return RSP  } on each loop iteration; overwritten  |
1067	 * | iret RFLAGS      } by a nested NMI to force another     |
1068	 * | iret CS          } iteration if needed.                 |
1069	 * | iret RIP         }                                      |
1070	 * +---------------------------------------------------------+
1071	 * | outermost SS          } initialized in first_nmi;       |
1072	 * | outermost Return RSP  } will not be changed before      |
1073	 * | outermost RFLAGS      } NMI processing is done.         |
1074	 * | outermost CS          } Copied to "iret" frame on each  |
1075	 * | outermost RIP         } iteration.                      |
1076	 * +---------------------------------------------------------+
1077	 * | pt_regs                                                 |
1078	 * +---------------------------------------------------------+
1079	 *
1080	 * The "original" frame is used by hardware.  Before re-enabling
1081	 * NMIs, we need to be done with it, and we need to leave enough
1082	 * space for the asm code here.
1083	 *
1084	 * We return by executing IRET while RSP points to the "iret" frame.
1085	 * That will either return for real or it will loop back into NMI
1086	 * processing.
1087	 *
1088	 * The "outermost" frame is copied to the "iret" frame on each
1089	 * iteration of the loop, so each iteration starts with the "iret"
1090	 * frame pointing to the final return target.
1091	 */
1092
1093	/*
1094	 * Determine whether we're a nested NMI.
1095	 *
1096	 * If we interrupted kernel code between repeat_nmi and
1097	 * end_repeat_nmi, then we are a nested NMI.  We must not
1098	 * modify the "iret" frame because it's being written by
1099	 * the outer NMI.  That's okay; the outer NMI handler is
1100	 * about to about to call exc_nmi() anyway, so we can just
1101	 * resume the outer NMI.
1102	 */
1103
1104	movq	$repeat_nmi, %rdx
1105	cmpq	8(%rsp), %rdx
1106	ja	1f
1107	movq	$end_repeat_nmi, %rdx
1108	cmpq	8(%rsp), %rdx
1109	ja	nested_nmi_out
11101:
1111
1112	/*
1113	 * Now check "NMI executing".  If it's set, then we're nested.
1114	 * This will not detect if we interrupted an outer NMI just
1115	 * before IRET.
1116	 */
1117	cmpl	$1, -8(%rsp)
1118	je	nested_nmi
1119
1120	/*
1121	 * Now test if the previous stack was an NMI stack.  This covers
1122	 * the case where we interrupt an outer NMI after it clears
1123	 * "NMI executing" but before IRET.  We need to be careful, though:
1124	 * there is one case in which RSP could point to the NMI stack
1125	 * despite there being no NMI active: naughty userspace controls
1126	 * RSP at the very beginning of the SYSCALL targets.  We can
1127	 * pull a fast one on naughty userspace, though: we program
1128	 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1129	 * if it controls the kernel's RSP.  We set DF before we clear
1130	 * "NMI executing".
1131	 */
1132	lea	6*8(%rsp), %rdx
1133	/* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1134	cmpq	%rdx, 4*8(%rsp)
1135	/* If the stack pointer is above the NMI stack, this is a normal NMI */
1136	ja	first_nmi
1137
1138	subq	$EXCEPTION_STKSZ, %rdx
1139	cmpq	%rdx, 4*8(%rsp)
1140	/* If it is below the NMI stack, it is a normal NMI */
1141	jb	first_nmi
1142
1143	/* Ah, it is within the NMI stack. */
1144
1145	testb	$(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1146	jz	first_nmi	/* RSP was user controlled. */
1147
1148	/* This is a nested NMI. */
1149
1150nested_nmi:
1151	/*
1152	 * Modify the "iret" frame to point to repeat_nmi, forcing another
1153	 * iteration of NMI handling.
1154	 */
1155	subq	$8, %rsp
1156	leaq	-10*8(%rsp), %rdx
1157	pushq	$__KERNEL_DS
1158	pushq	%rdx
1159	pushfq
1160	pushq	$__KERNEL_CS
1161	pushq	$repeat_nmi
1162
1163	/* Put stack back */
1164	addq	$(6*8), %rsp
1165
1166nested_nmi_out:
1167	popq	%rdx
1168
1169	/* We are returning to kernel mode, so this cannot result in a fault. */
1170	iretq
1171
1172first_nmi:
1173	/* Restore rdx. */
1174	movq	(%rsp), %rdx
1175
1176	/* Make room for "NMI executing". */
1177	pushq	$0
1178
1179	/* Leave room for the "iret" frame */
1180	subq	$(5*8), %rsp
1181
1182	/* Copy the "original" frame to the "outermost" frame */
1183	.rept 5
1184	pushq	11*8(%rsp)
1185	.endr
1186	UNWIND_HINT_IRET_REGS
1187
1188	/* Everything up to here is safe from nested NMIs */
1189
1190#ifdef CONFIG_DEBUG_ENTRY
1191	/*
1192	 * For ease of testing, unmask NMIs right away.  Disabled by
1193	 * default because IRET is very expensive.
1194	 */
1195	pushq	$0		/* SS */
1196	pushq	%rsp		/* RSP (minus 8 because of the previous push) */
1197	addq	$8, (%rsp)	/* Fix up RSP */
1198	pushfq			/* RFLAGS */
1199	pushq	$__KERNEL_CS	/* CS */
1200	pushq	$1f		/* RIP */
1201	iretq			/* continues at repeat_nmi below */
1202	UNWIND_HINT_IRET_REGS
12031:
1204#endif
1205
1206repeat_nmi:
1207	/*
1208	 * If there was a nested NMI, the first NMI's iret will return
1209	 * here. But NMIs are still enabled and we can take another
1210	 * nested NMI. The nested NMI checks the interrupted RIP to see
1211	 * if it is between repeat_nmi and end_repeat_nmi, and if so
1212	 * it will just return, as we are about to repeat an NMI anyway.
1213	 * This makes it safe to copy to the stack frame that a nested
1214	 * NMI will update.
1215	 *
1216	 * RSP is pointing to "outermost RIP".  gsbase is unknown, but, if
1217	 * we're repeating an NMI, gsbase has the same value that it had on
1218	 * the first iteration.  paranoid_entry will load the kernel
1219	 * gsbase if needed before we call exc_nmi().  "NMI executing"
1220	 * is zero.
1221	 */
1222	movq	$1, 10*8(%rsp)		/* Set "NMI executing". */
1223
1224	/*
1225	 * Copy the "outermost" frame to the "iret" frame.  NMIs that nest
1226	 * here must not modify the "iret" frame while we're writing to
1227	 * it or it will end up containing garbage.
1228	 */
1229	addq	$(10*8), %rsp
1230	.rept 5
1231	pushq	-6*8(%rsp)
1232	.endr
1233	subq	$(5*8), %rsp
1234end_repeat_nmi:
1235
1236	/*
1237	 * Everything below this point can be preempted by a nested NMI.
1238	 * If this happens, then the inner NMI will change the "iret"
1239	 * frame to point back to repeat_nmi.
1240	 */
1241	pushq	$-1				/* ORIG_RAX: no syscall to restart */
1242
1243	/*
1244	 * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
1245	 * as we should not be calling schedule in NMI context.
1246	 * Even with normal interrupts enabled. An NMI should not be
1247	 * setting NEED_RESCHED or anything that normal interrupts and
1248	 * exceptions might do.
1249	 */
1250	call	paranoid_entry
1251	UNWIND_HINT_REGS
1252
1253	movq	%rsp, %rdi
1254	movq	$-1, %rsi
1255	call	exc_nmi
1256
1257	/* Always restore stashed CR3 value (see paranoid_entry) */
1258	RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
1259
1260	testl	%ebx, %ebx			/* swapgs needed? */
1261	jnz	nmi_restore
1262nmi_swapgs:
1263	SWAPGS_UNSAFE_STACK
1264nmi_restore:
1265	POP_REGS
1266
1267	/*
1268	 * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
1269	 * at the "iret" frame.
1270	 */
1271	addq	$6*8, %rsp
1272
1273	/*
1274	 * Clear "NMI executing".  Set DF first so that we can easily
1275	 * distinguish the remaining code between here and IRET from
1276	 * the SYSCALL entry and exit paths.
1277	 *
1278	 * We arguably should just inspect RIP instead, but I (Andy) wrote
1279	 * this code when I had the misapprehension that Xen PV supported
1280	 * NMIs, and Xen PV would break that approach.
1281	 */
1282	std
1283	movq	$0, 5*8(%rsp)		/* clear "NMI executing" */
1284
1285	/*
1286	 * iretq reads the "iret" frame and exits the NMI stack in a
1287	 * single instruction.  We are returning to kernel mode, so this
1288	 * cannot result in a fault.  Similarly, we don't need to worry
1289	 * about espfix64 on the way back to kernel mode.
1290	 */
1291	iretq
1292SYM_CODE_END(asm_exc_nmi)
1293
1294#ifndef CONFIG_IA32_EMULATION
1295/*
1296 * This handles SYSCALL from 32-bit code.  There is no way to program
1297 * MSRs to fully disable 32-bit SYSCALL.
1298 */
1299SYM_CODE_START(ignore_sysret)
1300	UNWIND_HINT_EMPTY
1301	mov	$-ENOSYS, %eax
1302	sysretl
1303SYM_CODE_END(ignore_sysret)
1304#endif
1305
1306.pushsection .text, "ax"
1307SYM_CODE_START(rewind_stack_do_exit)
1308	UNWIND_HINT_FUNC
1309	/* Prevent any naive code from trying to unwind to our caller. */
1310	xorl	%ebp, %ebp
1311
1312	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rax
1313	leaq	-PTREGS_SIZE(%rax), %rsp
1314	UNWIND_HINT_REGS
1315
1316	call	do_exit
1317SYM_CODE_END(rewind_stack_do_exit)
1318.popsection
1319