bug.c 4.7 KB

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