blob: 2dfc049dafa3dc8cebed69d872a46284388230e7 [file] [log] [blame]
Eric W. Biederman5033cba2005-06-25 14:57:56 -07001/*
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. Biederman5033cba2005-06-25 14:57:56 -070014#include <linux/reboot.h>
15#include <linux/kexec.h>
Eric W. Biederman5033cba2005-06-25 14:57:56 -070016#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 Goyal19842d62005-11-15 00:09:04 -080024#include <asm/apic.h>
Don Zickus2fbe7b22006-09-26 10:52:27 +020025#include <asm/kdebug.h>
Fernando Vazquezce53af92006-09-30 23:29:09 -070026#include <asm/smp.h>
Don Zickus2fbe7b22006-09-26 10:52:27 +020027
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070028#include <mach_ipi.h>
Eric W. Biederman5033cba2005-06-25 14:57:56 -070029
Eric W. Biederman5033cba2005-06-25 14:57:56 -070030
Vivek Goyala3ea8ac2005-06-25 14:58:14 -070031/* This keeps a track of which one is crashing cpu. */
32static int crashing_cpu;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070033
Maneesh Soni72414d32005-06-25 14:58:28 -070034static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
35 size_t data_len)
Eric W. Biederman2c818b42005-06-25 14:57:59 -070036{
37 struct elf_note note;
Maneesh Soni72414d32005-06-25 14:58:28 -070038
Eric W. Biederman2c818b42005-06-25 14:57:59 -070039 note.n_namesz = strlen(name) + 1;
40 note.n_descsz = data_len;
41 note.n_type = type;
42 memcpy(buf, &note, 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;
Maneesh Soni72414d32005-06-25 14:58:28 -070048
Eric W. Biederman2c818b42005-06-25 14:57:59 -070049 return buf;
50}
51
52static void final_note(u32 *buf)
53{
54 struct elf_note note;
Maneesh Soni72414d32005-06-25 14:58:28 -070055
Eric W. Biederman2c818b42005-06-25 14:57:59 -070056 note.n_namesz = 0;
57 note.n_descsz = 0;
58 note.n_type = 0;
59 memcpy(buf, &note, sizeof(note));
60}
61
Eric W. Biederman2c818b42005-06-25 14:57:59 -070062static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
63{
64 struct elf_prstatus prstatus;
65 u32 *buf;
Maneesh Soni72414d32005-06-25 14:58:28 -070066
67 if ((cpu < 0) || (cpu >= NR_CPUS))
Eric W. Biederman2c818b42005-06-25 14:57:59 -070068 return;
Maneesh Soni72414d32005-06-25 14:58:28 -070069
Eric W. Biederman2c818b42005-06-25 14:57:59 -070070 /* 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
Horms36a891b2006-04-01 01:39:17 +020075 * all of that, so there is no need to invent something new.
Eric W. Biederman2c818b42005-06-25 14:57:59 -070076 */
Vivek Goyalcc571652006-01-09 20:51:41 -080077 buf = (u32*)per_cpu_ptr(crash_notes, cpu);
78 if (!buf)
79 return;
Eric W. Biederman2c818b42005-06-25 14:57:59 -070080 memset(&prstatus, 0, sizeof(prstatus));
81 prstatus.pr_pid = current->pid;
82 elf_core_copy_regs(&prstatus.pr_reg, regs);
Maneesh Soni72414d32005-06-25 14:58:28 -070083 buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus,
84 sizeof(prstatus));
Eric W. Biederman2c818b42005-06-25 14:57:59 -070085 final_note(buf);
86}
87
Vivek Goyale996e582006-01-09 20:51:44 -080088static void crash_save_self(struct pt_regs *regs)
Eric W. Biederman2c818b42005-06-25 14:57:59 -070089{
Eric W. Biederman2c818b42005-06-25 14:57:59 -070090 int cpu;
Alexander Nyberg6e274d12005-06-25 14:58:26 -070091
Fernando Vazquezce53af92006-09-30 23:29:09 -070092 cpu = safe_smp_processor_id();
Vivek Goyale996e582006-01-09 20:51:44 -080093 crash_save_this_cpu(regs, cpu);
Eric W. Biederman2c818b42005-06-25 14:57:59 -070094}
95
Eric W. Biedermane78a8872006-07-14 00:24:00 -070096#if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070097static atomic_t waiting_for_crash_ipi;
98
Don Zickus2fbe7b22006-09-26 10:52:27 +020099static int crash_nmi_callback(struct notifier_block *self,
100 unsigned long val, void *data)
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700101{
Don Zickus2fbe7b22006-09-26 10:52:27 +0200102 struct pt_regs *regs;
Vivek Goyal4d554762005-06-25 14:58:13 -0700103 struct pt_regs fixed_regs;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200104 int cpu;
105
Vivek Goyal260d6792006-09-26 10:52:27 +0200106 if (val != DIE_NMI_IPI)
Don Zickus2fbe7b22006-09-26 10:52:27 +0200107 return NOTIFY_OK;
108
109 regs = ((struct die_args *)data)->regs;
110 cpu = raw_smp_processor_id();
Vivek Goyala3ea8ac2005-06-25 14:58:14 -0700111
112 /* Don't do anything if this handler is invoked on crashing cpu.
113 * Otherwise, system will completely hang. Crashing cpu can get
114 * an NMI if system was initially booted with nmi_watchdog parameter.
115 */
116 if (cpu == crashing_cpu)
Vivek Goyal260d6792006-09-26 10:52:27 +0200117 return NOTIFY_STOP;
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700118 local_irq_disable();
Vivek Goyal4d554762005-06-25 14:58:13 -0700119
Jan Beulichdb753bd2006-03-23 02:59:46 -0800120 if (!user_mode_vm(regs)) {
Vivek Goyale996e582006-01-09 20:51:44 -0800121 crash_fixup_ss_esp(&fixed_regs, regs);
Vivek Goyal4d554762005-06-25 14:58:13 -0700122 regs = &fixed_regs;
123 }
Eric W. Biederman2c818b42005-06-25 14:57:59 -0700124 crash_save_this_cpu(regs, cpu);
Vivek Goyal19842d62005-11-15 00:09:04 -0800125 disable_local_APIC();
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700126 atomic_dec(&waiting_for_crash_ipi);
127 /* Assume hlt works */
Zachary Amsdenf2ab4462005-09-03 15:56:42 -0700128 halt();
Chuck Ebbertcaad3c22006-06-25 05:46:53 -0700129 for (;;)
130 cpu_relax();
Maneesh Soni72414d32005-06-25 14:58:28 -0700131
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700132 return 1;
133}
134
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700135static void smp_send_nmi_allbutself(void)
136{
Keith Owens45486f82006-06-26 13:59:41 +0200137 send_IPI_allbutself(NMI_VECTOR);
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700138}
139
Don Zickus2fbe7b22006-09-26 10:52:27 +0200140static struct notifier_block crash_nmi_nb = {
141 .notifier_call = crash_nmi_callback,
142};
143
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700144static void nmi_shootdown_cpus(void)
145{
146 unsigned long msecs;
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700147
Maneesh Soni72414d32005-06-25 14:58:28 -0700148 atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700149 /* Would it be better to replace the trap vector here? */
Don Zickus2fbe7b22006-09-26 10:52:27 +0200150 if (register_die_notifier(&crash_nmi_nb))
151 return; /* return what? */
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700152 /* Ensure the new callback function is set before sending
153 * out the NMI
154 */
155 wmb();
156
157 smp_send_nmi_allbutself();
158
159 msecs = 1000; /* Wait at most a second for the other cpus to stop */
160 while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
161 mdelay(1);
162 msecs--;
163 }
164
165 /* Leave the nmi callback set */
Vivek Goyal19842d62005-11-15 00:09:04 -0800166 disable_local_APIC();
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700167}
168#else
169static void nmi_shootdown_cpus(void)
170{
171 /* There are no cpus to shootdown */
172}
173#endif
174
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700175void machine_crash_shutdown(struct pt_regs *regs)
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700176{
177 /* This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200178 * has panicked or is otherwise in a critical state.
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700179 * The minimum amount of code to allow a kexec'd kernel
180 * to run successfully needs to happen here.
181 *
182 * In practice this means shooting down the other cpus in
183 * an SMP system.
184 */
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700185 /* The kernel is broken so disable interrupts */
186 local_irq_disable();
Vivek Goyala3ea8ac2005-06-25 14:58:14 -0700187
188 /* Make a note of crashing cpu. Will be used in NMI callback.*/
Fernando Vazquezce53af92006-09-30 23:29:09 -0700189 crashing_cpu = safe_smp_processor_id();
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700190 nmi_shootdown_cpus();
Vivek Goyal19842d62005-11-15 00:09:04 -0800191 lapic_shutdown();
192#if defined(CONFIG_X86_IO_APIC)
193 disable_IO_APIC();
194#endif
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700195 crash_save_self(regs);
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700196}