crash.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Architecture specific (i386/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/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 <linux/kdebug.h>
  24. #include <asm/smp.h>
  25. #ifdef X86_32
  26. #include <mach_ipi.h>
  27. #else
  28. #include <asm/mach_apic.h>
  29. #endif
  30. /* This keeps a track of which one is crashing cpu. */
  31. static int crashing_cpu;
  32. #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
  33. static atomic_t waiting_for_crash_ipi;
  34. static int crash_nmi_callback(struct notifier_block *self,
  35. unsigned long val, void *data)
  36. {
  37. struct pt_regs *regs;
  38. #ifdef X86_32
  39. struct pt_regs fixed_regs;
  40. #endif
  41. int cpu;
  42. if (val != DIE_NMI_IPI)
  43. return NOTIFY_OK;
  44. regs = ((struct die_args *)data)->regs;
  45. cpu = raw_smp_processor_id();
  46. /* Don't do anything if this handler is invoked on crashing cpu.
  47. * Otherwise, system will completely hang. Crashing cpu can get
  48. * an NMI if system was initially booted with nmi_watchdog parameter.
  49. */
  50. if (cpu == crashing_cpu)
  51. return NOTIFY_STOP;
  52. local_irq_disable();
  53. #ifdef X86_32
  54. if (!user_mode_vm(regs)) {
  55. crash_fixup_ss_esp(&fixed_regs, regs);
  56. regs = &fixed_regs;
  57. }
  58. #endif
  59. crash_save_cpu(regs, cpu);
  60. disable_local_APIC();
  61. atomic_dec(&waiting_for_crash_ipi);
  62. /* Assume hlt works */
  63. halt();
  64. for (;;)
  65. cpu_relax();
  66. return 1;
  67. }
  68. static void smp_send_nmi_allbutself(void)
  69. {
  70. cpumask_t mask = cpu_online_map;
  71. cpu_clear(safe_smp_processor_id(), mask);
  72. if (!cpus_empty(mask))
  73. send_IPI_mask(mask, NMI_VECTOR);
  74. }
  75. static struct notifier_block crash_nmi_nb = {
  76. .notifier_call = crash_nmi_callback,
  77. };
  78. static void nmi_shootdown_cpus(void)
  79. {
  80. unsigned long msecs;
  81. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  82. /* Would it be better to replace the trap vector here? */
  83. if (register_die_notifier(&crash_nmi_nb))
  84. return; /* return what? */
  85. /* Ensure the new callback function is set before sending
  86. * out the NMI
  87. */
  88. wmb();
  89. smp_send_nmi_allbutself();
  90. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  91. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  92. mdelay(1);
  93. msecs--;
  94. }
  95. /* Leave the nmi callback set */
  96. disable_local_APIC();
  97. }
  98. #else
  99. static void nmi_shootdown_cpus(void)
  100. {
  101. /* There are no cpus to shootdown */
  102. }
  103. #endif
  104. void machine_crash_shutdown(struct pt_regs *regs)
  105. {
  106. /* This function is only called after the system
  107. * has panicked or is otherwise in a critical state.
  108. * The minimum amount of code to allow a kexec'd kernel
  109. * to run successfully needs to happen here.
  110. *
  111. * In practice this means shooting down the other cpus in
  112. * an SMP system.
  113. */
  114. /* The kernel is broken so disable interrupts */
  115. local_irq_disable();
  116. /* Make a note of crashing cpu. Will be used in NMI callback.*/
  117. crashing_cpu = safe_smp_processor_id();
  118. nmi_shootdown_cpus();
  119. lapic_shutdown();
  120. #if defined(CONFIG_X86_IO_APIC)
  121. disable_IO_APIC();
  122. #endif
  123. crash_save_cpu(regs, safe_smp_processor_id());
  124. }