blob: b9c901ce6582a8974356154d1fd935b136333e78 [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>
David Woodhouse8b1bacc2018-01-11 21:46:28 +000040#include <asm/nospec-branch.h>
Eric Parisd7e75282012-01-03 14:23:06 -050041#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Roland McGrath86a1c342008-06-23 15:37:04 -070043/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
44#include <linux/elf-em.h>
Ingo Molnar4d732132015-06-08 20:43:07 +020045#define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
46#define __AUDIT_ARCH_64BIT 0x80000000
47#define __AUDIT_ARCH_LE 0x40000000
Roland McGrath86a1c342008-06-23 15:37:04 -070048
Ingo Molnar4d732132015-06-08 20:43:07 +020049.code64
50.section .entry.text, "ax"
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020051
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010052#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040053ENTRY(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010054 swapgs
55 sysretq
Cyrill Gorcunovb3baaa12009-02-23 22:57:00 +030056ENDPROC(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010057#endif /* CONFIG_PARAVIRT */
58
Greg Kroah-Hartmana11ca512017-12-04 12:59:57 +010059.macro TRACE_IRQS_IRETQ
Ingo Molnar2601e642006-07-03 00:24:45 -070060#ifdef CONFIG_TRACE_IRQFLAGS
Greg Kroah-Hartmana11ca512017-12-04 12:59:57 +010061 bt $9, EFLAGS(%rsp) /* interrupts off? */
Ingo Molnar4d732132015-06-08 20:43:07 +020062 jnc 1f
Ingo Molnar2601e642006-07-03 00:24:45 -070063 TRACE_IRQS_ON
641:
65#endif
66.endm
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
Steven Rostedt5963e312012-05-30 11:54:53 -040069 * When dynamic function tracer is enabled it will add a breakpoint
70 * to all locations that it is about to modify, sync CPUs, update
71 * all the code, sync CPUs, then remove the breakpoints. In this time
72 * if lockdep is enabled, it might jump back into the debug handler
73 * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
74 *
75 * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
76 * make sure the stack pointer does not get reset back to the top
77 * of the debug stack, and instead just reuses the current stack.
78 */
79#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
80
81.macro TRACE_IRQS_OFF_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +020082 call debug_stack_set_zero
Steven Rostedt5963e312012-05-30 11:54:53 -040083 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +020084 call debug_stack_reset
Steven Rostedt5963e312012-05-30 11:54:53 -040085.endm
86
87.macro TRACE_IRQS_ON_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +020088 call debug_stack_set_zero
Steven Rostedt5963e312012-05-30 11:54:53 -040089 TRACE_IRQS_ON
Ingo Molnar4d732132015-06-08 20:43:07 +020090 call debug_stack_reset
Steven Rostedt5963e312012-05-30 11:54:53 -040091.endm
92
Denys Vlasenkof2db9382015-02-26 14:40:30 -080093.macro TRACE_IRQS_IRETQ_DEBUG
Ingo Molnar4d732132015-06-08 20:43:07 +020094 bt $9, EFLAGS(%rsp) /* interrupts off? */
95 jnc 1f
Steven Rostedt5963e312012-05-30 11:54:53 -040096 TRACE_IRQS_ON_DEBUG
971:
98.endm
99
100#else
Ingo Molnar4d732132015-06-08 20:43:07 +0200101# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
102# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
103# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
Steven Rostedt5963e312012-05-30 11:54:53 -0400104#endif
105
106/*
Ingo Molnar4d732132015-06-08 20:43:07 +0200107 * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Andy Lutomirskifda57b22016-03-09 19:00:35 -0800109 * This is the only entry point used for 64-bit system calls. The
110 * hardware interface is reasonably well designed and the register to
111 * argument mapping Linux uses fits well with the registers that are
112 * available when SYSCALL is used.
113 *
114 * SYSCALL instructions can be found inlined in libc implementations as
115 * well as some other programs and libraries. There are also a handful
116 * of SYSCALL instructions in the vDSO used, for example, as a
117 * clock_gettimeofday fallback.
118 *
Ingo Molnar4d732132015-06-08 20:43:07 +0200119 * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800120 * then loads new ss, cs, and rip from previously programmed MSRs.
121 * rflags gets masked by a value from another MSR (so CLD and CLAC
122 * are not needed). SYSCALL does not save anything on the stack
123 * and does not change rsp.
124 *
125 * Registers on entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * rax system call number
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800127 * rcx return address
128 * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * rdi arg0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * rsi arg1
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100131 * rdx arg2
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800132 * r10 arg3 (needs to be moved to rcx to conform to C ABI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * r8 arg4
134 * r9 arg5
Ingo Molnar4d732132015-06-08 20:43:07 +0200135 * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100136 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * Only called from user space.
138 *
Ingo Molnar7fcb3bc2015-03-17 14:42:59 +0100139 * When user can change pt_regs->foo always force IRET. That is because
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200140 * it deals with uncanonical addresses better. SYSRET has trouble
141 * with them due to bugs in both AMD and Intel CPUs.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Ingo Molnarb2502b42015-06-08 08:42:03 +0200144ENTRY(entry_SYSCALL_64)
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100145 /*
146 * Interrupts are off on entry.
147 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
148 * it is too small to ever cause noticeable irq latency.
149 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100150 SWAPGS_UNSAFE_STACK
Richard Fellner13be4482017-05-04 14:26:50 +0200151 SWITCH_KERNEL_CR3_NO_STACK
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100152 /*
153 * A hypervisor implementation might want to use a label
154 * after the swapgs, so that it can do the swapgs
155 * for the guest and jump here on syscall.
156 */
Ingo Molnarb2502b42015-06-08 08:42:03 +0200157GLOBAL(entry_SYSCALL_64_after_swapgs)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100158
Ingo Molnar4d732132015-06-08 20:43:07 +0200159 movq %rsp, PER_CPU_VAR(rsp_scratch)
160 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100161
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800162 TRACE_IRQS_OFF
163
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100164 /* Construct struct pt_regs on stack */
Ingo Molnar4d732132015-06-08 20:43:07 +0200165 pushq $__USER_DS /* pt_regs->ss */
166 pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */
Ingo Molnar4d732132015-06-08 20:43:07 +0200167 pushq %r11 /* pt_regs->flags */
168 pushq $__USER_CS /* pt_regs->cs */
169 pushq %rcx /* pt_regs->ip */
170 pushq %rax /* pt_regs->orig_ax */
171 pushq %rdi /* pt_regs->di */
172 pushq %rsi /* pt_regs->si */
173 pushq %rdx /* pt_regs->dx */
174 pushq %rcx /* pt_regs->cx */
175 pushq $-ENOSYS /* pt_regs->ax */
176 pushq %r8 /* pt_regs->r8 */
177 pushq %r9 /* pt_regs->r9 */
178 pushq %r10 /* pt_regs->r10 */
179 pushq %r11 /* pt_regs->r11 */
180 sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
Denys Vlasenko9ed8e7d2015-03-19 18:17:47 +0100181
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800182 /*
183 * If we need to do entry work or if we guess we'll need to do
184 * exit work, go straight to the slow path.
185 */
Andy Lutomirski15f4eae2016-09-13 14:29:25 -0700186 movq PER_CPU_VAR(current_task), %r11
187 testl $_TIF_WORK_SYSCALL_ENTRY|_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800188 jnz entry_SYSCALL64_slow_path
189
Ingo Molnarb2502b42015-06-08 08:42:03 +0200190entry_SYSCALL_64_fastpath:
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800191 /*
192 * Easy case: enable interrupts and issue the syscall. If the syscall
193 * needs pt_regs, we'll call a stub that disables interrupts again
194 * and jumps to the slow path.
195 */
196 TRACE_IRQS_ON
197 ENABLE_INTERRUPTS(CLBR_NONE)
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800198#if __SYSCALL_MASK == ~0
Ingo Molnar4d732132015-06-08 20:43:07 +0200199 cmpq $__NR_syscall_max, %rax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800200#else
Ingo Molnar4d732132015-06-08 20:43:07 +0200201 andl $__SYSCALL_MASK, %eax
202 cmpl $__NR_syscall_max, %eax
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800203#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200204 ja 1f /* return -ENOSYS (already in pt_regs->ax) */
205 movq %r10, %rcx
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800206
207 /*
208 * This call instruction is handled specially in stub_ptregs_64.
Andy Lutomirskib7765082016-01-31 09:33:26 -0800209 * It might end up jumping to the slow path. If it jumps, RAX
210 * and all argument registers are clobbered.
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800211 */
David Woodhouse8b1bacc2018-01-11 21:46:28 +0000212#ifdef CONFIG_RETPOLINE
213 movq sys_call_table(, %rax, 8), %rax
214 call __x86_indirect_thunk_rax
215#else
Ingo Molnar4d732132015-06-08 20:43:07 +0200216 call *sys_call_table(, %rax, 8)
David Woodhouse8b1bacc2018-01-11 21:46:28 +0000217#endif
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800218.Lentry_SYSCALL_64_after_fastpath_call:
219
Ingo Molnar4d732132015-06-08 20:43:07 +0200220 movq %rax, RAX(%rsp)
Denys Vlasenko146b2b02015-03-25 18:18:13 +01002211:
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800222
Denys Vlasenko4416c5a2015-03-31 19:00:03 +0200223 /*
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800224 * If we get here, then we know that pt_regs is clean for SYSRET64.
225 * If we see that no exit work is required (which we are required
226 * to check with IRQs off), then we can go straight to SYSRET64.
Denys Vlasenko4416c5a2015-03-31 19:00:03 +0200227 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100228 DISABLE_INTERRUPTS(CLBR_NONE)
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800229 TRACE_IRQS_OFF
Andy Lutomirski15f4eae2016-09-13 14:29:25 -0700230 movq PER_CPU_VAR(current_task), %r11
231 testl $_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800232 jnz 1f
Andy Lutomirskib3494a42015-03-23 12:32:54 -0700233
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800234 LOCKDEP_SYS_EXIT
235 TRACE_IRQS_ON /* user mode is traced as IRQs on */
Andy Lutomirskieb2a54c2016-01-31 09:33:27 -0800236 movq RIP(%rsp), %rcx
237 movq EFLAGS(%rsp), %r11
238 RESTORE_C_REGS_EXCEPT_RCX_R11
Dave Hansen8f0baad2017-08-30 16:23:00 -0700239 /*
240 * This opens a window where we have a user CR3, but are
241 * running in the kernel. This makes using the CS
242 * register useless for telling whether or not we need to
243 * switch CR3 in NMIs. Normal interrupts are OK because
244 * they are off here.
245 */
Richard Fellner13be4482017-05-04 14:26:50 +0200246 SWITCH_USER_CR3
Ingo Molnar4d732132015-06-08 20:43:07 +0200247 movq RSP(%rsp), %rsp
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400248 USERGS_SYSRET64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Andy Lutomirski1e423bf2016-01-28 15:11:28 -08002501:
251 /*
252 * The fast path looked good when we started, but something changed
253 * along the way and we need to switch to the slow path. Calling
254 * raise(3) will trigger this, for example. IRQs are off.
255 */
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700256 TRACE_IRQS_ON
257 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800258 SAVE_EXTRA_REGS
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700259 movq %rsp, %rdi
260 call syscall_return_slowpath /* returns with IRQs disabled */
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800261 jmp return_from_SYSCALL_64
262
263entry_SYSCALL64_slow_path:
264 /* IRQs are off. */
265 SAVE_EXTRA_REGS
266 movq %rsp, %rdi
267 call do_syscall_64 /* returns with IRQs disabled */
268
269return_from_SYSCALL_64:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800270 RESTORE_EXTRA_REGS
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700271 TRACE_IRQS_IRETQ /* we're about to change IF */
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200272
273 /*
274 * Try to use SYSRET instead of IRET if we're returning to
275 * a completely clean 64-bit userspace context.
276 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200277 movq RCX(%rsp), %rcx
278 movq RIP(%rsp), %r11
279 cmpq %rcx, %r11 /* RCX == RIP */
280 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200281
282 /*
283 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
284 * in kernel space. This essentially lets the user take over
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200285 * the kernel, since userspace controls RSP.
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200286 *
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200287 * If width of "canonical tail" ever becomes variable, this will need
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200288 * to be updated to remain correct on both old and new CPUs.
289 */
290 .ifne __VIRTUAL_MASK_SHIFT - 47
291 .error "virtual address width changed -- SYSRET checks need update"
292 .endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200293
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200294 /* Change top 16 bits to be the sign-extension of 47th bit */
295 shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
296 sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
Ingo Molnar4d732132015-06-08 20:43:07 +0200297
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200298 /* If this changed %rcx, it was not canonical */
299 cmpq %rcx, %r11
300 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200301
Ingo Molnar4d732132015-06-08 20:43:07 +0200302 cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
303 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200304
Ingo Molnar4d732132015-06-08 20:43:07 +0200305 movq R11(%rsp), %r11
306 cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
307 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200308
309 /*
Borislav Petkov3e035302016-08-03 19:14:29 +0200310 * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
311 * restore RF properly. If the slowpath sets it for whatever reason, we
312 * need to restore it correctly.
313 *
314 * SYSRET can restore TF, but unlike IRET, restoring TF results in a
315 * trap from userspace immediately after SYSRET. This would cause an
316 * infinite loop whenever #DB happens with register state that satisfies
317 * the opportunistic SYSRET conditions. For example, single-stepping
318 * this user code:
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200319 *
Ingo Molnar4d732132015-06-08 20:43:07 +0200320 * movq $stuck_here, %rcx
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200321 * pushfq
322 * popq %r11
323 * stuck_here:
324 *
325 * would never get past 'stuck_here'.
326 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200327 testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
328 jnz opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200329
330 /* nothing to check for RSP */
331
Ingo Molnar4d732132015-06-08 20:43:07 +0200332 cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
333 jne opportunistic_sysret_failed
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200334
335 /*
Ingo Molnar4d732132015-06-08 20:43:07 +0200336 * We win! This label is here just for ease of understanding
337 * perf profiles. Nothing jumps here.
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200338 */
339syscall_return_via_sysret:
Denys Vlasenko17be0ae2015-04-21 18:27:29 +0200340 /* rcx and r11 are already restored (see code above) */
341 RESTORE_C_REGS_EXCEPT_RCX_R11
Dave Hansen8f0baad2017-08-30 16:23:00 -0700342 /*
343 * This opens a window where we have a user CR3, but are
344 * running in the kernel. This makes using the CS
345 * register useless for telling whether or not we need to
346 * switch CR3 in NMIs. Normal interrupts are OK because
347 * they are off here.
348 */
Richard Fellner13be4482017-05-04 14:26:50 +0200349 SWITCH_USER_CR3
Ingo Molnar4d732132015-06-08 20:43:07 +0200350 movq RSP(%rsp), %rsp
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200351 USERGS_SYSRET64
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200352
353opportunistic_sysret_failed:
Dave Hansen8f0baad2017-08-30 16:23:00 -0700354 /*
355 * This opens a window where we have a user CR3, but are
356 * running in the kernel. This makes using the CS
357 * register useless for telling whether or not we need to
358 * switch CR3 in NMIs. Normal interrupts are OK because
359 * they are off here.
360 */
Richard Fellner13be4482017-05-04 14:26:50 +0200361 SWITCH_USER_CR3
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200362 SWAPGS
363 jmp restore_c_regs_and_iret
Ingo Molnarb2502b42015-06-08 08:42:03 +0200364END(entry_SYSCALL_64)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100365
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800366ENTRY(stub_ptregs_64)
367 /*
368 * Syscalls marked as needing ptregs land here.
Andy Lutomirskib7765082016-01-31 09:33:26 -0800369 * If we are on the fast path, we need to save the extra regs,
370 * which we achieve by trying again on the slow path. If we are on
371 * the slow path, the extra regs are already saved.
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800372 *
373 * RAX stores a pointer to the C function implementing the syscall.
Andy Lutomirskib7765082016-01-31 09:33:26 -0800374 * IRQs are on.
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800375 */
376 cmpq $.Lentry_SYSCALL_64_after_fastpath_call, (%rsp)
377 jne 1f
378
Andy Lutomirskib7765082016-01-31 09:33:26 -0800379 /*
380 * Called from fast path -- disable IRQs again, pop return address
381 * and jump to slow path
382 */
383 DISABLE_INTERRUPTS(CLBR_NONE)
384 TRACE_IRQS_OFF
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800385 popq %rax
Andy Lutomirskib7765082016-01-31 09:33:26 -0800386 jmp entry_SYSCALL64_slow_path
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800387
3881:
David Woodhouse8b1bacc2018-01-11 21:46:28 +0000389 JMP_NOSPEC %rax /* Called from C */
Andy Lutomirski302f5b22016-01-28 15:11:25 -0800390END(stub_ptregs_64)
391
392.macro ptregs_stub func
393ENTRY(ptregs_\func)
394 leaq \func(%rip), %rax
395 jmp stub_ptregs_64
396END(ptregs_\func)
397.endm
398
399/* Instantiate ptregs_stub for each ptregs-using syscall */
400#define __SYSCALL_64_QUAL_(sym)
401#define __SYSCALL_64_QUAL_ptregs(sym) ptregs_stub sym
402#define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym)
403#include <asm/syscalls_64.h>
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200404
Jan Beulich7effaa82005-09-12 18:49:24 +0200405/*
Brian Gerst01003012016-08-13 12:38:19 -0400406 * %rdi: prev task
407 * %rsi: next task
408 */
409ENTRY(__switch_to_asm)
410 /*
411 * Save callee-saved registers
412 * This must match the order in inactive_task_frame
413 */
414 pushq %rbp
415 pushq %rbx
416 pushq %r12
417 pushq %r13
418 pushq %r14
419 pushq %r15
420
421 /* switch stack */
422 movq %rsp, TASK_threadsp(%rdi)
423 movq TASK_threadsp(%rsi), %rsp
424
425#ifdef CONFIG_CC_STACKPROTECTOR
426 movq TASK_stack_canary(%rsi), %rbx
427 movq %rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset
428#endif
429
430 /* restore callee-saved registers */
431 popq %r15
432 popq %r14
433 popq %r13
434 popq %r12
435 popq %rbx
436 popq %rbp
437
438 jmp __switch_to
439END(__switch_to_asm)
440
441/*
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800442 * A newly forked process directly context switches into this address.
443 *
Brian Gerst01003012016-08-13 12:38:19 -0400444 * rax: prev task we switched from
Brian Gerst616d2482016-08-13 12:38:20 -0400445 * rbx: kernel thread func (NULL for user thread)
446 * r12: kernel thread arg
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800447 */
448ENTRY(ret_from_fork)
Brian Gerst01003012016-08-13 12:38:19 -0400449 movq %rax, %rdi
Ingo Molnar4d732132015-06-08 20:43:07 +0200450 call schedule_tail /* rdi: 'prev' task parameter */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800451
Brian Gerst616d2482016-08-13 12:38:20 -0400452 testq %rbx, %rbx /* from kernel_thread? */
453 jnz 1f /* kernel threads are uncommon */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800454
Brian Gerst616d2482016-08-13 12:38:20 -04004552:
Andy Lutomirski24d978b2016-01-28 15:11:27 -0800456 movq %rsp, %rdi
457 call syscall_return_slowpath /* returns with IRQs disabled */
458 TRACE_IRQS_ON /* user mode is traced as IRQS on */
Richard Fellner13be4482017-05-04 14:26:50 +0200459 SWITCH_USER_CR3
Andy Lutomirski24d978b2016-01-28 15:11:27 -0800460 SWAPGS
461 jmp restore_regs_and_iret
Brian Gerst616d2482016-08-13 12:38:20 -0400462
4631:
464 /* kernel thread */
465 movq %r12, %rdi
David Woodhouse8b1bacc2018-01-11 21:46:28 +0000466 CALL_NOSPEC %rbx
Brian Gerst616d2482016-08-13 12:38:20 -0400467 /*
468 * A kernel thread is allowed to return here after successfully
469 * calling do_execve(). Exit to userspace to complete the execve()
470 * syscall.
471 */
472 movq $0, RAX(%rsp)
473 jmp 2b
Denys Vlasenko1eeb2072015-02-26 14:40:33 -0800474END(ret_from_fork)
475
476/*
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200477 * Build the entry stubs with some assembler magic.
478 * We pack 1 stub into every 8-byte block.
H. Peter Anvin939b7872008-11-11 13:51:52 -0800479 */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200480 .align 8
H. Peter Anvin939b7872008-11-11 13:51:52 -0800481ENTRY(irq_entries_start)
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200482 vector=FIRST_EXTERNAL_VECTOR
483 .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
Ingo Molnar4d732132015-06-08 20:43:07 +0200484 pushq $(~vector+0x80) /* Note: always in signed byte range */
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200485 vector=vector+1
486 jmp common_interrupt
Denys Vlasenko3304c9c2015-04-03 21:49:13 +0200487 .align 8
488 .endr
H. Peter Anvin939b7872008-11-11 13:51:52 -0800489END(irq_entries_start)
490
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100491/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * Interrupt entry/exit.
493 *
494 * Interrupt entry points save only callee clobbered registers in fast path.
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100495 *
496 * Entry runs with interrupts off.
497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100499/* 0(%rsp): ~(interrupt number) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 .macro interrupt func
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100501 cld
Andy Lutomirskiff467592015-07-03 12:44:29 -0700502 ALLOC_PT_GPREGS_ON_STACK
503 SAVE_C_REGS
504 SAVE_EXTRA_REGS
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100505
Andy Lutomirskiff467592015-07-03 12:44:29 -0700506 testb $3, CS(%rsp)
Denys Vlasenkodde74f22015-04-27 15:21:51 +0200507 jz 1f
Andy Lutomirski02bc7762015-07-03 12:44:31 -0700508
509 /*
510 * IRQ from user mode. Switch to kernel gsbase and inform context
511 * tracking that we're in kernel mode.
512 */
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100513 SWAPGS
Richard Fellner13be4482017-05-04 14:26:50 +0200514 SWITCH_KERNEL_CR3
Andy Lutomirskif1075052015-11-12 12:59:00 -0800515
516 /*
517 * We need to tell lockdep that IRQs are off. We can't do this until
518 * we fix gsbase, and we should do it before enter_from_user_mode
519 * (which can take locks). Since TRACE_IRQS_OFF idempotent,
520 * the simplest way to handle it is to just call it twice if
521 * we enter from user mode. There's no reason to optimize this since
522 * TRACE_IRQS_OFF is a no-op if lockdep is off.
523 */
524 TRACE_IRQS_OFF
525
Andy Lutomirski478dc892015-11-12 12:59:04 -0800526 CALL_enter_from_user_mode
Andy Lutomirski02bc7762015-07-03 12:44:31 -0700527
Denys Vlasenko76f5df42015-02-26 14:40:27 -08005281:
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100529 /*
Denys Vlasenkoe90e1472015-02-26 14:40:28 -0800530 * Save previous stack pointer, optionally switch to interrupt stack.
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100531 * irq_count is used to check if a CPU is already on an interrupt stack
532 * or not. While this is essentially redundant with preempt_count it is
533 * a little cheaper to use a separate counter in the PDA (short of
534 * moving irq_enter into assembly, which would be too much work)
535 */
Andy Lutomirskia586f982015-07-03 12:44:30 -0700536 movq %rsp, %rdi
Ingo Molnar4d732132015-06-08 20:43:07 +0200537 incl PER_CPU_VAR(irq_count)
538 cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
Andy Lutomirskia586f982015-07-03 12:44:30 -0700539 pushq %rdi
Denys Vlasenkof6f64682015-01-08 17:25:15 +0100540 /* We entered an interrupt context - irqs are off: */
541 TRACE_IRQS_OFF
542
Andy Lutomirskia586f982015-07-03 12:44:30 -0700543 call \func /* rdi points to pt_regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 .endm
545
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100546 /*
547 * The interrupt stubs push (~vector+0x80) onto the stack and
548 * then jump to common_interrupt.
549 */
H. Peter Anvin939b7872008-11-11 13:51:52 -0800550 .p2align CONFIG_X86_L1_CACHE_SHIFT
551common_interrupt:
Jan Beulichee4eb872012-11-02 11:18:39 +0000552 ASM_CLAC
Ingo Molnar4d732132015-06-08 20:43:07 +0200553 addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 interrupt do_IRQ
Denys Vlasenko34061f12015-03-23 14:03:59 +0100555 /* 0(%rsp): old RSP */
Jan Beulich7effaa82005-09-12 18:49:24 +0200556ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100557 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700558 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +0200559 decl PER_CPU_VAR(irq_count)
Frederic Weisbecker625dbc32011-01-06 15:22:47 +0100560
Frederic Weisbeckera2bbe752011-07-02 16:52:45 +0200561 /* Restore saved previous stack */
Andy Lutomirskiff467592015-07-03 12:44:29 -0700562 popq %rsp
Frederic Weisbecker625dbc32011-01-06 15:22:47 +0100563
Denys Vlasenko03335e92015-04-27 15:21:52 +0200564 testb $3, CS(%rsp)
Denys Vlasenkodde74f22015-04-27 15:21:51 +0200565 jz retint_kernel
Andy Lutomirski02bc7762015-07-03 12:44:31 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Interrupt came from user space */
Andy Lutomirski02bc7762015-07-03 12:44:31 -0700568GLOBAL(retint_user)
569 mov %rsp,%rdi
570 call prepare_exit_to_usermode
Ingo Molnar2601e642006-07-03 00:24:45 -0700571 TRACE_IRQS_IRETQ
Richard Fellner13be4482017-05-04 14:26:50 +0200572 SWITCH_USER_CR3
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100573 SWAPGS
Andy Lutomirskiff467592015-07-03 12:44:29 -0700574 jmp restore_regs_and_iret
Ingo Molnar2601e642006-07-03 00:24:45 -0700575
Denys Vlasenko627276c2015-03-30 20:09:31 +0200576/* Returning to kernel space */
Denys Vlasenko6ba71b72015-03-31 19:00:05 +0200577retint_kernel:
Denys Vlasenko627276c2015-03-30 20:09:31 +0200578#ifdef CONFIG_PREEMPT
579 /* Interrupts are off */
580 /* Check if we need preemption */
Ingo Molnar4d732132015-06-08 20:43:07 +0200581 bt $9, EFLAGS(%rsp) /* were interrupts off? */
Denys Vlasenko6ba71b72015-03-31 19:00:05 +0200582 jnc 1f
Ingo Molnar4d732132015-06-08 20:43:07 +02005830: cmpl $0, PER_CPU_VAR(__preempt_count)
Denys Vlasenko36acef22015-03-31 19:00:07 +0200584 jnz 1f
Denys Vlasenko627276c2015-03-30 20:09:31 +0200585 call preempt_schedule_irq
Denys Vlasenko36acef22015-03-31 19:00:07 +0200586 jmp 0b
Denys Vlasenko6ba71b72015-03-31 19:00:05 +02005871:
Denys Vlasenko627276c2015-03-30 20:09:31 +0200588#endif
Ingo Molnar2601e642006-07-03 00:24:45 -0700589 /*
590 * The iretq could re-enable interrupts:
591 */
592 TRACE_IRQS_IRETQ
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200593
594/*
595 * At this label, code paths which return to kernel and to user,
596 * which come from interrupts/exception and from syscalls, merge.
597 */
Andy Lutomirskiee08c6b2015-10-05 17:48:09 -0700598GLOBAL(restore_regs_and_iret)
Andy Lutomirskiff467592015-07-03 12:44:29 -0700599 RESTORE_EXTRA_REGS
Denys Vlasenkofffbb5d2015-04-02 18:46:59 +0200600restore_c_regs_and_iret:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800601 RESTORE_C_REGS
602 REMOVE_PT_GPREGS_FROM_STACK 8
Andy Lutomirski7209a752014-07-23 08:34:11 -0700603 INTERRUPT_RETURN
604
605ENTRY(native_iret)
H. Peter Anvin3891a042014-04-29 16:46:09 -0700606 /*
607 * Are we returning to a stack segment from the LDT? Note: in
608 * 64-bit mode SS:RSP on the exception stack is always valid.
609 */
H. Peter Anvin34273f42014-05-04 10:36:22 -0700610#ifdef CONFIG_X86_ESPFIX64
Ingo Molnar4d732132015-06-08 20:43:07 +0200611 testb $4, (SS-RIP)(%rsp)
612 jnz native_irq_return_ldt
H. Peter Anvin34273f42014-05-04 10:36:22 -0700613#endif
H. Peter Anvin3891a042014-04-29 16:46:09 -0700614
Andy Lutomirskiaf726f22014-11-22 18:00:31 -0800615.global native_irq_return_iret
Andy Lutomirski7209a752014-07-23 08:34:11 -0700616native_irq_return_iret:
Andy Lutomirskib645af22014-11-22 18:00:33 -0800617 /*
618 * This may fault. Non-paranoid faults on return to userspace are
619 * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
620 * Double-faults due to espfix64 are handled in do_double_fault.
621 * Other faults here are fatal.
622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 iretq
Ingo Molnar3701d8632008-02-09 23:24:08 +0100624
H. Peter Anvin34273f42014-05-04 10:36:22 -0700625#ifdef CONFIG_X86_ESPFIX64
Andy Lutomirski7209a752014-07-23 08:34:11 -0700626native_irq_return_ldt:
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700627 /*
628 * We are running with user GSBASE. All GPRs contain their user
629 * values. We have a percpu ESPFIX stack that is eight slots
630 * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom
631 * of the ESPFIX stack.
632 *
633 * We clobber RAX and RDI in this code. We stash RDI on the
634 * normal stack and RAX on the ESPFIX stack.
635 *
636 * The ESPFIX stack layout we set up looks like this:
637 *
638 * --- top of ESPFIX stack ---
639 * SS
640 * RSP
641 * RFLAGS
642 * CS
643 * RIP <-- RSP points here when we're done
644 * RAX <-- espfix_waddr points here
645 * --- bottom of ESPFIX stack ---
646 */
647
648 pushq %rdi /* Stash user RDI */
H. Peter Anvin3891a042014-04-29 16:46:09 -0700649 SWAPGS
Richard Fellner13be4482017-05-04 14:26:50 +0200650 SWITCH_KERNEL_CR3
Ingo Molnar4d732132015-06-08 20:43:07 +0200651 movq PER_CPU_VAR(espfix_waddr), %rdi
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700652 movq %rax, (0*8)(%rdi) /* user RAX */
653 movq (1*8)(%rsp), %rax /* user RIP */
Ingo Molnar4d732132015-06-08 20:43:07 +0200654 movq %rax, (1*8)(%rdi)
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700655 movq (2*8)(%rsp), %rax /* user CS */
Ingo Molnar4d732132015-06-08 20:43:07 +0200656 movq %rax, (2*8)(%rdi)
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700657 movq (3*8)(%rsp), %rax /* user RFLAGS */
Ingo Molnar4d732132015-06-08 20:43:07 +0200658 movq %rax, (3*8)(%rdi)
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700659 movq (5*8)(%rsp), %rax /* user SS */
Ingo Molnar4d732132015-06-08 20:43:07 +0200660 movq %rax, (5*8)(%rdi)
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700661 movq (4*8)(%rsp), %rax /* user RSP */
Ingo Molnar4d732132015-06-08 20:43:07 +0200662 movq %rax, (4*8)(%rdi)
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700663 /* Now RAX == RSP. */
664
665 andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */
666 popq %rdi /* Restore user RDI */
667
668 /*
669 * espfix_stack[31:16] == 0. The page tables are set up such that
670 * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
671 * espfix_waddr for any X. That is, there are 65536 RO aliases of
672 * the same page. Set up RSP so that RSP[31:16] contains the
673 * respective 16 bits of the /userspace/ RSP and RSP nonetheless
674 * still points to an RO alias of the ESPFIX stack.
675 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200676 orq PER_CPU_VAR(espfix_stack), %rax
Richard Fellner13be4482017-05-04 14:26:50 +0200677 SWITCH_USER_CR3
H. Peter Anvin3891a042014-04-29 16:46:09 -0700678 SWAPGS
Ingo Molnar4d732132015-06-08 20:43:07 +0200679 movq %rax, %rsp
Andy Lutomirski85063fa2016-09-12 15:05:51 -0700680
681 /*
682 * At this point, we cannot write to the stack any more, but we can
683 * still read.
684 */
685 popq %rax /* Restore user RAX */
686
687 /*
688 * RSP now points to an ordinary IRET frame, except that the page
689 * is read-only and RSP[31:16] are preloaded with the userspace
690 * values. We can now IRET back to userspace.
691 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200692 jmp native_irq_return_iret
H. Peter Anvin34273f42014-05-04 10:36:22 -0700693#endif
Jan Beulich4b787e02006-06-26 13:56:55 +0200694END(common_interrupt)
H. Peter Anvin3891a042014-04-29 16:46:09 -0700695
Masami Hiramatsu8222d712009-08-27 13:23:25 -0400696/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * APIC interrupts.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100698 */
Seiji Aguchicf910e82013-06-20 11:46:53 -0400699.macro apicinterrupt3 num sym do_sym
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100700ENTRY(\sym)
Jan Beulichee4eb872012-11-02 11:18:39 +0000701 ASM_CLAC
Ingo Molnar4d732132015-06-08 20:43:07 +0200702 pushq $~(\num)
Jan Beulich39e95432011-11-29 11:03:46 +0000703.Lcommon_\sym:
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100704 interrupt \do_sym
Ingo Molnar4d732132015-06-08 20:43:07 +0200705 jmp ret_from_intr
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100706END(\sym)
707.endm
Jacob Shin89b831e2005-11-05 17:25:53 +0100708
Seiji Aguchicf910e82013-06-20 11:46:53 -0400709#ifdef CONFIG_TRACING
710#define trace(sym) trace_##sym
711#define smp_trace(sym) smp_trace_##sym
712
713.macro trace_apicinterrupt num sym
714apicinterrupt3 \num trace(\sym) smp_trace(\sym)
715.endm
716#else
717.macro trace_apicinterrupt num sym do_sym
718.endm
719#endif
720
Alexander Potapenko469f0022016-07-15 11:42:43 +0200721/* Make sure APIC interrupt handlers end up in the irqentry section: */
722#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
723# define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
724# define POP_SECTION_IRQENTRY .popsection
725#else
726# define PUSH_SECTION_IRQENTRY
727# define POP_SECTION_IRQENTRY
728#endif
729
Seiji Aguchicf910e82013-06-20 11:46:53 -0400730.macro apicinterrupt num sym do_sym
Alexander Potapenko469f0022016-07-15 11:42:43 +0200731PUSH_SECTION_IRQENTRY
Seiji Aguchicf910e82013-06-20 11:46:53 -0400732apicinterrupt3 \num \sym \do_sym
733trace_apicinterrupt \num \sym
Alexander Potapenko469f0022016-07-15 11:42:43 +0200734POP_SECTION_IRQENTRY
Seiji Aguchicf910e82013-06-20 11:46:53 -0400735.endm
736
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100737#ifdef CONFIG_SMP
Ingo Molnar4d732132015-06-08 20:43:07 +0200738apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
739apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740#endif
741
Nick Piggin03b48632009-01-20 04:36:04 +0100742#ifdef CONFIG_X86_UV
Ingo Molnar4d732132015-06-08 20:43:07 +0200743apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
Nick Piggin03b48632009-01-20 04:36:04 +0100744#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200745
746apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
747apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Yang Zhangd78f2662013-04-11 19:25:11 +0800749#ifdef CONFIG_HAVE_KVM
Ingo Molnar4d732132015-06-08 20:43:07 +0200750apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
751apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
Yang Zhangd78f2662013-04-11 19:25:11 +0800752#endif
753
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400754#ifdef CONFIG_X86_MCE_THRESHOLD
Ingo Molnar4d732132015-06-08 20:43:07 +0200755apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400756#endif
757
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500758#ifdef CONFIG_X86_MCE_AMD
Ingo Molnar4d732132015-06-08 20:43:07 +0200759apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500760#endif
761
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400762#ifdef CONFIG_X86_THERMAL_VECTOR
Ingo Molnar4d732132015-06-08 20:43:07 +0200763apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
Seiji Aguchi33e5ff62013-06-22 07:33:30 -0400764#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100766#ifdef CONFIG_SMP
Ingo Molnar4d732132015-06-08 20:43:07 +0200767apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
768apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
769apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100770#endif
771
Ingo Molnar4d732132015-06-08 20:43:07 +0200772apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
773apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100774
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800775#ifdef CONFIG_IRQ_WORK
Ingo Molnar4d732132015-06-08 20:43:07 +0200776apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
Ingo Molnar241771e2008-12-03 10:39:53 +0100777#endif
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779/*
780 * Exception entry points.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100781 */
Andy Lutomirski9b476682015-03-05 19:19:07 -0800782#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700783
784.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100785ENTRY(\sym)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700786 /* Sanity check */
787 .if \shift_ist != -1 && \paranoid == 0
788 .error "using shift_ist requires paranoid=1"
789 .endif
790
Jan Beulichee4eb872012-11-02 11:18:39 +0000791 ASM_CLAC
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100792 PARAVIRT_ADJUST_EXCEPTION_FRAME
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700793
794 .ifeq \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200795 pushq $-1 /* ORIG_RAX: no syscall to restart */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700796 .endif
797
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800798 ALLOC_PT_GPREGS_ON_STACK
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700799
800 .if \paranoid
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800801 .if \paranoid == 1
Ingo Molnar4d732132015-06-08 20:43:07 +0200802 testb $3, CS(%rsp) /* If coming from userspace, switch stacks */
803 jnz 1f
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800804 .endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200805 call paranoid_entry
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700806 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200807 call error_entry
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700808 .endif
Denys Vlasenkoebfc4532015-02-26 14:40:34 -0800809 /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700810
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700811 .if \paranoid
Andy Lutomirski577ed452014-05-21 15:07:09 -0700812 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200813 TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
Andy Lutomirski577ed452014-05-21 15:07:09 -0700814 .else
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100815 TRACE_IRQS_OFF
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700816 .endif
Andy Lutomirski577ed452014-05-21 15:07:09 -0700817 .endif
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700818
Ingo Molnar4d732132015-06-08 20:43:07 +0200819 movq %rsp, %rdi /* pt_regs pointer */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700820
821 .if \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200822 movq ORIG_RAX(%rsp), %rsi /* get error code */
823 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700824 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200825 xorl %esi, %esi /* no error code */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700826 .endif
827
Andy Lutomirski577ed452014-05-21 15:07:09 -0700828 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200829 subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700830 .endif
831
Ingo Molnar4d732132015-06-08 20:43:07 +0200832 call \do_sym
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700833
Andy Lutomirski577ed452014-05-21 15:07:09 -0700834 .if \shift_ist != -1
Ingo Molnar4d732132015-06-08 20:43:07 +0200835 addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
Andy Lutomirski577ed452014-05-21 15:07:09 -0700836 .endif
837
Denys Vlasenkoebfc4532015-02-26 14:40:34 -0800838 /* these procedures expect "no swapgs" flag in ebx */
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700839 .if \paranoid
Ingo Molnar4d732132015-06-08 20:43:07 +0200840 jmp paranoid_exit
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700841 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200842 jmp error_exit
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700843 .endif
844
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800845 .if \paranoid == 1
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800846 /*
847 * Paranoid entry from userspace. Switch stacks and treat it
848 * as a normal entry. This means that paranoid handlers
849 * run in real process context if user_mode(regs).
850 */
8511:
Ingo Molnar4d732132015-06-08 20:43:07 +0200852 call error_entry
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800853
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800854
Ingo Molnar4d732132015-06-08 20:43:07 +0200855 movq %rsp, %rdi /* pt_regs pointer */
856 call sync_regs
857 movq %rax, %rsp /* switch stack */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800858
Ingo Molnar4d732132015-06-08 20:43:07 +0200859 movq %rsp, %rdi /* pt_regs pointer */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800860
861 .if \has_error_code
Ingo Molnar4d732132015-06-08 20:43:07 +0200862 movq ORIG_RAX(%rsp), %rsi /* get error code */
863 movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800864 .else
Ingo Molnar4d732132015-06-08 20:43:07 +0200865 xorl %esi, %esi /* no error code */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800866 .endif
867
Ingo Molnar4d732132015-06-08 20:43:07 +0200868 call \do_sym
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800869
Ingo Molnar4d732132015-06-08 20:43:07 +0200870 jmp error_exit /* %ebx: no swapgs flag */
Andy Lutomirski48e08d02014-11-11 12:49:41 -0800871 .endif
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +0100872END(\sym)
Alexander van Heukelum322648d2008-11-23 10:08:28 +0100873.endm
Alexander van Heukelumb8b1d082008-11-21 16:44:28 +0100874
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400875#ifdef CONFIG_TRACING
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700876.macro trace_idtentry sym do_sym has_error_code:req
877idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
878idtentry \sym \do_sym has_error_code=\has_error_code
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400879.endm
880#else
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700881.macro trace_idtentry sym do_sym has_error_code:req
882idtentry \sym \do_sym has_error_code=\has_error_code
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400883.endm
884#endif
885
Ingo Molnar4d732132015-06-08 20:43:07 +0200886idtentry divide_error do_divide_error has_error_code=0
887idtentry overflow do_overflow has_error_code=0
888idtentry bounds do_bounds has_error_code=0
889idtentry invalid_op do_invalid_op has_error_code=0
890idtentry device_not_available do_device_not_available has_error_code=0
891idtentry double_fault do_double_fault has_error_code=1 paranoid=2
892idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
893idtentry invalid_TSS do_invalid_TSS has_error_code=1
894idtentry segment_not_present do_segment_not_present has_error_code=1
895idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
896idtentry coprocessor_error do_coprocessor_error has_error_code=0
897idtentry alignment_check do_alignment_check has_error_code=1
898idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400899
Ingo Molnar2601e642006-07-03 00:24:45 -0700900
Ingo Molnar4d732132015-06-08 20:43:07 +0200901 /*
902 * Reload gs selector with exception handling
903 * edi: new selector
904 */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -0400905ENTRY(native_load_gs_index)
Ingo Molnar131484c2015-05-28 12:21:47 +0200906 pushfq
Jeremy Fitzhardingeb8aa2872009-01-28 14:35:03 -0800907 DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300908 SWAPGS
Borislav Petkov42c748bb2016-04-07 17:31:50 -0700909.Lgs_change:
Ingo Molnar4d732132015-06-08 20:43:07 +0200910 movl %edi, %gs
Borislav Petkov96e5d282016-04-07 17:31:49 -07009112: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100912 SWAPGS
Ingo Molnar131484c2015-05-28 12:21:47 +0200913 popfq
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300914 ret
Alexander van Heukelum6efdcfa2008-11-23 10:15:32 +0100915END(native_load_gs_index)
Al Viro784d5692016-01-11 11:04:34 -0500916EXPORT_SYMBOL(native_load_gs_index)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100917
Borislav Petkov42c748bb2016-04-07 17:31:50 -0700918 _ASM_EXTABLE(.Lgs_change, bad_gs)
Ingo Molnar4d732132015-06-08 20:43:07 +0200919 .section .fixup, "ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* running with kernelgs */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100921bad_gs:
Ingo Molnar4d732132015-06-08 20:43:07 +0200922 SWAPGS /* switch back to user gs */
Andy Lutomirskib038c842016-04-26 12:23:27 -0700923.macro ZAP_GS
924 /* This can't be a string because the preprocessor needs to see it. */
925 movl $__USER_DS, %eax
926 movl %eax, %gs
927.endm
928 ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
Ingo Molnar4d732132015-06-08 20:43:07 +0200929 xorl %eax, %eax
930 movl %eax, %gs
931 jmp 2b
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300932 .previous
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100933
Andi Kleen26995002006-08-02 22:37:28 +0200934/* Call softirq on interrupt stack. Interrupts are off. */
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200935ENTRY(do_softirq_own_stack)
Ingo Molnar4d732132015-06-08 20:43:07 +0200936 pushq %rbp
937 mov %rsp, %rbp
938 incl PER_CPU_VAR(irq_count)
939 cmove PER_CPU_VAR(irq_stack_ptr), %rsp
940 push %rbp /* frame pointer backlink */
941 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +0200942 leaveq
Ingo Molnar4d732132015-06-08 20:43:07 +0200943 decl PER_CPU_VAR(irq_count)
Andi Kleened6b6762005-07-28 21:15:49 -0700944 ret
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200945END(do_softirq_own_stack)
Andi Kleen75154f42007-06-23 02:29:25 +0200946
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700947#ifdef CONFIG_XEN
Andy Lutomirskicb5dd2c2014-05-21 15:07:08 -0700948idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700949
950/*
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300951 * A note on the "critical region" in our callback handler.
952 * We want to avoid stacking callback handlers due to events occurring
953 * during handling of the last event. To do this, we keep events disabled
954 * until we've done all processing. HOWEVER, we must enable events before
955 * popping the stack frame (can't be done atomically) and so it would still
956 * be possible to get enough handler activations to overflow the stack.
957 * Although unlikely, bugs of that kind are hard to track down, so we'd
958 * like to avoid the possibility.
959 * So, on entry to the handler we detect whether we interrupted an
960 * existing activation in its critical region -- if so, we pop the current
961 * activation and restart the handler using the previous one.
962 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200963ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */
964
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300965/*
966 * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
967 * see the correct pointer to the pt_regs
968 */
Ingo Molnar4d732132015-06-08 20:43:07 +0200969 movq %rdi, %rsp /* we don't return, adjust the stack frame */
97011: incl PER_CPU_VAR(irq_count)
971 movq %rsp, %rbp
972 cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
973 pushq %rbp /* frame pointer backlink */
974 call xen_evtchn_do_upcall
975 popq %rsp
976 decl PER_CPU_VAR(irq_count)
David Vrabelfdfd8112015-02-19 15:23:17 +0000977#ifndef CONFIG_PREEMPT
Ingo Molnar4d732132015-06-08 20:43:07 +0200978 call xen_maybe_preempt_hcall
David Vrabelfdfd8112015-02-19 15:23:17 +0000979#endif
Ingo Molnar4d732132015-06-08 20:43:07 +0200980 jmp error_exit
Alexander van Heukelum371c3942011-03-11 21:59:38 +0100981END(xen_do_hypervisor_callback)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700982
983/*
Cyrill Gorcunov9f1e87e2008-11-27 21:10:08 +0300984 * Hypervisor uses this for application faults while it executes.
985 * We get here for two reasons:
986 * 1. Fault while reloading DS, ES, FS or GS
987 * 2. Fault while executing IRET
988 * Category 1 we do not need to fix up as Xen has already reloaded all segment
989 * registers that could be reloaded and zeroed the others.
990 * Category 2 we fix up by killing the current process. We cannot use the
991 * normal Linux return path in this case because if we use the IRET hypercall
992 * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
993 * We distinguish between categories by comparing each saved segment register
994 * with its current contents: any discrepancy means we in category 1.
995 */
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700996ENTRY(xen_failsafe_callback)
Ingo Molnar4d732132015-06-08 20:43:07 +0200997 movl %ds, %ecx
998 cmpw %cx, 0x10(%rsp)
999 jne 1f
1000 movl %es, %ecx
1001 cmpw %cx, 0x18(%rsp)
1002 jne 1f
1003 movl %fs, %ecx
1004 cmpw %cx, 0x20(%rsp)
1005 jne 1f
1006 movl %gs, %ecx
1007 cmpw %cx, 0x28(%rsp)
1008 jne 1f
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001009 /* All segments match their saved values => Category 2 (Bad IRET). */
Ingo Molnar4d732132015-06-08 20:43:07 +02001010 movq (%rsp), %rcx
1011 movq 8(%rsp), %r11
1012 addq $0x30, %rsp
1013 pushq $0 /* RIP */
1014 pushq %r11
1015 pushq %rcx
1016 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -070010171: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
Ingo Molnar4d732132015-06-08 20:43:07 +02001018 movq (%rsp), %rcx
1019 movq 8(%rsp), %r11
1020 addq $0x30, %rsp
1021 pushq $-1 /* orig_ax = -1 => not a system call */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001022 ALLOC_PT_GPREGS_ON_STACK
1023 SAVE_C_REGS
1024 SAVE_EXTRA_REGS
Ingo Molnar4d732132015-06-08 20:43:07 +02001025 jmp error_exit
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001026END(xen_failsafe_callback)
1027
Seiji Aguchicf910e82013-06-20 11:46:53 -04001028apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
Sheng Yang38e20b02010-05-14 12:40:51 +01001029 xen_hvm_callback_vector xen_evtchn_do_upcall
1030
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001031#endif /* CONFIG_XEN */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001032
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001033#if IS_ENABLED(CONFIG_HYPERV)
Seiji Aguchicf910e82013-06-20 11:46:53 -04001034apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001035 hyperv_callback_vector hyperv_vector_handler
1036#endif /* CONFIG_HYPERV */
1037
Ingo Molnar4d732132015-06-08 20:43:07 +02001038idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1039idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1040idtentry stack_segment do_stack_segment has_error_code=1
1041
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -07001042#ifdef CONFIG_XEN
Ingo Molnar4d732132015-06-08 20:43:07 +02001043idtentry xen_debug do_debug has_error_code=0
1044idtentry xen_int3 do_int3 has_error_code=0
1045idtentry xen_stack_segment do_stack_segment has_error_code=1
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -07001046#endif
Ingo Molnar4d732132015-06-08 20:43:07 +02001047
1048idtentry general_protection do_general_protection has_error_code=1
1049trace_idtentry page_fault do_page_fault has_error_code=1
1050
Gleb Natapov631bc482010-10-14 11:22:52 +02001051#ifdef CONFIG_KVM_GUEST
Ingo Molnar4d732132015-06-08 20:43:07 +02001052idtentry async_page_fault do_async_page_fault has_error_code=1
Gleb Natapov631bc482010-10-14 11:22:52 +02001053#endif
Ingo Molnar4d732132015-06-08 20:43:07 +02001054
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001055#ifdef CONFIG_X86_MCE
Ingo Molnar4d732132015-06-08 20:43:07 +02001056idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001057#endif
1058
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001059/*
1060 * Save all registers in pt_regs, and switch gs if needed.
1061 * Use slow, but surefire "are we in kernel?" check.
Hugh Dickins05ddad12017-09-26 18:43:07 -07001062 *
1063 * Return: ebx=0: needs swapgs but not SWITCH_USER_CR3 in paranoid_exit
1064 * ebx=1: needs neither swapgs nor SWITCH_USER_CR3 in paranoid_exit
1065 * ebx=2: needs both swapgs and SWITCH_USER_CR3 in paranoid_exit
1066 * ebx=3: needs SWITCH_USER_CR3 but not swapgs in paranoid_exit
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001067 */
1068ENTRY(paranoid_entry)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001069 cld
1070 SAVE_C_REGS 8
1071 SAVE_EXTRA_REGS 8
Ingo Molnar4d732132015-06-08 20:43:07 +02001072 movl $1, %ebx
1073 movl $MSR_GS_BASE, %ecx
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001074 rdmsr
Ingo Molnar4d732132015-06-08 20:43:07 +02001075 testl %edx, %edx
1076 js 1f /* negative -> in kernel */
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001077 SWAPGS
Ingo Molnar4d732132015-06-08 20:43:07 +02001078 xorl %ebx, %ebx
Hugh Dickins05ddad12017-09-26 18:43:07 -070010791:
Kees Cooke71fac02018-01-03 10:17:35 -08001080#ifdef CONFIG_PAGE_TABLE_ISOLATION
Hugh Dickins05ddad12017-09-26 18:43:07 -07001081 /*
1082 * We might have come in between a swapgs and a SWITCH_KERNEL_CR3
1083 * on entry, or between a SWITCH_USER_CR3 and a swapgs on exit.
1084 * Do a conditional SWITCH_KERNEL_CR3: this could safely be done
1085 * unconditionally, but we need to find out whether the reverse
1086 * should be done on return (conveyed to paranoid_exit in %ebx).
1087 */
Hugh Dickins23e09432017-09-24 16:59:49 -07001088 ALTERNATIVE "jmp 2f", "movq %cr3, %rax", X86_FEATURE_KAISER
Hugh Dickins05ddad12017-09-26 18:43:07 -07001089 testl $KAISER_SHADOW_PGD_OFFSET, %eax
1090 jz 2f
1091 orl $2, %ebx
1092 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax
Hugh Dickins169b3692017-10-03 20:49:04 -07001093 /* If PCID enabled, set X86_CR3_PCID_NOFLUSH_BIT */
1094 ALTERNATIVE "", "bts $63, %rax", X86_FEATURE_PCID
Hugh Dickins05ddad12017-09-26 18:43:07 -07001095 movq %rax, %cr3
10962:
1097#endif
1098 ret
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001099END(paranoid_entry)
Denys Vlasenko1eeb2072015-02-26 14:40:33 -08001100
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001101/*
1102 * "Paranoid" exit path from exception stack. This is invoked
1103 * only on return from non-NMI IST interrupts that came
1104 * from kernel space.
1105 *
1106 * We may be returning to very strange contexts (e.g. very early
1107 * in syscall entry), so checking for preemption here would
1108 * be complicated. Fortunately, we there's no good reason
1109 * to try to handle preemption here.
Ingo Molnar4d732132015-06-08 20:43:07 +02001110 *
Hugh Dickins05ddad12017-09-26 18:43:07 -07001111 * On entry: ebx=0: needs swapgs but not SWITCH_USER_CR3
1112 * ebx=1: needs neither swapgs nor SWITCH_USER_CR3
1113 * ebx=2: needs both swapgs and SWITCH_USER_CR3
1114 * ebx=3: needs SWITCH_USER_CR3 but not swapgs
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001115 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001116ENTRY(paranoid_exit)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001117 DISABLE_INTERRUPTS(CLBR_NONE)
Steven Rostedt5963e312012-05-30 11:54:53 -04001118 TRACE_IRQS_OFF_DEBUG
Denys Vlasenkof2db9382015-02-26 14:40:30 -08001119 TRACE_IRQS_IRETQ_DEBUG
Kees Cooke71fac02018-01-03 10:17:35 -08001120#ifdef CONFIG_PAGE_TABLE_ISOLATION
Hugh Dickins23e09432017-09-24 16:59:49 -07001121 /* No ALTERNATIVE for X86_FEATURE_KAISER: paranoid_entry sets %ebx */
Hugh Dickins05ddad12017-09-26 18:43:07 -07001122 testl $2, %ebx /* SWITCH_USER_CR3 needed? */
1123 jz paranoid_exit_no_switch
1124 SWITCH_USER_CR3
1125paranoid_exit_no_switch:
1126#endif
1127 testl $1, %ebx /* swapgs needed? */
1128 jnz paranoid_exit_no_swapgs
1129 SWAPGS_UNSAFE_STACK
1130paranoid_exit_no_swapgs:
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001131 RESTORE_EXTRA_REGS
1132 RESTORE_C_REGS
1133 REMOVE_PT_GPREGS_FROM_STACK 8
Andy Lutomirski48e08d02014-11-11 12:49:41 -08001134 INTERRUPT_RETURN
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001135END(paranoid_exit)
1136
1137/*
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001138 * Save all registers in pt_regs, and switch gs if needed.
Andy Lutomirski539f5112015-06-09 12:36:01 -07001139 * Return: EBX=0: came from user mode; EBX=1: otherwise
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001140 */
1141ENTRY(error_entry)
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001142 cld
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001143 SAVE_C_REGS 8
1144 SAVE_EXTRA_REGS 8
Dave Hansen8f0baad2017-08-30 16:23:00 -07001145 /*
1146 * error_entry() always returns with a kernel gsbase and
1147 * CR3. We must also have a kernel CR3/gsbase before
1148 * calling TRACE_IRQS_*. Just unconditionally switch to
1149 * the kernel CR3 here.
1150 */
1151 SWITCH_KERNEL_CR3
Ingo Molnar4d732132015-06-08 20:43:07 +02001152 xorl %ebx, %ebx
Denys Vlasenko03335e92015-04-27 15:21:52 +02001153 testb $3, CS+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001154 jz .Lerror_kernelspace
Andy Lutomirski539f5112015-06-09 12:36:01 -07001155
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001156 /*
1157 * We entered from user mode or we're pretending to have entered
1158 * from user mode due to an IRET fault.
1159 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001160 SWAPGS
Andy Lutomirski539f5112015-06-09 12:36:01 -07001161
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001162.Lerror_entry_from_usermode_after_swapgs:
Andy Lutomirskif1075052015-11-12 12:59:00 -08001163 /*
1164 * We need to tell lockdep that IRQs are off. We can't do this until
1165 * we fix gsbase, and we should do it before enter_from_user_mode
1166 * (which can take locks).
1167 */
1168 TRACE_IRQS_OFF
Andy Lutomirski478dc892015-11-12 12:59:04 -08001169 CALL_enter_from_user_mode
Andy Lutomirskif1075052015-11-12 12:59:00 -08001170 ret
Andy Lutomirski02bc7762015-07-03 12:44:31 -07001171
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001172.Lerror_entry_done:
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001173 TRACE_IRQS_OFF
1174 ret
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001175
Denys Vlasenkoebfc4532015-02-26 14:40:34 -08001176 /*
1177 * There are two places in the kernel that can potentially fault with
1178 * usergs. Handle them here. B stepping K8s sometimes report a
1179 * truncated RIP for IRET exceptions returning to compat mode. Check
1180 * for these here too.
1181 */
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001182.Lerror_kernelspace:
Ingo Molnar4d732132015-06-08 20:43:07 +02001183 incl %ebx
1184 leaq native_irq_return_iret(%rip), %rcx
1185 cmpq %rcx, RIP+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001186 je .Lerror_bad_iret
Ingo Molnar4d732132015-06-08 20:43:07 +02001187 movl %ecx, %eax /* zero extend */
1188 cmpq %rax, RIP+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001189 je .Lbstep_iret
Borislav Petkov42c748bb2016-04-07 17:31:50 -07001190 cmpq $.Lgs_change, RIP+8(%rsp)
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001191 jne .Lerror_entry_done
Andy Lutomirski539f5112015-06-09 12:36:01 -07001192
1193 /*
Borislav Petkov42c748bb2016-04-07 17:31:50 -07001194 * hack: .Lgs_change can fail with user gsbase. If this happens, fix up
Andy Lutomirski539f5112015-06-09 12:36:01 -07001195 * gsbase and proceed. We'll fix up the exception and land in
Borislav Petkov42c748bb2016-04-07 17:31:50 -07001196 * .Lgs_change's error handler with kernel gsbase.
Andy Lutomirski539f5112015-06-09 12:36:01 -07001197 */
Wanpeng Li2fa5f042016-09-30 09:01:06 +08001198 SWAPGS
1199 jmp .Lerror_entry_done
Brian Gerstae24ffe2009-10-12 10:18:23 -04001200
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001201.Lbstep_iret:
Brian Gerstae24ffe2009-10-12 10:18:23 -04001202 /* Fix truncated RIP */
Ingo Molnar4d732132015-06-08 20:43:07 +02001203 movq %rcx, RIP+8(%rsp)
Andy Lutomirskib645af22014-11-22 18:00:33 -08001204 /* fall through */
1205
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001206.Lerror_bad_iret:
Andy Lutomirski539f5112015-06-09 12:36:01 -07001207 /*
1208 * We came from an IRET to user mode, so we have user gsbase.
1209 * Switch to kernel gsbase:
1210 */
Andy Lutomirskib645af22014-11-22 18:00:33 -08001211 SWAPGS
Andy Lutomirski539f5112015-06-09 12:36:01 -07001212
1213 /*
1214 * Pretend that the exception came from user mode: set up pt_regs
1215 * as if we faulted immediately after IRET and clear EBX so that
1216 * error_exit knows that we will be returning to user mode.
1217 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001218 mov %rsp, %rdi
1219 call fixup_bad_iret
1220 mov %rax, %rsp
Andy Lutomirski539f5112015-06-09 12:36:01 -07001221 decl %ebx
Andy Lutomirskicb6f64e2015-07-03 12:44:27 -07001222 jmp .Lerror_entry_from_usermode_after_swapgs
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001223END(error_entry)
1224
1225
Andy Lutomirski539f5112015-06-09 12:36:01 -07001226/*
Nicolas Iooss75ca5b22016-07-29 13:39:51 +02001227 * On entry, EBX is a "return to kernel mode" flag:
Andy Lutomirski539f5112015-06-09 12:36:01 -07001228 * 1: already in kernel mode, don't need SWAPGS
1229 * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
1230 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001231ENTRY(error_exit)
Ingo Molnar4d732132015-06-08 20:43:07 +02001232 movl %ebx, %eax
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001233 DISABLE_INTERRUPTS(CLBR_NONE)
1234 TRACE_IRQS_OFF
Ingo Molnar4d732132015-06-08 20:43:07 +02001235 testl %eax, %eax
1236 jnz retint_kernel
1237 jmp retint_user
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001238END(error_exit)
1239
Denys Vlasenko0784b362015-04-01 16:50:57 +02001240/* Runs on exception stack */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001241ENTRY(nmi)
Andy Lutomirskifc57a7c2015-09-20 16:32:04 -07001242 /*
1243 * Fix up the exception frame if we're on Xen.
1244 * PARAVIRT_ADJUST_EXCEPTION_FRAME is guaranteed to push at most
1245 * one value to the stack on native, so it may clobber the rdx
1246 * scratch slot, but it won't clobber any of the important
1247 * slots past it.
1248 *
1249 * Xen is a different story, because the Xen frame itself overlaps
1250 * the "NMI executing" variable.
1251 */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001252 PARAVIRT_ADJUST_EXCEPTION_FRAME
Andy Lutomirskifc57a7c2015-09-20 16:32:04 -07001253
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001254 /*
1255 * We allow breakpoints in NMIs. If a breakpoint occurs, then
1256 * the iretq it performs will take us out of NMI context.
1257 * This means that we can have nested NMIs where the next
1258 * NMI is using the top of the stack of the previous NMI. We
1259 * can't let it execute because the nested NMI will corrupt the
1260 * stack of the previous NMI. NMI handlers are not re-entrant
1261 * anyway.
1262 *
1263 * To handle this case we do the following:
1264 * Check the a special location on the stack that contains
1265 * a variable that is set when NMIs are executing.
1266 * The interrupted task's stack is also checked to see if it
1267 * is an NMI stack.
1268 * If the variable is not set and the stack is not the NMI
1269 * stack then:
1270 * o Set the special variable on the stack
Andy Lutomirski0b229302015-07-15 10:29:36 -07001271 * o Copy the interrupt frame into an "outermost" location on the
1272 * stack
1273 * o Copy the interrupt frame into an "iret" location on the stack
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001274 * o Continue processing the NMI
1275 * If the variable is set or the previous stack is the NMI stack:
Andy Lutomirski0b229302015-07-15 10:29:36 -07001276 * o Modify the "iret" location to jump to the repeat_nmi
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001277 * o return back to the first NMI
1278 *
1279 * Now on exit of the first NMI, we first clear the stack variable
1280 * The NMI stack will tell any nested NMIs at that point that it is
1281 * nested. Then we pop the stack normally with iret, and if there was
1282 * a nested NMI that updated the copy interrupt stack frame, a
1283 * jump will be made to the repeat_nmi code that will handle the second
1284 * NMI.
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001285 *
1286 * However, espfix prevents us from directly returning to userspace
1287 * with a single IRET instruction. Similarly, IRET to user mode
1288 * can fault. We therefore handle NMIs from user space like
1289 * other IST entries.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001290 */
1291
Andy Lutomirski10d5bf22017-08-07 19:43:13 -07001292 ASM_CLAC
1293
Denys Vlasenko146b2b02015-03-25 18:18:13 +01001294 /* Use %rdx as our temp variable throughout */
Ingo Molnar4d732132015-06-08 20:43:07 +02001295 pushq %rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001296
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001297 testb $3, CS-RIP+8(%rsp)
1298 jz .Lnmi_from_kernel
Steven Rostedt45d5a162012-02-19 16:43:37 -05001299
1300 /*
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001301 * NMI from user mode. We need to run on the thread stack, but we
1302 * can't go through the normal entry paths: NMIs are masked, and
1303 * we don't want to enable interrupts, because then we'll end
1304 * up in an awkward situation in which IRQs are on but NMIs
1305 * are off.
Andy Lutomirski83c133c2015-09-20 16:32:05 -07001306 *
1307 * We also must not push anything to the stack before switching
1308 * stacks lest we corrupt the "NMI executing" variable.
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001309 */
1310
Andy Lutomirski83c133c2015-09-20 16:32:05 -07001311 SWAPGS_UNSAFE_STACK
Dave Hansen8f0baad2017-08-30 16:23:00 -07001312 /*
1313 * percpu variables are mapped with user CR3, so no need
1314 * to switch CR3 here.
1315 */
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001316 cld
1317 movq %rsp, %rdx
1318 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
1319 pushq 5*8(%rdx) /* pt_regs->ss */
1320 pushq 4*8(%rdx) /* pt_regs->rsp */
1321 pushq 3*8(%rdx) /* pt_regs->flags */
1322 pushq 2*8(%rdx) /* pt_regs->cs */
1323 pushq 1*8(%rdx) /* pt_regs->rip */
1324 pushq $-1 /* pt_regs->orig_ax */
1325 pushq %rdi /* pt_regs->di */
1326 pushq %rsi /* pt_regs->si */
1327 pushq (%rdx) /* pt_regs->dx */
1328 pushq %rcx /* pt_regs->cx */
1329 pushq %rax /* pt_regs->ax */
1330 pushq %r8 /* pt_regs->r8 */
1331 pushq %r9 /* pt_regs->r9 */
1332 pushq %r10 /* pt_regs->r10 */
1333 pushq %r11 /* pt_regs->r11 */
1334 pushq %rbx /* pt_regs->rbx */
1335 pushq %rbp /* pt_regs->rbp */
1336 pushq %r12 /* pt_regs->r12 */
1337 pushq %r13 /* pt_regs->r13 */
1338 pushq %r14 /* pt_regs->r14 */
1339 pushq %r15 /* pt_regs->r15 */
1340
1341 /*
1342 * At this point we no longer need to worry about stack damage
1343 * due to nesting -- we're on the normal thread stack and we're
1344 * done with the NMI stack.
1345 */
1346
1347 movq %rsp, %rdi
1348 movq $-1, %rsi
Kees Cooke71fac02018-01-03 10:17:35 -08001349#ifdef CONFIG_PAGE_TABLE_ISOLATION
Dave Hansen8f0baad2017-08-30 16:23:00 -07001350 /* Unconditionally use kernel CR3 for do_nmi() */
1351 /* %rax is saved above, so OK to clobber here */
Hugh Dickins23e09432017-09-24 16:59:49 -07001352 ALTERNATIVE "jmp 2f", "movq %cr3, %rax", X86_FEATURE_KAISER
Hugh Dickinsd0142ce2017-08-27 16:24:27 -07001353 /* If PCID enabled, NOFLUSH now and NOFLUSH on return */
Hugh Dickins169b3692017-10-03 20:49:04 -07001354 ALTERNATIVE "", "bts $63, %rax", X86_FEATURE_PCID
Dave Hansen8f0baad2017-08-30 16:23:00 -07001355 pushq %rax
Hugh Dickins2684b122017-08-30 16:23:00 -07001356 /* mask off "user" bit of pgd address and 12 PCID bits: */
1357 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax
Dave Hansen8f0baad2017-08-30 16:23:00 -07001358 movq %rax, %cr3
Hugh Dickins23e09432017-09-24 16:59:49 -070013592:
Dave Hansen8f0baad2017-08-30 16:23:00 -07001360#endif
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001361 call do_nmi
Hugh Dickins19377942017-09-21 20:39:56 -07001362
Kees Cooke71fac02018-01-03 10:17:35 -08001363#ifdef CONFIG_PAGE_TABLE_ISOLATION
Dave Hansen8f0baad2017-08-30 16:23:00 -07001364 /*
1365 * Unconditionally restore CR3. I know we return to
1366 * kernel code that needs user CR3, but do we ever return
1367 * to "user mode" where we need the kernel CR3?
1368 */
Hugh Dickins23e09432017-09-24 16:59:49 -07001369 ALTERNATIVE "", "popq %rax; movq %rax, %cr3", X86_FEATURE_KAISER
Dave Hansen8f0baad2017-08-30 16:23:00 -07001370#endif
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001371
1372 /*
1373 * Return back to user mode. We must *not* do the normal exit
Dave Hansen8f0baad2017-08-30 16:23:00 -07001374 * work, because we don't want to enable interrupts. Do not
1375 * switch to user CR3: we might be going back to kernel code
1376 * that had a user CR3 set.
Andy Lutomirski9b6e6a82015-07-15 10:29:35 -07001377 */
1378 SWAPGS
1379 jmp restore_c_regs_and_iret
1380
1381.Lnmi_from_kernel:
1382 /*
Andy Lutomirski0b229302015-07-15 10:29:36 -07001383 * Here's what our stack frame will look like:
1384 * +---------------------------------------------------------+
1385 * | original SS |
1386 * | original Return RSP |
1387 * | original RFLAGS |
1388 * | original CS |
1389 * | original RIP |
1390 * +---------------------------------------------------------+
1391 * | temp storage for rdx |
1392 * +---------------------------------------------------------+
1393 * | "NMI executing" variable |
1394 * +---------------------------------------------------------+
1395 * | iret SS } Copied from "outermost" frame |
1396 * | iret Return RSP } on each loop iteration; overwritten |
1397 * | iret RFLAGS } by a nested NMI to force another |
1398 * | iret CS } iteration if needed. |
1399 * | iret RIP } |
1400 * +---------------------------------------------------------+
1401 * | outermost SS } initialized in first_nmi; |
1402 * | outermost Return RSP } will not be changed before |
1403 * | outermost RFLAGS } NMI processing is done. |
1404 * | outermost CS } Copied to "iret" frame on each |
1405 * | outermost RIP } iteration. |
1406 * +---------------------------------------------------------+
1407 * | pt_regs |
1408 * +---------------------------------------------------------+
1409 *
1410 * The "original" frame is used by hardware. Before re-enabling
1411 * NMIs, we need to be done with it, and we need to leave enough
1412 * space for the asm code here.
1413 *
1414 * We return by executing IRET while RSP points to the "iret" frame.
1415 * That will either return for real or it will loop back into NMI
1416 * processing.
1417 *
1418 * The "outermost" frame is copied to the "iret" frame on each
1419 * iteration of the loop, so each iteration starts with the "iret"
1420 * frame pointing to the final return target.
1421 */
1422
1423 /*
1424 * Determine whether we're a nested NMI.
1425 *
Andy Lutomirskia27507c2015-07-15 10:29:37 -07001426 * If we interrupted kernel code between repeat_nmi and
1427 * end_repeat_nmi, then we are a nested NMI. We must not
1428 * modify the "iret" frame because it's being written by
1429 * the outer NMI. That's okay; the outer NMI handler is
1430 * about to about to call do_nmi anyway, so we can just
1431 * resume the outer NMI.
1432 */
1433
1434 movq $repeat_nmi, %rdx
1435 cmpq 8(%rsp), %rdx
1436 ja 1f
1437 movq $end_repeat_nmi, %rdx
1438 cmpq 8(%rsp), %rdx
1439 ja nested_nmi_out
14401:
1441
1442 /*
1443 * Now check "NMI executing". If it's set, then we're nested.
Andy Lutomirski0b229302015-07-15 10:29:36 -07001444 * This will not detect if we interrupted an outer NMI just
1445 * before IRET.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001446 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001447 cmpl $1, -8(%rsp)
1448 je nested_nmi
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001449
1450 /*
Andy Lutomirski0b229302015-07-15 10:29:36 -07001451 * Now test if the previous stack was an NMI stack. This covers
1452 * the case where we interrupt an outer NMI after it clears
Andy Lutomirski810bc072015-07-15 10:29:38 -07001453 * "NMI executing" but before IRET. We need to be careful, though:
1454 * there is one case in which RSP could point to the NMI stack
1455 * despite there being no NMI active: naughty userspace controls
1456 * RSP at the very beginning of the SYSCALL targets. We can
1457 * pull a fast one on naughty userspace, though: we program
1458 * SYSCALL to mask DF, so userspace cannot cause DF to be set
1459 * if it controls the kernel's RSP. We set DF before we clear
1460 * "NMI executing".
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001461 */
Denys Vlasenko0784b362015-04-01 16:50:57 +02001462 lea 6*8(%rsp), %rdx
1463 /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
1464 cmpq %rdx, 4*8(%rsp)
1465 /* If the stack pointer is above the NMI stack, this is a normal NMI */
1466 ja first_nmi
Ingo Molnar4d732132015-06-08 20:43:07 +02001467
Denys Vlasenko0784b362015-04-01 16:50:57 +02001468 subq $EXCEPTION_STKSZ, %rdx
1469 cmpq %rdx, 4*8(%rsp)
1470 /* If it is below the NMI stack, it is a normal NMI */
1471 jb first_nmi
Andy Lutomirski810bc072015-07-15 10:29:38 -07001472
1473 /* Ah, it is within the NMI stack. */
1474
1475 testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
1476 jz first_nmi /* RSP was user controlled. */
1477
1478 /* This is a nested NMI. */
Denys Vlasenko0784b362015-04-01 16:50:57 +02001479
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001480nested_nmi:
1481 /*
Andy Lutomirski0b229302015-07-15 10:29:36 -07001482 * Modify the "iret" frame to point to repeat_nmi, forcing another
1483 * iteration of NMI handling.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001484 */
Andy Lutomirski23a781e2015-07-15 10:29:39 -07001485 subq $8, %rsp
Ingo Molnar4d732132015-06-08 20:43:07 +02001486 leaq -10*8(%rsp), %rdx
1487 pushq $__KERNEL_DS
1488 pushq %rdx
Ingo Molnar131484c2015-05-28 12:21:47 +02001489 pushfq
Ingo Molnar4d732132015-06-08 20:43:07 +02001490 pushq $__KERNEL_CS
1491 pushq $repeat_nmi
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001492
1493 /* Put stack back */
Ingo Molnar4d732132015-06-08 20:43:07 +02001494 addq $(6*8), %rsp
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001495
1496nested_nmi_out:
Ingo Molnar4d732132015-06-08 20:43:07 +02001497 popq %rdx
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001498
Andy Lutomirski0b229302015-07-15 10:29:36 -07001499 /* We are returning to kernel mode, so this cannot result in a fault. */
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001500 INTERRUPT_RETURN
1501
1502first_nmi:
Andy Lutomirski0b229302015-07-15 10:29:36 -07001503 /* Restore rdx. */
Ingo Molnar4d732132015-06-08 20:43:07 +02001504 movq (%rsp), %rdx
Jan Beulich62610912012-02-24 14:54:37 +00001505
Andy Lutomirski36f1a772015-07-15 10:29:40 -07001506 /* Make room for "NMI executing". */
1507 pushq $0
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001508
Andy Lutomirski0b229302015-07-15 10:29:36 -07001509 /* Leave room for the "iret" frame */
Ingo Molnar4d732132015-06-08 20:43:07 +02001510 subq $(5*8), %rsp
Salman Qazi28696f42012-10-01 17:29:25 -07001511
Andy Lutomirski0b229302015-07-15 10:29:36 -07001512 /* Copy the "original" frame to the "outermost" frame */
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001513 .rept 5
Ingo Molnar4d732132015-06-08 20:43:07 +02001514 pushq 11*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001515 .endr
Jan Beulich62610912012-02-24 14:54:37 +00001516
Steven Rostedt79fb4ad2012-02-24 15:55:13 -05001517 /* Everything up to here is safe from nested NMIs */
1518
Andy Lutomirskia97439a2015-07-15 10:29:41 -07001519#ifdef CONFIG_DEBUG_ENTRY
1520 /*
1521 * For ease of testing, unmask NMIs right away. Disabled by
1522 * default because IRET is very expensive.
1523 */
1524 pushq $0 /* SS */
1525 pushq %rsp /* RSP (minus 8 because of the previous push) */
1526 addq $8, (%rsp) /* Fix up RSP */
1527 pushfq /* RFLAGS */
1528 pushq $__KERNEL_CS /* CS */
1529 pushq $1f /* RIP */
1530 INTERRUPT_RETURN /* continues at repeat_nmi below */
15311:
1532#endif
1533
Andy Lutomirski0b229302015-07-15 10:29:36 -07001534repeat_nmi:
Jan Beulich62610912012-02-24 14:54:37 +00001535 /*
1536 * If there was a nested NMI, the first NMI's iret will return
1537 * here. But NMIs are still enabled and we can take another
1538 * nested NMI. The nested NMI checks the interrupted RIP to see
1539 * if it is between repeat_nmi and end_repeat_nmi, and if so
1540 * it will just return, as we are about to repeat an NMI anyway.
1541 * This makes it safe to copy to the stack frame that a nested
1542 * NMI will update.
Andy Lutomirski0b229302015-07-15 10:29:36 -07001543 *
1544 * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
1545 * we're repeating an NMI, gsbase has the same value that it had on
1546 * the first iteration. paranoid_entry will load the kernel
Andy Lutomirski36f1a772015-07-15 10:29:40 -07001547 * gsbase if needed before we call do_nmi. "NMI executing"
1548 * is zero.
Jan Beulich62610912012-02-24 14:54:37 +00001549 */
Andy Lutomirski36f1a772015-07-15 10:29:40 -07001550 movq $1, 10*8(%rsp) /* Set "NMI executing". */
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001551
Andy Lutomirski0b229302015-07-15 10:29:36 -07001552 /*
1553 * Copy the "outermost" frame to the "iret" frame. NMIs that nest
1554 * here must not modify the "iret" frame while we're writing to
1555 * it or it will end up containing garbage.
1556 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001557 addq $(10*8), %rsp
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001558 .rept 5
Ingo Molnar4d732132015-06-08 20:43:07 +02001559 pushq -6*8(%rsp)
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001560 .endr
Ingo Molnar4d732132015-06-08 20:43:07 +02001561 subq $(5*8), %rsp
Jan Beulich62610912012-02-24 14:54:37 +00001562end_repeat_nmi:
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001563
1564 /*
Andy Lutomirski0b229302015-07-15 10:29:36 -07001565 * Everything below this point can be preempted by a nested NMI.
1566 * If this happens, then the inner NMI will change the "iret"
1567 * frame to point back to repeat_nmi.
Steven Rostedt3f3c8b82011-12-08 12:36:23 -05001568 */
Ingo Molnar4d732132015-06-08 20:43:07 +02001569 pushq $-1 /* ORIG_RAX: no syscall to restart */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001570 ALLOC_PT_GPREGS_ON_STACK
1571
Steven Rostedt1fd466e2011-12-08 12:32:27 -05001572 /*
Dave Hansen8f0baad2017-08-30 16:23:00 -07001573 * Use the same approach as paranoid_entry to handle SWAPGS, but
1574 * without CR3 handling since we do that differently in NMIs. No
1575 * need to use paranoid_exit as we should not be calling schedule
1576 * in NMI context. Even with normal interrupts enabled. An NMI
1577 * should not be setting NEED_RESCHED or anything that normal
1578 * interrupts and exceptions might do.
Steven Rostedt1fd466e2011-12-08 12:32:27 -05001579 */
Dave Hansen8f0baad2017-08-30 16:23:00 -07001580 cld
1581 SAVE_C_REGS
1582 SAVE_EXTRA_REGS
1583 movl $1, %ebx
1584 movl $MSR_GS_BASE, %ecx
1585 rdmsr
1586 testl %edx, %edx
1587 js 1f /* negative -> in kernel */
1588 SWAPGS
1589 xorl %ebx, %ebx
15901:
Hugh Dickins19377942017-09-21 20:39:56 -07001591 movq %rsp, %rdi
1592 movq $-1, %rsi
Kees Cooke71fac02018-01-03 10:17:35 -08001593#ifdef CONFIG_PAGE_TABLE_ISOLATION
Dave Hansen8f0baad2017-08-30 16:23:00 -07001594 /* Unconditionally use kernel CR3 for do_nmi() */
1595 /* %rax is saved above, so OK to clobber here */
Hugh Dickins23e09432017-09-24 16:59:49 -07001596 ALTERNATIVE "jmp 2f", "movq %cr3, %rax", X86_FEATURE_KAISER
Hugh Dickinsd0142ce2017-08-27 16:24:27 -07001597 /* If PCID enabled, NOFLUSH now and NOFLUSH on return */
Hugh Dickins169b3692017-10-03 20:49:04 -07001598 ALTERNATIVE "", "bts $63, %rax", X86_FEATURE_PCID
Dave Hansen8f0baad2017-08-30 16:23:00 -07001599 pushq %rax
Hugh Dickins2684b122017-08-30 16:23:00 -07001600 /* mask off "user" bit of pgd address and 12 PCID bits: */
1601 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax
Dave Hansen8f0baad2017-08-30 16:23:00 -07001602 movq %rax, %cr3
Hugh Dickins23e09432017-09-24 16:59:49 -070016032:
Dave Hansen8f0baad2017-08-30 16:23:00 -07001604#endif
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001605
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001606 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
Ingo Molnar4d732132015-06-08 20:43:07 +02001607 call do_nmi
Hugh Dickins19377942017-09-21 20:39:56 -07001608
Kees Cooke71fac02018-01-03 10:17:35 -08001609#ifdef CONFIG_PAGE_TABLE_ISOLATION
Dave Hansen8f0baad2017-08-30 16:23:00 -07001610 /*
1611 * Unconditionally restore CR3. We might be returning to
1612 * kernel code that needs user CR3, like just just before
1613 * a sysret.
1614 */
Hugh Dickins23e09432017-09-24 16:59:49 -07001615 ALTERNATIVE "", "popq %rax; movq %rax, %cr3", X86_FEATURE_KAISER
Dave Hansen8f0baad2017-08-30 16:23:00 -07001616#endif
Steven Rostedt7fbb98c2012-06-07 10:21:21 -04001617
Ingo Molnar4d732132015-06-08 20:43:07 +02001618 testl %ebx, %ebx /* swapgs needed? */
1619 jnz nmi_restore
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001620nmi_swapgs:
Dave Hansen8f0baad2017-08-30 16:23:00 -07001621 /* We fixed up CR3 above, so no need to switch it here */
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001622 SWAPGS_UNSAFE_STACK
1623nmi_restore:
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001624 RESTORE_EXTRA_REGS
1625 RESTORE_C_REGS
Andy Lutomirski0b229302015-07-15 10:29:36 -07001626
1627 /* Point RSP at the "iret" frame. */
Denys Vlasenko76f5df42015-02-26 14:40:27 -08001628 REMOVE_PT_GPREGS_FROM_STACK 6*8
Salman Qazi28696f42012-10-01 17:29:25 -07001629
Andy Lutomirski810bc072015-07-15 10:29:38 -07001630 /*
1631 * Clear "NMI executing". Set DF first so that we can easily
1632 * distinguish the remaining code between here and IRET from
1633 * the SYSCALL entry and exit paths. On a native kernel, we
1634 * could just inspect RIP, but, on paravirt kernels,
1635 * INTERRUPT_RETURN can translate into a jump into a
1636 * hypercall page.
1637 */
1638 std
1639 movq $0, 5*8(%rsp) /* clear "NMI executing" */
Andy Lutomirski0b229302015-07-15 10:29:36 -07001640
1641 /*
1642 * INTERRUPT_RETURN reads the "iret" frame and exits the NMI
1643 * stack in a single instruction. We are returning to kernel
1644 * mode, so this cannot result in a fault.
1645 */
Andy Lutomirski5ca6f702015-06-04 13:24:29 -07001646 INTERRUPT_RETURN
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001647END(nmi)
1648
1649ENTRY(ignore_sysret)
Ingo Molnar4d732132015-06-08 20:43:07 +02001650 mov $-ENOSYS, %eax
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001651 sysret
Alexander van Heukelumddeb8f22008-11-24 13:24:28 +01001652END(ignore_sysret)
Andy Lutomirski2deb4be2016-07-14 13:22:55 -07001653
1654ENTRY(rewind_stack_do_exit)
1655 /* Prevent any naive code from trying to unwind to our caller. */
1656 xorl %ebp, %ebp
1657
1658 movq PER_CPU_VAR(cpu_current_top_of_stack), %rax
1659 leaq -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%rax), %rsp
1660
1661 call do_exit
16621: jmp 1b
1663END(rewind_stack_do_exit)