bug.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #define pr_fmt(fmt) fmt
  32. #include <linux/list.h>
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/bug.h>
  36. #include <linux/sched.h>
  37. #include <linux/rculist.h>
  38. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  39. static inline unsigned long bug_addr(const struct bug_entry *bug)
  40. {
  41. #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  42. return bug->bug_addr;
  43. #else
  44. return (unsigned long)bug + bug->bug_addr_disp;
  45. #endif
  46. }
  47. #ifdef CONFIG_MODULES
  48. /* Updates are protected by module mutex */
  49. static LIST_HEAD(module_bug_list);
  50. static struct bug_entry *module_find_bug(unsigned long bugaddr)
  51. {
  52. struct module *mod;
  53. struct bug_entry *bug = NULL;
  54. rcu_read_lock_sched();
  55. list_for_each_entry_rcu(mod, &module_bug_list, bug_list) {
  56. unsigned i;
  57. bug = mod->bug_table;
  58. for (i = 0; i < mod->num_bugs; ++i, ++bug)
  59. if (bugaddr == bug_addr(bug))
  60. goto out;
  61. }
  62. bug = NULL;
  63. out:
  64. rcu_read_unlock_sched();
  65. return bug;
  66. }
  67. void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  68. struct module *mod)
  69. {
  70. char *secstrings;
  71. unsigned int i;
  72. lockdep_assert_held(&module_mutex);
  73. mod->bug_table = NULL;
  74. mod->num_bugs = 0;
  75. /* Find the __bug_table section, if present */
  76. secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  77. for (i = 1; i < hdr->e_shnum; i++) {
  78. if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
  79. continue;
  80. mod->bug_table = (void *) sechdrs[i].sh_addr;
  81. mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
  82. break;
  83. }
  84. /*
  85. * Strictly speaking this should have a spinlock to protect against
  86. * traversals, but since we only traverse on BUG()s, a spinlock
  87. * could potentially lead to deadlock and thus be counter-productive.
  88. * Thus, this uses RCU to safely manipulate the bug list, since BUG
  89. * must run in non-interruptive state.
  90. */
  91. list_add_rcu(&mod->bug_list, &module_bug_list);
  92. }
  93. void module_bug_cleanup(struct module *mod)
  94. {
  95. lockdep_assert_held(&module_mutex);
  96. list_del_rcu(&mod->bug_list);
  97. }
  98. #else
  99. static inline struct bug_entry *module_find_bug(unsigned long bugaddr)
  100. {
  101. return NULL;
  102. }
  103. #endif
  104. struct bug_entry *find_bug(unsigned long bugaddr)
  105. {
  106. struct bug_entry *bug;
  107. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  108. if (bugaddr == bug_addr(bug))
  109. return bug;
  110. return module_find_bug(bugaddr);
  111. }
  112. enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
  113. {
  114. struct bug_entry *bug;
  115. const char *file;
  116. unsigned line, warning, once, done;
  117. if (!is_valid_bugaddr(bugaddr))
  118. return BUG_TRAP_TYPE_NONE;
  119. bug = find_bug(bugaddr);
  120. file = NULL;
  121. line = 0;
  122. warning = 0;
  123. if (bug) {
  124. #ifdef CONFIG_DEBUG_BUGVERBOSE
  125. #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  126. file = bug->file;
  127. #else
  128. file = (const char *)bug + bug->file_disp;
  129. #endif
  130. line = bug->line;
  131. #endif
  132. warning = (bug->flags & BUGFLAG_WARNING) != 0;
  133. once = (bug->flags & BUGFLAG_ONCE) != 0;
  134. done = (bug->flags & BUGFLAG_DONE) != 0;
  135. if (warning && once) {
  136. if (done)
  137. return BUG_TRAP_TYPE_WARN;
  138. /*
  139. * Since this is the only store, concurrency is not an issue.
  140. */
  141. bug->flags |= BUGFLAG_DONE;
  142. }
  143. }
  144. if (warning) {
  145. /* this is a WARN_ON rather than BUG/BUG_ON */
  146. __warn(file, line, (void *)bugaddr, BUG_GET_TAINT(bug), regs,
  147. NULL);
  148. return BUG_TRAP_TYPE_WARN;
  149. }
  150. printk(KERN_DEFAULT "------------[ cut here ]------------\n");
  151. if (file)
  152. pr_crit("kernel BUG at %s:%u!\n", file, line);
  153. else
  154. pr_crit("Kernel BUG at %p [verbose debug info unavailable]\n",
  155. (void *)bugaddr);
  156. return BUG_TRAP_TYPE_BUG;
  157. }