xen/mce: Add mcelog support for Xen platform
[deliverable/linux.git] / arch / x86 / kernel / cpu / mcheck / mce.c
1 /*
2 * Machine check handler.
3 *
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/ratelimit.h>
14 #include <linux/kallsyms.h>
15 #include <linux/rcupdate.h>
16 #include <linux/kobject.h>
17 #include <linux/uaccess.h>
18 #include <linux/kdebug.h>
19 #include <linux/kernel.h>
20 #include <linux/percpu.h>
21 #include <linux/string.h>
22 #include <linux/device.h>
23 #include <linux/syscore_ops.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/slab.h>
30 #include <linux/init.h>
31 #include <linux/kmod.h>
32 #include <linux/poll.h>
33 #include <linux/nmi.h>
34 #include <linux/cpu.h>
35 #include <linux/smp.h>
36 #include <linux/fs.h>
37 #include <linux/mm.h>
38 #include <linux/debugfs.h>
39 #include <linux/irq_work.h>
40 #include <linux/export.h>
41
42 #include <asm/processor.h>
43 #include <asm/mce.h>
44 #include <asm/msr.h>
45
46 #include "mce-internal.h"
47
48 static DEFINE_MUTEX(mce_chrdev_read_mutex);
49
50 #define rcu_dereference_check_mce(p) \
51 rcu_dereference_index_check((p), \
52 rcu_read_lock_sched_held() || \
53 lockdep_is_held(&mce_chrdev_read_mutex))
54
55 #define CREATE_TRACE_POINTS
56 #include <trace/events/mce.h>
57
58 int mce_disabled __read_mostly;
59
60 #define SPINUNIT 100 /* 100ns */
61
62 atomic_t mce_entry;
63
64 DEFINE_PER_CPU(unsigned, mce_exception_count);
65
66 /*
67 * Tolerant levels:
68 * 0: always panic on uncorrected errors, log corrected errors
69 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
70 * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
71 * 3: never panic or SIGBUS, log all errors (for testing only)
72 */
73 static int tolerant __read_mostly = 1;
74 static int banks __read_mostly;
75 static int rip_msr __read_mostly;
76 static int mce_bootlog __read_mostly = -1;
77 static int monarch_timeout __read_mostly = -1;
78 static int mce_panic_timeout __read_mostly;
79 static int mce_dont_log_ce __read_mostly;
80 int mce_cmci_disabled __read_mostly;
81 int mce_ignore_ce __read_mostly;
82 int mce_ser __read_mostly;
83
84 struct mce_bank *mce_banks __read_mostly;
85
86 /* User mode helper program triggered by machine check event */
87 static unsigned long mce_need_notify;
88 static char mce_helper[128];
89 static char *mce_helper_argv[2] = { mce_helper, NULL };
90
91 static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
92
93 static DEFINE_PER_CPU(struct mce, mces_seen);
94 static int cpu_missing;
95
96 /* MCA banks polled by the period polling timer for corrected events */
97 DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
98 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
99 };
100
101 static DEFINE_PER_CPU(struct work_struct, mce_work);
102
103 /*
104 * CPU/chipset specific EDAC code can register a notifier call here to print
105 * MCE errors in a human-readable form.
106 */
107 ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
108
109 /* Do initial initialization of a struct mce */
110 void mce_setup(struct mce *m)
111 {
112 memset(m, 0, sizeof(struct mce));
113 m->cpu = m->extcpu = smp_processor_id();
114 rdtscll(m->tsc);
115 /* We hope get_seconds stays lockless */
116 m->time = get_seconds();
117 m->cpuvendor = boot_cpu_data.x86_vendor;
118 m->cpuid = cpuid_eax(1);
119 m->socketid = cpu_data(m->extcpu).phys_proc_id;
120 m->apicid = cpu_data(m->extcpu).initial_apicid;
121 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
122 }
123
124 DEFINE_PER_CPU(struct mce, injectm);
125 EXPORT_PER_CPU_SYMBOL_GPL(injectm);
126
127 /*
128 * Lockless MCE logging infrastructure.
129 * This avoids deadlocks on printk locks without having to break locks. Also
130 * separate MCEs from kernel messages to avoid bogus bug reports.
131 */
132
133 static struct mce_log mcelog = {
134 .signature = MCE_LOG_SIGNATURE,
135 .len = MCE_LOG_LEN,
136 .recordlen = sizeof(struct mce),
137 };
138
139 void mce_log(struct mce *mce)
140 {
141 unsigned next, entry;
142 int ret = 0;
143
144 /* Emit the trace record: */
145 trace_mce_record(mce);
146
147 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
148 if (ret == NOTIFY_STOP)
149 return;
150
151 mce->finished = 0;
152 wmb();
153 for (;;) {
154 entry = rcu_dereference_check_mce(mcelog.next);
155 for (;;) {
156
157 /*
158 * When the buffer fills up discard new entries.
159 * Assume that the earlier errors are the more
160 * interesting ones:
161 */
162 if (entry >= MCE_LOG_LEN) {
163 set_bit(MCE_OVERFLOW,
164 (unsigned long *)&mcelog.flags);
165 return;
166 }
167 /* Old left over entry. Skip: */
168 if (mcelog.entry[entry].finished) {
169 entry++;
170 continue;
171 }
172 break;
173 }
174 smp_rmb();
175 next = entry + 1;
176 if (cmpxchg(&mcelog.next, entry, next) == entry)
177 break;
178 }
179 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
180 wmb();
181 mcelog.entry[entry].finished = 1;
182 wmb();
183
184 mce->finished = 1;
185 set_bit(0, &mce_need_notify);
186 }
187
188 static void drain_mcelog_buffer(void)
189 {
190 unsigned int next, i, prev = 0;
191
192 next = ACCESS_ONCE(mcelog.next);
193
194 do {
195 struct mce *m;
196
197 /* drain what was logged during boot */
198 for (i = prev; i < next; i++) {
199 unsigned long start = jiffies;
200 unsigned retries = 1;
201
202 m = &mcelog.entry[i];
203
204 while (!m->finished) {
205 if (time_after_eq(jiffies, start + 2*retries))
206 retries++;
207
208 cpu_relax();
209
210 if (!m->finished && retries >= 4) {
211 pr_err("MCE: skipping error being logged currently!\n");
212 break;
213 }
214 }
215 smp_rmb();
216 atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
217 }
218
219 memset(mcelog.entry + prev, 0, (next - prev) * sizeof(*m));
220 prev = next;
221 next = cmpxchg(&mcelog.next, prev, 0);
222 } while (next != prev);
223 }
224
225
226 void mce_register_decode_chain(struct notifier_block *nb)
227 {
228 atomic_notifier_chain_register(&x86_mce_decoder_chain, nb);
229 drain_mcelog_buffer();
230 }
231 EXPORT_SYMBOL_GPL(mce_register_decode_chain);
232
233 void mce_unregister_decode_chain(struct notifier_block *nb)
234 {
235 atomic_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
236 }
237 EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
238
239 static void print_mce(struct mce *m)
240 {
241 int ret = 0;
242
243 pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
244 m->extcpu, m->mcgstatus, m->bank, m->status);
245
246 if (m->ip) {
247 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
248 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
249 m->cs, m->ip);
250
251 if (m->cs == __KERNEL_CS)
252 print_symbol("{%s}", m->ip);
253 pr_cont("\n");
254 }
255
256 pr_emerg(HW_ERR "TSC %llx ", m->tsc);
257 if (m->addr)
258 pr_cont("ADDR %llx ", m->addr);
259 if (m->misc)
260 pr_cont("MISC %llx ", m->misc);
261
262 pr_cont("\n");
263 /*
264 * Note this output is parsed by external tools and old fields
265 * should not be changed.
266 */
267 pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
268 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
269 cpu_data(m->extcpu).microcode);
270
271 /*
272 * Print out human-readable details about the MCE error,
273 * (if the CPU has an implementation for that)
274 */
275 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
276 if (ret == NOTIFY_STOP)
277 return;
278
279 pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
280 }
281
282 #define PANIC_TIMEOUT 5 /* 5 seconds */
283
284 static atomic_t mce_paniced;
285
286 static int fake_panic;
287 static atomic_t mce_fake_paniced;
288
289 /* Panic in progress. Enable interrupts and wait for final IPI */
290 static void wait_for_panic(void)
291 {
292 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
293
294 preempt_disable();
295 local_irq_enable();
296 while (timeout-- > 0)
297 udelay(1);
298 if (panic_timeout == 0)
299 panic_timeout = mce_panic_timeout;
300 panic("Panicing machine check CPU died");
301 }
302
303 static void mce_panic(char *msg, struct mce *final, char *exp)
304 {
305 int i, apei_err = 0;
306
307 if (!fake_panic) {
308 /*
309 * Make sure only one CPU runs in machine check panic
310 */
311 if (atomic_inc_return(&mce_paniced) > 1)
312 wait_for_panic();
313 barrier();
314
315 bust_spinlocks(1);
316 console_verbose();
317 } else {
318 /* Don't log too much for fake panic */
319 if (atomic_inc_return(&mce_fake_paniced) > 1)
320 return;
321 }
322 /* First print corrected ones that are still unlogged */
323 for (i = 0; i < MCE_LOG_LEN; i++) {
324 struct mce *m = &mcelog.entry[i];
325 if (!(m->status & MCI_STATUS_VAL))
326 continue;
327 if (!(m->status & MCI_STATUS_UC)) {
328 print_mce(m);
329 if (!apei_err)
330 apei_err = apei_write_mce(m);
331 }
332 }
333 /* Now print uncorrected but with the final one last */
334 for (i = 0; i < MCE_LOG_LEN; i++) {
335 struct mce *m = &mcelog.entry[i];
336 if (!(m->status & MCI_STATUS_VAL))
337 continue;
338 if (!(m->status & MCI_STATUS_UC))
339 continue;
340 if (!final || memcmp(m, final, sizeof(struct mce))) {
341 print_mce(m);
342 if (!apei_err)
343 apei_err = apei_write_mce(m);
344 }
345 }
346 if (final) {
347 print_mce(final);
348 if (!apei_err)
349 apei_err = apei_write_mce(final);
350 }
351 if (cpu_missing)
352 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
353 if (exp)
354 pr_emerg(HW_ERR "Machine check: %s\n", exp);
355 if (!fake_panic) {
356 if (panic_timeout == 0)
357 panic_timeout = mce_panic_timeout;
358 panic(msg);
359 } else
360 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
361 }
362
363 /* Support code for software error injection */
364
365 static int msr_to_offset(u32 msr)
366 {
367 unsigned bank = __this_cpu_read(injectm.bank);
368
369 if (msr == rip_msr)
370 return offsetof(struct mce, ip);
371 if (msr == MSR_IA32_MCx_STATUS(bank))
372 return offsetof(struct mce, status);
373 if (msr == MSR_IA32_MCx_ADDR(bank))
374 return offsetof(struct mce, addr);
375 if (msr == MSR_IA32_MCx_MISC(bank))
376 return offsetof(struct mce, misc);
377 if (msr == MSR_IA32_MCG_STATUS)
378 return offsetof(struct mce, mcgstatus);
379 return -1;
380 }
381
382 /* MSR access wrappers used for error injection */
383 static u64 mce_rdmsrl(u32 msr)
384 {
385 u64 v;
386
387 if (__this_cpu_read(injectm.finished)) {
388 int offset = msr_to_offset(msr);
389
390 if (offset < 0)
391 return 0;
392 return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
393 }
394
395 if (rdmsrl_safe(msr, &v)) {
396 WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
397 /*
398 * Return zero in case the access faulted. This should
399 * not happen normally but can happen if the CPU does
400 * something weird, or if the code is buggy.
401 */
402 v = 0;
403 }
404
405 return v;
406 }
407
408 static void mce_wrmsrl(u32 msr, u64 v)
409 {
410 if (__this_cpu_read(injectm.finished)) {
411 int offset = msr_to_offset(msr);
412
413 if (offset >= 0)
414 *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
415 return;
416 }
417 wrmsrl(msr, v);
418 }
419
420 /*
421 * Collect all global (w.r.t. this processor) status about this machine
422 * check into our "mce" struct so that we can use it later to assess
423 * the severity of the problem as we read per-bank specific details.
424 */
425 static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
426 {
427 mce_setup(m);
428
429 m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
430 if (regs) {
431 /*
432 * Get the address of the instruction at the time of
433 * the machine check error.
434 */
435 if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
436 m->ip = regs->ip;
437 m->cs = regs->cs;
438
439 /*
440 * When in VM86 mode make the cs look like ring 3
441 * always. This is a lie, but it's better than passing
442 * the additional vm86 bit around everywhere.
443 */
444 if (v8086_mode(regs))
445 m->cs |= 3;
446 }
447 /* Use accurate RIP reporting if available. */
448 if (rip_msr)
449 m->ip = mce_rdmsrl(rip_msr);
450 }
451 }
452
453 /*
454 * Simple lockless ring to communicate PFNs from the exception handler with the
455 * process context work function. This is vastly simplified because there's
456 * only a single reader and a single writer.
457 */
458 #define MCE_RING_SIZE 16 /* we use one entry less */
459
460 struct mce_ring {
461 unsigned short start;
462 unsigned short end;
463 unsigned long ring[MCE_RING_SIZE];
464 };
465 static DEFINE_PER_CPU(struct mce_ring, mce_ring);
466
467 /* Runs with CPU affinity in workqueue */
468 static int mce_ring_empty(void)
469 {
470 struct mce_ring *r = &__get_cpu_var(mce_ring);
471
472 return r->start == r->end;
473 }
474
475 static int mce_ring_get(unsigned long *pfn)
476 {
477 struct mce_ring *r;
478 int ret = 0;
479
480 *pfn = 0;
481 get_cpu();
482 r = &__get_cpu_var(mce_ring);
483 if (r->start == r->end)
484 goto out;
485 *pfn = r->ring[r->start];
486 r->start = (r->start + 1) % MCE_RING_SIZE;
487 ret = 1;
488 out:
489 put_cpu();
490 return ret;
491 }
492
493 /* Always runs in MCE context with preempt off */
494 static int mce_ring_add(unsigned long pfn)
495 {
496 struct mce_ring *r = &__get_cpu_var(mce_ring);
497 unsigned next;
498
499 next = (r->end + 1) % MCE_RING_SIZE;
500 if (next == r->start)
501 return -1;
502 r->ring[r->end] = pfn;
503 wmb();
504 r->end = next;
505 return 0;
506 }
507
508 int mce_available(struct cpuinfo_x86 *c)
509 {
510 if (mce_disabled)
511 return 0;
512 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
513 }
514
515 static void mce_schedule_work(void)
516 {
517 if (!mce_ring_empty()) {
518 struct work_struct *work = &__get_cpu_var(mce_work);
519 if (!work_pending(work))
520 schedule_work(work);
521 }
522 }
523
524 DEFINE_PER_CPU(struct irq_work, mce_irq_work);
525
526 static void mce_irq_work_cb(struct irq_work *entry)
527 {
528 mce_notify_irq();
529 mce_schedule_work();
530 }
531
532 static void mce_report_event(struct pt_regs *regs)
533 {
534 if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
535 mce_notify_irq();
536 /*
537 * Triggering the work queue here is just an insurance
538 * policy in case the syscall exit notify handler
539 * doesn't run soon enough or ends up running on the
540 * wrong CPU (can happen when audit sleeps)
541 */
542 mce_schedule_work();
543 return;
544 }
545
546 irq_work_queue(&__get_cpu_var(mce_irq_work));
547 }
548
549 /*
550 * Read ADDR and MISC registers.
551 */
552 static void mce_read_aux(struct mce *m, int i)
553 {
554 if (m->status & MCI_STATUS_MISCV)
555 m->misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
556 if (m->status & MCI_STATUS_ADDRV) {
557 m->addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
558
559 /*
560 * Mask the reported address by the reported granularity.
561 */
562 if (mce_ser && (m->status & MCI_STATUS_MISCV)) {
563 u8 shift = MCI_MISC_ADDR_LSB(m->misc);
564 m->addr >>= shift;
565 m->addr <<= shift;
566 }
567 }
568 }
569
570 DEFINE_PER_CPU(unsigned, mce_poll_count);
571
572 /*
573 * Poll for corrected events or events that happened before reset.
574 * Those are just logged through /dev/mcelog.
575 *
576 * This is executed in standard interrupt context.
577 *
578 * Note: spec recommends to panic for fatal unsignalled
579 * errors here. However this would be quite problematic --
580 * we would need to reimplement the Monarch handling and
581 * it would mess up the exclusion between exception handler
582 * and poll hander -- * so we skip this for now.
583 * These cases should not happen anyways, or only when the CPU
584 * is already totally * confused. In this case it's likely it will
585 * not fully execute the machine check handler either.
586 */
587 void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
588 {
589 struct mce m;
590 int i;
591
592 this_cpu_inc(mce_poll_count);
593
594 mce_gather_info(&m, NULL);
595
596 for (i = 0; i < banks; i++) {
597 if (!mce_banks[i].ctl || !test_bit(i, *b))
598 continue;
599
600 m.misc = 0;
601 m.addr = 0;
602 m.bank = i;
603 m.tsc = 0;
604
605 barrier();
606 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
607 if (!(m.status & MCI_STATUS_VAL))
608 continue;
609
610 /*
611 * Uncorrected or signalled events are handled by the exception
612 * handler when it is enabled, so don't process those here.
613 *
614 * TBD do the same check for MCI_STATUS_EN here?
615 */
616 if (!(flags & MCP_UC) &&
617 (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
618 continue;
619
620 mce_read_aux(&m, i);
621
622 if (!(flags & MCP_TIMESTAMP))
623 m.tsc = 0;
624 /*
625 * Don't get the IP here because it's unlikely to
626 * have anything to do with the actual error location.
627 */
628 if (!(flags & MCP_DONTLOG) && !mce_dont_log_ce)
629 mce_log(&m);
630
631 /*
632 * Clear state for this bank.
633 */
634 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
635 }
636
637 /*
638 * Don't clear MCG_STATUS here because it's only defined for
639 * exceptions.
640 */
641
642 sync_core();
643 }
644 EXPORT_SYMBOL_GPL(machine_check_poll);
645
646 /*
647 * Do a quick check if any of the events requires a panic.
648 * This decides if we keep the events around or clear them.
649 */
650 static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp)
651 {
652 int i, ret = 0;
653
654 for (i = 0; i < banks; i++) {
655 m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
656 if (m->status & MCI_STATUS_VAL)
657 __set_bit(i, validp);
658 if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
659 ret = 1;
660 }
661 return ret;
662 }
663
664 /*
665 * Variable to establish order between CPUs while scanning.
666 * Each CPU spins initially until executing is equal its number.
667 */
668 static atomic_t mce_executing;
669
670 /*
671 * Defines order of CPUs on entry. First CPU becomes Monarch.
672 */
673 static atomic_t mce_callin;
674
675 /*
676 * Check if a timeout waiting for other CPUs happened.
677 */
678 static int mce_timed_out(u64 *t)
679 {
680 /*
681 * The others already did panic for some reason.
682 * Bail out like in a timeout.
683 * rmb() to tell the compiler that system_state
684 * might have been modified by someone else.
685 */
686 rmb();
687 if (atomic_read(&mce_paniced))
688 wait_for_panic();
689 if (!monarch_timeout)
690 goto out;
691 if ((s64)*t < SPINUNIT) {
692 /* CHECKME: Make panic default for 1 too? */
693 if (tolerant < 1)
694 mce_panic("Timeout synchronizing machine check over CPUs",
695 NULL, NULL);
696 cpu_missing = 1;
697 return 1;
698 }
699 *t -= SPINUNIT;
700 out:
701 touch_nmi_watchdog();
702 return 0;
703 }
704
705 /*
706 * The Monarch's reign. The Monarch is the CPU who entered
707 * the machine check handler first. It waits for the others to
708 * raise the exception too and then grades them. When any
709 * error is fatal panic. Only then let the others continue.
710 *
711 * The other CPUs entering the MCE handler will be controlled by the
712 * Monarch. They are called Subjects.
713 *
714 * This way we prevent any potential data corruption in a unrecoverable case
715 * and also makes sure always all CPU's errors are examined.
716 *
717 * Also this detects the case of a machine check event coming from outer
718 * space (not detected by any CPUs) In this case some external agent wants
719 * us to shut down, so panic too.
720 *
721 * The other CPUs might still decide to panic if the handler happens
722 * in a unrecoverable place, but in this case the system is in a semi-stable
723 * state and won't corrupt anything by itself. It's ok to let the others
724 * continue for a bit first.
725 *
726 * All the spin loops have timeouts; when a timeout happens a CPU
727 * typically elects itself to be Monarch.
728 */
729 static void mce_reign(void)
730 {
731 int cpu;
732 struct mce *m = NULL;
733 int global_worst = 0;
734 char *msg = NULL;
735 char *nmsg = NULL;
736
737 /*
738 * This CPU is the Monarch and the other CPUs have run
739 * through their handlers.
740 * Grade the severity of the errors of all the CPUs.
741 */
742 for_each_possible_cpu(cpu) {
743 int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
744 &nmsg);
745 if (severity > global_worst) {
746 msg = nmsg;
747 global_worst = severity;
748 m = &per_cpu(mces_seen, cpu);
749 }
750 }
751
752 /*
753 * Cannot recover? Panic here then.
754 * This dumps all the mces in the log buffer and stops the
755 * other CPUs.
756 */
757 if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
758 mce_panic("Fatal Machine check", m, msg);
759
760 /*
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.
764 */
765
766 /*
767 * No machine check event found. Must be some external
768 * source or one CPU is hung. Panic.
769 */
770 if (global_worst <= MCE_KEEP_SEVERITY && tolerant < 3)
771 mce_panic("Machine check from unknown source", NULL, NULL);
772
773 /*
774 * Now clear all the mces_seen so that they don't reappear on
775 * the next mce.
776 */
777 for_each_possible_cpu(cpu)
778 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
779 }
780
781 static atomic_t global_nwo;
782
783 /*
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
789 */
790 static int mce_start(int *no_way_out)
791 {
792 int order;
793 int cpus = num_online_cpus();
794 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
795
796 if (!timeout)
797 return -1;
798
799 atomic_add(*no_way_out, &global_nwo);
800 /*
801 * global_nwo should be updated before mce_callin
802 */
803 smp_wmb();
804 order = atomic_inc_return(&mce_callin);
805
806 /*
807 * Wait for everyone.
808 */
809 while (atomic_read(&mce_callin) != cpus) {
810 if (mce_timed_out(&timeout)) {
811 atomic_set(&global_nwo, 0);
812 return -1;
813 }
814 ndelay(SPINUNIT);
815 }
816
817 /*
818 * mce_callin should be read before global_nwo
819 */
820 smp_rmb();
821
822 if (order == 1) {
823 /*
824 * Monarch: Starts executing now, the others wait.
825 */
826 atomic_set(&mce_executing, 1);
827 } else {
828 /*
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.
833 */
834 while (atomic_read(&mce_executing) < order) {
835 if (mce_timed_out(&timeout)) {
836 atomic_set(&global_nwo, 0);
837 return -1;
838 }
839 ndelay(SPINUNIT);
840 }
841 }
842
843 /*
844 * Cache the global no_way_out state.
845 */
846 *no_way_out = atomic_read(&global_nwo);
847
848 return order;
849 }
850
851 /*
852 * Synchronize between CPUs after main scanning loop.
853 * This invokes the bulk of the Monarch processing.
854 */
855 static int mce_end(int order)
856 {
857 int ret = -1;
858 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
859
860 if (!timeout)
861 goto reset;
862 if (order < 0)
863 goto reset;
864
865 /*
866 * Allow others to run.
867 */
868 atomic_inc(&mce_executing);
869
870 if (order == 1) {
871 /* CHECKME: Can this race with a parallel hotplug? */
872 int cpus = num_online_cpus();
873
874 /*
875 * Monarch: Wait for everyone to go through their scanning
876 * loops.
877 */
878 while (atomic_read(&mce_executing) <= cpus) {
879 if (mce_timed_out(&timeout))
880 goto reset;
881 ndelay(SPINUNIT);
882 }
883
884 mce_reign();
885 barrier();
886 ret = 0;
887 } else {
888 /*
889 * Subject: Wait for Monarch to finish.
890 */
891 while (atomic_read(&mce_executing) != 0) {
892 if (mce_timed_out(&timeout))
893 goto reset;
894 ndelay(SPINUNIT);
895 }
896
897 /*
898 * Don't reset anything. That's done by the Monarch.
899 */
900 return 0;
901 }
902
903 /*
904 * Reset all global state.
905 */
906 reset:
907 atomic_set(&global_nwo, 0);
908 atomic_set(&mce_callin, 0);
909 barrier();
910
911 /*
912 * Let others run again.
913 */
914 atomic_set(&mce_executing, 0);
915 return ret;
916 }
917
918 /*
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.
923 */
924 static int mce_usable_address(struct mce *m)
925 {
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;
933 }
934
935 static void mce_clear_state(unsigned long *toclear)
936 {
937 int i;
938
939 for (i = 0; i < banks; i++) {
940 if (test_bit(i, toclear))
941 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
942 }
943 }
944
945 /*
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()
949 */
950 #define MCE_INFO_MAX 16
951
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];
958
959 static void mce_save_info(__u64 addr, int c)
960 {
961 struct mce_info *mi;
962
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;
969 }
970 }
971
972 mce_panic("Too many concurrent recoverable errors", NULL, NULL);
973 }
974
975 static struct mce_info *mce_find_info(void)
976 {
977 struct mce_info *mi;
978
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;
983 }
984
985 static void mce_clear_info(struct mce_info *mi)
986 {
987 atomic_set(&mi->inuse, 0);
988 }
989
990 /*
991 * The actual machine check handler. This only handles real
992 * exceptions when something got corrupted coming in through int 18.
993 *
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!
997 *
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.
1001 */
1002 void do_machine_check(struct pt_regs *regs, long error_code)
1003 {
1004 struct mce m, *final;
1005 int i;
1006 int worst = 0;
1007 int severity;
1008 /*
1009 * Establish sequential order between the CPUs entering the machine
1010 * check handler.
1011 */
1012 int order;
1013 /*
1014 * If no_way_out gets set, there is no safe way to recover from this
1015 * MCE. If tolerant is cranked up, we'll try anyway.
1016 */
1017 int no_way_out = 0;
1018 /*
1019 * If kill_it gets set, there might be a way to recover from this
1020 * error.
1021 */
1022 int kill_it = 0;
1023 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
1024 DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
1025 char *msg = "Unknown";
1026
1027 atomic_inc(&mce_entry);
1028
1029 this_cpu_inc(mce_exception_count);
1030
1031 if (!banks)
1032 goto out;
1033
1034 mce_gather_info(&m, regs);
1035
1036 final = &__get_cpu_var(mces_seen);
1037 *final = m;
1038
1039 memset(valid_banks, 0, sizeof(valid_banks));
1040 no_way_out = mce_no_way_out(&m, &msg, valid_banks);
1041
1042 barrier();
1043
1044 /*
1045 * When no restart IP might need to kill or panic.
1046 * Assume the worst for now, but if we find the
1047 * severity is MCE_AR_SEVERITY we have other options.
1048 */
1049 if (!(m.mcgstatus & MCG_STATUS_RIPV))
1050 kill_it = 1;
1051
1052 /*
1053 * Go through all the banks in exclusion of the other CPUs.
1054 * This way we don't report duplicated events on shared banks
1055 * because the first one to see it will clear it.
1056 */
1057 order = mce_start(&no_way_out);
1058 for (i = 0; i < banks; i++) {
1059 __clear_bit(i, toclear);
1060 if (!test_bit(i, valid_banks))
1061 continue;
1062 if (!mce_banks[i].ctl)
1063 continue;
1064
1065 m.misc = 0;
1066 m.addr = 0;
1067 m.bank = i;
1068
1069 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
1070 if ((m.status & MCI_STATUS_VAL) == 0)
1071 continue;
1072
1073 /*
1074 * Non uncorrected or non signaled errors are handled by
1075 * machine_check_poll. Leave them alone, unless this panics.
1076 */
1077 if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
1078 !no_way_out)
1079 continue;
1080
1081 /*
1082 * Set taint even when machine check was not enabled.
1083 */
1084 add_taint(TAINT_MACHINE_CHECK);
1085
1086 severity = mce_severity(&m, tolerant, NULL);
1087
1088 /*
1089 * When machine check was for corrected handler don't touch,
1090 * unless we're panicing.
1091 */
1092 if (severity == MCE_KEEP_SEVERITY && !no_way_out)
1093 continue;
1094 __set_bit(i, toclear);
1095 if (severity == MCE_NO_SEVERITY) {
1096 /*
1097 * Machine check event was not enabled. Clear, but
1098 * ignore.
1099 */
1100 continue;
1101 }
1102
1103 mce_read_aux(&m, i);
1104
1105 /*
1106 * Action optional error. Queue address for later processing.
1107 * When the ring overflows we just ignore the AO error.
1108 * RED-PEN add some logging mechanism when
1109 * usable_address or mce_add_ring fails.
1110 * RED-PEN don't ignore overflow for tolerant == 0
1111 */
1112 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
1113 mce_ring_add(m.addr >> PAGE_SHIFT);
1114
1115 mce_log(&m);
1116
1117 if (severity > worst) {
1118 *final = m;
1119 worst = severity;
1120 }
1121 }
1122
1123 /* mce_clear_state will clear *final, save locally for use later */
1124 m = *final;
1125
1126 if (!no_way_out)
1127 mce_clear_state(toclear);
1128
1129 /*
1130 * Do most of the synchronization with other CPUs.
1131 * When there's any problem use only local no_way_out state.
1132 */
1133 if (mce_end(order) < 0)
1134 no_way_out = worst >= MCE_PANIC_SEVERITY;
1135
1136 /*
1137 * At insane "tolerant" levels we take no action. Otherwise
1138 * we only die if we have no other choice. For less serious
1139 * issues we try to recover, or limit damage to the current
1140 * process.
1141 */
1142 if (tolerant < 3) {
1143 if (no_way_out)
1144 mce_panic("Fatal machine check on current CPU", &m, msg);
1145 if (worst == MCE_AR_SEVERITY) {
1146 /* schedule action before return to userland */
1147 mce_save_info(m.addr, m.mcgstatus & MCG_STATUS_RIPV);
1148 set_thread_flag(TIF_MCE_NOTIFY);
1149 } else if (kill_it) {
1150 force_sig(SIGBUS, current);
1151 }
1152 }
1153
1154 if (worst > 0)
1155 mce_report_event(regs);
1156 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
1157 out:
1158 atomic_dec(&mce_entry);
1159 sync_core();
1160 }
1161 EXPORT_SYMBOL_GPL(do_machine_check);
1162
1163 #ifndef CONFIG_MEMORY_FAILURE
1164 int memory_failure(unsigned long pfn, int vector, int flags)
1165 {
1166 /* mce_severity() should not hand us an ACTION_REQUIRED error */
1167 BUG_ON(flags & MF_ACTION_REQUIRED);
1168 printk(KERN_ERR "Uncorrected memory error in page 0x%lx ignored\n"
1169 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n", pfn);
1170
1171 return 0;
1172 }
1173 #endif
1174
1175 /*
1176 * Called in process context that interrupted by MCE and marked with
1177 * TIF_MCE_NOTIFY, just before returning to erroneous userland.
1178 * This code is allowed to sleep.
1179 * Attempt possible recovery such as calling the high level VM handler to
1180 * process any corrupted pages, and kill/signal current process if required.
1181 * Action required errors are handled here.
1182 */
1183 void mce_notify_process(void)
1184 {
1185 unsigned long pfn;
1186 struct mce_info *mi = mce_find_info();
1187
1188 if (!mi)
1189 mce_panic("Lost physical address for unconsumed uncorrectable error", NULL, NULL);
1190 pfn = mi->paddr >> PAGE_SHIFT;
1191
1192 clear_thread_flag(TIF_MCE_NOTIFY);
1193
1194 pr_err("Uncorrected hardware memory error in user-access at %llx",
1195 mi->paddr);
1196 /*
1197 * We must call memory_failure() here even if the current process is
1198 * doomed. We still need to mark the page as poisoned and alert any
1199 * other users of the page.
1200 */
1201 if (memory_failure(pfn, MCE_VECTOR, MF_ACTION_REQUIRED) < 0 ||
1202 mi->restartable == 0) {
1203 pr_err("Memory error not recovered");
1204 force_sig(SIGBUS, current);
1205 }
1206 mce_clear_info(mi);
1207 }
1208
1209 /*
1210 * Action optional processing happens here (picking up
1211 * from the list of faulting pages that do_machine_check()
1212 * placed into the "ring").
1213 */
1214 static void mce_process_work(struct work_struct *dummy)
1215 {
1216 unsigned long pfn;
1217
1218 while (mce_ring_get(&pfn))
1219 memory_failure(pfn, MCE_VECTOR, 0);
1220 }
1221
1222 #ifdef CONFIG_X86_MCE_INTEL
1223 /***
1224 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
1225 * @cpu: The CPU on which the event occurred.
1226 * @status: Event status information
1227 *
1228 * This function should be called by the thermal interrupt after the
1229 * event has been processed and the decision was made to log the event
1230 * further.
1231 *
1232 * The status parameter will be saved to the 'status' field of 'struct mce'
1233 * and historically has been the register value of the
1234 * MSR_IA32_THERMAL_STATUS (Intel) msr.
1235 */
1236 void mce_log_therm_throt_event(__u64 status)
1237 {
1238 struct mce m;
1239
1240 mce_setup(&m);
1241 m.bank = MCE_THERMAL_BANK;
1242 m.status = status;
1243 mce_log(&m);
1244 }
1245 #endif /* CONFIG_X86_MCE_INTEL */
1246
1247 /*
1248 * Periodic polling timer for "silent" machine check errors. If the
1249 * poller finds an MCE, poll 2x faster. When the poller finds no more
1250 * errors, poll 2x slower (up to check_interval seconds).
1251 */
1252 static unsigned long check_interval = 5 * 60; /* 5 minutes */
1253
1254 static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
1255 static DEFINE_PER_CPU(struct timer_list, mce_timer);
1256
1257 static void mce_timer_fn(unsigned long data)
1258 {
1259 struct timer_list *t = &__get_cpu_var(mce_timer);
1260 unsigned long iv;
1261
1262 WARN_ON(smp_processor_id() != data);
1263
1264 if (mce_available(__this_cpu_ptr(&cpu_info))) {
1265 machine_check_poll(MCP_TIMESTAMP,
1266 &__get_cpu_var(mce_poll_banks));
1267 }
1268
1269 /*
1270 * Alert userspace if needed. If we logged an MCE, reduce the
1271 * polling interval, otherwise increase the polling interval.
1272 */
1273 iv = __this_cpu_read(mce_next_interval);
1274 if (mce_notify_irq())
1275 iv = max(iv / 2, (unsigned long) HZ/100);
1276 else
1277 iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
1278 __this_cpu_write(mce_next_interval, iv);
1279
1280 t->expires = jiffies + iv;
1281 add_timer_on(t, smp_processor_id());
1282 }
1283
1284 /* Must not be called in IRQ context where del_timer_sync() can deadlock */
1285 static void mce_timer_delete_all(void)
1286 {
1287 int cpu;
1288
1289 for_each_online_cpu(cpu)
1290 del_timer_sync(&per_cpu(mce_timer, cpu));
1291 }
1292
1293 static void mce_do_trigger(struct work_struct *work)
1294 {
1295 call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
1296 }
1297
1298 static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1299
1300 /*
1301 * Notify the user(s) about new machine check events.
1302 * Can be called from interrupt context, but not from machine check/NMI
1303 * context.
1304 */
1305 int mce_notify_irq(void)
1306 {
1307 /* Not more than two messages every minute */
1308 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1309
1310 if (test_and_clear_bit(0, &mce_need_notify)) {
1311 /* wake processes polling /dev/mcelog */
1312 wake_up_interruptible(&mce_chrdev_wait);
1313
1314 /*
1315 * There is no risk of missing notifications because
1316 * work_pending is always cleared before the function is
1317 * executed.
1318 */
1319 if (mce_helper[0] && !work_pending(&mce_trigger_work))
1320 schedule_work(&mce_trigger_work);
1321
1322 if (__ratelimit(&ratelimit))
1323 pr_info(HW_ERR "Machine check events logged\n");
1324
1325 return 1;
1326 }
1327 return 0;
1328 }
1329 EXPORT_SYMBOL_GPL(mce_notify_irq);
1330
1331 static int __cpuinit __mcheck_cpu_mce_banks_init(void)
1332 {
1333 int i;
1334
1335 mce_banks = kzalloc(banks * sizeof(struct mce_bank), GFP_KERNEL);
1336 if (!mce_banks)
1337 return -ENOMEM;
1338 for (i = 0; i < banks; i++) {
1339 struct mce_bank *b = &mce_banks[i];
1340
1341 b->ctl = -1ULL;
1342 b->init = 1;
1343 }
1344 return 0;
1345 }
1346
1347 /*
1348 * Initialize Machine Checks for a CPU.
1349 */
1350 static int __cpuinit __mcheck_cpu_cap_init(void)
1351 {
1352 unsigned b;
1353 u64 cap;
1354
1355 rdmsrl(MSR_IA32_MCG_CAP, cap);
1356
1357 b = cap & MCG_BANKCNT_MASK;
1358 if (!banks)
1359 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
1360
1361 if (b > MAX_NR_BANKS) {
1362 printk(KERN_WARNING
1363 "MCE: Using only %u machine check banks out of %u\n",
1364 MAX_NR_BANKS, b);
1365 b = MAX_NR_BANKS;
1366 }
1367
1368 /* Don't support asymmetric configurations today */
1369 WARN_ON(banks != 0 && b != banks);
1370 banks = b;
1371 if (!mce_banks) {
1372 int err = __mcheck_cpu_mce_banks_init();
1373
1374 if (err)
1375 return err;
1376 }
1377
1378 /* Use accurate RIP reporting if available. */
1379 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
1380 rip_msr = MSR_IA32_MCG_EIP;
1381
1382 if (cap & MCG_SER_P)
1383 mce_ser = 1;
1384
1385 return 0;
1386 }
1387
1388 static void __mcheck_cpu_init_generic(void)
1389 {
1390 mce_banks_t all_banks;
1391 u64 cap;
1392 int i;
1393
1394 /*
1395 * Log the machine checks left over from the previous reset.
1396 */
1397 bitmap_fill(all_banks, MAX_NR_BANKS);
1398 machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
1399
1400 set_in_cr4(X86_CR4_MCE);
1401
1402 rdmsrl(MSR_IA32_MCG_CAP, cap);
1403 if (cap & MCG_CTL_P)
1404 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1405
1406 for (i = 0; i < banks; i++) {
1407 struct mce_bank *b = &mce_banks[i];
1408
1409 if (!b->init)
1410 continue;
1411 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
1412 wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
1413 }
1414 }
1415
1416 /* Add per CPU specific workarounds here */
1417 static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
1418 {
1419 if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
1420 pr_info("MCE: unknown CPU type - not enabling MCE support.\n");
1421 return -EOPNOTSUPP;
1422 }
1423
1424 /* This should be disabled by the BIOS, but isn't always */
1425 if (c->x86_vendor == X86_VENDOR_AMD) {
1426 if (c->x86 == 15 && banks > 4) {
1427 /*
1428 * disable GART TBL walk error reporting, which
1429 * trips off incorrectly with the IOMMU & 3ware
1430 * & Cerberus:
1431 */
1432 clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
1433 }
1434 if (c->x86 <= 17 && mce_bootlog < 0) {
1435 /*
1436 * Lots of broken BIOS around that don't clear them
1437 * by default and leave crap in there. Don't log:
1438 */
1439 mce_bootlog = 0;
1440 }
1441 /*
1442 * Various K7s with broken bank 0 around. Always disable
1443 * by default.
1444 */
1445 if (c->x86 == 6 && banks > 0)
1446 mce_banks[0].ctl = 0;
1447
1448 /*
1449 * Turn off MC4_MISC thresholding banks on those models since
1450 * they're not supported there.
1451 */
1452 if (c->x86 == 0x15 &&
1453 (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
1454 int i;
1455 u64 val, hwcr;
1456 bool need_toggle;
1457 u32 msrs[] = {
1458 0x00000413, /* MC4_MISC0 */
1459 0xc0000408, /* MC4_MISC1 */
1460 };
1461
1462 rdmsrl(MSR_K7_HWCR, hwcr);
1463
1464 /* McStatusWrEn has to be set */
1465 need_toggle = !(hwcr & BIT(18));
1466
1467 if (need_toggle)
1468 wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
1469
1470 for (i = 0; i < ARRAY_SIZE(msrs); i++) {
1471 rdmsrl(msrs[i], val);
1472
1473 /* CntP bit set? */
1474 if (val & BIT_64(62)) {
1475 val &= ~BIT_64(62);
1476 wrmsrl(msrs[i], val);
1477 }
1478 }
1479
1480 /* restore old settings */
1481 if (need_toggle)
1482 wrmsrl(MSR_K7_HWCR, hwcr);
1483 }
1484 }
1485
1486 if (c->x86_vendor == X86_VENDOR_INTEL) {
1487 /*
1488 * SDM documents that on family 6 bank 0 should not be written
1489 * because it aliases to another special BIOS controlled
1490 * register.
1491 * But it's not aliased anymore on model 0x1a+
1492 * Don't ignore bank 0 completely because there could be a
1493 * valid event later, merely don't write CTL0.
1494 */
1495
1496 if (c->x86 == 6 && c->x86_model < 0x1A && banks > 0)
1497 mce_banks[0].init = 0;
1498
1499 /*
1500 * All newer Intel systems support MCE broadcasting. Enable
1501 * synchronization with a one second timeout.
1502 */
1503 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1504 monarch_timeout < 0)
1505 monarch_timeout = USEC_PER_SEC;
1506
1507 /*
1508 * There are also broken BIOSes on some Pentium M and
1509 * earlier systems:
1510 */
1511 if (c->x86 == 6 && c->x86_model <= 13 && mce_bootlog < 0)
1512 mce_bootlog = 0;
1513 }
1514 if (monarch_timeout < 0)
1515 monarch_timeout = 0;
1516 if (mce_bootlog != 0)
1517 mce_panic_timeout = 30;
1518
1519 return 0;
1520 }
1521
1522 static int __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
1523 {
1524 if (c->x86 != 5)
1525 return 0;
1526
1527 switch (c->x86_vendor) {
1528 case X86_VENDOR_INTEL:
1529 intel_p5_mcheck_init(c);
1530 return 1;
1531 break;
1532 case X86_VENDOR_CENTAUR:
1533 winchip_mcheck_init(c);
1534 return 1;
1535 break;
1536 }
1537
1538 return 0;
1539 }
1540
1541 static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
1542 {
1543 switch (c->x86_vendor) {
1544 case X86_VENDOR_INTEL:
1545 mce_intel_feature_init(c);
1546 break;
1547 case X86_VENDOR_AMD:
1548 mce_amd_feature_init(c);
1549 break;
1550 default:
1551 break;
1552 }
1553 }
1554
1555 static void __mcheck_cpu_init_timer(void)
1556 {
1557 struct timer_list *t = &__get_cpu_var(mce_timer);
1558 unsigned long iv = check_interval * HZ;
1559
1560 setup_timer(t, mce_timer_fn, smp_processor_id());
1561
1562 if (mce_ignore_ce)
1563 return;
1564
1565 __this_cpu_write(mce_next_interval, iv);
1566 if (!iv)
1567 return;
1568 t->expires = round_jiffies(jiffies + iv);
1569 add_timer_on(t, smp_processor_id());
1570 }
1571
1572 /* Handle unconfigured int18 (should never happen) */
1573 static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1574 {
1575 printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
1576 smp_processor_id());
1577 }
1578
1579 /* Call the installed machine check handler for this CPU setup. */
1580 void (*machine_check_vector)(struct pt_regs *, long error_code) =
1581 unexpected_machine_check;
1582
1583 /*
1584 * Called for each booted CPU to set up machine checks.
1585 * Must be called with preempt off:
1586 */
1587 void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
1588 {
1589 if (mce_disabled)
1590 return;
1591
1592 if (__mcheck_cpu_ancient_init(c))
1593 return;
1594
1595 if (!mce_available(c))
1596 return;
1597
1598 if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
1599 mce_disabled = 1;
1600 return;
1601 }
1602
1603 machine_check_vector = do_machine_check;
1604
1605 __mcheck_cpu_init_generic();
1606 __mcheck_cpu_init_vendor(c);
1607 __mcheck_cpu_init_timer();
1608 INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
1609 init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
1610 }
1611
1612 /*
1613 * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
1614 */
1615
1616 static DEFINE_SPINLOCK(mce_chrdev_state_lock);
1617 static int mce_chrdev_open_count; /* #times opened */
1618 static int mce_chrdev_open_exclu; /* already open exclusive? */
1619
1620 static int mce_chrdev_open(struct inode *inode, struct file *file)
1621 {
1622 spin_lock(&mce_chrdev_state_lock);
1623
1624 if (mce_chrdev_open_exclu ||
1625 (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
1626 spin_unlock(&mce_chrdev_state_lock);
1627
1628 return -EBUSY;
1629 }
1630
1631 if (file->f_flags & O_EXCL)
1632 mce_chrdev_open_exclu = 1;
1633 mce_chrdev_open_count++;
1634
1635 spin_unlock(&mce_chrdev_state_lock);
1636
1637 return nonseekable_open(inode, file);
1638 }
1639
1640 static int mce_chrdev_release(struct inode *inode, struct file *file)
1641 {
1642 spin_lock(&mce_chrdev_state_lock);
1643
1644 mce_chrdev_open_count--;
1645 mce_chrdev_open_exclu = 0;
1646
1647 spin_unlock(&mce_chrdev_state_lock);
1648
1649 return 0;
1650 }
1651
1652 static void collect_tscs(void *data)
1653 {
1654 unsigned long *cpu_tsc = (unsigned long *)data;
1655
1656 rdtscll(cpu_tsc[smp_processor_id()]);
1657 }
1658
1659 static int mce_apei_read_done;
1660
1661 /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
1662 static int __mce_read_apei(char __user **ubuf, size_t usize)
1663 {
1664 int rc;
1665 u64 record_id;
1666 struct mce m;
1667
1668 if (usize < sizeof(struct mce))
1669 return -EINVAL;
1670
1671 rc = apei_read_mce(&m, &record_id);
1672 /* Error or no more MCE record */
1673 if (rc <= 0) {
1674 mce_apei_read_done = 1;
1675 /*
1676 * When ERST is disabled, mce_chrdev_read() should return
1677 * "no record" instead of "no device."
1678 */
1679 if (rc == -ENODEV)
1680 return 0;
1681 return rc;
1682 }
1683 rc = -EFAULT;
1684 if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
1685 return rc;
1686 /*
1687 * In fact, we should have cleared the record after that has
1688 * been flushed to the disk or sent to network in
1689 * /sbin/mcelog, but we have no interface to support that now,
1690 * so just clear it to avoid duplication.
1691 */
1692 rc = apei_clear_mce(record_id);
1693 if (rc) {
1694 mce_apei_read_done = 1;
1695 return rc;
1696 }
1697 *ubuf += sizeof(struct mce);
1698
1699 return 0;
1700 }
1701
1702 static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
1703 size_t usize, loff_t *off)
1704 {
1705 char __user *buf = ubuf;
1706 unsigned long *cpu_tsc;
1707 unsigned prev, next;
1708 int i, err;
1709
1710 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
1711 if (!cpu_tsc)
1712 return -ENOMEM;
1713
1714 mutex_lock(&mce_chrdev_read_mutex);
1715
1716 if (!mce_apei_read_done) {
1717 err = __mce_read_apei(&buf, usize);
1718 if (err || buf != ubuf)
1719 goto out;
1720 }
1721
1722 next = rcu_dereference_check_mce(mcelog.next);
1723
1724 /* Only supports full reads right now */
1725 err = -EINVAL;
1726 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
1727 goto out;
1728
1729 err = 0;
1730 prev = 0;
1731 do {
1732 for (i = prev; i < next; i++) {
1733 unsigned long start = jiffies;
1734 struct mce *m = &mcelog.entry[i];
1735
1736 while (!m->finished) {
1737 if (time_after_eq(jiffies, start + 2)) {
1738 memset(m, 0, sizeof(*m));
1739 goto timeout;
1740 }
1741 cpu_relax();
1742 }
1743 smp_rmb();
1744 err |= copy_to_user(buf, m, sizeof(*m));
1745 buf += sizeof(*m);
1746 timeout:
1747 ;
1748 }
1749
1750 memset(mcelog.entry + prev, 0,
1751 (next - prev) * sizeof(struct mce));
1752 prev = next;
1753 next = cmpxchg(&mcelog.next, prev, 0);
1754 } while (next != prev);
1755
1756 synchronize_sched();
1757
1758 /*
1759 * Collect entries that were still getting written before the
1760 * synchronize.
1761 */
1762 on_each_cpu(collect_tscs, cpu_tsc, 1);
1763
1764 for (i = next; i < MCE_LOG_LEN; i++) {
1765 struct mce *m = &mcelog.entry[i];
1766
1767 if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
1768 err |= copy_to_user(buf, m, sizeof(*m));
1769 smp_rmb();
1770 buf += sizeof(*m);
1771 memset(m, 0, sizeof(*m));
1772 }
1773 }
1774
1775 if (err)
1776 err = -EFAULT;
1777
1778 out:
1779 mutex_unlock(&mce_chrdev_read_mutex);
1780 kfree(cpu_tsc);
1781
1782 return err ? err : buf - ubuf;
1783 }
1784
1785 static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
1786 {
1787 poll_wait(file, &mce_chrdev_wait, wait);
1788 if (rcu_access_index(mcelog.next))
1789 return POLLIN | POLLRDNORM;
1790 if (!mce_apei_read_done && apei_check_mce())
1791 return POLLIN | POLLRDNORM;
1792 return 0;
1793 }
1794
1795 static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
1796 unsigned long arg)
1797 {
1798 int __user *p = (int __user *)arg;
1799
1800 if (!capable(CAP_SYS_ADMIN))
1801 return -EPERM;
1802
1803 switch (cmd) {
1804 case MCE_GET_RECORD_LEN:
1805 return put_user(sizeof(struct mce), p);
1806 case MCE_GET_LOG_LEN:
1807 return put_user(MCE_LOG_LEN, p);
1808 case MCE_GETCLEAR_FLAGS: {
1809 unsigned flags;
1810
1811 do {
1812 flags = mcelog.flags;
1813 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
1814
1815 return put_user(flags, p);
1816 }
1817 default:
1818 return -ENOTTY;
1819 }
1820 }
1821
1822 static ssize_t (*mce_write)(struct file *filp, const char __user *ubuf,
1823 size_t usize, loff_t *off);
1824
1825 void register_mce_write_callback(ssize_t (*fn)(struct file *filp,
1826 const char __user *ubuf,
1827 size_t usize, loff_t *off))
1828 {
1829 mce_write = fn;
1830 }
1831 EXPORT_SYMBOL_GPL(register_mce_write_callback);
1832
1833 ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
1834 size_t usize, loff_t *off)
1835 {
1836 if (mce_write)
1837 return mce_write(filp, ubuf, usize, off);
1838 else
1839 return -EINVAL;
1840 }
1841
1842 static const struct file_operations mce_chrdev_ops = {
1843 .open = mce_chrdev_open,
1844 .release = mce_chrdev_release,
1845 .read = mce_chrdev_read,
1846 .write = mce_chrdev_write,
1847 .poll = mce_chrdev_poll,
1848 .unlocked_ioctl = mce_chrdev_ioctl,
1849 .llseek = no_llseek,
1850 };
1851
1852 static struct miscdevice mce_chrdev_device = {
1853 MISC_MCELOG_MINOR,
1854 "mcelog",
1855 &mce_chrdev_ops,
1856 };
1857
1858 /*
1859 * mce=off Disables machine check
1860 * mce=no_cmci Disables CMCI
1861 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1862 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
1863 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1864 * monarchtimeout is how long to wait for other CPUs on machine
1865 * check, or 0 to not wait
1866 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1867 * mce=nobootlog Don't log MCEs from before booting.
1868 */
1869 static int __init mcheck_enable(char *str)
1870 {
1871 if (*str == 0) {
1872 enable_p5_mce();
1873 return 1;
1874 }
1875 if (*str == '=')
1876 str++;
1877 if (!strcmp(str, "off"))
1878 mce_disabled = 1;
1879 else if (!strcmp(str, "no_cmci"))
1880 mce_cmci_disabled = 1;
1881 else if (!strcmp(str, "dont_log_ce"))
1882 mce_dont_log_ce = 1;
1883 else if (!strcmp(str, "ignore_ce"))
1884 mce_ignore_ce = 1;
1885 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
1886 mce_bootlog = (str[0] == 'b');
1887 else if (isdigit(str[0])) {
1888 get_option(&str, &tolerant);
1889 if (*str == ',') {
1890 ++str;
1891 get_option(&str, &monarch_timeout);
1892 }
1893 } else {
1894 printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
1895 str);
1896 return 0;
1897 }
1898 return 1;
1899 }
1900 __setup("mce", mcheck_enable);
1901
1902 int __init mcheck_init(void)
1903 {
1904 mcheck_intel_therm_init();
1905
1906 return 0;
1907 }
1908
1909 /*
1910 * mce_syscore: PM support
1911 */
1912
1913 /*
1914 * Disable machine checks on suspend and shutdown. We can't really handle
1915 * them later.
1916 */
1917 static int mce_disable_error_reporting(void)
1918 {
1919 int i;
1920
1921 for (i = 0; i < banks; i++) {
1922 struct mce_bank *b = &mce_banks[i];
1923
1924 if (b->init)
1925 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
1926 }
1927 return 0;
1928 }
1929
1930 static int mce_syscore_suspend(void)
1931 {
1932 return mce_disable_error_reporting();
1933 }
1934
1935 static void mce_syscore_shutdown(void)
1936 {
1937 mce_disable_error_reporting();
1938 }
1939
1940 /*
1941 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
1942 * Only one CPU is active at this time, the others get re-added later using
1943 * CPU hotplug:
1944 */
1945 static void mce_syscore_resume(void)
1946 {
1947 __mcheck_cpu_init_generic();
1948 __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
1949 }
1950
1951 static struct syscore_ops mce_syscore_ops = {
1952 .suspend = mce_syscore_suspend,
1953 .shutdown = mce_syscore_shutdown,
1954 .resume = mce_syscore_resume,
1955 };
1956
1957 /*
1958 * mce_device: Sysfs support
1959 */
1960
1961 static void mce_cpu_restart(void *data)
1962 {
1963 if (!mce_available(__this_cpu_ptr(&cpu_info)))
1964 return;
1965 __mcheck_cpu_init_generic();
1966 __mcheck_cpu_init_timer();
1967 }
1968
1969 /* Reinit MCEs after user configuration changes */
1970 static void mce_restart(void)
1971 {
1972 mce_timer_delete_all();
1973 on_each_cpu(mce_cpu_restart, NULL, 1);
1974 }
1975
1976 /* Toggle features for corrected errors */
1977 static void mce_disable_cmci(void *data)
1978 {
1979 if (!mce_available(__this_cpu_ptr(&cpu_info)))
1980 return;
1981 cmci_clear();
1982 }
1983
1984 static void mce_enable_ce(void *all)
1985 {
1986 if (!mce_available(__this_cpu_ptr(&cpu_info)))
1987 return;
1988 cmci_reenable();
1989 cmci_recheck();
1990 if (all)
1991 __mcheck_cpu_init_timer();
1992 }
1993
1994 static struct bus_type mce_subsys = {
1995 .name = "machinecheck",
1996 .dev_name = "machinecheck",
1997 };
1998
1999 DEFINE_PER_CPU(struct device *, mce_device);
2000
2001 __cpuinitdata
2002 void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
2003
2004 static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
2005 {
2006 return container_of(attr, struct mce_bank, attr);
2007 }
2008
2009 static ssize_t show_bank(struct device *s, struct device_attribute *attr,
2010 char *buf)
2011 {
2012 return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
2013 }
2014
2015 static ssize_t set_bank(struct device *s, struct device_attribute *attr,
2016 const char *buf, size_t size)
2017 {
2018 u64 new;
2019
2020 if (strict_strtoull(buf, 0, &new) < 0)
2021 return -EINVAL;
2022
2023 attr_to_bank(attr)->ctl = new;
2024 mce_restart();
2025
2026 return size;
2027 }
2028
2029 static ssize_t
2030 show_trigger(struct device *s, struct device_attribute *attr, char *buf)
2031 {
2032 strcpy(buf, mce_helper);
2033 strcat(buf, "\n");
2034 return strlen(mce_helper) + 1;
2035 }
2036
2037 static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
2038 const char *buf, size_t siz)
2039 {
2040 char *p;
2041
2042 strncpy(mce_helper, buf, sizeof(mce_helper));
2043 mce_helper[sizeof(mce_helper)-1] = 0;
2044 p = strchr(mce_helper, '\n');
2045
2046 if (p)
2047 *p = 0;
2048
2049 return strlen(mce_helper) + !!p;
2050 }
2051
2052 static ssize_t set_ignore_ce(struct device *s,
2053 struct device_attribute *attr,
2054 const char *buf, size_t size)
2055 {
2056 u64 new;
2057
2058 if (strict_strtoull(buf, 0, &new) < 0)
2059 return -EINVAL;
2060
2061 if (mce_ignore_ce ^ !!new) {
2062 if (new) {
2063 /* disable ce features */
2064 mce_timer_delete_all();
2065 on_each_cpu(mce_disable_cmci, NULL, 1);
2066 mce_ignore_ce = 1;
2067 } else {
2068 /* enable ce features */
2069 mce_ignore_ce = 0;
2070 on_each_cpu(mce_enable_ce, (void *)1, 1);
2071 }
2072 }
2073 return size;
2074 }
2075
2076 static ssize_t set_cmci_disabled(struct device *s,
2077 struct device_attribute *attr,
2078 const char *buf, size_t size)
2079 {
2080 u64 new;
2081
2082 if (strict_strtoull(buf, 0, &new) < 0)
2083 return -EINVAL;
2084
2085 if (mce_cmci_disabled ^ !!new) {
2086 if (new) {
2087 /* disable cmci */
2088 on_each_cpu(mce_disable_cmci, NULL, 1);
2089 mce_cmci_disabled = 1;
2090 } else {
2091 /* enable cmci */
2092 mce_cmci_disabled = 0;
2093 on_each_cpu(mce_enable_ce, NULL, 1);
2094 }
2095 }
2096 return size;
2097 }
2098
2099 static ssize_t store_int_with_restart(struct device *s,
2100 struct device_attribute *attr,
2101 const char *buf, size_t size)
2102 {
2103 ssize_t ret = device_store_int(s, attr, buf, size);
2104 mce_restart();
2105 return ret;
2106 }
2107
2108 static DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
2109 static DEVICE_INT_ATTR(tolerant, 0644, tolerant);
2110 static DEVICE_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
2111 static DEVICE_INT_ATTR(dont_log_ce, 0644, mce_dont_log_ce);
2112
2113 static struct dev_ext_attribute dev_attr_check_interval = {
2114 __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
2115 &check_interval
2116 };
2117
2118 static struct dev_ext_attribute dev_attr_ignore_ce = {
2119 __ATTR(ignore_ce, 0644, device_show_int, set_ignore_ce),
2120 &mce_ignore_ce
2121 };
2122
2123 static struct dev_ext_attribute dev_attr_cmci_disabled = {
2124 __ATTR(cmci_disabled, 0644, device_show_int, set_cmci_disabled),
2125 &mce_cmci_disabled
2126 };
2127
2128 static struct device_attribute *mce_device_attrs[] = {
2129 &dev_attr_tolerant.attr,
2130 &dev_attr_check_interval.attr,
2131 &dev_attr_trigger,
2132 &dev_attr_monarch_timeout.attr,
2133 &dev_attr_dont_log_ce.attr,
2134 &dev_attr_ignore_ce.attr,
2135 &dev_attr_cmci_disabled.attr,
2136 NULL
2137 };
2138
2139 static cpumask_var_t mce_device_initialized;
2140
2141 static void mce_device_release(struct device *dev)
2142 {
2143 kfree(dev);
2144 }
2145
2146 /* Per cpu device init. All of the cpus still share the same ctrl bank: */
2147 static __cpuinit int mce_device_create(unsigned int cpu)
2148 {
2149 struct device *dev;
2150 int err;
2151 int i, j;
2152
2153 if (!mce_available(&boot_cpu_data))
2154 return -EIO;
2155
2156 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2157 if (!dev)
2158 return -ENOMEM;
2159 dev->id = cpu;
2160 dev->bus = &mce_subsys;
2161 dev->release = &mce_device_release;
2162
2163 err = device_register(dev);
2164 if (err)
2165 return err;
2166
2167 for (i = 0; mce_device_attrs[i]; i++) {
2168 err = device_create_file(dev, mce_device_attrs[i]);
2169 if (err)
2170 goto error;
2171 }
2172 for (j = 0; j < banks; j++) {
2173 err = device_create_file(dev, &mce_banks[j].attr);
2174 if (err)
2175 goto error2;
2176 }
2177 cpumask_set_cpu(cpu, mce_device_initialized);
2178 per_cpu(mce_device, cpu) = dev;
2179
2180 return 0;
2181 error2:
2182 while (--j >= 0)
2183 device_remove_file(dev, &mce_banks[j].attr);
2184 error:
2185 while (--i >= 0)
2186 device_remove_file(dev, mce_device_attrs[i]);
2187
2188 device_unregister(dev);
2189
2190 return err;
2191 }
2192
2193 static __cpuinit void mce_device_remove(unsigned int cpu)
2194 {
2195 struct device *dev = per_cpu(mce_device, cpu);
2196 int i;
2197
2198 if (!cpumask_test_cpu(cpu, mce_device_initialized))
2199 return;
2200
2201 for (i = 0; mce_device_attrs[i]; i++)
2202 device_remove_file(dev, mce_device_attrs[i]);
2203
2204 for (i = 0; i < banks; i++)
2205 device_remove_file(dev, &mce_banks[i].attr);
2206
2207 device_unregister(dev);
2208 cpumask_clear_cpu(cpu, mce_device_initialized);
2209 per_cpu(mce_device, cpu) = NULL;
2210 }
2211
2212 /* Make sure there are no machine checks on offlined CPUs. */
2213 static void __cpuinit mce_disable_cpu(void *h)
2214 {
2215 unsigned long action = *(unsigned long *)h;
2216 int i;
2217
2218 if (!mce_available(__this_cpu_ptr(&cpu_info)))
2219 return;
2220
2221 if (!(action & CPU_TASKS_FROZEN))
2222 cmci_clear();
2223 for (i = 0; i < banks; i++) {
2224 struct mce_bank *b = &mce_banks[i];
2225
2226 if (b->init)
2227 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
2228 }
2229 }
2230
2231 static void __cpuinit mce_reenable_cpu(void *h)
2232 {
2233 unsigned long action = *(unsigned long *)h;
2234 int i;
2235
2236 if (!mce_available(__this_cpu_ptr(&cpu_info)))
2237 return;
2238
2239 if (!(action & CPU_TASKS_FROZEN))
2240 cmci_reenable();
2241 for (i = 0; i < banks; i++) {
2242 struct mce_bank *b = &mce_banks[i];
2243
2244 if (b->init)
2245 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
2246 }
2247 }
2248
2249 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
2250 static int __cpuinit
2251 mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
2252 {
2253 unsigned int cpu = (unsigned long)hcpu;
2254 struct timer_list *t = &per_cpu(mce_timer, cpu);
2255
2256 switch (action) {
2257 case CPU_ONLINE:
2258 case CPU_ONLINE_FROZEN:
2259 mce_device_create(cpu);
2260 if (threshold_cpu_callback)
2261 threshold_cpu_callback(action, cpu);
2262 break;
2263 case CPU_DEAD:
2264 case CPU_DEAD_FROZEN:
2265 if (threshold_cpu_callback)
2266 threshold_cpu_callback(action, cpu);
2267 mce_device_remove(cpu);
2268 break;
2269 case CPU_DOWN_PREPARE:
2270 case CPU_DOWN_PREPARE_FROZEN:
2271 del_timer_sync(t);
2272 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
2273 break;
2274 case CPU_DOWN_FAILED:
2275 case CPU_DOWN_FAILED_FROZEN:
2276 if (!mce_ignore_ce && check_interval) {
2277 t->expires = round_jiffies(jiffies +
2278 per_cpu(mce_next_interval, cpu));
2279 add_timer_on(t, cpu);
2280 }
2281 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
2282 break;
2283 case CPU_POST_DEAD:
2284 /* intentionally ignoring frozen here */
2285 cmci_rediscover(cpu);
2286 break;
2287 }
2288 return NOTIFY_OK;
2289 }
2290
2291 static struct notifier_block mce_cpu_notifier __cpuinitdata = {
2292 .notifier_call = mce_cpu_callback,
2293 };
2294
2295 static __init void mce_init_banks(void)
2296 {
2297 int i;
2298
2299 for (i = 0; i < banks; i++) {
2300 struct mce_bank *b = &mce_banks[i];
2301 struct device_attribute *a = &b->attr;
2302
2303 sysfs_attr_init(&a->attr);
2304 a->attr.name = b->attrname;
2305 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
2306
2307 a->attr.mode = 0644;
2308 a->show = show_bank;
2309 a->store = set_bank;
2310 }
2311 }
2312
2313 static __init int mcheck_init_device(void)
2314 {
2315 int err;
2316 int i = 0;
2317
2318 if (!mce_available(&boot_cpu_data))
2319 return -EIO;
2320
2321 zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL);
2322
2323 mce_init_banks();
2324
2325 err = subsys_system_register(&mce_subsys, NULL);
2326 if (err)
2327 return err;
2328
2329 for_each_online_cpu(i) {
2330 err = mce_device_create(i);
2331 if (err)
2332 return err;
2333 }
2334
2335 register_syscore_ops(&mce_syscore_ops);
2336 register_hotcpu_notifier(&mce_cpu_notifier);
2337
2338 /* register character device /dev/mcelog */
2339 misc_register(&mce_chrdev_device);
2340
2341 return err;
2342 }
2343 device_initcall_sync(mcheck_init_device);
2344
2345 /*
2346 * Old style boot options parsing. Only for compatibility.
2347 */
2348 static int __init mcheck_disable(char *str)
2349 {
2350 mce_disabled = 1;
2351 return 1;
2352 }
2353 __setup("nomce", mcheck_disable);
2354
2355 #ifdef CONFIG_DEBUG_FS
2356 struct dentry *mce_get_debugfs_dir(void)
2357 {
2358 static struct dentry *dmce;
2359
2360 if (!dmce)
2361 dmce = debugfs_create_dir("mce", NULL);
2362
2363 return dmce;
2364 }
2365
2366 static void mce_reset(void)
2367 {
2368 cpu_missing = 0;
2369 atomic_set(&mce_fake_paniced, 0);
2370 atomic_set(&mce_executing, 0);
2371 atomic_set(&mce_callin, 0);
2372 atomic_set(&global_nwo, 0);
2373 }
2374
2375 static int fake_panic_get(void *data, u64 *val)
2376 {
2377 *val = fake_panic;
2378 return 0;
2379 }
2380
2381 static int fake_panic_set(void *data, u64 val)
2382 {
2383 mce_reset();
2384 fake_panic = val;
2385 return 0;
2386 }
2387
2388 DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
2389 fake_panic_set, "%llu\n");
2390
2391 static int __init mcheck_debugfs_init(void)
2392 {
2393 struct dentry *dmce, *ffake_panic;
2394
2395 dmce = mce_get_debugfs_dir();
2396 if (!dmce)
2397 return -ENOMEM;
2398 ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
2399 &fake_panic_fops);
2400 if (!ffake_panic)
2401 return -ENOMEM;
2402
2403 return 0;
2404 }
2405 late_initcall(mcheck_debugfs_init);
2406 #endif
This page took 0.078377 seconds and 5 git commands to generate.