staging:iio:adc:ad7606 move to info_mask_(shared_by_type/separate)
[linux/fpc-iii.git] / arch / x86 / kernel / cpu / mcheck / mce.c
blob7bc126346ace7d0946c88039e4bc847244d751df
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 */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/thread_info.h>
14 #include <linux/capability.h>
15 #include <linux/miscdevice.h>
16 #include <linux/ratelimit.h>
17 #include <linux/kallsyms.h>
18 #include <linux/rcupdate.h>
19 #include <linux/kobject.h>
20 #include <linux/uaccess.h>
21 #include <linux/kdebug.h>
22 #include <linux/kernel.h>
23 #include <linux/percpu.h>
24 #include <linux/string.h>
25 #include <linux/device.h>
26 #include <linux/syscore_ops.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/sched.h>
30 #include <linux/sysfs.h>
31 #include <linux/types.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/kmod.h>
35 #include <linux/poll.h>
36 #include <linux/nmi.h>
37 #include <linux/cpu.h>
38 #include <linux/smp.h>
39 #include <linux/fs.h>
40 #include <linux/mm.h>
41 #include <linux/debugfs.h>
42 #include <linux/irq_work.h>
43 #include <linux/export.h>
45 #include <asm/processor.h>
46 #include <asm/mce.h>
47 #include <asm/msr.h>
49 #include "mce-internal.h"
51 static DEFINE_MUTEX(mce_chrdev_read_mutex);
53 #define rcu_dereference_check_mce(p) \
54 rcu_dereference_index_check((p), \
55 rcu_read_lock_sched_held() || \
56 lockdep_is_held(&mce_chrdev_read_mutex))
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/mce.h>
61 #define SPINUNIT 100 /* 100ns */
63 atomic_t mce_entry;
65 DEFINE_PER_CPU(unsigned, mce_exception_count);
67 struct mce_bank *mce_banks __read_mostly;
69 struct mca_config mca_cfg __read_mostly = {
70 .bootlog = -1,
72 * Tolerant levels:
73 * 0: always panic on uncorrected errors, log corrected errors
74 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
75 * 2: SIGBUS or log uncorrected errors (if possible), log corr. errors
76 * 3: never panic or SIGBUS, log all errors (for testing only)
78 .tolerant = 1,
79 .monarch_timeout = -1
82 /* User mode helper program triggered by machine check event */
83 static unsigned long mce_need_notify;
84 static char mce_helper[128];
85 static char *mce_helper_argv[2] = { mce_helper, NULL };
87 static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
89 static DEFINE_PER_CPU(struct mce, mces_seen);
90 static int cpu_missing;
92 /* MCA banks polled by the period polling timer for corrected events */
93 DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
94 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
97 static DEFINE_PER_CPU(struct work_struct, mce_work);
99 static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
102 * CPU/chipset specific EDAC code can register a notifier call here to print
103 * MCE errors in a human-readable form.
105 ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
107 /* Do initial initialization of a struct mce */
108 void mce_setup(struct mce *m)
110 memset(m, 0, sizeof(struct mce));
111 m->cpu = m->extcpu = smp_processor_id();
112 rdtscll(m->tsc);
113 /* We hope get_seconds stays lockless */
114 m->time = get_seconds();
115 m->cpuvendor = boot_cpu_data.x86_vendor;
116 m->cpuid = cpuid_eax(1);
117 m->socketid = cpu_data(m->extcpu).phys_proc_id;
118 m->apicid = cpu_data(m->extcpu).initial_apicid;
119 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
122 DEFINE_PER_CPU(struct mce, injectm);
123 EXPORT_PER_CPU_SYMBOL_GPL(injectm);
126 * Lockless MCE logging infrastructure.
127 * This avoids deadlocks on printk locks without having to break locks. Also
128 * separate MCEs from kernel messages to avoid bogus bug reports.
131 static struct mce_log mcelog = {
132 .signature = MCE_LOG_SIGNATURE,
133 .len = MCE_LOG_LEN,
134 .recordlen = sizeof(struct mce),
137 void mce_log(struct mce *mce)
139 unsigned next, entry;
140 int ret = 0;
142 /* Emit the trace record: */
143 trace_mce_record(mce);
145 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
146 if (ret == NOTIFY_STOP)
147 return;
149 mce->finished = 0;
150 wmb();
151 for (;;) {
152 entry = rcu_dereference_check_mce(mcelog.next);
153 for (;;) {
156 * When the buffer fills up discard new entries.
157 * Assume that the earlier errors are the more
158 * interesting ones:
160 if (entry >= MCE_LOG_LEN) {
161 set_bit(MCE_OVERFLOW,
162 (unsigned long *)&mcelog.flags);
163 return;
165 /* Old left over entry. Skip: */
166 if (mcelog.entry[entry].finished) {
167 entry++;
168 continue;
170 break;
172 smp_rmb();
173 next = entry + 1;
174 if (cmpxchg(&mcelog.next, entry, next) == entry)
175 break;
177 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
178 wmb();
179 mcelog.entry[entry].finished = 1;
180 wmb();
182 mce->finished = 1;
183 set_bit(0, &mce_need_notify);
186 static void drain_mcelog_buffer(void)
188 unsigned int next, i, prev = 0;
190 next = ACCESS_ONCE(mcelog.next);
192 do {
193 struct mce *m;
195 /* drain what was logged during boot */
196 for (i = prev; i < next; i++) {
197 unsigned long start = jiffies;
198 unsigned retries = 1;
200 m = &mcelog.entry[i];
202 while (!m->finished) {
203 if (time_after_eq(jiffies, start + 2*retries))
204 retries++;
206 cpu_relax();
208 if (!m->finished && retries >= 4) {
209 pr_err("skipping error being logged currently!\n");
210 break;
213 smp_rmb();
214 atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
217 memset(mcelog.entry + prev, 0, (next - prev) * sizeof(*m));
218 prev = next;
219 next = cmpxchg(&mcelog.next, prev, 0);
220 } while (next != prev);
224 void mce_register_decode_chain(struct notifier_block *nb)
226 atomic_notifier_chain_register(&x86_mce_decoder_chain, nb);
227 drain_mcelog_buffer();
229 EXPORT_SYMBOL_GPL(mce_register_decode_chain);
231 void mce_unregister_decode_chain(struct notifier_block *nb)
233 atomic_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
235 EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
237 static void print_mce(struct mce *m)
239 int ret = 0;
241 pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
242 m->extcpu, m->mcgstatus, m->bank, m->status);
244 if (m->ip) {
245 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
246 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
247 m->cs, m->ip);
249 if (m->cs == __KERNEL_CS)
250 print_symbol("{%s}", m->ip);
251 pr_cont("\n");
254 pr_emerg(HW_ERR "TSC %llx ", m->tsc);
255 if (m->addr)
256 pr_cont("ADDR %llx ", m->addr);
257 if (m->misc)
258 pr_cont("MISC %llx ", m->misc);
260 pr_cont("\n");
262 * Note this output is parsed by external tools and old fields
263 * should not be changed.
265 pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
266 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
267 cpu_data(m->extcpu).microcode);
270 * Print out human-readable details about the MCE error,
271 * (if the CPU has an implementation for that)
273 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
274 if (ret == NOTIFY_STOP)
275 return;
277 pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
280 #define PANIC_TIMEOUT 5 /* 5 seconds */
282 static atomic_t mce_paniced;
284 static int fake_panic;
285 static atomic_t mce_fake_paniced;
287 /* Panic in progress. Enable interrupts and wait for final IPI */
288 static void wait_for_panic(void)
290 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
292 preempt_disable();
293 local_irq_enable();
294 while (timeout-- > 0)
295 udelay(1);
296 if (panic_timeout == 0)
297 panic_timeout = mca_cfg.panic_timeout;
298 panic("Panicing machine check CPU died");
301 static void mce_panic(char *msg, struct mce *final, char *exp)
303 int i, apei_err = 0;
305 if (!fake_panic) {
307 * Make sure only one CPU runs in machine check panic
309 if (atomic_inc_return(&mce_paniced) > 1)
310 wait_for_panic();
311 barrier();
313 bust_spinlocks(1);
314 console_verbose();
315 } else {
316 /* Don't log too much for fake panic */
317 if (atomic_inc_return(&mce_fake_paniced) > 1)
318 return;
320 /* First print corrected ones that are still unlogged */
321 for (i = 0; i < MCE_LOG_LEN; i++) {
322 struct mce *m = &mcelog.entry[i];
323 if (!(m->status & MCI_STATUS_VAL))
324 continue;
325 if (!(m->status & MCI_STATUS_UC)) {
326 print_mce(m);
327 if (!apei_err)
328 apei_err = apei_write_mce(m);
331 /* Now print uncorrected but with the final one last */
332 for (i = 0; i < MCE_LOG_LEN; i++) {
333 struct mce *m = &mcelog.entry[i];
334 if (!(m->status & MCI_STATUS_VAL))
335 continue;
336 if (!(m->status & MCI_STATUS_UC))
337 continue;
338 if (!final || memcmp(m, final, sizeof(struct mce))) {
339 print_mce(m);
340 if (!apei_err)
341 apei_err = apei_write_mce(m);
344 if (final) {
345 print_mce(final);
346 if (!apei_err)
347 apei_err = apei_write_mce(final);
349 if (cpu_missing)
350 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
351 if (exp)
352 pr_emerg(HW_ERR "Machine check: %s\n", exp);
353 if (!fake_panic) {
354 if (panic_timeout == 0)
355 panic_timeout = mca_cfg.panic_timeout;
356 panic(msg);
357 } else
358 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
361 /* Support code for software error injection */
363 static int msr_to_offset(u32 msr)
365 unsigned bank = __this_cpu_read(injectm.bank);
367 if (msr == mca_cfg.rip_msr)
368 return offsetof(struct mce, ip);
369 if (msr == MSR_IA32_MCx_STATUS(bank))
370 return offsetof(struct mce, status);
371 if (msr == MSR_IA32_MCx_ADDR(bank))
372 return offsetof(struct mce, addr);
373 if (msr == MSR_IA32_MCx_MISC(bank))
374 return offsetof(struct mce, misc);
375 if (msr == MSR_IA32_MCG_STATUS)
376 return offsetof(struct mce, mcgstatus);
377 return -1;
380 /* MSR access wrappers used for error injection */
381 static u64 mce_rdmsrl(u32 msr)
383 u64 v;
385 if (__this_cpu_read(injectm.finished)) {
386 int offset = msr_to_offset(msr);
388 if (offset < 0)
389 return 0;
390 return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
393 if (rdmsrl_safe(msr, &v)) {
394 WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
396 * Return zero in case the access faulted. This should
397 * not happen normally but can happen if the CPU does
398 * something weird, or if the code is buggy.
400 v = 0;
403 return v;
406 static void mce_wrmsrl(u32 msr, u64 v)
408 if (__this_cpu_read(injectm.finished)) {
409 int offset = msr_to_offset(msr);
411 if (offset >= 0)
412 *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
413 return;
415 wrmsrl(msr, v);
419 * Collect all global (w.r.t. this processor) status about this machine
420 * check into our "mce" struct so that we can use it later to assess
421 * the severity of the problem as we read per-bank specific details.
423 static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
425 mce_setup(m);
427 m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
428 if (regs) {
430 * Get the address of the instruction at the time of
431 * the machine check error.
433 if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
434 m->ip = regs->ip;
435 m->cs = regs->cs;
438 * When in VM86 mode make the cs look like ring 3
439 * always. This is a lie, but it's better than passing
440 * the additional vm86 bit around everywhere.
442 if (v8086_mode(regs))
443 m->cs |= 3;
445 /* Use accurate RIP reporting if available. */
446 if (mca_cfg.rip_msr)
447 m->ip = mce_rdmsrl(mca_cfg.rip_msr);
452 * Simple lockless ring to communicate PFNs from the exception handler with the
453 * process context work function. This is vastly simplified because there's
454 * only a single reader and a single writer.
456 #define MCE_RING_SIZE 16 /* we use one entry less */
458 struct mce_ring {
459 unsigned short start;
460 unsigned short end;
461 unsigned long ring[MCE_RING_SIZE];
463 static DEFINE_PER_CPU(struct mce_ring, mce_ring);
465 /* Runs with CPU affinity in workqueue */
466 static int mce_ring_empty(void)
468 struct mce_ring *r = &__get_cpu_var(mce_ring);
470 return r->start == r->end;
473 static int mce_ring_get(unsigned long *pfn)
475 struct mce_ring *r;
476 int ret = 0;
478 *pfn = 0;
479 get_cpu();
480 r = &__get_cpu_var(mce_ring);
481 if (r->start == r->end)
482 goto out;
483 *pfn = r->ring[r->start];
484 r->start = (r->start + 1) % MCE_RING_SIZE;
485 ret = 1;
486 out:
487 put_cpu();
488 return ret;
491 /* Always runs in MCE context with preempt off */
492 static int mce_ring_add(unsigned long pfn)
494 struct mce_ring *r = &__get_cpu_var(mce_ring);
495 unsigned next;
497 next = (r->end + 1) % MCE_RING_SIZE;
498 if (next == r->start)
499 return -1;
500 r->ring[r->end] = pfn;
501 wmb();
502 r->end = next;
503 return 0;
506 int mce_available(struct cpuinfo_x86 *c)
508 if (mca_cfg.disabled)
509 return 0;
510 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
513 static void mce_schedule_work(void)
515 if (!mce_ring_empty())
516 schedule_work(&__get_cpu_var(mce_work));
519 DEFINE_PER_CPU(struct irq_work, mce_irq_work);
521 static void mce_irq_work_cb(struct irq_work *entry)
523 mce_notify_irq();
524 mce_schedule_work();
527 static void mce_report_event(struct pt_regs *regs)
529 if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
530 mce_notify_irq();
532 * Triggering the work queue here is just an insurance
533 * policy in case the syscall exit notify handler
534 * doesn't run soon enough or ends up running on the
535 * wrong CPU (can happen when audit sleeps)
537 mce_schedule_work();
538 return;
541 irq_work_queue(&__get_cpu_var(mce_irq_work));
545 * Read ADDR and MISC registers.
547 static void mce_read_aux(struct mce *m, int i)
549 if (m->status & MCI_STATUS_MISCV)
550 m->misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
551 if (m->status & MCI_STATUS_ADDRV) {
552 m->addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
555 * Mask the reported address by the reported granularity.
557 if (mca_cfg.ser && (m->status & MCI_STATUS_MISCV)) {
558 u8 shift = MCI_MISC_ADDR_LSB(m->misc);
559 m->addr >>= shift;
560 m->addr <<= shift;
565 DEFINE_PER_CPU(unsigned, mce_poll_count);
568 * Poll for corrected events or events that happened before reset.
569 * Those are just logged through /dev/mcelog.
571 * This is executed in standard interrupt context.
573 * Note: spec recommends to panic for fatal unsignalled
574 * errors here. However this would be quite problematic --
575 * we would need to reimplement the Monarch handling and
576 * it would mess up the exclusion between exception handler
577 * and poll hander -- * so we skip this for now.
578 * These cases should not happen anyways, or only when the CPU
579 * is already totally * confused. In this case it's likely it will
580 * not fully execute the machine check handler either.
582 void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
584 struct mce m;
585 int i;
587 this_cpu_inc(mce_poll_count);
589 mce_gather_info(&m, NULL);
591 for (i = 0; i < mca_cfg.banks; i++) {
592 if (!mce_banks[i].ctl || !test_bit(i, *b))
593 continue;
595 m.misc = 0;
596 m.addr = 0;
597 m.bank = i;
598 m.tsc = 0;
600 barrier();
601 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
602 if (!(m.status & MCI_STATUS_VAL))
603 continue;
606 * Uncorrected or signalled events are handled by the exception
607 * handler when it is enabled, so don't process those here.
609 * TBD do the same check for MCI_STATUS_EN here?
611 if (!(flags & MCP_UC) &&
612 (m.status & (mca_cfg.ser ? MCI_STATUS_S : MCI_STATUS_UC)))
613 continue;
615 mce_read_aux(&m, i);
617 if (!(flags & MCP_TIMESTAMP))
618 m.tsc = 0;
620 * Don't get the IP here because it's unlikely to
621 * have anything to do with the actual error location.
623 if (!(flags & MCP_DONTLOG) && !mca_cfg.dont_log_ce)
624 mce_log(&m);
627 * Clear state for this bank.
629 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
633 * Don't clear MCG_STATUS here because it's only defined for
634 * exceptions.
637 sync_core();
639 EXPORT_SYMBOL_GPL(machine_check_poll);
642 * Do a quick check if any of the events requires a panic.
643 * This decides if we keep the events around or clear them.
645 static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp,
646 struct pt_regs *regs)
648 int i, ret = 0;
650 for (i = 0; i < mca_cfg.banks; i++) {
651 m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
652 if (m->status & MCI_STATUS_VAL) {
653 __set_bit(i, validp);
654 if (quirk_no_way_out)
655 quirk_no_way_out(i, m, regs);
657 if (mce_severity(m, mca_cfg.tolerant, msg) >= MCE_PANIC_SEVERITY)
658 ret = 1;
660 return ret;
664 * Variable to establish order between CPUs while scanning.
665 * Each CPU spins initially until executing is equal its number.
667 static atomic_t mce_executing;
670 * Defines order of CPUs on entry. First CPU becomes Monarch.
672 static atomic_t mce_callin;
675 * Check if a timeout waiting for other CPUs happened.
677 static int mce_timed_out(u64 *t)
680 * The others already did panic for some reason.
681 * Bail out like in a timeout.
682 * rmb() to tell the compiler that system_state
683 * might have been modified by someone else.
685 rmb();
686 if (atomic_read(&mce_paniced))
687 wait_for_panic();
688 if (!mca_cfg.monarch_timeout)
689 goto out;
690 if ((s64)*t < SPINUNIT) {
691 /* CHECKME: Make panic default for 1 too? */
692 if (mca_cfg.tolerant < 1)
693 mce_panic("Timeout synchronizing machine check over CPUs",
694 NULL, NULL);
695 cpu_missing = 1;
696 return 1;
698 *t -= SPINUNIT;
699 out:
700 touch_nmi_watchdog();
701 return 0;
705 * The Monarch's reign. The Monarch is the CPU who entered
706 * the machine check handler first. It waits for the others to
707 * raise the exception too and then grades them. When any
708 * error is fatal panic. Only then let the others continue.
710 * The other CPUs entering the MCE handler will be controlled by the
711 * Monarch. They are called Subjects.
713 * This way we prevent any potential data corruption in a unrecoverable case
714 * and also makes sure always all CPU's errors are examined.
716 * Also this detects the case of a machine check event coming from outer
717 * space (not detected by any CPUs) In this case some external agent wants
718 * us to shut down, so panic too.
720 * The other CPUs might still decide to panic if the handler happens
721 * in a unrecoverable place, but in this case the system is in a semi-stable
722 * state and won't corrupt anything by itself. It's ok to let the others
723 * continue for a bit first.
725 * All the spin loops have timeouts; when a timeout happens a CPU
726 * typically elects itself to be Monarch.
728 static void mce_reign(void)
730 int cpu;
731 struct mce *m = NULL;
732 int global_worst = 0;
733 char *msg = NULL;
734 char *nmsg = NULL;
737 * This CPU is the Monarch and the other CPUs have run
738 * through their handlers.
739 * Grade the severity of the errors of all the CPUs.
741 for_each_possible_cpu(cpu) {
742 int severity = mce_severity(&per_cpu(mces_seen, cpu),
743 mca_cfg.tolerant,
744 &nmsg);
745 if (severity > global_worst) {
746 msg = nmsg;
747 global_worst = severity;
748 m = &per_cpu(mces_seen, cpu);
753 * Cannot recover? Panic here then.
754 * This dumps all the mces in the log buffer and stops the
755 * other CPUs.
757 if (m && global_worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3)
758 mce_panic("Fatal Machine check", m, msg);
761 * For UC somewhere we let the CPU who detects it handle it.
762 * Also must let continue the others, otherwise the handling
763 * CPU could deadlock on a lock.
767 * No machine check event found. Must be some external
768 * source or one CPU is hung. Panic.
770 if (global_worst <= MCE_KEEP_SEVERITY && mca_cfg.tolerant < 3)
771 mce_panic("Machine check from unknown source", NULL, NULL);
774 * Now clear all the mces_seen so that they don't reappear on
775 * the next mce.
777 for_each_possible_cpu(cpu)
778 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
781 static atomic_t global_nwo;
784 * Start of Monarch synchronization. This waits until all CPUs have
785 * entered the exception handler and then determines if any of them
786 * saw a fatal event that requires panic. Then it executes them
787 * in the entry order.
788 * TBD double check parallel CPU hotunplug
790 static int mce_start(int *no_way_out)
792 int order;
793 int cpus = num_online_cpus();
794 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
796 if (!timeout)
797 return -1;
799 atomic_add(*no_way_out, &global_nwo);
801 * global_nwo should be updated before mce_callin
803 smp_wmb();
804 order = atomic_inc_return(&mce_callin);
807 * Wait for everyone.
809 while (atomic_read(&mce_callin) != cpus) {
810 if (mce_timed_out(&timeout)) {
811 atomic_set(&global_nwo, 0);
812 return -1;
814 ndelay(SPINUNIT);
818 * mce_callin should be read before global_nwo
820 smp_rmb();
822 if (order == 1) {
824 * Monarch: Starts executing now, the others wait.
826 atomic_set(&mce_executing, 1);
827 } else {
829 * Subject: Now start the scanning loop one by one in
830 * the original callin order.
831 * This way when there are any shared banks it will be
832 * only seen by one CPU before cleared, avoiding duplicates.
834 while (atomic_read(&mce_executing) < order) {
835 if (mce_timed_out(&timeout)) {
836 atomic_set(&global_nwo, 0);
837 return -1;
839 ndelay(SPINUNIT);
844 * Cache the global no_way_out state.
846 *no_way_out = atomic_read(&global_nwo);
848 return order;
852 * Synchronize between CPUs after main scanning loop.
853 * This invokes the bulk of the Monarch processing.
855 static int mce_end(int order)
857 int ret = -1;
858 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
860 if (!timeout)
861 goto reset;
862 if (order < 0)
863 goto reset;
866 * Allow others to run.
868 atomic_inc(&mce_executing);
870 if (order == 1) {
871 /* CHECKME: Can this race with a parallel hotplug? */
872 int cpus = num_online_cpus();
875 * Monarch: Wait for everyone to go through their scanning
876 * loops.
878 while (atomic_read(&mce_executing) <= cpus) {
879 if (mce_timed_out(&timeout))
880 goto reset;
881 ndelay(SPINUNIT);
884 mce_reign();
885 barrier();
886 ret = 0;
887 } else {
889 * Subject: Wait for Monarch to finish.
891 while (atomic_read(&mce_executing) != 0) {
892 if (mce_timed_out(&timeout))
893 goto reset;
894 ndelay(SPINUNIT);
898 * Don't reset anything. That's done by the Monarch.
900 return 0;
904 * Reset all global state.
906 reset:
907 atomic_set(&global_nwo, 0);
908 atomic_set(&mce_callin, 0);
909 barrier();
912 * Let others run again.
914 atomic_set(&mce_executing, 0);
915 return ret;
919 * Check if the address reported by the CPU is in a format we can parse.
920 * It would be possible to add code for most other cases, but all would
921 * be somewhat complicated (e.g. segment offset would require an instruction
922 * parser). So only support physical addresses up to page granuality for now.
924 static int mce_usable_address(struct mce *m)
926 if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
927 return 0;
928 if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
929 return 0;
930 if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
931 return 0;
932 return 1;
935 static void mce_clear_state(unsigned long *toclear)
937 int i;
939 for (i = 0; i < mca_cfg.banks; i++) {
940 if (test_bit(i, toclear))
941 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
946 * Need to save faulting physical address associated with a process
947 * in the machine check handler some place where we can grab it back
948 * later in mce_notify_process()
950 #define MCE_INFO_MAX 16
952 struct mce_info {
953 atomic_t inuse;
954 struct task_struct *t;
955 __u64 paddr;
956 int restartable;
957 } mce_info[MCE_INFO_MAX];
959 static void mce_save_info(__u64 addr, int c)
961 struct mce_info *mi;
963 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++) {
964 if (atomic_cmpxchg(&mi->inuse, 0, 1) == 0) {
965 mi->t = current;
966 mi->paddr = addr;
967 mi->restartable = c;
968 return;
972 mce_panic("Too many concurrent recoverable errors", NULL, NULL);
975 static struct mce_info *mce_find_info(void)
977 struct mce_info *mi;
979 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++)
980 if (atomic_read(&mi->inuse) && mi->t == current)
981 return mi;
982 return NULL;
985 static void mce_clear_info(struct mce_info *mi)
987 atomic_set(&mi->inuse, 0);
991 * The actual machine check handler. This only handles real
992 * exceptions when something got corrupted coming in through int 18.
994 * This is executed in NMI context not subject to normal locking rules. This
995 * implies that most kernel services cannot be safely used. Don't even
996 * think about putting a printk in there!
998 * On Intel systems this is entered on all CPUs in parallel through
999 * MCE broadcast. However some CPUs might be broken beyond repair,
1000 * so be always careful when synchronizing with others.
1002 void do_machine_check(struct pt_regs *regs, long error_code)
1004 struct mca_config *cfg = &mca_cfg;
1005 struct mce m, *final;
1006 int i;
1007 int worst = 0;
1008 int severity;
1010 * Establish sequential order between the CPUs entering the machine
1011 * check handler.
1013 int order;
1015 * If no_way_out gets set, there is no safe way to recover from this
1016 * MCE. If mca_cfg.tolerant is cranked up, we'll try anyway.
1018 int no_way_out = 0;
1020 * If kill_it gets set, there might be a way to recover from this
1021 * error.
1023 int kill_it = 0;
1024 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
1025 DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
1026 char *msg = "Unknown";
1028 atomic_inc(&mce_entry);
1030 this_cpu_inc(mce_exception_count);
1032 if (!cfg->banks)
1033 goto out;
1035 mce_gather_info(&m, regs);
1037 final = &__get_cpu_var(mces_seen);
1038 *final = m;
1040 memset(valid_banks, 0, sizeof(valid_banks));
1041 no_way_out = mce_no_way_out(&m, &msg, valid_banks, regs);
1043 barrier();
1046 * When no restart IP might need to kill or panic.
1047 * Assume the worst for now, but if we find the
1048 * severity is MCE_AR_SEVERITY we have other options.
1050 if (!(m.mcgstatus & MCG_STATUS_RIPV))
1051 kill_it = 1;
1054 * Go through all the banks in exclusion of the other CPUs.
1055 * This way we don't report duplicated events on shared banks
1056 * because the first one to see it will clear it.
1058 order = mce_start(&no_way_out);
1059 for (i = 0; i < cfg->banks; i++) {
1060 __clear_bit(i, toclear);
1061 if (!test_bit(i, valid_banks))
1062 continue;
1063 if (!mce_banks[i].ctl)
1064 continue;
1066 m.misc = 0;
1067 m.addr = 0;
1068 m.bank = i;
1070 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
1071 if ((m.status & MCI_STATUS_VAL) == 0)
1072 continue;
1075 * Non uncorrected or non signaled errors are handled by
1076 * machine_check_poll. Leave them alone, unless this panics.
1078 if (!(m.status & (cfg->ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
1079 !no_way_out)
1080 continue;
1083 * Set taint even when machine check was not enabled.
1085 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
1087 severity = mce_severity(&m, cfg->tolerant, NULL);
1090 * When machine check was for corrected handler don't touch,
1091 * unless we're panicing.
1093 if (severity == MCE_KEEP_SEVERITY && !no_way_out)
1094 continue;
1095 __set_bit(i, toclear);
1096 if (severity == MCE_NO_SEVERITY) {
1098 * Machine check event was not enabled. Clear, but
1099 * ignore.
1101 continue;
1104 mce_read_aux(&m, i);
1107 * Action optional error. Queue address for later processing.
1108 * When the ring overflows we just ignore the AO error.
1109 * RED-PEN add some logging mechanism when
1110 * usable_address or mce_add_ring fails.
1111 * RED-PEN don't ignore overflow for mca_cfg.tolerant == 0
1113 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
1114 mce_ring_add(m.addr >> PAGE_SHIFT);
1116 mce_log(&m);
1118 if (severity > worst) {
1119 *final = m;
1120 worst = severity;
1124 /* mce_clear_state will clear *final, save locally for use later */
1125 m = *final;
1127 if (!no_way_out)
1128 mce_clear_state(toclear);
1131 * Do most of the synchronization with other CPUs.
1132 * When there's any problem use only local no_way_out state.
1134 if (mce_end(order) < 0)
1135 no_way_out = worst >= MCE_PANIC_SEVERITY;
1138 * At insane "tolerant" levels we take no action. Otherwise
1139 * we only die if we have no other choice. For less serious
1140 * issues we try to recover, or limit damage to the current
1141 * process.
1143 if (cfg->tolerant < 3) {
1144 if (no_way_out)
1145 mce_panic("Fatal machine check on current CPU", &m, msg);
1146 if (worst == MCE_AR_SEVERITY) {
1147 /* schedule action before return to userland */
1148 mce_save_info(m.addr, m.mcgstatus & MCG_STATUS_RIPV);
1149 set_thread_flag(TIF_MCE_NOTIFY);
1150 } else if (kill_it) {
1151 force_sig(SIGBUS, current);
1155 if (worst > 0)
1156 mce_report_event(regs);
1157 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
1158 out:
1159 atomic_dec(&mce_entry);
1160 sync_core();
1162 EXPORT_SYMBOL_GPL(do_machine_check);
1164 #ifndef CONFIG_MEMORY_FAILURE
1165 int memory_failure(unsigned long pfn, int vector, int flags)
1167 /* mce_severity() should not hand us an ACTION_REQUIRED error */
1168 BUG_ON(flags & MF_ACTION_REQUIRED);
1169 pr_err("Uncorrected memory error in page 0x%lx ignored\n"
1170 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
1171 pfn);
1173 return 0;
1175 #endif
1178 * Called in process context that interrupted by MCE and marked with
1179 * TIF_MCE_NOTIFY, just before returning to erroneous userland.
1180 * This code is allowed to sleep.
1181 * Attempt possible recovery such as calling the high level VM handler to
1182 * process any corrupted pages, and kill/signal current process if required.
1183 * Action required errors are handled here.
1185 void mce_notify_process(void)
1187 unsigned long pfn;
1188 struct mce_info *mi = mce_find_info();
1189 int flags = MF_ACTION_REQUIRED;
1191 if (!mi)
1192 mce_panic("Lost physical address for unconsumed uncorrectable error", NULL, NULL);
1193 pfn = mi->paddr >> PAGE_SHIFT;
1195 clear_thread_flag(TIF_MCE_NOTIFY);
1197 pr_err("Uncorrected hardware memory error in user-access at %llx",
1198 mi->paddr);
1200 * We must call memory_failure() here even if the current process is
1201 * doomed. We still need to mark the page as poisoned and alert any
1202 * other users of the page.
1204 if (!mi->restartable)
1205 flags |= MF_MUST_KILL;
1206 if (memory_failure(pfn, MCE_VECTOR, flags) < 0) {
1207 pr_err("Memory error not recovered");
1208 force_sig(SIGBUS, current);
1210 mce_clear_info(mi);
1214 * Action optional processing happens here (picking up
1215 * from the list of faulting pages that do_machine_check()
1216 * placed into the "ring").
1218 static void mce_process_work(struct work_struct *dummy)
1220 unsigned long pfn;
1222 while (mce_ring_get(&pfn))
1223 memory_failure(pfn, MCE_VECTOR, 0);
1226 #ifdef CONFIG_X86_MCE_INTEL
1227 /***
1228 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
1229 * @cpu: The CPU on which the event occurred.
1230 * @status: Event status information
1232 * This function should be called by the thermal interrupt after the
1233 * event has been processed and the decision was made to log the event
1234 * further.
1236 * The status parameter will be saved to the 'status' field of 'struct mce'
1237 * and historically has been the register value of the
1238 * MSR_IA32_THERMAL_STATUS (Intel) msr.
1240 void mce_log_therm_throt_event(__u64 status)
1242 struct mce m;
1244 mce_setup(&m);
1245 m.bank = MCE_THERMAL_BANK;
1246 m.status = status;
1247 mce_log(&m);
1249 #endif /* CONFIG_X86_MCE_INTEL */
1252 * Periodic polling timer for "silent" machine check errors. If the
1253 * poller finds an MCE, poll 2x faster. When the poller finds no more
1254 * errors, poll 2x slower (up to check_interval seconds).
1256 static unsigned long check_interval = 5 * 60; /* 5 minutes */
1258 static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
1259 static DEFINE_PER_CPU(struct timer_list, mce_timer);
1261 static unsigned long mce_adjust_timer_default(unsigned long interval)
1263 return interval;
1266 static unsigned long (*mce_adjust_timer)(unsigned long interval) =
1267 mce_adjust_timer_default;
1269 static void mce_timer_fn(unsigned long data)
1271 struct timer_list *t = &__get_cpu_var(mce_timer);
1272 unsigned long iv;
1274 WARN_ON(smp_processor_id() != data);
1276 if (mce_available(__this_cpu_ptr(&cpu_info))) {
1277 machine_check_poll(MCP_TIMESTAMP,
1278 &__get_cpu_var(mce_poll_banks));
1279 mce_intel_cmci_poll();
1283 * Alert userspace if needed. If we logged an MCE, reduce the
1284 * polling interval, otherwise increase the polling interval.
1286 iv = __this_cpu_read(mce_next_interval);
1287 if (mce_notify_irq()) {
1288 iv = max(iv / 2, (unsigned long) HZ/100);
1289 } else {
1290 iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
1291 iv = mce_adjust_timer(iv);
1293 __this_cpu_write(mce_next_interval, iv);
1294 /* Might have become 0 after CMCI storm subsided */
1295 if (iv) {
1296 t->expires = jiffies + iv;
1297 add_timer_on(t, smp_processor_id());
1302 * Ensure that the timer is firing in @interval from now.
1304 void mce_timer_kick(unsigned long interval)
1306 struct timer_list *t = &__get_cpu_var(mce_timer);
1307 unsigned long when = jiffies + interval;
1308 unsigned long iv = __this_cpu_read(mce_next_interval);
1310 if (timer_pending(t)) {
1311 if (time_before(when, t->expires))
1312 mod_timer_pinned(t, when);
1313 } else {
1314 t->expires = round_jiffies(when);
1315 add_timer_on(t, smp_processor_id());
1317 if (interval < iv)
1318 __this_cpu_write(mce_next_interval, interval);
1321 /* Must not be called in IRQ context where del_timer_sync() can deadlock */
1322 static void mce_timer_delete_all(void)
1324 int cpu;
1326 for_each_online_cpu(cpu)
1327 del_timer_sync(&per_cpu(mce_timer, cpu));
1330 static void mce_do_trigger(struct work_struct *work)
1332 call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
1335 static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1338 * Notify the user(s) about new machine check events.
1339 * Can be called from interrupt context, but not from machine check/NMI
1340 * context.
1342 int mce_notify_irq(void)
1344 /* Not more than two messages every minute */
1345 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1347 if (test_and_clear_bit(0, &mce_need_notify)) {
1348 /* wake processes polling /dev/mcelog */
1349 wake_up_interruptible(&mce_chrdev_wait);
1351 if (mce_helper[0])
1352 schedule_work(&mce_trigger_work);
1354 if (__ratelimit(&ratelimit))
1355 pr_info(HW_ERR "Machine check events logged\n");
1357 return 1;
1359 return 0;
1361 EXPORT_SYMBOL_GPL(mce_notify_irq);
1363 static int __cpuinit __mcheck_cpu_mce_banks_init(void)
1365 int i;
1366 u8 num_banks = mca_cfg.banks;
1368 mce_banks = kzalloc(num_banks * sizeof(struct mce_bank), GFP_KERNEL);
1369 if (!mce_banks)
1370 return -ENOMEM;
1372 for (i = 0; i < num_banks; i++) {
1373 struct mce_bank *b = &mce_banks[i];
1375 b->ctl = -1ULL;
1376 b->init = 1;
1378 return 0;
1382 * Initialize Machine Checks for a CPU.
1384 static int __cpuinit __mcheck_cpu_cap_init(void)
1386 unsigned b;
1387 u64 cap;
1389 rdmsrl(MSR_IA32_MCG_CAP, cap);
1391 b = cap & MCG_BANKCNT_MASK;
1392 if (!mca_cfg.banks)
1393 pr_info("CPU supports %d MCE banks\n", b);
1395 if (b > MAX_NR_BANKS) {
1396 pr_warn("Using only %u machine check banks out of %u\n",
1397 MAX_NR_BANKS, b);
1398 b = MAX_NR_BANKS;
1401 /* Don't support asymmetric configurations today */
1402 WARN_ON(mca_cfg.banks != 0 && b != mca_cfg.banks);
1403 mca_cfg.banks = b;
1405 if (!mce_banks) {
1406 int err = __mcheck_cpu_mce_banks_init();
1408 if (err)
1409 return err;
1412 /* Use accurate RIP reporting if available. */
1413 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
1414 mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
1416 if (cap & MCG_SER_P)
1417 mca_cfg.ser = true;
1419 return 0;
1422 static void __mcheck_cpu_init_generic(void)
1424 enum mcp_flags m_fl = 0;
1425 mce_banks_t all_banks;
1426 u64 cap;
1427 int i;
1429 if (!mca_cfg.bootlog)
1430 m_fl = MCP_DONTLOG;
1433 * Log the machine checks left over from the previous reset.
1435 bitmap_fill(all_banks, MAX_NR_BANKS);
1436 machine_check_poll(MCP_UC | m_fl, &all_banks);
1438 set_in_cr4(X86_CR4_MCE);
1440 rdmsrl(MSR_IA32_MCG_CAP, cap);
1441 if (cap & MCG_CTL_P)
1442 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1444 for (i = 0; i < mca_cfg.banks; i++) {
1445 struct mce_bank *b = &mce_banks[i];
1447 if (!b->init)
1448 continue;
1449 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
1450 wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
1455 * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
1456 * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
1457 * Vol 3B Table 15-20). But this confuses both the code that determines
1458 * whether the machine check occurred in kernel or user mode, and also
1459 * the severity assessment code. Pretend that EIPV was set, and take the
1460 * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
1462 static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
1464 if (bank != 0)
1465 return;
1466 if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
1467 return;
1468 if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
1469 MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
1470 MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
1471 MCACOD)) !=
1472 (MCI_STATUS_UC|MCI_STATUS_EN|
1473 MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
1474 MCI_STATUS_AR|MCACOD_INSTR))
1475 return;
1477 m->mcgstatus |= MCG_STATUS_EIPV;
1478 m->ip = regs->ip;
1479 m->cs = regs->cs;
1482 /* Add per CPU specific workarounds here */
1483 static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
1485 struct mca_config *cfg = &mca_cfg;
1487 if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
1488 pr_info("unknown CPU type - not enabling MCE support\n");
1489 return -EOPNOTSUPP;
1492 /* This should be disabled by the BIOS, but isn't always */
1493 if (c->x86_vendor == X86_VENDOR_AMD) {
1494 if (c->x86 == 15 && cfg->banks > 4) {
1496 * disable GART TBL walk error reporting, which
1497 * trips off incorrectly with the IOMMU & 3ware
1498 * & Cerberus:
1500 clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
1502 if (c->x86 <= 17 && cfg->bootlog < 0) {
1504 * Lots of broken BIOS around that don't clear them
1505 * by default and leave crap in there. Don't log:
1507 cfg->bootlog = 0;
1510 * Various K7s with broken bank 0 around. Always disable
1511 * by default.
1513 if (c->x86 == 6 && cfg->banks > 0)
1514 mce_banks[0].ctl = 0;
1517 * Turn off MC4_MISC thresholding banks on those models since
1518 * they're not supported there.
1520 if (c->x86 == 0x15 &&
1521 (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
1522 int i;
1523 u64 val, hwcr;
1524 bool need_toggle;
1525 u32 msrs[] = {
1526 0x00000413, /* MC4_MISC0 */
1527 0xc0000408, /* MC4_MISC1 */
1530 rdmsrl(MSR_K7_HWCR, hwcr);
1532 /* McStatusWrEn has to be set */
1533 need_toggle = !(hwcr & BIT(18));
1535 if (need_toggle)
1536 wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
1538 for (i = 0; i < ARRAY_SIZE(msrs); i++) {
1539 rdmsrl(msrs[i], val);
1541 /* CntP bit set? */
1542 if (val & BIT_64(62)) {
1543 val &= ~BIT_64(62);
1544 wrmsrl(msrs[i], val);
1548 /* restore old settings */
1549 if (need_toggle)
1550 wrmsrl(MSR_K7_HWCR, hwcr);
1554 if (c->x86_vendor == X86_VENDOR_INTEL) {
1556 * SDM documents that on family 6 bank 0 should not be written
1557 * because it aliases to another special BIOS controlled
1558 * register.
1559 * But it's not aliased anymore on model 0x1a+
1560 * Don't ignore bank 0 completely because there could be a
1561 * valid event later, merely don't write CTL0.
1564 if (c->x86 == 6 && c->x86_model < 0x1A && cfg->banks > 0)
1565 mce_banks[0].init = 0;
1568 * All newer Intel systems support MCE broadcasting. Enable
1569 * synchronization with a one second timeout.
1571 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1572 cfg->monarch_timeout < 0)
1573 cfg->monarch_timeout = USEC_PER_SEC;
1576 * There are also broken BIOSes on some Pentium M and
1577 * earlier systems:
1579 if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
1580 cfg->bootlog = 0;
1582 if (c->x86 == 6 && c->x86_model == 45)
1583 quirk_no_way_out = quirk_sandybridge_ifu;
1585 if (cfg->monarch_timeout < 0)
1586 cfg->monarch_timeout = 0;
1587 if (cfg->bootlog != 0)
1588 cfg->panic_timeout = 30;
1590 return 0;
1593 static int __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
1595 if (c->x86 != 5)
1596 return 0;
1598 switch (c->x86_vendor) {
1599 case X86_VENDOR_INTEL:
1600 intel_p5_mcheck_init(c);
1601 return 1;
1602 break;
1603 case X86_VENDOR_CENTAUR:
1604 winchip_mcheck_init(c);
1605 return 1;
1606 break;
1609 return 0;
1612 static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
1614 switch (c->x86_vendor) {
1615 case X86_VENDOR_INTEL:
1616 mce_intel_feature_init(c);
1617 mce_adjust_timer = mce_intel_adjust_timer;
1618 break;
1619 case X86_VENDOR_AMD:
1620 mce_amd_feature_init(c);
1621 break;
1622 default:
1623 break;
1627 static void mce_start_timer(unsigned int cpu, struct timer_list *t)
1629 unsigned long iv = mce_adjust_timer(check_interval * HZ);
1631 __this_cpu_write(mce_next_interval, iv);
1633 if (mca_cfg.ignore_ce || !iv)
1634 return;
1636 t->expires = round_jiffies(jiffies + iv);
1637 add_timer_on(t, smp_processor_id());
1640 static void __mcheck_cpu_init_timer(void)
1642 struct timer_list *t = &__get_cpu_var(mce_timer);
1643 unsigned int cpu = smp_processor_id();
1645 setup_timer(t, mce_timer_fn, cpu);
1646 mce_start_timer(cpu, t);
1649 /* Handle unconfigured int18 (should never happen) */
1650 static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1652 pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
1653 smp_processor_id());
1656 /* Call the installed machine check handler for this CPU setup. */
1657 void (*machine_check_vector)(struct pt_regs *, long error_code) =
1658 unexpected_machine_check;
1661 * Called for each booted CPU to set up machine checks.
1662 * Must be called with preempt off:
1664 void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
1666 if (mca_cfg.disabled)
1667 return;
1669 if (__mcheck_cpu_ancient_init(c))
1670 return;
1672 if (!mce_available(c))
1673 return;
1675 if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
1676 mca_cfg.disabled = true;
1677 return;
1680 machine_check_vector = do_machine_check;
1682 __mcheck_cpu_init_generic();
1683 __mcheck_cpu_init_vendor(c);
1684 __mcheck_cpu_init_timer();
1685 INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
1686 init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
1690 * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
1693 static DEFINE_SPINLOCK(mce_chrdev_state_lock);
1694 static int mce_chrdev_open_count; /* #times opened */
1695 static int mce_chrdev_open_exclu; /* already open exclusive? */
1697 static int mce_chrdev_open(struct inode *inode, struct file *file)
1699 spin_lock(&mce_chrdev_state_lock);
1701 if (mce_chrdev_open_exclu ||
1702 (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
1703 spin_unlock(&mce_chrdev_state_lock);
1705 return -EBUSY;
1708 if (file->f_flags & O_EXCL)
1709 mce_chrdev_open_exclu = 1;
1710 mce_chrdev_open_count++;
1712 spin_unlock(&mce_chrdev_state_lock);
1714 return nonseekable_open(inode, file);
1717 static int mce_chrdev_release(struct inode *inode, struct file *file)
1719 spin_lock(&mce_chrdev_state_lock);
1721 mce_chrdev_open_count--;
1722 mce_chrdev_open_exclu = 0;
1724 spin_unlock(&mce_chrdev_state_lock);
1726 return 0;
1729 static void collect_tscs(void *data)
1731 unsigned long *cpu_tsc = (unsigned long *)data;
1733 rdtscll(cpu_tsc[smp_processor_id()]);
1736 static int mce_apei_read_done;
1738 /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
1739 static int __mce_read_apei(char __user **ubuf, size_t usize)
1741 int rc;
1742 u64 record_id;
1743 struct mce m;
1745 if (usize < sizeof(struct mce))
1746 return -EINVAL;
1748 rc = apei_read_mce(&m, &record_id);
1749 /* Error or no more MCE record */
1750 if (rc <= 0) {
1751 mce_apei_read_done = 1;
1753 * When ERST is disabled, mce_chrdev_read() should return
1754 * "no record" instead of "no device."
1756 if (rc == -ENODEV)
1757 return 0;
1758 return rc;
1760 rc = -EFAULT;
1761 if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
1762 return rc;
1764 * In fact, we should have cleared the record after that has
1765 * been flushed to the disk or sent to network in
1766 * /sbin/mcelog, but we have no interface to support that now,
1767 * so just clear it to avoid duplication.
1769 rc = apei_clear_mce(record_id);
1770 if (rc) {
1771 mce_apei_read_done = 1;
1772 return rc;
1774 *ubuf += sizeof(struct mce);
1776 return 0;
1779 static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
1780 size_t usize, loff_t *off)
1782 char __user *buf = ubuf;
1783 unsigned long *cpu_tsc;
1784 unsigned prev, next;
1785 int i, err;
1787 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
1788 if (!cpu_tsc)
1789 return -ENOMEM;
1791 mutex_lock(&mce_chrdev_read_mutex);
1793 if (!mce_apei_read_done) {
1794 err = __mce_read_apei(&buf, usize);
1795 if (err || buf != ubuf)
1796 goto out;
1799 next = rcu_dereference_check_mce(mcelog.next);
1801 /* Only supports full reads right now */
1802 err = -EINVAL;
1803 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
1804 goto out;
1806 err = 0;
1807 prev = 0;
1808 do {
1809 for (i = prev; i < next; i++) {
1810 unsigned long start = jiffies;
1811 struct mce *m = &mcelog.entry[i];
1813 while (!m->finished) {
1814 if (time_after_eq(jiffies, start + 2)) {
1815 memset(m, 0, sizeof(*m));
1816 goto timeout;
1818 cpu_relax();
1820 smp_rmb();
1821 err |= copy_to_user(buf, m, sizeof(*m));
1822 buf += sizeof(*m);
1823 timeout:
1827 memset(mcelog.entry + prev, 0,
1828 (next - prev) * sizeof(struct mce));
1829 prev = next;
1830 next = cmpxchg(&mcelog.next, prev, 0);
1831 } while (next != prev);
1833 synchronize_sched();
1836 * Collect entries that were still getting written before the
1837 * synchronize.
1839 on_each_cpu(collect_tscs, cpu_tsc, 1);
1841 for (i = next; i < MCE_LOG_LEN; i++) {
1842 struct mce *m = &mcelog.entry[i];
1844 if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
1845 err |= copy_to_user(buf, m, sizeof(*m));
1846 smp_rmb();
1847 buf += sizeof(*m);
1848 memset(m, 0, sizeof(*m));
1852 if (err)
1853 err = -EFAULT;
1855 out:
1856 mutex_unlock(&mce_chrdev_read_mutex);
1857 kfree(cpu_tsc);
1859 return err ? err : buf - ubuf;
1862 static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
1864 poll_wait(file, &mce_chrdev_wait, wait);
1865 if (rcu_access_index(mcelog.next))
1866 return POLLIN | POLLRDNORM;
1867 if (!mce_apei_read_done && apei_check_mce())
1868 return POLLIN | POLLRDNORM;
1869 return 0;
1872 static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
1873 unsigned long arg)
1875 int __user *p = (int __user *)arg;
1877 if (!capable(CAP_SYS_ADMIN))
1878 return -EPERM;
1880 switch (cmd) {
1881 case MCE_GET_RECORD_LEN:
1882 return put_user(sizeof(struct mce), p);
1883 case MCE_GET_LOG_LEN:
1884 return put_user(MCE_LOG_LEN, p);
1885 case MCE_GETCLEAR_FLAGS: {
1886 unsigned flags;
1888 do {
1889 flags = mcelog.flags;
1890 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
1892 return put_user(flags, p);
1894 default:
1895 return -ENOTTY;
1899 static ssize_t (*mce_write)(struct file *filp, const char __user *ubuf,
1900 size_t usize, loff_t *off);
1902 void register_mce_write_callback(ssize_t (*fn)(struct file *filp,
1903 const char __user *ubuf,
1904 size_t usize, loff_t *off))
1906 mce_write = fn;
1908 EXPORT_SYMBOL_GPL(register_mce_write_callback);
1910 ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
1911 size_t usize, loff_t *off)
1913 if (mce_write)
1914 return mce_write(filp, ubuf, usize, off);
1915 else
1916 return -EINVAL;
1919 static const struct file_operations mce_chrdev_ops = {
1920 .open = mce_chrdev_open,
1921 .release = mce_chrdev_release,
1922 .read = mce_chrdev_read,
1923 .write = mce_chrdev_write,
1924 .poll = mce_chrdev_poll,
1925 .unlocked_ioctl = mce_chrdev_ioctl,
1926 .llseek = no_llseek,
1929 static struct miscdevice mce_chrdev_device = {
1930 MISC_MCELOG_MINOR,
1931 "mcelog",
1932 &mce_chrdev_ops,
1936 * mce=off Disables machine check
1937 * mce=no_cmci Disables CMCI
1938 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1939 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
1940 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1941 * monarchtimeout is how long to wait for other CPUs on machine
1942 * check, or 0 to not wait
1943 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1944 * mce=nobootlog Don't log MCEs from before booting.
1945 * mce=bios_cmci_threshold Don't program the CMCI threshold
1947 static int __init mcheck_enable(char *str)
1949 struct mca_config *cfg = &mca_cfg;
1951 if (*str == 0) {
1952 enable_p5_mce();
1953 return 1;
1955 if (*str == '=')
1956 str++;
1957 if (!strcmp(str, "off"))
1958 cfg->disabled = true;
1959 else if (!strcmp(str, "no_cmci"))
1960 cfg->cmci_disabled = true;
1961 else if (!strcmp(str, "dont_log_ce"))
1962 cfg->dont_log_ce = true;
1963 else if (!strcmp(str, "ignore_ce"))
1964 cfg->ignore_ce = true;
1965 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
1966 cfg->bootlog = (str[0] == 'b');
1967 else if (!strcmp(str, "bios_cmci_threshold"))
1968 cfg->bios_cmci_threshold = true;
1969 else if (isdigit(str[0])) {
1970 get_option(&str, &(cfg->tolerant));
1971 if (*str == ',') {
1972 ++str;
1973 get_option(&str, &(cfg->monarch_timeout));
1975 } else {
1976 pr_info("mce argument %s ignored. Please use /sys\n", str);
1977 return 0;
1979 return 1;
1981 __setup("mce", mcheck_enable);
1983 int __init mcheck_init(void)
1985 mcheck_intel_therm_init();
1987 return 0;
1991 * mce_syscore: PM support
1995 * Disable machine checks on suspend and shutdown. We can't really handle
1996 * them later.
1998 static int mce_disable_error_reporting(void)
2000 int i;
2002 for (i = 0; i < mca_cfg.banks; i++) {
2003 struct mce_bank *b = &mce_banks[i];
2005 if (b->init)
2006 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
2008 return 0;
2011 static int mce_syscore_suspend(void)
2013 return mce_disable_error_reporting();
2016 static void mce_syscore_shutdown(void)
2018 mce_disable_error_reporting();
2022 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
2023 * Only one CPU is active at this time, the others get re-added later using
2024 * CPU hotplug:
2026 static void mce_syscore_resume(void)
2028 __mcheck_cpu_init_generic();
2029 __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
2032 static struct syscore_ops mce_syscore_ops = {
2033 .suspend = mce_syscore_suspend,
2034 .shutdown = mce_syscore_shutdown,
2035 .resume = mce_syscore_resume,
2039 * mce_device: Sysfs support
2042 static void mce_cpu_restart(void *data)
2044 if (!mce_available(__this_cpu_ptr(&cpu_info)))
2045 return;
2046 __mcheck_cpu_init_generic();
2047 __mcheck_cpu_init_timer();
2050 /* Reinit MCEs after user configuration changes */
2051 static void mce_restart(void)
2053 mce_timer_delete_all();
2054 on_each_cpu(mce_cpu_restart, NULL, 1);
2057 /* Toggle features for corrected errors */
2058 static void mce_disable_cmci(void *data)
2060 if (!mce_available(__this_cpu_ptr(&cpu_info)))
2061 return;
2062 cmci_clear();
2065 static void mce_enable_ce(void *all)
2067 if (!mce_available(__this_cpu_ptr(&cpu_info)))
2068 return;
2069 cmci_reenable();
2070 cmci_recheck();
2071 if (all)
2072 __mcheck_cpu_init_timer();
2075 static struct bus_type mce_subsys = {
2076 .name = "machinecheck",
2077 .dev_name = "machinecheck",
2080 DEFINE_PER_CPU(struct device *, mce_device);
2082 __cpuinitdata
2083 void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
2085 static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
2087 return container_of(attr, struct mce_bank, attr);
2090 static ssize_t show_bank(struct device *s, struct device_attribute *attr,
2091 char *buf)
2093 return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
2096 static ssize_t set_bank(struct device *s, struct device_attribute *attr,
2097 const char *buf, size_t size)
2099 u64 new;
2101 if (strict_strtoull(buf, 0, &new) < 0)
2102 return -EINVAL;
2104 attr_to_bank(attr)->ctl = new;
2105 mce_restart();
2107 return size;
2110 static ssize_t
2111 show_trigger(struct device *s, struct device_attribute *attr, char *buf)
2113 strcpy(buf, mce_helper);
2114 strcat(buf, "\n");
2115 return strlen(mce_helper) + 1;
2118 static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
2119 const char *buf, size_t siz)
2121 char *p;
2123 strncpy(mce_helper, buf, sizeof(mce_helper));
2124 mce_helper[sizeof(mce_helper)-1] = 0;
2125 p = strchr(mce_helper, '\n');
2127 if (p)
2128 *p = 0;
2130 return strlen(mce_helper) + !!p;
2133 static ssize_t set_ignore_ce(struct device *s,
2134 struct device_attribute *attr,
2135 const char *buf, size_t size)
2137 u64 new;
2139 if (strict_strtoull(buf, 0, &new) < 0)
2140 return -EINVAL;
2142 if (mca_cfg.ignore_ce ^ !!new) {
2143 if (new) {
2144 /* disable ce features */
2145 mce_timer_delete_all();
2146 on_each_cpu(mce_disable_cmci, NULL, 1);
2147 mca_cfg.ignore_ce = true;
2148 } else {
2149 /* enable ce features */
2150 mca_cfg.ignore_ce = false;
2151 on_each_cpu(mce_enable_ce, (void *)1, 1);
2154 return size;
2157 static ssize_t set_cmci_disabled(struct device *s,
2158 struct device_attribute *attr,
2159 const char *buf, size_t size)
2161 u64 new;
2163 if (strict_strtoull(buf, 0, &new) < 0)
2164 return -EINVAL;
2166 if (mca_cfg.cmci_disabled ^ !!new) {
2167 if (new) {
2168 /* disable cmci */
2169 on_each_cpu(mce_disable_cmci, NULL, 1);
2170 mca_cfg.cmci_disabled = true;
2171 } else {
2172 /* enable cmci */
2173 mca_cfg.cmci_disabled = false;
2174 on_each_cpu(mce_enable_ce, NULL, 1);
2177 return size;
2180 static ssize_t store_int_with_restart(struct device *s,
2181 struct device_attribute *attr,
2182 const char *buf, size_t size)
2184 ssize_t ret = device_store_int(s, attr, buf, size);
2185 mce_restart();
2186 return ret;
2189 static DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
2190 static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
2191 static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
2192 static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
2194 static struct dev_ext_attribute dev_attr_check_interval = {
2195 __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
2196 &check_interval
2199 static struct dev_ext_attribute dev_attr_ignore_ce = {
2200 __ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
2201 &mca_cfg.ignore_ce
2204 static struct dev_ext_attribute dev_attr_cmci_disabled = {
2205 __ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
2206 &mca_cfg.cmci_disabled
2209 static struct device_attribute *mce_device_attrs[] = {
2210 &dev_attr_tolerant.attr,
2211 &dev_attr_check_interval.attr,
2212 &dev_attr_trigger,
2213 &dev_attr_monarch_timeout.attr,
2214 &dev_attr_dont_log_ce.attr,
2215 &dev_attr_ignore_ce.attr,
2216 &dev_attr_cmci_disabled.attr,
2217 NULL
2220 static cpumask_var_t mce_device_initialized;
2222 static void mce_device_release(struct device *dev)
2224 kfree(dev);
2227 /* Per cpu device init. All of the cpus still share the same ctrl bank: */
2228 static __cpuinit int mce_device_create(unsigned int cpu)
2230 struct device *dev;
2231 int err;
2232 int i, j;
2234 if (!mce_available(&boot_cpu_data))
2235 return -EIO;
2237 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2238 if (!dev)
2239 return -ENOMEM;
2240 dev->id = cpu;
2241 dev->bus = &mce_subsys;
2242 dev->release = &mce_device_release;
2244 err = device_register(dev);
2245 if (err)
2246 return err;
2248 for (i = 0; mce_device_attrs[i]; i++) {
2249 err = device_create_file(dev, mce_device_attrs[i]);
2250 if (err)
2251 goto error;
2253 for (j = 0; j < mca_cfg.banks; j++) {
2254 err = device_create_file(dev, &mce_banks[j].attr);
2255 if (err)
2256 goto error2;
2258 cpumask_set_cpu(cpu, mce_device_initialized);
2259 per_cpu(mce_device, cpu) = dev;
2261 return 0;
2262 error2:
2263 while (--j >= 0)
2264 device_remove_file(dev, &mce_banks[j].attr);
2265 error:
2266 while (--i >= 0)
2267 device_remove_file(dev, mce_device_attrs[i]);
2269 device_unregister(dev);
2271 return err;
2274 static __cpuinit void mce_device_remove(unsigned int cpu)
2276 struct device *dev = per_cpu(mce_device, cpu);
2277 int i;
2279 if (!cpumask_test_cpu(cpu, mce_device_initialized))
2280 return;
2282 for (i = 0; mce_device_attrs[i]; i++)
2283 device_remove_file(dev, mce_device_attrs[i]);
2285 for (i = 0; i < mca_cfg.banks; i++)
2286 device_remove_file(dev, &mce_banks[i].attr);
2288 device_unregister(dev);
2289 cpumask_clear_cpu(cpu, mce_device_initialized);
2290 per_cpu(mce_device, cpu) = NULL;
2293 /* Make sure there are no machine checks on offlined CPUs. */
2294 static void __cpuinit mce_disable_cpu(void *h)
2296 unsigned long action = *(unsigned long *)h;
2297 int i;
2299 if (!mce_available(__this_cpu_ptr(&cpu_info)))
2300 return;
2302 if (!(action & CPU_TASKS_FROZEN))
2303 cmci_clear();
2304 for (i = 0; i < mca_cfg.banks; i++) {
2305 struct mce_bank *b = &mce_banks[i];
2307 if (b->init)
2308 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
2312 static void __cpuinit mce_reenable_cpu(void *h)
2314 unsigned long action = *(unsigned long *)h;
2315 int i;
2317 if (!mce_available(__this_cpu_ptr(&cpu_info)))
2318 return;
2320 if (!(action & CPU_TASKS_FROZEN))
2321 cmci_reenable();
2322 for (i = 0; i < mca_cfg.banks; i++) {
2323 struct mce_bank *b = &mce_banks[i];
2325 if (b->init)
2326 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
2330 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
2331 static int __cpuinit
2332 mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
2334 unsigned int cpu = (unsigned long)hcpu;
2335 struct timer_list *t = &per_cpu(mce_timer, cpu);
2337 switch (action & ~CPU_TASKS_FROZEN) {
2338 case CPU_ONLINE:
2339 mce_device_create(cpu);
2340 if (threshold_cpu_callback)
2341 threshold_cpu_callback(action, cpu);
2342 break;
2343 case CPU_DEAD:
2344 if (threshold_cpu_callback)
2345 threshold_cpu_callback(action, cpu);
2346 mce_device_remove(cpu);
2347 mce_intel_hcpu_update(cpu);
2348 break;
2349 case CPU_DOWN_PREPARE:
2350 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
2351 del_timer_sync(t);
2352 break;
2353 case CPU_DOWN_FAILED:
2354 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
2355 mce_start_timer(cpu, t);
2356 break;
2359 if (action == CPU_POST_DEAD) {
2360 /* intentionally ignoring frozen here */
2361 cmci_rediscover(cpu);
2364 return NOTIFY_OK;
2367 static struct notifier_block mce_cpu_notifier __cpuinitdata = {
2368 .notifier_call = mce_cpu_callback,
2371 static __init void mce_init_banks(void)
2373 int i;
2375 for (i = 0; i < mca_cfg.banks; i++) {
2376 struct mce_bank *b = &mce_banks[i];
2377 struct device_attribute *a = &b->attr;
2379 sysfs_attr_init(&a->attr);
2380 a->attr.name = b->attrname;
2381 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
2383 a->attr.mode = 0644;
2384 a->show = show_bank;
2385 a->store = set_bank;
2389 static __init int mcheck_init_device(void)
2391 int err;
2392 int i = 0;
2394 if (!mce_available(&boot_cpu_data))
2395 return -EIO;
2397 zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL);
2399 mce_init_banks();
2401 err = subsys_system_register(&mce_subsys, NULL);
2402 if (err)
2403 return err;
2405 for_each_online_cpu(i) {
2406 err = mce_device_create(i);
2407 if (err)
2408 return err;
2411 register_syscore_ops(&mce_syscore_ops);
2412 register_hotcpu_notifier(&mce_cpu_notifier);
2414 /* register character device /dev/mcelog */
2415 misc_register(&mce_chrdev_device);
2417 return err;
2419 device_initcall_sync(mcheck_init_device);
2422 * Old style boot options parsing. Only for compatibility.
2424 static int __init mcheck_disable(char *str)
2426 mca_cfg.disabled = true;
2427 return 1;
2429 __setup("nomce", mcheck_disable);
2431 #ifdef CONFIG_DEBUG_FS
2432 struct dentry *mce_get_debugfs_dir(void)
2434 static struct dentry *dmce;
2436 if (!dmce)
2437 dmce = debugfs_create_dir("mce", NULL);
2439 return dmce;
2442 static void mce_reset(void)
2444 cpu_missing = 0;
2445 atomic_set(&mce_fake_paniced, 0);
2446 atomic_set(&mce_executing, 0);
2447 atomic_set(&mce_callin, 0);
2448 atomic_set(&global_nwo, 0);
2451 static int fake_panic_get(void *data, u64 *val)
2453 *val = fake_panic;
2454 return 0;
2457 static int fake_panic_set(void *data, u64 val)
2459 mce_reset();
2460 fake_panic = val;
2461 return 0;
2464 DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
2465 fake_panic_set, "%llu\n");
2467 static int __init mcheck_debugfs_init(void)
2469 struct dentry *dmce, *ffake_panic;
2471 dmce = mce_get_debugfs_dir();
2472 if (!dmce)
2473 return -ENOMEM;
2474 ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
2475 &fake_panic_fops);
2476 if (!ffake_panic)
2477 return -ENOMEM;
2479 return 0;
2481 late_initcall(mcheck_debugfs_init);
2482 #endif