Merge ../linus
[pandora-kernel.git] / arch / x86_64 / kernel / crash.c
1 /*
2  * Architecture specific (x86_64) functions for kexec based crash dumps.
3  *
4  * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
5  *
6  * Copyright (C) IBM Corporation, 2004. All rights reserved.
7  *
8  */
9
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>
20
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>
26
27 /* This keeps a track of which one is crashing cpu. */
28 static int crashing_cpu;
29
30 static u32 *append_elf_note(u32 *buf, char *name, unsigned type,
31                                                 void *data, size_t data_len)
32 {
33         struct elf_note note;
34
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;
44
45         return buf;
46 }
47
48 static void final_note(u32 *buf)
49 {
50         struct elf_note note;
51
52         note.n_namesz = 0;
53         note.n_descsz = 0;
54         note.n_type   = 0;
55         memcpy(buf, &note, sizeof(note));
56 }
57
58 static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
59 {
60         struct elf_prstatus prstatus;
61         u32 *buf;
62
63         if ((cpu < 0) || (cpu >= NR_CPUS))
64                 return;
65
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.
72          */
73
74         buf = (u32*)per_cpu_ptr(crash_notes, cpu);
75
76         if (!buf)
77                 return;
78
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);
85 }
86
87 static void crash_save_self(struct pt_regs *regs)
88 {
89         int cpu;
90
91         cpu = smp_processor_id();
92         crash_save_this_cpu(regs, cpu);
93 }
94
95 #ifdef CONFIG_SMP
96 static atomic_t waiting_for_crash_ipi;
97
98 static int crash_nmi_callback(struct pt_regs *regs, int cpu)
99 {
100         /*
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.
104          */
105         if (cpu == crashing_cpu)
106                 return 1;
107         local_irq_disable();
108
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();
115
116         return 1;
117 }
118
119 static void smp_send_nmi_allbutself(void)
120 {
121         send_IPI_allbutself(NMI_VECTOR);
122 }
123
124 /*
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.
128  */
129
130 static void nmi_shootdown_cpus(void)
131 {
132         unsigned long msecs;
133
134         atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
135         set_nmi_callback(crash_nmi_callback);
136
137         /*
138          * Ensure the new callback function is set before sending
139          * out the NMI
140          */
141         wmb();
142
143         smp_send_nmi_allbutself();
144
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--;
149         }
150         /* Leave the nmi callback set */
151         disable_local_APIC();
152 }
153 #else
154 static void nmi_shootdown_cpus(void)
155 {
156         /* There are no cpus to shootdown */
157 }
158 #endif
159
160 void machine_crash_shutdown(struct pt_regs *regs)
161 {
162         /*
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.
167          *
168          * In practice this means shooting down the other cpus in
169          * an SMP system.
170          */
171         /* The kernel is broken so disable interrupts */
172         local_irq_disable();
173
174         /* Make a note of crashing cpu. Will be used in NMI callback.*/
175         crashing_cpu = smp_processor_id();
176         nmi_shootdown_cpus();
177
178         if(cpu_has_apic)
179                  disable_local_APIC();
180
181 #if defined(CONFIG_X86_IO_APIC)
182         disable_IO_APIC();
183 #endif
184
185         crash_save_self(regs);
186 }