crash.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. static u32 *append_elf_note(u32 *buf,
  28. char *name, unsigned type, 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. }
  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 that no need to invent something new.
  63. */
  64. buf = &crash_notes[cpu][0];
  65. memset(&prstatus, 0, sizeof(prstatus));
  66. prstatus.pr_pid = current->pid;
  67. elf_core_copy_regs(&prstatus.pr_reg, regs);
  68. buf = append_elf_note(buf, "CORE", NT_PRSTATUS,
  69. &prstatus, sizeof(prstatus));
  70. final_note(buf);
  71. }
  72. static void crash_get_current_regs(struct pt_regs *regs)
  73. {
  74. __asm__ __volatile__("movl %%ebx,%0" : "=m"(regs->ebx));
  75. __asm__ __volatile__("movl %%ecx,%0" : "=m"(regs->ecx));
  76. __asm__ __volatile__("movl %%edx,%0" : "=m"(regs->edx));
  77. __asm__ __volatile__("movl %%esi,%0" : "=m"(regs->esi));
  78. __asm__ __volatile__("movl %%edi,%0" : "=m"(regs->edi));
  79. __asm__ __volatile__("movl %%ebp,%0" : "=m"(regs->ebp));
  80. __asm__ __volatile__("movl %%eax,%0" : "=m"(regs->eax));
  81. __asm__ __volatile__("movl %%esp,%0" : "=m"(regs->esp));
  82. __asm__ __volatile__("movw %%ss, %%ax;" :"=a"(regs->xss));
  83. __asm__ __volatile__("movw %%cs, %%ax;" :"=a"(regs->xcs));
  84. __asm__ __volatile__("movw %%ds, %%ax;" :"=a"(regs->xds));
  85. __asm__ __volatile__("movw %%es, %%ax;" :"=a"(regs->xes));
  86. __asm__ __volatile__("pushfl; popl %0" :"=m"(regs->eflags));
  87. regs->eip = (unsigned long)current_text_addr();
  88. }
  89. static void crash_save_self(void)
  90. {
  91. struct pt_regs regs;
  92. int cpu;
  93. cpu = smp_processor_id();
  94. crash_get_current_regs(&regs);
  95. crash_save_this_cpu(&regs, cpu);
  96. }
  97. #ifdef CONFIG_SMP
  98. static atomic_t waiting_for_crash_ipi;
  99. static int crash_nmi_callback(struct pt_regs *regs, int cpu)
  100. {
  101. local_irq_disable();
  102. crash_save_this_cpu(regs, cpu);
  103. disable_local_APIC();
  104. atomic_dec(&waiting_for_crash_ipi);
  105. /* Assume hlt works */
  106. __asm__("hlt");
  107. for(;;);
  108. return 1;
  109. }
  110. /*
  111. * By using the NMI code instead of a vector we just sneak thru the
  112. * word generator coming out with just what we want. AND it does
  113. * not matter if clustered_apic_mode is set or not.
  114. */
  115. static void smp_send_nmi_allbutself(void)
  116. {
  117. send_IPI_allbutself(APIC_DM_NMI);
  118. }
  119. static void nmi_shootdown_cpus(void)
  120. {
  121. unsigned long msecs;
  122. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  123. /* Would it be better to replace the trap vector here? */
  124. set_nmi_callback(crash_nmi_callback);
  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(void)
  145. {
  146. /* This function is only called after the system
  147. * has paniced or is otherwise in a critical state.
  148. * The minimum amount of code to allow a kexec'd kernel
  149. * to run successfully needs to happen here.
  150. *
  151. * In practice this means shooting down the other cpus in
  152. * an SMP system.
  153. */
  154. /* The kernel is broken so disable interrupts */
  155. local_irq_disable();
  156. nmi_shootdown_cpus();
  157. lapic_shutdown();
  158. #if defined(CONFIG_X86_IO_APIC)
  159. disable_IO_APIC();
  160. #endif
  161. crash_save_self();
  162. }