Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 1 | /* |
Hiroshi Shimamoto | 62a31a0 | 2007-10-19 18:24:20 -0700 | [diff] [blame] | 2 | * Architecture specific (i386/x86_64) functions for kexec based crash dumps. |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 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. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 14 | #include <linux/reboot.h> |
| 15 | #include <linux/kexec.h> |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 16 | #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 Goyal | 19842d6 | 2005-11-15 00:09:04 -0800 | [diff] [blame] | 24 | #include <asm/apic.h> |
OGAWA Hirofumi | 0c1b272 | 2007-12-03 17:17:10 +0100 | [diff] [blame] | 25 | #include <asm/hpet.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 26 | #include <linux/kdebug.h> |
Fernando Vazquez | ce53af9 | 2006-09-30 23:29:09 -0700 | [diff] [blame] | 27 | #include <asm/smp.h> |
Glauber Costa | ed23dc6 | 2008-03-17 16:08:38 -0300 | [diff] [blame] | 28 | #include <asm/reboot.h> |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 29 | |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 30 | #include <mach_ipi.h> |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 31 | |
Eduardo Habkost | 8e29478 | 2008-11-12 11:34:40 -0200 | [diff] [blame^] | 32 | typedef void (*nmi_shootdown_cb)(int, struct die_args*); |
| 33 | |
Eduardo Habkost | b2bbe71 | 2008-11-12 11:34:38 -0200 | [diff] [blame] | 34 | #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC) |
| 35 | |
Vivek Goyal | a3ea8ac | 2005-06-25 14:58:14 -0700 | [diff] [blame] | 36 | /* This keeps a track of which one is crashing cpu. */ |
| 37 | static int crashing_cpu; |
Eduardo Habkost | 8e29478 | 2008-11-12 11:34:40 -0200 | [diff] [blame^] | 38 | static nmi_shootdown_cb shootdown_callback; |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 39 | |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 40 | static atomic_t waiting_for_crash_ipi; |
| 41 | |
Eduardo Habkost | a7d4182 | 2008-11-12 11:34:37 -0200 | [diff] [blame] | 42 | static void kdump_nmi_callback(int cpu, struct die_args *args) |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 43 | { |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 44 | struct pt_regs *regs; |
Mike Galbraith | 1fb473d | 2007-10-24 12:58:01 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_X86_32 |
Vivek Goyal | 4d55476 | 2005-06-25 14:58:13 -0700 | [diff] [blame] | 46 | struct pt_regs fixed_regs; |
Hiroshi Shimamoto | 62a31a0 | 2007-10-19 18:24:20 -0700 | [diff] [blame] | 47 | #endif |
Eduardo Habkost | a7d4182 | 2008-11-12 11:34:37 -0200 | [diff] [blame] | 48 | |
| 49 | regs = args->regs; |
| 50 | |
| 51 | #ifdef CONFIG_X86_32 |
| 52 | if (!user_mode_vm(regs)) { |
| 53 | crash_fixup_ss_esp(&fixed_regs, regs); |
| 54 | regs = &fixed_regs; |
| 55 | } |
| 56 | #endif |
| 57 | crash_save_cpu(regs, cpu); |
| 58 | |
| 59 | disable_local_APIC(); |
| 60 | } |
| 61 | |
| 62 | static int crash_nmi_callback(struct notifier_block *self, |
| 63 | unsigned long val, void *data) |
| 64 | { |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 65 | int cpu; |
| 66 | |
Vivek Goyal | 260d679 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 67 | if (val != DIE_NMI_IPI) |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 68 | return NOTIFY_OK; |
| 69 | |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 70 | cpu = raw_smp_processor_id(); |
Vivek Goyal | a3ea8ac | 2005-06-25 14:58:14 -0700 | [diff] [blame] | 71 | |
| 72 | /* Don't do anything if this handler is invoked on crashing cpu. |
| 73 | * Otherwise, system will completely hang. Crashing cpu can get |
| 74 | * an NMI if system was initially booted with nmi_watchdog parameter. |
| 75 | */ |
| 76 | if (cpu == crashing_cpu) |
Vivek Goyal | 260d679 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 77 | return NOTIFY_STOP; |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 78 | local_irq_disable(); |
Vivek Goyal | 4d55476 | 2005-06-25 14:58:13 -0700 | [diff] [blame] | 79 | |
Eduardo Habkost | 8e29478 | 2008-11-12 11:34:40 -0200 | [diff] [blame^] | 80 | shootdown_callback(cpu, (struct die_args *)data); |
Eduardo Habkost | a7d4182 | 2008-11-12 11:34:37 -0200 | [diff] [blame] | 81 | |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 82 | atomic_dec(&waiting_for_crash_ipi); |
| 83 | /* Assume hlt works */ |
Zachary Amsden | f2ab446 | 2005-09-03 15:56:42 -0700 | [diff] [blame] | 84 | halt(); |
Chuck Ebbert | caad3c2 | 2006-06-25 05:46:53 -0700 | [diff] [blame] | 85 | for (;;) |
| 86 | cpu_relax(); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 87 | |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 88 | return 1; |
| 89 | } |
| 90 | |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 91 | static void smp_send_nmi_allbutself(void) |
| 92 | { |
Fernando Vazquez | bc03613 | 2006-09-30 23:29:10 -0700 | [diff] [blame] | 93 | cpumask_t mask = cpu_online_map; |
| 94 | cpu_clear(safe_smp_processor_id(), mask); |
| 95 | if (!cpus_empty(mask)) |
| 96 | send_IPI_mask(mask, NMI_VECTOR); |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 99 | static struct notifier_block crash_nmi_nb = { |
| 100 | .notifier_call = crash_nmi_callback, |
| 101 | }; |
| 102 | |
Eduardo Habkost | 8e29478 | 2008-11-12 11:34:40 -0200 | [diff] [blame^] | 103 | static void nmi_shootdown_cpus(nmi_shootdown_cb callback) |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 104 | { |
| 105 | unsigned long msecs; |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 106 | |
Eduardo Habkost | b2bbe71 | 2008-11-12 11:34:38 -0200 | [diff] [blame] | 107 | /* Make a note of crashing cpu. Will be used in NMI callback.*/ |
| 108 | crashing_cpu = safe_smp_processor_id(); |
| 109 | |
Eduardo Habkost | 8e29478 | 2008-11-12 11:34:40 -0200 | [diff] [blame^] | 110 | shootdown_callback = callback; |
| 111 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 112 | atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1); |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 113 | /* Would it be better to replace the trap vector here? */ |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 114 | if (register_die_notifier(&crash_nmi_nb)) |
| 115 | return; /* return what? */ |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 116 | /* Ensure the new callback function is set before sending |
| 117 | * out the NMI |
| 118 | */ |
| 119 | wmb(); |
| 120 | |
| 121 | smp_send_nmi_allbutself(); |
| 122 | |
| 123 | msecs = 1000; /* Wait at most a second for the other cpus to stop */ |
| 124 | while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) { |
| 125 | mdelay(1); |
| 126 | msecs--; |
| 127 | } |
| 128 | |
| 129 | /* Leave the nmi callback set */ |
Eduardo Habkost | d1e7b91 | 2008-11-12 11:34:39 -0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void kdump_nmi_shootdown_cpus(void) |
| 133 | { |
Eduardo Habkost | 8e29478 | 2008-11-12 11:34:40 -0200 | [diff] [blame^] | 134 | nmi_shootdown_cpus(kdump_nmi_callback); |
Eduardo Habkost | d1e7b91 | 2008-11-12 11:34:39 -0200 | [diff] [blame] | 135 | |
Vivek Goyal | 19842d6 | 2005-11-15 00:09:04 -0800 | [diff] [blame] | 136 | disable_local_APIC(); |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 137 | } |
Eduardo Habkost | d1e7b91 | 2008-11-12 11:34:39 -0200 | [diff] [blame] | 138 | |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 139 | #else |
Eduardo Habkost | d1e7b91 | 2008-11-12 11:34:39 -0200 | [diff] [blame] | 140 | static void kdump_nmi_shootdown_cpus(void) |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 141 | { |
| 142 | /* There are no cpus to shootdown */ |
| 143 | } |
| 144 | #endif |
| 145 | |
Glauber Costa | ed23dc6 | 2008-03-17 16:08:38 -0300 | [diff] [blame] | 146 | void native_machine_crash_shutdown(struct pt_regs *regs) |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 147 | { |
| 148 | /* This function is only called after the system |
Lee Revell | f18190b | 2006-06-26 18:30:00 +0200 | [diff] [blame] | 149 | * has panicked or is otherwise in a critical state. |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 150 | * The minimum amount of code to allow a kexec'd kernel |
| 151 | * to run successfully needs to happen here. |
| 152 | * |
| 153 | * In practice this means shooting down the other cpus in |
| 154 | * an SMP system. |
| 155 | */ |
Eric W. Biederman | c4ac426 | 2005-06-25 14:57:58 -0700 | [diff] [blame] | 156 | /* The kernel is broken so disable interrupts */ |
| 157 | local_irq_disable(); |
Vivek Goyal | a3ea8ac | 2005-06-25 14:58:14 -0700 | [diff] [blame] | 158 | |
Eduardo Habkost | d1e7b91 | 2008-11-12 11:34:39 -0200 | [diff] [blame] | 159 | kdump_nmi_shootdown_cpus(); |
Vivek Goyal | 19842d6 | 2005-11-15 00:09:04 -0800 | [diff] [blame] | 160 | lapic_shutdown(); |
| 161 | #if defined(CONFIG_X86_IO_APIC) |
| 162 | disable_IO_APIC(); |
| 163 | #endif |
OGAWA Hirofumi | 0c1b272 | 2007-12-03 17:17:10 +0100 | [diff] [blame] | 164 | #ifdef CONFIG_HPET_TIMER |
| 165 | hpet_disable(); |
| 166 | #endif |
Magnus Damm | 85916f8 | 2006-12-06 20:40:41 -0800 | [diff] [blame] | 167 | crash_save_cpu(regs, safe_smp_processor_id()); |
Eric W. Biederman | 5033cba | 2005-06-25 14:57:56 -0700 | [diff] [blame] | 168 | } |