panic.c 7.0 KB

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