1
2
3
4
5
6
7
8
9
10
11#include <linux/debug_locks.h>
12#include <linux/interrupt.h>
13#include <linux/kmsg_dump.h>
14#include <linux/kallsyms.h>
15#include <linux/notifier.h>
16#include <linux/module.h>
17#include <linux/random.h>
18#include <linux/ftrace.h>
19#include <linux/reboot.h>
20#include <linux/delay.h>
21#include <linux/kexec.h>
22#include <linux/sched.h>
23#include <linux/sysrq.h>
24#include <linux/init.h>
25#include <linux/nmi.h>
26
27#define PANIC_TIMER_STEP 100
28#define PANIC_BLINK_SPD 18
29
30int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
31static unsigned long tainted_mask;
32static int pause_on_oops;
33static int pause_on_oops_flag;
34static DEFINE_SPINLOCK(pause_on_oops_lock);
35static bool crash_kexec_post_notifiers;
36int panic_on_warn __read_mostly;
37
38int panic_timeout = CONFIG_PANIC_TIMEOUT;
39EXPORT_SYMBOL_GPL(panic_timeout);
40
41ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
42
43EXPORT_SYMBOL(panic_notifier_list);
44
45static long no_blink(int state)
46{
47 return 0;
48}
49
50
51long (*panic_blink)(int state);
52EXPORT_SYMBOL(panic_blink);
53
54
55
56
57void __weak panic_smp_self_stop(void)
58{
59 while (1)
60 cpu_relax();
61}
62
63
64
65
66
67
68
69
70
71void panic(const char *fmt, ...)
72{
73 static DEFINE_SPINLOCK(panic_lock);
74 static char buf[1024];
75 va_list args;
76 long i, i_next = 0;
77 int state = 0;
78
79
80
81
82
83
84
85 local_irq_disable();
86
87
88
89
90
91
92
93
94
95
96
97 if (!spin_trylock(&panic_lock))
98 panic_smp_self_stop();
99
100 console_verbose();
101 bust_spinlocks(1);
102 va_start(args, fmt);
103 vsnprintf(buf, sizeof(buf), fmt, args);
104 va_end(args);
105 pr_emerg("Kernel panic - not syncing: %s\n", buf);
106#ifdef CONFIG_DEBUG_BUGVERBOSE
107
108
109
110 if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
111 dump_stack();
112#endif
113
114
115
116
117
118
119
120 if (!crash_kexec_post_notifiers)
121 crash_kexec(NULL);
122
123
124
125
126
127
128 smp_send_stop();
129
130
131
132
133
134 atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
135
136 kmsg_dump(KMSG_DUMP_PANIC);
137
138
139
140
141
142
143
144
145 crash_kexec(NULL);
146
147 bust_spinlocks(0);
148
149 if (!panic_blink)
150 panic_blink = no_blink;
151
152 if (panic_timeout > 0) {
153
154
155
156
157 pr_emerg("Rebooting in %d seconds..", panic_timeout);
158
159 for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
160 touch_nmi_watchdog();
161 if (i >= i_next) {
162 i += panic_blink(state ^= 1);
163 i_next = i + 3600 / PANIC_BLINK_SPD;
164 }
165 mdelay(PANIC_TIMER_STEP);
166 }
167 }
168 if (panic_timeout != 0) {
169
170
171
172
173
174 emergency_restart();
175 }
176#ifdef __sparc__
177 {
178 extern int stop_a_enabled;
179
180 stop_a_enabled = 1;
181 pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
182 }
183#endif
184#if defined(CONFIG_S390)
185 {
186 unsigned long caller;
187
188 caller = (unsigned long)__builtin_return_address(0);
189 disabled_wait(caller);
190 }
191#endif
192 pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
193 local_irq_enable();
194 for (i = 0; ; i += PANIC_TIMER_STEP) {
195 touch_softlockup_watchdog();
196 if (i >= i_next) {
197 i += panic_blink(state ^= 1);
198 i_next = i + 3600 / PANIC_BLINK_SPD;
199 }
200 mdelay(PANIC_TIMER_STEP);
201 }
202}
203
204EXPORT_SYMBOL(panic);
205
206
207struct tnt {
208 u8 bit;
209 char true;
210 char false;
211};
212
213static const struct tnt tnts[] = {
214 { TAINT_PROPRIETARY_MODULE, 'P', 'G' },
215 { TAINT_FORCED_MODULE, 'F', ' ' },
216 { TAINT_CPU_OUT_OF_SPEC, 'S', ' ' },
217 { TAINT_FORCED_RMMOD, 'R', ' ' },
218 { TAINT_MACHINE_CHECK, 'M', ' ' },
219 { TAINT_BAD_PAGE, 'B', ' ' },
220 { TAINT_USER, 'U', ' ' },
221 { TAINT_DIE, 'D', ' ' },
222 { TAINT_OVERRIDDEN_ACPI_TABLE, 'A', ' ' },
223 { TAINT_WARN, 'W', ' ' },
224 { TAINT_CRAP, 'C', ' ' },
225 { TAINT_FIRMWARE_WORKAROUND, 'I', ' ' },
226 { TAINT_OOT_MODULE, 'O', ' ' },
227 { TAINT_UNSIGNED_MODULE, 'E', ' ' },
228 { TAINT_SOFTLOCKUP, 'L', ' ' },
229};
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252const char *print_tainted(void)
253{
254 static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
255
256 if (tainted_mask) {
257 char *s;
258 int i;
259
260 s = buf + sprintf(buf, "Tainted: ");
261 for (i = 0; i < ARRAY_SIZE(tnts); i++) {
262 const struct tnt *t = &tnts[i];
263 *s++ = test_bit(t->bit, &tainted_mask) ?
264 t->true : t->false;
265 }
266 *s = 0;
267 } else
268 snprintf(buf, sizeof(buf), "Not tainted");
269
270 return buf;
271}
272
273int test_taint(unsigned flag)
274{
275 return test_bit(flag, &tainted_mask);
276}
277EXPORT_SYMBOL(test_taint);
278
279unsigned long get_taint(void)
280{
281 return tainted_mask;
282}
283
284
285
286
287
288
289
290
291
292void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
293{
294 if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
295 pr_warn("Disabling lock debugging due to kernel taint\n");
296
297 set_bit(flag, &tainted_mask);
298}
299EXPORT_SYMBOL(add_taint);
300
301static void spin_msec(int msecs)
302{
303 int i;
304
305 for (i = 0; i < msecs; i++) {
306 touch_nmi_watchdog();
307 mdelay(1);
308 }
309}
310
311
312
313
314
315static void do_oops_enter_exit(void)
316{
317 unsigned long flags;
318 static int spin_counter;
319
320 if (!pause_on_oops)
321 return;
322
323 spin_lock_irqsave(&pause_on_oops_lock, flags);
324 if (pause_on_oops_flag == 0) {
325
326 pause_on_oops_flag = 1;
327 } else {
328
329 if (!spin_counter) {
330
331 spin_counter = pause_on_oops;
332 do {
333 spin_unlock(&pause_on_oops_lock);
334 spin_msec(MSEC_PER_SEC);
335 spin_lock(&pause_on_oops_lock);
336 } while (--spin_counter);
337 pause_on_oops_flag = 0;
338 } else {
339
340 while (spin_counter) {
341 spin_unlock(&pause_on_oops_lock);
342 spin_msec(1);
343 spin_lock(&pause_on_oops_lock);
344 }
345 }
346 }
347 spin_unlock_irqrestore(&pause_on_oops_lock, flags);
348}
349
350
351
352
353
354int oops_may_print(void)
355{
356 return pause_on_oops_flag == 0;
357}
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373void oops_enter(void)
374{
375 tracing_off();
376
377 debug_locks_off();
378 do_oops_enter_exit();
379}
380
381
382
383
384static u64 oops_id;
385
386static int init_oops_id(void)
387{
388 if (!oops_id)
389 get_random_bytes(&oops_id, sizeof(oops_id));
390 else
391 oops_id++;
392
393 return 0;
394}
395late_initcall(init_oops_id);
396
397void print_oops_end_marker(void)
398{
399 init_oops_id();
400 pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
401}
402
403
404
405
406
407void oops_exit(void)
408{
409 do_oops_enter_exit();
410 print_oops_end_marker();
411 kmsg_dump(KMSG_DUMP_OOPS);
412}
413
414#ifdef WANT_WARN_ON_SLOWPATH
415struct slowpath_args {
416 const char *fmt;
417 va_list args;
418};
419
420static void warn_slowpath_common(const char *file, int line, void *caller,
421 unsigned taint, struct slowpath_args *args)
422{
423 disable_trace_on_warning();
424
425 pr_warn("------------[ cut here ]------------\n");
426 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS()\n",
427 raw_smp_processor_id(), current->pid, file, line, caller);
428
429 if (args)
430 vprintk(args->fmt, args->args);
431
432 if (panic_on_warn) {
433
434
435
436
437
438
439 panic_on_warn = 0;
440 panic("panic_on_warn set ...\n");
441 }
442
443 print_modules();
444 dump_stack();
445 print_oops_end_marker();
446
447 add_taint(taint, LOCKDEP_STILL_OK);
448}
449
450void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
451{
452 struct slowpath_args args;
453
454 args.fmt = fmt;
455 va_start(args.args, fmt);
456 warn_slowpath_common(file, line, __builtin_return_address(0),
457 TAINT_WARN, &args);
458 va_end(args.args);
459}
460EXPORT_SYMBOL(warn_slowpath_fmt);
461
462void warn_slowpath_fmt_taint(const char *file, int line,
463 unsigned taint, const char *fmt, ...)
464{
465 struct slowpath_args args;
466
467 args.fmt = fmt;
468 va_start(args.args, fmt);
469 warn_slowpath_common(file, line, __builtin_return_address(0),
470 taint, &args);
471 va_end(args.args);
472}
473EXPORT_SYMBOL(warn_slowpath_fmt_taint);
474
475void warn_slowpath_null(const char *file, int line)
476{
477 warn_slowpath_common(file, line, __builtin_return_address(0),
478 TAINT_WARN, NULL);
479}
480EXPORT_SYMBOL(warn_slowpath_null);
481#endif
482
483#ifdef CONFIG_CC_STACKPROTECTOR
484
485
486
487
488
489__visible void __stack_chk_fail(void)
490{
491 panic("stack-protector: Kernel stack is corrupted in: %p\n",
492 __builtin_return_address(0));
493}
494EXPORT_SYMBOL(__stack_chk_fail);
495
496#endif
497
498core_param(panic, panic_timeout, int, 0644);
499core_param(pause_on_oops, pause_on_oops, int, 0644);
500core_param(panic_on_warn, panic_on_warn, int, 0644);
501
502static int __init setup_crash_kexec_post_notifiers(char *s)
503{
504 crash_kexec_post_notifiers = true;
505 return 0;
506}
507early_param("crash_kexec_post_notifiers", setup_crash_kexec_post_notifiers);
508
509static int __init oops_setup(char *s)
510{
511 if (!s)
512 return -EINVAL;
513 if (!strcmp(s, "panic"))
514 panic_on_oops = 1;
515 return 0;
516}
517early_param("oops", oops_setup);
518