crash.c 4.6 KB

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