1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun Generic support for BUG()
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun This respects the following config options:
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun CONFIG_BUG - emit BUG traps. Nothing happens without this.
8*4882a593Smuzhiyun CONFIG_GENERIC_BUG - enable this code.
9*4882a593Smuzhiyun CONFIG_GENERIC_BUG_RELATIVE_POINTERS - use 32-bit pointers relative to
10*4882a593Smuzhiyun the containing struct bug_entry for bug_addr and file.
11*4882a593Smuzhiyun CONFIG_DEBUG_BUGVERBOSE - emit full file+line information for each BUG
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun CONFIG_BUG and CONFIG_DEBUG_BUGVERBOSE are potentially user-settable
14*4882a593Smuzhiyun (though they're generally always on).
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun CONFIG_GENERIC_BUG is set by each architecture using this code.
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun To use this, your architecture must:
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun 1. Set up the config options:
21*4882a593Smuzhiyun - Enable CONFIG_GENERIC_BUG if CONFIG_BUG
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun 2. Implement BUG (and optionally BUG_ON, WARN, WARN_ON)
24*4882a593Smuzhiyun - Define HAVE_ARCH_BUG
25*4882a593Smuzhiyun - Implement BUG() to generate a faulting instruction
26*4882a593Smuzhiyun - NOTE: struct bug_entry does not have "file" or "line" entries
27*4882a593Smuzhiyun when CONFIG_DEBUG_BUGVERBOSE is not enabled, so you must generate
28*4882a593Smuzhiyun the values accordingly.
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun 3. Implement the trap
31*4882a593Smuzhiyun - In the illegal instruction trap handler (typically), verify
32*4882a593Smuzhiyun that the fault was in kernel mode, and call report_bug()
33*4882a593Smuzhiyun - report_bug() will return whether it was a false alarm, a warning,
34*4882a593Smuzhiyun or an actual bug.
35*4882a593Smuzhiyun - You must implement the is_valid_bugaddr(bugaddr) callback which
36*4882a593Smuzhiyun returns true if the eip is a real kernel address, and it points
37*4882a593Smuzhiyun to the expected BUG trap instruction.
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun Jeremy Fitzhardinge <jeremy@goop.org> 2006
40*4882a593Smuzhiyun */
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define pr_fmt(fmt) fmt
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #include <linux/list.h>
45*4882a593Smuzhiyun #include <linux/module.h>
46*4882a593Smuzhiyun #include <linux/kernel.h>
47*4882a593Smuzhiyun #include <linux/bug.h>
48*4882a593Smuzhiyun #include <linux/sched.h>
49*4882a593Smuzhiyun #include <linux/rculist.h>
50*4882a593Smuzhiyun #include <linux/ftrace.h>
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #include <trace/hooks/bug.h>
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun extern struct bug_entry __start___bug_table[], __stop___bug_table[];
55*4882a593Smuzhiyun
bug_addr(const struct bug_entry * bug)56*4882a593Smuzhiyun static inline unsigned long bug_addr(const struct bug_entry *bug)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
59*4882a593Smuzhiyun return bug->bug_addr;
60*4882a593Smuzhiyun #else
61*4882a593Smuzhiyun return (unsigned long)bug + bug->bug_addr_disp;
62*4882a593Smuzhiyun #endif
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun #ifdef CONFIG_MODULES
66*4882a593Smuzhiyun /* Updates are protected by module mutex */
67*4882a593Smuzhiyun static LIST_HEAD(module_bug_list);
68*4882a593Smuzhiyun
module_find_bug(unsigned long bugaddr)69*4882a593Smuzhiyun static struct bug_entry *module_find_bug(unsigned long bugaddr)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun struct module *mod;
72*4882a593Smuzhiyun struct bug_entry *bug = NULL;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun rcu_read_lock_sched();
75*4882a593Smuzhiyun list_for_each_entry_rcu(mod, &module_bug_list, bug_list) {
76*4882a593Smuzhiyun unsigned i;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun bug = mod->bug_table;
79*4882a593Smuzhiyun for (i = 0; i < mod->num_bugs; ++i, ++bug)
80*4882a593Smuzhiyun if (bugaddr == bug_addr(bug))
81*4882a593Smuzhiyun goto out;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun bug = NULL;
84*4882a593Smuzhiyun out:
85*4882a593Smuzhiyun rcu_read_unlock_sched();
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun return bug;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
module_bug_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * mod)90*4882a593Smuzhiyun void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
91*4882a593Smuzhiyun struct module *mod)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun char *secstrings;
94*4882a593Smuzhiyun unsigned int i;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun lockdep_assert_held(&module_mutex);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun mod->bug_table = NULL;
99*4882a593Smuzhiyun mod->num_bugs = 0;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* Find the __bug_table section, if present */
102*4882a593Smuzhiyun secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
103*4882a593Smuzhiyun for (i = 1; i < hdr->e_shnum; i++) {
104*4882a593Smuzhiyun if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
105*4882a593Smuzhiyun continue;
106*4882a593Smuzhiyun mod->bug_table = (void *) sechdrs[i].sh_addr;
107*4882a593Smuzhiyun mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
108*4882a593Smuzhiyun break;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun * Strictly speaking this should have a spinlock to protect against
113*4882a593Smuzhiyun * traversals, but since we only traverse on BUG()s, a spinlock
114*4882a593Smuzhiyun * could potentially lead to deadlock and thus be counter-productive.
115*4882a593Smuzhiyun * Thus, this uses RCU to safely manipulate the bug list, since BUG
116*4882a593Smuzhiyun * must run in non-interruptive state.
117*4882a593Smuzhiyun */
118*4882a593Smuzhiyun list_add_rcu(&mod->bug_list, &module_bug_list);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
module_bug_cleanup(struct module * mod)121*4882a593Smuzhiyun void module_bug_cleanup(struct module *mod)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun lockdep_assert_held(&module_mutex);
124*4882a593Smuzhiyun list_del_rcu(&mod->bug_list);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun #else
128*4882a593Smuzhiyun
module_find_bug(unsigned long bugaddr)129*4882a593Smuzhiyun static inline struct bug_entry *module_find_bug(unsigned long bugaddr)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun return NULL;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun #endif
134*4882a593Smuzhiyun
find_bug(unsigned long bugaddr)135*4882a593Smuzhiyun struct bug_entry *find_bug(unsigned long bugaddr)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun struct bug_entry *bug;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
140*4882a593Smuzhiyun if (bugaddr == bug_addr(bug))
141*4882a593Smuzhiyun return bug;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun return module_find_bug(bugaddr);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
report_bug(unsigned long bugaddr,struct pt_regs * regs)146*4882a593Smuzhiyun enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun struct bug_entry *bug;
149*4882a593Smuzhiyun const char *file;
150*4882a593Smuzhiyun unsigned line, warning, once, done;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun if (!is_valid_bugaddr(bugaddr))
153*4882a593Smuzhiyun return BUG_TRAP_TYPE_NONE;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun bug = find_bug(bugaddr);
156*4882a593Smuzhiyun if (!bug)
157*4882a593Smuzhiyun return BUG_TRAP_TYPE_NONE;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun disable_trace_on_warning();
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun file = NULL;
162*4882a593Smuzhiyun line = 0;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_BUGVERBOSE
165*4882a593Smuzhiyun #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
166*4882a593Smuzhiyun file = bug->file;
167*4882a593Smuzhiyun #else
168*4882a593Smuzhiyun file = (const char *)bug + bug->file_disp;
169*4882a593Smuzhiyun #endif
170*4882a593Smuzhiyun line = bug->line;
171*4882a593Smuzhiyun #endif
172*4882a593Smuzhiyun warning = (bug->flags & BUGFLAG_WARNING) != 0;
173*4882a593Smuzhiyun once = (bug->flags & BUGFLAG_ONCE) != 0;
174*4882a593Smuzhiyun done = (bug->flags & BUGFLAG_DONE) != 0;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun if (warning && once) {
177*4882a593Smuzhiyun if (done)
178*4882a593Smuzhiyun return BUG_TRAP_TYPE_WARN;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun * Since this is the only store, concurrency is not an issue.
182*4882a593Smuzhiyun */
183*4882a593Smuzhiyun bug->flags |= BUGFLAG_DONE;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun * BUG() and WARN_ON() families don't print a custom debug message
188*4882a593Smuzhiyun * before triggering the exception handler, so we must add the
189*4882a593Smuzhiyun * "cut here" line now. WARN() issues its own "cut here" before the
190*4882a593Smuzhiyun * extra debugging message it writes before triggering the handler.
191*4882a593Smuzhiyun */
192*4882a593Smuzhiyun if ((bug->flags & BUGFLAG_NO_CUT_HERE) == 0)
193*4882a593Smuzhiyun printk(KERN_DEFAULT CUT_HERE);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun if (warning) {
196*4882a593Smuzhiyun /* this is a WARN_ON rather than BUG/BUG_ON */
197*4882a593Smuzhiyun __warn(file, line, (void *)bugaddr, BUG_GET_TAINT(bug), regs,
198*4882a593Smuzhiyun NULL);
199*4882a593Smuzhiyun return BUG_TRAP_TYPE_WARN;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun if (file)
203*4882a593Smuzhiyun pr_crit("kernel BUG at %s:%u!\n", file, line);
204*4882a593Smuzhiyun else
205*4882a593Smuzhiyun pr_crit("Kernel BUG at %pB [verbose debug info unavailable]\n",
206*4882a593Smuzhiyun (void *)bugaddr);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun trace_android_rvh_report_bug(file, line, bugaddr);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun return BUG_TRAP_TYPE_BUG;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
clear_once_table(struct bug_entry * start,struct bug_entry * end)213*4882a593Smuzhiyun static void clear_once_table(struct bug_entry *start, struct bug_entry *end)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun struct bug_entry *bug;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun for (bug = start; bug < end; bug++)
218*4882a593Smuzhiyun bug->flags &= ~BUGFLAG_DONE;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
generic_bug_clear_once(void)221*4882a593Smuzhiyun void generic_bug_clear_once(void)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun #ifdef CONFIG_MODULES
224*4882a593Smuzhiyun struct module *mod;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun rcu_read_lock_sched();
227*4882a593Smuzhiyun list_for_each_entry_rcu(mod, &module_bug_list, bug_list)
228*4882a593Smuzhiyun clear_once_table(mod->bug_table,
229*4882a593Smuzhiyun mod->bug_table + mod->num_bugs);
230*4882a593Smuzhiyun rcu_read_unlock_sched();
231*4882a593Smuzhiyun #endif
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun clear_once_table(__start___bug_table, __stop___bug_table);
234*4882a593Smuzhiyun }
235