blob: fb99484d21cf6db94f7349d466a124515266b858 [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
24#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/proto.h>
Andi Kleen553f2652006-04-07 19:49:57 +020027#include <asm/mce.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Andi Kleen29cbc782006-09-30 01:47:55 +020029int unknown_nmi_panic;
30int nmi_watchdog_enabled;
31int panic_on_unrecovered_nmi;
32
Andi Kleen1714f9b2007-04-16 10:30:27 +020033static cpumask_t backtrace_mask = CPU_MASK_NONE;
Don Zickus828f0af2006-09-26 10:52:26 +020034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/* nmi_active:
Don Zickusf2802e72006-09-26 10:52:26 +020036 * >0: the lapic NMI watchdog is active, but can be disabled
37 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * be enabled
Don Zickusf2802e72006-09-26 10:52:26 +020039 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
Don Zickusf2802e72006-09-26 10:52:26 +020041atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Adrian Bunk867ab542008-01-30 13:30:31 +010042static int panic_on_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44unsigned int nmi_watchdog = NMI_DEFAULT;
45static unsigned int nmi_hz = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Andi Kleen05cb0072007-05-02 19:27:20 +020047static DEFINE_PER_CPU(short, wd_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Don Zickusf2802e72006-09-26 10:52:26 +020049/* local prototypes */
Don Zickusf2802e72006-09-26 10:52:26 +020050static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
Andi Kleen75152112005-05-16 21:53:34 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* Run after command line and cpu_init init, but before all other checks */
Don Zickuse33e89a2006-09-26 10:52:27 +020053void nmi_watchdog_default(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 if (nmi_watchdog != NMI_DEFAULT)
56 return;
Linus Torvalds8ce5e3e2007-03-14 17:50:48 -070057 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010060static int endflag __initdata = 0;
61
Andi Kleen75152112005-05-16 21:53:34 -070062#ifdef CONFIG_SMP
63/* The performance counters used by NMI_LOCAL_APIC don't trigger when
64 * the CPU is idle. To make sure the NMI watchdog really ticks on all
65 * CPUs during the test make them busy.
66 */
67static __init void nmi_cpu_busy(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Ingo Molnar366c7f52006-07-03 00:25:25 -070069 local_irq_enable_in_hardirq();
Andi Kleen75152112005-05-16 21:53:34 -070070 /* Intentionally don't use cpu_relax here. This is
71 to make sure that the performance counter really ticks,
72 even if there is a simulator or similar that catches the
73 pause instruction. On a real HT machine this is fine because
74 all other CPUs are busy with "useless" delay loops and don't
75 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010076 while (endflag == 0)
77 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Andi Kleen75152112005-05-16 21:53:34 -070079#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010081int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010083 int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int cpu;
85
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010086 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
Don Zickusf2802e72006-09-26 10:52:26 +020087 return 0;
88
89 if (!atomic_read(&nmi_active))
90 return 0;
91
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010092 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
93 if (!prev_nmi_count)
Andi Kleen75152112005-05-16 21:53:34 -070094 return -1;
Jack F Vogel67701ae2005-05-01 08:58:48 -070095
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010096 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Andi Kleen7554c3f2006-01-11 22:45:45 +010098#ifdef CONFIG_SMP
Andi Kleen75152112005-05-16 21:53:34 -070099 if (nmi_watchdog == NMI_LOCAL_APIC)
100 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Andi Kleen7554c3f2006-01-11 22:45:45 +0100101#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 for (cpu = 0; cpu < NR_CPUS; cpu++)
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100104 prev_nmi_count[cpu] = cpu_pda(cpu)->__nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 local_irq_enable();
Andi Kleen0fb2ebf2007-04-02 12:14:12 +0200106 mdelay((20*1000)/nmi_hz); // wait 20 ticks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Andrew Morton394e3902006-03-23 03:01:05 -0800108 for_each_online_cpu(cpu) {
Andi Kleen05cb0072007-05-02 19:27:20 +0200109 if (!per_cpu(wd_enabled, cpu))
Don Zickusf2802e72006-09-26 10:52:26 +0200110 continue;
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100111 if (cpu_pda(cpu)->__nmi_count - prev_nmi_count[cpu] <= 5) {
Don Zickus75bc1222007-12-04 17:19:07 +0100112 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
113 "appears to be stuck (%d->%d)!\n",
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100114 cpu,
115 prev_nmi_count[cpu],
116 cpu_pda(cpu)->__nmi_count);
Andi Kleen05cb0072007-05-02 19:27:20 +0200117 per_cpu(wd_enabled, cpu) = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200118 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 }
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100121 endflag = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200122 if (!atomic_read(&nmi_active)) {
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100123 kfree(prev_nmi_count);
Don Zickusf2802e72006-09-26 10:52:26 +0200124 atomic_set(&nmi_active, -1);
125 return -1;
126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 printk("OK.\n");
128
129 /* now that we know it works we can reduce NMI frequency to
130 something more reasonable; makes a difference in some configs */
Andi Kleen05cb0072007-05-02 19:27:20 +0200131 if (nmi_watchdog == NMI_LOCAL_APIC)
132 nmi_hz = lapic_adjust_nmi_hz(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100134 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136}
137
Adrian Bunk867ab542008-01-30 13:30:31 +0100138static int __init setup_nmi_watchdog(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 int nmi;
141
142 if (!strncmp(str,"panic",5)) {
143 panic_on_timeout = 1;
144 str = strchr(str, ',');
145 if (!str)
146 return 1;
147 ++str;
148 }
149
150 get_option(&str, &nmi);
151
Don Zickusf2802e72006-09-26 10:52:26 +0200152 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200154
Andi Kleen75152112005-05-16 21:53:34 -0700155 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return 1;
157}
158
159__setup("nmi_watchdog=", setup_nmi_watchdog);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#ifdef CONFIG_PM
162
163static int nmi_pm_active; /* nmi_active before suspend */
164
Pavel Machek829ca9a2005-09-03 15:56:56 -0700165static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Shaohua Li4038f902006-09-26 10:52:27 +0200167 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusf2802e72006-09-26 10:52:26 +0200168 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200169 stop_apic_nmi_watchdog(NULL);
170 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172}
173
174static int lapic_nmi_resume(struct sys_device *dev)
175{
Shaohua Li4038f902006-09-26 10:52:27 +0200176 /* only CPU0 goes here, other CPUs should be offline */
177 if (nmi_pm_active > 0) {
178 setup_apic_nmi_watchdog(NULL);
179 touch_nmi_watchdog();
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
182}
183
184static struct sysdev_class nmi_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100185 .name = "lapic_nmi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .resume = lapic_nmi_resume,
187 .suspend = lapic_nmi_suspend,
188};
189
190static struct sys_device device_lapic_nmi = {
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100191 .id = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .cls = &nmi_sysclass,
193};
194
195static int __init init_lapic_nmi_sysfs(void)
196{
197 int error;
198
Don Zickusf2802e72006-09-26 10:52:26 +0200199 /* should really be a BUG_ON but b/c this is an
200 * init call, it just doesn't work. -dcz
201 */
202 if (nmi_watchdog != NMI_LOCAL_APIC)
203 return 0;
204
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100205 if (atomic_read(&nmi_active) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207
208 error = sysdev_class_register(&nmi_sysclass);
209 if (!error)
210 error = sysdev_register(&device_lapic_nmi);
211 return error;
212}
213/* must come after the local APIC's device_initcall() */
214late_initcall(init_lapic_nmi_sysfs);
215
216#endif /* CONFIG_PM */
217
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100218static void __acpi_nmi_enable(void *__unused)
219{
220 apic_write(APIC_LVT0, APIC_DM_NMI);
221}
222
223/*
224 * Enable timer based NMIs on all CPUs:
225 */
226void acpi_nmi_enable(void)
227{
228 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
229 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
230}
231
232static void __acpi_nmi_disable(void *__unused)
233{
234 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
235}
236
237/*
238 * Disable timer based NMIs on all CPUs:
239 */
240void acpi_nmi_disable(void)
241{
242 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
243 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
244}
245
Don Zickusf2802e72006-09-26 10:52:26 +0200246void setup_apic_nmi_watchdog(void *unused)
247{
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100248 if (__get_cpu_var(wd_enabled))
Shaohua Li4038f902006-09-26 10:52:27 +0200249 return;
250
251 /* cheap hack to support suspend/resume */
252 /* if cpu0 is not active neither should the other cpus */
253 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
254 return;
255
Andi Kleen05cb0072007-05-02 19:27:20 +0200256 switch (nmi_watchdog) {
257 case NMI_LOCAL_APIC:
258 __get_cpu_var(wd_enabled) = 1;
259 if (lapic_watchdog_init(nmi_hz) < 0) {
260 __get_cpu_var(wd_enabled) = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200261 return;
262 }
Andi Kleen05cb0072007-05-02 19:27:20 +0200263 /* FALL THROUGH */
264 case NMI_IO_APIC:
265 __get_cpu_var(wd_enabled) = 1;
266 atomic_inc(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Don Zickusf2802e72006-09-26 10:52:26 +0200268}
269
Shaohua Li4038f902006-09-26 10:52:27 +0200270void stop_apic_nmi_watchdog(void *unused)
Don Zickusf2802e72006-09-26 10:52:26 +0200271{
272 /* only support LOCAL and IO APICs for now */
273 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
274 (nmi_watchdog != NMI_IO_APIC))
275 return;
Andi Kleen05cb0072007-05-02 19:27:20 +0200276 if (__get_cpu_var(wd_enabled) == 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200277 return;
Andi Kleen05cb0072007-05-02 19:27:20 +0200278 if (nmi_watchdog == NMI_LOCAL_APIC)
279 lapic_watchdog_stop();
280 __get_cpu_var(wd_enabled) = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200281 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/*
285 * the best way to detect whether a CPU has a 'hard lockup' problem
286 * is to check it's local APIC timer IRQ counts. If they are not
287 * changing then that CPU has some problem.
288 *
289 * as these watchdog NMI IRQs are generated on every CPU, we only
290 * have to check the current processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292
Andi Kleen75152112005-05-16 21:53:34 -0700293static DEFINE_PER_CPU(unsigned, last_irq_sum);
294static DEFINE_PER_CPU(local_t, alert_counter);
295static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Andrew Morton567f3e42007-07-17 04:03:58 -0700297void touch_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Jan Beulich99019e92006-02-16 23:41:55 +0100299 if (nmi_watchdog > 0) {
300 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Jan Beulich99019e92006-02-16 23:41:55 +0100302 /*
303 * Tell other CPUs to reset their alert counters. We cannot
304 * do it ourselves because the alert count increase is not
305 * atomic.
306 */
Andrew Morton567f3e42007-07-17 04:03:58 -0700307 for_each_present_cpu(cpu) {
308 if (per_cpu(nmi_touch, cpu) != 1)
309 per_cpu(nmi_touch, cpu) = 1;
310 }
Jan Beulich99019e92006-02-16 23:41:55 +0100311 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700312
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100313 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100315EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Don Zickus3adbbcce2006-09-26 10:52:26 +0200317int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Andi Kleen75152112005-05-16 21:53:34 -0700319 int sum;
320 int touched = 0;
Andrew Mortonbb81a092006-12-07 02:14:01 +0100321 int cpu = smp_processor_id();
Andi Kleen05cb0072007-05-02 19:27:20 +0200322 int rc = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200323
324 /* check for other users first */
325 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
326 == NOTIFY_STOP) {
Don Zickus3adbbcce2006-09-26 10:52:26 +0200327 rc = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200328 touched = 1;
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Thomas Gleixner4e77ae32007-10-12 23:04:07 +0200331 sum = read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
Andi Kleen75152112005-05-16 21:53:34 -0700332 if (__get_cpu_var(nmi_touch)) {
333 __get_cpu_var(nmi_touch) = 0;
334 touched = 1;
335 }
Don Zickusf2802e72006-09-26 10:52:26 +0200336
Andrew Mortonbb81a092006-12-07 02:14:01 +0100337 if (cpu_isset(cpu, backtrace_mask)) {
338 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
339
340 spin_lock(&lock);
341 printk("NMI backtrace for cpu %d\n", cpu);
342 dump_stack();
343 spin_unlock(&lock);
344 cpu_clear(cpu, backtrace_mask);
345 }
346
Andi Kleen553f2652006-04-07 19:49:57 +0200347#ifdef CONFIG_X86_MCE
348 /* Could check oops_in_progress here too, but it's safer
349 not too */
350 if (atomic_read(&mce_entry) > 0)
351 touched = 1;
352#endif
Don Zickusf2802e72006-09-26 10:52:26 +0200353 /* if the apic timer isn't firing, this cpu isn't doing much */
Andi Kleen75152112005-05-16 21:53:34 -0700354 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /*
356 * Ayiee, looks like this CPU is stuck ...
357 * wait a few IRQs (5 seconds) before doing the oops ...
358 */
Andi Kleen75152112005-05-16 21:53:34 -0700359 local_inc(&__get_cpu_var(alert_counter));
Don Zickusf2802e72006-09-26 10:52:26 +0200360 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
Andi Kleenfac58552006-09-26 10:52:27 +0200361 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
362 panic_on_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 } else {
Andi Kleen75152112005-05-16 21:53:34 -0700364 __get_cpu_var(last_irq_sum) = sum;
365 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Don Zickusf2802e72006-09-26 10:52:26 +0200367
368 /* see if the nmi watchdog went off */
Andi Kleen05cb0072007-05-02 19:27:20 +0200369 if (!__get_cpu_var(wd_enabled))
370 return rc;
371 switch (nmi_watchdog) {
372 case NMI_LOCAL_APIC:
373 rc |= lapic_wd_event(nmi_hz);
374 break;
375 case NMI_IO_APIC:
376 /* don't know how to accurately check for this.
377 * just assume it was a watchdog timer interrupt
378 * This matches the old behaviour.
379 */
380 rc = 1;
381 break;
Andi Kleen75152112005-05-16 21:53:34 -0700382 }
Don Zickus3adbbcce2006-09-26 10:52:26 +0200383 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Andi Kleen8f4e9562007-07-22 11:12:32 +0200386static unsigned ignore_nmis;
387
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100388asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 nmi_enter();
391 add_pda(__nmi_count,1);
Andi Kleen8f4e9562007-07-22 11:12:32 +0200392 if (!ignore_nmis)
393 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 nmi_exit();
395}
396
Don Zickus3adbbcce2006-09-26 10:52:26 +0200397int do_nmi_callback(struct pt_regs * regs, int cpu)
398{
Don Zickus2fbe7b22006-09-26 10:52:27 +0200399#ifdef CONFIG_SYSCTL
400 if (unknown_nmi_panic)
401 return unknown_nmi_panic_callback(regs, cpu);
402#endif
403 return 0;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200404}
405
Andi Kleen8f4e9562007-07-22 11:12:32 +0200406void stop_nmi(void)
407{
408 acpi_nmi_disable();
409 ignore_nmis++;
410}
411
412void restart_nmi(void)
413{
414 ignore_nmis--;
415 acpi_nmi_enable();
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#ifdef CONFIG_SYSCTL
419
420static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
421{
422 unsigned char reason = get_nmi_reason();
423 char buf[64];
424
Don Zickus2fbe7b22006-09-26 10:52:27 +0200425 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Andi Kleenfac58552006-09-26 10:52:27 +0200426 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428}
429
Don Zickus407984f2006-09-26 10:52:27 +0200430/*
431 * proc handler for /proc/sys/kernel/nmi
432 */
433int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
434 void __user *buffer, size_t *length, loff_t *ppos)
435{
436 int old_state;
437
438 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
439 old_state = nmi_watchdog_enabled;
440 proc_dointvec(table, write, file, buffer, length, ppos);
441 if (!!old_state == !!nmi_watchdog_enabled)
442 return 0;
443
Daniel Gollub0328ece2007-08-15 02:40:35 +0200444 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Don Zickus407984f2006-09-26 10:52:27 +0200445 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
Don Zickuse33e89a2006-09-26 10:52:27 +0200446 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200447 }
448
449 /* if nmi_watchdog is not set yet, then set it */
450 nmi_watchdog_default();
451
Don Zickuse33e89a2006-09-26 10:52:27 +0200452 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200453 if (nmi_watchdog_enabled)
454 enable_lapic_nmi_watchdog();
455 else
456 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200457 } else {
Don Zickuse33e89a2006-09-26 10:52:27 +0200458 printk( KERN_WARNING
Don Zickus407984f2006-09-26 10:52:27 +0200459 "NMI watchdog doesn't know what hardware to touch\n");
460 return -EIO;
461 }
462 return 0;
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#endif
466
Andrew Mortonbb81a092006-12-07 02:14:01 +0100467void __trigger_all_cpu_backtrace(void)
468{
469 int i;
470
471 backtrace_mask = cpu_online_map;
472 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
473 for (i = 0; i < 10 * 1000; i++) {
474 if (cpus_empty(backtrace_mask))
475 break;
476 mdelay(1);
477 }
478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480EXPORT_SYMBOL(nmi_active);
481EXPORT_SYMBOL(nmi_watchdog);