blob: 9a4fde74bee119f393a6d05c7207f9a23531a4d1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* Run after command line and cpu_init init, but before all other checks */
Don Zickuse33e89a2006-09-26 10:52:27 +020050void nmi_watchdog_default(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 if (nmi_watchdog != NMI_DEFAULT)
53 return;
Linus Torvalds8ce5e3e2007-03-14 17:50:48 -070054 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010057static int endflag __initdata = 0;
58
Andi Kleen75152112005-05-16 21:53:34 -070059#ifdef CONFIG_SMP
60/* The performance counters used by NMI_LOCAL_APIC don't trigger when
61 * the CPU is idle. To make sure the NMI watchdog really ticks on all
62 * CPUs during the test make them busy.
63 */
64static __init void nmi_cpu_busy(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Ingo Molnar366c7f52006-07-03 00:25:25 -070066 local_irq_enable_in_hardirq();
Andi Kleen75152112005-05-16 21:53:34 -070067 /* Intentionally don't use cpu_relax here. This is
68 to make sure that the performance counter really ticks,
69 even if there is a simulator or similar that catches the
70 pause instruction. On a real HT machine this is fine because
71 all other CPUs are busy with "useless" delay loops and don't
72 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010073 while (endflag == 0)
74 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Andi Kleen75152112005-05-16 21:53:34 -070076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010078int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010080 int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int cpu;
82
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010083 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
Don Zickusf2802e72006-09-26 10:52:26 +020084 return 0;
85
86 if (!atomic_read(&nmi_active))
87 return 0;
88
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010089 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
90 if (!prev_nmi_count)
Andi Kleen75152112005-05-16 21:53:34 -070091 return -1;
Jack F Vogel67701ae2005-05-01 08:58:48 -070092
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010093 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Andi Kleen7554c3f2006-01-11 22:45:45 +010095#ifdef CONFIG_SMP
Andi Kleen75152112005-05-16 21:53:34 -070096 if (nmi_watchdog == NMI_LOCAL_APIC)
97 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Andi Kleen7554c3f2006-01-11 22:45:45 +010098#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 for (cpu = 0; cpu < NR_CPUS; cpu++)
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100101 prev_nmi_count[cpu] = cpu_pda(cpu)->__nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 local_irq_enable();
Andi Kleen0fb2ebf2007-04-02 12:14:12 +0200103 mdelay((20*1000)/nmi_hz); // wait 20 ticks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Andrew Morton394e3902006-03-23 03:01:05 -0800105 for_each_online_cpu(cpu) {
Andi Kleen05cb0072007-05-02 19:27:20 +0200106 if (!per_cpu(wd_enabled, cpu))
Don Zickusf2802e72006-09-26 10:52:26 +0200107 continue;
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100108 if (cpu_pda(cpu)->__nmi_count - prev_nmi_count[cpu] <= 5) {
Don Zickus75bc1222007-12-04 17:19:07 +0100109 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
110 "appears to be stuck (%d->%d)!\n",
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100111 cpu,
112 prev_nmi_count[cpu],
113 cpu_pda(cpu)->__nmi_count);
Andi Kleen05cb0072007-05-02 19:27:20 +0200114 per_cpu(wd_enabled, cpu) = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200115 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117 }
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100118 endflag = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200119 if (!atomic_read(&nmi_active)) {
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100120 kfree(prev_nmi_count);
Don Zickusf2802e72006-09-26 10:52:26 +0200121 atomic_set(&nmi_active, -1);
122 return -1;
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 printk("OK.\n");
125
126 /* now that we know it works we can reduce NMI frequency to
127 something more reasonable; makes a difference in some configs */
Andi Kleen05cb0072007-05-02 19:27:20 +0200128 if (nmi_watchdog == NMI_LOCAL_APIC)
129 nmi_hz = lapic_adjust_nmi_hz(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100131 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 0;
133}
134
Adrian Bunk867ab542008-01-30 13:30:31 +0100135static int __init setup_nmi_watchdog(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 int nmi;
138
139 if (!strncmp(str,"panic",5)) {
140 panic_on_timeout = 1;
141 str = strchr(str, ',');
142 if (!str)
143 return 1;
144 ++str;
145 }
146
147 get_option(&str, &nmi);
148
Don Zickusf2802e72006-09-26 10:52:26 +0200149 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200151
Andi Kleen75152112005-05-16 21:53:34 -0700152 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 1;
154}
155
156__setup("nmi_watchdog=", setup_nmi_watchdog);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#ifdef CONFIG_PM
159
160static int nmi_pm_active; /* nmi_active before suspend */
161
Pavel Machek829ca9a2005-09-03 15:56:56 -0700162static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Shaohua Li4038f902006-09-26 10:52:27 +0200164 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusf2802e72006-09-26 10:52:26 +0200165 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200166 stop_apic_nmi_watchdog(NULL);
167 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return 0;
169}
170
171static int lapic_nmi_resume(struct sys_device *dev)
172{
Shaohua Li4038f902006-09-26 10:52:27 +0200173 /* only CPU0 goes here, other CPUs should be offline */
174 if (nmi_pm_active > 0) {
175 setup_apic_nmi_watchdog(NULL);
176 touch_nmi_watchdog();
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return 0;
179}
180
181static struct sysdev_class nmi_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100182 .name = "lapic_nmi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 .resume = lapic_nmi_resume,
184 .suspend = lapic_nmi_suspend,
185};
186
187static struct sys_device device_lapic_nmi = {
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100188 .id = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .cls = &nmi_sysclass,
190};
191
192static int __init init_lapic_nmi_sysfs(void)
193{
194 int error;
195
Don Zickusf2802e72006-09-26 10:52:26 +0200196 /* should really be a BUG_ON but b/c this is an
197 * init call, it just doesn't work. -dcz
198 */
199 if (nmi_watchdog != NMI_LOCAL_APIC)
200 return 0;
201
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100202 if (atomic_read(&nmi_active) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return 0;
204
205 error = sysdev_class_register(&nmi_sysclass);
206 if (!error)
207 error = sysdev_register(&device_lapic_nmi);
208 return error;
209}
210/* must come after the local APIC's device_initcall() */
211late_initcall(init_lapic_nmi_sysfs);
212
213#endif /* CONFIG_PM */
214
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100215static void __acpi_nmi_enable(void *__unused)
216{
217 apic_write(APIC_LVT0, APIC_DM_NMI);
218}
219
220/*
221 * Enable timer based NMIs on all CPUs:
222 */
223void acpi_nmi_enable(void)
224{
225 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
226 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
227}
228
229static void __acpi_nmi_disable(void *__unused)
230{
231 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
232}
233
234/*
235 * Disable timer based NMIs on all CPUs:
236 */
237void acpi_nmi_disable(void)
238{
239 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
240 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
241}
242
Don Zickusf2802e72006-09-26 10:52:26 +0200243void setup_apic_nmi_watchdog(void *unused)
244{
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100245 if (__get_cpu_var(wd_enabled))
Shaohua Li4038f902006-09-26 10:52:27 +0200246 return;
247
248 /* cheap hack to support suspend/resume */
249 /* if cpu0 is not active neither should the other cpus */
250 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
251 return;
252
Andi Kleen05cb0072007-05-02 19:27:20 +0200253 switch (nmi_watchdog) {
254 case NMI_LOCAL_APIC:
255 __get_cpu_var(wd_enabled) = 1;
256 if (lapic_watchdog_init(nmi_hz) < 0) {
257 __get_cpu_var(wd_enabled) = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200258 return;
259 }
Andi Kleen05cb0072007-05-02 19:27:20 +0200260 /* FALL THROUGH */
261 case NMI_IO_APIC:
262 __get_cpu_var(wd_enabled) = 1;
263 atomic_inc(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Don Zickusf2802e72006-09-26 10:52:26 +0200265}
266
Shaohua Li4038f902006-09-26 10:52:27 +0200267void stop_apic_nmi_watchdog(void *unused)
Don Zickusf2802e72006-09-26 10:52:26 +0200268{
269 /* only support LOCAL and IO APICs for now */
270 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
271 (nmi_watchdog != NMI_IO_APIC))
272 return;
Andi Kleen05cb0072007-05-02 19:27:20 +0200273 if (__get_cpu_var(wd_enabled) == 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200274 return;
Andi Kleen05cb0072007-05-02 19:27:20 +0200275 if (nmi_watchdog == NMI_LOCAL_APIC)
276 lapic_watchdog_stop();
277 __get_cpu_var(wd_enabled) = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200278 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/*
282 * the best way to detect whether a CPU has a 'hard lockup' problem
283 * is to check it's local APIC timer IRQ counts. If they are not
284 * changing then that CPU has some problem.
285 *
286 * as these watchdog NMI IRQs are generated on every CPU, we only
287 * have to check the current processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289
Andi Kleen75152112005-05-16 21:53:34 -0700290static DEFINE_PER_CPU(unsigned, last_irq_sum);
291static DEFINE_PER_CPU(local_t, alert_counter);
292static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Andrew Morton567f3e42007-07-17 04:03:58 -0700294void touch_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Jan Beulich99019e92006-02-16 23:41:55 +0100296 if (nmi_watchdog > 0) {
297 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Jan Beulich99019e92006-02-16 23:41:55 +0100299 /*
300 * Tell other CPUs to reset their alert counters. We cannot
301 * do it ourselves because the alert count increase is not
302 * atomic.
303 */
Andrew Morton567f3e42007-07-17 04:03:58 -0700304 for_each_present_cpu(cpu) {
305 if (per_cpu(nmi_touch, cpu) != 1)
306 per_cpu(nmi_touch, cpu) = 1;
307 }
Jan Beulich99019e92006-02-16 23:41:55 +0100308 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700309
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100310 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100312EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Don Zickus3adbbcc2006-09-26 10:52:26 +0200314int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Andi Kleen75152112005-05-16 21:53:34 -0700316 int sum;
317 int touched = 0;
Andrew Mortonbb81a092006-12-07 02:14:01 +0100318 int cpu = smp_processor_id();
Andi Kleen05cb0072007-05-02 19:27:20 +0200319 int rc = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200320
321 /* check for other users first */
322 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
323 == NOTIFY_STOP) {
Don Zickus3adbbcc2006-09-26 10:52:26 +0200324 rc = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200325 touched = 1;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Thomas Gleixner4e77ae32007-10-12 23:04:07 +0200328 sum = read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
Andi Kleen75152112005-05-16 21:53:34 -0700329 if (__get_cpu_var(nmi_touch)) {
330 __get_cpu_var(nmi_touch) = 0;
331 touched = 1;
332 }
Don Zickusf2802e72006-09-26 10:52:26 +0200333
Andrew Mortonbb81a092006-12-07 02:14:01 +0100334 if (cpu_isset(cpu, backtrace_mask)) {
335 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
336
337 spin_lock(&lock);
338 printk("NMI backtrace for cpu %d\n", cpu);
339 dump_stack();
340 spin_unlock(&lock);
341 cpu_clear(cpu, backtrace_mask);
342 }
343
Andi Kleen553f2652006-04-07 19:49:57 +0200344#ifdef CONFIG_X86_MCE
345 /* Could check oops_in_progress here too, but it's safer
346 not too */
347 if (atomic_read(&mce_entry) > 0)
348 touched = 1;
349#endif
Don Zickusf2802e72006-09-26 10:52:26 +0200350 /* if the apic timer isn't firing, this cpu isn't doing much */
Andi Kleen75152112005-05-16 21:53:34 -0700351 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /*
353 * Ayiee, looks like this CPU is stuck ...
354 * wait a few IRQs (5 seconds) before doing the oops ...
355 */
Andi Kleen75152112005-05-16 21:53:34 -0700356 local_inc(&__get_cpu_var(alert_counter));
Don Zickusf2802e72006-09-26 10:52:26 +0200357 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
Andi Kleenfac58552006-09-26 10:52:27 +0200358 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
359 panic_on_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 } else {
Andi Kleen75152112005-05-16 21:53:34 -0700361 __get_cpu_var(last_irq_sum) = sum;
362 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Don Zickusf2802e72006-09-26 10:52:26 +0200364
365 /* see if the nmi watchdog went off */
Andi Kleen05cb0072007-05-02 19:27:20 +0200366 if (!__get_cpu_var(wd_enabled))
367 return rc;
368 switch (nmi_watchdog) {
369 case NMI_LOCAL_APIC:
370 rc |= lapic_wd_event(nmi_hz);
371 break;
372 case NMI_IO_APIC:
373 /* don't know how to accurately check for this.
374 * just assume it was a watchdog timer interrupt
375 * This matches the old behaviour.
376 */
377 rc = 1;
378 break;
Andi Kleen75152112005-05-16 21:53:34 -0700379 }
Don Zickus3adbbcc2006-09-26 10:52:26 +0200380 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Andi Kleen8f4e9562007-07-22 11:12:32 +0200383static unsigned ignore_nmis;
384
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100385asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 nmi_enter();
388 add_pda(__nmi_count,1);
Andi Kleen8f4e9562007-07-22 11:12:32 +0200389 if (!ignore_nmis)
390 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 nmi_exit();
392}
393
Andi Kleen8f4e9562007-07-22 11:12:32 +0200394void stop_nmi(void)
395{
396 acpi_nmi_disable();
397 ignore_nmis++;
398}
399
400void restart_nmi(void)
401{
402 ignore_nmis--;
403 acpi_nmi_enable();
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#ifdef CONFIG_SYSCTL
407
408static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
409{
410 unsigned char reason = get_nmi_reason();
411 char buf[64];
412
Don Zickus2fbe7b22006-09-26 10:52:27 +0200413 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Andi Kleenfac58552006-09-26 10:52:27 +0200414 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
416}
417
Don Zickus407984f2006-09-26 10:52:27 +0200418/*
419 * proc handler for /proc/sys/kernel/nmi
420 */
421int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
422 void __user *buffer, size_t *length, loff_t *ppos)
423{
424 int old_state;
425
426 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
427 old_state = nmi_watchdog_enabled;
428 proc_dointvec(table, write, file, buffer, length, ppos);
429 if (!!old_state == !!nmi_watchdog_enabled)
430 return 0;
431
Daniel Gollub0328ece2007-08-15 02:40:35 +0200432 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Don Zickus407984f2006-09-26 10:52:27 +0200433 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
Don Zickuse33e89a2006-09-26 10:52:27 +0200434 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200435 }
436
437 /* if nmi_watchdog is not set yet, then set it */
438 nmi_watchdog_default();
439
Don Zickuse33e89a2006-09-26 10:52:27 +0200440 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200441 if (nmi_watchdog_enabled)
442 enable_lapic_nmi_watchdog();
443 else
444 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200445 } else {
Don Zickuse33e89a2006-09-26 10:52:27 +0200446 printk( KERN_WARNING
Don Zickus407984f2006-09-26 10:52:27 +0200447 "NMI watchdog doesn't know what hardware to touch\n");
448 return -EIO;
449 }
450 return 0;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453#endif
454
Li Zefana062bae2008-02-03 15:40:30 +0800455int do_nmi_callback(struct pt_regs *regs, int cpu)
456{
457#ifdef CONFIG_SYSCTL
458 if (unknown_nmi_panic)
459 return unknown_nmi_panic_callback(regs, cpu);
460#endif
461 return 0;
462}
463
Andrew Mortonbb81a092006-12-07 02:14:01 +0100464void __trigger_all_cpu_backtrace(void)
465{
466 int i;
467
468 backtrace_mask = cpu_online_map;
469 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
470 for (i = 0; i < 10 * 1000; i++) {
471 if (cpus_empty(backtrace_mask))
472 break;
473 mdelay(1);
474 }
475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477EXPORT_SYMBOL(nmi_active);
478EXPORT_SYMBOL(nmi_watchdog);