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
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
16 #ifdef CONFIG_SMP
crash_shutdown_secondary(void * passed_regs)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 /*
23 * If we are passed registers, use those. Otherwise get the
24 * regs from the last interrupt, which should be correct, as
25 * we are in an interrupt. But if the regs are not there,
26 * pull them from the top of the stack. They are probably
27 * wrong, but we need something to keep from crashing again.
28 */
29 if (!regs)
30 regs = get_irq_regs();
31 if (!regs)
32 regs = task_pt_regs(current);
33
34 if (!cpu_online(cpu))
35 return;
36
37 /* We won't be sent IPIs any more. */
38 set_cpu_online(cpu, false);
39
40 local_irq_disable();
41 if (!cpumask_test_cpu(cpu, &cpus_in_crash))
42 crash_save_cpu(regs, cpu);
43 cpumask_set_cpu(cpu, &cpus_in_crash);
44
45 while (!atomic_read(&kexec_ready_to_reboot))
46 cpu_relax();
47 relocated_kexec_smp_wait(NULL);
48 /* NOTREACHED */
49 }
50
crash_kexec_prepare_cpus(void)51 static void crash_kexec_prepare_cpus(void)
52 {
53 static int cpus_stopped;
54 unsigned int msecs;
55 unsigned int ncpus;
56
57 if (cpus_stopped)
58 return;
59
60 ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
61
62 dump_send_ipi(crash_shutdown_secondary);
63 smp_wmb();
64
65 /*
66 * The crash CPU sends an IPI and wait for other CPUs to
67 * respond. Delay of at least 10 seconds.
68 */
69 pr_emerg("Sending IPI to other cpus...\n");
70 msecs = 10000;
71 while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
72 cpu_relax();
73 mdelay(1);
74 }
75
76 cpus_stopped = 1;
77 }
78
79 /* Override the weak function in kernel/panic.c */
crash_smp_send_stop(void)80 void crash_smp_send_stop(void)
81 {
82 if (_crash_smp_send_stop)
83 _crash_smp_send_stop();
84
85 crash_kexec_prepare_cpus();
86 }
87
88 #else /* !defined(CONFIG_SMP) */
crash_kexec_prepare_cpus(void)89 static void crash_kexec_prepare_cpus(void) {}
90 #endif /* !defined(CONFIG_SMP) */
91
default_machine_crash_shutdown(struct pt_regs * regs)92 void default_machine_crash_shutdown(struct pt_regs *regs)
93 {
94 local_irq_disable();
95 crashing_cpu = smp_processor_id();
96 crash_save_cpu(regs, crashing_cpu);
97 crash_kexec_prepare_cpus();
98 cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
99 }
100