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