crash.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Architecture specific (PPC64) functions for kexec based crash dumps.
  3. *
  4. * Copyright (C) 2005, IBM Corp.
  5. *
  6. * Created by: Haren Myneni
  7. *
  8. * This source code is licensed under the GNU General Public License,
  9. * Version 2. See the file COPYING for more details.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/smp.h>
  14. #include <linux/reboot.h>
  15. #include <linux/kexec.h>
  16. #include <linux/export.h>
  17. #include <linux/crash_dump.h>
  18. #include <linux/delay.h>
  19. #include <linux/irq.h>
  20. #include <linux/types.h>
  21. #include <asm/processor.h>
  22. #include <asm/machdep.h>
  23. #include <asm/kexec.h>
  24. #include <asm/prom.h>
  25. #include <asm/smp.h>
  26. #include <asm/setjmp.h>
  27. #include <asm/debug.h>
  28. /*
  29. * The primary CPU waits a while for all secondary CPUs to enter. This is to
  30. * avoid sending an IPI if the secondary CPUs are entering
  31. * crash_kexec_secondary on their own (eg via a system reset).
  32. *
  33. * The secondary timeout has to be longer than the primary. Both timeouts are
  34. * in milliseconds.
  35. */
  36. #define PRIMARY_TIMEOUT 500
  37. #define SECONDARY_TIMEOUT 1000
  38. #define IPI_TIMEOUT 10000
  39. #define REAL_MODE_TIMEOUT 10000
  40. static int time_to_dump;
  41. /*
  42. * crash_wake_offline should be set to 1 by platforms that intend to wake
  43. * up offline cpus prior to jumping to a kdump kernel. Currently powernv
  44. * sets it to 1, since we want to avoid things from happening when an
  45. * offline CPU wakes up due to something like an HMI (malfunction error),
  46. * which propagates to all threads.
  47. */
  48. int crash_wake_offline;
  49. #define CRASH_HANDLER_MAX 3
  50. /* List of shutdown handles */
  51. static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX];
  52. static DEFINE_SPINLOCK(crash_handlers_lock);
  53. static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
  54. static int crash_shutdown_cpu = -1;
  55. static int handle_fault(struct pt_regs *regs)
  56. {
  57. if (crash_shutdown_cpu == smp_processor_id())
  58. longjmp(crash_shutdown_buf, 1);
  59. return 0;
  60. }
  61. #ifdef CONFIG_SMP
  62. static atomic_t cpus_in_crash;
  63. void crash_ipi_callback(struct pt_regs *regs)
  64. {
  65. static cpumask_t cpus_state_saved = CPU_MASK_NONE;
  66. int cpu = smp_processor_id();
  67. hard_irq_disable();
  68. if (!cpumask_test_cpu(cpu, &cpus_state_saved)) {
  69. crash_save_cpu(regs, cpu);
  70. cpumask_set_cpu(cpu, &cpus_state_saved);
  71. }
  72. atomic_inc(&cpus_in_crash);
  73. smp_mb__after_atomic();
  74. /*
  75. * Starting the kdump boot.
  76. * This barrier is needed to make sure that all CPUs are stopped.
  77. */
  78. while (!time_to_dump)
  79. cpu_relax();
  80. if (ppc_md.kexec_cpu_down)
  81. ppc_md.kexec_cpu_down(1, 1);
  82. #ifdef CONFIG_PPC64
  83. kexec_smp_wait();
  84. #else
  85. for (;;); /* FIXME */
  86. #endif
  87. /* NOTREACHED */
  88. }
  89. static void crash_kexec_prepare_cpus(int cpu)
  90. {
  91. unsigned int msecs;
  92. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  93. int tries = 0;
  94. int (*old_handler)(struct pt_regs *regs);
  95. printk(KERN_EMERG "Sending IPI to other CPUs\n");
  96. if (crash_wake_offline)
  97. ncpus = num_present_cpus() - 1;
  98. crash_send_ipi(crash_ipi_callback);
  99. smp_wmb();
  100. again:
  101. /*
  102. * FIXME: Until we will have the way to stop other CPUs reliably,
  103. * the crash CPU will send an IPI and wait for other CPUs to
  104. * respond.
  105. */
  106. msecs = IPI_TIMEOUT;
  107. while ((atomic_read(&cpus_in_crash) < ncpus) && (--msecs > 0))
  108. mdelay(1);
  109. /* Would it be better to replace the trap vector here? */
  110. if (atomic_read(&cpus_in_crash) >= ncpus) {
  111. printk(KERN_EMERG "IPI complete\n");
  112. return;
  113. }
  114. printk(KERN_EMERG "ERROR: %d cpu(s) not responding\n",
  115. ncpus - atomic_read(&cpus_in_crash));
  116. /*
  117. * If we have a panic timeout set then we can't wait indefinitely
  118. * for someone to activate system reset. We also give up on the
  119. * second time through if system reset fail to work.
  120. */
  121. if ((panic_timeout > 0) || (tries > 0))
  122. return;
  123. /*
  124. * A system reset will cause all CPUs to take an 0x100 exception.
  125. * The primary CPU returns here via setjmp, and the secondary
  126. * CPUs reexecute the crash_kexec_secondary path.
  127. */
  128. old_handler = __debugger;
  129. __debugger = handle_fault;
  130. crash_shutdown_cpu = smp_processor_id();
  131. if (setjmp(crash_shutdown_buf) == 0) {
  132. printk(KERN_EMERG "Activate system reset (dumprestart) "
  133. "to stop other cpu(s)\n");
  134. /*
  135. * A system reset will force all CPUs to execute the
  136. * crash code again. We need to reset cpus_in_crash so we
  137. * wait for everyone to do this.
  138. */
  139. atomic_set(&cpus_in_crash, 0);
  140. smp_mb();
  141. while (atomic_read(&cpus_in_crash) < ncpus)
  142. cpu_relax();
  143. }
  144. crash_shutdown_cpu = -1;
  145. __debugger = old_handler;
  146. tries++;
  147. goto again;
  148. }
  149. /*
  150. * This function will be called by secondary cpus.
  151. */
  152. void crash_kexec_secondary(struct pt_regs *regs)
  153. {
  154. unsigned long flags;
  155. int msecs = SECONDARY_TIMEOUT;
  156. local_irq_save(flags);
  157. /* Wait for the primary crash CPU to signal its progress */
  158. while (crashing_cpu < 0) {
  159. if (--msecs < 0) {
  160. /* No response, kdump image may not have been loaded */
  161. local_irq_restore(flags);
  162. return;
  163. }
  164. mdelay(1);
  165. }
  166. crash_ipi_callback(regs);
  167. }
  168. #else /* ! CONFIG_SMP */
  169. static void crash_kexec_prepare_cpus(int cpu)
  170. {
  171. /*
  172. * move the secondaries to us so that we can copy
  173. * the new kernel 0-0x100 safely
  174. *
  175. * do this if kexec in setup.c ?
  176. */
  177. #ifdef CONFIG_PPC64
  178. smp_release_cpus();
  179. #else
  180. /* FIXME */
  181. #endif
  182. }
  183. void crash_kexec_secondary(struct pt_regs *regs)
  184. {
  185. }
  186. #endif /* CONFIG_SMP */
  187. /* wait for all the CPUs to hit real mode but timeout if they don't come in */
  188. #if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
  189. static void __maybe_unused crash_kexec_wait_realmode(int cpu)
  190. {
  191. unsigned int msecs;
  192. int i;
  193. msecs = REAL_MODE_TIMEOUT;
  194. for (i=0; i < nr_cpu_ids && msecs > 0; i++) {
  195. if (i == cpu)
  196. continue;
  197. while (paca_ptrs[i]->kexec_state < KEXEC_STATE_REAL_MODE) {
  198. barrier();
  199. if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0))
  200. break;
  201. msecs--;
  202. mdelay(1);
  203. }
  204. }
  205. mb();
  206. }
  207. #else
  208. static inline void crash_kexec_wait_realmode(int cpu) {}
  209. #endif /* CONFIG_SMP && CONFIG_PPC64 */
  210. /*
  211. * Register a function to be called on shutdown. Only use this if you
  212. * can't reset your device in the second kernel.
  213. */
  214. int crash_shutdown_register(crash_shutdown_t handler)
  215. {
  216. unsigned int i, rc;
  217. spin_lock(&crash_handlers_lock);
  218. for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
  219. if (!crash_shutdown_handles[i]) {
  220. /* Insert handle at first empty entry */
  221. crash_shutdown_handles[i] = handler;
  222. rc = 0;
  223. break;
  224. }
  225. if (i == CRASH_HANDLER_MAX) {
  226. printk(KERN_ERR "Crash shutdown handles full, "
  227. "not registered.\n");
  228. rc = 1;
  229. }
  230. spin_unlock(&crash_handlers_lock);
  231. return rc;
  232. }
  233. EXPORT_SYMBOL(crash_shutdown_register);
  234. int crash_shutdown_unregister(crash_shutdown_t handler)
  235. {
  236. unsigned int i, rc;
  237. spin_lock(&crash_handlers_lock);
  238. for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
  239. if (crash_shutdown_handles[i] == handler)
  240. break;
  241. if (i == CRASH_HANDLER_MAX) {
  242. printk(KERN_ERR "Crash shutdown handle not found\n");
  243. rc = 1;
  244. } else {
  245. /* Shift handles down */
  246. for (; i < (CRASH_HANDLER_MAX - 1); i++)
  247. crash_shutdown_handles[i] =
  248. crash_shutdown_handles[i+1];
  249. /*
  250. * Reset last entry to NULL now that it has been shifted down,
  251. * this will allow new handles to be added here.
  252. */
  253. crash_shutdown_handles[i] = NULL;
  254. rc = 0;
  255. }
  256. spin_unlock(&crash_handlers_lock);
  257. return rc;
  258. }
  259. EXPORT_SYMBOL(crash_shutdown_unregister);
  260. void default_machine_crash_shutdown(struct pt_regs *regs)
  261. {
  262. unsigned int i;
  263. int (*old_handler)(struct pt_regs *regs);
  264. /*
  265. * This function is only called after the system
  266. * has panicked or is otherwise in a critical state.
  267. * The minimum amount of code to allow a kexec'd kernel
  268. * to run successfully needs to happen here.
  269. *
  270. * In practice this means stopping other cpus in
  271. * an SMP system.
  272. * The kernel is broken so disable interrupts.
  273. */
  274. hard_irq_disable();
  275. /*
  276. * Make a note of crashing cpu. Will be used in machine_kexec
  277. * such that another IPI will not be sent.
  278. */
  279. crashing_cpu = smp_processor_id();
  280. /*
  281. * If we came in via system reset, wait a while for the secondary
  282. * CPUs to enter.
  283. */
  284. if (TRAP(regs) == 0x100)
  285. mdelay(PRIMARY_TIMEOUT);
  286. crash_kexec_prepare_cpus(crashing_cpu);
  287. crash_save_cpu(regs, crashing_cpu);
  288. time_to_dump = 1;
  289. crash_kexec_wait_realmode(crashing_cpu);
  290. machine_kexec_mask_interrupts();
  291. /*
  292. * Call registered shutdown routines safely. Swap out
  293. * __debugger_fault_handler, and replace on exit.
  294. */
  295. old_handler = __debugger_fault_handler;
  296. __debugger_fault_handler = handle_fault;
  297. crash_shutdown_cpu = smp_processor_id();
  298. for (i = 0; i < CRASH_HANDLER_MAX && crash_shutdown_handles[i]; i++) {
  299. if (setjmp(crash_shutdown_buf) == 0) {
  300. /*
  301. * Insert syncs and delay to ensure
  302. * instructions in the dangerous region don't
  303. * leak away from this protected region.
  304. */
  305. asm volatile("sync; isync");
  306. /* dangerous region */
  307. crash_shutdown_handles[i]();
  308. asm volatile("sync; isync");
  309. }
  310. }
  311. crash_shutdown_cpu = -1;
  312. __debugger_fault_handler = old_handler;
  313. if (ppc_md.kexec_cpu_down)
  314. ppc_md.kexec_cpu_down(1, 0);
  315. }