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> |
| 14 | #include <linux/irq.h> |
| 15 | #include <linux/reboot.h> |
| 16 | #include <linux/kexec.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/elf.h> |
| 20 | #include <linux/elfcore.h> |
| 21 | |
| 22 | #include <asm/processor.h> |
| 23 | #include <asm/hardirq.h> |
| 24 | #include <asm/nmi.h> |
| 25 | #include <asm/hw_irq.h> |
Eric W. Biederman | 63d3029 | 2005-06-25 14:58:00 -0700 | [diff] [blame^] | 26 | #include <asm/apic.h> |
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 | |
| 29 | #define MAX_NOTE_BYTES 1024 |
| 30 | typedef u32 note_buf_t[MAX_NOTE_BYTES/4]; |
| 31 | |
| 32 | note_buf_t crash_notes[NR_CPUS]; |
| 33 | |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 34 | static u32 *append_elf_note(u32 *buf, |
| 35 | char *name, unsigned type, void *data, size_t data_len) |
| 36 | { |
| 37 | struct elf_note note; |
| 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; |
| 47 | return buf; |
| 48 | } |
| 49 | |
| 50 | static void final_note(u32 *buf) |
| 51 | { |
| 52 | struct elf_note note; |
| 53 | note.n_namesz = 0; |
| 54 | note.n_descsz = 0; |
| 55 | note.n_type = 0; |
| 56 | memcpy(buf, ¬e, sizeof(note)); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static void crash_save_this_cpu(struct pt_regs *regs, int cpu) |
| 61 | { |
| 62 | struct elf_prstatus prstatus; |
| 63 | u32 *buf; |
| 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 | buf = &crash_notes[cpu][0]; |
| 75 | memset(&prstatus, 0, sizeof(prstatus)); |
| 76 | prstatus.pr_pid = current->pid; |
| 77 | elf_core_copy_regs(&prstatus.pr_reg, regs); |
| 78 | buf = append_elf_note(buf, "CORE", NT_PRSTATUS, |
| 79 | &prstatus, sizeof(prstatus)); |
| 80 | |
| 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 | |
| 103 | static void crash_save_self(void) |
| 104 | { |
| 105 | struct pt_regs regs; |
| 106 | int cpu; |
| 107 | cpu = smp_processor_id(); |
| 108 | crash_get_current_regs(®s); |
| 109 | crash_save_this_cpu(®s, cpu); |
| 110 | } |
| 111 | |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 112 | #ifdef CONFIG_SMP |
| 113 | static atomic_t waiting_for_crash_ipi; |
| 114 | |
| 115 | static int crash_nmi_callback(struct pt_regs *regs, int cpu) |
| 116 | { |
| 117 | local_irq_disable(); |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 118 | crash_save_this_cpu(regs, cpu); |
Eric W. Biederman | 63d3029 | 2005-06-25 14:58:00 -0700 | [diff] [blame^] | 119 | disable_local_APIC(); |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 120 | atomic_dec(&waiting_for_crash_ipi); |
| 121 | /* Assume hlt works */ |
| 122 | __asm__("hlt"); |
| 123 | for(;;); |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * By using the NMI code instead of a vector we just sneak thru the |
| 129 | * word generator coming out with just what we want. AND it does |
| 130 | * not matter if clustered_apic_mode is set or not. |
| 131 | */ |
| 132 | static void smp_send_nmi_allbutself(void) |
| 133 | { |
| 134 | send_IPI_allbutself(APIC_DM_NMI); |
| 135 | } |
| 136 | |
| 137 | static void nmi_shootdown_cpus(void) |
| 138 | { |
| 139 | unsigned long msecs; |
| 140 | atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1); |
| 141 | |
| 142 | /* Would it be better to replace the trap vector here? */ |
| 143 | set_nmi_callback(crash_nmi_callback); |
| 144 | /* Ensure the new callback function is set before sending |
| 145 | * out the NMI |
| 146 | */ |
| 147 | wmb(); |
| 148 | |
| 149 | smp_send_nmi_allbutself(); |
| 150 | |
| 151 | msecs = 1000; /* Wait at most a second for the other cpus to stop */ |
| 152 | while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) { |
| 153 | mdelay(1); |
| 154 | msecs--; |
| 155 | } |
| 156 | |
| 157 | /* Leave the nmi callback set */ |
Eric W. Biederman | 63d3029 | 2005-06-25 14:58:00 -0700 | [diff] [blame^] | 158 | disable_local_APIC(); |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 159 | } |
| 160 | #else |
| 161 | static void nmi_shootdown_cpus(void) |
| 162 | { |
| 163 | /* There are no cpus to shootdown */ |
| 164 | } |
| 165 | #endif |
| 166 | |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 167 | void machine_crash_shutdown(void) |
| 168 | { |
| 169 | /* This function is only called after the system |
| 170 | * has paniced or is otherwise in a critical state. |
| 171 | * The minimum amount of code to allow a kexec'd kernel |
| 172 | * to run successfully needs to happen here. |
| 173 | * |
| 174 | * In practice this means shooting down the other cpus in |
| 175 | * an SMP system. |
| 176 | */ |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 177 | /* The kernel is broken so disable interrupts */ |
| 178 | local_irq_disable(); |
| 179 | nmi_shootdown_cpus(); |
Eric W. Biederman | 63d3029 | 2005-06-25 14:58:00 -0700 | [diff] [blame^] | 180 | lapic_shutdown(); |
| 181 | #if defined(CONFIG_X86_IO_APIC) |
| 182 | disable_IO_APIC(); |
| 183 | #endif |
Eric W. Biederman | 2c818b4 | 2005-06-25 14:57:59 -0700 | [diff] [blame] | 184 | crash_save_self(); |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 185 | } |