x86, mce: Fix mce printing
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / cpu / mcheck / mce.c
blob6a3127ecb5cc61d4342ce0d1b4649475fd6c7e4b
1 /*
2 * Machine check handler.
4 * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
5 * Rest from unknown author(s).
6 * 2004 Andi Kleen. Rewrote most of it.
7 * Copyright 2008 Intel Corporation
8 * Author: Andi Kleen
9 */
10 #include <linux/thread_info.h>
11 #include <linux/capability.h>
12 #include <linux/miscdevice.h>
13 #include <linux/interrupt.h>
14 #include <linux/ratelimit.h>
15 #include <linux/kallsyms.h>
16 #include <linux/rcupdate.h>
17 #include <linux/kobject.h>
18 #include <linux/uaccess.h>
19 #include <linux/kdebug.h>
20 #include <linux/kernel.h>
21 #include <linux/percpu.h>
22 #include <linux/string.h>
23 #include <linux/sysdev.h>
24 #include <linux/delay.h>
25 #include <linux/ctype.h>
26 #include <linux/sched.h>
27 #include <linux/sysfs.h>
28 #include <linux/types.h>
29 #include <linux/init.h>
30 #include <linux/kmod.h>
31 #include <linux/poll.h>
32 #include <linux/nmi.h>
33 #include <linux/cpu.h>
34 #include <linux/smp.h>
35 #include <linux/fs.h>
36 #include <linux/mm.h>
38 #include <asm/processor.h>
39 #include <asm/hw_irq.h>
40 #include <asm/apic.h>
41 #include <asm/idle.h>
42 #include <asm/ipi.h>
43 #include <asm/mce.h>
44 #include <asm/msr.h>
46 #include "mce-internal.h"
47 #include "mce.h"
49 /* Handle unconfigured int18 (should never happen) */
50 static void unexpected_machine_check(struct pt_regs *regs, long error_code)
52 printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
53 smp_processor_id());
56 /* Call the installed machine check handler for this CPU setup. */
57 void (*machine_check_vector)(struct pt_regs *, long error_code) =
58 unexpected_machine_check;
60 int mce_disabled;
62 #ifdef CONFIG_X86_NEW_MCE
64 #define MISC_MCELOG_MINOR 227
66 #define SPINUNIT 100 /* 100ns */
68 atomic_t mce_entry;
70 DEFINE_PER_CPU(unsigned, mce_exception_count);
73 * Tolerant levels:
74 * 0: always panic on uncorrected errors, log corrected errors
75 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
76 * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
77 * 3: never panic or SIGBUS, log all errors (for testing only)
79 static int tolerant = 1;
80 static int banks;
81 static u64 *bank;
82 static unsigned long notify_user;
83 static int rip_msr;
84 static int mce_bootlog = -1;
85 static int monarch_timeout = -1;
86 static int mce_panic_timeout;
87 int mce_ser;
89 static char trigger[128];
90 static char *trigger_argv[2] = { trigger, NULL };
92 static unsigned long dont_init_banks;
94 static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
95 static DEFINE_PER_CPU(struct mce, mces_seen);
96 static int cpu_missing;
99 /* MCA banks polled by the period polling timer for corrected events */
100 DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
101 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
104 static inline int skip_bank_init(int i)
106 return i < BITS_PER_LONG && test_bit(i, &dont_init_banks);
109 static DEFINE_PER_CPU(struct work_struct, mce_work);
111 /* Do initial initialization of a struct mce */
112 void mce_setup(struct mce *m)
114 memset(m, 0, sizeof(struct mce));
115 m->cpu = m->extcpu = smp_processor_id();
116 rdtscll(m->tsc);
117 /* We hope get_seconds stays lockless */
118 m->time = get_seconds();
119 m->cpuvendor = boot_cpu_data.x86_vendor;
120 m->cpuid = cpuid_eax(1);
121 #ifdef CONFIG_SMP
122 m->socketid = cpu_data(m->extcpu).phys_proc_id;
123 #endif
124 m->apicid = cpu_data(m->extcpu).initial_apicid;
125 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
128 DEFINE_PER_CPU(struct mce, injectm);
129 EXPORT_PER_CPU_SYMBOL_GPL(injectm);
132 * Lockless MCE logging infrastructure.
133 * This avoids deadlocks on printk locks without having to break locks. Also
134 * separate MCEs from kernel messages to avoid bogus bug reports.
137 static struct mce_log mcelog = {
138 .signature = MCE_LOG_SIGNATURE,
139 .len = MCE_LOG_LEN,
140 .recordlen = sizeof(struct mce),
143 void mce_log(struct mce *mce)
145 unsigned next, entry;
147 mce->finished = 0;
148 wmb();
149 for (;;) {
150 entry = rcu_dereference(mcelog.next);
151 for (;;) {
153 * When the buffer fills up discard new entries.
154 * Assume that the earlier errors are the more
155 * interesting ones:
157 if (entry >= MCE_LOG_LEN) {
158 set_bit(MCE_OVERFLOW,
159 (unsigned long *)&mcelog.flags);
160 return;
162 /* Old left over entry. Skip: */
163 if (mcelog.entry[entry].finished) {
164 entry++;
165 continue;
167 break;
169 smp_rmb();
170 next = entry + 1;
171 if (cmpxchg(&mcelog.next, entry, next) == entry)
172 break;
174 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
175 wmb();
176 mcelog.entry[entry].finished = 1;
177 wmb();
179 mce->finished = 1;
180 set_bit(0, &notify_user);
183 static void print_mce(struct mce *m)
185 printk(KERN_EMERG
186 "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
187 m->extcpu, m->mcgstatus, m->bank, m->status);
188 if (m->ip) {
189 printk(KERN_EMERG "RIP%s %02x:<%016Lx> ",
190 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
191 m->cs, m->ip);
192 if (m->cs == __KERNEL_CS)
193 print_symbol("{%s}", m->ip);
194 printk("\n");
196 printk(KERN_EMERG "TSC %llx ", m->tsc);
197 if (m->addr)
198 printk("ADDR %llx ", m->addr);
199 if (m->misc)
200 printk("MISC %llx ", m->misc);
201 printk("\n");
202 printk(KERN_EMERG "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
203 m->cpuvendor, m->cpuid, m->time, m->socketid,
204 m->apicid);
207 static void print_mce_head(void)
209 printk(KERN_EMERG "\n" KERN_EMERG "HARDWARE ERROR\n");
212 static void print_mce_tail(void)
214 printk(KERN_EMERG "This is not a software problem!\n"
215 KERN_EMERG "Run through mcelog --ascii to decode and contact your hardware vendor\n");
218 #define PANIC_TIMEOUT 5 /* 5 seconds */
220 static atomic_t mce_paniced;
222 /* Panic in progress. Enable interrupts and wait for final IPI */
223 static void wait_for_panic(void)
225 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
226 preempt_disable();
227 local_irq_enable();
228 while (timeout-- > 0)
229 udelay(1);
230 if (panic_timeout == 0)
231 panic_timeout = mce_panic_timeout;
232 panic("Panicing machine check CPU died");
235 static void mce_panic(char *msg, struct mce *final, char *exp)
237 int i;
240 * Make sure only one CPU runs in machine check panic
242 if (atomic_add_return(1, &mce_paniced) > 1)
243 wait_for_panic();
244 barrier();
246 bust_spinlocks(1);
247 console_verbose();
248 print_mce_head();
249 /* First print corrected ones that are still unlogged */
250 for (i = 0; i < MCE_LOG_LEN; i++) {
251 struct mce *m = &mcelog.entry[i];
252 if (!(m->status & MCI_STATUS_VAL))
253 continue;
254 if (!(m->status & MCI_STATUS_UC))
255 print_mce(m);
257 /* Now print uncorrected but with the final one last */
258 for (i = 0; i < MCE_LOG_LEN; i++) {
259 struct mce *m = &mcelog.entry[i];
260 if (!(m->status & MCI_STATUS_VAL))
261 continue;
262 if (!(m->status & MCI_STATUS_UC))
263 continue;
264 if (!final || memcmp(m, final, sizeof(struct mce)))
265 print_mce(m);
267 if (final)
268 print_mce(final);
269 if (cpu_missing)
270 printk(KERN_EMERG "Some CPUs didn't answer in synchronization\n");
271 print_mce_tail();
272 if (exp)
273 printk(KERN_EMERG "Machine check: %s\n", exp);
274 if (panic_timeout == 0)
275 panic_timeout = mce_panic_timeout;
276 panic(msg);
279 /* Support code for software error injection */
281 static int msr_to_offset(u32 msr)
283 unsigned bank = __get_cpu_var(injectm.bank);
284 if (msr == rip_msr)
285 return offsetof(struct mce, ip);
286 if (msr == MSR_IA32_MC0_STATUS + bank*4)
287 return offsetof(struct mce, status);
288 if (msr == MSR_IA32_MC0_ADDR + bank*4)
289 return offsetof(struct mce, addr);
290 if (msr == MSR_IA32_MC0_MISC + bank*4)
291 return offsetof(struct mce, misc);
292 if (msr == MSR_IA32_MCG_STATUS)
293 return offsetof(struct mce, mcgstatus);
294 return -1;
297 /* MSR access wrappers used for error injection */
298 static u64 mce_rdmsrl(u32 msr)
300 u64 v;
301 if (__get_cpu_var(injectm).finished) {
302 int offset = msr_to_offset(msr);
303 if (offset < 0)
304 return 0;
305 return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
307 rdmsrl(msr, v);
308 return v;
311 static void mce_wrmsrl(u32 msr, u64 v)
313 if (__get_cpu_var(injectm).finished) {
314 int offset = msr_to_offset(msr);
315 if (offset >= 0)
316 *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
317 return;
319 wrmsrl(msr, v);
323 * Simple lockless ring to communicate PFNs from the exception handler with the
324 * process context work function. This is vastly simplified because there's
325 * only a single reader and a single writer.
327 #define MCE_RING_SIZE 16 /* we use one entry less */
329 struct mce_ring {
330 unsigned short start;
331 unsigned short end;
332 unsigned long ring[MCE_RING_SIZE];
334 static DEFINE_PER_CPU(struct mce_ring, mce_ring);
336 /* Runs with CPU affinity in workqueue */
337 static int mce_ring_empty(void)
339 struct mce_ring *r = &__get_cpu_var(mce_ring);
341 return r->start == r->end;
344 static int mce_ring_get(unsigned long *pfn)
346 struct mce_ring *r;
347 int ret = 0;
349 *pfn = 0;
350 get_cpu();
351 r = &__get_cpu_var(mce_ring);
352 if (r->start == r->end)
353 goto out;
354 *pfn = r->ring[r->start];
355 r->start = (r->start + 1) % MCE_RING_SIZE;
356 ret = 1;
357 out:
358 put_cpu();
359 return ret;
362 /* Always runs in MCE context with preempt off */
363 static int mce_ring_add(unsigned long pfn)
365 struct mce_ring *r = &__get_cpu_var(mce_ring);
366 unsigned next;
368 next = (r->end + 1) % MCE_RING_SIZE;
369 if (next == r->start)
370 return -1;
371 r->ring[r->end] = pfn;
372 wmb();
373 r->end = next;
374 return 0;
377 int mce_available(struct cpuinfo_x86 *c)
379 if (mce_disabled)
380 return 0;
381 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
384 static void mce_schedule_work(void)
386 if (!mce_ring_empty()) {
387 struct work_struct *work = &__get_cpu_var(mce_work);
388 if (!work_pending(work))
389 schedule_work(work);
394 * Get the address of the instruction at the time of the machine check
395 * error.
397 static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
400 if (regs && (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV))) {
401 m->ip = regs->ip;
402 m->cs = regs->cs;
403 } else {
404 m->ip = 0;
405 m->cs = 0;
407 if (rip_msr)
408 m->ip = mce_rdmsrl(rip_msr);
411 #ifdef CONFIG_X86_LOCAL_APIC
413 * Called after interrupts have been reenabled again
414 * when a MCE happened during an interrupts off region
415 * in the kernel.
417 asmlinkage void smp_mce_self_interrupt(struct pt_regs *regs)
419 ack_APIC_irq();
420 exit_idle();
421 irq_enter();
422 mce_notify_irq();
423 mce_schedule_work();
424 irq_exit();
426 #endif
428 static void mce_report_event(struct pt_regs *regs)
430 if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
431 mce_notify_irq();
433 * Triggering the work queue here is just an insurance
434 * policy in case the syscall exit notify handler
435 * doesn't run soon enough or ends up running on the
436 * wrong CPU (can happen when audit sleeps)
438 mce_schedule_work();
439 return;
442 #ifdef CONFIG_X86_LOCAL_APIC
444 * Without APIC do not notify. The event will be picked
445 * up eventually.
447 if (!cpu_has_apic)
448 return;
451 * When interrupts are disabled we cannot use
452 * kernel services safely. Trigger an self interrupt
453 * through the APIC to instead do the notification
454 * after interrupts are reenabled again.
456 apic->send_IPI_self(MCE_SELF_VECTOR);
459 * Wait for idle afterwards again so that we don't leave the
460 * APIC in a non idle state because the normal APIC writes
461 * cannot exclude us.
463 apic_wait_icr_idle();
464 #endif
467 DEFINE_PER_CPU(unsigned, mce_poll_count);
470 * Poll for corrected events or events that happened before reset.
471 * Those are just logged through /dev/mcelog.
473 * This is executed in standard interrupt context.
475 * Note: spec recommends to panic for fatal unsignalled
476 * errors here. However this would be quite problematic --
477 * we would need to reimplement the Monarch handling and
478 * it would mess up the exclusion between exception handler
479 * and poll hander -- * so we skip this for now.
480 * These cases should not happen anyways, or only when the CPU
481 * is already totally * confused. In this case it's likely it will
482 * not fully execute the machine check handler either.
484 void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
486 struct mce m;
487 int i;
489 __get_cpu_var(mce_poll_count)++;
491 mce_setup(&m);
493 m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
494 for (i = 0; i < banks; i++) {
495 if (!bank[i] || !test_bit(i, *b))
496 continue;
498 m.misc = 0;
499 m.addr = 0;
500 m.bank = i;
501 m.tsc = 0;
503 barrier();
504 m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
505 if (!(m.status & MCI_STATUS_VAL))
506 continue;
509 * Uncorrected or signalled events are handled by the exception
510 * handler when it is enabled, so don't process those here.
512 * TBD do the same check for MCI_STATUS_EN here?
514 if (!(flags & MCP_UC) &&
515 (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
516 continue;
518 if (m.status & MCI_STATUS_MISCV)
519 m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
520 if (m.status & MCI_STATUS_ADDRV)
521 m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
523 if (!(flags & MCP_TIMESTAMP))
524 m.tsc = 0;
526 * Don't get the IP here because it's unlikely to
527 * have anything to do with the actual error location.
529 if (!(flags & MCP_DONTLOG)) {
530 mce_log(&m);
531 add_taint(TAINT_MACHINE_CHECK);
535 * Clear state for this bank.
537 mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
541 * Don't clear MCG_STATUS here because it's only defined for
542 * exceptions.
545 sync_core();
547 EXPORT_SYMBOL_GPL(machine_check_poll);
550 * Do a quick check if any of the events requires a panic.
551 * This decides if we keep the events around or clear them.
553 static int mce_no_way_out(struct mce *m, char **msg)
555 int i;
557 for (i = 0; i < banks; i++) {
558 m->status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
559 if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
560 return 1;
562 return 0;
566 * Variable to establish order between CPUs while scanning.
567 * Each CPU spins initially until executing is equal its number.
569 static atomic_t mce_executing;
572 * Defines order of CPUs on entry. First CPU becomes Monarch.
574 static atomic_t mce_callin;
577 * Check if a timeout waiting for other CPUs happened.
579 static int mce_timed_out(u64 *t)
582 * The others already did panic for some reason.
583 * Bail out like in a timeout.
584 * rmb() to tell the compiler that system_state
585 * might have been modified by someone else.
587 rmb();
588 if (atomic_read(&mce_paniced))
589 wait_for_panic();
590 if (!monarch_timeout)
591 goto out;
592 if ((s64)*t < SPINUNIT) {
593 /* CHECKME: Make panic default for 1 too? */
594 if (tolerant < 1)
595 mce_panic("Timeout synchronizing machine check over CPUs",
596 NULL, NULL);
597 cpu_missing = 1;
598 return 1;
600 *t -= SPINUNIT;
601 out:
602 touch_nmi_watchdog();
603 return 0;
607 * The Monarch's reign. The Monarch is the CPU who entered
608 * the machine check handler first. It waits for the others to
609 * raise the exception too and then grades them. When any
610 * error is fatal panic. Only then let the others continue.
612 * The other CPUs entering the MCE handler will be controlled by the
613 * Monarch. They are called Subjects.
615 * This way we prevent any potential data corruption in a unrecoverable case
616 * and also makes sure always all CPU's errors are examined.
618 * Also this detects the case of an machine check event coming from outer
619 * space (not detected by any CPUs) In this case some external agent wants
620 * us to shut down, so panic too.
622 * The other CPUs might still decide to panic if the handler happens
623 * in a unrecoverable place, but in this case the system is in a semi-stable
624 * state and won't corrupt anything by itself. It's ok to let the others
625 * continue for a bit first.
627 * All the spin loops have timeouts; when a timeout happens a CPU
628 * typically elects itself to be Monarch.
630 static void mce_reign(void)
632 int cpu;
633 struct mce *m = NULL;
634 int global_worst = 0;
635 char *msg = NULL;
636 char *nmsg = NULL;
639 * This CPU is the Monarch and the other CPUs have run
640 * through their handlers.
641 * Grade the severity of the errors of all the CPUs.
643 for_each_possible_cpu(cpu) {
644 int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
645 &nmsg);
646 if (severity > global_worst) {
647 msg = nmsg;
648 global_worst = severity;
649 m = &per_cpu(mces_seen, cpu);
654 * Cannot recover? Panic here then.
655 * This dumps all the mces in the log buffer and stops the
656 * other CPUs.
658 if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
659 mce_panic("Fatal Machine check", m, msg);
662 * For UC somewhere we let the CPU who detects it handle it.
663 * Also must let continue the others, otherwise the handling
664 * CPU could deadlock on a lock.
668 * No machine check event found. Must be some external
669 * source or one CPU is hung. Panic.
671 if (!m && tolerant < 3)
672 mce_panic("Machine check from unknown source", NULL, NULL);
675 * Now clear all the mces_seen so that they don't reappear on
676 * the next mce.
678 for_each_possible_cpu(cpu)
679 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
682 static atomic_t global_nwo;
685 * Start of Monarch synchronization. This waits until all CPUs have
686 * entered the exception handler and then determines if any of them
687 * saw a fatal event that requires panic. Then it executes them
688 * in the entry order.
689 * TBD double check parallel CPU hotunplug
691 static int mce_start(int no_way_out, int *order)
693 int nwo;
694 int cpus = num_online_cpus();
695 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
697 if (!timeout) {
698 *order = -1;
699 return no_way_out;
702 atomic_add(no_way_out, &global_nwo);
705 * Wait for everyone.
707 while (atomic_read(&mce_callin) != cpus) {
708 if (mce_timed_out(&timeout)) {
709 atomic_set(&global_nwo, 0);
710 *order = -1;
711 return no_way_out;
713 ndelay(SPINUNIT);
717 * Cache the global no_way_out state.
719 nwo = atomic_read(&global_nwo);
722 * Monarch starts executing now, the others wait.
724 if (*order == 1) {
725 atomic_set(&mce_executing, 1);
726 return nwo;
730 * Now start the scanning loop one by one
731 * in the original callin order.
732 * This way when there are any shared banks it will
733 * be only seen by one CPU before cleared, avoiding duplicates.
735 while (atomic_read(&mce_executing) < *order) {
736 if (mce_timed_out(&timeout)) {
737 atomic_set(&global_nwo, 0);
738 *order = -1;
739 return no_way_out;
741 ndelay(SPINUNIT);
743 return nwo;
747 * Synchronize between CPUs after main scanning loop.
748 * This invokes the bulk of the Monarch processing.
750 static int mce_end(int order)
752 int ret = -1;
753 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
755 if (!timeout)
756 goto reset;
757 if (order < 0)
758 goto reset;
761 * Allow others to run.
763 atomic_inc(&mce_executing);
765 if (order == 1) {
766 /* CHECKME: Can this race with a parallel hotplug? */
767 int cpus = num_online_cpus();
770 * Monarch: Wait for everyone to go through their scanning
771 * loops.
773 while (atomic_read(&mce_executing) <= cpus) {
774 if (mce_timed_out(&timeout))
775 goto reset;
776 ndelay(SPINUNIT);
779 mce_reign();
780 barrier();
781 ret = 0;
782 } else {
784 * Subject: Wait for Monarch to finish.
786 while (atomic_read(&mce_executing) != 0) {
787 if (mce_timed_out(&timeout))
788 goto reset;
789 ndelay(SPINUNIT);
793 * Don't reset anything. That's done by the Monarch.
795 return 0;
799 * Reset all global state.
801 reset:
802 atomic_set(&global_nwo, 0);
803 atomic_set(&mce_callin, 0);
804 barrier();
807 * Let others run again.
809 atomic_set(&mce_executing, 0);
810 return ret;
814 * Check if the address reported by the CPU is in a format we can parse.
815 * It would be possible to add code for most other cases, but all would
816 * be somewhat complicated (e.g. segment offset would require an instruction
817 * parser). So only support physical addresses upto page granuality for now.
819 static int mce_usable_address(struct mce *m)
821 if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
822 return 0;
823 if ((m->misc & 0x3f) > PAGE_SHIFT)
824 return 0;
825 if (((m->misc >> 6) & 7) != MCM_ADDR_PHYS)
826 return 0;
827 return 1;
830 static void mce_clear_state(unsigned long *toclear)
832 int i;
834 for (i = 0; i < banks; i++) {
835 if (test_bit(i, toclear))
836 mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
841 * The actual machine check handler. This only handles real
842 * exceptions when something got corrupted coming in through int 18.
844 * This is executed in NMI context not subject to normal locking rules. This
845 * implies that most kernel services cannot be safely used. Don't even
846 * think about putting a printk in there!
848 * On Intel systems this is entered on all CPUs in parallel through
849 * MCE broadcast. However some CPUs might be broken beyond repair,
850 * so be always careful when synchronizing with others.
852 void do_machine_check(struct pt_regs *regs, long error_code)
854 struct mce m, *final;
855 int i;
856 int worst = 0;
857 int severity;
859 * Establish sequential order between the CPUs entering the machine
860 * check handler.
862 int order;
865 * If no_way_out gets set, there is no safe way to recover from this
866 * MCE. If tolerant is cranked up, we'll try anyway.
868 int no_way_out = 0;
870 * If kill_it gets set, there might be a way to recover from this
871 * error.
873 int kill_it = 0;
874 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
875 char *msg = "Unknown";
877 atomic_inc(&mce_entry);
879 __get_cpu_var(mce_exception_count)++;
881 if (notify_die(DIE_NMI, "machine check", regs, error_code,
882 18, SIGKILL) == NOTIFY_STOP)
883 goto out;
884 if (!banks)
885 goto out;
887 order = atomic_add_return(1, &mce_callin);
888 mce_setup(&m);
890 m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
891 no_way_out = mce_no_way_out(&m, &msg);
893 final = &__get_cpu_var(mces_seen);
894 *final = m;
896 barrier();
899 * When no restart IP must always kill or panic.
901 if (!(m.mcgstatus & MCG_STATUS_RIPV))
902 kill_it = 1;
905 * Go through all the banks in exclusion of the other CPUs.
906 * This way we don't report duplicated events on shared banks
907 * because the first one to see it will clear it.
909 no_way_out = mce_start(no_way_out, &order);
910 for (i = 0; i < banks; i++) {
911 __clear_bit(i, toclear);
912 if (!bank[i])
913 continue;
915 m.misc = 0;
916 m.addr = 0;
917 m.bank = i;
919 m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
920 if ((m.status & MCI_STATUS_VAL) == 0)
921 continue;
924 * Non uncorrected or non signaled errors are handled by
925 * machine_check_poll. Leave them alone, unless this panics.
927 if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
928 !no_way_out)
929 continue;
932 * Set taint even when machine check was not enabled.
934 add_taint(TAINT_MACHINE_CHECK);
936 severity = mce_severity(&m, tolerant, NULL);
939 * When machine check was for corrected handler don't touch,
940 * unless we're panicing.
942 if (severity == MCE_KEEP_SEVERITY && !no_way_out)
943 continue;
944 __set_bit(i, toclear);
945 if (severity == MCE_NO_SEVERITY) {
947 * Machine check event was not enabled. Clear, but
948 * ignore.
950 continue;
954 * Kill on action required.
956 if (severity == MCE_AR_SEVERITY)
957 kill_it = 1;
959 if (m.status & MCI_STATUS_MISCV)
960 m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
961 if (m.status & MCI_STATUS_ADDRV)
962 m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
965 * Action optional error. Queue address for later processing.
966 * When the ring overflows we just ignore the AO error.
967 * RED-PEN add some logging mechanism when
968 * usable_address or mce_add_ring fails.
969 * RED-PEN don't ignore overflow for tolerant == 0
971 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
972 mce_ring_add(m.addr >> PAGE_SHIFT);
974 mce_get_rip(&m, regs);
975 mce_log(&m);
977 if (severity > worst) {
978 *final = m;
979 worst = severity;
983 if (!no_way_out)
984 mce_clear_state(toclear);
987 * Do most of the synchronization with other CPUs.
988 * When there's any problem use only local no_way_out state.
990 if (mce_end(order) < 0)
991 no_way_out = worst >= MCE_PANIC_SEVERITY;
994 * If we have decided that we just CAN'T continue, and the user
995 * has not set tolerant to an insane level, give up and die.
997 * This is mainly used in the case when the system doesn't
998 * support MCE broadcasting or it has been disabled.
1000 if (no_way_out && tolerant < 3)
1001 mce_panic("Fatal machine check on current CPU", final, msg);
1004 * If the error seems to be unrecoverable, something should be
1005 * done. Try to kill as little as possible. If we can kill just
1006 * one task, do that. If the user has set the tolerance very
1007 * high, don't try to do anything at all.
1010 if (kill_it && tolerant < 3)
1011 force_sig(SIGBUS, current);
1013 /* notify userspace ASAP */
1014 set_thread_flag(TIF_MCE_NOTIFY);
1016 if (worst > 0)
1017 mce_report_event(regs);
1018 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
1019 out:
1020 atomic_dec(&mce_entry);
1021 sync_core();
1023 EXPORT_SYMBOL_GPL(do_machine_check);
1025 /* dummy to break dependency. actual code is in mm/memory-failure.c */
1026 void __attribute__((weak)) memory_failure(unsigned long pfn, int vector)
1028 printk(KERN_ERR "Action optional memory failure at %lx ignored\n", pfn);
1032 * Called after mce notification in process context. This code
1033 * is allowed to sleep. Call the high level VM handler to process
1034 * any corrupted pages.
1035 * Assume that the work queue code only calls this one at a time
1036 * per CPU.
1037 * Note we don't disable preemption, so this code might run on the wrong
1038 * CPU. In this case the event is picked up by the scheduled work queue.
1039 * This is merely a fast path to expedite processing in some common
1040 * cases.
1042 void mce_notify_process(void)
1044 unsigned long pfn;
1045 mce_notify_irq();
1046 while (mce_ring_get(&pfn))
1047 memory_failure(pfn, MCE_VECTOR);
1050 static void mce_process_work(struct work_struct *dummy)
1052 mce_notify_process();
1055 #ifdef CONFIG_X86_MCE_INTEL
1056 /***
1057 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
1058 * @cpu: The CPU on which the event occurred.
1059 * @status: Event status information
1061 * This function should be called by the thermal interrupt after the
1062 * event has been processed and the decision was made to log the event
1063 * further.
1065 * The status parameter will be saved to the 'status' field of 'struct mce'
1066 * and historically has been the register value of the
1067 * MSR_IA32_THERMAL_STATUS (Intel) msr.
1069 void mce_log_therm_throt_event(__u64 status)
1071 struct mce m;
1073 mce_setup(&m);
1074 m.bank = MCE_THERMAL_BANK;
1075 m.status = status;
1076 mce_log(&m);
1078 #endif /* CONFIG_X86_MCE_INTEL */
1081 * Periodic polling timer for "silent" machine check errors. If the
1082 * poller finds an MCE, poll 2x faster. When the poller finds no more
1083 * errors, poll 2x slower (up to check_interval seconds).
1085 static int check_interval = 5 * 60; /* 5 minutes */
1087 static DEFINE_PER_CPU(int, next_interval); /* in jiffies */
1088 static DEFINE_PER_CPU(struct timer_list, mce_timer);
1090 static void mcheck_timer(unsigned long data)
1092 struct timer_list *t = &per_cpu(mce_timer, data);
1093 int *n;
1095 WARN_ON(smp_processor_id() != data);
1097 if (mce_available(&current_cpu_data)) {
1098 machine_check_poll(MCP_TIMESTAMP,
1099 &__get_cpu_var(mce_poll_banks));
1103 * Alert userspace if needed. If we logged an MCE, reduce the
1104 * polling interval, otherwise increase the polling interval.
1106 n = &__get_cpu_var(next_interval);
1107 if (mce_notify_irq())
1108 *n = max(*n/2, HZ/100);
1109 else
1110 *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
1112 t->expires = jiffies + *n;
1113 add_timer(t);
1116 static void mce_do_trigger(struct work_struct *work)
1118 call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT);
1121 static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1124 * Notify the user(s) about new machine check events.
1125 * Can be called from interrupt context, but not from machine check/NMI
1126 * context.
1128 int mce_notify_irq(void)
1130 /* Not more than two messages every minute */
1131 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1133 clear_thread_flag(TIF_MCE_NOTIFY);
1135 if (test_and_clear_bit(0, &notify_user)) {
1136 wake_up_interruptible(&mce_wait);
1139 * There is no risk of missing notifications because
1140 * work_pending is always cleared before the function is
1141 * executed.
1143 if (trigger[0] && !work_pending(&mce_trigger_work))
1144 schedule_work(&mce_trigger_work);
1146 if (__ratelimit(&ratelimit))
1147 printk(KERN_INFO "Machine check events logged\n");
1149 return 1;
1151 return 0;
1153 EXPORT_SYMBOL_GPL(mce_notify_irq);
1156 * Initialize Machine Checks for a CPU.
1158 static int mce_cap_init(void)
1160 unsigned b;
1161 u64 cap;
1163 rdmsrl(MSR_IA32_MCG_CAP, cap);
1165 b = cap & MCG_BANKCNT_MASK;
1166 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
1168 if (b > MAX_NR_BANKS) {
1169 printk(KERN_WARNING
1170 "MCE: Using only %u machine check banks out of %u\n",
1171 MAX_NR_BANKS, b);
1172 b = MAX_NR_BANKS;
1175 /* Don't support asymmetric configurations today */
1176 WARN_ON(banks != 0 && b != banks);
1177 banks = b;
1178 if (!bank) {
1179 bank = kmalloc(banks * sizeof(u64), GFP_KERNEL);
1180 if (!bank)
1181 return -ENOMEM;
1182 memset(bank, 0xff, banks * sizeof(u64));
1185 /* Use accurate RIP reporting if available. */
1186 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
1187 rip_msr = MSR_IA32_MCG_EIP;
1189 if (cap & MCG_SER_P)
1190 mce_ser = 1;
1192 return 0;
1195 static void mce_init(void)
1197 mce_banks_t all_banks;
1198 u64 cap;
1199 int i;
1202 * Log the machine checks left over from the previous reset.
1204 bitmap_fill(all_banks, MAX_NR_BANKS);
1205 machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
1207 set_in_cr4(X86_CR4_MCE);
1209 rdmsrl(MSR_IA32_MCG_CAP, cap);
1210 if (cap & MCG_CTL_P)
1211 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1213 for (i = 0; i < banks; i++) {
1214 if (skip_bank_init(i))
1215 continue;
1216 wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]);
1217 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
1221 /* Add per CPU specific workarounds here */
1222 static void mce_cpu_quirks(struct cpuinfo_x86 *c)
1224 /* This should be disabled by the BIOS, but isn't always */
1225 if (c->x86_vendor == X86_VENDOR_AMD) {
1226 if (c->x86 == 15 && banks > 4) {
1228 * disable GART TBL walk error reporting, which
1229 * trips off incorrectly with the IOMMU & 3ware
1230 * & Cerberus:
1232 clear_bit(10, (unsigned long *)&bank[4]);
1234 if (c->x86 <= 17 && mce_bootlog < 0) {
1236 * Lots of broken BIOS around that don't clear them
1237 * by default and leave crap in there. Don't log:
1239 mce_bootlog = 0;
1242 * Various K7s with broken bank 0 around. Always disable
1243 * by default.
1245 if (c->x86 == 6)
1246 bank[0] = 0;
1249 if (c->x86_vendor == X86_VENDOR_INTEL) {
1251 * SDM documents that on family 6 bank 0 should not be written
1252 * because it aliases to another special BIOS controlled
1253 * register.
1254 * But it's not aliased anymore on model 0x1a+
1255 * Don't ignore bank 0 completely because there could be a
1256 * valid event later, merely don't write CTL0.
1259 if (c->x86 == 6 && c->x86_model < 0x1A)
1260 __set_bit(0, &dont_init_banks);
1263 * All newer Intel systems support MCE broadcasting. Enable
1264 * synchronization with a one second timeout.
1266 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1267 monarch_timeout < 0)
1268 monarch_timeout = USEC_PER_SEC;
1270 if (monarch_timeout < 0)
1271 monarch_timeout = 0;
1272 if (mce_bootlog != 0)
1273 mce_panic_timeout = 30;
1276 static void __cpuinit mce_ancient_init(struct cpuinfo_x86 *c)
1278 if (c->x86 != 5)
1279 return;
1280 switch (c->x86_vendor) {
1281 case X86_VENDOR_INTEL:
1282 if (mce_p5_enabled())
1283 intel_p5_mcheck_init(c);
1284 break;
1285 case X86_VENDOR_CENTAUR:
1286 winchip_mcheck_init(c);
1287 break;
1291 static void mce_cpu_features(struct cpuinfo_x86 *c)
1293 switch (c->x86_vendor) {
1294 case X86_VENDOR_INTEL:
1295 mce_intel_feature_init(c);
1296 break;
1297 case X86_VENDOR_AMD:
1298 mce_amd_feature_init(c);
1299 break;
1300 default:
1301 break;
1305 static void mce_init_timer(void)
1307 struct timer_list *t = &__get_cpu_var(mce_timer);
1308 int *n = &__get_cpu_var(next_interval);
1310 *n = check_interval * HZ;
1311 if (!*n)
1312 return;
1313 setup_timer(t, mcheck_timer, smp_processor_id());
1314 t->expires = round_jiffies(jiffies + *n);
1315 add_timer(t);
1319 * Called for each booted CPU to set up machine checks.
1320 * Must be called with preempt off:
1322 void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
1324 if (mce_disabled)
1325 return;
1327 mce_ancient_init(c);
1329 if (!mce_available(c))
1330 return;
1332 if (mce_cap_init() < 0) {
1333 mce_disabled = 1;
1334 return;
1336 mce_cpu_quirks(c);
1338 machine_check_vector = do_machine_check;
1340 mce_init();
1341 mce_cpu_features(c);
1342 mce_init_timer();
1343 INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
1347 * Character device to read and clear the MCE log.
1350 static DEFINE_SPINLOCK(mce_state_lock);
1351 static int open_count; /* #times opened */
1352 static int open_exclu; /* already open exclusive? */
1354 static int mce_open(struct inode *inode, struct file *file)
1356 spin_lock(&mce_state_lock);
1358 if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
1359 spin_unlock(&mce_state_lock);
1361 return -EBUSY;
1364 if (file->f_flags & O_EXCL)
1365 open_exclu = 1;
1366 open_count++;
1368 spin_unlock(&mce_state_lock);
1370 return nonseekable_open(inode, file);
1373 static int mce_release(struct inode *inode, struct file *file)
1375 spin_lock(&mce_state_lock);
1377 open_count--;
1378 open_exclu = 0;
1380 spin_unlock(&mce_state_lock);
1382 return 0;
1385 static void collect_tscs(void *data)
1387 unsigned long *cpu_tsc = (unsigned long *)data;
1389 rdtscll(cpu_tsc[smp_processor_id()]);
1392 static DEFINE_MUTEX(mce_read_mutex);
1394 static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
1395 loff_t *off)
1397 char __user *buf = ubuf;
1398 unsigned long *cpu_tsc;
1399 unsigned prev, next;
1400 int i, err;
1402 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
1403 if (!cpu_tsc)
1404 return -ENOMEM;
1406 mutex_lock(&mce_read_mutex);
1407 next = rcu_dereference(mcelog.next);
1409 /* Only supports full reads right now */
1410 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) {
1411 mutex_unlock(&mce_read_mutex);
1412 kfree(cpu_tsc);
1414 return -EINVAL;
1417 err = 0;
1418 prev = 0;
1419 do {
1420 for (i = prev; i < next; i++) {
1421 unsigned long start = jiffies;
1423 while (!mcelog.entry[i].finished) {
1424 if (time_after_eq(jiffies, start + 2)) {
1425 memset(mcelog.entry + i, 0,
1426 sizeof(struct mce));
1427 goto timeout;
1429 cpu_relax();
1431 smp_rmb();
1432 err |= copy_to_user(buf, mcelog.entry + i,
1433 sizeof(struct mce));
1434 buf += sizeof(struct mce);
1435 timeout:
1439 memset(mcelog.entry + prev, 0,
1440 (next - prev) * sizeof(struct mce));
1441 prev = next;
1442 next = cmpxchg(&mcelog.next, prev, 0);
1443 } while (next != prev);
1445 synchronize_sched();
1448 * Collect entries that were still getting written before the
1449 * synchronize.
1451 on_each_cpu(collect_tscs, cpu_tsc, 1);
1453 for (i = next; i < MCE_LOG_LEN; i++) {
1454 if (mcelog.entry[i].finished &&
1455 mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
1456 err |= copy_to_user(buf, mcelog.entry+i,
1457 sizeof(struct mce));
1458 smp_rmb();
1459 buf += sizeof(struct mce);
1460 memset(&mcelog.entry[i], 0, sizeof(struct mce));
1463 mutex_unlock(&mce_read_mutex);
1464 kfree(cpu_tsc);
1466 return err ? -EFAULT : buf - ubuf;
1469 static unsigned int mce_poll(struct file *file, poll_table *wait)
1471 poll_wait(file, &mce_wait, wait);
1472 if (rcu_dereference(mcelog.next))
1473 return POLLIN | POLLRDNORM;
1474 return 0;
1477 static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
1479 int __user *p = (int __user *)arg;
1481 if (!capable(CAP_SYS_ADMIN))
1482 return -EPERM;
1484 switch (cmd) {
1485 case MCE_GET_RECORD_LEN:
1486 return put_user(sizeof(struct mce), p);
1487 case MCE_GET_LOG_LEN:
1488 return put_user(MCE_LOG_LEN, p);
1489 case MCE_GETCLEAR_FLAGS: {
1490 unsigned flags;
1492 do {
1493 flags = mcelog.flags;
1494 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
1496 return put_user(flags, p);
1498 default:
1499 return -ENOTTY;
1503 /* Modified in mce-inject.c, so not static or const */
1504 struct file_operations mce_chrdev_ops = {
1505 .open = mce_open,
1506 .release = mce_release,
1507 .read = mce_read,
1508 .poll = mce_poll,
1509 .unlocked_ioctl = mce_ioctl,
1511 EXPORT_SYMBOL_GPL(mce_chrdev_ops);
1513 static struct miscdevice mce_log_device = {
1514 MISC_MCELOG_MINOR,
1515 "mcelog",
1516 &mce_chrdev_ops,
1520 * mce=off disables machine check
1521 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1522 * monarchtimeout is how long to wait for other CPUs on machine
1523 * check, or 0 to not wait
1524 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1525 * mce=nobootlog Don't log MCEs from before booting.
1527 static int __init mcheck_enable(char *str)
1529 if (*str == 0)
1530 enable_p5_mce();
1531 if (*str == '=')
1532 str++;
1533 if (!strcmp(str, "off"))
1534 mce_disabled = 1;
1535 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
1536 mce_bootlog = (str[0] == 'b');
1537 else if (isdigit(str[0])) {
1538 get_option(&str, &tolerant);
1539 if (*str == ',') {
1540 ++str;
1541 get_option(&str, &monarch_timeout);
1543 } else {
1544 printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
1545 str);
1546 return 0;
1548 return 1;
1550 __setup("mce", mcheck_enable);
1553 * Sysfs support
1557 * Disable machine checks on suspend and shutdown. We can't really handle
1558 * them later.
1560 static int mce_disable(void)
1562 int i;
1564 for (i = 0; i < banks; i++) {
1565 if (!skip_bank_init(i))
1566 wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
1568 return 0;
1571 static int mce_suspend(struct sys_device *dev, pm_message_t state)
1573 return mce_disable();
1576 static int mce_shutdown(struct sys_device *dev)
1578 return mce_disable();
1582 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
1583 * Only one CPU is active at this time, the others get re-added later using
1584 * CPU hotplug:
1586 static int mce_resume(struct sys_device *dev)
1588 mce_init();
1589 mce_cpu_features(&current_cpu_data);
1591 return 0;
1594 static void mce_cpu_restart(void *data)
1596 del_timer_sync(&__get_cpu_var(mce_timer));
1597 if (mce_available(&current_cpu_data))
1598 mce_init();
1599 mce_init_timer();
1602 /* Reinit MCEs after user configuration changes */
1603 static void mce_restart(void)
1605 on_each_cpu(mce_cpu_restart, NULL, 1);
1608 static struct sysdev_class mce_sysclass = {
1609 .suspend = mce_suspend,
1610 .shutdown = mce_shutdown,
1611 .resume = mce_resume,
1612 .name = "machinecheck",
1615 DEFINE_PER_CPU(struct sys_device, mce_dev);
1617 __cpuinitdata
1618 void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
1620 static struct sysdev_attribute *bank_attrs;
1622 static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
1623 char *buf)
1625 u64 b = bank[attr - bank_attrs];
1627 return sprintf(buf, "%llx\n", b);
1630 static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
1631 const char *buf, size_t size)
1633 u64 new;
1635 if (strict_strtoull(buf, 0, &new) < 0)
1636 return -EINVAL;
1638 bank[attr - bank_attrs] = new;
1639 mce_restart();
1641 return size;
1644 static ssize_t
1645 show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
1647 strcpy(buf, trigger);
1648 strcat(buf, "\n");
1649 return strlen(trigger) + 1;
1652 static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
1653 const char *buf, size_t siz)
1655 char *p;
1656 int len;
1658 strncpy(trigger, buf, sizeof(trigger));
1659 trigger[sizeof(trigger)-1] = 0;
1660 len = strlen(trigger);
1661 p = strchr(trigger, '\n');
1663 if (*p)
1664 *p = 0;
1666 return len;
1669 static ssize_t store_int_with_restart(struct sys_device *s,
1670 struct sysdev_attribute *attr,
1671 const char *buf, size_t size)
1673 ssize_t ret = sysdev_store_int(s, attr, buf, size);
1674 mce_restart();
1675 return ret;
1678 static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
1679 static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
1680 static SYSDEV_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
1682 static struct sysdev_ext_attribute attr_check_interval = {
1683 _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int,
1684 store_int_with_restart),
1685 &check_interval
1688 static struct sysdev_attribute *mce_attrs[] = {
1689 &attr_tolerant.attr, &attr_check_interval.attr, &attr_trigger,
1690 &attr_monarch_timeout.attr,
1691 NULL
1694 static cpumask_var_t mce_dev_initialized;
1696 /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
1697 static __cpuinit int mce_create_device(unsigned int cpu)
1699 int err;
1700 int i;
1702 if (!mce_available(&boot_cpu_data))
1703 return -EIO;
1705 memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
1706 per_cpu(mce_dev, cpu).id = cpu;
1707 per_cpu(mce_dev, cpu).cls = &mce_sysclass;
1709 err = sysdev_register(&per_cpu(mce_dev, cpu));
1710 if (err)
1711 return err;
1713 for (i = 0; mce_attrs[i]; i++) {
1714 err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1715 if (err)
1716 goto error;
1718 for (i = 0; i < banks; i++) {
1719 err = sysdev_create_file(&per_cpu(mce_dev, cpu),
1720 &bank_attrs[i]);
1721 if (err)
1722 goto error2;
1724 cpumask_set_cpu(cpu, mce_dev_initialized);
1726 return 0;
1727 error2:
1728 while (--i >= 0)
1729 sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
1730 error:
1731 while (--i >= 0)
1732 sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1734 sysdev_unregister(&per_cpu(mce_dev, cpu));
1736 return err;
1739 static __cpuinit void mce_remove_device(unsigned int cpu)
1741 int i;
1743 if (!cpumask_test_cpu(cpu, mce_dev_initialized))
1744 return;
1746 for (i = 0; mce_attrs[i]; i++)
1747 sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1749 for (i = 0; i < banks; i++)
1750 sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
1752 sysdev_unregister(&per_cpu(mce_dev, cpu));
1753 cpumask_clear_cpu(cpu, mce_dev_initialized);
1756 /* Make sure there are no machine checks on offlined CPUs. */
1757 static void mce_disable_cpu(void *h)
1759 unsigned long action = *(unsigned long *)h;
1760 int i;
1762 if (!mce_available(&current_cpu_data))
1763 return;
1764 if (!(action & CPU_TASKS_FROZEN))
1765 cmci_clear();
1766 for (i = 0; i < banks; i++) {
1767 if (!skip_bank_init(i))
1768 wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
1772 static void mce_reenable_cpu(void *h)
1774 unsigned long action = *(unsigned long *)h;
1775 int i;
1777 if (!mce_available(&current_cpu_data))
1778 return;
1780 if (!(action & CPU_TASKS_FROZEN))
1781 cmci_reenable();
1782 for (i = 0; i < banks; i++) {
1783 if (!skip_bank_init(i))
1784 wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]);
1788 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
1789 static int __cpuinit
1790 mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
1792 unsigned int cpu = (unsigned long)hcpu;
1793 struct timer_list *t = &per_cpu(mce_timer, cpu);
1795 switch (action) {
1796 case CPU_ONLINE:
1797 case CPU_ONLINE_FROZEN:
1798 mce_create_device(cpu);
1799 if (threshold_cpu_callback)
1800 threshold_cpu_callback(action, cpu);
1801 break;
1802 case CPU_DEAD:
1803 case CPU_DEAD_FROZEN:
1804 if (threshold_cpu_callback)
1805 threshold_cpu_callback(action, cpu);
1806 mce_remove_device(cpu);
1807 break;
1808 case CPU_DOWN_PREPARE:
1809 case CPU_DOWN_PREPARE_FROZEN:
1810 del_timer_sync(t);
1811 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
1812 break;
1813 case CPU_DOWN_FAILED:
1814 case CPU_DOWN_FAILED_FROZEN:
1815 t->expires = round_jiffies(jiffies +
1816 __get_cpu_var(next_interval));
1817 add_timer_on(t, cpu);
1818 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
1819 break;
1820 case CPU_POST_DEAD:
1821 /* intentionally ignoring frozen here */
1822 cmci_rediscover(cpu);
1823 break;
1825 return NOTIFY_OK;
1828 static struct notifier_block mce_cpu_notifier __cpuinitdata = {
1829 .notifier_call = mce_cpu_callback,
1832 static __init int mce_init_banks(void)
1834 int i;
1836 bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks,
1837 GFP_KERNEL);
1838 if (!bank_attrs)
1839 return -ENOMEM;
1841 for (i = 0; i < banks; i++) {
1842 struct sysdev_attribute *a = &bank_attrs[i];
1844 a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i);
1845 if (!a->attr.name)
1846 goto nomem;
1848 a->attr.mode = 0644;
1849 a->show = show_bank;
1850 a->store = set_bank;
1852 return 0;
1854 nomem:
1855 while (--i >= 0)
1856 kfree(bank_attrs[i].attr.name);
1857 kfree(bank_attrs);
1858 bank_attrs = NULL;
1860 return -ENOMEM;
1863 static __init int mce_init_device(void)
1865 int err;
1866 int i = 0;
1868 if (!mce_available(&boot_cpu_data))
1869 return -EIO;
1871 alloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
1873 err = mce_init_banks();
1874 if (err)
1875 return err;
1877 err = sysdev_class_register(&mce_sysclass);
1878 if (err)
1879 return err;
1881 for_each_online_cpu(i) {
1882 err = mce_create_device(i);
1883 if (err)
1884 return err;
1887 register_hotcpu_notifier(&mce_cpu_notifier);
1888 misc_register(&mce_log_device);
1890 return err;
1893 device_initcall(mce_init_device);
1895 #else /* CONFIG_X86_OLD_MCE: */
1897 int nr_mce_banks;
1898 EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */
1900 /* This has to be run for each processor */
1901 void mcheck_init(struct cpuinfo_x86 *c)
1903 if (mce_disabled == 1)
1904 return;
1906 switch (c->x86_vendor) {
1907 case X86_VENDOR_AMD:
1908 amd_mcheck_init(c);
1909 break;
1911 case X86_VENDOR_INTEL:
1912 if (c->x86 == 5)
1913 intel_p5_mcheck_init(c);
1914 if (c->x86 == 6)
1915 intel_p6_mcheck_init(c);
1916 if (c->x86 == 15)
1917 intel_p4_mcheck_init(c);
1918 break;
1920 case X86_VENDOR_CENTAUR:
1921 if (c->x86 == 5)
1922 winchip_mcheck_init(c);
1923 break;
1925 default:
1926 break;
1928 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks);
1931 static int __init mcheck_enable(char *str)
1933 mce_disabled = -1;
1934 return 1;
1937 __setup("mce", mcheck_enable);
1939 #endif /* CONFIG_X86_OLD_MCE */
1942 * Old style boot options parsing. Only for compatibility.
1944 static int __init mcheck_disable(char *str)
1946 mce_disabled = 1;
1947 return 1;
1949 __setup("nomce", mcheck_disable);