packaging: install license for rpm package instead of license package
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / kernel / panic.c
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  */
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/reboot.h>
19 #include <linux/delay.h>
20 #include <linux/kexec.h>
21 #include <linux/sched.h>
22 #include <linux/sysrq.h>
23 #include <linux/init.h>
24 #include <linux/nmi.h>
25
26 #define PANIC_TIMER_STEP 100
27 #define PANIC_BLINK_SPD 18
28
29 /* Machine specific panic information string */
30 char *mach_panic_string;
31
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
38 #ifndef CONFIG_PANIC_TIMEOUT
39 #define CONFIG_PANIC_TIMEOUT 0
40 #endif
41 int panic_timeout = CONFIG_PANIC_TIMEOUT;
42 EXPORT_SYMBOL_GPL(panic_timeout);
43
44 ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
45
46 EXPORT_SYMBOL(panic_notifier_list);
47
48 static long no_blink(int state)
49 {
50         return 0;
51 }
52
53 /* Returns how long it waited in ms */
54 long (*panic_blink)(int state);
55 EXPORT_SYMBOL(panic_blink);
56
57 /*
58  * Stop ourself in panic -- architecture code may override this
59  */
60 void __weak panic_smp_self_stop(void)
61 {
62         while (1)
63                 cpu_relax();
64 }
65
66 #ifdef CONFIG_SPRD_SYSDUMP
67         extern void sysdump_enter(int enter_id, const char *reason, struct pt_regs *regs);
68 #endif
69
70 /**
71  *      panic - halt the system
72  *      @fmt: The text string to print
73  *
74  *      Display a message, then perform cleanups.
75  *
76  *      This function never returns.
77  */
78 void panic(const char *fmt, ...)
79 {
80         static DEFINE_SPINLOCK(panic_lock);
81         static char buf[1024];
82         va_list args;
83         long i, i_next = 0;
84         int state = 0;
85
86         /*
87          * Disable local interrupts. This will prevent panic_smp_self_stop
88          * from deadlocking the first cpu that invokes the panic, since
89          * there is nothing to prevent an interrupt handler (that runs
90          * after the panic_lock is acquired) from invoking panic again.
91          */
92         local_irq_disable();
93
94         /*
95          * It's possible to come here directly from a panic-assertion and
96          * not have preempt disabled. Some functions called from here want
97          * preempt to be disabled. No point enabling it later though...
98          *
99          * Only one CPU is allowed to execute the panic code from here. For
100          * multiple parallel invocations of panic, all other CPUs either
101          * stop themself or will wait until they are stopped by the 1st CPU
102          * with smp_send_stop().
103          */
104         if (!spin_trylock(&panic_lock))
105                 panic_smp_self_stop();
106
107         console_verbose();
108         bust_spinlocks(1);
109         va_start(args, fmt);
110         vsnprintf(buf, sizeof(buf), fmt, args);
111         va_end(args);
112         printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf);
113         print_modules();
114 #ifdef CONFIG_DEBUG_BUGVERBOSE
115         /*
116          * Avoid nested stack-dumping if a panic occurs during oops processing
117          */
118         if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
119                 dump_stack();
120 #endif
121
122 #ifdef CONFIG_SPRD_SYSDUMP
123         sysdump_enter(0,buf,NULL);
124 #endif
125         /*
126          * If we have crashed and we have a crash kernel loaded let it handle
127          * everything else.
128          * Do we want to call this before we try to display a message?
129          */
130         crash_kexec(NULL);
131
132         /*
133          * Note smp_send_stop is the usual smp shutdown function, which
134          * unfortunately means it may not be hardened to work in a panic
135          * situation.
136          */
137         smp_send_stop();
138
139         kmsg_dump(KMSG_DUMP_PANIC);
140
141         atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
142
143         bust_spinlocks(0);
144
145         if (!panic_blink)
146                 panic_blink = no_blink;
147
148         if (panic_timeout > 0) {
149                 /*
150                  * Delay timeout seconds before rebooting the machine.
151                  * We can't use the "normal" timers since we just panicked.
152                  */
153                 printk(KERN_EMERG "Rebooting in %d seconds..", panic_timeout);
154
155                 for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
156                         touch_nmi_watchdog();
157                         if (i >= i_next) {
158                                 i += panic_blink(state ^= 1);
159                                 i_next = i + 3600 / PANIC_BLINK_SPD;
160                         }
161                         mdelay(PANIC_TIMER_STEP);
162                 }
163         }
164         if (panic_timeout != 0) {
165                 /*
166                  * This will not be a clean reboot, with everything
167                  * shutting down.  But if there is a chance of
168                  * rebooting the system it will be rebooted.
169                  */
170                 emergency_restart();
171         }
172 #ifdef __sparc__
173         {
174                 extern int stop_a_enabled;
175                 /* Make sure the user can actually press Stop-A (L1-A) */
176                 stop_a_enabled = 1;
177                 printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n");
178         }
179 #endif
180 #if defined(CONFIG_S390)
181         {
182                 unsigned long caller;
183
184                 caller = (unsigned long)__builtin_return_address(0);
185                 disabled_wait(caller);
186         }
187 #endif
188         local_irq_enable();
189         for (i = 0; ; i += PANIC_TIMER_STEP) {
190                 touch_softlockup_watchdog();
191                 if (i >= i_next) {
192                         i += panic_blink(state ^= 1);
193                         i_next = i + 3600 / PANIC_BLINK_SPD;
194                 }
195                 mdelay(PANIC_TIMER_STEP);
196         }
197 }
198
199 EXPORT_SYMBOL(panic);
200
201
202 struct tnt {
203         u8      bit;
204         char    true;
205         char    false;
206 };
207
208 static const struct tnt tnts[] = {
209         { TAINT_PROPRIETARY_MODULE,     'P', 'G' },
210         { TAINT_FORCED_MODULE,          'F', ' ' },
211         { TAINT_UNSAFE_SMP,             'S', ' ' },
212         { TAINT_FORCED_RMMOD,           'R', ' ' },
213         { TAINT_MACHINE_CHECK,          'M', ' ' },
214         { TAINT_BAD_PAGE,               'B', ' ' },
215         { TAINT_USER,                   'U', ' ' },
216         { TAINT_DIE,                    'D', ' ' },
217         { TAINT_OVERRIDDEN_ACPI_TABLE,  'A', ' ' },
218         { TAINT_WARN,                   'W', ' ' },
219         { TAINT_CRAP,                   'C', ' ' },
220         { TAINT_FIRMWARE_WORKAROUND,    'I', ' ' },
221         { TAINT_OOT_MODULE,             'O', ' ' },
222 };
223
224 /**
225  *      print_tainted - return a string to represent the kernel taint state.
226  *
227  *  'P' - Proprietary module has been loaded.
228  *  'F' - Module has been forcibly loaded.
229  *  'S' - SMP with CPUs not designed for SMP.
230  *  'R' - User forced a module unload.
231  *  'M' - System experienced a machine check exception.
232  *  'B' - System has hit bad_page.
233  *  'U' - Userspace-defined naughtiness.
234  *  'D' - Kernel has oopsed before
235  *  'A' - ACPI table overridden.
236  *  'W' - Taint on warning.
237  *  'C' - modules from drivers/staging are loaded.
238  *  'I' - Working around severe firmware bug.
239  *  'O' - Out-of-tree module has been loaded.
240  *
241  *      The string is overwritten by the next call to print_tainted().
242  */
243 const char *print_tainted(void)
244 {
245         static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ") + 1];
246
247         if (tainted_mask) {
248                 char *s;
249                 int i;
250
251                 s = buf + sprintf(buf, "Tainted: ");
252                 for (i = 0; i < ARRAY_SIZE(tnts); i++) {
253                         const struct tnt *t = &tnts[i];
254                         *s++ = test_bit(t->bit, &tainted_mask) ?
255                                         t->true : t->false;
256                 }
257                 *s = 0;
258         } else
259                 snprintf(buf, sizeof(buf), "Not tainted");
260
261         return buf;
262 }
263
264 int test_taint(unsigned flag)
265 {
266         return test_bit(flag, &tainted_mask);
267 }
268 EXPORT_SYMBOL(test_taint);
269
270 unsigned long get_taint(void)
271 {
272         return tainted_mask;
273 }
274
275 /**
276  * add_taint: add a taint flag if not already set.
277  * @flag: one of the TAINT_* constants.
278  * @lockdep_ok: whether lock debugging is still OK.
279  *
280  * If something bad has gone wrong, you'll want @lockdebug_ok = false, but for
281  * some notewortht-but-not-corrupting cases, it can be set to true.
282  */
283 void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
284 {
285         if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
286                 printk(KERN_WARNING
287                        "Disabling lock debugging due to kernel taint\n");
288
289         set_bit(flag, &tainted_mask);
290 }
291 EXPORT_SYMBOL(add_taint);
292
293 static void spin_msec(int msecs)
294 {
295         int i;
296
297         for (i = 0; i < msecs; i++) {
298                 touch_nmi_watchdog();
299                 mdelay(1);
300         }
301 }
302
303 /*
304  * It just happens that oops_enter() and oops_exit() are identically
305  * implemented...
306  */
307 static void do_oops_enter_exit(void)
308 {
309         unsigned long flags;
310         static int spin_counter;
311
312         if (!pause_on_oops)
313                 return;
314
315         spin_lock_irqsave(&pause_on_oops_lock, flags);
316         if (pause_on_oops_flag == 0) {
317                 /* This CPU may now print the oops message */
318                 pause_on_oops_flag = 1;
319         } else {
320                 /* We need to stall this CPU */
321                 if (!spin_counter) {
322                         /* This CPU gets to do the counting */
323                         spin_counter = pause_on_oops;
324                         do {
325                                 spin_unlock(&pause_on_oops_lock);
326                                 spin_msec(MSEC_PER_SEC);
327                                 spin_lock(&pause_on_oops_lock);
328                         } while (--spin_counter);
329                         pause_on_oops_flag = 0;
330                 } else {
331                         /* This CPU waits for a different one */
332                         while (spin_counter) {
333                                 spin_unlock(&pause_on_oops_lock);
334                                 spin_msec(1);
335                                 spin_lock(&pause_on_oops_lock);
336                         }
337                 }
338         }
339         spin_unlock_irqrestore(&pause_on_oops_lock, flags);
340 }
341
342 /*
343  * Return true if the calling CPU is allowed to print oops-related info.
344  * This is a bit racy..
345  */
346 int oops_may_print(void)
347 {
348         return pause_on_oops_flag == 0;
349 }
350
351 /*
352  * Called when the architecture enters its oops handler, before it prints
353  * anything.  If this is the first CPU to oops, and it's oopsing the first
354  * time then let it proceed.
355  *
356  * This is all enabled by the pause_on_oops kernel boot option.  We do all
357  * this to ensure that oopses don't scroll off the screen.  It has the
358  * side-effect of preventing later-oopsing CPUs from mucking up the display,
359  * too.
360  *
361  * It turns out that the CPU which is allowed to print ends up pausing for
362  * the right duration, whereas all the other CPUs pause for twice as long:
363  * once in oops_enter(), once in oops_exit().
364  */
365 void oops_enter(void)
366 {
367         tracing_off();
368         /* can't trust the integrity of the kernel anymore: */
369         debug_locks_off();
370         do_oops_enter_exit();
371 }
372
373 /*
374  * 64-bit random ID for oopses:
375  */
376 static u64 oops_id;
377
378 static int init_oops_id(void)
379 {
380         if (!oops_id)
381                 get_random_bytes(&oops_id, sizeof(oops_id));
382         else
383                 oops_id++;
384
385         return 0;
386 }
387 late_initcall(init_oops_id);
388
389 void print_oops_end_marker(void)
390 {
391         init_oops_id();
392
393         if (mach_panic_string)
394                 printk(KERN_WARNING "Board Information: %s\n",
395                        mach_panic_string);
396
397         printk(KERN_WARNING "---[ end trace %016llx ]---\n",
398                 (unsigned long long)oops_id);
399 }
400
401 /*
402  * Called when the architecture exits its oops handler, after printing
403  * everything.
404  */
405 void oops_exit(void)
406 {
407         do_oops_enter_exit();
408         print_oops_end_marker();
409         kmsg_dump(KMSG_DUMP_OOPS);
410 }
411
412 #ifdef WANT_WARN_ON_SLOWPATH
413 struct slowpath_args {
414         const char *fmt;
415         va_list args;
416 };
417
418 static void warn_slowpath_common(const char *file, int line, void *caller,
419                                  unsigned taint, struct slowpath_args *args)
420 {
421         printk(KERN_WARNING "------------[ cut here ]------------\n");
422         printk(KERN_WARNING "WARNING: at %s:%d %pS()\n", file, line, caller);
423
424         if (args)
425                 vprintk(args->fmt, args->args);
426
427         print_modules();
428         dump_stack();
429         print_oops_end_marker();
430         /* Just a warning, don't kill lockdep. */
431         add_taint(taint, LOCKDEP_STILL_OK);
432 }
433
434 void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
435 {
436         struct slowpath_args args;
437
438         args.fmt = fmt;
439         va_start(args.args, fmt);
440         warn_slowpath_common(file, line, __builtin_return_address(0),
441                              TAINT_WARN, &args);
442         va_end(args.args);
443 }
444 EXPORT_SYMBOL(warn_slowpath_fmt);
445
446 void warn_slowpath_fmt_taint(const char *file, int line,
447                              unsigned taint, const char *fmt, ...)
448 {
449         struct slowpath_args args;
450
451         args.fmt = fmt;
452         va_start(args.args, fmt);
453         warn_slowpath_common(file, line, __builtin_return_address(0),
454                              taint, &args);
455         va_end(args.args);
456 }
457 EXPORT_SYMBOL(warn_slowpath_fmt_taint);
458
459 void warn_slowpath_null(const char *file, int line)
460 {
461         warn_slowpath_common(file, line, __builtin_return_address(0),
462                              TAINT_WARN, NULL);
463 }
464 EXPORT_SYMBOL(warn_slowpath_null);
465 #endif
466
467 #ifdef CONFIG_CC_STACKPROTECTOR
468
469 /*
470  * Called when gcc's -fstack-protector feature is used, and
471  * gcc detects corruption of the on-stack canary value
472  */
473 void __stack_chk_fail(void)
474 {
475         panic("stack-protector: Kernel stack is corrupted in: %p\n",
476                 __builtin_return_address(0));
477 }
478 EXPORT_SYMBOL(__stack_chk_fail);
479
480 #endif
481
482 core_param(panic, panic_timeout, int, 0644);
483 core_param(pause_on_oops, pause_on_oops, int, 0644);
484
485 static int __init oops_setup(char *s)
486 {
487         if (!s)
488                 return -EINVAL;
489         if (!strcmp(s, "panic"))
490                 panic_on_oops = 1;
491         return 0;
492 }
493 early_param("oops", oops_setup);