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