crash.c 8.4 KB

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