blob: 0995fdc7b2998217139f31150346cb6918fbda20 [file] [log] [blame]
Zou Nan haia79561132006-12-07 09:51:35 -08001/*
2 * arch/ia64/kernel/crash.c
3 *
4 * Architecture specific (ia64) functions for kexec based crash dumps.
5 *
6 * Created by: Khalid Aziz <khalid.aziz@hp.com>
7 * Copyright (C) 2005 Hewlett-Packard Development Company, L.P.
8 * Copyright (C) 2005 Intel Corp Zou Nan hai <nanhai.zou@intel.com>
9 *
10 */
11#include <linux/smp.h>
12#include <linux/delay.h>
13#include <linux/crash_dump.h>
14#include <linux/bootmem.h>
15#include <linux/kexec.h>
16#include <linux/elfcore.h>
17#include <linux/sysctl.h>
18#include <linux/init.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070019#include <linux/kdebug.h>
Zou Nan haia79561132006-12-07 09:51:35 -080020
Zou Nan haia79561132006-12-07 09:51:35 -080021#include <asm/mca.h>
Zou Nan haia79561132006-12-07 09:51:35 -080022
23int kdump_status[NR_CPUS];
Simon Horman0ac1fac2007-02-14 16:15:02 +090024static atomic_t kdump_cpu_frozen;
Zou Nan haia79561132006-12-07 09:51:35 -080025atomic_t kdump_in_progress;
Hidetoshi Seto1726b082009-08-06 14:51:57 -070026static int kdump_freeze_monarch;
Simon Horman0ac1fac2007-02-14 16:15:02 +090027static int kdump_on_init = 1;
Hidetoshi Setob0247a52008-04-08 13:31:47 +090028static int kdump_on_fatal_mca = 1;
Zou Nan haia79561132006-12-07 09:51:35 -080029
30static inline Elf64_Word
31*append_elf_note(Elf64_Word *buf, char *name, unsigned type, void *data,
32 size_t data_len)
33{
34 struct elf_note *note = (struct elf_note *)buf;
35 note->n_namesz = strlen(name) + 1;
36 note->n_descsz = data_len;
37 note->n_type = type;
38 buf += (sizeof(*note) + 3)/4;
39 memcpy(buf, name, note->n_namesz);
40 buf += (note->n_namesz + 3)/4;
41 memcpy(buf, data, data_len);
42 buf += (data_len + 3)/4;
43 return buf;
44}
45
46static void
47final_note(void *buf)
48{
49 memset(buf, 0, sizeof(struct elf_note));
50}
51
52extern void ia64_dump_cpu_regs(void *);
53
54static DEFINE_PER_CPU(struct elf_prstatus, elf_prstatus);
55
56void
Al Viroccbebda2007-02-09 16:38:10 +000057crash_save_this_cpu(void)
Zou Nan haia79561132006-12-07 09:51:35 -080058{
59 void *buf;
60 unsigned long cfm, sof, sol;
61
62 int cpu = smp_processor_id();
63 struct elf_prstatus *prstatus = &per_cpu(elf_prstatus, cpu);
64
65 elf_greg_t *dst = (elf_greg_t *)&(prstatus->pr_reg);
66 memset(prstatus, 0, sizeof(*prstatus));
67 prstatus->pr_pid = current->pid;
68
69 ia64_dump_cpu_regs(dst);
70 cfm = dst[43];
71 sol = (cfm >> 7) & 0x7f;
72 sof = cfm & 0x7f;
73 dst[46] = (unsigned long)ia64_rse_skip_regs((unsigned long *)dst[46],
74 sof - sol);
75
76 buf = (u64 *) per_cpu_ptr(crash_notes, cpu);
77 if (!buf)
78 return;
Simon Horman6672f762007-05-08 00:28:22 -070079 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS, prstatus,
Zou Nan haia79561132006-12-07 09:51:35 -080080 sizeof(*prstatus));
81 final_note(buf);
82}
83
Magnus Dammbcb9b992007-02-05 15:43:42 +090084#ifdef CONFIG_SMP
Zou Nan haia79561132006-12-07 09:51:35 -080085static int
86kdump_wait_cpu_freeze(void)
87{
88 int cpu_num = num_online_cpus() - 1;
89 int timeout = 1000;
90 while(timeout-- > 0) {
Simon Horman0ac1fac2007-02-14 16:15:02 +090091 if (atomic_read(&kdump_cpu_frozen) == cpu_num)
Zou Nan haia79561132006-12-07 09:51:35 -080092 return 0;
93 udelay(1000);
94 }
95 return 1;
96}
Magnus Dammbcb9b992007-02-05 15:43:42 +090097#endif
Zou Nan haia79561132006-12-07 09:51:35 -080098
99void
100machine_crash_shutdown(struct pt_regs *pt)
101{
102 /* This function is only called after the system
103 * has paniced or is otherwise in a critical state.
104 * The minimum amount of code to allow a kexec'd kernel
105 * to run successfully needs to happen here.
106 *
107 * In practice this means shooting down the other cpus in
108 * an SMP system.
109 */
110 kexec_disable_iosapic();
111#ifdef CONFIG_SMP
Hidetoshi Seto1726b082009-08-06 14:51:57 -0700112 /*
113 * If kdump_on_init is set and an INIT is asserted here, kdump will
114 * be started again via INIT monarch.
115 */
116 local_irq_disable();
117 ia64_set_psr_mc(); /* mask MCA/INIT */
118 if (atomic_inc_return(&kdump_in_progress) != 1)
119 unw_init_running(kdump_cpu_freeze, NULL);
120
121 /*
122 * Now this cpu is ready for kdump.
123 * Stop all others by IPI or INIT. They could receive INIT from
124 * outside and might be INIT monarch, but only thing they have to
125 * do is falling into kdump_cpu_freeze().
126 *
127 * If an INIT is asserted here:
128 * - All receivers might be slaves, since some of cpus could already
129 * be frozen and INIT might be masked on monarch. In this case,
130 * all slaves will park in while (monarch_cpu == -1) loop before
131 * DIE_INIT_SLAVE_ENTER that for waiting monarch enters.
132 * => TBD: freeze all slaves
133 * - One might be a monarch, but INIT rendezvous will fail since
134 * at least this cpu already have INIT masked so it never join
135 * to the rendezvous. In this case, all slaves and monarch will
136 * be frozen after timeout of the INIT rendezvous.
137 * => TBD: freeze them without waiting timeout
138 */
Zou Nan haia79561132006-12-07 09:51:35 -0800139 kdump_smp_send_stop();
Simon Horman0ac1fac2007-02-14 16:15:02 +0900140 /* not all cpu response to IPI, send INIT to freeze them */
Hidetoshi Seto59599062009-08-06 14:51:57 -0700141 if (kdump_wait_cpu_freeze()) {
Zou Nan haia79561132006-12-07 09:51:35 -0800142 kdump_smp_send_init();
Hidetoshi Seto59599062009-08-06 14:51:57 -0700143 /* wait again, don't go ahead if possible */
144 kdump_wait_cpu_freeze();
Zou Nan haia79561132006-12-07 09:51:35 -0800145 }
146#endif
147}
148
149static void
150machine_kdump_on_init(void)
151{
Takao Indoh072f0422008-04-15 05:59:54 -0400152 crash_save_vmcoreinfo();
Zou Nan haia79561132006-12-07 09:51:35 -0800153 local_irq_disable();
154 kexec_disable_iosapic();
155 machine_kexec(ia64_kimage);
156}
157
158void
159kdump_cpu_freeze(struct unw_frame_info *info, void *arg)
160{
161 int cpuid;
Hidetoshi Seto4295ab32009-08-06 14:51:56 -0700162
Zou Nan haia79561132006-12-07 09:51:35 -0800163 local_irq_disable();
164 cpuid = smp_processor_id();
165 crash_save_this_cpu();
166 current->thread.ksp = (__u64)info->sw - 16;
Hidetoshi Seto4295ab32009-08-06 14:51:56 -0700167
168 ia64_set_psr_mc(); /* mask MCA/INIT and stop reentrance */
169
Simon Horman0ac1fac2007-02-14 16:15:02 +0900170 atomic_inc(&kdump_cpu_frozen);
Zou Nan haia79561132006-12-07 09:51:35 -0800171 kdump_status[cpuid] = 1;
172 mb();
Magnus Dammbcb9b992007-02-05 15:43:42 +0900173 for (;;)
174 cpu_relax();
Zou Nan haia79561132006-12-07 09:51:35 -0800175}
176
177static int
178kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data)
179{
180 struct ia64_mca_notify_die *nd;
181 struct die_args *args = data;
182
Hidetoshi Seto59599062009-08-06 14:51:57 -0700183 if (atomic_read(&kdump_in_progress)) {
184 switch (val) {
185 case DIE_INIT_MONARCH_LEAVE:
186 if (!kdump_freeze_monarch)
187 break;
188 /* fall through */
189 case DIE_INIT_SLAVE_LEAVE:
190 case DIE_MCA_RENDZVOUS_LEAVE:
191 unw_init_running(kdump_cpu_freeze, NULL);
192 break;
193 }
194 }
195
Hidetoshi Setob0247a52008-04-08 13:31:47 +0900196 if (!kdump_on_init && !kdump_on_fatal_mca)
Zou Nan haia79561132006-12-07 09:51:35 -0800197 return NOTIFY_DONE;
198
Kenji Kaneshige2010d7f2007-09-01 16:37:48 +0900199 if (!ia64_kimage) {
200 if (val == DIE_INIT_MONARCH_LEAVE)
201 ia64_mca_printk(KERN_NOTICE
202 "%s: kdump not configured\n",
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800203 __func__);
Kenji Kaneshige2010d7f2007-09-01 16:37:48 +0900204 return NOTIFY_DONE;
205 }
206
Jay Lan311f5942007-04-03 17:53:42 -0700207 if (val != DIE_INIT_MONARCH_LEAVE &&
Jay Lan311f5942007-04-03 17:53:42 -0700208 val != DIE_INIT_MONARCH_PROCESS &&
Zou Nan haia79561132006-12-07 09:51:35 -0800209 val != DIE_MCA_MONARCH_LEAVE)
210 return NOTIFY_DONE;
211
212 nd = (struct ia64_mca_notify_die *)args->err;
Zou Nan haia79561132006-12-07 09:51:35 -0800213
214 switch (val) {
Hidetoshi Seto3975aff2008-04-08 13:33:08 +0900215 case DIE_INIT_MONARCH_PROCESS:
Hidetoshi Seto59599062009-08-06 14:51:57 -0700216 /* Reason code 1 means machine check rendezvous*/
217 if (kdump_on_init && (nd->sos->rv_rc != 1)) {
Hidetoshi Seto1726b082009-08-06 14:51:57 -0700218 if (atomic_inc_return(&kdump_in_progress) != 1)
219 kdump_freeze_monarch = 1;
Hidetoshi Seto3975aff2008-04-08 13:33:08 +0900220 *(nd->monarch_cpu) = -1;
221 }
222 break;
223 case DIE_INIT_MONARCH_LEAVE:
Hidetoshi Seto59599062009-08-06 14:51:57 -0700224 /* Reason code 1 means machine check rendezvous*/
225 if (kdump_on_init && (nd->sos->rv_rc != 1))
226 machine_kdump_on_init();
Hidetoshi Seto3975aff2008-04-08 13:33:08 +0900227 break;
228 case DIE_MCA_MONARCH_LEAVE:
Hidetoshi Seto4fa2f0e2008-04-17 17:00:37 +0900229 /* *(nd->data) indicate if MCA is recoverable */
230 if (kdump_on_fatal_mca && !(*(nd->data))) {
Hidetoshi Seto1726b082009-08-06 14:51:57 -0700231 if (atomic_inc_return(&kdump_in_progress) == 1) {
232 *(nd->monarch_cpu) = -1;
233 machine_kdump_on_init();
234 }
235 /* We got fatal MCA while kdump!? No way!! */
Hidetoshi Seto3975aff2008-04-08 13:33:08 +0900236 }
237 break;
Zou Nan haia79561132006-12-07 09:51:35 -0800238 }
239 return NOTIFY_DONE;
240}
241
242#ifdef CONFIG_SYSCTL
Hidetoshi Setob0247a52008-04-08 13:31:47 +0900243static ctl_table kdump_ctl_table[] = {
Zou Nan haia79561132006-12-07 09:51:35 -0800244 {
245 .ctl_name = CTL_UNNUMBERED,
246 .procname = "kdump_on_init",
247 .data = &kdump_on_init,
248 .maxlen = sizeof(int),
249 .mode = 0644,
250 .proc_handler = &proc_dointvec,
251 },
Hidetoshi Setob0247a52008-04-08 13:31:47 +0900252 {
253 .ctl_name = CTL_UNNUMBERED,
254 .procname = "kdump_on_fatal_mca",
255 .data = &kdump_on_fatal_mca,
256 .maxlen = sizeof(int),
257 .mode = 0644,
258 .proc_handler = &proc_dointvec,
259 },
Zou Nan haia79561132006-12-07 09:51:35 -0800260 { .ctl_name = 0 }
261};
262
263static ctl_table sys_table[] = {
264 {
265 .ctl_name = CTL_KERN,
266 .procname = "kernel",
267 .mode = 0555,
Hidetoshi Setob0247a52008-04-08 13:31:47 +0900268 .child = kdump_ctl_table,
Zou Nan haia79561132006-12-07 09:51:35 -0800269 },
270 { .ctl_name = 0 }
271};
272#endif
273
274static int
275machine_crash_setup(void)
276{
Jay Lan311f5942007-04-03 17:53:42 -0700277 /* be notified before default_monarch_init_process */
Zou Nan haia79561132006-12-07 09:51:35 -0800278 static struct notifier_block kdump_init_notifier_nb = {
279 .notifier_call = kdump_init_notifier,
Jay Lan311f5942007-04-03 17:53:42 -0700280 .priority = 1,
Zou Nan haia79561132006-12-07 09:51:35 -0800281 };
282 int ret;
Zou Nan haia79561132006-12-07 09:51:35 -0800283 if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
284 return ret;
285#ifdef CONFIG_SYSCTL
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800286 register_sysctl_table(sys_table);
Zou Nan haia79561132006-12-07 09:51:35 -0800287#endif
288 return 0;
289}
290
291__initcall(machine_crash_setup);
292