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> | |
19842d67 | 24 | #include <asm/apic.h> |
2fbe7b25 | 25 | #include <asm/kdebug.h> |
ce53af94 | 26 | #include <asm/smp.h> |
2fbe7b25 | 27 | |
c4ac4263 | 28 | #include <mach_ipi.h> |
5033cba0 | 29 | |
5033cba0 | 30 | |
a3ea8ac8 VG |
31 | /* This keeps a track of which one is crashing cpu. */ |
32 | static int crashing_cpu; | |
5033cba0 | 33 | |
72414d3f MS |
34 | static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data, |
35 | size_t data_len) | |
2c818b45 EB |
36 | { |
37 | struct elf_note note; | |
72414d3f | 38 | |
2c818b45 EB |
39 | note.n_namesz = strlen(name) + 1; |
40 | note.n_descsz = data_len; | |
41 | note.n_type = type; | |
42 | memcpy(buf, ¬e, sizeof(note)); | |
43 | buf += (sizeof(note) +3)/4; | |
44 | memcpy(buf, name, note.n_namesz); | |
45 | buf += (note.n_namesz + 3)/4; | |
46 | memcpy(buf, data, note.n_descsz); | |
47 | buf += (note.n_descsz + 3)/4; | |
72414d3f | 48 | |
2c818b45 EB |
49 | return buf; |
50 | } | |
51 | ||
52 | static void final_note(u32 *buf) | |
53 | { | |
54 | struct elf_note note; | |
72414d3f | 55 | |
2c818b45 EB |
56 | note.n_namesz = 0; |
57 | note.n_descsz = 0; | |
58 | note.n_type = 0; | |
59 | memcpy(buf, ¬e, sizeof(note)); | |
60 | } | |
61 | ||
2c818b45 EB |
62 | static void crash_save_this_cpu(struct pt_regs *regs, int cpu) |
63 | { | |
64 | struct elf_prstatus prstatus; | |
65 | u32 *buf; | |
72414d3f MS |
66 | |
67 | if ((cpu < 0) || (cpu >= NR_CPUS)) | |
2c818b45 | 68 | return; |
72414d3f | 69 | |
2c818b45 EB |
70 | /* Using ELF notes here is opportunistic. |
71 | * I need a well defined structure format | |
72 | * for the data I pass, and I need tags | |
73 | * on the data to indicate what information I have | |
74 | * squirrelled away. ELF notes happen to provide | |
36a891b6 | 75 | * all of that, so there is no need to invent something new. |
2c818b45 | 76 | */ |
cc571658 VG |
77 | buf = (u32*)per_cpu_ptr(crash_notes, cpu); |
78 | if (!buf) | |
79 | return; | |
2c818b45 EB |
80 | memset(&prstatus, 0, sizeof(prstatus)); |
81 | prstatus.pr_pid = current->pid; | |
82 | elf_core_copy_regs(&prstatus.pr_reg, regs); | |
72414d3f MS |
83 | buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus, |
84 | sizeof(prstatus)); | |
2c818b45 EB |
85 | final_note(buf); |
86 | } | |
87 | ||
e996e581 | 88 | static void crash_save_self(struct pt_regs *regs) |
2c818b45 | 89 | { |
2c818b45 | 90 | int cpu; |
6e274d14 | 91 | |
ce53af94 | 92 | cpu = safe_smp_processor_id(); |
e996e581 | 93 | crash_save_this_cpu(regs, cpu); |
2c818b45 EB |
94 | } |
95 | ||
e78a887a | 96 | #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC) |
c4ac4263 EB |
97 | static atomic_t waiting_for_crash_ipi; |
98 | ||
2fbe7b25 DZ |
99 | static int crash_nmi_callback(struct notifier_block *self, |
100 | unsigned long val, void *data) | |
c4ac4263 | 101 | { |
2fbe7b25 | 102 | struct pt_regs *regs; |
4d55476c | 103 | struct pt_regs fixed_regs; |
2fbe7b25 DZ |
104 | int cpu; |
105 | ||
260d6790 | 106 | if (val != DIE_NMI_IPI) |
2fbe7b25 DZ |
107 | return NOTIFY_OK; |
108 | ||
109 | regs = ((struct die_args *)data)->regs; | |
110 | cpu = raw_smp_processor_id(); | |
a3ea8ac8 VG |
111 | |
112 | /* Don't do anything if this handler is invoked on crashing cpu. | |
113 | * Otherwise, system will completely hang. Crashing cpu can get | |
114 | * an NMI if system was initially booted with nmi_watchdog parameter. | |
115 | */ | |
116 | if (cpu == crashing_cpu) | |
260d6790 | 117 | return NOTIFY_STOP; |
c4ac4263 | 118 | local_irq_disable(); |
4d55476c | 119 | |
db753bdf | 120 | if (!user_mode_vm(regs)) { |
e996e581 | 121 | crash_fixup_ss_esp(&fixed_regs, regs); |
4d55476c VG |
122 | regs = &fixed_regs; |
123 | } | |
2c818b45 | 124 | crash_save_this_cpu(regs, cpu); |
19842d67 | 125 | disable_local_APIC(); |
c4ac4263 EB |
126 | atomic_dec(&waiting_for_crash_ipi); |
127 | /* Assume hlt works */ | |
f2ab4461 | 128 | halt(); |
caad3c2a CE |
129 | for (;;) |
130 | cpu_relax(); | |
72414d3f | 131 | |
c4ac4263 EB |
132 | return 1; |
133 | } | |
134 | ||
c4ac4263 EB |
135 | static void smp_send_nmi_allbutself(void) |
136 | { | |
bc03613d FV |
137 | cpumask_t mask = cpu_online_map; |
138 | cpu_clear(safe_smp_processor_id(), mask); | |
139 | if (!cpus_empty(mask)) | |
140 | send_IPI_mask(mask, NMI_VECTOR); | |
c4ac4263 EB |
141 | } |
142 | ||
2fbe7b25 DZ |
143 | static struct notifier_block crash_nmi_nb = { |
144 | .notifier_call = crash_nmi_callback, | |
145 | }; | |
146 | ||
c4ac4263 EB |
147 | static void nmi_shootdown_cpus(void) |
148 | { | |
149 | unsigned long msecs; | |
c4ac4263 | 150 | |
72414d3f | 151 | atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1); |
c4ac4263 | 152 | /* Would it be better to replace the trap vector here? */ |
2fbe7b25 DZ |
153 | if (register_die_notifier(&crash_nmi_nb)) |
154 | return; /* return what? */ | |
c4ac4263 EB |
155 | /* Ensure the new callback function is set before sending |
156 | * out the NMI | |
157 | */ | |
158 | wmb(); | |
159 | ||
160 | smp_send_nmi_allbutself(); | |
161 | ||
162 | msecs = 1000; /* Wait at most a second for the other cpus to stop */ | |
163 | while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) { | |
164 | mdelay(1); | |
165 | msecs--; | |
166 | } | |
167 | ||
168 | /* Leave the nmi callback set */ | |
19842d67 | 169 | disable_local_APIC(); |
c4ac4263 EB |
170 | } |
171 | #else | |
172 | static void nmi_shootdown_cpus(void) | |
173 | { | |
174 | /* There are no cpus to shootdown */ | |
175 | } | |
176 | #endif | |
177 | ||
6e274d14 | 178 | void machine_crash_shutdown(struct pt_regs *regs) |
5033cba0 EB |
179 | { |
180 | /* This function is only called after the system | |
f18190bd | 181 | * has panicked or is otherwise in a critical state. |
5033cba0 EB |
182 | * The minimum amount of code to allow a kexec'd kernel |
183 | * to run successfully needs to happen here. | |
184 | * | |
185 | * In practice this means shooting down the other cpus in | |
186 | * an SMP system. | |
187 | */ | |
c4ac4263 EB |
188 | /* The kernel is broken so disable interrupts */ |
189 | local_irq_disable(); | |
a3ea8ac8 VG |
190 | |
191 | /* Make a note of crashing cpu. Will be used in NMI callback.*/ | |
ce53af94 | 192 | crashing_cpu = safe_smp_processor_id(); |
c4ac4263 | 193 | nmi_shootdown_cpus(); |
19842d67 VG |
194 | lapic_shutdown(); |
195 | #if defined(CONFIG_X86_IO_APIC) | |
196 | disable_IO_APIC(); | |
197 | #endif | |
6e274d14 | 198 | crash_save_self(regs); |
5033cba0 | 199 | } |