Commit | Line | Data |
---|---|---|
cc532915 ME |
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> | |
cc532915 ME |
21 | #include <linux/delay.h> |
22 | #include <linux/elf.h> | |
23 | #include <linux/elfcore.h> | |
24 | #include <linux/init.h> | |
d6c1a908 | 25 | #include <linux/irq.h> |
cc532915 | 26 | #include <linux/types.h> |
95f72d1e | 27 | #include <linux/memblock.h> |
cc532915 ME |
28 | |
29 | #include <asm/processor.h> | |
30 | #include <asm/machdep.h> | |
c0ce7d08 | 31 | #include <asm/kexec.h> |
cc532915 | 32 | #include <asm/kdump.h> |
d9b2b2a2 | 33 | #include <asm/prom.h> |
cc532915 | 34 | #include <asm/firmware.h> |
f6cc82fc | 35 | #include <asm/smp.h> |
496b010e MN |
36 | #include <asm/system.h> |
37 | #include <asm/setjmp.h> | |
cc532915 ME |
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; | |
c0ce7d08 | 48 | static cpumask_t cpus_in_crash = CPU_MASK_NONE; |
b6f35b49 | 49 | cpumask_t cpus_in_sr = CPU_MASK_NONE; |
cc532915 | 50 | |
158d5b5e | 51 | #define CRASH_HANDLER_MAX 3 |
496b010e MN |
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 | ||
cc532915 | 56 | #ifdef CONFIG_SMP |
c0ce7d08 | 57 | static atomic_t enter_on_soft_reset = ATOMIC_INIT(0); |
cc532915 ME |
58 | |
59 | void crash_ipi_callback(struct pt_regs *regs) | |
60 | { | |
61 | int cpu = smp_processor_id(); | |
62 | ||
cc532915 ME |
63 | if (!cpu_online(cpu)) |
64 | return; | |
65 | ||
d04c56f7 | 66 | hard_irq_disable(); |
c0ce7d08 | 67 | if (!cpu_isset(cpu, cpus_in_crash)) |
85916f81 | 68 | crash_save_cpu(regs, cpu); |
c0ce7d08 | 69 | cpu_set(cpu, cpus_in_crash); |
cc532915 | 70 | |
c0ce7d08 DW |
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 | */ | |
80 | if (cpu_isset(cpu, cpus_in_sr)) { | |
81 | cpu_clear(cpu, cpus_in_sr); | |
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 | */ | |
90 | while (!cpu_isset(crashing_cpu, cpus_in_crash)) | |
91 | cpu_relax(); | |
92 | ||
93 | if (ppc_md.kexec_cpu_down) | |
94 | ppc_md.kexec_cpu_down(1, 1); | |
b6f35b49 ME |
95 | |
96 | #ifdef CONFIG_PPC64 | |
cc532915 | 97 | kexec_smp_wait(); |
b6f35b49 ME |
98 | #else |
99 | for (;;); /* FIXME */ | |
100 | #endif | |
101 | ||
cc532915 ME |
102 | /* NOTREACHED */ |
103 | } | |
104 | ||
c0ce7d08 DW |
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 | ||
112 | cpu_clear(cpu, cpus_in_sr); | |
113 | while (atomic_read(&enter_on_soft_reset) != ncpus) | |
114 | cpu_relax(); | |
115 | } | |
116 | ||
117 | ||
118 | static void crash_kexec_prepare_cpus(int cpu) | |
cc532915 ME |
119 | { |
120 | unsigned int msecs; | |
121 | ||
c0ce7d08 | 122 | unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */ |
cc532915 ME |
123 | |
124 | crash_send_ipi(crash_ipi_callback); | |
125 | smp_wmb(); | |
126 | ||
127 | /* | |
158d5b5e | 128 | * FIXME: Until we will have the way to stop other CPUs reliably, |
cc532915 | 129 | * the crash CPU will send an IPI and wait for other CPUs to |
c0ce7d08 | 130 | * respond. |
01aaed9d | 131 | * Delay of at least 10 seconds. |
cc532915 | 132 | */ |
c0ce7d08 | 133 | printk(KERN_EMERG "Sending IPI to other cpus...\n"); |
01aaed9d | 134 | msecs = 10000; |
c0ce7d08 DW |
135 | while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) { |
136 | cpu_relax(); | |
cc532915 ME |
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. | |
c0ce7d08 DW |
145 | * Soft-reset will be used until better mechanism is implemented. |
146 | */ | |
147 | if (cpus_weight(cpus_in_crash) < ncpus) { | |
148 | printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n", | |
149 | ncpus - cpus_weight(cpus_in_crash)); | |
150 | printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n"); | |
151 | cpus_in_sr = CPU_MASK_NONE; | |
152 | atomic_set(&enter_on_soft_reset, 0); | |
153 | while (cpus_weight(cpus_in_crash) < ncpus) | |
154 | cpu_relax(); | |
155 | } | |
156 | /* | |
157 | * Make sure all CPUs are entered via soft-reset if the kdump is | |
158 | * invoked using soft-reset. | |
cc532915 | 159 | */ |
c0ce7d08 DW |
160 | if (cpu_isset(cpu, cpus_in_sr)) |
161 | crash_soft_reset_check(cpu); | |
cc532915 ME |
162 | /* Leave the IPI callback set */ |
163 | } | |
c0ce7d08 | 164 | |
60adec62 | 165 | /* wait for all the CPUs to hit real mode but timeout if they don't come in */ |
7c7a81b5 | 166 | #ifdef CONFIG_PPC_STD_MMU_64 |
60adec62 MN |
167 | static void crash_kexec_wait_realmode(int cpu) |
168 | { | |
169 | unsigned int msecs; | |
170 | int i; | |
171 | ||
172 | msecs = 10000; | |
173 | for (i=0; i < NR_CPUS && msecs > 0; i++) { | |
174 | if (i == cpu) | |
175 | continue; | |
176 | ||
177 | while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) { | |
178 | barrier(); | |
179 | if (!cpu_possible(i)) { | |
180 | break; | |
181 | } | |
182 | if (!cpu_online(i)) { | |
183 | break; | |
184 | } | |
185 | msecs--; | |
186 | mdelay(1); | |
187 | } | |
188 | } | |
189 | mb(); | |
190 | } | |
7c7a81b5 | 191 | #endif /* CONFIG_PPC_STD_MMU_64 */ |
60adec62 | 192 | |
c0ce7d08 DW |
193 | /* |
194 | * This function will be called by secondary cpus or by kexec cpu | |
195 | * if soft-reset is activated to stop some CPUs. | |
196 | */ | |
197 | void crash_kexec_secondary(struct pt_regs *regs) | |
198 | { | |
199 | int cpu = smp_processor_id(); | |
200 | unsigned long flags; | |
201 | int msecs = 5; | |
202 | ||
203 | local_irq_save(flags); | |
204 | /* Wait 5ms if the kexec CPU is not entered yet. */ | |
205 | while (crashing_cpu < 0) { | |
206 | if (--msecs < 0) { | |
207 | /* | |
208 | * Either kdump image is not loaded or | |
209 | * kdump process is not started - Probably xmon | |
210 | * exited using 'x'(exit and recover) or | |
211 | * kexec_should_crash() failed for all running tasks. | |
212 | */ | |
213 | cpu_clear(cpu, cpus_in_sr); | |
214 | local_irq_restore(flags); | |
215 | return; | |
216 | } | |
217 | mdelay(1); | |
218 | cpu_relax(); | |
219 | } | |
220 | if (cpu == crashing_cpu) { | |
221 | /* | |
222 | * Panic CPU will enter this func only via soft-reset. | |
223 | * Wait until all secondary CPUs entered and | |
224 | * then start kexec boot. | |
225 | */ | |
226 | crash_soft_reset_check(cpu); | |
227 | cpu_set(crashing_cpu, cpus_in_crash); | |
228 | if (ppc_md.kexec_cpu_down) | |
229 | ppc_md.kexec_cpu_down(1, 0); | |
230 | machine_kexec(kexec_crash_image); | |
231 | /* NOTREACHED */ | |
232 | } | |
233 | crash_ipi_callback(regs); | |
234 | } | |
235 | ||
7c7a81b5 PG |
236 | #else /* ! CONFIG_SMP */ |
237 | static inline void crash_kexec_wait_realmode(int cpu) {} | |
238 | ||
c0ce7d08 | 239 | static void crash_kexec_prepare_cpus(int cpu) |
cc532915 ME |
240 | { |
241 | /* | |
242 | * move the secondarys to us so that we can copy | |
243 | * the new kernel 0-0x100 safely | |
244 | * | |
245 | * do this if kexec in setup.c ? | |
246 | */ | |
b6f35b49 | 247 | #ifdef CONFIG_PPC64 |
cc532915 | 248 | smp_release_cpus(); |
b6f35b49 ME |
249 | #else |
250 | /* FIXME */ | |
251 | #endif | |
cc532915 ME |
252 | } |
253 | ||
c0ce7d08 DW |
254 | void crash_kexec_secondary(struct pt_regs *regs) |
255 | { | |
256 | cpus_in_sr = CPU_MASK_NONE; | |
257 | } | |
7c7a81b5 | 258 | #endif /* CONFIG_SMP */ |
cc532915 | 259 | |
496b010e MN |
260 | /* |
261 | * Register a function to be called on shutdown. Only use this if you | |
262 | * can't reset your device in the second kernel. | |
263 | */ | |
264 | int crash_shutdown_register(crash_shutdown_t handler) | |
265 | { | |
266 | unsigned int i, rc; | |
267 | ||
268 | spin_lock(&crash_handlers_lock); | |
269 | for (i = 0 ; i < CRASH_HANDLER_MAX; i++) | |
270 | if (!crash_shutdown_handles[i]) { | |
271 | /* Insert handle at first empty entry */ | |
272 | crash_shutdown_handles[i] = handler; | |
273 | rc = 0; | |
274 | break; | |
275 | } | |
276 | ||
277 | if (i == CRASH_HANDLER_MAX) { | |
278 | printk(KERN_ERR "Crash shutdown handles full, " | |
279 | "not registered.\n"); | |
280 | rc = 1; | |
281 | } | |
282 | ||
283 | spin_unlock(&crash_handlers_lock); | |
284 | return rc; | |
285 | } | |
286 | EXPORT_SYMBOL(crash_shutdown_register); | |
287 | ||
288 | int crash_shutdown_unregister(crash_shutdown_t handler) | |
289 | { | |
290 | unsigned int i, rc; | |
291 | ||
292 | spin_lock(&crash_handlers_lock); | |
293 | for (i = 0 ; i < CRASH_HANDLER_MAX; i++) | |
294 | if (crash_shutdown_handles[i] == handler) | |
295 | break; | |
296 | ||
297 | if (i == CRASH_HANDLER_MAX) { | |
298 | printk(KERN_ERR "Crash shutdown handle not found\n"); | |
299 | rc = 1; | |
300 | } else { | |
301 | /* Shift handles down */ | |
302 | for (; crash_shutdown_handles[i]; i++) | |
303 | crash_shutdown_handles[i] = | |
304 | crash_shutdown_handles[i+1]; | |
305 | rc = 0; | |
306 | } | |
307 | ||
308 | spin_unlock(&crash_handlers_lock); | |
309 | return rc; | |
310 | } | |
311 | EXPORT_SYMBOL(crash_shutdown_unregister); | |
312 | ||
313 | static unsigned long crash_shutdown_buf[JMP_BUF_LEN]; | |
06440794 | 314 | static int crash_shutdown_cpu = -1; |
496b010e MN |
315 | |
316 | static int handle_fault(struct pt_regs *regs) | |
317 | { | |
06440794 AB |
318 | if (crash_shutdown_cpu == smp_processor_id()) |
319 | longjmp(crash_shutdown_buf, 1); | |
496b010e MN |
320 | return 0; |
321 | } | |
322 | ||
cc532915 ME |
323 | void default_machine_crash_shutdown(struct pt_regs *regs) |
324 | { | |
496b010e MN |
325 | unsigned int i; |
326 | int (*old_handler)(struct pt_regs *regs); | |
327 | ||
d6c1a908 | 328 | |
cc532915 ME |
329 | /* |
330 | * This function is only called after the system | |
f18190bd | 331 | * has panicked or is otherwise in a critical state. |
cc532915 ME |
332 | * The minimum amount of code to allow a kexec'd kernel |
333 | * to run successfully needs to happen here. | |
334 | * | |
335 | * In practice this means stopping other cpus in | |
336 | * an SMP system. | |
337 | * The kernel is broken so disable interrupts. | |
338 | */ | |
d04c56f7 | 339 | hard_irq_disable(); |
cc532915 | 340 | |
249ec228 AB |
341 | /* |
342 | * Make a note of crashing cpu. Will be used in machine_kexec | |
343 | * such that another IPI will not be sent. | |
344 | */ | |
345 | crashing_cpu = smp_processor_id(); | |
346 | crash_save_cpu(regs, crashing_cpu); | |
347 | crash_kexec_prepare_cpus(crashing_cpu); | |
348 | cpu_set(crashing_cpu, cpus_in_crash); | |
249ec228 | 349 | crash_kexec_wait_realmode(crashing_cpu); |
249ec228 | 350 | |
c71635d2 | 351 | machine_kexec_mask_interrupts(); |
496b010e MN |
352 | |
353 | /* | |
354 | * Call registered shutdown routines savely. Swap out | |
355 | * __debugger_fault_handler, and replace on exit. | |
356 | */ | |
357 | old_handler = __debugger_fault_handler; | |
358 | __debugger_fault_handler = handle_fault; | |
06440794 | 359 | crash_shutdown_cpu = smp_processor_id(); |
496b010e MN |
360 | for (i = 0; crash_shutdown_handles[i]; i++) { |
361 | if (setjmp(crash_shutdown_buf) == 0) { | |
362 | /* | |
363 | * Insert syncs and delay to ensure | |
364 | * instructions in the dangerous region don't | |
365 | * leak away from this protected region. | |
366 | */ | |
367 | asm volatile("sync; isync"); | |
368 | /* dangerous region */ | |
369 | crash_shutdown_handles[i](); | |
370 | asm volatile("sync; isync"); | |
371 | } | |
d6c1a908 | 372 | } |
06440794 | 373 | crash_shutdown_cpu = -1; |
496b010e | 374 | __debugger_fault_handler = old_handler; |
d6c1a908 | 375 | |
c0ce7d08 DW |
376 | if (ppc_md.kexec_cpu_down) |
377 | ppc_md.kexec_cpu_down(1, 0); | |
cc532915 | 378 | } |