panic.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. bust_spinlocks(1);
  79. va_start(args, fmt);
  80. vsnprintf(buf, sizeof(buf), fmt, args);
  81. va_end(args);
  82. printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf);
  83. #ifdef CONFIG_DEBUG_BUGVERBOSE
  84. dump_stack();
  85. #endif
  86. /*
  87. * If we have crashed and we have a crash kernel loaded let it handle
  88. * everything else.
  89. * Do we want to call this before we try to display a message?
  90. */
  91. crash_kexec(NULL);
  92. kmsg_dump(KMSG_DUMP_PANIC);
  93. /*
  94. * Note smp_send_stop is the usual smp shutdown function, which
  95. * unfortunately means it may not be hardened to work in a panic
  96. * situation.
  97. */
  98. smp_send_stop();
  99. atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
  100. bust_spinlocks(0);
  101. if (panic_timeout > 0) {
  102. /*
  103. * Delay timeout seconds before rebooting the machine.
  104. * We can't use the "normal" timers since we just panicked.
  105. */
  106. printk(KERN_EMERG "Rebooting in %d seconds..", panic_timeout);
  107. for (i = 0; i < panic_timeout; i++) {
  108. touch_nmi_watchdog();
  109. panic_blink_one_second();
  110. }
  111. /*
  112. * This will not be a clean reboot, with everything
  113. * shutting down. But if there is a chance of
  114. * rebooting the system it will be rebooted.
  115. */
  116. emergency_restart();
  117. }
  118. #ifdef __sparc__
  119. {
  120. extern int stop_a_enabled;
  121. /* Make sure the user can actually press Stop-A (L1-A) */
  122. stop_a_enabled = 1;
  123. printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n");
  124. }
  125. #endif
  126. #if defined(CONFIG_S390)
  127. {
  128. unsigned long caller;
  129. caller = (unsigned long)__builtin_return_address(0);
  130. disabled_wait(caller);
  131. }
  132. #endif
  133. local_irq_enable();
  134. while (1) {
  135. touch_softlockup_watchdog();
  136. panic_blink_one_second();
  137. }
  138. }
  139. EXPORT_SYMBOL(panic);
  140. struct tnt {
  141. u8 bit;
  142. char true;
  143. char false;
  144. };
  145. static const struct tnt tnts[] = {
  146. { TAINT_PROPRIETARY_MODULE, 'P', 'G' },
  147. { TAINT_FORCED_MODULE, 'F', ' ' },
  148. { TAINT_UNSAFE_SMP, 'S', ' ' },
  149. { TAINT_FORCED_RMMOD, 'R', ' ' },
  150. { TAINT_MACHINE_CHECK, 'M', ' ' },
  151. { TAINT_BAD_PAGE, 'B', ' ' },
  152. { TAINT_USER, 'U', ' ' },
  153. { TAINT_DIE, 'D', ' ' },
  154. { TAINT_OVERRIDDEN_ACPI_TABLE, 'A', ' ' },
  155. { TAINT_WARN, 'W', ' ' },
  156. { TAINT_CRAP, 'C', ' ' },
  157. };
  158. /**
  159. * print_tainted - return a string to represent the kernel taint state.
  160. *
  161. * 'P' - Proprietary module has been loaded.
  162. * 'F' - Module has been forcibly loaded.
  163. * 'S' - SMP with CPUs not designed for SMP.
  164. * 'R' - User forced a module unload.
  165. * 'M' - System experienced a machine check exception.
  166. * 'B' - System has hit bad_page.
  167. * 'U' - Userspace-defined naughtiness.
  168. * 'D' - Kernel has oopsed before
  169. * 'A' - ACPI table overridden.
  170. * 'W' - Taint on warning.
  171. * 'C' - modules from drivers/staging are loaded.
  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. static 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, struct slowpath_args *args)
  323. {
  324. const char *board;
  325. printk(KERN_WARNING "------------[ cut here ]------------\n");
  326. printk(KERN_WARNING "WARNING: at %s:%d %pS()\n", file, line, caller);
  327. board = dmi_get_system_info(DMI_PRODUCT_NAME);
  328. if (board)
  329. printk(KERN_WARNING "Hardware name: %s\n", board);
  330. if (args)
  331. vprintk(args->fmt, args->args);
  332. print_modules();
  333. dump_stack();
  334. print_oops_end_marker();
  335. add_taint(TAINT_WARN);
  336. }
  337. void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
  338. {
  339. struct slowpath_args args;
  340. args.fmt = fmt;
  341. va_start(args.args, fmt);
  342. warn_slowpath_common(file, line, __builtin_return_address(0), &args);
  343. va_end(args.args);
  344. }
  345. EXPORT_SYMBOL(warn_slowpath_fmt);
  346. void warn_slowpath_null(const char *file, int line)
  347. {
  348. warn_slowpath_common(file, line, __builtin_return_address(0), NULL);
  349. }
  350. EXPORT_SYMBOL(warn_slowpath_null);
  351. #endif
  352. #ifdef CONFIG_CC_STACKPROTECTOR
  353. /*
  354. * Called when gcc's -fstack-protector feature is used, and
  355. * gcc detects corruption of the on-stack canary value
  356. */
  357. void __stack_chk_fail(void)
  358. {
  359. panic("stack-protector: Kernel stack is corrupted in: %p\n",
  360. __builtin_return_address(0));
  361. }
  362. EXPORT_SYMBOL(__stack_chk_fail);
  363. #endif
  364. core_param(panic, panic_timeout, int, 0644);
  365. core_param(pause_on_oops, pause_on_oops, int, 0644);