1 /*
2 * MCE grading rules.
3 * Copyright 2008, 2009 Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; version 2
8 * of the License.
9 *
10 * Author: Andi Kleen
11 */
12 #include <linux/kernel.h>
13 #include <linux/seq_file.h>
14 #include <linux/init.h>
15 #include <linux/debugfs.h>
16 #include <asm/mce.h>
17 #include <linux/uaccess.h>
18
19 #include "mce-internal.h"
20
21 /*
22 * Grade an mce by severity. In general the most severe ones are processed
23 * first. Since there are quite a lot of combinations test the bits in a
24 * table-driven way. The rules are simply processed in order, first
25 * match wins.
26 *
27 * Note this is only used for machine check exceptions, the corrected
28 * errors use much simpler rules. The exceptions still check for the corrected
29 * errors, but only to leave them alone for the CMCI handler (except for
30 * panic situations)
31 */
32
33 enum context { IN_KERNEL = 1, IN_USER = 2, IN_KERNEL_RECOV = 3 };
34 enum ser { SER_REQUIRED = 1, NO_SER = 2 };
35 enum exception { EXCP_CONTEXT = 1, NO_EXCP = 2 };
36
37 static struct severity {
38 u64 mask;
39 u64 result;
40 unsigned char sev;
41 unsigned char mcgmask;
42 unsigned char mcgres;
43 unsigned char ser;
44 unsigned char context;
45 unsigned char excp;
46 unsigned char covered;
47 char *msg;
48 } severities[] = {
49 #define MCESEV(s, m, c...) { .sev = MCE_ ## s ## _SEVERITY, .msg = m, ## c }
50 #define KERNEL .context = IN_KERNEL
51 #define USER .context = IN_USER
52 #define KERNEL_RECOV .context = IN_KERNEL_RECOV
53 #define SER .ser = SER_REQUIRED
54 #define NOSER .ser = NO_SER
55 #define EXCP .excp = EXCP_CONTEXT
56 #define NOEXCP .excp = NO_EXCP
57 #define BITCLR(x) .mask = x, .result = 0
58 #define BITSET(x) .mask = x, .result = x
59 #define MCGMASK(x, y) .mcgmask = x, .mcgres = y
60 #define MASK(x, y) .mask = x, .result = y
61 #define MCI_UC_S (MCI_STATUS_UC|MCI_STATUS_S)
62 #define MCI_UC_SAR (MCI_STATUS_UC|MCI_STATUS_S|MCI_STATUS_AR)
63 #define MCI_ADDR (MCI_STATUS_ADDRV|MCI_STATUS_MISCV)
64
65 MCESEV(
66 NO, "Invalid",
67 BITCLR(MCI_STATUS_VAL)
68 ),
69 MCESEV(
70 NO, "Not enabled",
71 EXCP, BITCLR(MCI_STATUS_EN)
72 ),
73 MCESEV(
74 PANIC, "Processor context corrupt",
75 BITSET(MCI_STATUS_PCC)
76 ),
77 /* When MCIP is not set something is very confused */
78 MCESEV(
79 PANIC, "MCIP not set in MCA handler",
80 EXCP, MCGMASK(MCG_STATUS_MCIP, 0)
81 ),
82 /* Neither return not error IP -- no chance to recover -> PANIC */
83 MCESEV(
84 PANIC, "Neither restart nor error IP",
85 EXCP, MCGMASK(MCG_STATUS_RIPV|MCG_STATUS_EIPV, 0)
86 ),
87 MCESEV(
88 PANIC, "In kernel and no restart IP",
89 EXCP, KERNEL, MCGMASK(MCG_STATUS_RIPV, 0)
90 ),
91 MCESEV(
92 PANIC, "In kernel and no restart IP",
93 EXCP, KERNEL_RECOV, MCGMASK(MCG_STATUS_RIPV, 0)
94 ),
95 MCESEV(
96 DEFERRED, "Deferred error",
97 NOSER, MASK(MCI_STATUS_UC|MCI_STATUS_DEFERRED|MCI_STATUS_POISON, MCI_STATUS_DEFERRED)
98 ),
99 MCESEV(
100 KEEP, "Corrected error",
101 NOSER, BITCLR(MCI_STATUS_UC)
102 ),
103
104 /* ignore OVER for UCNA */
105 MCESEV(
106 UCNA, "Uncorrected no action required",
107 SER, MASK(MCI_UC_SAR, MCI_STATUS_UC)
108 ),
109 MCESEV(
110 PANIC, "Illegal combination (UCNA with AR=1)",
111 SER,
112 MASK(MCI_STATUS_OVER|MCI_UC_SAR, MCI_STATUS_UC|MCI_STATUS_AR)
113 ),
114 MCESEV(
115 KEEP, "Non signalled machine check",
116 SER, BITCLR(MCI_STATUS_S)
117 ),
118
119 MCESEV(
120 PANIC, "Action required with lost events",
121 SER, BITSET(MCI_STATUS_OVER|MCI_UC_SAR)
122 ),
123
124 /* known AR MCACODs: */
125 #ifdef CONFIG_MEMORY_FAILURE
126 MCESEV(
127 KEEP, "Action required but unaffected thread is continuable",
128 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR, MCI_UC_SAR|MCI_ADDR),
129 MCGMASK(MCG_STATUS_RIPV|MCG_STATUS_EIPV, MCG_STATUS_RIPV)
130 ),
131 MCESEV(
132 AR, "Action required: data load in error recoverable area of kernel",
133 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR|MCACOD, MCI_UC_SAR|MCI_ADDR|MCACOD_DATA),
134 KERNEL_RECOV
135 ),
136 MCESEV(
137 AR, "Action required: data load error in a user process",
138 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR|MCACOD, MCI_UC_SAR|MCI_ADDR|MCACOD_DATA),
139 USER
140 ),
141 MCESEV(
142 AR, "Action required: instruction fetch error in a user process",
143 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR|MCACOD, MCI_UC_SAR|MCI_ADDR|MCACOD_INSTR),
144 USER
145 ),
146 MCESEV(
147 PANIC, "Data load in unrecoverable area of kernel",
148 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR|MCACOD, MCI_UC_SAR|MCI_ADDR|MCACOD_DATA),
149 KERNEL
150 ),
151 MCESEV(
152 PANIC, "Instruction fetch error in kernel",
153 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR|MCACOD, MCI_UC_SAR|MCI_ADDR|MCACOD_INSTR),
154 KERNEL
155 ),
156 #endif
157 MCESEV(
158 PANIC, "Action required: unknown MCACOD",
159 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR, MCI_UC_SAR)
160 ),
161
162 /* known AO MCACODs: */
163 MCESEV(
164 AO, "Action optional: memory scrubbing error",
165 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCACOD_SCRUBMSK, MCI_UC_S|MCACOD_SCRUB)
166 ),
167 MCESEV(
168 AO, "Action optional: last level cache writeback error",
169 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCACOD, MCI_UC_S|MCACOD_L3WB)
170 ),
171 MCESEV(
172 SOME, "Action optional: unknown MCACOD",
173 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR, MCI_UC_S)
174 ),
175 MCESEV(
176 SOME, "Action optional with lost events",
177 SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR, MCI_STATUS_OVER|MCI_UC_S)
178 ),
179
180 MCESEV(
181 PANIC, "Overflowed uncorrected",
182 BITSET(MCI_STATUS_OVER|MCI_STATUS_UC)
183 ),
184 MCESEV(
185 UC, "Uncorrected",
186 BITSET(MCI_STATUS_UC)
187 ),
188 MCESEV(
189 SOME, "No match",
190 BITSET(0)
191 ) /* always matches. keep at end */
192 };
193
194 #define mc_recoverable(mcg) (((mcg) & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) == \
195 (MCG_STATUS_RIPV|MCG_STATUS_EIPV))
196
197 /*
198 * If mcgstatus indicated that ip/cs on the stack were
199 * no good, then "m->cs" will be zero and we will have
200 * to assume the worst case (IN_KERNEL) as we actually
201 * have no idea what we were executing when the machine
202 * check hit.
203 * If we do have a good "m->cs" (or a faked one in the
204 * case we were executing in VM86 mode) we can use it to
205 * distinguish an exception taken in user from from one
206 * taken in the kernel.
207 */
error_context(struct mce * m)208 static int error_context(struct mce *m)
209 {
210 if ((m->cs & 3) == 3)
211 return IN_USER;
212 if (mc_recoverable(m->mcgstatus) && ex_has_fault_handler(m->ip))
213 return IN_KERNEL_RECOV;
214 return IN_KERNEL;
215 }
216
mce_severity_amd_smca(struct mce * m,int err_ctx)217 static int mce_severity_amd_smca(struct mce *m, int err_ctx)
218 {
219 u32 addr = MSR_AMD64_SMCA_MCx_CONFIG(m->bank);
220 u32 low, high;
221
222 /*
223 * We need to look at the following bits:
224 * - "succor" bit (data poisoning support), and
225 * - TCC bit (Task Context Corrupt)
226 * in MCi_STATUS to determine error severity.
227 */
228 if (!mce_flags.succor)
229 return MCE_PANIC_SEVERITY;
230
231 if (rdmsr_safe(addr, &low, &high))
232 return MCE_PANIC_SEVERITY;
233
234 /* TCC (Task context corrupt). If set and if IN_KERNEL, panic. */
235 if ((low & MCI_CONFIG_MCAX) &&
236 (m->status & MCI_STATUS_TCC) &&
237 (err_ctx == IN_KERNEL))
238 return MCE_PANIC_SEVERITY;
239
240 /* ...otherwise invoke hwpoison handler. */
241 return MCE_AR_SEVERITY;
242 }
243
244 /*
245 * See AMD Error Scope Hierarchy table in a newer BKDG. For example
246 * 49125_15h_Models_30h-3Fh_BKDG.pdf, section "RAS Features"
247 */
mce_severity_amd(struct mce * m,int tolerant,char ** msg,bool is_excp)248 static int mce_severity_amd(struct mce *m, int tolerant, char **msg, bool is_excp)
249 {
250 enum context ctx = error_context(m);
251
252 /* Processor Context Corrupt, no need to fumble too much, die! */
253 if (m->status & MCI_STATUS_PCC)
254 return MCE_PANIC_SEVERITY;
255
256 if (m->status & MCI_STATUS_UC) {
257
258 if (ctx == IN_KERNEL)
259 return MCE_PANIC_SEVERITY;
260
261 /*
262 * On older systems where overflow_recov flag is not present, we
263 * should simply panic if an error overflow occurs. If
264 * overflow_recov flag is present and set, then software can try
265 * to at least kill process to prolong system operation.
266 */
267 if (mce_flags.overflow_recov) {
268 if (mce_flags.smca)
269 return mce_severity_amd_smca(m, ctx);
270
271 /* kill current process */
272 return MCE_AR_SEVERITY;
273 } else {
274 /* at least one error was not logged */
275 if (m->status & MCI_STATUS_OVER)
276 return MCE_PANIC_SEVERITY;
277 }
278
279 /*
280 * For any other case, return MCE_UC_SEVERITY so that we log the
281 * error and exit #MC handler.
282 */
283 return MCE_UC_SEVERITY;
284 }
285
286 /*
287 * deferred error: poll handler catches these and adds to mce_ring so
288 * memory-failure can take recovery actions.
289 */
290 if (m->status & MCI_STATUS_DEFERRED)
291 return MCE_DEFERRED_SEVERITY;
292
293 /*
294 * corrected error: poll handler catches these and passes responsibility
295 * of decoding the error to EDAC
296 */
297 return MCE_KEEP_SEVERITY;
298 }
299
mce_severity_intel(struct mce * m,int tolerant,char ** msg,bool is_excp)300 static int mce_severity_intel(struct mce *m, int tolerant, char **msg, bool is_excp)
301 {
302 enum exception excp = (is_excp ? EXCP_CONTEXT : NO_EXCP);
303 enum context ctx = error_context(m);
304 struct severity *s;
305
306 for (s = severities;; s++) {
307 if ((m->status & s->mask) != s->result)
308 continue;
309 if ((m->mcgstatus & s->mcgmask) != s->mcgres)
310 continue;
311 if (s->ser == SER_REQUIRED && !mca_cfg.ser)
312 continue;
313 if (s->ser == NO_SER && mca_cfg.ser)
314 continue;
315 if (s->context && ctx != s->context)
316 continue;
317 if (s->excp && excp != s->excp)
318 continue;
319 if (msg)
320 *msg = s->msg;
321 s->covered = 1;
322 if (s->sev >= MCE_UC_SEVERITY && ctx == IN_KERNEL) {
323 if (tolerant < 1)
324 return MCE_PANIC_SEVERITY;
325 }
326 return s->sev;
327 }
328 }
329
330 /* Default to mce_severity_intel */
331 int (*mce_severity)(struct mce *m, int tolerant, char **msg, bool is_excp) =
332 mce_severity_intel;
333
mcheck_vendor_init_severity(void)334 void __init mcheck_vendor_init_severity(void)
335 {
336 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
337 mce_severity = mce_severity_amd;
338 }
339
340 #ifdef CONFIG_DEBUG_FS
s_start(struct seq_file * f,loff_t * pos)341 static void *s_start(struct seq_file *f, loff_t *pos)
342 {
343 if (*pos >= ARRAY_SIZE(severities))
344 return NULL;
345 return &severities[*pos];
346 }
347
s_next(struct seq_file * f,void * data,loff_t * pos)348 static void *s_next(struct seq_file *f, void *data, loff_t *pos)
349 {
350 if (++(*pos) >= ARRAY_SIZE(severities))
351 return NULL;
352 return &severities[*pos];
353 }
354
s_stop(struct seq_file * f,void * data)355 static void s_stop(struct seq_file *f, void *data)
356 {
357 }
358
s_show(struct seq_file * f,void * data)359 static int s_show(struct seq_file *f, void *data)
360 {
361 struct severity *ser = data;
362 seq_printf(f, "%d\t%s\n", ser->covered, ser->msg);
363 return 0;
364 }
365
366 static const struct seq_operations severities_seq_ops = {
367 .start = s_start,
368 .next = s_next,
369 .stop = s_stop,
370 .show = s_show,
371 };
372
severities_coverage_open(struct inode * inode,struct file * file)373 static int severities_coverage_open(struct inode *inode, struct file *file)
374 {
375 return seq_open(file, &severities_seq_ops);
376 }
377
severities_coverage_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)378 static ssize_t severities_coverage_write(struct file *file,
379 const char __user *ubuf,
380 size_t count, loff_t *ppos)
381 {
382 int i;
383 for (i = 0; i < ARRAY_SIZE(severities); i++)
384 severities[i].covered = 0;
385 return count;
386 }
387
388 static const struct file_operations severities_coverage_fops = {
389 .open = severities_coverage_open,
390 .release = seq_release,
391 .read = seq_read,
392 .write = severities_coverage_write,
393 .llseek = seq_lseek,
394 };
395
severities_debugfs_init(void)396 static int __init severities_debugfs_init(void)
397 {
398 struct dentry *dmce, *fsev;
399
400 dmce = mce_get_debugfs_dir();
401 if (!dmce)
402 goto err_out;
403
404 fsev = debugfs_create_file("severities-coverage", 0444, dmce, NULL,
405 &severities_coverage_fops);
406 if (!fsev)
407 goto err_out;
408
409 return 0;
410
411 err_out:
412 return -ENOMEM;
413 }
414 late_initcall(severities_debugfs_init);
415 #endif /* CONFIG_DEBUG_FS */
416