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