blob: ec1c7431d5af91074f2640564a0d4b0e6a624783 [file] [log] [blame]
Eric W. Biederman5234f5e2005-06-25 14:58:02 -07001/*
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>
Vivek Goyalec9ce0d2006-01-09 20:51:49 -080014#include <linux/irq.h>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070015#include <linux/reboot.h>
16#include <linux/kexec.h>
akpm@osdl.orgcffe6322006-01-09 20:51:48 -080017#include <linux/delay.h>
Vivek Goyalec9ce0d2006-01-09 20:51:49 -080018#include <linux/elf.h>
19#include <linux/elfcore.h>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070020
21#include <asm/processor.h>
22#include <asm/hardirq.h>
23#include <asm/nmi.h>
24#include <asm/hw_irq.h>
akpm@osdl.orgcffe6322006-01-09 20:51:48 -080025#include <asm/mach_apic.h>
26
27/* This keeps a track of which one is crashing cpu. */
28static int crashing_cpu;
29
Vivek Goyalec9ce0d2006-01-09 20:51:49 -080030static 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
48static 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
58static 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
87static 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
akpm@osdl.orgcffe6322006-01-09 20:51:48 -080095#ifdef CONFIG_SMP
96static atomic_t waiting_for_crash_ipi;
97
98static 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
Vivek Goyalec9ce0d2006-01-09 20:51:49 -0800109 crash_save_this_cpu(regs, cpu);
akpm@osdl.orgcffe6322006-01-09 20:51:48 -0800110 disable_local_APIC();
111 atomic_dec(&waiting_for_crash_ipi);
112 /* Assume hlt works */
113 for(;;)
Jan Beulich46d13a32006-06-26 13:57:59 +0200114 halt();
akpm@osdl.orgcffe6322006-01-09 20:51:48 -0800115
116 return 1;
117}
118
119static void smp_send_nmi_allbutself(void)
120{
121 send_IPI_allbutself(APIC_DM_NMI);
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
130static 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
154static void nmi_shootdown_cpus(void)
155{
156 /* There are no cpus to shootdown */
157}
158#endif
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700159
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700160void machine_crash_shutdown(struct pt_regs *regs)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700161{
akpm@osdl.orgcffe6322006-01-09 20:51:48 -0800162 /*
163 * This function is only called after the system
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700164 * 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.
167 *
168 * In practice this means shooting down the other cpus in
169 * an SMP system.
170 */
akpm@osdl.orgcffe6322006-01-09 20:51:48 -0800171 /* 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
Vivek Goyalec9ce0d2006-01-09 20:51:49 -0800185 crash_save_self(regs);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700186}