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#include <linux/bug.h>
27
28#define PANIC_TIMER_STEP 100
29#define PANIC_BLINK_SPD 18
30
31int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
32static unsigned long tainted_mask;
33static int pause_on_oops;
34static int pause_on_oops_flag;
35static DEFINE_SPINLOCK(pause_on_oops_lock);
36bool crash_kexec_post_notifiers;
37int panic_on_warn __read_mostly;
38
39int panic_timeout = CONFIG_PANIC_TIMEOUT;
40EXPORT_SYMBOL_GPL(panic_timeout);
41
42ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
43
44EXPORT_SYMBOL(panic_notifier_list);
45
46static long no_blink(int state)
47{
48 return 0;
49}
50
51
52long (*panic_blink)(int state);
53EXPORT_SYMBOL(panic_blink);
54
55
56
57
58void __weak panic_smp_self_stop(void)
59{
60 while (1)
61 cpu_relax();
62}
63
64
65
66
67
68void __weak nmi_panic_self_stop(struct pt_regs *regs)
69{
70 panic_smp_self_stop();
71}
72
73
74
75
76
77
78
79void __weak crash_smp_send_stop(void)
80{
81 static int cpus_stopped;
82
83
84
85
86
87 if (cpus_stopped)
88 return;
89
90
91
92
93
94
95 smp_send_stop();
96 cpus_stopped = 1;
97}
98
99atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
100
101
102
103
104
105
106
107void nmi_panic(struct pt_regs *regs, const char *msg)
108{
109 int old_cpu, cpu;
110
111 cpu = raw_smp_processor_id();
112 old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
113
114 if (old_cpu == PANIC_CPU_INVALID)
115 panic("%s", msg);
116 else if (old_cpu != cpu)
117 nmi_panic_self_stop(regs);
118}
119EXPORT_SYMBOL(nmi_panic);
120
121
122
123
124
125
126
127
128
129void panic(const char *fmt, ...)
130{
131 static char buf[1024];
132 va_list args;
133 long i, i_next = 0;
134 int state = 0;
135 int old_cpu, this_cpu;
136 bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
137
138
139
140
141
142
143
144 local_irq_disable();
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 this_cpu = raw_smp_processor_id();
162 old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
163
164 if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
165 panic_smp_self_stop();
166
167 console_verbose();
168 bust_spinlocks(1);
169 va_start(args, fmt);
170 vsnprintf(buf, sizeof(buf), fmt, args);
171 va_end(args);
172 printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf);
173#ifdef CONFIG_DEBUG_BUGVERBOSE
174
175
176
177 if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
178 dump_stack();
179#endif
180
181
182
183
184
185
186
187
188
189 if (!_crash_kexec_post_notifiers) {
190 __crash_kexec(NULL);
191
192
193
194
195
196
197 smp_send_stop();
198 } else {
199
200
201
202
203
204 crash_smp_send_stop();
205 }
206
207
208
209
210
211 atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
212
213 kmsg_dump(KMSG_DUMP_PANIC);
214
215
216
217
218
219
220
221
222
223
224 if (_crash_kexec_post_notifiers)
225 __crash_kexec(NULL);
226
227 bust_spinlocks(0);
228
229 if (!panic_blink)
230 panic_blink = no_blink;
231
232 if (panic_timeout > 0) {
233
234
235
236
237 printk(KERN_EMERG "Rebooting in %d seconds..", panic_timeout);
238
239 for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
240 touch_nmi_watchdog();
241 if (i >= i_next) {
242 i += panic_blink(state ^= 1);
243 i_next = i + 3600 / PANIC_BLINK_SPD;
244 }
245 mdelay(PANIC_TIMER_STEP);
246 }
247 }
248 if (panic_timeout != 0) {
249
250
251
252
253
254 emergency_restart();
255 }
256#ifdef __sparc__
257 {
258 extern int stop_a_enabled;
259
260 stop_a_enabled = 1;
261 printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n");
262 }
263#endif
264#if defined(CONFIG_S390)
265 {
266 unsigned long caller;
267
268 caller = (unsigned long)__builtin_return_address(0);
269 disabled_wait(caller);
270 }
271#endif
272 local_irq_enable();
273 for (i = 0; ; i += PANIC_TIMER_STEP) {
274 touch_softlockup_watchdog();
275 if (i >= i_next) {
276 i += panic_blink(state ^= 1);
277 i_next = i + 3600 / PANIC_BLINK_SPD;
278 }
279 mdelay(PANIC_TIMER_STEP);
280 }
281}
282
283EXPORT_SYMBOL(panic);
284
285
286
287
288
289const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
290 { 'P', 'G', true },
291 { 'F', ' ', true },
292 { 'S', ' ', false },
293 { 'R', ' ', false },
294 { 'M', ' ', false },
295 { 'B', ' ', false },
296 { 'U', ' ', false },
297 { 'D', ' ', false },
298 { 'A', ' ', false },
299 { 'W', ' ', false },
300 { 'C', ' ', true },
301 { 'I', ' ', false },
302 { 'O', ' ', true },
303 { 'E', ' ', true },
304 { 'L', ' ', false },
305 { 'K', ' ', true },
306 { '?', '-', false },
307 { '?', '-', false },
308 { '?', '-', false },
309 { '?', '-', false },
310 { '?', '-', false },
311 { '?', '-', false },
312 { '?', '-', false },
313 { '?', '-', false },
314 { '?', '-', false },
315 { '?', '-', false },
316 { '?', '-', false },
317 { '?', '-', false },
318 { 'H', ' ', false },
319 { 'T', ' ', true },
320};
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344const char *print_tainted(void)
345{
346 static char buf[TAINT_FLAGS_COUNT + sizeof("Tainted: ")];
347
348 if (tainted_mask) {
349 char *s;
350 int i;
351
352 s = buf + sprintf(buf, "Tainted: ");
353 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
354 const struct taint_flag *t = &taint_flags[i];
355 *s++ = test_bit(i, &tainted_mask) ?
356 t->c_true : t->c_false;
357 }
358 *s = 0;
359 } else
360 snprintf(buf, sizeof(buf), "Not tainted");
361
362 return buf;
363}
364
365int test_taint(unsigned flag)
366{
367 return test_bit(flag, &tainted_mask);
368}
369EXPORT_SYMBOL(test_taint);
370
371unsigned long get_taint(void)
372{
373 return tainted_mask;
374}
375
376
377
378
379
380
381
382
383
384void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
385{
386 if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
387 printk(KERN_WARNING
388 "Disabling lock debugging due to kernel taint\n");
389
390 set_bit(flag, &tainted_mask);
391}
392EXPORT_SYMBOL(add_taint);
393
394static void spin_msec(int msecs)
395{
396 int i;
397
398 for (i = 0; i < msecs; i++) {
399 touch_nmi_watchdog();
400 mdelay(1);
401 }
402}
403
404
405
406
407
408static void do_oops_enter_exit(void)
409{
410 unsigned long flags;
411 static int spin_counter;
412
413 if (!pause_on_oops)
414 return;
415
416 spin_lock_irqsave(&pause_on_oops_lock, flags);
417 if (pause_on_oops_flag == 0) {
418
419 pause_on_oops_flag = 1;
420 } else {
421
422 if (!spin_counter) {
423
424 spin_counter = pause_on_oops;
425 do {
426 spin_unlock(&pause_on_oops_lock);
427 spin_msec(MSEC_PER_SEC);
428 spin_lock(&pause_on_oops_lock);
429 } while (--spin_counter);
430 pause_on_oops_flag = 0;
431 } else {
432
433 while (spin_counter) {
434 spin_unlock(&pause_on_oops_lock);
435 spin_msec(1);
436 spin_lock(&pause_on_oops_lock);
437 }
438 }
439 }
440 spin_unlock_irqrestore(&pause_on_oops_lock, flags);
441}
442
443
444
445
446
447int oops_may_print(void)
448{
449 return pause_on_oops_flag == 0;
450}
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466void oops_enter(void)
467{
468 tracing_off();
469
470 debug_locks_off();
471 do_oops_enter_exit();
472}
473
474
475
476
477static u64 oops_id;
478
479static int init_oops_id(void)
480{
481 if (!oops_id)
482 get_random_bytes(&oops_id, sizeof(oops_id));
483 else
484 oops_id++;
485
486 return 0;
487}
488late_initcall(init_oops_id);
489
490void print_oops_end_marker(void)
491{
492 init_oops_id();
493 printk(KERN_WARNING "---[ end trace %016llx ]---\n",
494 (unsigned long long)oops_id);
495}
496
497
498
499
500
501void oops_exit(void)
502{
503 do_oops_enter_exit();
504 print_oops_end_marker();
505 kmsg_dump(KMSG_DUMP_OOPS);
506}
507
508struct warn_args {
509 const char *fmt;
510 va_list args;
511};
512
513void __warn(const char *file, int line, void *caller, unsigned taint,
514 struct pt_regs *regs, struct warn_args *args)
515{
516 disable_trace_on_warning();
517
518 pr_warn("------------[ cut here ]------------\n");
519
520 if (file)
521 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
522 raw_smp_processor_id(), current->pid, file, line,
523 caller);
524 else
525 pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
526 raw_smp_processor_id(), current->pid, caller);
527
528 if (args)
529 vprintk(args->fmt, args->args);
530
531 if (panic_on_warn) {
532
533
534
535
536
537
538 panic_on_warn = 0;
539 panic("panic_on_warn set ...\n");
540 }
541
542 print_modules();
543
544 if (regs)
545 show_regs(regs);
546 else
547 dump_stack();
548
549 print_oops_end_marker();
550
551
552 add_taint(taint, LOCKDEP_STILL_OK);
553}
554
555#ifdef WANT_WARN_ON_SLOWPATH
556void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
557{
558 struct warn_args args;
559
560 args.fmt = fmt;
561 va_start(args.args, fmt);
562 __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL,
563 &args);
564 va_end(args.args);
565}
566EXPORT_SYMBOL(warn_slowpath_fmt);
567
568void warn_slowpath_fmt_taint(const char *file, int line,
569 unsigned taint, const char *fmt, ...)
570{
571 struct warn_args args;
572
573 args.fmt = fmt;
574 va_start(args.args, fmt);
575 __warn(file, line, __builtin_return_address(0), taint, NULL, &args);
576 va_end(args.args);
577}
578EXPORT_SYMBOL(warn_slowpath_fmt_taint);
579
580void warn_slowpath_null(const char *file, int line)
581{
582 __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL, NULL);
583}
584EXPORT_SYMBOL(warn_slowpath_null);
585#endif
586
587#ifdef CONFIG_CC_STACKPROTECTOR
588
589
590
591
592
593void __stack_chk_fail(void)
594{
595 panic("stack-protector: Kernel stack is corrupted in: %p\n",
596 __builtin_return_address(0));
597}
598EXPORT_SYMBOL(__stack_chk_fail);
599
600#endif
601
602core_param(panic, panic_timeout, int, 0644);
603core_param(pause_on_oops, pause_on_oops, int, 0644);
604core_param(panic_on_warn, panic_on_warn, int, 0644);
605core_param(crash_kexec_post_notifiers, crash_kexec_post_notifiers, bool, 0644);
606
607static int __init oops_setup(char *s)
608{
609 if (!s)
610 return -EINVAL;
611 if (!strcmp(s, "panic"))
612 panic_on_oops = 1;
613 return 0;
614}
615early_param("oops", oops_setup);
616