panic.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. int panic_on_oops;
  21. int tainted;
  22. static int pause_on_oops;
  23. static int pause_on_oops_flag;
  24. static DEFINE_SPINLOCK(pause_on_oops_lock);
  25. int panic_timeout;
  26. ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
  27. EXPORT_SYMBOL(panic_notifier_list);
  28. static int __init panic_setup(char *str)
  29. {
  30. panic_timeout = simple_strtoul(str, NULL, 0);
  31. return 1;
  32. }
  33. __setup("panic=", panic_setup);
  34. static long no_blink(long time)
  35. {
  36. return 0;
  37. }
  38. /* Returns how long it waited in ms */
  39. long (*panic_blink)(long time);
  40. EXPORT_SYMBOL(panic_blink);
  41. /**
  42. * panic - halt the system
  43. * @fmt: The text string to print
  44. *
  45. * Display a message, then perform cleanups.
  46. *
  47. * This function never returns.
  48. */
  49. NORET_TYPE void panic(const char * fmt, ...)
  50. {
  51. long i;
  52. static char buf[1024];
  53. va_list args;
  54. #if defined(CONFIG_S390)
  55. unsigned long caller = (unsigned long) __builtin_return_address(0);
  56. #endif
  57. /*
  58. * It's possible to come here directly from a panic-assertion and not
  59. * have preempt disabled. Some functions called from here want
  60. * preempt to be disabled. No point enabling it later though...
  61. */
  62. preempt_disable();
  63. bust_spinlocks(1);
  64. va_start(args, fmt);
  65. vsnprintf(buf, sizeof(buf), fmt, args);
  66. va_end(args);
  67. printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf);
  68. bust_spinlocks(0);
  69. /*
  70. * If we have crashed and we have a crash kernel loaded let it handle
  71. * everything else.
  72. * Do we want to call this before we try to display a message?
  73. */
  74. crash_kexec(NULL);
  75. #ifdef CONFIG_SMP
  76. /*
  77. * Note smp_send_stop is the usual smp shutdown function, which
  78. * unfortunately means it may not be hardened to work in a panic
  79. * situation.
  80. */
  81. smp_send_stop();
  82. #endif
  83. atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
  84. if (!panic_blink)
  85. panic_blink = no_blink;
  86. if (panic_timeout > 0) {
  87. /*
  88. * Delay timeout seconds before rebooting the machine.
  89. * We can't use the "normal" timers since we just panicked..
  90. */
  91. printk(KERN_EMERG "Rebooting in %d seconds..",panic_timeout);
  92. for (i = 0; i < panic_timeout*1000; ) {
  93. touch_nmi_watchdog();
  94. i += panic_blink(i);
  95. mdelay(1);
  96. i++;
  97. }
  98. /* This will not be a clean reboot, with everything
  99. * shutting down. But if there is a chance of
  100. * rebooting the system it will be rebooted.
  101. */
  102. emergency_restart();
  103. }
  104. #ifdef __sparc__
  105. {
  106. extern int stop_a_enabled;
  107. /* Make sure the user can actually press Stop-A (L1-A) */
  108. stop_a_enabled = 1;
  109. printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n");
  110. }
  111. #endif
  112. #if defined(CONFIG_S390)
  113. disabled_wait(caller);
  114. #endif
  115. local_irq_enable();
  116. for (i = 0;;) {
  117. touch_softlockup_watchdog();
  118. i += panic_blink(i);
  119. mdelay(1);
  120. i++;
  121. }
  122. }
  123. EXPORT_SYMBOL(panic);
  124. /**
  125. * print_tainted - return a string to represent the kernel taint state.
  126. *
  127. * 'P' - Proprietary module has been loaded.
  128. * 'F' - Module has been forcibly loaded.
  129. * 'S' - SMP with CPUs not designed for SMP.
  130. * 'R' - User forced a module unload.
  131. * 'M' - Machine had a machine check experience.
  132. * 'B' - System has hit bad_page.
  133. *
  134. * The string is overwritten by the next call to print_taint().
  135. */
  136. const char *print_tainted(void)
  137. {
  138. static char buf[20];
  139. if (tainted) {
  140. snprintf(buf, sizeof(buf), "Tainted: %c%c%c%c%c%c",
  141. tainted & TAINT_PROPRIETARY_MODULE ? 'P' : 'G',
  142. tainted & TAINT_FORCED_MODULE ? 'F' : ' ',
  143. tainted & TAINT_UNSAFE_SMP ? 'S' : ' ',
  144. tainted & TAINT_FORCED_RMMOD ? 'R' : ' ',
  145. tainted & TAINT_MACHINE_CHECK ? 'M' : ' ',
  146. tainted & TAINT_BAD_PAGE ? 'B' : ' ');
  147. }
  148. else
  149. snprintf(buf, sizeof(buf), "Not tainted");
  150. return(buf);
  151. }
  152. void add_taint(unsigned flag)
  153. {
  154. tainted |= flag;
  155. }
  156. EXPORT_SYMBOL(add_taint);
  157. static int __init pause_on_oops_setup(char *str)
  158. {
  159. pause_on_oops = simple_strtoul(str, NULL, 0);
  160. return 1;
  161. }
  162. __setup("pause_on_oops=", pause_on_oops_setup);
  163. static void spin_msec(int msecs)
  164. {
  165. int i;
  166. for (i = 0; i < msecs; i++) {
  167. touch_nmi_watchdog();
  168. mdelay(1);
  169. }
  170. }
  171. /*
  172. * It just happens that oops_enter() and oops_exit() are identically
  173. * implemented...
  174. */
  175. static void do_oops_enter_exit(void)
  176. {
  177. unsigned long flags;
  178. static int spin_counter;
  179. if (!pause_on_oops)
  180. return;
  181. spin_lock_irqsave(&pause_on_oops_lock, flags);
  182. if (pause_on_oops_flag == 0) {
  183. /* This CPU may now print the oops message */
  184. pause_on_oops_flag = 1;
  185. } else {
  186. /* We need to stall this CPU */
  187. if (!spin_counter) {
  188. /* This CPU gets to do the counting */
  189. spin_counter = pause_on_oops;
  190. do {
  191. spin_unlock(&pause_on_oops_lock);
  192. spin_msec(MSEC_PER_SEC);
  193. spin_lock(&pause_on_oops_lock);
  194. } while (--spin_counter);
  195. pause_on_oops_flag = 0;
  196. } else {
  197. /* This CPU waits for a different one */
  198. while (spin_counter) {
  199. spin_unlock(&pause_on_oops_lock);
  200. spin_msec(1);
  201. spin_lock(&pause_on_oops_lock);
  202. }
  203. }
  204. }
  205. spin_unlock_irqrestore(&pause_on_oops_lock, flags);
  206. }
  207. /*
  208. * Return true if the calling CPU is allowed to print oops-related info. This
  209. * is a bit racy..
  210. */
  211. int oops_may_print(void)
  212. {
  213. return pause_on_oops_flag == 0;
  214. }
  215. /*
  216. * Called when the architecture enters its oops handler, before it prints
  217. * anything. If this is the first CPU to oops, and it's oopsing the first time
  218. * then let it proceed.
  219. *
  220. * This is all enabled by the pause_on_oops kernel boot option. We do all this
  221. * to ensure that oopses don't scroll off the screen. It has the side-effect
  222. * of preventing later-oopsing CPUs from mucking up the display, too.
  223. *
  224. * It turns out that the CPU which is allowed to print ends up pausing for the
  225. * right duration, whereas all the other CPUs pause for twice as long: once in
  226. * oops_enter(), once in oops_exit().
  227. */
  228. void oops_enter(void)
  229. {
  230. do_oops_enter_exit();
  231. }
  232. /*
  233. * Called when the architecture exits its oops handler, after printing
  234. * everything.
  235. */
  236. void oops_exit(void)
  237. {
  238. do_oops_enter_exit();
  239. }