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