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