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