]> Git Repo - linux.git/blame - arch/x86/kernel/traps.c
x86/traps: Prepare for using DEFINE_IDTENTRY
[linux.git] / arch / x86 / kernel / traps.c
CommitLineData
1da177e4 1/*
1da177e4 2 * Copyright (C) 1991, 1992 Linus Torvalds
a8c1be9d 3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
1da177e4
LT
4 *
5 * Pentium III FXSR, SSE support
6 * Gareth Hughes <[email protected]>, May 2000
7 */
8
9/*
c1d518c8 10 * Handle hardware traps and faults.
1da177e4 11 */
c767a54b
JP
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
56dd9470 15#include <linux/context_tracking.h>
b5964405
IM
16#include <linux/interrupt.h>
17#include <linux/kallsyms.h>
18#include <linux/spinlock.h>
b5964405
IM
19#include <linux/kprobes.h>
20#include <linux/uaccess.h>
b5964405 21#include <linux/kdebug.h>
f503b5ae 22#include <linux/kgdb.h>
1da177e4 23#include <linux/kernel.h>
186f4360 24#include <linux/export.h>
b5964405 25#include <linux/ptrace.h>
b02ef20a 26#include <linux/uprobes.h>
1da177e4 27#include <linux/string.h>
b5964405 28#include <linux/delay.h>
1da177e4 29#include <linux/errno.h>
b5964405
IM
30#include <linux/kexec.h>
31#include <linux/sched.h>
68db0cf1 32#include <linux/sched/task_stack.h>
1da177e4 33#include <linux/timer.h>
1da177e4 34#include <linux/init.h>
91768d6c 35#include <linux/bug.h>
b5964405
IM
36#include <linux/nmi.h>
37#include <linux/mm.h>
c1d518c8
AH
38#include <linux/smp.h>
39#include <linux/io.h>
0d00449c
PZ
40#include <linux/hardirq.h>
41#include <linux/atomic.h>
42
b5964405 43#include <asm/stacktrace.h>
1da177e4 44#include <asm/processor.h>
1da177e4 45#include <asm/debugreg.h>
35de5b06 46#include <asm/text-patching.h>
08d636b6 47#include <asm/ftrace.h>
c1d518c8 48#include <asm/traps.h>
1da177e4 49#include <asm/desc.h>
78f7f1e5 50#include <asm/fpu/internal.h>
6650cdd9 51#include <asm/cpu.h>
ed1bbc40 52#include <asm/cpu_entry_area.h>
9e55e44e 53#include <asm/mce.h>
4eefbe79 54#include <asm/fixmap.h>
1164dd00 55#include <asm/mach_traps.h>
17f41571 56#include <asm/alternative.h>
a84eeaa9 57#include <asm/fpu/xstate.h>
ba3e127e 58#include <asm/vm86.h>
6fc9dc81 59#include <asm/umip.h>
59c1dcbe
JH
60#include <asm/insn.h>
61#include <asm/insn-eval.h>
c1d518c8 62
081f75bb 63#ifdef CONFIG_X86_64
428cf902 64#include <asm/x86_init.h>
081f75bb
AH
65#include <asm/pgalloc.h>
66#include <asm/proto.h>
081f75bb 67#else
c1d518c8 68#include <asm/processor-flags.h>
8e6dafd6 69#include <asm/setup.h>
b2502b41 70#include <asm/proto.h>
081f75bb 71#endif
1da177e4 72
7854f822 73DECLARE_BITMAP(system_vectors, NR_VECTORS);
b77b881f 74
d99e1bd1 75static inline void cond_local_irq_enable(struct pt_regs *regs)
762db434
AH
76{
77 if (regs->flags & X86_EFLAGS_IF)
78 local_irq_enable();
79}
80
d99e1bd1 81static inline void cond_local_irq_disable(struct pt_regs *regs)
3d2a71a5
AH
82{
83 if (regs->flags & X86_EFLAGS_IF)
84 local_irq_disable();
3d2a71a5
AH
85}
86
9a93848f
PZ
87int is_valid_bugaddr(unsigned long addr)
88{
89 unsigned short ud;
90
91 if (addr < TASK_SIZE_MAX)
92 return 0;
93
94 if (probe_kernel_address((unsigned short *)addr, ud))
95 return 0;
96
97 return ud == INSN_UD0 || ud == INSN_UD2;
98}
99
8a524f80 100int fixup_bug(struct pt_regs *regs, int trapnr)
9a93848f
PZ
101{
102 if (trapnr != X86_TRAP_UD)
103 return 0;
104
105 switch (report_bug(regs->ip, regs)) {
106 case BUG_TRAP_TYPE_NONE:
107 case BUG_TRAP_TYPE_BUG:
108 break;
109
110 case BUG_TRAP_TYPE_WARN:
3b3a371c 111 regs->ip += LEN_UD2;
9a93848f
PZ
112 return 1;
113 }
114
115 return 0;
116}
117
9326638c 118static nokprobe_inline int
79e21d65 119do_trap_no_signal(struct task_struct *tsk, int trapnr, const char *str,
c416ddf5 120 struct pt_regs *regs, long error_code)
1da177e4 121{
d74ef111 122 if (v8086_mode(regs)) {
3c1326f8 123 /*
c416ddf5 124 * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
3c1326f8
AH
125 * On nmi (interrupt 2), do_trap should not be called.
126 */
c416ddf5
FW
127 if (trapnr < X86_TRAP_UD) {
128 if (!handle_vm86_trap((struct kernel_vm86_regs *) regs,
129 error_code, trapnr))
130 return 0;
131 }
79e21d65 132 } else if (!user_mode(regs)) {
81fd9c18 133 if (fixup_exception(regs, trapnr, error_code, 0))
9a93848f
PZ
134 return 0;
135
9a93848f
PZ
136 tsk->thread.error_code = error_code;
137 tsk->thread.trap_nr = trapnr;
138 die(str, regs, error_code);
c416ddf5 139 }
1da177e4 140
b5964405 141 /*
51e7dc70 142 * We want error_code and trap_nr set for userspace faults and
b5964405
IM
143 * kernelspace faults which result in die(), but not
144 * kernelspace faults which are fixed up. die() gives the
145 * process no chance to handle the signal and notice the
146 * kernel fault information, so that won't result in polluting
147 * the information about previously queued, but not yet
148 * delivered, faults. See also do_general_protection below.
149 */
150 tsk->thread.error_code = error_code;
51e7dc70 151 tsk->thread.trap_nr = trapnr;
d1895183 152
c416ddf5
FW
153 return -1;
154}
1da177e4 155
6ace1098
EB
156static void show_signal(struct task_struct *tsk, int signr,
157 const char *type, const char *desc,
158 struct pt_regs *regs, long error_code)
159{
081f75bb
AH
160 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
161 printk_ratelimit()) {
6ace1098
EB
162 pr_info("%s[%d] %s%s ip:%lx sp:%lx error:%lx",
163 tsk->comm, task_pid_nr(tsk), type, desc,
c767a54b 164 regs->ip, regs->sp, error_code);
1c99a687 165 print_vma_addr(KERN_CONT " in ", regs->ip);
c767a54b 166 pr_cont("\n");
081f75bb 167 }
6ace1098
EB
168}
169
9326638c 170static void
c416ddf5 171do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
164881b6 172 long error_code, int sicode, void __user *addr)
c416ddf5
FW
173{
174 struct task_struct *tsk = current;
175
c416ddf5
FW
176 if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code))
177 return;
d1895183 178
6ace1098 179 show_signal(tsk, signr, "trap ", str, regs, error_code);
081f75bb 180
164881b6 181 if (!sicode)
3cf5d076 182 force_sig(signr);
164881b6 183 else
2e1661d2 184 force_sig_fault(signr, sicode, addr);
1da177e4 185}
9326638c 186NOKPROBE_SYMBOL(do_trap);
1da177e4 187
dff0796e 188static void do_error_trap(struct pt_regs *regs, long error_code, char *str,
164881b6 189 unsigned long trapnr, int signr, int sicode, void __user *addr)
dff0796e 190{
5778077d 191 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
02fdcd5e 192
b8347c21
AS
193 /*
194 * WARN*()s end up here; fix them up before we call the
195 * notifier chain.
196 */
197 if (!user_mode(regs) && fixup_bug(regs, trapnr))
198 return;
199
dff0796e
ON
200 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) !=
201 NOTIFY_STOP) {
d99e1bd1 202 cond_local_irq_enable(regs);
164881b6 203 do_trap(trapnr, signr, str, regs, error_code, sicode, addr);
ca4c6a98 204 cond_local_irq_disable(regs);
dff0796e 205 }
dff0796e
ON
206}
207
218e31b6
TG
208/*
209 * Posix requires to provide the address of the faulting instruction for
210 * SIGILL (#UD) and SIGFPE (#DE) in the si_addr member of siginfo_t.
211 *
212 * This address is usually regs->ip, but when an uprobe moved the code out
213 * of line then regs->ip points to the XOL code which would confuse
214 * anything which analyzes the fault address vs. the unmodified binary. If
215 * a trap happened in XOL code then uprobe maps regs->ip back to the
216 * original instruction address.
217 */
218static __always_inline void __user *error_get_trap_addr(struct pt_regs *regs)
219{
220 return (void __user *)uprobe_get_trap_addr(regs);
221}
222
164881b6
EB
223#define IP ((void __user *)uprobe_get_trap_addr(regs))
224#define DO_ERROR(trapnr, signr, sicode, addr, str, name) \
225dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
226{ \
227 do_error_trap(regs, error_code, str, trapnr, signr, sicode, addr); \
1da177e4
LT
228}
229
164881b6
EB
230DO_ERROR(X86_TRAP_DE, SIGFPE, FPE_INTDIV, IP, "divide error", divide_error)
231DO_ERROR(X86_TRAP_OF, SIGSEGV, 0, NULL, "overflow", overflow)
232DO_ERROR(X86_TRAP_UD, SIGILL, ILL_ILLOPN, IP, "invalid opcode", invalid_op)
233DO_ERROR(X86_TRAP_OLD_MF, SIGFPE, 0, NULL, "coprocessor segment overrun", coprocessor_segment_overrun)
234DO_ERROR(X86_TRAP_TS, SIGSEGV, 0, NULL, "invalid TSS", invalid_TSS)
235DO_ERROR(X86_TRAP_NP, SIGBUS, 0, NULL, "segment not present", segment_not_present)
236DO_ERROR(X86_TRAP_SS, SIGBUS, 0, NULL, "stack segment", stack_segment)
164881b6 237#undef IP
1da177e4 238
6650cdd9
PZI
239dotraplinkage void do_alignment_check(struct pt_regs *regs, long error_code)
240{
241 char *str = "alignment check";
242
243 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
244
245 if (notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_AC, SIGBUS) == NOTIFY_STOP)
246 return;
247
248 if (!user_mode(regs))
249 die("Split lock detected\n", regs, error_code);
250
251 local_irq_enable();
252
253 if (handle_user_split_lock(regs, error_code))
254 return;
255
256 do_trap(X86_TRAP_AC, SIGBUS, "alignment check", regs,
257 error_code, BUS_ADRALN, NULL);
258}
259
e37e43a4 260#ifdef CONFIG_VMAP_STACK
6271cfdf
AL
261__visible void __noreturn handle_stack_overflow(const char *message,
262 struct pt_regs *regs,
263 unsigned long fault_address)
e37e43a4
AL
264{
265 printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n",
266 (void *)fault_address, current->stack,
267 (char *)current->stack + THREAD_SIZE - 1);
268 die(message, regs, 0);
269
270 /* Be absolutely certain we don't return. */
2022cceb 271 panic("%s", message);
e37e43a4
AL
272}
273#endif
274
7d8d8cfd
AL
275/*
276 * Runs on an IST stack for x86_64 and on a special task stack for x86_32.
277 *
278 * On x86_64, this is more or less a normal kernel entry. Notwithstanding the
279 * SDM's warnings about double faults being unrecoverable, returning works as
280 * expected. Presumably what the SDM actually means is that the CPU may get
281 * the register state wrong on entry, so returning could be a bad idea.
282 *
283 * Various CPU engineers have promised that double faults due to an IRET fault
284 * while the stack is read-only are, in fact, recoverable.
285 *
286 * On x86_32, this is entered through a task gate, and regs are synthesized
287 * from the TSS. Returning is, in principle, okay, but changes to regs will
288 * be lost. If, for some reason, we need to return to a context with modified
289 * regs, the shim code could be adjusted to synchronize the registers.
290 */
a0d14b89 291dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code, unsigned long cr2)
081f75bb
AH
292{
293 static const char str[] = "double fault";
294 struct task_struct *tsk = current;
295
af726f21
AL
296#ifdef CONFIG_X86_ESPFIX64
297 extern unsigned char native_irq_return_iret[];
298
299 /*
300 * If IRET takes a non-IST fault on the espfix64 stack, then we
6d9256f0
AL
301 * end up promoting it to a doublefault. In that case, take
302 * advantage of the fact that we're not using the normal (TSS.sp0)
303 * stack right now. We can write a fake #GP(0) frame at TSS.sp0
304 * and then modify our own IRET frame so that, when we return,
305 * we land directly at the #GP(0) vector with the stack already
306 * set up according to its expectations.
307 *
308 * The net result is that our #GP handler will think that we
309 * entered from usermode with the bad user context.
95927475 310 *
0d00449c 311 * No need for nmi_enter() here because we don't use RCU.
af726f21 312 */
c739f930 313 if (((long)regs->sp >> P4D_SHIFT) == ESPFIX_PGD_ENTRY &&
af726f21
AL
314 regs->cs == __KERNEL_CS &&
315 regs->ip == (unsigned long)native_irq_return_iret)
316 {
c482feef 317 struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
e9660391 318 unsigned long *p = (unsigned long *)regs->sp;
af726f21 319
6d9256f0
AL
320 /*
321 * regs->sp points to the failing IRET frame on the
322 * ESPFIX64 stack. Copy it to the entry stack. This fills
323 * in gpregs->ss through gpregs->ip.
324 *
325 */
e9660391
PZ
326 gpregs->ip = p[0];
327 gpregs->cs = p[1];
328 gpregs->flags = p[2];
329 gpregs->sp = p[3];
330 gpregs->ss = p[4];
6d9256f0 331 gpregs->orig_ax = 0; /* Missing (lost) #GP error code */
af726f21 332
6d9256f0
AL
333 /*
334 * Adjust our frame so that we return straight to the #GP
335 * vector with the expected RSP value. This is safe because
336 * we won't enable interupts or schedule before we invoke
337 * general_protection, so nothing will clobber the stack
338 * frame we just set up.
bd7b1f7c
AL
339 *
340 * We will enter general_protection with kernel GSBASE,
341 * which is what the stub expects, given that the faulting
342 * RIP will be the IRET instruction.
6d9256f0 343 */
af726f21 344 regs->ip = (unsigned long)general_protection;
6d9256f0 345 regs->sp = (unsigned long)&gpregs->orig_ax;
95927475 346
af726f21
AL
347 return;
348 }
349#endif
350
0d00449c 351 nmi_enter();
c9408265 352 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
081f75bb
AH
353
354 tsk->thread.error_code = error_code;
51e7dc70 355 tsk->thread.trap_nr = X86_TRAP_DF;
081f75bb 356
e37e43a4
AL
357#ifdef CONFIG_VMAP_STACK
358 /*
359 * If we overflow the stack into a guard page, the CPU will fail
360 * to deliver #PF and will send #DF instead. Similarly, if we
361 * take any non-IST exception while too close to the bottom of
362 * the stack, the processor will get a page fault while
363 * delivering the exception and will generate a double fault.
364 *
365 * According to the SDM (footnote in 6.15 under "Interrupt 14 -
366 * Page-Fault Exception (#PF):
367 *
368 * Processors update CR2 whenever a page fault is detected. If a
369 * second page fault occurs while an earlier page fault is being
6d9256f0 370 * delivered, the faulting linear address of the second fault will
e37e43a4
AL
371 * overwrite the contents of CR2 (replacing the previous
372 * address). These updates to CR2 occur even if the page fault
373 * results in a double fault or occurs during the delivery of a
374 * double fault.
375 *
376 * The logic below has a small possibility of incorrectly diagnosing
377 * some errors as stack overflows. For example, if the IDT or GDT
378 * gets corrupted such that #GP delivery fails due to a bad descriptor
379 * causing #GP and we hit this condition while CR2 coincidentally
380 * points to the stack guard page, we'll think we overflowed the
381 * stack. Given that we're going to panic one way or another
382 * if this happens, this isn't necessarily worth fixing.
383 *
384 * If necessary, we could improve the test by only diagnosing
385 * a stack overflow if the saved RSP points within 47 bytes of
386 * the bottom of the stack: if RSP == tsk_stack + 48 and we
387 * take an exception, the stack is already aligned and there
388 * will be enough room SS, RSP, RFLAGS, CS, RIP, and a
389 * possible error code, so a stack overflow would *not* double
390 * fault. With any less space left, exception delivery could
391 * fail, and, as a practical matter, we've overflowed the
392 * stack even if the actual trigger for the double fault was
393 * something else.
394 */
e37e43a4
AL
395 if ((unsigned long)task_stack_page(tsk) - 1 - cr2 < PAGE_SIZE)
396 handle_stack_overflow("kernel stack overflow (double-fault)", regs, cr2);
397#endif
398
93efbde2 399 pr_emerg("PANIC: double fault, error_code: 0x%lx\n", error_code);
0337b7eb 400 die("double fault", regs, error_code);
93efbde2 401 panic("Machine halted.");
081f75bb 402}
081f75bb 403
fe3d197f
DH
404dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)
405{
5778077d 406 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
fe3d197f
DH
407 if (notify_die(DIE_TRAP, "bounds", regs, error_code,
408 X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
8c84014f 409 return;
d99e1bd1 410 cond_local_irq_enable(regs);
fe3d197f 411
f39b6f0e 412 if (!user_mode(regs))
fe3d197f
DH
413 die("bounds", regs, error_code);
414
164881b6 415 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, 0, NULL);
ca4c6a98
TG
416
417 cond_local_irq_disable(regs);
fe3d197f
DH
418}
419
59c1dcbe
JH
420enum kernel_gp_hint {
421 GP_NO_HINT,
422 GP_NON_CANONICAL,
423 GP_CANONICAL
424};
425
426/*
427 * When an uncaught #GP occurs, try to determine the memory address accessed by
428 * the instruction and return that address to the caller. Also, try to figure
429 * out whether any part of the access to that address was non-canonical.
430 */
431static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
432 unsigned long *addr)
1da177e4 433{
59c1dcbe
JH
434 u8 insn_buf[MAX_INSN_SIZE];
435 struct insn insn;
436
437 if (probe_kernel_read(insn_buf, (void *)regs->ip, MAX_INSN_SIZE))
438 return GP_NO_HINT;
439
440 kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE);
441 insn_get_modrm(&insn);
442 insn_get_sib(&insn);
443
444 *addr = (unsigned long)insn_get_addr_ref(&insn, regs);
445 if (*addr == -1UL)
446 return GP_NO_HINT;
447
448#ifdef CONFIG_X86_64
449 /*
450 * Check that:
451 * - the operand is not in the kernel half
452 * - the last byte of the operand is not in the user canonical half
453 */
454 if (*addr < ~__VIRTUAL_MASK &&
455 *addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK)
456 return GP_NON_CANONICAL;
457#endif
458
459 return GP_CANONICAL;
460}
461
462#define GPFSTR "general protection fault"
463
464dotraplinkage void do_general_protection(struct pt_regs *regs, long error_code)
1da177e4 465{
59c1dcbe 466 char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
36209766 467 enum kernel_gp_hint hint = GP_NO_HINT;
13485ab5 468 struct task_struct *tsk;
36209766
BP
469 unsigned long gp_addr;
470 int ret;
b5964405 471
5778077d 472 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
d99e1bd1 473 cond_local_irq_enable(regs);
c6df0d71 474
6fc9dc81
RN
475 if (static_cpu_has(X86_FEATURE_UMIP)) {
476 if (user_mode(regs) && fixup_umip_exception(regs))
ca4c6a98 477 goto exit;
6fc9dc81
RN
478 }
479
d74ef111 480 if (v8086_mode(regs)) {
ef3f6288
FW
481 local_irq_enable();
482 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
ca4c6a98 483 local_irq_disable();
8c84014f 484 return;
ef3f6288 485 }
1da177e4 486
13485ab5 487 tsk = current;
ef3f6288 488
36209766 489 if (user_mode(regs)) {
ef3f6288
FW
490 tsk->thread.error_code = error_code;
491 tsk->thread.trap_nr = X86_TRAP_GP;
76dee4a7 492
36209766
BP
493 show_signal(tsk, SIGSEGV, "", desc, regs, error_code);
494 force_sig(SIGSEGV);
ca4c6a98 495 goto exit;
ef3f6288 496 }
1da177e4 497
36209766 498 if (fixup_exception(regs, X86_TRAP_GP, error_code, 0))
ca4c6a98 499 goto exit;
59c1dcbe 500
13485ab5 501 tsk->thread.error_code = error_code;
51e7dc70 502 tsk->thread.trap_nr = X86_TRAP_GP;
b5964405 503
36209766
BP
504 /*
505 * To be potentially processing a kprobe fault and to trust the result
506 * from kprobe_running(), we have to be non-preemptible.
507 */
508 if (!preemptible() &&
509 kprobe_running() &&
510 kprobe_fault_handler(regs, X86_TRAP_GP))
ca4c6a98 511 goto exit;
aa49f204 512
36209766
BP
513 ret = notify_die(DIE_GPF, desc, regs, error_code, X86_TRAP_GP, SIGSEGV);
514 if (ret == NOTIFY_STOP)
ca4c6a98 515 goto exit;
1da177e4 516
36209766
BP
517 if (error_code)
518 snprintf(desc, sizeof(desc), "segment-related " GPFSTR);
519 else
520 hint = get_kernel_gp_address(regs, &gp_addr);
521
522 if (hint != GP_NO_HINT)
523 snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx",
524 (hint == GP_NON_CANONICAL) ? "probably for non-canonical address"
525 : "maybe for address",
526 gp_addr);
527
528 /*
529 * KASAN is interested only in the non-canonical case, clear it
530 * otherwise.
531 */
532 if (hint != GP_NON_CANONICAL)
533 gp_addr = 0;
b5964405 534
36209766 535 die_addr(desc, regs, error_code, gp_addr);
abd4f750 536
ca4c6a98
TG
537exit:
538 cond_local_irq_disable(regs);
1da177e4 539}
9326638c 540NOKPROBE_SYMBOL(do_general_protection);
1da177e4 541
9326638c 542dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
1da177e4 543{
17f41571
JK
544 if (poke_int3_handler(regs))
545 return;
546
d8ba61ba 547 /*
0d00449c
PZ
548 * Unlike any other non-IST entry, we can be called from pretty much
549 * any location in the kernel through kprobes -- text_poke() will most
550 * likely be handled by poke_int3_handler() above. This means this
551 * handler is effectively NMI-like.
d8ba61ba 552 */
0d00449c
PZ
553 if (!user_mode(regs))
554 nmi_enter();
65c668f5 555
f503b5ae 556#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
c9408265
KC
557 if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
558 SIGTRAP) == NOTIFY_STOP)
6ba3c97a 559 goto exit;
f503b5ae 560#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
cc3a1bf5 561
6f6343f5
MH
562#ifdef CONFIG_KPROBES
563 if (kprobe_int3_handler(regs))
4cdf77a8 564 goto exit;
6f6343f5
MH
565#endif
566
c9408265
KC
567 if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
568 SIGTRAP) == NOTIFY_STOP)
6ba3c97a 569 goto exit;
b5964405 570
d99e1bd1 571 cond_local_irq_enable(regs);
164881b6 572 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, 0, NULL);
d99e1bd1 573 cond_local_irq_disable(regs);
d8ba61ba 574
6ba3c97a 575exit:
0d00449c
PZ
576 if (!user_mode(regs))
577 nmi_exit();
1da177e4 578}
9326638c 579NOKPROBE_SYMBOL(do_int3);
1da177e4 580
081f75bb 581#ifdef CONFIG_X86_64
bd8b96df 582/*
7f2590a1
AL
583 * Help handler running on a per-cpu (IST or entry trampoline) stack
584 * to switch to the normal thread stack if the interrupted code was in
585 * user mode. The actual stack switch is done in entry_64.S
bd8b96df 586 */
daf7a697 587asmlinkage __visible noinstr struct pt_regs *sync_regs(struct pt_regs *eregs)
081f75bb 588{
7f2590a1
AL
589 struct pt_regs *regs = (struct pt_regs *)this_cpu_read(cpu_current_top_of_stack) - 1;
590 if (regs != eregs)
591 *regs = *eregs;
081f75bb
AH
592 return regs;
593}
b645af2d
AL
594
595struct bad_iret_stack {
596 void *error_entry_ret;
597 struct pt_regs regs;
598};
599
d73a3329 600asmlinkage __visible noinstr
b645af2d
AL
601struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
602{
603 /*
604 * This is called from entry_64.S early in handling a fault
605 * caused by a bad iret to user mode. To handle the fault
7f2590a1
AL
606 * correctly, we want to move our stack frame to where it would
607 * be had we entered directly on the entry stack (rather than
608 * just below the IRET frame) and we want to pretend that the
609 * exception came from the IRET target.
b645af2d 610 */
d73a3329
TG
611 struct bad_iret_stack tmp, *new_stack =
612 (struct bad_iret_stack *)__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
b645af2d 613
d73a3329
TG
614 /* Copy the IRET target to the temporary storage. */
615 memcpy(&tmp.regs.ip, (void *)s->regs.sp, 5*8);
b645af2d
AL
616
617 /* Copy the remainder of the stack from the current stack. */
d73a3329
TG
618 memcpy(&tmp, s, offsetof(struct bad_iret_stack, regs.ip));
619
620 /* Update the entry stack */
621 memcpy(new_stack, &tmp, sizeof(tmp));
b645af2d 622
f39b6f0e 623 BUG_ON(!user_mode(&new_stack->regs));
b645af2d
AL
624 return new_stack;
625}
081f75bb
AH
626#endif
627
f2b37575
AL
628static bool is_sysenter_singlestep(struct pt_regs *regs)
629{
630 /*
631 * We don't try for precision here. If we're anywhere in the region of
632 * code that can be single-stepped in the SYSENTER entry path, then
633 * assume that this is a useless single-step trap due to SYSENTER
634 * being invoked with TF set. (We don't know in advance exactly
635 * which instructions will be hit because BTF could plausibly
636 * be set.)
637 */
638#ifdef CONFIG_X86_32
639 return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) <
640 (unsigned long)__end_SYSENTER_singlestep_region -
641 (unsigned long)__begin_SYSENTER_singlestep_region;
642#elif defined(CONFIG_IA32_EMULATION)
643 return (regs->ip - (unsigned long)entry_SYSENTER_compat) <
644 (unsigned long)__end_entry_SYSENTER_compat -
645 (unsigned long)entry_SYSENTER_compat;
646#else
647 return false;
648#endif
649}
650
1da177e4
LT
651/*
652 * Our handling of the processor debug registers is non-trivial.
653 * We do not clear them on entry and exit from the kernel. Therefore
654 * it is possible to get a watchpoint trap here from inside the kernel.
655 * However, the code in ./ptrace.c has ensured that the user can
656 * only set watchpoints on userspace addresses. Therefore the in-kernel
657 * watchpoint trap can only occur in code which is reading/writing
658 * from user space. Such code must not hold kernel locks (since it
659 * can equally take a page fault), therefore it is safe to call
660 * force_sig_info even though that claims and releases locks.
b5964405 661 *
1da177e4
LT
662 * Code in ./signal.c ensures that the debug control register
663 * is restored before we deliver any signal, and therefore that
664 * user code runs with the correct debug control register even though
665 * we clear it here.
666 *
667 * Being careful here means that we don't have to be as careful in a
668 * lot of more complicated places (task switching can be a bit lazy
669 * about restoring all the debug state, and ptrace doesn't have to
670 * find every occurrence of the TF bit that could be saved away even
671 * by user code)
c1d518c8
AH
672 *
673 * May run on IST stack.
1da177e4 674 */
9326638c 675dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
1da177e4 676{
1da177e4 677 struct task_struct *tsk = current;
a1e80faf 678 int user_icebp = 0;
08d68323 679 unsigned long dr6;
da654b74 680 int si_code;
1da177e4 681
0d00449c 682 nmi_enter();
4cdf77a8 683
08d68323 684 get_debugreg(dr6, 6);
8bb56436
AL
685 /*
686 * The Intel SDM says:
687 *
688 * Certain debug exceptions may clear bits 0-3. The remaining
689 * contents of the DR6 register are never cleared by the
690 * processor. To avoid confusion in identifying debug
691 * exceptions, debug handlers should clear the register before
692 * returning to the interrupted task.
693 *
694 * Keep it simple: clear DR6 immediately.
695 */
696 set_debugreg(0, 6);
1da177e4 697
40f9249a
P
698 /* Filter out all the reserved bits which are preset to 1 */
699 dr6 &= ~DR6_RESERVED;
700
81edd9f6
AL
701 /*
702 * The SDM says "The processor clears the BTF flag when it
703 * generates a debug exception." Clear TIF_BLOCKSTEP to keep
704 * TIF_BLOCKSTEP in sync with the hardware BTF flag.
705 */
706 clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
707
f2b37575
AL
708 if (unlikely(!user_mode(regs) && (dr6 & DR_STEP) &&
709 is_sysenter_singlestep(regs))) {
710 dr6 &= ~DR_STEP;
711 if (!dr6)
712 goto exit;
713 /*
714 * else we might have gotten a single-step trap and hit a
715 * watchpoint at the same time, in which case we should fall
716 * through and handle the watchpoint.
717 */
718 }
719
a1e80faf
FW
720 /*
721 * If dr6 has no reason to give us about the origin of this trap,
722 * then it's very likely the result of an icebp/int01 trap.
723 * User wants a sigtrap for that.
724 */
f39b6f0e 725 if (!dr6 && user_mode(regs))
a1e80faf
FW
726 user_icebp = 1;
727
08d68323
P
728 /* Store the virtualized DR6 value */
729 tsk->thread.debugreg6 = dr6;
730
6f6343f5
MH
731#ifdef CONFIG_KPROBES
732 if (kprobe_debug_handler(regs))
733 goto exit;
734#endif
735
5a802e15 736 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
62edab90 737 SIGTRAP) == NOTIFY_STOP)
6ba3c97a 738 goto exit;
3d2a71a5 739
42181186
SR
740 /*
741 * Let others (NMI) know that the debug stack is in use
742 * as we may switch to the interrupt stack.
743 */
744 debug_stack_usage_inc();
745
1da177e4 746 /* It's safe to allow irq's after DR6 has been saved */
d99e1bd1 747 cond_local_irq_enable(regs);
1da177e4 748
d74ef111 749 if (v8086_mode(regs)) {
c9408265
KC
750 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
751 X86_TRAP_DB);
d99e1bd1 752 cond_local_irq_disable(regs);
42181186 753 debug_stack_usage_dec();
6ba3c97a 754 goto exit;
1da177e4
LT
755 }
756
f2b37575
AL
757 if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
758 /*
759 * Historical junk that used to handle SYSENTER single-stepping.
760 * This should be unreachable now. If we survive for a while
761 * without anyone hitting this warning, we'll turn this into
762 * an oops.
763 */
08d68323
P
764 tsk->thread.debugreg6 &= ~DR_STEP;
765 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
766 regs->flags &= ~X86_EFLAGS_TF;
1da177e4 767 }
08d68323 768 si_code = get_si_code(tsk->thread.debugreg6);
a1e80faf 769 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
28d42ea1 770 send_sigtrap(regs, error_code, si_code);
d99e1bd1 771 cond_local_irq_disable(regs);
42181186 772 debug_stack_usage_dec();
1da177e4 773
6ba3c97a 774exit:
0d00449c 775 nmi_exit();
1da177e4 776}
9326638c 777NOKPROBE_SYMBOL(do_debug);
1da177e4
LT
778
779/*
780 * Note that we play around with the 'TS' bit in an attempt to get
781 * the correct behaviour even in the presence of the asynchronous
782 * IRQ13 behaviour
783 */
5e1b05be 784static void math_error(struct pt_regs *regs, int error_code, int trapnr)
1da177e4 785{
e2e75c91 786 struct task_struct *task = current;
e1cebad4 787 struct fpu *fpu = &task->thread.fpu;
0a996c1a 788 int si_code;
c9408265
KC
789 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
790 "simd exception";
e2e75c91 791
d99e1bd1 792 cond_local_irq_enable(regs);
e2e75c91 793
e1cebad4 794 if (!user_mode(regs)) {
81fd9c18 795 if (fixup_exception(regs, trapnr, error_code, 0))
ca4c6a98 796 goto exit;
3ae6295c
SL
797
798 task->thread.error_code = error_code;
799 task->thread.trap_nr = trapnr;
800
801 if (notify_die(DIE_TRAP, str, regs, error_code,
802 trapnr, SIGFPE) != NOTIFY_STOP)
e2e75c91 803 die(str, regs, error_code);
ca4c6a98 804 goto exit;
e2e75c91 805 }
1da177e4
LT
806
807 /*
808 * Save the info for the exception handler and clear the error.
809 */
e1cebad4
IM
810 fpu__save(fpu);
811
812 task->thread.trap_nr = trapnr;
9b6dba9e 813 task->thread.error_code = error_code;
adf77bac 814
0a996c1a 815 si_code = fpu__exception_code(fpu, trapnr);
e1cebad4 816 /* Retry when we get spurious exceptions: */
0a996c1a 817 if (!si_code)
ca4c6a98 818 goto exit;
e1cebad4 819
0a996c1a 820 force_sig_fault(SIGFPE, si_code,
2e1661d2 821 (void __user *)uprobe_get_trap_addr(regs));
ca4c6a98
TG
822exit:
823 cond_local_irq_disable(regs);
1da177e4
LT
824}
825
e407d620 826dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
1da177e4 827{
5778077d 828 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
c9408265 829 math_error(regs, error_code, X86_TRAP_MF);
1da177e4
LT
830}
831
e407d620
AH
832dotraplinkage void
833do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
1da177e4 834{
5778077d 835 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
c9408265 836 math_error(regs, error_code, X86_TRAP_XF);
1da177e4
LT
837}
838
e407d620
AH
839dotraplinkage void
840do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
1da177e4 841{
d244d0e1
TG
842 /*
843 * This addresses a Pentium Pro Erratum:
844 *
845 * PROBLEM: If the APIC subsystem is configured in mixed mode with
846 * Virtual Wire mode implemented through the local APIC, an
847 * interrupt vector of 0Fh (Intel reserved encoding) may be
848 * generated by the local APIC (Int 15). This vector may be
849 * generated upon receipt of a spurious interrupt (an interrupt
850 * which is removed before the system receives the INTA sequence)
851 * instead of the programmed 8259 spurious interrupt vector.
852 *
853 * IMPLICATION: The spurious interrupt vector programmed in the
854 * 8259 is normally handled by an operating system's spurious
855 * interrupt handler. However, a vector of 0Fh is unknown to some
856 * operating systems, which would crash if this erratum occurred.
857 *
858 * In theory this could be limited to 32bit, but the handler is not
859 * hurting and who knows which other CPUs suffer from this.
860 */
081f75bb
AH
861}
862
9326638c 863dotraplinkage void
aa78bcfa 864do_device_not_available(struct pt_regs *regs, long error_code)
7643e9b9 865{
ee35b9b9 866 unsigned long cr0 = read_cr0();
bef8b6da 867
5778077d 868 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
304bceda 869
a334fe43 870#ifdef CONFIG_MATH_EMULATION
ee35b9b9 871 if (!boot_cpu_has(X86_FEATURE_FPU) && (cr0 & X86_CR0_EM)) {
d315760f
TH
872 struct math_emu_info info = { };
873
d99e1bd1 874 cond_local_irq_enable(regs);
d315760f 875
aa78bcfa 876 info.regs = regs;
d315760f 877 math_emulate(&info);
ca4c6a98
TG
878
879 cond_local_irq_disable(regs);
a334fe43 880 return;
7643e9b9 881 }
a334fe43 882#endif
bef8b6da
AL
883
884 /* This should not happen. */
bef8b6da
AL
885 if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) {
886 /* Try to fix it up and carry on. */
887 write_cr0(cr0 & ~X86_CR0_TS);
888 } else {
889 /*
890 * Something terrible happened, and we're better off trying
891 * to kill the task than getting stuck in a never-ending
892 * loop of #NM faults.
893 */
894 die("unexpected #NM exception", regs, error_code);
895 }
7643e9b9 896}
9326638c 897NOKPROBE_SYMBOL(do_device_not_available);
7643e9b9 898
081f75bb 899#ifdef CONFIG_X86_32
e407d620 900dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
f8e0870f 901{
5778077d 902 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
f8e0870f
AH
903 local_irq_enable();
904
c9408265 905 if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
6ba3c97a
FW
906 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
907 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
164881b6 908 ILL_BADSTK, (void __user *)NULL);
6ba3c97a 909 }
ca4c6a98 910 local_irq_disable();
f8e0870f 911}
081f75bb 912#endif
f8e0870f 913
1da177e4
LT
914void __init trap_init(void)
915{
40e7f949
AL
916 /* Init cpu_entry_area before IST entries are set up */
917 setup_cpu_entry_areas();
918
b70543a0 919 idt_setup_traps();
bb3f0b59 920
4eefbe79
KC
921 /*
922 * Set the IDT descriptor to a fixed read-only location, so that the
923 * "sidt" instruction will not leak the location of the kernel, and
924 * to defend the IDT against arbitrary memory write vulnerabilities.
925 * It will be reloaded in cpu_init() */
92a0f81d
TG
926 cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table),
927 PAGE_KERNEL_RO);
928 idt_descr.address = CPU_ENTRY_AREA_RO_IDT;
4eefbe79 929
1da177e4 930 /*
b5964405 931 * Should be a barrier for any external CPU state:
1da177e4
LT
932 */
933 cpu_init();
934
90f6225f 935 idt_setup_ist_traps();
b4d83270 936
0a30908b 937 idt_setup_debugidt_traps();
1da177e4 938}
This page took 2.517896 seconds and 4 git commands to generate.