panic.c 9.1 KB

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