]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | /* |
3 | * linux/arch/x86_64/entry.S | |
4 | * | |
5 | * Copyright (C) 1991, 1992 Linus Torvalds | |
6 | * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs | |
7 | * Copyright (C) 2000 Pavel Machek <pavel@suse.cz> | |
4d732138 | 8 | * |
1da177e4 LT |
9 | * entry.S contains the system-call and fault low-level handling routines. |
10 | * | |
8b4777a4 AL |
11 | * Some of this is documented in Documentation/x86/entry_64.txt |
12 | * | |
0bd7b798 | 13 | * A note on terminology: |
4d732138 IM |
14 | * - iret frame: Architecture defined interrupt frame from SS to RIP |
15 | * at the top of the kernel process stack. | |
2e91a17b AK |
16 | * |
17 | * Some macro usage: | |
4d732138 IM |
18 | * - ENTRY/END: Define functions in the symbol table. |
19 | * - TRACE_IRQ_*: Trace hardirq state for lock debugging. | |
20 | * - idtentry: Define exception entry points. | |
1da177e4 | 21 | */ |
1da177e4 LT |
22 | #include <linux/linkage.h> |
23 | #include <asm/segment.h> | |
1da177e4 LT |
24 | #include <asm/cache.h> |
25 | #include <asm/errno.h> | |
d36f9479 | 26 | #include "calling.h" |
e2d5df93 | 27 | #include <asm/asm-offsets.h> |
1da177e4 LT |
28 | #include <asm/msr.h> |
29 | #include <asm/unistd.h> | |
30 | #include <asm/thread_info.h> | |
31 | #include <asm/hw_irq.h> | |
0341c14d | 32 | #include <asm/page_types.h> |
2601e64d | 33 | #include <asm/irqflags.h> |
72fe4858 | 34 | #include <asm/paravirt.h> |
9939ddaf | 35 | #include <asm/percpu.h> |
d7abc0fa | 36 | #include <asm/asm.h> |
63bcff2a | 37 | #include <asm/smap.h> |
3891a04a | 38 | #include <asm/pgtable_types.h> |
784d5699 | 39 | #include <asm/export.h> |
8c1f7558 | 40 | #include <asm/frame.h> |
d7e7528b | 41 | #include <linux/err.h> |
1da177e4 | 42 | |
4d732138 IM |
43 | .code64 |
44 | .section .entry.text, "ax" | |
16444a8a | 45 | |
72fe4858 | 46 | #ifdef CONFIG_PARAVIRT |
2be29982 | 47 | ENTRY(native_usergs_sysret64) |
8c1f7558 | 48 | UNWIND_HINT_EMPTY |
72fe4858 GOC |
49 | swapgs |
50 | sysretq | |
8c1f7558 | 51 | END(native_usergs_sysret64) |
72fe4858 GOC |
52 | #endif /* CONFIG_PARAVIRT */ |
53 | ||
f2db9382 | 54 | .macro TRACE_IRQS_IRETQ |
2601e64d | 55 | #ifdef CONFIG_TRACE_IRQFLAGS |
4d732138 IM |
56 | bt $9, EFLAGS(%rsp) /* interrupts off? */ |
57 | jnc 1f | |
2601e64d IM |
58 | TRACE_IRQS_ON |
59 | 1: | |
60 | #endif | |
61 | .endm | |
62 | ||
5963e317 SR |
63 | /* |
64 | * When dynamic function tracer is enabled it will add a breakpoint | |
65 | * to all locations that it is about to modify, sync CPUs, update | |
66 | * all the code, sync CPUs, then remove the breakpoints. In this time | |
67 | * if lockdep is enabled, it might jump back into the debug handler | |
68 | * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF). | |
69 | * | |
70 | * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to | |
71 | * make sure the stack pointer does not get reset back to the top | |
72 | * of the debug stack, and instead just reuses the current stack. | |
73 | */ | |
74 | #if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS) | |
75 | ||
76 | .macro TRACE_IRQS_OFF_DEBUG | |
4d732138 | 77 | call debug_stack_set_zero |
5963e317 | 78 | TRACE_IRQS_OFF |
4d732138 | 79 | call debug_stack_reset |
5963e317 SR |
80 | .endm |
81 | ||
82 | .macro TRACE_IRQS_ON_DEBUG | |
4d732138 | 83 | call debug_stack_set_zero |
5963e317 | 84 | TRACE_IRQS_ON |
4d732138 | 85 | call debug_stack_reset |
5963e317 SR |
86 | .endm |
87 | ||
f2db9382 | 88 | .macro TRACE_IRQS_IRETQ_DEBUG |
4d732138 IM |
89 | bt $9, EFLAGS(%rsp) /* interrupts off? */ |
90 | jnc 1f | |
5963e317 SR |
91 | TRACE_IRQS_ON_DEBUG |
92 | 1: | |
93 | .endm | |
94 | ||
95 | #else | |
4d732138 IM |
96 | # define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF |
97 | # define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON | |
98 | # define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ | |
5963e317 SR |
99 | #endif |
100 | ||
1da177e4 | 101 | /* |
4d732138 | 102 | * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers. |
1da177e4 | 103 | * |
fda57b22 AL |
104 | * This is the only entry point used for 64-bit system calls. The |
105 | * hardware interface is reasonably well designed and the register to | |
106 | * argument mapping Linux uses fits well with the registers that are | |
107 | * available when SYSCALL is used. | |
108 | * | |
109 | * SYSCALL instructions can be found inlined in libc implementations as | |
110 | * well as some other programs and libraries. There are also a handful | |
111 | * of SYSCALL instructions in the vDSO used, for example, as a | |
112 | * clock_gettimeofday fallback. | |
113 | * | |
4d732138 | 114 | * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11, |
b87cf63e DV |
115 | * then loads new ss, cs, and rip from previously programmed MSRs. |
116 | * rflags gets masked by a value from another MSR (so CLD and CLAC | |
117 | * are not needed). SYSCALL does not save anything on the stack | |
118 | * and does not change rsp. | |
119 | * | |
120 | * Registers on entry: | |
1da177e4 | 121 | * rax system call number |
b87cf63e DV |
122 | * rcx return address |
123 | * r11 saved rflags (note: r11 is callee-clobbered register in C ABI) | |
1da177e4 | 124 | * rdi arg0 |
1da177e4 | 125 | * rsi arg1 |
0bd7b798 | 126 | * rdx arg2 |
b87cf63e | 127 | * r10 arg3 (needs to be moved to rcx to conform to C ABI) |
1da177e4 LT |
128 | * r8 arg4 |
129 | * r9 arg5 | |
4d732138 | 130 | * (note: r12-r15, rbp, rbx are callee-preserved in C ABI) |
0bd7b798 | 131 | * |
1da177e4 LT |
132 | * Only called from user space. |
133 | * | |
7fcb3bc3 | 134 | * When user can change pt_regs->foo always force IRET. That is because |
7bf36bbc AK |
135 | * it deals with uncanonical addresses better. SYSRET has trouble |
136 | * with them due to bugs in both AMD and Intel CPUs. | |
0bd7b798 | 137 | */ |
1da177e4 | 138 | |
b2502b41 | 139 | ENTRY(entry_SYSCALL_64) |
8c1f7558 | 140 | UNWIND_HINT_EMPTY |
9ed8e7d8 DV |
141 | /* |
142 | * Interrupts are off on entry. | |
143 | * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON, | |
144 | * it is too small to ever cause noticeable irq latency. | |
145 | */ | |
72fe4858 | 146 | |
8a9949bc | 147 | swapgs |
4d732138 IM |
148 | movq %rsp, PER_CPU_VAR(rsp_scratch) |
149 | movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp | |
9ed8e7d8 | 150 | |
1e423bff AL |
151 | TRACE_IRQS_OFF |
152 | ||
9ed8e7d8 | 153 | /* Construct struct pt_regs on stack */ |
4d732138 IM |
154 | pushq $__USER_DS /* pt_regs->ss */ |
155 | pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */ | |
4d732138 IM |
156 | pushq %r11 /* pt_regs->flags */ |
157 | pushq $__USER_CS /* pt_regs->cs */ | |
158 | pushq %rcx /* pt_regs->ip */ | |
8a9949bc | 159 | GLOBAL(entry_SYSCALL_64_after_hwframe) |
4d732138 IM |
160 | pushq %rax /* pt_regs->orig_ax */ |
161 | pushq %rdi /* pt_regs->di */ | |
162 | pushq %rsi /* pt_regs->si */ | |
163 | pushq %rdx /* pt_regs->dx */ | |
164 | pushq %rcx /* pt_regs->cx */ | |
165 | pushq $-ENOSYS /* pt_regs->ax */ | |
166 | pushq %r8 /* pt_regs->r8 */ | |
167 | pushq %r9 /* pt_regs->r9 */ | |
168 | pushq %r10 /* pt_regs->r10 */ | |
169 | pushq %r11 /* pt_regs->r11 */ | |
170 | sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */ | |
8c1f7558 | 171 | UNWIND_HINT_REGS extra=0 |
4d732138 | 172 | |
1e423bff AL |
173 | /* |
174 | * If we need to do entry work or if we guess we'll need to do | |
175 | * exit work, go straight to the slow path. | |
176 | */ | |
15f4eae7 AL |
177 | movq PER_CPU_VAR(current_task), %r11 |
178 | testl $_TIF_WORK_SYSCALL_ENTRY|_TIF_ALLWORK_MASK, TASK_TI_flags(%r11) | |
1e423bff AL |
179 | jnz entry_SYSCALL64_slow_path |
180 | ||
b2502b41 | 181 | entry_SYSCALL_64_fastpath: |
1e423bff AL |
182 | /* |
183 | * Easy case: enable interrupts and issue the syscall. If the syscall | |
184 | * needs pt_regs, we'll call a stub that disables interrupts again | |
185 | * and jumps to the slow path. | |
186 | */ | |
187 | TRACE_IRQS_ON | |
188 | ENABLE_INTERRUPTS(CLBR_NONE) | |
fca460f9 | 189 | #if __SYSCALL_MASK == ~0 |
4d732138 | 190 | cmpq $__NR_syscall_max, %rax |
fca460f9 | 191 | #else |
4d732138 IM |
192 | andl $__SYSCALL_MASK, %eax |
193 | cmpl $__NR_syscall_max, %eax | |
fca460f9 | 194 | #endif |
4d732138 IM |
195 | ja 1f /* return -ENOSYS (already in pt_regs->ax) */ |
196 | movq %r10, %rcx | |
302f5b26 AL |
197 | |
198 | /* | |
199 | * This call instruction is handled specially in stub_ptregs_64. | |
b7765086 AL |
200 | * It might end up jumping to the slow path. If it jumps, RAX |
201 | * and all argument registers are clobbered. | |
302f5b26 | 202 | */ |
4d732138 | 203 | call *sys_call_table(, %rax, 8) |
302f5b26 AL |
204 | .Lentry_SYSCALL_64_after_fastpath_call: |
205 | ||
4d732138 | 206 | movq %rax, RAX(%rsp) |
146b2b09 | 207 | 1: |
b3494a4a AL |
208 | |
209 | /* | |
1e423bff AL |
210 | * If we get here, then we know that pt_regs is clean for SYSRET64. |
211 | * If we see that no exit work is required (which we are required | |
212 | * to check with IRQs off), then we can go straight to SYSRET64. | |
b3494a4a | 213 | */ |
2140a994 | 214 | DISABLE_INTERRUPTS(CLBR_ANY) |
1e423bff | 215 | TRACE_IRQS_OFF |
15f4eae7 AL |
216 | movq PER_CPU_VAR(current_task), %r11 |
217 | testl $_TIF_ALLWORK_MASK, TASK_TI_flags(%r11) | |
1e423bff | 218 | jnz 1f |
b3494a4a | 219 | |
1e423bff AL |
220 | LOCKDEP_SYS_EXIT |
221 | TRACE_IRQS_ON /* user mode is traced as IRQs on */ | |
eb2a54c3 AL |
222 | movq RIP(%rsp), %rcx |
223 | movq EFLAGS(%rsp), %r11 | |
224 | RESTORE_C_REGS_EXCEPT_RCX_R11 | |
4d732138 | 225 | movq RSP(%rsp), %rsp |
8c1f7558 | 226 | UNWIND_HINT_EMPTY |
2be29982 | 227 | USERGS_SYSRET64 |
1da177e4 | 228 | |
1e423bff AL |
229 | 1: |
230 | /* | |
231 | * The fast path looked good when we started, but something changed | |
232 | * along the way and we need to switch to the slow path. Calling | |
233 | * raise(3) will trigger this, for example. IRQs are off. | |
234 | */ | |
29ea1b25 | 235 | TRACE_IRQS_ON |
2140a994 | 236 | ENABLE_INTERRUPTS(CLBR_ANY) |
76f5df43 | 237 | SAVE_EXTRA_REGS |
4d732138 | 238 | movq %rsp, %rdi |
1e423bff AL |
239 | call syscall_return_slowpath /* returns with IRQs disabled */ |
240 | jmp return_from_SYSCALL_64 | |
0bd7b798 | 241 | |
1e423bff AL |
242 | entry_SYSCALL64_slow_path: |
243 | /* IRQs are off. */ | |
76f5df43 | 244 | SAVE_EXTRA_REGS |
29ea1b25 | 245 | movq %rsp, %rdi |
1e423bff AL |
246 | call do_syscall_64 /* returns with IRQs disabled */ |
247 | ||
248 | return_from_SYSCALL_64: | |
76f5df43 | 249 | RESTORE_EXTRA_REGS |
29ea1b25 | 250 | TRACE_IRQS_IRETQ /* we're about to change IF */ |
fffbb5dc DV |
251 | |
252 | /* | |
253 | * Try to use SYSRET instead of IRET if we're returning to | |
254 | * a completely clean 64-bit userspace context. | |
255 | */ | |
4d732138 IM |
256 | movq RCX(%rsp), %rcx |
257 | movq RIP(%rsp), %r11 | |
258 | cmpq %rcx, %r11 /* RCX == RIP */ | |
259 | jne opportunistic_sysret_failed | |
fffbb5dc DV |
260 | |
261 | /* | |
262 | * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP | |
263 | * in kernel space. This essentially lets the user take over | |
17be0aec | 264 | * the kernel, since userspace controls RSP. |
fffbb5dc | 265 | * |
17be0aec | 266 | * If width of "canonical tail" ever becomes variable, this will need |
fffbb5dc | 267 | * to be updated to remain correct on both old and new CPUs. |
361b4b58 | 268 | * |
cbe0317b KS |
269 | * Change top bits to match most significant bit (47th or 56th bit |
270 | * depending on paging mode) in the address. | |
fffbb5dc | 271 | */ |
17be0aec DV |
272 | shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx |
273 | sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx | |
4d732138 | 274 | |
17be0aec DV |
275 | /* If this changed %rcx, it was not canonical */ |
276 | cmpq %rcx, %r11 | |
277 | jne opportunistic_sysret_failed | |
fffbb5dc | 278 | |
4d732138 IM |
279 | cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */ |
280 | jne opportunistic_sysret_failed | |
fffbb5dc | 281 | |
4d732138 IM |
282 | movq R11(%rsp), %r11 |
283 | cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */ | |
284 | jne opportunistic_sysret_failed | |
fffbb5dc DV |
285 | |
286 | /* | |
3e035305 BP |
287 | * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot |
288 | * restore RF properly. If the slowpath sets it for whatever reason, we | |
289 | * need to restore it correctly. | |
290 | * | |
291 | * SYSRET can restore TF, but unlike IRET, restoring TF results in a | |
292 | * trap from userspace immediately after SYSRET. This would cause an | |
293 | * infinite loop whenever #DB happens with register state that satisfies | |
294 | * the opportunistic SYSRET conditions. For example, single-stepping | |
295 | * this user code: | |
fffbb5dc | 296 | * |
4d732138 | 297 | * movq $stuck_here, %rcx |
fffbb5dc DV |
298 | * pushfq |
299 | * popq %r11 | |
300 | * stuck_here: | |
301 | * | |
302 | * would never get past 'stuck_here'. | |
303 | */ | |
4d732138 IM |
304 | testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11 |
305 | jnz opportunistic_sysret_failed | |
fffbb5dc DV |
306 | |
307 | /* nothing to check for RSP */ | |
308 | ||
4d732138 IM |
309 | cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */ |
310 | jne opportunistic_sysret_failed | |
fffbb5dc DV |
311 | |
312 | /* | |
4d732138 IM |
313 | * We win! This label is here just for ease of understanding |
314 | * perf profiles. Nothing jumps here. | |
fffbb5dc DV |
315 | */ |
316 | syscall_return_via_sysret: | |
17be0aec DV |
317 | /* rcx and r11 are already restored (see code above) */ |
318 | RESTORE_C_REGS_EXCEPT_RCX_R11 | |
4d732138 | 319 | movq RSP(%rsp), %rsp |
8c1f7558 | 320 | UNWIND_HINT_EMPTY |
fffbb5dc | 321 | USERGS_SYSRET64 |
fffbb5dc DV |
322 | |
323 | opportunistic_sysret_failed: | |
324 | SWAPGS | |
325 | jmp restore_c_regs_and_iret | |
b2502b41 | 326 | END(entry_SYSCALL_64) |
0bd7b798 | 327 | |
302f5b26 AL |
328 | ENTRY(stub_ptregs_64) |
329 | /* | |
330 | * Syscalls marked as needing ptregs land here. | |
b7765086 AL |
331 | * If we are on the fast path, we need to save the extra regs, |
332 | * which we achieve by trying again on the slow path. If we are on | |
333 | * the slow path, the extra regs are already saved. | |
302f5b26 AL |
334 | * |
335 | * RAX stores a pointer to the C function implementing the syscall. | |
b7765086 | 336 | * IRQs are on. |
302f5b26 AL |
337 | */ |
338 | cmpq $.Lentry_SYSCALL_64_after_fastpath_call, (%rsp) | |
339 | jne 1f | |
340 | ||
b7765086 AL |
341 | /* |
342 | * Called from fast path -- disable IRQs again, pop return address | |
343 | * and jump to slow path | |
344 | */ | |
2140a994 | 345 | DISABLE_INTERRUPTS(CLBR_ANY) |
b7765086 | 346 | TRACE_IRQS_OFF |
302f5b26 | 347 | popq %rax |
8c1f7558 | 348 | UNWIND_HINT_REGS extra=0 |
b7765086 | 349 | jmp entry_SYSCALL64_slow_path |
302f5b26 AL |
350 | |
351 | 1: | |
b3830e8d | 352 | jmp *%rax /* Called from C */ |
302f5b26 AL |
353 | END(stub_ptregs_64) |
354 | ||
355 | .macro ptregs_stub func | |
356 | ENTRY(ptregs_\func) | |
8c1f7558 | 357 | UNWIND_HINT_FUNC |
302f5b26 AL |
358 | leaq \func(%rip), %rax |
359 | jmp stub_ptregs_64 | |
360 | END(ptregs_\func) | |
361 | .endm | |
362 | ||
363 | /* Instantiate ptregs_stub for each ptregs-using syscall */ | |
364 | #define __SYSCALL_64_QUAL_(sym) | |
365 | #define __SYSCALL_64_QUAL_ptregs(sym) ptregs_stub sym | |
366 | #define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym) | |
367 | #include <asm/syscalls_64.h> | |
fffbb5dc | 368 | |
0100301b BG |
369 | /* |
370 | * %rdi: prev task | |
371 | * %rsi: next task | |
372 | */ | |
373 | ENTRY(__switch_to_asm) | |
8c1f7558 | 374 | UNWIND_HINT_FUNC |
0100301b BG |
375 | /* |
376 | * Save callee-saved registers | |
377 | * This must match the order in inactive_task_frame | |
378 | */ | |
379 | pushq %rbp | |
380 | pushq %rbx | |
381 | pushq %r12 | |
382 | pushq %r13 | |
383 | pushq %r14 | |
384 | pushq %r15 | |
385 | ||
386 | /* switch stack */ | |
387 | movq %rsp, TASK_threadsp(%rdi) | |
388 | movq TASK_threadsp(%rsi), %rsp | |
389 | ||
390 | #ifdef CONFIG_CC_STACKPROTECTOR | |
391 | movq TASK_stack_canary(%rsi), %rbx | |
392 | movq %rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset | |
393 | #endif | |
394 | ||
395 | /* restore callee-saved registers */ | |
396 | popq %r15 | |
397 | popq %r14 | |
398 | popq %r13 | |
399 | popq %r12 | |
400 | popq %rbx | |
401 | popq %rbp | |
402 | ||
403 | jmp __switch_to | |
404 | END(__switch_to_asm) | |
405 | ||
1eeb207f DV |
406 | /* |
407 | * A newly forked process directly context switches into this address. | |
408 | * | |
0100301b | 409 | * rax: prev task we switched from |
616d2483 BG |
410 | * rbx: kernel thread func (NULL for user thread) |
411 | * r12: kernel thread arg | |
1eeb207f DV |
412 | */ |
413 | ENTRY(ret_from_fork) | |
8c1f7558 | 414 | UNWIND_HINT_EMPTY |
0100301b | 415 | movq %rax, %rdi |
ebd57499 | 416 | call schedule_tail /* rdi: 'prev' task parameter */ |
1eeb207f | 417 | |
ebd57499 JP |
418 | testq %rbx, %rbx /* from kernel_thread? */ |
419 | jnz 1f /* kernel threads are uncommon */ | |
24d978b7 | 420 | |
616d2483 | 421 | 2: |
8c1f7558 | 422 | UNWIND_HINT_REGS |
ebd57499 | 423 | movq %rsp, %rdi |
24d978b7 AL |
424 | call syscall_return_slowpath /* returns with IRQs disabled */ |
425 | TRACE_IRQS_ON /* user mode is traced as IRQS on */ | |
426 | SWAPGS | |
427 | jmp restore_regs_and_iret | |
616d2483 BG |
428 | |
429 | 1: | |
430 | /* kernel thread */ | |
431 | movq %r12, %rdi | |
432 | call *%rbx | |
433 | /* | |
434 | * A kernel thread is allowed to return here after successfully | |
435 | * calling do_execve(). Exit to userspace to complete the execve() | |
436 | * syscall. | |
437 | */ | |
438 | movq $0, RAX(%rsp) | |
439 | jmp 2b | |
1eeb207f DV |
440 | END(ret_from_fork) |
441 | ||
939b7871 | 442 | /* |
3304c9c3 DV |
443 | * Build the entry stubs with some assembler magic. |
444 | * We pack 1 stub into every 8-byte block. | |
939b7871 | 445 | */ |
3304c9c3 | 446 | .align 8 |
939b7871 | 447 | ENTRY(irq_entries_start) |
3304c9c3 DV |
448 | vector=FIRST_EXTERNAL_VECTOR |
449 | .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR) | |
8c1f7558 | 450 | UNWIND_HINT_IRET_REGS |
4d732138 | 451 | pushq $(~vector+0x80) /* Note: always in signed byte range */ |
3304c9c3 | 452 | jmp common_interrupt |
3304c9c3 | 453 | .align 8 |
8c1f7558 | 454 | vector=vector+1 |
3304c9c3 | 455 | .endr |
939b7871 PA |
456 | END(irq_entries_start) |
457 | ||
1d3e53e8 AL |
458 | .macro DEBUG_ENTRY_ASSERT_IRQS_OFF |
459 | #ifdef CONFIG_DEBUG_ENTRY | |
460 | pushfq | |
461 | testl $X86_EFLAGS_IF, (%rsp) | |
462 | jz .Lokay_\@ | |
463 | ud2 | |
464 | .Lokay_\@: | |
465 | addq $8, %rsp | |
466 | #endif | |
467 | .endm | |
468 | ||
469 | /* | |
470 | * Enters the IRQ stack if we're not already using it. NMI-safe. Clobbers | |
471 | * flags and puts old RSP into old_rsp, and leaves all other GPRs alone. | |
472 | * Requires kernel GSBASE. | |
473 | * | |
474 | * The invariant is that, if irq_count != -1, then the IRQ stack is in use. | |
475 | */ | |
8c1f7558 | 476 | .macro ENTER_IRQ_STACK regs=1 old_rsp |
1d3e53e8 AL |
477 | DEBUG_ENTRY_ASSERT_IRQS_OFF |
478 | movq %rsp, \old_rsp | |
8c1f7558 JP |
479 | |
480 | .if \regs | |
481 | UNWIND_HINT_REGS base=\old_rsp | |
482 | .endif | |
483 | ||
1d3e53e8 | 484 | incl PER_CPU_VAR(irq_count) |
29955909 | 485 | jnz .Lirq_stack_push_old_rsp_\@ |
1d3e53e8 AL |
486 | |
487 | /* | |
488 | * Right now, if we just incremented irq_count to zero, we've | |
489 | * claimed the IRQ stack but we haven't switched to it yet. | |
490 | * | |
491 | * If anything is added that can interrupt us here without using IST, | |
492 | * it must be *extremely* careful to limit its stack usage. This | |
493 | * could include kprobes and a hypothetical future IST-less #DB | |
494 | * handler. | |
29955909 AL |
495 | * |
496 | * The OOPS unwinder relies on the word at the top of the IRQ | |
497 | * stack linking back to the previous RSP for the entire time we're | |
498 | * on the IRQ stack. For this to work reliably, we need to write | |
499 | * it before we actually move ourselves to the IRQ stack. | |
500 | */ | |
501 | ||
502 | movq \old_rsp, PER_CPU_VAR(irq_stack_union + IRQ_STACK_SIZE - 8) | |
503 | movq PER_CPU_VAR(irq_stack_ptr), %rsp | |
504 | ||
505 | #ifdef CONFIG_DEBUG_ENTRY | |
506 | /* | |
507 | * If the first movq above becomes wrong due to IRQ stack layout | |
508 | * changes, the only way we'll notice is if we try to unwind right | |
509 | * here. Assert that we set up the stack right to catch this type | |
510 | * of bug quickly. | |
1d3e53e8 | 511 | */ |
29955909 AL |
512 | cmpq -8(%rsp), \old_rsp |
513 | je .Lirq_stack_okay\@ | |
514 | ud2 | |
515 | .Lirq_stack_okay\@: | |
516 | #endif | |
1d3e53e8 | 517 | |
29955909 | 518 | .Lirq_stack_push_old_rsp_\@: |
1d3e53e8 | 519 | pushq \old_rsp |
8c1f7558 JP |
520 | |
521 | .if \regs | |
522 | UNWIND_HINT_REGS indirect=1 | |
523 | .endif | |
1d3e53e8 AL |
524 | .endm |
525 | ||
526 | /* | |
527 | * Undoes ENTER_IRQ_STACK. | |
528 | */ | |
8c1f7558 | 529 | .macro LEAVE_IRQ_STACK regs=1 |
1d3e53e8 AL |
530 | DEBUG_ENTRY_ASSERT_IRQS_OFF |
531 | /* We need to be off the IRQ stack before decrementing irq_count. */ | |
532 | popq %rsp | |
533 | ||
8c1f7558 JP |
534 | .if \regs |
535 | UNWIND_HINT_REGS | |
536 | .endif | |
537 | ||
1d3e53e8 AL |
538 | /* |
539 | * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming | |
540 | * the irq stack but we're not on it. | |
541 | */ | |
542 | ||
543 | decl PER_CPU_VAR(irq_count) | |
544 | .endm | |
545 | ||
d99015b1 | 546 | /* |
1da177e4 LT |
547 | * Interrupt entry/exit. |
548 | * | |
549 | * Interrupt entry points save only callee clobbered registers in fast path. | |
d99015b1 AH |
550 | * |
551 | * Entry runs with interrupts off. | |
552 | */ | |
1da177e4 | 553 | |
722024db | 554 | /* 0(%rsp): ~(interrupt number) */ |
1da177e4 | 555 | .macro interrupt func |
f6f64681 | 556 | cld |
ff467594 AL |
557 | ALLOC_PT_GPREGS_ON_STACK |
558 | SAVE_C_REGS | |
559 | SAVE_EXTRA_REGS | |
946c1911 | 560 | ENCODE_FRAME_POINTER |
76f5df43 | 561 | |
ff467594 | 562 | testb $3, CS(%rsp) |
dde74f2e | 563 | jz 1f |
02bc7768 AL |
564 | |
565 | /* | |
566 | * IRQ from user mode. Switch to kernel gsbase and inform context | |
567 | * tracking that we're in kernel mode. | |
568 | */ | |
f6f64681 | 569 | SWAPGS |
f1075053 AL |
570 | |
571 | /* | |
572 | * We need to tell lockdep that IRQs are off. We can't do this until | |
573 | * we fix gsbase, and we should do it before enter_from_user_mode | |
574 | * (which can take locks). Since TRACE_IRQS_OFF idempotent, | |
575 | * the simplest way to handle it is to just call it twice if | |
576 | * we enter from user mode. There's no reason to optimize this since | |
577 | * TRACE_IRQS_OFF is a no-op if lockdep is off. | |
578 | */ | |
579 | TRACE_IRQS_OFF | |
580 | ||
478dc89c | 581 | CALL_enter_from_user_mode |
02bc7768 | 582 | |
76f5df43 | 583 | 1: |
1d3e53e8 | 584 | ENTER_IRQ_STACK old_rsp=%rdi |
f6f64681 DV |
585 | /* We entered an interrupt context - irqs are off: */ |
586 | TRACE_IRQS_OFF | |
587 | ||
a586f98e | 588 | call \func /* rdi points to pt_regs */ |
1da177e4 LT |
589 | .endm |
590 | ||
722024db AH |
591 | /* |
592 | * The interrupt stubs push (~vector+0x80) onto the stack and | |
593 | * then jump to common_interrupt. | |
594 | */ | |
939b7871 PA |
595 | .p2align CONFIG_X86_L1_CACHE_SHIFT |
596 | common_interrupt: | |
ee4eb87b | 597 | ASM_CLAC |
4d732138 | 598 | addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */ |
1da177e4 | 599 | interrupt do_IRQ |
34061f13 | 600 | /* 0(%rsp): old RSP */ |
7effaa88 | 601 | ret_from_intr: |
2140a994 | 602 | DISABLE_INTERRUPTS(CLBR_ANY) |
2601e64d | 603 | TRACE_IRQS_OFF |
625dbc3b | 604 | |
1d3e53e8 | 605 | LEAVE_IRQ_STACK |
625dbc3b | 606 | |
03335e95 | 607 | testb $3, CS(%rsp) |
dde74f2e | 608 | jz retint_kernel |
4d732138 | 609 | |
02bc7768 | 610 | /* Interrupt came from user space */ |
02bc7768 AL |
611 | GLOBAL(retint_user) |
612 | mov %rsp,%rdi | |
613 | call prepare_exit_to_usermode | |
2601e64d | 614 | TRACE_IRQS_IRETQ |
72fe4858 | 615 | SWAPGS |
ff467594 | 616 | jmp restore_regs_and_iret |
2601e64d | 617 | |
627276cb | 618 | /* Returning to kernel space */ |
6ba71b76 | 619 | retint_kernel: |
627276cb DV |
620 | #ifdef CONFIG_PREEMPT |
621 | /* Interrupts are off */ | |
622 | /* Check if we need preemption */ | |
4d732138 | 623 | bt $9, EFLAGS(%rsp) /* were interrupts off? */ |
6ba71b76 | 624 | jnc 1f |
4d732138 | 625 | 0: cmpl $0, PER_CPU_VAR(__preempt_count) |
36acef25 | 626 | jnz 1f |
627276cb | 627 | call preempt_schedule_irq |
36acef25 | 628 | jmp 0b |
6ba71b76 | 629 | 1: |
627276cb | 630 | #endif |
2601e64d IM |
631 | /* |
632 | * The iretq could re-enable interrupts: | |
633 | */ | |
634 | TRACE_IRQS_IRETQ | |
fffbb5dc DV |
635 | |
636 | /* | |
637 | * At this label, code paths which return to kernel and to user, | |
638 | * which come from interrupts/exception and from syscalls, merge. | |
639 | */ | |
ee08c6bd | 640 | GLOBAL(restore_regs_and_iret) |
ff467594 | 641 | RESTORE_EXTRA_REGS |
fffbb5dc | 642 | restore_c_regs_and_iret: |
76f5df43 DV |
643 | RESTORE_C_REGS |
644 | REMOVE_PT_GPREGS_FROM_STACK 8 | |
7209a75d AL |
645 | INTERRUPT_RETURN |
646 | ||
647 | ENTRY(native_iret) | |
8c1f7558 | 648 | UNWIND_HINT_IRET_REGS |
3891a04a PA |
649 | /* |
650 | * Are we returning to a stack segment from the LDT? Note: in | |
651 | * 64-bit mode SS:RSP on the exception stack is always valid. | |
652 | */ | |
34273f41 | 653 | #ifdef CONFIG_X86_ESPFIX64 |
4d732138 IM |
654 | testb $4, (SS-RIP)(%rsp) |
655 | jnz native_irq_return_ldt | |
34273f41 | 656 | #endif |
3891a04a | 657 | |
af726f21 | 658 | .global native_irq_return_iret |
7209a75d | 659 | native_irq_return_iret: |
b645af2d AL |
660 | /* |
661 | * This may fault. Non-paranoid faults on return to userspace are | |
662 | * handled by fixup_bad_iret. These include #SS, #GP, and #NP. | |
663 | * Double-faults due to espfix64 are handled in do_double_fault. | |
664 | * Other faults here are fatal. | |
665 | */ | |
1da177e4 | 666 | iretq |
3701d863 | 667 | |
34273f41 | 668 | #ifdef CONFIG_X86_ESPFIX64 |
7209a75d | 669 | native_irq_return_ldt: |
85063fac AL |
670 | /* |
671 | * We are running with user GSBASE. All GPRs contain their user | |
672 | * values. We have a percpu ESPFIX stack that is eight slots | |
673 | * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom | |
674 | * of the ESPFIX stack. | |
675 | * | |
676 | * We clobber RAX and RDI in this code. We stash RDI on the | |
677 | * normal stack and RAX on the ESPFIX stack. | |
678 | * | |
679 | * The ESPFIX stack layout we set up looks like this: | |
680 | * | |
681 | * --- top of ESPFIX stack --- | |
682 | * SS | |
683 | * RSP | |
684 | * RFLAGS | |
685 | * CS | |
686 | * RIP <-- RSP points here when we're done | |
687 | * RAX <-- espfix_waddr points here | |
688 | * --- bottom of ESPFIX stack --- | |
689 | */ | |
690 | ||
691 | pushq %rdi /* Stash user RDI */ | |
3891a04a | 692 | SWAPGS |
4d732138 | 693 | movq PER_CPU_VAR(espfix_waddr), %rdi |
85063fac AL |
694 | movq %rax, (0*8)(%rdi) /* user RAX */ |
695 | movq (1*8)(%rsp), %rax /* user RIP */ | |
4d732138 | 696 | movq %rax, (1*8)(%rdi) |
85063fac | 697 | movq (2*8)(%rsp), %rax /* user CS */ |
4d732138 | 698 | movq %rax, (2*8)(%rdi) |
85063fac | 699 | movq (3*8)(%rsp), %rax /* user RFLAGS */ |
4d732138 | 700 | movq %rax, (3*8)(%rdi) |
85063fac | 701 | movq (5*8)(%rsp), %rax /* user SS */ |
4d732138 | 702 | movq %rax, (5*8)(%rdi) |
85063fac | 703 | movq (4*8)(%rsp), %rax /* user RSP */ |
4d732138 | 704 | movq %rax, (4*8)(%rdi) |
85063fac AL |
705 | /* Now RAX == RSP. */ |
706 | ||
707 | andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */ | |
708 | popq %rdi /* Restore user RDI */ | |
709 | ||
710 | /* | |
711 | * espfix_stack[31:16] == 0. The page tables are set up such that | |
712 | * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of | |
713 | * espfix_waddr for any X. That is, there are 65536 RO aliases of | |
714 | * the same page. Set up RSP so that RSP[31:16] contains the | |
715 | * respective 16 bits of the /userspace/ RSP and RSP nonetheless | |
716 | * still points to an RO alias of the ESPFIX stack. | |
717 | */ | |
4d732138 | 718 | orq PER_CPU_VAR(espfix_stack), %rax |
3891a04a | 719 | SWAPGS |
4d732138 | 720 | movq %rax, %rsp |
8c1f7558 | 721 | UNWIND_HINT_IRET_REGS offset=8 |
85063fac AL |
722 | |
723 | /* | |
724 | * At this point, we cannot write to the stack any more, but we can | |
725 | * still read. | |
726 | */ | |
727 | popq %rax /* Restore user RAX */ | |
728 | ||
729 | /* | |
730 | * RSP now points to an ordinary IRET frame, except that the page | |
731 | * is read-only and RSP[31:16] are preloaded with the userspace | |
732 | * values. We can now IRET back to userspace. | |
733 | */ | |
4d732138 | 734 | jmp native_irq_return_iret |
34273f41 | 735 | #endif |
4b787e0b | 736 | END(common_interrupt) |
3891a04a | 737 | |
1da177e4 LT |
738 | /* |
739 | * APIC interrupts. | |
0bd7b798 | 740 | */ |
cf910e83 | 741 | .macro apicinterrupt3 num sym do_sym |
322648d1 | 742 | ENTRY(\sym) |
8c1f7558 | 743 | UNWIND_HINT_IRET_REGS |
ee4eb87b | 744 | ASM_CLAC |
4d732138 | 745 | pushq $~(\num) |
39e95433 | 746 | .Lcommon_\sym: |
322648d1 | 747 | interrupt \do_sym |
4d732138 | 748 | jmp ret_from_intr |
322648d1 AH |
749 | END(\sym) |
750 | .endm | |
1da177e4 | 751 | |
469f0023 | 752 | /* Make sure APIC interrupt handlers end up in the irqentry section: */ |
229a7186 MH |
753 | #define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax" |
754 | #define POP_SECTION_IRQENTRY .popsection | |
469f0023 | 755 | |
cf910e83 | 756 | .macro apicinterrupt num sym do_sym |
469f0023 | 757 | PUSH_SECTION_IRQENTRY |
cf910e83 | 758 | apicinterrupt3 \num \sym \do_sym |
469f0023 | 759 | POP_SECTION_IRQENTRY |
cf910e83 SA |
760 | .endm |
761 | ||
322648d1 | 762 | #ifdef CONFIG_SMP |
4d732138 IM |
763 | apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt |
764 | apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt | |
322648d1 | 765 | #endif |
1da177e4 | 766 | |
03b48632 | 767 | #ifdef CONFIG_X86_UV |
4d732138 | 768 | apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt |
03b48632 | 769 | #endif |
4d732138 IM |
770 | |
771 | apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt | |
772 | apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi | |
89b831ef | 773 | |
d78f2664 | 774 | #ifdef CONFIG_HAVE_KVM |
4d732138 IM |
775 | apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi |
776 | apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi | |
210f84b0 | 777 | apicinterrupt3 POSTED_INTR_NESTED_VECTOR kvm_posted_intr_nested_ipi smp_kvm_posted_intr_nested_ipi |
d78f2664 YZ |
778 | #endif |
779 | ||
33e5ff63 | 780 | #ifdef CONFIG_X86_MCE_THRESHOLD |
4d732138 | 781 | apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt |
33e5ff63 SA |
782 | #endif |
783 | ||
24fd78a8 | 784 | #ifdef CONFIG_X86_MCE_AMD |
4d732138 | 785 | apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt |
24fd78a8 AG |
786 | #endif |
787 | ||
33e5ff63 | 788 | #ifdef CONFIG_X86_THERMAL_VECTOR |
4d732138 | 789 | apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt |
33e5ff63 | 790 | #endif |
1812924b | 791 | |
322648d1 | 792 | #ifdef CONFIG_SMP |
4d732138 IM |
793 | apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt |
794 | apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt | |
795 | apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt | |
322648d1 | 796 | #endif |
1da177e4 | 797 | |
4d732138 IM |
798 | apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt |
799 | apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt | |
0bd7b798 | 800 | |
e360adbe | 801 | #ifdef CONFIG_IRQ_WORK |
4d732138 | 802 | apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt |
241771ef IM |
803 | #endif |
804 | ||
1da177e4 LT |
805 | /* |
806 | * Exception entry points. | |
0bd7b798 | 807 | */ |
9b476688 | 808 | #define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8) |
577ed45e AL |
809 | |
810 | .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 | |
322648d1 | 811 | ENTRY(\sym) |
98990a33 | 812 | UNWIND_HINT_IRET_REGS offset=\has_error_code*8 |
8c1f7558 | 813 | |
577ed45e AL |
814 | /* Sanity check */ |
815 | .if \shift_ist != -1 && \paranoid == 0 | |
816 | .error "using shift_ist requires paranoid=1" | |
817 | .endif | |
818 | ||
ee4eb87b | 819 | ASM_CLAC |
cb5dd2c5 AL |
820 | |
821 | .ifeq \has_error_code | |
4d732138 | 822 | pushq $-1 /* ORIG_RAX: no syscall to restart */ |
cb5dd2c5 AL |
823 | .endif |
824 | ||
76f5df43 | 825 | ALLOC_PT_GPREGS_ON_STACK |
cb5dd2c5 AL |
826 | |
827 | .if \paranoid | |
48e08d0f | 828 | .if \paranoid == 1 |
4d732138 IM |
829 | testb $3, CS(%rsp) /* If coming from userspace, switch stacks */ |
830 | jnz 1f | |
48e08d0f | 831 | .endif |
4d732138 | 832 | call paranoid_entry |
cb5dd2c5 | 833 | .else |
4d732138 | 834 | call error_entry |
cb5dd2c5 | 835 | .endif |
8c1f7558 | 836 | UNWIND_HINT_REGS |
ebfc453e | 837 | /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */ |
cb5dd2c5 | 838 | |
cb5dd2c5 | 839 | .if \paranoid |
577ed45e | 840 | .if \shift_ist != -1 |
4d732138 | 841 | TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */ |
577ed45e | 842 | .else |
b8b1d08b | 843 | TRACE_IRQS_OFF |
cb5dd2c5 | 844 | .endif |
577ed45e | 845 | .endif |
cb5dd2c5 | 846 | |
4d732138 | 847 | movq %rsp, %rdi /* pt_regs pointer */ |
cb5dd2c5 AL |
848 | |
849 | .if \has_error_code | |
4d732138 IM |
850 | movq ORIG_RAX(%rsp), %rsi /* get error code */ |
851 | movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ | |
cb5dd2c5 | 852 | .else |
4d732138 | 853 | xorl %esi, %esi /* no error code */ |
cb5dd2c5 AL |
854 | .endif |
855 | ||
577ed45e | 856 | .if \shift_ist != -1 |
4d732138 | 857 | subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist) |
577ed45e AL |
858 | .endif |
859 | ||
4d732138 | 860 | call \do_sym |
cb5dd2c5 | 861 | |
577ed45e | 862 | .if \shift_ist != -1 |
4d732138 | 863 | addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist) |
577ed45e AL |
864 | .endif |
865 | ||
ebfc453e | 866 | /* these procedures expect "no swapgs" flag in ebx */ |
cb5dd2c5 | 867 | .if \paranoid |
4d732138 | 868 | jmp paranoid_exit |
cb5dd2c5 | 869 | .else |
4d732138 | 870 | jmp error_exit |
cb5dd2c5 AL |
871 | .endif |
872 | ||
48e08d0f | 873 | .if \paranoid == 1 |
48e08d0f AL |
874 | /* |
875 | * Paranoid entry from userspace. Switch stacks and treat it | |
876 | * as a normal entry. This means that paranoid handlers | |
877 | * run in real process context if user_mode(regs). | |
878 | */ | |
879 | 1: | |
4d732138 | 880 | call error_entry |
48e08d0f | 881 | |
48e08d0f | 882 | |
4d732138 IM |
883 | movq %rsp, %rdi /* pt_regs pointer */ |
884 | call sync_regs | |
885 | movq %rax, %rsp /* switch stack */ | |
48e08d0f | 886 | |
4d732138 | 887 | movq %rsp, %rdi /* pt_regs pointer */ |
48e08d0f AL |
888 | |
889 | .if \has_error_code | |
4d732138 IM |
890 | movq ORIG_RAX(%rsp), %rsi /* get error code */ |
891 | movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ | |
48e08d0f | 892 | .else |
4d732138 | 893 | xorl %esi, %esi /* no error code */ |
48e08d0f AL |
894 | .endif |
895 | ||
4d732138 | 896 | call \do_sym |
48e08d0f | 897 | |
4d732138 | 898 | jmp error_exit /* %ebx: no swapgs flag */ |
48e08d0f | 899 | .endif |
ddeb8f21 | 900 | END(\sym) |
322648d1 | 901 | .endm |
b8b1d08b | 902 | |
4d732138 IM |
903 | idtentry divide_error do_divide_error has_error_code=0 |
904 | idtentry overflow do_overflow has_error_code=0 | |
905 | idtentry bounds do_bounds has_error_code=0 | |
906 | idtentry invalid_op do_invalid_op has_error_code=0 | |
907 | idtentry device_not_available do_device_not_available has_error_code=0 | |
908 | idtentry double_fault do_double_fault has_error_code=1 paranoid=2 | |
909 | idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0 | |
910 | idtentry invalid_TSS do_invalid_TSS has_error_code=1 | |
911 | idtentry segment_not_present do_segment_not_present has_error_code=1 | |
912 | idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0 | |
913 | idtentry coprocessor_error do_coprocessor_error has_error_code=0 | |
914 | idtentry alignment_check do_alignment_check has_error_code=1 | |
915 | idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0 | |
916 | ||
917 | ||
918 | /* | |
919 | * Reload gs selector with exception handling | |
920 | * edi: new selector | |
921 | */ | |
9f9d489a | 922 | ENTRY(native_load_gs_index) |
8c1f7558 | 923 | FRAME_BEGIN |
131484c8 | 924 | pushfq |
b8aa287f | 925 | DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI) |
9f1e87ea | 926 | SWAPGS |
42c748bb | 927 | .Lgs_change: |
4d732138 | 928 | movl %edi, %gs |
96e5d28a | 929 | 2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE |
72fe4858 | 930 | SWAPGS |
131484c8 | 931 | popfq |
8c1f7558 | 932 | FRAME_END |
9f1e87ea | 933 | ret |
8c1f7558 | 934 | ENDPROC(native_load_gs_index) |
784d5699 | 935 | EXPORT_SYMBOL(native_load_gs_index) |
0bd7b798 | 936 | |
42c748bb | 937 | _ASM_EXTABLE(.Lgs_change, bad_gs) |
4d732138 | 938 | .section .fixup, "ax" |
1da177e4 | 939 | /* running with kernelgs */ |
0bd7b798 | 940 | bad_gs: |
4d732138 | 941 | SWAPGS /* switch back to user gs */ |
b038c842 AL |
942 | .macro ZAP_GS |
943 | /* This can't be a string because the preprocessor needs to see it. */ | |
944 | movl $__USER_DS, %eax | |
945 | movl %eax, %gs | |
946 | .endm | |
947 | ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG | |
4d732138 IM |
948 | xorl %eax, %eax |
949 | movl %eax, %gs | |
950 | jmp 2b | |
9f1e87ea | 951 | .previous |
0bd7b798 | 952 | |
2699500b | 953 | /* Call softirq on interrupt stack. Interrupts are off. */ |
7d65f4a6 | 954 | ENTRY(do_softirq_own_stack) |
4d732138 IM |
955 | pushq %rbp |
956 | mov %rsp, %rbp | |
8c1f7558 | 957 | ENTER_IRQ_STACK regs=0 old_rsp=%r11 |
4d732138 | 958 | call __do_softirq |
8c1f7558 | 959 | LEAVE_IRQ_STACK regs=0 |
2699500b | 960 | leaveq |
ed6b676c | 961 | ret |
8c1f7558 | 962 | ENDPROC(do_softirq_own_stack) |
75154f40 | 963 | |
3d75e1b8 | 964 | #ifdef CONFIG_XEN |
5878d5d6 | 965 | idtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0 |
3d75e1b8 JF |
966 | |
967 | /* | |
9f1e87ea CG |
968 | * A note on the "critical region" in our callback handler. |
969 | * We want to avoid stacking callback handlers due to events occurring | |
970 | * during handling of the last event. To do this, we keep events disabled | |
971 | * until we've done all processing. HOWEVER, we must enable events before | |
972 | * popping the stack frame (can't be done atomically) and so it would still | |
973 | * be possible to get enough handler activations to overflow the stack. | |
974 | * Although unlikely, bugs of that kind are hard to track down, so we'd | |
975 | * like to avoid the possibility. | |
976 | * So, on entry to the handler we detect whether we interrupted an | |
977 | * existing activation in its critical region -- if so, we pop the current | |
978 | * activation and restart the handler using the previous one. | |
979 | */ | |
4d732138 IM |
980 | ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */ |
981 | ||
9f1e87ea CG |
982 | /* |
983 | * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will | |
984 | * see the correct pointer to the pt_regs | |
985 | */ | |
8c1f7558 | 986 | UNWIND_HINT_FUNC |
4d732138 | 987 | movq %rdi, %rsp /* we don't return, adjust the stack frame */ |
8c1f7558 | 988 | UNWIND_HINT_REGS |
1d3e53e8 AL |
989 | |
990 | ENTER_IRQ_STACK old_rsp=%r10 | |
4d732138 | 991 | call xen_evtchn_do_upcall |
1d3e53e8 AL |
992 | LEAVE_IRQ_STACK |
993 | ||
fdfd811d | 994 | #ifndef CONFIG_PREEMPT |
4d732138 | 995 | call xen_maybe_preempt_hcall |
fdfd811d | 996 | #endif |
4d732138 | 997 | jmp error_exit |
371c394a | 998 | END(xen_do_hypervisor_callback) |
3d75e1b8 JF |
999 | |
1000 | /* | |
9f1e87ea CG |
1001 | * Hypervisor uses this for application faults while it executes. |
1002 | * We get here for two reasons: | |
1003 | * 1. Fault while reloading DS, ES, FS or GS | |
1004 | * 2. Fault while executing IRET | |
1005 | * Category 1 we do not need to fix up as Xen has already reloaded all segment | |
1006 | * registers that could be reloaded and zeroed the others. | |
1007 | * Category 2 we fix up by killing the current process. We cannot use the | |
1008 | * normal Linux return path in this case because if we use the IRET hypercall | |
1009 | * to pop the stack frame we end up in an infinite loop of failsafe callbacks. | |
1010 | * We distinguish between categories by comparing each saved segment register | |
1011 | * with its current contents: any discrepancy means we in category 1. | |
1012 | */ | |
3d75e1b8 | 1013 | ENTRY(xen_failsafe_callback) |
8c1f7558 | 1014 | UNWIND_HINT_EMPTY |
4d732138 IM |
1015 | movl %ds, %ecx |
1016 | cmpw %cx, 0x10(%rsp) | |
1017 | jne 1f | |
1018 | movl %es, %ecx | |
1019 | cmpw %cx, 0x18(%rsp) | |
1020 | jne 1f | |
1021 | movl %fs, %ecx | |
1022 | cmpw %cx, 0x20(%rsp) | |
1023 | jne 1f | |
1024 | movl %gs, %ecx | |
1025 | cmpw %cx, 0x28(%rsp) | |
1026 | jne 1f | |
3d75e1b8 | 1027 | /* All segments match their saved values => Category 2 (Bad IRET). */ |
4d732138 IM |
1028 | movq (%rsp), %rcx |
1029 | movq 8(%rsp), %r11 | |
1030 | addq $0x30, %rsp | |
1031 | pushq $0 /* RIP */ | |
8c1f7558 | 1032 | UNWIND_HINT_IRET_REGS offset=8 |
4d732138 | 1033 | jmp general_protection |
3d75e1b8 | 1034 | 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */ |
4d732138 IM |
1035 | movq (%rsp), %rcx |
1036 | movq 8(%rsp), %r11 | |
1037 | addq $0x30, %rsp | |
8c1f7558 | 1038 | UNWIND_HINT_IRET_REGS |
4d732138 | 1039 | pushq $-1 /* orig_ax = -1 => not a system call */ |
76f5df43 DV |
1040 | ALLOC_PT_GPREGS_ON_STACK |
1041 | SAVE_C_REGS | |
1042 | SAVE_EXTRA_REGS | |
946c1911 | 1043 | ENCODE_FRAME_POINTER |
4d732138 | 1044 | jmp error_exit |
3d75e1b8 JF |
1045 | END(xen_failsafe_callback) |
1046 | ||
cf910e83 | 1047 | apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ |
38e20b07 SY |
1048 | xen_hvm_callback_vector xen_evtchn_do_upcall |
1049 | ||
3d75e1b8 | 1050 | #endif /* CONFIG_XEN */ |
ddeb8f21 | 1051 | |
bc2b0331 | 1052 | #if IS_ENABLED(CONFIG_HYPERV) |
cf910e83 | 1053 | apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ |
bc2b0331 S |
1054 | hyperv_callback_vector hyperv_vector_handler |
1055 | #endif /* CONFIG_HYPERV */ | |
1056 | ||
4d732138 IM |
1057 | idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK |
1058 | idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK | |
1059 | idtentry stack_segment do_stack_segment has_error_code=1 | |
1060 | ||
6cac5a92 | 1061 | #ifdef CONFIG_XEN |
5878d5d6 JG |
1062 | idtentry xendebug do_debug has_error_code=0 |
1063 | idtentry xenint3 do_int3 has_error_code=0 | |
6cac5a92 | 1064 | #endif |
4d732138 IM |
1065 | |
1066 | idtentry general_protection do_general_protection has_error_code=1 | |
11a7ffb0 | 1067 | idtentry page_fault do_page_fault has_error_code=1 |
4d732138 | 1068 | |
631bc487 | 1069 | #ifdef CONFIG_KVM_GUEST |
4d732138 | 1070 | idtentry async_page_fault do_async_page_fault has_error_code=1 |
631bc487 | 1071 | #endif |
4d732138 | 1072 | |
ddeb8f21 | 1073 | #ifdef CONFIG_X86_MCE |
4d732138 | 1074 | idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip) |
ddeb8f21 AH |
1075 | #endif |
1076 | ||
ebfc453e DV |
1077 | /* |
1078 | * Save all registers in pt_regs, and switch gs if needed. | |
1079 | * Use slow, but surefire "are we in kernel?" check. | |
1080 | * Return: ebx=0: need swapgs on exit, ebx=1: otherwise | |
1081 | */ | |
1082 | ENTRY(paranoid_entry) | |
8c1f7558 | 1083 | UNWIND_HINT_FUNC |
1eeb207f DV |
1084 | cld |
1085 | SAVE_C_REGS 8 | |
1086 | SAVE_EXTRA_REGS 8 | |
946c1911 | 1087 | ENCODE_FRAME_POINTER 8 |
4d732138 IM |
1088 | movl $1, %ebx |
1089 | movl $MSR_GS_BASE, %ecx | |
1eeb207f | 1090 | rdmsr |
4d732138 IM |
1091 | testl %edx, %edx |
1092 | js 1f /* negative -> in kernel */ | |
1eeb207f | 1093 | SWAPGS |
4d732138 | 1094 | xorl %ebx, %ebx |
1eeb207f | 1095 | 1: ret |
ebfc453e | 1096 | END(paranoid_entry) |
ddeb8f21 | 1097 | |
ebfc453e DV |
1098 | /* |
1099 | * "Paranoid" exit path from exception stack. This is invoked | |
1100 | * only on return from non-NMI IST interrupts that came | |
1101 | * from kernel space. | |
1102 | * | |
1103 | * We may be returning to very strange contexts (e.g. very early | |
1104 | * in syscall entry), so checking for preemption here would | |
1105 | * be complicated. Fortunately, we there's no good reason | |
1106 | * to try to handle preemption here. | |
4d732138 IM |
1107 | * |
1108 | * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) | |
ebfc453e | 1109 | */ |
ddeb8f21 | 1110 | ENTRY(paranoid_exit) |
8c1f7558 | 1111 | UNWIND_HINT_REGS |
2140a994 | 1112 | DISABLE_INTERRUPTS(CLBR_ANY) |
5963e317 | 1113 | TRACE_IRQS_OFF_DEBUG |
4d732138 IM |
1114 | testl %ebx, %ebx /* swapgs needed? */ |
1115 | jnz paranoid_exit_no_swapgs | |
f2db9382 | 1116 | TRACE_IRQS_IRETQ |
ddeb8f21 | 1117 | SWAPGS_UNSAFE_STACK |
4d732138 | 1118 | jmp paranoid_exit_restore |
0d550836 | 1119 | paranoid_exit_no_swapgs: |
f2db9382 | 1120 | TRACE_IRQS_IRETQ_DEBUG |
0d550836 | 1121 | paranoid_exit_restore: |
76f5df43 DV |
1122 | RESTORE_EXTRA_REGS |
1123 | RESTORE_C_REGS | |
1124 | REMOVE_PT_GPREGS_FROM_STACK 8 | |
48e08d0f | 1125 | INTERRUPT_RETURN |
ddeb8f21 AH |
1126 | END(paranoid_exit) |
1127 | ||
1128 | /* | |
ebfc453e | 1129 | * Save all registers in pt_regs, and switch gs if needed. |
539f5113 | 1130 | * Return: EBX=0: came from user mode; EBX=1: otherwise |
ddeb8f21 AH |
1131 | */ |
1132 | ENTRY(error_entry) | |
8c1f7558 | 1133 | UNWIND_HINT_FUNC |
ddeb8f21 | 1134 | cld |
76f5df43 DV |
1135 | SAVE_C_REGS 8 |
1136 | SAVE_EXTRA_REGS 8 | |
946c1911 | 1137 | ENCODE_FRAME_POINTER 8 |
4d732138 | 1138 | xorl %ebx, %ebx |
03335e95 | 1139 | testb $3, CS+8(%rsp) |
cb6f64ed | 1140 | jz .Lerror_kernelspace |
539f5113 | 1141 | |
cb6f64ed AL |
1142 | /* |
1143 | * We entered from user mode or we're pretending to have entered | |
1144 | * from user mode due to an IRET fault. | |
1145 | */ | |
ddeb8f21 | 1146 | SWAPGS |
539f5113 | 1147 | |
cb6f64ed | 1148 | .Lerror_entry_from_usermode_after_swapgs: |
f1075053 AL |
1149 | /* |
1150 | * We need to tell lockdep that IRQs are off. We can't do this until | |
1151 | * we fix gsbase, and we should do it before enter_from_user_mode | |
1152 | * (which can take locks). | |
1153 | */ | |
1154 | TRACE_IRQS_OFF | |
478dc89c | 1155 | CALL_enter_from_user_mode |
f1075053 | 1156 | ret |
02bc7768 | 1157 | |
cb6f64ed | 1158 | .Lerror_entry_done: |
ddeb8f21 AH |
1159 | TRACE_IRQS_OFF |
1160 | ret | |
ddeb8f21 | 1161 | |
ebfc453e DV |
1162 | /* |
1163 | * There are two places in the kernel that can potentially fault with | |
1164 | * usergs. Handle them here. B stepping K8s sometimes report a | |
1165 | * truncated RIP for IRET exceptions returning to compat mode. Check | |
1166 | * for these here too. | |
1167 | */ | |
cb6f64ed | 1168 | .Lerror_kernelspace: |
4d732138 IM |
1169 | incl %ebx |
1170 | leaq native_irq_return_iret(%rip), %rcx | |
1171 | cmpq %rcx, RIP+8(%rsp) | |
cb6f64ed | 1172 | je .Lerror_bad_iret |
4d732138 IM |
1173 | movl %ecx, %eax /* zero extend */ |
1174 | cmpq %rax, RIP+8(%rsp) | |
cb6f64ed | 1175 | je .Lbstep_iret |
42c748bb | 1176 | cmpq $.Lgs_change, RIP+8(%rsp) |
cb6f64ed | 1177 | jne .Lerror_entry_done |
539f5113 AL |
1178 | |
1179 | /* | |
42c748bb | 1180 | * hack: .Lgs_change can fail with user gsbase. If this happens, fix up |
539f5113 | 1181 | * gsbase and proceed. We'll fix up the exception and land in |
42c748bb | 1182 | * .Lgs_change's error handler with kernel gsbase. |
539f5113 | 1183 | */ |
2fa5f04f WL |
1184 | SWAPGS |
1185 | jmp .Lerror_entry_done | |
ae24ffe5 | 1186 | |
cb6f64ed | 1187 | .Lbstep_iret: |
ae24ffe5 | 1188 | /* Fix truncated RIP */ |
4d732138 | 1189 | movq %rcx, RIP+8(%rsp) |
b645af2d AL |
1190 | /* fall through */ |
1191 | ||
cb6f64ed | 1192 | .Lerror_bad_iret: |
539f5113 AL |
1193 | /* |
1194 | * We came from an IRET to user mode, so we have user gsbase. | |
1195 | * Switch to kernel gsbase: | |
1196 | */ | |
b645af2d | 1197 | SWAPGS |
539f5113 AL |
1198 | |
1199 | /* | |
1200 | * Pretend that the exception came from user mode: set up pt_regs | |
1201 | * as if we faulted immediately after IRET and clear EBX so that | |
1202 | * error_exit knows that we will be returning to user mode. | |
1203 | */ | |
4d732138 IM |
1204 | mov %rsp, %rdi |
1205 | call fixup_bad_iret | |
1206 | mov %rax, %rsp | |
539f5113 | 1207 | decl %ebx |
cb6f64ed | 1208 | jmp .Lerror_entry_from_usermode_after_swapgs |
ddeb8f21 AH |
1209 | END(error_entry) |
1210 | ||
1211 | ||
539f5113 | 1212 | /* |
75ca5b22 | 1213 | * On entry, EBX is a "return to kernel mode" flag: |
539f5113 AL |
1214 | * 1: already in kernel mode, don't need SWAPGS |
1215 | * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode | |
1216 | */ | |
ddeb8f21 | 1217 | ENTRY(error_exit) |
8c1f7558 | 1218 | UNWIND_HINT_REGS |
2140a994 | 1219 | DISABLE_INTERRUPTS(CLBR_ANY) |
ddeb8f21 | 1220 | TRACE_IRQS_OFF |
2140a994 | 1221 | testl %ebx, %ebx |
4d732138 IM |
1222 | jnz retint_kernel |
1223 | jmp retint_user | |
ddeb8f21 AH |
1224 | END(error_exit) |
1225 | ||
0784b364 | 1226 | /* Runs on exception stack */ |
5878d5d6 | 1227 | /* XXX: broken on Xen PV */ |
ddeb8f21 | 1228 | ENTRY(nmi) |
8c1f7558 | 1229 | UNWIND_HINT_IRET_REGS |
3f3c8b8c SR |
1230 | /* |
1231 | * We allow breakpoints in NMIs. If a breakpoint occurs, then | |
1232 | * the iretq it performs will take us out of NMI context. | |
1233 | * This means that we can have nested NMIs where the next | |
1234 | * NMI is using the top of the stack of the previous NMI. We | |
1235 | * can't let it execute because the nested NMI will corrupt the | |
1236 | * stack of the previous NMI. NMI handlers are not re-entrant | |
1237 | * anyway. | |
1238 | * | |
1239 | * To handle this case we do the following: | |
1240 | * Check the a special location on the stack that contains | |
1241 | * a variable that is set when NMIs are executing. | |
1242 | * The interrupted task's stack is also checked to see if it | |
1243 | * is an NMI stack. | |
1244 | * If the variable is not set and the stack is not the NMI | |
1245 | * stack then: | |
1246 | * o Set the special variable on the stack | |
0b22930e AL |
1247 | * o Copy the interrupt frame into an "outermost" location on the |
1248 | * stack | |
1249 | * o Copy the interrupt frame into an "iret" location on the stack | |
3f3c8b8c SR |
1250 | * o Continue processing the NMI |
1251 | * If the variable is set or the previous stack is the NMI stack: | |
0b22930e | 1252 | * o Modify the "iret" location to jump to the repeat_nmi |
3f3c8b8c SR |
1253 | * o return back to the first NMI |
1254 | * | |
1255 | * Now on exit of the first NMI, we first clear the stack variable | |
1256 | * The NMI stack will tell any nested NMIs at that point that it is | |
1257 | * nested. Then we pop the stack normally with iret, and if there was | |
1258 | * a nested NMI that updated the copy interrupt stack frame, a | |
1259 | * jump will be made to the repeat_nmi code that will handle the second | |
1260 | * NMI. | |
9b6e6a83 AL |
1261 | * |
1262 | * However, espfix prevents us from directly returning to userspace | |
1263 | * with a single IRET instruction. Similarly, IRET to user mode | |
1264 | * can fault. We therefore handle NMIs from user space like | |
1265 | * other IST entries. | |
3f3c8b8c SR |
1266 | */ |
1267 | ||
e93c1730 AL |
1268 | ASM_CLAC |
1269 | ||
146b2b09 | 1270 | /* Use %rdx as our temp variable throughout */ |
4d732138 | 1271 | pushq %rdx |
3f3c8b8c | 1272 | |
9b6e6a83 AL |
1273 | testb $3, CS-RIP+8(%rsp) |
1274 | jz .Lnmi_from_kernel | |
1275 | ||
1276 | /* | |
1277 | * NMI from user mode. We need to run on the thread stack, but we | |
1278 | * can't go through the normal entry paths: NMIs are masked, and | |
1279 | * we don't want to enable interrupts, because then we'll end | |
1280 | * up in an awkward situation in which IRQs are on but NMIs | |
1281 | * are off. | |
83c133cf AL |
1282 | * |
1283 | * We also must not push anything to the stack before switching | |
1284 | * stacks lest we corrupt the "NMI executing" variable. | |
9b6e6a83 AL |
1285 | */ |
1286 | ||
83c133cf | 1287 | SWAPGS_UNSAFE_STACK |
9b6e6a83 AL |
1288 | cld |
1289 | movq %rsp, %rdx | |
1290 | movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp | |
8c1f7558 | 1291 | UNWIND_HINT_IRET_REGS base=%rdx offset=8 |
9b6e6a83 AL |
1292 | pushq 5*8(%rdx) /* pt_regs->ss */ |
1293 | pushq 4*8(%rdx) /* pt_regs->rsp */ | |
1294 | pushq 3*8(%rdx) /* pt_regs->flags */ | |
1295 | pushq 2*8(%rdx) /* pt_regs->cs */ | |
1296 | pushq 1*8(%rdx) /* pt_regs->rip */ | |
8c1f7558 | 1297 | UNWIND_HINT_IRET_REGS |
9b6e6a83 AL |
1298 | pushq $-1 /* pt_regs->orig_ax */ |
1299 | pushq %rdi /* pt_regs->di */ | |
1300 | pushq %rsi /* pt_regs->si */ | |
1301 | pushq (%rdx) /* pt_regs->dx */ | |
1302 | pushq %rcx /* pt_regs->cx */ | |
1303 | pushq %rax /* pt_regs->ax */ | |
1304 | pushq %r8 /* pt_regs->r8 */ | |
1305 | pushq %r9 /* pt_regs->r9 */ | |
1306 | pushq %r10 /* pt_regs->r10 */ | |
1307 | pushq %r11 /* pt_regs->r11 */ | |
1308 | pushq %rbx /* pt_regs->rbx */ | |
1309 | pushq %rbp /* pt_regs->rbp */ | |
1310 | pushq %r12 /* pt_regs->r12 */ | |
1311 | pushq %r13 /* pt_regs->r13 */ | |
1312 | pushq %r14 /* pt_regs->r14 */ | |
1313 | pushq %r15 /* pt_regs->r15 */ | |
8c1f7558 | 1314 | UNWIND_HINT_REGS |
946c1911 | 1315 | ENCODE_FRAME_POINTER |
9b6e6a83 AL |
1316 | |
1317 | /* | |
1318 | * At this point we no longer need to worry about stack damage | |
1319 | * due to nesting -- we're on the normal thread stack and we're | |
1320 | * done with the NMI stack. | |
1321 | */ | |
1322 | ||
1323 | movq %rsp, %rdi | |
1324 | movq $-1, %rsi | |
1325 | call do_nmi | |
1326 | ||
45d5a168 | 1327 | /* |
9b6e6a83 | 1328 | * Return back to user mode. We must *not* do the normal exit |
946c1911 | 1329 | * work, because we don't want to enable interrupts. |
45d5a168 | 1330 | */ |
9b6e6a83 | 1331 | SWAPGS |
946c1911 | 1332 | jmp restore_regs_and_iret |
45d5a168 | 1333 | |
9b6e6a83 | 1334 | .Lnmi_from_kernel: |
3f3c8b8c | 1335 | /* |
0b22930e AL |
1336 | * Here's what our stack frame will look like: |
1337 | * +---------------------------------------------------------+ | |
1338 | * | original SS | | |
1339 | * | original Return RSP | | |
1340 | * | original RFLAGS | | |
1341 | * | original CS | | |
1342 | * | original RIP | | |
1343 | * +---------------------------------------------------------+ | |
1344 | * | temp storage for rdx | | |
1345 | * +---------------------------------------------------------+ | |
1346 | * | "NMI executing" variable | | |
1347 | * +---------------------------------------------------------+ | |
1348 | * | iret SS } Copied from "outermost" frame | | |
1349 | * | iret Return RSP } on each loop iteration; overwritten | | |
1350 | * | iret RFLAGS } by a nested NMI to force another | | |
1351 | * | iret CS } iteration if needed. | | |
1352 | * | iret RIP } | | |
1353 | * +---------------------------------------------------------+ | |
1354 | * | outermost SS } initialized in first_nmi; | | |
1355 | * | outermost Return RSP } will not be changed before | | |
1356 | * | outermost RFLAGS } NMI processing is done. | | |
1357 | * | outermost CS } Copied to "iret" frame on each | | |
1358 | * | outermost RIP } iteration. | | |
1359 | * +---------------------------------------------------------+ | |
1360 | * | pt_regs | | |
1361 | * +---------------------------------------------------------+ | |
1362 | * | |
1363 | * The "original" frame is used by hardware. Before re-enabling | |
1364 | * NMIs, we need to be done with it, and we need to leave enough | |
1365 | * space for the asm code here. | |
1366 | * | |
1367 | * We return by executing IRET while RSP points to the "iret" frame. | |
1368 | * That will either return for real or it will loop back into NMI | |
1369 | * processing. | |
1370 | * | |
1371 | * The "outermost" frame is copied to the "iret" frame on each | |
1372 | * iteration of the loop, so each iteration starts with the "iret" | |
1373 | * frame pointing to the final return target. | |
1374 | */ | |
1375 | ||
45d5a168 | 1376 | /* |
0b22930e AL |
1377 | * Determine whether we're a nested NMI. |
1378 | * | |
a27507ca AL |
1379 | * If we interrupted kernel code between repeat_nmi and |
1380 | * end_repeat_nmi, then we are a nested NMI. We must not | |
1381 | * modify the "iret" frame because it's being written by | |
1382 | * the outer NMI. That's okay; the outer NMI handler is | |
1383 | * about to about to call do_nmi anyway, so we can just | |
1384 | * resume the outer NMI. | |
45d5a168 | 1385 | */ |
a27507ca AL |
1386 | |
1387 | movq $repeat_nmi, %rdx | |
1388 | cmpq 8(%rsp), %rdx | |
1389 | ja 1f | |
1390 | movq $end_repeat_nmi, %rdx | |
1391 | cmpq 8(%rsp), %rdx | |
1392 | ja nested_nmi_out | |
1393 | 1: | |
45d5a168 | 1394 | |
3f3c8b8c | 1395 | /* |
a27507ca | 1396 | * Now check "NMI executing". If it's set, then we're nested. |
0b22930e AL |
1397 | * This will not detect if we interrupted an outer NMI just |
1398 | * before IRET. | |
3f3c8b8c | 1399 | */ |
4d732138 IM |
1400 | cmpl $1, -8(%rsp) |
1401 | je nested_nmi | |
3f3c8b8c SR |
1402 | |
1403 | /* | |
0b22930e AL |
1404 | * Now test if the previous stack was an NMI stack. This covers |
1405 | * the case where we interrupt an outer NMI after it clears | |
810bc075 AL |
1406 | * "NMI executing" but before IRET. We need to be careful, though: |
1407 | * there is one case in which RSP could point to the NMI stack | |
1408 | * despite there being no NMI active: naughty userspace controls | |
1409 | * RSP at the very beginning of the SYSCALL targets. We can | |
1410 | * pull a fast one on naughty userspace, though: we program | |
1411 | * SYSCALL to mask DF, so userspace cannot cause DF to be set | |
1412 | * if it controls the kernel's RSP. We set DF before we clear | |
1413 | * "NMI executing". | |
3f3c8b8c | 1414 | */ |
0784b364 DV |
1415 | lea 6*8(%rsp), %rdx |
1416 | /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */ | |
1417 | cmpq %rdx, 4*8(%rsp) | |
1418 | /* If the stack pointer is above the NMI stack, this is a normal NMI */ | |
1419 | ja first_nmi | |
4d732138 | 1420 | |
0784b364 DV |
1421 | subq $EXCEPTION_STKSZ, %rdx |
1422 | cmpq %rdx, 4*8(%rsp) | |
1423 | /* If it is below the NMI stack, it is a normal NMI */ | |
1424 | jb first_nmi | |
810bc075 AL |
1425 | |
1426 | /* Ah, it is within the NMI stack. */ | |
1427 | ||
1428 | testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp) | |
1429 | jz first_nmi /* RSP was user controlled. */ | |
1430 | ||
1431 | /* This is a nested NMI. */ | |
0784b364 | 1432 | |
3f3c8b8c SR |
1433 | nested_nmi: |
1434 | /* | |
0b22930e AL |
1435 | * Modify the "iret" frame to point to repeat_nmi, forcing another |
1436 | * iteration of NMI handling. | |
3f3c8b8c | 1437 | */ |
23a781e9 | 1438 | subq $8, %rsp |
4d732138 IM |
1439 | leaq -10*8(%rsp), %rdx |
1440 | pushq $__KERNEL_DS | |
1441 | pushq %rdx | |
131484c8 | 1442 | pushfq |
4d732138 IM |
1443 | pushq $__KERNEL_CS |
1444 | pushq $repeat_nmi | |
3f3c8b8c SR |
1445 | |
1446 | /* Put stack back */ | |
4d732138 | 1447 | addq $(6*8), %rsp |
3f3c8b8c SR |
1448 | |
1449 | nested_nmi_out: | |
4d732138 | 1450 | popq %rdx |
3f3c8b8c | 1451 | |
0b22930e | 1452 | /* We are returning to kernel mode, so this cannot result in a fault. */ |
3f3c8b8c SR |
1453 | INTERRUPT_RETURN |
1454 | ||
1455 | first_nmi: | |
0b22930e | 1456 | /* Restore rdx. */ |
4d732138 | 1457 | movq (%rsp), %rdx |
62610913 | 1458 | |
36f1a77b AL |
1459 | /* Make room for "NMI executing". */ |
1460 | pushq $0 | |
3f3c8b8c | 1461 | |
0b22930e | 1462 | /* Leave room for the "iret" frame */ |
4d732138 | 1463 | subq $(5*8), %rsp |
28696f43 | 1464 | |
0b22930e | 1465 | /* Copy the "original" frame to the "outermost" frame */ |
3f3c8b8c | 1466 | .rept 5 |
4d732138 | 1467 | pushq 11*8(%rsp) |
3f3c8b8c | 1468 | .endr |
8c1f7558 | 1469 | UNWIND_HINT_IRET_REGS |
62610913 | 1470 | |
79fb4ad6 SR |
1471 | /* Everything up to here is safe from nested NMIs */ |
1472 | ||
a97439aa AL |
1473 | #ifdef CONFIG_DEBUG_ENTRY |
1474 | /* | |
1475 | * For ease of testing, unmask NMIs right away. Disabled by | |
1476 | * default because IRET is very expensive. | |
1477 | */ | |
1478 | pushq $0 /* SS */ | |
1479 | pushq %rsp /* RSP (minus 8 because of the previous push) */ | |
1480 | addq $8, (%rsp) /* Fix up RSP */ | |
1481 | pushfq /* RFLAGS */ | |
1482 | pushq $__KERNEL_CS /* CS */ | |
1483 | pushq $1f /* RIP */ | |
1484 | INTERRUPT_RETURN /* continues at repeat_nmi below */ | |
8c1f7558 | 1485 | UNWIND_HINT_IRET_REGS |
a97439aa AL |
1486 | 1: |
1487 | #endif | |
1488 | ||
0b22930e | 1489 | repeat_nmi: |
62610913 JB |
1490 | /* |
1491 | * If there was a nested NMI, the first NMI's iret will return | |
1492 | * here. But NMIs are still enabled and we can take another | |
1493 | * nested NMI. The nested NMI checks the interrupted RIP to see | |
1494 | * if it is between repeat_nmi and end_repeat_nmi, and if so | |
1495 | * it will just return, as we are about to repeat an NMI anyway. | |
1496 | * This makes it safe to copy to the stack frame that a nested | |
1497 | * NMI will update. | |
0b22930e AL |
1498 | * |
1499 | * RSP is pointing to "outermost RIP". gsbase is unknown, but, if | |
1500 | * we're repeating an NMI, gsbase has the same value that it had on | |
1501 | * the first iteration. paranoid_entry will load the kernel | |
36f1a77b AL |
1502 | * gsbase if needed before we call do_nmi. "NMI executing" |
1503 | * is zero. | |
62610913 | 1504 | */ |
36f1a77b | 1505 | movq $1, 10*8(%rsp) /* Set "NMI executing". */ |
3f3c8b8c | 1506 | |
62610913 | 1507 | /* |
0b22930e AL |
1508 | * Copy the "outermost" frame to the "iret" frame. NMIs that nest |
1509 | * here must not modify the "iret" frame while we're writing to | |
1510 | * it or it will end up containing garbage. | |
62610913 | 1511 | */ |
4d732138 | 1512 | addq $(10*8), %rsp |
3f3c8b8c | 1513 | .rept 5 |
4d732138 | 1514 | pushq -6*8(%rsp) |
3f3c8b8c | 1515 | .endr |
4d732138 | 1516 | subq $(5*8), %rsp |
62610913 | 1517 | end_repeat_nmi: |
3f3c8b8c SR |
1518 | |
1519 | /* | |
0b22930e AL |
1520 | * Everything below this point can be preempted by a nested NMI. |
1521 | * If this happens, then the inner NMI will change the "iret" | |
1522 | * frame to point back to repeat_nmi. | |
3f3c8b8c | 1523 | */ |
4d732138 | 1524 | pushq $-1 /* ORIG_RAX: no syscall to restart */ |
76f5df43 DV |
1525 | ALLOC_PT_GPREGS_ON_STACK |
1526 | ||
1fd466ef | 1527 | /* |
ebfc453e | 1528 | * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit |
1fd466ef SR |
1529 | * as we should not be calling schedule in NMI context. |
1530 | * Even with normal interrupts enabled. An NMI should not be | |
1531 | * setting NEED_RESCHED or anything that normal interrupts and | |
1532 | * exceptions might do. | |
1533 | */ | |
4d732138 | 1534 | call paranoid_entry |
8c1f7558 | 1535 | UNWIND_HINT_REGS |
7fbb98c5 | 1536 | |
ddeb8f21 | 1537 | /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */ |
4d732138 IM |
1538 | movq %rsp, %rdi |
1539 | movq $-1, %rsi | |
1540 | call do_nmi | |
7fbb98c5 | 1541 | |
4d732138 IM |
1542 | testl %ebx, %ebx /* swapgs needed? */ |
1543 | jnz nmi_restore | |
ddeb8f21 AH |
1544 | nmi_swapgs: |
1545 | SWAPGS_UNSAFE_STACK | |
1546 | nmi_restore: | |
76f5df43 DV |
1547 | RESTORE_EXTRA_REGS |
1548 | RESTORE_C_REGS | |
0b22930e AL |
1549 | |
1550 | /* Point RSP at the "iret" frame. */ | |
76f5df43 | 1551 | REMOVE_PT_GPREGS_FROM_STACK 6*8 |
28696f43 | 1552 | |
810bc075 AL |
1553 | /* |
1554 | * Clear "NMI executing". Set DF first so that we can easily | |
1555 | * distinguish the remaining code between here and IRET from | |
1556 | * the SYSCALL entry and exit paths. On a native kernel, we | |
1557 | * could just inspect RIP, but, on paravirt kernels, | |
1558 | * INTERRUPT_RETURN can translate into a jump into a | |
1559 | * hypercall page. | |
1560 | */ | |
1561 | std | |
1562 | movq $0, 5*8(%rsp) /* clear "NMI executing" */ | |
0b22930e AL |
1563 | |
1564 | /* | |
1565 | * INTERRUPT_RETURN reads the "iret" frame and exits the NMI | |
1566 | * stack in a single instruction. We are returning to kernel | |
1567 | * mode, so this cannot result in a fault. | |
1568 | */ | |
5ca6f70f | 1569 | INTERRUPT_RETURN |
ddeb8f21 AH |
1570 | END(nmi) |
1571 | ||
1572 | ENTRY(ignore_sysret) | |
8c1f7558 | 1573 | UNWIND_HINT_EMPTY |
4d732138 | 1574 | mov $-ENOSYS, %eax |
ddeb8f21 | 1575 | sysret |
ddeb8f21 | 1576 | END(ignore_sysret) |
2deb4be2 AL |
1577 | |
1578 | ENTRY(rewind_stack_do_exit) | |
8c1f7558 | 1579 | UNWIND_HINT_FUNC |
2deb4be2 AL |
1580 | /* Prevent any naive code from trying to unwind to our caller. */ |
1581 | xorl %ebp, %ebp | |
1582 | ||
1583 | movq PER_CPU_VAR(cpu_current_top_of_stack), %rax | |
8c1f7558 JP |
1584 | leaq -PTREGS_SIZE(%rax), %rsp |
1585 | UNWIND_HINT_FUNC sp_offset=PTREGS_SIZE | |
2deb4be2 AL |
1586 | |
1587 | call do_exit | |
2deb4be2 | 1588 | END(rewind_stack_do_exit) |