crash.c 3.3 KB

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