panic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /* Returns how long it waited in ms */
  34. long (*panic_blink)(long time);
  35. EXPORT_SYMBOL(panic_blink);
  36. static void panic_blink_one_second(void)
  37. {
  38. static long i = 0, end;
  39. if (panic_blink) {
  40. end = i + MSEC_PER_SEC;
  41. while (i < end) {
  42. i += panic_blink(i);
  43. mdelay(1);
  44. i++;
  45. }
  46. } else {
  47. /*
  48. * When running under a hypervisor a small mdelay may get
  49. * rounded up to the hypervisor timeslice. For example, with
  50. * a 1ms in 10ms hypervisor timeslice we might inflate a
  51. * mdelay(1) loop by 10x.
  52. *
  53. * If we have nothing to blink, spin on 1 second calls to
  54. * mdelay to avoid this.
  55. */
  56. mdelay(MSEC_PER_SEC);
  57. }
  58. }
  59. /**
  60. * panic - halt the system
  61. * @fmt: The text string to print
  62. *
  63. * Display a message, then perform cleanups.
  64. *
  65. * This function never returns.
  66. */
  67. NORET_TYPE void panic(const char * fmt, ...)
  68. {
  69. static char buf[1024];
  70. va_list args;
  71. long i;
  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. preempt_disable();
  78. console_verbose();
  79. bust_spinlocks(1);
  80. va_start(args, fmt);
  81. vsnprintf(buf, sizeof(buf), fmt, args);
  82. va_end(args);
  83. printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf);
  84. #ifdef CONFIG_DEBUG_BUGVERBOSE
  85. dump_stack();
  86. #endif
  87. /*
  88. * If we have crashed and we have a crash kernel loaded let it handle
  89. * everything else.
  90. * Do we want to call this before we try to display a message?
  91. */
  92. crash_kexec(NULL);
  93. kmsg_dump(KMSG_DUMP_PANIC);
  94. /*
  95. * Note smp_send_stop is the usual smp shutdown function, which
  96. * unfortunately means it may not be hardened to work in a panic
  97. * situation.
  98. */
  99. smp_send_stop();
  100. atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
  101. bust_spinlocks(0);
  102. if (panic_timeout > 0) {
  103. /*
  104. * Delay timeout seconds before rebooting the machine.
  105. * We can't use the "normal" timers since we just panicked.
  106. */
  107. printk(KERN_EMERG "Rebooting in %d seconds..", panic_timeout);
  108. for (i = 0; i < panic_timeout; i++) {
  109. touch_nmi_watchdog();
  110. panic_blink_one_second();
  111. }
  112. /*
  113. * This will not be a clean reboot, with everything
  114. * shutting down. But if there is a chance of
  115. * rebooting the system it will be rebooted.
  116. */
  117. emergency_restart();
  118. }
  119. #ifdef __sparc__
  120. {
  121. extern int stop_a_enabled;
  122. /* Make sure the user can actually press Stop-A (L1-A) */
  123. stop_a_enabled = 1;
  124. printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n");
  125. }
  126. #endif
  127. #if defined(CONFIG_S390)
  128. {
  129. unsigned long caller;
  130. caller = (unsigned long)__builtin_return_address(0);
  131. disabled_wait(caller);
  132. }
  133. #endif
  134. local_irq_enable();
  135. while (1) {
  136. touch_softlockup_watchdog();
  137. panic_blink_one_second();
  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. if (flag != TAINT_CRAP && flag != TAINT_WARN && __debug_locks_off())
  214. printk(KERN_WARNING "Disabling lock debugging due to kernel taint\n");
  215. set_bit(flag, &tainted_mask);
  216. }
  217. EXPORT_SYMBOL(add_taint);
  218. static void spin_msec(int msecs)
  219. {
  220. int i;
  221. for (i = 0; i < msecs; i++) {
  222. touch_nmi_watchdog();
  223. mdelay(1);
  224. }
  225. }
  226. /*
  227. * It just happens that oops_enter() and oops_exit() are identically
  228. * implemented...
  229. */
  230. static void do_oops_enter_exit(void)
  231. {
  232. unsigned long flags;
  233. static int spin_counter;
  234. if (!pause_on_oops)
  235. return;
  236. spin_lock_irqsave(&pause_on_oops_lock, flags);
  237. if (pause_on_oops_flag == 0) {
  238. /* This CPU may now print the oops message */
  239. pause_on_oops_flag = 1;
  240. } else {
  241. /* We need to stall this CPU */
  242. if (!spin_counter) {
  243. /* This CPU gets to do the counting */
  244. spin_counter = pause_on_oops;
  245. do {
  246. spin_unlock(&pause_on_oops_lock);
  247. spin_msec(MSEC_PER_SEC);
  248. spin_lock(&pause_on_oops_lock);
  249. } while (--spin_counter);
  250. pause_on_oops_flag = 0;
  251. } else {
  252. /* This CPU waits for a different one */
  253. while (spin_counter) {
  254. spin_unlock(&pause_on_oops_lock);
  255. spin_msec(1);
  256. spin_lock(&pause_on_oops_lock);
  257. }
  258. }
  259. }
  260. spin_unlock_irqrestore(&pause_on_oops_lock, flags);
  261. }
  262. /*
  263. * Return true if the calling CPU is allowed to print oops-related info.
  264. * This is a bit racy..
  265. */
  266. int oops_may_print(void)
  267. {
  268. return pause_on_oops_flag == 0;
  269. }
  270. /*
  271. * Called when the architecture enters its oops handler, before it prints
  272. * anything. If this is the first CPU to oops, and it's oopsing the first
  273. * time then let it proceed.
  274. *
  275. * This is all enabled by the pause_on_oops kernel boot option. We do all
  276. * this to ensure that oopses don't scroll off the screen. It has the
  277. * side-effect of preventing later-oopsing CPUs from mucking up the display,
  278. * too.
  279. *
  280. * It turns out that the CPU which is allowed to print ends up pausing for
  281. * the right duration, whereas all the other CPUs pause for twice as long:
  282. * once in oops_enter(), once in oops_exit().
  283. */
  284. void oops_enter(void)
  285. {
  286. tracing_off();
  287. /* can't trust the integrity of the kernel anymore: */
  288. debug_locks_off();
  289. do_oops_enter_exit();
  290. }
  291. /*
  292. * 64-bit random ID for oopses:
  293. */
  294. static u64 oops_id;
  295. static int init_oops_id(void)
  296. {
  297. if (!oops_id)
  298. get_random_bytes(&oops_id, sizeof(oops_id));
  299. else
  300. oops_id++;
  301. return 0;
  302. }
  303. late_initcall(init_oops_id);
  304. static void print_oops_end_marker(void)
  305. {
  306. init_oops_id();
  307. printk(KERN_WARNING "---[ end trace %016llx ]---\n",
  308. (unsigned long long)oops_id);
  309. }
  310. /*
  311. * Called when the architecture exits its oops handler, after printing
  312. * everything.
  313. */
  314. void oops_exit(void)
  315. {
  316. do_oops_enter_exit();
  317. print_oops_end_marker();
  318. kmsg_dump(KMSG_DUMP_OOPS);
  319. }
  320. #ifdef WANT_WARN_ON_SLOWPATH
  321. struct slowpath_args {
  322. const char *fmt;
  323. va_list args;
  324. };
  325. static void warn_slowpath_common(const char *file, int line, void *caller,
  326. unsigned taint, struct slowpath_args *args)
  327. {
  328. const char *board;
  329. printk(KERN_WARNING "------------[ cut here ]------------\n");
  330. printk(KERN_WARNING "WARNING: at %s:%d %pS()\n", file, line, caller);
  331. board = dmi_get_system_info(DMI_PRODUCT_NAME);
  332. if (board)
  333. printk(KERN_WARNING "Hardware name: %s\n", board);
  334. if (args)
  335. vprintk(args->fmt, args->args);
  336. print_modules();
  337. dump_stack();
  338. print_oops_end_marker();
  339. add_taint(taint);
  340. }
  341. void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
  342. {
  343. struct slowpath_args args;
  344. args.fmt = fmt;
  345. va_start(args.args, fmt);
  346. warn_slowpath_common(file, line, __builtin_return_address(0),
  347. TAINT_WARN, &args);
  348. va_end(args.args);
  349. }
  350. EXPORT_SYMBOL(warn_slowpath_fmt);
  351. void warn_slowpath_fmt_taint(const char *file, int line,
  352. unsigned taint, const char *fmt, ...)
  353. {
  354. struct slowpath_args args;
  355. args.fmt = fmt;
  356. va_start(args.args, fmt);
  357. warn_slowpath_common(file, line, __builtin_return_address(0),
  358. taint, &args);
  359. va_end(args.args);
  360. }
  361. EXPORT_SYMBOL(warn_slowpath_fmt_taint);
  362. void warn_slowpath_null(const char *file, int line)
  363. {
  364. warn_slowpath_common(file, line, __builtin_return_address(0),
  365. TAINT_WARN, NULL);
  366. }
  367. EXPORT_SYMBOL(warn_slowpath_null);
  368. #endif
  369. #ifdef CONFIG_CC_STACKPROTECTOR
  370. /*
  371. * Called when gcc's -fstack-protector feature is used, and
  372. * gcc detects corruption of the on-stack canary value
  373. */
  374. void __stack_chk_fail(void)
  375. {
  376. panic("stack-protector: Kernel stack is corrupted in: %p\n",
  377. __builtin_return_address(0));
  378. }
  379. EXPORT_SYMBOL(__stack_chk_fail);
  380. #endif
  381. core_param(panic, panic_timeout, int, 0644);
  382. core_param(pause_on_oops, pause_on_oops, int, 0644);