crash.c 8.4 KB

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