blob: 0060e44e8989188ee31b9f28f6ced980320d32fb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * NMI watchdog support on APIC systems
3 *
4 * Started by Ingo Molnar <mingo@redhat.com>
5 *
6 * Fixes:
7 * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
8 * Mikael Pettersson : Power Management for local APIC NMI watchdog.
9 * Pavel Machek and
10 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
11 */
12
Andrew Mortonbb81a092006-12-07 02:14:01 +010013#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/sysdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/sysctl.h>
Andi Kleeneddb6fb2006-02-03 21:50:41 +010020#include <linux/kprobes.h>
Andrew Mortonbb81a092006-12-07 02:14:01 +010021#include <linux/cpumask.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070022#include <linux/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Maciej W. Rozycki35542c52008-05-21 22:10:22 +010024#include <asm/i8259.h>
25#include <asm/io_apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/proto.h>
Andi Kleen553f2652006-04-07 19:49:57 +020029#include <asm/mce.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Glauber de Oliveira Costae32ede12008-03-19 14:25:35 -030031#include <mach_traps.h>
32
Andi Kleen29cbc782006-09-30 01:47:55 +020033int unknown_nmi_panic;
34int nmi_watchdog_enabled;
35int panic_on_unrecovered_nmi;
36
Andi Kleen1714f9b2007-04-16 10:30:27 +020037static cpumask_t backtrace_mask = CPU_MASK_NONE;
Don Zickus828f0af2006-09-26 10:52:26 +020038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* nmi_active:
Don Zickusf2802e72006-09-26 10:52:26 +020040 * >0: the lapic NMI watchdog is active, but can be disabled
41 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * be enabled
Don Zickusf2802e72006-09-26 10:52:26 +020043 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
Don Zickusf2802e72006-09-26 10:52:26 +020045atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Adrian Bunk867ab542008-01-30 13:30:31 +010046static int panic_on_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48unsigned int nmi_watchdog = NMI_DEFAULT;
49static unsigned int nmi_hz = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andi Kleen05cb0072007-05-02 19:27:20 +020051static DEFINE_PER_CPU(short, wd_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* Run after command line and cpu_init init, but before all other checks */
Don Zickuse33e89a2006-09-26 10:52:27 +020054void nmi_watchdog_default(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 if (nmi_watchdog != NMI_DEFAULT)
57 return;
Linus Torvalds8ce5e3e2007-03-14 17:50:48 -070058 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010061static int endflag __initdata = 0;
62
Andi Kleen75152112005-05-16 21:53:34 -070063#ifdef CONFIG_SMP
64/* The performance counters used by NMI_LOCAL_APIC don't trigger when
65 * the CPU is idle. To make sure the NMI watchdog really ticks on all
66 * CPUs during the test make them busy.
67 */
68static __init void nmi_cpu_busy(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Ingo Molnar366c7f52006-07-03 00:25:25 -070070 local_irq_enable_in_hardirq();
Andi Kleen75152112005-05-16 21:53:34 -070071 /* Intentionally don't use cpu_relax here. This is
72 to make sure that the performance counter really ticks,
73 even if there is a simulator or similar that catches the
74 pause instruction. On a real HT machine this is fine because
75 all other CPUs are busy with "useless" delay loops and don't
76 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010077 while (endflag == 0)
78 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
Andi Kleen75152112005-05-16 21:53:34 -070080#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010082int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010084 int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int cpu;
86
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010087 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
Don Zickusf2802e72006-09-26 10:52:26 +020088 return 0;
89
90 if (!atomic_read(&nmi_active))
91 return 0;
92
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010093 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
94 if (!prev_nmi_count)
Maciej W. Rozycki35542c52008-05-21 22:10:22 +010095 goto error;
Jack F Vogel67701ae2005-05-01 08:58:48 -070096
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010097 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Andi Kleen7554c3f2006-01-11 22:45:45 +010099#ifdef CONFIG_SMP
Andi Kleen75152112005-05-16 21:53:34 -0700100 if (nmi_watchdog == NMI_LOCAL_APIC)
101 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Andi Kleen7554c3f2006-01-11 22:45:45 +0100102#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 for (cpu = 0; cpu < NR_CPUS; cpu++)
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100105 prev_nmi_count[cpu] = cpu_pda(cpu)->__nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 local_irq_enable();
Andi Kleen0fb2ebf2007-04-02 12:14:12 +0200107 mdelay((20*1000)/nmi_hz); // wait 20 ticks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Andrew Morton394e3902006-03-23 03:01:05 -0800109 for_each_online_cpu(cpu) {
Andi Kleen05cb0072007-05-02 19:27:20 +0200110 if (!per_cpu(wd_enabled, cpu))
Don Zickusf2802e72006-09-26 10:52:26 +0200111 continue;
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100112 if (cpu_pda(cpu)->__nmi_count - prev_nmi_count[cpu] <= 5) {
Don Zickus75bc1222007-12-04 17:19:07 +0100113 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
114 "appears to be stuck (%d->%d)!\n",
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100115 cpu,
116 prev_nmi_count[cpu],
117 cpu_pda(cpu)->__nmi_count);
Andi Kleen05cb0072007-05-02 19:27:20 +0200118 per_cpu(wd_enabled, cpu) = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200119 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 }
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100122 endflag = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200123 if (!atomic_read(&nmi_active)) {
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100124 kfree(prev_nmi_count);
Don Zickusf2802e72006-09-26 10:52:26 +0200125 atomic_set(&nmi_active, -1);
Maciej W. Rozycki35542c52008-05-21 22:10:22 +0100126 goto error;
Don Zickusf2802e72006-09-26 10:52:26 +0200127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 printk("OK.\n");
129
130 /* now that we know it works we can reduce NMI frequency to
131 something more reasonable; makes a difference in some configs */
Andi Kleen05cb0072007-05-02 19:27:20 +0200132 if (nmi_watchdog == NMI_LOCAL_APIC)
133 nmi_hz = lapic_adjust_nmi_hz(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100135 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return 0;
Maciej W. Rozycki35542c52008-05-21 22:10:22 +0100137error:
138 if (nmi_watchdog == NMI_IO_APIC && !timer_through_8259)
139 disable_8259A_irq(0);
140
141 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Adrian Bunk867ab542008-01-30 13:30:31 +0100144static int __init setup_nmi_watchdog(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int nmi;
147
148 if (!strncmp(str,"panic",5)) {
149 panic_on_timeout = 1;
150 str = strchr(str, ',');
151 if (!str)
152 return 1;
153 ++str;
154 }
155
156 get_option(&str, &nmi);
157
Don Zickusf2802e72006-09-26 10:52:26 +0200158 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200160
Andi Kleen75152112005-05-16 21:53:34 -0700161 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return 1;
163}
164
165__setup("nmi_watchdog=", setup_nmi_watchdog);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#ifdef CONFIG_PM
168
169static int nmi_pm_active; /* nmi_active before suspend */
170
Pavel Machek829ca9a2005-09-03 15:56:56 -0700171static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Shaohua Li4038f902006-09-26 10:52:27 +0200173 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusf2802e72006-09-26 10:52:26 +0200174 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200175 stop_apic_nmi_watchdog(NULL);
176 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return 0;
178}
179
180static int lapic_nmi_resume(struct sys_device *dev)
181{
Shaohua Li4038f902006-09-26 10:52:27 +0200182 /* only CPU0 goes here, other CPUs should be offline */
183 if (nmi_pm_active > 0) {
184 setup_apic_nmi_watchdog(NULL);
185 touch_nmi_watchdog();
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return 0;
188}
189
190static struct sysdev_class nmi_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100191 .name = "lapic_nmi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .resume = lapic_nmi_resume,
193 .suspend = lapic_nmi_suspend,
194};
195
196static struct sys_device device_lapic_nmi = {
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100197 .id = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .cls = &nmi_sysclass,
199};
200
201static int __init init_lapic_nmi_sysfs(void)
202{
203 int error;
204
Don Zickusf2802e72006-09-26 10:52:26 +0200205 /* should really be a BUG_ON but b/c this is an
206 * init call, it just doesn't work. -dcz
207 */
208 if (nmi_watchdog != NMI_LOCAL_APIC)
209 return 0;
210
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100211 if (atomic_read(&nmi_active) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 0;
213
214 error = sysdev_class_register(&nmi_sysclass);
215 if (!error)
216 error = sysdev_register(&device_lapic_nmi);
217 return error;
218}
219/* must come after the local APIC's device_initcall() */
220late_initcall(init_lapic_nmi_sysfs);
221
222#endif /* CONFIG_PM */
223
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100224static void __acpi_nmi_enable(void *__unused)
225{
226 apic_write(APIC_LVT0, APIC_DM_NMI);
227}
228
229/*
230 * Enable timer based NMIs on all CPUs:
231 */
232void acpi_nmi_enable(void)
233{
234 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
235 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
236}
237
238static void __acpi_nmi_disable(void *__unused)
239{
240 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
241}
242
243/*
244 * Disable timer based NMIs on all CPUs:
245 */
246void acpi_nmi_disable(void)
247{
248 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
249 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
250}
251
Don Zickusf2802e72006-09-26 10:52:26 +0200252void setup_apic_nmi_watchdog(void *unused)
253{
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100254 if (__get_cpu_var(wd_enabled))
Shaohua Li4038f902006-09-26 10:52:27 +0200255 return;
256
257 /* cheap hack to support suspend/resume */
258 /* if cpu0 is not active neither should the other cpus */
259 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
260 return;
261
Andi Kleen05cb0072007-05-02 19:27:20 +0200262 switch (nmi_watchdog) {
263 case NMI_LOCAL_APIC:
264 __get_cpu_var(wd_enabled) = 1;
265 if (lapic_watchdog_init(nmi_hz) < 0) {
266 __get_cpu_var(wd_enabled) = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200267 return;
268 }
Andi Kleen05cb0072007-05-02 19:27:20 +0200269 /* FALL THROUGH */
270 case NMI_IO_APIC:
271 __get_cpu_var(wd_enabled) = 1;
272 atomic_inc(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Don Zickusf2802e72006-09-26 10:52:26 +0200274}
275
Shaohua Li4038f902006-09-26 10:52:27 +0200276void stop_apic_nmi_watchdog(void *unused)
Don Zickusf2802e72006-09-26 10:52:26 +0200277{
278 /* only support LOCAL and IO APICs for now */
279 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
280 (nmi_watchdog != NMI_IO_APIC))
281 return;
Andi Kleen05cb0072007-05-02 19:27:20 +0200282 if (__get_cpu_var(wd_enabled) == 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200283 return;
Andi Kleen05cb0072007-05-02 19:27:20 +0200284 if (nmi_watchdog == NMI_LOCAL_APIC)
285 lapic_watchdog_stop();
286 __get_cpu_var(wd_enabled) = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200287 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/*
291 * the best way to detect whether a CPU has a 'hard lockup' problem
292 * is to check it's local APIC timer IRQ counts. If they are not
293 * changing then that CPU has some problem.
294 *
295 * as these watchdog NMI IRQs are generated on every CPU, we only
296 * have to check the current processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
298
Andi Kleen75152112005-05-16 21:53:34 -0700299static DEFINE_PER_CPU(unsigned, last_irq_sum);
300static DEFINE_PER_CPU(local_t, alert_counter);
301static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Andrew Morton567f3e42007-07-17 04:03:58 -0700303void touch_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Jan Beulich99019e92006-02-16 23:41:55 +0100305 if (nmi_watchdog > 0) {
306 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Jan Beulich99019e92006-02-16 23:41:55 +0100308 /*
309 * Tell other CPUs to reset their alert counters. We cannot
310 * do it ourselves because the alert count increase is not
311 * atomic.
312 */
Andrew Morton567f3e42007-07-17 04:03:58 -0700313 for_each_present_cpu(cpu) {
314 if (per_cpu(nmi_touch, cpu) != 1)
315 per_cpu(nmi_touch, cpu) = 1;
316 }
Jan Beulich99019e92006-02-16 23:41:55 +0100317 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700318
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100319 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100321EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200323notrace __kprobes int
324nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Andi Kleen75152112005-05-16 21:53:34 -0700326 int sum;
327 int touched = 0;
Andrew Mortonbb81a092006-12-07 02:14:01 +0100328 int cpu = smp_processor_id();
Andi Kleen05cb0072007-05-02 19:27:20 +0200329 int rc = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200330
331 /* check for other users first */
332 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
333 == NOTIFY_STOP) {
Don Zickus3adbbcc2006-09-26 10:52:26 +0200334 rc = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200335 touched = 1;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Thomas Gleixner4e77ae32007-10-12 23:04:07 +0200338 sum = read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
Andi Kleen75152112005-05-16 21:53:34 -0700339 if (__get_cpu_var(nmi_touch)) {
340 __get_cpu_var(nmi_touch) = 0;
341 touched = 1;
342 }
Don Zickusf2802e72006-09-26 10:52:26 +0200343
Andrew Mortonbb81a092006-12-07 02:14:01 +0100344 if (cpu_isset(cpu, backtrace_mask)) {
345 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
346
347 spin_lock(&lock);
348 printk("NMI backtrace for cpu %d\n", cpu);
349 dump_stack();
350 spin_unlock(&lock);
351 cpu_clear(cpu, backtrace_mask);
352 }
353
Andi Kleen553f2652006-04-07 19:49:57 +0200354#ifdef CONFIG_X86_MCE
355 /* Could check oops_in_progress here too, but it's safer
356 not too */
357 if (atomic_read(&mce_entry) > 0)
358 touched = 1;
359#endif
Don Zickusf2802e72006-09-26 10:52:26 +0200360 /* if the apic timer isn't firing, this cpu isn't doing much */
Andi Kleen75152112005-05-16 21:53:34 -0700361 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /*
363 * Ayiee, looks like this CPU is stuck ...
364 * wait a few IRQs (5 seconds) before doing the oops ...
365 */
Andi Kleen75152112005-05-16 21:53:34 -0700366 local_inc(&__get_cpu_var(alert_counter));
Don Zickusf2802e72006-09-26 10:52:26 +0200367 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
Andi Kleenfac58552006-09-26 10:52:27 +0200368 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
369 panic_on_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 } else {
Andi Kleen75152112005-05-16 21:53:34 -0700371 __get_cpu_var(last_irq_sum) = sum;
372 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Don Zickusf2802e72006-09-26 10:52:26 +0200374
375 /* see if the nmi watchdog went off */
Andi Kleen05cb0072007-05-02 19:27:20 +0200376 if (!__get_cpu_var(wd_enabled))
377 return rc;
378 switch (nmi_watchdog) {
379 case NMI_LOCAL_APIC:
380 rc |= lapic_wd_event(nmi_hz);
381 break;
382 case NMI_IO_APIC:
383 /* don't know how to accurately check for this.
384 * just assume it was a watchdog timer interrupt
385 * This matches the old behaviour.
386 */
387 rc = 1;
388 break;
Andi Kleen75152112005-05-16 21:53:34 -0700389 }
Don Zickus3adbbcc2006-09-26 10:52:26 +0200390 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Andi Kleen8f4e9562007-07-22 11:12:32 +0200393static unsigned ignore_nmis;
394
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200395asmlinkage notrace __kprobes void
396do_nmi(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 nmi_enter();
399 add_pda(__nmi_count,1);
Andi Kleen8f4e9562007-07-22 11:12:32 +0200400 if (!ignore_nmis)
401 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 nmi_exit();
403}
404
Andi Kleen8f4e9562007-07-22 11:12:32 +0200405void stop_nmi(void)
406{
407 acpi_nmi_disable();
408 ignore_nmis++;
409}
410
411void restart_nmi(void)
412{
413 ignore_nmis--;
414 acpi_nmi_enable();
415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#ifdef CONFIG_SYSCTL
418
419static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
420{
421 unsigned char reason = get_nmi_reason();
422 char buf[64];
423
Don Zickus2fbe7b22006-09-26 10:52:27 +0200424 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Andi Kleenfac58552006-09-26 10:52:27 +0200425 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427}
428
Don Zickus407984f2006-09-26 10:52:27 +0200429/*
430 * proc handler for /proc/sys/kernel/nmi
431 */
432int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
433 void __user *buffer, size_t *length, loff_t *ppos)
434{
435 int old_state;
436
437 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
438 old_state = nmi_watchdog_enabled;
439 proc_dointvec(table, write, file, buffer, length, ppos);
440 if (!!old_state == !!nmi_watchdog_enabled)
441 return 0;
442
Daniel Gollub0328ece2007-08-15 02:40:35 +0200443 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Don Zickus407984f2006-09-26 10:52:27 +0200444 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
Don Zickuse33e89a2006-09-26 10:52:27 +0200445 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200446 }
447
448 /* if nmi_watchdog is not set yet, then set it */
449 nmi_watchdog_default();
450
Don Zickuse33e89a2006-09-26 10:52:27 +0200451 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200452 if (nmi_watchdog_enabled)
453 enable_lapic_nmi_watchdog();
454 else
455 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200456 } else {
Don Zickuse33e89a2006-09-26 10:52:27 +0200457 printk( KERN_WARNING
Don Zickus407984f2006-09-26 10:52:27 +0200458 "NMI watchdog doesn't know what hardware to touch\n");
459 return -EIO;
460 }
461 return 0;
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464#endif
465
Li Zefana062bae2008-02-03 15:40:30 +0800466int do_nmi_callback(struct pt_regs *regs, int cpu)
467{
468#ifdef CONFIG_SYSCTL
469 if (unknown_nmi_panic)
470 return unknown_nmi_panic_callback(regs, cpu);
471#endif
472 return 0;
473}
474
Andrew Mortonbb81a092006-12-07 02:14:01 +0100475void __trigger_all_cpu_backtrace(void)
476{
477 int i;
478
479 backtrace_mask = cpu_online_map;
480 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
481 for (i = 0; i < 10 * 1000; i++) {
482 if (cpus_empty(backtrace_mask))
483 break;
484 mdelay(1);
485 }
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488EXPORT_SYMBOL(nmi_active);
489EXPORT_SYMBOL(nmi_watchdog);