traps.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <linux/bug.h>
  2. #include <linux/io.h>
  3. #include <linux/types.h>
  4. #include <linux/kdebug.h>
  5. #include <linux/signal.h>
  6. #include <linux/sched.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/hardirq.h>
  9. #include <linux/kernel.h>
  10. #include <linux/kexec.h>
  11. #include <linux/module.h>
  12. #include <asm/unwinder.h>
  13. #include <asm/traps.h>
  14. static DEFINE_SPINLOCK(die_lock);
  15. void die(const char *str, struct pt_regs *regs, long err)
  16. {
  17. static int die_counter;
  18. oops_enter();
  19. spin_lock_irq(&die_lock);
  20. console_verbose();
  21. bust_spinlocks(1);
  22. printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
  23. print_modules();
  24. show_regs(regs);
  25. printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
  26. task_pid_nr(current), task_stack_page(current) + 1);
  27. if (!user_mode(regs) || in_interrupt())
  28. dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
  29. (unsigned long)task_stack_page(current));
  30. notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
  31. bust_spinlocks(0);
  32. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  33. spin_unlock_irq(&die_lock);
  34. oops_exit();
  35. if (kexec_should_crash(current))
  36. crash_kexec(regs);
  37. if (in_interrupt())
  38. panic("Fatal exception in interrupt");
  39. if (panic_on_oops)
  40. panic("Fatal exception");
  41. do_exit(SIGSEGV);
  42. }
  43. void die_if_kernel(const char *str, struct pt_regs *regs, long err)
  44. {
  45. if (!user_mode(regs))
  46. die(str, regs, err);
  47. }
  48. /*
  49. * try and fix up kernelspace address errors
  50. * - userspace errors just cause EFAULT to be returned, resulting in SEGV
  51. * - kernel/userspace interfaces cause a jump to an appropriate handler
  52. * - other kernel errors are bad
  53. */
  54. void die_if_no_fixup(const char *str, struct pt_regs *regs, long err)
  55. {
  56. if (!user_mode(regs)) {
  57. const struct exception_table_entry *fixup;
  58. fixup = search_exception_tables(regs->pc);
  59. if (fixup) {
  60. regs->pc = fixup->fixup;
  61. return;
  62. }
  63. die(str, regs, err);
  64. }
  65. }
  66. #ifdef CONFIG_GENERIC_BUG
  67. static void handle_BUG(struct pt_regs *regs)
  68. {
  69. const struct bug_entry *bug;
  70. unsigned long bugaddr = regs->pc;
  71. enum bug_trap_type tt;
  72. if (!is_valid_bugaddr(bugaddr))
  73. goto invalid;
  74. bug = find_bug(bugaddr);
  75. /* Switch unwinders when unwind_stack() is called */
  76. if (bug->flags & BUGFLAG_UNWINDER)
  77. unwinder_faulted = 1;
  78. tt = report_bug(bugaddr, regs);
  79. if (tt == BUG_TRAP_TYPE_WARN) {
  80. regs->pc += instruction_size(bugaddr);
  81. return;
  82. }
  83. invalid:
  84. die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
  85. }
  86. int is_valid_bugaddr(unsigned long addr)
  87. {
  88. insn_size_t opcode;
  89. if (addr < PAGE_OFFSET)
  90. return 0;
  91. if (probe_kernel_address((insn_size_t *)addr, opcode))
  92. return 0;
  93. if (opcode == TRAPA_BUG_OPCODE)
  94. return 1;
  95. return 0;
  96. }
  97. #endif
  98. /*
  99. * Generic trap handler.
  100. */
  101. BUILD_TRAP_HANDLER(debug)
  102. {
  103. TRAP_HANDLER_DECL;
  104. /* Rewind */
  105. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  106. if (notify_die(DIE_TRAP, "debug trap", regs, 0, vec & 0xff,
  107. SIGTRAP) == NOTIFY_STOP)
  108. return;
  109. force_sig(SIGTRAP, current);
  110. }
  111. /*
  112. * Special handler for BUG() traps.
  113. */
  114. BUILD_TRAP_HANDLER(bug)
  115. {
  116. TRAP_HANDLER_DECL;
  117. /* Rewind */
  118. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  119. if (notify_die(DIE_TRAP, "bug trap", regs, 0, TRAPA_BUG_OPCODE & 0xff,
  120. SIGTRAP) == NOTIFY_STOP)
  121. return;
  122. #ifdef CONFIG_GENERIC_BUG
  123. if (__kernel_text_address(instruction_pointer(regs))) {
  124. insn_size_t insn = *(insn_size_t *)instruction_pointer(regs);
  125. if (insn == TRAPA_BUG_OPCODE)
  126. handle_BUG(regs);
  127. return;
  128. }
  129. #endif
  130. force_sig(SIGTRAP, current);
  131. }
  132. BUILD_TRAP_HANDLER(nmi)
  133. {
  134. unsigned int cpu = smp_processor_id();
  135. TRAP_HANDLER_DECL;
  136. nmi_enter();
  137. nmi_count(cpu)++;
  138. switch (notify_die(DIE_NMI, "NMI", regs, 0, vec & 0xff, SIGINT)) {
  139. case NOTIFY_OK:
  140. case NOTIFY_STOP:
  141. break;
  142. case NOTIFY_BAD:
  143. die("Fatal Non-Maskable Interrupt", regs, SIGINT);
  144. default:
  145. printk(KERN_ALERT "Got NMI, but nobody cared. Ignoring...\n");
  146. break;
  147. }
  148. nmi_exit();
  149. }