Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[sfrench/cifs-2.6.git] / arch / x86 / kernel / traps.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  * Handle hardware traps and faults.
11  */
12 #include <linux/interrupt.h>
13 #include <linux/kallsyms.h>
14 #include <linux/spinlock.h>
15 #include <linux/kprobes.h>
16 #include <linux/uaccess.h>
17 #include <linux/utsname.h>
18 #include <linux/kdebug.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/ptrace.h>
22 #include <linux/string.h>
23 #include <linux/delay.h>
24 #include <linux/errno.h>
25 #include <linux/kexec.h>
26 #include <linux/sched.h>
27 #include <linux/timer.h>
28 #include <linux/init.h>
29 #include <linux/bug.h>
30 #include <linux/nmi.h>
31 #include <linux/mm.h>
32 #include <linux/smp.h>
33 #include <linux/io.h>
34
35 #ifdef CONFIG_EISA
36 #include <linux/ioport.h>
37 #include <linux/eisa.h>
38 #endif
39
40 #ifdef CONFIG_MCA
41 #include <linux/mca.h>
42 #endif
43
44 #if defined(CONFIG_EDAC)
45 #include <linux/edac.h>
46 #endif
47
48 #include <asm/stacktrace.h>
49 #include <asm/processor.h>
50 #include <asm/debugreg.h>
51 #include <asm/atomic.h>
52 #include <asm/system.h>
53 #include <asm/traps.h>
54 #include <asm/desc.h>
55 #include <asm/i387.h>
56
57 #include <mach_traps.h>
58
59 #ifdef CONFIG_X86_64
60 #include <asm/pgalloc.h>
61 #include <asm/proto.h>
62 #include <asm/pda.h>
63 #else
64 #include <asm/processor-flags.h>
65 #include <asm/arch_hooks.h>
66 #include <asm/traps.h>
67
68 #include "cpu/mcheck/mce.h"
69
70 asmlinkage int system_call(void);
71
72 /* Do we ignore FPU interrupts ? */
73 char ignore_fpu_irq;
74
75 /*
76  * The IDT has to be page-aligned to simplify the Pentium
77  * F0 0F bug workaround.. We have a special link segment
78  * for this.
79  */
80 gate_desc idt_table[256]
81         __attribute__((__section__(".data.idt"))) = { { { { 0, 0 } } }, };
82 #endif
83
84 DECLARE_BITMAP(used_vectors, NR_VECTORS);
85 EXPORT_SYMBOL_GPL(used_vectors);
86
87 static int ignore_nmis;
88
89 static inline void conditional_sti(struct pt_regs *regs)
90 {
91         if (regs->flags & X86_EFLAGS_IF)
92                 local_irq_enable();
93 }
94
95 static inline void preempt_conditional_sti(struct pt_regs *regs)
96 {
97         inc_preempt_count();
98         if (regs->flags & X86_EFLAGS_IF)
99                 local_irq_enable();
100 }
101
102 static inline void conditional_cli(struct pt_regs *regs)
103 {
104         if (regs->flags & X86_EFLAGS_IF)
105                 local_irq_disable();
106 }
107
108 static inline void preempt_conditional_cli(struct pt_regs *regs)
109 {
110         if (regs->flags & X86_EFLAGS_IF)
111                 local_irq_disable();
112         dec_preempt_count();
113 }
114
115 #ifdef CONFIG_X86_32
116 static inline void
117 die_if_kernel(const char *str, struct pt_regs *regs, long err)
118 {
119         if (!user_mode_vm(regs))
120                 die(str, regs, err);
121 }
122
123 /*
124  * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
125  * invalid offset set (the LAZY one) and the faulting thread has
126  * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS,
127  * we set the offset field correctly and return 1.
128  */
129 static int lazy_iobitmap_copy(void)
130 {
131         struct thread_struct *thread;
132         struct tss_struct *tss;
133         int cpu;
134
135         cpu = get_cpu();
136         tss = &per_cpu(init_tss, cpu);
137         thread = &current->thread;
138
139         if (tss->x86_tss.io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
140             thread->io_bitmap_ptr) {
141                 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
142                        thread->io_bitmap_max);
143                 /*
144                  * If the previously set map was extending to higher ports
145                  * than the current one, pad extra space with 0xff (no access).
146                  */
147                 if (thread->io_bitmap_max < tss->io_bitmap_max) {
148                         memset((char *) tss->io_bitmap +
149                                 thread->io_bitmap_max, 0xff,
150                                 tss->io_bitmap_max - thread->io_bitmap_max);
151                 }
152                 tss->io_bitmap_max = thread->io_bitmap_max;
153                 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
154                 tss->io_bitmap_owner = thread;
155                 put_cpu();
156
157                 return 1;
158         }
159         put_cpu();
160
161         return 0;
162 }
163 #endif
164
165 static void __kprobes
166 do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
167         long error_code, siginfo_t *info)
168 {
169         struct task_struct *tsk = current;
170
171 #ifdef CONFIG_X86_32
172         if (regs->flags & X86_VM_MASK) {
173                 /*
174                  * traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
175                  * On nmi (interrupt 2), do_trap should not be called.
176                  */
177                 if (trapnr < 6)
178                         goto vm86_trap;
179                 goto trap_signal;
180         }
181 #endif
182
183         if (!user_mode(regs))
184                 goto kernel_trap;
185
186 #ifdef CONFIG_X86_32
187 trap_signal:
188 #endif
189         /*
190          * We want error_code and trap_no set for userspace faults and
191          * kernelspace faults which result in die(), but not
192          * kernelspace faults which are fixed up.  die() gives the
193          * process no chance to handle the signal and notice the
194          * kernel fault information, so that won't result in polluting
195          * the information about previously queued, but not yet
196          * delivered, faults.  See also do_general_protection below.
197          */
198         tsk->thread.error_code = error_code;
199         tsk->thread.trap_no = trapnr;
200
201 #ifdef CONFIG_X86_64
202         if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
203             printk_ratelimit()) {
204                 printk(KERN_INFO
205                        "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
206                        tsk->comm, tsk->pid, str,
207                        regs->ip, regs->sp, error_code);
208                 print_vma_addr(" in ", regs->ip);
209                 printk("\n");
210         }
211 #endif
212
213         if (info)
214                 force_sig_info(signr, info, tsk);
215         else
216                 force_sig(signr, tsk);
217         return;
218
219 kernel_trap:
220         if (!fixup_exception(regs)) {
221                 tsk->thread.error_code = error_code;
222                 tsk->thread.trap_no = trapnr;
223                 die(str, regs, error_code);
224         }
225         return;
226
227 #ifdef CONFIG_X86_32
228 vm86_trap:
229         if (handle_vm86_trap((struct kernel_vm86_regs *) regs,
230                                                 error_code, trapnr))
231                 goto trap_signal;
232         return;
233 #endif
234 }
235
236 #define DO_ERROR(trapnr, signr, str, name)                              \
237 dotraplinkage void do_##name(struct pt_regs *regs, long error_code)     \
238 {                                                                       \
239         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr)  \
240                                                         == NOTIFY_STOP) \
241                 return;                                                 \
242         conditional_sti(regs);                                          \
243         do_trap(trapnr, signr, str, regs, error_code, NULL);            \
244 }
245
246 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr)         \
247 dotraplinkage void do_##name(struct pt_regs *regs, long error_code)     \
248 {                                                                       \
249         siginfo_t info;                                                 \
250         info.si_signo = signr;                                          \
251         info.si_errno = 0;                                              \
252         info.si_code = sicode;                                          \
253         info.si_addr = (void __user *)siaddr;                           \
254         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr)  \
255                                                         == NOTIFY_STOP) \
256                 return;                                                 \
257         conditional_sti(regs);                                          \
258         do_trap(trapnr, signr, str, regs, error_code, &info);           \
259 }
260
261 DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
262 DO_ERROR(4, SIGSEGV, "overflow", overflow)
263 DO_ERROR(5, SIGSEGV, "bounds", bounds)
264 DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
265 DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
266 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
267 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
268 #ifdef CONFIG_X86_32
269 DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
270 #endif
271 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
272
273 #ifdef CONFIG_X86_64
274 /* Runs on IST stack */
275 dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code)
276 {
277         if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
278                         12, SIGBUS) == NOTIFY_STOP)
279                 return;
280         preempt_conditional_sti(regs);
281         do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
282         preempt_conditional_cli(regs);
283 }
284
285 dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
286 {
287         static const char str[] = "double fault";
288         struct task_struct *tsk = current;
289
290         /* Return not checked because double check cannot be ignored */
291         notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
292
293         tsk->thread.error_code = error_code;
294         tsk->thread.trap_no = 8;
295
296         /*
297          * This is always a kernel trap and never fixable (and thus must
298          * never return).
299          */
300         for (;;)
301                 die(str, regs, error_code);
302 }
303 #endif
304
305 dotraplinkage void __kprobes
306 do_general_protection(struct pt_regs *regs, long error_code)
307 {
308         struct task_struct *tsk;
309
310         conditional_sti(regs);
311
312 #ifdef CONFIG_X86_32
313         if (lazy_iobitmap_copy()) {
314                 /* restart the faulting instruction */
315                 return;
316         }
317
318         if (regs->flags & X86_VM_MASK)
319                 goto gp_in_vm86;
320 #endif
321
322         tsk = current;
323         if (!user_mode(regs))
324                 goto gp_in_kernel;
325
326         tsk->thread.error_code = error_code;
327         tsk->thread.trap_no = 13;
328
329         if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
330                         printk_ratelimit()) {
331                 printk(KERN_INFO
332                         "%s[%d] general protection ip:%lx sp:%lx error:%lx",
333                         tsk->comm, task_pid_nr(tsk),
334                         regs->ip, regs->sp, error_code);
335                 print_vma_addr(" in ", regs->ip);
336                 printk("\n");
337         }
338
339         force_sig(SIGSEGV, tsk);
340         return;
341
342 #ifdef CONFIG_X86_32
343 gp_in_vm86:
344         local_irq_enable();
345         handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
346         return;
347 #endif
348
349 gp_in_kernel:
350         if (fixup_exception(regs))
351                 return;
352
353         tsk->thread.error_code = error_code;
354         tsk->thread.trap_no = 13;
355         if (notify_die(DIE_GPF, "general protection fault", regs,
356                                 error_code, 13, SIGSEGV) == NOTIFY_STOP)
357                 return;
358         die("general protection fault", regs, error_code);
359 }
360
361 static notrace __kprobes void
362 mem_parity_error(unsigned char reason, struct pt_regs *regs)
363 {
364         printk(KERN_EMERG
365                 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
366                         reason, smp_processor_id());
367
368         printk(KERN_EMERG
369                 "You have some hardware problem, likely on the PCI bus.\n");
370
371 #if defined(CONFIG_EDAC)
372         if (edac_handler_set()) {
373                 edac_atomic_assert_error();
374                 return;
375         }
376 #endif
377
378         if (panic_on_unrecovered_nmi)
379                 panic("NMI: Not continuing");
380
381         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
382
383         /* Clear and disable the memory parity error line. */
384         reason = (reason & 0xf) | 4;
385         outb(reason, 0x61);
386 }
387
388 static notrace __kprobes void
389 io_check_error(unsigned char reason, struct pt_regs *regs)
390 {
391         unsigned long i;
392
393         printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
394         show_registers(regs);
395
396         /* Re-enable the IOCK line, wait for a few seconds */
397         reason = (reason & 0xf) | 8;
398         outb(reason, 0x61);
399
400         i = 2000;
401         while (--i)
402                 udelay(1000);
403
404         reason &= ~8;
405         outb(reason, 0x61);
406 }
407
408 static notrace __kprobes void
409 unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
410 {
411         if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
412                         NOTIFY_STOP)
413                 return;
414 #ifdef CONFIG_MCA
415         /*
416          * Might actually be able to figure out what the guilty party
417          * is:
418          */
419         if (MCA_bus) {
420                 mca_handle_nmi();
421                 return;
422         }
423 #endif
424         printk(KERN_EMERG
425                 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
426                         reason, smp_processor_id());
427
428         printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
429         if (panic_on_unrecovered_nmi)
430                 panic("NMI: Not continuing");
431
432         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
433 }
434
435 static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
436 {
437         unsigned char reason = 0;
438         int cpu;
439
440         cpu = smp_processor_id();
441
442         /* Only the BSP gets external NMIs from the system. */
443         if (!cpu)
444                 reason = get_nmi_reason();
445
446         if (!(reason & 0xc0)) {
447                 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
448                                                                 == NOTIFY_STOP)
449                         return;
450 #ifdef CONFIG_X86_LOCAL_APIC
451                 /*
452                  * Ok, so this is none of the documented NMI sources,
453                  * so it must be the NMI watchdog.
454                  */
455                 if (nmi_watchdog_tick(regs, reason))
456                         return;
457                 if (!do_nmi_callback(regs, cpu))
458                         unknown_nmi_error(reason, regs);
459 #else
460                 unknown_nmi_error(reason, regs);
461 #endif
462
463                 return;
464         }
465         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
466                 return;
467
468         /* AK: following checks seem to be broken on modern chipsets. FIXME */
469         if (reason & 0x80)
470                 mem_parity_error(reason, regs);
471         if (reason & 0x40)
472                 io_check_error(reason, regs);
473 #ifdef CONFIG_X86_32
474         /*
475          * Reassert NMI in case it became active meanwhile
476          * as it's edge-triggered:
477          */
478         reassert_nmi();
479 #endif
480 }
481
482 dotraplinkage notrace __kprobes void
483 do_nmi(struct pt_regs *regs, long error_code)
484 {
485         nmi_enter();
486
487         inc_irq_stat(__nmi_count);
488
489         if (!ignore_nmis)
490                 default_do_nmi(regs);
491
492         nmi_exit();
493 }
494
495 void stop_nmi(void)
496 {
497         acpi_nmi_disable();
498         ignore_nmis++;
499 }
500
501 void restart_nmi(void)
502 {
503         ignore_nmis--;
504         acpi_nmi_enable();
505 }
506
507 /* May run on IST stack. */
508 dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
509 {
510 #ifdef CONFIG_KPROBES
511         if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
512                         == NOTIFY_STOP)
513                 return;
514 #else
515         if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
516                         == NOTIFY_STOP)
517                 return;
518 #endif
519
520         preempt_conditional_sti(regs);
521         do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
522         preempt_conditional_cli(regs);
523 }
524
525 #ifdef CONFIG_X86_64
526 /*
527  * Help handler running on IST stack to switch back to user stack
528  * for scheduling or signal handling. The actual stack switch is done in
529  * entry.S
530  */
531 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
532 {
533         struct pt_regs *regs = eregs;
534         /* Did already sync */
535         if (eregs == (struct pt_regs *)eregs->sp)
536                 ;
537         /* Exception from user space */
538         else if (user_mode(eregs))
539                 regs = task_pt_regs(current);
540         /*
541          * Exception from kernel and interrupts are enabled. Move to
542          * kernel process stack.
543          */
544         else if (eregs->flags & X86_EFLAGS_IF)
545                 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
546         if (eregs != regs)
547                 *regs = *eregs;
548         return regs;
549 }
550 #endif
551
552 /*
553  * Our handling of the processor debug registers is non-trivial.
554  * We do not clear them on entry and exit from the kernel. Therefore
555  * it is possible to get a watchpoint trap here from inside the kernel.
556  * However, the code in ./ptrace.c has ensured that the user can
557  * only set watchpoints on userspace addresses. Therefore the in-kernel
558  * watchpoint trap can only occur in code which is reading/writing
559  * from user space. Such code must not hold kernel locks (since it
560  * can equally take a page fault), therefore it is safe to call
561  * force_sig_info even though that claims and releases locks.
562  *
563  * Code in ./signal.c ensures that the debug control register
564  * is restored before we deliver any signal, and therefore that
565  * user code runs with the correct debug control register even though
566  * we clear it here.
567  *
568  * Being careful here means that we don't have to be as careful in a
569  * lot of more complicated places (task switching can be a bit lazy
570  * about restoring all the debug state, and ptrace doesn't have to
571  * find every occurrence of the TF bit that could be saved away even
572  * by user code)
573  *
574  * May run on IST stack.
575  */
576 dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
577 {
578         struct task_struct *tsk = current;
579         unsigned long condition;
580         int si_code;
581
582         get_debugreg(condition, 6);
583
584         /*
585          * The processor cleared BTF, so don't mark that we need it set.
586          */
587         clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
588         tsk->thread.debugctlmsr = 0;
589
590         if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
591                                                 SIGTRAP) == NOTIFY_STOP)
592                 return;
593
594         /* It's safe to allow irq's after DR6 has been saved */
595         preempt_conditional_sti(regs);
596
597         /* Mask out spurious debug traps due to lazy DR7 setting */
598         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
599                 if (!tsk->thread.debugreg7)
600                         goto clear_dr7;
601         }
602
603 #ifdef CONFIG_X86_32
604         if (regs->flags & X86_VM_MASK)
605                 goto debug_vm86;
606 #endif
607
608         /* Save debug status register where ptrace can see it */
609         tsk->thread.debugreg6 = condition;
610
611         /*
612          * Single-stepping through TF: make sure we ignore any events in
613          * kernel space (but re-enable TF when returning to user mode).
614          */
615         if (condition & DR_STEP) {
616                 if (!user_mode(regs))
617                         goto clear_TF_reenable;
618         }
619
620         si_code = get_si_code(condition);
621         /* Ok, finally something we can handle */
622         send_sigtrap(tsk, regs, error_code, si_code);
623
624         /*
625          * Disable additional traps. They'll be re-enabled when
626          * the signal is delivered.
627          */
628 clear_dr7:
629         set_debugreg(0, 7);
630         preempt_conditional_cli(regs);
631         return;
632
633 #ifdef CONFIG_X86_32
634 debug_vm86:
635         /* reenable preemption: handle_vm86_trap() might sleep */
636         dec_preempt_count();
637         handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
638         conditional_cli(regs);
639         return;
640 #endif
641
642 clear_TF_reenable:
643         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
644         regs->flags &= ~X86_EFLAGS_TF;
645         preempt_conditional_cli(regs);
646         return;
647 }
648
649 #ifdef CONFIG_X86_64
650 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
651 {
652         if (fixup_exception(regs))
653                 return 1;
654
655         notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
656         /* Illegal floating point operation in the kernel */
657         current->thread.trap_no = trapnr;
658         die(str, regs, 0);
659         return 0;
660 }
661 #endif
662
663 /*
664  * Note that we play around with the 'TS' bit in an attempt to get
665  * the correct behaviour even in the presence of the asynchronous
666  * IRQ13 behaviour
667  */
668 void math_error(void __user *ip)
669 {
670         struct task_struct *task;
671         siginfo_t info;
672         unsigned short cwd, swd, err;
673
674         /*
675          * Save the info for the exception handler and clear the error.
676          */
677         task = current;
678         save_init_fpu(task);
679         task->thread.trap_no = 16;
680         task->thread.error_code = 0;
681         info.si_signo = SIGFPE;
682         info.si_errno = 0;
683         info.si_addr = ip;
684         /*
685          * (~cwd & swd) will mask out exceptions that are not set to unmasked
686          * status.  0x3f is the exception bits in these regs, 0x200 is the
687          * C1 reg you need in case of a stack fault, 0x040 is the stack
688          * fault bit.  We should only be taking one exception at a time,
689          * so if this combination doesn't produce any single exception,
690          * then we have a bad program that isn't synchronizing its FPU usage
691          * and it will suffer the consequences since we won't be able to
692          * fully reproduce the context of the exception
693          */
694         cwd = get_fpu_cwd(task);
695         swd = get_fpu_swd(task);
696
697         err = swd & ~cwd;
698
699         if (err & 0x001) {      /* Invalid op */
700                 /*
701                  * swd & 0x240 == 0x040: Stack Underflow
702                  * swd & 0x240 == 0x240: Stack Overflow
703                  * User must clear the SF bit (0x40) if set
704                  */
705                 info.si_code = FPE_FLTINV;
706         } else if (err & 0x004) { /* Divide by Zero */
707                 info.si_code = FPE_FLTDIV;
708         } else if (err & 0x008) { /* Overflow */
709                 info.si_code = FPE_FLTOVF;
710         } else if (err & 0x012) { /* Denormal, Underflow */
711                 info.si_code = FPE_FLTUND;
712         } else if (err & 0x020) { /* Precision */
713                 info.si_code = FPE_FLTRES;
714         } else {
715                 /*
716                  * If we're using IRQ 13, or supposedly even some trap 16
717                  * implementations, it's possible we get a spurious trap...
718                  */
719                 return;         /* Spurious trap, no error */
720         }
721         force_sig_info(SIGFPE, &info, task);
722 }
723
724 dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
725 {
726         conditional_sti(regs);
727
728 #ifdef CONFIG_X86_32
729         ignore_fpu_irq = 1;
730 #else
731         if (!user_mode(regs) &&
732             kernel_math_error(regs, "kernel x87 math error", 16))
733                 return;
734 #endif
735
736         math_error((void __user *)regs->ip);
737 }
738
739 static void simd_math_error(void __user *ip)
740 {
741         struct task_struct *task;
742         siginfo_t info;
743         unsigned short mxcsr;
744
745         /*
746          * Save the info for the exception handler and clear the error.
747          */
748         task = current;
749         save_init_fpu(task);
750         task->thread.trap_no = 19;
751         task->thread.error_code = 0;
752         info.si_signo = SIGFPE;
753         info.si_errno = 0;
754         info.si_code = __SI_FAULT;
755         info.si_addr = ip;
756         /*
757          * The SIMD FPU exceptions are handled a little differently, as there
758          * is only a single status/control register.  Thus, to determine which
759          * unmasked exception was caught we must mask the exception mask bits
760          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
761          */
762         mxcsr = get_fpu_mxcsr(task);
763         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
764         case 0x000:
765         default:
766                 break;
767         case 0x001: /* Invalid Op */
768                 info.si_code = FPE_FLTINV;
769                 break;
770         case 0x002: /* Denormalize */
771         case 0x010: /* Underflow */
772                 info.si_code = FPE_FLTUND;
773                 break;
774         case 0x004: /* Zero Divide */
775                 info.si_code = FPE_FLTDIV;
776                 break;
777         case 0x008: /* Overflow */
778                 info.si_code = FPE_FLTOVF;
779                 break;
780         case 0x020: /* Precision */
781                 info.si_code = FPE_FLTRES;
782                 break;
783         }
784         force_sig_info(SIGFPE, &info, task);
785 }
786
787 dotraplinkage void
788 do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
789 {
790         conditional_sti(regs);
791
792 #ifdef CONFIG_X86_32
793         if (cpu_has_xmm) {
794                 /* Handle SIMD FPU exceptions on PIII+ processors. */
795                 ignore_fpu_irq = 1;
796                 simd_math_error((void __user *)regs->ip);
797                 return;
798         }
799         /*
800          * Handle strange cache flush from user space exception
801          * in all other cases.  This is undocumented behaviour.
802          */
803         if (regs->flags & X86_VM_MASK) {
804                 handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
805                 return;
806         }
807         current->thread.trap_no = 19;
808         current->thread.error_code = error_code;
809         die_if_kernel("cache flush denied", regs, error_code);
810         force_sig(SIGSEGV, current);
811 #else
812         if (!user_mode(regs) &&
813                         kernel_math_error(regs, "kernel simd math error", 19))
814                 return;
815         simd_math_error((void __user *)regs->ip);
816 #endif
817 }
818
819 dotraplinkage void
820 do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
821 {
822         conditional_sti(regs);
823 #if 0
824         /* No need to warn about this any longer. */
825         printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
826 #endif
827 }
828
829 #ifdef CONFIG_X86_32
830 unsigned long patch_espfix_desc(unsigned long uesp, unsigned long kesp)
831 {
832         struct desc_struct *gdt = get_cpu_gdt_table(smp_processor_id());
833         unsigned long base = (kesp - uesp) & -THREAD_SIZE;
834         unsigned long new_kesp = kesp - base;
835         unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
836         __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
837
838         /* Set up base for espfix segment */
839         desc &= 0x00f0ff0000000000ULL;
840         desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
841                 ((((__u64)base) << 32) & 0xff00000000000000ULL) |
842                 ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
843                 (lim_pages & 0xffff);
844         *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
845
846         return new_kesp;
847 }
848 #else
849 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
850 {
851 }
852
853 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
854 {
855 }
856 #endif
857
858 /*
859  * 'math_state_restore()' saves the current math information in the
860  * old math state array, and gets the new ones from the current task
861  *
862  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
863  * Don't touch unless you *really* know how it works.
864  *
865  * Must be called with kernel preemption disabled (in this case,
866  * local interrupts are disabled at the call-site in entry.S).
867  */
868 asmlinkage void math_state_restore(void)
869 {
870         struct thread_info *thread = current_thread_info();
871         struct task_struct *tsk = thread->task;
872
873         if (!tsk_used_math(tsk)) {
874                 local_irq_enable();
875                 /*
876                  * does a slab alloc which can sleep
877                  */
878                 if (init_fpu(tsk)) {
879                         /*
880                          * ran out of memory!
881                          */
882                         do_group_exit(SIGKILL);
883                         return;
884                 }
885                 local_irq_disable();
886         }
887
888         clts();                         /* Allow maths ops (or we recurse) */
889 #ifdef CONFIG_X86_32
890         restore_fpu(tsk);
891 #else
892         /*
893          * Paranoid restore. send a SIGSEGV if we fail to restore the state.
894          */
895         if (unlikely(restore_fpu_checking(tsk))) {
896                 stts();
897                 force_sig(SIGSEGV, tsk);
898                 return;
899         }
900 #endif
901         thread->status |= TS_USEDFPU;   /* So we fnsave on switch_to() */
902         tsk->fpu_counter++;
903 }
904 EXPORT_SYMBOL_GPL(math_state_restore);
905
906 #ifndef CONFIG_MATH_EMULATION
907 void math_emulate(struct math_emu_info *info)
908 {
909         printk(KERN_EMERG
910                 "math-emulation not enabled and no coprocessor found.\n");
911         printk(KERN_EMERG "killing %s.\n", current->comm);
912         force_sig(SIGFPE, current);
913         schedule();
914 }
915 #endif /* CONFIG_MATH_EMULATION */
916
917 dotraplinkage void __kprobes do_device_not_available(struct pt_regs regs)
918 {
919 #ifdef CONFIG_X86_32
920         if (read_cr0() & X86_CR0_EM) {
921                 struct math_emu_info info = { };
922
923                 conditional_sti(&regs);
924
925                 info.regs = &regs;
926                 math_emulate(&info);
927         } else {
928                 math_state_restore(); /* interrupts still off */
929                 conditional_sti(&regs);
930         }
931 #else
932         math_state_restore();
933 #endif
934 }
935
936 #ifdef CONFIG_X86_32
937 dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
938 {
939         siginfo_t info;
940         local_irq_enable();
941
942         info.si_signo = SIGILL;
943         info.si_errno = 0;
944         info.si_code = ILL_BADSTK;
945         info.si_addr = 0;
946         if (notify_die(DIE_TRAP, "iret exception",
947                         regs, error_code, 32, SIGILL) == NOTIFY_STOP)
948                 return;
949         do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
950 }
951 #endif
952
953 void __init trap_init(void)
954 {
955         int i;
956
957 #ifdef CONFIG_EISA
958         void __iomem *p = early_ioremap(0x0FFFD9, 4);
959
960         if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
961                 EISA_bus = 1;
962         early_iounmap(p, 4);
963 #endif
964
965         set_intr_gate(0, &divide_error);
966         set_intr_gate_ist(1, &debug, DEBUG_STACK);
967         set_intr_gate_ist(2, &nmi, NMI_STACK);
968         /* int3 can be called from all */
969         set_system_intr_gate_ist(3, &int3, DEBUG_STACK);
970         /* int4 can be called from all */
971         set_system_intr_gate(4, &overflow);
972         set_intr_gate(5, &bounds);
973         set_intr_gate(6, &invalid_op);
974         set_intr_gate(7, &device_not_available);
975 #ifdef CONFIG_X86_32
976         set_task_gate(8, GDT_ENTRY_DOUBLEFAULT_TSS);
977 #else
978         set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
979 #endif
980         set_intr_gate(9, &coprocessor_segment_overrun);
981         set_intr_gate(10, &invalid_TSS);
982         set_intr_gate(11, &segment_not_present);
983         set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
984         set_intr_gate(13, &general_protection);
985         set_intr_gate(14, &page_fault);
986         set_intr_gate(15, &spurious_interrupt_bug);
987         set_intr_gate(16, &coprocessor_error);
988         set_intr_gate(17, &alignment_check);
989 #ifdef CONFIG_X86_MCE
990         set_intr_gate_ist(18, &machine_check, MCE_STACK);
991 #endif
992         set_intr_gate(19, &simd_coprocessor_error);
993
994 #ifdef CONFIG_IA32_EMULATION
995         set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
996 #endif
997
998 #ifdef CONFIG_X86_32
999         if (cpu_has_fxsr) {
1000                 printk(KERN_INFO "Enabling fast FPU save and restore... ");
1001                 set_in_cr4(X86_CR4_OSFXSR);
1002                 printk("done.\n");
1003         }
1004         if (cpu_has_xmm) {
1005                 printk(KERN_INFO
1006                         "Enabling unmasked SIMD FPU exception support... ");
1007                 set_in_cr4(X86_CR4_OSXMMEXCPT);
1008                 printk("done.\n");
1009         }
1010
1011         set_system_trap_gate(SYSCALL_VECTOR, &system_call);
1012 #endif
1013
1014         /* Reserve all the builtin and the syscall vector: */
1015         for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
1016                 set_bit(i, used_vectors);
1017
1018 #ifdef CONFIG_X86_64
1019         set_bit(IA32_SYSCALL_VECTOR, used_vectors);
1020 #else
1021         set_bit(SYSCALL_VECTOR, used_vectors);
1022 #endif
1023         /*
1024          * Should be a barrier for any external CPU state:
1025          */
1026         cpu_init();
1027
1028 #ifdef CONFIG_X86_32
1029         trap_init_hook();
1030 #endif
1031 }