bug.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. Generic support for BUG()
  4. This respects the following config options:
  5. CONFIG_BUG - emit BUG traps. Nothing happens without this.
  6. CONFIG_GENERIC_BUG - enable this code.
  7. CONFIG_GENERIC_BUG_RELATIVE_POINTERS - use 32-bit pointers relative to
  8. the containing struct bug_entry for bug_addr and file.
  9. CONFIG_DEBUG_BUGVERBOSE - emit full file+line information for each BUG
  10. CONFIG_BUG and CONFIG_DEBUG_BUGVERBOSE are potentially user-settable
  11. (though they're generally always on).
  12. CONFIG_GENERIC_BUG is set by each architecture using this code.
  13. To use this, your architecture must:
  14. 1. Set up the config options:
  15. - Enable CONFIG_GENERIC_BUG if CONFIG_BUG
  16. 2. Implement BUG (and optionally BUG_ON, WARN, WARN_ON)
  17. - Define HAVE_ARCH_BUG
  18. - Implement BUG() to generate a faulting instruction
  19. - NOTE: struct bug_entry does not have "file" or "line" entries
  20. when CONFIG_DEBUG_BUGVERBOSE is not enabled, so you must generate
  21. the values accordingly.
  22. 3. Implement the trap
  23. - In the illegal instruction trap handler (typically), verify
  24. that the fault was in kernel mode, and call report_bug()
  25. - report_bug() will return whether it was a false alarm, a warning,
  26. or an actual bug.
  27. - You must implement the is_valid_bugaddr(bugaddr) callback which
  28. returns true if the eip is a real kernel address, and it points
  29. to the expected BUG trap instruction.
  30. Jeremy Fitzhardinge <jeremy@goop.org> 2006
  31. */
  32. #define pr_fmt(fmt) fmt
  33. #include <linux/list.h>
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/bug.h>
  37. #include <linux/sched.h>
  38. #include <linux/rculist.h>
  39. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  40. static inline unsigned long bug_addr(const struct bug_entry *bug)
  41. {
  42. #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  43. return bug->bug_addr;
  44. #else
  45. return (unsigned long)bug + bug->bug_addr_disp;
  46. #endif
  47. }
  48. #ifdef CONFIG_MODULES
  49. /* Updates are protected by module mutex */
  50. static LIST_HEAD(module_bug_list);
  51. static struct bug_entry *module_find_bug(unsigned long bugaddr)
  52. {
  53. struct module *mod;
  54. struct bug_entry *bug = NULL;
  55. rcu_read_lock_sched();
  56. list_for_each_entry_rcu(mod, &module_bug_list, bug_list) {
  57. unsigned i;
  58. bug = mod->bug_table;
  59. for (i = 0; i < mod->num_bugs; ++i, ++bug)
  60. if (bugaddr == bug_addr(bug))
  61. goto out;
  62. }
  63. bug = NULL;
  64. out:
  65. rcu_read_unlock_sched();
  66. return bug;
  67. }
  68. void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  69. struct module *mod)
  70. {
  71. char *secstrings;
  72. unsigned int i;
  73. lockdep_assert_held(&module_mutex);
  74. mod->bug_table = NULL;
  75. mod->num_bugs = 0;
  76. /* Find the __bug_table section, if present */
  77. secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  78. for (i = 1; i < hdr->e_shnum; i++) {
  79. if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
  80. continue;
  81. mod->bug_table = (void *) sechdrs[i].sh_addr;
  82. mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
  83. break;
  84. }
  85. /*
  86. * Strictly speaking this should have a spinlock to protect against
  87. * traversals, but since we only traverse on BUG()s, a spinlock
  88. * could potentially lead to deadlock and thus be counter-productive.
  89. * Thus, this uses RCU to safely manipulate the bug list, since BUG
  90. * must run in non-interruptive state.
  91. */
  92. list_add_rcu(&mod->bug_list, &module_bug_list);
  93. }
  94. void module_bug_cleanup(struct module *mod)
  95. {
  96. lockdep_assert_held(&module_mutex);
  97. list_del_rcu(&mod->bug_list);
  98. }
  99. #else
  100. static inline struct bug_entry *module_find_bug(unsigned long bugaddr)
  101. {
  102. return NULL;
  103. }
  104. #endif
  105. struct bug_entry *find_bug(unsigned long bugaddr)
  106. {
  107. struct bug_entry *bug;
  108. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  109. if (bugaddr == bug_addr(bug))
  110. return bug;
  111. return module_find_bug(bugaddr);
  112. }
  113. enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
  114. {
  115. struct bug_entry *bug;
  116. const char *file;
  117. unsigned line, warning, once, done;
  118. if (!is_valid_bugaddr(bugaddr))
  119. return BUG_TRAP_TYPE_NONE;
  120. bug = find_bug(bugaddr);
  121. if (!bug)
  122. return BUG_TRAP_TYPE_NONE;
  123. file = NULL;
  124. line = 0;
  125. #ifdef CONFIG_DEBUG_BUGVERBOSE
  126. #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  127. file = bug->file;
  128. #else
  129. file = (const char *)bug + bug->file_disp;
  130. #endif
  131. line = bug->line;
  132. #endif
  133. warning = (bug->flags & BUGFLAG_WARNING) != 0;
  134. once = (bug->flags & BUGFLAG_ONCE) != 0;
  135. done = (bug->flags & BUGFLAG_DONE) != 0;
  136. if (warning && once) {
  137. if (done)
  138. return BUG_TRAP_TYPE_WARN;
  139. /*
  140. * Since this is the only store, concurrency is not an issue.
  141. */
  142. bug->flags |= BUGFLAG_DONE;
  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);
  151. if (file)
  152. pr_crit("kernel BUG at %s:%u!\n", file, line);
  153. else
  154. pr_crit("Kernel BUG at %pB [verbose debug info unavailable]\n",
  155. (void *)bugaddr);
  156. return BUG_TRAP_TYPE_BUG;
  157. }
  158. static void clear_once_table(struct bug_entry *start, struct bug_entry *end)
  159. {
  160. struct bug_entry *bug;
  161. for (bug = start; bug < end; bug++)
  162. bug->flags &= ~BUGFLAG_DONE;
  163. }
  164. void generic_bug_clear_once(void)
  165. {
  166. #ifdef CONFIG_MODULES
  167. struct module *mod;
  168. rcu_read_lock_sched();
  169. list_for_each_entry_rcu(mod, &module_bug_list, bug_list)
  170. clear_once_table(mod->bug_table,
  171. mod->bug_table + mod->num_bugs);
  172. rcu_read_unlock_sched();
  173. #endif
  174. clear_once_table(__start___bug_table, __stop___bug_table);
  175. }