traps.c 4.0 KB

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