crash.c 2.3 KB

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