2 * Architecture specific (PPC64) functions for kexec based crash dumps.
4 * Copyright (C) 2005, IBM Corp.
6 * Created by: Haren Myneni
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
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>
21 #include <linux/delay.h>
22 #include <linux/elf.h>
23 #include <linux/elfcore.h>
24 #include <linux/init.h>
25 #include <linux/irq.h>
26 #include <linux/types.h>
27 #include <linux/irq.h>
29 #include <asm/processor.h>
30 #include <asm/machdep.h>
31 #include <asm/kexec.h>
32 #include <asm/kdump.h>
34 #include <asm/firmware.h>
39 #define DBG(fmt...) udbg_printf(fmt)
44 /* This keeps a track of which one is crashing cpu. */
45 int crashing_cpu
= -1;
46 static cpumask_t cpus_in_crash
= CPU_MASK_NONE
;
47 cpumask_t cpus_in_sr
= CPU_MASK_NONE
;
49 static u32
*append_elf_note(u32
*buf
, char *name
, unsigned type
, void *data
,
54 note
.n_namesz
= strlen(name
) + 1;
55 note
.n_descsz
= data_len
;
57 memcpy(buf
, ¬e
, sizeof(note
));
58 buf
+= (sizeof(note
) +3)/4;
59 memcpy(buf
, name
, note
.n_namesz
);
60 buf
+= (note
.n_namesz
+ 3)/4;
61 memcpy(buf
, data
, note
.n_descsz
);
62 buf
+= (note
.n_descsz
+ 3)/4;
67 static void final_note(u32
*buf
)
74 memcpy(buf
, ¬e
, sizeof(note
));
77 static void crash_save_this_cpu(struct pt_regs
*regs
, int cpu
)
79 struct elf_prstatus prstatus
;
82 if ((cpu
< 0) || (cpu
>= NR_CPUS
))
85 /* Using ELF notes here is opportunistic.
86 * I need a well defined structure format
87 * for the data I pass, and I need tags
88 * on the data to indicate what information I have
89 * squirrelled away. ELF notes happen to provide
90 * all of that that no need to invent something new.
92 buf
= (u32
*)per_cpu_ptr(crash_notes
, cpu
);
96 memset(&prstatus
, 0, sizeof(prstatus
));
97 prstatus
.pr_pid
= current
->pid
;
98 elf_core_copy_regs(&prstatus
.pr_reg
, regs
);
99 buf
= append_elf_note(buf
, "CORE", NT_PRSTATUS
, &prstatus
,
105 static atomic_t enter_on_soft_reset
= ATOMIC_INIT(0);
107 void crash_ipi_callback(struct pt_regs
*regs
)
109 int cpu
= smp_processor_id();
111 if (!cpu_online(cpu
))
115 if (!cpu_isset(cpu
, cpus_in_crash
))
116 crash_save_this_cpu(regs
, cpu
);
117 cpu_set(cpu
, cpus_in_crash
);
120 * Entered via soft-reset - could be the kdump
121 * process is invoked using soft-reset or user activated
122 * it if some CPU did not respond to an IPI.
123 * For soft-reset, the secondary CPU can enter this func
124 * twice. 1 - using IPI, and 2. soft-reset.
125 * Tell the kexec CPU that entered via soft-reset and ready
128 if (cpu_isset(cpu
, cpus_in_sr
)) {
129 cpu_clear(cpu
, cpus_in_sr
);
130 atomic_inc(&enter_on_soft_reset
);
134 * Starting the kdump boot.
135 * This barrier is needed to make sure that all CPUs are stopped.
136 * If not, soft-reset will be invoked to bring other CPUs.
138 while (!cpu_isset(crashing_cpu
, cpus_in_crash
))
141 if (ppc_md
.kexec_cpu_down
)
142 ppc_md
.kexec_cpu_down(1, 1);
147 for (;;); /* FIXME */
154 * Wait until all CPUs are entered via soft-reset.
156 static void crash_soft_reset_check(int cpu
)
158 unsigned int ncpus
= num_online_cpus() - 1;/* Excluding the panic cpu */
160 cpu_clear(cpu
, cpus_in_sr
);
161 while (atomic_read(&enter_on_soft_reset
) != ncpus
)
166 static void crash_kexec_prepare_cpus(int cpu
)
170 unsigned int ncpus
= num_online_cpus() - 1;/* Excluding the panic cpu */
172 crash_send_ipi(crash_ipi_callback
);
176 * FIXME: Until we will have the way to stop other CPUSs reliabally,
177 * the crash CPU will send an IPI and wait for other CPUs to
179 * Delay of at least 10 seconds.
181 printk(KERN_EMERG
"Sending IPI to other cpus...\n");
183 while ((cpus_weight(cpus_in_crash
) < ncpus
) && (--msecs
> 0)) {
188 /* Would it be better to replace the trap vector here? */
191 * FIXME: In case if we do not get all CPUs, one possibility: ask the
192 * user to do soft reset such that we get all.
193 * Soft-reset will be used until better mechanism is implemented.
195 if (cpus_weight(cpus_in_crash
) < ncpus
) {
196 printk(KERN_EMERG
"done waiting: %d cpu(s) not responding\n",
197 ncpus
- cpus_weight(cpus_in_crash
));
198 printk(KERN_EMERG
"Activate soft-reset to stop other cpu(s)\n");
199 cpus_in_sr
= CPU_MASK_NONE
;
200 atomic_set(&enter_on_soft_reset
, 0);
201 while (cpus_weight(cpus_in_crash
) < ncpus
)
205 * Make sure all CPUs are entered via soft-reset if the kdump is
206 * invoked using soft-reset.
208 if (cpu_isset(cpu
, cpus_in_sr
))
209 crash_soft_reset_check(cpu
);
210 /* Leave the IPI callback set */
214 * This function will be called by secondary cpus or by kexec cpu
215 * if soft-reset is activated to stop some CPUs.
217 void crash_kexec_secondary(struct pt_regs
*regs
)
219 int cpu
= smp_processor_id();
223 local_irq_save(flags
);
224 /* Wait 5ms if the kexec CPU is not entered yet. */
225 while (crashing_cpu
< 0) {
228 * Either kdump image is not loaded or
229 * kdump process is not started - Probably xmon
230 * exited using 'x'(exit and recover) or
231 * kexec_should_crash() failed for all running tasks.
233 cpu_clear(cpu
, cpus_in_sr
);
234 local_irq_restore(flags
);
240 if (cpu
== crashing_cpu
) {
242 * Panic CPU will enter this func only via soft-reset.
243 * Wait until all secondary CPUs entered and
244 * then start kexec boot.
246 crash_soft_reset_check(cpu
);
247 cpu_set(crashing_cpu
, cpus_in_crash
);
248 if (ppc_md
.kexec_cpu_down
)
249 ppc_md
.kexec_cpu_down(1, 0);
250 machine_kexec(kexec_crash_image
);
253 crash_ipi_callback(regs
);
257 static void crash_kexec_prepare_cpus(int cpu
)
260 * move the secondarys to us so that we can copy
261 * the new kernel 0-0x100 safely
263 * do this if kexec in setup.c ?
272 void crash_kexec_secondary(struct pt_regs
*regs
)
274 cpus_in_sr
= CPU_MASK_NONE
;
278 void default_machine_crash_shutdown(struct pt_regs
*regs
)
283 * This function is only called after the system
284 * has panicked or is otherwise in a critical state.
285 * The minimum amount of code to allow a kexec'd kernel
286 * to run successfully needs to happen here.
288 * In practice this means stopping other cpus in
290 * The kernel is broken so disable interrupts.
295 struct irq_desc
*desc
= irq_desc
+ irq
;
297 if (desc
->status
& IRQ_INPROGRESS
)
298 desc
->chip
->eoi(irq
);
300 if (!(desc
->status
& IRQ_DISABLED
))
301 desc
->chip
->disable(irq
);
305 * Make a note of crashing cpu. Will be used in machine_kexec
306 * such that another IPI will not be sent.
308 crashing_cpu
= smp_processor_id();
309 crash_save_this_cpu(regs
, crashing_cpu
);
310 crash_kexec_prepare_cpus(crashing_cpu
);
311 cpu_set(crashing_cpu
, cpus_in_crash
);
312 if (ppc_md
.kexec_cpu_down
)
313 ppc_md
.kexec_cpu_down(1, 0);