blob: 53589d1b1a05ba882ef7b69bd43557970a69e742 [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>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070025#include <linux/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
Eric W. Biedermane78a8872006-07-14 00:24:00 -070034#if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070035static atomic_t waiting_for_crash_ipi;
36
Don Zickus2fbe7b22006-09-26 10:52:27 +020037static int crash_nmi_callback(struct notifier_block *self,
38 unsigned long val, void *data)
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070039{
Don Zickus2fbe7b22006-09-26 10:52:27 +020040 struct pt_regs *regs;
Vivek Goyal4d554762005-06-25 14:58:13 -070041 struct pt_regs fixed_regs;
Don Zickus2fbe7b22006-09-26 10:52:27 +020042 int cpu;
43
Vivek Goyal260d6792006-09-26 10:52:27 +020044 if (val != DIE_NMI_IPI)
Don Zickus2fbe7b22006-09-26 10:52:27 +020045 return NOTIFY_OK;
46
47 regs = ((struct die_args *)data)->regs;
48 cpu = raw_smp_processor_id();
Vivek Goyala3ea8ac2005-06-25 14:58:14 -070049
50 /* Don't do anything if this handler is invoked on crashing cpu.
51 * Otherwise, system will completely hang. Crashing cpu can get
52 * an NMI if system was initially booted with nmi_watchdog parameter.
53 */
54 if (cpu == crashing_cpu)
Vivek Goyal260d6792006-09-26 10:52:27 +020055 return NOTIFY_STOP;
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070056 local_irq_disable();
Vivek Goyal4d554762005-06-25 14:58:13 -070057
Jan Beulichdb753bd2006-03-23 02:59:46 -080058 if (!user_mode_vm(regs)) {
Vivek Goyale996e582006-01-09 20:51:44 -080059 crash_fixup_ss_esp(&fixed_regs, regs);
Vivek Goyal4d554762005-06-25 14:58:13 -070060 regs = &fixed_regs;
61 }
Magnus Damm85916f82006-12-06 20:40:41 -080062 crash_save_cpu(regs, cpu);
Vivek Goyal19842d62005-11-15 00:09:04 -080063 disable_local_APIC();
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070064 atomic_dec(&waiting_for_crash_ipi);
65 /* Assume hlt works */
Zachary Amsdenf2ab4462005-09-03 15:56:42 -070066 halt();
Chuck Ebbertcaad3c22006-06-25 05:46:53 -070067 for (;;)
68 cpu_relax();
Maneesh Soni72414d32005-06-25 14:58:28 -070069
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070070 return 1;
71}
72
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070073static void smp_send_nmi_allbutself(void)
74{
Fernando Vazquezbc036132006-09-30 23:29:10 -070075 cpumask_t mask = cpu_online_map;
76 cpu_clear(safe_smp_processor_id(), mask);
77 if (!cpus_empty(mask))
78 send_IPI_mask(mask, NMI_VECTOR);
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070079}
80
Don Zickus2fbe7b22006-09-26 10:52:27 +020081static struct notifier_block crash_nmi_nb = {
82 .notifier_call = crash_nmi_callback,
83};
84
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070085static void nmi_shootdown_cpus(void)
86{
87 unsigned long msecs;
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070088
Maneesh Soni72414d32005-06-25 14:58:28 -070089 atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070090 /* Would it be better to replace the trap vector here? */
Don Zickus2fbe7b22006-09-26 10:52:27 +020091 if (register_die_notifier(&crash_nmi_nb))
92 return; /* return what? */
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -070093 /* Ensure the new callback function is set before sending
94 * out the NMI
95 */
96 wmb();
97
98 smp_send_nmi_allbutself();
99
100 msecs = 1000; /* Wait at most a second for the other cpus to stop */
101 while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
102 mdelay(1);
103 msecs--;
104 }
105
106 /* Leave the nmi callback set */
Vivek Goyal19842d62005-11-15 00:09:04 -0800107 disable_local_APIC();
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700108}
109#else
110static void nmi_shootdown_cpus(void)
111{
112 /* There are no cpus to shootdown */
113}
114#endif
115
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700116void machine_crash_shutdown(struct pt_regs *regs)
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700117{
118 /* This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200119 * has panicked or is otherwise in a critical state.
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700120 * The minimum amount of code to allow a kexec'd kernel
121 * to run successfully needs to happen here.
122 *
123 * In practice this means shooting down the other cpus in
124 * an SMP system.
125 */
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700126 /* The kernel is broken so disable interrupts */
127 local_irq_disable();
Vivek Goyala3ea8ac2005-06-25 14:58:14 -0700128
129 /* Make a note of crashing cpu. Will be used in NMI callback.*/
Fernando Vazquezce53af92006-09-30 23:29:09 -0700130 crashing_cpu = safe_smp_processor_id();
Eric W. Biedermanc4ac4262005-06-25 14:57:58 -0700131 nmi_shootdown_cpus();
Vivek Goyal19842d62005-11-15 00:09:04 -0800132 lapic_shutdown();
133#if defined(CONFIG_X86_IO_APIC)
134 disable_IO_APIC();
135#endif
Magnus Damm85916f82006-12-06 20:40:41 -0800136 crash_save_cpu(regs, safe_smp_processor_id());
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700137}