blob: cba8cd3e957bfe4ec6724e855961121105ad3e6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Machine check handler.
Ingo Molnare9eee032009-04-08 12:31:17 +02003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02005 * Rest from unknown author(s).
6 * 2004 Andi Kleen. Rewrote most of it.
Andi Kleenb79109c2009-02-12 13:43:23 +01007 * Copyright 2008 Intel Corporation
8 * Author: Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Tim Hockine02e68d2007-07-21 17:10:36 +020010#include <linux/thread_info.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020011#include <linux/capability.h>
12#include <linux/miscdevice.h>
Andi Kleenccc3c312009-05-27 21:56:54 +020013#include <linux/interrupt.h>
Andi Kleen8457c842009-02-12 13:49:33 +010014#include <linux/ratelimit.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020015#include <linux/kallsyms.h>
16#include <linux/rcupdate.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020017#include <linux/kobject.h>
Hidetoshi Seto14a02532009-04-30 16:04:51 +090018#include <linux/uaccess.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020019#include <linux/kdebug.h>
20#include <linux/kernel.h>
21#include <linux/percpu.h>
22#include <linux/string.h>
23#include <linux/sysdev.h>
Andi Kleen3c079792009-05-27 21:56:55 +020024#include <linux/delay.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020025#include <linux/ctype.h>
26#include <linux/sched.h>
27#include <linux/sysfs.h>
28#include <linux/types.h>
29#include <linux/init.h>
30#include <linux/kmod.h>
31#include <linux/poll.h>
Andi Kleen3c079792009-05-27 21:56:55 +020032#include <linux/nmi.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020033#include <linux/cpu.h>
Hidetoshi Seto14a02532009-04-30 16:04:51 +090034#include <linux/smp.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020035#include <linux/fs.h>
Andi Kleen9b1beaf2009-05-27 21:56:59 +020036#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Ingo Molnare9eee032009-04-08 12:31:17 +020038#include <asm/processor.h>
Andi Kleenccc3c312009-05-27 21:56:54 +020039#include <asm/hw_irq.h>
40#include <asm/apic.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020041#include <asm/idle.h>
Andi Kleenccc3c312009-05-27 21:56:54 +020042#include <asm/ipi.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020043#include <asm/mce.h>
44#include <asm/msr.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020045
Andi Kleenbd19a5e2009-05-27 21:56:55 +020046#include "mce-internal.h"
Ingo Molnar711c2e42009-04-08 12:31:26 +020047
Andi Kleen5d727922009-04-27 19:25:48 +020048/* Handle unconfigured int18 (should never happen) */
49static void unexpected_machine_check(struct pt_regs *regs, long error_code)
50{
51 printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
52 smp_processor_id());
53}
54
55/* Call the installed machine check handler for this CPU setup. */
56void (*machine_check_vector)(struct pt_regs *, long error_code) =
57 unexpected_machine_check;
Andi Kleen04b2b1a2009-04-28 22:50:19 +020058
Hidetoshi Seto4e5b3e62009-06-15 17:20:20 +090059int mce_disabled __read_mostly;
Andi Kleen04b2b1a2009-04-28 22:50:19 +020060
Andi Kleen4efc0672009-04-28 19:07:31 +020061#ifdef CONFIG_X86_NEW_MCE
Ingo Molnar711c2e42009-04-08 12:31:26 +020062
Ingo Molnare9eee032009-04-08 12:31:17 +020063#define MISC_MCELOG_MINOR 227
Andi Kleen0d7482e32009-02-17 23:07:13 +010064
Andi Kleen3c079792009-05-27 21:56:55 +020065#define SPINUNIT 100 /* 100ns */
66
Andi Kleen553f2652006-04-07 19:49:57 +020067atomic_t mce_entry;
68
Andi Kleen01ca79f2009-05-27 21:56:52 +020069DEFINE_PER_CPU(unsigned, mce_exception_count);
70
Tim Hockinbd784322007-07-21 17:10:37 +020071/*
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 corrected errors
76 * 3: never panic or SIGBUS, log all errors (for testing only)
77 */
Hidetoshi Seto4e5b3e62009-06-15 17:20:20 +090078static int tolerant __read_mostly = 1;
79static int banks __read_mostly;
80static u64 *bank __read_mostly;
81static int rip_msr __read_mostly;
82static int mce_bootlog __read_mostly = -1;
83static int monarch_timeout __read_mostly = -1;
84static int mce_panic_timeout __read_mostly;
85static int mce_dont_log_ce __read_mostly;
86int mce_cmci_disabled __read_mostly;
87int mce_ignore_ce __read_mostly;
88int mce_ser __read_mostly;
Andi Kleena98f0dd2007-02-13 13:26:23 +010089
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +090090/* User mode helper program triggered by machine check event */
91static unsigned long mce_need_notify;
92static char mce_helper[128];
93static char *mce_helper_argv[2] = { mce_helper, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Andi Kleen06b7a7a2009-04-27 18:37:43 +020095static unsigned long dont_init_banks;
96
Tim Hockine02e68d2007-07-21 17:10:36 +020097static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
Andi Kleen3c079792009-05-27 21:56:55 +020098static DEFINE_PER_CPU(struct mce, mces_seen);
99static int cpu_missing;
100
Tim Hockine02e68d2007-07-21 17:10:36 +0200101
Andi Kleenee031c32009-02-12 13:49:34 +0100102/* MCA banks polled by the period polling timer for corrected events */
103DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
104 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
105};
106
Andi Kleen06b7a7a2009-04-27 18:37:43 +0200107static inline int skip_bank_init(int i)
108{
109 return i < BITS_PER_LONG && test_bit(i, &dont_init_banks);
110}
111
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200112static DEFINE_PER_CPU(struct work_struct, mce_work);
113
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100114/* Do initial initialization of a struct mce */
115void mce_setup(struct mce *m)
116{
117 memset(m, 0, sizeof(struct mce));
Andi Kleend620c672009-05-27 21:56:56 +0200118 m->cpu = m->extcpu = smp_processor_id();
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100119 rdtscll(m->tsc);
Andi Kleen8ee08342009-05-27 21:56:56 +0200120 /* We hope get_seconds stays lockless */
121 m->time = get_seconds();
122 m->cpuvendor = boot_cpu_data.x86_vendor;
123 m->cpuid = cpuid_eax(1);
124#ifdef CONFIG_SMP
125 m->socketid = cpu_data(m->extcpu).phys_proc_id;
126#endif
127 m->apicid = cpu_data(m->extcpu).initial_apicid;
128 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100129}
130
Andi Kleenea149b32009-04-29 19:31:00 +0200131DEFINE_PER_CPU(struct mce, injectm);
132EXPORT_PER_CPU_SYMBOL_GPL(injectm);
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * Lockless MCE logging infrastructure.
136 * This avoids deadlocks on printk locks without having to break locks. Also
137 * separate MCEs from kernel messages to avoid bogus bug reports.
138 */
139
Adrian Bunk231fd902008-01-30 13:30:30 +0100140static struct mce_log mcelog = {
Andi Kleenf6fb0ac2009-05-27 21:56:55 +0200141 .signature = MCE_LOG_SIGNATURE,
142 .len = MCE_LOG_LEN,
143 .recordlen = sizeof(struct mce),
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200144};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146void mce_log(struct mce *mce)
147{
148 unsigned next, entry;
Ingo Molnare9eee032009-04-08 12:31:17 +0200149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 mce->finished = 0;
Mike Waychison76441432005-09-30 00:01:27 +0200151 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 for (;;) {
153 entry = rcu_dereference(mcelog.next);
Andi Kleen673242c2005-09-12 18:49:24 +0200154 for (;;) {
Ingo Molnare9eee032009-04-08 12:31:17 +0200155 /*
156 * When the buffer fills up discard new entries.
157 * Assume that the earlier errors are the more
158 * interesting ones:
159 */
Andi Kleen673242c2005-09-12 18:49:24 +0200160 if (entry >= MCE_LOG_LEN) {
Hidetoshi Seto14a02532009-04-30 16:04:51 +0900161 set_bit(MCE_OVERFLOW,
162 (unsigned long *)&mcelog.flags);
Andi Kleen673242c2005-09-12 18:49:24 +0200163 return;
164 }
Ingo Molnare9eee032009-04-08 12:31:17 +0200165 /* Old left over entry. Skip: */
Andi Kleen673242c2005-09-12 18:49:24 +0200166 if (mcelog.entry[entry].finished) {
167 entry++;
168 continue;
169 }
Mike Waychison76441432005-09-30 00:01:27 +0200170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 smp_rmb();
173 next = entry + 1;
174 if (cmpxchg(&mcelog.next, entry, next) == entry)
175 break;
176 }
177 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
Mike Waychison76441432005-09-30 00:01:27 +0200178 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 mcelog.entry[entry].finished = 1;
Mike Waychison76441432005-09-30 00:01:27 +0200180 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Andi Kleena0189c72009-05-27 21:56:54 +0200182 mce->finished = 1;
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +0900183 set_bit(0, &mce_need_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900186static void print_mce(struct mce *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Andi Kleen86503562009-05-27 21:56:58 +0200188 printk(KERN_EMERG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
Andi Kleend620c672009-05-27 21:56:56 +0200190 m->extcpu, m->mcgstatus, m->bank, m->status);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100191 if (m->ip) {
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200192 printk(KERN_EMERG "RIP%s %02x:<%016Lx> ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100194 m->cs, m->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (m->cs == __KERNEL_CS)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100196 print_symbol("{%s}", m->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 printk("\n");
198 }
H. Peter Anvinf6d18262009-02-19 15:44:58 -0800199 printk(KERN_EMERG "TSC %llx ", m->tsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (m->addr)
H. Peter Anvinf6d18262009-02-19 15:44:58 -0800201 printk("ADDR %llx ", m->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (m->misc)
H. Peter Anvinf6d18262009-02-19 15:44:58 -0800203 printk("MISC %llx ", m->misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 printk("\n");
Andi Kleen8ee08342009-05-27 21:56:56 +0200205 printk(KERN_EMERG "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
206 m->cpuvendor, m->cpuid, m->time, m->socketid,
207 m->apicid);
Andi Kleen86503562009-05-27 21:56:58 +0200208}
209
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900210static void print_mce_head(void)
211{
212 printk(KERN_EMERG "\n" KERN_EMERG "HARDWARE ERROR\n");
213}
214
Andi Kleen86503562009-05-27 21:56:58 +0200215static void print_mce_tail(void)
216{
217 printk(KERN_EMERG "This is not a software problem!\n"
218 KERN_EMERG "Run through mcelog --ascii to decode and contact your hardware vendor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Andi Kleenf94b61c2009-05-27 21:56:55 +0200221#define PANIC_TIMEOUT 5 /* 5 seconds */
222
223static atomic_t mce_paniced;
224
225/* Panic in progress. Enable interrupts and wait for final IPI */
226static void wait_for_panic(void)
227{
228 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
229 preempt_disable();
230 local_irq_enable();
231 while (timeout-- > 0)
232 udelay(1);
Andi Kleen29b0f592009-05-27 21:56:56 +0200233 if (panic_timeout == 0)
234 panic_timeout = mce_panic_timeout;
Andi Kleenf94b61c2009-05-27 21:56:55 +0200235 panic("Panicing machine check CPU died");
236}
237
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200238static void mce_panic(char *msg, struct mce *final, char *exp)
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200239{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 int i;
Tim Hockine02e68d2007-07-21 17:10:36 +0200241
Andi Kleenf94b61c2009-05-27 21:56:55 +0200242 /*
243 * Make sure only one CPU runs in machine check panic
244 */
245 if (atomic_add_return(1, &mce_paniced) > 1)
246 wait_for_panic();
247 barrier();
248
Andi Kleend896a942009-04-28 14:25:18 +0200249 bust_spinlocks(1);
250 console_verbose();
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900251 print_mce_head();
Andi Kleena0189c72009-05-27 21:56:54 +0200252 /* First print corrected ones that are still unlogged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 for (i = 0; i < MCE_LOG_LEN; i++) {
Andi Kleena0189c72009-05-27 21:56:54 +0200254 struct mce *m = &mcelog.entry[i];
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900255 if (!(m->status & MCI_STATUS_VAL))
256 continue;
257 if (!(m->status & MCI_STATUS_UC))
258 print_mce(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Andi Kleena0189c72009-05-27 21:56:54 +0200260 /* Now print uncorrected but with the final one last */
261 for (i = 0; i < MCE_LOG_LEN; i++) {
262 struct mce *m = &mcelog.entry[i];
263 if (!(m->status & MCI_STATUS_VAL))
264 continue;
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900265 if (!(m->status & MCI_STATUS_UC))
266 continue;
Andi Kleena0189c72009-05-27 21:56:54 +0200267 if (!final || memcmp(m, final, sizeof(struct mce)))
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900268 print_mce(m);
Andi Kleena0189c72009-05-27 21:56:54 +0200269 }
270 if (final)
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900271 print_mce(final);
Andi Kleen3c079792009-05-27 21:56:55 +0200272 if (cpu_missing)
273 printk(KERN_EMERG "Some CPUs didn't answer in synchronization\n");
Andi Kleen86503562009-05-27 21:56:58 +0200274 print_mce_tail();
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200275 if (exp)
276 printk(KERN_EMERG "Machine check: %s\n", exp);
Andi Kleen29b0f592009-05-27 21:56:56 +0200277 if (panic_timeout == 0)
278 panic_timeout = mce_panic_timeout;
Tim Hockine02e68d2007-07-21 17:10:36 +0200279 panic(msg);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200280}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Andi Kleenea149b32009-04-29 19:31:00 +0200282/* Support code for software error injection */
283
284static int msr_to_offset(u32 msr)
285{
286 unsigned bank = __get_cpu_var(injectm.bank);
287 if (msr == rip_msr)
288 return offsetof(struct mce, ip);
289 if (msr == MSR_IA32_MC0_STATUS + bank*4)
290 return offsetof(struct mce, status);
291 if (msr == MSR_IA32_MC0_ADDR + bank*4)
292 return offsetof(struct mce, addr);
293 if (msr == MSR_IA32_MC0_MISC + bank*4)
294 return offsetof(struct mce, misc);
295 if (msr == MSR_IA32_MCG_STATUS)
296 return offsetof(struct mce, mcgstatus);
297 return -1;
298}
299
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200300/* MSR access wrappers used for error injection */
301static u64 mce_rdmsrl(u32 msr)
302{
303 u64 v;
Andi Kleenea149b32009-04-29 19:31:00 +0200304 if (__get_cpu_var(injectm).finished) {
305 int offset = msr_to_offset(msr);
306 if (offset < 0)
307 return 0;
308 return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
309 }
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200310 rdmsrl(msr, v);
311 return v;
312}
313
314static void mce_wrmsrl(u32 msr, u64 v)
315{
Andi Kleenea149b32009-04-29 19:31:00 +0200316 if (__get_cpu_var(injectm).finished) {
317 int offset = msr_to_offset(msr);
318 if (offset >= 0)
319 *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
320 return;
321 }
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200322 wrmsrl(msr, v);
323}
324
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200325/*
326 * Simple lockless ring to communicate PFNs from the exception handler with the
327 * process context work function. This is vastly simplified because there's
328 * only a single reader and a single writer.
329 */
330#define MCE_RING_SIZE 16 /* we use one entry less */
331
332struct mce_ring {
333 unsigned short start;
334 unsigned short end;
335 unsigned long ring[MCE_RING_SIZE];
336};
337static DEFINE_PER_CPU(struct mce_ring, mce_ring);
338
339/* Runs with CPU affinity in workqueue */
340static int mce_ring_empty(void)
341{
342 struct mce_ring *r = &__get_cpu_var(mce_ring);
343
344 return r->start == r->end;
345}
346
347static int mce_ring_get(unsigned long *pfn)
348{
349 struct mce_ring *r;
350 int ret = 0;
351
352 *pfn = 0;
353 get_cpu();
354 r = &__get_cpu_var(mce_ring);
355 if (r->start == r->end)
356 goto out;
357 *pfn = r->ring[r->start];
358 r->start = (r->start + 1) % MCE_RING_SIZE;
359 ret = 1;
360out:
361 put_cpu();
362 return ret;
363}
364
365/* Always runs in MCE context with preempt off */
366static int mce_ring_add(unsigned long pfn)
367{
368 struct mce_ring *r = &__get_cpu_var(mce_ring);
369 unsigned next;
370
371 next = (r->end + 1) % MCE_RING_SIZE;
372 if (next == r->start)
373 return -1;
374 r->ring[r->end] = pfn;
375 wmb();
376 r->end = next;
377 return 0;
378}
379
Andi Kleen88ccbed2009-02-12 13:49:36 +0100380int mce_available(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Andi Kleen04b2b1a2009-04-28 22:50:19 +0200382 if (mce_disabled)
Andi Kleen5b4408f2009-02-12 13:39:30 +0100383 return 0;
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800384 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200387static void mce_schedule_work(void)
388{
389 if (!mce_ring_empty()) {
390 struct work_struct *work = &__get_cpu_var(mce_work);
391 if (!work_pending(work))
392 schedule_work(work);
393 }
394}
395
Huang Ying1b2797d2009-05-27 21:56:51 +0200396/*
397 * Get the address of the instruction at the time of the machine check
398 * error.
399 */
Andi Kleen94ad8472005-04-16 15:25:09 -0700400static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
401{
Huang Ying1b2797d2009-05-27 21:56:51 +0200402
403 if (regs && (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV))) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100404 m->ip = regs->ip;
Andi Kleen94ad8472005-04-16 15:25:09 -0700405 m->cs = regs->cs;
406 } else {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100407 m->ip = 0;
Andi Kleen94ad8472005-04-16 15:25:09 -0700408 m->cs = 0;
409 }
Huang Ying1b2797d2009-05-27 21:56:51 +0200410 if (rip_msr)
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200411 m->ip = mce_rdmsrl(rip_msr);
Andi Kleen94ad8472005-04-16 15:25:09 -0700412}
413
Andi Kleenccc3c312009-05-27 21:56:54 +0200414#ifdef CONFIG_X86_LOCAL_APIC
415/*
416 * Called after interrupts have been reenabled again
417 * when a MCE happened during an interrupts off region
418 * in the kernel.
419 */
420asmlinkage void smp_mce_self_interrupt(struct pt_regs *regs)
421{
422 ack_APIC_irq();
423 exit_idle();
424 irq_enter();
Andi Kleen9ff36ee2009-05-27 21:56:58 +0200425 mce_notify_irq();
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200426 mce_schedule_work();
Andi Kleenccc3c312009-05-27 21:56:54 +0200427 irq_exit();
428}
429#endif
430
431static void mce_report_event(struct pt_regs *regs)
432{
433 if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
Andi Kleen9ff36ee2009-05-27 21:56:58 +0200434 mce_notify_irq();
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200435 /*
436 * Triggering the work queue here is just an insurance
437 * policy in case the syscall exit notify handler
438 * doesn't run soon enough or ends up running on the
439 * wrong CPU (can happen when audit sleeps)
440 */
441 mce_schedule_work();
Andi Kleenccc3c312009-05-27 21:56:54 +0200442 return;
443 }
444
445#ifdef CONFIG_X86_LOCAL_APIC
446 /*
447 * Without APIC do not notify. The event will be picked
448 * up eventually.
449 */
450 if (!cpu_has_apic)
451 return;
452
453 /*
454 * When interrupts are disabled we cannot use
455 * kernel services safely. Trigger an self interrupt
456 * through the APIC to instead do the notification
457 * after interrupts are reenabled again.
458 */
459 apic->send_IPI_self(MCE_SELF_VECTOR);
460
461 /*
462 * Wait for idle afterwards again so that we don't leave the
463 * APIC in a non idle state because the normal APIC writes
464 * cannot exclude us.
465 */
466 apic_wait_icr_idle();
467#endif
468}
469
Andi Kleenca84f692009-05-27 21:56:57 +0200470DEFINE_PER_CPU(unsigned, mce_poll_count);
471
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200472/*
Andi Kleenb79109c2009-02-12 13:43:23 +0100473 * Poll for corrected events or events that happened before reset.
474 * Those are just logged through /dev/mcelog.
475 *
476 * This is executed in standard interrupt context.
Andi Kleened7290d2009-05-27 21:56:57 +0200477 *
478 * Note: spec recommends to panic for fatal unsignalled
479 * errors here. However this would be quite problematic --
480 * we would need to reimplement the Monarch handling and
481 * it would mess up the exclusion between exception handler
482 * and poll hander -- * so we skip this for now.
483 * These cases should not happen anyways, or only when the CPU
484 * is already totally * confused. In this case it's likely it will
485 * not fully execute the machine check handler either.
Andi Kleenb79109c2009-02-12 13:43:23 +0100486 */
Andi Kleenee031c32009-02-12 13:49:34 +0100487void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
Andi Kleenb79109c2009-02-12 13:43:23 +0100488{
489 struct mce m;
490 int i;
491
Andi Kleenca84f692009-05-27 21:56:57 +0200492 __get_cpu_var(mce_poll_count)++;
493
Andi Kleenb79109c2009-02-12 13:43:23 +0100494 mce_setup(&m);
495
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200496 m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
Andi Kleenb79109c2009-02-12 13:43:23 +0100497 for (i = 0; i < banks; i++) {
Andi Kleenee031c32009-02-12 13:49:34 +0100498 if (!bank[i] || !test_bit(i, *b))
Andi Kleenb79109c2009-02-12 13:43:23 +0100499 continue;
500
501 m.misc = 0;
502 m.addr = 0;
503 m.bank = i;
504 m.tsc = 0;
505
506 barrier();
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200507 m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
Andi Kleenb79109c2009-02-12 13:43:23 +0100508 if (!(m.status & MCI_STATUS_VAL))
509 continue;
510
511 /*
Andi Kleened7290d2009-05-27 21:56:57 +0200512 * Uncorrected or signalled events are handled by the exception
513 * handler when it is enabled, so don't process those here.
Andi Kleenb79109c2009-02-12 13:43:23 +0100514 *
515 * TBD do the same check for MCI_STATUS_EN here?
516 */
Andi Kleened7290d2009-05-27 21:56:57 +0200517 if (!(flags & MCP_UC) &&
518 (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
Andi Kleenb79109c2009-02-12 13:43:23 +0100519 continue;
520
521 if (m.status & MCI_STATUS_MISCV)
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200522 m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
Andi Kleenb79109c2009-02-12 13:43:23 +0100523 if (m.status & MCI_STATUS_ADDRV)
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200524 m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
Andi Kleenb79109c2009-02-12 13:43:23 +0100525
526 if (!(flags & MCP_TIMESTAMP))
527 m.tsc = 0;
528 /*
529 * Don't get the IP here because it's unlikely to
530 * have anything to do with the actual error location.
531 */
Hidetoshi Seto62fdac52009-06-11 16:06:07 +0900532 if (!(flags & MCP_DONTLOG) && !mce_dont_log_ce) {
Andi Kleen5679af42009-04-07 17:06:55 +0200533 mce_log(&m);
534 add_taint(TAINT_MACHINE_CHECK);
535 }
Andi Kleenb79109c2009-02-12 13:43:23 +0100536
537 /*
538 * Clear state for this bank.
539 */
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200540 mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
Andi Kleenb79109c2009-02-12 13:43:23 +0100541 }
542
543 /*
544 * Don't clear MCG_STATUS here because it's only defined for
545 * exceptions.
546 */
Andi Kleen88921be2009-05-27 21:56:51 +0200547
548 sync_core();
Andi Kleenb79109c2009-02-12 13:43:23 +0100549}
Andi Kleenea149b32009-04-29 19:31:00 +0200550EXPORT_SYMBOL_GPL(machine_check_poll);
Andi Kleenb79109c2009-02-12 13:43:23 +0100551
552/*
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200553 * Do a quick check if any of the events requires a panic.
554 * This decides if we keep the events around or clear them.
555 */
556static int mce_no_way_out(struct mce *m, char **msg)
557{
558 int i;
559
560 for (i = 0; i < banks; i++) {
561 m->status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
562 if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
563 return 1;
564 }
565 return 0;
566}
567
568/*
Andi Kleen3c079792009-05-27 21:56:55 +0200569 * Variable to establish order between CPUs while scanning.
570 * Each CPU spins initially until executing is equal its number.
571 */
572static atomic_t mce_executing;
573
574/*
575 * Defines order of CPUs on entry. First CPU becomes Monarch.
576 */
577static atomic_t mce_callin;
578
579/*
580 * Check if a timeout waiting for other CPUs happened.
581 */
582static int mce_timed_out(u64 *t)
583{
584 /*
585 * The others already did panic for some reason.
586 * Bail out like in a timeout.
587 * rmb() to tell the compiler that system_state
588 * might have been modified by someone else.
589 */
590 rmb();
591 if (atomic_read(&mce_paniced))
592 wait_for_panic();
593 if (!monarch_timeout)
594 goto out;
595 if ((s64)*t < SPINUNIT) {
596 /* CHECKME: Make panic default for 1 too? */
597 if (tolerant < 1)
598 mce_panic("Timeout synchronizing machine check over CPUs",
599 NULL, NULL);
600 cpu_missing = 1;
601 return 1;
602 }
603 *t -= SPINUNIT;
604out:
605 touch_nmi_watchdog();
606 return 0;
607}
608
609/*
610 * The Monarch's reign. The Monarch is the CPU who entered
611 * the machine check handler first. It waits for the others to
612 * raise the exception too and then grades them. When any
613 * error is fatal panic. Only then let the others continue.
614 *
615 * The other CPUs entering the MCE handler will be controlled by the
616 * Monarch. They are called Subjects.
617 *
618 * This way we prevent any potential data corruption in a unrecoverable case
619 * and also makes sure always all CPU's errors are examined.
620 *
621 * Also this detects the case of an machine check event coming from outer
622 * space (not detected by any CPUs) In this case some external agent wants
623 * us to shut down, so panic too.
624 *
625 * The other CPUs might still decide to panic if the handler happens
626 * in a unrecoverable place, but in this case the system is in a semi-stable
627 * state and won't corrupt anything by itself. It's ok to let the others
628 * continue for a bit first.
629 *
630 * All the spin loops have timeouts; when a timeout happens a CPU
631 * typically elects itself to be Monarch.
632 */
633static void mce_reign(void)
634{
635 int cpu;
636 struct mce *m = NULL;
637 int global_worst = 0;
638 char *msg = NULL;
639 char *nmsg = NULL;
640
641 /*
642 * This CPU is the Monarch and the other CPUs have run
643 * through their handlers.
644 * Grade the severity of the errors of all the CPUs.
645 */
646 for_each_possible_cpu(cpu) {
647 int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
648 &nmsg);
649 if (severity > global_worst) {
650 msg = nmsg;
651 global_worst = severity;
652 m = &per_cpu(mces_seen, cpu);
653 }
654 }
655
656 /*
657 * Cannot recover? Panic here then.
658 * This dumps all the mces in the log buffer and stops the
659 * other CPUs.
660 */
661 if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
Andi Kleenac960372009-05-27 21:56:58 +0200662 mce_panic("Fatal Machine check", m, msg);
Andi Kleen3c079792009-05-27 21:56:55 +0200663
664 /*
665 * For UC somewhere we let the CPU who detects it handle it.
666 * Also must let continue the others, otherwise the handling
667 * CPU could deadlock on a lock.
668 */
669
670 /*
671 * No machine check event found. Must be some external
672 * source or one CPU is hung. Panic.
673 */
674 if (!m && tolerant < 3)
675 mce_panic("Machine check from unknown source", NULL, NULL);
676
677 /*
678 * Now clear all the mces_seen so that they don't reappear on
679 * the next mce.
680 */
681 for_each_possible_cpu(cpu)
682 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
683}
684
685static atomic_t global_nwo;
686
687/*
688 * Start of Monarch synchronization. This waits until all CPUs have
689 * entered the exception handler and then determines if any of them
690 * saw a fatal event that requires panic. Then it executes them
691 * in the entry order.
692 * TBD double check parallel CPU hotunplug
693 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900694static int mce_start(int *no_way_out)
Andi Kleen3c079792009-05-27 21:56:55 +0200695{
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900696 int order;
Andi Kleen3c079792009-05-27 21:56:55 +0200697 int cpus = num_online_cpus();
698 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
699
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900700 if (!timeout)
701 return -1;
Andi Kleen3c079792009-05-27 21:56:55 +0200702
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900703 atomic_add(*no_way_out, &global_nwo);
Huang Ying184e1fd2009-06-15 15:37:07 +0800704 /*
705 * global_nwo should be updated before mce_callin
706 */
707 smp_wmb();
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900708 order = atomic_add_return(1, &mce_callin);
Andi Kleen3c079792009-05-27 21:56:55 +0200709
710 /*
711 * Wait for everyone.
712 */
713 while (atomic_read(&mce_callin) != cpus) {
714 if (mce_timed_out(&timeout)) {
715 atomic_set(&global_nwo, 0);
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900716 return -1;
Andi Kleen3c079792009-05-27 21:56:55 +0200717 }
718 ndelay(SPINUNIT);
719 }
720
721 /*
Huang Ying184e1fd2009-06-15 15:37:07 +0800722 * mce_callin should be read before global_nwo
723 */
724 smp_rmb();
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900725
726 if (order == 1) {
727 /*
728 * Monarch: Starts executing now, the others wait.
729 */
730 atomic_set(&mce_executing, 1);
731 } else {
732 /*
733 * Subject: Now start the scanning loop one by one in
734 * the original callin order.
735 * This way when there are any shared banks it will be
736 * only seen by one CPU before cleared, avoiding duplicates.
737 */
738 while (atomic_read(&mce_executing) < order) {
739 if (mce_timed_out(&timeout)) {
740 atomic_set(&global_nwo, 0);
741 return -1;
742 }
743 ndelay(SPINUNIT);
744 }
745 }
746
Huang Ying184e1fd2009-06-15 15:37:07 +0800747 /*
Andi Kleen3c079792009-05-27 21:56:55 +0200748 * Cache the global no_way_out state.
749 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900750 *no_way_out = atomic_read(&global_nwo);
Andi Kleen3c079792009-05-27 21:56:55 +0200751
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900752 return order;
Andi Kleen3c079792009-05-27 21:56:55 +0200753}
754
755/*
756 * Synchronize between CPUs after main scanning loop.
757 * This invokes the bulk of the Monarch processing.
758 */
759static int mce_end(int order)
760{
761 int ret = -1;
762 u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
763
764 if (!timeout)
765 goto reset;
766 if (order < 0)
767 goto reset;
768
769 /*
770 * Allow others to run.
771 */
772 atomic_inc(&mce_executing);
773
774 if (order == 1) {
775 /* CHECKME: Can this race with a parallel hotplug? */
776 int cpus = num_online_cpus();
777
778 /*
779 * Monarch: Wait for everyone to go through their scanning
780 * loops.
781 */
782 while (atomic_read(&mce_executing) <= cpus) {
783 if (mce_timed_out(&timeout))
784 goto reset;
785 ndelay(SPINUNIT);
786 }
787
788 mce_reign();
789 barrier();
790 ret = 0;
791 } else {
792 /*
793 * Subject: Wait for Monarch to finish.
794 */
795 while (atomic_read(&mce_executing) != 0) {
796 if (mce_timed_out(&timeout))
797 goto reset;
798 ndelay(SPINUNIT);
799 }
800
801 /*
802 * Don't reset anything. That's done by the Monarch.
803 */
804 return 0;
805 }
806
807 /*
808 * Reset all global state.
809 */
810reset:
811 atomic_set(&global_nwo, 0);
812 atomic_set(&mce_callin, 0);
813 barrier();
814
815 /*
816 * Let others run again.
817 */
818 atomic_set(&mce_executing, 0);
819 return ret;
820}
821
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200822/*
823 * Check if the address reported by the CPU is in a format we can parse.
824 * It would be possible to add code for most other cases, but all would
825 * be somewhat complicated (e.g. segment offset would require an instruction
826 * parser). So only support physical addresses upto page granuality for now.
827 */
828static int mce_usable_address(struct mce *m)
829{
830 if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
831 return 0;
832 if ((m->misc & 0x3f) > PAGE_SHIFT)
833 return 0;
834 if (((m->misc >> 6) & 7) != MCM_ADDR_PHYS)
835 return 0;
836 return 1;
837}
838
Andi Kleen3c079792009-05-27 21:56:55 +0200839static void mce_clear_state(unsigned long *toclear)
840{
841 int i;
842
843 for (i = 0; i < banks; i++) {
844 if (test_bit(i, toclear))
845 mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
846 }
847}
848
849/*
Andi Kleenb79109c2009-02-12 13:43:23 +0100850 * The actual machine check handler. This only handles real
851 * exceptions when something got corrupted coming in through int 18.
852 *
853 * This is executed in NMI context not subject to normal locking rules. This
854 * implies that most kernel services cannot be safely used. Don't even
855 * think about putting a printk in there!
Andi Kleen3c079792009-05-27 21:56:55 +0200856 *
857 * On Intel systems this is entered on all CPUs in parallel through
858 * MCE broadcast. However some CPUs might be broken beyond repair,
859 * so be always careful when synchronizing with others.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
Ingo Molnare9eee032009-04-08 12:31:17 +0200861void do_machine_check(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Andi Kleen3c079792009-05-27 21:56:55 +0200863 struct mce m, *final;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 int i;
Andi Kleen3c079792009-05-27 21:56:55 +0200865 int worst = 0;
866 int severity;
867 /*
868 * Establish sequential order between the CPUs entering the machine
869 * check handler.
870 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900871 int order;
Tim Hockinbd784322007-07-21 17:10:37 +0200872 /*
873 * If no_way_out gets set, there is no safe way to recover from this
874 * MCE. If tolerant is cranked up, we'll try anyway.
875 */
876 int no_way_out = 0;
877 /*
878 * If kill_it gets set, there might be a way to recover from this
879 * error.
880 */
881 int kill_it = 0;
Andi Kleenb79109c2009-02-12 13:43:23 +0100882 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200883 char *msg = "Unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Andi Kleen553f2652006-04-07 19:49:57 +0200885 atomic_inc(&mce_entry);
886
Andi Kleen01ca79f2009-05-27 21:56:52 +0200887 __get_cpu_var(mce_exception_count)++;
888
Andi Kleenb79109c2009-02-12 13:43:23 +0100889 if (notify_die(DIE_NMI, "machine check", regs, error_code,
Jan Beulich22f59912008-01-30 13:31:23 +0100890 18, SIGKILL) == NOTIFY_STOP)
Andi Kleen32561692009-05-27 21:56:53 +0200891 goto out;
Andi Kleenb79109c2009-02-12 13:43:23 +0100892 if (!banks)
Andi Kleen32561692009-05-27 21:56:53 +0200893 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100895 mce_setup(&m);
896
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200897 m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200898 no_way_out = mce_no_way_out(&m, &msg);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200899
Andi Kleen3c079792009-05-27 21:56:55 +0200900 final = &__get_cpu_var(mces_seen);
901 *final = m;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 barrier();
904
Andi Kleen3c079792009-05-27 21:56:55 +0200905 /*
Andi Kleened7290d2009-05-27 21:56:57 +0200906 * When no restart IP must always kill or panic.
907 */
908 if (!(m.mcgstatus & MCG_STATUS_RIPV))
909 kill_it = 1;
910
911 /*
Andi Kleen3c079792009-05-27 21:56:55 +0200912 * Go through all the banks in exclusion of the other CPUs.
913 * This way we don't report duplicated events on shared banks
914 * because the first one to see it will clear it.
915 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900916 order = mce_start(&no_way_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 for (i = 0; i < banks; i++) {
Andi Kleenb79109c2009-02-12 13:43:23 +0100918 __clear_bit(i, toclear);
Andi Kleen0d7482e32009-02-17 23:07:13 +0100919 if (!bank[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 continue;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200921
922 m.misc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 m.addr = 0;
924 m.bank = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200926 m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if ((m.status & MCI_STATUS_VAL) == 0)
928 continue;
929
Andi Kleenb79109c2009-02-12 13:43:23 +0100930 /*
Andi Kleened7290d2009-05-27 21:56:57 +0200931 * Non uncorrected or non signaled errors are handled by
932 * machine_check_poll. Leave them alone, unless this panics.
Andi Kleenb79109c2009-02-12 13:43:23 +0100933 */
Andi Kleened7290d2009-05-27 21:56:57 +0200934 if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
935 !no_way_out)
Andi Kleenb79109c2009-02-12 13:43:23 +0100936 continue;
937
938 /*
939 * Set taint even when machine check was not enabled.
940 */
941 add_taint(TAINT_MACHINE_CHECK);
942
Andi Kleened7290d2009-05-27 21:56:57 +0200943 severity = mce_severity(&m, tolerant, NULL);
Andi Kleenb79109c2009-02-12 13:43:23 +0100944
Andi Kleened7290d2009-05-27 21:56:57 +0200945 /*
946 * When machine check was for corrected handler don't touch,
947 * unless we're panicing.
948 */
949 if (severity == MCE_KEEP_SEVERITY && !no_way_out)
950 continue;
951 __set_bit(i, toclear);
952 if (severity == MCE_NO_SEVERITY) {
Andi Kleenb79109c2009-02-12 13:43:23 +0100953 /*
954 * Machine check event was not enabled. Clear, but
955 * ignore.
956 */
957 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
Andi Kleened7290d2009-05-27 21:56:57 +0200960 /*
961 * Kill on action required.
962 */
963 if (severity == MCE_AR_SEVERITY)
964 kill_it = 1;
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (m.status & MCI_STATUS_MISCV)
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200967 m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (m.status & MCI_STATUS_ADDRV)
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200969 m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200971 /*
972 * Action optional error. Queue address for later processing.
973 * When the ring overflows we just ignore the AO error.
974 * RED-PEN add some logging mechanism when
975 * usable_address or mce_add_ring fails.
976 * RED-PEN don't ignore overflow for tolerant == 0
977 */
978 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
979 mce_ring_add(m.addr >> PAGE_SHIFT);
980
Andi Kleen94ad8472005-04-16 15:25:09 -0700981 mce_get_rip(&m, regs);
Andi Kleenb79109c2009-02-12 13:43:23 +0100982 mce_log(&m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Andi Kleen3c079792009-05-27 21:56:55 +0200984 if (severity > worst) {
985 *final = m;
986 worst = severity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
Andi Kleen3c079792009-05-27 21:56:55 +0200990 if (!no_way_out)
991 mce_clear_state(toclear);
992
Ingo Molnare9eee032009-04-08 12:31:17 +0200993 /*
Andi Kleen3c079792009-05-27 21:56:55 +0200994 * Do most of the synchronization with other CPUs.
995 * When there's any problem use only local no_way_out state.
Ingo Molnare9eee032009-04-08 12:31:17 +0200996 */
Andi Kleen3c079792009-05-27 21:56:55 +0200997 if (mce_end(order) < 0)
998 no_way_out = worst >= MCE_PANIC_SEVERITY;
Tim Hockinbd784322007-07-21 17:10:37 +0200999
1000 /*
1001 * If we have decided that we just CAN'T continue, and the user
Ingo Molnare9eee032009-04-08 12:31:17 +02001002 * has not set tolerant to an insane level, give up and die.
Andi Kleen3c079792009-05-27 21:56:55 +02001003 *
1004 * This is mainly used in the case when the system doesn't
1005 * support MCE broadcasting or it has been disabled.
Tim Hockinbd784322007-07-21 17:10:37 +02001006 */
1007 if (no_way_out && tolerant < 3)
Andi Kleenac960372009-05-27 21:56:58 +02001008 mce_panic("Fatal machine check on current CPU", final, msg);
Tim Hockinbd784322007-07-21 17:10:37 +02001009
1010 /*
1011 * If the error seems to be unrecoverable, something should be
1012 * done. Try to kill as little as possible. If we can kill just
1013 * one task, do that. If the user has set the tolerance very
1014 * high, don't try to do anything at all.
1015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Andi Kleened7290d2009-05-27 21:56:57 +02001017 if (kill_it && tolerant < 3)
1018 force_sig(SIGBUS, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Tim Hockine02e68d2007-07-21 17:10:36 +02001020 /* notify userspace ASAP */
1021 set_thread_flag(TIF_MCE_NOTIFY);
1022
Andi Kleen3c079792009-05-27 21:56:55 +02001023 if (worst > 0)
1024 mce_report_event(regs);
Andi Kleen5f8c1a52009-04-29 19:29:12 +02001025 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
Andi Kleen32561692009-05-27 21:56:53 +02001026out:
Andi Kleen553f2652006-04-07 19:49:57 +02001027 atomic_dec(&mce_entry);
Andi Kleen88921be2009-05-27 21:56:51 +02001028 sync_core();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
Andi Kleenea149b32009-04-29 19:31:00 +02001030EXPORT_SYMBOL_GPL(do_machine_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001032/* dummy to break dependency. actual code is in mm/memory-failure.c */
1033void __attribute__((weak)) memory_failure(unsigned long pfn, int vector)
1034{
1035 printk(KERN_ERR "Action optional memory failure at %lx ignored\n", pfn);
1036}
1037
1038/*
1039 * Called after mce notification in process context. This code
1040 * is allowed to sleep. Call the high level VM handler to process
1041 * any corrupted pages.
1042 * Assume that the work queue code only calls this one at a time
1043 * per CPU.
1044 * Note we don't disable preemption, so this code might run on the wrong
1045 * CPU. In this case the event is picked up by the scheduled work queue.
1046 * This is merely a fast path to expedite processing in some common
1047 * cases.
1048 */
1049void mce_notify_process(void)
1050{
1051 unsigned long pfn;
1052 mce_notify_irq();
1053 while (mce_ring_get(&pfn))
1054 memory_failure(pfn, MCE_VECTOR);
1055}
1056
1057static void mce_process_work(struct work_struct *dummy)
1058{
1059 mce_notify_process();
1060}
1061
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001062#ifdef CONFIG_X86_MCE_INTEL
1063/***
1064 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
Simon Arlott676b1852007-10-20 01:25:36 +02001065 * @cpu: The CPU on which the event occurred.
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001066 * @status: Event status information
1067 *
1068 * This function should be called by the thermal interrupt after the
1069 * event has been processed and the decision was made to log the event
1070 * further.
1071 *
1072 * The status parameter will be saved to the 'status' field of 'struct mce'
1073 * and historically has been the register value of the
1074 * MSR_IA32_THERMAL_STATUS (Intel) msr.
1075 */
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001076void mce_log_therm_throt_event(__u64 status)
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001077{
1078 struct mce m;
1079
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001080 mce_setup(&m);
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001081 m.bank = MCE_THERMAL_BANK;
1082 m.status = status;
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001083 mce_log(&m);
1084}
1085#endif /* CONFIG_X86_MCE_INTEL */
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/*
Tim Hockin8a336b02007-05-02 19:27:19 +02001088 * Periodic polling timer for "silent" machine check errors. If the
1089 * poller finds an MCE, poll 2x faster. When the poller finds no more
1090 * errors, poll 2x slower (up to check_interval seconds).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static int check_interval = 5 * 60; /* 5 minutes */
Ingo Molnare9eee032009-04-08 12:31:17 +02001093
Tejun Heo245b2e72009-06-24 15:13:48 +09001094static DEFINE_PER_CPU(int, mce_next_interval); /* in jiffies */
Andi Kleen52d168e2009-02-12 13:39:29 +01001095static DEFINE_PER_CPU(struct timer_list, mce_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Andi Kleen52d168e2009-02-12 13:39:29 +01001097static void mcheck_timer(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Andi Kleen52d168e2009-02-12 13:39:29 +01001099 struct timer_list *t = &per_cpu(mce_timer, data);
Andi Kleen6298c512009-04-09 12:28:22 +02001100 int *n;
Andi Kleen52d168e2009-02-12 13:39:29 +01001101
1102 WARN_ON(smp_processor_id() != data);
1103
Ingo Molnare9eee032009-04-08 12:31:17 +02001104 if (mce_available(&current_cpu_data)) {
Andi Kleenee031c32009-02-12 13:49:34 +01001105 machine_check_poll(MCP_TIMESTAMP,
1106 &__get_cpu_var(mce_poll_banks));
Ingo Molnare9eee032009-04-08 12:31:17 +02001107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /*
Tim Hockine02e68d2007-07-21 17:10:36 +02001110 * Alert userspace if needed. If we logged an MCE, reduce the
1111 * polling interval, otherwise increase the polling interval.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 */
Tejun Heo245b2e72009-06-24 15:13:48 +09001113 n = &__get_cpu_var(mce_next_interval);
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001114 if (mce_notify_irq())
Andi Kleen6298c512009-04-09 12:28:22 +02001115 *n = max(*n/2, HZ/100);
Hidetoshi Seto14a02532009-04-30 16:04:51 +09001116 else
Andi Kleen6298c512009-04-09 12:28:22 +02001117 *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
Tim Hockin8a336b02007-05-02 19:27:19 +02001118
Andi Kleen6298c512009-04-09 12:28:22 +02001119 t->expires = jiffies + *n;
Andi Kleen52d168e2009-02-12 13:39:29 +01001120 add_timer(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Andi Kleen9bd98402009-02-12 13:39:28 +01001123static void mce_do_trigger(struct work_struct *work)
1124{
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001125 call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
Andi Kleen9bd98402009-02-12 13:39:28 +01001126}
1127
1128static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1129
Tim Hockine02e68d2007-07-21 17:10:36 +02001130/*
Andi Kleen9bd98402009-02-12 13:39:28 +01001131 * Notify the user(s) about new machine check events.
1132 * Can be called from interrupt context, but not from machine check/NMI
1133 * context.
Tim Hockine02e68d2007-07-21 17:10:36 +02001134 */
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001135int mce_notify_irq(void)
Tim Hockine02e68d2007-07-21 17:10:36 +02001136{
Andi Kleen8457c842009-02-12 13:49:33 +01001137 /* Not more than two messages every minute */
1138 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1139
Tim Hockine02e68d2007-07-21 17:10:36 +02001140 clear_thread_flag(TIF_MCE_NOTIFY);
Ingo Molnare9eee032009-04-08 12:31:17 +02001141
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001142 if (test_and_clear_bit(0, &mce_need_notify)) {
Tim Hockine02e68d2007-07-21 17:10:36 +02001143 wake_up_interruptible(&mce_wait);
Andi Kleen9bd98402009-02-12 13:39:28 +01001144
1145 /*
1146 * There is no risk of missing notifications because
1147 * work_pending is always cleared before the function is
1148 * executed.
1149 */
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001150 if (mce_helper[0] && !work_pending(&mce_trigger_work))
Andi Kleen9bd98402009-02-12 13:39:28 +01001151 schedule_work(&mce_trigger_work);
Tim Hockine02e68d2007-07-21 17:10:36 +02001152
Andi Kleen8457c842009-02-12 13:49:33 +01001153 if (__ratelimit(&ratelimit))
Tim Hockine02e68d2007-07-21 17:10:36 +02001154 printk(KERN_INFO "Machine check events logged\n");
Tim Hockine02e68d2007-07-21 17:10:36 +02001155
1156 return 1;
1157 }
1158 return 0;
1159}
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001160EXPORT_SYMBOL_GPL(mce_notify_irq);
Tim Hockine02e68d2007-07-21 17:10:36 +02001161
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001162/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * Initialize Machine Checks for a CPU.
1164 */
Andi Kleen0d7482e32009-02-17 23:07:13 +01001165static int mce_cap_init(void)
1166{
Andi Kleen0d7482e32009-02-17 23:07:13 +01001167 unsigned b;
Ingo Molnare9eee032009-04-08 12:31:17 +02001168 u64 cap;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001169
1170 rdmsrl(MSR_IA32_MCG_CAP, cap);
Thomas Gleixner01c66802009-04-08 12:31:24 +02001171
1172 b = cap & MCG_BANKCNT_MASK;
Ingo Molnarb6592942009-04-08 12:31:27 +02001173 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
1174
Andi Kleen0d7482e32009-02-17 23:07:13 +01001175 if (b > MAX_NR_BANKS) {
1176 printk(KERN_WARNING
1177 "MCE: Using only %u machine check banks out of %u\n",
1178 MAX_NR_BANKS, b);
1179 b = MAX_NR_BANKS;
1180 }
1181
1182 /* Don't support asymmetric configurations today */
1183 WARN_ON(banks != 0 && b != banks);
1184 banks = b;
1185 if (!bank) {
1186 bank = kmalloc(banks * sizeof(u64), GFP_KERNEL);
1187 if (!bank)
1188 return -ENOMEM;
1189 memset(bank, 0xff, banks * sizeof(u64));
1190 }
1191
1192 /* Use accurate RIP reporting if available. */
Thomas Gleixner01c66802009-04-08 12:31:24 +02001193 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001194 rip_msr = MSR_IA32_MCG_EIP;
1195
Andi Kleened7290d2009-05-27 21:56:57 +02001196 if (cap & MCG_SER_P)
1197 mce_ser = 1;
1198
Andi Kleen0d7482e32009-02-17 23:07:13 +01001199 return 0;
1200}
1201
Thomas Gleixner8be91102009-05-27 21:56:53 +02001202static void mce_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Ingo Molnare9eee032009-04-08 12:31:17 +02001204 mce_banks_t all_banks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 u64 cap;
1206 int i;
1207
Andi Kleenb79109c2009-02-12 13:43:23 +01001208 /*
1209 * Log the machine checks left over from the previous reset.
1210 */
Andi Kleenee031c32009-02-12 13:49:34 +01001211 bitmap_fill(all_banks, MAX_NR_BANKS);
Andi Kleen5679af42009-04-07 17:06:55 +02001212 machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 set_in_cr4(X86_CR4_MCE);
1215
Andi Kleen0d7482e32009-02-17 23:07:13 +01001216 rdmsrl(MSR_IA32_MCG_CAP, cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (cap & MCG_CTL_P)
1218 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1219
1220 for (i = 0; i < banks; i++) {
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001221 if (skip_bank_init(i))
1222 continue;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001223 wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
1228/* Add per CPU specific workarounds here */
H. Peter Anvinec5b3d32009-02-23 14:01:04 -08001229static void mce_cpu_quirks(struct cpuinfo_x86 *c)
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* This should be disabled by the BIOS, but isn't always */
Jan Beulich911f6a72008-04-22 16:22:21 +01001232 if (c->x86_vendor == X86_VENDOR_AMD) {
Ingo Molnare9eee032009-04-08 12:31:17 +02001233 if (c->x86 == 15 && banks > 4) {
1234 /*
1235 * disable GART TBL walk error reporting, which
1236 * trips off incorrectly with the IOMMU & 3ware
1237 * & Cerberus:
1238 */
Andi Kleen0d7482e32009-02-17 23:07:13 +01001239 clear_bit(10, (unsigned long *)&bank[4]);
Ingo Molnare9eee032009-04-08 12:31:17 +02001240 }
1241 if (c->x86 <= 17 && mce_bootlog < 0) {
1242 /*
1243 * Lots of broken BIOS around that don't clear them
1244 * by default and leave crap in there. Don't log:
1245 */
Jan Beulich911f6a72008-04-22 16:22:21 +01001246 mce_bootlog = 0;
Ingo Molnare9eee032009-04-08 12:31:17 +02001247 }
Andi Kleen2e6f6942009-04-27 18:42:48 +02001248 /*
1249 * Various K7s with broken bank 0 around. Always disable
1250 * by default.
1251 */
Andi Kleen203abd62009-06-15 14:52:01 +02001252 if (c->x86 == 6 && banks > 0)
Andi Kleen2e6f6942009-04-27 18:42:48 +02001253 bank[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Andi Kleene5835382005-11-05 17:25:54 +01001255
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001256 if (c->x86_vendor == X86_VENDOR_INTEL) {
1257 /*
1258 * SDM documents that on family 6 bank 0 should not be written
1259 * because it aliases to another special BIOS controlled
1260 * register.
1261 * But it's not aliased anymore on model 0x1a+
1262 * Don't ignore bank 0 completely because there could be a
1263 * valid event later, merely don't write CTL0.
1264 */
1265
1266 if (c->x86 == 6 && c->x86_model < 0x1A)
1267 __set_bit(0, &dont_init_banks);
Andi Kleen3c079792009-05-27 21:56:55 +02001268
1269 /*
1270 * All newer Intel systems support MCE broadcasting. Enable
1271 * synchronization with a one second timeout.
1272 */
1273 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1274 monarch_timeout < 0)
1275 monarch_timeout = USEC_PER_SEC;
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001276 }
Andi Kleen3c079792009-05-27 21:56:55 +02001277 if (monarch_timeout < 0)
1278 monarch_timeout = 0;
Andi Kleen29b0f592009-05-27 21:56:56 +02001279 if (mce_bootlog != 0)
1280 mce_panic_timeout = 30;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Andi Kleen4efc0672009-04-28 19:07:31 +02001283static void __cpuinit mce_ancient_init(struct cpuinfo_x86 *c)
1284{
1285 if (c->x86 != 5)
1286 return;
1287 switch (c->x86_vendor) {
1288 case X86_VENDOR_INTEL:
Hidetoshi Setoc6978362009-06-15 17:22:49 +09001289 intel_p5_mcheck_init(c);
Andi Kleen4efc0672009-04-28 19:07:31 +02001290 break;
1291 case X86_VENDOR_CENTAUR:
1292 winchip_mcheck_init(c);
1293 break;
1294 }
1295}
1296
H. Peter Anvincc3ca222009-02-20 23:35:51 -08001297static void mce_cpu_features(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 switch (c->x86_vendor) {
1300 case X86_VENDOR_INTEL:
1301 mce_intel_feature_init(c);
1302 break;
Jacob Shin89b831e2005-11-05 17:25:53 +01001303 case X86_VENDOR_AMD:
1304 mce_amd_feature_init(c);
1305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 default:
1307 break;
1308 }
1309}
1310
Andi Kleen52d168e2009-02-12 13:39:29 +01001311static void mce_init_timer(void)
1312{
1313 struct timer_list *t = &__get_cpu_var(mce_timer);
Tejun Heo245b2e72009-06-24 15:13:48 +09001314 int *n = &__get_cpu_var(mce_next_interval);
Andi Kleen52d168e2009-02-12 13:39:29 +01001315
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001316 if (mce_ignore_ce)
1317 return;
1318
Andi Kleen6298c512009-04-09 12:28:22 +02001319 *n = check_interval * HZ;
1320 if (!*n)
Andi Kleen52d168e2009-02-12 13:39:29 +01001321 return;
1322 setup_timer(t, mcheck_timer, smp_processor_id());
Andi Kleen6298c512009-04-09 12:28:22 +02001323 t->expires = round_jiffies(jiffies + *n);
Andi Kleen52d168e2009-02-12 13:39:29 +01001324 add_timer(t);
1325}
1326
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001327/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 * Called for each booted CPU to set up machine checks.
Ingo Molnare9eee032009-04-08 12:31:17 +02001329 * Must be called with preempt off:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
Ashok Raje6982c62005-06-25 14:54:58 -07001331void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Andi Kleen4efc0672009-04-28 19:07:31 +02001333 if (mce_disabled)
1334 return;
1335
1336 mce_ancient_init(c);
1337
Andi Kleen5b4408f2009-02-12 13:39:30 +01001338 if (!mce_available(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return;
1340
Andi Kleen0d7482e32009-02-17 23:07:13 +01001341 if (mce_cap_init() < 0) {
Andi Kleen04b2b1a2009-04-28 22:50:19 +02001342 mce_disabled = 1;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001343 return;
1344 }
1345 mce_cpu_quirks(c);
1346
Andi Kleen5d727922009-04-27 19:25:48 +02001347 machine_check_vector = do_machine_check;
1348
Thomas Gleixner8be91102009-05-27 21:56:53 +02001349 mce_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 mce_cpu_features(c);
Andi Kleen52d168e2009-02-12 13:39:29 +01001351 mce_init_timer();
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001352 INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355/*
1356 * Character device to read and clear the MCE log.
1357 */
1358
Tim Hockinf528e7b2007-07-21 17:10:35 +02001359static DEFINE_SPINLOCK(mce_state_lock);
Ingo Molnare9eee032009-04-08 12:31:17 +02001360static int open_count; /* #times opened */
1361static int open_exclu; /* already open exclusive? */
Tim Hockinf528e7b2007-07-21 17:10:35 +02001362
1363static int mce_open(struct inode *inode, struct file *file)
1364{
1365 spin_lock(&mce_state_lock);
1366
1367 if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
1368 spin_unlock(&mce_state_lock);
Ingo Molnare9eee032009-04-08 12:31:17 +02001369
Tim Hockinf528e7b2007-07-21 17:10:35 +02001370 return -EBUSY;
1371 }
1372
1373 if (file->f_flags & O_EXCL)
1374 open_exclu = 1;
1375 open_count++;
1376
1377 spin_unlock(&mce_state_lock);
1378
Tim Hockinbd784322007-07-21 17:10:37 +02001379 return nonseekable_open(inode, file);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001380}
1381
1382static int mce_release(struct inode *inode, struct file *file)
1383{
1384 spin_lock(&mce_state_lock);
1385
1386 open_count--;
1387 open_exclu = 0;
1388
1389 spin_unlock(&mce_state_lock);
1390
1391 return 0;
1392}
1393
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001394static void collect_tscs(void *data)
1395{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 unsigned long *cpu_tsc = (unsigned long *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001398 rdtscll(cpu_tsc[smp_processor_id()]);
1399}
1400
Ingo Molnare9eee032009-04-08 12:31:17 +02001401static DEFINE_MUTEX(mce_read_mutex);
1402
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001403static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
1404 loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 char __user *buf = ubuf;
Ingo Molnare9eee032009-04-08 12:31:17 +02001407 unsigned long *cpu_tsc;
1408 unsigned prev, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int i, err;
1410
Mike Travis6bca67f2008-07-18 18:11:27 -07001411 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001412 if (!cpu_tsc)
1413 return -ENOMEM;
1414
Daniel Walker8c8b8852008-01-30 13:31:17 +01001415 mutex_lock(&mce_read_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 next = rcu_dereference(mcelog.next);
1417
1418 /* Only supports full reads right now */
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001419 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) {
Daniel Walker8c8b8852008-01-30 13:31:17 +01001420 mutex_unlock(&mce_read_mutex);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001421 kfree(cpu_tsc);
Ingo Molnare9eee032009-04-08 12:31:17 +02001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EINVAL;
1424 }
1425
1426 err = 0;
Huang Yingef41df4342009-02-12 13:39:34 +01001427 prev = 0;
1428 do {
1429 for (i = prev; i < next; i++) {
1430 unsigned long start = jiffies;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001431
Huang Yingef41df4342009-02-12 13:39:34 +01001432 while (!mcelog.entry[i].finished) {
1433 if (time_after_eq(jiffies, start + 2)) {
1434 memset(mcelog.entry + i, 0,
1435 sizeof(struct mce));
1436 goto timeout;
1437 }
1438 cpu_relax();
Andi Kleen673242c2005-09-12 18:49:24 +02001439 }
Huang Yingef41df4342009-02-12 13:39:34 +01001440 smp_rmb();
1441 err |= copy_to_user(buf, mcelog.entry + i,
1442 sizeof(struct mce));
1443 buf += sizeof(struct mce);
1444timeout:
1445 ;
Andi Kleen673242c2005-09-12 18:49:24 +02001446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Huang Yingef41df4342009-02-12 13:39:34 +01001448 memset(mcelog.entry + prev, 0,
1449 (next - prev) * sizeof(struct mce));
1450 prev = next;
1451 next = cmpxchg(&mcelog.next, prev, 0);
1452 } while (next != prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Paul E. McKenneyb2b18662005-06-25 14:55:38 -07001454 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001456 /*
1457 * Collect entries that were still getting written before the
1458 * synchronize.
1459 */
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001460 on_each_cpu(collect_tscs, cpu_tsc, 1);
Ingo Molnare9eee032009-04-08 12:31:17 +02001461
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001462 for (i = next; i < MCE_LOG_LEN; i++) {
1463 if (mcelog.entry[i].finished &&
1464 mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
1465 err |= copy_to_user(buf, mcelog.entry+i,
1466 sizeof(struct mce));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 smp_rmb();
1468 buf += sizeof(struct mce);
1469 memset(&mcelog.entry[i], 0, sizeof(struct mce));
1470 }
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001471 }
Daniel Walker8c8b8852008-01-30 13:31:17 +01001472 mutex_unlock(&mce_read_mutex);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001473 kfree(cpu_tsc);
Ingo Molnare9eee032009-04-08 12:31:17 +02001474
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001475 return err ? -EFAULT : buf - ubuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Tim Hockine02e68d2007-07-21 17:10:36 +02001478static unsigned int mce_poll(struct file *file, poll_table *wait)
1479{
1480 poll_wait(file, &mce_wait, wait);
1481 if (rcu_dereference(mcelog.next))
1482 return POLLIN | POLLRDNORM;
1483 return 0;
1484}
1485
Nikanth Karthikesanc68461b2008-01-30 13:32:59 +01001486static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 int __user *p = (int __user *)arg;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (!capable(CAP_SYS_ADMIN))
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001491 return -EPERM;
Ingo Molnare9eee032009-04-08 12:31:17 +02001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 switch (cmd) {
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001494 case MCE_GET_RECORD_LEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return put_user(sizeof(struct mce), p);
1496 case MCE_GET_LOG_LEN:
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001497 return put_user(MCE_LOG_LEN, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 case MCE_GETCLEAR_FLAGS: {
1499 unsigned flags;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001500
1501 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 flags = mcelog.flags;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001503 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
Ingo Molnare9eee032009-04-08 12:31:17 +02001504
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001505 return put_user(flags, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507 default:
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001508 return -ENOTTY;
1509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
H. Peter Anvina1ff41b2009-05-25 22:16:14 -07001512/* Modified in mce-inject.c, so not static or const */
Andi Kleenea149b32009-04-29 19:31:00 +02001513struct file_operations mce_chrdev_ops = {
Ingo Molnare9eee032009-04-08 12:31:17 +02001514 .open = mce_open,
1515 .release = mce_release,
1516 .read = mce_read,
1517 .poll = mce_poll,
1518 .unlocked_ioctl = mce_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519};
Andi Kleenea149b32009-04-29 19:31:00 +02001520EXPORT_SYMBOL_GPL(mce_chrdev_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522static struct miscdevice mce_log_device = {
1523 MISC_MCELOG_MINOR,
1524 "mcelog",
1525 &mce_chrdev_ops,
1526};
1527
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001528/*
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001529 * mce=off Disables machine check
1530 * mce=no_cmci Disables CMCI
1531 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1532 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
Andi Kleen3c079792009-05-27 21:56:55 +02001533 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1534 * monarchtimeout is how long to wait for other CPUs on machine
1535 * check, or 0 to not wait
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001536 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1537 * mce=nobootlog Don't log MCEs from before booting.
1538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539static int __init mcheck_enable(char *str)
1540{
Andi Kleen4efc0672009-04-28 19:07:31 +02001541 if (*str == 0)
1542 enable_p5_mce();
1543 if (*str == '=')
1544 str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (!strcmp(str, "off"))
Andi Kleen04b2b1a2009-04-28 22:50:19 +02001546 mce_disabled = 1;
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001547 else if (!strcmp(str, "no_cmci"))
1548 mce_cmci_disabled = 1;
1549 else if (!strcmp(str, "dont_log_ce"))
1550 mce_dont_log_ce = 1;
1551 else if (!strcmp(str, "ignore_ce"))
1552 mce_ignore_ce = 1;
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001553 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
1554 mce_bootlog = (str[0] == 'b');
Andi Kleen3c079792009-05-27 21:56:55 +02001555 else if (isdigit(str[0])) {
Andi Kleen8c566ef2005-09-12 18:49:24 +02001556 get_option(&str, &tolerant);
Andi Kleen3c079792009-05-27 21:56:55 +02001557 if (*str == ',') {
1558 ++str;
1559 get_option(&str, &monarch_timeout);
1560 }
1561 } else {
Andi Kleen4efc0672009-04-28 19:07:31 +02001562 printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001563 str);
1564 return 0;
1565 }
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001566 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
Andi Kleen4efc0672009-04-28 19:07:31 +02001568__setup("mce", mcheck_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001570/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 * Sysfs support
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001572 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Andi Kleen973a2dd2009-02-12 13:39:32 +01001574/*
1575 * Disable machine checks on suspend and shutdown. We can't really handle
1576 * them later.
1577 */
1578static int mce_disable(void)
1579{
1580 int i;
1581
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001582 for (i = 0; i < banks; i++) {
1583 if (!skip_bank_init(i))
1584 wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
1585 }
Andi Kleen973a2dd2009-02-12 13:39:32 +01001586 return 0;
1587}
1588
1589static int mce_suspend(struct sys_device *dev, pm_message_t state)
1590{
1591 return mce_disable();
1592}
1593
1594static int mce_shutdown(struct sys_device *dev)
1595{
1596 return mce_disable();
1597}
1598
Ingo Molnare9eee032009-04-08 12:31:17 +02001599/*
1600 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
1601 * Only one CPU is active at this time, the others get re-added later using
1602 * CPU hotplug:
1603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604static int mce_resume(struct sys_device *dev)
1605{
Thomas Gleixner8be91102009-05-27 21:56:53 +02001606 mce_init();
Andi Kleen6ec68bf2009-02-12 13:39:26 +01001607 mce_cpu_features(&current_cpu_data);
Ingo Molnare9eee032009-04-08 12:31:17 +02001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return 0;
1610}
1611
Andi Kleen52d168e2009-02-12 13:39:29 +01001612static void mce_cpu_restart(void *data)
1613{
1614 del_timer_sync(&__get_cpu_var(mce_timer));
Hidetoshi Seto33edbf02009-06-15 17:18:45 +09001615 if (!mce_available(&current_cpu_data))
1616 return;
1617 mce_init();
Andi Kleen52d168e2009-02-12 13:39:29 +01001618 mce_init_timer();
1619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621/* Reinit MCEs after user configuration changes */
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001622static void mce_restart(void)
1623{
Andi Kleen52d168e2009-02-12 13:39:29 +01001624 on_each_cpu(mce_cpu_restart, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001627/* Toggle features for corrected errors */
1628static void mce_disable_ce(void *all)
1629{
1630 if (!mce_available(&current_cpu_data))
1631 return;
1632 if (all)
1633 del_timer_sync(&__get_cpu_var(mce_timer));
1634 cmci_clear();
1635}
1636
1637static void mce_enable_ce(void *all)
1638{
1639 if (!mce_available(&current_cpu_data))
1640 return;
1641 cmci_reenable();
1642 cmci_recheck();
1643 if (all)
1644 mce_init_timer();
1645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647static struct sysdev_class mce_sysclass = {
Ingo Molnare9eee032009-04-08 12:31:17 +02001648 .suspend = mce_suspend,
1649 .shutdown = mce_shutdown,
1650 .resume = mce_resume,
1651 .name = "machinecheck",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652};
1653
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001654DEFINE_PER_CPU(struct sys_device, mce_dev);
Ingo Molnare9eee032009-04-08 12:31:17 +02001655
1656__cpuinitdata
1657void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Andi Kleen0d7482e32009-02-17 23:07:13 +01001659static struct sysdev_attribute *bank_attrs;
1660
1661static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
1662 char *buf)
1663{
1664 u64 b = bank[attr - bank_attrs];
Ingo Molnare9eee032009-04-08 12:31:17 +02001665
H. Peter Anvinf6d18262009-02-19 15:44:58 -08001666 return sprintf(buf, "%llx\n", b);
Andi Kleen0d7482e32009-02-17 23:07:13 +01001667}
1668
1669static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09001670 const char *buf, size_t size)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001671{
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09001672 u64 new;
Ingo Molnare9eee032009-04-08 12:31:17 +02001673
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09001674 if (strict_strtoull(buf, 0, &new) < 0)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001675 return -EINVAL;
Ingo Molnare9eee032009-04-08 12:31:17 +02001676
Andi Kleen0d7482e32009-02-17 23:07:13 +01001677 bank[attr - bank_attrs] = new;
1678 mce_restart();
Ingo Molnare9eee032009-04-08 12:31:17 +02001679
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09001680 return size;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001681}
Andi Kleena98f0dd2007-02-13 13:26:23 +01001682
Ingo Molnare9eee032009-04-08 12:31:17 +02001683static ssize_t
1684show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
Andi Kleena98f0dd2007-02-13 13:26:23 +01001685{
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001686 strcpy(buf, mce_helper);
Andi Kleena98f0dd2007-02-13 13:26:23 +01001687 strcat(buf, "\n");
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001688 return strlen(mce_helper) + 1;
Andi Kleena98f0dd2007-02-13 13:26:23 +01001689}
1690
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001691static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
Ingo Molnare9eee032009-04-08 12:31:17 +02001692 const char *buf, size_t siz)
Andi Kleena98f0dd2007-02-13 13:26:23 +01001693{
1694 char *p;
1695 int len;
Ingo Molnare9eee032009-04-08 12:31:17 +02001696
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001697 strncpy(mce_helper, buf, sizeof(mce_helper));
1698 mce_helper[sizeof(mce_helper)-1] = 0;
1699 len = strlen(mce_helper);
1700 p = strchr(mce_helper, '\n');
Ingo Molnare9eee032009-04-08 12:31:17 +02001701
1702 if (*p)
1703 *p = 0;
1704
Andi Kleena98f0dd2007-02-13 13:26:23 +01001705 return len;
1706}
1707
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001708static ssize_t set_ignore_ce(struct sys_device *s,
1709 struct sysdev_attribute *attr,
1710 const char *buf, size_t size)
1711{
1712 u64 new;
1713
1714 if (strict_strtoull(buf, 0, &new) < 0)
1715 return -EINVAL;
1716
1717 if (mce_ignore_ce ^ !!new) {
1718 if (new) {
1719 /* disable ce features */
1720 on_each_cpu(mce_disable_ce, (void *)1, 1);
1721 mce_ignore_ce = 1;
1722 } else {
1723 /* enable ce features */
1724 mce_ignore_ce = 0;
1725 on_each_cpu(mce_enable_ce, (void *)1, 1);
1726 }
1727 }
1728 return size;
1729}
1730
1731static ssize_t set_cmci_disabled(struct sys_device *s,
1732 struct sysdev_attribute *attr,
1733 const char *buf, size_t size)
1734{
1735 u64 new;
1736
1737 if (strict_strtoull(buf, 0, &new) < 0)
1738 return -EINVAL;
1739
1740 if (mce_cmci_disabled ^ !!new) {
1741 if (new) {
1742 /* disable cmci */
1743 on_each_cpu(mce_disable_ce, NULL, 1);
1744 mce_cmci_disabled = 1;
1745 } else {
1746 /* enable cmci */
1747 mce_cmci_disabled = 0;
1748 on_each_cpu(mce_enable_ce, NULL, 1);
1749 }
1750 }
1751 return size;
1752}
1753
Andi Kleenb56f6422009-05-27 21:56:52 +02001754static ssize_t store_int_with_restart(struct sys_device *s,
1755 struct sysdev_attribute *attr,
1756 const char *buf, size_t size)
1757{
1758 ssize_t ret = sysdev_store_int(s, attr, buf, size);
1759 mce_restart();
1760 return ret;
1761}
1762
Andi Kleena98f0dd2007-02-13 13:26:23 +01001763static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
Andi Kleend95d62c2008-07-01 18:48:43 +02001764static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
Andi Kleen3c079792009-05-27 21:56:55 +02001765static SYSDEV_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001766static SYSDEV_INT_ATTR(dont_log_ce, 0644, mce_dont_log_ce);
Ingo Molnare9eee032009-04-08 12:31:17 +02001767
Andi Kleenb56f6422009-05-27 21:56:52 +02001768static struct sysdev_ext_attribute attr_check_interval = {
1769 _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int,
1770 store_int_with_restart),
1771 &check_interval
1772};
Ingo Molnare9eee032009-04-08 12:31:17 +02001773
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001774static struct sysdev_ext_attribute attr_ignore_ce = {
1775 _SYSDEV_ATTR(ignore_ce, 0644, sysdev_show_int, set_ignore_ce),
1776 &mce_ignore_ce
1777};
1778
1779static struct sysdev_ext_attribute attr_cmci_disabled = {
Yinghai Lu74b602c2009-06-17 14:43:32 -07001780 _SYSDEV_ATTR(cmci_disabled, 0644, sysdev_show_int, set_cmci_disabled),
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001781 &mce_cmci_disabled
1782};
1783
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001784static struct sysdev_attribute *mce_attrs[] = {
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001785 &attr_tolerant.attr,
1786 &attr_check_interval.attr,
1787 &attr_trigger,
Andi Kleen3c079792009-05-27 21:56:55 +02001788 &attr_monarch_timeout.attr,
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09001789 &attr_dont_log_ce.attr,
1790 &attr_ignore_ce.attr,
1791 &attr_cmci_disabled.attr,
Andi Kleena98f0dd2007-02-13 13:26:23 +01001792 NULL
1793};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001795static cpumask_var_t mce_dev_initialized;
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08001796
Ingo Molnare9eee032009-04-08 12:31:17 +02001797/* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
Andi Kleen91c6d402005-07-28 21:15:39 -07001798static __cpuinit int mce_create_device(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 int err;
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09001801 int i, j;
Mike Travis92cb7612007-10-19 20:35:04 +02001802
Andreas Herrmann90367552007-11-07 02:12:58 +01001803 if (!mce_available(&boot_cpu_data))
Andi Kleen91c6d402005-07-28 21:15:39 -07001804 return -EIO;
1805
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001806 memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
1807 per_cpu(mce_dev, cpu).id = cpu;
1808 per_cpu(mce_dev, cpu).cls = &mce_sysclass;
Andi Kleen91c6d402005-07-28 21:15:39 -07001809
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001810 err = sysdev_register(&per_cpu(mce_dev, cpu));
Akinobu Mitad435d862007-10-18 03:05:15 -07001811 if (err)
1812 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07001813
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001814 for (i = 0; mce_attrs[i]; i++) {
1815 err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
Akinobu Mitad435d862007-10-18 03:05:15 -07001816 if (err)
1817 goto error;
Andi Kleen91c6d402005-07-28 21:15:39 -07001818 }
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09001819 for (j = 0; j < banks; j++) {
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001820 err = sysdev_create_file(&per_cpu(mce_dev, cpu),
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09001821 &bank_attrs[j]);
Andi Kleen0d7482e32009-02-17 23:07:13 +01001822 if (err)
1823 goto error2;
1824 }
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001825 cpumask_set_cpu(cpu, mce_dev_initialized);
Akinobu Mitad435d862007-10-18 03:05:15 -07001826
1827 return 0;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001828error2:
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09001829 while (--j >= 0)
1830 sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[j]);
Akinobu Mitad435d862007-10-18 03:05:15 -07001831error:
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001832 while (--i >= 0)
1833 sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1834
1835 sysdev_unregister(&per_cpu(mce_dev, cpu));
Akinobu Mitad435d862007-10-18 03:05:15 -07001836
Andi Kleen91c6d402005-07-28 21:15:39 -07001837 return err;
1838}
1839
Jan Beulich2d9cd6c2008-08-29 13:15:04 +01001840static __cpuinit void mce_remove_device(unsigned int cpu)
Andi Kleen91c6d402005-07-28 21:15:39 -07001841{
Shaohua Li73ca5352006-01-11 22:43:06 +01001842 int i;
1843
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001844 if (!cpumask_test_cpu(cpu, mce_dev_initialized))
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08001845 return;
1846
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001847 for (i = 0; mce_attrs[i]; i++)
1848 sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1849
Andi Kleen0d7482e32009-02-17 23:07:13 +01001850 for (i = 0; i < banks; i++)
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001851 sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
1852
1853 sysdev_unregister(&per_cpu(mce_dev, cpu));
1854 cpumask_clear_cpu(cpu, mce_dev_initialized);
Andi Kleen91c6d402005-07-28 21:15:39 -07001855}
Andi Kleen91c6d402005-07-28 21:15:39 -07001856
Andi Kleend6b75582009-02-12 13:39:31 +01001857/* Make sure there are no machine checks on offlined CPUs. */
H. Peter Anvinec5b3d32009-02-23 14:01:04 -08001858static void mce_disable_cpu(void *h)
Andi Kleend6b75582009-02-12 13:39:31 +01001859{
Andi Kleen88ccbed2009-02-12 13:49:36 +01001860 unsigned long action = *(unsigned long *)h;
Ingo Molnarcb491fc2009-04-08 12:31:17 +02001861 int i;
Andi Kleend6b75582009-02-12 13:39:31 +01001862
1863 if (!mce_available(&current_cpu_data))
1864 return;
Andi Kleen88ccbed2009-02-12 13:49:36 +01001865 if (!(action & CPU_TASKS_FROZEN))
1866 cmci_clear();
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001867 for (i = 0; i < banks; i++) {
1868 if (!skip_bank_init(i))
1869 wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
1870 }
Andi Kleend6b75582009-02-12 13:39:31 +01001871}
1872
H. Peter Anvinec5b3d32009-02-23 14:01:04 -08001873static void mce_reenable_cpu(void *h)
Andi Kleend6b75582009-02-12 13:39:31 +01001874{
Andi Kleen88ccbed2009-02-12 13:49:36 +01001875 unsigned long action = *(unsigned long *)h;
Ingo Molnare9eee032009-04-08 12:31:17 +02001876 int i;
Andi Kleend6b75582009-02-12 13:39:31 +01001877
1878 if (!mce_available(&current_cpu_data))
1879 return;
Ingo Molnare9eee032009-04-08 12:31:17 +02001880
Andi Kleen88ccbed2009-02-12 13:49:36 +01001881 if (!(action & CPU_TASKS_FROZEN))
1882 cmci_reenable();
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001883 for (i = 0; i < banks; i++) {
1884 if (!skip_bank_init(i))
1885 wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]);
1886 }
Andi Kleend6b75582009-02-12 13:39:31 +01001887}
1888
Andi Kleen91c6d402005-07-28 21:15:39 -07001889/* Get notified when a cpu comes on/off. Be hotplug friendly. */
Ingo Molnare9eee032009-04-08 12:31:17 +02001890static int __cpuinit
1891mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
Andi Kleen91c6d402005-07-28 21:15:39 -07001892{
1893 unsigned int cpu = (unsigned long)hcpu;
Andi Kleen52d168e2009-02-12 13:39:29 +01001894 struct timer_list *t = &per_cpu(mce_timer, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07001895
1896 switch (action) {
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08001897 case CPU_ONLINE:
1898 case CPU_ONLINE_FROZEN:
1899 mce_create_device(cpu);
Rafael J. Wysocki87357282008-08-22 22:23:09 +02001900 if (threshold_cpu_callback)
1901 threshold_cpu_callback(action, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07001902 break;
Andi Kleen91c6d402005-07-28 21:15:39 -07001903 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001904 case CPU_DEAD_FROZEN:
Rafael J. Wysocki87357282008-08-22 22:23:09 +02001905 if (threshold_cpu_callback)
1906 threshold_cpu_callback(action, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07001907 mce_remove_device(cpu);
1908 break;
Andi Kleen52d168e2009-02-12 13:39:29 +01001909 case CPU_DOWN_PREPARE:
1910 case CPU_DOWN_PREPARE_FROZEN:
1911 del_timer_sync(t);
Andi Kleen88ccbed2009-02-12 13:49:36 +01001912 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
Andi Kleen52d168e2009-02-12 13:39:29 +01001913 break;
1914 case CPU_DOWN_FAILED:
1915 case CPU_DOWN_FAILED_FROZEN:
Andi Kleen6298c512009-04-09 12:28:22 +02001916 t->expires = round_jiffies(jiffies +
Tejun Heo245b2e72009-06-24 15:13:48 +09001917 __get_cpu_var(mce_next_interval));
Andi Kleen52d168e2009-02-12 13:39:29 +01001918 add_timer_on(t, cpu);
Andi Kleen88ccbed2009-02-12 13:49:36 +01001919 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
1920 break;
1921 case CPU_POST_DEAD:
1922 /* intentionally ignoring frozen here */
1923 cmci_rediscover(cpu);
Andi Kleen52d168e2009-02-12 13:39:29 +01001924 break;
Andi Kleen91c6d402005-07-28 21:15:39 -07001925 }
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08001926 return NOTIFY_OK;
Andi Kleen91c6d402005-07-28 21:15:39 -07001927}
1928
Sam Ravnborg1e356692008-01-30 13:33:36 +01001929static struct notifier_block mce_cpu_notifier __cpuinitdata = {
Andi Kleen91c6d402005-07-28 21:15:39 -07001930 .notifier_call = mce_cpu_callback,
1931};
1932
Andi Kleen0d7482e32009-02-17 23:07:13 +01001933static __init int mce_init_banks(void)
1934{
1935 int i;
1936
1937 bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks,
1938 GFP_KERNEL);
1939 if (!bank_attrs)
1940 return -ENOMEM;
1941
1942 for (i = 0; i < banks; i++) {
1943 struct sysdev_attribute *a = &bank_attrs[i];
Ingo Molnare9eee032009-04-08 12:31:17 +02001944
1945 a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i);
Andi Kleen0d7482e32009-02-17 23:07:13 +01001946 if (!a->attr.name)
1947 goto nomem;
Ingo Molnare9eee032009-04-08 12:31:17 +02001948
1949 a->attr.mode = 0644;
1950 a->show = show_bank;
1951 a->store = set_bank;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001952 }
1953 return 0;
1954
1955nomem:
1956 while (--i >= 0)
1957 kfree(bank_attrs[i].attr.name);
1958 kfree(bank_attrs);
1959 bank_attrs = NULL;
Ingo Molnare9eee032009-04-08 12:31:17 +02001960
Andi Kleen0d7482e32009-02-17 23:07:13 +01001961 return -ENOMEM;
1962}
1963
Andi Kleen91c6d402005-07-28 21:15:39 -07001964static __init int mce_init_device(void)
1965{
1966 int err;
1967 int i = 0;
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (!mce_available(&boot_cpu_data))
1970 return -EIO;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001971
Yinghai Lue92fae02009-06-17 16:21:33 -07001972 zalloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
Rusty Russell996867d2009-03-13 14:49:51 +10301973
Andi Kleen0d7482e32009-02-17 23:07:13 +01001974 err = mce_init_banks();
1975 if (err)
1976 return err;
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 err = sysdev_class_register(&mce_sysclass);
Akinobu Mitad435d862007-10-18 03:05:15 -07001979 if (err)
1980 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07001981
1982 for_each_online_cpu(i) {
Akinobu Mitad435d862007-10-18 03:05:15 -07001983 err = mce_create_device(i);
1984 if (err)
1985 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07001986 }
1987
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -07001988 register_hotcpu_notifier(&mce_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 misc_register(&mce_log_device);
Ingo Molnare9eee032009-04-08 12:31:17 +02001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
Andi Kleen91c6d402005-07-28 21:15:39 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994device_initcall(mce_init_device);
Ingo Molnara988d332009-04-08 12:31:25 +02001995
Andi Kleen4efc0672009-04-28 19:07:31 +02001996#else /* CONFIG_X86_OLD_MCE: */
Ingo Molnara988d332009-04-08 12:31:25 +02001997
Ingo Molnara988d332009-04-08 12:31:25 +02001998int nr_mce_banks;
1999EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */
2000
Ingo Molnara988d332009-04-08 12:31:25 +02002001/* This has to be run for each processor */
2002void mcheck_init(struct cpuinfo_x86 *c)
2003{
Hidetoshi Setoc6978362009-06-15 17:22:49 +09002004 if (mce_disabled)
Ingo Molnara988d332009-04-08 12:31:25 +02002005 return;
2006
2007 switch (c->x86_vendor) {
2008 case X86_VENDOR_AMD:
2009 amd_mcheck_init(c);
2010 break;
2011
2012 case X86_VENDOR_INTEL:
2013 if (c->x86 == 5)
2014 intel_p5_mcheck_init(c);
2015 if (c->x86 == 6)
2016 intel_p6_mcheck_init(c);
2017 if (c->x86 == 15)
2018 intel_p4_mcheck_init(c);
2019 break;
2020
2021 case X86_VENDOR_CENTAUR:
2022 if (c->x86 == 5)
2023 winchip_mcheck_init(c);
2024 break;
2025
2026 default:
2027 break;
2028 }
Ingo Molnarb6592942009-04-08 12:31:27 +02002029 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks);
Ingo Molnara988d332009-04-08 12:31:25 +02002030}
2031
Ingo Molnara988d332009-04-08 12:31:25 +02002032static int __init mcheck_enable(char *str)
2033{
Hidetoshi Setoc6978362009-06-15 17:22:49 +09002034 mce_p5_enabled = 1;
Ingo Molnara988d332009-04-08 12:31:25 +02002035 return 1;
2036}
Ingo Molnara988d332009-04-08 12:31:25 +02002037__setup("mce", mcheck_enable);
2038
Andi Kleend7c3c9a2009-04-28 23:07:25 +02002039#endif /* CONFIG_X86_OLD_MCE */
2040
2041/*
2042 * Old style boot options parsing. Only for compatibility.
2043 */
2044static int __init mcheck_disable(char *str)
2045{
2046 mce_disabled = 1;
2047 return 1;
2048}
2049__setup("nomce", mcheck_disable);