Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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