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