2 * Architecture specific (i386) functions for kexec based crash dumps.
4 * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
6 * Copyright (C) IBM Corporation, 2004. All rights reserved.
10 #include <linux/init.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/smp.h>
14 #include <linux/reboot.h>
15 #include <linux/kexec.h>
16 #include <linux/delay.h>
17 #include <linux/elf.h>
18 #include <linux/elfcore.h>
20 #include <asm/processor.h>
21 #include <asm/hardirq.h>
23 #include <asm/hw_irq.h>
28 /* This keeps a track of which one is crashing cpu. */
29 static int crashing_cpu
;
31 static u32
*append_elf_note(u32
*buf
, char *name
, unsigned type
, void *data
,
36 note
.n_namesz
= strlen(name
) + 1;
37 note
.n_descsz
= data_len
;
39 memcpy(buf
, ¬e
, sizeof(note
));
40 buf
+= (sizeof(note
) +3)/4;
41 memcpy(buf
, name
, note
.n_namesz
);
42 buf
+= (note
.n_namesz
+ 3)/4;
43 memcpy(buf
, data
, note
.n_descsz
);
44 buf
+= (note
.n_descsz
+ 3)/4;
49 static void final_note(u32
*buf
)
56 memcpy(buf
, ¬e
, sizeof(note
));
59 static void crash_save_this_cpu(struct pt_regs
*regs
, int cpu
)
61 struct elf_prstatus prstatus
;
64 if ((cpu
< 0) || (cpu
>= NR_CPUS
))
67 /* Using ELF notes here is opportunistic.
68 * I need a well defined structure format
69 * for the data I pass, and I need tags
70 * on the data to indicate what information I have
71 * squirrelled away. ELF notes happen to provide
72 * all of that that no need to invent something new.
74 buf
= (u32
*)per_cpu_ptr(crash_notes
, cpu
);
77 memset(&prstatus
, 0, sizeof(prstatus
));
78 prstatus
.pr_pid
= current
->pid
;
79 elf_core_copy_regs(&prstatus
.pr_reg
, regs
);
80 buf
= append_elf_note(buf
, "CORE", NT_PRSTATUS
, &prstatus
,
85 static void crash_save_self(struct pt_regs
*regs
)
89 cpu
= smp_processor_id();
90 crash_save_this_cpu(regs
, cpu
);
94 static atomic_t waiting_for_crash_ipi
;
96 static int crash_nmi_callback(struct pt_regs
*regs
, int cpu
)
98 struct pt_regs fixed_regs
;
100 /* Don't do anything if this handler is invoked on crashing cpu.
101 * Otherwise, system will completely hang. Crashing cpu can get
102 * an NMI if system was initially booted with nmi_watchdog parameter.
104 if (cpu
== crashing_cpu
)
108 if (!user_mode_vm(regs
)) {
109 crash_fixup_ss_esp(&fixed_regs
, regs
);
112 crash_save_this_cpu(regs
, cpu
);
113 disable_local_APIC();
114 atomic_dec(&waiting_for_crash_ipi
);
115 /* Assume hlt works */
123 * By using the NMI code instead of a vector we just sneak thru the
124 * word generator coming out with just what we want. AND it does
125 * not matter if clustered_apic_mode is set or not.
127 static void smp_send_nmi_allbutself(void)
129 send_IPI_allbutself(APIC_DM_NMI
);
132 static void nmi_shootdown_cpus(void)
136 atomic_set(&waiting_for_crash_ipi
, num_online_cpus() - 1);
137 /* Would it be better to replace the trap vector here? */
138 set_nmi_callback(crash_nmi_callback
);
139 /* Ensure the new callback function is set before sending
144 smp_send_nmi_allbutself();
146 msecs
= 1000; /* Wait at most a second for the other cpus to stop */
147 while ((atomic_read(&waiting_for_crash_ipi
) > 0) && msecs
) {
152 /* Leave the nmi callback set */
153 disable_local_APIC();
156 static void nmi_shootdown_cpus(void)
158 /* There are no cpus to shootdown */
162 void machine_crash_shutdown(struct pt_regs
*regs
)
164 /* This function is only called after the system
165 * has paniced or is otherwise in a critical state.
166 * The minimum amount of code to allow a kexec'd kernel
167 * to run successfully needs to happen here.
169 * In practice this means shooting down the other cpus in
172 /* The kernel is broken so disable interrupts */
175 /* Make a note of crashing cpu. Will be used in NMI callback.*/
176 crashing_cpu
= smp_processor_id();
177 nmi_shootdown_cpus();
179 #if defined(CONFIG_X86_IO_APIC)
182 crash_save_self(regs
);