x86-64: clean up linker script
[linux-2.6] / arch / x86 / kernel / nmi_64.c
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  *  Pavel Machek and
10  *  Mikael Pettersson   : PM converted to driver model. Disable/enable API.
11  */
12
13 #include <linux/nmi.h>
14 #include <linux/mm.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/sysdev.h>
19 #include <linux/sysctl.h>
20 #include <linux/kprobes.h>
21 #include <linux/cpumask.h>
22 #include <linux/kdebug.h>
23
24 #include <asm/smp.h>
25 #include <asm/nmi.h>
26 #include <asm/proto.h>
27 #include <asm/mce.h>
28
29 int unknown_nmi_panic;
30 int nmi_watchdog_enabled;
31 int panic_on_unrecovered_nmi;
32
33 static cpumask_t backtrace_mask = CPU_MASK_NONE;
34
35 /* nmi_active:
36  * >0: the lapic NMI watchdog is active, but can be disabled
37  * <0: the lapic NMI watchdog has not been set up, and cannot
38  *     be enabled
39  *  0: the lapic NMI watchdog is disabled, but can be enabled
40  */
41 atomic_t nmi_active = ATOMIC_INIT(0);           /* oprofile uses this */
42 static int panic_on_timeout;
43
44 unsigned int nmi_watchdog = NMI_DEFAULT;
45 static unsigned int nmi_hz = HZ;
46
47 static DEFINE_PER_CPU(short, wd_enabled);
48
49 /* local prototypes */
50 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
51
52 /* Run after command line and cpu_init init, but before all other checks */
53 void nmi_watchdog_default(void)
54 {
55         if (nmi_watchdog != NMI_DEFAULT)
56                 return;
57         nmi_watchdog = NMI_NONE;
58 }
59
60 static int endflag __initdata = 0;
61
62 #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  */
67 static __init void nmi_cpu_busy(void *data)
68 {
69         local_irq_enable_in_hardirq();
70         /* 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. */
76         while (endflag == 0)
77                 mb();
78 }
79 #endif
80
81 int __init check_nmi_watchdog(void)
82 {
83         int *prev_nmi_count;
84         int cpu;
85
86         if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
87                 return 0;
88
89         if (!atomic_read(&nmi_active))
90                 return 0;
91
92         prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
93         if (!prev_nmi_count)
94                 return -1;
95
96         printk(KERN_INFO "Testing NMI watchdog ... ");
97
98 #ifdef CONFIG_SMP
99         if (nmi_watchdog == NMI_LOCAL_APIC)
100                 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
101 #endif
102
103         for (cpu = 0; cpu < NR_CPUS; cpu++)
104                 prev_nmi_count[cpu] = cpu_pda(cpu)->__nmi_count;
105         local_irq_enable();
106         mdelay((20*1000)/nmi_hz); // wait 20 ticks
107
108         for_each_online_cpu(cpu) {
109                 if (!per_cpu(wd_enabled, cpu))
110                         continue;
111                 if (cpu_pda(cpu)->__nmi_count - prev_nmi_count[cpu] <= 5) {
112                         printk(KERN_WARNING "WARNING: CPU#%d: NMI "
113                                "appears to be stuck (%d->%d)!\n",
114                                 cpu,
115                                 prev_nmi_count[cpu],
116                                 cpu_pda(cpu)->__nmi_count);
117                         per_cpu(wd_enabled, cpu) = 0;
118                         atomic_dec(&nmi_active);
119                 }
120         }
121         endflag = 1;
122         if (!atomic_read(&nmi_active)) {
123                 kfree(prev_nmi_count);
124                 atomic_set(&nmi_active, -1);
125                 return -1;
126         }
127         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 */
131         if (nmi_watchdog == NMI_LOCAL_APIC)
132                 nmi_hz = lapic_adjust_nmi_hz(1);
133
134         kfree(prev_nmi_count);
135         return 0;
136 }
137
138 static int __init setup_nmi_watchdog(char *str)
139 {
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
152         if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
153                 return 0;
154
155         nmi_watchdog = nmi;
156         return 1;
157 }
158
159 __setup("nmi_watchdog=", setup_nmi_watchdog);
160
161 #ifdef CONFIG_PM
162
163 static int nmi_pm_active; /* nmi_active before suspend */
164
165 static 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
174 static 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 static struct sysdev_class nmi_sysclass = {
185         .name           = "lapic_nmi",
186         .resume         = lapic_nmi_resume,
187         .suspend        = lapic_nmi_suspend,
188 };
189
190 static struct sys_device device_lapic_nmi = {
191         .id     = 0,
192         .cls    = &nmi_sysclass,
193 };
194
195 static int __init init_lapic_nmi_sysfs(void)
196 {
197         int error;
198
199         /* 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
205         if (atomic_read(&nmi_active) < 0)
206                 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() */
214 late_initcall(init_lapic_nmi_sysfs);
215
216 #endif  /* CONFIG_PM */
217
218 static 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  */
226 void 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
232 static 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  */
240 void 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
246 void setup_apic_nmi_watchdog(void *unused)
247 {
248         if (__get_cpu_var(wd_enabled))
249                 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
256         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;
261                         return;
262                 }
263                 /* FALL THROUGH */
264         case NMI_IO_APIC:
265                 __get_cpu_var(wd_enabled) = 1;
266                 atomic_inc(&nmi_active);
267         }
268 }
269
270 void stop_apic_nmi_watchdog(void *unused)
271 {
272         /* only support LOCAL and IO APICs for now */
273         if ((nmi_watchdog != NMI_LOCAL_APIC) &&
274             (nmi_watchdog != NMI_IO_APIC))
275                 return;
276         if (__get_cpu_var(wd_enabled) == 0)
277                 return;
278         if (nmi_watchdog == NMI_LOCAL_APIC)
279                 lapic_watchdog_stop();
280         __get_cpu_var(wd_enabled) = 0;
281         atomic_dec(&nmi_active);
282 }
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.
291  */
292
293 static DEFINE_PER_CPU(unsigned, last_irq_sum);
294 static DEFINE_PER_CPU(local_t, alert_counter);
295 static DEFINE_PER_CPU(int, nmi_touch);
296
297 void touch_nmi_watchdog(void)
298 {
299         if (nmi_watchdog > 0) {
300                 unsigned cpu;
301
302                 /*
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                  */
307                 for_each_present_cpu(cpu) {
308                         if (per_cpu(nmi_touch, cpu) != 1)
309                                 per_cpu(nmi_touch, cpu) = 1;
310                 }
311         }
312
313         touch_softlockup_watchdog();
314 }
315 EXPORT_SYMBOL(touch_nmi_watchdog);
316
317 int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
318 {
319         int sum;
320         int touched = 0;
321         int cpu = smp_processor_id();
322         int rc = 0;
323
324         /* check for other users first */
325         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
326                         == NOTIFY_STOP) {
327                 rc = 1;
328                 touched = 1;
329         }
330
331         sum = read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
332         if (__get_cpu_var(nmi_touch)) {
333                 __get_cpu_var(nmi_touch) = 0;
334                 touched = 1;
335         }
336
337         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
347 #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
353         /* if the apic timer isn't firing, this cpu isn't doing much */
354         if (!touched && __get_cpu_var(last_irq_sum) == sum) {
355                 /*
356                  * Ayiee, looks like this CPU is stuck ...
357                  * wait a few IRQs (5 seconds) before doing the oops ...
358                  */
359                 local_inc(&__get_cpu_var(alert_counter));
360                 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
361                         die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
362                                 panic_on_timeout);
363         } else {
364                 __get_cpu_var(last_irq_sum) = sum;
365                 local_set(&__get_cpu_var(alert_counter), 0);
366         }
367
368         /* see if the nmi watchdog went off */
369         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;
382         }
383         return rc;
384 }
385
386 static unsigned ignore_nmis;
387
388 asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
389 {
390         nmi_enter();
391         add_pda(__nmi_count,1);
392         if (!ignore_nmis)
393                 default_do_nmi(regs);
394         nmi_exit();
395 }
396
397 int do_nmi_callback(struct pt_regs * regs, int cpu)
398 {
399 #ifdef CONFIG_SYSCTL
400         if (unknown_nmi_panic)
401                 return unknown_nmi_panic_callback(regs, cpu);
402 #endif
403         return 0;
404 }
405
406 void stop_nmi(void)
407 {
408         acpi_nmi_disable();
409         ignore_nmis++;
410 }
411
412 void restart_nmi(void)
413 {
414         ignore_nmis--;
415         acpi_nmi_enable();
416 }
417
418 #ifdef CONFIG_SYSCTL
419
420 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
421 {
422         unsigned char reason = get_nmi_reason();
423         char buf[64];
424
425         sprintf(buf, "NMI received for unknown reason %02x\n", reason);
426         die_nmi(buf, regs, 1);  /* Always panic here */
427         return 0;
428 }
429
430 /*
431  * proc handler for /proc/sys/kernel/nmi
432  */
433 int 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
444         if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
445                 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
446                 return -EIO;
447         }
448
449         /* if nmi_watchdog is not set yet, then set it */
450         nmi_watchdog_default();
451
452         if (nmi_watchdog == NMI_LOCAL_APIC) {
453                 if (nmi_watchdog_enabled)
454                         enable_lapic_nmi_watchdog();
455                 else
456                         disable_lapic_nmi_watchdog();
457         } else {
458                 printk( KERN_WARNING
459                         "NMI watchdog doesn't know what hardware to touch\n");
460                 return -EIO;
461         }
462         return 0;
463 }
464
465 #endif
466
467 void __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
480 EXPORT_SYMBOL(nmi_active);
481 EXPORT_SYMBOL(nmi_watchdog);