panic.c 11 KB

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