Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee13...
[pandora-kernel.git] / arch / i386 / kernel / crash.c
1 /*
2  * Architecture specific (i386) 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/reboot.h>
15 #include <linux/kexec.h>
16 #include <linux/delay.h>
17 #include <linux/elf.h>
18 #include <linux/elfcore.h>
19
20 #include <asm/processor.h>
21 #include <asm/hardirq.h>
22 #include <asm/nmi.h>
23 #include <asm/hw_irq.h>
24 #include <asm/apic.h>
25 #include <asm/kdebug.h>
26
27 #include <mach_ipi.h>
28
29
30 /* This keeps a track of which one is crashing cpu. */
31 static int crashing_cpu;
32
33 static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
34                                                                size_t data_len)
35 {
36         struct elf_note note;
37
38         note.n_namesz = strlen(name) + 1;
39         note.n_descsz = data_len;
40         note.n_type   = type;
41         memcpy(buf, &note, sizeof(note));
42         buf += (sizeof(note) +3)/4;
43         memcpy(buf, name, note.n_namesz);
44         buf += (note.n_namesz + 3)/4;
45         memcpy(buf, data, note.n_descsz);
46         buf += (note.n_descsz + 3)/4;
47
48         return buf;
49 }
50
51 static void final_note(u32 *buf)
52 {
53         struct elf_note note;
54
55         note.n_namesz = 0;
56         note.n_descsz = 0;
57         note.n_type   = 0;
58         memcpy(buf, &note, sizeof(note));
59 }
60
61 static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
62 {
63         struct elf_prstatus prstatus;
64         u32 *buf;
65
66         if ((cpu < 0) || (cpu >= NR_CPUS))
67                 return;
68
69         /* Using ELF notes here is opportunistic.
70          * I need a well defined structure format
71          * for the data I pass, and I need tags
72          * on the data to indicate what information I have
73          * squirrelled away.  ELF notes happen to provide
74          * all of that, so there is no need to invent something new.
75          */
76         buf = (u32*)per_cpu_ptr(crash_notes, cpu);
77         if (!buf)
78                 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);
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 #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
96 static atomic_t waiting_for_crash_ipi;
97
98 static int crash_nmi_callback(struct notifier_block *self,
99                         unsigned long val, void *data)
100 {
101         struct pt_regs *regs;
102         struct pt_regs fixed_regs;
103         int cpu;
104
105         if (val != DIE_NMI_IPI)
106                 return NOTIFY_OK;
107
108         regs = ((struct die_args *)data)->regs;
109         cpu = raw_smp_processor_id();
110
111         /* Don't do anything if this handler is invoked on crashing cpu.
112          * Otherwise, system will completely hang. Crashing cpu can get
113          * an NMI if system was initially booted with nmi_watchdog parameter.
114          */
115         if (cpu == crashing_cpu)
116                 return NOTIFY_STOP;
117         local_irq_disable();
118
119         if (!user_mode_vm(regs)) {
120                 crash_fixup_ss_esp(&fixed_regs, regs);
121                 regs = &fixed_regs;
122         }
123         crash_save_this_cpu(regs, cpu);
124         disable_local_APIC();
125         atomic_dec(&waiting_for_crash_ipi);
126         /* Assume hlt works */
127         halt();
128         for (;;)
129                 cpu_relax();
130
131         return 1;
132 }
133
134 static void smp_send_nmi_allbutself(void)
135 {
136         send_IPI_allbutself(NMI_VECTOR);
137 }
138
139 static struct notifier_block crash_nmi_nb = {
140         .notifier_call = crash_nmi_callback,
141 };
142
143 static void nmi_shootdown_cpus(void)
144 {
145         unsigned long msecs;
146
147         atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
148         /* Would it be better to replace the trap vector here? */
149         if (register_die_notifier(&crash_nmi_nb))
150                 return;         /* return what? */
151         /* Ensure the new callback function is set before sending
152          * out the NMI
153          */
154         wmb();
155
156         smp_send_nmi_allbutself();
157
158         msecs = 1000; /* Wait at most a second for the other cpus to stop */
159         while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
160                 mdelay(1);
161                 msecs--;
162         }
163
164         /* Leave the nmi callback set */
165         disable_local_APIC();
166 }
167 #else
168 static void nmi_shootdown_cpus(void)
169 {
170         /* There are no cpus to shootdown */
171 }
172 #endif
173
174 void machine_crash_shutdown(struct pt_regs *regs)
175 {
176         /* This function is only called after the system
177          * has panicked or is otherwise in a critical state.
178          * The minimum amount of code to allow a kexec'd kernel
179          * to run successfully needs to happen here.
180          *
181          * In practice this means shooting down the other cpus in
182          * an SMP system.
183          */
184         /* The kernel is broken so disable interrupts */
185         local_irq_disable();
186
187         /* Make a note of crashing cpu. Will be used in NMI callback.*/
188         crashing_cpu = smp_processor_id();
189         nmi_shootdown_cpus();
190         lapic_shutdown();
191 #if defined(CONFIG_X86_IO_APIC)
192         disable_IO_APIC();
193 #endif
194         crash_save_self(regs);
195 }