crash.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Architecture specific (x86_64) functions for kexec based crash dumps.
  3. *
  4. * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
  5. *
  6. * Copyright (C) IBM Corporation, 2004. All rights reserved.
  7. *
  8. */
  9. #include <linux/init.h>
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/smp.h>
  13. #include <linux/irq.h>
  14. #include <linux/reboot.h>
  15. #include <linux/kexec.h>
  16. #include <linux/delay.h>
  17. #include <linux/elf.h>
  18. #include <linux/elfcore.h>
  19. #include <asm/processor.h>
  20. #include <asm/hardirq.h>
  21. #include <asm/nmi.h>
  22. #include <asm/hw_irq.h>
  23. #include <asm/mach_apic.h>
  24. #include <asm/kdebug.h>
  25. /* This keeps a track of which one is crashing cpu. */
  26. static int crashing_cpu;
  27. static u32 *append_elf_note(u32 *buf, char *name, unsigned type,
  28. void *data, size_t data_len)
  29. {
  30. struct elf_note note;
  31. note.n_namesz = strlen(name) + 1;
  32. note.n_descsz = data_len;
  33. note.n_type = type;
  34. memcpy(buf, &note, sizeof(note));
  35. buf += (sizeof(note) +3)/4;
  36. memcpy(buf, name, note.n_namesz);
  37. buf += (note.n_namesz + 3)/4;
  38. memcpy(buf, data, note.n_descsz);
  39. buf += (note.n_descsz + 3)/4;
  40. return buf;
  41. }
  42. static void final_note(u32 *buf)
  43. {
  44. struct elf_note note;
  45. note.n_namesz = 0;
  46. note.n_descsz = 0;
  47. note.n_type = 0;
  48. memcpy(buf, &note, sizeof(note));
  49. }
  50. static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
  51. {
  52. struct elf_prstatus prstatus;
  53. u32 *buf;
  54. if ((cpu < 0) || (cpu >= NR_CPUS))
  55. return;
  56. /* Using ELF notes here is opportunistic.
  57. * I need a well defined structure format
  58. * for the data I pass, and I need tags
  59. * on the data to indicate what information I have
  60. * squirrelled away. ELF notes happen to provide
  61. * all of that, no need to invent something new.
  62. */
  63. buf = (u32*)per_cpu_ptr(crash_notes, cpu);
  64. if (!buf)
  65. return;
  66. memset(&prstatus, 0, sizeof(prstatus));
  67. prstatus.pr_pid = current->pid;
  68. elf_core_copy_regs(&prstatus.pr_reg, regs);
  69. buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus,
  70. sizeof(prstatus));
  71. final_note(buf);
  72. }
  73. static void crash_save_self(struct pt_regs *regs)
  74. {
  75. int cpu;
  76. cpu = smp_processor_id();
  77. crash_save_this_cpu(regs, cpu);
  78. }
  79. #ifdef CONFIG_SMP
  80. static atomic_t waiting_for_crash_ipi;
  81. static int crash_nmi_callback(struct notifier_block *self,
  82. unsigned long val, void *data)
  83. {
  84. struct pt_regs *regs;
  85. int cpu;
  86. if (val != DIE_NMI_IPI)
  87. return NOTIFY_OK;
  88. regs = ((struct die_args *)data)->regs;
  89. cpu = raw_smp_processor_id();
  90. /*
  91. * Don't do anything if this handler is invoked on crashing cpu.
  92. * Otherwise, system will completely hang. Crashing cpu can get
  93. * an NMI if system was initially booted with nmi_watchdog parameter.
  94. */
  95. if (cpu == crashing_cpu)
  96. return NOTIFY_STOP;
  97. local_irq_disable();
  98. crash_save_this_cpu(regs, cpu);
  99. disable_local_APIC();
  100. atomic_dec(&waiting_for_crash_ipi);
  101. /* Assume hlt works */
  102. for(;;)
  103. halt();
  104. return 1;
  105. }
  106. static void smp_send_nmi_allbutself(void)
  107. {
  108. send_IPI_allbutself(NMI_VECTOR);
  109. }
  110. /*
  111. * This code is a best effort heuristic to get the
  112. * other cpus to stop executing. So races with
  113. * cpu hotplug shouldn't matter.
  114. */
  115. static struct notifier_block crash_nmi_nb = {
  116. .notifier_call = crash_nmi_callback,
  117. };
  118. static void nmi_shootdown_cpus(void)
  119. {
  120. unsigned long msecs;
  121. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  122. if (register_die_notifier(&crash_nmi_nb))
  123. return; /* return what? */
  124. /*
  125. * Ensure the new callback function is set before sending
  126. * out the NMI
  127. */
  128. wmb();
  129. smp_send_nmi_allbutself();
  130. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  131. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  132. mdelay(1);
  133. msecs--;
  134. }
  135. /* Leave the nmi callback set */
  136. disable_local_APIC();
  137. }
  138. #else
  139. static void nmi_shootdown_cpus(void)
  140. {
  141. /* There are no cpus to shootdown */
  142. }
  143. #endif
  144. void machine_crash_shutdown(struct pt_regs *regs)
  145. {
  146. /*
  147. * This function is only called after the system
  148. * has panicked or is otherwise in a critical state.
  149. * The minimum amount of code to allow a kexec'd kernel
  150. * to run successfully needs to happen here.
  151. *
  152. * In practice this means shooting down the other cpus in
  153. * an SMP system.
  154. */
  155. /* The kernel is broken so disable interrupts */
  156. local_irq_disable();
  157. /* Make a note of crashing cpu. Will be used in NMI callback.*/
  158. crashing_cpu = smp_processor_id();
  159. nmi_shootdown_cpus();
  160. if(cpu_has_apic)
  161. disable_local_APIC();
  162. disable_IO_APIC();
  163. crash_save_self(regs);
  164. }