bug.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #include <linux/ftrace.h>
  40. #include <trace/hooks/bug.h>
  41. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  42. static inline unsigned long bug_addr(const struct bug_entry *bug)
  43. {
  44. #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  45. return bug->bug_addr;
  46. #else
  47. return (unsigned long)bug + bug->bug_addr_disp;
  48. #endif
  49. }
  50. #ifdef CONFIG_MODULES
  51. /* Updates are protected by module mutex */
  52. static LIST_HEAD(module_bug_list);
  53. static struct bug_entry *module_find_bug(unsigned long bugaddr)
  54. {
  55. struct module *mod;
  56. struct bug_entry *bug = NULL;
  57. rcu_read_lock_sched();
  58. list_for_each_entry_rcu(mod, &module_bug_list, bug_list) {
  59. unsigned i;
  60. bug = mod->bug_table;
  61. for (i = 0; i < mod->num_bugs; ++i, ++bug)
  62. if (bugaddr == bug_addr(bug))
  63. goto out;
  64. }
  65. bug = NULL;
  66. out:
  67. rcu_read_unlock_sched();
  68. return bug;
  69. }
  70. void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  71. struct module *mod)
  72. {
  73. char *secstrings;
  74. unsigned int i;
  75. lockdep_assert_held(&module_mutex);
  76. mod->bug_table = NULL;
  77. mod->num_bugs = 0;
  78. /* Find the __bug_table section, if present */
  79. secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  80. for (i = 1; i < hdr->e_shnum; i++) {
  81. if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
  82. continue;
  83. mod->bug_table = (void *) sechdrs[i].sh_addr;
  84. mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
  85. break;
  86. }
  87. /*
  88. * Strictly speaking this should have a spinlock to protect against
  89. * traversals, but since we only traverse on BUG()s, a spinlock
  90. * could potentially lead to deadlock and thus be counter-productive.
  91. * Thus, this uses RCU to safely manipulate the bug list, since BUG
  92. * must run in non-interruptive state.
  93. */
  94. list_add_rcu(&mod->bug_list, &module_bug_list);
  95. }
  96. void module_bug_cleanup(struct module *mod)
  97. {
  98. lockdep_assert_held(&module_mutex);
  99. list_del_rcu(&mod->bug_list);
  100. }
  101. #else
  102. static inline struct bug_entry *module_find_bug(unsigned long bugaddr)
  103. {
  104. return NULL;
  105. }
  106. #endif
  107. struct bug_entry *find_bug(unsigned long bugaddr)
  108. {
  109. struct bug_entry *bug;
  110. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  111. if (bugaddr == bug_addr(bug))
  112. return bug;
  113. return module_find_bug(bugaddr);
  114. }
  115. enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
  116. {
  117. struct bug_entry *bug;
  118. const char *file;
  119. unsigned line, warning, once, done;
  120. if (!is_valid_bugaddr(bugaddr))
  121. return BUG_TRAP_TYPE_NONE;
  122. bug = find_bug(bugaddr);
  123. if (!bug)
  124. return BUG_TRAP_TYPE_NONE;
  125. disable_trace_on_warning();
  126. file = NULL;
  127. line = 0;
  128. #ifdef CONFIG_DEBUG_BUGVERBOSE
  129. #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  130. file = bug->file;
  131. #else
  132. file = (const char *)bug + bug->file_disp;
  133. #endif
  134. line = bug->line;
  135. #endif
  136. warning = (bug->flags & BUGFLAG_WARNING) != 0;
  137. once = (bug->flags & BUGFLAG_ONCE) != 0;
  138. done = (bug->flags & BUGFLAG_DONE) != 0;
  139. if (warning && once) {
  140. if (done)
  141. return BUG_TRAP_TYPE_WARN;
  142. /*
  143. * Since this is the only store, concurrency is not an issue.
  144. */
  145. bug->flags |= BUGFLAG_DONE;
  146. }
  147. /*
  148. * BUG() and WARN_ON() families don't print a custom debug message
  149. * before triggering the exception handler, so we must add the
  150. * "cut here" line now. WARN() issues its own "cut here" before the
  151. * extra debugging message it writes before triggering the handler.
  152. */
  153. if ((bug->flags & BUGFLAG_NO_CUT_HERE) == 0)
  154. printk(KERN_DEFAULT CUT_HERE);
  155. if (warning) {
  156. /* this is a WARN_ON rather than BUG/BUG_ON */
  157. __warn(file, line, (void *)bugaddr, BUG_GET_TAINT(bug), regs,
  158. NULL);
  159. return BUG_TRAP_TYPE_WARN;
  160. }
  161. if (file)
  162. pr_crit("kernel BUG at %s:%u!\n", file, line);
  163. else
  164. pr_crit("Kernel BUG at %pB [verbose debug info unavailable]\n",
  165. (void *)bugaddr);
  166. trace_android_rvh_report_bug(file, line, bugaddr);
  167. return BUG_TRAP_TYPE_BUG;
  168. }
  169. static void clear_once_table(struct bug_entry *start, struct bug_entry *end)
  170. {
  171. struct bug_entry *bug;
  172. for (bug = start; bug < end; bug++)
  173. bug->flags &= ~BUGFLAG_DONE;
  174. }
  175. void generic_bug_clear_once(void)
  176. {
  177. #ifdef CONFIG_MODULES
  178. struct module *mod;
  179. rcu_read_lock_sched();
  180. list_for_each_entry_rcu(mod, &module_bug_list, bug_list)
  181. clear_once_table(mod->bug_table,
  182. mod->bug_table + mod->num_bugs);
  183. rcu_read_unlock_sched();
  184. #endif
  185. clear_once_table(__start___bug_table, __stop___bug_table);
  186. }