crash.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/crash_dump.h>
  19. #include <linux/delay.h>
  20. #include <linux/elf.h>
  21. #include <linux/elfcore.h>
  22. #include <linux/init.h>
  23. #include <linux/irq.h>
  24. #include <linux/types.h>
  25. #include <linux/irq.h>
  26. #include <asm/processor.h>
  27. #include <asm/machdep.h>
  28. #include <asm/kexec.h>
  29. #include <asm/kdump.h>
  30. #include <asm/lmb.h>
  31. #include <asm/firmware.h>
  32. #include <asm/smp.h>
  33. #ifdef DEBUG
  34. #include <asm/udbg.h>
  35. #define DBG(fmt...) udbg_printf(fmt)
  36. #else
  37. #define DBG(fmt...)
  38. #endif
  39. /* This keeps a track of which one is crashing cpu. */
  40. int crashing_cpu = -1;
  41. static cpumask_t cpus_in_crash = CPU_MASK_NONE;
  42. cpumask_t cpus_in_sr = CPU_MASK_NONE;
  43. #ifdef CONFIG_SMP
  44. static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
  45. void crash_ipi_callback(struct pt_regs *regs)
  46. {
  47. int cpu = smp_processor_id();
  48. if (!cpu_online(cpu))
  49. return;
  50. hard_irq_disable();
  51. if (!cpu_isset(cpu, cpus_in_crash))
  52. crash_save_cpu(regs, cpu);
  53. cpu_set(cpu, cpus_in_crash);
  54. /*
  55. * Entered via soft-reset - could be the kdump
  56. * process is invoked using soft-reset or user activated
  57. * it if some CPU did not respond to an IPI.
  58. * For soft-reset, the secondary CPU can enter this func
  59. * twice. 1 - using IPI, and 2. soft-reset.
  60. * Tell the kexec CPU that entered via soft-reset and ready
  61. * to go down.
  62. */
  63. if (cpu_isset(cpu, cpus_in_sr)) {
  64. cpu_clear(cpu, cpus_in_sr);
  65. atomic_inc(&enter_on_soft_reset);
  66. }
  67. /*
  68. * Starting the kdump boot.
  69. * This barrier is needed to make sure that all CPUs are stopped.
  70. * If not, soft-reset will be invoked to bring other CPUs.
  71. */
  72. while (!cpu_isset(crashing_cpu, cpus_in_crash))
  73. cpu_relax();
  74. if (ppc_md.kexec_cpu_down)
  75. ppc_md.kexec_cpu_down(1, 1);
  76. #ifdef CONFIG_PPC64
  77. kexec_smp_wait();
  78. #else
  79. for (;;); /* FIXME */
  80. #endif
  81. /* NOTREACHED */
  82. }
  83. /*
  84. * Wait until all CPUs are entered via soft-reset.
  85. */
  86. static void crash_soft_reset_check(int cpu)
  87. {
  88. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  89. cpu_clear(cpu, cpus_in_sr);
  90. while (atomic_read(&enter_on_soft_reset) != ncpus)
  91. cpu_relax();
  92. }
  93. static void crash_kexec_prepare_cpus(int cpu)
  94. {
  95. unsigned int msecs;
  96. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  97. crash_send_ipi(crash_ipi_callback);
  98. smp_wmb();
  99. /*
  100. * FIXME: Until we will have the way to stop other CPUSs reliabally,
  101. * the crash CPU will send an IPI and wait for other CPUs to
  102. * respond.
  103. * Delay of at least 10 seconds.
  104. */
  105. printk(KERN_EMERG "Sending IPI to other cpus...\n");
  106. msecs = 10000;
  107. while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
  108. cpu_relax();
  109. mdelay(1);
  110. }
  111. /* Would it be better to replace the trap vector here? */
  112. /*
  113. * FIXME: In case if we do not get all CPUs, one possibility: ask the
  114. * user to do soft reset such that we get all.
  115. * Soft-reset will be used until better mechanism is implemented.
  116. */
  117. if (cpus_weight(cpus_in_crash) < ncpus) {
  118. printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
  119. ncpus - cpus_weight(cpus_in_crash));
  120. printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
  121. cpus_in_sr = CPU_MASK_NONE;
  122. atomic_set(&enter_on_soft_reset, 0);
  123. while (cpus_weight(cpus_in_crash) < ncpus)
  124. cpu_relax();
  125. }
  126. /*
  127. * Make sure all CPUs are entered via soft-reset if the kdump is
  128. * invoked using soft-reset.
  129. */
  130. if (cpu_isset(cpu, cpus_in_sr))
  131. crash_soft_reset_check(cpu);
  132. /* Leave the IPI callback set */
  133. }
  134. /*
  135. * This function will be called by secondary cpus or by kexec cpu
  136. * if soft-reset is activated to stop some CPUs.
  137. */
  138. void crash_kexec_secondary(struct pt_regs *regs)
  139. {
  140. int cpu = smp_processor_id();
  141. unsigned long flags;
  142. int msecs = 5;
  143. local_irq_save(flags);
  144. /* Wait 5ms if the kexec CPU is not entered yet. */
  145. while (crashing_cpu < 0) {
  146. if (--msecs < 0) {
  147. /*
  148. * Either kdump image is not loaded or
  149. * kdump process is not started - Probably xmon
  150. * exited using 'x'(exit and recover) or
  151. * kexec_should_crash() failed for all running tasks.
  152. */
  153. cpu_clear(cpu, cpus_in_sr);
  154. local_irq_restore(flags);
  155. return;
  156. }
  157. mdelay(1);
  158. cpu_relax();
  159. }
  160. if (cpu == crashing_cpu) {
  161. /*
  162. * Panic CPU will enter this func only via soft-reset.
  163. * Wait until all secondary CPUs entered and
  164. * then start kexec boot.
  165. */
  166. crash_soft_reset_check(cpu);
  167. cpu_set(crashing_cpu, cpus_in_crash);
  168. if (ppc_md.kexec_cpu_down)
  169. ppc_md.kexec_cpu_down(1, 0);
  170. machine_kexec(kexec_crash_image);
  171. /* NOTREACHED */
  172. }
  173. crash_ipi_callback(regs);
  174. }
  175. #else
  176. static void crash_kexec_prepare_cpus(int cpu)
  177. {
  178. /*
  179. * move the secondarys to us so that we can copy
  180. * the new kernel 0-0x100 safely
  181. *
  182. * do this if kexec in setup.c ?
  183. */
  184. #ifdef CONFIG_PPC64
  185. smp_release_cpus();
  186. #else
  187. /* FIXME */
  188. #endif
  189. }
  190. void crash_kexec_secondary(struct pt_regs *regs)
  191. {
  192. cpus_in_sr = CPU_MASK_NONE;
  193. }
  194. #endif
  195. void default_machine_crash_shutdown(struct pt_regs *regs)
  196. {
  197. unsigned int irq;
  198. /*
  199. * This function is only called after the system
  200. * has panicked or is otherwise in a critical state.
  201. * The minimum amount of code to allow a kexec'd kernel
  202. * to run successfully needs to happen here.
  203. *
  204. * In practice this means stopping other cpus in
  205. * an SMP system.
  206. * The kernel is broken so disable interrupts.
  207. */
  208. hard_irq_disable();
  209. for_each_irq(irq) {
  210. struct irq_desc *desc = irq_desc + irq;
  211. if (desc->status & IRQ_INPROGRESS)
  212. desc->chip->eoi(irq);
  213. if (!(desc->status & IRQ_DISABLED))
  214. desc->chip->disable(irq);
  215. }
  216. /*
  217. * Make a note of crashing cpu. Will be used in machine_kexec
  218. * such that another IPI will not be sent.
  219. */
  220. crashing_cpu = smp_processor_id();
  221. crash_save_cpu(regs, crashing_cpu);
  222. crash_kexec_prepare_cpus(crashing_cpu);
  223. cpu_set(crashing_cpu, cpus_in_crash);
  224. if (ppc_md.kexec_cpu_down)
  225. ppc_md.kexec_cpu_down(1, 0);
  226. }