Eric W. Biederman | 5234f5e | 2005-06-25 14:58:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Architecture specific (x86_64) 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> |
Vivek Goyal | ec9ce0d | 2006-01-09 20:51:49 -0800 | [diff] [blame] | 14 | #include <linux/irq.h> |
Eric W. Biederman | 5234f5e | 2005-06-25 14:58:02 -0700 | [diff] [blame] | 15 | #include <linux/reboot.h> |
| 16 | #include <linux/kexec.h> |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 17 | #include <linux/delay.h> |
Vivek Goyal | ec9ce0d | 2006-01-09 20:51:49 -0800 | [diff] [blame] | 18 | #include <linux/elf.h> |
| 19 | #include <linux/elfcore.h> |
Eric W. Biederman | 5234f5e | 2005-06-25 14:58:02 -0700 | [diff] [blame] | 20 | |
| 21 | #include <asm/processor.h> |
| 22 | #include <asm/hardirq.h> |
| 23 | #include <asm/nmi.h> |
| 24 | #include <asm/hw_irq.h> |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 25 | #include <asm/mach_apic.h> |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame^] | 26 | #include <asm/kdebug.h> |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 27 | |
| 28 | /* This keeps a track of which one is crashing cpu. */ |
| 29 | static int crashing_cpu; |
| 30 | |
Vivek Goyal | ec9ce0d | 2006-01-09 20:51:49 -0800 | [diff] [blame] | 31 | static u32 *append_elf_note(u32 *buf, char *name, unsigned type, |
| 32 | void *data, size_t data_len) |
| 33 | { |
| 34 | struct elf_note note; |
| 35 | |
| 36 | note.n_namesz = strlen(name) + 1; |
| 37 | note.n_descsz = data_len; |
| 38 | note.n_type = type; |
| 39 | memcpy(buf, ¬e, sizeof(note)); |
| 40 | buf += (sizeof(note) +3)/4; |
| 41 | memcpy(buf, name, note.n_namesz); |
| 42 | buf += (note.n_namesz + 3)/4; |
| 43 | memcpy(buf, data, note.n_descsz); |
| 44 | buf += (note.n_descsz + 3)/4; |
| 45 | |
| 46 | return buf; |
| 47 | } |
| 48 | |
| 49 | static void final_note(u32 *buf) |
| 50 | { |
| 51 | struct elf_note note; |
| 52 | |
| 53 | note.n_namesz = 0; |
| 54 | note.n_descsz = 0; |
| 55 | note.n_type = 0; |
| 56 | memcpy(buf, ¬e, sizeof(note)); |
| 57 | } |
| 58 | |
| 59 | static void crash_save_this_cpu(struct pt_regs *regs, int cpu) |
| 60 | { |
| 61 | struct elf_prstatus prstatus; |
| 62 | u32 *buf; |
| 63 | |
| 64 | if ((cpu < 0) || (cpu >= NR_CPUS)) |
| 65 | return; |
| 66 | |
| 67 | /* Using ELF notes here is opportunistic. |
| 68 | * I need a well defined structure format |
| 69 | * for the data I pass, and I need tags |
| 70 | * on the data to indicate what information I have |
| 71 | * squirrelled away. ELF notes happen to provide |
| 72 | * all of that that no need to invent something new. |
| 73 | */ |
| 74 | |
| 75 | buf = (u32*)per_cpu_ptr(crash_notes, cpu); |
| 76 | |
| 77 | if (!buf) |
| 78 | return; |
| 79 | |
| 80 | memset(&prstatus, 0, sizeof(prstatus)); |
| 81 | prstatus.pr_pid = current->pid; |
| 82 | elf_core_copy_regs(&prstatus.pr_reg, regs); |
| 83 | buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus, |
| 84 | sizeof(prstatus)); |
| 85 | final_note(buf); |
| 86 | } |
| 87 | |
| 88 | static void crash_save_self(struct pt_regs *regs) |
| 89 | { |
| 90 | int cpu; |
| 91 | |
| 92 | cpu = smp_processor_id(); |
| 93 | crash_save_this_cpu(regs, cpu); |
| 94 | } |
| 95 | |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 96 | #ifdef CONFIG_SMP |
| 97 | static atomic_t waiting_for_crash_ipi; |
| 98 | |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame^] | 99 | static int crash_nmi_callback(struct notifier_block *self, |
| 100 | unsigned long val, void *data) |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 101 | { |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame^] | 102 | struct pt_regs *regs; |
| 103 | int cpu; |
| 104 | |
| 105 | if (val != DIE_NMI) |
| 106 | return NOTIFY_OK; |
| 107 | |
| 108 | regs = ((struct die_args *)data)->regs; |
| 109 | cpu = raw_smp_processor_id(); |
| 110 | |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 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) |
| 117 | return 1; |
| 118 | local_irq_disable(); |
| 119 | |
Vivek Goyal | ec9ce0d | 2006-01-09 20:51:49 -0800 | [diff] [blame] | 120 | crash_save_this_cpu(regs, cpu); |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 121 | disable_local_APIC(); |
| 122 | atomic_dec(&waiting_for_crash_ipi); |
| 123 | /* Assume hlt works */ |
| 124 | for(;;) |
Jan Beulich | 46d13a3 | 2006-06-26 13:57:59 +0200 | [diff] [blame] | 125 | halt(); |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 126 | |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | static void smp_send_nmi_allbutself(void) |
| 131 | { |
Keith Owens | 45486f8 | 2006-06-26 13:59:41 +0200 | [diff] [blame] | 132 | send_IPI_allbutself(NMI_VECTOR); |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | * This code is a best effort heuristic to get the |
| 137 | * other cpus to stop executing. So races with |
| 138 | * cpu hotplug shouldn't matter. |
| 139 | */ |
| 140 | |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame^] | 141 | static struct notifier_block crash_nmi_nb = { |
| 142 | .notifier_call = crash_nmi_callback, |
| 143 | }; |
| 144 | |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 145 | static void nmi_shootdown_cpus(void) |
| 146 | { |
| 147 | unsigned long msecs; |
| 148 | |
| 149 | atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1); |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame^] | 150 | if (register_die_notifier(&crash_nmi_nb)) |
| 151 | return; /* return what? */ |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * Ensure the new callback function is set before sending |
| 155 | * out the NMI |
| 156 | */ |
| 157 | wmb(); |
| 158 | |
| 159 | smp_send_nmi_allbutself(); |
| 160 | |
| 161 | msecs = 1000; /* Wait at most a second for the other cpus to stop */ |
| 162 | while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) { |
| 163 | mdelay(1); |
| 164 | msecs--; |
| 165 | } |
| 166 | /* Leave the nmi callback set */ |
| 167 | disable_local_APIC(); |
| 168 | } |
| 169 | #else |
| 170 | static void nmi_shootdown_cpus(void) |
| 171 | { |
| 172 | /* There are no cpus to shootdown */ |
| 173 | } |
| 174 | #endif |
Eric W. Biederman | 5234f5e | 2005-06-25 14:58:02 -0700 | [diff] [blame] | 175 | |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 176 | void machine_crash_shutdown(struct pt_regs *regs) |
Eric W. Biederman | 5234f5e | 2005-06-25 14:58:02 -0700 | [diff] [blame] | 177 | { |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 178 | /* |
| 179 | * This function is only called after the system |
Lee Revell | f18190b | 2006-06-26 18:30:00 +0200 | [diff] [blame] | 180 | * has panicked or is otherwise in a critical state. |
Eric W. Biederman | 5234f5e | 2005-06-25 14:58:02 -0700 | [diff] [blame] | 181 | * The minimum amount of code to allow a kexec'd kernel |
| 182 | * to run successfully needs to happen here. |
| 183 | * |
| 184 | * In practice this means shooting down the other cpus in |
| 185 | * an SMP system. |
| 186 | */ |
akpm@osdl.org | cffe632 | 2006-01-09 20:51:48 -0800 | [diff] [blame] | 187 | /* The kernel is broken so disable interrupts */ |
| 188 | local_irq_disable(); |
| 189 | |
| 190 | /* Make a note of crashing cpu. Will be used in NMI callback.*/ |
| 191 | crashing_cpu = smp_processor_id(); |
| 192 | nmi_shootdown_cpus(); |
| 193 | |
| 194 | if(cpu_has_apic) |
| 195 | disable_local_APIC(); |
| 196 | |
| 197 | #if defined(CONFIG_X86_IO_APIC) |
| 198 | disable_IO_APIC(); |
| 199 | #endif |
| 200 | |
Vivek Goyal | ec9ce0d | 2006-01-09 20:51:49 -0800 | [diff] [blame] | 201 | crash_save_self(regs); |
Eric W. Biederman | 5234f5e | 2005-06-25 14:58:02 -0700 | [diff] [blame] | 202 | } |