]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/panic.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | */ | |
6 | ||
7 | /* | |
8 | * This function is used through-out the kernel (including mm and fs) | |
9 | * to indicate a major problem. | |
10 | */ | |
c95dbf27 IM |
11 | #include <linux/debug_locks.h> |
12 | #include <linux/interrupt.h> | |
456b565c | 13 | #include <linux/kmsg_dump.h> |
c95dbf27 IM |
14 | #include <linux/kallsyms.h> |
15 | #include <linux/notifier.h> | |
1da177e4 | 16 | #include <linux/module.h> |
c95dbf27 | 17 | #include <linux/random.h> |
1da177e4 | 18 | #include <linux/reboot.h> |
c95dbf27 IM |
19 | #include <linux/delay.h> |
20 | #include <linux/kexec.h> | |
21 | #include <linux/sched.h> | |
1da177e4 | 22 | #include <linux/sysrq.h> |
c95dbf27 | 23 | #include <linux/init.h> |
1da177e4 | 24 | #include <linux/nmi.h> |
bd89bb29 | 25 | #include <linux/dmi.h> |
1da177e4 | 26 | |
c7ff0d9c TS |
27 | #define PANIC_TIMER_STEP 100 |
28 | #define PANIC_BLINK_SPD 18 | |
29 | ||
1da177e4 | 30 | int panic_on_oops; |
25ddbb18 | 31 | static unsigned long tainted_mask; |
dd287796 AM |
32 | static int pause_on_oops; |
33 | static int pause_on_oops_flag; | |
34 | static DEFINE_SPINLOCK(pause_on_oops_lock); | |
1da177e4 | 35 | |
dd287796 | 36 | int panic_timeout; |
81e88fdc | 37 | EXPORT_SYMBOL_GPL(panic_timeout); |
1da177e4 | 38 | |
e041c683 | 39 | ATOMIC_NOTIFIER_HEAD(panic_notifier_list); |
1da177e4 LT |
40 | |
41 | EXPORT_SYMBOL(panic_notifier_list); | |
42 | ||
c7ff0d9c | 43 | static long no_blink(int state) |
8aeee85a | 44 | { |
c7ff0d9c | 45 | return 0; |
8aeee85a AB |
46 | } |
47 | ||
c7ff0d9c TS |
48 | /* Returns how long it waited in ms */ |
49 | long (*panic_blink)(int state); | |
50 | EXPORT_SYMBOL(panic_blink); | |
51 | ||
1da177e4 LT |
52 | /** |
53 | * panic - halt the system | |
54 | * @fmt: The text string to print | |
55 | * | |
56 | * Display a message, then perform cleanups. | |
57 | * | |
58 | * This function never returns. | |
59 | */ | |
1da177e4 LT |
60 | NORET_TYPE void panic(const char * fmt, ...) |
61 | { | |
1da177e4 LT |
62 | static char buf[1024]; |
63 | va_list args; | |
c7ff0d9c TS |
64 | long i, i_next = 0; |
65 | int state = 0; | |
1da177e4 | 66 | |
dc009d92 | 67 | /* |
c95dbf27 IM |
68 | * It's possible to come here directly from a panic-assertion and |
69 | * not have preempt disabled. Some functions called from here want | |
dc009d92 EB |
70 | * preempt to be disabled. No point enabling it later though... |
71 | */ | |
72 | preempt_disable(); | |
73 | ||
5b530fc1 | 74 | console_verbose(); |
1da177e4 LT |
75 | bust_spinlocks(1); |
76 | va_start(args, fmt); | |
77 | vsnprintf(buf, sizeof(buf), fmt, args); | |
78 | va_end(args); | |
79 | printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf); | |
5cb27301 IM |
80 | #ifdef CONFIG_DEBUG_BUGVERBOSE |
81 | dump_stack(); | |
82 | #endif | |
1da177e4 | 83 | |
dc009d92 EB |
84 | /* |
85 | * If we have crashed and we have a crash kernel loaded let it handle | |
86 | * everything else. | |
87 | * Do we want to call this before we try to display a message? | |
88 | */ | |
6e274d14 | 89 | crash_kexec(NULL); |
dc009d92 | 90 | |
0f4bd46e KM |
91 | kmsg_dump(KMSG_DUMP_PANIC); |
92 | ||
dc009d92 EB |
93 | /* |
94 | * Note smp_send_stop is the usual smp shutdown function, which | |
95 | * unfortunately means it may not be hardened to work in a panic | |
96 | * situation. | |
97 | */ | |
1da177e4 | 98 | smp_send_stop(); |
1da177e4 | 99 | |
e041c683 | 100 | atomic_notifier_call_chain(&panic_notifier_list, 0, buf); |
1da177e4 | 101 | |
d014e889 AK |
102 | bust_spinlocks(0); |
103 | ||
c7ff0d9c TS |
104 | if (!panic_blink) |
105 | panic_blink = no_blink; | |
106 | ||
dc009d92 | 107 | if (panic_timeout > 0) { |
1da177e4 | 108 | /* |
c95dbf27 IM |
109 | * Delay timeout seconds before rebooting the machine. |
110 | * We can't use the "normal" timers since we just panicked. | |
111 | */ | |
112 | printk(KERN_EMERG "Rebooting in %d seconds..", panic_timeout); | |
113 | ||
c7ff0d9c | 114 | for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) { |
1da177e4 | 115 | touch_nmi_watchdog(); |
c7ff0d9c TS |
116 | if (i >= i_next) { |
117 | i += panic_blink(state ^= 1); | |
118 | i_next = i + 3600 / PANIC_BLINK_SPD; | |
119 | } | |
120 | mdelay(PANIC_TIMER_STEP); | |
1da177e4 | 121 | } |
c95dbf27 IM |
122 | /* |
123 | * This will not be a clean reboot, with everything | |
124 | * shutting down. But if there is a chance of | |
125 | * rebooting the system it will be rebooted. | |
1da177e4 | 126 | */ |
2f048ea8 | 127 | emergency_restart(); |
1da177e4 LT |
128 | } |
129 | #ifdef __sparc__ | |
130 | { | |
131 | extern int stop_a_enabled; | |
a271c241 | 132 | /* Make sure the user can actually press Stop-A (L1-A) */ |
1da177e4 | 133 | stop_a_enabled = 1; |
a271c241 | 134 | printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n"); |
1da177e4 LT |
135 | } |
136 | #endif | |
347a8dc3 | 137 | #if defined(CONFIG_S390) |
c95dbf27 IM |
138 | { |
139 | unsigned long caller; | |
140 | ||
141 | caller = (unsigned long)__builtin_return_address(0); | |
142 | disabled_wait(caller); | |
143 | } | |
1da177e4 LT |
144 | #endif |
145 | local_irq_enable(); | |
c7ff0d9c | 146 | for (i = 0; ; i += PANIC_TIMER_STEP) { |
c22db941 | 147 | touch_softlockup_watchdog(); |
c7ff0d9c TS |
148 | if (i >= i_next) { |
149 | i += panic_blink(state ^= 1); | |
150 | i_next = i + 3600 / PANIC_BLINK_SPD; | |
151 | } | |
152 | mdelay(PANIC_TIMER_STEP); | |
1da177e4 LT |
153 | } |
154 | } | |
155 | ||
156 | EXPORT_SYMBOL(panic); | |
157 | ||
c277e63f | 158 | |
25ddbb18 | 159 | struct tnt { |
c95dbf27 IM |
160 | u8 bit; |
161 | char true; | |
162 | char false; | |
25ddbb18 AK |
163 | }; |
164 | ||
165 | static const struct tnt tnts[] = { | |
c95dbf27 IM |
166 | { TAINT_PROPRIETARY_MODULE, 'P', 'G' }, |
167 | { TAINT_FORCED_MODULE, 'F', ' ' }, | |
168 | { TAINT_UNSAFE_SMP, 'S', ' ' }, | |
169 | { TAINT_FORCED_RMMOD, 'R', ' ' }, | |
170 | { TAINT_MACHINE_CHECK, 'M', ' ' }, | |
171 | { TAINT_BAD_PAGE, 'B', ' ' }, | |
172 | { TAINT_USER, 'U', ' ' }, | |
173 | { TAINT_DIE, 'D', ' ' }, | |
174 | { TAINT_OVERRIDDEN_ACPI_TABLE, 'A', ' ' }, | |
175 | { TAINT_WARN, 'W', ' ' }, | |
176 | { TAINT_CRAP, 'C', ' ' }, | |
92946bc7 | 177 | { TAINT_FIRMWARE_WORKAROUND, 'I', ' ' }, |
25ddbb18 AK |
178 | }; |
179 | ||
1da177e4 LT |
180 | /** |
181 | * print_tainted - return a string to represent the kernel taint state. | |
182 | * | |
183 | * 'P' - Proprietary module has been loaded. | |
184 | * 'F' - Module has been forcibly loaded. | |
185 | * 'S' - SMP with CPUs not designed for SMP. | |
186 | * 'R' - User forced a module unload. | |
9aa5e993 | 187 | * 'M' - System experienced a machine check exception. |
1da177e4 | 188 | * 'B' - System has hit bad_page. |
34f5a398 | 189 | * 'U' - Userspace-defined naughtiness. |
a8005992 | 190 | * 'D' - Kernel has oopsed before |
95b570c9 NH |
191 | * 'A' - ACPI table overridden. |
192 | * 'W' - Taint on warning. | |
061b1bd3 | 193 | * 'C' - modules from drivers/staging are loaded. |
92946bc7 | 194 | * 'I' - Working around severe firmware bug. |
1da177e4 | 195 | * |
fe002a41 | 196 | * The string is overwritten by the next call to print_tainted(). |
1da177e4 | 197 | */ |
1da177e4 LT |
198 | const char *print_tainted(void) |
199 | { | |
25ddbb18 AK |
200 | static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ") + 1]; |
201 | ||
202 | if (tainted_mask) { | |
203 | char *s; | |
204 | int i; | |
205 | ||
206 | s = buf + sprintf(buf, "Tainted: "); | |
207 | for (i = 0; i < ARRAY_SIZE(tnts); i++) { | |
208 | const struct tnt *t = &tnts[i]; | |
209 | *s++ = test_bit(t->bit, &tainted_mask) ? | |
210 | t->true : t->false; | |
211 | } | |
212 | *s = 0; | |
213 | } else | |
1da177e4 | 214 | snprintf(buf, sizeof(buf), "Not tainted"); |
c95dbf27 IM |
215 | |
216 | return buf; | |
1da177e4 LT |
217 | } |
218 | ||
25ddbb18 | 219 | int test_taint(unsigned flag) |
1da177e4 | 220 | { |
25ddbb18 AK |
221 | return test_bit(flag, &tainted_mask); |
222 | } | |
223 | EXPORT_SYMBOL(test_taint); | |
224 | ||
225 | unsigned long get_taint(void) | |
226 | { | |
227 | return tainted_mask; | |
1da177e4 | 228 | } |
dd287796 | 229 | |
1da177e4 | 230 | void add_taint(unsigned flag) |
dd287796 | 231 | { |
9eeba613 FW |
232 | /* |
233 | * Can't trust the integrity of the kernel anymore. | |
234 | * We don't call directly debug_locks_off() because the issue | |
235 | * is not necessarily serious enough to set oops_in_progress to 1 | |
574bbe78 FW |
236 | * Also we want to keep up lockdep for staging development and |
237 | * post-warning case. | |
9eeba613 | 238 | */ |
574bbe78 | 239 | if (flag != TAINT_CRAP && flag != TAINT_WARN && __debug_locks_off()) |
b48ccb09 | 240 | printk(KERN_WARNING "Disabling lock debugging due to kernel taint\n"); |
9eeba613 | 241 | |
25ddbb18 | 242 | set_bit(flag, &tainted_mask); |
dd287796 | 243 | } |
1da177e4 | 244 | EXPORT_SYMBOL(add_taint); |
dd287796 AM |
245 | |
246 | static void spin_msec(int msecs) | |
247 | { | |
248 | int i; | |
249 | ||
250 | for (i = 0; i < msecs; i++) { | |
251 | touch_nmi_watchdog(); | |
252 | mdelay(1); | |
253 | } | |
254 | } | |
255 | ||
256 | /* | |
257 | * It just happens that oops_enter() and oops_exit() are identically | |
258 | * implemented... | |
259 | */ | |
260 | static void do_oops_enter_exit(void) | |
261 | { | |
262 | unsigned long flags; | |
263 | static int spin_counter; | |
264 | ||
265 | if (!pause_on_oops) | |
266 | return; | |
267 | ||
268 | spin_lock_irqsave(&pause_on_oops_lock, flags); | |
269 | if (pause_on_oops_flag == 0) { | |
270 | /* This CPU may now print the oops message */ | |
271 | pause_on_oops_flag = 1; | |
272 | } else { | |
273 | /* We need to stall this CPU */ | |
274 | if (!spin_counter) { | |
275 | /* This CPU gets to do the counting */ | |
276 | spin_counter = pause_on_oops; | |
277 | do { | |
278 | spin_unlock(&pause_on_oops_lock); | |
279 | spin_msec(MSEC_PER_SEC); | |
280 | spin_lock(&pause_on_oops_lock); | |
281 | } while (--spin_counter); | |
282 | pause_on_oops_flag = 0; | |
283 | } else { | |
284 | /* This CPU waits for a different one */ | |
285 | while (spin_counter) { | |
286 | spin_unlock(&pause_on_oops_lock); | |
287 | spin_msec(1); | |
288 | spin_lock(&pause_on_oops_lock); | |
289 | } | |
290 | } | |
291 | } | |
292 | spin_unlock_irqrestore(&pause_on_oops_lock, flags); | |
293 | } | |
294 | ||
295 | /* | |
c95dbf27 IM |
296 | * Return true if the calling CPU is allowed to print oops-related info. |
297 | * This is a bit racy.. | |
dd287796 AM |
298 | */ |
299 | int oops_may_print(void) | |
300 | { | |
301 | return pause_on_oops_flag == 0; | |
302 | } | |
303 | ||
304 | /* | |
305 | * Called when the architecture enters its oops handler, before it prints | |
c95dbf27 IM |
306 | * anything. If this is the first CPU to oops, and it's oopsing the first |
307 | * time then let it proceed. | |
dd287796 | 308 | * |
c95dbf27 IM |
309 | * This is all enabled by the pause_on_oops kernel boot option. We do all |
310 | * this to ensure that oopses don't scroll off the screen. It has the | |
311 | * side-effect of preventing later-oopsing CPUs from mucking up the display, | |
312 | * too. | |
dd287796 | 313 | * |
c95dbf27 IM |
314 | * It turns out that the CPU which is allowed to print ends up pausing for |
315 | * the right duration, whereas all the other CPUs pause for twice as long: | |
316 | * once in oops_enter(), once in oops_exit(). | |
dd287796 AM |
317 | */ |
318 | void oops_enter(void) | |
319 | { | |
bdff7870 | 320 | tracing_off(); |
c95dbf27 IM |
321 | /* can't trust the integrity of the kernel anymore: */ |
322 | debug_locks_off(); | |
dd287796 AM |
323 | do_oops_enter_exit(); |
324 | } | |
325 | ||
2c3b20e9 AV |
326 | /* |
327 | * 64-bit random ID for oopses: | |
328 | */ | |
329 | static u64 oops_id; | |
330 | ||
331 | static int init_oops_id(void) | |
332 | { | |
333 | if (!oops_id) | |
334 | get_random_bytes(&oops_id, sizeof(oops_id)); | |
d6624f99 AV |
335 | else |
336 | oops_id++; | |
2c3b20e9 AV |
337 | |
338 | return 0; | |
339 | } | |
340 | late_initcall(init_oops_id); | |
341 | ||
863a6049 | 342 | void print_oops_end_marker(void) |
71c33911 AV |
343 | { |
344 | init_oops_id(); | |
345 | printk(KERN_WARNING "---[ end trace %016llx ]---\n", | |
346 | (unsigned long long)oops_id); | |
347 | } | |
348 | ||
dd287796 AM |
349 | /* |
350 | * Called when the architecture exits its oops handler, after printing | |
351 | * everything. | |
352 | */ | |
353 | void oops_exit(void) | |
354 | { | |
355 | do_oops_enter_exit(); | |
71c33911 | 356 | print_oops_end_marker(); |
456b565c | 357 | kmsg_dump(KMSG_DUMP_OOPS); |
dd287796 | 358 | } |
3162f751 | 359 | |
79b4cc5e | 360 | #ifdef WANT_WARN_ON_SLOWPATH |
0f6f49a8 LT |
361 | struct slowpath_args { |
362 | const char *fmt; | |
a8f18b90 | 363 | va_list args; |
0f6f49a8 | 364 | }; |
bd89bb29 | 365 | |
b2be0527 BH |
366 | static void warn_slowpath_common(const char *file, int line, void *caller, |
367 | unsigned taint, struct slowpath_args *args) | |
0f6f49a8 LT |
368 | { |
369 | const char *board; | |
a8f18b90 AV |
370 | |
371 | printk(KERN_WARNING "------------[ cut here ]------------\n"); | |
0f6f49a8 | 372 | printk(KERN_WARNING "WARNING: at %s:%d %pS()\n", file, line, caller); |
bd89bb29 AV |
373 | board = dmi_get_system_info(DMI_PRODUCT_NAME); |
374 | if (board) | |
375 | printk(KERN_WARNING "Hardware name: %s\n", board); | |
74853dba | 376 | |
0f6f49a8 LT |
377 | if (args) |
378 | vprintk(args->fmt, args->args); | |
a8f18b90 AV |
379 | |
380 | print_modules(); | |
381 | dump_stack(); | |
382 | print_oops_end_marker(); | |
b2be0527 | 383 | add_taint(taint); |
a8f18b90 | 384 | } |
0f6f49a8 LT |
385 | |
386 | void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...) | |
387 | { | |
388 | struct slowpath_args args; | |
389 | ||
390 | args.fmt = fmt; | |
391 | va_start(args.args, fmt); | |
b2be0527 BH |
392 | warn_slowpath_common(file, line, __builtin_return_address(0), |
393 | TAINT_WARN, &args); | |
0f6f49a8 LT |
394 | va_end(args.args); |
395 | } | |
57adc4d2 AK |
396 | EXPORT_SYMBOL(warn_slowpath_fmt); |
397 | ||
b2be0527 BH |
398 | void warn_slowpath_fmt_taint(const char *file, int line, |
399 | unsigned taint, const char *fmt, ...) | |
400 | { | |
401 | struct slowpath_args args; | |
402 | ||
403 | args.fmt = fmt; | |
404 | va_start(args.args, fmt); | |
405 | warn_slowpath_common(file, line, __builtin_return_address(0), | |
406 | taint, &args); | |
407 | va_end(args.args); | |
408 | } | |
409 | EXPORT_SYMBOL(warn_slowpath_fmt_taint); | |
410 | ||
57adc4d2 AK |
411 | void warn_slowpath_null(const char *file, int line) |
412 | { | |
b2be0527 BH |
413 | warn_slowpath_common(file, line, __builtin_return_address(0), |
414 | TAINT_WARN, NULL); | |
57adc4d2 AK |
415 | } |
416 | EXPORT_SYMBOL(warn_slowpath_null); | |
79b4cc5e AV |
417 | #endif |
418 | ||
3162f751 | 419 | #ifdef CONFIG_CC_STACKPROTECTOR |
54371a43 | 420 | |
3162f751 AV |
421 | /* |
422 | * Called when gcc's -fstack-protector feature is used, and | |
423 | * gcc detects corruption of the on-stack canary value | |
424 | */ | |
425 | void __stack_chk_fail(void) | |
426 | { | |
517a92c4 IM |
427 | panic("stack-protector: Kernel stack is corrupted in: %p\n", |
428 | __builtin_return_address(0)); | |
3162f751 AV |
429 | } |
430 | EXPORT_SYMBOL(__stack_chk_fail); | |
54371a43 | 431 | |
3162f751 | 432 | #endif |
f44dd164 RR |
433 | |
434 | core_param(panic, panic_timeout, int, 0644); | |
435 | core_param(pause_on_oops, pause_on_oops, int, 0644); | |
d404ab0a OH |
436 | |
437 | static int __init oops_setup(char *s) | |
438 | { | |
439 | if (!s) | |
440 | return -EINVAL; | |
441 | if (!strcmp(s, "panic")) | |
442 | panic_on_oops = 1; | |
443 | return 0; | |
444 | } | |
445 | early_param("oops", oops_setup); |