traps: x86_64: make io_check_error equal to the one on i386
[pandora-kernel.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 dotraplinkage 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 dotraplinkage 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 dotraplinkage 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 dotraplinkage 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 dotraplinkage 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         unsigned long i;
256
257         printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
258         show_registers(regs);
259
260         /* Re-enable the IOCK line, wait for a few seconds */
261         reason = (reason & 0xf) | 8;
262         outb(reason, 0x61);
263
264         i = 2000;
265         while (--i)
266                 udelay(1000);
267
268         reason &= ~8;
269         outb(reason, 0x61);
270 }
271
272 static notrace __kprobes void
273 unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
274 {
275         if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
276                         NOTIFY_STOP)
277                 return;
278         printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
279                 reason);
280         printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
281
282         if (panic_on_unrecovered_nmi)
283                 panic("NMI: Not continuing");
284
285         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
286 }
287
288 /* Runs on IST stack. This code must keep interrupts off all the time.
289    Nested NMIs are prevented by the CPU. */
290 asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
291 {
292         unsigned char reason = 0;
293         int cpu;
294
295         cpu = smp_processor_id();
296
297         /* Only the BSP gets external NMIs from the system. */
298         if (!cpu)
299                 reason = get_nmi_reason();
300
301         if (!(reason & 0xc0)) {
302                 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
303                                                                 == NOTIFY_STOP)
304                         return;
305                 /*
306                  * Ok, so this is none of the documented NMI sources,
307                  * so it must be the NMI watchdog.
308                  */
309                 if (nmi_watchdog_tick(regs, reason))
310                         return;
311                 if (!do_nmi_callback(regs, cpu))
312                         unknown_nmi_error(reason, regs);
313
314                 return;
315         }
316         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
317                 return;
318
319         /* AK: following checks seem to be broken on modern chipsets. FIXME */
320         if (reason & 0x80)
321                 mem_parity_error(reason, regs);
322         if (reason & 0x40)
323                 io_check_error(reason, regs);
324 }
325
326 dotraplinkage notrace __kprobes void
327 do_nmi(struct pt_regs *regs, long error_code)
328 {
329         nmi_enter();
330
331         add_pda(__nmi_count, 1);
332
333         if (!ignore_nmis)
334                 default_do_nmi(regs);
335
336         nmi_exit();
337 }
338
339 void stop_nmi(void)
340 {
341         acpi_nmi_disable();
342         ignore_nmis++;
343 }
344
345 void restart_nmi(void)
346 {
347         ignore_nmis--;
348         acpi_nmi_enable();
349 }
350
351 /* runs on IST stack. */
352 dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
353 {
354         if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
355                         == NOTIFY_STOP)
356                 return;
357
358         preempt_conditional_sti(regs);
359         do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
360         preempt_conditional_cli(regs);
361 }
362
363 /* Help handler running on IST stack to switch back to user stack
364    for scheduling or signal handling. The actual stack switch is done in
365    entry.S */
366 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
367 {
368         struct pt_regs *regs = eregs;
369         /* Did already sync */
370         if (eregs == (struct pt_regs *)eregs->sp)
371                 ;
372         /* Exception from user space */
373         else if (user_mode(eregs))
374                 regs = task_pt_regs(current);
375         /* Exception from kernel and interrupts are enabled. Move to
376            kernel process stack. */
377         else if (eregs->flags & X86_EFLAGS_IF)
378                 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
379         if (eregs != regs)
380                 *regs = *eregs;
381         return regs;
382 }
383
384 /* runs on IST stack. */
385 dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
386 {
387         struct task_struct *tsk = current;
388         unsigned long condition;
389         int si_code;
390
391         get_debugreg(condition, 6);
392
393         /*
394          * The processor cleared BTF, so don't mark that we need it set.
395          */
396         clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
397         tsk->thread.debugctlmsr = 0;
398
399         if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
400                                                 SIGTRAP) == NOTIFY_STOP)
401                 return;
402
403         /* It's safe to allow irq's after DR6 has been saved */
404         preempt_conditional_sti(regs);
405
406         /* Mask out spurious debug traps due to lazy DR7 setting */
407         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
408                 if (!tsk->thread.debugreg7)
409                         goto clear_dr7;
410         }
411
412         /* Save debug status register where ptrace can see it */
413         tsk->thread.debugreg6 = condition;
414
415         /*
416          * Single-stepping through TF: make sure we ignore any events in
417          * kernel space (but re-enable TF when returning to user mode).
418          */
419         if (condition & DR_STEP) {
420                 if (!user_mode(regs))
421                         goto clear_TF_reenable;
422         }
423
424         si_code = get_si_code(condition);
425         /* Ok, finally something we can handle */
426         send_sigtrap(tsk, regs, error_code, si_code);
427
428         /*
429          * Disable additional traps. They'll be re-enabled when
430          * the signal is delivered.
431          */
432 clear_dr7:
433         set_debugreg(0, 7);
434         preempt_conditional_cli(regs);
435         return;
436
437 clear_TF_reenable:
438         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
439         regs->flags &= ~X86_EFLAGS_TF;
440         preempt_conditional_cli(regs);
441         return;
442 }
443
444 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
445 {
446         if (fixup_exception(regs))
447                 return 1;
448
449         notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
450         /* Illegal floating point operation in the kernel */
451         current->thread.trap_no = trapnr;
452         die(str, regs, 0);
453         return 0;
454 }
455
456 /*
457  * Note that we play around with the 'TS' bit in an attempt to get
458  * the correct behaviour even in the presence of the asynchronous
459  * IRQ13 behaviour
460  */
461 void math_error(void __user *ip)
462 {
463         struct task_struct *task;
464         siginfo_t info;
465         unsigned short cwd, swd;
466
467         /*
468          * Save the info for the exception handler and clear the error.
469          */
470         task = current;
471         save_init_fpu(task);
472         task->thread.trap_no = 16;
473         task->thread.error_code = 0;
474         info.si_signo = SIGFPE;
475         info.si_errno = 0;
476         info.si_code = __SI_FAULT;
477         info.si_addr = ip;
478         /*
479          * (~cwd & swd) will mask out exceptions that are not set to unmasked
480          * status.  0x3f is the exception bits in these regs, 0x200 is the
481          * C1 reg you need in case of a stack fault, 0x040 is the stack
482          * fault bit.  We should only be taking one exception at a time,
483          * so if this combination doesn't produce any single exception,
484          * then we have a bad program that isn't synchronizing its FPU usage
485          * and it will suffer the consequences since we won't be able to
486          * fully reproduce the context of the exception
487          */
488         cwd = get_fpu_cwd(task);
489         swd = get_fpu_swd(task);
490         switch (swd & ~cwd & 0x3f) {
491         case 0x000: /* No unmasked exception */
492         default: /* Multiple exceptions */
493                 break;
494         case 0x001: /* Invalid Op */
495                 /*
496                  * swd & 0x240 == 0x040: Stack Underflow
497                  * swd & 0x240 == 0x240: Stack Overflow
498                  * User must clear the SF bit (0x40) if set
499                  */
500                 info.si_code = FPE_FLTINV;
501                 break;
502         case 0x002: /* Denormalize */
503         case 0x010: /* Underflow */
504                 info.si_code = FPE_FLTUND;
505                 break;
506         case 0x004: /* Zero Divide */
507                 info.si_code = FPE_FLTDIV;
508                 break;
509         case 0x008: /* Overflow */
510                 info.si_code = FPE_FLTOVF;
511                 break;
512         case 0x020: /* Precision */
513                 info.si_code = FPE_FLTRES;
514                 break;
515         }
516         force_sig_info(SIGFPE, &info, task);
517 }
518
519 dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
520 {
521         conditional_sti(regs);
522         if (!user_mode(regs) &&
523             kernel_math_error(regs, "kernel x87 math error", 16))
524                 return;
525         math_error((void __user *)regs->ip);
526 }
527
528 asmlinkage void bad_intr(void)
529 {
530         printk("bad interrupt");
531 }
532
533 static void simd_math_error(void __user *ip)
534 {
535         struct task_struct *task;
536         siginfo_t info;
537         unsigned short mxcsr;
538
539         /*
540          * Save the info for the exception handler and clear the error.
541          */
542         task = current;
543         save_init_fpu(task);
544         task->thread.trap_no = 19;
545         task->thread.error_code = 0;
546         info.si_signo = SIGFPE;
547         info.si_errno = 0;
548         info.si_code = __SI_FAULT;
549         info.si_addr = ip;
550         /*
551          * The SIMD FPU exceptions are handled a little differently, as there
552          * is only a single status/control register.  Thus, to determine which
553          * unmasked exception was caught we must mask the exception mask bits
554          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
555          */
556         mxcsr = get_fpu_mxcsr(task);
557         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
558         case 0x000:
559         default:
560                 break;
561         case 0x001: /* Invalid Op */
562                 info.si_code = FPE_FLTINV;
563                 break;
564         case 0x002: /* Denormalize */
565         case 0x010: /* Underflow */
566                 info.si_code = FPE_FLTUND;
567                 break;
568         case 0x004: /* Zero Divide */
569                 info.si_code = FPE_FLTDIV;
570                 break;
571         case 0x008: /* Overflow */
572                 info.si_code = FPE_FLTOVF;
573                 break;
574         case 0x020: /* Precision */
575                 info.si_code = FPE_FLTRES;
576                 break;
577         }
578         force_sig_info(SIGFPE, &info, task);
579 }
580
581 dotraplinkage void
582 do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
583 {
584         conditional_sti(regs);
585         if (!user_mode(regs) &&
586                         kernel_math_error(regs, "kernel simd math error", 19))
587                 return;
588         simd_math_error((void __user *)regs->ip);
589 }
590
591 dotraplinkage void
592 do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
593 {
594 }
595
596 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
597 {
598 }
599
600 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
601 {
602 }
603
604 /*
605  * 'math_state_restore()' saves the current math information in the
606  * old math state array, and gets the new ones from the current task
607  *
608  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
609  * Don't touch unless you *really* know how it works.
610  */
611 asmlinkage void math_state_restore(void)
612 {
613         struct thread_info *thread = current_thread_info();
614         struct task_struct *tsk = thread->task;
615
616         if (!tsk_used_math(tsk)) {
617                 local_irq_enable();
618                 /*
619                  * does a slab alloc which can sleep
620                  */
621                 if (init_fpu(tsk)) {
622                         /*
623                          * ran out of memory!
624                          */
625                         do_group_exit(SIGKILL);
626                         return;
627                 }
628                 local_irq_disable();
629         }
630
631         clts();                         /* Allow maths ops (or we recurse) */
632         /*
633          * Paranoid restore. send a SIGSEGV if we fail to restore the state.
634          */
635         if (unlikely(restore_fpu_checking(tsk))) {
636                 stts();
637                 force_sig(SIGSEGV, tsk);
638                 return;
639         }
640         thread->status |= TS_USEDFPU;   /* So we fnsave on switch_to() */
641         tsk->fpu_counter++;
642 }
643 EXPORT_SYMBOL_GPL(math_state_restore);
644
645 dotraplinkage void __kprobes
646 do_device_not_available(struct pt_regs *regs, long error)
647 {
648         math_state_restore();
649 }
650
651 void __init trap_init(void)
652 {
653         set_intr_gate(0, &divide_error);
654         set_intr_gate_ist(1, &debug, DEBUG_STACK);
655         set_intr_gate_ist(2, &nmi, NMI_STACK);
656         /* int3 can be called from all */
657         set_system_intr_gate_ist(3, &int3, DEBUG_STACK);
658         /* int4 can be called from all */
659         set_system_intr_gate(4, &overflow);
660         set_intr_gate(5, &bounds);
661         set_intr_gate(6, &invalid_op);
662         set_intr_gate(7, &device_not_available);
663         set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
664         set_intr_gate(9, &coprocessor_segment_overrun);
665         set_intr_gate(10, &invalid_TSS);
666         set_intr_gate(11, &segment_not_present);
667         set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
668         set_intr_gate(13, &general_protection);
669         set_intr_gate(14, &page_fault);
670         set_intr_gate(15, &spurious_interrupt_bug);
671         set_intr_gate(16, &coprocessor_error);
672         set_intr_gate(17, &alignment_check);
673 #ifdef CONFIG_X86_MCE
674         set_intr_gate_ist(18, &machine_check, MCE_STACK);
675 #endif
676         set_intr_gate(19, &simd_coprocessor_error);
677
678 #ifdef CONFIG_IA32_EMULATION
679         set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
680 #endif
681         /*
682          * Should be a barrier for any external CPU state:
683          */
684         cpu_init();
685 }