crash.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <asm/processor.h>
  26. #include <asm/machdep.h>
  27. #include <asm/kdump.h>
  28. #include <asm/lmb.h>
  29. #include <asm/firmware.h>
  30. #include <asm/smp.h>
  31. #ifdef DEBUG
  32. #include <asm/udbg.h>
  33. #define DBG(fmt...) udbg_printf(fmt)
  34. #else
  35. #define DBG(fmt...)
  36. #endif
  37. /* This keeps a track of which one is crashing cpu. */
  38. int crashing_cpu = -1;
  39. static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
  40. size_t data_len)
  41. {
  42. struct elf_note note;
  43. note.n_namesz = strlen(name) + 1;
  44. note.n_descsz = data_len;
  45. note.n_type = type;
  46. memcpy(buf, &note, sizeof(note));
  47. buf += (sizeof(note) +3)/4;
  48. memcpy(buf, name, note.n_namesz);
  49. buf += (note.n_namesz + 3)/4;
  50. memcpy(buf, data, note.n_descsz);
  51. buf += (note.n_descsz + 3)/4;
  52. return buf;
  53. }
  54. static void final_note(u32 *buf)
  55. {
  56. struct elf_note note;
  57. note.n_namesz = 0;
  58. note.n_descsz = 0;
  59. note.n_type = 0;
  60. memcpy(buf, &note, sizeof(note));
  61. }
  62. static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
  63. {
  64. struct elf_prstatus prstatus;
  65. u32 *buf;
  66. if ((cpu < 0) || (cpu >= NR_CPUS))
  67. return;
  68. /* Using ELF notes here is opportunistic.
  69. * I need a well defined structure format
  70. * for the data I pass, and I need tags
  71. * on the data to indicate what information I have
  72. * squirrelled away. ELF notes happen to provide
  73. * all of that that no need to invent something new.
  74. */
  75. buf = (u32*)per_cpu_ptr(crash_notes, cpu);
  76. if (!buf)
  77. return;
  78. memset(&prstatus, 0, sizeof(prstatus));
  79. prstatus.pr_pid = current->pid;
  80. elf_core_copy_regs(&prstatus.pr_reg, regs);
  81. buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus,
  82. sizeof(prstatus));
  83. final_note(buf);
  84. }
  85. #ifdef CONFIG_SMP
  86. static atomic_t waiting_for_crash_ipi;
  87. void crash_ipi_callback(struct pt_regs *regs)
  88. {
  89. int cpu = smp_processor_id();
  90. if (cpu == crashing_cpu)
  91. return;
  92. if (!cpu_online(cpu))
  93. return;
  94. if (ppc_md.kexec_cpu_down)
  95. ppc_md.kexec_cpu_down(1, 1);
  96. local_irq_disable();
  97. crash_save_this_cpu(regs, cpu);
  98. atomic_dec(&waiting_for_crash_ipi);
  99. kexec_smp_wait();
  100. /* NOTREACHED */
  101. }
  102. static void crash_kexec_prepare_cpus(void)
  103. {
  104. unsigned int msecs;
  105. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  106. crash_send_ipi(crash_ipi_callback);
  107. smp_wmb();
  108. /*
  109. * FIXME: Until we will have the way to stop other CPUSs reliabally,
  110. * the crash CPU will send an IPI and wait for other CPUs to
  111. * respond. If not, proceed the kexec boot even though we failed to
  112. * capture other CPU states.
  113. * Delay of at least 10 seconds.
  114. */
  115. printk(KERN_ALERT "Sending IPI to other cpus...\n");
  116. msecs = 10000;
  117. while ((atomic_read(&waiting_for_crash_ipi) > 0) && (--msecs > 0)) {
  118. barrier();
  119. mdelay(1);
  120. }
  121. /* Would it be better to replace the trap vector here? */
  122. /*
  123. * FIXME: In case if we do not get all CPUs, one possibility: ask the
  124. * user to do soft reset such that we get all.
  125. * IPI handler is already set by the panic cpu initially. Therefore,
  126. * all cpus could invoke this handler from die() and the panic CPU
  127. * will call machine_kexec() directly from this handler to do
  128. * kexec boot.
  129. */
  130. if (atomic_read(&waiting_for_crash_ipi))
  131. printk(KERN_ALERT "done waiting: %d cpus not responding\n",
  132. atomic_read(&waiting_for_crash_ipi));
  133. /* Leave the IPI callback set */
  134. }
  135. #else
  136. static void crash_kexec_prepare_cpus(void)
  137. {
  138. /*
  139. * move the secondarys to us so that we can copy
  140. * the new kernel 0-0x100 safely
  141. *
  142. * do this if kexec in setup.c ?
  143. */
  144. smp_release_cpus();
  145. }
  146. #endif
  147. void default_machine_crash_shutdown(struct pt_regs *regs)
  148. {
  149. unsigned int irq;
  150. /*
  151. * This function is only called after the system
  152. * has paniced or is otherwise in a critical state.
  153. * The minimum amount of code to allow a kexec'd kernel
  154. * to run successfully needs to happen here.
  155. *
  156. * In practice this means stopping other cpus in
  157. * an SMP system.
  158. * The kernel is broken so disable interrupts.
  159. */
  160. local_irq_disable();
  161. for_each_irq(irq) {
  162. struct irq_desc *desc = irq_descp(irq);
  163. if (desc->status & IRQ_INPROGRESS)
  164. desc->handler->end(irq);
  165. if (!(desc->status & IRQ_DISABLED))
  166. desc->handler->disable(irq);
  167. }
  168. if (ppc_md.kexec_cpu_down)
  169. ppc_md.kexec_cpu_down(1, 0);
  170. /*
  171. * Make a note of crashing cpu. Will be used in machine_kexec
  172. * such that another IPI will not be sent.
  173. */
  174. crashing_cpu = smp_processor_id();
  175. crash_kexec_prepare_cpus();
  176. crash_save_this_cpu(regs, crashing_cpu);
  177. }