xref: /linux/lib/bug.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
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 <jeremy@goop.org> 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>
50*5a5d7e9bSPeter 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 {
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 
module_bug_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * mod)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 
module_bug_cleanup(struct module * mod)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 
module_find_bug(unsigned long bugaddr)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 
bug_get_file_line(struct bug_entry * bug,const char ** file,unsigned int * line)13026dbc7e2SAndrew Scull void bug_get_file_line(struct bug_entry *bug, const char **file,
13126dbc7e2SAndrew Scull 		       unsigned int *line)
13226dbc7e2SAndrew Scull {
13326dbc7e2SAndrew Scull #ifdef CONFIG_DEBUG_BUGVERBOSE
13469505e3dSJosh Poimboeuf #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
13569505e3dSJosh Poimboeuf 	*file = (const char *)&bug->file_disp + bug->file_disp;
13626dbc7e2SAndrew Scull #else
13769505e3dSJosh Poimboeuf 	*file = bug->file;
13826dbc7e2SAndrew Scull #endif
13926dbc7e2SAndrew Scull 	*line = bug->line;
1405b8be5d8SAndrew Scull #else
1415b8be5d8SAndrew Scull 	*file = NULL;
1425b8be5d8SAndrew Scull 	*line = 0;
14326dbc7e2SAndrew Scull #endif
14426dbc7e2SAndrew Scull }
14526dbc7e2SAndrew Scull 
find_bug(unsigned long bugaddr)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 
__report_bug(unsigned long bugaddr,struct pt_regs * regs)157*5a5d7e9bSPeter Zijlstra static 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 
17226dbc7e2SAndrew 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 
report_bug(unsigned long bugaddr,struct pt_regs * regs)213*5a5d7e9bSPeter Zijlstra enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
214*5a5d7e9bSPeter Zijlstra {
215*5a5d7e9bSPeter Zijlstra 	enum bug_trap_type ret;
216*5a5d7e9bSPeter Zijlstra 	bool rcu = false;
217*5a5d7e9bSPeter Zijlstra 
218*5a5d7e9bSPeter Zijlstra 	rcu = warn_rcu_enter();
219*5a5d7e9bSPeter Zijlstra 	ret = __report_bug(bugaddr, regs);
220*5a5d7e9bSPeter Zijlstra 	warn_rcu_exit(rcu);
221*5a5d7e9bSPeter Zijlstra 
222*5a5d7e9bSPeter Zijlstra 	return ret;
223*5a5d7e9bSPeter Zijlstra }
224*5a5d7e9bSPeter Zijlstra 
clear_once_table(struct bug_entry * start,struct bug_entry * end)225aaf5dcfbSAndi Kleen static void clear_once_table(struct bug_entry *start, struct bug_entry *end)
226aaf5dcfbSAndi Kleen {
227aaf5dcfbSAndi Kleen 	struct bug_entry *bug;
228aaf5dcfbSAndi Kleen 
229aaf5dcfbSAndi Kleen 	for (bug = start; bug < end; bug++)
230aaf5dcfbSAndi Kleen 		bug->flags &= ~BUGFLAG_DONE;
231aaf5dcfbSAndi Kleen }
232aaf5dcfbSAndi Kleen 
generic_bug_clear_once(void)233aaf5dcfbSAndi Kleen void generic_bug_clear_once(void)
234aaf5dcfbSAndi Kleen {
235aaf5dcfbSAndi Kleen #ifdef CONFIG_MODULES
236aaf5dcfbSAndi Kleen 	struct module *mod;
237aaf5dcfbSAndi Kleen 
238aaf5dcfbSAndi Kleen 	rcu_read_lock_sched();
239aaf5dcfbSAndi Kleen 	list_for_each_entry_rcu(mod, &module_bug_list, bug_list)
240aaf5dcfbSAndi Kleen 		clear_once_table(mod->bug_table,
241aaf5dcfbSAndi Kleen 				 mod->bug_table + mod->num_bugs);
242aaf5dcfbSAndi Kleen 	rcu_read_unlock_sched();
243aaf5dcfbSAndi Kleen #endif
244aaf5dcfbSAndi Kleen 
245aaf5dcfbSAndi Kleen 	clear_once_table(__start___bug_table, __stop___bug_table);
246aaf5dcfbSAndi Kleen }
247