This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / x86_64 / kernel / mce.c
1 /*
2  * Machine check handler.
3  * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
4  * Rest from unknown author(s). 
5  * 2004 Andi Kleen. Rewrote most of it. 
6  */
7
8 #include <linux/init.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/rcupdate.h>
14 #include <linux/kallsyms.h>
15 #include <linux/sysdev.h>
16 #include <linux/miscdevice.h>
17 #include <linux/fs.h>
18 #include <asm/processor.h> 
19 #include <asm/msr.h>
20 #include <asm/mce.h>
21 #include <asm/kdebug.h>
22 #include <asm/uaccess.h>
23
24 #define MISC_MCELOG_MINOR 227
25 #define NR_BANKS 5
26
27 static int mce_dont_init;
28
29 /* 0: always panic, 1: panic if deadlock possible, 2: try to avoid panic,
30    3: never panic or exit (for testing only) */
31 static int tolerant = 1;
32 static int banks;
33 static unsigned long bank[NR_BANKS] = { [0 ... NR_BANKS-1] = ~0UL };
34
35 /*
36  * Lockless MCE logging infrastructure.
37  * This avoids deadlocks on printk locks without having to break locks. Also
38  * separate MCEs from kernel messages to avoid bogus bug reports.
39  */
40
41 struct mce_log mcelog = { 
42         MCE_LOG_SIGNATURE,
43         MCE_LOG_LEN,
44 }; 
45
46 static void mce_log(struct mce *mce)
47 {
48         unsigned next, entry;
49         mce->finished = 0;
50         smp_wmb();
51         for (;;) {
52                 entry = rcu_dereference(mcelog.next);
53                 /* When the buffer fills up discard new entries. Assume 
54                    that the earlier errors are the more interesting. */
55                 if (entry >= MCE_LOG_LEN) {
56                         set_bit(MCE_OVERFLOW, &mcelog.flags);
57                         return;
58                 }
59                 /* Old left over entry. Skip. */
60                 if (mcelog.entry[entry].finished)
61                         continue;
62                 smp_rmb();
63                 next = entry + 1;
64                 if (cmpxchg(&mcelog.next, entry, next) == entry)
65                         break;
66         }
67         memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
68         smp_wmb();
69         mcelog.entry[entry].finished = 1;
70         smp_wmb();
71 }
72
73 static void print_mce(struct mce *m)
74 {
75         printk(KERN_EMERG "\n"
76                KERN_EMERG
77                "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
78                m->cpu, m->mcgstatus, m->bank, m->status);
79         if (m->rip) {
80                 printk(KERN_EMERG 
81                        "RIP%s %02x:<%016Lx> ",
82                        !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
83                        m->cs, m->rip);
84                 if (m->cs == __KERNEL_CS)
85                         print_symbol("{%s}", m->rip);
86                 printk("\n");
87         }
88         printk(KERN_EMERG "TSC %Lx ", m->tsc); 
89         if (m->addr)
90                 printk("ADDR %Lx ", m->addr);
91         if (m->misc)
92                 printk("MISC %Lx ", m->misc);   
93         printk("\n");
94 }
95
96 static void mce_panic(char *msg, struct mce *backup, unsigned long start)
97
98         int i;
99         oops_begin();
100         for (i = 0; i < MCE_LOG_LEN; i++) {
101                 unsigned long tsc = mcelog.entry[i].tsc;
102                 if (time_before(tsc, start))
103                         continue;
104                 print_mce(&mcelog.entry[i]); 
105                 if (backup && mcelog.entry[i].tsc == backup->tsc)
106                         backup = NULL;
107         }
108         if (backup)
109                 print_mce(backup);
110         if (tolerant >= 3)
111                 printk("Fake panic: %s\n", msg);
112         else
113                 panic(msg);
114
115
116 static int mce_available(struct cpuinfo_x86 *c)
117 {
118         return test_bit(X86_FEATURE_MCE, &c->x86_capability) &&
119                test_bit(X86_FEATURE_MCA, &c->x86_capability);
120 }
121
122 /* 
123  * The actual machine check handler
124  */
125
126 void do_machine_check(struct pt_regs * regs, long error_code)
127 {
128         struct mce m, panicm;
129         int nowayout = (tolerant < 1); 
130         int kill_it = 0;
131         u64 mcestart = 0;
132         int i;
133         int panicm_found = 0;
134
135         if (regs)
136                 notify_die(DIE_NMI, "machine check", regs, error_code, 255, SIGKILL);
137         if (!banks)
138                 return;
139
140         memset(&m, 0, sizeof(struct mce));
141         m.cpu = hard_smp_processor_id();
142         rdmsrl(MSR_IA32_MCG_STATUS, m.mcgstatus);
143         if (!(m.mcgstatus & MCG_STATUS_RIPV))
144                 kill_it = 1;
145         
146         rdtscll(mcestart);
147         barrier();
148
149         for (i = 0; i < banks; i++) {
150                 if (!bank[i])
151                         continue;
152                 
153                 m.misc = 0; 
154                 m.addr = 0;
155                 m.bank = i;
156                 m.tsc = 0;
157
158                 rdmsrl(MSR_IA32_MC0_STATUS + i*4, m.status);
159                 if ((m.status & MCI_STATUS_VAL) == 0)
160                         continue;
161
162                 if (m.status & MCI_STATUS_EN) {
163                         /* In theory _OVER could be a nowayout too, but
164                            assume any overflowed errors were no fatal. */
165                         nowayout |= !!(m.status & MCI_STATUS_PCC);
166                         kill_it |= !!(m.status & MCI_STATUS_UC);
167                 }
168
169                 if (m.status & MCI_STATUS_MISCV)
170                         rdmsrl(MSR_IA32_MC0_MISC + i*4, m.misc);
171                 if (m.status & MCI_STATUS_ADDRV)
172                         rdmsrl(MSR_IA32_MC0_ADDR + i*4, m.addr);
173
174                 if (regs && (m.mcgstatus & MCG_STATUS_RIPV)) {
175                         m.rip = regs->rip;
176                         m.cs = regs->cs;
177                 } else {
178                         m.rip = 0;
179                         m.cs = 0;
180                 }
181
182                 if (error_code != -1)
183                         rdtscll(m.tsc);
184                 wrmsrl(MSR_IA32_MC0_STATUS + i*4, 0);
185                 mce_log(&m);
186
187                 /* Did this bank cause the exception? */
188                 /* Assume that the bank with uncorrectable errors did it,
189                    and that there is only a single one. */
190                 if ((m.status & MCI_STATUS_UC) && (m.status & MCI_STATUS_EN)) {
191                         panicm = m;
192                         panicm_found = 1;
193                 }
194
195                 tainted |= TAINT_MACHINE_CHECK;
196         }
197
198         /* Never do anything final in the polling timer */
199         if (!regs)
200                 goto out;
201
202         /* If we didn't find an uncorrectable error, pick
203            the last one (shouldn't happen, just being safe). */
204         if (!panicm_found)
205                 panicm = m;
206         if (nowayout)
207                 mce_panic("Machine check", &panicm, mcestart);
208         if (kill_it) {
209                 int user_space = 0;
210
211                 if (m.mcgstatus & MCG_STATUS_RIPV)
212                         user_space = panicm.rip && (panicm.cs & 3);
213                 
214                 /* When the machine was in user space and the CPU didn't get
215                    confused it's normally not necessary to panic, unless you 
216                    are paranoid (tolerant == 0)
217
218                    RED-PEN could be more tolerant for MCEs in idle,
219                    but most likely they occur at boot anyways, where
220                    it is best to just halt the machine. */
221                 if ((!user_space && (panic_on_oops || tolerant < 2)) ||
222                     (unsigned)current->pid <= 1)
223                         mce_panic("Uncorrected machine check", &panicm, mcestart);
224
225                 /* do_exit takes an awful lot of locks and has as
226                    slight risk of deadlocking. If you don't want that
227                    don't set tolerant >= 2 */
228                 if (tolerant < 3)
229                         do_exit(SIGBUS);
230         }
231
232  out:
233         /* Last thing done in the machine check exception to clear state. */
234         wrmsrl(MSR_IA32_MCG_STATUS, 0);
235 }
236
237 /*
238  * Periodic polling timer for "silent" machine check errors.
239  */
240
241 static int check_interval = 5 * 60; /* 5 minutes */
242 static void mcheck_timer(void *data);
243 static DECLARE_WORK(mcheck_work, mcheck_timer, NULL);
244
245 static void mcheck_check_cpu(void *info)
246 {
247         if (mce_available(&current_cpu_data))
248                 do_machine_check(NULL, 0);
249 }
250
251 static void mcheck_timer(void *data)
252 {
253         on_each_cpu(mcheck_check_cpu, NULL, 1, 1);
254         schedule_delayed_work(&mcheck_work, check_interval * HZ);
255 }
256
257
258 static __init int periodic_mcheck_init(void)
259
260         if (check_interval)
261                 schedule_delayed_work(&mcheck_work, check_interval*HZ);
262         return 0;
263
264 __initcall(periodic_mcheck_init);
265
266
267 /* 
268  * Initialize Machine Checks for a CPU.
269  */
270 static void mce_init(void *dummy)
271 {
272         u64 cap;
273         int i;
274
275         rdmsrl(MSR_IA32_MCG_CAP, cap);
276         banks = cap & 0xff;
277         if (banks > NR_BANKS) { 
278                 printk(KERN_INFO "MCE: warning: using only %d banks\n", banks);
279                 banks = NR_BANKS; 
280         }
281
282         /* Log the machine checks left over from the previous reset.
283            This also clears all registers */
284         do_machine_check(NULL, -1);
285
286         set_in_cr4(X86_CR4_MCE);
287
288         if (cap & MCG_CTL_P)
289                 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
290
291         for (i = 0; i < banks; i++) {
292                 wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]);
293                 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
294         }       
295 }
296
297 /* Add per CPU specific workarounds here */
298 static void __init mce_cpu_quirks(struct cpuinfo_x86 *c) 
299
300         /* This should be disabled by the BIOS, but isn't always */
301         if (c->x86_vendor == X86_VENDOR_AMD && c->x86 == 15) {
302                 /* disable GART TBL walk error reporting, which trips off 
303                    incorrectly with the IOMMU & 3ware & Cerberus. */
304                 clear_bit(10, &bank[4]);
305         }
306 }                       
307
308 /* 
309  * Called for each booted CPU to set up machine checks.
310  * Must be called with preempt off. 
311  */
312 void __init mcheck_init(struct cpuinfo_x86 *c)
313 {
314         static unsigned long mce_cpus __initdata = 0;
315
316         mce_cpu_quirks(c); 
317
318         if (mce_dont_init ||
319             test_and_set_bit(smp_processor_id(), &mce_cpus) ||
320             !mce_available(c))
321                 return;
322
323         mce_init(NULL);
324 }
325
326 /*
327  * Character device to read and clear the MCE log.
328  */
329
330 static void collect_tscs(void *data) 
331
332         unsigned long *cpu_tsc = (unsigned long *)data;
333         rdtscll(cpu_tsc[smp_processor_id()]);
334
335
336 static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize, loff_t *off)
337 {
338         unsigned long cpu_tsc[NR_CPUS];
339         static DECLARE_MUTEX(mce_read_sem);
340         unsigned next;
341         char __user *buf = ubuf;
342         int i, err;
343
344         down(&mce_read_sem); 
345         next = rcu_dereference(mcelog.next);
346
347         /* Only supports full reads right now */
348         if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) { 
349                 up(&mce_read_sem);
350                 return -EINVAL;
351         }
352
353         err = 0;
354         for (i = 0; i < next; i++) {
355                 if (!mcelog.entry[i].finished)
356                         continue;
357                 smp_rmb();
358                 err |= copy_to_user(buf, mcelog.entry + i, sizeof(struct mce));
359                 buf += sizeof(struct mce); 
360         } 
361
362         memset(mcelog.entry, 0, next * sizeof(struct mce));
363         mcelog.next = 0;
364         smp_wmb(); 
365         
366         synchronize_kernel();   
367
368         /* Collect entries that were still getting written before the synchronize. */
369
370         on_each_cpu(collect_tscs, cpu_tsc, 1, 1);
371         for (i = next; i < MCE_LOG_LEN; i++) { 
372                 if (mcelog.entry[i].finished && 
373                     mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {  
374                         err |= copy_to_user(buf, mcelog.entry+i, sizeof(struct mce));
375                         smp_rmb();
376                         buf += sizeof(struct mce);
377                         memset(&mcelog.entry[i], 0, sizeof(struct mce));
378                 }
379         }       
380         up(&mce_read_sem);
381         return err ? -EFAULT : buf - ubuf; 
382 }
383
384 static int mce_ioctl(struct inode *i, struct file *f,unsigned int cmd, unsigned long arg)
385 {
386         int __user *p = (int __user *)arg;
387         if (!capable(CAP_SYS_ADMIN))
388                 return -EPERM; 
389         switch (cmd) {
390         case MCE_GET_RECORD_LEN: 
391                 return put_user(sizeof(struct mce), p);
392         case MCE_GET_LOG_LEN:
393                 return put_user(MCE_LOG_LEN, p);                
394         case MCE_GETCLEAR_FLAGS: {
395                 unsigned flags;
396                 do { 
397                         flags = mcelog.flags;
398                 } while (cmpxchg(&mcelog.flags, flags, 0) != flags); 
399                 return put_user(flags, p); 
400         }
401         default:
402                 return -ENOTTY; 
403         } 
404 }
405
406 static struct file_operations mce_chrdev_ops = {
407         .read = mce_read,
408         .ioctl = mce_ioctl,
409 };
410
411 static struct miscdevice mce_log_device = {
412         MISC_MCELOG_MINOR,
413         "mcelog",
414         &mce_chrdev_ops,
415 };
416
417 /* 
418  * Old style boot options parsing. Only for compatibility. 
419  */
420
421 static int __init mcheck_disable(char *str)
422 {
423         mce_dont_init = 1;
424         return 0;
425 }
426
427 /* mce=off disables machine check. Note you can reenable it later
428    using sysfs */
429 static int __init mcheck_enable(char *str)
430 {
431         if (!strcmp(str, "off"))
432                 mce_dont_init = 1;
433         else
434                 printk("mce= argument %s ignored. Please use /sys", str); 
435         return 0;
436 }
437
438 __setup("nomce", mcheck_disable);
439 __setup("mce", mcheck_enable);
440
441 /* 
442  * Sysfs support
443  */ 
444
445 /* On resume clear all MCE state. Don't want to see leftovers from the BIOS. */
446 static int mce_resume(struct sys_device *dev)
447 {
448         on_each_cpu(mce_init, NULL, 1, 1);
449         return 0;
450 }
451
452 /* Reinit MCEs after user configuration changes */
453 static void mce_restart(void) 
454
455         if (check_interval)
456                 cancel_delayed_work(&mcheck_work);
457         /* Timer race is harmless here */
458         on_each_cpu(mce_init, NULL, 1, 1);       
459         if (check_interval)
460                 schedule_delayed_work(&mcheck_work, check_interval*HZ);
461 }
462
463 static struct sysdev_class mce_sysclass = {
464         .resume = mce_resume,
465         set_kset_name("machinecheck"),
466 };
467
468 static struct sys_device device_mce = {
469         .id     = 0,
470         .cls    = &mce_sysclass,
471 };
472
473 /* Why are there no generic functions for this? */
474 #define ACCESSOR(name, var, start) \
475         static ssize_t show_ ## name(struct sys_device *s, char *buf) {                    \
476                 return sprintf(buf, "%lx\n", (unsigned long)var);                  \
477         }                                                                          \
478         static ssize_t set_ ## name(struct sys_device *s,const char *buf,size_t siz) { \
479                 char *end;                                                         \
480                 unsigned long new = simple_strtoul(buf, &end, 0);                  \
481                 if (end == buf) return -EINVAL;                                    \
482                 var = new;                                                         \
483                 start;                                                             \
484                 return end-buf;                                                    \
485         }                                                                          \
486         static SYSDEV_ATTR(name, 0644, show_ ## name, set_ ## name);
487
488 ACCESSOR(bank0ctl,bank[0],mce_restart())
489 ACCESSOR(bank1ctl,bank[1],mce_restart())
490 ACCESSOR(bank2ctl,bank[2],mce_restart())
491 ACCESSOR(bank3ctl,bank[3],mce_restart())
492 ACCESSOR(bank4ctl,bank[4],mce_restart())
493 ACCESSOR(tolerant,tolerant,)
494 ACCESSOR(check_interval,check_interval,mce_restart())
495
496 static __init int mce_init_device(void)
497 {
498         int err;
499         if (!mce_available(&boot_cpu_data))
500                 return -EIO;
501         err = sysdev_class_register(&mce_sysclass);
502         if (!err)
503                 err = sysdev_register(&device_mce);
504         if (!err) { 
505                 /* could create per CPU objects, but it is not worth it. */
506                 sysdev_create_file(&device_mce, &attr_bank0ctl); 
507                 sysdev_create_file(&device_mce, &attr_bank1ctl); 
508                 sysdev_create_file(&device_mce, &attr_bank2ctl); 
509                 sysdev_create_file(&device_mce, &attr_bank3ctl); 
510                 sysdev_create_file(&device_mce, &attr_bank4ctl); 
511                 sysdev_create_file(&device_mce, &attr_tolerant); 
512                 sysdev_create_file(&device_mce, &attr_check_interval);
513         } 
514         
515         misc_register(&mce_log_device);
516         return err;
517
518 }
519 device_initcall(mce_init_device);