Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Machine check handler. |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs. |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 5 | * Rest from unknown author(s). |
| 6 | * 2004 Andi Kleen. Rewrote most of it. |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 7 | * Copyright 2008 Intel Corporation |
| 8 | * Author: Andi Kleen |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 10 | #include <linux/thread_info.h> |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 11 | #include <linux/capability.h> |
| 12 | #include <linux/miscdevice.h> |
Andi Kleen | 8457c84 | 2009-02-12 13:49:33 +0100 | [diff] [blame] | 13 | #include <linux/ratelimit.h> |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 14 | #include <linux/kallsyms.h> |
| 15 | #include <linux/rcupdate.h> |
| 16 | #include <linux/smp_lock.h> |
| 17 | #include <linux/kobject.h> |
| 18 | #include <linux/kdebug.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/percpu.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/sysdev.h> |
| 23 | #include <linux/ctype.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/sysfs.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/kmod.h> |
| 29 | #include <linux/poll.h> |
| 30 | #include <linux/cpu.h> |
| 31 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 33 | #include <asm/processor.h> |
| 34 | #include <asm/uaccess.h> |
| 35 | #include <asm/idle.h> |
| 36 | #include <asm/mce.h> |
| 37 | #include <asm/msr.h> |
| 38 | #include <asm/smp.h> |
| 39 | |
Ingo Molnar | 711c2e4 | 2009-04-08 12:31:26 +0200 | [diff] [blame] | 40 | #include "mce.h" |
| 41 | |
Andi Kleen | 5d72792 | 2009-04-27 19:25:48 +0200 | [diff] [blame] | 42 | /* Handle unconfigured int18 (should never happen) */ |
| 43 | static void unexpected_machine_check(struct pt_regs *regs, long error_code) |
| 44 | { |
| 45 | printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n", |
| 46 | smp_processor_id()); |
| 47 | } |
| 48 | |
| 49 | /* Call the installed machine check handler for this CPU setup. */ |
| 50 | void (*machine_check_vector)(struct pt_regs *, long error_code) = |
| 51 | unexpected_machine_check; |
Andi Kleen | 04b2b1a | 2009-04-28 22:50:19 +0200 | [diff] [blame] | 52 | |
| 53 | int mce_disabled; |
| 54 | |
Andi Kleen | 4efc067 | 2009-04-28 19:07:31 +0200 | [diff] [blame] | 55 | #ifdef CONFIG_X86_NEW_MCE |
Ingo Molnar | 711c2e4 | 2009-04-08 12:31:26 +0200 | [diff] [blame] | 56 | |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 57 | #define MISC_MCELOG_MINOR 227 |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 58 | |
Andi Kleen | 553f265 | 2006-04-07 19:49:57 +0200 | [diff] [blame] | 59 | atomic_t mce_entry; |
| 60 | |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 61 | /* |
| 62 | * Tolerant levels: |
| 63 | * 0: always panic on uncorrected errors, log corrected errors |
| 64 | * 1: panic or SIGBUS on uncorrected errors, log corrected errors |
| 65 | * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors |
| 66 | * 3: never panic or SIGBUS, log all errors (for testing only) |
| 67 | */ |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 68 | static int tolerant = 1; |
| 69 | static int banks; |
| 70 | static u64 *bank; |
| 71 | static unsigned long notify_user; |
| 72 | static int rip_msr; |
| 73 | static int mce_bootlog = -1; |
| 74 | static atomic_t mce_events; |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 75 | |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 76 | static char trigger[128]; |
| 77 | static char *trigger_argv[2] = { trigger, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Andi Kleen | 06b7a7a | 2009-04-27 18:37:43 +0200 | [diff] [blame] | 79 | static unsigned long dont_init_banks; |
| 80 | |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 81 | static DECLARE_WAIT_QUEUE_HEAD(mce_wait); |
| 82 | |
Andi Kleen | ee031c3 | 2009-02-12 13:49:34 +0100 | [diff] [blame] | 83 | /* MCA banks polled by the period polling timer for corrected events */ |
| 84 | DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = { |
| 85 | [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL |
| 86 | }; |
| 87 | |
Andi Kleen | 06b7a7a | 2009-04-27 18:37:43 +0200 | [diff] [blame] | 88 | static inline int skip_bank_init(int i) |
| 89 | { |
| 90 | return i < BITS_PER_LONG && test_bit(i, &dont_init_banks); |
| 91 | } |
| 92 | |
Andi Kleen | b5f2fa4 | 2009-02-12 13:43:22 +0100 | [diff] [blame] | 93 | /* Do initial initialization of a struct mce */ |
| 94 | void mce_setup(struct mce *m) |
| 95 | { |
| 96 | memset(m, 0, sizeof(struct mce)); |
| 97 | m->cpu = smp_processor_id(); |
| 98 | rdtscll(m->tsc); |
| 99 | } |
| 100 | |
Andi Kleen | ea149b3 | 2009-04-29 19:31:00 +0200 | [diff] [blame] | 101 | DEFINE_PER_CPU(struct mce, injectm); |
| 102 | EXPORT_PER_CPU_SYMBOL_GPL(injectm); |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* |
| 105 | * Lockless MCE logging infrastructure. |
| 106 | * This avoids deadlocks on printk locks without having to break locks. Also |
| 107 | * separate MCEs from kernel messages to avoid bogus bug reports. |
| 108 | */ |
| 109 | |
Adrian Bunk | 231fd90 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 110 | static struct mce_log mcelog = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | MCE_LOG_SIGNATURE, |
| 112 | MCE_LOG_LEN, |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 113 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | void mce_log(struct mce *mce) |
| 116 | { |
| 117 | unsigned next, entry; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 118 | |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 119 | atomic_inc(&mce_events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | mce->finished = 0; |
Mike Waychison | 7644143 | 2005-09-30 00:01:27 +0200 | [diff] [blame] | 121 | wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | for (;;) { |
| 123 | entry = rcu_dereference(mcelog.next); |
Andi Kleen | 673242c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 124 | for (;;) { |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 125 | /* |
| 126 | * When the buffer fills up discard new entries. |
| 127 | * Assume that the earlier errors are the more |
| 128 | * interesting ones: |
| 129 | */ |
Andi Kleen | 673242c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 130 | if (entry >= MCE_LOG_LEN) { |
Jeremy Fitzhardinge | 53756d3 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 131 | set_bit(MCE_OVERFLOW, (unsigned long *)&mcelog.flags); |
Andi Kleen | 673242c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 132 | return; |
| 133 | } |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 134 | /* Old left over entry. Skip: */ |
Andi Kleen | 673242c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 135 | if (mcelog.entry[entry].finished) { |
| 136 | entry++; |
| 137 | continue; |
| 138 | } |
Mike Waychison | 7644143 | 2005-09-30 00:01:27 +0200 | [diff] [blame] | 139 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | smp_rmb(); |
| 142 | next = entry + 1; |
| 143 | if (cmpxchg(&mcelog.next, entry, next) == entry) |
| 144 | break; |
| 145 | } |
| 146 | memcpy(mcelog.entry + entry, mce, sizeof(struct mce)); |
Mike Waychison | 7644143 | 2005-09-30 00:01:27 +0200 | [diff] [blame] | 147 | wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | mcelog.entry[entry].finished = 1; |
Mike Waychison | 7644143 | 2005-09-30 00:01:27 +0200 | [diff] [blame] | 149 | wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 151 | set_bit(0, ¬ify_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void print_mce(struct mce *m) |
| 155 | { |
| 156 | printk(KERN_EMERG "\n" |
Andi Kleen | 4855170 | 2006-01-11 22:44:48 +0100 | [diff] [blame] | 157 | KERN_EMERG "HARDWARE ERROR\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | KERN_EMERG |
| 159 | "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n", |
| 160 | m->cpu, m->mcgstatus, m->bank, m->status); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 161 | if (m->ip) { |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 162 | printk(KERN_EMERG "RIP%s %02x:<%016Lx> ", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "", |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 164 | m->cs, m->ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | if (m->cs == __KERNEL_CS) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 166 | print_symbol("{%s}", m->ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | printk("\n"); |
| 168 | } |
H. Peter Anvin | f6d1826 | 2009-02-19 15:44:58 -0800 | [diff] [blame] | 169 | printk(KERN_EMERG "TSC %llx ", m->tsc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | if (m->addr) |
H. Peter Anvin | f6d1826 | 2009-02-19 15:44:58 -0800 | [diff] [blame] | 171 | printk("ADDR %llx ", m->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | if (m->misc) |
H. Peter Anvin | f6d1826 | 2009-02-19 15:44:58 -0800 | [diff] [blame] | 173 | printk("MISC %llx ", m->misc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | printk("\n"); |
Andi Kleen | 4855170 | 2006-01-11 22:44:48 +0100 | [diff] [blame] | 175 | printk(KERN_EMERG "This is not a software problem!\n"); |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 176 | printk(KERN_EMERG "Run through mcelog --ascii to decode " |
| 177 | "and contact your hardware vendor\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Andi Kleen | 3cde5c8 | 2009-04-27 18:01:31 +0200 | [diff] [blame] | 180 | static void mce_panic(char *msg, struct mce *backup, u64 start) |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 181 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | int i; |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 183 | |
Andi Kleen | d896a94 | 2009-04-28 14:25:18 +0200 | [diff] [blame] | 184 | bust_spinlocks(1); |
| 185 | console_verbose(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | for (i = 0; i < MCE_LOG_LEN; i++) { |
Andi Kleen | 3cde5c8 | 2009-04-27 18:01:31 +0200 | [diff] [blame] | 187 | u64 tsc = mcelog.entry[i].tsc; |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 188 | |
Andi Kleen | 3cde5c8 | 2009-04-27 18:01:31 +0200 | [diff] [blame] | 189 | if ((s64)(tsc - start) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | continue; |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 191 | print_mce(&mcelog.entry[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | if (backup && mcelog.entry[i].tsc == backup->tsc) |
| 193 | backup = NULL; |
| 194 | } |
| 195 | if (backup) |
| 196 | print_mce(backup); |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 197 | panic(msg); |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Andi Kleen | ea149b3 | 2009-04-29 19:31:00 +0200 | [diff] [blame] | 200 | /* Support code for software error injection */ |
| 201 | |
| 202 | static int msr_to_offset(u32 msr) |
| 203 | { |
| 204 | unsigned bank = __get_cpu_var(injectm.bank); |
| 205 | if (msr == rip_msr) |
| 206 | return offsetof(struct mce, ip); |
| 207 | if (msr == MSR_IA32_MC0_STATUS + bank*4) |
| 208 | return offsetof(struct mce, status); |
| 209 | if (msr == MSR_IA32_MC0_ADDR + bank*4) |
| 210 | return offsetof(struct mce, addr); |
| 211 | if (msr == MSR_IA32_MC0_MISC + bank*4) |
| 212 | return offsetof(struct mce, misc); |
| 213 | if (msr == MSR_IA32_MCG_STATUS) |
| 214 | return offsetof(struct mce, mcgstatus); |
| 215 | return -1; |
| 216 | } |
| 217 | |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 218 | /* MSR access wrappers used for error injection */ |
| 219 | static u64 mce_rdmsrl(u32 msr) |
| 220 | { |
| 221 | u64 v; |
Andi Kleen | ea149b3 | 2009-04-29 19:31:00 +0200 | [diff] [blame] | 222 | if (__get_cpu_var(injectm).finished) { |
| 223 | int offset = msr_to_offset(msr); |
| 224 | if (offset < 0) |
| 225 | return 0; |
| 226 | return *(u64 *)((char *)&__get_cpu_var(injectm) + offset); |
| 227 | } |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 228 | rdmsrl(msr, v); |
| 229 | return v; |
| 230 | } |
| 231 | |
| 232 | static void mce_wrmsrl(u32 msr, u64 v) |
| 233 | { |
Andi Kleen | ea149b3 | 2009-04-29 19:31:00 +0200 | [diff] [blame] | 234 | if (__get_cpu_var(injectm).finished) { |
| 235 | int offset = msr_to_offset(msr); |
| 236 | if (offset >= 0) |
| 237 | *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v; |
| 238 | return; |
| 239 | } |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 240 | wrmsrl(msr, v); |
| 241 | } |
| 242 | |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 243 | int mce_available(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Andi Kleen | 04b2b1a | 2009-04-28 22:50:19 +0200 | [diff] [blame] | 245 | if (mce_disabled) |
Andi Kleen | 5b4408f | 2009-02-12 13:39:30 +0100 | [diff] [blame] | 246 | return 0; |
Akinobu Mita | 3d1712c | 2006-03-24 03:15:11 -0800 | [diff] [blame] | 247 | return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Andi Kleen | 94ad847 | 2005-04-16 15:25:09 -0700 | [diff] [blame] | 250 | static inline void mce_get_rip(struct mce *m, struct pt_regs *regs) |
| 251 | { |
| 252 | if (regs && (m->mcgstatus & MCG_STATUS_RIPV)) { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 253 | m->ip = regs->ip; |
Andi Kleen | 94ad847 | 2005-04-16 15:25:09 -0700 | [diff] [blame] | 254 | m->cs = regs->cs; |
| 255 | } else { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 256 | m->ip = 0; |
Andi Kleen | 94ad847 | 2005-04-16 15:25:09 -0700 | [diff] [blame] | 257 | m->cs = 0; |
| 258 | } |
| 259 | if (rip_msr) { |
| 260 | /* Assume the RIP in the MSR is exact. Is this true? */ |
| 261 | m->mcgstatus |= MCG_STATUS_EIPV; |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 262 | m->ip = mce_rdmsrl(rip_msr); |
Andi Kleen | 94ad847 | 2005-04-16 15:25:09 -0700 | [diff] [blame] | 263 | m->cs = 0; |
| 264 | } |
| 265 | } |
| 266 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 267 | /* |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 268 | * Poll for corrected events or events that happened before reset. |
| 269 | * Those are just logged through /dev/mcelog. |
| 270 | * |
| 271 | * This is executed in standard interrupt context. |
| 272 | */ |
Andi Kleen | ee031c3 | 2009-02-12 13:49:34 +0100 | [diff] [blame] | 273 | void machine_check_poll(enum mcp_flags flags, mce_banks_t *b) |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 274 | { |
| 275 | struct mce m; |
| 276 | int i; |
| 277 | |
| 278 | mce_setup(&m); |
| 279 | |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 280 | m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS); |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 281 | for (i = 0; i < banks; i++) { |
Andi Kleen | ee031c3 | 2009-02-12 13:49:34 +0100 | [diff] [blame] | 282 | if (!bank[i] || !test_bit(i, *b)) |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 283 | continue; |
| 284 | |
| 285 | m.misc = 0; |
| 286 | m.addr = 0; |
| 287 | m.bank = i; |
| 288 | m.tsc = 0; |
| 289 | |
| 290 | barrier(); |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 291 | m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4); |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 292 | if (!(m.status & MCI_STATUS_VAL)) |
| 293 | continue; |
| 294 | |
| 295 | /* |
| 296 | * Uncorrected events are handled by the exception handler |
| 297 | * when it is enabled. But when the exception is disabled log |
| 298 | * everything. |
| 299 | * |
| 300 | * TBD do the same check for MCI_STATUS_EN here? |
| 301 | */ |
| 302 | if ((m.status & MCI_STATUS_UC) && !(flags & MCP_UC)) |
| 303 | continue; |
| 304 | |
| 305 | if (m.status & MCI_STATUS_MISCV) |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 306 | m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4); |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 307 | if (m.status & MCI_STATUS_ADDRV) |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 308 | m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4); |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 309 | |
| 310 | if (!(flags & MCP_TIMESTAMP)) |
| 311 | m.tsc = 0; |
| 312 | /* |
| 313 | * Don't get the IP here because it's unlikely to |
| 314 | * have anything to do with the actual error location. |
| 315 | */ |
Andi Kleen | 5679af4 | 2009-04-07 17:06:55 +0200 | [diff] [blame] | 316 | if (!(flags & MCP_DONTLOG)) { |
| 317 | mce_log(&m); |
| 318 | add_taint(TAINT_MACHINE_CHECK); |
| 319 | } |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * Clear state for this bank. |
| 323 | */ |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 324 | mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0); |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Don't clear MCG_STATUS here because it's only defined for |
| 329 | * exceptions. |
| 330 | */ |
| 331 | } |
Andi Kleen | ea149b3 | 2009-04-29 19:31:00 +0200 | [diff] [blame] | 332 | EXPORT_SYMBOL_GPL(machine_check_poll); |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * The actual machine check handler. This only handles real |
| 336 | * exceptions when something got corrupted coming in through int 18. |
| 337 | * |
| 338 | * This is executed in NMI context not subject to normal locking rules. This |
| 339 | * implies that most kernel services cannot be safely used. Don't even |
| 340 | * think about putting a printk in there! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | */ |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 342 | void do_machine_check(struct pt_regs *regs, long error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | struct mce m, panicm; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 345 | int panicm_found = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | u64 mcestart = 0; |
| 347 | int i; |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 348 | /* |
| 349 | * If no_way_out gets set, there is no safe way to recover from this |
| 350 | * MCE. If tolerant is cranked up, we'll try anyway. |
| 351 | */ |
| 352 | int no_way_out = 0; |
| 353 | /* |
| 354 | * If kill_it gets set, there might be a way to recover from this |
| 355 | * error. |
| 356 | */ |
| 357 | int kill_it = 0; |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 358 | DECLARE_BITMAP(toclear, MAX_NR_BANKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Andi Kleen | 553f265 | 2006-04-07 19:49:57 +0200 | [diff] [blame] | 360 | atomic_inc(&mce_entry); |
| 361 | |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 362 | if (notify_die(DIE_NMI, "machine check", regs, error_code, |
Jan Beulich | 22f5991 | 2008-01-30 13:31:23 +0100 | [diff] [blame] | 363 | 18, SIGKILL) == NOTIFY_STOP) |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 364 | goto out2; |
| 365 | if (!banks) |
Andi Kleen | 553f265 | 2006-04-07 19:49:57 +0200 | [diff] [blame] | 366 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Andi Kleen | b5f2fa4 | 2009-02-12 13:43:22 +0100 | [diff] [blame] | 368 | mce_setup(&m); |
| 369 | |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 370 | m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 371 | |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 372 | /* if the restart IP is not valid, we're done for */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | if (!(m.mcgstatus & MCG_STATUS_RIPV)) |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 374 | no_way_out = 1; |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | rdtscll(mcestart); |
| 377 | barrier(); |
| 378 | |
| 379 | for (i = 0; i < banks; i++) { |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 380 | __clear_bit(i, toclear); |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 381 | if (!bank[i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | continue; |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 383 | |
| 384 | m.misc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | m.addr = 0; |
| 386 | m.bank = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 388 | m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | if ((m.status & MCI_STATUS_VAL) == 0) |
| 390 | continue; |
| 391 | |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 392 | /* |
| 393 | * Non uncorrected errors are handled by machine_check_poll |
| 394 | * Leave them alone. |
| 395 | */ |
| 396 | if ((m.status & MCI_STATUS_UC) == 0) |
| 397 | continue; |
| 398 | |
| 399 | /* |
| 400 | * Set taint even when machine check was not enabled. |
| 401 | */ |
| 402 | add_taint(TAINT_MACHINE_CHECK); |
| 403 | |
| 404 | __set_bit(i, toclear); |
| 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | if (m.status & MCI_STATUS_EN) { |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 407 | /* if PCC was set, there's no way out */ |
| 408 | no_way_out |= !!(m.status & MCI_STATUS_PCC); |
| 409 | /* |
| 410 | * If this error was uncorrectable and there was |
| 411 | * an overflow, we're in trouble. If no overflow, |
| 412 | * we might get away with just killing a task. |
| 413 | */ |
| 414 | if (m.status & MCI_STATUS_UC) { |
| 415 | if (tolerant < 1 || m.status & MCI_STATUS_OVER) |
| 416 | no_way_out = 1; |
| 417 | kill_it = 1; |
| 418 | } |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 419 | } else { |
| 420 | /* |
| 421 | * Machine check event was not enabled. Clear, but |
| 422 | * ignore. |
| 423 | */ |
| 424 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | if (m.status & MCI_STATUS_MISCV) |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 428 | m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | if (m.status & MCI_STATUS_ADDRV) |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 430 | m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Andi Kleen | 94ad847 | 2005-04-16 15:25:09 -0700 | [diff] [blame] | 432 | mce_get_rip(&m, regs); |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 433 | mce_log(&m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 435 | /* |
| 436 | * Did this bank cause the exception? |
| 437 | * |
| 438 | * Assume that the bank with uncorrectable errors did it, |
| 439 | * and that there is only a single one: |
| 440 | */ |
| 441 | if ((m.status & MCI_STATUS_UC) && |
| 442 | (m.status & MCI_STATUS_EN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | panicm = m; |
| 444 | panicm_found = 1; |
| 445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 448 | /* |
| 449 | * If we didn't find an uncorrectable error, pick |
| 450 | * the last one (shouldn't happen, just being safe). |
| 451 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | if (!panicm_found) |
| 453 | panicm = m; |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * If we have decided that we just CAN'T continue, and the user |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 457 | * has not set tolerant to an insane level, give up and die. |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 458 | */ |
| 459 | if (no_way_out && tolerant < 3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | mce_panic("Machine check", &panicm, mcestart); |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * If the error seems to be unrecoverable, something should be |
| 464 | * done. Try to kill as little as possible. If we can kill just |
| 465 | * one task, do that. If the user has set the tolerance very |
| 466 | * high, don't try to do anything at all. |
| 467 | */ |
| 468 | if (kill_it && tolerant < 3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | int user_space = 0; |
| 470 | |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 471 | /* |
| 472 | * If the EIPV bit is set, it means the saved IP is the |
| 473 | * instruction which caused the MCE. |
| 474 | */ |
| 475 | if (m.mcgstatus & MCG_STATUS_EIPV) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 476 | user_space = panicm.ip && (panicm.cs & 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 478 | /* |
| 479 | * If we know that the error was in user space, send a |
| 480 | * SIGBUS. Otherwise, panic if tolerance is low. |
| 481 | * |
Andi Kleen | 380851b | 2009-02-12 13:39:33 +0100 | [diff] [blame] | 482 | * force_sig() takes an awful lot of locks and has a slight |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 483 | * risk of deadlocking. |
| 484 | */ |
| 485 | if (user_space) { |
Andi Kleen | 380851b | 2009-02-12 13:39:33 +0100 | [diff] [blame] | 486 | force_sig(SIGBUS, current); |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 487 | } else if (panic_on_oops || tolerant < 2) { |
| 488 | mce_panic("Uncorrected machine check", |
| 489 | &panicm, mcestart); |
| 490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 493 | /* notify userspace ASAP */ |
| 494 | set_thread_flag(TIF_MCE_NOTIFY); |
| 495 | |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 496 | /* the last thing we do is clear state */ |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 497 | for (i = 0; i < banks; i++) { |
| 498 | if (test_bit(i, toclear)) |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 499 | mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0); |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 500 | } |
Andi Kleen | 5f8c1a5 | 2009-04-29 19:29:12 +0200 | [diff] [blame] | 501 | mce_wrmsrl(MSR_IA32_MCG_STATUS, 0); |
Andi Kleen | 553f265 | 2006-04-07 19:49:57 +0200 | [diff] [blame] | 502 | out2: |
| 503 | atomic_dec(&mce_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
Andi Kleen | ea149b3 | 2009-04-29 19:31:00 +0200 | [diff] [blame] | 505 | EXPORT_SYMBOL_GPL(do_machine_check); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
Dmitriy Zavin | 15d5f83 | 2006-09-26 10:52:42 +0200 | [diff] [blame] | 507 | #ifdef CONFIG_X86_MCE_INTEL |
| 508 | /*** |
| 509 | * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog |
Simon Arlott | 676b185 | 2007-10-20 01:25:36 +0200 | [diff] [blame] | 510 | * @cpu: The CPU on which the event occurred. |
Dmitriy Zavin | 15d5f83 | 2006-09-26 10:52:42 +0200 | [diff] [blame] | 511 | * @status: Event status information |
| 512 | * |
| 513 | * This function should be called by the thermal interrupt after the |
| 514 | * event has been processed and the decision was made to log the event |
| 515 | * further. |
| 516 | * |
| 517 | * The status parameter will be saved to the 'status' field of 'struct mce' |
| 518 | * and historically has been the register value of the |
| 519 | * MSR_IA32_THERMAL_STATUS (Intel) msr. |
| 520 | */ |
Andi Kleen | b5f2fa4 | 2009-02-12 13:43:22 +0100 | [diff] [blame] | 521 | void mce_log_therm_throt_event(__u64 status) |
Dmitriy Zavin | 15d5f83 | 2006-09-26 10:52:42 +0200 | [diff] [blame] | 522 | { |
| 523 | struct mce m; |
| 524 | |
Andi Kleen | b5f2fa4 | 2009-02-12 13:43:22 +0100 | [diff] [blame] | 525 | mce_setup(&m); |
Dmitriy Zavin | 15d5f83 | 2006-09-26 10:52:42 +0200 | [diff] [blame] | 526 | m.bank = MCE_THERMAL_BANK; |
| 527 | m.status = status; |
Dmitriy Zavin | 15d5f83 | 2006-09-26 10:52:42 +0200 | [diff] [blame] | 528 | mce_log(&m); |
| 529 | } |
| 530 | #endif /* CONFIG_X86_MCE_INTEL */ |
| 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | /* |
Tim Hockin | 8a336b0 | 2007-05-02 19:27:19 +0200 | [diff] [blame] | 533 | * Periodic polling timer for "silent" machine check errors. If the |
| 534 | * poller finds an MCE, poll 2x faster. When the poller finds no more |
| 535 | * errors, poll 2x slower (up to check_interval seconds). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | static int check_interval = 5 * 60; /* 5 minutes */ |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 538 | |
Andi Kleen | 6298c51 | 2009-04-09 12:28:22 +0200 | [diff] [blame] | 539 | static DEFINE_PER_CPU(int, next_interval); /* in jiffies */ |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 540 | static DEFINE_PER_CPU(struct timer_list, mce_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 542 | static void mcheck_timer(unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 544 | struct timer_list *t = &per_cpu(mce_timer, data); |
Andi Kleen | 6298c51 | 2009-04-09 12:28:22 +0200 | [diff] [blame] | 545 | int *n; |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 546 | |
| 547 | WARN_ON(smp_processor_id() != data); |
| 548 | |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 549 | if (mce_available(¤t_cpu_data)) { |
Andi Kleen | ee031c3 | 2009-02-12 13:49:34 +0100 | [diff] [blame] | 550 | machine_check_poll(MCP_TIMESTAMP, |
| 551 | &__get_cpu_var(mce_poll_banks)); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
| 554 | /* |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 555 | * Alert userspace if needed. If we logged an MCE, reduce the |
| 556 | * polling interval, otherwise increase the polling interval. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | */ |
Andi Kleen | 6298c51 | 2009-04-09 12:28:22 +0200 | [diff] [blame] | 558 | n = &__get_cpu_var(next_interval); |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 559 | if (mce_notify_user()) { |
Andi Kleen | 6298c51 | 2009-04-09 12:28:22 +0200 | [diff] [blame] | 560 | *n = max(*n/2, HZ/100); |
Tim Hockin | 8a336b0 | 2007-05-02 19:27:19 +0200 | [diff] [blame] | 561 | } else { |
Andi Kleen | 6298c51 | 2009-04-09 12:28:22 +0200 | [diff] [blame] | 562 | *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
Tim Hockin | 8a336b0 | 2007-05-02 19:27:19 +0200 | [diff] [blame] | 564 | |
Andi Kleen | 6298c51 | 2009-04-09 12:28:22 +0200 | [diff] [blame] | 565 | t->expires = jiffies + *n; |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 566 | add_timer(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Andi Kleen | 9bd9840 | 2009-02-12 13:39:28 +0100 | [diff] [blame] | 569 | static void mce_do_trigger(struct work_struct *work) |
| 570 | { |
| 571 | call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT); |
| 572 | } |
| 573 | |
| 574 | static DECLARE_WORK(mce_trigger_work, mce_do_trigger); |
| 575 | |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 576 | /* |
Andi Kleen | 9bd9840 | 2009-02-12 13:39:28 +0100 | [diff] [blame] | 577 | * Notify the user(s) about new machine check events. |
| 578 | * Can be called from interrupt context, but not from machine check/NMI |
| 579 | * context. |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 580 | */ |
| 581 | int mce_notify_user(void) |
| 582 | { |
Andi Kleen | 8457c84 | 2009-02-12 13:49:33 +0100 | [diff] [blame] | 583 | /* Not more than two messages every minute */ |
| 584 | static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2); |
| 585 | |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 586 | clear_thread_flag(TIF_MCE_NOTIFY); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 587 | |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 588 | if (test_and_clear_bit(0, ¬ify_user)) { |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 589 | wake_up_interruptible(&mce_wait); |
Andi Kleen | 9bd9840 | 2009-02-12 13:39:28 +0100 | [diff] [blame] | 590 | |
| 591 | /* |
| 592 | * There is no risk of missing notifications because |
| 593 | * work_pending is always cleared before the function is |
| 594 | * executed. |
| 595 | */ |
| 596 | if (trigger[0] && !work_pending(&mce_trigger_work)) |
| 597 | schedule_work(&mce_trigger_work); |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 598 | |
Andi Kleen | 8457c84 | 2009-02-12 13:49:33 +0100 | [diff] [blame] | 599 | if (__ratelimit(&ratelimit)) |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 600 | printk(KERN_INFO "Machine check events logged\n"); |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 601 | |
| 602 | return 1; |
| 603 | } |
| 604 | return 0; |
| 605 | } |
Andi Kleen | ea149b3 | 2009-04-29 19:31:00 +0200 | [diff] [blame] | 606 | EXPORT_SYMBOL_GPL(mce_notify_user); |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 607 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 608 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | * Initialize Machine Checks for a CPU. |
| 610 | */ |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 611 | static int mce_cap_init(void) |
| 612 | { |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 613 | unsigned b; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 614 | u64 cap; |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 615 | |
| 616 | rdmsrl(MSR_IA32_MCG_CAP, cap); |
Thomas Gleixner | 01c6680 | 2009-04-08 12:31:24 +0200 | [diff] [blame] | 617 | |
| 618 | b = cap & MCG_BANKCNT_MASK; |
Ingo Molnar | b659294 | 2009-04-08 12:31:27 +0200 | [diff] [blame] | 619 | printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b); |
| 620 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 621 | if (b > MAX_NR_BANKS) { |
| 622 | printk(KERN_WARNING |
| 623 | "MCE: Using only %u machine check banks out of %u\n", |
| 624 | MAX_NR_BANKS, b); |
| 625 | b = MAX_NR_BANKS; |
| 626 | } |
| 627 | |
| 628 | /* Don't support asymmetric configurations today */ |
| 629 | WARN_ON(banks != 0 && b != banks); |
| 630 | banks = b; |
| 631 | if (!bank) { |
| 632 | bank = kmalloc(banks * sizeof(u64), GFP_KERNEL); |
| 633 | if (!bank) |
| 634 | return -ENOMEM; |
| 635 | memset(bank, 0xff, banks * sizeof(u64)); |
| 636 | } |
| 637 | |
| 638 | /* Use accurate RIP reporting if available. */ |
Thomas Gleixner | 01c6680 | 2009-04-08 12:31:24 +0200 | [diff] [blame] | 639 | if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9) |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 640 | rip_msr = MSR_IA32_MCG_EIP; |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | static void mce_init(void *dummy) |
| 646 | { |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 647 | mce_banks_t all_banks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | u64 cap; |
| 649 | int i; |
| 650 | |
Andi Kleen | b79109c | 2009-02-12 13:43:23 +0100 | [diff] [blame] | 651 | /* |
| 652 | * Log the machine checks left over from the previous reset. |
| 653 | */ |
Andi Kleen | ee031c3 | 2009-02-12 13:49:34 +0100 | [diff] [blame] | 654 | bitmap_fill(all_banks, MAX_NR_BANKS); |
Andi Kleen | 5679af4 | 2009-04-07 17:06:55 +0200 | [diff] [blame] | 655 | machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | set_in_cr4(X86_CR4_MCE); |
| 658 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 659 | rdmsrl(MSR_IA32_MCG_CAP, cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | if (cap & MCG_CTL_P) |
| 661 | wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff); |
| 662 | |
| 663 | for (i = 0; i < banks; i++) { |
Andi Kleen | 06b7a7a | 2009-04-27 18:37:43 +0200 | [diff] [blame] | 664 | if (skip_bank_init(i)) |
| 665 | continue; |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 666 | wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0); |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /* Add per CPU specific workarounds here */ |
H. Peter Anvin | ec5b3d3 | 2009-02-23 14:01:04 -0800 | [diff] [blame] | 672 | static void mce_cpu_quirks(struct cpuinfo_x86 *c) |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 673 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | /* This should be disabled by the BIOS, but isn't always */ |
Jan Beulich | 911f6a7 | 2008-04-22 16:22:21 +0100 | [diff] [blame] | 675 | if (c->x86_vendor == X86_VENDOR_AMD) { |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 676 | if (c->x86 == 15 && banks > 4) { |
| 677 | /* |
| 678 | * disable GART TBL walk error reporting, which |
| 679 | * trips off incorrectly with the IOMMU & 3ware |
| 680 | * & Cerberus: |
| 681 | */ |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 682 | clear_bit(10, (unsigned long *)&bank[4]); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 683 | } |
| 684 | if (c->x86 <= 17 && mce_bootlog < 0) { |
| 685 | /* |
| 686 | * Lots of broken BIOS around that don't clear them |
| 687 | * by default and leave crap in there. Don't log: |
| 688 | */ |
Jan Beulich | 911f6a7 | 2008-04-22 16:22:21 +0100 | [diff] [blame] | 689 | mce_bootlog = 0; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 690 | } |
Andi Kleen | 2e6f694 | 2009-04-27 18:42:48 +0200 | [diff] [blame] | 691 | /* |
| 692 | * Various K7s with broken bank 0 around. Always disable |
| 693 | * by default. |
| 694 | */ |
| 695 | if (c->x86 == 6) |
| 696 | bank[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
Andi Kleen | e583538 | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 698 | |
Andi Kleen | 06b7a7a | 2009-04-27 18:37:43 +0200 | [diff] [blame] | 699 | if (c->x86_vendor == X86_VENDOR_INTEL) { |
| 700 | /* |
| 701 | * SDM documents that on family 6 bank 0 should not be written |
| 702 | * because it aliases to another special BIOS controlled |
| 703 | * register. |
| 704 | * But it's not aliased anymore on model 0x1a+ |
| 705 | * Don't ignore bank 0 completely because there could be a |
| 706 | * valid event later, merely don't write CTL0. |
| 707 | */ |
| 708 | |
| 709 | if (c->x86 == 6 && c->x86_model < 0x1A) |
| 710 | __set_bit(0, &dont_init_banks); |
| 711 | } |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 712 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Andi Kleen | 4efc067 | 2009-04-28 19:07:31 +0200 | [diff] [blame] | 714 | static void __cpuinit mce_ancient_init(struct cpuinfo_x86 *c) |
| 715 | { |
| 716 | if (c->x86 != 5) |
| 717 | return; |
| 718 | switch (c->x86_vendor) { |
| 719 | case X86_VENDOR_INTEL: |
| 720 | if (mce_p5_enabled()) |
| 721 | intel_p5_mcheck_init(c); |
| 722 | break; |
| 723 | case X86_VENDOR_CENTAUR: |
| 724 | winchip_mcheck_init(c); |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | |
H. Peter Anvin | cc3ca22 | 2009-02-20 23:35:51 -0800 | [diff] [blame] | 729 | static void mce_cpu_features(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
| 731 | switch (c->x86_vendor) { |
| 732 | case X86_VENDOR_INTEL: |
| 733 | mce_intel_feature_init(c); |
| 734 | break; |
Jacob Shin | 89b831e | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 735 | case X86_VENDOR_AMD: |
| 736 | mce_amd_feature_init(c); |
| 737 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | default: |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 743 | static void mce_init_timer(void) |
| 744 | { |
| 745 | struct timer_list *t = &__get_cpu_var(mce_timer); |
Andi Kleen | 6298c51 | 2009-04-09 12:28:22 +0200 | [diff] [blame] | 746 | int *n = &__get_cpu_var(next_interval); |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 747 | |
Andi Kleen | 6298c51 | 2009-04-09 12:28:22 +0200 | [diff] [blame] | 748 | *n = check_interval * HZ; |
| 749 | if (!*n) |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 750 | return; |
| 751 | setup_timer(t, mcheck_timer, smp_processor_id()); |
Andi Kleen | 6298c51 | 2009-04-09 12:28:22 +0200 | [diff] [blame] | 752 | t->expires = round_jiffies(jiffies + *n); |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 753 | add_timer(t); |
| 754 | } |
| 755 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 756 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | * Called for each booted CPU to set up machine checks. |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 758 | * Must be called with preempt off: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | */ |
Ashok Raj | e6982c6 | 2005-06-25 14:54:58 -0700 | [diff] [blame] | 760 | void __cpuinit mcheck_init(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
Andi Kleen | 4efc067 | 2009-04-28 19:07:31 +0200 | [diff] [blame] | 762 | if (mce_disabled) |
| 763 | return; |
| 764 | |
| 765 | mce_ancient_init(c); |
| 766 | |
Andi Kleen | 5b4408f | 2009-02-12 13:39:30 +0100 | [diff] [blame] | 767 | if (!mce_available(c)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | return; |
| 769 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 770 | if (mce_cap_init() < 0) { |
Andi Kleen | 04b2b1a | 2009-04-28 22:50:19 +0200 | [diff] [blame] | 771 | mce_disabled = 1; |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 772 | return; |
| 773 | } |
| 774 | mce_cpu_quirks(c); |
| 775 | |
Andi Kleen | 5d72792 | 2009-04-27 19:25:48 +0200 | [diff] [blame] | 776 | machine_check_vector = do_machine_check; |
| 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | mce_init(NULL); |
| 779 | mce_cpu_features(c); |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 780 | mce_init_timer(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Character device to read and clear the MCE log. |
| 785 | */ |
| 786 | |
Tim Hockin | f528e7b | 2007-07-21 17:10:35 +0200 | [diff] [blame] | 787 | static DEFINE_SPINLOCK(mce_state_lock); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 788 | static int open_count; /* #times opened */ |
| 789 | static int open_exclu; /* already open exclusive? */ |
Tim Hockin | f528e7b | 2007-07-21 17:10:35 +0200 | [diff] [blame] | 790 | |
| 791 | static int mce_open(struct inode *inode, struct file *file) |
| 792 | { |
Arnd Bergmann | 38c4c97 | 2008-05-20 19:17:02 +0200 | [diff] [blame] | 793 | lock_kernel(); |
Tim Hockin | f528e7b | 2007-07-21 17:10:35 +0200 | [diff] [blame] | 794 | spin_lock(&mce_state_lock); |
| 795 | |
| 796 | if (open_exclu || (open_count && (file->f_flags & O_EXCL))) { |
| 797 | spin_unlock(&mce_state_lock); |
Arnd Bergmann | 38c4c97 | 2008-05-20 19:17:02 +0200 | [diff] [blame] | 798 | unlock_kernel(); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 799 | |
Tim Hockin | f528e7b | 2007-07-21 17:10:35 +0200 | [diff] [blame] | 800 | return -EBUSY; |
| 801 | } |
| 802 | |
| 803 | if (file->f_flags & O_EXCL) |
| 804 | open_exclu = 1; |
| 805 | open_count++; |
| 806 | |
| 807 | spin_unlock(&mce_state_lock); |
Arnd Bergmann | 38c4c97 | 2008-05-20 19:17:02 +0200 | [diff] [blame] | 808 | unlock_kernel(); |
Tim Hockin | f528e7b | 2007-07-21 17:10:35 +0200 | [diff] [blame] | 809 | |
Tim Hockin | bd78432 | 2007-07-21 17:10:37 +0200 | [diff] [blame] | 810 | return nonseekable_open(inode, file); |
Tim Hockin | f528e7b | 2007-07-21 17:10:35 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | static int mce_release(struct inode *inode, struct file *file) |
| 814 | { |
| 815 | spin_lock(&mce_state_lock); |
| 816 | |
| 817 | open_count--; |
| 818 | open_exclu = 0; |
| 819 | |
| 820 | spin_unlock(&mce_state_lock); |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 825 | static void collect_tscs(void *data) |
| 826 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | unsigned long *cpu_tsc = (unsigned long *)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 829 | rdtscll(cpu_tsc[smp_processor_id()]); |
| 830 | } |
| 831 | |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 832 | static DEFINE_MUTEX(mce_read_mutex); |
| 833 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 834 | static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize, |
| 835 | loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | char __user *buf = ubuf; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 838 | unsigned long *cpu_tsc; |
| 839 | unsigned prev, next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | int i, err; |
| 841 | |
Mike Travis | 6bca67f | 2008-07-18 18:11:27 -0700 | [diff] [blame] | 842 | cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL); |
Andi Kleen | f0de53b | 2005-04-16 15:25:10 -0700 | [diff] [blame] | 843 | if (!cpu_tsc) |
| 844 | return -ENOMEM; |
| 845 | |
Daniel Walker | 8c8b885 | 2008-01-30 13:31:17 +0100 | [diff] [blame] | 846 | mutex_lock(&mce_read_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | next = rcu_dereference(mcelog.next); |
| 848 | |
| 849 | /* Only supports full reads right now */ |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 850 | if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) { |
Daniel Walker | 8c8b885 | 2008-01-30 13:31:17 +0100 | [diff] [blame] | 851 | mutex_unlock(&mce_read_mutex); |
Andi Kleen | f0de53b | 2005-04-16 15:25:10 -0700 | [diff] [blame] | 852 | kfree(cpu_tsc); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | return -EINVAL; |
| 855 | } |
| 856 | |
| 857 | err = 0; |
Huang Ying | ef41df434 | 2009-02-12 13:39:34 +0100 | [diff] [blame] | 858 | prev = 0; |
| 859 | do { |
| 860 | for (i = prev; i < next; i++) { |
| 861 | unsigned long start = jiffies; |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 862 | |
Huang Ying | ef41df434 | 2009-02-12 13:39:34 +0100 | [diff] [blame] | 863 | while (!mcelog.entry[i].finished) { |
| 864 | if (time_after_eq(jiffies, start + 2)) { |
| 865 | memset(mcelog.entry + i, 0, |
| 866 | sizeof(struct mce)); |
| 867 | goto timeout; |
| 868 | } |
| 869 | cpu_relax(); |
Andi Kleen | 673242c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 870 | } |
Huang Ying | ef41df434 | 2009-02-12 13:39:34 +0100 | [diff] [blame] | 871 | smp_rmb(); |
| 872 | err |= copy_to_user(buf, mcelog.entry + i, |
| 873 | sizeof(struct mce)); |
| 874 | buf += sizeof(struct mce); |
| 875 | timeout: |
| 876 | ; |
Andi Kleen | 673242c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 877 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
Huang Ying | ef41df434 | 2009-02-12 13:39:34 +0100 | [diff] [blame] | 879 | memset(mcelog.entry + prev, 0, |
| 880 | (next - prev) * sizeof(struct mce)); |
| 881 | prev = next; |
| 882 | next = cmpxchg(&mcelog.next, prev, 0); |
| 883 | } while (next != prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Paul E. McKenney | b2b1866 | 2005-06-25 14:55:38 -0700 | [diff] [blame] | 885 | synchronize_sched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 887 | /* |
| 888 | * Collect entries that were still getting written before the |
| 889 | * synchronize. |
| 890 | */ |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 891 | on_each_cpu(collect_tscs, cpu_tsc, 1); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 892 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 893 | for (i = next; i < MCE_LOG_LEN; i++) { |
| 894 | if (mcelog.entry[i].finished && |
| 895 | mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) { |
| 896 | err |= copy_to_user(buf, mcelog.entry+i, |
| 897 | sizeof(struct mce)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | smp_rmb(); |
| 899 | buf += sizeof(struct mce); |
| 900 | memset(&mcelog.entry[i], 0, sizeof(struct mce)); |
| 901 | } |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 902 | } |
Daniel Walker | 8c8b885 | 2008-01-30 13:31:17 +0100 | [diff] [blame] | 903 | mutex_unlock(&mce_read_mutex); |
Andi Kleen | f0de53b | 2005-04-16 15:25:10 -0700 | [diff] [blame] | 904 | kfree(cpu_tsc); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 905 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 906 | return err ? -EFAULT : buf - ubuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Tim Hockin | e02e68d | 2007-07-21 17:10:36 +0200 | [diff] [blame] | 909 | static unsigned int mce_poll(struct file *file, poll_table *wait) |
| 910 | { |
| 911 | poll_wait(file, &mce_wait, wait); |
| 912 | if (rcu_dereference(mcelog.next)) |
| 913 | return POLLIN | POLLRDNORM; |
| 914 | return 0; |
| 915 | } |
| 916 | |
Nikanth Karthikesan | c68461b | 2008-01-30 13:32:59 +0100 | [diff] [blame] | 917 | static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | { |
| 919 | int __user *p = (int __user *)arg; |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | if (!capable(CAP_SYS_ADMIN)) |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 922 | return -EPERM; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | switch (cmd) { |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 925 | case MCE_GET_RECORD_LEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return put_user(sizeof(struct mce), p); |
| 927 | case MCE_GET_LOG_LEN: |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 928 | return put_user(MCE_LOG_LEN, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | case MCE_GETCLEAR_FLAGS: { |
| 930 | unsigned flags; |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 931 | |
| 932 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | flags = mcelog.flags; |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 934 | } while (cmpxchg(&mcelog.flags, flags, 0) != flags); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 935 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 936 | return put_user(flags, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | default: |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 939 | return -ENOTTY; |
| 940 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | |
H. Peter Anvin | a1ff41b | 2009-05-25 22:16:14 -0700 | [diff] [blame^] | 943 | /* Modified in mce-inject.c, so not static or const */ |
Andi Kleen | ea149b3 | 2009-04-29 19:31:00 +0200 | [diff] [blame] | 944 | struct file_operations mce_chrdev_ops = { |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 945 | .open = mce_open, |
| 946 | .release = mce_release, |
| 947 | .read = mce_read, |
| 948 | .poll = mce_poll, |
| 949 | .unlocked_ioctl = mce_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | }; |
Andi Kleen | ea149b3 | 2009-04-29 19:31:00 +0200 | [diff] [blame] | 951 | EXPORT_SYMBOL_GPL(mce_chrdev_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
| 953 | static struct miscdevice mce_log_device = { |
| 954 | MISC_MCELOG_MINOR, |
| 955 | "mcelog", |
| 956 | &mce_chrdev_ops, |
| 957 | }; |
| 958 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 959 | /* |
Hidetoshi Seto | 13503fa | 2009-03-26 17:39:20 +0900 | [diff] [blame] | 960 | * mce=off disables machine check |
| 961 | * mce=TOLERANCELEVEL (number, see above) |
| 962 | * mce=bootlog Log MCEs from before booting. Disabled by default on AMD. |
| 963 | * mce=nobootlog Don't log MCEs from before booting. |
| 964 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | static int __init mcheck_enable(char *str) |
| 966 | { |
Andi Kleen | 4efc067 | 2009-04-28 19:07:31 +0200 | [diff] [blame] | 967 | if (*str == 0) |
| 968 | enable_p5_mce(); |
| 969 | if (*str == '=') |
| 970 | str++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | if (!strcmp(str, "off")) |
Andi Kleen | 04b2b1a | 2009-04-28 22:50:19 +0200 | [diff] [blame] | 972 | mce_disabled = 1; |
Hidetoshi Seto | 13503fa | 2009-03-26 17:39:20 +0900 | [diff] [blame] | 973 | else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog")) |
| 974 | mce_bootlog = (str[0] == 'b'); |
Andi Kleen | 8c566ef | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 975 | else if (isdigit(str[0])) |
| 976 | get_option(&str, &tolerant); |
Hidetoshi Seto | 13503fa | 2009-03-26 17:39:20 +0900 | [diff] [blame] | 977 | else { |
Andi Kleen | 4efc067 | 2009-04-28 19:07:31 +0200 | [diff] [blame] | 978 | printk(KERN_INFO "mce argument %s ignored. Please use /sys\n", |
Hidetoshi Seto | 13503fa | 2009-03-26 17:39:20 +0900 | [diff] [blame] | 979 | str); |
| 980 | return 0; |
| 981 | } |
OGAWA Hirofumi | 9b41046 | 2006-03-31 02:30:33 -0800 | [diff] [blame] | 982 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | } |
Andi Kleen | 4efc067 | 2009-04-28 19:07:31 +0200 | [diff] [blame] | 984 | __setup("mce", mcheck_enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 986 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | * Sysfs support |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 988 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
Andi Kleen | 973a2dd | 2009-02-12 13:39:32 +0100 | [diff] [blame] | 990 | /* |
| 991 | * Disable machine checks on suspend and shutdown. We can't really handle |
| 992 | * them later. |
| 993 | */ |
| 994 | static int mce_disable(void) |
| 995 | { |
| 996 | int i; |
| 997 | |
Andi Kleen | 06b7a7a | 2009-04-27 18:37:43 +0200 | [diff] [blame] | 998 | for (i = 0; i < banks; i++) { |
| 999 | if (!skip_bank_init(i)) |
| 1000 | wrmsrl(MSR_IA32_MC0_CTL + i*4, 0); |
| 1001 | } |
Andi Kleen | 973a2dd | 2009-02-12 13:39:32 +0100 | [diff] [blame] | 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | static int mce_suspend(struct sys_device *dev, pm_message_t state) |
| 1006 | { |
| 1007 | return mce_disable(); |
| 1008 | } |
| 1009 | |
| 1010 | static int mce_shutdown(struct sys_device *dev) |
| 1011 | { |
| 1012 | return mce_disable(); |
| 1013 | } |
| 1014 | |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1015 | /* |
| 1016 | * On resume clear all MCE state. Don't want to see leftovers from the BIOS. |
| 1017 | * Only one CPU is active at this time, the others get re-added later using |
| 1018 | * CPU hotplug: |
| 1019 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | static int mce_resume(struct sys_device *dev) |
| 1021 | { |
Andi Kleen | 413588c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 1022 | mce_init(NULL); |
Andi Kleen | 6ec68bf | 2009-02-12 13:39:26 +0100 | [diff] [blame] | 1023 | mce_cpu_features(¤t_cpu_data); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | return 0; |
| 1026 | } |
| 1027 | |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 1028 | static void mce_cpu_restart(void *data) |
| 1029 | { |
| 1030 | del_timer_sync(&__get_cpu_var(mce_timer)); |
| 1031 | if (mce_available(¤t_cpu_data)) |
| 1032 | mce_init(NULL); |
| 1033 | mce_init_timer(); |
| 1034 | } |
| 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | /* Reinit MCEs after user configuration changes */ |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 1037 | static void mce_restart(void) |
| 1038 | { |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 1039 | on_each_cpu(mce_cpu_restart, NULL, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static struct sysdev_class mce_sysclass = { |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1043 | .suspend = mce_suspend, |
| 1044 | .shutdown = mce_shutdown, |
| 1045 | .resume = mce_resume, |
| 1046 | .name = "machinecheck", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | }; |
| 1048 | |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1049 | DEFINE_PER_CPU(struct sys_device, mce_dev); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1050 | |
| 1051 | __cpuinitdata |
| 1052 | void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | /* Why are there no generic functions for this? */ |
| 1055 | #define ACCESSOR(name, var, start) \ |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1056 | static ssize_t show_ ## name(struct sys_device *s, \ |
| 1057 | struct sysdev_attribute *attr, \ |
| 1058 | char *buf) { \ |
Andi Kleen | 3cde5c8 | 2009-04-27 18:01:31 +0200 | [diff] [blame] | 1059 | return sprintf(buf, "%Lx\n", (u64)var); \ |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 1060 | } \ |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1061 | static ssize_t set_ ## name(struct sys_device *s, \ |
| 1062 | struct sysdev_attribute *attr, \ |
| 1063 | const char *buf, size_t siz) { \ |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 1064 | char *end; \ |
Andi Kleen | 3cde5c8 | 2009-04-27 18:01:31 +0200 | [diff] [blame] | 1065 | u64 new = simple_strtoull(buf, &end, 0); \ |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1066 | \ |
| 1067 | if (end == buf) \ |
| 1068 | return -EINVAL; \ |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 1069 | var = new; \ |
| 1070 | start; \ |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1071 | \ |
Thomas Gleixner | d88203d | 2007-10-23 22:37:23 +0200 | [diff] [blame] | 1072 | return end-buf; \ |
| 1073 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | static SYSDEV_ATTR(name, 0644, show_ ## name, set_ ## name); |
| 1075 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1076 | static struct sysdev_attribute *bank_attrs; |
| 1077 | |
| 1078 | static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr, |
| 1079 | char *buf) |
| 1080 | { |
| 1081 | u64 b = bank[attr - bank_attrs]; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1082 | |
H. Peter Anvin | f6d1826 | 2009-02-19 15:44:58 -0800 | [diff] [blame] | 1083 | return sprintf(buf, "%llx\n", b); |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr, |
| 1087 | const char *buf, size_t siz) |
| 1088 | { |
| 1089 | char *end; |
| 1090 | u64 new = simple_strtoull(buf, &end, 0); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1091 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1092 | if (end == buf) |
| 1093 | return -EINVAL; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1094 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1095 | bank[attr - bank_attrs] = new; |
| 1096 | mce_restart(); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1097 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1098 | return end-buf; |
| 1099 | } |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 1100 | |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1101 | static ssize_t |
| 1102 | show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf) |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 1103 | { |
| 1104 | strcpy(buf, trigger); |
| 1105 | strcat(buf, "\n"); |
| 1106 | return strlen(trigger) + 1; |
| 1107 | } |
| 1108 | |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1109 | static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr, |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1110 | const char *buf, size_t siz) |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 1111 | { |
| 1112 | char *p; |
| 1113 | int len; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1114 | |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 1115 | strncpy(trigger, buf, sizeof(trigger)); |
| 1116 | trigger[sizeof(trigger)-1] = 0; |
| 1117 | len = strlen(trigger); |
| 1118 | p = strchr(trigger, '\n'); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1119 | |
| 1120 | if (*p) |
| 1121 | *p = 0; |
| 1122 | |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 1123 | return len; |
| 1124 | } |
| 1125 | |
| 1126 | static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger); |
Andi Kleen | d95d62c | 2008-07-01 18:48:43 +0200 | [diff] [blame] | 1127 | static SYSDEV_INT_ATTR(tolerant, 0644, tolerant); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1128 | |
| 1129 | ACCESSOR(check_interval, check_interval, mce_restart()) |
| 1130 | |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1131 | static struct sysdev_attribute *mce_attrs[] = { |
Andi Kleen | d95d62c | 2008-07-01 18:48:43 +0200 | [diff] [blame] | 1132 | &attr_tolerant.attr, &attr_check_interval, &attr_trigger, |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 1133 | NULL |
| 1134 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1136 | static cpumask_var_t mce_dev_initialized; |
Andreas Herrmann | bae19fe | 2007-11-14 17:00:44 -0800 | [diff] [blame] | 1137 | |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1138 | /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */ |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1139 | static __cpuinit int mce_create_device(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | { |
| 1141 | int err; |
Shaohua Li | 73ca535 | 2006-01-11 22:43:06 +0100 | [diff] [blame] | 1142 | int i; |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 1143 | |
Andreas Herrmann | 9036755 | 2007-11-07 02:12:58 +0100 | [diff] [blame] | 1144 | if (!mce_available(&boot_cpu_data)) |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1145 | return -EIO; |
| 1146 | |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1147 | memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject)); |
| 1148 | per_cpu(mce_dev, cpu).id = cpu; |
| 1149 | per_cpu(mce_dev, cpu).cls = &mce_sysclass; |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1150 | |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1151 | err = sysdev_register(&per_cpu(mce_dev, cpu)); |
Akinobu Mita | d435d86 | 2007-10-18 03:05:15 -0700 | [diff] [blame] | 1152 | if (err) |
| 1153 | return err; |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1154 | |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1155 | for (i = 0; mce_attrs[i]; i++) { |
| 1156 | err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]); |
Akinobu Mita | d435d86 | 2007-10-18 03:05:15 -0700 | [diff] [blame] | 1157 | if (err) |
| 1158 | goto error; |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1159 | } |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1160 | for (i = 0; i < banks; i++) { |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1161 | err = sysdev_create_file(&per_cpu(mce_dev, cpu), |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1162 | &bank_attrs[i]); |
| 1163 | if (err) |
| 1164 | goto error2; |
| 1165 | } |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1166 | cpumask_set_cpu(cpu, mce_dev_initialized); |
Akinobu Mita | d435d86 | 2007-10-18 03:05:15 -0700 | [diff] [blame] | 1167 | |
| 1168 | return 0; |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1169 | error2: |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1170 | while (--i >= 0) |
| 1171 | sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]); |
Akinobu Mita | d435d86 | 2007-10-18 03:05:15 -0700 | [diff] [blame] | 1172 | error: |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1173 | while (--i >= 0) |
| 1174 | sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]); |
| 1175 | |
| 1176 | sysdev_unregister(&per_cpu(mce_dev, cpu)); |
Akinobu Mita | d435d86 | 2007-10-18 03:05:15 -0700 | [diff] [blame] | 1177 | |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1178 | return err; |
| 1179 | } |
| 1180 | |
Jan Beulich | 2d9cd6c | 2008-08-29 13:15:04 +0100 | [diff] [blame] | 1181 | static __cpuinit void mce_remove_device(unsigned int cpu) |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1182 | { |
Shaohua Li | 73ca535 | 2006-01-11 22:43:06 +0100 | [diff] [blame] | 1183 | int i; |
| 1184 | |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1185 | if (!cpumask_test_cpu(cpu, mce_dev_initialized)) |
Andreas Herrmann | bae19fe | 2007-11-14 17:00:44 -0800 | [diff] [blame] | 1186 | return; |
| 1187 | |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1188 | for (i = 0; mce_attrs[i]; i++) |
| 1189 | sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]); |
| 1190 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1191 | for (i = 0; i < banks; i++) |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1192 | sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]); |
| 1193 | |
| 1194 | sysdev_unregister(&per_cpu(mce_dev, cpu)); |
| 1195 | cpumask_clear_cpu(cpu, mce_dev_initialized); |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1196 | } |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1197 | |
Andi Kleen | d6b7558 | 2009-02-12 13:39:31 +0100 | [diff] [blame] | 1198 | /* Make sure there are no machine checks on offlined CPUs. */ |
H. Peter Anvin | ec5b3d3 | 2009-02-23 14:01:04 -0800 | [diff] [blame] | 1199 | static void mce_disable_cpu(void *h) |
Andi Kleen | d6b7558 | 2009-02-12 13:39:31 +0100 | [diff] [blame] | 1200 | { |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 1201 | unsigned long action = *(unsigned long *)h; |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1202 | int i; |
Andi Kleen | d6b7558 | 2009-02-12 13:39:31 +0100 | [diff] [blame] | 1203 | |
| 1204 | if (!mce_available(¤t_cpu_data)) |
| 1205 | return; |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 1206 | if (!(action & CPU_TASKS_FROZEN)) |
| 1207 | cmci_clear(); |
Andi Kleen | 06b7a7a | 2009-04-27 18:37:43 +0200 | [diff] [blame] | 1208 | for (i = 0; i < banks; i++) { |
| 1209 | if (!skip_bank_init(i)) |
| 1210 | wrmsrl(MSR_IA32_MC0_CTL + i*4, 0); |
| 1211 | } |
Andi Kleen | d6b7558 | 2009-02-12 13:39:31 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
H. Peter Anvin | ec5b3d3 | 2009-02-23 14:01:04 -0800 | [diff] [blame] | 1214 | static void mce_reenable_cpu(void *h) |
Andi Kleen | d6b7558 | 2009-02-12 13:39:31 +0100 | [diff] [blame] | 1215 | { |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 1216 | unsigned long action = *(unsigned long *)h; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1217 | int i; |
Andi Kleen | d6b7558 | 2009-02-12 13:39:31 +0100 | [diff] [blame] | 1218 | |
| 1219 | if (!mce_available(¤t_cpu_data)) |
| 1220 | return; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1221 | |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 1222 | if (!(action & CPU_TASKS_FROZEN)) |
| 1223 | cmci_reenable(); |
Andi Kleen | 06b7a7a | 2009-04-27 18:37:43 +0200 | [diff] [blame] | 1224 | for (i = 0; i < banks; i++) { |
| 1225 | if (!skip_bank_init(i)) |
| 1226 | wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]); |
| 1227 | } |
Andi Kleen | d6b7558 | 2009-02-12 13:39:31 +0100 | [diff] [blame] | 1228 | } |
| 1229 | |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1230 | /* Get notified when a cpu comes on/off. Be hotplug friendly. */ |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1231 | static int __cpuinit |
| 1232 | mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu) |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1233 | { |
| 1234 | unsigned int cpu = (unsigned long)hcpu; |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 1235 | struct timer_list *t = &per_cpu(mce_timer, cpu); |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1236 | |
| 1237 | switch (action) { |
Andreas Herrmann | bae19fe | 2007-11-14 17:00:44 -0800 | [diff] [blame] | 1238 | case CPU_ONLINE: |
| 1239 | case CPU_ONLINE_FROZEN: |
| 1240 | mce_create_device(cpu); |
Rafael J. Wysocki | 8735728 | 2008-08-22 22:23:09 +0200 | [diff] [blame] | 1241 | if (threshold_cpu_callback) |
| 1242 | threshold_cpu_callback(action, cpu); |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1243 | break; |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1244 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1245 | case CPU_DEAD_FROZEN: |
Rafael J. Wysocki | 8735728 | 2008-08-22 22:23:09 +0200 | [diff] [blame] | 1246 | if (threshold_cpu_callback) |
| 1247 | threshold_cpu_callback(action, cpu); |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1248 | mce_remove_device(cpu); |
| 1249 | break; |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 1250 | case CPU_DOWN_PREPARE: |
| 1251 | case CPU_DOWN_PREPARE_FROZEN: |
| 1252 | del_timer_sync(t); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 1253 | smp_call_function_single(cpu, mce_disable_cpu, &action, 1); |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 1254 | break; |
| 1255 | case CPU_DOWN_FAILED: |
| 1256 | case CPU_DOWN_FAILED_FROZEN: |
Andi Kleen | 6298c51 | 2009-04-09 12:28:22 +0200 | [diff] [blame] | 1257 | t->expires = round_jiffies(jiffies + |
| 1258 | __get_cpu_var(next_interval)); |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 1259 | add_timer_on(t, cpu); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 1260 | smp_call_function_single(cpu, mce_reenable_cpu, &action, 1); |
| 1261 | break; |
| 1262 | case CPU_POST_DEAD: |
| 1263 | /* intentionally ignoring frozen here */ |
| 1264 | cmci_rediscover(cpu); |
Andi Kleen | 52d168e | 2009-02-12 13:39:29 +0100 | [diff] [blame] | 1265 | break; |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1266 | } |
Andreas Herrmann | bae19fe | 2007-11-14 17:00:44 -0800 | [diff] [blame] | 1267 | return NOTIFY_OK; |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
Sam Ravnborg | 1e35669 | 2008-01-30 13:33:36 +0100 | [diff] [blame] | 1270 | static struct notifier_block mce_cpu_notifier __cpuinitdata = { |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1271 | .notifier_call = mce_cpu_callback, |
| 1272 | }; |
| 1273 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1274 | static __init int mce_init_banks(void) |
| 1275 | { |
| 1276 | int i; |
| 1277 | |
| 1278 | bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks, |
| 1279 | GFP_KERNEL); |
| 1280 | if (!bank_attrs) |
| 1281 | return -ENOMEM; |
| 1282 | |
| 1283 | for (i = 0; i < banks; i++) { |
| 1284 | struct sysdev_attribute *a = &bank_attrs[i]; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1285 | |
| 1286 | a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i); |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1287 | if (!a->attr.name) |
| 1288 | goto nomem; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1289 | |
| 1290 | a->attr.mode = 0644; |
| 1291 | a->show = show_bank; |
| 1292 | a->store = set_bank; |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1293 | } |
| 1294 | return 0; |
| 1295 | |
| 1296 | nomem: |
| 1297 | while (--i >= 0) |
| 1298 | kfree(bank_attrs[i].attr.name); |
| 1299 | kfree(bank_attrs); |
| 1300 | bank_attrs = NULL; |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1301 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1302 | return -ENOMEM; |
| 1303 | } |
| 1304 | |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1305 | static __init int mce_init_device(void) |
| 1306 | { |
| 1307 | int err; |
| 1308 | int i = 0; |
| 1309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | if (!mce_available(&boot_cpu_data)) |
| 1311 | return -EIO; |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1312 | |
Ingo Molnar | cb491fc | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1313 | alloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL); |
Rusty Russell | 996867d | 2009-03-13 14:49:51 +1030 | [diff] [blame] | 1314 | |
Andi Kleen | 0d7482e3 | 2009-02-17 23:07:13 +0100 | [diff] [blame] | 1315 | err = mce_init_banks(); |
| 1316 | if (err) |
| 1317 | return err; |
| 1318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | err = sysdev_class_register(&mce_sysclass); |
Akinobu Mita | d435d86 | 2007-10-18 03:05:15 -0700 | [diff] [blame] | 1320 | if (err) |
| 1321 | return err; |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1322 | |
| 1323 | for_each_online_cpu(i) { |
Akinobu Mita | d435d86 | 2007-10-18 03:05:15 -0700 | [diff] [blame] | 1324 | err = mce_create_device(i); |
| 1325 | if (err) |
| 1326 | return err; |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Chandra Seetharaman | be6b5a3 | 2006-07-30 03:03:37 -0700 | [diff] [blame] | 1329 | register_hotcpu_notifier(&mce_cpu_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | misc_register(&mce_log_device); |
Ingo Molnar | e9eee03 | 2009-04-08 12:31:17 +0200 | [diff] [blame] | 1331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | } |
Andi Kleen | 91c6d40 | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 1334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | device_initcall(mce_init_device); |
Ingo Molnar | a988d33 | 2009-04-08 12:31:25 +0200 | [diff] [blame] | 1336 | |
Andi Kleen | 4efc067 | 2009-04-28 19:07:31 +0200 | [diff] [blame] | 1337 | #else /* CONFIG_X86_OLD_MCE: */ |
Ingo Molnar | a988d33 | 2009-04-08 12:31:25 +0200 | [diff] [blame] | 1338 | |
Ingo Molnar | a988d33 | 2009-04-08 12:31:25 +0200 | [diff] [blame] | 1339 | int nr_mce_banks; |
| 1340 | EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */ |
| 1341 | |
Ingo Molnar | a988d33 | 2009-04-08 12:31:25 +0200 | [diff] [blame] | 1342 | /* This has to be run for each processor */ |
| 1343 | void mcheck_init(struct cpuinfo_x86 *c) |
| 1344 | { |
| 1345 | if (mce_disabled == 1) |
| 1346 | return; |
| 1347 | |
| 1348 | switch (c->x86_vendor) { |
| 1349 | case X86_VENDOR_AMD: |
| 1350 | amd_mcheck_init(c); |
| 1351 | break; |
| 1352 | |
| 1353 | case X86_VENDOR_INTEL: |
| 1354 | if (c->x86 == 5) |
| 1355 | intel_p5_mcheck_init(c); |
| 1356 | if (c->x86 == 6) |
| 1357 | intel_p6_mcheck_init(c); |
| 1358 | if (c->x86 == 15) |
| 1359 | intel_p4_mcheck_init(c); |
| 1360 | break; |
| 1361 | |
| 1362 | case X86_VENDOR_CENTAUR: |
| 1363 | if (c->x86 == 5) |
| 1364 | winchip_mcheck_init(c); |
| 1365 | break; |
| 1366 | |
| 1367 | default: |
| 1368 | break; |
| 1369 | } |
Ingo Molnar | b659294 | 2009-04-08 12:31:27 +0200 | [diff] [blame] | 1370 | printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks); |
Ingo Molnar | a988d33 | 2009-04-08 12:31:25 +0200 | [diff] [blame] | 1371 | } |
| 1372 | |
Ingo Molnar | a988d33 | 2009-04-08 12:31:25 +0200 | [diff] [blame] | 1373 | static int __init mcheck_enable(char *str) |
| 1374 | { |
| 1375 | mce_disabled = -1; |
| 1376 | return 1; |
| 1377 | } |
| 1378 | |
Ingo Molnar | a988d33 | 2009-04-08 12:31:25 +0200 | [diff] [blame] | 1379 | __setup("mce", mcheck_enable); |
| 1380 | |
Andi Kleen | d7c3c9a | 2009-04-28 23:07:25 +0200 | [diff] [blame] | 1381 | #endif /* CONFIG_X86_OLD_MCE */ |
| 1382 | |
| 1383 | /* |
| 1384 | * Old style boot options parsing. Only for compatibility. |
| 1385 | */ |
| 1386 | static int __init mcheck_disable(char *str) |
| 1387 | { |
| 1388 | mce_disabled = 1; |
| 1389 | return 1; |
| 1390 | } |
| 1391 | __setup("nomce", mcheck_disable); |