bug.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <[email protected]> 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. #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. #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  45. return (unsigned long)&bug->bug_addr_disp + bug->bug_addr_disp;
  46. #else
  47. return bug->bug_addr;
  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. mod->bug_table = NULL;
  76. mod->num_bugs = 0;
  77. /* Find the __bug_table section, if present */
  78. secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  79. for (i = 1; i < hdr->e_shnum; i++) {
  80. if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
  81. continue;
  82. mod->bug_table = (void *) sechdrs[i].sh_addr;
  83. mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
  84. break;
  85. }
  86. /*
  87. * Strictly speaking this should have a spinlock to protect against
  88. * traversals, but since we only traverse on BUG()s, a spinlock
  89. * could potentially lead to deadlock and thus be counter-productive.
  90. * Thus, this uses RCU to safely manipulate the bug list, since BUG
  91. * must run in non-interruptive state.
  92. */
  93. list_add_rcu(&mod->bug_list, &module_bug_list);
  94. }
  95. void module_bug_cleanup(struct module *mod)
  96. {
  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. void bug_get_file_line(struct bug_entry *bug, const char **file,
  106. unsigned int *line)
  107. {
  108. #ifdef CONFIG_DEBUG_BUGVERBOSE
  109. #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  110. *file = (const char *)&bug->file_disp + bug->file_disp;
  111. #else
  112. *file = bug->file;
  113. #endif
  114. *line = bug->line;
  115. #else
  116. *file = NULL;
  117. *line = 0;
  118. #endif
  119. }
  120. struct bug_entry *find_bug(unsigned long bugaddr)
  121. {
  122. struct bug_entry *bug;
  123. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  124. if (bugaddr == bug_addr(bug))
  125. return bug;
  126. return module_find_bug(bugaddr);
  127. }
  128. static enum bug_trap_type __report_bug(unsigned long bugaddr, struct pt_regs *regs)
  129. {
  130. struct bug_entry *bug;
  131. const char *file;
  132. unsigned line, warning, once, done;
  133. if (!is_valid_bugaddr(bugaddr))
  134. return BUG_TRAP_TYPE_NONE;
  135. bug = find_bug(bugaddr);
  136. if (!bug)
  137. return BUG_TRAP_TYPE_NONE;
  138. disable_trace_on_warning();
  139. bug_get_file_line(bug, &file, &line);
  140. warning = (bug->flags & BUGFLAG_WARNING) != 0;
  141. once = (bug->flags & BUGFLAG_ONCE) != 0;
  142. done = (bug->flags & BUGFLAG_DONE) != 0;
  143. if (warning && once) {
  144. if (done)
  145. return BUG_TRAP_TYPE_WARN;
  146. /*
  147. * Since this is the only store, concurrency is not an issue.
  148. */
  149. bug->flags |= BUGFLAG_DONE;
  150. }
  151. /*
  152. * BUG() and WARN_ON() families don't print a custom debug message
  153. * before triggering the exception handler, so we must add the
  154. * "cut here" line now. WARN() issues its own "cut here" before the
  155. * extra debugging message it writes before triggering the handler.
  156. */
  157. if ((bug->flags & BUGFLAG_NO_CUT_HERE) == 0)
  158. printk(KERN_DEFAULT CUT_HERE);
  159. if (warning) {
  160. /* this is a WARN_ON rather than BUG/BUG_ON */
  161. __warn(file, line, (void *)bugaddr, BUG_GET_TAINT(bug), regs,
  162. NULL);
  163. return BUG_TRAP_TYPE_WARN;
  164. }
  165. if (file)
  166. pr_crit("kernel BUG at %s:%u!\n", file, line);
  167. else
  168. pr_crit("Kernel BUG at %pB [verbose debug info unavailable]\n",
  169. (void *)bugaddr);
  170. trace_android_rvh_report_bug(file, line, bugaddr);
  171. return BUG_TRAP_TYPE_BUG;
  172. }
  173. enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
  174. {
  175. enum bug_trap_type ret;
  176. bool rcu = false;
  177. rcu = warn_rcu_enter();
  178. ret = __report_bug(bugaddr, regs);
  179. warn_rcu_exit(rcu);
  180. return ret;
  181. }
  182. static void clear_once_table(struct bug_entry *start, struct bug_entry *end)
  183. {
  184. struct bug_entry *bug;
  185. for (bug = start; bug < end; bug++)
  186. bug->flags &= ~BUGFLAG_DONE;
  187. }
  188. void generic_bug_clear_once(void)
  189. {
  190. #ifdef CONFIG_MODULES
  191. struct module *mod;
  192. rcu_read_lock_sched();
  193. list_for_each_entry_rcu(mod, &module_bug_list, bug_list)
  194. clear_once_table(mod->bug_table,
  195. mod->bug_table + mod->num_bugs);
  196. rcu_read_unlock_sched();
  197. #endif
  198. clear_once_table(__start___bug_table, __stop___bug_table);
  199. }