2 * Architecture specific (x86_64) 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/irq.h>
15 #include <linux/reboot.h>
16 #include <linux/kexec.h>
17 #include <linux/delay.h>
18 #include <linux/elf.h>
19 #include <linux/elfcore.h>
21 #include <asm/processor.h>
22 #include <asm/hardirq.h>
24 #include <asm/hw_irq.h>
25 #include <asm/mach_apic.h>
27 /* This keeps a track of which one is crashing cpu. */
28 static int crashing_cpu;
30 static u32 *append_elf_note(u32 *buf, char *name, unsigned type,
31 void *data, size_t data_len)
35 note.n_namesz = strlen(name) + 1;
36 note.n_descsz = data_len;
38 memcpy(buf, ¬e, sizeof(note));
39 buf += (sizeof(note) +3)/4;
40 memcpy(buf, name, note.n_namesz);
41 buf += (note.n_namesz + 3)/4;
42 memcpy(buf, data, note.n_descsz);
43 buf += (note.n_descsz + 3)/4;
48 static void final_note(u32 *buf)
55 memcpy(buf, ¬e, sizeof(note));
58 static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
60 struct elf_prstatus prstatus;
63 if ((cpu < 0) || (cpu >= NR_CPUS))
66 /* Using ELF notes here is opportunistic.
67 * I need a well defined structure format
68 * for the data I pass, and I need tags
69 * on the data to indicate what information I have
70 * squirrelled away. ELF notes happen to provide
71 * all of that that no need to invent something new.
74 buf = (u32*)per_cpu_ptr(crash_notes, cpu);
79 memset(&prstatus, 0, sizeof(prstatus));
80 prstatus.pr_pid = current->pid;
81 elf_core_copy_regs(&prstatus.pr_reg, regs);
82 buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus,
87 static void crash_save_self(struct pt_regs *regs)
91 cpu = smp_processor_id();
92 crash_save_this_cpu(regs, cpu);
96 static atomic_t waiting_for_crash_ipi;
98 static int crash_nmi_callback(struct pt_regs *regs, int cpu)
101 * Don't do anything if this handler is invoked on crashing cpu.
102 * Otherwise, system will completely hang. Crashing cpu can get
103 * an NMI if system was initially booted with nmi_watchdog parameter.
105 if (cpu == crashing_cpu)
109 crash_save_this_cpu(regs, cpu);
110 disable_local_APIC();
111 atomic_dec(&waiting_for_crash_ipi);
112 /* Assume hlt works */
119 static void smp_send_nmi_allbutself(void)
121 send_IPI_allbutself(NMI_VECTOR);
125 * This code is a best effort heuristic to get the
126 * other cpus to stop executing. So races with
127 * cpu hotplug shouldn't matter.
130 static void nmi_shootdown_cpus(void)
134 atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
135 set_nmi_callback(crash_nmi_callback);
138 * Ensure the new callback function is set before sending
143 smp_send_nmi_allbutself();
145 msecs = 1000; /* Wait at most a second for the other cpus to stop */
146 while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
150 /* Leave the nmi callback set */
151 disable_local_APIC();
154 static void nmi_shootdown_cpus(void)
156 /* There are no cpus to shootdown */
160 void machine_crash_shutdown(struct pt_regs *regs)
163 * This function is only called after the system
164 * has paniced or is otherwise in a critical state.
165 * The minimum amount of code to allow a kexec'd kernel
166 * to run successfully needs to happen here.
168 * In practice this means shooting down the other cpus in
171 /* The kernel is broken so disable interrupts */
174 /* Make a note of crashing cpu. Will be used in NMI callback.*/
175 crashing_cpu = smp_processor_id();
176 nmi_shootdown_cpus();
179 disable_local_APIC();
181 #if defined(CONFIG_X86_IO_APIC)
185 crash_save_self(regs);