crash.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <linux/kernel.h>
  2. #include <linux/smp.h>
  3. #include <linux/reboot.h>
  4. #include <linux/kexec.h>
  5. #include <linux/bootmem.h>
  6. #include <linux/crash_dump.h>
  7. #include <linux/delay.h>
  8. #include <linux/irq.h>
  9. #include <linux/types.h>
  10. #include <linux/sched.h>
  11. /* This keeps a track of which one is crashing cpu. */
  12. static int crashing_cpu = -1;
  13. static cpumask_t cpus_in_crash = CPU_MASK_NONE;
  14. #ifdef CONFIG_SMP
  15. static void crash_shutdown_secondary(void *passed_regs)
  16. {
  17. struct pt_regs *regs = passed_regs;
  18. int cpu = smp_processor_id();
  19. /*
  20. * If we are passed registers, use those. Otherwise get the
  21. * regs from the last interrupt, which should be correct, as
  22. * we are in an interrupt. But if the regs are not there,
  23. * pull them from the top of the stack. They are probably
  24. * wrong, but we need something to keep from crashing again.
  25. */
  26. if (!regs)
  27. regs = get_irq_regs();
  28. if (!regs)
  29. regs = task_pt_regs(current);
  30. if (!cpu_online(cpu))
  31. return;
  32. local_irq_disable();
  33. if (!cpumask_test_cpu(cpu, &cpus_in_crash))
  34. crash_save_cpu(regs, cpu);
  35. cpumask_set_cpu(cpu, &cpus_in_crash);
  36. while (!atomic_read(&kexec_ready_to_reboot))
  37. cpu_relax();
  38. relocated_kexec_smp_wait(NULL);
  39. /* NOTREACHED */
  40. }
  41. static void crash_kexec_prepare_cpus(void)
  42. {
  43. static int cpus_stopped;
  44. unsigned int msecs;
  45. unsigned int ncpus;
  46. if (cpus_stopped)
  47. return;
  48. ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  49. dump_send_ipi(crash_shutdown_secondary);
  50. smp_wmb();
  51. /*
  52. * The crash CPU sends an IPI and wait for other CPUs to
  53. * respond. Delay of at least 10 seconds.
  54. */
  55. pr_emerg("Sending IPI to other cpus...\n");
  56. msecs = 10000;
  57. while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
  58. cpu_relax();
  59. mdelay(1);
  60. }
  61. cpus_stopped = 1;
  62. }
  63. /* Override the weak function in kernel/panic.c */
  64. void crash_smp_send_stop(void)
  65. {
  66. if (_crash_smp_send_stop)
  67. _crash_smp_send_stop();
  68. crash_kexec_prepare_cpus();
  69. }
  70. #else /* !defined(CONFIG_SMP) */
  71. static void crash_kexec_prepare_cpus(void) {}
  72. #endif /* !defined(CONFIG_SMP) */
  73. void default_machine_crash_shutdown(struct pt_regs *regs)
  74. {
  75. local_irq_disable();
  76. crashing_cpu = smp_processor_id();
  77. crash_save_cpu(regs, crashing_cpu);
  78. crash_kexec_prepare_cpus();
  79. cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
  80. }