]> git.karo-electronics.de Git - mv-sheeva.git/blob - arch/x86/kernel/traps_64.c
Merge branches 'x86/alternatives', 'x86/cleanups', 'x86/commandline', 'x86/crashdump...
[mv-sheeva.git] / arch / x86 / kernel / traps_64.c
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4  *
5  *  Pentium III FXSR, SSE support
6  *      Gareth Hughes <gareth@valinux.com>, May 2000
7  */
8
9 /*
10  * 'Traps.c' handles hardware traps and faults after we have saved some
11  * state in 'entry.S'.
12  */
13 #include <linux/moduleparam.h>
14 #include <linux/interrupt.h>
15 #include <linux/kallsyms.h>
16 #include <linux/spinlock.h>
17 #include <linux/kprobes.h>
18 #include <linux/uaccess.h>
19 #include <linux/utsname.h>
20 #include <linux/kdebug.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/ptrace.h>
24 #include <linux/string.h>
25 #include <linux/unwind.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/kexec.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/init.h>
32 #include <linux/bug.h>
33 #include <linux/nmi.h>
34 #include <linux/mm.h>
35 #include <linux/smp.h>
36 #include <linux/io.h>
37
38 #if defined(CONFIG_EDAC)
39 #include <linux/edac.h>
40 #endif
41
42 #include <asm/stacktrace.h>
43 #include <asm/processor.h>
44 #include <asm/debugreg.h>
45 #include <asm/atomic.h>
46 #include <asm/system.h>
47 #include <asm/unwind.h>
48 #include <asm/desc.h>
49 #include <asm/i387.h>
50 #include <asm/pgalloc.h>
51 #include <asm/proto.h>
52 #include <asm/pda.h>
53 #include <asm/traps.h>
54
55 #include <mach_traps.h>
56
57 int panic_on_unrecovered_nmi;
58 int kstack_depth_to_print = 12;
59 static unsigned int code_bytes = 64;
60 static int ignore_nmis;
61 static int die_counter;
62
63 static inline void conditional_sti(struct pt_regs *regs)
64 {
65         if (regs->flags & X86_EFLAGS_IF)
66                 local_irq_enable();
67 }
68
69 static inline void preempt_conditional_sti(struct pt_regs *regs)
70 {
71         inc_preempt_count();
72         if (regs->flags & X86_EFLAGS_IF)
73                 local_irq_enable();
74 }
75
76 static inline void preempt_conditional_cli(struct pt_regs *regs)
77 {
78         if (regs->flags & X86_EFLAGS_IF)
79                 local_irq_disable();
80         /* Make sure to not schedule here because we could be running
81            on an exception stack. */
82         dec_preempt_count();
83 }
84
85 void printk_address(unsigned long address, int reliable)
86 {
87         printk(" [<%016lx>] %s%pS\n",
88                         address, reliable ?     "" : "? ", (void *) address);
89 }
90
91 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
92                                         unsigned *usedp, char **idp)
93 {
94         static char ids[][8] = {
95                 [DEBUG_STACK - 1] = "#DB",
96                 [NMI_STACK - 1] = "NMI",
97                 [DOUBLEFAULT_STACK - 1] = "#DF",
98                 [STACKFAULT_STACK - 1] = "#SS",
99                 [MCE_STACK - 1] = "#MC",
100 #if DEBUG_STKSZ > EXCEPTION_STKSZ
101                 [N_EXCEPTION_STACKS ...
102                         N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
103 #endif
104         };
105         unsigned k;
106
107         /*
108          * Iterate over all exception stacks, and figure out whether
109          * 'stack' is in one of them:
110          */
111         for (k = 0; k < N_EXCEPTION_STACKS; k++) {
112                 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
113                 /*
114                  * Is 'stack' above this exception frame's end?
115                  * If yes then skip to the next frame.
116                  */
117                 if (stack >= end)
118                         continue;
119                 /*
120                  * Is 'stack' above this exception frame's start address?
121                  * If yes then we found the right frame.
122                  */
123                 if (stack >= end - EXCEPTION_STKSZ) {
124                         /*
125                          * Make sure we only iterate through an exception
126                          * stack once. If it comes up for the second time
127                          * then there's something wrong going on - just
128                          * break out and return NULL:
129                          */
130                         if (*usedp & (1U << k))
131                                 break;
132                         *usedp |= 1U << k;
133                         *idp = ids[k];
134                         return (unsigned long *)end;
135                 }
136                 /*
137                  * If this is a debug stack, and if it has a larger size than
138                  * the usual exception stacks, then 'stack' might still
139                  * be within the lower portion of the debug stack:
140                  */
141 #if DEBUG_STKSZ > EXCEPTION_STKSZ
142                 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
143                         unsigned j = N_EXCEPTION_STACKS - 1;
144
145                         /*
146                          * Black magic. A large debug stack is composed of
147                          * multiple exception stack entries, which we
148                          * iterate through now. Dont look:
149                          */
150                         do {
151                                 ++j;
152                                 end -= EXCEPTION_STKSZ;
153                                 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
154                         } while (stack < end - EXCEPTION_STKSZ);
155                         if (*usedp & (1U << j))
156                                 break;
157                         *usedp |= 1U << j;
158                         *idp = ids[j];
159                         return (unsigned long *)end;
160                 }
161 #endif
162         }
163         return NULL;
164 }
165
166 /*
167  * x86-64 can have up to three kernel stacks:
168  * process stack
169  * interrupt stack
170  * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
171  */
172
173 static inline int valid_stack_ptr(struct thread_info *tinfo,
174                         void *p, unsigned int size, void *end)
175 {
176         void *t = tinfo;
177         if (end) {
178                 if (p < end && p >= (end-THREAD_SIZE))
179                         return 1;
180                 else
181                         return 0;
182         }
183         return p > t && p < t + THREAD_SIZE - size;
184 }
185
186 /* The form of the top of the frame on the stack */
187 struct stack_frame {
188         struct stack_frame *next_frame;
189         unsigned long return_address;
190 };
191
192 static inline unsigned long
193 print_context_stack(struct thread_info *tinfo,
194                 unsigned long *stack, unsigned long bp,
195                 const struct stacktrace_ops *ops, void *data,
196                 unsigned long *end)
197 {
198         struct stack_frame *frame = (struct stack_frame *)bp;
199
200         while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
201                 unsigned long addr;
202
203                 addr = *stack;
204                 if (__kernel_text_address(addr)) {
205                         if ((unsigned long) stack == bp + 8) {
206                                 ops->address(data, addr, 1);
207                                 frame = frame->next_frame;
208                                 bp = (unsigned long) frame;
209                         } else {
210                                 ops->address(data, addr, bp == 0);
211                         }
212                 }
213                 stack++;
214         }
215         return bp;
216 }
217
218 void dump_trace(struct task_struct *task, struct pt_regs *regs,
219                 unsigned long *stack, unsigned long bp,
220                 const struct stacktrace_ops *ops, void *data)
221 {
222         const unsigned cpu = get_cpu();
223         unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
224         unsigned used = 0;
225         struct thread_info *tinfo;
226
227         if (!task)
228                 task = current;
229
230         if (!stack) {
231                 unsigned long dummy;
232                 stack = &dummy;
233                 if (task && task != current)
234                         stack = (unsigned long *)task->thread.sp;
235         }
236
237 #ifdef CONFIG_FRAME_POINTER
238         if (!bp) {
239                 if (task == current) {
240                         /* Grab bp right from our regs */
241                         asm("movq %%rbp, %0" : "=r" (bp) : );
242                 } else {
243                         /* bp is the last reg pushed by switch_to */
244                         bp = *(unsigned long *) task->thread.sp;
245                 }
246         }
247 #endif
248
249         /*
250          * Print function call entries in all stacks, starting at the
251          * current stack address. If the stacks consist of nested
252          * exceptions
253          */
254         tinfo = task_thread_info(task);
255         for (;;) {
256                 char *id;
257                 unsigned long *estack_end;
258                 estack_end = in_exception_stack(cpu, (unsigned long)stack,
259                                                 &used, &id);
260
261                 if (estack_end) {
262                         if (ops->stack(data, id) < 0)
263                                 break;
264
265                         bp = print_context_stack(tinfo, stack, bp, ops,
266                                                         data, estack_end);
267                         ops->stack(data, "<EOE>");
268                         /*
269                          * We link to the next stack via the
270                          * second-to-last pointer (index -2 to end) in the
271                          * exception stack:
272                          */
273                         stack = (unsigned long *) estack_end[-2];
274                         continue;
275                 }
276                 if (irqstack_end) {
277                         unsigned long *irqstack;
278                         irqstack = irqstack_end -
279                                 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
280
281                         if (stack >= irqstack && stack < irqstack_end) {
282                                 if (ops->stack(data, "IRQ") < 0)
283                                         break;
284                                 bp = print_context_stack(tinfo, stack, bp,
285                                                 ops, data, irqstack_end);
286                                 /*
287                                  * We link to the next stack (which would be
288                                  * the process stack normally) the last
289                                  * pointer (index -1 to end) in the IRQ stack:
290                                  */
291                                 stack = (unsigned long *) (irqstack_end[-1]);
292                                 irqstack_end = NULL;
293                                 ops->stack(data, "EOI");
294                                 continue;
295                         }
296                 }
297                 break;
298         }
299
300         /*
301          * This handles the process stack:
302          */
303         bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
304         put_cpu();
305 }
306 EXPORT_SYMBOL(dump_trace);
307
308 static void
309 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
310 {
311         print_symbol(msg, symbol);
312         printk("\n");
313 }
314
315 static void print_trace_warning(void *data, char *msg)
316 {
317         printk("%s\n", msg);
318 }
319
320 static int print_trace_stack(void *data, char *name)
321 {
322         printk(" <%s> ", name);
323         return 0;
324 }
325
326 static void print_trace_address(void *data, unsigned long addr, int reliable)
327 {
328         touch_nmi_watchdog();
329         printk_address(addr, reliable);
330 }
331
332 static const struct stacktrace_ops print_trace_ops = {
333         .warning = print_trace_warning,
334         .warning_symbol = print_trace_warning_symbol,
335         .stack = print_trace_stack,
336         .address = print_trace_address,
337 };
338
339 static void
340 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
341                 unsigned long *stack, unsigned long bp, char *log_lvl)
342 {
343         printk("Call Trace:\n");
344         dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
345 }
346
347 void show_trace(struct task_struct *task, struct pt_regs *regs,
348                 unsigned long *stack, unsigned long bp)
349 {
350         show_trace_log_lvl(task, regs, stack, bp, "");
351 }
352
353 static void
354 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
355                 unsigned long *sp, unsigned long bp, char *log_lvl)
356 {
357         unsigned long *stack;
358         int i;
359         const int cpu = smp_processor_id();
360         unsigned long *irqstack_end =
361                 (unsigned long *) (cpu_pda(cpu)->irqstackptr);
362         unsigned long *irqstack =
363                 (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
364
365         /*
366          * debugging aid: "show_stack(NULL, NULL);" prints the
367          * back trace for this cpu.
368          */
369
370         if (sp == NULL) {
371                 if (task)
372                         sp = (unsigned long *)task->thread.sp;
373                 else
374                         sp = (unsigned long *)&sp;
375         }
376
377         stack = sp;
378         for (i = 0; i < kstack_depth_to_print; i++) {
379                 if (stack >= irqstack && stack <= irqstack_end) {
380                         if (stack == irqstack_end) {
381                                 stack = (unsigned long *) (irqstack_end[-1]);
382                                 printk(" <EOI> ");
383                         }
384                 } else {
385                 if (((long) stack & (THREAD_SIZE-1)) == 0)
386                         break;
387                 }
388                 if (i && ((i % 4) == 0))
389                         printk("\n");
390                 printk(" %016lx", *stack++);
391                 touch_nmi_watchdog();
392         }
393         printk("\n");
394         show_trace_log_lvl(task, regs, sp, bp, log_lvl);
395 }
396
397 void show_stack(struct task_struct *task, unsigned long *sp)
398 {
399         show_stack_log_lvl(task, NULL, sp, 0, "");
400 }
401
402 /*
403  * The architecture-independent dump_stack generator
404  */
405 void dump_stack(void)
406 {
407         unsigned long bp = 0;
408         unsigned long stack;
409
410 #ifdef CONFIG_FRAME_POINTER
411         if (!bp)
412                 asm("movq %%rbp, %0" : "=r" (bp) : );
413 #endif
414
415         printk("Pid: %d, comm: %.20s %s %s %.*s\n",
416                 current->pid, current->comm, print_tainted(),
417                 init_utsname()->release,
418                 (int)strcspn(init_utsname()->version, " "),
419                 init_utsname()->version);
420         show_trace(NULL, NULL, &stack, bp);
421 }
422 EXPORT_SYMBOL(dump_stack);
423
424 void show_registers(struct pt_regs *regs)
425 {
426         int i;
427         unsigned long sp;
428         const int cpu = smp_processor_id();
429         struct task_struct *cur = cpu_pda(cpu)->pcurrent;
430
431         sp = regs->sp;
432         printk("CPU %d ", cpu);
433         __show_regs(regs);
434         printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
435                 cur->comm, cur->pid, task_thread_info(cur), cur);
436
437         /*
438          * When in-kernel, we also print out the stack and code at the
439          * time of the fault..
440          */
441         if (!user_mode(regs)) {
442                 unsigned int code_prologue = code_bytes * 43 / 64;
443                 unsigned int code_len = code_bytes;
444                 unsigned char c;
445                 u8 *ip;
446
447                 printk("Stack: ");
448                 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
449                                 regs->bp, "");
450
451                 printk(KERN_EMERG "Code: ");
452
453                 ip = (u8 *)regs->ip - code_prologue;
454                 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
455                         /* try starting at RIP */
456                         ip = (u8 *)regs->ip;
457                         code_len = code_len - code_prologue + 1;
458                 }
459                 for (i = 0; i < code_len; i++, ip++) {
460                         if (ip < (u8 *)PAGE_OFFSET ||
461                                         probe_kernel_address(ip, c)) {
462                                 printk(" Bad RIP value.");
463                                 break;
464                         }
465                         if (ip == (u8 *)regs->ip)
466                                 printk("<%02x> ", c);
467                         else
468                                 printk("%02x ", c);
469                 }
470         }
471         printk("\n");
472 }
473
474 int is_valid_bugaddr(unsigned long ip)
475 {
476         unsigned short ud2;
477
478         if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
479                 return 0;
480
481         return ud2 == 0x0b0f;
482 }
483
484 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
485 static int die_owner = -1;
486 static unsigned int die_nest_count;
487
488 unsigned __kprobes long oops_begin(void)
489 {
490         int cpu;
491         unsigned long flags;
492
493         oops_enter();
494
495         /* racy, but better than risking deadlock. */
496         raw_local_irq_save(flags);
497         cpu = smp_processor_id();
498         if (!__raw_spin_trylock(&die_lock)) {
499                 if (cpu == die_owner)
500                         /* nested oops. should stop eventually */;
501                 else
502                         __raw_spin_lock(&die_lock);
503         }
504         die_nest_count++;
505         die_owner = cpu;
506         console_verbose();
507         bust_spinlocks(1);
508         return flags;
509 }
510
511 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
512 {
513         die_owner = -1;
514         bust_spinlocks(0);
515         die_nest_count--;
516         if (!die_nest_count)
517                 /* Nest count reaches zero, release the lock. */
518                 __raw_spin_unlock(&die_lock);
519         raw_local_irq_restore(flags);
520         if (!regs) {
521                 oops_exit();
522                 return;
523         }
524         if (panic_on_oops)
525                 panic("Fatal exception");
526         oops_exit();
527         do_exit(signr);
528 }
529
530 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
531 {
532         printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff, ++die_counter);
533 #ifdef CONFIG_PREEMPT
534         printk("PREEMPT ");
535 #endif
536 #ifdef CONFIG_SMP
537         printk("SMP ");
538 #endif
539 #ifdef CONFIG_DEBUG_PAGEALLOC
540         printk("DEBUG_PAGEALLOC");
541 #endif
542         printk("\n");
543         if (notify_die(DIE_OOPS, str, regs, err,
544                         current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
545                 return 1;
546
547         show_registers(regs);
548         add_taint(TAINT_DIE);
549         /* Executive summary in case the oops scrolled away */
550         printk(KERN_ALERT "RIP ");
551         printk_address(regs->ip, 1);
552         printk(" RSP <%016lx>\n", regs->sp);
553         if (kexec_should_crash(current))
554                 crash_kexec(regs);
555         return 0;
556 }
557
558 void die(const char *str, struct pt_regs *regs, long err)
559 {
560         unsigned long flags = oops_begin();
561
562         if (!user_mode(regs))
563                 report_bug(regs->ip, regs);
564
565         if (__die(str, regs, err))
566                 regs = NULL;
567         oops_end(flags, regs, SIGSEGV);
568 }
569
570 notrace __kprobes void
571 die_nmi(char *str, struct pt_regs *regs, int do_panic)
572 {
573         unsigned long flags;
574
575         if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
576                 return;
577
578         flags = oops_begin();
579         /*
580          * We are in trouble anyway, lets at least try
581          * to get a message out.
582          */
583         printk(KERN_EMERG "%s", str);
584         printk(" on CPU%d, ip %08lx, registers:\n",
585                 smp_processor_id(), regs->ip);
586         show_registers(regs);
587         if (kexec_should_crash(current))
588                 crash_kexec(regs);
589         if (do_panic || panic_on_oops)
590                 panic("Non maskable interrupt");
591         oops_end(flags, NULL, SIGBUS);
592         nmi_exit();
593         local_irq_enable();
594         do_exit(SIGBUS);
595 }
596
597 static void __kprobes
598 do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
599         long error_code, siginfo_t *info)
600 {
601         struct task_struct *tsk = current;
602
603         if (!user_mode(regs))
604                 goto kernel_trap;
605
606         /*
607          * We want error_code and trap_no set for userspace faults and
608          * kernelspace faults which result in die(), but not
609          * kernelspace faults which are fixed up.  die() gives the
610          * process no chance to handle the signal and notice the
611          * kernel fault information, so that won't result in polluting
612          * the information about previously queued, but not yet
613          * delivered, faults.  See also do_general_protection below.
614          */
615         tsk->thread.error_code = error_code;
616         tsk->thread.trap_no = trapnr;
617
618         if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
619             printk_ratelimit()) {
620                 printk(KERN_INFO
621                        "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
622                        tsk->comm, tsk->pid, str,
623                        regs->ip, regs->sp, error_code);
624                 print_vma_addr(" in ", regs->ip);
625                 printk("\n");
626         }
627
628         if (info)
629                 force_sig_info(signr, info, tsk);
630         else
631                 force_sig(signr, tsk);
632         return;
633
634 kernel_trap:
635         if (!fixup_exception(regs)) {
636                 tsk->thread.error_code = error_code;
637                 tsk->thread.trap_no = trapnr;
638                 die(str, regs, error_code);
639         }
640         return;
641 }
642
643 #define DO_ERROR(trapnr, signr, str, name) \
644 asmlinkage void do_##name(struct pt_regs *regs, long error_code)        \
645 {                                                                       \
646         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr)  \
647                                                         == NOTIFY_STOP) \
648                 return;                                                 \
649         conditional_sti(regs);                                          \
650         do_trap(trapnr, signr, str, regs, error_code, NULL);            \
651 }
652
653 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr)         \
654 asmlinkage void do_##name(struct pt_regs *regs, long error_code)        \
655 {                                                                       \
656         siginfo_t info;                                                 \
657         info.si_signo = signr;                                          \
658         info.si_errno = 0;                                              \
659         info.si_code = sicode;                                          \
660         info.si_addr = (void __user *)siaddr;                           \
661         trace_hardirqs_fixup();                                         \
662         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr)  \
663                                                         == NOTIFY_STOP) \
664                 return;                                                 \
665         conditional_sti(regs);                                          \
666         do_trap(trapnr, signr, str, regs, error_code, &info);           \
667 }
668
669 DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
670 DO_ERROR(4, SIGSEGV, "overflow", overflow)
671 DO_ERROR(5, SIGSEGV, "bounds", bounds)
672 DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
673 DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
674 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
675 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
676 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
677
678 /* Runs on IST stack */
679 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
680 {
681         if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
682                         12, SIGBUS) == NOTIFY_STOP)
683                 return;
684         preempt_conditional_sti(regs);
685         do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
686         preempt_conditional_cli(regs);
687 }
688
689 asmlinkage void do_double_fault(struct pt_regs *regs, long error_code)
690 {
691         static const char str[] = "double fault";
692         struct task_struct *tsk = current;
693
694         /* Return not checked because double check cannot be ignored */
695         notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
696
697         tsk->thread.error_code = error_code;
698         tsk->thread.trap_no = 8;
699
700         /* This is always a kernel trap and never fixable (and thus must
701            never return). */
702         for (;;)
703                 die(str, regs, error_code);
704 }
705
706 asmlinkage void __kprobes
707 do_general_protection(struct pt_regs *regs, long error_code)
708 {
709         struct task_struct *tsk;
710
711         conditional_sti(regs);
712
713         tsk = current;
714         if (!user_mode(regs))
715                 goto gp_in_kernel;
716
717         tsk->thread.error_code = error_code;
718         tsk->thread.trap_no = 13;
719
720         if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
721                         printk_ratelimit()) {
722                 printk(KERN_INFO
723                         "%s[%d] general protection ip:%lx sp:%lx error:%lx",
724                         tsk->comm, tsk->pid,
725                         regs->ip, regs->sp, error_code);
726                 print_vma_addr(" in ", regs->ip);
727                 printk("\n");
728         }
729
730         force_sig(SIGSEGV, tsk);
731         return;
732
733 gp_in_kernel:
734         if (fixup_exception(regs))
735                 return;
736
737         tsk->thread.error_code = error_code;
738         tsk->thread.trap_no = 13;
739         if (notify_die(DIE_GPF, "general protection fault", regs,
740                                 error_code, 13, SIGSEGV) == NOTIFY_STOP)
741                 return;
742         die("general protection fault", regs, error_code);
743 }
744
745 static notrace __kprobes void
746 mem_parity_error(unsigned char reason, struct pt_regs *regs)
747 {
748         printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
749                 reason);
750         printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
751
752 #if defined(CONFIG_EDAC)
753         if (edac_handler_set()) {
754                 edac_atomic_assert_error();
755                 return;
756         }
757 #endif
758
759         if (panic_on_unrecovered_nmi)
760                 panic("NMI: Not continuing");
761
762         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
763
764         /* Clear and disable the memory parity error line. */
765         reason = (reason & 0xf) | 4;
766         outb(reason, 0x61);
767 }
768
769 static notrace __kprobes void
770 io_check_error(unsigned char reason, struct pt_regs *regs)
771 {
772         printk("NMI: IOCK error (debug interrupt?)\n");
773         show_registers(regs);
774
775         /* Re-enable the IOCK line, wait for a few seconds */
776         reason = (reason & 0xf) | 8;
777         outb(reason, 0x61);
778         mdelay(2000);
779         reason &= ~8;
780         outb(reason, 0x61);
781 }
782
783 static notrace __kprobes void
784 unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
785 {
786         if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
787                         NOTIFY_STOP)
788                 return;
789         printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
790                 reason);
791         printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
792
793         if (panic_on_unrecovered_nmi)
794                 panic("NMI: Not continuing");
795
796         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
797 }
798
799 /* Runs on IST stack. This code must keep interrupts off all the time.
800    Nested NMIs are prevented by the CPU. */
801 asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
802 {
803         unsigned char reason = 0;
804         int cpu;
805
806         cpu = smp_processor_id();
807
808         /* Only the BSP gets external NMIs from the system. */
809         if (!cpu)
810                 reason = get_nmi_reason();
811
812         if (!(reason & 0xc0)) {
813                 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
814                                                                 == NOTIFY_STOP)
815                         return;
816                 /*
817                  * Ok, so this is none of the documented NMI sources,
818                  * so it must be the NMI watchdog.
819                  */
820                 if (nmi_watchdog_tick(regs, reason))
821                         return;
822                 if (!do_nmi_callback(regs, cpu))
823                         unknown_nmi_error(reason, regs);
824
825                 return;
826         }
827         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
828                 return;
829
830         /* AK: following checks seem to be broken on modern chipsets. FIXME */
831         if (reason & 0x80)
832                 mem_parity_error(reason, regs);
833         if (reason & 0x40)
834                 io_check_error(reason, regs);
835 }
836
837 asmlinkage notrace __kprobes void
838 do_nmi(struct pt_regs *regs, long error_code)
839 {
840         nmi_enter();
841
842         add_pda(__nmi_count, 1);
843
844         if (!ignore_nmis)
845                 default_do_nmi(regs);
846
847         nmi_exit();
848 }
849
850 void stop_nmi(void)
851 {
852         acpi_nmi_disable();
853         ignore_nmis++;
854 }
855
856 void restart_nmi(void)
857 {
858         ignore_nmis--;
859         acpi_nmi_enable();
860 }
861
862 /* runs on IST stack. */
863 asmlinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
864 {
865         trace_hardirqs_fixup();
866
867         if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
868                         == NOTIFY_STOP)
869                 return;
870
871         preempt_conditional_sti(regs);
872         do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
873         preempt_conditional_cli(regs);
874 }
875
876 /* Help handler running on IST stack to switch back to user stack
877    for scheduling or signal handling. The actual stack switch is done in
878    entry.S */
879 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
880 {
881         struct pt_regs *regs = eregs;
882         /* Did already sync */
883         if (eregs == (struct pt_regs *)eregs->sp)
884                 ;
885         /* Exception from user space */
886         else if (user_mode(eregs))
887                 regs = task_pt_regs(current);
888         /* Exception from kernel and interrupts are enabled. Move to
889            kernel process stack. */
890         else if (eregs->flags & X86_EFLAGS_IF)
891                 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
892         if (eregs != regs)
893                 *regs = *eregs;
894         return regs;
895 }
896
897 /* runs on IST stack. */
898 asmlinkage void __kprobes do_debug(struct pt_regs *regs,
899                                    unsigned long error_code)
900 {
901         struct task_struct *tsk = current;
902         unsigned long condition;
903         siginfo_t info;
904
905         trace_hardirqs_fixup();
906
907         get_debugreg(condition, 6);
908
909         /*
910          * The processor cleared BTF, so don't mark that we need it set.
911          */
912         clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
913         tsk->thread.debugctlmsr = 0;
914
915         if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
916                                                 SIGTRAP) == NOTIFY_STOP)
917                 return;
918
919         preempt_conditional_sti(regs);
920
921         /* Mask out spurious debug traps due to lazy DR7 setting */
922         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
923                 if (!tsk->thread.debugreg7)
924                         goto clear_dr7;
925         }
926
927         tsk->thread.debugreg6 = condition;
928
929         /*
930          * Single-stepping through TF: make sure we ignore any events in
931          * kernel space (but re-enable TF when returning to user mode).
932          */
933         if (condition & DR_STEP) {
934                 if (!user_mode(regs))
935                         goto clear_TF_reenable;
936         }
937
938         /* Ok, finally something we can handle */
939         tsk->thread.trap_no = 1;
940         tsk->thread.error_code = error_code;
941         info.si_signo = SIGTRAP;
942         info.si_errno = 0;
943         info.si_code = TRAP_BRKPT;
944         info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
945         force_sig_info(SIGTRAP, &info, tsk);
946
947 clear_dr7:
948         set_debugreg(0, 7);
949         preempt_conditional_cli(regs);
950         return;
951
952 clear_TF_reenable:
953         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
954         regs->flags &= ~X86_EFLAGS_TF;
955         preempt_conditional_cli(regs);
956         return;
957 }
958
959 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
960 {
961         if (fixup_exception(regs))
962                 return 1;
963
964         notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
965         /* Illegal floating point operation in the kernel */
966         current->thread.trap_no = trapnr;
967         die(str, regs, 0);
968         return 0;
969 }
970
971 /*
972  * Note that we play around with the 'TS' bit in an attempt to get
973  * the correct behaviour even in the presence of the asynchronous
974  * IRQ13 behaviour
975  */
976 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
977 {
978         void __user *ip = (void __user *)(regs->ip);
979         struct task_struct *task;
980         siginfo_t info;
981         unsigned short cwd, swd;
982
983         conditional_sti(regs);
984         if (!user_mode(regs) &&
985             kernel_math_error(regs, "kernel x87 math error", 16))
986                 return;
987
988         /*
989          * Save the info for the exception handler and clear the error.
990          */
991         task = current;
992         save_init_fpu(task);
993         task->thread.trap_no = 16;
994         task->thread.error_code = 0;
995         info.si_signo = SIGFPE;
996         info.si_errno = 0;
997         info.si_code = __SI_FAULT;
998         info.si_addr = ip;
999         /*
1000          * (~cwd & swd) will mask out exceptions that are not set to unmasked
1001          * status.  0x3f is the exception bits in these regs, 0x200 is the
1002          * C1 reg you need in case of a stack fault, 0x040 is the stack
1003          * fault bit.  We should only be taking one exception at a time,
1004          * so if this combination doesn't produce any single exception,
1005          * then we have a bad program that isn't synchronizing its FPU usage
1006          * and it will suffer the consequences since we won't be able to
1007          * fully reproduce the context of the exception
1008          */
1009         cwd = get_fpu_cwd(task);
1010         swd = get_fpu_swd(task);
1011         switch (swd & ~cwd & 0x3f) {
1012         case 0x000: /* No unmasked exception */
1013         default: /* Multiple exceptions */
1014                 break;
1015         case 0x001: /* Invalid Op */
1016                 /*
1017                  * swd & 0x240 == 0x040: Stack Underflow
1018                  * swd & 0x240 == 0x240: Stack Overflow
1019                  * User must clear the SF bit (0x40) if set
1020                  */
1021                 info.si_code = FPE_FLTINV;
1022                 break;
1023         case 0x002: /* Denormalize */
1024         case 0x010: /* Underflow */
1025                 info.si_code = FPE_FLTUND;
1026                 break;
1027         case 0x004: /* Zero Divide */
1028                 info.si_code = FPE_FLTDIV;
1029                 break;
1030         case 0x008: /* Overflow */
1031                 info.si_code = FPE_FLTOVF;
1032                 break;
1033         case 0x020: /* Precision */
1034                 info.si_code = FPE_FLTRES;
1035                 break;
1036         }
1037         force_sig_info(SIGFPE, &info, task);
1038 }
1039
1040 asmlinkage void bad_intr(void)
1041 {
1042         printk("bad interrupt");
1043 }
1044
1045 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1046 {
1047         void __user *ip = (void __user *)(regs->ip);
1048         struct task_struct *task;
1049         siginfo_t info;
1050         unsigned short mxcsr;
1051
1052         conditional_sti(regs);
1053         if (!user_mode(regs) &&
1054                         kernel_math_error(regs, "kernel simd math error", 19))
1055                 return;
1056
1057         /*
1058          * Save the info for the exception handler and clear the error.
1059          */
1060         task = current;
1061         save_init_fpu(task);
1062         task->thread.trap_no = 19;
1063         task->thread.error_code = 0;
1064         info.si_signo = SIGFPE;
1065         info.si_errno = 0;
1066         info.si_code = __SI_FAULT;
1067         info.si_addr = ip;
1068         /*
1069          * The SIMD FPU exceptions are handled a little differently, as there
1070          * is only a single status/control register.  Thus, to determine which
1071          * unmasked exception was caught we must mask the exception mask bits
1072          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1073          */
1074         mxcsr = get_fpu_mxcsr(task);
1075         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1076         case 0x000:
1077         default:
1078                 break;
1079         case 0x001: /* Invalid Op */
1080                 info.si_code = FPE_FLTINV;
1081                 break;
1082         case 0x002: /* Denormalize */
1083         case 0x010: /* Underflow */
1084                 info.si_code = FPE_FLTUND;
1085                 break;
1086         case 0x004: /* Zero Divide */
1087                 info.si_code = FPE_FLTDIV;
1088                 break;
1089         case 0x008: /* Overflow */
1090                 info.si_code = FPE_FLTOVF;
1091                 break;
1092         case 0x020: /* Precision */
1093                 info.si_code = FPE_FLTRES;
1094                 break;
1095         }
1096         force_sig_info(SIGFPE, &info, task);
1097 }
1098
1099 asmlinkage void do_spurious_interrupt_bug(struct pt_regs *regs)
1100 {
1101 }
1102
1103 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1104 {
1105 }
1106
1107 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1108 {
1109 }
1110
1111 /*
1112  * 'math_state_restore()' saves the current math information in the
1113  * old math state array, and gets the new ones from the current task
1114  *
1115  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1116  * Don't touch unless you *really* know how it works.
1117  */
1118 asmlinkage void math_state_restore(void)
1119 {
1120         struct task_struct *me = current;
1121
1122         if (!used_math()) {
1123                 local_irq_enable();
1124                 /*
1125                  * does a slab alloc which can sleep
1126                  */
1127                 if (init_fpu(me)) {
1128                         /*
1129                          * ran out of memory!
1130                          */
1131                         do_group_exit(SIGKILL);
1132                         return;
1133                 }
1134                 local_irq_disable();
1135         }
1136
1137         clts();                         /* Allow maths ops (or we recurse) */
1138         /*
1139          * Paranoid restore. send a SIGSEGV if we fail to restore the state.
1140          */
1141         if (unlikely(restore_fpu_checking(&me->thread.xstate->fxsave))) {
1142                 stts();
1143                 force_sig(SIGSEGV, me);
1144                 return;
1145         }
1146         task_thread_info(me)->status |= TS_USEDFPU;
1147         me->fpu_counter++;
1148 }
1149 EXPORT_SYMBOL_GPL(math_state_restore);
1150
1151 void __init trap_init(void)
1152 {
1153         set_intr_gate(0, &divide_error);
1154         set_intr_gate_ist(1, &debug, DEBUG_STACK);
1155         set_intr_gate_ist(2, &nmi, NMI_STACK);
1156         /* int3 can be called from all */
1157         set_system_gate_ist(3, &int3, DEBUG_STACK);
1158         /* int4 can be called from all */
1159         set_system_gate(4, &overflow);
1160         set_intr_gate(5, &bounds);
1161         set_intr_gate(6, &invalid_op);
1162         set_intr_gate(7, &device_not_available);
1163         set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
1164         set_intr_gate(9, &coprocessor_segment_overrun);
1165         set_intr_gate(10, &invalid_TSS);
1166         set_intr_gate(11, &segment_not_present);
1167         set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
1168         set_intr_gate(13, &general_protection);
1169         set_intr_gate(14, &page_fault);
1170         set_intr_gate(15, &spurious_interrupt_bug);
1171         set_intr_gate(16, &coprocessor_error);
1172         set_intr_gate(17, &alignment_check);
1173 #ifdef CONFIG_X86_MCE
1174         set_intr_gate_ist(18, &machine_check, MCE_STACK);
1175 #endif
1176         set_intr_gate(19, &simd_coprocessor_error);
1177
1178 #ifdef CONFIG_IA32_EMULATION
1179         set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1180 #endif
1181         /*
1182          * initialize the per thread extended state:
1183          */
1184         init_thread_xstate();
1185         /*
1186          * Should be a barrier for any external CPU state:
1187          */
1188         cpu_init();
1189 }
1190
1191 static int __init oops_setup(char *s)
1192 {
1193         if (!s)
1194                 return -EINVAL;
1195         if (!strcmp(s, "panic"))
1196                 panic_on_oops = 1;
1197         return 0;
1198 }
1199 early_param("oops", oops_setup);
1200
1201 static int __init kstack_setup(char *s)
1202 {
1203         if (!s)
1204                 return -EINVAL;
1205         kstack_depth_to_print = simple_strtoul(s, NULL, 0);
1206         return 0;
1207 }
1208 early_param("kstack", kstack_setup);
1209
1210 static int __init code_bytes_setup(char *s)
1211 {
1212         code_bytes = simple_strtoul(s, NULL, 0);
1213         if (code_bytes > 8192)
1214                 code_bytes = 8192;
1215
1216         return 1;
1217 }
1218 __setup("code_bytes=", code_bytes_setup);