blob: f065093f8e9be08789b30c90ee6374c5f3c76869 [file] [log] [blame]
Zou Nan haia7956112006-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 haia7956112006-12-07 09:51:35 -080020
Zou Nan haia7956112006-12-07 09:51:35 -080021#include <asm/mca.h>
Zou Nan haia7956112006-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 haia7956112006-12-07 09:51:35 -080025atomic_t kdump_in_progress;
Simon Horman0ac1fac2007-02-14 16:15:02 +090026static int kdump_on_init = 1;
Hidetoshi Setob0247a52008-04-08 13:31:47 +090027static int kdump_on_fatal_mca = 1;
Zou Nan haia7956112006-12-07 09:51:35 -080028
29static 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
45static void
46final_note(void *buf)
47{
48 memset(buf, 0, sizeof(struct elf_note));
49}
50
51extern void ia64_dump_cpu_regs(void *);
52
53static DEFINE_PER_CPU(struct elf_prstatus, elf_prstatus);
54
55void
Al Viroccbebda2007-02-09 16:38:10 +000056crash_save_this_cpu(void)
Zou Nan haia7956112006-12-07 09:51:35 -080057{
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;
Simon Horman6672f762007-05-08 00:28:22 -070078 buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS, prstatus,
Zou Nan haia7956112006-12-07 09:51:35 -080079 sizeof(*prstatus));
80 final_note(buf);
81}
82
Magnus Dammbcb9b992007-02-05 15:43:42 +090083#ifdef CONFIG_SMP
Zou Nan haia7956112006-12-07 09:51:35 -080084static int
85kdump_wait_cpu_freeze(void)
86{
87 int cpu_num = num_online_cpus() - 1;
88 int timeout = 1000;
89 while(timeout-- > 0) {
Simon Horman0ac1fac2007-02-14 16:15:02 +090090 if (atomic_read(&kdump_cpu_frozen) == cpu_num)
Zou Nan haia7956112006-12-07 09:51:35 -080091 return 0;
92 udelay(1000);
93 }
94 return 1;
95}
Magnus Dammbcb9b992007-02-05 15:43:42 +090096#endif
Zou Nan haia7956112006-12-07 09:51:35 -080097
98void
99machine_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();
Simon Horman0ac1fac2007-02-14 16:15:02 +0900112 /* not all cpu response to IPI, send INIT to freeze them */
Zou Nan haia7956112006-12-07 09:51:35 -0800113 if (kdump_wait_cpu_freeze() && kdump_on_init) {
Zou Nan haia7956112006-12-07 09:51:35 -0800114 kdump_smp_send_init();
115 }
116#endif
117}
118
119static void
120machine_kdump_on_init(void)
121{
Takao Indoh072f0422008-04-15 05:59:54 -0400122 crash_save_vmcoreinfo();
Zou Nan haia7956112006-12-07 09:51:35 -0800123 local_irq_disable();
124 kexec_disable_iosapic();
125 machine_kexec(ia64_kimage);
126}
127
128void
129kdump_cpu_freeze(struct unw_frame_info *info, void *arg)
130{
131 int cpuid;
132 local_irq_disable();
133 cpuid = smp_processor_id();
134 crash_save_this_cpu();
135 current->thread.ksp = (__u64)info->sw - 16;
Simon Horman0ac1fac2007-02-14 16:15:02 +0900136 atomic_inc(&kdump_cpu_frozen);
Zou Nan haia7956112006-12-07 09:51:35 -0800137 kdump_status[cpuid] = 1;
138 mb();
Magnus Dammbcb9b992007-02-05 15:43:42 +0900139#ifdef CONFIG_HOTPLUG_CPU
140 if (cpuid != 0)
Zou Nan haia7956112006-12-07 09:51:35 -0800141 ia64_jump_to_sal(&sal_boot_rendez_state[cpuid]);
Magnus Dammbcb9b992007-02-05 15:43:42 +0900142#endif
143 for (;;)
144 cpu_relax();
Zou Nan haia7956112006-12-07 09:51:35 -0800145}
146
147static int
148kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data)
149{
150 struct ia64_mca_notify_die *nd;
151 struct die_args *args = data;
152
Hidetoshi Setob0247a52008-04-08 13:31:47 +0900153 if (!kdump_on_init && !kdump_on_fatal_mca)
Zou Nan haia7956112006-12-07 09:51:35 -0800154 return NOTIFY_DONE;
155
Kenji Kaneshige2010d7f2007-09-01 16:37:48 +0900156 if (!ia64_kimage) {
157 if (val == DIE_INIT_MONARCH_LEAVE)
158 ia64_mca_printk(KERN_NOTICE
159 "%s: kdump not configured\n",
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800160 __func__);
Kenji Kaneshige2010d7f2007-09-01 16:37:48 +0900161 return NOTIFY_DONE;
162 }
163
Jay Lan311f5942007-04-03 17:53:42 -0700164 if (val != DIE_INIT_MONARCH_LEAVE &&
165 val != DIE_INIT_SLAVE_LEAVE &&
166 val != DIE_INIT_MONARCH_PROCESS &&
Zou Nan haia7956112006-12-07 09:51:35 -0800167 val != DIE_MCA_RENDZVOUS_LEAVE &&
168 val != DIE_MCA_MONARCH_LEAVE)
169 return NOTIFY_DONE;
170
171 nd = (struct ia64_mca_notify_die *)args->err;
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700172 /* Reason code 1 means machine check rendezvous*/
Jay Lan311f5942007-04-03 17:53:42 -0700173 if ((val == DIE_INIT_MONARCH_LEAVE || val == DIE_INIT_SLAVE_LEAVE
174 || val == DIE_INIT_MONARCH_PROCESS) && nd->sos->rv_rc == 1)
Zou Nan haia7956112006-12-07 09:51:35 -0800175 return NOTIFY_DONE;
176
177 switch (val) {
Hidetoshi Seto3975aff2008-04-08 13:33:08 +0900178 case DIE_INIT_MONARCH_PROCESS:
179 if (kdump_on_init) {
180 atomic_set(&kdump_in_progress, 1);
181 *(nd->monarch_cpu) = -1;
182 }
183 break;
184 case DIE_INIT_MONARCH_LEAVE:
185 if (kdump_on_init)
186 machine_kdump_on_init();
187 break;
188 case DIE_INIT_SLAVE_LEAVE:
189 if (atomic_read(&kdump_in_progress))
190 unw_init_running(kdump_cpu_freeze, NULL);
191 break;
192 case DIE_MCA_RENDZVOUS_LEAVE:
193 if (atomic_read(&kdump_in_progress))
194 unw_init_running(kdump_cpu_freeze, NULL);
195 break;
196 case DIE_MCA_MONARCH_LEAVE:
Hidetoshi Seto4fa2f0e2008-04-17 17:00:37 +0900197 /* *(nd->data) indicate if MCA is recoverable */
198 if (kdump_on_fatal_mca && !(*(nd->data))) {
Hidetoshi Seto3975aff2008-04-08 13:33:08 +0900199 atomic_set(&kdump_in_progress, 1);
200 *(nd->monarch_cpu) = -1;
201 machine_kdump_on_init();
202 }
203 break;
Zou Nan haia7956112006-12-07 09:51:35 -0800204 }
205 return NOTIFY_DONE;
206}
207
208#ifdef CONFIG_SYSCTL
Hidetoshi Setob0247a52008-04-08 13:31:47 +0900209static ctl_table kdump_ctl_table[] = {
Zou Nan haia7956112006-12-07 09:51:35 -0800210 {
211 .ctl_name = CTL_UNNUMBERED,
212 .procname = "kdump_on_init",
213 .data = &kdump_on_init,
214 .maxlen = sizeof(int),
215 .mode = 0644,
216 .proc_handler = &proc_dointvec,
217 },
Hidetoshi Setob0247a52008-04-08 13:31:47 +0900218 {
219 .ctl_name = CTL_UNNUMBERED,
220 .procname = "kdump_on_fatal_mca",
221 .data = &kdump_on_fatal_mca,
222 .maxlen = sizeof(int),
223 .mode = 0644,
224 .proc_handler = &proc_dointvec,
225 },
Zou Nan haia7956112006-12-07 09:51:35 -0800226 { .ctl_name = 0 }
227};
228
229static ctl_table sys_table[] = {
230 {
231 .ctl_name = CTL_KERN,
232 .procname = "kernel",
233 .mode = 0555,
Hidetoshi Setob0247a52008-04-08 13:31:47 +0900234 .child = kdump_ctl_table,
Zou Nan haia7956112006-12-07 09:51:35 -0800235 },
236 { .ctl_name = 0 }
237};
238#endif
239
240static int
241machine_crash_setup(void)
242{
Jay Lan311f5942007-04-03 17:53:42 -0700243 /* be notified before default_monarch_init_process */
Zou Nan haia7956112006-12-07 09:51:35 -0800244 static struct notifier_block kdump_init_notifier_nb = {
245 .notifier_call = kdump_init_notifier,
Jay Lan311f5942007-04-03 17:53:42 -0700246 .priority = 1,
Zou Nan haia7956112006-12-07 09:51:35 -0800247 };
248 int ret;
Zou Nan haia7956112006-12-07 09:51:35 -0800249 if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
250 return ret;
251#ifdef CONFIG_SYSCTL
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800252 register_sysctl_table(sys_table);
Zou Nan haia7956112006-12-07 09:51:35 -0800253#endif
254 return 0;
255}
256
257__initcall(machine_crash_setup);
258