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