bug.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Generic support for BUG()
  3. This respects the following config options:
  4. CONFIG_BUG - emit BUG traps. Nothing happens without this.
  5. CONFIG_GENERIC_BUG - enable this code.
  6. CONFIG_DEBUG_BUGVERBOSE - emit full file+line information for each BUG
  7. CONFIG_BUG and CONFIG_DEBUG_BUGVERBOSE are potentially user-settable
  8. (though they're generally always on).
  9. CONFIG_GENERIC_BUG is set by each architecture using this code.
  10. To use this, your architecture must:
  11. 1. Set up the config options:
  12. - Enable CONFIG_GENERIC_BUG if CONFIG_BUG
  13. 2. Implement BUG (and optionally BUG_ON, WARN, WARN_ON)
  14. - Define HAVE_ARCH_BUG
  15. - Implement BUG() to generate a faulting instruction
  16. - NOTE: struct bug_entry does not have "file" or "line" entries
  17. when CONFIG_DEBUG_BUGVERBOSE is not enabled, so you must generate
  18. the values accordingly.
  19. 3. Implement the trap
  20. - In the illegal instruction trap handler (typically), verify
  21. that the fault was in kernel mode, and call report_bug()
  22. - report_bug() will return whether it was a false alarm, a warning,
  23. or an actual bug.
  24. - You must implement the is_valid_bugaddr(bugaddr) callback which
  25. returns true if the eip is a real kernel address, and it points
  26. to the expected BUG trap instruction.
  27. Jeremy Fitzhardinge <jeremy@goop.org> 2006
  28. */
  29. #include <linux/list.h>
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/bug.h>
  33. #include <linux/sched.h>
  34. extern const struct bug_entry __start___bug_table[], __stop___bug_table[];
  35. #ifdef CONFIG_MODULES
  36. static LIST_HEAD(module_bug_list);
  37. static const struct bug_entry *module_find_bug(unsigned long bugaddr)
  38. {
  39. struct module *mod;
  40. list_for_each_entry(mod, &module_bug_list, bug_list) {
  41. const struct bug_entry *bug = mod->bug_table;
  42. unsigned i;
  43. for (i = 0; i < mod->num_bugs; ++i, ++bug)
  44. if (bugaddr == bug->bug_addr)
  45. return bug;
  46. }
  47. return NULL;
  48. }
  49. int module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  50. struct module *mod)
  51. {
  52. char *secstrings;
  53. unsigned int i;
  54. mod->bug_table = NULL;
  55. mod->num_bugs = 0;
  56. /* Find the __bug_table section, if present */
  57. secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  58. for (i = 1; i < hdr->e_shnum; i++) {
  59. if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
  60. continue;
  61. mod->bug_table = (void *) sechdrs[i].sh_addr;
  62. mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
  63. break;
  64. }
  65. /*
  66. * Strictly speaking this should have a spinlock to protect against
  67. * traversals, but since we only traverse on BUG()s, a spinlock
  68. * could potentially lead to deadlock and thus be counter-productive.
  69. */
  70. list_add(&mod->bug_list, &module_bug_list);
  71. return 0;
  72. }
  73. void module_bug_cleanup(struct module *mod)
  74. {
  75. list_del(&mod->bug_list);
  76. }
  77. #else
  78. static inline const struct bug_entry *module_find_bug(unsigned long bugaddr)
  79. {
  80. return NULL;
  81. }
  82. #endif
  83. const struct bug_entry *find_bug(unsigned long bugaddr)
  84. {
  85. const struct bug_entry *bug;
  86. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  87. if (bugaddr == bug->bug_addr)
  88. return bug;
  89. return module_find_bug(bugaddr);
  90. }
  91. enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
  92. {
  93. const struct bug_entry *bug;
  94. const char *file;
  95. unsigned line, warning;
  96. if (!is_valid_bugaddr(bugaddr))
  97. return BUG_TRAP_TYPE_NONE;
  98. bug = find_bug(bugaddr);
  99. printk(KERN_EMERG "------------[ cut here ]------------\n");
  100. file = NULL;
  101. line = 0;
  102. warning = 0;
  103. if (bug) {
  104. #ifdef CONFIG_DEBUG_BUGVERBOSE
  105. file = bug->file;
  106. line = bug->line;
  107. #endif
  108. warning = (bug->flags & BUGFLAG_WARNING) != 0;
  109. }
  110. if (warning) {
  111. /* this is a WARN_ON rather than BUG/BUG_ON */
  112. if (file)
  113. printk(KERN_ERR "Badness at %s:%u\n",
  114. file, line);
  115. else
  116. printk(KERN_ERR "Badness at %p "
  117. "[verbose debug info unavailable]\n",
  118. (void *)bugaddr);
  119. show_regs(regs);
  120. add_taint(TAINT_WARN);
  121. return BUG_TRAP_TYPE_WARN;
  122. }
  123. if (file)
  124. printk(KERN_CRIT "kernel BUG at %s:%u!\n",
  125. file, line);
  126. else
  127. printk(KERN_CRIT "Kernel BUG at %p "
  128. "[verbose debug info unavailable]\n",
  129. (void *)bugaddr);
  130. return BUG_TRAP_TYPE_BUG;
  131. }