crash.c 4.6 KB

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