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