Thomas Gleixner | 40b0b3f | 2019-06-03 07:44:46 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Architecture specific (PPC64) functions for kexec based crash dumps. |
| 4 | * |
| 5 | * Copyright (C) 2005, IBM Corp. |
| 6 | * |
| 7 | * Created by: Haren Myneni |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 8 | */ |
| 9 | |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/smp.h> |
| 12 | #include <linux/reboot.h> |
| 13 | #include <linux/kexec.h> |
Paul Gortmaker | 66b15db | 2011-05-27 10:46:24 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 15 | #include <linux/crash_dump.h> |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 16 | #include <linux/delay.h> |
Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 17 | #include <linux/irq.h> |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | |
| 20 | #include <asm/processor.h> |
| 21 | #include <asm/machdep.h> |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 22 | #include <asm/kexec.h> |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 23 | #include <asm/prom.h> |
Haren Myneni | f6cc82f | 2006-01-10 19:25:25 -0800 | [diff] [blame] | 24 | #include <asm/smp.h> |
Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 25 | #include <asm/setjmp.h> |
David Howells | ae3a197 | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 26 | #include <asm/debug.h> |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 27 | |
Anton Blanchard | 549e88a | 2011-11-30 00:23:16 +0000 | [diff] [blame] | 28 | /* |
| 29 | * The primary CPU waits a while for all secondary CPUs to enter. This is to |
| 30 | * avoid sending an IPI if the secondary CPUs are entering |
| 31 | * crash_kexec_secondary on their own (eg via a system reset). |
| 32 | * |
| 33 | * The secondary timeout has to be longer than the primary. Both timeouts are |
| 34 | * in milliseconds. |
| 35 | */ |
| 36 | #define PRIMARY_TIMEOUT 500 |
| 37 | #define SECONDARY_TIMEOUT 1000 |
| 38 | |
| 39 | #define IPI_TIMEOUT 10000 |
| 40 | #define REAL_MODE_TIMEOUT 10000 |
| 41 | |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 42 | static int time_to_dump; |
Balbir Singh | 4145f35 | 2017-12-15 19:14:55 +1100 | [diff] [blame] | 43 | /* |
| 44 | * crash_wake_offline should be set to 1 by platforms that intend to wake |
| 45 | * up offline cpus prior to jumping to a kdump kernel. Currently powernv |
| 46 | * sets it to 1, since we want to avoid things from happening when an |
| 47 | * offline CPU wakes up due to something like an HMI (malfunction error), |
| 48 | * which propagates to all threads. |
| 49 | */ |
| 50 | int crash_wake_offline; |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 51 | |
Anton Blanchard | 158d5b5e | 2011-01-21 13:43:59 +1100 | [diff] [blame] | 52 | #define CRASH_HANDLER_MAX 3 |
Suraj Jitindar Singh | 1d14516 | 2016-05-11 10:57:32 +1000 | [diff] [blame] | 53 | /* List of shutdown handles */ |
| 54 | static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX]; |
Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 55 | static DEFINE_SPINLOCK(crash_handlers_lock); |
| 56 | |
Anton Blanchard | 07fe0c6 | 2011-11-30 00:23:11 +0000 | [diff] [blame] | 57 | static unsigned long crash_shutdown_buf[JMP_BUF_LEN]; |
| 58 | static int crash_shutdown_cpu = -1; |
| 59 | |
| 60 | static int handle_fault(struct pt_regs *regs) |
| 61 | { |
| 62 | if (crash_shutdown_cpu == smp_processor_id()) |
| 63 | longjmp(crash_shutdown_buf, 1); |
| 64 | return 0; |
| 65 | } |
| 66 | |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 67 | #ifdef CONFIG_SMP |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 68 | |
Christian Kujau | 897e01a | 2012-01-17 19:13:05 +0000 | [diff] [blame] | 69 | static atomic_t cpus_in_crash; |
Balbir Singh | 4145f35 | 2017-12-15 19:14:55 +1100 | [diff] [blame] | 70 | void crash_ipi_callback(struct pt_regs *regs) |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 71 | { |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 72 | static cpumask_t cpus_state_saved = CPU_MASK_NONE; |
| 73 | |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 74 | int cpu = smp_processor_id(); |
| 75 | |
Paul Mackerras | d04c56f | 2006-10-04 16:47:49 +1000 | [diff] [blame] | 76 | hard_irq_disable(); |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 77 | if (!cpumask_test_cpu(cpu, &cpus_state_saved)) { |
Magnus Damm | 85916f8 | 2006-12-06 20:40:41 -0800 | [diff] [blame] | 78 | crash_save_cpu(regs, cpu); |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 79 | cpumask_set_cpu(cpu, &cpus_state_saved); |
| 80 | } |
| 81 | |
| 82 | atomic_inc(&cpus_in_crash); |
Peter Zijlstra | c645073 | 2014-03-13 19:00:35 +0100 | [diff] [blame] | 83 | smp_mb__after_atomic(); |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 84 | |
| 85 | /* |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 86 | * Starting the kdump boot. |
| 87 | * This barrier is needed to make sure that all CPUs are stopped. |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 88 | */ |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 89 | while (!time_to_dump) |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 90 | cpu_relax(); |
| 91 | |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 92 | if (ppc_md.kexec_cpu_down) |
| 93 | ppc_md.kexec_cpu_down(1, 1); |
Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 94 | |
| 95 | #ifdef CONFIG_PPC64 |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 96 | kexec_smp_wait(); |
Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 97 | #else |
| 98 | for (;;); /* FIXME */ |
| 99 | #endif |
| 100 | |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 101 | /* NOTREACHED */ |
| 102 | } |
| 103 | |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 104 | static void crash_kexec_prepare_cpus(int cpu) |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 105 | { |
| 106 | unsigned int msecs; |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 107 | unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */ |
Anton Blanchard | 07fe0c6 | 2011-11-30 00:23:11 +0000 | [diff] [blame] | 108 | int tries = 0; |
| 109 | int (*old_handler)(struct pt_regs *regs); |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 110 | |
Anton Blanchard | 9b00ac0 | 2011-11-30 00:23:10 +0000 | [diff] [blame] | 111 | printk(KERN_EMERG "Sending IPI to other CPUs\n"); |
| 112 | |
Balbir Singh | 4145f35 | 2017-12-15 19:14:55 +1100 | [diff] [blame] | 113 | if (crash_wake_offline) |
| 114 | ncpus = num_present_cpus() - 1; |
| 115 | |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 116 | crash_send_ipi(crash_ipi_callback); |
| 117 | smp_wmb(); |
| 118 | |
Anton Blanchard | 07fe0c6 | 2011-11-30 00:23:11 +0000 | [diff] [blame] | 119 | again: |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 120 | /* |
Anton Blanchard | 158d5b5e | 2011-01-21 13:43:59 +1100 | [diff] [blame] | 121 | * FIXME: Until we will have the way to stop other CPUs reliably, |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 122 | * the crash CPU will send an IPI and wait for other CPUs to |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 123 | * respond. |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 124 | */ |
Anton Blanchard | 549e88a | 2011-11-30 00:23:16 +0000 | [diff] [blame] | 125 | msecs = IPI_TIMEOUT; |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 126 | while ((atomic_read(&cpus_in_crash) < ncpus) && (--msecs > 0)) |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 127 | mdelay(1); |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 128 | |
| 129 | /* Would it be better to replace the trap vector here? */ |
| 130 | |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 131 | if (atomic_read(&cpus_in_crash) >= ncpus) { |
Anton Blanchard | 07fe0c6 | 2011-11-30 00:23:11 +0000 | [diff] [blame] | 132 | printk(KERN_EMERG "IPI complete\n"); |
| 133 | return; |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 134 | } |
Anton Blanchard | 9b00ac0 | 2011-11-30 00:23:10 +0000 | [diff] [blame] | 135 | |
Anton Blanchard | 07fe0c6 | 2011-11-30 00:23:11 +0000 | [diff] [blame] | 136 | printk(KERN_EMERG "ERROR: %d cpu(s) not responding\n", |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 137 | ncpus - atomic_read(&cpus_in_crash)); |
Anton Blanchard | 07fe0c6 | 2011-11-30 00:23:11 +0000 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * If we have a panic timeout set then we can't wait indefinitely |
| 141 | * for someone to activate system reset. We also give up on the |
| 142 | * second time through if system reset fail to work. |
| 143 | */ |
| 144 | if ((panic_timeout > 0) || (tries > 0)) |
| 145 | return; |
| 146 | |
| 147 | /* |
| 148 | * A system reset will cause all CPUs to take an 0x100 exception. |
| 149 | * The primary CPU returns here via setjmp, and the secondary |
| 150 | * CPUs reexecute the crash_kexec_secondary path. |
| 151 | */ |
| 152 | old_handler = __debugger; |
| 153 | __debugger = handle_fault; |
| 154 | crash_shutdown_cpu = smp_processor_id(); |
| 155 | |
| 156 | if (setjmp(crash_shutdown_buf) == 0) { |
| 157 | printk(KERN_EMERG "Activate system reset (dumprestart) " |
| 158 | "to stop other cpu(s)\n"); |
| 159 | |
| 160 | /* |
| 161 | * A system reset will force all CPUs to execute the |
| 162 | * crash code again. We need to reset cpus_in_crash so we |
| 163 | * wait for everyone to do this. |
| 164 | */ |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 165 | atomic_set(&cpus_in_crash, 0); |
Anton Blanchard | 07fe0c6 | 2011-11-30 00:23:11 +0000 | [diff] [blame] | 166 | smp_mb(); |
| 167 | |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 168 | while (atomic_read(&cpus_in_crash) < ncpus) |
Anton Blanchard | 07fe0c6 | 2011-11-30 00:23:11 +0000 | [diff] [blame] | 169 | cpu_relax(); |
| 170 | } |
| 171 | |
| 172 | crash_shutdown_cpu = -1; |
| 173 | __debugger = old_handler; |
| 174 | |
| 175 | tries++; |
| 176 | goto again; |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 177 | } |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 178 | |
| 179 | /* |
Anton Blanchard | 9b00ac0 | 2011-11-30 00:23:10 +0000 | [diff] [blame] | 180 | * This function will be called by secondary cpus. |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 181 | */ |
| 182 | void crash_kexec_secondary(struct pt_regs *regs) |
| 183 | { |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 184 | unsigned long flags; |
Anton Blanchard | 549e88a | 2011-11-30 00:23:16 +0000 | [diff] [blame] | 185 | int msecs = SECONDARY_TIMEOUT; |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 186 | |
| 187 | local_irq_save(flags); |
Anton Blanchard | 9b00ac0 | 2011-11-30 00:23:10 +0000 | [diff] [blame] | 188 | |
Anton Blanchard | 549e88a | 2011-11-30 00:23:16 +0000 | [diff] [blame] | 189 | /* Wait for the primary crash CPU to signal its progress */ |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 190 | while (crashing_cpu < 0) { |
| 191 | if (--msecs < 0) { |
Anton Blanchard | 9b00ac0 | 2011-11-30 00:23:10 +0000 | [diff] [blame] | 192 | /* No response, kdump image may not have been loaded */ |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 193 | local_irq_restore(flags); |
| 194 | return; |
| 195 | } |
Anton Blanchard | 9b00ac0 | 2011-11-30 00:23:10 +0000 | [diff] [blame] | 196 | |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 197 | mdelay(1); |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 198 | } |
Anton Blanchard | 9b00ac0 | 2011-11-30 00:23:10 +0000 | [diff] [blame] | 199 | |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 200 | crash_ipi_callback(regs); |
| 201 | } |
| 202 | |
Paul Gortmaker | 7c7a81b | 2011-04-13 06:30:08 +0000 | [diff] [blame] | 203 | #else /* ! CONFIG_SMP */ |
Paul Gortmaker | 7c7a81b | 2011-04-13 06:30:08 +0000 | [diff] [blame] | 204 | |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 205 | static void crash_kexec_prepare_cpus(int cpu) |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 206 | { |
| 207 | /* |
Anton Blanchard | 8c27474 | 2011-11-30 00:23:12 +0000 | [diff] [blame] | 208 | * move the secondaries to us so that we can copy |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 209 | * the new kernel 0-0x100 safely |
| 210 | * |
| 211 | * do this if kexec in setup.c ? |
| 212 | */ |
Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 213 | #ifdef CONFIG_PPC64 |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 214 | smp_release_cpus(); |
Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 215 | #else |
| 216 | /* FIXME */ |
| 217 | #endif |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 218 | } |
| 219 | |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 220 | void crash_kexec_secondary(struct pt_regs *regs) |
| 221 | { |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 222 | } |
Paul Gortmaker | 7c7a81b | 2011-04-13 06:30:08 +0000 | [diff] [blame] | 223 | #endif /* CONFIG_SMP */ |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 224 | |
Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 225 | /* wait for all the CPUs to hit real mode but timeout if they don't come in */ |
Scott Wood | eeaab66 | 2015-10-06 22:48:16 -0500 | [diff] [blame] | 226 | #if defined(CONFIG_SMP) && defined(CONFIG_PPC64) |
| 227 | static void __maybe_unused crash_kexec_wait_realmode(int cpu) |
Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 228 | { |
| 229 | unsigned int msecs; |
| 230 | int i; |
| 231 | |
Anton Blanchard | 549e88a | 2011-11-30 00:23:16 +0000 | [diff] [blame] | 232 | msecs = REAL_MODE_TIMEOUT; |
Milton Miller | bd9e5ee | 2011-05-10 19:28:41 +0000 | [diff] [blame] | 233 | for (i=0; i < nr_cpu_ids && msecs > 0; i++) { |
Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 234 | if (i == cpu) |
| 235 | continue; |
| 236 | |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 237 | while (paca_ptrs[i]->kexec_state < KEXEC_STATE_REAL_MODE) { |
Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 238 | barrier(); |
Michael Neuling | 63f21a5 | 2011-07-04 20:40:10 +0000 | [diff] [blame] | 239 | if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0)) |
Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 240 | break; |
Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 241 | msecs--; |
| 242 | mdelay(1); |
| 243 | } |
| 244 | } |
| 245 | mb(); |
| 246 | } |
| 247 | #else |
| 248 | static inline void crash_kexec_wait_realmode(int cpu) {} |
Scott Wood | eeaab66 | 2015-10-06 22:48:16 -0500 | [diff] [blame] | 249 | #endif /* CONFIG_SMP && CONFIG_PPC64 */ |
Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 250 | |
Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 251 | /* |
| 252 | * Register a function to be called on shutdown. Only use this if you |
| 253 | * can't reset your device in the second kernel. |
| 254 | */ |
| 255 | int crash_shutdown_register(crash_shutdown_t handler) |
| 256 | { |
| 257 | unsigned int i, rc; |
| 258 | |
| 259 | spin_lock(&crash_handlers_lock); |
| 260 | for (i = 0 ; i < CRASH_HANDLER_MAX; i++) |
| 261 | if (!crash_shutdown_handles[i]) { |
| 262 | /* Insert handle at first empty entry */ |
| 263 | crash_shutdown_handles[i] = handler; |
| 264 | rc = 0; |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | if (i == CRASH_HANDLER_MAX) { |
| 269 | printk(KERN_ERR "Crash shutdown handles full, " |
| 270 | "not registered.\n"); |
| 271 | rc = 1; |
| 272 | } |
| 273 | |
| 274 | spin_unlock(&crash_handlers_lock); |
| 275 | return rc; |
| 276 | } |
| 277 | EXPORT_SYMBOL(crash_shutdown_register); |
| 278 | |
| 279 | int crash_shutdown_unregister(crash_shutdown_t handler) |
| 280 | { |
| 281 | unsigned int i, rc; |
| 282 | |
| 283 | spin_lock(&crash_handlers_lock); |
| 284 | for (i = 0 ; i < CRASH_HANDLER_MAX; i++) |
| 285 | if (crash_shutdown_handles[i] == handler) |
| 286 | break; |
| 287 | |
| 288 | if (i == CRASH_HANDLER_MAX) { |
| 289 | printk(KERN_ERR "Crash shutdown handle not found\n"); |
| 290 | rc = 1; |
| 291 | } else { |
| 292 | /* Shift handles down */ |
Suraj Jitindar Singh | 1d14516 | 2016-05-11 10:57:32 +1000 | [diff] [blame] | 293 | for (; i < (CRASH_HANDLER_MAX - 1); i++) |
Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 294 | crash_shutdown_handles[i] = |
| 295 | crash_shutdown_handles[i+1]; |
Suraj Jitindar Singh | 1d14516 | 2016-05-11 10:57:32 +1000 | [diff] [blame] | 296 | /* |
| 297 | * Reset last entry to NULL now that it has been shifted down, |
| 298 | * this will allow new handles to be added here. |
| 299 | */ |
| 300 | crash_shutdown_handles[i] = NULL; |
Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 301 | rc = 0; |
| 302 | } |
| 303 | |
| 304 | spin_unlock(&crash_handlers_lock); |
| 305 | return rc; |
| 306 | } |
| 307 | EXPORT_SYMBOL(crash_shutdown_unregister); |
| 308 | |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 309 | void default_machine_crash_shutdown(struct pt_regs *regs) |
| 310 | { |
Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 311 | unsigned int i; |
| 312 | int (*old_handler)(struct pt_regs *regs); |
| 313 | |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 314 | /* |
| 315 | * This function is only called after the system |
Lee Revell | f18190b | 2006-06-26 18:30:00 +0200 | [diff] [blame] | 316 | * has panicked or is otherwise in a critical state. |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 317 | * The minimum amount of code to allow a kexec'd kernel |
| 318 | * to run successfully needs to happen here. |
| 319 | * |
| 320 | * In practice this means stopping other cpus in |
| 321 | * an SMP system. |
| 322 | * The kernel is broken so disable interrupts. |
| 323 | */ |
Paul Mackerras | d04c56f | 2006-10-04 16:47:49 +1000 | [diff] [blame] | 324 | hard_irq_disable(); |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 325 | |
Anton Blanchard | 249ec22 | 2010-08-02 20:39:41 +0000 | [diff] [blame] | 326 | /* |
| 327 | * Make a note of crashing cpu. Will be used in machine_kexec |
| 328 | * such that another IPI will not be sent. |
| 329 | */ |
| 330 | crashing_cpu = smp_processor_id(); |
Anton Blanchard | 549e88a | 2011-11-30 00:23:16 +0000 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * If we came in via system reset, wait a while for the secondary |
| 334 | * CPUs to enter. |
| 335 | */ |
| 336 | if (TRAP(regs) == 0x100) |
| 337 | mdelay(PRIMARY_TIMEOUT); |
| 338 | |
Anton Blanchard | 249ec22 | 2010-08-02 20:39:41 +0000 | [diff] [blame] | 339 | crash_kexec_prepare_cpus(crashing_cpu); |
Anton Blanchard | 2440c01 | 2011-11-30 00:23:17 +0000 | [diff] [blame] | 340 | |
| 341 | crash_save_cpu(regs, crashing_cpu); |
| 342 | |
| 343 | time_to_dump = 1; |
| 344 | |
Anton Blanchard | 249ec22 | 2010-08-02 20:39:41 +0000 | [diff] [blame] | 345 | crash_kexec_wait_realmode(crashing_cpu); |
Anton Blanchard | 249ec22 | 2010-08-02 20:39:41 +0000 | [diff] [blame] | 346 | |
Matthew McClintock | c71635d | 2010-09-16 17:58:23 -0500 | [diff] [blame] | 347 | machine_kexec_mask_interrupts(); |
Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 348 | |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 349 | /* |
Anton Blanchard | 8c27474 | 2011-11-30 00:23:12 +0000 | [diff] [blame] | 350 | * Call registered shutdown routines safely. Swap out |
Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 351 | * __debugger_fault_handler, and replace on exit. |
| 352 | */ |
| 353 | old_handler = __debugger_fault_handler; |
| 354 | __debugger_fault_handler = handle_fault; |
Anton Blanchard | 0644079 | 2010-05-10 16:25:51 +0000 | [diff] [blame] | 355 | crash_shutdown_cpu = smp_processor_id(); |
Suraj Jitindar Singh | a7d6392 | 2016-07-11 14:17:31 +1000 | [diff] [blame] | 356 | for (i = 0; i < CRASH_HANDLER_MAX && crash_shutdown_handles[i]; i++) { |
Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 357 | if (setjmp(crash_shutdown_buf) == 0) { |
| 358 | /* |
| 359 | * Insert syncs and delay to ensure |
| 360 | * instructions in the dangerous region don't |
| 361 | * leak away from this protected region. |
| 362 | */ |
| 363 | asm volatile("sync; isync"); |
| 364 | /* dangerous region */ |
| 365 | crash_shutdown_handles[i](); |
| 366 | asm volatile("sync; isync"); |
| 367 | } |
| 368 | } |
Anton Blanchard | 0644079 | 2010-05-10 16:25:51 +0000 | [diff] [blame] | 369 | crash_shutdown_cpu = -1; |
Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 370 | __debugger_fault_handler = old_handler; |
| 371 | |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 372 | if (ppc_md.kexec_cpu_down) |
| 373 | ppc_md.kexec_cpu_down(1, 0); |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 374 | } |