crash.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/irq.h>
  14. #include <linux/reboot.h>
  15. #include <linux/kexec.h>
  16. #include <linux/irq.h>
  17. #include <linux/delay.h>
  18. #include <linux/elf.h>
  19. #include <linux/elfcore.h>
  20. #include <asm/processor.h>
  21. #include <asm/hardirq.h>
  22. #include <asm/nmi.h>
  23. #include <asm/hw_irq.h>
  24. #include <asm/apic.h>
  25. #include <mach_ipi.h>
  26. note_buf_t crash_notes[NR_CPUS];
  27. /* This keeps a track of which one is crashing cpu. */
  28. static int crashing_cpu;
  29. static u32 *append_elf_note(u32 *buf,
  30. char *name, unsigned type, void *data, size_t data_len)
  31. {
  32. struct elf_note note;
  33. note.n_namesz = strlen(name) + 1;
  34. note.n_descsz = data_len;
  35. note.n_type = type;
  36. memcpy(buf, &note, sizeof(note));
  37. buf += (sizeof(note) +3)/4;
  38. memcpy(buf, name, note.n_namesz);
  39. buf += (note.n_namesz + 3)/4;
  40. memcpy(buf, data, note.n_descsz);
  41. buf += (note.n_descsz + 3)/4;
  42. return buf;
  43. }
  44. static void final_note(u32 *buf)
  45. {
  46. struct elf_note note;
  47. note.n_namesz = 0;
  48. note.n_descsz = 0;
  49. note.n_type = 0;
  50. memcpy(buf, &note, sizeof(note));
  51. }
  52. static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
  53. {
  54. struct elf_prstatus prstatus;
  55. u32 *buf;
  56. if ((cpu < 0) || (cpu >= NR_CPUS)) {
  57. return;
  58. }
  59. /* Using ELF notes here is opportunistic.
  60. * I need a well defined structure format
  61. * for the data I pass, and I need tags
  62. * on the data to indicate what information I have
  63. * squirrelled away. ELF notes happen to provide
  64. * all of that that no need to invent something new.
  65. */
  66. buf = &crash_notes[cpu][0];
  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,
  71. &prstatus, sizeof(prstatus));
  72. final_note(buf);
  73. }
  74. static void crash_get_current_regs(struct pt_regs *regs)
  75. {
  76. __asm__ __volatile__("movl %%ebx,%0" : "=m"(regs->ebx));
  77. __asm__ __volatile__("movl %%ecx,%0" : "=m"(regs->ecx));
  78. __asm__ __volatile__("movl %%edx,%0" : "=m"(regs->edx));
  79. __asm__ __volatile__("movl %%esi,%0" : "=m"(regs->esi));
  80. __asm__ __volatile__("movl %%edi,%0" : "=m"(regs->edi));
  81. __asm__ __volatile__("movl %%ebp,%0" : "=m"(regs->ebp));
  82. __asm__ __volatile__("movl %%eax,%0" : "=m"(regs->eax));
  83. __asm__ __volatile__("movl %%esp,%0" : "=m"(regs->esp));
  84. __asm__ __volatile__("movw %%ss, %%ax;" :"=a"(regs->xss));
  85. __asm__ __volatile__("movw %%cs, %%ax;" :"=a"(regs->xcs));
  86. __asm__ __volatile__("movw %%ds, %%ax;" :"=a"(regs->xds));
  87. __asm__ __volatile__("movw %%es, %%ax;" :"=a"(regs->xes));
  88. __asm__ __volatile__("pushfl; popl %0" :"=m"(regs->eflags));
  89. regs->eip = (unsigned long)current_text_addr();
  90. }
  91. /* CPU does not save ss and esp on stack if execution is already
  92. * running in kernel mode at the time of NMI occurrence. This code
  93. * fixes it.
  94. */
  95. static void crash_setup_regs(struct pt_regs *newregs, struct pt_regs *oldregs)
  96. {
  97. memcpy(newregs, oldregs, sizeof(*newregs));
  98. newregs->esp = (unsigned long)&(oldregs->esp);
  99. __asm__ __volatile__("xorl %eax, %eax;");
  100. __asm__ __volatile__ ("movw %%ss, %%ax;" :"=a"(newregs->xss));
  101. }
  102. /* We may have saved_regs from where the error came from
  103. * or it is NULL if via a direct panic().
  104. */
  105. static void crash_save_self(struct pt_regs *saved_regs)
  106. {
  107. struct pt_regs regs;
  108. int cpu;
  109. cpu = smp_processor_id();
  110. if (saved_regs)
  111. crash_setup_regs(&regs, saved_regs);
  112. else
  113. crash_get_current_regs(&regs);
  114. crash_save_this_cpu(&regs, cpu);
  115. }
  116. #ifdef CONFIG_SMP
  117. static atomic_t waiting_for_crash_ipi;
  118. static int crash_nmi_callback(struct pt_regs *regs, int cpu)
  119. {
  120. struct pt_regs fixed_regs;
  121. /* Don't do anything if this handler is invoked on crashing cpu.
  122. * Otherwise, system will completely hang. Crashing cpu can get
  123. * an NMI if system was initially booted with nmi_watchdog parameter.
  124. */
  125. if (cpu == crashing_cpu)
  126. return 1;
  127. local_irq_disable();
  128. if (!user_mode(regs)) {
  129. crash_setup_regs(&fixed_regs, regs);
  130. regs = &fixed_regs;
  131. }
  132. crash_save_this_cpu(regs, cpu);
  133. disable_local_APIC();
  134. atomic_dec(&waiting_for_crash_ipi);
  135. /* Assume hlt works */
  136. __asm__("hlt");
  137. for(;;);
  138. return 1;
  139. }
  140. /*
  141. * By using the NMI code instead of a vector we just sneak thru the
  142. * word generator coming out with just what we want. AND it does
  143. * not matter if clustered_apic_mode is set or not.
  144. */
  145. static void smp_send_nmi_allbutself(void)
  146. {
  147. send_IPI_allbutself(APIC_DM_NMI);
  148. }
  149. static void nmi_shootdown_cpus(void)
  150. {
  151. unsigned long msecs;
  152. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  153. /* Would it be better to replace the trap vector here? */
  154. set_nmi_callback(crash_nmi_callback);
  155. /* Ensure the new callback function is set before sending
  156. * out the NMI
  157. */
  158. wmb();
  159. smp_send_nmi_allbutself();
  160. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  161. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  162. mdelay(1);
  163. msecs--;
  164. }
  165. /* Leave the nmi callback set */
  166. disable_local_APIC();
  167. }
  168. #else
  169. static void nmi_shootdown_cpus(void)
  170. {
  171. /* There are no cpus to shootdown */
  172. }
  173. #endif
  174. void machine_crash_shutdown(struct pt_regs *regs)
  175. {
  176. /* This function is only called after the system
  177. * has paniced or is otherwise in a critical state.
  178. * The minimum amount of code to allow a kexec'd kernel
  179. * to run successfully needs to happen here.
  180. *
  181. * In practice this means shooting down the other cpus in
  182. * an SMP system.
  183. */
  184. /* The kernel is broken so disable interrupts */
  185. local_irq_disable();
  186. /* Make a note of crashing cpu. Will be used in NMI callback.*/
  187. crashing_cpu = smp_processor_id();
  188. nmi_shootdown_cpus();
  189. lapic_shutdown();
  190. #if defined(CONFIG_X86_IO_APIC)
  191. disable_IO_APIC();
  192. #endif
  193. crash_save_self(regs);
  194. }