3d08b2d9c554cc45e762072821976dbba299dc9c
[linux-2.6.git] / arch / i386 / kernel / traps.c
1 /*
2  *  linux/arch/i386/traps.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  Pentium III FXSR, SSE support
7  *      Gareth Hughes <gareth@valinux.com>, May 2000
8  */
9
10 /*
11  * 'Traps.c' handles hardware traps and faults after we have saved some
12  * state in 'asm.s'.
13  */
14 #include <linux/config.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/timer.h>
20 #include <linux/mm.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <linux/highmem.h>
26 #include <linux/kallsyms.h>
27 #include <linux/ptrace.h>
28 #include <linux/version.h>
29
30 #ifdef CONFIG_EISA
31 #include <linux/ioport.h>
32 #include <linux/eisa.h>
33 #endif
34
35 #ifdef CONFIG_MCA
36 #include <linux/mca.h>
37 #endif
38
39 #include <asm/processor.h>
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
42 #include <asm/io.h>
43 #include <asm/atomic.h>
44 #include <asm/debugreg.h>
45 #include <asm/desc.h>
46 #include <asm/i387.h>
47 #include <asm/nmi.h>
48
49 #include <asm/smp.h>
50 #include <asm/arch_hooks.h>
51
52 #include <linux/irq.h>
53 #include <linux/module.h>
54
55 #include "mach_traps.h"
56
57 struct desc_struct default_ldt[] __attribute__((__section__(".data.default_ldt"))) = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
58 struct page *default_ldt_page;
59
60 /* Do we ignore FPU interrupts ? */
61 char ignore_fpu_irq = 0;
62
63 /*
64  * The IDT has to be page-aligned to simplify the Pentium
65  * F0 0F bug workaround.. We have a special link segment
66  * for this.
67  */
68 struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
69
70 asmlinkage void divide_error(void);
71 asmlinkage void debug(void);
72 asmlinkage void nmi(void);
73 asmlinkage void int3(void);
74 asmlinkage void overflow(void);
75 asmlinkage void bounds(void);
76 asmlinkage void invalid_op(void);
77 asmlinkage void device_not_available(void);
78 asmlinkage void coprocessor_segment_overrun(void);
79 asmlinkage void invalid_TSS(void);
80 asmlinkage void segment_not_present(void);
81 asmlinkage void stack_segment(void);
82 asmlinkage void general_protection(void);
83 asmlinkage void page_fault(void);
84 asmlinkage void coprocessor_error(void);
85 asmlinkage void simd_coprocessor_error(void);
86 asmlinkage void alignment_check(void);
87 asmlinkage void spurious_interrupt_bug(void);
88 asmlinkage void machine_check(void);
89
90 static int kstack_depth_to_print = 24;
91
92 static int valid_stack_ptr(struct task_struct *task, void *p)
93 {
94         if (p <= (void *)task->thread_info)
95                 return 0;
96         if (kstack_end(p))
97                 return 0;
98         return 1;
99 }
100
101 #ifdef CONFIG_FRAME_POINTER
102 void print_context_stack(struct task_struct *task, unsigned long *stack,
103                          unsigned long ebp)
104 {
105         unsigned long addr;
106
107         while (valid_stack_ptr(task, (void *)ebp)) {
108                 addr = *(unsigned long *)(ebp + 4);
109                 printk(" [<%08lx>] ", addr);
110                 print_symbol("%s", addr);
111                 printk("\n");
112                 ebp = *(unsigned long *)ebp;
113         }
114 }
115 #else
116 void print_context_stack(struct task_struct *task, unsigned long *stack,
117                          unsigned long ebp)
118 {
119         unsigned long addr;
120
121         while (!kstack_end(stack)) {
122                 addr = *stack++;
123                 if (kernel_text_address(addr)) {
124                         printk(" [<%08lx>] ", addr);
125                         print_symbol("%s\n", addr);
126                 }
127         }
128 }
129 #endif
130
131 void show_trace(struct task_struct *task, unsigned long * stack)
132 {
133         unsigned long ebp;
134
135         if (!task)
136                 task = current;
137
138         if (!valid_stack_ptr(task, stack)) {
139                 printk("Stack pointer is garbage, not printing trace\n");
140                 return;
141         }
142
143         if (task == current) {
144                 /* Grab ebp right from our regs */
145                 asm ("movl %%ebp, %0" : "=r" (ebp) : );
146         } else {
147                 /* ebp is the last reg pushed by switch_to */
148                 ebp = *(unsigned long *) task->thread.esp;
149         }
150
151         while (1) {
152                 struct thread_info *context;
153                 context = (struct thread_info *)
154                         ((unsigned long)stack & (~(THREAD_SIZE - 1)));
155                 print_context_stack(task, stack, ebp);
156                 stack = (unsigned long*)context->previous_esp;
157                 if (!stack)
158                         break;
159                 printk(" =======================\n");
160         }
161 }
162
163 void show_stack(struct task_struct *task, unsigned long *esp)
164 {
165         unsigned long *stack;
166         int i;
167
168         if (esp == NULL) {
169                 if (task)
170                         esp = (unsigned long*)task->thread.esp;
171                 else
172                         esp = (unsigned long *)&esp;
173         }
174
175         stack = esp;
176         for(i = 0; i < kstack_depth_to_print; i++) {
177                 if (kstack_end(stack))
178                         break;
179                 if (i && ((i % 8) == 0))
180                         printk("\n       ");
181                 printk("%08lx ", *stack++);
182         }
183         printk("\nCall Trace:\n");
184         show_trace(task, esp);
185 }
186
187 /*
188  * The architecture-independent dump_stack generator
189  */
190 void dump_stack(void)
191 {
192         unsigned long stack;
193
194         show_trace(current, &stack);
195 }
196
197 EXPORT_SYMBOL(dump_stack);
198
199 void show_registers(struct pt_regs *regs)
200 {
201         int i;
202         int in_kernel = 1;
203         unsigned long esp;
204         unsigned short ss;
205
206         esp = (unsigned long) (&regs->esp);
207         ss = __KERNEL_DS;
208         if (regs->xcs & 3) {
209                 in_kernel = 0;
210                 esp = regs->esp;
211                 ss = regs->xss & 0xffff;
212         }
213         print_modules();
214         printk("CPU:    %d\nEIP:    %04x:[<%08lx>]    %s\nEFLAGS: %08lx"
215                         "   (%s) \n",
216                 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
217                 print_tainted(), regs->eflags, UTS_RELEASE);
218         print_symbol("EIP is at %s\n", regs->eip);
219         printk("eax: %08lx   ebx: %08lx   ecx: %08lx   edx: %08lx\n",
220                 regs->eax, regs->ebx, regs->ecx, regs->edx);
221         printk("esi: %08lx   edi: %08lx   ebp: %08lx   esp: %08lx\n",
222                 regs->esi, regs->edi, regs->ebp, esp);
223         printk("ds: %04x   es: %04x   ss: %04x\n",
224                 regs->xds & 0xffff, regs->xes & 0xffff, ss);
225         printk("Process %s (pid: %d, threadinfo=%p task=%p)",
226                 current->comm, current->pid, current_thread_info(), current);
227         /*
228          * When in-kernel, we also print out the stack and code at the
229          * time of the fault..
230          */
231         if (in_kernel) {
232
233                 printk("\nStack: ");
234                 show_stack(NULL, (unsigned long*)esp);
235
236                 printk("Code: ");
237                 if(regs->eip < PAGE_OFFSET)
238                         goto bad;
239
240                 for(i=0;i<20;i++)
241                 {
242                         unsigned char c = 0;
243                         if ((user_mode(regs) && get_user(c, &((unsigned char*)regs->eip)[i])) ||
244                             (!user_mode(regs) && __direct_get_user(c, &((unsigned char*)regs->eip)[i]))) {
245                         
246 bad:
247                                 printk(" Bad EIP value.");
248                                 break;
249                         }
250                         printk("%02x ", c);
251                 }
252         }
253         printk("\n");
254 }       
255
256 static void handle_BUG(struct pt_regs *regs)
257 {
258         unsigned short ud2;
259         unsigned short line;
260         char *file;
261         char c;
262         unsigned long eip;
263
264         if (regs->xcs & 3)
265                 goto no_bug;            /* Not in kernel */
266
267         eip = regs->eip;
268
269         if (__direct_get_user(ud2, (unsigned short *)eip))
270                 goto no_bug;
271         if (ud2 != 0x0b0f)
272                 goto no_bug;
273         if (__direct_get_user(line, (unsigned short *)(eip + 2)))
274                 goto bug;
275         if (__direct_get_user(file, (char **)(eip + 4)) ||
276                         __direct_get_user(c, file))
277                 file = "<bad filename>";
278
279         printk("------------[ cut here ]------------\n");
280         printk(KERN_ALERT "kernel BUG at %s:%d!\n", file, line);
281
282 no_bug:
283         return;
284
285         /* Here we know it was a BUG but file-n-line is unavailable */
286 bug:
287         printk("Kernel BUG\n");
288 }
289
290 spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
291
292 void die(const char * str, struct pt_regs * regs, long err)
293 {
294         static int die_counter;
295         int nl = 0;
296
297         console_verbose();
298         spin_lock_irq(&die_lock);
299         bust_spinlocks(1);
300         handle_BUG(regs);
301         printk(KERN_ALERT "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
302 #ifdef CONFIG_PREEMPT
303         printk("PREEMPT ");
304         nl = 1;
305 #endif
306 #ifdef CONFIG_SMP
307         printk("SMP ");
308         nl = 1;
309 #endif
310 #ifdef CONFIG_DEBUG_PAGEALLOC
311         printk("DEBUG_PAGEALLOC");
312         nl = 1;
313 #endif
314         if (nl)
315                 printk("\n");
316         show_registers(regs);
317         bust_spinlocks(0);
318         spin_unlock_irq(&die_lock);
319         if (in_interrupt())
320                 panic("Fatal exception in interrupt");
321
322         if (panic_on_oops) {
323                 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
324                 set_current_state(TASK_UNINTERRUPTIBLE);
325                 schedule_timeout(5 * HZ);
326                 panic("Fatal exception");
327         }
328         do_exit(SIGSEGV);
329 }
330
331 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
332 {
333         if (!(regs->eflags & VM_MASK) && !(3 & regs->xcs))
334                 die(str, regs, err);
335 }
336
337 static inline unsigned long get_cr2(void)
338 {
339         unsigned long address;
340
341         /* get the address */
342         __asm__("movl %%cr2,%0":"=r" (address));
343         return address;
344 }
345
346 static inline void do_trap(int trapnr, int signr, char *str, int vm86,
347                            struct pt_regs * regs, long error_code, siginfo_t *info)
348 {
349         if (regs->eflags & VM_MASK) {
350                 if (vm86)
351                         goto vm86_trap;
352                 goto trap_signal;
353         }
354
355         if (!(regs->xcs & 3))
356                 goto kernel_trap;
357
358         trap_signal: {
359                 struct task_struct *tsk = current;
360                 tsk->thread.error_code = error_code;
361                 tsk->thread.trap_no = trapnr;
362                 if (info)
363                         force_sig_info(signr, info, tsk);
364                 else
365                         force_sig(signr, tsk);
366                 return;
367         }
368
369         kernel_trap: {
370                 if (!fixup_exception(regs))
371                         die(str, regs, error_code);
372                 return;
373         }
374
375         vm86_trap: {
376                 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
377                 if (ret) goto trap_signal;
378                 return;
379         }
380 }
381
382 #define DO_ERROR(trapnr, signr, str, name) \
383 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
384 { \
385         do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
386 }
387
388 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
389 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
390 { \
391         siginfo_t info; \
392         info.si_signo = signr; \
393         info.si_errno = 0; \
394         info.si_code = sicode; \
395         info.si_addr = (void *)siaddr; \
396         do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
397 }
398
399 #define DO_VM86_ERROR(trapnr, signr, str, name) \
400 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
401 { \
402         do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
403 }
404
405 #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
406 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
407 { \
408         siginfo_t info; \
409         info.si_signo = signr; \
410         info.si_errno = 0; \
411         info.si_code = sicode; \
412         info.si_addr = (void *)siaddr; \
413         do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
414 }
415
416 DO_VM86_ERROR_INFO( 0, SIGFPE,  "divide error", divide_error, FPE_INTDIV, regs->eip)
417 DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
418 DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
419 DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
420 DO_ERROR_INFO( 6, SIGILL,  "invalid operand", invalid_op, ILL_ILLOPN, regs->eip)
421 DO_ERROR( 9, SIGFPE,  "coprocessor segment overrun", coprocessor_segment_overrun)
422 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
423 DO_ERROR(11, SIGBUS,  "segment not present", segment_not_present)
424 DO_ERROR(12, SIGBUS,  "stack segment", stack_segment)
425 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, get_cr2())
426
427 /*
428  * the original non-exec stack patch was written by
429  * Solar Designer <solar at openwall.com>. Thanks!
430  */
431 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
432 {
433         if (regs->eflags & X86_EFLAGS_IF)
434                 local_irq_enable();
435  
436         if (regs->eflags & VM_MASK)
437                 goto gp_in_vm86;
438
439         if (!(regs->xcs & 3))
440                 goto gp_in_kernel;
441
442         /*
443          * lazy-check for CS validity on exec-shield binaries:
444          */
445         if (current->mm) {
446                 int cpu = smp_processor_id();
447                 struct desc_struct *desc1, *desc2;
448                 struct vm_area_struct *vma;
449                 unsigned long limit = 0;
450                 
451                 spin_lock(&current->mm->page_table_lock);
452                 for (vma = current->mm->mmap; vma; vma = vma->vm_next)
453                         if ((vma->vm_flags & VM_EXEC) && (vma->vm_end > limit))
454                                 limit = vma->vm_end;
455                 spin_unlock(&current->mm->page_table_lock);
456
457                 current->mm->context.exec_limit = limit;
458                 set_user_cs(&current->mm->context.user_cs, limit);
459
460                 desc1 = &current->mm->context.user_cs;
461                 desc2 = cpu_gdt_table[cpu] + GDT_ENTRY_DEFAULT_USER_CS;
462
463                 /*
464                  * The CS was not in sync - reload it and retry the
465                  * instruction. If the instruction still faults then
466                  * we wont hit this branch next time around.
467                  */
468                 if (desc1->a != desc2->a || desc1->b != desc2->b) {
469                         if (print_fatal_signals >= 2) {
470                                 printk("#GPF fixup (%ld[seg:%lx]) at %08lx, CPU#%d.\n", error_code, error_code/8, regs->eip, smp_processor_id());
471                                 printk(" exec_limit: %08lx, user_cs: %08lx/%08lx, CPU_cs: %08lx/%08lx.\n", current->mm->context.exec_limit, desc1->a, desc1->b, desc2->a, desc2->b);
472                         }
473                         load_user_cs_desc(cpu, current->mm);
474                         return;
475                 }
476         }
477         if (print_fatal_signals) {
478                 printk("#GPF(%ld[seg:%lx]) at %08lx, CPU#%d.\n", error_code, error_code/8, regs->eip, smp_processor_id());
479                 printk(" exec_limit: %08lx, user_cs: %08lx/%08lx.\n", current->mm->context.exec_limit, current->mm->context.user_cs.a, current->mm->context.user_cs.b);
480         }
481
482         current->thread.error_code = error_code;
483         current->thread.trap_no = 13;
484         force_sig(SIGSEGV, current);
485         return;
486
487 gp_in_vm86:
488         local_irq_enable();
489         handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
490         return;
491
492 gp_in_kernel:
493         if (!fixup_exception(regs))
494                 die("general protection fault", regs, error_code);
495 }
496
497 static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
498 {
499         printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
500         printk("You probably have a hardware problem with your RAM chips\n");
501
502         /* Clear and disable the memory parity error line. */
503         clear_mem_error(reason);
504 }
505
506 static void io_check_error(unsigned char reason, struct pt_regs * regs)
507 {
508         unsigned long i;
509
510         printk("NMI: IOCK error (debug interrupt?)\n");
511         show_registers(regs);
512
513         /* Re-enable the IOCK line, wait for a few seconds */
514         reason = (reason & 0xf) | 8;
515         outb(reason, 0x61);
516         i = 2000;
517         while (--i) udelay(1000);
518         reason &= ~8;
519         outb(reason, 0x61);
520 }
521
522 static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
523 {
524 #ifdef CONFIG_MCA
525         /* Might actually be able to figure out what the guilty party
526         * is. */
527         if( MCA_bus ) {
528                 mca_handle_nmi();
529                 return;
530         }
531 #endif
532         printk("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
533                 reason, smp_processor_id());
534         printk("Dazed and confused, but trying to continue\n");
535         printk("Do you have a strange power saving mode enabled?\n");
536 }
537
538 static void default_do_nmi(struct pt_regs * regs)
539 {
540         unsigned char reason = get_nmi_reason();
541  
542         if (!(reason & 0xc0)) {
543 #ifdef CONFIG_X86_LOCAL_APIC
544                 /*
545                  * Ok, so this is none of the documented NMI sources,
546                  * so it must be the NMI watchdog.
547                  */
548                 if (nmi_watchdog) {
549                         nmi_watchdog_tick(regs);
550                         return;
551                 }
552 #endif
553                 unknown_nmi_error(reason, regs);
554                 return;
555         }
556         if (reason & 0x80)
557                 mem_parity_error(reason, regs);
558         if (reason & 0x40)
559                 io_check_error(reason, regs);
560         /*
561          * Reassert NMI in case it became active meanwhile
562          * as it's edge-triggered.
563          */
564         reassert_nmi();
565 }
566
567 static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
568 {
569         return 0;
570 }
571  
572 static nmi_callback_t nmi_callback = dummy_nmi_callback;
573  
574 asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
575 {
576         int cpu;
577
578         nmi_enter();
579
580         cpu = smp_processor_id();
581         ++nmi_count(cpu);
582
583         if (!nmi_callback(regs, cpu))
584                 default_do_nmi(regs);
585
586         nmi_exit();
587 }
588
589 void set_nmi_callback(nmi_callback_t callback)
590 {
591         nmi_callback = callback;
592 }
593
594 void unset_nmi_callback(void)
595 {
596         nmi_callback = dummy_nmi_callback;
597 }
598
599 /*
600  * Our handling of the processor debug registers is non-trivial.
601  * We do not clear them on entry and exit from the kernel. Therefore
602  * it is possible to get a watchpoint trap here from inside the kernel.
603  * However, the code in ./ptrace.c has ensured that the user can
604  * only set watchpoints on userspace addresses. Therefore the in-kernel
605  * watchpoint trap can only occur in code which is reading/writing
606  * from user space. Such code must not hold kernel locks (since it
607  * can equally take a page fault), therefore it is safe to call
608  * force_sig_info even though that claims and releases locks.
609  * 
610  * Code in ./signal.c ensures that the debug control register
611  * is restored before we deliver any signal, and therefore that
612  * user code runs with the correct debug control register even though
613  * we clear it here.
614  *
615  * Being careful here means that we don't have to be as careful in a
616  * lot of more complicated places (task switching can be a bit lazy
617  * about restoring all the debug state, and ptrace doesn't have to
618  * find every occurrence of the TF bit that could be saved away even
619  * by user code)
620  */
621 asmlinkage void do_debug(struct pt_regs * regs, long error_code)
622 {
623         unsigned int condition;
624         struct task_struct *tsk = current;
625         siginfo_t info;
626
627         __asm__ __volatile__("movl %%db6,%0" : "=r" (condition));
628
629         /* It's safe to allow irq's after DR6 has been saved */
630         if (regs->eflags & X86_EFLAGS_IF)
631                 local_irq_enable();
632
633         /*
634          * Mask out spurious debug traps due to lazy DR7 setting or
635          * due to 4G/4G kernel mode:
636          */
637         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
638                 if (!tsk->thread.debugreg[7])
639                         goto clear_dr7;
640                 if (!user_mode(regs)) {
641                         // restore upon return-to-userspace:
642                         set_thread_flag(TIF_DB7);
643                         goto clear_dr7;
644                 }
645         }
646
647         if (regs->eflags & VM_MASK)
648                 goto debug_vm86;
649
650         /* Save debug status register where ptrace can see it */
651         tsk->thread.debugreg[6] = condition;
652
653         /* Mask out spurious TF errors due to lazy TF clearing */
654         if (condition & DR_STEP) {
655                 /*
656                  * The TF error should be masked out only if the current
657                  * process is not traced and if the TRAP flag has been set
658                  * previously by a tracing process (condition detected by
659                  * the PT_DTRACE flag); remember that the i386 TRAP flag
660                  * can be modified by the process itself in user mode,
661                  * allowing programs to debug themselves without the ptrace()
662                  * interface.
663                  */
664                 if ((regs->xcs & 3) == 0)
665                         goto clear_TF_reenable;
666                 if ((tsk->ptrace & (PT_DTRACE|PT_PTRACED)) == PT_DTRACE)
667                         goto clear_TF;
668         }
669
670         /* Ok, finally something we can handle */
671         tsk->thread.trap_no = 1;
672         tsk->thread.error_code = error_code;
673         info.si_signo = SIGTRAP;
674         info.si_errno = 0;
675         info.si_code = TRAP_BRKPT;
676         
677         /* If this is a kernel mode trap, save the user PC on entry to 
678          * the kernel, that's what the debugger can make sense of.
679          */
680         info.si_addr = ((regs->xcs & 3) == 0) ? (void *)tsk->thread.eip : 
681                                                 (void *)regs->eip;
682         force_sig_info(SIGTRAP, &info, tsk);
683
684         /* Disable additional traps. They'll be re-enabled when
685          * the signal is delivered.
686          */
687 clear_dr7:
688         __asm__("movl %0,%%db7"
689                 : /* no output */
690                 : "r" (0));
691         return;
692
693 debug_vm86:
694         handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
695         return;
696
697 clear_TF_reenable:
698         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
699 clear_TF:
700         regs->eflags &= ~TF_MASK;
701         return;
702 }
703
704 /*
705  * Note that we play around with the 'TS' bit in an attempt to get
706  * the correct behaviour even in the presence of the asynchronous
707  * IRQ13 behaviour
708  */
709 void math_error(void *eip)
710 {
711         struct task_struct * task;
712         siginfo_t info;
713         unsigned short cwd, swd;
714
715         /*
716          * Save the info for the exception handler and clear the error.
717          */
718         task = current;
719         save_init_fpu(task);
720         task->thread.trap_no = 16;
721         task->thread.error_code = 0;
722         info.si_signo = SIGFPE;
723         info.si_errno = 0;
724         info.si_code = __SI_FAULT;
725         info.si_addr = eip;
726         /*
727          * (~cwd & swd) will mask out exceptions that are not set to unmasked
728          * status.  0x3f is the exception bits in these regs, 0x200 is the
729          * C1 reg you need in case of a stack fault, 0x040 is the stack
730          * fault bit.  We should only be taking one exception at a time,
731          * so if this combination doesn't produce any single exception,
732          * then we have a bad program that isn't syncronizing its FPU usage
733          * and it will suffer the consequences since we won't be able to
734          * fully reproduce the context of the exception
735          */
736         cwd = get_fpu_cwd(task);
737         swd = get_fpu_swd(task);
738         switch (((~cwd) & swd & 0x3f) | (swd & 0x240)) {
739                 case 0x000:
740                 default:
741                         break;
742                 case 0x001: /* Invalid Op */
743                 case 0x041: /* Stack Fault */
744                 case 0x241: /* Stack Fault | Direction */
745                         info.si_code = FPE_FLTINV;
746                         /* Should we clear the SF or let user space do it ???? */
747                         break;
748                 case 0x002: /* Denormalize */
749                 case 0x010: /* Underflow */
750                         info.si_code = FPE_FLTUND;
751                         break;
752                 case 0x004: /* Zero Divide */
753                         info.si_code = FPE_FLTDIV;
754                         break;
755                 case 0x008: /* Overflow */
756                         info.si_code = FPE_FLTOVF;
757                         break;
758                 case 0x020: /* Precision */
759                         info.si_code = FPE_FLTRES;
760                         break;
761         }
762         force_sig_info(SIGFPE, &info, task);
763 }
764
765 asmlinkage void do_coprocessor_error(struct pt_regs * regs, long error_code)
766 {
767         ignore_fpu_irq = 1;
768         math_error((void *)regs->eip);
769 }
770
771 void simd_math_error(void *eip)
772 {
773         struct task_struct * task;
774         siginfo_t info;
775         unsigned short mxcsr;
776
777         /*
778          * Save the info for the exception handler and clear the error.
779          */
780         task = current;
781         save_init_fpu(task);
782         task->thread.trap_no = 19;
783         task->thread.error_code = 0;
784         info.si_signo = SIGFPE;
785         info.si_errno = 0;
786         info.si_code = __SI_FAULT;
787         info.si_addr = eip;
788         /*
789          * The SIMD FPU exceptions are handled a little differently, as there
790          * is only a single status/control register.  Thus, to determine which
791          * unmasked exception was caught we must mask the exception mask bits
792          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
793          */
794         mxcsr = get_fpu_mxcsr(task);
795         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
796                 case 0x000:
797                 default:
798                         break;
799                 case 0x001: /* Invalid Op */
800                         info.si_code = FPE_FLTINV;
801                         break;
802                 case 0x002: /* Denormalize */
803                 case 0x010: /* Underflow */
804                         info.si_code = FPE_FLTUND;
805                         break;
806                 case 0x004: /* Zero Divide */
807                         info.si_code = FPE_FLTDIV;
808                         break;
809                 case 0x008: /* Overflow */
810                         info.si_code = FPE_FLTOVF;
811                         break;
812                 case 0x020: /* Precision */
813                         info.si_code = FPE_FLTRES;
814                         break;
815         }
816         force_sig_info(SIGFPE, &info, task);
817 }
818
819 asmlinkage void do_simd_coprocessor_error(struct pt_regs * regs,
820                                           long error_code)
821 {
822         if (cpu_has_xmm) {
823                 /* Handle SIMD FPU exceptions on PIII+ processors. */
824                 ignore_fpu_irq = 1;
825                 simd_math_error((void *)regs->eip);
826         } else {
827                 /*
828                  * Handle strange cache flush from user space exception
829                  * in all other cases.  This is undocumented behaviour.
830                  */
831                 if (regs->eflags & VM_MASK) {
832                         handle_vm86_fault((struct kernel_vm86_regs *)regs,
833                                           error_code);
834                         return;
835                 }
836                 die_if_kernel("cache flush denied", regs, error_code);
837                 current->thread.trap_no = 19;
838                 current->thread.error_code = error_code;
839                 force_sig(SIGSEGV, current);
840         }
841 }
842
843 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs,
844                                           long error_code)
845 {
846 #if 0
847         /* No need to warn about this any longer. */
848         printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
849 #endif
850 }
851
852 /*
853  *  'math_state_restore()' saves the current math information in the
854  * old math state array, and gets the new ones from the current task
855  *
856  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
857  * Don't touch unless you *really* know how it works.
858  *
859  * Must be called with kernel preemption disabled (in this case,
860  * local interrupts are disabled at the call-site in entry.S).
861  */
862 asmlinkage void math_state_restore(struct pt_regs regs)
863 {
864         struct thread_info *thread = current_thread_info();
865         struct task_struct *tsk = thread->task;
866
867         clts();         /* Allow maths ops (or we recurse) */
868         if (!tsk->used_math)
869                 init_fpu(tsk);
870         restore_fpu(tsk);
871         thread->status |= TS_USEDFPU;   /* So we fnsave on switch_to() */
872 }
873
874 #ifndef CONFIG_MATH_EMULATION
875
876 asmlinkage void math_emulate(long arg)
877 {
878         printk("math-emulation not enabled and no coprocessor found.\n");
879         printk("killing %s.\n",current->comm);
880         force_sig(SIGFPE,current);
881         schedule();
882 }
883
884 #endif /* CONFIG_MATH_EMULATION */
885
886 void __init trap_init_virtual_IDT(void)
887 {
888         /*
889          * "idt" is magic - it overlaps the idt_descr
890          * variable so that updating idt will automatically
891          * update the idt descriptor..
892          */
893         __set_fixmap(FIX_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
894         idt_descr.address = __fix_to_virt(FIX_IDT);
895
896         __asm__ __volatile__("lidt %0" : : "m" (idt_descr));
897 }
898
899 void __init trap_init_virtual_GDT(void)
900 {
901         int cpu = smp_processor_id();
902         struct Xgt_desc_struct *gdt_desc = cpu_gdt_descr + cpu;
903         struct Xgt_desc_struct tmp_desc = {0, 0};
904         struct tss_struct * t;
905
906         __asm__ __volatile__("sgdt %0": "=m" (tmp_desc): :"memory");
907
908 #ifdef CONFIG_X86_HIGH_ENTRY
909         if (!cpu) {
910                 __set_fixmap(FIX_GDT_0, __pa(cpu_gdt_table), PAGE_KERNEL);
911                 __set_fixmap(FIX_GDT_1, __pa(cpu_gdt_table) + PAGE_SIZE, PAGE_KERNEL);
912                 __set_fixmap(FIX_TSS_0, __pa(init_tss), PAGE_KERNEL);
913                 __set_fixmap(FIX_TSS_1, __pa(init_tss) + 1*PAGE_SIZE, PAGE_KERNEL);
914                 __set_fixmap(FIX_TSS_2, __pa(init_tss) + 2*PAGE_SIZE, PAGE_KERNEL);
915                 __set_fixmap(FIX_TSS_3, __pa(init_tss) + 3*PAGE_SIZE, PAGE_KERNEL);
916         }
917
918         gdt_desc->address = __fix_to_virt(FIX_GDT_0) + sizeof(cpu_gdt_table[0]) * cpu;
919 #else
920         gdt_desc->address = (unsigned long)cpu_gdt_table[cpu];
921 #endif
922         __asm__ __volatile__("lgdt %0": "=m" (*gdt_desc));
923
924 #ifdef CONFIG_X86_HIGH_ENTRY
925         t = (struct tss_struct *) __fix_to_virt(FIX_TSS_0) + cpu;
926 #else
927         t = init_tss + cpu;
928 #endif
929         set_tss_desc(cpu, t);
930         cpu_gdt_table[cpu][GDT_ENTRY_TSS].b &= 0xfffffdff;
931         load_TR_desc();
932 }
933
934 #define _set_gate(gate_addr,type,dpl,addr,seg) \
935 do { \
936   int __d0, __d1; \
937   __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
938         "movw %4,%%dx\n\t" \
939         "movl %%eax,%0\n\t" \
940         "movl %%edx,%1" \
941         :"=m" (*((long *) (gate_addr))), \
942          "=m" (*(1+(long *) (gate_addr))), "=&a" (__d0), "=&d" (__d1) \
943         :"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
944          "3" ((char *) (addr)),"2" ((seg) << 16)); \
945 } while (0)
946
947
948 /*
949  * This needs to use 'idt_table' rather than 'idt', and
950  * thus use the _nonmapped_ version of the IDT, as the
951  * Pentium F0 0F bugfix can have resulted in the mapped
952  * IDT being write-protected.
953  */
954 void set_intr_gate(unsigned int n, void *addr)
955 {
956         _set_gate(idt_table+n,14,0,addr,__KERNEL_CS);
957 }
958
959 void __init set_trap_gate(unsigned int n, void *addr)
960 {
961         _set_gate(idt_table+n,15,0,addr,__KERNEL_CS);
962 }
963
964 void __init set_system_gate(unsigned int n, void *addr)
965 {
966         _set_gate(idt_table+n,15,3,addr,__KERNEL_CS);
967 }
968
969 void __init set_call_gate(void *a, void *addr)
970 {
971         _set_gate(a,12,3,addr,__KERNEL_CS);
972 }
973
974 static void __init set_task_gate(unsigned int n, unsigned int gdt_entry)
975 {
976         _set_gate(idt_table+n,5,0,0,(gdt_entry<<3));
977 }
978
979
980 void __init trap_init(void)
981 {
982 #ifdef CONFIG_EISA
983         if (isa_readl(0x0FFFD9) == 'E'+('I'<<8)+('S'<<16)+('A'<<24)) {
984                 EISA_bus = 1;
985         }
986 #endif
987
988 #ifdef CONFIG_X86_LOCAL_APIC
989         init_apic_mappings();
990 #endif
991         init_entry_mappings();
992
993         set_trap_gate(0,&divide_error);
994         set_intr_gate(1,&debug);
995         set_intr_gate(2,&nmi);
996         set_system_gate(3,&int3);       /* int3-5 can be called from all */
997         set_system_gate(4,&overflow);
998         set_system_gate(5,&bounds);
999         set_trap_gate(6,&invalid_op);
1000         set_trap_gate(7,&device_not_available);
1001         set_task_gate(8,GDT_ENTRY_DOUBLEFAULT_TSS);
1002         set_trap_gate(9,&coprocessor_segment_overrun);
1003         set_trap_gate(10,&invalid_TSS);
1004         set_trap_gate(11,&segment_not_present);
1005         set_trap_gate(12,&stack_segment);
1006         set_trap_gate(13,&general_protection);
1007         set_intr_gate(14,&page_fault);
1008         set_trap_gate(15,&spurious_interrupt_bug);
1009         set_trap_gate(16,&coprocessor_error);
1010         set_trap_gate(17,&alignment_check);
1011 #ifdef CONFIG_X86_MCE
1012         set_trap_gate(18,&machine_check);
1013 #endif
1014         set_trap_gate(19,&simd_coprocessor_error);
1015
1016         set_system_gate(SYSCALL_VECTOR,&system_call);
1017
1018         /*
1019          * default LDT is a single-entry callgate to lcall7 for iBCS
1020          * and a callgate to lcall27 for Solaris/x86 binaries
1021          */
1022 #if 0    
1023         set_call_gate(&default_ldt[0],lcall7);
1024         set_call_gate(&default_ldt[4],lcall27);
1025 #endif
1026         /*
1027          * Should be a barrier for any external CPU state.
1028          */
1029         cpu_init();
1030
1031         trap_init_hook();
1032 }