Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 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> |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 8 | #include <linux/irq.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/sched.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame^] | 11 | #include <linux/sched/task_stack.h> |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 12 | |
| 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 | |
| 17 | #ifdef CONFIG_SMP |
Corey Minyard | c80e1b6 | 2016-04-11 09:10:19 -0500 | [diff] [blame] | 18 | static void crash_shutdown_secondary(void *passed_regs) |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 19 | { |
Corey Minyard | c80e1b6 | 2016-04-11 09:10:19 -0500 | [diff] [blame] | 20 | struct pt_regs *regs = passed_regs; |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 21 | int cpu = smp_processor_id(); |
| 22 | |
Corey Minyard | c80e1b6 | 2016-04-11 09:10:19 -0500 | [diff] [blame] | 23 | /* |
| 24 | * If we are passed registers, use those. Otherwise get the |
| 25 | * regs from the last interrupt, which should be correct, as |
| 26 | * we are in an interrupt. But if the regs are not there, |
| 27 | * pull them from the top of the stack. They are probably |
| 28 | * wrong, but we need something to keep from crashing again. |
| 29 | */ |
| 30 | if (!regs) |
| 31 | regs = get_irq_regs(); |
| 32 | if (!regs) |
| 33 | regs = task_pt_regs(current); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 34 | |
| 35 | if (!cpu_online(cpu)) |
| 36 | return; |
| 37 | |
| 38 | local_irq_disable(); |
Rusty Russell | 8dd9289 | 2015-03-05 10:49:17 +1030 | [diff] [blame] | 39 | if (!cpumask_test_cpu(cpu, &cpus_in_crash)) |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 40 | crash_save_cpu(regs, cpu); |
Rusty Russell | 8dd9289 | 2015-03-05 10:49:17 +1030 | [diff] [blame] | 41 | cpumask_set_cpu(cpu, &cpus_in_crash); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 42 | |
| 43 | while (!atomic_read(&kexec_ready_to_reboot)) |
| 44 | cpu_relax(); |
| 45 | relocated_kexec_smp_wait(NULL); |
| 46 | /* NOTREACHED */ |
| 47 | } |
| 48 | |
| 49 | static void crash_kexec_prepare_cpus(void) |
| 50 | { |
Hidehiro Kawai | 54c721b | 2016-10-11 13:54:26 -0700 | [diff] [blame] | 51 | static int cpus_stopped; |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 52 | unsigned int msecs; |
Hidehiro Kawai | 54c721b | 2016-10-11 13:54:26 -0700 | [diff] [blame] | 53 | unsigned int ncpus; |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 54 | |
Hidehiro Kawai | 54c721b | 2016-10-11 13:54:26 -0700 | [diff] [blame] | 55 | if (cpus_stopped) |
| 56 | return; |
| 57 | |
| 58 | ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */ |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 59 | |
Marcin Nowakowski | c83c2ee | 2016-12-02 09:58:28 +0100 | [diff] [blame] | 60 | smp_call_function(crash_shutdown_secondary, NULL, 0); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 61 | smp_wmb(); |
| 62 | |
| 63 | /* |
| 64 | * The crash CPU sends an IPI and wait for other CPUs to |
| 65 | * respond. Delay of at least 10 seconds. |
| 66 | */ |
| 67 | pr_emerg("Sending IPI to other cpus...\n"); |
| 68 | msecs = 10000; |
Rusty Russell | 8dd9289 | 2015-03-05 10:49:17 +1030 | [diff] [blame] | 69 | while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) { |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 70 | cpu_relax(); |
| 71 | mdelay(1); |
| 72 | } |
Hidehiro Kawai | 54c721b | 2016-10-11 13:54:26 -0700 | [diff] [blame] | 73 | |
| 74 | cpus_stopped = 1; |
| 75 | } |
| 76 | |
| 77 | /* Override the weak function in kernel/panic.c */ |
| 78 | void crash_smp_send_stop(void) |
| 79 | { |
| 80 | if (_crash_smp_send_stop) |
| 81 | _crash_smp_send_stop(); |
| 82 | |
| 83 | crash_kexec_prepare_cpus(); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | #else /* !defined(CONFIG_SMP) */ |
| 87 | static void crash_kexec_prepare_cpus(void) {} |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 88 | #endif /* !defined(CONFIG_SMP) */ |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 89 | |
| 90 | void default_machine_crash_shutdown(struct pt_regs *regs) |
| 91 | { |
| 92 | local_irq_disable(); |
| 93 | crashing_cpu = smp_processor_id(); |
| 94 | crash_save_cpu(regs, crashing_cpu); |
| 95 | crash_kexec_prepare_cpus(); |
Rusty Russell | 8dd9289 | 2015-03-05 10:49:17 +1030 | [diff] [blame] | 96 | cpumask_set_cpu(crashing_cpu, &cpus_in_crash); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 97 | } |