panic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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/debug_locks.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kmsg_dump.h>
  13. #include <linux/kallsyms.h>
  14. #include <linux/notifier.h>
  15. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/reboot.h>
  18. #include <linux/delay.h>
  19. #include <linux/kexec.h>
  20. #include <linux/sched.h>
  21. #include <linux/sysrq.h>
  22. #include <linux/init.h>
  23. #include <linux/nmi.h>
  24. #include <linux/dmi.h>
  25. #include <linux/coresight.h>
  26. #include <mach/sec_debug.h>
  27. #define PANIC_TIMER_STEP 100
  28. #define PANIC_BLINK_SPD 18
  29. /* Machine specific panic information string */
  30. char *mach_panic_string;
  31. int panic_on_oops;
  32. static unsigned long tainted_mask;
  33. static int pause_on_oops;
  34. static int pause_on_oops_flag;
  35. static DEFINE_SPINLOCK(pause_on_oops_lock);
  36. #ifndef CONFIG_PANIC_TIMEOUT
  37. #define CONFIG_PANIC_TIMEOUT 0
  38. #endif
  39. int panic_timeout = CONFIG_PANIC_TIMEOUT;
  40. EXPORT_SYMBOL_GPL(panic_timeout);
  41. ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
  42. EXPORT_SYMBOL(panic_notifier_list);
  43. static long no_blink(int state)
  44. {
  45. return 0;
  46. }
  47. /* Returns how long it waited in ms */
  48. long (*panic_blink)(int state);
  49. EXPORT_SYMBOL(panic_blink);
  50. /*
  51. * Stop ourself in panic -- architecture code may override this
  52. */
  53. void __weak panic_smp_self_stop(void)
  54. {
  55. while (1)
  56. cpu_relax();
  57. }
  58. /**
  59. * panic - halt the system
  60. * @fmt: The text string to print
  61. *
  62. * Display a message, then perform cleanups.
  63. *
  64. * This function never returns.
  65. */
  66. void panic(const char *fmt, ...)
  67. {
  68. static DEFINE_SPINLOCK(panic_lock);
  69. static char buf[1024];
  70. va_list args;
  71. long i, i_next = 0;
  72. int state = 0;
  73. #ifdef CONFIG_SEC_DEBUG
  74. emerg_pet_watchdog(); /*To prevent watchdog reset during panic handling. */
  75. #endif
  76. coresight_abort();
  77. /*
  78. * Disable local interrupts. This will prevent panic_smp_self_stop
  79. * from deadlocking the first cpu that invokes the panic, since
  80. * there is nothing to prevent an interrupt handler (that runs
  81. * after the panic_lock is acquired) from invoking panic again.
  82. */
  83. local_irq_disable();
  84. /*
  85. * Disable local interrupts. This will prevent panic_smp_self_stop
  86. * from deadlocking the first cpu that invokes the panic, since
  87. * there is nothing to prevent an interrupt handler (that runs
  88. * after the panic_lock is acquired) from invoking panic again.
  89. */
  90. local_irq_disable();
  91. preempt_disable_notrace();
  92. /*
  93. * It's possible to come here directly from a panic-assertion and
  94. * not have preempt disabled. Some functions called from here want
  95. * preempt to be disabled. No point enabling it later though...
  96. *
  97. * Only one CPU is allowed to execute the panic code from here. For
  98. * multiple parallel invocations of panic, all other CPUs either
  99. * stop themself or will wait until they are stopped by the 1st CPU
  100. * with smp_send_stop().
  101. */
  102. if (!spin_trylock(&panic_lock))
  103. panic_smp_self_stop();
  104. #ifdef CONFIG_SEC_DEBUG
  105. secdbg_sched_msg("!!panic!!");
  106. #endif
  107. console_verbose();
  108. bust_spinlocks(1);
  109. va_start(args, fmt);
  110. vsnprintf(buf, sizeof(buf), fmt, args);
  111. va_end(args);
  112. printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf);
  113. #ifdef CONFIG_DEBUG_BUGVERBOSE
  114. /*
  115. * Avoid nested stack-dumping if a panic occurs during oops processing
  116. */
  117. if (!test_taint(TAINT_DIE) && oops_in_progress <= 1)
  118. dump_stack();
  119. #endif
  120. #ifdef CONFIG_SEC_DEBUG_SUBSYS
  121. sec_debug_save_panic_info(buf,
  122. (unsigned int)__builtin_return_address(0));
  123. #endif
  124. /*
  125. * If we have crashed and we have a crash kernel loaded let it handle
  126. * everything else.
  127. * Do we want to call this before we try to display a message?
  128. */
  129. crash_kexec(NULL);
  130. /*
  131. * Note smp_send_stop is the usual smp shutdown function, which
  132. * unfortunately means it may not be hardened to work in a panic
  133. * situation.
  134. */
  135. smp_send_stop();
  136. kmsg_dump(KMSG_DUMP_PANIC);
  137. atomic_notifier_call_chain(&panic_notifier_list, 0, buf);
  138. bust_spinlocks(0);
  139. if (!panic_blink)
  140. panic_blink = no_blink;
  141. if (panic_timeout > 0) {
  142. /*
  143. * Delay timeout seconds before rebooting the machine.
  144. * We can't use the "normal" timers since we just panicked.
  145. */
  146. printk(KERN_EMERG "Rebooting in %d seconds..", panic_timeout);
  147. for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
  148. touch_nmi_watchdog();
  149. if (i >= i_next) {
  150. i += panic_blink(state ^= 1);
  151. i_next = i + 3600 / PANIC_BLINK_SPD;
  152. }
  153. mdelay(PANIC_TIMER_STEP);
  154. }
  155. }
  156. if (panic_timeout != 0) {
  157. /*
  158. * This will not be a clean reboot, with everything
  159. * shutting down. But if there is a chance of
  160. * rebooting the system it will be rebooted.
  161. */
  162. emergency_restart();
  163. }
  164. #ifdef __sparc__
  165. {
  166. extern int stop_a_enabled;
  167. /* Make sure the user can actually press Stop-A (L1-A) */
  168. stop_a_enabled = 1;
  169. printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n");
  170. }
  171. #endif
  172. #if defined(CONFIG_S390)
  173. {
  174. unsigned long caller;
  175. caller = (unsigned long)__builtin_return_address(0);
  176. disabled_wait(caller);
  177. }
  178. #endif
  179. local_irq_enable();
  180. for (i = 0; ; i += PANIC_TIMER_STEP) {
  181. touch_softlockup_watchdog();
  182. if (i >= i_next) {
  183. i += panic_blink(state ^= 1);
  184. i_next = i + 3600 / PANIC_BLINK_SPD;
  185. }
  186. mdelay(PANIC_TIMER_STEP);
  187. }
  188. }
  189. EXPORT_SYMBOL(panic);
  190. struct tnt {
  191. u8 bit;
  192. char true;
  193. char false;
  194. };
  195. static const struct tnt tnts[] = {
  196. { TAINT_PROPRIETARY_MODULE, 'P', 'G' },
  197. { TAINT_FORCED_MODULE, 'F', ' ' },
  198. { TAINT_UNSAFE_SMP, 'S', ' ' },
  199. { TAINT_FORCED_RMMOD, 'R', ' ' },
  200. { TAINT_MACHINE_CHECK, 'M', ' ' },
  201. { TAINT_BAD_PAGE, 'B', ' ' },
  202. { TAINT_USER, 'U', ' ' },
  203. { TAINT_DIE, 'D', ' ' },
  204. { TAINT_OVERRIDDEN_ACPI_TABLE, 'A', ' ' },
  205. { TAINT_WARN, 'W', ' ' },
  206. { TAINT_CRAP, 'C', ' ' },
  207. { TAINT_FIRMWARE_WORKAROUND, 'I', ' ' },
  208. { TAINT_OOT_MODULE, 'O', ' ' },
  209. };
  210. /**
  211. * print_tainted - return a string to represent the kernel taint state.
  212. *
  213. * 'P' - Proprietary module has been loaded.
  214. * 'F' - Module has been forcibly loaded.
  215. * 'S' - SMP with CPUs not designed for SMP.
  216. * 'R' - User forced a module unload.
  217. * 'M' - System experienced a machine check exception.
  218. * 'B' - System has hit bad_page.
  219. * 'U' - Userspace-defined naughtiness.
  220. * 'D' - Kernel has oopsed before
  221. * 'A' - ACPI table overridden.
  222. * 'W' - Taint on warning.
  223. * 'C' - modules from drivers/staging are loaded.
  224. * 'I' - Working around severe firmware bug.
  225. * 'O' - Out-of-tree module has been loaded.
  226. *
  227. * The string is overwritten by the next call to print_tainted().
  228. */
  229. const char *print_tainted(void)
  230. {
  231. static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ") + 1];
  232. if (tainted_mask) {
  233. char *s;
  234. int i;
  235. s = buf + sprintf(buf, "Tainted: ");
  236. for (i = 0; i < ARRAY_SIZE(tnts); i++) {
  237. const struct tnt *t = &tnts[i];
  238. *s++ = test_bit(t->bit, &tainted_mask) ?
  239. t->true : t->false;
  240. }
  241. *s = 0;
  242. } else
  243. snprintf(buf, sizeof(buf), "Not tainted");
  244. return buf;
  245. }
  246. int test_taint(unsigned flag)
  247. {
  248. return test_bit(flag, &tainted_mask);
  249. }
  250. EXPORT_SYMBOL(test_taint);
  251. unsigned long get_taint(void)
  252. {
  253. return tainted_mask;
  254. }
  255. void add_taint(unsigned flag)
  256. {
  257. /*
  258. * Can't trust the integrity of the kernel anymore.
  259. * We don't call directly debug_locks_off() because the issue
  260. * is not necessarily serious enough to set oops_in_progress to 1
  261. * Also we want to keep up lockdep for staging/out-of-tree
  262. * development and post-warning case.
  263. */
  264. switch (flag) {
  265. case TAINT_CRAP:
  266. case TAINT_OOT_MODULE:
  267. case TAINT_WARN:
  268. case TAINT_FIRMWARE_WORKAROUND:
  269. break;
  270. default:
  271. if (__debug_locks_off())
  272. printk(KERN_WARNING "Disabling lock debugging due to kernel taint\n");
  273. }
  274. set_bit(flag, &tainted_mask);
  275. }
  276. EXPORT_SYMBOL(add_taint);
  277. static void spin_msec(int msecs)
  278. {
  279. int i;
  280. for (i = 0; i < msecs; i++) {
  281. touch_nmi_watchdog();
  282. mdelay(1);
  283. }
  284. }
  285. /*
  286. * It just happens that oops_enter() and oops_exit() are identically
  287. * implemented...
  288. */
  289. static void do_oops_enter_exit(void)
  290. {
  291. unsigned long flags;
  292. static int spin_counter;
  293. if (!pause_on_oops)
  294. return;
  295. spin_lock_irqsave(&pause_on_oops_lock, flags);
  296. if (pause_on_oops_flag == 0) {
  297. /* This CPU may now print the oops message */
  298. pause_on_oops_flag = 1;
  299. } else {
  300. /* We need to stall this CPU */
  301. if (!spin_counter) {
  302. /* This CPU gets to do the counting */
  303. spin_counter = pause_on_oops;
  304. do {
  305. spin_unlock(&pause_on_oops_lock);
  306. spin_msec(MSEC_PER_SEC);
  307. spin_lock(&pause_on_oops_lock);
  308. } while (--spin_counter);
  309. pause_on_oops_flag = 0;
  310. } else {
  311. /* This CPU waits for a different one */
  312. while (spin_counter) {
  313. spin_unlock(&pause_on_oops_lock);
  314. spin_msec(1);
  315. spin_lock(&pause_on_oops_lock);
  316. }
  317. }
  318. }
  319. spin_unlock_irqrestore(&pause_on_oops_lock, flags);
  320. }
  321. /*
  322. * Return true if the calling CPU is allowed to print oops-related info.
  323. * This is a bit racy..
  324. */
  325. int oops_may_print(void)
  326. {
  327. return pause_on_oops_flag == 0;
  328. }
  329. /*
  330. * Called when the architecture enters its oops handler, before it prints
  331. * anything. If this is the first CPU to oops, and it's oopsing the first
  332. * time then let it proceed.
  333. *
  334. * This is all enabled by the pause_on_oops kernel boot option. We do all
  335. * this to ensure that oopses don't scroll off the screen. It has the
  336. * side-effect of preventing later-oopsing CPUs from mucking up the display,
  337. * too.
  338. *
  339. * It turns out that the CPU which is allowed to print ends up pausing for
  340. * the right duration, whereas all the other CPUs pause for twice as long:
  341. * once in oops_enter(), once in oops_exit().
  342. */
  343. void oops_enter(void)
  344. {
  345. tracing_off();
  346. /* can't trust the integrity of the kernel anymore: */
  347. debug_locks_off();
  348. do_oops_enter_exit();
  349. }
  350. /*
  351. * 64-bit random ID for oopses:
  352. */
  353. static u64 oops_id;
  354. static int init_oops_id(void)
  355. {
  356. if (!oops_id)
  357. get_random_bytes(&oops_id, sizeof(oops_id));
  358. else
  359. oops_id++;
  360. return 0;
  361. }
  362. late_initcall(init_oops_id);
  363. void print_oops_end_marker(void)
  364. {
  365. init_oops_id();
  366. if (mach_panic_string)
  367. printk(KERN_WARNING "Board Information: %s\n",
  368. mach_panic_string);
  369. printk(KERN_WARNING "---[ end trace %016llx ]---\n",
  370. (unsigned long long)oops_id);
  371. }
  372. /*
  373. * Called when the architecture exits its oops handler, after printing
  374. * everything.
  375. */
  376. void oops_exit(void)
  377. {
  378. do_oops_enter_exit();
  379. print_oops_end_marker();
  380. kmsg_dump(KMSG_DUMP_OOPS);
  381. }
  382. #ifdef WANT_WARN_ON_SLOWPATH
  383. struct slowpath_args {
  384. const char *fmt;
  385. va_list args;
  386. };
  387. static void warn_slowpath_common(const char *file, int line, void *caller,
  388. unsigned taint, struct slowpath_args *args)
  389. {
  390. const char *board;
  391. printk(KERN_WARNING "------------[ cut here ]------------\n");
  392. printk(KERN_WARNING "WARNING: at %s:%d %pS()\n", file, line, caller);
  393. board = dmi_get_system_info(DMI_PRODUCT_NAME);
  394. if (board)
  395. printk(KERN_WARNING "Hardware name: %s\n", board);
  396. if (args)
  397. vprintk(args->fmt, args->args);
  398. print_modules();
  399. dump_stack();
  400. print_oops_end_marker();
  401. add_taint(taint);
  402. }
  403. void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
  404. {
  405. struct slowpath_args args;
  406. args.fmt = fmt;
  407. va_start(args.args, fmt);
  408. warn_slowpath_common(file, line, __builtin_return_address(0),
  409. TAINT_WARN, &args);
  410. va_end(args.args);
  411. }
  412. EXPORT_SYMBOL(warn_slowpath_fmt);
  413. void warn_slowpath_fmt_taint(const char *file, int line,
  414. unsigned taint, const char *fmt, ...)
  415. {
  416. struct slowpath_args args;
  417. args.fmt = fmt;
  418. va_start(args.args, fmt);
  419. warn_slowpath_common(file, line, __builtin_return_address(0),
  420. taint, &args);
  421. va_end(args.args);
  422. }
  423. EXPORT_SYMBOL(warn_slowpath_fmt_taint);
  424. void warn_slowpath_null(const char *file, int line)
  425. {
  426. warn_slowpath_common(file, line, __builtin_return_address(0),
  427. TAINT_WARN, NULL);
  428. }
  429. EXPORT_SYMBOL(warn_slowpath_null);
  430. #endif
  431. #ifdef CONFIG_CC_STACKPROTECTOR
  432. /*
  433. * Called when gcc's -fstack-protector feature is used, and
  434. * gcc detects corruption of the on-stack canary value
  435. */
  436. void __stack_chk_fail(void)
  437. {
  438. panic("stack-protector: Kernel stack is corrupted in: %p\n",
  439. __builtin_return_address(0));
  440. }
  441. EXPORT_SYMBOL(__stack_chk_fail);
  442. #endif
  443. core_param(panic, panic_timeout, int, 0644);
  444. core_param(pause_on_oops, pause_on_oops, int, 0644);
  445. static int __init oops_setup(char *s)
  446. {
  447. if (!s)
  448. return -EINVAL;
  449. if (!strcmp(s, "panic"))
  450. panic_on_oops = 1;
  451. return 0;
  452. }
  453. early_param("oops", oops_setup);