]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/ia64/kernel/crash.c
[IA64] kdump: add kdump_on_fatal_mca
[linux-2.6-omap-h63xx.git] / arch / ia64 / kernel / crash.c
1 /*
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>
19 #include <linux/kdebug.h>
20
21 #include <asm/mca.h>
22
23 int kdump_status[NR_CPUS];
24 static atomic_t kdump_cpu_frozen;
25 atomic_t kdump_in_progress;
26 static int kdump_on_init = 1;
27 static int kdump_on_fatal_mca = 1;
28
29 static inline Elf64_Word
30 *append_elf_note(Elf64_Word *buf, char *name, unsigned type, void *data,
31                 size_t data_len)
32 {
33         struct elf_note *note = (struct elf_note *)buf;
34         note->n_namesz = strlen(name) + 1;
35         note->n_descsz = data_len;
36         note->n_type   = type;
37         buf += (sizeof(*note) + 3)/4;
38         memcpy(buf, name, note->n_namesz);
39         buf += (note->n_namesz + 3)/4;
40         memcpy(buf, data, data_len);
41         buf += (data_len + 3)/4;
42         return buf;
43 }
44
45 static void
46 final_note(void *buf)
47 {
48         memset(buf, 0, sizeof(struct elf_note));
49 }
50
51 extern void ia64_dump_cpu_regs(void *);
52
53 static DEFINE_PER_CPU(struct elf_prstatus, elf_prstatus);
54
55 void
56 crash_save_this_cpu(void)
57 {
58         void *buf;
59         unsigned long cfm, sof, sol;
60
61         int cpu = smp_processor_id();
62         struct elf_prstatus *prstatus = &per_cpu(elf_prstatus, cpu);
63
64         elf_greg_t *dst = (elf_greg_t *)&(prstatus->pr_reg);
65         memset(prstatus, 0, sizeof(*prstatus));
66         prstatus->pr_pid = current->pid;
67
68         ia64_dump_cpu_regs(dst);
69         cfm = dst[43];
70         sol = (cfm >> 7) & 0x7f;
71         sof = cfm & 0x7f;
72         dst[46] = (unsigned long)ia64_rse_skip_regs((unsigned long *)dst[46],
73                         sof - sol);
74
75         buf = (u64 *) per_cpu_ptr(crash_notes, cpu);
76         if (!buf)
77                 return;
78         buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS, prstatus,
79                         sizeof(*prstatus));
80         final_note(buf);
81 }
82
83 #ifdef CONFIG_SMP
84 static int
85 kdump_wait_cpu_freeze(void)
86 {
87         int cpu_num = num_online_cpus() - 1;
88         int timeout = 1000;
89         while(timeout-- > 0) {
90                 if (atomic_read(&kdump_cpu_frozen) == cpu_num)
91                         return 0;
92                 udelay(1000);
93         }
94         return 1;
95 }
96 #endif
97
98 void
99 machine_crash_shutdown(struct pt_regs *pt)
100 {
101         /* This function is only called after the system
102          * has paniced or is otherwise in a critical state.
103          * The minimum amount of code to allow a kexec'd kernel
104          * to run successfully needs to happen here.
105          *
106          * In practice this means shooting down the other cpus in
107          * an SMP system.
108          */
109         kexec_disable_iosapic();
110 #ifdef CONFIG_SMP
111         kdump_smp_send_stop();
112         /* not all cpu response to IPI, send INIT to freeze them */
113         if (kdump_wait_cpu_freeze() && kdump_on_init)   {
114                 kdump_smp_send_init();
115         }
116 #endif
117 }
118
119 static void
120 machine_kdump_on_init(void)
121 {
122         local_irq_disable();
123         kexec_disable_iosapic();
124         machine_kexec(ia64_kimage);
125 }
126
127 void
128 kdump_cpu_freeze(struct unw_frame_info *info, void *arg)
129 {
130         int cpuid;
131         local_irq_disable();
132         cpuid = smp_processor_id();
133         crash_save_this_cpu();
134         current->thread.ksp = (__u64)info->sw - 16;
135         atomic_inc(&kdump_cpu_frozen);
136         kdump_status[cpuid] = 1;
137         mb();
138 #ifdef CONFIG_HOTPLUG_CPU
139         if (cpuid != 0)
140                 ia64_jump_to_sal(&sal_boot_rendez_state[cpuid]);
141 #endif
142         for (;;)
143                 cpu_relax();
144 }
145
146 static int
147 kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data)
148 {
149         struct ia64_mca_notify_die *nd;
150         struct die_args *args = data;
151
152         if (!kdump_on_init && !kdump_on_fatal_mca)
153                 return NOTIFY_DONE;
154
155         if (!ia64_kimage) {
156                 if (val == DIE_INIT_MONARCH_LEAVE)
157                         ia64_mca_printk(KERN_NOTICE
158                                         "%s: kdump not configured\n",
159                                         __func__);
160                 return NOTIFY_DONE;
161         }
162
163         if (val != DIE_INIT_MONARCH_LEAVE &&
164             val != DIE_INIT_SLAVE_LEAVE &&
165             val != DIE_INIT_MONARCH_PROCESS &&
166             val != DIE_MCA_RENDZVOUS_LEAVE &&
167             val != DIE_MCA_MONARCH_LEAVE)
168                 return NOTIFY_DONE;
169
170         nd = (struct ia64_mca_notify_die *)args->err;
171         /* Reason code 1 means machine check rendezvous*/
172         if ((val == DIE_INIT_MONARCH_LEAVE || val == DIE_INIT_SLAVE_LEAVE
173             || val == DIE_INIT_MONARCH_PROCESS) && nd->sos->rv_rc == 1)
174                 return NOTIFY_DONE;
175
176         switch (val) {
177                 case DIE_INIT_MONARCH_PROCESS:
178                         if (kdump_on_init) {
179                                 atomic_set(&kdump_in_progress, 1);
180                                 *(nd->monarch_cpu) = -1;
181                         }
182                         break;
183                 case DIE_INIT_MONARCH_LEAVE:
184                         if (kdump_on_init)
185                                 machine_kdump_on_init();
186                         break;
187                 case DIE_INIT_SLAVE_LEAVE:
188                         if (atomic_read(&kdump_in_progress))
189                                 unw_init_running(kdump_cpu_freeze, NULL);
190                         break;
191                 case DIE_MCA_RENDZVOUS_LEAVE:
192                         if (atomic_read(&kdump_in_progress))
193                                 unw_init_running(kdump_cpu_freeze, NULL);
194                         break;
195                 case DIE_MCA_MONARCH_LEAVE:
196                         /* die_register->signr indicate if MCA is recoverable */
197                         if (kdump_on_fatal_mca && !args->signr) {
198                                 atomic_set(&kdump_in_progress, 1);
199                                 *(nd->monarch_cpu) = -1;
200                                 machine_kdump_on_init();
201                         }
202                         break;
203         }
204         return NOTIFY_DONE;
205 }
206
207 #ifdef CONFIG_SYSCTL
208 static ctl_table kdump_ctl_table[] = {
209         {
210                 .ctl_name = CTL_UNNUMBERED,
211                 .procname = "kdump_on_init",
212                 .data = &kdump_on_init,
213                 .maxlen = sizeof(int),
214                 .mode = 0644,
215                 .proc_handler = &proc_dointvec,
216         },
217         {
218                 .ctl_name = CTL_UNNUMBERED,
219                 .procname = "kdump_on_fatal_mca",
220                 .data = &kdump_on_fatal_mca,
221                 .maxlen = sizeof(int),
222                 .mode = 0644,
223                 .proc_handler = &proc_dointvec,
224         },
225         { .ctl_name = 0 }
226 };
227
228 static ctl_table sys_table[] = {
229         {
230           .ctl_name = CTL_KERN,
231           .procname = "kernel",
232           .mode = 0555,
233           .child = kdump_ctl_table,
234         },
235         { .ctl_name = 0 }
236 };
237 #endif
238
239 static int
240 machine_crash_setup(void)
241 {
242         /* be notified before default_monarch_init_process */
243         static struct notifier_block kdump_init_notifier_nb = {
244                 .notifier_call = kdump_init_notifier,
245                 .priority = 1,
246         };
247         int ret;
248         if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
249                 return ret;
250 #ifdef CONFIG_SYSCTL
251         register_sysctl_table(sys_table);
252 #endif
253         return 0;
254 }
255
256 __initcall(machine_crash_setup);
257