traps.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <asm/unwinder.h>
  10. #include <asm/traps.h>
  11. #ifdef CONFIG_GENERIC_BUG
  12. static void handle_BUG(struct pt_regs *regs)
  13. {
  14. const struct bug_entry *bug;
  15. unsigned long bugaddr = regs->pc;
  16. enum bug_trap_type tt;
  17. if (!is_valid_bugaddr(bugaddr))
  18. goto invalid;
  19. bug = find_bug(bugaddr);
  20. /* Switch unwinders when unwind_stack() is called */
  21. if (bug->flags & BUGFLAG_UNWINDER)
  22. unwinder_faulted = 1;
  23. tt = report_bug(bugaddr, regs);
  24. if (tt == BUG_TRAP_TYPE_WARN) {
  25. regs->pc += instruction_size(bugaddr);
  26. return;
  27. }
  28. invalid:
  29. die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
  30. }
  31. int is_valid_bugaddr(unsigned long addr)
  32. {
  33. insn_size_t opcode;
  34. if (addr < PAGE_OFFSET)
  35. return 0;
  36. if (probe_kernel_address((insn_size_t *)addr, opcode))
  37. return 0;
  38. if (opcode == TRAPA_BUG_OPCODE)
  39. return 1;
  40. return 0;
  41. }
  42. #endif
  43. /*
  44. * Generic trap handler.
  45. */
  46. BUILD_TRAP_HANDLER(debug)
  47. {
  48. TRAP_HANDLER_DECL;
  49. /* Rewind */
  50. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  51. if (notify_die(DIE_TRAP, "debug trap", regs, 0, vec & 0xff,
  52. SIGTRAP) == NOTIFY_STOP)
  53. return;
  54. force_sig(SIGTRAP, current);
  55. }
  56. /*
  57. * Special handler for BUG() traps.
  58. */
  59. BUILD_TRAP_HANDLER(bug)
  60. {
  61. TRAP_HANDLER_DECL;
  62. /* Rewind */
  63. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  64. if (notify_die(DIE_TRAP, "bug trap", regs, 0, TRAPA_BUG_OPCODE & 0xff,
  65. SIGTRAP) == NOTIFY_STOP)
  66. return;
  67. #ifdef CONFIG_GENERIC_BUG
  68. if (__kernel_text_address(instruction_pointer(regs))) {
  69. insn_size_t insn = *(insn_size_t *)instruction_pointer(regs);
  70. if (insn == TRAPA_BUG_OPCODE)
  71. handle_BUG(regs);
  72. return;
  73. }
  74. #endif
  75. force_sig(SIGTRAP, current);
  76. }
  77. BUILD_TRAP_HANDLER(nmi)
  78. {
  79. unsigned int cpu = smp_processor_id();
  80. TRAP_HANDLER_DECL;
  81. nmi_enter();
  82. nmi_count(cpu)++;
  83. switch (notify_die(DIE_NMI, "NMI", regs, 0, vec & 0xff, SIGINT)) {
  84. case NOTIFY_OK:
  85. case NOTIFY_STOP:
  86. break;
  87. case NOTIFY_BAD:
  88. die("Fatal Non-Maskable Interrupt", regs, SIGINT);
  89. default:
  90. printk(KERN_ALERT "Got NMI, but nobody cared. Ignoring...\n");
  91. break;
  92. }
  93. nmi_exit();
  94. }