| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 1 | /* | 
|  | 2 | * Architecture specific (PPC64) functions for kexec based crash dumps. | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2005, IBM Corp. | 
|  | 5 | * | 
|  | 6 | * Created by: Haren Myneni | 
|  | 7 | * | 
|  | 8 | * This source code is licensed under the GNU General Public License, | 
|  | 9 | * Version 2.  See the file COPYING for more details. | 
|  | 10 | * | 
|  | 11 | */ | 
|  | 12 |  | 
|  | 13 | #undef DEBUG | 
|  | 14 |  | 
|  | 15 | #include <linux/kernel.h> | 
|  | 16 | #include <linux/smp.h> | 
|  | 17 | #include <linux/reboot.h> | 
|  | 18 | #include <linux/kexec.h> | 
|  | 19 | #include <linux/bootmem.h> | 
|  | 20 | #include <linux/crash_dump.h> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 21 | #include <linux/delay.h> | 
|  | 22 | #include <linux/elf.h> | 
|  | 23 | #include <linux/elfcore.h> | 
|  | 24 | #include <linux/init.h> | 
| Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 25 | #include <linux/irq.h> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 26 | #include <linux/types.h> | 
| Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 27 | #include <linux/memblock.h> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 28 |  | 
|  | 29 | #include <asm/processor.h> | 
|  | 30 | #include <asm/machdep.h> | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 31 | #include <asm/kexec.h> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 32 | #include <asm/kdump.h> | 
| David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 33 | #include <asm/prom.h> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 34 | #include <asm/firmware.h> | 
| Haren Myneni | f6cc82f | 2006-01-10 19:25:25 -0800 | [diff] [blame] | 35 | #include <asm/smp.h> | 
| Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 36 | #include <asm/system.h> | 
|  | 37 | #include <asm/setjmp.h> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 38 |  | 
|  | 39 | #ifdef DEBUG | 
|  | 40 | #include <asm/udbg.h> | 
|  | 41 | #define DBG(fmt...) udbg_printf(fmt) | 
|  | 42 | #else | 
|  | 43 | #define DBG(fmt...) | 
|  | 44 | #endif | 
|  | 45 |  | 
|  | 46 | /* This keeps a track of which one is crashing cpu. */ | 
|  | 47 | int crashing_cpu = -1; | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 48 | static cpumask_t cpus_in_crash = CPU_MASK_NONE; | 
| Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 49 | cpumask_t cpus_in_sr = CPU_MASK_NONE; | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 50 |  | 
| Anton Blanchard | 158d5b5e | 2011-01-21 13:43:59 +1100 | [diff] [blame] | 51 | #define CRASH_HANDLER_MAX 3 | 
| Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 52 | /* NULL terminated list of shutdown handles */ | 
|  | 53 | static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1]; | 
|  | 54 | static DEFINE_SPINLOCK(crash_handlers_lock); | 
|  | 55 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 56 | #ifdef CONFIG_SMP | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 57 | static atomic_t enter_on_soft_reset = ATOMIC_INIT(0); | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 58 |  | 
|  | 59 | void crash_ipi_callback(struct pt_regs *regs) | 
|  | 60 | { | 
|  | 61 | int cpu = smp_processor_id(); | 
|  | 62 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 63 | if (!cpu_online(cpu)) | 
|  | 64 | return; | 
|  | 65 |  | 
| Paul Mackerras | d04c56f | 2006-10-04 16:47:49 +1000 | [diff] [blame] | 66 | hard_irq_disable(); | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 67 | if (!cpumask_test_cpu(cpu, &cpus_in_crash)) | 
| Magnus Damm | 85916f8 | 2006-12-06 20:40:41 -0800 | [diff] [blame] | 68 | crash_save_cpu(regs, cpu); | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 69 | cpumask_set_cpu(cpu, &cpus_in_crash); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 70 |  | 
|  | 71 | /* | 
|  | 72 | * Entered via soft-reset - could be the kdump | 
|  | 73 | * process is invoked using soft-reset or user activated | 
|  | 74 | * it if some CPU did not respond to an IPI. | 
|  | 75 | * For soft-reset, the secondary CPU can enter this func | 
|  | 76 | * twice. 1 - using IPI, and 2. soft-reset. | 
|  | 77 | * Tell the kexec CPU that entered via soft-reset and ready | 
|  | 78 | * to go down. | 
|  | 79 | */ | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 80 | if (cpumask_test_cpu(cpu, &cpus_in_sr)) { | 
|  | 81 | cpumask_clear_cpu(cpu, &cpus_in_sr); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 82 | atomic_inc(&enter_on_soft_reset); | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | /* | 
|  | 86 | * Starting the kdump boot. | 
|  | 87 | * This barrier is needed to make sure that all CPUs are stopped. | 
|  | 88 | * If not, soft-reset will be invoked to bring other CPUs. | 
|  | 89 | */ | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 90 | while (!cpumask_test_cpu(crashing_cpu, &cpus_in_crash)) | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 91 | cpu_relax(); | 
|  | 92 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 93 | if (ppc_md.kexec_cpu_down) | 
|  | 94 | ppc_md.kexec_cpu_down(1, 1); | 
| Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 95 |  | 
|  | 96 | #ifdef CONFIG_PPC64 | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 97 | kexec_smp_wait(); | 
| Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 98 | #else | 
|  | 99 | for (;;);	/* FIXME */ | 
|  | 100 | #endif | 
|  | 101 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 102 | /* NOTREACHED */ | 
|  | 103 | } | 
|  | 104 |  | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 105 | /* | 
|  | 106 | * Wait until all CPUs are entered via soft-reset. | 
|  | 107 | */ | 
|  | 108 | static void crash_soft_reset_check(int cpu) | 
|  | 109 | { | 
|  | 110 | unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */ | 
|  | 111 |  | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 112 | cpumask_clear_cpu(cpu, &cpus_in_sr); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 113 | while (atomic_read(&enter_on_soft_reset) != ncpus) | 
|  | 114 | cpu_relax(); | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 |  | 
|  | 118 | static void crash_kexec_prepare_cpus(int cpu) | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 119 | { | 
|  | 120 | unsigned int msecs; | 
|  | 121 |  | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 122 | unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */ | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 123 |  | 
|  | 124 | crash_send_ipi(crash_ipi_callback); | 
|  | 125 | smp_wmb(); | 
|  | 126 |  | 
|  | 127 | /* | 
| Anton Blanchard | 158d5b5e | 2011-01-21 13:43:59 +1100 | [diff] [blame] | 128 | * FIXME: Until we will have the way to stop other CPUs reliably, | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 129 | * 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] | 130 | * respond. | 
| Haren Myneni | 01aaed9 | 2006-02-07 15:47:03 -0800 | [diff] [blame] | 131 | * Delay of at least 10 seconds. | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 132 | */ | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 133 | printk(KERN_EMERG "Sending IPI to other cpus...\n"); | 
| Haren Myneni | 01aaed9 | 2006-02-07 15:47:03 -0800 | [diff] [blame] | 134 | msecs = 10000; | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 135 | while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) { | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 136 | cpu_relax(); | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 137 | mdelay(1); | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | /* Would it be better to replace the trap vector here? */ | 
|  | 141 |  | 
|  | 142 | /* | 
|  | 143 | * FIXME: In case if we do not get all CPUs, one possibility: ask the | 
|  | 144 | * user to do soft reset such that we get all. | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 145 | * Soft-reset will be used until better mechanism is implemented. | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 146 | */ | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 147 | if (cpumask_weight(&cpus_in_crash) < ncpus) { | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 148 | printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n", | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 149 | ncpus - cpumask_weight(&cpus_in_crash)); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 150 | printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n"); | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 151 | cpumask_clear(&cpus_in_sr); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 152 | atomic_set(&enter_on_soft_reset, 0); | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 153 | while (cpumask_weight(&cpus_in_crash) < ncpus) | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 154 | cpu_relax(); | 
|  | 155 | } | 
|  | 156 | /* | 
|  | 157 | * Make sure all CPUs are entered via soft-reset if the kdump is | 
|  | 158 | * invoked using soft-reset. | 
|  | 159 | */ | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 160 | if (cpumask_test_cpu(cpu, &cpus_in_sr)) | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 161 | crash_soft_reset_check(cpu); | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 162 | /* Leave the IPI callback set */ | 
|  | 163 | } | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 164 |  | 
|  | 165 | /* | 
|  | 166 | * This function will be called by secondary cpus or by kexec cpu | 
|  | 167 | * if soft-reset is activated to stop some CPUs. | 
|  | 168 | */ | 
|  | 169 | void crash_kexec_secondary(struct pt_regs *regs) | 
|  | 170 | { | 
|  | 171 | int cpu = smp_processor_id(); | 
|  | 172 | unsigned long flags; | 
|  | 173 | int msecs = 5; | 
|  | 174 |  | 
|  | 175 | local_irq_save(flags); | 
|  | 176 | /* Wait 5ms if the kexec CPU is not entered yet. */ | 
|  | 177 | while (crashing_cpu < 0) { | 
|  | 178 | if (--msecs < 0) { | 
|  | 179 | /* | 
|  | 180 | * Either kdump image is not loaded or | 
|  | 181 | * kdump process is not started - Probably xmon | 
|  | 182 | * exited using 'x'(exit and recover) or | 
|  | 183 | * kexec_should_crash() failed for all running tasks. | 
|  | 184 | */ | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 185 | cpumask_clear_cpu(cpu, &cpus_in_sr); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 186 | local_irq_restore(flags); | 
|  | 187 | return; | 
|  | 188 | } | 
|  | 189 | mdelay(1); | 
|  | 190 | cpu_relax(); | 
|  | 191 | } | 
|  | 192 | if (cpu == crashing_cpu) { | 
|  | 193 | /* | 
|  | 194 | * Panic CPU will enter this func only via soft-reset. | 
|  | 195 | * Wait until all secondary CPUs entered and | 
|  | 196 | * then start kexec boot. | 
|  | 197 | */ | 
|  | 198 | crash_soft_reset_check(cpu); | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 199 | cpumask_set_cpu(crashing_cpu, &cpus_in_crash); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 200 | if (ppc_md.kexec_cpu_down) | 
|  | 201 | ppc_md.kexec_cpu_down(1, 0); | 
|  | 202 | machine_kexec(kexec_crash_image); | 
|  | 203 | /* NOTREACHED */ | 
|  | 204 | } | 
|  | 205 | crash_ipi_callback(regs); | 
|  | 206 | } | 
|  | 207 |  | 
| Paul Gortmaker | 7c7a81b | 2011-04-13 06:30:08 +0000 | [diff] [blame] | 208 | #else	/* ! CONFIG_SMP */ | 
| Paul Gortmaker | 7c7a81b | 2011-04-13 06:30:08 +0000 | [diff] [blame] | 209 |  | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 210 | static void crash_kexec_prepare_cpus(int cpu) | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 211 | { | 
|  | 212 | /* | 
|  | 213 | * move the secondarys to us so that we can copy | 
|  | 214 | * the new kernel 0-0x100 safely | 
|  | 215 | * | 
|  | 216 | * do this if kexec in setup.c ? | 
|  | 217 | */ | 
| Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 218 | #ifdef CONFIG_PPC64 | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 219 | smp_release_cpus(); | 
| Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 220 | #else | 
|  | 221 | /* FIXME */ | 
|  | 222 | #endif | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 223 | } | 
|  | 224 |  | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 225 | void crash_kexec_secondary(struct pt_regs *regs) | 
|  | 226 | { | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 227 | cpumask_clear(&cpus_in_sr); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 228 | } | 
| Paul Gortmaker | 7c7a81b | 2011-04-13 06:30:08 +0000 | [diff] [blame] | 229 | #endif	/* CONFIG_SMP */ | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 230 |  | 
| Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 231 | /* wait for all the CPUs to hit real mode but timeout if they don't come in */ | 
|  | 232 | #if defined(CONFIG_SMP) && defined(CONFIG_PPC_STD_MMU_64) | 
|  | 233 | static void crash_kexec_wait_realmode(int cpu) | 
|  | 234 | { | 
|  | 235 | unsigned int msecs; | 
|  | 236 | int i; | 
|  | 237 |  | 
|  | 238 | msecs = 10000; | 
| Milton Miller | bd9e5ee | 2011-05-10 19:28:41 +0000 | [diff] [blame] | 239 | for (i=0; i < nr_cpu_ids && msecs > 0; i++) { | 
| Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 240 | if (i == cpu) | 
|  | 241 | continue; | 
|  | 242 |  | 
|  | 243 | while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) { | 
|  | 244 | barrier(); | 
| Michael Neuling | 63f21a5 | 2011-07-04 20:40:10 +0000 | [diff] [blame] | 245 | if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0)) | 
| Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 246 | break; | 
| Ben Hutchings | 7707e41 | 2011-04-24 15:04:31 +0000 | [diff] [blame] | 247 | msecs--; | 
|  | 248 | mdelay(1); | 
|  | 249 | } | 
|  | 250 | } | 
|  | 251 | mb(); | 
|  | 252 | } | 
|  | 253 | #else | 
|  | 254 | static inline void crash_kexec_wait_realmode(int cpu) {} | 
|  | 255 | #endif	/* CONFIG_SMP && CONFIG_PPC_STD_MMU_64 */ | 
|  | 256 |  | 
| Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 257 | /* | 
|  | 258 | * Register a function to be called on shutdown.  Only use this if you | 
|  | 259 | * can't reset your device in the second kernel. | 
|  | 260 | */ | 
|  | 261 | int crash_shutdown_register(crash_shutdown_t handler) | 
|  | 262 | { | 
|  | 263 | unsigned int i, rc; | 
|  | 264 |  | 
|  | 265 | spin_lock(&crash_handlers_lock); | 
|  | 266 | for (i = 0 ; i < CRASH_HANDLER_MAX; i++) | 
|  | 267 | if (!crash_shutdown_handles[i]) { | 
|  | 268 | /* Insert handle at first empty entry */ | 
|  | 269 | crash_shutdown_handles[i] = handler; | 
|  | 270 | rc = 0; | 
|  | 271 | break; | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | if (i == CRASH_HANDLER_MAX) { | 
|  | 275 | printk(KERN_ERR "Crash shutdown handles full, " | 
|  | 276 | "not registered.\n"); | 
|  | 277 | rc = 1; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | spin_unlock(&crash_handlers_lock); | 
|  | 281 | return rc; | 
|  | 282 | } | 
|  | 283 | EXPORT_SYMBOL(crash_shutdown_register); | 
|  | 284 |  | 
|  | 285 | int crash_shutdown_unregister(crash_shutdown_t handler) | 
|  | 286 | { | 
|  | 287 | unsigned int i, rc; | 
|  | 288 |  | 
|  | 289 | spin_lock(&crash_handlers_lock); | 
|  | 290 | for (i = 0 ; i < CRASH_HANDLER_MAX; i++) | 
|  | 291 | if (crash_shutdown_handles[i] == handler) | 
|  | 292 | break; | 
|  | 293 |  | 
|  | 294 | if (i == CRASH_HANDLER_MAX) { | 
|  | 295 | printk(KERN_ERR "Crash shutdown handle not found\n"); | 
|  | 296 | rc = 1; | 
|  | 297 | } else { | 
|  | 298 | /* Shift handles down */ | 
|  | 299 | for (; crash_shutdown_handles[i]; i++) | 
|  | 300 | crash_shutdown_handles[i] = | 
|  | 301 | crash_shutdown_handles[i+1]; | 
|  | 302 | rc = 0; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | spin_unlock(&crash_handlers_lock); | 
|  | 306 | return rc; | 
|  | 307 | } | 
|  | 308 | EXPORT_SYMBOL(crash_shutdown_unregister); | 
|  | 309 |  | 
|  | 310 | static unsigned long crash_shutdown_buf[JMP_BUF_LEN]; | 
| Anton Blanchard | 0644079 | 2010-05-10 16:25:51 +0000 | [diff] [blame] | 311 | static int crash_shutdown_cpu = -1; | 
| Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 312 |  | 
|  | 313 | static int handle_fault(struct pt_regs *regs) | 
|  | 314 | { | 
| Anton Blanchard | 0644079 | 2010-05-10 16:25:51 +0000 | [diff] [blame] | 315 | if (crash_shutdown_cpu == smp_processor_id()) | 
|  | 316 | longjmp(crash_shutdown_buf, 1); | 
| Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 317 | return 0; | 
|  | 318 | } | 
|  | 319 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 320 | void default_machine_crash_shutdown(struct pt_regs *regs) | 
|  | 321 | { | 
| Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 322 | unsigned int i; | 
|  | 323 | int (*old_handler)(struct pt_regs *regs); | 
|  | 324 |  | 
| Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 325 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 326 | /* | 
|  | 327 | * This function is only called after the system | 
| Lee Revell | f18190b | 2006-06-26 18:30:00 +0200 | [diff] [blame] | 328 | * has panicked or is otherwise in a critical state. | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 329 | * The minimum amount of code to allow a kexec'd kernel | 
|  | 330 | * to run successfully needs to happen here. | 
|  | 331 | * | 
|  | 332 | * In practice this means stopping other cpus in | 
|  | 333 | * an SMP system. | 
|  | 334 | * The kernel is broken so disable interrupts. | 
|  | 335 | */ | 
| Paul Mackerras | d04c56f | 2006-10-04 16:47:49 +1000 | [diff] [blame] | 336 | hard_irq_disable(); | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 337 |  | 
| Anton Blanchard | 249ec22 | 2010-08-02 20:39:41 +0000 | [diff] [blame] | 338 | /* | 
|  | 339 | * Make a note of crashing cpu. Will be used in machine_kexec | 
|  | 340 | * such that another IPI will not be sent. | 
|  | 341 | */ | 
|  | 342 | crashing_cpu = smp_processor_id(); | 
|  | 343 | crash_save_cpu(regs, crashing_cpu); | 
|  | 344 | crash_kexec_prepare_cpus(crashing_cpu); | 
| KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 345 | cpumask_set_cpu(crashing_cpu, &cpus_in_crash); | 
| Anton Blanchard | 249ec22 | 2010-08-02 20:39:41 +0000 | [diff] [blame] | 346 | crash_kexec_wait_realmode(crashing_cpu); | 
| Anton Blanchard | 249ec22 | 2010-08-02 20:39:41 +0000 | [diff] [blame] | 347 |  | 
| Matthew McClintock | c71635d | 2010-09-16 17:58:23 -0500 | [diff] [blame] | 348 | machine_kexec_mask_interrupts(); | 
| Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 349 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 350 | /* | 
| Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 351 | * Call registered shutdown routines savely.  Swap out | 
|  | 352 | * __debugger_fault_handler, and replace on exit. | 
|  | 353 | */ | 
|  | 354 | old_handler = __debugger_fault_handler; | 
|  | 355 | __debugger_fault_handler = handle_fault; | 
| Anton Blanchard | 0644079 | 2010-05-10 16:25:51 +0000 | [diff] [blame] | 356 | crash_shutdown_cpu = smp_processor_id(); | 
| Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 357 | for (i = 0; crash_shutdown_handles[i]; i++) { | 
|  | 358 | if (setjmp(crash_shutdown_buf) == 0) { | 
|  | 359 | /* | 
|  | 360 | * Insert syncs and delay to ensure | 
|  | 361 | * instructions in the dangerous region don't | 
|  | 362 | * leak away from this protected region. | 
|  | 363 | */ | 
|  | 364 | asm volatile("sync; isync"); | 
|  | 365 | /* dangerous region */ | 
|  | 366 | crash_shutdown_handles[i](); | 
|  | 367 | asm volatile("sync; isync"); | 
|  | 368 | } | 
|  | 369 | } | 
| Anton Blanchard | 0644079 | 2010-05-10 16:25:51 +0000 | [diff] [blame] | 370 | crash_shutdown_cpu = -1; | 
| Michael Neuling | 496b010 | 2008-01-18 15:50:30 +1100 | [diff] [blame] | 371 | __debugger_fault_handler = old_handler; | 
|  | 372 |  | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 373 | if (ppc_md.kexec_cpu_down) | 
|  | 374 | ppc_md.kexec_cpu_down(1, 0); | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 375 | } |