aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel/nmi_32.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/kernel/nmi_32.c')
-rw-r--r--arch/x86/kernel/nmi_32.c476
1 files changed, 0 insertions, 476 deletions
diff --git a/arch/x86/kernel/nmi_32.c b/arch/x86/kernel/nmi_32.c
deleted file mode 100644
index 6580dae46277..000000000000
--- a/arch/x86/kernel/nmi_32.c
+++ /dev/null
@@ -1,476 +0,0 @@
1/*
2 * 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 * Mikael Pettersson : Pentium 4 support for local APIC NMI watchdog.
10 * Pavel Machek and
11 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
12 */
13
14#include <linux/delay.h>
15#include <linux/interrupt.h>
16#include <linux/module.h>
17#include <linux/nmi.h>
18#include <linux/sysdev.h>
19#include <linux/sysctl.h>
20#include <linux/percpu.h>
21#include <linux/kprobes.h>
22#include <linux/cpumask.h>
23#include <linux/kernel_stat.h>
24#include <linux/kdebug.h>
25#include <linux/slab.h>
26
27#include <asm/i8259.h>
28#include <asm/io_apic.h>
29#include <asm/smp.h>
30#include <asm/nmi.h>
31#include <asm/timer.h>
32
33#include "mach_traps.h"
34
35int unknown_nmi_panic;
36int nmi_watchdog_enabled;
37
38static cpumask_t backtrace_mask = CPU_MASK_NONE;
39
40/* nmi_active:
41 * >0: the lapic NMI watchdog is active, but can be disabled
42 * <0: the lapic NMI watchdog has not been set up, and cannot
43 * be enabled
44 * 0: the lapic NMI watchdog is disabled, but can be enabled
45 */
46atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
47
48unsigned int nmi_watchdog = NMI_DEFAULT;
49static unsigned int nmi_hz = HZ;
50
51static DEFINE_PER_CPU(short, wd_enabled);
52
53static int endflag __initdata = 0;
54
55#ifdef CONFIG_SMP
56/* The performance counters used by NMI_LOCAL_APIC don't trigger when
57 * the CPU is idle. To make sure the NMI watchdog really ticks on all
58 * CPUs during the test make them busy.
59 */
60static __init void nmi_cpu_busy(void *data)
61{
62 local_irq_enable_in_hardirq();
63 /* Intentionally don't use cpu_relax here. This is
64 to make sure that the performance counter really ticks,
65 even if there is a simulator or similar that catches the
66 pause instruction. On a real HT machine this is fine because
67 all other CPUs are busy with "useless" delay loops and don't
68 care if they get somewhat less cycles. */
69 while (endflag == 0)
70 mb();
71}
72#endif
73
74int __init check_nmi_watchdog(void)
75{
76 unsigned int *prev_nmi_count;
77 int cpu;
78
79 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
80 return 0;
81
82 if (!atomic_read(&nmi_active))
83 return 0;
84
85 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
86 if (!prev_nmi_count)
87 goto error;
88
89 printk(KERN_INFO "Testing NMI watchdog ... ");
90
91#ifdef CONFIG_SMP
92 if (nmi_watchdog == NMI_LOCAL_APIC)
93 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
94#endif
95
96 for_each_possible_cpu(cpu)
97 prev_nmi_count[cpu] = nmi_count(cpu);
98 local_irq_enable();
99 mdelay((20*1000)/nmi_hz); // wait 20 ticks
100
101 for_each_possible_cpu(cpu) {
102#ifdef CONFIG_SMP
103 /* Check cpu_callin_map here because that is set
104 after the timer is started. */
105 if (!cpu_isset(cpu, cpu_callin_map))
106 continue;
107#endif
108 if (!per_cpu(wd_enabled, cpu))
109 continue;
110 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
111 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
112 "appears to be stuck (%d->%d)!\n",
113 cpu,
114 prev_nmi_count[cpu],
115 nmi_count(cpu));
116 per_cpu(wd_enabled, cpu) = 0;
117 atomic_dec(&nmi_active);
118 }
119 }
120 endflag = 1;
121 if (!atomic_read(&nmi_active)) {
122 kfree(prev_nmi_count);
123 atomic_set(&nmi_active, -1);
124 goto error;
125 }
126 printk("OK.\n");
127
128 /* now that we know it works we can reduce NMI frequency to
129 something more reasonable; makes a difference in some configs */
130 if (nmi_watchdog == NMI_LOCAL_APIC)
131 nmi_hz = lapic_adjust_nmi_hz(1);
132
133 kfree(prev_nmi_count);
134 return 0;
135error:
136 if (nmi_watchdog == NMI_IO_APIC && !timer_through_8259)
137 disable_8259A_irq(0);
138 timer_ack = 0;
139
140 return -1;
141}
142
143static int __init setup_nmi_watchdog(char *str)
144{
145 int nmi;
146
147 get_option(&str, &nmi);
148
149 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
150 return 0;
151
152 nmi_watchdog = nmi;
153 return 1;
154}
155
156__setup("nmi_watchdog=", setup_nmi_watchdog);
157
158
159/* Suspend/resume support */
160
161#ifdef CONFIG_PM
162
163static int nmi_pm_active; /* nmi_active before suspend */
164
165static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
166{
167 /* only CPU0 goes here, other CPUs should be offline */
168 nmi_pm_active = atomic_read(&nmi_active);
169 stop_apic_nmi_watchdog(NULL);
170 BUG_ON(atomic_read(&nmi_active) != 0);
171 return 0;
172}
173
174static int lapic_nmi_resume(struct sys_device *dev)
175{
176 /* 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 }
181 return 0;
182}
183
184
185static struct sysdev_class nmi_sysclass = {
186 .name = "lapic_nmi",
187 .resume = lapic_nmi_resume,
188 .suspend = lapic_nmi_suspend,
189};
190
191static struct sys_device device_lapic_nmi = {
192 .id = 0,
193 .cls = &nmi_sysclass,
194};
195
196static int __init init_lapic_nmi_sysfs(void)
197{
198 int error;
199
200 /* should really be a BUG_ON but b/c this is an
201 * init call, it just doesn't work. -dcz
202 */
203 if (nmi_watchdog != NMI_LOCAL_APIC)
204 return 0;
205
206 if (atomic_read(&nmi_active) < 0)
207 return 0;
208
209 error = sysdev_class_register(&nmi_sysclass);
210 if (!error)
211 error = sysdev_register(&device_lapic_nmi);
212 return error;
213}
214/* must come after the local APIC's device_initcall() */
215late_initcall(init_lapic_nmi_sysfs);
216
217#endif /* CONFIG_PM */
218
219static void __acpi_nmi_enable(void *__unused)
220{
221 apic_write_around(APIC_LVT0, APIC_DM_NMI);
222}
223
224/*
225 * Enable timer based NMIs on all CPUs:
226 */
227void acpi_nmi_enable(void)
228{
229 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
230 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
231}
232
233static void __acpi_nmi_disable(void *__unused)
234{
235 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
236}
237
238/*
239 * Disable timer based NMIs on all CPUs:
240 */
241void acpi_nmi_disable(void)
242{
243 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
244 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
245}
246
247void setup_apic_nmi_watchdog(void *unused)
248{
249 if (__get_cpu_var(wd_enabled))
250 return;
251
252 /* cheap hack to support suspend/resume */
253 /* if cpu0 is not active neither should the other cpus */
254 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
255 return;
256
257 switch (nmi_watchdog) {
258 case NMI_LOCAL_APIC:
259 __get_cpu_var(wd_enabled) = 1; /* enable it before to avoid race with handler */
260 if (lapic_watchdog_init(nmi_hz) < 0) {
261 __get_cpu_var(wd_enabled) = 0;
262 return;
263 }
264 /* FALL THROUGH */
265 case NMI_IO_APIC:
266 __get_cpu_var(wd_enabled) = 1;
267 atomic_inc(&nmi_active);
268 }
269}
270
271void stop_apic_nmi_watchdog(void *unused)
272{
273 /* only support LOCAL and IO APICs for now */
274 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
275 (nmi_watchdog != NMI_IO_APIC))
276 return;
277 if (__get_cpu_var(wd_enabled) == 0)
278 return;
279 if (nmi_watchdog == NMI_LOCAL_APIC)
280 lapic_watchdog_stop();
281 __get_cpu_var(wd_enabled) = 0;
282 atomic_dec(&nmi_active);
283}
284
285/*
286 * the best way to detect whether a CPU has a 'hard lockup' problem
287 * is to check it's local APIC timer IRQ counts. If they are not
288 * changing then that CPU has some problem.
289 *
290 * as these watchdog NMI IRQs are generated on every CPU, we only
291 * have to check the current processor.
292 *
293 * since NMIs don't listen to _any_ locks, we have to be extremely
294 * careful not to rely on unsafe variables. The printk might lock
295 * up though, so we have to break up any console locks first ...
296 * [when there will be more tty-related locks, break them up
297 * here too!]
298 */
299
300static unsigned int
301 last_irq_sums [NR_CPUS],
302 alert_counter [NR_CPUS];
303
304void touch_nmi_watchdog(void)
305{
306 if (nmi_watchdog > 0) {
307 unsigned cpu;
308
309 /*
310 * Just reset the alert counters, (other CPUs might be
311 * spinning on locks we hold):
312 */
313 for_each_present_cpu(cpu) {
314 if (alert_counter[cpu])
315 alert_counter[cpu] = 0;
316 }
317 }
318
319 /*
320 * Tickle the softlockup detector too:
321 */
322 touch_softlockup_watchdog();
323}
324EXPORT_SYMBOL(touch_nmi_watchdog);
325
326extern void die_nmi(struct pt_regs *, const char *msg);
327
328notrace __kprobes int
329nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
330{
331
332 /*
333 * Since current_thread_info()-> is always on the stack, and we
334 * always switch the stack NMI-atomically, it's safe to use
335 * smp_processor_id().
336 */
337 unsigned int sum;
338 int touched = 0;
339 int cpu = smp_processor_id();
340 int rc = 0;
341
342 /* check for other users first */
343 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
344 == NOTIFY_STOP) {
345 rc = 1;
346 touched = 1;
347 }
348
349 if (cpu_isset(cpu, backtrace_mask)) {
350 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
351
352 spin_lock(&lock);
353 printk("NMI backtrace for cpu %d\n", cpu);
354 dump_stack();
355 spin_unlock(&lock);
356 cpu_clear(cpu, backtrace_mask);
357 }
358
359 /*
360 * Take the local apic timer and PIT/HPET into account. We don't
361 * know which one is active, when we have highres/dyntick on
362 */
363 sum = per_cpu(irq_stat, cpu).apic_timer_irqs +
364 per_cpu(irq_stat, cpu).irq0_irqs;
365
366 /* if the none of the timers isn't firing, this cpu isn't doing much */
367 if (!touched && last_irq_sums[cpu] == sum) {
368 /*
369 * Ayiee, looks like this CPU is stuck ...
370 * wait a few IRQs (5 seconds) before doing the oops ...
371 */
372 alert_counter[cpu]++;
373 if (alert_counter[cpu] == 5*nmi_hz)
374 /*
375 * die_nmi will return ONLY if NOTIFY_STOP happens..
376 */
377 die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP");
378 } else {
379 last_irq_sums[cpu] = sum;
380 alert_counter[cpu] = 0;
381 }
382 /* see if the nmi watchdog went off */
383 if (!__get_cpu_var(wd_enabled))
384 return rc;
385 switch (nmi_watchdog) {
386 case NMI_LOCAL_APIC:
387 rc |= lapic_wd_event(nmi_hz);
388 break;
389 case NMI_IO_APIC:
390 /* don't know how to accurately check for this.
391 * just assume it was a watchdog timer interrupt
392 * This matches the old behaviour.
393 */
394 rc = 1;
395 break;
396 }
397 return rc;
398}
399
400#ifdef CONFIG_SYSCTL
401
402static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
403{
404 unsigned char reason = get_nmi_reason();
405 char buf[64];
406
407 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
408 die_nmi(regs, buf);
409 return 0;
410}
411
412/*
413 * proc handler for /proc/sys/kernel/nmi
414 */
415int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
416 void __user *buffer, size_t *length, loff_t *ppos)
417{
418 int old_state;
419
420 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
421 old_state = nmi_watchdog_enabled;
422 proc_dointvec(table, write, file, buffer, length, ppos);
423 if (!!old_state == !!nmi_watchdog_enabled)
424 return 0;
425
426 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
427 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
428 return -EIO;
429 }
430
431 if (nmi_watchdog == NMI_DEFAULT) {
432 if (lapic_watchdog_ok())
433 nmi_watchdog = NMI_LOCAL_APIC;
434 else
435 nmi_watchdog = NMI_IO_APIC;
436 }
437
438 if (nmi_watchdog == NMI_LOCAL_APIC) {
439 if (nmi_watchdog_enabled)
440 enable_lapic_nmi_watchdog();
441 else
442 disable_lapic_nmi_watchdog();
443 } else {
444 printk( KERN_WARNING
445 "NMI watchdog doesn't know what hardware to touch\n");
446 return -EIO;
447 }
448 return 0;
449}
450
451#endif
452
453int do_nmi_callback(struct pt_regs *regs, int cpu)
454{
455#ifdef CONFIG_SYSCTL
456 if (unknown_nmi_panic)
457 return unknown_nmi_panic_callback(regs, cpu);
458#endif
459 return 0;
460}
461
462void __trigger_all_cpu_backtrace(void)
463{
464 int i;
465
466 backtrace_mask = cpu_online_map;
467 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
468 for (i = 0; i < 10 * 1000; i++) {
469 if (cpus_empty(backtrace_mask))
470 break;
471 mdelay(1);
472 }
473}
474
475EXPORT_SYMBOL(nmi_active);
476EXPORT_SYMBOL(nmi_watchdog);