]>
Commit | Line | Data |
---|---|---|
5033cba0 EB |
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> | |
5033cba0 EB |
14 | #include <linux/reboot.h> |
15 | #include <linux/kexec.h> | |
5033cba0 EB |
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> | |
c4ac4263 | 24 | #include <mach_ipi.h> |
5033cba0 | 25 | |
5033cba0 EB |
26 | |
27 | note_buf_t crash_notes[NR_CPUS]; | |
a3ea8ac8 VG |
28 | /* This keeps a track of which one is crashing cpu. */ |
29 | static int crashing_cpu; | |
5033cba0 | 30 | |
72414d3f MS |
31 | static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data, |
32 | size_t data_len) | |
2c818b45 EB |
33 | { |
34 | struct elf_note note; | |
72414d3f | 35 | |
2c818b45 EB |
36 | note.n_namesz = strlen(name) + 1; |
37 | note.n_descsz = data_len; | |
38 | note.n_type = type; | |
39 | memcpy(buf, ¬e, sizeof(note)); | |
40 | buf += (sizeof(note) +3)/4; | |
41 | memcpy(buf, name, note.n_namesz); | |
42 | buf += (note.n_namesz + 3)/4; | |
43 | memcpy(buf, data, note.n_descsz); | |
44 | buf += (note.n_descsz + 3)/4; | |
72414d3f | 45 | |
2c818b45 EB |
46 | return buf; |
47 | } | |
48 | ||
49 | static void final_note(u32 *buf) | |
50 | { | |
51 | struct elf_note note; | |
72414d3f | 52 | |
2c818b45 EB |
53 | note.n_namesz = 0; |
54 | note.n_descsz = 0; | |
55 | note.n_type = 0; | |
56 | memcpy(buf, ¬e, sizeof(note)); | |
57 | } | |
58 | ||
2c818b45 EB |
59 | static void crash_save_this_cpu(struct pt_regs *regs, int cpu) |
60 | { | |
61 | struct elf_prstatus prstatus; | |
62 | u32 *buf; | |
72414d3f MS |
63 | |
64 | if ((cpu < 0) || (cpu >= NR_CPUS)) | |
2c818b45 | 65 | return; |
72414d3f | 66 | |
2c818b45 EB |
67 | /* Using ELF notes here is opportunistic. |
68 | * I need a well defined structure format | |
69 | * for the data I pass, and I need tags | |
70 | * on the data to indicate what information I have | |
71 | * squirrelled away. ELF notes happen to provide | |
72 | * all of that that no need to invent something new. | |
73 | */ | |
74 | buf = &crash_notes[cpu][0]; | |
75 | memset(&prstatus, 0, sizeof(prstatus)); | |
76 | prstatus.pr_pid = current->pid; | |
77 | elf_core_copy_regs(&prstatus.pr_reg, regs); | |
72414d3f MS |
78 | buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus, |
79 | sizeof(prstatus)); | |
2c818b45 EB |
80 | final_note(buf); |
81 | } | |
82 | ||
83 | static void crash_get_current_regs(struct pt_regs *regs) | |
84 | { | |
85 | __asm__ __volatile__("movl %%ebx,%0" : "=m"(regs->ebx)); | |
86 | __asm__ __volatile__("movl %%ecx,%0" : "=m"(regs->ecx)); | |
87 | __asm__ __volatile__("movl %%edx,%0" : "=m"(regs->edx)); | |
88 | __asm__ __volatile__("movl %%esi,%0" : "=m"(regs->esi)); | |
89 | __asm__ __volatile__("movl %%edi,%0" : "=m"(regs->edi)); | |
90 | __asm__ __volatile__("movl %%ebp,%0" : "=m"(regs->ebp)); | |
91 | __asm__ __volatile__("movl %%eax,%0" : "=m"(regs->eax)); | |
92 | __asm__ __volatile__("movl %%esp,%0" : "=m"(regs->esp)); | |
93 | __asm__ __volatile__("movw %%ss, %%ax;" :"=a"(regs->xss)); | |
94 | __asm__ __volatile__("movw %%cs, %%ax;" :"=a"(regs->xcs)); | |
95 | __asm__ __volatile__("movw %%ds, %%ax;" :"=a"(regs->xds)); | |
96 | __asm__ __volatile__("movw %%es, %%ax;" :"=a"(regs->xes)); | |
97 | __asm__ __volatile__("pushfl; popl %0" :"=m"(regs->eflags)); | |
98 | ||
99 | regs->eip = (unsigned long)current_text_addr(); | |
100 | } | |
101 | ||
6e274d14 AN |
102 | /* CPU does not save ss and esp on stack if execution is already |
103 | * running in kernel mode at the time of NMI occurrence. This code | |
104 | * fixes it. | |
105 | */ | |
106 | static void crash_setup_regs(struct pt_regs *newregs, struct pt_regs *oldregs) | |
107 | { | |
108 | memcpy(newregs, oldregs, sizeof(*newregs)); | |
109 | newregs->esp = (unsigned long)&(oldregs->esp); | |
110 | __asm__ __volatile__("xorl %eax, %eax;"); | |
111 | __asm__ __volatile__ ("movw %%ss, %%ax;" :"=a"(newregs->xss)); | |
112 | } | |
113 | ||
114 | /* We may have saved_regs from where the error came from | |
115 | * or it is NULL if via a direct panic(). | |
116 | */ | |
117 | static void crash_save_self(struct pt_regs *saved_regs) | |
2c818b45 EB |
118 | { |
119 | struct pt_regs regs; | |
120 | int cpu; | |
6e274d14 | 121 | |
72414d3f | 122 | cpu = smp_processor_id(); |
6e274d14 AN |
123 | if (saved_regs) |
124 | crash_setup_regs(®s, saved_regs); | |
125 | else | |
126 | crash_get_current_regs(®s); | |
2c818b45 EB |
127 | crash_save_this_cpu(®s, cpu); |
128 | } | |
129 | ||
c4ac4263 EB |
130 | #ifdef CONFIG_SMP |
131 | static atomic_t waiting_for_crash_ipi; | |
132 | ||
133 | static int crash_nmi_callback(struct pt_regs *regs, int cpu) | |
134 | { | |
4d55476c | 135 | struct pt_regs fixed_regs; |
a3ea8ac8 VG |
136 | |
137 | /* Don't do anything if this handler is invoked on crashing cpu. | |
138 | * Otherwise, system will completely hang. Crashing cpu can get | |
139 | * an NMI if system was initially booted with nmi_watchdog parameter. | |
140 | */ | |
141 | if (cpu == crashing_cpu) | |
142 | return 1; | |
c4ac4263 | 143 | local_irq_disable(); |
4d55476c | 144 | |
4d55476c | 145 | if (!user_mode(regs)) { |
6e274d14 | 146 | crash_setup_regs(&fixed_regs, regs); |
4d55476c VG |
147 | regs = &fixed_regs; |
148 | } | |
2c818b45 | 149 | crash_save_this_cpu(regs, cpu); |
c4ac4263 EB |
150 | atomic_dec(&waiting_for_crash_ipi); |
151 | /* Assume hlt works */ | |
f2ab4461 | 152 | halt(); |
c4ac4263 | 153 | for(;;); |
72414d3f | 154 | |
c4ac4263 EB |
155 | return 1; |
156 | } | |
157 | ||
158 | /* | |
159 | * By using the NMI code instead of a vector we just sneak thru the | |
160 | * word generator coming out with just what we want. AND it does | |
161 | * not matter if clustered_apic_mode is set or not. | |
162 | */ | |
163 | static void smp_send_nmi_allbutself(void) | |
164 | { | |
165 | send_IPI_allbutself(APIC_DM_NMI); | |
166 | } | |
167 | ||
168 | static void nmi_shootdown_cpus(void) | |
169 | { | |
170 | unsigned long msecs; | |
c4ac4263 | 171 | |
72414d3f | 172 | atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1); |
c4ac4263 EB |
173 | /* Would it be better to replace the trap vector here? */ |
174 | set_nmi_callback(crash_nmi_callback); | |
175 | /* Ensure the new callback function is set before sending | |
176 | * out the NMI | |
177 | */ | |
178 | wmb(); | |
179 | ||
180 | smp_send_nmi_allbutself(); | |
181 | ||
182 | msecs = 1000; /* Wait at most a second for the other cpus to stop */ | |
183 | while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) { | |
184 | mdelay(1); | |
185 | msecs--; | |
186 | } | |
187 | ||
188 | /* Leave the nmi callback set */ | |
189 | } | |
190 | #else | |
191 | static void nmi_shootdown_cpus(void) | |
192 | { | |
193 | /* There are no cpus to shootdown */ | |
194 | } | |
195 | #endif | |
196 | ||
6e274d14 | 197 | void machine_crash_shutdown(struct pt_regs *regs) |
5033cba0 EB |
198 | { |
199 | /* This function is only called after the system | |
200 | * has paniced or is otherwise in a critical state. | |
201 | * The minimum amount of code to allow a kexec'd kernel | |
202 | * to run successfully needs to happen here. | |
203 | * | |
204 | * In practice this means shooting down the other cpus in | |
205 | * an SMP system. | |
206 | */ | |
c4ac4263 EB |
207 | /* The kernel is broken so disable interrupts */ |
208 | local_irq_disable(); | |
a3ea8ac8 VG |
209 | |
210 | /* Make a note of crashing cpu. Will be used in NMI callback.*/ | |
211 | crashing_cpu = smp_processor_id(); | |
c4ac4263 | 212 | nmi_shootdown_cpus(); |
6e274d14 | 213 | crash_save_self(regs); |
5033cba0 | 214 | } |