crash.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #undef DEBUG
  13. #include <linux/kernel.h>
  14. #include <linux/smp.h>
  15. #include <linux/reboot.h>
  16. #include <linux/kexec.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/export.h>
  19. #include <linux/crash_dump.h>
  20. #include <linux/delay.h>
  21. #include <linux/elf.h>
  22. #include <linux/elfcore.h>
  23. #include <linux/init.h>
  24. #include <linux/irq.h>
  25. #include <linux/types.h>
  26. #include <linux/memblock.h>
  27. #include <asm/processor.h>
  28. #include <asm/machdep.h>
  29. #include <asm/kexec.h>
  30. #include <asm/kdump.h>
  31. #include <asm/prom.h>
  32. #include <asm/firmware.h>
  33. #include <asm/smp.h>
  34. #include <asm/system.h>
  35. #include <asm/setjmp.h>
  36. #ifdef DEBUG
  37. #include <asm/udbg.h>
  38. #define DBG(fmt...) udbg_printf(fmt)
  39. #else
  40. #define DBG(fmt...)
  41. #endif
  42. /* This keeps a track of which one is crashing cpu. */
  43. int crashing_cpu = -1;
  44. static cpumask_t cpus_in_crash = CPU_MASK_NONE;
  45. cpumask_t cpus_in_sr = CPU_MASK_NONE;
  46. #define CRASH_HANDLER_MAX 3
  47. /* NULL terminated list of shutdown handles */
  48. static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1];
  49. static DEFINE_SPINLOCK(crash_handlers_lock);
  50. #ifdef CONFIG_SMP
  51. static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
  52. void crash_ipi_callback(struct pt_regs *regs)
  53. {
  54. int cpu = smp_processor_id();
  55. if (!cpu_online(cpu))
  56. return;
  57. hard_irq_disable();
  58. if (!cpumask_test_cpu(cpu, &cpus_in_crash))
  59. crash_save_cpu(regs, cpu);
  60. cpumask_set_cpu(cpu, &cpus_in_crash);
  61. /*
  62. * Entered via soft-reset - could be the kdump
  63. * process is invoked using soft-reset or user activated
  64. * it if some CPU did not respond to an IPI.
  65. * For soft-reset, the secondary CPU can enter this func
  66. * twice. 1 - using IPI, and 2. soft-reset.
  67. * Tell the kexec CPU that entered via soft-reset and ready
  68. * to go down.
  69. */
  70. if (cpumask_test_cpu(cpu, &cpus_in_sr)) {
  71. cpumask_clear_cpu(cpu, &cpus_in_sr);
  72. atomic_inc(&enter_on_soft_reset);
  73. }
  74. /*
  75. * Starting the kdump boot.
  76. * This barrier is needed to make sure that all CPUs are stopped.
  77. * If not, soft-reset will be invoked to bring other CPUs.
  78. */
  79. while (!cpumask_test_cpu(crashing_cpu, &cpus_in_crash))
  80. cpu_relax();
  81. if (ppc_md.kexec_cpu_down)
  82. ppc_md.kexec_cpu_down(1, 1);
  83. #ifdef CONFIG_PPC64
  84. kexec_smp_wait();
  85. #else
  86. for (;;); /* FIXME */
  87. #endif
  88. /* NOTREACHED */
  89. }
  90. /*
  91. * Wait until all CPUs are entered via soft-reset.
  92. */
  93. static void crash_soft_reset_check(int cpu)
  94. {
  95. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  96. cpumask_clear_cpu(cpu, &cpus_in_sr);
  97. while (atomic_read(&enter_on_soft_reset) != ncpus)
  98. cpu_relax();
  99. }
  100. static void crash_kexec_prepare_cpus(int cpu)
  101. {
  102. unsigned int msecs;
  103. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  104. crash_send_ipi(crash_ipi_callback);
  105. smp_wmb();
  106. /*
  107. * FIXME: Until we will have the way to stop other CPUs reliably,
  108. * the crash CPU will send an IPI and wait for other CPUs to
  109. * respond.
  110. * Delay of at least 10 seconds.
  111. */
  112. printk(KERN_EMERG "Sending IPI to other cpus...\n");
  113. msecs = 10000;
  114. while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
  115. cpu_relax();
  116. mdelay(1);
  117. }
  118. /* Would it be better to replace the trap vector here? */
  119. /*
  120. * FIXME: In case if we do not get all CPUs, one possibility: ask the
  121. * user to do soft reset such that we get all.
  122. * Soft-reset will be used until better mechanism is implemented.
  123. */
  124. if (cpumask_weight(&cpus_in_crash) < ncpus) {
  125. printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
  126. ncpus - cpumask_weight(&cpus_in_crash));
  127. printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
  128. cpumask_clear(&cpus_in_sr);
  129. atomic_set(&enter_on_soft_reset, 0);
  130. while (cpumask_weight(&cpus_in_crash) < ncpus)
  131. cpu_relax();
  132. }
  133. /*
  134. * Make sure all CPUs are entered via soft-reset if the kdump is
  135. * invoked using soft-reset.
  136. */
  137. if (cpumask_test_cpu(cpu, &cpus_in_sr))
  138. crash_soft_reset_check(cpu);
  139. /* Leave the IPI callback set */
  140. }
  141. /*
  142. * This function will be called by secondary cpus or by kexec cpu
  143. * if soft-reset is activated to stop some CPUs.
  144. */
  145. void crash_kexec_secondary(struct pt_regs *regs)
  146. {
  147. int cpu = smp_processor_id();
  148. unsigned long flags;
  149. int msecs = 5;
  150. local_irq_save(flags);
  151. /* Wait 5ms if the kexec CPU is not entered yet. */
  152. while (crashing_cpu < 0) {
  153. if (--msecs < 0) {
  154. /*
  155. * Either kdump image is not loaded or
  156. * kdump process is not started - Probably xmon
  157. * exited using 'x'(exit and recover) or
  158. * kexec_should_crash() failed for all running tasks.
  159. */
  160. cpumask_clear_cpu(cpu, &cpus_in_sr);
  161. local_irq_restore(flags);
  162. return;
  163. }
  164. mdelay(1);
  165. cpu_relax();
  166. }
  167. if (cpu == crashing_cpu) {
  168. /*
  169. * Panic CPU will enter this func only via soft-reset.
  170. * Wait until all secondary CPUs entered and
  171. * then start kexec boot.
  172. */
  173. crash_soft_reset_check(cpu);
  174. cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
  175. if (ppc_md.kexec_cpu_down)
  176. ppc_md.kexec_cpu_down(1, 0);
  177. machine_kexec(kexec_crash_image);
  178. /* NOTREACHED */
  179. }
  180. crash_ipi_callback(regs);
  181. }
  182. #else /* ! CONFIG_SMP */
  183. static void crash_kexec_prepare_cpus(int cpu)
  184. {
  185. /*
  186. * move the secondarys to us so that we can copy
  187. * the new kernel 0-0x100 safely
  188. *
  189. * do this if kexec in setup.c ?
  190. */
  191. #ifdef CONFIG_PPC64
  192. smp_release_cpus();
  193. #else
  194. /* FIXME */
  195. #endif
  196. }
  197. void crash_kexec_secondary(struct pt_regs *regs)
  198. {
  199. cpumask_clear(&cpus_in_sr);
  200. }
  201. #endif /* CONFIG_SMP */
  202. /* wait for all the CPUs to hit real mode but timeout if they don't come in */
  203. #if defined(CONFIG_SMP) && defined(CONFIG_PPC_STD_MMU_64)
  204. static void crash_kexec_wait_realmode(int cpu)
  205. {
  206. unsigned int msecs;
  207. int i;
  208. msecs = 10000;
  209. for (i=0; i < nr_cpu_ids && msecs > 0; i++) {
  210. if (i == cpu)
  211. continue;
  212. while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
  213. barrier();
  214. if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0))
  215. break;
  216. msecs--;
  217. mdelay(1);
  218. }
  219. }
  220. mb();
  221. }
  222. #else
  223. static inline void crash_kexec_wait_realmode(int cpu) {}
  224. #endif /* CONFIG_SMP && CONFIG_PPC_STD_MMU_64 */
  225. /*
  226. * Register a function to be called on shutdown. Only use this if you
  227. * can't reset your device in the second kernel.
  228. */
  229. int crash_shutdown_register(crash_shutdown_t handler)
  230. {
  231. unsigned int i, rc;
  232. spin_lock(&crash_handlers_lock);
  233. for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
  234. if (!crash_shutdown_handles[i]) {
  235. /* Insert handle at first empty entry */
  236. crash_shutdown_handles[i] = handler;
  237. rc = 0;
  238. break;
  239. }
  240. if (i == CRASH_HANDLER_MAX) {
  241. printk(KERN_ERR "Crash shutdown handles full, "
  242. "not registered.\n");
  243. rc = 1;
  244. }
  245. spin_unlock(&crash_handlers_lock);
  246. return rc;
  247. }
  248. EXPORT_SYMBOL(crash_shutdown_register);
  249. int crash_shutdown_unregister(crash_shutdown_t handler)
  250. {
  251. unsigned int i, rc;
  252. spin_lock(&crash_handlers_lock);
  253. for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
  254. if (crash_shutdown_handles[i] == handler)
  255. break;
  256. if (i == CRASH_HANDLER_MAX) {
  257. printk(KERN_ERR "Crash shutdown handle not found\n");
  258. rc = 1;
  259. } else {
  260. /* Shift handles down */
  261. for (; crash_shutdown_handles[i]; i++)
  262. crash_shutdown_handles[i] =
  263. crash_shutdown_handles[i+1];
  264. rc = 0;
  265. }
  266. spin_unlock(&crash_handlers_lock);
  267. return rc;
  268. }
  269. EXPORT_SYMBOL(crash_shutdown_unregister);
  270. static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
  271. static int crash_shutdown_cpu = -1;
  272. static int handle_fault(struct pt_regs *regs)
  273. {
  274. if (crash_shutdown_cpu == smp_processor_id())
  275. longjmp(crash_shutdown_buf, 1);
  276. return 0;
  277. }
  278. void default_machine_crash_shutdown(struct pt_regs *regs)
  279. {
  280. unsigned int i;
  281. int (*old_handler)(struct pt_regs *regs);
  282. /*
  283. * This function is only called after the system
  284. * has panicked or is otherwise in a critical state.
  285. * The minimum amount of code to allow a kexec'd kernel
  286. * to run successfully needs to happen here.
  287. *
  288. * In practice this means stopping other cpus in
  289. * an SMP system.
  290. * The kernel is broken so disable interrupts.
  291. */
  292. hard_irq_disable();
  293. /*
  294. * Make a note of crashing cpu. Will be used in machine_kexec
  295. * such that another IPI will not be sent.
  296. */
  297. crashing_cpu = smp_processor_id();
  298. crash_save_cpu(regs, crashing_cpu);
  299. crash_kexec_prepare_cpus(crashing_cpu);
  300. cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
  301. crash_kexec_wait_realmode(crashing_cpu);
  302. machine_kexec_mask_interrupts();
  303. /*
  304. * Call registered shutdown routines savely. Swap out
  305. * __debugger_fault_handler, and replace on exit.
  306. */
  307. old_handler = __debugger_fault_handler;
  308. __debugger_fault_handler = handle_fault;
  309. crash_shutdown_cpu = smp_processor_id();
  310. for (i = 0; crash_shutdown_handles[i]; i++) {
  311. if (setjmp(crash_shutdown_buf) == 0) {
  312. /*
  313. * Insert syncs and delay to ensure
  314. * instructions in the dangerous region don't
  315. * leak away from this protected region.
  316. */
  317. asm volatile("sync; isync");
  318. /* dangerous region */
  319. crash_shutdown_handles[i]();
  320. asm volatile("sync; isync");
  321. }
  322. }
  323. crash_shutdown_cpu = -1;
  324. __debugger_fault_handler = old_handler;
  325. if (ppc_md.kexec_cpu_down)
  326. ppc_md.kexec_cpu_down(1, 0);
  327. }