Linux 3.16.66
[linux/fpc-iii.git] / kernel / panic.c
blobe06b7d22c3b74bde1eae5dc7fbfe4e01efc6fcac
1 /*
2 * linux/kernel/panic.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * This function is used through-out the kernel (including mm and fs)
9 * to indicate a major problem.
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/vt_kern.h>
17 #include <linux/module.h>
18 #include <linux/random.h>
19 #include <linux/ftrace.h>
20 #include <linux/reboot.h>
21 #include <linux/delay.h>
22 #include <linux/kexec.h>
23 #include <linux/sched.h>
24 #include <linux/sysrq.h>
25 #include <linux/init.h>
26 #include <linux/nmi.h>
27 #include <linux/console.h>
29 #define PANIC_TIMER_STEP 100
30 #define PANIC_BLINK_SPD 18
32 int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE;
33 static unsigned long tainted_mask;
34 static int pause_on_oops;
35 static int pause_on_oops_flag;
36 static DEFINE_SPINLOCK(pause_on_oops_lock);
37 static bool crash_kexec_post_notifiers;
39 int panic_timeout = CONFIG_PANIC_TIMEOUT;
40 EXPORT_SYMBOL_GPL(panic_timeout);
42 ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
44 EXPORT_SYMBOL(panic_notifier_list);
46 static long no_blink(int state)
48 return 0;
51 /* Returns how long it waited in ms */
52 long (*panic_blink)(int state);
53 EXPORT_SYMBOL(panic_blink);
56 * Stop ourself in panic -- architecture code may override this
58 void __weak panic_smp_self_stop(void)
60 while (1)
61 cpu_relax();
65 * Stop other CPUs in panic. Architecture dependent code may override this
66 * with more suitable version. For example, if the architecture supports
67 * crash dump, it should save registers of each stopped CPU and disable
68 * per-CPU features such as virtualization extensions.
70 void __weak crash_smp_send_stop(void)
72 static int cpus_stopped;
75 * This function can be called twice in panic path, but obviously
76 * we execute this only once.
78 if (cpus_stopped)
79 return;
82 * Note smp_send_stop is the usual smp shutdown function, which
83 * unfortunately means it may not be hardened to work in a panic
84 * situation.
86 smp_send_stop();
87 cpus_stopped = 1;
90 /**
91 * panic - halt the system
92 * @fmt: The text string to print
94 * Display a message, then perform cleanups.
96 * This function never returns.
98 void panic(const char *fmt, ...)
100 static DEFINE_SPINLOCK(panic_lock);
101 static char buf[1024];
102 va_list args;
103 long i, i_next = 0;
104 int state = 0;
107 * Disable local interrupts. This will prevent panic_smp_self_stop
108 * from deadlocking the first cpu that invokes the panic, since
109 * there is nothing to prevent an interrupt handler (that runs
110 * after the panic_lock is acquired) from invoking panic again.
112 local_irq_disable();
115 * It's possible to come here directly from a panic-assertion and
116 * not have preempt disabled. Some functions called from here want
117 * preempt to be disabled. No point enabling it later though...
119 * Only one CPU is allowed to execute the panic code from here. For
120 * multiple parallel invocations of panic, all other CPUs either
121 * stop themself or will wait until they are stopped by the 1st CPU
122 * with smp_send_stop().
124 if (!spin_trylock(&panic_lock))
125 panic_smp_self_stop();
127 console_verbose();
128 bust_spinlocks(1);
129 va_start(args, fmt);
130 vsnprintf(buf, sizeof(buf), fmt, args);
131 va_end(args);
132 pr_emerg("Kernel panic - not syncing: %s\n", buf);
133 #ifdef CONFIG_DEBUG_BUGVERBOSE
135 * Avoid nested stack-dumping if a panic occurs during oops processing
137 if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
138 dump_stack();
139 #endif
142 * If we have crashed and we have a crash kernel loaded let it handle
143 * everything else.
144 * If we want to run this after calling panic_notifiers, pass
145 * the "crash_kexec_post_notifiers" option to the kernel.
147 if (!crash_kexec_post_notifiers) {
148 crash_kexec(NULL);
151 * Note smp_send_stop is the usual smp shutdown function, which
152 * unfortunately means it may not be hardened to work in a
153 * panic situation.
155 smp_send_stop();
156 } else {
158 * If we want to do crash dump after notifier calls and
159 * kmsg_dump, we will need architecture dependent extra
160 * works in addition to stopping other CPUs.
162 crash_smp_send_stop();
166 * Run any panic handlers, including those that might need to
167 * add information to the kmsg dump output.
169 atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
171 kmsg_dump(KMSG_DUMP_PANIC);
174 * If you doubt kdump always works fine in any situation,
175 * "crash_kexec_post_notifiers" offers you a chance to run
176 * panic_notifiers and dumping kmsg before kdump.
177 * Note: since some panic_notifiers can make crashed kernel
178 * more unstable, it can increase risks of the kdump failure too.
180 crash_kexec(NULL);
182 #ifdef CONFIG_VT
183 unblank_screen();
184 #endif
185 console_unblank();
188 * We may have ended up stopping the CPU holding the lock (in
189 * smp_send_stop()) while still having some valuable data in the console
190 * buffer. Try to acquire the lock then release it regardless of the
191 * result. The release will also print the buffers out. Locks debug
192 * should be disabled to avoid reporting bad unlock balance when
193 * panic() is not being callled from OOPS.
195 debug_locks_off();
196 console_flush_on_panic();
198 if (!panic_blink)
199 panic_blink = no_blink;
201 if (panic_timeout > 0) {
203 * Delay timeout seconds before rebooting the machine.
204 * We can't use the "normal" timers since we just panicked.
206 pr_emerg("Rebooting in %d seconds..", panic_timeout);
208 for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
209 touch_nmi_watchdog();
210 if (i >= i_next) {
211 i += panic_blink(state ^= 1);
212 i_next = i + 3600 / PANIC_BLINK_SPD;
214 mdelay(PANIC_TIMER_STEP);
217 if (panic_timeout != 0) {
219 * This will not be a clean reboot, with everything
220 * shutting down. But if there is a chance of
221 * rebooting the system it will be rebooted.
223 emergency_restart();
225 #ifdef __sparc__
227 extern int stop_a_enabled;
228 /* Make sure the user can actually press Stop-A (L1-A) */
229 stop_a_enabled = 1;
230 pr_emerg("Press Stop-A (L1-A) to return to the boot prom\n");
232 #endif
233 #if defined(CONFIG_S390)
235 unsigned long caller;
237 caller = (unsigned long)__builtin_return_address(0);
238 disabled_wait(caller);
240 #endif
241 pr_emerg("---[ end Kernel panic - not syncing: %s\n", buf);
242 local_irq_enable();
243 for (i = 0; ; i += PANIC_TIMER_STEP) {
244 touch_softlockup_watchdog();
245 if (i >= i_next) {
246 i += panic_blink(state ^= 1);
247 i_next = i + 3600 / PANIC_BLINK_SPD;
249 mdelay(PANIC_TIMER_STEP);
253 EXPORT_SYMBOL(panic);
256 struct tnt {
257 u8 bit;
258 char true;
259 char false;
262 static const struct tnt tnts[] = {
263 { TAINT_PROPRIETARY_MODULE, 'P', 'G' },
264 { TAINT_FORCED_MODULE, 'F', ' ' },
265 { TAINT_CPU_OUT_OF_SPEC, 'S', ' ' },
266 { TAINT_FORCED_RMMOD, 'R', ' ' },
267 { TAINT_MACHINE_CHECK, 'M', ' ' },
268 { TAINT_BAD_PAGE, 'B', ' ' },
269 { TAINT_USER, 'U', ' ' },
270 { TAINT_DIE, 'D', ' ' },
271 { TAINT_OVERRIDDEN_ACPI_TABLE, 'A', ' ' },
272 { TAINT_WARN, 'W', ' ' },
273 { TAINT_CRAP, 'C', ' ' },
274 { TAINT_FIRMWARE_WORKAROUND, 'I', ' ' },
275 { TAINT_OOT_MODULE, 'O', ' ' },
276 { TAINT_UNSIGNED_MODULE, 'E', ' ' },
280 * print_tainted - return a string to represent the kernel taint state.
282 * 'P' - Proprietary module has been loaded.
283 * 'F' - Module has been forcibly loaded.
284 * 'S' - SMP with CPUs not designed for SMP.
285 * 'R' - User forced a module unload.
286 * 'M' - System experienced a machine check exception.
287 * 'B' - System has hit bad_page.
288 * 'U' - Userspace-defined naughtiness.
289 * 'D' - Kernel has oopsed before
290 * 'A' - ACPI table overridden.
291 * 'W' - Taint on warning.
292 * 'C' - modules from drivers/staging are loaded.
293 * 'I' - Working around severe firmware bug.
294 * 'O' - Out-of-tree module has been loaded.
295 * 'E' - Unsigned module has been loaded.
297 * The string is overwritten by the next call to print_tainted().
299 const char *print_tainted(void)
301 static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
303 if (tainted_mask) {
304 char *s;
305 int i;
307 s = buf + sprintf(buf, "Tainted: ");
308 for (i = 0; i < ARRAY_SIZE(tnts); i++) {
309 const struct tnt *t = &tnts[i];
310 *s++ = test_bit(t->bit, &tainted_mask) ?
311 t->true : t->false;
313 *s = 0;
314 } else
315 snprintf(buf, sizeof(buf), "Not tainted");
317 return buf;
320 int test_taint(unsigned flag)
322 return test_bit(flag, &tainted_mask);
324 EXPORT_SYMBOL(test_taint);
326 unsigned long get_taint(void)
328 return tainted_mask;
332 * add_taint: add a taint flag if not already set.
333 * @flag: one of the TAINT_* constants.
334 * @lockdep_ok: whether lock debugging is still OK.
336 * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
337 * some notewortht-but-not-corrupting cases, it can be set to true.
339 void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
341 if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
342 pr_warn("Disabling lock debugging due to kernel taint\n");
344 set_bit(flag, &tainted_mask);
346 EXPORT_SYMBOL(add_taint);
348 static void spin_msec(int msecs)
350 int i;
352 for (i = 0; i < msecs; i++) {
353 touch_nmi_watchdog();
354 mdelay(1);
359 * It just happens that oops_enter() and oops_exit() are identically
360 * implemented...
362 static void do_oops_enter_exit(void)
364 unsigned long flags;
365 static int spin_counter;
367 if (!pause_on_oops)
368 return;
370 spin_lock_irqsave(&pause_on_oops_lock, flags);
371 if (pause_on_oops_flag == 0) {
372 /* This CPU may now print the oops message */
373 pause_on_oops_flag = 1;
374 } else {
375 /* We need to stall this CPU */
376 if (!spin_counter) {
377 /* This CPU gets to do the counting */
378 spin_counter = pause_on_oops;
379 do {
380 spin_unlock(&pause_on_oops_lock);
381 spin_msec(MSEC_PER_SEC);
382 spin_lock(&pause_on_oops_lock);
383 } while (--spin_counter);
384 pause_on_oops_flag = 0;
385 } else {
386 /* This CPU waits for a different one */
387 while (spin_counter) {
388 spin_unlock(&pause_on_oops_lock);
389 spin_msec(1);
390 spin_lock(&pause_on_oops_lock);
394 spin_unlock_irqrestore(&pause_on_oops_lock, flags);
398 * Return true if the calling CPU is allowed to print oops-related info.
399 * This is a bit racy..
401 int oops_may_print(void)
403 return pause_on_oops_flag == 0;
407 * Called when the architecture enters its oops handler, before it prints
408 * anything. If this is the first CPU to oops, and it's oopsing the first
409 * time then let it proceed.
411 * This is all enabled by the pause_on_oops kernel boot option. We do all
412 * this to ensure that oopses don't scroll off the screen. It has the
413 * side-effect of preventing later-oopsing CPUs from mucking up the display,
414 * too.
416 * It turns out that the CPU which is allowed to print ends up pausing for
417 * the right duration, whereas all the other CPUs pause for twice as long:
418 * once in oops_enter(), once in oops_exit().
420 void oops_enter(void)
422 tracing_off();
423 /* can't trust the integrity of the kernel anymore: */
424 debug_locks_off();
425 do_oops_enter_exit();
429 * 64-bit random ID for oopses:
431 static u64 oops_id;
433 static int init_oops_id(void)
435 if (!oops_id)
436 get_random_bytes(&oops_id, sizeof(oops_id));
437 else
438 oops_id++;
440 return 0;
442 late_initcall(init_oops_id);
444 void print_oops_end_marker(void)
446 init_oops_id();
447 pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id);
451 * Called when the architecture exits its oops handler, after printing
452 * everything.
454 void oops_exit(void)
456 do_oops_enter_exit();
457 print_oops_end_marker();
458 kmsg_dump(KMSG_DUMP_OOPS);
461 #ifdef WANT_WARN_ON_SLOWPATH
462 struct slowpath_args {
463 const char *fmt;
464 va_list args;
467 static void warn_slowpath_common(const char *file, int line, void *caller,
468 unsigned taint, struct slowpath_args *args)
470 disable_trace_on_warning();
472 pr_warn("------------[ cut here ]------------\n");
473 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS()\n",
474 raw_smp_processor_id(), current->pid, file, line, caller);
476 if (args)
477 vprintk(args->fmt, args->args);
479 print_modules();
480 dump_stack();
481 print_oops_end_marker();
482 /* Just a warning, don't kill lockdep. */
483 add_taint(taint, LOCKDEP_STILL_OK);
486 void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
488 struct slowpath_args args;
490 args.fmt = fmt;
491 va_start(args.args, fmt);
492 warn_slowpath_common(file, line, __builtin_return_address(0),
493 TAINT_WARN, &args);
494 va_end(args.args);
496 EXPORT_SYMBOL(warn_slowpath_fmt);
498 void warn_slowpath_fmt_taint(const char *file, int line,
499 unsigned taint, const char *fmt, ...)
501 struct slowpath_args args;
503 args.fmt = fmt;
504 va_start(args.args, fmt);
505 warn_slowpath_common(file, line, __builtin_return_address(0),
506 taint, &args);
507 va_end(args.args);
509 EXPORT_SYMBOL(warn_slowpath_fmt_taint);
511 void warn_slowpath_null(const char *file, int line)
513 warn_slowpath_common(file, line, __builtin_return_address(0),
514 TAINT_WARN, NULL);
516 EXPORT_SYMBOL(warn_slowpath_null);
517 #endif
519 #ifdef CONFIG_CC_STACKPROTECTOR
522 * Called when gcc's -fstack-protector feature is used, and
523 * gcc detects corruption of the on-stack canary value
525 __visible void __stack_chk_fail(void)
527 panic("stack-protector: Kernel stack is corrupted in: %p\n",
528 __builtin_return_address(0));
530 EXPORT_SYMBOL(__stack_chk_fail);
532 #endif
534 core_param(panic, panic_timeout, int, 0644);
535 core_param(pause_on_oops, pause_on_oops, int, 0644);
537 static int __init setup_crash_kexec_post_notifiers(char *s)
539 crash_kexec_post_notifiers = true;
540 return 0;
542 early_param("crash_kexec_post_notifiers", setup_crash_kexec_post_notifiers);
544 static int __init oops_setup(char *s)
546 if (!s)
547 return -EINVAL;
548 if (!strcmp(s, "panic"))
549 panic_on_oops = 1;
550 return 0;
552 early_param("oops", oops_setup);