[PATCH] kdump: x86_64 kexec on panic
[powerpc.git] / arch / x86_64 / kernel / crash.c
1 /*
2  * Architecture specific (x86_64) 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>
14 #include <linux/reboot.h>
15 #include <linux/kexec.h>
16 #include <linux/delay.h>
17
18 #include <asm/processor.h>
19 #include <asm/hardirq.h>
20 #include <asm/nmi.h>
21 #include <asm/hw_irq.h>
22 #include <asm/mach_apic.h>
23
24 /* This keeps a track of which one is crashing cpu. */
25 static int crashing_cpu;
26
27 #ifdef CONFIG_SMP
28 static atomic_t waiting_for_crash_ipi;
29
30 static int crash_nmi_callback(struct pt_regs *regs, int cpu)
31 {
32         /*
33          * Don't do anything if this handler is invoked on crashing cpu.
34          * Otherwise, system will completely hang. Crashing cpu can get
35          * an NMI if system was initially booted with nmi_watchdog parameter.
36          */
37         if (cpu == crashing_cpu)
38                 return 1;
39         local_irq_disable();
40
41         disable_local_APIC();
42         atomic_dec(&waiting_for_crash_ipi);
43         /* Assume hlt works */
44         for(;;)
45                 asm("hlt");
46
47         return 1;
48 }
49
50 static void smp_send_nmi_allbutself(void)
51 {
52         send_IPI_allbutself(APIC_DM_NMI);
53 }
54
55 /*
56  * This code is a best effort heuristic to get the
57  * other cpus to stop executing. So races with
58  * cpu hotplug shouldn't matter.
59  */
60
61 static void nmi_shootdown_cpus(void)
62 {
63         unsigned long msecs;
64
65         atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
66         set_nmi_callback(crash_nmi_callback);
67
68         /*
69          * Ensure the new callback function is set before sending
70          * out the NMI
71          */
72         wmb();
73
74         smp_send_nmi_allbutself();
75
76         msecs = 1000; /* Wait at most a second for the other cpus to stop */
77         while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
78                 mdelay(1);
79                 msecs--;
80         }
81         /* Leave the nmi callback set */
82         disable_local_APIC();
83 }
84 #else
85 static void nmi_shootdown_cpus(void)
86 {
87         /* There are no cpus to shootdown */
88 }
89 #endif
90
91 void machine_crash_shutdown(struct pt_regs *regs)
92 {
93         /*
94          * This function is only called after the system
95          * has paniced or is otherwise in a critical state.
96          * The minimum amount of code to allow a kexec'd kernel
97          * to run successfully needs to happen here.
98          *
99          * In practice this means shooting down the other cpus in
100          * an SMP system.
101          */
102         /* The kernel is broken so disable interrupts */
103         local_irq_disable();
104
105         /* Make a note of crashing cpu. Will be used in NMI callback.*/
106         crashing_cpu = smp_processor_id();
107         nmi_shootdown_cpus();
108
109         if(cpu_has_apic)
110                  disable_local_APIC();
111
112 #if defined(CONFIG_X86_IO_APIC)
113         disable_IO_APIC();
114 #endif
115
116 }