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.
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>
18 #include <asm/processor.h>
21 #include <asm/kdebug.h>
22 #include <asm/uaccess.h>
24 #define MISC_MCELOG_MINOR 227
27 static int mce_dont_init
;
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;
33 static unsigned long bank
[NR_BANKS
] = { [0 ... NR_BANKS
-1] = ~0UL };
34 static unsigned long console_logged
;
35 static int notify_user
;
39 * Lockless MCE logging infrastructure.
40 * This avoids deadlocks on printk locks without having to break locks. Also
41 * separate MCEs from kernel messages to avoid bogus bug reports.
44 struct mce_log mcelog
= {
49 void mce_log(struct mce
*mce
)
55 entry
= rcu_dereference(mcelog
.next
);
56 /* When the buffer fills up discard new entries. Assume
57 that the earlier errors are the more interesting. */
58 if (entry
>= MCE_LOG_LEN
) {
59 set_bit(MCE_OVERFLOW
, &mcelog
.flags
);
62 /* Old left over entry. Skip. */
63 if (mcelog
.entry
[entry
].finished
)
67 if (cmpxchg(&mcelog
.next
, entry
, next
) == entry
)
70 memcpy(mcelog
.entry
+ entry
, mce
, sizeof(struct mce
));
72 mcelog
.entry
[entry
].finished
= 1;
75 if (!test_and_set_bit(0, &console_logged
))
79 static void print_mce(struct mce
*m
)
81 printk(KERN_EMERG
"\n"
83 "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
84 m
->cpu
, m
->mcgstatus
, m
->bank
, m
->status
);
87 "RIP%s %02x:<%016Lx> ",
88 !(m
->mcgstatus
& MCG_STATUS_EIPV
) ? " !INEXACT!" : "",
90 if (m
->cs
== __KERNEL_CS
)
91 print_symbol("{%s}", m
->rip
);
94 printk(KERN_EMERG
"TSC %Lx ", m
->tsc
);
96 printk("ADDR %Lx ", m
->addr
);
98 printk("MISC %Lx ", m
->misc
);
102 static void mce_panic(char *msg
, struct mce
*backup
, unsigned long start
)
106 for (i
= 0; i
< MCE_LOG_LEN
; i
++) {
107 unsigned long tsc
= mcelog
.entry
[i
].tsc
;
108 if (time_before(tsc
, start
))
110 print_mce(&mcelog
.entry
[i
]);
111 if (backup
&& mcelog
.entry
[i
].tsc
== backup
->tsc
)
117 printk("Fake panic: %s\n", msg
);
122 static int mce_available(struct cpuinfo_x86
*c
)
124 return test_bit(X86_FEATURE_MCE
, &c
->x86_capability
) &&
125 test_bit(X86_FEATURE_MCA
, &c
->x86_capability
);
128 static inline void mce_get_rip(struct mce
*m
, struct pt_regs
*regs
)
130 if (regs
&& (m
->mcgstatus
& MCG_STATUS_RIPV
)) {
138 /* Assume the RIP in the MSR is exact. Is this true? */
139 m
->mcgstatus
|= MCG_STATUS_EIPV
;
140 rdmsrl(rip_msr
, m
->rip
);
146 * The actual machine check handler
149 void do_machine_check(struct pt_regs
* regs
, long error_code
)
151 struct mce m
, panicm
;
152 int nowayout
= (tolerant
< 1);
156 int panicm_found
= 0;
159 notify_die(DIE_NMI
, "machine check", regs
, error_code
, 255, SIGKILL
);
163 memset(&m
, 0, sizeof(struct mce
));
164 m
.cpu
= hard_smp_processor_id();
165 rdmsrl(MSR_IA32_MCG_STATUS
, m
.mcgstatus
);
166 if (!(m
.mcgstatus
& MCG_STATUS_RIPV
))
172 for (i
= 0; i
< banks
; i
++) {
181 rdmsrl(MSR_IA32_MC0_STATUS
+ i
*4, m
.status
);
182 if ((m
.status
& MCI_STATUS_VAL
) == 0)
185 if (m
.status
& MCI_STATUS_EN
) {
186 /* In theory _OVER could be a nowayout too, but
187 assume any overflowed errors were no fatal. */
188 nowayout
|= !!(m
.status
& MCI_STATUS_PCC
);
189 kill_it
|= !!(m
.status
& MCI_STATUS_UC
);
192 if (m
.status
& MCI_STATUS_MISCV
)
193 rdmsrl(MSR_IA32_MC0_MISC
+ i
*4, m
.misc
);
194 if (m
.status
& MCI_STATUS_ADDRV
)
195 rdmsrl(MSR_IA32_MC0_ADDR
+ i
*4, m
.addr
);
197 mce_get_rip(&m
, regs
);
198 if (error_code
!= -1)
200 wrmsrl(MSR_IA32_MC0_STATUS
+ i
*4, 0);
203 /* Did this bank cause the exception? */
204 /* Assume that the bank with uncorrectable errors did it,
205 and that there is only a single one. */
206 if ((m
.status
& MCI_STATUS_UC
) && (m
.status
& MCI_STATUS_EN
)) {
211 tainted
|= TAINT_MACHINE_CHECK
;
214 /* Never do anything final in the polling timer */
218 /* If we didn't find an uncorrectable error, pick
219 the last one (shouldn't happen, just being safe). */
223 mce_panic("Machine check", &panicm
, mcestart
);
227 if (m
.mcgstatus
& MCG_STATUS_RIPV
)
228 user_space
= panicm
.rip
&& (panicm
.cs
& 3);
230 /* When the machine was in user space and the CPU didn't get
231 confused it's normally not necessary to panic, unless you
232 are paranoid (tolerant == 0)
234 RED-PEN could be more tolerant for MCEs in idle,
235 but most likely they occur at boot anyways, where
236 it is best to just halt the machine. */
237 if ((!user_space
&& (panic_on_oops
|| tolerant
< 2)) ||
238 (unsigned)current
->pid
<= 1)
239 mce_panic("Uncorrected machine check", &panicm
, mcestart
);
241 /* do_exit takes an awful lot of locks and has as
242 slight risk of deadlocking. If you don't want that
243 don't set tolerant >= 2 */
249 /* Last thing done in the machine check exception to clear state. */
250 wrmsrl(MSR_IA32_MCG_STATUS
, 0);
254 * Periodic polling timer for "silent" machine check errors.
257 static int check_interval
= 5 * 60; /* 5 minutes */
258 static void mcheck_timer(void *data
);
259 static DECLARE_WORK(mcheck_work
, mcheck_timer
, NULL
);
261 static void mcheck_check_cpu(void *info
)
263 if (mce_available(¤t_cpu_data
))
264 do_machine_check(NULL
, 0);
267 static void mcheck_timer(void *data
)
269 on_each_cpu(mcheck_check_cpu
, NULL
, 1, 1);
270 schedule_delayed_work(&mcheck_work
, check_interval
* HZ
);
273 * It's ok to read stale data here for notify_user and
274 * console_logged as we'll simply get the updated versions
275 * on the next mcheck_timer execution and atomic operations
276 * on console_logged act as synchronization for notify_user
279 if (notify_user
&& console_logged
) {
281 clear_bit(0, &console_logged
);
282 printk(KERN_INFO
"Machine check events logged\n");
287 static __init
int periodic_mcheck_init(void)
290 schedule_delayed_work(&mcheck_work
, check_interval
*HZ
);
293 __initcall(periodic_mcheck_init
);
297 * Initialize Machine Checks for a CPU.
299 static void mce_init(void *dummy
)
304 rdmsrl(MSR_IA32_MCG_CAP
, cap
);
306 if (banks
> NR_BANKS
) {
307 printk(KERN_INFO
"MCE: warning: using only %d banks\n", banks
);
310 /* Use accurate RIP reporting if available. */
311 if ((cap
& (1<<9)) && ((cap
>> 16) & 0xff) >= 9)
312 rip_msr
= MSR_IA32_MCG_EIP
;
314 /* Log the machine checks left over from the previous reset.
315 This also clears all registers */
316 do_machine_check(NULL
, -1);
318 set_in_cr4(X86_CR4_MCE
);
321 wrmsr(MSR_IA32_MCG_CTL
, 0xffffffff, 0xffffffff);
323 for (i
= 0; i
< banks
; i
++) {
324 wrmsrl(MSR_IA32_MC0_CTL
+4*i
, bank
[i
]);
325 wrmsrl(MSR_IA32_MC0_STATUS
+4*i
, 0);
329 /* Add per CPU specific workarounds here */
330 static void __cpuinit
mce_cpu_quirks(struct cpuinfo_x86
*c
)
332 /* This should be disabled by the BIOS, but isn't always */
333 if (c
->x86_vendor
== X86_VENDOR_AMD
&& c
->x86
== 15) {
334 /* disable GART TBL walk error reporting, which trips off
335 incorrectly with the IOMMU & 3ware & Cerberus. */
336 clear_bit(10, &bank
[4]);
340 static void __cpuinit
mce_cpu_features(struct cpuinfo_x86
*c
)
342 switch (c
->x86_vendor
) {
343 case X86_VENDOR_INTEL
:
344 mce_intel_feature_init(c
);
352 * Called for each booted CPU to set up machine checks.
353 * Must be called with preempt off.
355 void __cpuinit
mcheck_init(struct cpuinfo_x86
*c
)
357 static cpumask_t mce_cpus __initdata
= CPU_MASK_NONE
;
362 cpu_test_and_set(smp_processor_id(), mce_cpus
) ||
371 * Character device to read and clear the MCE log.
374 static void collect_tscs(void *data
)
376 unsigned long *cpu_tsc
= (unsigned long *)data
;
377 rdtscll(cpu_tsc
[smp_processor_id()]);
380 static ssize_t
mce_read(struct file
*filp
, char __user
*ubuf
, size_t usize
, loff_t
*off
)
382 unsigned long *cpu_tsc
;
383 static DECLARE_MUTEX(mce_read_sem
);
385 char __user
*buf
= ubuf
;
388 cpu_tsc
= kmalloc(NR_CPUS
* sizeof(long), GFP_KERNEL
);
393 next
= rcu_dereference(mcelog
.next
);
395 /* Only supports full reads right now */
396 if (*off
!= 0 || usize
< MCE_LOG_LEN
*sizeof(struct mce
)) {
403 for (i
= 0; i
< next
; i
++) {
404 if (!mcelog
.entry
[i
].finished
)
407 err
|= copy_to_user(buf
, mcelog
.entry
+ i
, sizeof(struct mce
));
408 buf
+= sizeof(struct mce
);
411 memset(mcelog
.entry
, 0, next
* sizeof(struct mce
));
416 /* Collect entries that were still getting written before the synchronize. */
418 on_each_cpu(collect_tscs
, cpu_tsc
, 1, 1);
419 for (i
= next
; i
< MCE_LOG_LEN
; i
++) {
420 if (mcelog
.entry
[i
].finished
&&
421 mcelog
.entry
[i
].tsc
< cpu_tsc
[mcelog
.entry
[i
].cpu
]) {
422 err
|= copy_to_user(buf
, mcelog
.entry
+i
, sizeof(struct mce
));
424 buf
+= sizeof(struct mce
);
425 memset(&mcelog
.entry
[i
], 0, sizeof(struct mce
));
430 return err
? -EFAULT
: buf
- ubuf
;
433 static int mce_ioctl(struct inode
*i
, struct file
*f
,unsigned int cmd
, unsigned long arg
)
435 int __user
*p
= (int __user
*)arg
;
436 if (!capable(CAP_SYS_ADMIN
))
439 case MCE_GET_RECORD_LEN
:
440 return put_user(sizeof(struct mce
), p
);
441 case MCE_GET_LOG_LEN
:
442 return put_user(MCE_LOG_LEN
, p
);
443 case MCE_GETCLEAR_FLAGS
: {
446 flags
= mcelog
.flags
;
447 } while (cmpxchg(&mcelog
.flags
, flags
, 0) != flags
);
448 return put_user(flags
, p
);
455 static struct file_operations mce_chrdev_ops
= {
460 static struct miscdevice mce_log_device
= {
467 * Old style boot options parsing. Only for compatibility.
470 static int __init
mcheck_disable(char *str
)
476 /* mce=off disables machine check. Note you can reenable it later
478 static int __init
mcheck_enable(char *str
)
480 if (!strcmp(str
, "off"))
483 printk("mce= argument %s ignored. Please use /sys", str
);
487 __setup("nomce", mcheck_disable
);
488 __setup("mce", mcheck_enable
);
494 /* On resume clear all MCE state. Don't want to see leftovers from the BIOS. */
495 static int mce_resume(struct sys_device
*dev
)
497 on_each_cpu(mce_init
, NULL
, 1, 1);
501 /* Reinit MCEs after user configuration changes */
502 static void mce_restart(void)
505 cancel_delayed_work(&mcheck_work
);
506 /* Timer race is harmless here */
507 on_each_cpu(mce_init
, NULL
, 1, 1);
509 schedule_delayed_work(&mcheck_work
, check_interval
*HZ
);
512 static struct sysdev_class mce_sysclass
= {
513 .resume
= mce_resume
,
514 set_kset_name("machinecheck"),
517 static struct sys_device device_mce
= {
519 .cls
= &mce_sysclass
,
522 /* Why are there no generic functions for this? */
523 #define ACCESSOR(name, var, start) \
524 static ssize_t show_ ## name(struct sys_device *s, char *buf) { \
525 return sprintf(buf, "%lx\n", (unsigned long)var); \
527 static ssize_t set_ ## name(struct sys_device *s,const char *buf,size_t siz) { \
529 unsigned long new = simple_strtoul(buf, &end, 0); \
530 if (end == buf) return -EINVAL; \
535 static SYSDEV_ATTR(name, 0644, show_ ## name, set_ ## name);
537 ACCESSOR(bank0ctl
,bank
[0],mce_restart())
538 ACCESSOR(bank1ctl
,bank
[1],mce_restart())
539 ACCESSOR(bank2ctl
,bank
[2],mce_restart())
540 ACCESSOR(bank3ctl
,bank
[3],mce_restart())
541 ACCESSOR(bank4ctl
,bank
[4],mce_restart())
542 ACCESSOR(tolerant
,tolerant
,)
543 ACCESSOR(check_interval
,check_interval
,mce_restart())
545 static __cpuinit
int mce_init_device(void)
548 if (!mce_available(&boot_cpu_data
))
550 err
= sysdev_class_register(&mce_sysclass
);
552 err
= sysdev_register(&device_mce
);
554 /* could create per CPU objects, but it is not worth it. */
555 sysdev_create_file(&device_mce
, &attr_bank0ctl
);
556 sysdev_create_file(&device_mce
, &attr_bank1ctl
);
557 sysdev_create_file(&device_mce
, &attr_bank2ctl
);
558 sysdev_create_file(&device_mce
, &attr_bank3ctl
);
559 sysdev_create_file(&device_mce
, &attr_bank4ctl
);
560 sysdev_create_file(&device_mce
, &attr_tolerant
);
561 sysdev_create_file(&device_mce
, &attr_check_interval
);
564 misc_register(&mce_log_device
);
568 device_initcall(mce_init_device
);