panic.c 8.2 KB

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