bug.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 relative pointers 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. #include <linux/ftrace.h>
  39. #include <linux/context_tracking.h>
  40. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  41. static inline unsigned long bug_addr(const struct bug_entry *bug)
  42. {
  43. #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  44. return (unsigned long)&bug->bug_addr_disp + bug->bug_addr_disp;
  45. #else
  46. return bug->bug_addr;
  47. #endif
  48. }
  49. #ifdef CONFIG_MODULES
  50. /* Updates are protected by module mutex */
  51. static LIST_HEAD(module_bug_list);
  52. static struct bug_entry *module_find_bug(unsigned long bugaddr)
  53. {
  54. struct bug_entry *bug;
  55. struct module *mod;
  56. guard(rcu)();
  57. list_for_each_entry_rcu(mod, &module_bug_list, bug_list) {
  58. unsigned i;
  59. bug = mod->bug_table;
  60. for (i = 0; i < mod->num_bugs; ++i, ++bug)
  61. if (bugaddr == bug_addr(bug))
  62. return bug;
  63. }
  64. return NULL;
  65. }
  66. void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  67. struct module *mod)
  68. {
  69. char *secstrings;
  70. unsigned int i;
  71. mod->bug_table = NULL;
  72. mod->num_bugs = 0;
  73. /* Find the __bug_table section, if present */
  74. secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  75. for (i = 1; i < hdr->e_shnum; i++) {
  76. if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
  77. continue;
  78. mod->bug_table = (void *) sechdrs[i].sh_addr;
  79. mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
  80. break;
  81. }
  82. /*
  83. * Strictly speaking this should have a spinlock to protect against
  84. * traversals, but since we only traverse on BUG()s, a spinlock
  85. * could potentially lead to deadlock and thus be counter-productive.
  86. * Thus, this uses RCU to safely manipulate the bug list, since BUG
  87. * must run in non-interruptive state.
  88. */
  89. list_add_rcu(&mod->bug_list, &module_bug_list);
  90. }
  91. void module_bug_cleanup(struct module *mod)
  92. {
  93. list_del_rcu(&mod->bug_list);
  94. }
  95. #else
  96. static inline struct bug_entry *module_find_bug(unsigned long bugaddr)
  97. {
  98. return NULL;
  99. }
  100. #endif
  101. void bug_get_file_line(struct bug_entry *bug, const char **file,
  102. unsigned int *line)
  103. {
  104. #ifdef CONFIG_DEBUG_BUGVERBOSE
  105. #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  106. *file = (const char *)&bug->file_disp + bug->file_disp;
  107. #else
  108. *file = bug->file;
  109. #endif
  110. *line = bug->line;
  111. #else
  112. *file = NULL;
  113. *line = 0;
  114. #endif
  115. }
  116. static const char *bug_get_format(struct bug_entry *bug)
  117. {
  118. const char *format = NULL;
  119. #ifdef HAVE_ARCH_BUG_FORMAT
  120. #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  121. /*
  122. * Allow an architecture to:
  123. * - relative encode NULL (difficult vs KASLR);
  124. * - use a literal 0 (there are no valid objects inside
  125. * the __bug_table itself to refer to after all);
  126. * - use an empty string.
  127. */
  128. if (bug->format_disp)
  129. format = (const char *)&bug->format_disp + bug->format_disp;
  130. if (format && format[0] == '\0')
  131. format = NULL;
  132. #else
  133. format = bug->format;
  134. #endif
  135. #endif
  136. return format;
  137. }
  138. struct bug_entry *find_bug(unsigned long bugaddr)
  139. {
  140. struct bug_entry *bug;
  141. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  142. if (bugaddr == bug_addr(bug))
  143. return bug;
  144. return module_find_bug(bugaddr);
  145. }
  146. static __printf(1, 0)
  147. void __warn_printf(const char *fmt, struct pt_regs *regs)
  148. {
  149. if (!fmt)
  150. return;
  151. #ifdef HAVE_ARCH_BUG_FORMAT_ARGS
  152. if (regs) {
  153. struct arch_va_list _args;
  154. va_list *args = __warn_args(&_args, regs);
  155. if (args) {
  156. vprintk(fmt, *args);
  157. return;
  158. }
  159. }
  160. #endif
  161. printk("%s", fmt);
  162. }
  163. static enum bug_trap_type __report_bug(struct bug_entry *bug, unsigned long bugaddr, struct pt_regs *regs)
  164. {
  165. bool warning, once, done, no_cut, has_args;
  166. const char *file, *fmt;
  167. unsigned line;
  168. if (!bug) {
  169. if (!is_valid_bugaddr(bugaddr))
  170. return BUG_TRAP_TYPE_NONE;
  171. bug = find_bug(bugaddr);
  172. if (!bug)
  173. return BUG_TRAP_TYPE_NONE;
  174. }
  175. disable_trace_on_warning();
  176. bug_get_file_line(bug, &file, &line);
  177. fmt = bug_get_format(bug);
  178. warning = bug->flags & BUGFLAG_WARNING;
  179. once = bug->flags & BUGFLAG_ONCE;
  180. done = bug->flags & BUGFLAG_DONE;
  181. no_cut = bug->flags & BUGFLAG_NO_CUT_HERE;
  182. has_args = bug->flags & BUGFLAG_ARGS;
  183. if (warning && once) {
  184. if (done)
  185. return BUG_TRAP_TYPE_WARN;
  186. /*
  187. * Since this is the only store, concurrency is not an issue.
  188. */
  189. bug->flags |= BUGFLAG_DONE;
  190. }
  191. /*
  192. * BUG() and WARN_ON() families don't print a custom debug message
  193. * before triggering the exception handler, so we must add the
  194. * "cut here" line now. WARN() issues its own "cut here" before the
  195. * extra debugging message it writes before triggering the handler.
  196. */
  197. if (!no_cut) {
  198. printk(KERN_DEFAULT CUT_HERE);
  199. __warn_printf(fmt, has_args ? regs : NULL);
  200. }
  201. if (warning) {
  202. /* this is a WARN_ON rather than BUG/BUG_ON */
  203. __warn(file, line, (void *)bugaddr, BUG_GET_TAINT(bug), regs,
  204. NULL);
  205. return BUG_TRAP_TYPE_WARN;
  206. }
  207. if (file)
  208. pr_crit("kernel BUG at %s:%u!\n", file, line);
  209. else
  210. pr_crit("Kernel BUG at %pB [verbose debug info unavailable]\n",
  211. (void *)bugaddr);
  212. return BUG_TRAP_TYPE_BUG;
  213. }
  214. enum bug_trap_type report_bug_entry(struct bug_entry *bug, struct pt_regs *regs)
  215. {
  216. enum bug_trap_type ret;
  217. bool rcu = false;
  218. rcu = warn_rcu_enter();
  219. ret = __report_bug(bug, bug_addr(bug), regs);
  220. warn_rcu_exit(rcu);
  221. return ret;
  222. }
  223. enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
  224. {
  225. enum bug_trap_type ret;
  226. bool rcu = false;
  227. rcu = warn_rcu_enter();
  228. ret = __report_bug(NULL, bugaddr, regs);
  229. warn_rcu_exit(rcu);
  230. return ret;
  231. }
  232. static void clear_once_table(struct bug_entry *start, struct bug_entry *end)
  233. {
  234. struct bug_entry *bug;
  235. for (bug = start; bug < end; bug++)
  236. bug->flags &= ~BUGFLAG_DONE;
  237. }
  238. void generic_bug_clear_once(void)
  239. {
  240. #ifdef CONFIG_MODULES
  241. struct module *mod;
  242. scoped_guard(rcu) {
  243. list_for_each_entry_rcu(mod, &module_bug_list, bug_list)
  244. clear_once_table(mod->bug_table,
  245. mod->bug_table + mod->num_bugs);
  246. }
  247. #endif
  248. clear_once_table(__start___bug_table, __stop___bug_table);
  249. }