blob: 4e5c41005dd0e0d6be7eb38f5961a03e73c73b35 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86_64/entry.S
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
6 * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
Ingo Molnar4d732132015-06-08 20:43:07 +02007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * entry.S contains the system-call and fault low-level handling routines.
9 *
Andy Lutomirski8b4777a2011-06-05 13:50:18 -040010 * Some of this is documented in Documentation/x86/entry_64.txt
11 *
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +010012 * A note on terminology:
Ingo Molnar4d732132015-06-08 20:43:07 +020013 * - iret frame: Architecture defined interrupt frame from SS to RIP
14 * at the top of the kernel process stack.
Andi Kleen2e91a172006-09-26 10:52:29 +020015 *
16 * Some macro usage:
Ingo Molnar4d732132015-06-08 20:43:07 +020017 * - ENTRY/END: Define functions in the symbol table.
18 * - TRACE_IRQ_*: Trace hardirq state for lock debugging.
19 * - idtentry: Define exception entry points.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/linkage.h>
22#include <asm/segment.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/cache.h>
24#include <asm/errno.h>
Ingo Molnard36f9472015-06-03 18:29:26 +020025#include "calling.h"
Sam Ravnborge2d5df92005-09-09 21:28:48 +020026#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/msr.h>
28#include <asm/unistd.h>
29#include <asm/thread_info.h>
30#include <asm/hw_irq.h>
Jeremy Fitzhardinge0341c142009-02-13 11:14:01 -080031#include <asm/page_types.h>
Ingo Molnar2601e642006-07-03 00:24:45 -070032#include <asm/irqflags.h>
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010033#include <asm/paravirt.h>
Tejun Heo9939dda2009-01-13 20:41:35 +090034#include <asm/percpu.h>
H. Peter Anvind7abc0f2012-04-20 12:19:50 -070035#include <asm/asm.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070036#include <asm/smap.h>
H. Peter Anvin3891a042014-04-29 16:46:09 -070037#include <asm/pgtable_types.h>
Al Viro784d5692016-01-11 11:04:34 -050038#include <asm/export.h>
Richard Fellner13be4482017-05-04 14:26:50 +020039#include <asm/kaiser.h>
Eric Parisd7e75282012-01-03 14:23:06 -050040#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Roland McGrath86a1c342008-06-23 15:37:04 -070042/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
43#include <linux/elf-em.h>
Ingo Molnar4d732132015-06-08 20:43:07 +020044#define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
45#define __AUDIT_ARCH_64BIT 0x80000000
46#define __AUDIT_ARCH_LE 0x40000000
Roland McGrath86a1c342008-06-23 15:37:04 -070047
Ingo Molnar4d732132015-06-08 20:43:07 +020048.code64
49.section .entry.text, "ax"
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020050
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010051#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040052ENTRY(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010053 swapgs
54 sysretq
Cyrill Gorcunovb3baaa12009-02-23 22:57:00 +030055ENDPROC(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010056#endif /* CONFIG_PARAVIRT */
57
Greg Kroah-Hartmana11ca512017-12-04 12:59:57 +010058.macro TRACE_IRQS_IRETQ
Ingo Molnar2601e642006-07-03 00:24:45 -070059#ifdef CONFIG_TRACE_IRQFLAGS
Greg Kroah-Hartmana11ca512017-12-04 12:59:57 +010060 bt $9, EFLAGS(%rsp) /* interrupts off? */
Ingo Molnar4d732132015-06-08 20:43:07 +020061 jnc 1f
Ingo Molnar2601e642006-07-03 00:24:45 -070062 TRACE_IRQS_ON
631:
64#endif
65.endm
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Steven Rostedt5963e312012-05-30 11:54:53 -040068 * When dynamic function tracer is enabled it will add a breakpoint
69 * to all locations that it is about to modify, sync CPUs, update
70 * all the code, sync CPUs, then remove the breakpoints. In this time
71 * if lockdep is enabled, it might jump back into the debug handler
72 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
73 *
74 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
75 * make sure the stack pointer does not get reset back to the top
76 * of the debug stack, and instead just reuses the current stack.
77 */
78#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
79
80.macro TRACE_IRQS_OFF_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +020081 call debug_stack_set_zero
Steven Rostedt5963e312012-05-30 11:54:53 -040082 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +020083 call debug_stack_reset
Steven Rostedt5963e312012-05-30 11:54:53 -040084.endm
85
86.macro TRACE_IRQS_ON_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +020087 call debug_stack_set_zero
Steven Rostedt5963e312012-05-30 11:54:53 -040088 TRACE_IRQS_ON
Ingo Molnar4d732132015-06-08 20:43:07 +020089 call debug_stack_reset
Steven Rostedt5963e312012-05-30 11:54:53 -040090.endm
91
Denys Vlasenkof2db9382015-02-26 14:40:30 -080092.macro TRACE_IRQS_IRETQ_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +020093 bt $9, EFLAGS(%rsp) /* interrupts off? */
94 jnc 1f
Steven Rostedt5963e312012-05-30 11:54:53 -040095 TRACE_IRQS_ON_DEBUG
961:
97.endm
98
99#else
Ingo Molnar4d732132015-06-08 20:43:07 +0200100# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
101# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
102# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
Steven Rostedt5963e312012-05-30 11:54:53 -0400103#endif
104
105/*
Ingo Molnar4d732132015-06-08 20:43:07 +0200106 * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Andy Lutomirskifda57b22016-03-09 19:00:35 -0800108 * This is the only entry point used for 64-bit system calls. The
109 * hardware interface is reasonably well designed and the register to
110 * argument mapping Linux uses fits well with the registers that are
111 * available when SYSCALL is used.
112 *
113 * SYSCALL instructions can be found inlined in libc implementations as
114 * well as some other programs and libraries. There are also a handful
115 * of SYSCALL instructions in the vDSO used, for example, as a
116 * clock_gettimeofday fallback.
117 *
Ingo Molnar4d732132015-06-08 20:43:07 +0200118 * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800119 * then loads new ss, cs, and rip from previously programmed MSRs.
120 * rflags gets masked by a value from another MSR (so CLD and CLAC
121 * are not needed). SYSCALL does not save anything on the stack
122 * and does not change rsp.
123 *
124 * Registers on entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * rax system call number
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800126 * rcx return address
127 * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * rdi arg0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * rsi arg1
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100130 * rdx arg2
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800131 * r10 arg3 (needs to be moved to rcx to conform to C ABI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * r8 arg4
133 * r9 arg5
Ingo Molnar4d732132015-06-08 20:43:07 +0200134 * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100135 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * Only called from user space.
137 *
Ingo Molnar7fcb3bc2015-03-17 14:42:59 +0100138 * When user can change pt_regs->foo always force IRET. That is because
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200139 * it deals with uncanonical addresses better. SYSRET has trouble
140 * with them due to bugs in both AMD and Intel CPUs.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Ingo Molnarb2502b42015-06-08 08:42:03 +0200143ENTRY(entry_SYSCALL_64)
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100144 /*
145 * Interrupts are off on entry.
146 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
147 * it is too small to ever cause noticeable irq latency.
148 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100149 SWAPGS_UNSAFE_STACK
Richard Fellner13be4482017-05-04 14:26:50 +0200150 SWITCH_KERNEL_CR3_NO_STACK
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100151 /*
152 * A hypervisor implementation might want to use a label
153 * after the swapgs, so that it can do the swapgs
154 * for the guest and jump here on syscall.
155 */
Ingo Molnarb2502b42015-06-08 08:42:03 +0200156GLOBAL(entry_SYSCALL_64_after_swapgs)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100157
Ingo Molnar4d732132015-06-08 20:43:07 +0200158 movq %rsp, PER_CPU_VAR(rsp_scratch)
159 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100160
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800161 TRACE_IRQS_OFF
162
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100163 /* Construct struct pt_regs on stack */
Ingo Molnar4d732132015-06-08 20:43:07 +0200164 pushq $__USER_DS /* pt_regs->ss */
165 pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */
Ingo Molnar4d732132015-06-08 20:43:07 +0200166 pushq %r11 /* pt_regs->flags */
167 pushq $__USER_CS /* pt_regs->cs */
168 pushq %rcx /* pt_regs->ip */
169 pushq %rax /* pt_regs->orig_ax */
170 pushq %rdi /* pt_regs->di */
171 pushq %rsi /* pt_regs->si */
172 pushq %rdx /* pt_regs->dx */
173 pushq %rcx /* pt_regs->cx */
174 pushq $-ENOSYS /* pt_regs->ax */
175 pushq %r8 /* pt_regs->r8 */
176 pushq %r9 /* pt_regs->r9 */
177 pushq %r10 /* pt_regs->r10 */
178 pushq %r11 /* pt_regs->r11 */
179 sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100180
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800181 /*
182 * If we need to do entry work or if we guess we'll need to do
183 * exit work, go straight to the slow path.
184 */
Andy Lutomirski15f4eae2016-09-13 14:29:25 -0700185 movq PER_CPU_VAR(current_task), %r11
186 testl $_TIF_WORK_SYSCALL_ENTRY|_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800187 jnz entry_SYSCALL64_slow_path
188
Ingo Molnarb2502b42015-06-08 08:42:03 +0200189entry_SYSCALL_64_fastpath:
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800190 /*
191 * Easy case: enable interrupts and issue the syscall. If the syscall
192 * needs pt_regs, we'll call a stub that disables interrupts again
193 * and jumps to the slow path.
194 */
195 TRACE_IRQS_ON
196 ENABLE_INTERRUPTS(CLBR_NONE)
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800197#if __SYSCALL_MASK == ~0
Ingo Molnar4d732132015-06-08 20:43:07 +0200198 cmpq $__NR_syscall_max, %rax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800199#else
Ingo Molnar4d732132015-06-08 20:43:07 +0200200 andl $__SYSCALL_MASK, %eax
201 cmpl $__NR_syscall_max, %eax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800202#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200203 ja 1f /* return -ENOSYS (already in pt_regs->ax) */
204 movq %r10, %rcx
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800205
206 /*
207 * This call instruction is handled specially in stub_ptregs_64.
Andy Lutomirskib7765082016-01-31 09:33:26 -0800208 * It might end up jumping to the slow path. If it jumps, RAX
209 * and all argument registers are clobbered.
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800210 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200211 call *sys_call_table(, %rax, 8)
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800212.Lentry_SYSCALL_64_after_fastpath_call:
213
Ingo Molnar4d732132015-06-08 20:43:07 +0200214 movq %rax, RAX(%rsp)
Denys Vlasenko146b2b02015-03-25 18:18:13 +01002151:
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800216
Denys Vlasenko4416c5a2015-03-31 19:00:03 +0200217 /*
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800218 * If we get here, then we know that pt_regs is clean for SYSRET64.
219 * If we see that no exit work is required (which we are required
220 * to check with IRQs off), then we can go straight to SYSRET64.
Denys Vlasenko4416c5a2015-03-31 19:00:03 +0200221 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100222 DISABLE_INTERRUPTS(CLBR_NONE)
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800223 TRACE_IRQS_OFF
Andy Lutomirski15f4eae2016-09-13 14:29:25 -0700224 movq PER_CPU_VAR(current_task), %r11
225 testl $_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800226 jnz 1f
Andy Lutomirskib3494a42015-03-23 12:32:54 -0700227
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800228 LOCKDEP_SYS_EXIT
229 TRACE_IRQS_ON /* user mode is traced as IRQs on */
Andy Lutomirskieb2a54c2016-01-31 09:33:27 -0800230 movq RIP(%rsp), %rcx
231 movq EFLAGS(%rsp), %r11
232 RESTORE_C_REGS_EXCEPT_RCX_R11
Dave Hansen8f0baad2017-08-30 16:23:00 -0700233 /*
234 * This opens a window where we have a user CR3, but are
235 * running in the kernel. This makes using the CS
236 * register useless for telling whether or not we need to
237 * switch CR3 in NMIs. Normal interrupts are OK because
238 * they are off here.
239 */
Richard Fellner13be4482017-05-04 14:26:50 +0200240 SWITCH_USER_CR3
Ingo Molnar4d732132015-06-08 20:43:07 +0200241 movq RSP(%rsp), %rsp
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400242 USERGS_SYSRET64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Andy Lutomirski1e423bf2016-01-28 15:11:28 -08002441:
245 /*
246 * The fast path looked good when we started, but something changed
247 * along the way and we need to switch to the slow path. Calling
248 * raise(3) will trigger this, for example. IRQs are off.
249 */
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700250 TRACE_IRQS_ON
251 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800252 SAVE_EXTRA_REGS
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700253 movq %rsp, %rdi
254 call syscall_return_slowpath /* returns with IRQs disabled */
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800255 jmp return_from_SYSCALL_64
256
257entry_SYSCALL64_slow_path:
258 /* IRQs are off. */
259 SAVE_EXTRA_REGS
260 movq %rsp, %rdi
261 call do_syscall_64 /* returns with IRQs disabled */
262
263return_from_SYSCALL_64:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800264 RESTORE_EXTRA_REGS
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700265 TRACE_IRQS_IRETQ /* we're about to change IF */
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200266
267 /*
268 * Try to use SYSRET instead of IRET if we're returning to
269 * a completely clean 64-bit userspace context.
270 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200271 movq RCX(%rsp), %rcx
272 movq RIP(%rsp), %r11
273 cmpq %rcx, %r11 /* RCX == RIP */
274 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200275
276 /*
277 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
278 * in kernel space. This essentially lets the user take over
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200279 * the kernel, since userspace controls RSP.
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200280 *
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200281 * If width of "canonical tail" ever becomes variable, this will need
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200282 * to be updated to remain correct on both old and new CPUs.
283 */
284 .ifne __VIRTUAL_MASK_SHIFT - 47
285 .error "virtual address width changed -- SYSRET checks need update"
286 .endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200287
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200288 /* Change top 16 bits to be the sign-extension of 47th bit */
289 shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
290 sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
Ingo Molnar4d732132015-06-08 20:43:07 +0200291
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200292 /* If this changed %rcx, it was not canonical */
293 cmpq %rcx, %r11
294 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200295
Ingo Molnar4d732132015-06-08 20:43:07 +0200296 cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
297 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200298
Ingo Molnar4d732132015-06-08 20:43:07 +0200299 movq R11(%rsp), %r11
300 cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
301 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200302
303 /*
Borislav Petkov3e035302016-08-03 19:14:29 +0200304 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
305 * restore RF properly. If the slowpath sets it for whatever reason, we
306 * need to restore it correctly.
307 *
308 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
309 * trap from userspace immediately after SYSRET. This would cause an
310 * infinite loop whenever #DB happens with register state that satisfies
311 * the opportunistic SYSRET conditions. For example, single-stepping
312 * this user code:
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200313 *
Ingo Molnar4d732132015-06-08 20:43:07 +0200314 * movq $stuck_here, %rcx
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200315 * pushfq
316 * popq %r11
317 * stuck_here:
318 *
319 * would never get past 'stuck_here'.
320 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200321 testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
322 jnz opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200323
324 /* nothing to check for RSP */
325
Ingo Molnar4d732132015-06-08 20:43:07 +0200326 cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
327 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200328
329 /*
Ingo Molnar4d732132015-06-08 20:43:07 +0200330 * We win! This label is here just for ease of understanding
331 * perf profiles. Nothing jumps here.
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200332 */
333syscall_return_via_sysret:
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200334 /* rcx and r11 are already restored (see code above) */
335 RESTORE_C_REGS_EXCEPT_RCX_R11
Dave Hansen8f0baad2017-08-30 16:23:00 -0700336 /*
337 * This opens a window where we have a user CR3, but are
338 * running in the kernel. This makes using the CS
339 * register useless for telling whether or not we need to
340 * switch CR3 in NMIs. Normal interrupts are OK because
341 * they are off here.
342 */
Richard Fellner13be4482017-05-04 14:26:50 +0200343 SWITCH_USER_CR3
Ingo Molnar4d732132015-06-08 20:43:07 +0200344 movq RSP(%rsp), %rsp
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200345 USERGS_SYSRET64
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200346
347opportunistic_sysret_failed:
Dave Hansen8f0baad2017-08-30 16:23:00 -0700348 /*
349 * This opens a window where we have a user CR3, but are
350 * running in the kernel. This makes using the CS
351 * register useless for telling whether or not we need to
352 * switch CR3 in NMIs. Normal interrupts are OK because
353 * they are off here.
354 */
Richard Fellner13be4482017-05-04 14:26:50 +0200355 SWITCH_USER_CR3
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200356 SWAPGS
357 jmp restore_c_regs_and_iret
Ingo Molnarb2502b42015-06-08 08:42:03 +0200358END(entry_SYSCALL_64)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100359
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800360ENTRY(stub_ptregs_64)
361 /*
362 * Syscalls marked as needing ptregs land here.
Andy Lutomirskib7765082016-01-31 09:33:26 -0800363 * If we are on the fast path, we need to save the extra regs,
364 * which we achieve by trying again on the slow path. If we are on
365 * the slow path, the extra regs are already saved.
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800366 *
367 * RAX stores a pointer to the C function implementing the syscall.
Andy Lutomirskib7765082016-01-31 09:33:26 -0800368 * IRQs are on.
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800369 */
370 cmpq $.Lentry_SYSCALL_64_after_fastpath_call, (%rsp)
371 jne 1f
372
Andy Lutomirskib7765082016-01-31 09:33:26 -0800373 /*
374 * Called from fast path -- disable IRQs again, pop return address
375 * and jump to slow path
376 */
377 DISABLE_INTERRUPTS(CLBR_NONE)
378 TRACE_IRQS_OFF
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800379 popq %rax
Andy Lutomirskib7765082016-01-31 09:33:26 -0800380 jmp entry_SYSCALL64_slow_path
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800381
3821:
Borislav Petkovb3830e82016-08-01 12:05:02 +0200383 jmp *%rax /* Called from C */
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800384END(stub_ptregs_64)
385
386.macro ptregs_stub func
387ENTRY(ptregs_\func)
388 leaq \func(%rip), %rax
389 jmp stub_ptregs_64
390END(ptregs_\func)
391.endm
392
393/* Instantiate ptregs_stub for each ptregs-using syscall */
394#define __SYSCALL_64_QUAL_(sym)
395#define __SYSCALL_64_QUAL_ptregs(sym) ptregs_stub sym
396#define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym)
397#include <asm/syscalls_64.h>
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200398
Jan Beulich7effaa82005-09-12 18:49:24 +0200399/*
Brian Gerst01003012016-08-13 12:38:19 -0400400 * %rdi: prev task
401 * %rsi: next task
402 */
403ENTRY(__switch_to_asm)
404 /*
405 * Save callee-saved registers
406 * This must match the order in inactive_task_frame
407 */
408 pushq %rbp
409 pushq %rbx
410 pushq %r12
411 pushq %r13
412 pushq %r14
413 pushq %r15
414
415 /* switch stack */
416 movq %rsp, TASK_threadsp(%rdi)
417 movq TASK_threadsp(%rsi), %rsp
418
419#ifdef CONFIG_CC_STACKPROTECTOR
420 movq TASK_stack_canary(%rsi), %rbx
421 movq %rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset
422#endif
423
424 /* restore callee-saved registers */
425 popq %r15
426 popq %r14
427 popq %r13
428 popq %r12
429 popq %rbx
430 popq %rbp
431
432 jmp __switch_to
433END(__switch_to_asm)
434
435/*
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800436 * A newly forked process directly context switches into this address.
437 *
Brian Gerst01003012016-08-13 12:38:19 -0400438 * rax: prev task we switched from
Brian Gerst616d2482016-08-13 12:38:20 -0400439 * rbx: kernel thread func (NULL for user thread)
440 * r12: kernel thread arg
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800441 */
442ENTRY(ret_from_fork)
Brian Gerst01003012016-08-13 12:38:19 -0400443 movq %rax, %rdi
Ingo Molnar4d732132015-06-08 20:43:07 +0200444 call schedule_tail /* rdi: 'prev' task parameter */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800445
Brian Gerst616d2482016-08-13 12:38:20 -0400446 testq %rbx, %rbx /* from kernel_thread? */
447 jnz 1f /* kernel threads are uncommon */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800448
Brian Gerst616d2482016-08-13 12:38:20 -04004492:
Andy Lutomirski24d978b2016-01-28 15:11:27 -0800450 movq %rsp, %rdi
451 call syscall_return_slowpath /* returns with IRQs disabled */
452 TRACE_IRQS_ON /* user mode is traced as IRQS on */
Richard Fellner13be4482017-05-04 14:26:50 +0200453 SWITCH_USER_CR3
Andy Lutomirski24d978b2016-01-28 15:11:27 -0800454 SWAPGS
455 jmp restore_regs_and_iret
Brian Gerst616d2482016-08-13 12:38:20 -0400456
4571:
458 /* kernel thread */
459 movq %r12, %rdi
460 call *%rbx
461 /*
462 * A kernel thread is allowed to return here after successfully
463 * calling do_execve(). Exit to userspace to complete the execve()
464 * syscall.
465 */
466 movq $0, RAX(%rsp)
467 jmp 2b
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800468END(ret_from_fork)
469
470/*
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200471 * Build the entry stubs with some assembler magic.
472 * We pack 1 stub into every 8-byte block.
H. Peter Anvin939b7872008-11-11 13:51:52 -0800473 */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200474 .align 8
H. Peter Anvin939b7872008-11-11 13:51:52 -0800475ENTRY(irq_entries_start)
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200476 vector=FIRST_EXTERNAL_VECTOR
477 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
Ingo Molnar4d732132015-06-08 20:43:07 +0200478 pushq $(~vector+0x80) /* Note: always in signed byte range */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200479 vector=vector+1
480 jmp common_interrupt
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200481 .align 8
482 .endr
H. Peter Anvin939b7872008-11-11 13:51:52 -0800483END(irq_entries_start)
484
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100485/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * Interrupt entry/exit.
487 *
488 * Interrupt entry points save only callee clobbered registers in fast path.
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100489 *
490 * Entry runs with interrupts off.
491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100493/* 0(%rsp): ~(interrupt number) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 .macro interrupt func
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100495 cld
Andy Lutomirskiff467592015-07-03 12:44:29 -0700496 ALLOC_PT_GPREGS_ON_STACK
497 SAVE_C_REGS
498 SAVE_EXTRA_REGS
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100499
Andy Lutomirskiff467592015-07-03 12:44:29 -0700500 testb $3, CS(%rsp)
Denys Vlasenkodde74f22015-04-27 15:21:51 +0200501 jz 1f
Andy Lutomirski02bc7762015-07-03 12:44:31 -0700502
503 /*
504 * IRQ from user mode. Switch to kernel gsbase and inform context
505 * tracking that we're in kernel mode.
506 */
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100507 SWAPGS
Richard Fellner13be4482017-05-04 14:26:50 +0200508 SWITCH_KERNEL_CR3
Andy Lutomirskif1075052015-11-12 12:59:00 -0800509
510 /*
511 * We need to tell lockdep that IRQs are off. We can't do this until
512 * we fix gsbase, and we should do it before enter_from_user_mode
513 * (which can take locks). Since TRACE_IRQS_OFF idempotent,
514 * the simplest way to handle it is to just call it twice if
515 * we enter from user mode. There's no reason to optimize this since
516 * TRACE_IRQS_OFF is a no-op if lockdep is off.
517 */
518 TRACE_IRQS_OFF
519
Andy Lutomirski478dc892015-11-12 12:59:04 -0800520 CALL_enter_from_user_mode
Andy Lutomirski02bc7762015-07-03 12:44:31 -0700521
Denys Vlasenko76f5df42015-02-26 14:40:27 -08005221:
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100523 /*
Denys Vlasenkoe90e1472015-02-26 14:40:28 -0800524 * Save previous stack pointer, optionally switch to interrupt stack.
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100525 * irq_count is used to check if a CPU is already on an interrupt stack
526 * or not. While this is essentially redundant with preempt_count it is
527 * a little cheaper to use a separate counter in the PDA (short of
528 * moving irq_enter into assembly, which would be too much work)
529 */
Andy Lutomirskia586f982015-07-03 12:44:30 -0700530 movq %rsp, %rdi
Ingo Molnar4d732132015-06-08 20:43:07 +0200531 incl PER_CPU_VAR(irq_count)
532 cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
Andy Lutomirskia586f982015-07-03 12:44:30 -0700533 pushq %rdi
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100534 /* We entered an interrupt context - irqs are off: */
535 TRACE_IRQS_OFF
536
Andy Lutomirskia586f982015-07-03 12:44:30 -0700537 call \func /* rdi points to pt_regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 .endm
539
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100540 /*
541 * The interrupt stubs push (~vector+0x80) onto the stack and
542 * then jump to common_interrupt.
543 */
H. Peter Anvin939b7872008-11-11 13:51:52 -0800544 .p2align CONFIG_X86_L1_CACHE_SHIFT
545common_interrupt:
Jan Beulichee4eb872012-11-02 11:18:39 +0000546 ASM_CLAC
Ingo Molnar4d732132015-06-08 20:43:07 +0200547 addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 interrupt do_IRQ
Denys Vlasenko34061f12015-03-23 14:03:59 +0100549 /* 0(%rsp): old RSP */
Jan Beulich7effaa82005-09-12 18:49:24 +0200550ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100551 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700552 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +0200553 decl PER_CPU_VAR(irq_count)
Frederic Weisbecker625dbc32011-01-06 15:22:47 +0100554
Frederic Weisbeckera2bbe752011-07-02 16:52:45 +0200555 /* Restore saved previous stack */
Andy Lutomirskiff467592015-07-03 12:44:29 -0700556 popq %rsp
Frederic Weisbecker625dbc32011-01-06 15:22:47 +0100557
Denys Vlasenko03335e92015-04-27 15:21:52 +0200558 testb $3, CS(%rsp)
Denys Vlasenkodde74f22015-04-27 15:21:51 +0200559 jz retint_kernel
Andy Lutomirski02bc7762015-07-03 12:44:31 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Interrupt came from user space */
Andy Lutomirski02bc7762015-07-03 12:44:31 -0700562GLOBAL(retint_user)
563 mov %rsp,%rdi
564 call prepare_exit_to_usermode
Ingo Molnar2601e642006-07-03 00:24:45 -0700565 TRACE_IRQS_IRETQ
Richard Fellner13be4482017-05-04 14:26:50 +0200566 SWITCH_USER_CR3
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100567 SWAPGS
Andy Lutomirskiff467592015-07-03 12:44:29 -0700568 jmp restore_regs_and_iret
Ingo Molnar2601e642006-07-03 00:24:45 -0700569
Denys Vlasenko627276c2015-03-30 20:09:31 +0200570/* Returning to kernel space */
Denys Vlasenko6ba71b72015-03-31 19:00:05 +0200571retint_kernel:
Denys Vlasenko627276c2015-03-30 20:09:31 +0200572#ifdef CONFIG_PREEMPT
573 /* Interrupts are off */
574 /* Check if we need preemption */
Ingo Molnar4d732132015-06-08 20:43:07 +0200575 bt $9, EFLAGS(%rsp) /* were interrupts off? */
Denys Vlasenko6ba71b72015-03-31 19:00:05 +0200576 jnc 1f
Ingo Molnar4d732132015-06-08 20:43:07 +02005770: cmpl $0, PER_CPU_VAR(__preempt_count)
Denys Vlasenko36acef22015-03-31 19:00:07 +0200578 jnz 1f
Denys Vlasenko627276c2015-03-30 20:09:31 +0200579 call preempt_schedule_irq
Denys Vlasenko36acef22015-03-31 19:00:07 +0200580 jmp 0b
Denys Vlasenko6ba71b72015-03-31 19:00:05 +02005811:
Denys Vlasenko627276c2015-03-30 20:09:31 +0200582#endif
Ingo Molnar2601e642006-07-03 00:24:45 -0700583 /*
584 * The iretq could re-enable interrupts:
585 */
586 TRACE_IRQS_IRETQ
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200587
588/*
589 * At this label, code paths which return to kernel and to user,
590 * which come from interrupts/exception and from syscalls, merge.
591 */
Andy Lutomirskiee08c6b2015-10-05 17:48:09 -0700592GLOBAL(restore_regs_and_iret)
Andy Lutomirskiff467592015-07-03 12:44:29 -0700593 RESTORE_EXTRA_REGS
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200594restore_c_regs_and_iret:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800595 RESTORE_C_REGS
596 REMOVE_PT_GPREGS_FROM_STACK 8
Andy Lutomirski7209a752014-07-23 08:34:11 -0700597 INTERRUPT_RETURN
598
599ENTRY(native_iret)
H. Peter Anvin3891a042014-04-29 16:46:09 -0700600 /*
601 * Are we returning to a stack segment from the LDT? Note: in
602 * 64-bit mode SS:RSP on the exception stack is always valid.
603 */
H. Peter Anvin34273f42014-05-04 10:36:22 -0700604#ifdef CONFIG_X86_ESPFIX64
Ingo Molnar4d732132015-06-08 20:43:07 +0200605 testb $4, (SS-RIP)(%rsp)
606 jnz native_irq_return_ldt
H. Peter Anvin34273f42014-05-04 10:36:22 -0700607#endif
H. Peter Anvin3891a042014-04-29 16:46:09 -0700608
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800609.global native_irq_return_iret
Andy Lutomirski7209a752014-07-23 08:34:11 -0700610native_irq_return_iret:
Andy Lutomirskib645af22014-11-22 18:00:33 -0800611 /*
612 * This may fault. Non-paranoid faults on return to userspace are
613 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
614 * Double-faults due to espfix64 are handled in do_double_fault.
615 * Other faults here are fatal.
616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 iretq
Ingo Molnar3701d8632008-02-09 23:24:08 +0100618
H. Peter Anvin34273f42014-05-04 10:36:22 -0700619#ifdef CONFIG_X86_ESPFIX64
Andy Lutomirski7209a752014-07-23 08:34:11 -0700620native_irq_return_ldt:
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700621 /*
622 * We are running with user GSBASE. All GPRs contain their user
623 * values. We have a percpu ESPFIX stack that is eight slots
624 * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom
625 * of the ESPFIX stack.
626 *
627 * We clobber RAX and RDI in this code. We stash RDI on the
628 * normal stack and RAX on the ESPFIX stack.
629 *
630 * The ESPFIX stack layout we set up looks like this:
631 *
632 * --- top of ESPFIX stack ---
633 * SS
634 * RSP
635 * RFLAGS
636 * CS
637 * RIP <-- RSP points here when we're done
638 * RAX <-- espfix_waddr points here
639 * --- bottom of ESPFIX stack ---
640 */
641
642 pushq %rdi /* Stash user RDI */
H. Peter Anvin3891a042014-04-29 16:46:09 -0700643 SWAPGS
Richard Fellner13be4482017-05-04 14:26:50 +0200644 SWITCH_KERNEL_CR3
Ingo Molnar4d732132015-06-08 20:43:07 +0200645 movq PER_CPU_VAR(espfix_waddr), %rdi
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700646 movq %rax, (0*8)(%rdi) /* user RAX */
647 movq (1*8)(%rsp), %rax /* user RIP */
Ingo Molnar4d732132015-06-08 20:43:07 +0200648 movq %rax, (1*8)(%rdi)
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700649 movq (2*8)(%rsp), %rax /* user CS */
Ingo Molnar4d732132015-06-08 20:43:07 +0200650 movq %rax, (2*8)(%rdi)
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700651 movq (3*8)(%rsp), %rax /* user RFLAGS */
Ingo Molnar4d732132015-06-08 20:43:07 +0200652 movq %rax, (3*8)(%rdi)
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700653 movq (5*8)(%rsp), %rax /* user SS */
Ingo Molnar4d732132015-06-08 20:43:07 +0200654 movq %rax, (5*8)(%rdi)
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700655 movq (4*8)(%rsp), %rax /* user RSP */
Ingo Molnar4d732132015-06-08 20:43:07 +0200656 movq %rax, (4*8)(%rdi)
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700657 /* Now RAX == RSP. */
658
659 andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */
660 popq %rdi /* Restore user RDI */
661
662 /*
663 * espfix_stack[31:16] == 0. The page tables are set up such that
664 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
665 * espfix_waddr for any X. That is, there are 65536 RO aliases of
666 * the same page. Set up RSP so that RSP[31:16] contains the
667 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
668 * still points to an RO alias of the ESPFIX stack.
669 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200670 orq PER_CPU_VAR(espfix_stack), %rax
Richard Fellner13be4482017-05-04 14:26:50 +0200671 SWITCH_USER_CR3
H. Peter Anvin3891a042014-04-29 16:46:09 -0700672 SWAPGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200673 movq %rax, %rsp
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700674
675 /*
676 * At this point, we cannot write to the stack any more, but we can
677 * still read.
678 */
679 popq %rax /* Restore user RAX */
680
681 /*
682 * RSP now points to an ordinary IRET frame, except that the page
683 * is read-only and RSP[31:16] are preloaded with the userspace
684 * values. We can now IRET back to userspace.
685 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200686 jmp native_irq_return_iret
H. Peter Anvin34273f42014-05-04 10:36:22 -0700687#endif
Jan Beulich4b787e02006-06-26 13:56:55 +0200688END(common_interrupt)
H. Peter Anvin3891a042014-04-29 16:46:09 -0700689
Masami Hiramatsu8222d712009-08-27 13:23:25 -0400690/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * APIC interrupts.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100692 */
Seiji Aguchicf910e82013-06-20 11:46:53 -0400693.macro apicinterrupt3 num sym do_sym
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100694ENTRY(\sym)
Jan Beulichee4eb872012-11-02 11:18:39 +0000695 ASM_CLAC
Ingo Molnar4d732132015-06-08 20:43:07 +0200696 pushq $~(\num)
Jan Beulich39e95432011-11-29 11:03:46 +0000697.Lcommon_\sym:
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100698 interrupt \do_sym
Ingo Molnar4d732132015-06-08 20:43:07 +0200699 jmp ret_from_intr
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100700END(\sym)
701.endm
Jacob Shin89b831e2005-11-05 17:25:53 +0100702
Seiji Aguchicf910e82013-06-20 11:46:53 -0400703#ifdef CONFIG_TRACING
704#define trace(sym) trace_##sym
705#define smp_trace(sym) smp_trace_##sym
706
707.macro trace_apicinterrupt num sym
708apicinterrupt3 \num trace(\sym) smp_trace(\sym)
709.endm
710#else
711.macro trace_apicinterrupt num sym do_sym
712.endm
713#endif
714
Alexander Potapenko469f0022016-07-15 11:42:43 +0200715/* Make sure APIC interrupt handlers end up in the irqentry section: */
716#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
717# define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
718# define POP_SECTION_IRQENTRY .popsection
719#else
720# define PUSH_SECTION_IRQENTRY
721# define POP_SECTION_IRQENTRY
722#endif
723
Seiji Aguchicf910e82013-06-20 11:46:53 -0400724.macro apicinterrupt num sym do_sym
Alexander Potapenko469f0022016-07-15 11:42:43 +0200725PUSH_SECTION_IRQENTRY
Seiji Aguchicf910e82013-06-20 11:46:53 -0400726apicinterrupt3 \num \sym \do_sym
727trace_apicinterrupt \num \sym
Alexander Potapenko469f0022016-07-15 11:42:43 +0200728POP_SECTION_IRQENTRY
Seiji Aguchicf910e82013-06-20 11:46:53 -0400729.endm
730
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100731#ifdef CONFIG_SMP
Ingo Molnar4d732132015-06-08 20:43:07 +0200732apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
733apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734#endif
735
Nick Piggin03b48632009-01-20 04:36:04 +0100736#ifdef CONFIG_X86_UV
Ingo Molnar4d732132015-06-08 20:43:07 +0200737apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
Nick Piggin03b48632009-01-20 04:36:04 +0100738#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200739
740apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
741apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Yang Zhangd78f2662013-04-11 19:25:11 +0800743#ifdef CONFIG_HAVE_KVM
Ingo Molnar4d732132015-06-08 20:43:07 +0200744apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
745apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
Yang Zhangd78f2662013-04-11 19:25:11 +0800746#endif
747
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400748#ifdef CONFIG_X86_MCE_THRESHOLD
Ingo Molnar4d732132015-06-08 20:43:07 +0200749apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400750#endif
751
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500752#ifdef CONFIG_X86_MCE_AMD
Ingo Molnar4d732132015-06-08 20:43:07 +0200753apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500754#endif
755
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400756#ifdef CONFIG_X86_THERMAL_VECTOR
Ingo Molnar4d732132015-06-08 20:43:07 +0200757apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400758#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100760#ifdef CONFIG_SMP
Ingo Molnar4d732132015-06-08 20:43:07 +0200761apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
762apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
763apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100764#endif
765
Ingo Molnar4d732132015-06-08 20:43:07 +0200766apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
767apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100768
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800769#ifdef CONFIG_IRQ_WORK
Ingo Molnar4d732132015-06-08 20:43:07 +0200770apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
Ingo Molnar241771e2008-12-03 10:39:53 +0100771#endif
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/*
774 * Exception entry points.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100775 */
Andy Lutomirski9b476682015-03-05 19:19:07 -0800776#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700777
778.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100779ENTRY(\sym)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700780 /* Sanity check */
781 .if \shift_ist != -1 && \paranoid == 0
782 .error "using shift_ist requires paranoid=1"
783 .endif
784
Jan Beulichee4eb872012-11-02 11:18:39 +0000785 ASM_CLAC
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100786 PARAVIRT_ADJUST_EXCEPTION_FRAME
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700787
788 .ifeq \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200789 pushq $-1 /* ORIG_RAX: no syscall to restart */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700790 .endif
791
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800792 ALLOC_PT_GPREGS_ON_STACK
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700793
794 .if \paranoid
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800795 .if \paranoid == 1
Ingo Molnar4d732132015-06-08 20:43:07 +0200796 testb $3, CS(%rsp) /* If coming from userspace, switch stacks */
797 jnz 1f
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800798 .endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200799 call paranoid_entry
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700800 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200801 call error_entry
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700802 .endif
Denys Vlasenkoebfc4532015-02-26 14:40:34 -0800803 /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700804
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700805 .if \paranoid
Andy Lutomirski577ed452014-05-21 15:07:09 -0700806 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200807 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
Andy Lutomirski577ed452014-05-21 15:07:09 -0700808 .else
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100809 TRACE_IRQS_OFF
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700810 .endif
Andy Lutomirski577ed452014-05-21 15:07:09 -0700811 .endif
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700812
Ingo Molnar4d732132015-06-08 20:43:07 +0200813 movq %rsp, %rdi /* pt_regs pointer */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700814
815 .if \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200816 movq ORIG_RAX(%rsp), %rsi /* get error code */
817 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700818 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200819 xorl %esi, %esi /* no error code */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700820 .endif
821
Andy Lutomirski577ed452014-05-21 15:07:09 -0700822 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200823 subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700824 .endif
825
Ingo Molnar4d732132015-06-08 20:43:07 +0200826 call \do_sym
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700827
Andy Lutomirski577ed452014-05-21 15:07:09 -0700828 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200829 addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700830 .endif
831
Denys Vlasenkoebfc4532015-02-26 14:40:34 -0800832 /* these procedures expect "no swapgs" flag in ebx */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700833 .if \paranoid
Ingo Molnar4d732132015-06-08 20:43:07 +0200834 jmp paranoid_exit
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700835 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200836 jmp error_exit
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700837 .endif
838
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800839 .if \paranoid == 1
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800840 /*
841 * Paranoid entry from userspace. Switch stacks and treat it
842 * as a normal entry. This means that paranoid handlers
843 * run in real process context if user_mode(regs).
844 */
8451:
Ingo Molnar4d732132015-06-08 20:43:07 +0200846 call error_entry
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800847
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800848
Ingo Molnar4d732132015-06-08 20:43:07 +0200849 movq %rsp, %rdi /* pt_regs pointer */
850 call sync_regs
851 movq %rax, %rsp /* switch stack */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800852
Ingo Molnar4d732132015-06-08 20:43:07 +0200853 movq %rsp, %rdi /* pt_regs pointer */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800854
855 .if \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200856 movq ORIG_RAX(%rsp), %rsi /* get error code */
857 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800858 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200859 xorl %esi, %esi /* no error code */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800860 .endif
861
Ingo Molnar4d732132015-06-08 20:43:07 +0200862 call \do_sym
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800863
Ingo Molnar4d732132015-06-08 20:43:07 +0200864 jmp error_exit /* %ebx: no swapgs flag */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800865 .endif
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +0100866END(\sym)
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100867.endm
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100868
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400869#ifdef CONFIG_TRACING
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700870.macro trace_idtentry sym do_sym has_error_code:req
871idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
872idtentry \sym \do_sym has_error_code=\has_error_code
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400873.endm
874#else
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700875.macro trace_idtentry sym do_sym has_error_code:req
876idtentry \sym \do_sym has_error_code=\has_error_code
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400877.endm
878#endif
879
Ingo Molnar4d732132015-06-08 20:43:07 +0200880idtentry divide_error do_divide_error has_error_code=0
881idtentry overflow do_overflow has_error_code=0
882idtentry bounds do_bounds has_error_code=0
883idtentry invalid_op do_invalid_op has_error_code=0
884idtentry device_not_available do_device_not_available has_error_code=0
885idtentry double_fault do_double_fault has_error_code=1 paranoid=2
886idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
887idtentry invalid_TSS do_invalid_TSS has_error_code=1
888idtentry segment_not_present do_segment_not_present has_error_code=1
889idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
890idtentry coprocessor_error do_coprocessor_error has_error_code=0
891idtentry alignment_check do_alignment_check has_error_code=1
892idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400893
Ingo Molnar2601e642006-07-03 00:24:45 -0700894
Ingo Molnar4d732132015-06-08 20:43:07 +0200895 /*
896 * Reload gs selector with exception handling
897 * edi: new selector
898 */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -0400899ENTRY(native_load_gs_index)
Ingo Molnar131484c2015-05-28 12:21:47 +0200900 pushfq
Jeremy Fitzhardingeb8aa2872009-01-28 14:35:03 -0800901 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300902 SWAPGS
Borislav Petkov42c748bb2016-04-07 17:31:50 -0700903.Lgs_change:
Ingo Molnar4d732132015-06-08 20:43:07 +0200904 movl %edi, %gs
Borislav Petkov96e5d282016-04-07 17:31:49 -07009052: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100906 SWAPGS
Ingo Molnar131484c2015-05-28 12:21:47 +0200907 popfq
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300908 ret
Alexander van Heukelum6efdcfa2008-11-23 10:15:32 +0100909END(native_load_gs_index)
Al Viro784d5692016-01-11 11:04:34 -0500910EXPORT_SYMBOL(native_load_gs_index)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100911
Borislav Petkov42c748bb2016-04-07 17:31:50 -0700912 _ASM_EXTABLE(.Lgs_change, bad_gs)
Ingo Molnar4d732132015-06-08 20:43:07 +0200913 .section .fixup, "ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* running with kernelgs */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100915bad_gs:
Ingo Molnar4d732132015-06-08 20:43:07 +0200916 SWAPGS /* switch back to user gs */
Andy Lutomirskib038c842016-04-26 12:23:27 -0700917.macro ZAP_GS
918 /* This can't be a string because the preprocessor needs to see it. */
919 movl $__USER_DS, %eax
920 movl %eax, %gs
921.endm
922 ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
Ingo Molnar4d732132015-06-08 20:43:07 +0200923 xorl %eax, %eax
924 movl %eax, %gs
925 jmp 2b
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300926 .previous
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100927
Andi Kleen26995002006-08-02 22:37:28 +0200928/* Call softirq on interrupt stack. Interrupts are off. */
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200929ENTRY(do_softirq_own_stack)
Ingo Molnar4d732132015-06-08 20:43:07 +0200930 pushq %rbp
931 mov %rsp, %rbp
932 incl PER_CPU_VAR(irq_count)
933 cmove PER_CPU_VAR(irq_stack_ptr), %rsp
934 push %rbp /* frame pointer backlink */
935 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +0200936 leaveq
Ingo Molnar4d732132015-06-08 20:43:07 +0200937 decl PER_CPU_VAR(irq_count)
Andi Kleened6b6762005-07-28 21:15:49 -0700938 ret
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200939END(do_softirq_own_stack)
Andi Kleen75154f42007-06-23 02:29:25 +0200940
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700941#ifdef CONFIG_XEN
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700942idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700943
944/*
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300945 * A note on the "critical region" in our callback handler.
946 * We want to avoid stacking callback handlers due to events occurring
947 * during handling of the last event. To do this, we keep events disabled
948 * until we've done all processing. HOWEVER, we must enable events before
949 * popping the stack frame (can't be done atomically) and so it would still
950 * be possible to get enough handler activations to overflow the stack.
951 * Although unlikely, bugs of that kind are hard to track down, so we'd
952 * like to avoid the possibility.
953 * So, on entry to the handler we detect whether we interrupted an
954 * existing activation in its critical region -- if so, we pop the current
955 * activation and restart the handler using the previous one.
956 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200957ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */
958
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300959/*
960 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
961 * see the correct pointer to the pt_regs
962 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200963 movq %rdi, %rsp /* we don't return, adjust the stack frame */
96411: incl PER_CPU_VAR(irq_count)
965 movq %rsp, %rbp
966 cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
967 pushq %rbp /* frame pointer backlink */
968 call xen_evtchn_do_upcall
969 popq %rsp
970 decl PER_CPU_VAR(irq_count)
David Vrabelfdfd8112015-02-19 15:23:17 +0000971#ifndef CONFIG_PREEMPT
Ingo Molnar4d732132015-06-08 20:43:07 +0200972 call xen_maybe_preempt_hcall
David Vrabelfdfd8112015-02-19 15:23:17 +0000973#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200974 jmp error_exit
Alexander van Heukelum371c3942011-03-11 21:59:38 +0100975END(xen_do_hypervisor_callback)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700976
977/*
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300978 * Hypervisor uses this for application faults while it executes.
979 * We get here for two reasons:
980 * 1. Fault while reloading DS, ES, FS or GS
981 * 2. Fault while executing IRET
982 * Category 1 we do not need to fix up as Xen has already reloaded all segment
983 * registers that could be reloaded and zeroed the others.
984 * Category 2 we fix up by killing the current process. We cannot use the
985 * normal Linux return path in this case because if we use the IRET hypercall
986 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
987 * We distinguish between categories by comparing each saved segment register
988 * with its current contents: any discrepancy means we in category 1.
989 */
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700990ENTRY(xen_failsafe_callback)
Ingo Molnar4d732132015-06-08 20:43:07 +0200991 movl %ds, %ecx
992 cmpw %cx, 0x10(%rsp)
993 jne 1f
994 movl %es, %ecx
995 cmpw %cx, 0x18(%rsp)
996 jne 1f
997 movl %fs, %ecx
998 cmpw %cx, 0x20(%rsp)
999 jne 1f
1000 movl %gs, %ecx
1001 cmpw %cx, 0x28(%rsp)
1002 jne 1f
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001003 /* All segments match their saved values => Category 2 (Bad IRET). */
Ingo Molnar4d732132015-06-08 20:43:07 +02001004 movq (%rsp), %rcx
1005 movq 8(%rsp), %r11
1006 addq $0x30, %rsp
1007 pushq $0 /* RIP */
1008 pushq %r11
1009 pushq %rcx
1010 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -070010111: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
Ingo Molnar4d732132015-06-08 20:43:07 +02001012 movq (%rsp), %rcx
1013 movq 8(%rsp), %r11
1014 addq $0x30, %rsp
1015 pushq $-1 /* orig_ax = -1 => not a system call */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001016 ALLOC_PT_GPREGS_ON_STACK
1017 SAVE_C_REGS
1018 SAVE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +02001019 jmp error_exit
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001020END(xen_failsafe_callback)
1021
Seiji Aguchicf910e82013-06-20 11:46:53 -04001022apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
Sheng Yang38e20b02010-05-14 12:40:51 +01001023 xen_hvm_callback_vector xen_evtchn_do_upcall
1024
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001025#endif /* CONFIG_XEN */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001026
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001027#if IS_ENABLED(CONFIG_HYPERV)
Seiji Aguchicf910e82013-06-20 11:46:53 -04001028apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001029 hyperv_callback_vector hyperv_vector_handler
1030#endif /* CONFIG_HYPERV */
1031
Ingo Molnar4d732132015-06-08 20:43:07 +02001032idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1033idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1034idtentry stack_segment do_stack_segment has_error_code=1
1035
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -07001036#ifdef CONFIG_XEN
Ingo Molnar4d732132015-06-08 20:43:07 +02001037idtentry xen_debug do_debug has_error_code=0
1038idtentry xen_int3 do_int3 has_error_code=0
1039idtentry xen_stack_segment do_stack_segment has_error_code=1
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -07001040#endif
Ingo Molnar4d732132015-06-08 20:43:07 +02001041
1042idtentry general_protection do_general_protection has_error_code=1
1043trace_idtentry page_fault do_page_fault has_error_code=1
1044
Gleb Natapov631bc482010-10-14 11:22:52 +02001045#ifdef CONFIG_KVM_GUEST
Ingo Molnar4d732132015-06-08 20:43:07 +02001046idtentry async_page_fault do_async_page_fault has_error_code=1
Gleb Natapov631bc482010-10-14 11:22:52 +02001047#endif
Ingo Molnar4d732132015-06-08 20:43:07 +02001048
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001049#ifdef CONFIG_X86_MCE
Ingo Molnar4d732132015-06-08 20:43:07 +02001050idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001051#endif
1052
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001053/*
1054 * Save all registers in pt_regs, and switch gs if needed.
1055 * Use slow, but surefire "are we in kernel?" check.
Hugh Dickins05ddad12017-09-26 18:43:07 -07001056 *
1057 * Return: ebx=0: needs swapgs but not SWITCH_USER_CR3 in paranoid_exit
1058 * ebx=1: needs neither swapgs nor SWITCH_USER_CR3 in paranoid_exit
1059 * ebx=2: needs both swapgs and SWITCH_USER_CR3 in paranoid_exit
1060 * ebx=3: needs SWITCH_USER_CR3 but not swapgs in paranoid_exit
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001061 */
1062ENTRY(paranoid_entry)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001063 cld
1064 SAVE_C_REGS 8
1065 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +02001066 movl $1, %ebx
1067 movl $MSR_GS_BASE, %ecx
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001068 rdmsr
Ingo Molnar4d732132015-06-08 20:43:07 +02001069 testl %edx, %edx
1070 js 1f /* negative -> in kernel */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001071 SWAPGS
Ingo Molnar4d732132015-06-08 20:43:07 +02001072 xorl %ebx, %ebx
Hugh Dickins05ddad12017-09-26 18:43:07 -070010731:
1074#ifdef CONFIG_KAISER
1075 /*
1076 * We might have come in between a swapgs and a SWITCH_KERNEL_CR3
1077 * on entry, or between a SWITCH_USER_CR3 and a swapgs on exit.
1078 * Do a conditional SWITCH_KERNEL_CR3: this could safely be done
1079 * unconditionally, but we need to find out whether the reverse
1080 * should be done on return (conveyed to paranoid_exit in %ebx).
1081 */
Hugh Dickins23e09432017-09-24 16:59:49 -07001082 ALTERNATIVE "jmp 2f", "movq %cr3, %rax", X86_FEATURE_KAISER
Hugh Dickins05ddad12017-09-26 18:43:07 -07001083 testl $KAISER_SHADOW_PGD_OFFSET, %eax
1084 jz 2f
1085 orl $2, %ebx
1086 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax
1087 orq x86_cr3_pcid_noflush, %rax
1088 movq %rax, %cr3
10892:
1090#endif
1091 ret
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001092END(paranoid_entry)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001093
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001094/*
1095 * "Paranoid" exit path from exception stack. This is invoked
1096 * only on return from non-NMI IST interrupts that came
1097 * from kernel space.
1098 *
1099 * We may be returning to very strange contexts (e.g. very early
1100 * in syscall entry), so checking for preemption here would
1101 * be complicated. Fortunately, we there's no good reason
1102 * to try to handle preemption here.
Ingo Molnar4d732132015-06-08 20:43:07 +02001103 *
Hugh Dickins05ddad12017-09-26 18:43:07 -07001104 * On entry: ebx=0: needs swapgs but not SWITCH_USER_CR3
1105 * ebx=1: needs neither swapgs nor SWITCH_USER_CR3
1106 * ebx=2: needs both swapgs and SWITCH_USER_CR3
1107 * ebx=3: needs SWITCH_USER_CR3 but not swapgs
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001108 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001109ENTRY(paranoid_exit)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001110 DISABLE_INTERRUPTS(CLBR_NONE)
Steven Rostedt5963e312012-05-30 11:54:53 -04001111 TRACE_IRQS_OFF_DEBUG
Denys Vlasenkof2db9382015-02-26 14:40:30 -08001112 TRACE_IRQS_IRETQ_DEBUG
Hugh Dickins05ddad12017-09-26 18:43:07 -07001113#ifdef CONFIG_KAISER
Hugh Dickins23e09432017-09-24 16:59:49 -07001114 /* No ALTERNATIVE for X86_FEATURE_KAISER: paranoid_entry sets %ebx */
Hugh Dickins05ddad12017-09-26 18:43:07 -07001115 testl $2, %ebx /* SWITCH_USER_CR3 needed? */
1116 jz paranoid_exit_no_switch
1117 SWITCH_USER_CR3
1118paranoid_exit_no_switch:
1119#endif
1120 testl $1, %ebx /* swapgs needed? */
1121 jnz paranoid_exit_no_swapgs
1122 SWAPGS_UNSAFE_STACK
1123paranoid_exit_no_swapgs:
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001124 RESTORE_EXTRA_REGS
1125 RESTORE_C_REGS
1126 REMOVE_PT_GPREGS_FROM_STACK 8
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001127 INTERRUPT_RETURN
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001128END(paranoid_exit)
1129
1130/*
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001131 * Save all registers in pt_regs, and switch gs if needed.
Andy Lutomirski539f5112015-06-09 12:36:01 -07001132 * Return: EBX=0: came from user mode; EBX=1: otherwise
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001133 */
1134ENTRY(error_entry)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001135 cld
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001136 SAVE_C_REGS 8
1137 SAVE_EXTRA_REGS 8
Dave Hansen8f0baad2017-08-30 16:23:00 -07001138 /*
1139 * error_entry() always returns with a kernel gsbase and
1140 * CR3. We must also have a kernel CR3/gsbase before
1141 * calling TRACE_IRQS_*. Just unconditionally switch to
1142 * the kernel CR3 here.
1143 */
1144 SWITCH_KERNEL_CR3
Ingo Molnar4d732132015-06-08 20:43:07 +02001145 xorl %ebx, %ebx
Denys Vlasenko03335e92015-04-27 15:21:52 +02001146 testb $3, CS+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001147 jz .Lerror_kernelspace
Andy Lutomirski539f5112015-06-09 12:36:01 -07001148
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001149 /*
1150 * We entered from user mode or we're pretending to have entered
1151 * from user mode due to an IRET fault.
1152 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001153 SWAPGS
Andy Lutomirski539f5112015-06-09 12:36:01 -07001154
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001155.Lerror_entry_from_usermode_after_swapgs:
Andy Lutomirskif1075052015-11-12 12:59:00 -08001156 /*
1157 * We need to tell lockdep that IRQs are off. We can't do this until
1158 * we fix gsbase, and we should do it before enter_from_user_mode
1159 * (which can take locks).
1160 */
1161 TRACE_IRQS_OFF
Andy Lutomirski478dc892015-11-12 12:59:04 -08001162 CALL_enter_from_user_mode
Andy Lutomirskif1075052015-11-12 12:59:00 -08001163 ret
Andy Lutomirski02bc7762015-07-03 12:44:31 -07001164
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001165.Lerror_entry_done:
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001166 TRACE_IRQS_OFF
1167 ret
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001168
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001169 /*
1170 * There are two places in the kernel that can potentially fault with
1171 * usergs. Handle them here. B stepping K8s sometimes report a
1172 * truncated RIP for IRET exceptions returning to compat mode. Check
1173 * for these here too.
1174 */
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001175.Lerror_kernelspace:
Ingo Molnar4d732132015-06-08 20:43:07 +02001176 incl %ebx
1177 leaq native_irq_return_iret(%rip), %rcx
1178 cmpq %rcx, RIP+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001179 je .Lerror_bad_iret
Ingo Molnar4d732132015-06-08 20:43:07 +02001180 movl %ecx, %eax /* zero extend */
1181 cmpq %rax, RIP+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001182 je .Lbstep_iret
Borislav Petkov42c748bb2016-04-07 17:31:50 -07001183 cmpq $.Lgs_change, RIP+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001184 jne .Lerror_entry_done
Andy Lutomirski539f5112015-06-09 12:36:01 -07001185
1186 /*
Borislav Petkov42c748bb2016-04-07 17:31:50 -07001187 * hack: .Lgs_change can fail with user gsbase. If this happens, fix up
Andy Lutomirski539f5112015-06-09 12:36:01 -07001188 * gsbase and proceed. We'll fix up the exception and land in
Borislav Petkov42c748bb2016-04-07 17:31:50 -07001189 * .Lgs_change's error handler with kernel gsbase.
Andy Lutomirski539f5112015-06-09 12:36:01 -07001190 */
Wanpeng Li2fa5f042016-09-30 09:01:06 +08001191 SWAPGS
1192 jmp .Lerror_entry_done
Brian Gerstae24ffe2009-10-12 10:18:23 -04001193
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001194.Lbstep_iret:
Brian Gerstae24ffe2009-10-12 10:18:23 -04001195 /* Fix truncated RIP */
Ingo Molnar4d732132015-06-08 20:43:07 +02001196 movq %rcx, RIP+8(%rsp)
Andy Lutomirskib645af22014-11-22 18:00:33 -08001197 /* fall through */
1198
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001199.Lerror_bad_iret:
Andy Lutomirski539f5112015-06-09 12:36:01 -07001200 /*
1201 * We came from an IRET to user mode, so we have user gsbase.
1202 * Switch to kernel gsbase:
1203 */
Andy Lutomirskib645af22014-11-22 18:00:33 -08001204 SWAPGS
Andy Lutomirski539f5112015-06-09 12:36:01 -07001205
1206 /*
1207 * Pretend that the exception came from user mode: set up pt_regs
1208 * as if we faulted immediately after IRET and clear EBX so that
1209 * error_exit knows that we will be returning to user mode.
1210 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001211 mov %rsp, %rdi
1212 call fixup_bad_iret
1213 mov %rax, %rsp
Andy Lutomirski539f5112015-06-09 12:36:01 -07001214 decl %ebx
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001215 jmp .Lerror_entry_from_usermode_after_swapgs
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001216END(error_entry)
1217
1218
Andy Lutomirski539f5112015-06-09 12:36:01 -07001219/*
Nicolas Iooss75ca5b22016-07-29 13:39:51 +02001220 * On entry, EBX is a "return to kernel mode" flag:
Andy Lutomirski539f5112015-06-09 12:36:01 -07001221 * 1: already in kernel mode, don't need SWAPGS
1222 * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
1223 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001224ENTRY(error_exit)
Ingo Molnar4d732132015-06-08 20:43:07 +02001225 movl %ebx, %eax
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001226 DISABLE_INTERRUPTS(CLBR_NONE)
1227 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +02001228 testl %eax, %eax
1229 jnz retint_kernel
1230 jmp retint_user
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001231END(error_exit)
1232
Denys Vlasenko0784b362015-04-01 16:50:57 +02001233/* Runs on exception stack */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001234ENTRY(nmi)
Andy Lutomirskifc57a7c2015-09-20 16:32:04 -07001235 /*
1236 * Fix up the exception frame if we're on Xen.
1237 * PARAVIRT_ADJUST_EXCEPTION_FRAME is guaranteed to push at most
1238 * one value to the stack on native, so it may clobber the rdx
1239 * scratch slot, but it won't clobber any of the important
1240 * slots past it.
1241 *
1242 * Xen is a different story, because the Xen frame itself overlaps
1243 * the "NMI executing" variable.
1244 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001245 PARAVIRT_ADJUST_EXCEPTION_FRAME
Andy Lutomirskifc57a7c2015-09-20 16:32:04 -07001246
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001247 /*
1248 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1249 * the iretq it performs will take us out of NMI context.
1250 * This means that we can have nested NMIs where the next
1251 * NMI is using the top of the stack of the previous NMI. We
1252 * can't let it execute because the nested NMI will corrupt the
1253 * stack of the previous NMI. NMI handlers are not re-entrant
1254 * anyway.
1255 *
1256 * To handle this case we do the following:
1257 * Check the a special location on the stack that contains
1258 * a variable that is set when NMIs are executing.
1259 * The interrupted task's stack is also checked to see if it
1260 * is an NMI stack.
1261 * If the variable is not set and the stack is not the NMI
1262 * stack then:
1263 * o Set the special variable on the stack
Andy Lutomirski0b229302015-07-15 10:29:36 -07001264 * o Copy the interrupt frame into an "outermost" location on the
1265 * stack
1266 * o Copy the interrupt frame into an "iret" location on the stack
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001267 * o Continue processing the NMI
1268 * If the variable is set or the previous stack is the NMI stack:
Andy Lutomirski0b229302015-07-15 10:29:36 -07001269 * o Modify the "iret" location to jump to the repeat_nmi
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001270 * o return back to the first NMI
1271 *
1272 * Now on exit of the first NMI, we first clear the stack variable
1273 * The NMI stack will tell any nested NMIs at that point that it is
1274 * nested. Then we pop the stack normally with iret, and if there was
1275 * a nested NMI that updated the copy interrupt stack frame, a
1276 * jump will be made to the repeat_nmi code that will handle the second
1277 * NMI.
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001278 *
1279 * However, espfix prevents us from directly returning to userspace
1280 * with a single IRET instruction. Similarly, IRET to user mode
1281 * can fault. We therefore handle NMIs from user space like
1282 * other IST entries.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001283 */
1284
Andy Lutomirski10d5bf22017-08-07 19:43:13 -07001285 ASM_CLAC
1286
Denys Vlasenko146b2b02015-03-25 18:18:13 +01001287 /* Use %rdx as our temp variable throughout */
Ingo Molnar4d732132015-06-08 20:43:07 +02001288 pushq %rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001289
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001290 testb $3, CS-RIP+8(%rsp)
1291 jz .Lnmi_from_kernel
Steven Rostedt45d5a162012-02-19 16:43:37 -05001292
1293 /*
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001294 * NMI from user mode. We need to run on the thread stack, but we
1295 * can't go through the normal entry paths: NMIs are masked, and
1296 * we don't want to enable interrupts, because then we'll end
1297 * up in an awkward situation in which IRQs are on but NMIs
1298 * are off.
Andy Lutomirski83c133c2015-09-20 16:32:05 -07001299 *
1300 * We also must not push anything to the stack before switching
1301 * stacks lest we corrupt the "NMI executing" variable.
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001302 */
1303
Andy Lutomirski83c133c2015-09-20 16:32:05 -07001304 SWAPGS_UNSAFE_STACK
Dave Hansen8f0baad2017-08-30 16:23:00 -07001305 /*
1306 * percpu variables are mapped with user CR3, so no need
1307 * to switch CR3 here.
1308 */
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001309 cld
1310 movq %rsp, %rdx
1311 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
1312 pushq 5*8(%rdx) /* pt_regs->ss */
1313 pushq 4*8(%rdx) /* pt_regs->rsp */
1314 pushq 3*8(%rdx) /* pt_regs->flags */
1315 pushq 2*8(%rdx) /* pt_regs->cs */
1316 pushq 1*8(%rdx) /* pt_regs->rip */
1317 pushq $-1 /* pt_regs->orig_ax */
1318 pushq %rdi /* pt_regs->di */
1319 pushq %rsi /* pt_regs->si */
1320 pushq (%rdx) /* pt_regs->dx */
1321 pushq %rcx /* pt_regs->cx */
1322 pushq %rax /* pt_regs->ax */
1323 pushq %r8 /* pt_regs->r8 */
1324 pushq %r9 /* pt_regs->r9 */
1325 pushq %r10 /* pt_regs->r10 */
1326 pushq %r11 /* pt_regs->r11 */
1327 pushq %rbx /* pt_regs->rbx */
1328 pushq %rbp /* pt_regs->rbp */
1329 pushq %r12 /* pt_regs->r12 */
1330 pushq %r13 /* pt_regs->r13 */
1331 pushq %r14 /* pt_regs->r14 */
1332 pushq %r15 /* pt_regs->r15 */
1333
1334 /*
1335 * At this point we no longer need to worry about stack damage
1336 * due to nesting -- we're on the normal thread stack and we're
1337 * done with the NMI stack.
1338 */
1339
1340 movq %rsp, %rdi
1341 movq $-1, %rsi
Dave Hansen8f0baad2017-08-30 16:23:00 -07001342#ifdef CONFIG_KAISER
1343 /* Unconditionally use kernel CR3 for do_nmi() */
1344 /* %rax is saved above, so OK to clobber here */
Hugh Dickins23e09432017-09-24 16:59:49 -07001345 ALTERNATIVE "jmp 2f", "movq %cr3, %rax", X86_FEATURE_KAISER
Hugh Dickinsd0142ce2017-08-27 16:24:27 -07001346 /* If PCID enabled, NOFLUSH now and NOFLUSH on return */
1347 orq x86_cr3_pcid_noflush, %rax
Dave Hansen8f0baad2017-08-30 16:23:00 -07001348 pushq %rax
Hugh Dickins2684b122017-08-30 16:23:00 -07001349 /* mask off "user" bit of pgd address and 12 PCID bits: */
1350 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax
Dave Hansen8f0baad2017-08-30 16:23:00 -07001351 movq %rax, %cr3
Hugh Dickins23e09432017-09-24 16:59:49 -070013522:
Dave Hansen8f0baad2017-08-30 16:23:00 -07001353#endif
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001354 call do_nmi
Hugh Dickins19377942017-09-21 20:39:56 -07001355
1356#ifdef CONFIG_KAISER
Dave Hansen8f0baad2017-08-30 16:23:00 -07001357 /*
1358 * Unconditionally restore CR3. I know we return to
1359 * kernel code that needs user CR3, but do we ever return
1360 * to "user mode" where we need the kernel CR3?
1361 */
Hugh Dickins23e09432017-09-24 16:59:49 -07001362 ALTERNATIVE "", "popq %rax; movq %rax, %cr3", X86_FEATURE_KAISER
Dave Hansen8f0baad2017-08-30 16:23:00 -07001363#endif
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001364
1365 /*
1366 * Return back to user mode. We must *not* do the normal exit
Dave Hansen8f0baad2017-08-30 16:23:00 -07001367 * work, because we don't want to enable interrupts. Do not
1368 * switch to user CR3: we might be going back to kernel code
1369 * that had a user CR3 set.
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001370 */
1371 SWAPGS
1372 jmp restore_c_regs_and_iret
1373
1374.Lnmi_from_kernel:
1375 /*
Andy Lutomirski0b229302015-07-15 10:29:36 -07001376 * Here's what our stack frame will look like:
1377 * +---------------------------------------------------------+
1378 * | original SS |
1379 * | original Return RSP |
1380 * | original RFLAGS |
1381 * | original CS |
1382 * | original RIP |
1383 * +---------------------------------------------------------+
1384 * | temp storage for rdx |
1385 * +---------------------------------------------------------+
1386 * | "NMI executing" variable |
1387 * +---------------------------------------------------------+
1388 * | iret SS } Copied from "outermost" frame |
1389 * | iret Return RSP } on each loop iteration; overwritten |
1390 * | iret RFLAGS } by a nested NMI to force another |
1391 * | iret CS } iteration if needed. |
1392 * | iret RIP } |
1393 * +---------------------------------------------------------+
1394 * | outermost SS } initialized in first_nmi; |
1395 * | outermost Return RSP } will not be changed before |
1396 * | outermost RFLAGS } NMI processing is done. |
1397 * | outermost CS } Copied to "iret" frame on each |
1398 * | outermost RIP } iteration. |
1399 * +---------------------------------------------------------+
1400 * | pt_regs |
1401 * +---------------------------------------------------------+
1402 *
1403 * The "original" frame is used by hardware. Before re-enabling
1404 * NMIs, we need to be done with it, and we need to leave enough
1405 * space for the asm code here.
1406 *
1407 * We return by executing IRET while RSP points to the "iret" frame.
1408 * That will either return for real or it will loop back into NMI
1409 * processing.
1410 *
1411 * The "outermost" frame is copied to the "iret" frame on each
1412 * iteration of the loop, so each iteration starts with the "iret"
1413 * frame pointing to the final return target.
1414 */
1415
1416 /*
1417 * Determine whether we're a nested NMI.
1418 *
Andy Lutomirskia27507c2015-07-15 10:29:37 -07001419 * If we interrupted kernel code between repeat_nmi and
1420 * end_repeat_nmi, then we are a nested NMI. We must not
1421 * modify the "iret" frame because it's being written by
1422 * the outer NMI. That's okay; the outer NMI handler is
1423 * about to about to call do_nmi anyway, so we can just
1424 * resume the outer NMI.
1425 */
1426
1427 movq $repeat_nmi, %rdx
1428 cmpq 8(%rsp), %rdx
1429 ja 1f
1430 movq $end_repeat_nmi, %rdx
1431 cmpq 8(%rsp), %rdx
1432 ja nested_nmi_out
14331:
1434
1435 /*
1436 * Now check "NMI executing". If it's set, then we're nested.
Andy Lutomirski0b229302015-07-15 10:29:36 -07001437 * This will not detect if we interrupted an outer NMI just
1438 * before IRET.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001439 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001440 cmpl $1, -8(%rsp)
1441 je nested_nmi
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001442
1443 /*
Andy Lutomirski0b229302015-07-15 10:29:36 -07001444 * Now test if the previous stack was an NMI stack. This covers
1445 * the case where we interrupt an outer NMI after it clears
Andy Lutomirski810bc072015-07-15 10:29:38 -07001446 * "NMI executing" but before IRET. We need to be careful, though:
1447 * there is one case in which RSP could point to the NMI stack
1448 * despite there being no NMI active: naughty userspace controls
1449 * RSP at the very beginning of the SYSCALL targets. We can
1450 * pull a fast one on naughty userspace, though: we program
1451 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1452 * if it controls the kernel's RSP. We set DF before we clear
1453 * "NMI executing".
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001454 */
Denys Vlasenko0784b362015-04-01 16:50:57 +02001455 lea 6*8(%rsp), %rdx
1456 /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1457 cmpq %rdx, 4*8(%rsp)
1458 /* If the stack pointer is above the NMI stack, this is a normal NMI */
1459 ja first_nmi
Ingo Molnar4d732132015-06-08 20:43:07 +02001460
Denys Vlasenko0784b362015-04-01 16:50:57 +02001461 subq $EXCEPTION_STKSZ, %rdx
1462 cmpq %rdx, 4*8(%rsp)
1463 /* If it is below the NMI stack, it is a normal NMI */
1464 jb first_nmi
Andy Lutomirski810bc072015-07-15 10:29:38 -07001465
1466 /* Ah, it is within the NMI stack. */
1467
1468 testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1469 jz first_nmi /* RSP was user controlled. */
1470
1471 /* This is a nested NMI. */
Denys Vlasenko0784b362015-04-01 16:50:57 +02001472
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001473nested_nmi:
1474 /*
Andy Lutomirski0b229302015-07-15 10:29:36 -07001475 * Modify the "iret" frame to point to repeat_nmi, forcing another
1476 * iteration of NMI handling.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001477 */
Andy Lutomirski23a781e2015-07-15 10:29:39 -07001478 subq $8, %rsp
Ingo Molnar4d732132015-06-08 20:43:07 +02001479 leaq -10*8(%rsp), %rdx
1480 pushq $__KERNEL_DS
1481 pushq %rdx
Ingo Molnar131484c2015-05-28 12:21:47 +02001482 pushfq
Ingo Molnar4d732132015-06-08 20:43:07 +02001483 pushq $__KERNEL_CS
1484 pushq $repeat_nmi
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001485
1486 /* Put stack back */
Ingo Molnar4d732132015-06-08 20:43:07 +02001487 addq $(6*8), %rsp
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001488
1489nested_nmi_out:
Ingo Molnar4d732132015-06-08 20:43:07 +02001490 popq %rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001491
Andy Lutomirski0b229302015-07-15 10:29:36 -07001492 /* We are returning to kernel mode, so this cannot result in a fault. */
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001493 INTERRUPT_RETURN
1494
1495first_nmi:
Andy Lutomirski0b229302015-07-15 10:29:36 -07001496 /* Restore rdx. */
Ingo Molnar4d732132015-06-08 20:43:07 +02001497 movq (%rsp), %rdx
Jan Beulich62610912012-02-24 14:54:37 +00001498
Andy Lutomirski36f1a772015-07-15 10:29:40 -07001499 /* Make room for "NMI executing". */
1500 pushq $0
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001501
Andy Lutomirski0b229302015-07-15 10:29:36 -07001502 /* Leave room for the "iret" frame */
Ingo Molnar4d732132015-06-08 20:43:07 +02001503 subq $(5*8), %rsp
Salman Qazi28696f42012-10-01 17:29:25 -07001504
Andy Lutomirski0b229302015-07-15 10:29:36 -07001505 /* Copy the "original" frame to the "outermost" frame */
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001506 .rept 5
Ingo Molnar4d732132015-06-08 20:43:07 +02001507 pushq 11*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001508 .endr
Jan Beulich62610912012-02-24 14:54:37 +00001509
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001510 /* Everything up to here is safe from nested NMIs */
1511
Andy Lutomirskia97439a2015-07-15 10:29:41 -07001512#ifdef CONFIG_DEBUG_ENTRY
1513 /*
1514 * For ease of testing, unmask NMIs right away. Disabled by
1515 * default because IRET is very expensive.
1516 */
1517 pushq $0 /* SS */
1518 pushq %rsp /* RSP (minus 8 because of the previous push) */
1519 addq $8, (%rsp) /* Fix up RSP */
1520 pushfq /* RFLAGS */
1521 pushq $__KERNEL_CS /* CS */
1522 pushq $1f /* RIP */
1523 INTERRUPT_RETURN /* continues at repeat_nmi below */
15241:
1525#endif
1526
Andy Lutomirski0b229302015-07-15 10:29:36 -07001527repeat_nmi:
Jan Beulich62610912012-02-24 14:54:37 +00001528 /*
1529 * If there was a nested NMI, the first NMI's iret will return
1530 * here. But NMIs are still enabled and we can take another
1531 * nested NMI. The nested NMI checks the interrupted RIP to see
1532 * if it is between repeat_nmi and end_repeat_nmi, and if so
1533 * it will just return, as we are about to repeat an NMI anyway.
1534 * This makes it safe to copy to the stack frame that a nested
1535 * NMI will update.
Andy Lutomirski0b229302015-07-15 10:29:36 -07001536 *
1537 * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
1538 * we're repeating an NMI, gsbase has the same value that it had on
1539 * the first iteration. paranoid_entry will load the kernel
Andy Lutomirski36f1a772015-07-15 10:29:40 -07001540 * gsbase if needed before we call do_nmi. "NMI executing"
1541 * is zero.
Jan Beulich62610912012-02-24 14:54:37 +00001542 */
Andy Lutomirski36f1a772015-07-15 10:29:40 -07001543 movq $1, 10*8(%rsp) /* Set "NMI executing". */
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001544
Andy Lutomirski0b229302015-07-15 10:29:36 -07001545 /*
1546 * Copy the "outermost" frame to the "iret" frame. NMIs that nest
1547 * here must not modify the "iret" frame while we're writing to
1548 * it or it will end up containing garbage.
1549 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001550 addq $(10*8), %rsp
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001551 .rept 5
Ingo Molnar4d732132015-06-08 20:43:07 +02001552 pushq -6*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001553 .endr
Ingo Molnar4d732132015-06-08 20:43:07 +02001554 subq $(5*8), %rsp
Jan Beulich62610912012-02-24 14:54:37 +00001555end_repeat_nmi:
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001556
1557 /*
Andy Lutomirski0b229302015-07-15 10:29:36 -07001558 * Everything below this point can be preempted by a nested NMI.
1559 * If this happens, then the inner NMI will change the "iret"
1560 * frame to point back to repeat_nmi.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001561 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001562 pushq $-1 /* ORIG_RAX: no syscall to restart */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001563 ALLOC_PT_GPREGS_ON_STACK
1564
Steven Rostedt1fd466e2011-12-08 12:32:27 -05001565 /*
Dave Hansen8f0baad2017-08-30 16:23:00 -07001566 * Use the same approach as paranoid_entry to handle SWAPGS, but
1567 * without CR3 handling since we do that differently in NMIs. No
1568 * need to use paranoid_exit as we should not be calling schedule
1569 * in NMI context. Even with normal interrupts enabled. An NMI
1570 * should not be setting NEED_RESCHED or anything that normal
1571 * interrupts and exceptions might do.
Steven Rostedt1fd466e2011-12-08 12:32:27 -05001572 */
Dave Hansen8f0baad2017-08-30 16:23:00 -07001573 cld
1574 SAVE_C_REGS
1575 SAVE_EXTRA_REGS
1576 movl $1, %ebx
1577 movl $MSR_GS_BASE, %ecx
1578 rdmsr
1579 testl %edx, %edx
1580 js 1f /* negative -> in kernel */
1581 SWAPGS
1582 xorl %ebx, %ebx
15831:
Hugh Dickins19377942017-09-21 20:39:56 -07001584 movq %rsp, %rdi
1585 movq $-1, %rsi
Dave Hansen8f0baad2017-08-30 16:23:00 -07001586#ifdef CONFIG_KAISER
1587 /* Unconditionally use kernel CR3 for do_nmi() */
1588 /* %rax is saved above, so OK to clobber here */
Hugh Dickins23e09432017-09-24 16:59:49 -07001589 ALTERNATIVE "jmp 2f", "movq %cr3, %rax", X86_FEATURE_KAISER
Hugh Dickinsd0142ce2017-08-27 16:24:27 -07001590 /* If PCID enabled, NOFLUSH now and NOFLUSH on return */
1591 orq x86_cr3_pcid_noflush, %rax
Dave Hansen8f0baad2017-08-30 16:23:00 -07001592 pushq %rax
Hugh Dickins2684b122017-08-30 16:23:00 -07001593 /* mask off "user" bit of pgd address and 12 PCID bits: */
1594 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax
Dave Hansen8f0baad2017-08-30 16:23:00 -07001595 movq %rax, %cr3
Hugh Dickins23e09432017-09-24 16:59:49 -070015962:
Dave Hansen8f0baad2017-08-30 16:23:00 -07001597#endif
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001598
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001599 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
Ingo Molnar4d732132015-06-08 20:43:07 +02001600 call do_nmi
Hugh Dickins19377942017-09-21 20:39:56 -07001601
1602#ifdef CONFIG_KAISER
Dave Hansen8f0baad2017-08-30 16:23:00 -07001603 /*
1604 * Unconditionally restore CR3. We might be returning to
1605 * kernel code that needs user CR3, like just just before
1606 * a sysret.
1607 */
Hugh Dickins23e09432017-09-24 16:59:49 -07001608 ALTERNATIVE "", "popq %rax; movq %rax, %cr3", X86_FEATURE_KAISER
Dave Hansen8f0baad2017-08-30 16:23:00 -07001609#endif
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001610
Ingo Molnar4d732132015-06-08 20:43:07 +02001611 testl %ebx, %ebx /* swapgs needed? */
1612 jnz nmi_restore
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001613nmi_swapgs:
Dave Hansen8f0baad2017-08-30 16:23:00 -07001614 /* We fixed up CR3 above, so no need to switch it here */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001615 SWAPGS_UNSAFE_STACK
1616nmi_restore:
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001617 RESTORE_EXTRA_REGS
1618 RESTORE_C_REGS
Andy Lutomirski0b229302015-07-15 10:29:36 -07001619
1620 /* Point RSP at the "iret" frame. */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001621 REMOVE_PT_GPREGS_FROM_STACK 6*8
Salman Qazi28696f42012-10-01 17:29:25 -07001622
Andy Lutomirski810bc072015-07-15 10:29:38 -07001623 /*
1624 * Clear "NMI executing". Set DF first so that we can easily
1625 * distinguish the remaining code between here and IRET from
1626 * the SYSCALL entry and exit paths. On a native kernel, we
1627 * could just inspect RIP, but, on paravirt kernels,
1628 * INTERRUPT_RETURN can translate into a jump into a
1629 * hypercall page.
1630 */
1631 std
1632 movq $0, 5*8(%rsp) /* clear "NMI executing" */
Andy Lutomirski0b229302015-07-15 10:29:36 -07001633
1634 /*
1635 * INTERRUPT_RETURN reads the "iret" frame and exits the NMI
1636 * stack in a single instruction. We are returning to kernel
1637 * mode, so this cannot result in a fault.
1638 */
Andy Lutomirski5ca6f702015-06-04 13:24:29 -07001639 INTERRUPT_RETURN
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001640END(nmi)
1641
1642ENTRY(ignore_sysret)
Ingo Molnar4d732132015-06-08 20:43:07 +02001643 mov $-ENOSYS, %eax
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001644 sysret
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001645END(ignore_sysret)
Andy Lutomirski2deb4be2016-07-14 13:22:55 -07001646
1647ENTRY(rewind_stack_do_exit)
1648 /* Prevent any naive code from trying to unwind to our caller. */
1649 xorl %ebp, %ebp
1650
1651 movq PER_CPU_VAR(cpu_current_top_of_stack), %rax
1652 leaq -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%rax), %rsp
1653
1654 call do_exit
16551: jmp 1b
1656END(rewind_stack_do_exit)