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>
30 note_buf_t crash_notes[NR_CPUS];
31 /* This keeps a track of which one is crashing cpu. */
32 static int crashing_cpu;
34 static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
39 note.n_namesz = strlen(name) + 1;
40 note.n_descsz = data_len;
42 memcpy(buf, ¬e, sizeof(note));
43 buf += (sizeof(note) +3)/4;
44 memcpy(buf, name, note.n_namesz);
45 buf += (note.n_namesz + 3)/4;
46 memcpy(buf, data, note.n_descsz);
47 buf += (note.n_descsz + 3)/4;
52 static void final_note(u32 *buf)
59 memcpy(buf, ¬e, sizeof(note));
62 static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
64 struct elf_prstatus prstatus;
67 if ((cpu < 0) || (cpu >= NR_CPUS))
70 /* Using ELF notes here is opportunistic.
71 * I need a well defined structure format
72 * for the data I pass, and I need tags
73 * on the data to indicate what information I have
74 * squirrelled away. ELF notes happen to provide
75 * all of that that no need to invent something new.
77 buf = &crash_notes[cpu][0];
78 memset(&prstatus, 0, sizeof(prstatus));
79 prstatus.pr_pid = current->pid;
80 elf_core_copy_regs(&prstatus.pr_reg, regs);
81 buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus,
86 static void crash_get_current_regs(struct pt_regs *regs)
88 __asm__ __volatile__("movl %%ebx,%0" : "=m"(regs->ebx));
89 __asm__ __volatile__("movl %%ecx,%0" : "=m"(regs->ecx));
90 __asm__ __volatile__("movl %%edx,%0" : "=m"(regs->edx));
91 __asm__ __volatile__("movl %%esi,%0" : "=m"(regs->esi));
92 __asm__ __volatile__("movl %%edi,%0" : "=m"(regs->edi));
93 __asm__ __volatile__("movl %%ebp,%0" : "=m"(regs->ebp));
94 __asm__ __volatile__("movl %%eax,%0" : "=m"(regs->eax));
95 __asm__ __volatile__("movl %%esp,%0" : "=m"(regs->esp));
96 __asm__ __volatile__("movw %%ss, %%ax;" :"=a"(regs->xss));
97 __asm__ __volatile__("movw %%cs, %%ax;" :"=a"(regs->xcs));
98 __asm__ __volatile__("movw %%ds, %%ax;" :"=a"(regs->xds));
99 __asm__ __volatile__("movw %%es, %%ax;" :"=a"(regs->xes));
100 __asm__ __volatile__("pushfl; popl %0" :"=m"(regs->eflags));
102 regs->eip = (unsigned long)current_text_addr();
105 /* CPU does not save ss and esp on stack if execution is already
106 * running in kernel mode at the time of NMI occurrence. This code
109 static void crash_setup_regs(struct pt_regs *newregs, struct pt_regs *oldregs)
111 memcpy(newregs, oldregs, sizeof(*newregs));
112 newregs->esp = (unsigned long)&(oldregs->esp);
113 __asm__ __volatile__("xorl %eax, %eax;");
114 __asm__ __volatile__ ("movw %%ss, %%ax;" :"=a"(newregs->xss));
117 /* We may have saved_regs from where the error came from
118 * or it is NULL if via a direct panic().
120 static void crash_save_self(struct pt_regs *saved_regs)
125 cpu = smp_processor_id();
127 crash_setup_regs(®s, saved_regs);
129 crash_get_current_regs(®s);
130 crash_save_this_cpu(®s, cpu);
134 static atomic_t waiting_for_crash_ipi;
136 static int crash_nmi_callback(struct pt_regs *regs, int cpu)
138 struct pt_regs fixed_regs;
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.
144 if (cpu == crashing_cpu)
148 if (!user_mode(regs)) {
149 crash_setup_regs(&fixed_regs, regs);
152 crash_save_this_cpu(regs, cpu);
153 disable_local_APIC();
154 atomic_dec(&waiting_for_crash_ipi);
155 /* Assume hlt works */
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.
167 static void smp_send_nmi_allbutself(void)
169 send_IPI_allbutself(APIC_DM_NMI);
172 static void nmi_shootdown_cpus(void)
176 atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
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
184 smp_send_nmi_allbutself();
186 msecs = 1000; /* Wait at most a second for the other cpus to stop */
187 while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
192 /* Leave the nmi callback set */
193 disable_local_APIC();
196 static void nmi_shootdown_cpus(void)
198 /* There are no cpus to shootdown */
202 void machine_crash_shutdown(struct pt_regs *regs)
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.
209 * In practice this means shooting down the other cpus in
212 /* The kernel is broken so disable interrupts */
215 /* Make a note of crashing cpu. Will be used in NMI callback.*/
216 crashing_cpu = smp_processor_id();
217 nmi_shootdown_cpus();
219 #if defined(CONFIG_X86_IO_APIC)
222 crash_save_self(regs);