crash.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #define MAX_NOTE_BYTES 1024
  27. typedef u32 note_buf_t[MAX_NOTE_BYTES/4];
  28. note_buf_t crash_notes[NR_CPUS];
  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. static void crash_save_self(void)
  92. {
  93. struct pt_regs regs;
  94. int cpu;
  95. cpu = smp_processor_id();
  96. crash_get_current_regs(&regs);
  97. crash_save_this_cpu(&regs, cpu);
  98. }
  99. #ifdef CONFIG_SMP
  100. static atomic_t waiting_for_crash_ipi;
  101. static int crash_nmi_callback(struct pt_regs *regs, int cpu)
  102. {
  103. local_irq_disable();
  104. crash_save_this_cpu(regs, cpu);
  105. disable_local_APIC();
  106. atomic_dec(&waiting_for_crash_ipi);
  107. /* Assume hlt works */
  108. __asm__("hlt");
  109. for(;;);
  110. return 1;
  111. }
  112. /*
  113. * By using the NMI code instead of a vector we just sneak thru the
  114. * word generator coming out with just what we want. AND it does
  115. * not matter if clustered_apic_mode is set or not.
  116. */
  117. static void smp_send_nmi_allbutself(void)
  118. {
  119. send_IPI_allbutself(APIC_DM_NMI);
  120. }
  121. static void nmi_shootdown_cpus(void)
  122. {
  123. unsigned long msecs;
  124. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  125. /* Would it be better to replace the trap vector here? */
  126. set_nmi_callback(crash_nmi_callback);
  127. /* Ensure the new callback function is set before sending
  128. * out the NMI
  129. */
  130. wmb();
  131. smp_send_nmi_allbutself();
  132. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  133. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  134. mdelay(1);
  135. msecs--;
  136. }
  137. /* Leave the nmi callback set */
  138. disable_local_APIC();
  139. }
  140. #else
  141. static void nmi_shootdown_cpus(void)
  142. {
  143. /* There are no cpus to shootdown */
  144. }
  145. #endif
  146. void machine_crash_shutdown(void)
  147. {
  148. /* This function is only called after the system
  149. * has paniced or is otherwise in a critical state.
  150. * The minimum amount of code to allow a kexec'd kernel
  151. * to run successfully needs to happen here.
  152. *
  153. * In practice this means shooting down the other cpus in
  154. * an SMP system.
  155. */
  156. /* The kernel is broken so disable interrupts */
  157. local_irq_disable();
  158. nmi_shootdown_cpus();
  159. lapic_shutdown();
  160. #if defined(CONFIG_X86_IO_APIC)
  161. disable_IO_APIC();
  162. #endif
  163. crash_save_self();
  164. }