panic.c 8.4 KB

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