panic.c 10 KB

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