traps.c 3.9 KB

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