panic.c 10 KB

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