| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 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> | 
| Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 19 | #include <linux/kdebug.h> | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 20 |  | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 21 | #include <asm/mca.h> | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 22 |  | 
|  | 23 | int kdump_status[NR_CPUS]; | 
| Simon Horman | 0ac1fac | 2007-02-14 16:15:02 +0900 | [diff] [blame] | 24 | static atomic_t kdump_cpu_frozen; | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 25 | atomic_t kdump_in_progress; | 
| Simon Horman | 0ac1fac | 2007-02-14 16:15:02 +0900 | [diff] [blame] | 26 | static int kdump_on_init = 1; | 
| Hidetoshi Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 27 | static int kdump_on_fatal_mca = 1; | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 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 | 
| Al Viro | ccbebda | 2007-02-09 16:38:10 +0000 | [diff] [blame] | 56 | crash_save_this_cpu(void) | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 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; | 
| Simon Horman | 6672f76 | 2007-05-08 00:28:22 -0700 | [diff] [blame] | 78 | buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS, prstatus, | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 79 | sizeof(*prstatus)); | 
|  | 80 | final_note(buf); | 
|  | 81 | } | 
|  | 82 |  | 
| Magnus Damm | bcb9b99 | 2007-02-05 15:43:42 +0900 | [diff] [blame] | 83 | #ifdef CONFIG_SMP | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 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) { | 
| Simon Horman | 0ac1fac | 2007-02-14 16:15:02 +0900 | [diff] [blame] | 90 | if (atomic_read(&kdump_cpu_frozen) == cpu_num) | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 91 | return 0; | 
|  | 92 | udelay(1000); | 
|  | 93 | } | 
|  | 94 | return 1; | 
|  | 95 | } | 
| Magnus Damm | bcb9b99 | 2007-02-05 15:43:42 +0900 | [diff] [blame] | 96 | #endif | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 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(); | 
| Simon Horman | 0ac1fac | 2007-02-14 16:15:02 +0900 | [diff] [blame] | 112 | /* not all cpu response to IPI, send INIT to freeze them */ | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 113 | if (kdump_wait_cpu_freeze() && kdump_on_init) 	{ | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 114 | kdump_smp_send_init(); | 
|  | 115 | } | 
|  | 116 | #endif | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | static void | 
|  | 120 | machine_kdump_on_init(void) | 
|  | 121 | { | 
| Takao Indoh | 072f042 | 2008-04-15 05:59:54 -0400 | [diff] [blame] | 122 | crash_save_vmcoreinfo(); | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 123 | local_irq_disable(); | 
|  | 124 | kexec_disable_iosapic(); | 
|  | 125 | machine_kexec(ia64_kimage); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | void | 
|  | 129 | kdump_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 Horman | 0ac1fac | 2007-02-14 16:15:02 +0900 | [diff] [blame] | 136 | atomic_inc(&kdump_cpu_frozen); | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 137 | kdump_status[cpuid] = 1; | 
|  | 138 | mb(); | 
| Magnus Damm | bcb9b99 | 2007-02-05 15:43:42 +0900 | [diff] [blame] | 139 | #ifdef CONFIG_HOTPLUG_CPU | 
|  | 140 | if (cpuid != 0) | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 141 | ia64_jump_to_sal(&sal_boot_rendez_state[cpuid]); | 
| Magnus Damm | bcb9b99 | 2007-02-05 15:43:42 +0900 | [diff] [blame] | 142 | #endif | 
|  | 143 | for (;;) | 
|  | 144 | cpu_relax(); | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
|  | 147 | static int | 
|  | 148 | kdump_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 Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 153 | if (!kdump_on_init && !kdump_on_fatal_mca) | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 154 | return NOTIFY_DONE; | 
|  | 155 |  | 
| Kenji Kaneshige | 2010d7f | 2007-09-01 16:37:48 +0900 | [diff] [blame] | 156 | if (!ia64_kimage) { | 
|  | 157 | if (val == DIE_INIT_MONARCH_LEAVE) | 
|  | 158 | ia64_mca_printk(KERN_NOTICE | 
|  | 159 | "%s: kdump not configured\n", | 
| Harvey Harrison | d4ed808 | 2008-03-04 15:15:00 -0800 | [diff] [blame] | 160 | __func__); | 
| Kenji Kaneshige | 2010d7f | 2007-09-01 16:37:48 +0900 | [diff] [blame] | 161 | return NOTIFY_DONE; | 
|  | 162 | } | 
|  | 163 |  | 
| Jay Lan | 311f594 | 2007-04-03 17:53:42 -0700 | [diff] [blame] | 164 | if (val != DIE_INIT_MONARCH_LEAVE && | 
|  | 165 | val != DIE_INIT_SLAVE_LEAVE && | 
|  | 166 | val != DIE_INIT_MONARCH_PROCESS && | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 167 | 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 Arlott | 72fdbdc | 2007-05-11 14:55:43 -0700 | [diff] [blame] | 172 | /* Reason code 1 means machine check rendezvous*/ | 
| Jay Lan | 311f594 | 2007-04-03 17:53:42 -0700 | [diff] [blame] | 173 | if ((val == DIE_INIT_MONARCH_LEAVE || val == DIE_INIT_SLAVE_LEAVE | 
|  | 174 | || val == DIE_INIT_MONARCH_PROCESS) && nd->sos->rv_rc == 1) | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 175 | return NOTIFY_DONE; | 
|  | 176 |  | 
|  | 177 | switch (val) { | 
| Hidetoshi Seto | 3975aff | 2008-04-08 13:33:08 +0900 | [diff] [blame] | 178 | 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 Seto | 4fa2f0e | 2008-04-17 17:00:37 +0900 | [diff] [blame] | 197 | /* *(nd->data) indicate if MCA is recoverable */ | 
|  | 198 | if (kdump_on_fatal_mca && !(*(nd->data))) { | 
| Hidetoshi Seto | 3975aff | 2008-04-08 13:33:08 +0900 | [diff] [blame] | 199 | atomic_set(&kdump_in_progress, 1); | 
|  | 200 | *(nd->monarch_cpu) = -1; | 
|  | 201 | machine_kdump_on_init(); | 
|  | 202 | } | 
|  | 203 | break; | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 204 | } | 
|  | 205 | return NOTIFY_DONE; | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | #ifdef CONFIG_SYSCTL | 
| Hidetoshi Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 209 | static ctl_table kdump_ctl_table[] = { | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 210 | { | 
|  | 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 Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 218 | { | 
|  | 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 hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 226 | { .ctl_name = 0 } | 
|  | 227 | }; | 
|  | 228 |  | 
|  | 229 | static ctl_table sys_table[] = { | 
|  | 230 | { | 
|  | 231 | .ctl_name = CTL_KERN, | 
|  | 232 | .procname = "kernel", | 
|  | 233 | .mode = 0555, | 
| Hidetoshi Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 234 | .child = kdump_ctl_table, | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 235 | }, | 
|  | 236 | { .ctl_name = 0 } | 
|  | 237 | }; | 
|  | 238 | #endif | 
|  | 239 |  | 
|  | 240 | static int | 
|  | 241 | machine_crash_setup(void) | 
|  | 242 | { | 
| Jay Lan | 311f594 | 2007-04-03 17:53:42 -0700 | [diff] [blame] | 243 | /* be notified before default_monarch_init_process */ | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 244 | static struct notifier_block kdump_init_notifier_nb = { | 
|  | 245 | .notifier_call = kdump_init_notifier, | 
| Jay Lan | 311f594 | 2007-04-03 17:53:42 -0700 | [diff] [blame] | 246 | .priority = 1, | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 247 | }; | 
|  | 248 | int ret; | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 249 | if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0) | 
|  | 250 | return ret; | 
|  | 251 | #ifdef CONFIG_SYSCTL | 
| Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 252 | register_sysctl_table(sys_table); | 
| Zou Nan hai | a795611 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 253 | #endif | 
|  | 254 | return 0; | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | __initcall(machine_crash_setup); | 
|  | 258 |  |