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