[NETFILTER]: PPTP conntrack: simplify expectation handling
[hh.org.git] / arch / x86_64 / kernel / crash.c
blobd8d5750d6106a1270817a360868cdbfb3d281338
1 /*
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.
8 */
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>
23 #include <asm/nmi.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)
33 struct elf_note note;
35 note.n_namesz = strlen(name) + 1;
36 note.n_descsz = data_len;
37 note.n_type = type;
38 memcpy(buf, &note, 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;
45 return buf;
48 static void final_note(u32 *buf)
50 struct elf_note note;
52 note.n_namesz = 0;
53 note.n_descsz = 0;
54 note.n_type = 0;
55 memcpy(buf, &note, sizeof(note));
58 static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
60 struct elf_prstatus prstatus;
61 u32 *buf;
63 if ((cpu < 0) || (cpu >= NR_CPUS))
64 return;
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);
76 if (!buf)
77 return;
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,
83 sizeof(prstatus));
84 final_note(buf);
87 static void crash_save_self(struct pt_regs *regs)
89 int cpu;
91 cpu = smp_processor_id();
92 crash_save_this_cpu(regs, cpu);
95 #ifdef CONFIG_SMP
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)
106 return 1;
107 local_irq_disable();
109 crash_save_this_cpu(regs, cpu);
110 disable_local_APIC();
111 atomic_dec(&waiting_for_crash_ipi);
112 /* Assume hlt works */
113 for(;;)
114 halt();
116 return 1;
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)
132 unsigned long msecs;
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
139 * out the NMI
141 wmb();
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) {
147 mdelay(1);
148 msecs--;
150 /* Leave the nmi callback set */
151 disable_local_APIC();
153 #else
154 static void nmi_shootdown_cpus(void)
156 /* There are no cpus to shootdown */
158 #endif
160 void machine_crash_shutdown(struct pt_regs *regs)
163 * This function is only called after the system
164 * has panicked 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
169 * an SMP system.
171 /* The kernel is broken so disable interrupts */
172 local_irq_disable();
174 /* Make a note of crashing cpu. Will be used in NMI callback.*/
175 crashing_cpu = smp_processor_id();
176 nmi_shootdown_cpus();
178 if(cpu_has_apic)
179 disable_local_APIC();
181 #if defined(CONFIG_X86_IO_APIC)
182 disable_IO_APIC();
183 #endif
185 crash_save_self(regs);