ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/kernel/traps.c @ 6427:3428d58a85e1

merge?
author cl349@firebug.cl.cam.ac.uk
date Thu Aug 25 14:41:52 2005 +0000 (2005-08-25)
parents 4abd299ef2f6 8d31f9a9c423
children b54144915ae6
line source
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 */
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>
31 #ifdef CONFIG_EISA
32 #include <linux/ioport.h>
33 #include <linux/eisa.h>
34 #endif
36 #ifdef CONFIG_MCA
37 #include <linux/mca.h>
38 #endif
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>
50 #include <asm/smp.h>
51 #include <asm/arch_hooks.h>
52 #include <asm/kdebug.h>
54 #include <linux/irq.h>
55 #include <linux/module.h>
57 #include "mach_traps.h"
59 asmlinkage int system_call(void);
61 /* Do we ignore FPU interrupts ? */
62 char ignore_fpu_irq = 0;
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}, };
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 fixup_4gb_segment(void);
89 asmlinkage void machine_check(void);
91 static int kstack_depth_to_print = 24;
92 struct notifier_block *i386die_chain;
93 static DEFINE_SPINLOCK(die_notifier_lock);
95 int register_die_notifier(struct notifier_block *nb)
96 {
97 int err = 0;
98 unsigned long flags;
99 spin_lock_irqsave(&die_notifier_lock, flags);
100 err = notifier_chain_register(&i386die_chain, nb);
101 spin_unlock_irqrestore(&die_notifier_lock, flags);
102 return err;
103 }
105 static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
106 {
107 return p > (void *)tinfo &&
108 p < (void *)tinfo + THREAD_SIZE - 3;
109 }
111 static inline unsigned long print_context_stack(struct thread_info *tinfo,
112 unsigned long *stack, unsigned long ebp)
113 {
114 unsigned long addr;
116 #ifdef CONFIG_FRAME_POINTER
117 while (valid_stack_ptr(tinfo, (void *)ebp)) {
118 addr = *(unsigned long *)(ebp + 4);
119 printk(" [<%08lx>] ", addr);
120 print_symbol("%s", addr);
121 printk("\n");
122 ebp = *(unsigned long *)ebp;
123 }
124 #else
125 while (valid_stack_ptr(tinfo, stack)) {
126 addr = *stack++;
127 if (__kernel_text_address(addr)) {
128 printk(" [<%08lx>]", addr);
129 print_symbol(" %s", addr);
130 printk("\n");
131 }
132 }
133 #endif
134 return ebp;
135 }
137 void show_trace(struct task_struct *task, unsigned long * stack)
138 {
139 unsigned long ebp;
141 if (!task)
142 task = current;
144 if (task == current) {
145 /* Grab ebp right from our regs */
146 asm ("movl %%ebp, %0" : "=r" (ebp) : );
147 } else {
148 /* ebp is the last reg pushed by switch_to */
149 ebp = *(unsigned long *) task->thread.esp;
150 }
152 while (1) {
153 struct thread_info *context;
154 context = (struct thread_info *)
155 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
156 ebp = print_context_stack(context, stack, ebp);
157 stack = (unsigned long*)context->previous_esp;
158 if (!stack)
159 break;
160 printk(" =======================\n");
161 }
162 }
164 void show_stack(struct task_struct *task, unsigned long *esp)
165 {
166 unsigned long *stack;
167 int i;
169 if (esp == NULL) {
170 if (task)
171 esp = (unsigned long*)task->thread.esp;
172 else
173 esp = (unsigned long *)&esp;
174 }
176 stack = esp;
177 for(i = 0; i < kstack_depth_to_print; i++) {
178 if (kstack_end(stack))
179 break;
180 if (i && ((i % 8) == 0))
181 printk("\n ");
182 printk("%08lx ", *stack++);
183 }
184 printk("\nCall Trace:\n");
185 show_trace(task, esp);
186 }
188 /*
189 * The architecture-independent dump_stack generator
190 */
191 void dump_stack(void)
192 {
193 unsigned long stack;
195 show_trace(current, &stack);
196 }
198 EXPORT_SYMBOL(dump_stack);
200 void show_registers(struct pt_regs *regs)
201 {
202 int i;
203 int in_kernel = 1;
204 unsigned long esp;
205 unsigned short ss;
207 esp = (unsigned long) (&regs->esp);
208 ss = __KERNEL_DS;
209 if (regs->xcs & 2) {
210 in_kernel = 0;
211 esp = regs->esp;
212 ss = regs->xss & 0xffff;
213 }
214 print_modules();
215 printk("CPU: %d\nEIP: %04x:[<%08lx>] %s VLI\nEFLAGS: %08lx"
216 " (%s) \n",
217 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
218 print_tainted(), regs->eflags, system_utsname.release);
219 print_symbol("EIP is at %s\n", regs->eip);
220 printk("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
221 regs->eax, regs->ebx, regs->ecx, regs->edx);
222 printk("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
223 regs->esi, regs->edi, regs->ebp, esp);
224 printk("ds: %04x es: %04x ss: %04x\n",
225 regs->xds & 0xffff, regs->xes & 0xffff, ss);
226 printk("Process %s (pid: %d, threadinfo=%p task=%p)",
227 current->comm, current->pid, current_thread_info(), current);
228 /*
229 * When in-kernel, we also print out the stack and code at the
230 * time of the fault..
231 */
232 if (in_kernel) {
233 u8 *eip;
235 printk("\nStack: ");
236 show_stack(NULL, (unsigned long*)esp);
238 printk("Code: ");
240 eip = (u8 *)regs->eip - 43;
241 for (i = 0; i < 64; i++, eip++) {
242 unsigned char c;
244 if (eip < (u8 *)PAGE_OFFSET || __get_user(c, eip)) {
245 printk(" Bad EIP value.");
246 break;
247 }
248 if (eip == (u8 *)regs->eip)
249 printk("<%02x> ", c);
250 else
251 printk("%02x ", c);
252 }
253 }
254 printk("\n");
255 }
257 static void handle_BUG(struct pt_regs *regs)
258 {
259 unsigned short ud2;
260 unsigned short line;
261 char *file;
262 char c;
263 unsigned long eip;
265 if (regs->xcs & 2)
266 goto no_bug; /* Not in kernel */
268 eip = regs->eip;
270 if (eip < PAGE_OFFSET)
271 goto no_bug;
272 if (__get_user(ud2, (unsigned short *)eip))
273 goto no_bug;
274 if (ud2 != 0x0b0f)
275 goto no_bug;
276 if (__get_user(line, (unsigned short *)(eip + 2)))
277 goto bug;
278 if (__get_user(file, (char **)(eip + 4)) ||
279 (unsigned long)file < PAGE_OFFSET || __get_user(c, file))
280 file = "<bad filename>";
282 printk("------------[ cut here ]------------\n");
283 printk(KERN_ALERT "kernel BUG at %s:%d!\n", file, line);
285 no_bug:
286 return;
288 /* Here we know it was a BUG but file-n-line is unavailable */
289 bug:
290 printk("Kernel BUG\n");
291 }
293 void die(const char * str, struct pt_regs * regs, long err)
294 {
295 static struct {
296 spinlock_t lock;
297 u32 lock_owner;
298 int lock_owner_depth;
299 } die = {
300 .lock = SPIN_LOCK_UNLOCKED,
301 .lock_owner = -1,
302 .lock_owner_depth = 0
303 };
304 static int die_counter;
306 if (die.lock_owner != _smp_processor_id()) {
307 console_verbose();
308 spin_lock_irq(&die.lock);
309 die.lock_owner = smp_processor_id();
310 die.lock_owner_depth = 0;
311 bust_spinlocks(1);
312 }
314 if (++die.lock_owner_depth < 3) {
315 int nl = 0;
316 handle_BUG(regs);
317 printk(KERN_ALERT "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
318 #ifdef CONFIG_PREEMPT
319 printk("PREEMPT ");
320 nl = 1;
321 #endif
322 #ifdef CONFIG_SMP
323 printk("SMP ");
324 nl = 1;
325 #endif
326 #ifdef CONFIG_DEBUG_PAGEALLOC
327 printk("DEBUG_PAGEALLOC");
328 nl = 1;
329 #endif
330 if (nl)
331 printk("\n");
332 notify_die(DIE_OOPS, (char *)str, regs, err, 255, SIGSEGV);
333 show_registers(regs);
334 } else
335 printk(KERN_ERR "Recursive die() failure, output suppressed\n");
337 bust_spinlocks(0);
338 die.lock_owner = -1;
339 spin_unlock_irq(&die.lock);
340 if (in_interrupt())
341 panic("Fatal exception in interrupt");
343 if (panic_on_oops) {
344 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
345 ssleep(5);
346 panic("Fatal exception");
347 }
348 do_exit(SIGSEGV);
349 }
351 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
352 {
353 if (!(regs->eflags & VM_MASK) && !(2 & regs->xcs))
354 die(str, regs, err);
355 }
357 static void do_trap(int trapnr, int signr, char *str, int vm86,
358 struct pt_regs * regs, long error_code, siginfo_t *info)
359 {
360 if (regs->eflags & VM_MASK) {
361 if (vm86)
362 goto vm86_trap;
363 goto trap_signal;
364 }
366 if (!(regs->xcs & 2))
367 goto kernel_trap;
369 trap_signal: {
370 struct task_struct *tsk = current;
371 tsk->thread.error_code = error_code;
372 tsk->thread.trap_no = trapnr;
373 if (info)
374 force_sig_info(signr, info, tsk);
375 else
376 force_sig(signr, tsk);
377 return;
378 }
380 kernel_trap: {
381 if (!fixup_exception(regs))
382 die(str, regs, error_code);
383 return;
384 }
386 vm86_trap: {
387 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
388 if (ret) goto trap_signal;
389 return;
390 }
391 }
393 #define DO_ERROR(trapnr, signr, str, name) \
394 fastcall void do_##name(struct pt_regs * regs, long error_code) \
395 { \
396 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
397 == NOTIFY_STOP) \
398 return; \
399 do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
400 }
402 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
403 fastcall void do_##name(struct pt_regs * regs, long error_code) \
404 { \
405 siginfo_t info; \
406 info.si_signo = signr; \
407 info.si_errno = 0; \
408 info.si_code = sicode; \
409 info.si_addr = (void __user *)siaddr; \
410 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
411 == NOTIFY_STOP) \
412 return; \
413 do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
414 }
416 #define DO_VM86_ERROR(trapnr, signr, str, name) \
417 fastcall void do_##name(struct pt_regs * regs, long error_code) \
418 { \
419 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
420 == NOTIFY_STOP) \
421 return; \
422 do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
423 }
425 #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
426 fastcall void do_##name(struct pt_regs * regs, long error_code) \
427 { \
428 siginfo_t info; \
429 info.si_signo = signr; \
430 info.si_errno = 0; \
431 info.si_code = sicode; \
432 info.si_addr = (void __user *)siaddr; \
433 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
434 == NOTIFY_STOP) \
435 return; \
436 do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
437 }
439 DO_VM86_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->eip)
440 #ifndef CONFIG_KPROBES
441 DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
442 #endif
443 DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
444 DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
445 DO_ERROR_INFO( 6, SIGILL, "invalid operand", invalid_op, ILL_ILLOPN, regs->eip)
446 DO_VM86_ERROR( 7, SIGSEGV, "device not available", device_not_available)
447 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
448 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
449 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
450 DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
451 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
452 DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0)
453 #ifdef CONFIG_X86_MCE
454 DO_ERROR(18, SIGBUS, "machine check", machine_check)
455 #endif
457 fastcall void do_general_protection(struct pt_regs * regs, long error_code)
458 {
459 /*
460 * If we trapped on an LDT access then ensure that the default_ldt is
461 * loaded, if nothing else. We load default_ldt lazily because LDT
462 * switching costs time and many applications don't need it.
463 */
464 if (unlikely((error_code & 6) == 4)) {
465 unsigned long ldt;
466 __asm__ __volatile__ ("sldt %0" : "=r" (ldt));
467 if (ldt == 0) {
468 xen_set_ldt((unsigned long)&default_ldt[0], 5);
469 return;
470 }
471 }
473 if (regs->eflags & VM_MASK)
474 goto gp_in_vm86;
476 if (!(regs->xcs & 2))
477 goto gp_in_kernel;
479 current->thread.error_code = error_code;
480 current->thread.trap_no = 13;
481 force_sig(SIGSEGV, current);
482 return;
484 gp_in_vm86:
485 local_irq_enable();
486 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
487 return;
489 gp_in_kernel:
490 if (!fixup_exception(regs)) {
491 if (notify_die(DIE_GPF, "general protection fault", regs,
492 error_code, 13, SIGSEGV) == NOTIFY_STOP)
493 return;
494 die("general protection fault", regs, error_code);
495 }
496 }
498 static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
499 {
500 printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
501 printk("You probably have a hardware problem with your RAM chips\n");
503 /* Clear and disable the memory parity error line. */
504 clear_mem_error(reason);
505 }
507 static void io_check_error(unsigned char reason, struct pt_regs * regs)
508 {
509 unsigned long i;
511 printk("NMI: IOCK error (debug interrupt?)\n");
512 show_registers(regs);
514 /* Re-enable the IOCK line, wait for a few seconds */
515 reason = (reason & 0xf) | 8;
516 outb(reason, 0x61);
517 i = 2000;
518 while (--i) udelay(1000);
519 reason &= ~8;
520 outb(reason, 0x61);
521 }
523 static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
524 {
525 #ifdef CONFIG_MCA
526 /* Might actually be able to figure out what the guilty party
527 * is. */
528 if( MCA_bus ) {
529 mca_handle_nmi();
530 return;
531 }
532 #endif
533 printk("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
534 reason, smp_processor_id());
535 printk("Dazed and confused, but trying to continue\n");
536 printk("Do you have a strange power saving mode enabled?\n");
537 }
539 static DEFINE_SPINLOCK(nmi_print_lock);
541 void die_nmi (struct pt_regs *regs, const char *msg)
542 {
543 spin_lock(&nmi_print_lock);
544 /*
545 * We are in trouble anyway, lets at least try
546 * to get a message out.
547 */
548 bust_spinlocks(1);
549 printk(msg);
550 printk(" on CPU%d, eip %08lx, registers:\n",
551 smp_processor_id(), regs->eip);
552 show_registers(regs);
553 printk("console shuts up ...\n");
554 console_silent();
555 spin_unlock(&nmi_print_lock);
556 bust_spinlocks(0);
557 do_exit(SIGSEGV);
558 }
560 static void default_do_nmi(struct pt_regs * regs)
561 {
562 unsigned char reason = 0;
564 /* Only the BSP gets external NMIs from the system. */
565 if (!smp_processor_id())
566 reason = get_nmi_reason();
568 if (!(reason & 0xc0)) {
569 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 0, SIGINT)
570 == NOTIFY_STOP)
571 return;
572 #ifdef CONFIG_X86_LOCAL_APIC
573 /*
574 * Ok, so this is none of the documented NMI sources,
575 * so it must be the NMI watchdog.
576 */
577 if (nmi_watchdog) {
578 nmi_watchdog_tick(regs);
579 return;
580 }
581 #endif
582 unknown_nmi_error(reason, regs);
583 return;
584 }
585 if (notify_die(DIE_NMI, "nmi", regs, reason, 0, SIGINT) == NOTIFY_STOP)
586 return;
587 if (reason & 0x80)
588 mem_parity_error(reason, regs);
589 if (reason & 0x40)
590 io_check_error(reason, regs);
591 /*
592 * Reassert NMI in case it became active meanwhile
593 * as it's edge-triggered.
594 */
595 reassert_nmi();
596 }
598 static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
599 {
600 return 0;
601 }
603 static nmi_callback_t nmi_callback = dummy_nmi_callback;
605 fastcall void do_nmi(struct pt_regs * regs, long error_code)
606 {
607 int cpu;
609 nmi_enter();
611 cpu = smp_processor_id();
613 #ifdef CONFIG_HOTPLUG_CPU
614 if (!cpu_online(cpu)) {
615 nmi_exit();
616 return;
617 }
618 #endif
620 ++nmi_count(cpu);
622 if (!nmi_callback(regs, cpu))
623 default_do_nmi(regs);
625 nmi_exit();
626 }
628 void set_nmi_callback(nmi_callback_t callback)
629 {
630 nmi_callback = callback;
631 }
633 void unset_nmi_callback(void)
634 {
635 nmi_callback = dummy_nmi_callback;
636 }
638 #ifdef CONFIG_KPROBES
639 fastcall void do_int3(struct pt_regs *regs, long error_code)
640 {
641 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
642 == NOTIFY_STOP)
643 return;
644 /* This is an interrupt gate, because kprobes wants interrupts
645 disabled. Normal trap handlers don't. */
646 restore_interrupts(regs);
647 do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
648 }
649 #endif
651 /*
652 * Our handling of the processor debug registers is non-trivial.
653 * We do not clear them on entry and exit from the kernel. Therefore
654 * it is possible to get a watchpoint trap here from inside the kernel.
655 * However, the code in ./ptrace.c has ensured that the user can
656 * only set watchpoints on userspace addresses. Therefore the in-kernel
657 * watchpoint trap can only occur in code which is reading/writing
658 * from user space. Such code must not hold kernel locks (since it
659 * can equally take a page fault), therefore it is safe to call
660 * force_sig_info even though that claims and releases locks.
661 *
662 * Code in ./signal.c ensures that the debug control register
663 * is restored before we deliver any signal, and therefore that
664 * user code runs with the correct debug control register even though
665 * we clear it here.
666 *
667 * Being careful here means that we don't have to be as careful in a
668 * lot of more complicated places (task switching can be a bit lazy
669 * about restoring all the debug state, and ptrace doesn't have to
670 * find every occurrence of the TF bit that could be saved away even
671 * by user code)
672 */
673 fastcall void do_debug(struct pt_regs * regs, long error_code)
674 {
675 unsigned int condition;
676 struct task_struct *tsk = current;
678 condition = HYPERVISOR_get_debugreg(6);
680 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
681 SIGTRAP) == NOTIFY_STOP)
682 return;
683 #if 0
684 /* It's safe to allow irq's after DR6 has been saved */
685 if (regs->eflags & X86_EFLAGS_IF)
686 local_irq_enable();
687 #endif
689 /* Mask out spurious debug traps due to lazy DR7 setting */
690 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
691 if (!tsk->thread.debugreg[7])
692 goto clear_dr7;
693 }
695 if (regs->eflags & VM_MASK)
696 goto debug_vm86;
698 /* Save debug status register where ptrace can see it */
699 tsk->thread.debugreg[6] = condition;
701 /*
702 * Single-stepping through TF: make sure we ignore any events in
703 * kernel space (but re-enable TF when returning to user mode).
704 */
705 if (condition & DR_STEP) {
706 /*
707 * We already checked v86 mode above, so we can
708 * check for kernel mode by just checking the CPL
709 * of CS.
710 */
711 if ((regs->xcs & 2) == 0)
712 goto clear_TF_reenable;
713 }
715 /* Ok, finally something we can handle */
716 send_sigtrap(tsk, regs, error_code);
718 /* Disable additional traps. They'll be re-enabled when
719 * the signal is delivered.
720 */
721 clear_dr7:
722 HYPERVISOR_set_debugreg(7, 0);
723 return;
725 debug_vm86:
726 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
727 return;
729 clear_TF_reenable:
730 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
731 regs->eflags &= ~TF_MASK;
732 return;
733 }
735 /*
736 * Note that we play around with the 'TS' bit in an attempt to get
737 * the correct behaviour even in the presence of the asynchronous
738 * IRQ13 behaviour
739 */
740 void math_error(void __user *eip)
741 {
742 struct task_struct * task;
743 siginfo_t info;
744 unsigned short cwd, swd;
746 /*
747 * Save the info for the exception handler and clear the error.
748 */
749 task = current;
750 save_init_fpu(task);
751 task->thread.trap_no = 16;
752 task->thread.error_code = 0;
753 info.si_signo = SIGFPE;
754 info.si_errno = 0;
755 info.si_code = __SI_FAULT;
756 info.si_addr = eip;
757 /*
758 * (~cwd & swd) will mask out exceptions that are not set to unmasked
759 * status. 0x3f is the exception bits in these regs, 0x200 is the
760 * C1 reg you need in case of a stack fault, 0x040 is the stack
761 * fault bit. We should only be taking one exception at a time,
762 * so if this combination doesn't produce any single exception,
763 * then we have a bad program that isn't syncronizing its FPU usage
764 * and it will suffer the consequences since we won't be able to
765 * fully reproduce the context of the exception
766 */
767 cwd = get_fpu_cwd(task);
768 swd = get_fpu_swd(task);
769 switch (((~cwd) & swd & 0x3f) | (swd & 0x240)) {
770 case 0x000:
771 default:
772 break;
773 case 0x001: /* Invalid Op */
774 case 0x041: /* Stack Fault */
775 case 0x241: /* Stack Fault | Direction */
776 info.si_code = FPE_FLTINV;
777 /* Should we clear the SF or let user space do it ???? */
778 break;
779 case 0x002: /* Denormalize */
780 case 0x010: /* Underflow */
781 info.si_code = FPE_FLTUND;
782 break;
783 case 0x004: /* Zero Divide */
784 info.si_code = FPE_FLTDIV;
785 break;
786 case 0x008: /* Overflow */
787 info.si_code = FPE_FLTOVF;
788 break;
789 case 0x020: /* Precision */
790 info.si_code = FPE_FLTRES;
791 break;
792 }
793 force_sig_info(SIGFPE, &info, task);
794 }
796 fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
797 {
798 ignore_fpu_irq = 1;
799 math_error((void __user *)regs->eip);
800 }
802 static void simd_math_error(void __user *eip)
803 {
804 struct task_struct * task;
805 siginfo_t info;
806 unsigned short mxcsr;
808 /*
809 * Save the info for the exception handler and clear the error.
810 */
811 task = current;
812 save_init_fpu(task);
813 task->thread.trap_no = 19;
814 task->thread.error_code = 0;
815 info.si_signo = SIGFPE;
816 info.si_errno = 0;
817 info.si_code = __SI_FAULT;
818 info.si_addr = eip;
819 /*
820 * The SIMD FPU exceptions are handled a little differently, as there
821 * is only a single status/control register. Thus, to determine which
822 * unmasked exception was caught we must mask the exception mask bits
823 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
824 */
825 mxcsr = get_fpu_mxcsr(task);
826 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
827 case 0x000:
828 default:
829 break;
830 case 0x001: /* Invalid Op */
831 info.si_code = FPE_FLTINV;
832 break;
833 case 0x002: /* Denormalize */
834 case 0x010: /* Underflow */
835 info.si_code = FPE_FLTUND;
836 break;
837 case 0x004: /* Zero Divide */
838 info.si_code = FPE_FLTDIV;
839 break;
840 case 0x008: /* Overflow */
841 info.si_code = FPE_FLTOVF;
842 break;
843 case 0x020: /* Precision */
844 info.si_code = FPE_FLTRES;
845 break;
846 }
847 force_sig_info(SIGFPE, &info, task);
848 }
850 fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
851 long error_code)
852 {
853 if (cpu_has_xmm) {
854 /* Handle SIMD FPU exceptions on PIII+ processors. */
855 ignore_fpu_irq = 1;
856 simd_math_error((void __user *)regs->eip);
857 } else {
858 /*
859 * Handle strange cache flush from user space exception
860 * in all other cases. This is undocumented behaviour.
861 */
862 if (regs->eflags & VM_MASK) {
863 handle_vm86_fault((struct kernel_vm86_regs *)regs,
864 error_code);
865 return;
866 }
867 die_if_kernel("cache flush denied", regs, error_code);
868 current->thread.trap_no = 19;
869 current->thread.error_code = error_code;
870 force_sig(SIGSEGV, current);
871 }
872 }
874 #ifndef CONFIG_XEN
875 fastcall void setup_x86_bogus_stack(unsigned char * stk)
876 {
877 unsigned long *switch16_ptr, *switch32_ptr;
878 struct pt_regs *regs;
879 unsigned long stack_top, stack_bot;
880 unsigned short iret_frame16_off;
881 int cpu = smp_processor_id();
882 /* reserve the space on 32bit stack for the magic switch16 pointer */
883 memmove(stk, stk + 8, sizeof(struct pt_regs));
884 switch16_ptr = (unsigned long *)(stk + sizeof(struct pt_regs));
885 regs = (struct pt_regs *)stk;
886 /* now the switch32 on 16bit stack */
887 stack_bot = (unsigned long)&per_cpu(cpu_16bit_stack, cpu);
888 stack_top = stack_bot + CPU_16BIT_STACK_SIZE;
889 switch32_ptr = (unsigned long *)(stack_top - 8);
890 iret_frame16_off = CPU_16BIT_STACK_SIZE - 8 - 20;
891 /* copy iret frame on 16bit stack */
892 memcpy((void *)(stack_bot + iret_frame16_off), &regs->eip, 20);
893 /* fill in the switch pointers */
894 switch16_ptr[0] = (regs->esp & 0xffff0000) | iret_frame16_off;
895 switch16_ptr[1] = __ESPFIX_SS;
896 switch32_ptr[0] = (unsigned long)stk + sizeof(struct pt_regs) +
897 8 - CPU_16BIT_STACK_SIZE;
898 switch32_ptr[1] = __KERNEL_DS;
899 }
901 fastcall unsigned char * fixup_x86_bogus_stack(unsigned short sp)
902 {
903 unsigned long *switch32_ptr;
904 unsigned char *stack16, *stack32;
905 unsigned long stack_top, stack_bot;
906 int len;
907 int cpu = smp_processor_id();
908 stack_bot = (unsigned long)&per_cpu(cpu_16bit_stack, cpu);
909 stack_top = stack_bot + CPU_16BIT_STACK_SIZE;
910 switch32_ptr = (unsigned long *)(stack_top - 8);
911 /* copy the data from 16bit stack to 32bit stack */
912 len = CPU_16BIT_STACK_SIZE - 8 - sp;
913 stack16 = (unsigned char *)(stack_bot + sp);
914 stack32 = (unsigned char *)
915 (switch32_ptr[0] + CPU_16BIT_STACK_SIZE - 8 - len);
916 memcpy(stack32, stack16, len);
917 return stack32;
918 }
919 #endif
921 /*
922 * 'math_state_restore()' saves the current math information in the
923 * old math state array, and gets the new ones from the current task
924 *
925 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
926 * Don't touch unless you *really* know how it works.
927 *
928 * Must be called with kernel preemption disabled (in this case,
929 * local interrupts are disabled at the call-site in entry.S).
930 */
931 asmlinkage void math_state_restore(struct pt_regs regs)
932 {
933 struct thread_info *thread = current_thread_info();
934 struct task_struct *tsk = thread->task;
936 /* NB. 'clts' is done for us by Xen during virtual trap. */
937 if (!tsk_used_math(tsk))
938 init_fpu(tsk);
939 restore_fpu(tsk);
940 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
941 }
943 #ifndef CONFIG_MATH_EMULATION
945 asmlinkage void math_emulate(long arg)
946 {
947 printk("math-emulation not enabled and no coprocessor found.\n");
948 printk("killing %s.\n",current->comm);
949 force_sig(SIGFPE,current);
950 schedule();
951 }
953 #endif /* CONFIG_MATH_EMULATION */
955 #ifdef CONFIG_X86_F00F_BUG
956 void __init trap_init_f00f_bug(void)
957 {
958 __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
960 /*
961 * Update the IDT descriptor and reload the IDT so that
962 * it uses the read-only mapped virtual address.
963 */
964 idt_descr.address = fix_to_virt(FIX_F00F_IDT);
965 __asm__ __volatile__("lidt %0" : : "m" (idt_descr));
966 }
967 #endif
970 /* NB. All these are "trap gates" (i.e. events_mask isn't cleared). */
971 static trap_info_t trap_table[] = {
972 { 0, 0, __KERNEL_CS, (unsigned long)divide_error },
973 { 1, 0, __KERNEL_CS, (unsigned long)debug },
974 { 3, 3, __KERNEL_CS, (unsigned long)int3 },
975 { 4, 3, __KERNEL_CS, (unsigned long)overflow },
976 { 5, 3, __KERNEL_CS, (unsigned long)bounds },
977 { 6, 0, __KERNEL_CS, (unsigned long)invalid_op },
978 { 7, 0, __KERNEL_CS, (unsigned long)device_not_available },
979 { 9, 0, __KERNEL_CS, (unsigned long)coprocessor_segment_overrun },
980 { 10, 0, __KERNEL_CS, (unsigned long)invalid_TSS },
981 { 11, 0, __KERNEL_CS, (unsigned long)segment_not_present },
982 { 12, 0, __KERNEL_CS, (unsigned long)stack_segment },
983 { 13, 0, __KERNEL_CS, (unsigned long)general_protection },
984 { 14, 0, __KERNEL_CS, (unsigned long)page_fault },
985 { 15, 0, __KERNEL_CS, (unsigned long)fixup_4gb_segment },
986 { 16, 0, __KERNEL_CS, (unsigned long)coprocessor_error },
987 { 17, 0, __KERNEL_CS, (unsigned long)alignment_check },
988 #ifdef CONFIG_X86_MCE
989 { 18, 0, __KERNEL_CS, (unsigned long)machine_check },
990 #endif
991 { 19, 0, __KERNEL_CS, (unsigned long)simd_coprocessor_error },
992 { SYSCALL_VECTOR, 3, __KERNEL_CS, (unsigned long)system_call },
993 { 0, 0, 0, 0 }
994 };
996 void __init trap_init(void)
997 {
998 HYPERVISOR_set_trap_table(trap_table);
1000 /*
1001 * default LDT is a single-entry callgate to lcall7 for iBCS
1002 * and a callgate to lcall27 for Solaris/x86 binaries
1003 */
1004 make_lowmem_page_readonly(&default_ldt[0]);
1006 /*
1007 * Should be a barrier for any external CPU state.
1008 */
1009 cpu_init();
1012 void smp_trap_init(trap_info_t *trap_ctxt)
1014 trap_info_t *t = trap_table;
1016 for (t = trap_table; t->address; t++) {
1017 trap_ctxt[t->vector].flags = t->flags;
1018 trap_ctxt[t->vector].cs = t->cs;
1019 trap_ctxt[t->vector].address = t->address;
1023 static int __init kstack_setup(char *s)
1025 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
1026 return 0;
1028 __setup("kstack=", kstack_setup);