2 * Architecture specific (i386) functions for kexec based crash dumps.
4 * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
6 * Copyright (C) IBM Corporation, 2004. All rights reserved.
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>
22 #include <asm/processor.h>
23 #include <asm/hardirq.h>
25 #include <asm/hw_irq.h>
28 #define MAX_NOTE_BYTES 1024
29 typedef u32 note_buf_t[MAX_NOTE_BYTES/4];
31 note_buf_t crash_notes[NR_CPUS];
33 static u32 *append_elf_note(u32 *buf,
34 char *name, unsigned type, void *data, size_t data_len)
37 note.n_namesz = strlen(name) + 1;
38 note.n_descsz = data_len;
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;
49 static void final_note(u32 *buf)
55 memcpy(buf, ¬e, sizeof(note));
59 static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
61 struct elf_prstatus prstatus;
63 if ((cpu < 0) || (cpu >= NR_CPUS)) {
66 /* Using ELF notes here is opportunistic.
67 * I need a well defined structure format
68 * for the data I pass, and I need tags
69 * on the data to indicate what information I have
70 * squirrelled away. ELF notes happen to provide
71 * all of that that no need to invent something new.
73 buf = &crash_notes[cpu][0];
74 memset(&prstatus, 0, sizeof(prstatus));
75 prstatus.pr_pid = current->pid;
76 elf_core_copy_regs(&prstatus.pr_reg, regs);
77 buf = append_elf_note(buf, "CORE", NT_PRSTATUS,
78 &prstatus, sizeof(prstatus));
83 static void crash_get_current_regs(struct pt_regs *regs)
85 __asm__ __volatile__("movl %%ebx,%0" : "=m"(regs->ebx));
86 __asm__ __volatile__("movl %%ecx,%0" : "=m"(regs->ecx));
87 __asm__ __volatile__("movl %%edx,%0" : "=m"(regs->edx));
88 __asm__ __volatile__("movl %%esi,%0" : "=m"(regs->esi));
89 __asm__ __volatile__("movl %%edi,%0" : "=m"(regs->edi));
90 __asm__ __volatile__("movl %%ebp,%0" : "=m"(regs->ebp));
91 __asm__ __volatile__("movl %%eax,%0" : "=m"(regs->eax));
92 __asm__ __volatile__("movl %%esp,%0" : "=m"(regs->esp));
93 __asm__ __volatile__("movw %%ss, %%ax;" :"=a"(regs->xss));
94 __asm__ __volatile__("movw %%cs, %%ax;" :"=a"(regs->xcs));
95 __asm__ __volatile__("movw %%ds, %%ax;" :"=a"(regs->xds));
96 __asm__ __volatile__("movw %%es, %%ax;" :"=a"(regs->xes));
97 __asm__ __volatile__("pushfl; popl %0" :"=m"(regs->eflags));
99 regs->eip = (unsigned long)current_text_addr();
102 static void crash_save_self(void)
106 cpu = smp_processor_id();
107 crash_get_current_regs(®s);
108 crash_save_this_cpu(®s, cpu);
112 static atomic_t waiting_for_crash_ipi;
114 static int crash_nmi_callback(struct pt_regs *regs, int cpu)
117 crash_save_this_cpu(regs, cpu);
118 atomic_dec(&waiting_for_crash_ipi);
119 /* Assume hlt works */
126 * By using the NMI code instead of a vector we just sneak thru the
127 * word generator coming out with just what we want. AND it does
128 * not matter if clustered_apic_mode is set or not.
130 static void smp_send_nmi_allbutself(void)
132 send_IPI_allbutself(APIC_DM_NMI);
135 static void nmi_shootdown_cpus(void)
138 atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
140 /* Would it be better to replace the trap vector here? */
141 set_nmi_callback(crash_nmi_callback);
142 /* Ensure the new callback function is set before sending
147 smp_send_nmi_allbutself();
149 msecs = 1000; /* Wait at most a second for the other cpus to stop */
150 while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
155 /* Leave the nmi callback set */
158 static void nmi_shootdown_cpus(void)
160 /* There are no cpus to shootdown */
164 void machine_crash_shutdown(void)
166 /* This function is only called after the system
167 * has paniced or is otherwise in a critical state.
168 * The minimum amount of code to allow a kexec'd kernel
169 * to run successfully needs to happen here.
171 * In practice this means shooting down the other cpus in
174 /* The kernel is broken so disable interrupts */
176 nmi_shootdown_cpus();