crash.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
  44. size_t data_len)
  45. {
  46. struct elf_note note;
  47. note.n_namesz = strlen(name) + 1;
  48. note.n_descsz = data_len;
  49. note.n_type = type;
  50. memcpy(buf, &note, sizeof(note));
  51. buf += (sizeof(note) +3)/4;
  52. memcpy(buf, name, note.n_namesz);
  53. buf += (note.n_namesz + 3)/4;
  54. memcpy(buf, data, note.n_descsz);
  55. buf += (note.n_descsz + 3)/4;
  56. return buf;
  57. }
  58. static void final_note(u32 *buf)
  59. {
  60. struct elf_note note;
  61. note.n_namesz = 0;
  62. note.n_descsz = 0;
  63. note.n_type = 0;
  64. memcpy(buf, &note, sizeof(note));
  65. }
  66. static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
  67. {
  68. struct elf_prstatus prstatus;
  69. u32 *buf;
  70. if ((cpu < 0) || (cpu >= NR_CPUS))
  71. return;
  72. /* Using ELF notes here is opportunistic.
  73. * I need a well defined structure format
  74. * for the data I pass, and I need tags
  75. * on the data to indicate what information I have
  76. * squirrelled away. ELF notes happen to provide
  77. * all of that that no need to invent something new.
  78. */
  79. buf = (u32*)per_cpu_ptr(crash_notes, cpu);
  80. if (!buf)
  81. return;
  82. memset(&prstatus, 0, sizeof(prstatus));
  83. prstatus.pr_pid = current->pid;
  84. elf_core_copy_regs(&prstatus.pr_reg, regs);
  85. buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus,
  86. sizeof(prstatus));
  87. final_note(buf);
  88. }
  89. #ifdef CONFIG_SMP
  90. static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
  91. void crash_ipi_callback(struct pt_regs *regs)
  92. {
  93. int cpu = smp_processor_id();
  94. if (!cpu_online(cpu))
  95. return;
  96. local_irq_disable();
  97. if (!cpu_isset(cpu, cpus_in_crash))
  98. crash_save_this_cpu(regs, cpu);
  99. cpu_set(cpu, cpus_in_crash);
  100. /*
  101. * Entered via soft-reset - could be the kdump
  102. * process is invoked using soft-reset or user activated
  103. * it if some CPU did not respond to an IPI.
  104. * For soft-reset, the secondary CPU can enter this func
  105. * twice. 1 - using IPI, and 2. soft-reset.
  106. * Tell the kexec CPU that entered via soft-reset and ready
  107. * to go down.
  108. */
  109. if (cpu_isset(cpu, cpus_in_sr)) {
  110. cpu_clear(cpu, cpus_in_sr);
  111. atomic_inc(&enter_on_soft_reset);
  112. }
  113. /*
  114. * Starting the kdump boot.
  115. * This barrier is needed to make sure that all CPUs are stopped.
  116. * If not, soft-reset will be invoked to bring other CPUs.
  117. */
  118. while (!cpu_isset(crashing_cpu, cpus_in_crash))
  119. cpu_relax();
  120. if (ppc_md.kexec_cpu_down)
  121. ppc_md.kexec_cpu_down(1, 1);
  122. #ifdef CONFIG_PPC64
  123. kexec_smp_wait();
  124. #else
  125. for (;;); /* FIXME */
  126. #endif
  127. /* NOTREACHED */
  128. }
  129. /*
  130. * Wait until all CPUs are entered via soft-reset.
  131. */
  132. static void crash_soft_reset_check(int cpu)
  133. {
  134. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  135. cpu_clear(cpu, cpus_in_sr);
  136. while (atomic_read(&enter_on_soft_reset) != ncpus)
  137. cpu_relax();
  138. }
  139. static void crash_kexec_prepare_cpus(int cpu)
  140. {
  141. unsigned int msecs;
  142. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  143. crash_send_ipi(crash_ipi_callback);
  144. smp_wmb();
  145. /*
  146. * FIXME: Until we will have the way to stop other CPUSs reliabally,
  147. * the crash CPU will send an IPI and wait for other CPUs to
  148. * respond.
  149. * Delay of at least 10 seconds.
  150. */
  151. printk(KERN_EMERG "Sending IPI to other cpus...\n");
  152. msecs = 10000;
  153. while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
  154. cpu_relax();
  155. mdelay(1);
  156. }
  157. /* Would it be better to replace the trap vector here? */
  158. /*
  159. * FIXME: In case if we do not get all CPUs, one possibility: ask the
  160. * user to do soft reset such that we get all.
  161. * Soft-reset will be used until better mechanism is implemented.
  162. */
  163. if (cpus_weight(cpus_in_crash) < ncpus) {
  164. printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
  165. ncpus - cpus_weight(cpus_in_crash));
  166. printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
  167. cpus_in_sr = CPU_MASK_NONE;
  168. atomic_set(&enter_on_soft_reset, 0);
  169. while (cpus_weight(cpus_in_crash) < ncpus)
  170. cpu_relax();
  171. }
  172. /*
  173. * Make sure all CPUs are entered via soft-reset if the kdump is
  174. * invoked using soft-reset.
  175. */
  176. if (cpu_isset(cpu, cpus_in_sr))
  177. crash_soft_reset_check(cpu);
  178. /* Leave the IPI callback set */
  179. }
  180. /*
  181. * This function will be called by secondary cpus or by kexec cpu
  182. * if soft-reset is activated to stop some CPUs.
  183. */
  184. void crash_kexec_secondary(struct pt_regs *regs)
  185. {
  186. int cpu = smp_processor_id();
  187. unsigned long flags;
  188. int msecs = 5;
  189. local_irq_save(flags);
  190. /* Wait 5ms if the kexec CPU is not entered yet. */
  191. while (crashing_cpu < 0) {
  192. if (--msecs < 0) {
  193. /*
  194. * Either kdump image is not loaded or
  195. * kdump process is not started - Probably xmon
  196. * exited using 'x'(exit and recover) or
  197. * kexec_should_crash() failed for all running tasks.
  198. */
  199. cpu_clear(cpu, cpus_in_sr);
  200. local_irq_restore(flags);
  201. return;
  202. }
  203. mdelay(1);
  204. cpu_relax();
  205. }
  206. if (cpu == crashing_cpu) {
  207. /*
  208. * Panic CPU will enter this func only via soft-reset.
  209. * Wait until all secondary CPUs entered and
  210. * then start kexec boot.
  211. */
  212. crash_soft_reset_check(cpu);
  213. cpu_set(crashing_cpu, cpus_in_crash);
  214. if (ppc_md.kexec_cpu_down)
  215. ppc_md.kexec_cpu_down(1, 0);
  216. machine_kexec(kexec_crash_image);
  217. /* NOTREACHED */
  218. }
  219. crash_ipi_callback(regs);
  220. }
  221. #else
  222. static void crash_kexec_prepare_cpus(int cpu)
  223. {
  224. /*
  225. * move the secondarys to us so that we can copy
  226. * the new kernel 0-0x100 safely
  227. *
  228. * do this if kexec in setup.c ?
  229. */
  230. #ifdef CONFIG_PPC64
  231. smp_release_cpus();
  232. #else
  233. /* FIXME */
  234. #endif
  235. }
  236. void crash_kexec_secondary(struct pt_regs *regs)
  237. {
  238. cpus_in_sr = CPU_MASK_NONE;
  239. }
  240. #endif
  241. void default_machine_crash_shutdown(struct pt_regs *regs)
  242. {
  243. unsigned int irq;
  244. /*
  245. * This function is only called after the system
  246. * has panicked or is otherwise in a critical state.
  247. * The minimum amount of code to allow a kexec'd kernel
  248. * to run successfully needs to happen here.
  249. *
  250. * In practice this means stopping other cpus in
  251. * an SMP system.
  252. * The kernel is broken so disable interrupts.
  253. */
  254. local_irq_disable();
  255. for_each_irq(irq) {
  256. struct irq_desc *desc = irq_desc + irq;
  257. if (desc->status & IRQ_INPROGRESS)
  258. desc->chip->eoi(irq);
  259. if (!(desc->status & IRQ_DISABLED))
  260. desc->chip->disable(irq);
  261. }
  262. /*
  263. * Make a note of crashing cpu. Will be used in machine_kexec
  264. * such that another IPI will not be sent.
  265. */
  266. crashing_cpu = smp_processor_id();
  267. crash_save_this_cpu(regs, crashing_cpu);
  268. crash_kexec_prepare_cpus(crashing_cpu);
  269. cpu_set(crashing_cpu, cpus_in_crash);
  270. if (ppc_md.kexec_cpu_down)
  271. ppc_md.kexec_cpu_down(1, 0);
  272. }