]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/traps.c
x86/debug: Handle warnings before the notifier chain, to fix KGDB crash
[mirror_ubuntu-artful-kernel.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 <gareth@valinux.com>, 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>
1da177e4
LT
40
41#ifdef CONFIG_EISA
42#include <linux/ioport.h>
43#include <linux/eisa.h>
44#endif
45
c0d12172
DJ
46#if defined(CONFIG_EDAC)
47#include <linux/edac.h>
48#endif
49
f8561296 50#include <asm/kmemcheck.h>
b5964405 51#include <asm/stacktrace.h>
1da177e4 52#include <asm/processor.h>
1da177e4 53#include <asm/debugreg.h>
60063497 54#include <linux/atomic.h>
35de5b06 55#include <asm/text-patching.h>
08d636b6 56#include <asm/ftrace.h>
c1d518c8 57#include <asm/traps.h>
1da177e4 58#include <asm/desc.h>
78f7f1e5 59#include <asm/fpu/internal.h>
9e55e44e 60#include <asm/mce.h>
4eefbe79 61#include <asm/fixmap.h>
1164dd00 62#include <asm/mach_traps.h>
17f41571 63#include <asm/alternative.h>
a84eeaa9 64#include <asm/fpu/xstate.h>
e7126cf5 65#include <asm/trace/mpx.h>
fe3d197f 66#include <asm/mpx.h>
ba3e127e 67#include <asm/vm86.h>
c1d518c8 68
081f75bb 69#ifdef CONFIG_X86_64
428cf902 70#include <asm/x86_init.h>
081f75bb
AH
71#include <asm/pgalloc.h>
72#include <asm/proto.h>
4df05f36
KC
73
74/* No need to be aligned, but done to keep all IDTs defined the same way. */
75gate_desc debug_idt_table[NR_VECTORS] __page_aligned_bss;
081f75bb 76#else
c1d518c8 77#include <asm/processor-flags.h>
8e6dafd6 78#include <asm/setup.h>
b2502b41 79#include <asm/proto.h>
081f75bb 80#endif
1da177e4 81
4df05f36
KC
82/* Must be page-aligned because the real IDT is used in a fixmap. */
83gate_desc idt_table[NR_VECTORS] __page_aligned_bss;
84
b77b881f
YL
85DECLARE_BITMAP(used_vectors, NR_VECTORS);
86EXPORT_SYMBOL_GPL(used_vectors);
87
d99e1bd1 88static inline void cond_local_irq_enable(struct pt_regs *regs)
762db434
AH
89{
90 if (regs->flags & X86_EFLAGS_IF)
91 local_irq_enable();
92}
93
d99e1bd1 94static inline void cond_local_irq_disable(struct pt_regs *regs)
3d2a71a5
AH
95{
96 if (regs->flags & X86_EFLAGS_IF)
97 local_irq_disable();
3d2a71a5
AH
98}
99
aaee8c3c
AL
100/*
101 * In IST context, we explicitly disable preemption. This serves two
102 * purposes: it makes it much less likely that we would accidentally
103 * schedule in IST context and it will force a warning if we somehow
104 * manage to schedule by accident.
105 */
8c84014f 106void ist_enter(struct pt_regs *regs)
95927475 107{
f39b6f0e 108 if (user_mode(regs)) {
5778077d 109 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
95927475
AL
110 } else {
111 /*
112 * We might have interrupted pretty much anything. In
113 * fact, if we're a machine check, we can even interrupt
114 * NMI processing. We don't want in_nmi() to return true,
115 * but we need to notify RCU.
116 */
117 rcu_nmi_enter();
95927475 118 }
b926e6f6 119
aaee8c3c 120 preempt_disable();
b926e6f6
AL
121
122 /* This code is a bit fragile. Test it. */
f78f5b90 123 RCU_LOCKDEP_WARN(!rcu_is_watching(), "ist_enter didn't work");
95927475
AL
124}
125
8c84014f 126void ist_exit(struct pt_regs *regs)
95927475 127{
aaee8c3c 128 preempt_enable_no_resched();
95927475 129
8c84014f 130 if (!user_mode(regs))
95927475
AL
131 rcu_nmi_exit();
132}
133
bced35b6
AL
134/**
135 * ist_begin_non_atomic() - begin a non-atomic section in an IST exception
136 * @regs: regs passed to the IST exception handler
137 *
138 * IST exception handlers normally cannot schedule. As a special
139 * exception, if the exception interrupted userspace code (i.e.
f39b6f0e 140 * user_mode(regs) would return true) and the exception was not
bced35b6
AL
141 * a double fault, it can be safe to schedule. ist_begin_non_atomic()
142 * begins a non-atomic section within an ist_enter()/ist_exit() region.
143 * Callers are responsible for enabling interrupts themselves inside
8c84014f 144 * the non-atomic section, and callers must call ist_end_non_atomic()
bced35b6
AL
145 * before ist_exit().
146 */
147void ist_begin_non_atomic(struct pt_regs *regs)
148{
f39b6f0e 149 BUG_ON(!user_mode(regs));
bced35b6
AL
150
151 /*
152 * Sanity check: we need to be on the normal thread stack. This
153 * will catch asm bugs and any attempt to use ist_preempt_enable
154 * from double_fault.
155 */
a7fcf28d
AL
156 BUG_ON((unsigned long)(current_top_of_stack() -
157 current_stack_pointer()) >= THREAD_SIZE);
bced35b6 158
aaee8c3c 159 preempt_enable_no_resched();
bced35b6
AL
160}
161
162/**
163 * ist_end_non_atomic() - begin a non-atomic section in an IST exception
164 *
165 * Ends a non-atomic section started with ist_begin_non_atomic().
166 */
167void ist_end_non_atomic(void)
168{
aaee8c3c 169 preempt_disable();
bced35b6
AL
170}
171
9a93848f
PZ
172int is_valid_bugaddr(unsigned long addr)
173{
174 unsigned short ud;
175
176 if (addr < TASK_SIZE_MAX)
177 return 0;
178
179 if (probe_kernel_address((unsigned short *)addr, ud))
180 return 0;
181
182 return ud == INSN_UD0 || ud == INSN_UD2;
183}
184
8a524f80 185int fixup_bug(struct pt_regs *regs, int trapnr)
9a93848f
PZ
186{
187 if (trapnr != X86_TRAP_UD)
188 return 0;
189
190 switch (report_bug(regs->ip, regs)) {
191 case BUG_TRAP_TYPE_NONE:
192 case BUG_TRAP_TYPE_BUG:
193 break;
194
195 case BUG_TRAP_TYPE_WARN:
196 regs->ip += LEN_UD0;
197 return 1;
198 }
199
200 return 0;
201}
202
9326638c 203static nokprobe_inline int
c416ddf5
FW
204do_trap_no_signal(struct task_struct *tsk, int trapnr, char *str,
205 struct pt_regs *regs, long error_code)
1da177e4 206{
d74ef111 207 if (v8086_mode(regs)) {
3c1326f8 208 /*
c416ddf5 209 * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
3c1326f8
AH
210 * On nmi (interrupt 2), do_trap should not be called.
211 */
c416ddf5
FW
212 if (trapnr < X86_TRAP_UD) {
213 if (!handle_vm86_trap((struct kernel_vm86_regs *) regs,
214 error_code, trapnr))
215 return 0;
216 }
217 return -1;
1da177e4 218 }
d74ef111 219
55474c48 220 if (!user_mode(regs)) {
9a93848f
PZ
221 if (fixup_exception(regs, trapnr))
222 return 0;
223
9a93848f
PZ
224 tsk->thread.error_code = error_code;
225 tsk->thread.trap_nr = trapnr;
226 die(str, regs, error_code);
c416ddf5 227 }
1da177e4 228
c416ddf5
FW
229 return -1;
230}
1da177e4 231
1c326c4d
ON
232static siginfo_t *fill_trap_info(struct pt_regs *regs, int signr, int trapnr,
233 siginfo_t *info)
958d3d72
ON
234{
235 unsigned long siaddr;
236 int sicode;
237
238 switch (trapnr) {
1c326c4d
ON
239 default:
240 return SEND_SIG_PRIV;
241
958d3d72
ON
242 case X86_TRAP_DE:
243 sicode = FPE_INTDIV;
b02ef20a 244 siaddr = uprobe_get_trap_addr(regs);
958d3d72
ON
245 break;
246 case X86_TRAP_UD:
247 sicode = ILL_ILLOPN;
b02ef20a 248 siaddr = uprobe_get_trap_addr(regs);
958d3d72
ON
249 break;
250 case X86_TRAP_AC:
251 sicode = BUS_ADRALN;
252 siaddr = 0;
253 break;
254 }
255
256 info->si_signo = signr;
257 info->si_errno = 0;
258 info->si_code = sicode;
259 info->si_addr = (void __user *)siaddr;
1c326c4d 260 return info;
958d3d72
ON
261}
262
9326638c 263static void
c416ddf5
FW
264do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
265 long error_code, siginfo_t *info)
266{
267 struct task_struct *tsk = current;
268
269
270 if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code))
271 return;
b5964405 272 /*
51e7dc70 273 * We want error_code and trap_nr set for userspace faults and
b5964405
IM
274 * kernelspace faults which result in die(), but not
275 * kernelspace faults which are fixed up. die() gives the
276 * process no chance to handle the signal and notice the
277 * kernel fault information, so that won't result in polluting
278 * the information about previously queued, but not yet
279 * delivered, faults. See also do_general_protection below.
280 */
281 tsk->thread.error_code = error_code;
51e7dc70 282 tsk->thread.trap_nr = trapnr;
d1895183 283
081f75bb
AH
284 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
285 printk_ratelimit()) {
c767a54b
JP
286 pr_info("%s[%d] trap %s ip:%lx sp:%lx error:%lx",
287 tsk->comm, tsk->pid, str,
288 regs->ip, regs->sp, error_code);
1c99a687 289 print_vma_addr(KERN_CONT " in ", regs->ip);
c767a54b 290 pr_cont("\n");
081f75bb 291 }
081f75bb 292
38cad57b 293 force_sig_info(signr, info ?: SEND_SIG_PRIV, tsk);
1da177e4 294}
9326638c 295NOKPROBE_SYMBOL(do_trap);
1da177e4 296
dff0796e 297static void do_error_trap(struct pt_regs *regs, long error_code, char *str,
1c326c4d 298 unsigned long trapnr, int signr)
dff0796e 299{
1c326c4d 300 siginfo_t info;
dff0796e 301
5778077d 302 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
02fdcd5e 303
3aed4a86
AS
304 /*
305 * WARN*()s end up here; fix them up before we call the
306 * notifier chain.
307 */
308 if (!user_mode(regs) && fixup_bug(regs, trapnr))
309 return;
310
dff0796e
ON
311 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) !=
312 NOTIFY_STOP) {
d99e1bd1 313 cond_local_irq_enable(regs);
1c326c4d
ON
314 do_trap(trapnr, signr, str, regs, error_code,
315 fill_trap_info(regs, signr, trapnr, &info));
dff0796e 316 }
dff0796e
ON
317}
318
b5964405 319#define DO_ERROR(trapnr, signr, str, name) \
e407d620 320dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
b5964405 321{ \
1c326c4d 322 do_error_trap(regs, error_code, str, trapnr, signr); \
1da177e4
LT
323}
324
0eb14833
ON
325DO_ERROR(X86_TRAP_DE, SIGFPE, "divide error", divide_error)
326DO_ERROR(X86_TRAP_OF, SIGSEGV, "overflow", overflow)
0eb14833
ON
327DO_ERROR(X86_TRAP_UD, SIGILL, "invalid opcode", invalid_op)
328DO_ERROR(X86_TRAP_OLD_MF, SIGFPE, "coprocessor segment overrun",coprocessor_segment_overrun)
329DO_ERROR(X86_TRAP_TS, SIGSEGV, "invalid TSS", invalid_TSS)
330DO_ERROR(X86_TRAP_NP, SIGBUS, "segment not present", segment_not_present)
0eb14833 331DO_ERROR(X86_TRAP_SS, SIGBUS, "stack segment", stack_segment)
0eb14833 332DO_ERROR(X86_TRAP_AC, SIGBUS, "alignment check", alignment_check)
1da177e4 333
e37e43a4 334#ifdef CONFIG_VMAP_STACK
6271cfdf
AL
335__visible void __noreturn handle_stack_overflow(const char *message,
336 struct pt_regs *regs,
337 unsigned long fault_address)
e37e43a4
AL
338{
339 printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n",
340 (void *)fault_address, current->stack,
341 (char *)current->stack + THREAD_SIZE - 1);
342 die(message, regs, 0);
343
344 /* Be absolutely certain we don't return. */
345 panic(message);
346}
347#endif
348
081f75bb
AH
349#ifdef CONFIG_X86_64
350/* Runs on IST stack */
081f75bb
AH
351dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
352{
353 static const char str[] = "double fault";
354 struct task_struct *tsk = current;
e37e43a4
AL
355#ifdef CONFIG_VMAP_STACK
356 unsigned long cr2;
357#endif
081f75bb 358
af726f21
AL
359#ifdef CONFIG_X86_ESPFIX64
360 extern unsigned char native_irq_return_iret[];
361
362 /*
363 * If IRET takes a non-IST fault on the espfix64 stack, then we
364 * end up promoting it to a doublefault. In that case, modify
365 * the stack to make it look like we just entered the #GP
366 * handler from user space, similar to bad_iret.
95927475
AL
367 *
368 * No need for ist_enter here because we don't use RCU.
af726f21
AL
369 */
370 if (((long)regs->sp >> PGDIR_SHIFT) == ESPFIX_PGD_ENTRY &&
371 regs->cs == __KERNEL_CS &&
372 regs->ip == (unsigned long)native_irq_return_iret)
373 {
374 struct pt_regs *normal_regs = task_pt_regs(current);
375
376 /* Fake a #GP(0) from userspace. */
377 memmove(&normal_regs->ip, (void *)regs->sp, 5*8);
378 normal_regs->orig_ax = 0; /* Missing (lost) #GP error code */
379 regs->ip = (unsigned long)general_protection;
380 regs->sp = (unsigned long)&normal_regs->orig_ax;
95927475 381
af726f21
AL
382 return;
383 }
384#endif
385
8c84014f 386 ist_enter(regs);
c9408265 387 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
081f75bb
AH
388
389 tsk->thread.error_code = error_code;
51e7dc70 390 tsk->thread.trap_nr = X86_TRAP_DF;
081f75bb 391
e37e43a4
AL
392#ifdef CONFIG_VMAP_STACK
393 /*
394 * If we overflow the stack into a guard page, the CPU will fail
395 * to deliver #PF and will send #DF instead. Similarly, if we
396 * take any non-IST exception while too close to the bottom of
397 * the stack, the processor will get a page fault while
398 * delivering the exception and will generate a double fault.
399 *
400 * According to the SDM (footnote in 6.15 under "Interrupt 14 -
401 * Page-Fault Exception (#PF):
402 *
403 * Processors update CR2 whenever a page fault is detected. If a
404 * second page fault occurs while an earlier page fault is being
405 * deliv- ered, the faulting linear address of the second fault will
406 * overwrite the contents of CR2 (replacing the previous
407 * address). These updates to CR2 occur even if the page fault
408 * results in a double fault or occurs during the delivery of a
409 * double fault.
410 *
411 * The logic below has a small possibility of incorrectly diagnosing
412 * some errors as stack overflows. For example, if the IDT or GDT
413 * gets corrupted such that #GP delivery fails due to a bad descriptor
414 * causing #GP and we hit this condition while CR2 coincidentally
415 * points to the stack guard page, we'll think we overflowed the
416 * stack. Given that we're going to panic one way or another
417 * if this happens, this isn't necessarily worth fixing.
418 *
419 * If necessary, we could improve the test by only diagnosing
420 * a stack overflow if the saved RSP points within 47 bytes of
421 * the bottom of the stack: if RSP == tsk_stack + 48 and we
422 * take an exception, the stack is already aligned and there
423 * will be enough room SS, RSP, RFLAGS, CS, RIP, and a
424 * possible error code, so a stack overflow would *not* double
425 * fault. With any less space left, exception delivery could
426 * fail, and, as a practical matter, we've overflowed the
427 * stack even if the actual trigger for the double fault was
428 * something else.
429 */
430 cr2 = read_cr2();
431 if ((unsigned long)task_stack_page(tsk) - 1 - cr2 < PAGE_SIZE)
432 handle_stack_overflow("kernel stack overflow (double-fault)", regs, cr2);
433#endif
434
4d067d8e
BP
435#ifdef CONFIG_DOUBLEFAULT
436 df_debug(regs, error_code);
437#endif
bd8b96df
IM
438 /*
439 * This is always a kernel trap and never fixable (and thus must
440 * never return).
441 */
081f75bb
AH
442 for (;;)
443 die(str, regs, error_code);
444}
445#endif
446
fe3d197f
DH
447dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)
448{
1126cb45 449 const struct mpx_bndcsr *bndcsr;
fe3d197f
DH
450 siginfo_t *info;
451
5778077d 452 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
fe3d197f
DH
453 if (notify_die(DIE_TRAP, "bounds", regs, error_code,
454 X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
8c84014f 455 return;
d99e1bd1 456 cond_local_irq_enable(regs);
fe3d197f 457
f39b6f0e 458 if (!user_mode(regs))
fe3d197f
DH
459 die("bounds", regs, error_code);
460
461 if (!cpu_feature_enabled(X86_FEATURE_MPX)) {
462 /* The exception is not from Intel MPX */
463 goto exit_trap;
464 }
465
466 /*
467 * We need to look at BNDSTATUS to resolve this exception.
a84eeaa9
DH
468 * A NULL here might mean that it is in its 'init state',
469 * which is all zeros which indicates MPX was not
470 * responsible for the exception.
fe3d197f 471 */
d91cab78 472 bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
fe3d197f
DH
473 if (!bndcsr)
474 goto exit_trap;
475
e7126cf5 476 trace_bounds_exception_mpx(bndcsr);
fe3d197f
DH
477 /*
478 * The error code field of the BNDSTATUS register communicates status
479 * information of a bound range exception #BR or operation involving
480 * bound directory.
481 */
482 switch (bndcsr->bndstatus & MPX_BNDSTA_ERROR_CODE) {
483 case 2: /* Bound directory has invalid entry. */
46a6e0cf 484 if (mpx_handle_bd_fault())
fe3d197f
DH
485 goto exit_trap;
486 break; /* Success, it was handled */
487 case 1: /* Bound violation. */
46a6e0cf 488 info = mpx_generate_siginfo(regs);
e10abb2f 489 if (IS_ERR(info)) {
fe3d197f
DH
490 /*
491 * We failed to decode the MPX instruction. Act as if
492 * the exception was not caused by MPX.
493 */
494 goto exit_trap;
495 }
496 /*
497 * Success, we decoded the instruction and retrieved
498 * an 'info' containing the address being accessed
499 * which caused the exception. This information
500 * allows and application to possibly handle the
501 * #BR exception itself.
502 */
503 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, info);
504 kfree(info);
505 break;
506 case 0: /* No exception caused by Intel MPX operations. */
507 goto exit_trap;
508 default:
509 die("bounds", regs, error_code);
510 }
511
fe3d197f 512 return;
8c84014f 513
fe3d197f
DH
514exit_trap:
515 /*
516 * This path out is for all the cases where we could not
517 * handle the exception in some way (like allocating a
518 * table or telling userspace about it. We will also end
519 * up here if the kernel has MPX turned off at compile
520 * time..
521 */
522 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, NULL);
fe3d197f
DH
523}
524
9326638c 525dotraplinkage void
13485ab5 526do_general_protection(struct pt_regs *regs, long error_code)
1da177e4 527{
13485ab5 528 struct task_struct *tsk;
b5964405 529
5778077d 530 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
d99e1bd1 531 cond_local_irq_enable(regs);
c6df0d71 532
d74ef111 533 if (v8086_mode(regs)) {
ef3f6288
FW
534 local_irq_enable();
535 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
8c84014f 536 return;
ef3f6288 537 }
1da177e4 538
13485ab5 539 tsk = current;
55474c48 540 if (!user_mode(regs)) {
548acf19 541 if (fixup_exception(regs, X86_TRAP_GP))
8c84014f 542 return;
ef3f6288
FW
543
544 tsk->thread.error_code = error_code;
545 tsk->thread.trap_nr = X86_TRAP_GP;
6ba3c97a
FW
546 if (notify_die(DIE_GPF, "general protection fault", regs, error_code,
547 X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP)
ef3f6288 548 die("general protection fault", regs, error_code);
8c84014f 549 return;
ef3f6288 550 }
1da177e4 551
13485ab5 552 tsk->thread.error_code = error_code;
51e7dc70 553 tsk->thread.trap_nr = X86_TRAP_GP;
b5964405 554
13485ab5
AH
555 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
556 printk_ratelimit()) {
c767a54b 557 pr_info("%s[%d] general protection ip:%lx sp:%lx error:%lx",
13485ab5
AH
558 tsk->comm, task_pid_nr(tsk),
559 regs->ip, regs->sp, error_code);
1c99a687 560 print_vma_addr(KERN_CONT " in ", regs->ip);
c767a54b 561 pr_cont("\n");
03252919 562 }
abd4f750 563
38cad57b 564 force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
1da177e4 565}
9326638c 566NOKPROBE_SYMBOL(do_general_protection);
1da177e4 567
c1d518c8 568/* May run on IST stack. */
9326638c 569dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
1da177e4 570{
08d636b6 571#ifdef CONFIG_DYNAMIC_FTRACE
a192cd04
SR
572 /*
573 * ftrace must be first, everything else may cause a recursive crash.
574 * See note by declaration of modifying_ftrace_code in ftrace.c
575 */
576 if (unlikely(atomic_read(&modifying_ftrace_code)) &&
577 ftrace_int3_handler(regs))
08d636b6
SR
578 return;
579#endif
17f41571
JK
580 if (poke_int3_handler(regs))
581 return;
582
8c84014f 583 ist_enter(regs);
5778077d 584 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
f503b5ae 585#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
c9408265
KC
586 if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
587 SIGTRAP) == NOTIFY_STOP)
6ba3c97a 588 goto exit;
f503b5ae 589#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
cc3a1bf5 590
6f6343f5
MH
591#ifdef CONFIG_KPROBES
592 if (kprobe_int3_handler(regs))
4cdf77a8 593 goto exit;
6f6343f5
MH
594#endif
595
c9408265
KC
596 if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
597 SIGTRAP) == NOTIFY_STOP)
6ba3c97a 598 goto exit;
b5964405 599
42181186
SR
600 /*
601 * Let others (NMI) know that the debug stack is in use
602 * as we may switch to the interrupt stack.
603 */
604 debug_stack_usage_inc();
d99e1bd1 605 cond_local_irq_enable(regs);
c9408265 606 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
d99e1bd1 607 cond_local_irq_disable(regs);
42181186 608 debug_stack_usage_dec();
6ba3c97a 609exit:
8c84014f 610 ist_exit(regs);
1da177e4 611}
9326638c 612NOKPROBE_SYMBOL(do_int3);
1da177e4 613
081f75bb 614#ifdef CONFIG_X86_64
bd8b96df 615/*
48e08d0f
AL
616 * Help handler running on IST stack to switch off the IST stack if the
617 * interrupted code was in user mode. The actual stack switch is done in
618 * entry_64.S
bd8b96df 619 */
7ddc6a21 620asmlinkage __visible notrace struct pt_regs *sync_regs(struct pt_regs *eregs)
081f75bb 621{
48e08d0f
AL
622 struct pt_regs *regs = task_pt_regs(current);
623 *regs = *eregs;
081f75bb
AH
624 return regs;
625}
9326638c 626NOKPROBE_SYMBOL(sync_regs);
b645af2d
AL
627
628struct bad_iret_stack {
629 void *error_entry_ret;
630 struct pt_regs regs;
631};
632
7ddc6a21 633asmlinkage __visible notrace
b645af2d
AL
634struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
635{
636 /*
637 * This is called from entry_64.S early in handling a fault
638 * caused by a bad iret to user mode. To handle the fault
639 * correctly, we want move our stack frame to task_pt_regs
640 * and we want to pretend that the exception came from the
641 * iret target.
642 */
643 struct bad_iret_stack *new_stack =
644 container_of(task_pt_regs(current),
645 struct bad_iret_stack, regs);
646
647 /* Copy the IRET target to the new stack. */
648 memmove(&new_stack->regs.ip, (void *)s->regs.sp, 5*8);
649
650 /* Copy the remainder of the stack from the current stack. */
651 memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip));
652
f39b6f0e 653 BUG_ON(!user_mode(&new_stack->regs));
b645af2d
AL
654 return new_stack;
655}
7ddc6a21 656NOKPROBE_SYMBOL(fixup_bad_iret);
081f75bb
AH
657#endif
658
f2b37575
AL
659static bool is_sysenter_singlestep(struct pt_regs *regs)
660{
661 /*
662 * We don't try for precision here. If we're anywhere in the region of
663 * code that can be single-stepped in the SYSENTER entry path, then
664 * assume that this is a useless single-step trap due to SYSENTER
665 * being invoked with TF set. (We don't know in advance exactly
666 * which instructions will be hit because BTF could plausibly
667 * be set.)
668 */
669#ifdef CONFIG_X86_32
670 return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) <
671 (unsigned long)__end_SYSENTER_singlestep_region -
672 (unsigned long)__begin_SYSENTER_singlestep_region;
673#elif defined(CONFIG_IA32_EMULATION)
674 return (regs->ip - (unsigned long)entry_SYSENTER_compat) <
675 (unsigned long)__end_entry_SYSENTER_compat -
676 (unsigned long)entry_SYSENTER_compat;
677#else
678 return false;
679#endif
680}
681
1da177e4
LT
682/*
683 * Our handling of the processor debug registers is non-trivial.
684 * We do not clear them on entry and exit from the kernel. Therefore
685 * it is possible to get a watchpoint trap here from inside the kernel.
686 * However, the code in ./ptrace.c has ensured that the user can
687 * only set watchpoints on userspace addresses. Therefore the in-kernel
688 * watchpoint trap can only occur in code which is reading/writing
689 * from user space. Such code must not hold kernel locks (since it
690 * can equally take a page fault), therefore it is safe to call
691 * force_sig_info even though that claims and releases locks.
b5964405 692 *
1da177e4
LT
693 * Code in ./signal.c ensures that the debug control register
694 * is restored before we deliver any signal, and therefore that
695 * user code runs with the correct debug control register even though
696 * we clear it here.
697 *
698 * Being careful here means that we don't have to be as careful in a
699 * lot of more complicated places (task switching can be a bit lazy
700 * about restoring all the debug state, and ptrace doesn't have to
701 * find every occurrence of the TF bit that could be saved away even
702 * by user code)
c1d518c8
AH
703 *
704 * May run on IST stack.
1da177e4 705 */
9326638c 706dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
1da177e4 707{
1da177e4 708 struct task_struct *tsk = current;
a1e80faf 709 int user_icebp = 0;
08d68323 710 unsigned long dr6;
da654b74 711 int si_code;
1da177e4 712
8c84014f 713 ist_enter(regs);
4cdf77a8 714
08d68323 715 get_debugreg(dr6, 6);
8bb56436
AL
716 /*
717 * The Intel SDM says:
718 *
719 * Certain debug exceptions may clear bits 0-3. The remaining
720 * contents of the DR6 register are never cleared by the
721 * processor. To avoid confusion in identifying debug
722 * exceptions, debug handlers should clear the register before
723 * returning to the interrupted task.
724 *
725 * Keep it simple: clear DR6 immediately.
726 */
727 set_debugreg(0, 6);
1da177e4 728
40f9249a
P
729 /* Filter out all the reserved bits which are preset to 1 */
730 dr6 &= ~DR6_RESERVED;
731
81edd9f6
AL
732 /*
733 * The SDM says "The processor clears the BTF flag when it
734 * generates a debug exception." Clear TIF_BLOCKSTEP to keep
735 * TIF_BLOCKSTEP in sync with the hardware BTF flag.
736 */
737 clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
738
f2b37575
AL
739 if (unlikely(!user_mode(regs) && (dr6 & DR_STEP) &&
740 is_sysenter_singlestep(regs))) {
741 dr6 &= ~DR_STEP;
742 if (!dr6)
743 goto exit;
744 /*
745 * else we might have gotten a single-step trap and hit a
746 * watchpoint at the same time, in which case we should fall
747 * through and handle the watchpoint.
748 */
749 }
750
a1e80faf
FW
751 /*
752 * If dr6 has no reason to give us about the origin of this trap,
753 * then it's very likely the result of an icebp/int01 trap.
754 * User wants a sigtrap for that.
755 */
f39b6f0e 756 if (!dr6 && user_mode(regs))
a1e80faf
FW
757 user_icebp = 1;
758
f2b37575 759 /* Catch kmemcheck conditions! */
eadb8a09 760 if ((dr6 & DR_STEP) && kmemcheck_trap(regs))
6ba3c97a 761 goto exit;
f8561296 762
08d68323
P
763 /* Store the virtualized DR6 value */
764 tsk->thread.debugreg6 = dr6;
765
6f6343f5
MH
766#ifdef CONFIG_KPROBES
767 if (kprobe_debug_handler(regs))
768 goto exit;
769#endif
770
5a802e15 771 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
62edab90 772 SIGTRAP) == NOTIFY_STOP)
6ba3c97a 773 goto exit;
3d2a71a5 774
42181186
SR
775 /*
776 * Let others (NMI) know that the debug stack is in use
777 * as we may switch to the interrupt stack.
778 */
779 debug_stack_usage_inc();
780
1da177e4 781 /* It's safe to allow irq's after DR6 has been saved */
d99e1bd1 782 cond_local_irq_enable(regs);
1da177e4 783
d74ef111 784 if (v8086_mode(regs)) {
c9408265
KC
785 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
786 X86_TRAP_DB);
d99e1bd1 787 cond_local_irq_disable(regs);
42181186 788 debug_stack_usage_dec();
6ba3c97a 789 goto exit;
1da177e4
LT
790 }
791
f2b37575
AL
792 if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
793 /*
794 * Historical junk that used to handle SYSENTER single-stepping.
795 * This should be unreachable now. If we survive for a while
796 * without anyone hitting this warning, we'll turn this into
797 * an oops.
798 */
08d68323
P
799 tsk->thread.debugreg6 &= ~DR_STEP;
800 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
801 regs->flags &= ~X86_EFLAGS_TF;
1da177e4 802 }
08d68323 803 si_code = get_si_code(tsk->thread.debugreg6);
a1e80faf 804 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
08d68323 805 send_sigtrap(tsk, regs, error_code, si_code);
d99e1bd1 806 cond_local_irq_disable(regs);
42181186 807 debug_stack_usage_dec();
1da177e4 808
6ba3c97a 809exit:
2a41aa4f
AL
810#if defined(CONFIG_X86_32)
811 /*
812 * This is the most likely code path that involves non-trivial use
813 * of the SYSENTER stack. Check that we haven't overrun it.
814 */
815 WARN(this_cpu_read(cpu_tss.SYSENTER_stack_canary) != STACK_END_MAGIC,
816 "Overran or corrupted SYSENTER stack\n");
817#endif
8c84014f 818 ist_exit(regs);
1da177e4 819}
9326638c 820NOKPROBE_SYMBOL(do_debug);
1da177e4
LT
821
822/*
823 * Note that we play around with the 'TS' bit in an attempt to get
824 * the correct behaviour even in the presence of the asynchronous
825 * IRQ13 behaviour
826 */
5e1b05be 827static void math_error(struct pt_regs *regs, int error_code, int trapnr)
1da177e4 828{
e2e75c91 829 struct task_struct *task = current;
e1cebad4 830 struct fpu *fpu = &task->thread.fpu;
1da177e4 831 siginfo_t info;
c9408265
KC
832 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
833 "simd exception";
e2e75c91
BG
834
835 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, SIGFPE) == NOTIFY_STOP)
836 return;
d99e1bd1 837 cond_local_irq_enable(regs);
e2e75c91 838
e1cebad4 839 if (!user_mode(regs)) {
548acf19 840 if (!fixup_exception(regs, trapnr)) {
e2e75c91 841 task->thread.error_code = error_code;
51e7dc70 842 task->thread.trap_nr = trapnr;
e2e75c91
BG
843 die(str, regs, error_code);
844 }
845 return;
846 }
1da177e4
LT
847
848 /*
849 * Save the info for the exception handler and clear the error.
850 */
e1cebad4
IM
851 fpu__save(fpu);
852
853 task->thread.trap_nr = trapnr;
9b6dba9e 854 task->thread.error_code = error_code;
e1cebad4
IM
855 info.si_signo = SIGFPE;
856 info.si_errno = 0;
857 info.si_addr = (void __user *)uprobe_get_trap_addr(regs);
adf77bac 858
e1cebad4 859 info.si_code = fpu__exception_code(fpu, trapnr);
adf77bac 860
e1cebad4
IM
861 /* Retry when we get spurious exceptions: */
862 if (!info.si_code)
c9408265 863 return;
e1cebad4 864
1da177e4
LT
865 force_sig_info(SIGFPE, &info, task);
866}
867
e407d620 868dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
1da177e4 869{
5778077d 870 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
c9408265 871 math_error(regs, error_code, X86_TRAP_MF);
1da177e4
LT
872}
873
e407d620
AH
874dotraplinkage void
875do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
1da177e4 876{
5778077d 877 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
c9408265 878 math_error(regs, error_code, X86_TRAP_XF);
1da177e4
LT
879}
880
e407d620
AH
881dotraplinkage void
882do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
1da177e4 883{
d99e1bd1 884 cond_local_irq_enable(regs);
081f75bb
AH
885}
886
9326638c 887dotraplinkage void
aa78bcfa 888do_device_not_available(struct pt_regs *regs, long error_code)
7643e9b9 889{
bef8b6da
AL
890 unsigned long cr0;
891
5778077d 892 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
304bceda 893
a334fe43 894#ifdef CONFIG_MATH_EMULATION
c6ab109f 895 if (!boot_cpu_has(X86_FEATURE_FPU) && (read_cr0() & X86_CR0_EM)) {
d315760f
TH
896 struct math_emu_info info = { };
897
d99e1bd1 898 cond_local_irq_enable(regs);
d315760f 899
aa78bcfa 900 info.regs = regs;
d315760f 901 math_emulate(&info);
a334fe43 902 return;
7643e9b9 903 }
a334fe43 904#endif
bef8b6da
AL
905
906 /* This should not happen. */
907 cr0 = read_cr0();
908 if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) {
909 /* Try to fix it up and carry on. */
910 write_cr0(cr0 & ~X86_CR0_TS);
911 } else {
912 /*
913 * Something terrible happened, and we're better off trying
914 * to kill the task than getting stuck in a never-ending
915 * loop of #NM faults.
916 */
917 die("unexpected #NM exception", regs, error_code);
918 }
7643e9b9 919}
9326638c 920NOKPROBE_SYMBOL(do_device_not_available);
7643e9b9 921
081f75bb 922#ifdef CONFIG_X86_32
e407d620 923dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
f8e0870f
AH
924{
925 siginfo_t info;
6ba3c97a 926
5778077d 927 RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
f8e0870f
AH
928 local_irq_enable();
929
930 info.si_signo = SIGILL;
931 info.si_errno = 0;
932 info.si_code = ILL_BADSTK;
fc6fcdfb 933 info.si_addr = NULL;
c9408265 934 if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
6ba3c97a
FW
935 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
936 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
937 &info);
938 }
f8e0870f 939}
081f75bb 940#endif
f8e0870f 941
29c84391
JK
942/* Set of traps needed for early debugging. */
943void __init early_trap_init(void)
944{
b4d83270 945 /*
5eca7453
WN
946 * Don't use IST to set DEBUG_STACK as it doesn't work until TSS
947 * is ready in cpu_init() <-- trap_init(). Before trap_init(),
948 * CPU runs at ring 0 so it is impossible to hit an invalid
949 * stack. Using the original stack works well enough at this
950 * early stage. DEBUG_STACK will be equipped after cpu_init() in
b4d83270 951 * trap_init().
5eca7453
WN
952 *
953 * We don't need to set trace_idt_table like set_intr_gate(),
954 * since we don't have trace_debug and it will be reset to
955 * 'debug' in trap_init() by set_intr_gate_ist().
b4d83270 956 */
5eca7453 957 set_intr_gate_notrace(X86_TRAP_DB, debug);
29c84391 958 /* int3 can be called from all */
5eca7453 959 set_system_intr_gate(X86_TRAP_BP, &int3);
8170e6be 960#ifdef CONFIG_X86_32
25c74b10 961 set_intr_gate(X86_TRAP_PF, page_fault);
8170e6be 962#endif
29c84391
JK
963 load_idt(&idt_descr);
964}
965
8170e6be
PA
966void __init early_trap_pf_init(void)
967{
968#ifdef CONFIG_X86_64
25c74b10 969 set_intr_gate(X86_TRAP_PF, page_fault);
8170e6be
PA
970#endif
971}
972
1da177e4
LT
973void __init trap_init(void)
974{
dbeb2be2
RR
975 int i;
976
1da177e4 977#ifdef CONFIG_EISA
927222b1 978 void __iomem *p = early_ioremap(0x0FFFD9, 4);
b5964405
IM
979
980 if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
1da177e4 981 EISA_bus = 1;
927222b1 982 early_iounmap(p, 4);
1da177e4
LT
983#endif
984
25c74b10 985 set_intr_gate(X86_TRAP_DE, divide_error);
c9408265 986 set_intr_gate_ist(X86_TRAP_NMI, &nmi, NMI_STACK);
699d2937 987 /* int4 can be called from all */
c9408265 988 set_system_intr_gate(X86_TRAP_OF, &overflow);
25c74b10
SA
989 set_intr_gate(X86_TRAP_BR, bounds);
990 set_intr_gate(X86_TRAP_UD, invalid_op);
991 set_intr_gate(X86_TRAP_NM, device_not_available);
081f75bb 992#ifdef CONFIG_X86_32
c9408265 993 set_task_gate(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS);
081f75bb 994#else
c9408265 995 set_intr_gate_ist(X86_TRAP_DF, &double_fault, DOUBLEFAULT_STACK);
081f75bb 996#endif
25c74b10
SA
997 set_intr_gate(X86_TRAP_OLD_MF, coprocessor_segment_overrun);
998 set_intr_gate(X86_TRAP_TS, invalid_TSS);
999 set_intr_gate(X86_TRAP_NP, segment_not_present);
6f442be2 1000 set_intr_gate(X86_TRAP_SS, stack_segment);
25c74b10
SA
1001 set_intr_gate(X86_TRAP_GP, general_protection);
1002 set_intr_gate(X86_TRAP_SPURIOUS, spurious_interrupt_bug);
1003 set_intr_gate(X86_TRAP_MF, coprocessor_error);
1004 set_intr_gate(X86_TRAP_AC, alignment_check);
1da177e4 1005#ifdef CONFIG_X86_MCE
c9408265 1006 set_intr_gate_ist(X86_TRAP_MC, &machine_check, MCE_STACK);
1da177e4 1007#endif
25c74b10 1008 set_intr_gate(X86_TRAP_XF, simd_coprocessor_error);
1da177e4 1009
bb3f0b59
YL
1010 /* Reserve all the builtin and the syscall vector: */
1011 for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
1012 set_bit(i, used_vectors);
1013
081f75bb 1014#ifdef CONFIG_IA32_EMULATION
2cd23553 1015 set_system_intr_gate(IA32_SYSCALL_VECTOR, entry_INT80_compat);
bb3f0b59 1016 set_bit(IA32_SYSCALL_VECTOR, used_vectors);
081f75bb
AH
1017#endif
1018
1019#ifdef CONFIG_X86_32
a798f091 1020 set_system_intr_gate(IA32_SYSCALL_VECTOR, entry_INT80_32);
51bb9284 1021 set_bit(IA32_SYSCALL_VECTOR, used_vectors);
081f75bb 1022#endif
bb3f0b59 1023
4eefbe79
KC
1024 /*
1025 * Set the IDT descriptor to a fixed read-only location, so that the
1026 * "sidt" instruction will not leak the location of the kernel, and
1027 * to defend the IDT against arbitrary memory write vulnerabilities.
1028 * It will be reloaded in cpu_init() */
1029 __set_fixmap(FIX_RO_IDT, __pa_symbol(idt_table), PAGE_KERNEL_RO);
1030 idt_descr.address = fix_to_virt(FIX_RO_IDT);
1031
1da177e4 1032 /*
b5964405 1033 * Should be a barrier for any external CPU state:
1da177e4
LT
1034 */
1035 cpu_init();
1036
b4d83270
WN
1037 /*
1038 * X86_TRAP_DB and X86_TRAP_BP have been set
5eca7453 1039 * in early_trap_init(). However, ITS works only after
b4d83270
WN
1040 * cpu_init() loads TSS. See comments in early_trap_init().
1041 */
1042 set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
1043 /* int3 can be called from all */
1044 set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
1045
428cf902 1046 x86_init.irqs.trap_init();
228bdaa9
SR
1047
1048#ifdef CONFIG_X86_64
629f4f9d 1049 memcpy(&debug_idt_table, &idt_table, IDT_ENTRIES * 16);
c9408265
KC
1050 set_nmi_gate(X86_TRAP_DB, &debug);
1051 set_nmi_gate(X86_TRAP_BP, &int3);
228bdaa9 1052#endif
1da177e4 1053}