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