1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
27664c5a1SJeremy Fitzhardinge /*
37664c5a1SJeremy Fitzhardinge Generic support for BUG()
47664c5a1SJeremy Fitzhardinge
57664c5a1SJeremy Fitzhardinge This respects the following config options:
67664c5a1SJeremy Fitzhardinge
77664c5a1SJeremy Fitzhardinge CONFIG_BUG - emit BUG traps. Nothing happens without this.
87664c5a1SJeremy Fitzhardinge CONFIG_GENERIC_BUG - enable this code.
969505e3dSJosh Poimboeuf CONFIG_GENERIC_BUG_RELATIVE_POINTERS - use 32-bit relative pointers for bug_addr and file
107664c5a1SJeremy Fitzhardinge CONFIG_DEBUG_BUGVERBOSE - emit full file+line information for each BUG
117664c5a1SJeremy Fitzhardinge
127664c5a1SJeremy Fitzhardinge CONFIG_BUG and CONFIG_DEBUG_BUGVERBOSE are potentially user-settable
137664c5a1SJeremy Fitzhardinge (though they're generally always on).
147664c5a1SJeremy Fitzhardinge
157664c5a1SJeremy Fitzhardinge CONFIG_GENERIC_BUG is set by each architecture using this code.
167664c5a1SJeremy Fitzhardinge
177664c5a1SJeremy Fitzhardinge To use this, your architecture must:
187664c5a1SJeremy Fitzhardinge
197664c5a1SJeremy Fitzhardinge 1. Set up the config options:
207664c5a1SJeremy Fitzhardinge - Enable CONFIG_GENERIC_BUG if CONFIG_BUG
217664c5a1SJeremy Fitzhardinge
227664c5a1SJeremy Fitzhardinge 2. Implement BUG (and optionally BUG_ON, WARN, WARN_ON)
237664c5a1SJeremy Fitzhardinge - Define HAVE_ARCH_BUG
247664c5a1SJeremy Fitzhardinge - Implement BUG() to generate a faulting instruction
257664c5a1SJeremy Fitzhardinge - NOTE: struct bug_entry does not have "file" or "line" entries
267664c5a1SJeremy Fitzhardinge when CONFIG_DEBUG_BUGVERBOSE is not enabled, so you must generate
277664c5a1SJeremy Fitzhardinge the values accordingly.
287664c5a1SJeremy Fitzhardinge
297664c5a1SJeremy Fitzhardinge 3. Implement the trap
307664c5a1SJeremy Fitzhardinge - In the illegal instruction trap handler (typically), verify
317664c5a1SJeremy Fitzhardinge that the fault was in kernel mode, and call report_bug()
327664c5a1SJeremy Fitzhardinge - report_bug() will return whether it was a false alarm, a warning,
337664c5a1SJeremy Fitzhardinge or an actual bug.
347664c5a1SJeremy Fitzhardinge - You must implement the is_valid_bugaddr(bugaddr) callback which
357664c5a1SJeremy Fitzhardinge returns true if the eip is a real kernel address, and it points
367664c5a1SJeremy Fitzhardinge to the expected BUG trap instruction.
377664c5a1SJeremy Fitzhardinge
387664c5a1SJeremy Fitzhardinge Jeremy Fitzhardinge <[email protected]> 2006
397664c5a1SJeremy Fitzhardinge */
40c56ba703SFabian Frederick
41c56ba703SFabian Frederick #define pr_fmt(fmt) fmt
42c56ba703SFabian Frederick
437664c5a1SJeremy Fitzhardinge #include <linux/list.h>
447664c5a1SJeremy Fitzhardinge #include <linux/module.h>
45da9eac89SPaul Mundt #include <linux/kernel.h>
467664c5a1SJeremy Fitzhardinge #include <linux/bug.h>
47608e2619SHeiko Carstens #include <linux/sched.h>
48b2d09103SIngo Molnar #include <linux/rculist.h>
4958f6e384SPeter Zijlstra #include <linux/ftrace.h>
505a5d7e9bSPeter Zijlstra #include <linux/context_tracking.h>
517664c5a1SJeremy Fitzhardinge
5219d43626SPeter Zijlstra extern struct bug_entry __start___bug_table[], __stop___bug_table[];
537664c5a1SJeremy Fitzhardinge
bug_addr(const struct bug_entry * bug)54b93a531eSJan Beulich static inline unsigned long bug_addr(const struct bug_entry *bug)
55b93a531eSJan Beulich {
5669505e3dSJosh Poimboeuf #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
5769505e3dSJosh Poimboeuf return (unsigned long)&bug->bug_addr_disp + bug->bug_addr_disp;
58b93a531eSJan Beulich #else
5969505e3dSJosh Poimboeuf return bug->bug_addr;
60b93a531eSJan Beulich #endif
61b93a531eSJan Beulich }
62b93a531eSJan Beulich
637664c5a1SJeremy Fitzhardinge #ifdef CONFIG_MODULES
641fb9341aSRusty Russell /* Updates are protected by module mutex */
657664c5a1SJeremy Fitzhardinge static LIST_HEAD(module_bug_list);
667664c5a1SJeremy Fitzhardinge
module_find_bug(unsigned long bugaddr)6719d43626SPeter Zijlstra static struct bug_entry *module_find_bug(unsigned long bugaddr)
687664c5a1SJeremy Fitzhardinge {
69*f47d2a3fSSebastian Andrzej Siewior struct bug_entry *bug;
707664c5a1SJeremy Fitzhardinge struct module *mod;
717664c5a1SJeremy Fitzhardinge
72*f47d2a3fSSebastian Andrzej Siewior guard(rcu)();
730286b5eaSMasami Hiramatsu list_for_each_entry_rcu(mod, &module_bug_list, bug_list) {
747664c5a1SJeremy Fitzhardinge unsigned i;
757664c5a1SJeremy Fitzhardinge
760286b5eaSMasami Hiramatsu bug = mod->bug_table;
777664c5a1SJeremy Fitzhardinge for (i = 0; i < mod->num_bugs; ++i, ++bug)
78b93a531eSJan Beulich if (bugaddr == bug_addr(bug))
790286b5eaSMasami Hiramatsu return bug;
807664c5a1SJeremy Fitzhardinge }
81*f47d2a3fSSebastian Andrzej Siewior return NULL;
82*f47d2a3fSSebastian Andrzej Siewior }
837664c5a1SJeremy Fitzhardinge
module_bug_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * mod)845336377dSLinus Torvalds void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
857664c5a1SJeremy Fitzhardinge struct module *mod)
867664c5a1SJeremy Fitzhardinge {
877664c5a1SJeremy Fitzhardinge char *secstrings;
887664c5a1SJeremy Fitzhardinge unsigned int i;
897664c5a1SJeremy Fitzhardinge
907664c5a1SJeremy Fitzhardinge mod->bug_table = NULL;
917664c5a1SJeremy Fitzhardinge mod->num_bugs = 0;
927664c5a1SJeremy Fitzhardinge
937664c5a1SJeremy Fitzhardinge /* Find the __bug_table section, if present */
947664c5a1SJeremy Fitzhardinge secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
957664c5a1SJeremy Fitzhardinge for (i = 1; i < hdr->e_shnum; i++) {
967664c5a1SJeremy Fitzhardinge if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
977664c5a1SJeremy Fitzhardinge continue;
987664c5a1SJeremy Fitzhardinge mod->bug_table = (void *) sechdrs[i].sh_addr;
997664c5a1SJeremy Fitzhardinge mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
1007664c5a1SJeremy Fitzhardinge break;
1017664c5a1SJeremy Fitzhardinge }
1027664c5a1SJeremy Fitzhardinge
1037664c5a1SJeremy Fitzhardinge /*
1047664c5a1SJeremy Fitzhardinge * Strictly speaking this should have a spinlock to protect against
1057664c5a1SJeremy Fitzhardinge * traversals, but since we only traverse on BUG()s, a spinlock
1067664c5a1SJeremy Fitzhardinge * could potentially lead to deadlock and thus be counter-productive.
1070286b5eaSMasami Hiramatsu * Thus, this uses RCU to safely manipulate the bug list, since BUG
1080286b5eaSMasami Hiramatsu * must run in non-interruptive state.
1097664c5a1SJeremy Fitzhardinge */
1100286b5eaSMasami Hiramatsu list_add_rcu(&mod->bug_list, &module_bug_list);
1117664c5a1SJeremy Fitzhardinge }
1127664c5a1SJeremy Fitzhardinge
module_bug_cleanup(struct module * mod)1137664c5a1SJeremy Fitzhardinge void module_bug_cleanup(struct module *mod)
1147664c5a1SJeremy Fitzhardinge {
1150286b5eaSMasami Hiramatsu list_del_rcu(&mod->bug_list);
1167664c5a1SJeremy Fitzhardinge }
1177664c5a1SJeremy Fitzhardinge
1187664c5a1SJeremy Fitzhardinge #else
1197664c5a1SJeremy Fitzhardinge
module_find_bug(unsigned long bugaddr)12019d43626SPeter Zijlstra static inline struct bug_entry *module_find_bug(unsigned long bugaddr)
1217664c5a1SJeremy Fitzhardinge {
1227664c5a1SJeremy Fitzhardinge return NULL;
1237664c5a1SJeremy Fitzhardinge }
1247664c5a1SJeremy Fitzhardinge #endif
1257664c5a1SJeremy Fitzhardinge
bug_get_file_line(struct bug_entry * bug,const char ** file,unsigned int * line)12626dbc7e2SAndrew Scull void bug_get_file_line(struct bug_entry *bug, const char **file,
12726dbc7e2SAndrew Scull unsigned int *line)
12826dbc7e2SAndrew Scull {
12926dbc7e2SAndrew Scull #ifdef CONFIG_DEBUG_BUGVERBOSE
13069505e3dSJosh Poimboeuf #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
13169505e3dSJosh Poimboeuf *file = (const char *)&bug->file_disp + bug->file_disp;
13226dbc7e2SAndrew Scull #else
13369505e3dSJosh Poimboeuf *file = bug->file;
13426dbc7e2SAndrew Scull #endif
13526dbc7e2SAndrew Scull *line = bug->line;
1365b8be5d8SAndrew Scull #else
1375b8be5d8SAndrew Scull *file = NULL;
1385b8be5d8SAndrew Scull *line = 0;
13926dbc7e2SAndrew Scull #endif
14026dbc7e2SAndrew Scull }
14126dbc7e2SAndrew Scull
find_bug(unsigned long bugaddr)14219d43626SPeter Zijlstra struct bug_entry *find_bug(unsigned long bugaddr)
1437664c5a1SJeremy Fitzhardinge {
14419d43626SPeter Zijlstra struct bug_entry *bug;
1457664c5a1SJeremy Fitzhardinge
1467664c5a1SJeremy Fitzhardinge for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
147b93a531eSJan Beulich if (bugaddr == bug_addr(bug))
1487664c5a1SJeremy Fitzhardinge return bug;
1497664c5a1SJeremy Fitzhardinge
1507664c5a1SJeremy Fitzhardinge return module_find_bug(bugaddr);
1517664c5a1SJeremy Fitzhardinge }
1527664c5a1SJeremy Fitzhardinge
__report_bug(unsigned long bugaddr,struct pt_regs * regs)1535a5d7e9bSPeter Zijlstra static enum bug_trap_type __report_bug(unsigned long bugaddr, struct pt_regs *regs)
1547664c5a1SJeremy Fitzhardinge {
15519d43626SPeter Zijlstra struct bug_entry *bug;
1567664c5a1SJeremy Fitzhardinge const char *file;
15719d43626SPeter Zijlstra unsigned line, warning, once, done;
1587664c5a1SJeremy Fitzhardinge
1597664c5a1SJeremy Fitzhardinge if (!is_valid_bugaddr(bugaddr))
1607664c5a1SJeremy Fitzhardinge return BUG_TRAP_TYPE_NONE;
1617664c5a1SJeremy Fitzhardinge
1627664c5a1SJeremy Fitzhardinge bug = find_bug(bugaddr);
1631b4cfe3cSKees Cook if (!bug)
1641b4cfe3cSKees Cook return BUG_TRAP_TYPE_NONE;
1657664c5a1SJeremy Fitzhardinge
16658f6e384SPeter Zijlstra disable_trace_on_warning();
16758f6e384SPeter Zijlstra
16826dbc7e2SAndrew Scull bug_get_file_line(bug, &file, &line);
1697664c5a1SJeremy Fitzhardinge
1707664c5a1SJeremy Fitzhardinge warning = (bug->flags & BUGFLAG_WARNING) != 0;
17119d43626SPeter Zijlstra once = (bug->flags & BUGFLAG_ONCE) != 0;
17219d43626SPeter Zijlstra done = (bug->flags & BUGFLAG_DONE) != 0;
17319d43626SPeter Zijlstra
17419d43626SPeter Zijlstra if (warning && once) {
17519d43626SPeter Zijlstra if (done)
17619d43626SPeter Zijlstra return BUG_TRAP_TYPE_WARN;
17719d43626SPeter Zijlstra
17819d43626SPeter Zijlstra /*
17919d43626SPeter Zijlstra * Since this is the only store, concurrency is not an issue.
18019d43626SPeter Zijlstra */
18119d43626SPeter Zijlstra bug->flags |= BUGFLAG_DONE;
18219d43626SPeter Zijlstra }
1837664c5a1SJeremy Fitzhardinge
184a44f71a9SKees Cook /*
185a44f71a9SKees Cook * BUG() and WARN_ON() families don't print a custom debug message
186a44f71a9SKees Cook * before triggering the exception handler, so we must add the
187a44f71a9SKees Cook * "cut here" line now. WARN() issues its own "cut here" before the
188a44f71a9SKees Cook * extra debugging message it writes before triggering the handler.
189a44f71a9SKees Cook */
190a44f71a9SKees Cook if ((bug->flags & BUGFLAG_NO_CUT_HERE) == 0)
191a44f71a9SKees Cook printk(KERN_DEFAULT CUT_HERE);
192a44f71a9SKees Cook
1937664c5a1SJeremy Fitzhardinge if (warning) {
1947664c5a1SJeremy Fitzhardinge /* this is a WARN_ON rather than BUG/BUG_ON */
1952553b67aSJosh Poimboeuf __warn(file, line, (void *)bugaddr, BUG_GET_TAINT(bug), regs,
1962553b67aSJosh Poimboeuf NULL);
1977664c5a1SJeremy Fitzhardinge return BUG_TRAP_TYPE_WARN;
1987664c5a1SJeremy Fitzhardinge }
1997664c5a1SJeremy Fitzhardinge
2007664c5a1SJeremy Fitzhardinge if (file)
201c56ba703SFabian Frederick pr_crit("kernel BUG at %s:%u!\n", file, line);
2027664c5a1SJeremy Fitzhardinge else
2030862ca42SKees Cook pr_crit("Kernel BUG at %pB [verbose debug info unavailable]\n",
2047664c5a1SJeremy Fitzhardinge (void *)bugaddr);
2057664c5a1SJeremy Fitzhardinge
2067664c5a1SJeremy Fitzhardinge return BUG_TRAP_TYPE_BUG;
2077664c5a1SJeremy Fitzhardinge }
208aaf5dcfbSAndi Kleen
report_bug(unsigned long bugaddr,struct pt_regs * regs)2095a5d7e9bSPeter Zijlstra enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
2105a5d7e9bSPeter Zijlstra {
2115a5d7e9bSPeter Zijlstra enum bug_trap_type ret;
2125a5d7e9bSPeter Zijlstra bool rcu = false;
2135a5d7e9bSPeter Zijlstra
2145a5d7e9bSPeter Zijlstra rcu = warn_rcu_enter();
2155a5d7e9bSPeter Zijlstra ret = __report_bug(bugaddr, regs);
2165a5d7e9bSPeter Zijlstra warn_rcu_exit(rcu);
2175a5d7e9bSPeter Zijlstra
2185a5d7e9bSPeter Zijlstra return ret;
2195a5d7e9bSPeter Zijlstra }
2205a5d7e9bSPeter Zijlstra
clear_once_table(struct bug_entry * start,struct bug_entry * end)221aaf5dcfbSAndi Kleen static void clear_once_table(struct bug_entry *start, struct bug_entry *end)
222aaf5dcfbSAndi Kleen {
223aaf5dcfbSAndi Kleen struct bug_entry *bug;
224aaf5dcfbSAndi Kleen
225aaf5dcfbSAndi Kleen for (bug = start; bug < end; bug++)
226aaf5dcfbSAndi Kleen bug->flags &= ~BUGFLAG_DONE;
227aaf5dcfbSAndi Kleen }
228aaf5dcfbSAndi Kleen
generic_bug_clear_once(void)229aaf5dcfbSAndi Kleen void generic_bug_clear_once(void)
230aaf5dcfbSAndi Kleen {
231aaf5dcfbSAndi Kleen #ifdef CONFIG_MODULES
232aaf5dcfbSAndi Kleen struct module *mod;
233aaf5dcfbSAndi Kleen
234*f47d2a3fSSebastian Andrzej Siewior scoped_guard(rcu) {
235aaf5dcfbSAndi Kleen list_for_each_entry_rcu(mod, &module_bug_list, bug_list)
236aaf5dcfbSAndi Kleen clear_once_table(mod->bug_table,
237aaf5dcfbSAndi Kleen mod->bug_table + mod->num_bugs);
238*f47d2a3fSSebastian Andrzej Siewior }
239aaf5dcfbSAndi Kleen #endif
240aaf5dcfbSAndi Kleen
241aaf5dcfbSAndi Kleen clear_once_table(__start___bug_table, __stop___bug_table);
242aaf5dcfbSAndi Kleen }
243