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. 9b93a531eSJan Beulich CONFIG_GENERIC_BUG_RELATIVE_POINTERS - use 32-bit pointers relative to 10b93a531eSJan Beulich the containing struct bug_entry for bug_addr and file. 117664c5a1SJeremy Fitzhardinge CONFIG_DEBUG_BUGVERBOSE - emit full file+line information for each BUG 127664c5a1SJeremy Fitzhardinge 137664c5a1SJeremy Fitzhardinge CONFIG_BUG and CONFIG_DEBUG_BUGVERBOSE are potentially user-settable 147664c5a1SJeremy Fitzhardinge (though they're generally always on). 157664c5a1SJeremy Fitzhardinge 167664c5a1SJeremy Fitzhardinge CONFIG_GENERIC_BUG is set by each architecture using this code. 177664c5a1SJeremy Fitzhardinge 187664c5a1SJeremy Fitzhardinge To use this, your architecture must: 197664c5a1SJeremy Fitzhardinge 207664c5a1SJeremy Fitzhardinge 1. Set up the config options: 217664c5a1SJeremy Fitzhardinge - Enable CONFIG_GENERIC_BUG if CONFIG_BUG 227664c5a1SJeremy Fitzhardinge 237664c5a1SJeremy Fitzhardinge 2. Implement BUG (and optionally BUG_ON, WARN, WARN_ON) 247664c5a1SJeremy Fitzhardinge - Define HAVE_ARCH_BUG 257664c5a1SJeremy Fitzhardinge - Implement BUG() to generate a faulting instruction 267664c5a1SJeremy Fitzhardinge - NOTE: struct bug_entry does not have "file" or "line" entries 277664c5a1SJeremy Fitzhardinge when CONFIG_DEBUG_BUGVERBOSE is not enabled, so you must generate 287664c5a1SJeremy Fitzhardinge the values accordingly. 297664c5a1SJeremy Fitzhardinge 307664c5a1SJeremy Fitzhardinge 3. Implement the trap 317664c5a1SJeremy Fitzhardinge - In the illegal instruction trap handler (typically), verify 327664c5a1SJeremy Fitzhardinge that the fault was in kernel mode, and call report_bug() 337664c5a1SJeremy Fitzhardinge - report_bug() will return whether it was a false alarm, a warning, 347664c5a1SJeremy Fitzhardinge or an actual bug. 357664c5a1SJeremy Fitzhardinge - You must implement the is_valid_bugaddr(bugaddr) callback which 367664c5a1SJeremy Fitzhardinge returns true if the eip is a real kernel address, and it points 377664c5a1SJeremy Fitzhardinge to the expected BUG trap instruction. 387664c5a1SJeremy Fitzhardinge 397664c5a1SJeremy Fitzhardinge Jeremy Fitzhardinge <jeremy@goop.org> 2006 407664c5a1SJeremy Fitzhardinge */ 41c56ba703SFabian Frederick 42c56ba703SFabian Frederick #define pr_fmt(fmt) fmt 43c56ba703SFabian Frederick 447664c5a1SJeremy Fitzhardinge #include <linux/list.h> 457664c5a1SJeremy Fitzhardinge #include <linux/module.h> 46da9eac89SPaul Mundt #include <linux/kernel.h> 477664c5a1SJeremy Fitzhardinge #include <linux/bug.h> 48608e2619SHeiko Carstens #include <linux/sched.h> 49b2d09103SIngo Molnar #include <linux/rculist.h> 5058f6e384SPeter Zijlstra #include <linux/ftrace.h> 517664c5a1SJeremy Fitzhardinge 5219d43626SPeter Zijlstra extern struct bug_entry __start___bug_table[], __stop___bug_table[]; 537664c5a1SJeremy Fitzhardinge 54b93a531eSJan Beulich static inline unsigned long bug_addr(const struct bug_entry *bug) 55b93a531eSJan Beulich { 56b93a531eSJan Beulich #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS 57b93a531eSJan Beulich return bug->bug_addr; 58b93a531eSJan Beulich #else 59b93a531eSJan Beulich return (unsigned long)bug + bug->bug_addr_disp; 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 6719d43626SPeter Zijlstra static struct bug_entry *module_find_bug(unsigned long bugaddr) 687664c5a1SJeremy Fitzhardinge { 697664c5a1SJeremy Fitzhardinge struct module *mod; 7019d43626SPeter Zijlstra struct bug_entry *bug = NULL; 717664c5a1SJeremy Fitzhardinge 720be964beSPeter Zijlstra rcu_read_lock_sched(); 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 goto out; 807664c5a1SJeremy Fitzhardinge } 810286b5eaSMasami Hiramatsu bug = NULL; 820286b5eaSMasami Hiramatsu out: 830be964beSPeter Zijlstra rcu_read_unlock_sched(); 840286b5eaSMasami Hiramatsu 850286b5eaSMasami Hiramatsu return bug; 867664c5a1SJeremy Fitzhardinge } 877664c5a1SJeremy Fitzhardinge 885336377dSLinus Torvalds void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, 897664c5a1SJeremy Fitzhardinge struct module *mod) 907664c5a1SJeremy Fitzhardinge { 917664c5a1SJeremy Fitzhardinge char *secstrings; 927664c5a1SJeremy Fitzhardinge unsigned int i; 937664c5a1SJeremy Fitzhardinge 947664c5a1SJeremy Fitzhardinge mod->bug_table = NULL; 957664c5a1SJeremy Fitzhardinge mod->num_bugs = 0; 967664c5a1SJeremy Fitzhardinge 977664c5a1SJeremy Fitzhardinge /* Find the __bug_table section, if present */ 987664c5a1SJeremy Fitzhardinge secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 997664c5a1SJeremy Fitzhardinge for (i = 1; i < hdr->e_shnum; i++) { 1007664c5a1SJeremy Fitzhardinge if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table")) 1017664c5a1SJeremy Fitzhardinge continue; 1027664c5a1SJeremy Fitzhardinge mod->bug_table = (void *) sechdrs[i].sh_addr; 1037664c5a1SJeremy Fitzhardinge mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry); 1047664c5a1SJeremy Fitzhardinge break; 1057664c5a1SJeremy Fitzhardinge } 1067664c5a1SJeremy Fitzhardinge 1077664c5a1SJeremy Fitzhardinge /* 1087664c5a1SJeremy Fitzhardinge * Strictly speaking this should have a spinlock to protect against 1097664c5a1SJeremy Fitzhardinge * traversals, but since we only traverse on BUG()s, a spinlock 1107664c5a1SJeremy Fitzhardinge * could potentially lead to deadlock and thus be counter-productive. 1110286b5eaSMasami Hiramatsu * Thus, this uses RCU to safely manipulate the bug list, since BUG 1120286b5eaSMasami Hiramatsu * must run in non-interruptive state. 1137664c5a1SJeremy Fitzhardinge */ 1140286b5eaSMasami Hiramatsu list_add_rcu(&mod->bug_list, &module_bug_list); 1157664c5a1SJeremy Fitzhardinge } 1167664c5a1SJeremy Fitzhardinge 1177664c5a1SJeremy Fitzhardinge void module_bug_cleanup(struct module *mod) 1187664c5a1SJeremy Fitzhardinge { 1190286b5eaSMasami Hiramatsu list_del_rcu(&mod->bug_list); 1207664c5a1SJeremy Fitzhardinge } 1217664c5a1SJeremy Fitzhardinge 1227664c5a1SJeremy Fitzhardinge #else 1237664c5a1SJeremy Fitzhardinge 12419d43626SPeter Zijlstra static inline struct bug_entry *module_find_bug(unsigned long bugaddr) 1257664c5a1SJeremy Fitzhardinge { 1267664c5a1SJeremy Fitzhardinge return NULL; 1277664c5a1SJeremy Fitzhardinge } 1287664c5a1SJeremy Fitzhardinge #endif 1297664c5a1SJeremy Fitzhardinge 130*26dbc7e2SAndrew Scull void bug_get_file_line(struct bug_entry *bug, const char **file, 131*26dbc7e2SAndrew Scull unsigned int *line) 132*26dbc7e2SAndrew Scull { 133*26dbc7e2SAndrew Scull *file = NULL; 134*26dbc7e2SAndrew Scull *line = 0; 135*26dbc7e2SAndrew Scull 136*26dbc7e2SAndrew Scull #ifdef CONFIG_DEBUG_BUGVERBOSE 137*26dbc7e2SAndrew Scull #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS 138*26dbc7e2SAndrew Scull *file = bug->file; 139*26dbc7e2SAndrew Scull #else 140*26dbc7e2SAndrew Scull *file = (const char *)bug + bug->file_disp; 141*26dbc7e2SAndrew Scull #endif 142*26dbc7e2SAndrew Scull *line = bug->line; 143*26dbc7e2SAndrew Scull #endif 144*26dbc7e2SAndrew Scull } 145*26dbc7e2SAndrew Scull 14619d43626SPeter Zijlstra struct bug_entry *find_bug(unsigned long bugaddr) 1477664c5a1SJeremy Fitzhardinge { 14819d43626SPeter Zijlstra struct bug_entry *bug; 1497664c5a1SJeremy Fitzhardinge 1507664c5a1SJeremy Fitzhardinge for (bug = __start___bug_table; bug < __stop___bug_table; ++bug) 151b93a531eSJan Beulich if (bugaddr == bug_addr(bug)) 1527664c5a1SJeremy Fitzhardinge return bug; 1537664c5a1SJeremy Fitzhardinge 1547664c5a1SJeremy Fitzhardinge return module_find_bug(bugaddr); 1557664c5a1SJeremy Fitzhardinge } 1567664c5a1SJeremy Fitzhardinge 157608e2619SHeiko Carstens enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs) 1587664c5a1SJeremy Fitzhardinge { 15919d43626SPeter Zijlstra struct bug_entry *bug; 1607664c5a1SJeremy Fitzhardinge const char *file; 16119d43626SPeter Zijlstra unsigned line, warning, once, done; 1627664c5a1SJeremy Fitzhardinge 1637664c5a1SJeremy Fitzhardinge if (!is_valid_bugaddr(bugaddr)) 1647664c5a1SJeremy Fitzhardinge return BUG_TRAP_TYPE_NONE; 1657664c5a1SJeremy Fitzhardinge 1667664c5a1SJeremy Fitzhardinge bug = find_bug(bugaddr); 1671b4cfe3cSKees Cook if (!bug) 1681b4cfe3cSKees Cook return BUG_TRAP_TYPE_NONE; 1697664c5a1SJeremy Fitzhardinge 17058f6e384SPeter Zijlstra disable_trace_on_warning(); 17158f6e384SPeter Zijlstra 172*26dbc7e2SAndrew Scull bug_get_file_line(bug, &file, &line); 1737664c5a1SJeremy Fitzhardinge 1747664c5a1SJeremy Fitzhardinge warning = (bug->flags & BUGFLAG_WARNING) != 0; 17519d43626SPeter Zijlstra once = (bug->flags & BUGFLAG_ONCE) != 0; 17619d43626SPeter Zijlstra done = (bug->flags & BUGFLAG_DONE) != 0; 17719d43626SPeter Zijlstra 17819d43626SPeter Zijlstra if (warning && once) { 17919d43626SPeter Zijlstra if (done) 18019d43626SPeter Zijlstra return BUG_TRAP_TYPE_WARN; 18119d43626SPeter Zijlstra 18219d43626SPeter Zijlstra /* 18319d43626SPeter Zijlstra * Since this is the only store, concurrency is not an issue. 18419d43626SPeter Zijlstra */ 18519d43626SPeter Zijlstra bug->flags |= BUGFLAG_DONE; 18619d43626SPeter Zijlstra } 1877664c5a1SJeremy Fitzhardinge 188a44f71a9SKees Cook /* 189a44f71a9SKees Cook * BUG() and WARN_ON() families don't print a custom debug message 190a44f71a9SKees Cook * before triggering the exception handler, so we must add the 191a44f71a9SKees Cook * "cut here" line now. WARN() issues its own "cut here" before the 192a44f71a9SKees Cook * extra debugging message it writes before triggering the handler. 193a44f71a9SKees Cook */ 194a44f71a9SKees Cook if ((bug->flags & BUGFLAG_NO_CUT_HERE) == 0) 195a44f71a9SKees Cook printk(KERN_DEFAULT CUT_HERE); 196a44f71a9SKees Cook 1977664c5a1SJeremy Fitzhardinge if (warning) { 1987664c5a1SJeremy Fitzhardinge /* this is a WARN_ON rather than BUG/BUG_ON */ 1992553b67aSJosh Poimboeuf __warn(file, line, (void *)bugaddr, BUG_GET_TAINT(bug), regs, 2002553b67aSJosh Poimboeuf NULL); 2017664c5a1SJeremy Fitzhardinge return BUG_TRAP_TYPE_WARN; 2027664c5a1SJeremy Fitzhardinge } 2037664c5a1SJeremy Fitzhardinge 2047664c5a1SJeremy Fitzhardinge if (file) 205c56ba703SFabian Frederick pr_crit("kernel BUG at %s:%u!\n", file, line); 2067664c5a1SJeremy Fitzhardinge else 2070862ca42SKees Cook pr_crit("Kernel BUG at %pB [verbose debug info unavailable]\n", 2087664c5a1SJeremy Fitzhardinge (void *)bugaddr); 2097664c5a1SJeremy Fitzhardinge 2107664c5a1SJeremy Fitzhardinge return BUG_TRAP_TYPE_BUG; 2117664c5a1SJeremy Fitzhardinge } 212aaf5dcfbSAndi Kleen 213aaf5dcfbSAndi Kleen static void clear_once_table(struct bug_entry *start, struct bug_entry *end) 214aaf5dcfbSAndi Kleen { 215aaf5dcfbSAndi Kleen struct bug_entry *bug; 216aaf5dcfbSAndi Kleen 217aaf5dcfbSAndi Kleen for (bug = start; bug < end; bug++) 218aaf5dcfbSAndi Kleen bug->flags &= ~BUGFLAG_DONE; 219aaf5dcfbSAndi Kleen } 220aaf5dcfbSAndi Kleen 221aaf5dcfbSAndi Kleen void generic_bug_clear_once(void) 222aaf5dcfbSAndi Kleen { 223aaf5dcfbSAndi Kleen #ifdef CONFIG_MODULES 224aaf5dcfbSAndi Kleen struct module *mod; 225aaf5dcfbSAndi Kleen 226aaf5dcfbSAndi Kleen rcu_read_lock_sched(); 227aaf5dcfbSAndi Kleen list_for_each_entry_rcu(mod, &module_bug_list, bug_list) 228aaf5dcfbSAndi Kleen clear_once_table(mod->bug_table, 229aaf5dcfbSAndi Kleen mod->bug_table + mod->num_bugs); 230aaf5dcfbSAndi Kleen rcu_read_unlock_sched(); 231aaf5dcfbSAndi Kleen #endif 232aaf5dcfbSAndi Kleen 233aaf5dcfbSAndi Kleen clear_once_table(__start___bug_table, __stop___bug_table); 234aaf5dcfbSAndi Kleen } 235