traps.c 4.0 KB

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