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> |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 25 | #include <mach_ipi.h> |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 26 | |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 27 | |
| 28 | note_buf_t crash_notes[NR_CPUS]; |
Vivek Goyal | a3ea8ac | 2005-06-25 14:58:14 -0700 | [diff] [blame] | 29 | /* This keeps a track of which one is crashing cpu. */ |
| 30 | static int crashing_cpu; |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 31 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 32 | static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data, |
| 33 | size_t data_len) |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 34 | { |
| 35 | struct elf_note note; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 36 | |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 37 | note.n_namesz = strlen(name) + 1; |
| 38 | note.n_descsz = data_len; |
| 39 | note.n_type = type; |
| 40 | memcpy(buf, ¬e, sizeof(note)); |
| 41 | buf += (sizeof(note) +3)/4; |
| 42 | memcpy(buf, name, note.n_namesz); |
| 43 | buf += (note.n_namesz + 3)/4; |
| 44 | memcpy(buf, data, note.n_descsz); |
| 45 | buf += (note.n_descsz + 3)/4; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 46 | |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 47 | return buf; |
| 48 | } |
| 49 | |
| 50 | static void final_note(u32 *buf) |
| 51 | { |
| 52 | struct elf_note note; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 53 | |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 54 | note.n_namesz = 0; |
| 55 | note.n_descsz = 0; |
| 56 | note.n_type = 0; |
| 57 | memcpy(buf, ¬e, sizeof(note)); |
| 58 | } |
| 59 | |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 60 | static void crash_save_this_cpu(struct pt_regs *regs, int cpu) |
| 61 | { |
| 62 | struct elf_prstatus prstatus; |
| 63 | u32 *buf; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 64 | |
| 65 | if ((cpu < 0) || (cpu >= NR_CPUS)) |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 66 | return; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 67 | |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 68 | /* Using ELF notes here is opportunistic. |
| 69 | * I need a well defined structure format |
| 70 | * for the data I pass, and I need tags |
| 71 | * on the data to indicate what information I have |
| 72 | * squirrelled away. ELF notes happen to provide |
| 73 | * all of that that no need to invent something new. |
| 74 | */ |
| 75 | buf = &crash_notes[cpu][0]; |
| 76 | memset(&prstatus, 0, sizeof(prstatus)); |
| 77 | prstatus.pr_pid = current->pid; |
| 78 | elf_core_copy_regs(&prstatus.pr_reg, regs); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 79 | buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus, |
| 80 | sizeof(prstatus)); |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 81 | final_note(buf); |
| 82 | } |
| 83 | |
| 84 | static void crash_get_current_regs(struct pt_regs *regs) |
| 85 | { |
| 86 | __asm__ __volatile__("movl %%ebx,%0" : "=m"(regs->ebx)); |
| 87 | __asm__ __volatile__("movl %%ecx,%0" : "=m"(regs->ecx)); |
| 88 | __asm__ __volatile__("movl %%edx,%0" : "=m"(regs->edx)); |
| 89 | __asm__ __volatile__("movl %%esi,%0" : "=m"(regs->esi)); |
| 90 | __asm__ __volatile__("movl %%edi,%0" : "=m"(regs->edi)); |
| 91 | __asm__ __volatile__("movl %%ebp,%0" : "=m"(regs->ebp)); |
| 92 | __asm__ __volatile__("movl %%eax,%0" : "=m"(regs->eax)); |
| 93 | __asm__ __volatile__("movl %%esp,%0" : "=m"(regs->esp)); |
| 94 | __asm__ __volatile__("movw %%ss, %%ax;" :"=a"(regs->xss)); |
| 95 | __asm__ __volatile__("movw %%cs, %%ax;" :"=a"(regs->xcs)); |
| 96 | __asm__ __volatile__("movw %%ds, %%ax;" :"=a"(regs->xds)); |
| 97 | __asm__ __volatile__("movw %%es, %%ax;" :"=a"(regs->xes)); |
| 98 | __asm__ __volatile__("pushfl; popl %0" :"=m"(regs->eflags)); |
| 99 | |
| 100 | regs->eip = (unsigned long)current_text_addr(); |
| 101 | } |
| 102 | |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 103 | /* CPU does not save ss and esp on stack if execution is already |
| 104 | * running in kernel mode at the time of NMI occurrence. This code |
| 105 | * fixes it. |
| 106 | */ |
| 107 | static void crash_setup_regs(struct pt_regs *newregs, struct pt_regs *oldregs) |
| 108 | { |
| 109 | memcpy(newregs, oldregs, sizeof(*newregs)); |
| 110 | newregs->esp = (unsigned long)&(oldregs->esp); |
akpm@osdl.org | 8240941 | 2006-01-09 20:51:40 -0800 | [diff] [blame^] | 111 | __asm__ __volatile__( |
| 112 | "xorl %%eax, %%eax\n\t" |
| 113 | "movw %%ss, %%ax\n\t" |
| 114 | :"=a"(newregs->xss)); |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* We may have saved_regs from where the error came from |
| 118 | * or it is NULL if via a direct panic(). |
| 119 | */ |
| 120 | static void crash_save_self(struct pt_regs *saved_regs) |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 121 | { |
| 122 | struct pt_regs regs; |
| 123 | int cpu; |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 124 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 125 | cpu = smp_processor_id(); |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 126 | if (saved_regs) |
| 127 | crash_setup_regs(®s, saved_regs); |
| 128 | else |
| 129 | crash_get_current_regs(®s); |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 130 | crash_save_this_cpu(®s, cpu); |
| 131 | } |
| 132 | |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 133 | #ifdef CONFIG_SMP |
| 134 | static atomic_t waiting_for_crash_ipi; |
| 135 | |
| 136 | static int crash_nmi_callback(struct pt_regs *regs, int cpu) |
| 137 | { |
Vivek Goyal | 4d55476 | 2005-06-25 14:58:13 -0700 | [diff] [blame] | 138 | struct pt_regs fixed_regs; |
Vivek Goyal | a3ea8ac | 2005-06-25 14:58:14 -0700 | [diff] [blame] | 139 | |
| 140 | /* Don't do anything if this handler is invoked on crashing cpu. |
| 141 | * Otherwise, system will completely hang. Crashing cpu can get |
| 142 | * an NMI if system was initially booted with nmi_watchdog parameter. |
| 143 | */ |
| 144 | if (cpu == crashing_cpu) |
| 145 | return 1; |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 146 | local_irq_disable(); |
Vivek Goyal | 4d55476 | 2005-06-25 14:58:13 -0700 | [diff] [blame] | 147 | |
Vivek Goyal | 4d55476 | 2005-06-25 14:58:13 -0700 | [diff] [blame] | 148 | if (!user_mode(regs)) { |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 149 | crash_setup_regs(&fixed_regs, regs); |
Vivek Goyal | 4d55476 | 2005-06-25 14:58:13 -0700 | [diff] [blame] | 150 | regs = &fixed_regs; |
| 151 | } |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 152 | crash_save_this_cpu(regs, cpu); |
Vivek Goyal | 19842d6 | 2005-11-15 00:09:04 -0800 | [diff] [blame] | 153 | disable_local_APIC(); |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 154 | atomic_dec(&waiting_for_crash_ipi); |
| 155 | /* Assume hlt works */ |
Zachary Amsden | f2ab446 | 2005-09-03 15:56:42 -0700 | [diff] [blame] | 156 | halt(); |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 157 | for(;;); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 158 | |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * By using the NMI code instead of a vector we just sneak thru the |
| 164 | * word generator coming out with just what we want. AND it does |
| 165 | * not matter if clustered_apic_mode is set or not. |
| 166 | */ |
| 167 | static void smp_send_nmi_allbutself(void) |
| 168 | { |
| 169 | send_IPI_allbutself(APIC_DM_NMI); |
| 170 | } |
| 171 | |
| 172 | static void nmi_shootdown_cpus(void) |
| 173 | { |
| 174 | unsigned long msecs; |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 175 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 176 | atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1); |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 177 | /* Would it be better to replace the trap vector here? */ |
| 178 | set_nmi_callback(crash_nmi_callback); |
| 179 | /* Ensure the new callback function is set before sending |
| 180 | * out the NMI |
| 181 | */ |
| 182 | wmb(); |
| 183 | |
| 184 | smp_send_nmi_allbutself(); |
| 185 | |
| 186 | msecs = 1000; /* Wait at most a second for the other cpus to stop */ |
| 187 | while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) { |
| 188 | mdelay(1); |
| 189 | msecs--; |
| 190 | } |
| 191 | |
| 192 | /* Leave the nmi callback set */ |
Vivek Goyal | 19842d6 | 2005-11-15 00:09:04 -0800 | [diff] [blame] | 193 | disable_local_APIC(); |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 194 | } |
| 195 | #else |
| 196 | static void nmi_shootdown_cpus(void) |
| 197 | { |
| 198 | /* There are no cpus to shootdown */ |
| 199 | } |
| 200 | #endif |
| 201 | |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 202 | void machine_crash_shutdown(struct pt_regs *regs) |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 203 | { |
| 204 | /* This function is only called after the system |
| 205 | * has paniced or is otherwise in a critical state. |
| 206 | * The minimum amount of code to allow a kexec'd kernel |
| 207 | * to run successfully needs to happen here. |
| 208 | * |
| 209 | * In practice this means shooting down the other cpus in |
| 210 | * an SMP system. |
| 211 | */ |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 212 | /* The kernel is broken so disable interrupts */ |
| 213 | local_irq_disable(); |
Vivek Goyal | a3ea8ac | 2005-06-25 14:58:14 -0700 | [diff] [blame] | 214 | |
| 215 | /* Make a note of crashing cpu. Will be used in NMI callback.*/ |
| 216 | crashing_cpu = smp_processor_id(); |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 217 | nmi_shootdown_cpus(); |
Vivek Goyal | 19842d6 | 2005-11-15 00:09:04 -0800 | [diff] [blame] | 218 | lapic_shutdown(); |
| 219 | #if defined(CONFIG_X86_IO_APIC) |
| 220 | disable_IO_APIC(); |
| 221 | #endif |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 222 | crash_save_self(regs); |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 223 | } |