blob: ed2f0f9dc894d9599e6b19b4218771140047a6e2 [file] [log] [blame]
Eric W. Biederman5033cba2005-06-25 14:57:56 -07001/*
Hiroshi Shimamoto62a31a02007-10-19 18:24:20 -07002 * Architecture specific (i386/x86_64) functions for kexec based crash dumps.
Eric W. Biederman5033cba2005-06-25 14:57:56 -07003 *
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>
OGAWA Hirofumi0c1b2722007-12-03 17:17:10 +010025#include <asm/hpet.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070026#include <linux/kdebug.h>
Fernando Vazquezce53af92006-09-30 23:29:09 -070027#include <asm/smp.h>
Glauber Costaed23dc62008-03-17 16:08:38 -030028#include <asm/reboot.h>
Don Zickus2fbe7b22006-09-26 10:52:27 +020029
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070030#include <mach_ipi.h>
Eric W. Biederman5033cba2005-06-25 14:57:56 -070031
Eduardo Habkostb2bbe712008-11-12 11:34:38 -020032#if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
33
Vivek Goyala3ea8ac2005-06-25 14:58:14 -070034/* This keeps a track of which one is crashing cpu. */
35static int crashing_cpu;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070036
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070037static atomic_t waiting_for_crash_ipi;
38
Eduardo Habkosta7d41822008-11-12 11:34:37 -020039static void kdump_nmi_callback(int cpu, struct die_args *args)
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070040{
Don Zickus2fbe7b22006-09-26 10:52:27 +020041 struct pt_regs *regs;
Mike Galbraith1fb473d2007-10-24 12:58:01 +020042#ifdef CONFIG_X86_32
Vivek Goyal4d554762005-06-25 14:58:13 -070043 struct pt_regs fixed_regs;
Hiroshi Shimamoto62a31a02007-10-19 18:24:20 -070044#endif
Eduardo Habkosta7d41822008-11-12 11:34:37 -020045
46 regs = args->regs;
47
48#ifdef CONFIG_X86_32
49 if (!user_mode_vm(regs)) {
50 crash_fixup_ss_esp(&fixed_regs, regs);
51 regs = &fixed_regs;
52 }
53#endif
54 crash_save_cpu(regs, cpu);
55
56 disable_local_APIC();
57}
58
59static int crash_nmi_callback(struct notifier_block *self,
60 unsigned long val, void *data)
61{
Don Zickus2fbe7b22006-09-26 10:52:27 +020062 int cpu;
63
Vivek Goyal260d6792006-09-26 10:52:27 +020064 if (val != DIE_NMI_IPI)
Don Zickus2fbe7b22006-09-26 10:52:27 +020065 return NOTIFY_OK;
66
Don Zickus2fbe7b22006-09-26 10:52:27 +020067 cpu = raw_smp_processor_id();
Vivek Goyala3ea8ac2005-06-25 14:58:14 -070068
69 /* Don't do anything if this handler is invoked on crashing cpu.
70 * Otherwise, system will completely hang. Crashing cpu can get
71 * an NMI if system was initially booted with nmi_watchdog parameter.
72 */
73 if (cpu == crashing_cpu)
Vivek Goyal260d6792006-09-26 10:52:27 +020074 return NOTIFY_STOP;
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070075 local_irq_disable();
Vivek Goyal4d554762005-06-25 14:58:13 -070076
Eduardo Habkosta7d41822008-11-12 11:34:37 -020077 kdump_nmi_callback(cpu, (struct die_args *)data);
78
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070079 atomic_dec(&waiting_for_crash_ipi);
80 /* Assume hlt works */
Zachary Amsdenf2ab4462005-09-03 15:56:42 -070081 halt();
Chuck Ebbertcaad3c22006-06-25 05:46:53 -070082 for (;;)
83 cpu_relax();
Maneesh Soni72414d32005-06-25 14:58:28 -070084
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070085 return 1;
86}
87
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070088static void smp_send_nmi_allbutself(void)
89{
Fernando Vazquezbc036132006-09-30 23:29:10 -070090 cpumask_t mask = cpu_online_map;
91 cpu_clear(safe_smp_processor_id(), mask);
92 if (!cpus_empty(mask))
93 send_IPI_mask(mask, NMI_VECTOR);
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070094}
95
Don Zickus2fbe7b22006-09-26 10:52:27 +020096static struct notifier_block crash_nmi_nb = {
97 .notifier_call = crash_nmi_callback,
98};
99
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700100static void nmi_shootdown_cpus(void)
101{
102 unsigned long msecs;
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700103
Eduardo Habkostb2bbe712008-11-12 11:34:38 -0200104 /* Make a note of crashing cpu. Will be used in NMI callback.*/
105 crashing_cpu = safe_smp_processor_id();
106
Maneesh Soni72414d32005-06-25 14:58:28 -0700107 atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700108 /* Would it be better to replace the trap vector here? */
Don Zickus2fbe7b22006-09-26 10:52:27 +0200109 if (register_die_notifier(&crash_nmi_nb))
110 return; /* return what? */
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700111 /* Ensure the new callback function is set before sending
112 * out the NMI
113 */
114 wmb();
115
116 smp_send_nmi_allbutself();
117
118 msecs = 1000; /* Wait at most a second for the other cpus to stop */
119 while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
120 mdelay(1);
121 msecs--;
122 }
123
124 /* Leave the nmi callback set */
Vivek Goyal19842d62005-11-15 00:09:04 -0800125 disable_local_APIC();
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700126}
127#else
128static void nmi_shootdown_cpus(void)
129{
130 /* There are no cpus to shootdown */
131}
132#endif
133
Glauber Costaed23dc62008-03-17 16:08:38 -0300134void native_machine_crash_shutdown(struct pt_regs *regs)
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700135{
136 /* This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200137 * has panicked or is otherwise in a critical state.
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700138 * The minimum amount of code to allow a kexec'd kernel
139 * to run successfully needs to happen here.
140 *
141 * In practice this means shooting down the other cpus in
142 * an SMP system.
143 */
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700144 /* The kernel is broken so disable interrupts */
145 local_irq_disable();
Vivek Goyala3ea8ac2005-06-25 14:58:14 -0700146
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700147 nmi_shootdown_cpus();
Vivek Goyal19842d62005-11-15 00:09:04 -0800148 lapic_shutdown();
149#if defined(CONFIG_X86_IO_APIC)
150 disable_IO_APIC();
151#endif
OGAWA Hirofumi0c1b2722007-12-03 17:17:10 +0100152#ifdef CONFIG_HPET_TIMER
153 hpet_disable();
154#endif
Magnus Damm85916f82006-12-06 20:40:41 -0800155 crash_save_cpu(regs, safe_smp_processor_id());
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700156}