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