panic.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. int panic_on_oops;
  22. int tainted;
  23. static int pause_on_oops;
  24. static int pause_on_oops_flag;
  25. static DEFINE_SPINLOCK(pause_on_oops_lock);
  26. int panic_timeout;
  27. ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
  28. EXPORT_SYMBOL(panic_notifier_list);
  29. static int __init panic_setup(char *str)
  30. {
  31. panic_timeout = simple_strtoul(str, NULL, 0);
  32. return 1;
  33. }
  34. __setup("panic=", panic_setup);
  35. static long no_blink(long time)
  36. {
  37. return 0;
  38. }
  39. /* Returns how long it waited in ms */
  40. long (*panic_blink)(long time);
  41. EXPORT_SYMBOL(panic_blink);
  42. /**
  43. * panic - halt the system
  44. * @fmt: The text string to print
  45. *
  46. * Display a message, then perform cleanups.
  47. *
  48. * This function never returns.
  49. */
  50. NORET_TYPE void panic(const char * fmt, ...)
  51. {
  52. long i;
  53. static char buf[1024];
  54. va_list args;
  55. #if defined(CONFIG_S390)
  56. unsigned long caller = (unsigned long) __builtin_return_address(0);
  57. #endif
  58. /*
  59. * It's possible to come here directly from a panic-assertion and not
  60. * have preempt disabled. Some functions called from here want
  61. * preempt to be disabled. No point enabling it later though...
  62. */
  63. preempt_disable();
  64. bust_spinlocks(1);
  65. va_start(args, fmt);
  66. vsnprintf(buf, sizeof(buf), fmt, args);
  67. va_end(args);
  68. printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf);
  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. /**
  126. * print_tainted - return a string to represent the kernel taint state.
  127. *
  128. * 'P' - Proprietary module has been loaded.
  129. * 'F' - Module has been forcibly loaded.
  130. * 'S' - SMP with CPUs not designed for SMP.
  131. * 'R' - User forced a module unload.
  132. * 'M' - Machine had a machine check experience.
  133. * 'B' - System has hit bad_page.
  134. * 'U' - Userspace-defined naughtiness.
  135. *
  136. * The string is overwritten by the next call to print_taint().
  137. */
  138. const char *print_tainted(void)
  139. {
  140. static char buf[20];
  141. if (tainted) {
  142. snprintf(buf, sizeof(buf), "Tainted: %c%c%c%c%c%c%c",
  143. tainted & TAINT_PROPRIETARY_MODULE ? 'P' : 'G',
  144. tainted & TAINT_FORCED_MODULE ? 'F' : ' ',
  145. tainted & TAINT_UNSAFE_SMP ? 'S' : ' ',
  146. tainted & TAINT_FORCED_RMMOD ? 'R' : ' ',
  147. tainted & TAINT_MACHINE_CHECK ? 'M' : ' ',
  148. tainted & TAINT_BAD_PAGE ? 'B' : ' ',
  149. tainted & TAINT_USER ? 'U' : ' ');
  150. }
  151. else
  152. snprintf(buf, sizeof(buf), "Not tainted");
  153. return(buf);
  154. }
  155. void add_taint(unsigned flag)
  156. {
  157. debug_locks = 0; /* can't trust the integrity of the kernel anymore */
  158. tainted |= flag;
  159. }
  160. EXPORT_SYMBOL(add_taint);
  161. static int __init pause_on_oops_setup(char *str)
  162. {
  163. pause_on_oops = simple_strtoul(str, NULL, 0);
  164. return 1;
  165. }
  166. __setup("pause_on_oops=", pause_on_oops_setup);
  167. static void spin_msec(int msecs)
  168. {
  169. int i;
  170. for (i = 0; i < msecs; i++) {
  171. touch_nmi_watchdog();
  172. mdelay(1);
  173. }
  174. }
  175. /*
  176. * It just happens that oops_enter() and oops_exit() are identically
  177. * implemented...
  178. */
  179. static void do_oops_enter_exit(void)
  180. {
  181. unsigned long flags;
  182. static int spin_counter;
  183. if (!pause_on_oops)
  184. return;
  185. spin_lock_irqsave(&pause_on_oops_lock, flags);
  186. if (pause_on_oops_flag == 0) {
  187. /* This CPU may now print the oops message */
  188. pause_on_oops_flag = 1;
  189. } else {
  190. /* We need to stall this CPU */
  191. if (!spin_counter) {
  192. /* This CPU gets to do the counting */
  193. spin_counter = pause_on_oops;
  194. do {
  195. spin_unlock(&pause_on_oops_lock);
  196. spin_msec(MSEC_PER_SEC);
  197. spin_lock(&pause_on_oops_lock);
  198. } while (--spin_counter);
  199. pause_on_oops_flag = 0;
  200. } else {
  201. /* This CPU waits for a different one */
  202. while (spin_counter) {
  203. spin_unlock(&pause_on_oops_lock);
  204. spin_msec(1);
  205. spin_lock(&pause_on_oops_lock);
  206. }
  207. }
  208. }
  209. spin_unlock_irqrestore(&pause_on_oops_lock, flags);
  210. }
  211. /*
  212. * Return true if the calling CPU is allowed to print oops-related info. This
  213. * is a bit racy..
  214. */
  215. int oops_may_print(void)
  216. {
  217. return pause_on_oops_flag == 0;
  218. }
  219. /*
  220. * Called when the architecture enters its oops handler, before it prints
  221. * anything. If this is the first CPU to oops, and it's oopsing the first time
  222. * then let it proceed.
  223. *
  224. * This is all enabled by the pause_on_oops kernel boot option. We do all this
  225. * to ensure that oopses don't scroll off the screen. It has the side-effect
  226. * of preventing later-oopsing CPUs from mucking up the display, too.
  227. *
  228. * It turns out that the CPU which is allowed to print ends up pausing for the
  229. * right duration, whereas all the other CPUs pause for twice as long: once in
  230. * oops_enter(), once in oops_exit().
  231. */
  232. void oops_enter(void)
  233. {
  234. debug_locks_off(); /* can't trust the integrity of the kernel anymore */
  235. do_oops_enter_exit();
  236. }
  237. /*
  238. * Called when the architecture exits its oops handler, after printing
  239. * everything.
  240. */
  241. void oops_exit(void)
  242. {
  243. do_oops_enter_exit();
  244. }
  245. #ifdef CONFIG_CC_STACKPROTECTOR
  246. /*
  247. * Called when gcc's -fstack-protector feature is used, and
  248. * gcc detects corruption of the on-stack canary value
  249. */
  250. void __stack_chk_fail(void)
  251. {
  252. panic("stack-protector: Kernel stack is corrupted");
  253. }
  254. EXPORT_SYMBOL(__stack_chk_fail);
  255. #endif