panic.c 9.8 KB

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