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