crash.c 7.2 KB

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