]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/x86/kernel/traps_64.c
x86, traps: split out math_error and simd_math_error
[karo-tx-linux.git] / arch / x86 / kernel / traps_64.c
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4  *
5  *  Pentium III FXSR, SSE support
6  *      Gareth Hughes <gareth@valinux.com>, May 2000
7  */
8
9 /*
10  * 'Traps.c' handles hardware traps and faults after we have saved some
11  * state in 'entry.S'.
12  */
13 #include <linux/moduleparam.h>
14 #include <linux/interrupt.h>
15 #include <linux/kallsyms.h>
16 #include <linux/spinlock.h>
17 #include <linux/kprobes.h>
18 #include <linux/uaccess.h>
19 #include <linux/utsname.h>
20 #include <linux/kdebug.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/ptrace.h>
24 #include <linux/string.h>
25 #include <linux/unwind.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/kexec.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/init.h>
32 #include <linux/bug.h>
33 #include <linux/nmi.h>
34 #include <linux/mm.h>
35 #include <linux/smp.h>
36 #include <linux/io.h>
37
38 #if defined(CONFIG_EDAC)
39 #include <linux/edac.h>
40 #endif
41
42 #include <asm/stacktrace.h>
43 #include <asm/processor.h>
44 #include <asm/debugreg.h>
45 #include <asm/atomic.h>
46 #include <asm/system.h>
47 #include <asm/unwind.h>
48 #include <asm/desc.h>
49 #include <asm/i387.h>
50 #include <asm/pgalloc.h>
51 #include <asm/proto.h>
52 #include <asm/pda.h>
53 #include <asm/traps.h>
54
55 #include <mach_traps.h>
56
57 static int ignore_nmis;
58
59 static inline void conditional_sti(struct pt_regs *regs)
60 {
61         if (regs->flags & X86_EFLAGS_IF)
62                 local_irq_enable();
63 }
64
65 static inline void preempt_conditional_sti(struct pt_regs *regs)
66 {
67         inc_preempt_count();
68         if (regs->flags & X86_EFLAGS_IF)
69                 local_irq_enable();
70 }
71
72 static inline void preempt_conditional_cli(struct pt_regs *regs)
73 {
74         if (regs->flags & X86_EFLAGS_IF)
75                 local_irq_disable();
76         /* Make sure to not schedule here because we could be running
77            on an exception stack. */
78         dec_preempt_count();
79 }
80
81 static void __kprobes
82 do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
83         long error_code, siginfo_t *info)
84 {
85         struct task_struct *tsk = current;
86
87         if (!user_mode(regs))
88                 goto kernel_trap;
89
90         /*
91          * We want error_code and trap_no set for userspace faults and
92          * kernelspace faults which result in die(), but not
93          * kernelspace faults which are fixed up.  die() gives the
94          * process no chance to handle the signal and notice the
95          * kernel fault information, so that won't result in polluting
96          * the information about previously queued, but not yet
97          * delivered, faults.  See also do_general_protection below.
98          */
99         tsk->thread.error_code = error_code;
100         tsk->thread.trap_no = trapnr;
101
102         if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
103             printk_ratelimit()) {
104                 printk(KERN_INFO
105                        "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
106                        tsk->comm, tsk->pid, str,
107                        regs->ip, regs->sp, error_code);
108                 print_vma_addr(" in ", regs->ip);
109                 printk("\n");
110         }
111
112         if (info)
113                 force_sig_info(signr, info, tsk);
114         else
115                 force_sig(signr, tsk);
116         return;
117
118 kernel_trap:
119         if (!fixup_exception(regs)) {
120                 tsk->thread.error_code = error_code;
121                 tsk->thread.trap_no = trapnr;
122                 die(str, regs, error_code);
123         }
124         return;
125 }
126
127 #define DO_ERROR(trapnr, signr, str, name)                              \
128 asmlinkage void do_##name(struct pt_regs *regs, long error_code)        \
129 {                                                                       \
130         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr)  \
131                                                         == NOTIFY_STOP) \
132                 return;                                                 \
133         conditional_sti(regs);                                          \
134         do_trap(trapnr, signr, str, regs, error_code, NULL);            \
135 }
136
137 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr)         \
138 asmlinkage void do_##name(struct pt_regs *regs, long error_code)        \
139 {                                                                       \
140         siginfo_t info;                                                 \
141         info.si_signo = signr;                                          \
142         info.si_errno = 0;                                              \
143         info.si_code = sicode;                                          \
144         info.si_addr = (void __user *)siaddr;                           \
145         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr)  \
146                                                         == NOTIFY_STOP) \
147                 return;                                                 \
148         conditional_sti(regs);                                          \
149         do_trap(trapnr, signr, str, regs, error_code, &info);           \
150 }
151
152 DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
153 DO_ERROR(4, SIGSEGV, "overflow", overflow)
154 DO_ERROR(5, SIGSEGV, "bounds", bounds)
155 DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
156 DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
157 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
158 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
159 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
160
161 /* Runs on IST stack */
162 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
163 {
164         if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
165                         12, SIGBUS) == NOTIFY_STOP)
166                 return;
167         preempt_conditional_sti(regs);
168         do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
169         preempt_conditional_cli(regs);
170 }
171
172 asmlinkage void do_double_fault(struct pt_regs *regs, long error_code)
173 {
174         static const char str[] = "double fault";
175         struct task_struct *tsk = current;
176
177         /* Return not checked because double check cannot be ignored */
178         notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
179
180         tsk->thread.error_code = error_code;
181         tsk->thread.trap_no = 8;
182
183         /* This is always a kernel trap and never fixable (and thus must
184            never return). */
185         for (;;)
186                 die(str, regs, error_code);
187 }
188
189 asmlinkage void __kprobes
190 do_general_protection(struct pt_regs *regs, long error_code)
191 {
192         struct task_struct *tsk;
193
194         conditional_sti(regs);
195
196         tsk = current;
197         if (!user_mode(regs))
198                 goto gp_in_kernel;
199
200         tsk->thread.error_code = error_code;
201         tsk->thread.trap_no = 13;
202
203         if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
204                         printk_ratelimit()) {
205                 printk(KERN_INFO
206                         "%s[%d] general protection ip:%lx sp:%lx error:%lx",
207                         tsk->comm, tsk->pid,
208                         regs->ip, regs->sp, error_code);
209                 print_vma_addr(" in ", regs->ip);
210                 printk("\n");
211         }
212
213         force_sig(SIGSEGV, tsk);
214         return;
215
216 gp_in_kernel:
217         if (fixup_exception(regs))
218                 return;
219
220         tsk->thread.error_code = error_code;
221         tsk->thread.trap_no = 13;
222         if (notify_die(DIE_GPF, "general protection fault", regs,
223                                 error_code, 13, SIGSEGV) == NOTIFY_STOP)
224                 return;
225         die("general protection fault", regs, error_code);
226 }
227
228 static notrace __kprobes void
229 mem_parity_error(unsigned char reason, struct pt_regs *regs)
230 {
231         printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
232                 reason);
233         printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
234
235 #if defined(CONFIG_EDAC)
236         if (edac_handler_set()) {
237                 edac_atomic_assert_error();
238                 return;
239         }
240 #endif
241
242         if (panic_on_unrecovered_nmi)
243                 panic("NMI: Not continuing");
244
245         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
246
247         /* Clear and disable the memory parity error line. */
248         reason = (reason & 0xf) | 4;
249         outb(reason, 0x61);
250 }
251
252 static notrace __kprobes void
253 io_check_error(unsigned char reason, struct pt_regs *regs)
254 {
255         printk("NMI: IOCK error (debug interrupt?)\n");
256         show_registers(regs);
257
258         /* Re-enable the IOCK line, wait for a few seconds */
259         reason = (reason & 0xf) | 8;
260         outb(reason, 0x61);
261         mdelay(2000);
262         reason &= ~8;
263         outb(reason, 0x61);
264 }
265
266 static notrace __kprobes void
267 unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
268 {
269         if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
270                         NOTIFY_STOP)
271                 return;
272         printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
273                 reason);
274         printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
275
276         if (panic_on_unrecovered_nmi)
277                 panic("NMI: Not continuing");
278
279         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
280 }
281
282 /* Runs on IST stack. This code must keep interrupts off all the time.
283    Nested NMIs are prevented by the CPU. */
284 asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
285 {
286         unsigned char reason = 0;
287         int cpu;
288
289         cpu = smp_processor_id();
290
291         /* Only the BSP gets external NMIs from the system. */
292         if (!cpu)
293                 reason = get_nmi_reason();
294
295         if (!(reason & 0xc0)) {
296                 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
297                                                                 == NOTIFY_STOP)
298                         return;
299                 /*
300                  * Ok, so this is none of the documented NMI sources,
301                  * so it must be the NMI watchdog.
302                  */
303                 if (nmi_watchdog_tick(regs, reason))
304                         return;
305                 if (!do_nmi_callback(regs, cpu))
306                         unknown_nmi_error(reason, regs);
307
308                 return;
309         }
310         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
311                 return;
312
313         /* AK: following checks seem to be broken on modern chipsets. FIXME */
314         if (reason & 0x80)
315                 mem_parity_error(reason, regs);
316         if (reason & 0x40)
317                 io_check_error(reason, regs);
318 }
319
320 asmlinkage notrace __kprobes void
321 do_nmi(struct pt_regs *regs, long error_code)
322 {
323         nmi_enter();
324
325         add_pda(__nmi_count, 1);
326
327         if (!ignore_nmis)
328                 default_do_nmi(regs);
329
330         nmi_exit();
331 }
332
333 void stop_nmi(void)
334 {
335         acpi_nmi_disable();
336         ignore_nmis++;
337 }
338
339 void restart_nmi(void)
340 {
341         ignore_nmis--;
342         acpi_nmi_enable();
343 }
344
345 /* runs on IST stack. */
346 asmlinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
347 {
348         if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
349                         == NOTIFY_STOP)
350                 return;
351
352         preempt_conditional_sti(regs);
353         do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
354         preempt_conditional_cli(regs);
355 }
356
357 /* Help handler running on IST stack to switch back to user stack
358    for scheduling or signal handling. The actual stack switch is done in
359    entry.S */
360 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
361 {
362         struct pt_regs *regs = eregs;
363         /* Did already sync */
364         if (eregs == (struct pt_regs *)eregs->sp)
365                 ;
366         /* Exception from user space */
367         else if (user_mode(eregs))
368                 regs = task_pt_regs(current);
369         /* Exception from kernel and interrupts are enabled. Move to
370            kernel process stack. */
371         else if (eregs->flags & X86_EFLAGS_IF)
372                 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
373         if (eregs != regs)
374                 *regs = *eregs;
375         return regs;
376 }
377
378 /* runs on IST stack. */
379 asmlinkage void __kprobes do_debug(struct pt_regs *regs,
380                                    unsigned long error_code)
381 {
382         struct task_struct *tsk = current;
383         unsigned long condition;
384         siginfo_t info;
385
386         get_debugreg(condition, 6);
387
388         /*
389          * The processor cleared BTF, so don't mark that we need it set.
390          */
391         clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
392         tsk->thread.debugctlmsr = 0;
393
394         if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
395                                                 SIGTRAP) == NOTIFY_STOP)
396                 return;
397
398         preempt_conditional_sti(regs);
399
400         /* Mask out spurious debug traps due to lazy DR7 setting */
401         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
402                 if (!tsk->thread.debugreg7)
403                         goto clear_dr7;
404         }
405
406         tsk->thread.debugreg6 = condition;
407
408         /*
409          * Single-stepping through TF: make sure we ignore any events in
410          * kernel space (but re-enable TF when returning to user mode).
411          */
412         if (condition & DR_STEP) {
413                 if (!user_mode(regs))
414                         goto clear_TF_reenable;
415         }
416
417         /* Ok, finally something we can handle */
418         tsk->thread.trap_no = 1;
419         tsk->thread.error_code = error_code;
420         info.si_signo = SIGTRAP;
421         info.si_errno = 0;
422         info.si_code = get_si_code(condition);
423         info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
424         force_sig_info(SIGTRAP, &info, tsk);
425
426 clear_dr7:
427         set_debugreg(0, 7);
428         preempt_conditional_cli(regs);
429         return;
430
431 clear_TF_reenable:
432         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
433         regs->flags &= ~X86_EFLAGS_TF;
434         preempt_conditional_cli(regs);
435         return;
436 }
437
438 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
439 {
440         if (fixup_exception(regs))
441                 return 1;
442
443         notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
444         /* Illegal floating point operation in the kernel */
445         current->thread.trap_no = trapnr;
446         die(str, regs, 0);
447         return 0;
448 }
449
450 /*
451  * Note that we play around with the 'TS' bit in an attempt to get
452  * the correct behaviour even in the presence of the asynchronous
453  * IRQ13 behaviour
454  */
455 void math_error(void __user *ip)
456 {
457         struct task_struct *task;
458         siginfo_t info;
459         unsigned short cwd, swd;
460
461         /*
462          * Save the info for the exception handler and clear the error.
463          */
464         task = current;
465         save_init_fpu(task);
466         task->thread.trap_no = 16;
467         task->thread.error_code = 0;
468         info.si_signo = SIGFPE;
469         info.si_errno = 0;
470         info.si_code = __SI_FAULT;
471         info.si_addr = ip;
472         /*
473          * (~cwd & swd) will mask out exceptions that are not set to unmasked
474          * status.  0x3f is the exception bits in these regs, 0x200 is the
475          * C1 reg you need in case of a stack fault, 0x040 is the stack
476          * fault bit.  We should only be taking one exception at a time,
477          * so if this combination doesn't produce any single exception,
478          * then we have a bad program that isn't synchronizing its FPU usage
479          * and it will suffer the consequences since we won't be able to
480          * fully reproduce the context of the exception
481          */
482         cwd = get_fpu_cwd(task);
483         swd = get_fpu_swd(task);
484         switch (swd & ~cwd & 0x3f) {
485         case 0x000: /* No unmasked exception */
486         default: /* Multiple exceptions */
487                 break;
488         case 0x001: /* Invalid Op */
489                 /*
490                  * swd & 0x240 == 0x040: Stack Underflow
491                  * swd & 0x240 == 0x240: Stack Overflow
492                  * User must clear the SF bit (0x40) if set
493                  */
494                 info.si_code = FPE_FLTINV;
495                 break;
496         case 0x002: /* Denormalize */
497         case 0x010: /* Underflow */
498                 info.si_code = FPE_FLTUND;
499                 break;
500         case 0x004: /* Zero Divide */
501                 info.si_code = FPE_FLTDIV;
502                 break;
503         case 0x008: /* Overflow */
504                 info.si_code = FPE_FLTOVF;
505                 break;
506         case 0x020: /* Precision */
507                 info.si_code = FPE_FLTRES;
508                 break;
509         }
510         force_sig_info(SIGFPE, &info, task);
511 }
512
513 asmlinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
514 {
515         conditional_sti(regs);
516         if (!user_mode(regs) &&
517             kernel_math_error(regs, "kernel x87 math error", 16))
518                 return;
519         math_error((void __user *)regs->ip);
520 }
521
522 asmlinkage void bad_intr(void)
523 {
524         printk("bad interrupt");
525 }
526
527 static void simd_math_error(void __user *ip)
528 {
529         struct task_struct *task;
530         siginfo_t info;
531         unsigned short mxcsr;
532
533         /*
534          * Save the info for the exception handler and clear the error.
535          */
536         task = current;
537         save_init_fpu(task);
538         task->thread.trap_no = 19;
539         task->thread.error_code = 0;
540         info.si_signo = SIGFPE;
541         info.si_errno = 0;
542         info.si_code = __SI_FAULT;
543         info.si_addr = ip;
544         /*
545          * The SIMD FPU exceptions are handled a little differently, as there
546          * is only a single status/control register.  Thus, to determine which
547          * unmasked exception was caught we must mask the exception mask bits
548          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
549          */
550         mxcsr = get_fpu_mxcsr(task);
551         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
552         case 0x000:
553         default:
554                 break;
555         case 0x001: /* Invalid Op */
556                 info.si_code = FPE_FLTINV;
557                 break;
558         case 0x002: /* Denormalize */
559         case 0x010: /* Underflow */
560                 info.si_code = FPE_FLTUND;
561                 break;
562         case 0x004: /* Zero Divide */
563                 info.si_code = FPE_FLTDIV;
564                 break;
565         case 0x008: /* Overflow */
566                 info.si_code = FPE_FLTOVF;
567                 break;
568         case 0x020: /* Precision */
569                 info.si_code = FPE_FLTRES;
570                 break;
571         }
572         force_sig_info(SIGFPE, &info, task);
573 }
574
575 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
576 {
577         conditional_sti(regs);
578         if (!user_mode(regs) &&
579                         kernel_math_error(regs, "kernel simd math error", 19))
580                 return;
581         simd_math_error((void __user *)regs->ip);
582 }
583
584 asmlinkage void do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
585 {
586 }
587
588 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
589 {
590 }
591
592 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
593 {
594 }
595
596 /*
597  * 'math_state_restore()' saves the current math information in the
598  * old math state array, and gets the new ones from the current task
599  *
600  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
601  * Don't touch unless you *really* know how it works.
602  */
603 asmlinkage void math_state_restore(void)
604 {
605         struct task_struct *me = current;
606
607         if (!used_math()) {
608                 local_irq_enable();
609                 /*
610                  * does a slab alloc which can sleep
611                  */
612                 if (init_fpu(me)) {
613                         /*
614                          * ran out of memory!
615                          */
616                         do_group_exit(SIGKILL);
617                         return;
618                 }
619                 local_irq_disable();
620         }
621
622         clts();                         /* Allow maths ops (or we recurse) */
623         /*
624          * Paranoid restore. send a SIGSEGV if we fail to restore the state.
625          */
626         if (unlikely(restore_fpu_checking(me))) {
627                 stts();
628                 force_sig(SIGSEGV, me);
629                 return;
630         }
631         task_thread_info(me)->status |= TS_USEDFPU;
632         me->fpu_counter++;
633 }
634 EXPORT_SYMBOL_GPL(math_state_restore);
635
636 void __init trap_init(void)
637 {
638         set_intr_gate(0, &divide_error);
639         set_intr_gate_ist(1, &debug, DEBUG_STACK);
640         set_intr_gate_ist(2, &nmi, NMI_STACK);
641         /* int3 can be called from all */
642         set_system_gate_ist(3, &int3, DEBUG_STACK);
643         /* int4 can be called from all */
644         set_system_gate(4, &overflow);
645         set_intr_gate(5, &bounds);
646         set_intr_gate(6, &invalid_op);
647         set_intr_gate(7, &device_not_available);
648         set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
649         set_intr_gate(9, &coprocessor_segment_overrun);
650         set_intr_gate(10, &invalid_TSS);
651         set_intr_gate(11, &segment_not_present);
652         set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
653         set_intr_gate(13, &general_protection);
654         set_intr_gate(14, &page_fault);
655         set_intr_gate(15, &spurious_interrupt_bug);
656         set_intr_gate(16, &coprocessor_error);
657         set_intr_gate(17, &alignment_check);
658 #ifdef CONFIG_X86_MCE
659         set_intr_gate_ist(18, &machine_check, MCE_STACK);
660 #endif
661         set_intr_gate(19, &simd_coprocessor_error);
662
663 #ifdef CONFIG_IA32_EMULATION
664         set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
665 #endif
666         /*
667          * Should be a barrier for any external CPU state:
668          */
669         cpu_init();
670 }