]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/traps.c
Merge tag 'pr-20150114-x86-entry' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-bionic-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>
b5964405
IM
24#include <linux/module.h>
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>
1da177e4 32#include <linux/timer.h>
1da177e4 33#include <linux/init.h>
91768d6c 34#include <linux/bug.h>
b5964405
IM
35#include <linux/nmi.h>
36#include <linux/mm.h>
c1d518c8
AH
37#include <linux/smp.h>
38#include <linux/io.h>
1da177e4
LT
39
40#ifdef CONFIG_EISA
41#include <linux/ioport.h>
42#include <linux/eisa.h>
43#endif
44
c0d12172
DJ
45#if defined(CONFIG_EDAC)
46#include <linux/edac.h>
47#endif
48
f8561296 49#include <asm/kmemcheck.h>
b5964405 50#include <asm/stacktrace.h>
1da177e4 51#include <asm/processor.h>
1da177e4 52#include <asm/debugreg.h>
60063497 53#include <linux/atomic.h>
08d636b6 54#include <asm/ftrace.h>
c1d518c8 55#include <asm/traps.h>
1da177e4
LT
56#include <asm/desc.h>
57#include <asm/i387.h>
1361b83a 58#include <asm/fpu-internal.h>
9e55e44e 59#include <asm/mce.h>
4eefbe79 60#include <asm/fixmap.h>
1164dd00 61#include <asm/mach_traps.h>
17f41571 62#include <asm/alternative.h>
fe3d197f 63#include <asm/mpx.h>
c1d518c8 64
081f75bb 65#ifdef CONFIG_X86_64
428cf902 66#include <asm/x86_init.h>
081f75bb
AH
67#include <asm/pgalloc.h>
68#include <asm/proto.h>
4df05f36
KC
69
70/* No need to be aligned, but done to keep all IDTs defined the same way. */
71gate_desc debug_idt_table[NR_VECTORS] __page_aligned_bss;
081f75bb 72#else
c1d518c8 73#include <asm/processor-flags.h>
8e6dafd6 74#include <asm/setup.h>
1da177e4 75
1da177e4 76asmlinkage int system_call(void);
081f75bb 77#endif
1da177e4 78
4df05f36
KC
79/* Must be page-aligned because the real IDT is used in a fixmap. */
80gate_desc idt_table[NR_VECTORS] __page_aligned_bss;
81
b77b881f
YL
82DECLARE_BITMAP(used_vectors, NR_VECTORS);
83EXPORT_SYMBOL_GPL(used_vectors);
84
762db434
AH
85static inline void conditional_sti(struct pt_regs *regs)
86{
87 if (regs->flags & X86_EFLAGS_IF)
88 local_irq_enable();
89}
90
3d2a71a5
AH
91static inline void preempt_conditional_sti(struct pt_regs *regs)
92{
bdb43806 93 preempt_count_inc();
3d2a71a5
AH
94 if (regs->flags & X86_EFLAGS_IF)
95 local_irq_enable();
96}
97
be716615
TG
98static inline void conditional_cli(struct pt_regs *regs)
99{
100 if (regs->flags & X86_EFLAGS_IF)
101 local_irq_disable();
102}
103
3d2a71a5
AH
104static inline void preempt_conditional_cli(struct pt_regs *regs)
105{
106 if (regs->flags & X86_EFLAGS_IF)
107 local_irq_disable();
bdb43806 108 preempt_count_dec();
3d2a71a5
AH
109}
110
95927475
AL
111enum ctx_state ist_enter(struct pt_regs *regs)
112{
113 /*
114 * We are atomic because we're on the IST stack (or we're on x86_32,
115 * in which case we still shouldn't schedule.
116 */
117 preempt_count_add(HARDIRQ_OFFSET);
118
119 if (user_mode_vm(regs)) {
120 /* Other than that, we're just an exception. */
121 return exception_enter();
122 } else {
123 /*
124 * We might have interrupted pretty much anything. In
125 * fact, if we're a machine check, we can even interrupt
126 * NMI processing. We don't want in_nmi() to return true,
127 * but we need to notify RCU.
128 */
129 rcu_nmi_enter();
130 return IN_KERNEL; /* the value is irrelevant. */
131 }
132}
133
134void ist_exit(struct pt_regs *regs, enum ctx_state prev_state)
135{
136 preempt_count_sub(HARDIRQ_OFFSET);
137
138 if (user_mode_vm(regs))
139 return exception_exit(prev_state);
140 else
141 rcu_nmi_exit();
142}
143
bced35b6
AL
144/**
145 * ist_begin_non_atomic() - begin a non-atomic section in an IST exception
146 * @regs: regs passed to the IST exception handler
147 *
148 * IST exception handlers normally cannot schedule. As a special
149 * exception, if the exception interrupted userspace code (i.e.
150 * user_mode_vm(regs) would return true) and the exception was not
151 * a double fault, it can be safe to schedule. ist_begin_non_atomic()
152 * begins a non-atomic section within an ist_enter()/ist_exit() region.
153 * Callers are responsible for enabling interrupts themselves inside
154 * the non-atomic section, and callers must call is_end_non_atomic()
155 * before ist_exit().
156 */
157void ist_begin_non_atomic(struct pt_regs *regs)
158{
159 BUG_ON(!user_mode_vm(regs));
160
161 /*
162 * Sanity check: we need to be on the normal thread stack. This
163 * will catch asm bugs and any attempt to use ist_preempt_enable
164 * from double_fault.
165 */
166 BUG_ON(((current_stack_pointer() ^ this_cpu_read_stable(kernel_stack))
167 & ~(THREAD_SIZE - 1)) != 0);
168
169 preempt_count_sub(HARDIRQ_OFFSET);
170}
171
172/**
173 * ist_end_non_atomic() - begin a non-atomic section in an IST exception
174 *
175 * Ends a non-atomic section started with ist_begin_non_atomic().
176 */
177void ist_end_non_atomic(void)
178{
179 preempt_count_add(HARDIRQ_OFFSET);
180}
181
9326638c 182static nokprobe_inline int
c416ddf5
FW
183do_trap_no_signal(struct task_struct *tsk, int trapnr, char *str,
184 struct pt_regs *regs, long error_code)
1da177e4 185{
081f75bb 186#ifdef CONFIG_X86_32
6b6891f9 187 if (regs->flags & X86_VM_MASK) {
3c1326f8 188 /*
c416ddf5 189 * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
3c1326f8
AH
190 * On nmi (interrupt 2), do_trap should not be called.
191 */
c416ddf5
FW
192 if (trapnr < X86_TRAP_UD) {
193 if (!handle_vm86_trap((struct kernel_vm86_regs *) regs,
194 error_code, trapnr))
195 return 0;
196 }
197 return -1;
1da177e4 198 }
081f75bb 199#endif
c416ddf5
FW
200 if (!user_mode(regs)) {
201 if (!fixup_exception(regs)) {
202 tsk->thread.error_code = error_code;
203 tsk->thread.trap_nr = trapnr;
204 die(str, regs, error_code);
205 }
206 return 0;
207 }
1da177e4 208
c416ddf5
FW
209 return -1;
210}
1da177e4 211
1c326c4d
ON
212static siginfo_t *fill_trap_info(struct pt_regs *regs, int signr, int trapnr,
213 siginfo_t *info)
958d3d72
ON
214{
215 unsigned long siaddr;
216 int sicode;
217
218 switch (trapnr) {
1c326c4d
ON
219 default:
220 return SEND_SIG_PRIV;
221
958d3d72
ON
222 case X86_TRAP_DE:
223 sicode = FPE_INTDIV;
b02ef20a 224 siaddr = uprobe_get_trap_addr(regs);
958d3d72
ON
225 break;
226 case X86_TRAP_UD:
227 sicode = ILL_ILLOPN;
b02ef20a 228 siaddr = uprobe_get_trap_addr(regs);
958d3d72
ON
229 break;
230 case X86_TRAP_AC:
231 sicode = BUS_ADRALN;
232 siaddr = 0;
233 break;
234 }
235
236 info->si_signo = signr;
237 info->si_errno = 0;
238 info->si_code = sicode;
239 info->si_addr = (void __user *)siaddr;
1c326c4d 240 return info;
958d3d72
ON
241}
242
9326638c 243static void
c416ddf5
FW
244do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
245 long error_code, siginfo_t *info)
246{
247 struct task_struct *tsk = current;
248
249
250 if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code))
251 return;
b5964405 252 /*
51e7dc70 253 * We want error_code and trap_nr set for userspace faults and
b5964405
IM
254 * kernelspace faults which result in die(), but not
255 * kernelspace faults which are fixed up. die() gives the
256 * process no chance to handle the signal and notice the
257 * kernel fault information, so that won't result in polluting
258 * the information about previously queued, but not yet
259 * delivered, faults. See also do_general_protection below.
260 */
261 tsk->thread.error_code = error_code;
51e7dc70 262 tsk->thread.trap_nr = trapnr;
d1895183 263
081f75bb
AH
264#ifdef CONFIG_X86_64
265 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
266 printk_ratelimit()) {
c767a54b
JP
267 pr_info("%s[%d] trap %s ip:%lx sp:%lx error:%lx",
268 tsk->comm, tsk->pid, str,
269 regs->ip, regs->sp, error_code);
081f75bb 270 print_vma_addr(" in ", regs->ip);
c767a54b 271 pr_cont("\n");
081f75bb
AH
272 }
273#endif
274
38cad57b 275 force_sig_info(signr, info ?: SEND_SIG_PRIV, tsk);
1da177e4 276}
9326638c 277NOKPROBE_SYMBOL(do_trap);
1da177e4 278
dff0796e 279static void do_error_trap(struct pt_regs *regs, long error_code, char *str,
1c326c4d 280 unsigned long trapnr, int signr)
dff0796e
ON
281{
282 enum ctx_state prev_state = exception_enter();
1c326c4d 283 siginfo_t info;
dff0796e
ON
284
285 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) !=
286 NOTIFY_STOP) {
287 conditional_sti(regs);
1c326c4d
ON
288 do_trap(trapnr, signr, str, regs, error_code,
289 fill_trap_info(regs, signr, trapnr, &info));
dff0796e
ON
290 }
291
292 exception_exit(prev_state);
293}
294
b5964405 295#define DO_ERROR(trapnr, signr, str, name) \
e407d620 296dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
b5964405 297{ \
1c326c4d 298 do_error_trap(regs, error_code, str, trapnr, signr); \
1da177e4
LT
299}
300
0eb14833
ON
301DO_ERROR(X86_TRAP_DE, SIGFPE, "divide error", divide_error)
302DO_ERROR(X86_TRAP_OF, SIGSEGV, "overflow", overflow)
0eb14833
ON
303DO_ERROR(X86_TRAP_UD, SIGILL, "invalid opcode", invalid_op)
304DO_ERROR(X86_TRAP_OLD_MF, SIGFPE, "coprocessor segment overrun",coprocessor_segment_overrun)
305DO_ERROR(X86_TRAP_TS, SIGSEGV, "invalid TSS", invalid_TSS)
306DO_ERROR(X86_TRAP_NP, SIGBUS, "segment not present", segment_not_present)
0eb14833 307DO_ERROR(X86_TRAP_SS, SIGBUS, "stack segment", stack_segment)
0eb14833 308DO_ERROR(X86_TRAP_AC, SIGBUS, "alignment check", alignment_check)
1da177e4 309
081f75bb
AH
310#ifdef CONFIG_X86_64
311/* Runs on IST stack */
081f75bb
AH
312dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
313{
314 static const char str[] = "double fault";
315 struct task_struct *tsk = current;
316
af726f21
AL
317#ifdef CONFIG_X86_ESPFIX64
318 extern unsigned char native_irq_return_iret[];
319
320 /*
321 * If IRET takes a non-IST fault on the espfix64 stack, then we
322 * end up promoting it to a doublefault. In that case, modify
323 * the stack to make it look like we just entered the #GP
324 * handler from user space, similar to bad_iret.
95927475
AL
325 *
326 * No need for ist_enter here because we don't use RCU.
af726f21
AL
327 */
328 if (((long)regs->sp >> PGDIR_SHIFT) == ESPFIX_PGD_ENTRY &&
329 regs->cs == __KERNEL_CS &&
330 regs->ip == (unsigned long)native_irq_return_iret)
331 {
332 struct pt_regs *normal_regs = task_pt_regs(current);
333
334 /* Fake a #GP(0) from userspace. */
335 memmove(&normal_regs->ip, (void *)regs->sp, 5*8);
336 normal_regs->orig_ax = 0; /* Missing (lost) #GP error code */
337 regs->ip = (unsigned long)general_protection;
338 regs->sp = (unsigned long)&normal_regs->orig_ax;
95927475 339
af726f21
AL
340 return;
341 }
342#endif
343
95927475 344 ist_enter(regs); /* Discard prev_state because we won't return. */
c9408265 345 notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
081f75bb
AH
346
347 tsk->thread.error_code = error_code;
51e7dc70 348 tsk->thread.trap_nr = X86_TRAP_DF;
081f75bb 349
4d067d8e
BP
350#ifdef CONFIG_DOUBLEFAULT
351 df_debug(regs, error_code);
352#endif
bd8b96df
IM
353 /*
354 * This is always a kernel trap and never fixable (and thus must
355 * never return).
356 */
081f75bb
AH
357 for (;;)
358 die(str, regs, error_code);
359}
360#endif
361
fe3d197f
DH
362dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)
363{
364 struct task_struct *tsk = current;
365 struct xsave_struct *xsave_buf;
366 enum ctx_state prev_state;
367 struct bndcsr *bndcsr;
368 siginfo_t *info;
369
370 prev_state = exception_enter();
371 if (notify_die(DIE_TRAP, "bounds", regs, error_code,
372 X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
373 goto exit;
374 conditional_sti(regs);
375
376 if (!user_mode(regs))
377 die("bounds", regs, error_code);
378
379 if (!cpu_feature_enabled(X86_FEATURE_MPX)) {
380 /* The exception is not from Intel MPX */
381 goto exit_trap;
382 }
383
384 /*
385 * We need to look at BNDSTATUS to resolve this exception.
386 * It is not directly accessible, though, so we need to
387 * do an xsave and then pull it out of the xsave buffer.
388 */
389 fpu_save_init(&tsk->thread.fpu);
390 xsave_buf = &(tsk->thread.fpu.state->xsave);
391 bndcsr = get_xsave_addr(xsave_buf, XSTATE_BNDCSR);
392 if (!bndcsr)
393 goto exit_trap;
394
395 /*
396 * The error code field of the BNDSTATUS register communicates status
397 * information of a bound range exception #BR or operation involving
398 * bound directory.
399 */
400 switch (bndcsr->bndstatus & MPX_BNDSTA_ERROR_CODE) {
401 case 2: /* Bound directory has invalid entry. */
402 if (mpx_handle_bd_fault(xsave_buf))
403 goto exit_trap;
404 break; /* Success, it was handled */
405 case 1: /* Bound violation. */
406 info = mpx_generate_siginfo(regs, xsave_buf);
e10abb2f 407 if (IS_ERR(info)) {
fe3d197f
DH
408 /*
409 * We failed to decode the MPX instruction. Act as if
410 * the exception was not caused by MPX.
411 */
412 goto exit_trap;
413 }
414 /*
415 * Success, we decoded the instruction and retrieved
416 * an 'info' containing the address being accessed
417 * which caused the exception. This information
418 * allows and application to possibly handle the
419 * #BR exception itself.
420 */
421 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, info);
422 kfree(info);
423 break;
424 case 0: /* No exception caused by Intel MPX operations. */
425 goto exit_trap;
426 default:
427 die("bounds", regs, error_code);
428 }
429
430exit:
431 exception_exit(prev_state);
432 return;
433exit_trap:
434 /*
435 * This path out is for all the cases where we could not
436 * handle the exception in some way (like allocating a
437 * table or telling userspace about it. We will also end
438 * up here if the kernel has MPX turned off at compile
439 * time..
440 */
441 do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, NULL);
442 exception_exit(prev_state);
443}
444
9326638c 445dotraplinkage void
13485ab5 446do_general_protection(struct pt_regs *regs, long error_code)
1da177e4 447{
13485ab5 448 struct task_struct *tsk;
6c1e0256 449 enum ctx_state prev_state;
b5964405 450
6c1e0256 451 prev_state = exception_enter();
c6df0d71
AH
452 conditional_sti(regs);
453
081f75bb 454#ifdef CONFIG_X86_32
ef3f6288
FW
455 if (regs->flags & X86_VM_MASK) {
456 local_irq_enable();
457 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
6ba3c97a 458 goto exit;
ef3f6288 459 }
081f75bb 460#endif
1da177e4 461
13485ab5 462 tsk = current;
ef3f6288
FW
463 if (!user_mode(regs)) {
464 if (fixup_exception(regs))
6ba3c97a 465 goto exit;
ef3f6288
FW
466
467 tsk->thread.error_code = error_code;
468 tsk->thread.trap_nr = X86_TRAP_GP;
6ba3c97a
FW
469 if (notify_die(DIE_GPF, "general protection fault", regs, error_code,
470 X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP)
ef3f6288 471 die("general protection fault", regs, error_code);
6ba3c97a 472 goto exit;
ef3f6288 473 }
1da177e4 474
13485ab5 475 tsk->thread.error_code = error_code;
51e7dc70 476 tsk->thread.trap_nr = X86_TRAP_GP;
b5964405 477
13485ab5
AH
478 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
479 printk_ratelimit()) {
c767a54b 480 pr_info("%s[%d] general protection ip:%lx sp:%lx error:%lx",
13485ab5
AH
481 tsk->comm, task_pid_nr(tsk),
482 regs->ip, regs->sp, error_code);
03252919 483 print_vma_addr(" in ", regs->ip);
c767a54b 484 pr_cont("\n");
03252919 485 }
abd4f750 486
38cad57b 487 force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
6ba3c97a 488exit:
6c1e0256 489 exception_exit(prev_state);
1da177e4 490}
9326638c 491NOKPROBE_SYMBOL(do_general_protection);
1da177e4 492
c1d518c8 493/* May run on IST stack. */
9326638c 494dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
1da177e4 495{
6c1e0256
FW
496 enum ctx_state prev_state;
497
08d636b6 498#ifdef CONFIG_DYNAMIC_FTRACE
a192cd04
SR
499 /*
500 * ftrace must be first, everything else may cause a recursive crash.
501 * See note by declaration of modifying_ftrace_code in ftrace.c
502 */
503 if (unlikely(atomic_read(&modifying_ftrace_code)) &&
504 ftrace_int3_handler(regs))
08d636b6
SR
505 return;
506#endif
17f41571
JK
507 if (poke_int3_handler(regs))
508 return;
509
95927475 510 prev_state = ist_enter(regs);
f503b5ae 511#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
c9408265
KC
512 if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
513 SIGTRAP) == NOTIFY_STOP)
6ba3c97a 514 goto exit;
f503b5ae 515#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
cc3a1bf5 516
6f6343f5
MH
517#ifdef CONFIG_KPROBES
518 if (kprobe_int3_handler(regs))
4cdf77a8 519 goto exit;
6f6343f5
MH
520#endif
521
c9408265
KC
522 if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
523 SIGTRAP) == NOTIFY_STOP)
6ba3c97a 524 goto exit;
b5964405 525
42181186
SR
526 /*
527 * Let others (NMI) know that the debug stack is in use
528 * as we may switch to the interrupt stack.
529 */
530 debug_stack_usage_inc();
4915a35e 531 preempt_conditional_sti(regs);
c9408265 532 do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
4915a35e 533 preempt_conditional_cli(regs);
42181186 534 debug_stack_usage_dec();
6ba3c97a 535exit:
95927475 536 ist_exit(regs, prev_state);
1da177e4 537}
9326638c 538NOKPROBE_SYMBOL(do_int3);
1da177e4 539
081f75bb 540#ifdef CONFIG_X86_64
bd8b96df 541/*
48e08d0f
AL
542 * Help handler running on IST stack to switch off the IST stack if the
543 * interrupted code was in user mode. The actual stack switch is done in
544 * entry_64.S
bd8b96df 545 */
7ddc6a21 546asmlinkage __visible notrace struct pt_regs *sync_regs(struct pt_regs *eregs)
081f75bb 547{
48e08d0f
AL
548 struct pt_regs *regs = task_pt_regs(current);
549 *regs = *eregs;
081f75bb
AH
550 return regs;
551}
9326638c 552NOKPROBE_SYMBOL(sync_regs);
b645af2d
AL
553
554struct bad_iret_stack {
555 void *error_entry_ret;
556 struct pt_regs regs;
557};
558
7ddc6a21 559asmlinkage __visible notrace
b645af2d
AL
560struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
561{
562 /*
563 * This is called from entry_64.S early in handling a fault
564 * caused by a bad iret to user mode. To handle the fault
565 * correctly, we want move our stack frame to task_pt_regs
566 * and we want to pretend that the exception came from the
567 * iret target.
568 */
569 struct bad_iret_stack *new_stack =
570 container_of(task_pt_regs(current),
571 struct bad_iret_stack, regs);
572
573 /* Copy the IRET target to the new stack. */
574 memmove(&new_stack->regs.ip, (void *)s->regs.sp, 5*8);
575
576 /* Copy the remainder of the stack from the current stack. */
577 memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip));
578
579 BUG_ON(!user_mode_vm(&new_stack->regs));
580 return new_stack;
581}
7ddc6a21 582NOKPROBE_SYMBOL(fixup_bad_iret);
081f75bb
AH
583#endif
584
1da177e4
LT
585/*
586 * Our handling of the processor debug registers is non-trivial.
587 * We do not clear them on entry and exit from the kernel. Therefore
588 * it is possible to get a watchpoint trap here from inside the kernel.
589 * However, the code in ./ptrace.c has ensured that the user can
590 * only set watchpoints on userspace addresses. Therefore the in-kernel
591 * watchpoint trap can only occur in code which is reading/writing
592 * from user space. Such code must not hold kernel locks (since it
593 * can equally take a page fault), therefore it is safe to call
594 * force_sig_info even though that claims and releases locks.
b5964405 595 *
1da177e4
LT
596 * Code in ./signal.c ensures that the debug control register
597 * is restored before we deliver any signal, and therefore that
598 * user code runs with the correct debug control register even though
599 * we clear it here.
600 *
601 * Being careful here means that we don't have to be as careful in a
602 * lot of more complicated places (task switching can be a bit lazy
603 * about restoring all the debug state, and ptrace doesn't have to
604 * find every occurrence of the TF bit that could be saved away even
605 * by user code)
c1d518c8
AH
606 *
607 * May run on IST stack.
1da177e4 608 */
9326638c 609dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
1da177e4 610{
1da177e4 611 struct task_struct *tsk = current;
6c1e0256 612 enum ctx_state prev_state;
a1e80faf 613 int user_icebp = 0;
08d68323 614 unsigned long dr6;
da654b74 615 int si_code;
1da177e4 616
95927475 617 prev_state = ist_enter(regs);
4cdf77a8 618
08d68323 619 get_debugreg(dr6, 6);
1da177e4 620
40f9249a
P
621 /* Filter out all the reserved bits which are preset to 1 */
622 dr6 &= ~DR6_RESERVED;
623
a1e80faf
FW
624 /*
625 * If dr6 has no reason to give us about the origin of this trap,
626 * then it's very likely the result of an icebp/int01 trap.
627 * User wants a sigtrap for that.
628 */
629 if (!dr6 && user_mode(regs))
630 user_icebp = 1;
631
f8561296 632 /* Catch kmemcheck conditions first of all! */
eadb8a09 633 if ((dr6 & DR_STEP) && kmemcheck_trap(regs))
6ba3c97a 634 goto exit;
f8561296 635
08d68323
P
636 /* DR6 may or may not be cleared by the CPU */
637 set_debugreg(0, 6);
10faa81e 638
ea8e61b7
PZ
639 /*
640 * The processor cleared BTF, so don't mark that we need it set.
641 */
642 clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
643
08d68323
P
644 /* Store the virtualized DR6 value */
645 tsk->thread.debugreg6 = dr6;
646
6f6343f5
MH
647#ifdef CONFIG_KPROBES
648 if (kprobe_debug_handler(regs))
649 goto exit;
650#endif
651
5a802e15 652 if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
62edab90 653 SIGTRAP) == NOTIFY_STOP)
6ba3c97a 654 goto exit;
3d2a71a5 655
42181186
SR
656 /*
657 * Let others (NMI) know that the debug stack is in use
658 * as we may switch to the interrupt stack.
659 */
660 debug_stack_usage_inc();
661
1da177e4 662 /* It's safe to allow irq's after DR6 has been saved */
3d2a71a5 663 preempt_conditional_sti(regs);
1da177e4 664
08d68323 665 if (regs->flags & X86_VM_MASK) {
c9408265
KC
666 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
667 X86_TRAP_DB);
6554287b 668 preempt_conditional_cli(regs);
42181186 669 debug_stack_usage_dec();
6ba3c97a 670 goto exit;
1da177e4
LT
671 }
672
1da177e4 673 /*
08d68323
P
674 * Single-stepping through system calls: ignore any exceptions in
675 * kernel space, but re-enable TF when returning to user mode.
676 *
677 * We already checked v86 mode above, so we can check for kernel mode
678 * by just checking the CPL of CS.
1da177e4 679 */
08d68323
P
680 if ((dr6 & DR_STEP) && !user_mode(regs)) {
681 tsk->thread.debugreg6 &= ~DR_STEP;
682 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
683 regs->flags &= ~X86_EFLAGS_TF;
1da177e4 684 }
08d68323 685 si_code = get_si_code(tsk->thread.debugreg6);
a1e80faf 686 if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
08d68323 687 send_sigtrap(tsk, regs, error_code, si_code);
3d2a71a5 688 preempt_conditional_cli(regs);
42181186 689 debug_stack_usage_dec();
1da177e4 690
6ba3c97a 691exit:
95927475 692 ist_exit(regs, prev_state);
1da177e4 693}
9326638c 694NOKPROBE_SYMBOL(do_debug);
1da177e4
LT
695
696/*
697 * Note that we play around with the 'TS' bit in an attempt to get
698 * the correct behaviour even in the presence of the asynchronous
699 * IRQ13 behaviour
700 */
5e1b05be 701static void math_error(struct pt_regs *regs, int error_code, int trapnr)
1da177e4 702{
e2e75c91 703 struct task_struct *task = current;
1da177e4 704 siginfo_t info;
9b6dba9e 705 unsigned short err;
c9408265
KC
706 char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
707 "simd exception";
e2e75c91
BG
708
709 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, SIGFPE) == NOTIFY_STOP)
710 return;
711 conditional_sti(regs);
712
713 if (!user_mode_vm(regs))
714 {
715 if (!fixup_exception(regs)) {
716 task->thread.error_code = error_code;
51e7dc70 717 task->thread.trap_nr = trapnr;
e2e75c91
BG
718 die(str, regs, error_code);
719 }
720 return;
721 }
1da177e4
LT
722
723 /*
724 * Save the info for the exception handler and clear the error.
725 */
1da177e4 726 save_init_fpu(task);
51e7dc70 727 task->thread.trap_nr = trapnr;
9b6dba9e 728 task->thread.error_code = error_code;
1da177e4
LT
729 info.si_signo = SIGFPE;
730 info.si_errno = 0;
b02ef20a 731 info.si_addr = (void __user *)uprobe_get_trap_addr(regs);
c9408265 732 if (trapnr == X86_TRAP_MF) {
9b6dba9e
BG
733 unsigned short cwd, swd;
734 /*
735 * (~cwd & swd) will mask out exceptions that are not set to unmasked
736 * status. 0x3f is the exception bits in these regs, 0x200 is the
737 * C1 reg you need in case of a stack fault, 0x040 is the stack
738 * fault bit. We should only be taking one exception at a time,
739 * so if this combination doesn't produce any single exception,
740 * then we have a bad program that isn't synchronizing its FPU usage
741 * and it will suffer the consequences since we won't be able to
742 * fully reproduce the context of the exception
743 */
744 cwd = get_fpu_cwd(task);
745 swd = get_fpu_swd(task);
adf77bac 746
9b6dba9e
BG
747 err = swd & ~cwd;
748 } else {
749 /*
750 * The SIMD FPU exceptions are handled a little differently, as there
751 * is only a single status/control register. Thus, to determine which
752 * unmasked exception was caught we must mask the exception mask bits
753 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
754 */
755 unsigned short mxcsr = get_fpu_mxcsr(task);
756 err = ~(mxcsr >> 7) & mxcsr;
757 }
adf77bac
PA
758
759 if (err & 0x001) { /* Invalid op */
b5964405
IM
760 /*
761 * swd & 0x240 == 0x040: Stack Underflow
762 * swd & 0x240 == 0x240: Stack Overflow
763 * User must clear the SF bit (0x40) if set
764 */
765 info.si_code = FPE_FLTINV;
adf77bac 766 } else if (err & 0x004) { /* Divide by Zero */
b5964405 767 info.si_code = FPE_FLTDIV;
adf77bac 768 } else if (err & 0x008) { /* Overflow */
b5964405 769 info.si_code = FPE_FLTOVF;
adf77bac
PA
770 } else if (err & 0x012) { /* Denormal, Underflow */
771 info.si_code = FPE_FLTUND;
772 } else if (err & 0x020) { /* Precision */
b5964405 773 info.si_code = FPE_FLTRES;
adf77bac 774 } else {
bd8b96df 775 /*
c9408265
KC
776 * If we're using IRQ 13, or supposedly even some trap
777 * X86_TRAP_MF implementations, it's possible
778 * we get a spurious trap, which is not an error.
bd8b96df 779 */
c9408265 780 return;
1da177e4
LT
781 }
782 force_sig_info(SIGFPE, &info, task);
783}
784
e407d620 785dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
1da177e4 786{
6c1e0256
FW
787 enum ctx_state prev_state;
788
789 prev_state = exception_enter();
c9408265 790 math_error(regs, error_code, X86_TRAP_MF);
6c1e0256 791 exception_exit(prev_state);
1da177e4
LT
792}
793
e407d620
AH
794dotraplinkage void
795do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
1da177e4 796{
6c1e0256
FW
797 enum ctx_state prev_state;
798
799 prev_state = exception_enter();
c9408265 800 math_error(regs, error_code, X86_TRAP_XF);
6c1e0256 801 exception_exit(prev_state);
1da177e4
LT
802}
803
e407d620
AH
804dotraplinkage void
805do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
1da177e4 806{
cf81978d 807 conditional_sti(regs);
1da177e4
LT
808#if 0
809 /* No need to warn about this any longer. */
c767a54b 810 pr_info("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
1da177e4
LT
811#endif
812}
813
2605fc21 814asmlinkage __visible void __attribute__((weak)) smp_thermal_interrupt(void)
1da177e4 815{
1da177e4 816}
4efc0670 817
2605fc21 818asmlinkage __visible void __attribute__((weak)) smp_threshold_interrupt(void)
081f75bb
AH
819{
820}
821
1da177e4 822/*
b5964405 823 * 'math_state_restore()' saves the current math information in the
1da177e4
LT
824 * old math state array, and gets the new ones from the current task
825 *
826 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
827 * Don't touch unless you *really* know how it works.
828 *
be98c2cd
LT
829 * Must be called with kernel preemption disabled (eg with local
830 * local interrupts as in the case of do_device_not_available).
1da177e4 831 */
be98c2cd 832void math_state_restore(void)
1da177e4 833{
f94edacf 834 struct task_struct *tsk = current;
1da177e4 835
aa283f49
SS
836 if (!tsk_used_math(tsk)) {
837 local_irq_enable();
838 /*
839 * does a slab alloc which can sleep
840 */
841 if (init_fpu(tsk)) {
842 /*
843 * ran out of memory!
844 */
845 do_group_exit(SIGKILL);
846 return;
847 }
848 local_irq_disable();
849 }
850
f94edacf 851 __thread_fpu_begin(tsk);
304bceda 852
80ab6f1e
LT
853 /*
854 * Paranoid restore. send a SIGSEGV if we fail to restore the state.
855 */
856 if (unlikely(restore_fpu_checking(tsk))) {
304bceda 857 drop_init_fpu(tsk);
38cad57b 858 force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
80ab6f1e
LT
859 return;
860 }
b3b0870e 861
c375f15a 862 tsk->thread.fpu_counter++;
1da177e4 863}
5992b6da 864EXPORT_SYMBOL_GPL(math_state_restore);
1da177e4 865
9326638c 866dotraplinkage void
aa78bcfa 867do_device_not_available(struct pt_regs *regs, long error_code)
7643e9b9 868{
6c1e0256
FW
869 enum ctx_state prev_state;
870
871 prev_state = exception_enter();
5d2bd700 872 BUG_ON(use_eager_fpu());
304bceda 873
a334fe43 874#ifdef CONFIG_MATH_EMULATION
7643e9b9 875 if (read_cr0() & X86_CR0_EM) {
d315760f
TH
876 struct math_emu_info info = { };
877
7643e9b9 878 conditional_sti(regs);
d315760f 879
aa78bcfa 880 info.regs = regs;
d315760f 881 math_emulate(&info);
6c1e0256 882 exception_exit(prev_state);
a334fe43 883 return;
7643e9b9 884 }
a334fe43
BG
885#endif
886 math_state_restore(); /* interrupts still off */
887#ifdef CONFIG_X86_32
888 conditional_sti(regs);
081f75bb 889#endif
6c1e0256 890 exception_exit(prev_state);
7643e9b9 891}
9326638c 892NOKPROBE_SYMBOL(do_device_not_available);
7643e9b9 893
081f75bb 894#ifdef CONFIG_X86_32
e407d620 895dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
f8e0870f
AH
896{
897 siginfo_t info;
6c1e0256 898 enum ctx_state prev_state;
6ba3c97a 899
6c1e0256 900 prev_state = exception_enter();
f8e0870f
AH
901 local_irq_enable();
902
903 info.si_signo = SIGILL;
904 info.si_errno = 0;
905 info.si_code = ILL_BADSTK;
fc6fcdfb 906 info.si_addr = NULL;
c9408265 907 if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
6ba3c97a
FW
908 X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
909 do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
910 &info);
911 }
6c1e0256 912 exception_exit(prev_state);
f8e0870f 913}
081f75bb 914#endif
f8e0870f 915
29c84391
JK
916/* Set of traps needed for early debugging. */
917void __init early_trap_init(void)
918{
c9408265 919 set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
29c84391 920 /* int3 can be called from all */
c9408265 921 set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
8170e6be 922#ifdef CONFIG_X86_32
25c74b10 923 set_intr_gate(X86_TRAP_PF, page_fault);
8170e6be 924#endif
29c84391
JK
925 load_idt(&idt_descr);
926}
927
8170e6be
PA
928void __init early_trap_pf_init(void)
929{
930#ifdef CONFIG_X86_64
25c74b10 931 set_intr_gate(X86_TRAP_PF, page_fault);
8170e6be
PA
932#endif
933}
934
1da177e4
LT
935void __init trap_init(void)
936{
dbeb2be2
RR
937 int i;
938
1da177e4 939#ifdef CONFIG_EISA
927222b1 940 void __iomem *p = early_ioremap(0x0FFFD9, 4);
b5964405
IM
941
942 if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
1da177e4 943 EISA_bus = 1;
927222b1 944 early_iounmap(p, 4);
1da177e4
LT
945#endif
946
25c74b10 947 set_intr_gate(X86_TRAP_DE, divide_error);
c9408265 948 set_intr_gate_ist(X86_TRAP_NMI, &nmi, NMI_STACK);
699d2937 949 /* int4 can be called from all */
c9408265 950 set_system_intr_gate(X86_TRAP_OF, &overflow);
25c74b10
SA
951 set_intr_gate(X86_TRAP_BR, bounds);
952 set_intr_gate(X86_TRAP_UD, invalid_op);
953 set_intr_gate(X86_TRAP_NM, device_not_available);
081f75bb 954#ifdef CONFIG_X86_32
c9408265 955 set_task_gate(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS);
081f75bb 956#else
c9408265 957 set_intr_gate_ist(X86_TRAP_DF, &double_fault, DOUBLEFAULT_STACK);
081f75bb 958#endif
25c74b10
SA
959 set_intr_gate(X86_TRAP_OLD_MF, coprocessor_segment_overrun);
960 set_intr_gate(X86_TRAP_TS, invalid_TSS);
961 set_intr_gate(X86_TRAP_NP, segment_not_present);
6f442be2 962 set_intr_gate(X86_TRAP_SS, stack_segment);
25c74b10
SA
963 set_intr_gate(X86_TRAP_GP, general_protection);
964 set_intr_gate(X86_TRAP_SPURIOUS, spurious_interrupt_bug);
965 set_intr_gate(X86_TRAP_MF, coprocessor_error);
966 set_intr_gate(X86_TRAP_AC, alignment_check);
1da177e4 967#ifdef CONFIG_X86_MCE
c9408265 968 set_intr_gate_ist(X86_TRAP_MC, &machine_check, MCE_STACK);
1da177e4 969#endif
25c74b10 970 set_intr_gate(X86_TRAP_XF, simd_coprocessor_error);
1da177e4 971
bb3f0b59
YL
972 /* Reserve all the builtin and the syscall vector: */
973 for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
974 set_bit(i, used_vectors);
975
081f75bb
AH
976#ifdef CONFIG_IA32_EMULATION
977 set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
bb3f0b59 978 set_bit(IA32_SYSCALL_VECTOR, used_vectors);
081f75bb
AH
979#endif
980
981#ifdef CONFIG_X86_32
699d2937 982 set_system_trap_gate(SYSCALL_VECTOR, &system_call);
dbeb2be2 983 set_bit(SYSCALL_VECTOR, used_vectors);
081f75bb 984#endif
bb3f0b59 985
4eefbe79
KC
986 /*
987 * Set the IDT descriptor to a fixed read-only location, so that the
988 * "sidt" instruction will not leak the location of the kernel, and
989 * to defend the IDT against arbitrary memory write vulnerabilities.
990 * It will be reloaded in cpu_init() */
991 __set_fixmap(FIX_RO_IDT, __pa_symbol(idt_table), PAGE_KERNEL_RO);
992 idt_descr.address = fix_to_virt(FIX_RO_IDT);
993
1da177e4 994 /*
b5964405 995 * Should be a barrier for any external CPU state:
1da177e4
LT
996 */
997 cpu_init();
998
428cf902 999 x86_init.irqs.trap_init();
228bdaa9
SR
1000
1001#ifdef CONFIG_X86_64
629f4f9d 1002 memcpy(&debug_idt_table, &idt_table, IDT_ENTRIES * 16);
c9408265
KC
1003 set_nmi_gate(X86_TRAP_DB, &debug);
1004 set_nmi_gate(X86_TRAP_BP, &int3);
228bdaa9 1005#endif
1da177e4 1006}