blob: 67d297dc1003cd0d1fec64ade9fba9fd4218a33f [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>
26
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070027#include <mach_ipi.h>
Eric W. Biederman5033cba2005-06-25 14:57:56 -070028
Eric W. Biederman5033cba2005-06-25 14:57:56 -070029
Vivek Goyala3ea8ac2005-06-25 14:58:14 -070030/* This keeps a track of which one is crashing cpu. */
31static int crashing_cpu;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070032
Maneesh Soni72414d32005-06-25 14:58:28 -070033static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
34 size_t data_len)
Eric W. Biederman2c818b42005-06-25 14:57:59 -070035{
36 struct elf_note note;
Maneesh Soni72414d32005-06-25 14:58:28 -070037
Eric W. Biederman2c818b42005-06-25 14:57:59 -070038 note.n_namesz = strlen(name) + 1;
39 note.n_descsz = data_len;
40 note.n_type = type;
41 memcpy(buf, &note, 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;
Maneesh Soni72414d32005-06-25 14:58:28 -070047
Eric W. Biederman2c818b42005-06-25 14:57:59 -070048 return buf;
49}
50
51static void final_note(u32 *buf)
52{
53 struct elf_note note;
Maneesh Soni72414d32005-06-25 14:58:28 -070054
Eric W. Biederman2c818b42005-06-25 14:57:59 -070055 note.n_namesz = 0;
56 note.n_descsz = 0;
57 note.n_type = 0;
58 memcpy(buf, &note, sizeof(note));
59}
60
Eric W. Biederman2c818b42005-06-25 14:57:59 -070061static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
62{
63 struct elf_prstatus prstatus;
64 u32 *buf;
Maneesh Soni72414d32005-06-25 14:58:28 -070065
66 if ((cpu < 0) || (cpu >= NR_CPUS))
Eric W. Biederman2c818b42005-06-25 14:57:59 -070067 return;
Maneesh Soni72414d32005-06-25 14:58:28 -070068
Eric W. Biederman2c818b42005-06-25 14:57:59 -070069 /* Using ELF notes here is opportunistic.
70 * I need a well defined structure format
71 * for the data I pass, and I need tags
72 * on the data to indicate what information I have
73 * squirrelled away. ELF notes happen to provide
Horms36a891b2006-04-01 01:39:17 +020074 * all of that, so there is no need to invent something new.
Eric W. Biederman2c818b42005-06-25 14:57:59 -070075 */
Vivek Goyalcc571652006-01-09 20:51:41 -080076 buf = (u32*)per_cpu_ptr(crash_notes, cpu);
77 if (!buf)
78 return;
Eric W. Biederman2c818b42005-06-25 14:57:59 -070079 memset(&prstatus, 0, sizeof(prstatus));
80 prstatus.pr_pid = current->pid;
81 elf_core_copy_regs(&prstatus.pr_reg, regs);
Maneesh Soni72414d32005-06-25 14:58:28 -070082 buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus,
83 sizeof(prstatus));
Eric W. Biederman2c818b42005-06-25 14:57:59 -070084 final_note(buf);
85}
86
Vivek Goyale996e582006-01-09 20:51:44 -080087static void crash_save_self(struct pt_regs *regs)
Eric W. Biederman2c818b42005-06-25 14:57:59 -070088{
Eric W. Biederman2c818b42005-06-25 14:57:59 -070089 int cpu;
Alexander Nyberg6e274d12005-06-25 14:58:26 -070090
Maneesh Soni72414d32005-06-25 14:58:28 -070091 cpu = smp_processor_id();
Vivek Goyale996e582006-01-09 20:51:44 -080092 crash_save_this_cpu(regs, cpu);
Eric W. Biederman2c818b42005-06-25 14:57:59 -070093}
94
Eric W. Biedermane78a8872006-07-14 00:24:00 -070095#if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070096static atomic_t waiting_for_crash_ipi;
97
Don Zickus2fbe7b22006-09-26 10:52:27 +020098static int crash_nmi_callback(struct notifier_block *self,
99 unsigned long val, void *data)
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700100{
Don Zickus2fbe7b22006-09-26 10:52:27 +0200101 struct pt_regs *regs;
Vivek Goyal4d554762005-06-25 14:58:13 -0700102 struct pt_regs fixed_regs;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200103 int cpu;
104
Vivek Goyal260d6792006-09-26 10:52:27 +0200105 if (val != DIE_NMI_IPI)
Don Zickus2fbe7b22006-09-26 10:52:27 +0200106 return NOTIFY_OK;
107
108 regs = ((struct die_args *)data)->regs;
109 cpu = raw_smp_processor_id();
Vivek Goyala3ea8ac2005-06-25 14:58:14 -0700110
111 /* Don't do anything if this handler is invoked on crashing cpu.
112 * Otherwise, system will completely hang. Crashing cpu can get
113 * an NMI if system was initially booted with nmi_watchdog parameter.
114 */
115 if (cpu == crashing_cpu)
Vivek Goyal260d6792006-09-26 10:52:27 +0200116 return NOTIFY_STOP;
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700117 local_irq_disable();
Vivek Goyal4d554762005-06-25 14:58:13 -0700118
Jan Beulichdb753bd2006-03-23 02:59:46 -0800119 if (!user_mode_vm(regs)) {
Vivek Goyale996e582006-01-09 20:51:44 -0800120 crash_fixup_ss_esp(&fixed_regs, regs);
Vivek Goyal4d554762005-06-25 14:58:13 -0700121 regs = &fixed_regs;
122 }
Eric W. Biederman2c818b42005-06-25 14:57:59 -0700123 crash_save_this_cpu(regs, cpu);
Vivek Goyal19842d62005-11-15 00:09:04 -0800124 disable_local_APIC();
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700125 atomic_dec(&waiting_for_crash_ipi);
126 /* Assume hlt works */
Zachary Amsdenf2ab4462005-09-03 15:56:42 -0700127 halt();
Chuck Ebbertcaad3c22006-06-25 05:46:53 -0700128 for (;;)
129 cpu_relax();
Maneesh Soni72414d32005-06-25 14:58:28 -0700130
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700131 return 1;
132}
133
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700134static void smp_send_nmi_allbutself(void)
135{
Keith Owens45486f82006-06-26 13:59:41 +0200136 send_IPI_allbutself(NMI_VECTOR);
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700137}
138
Don Zickus2fbe7b22006-09-26 10:52:27 +0200139static struct notifier_block crash_nmi_nb = {
140 .notifier_call = crash_nmi_callback,
141};
142
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700143static void nmi_shootdown_cpus(void)
144{
145 unsigned long msecs;
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700146
Maneesh Soni72414d32005-06-25 14:58:28 -0700147 atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700148 /* Would it be better to replace the trap vector here? */
Don Zickus2fbe7b22006-09-26 10:52:27 +0200149 if (register_die_notifier(&crash_nmi_nb))
150 return; /* return what? */
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700151 /* Ensure the new callback function is set before sending
152 * out the NMI
153 */
154 wmb();
155
156 smp_send_nmi_allbutself();
157
158 msecs = 1000; /* Wait at most a second for the other cpus to stop */
159 while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
160 mdelay(1);
161 msecs--;
162 }
163
164 /* Leave the nmi callback set */
Vivek Goyal19842d62005-11-15 00:09:04 -0800165 disable_local_APIC();
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700166}
167#else
168static void nmi_shootdown_cpus(void)
169{
170 /* There are no cpus to shootdown */
171}
172#endif
173
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700174void machine_crash_shutdown(struct pt_regs *regs)
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700175{
176 /* This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200177 * has panicked or is otherwise in a critical state.
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700178 * The minimum amount of code to allow a kexec'd kernel
179 * to run successfully needs to happen here.
180 *
181 * In practice this means shooting down the other cpus in
182 * an SMP system.
183 */
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700184 /* The kernel is broken so disable interrupts */
185 local_irq_disable();
Vivek Goyala3ea8ac2005-06-25 14:58:14 -0700186
187 /* Make a note of crashing cpu. Will be used in NMI callback.*/
188 crashing_cpu = smp_processor_id();
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700189 nmi_shootdown_cpus();
Vivek Goyal19842d62005-11-15 00:09:04 -0800190 lapic_shutdown();
191#if defined(CONFIG_X86_IO_APIC)
192 disable_IO_APIC();
193#endif
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700194 crash_save_self(regs);
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700195}