2 * Machine check injection support.
3 * Copyright 2008 Intel Corporation.
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
14 * The AMD part (from mce_amd_inj.c): a simple MCE injection facility
15 * for testing different aspects of the RAS code. This driver should be
16 * built as module so that it can be loaded on production kernels for
19 * This file may be distributed under the terms of the GNU General Public
22 * Copyright (c) 2010-17: Borislav Petkov <bp@alien8.de>
23 * Advanced Micro Devices Inc.
26 #include <linux/cpu.h>
27 #include <linux/debugfs.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/notifier.h>
31 #include <linux/pci.h>
32 #include <linux/uaccess.h>
34 #include <asm/amd_nb.h>
36 #include <asm/irq_vectors.h>
44 * Collect all the MCi_XXX settings
46 static struct mce i_mce;
47 static struct dentry *dfs_inj;
51 #define MAX_FLAG_OPT_SIZE 4
55 SW_INJ = 0, /* SW injection, simply decode the error */
56 HW_INJ, /* Trigger a #MC */
57 DFR_INT_INJ, /* Trigger Deferred error interrupt */
58 THR_INT_INJ, /* Trigger threshold interrupt */
62 static const char * const flags_options[] = {
70 /* Set default injection to SW_INJ */
71 static enum injection_type inj_type = SW_INJ;
73 #define MCE_INJECT_SET(reg) \
74 static int inj_##reg##_set(void *data, u64 val) \
76 struct mce *m = (struct mce *)data; \
82 MCE_INJECT_SET(status);
87 #define MCE_INJECT_GET(reg) \
88 static int inj_##reg##_get(void *data, u64 *val) \
90 struct mce *m = (struct mce *)data; \
96 MCE_INJECT_GET(status);
101 DEFINE_SIMPLE_ATTRIBUTE(status_fops, inj_status_get, inj_status_set, "%llx\n");
102 DEFINE_SIMPLE_ATTRIBUTE(misc_fops, inj_misc_get, inj_misc_set, "%llx\n");
103 DEFINE_SIMPLE_ATTRIBUTE(addr_fops, inj_addr_get, inj_addr_set, "%llx\n");
104 DEFINE_SIMPLE_ATTRIBUTE(synd_fops, inj_synd_get, inj_synd_set, "%llx\n");
106 static void setup_inj_struct(struct mce *m)
108 memset(m, 0, sizeof(struct mce));
110 m->cpuvendor = boot_cpu_data.x86_vendor;
111 m->time = ktime_get_real_seconds();
112 m->cpuid = cpuid_eax(1);
113 m->microcode = boot_cpu_data.microcode;
116 /* Update fake mce registers on current CPU. */
117 static void inject_mce(struct mce *m)
119 struct mce *i = &per_cpu(injectm, m->extcpu);
121 /* Make sure no one reads partially written injectm */
125 /* First set the fields after finished */
126 i->extcpu = m->extcpu;
128 /* Now write record in order, finished last (except above) */
129 memcpy(i, m, sizeof(struct mce));
130 /* Finally activate it */
135 static void raise_poll(struct mce *m)
140 memset(&b, 0xff, sizeof(mce_banks_t));
141 local_irq_save(flags);
142 machine_check_poll(0, &b);
143 local_irq_restore(flags);
147 static void raise_exception(struct mce *m, struct pt_regs *pregs)
153 memset(®s, 0, sizeof(struct pt_regs));
158 /* in mcheck exeception handler, irq will be disabled */
159 local_irq_save(flags);
160 do_machine_check(pregs, 0);
161 local_irq_restore(flags);
165 static cpumask_var_t mce_inject_cpumask;
166 static DEFINE_MUTEX(mce_inject_mutex);
168 static int mce_raise_notify(unsigned int cmd, struct pt_regs *regs)
170 int cpu = smp_processor_id();
171 struct mce *m = this_cpu_ptr(&injectm);
172 if (!cpumask_test_cpu(cpu, mce_inject_cpumask))
174 cpumask_clear_cpu(cpu, mce_inject_cpumask);
175 if (m->inject_flags & MCJ_EXCEPTION)
176 raise_exception(m, regs);
182 static void mce_irq_ipi(void *info)
184 int cpu = smp_processor_id();
185 struct mce *m = this_cpu_ptr(&injectm);
187 if (cpumask_test_cpu(cpu, mce_inject_cpumask) &&
188 m->inject_flags & MCJ_EXCEPTION) {
189 cpumask_clear_cpu(cpu, mce_inject_cpumask);
190 raise_exception(m, NULL);
194 /* Inject mce on current CPU */
195 static int raise_local(void)
197 struct mce *m = this_cpu_ptr(&injectm);
198 int context = MCJ_CTX(m->inject_flags);
202 if (m->inject_flags & MCJ_EXCEPTION) {
203 pr_info("Triggering MCE exception on CPU %d\n", cpu);
207 * Could do more to fake interrupts like
208 * calling irq_enter, but the necessary
209 * machinery isn't exported currently.
212 case MCJ_CTX_PROCESS:
213 raise_exception(m, NULL);
216 pr_info("Invalid MCE context\n");
219 pr_info("MCE exception done on CPU %d\n", cpu);
220 } else if (m->status) {
221 pr_info("Starting machine check poll CPU %d\n", cpu);
224 pr_info("Machine check poll done on CPU %d\n", cpu);
231 static void __maybe_unused raise_mce(struct mce *m)
233 int context = MCJ_CTX(m->inject_flags);
237 if (context == MCJ_CTX_RANDOM)
240 if (m->inject_flags & (MCJ_IRQ_BROADCAST | MCJ_NMI_BROADCAST)) {
245 cpumask_copy(mce_inject_cpumask, cpu_online_mask);
246 cpumask_clear_cpu(get_cpu(), mce_inject_cpumask);
247 for_each_online_cpu(cpu) {
248 struct mce *mcpu = &per_cpu(injectm, cpu);
249 if (!mcpu->finished ||
250 MCJ_CTX(mcpu->inject_flags) != MCJ_CTX_RANDOM)
251 cpumask_clear_cpu(cpu, mce_inject_cpumask);
253 if (!cpumask_empty(mce_inject_cpumask)) {
254 if (m->inject_flags & MCJ_IRQ_BROADCAST) {
256 * don't wait because mce_irq_ipi is necessary
257 * to be sync with following raise_local
260 smp_call_function_many(mce_inject_cpumask,
261 mce_irq_ipi, NULL, 0);
263 } else if (m->inject_flags & MCJ_NMI_BROADCAST)
264 apic->send_IPI_mask(mce_inject_cpumask,
268 while (!cpumask_empty(mce_inject_cpumask)) {
269 if (!time_before(jiffies, start + 2*HZ)) {
270 pr_err("Timeout waiting for mce inject %lx\n",
271 *cpumask_bits(mce_inject_cpumask));
286 static int mce_inject_raise(struct notifier_block *nb, unsigned long val,
289 struct mce *m = (struct mce *)data;
294 mutex_lock(&mce_inject_mutex);
296 mutex_unlock(&mce_inject_mutex);
301 static struct notifier_block inject_nb = {
302 .notifier_call = mce_inject_raise,
306 * Caller needs to be make sure this cpu doesn't disappear
307 * from under us, i.e.: get_cpu/put_cpu.
309 static int toggle_hw_mce_inject(unsigned int cpu, bool enable)
314 err = rdmsr_on_cpu(cpu, MSR_K7_HWCR, &l, &h);
316 pr_err("%s: error reading HWCR\n", __func__);
320 enable ? (l |= BIT(18)) : (l &= ~BIT(18));
322 err = wrmsr_on_cpu(cpu, MSR_K7_HWCR, l, h);
324 pr_err("%s: error writing HWCR\n", __func__);
329 static int __set_inj(const char *buf)
333 for (i = 0; i < N_INJ_TYPES; i++) {
334 if (!strncmp(flags_options[i], buf, strlen(flags_options[i]))) {
342 static ssize_t flags_read(struct file *filp, char __user *ubuf,
343 size_t cnt, loff_t *ppos)
345 char buf[MAX_FLAG_OPT_SIZE];
348 n = sprintf(buf, "%s\n", flags_options[inj_type]);
350 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
353 static ssize_t flags_write(struct file *filp, const char __user *ubuf,
354 size_t cnt, loff_t *ppos)
356 char buf[MAX_FLAG_OPT_SIZE], *__buf;
359 if (cnt > MAX_FLAG_OPT_SIZE)
362 if (copy_from_user(&buf, ubuf, cnt))
367 /* strip whitespace */
368 __buf = strstrip(buf);
370 err = __set_inj(__buf);
372 pr_err("%s: Invalid flags value: %s\n", __func__, __buf);
381 static const struct file_operations flags_fops = {
383 .write = flags_write,
384 .llseek = generic_file_llseek,
388 * On which CPU to inject?
390 MCE_INJECT_GET(extcpu);
392 static int inj_extcpu_set(void *data, u64 val)
394 struct mce *m = (struct mce *)data;
396 if (val >= nr_cpu_ids || !cpu_online(val)) {
397 pr_err("%s: Invalid CPU: %llu\n", __func__, val);
404 DEFINE_SIMPLE_ATTRIBUTE(extcpu_fops, inj_extcpu_get, inj_extcpu_set, "%llu\n");
406 static void trigger_mce(void *info)
408 asm volatile("int $18");
411 static void trigger_dfr_int(void *info)
413 asm volatile("int %0" :: "i" (DEFERRED_ERROR_VECTOR));
416 static void trigger_thr_int(void *info)
418 asm volatile("int %0" :: "i" (THRESHOLD_APIC_VECTOR));
421 static u32 get_nbc_for_node(int node_id)
423 struct cpuinfo_x86 *c = &boot_cpu_data;
426 cores_per_node = (c->x86_max_cores * smp_num_siblings) / amd_get_nodes_per_socket();
428 return cores_per_node * node_id;
431 static void toggle_nb_mca_mst_cpu(u16 nid)
433 struct amd_northbridge *nb;
438 nb = node_to_amd_nb(nid);
446 err = pci_read_config_dword(F3, NBCFG, &val);
448 pr_err("%s: Error reading F%dx%03x.\n",
449 __func__, PCI_FUNC(F3->devfn), NBCFG);
456 pr_err("%s: Set D18F3x44[NbMcaToMstCpuEn] which BIOS hasn't done.\n",
460 err = pci_write_config_dword(F3, NBCFG, val);
462 pr_err("%s: Error writing F%dx%03x.\n",
463 __func__, PCI_FUNC(F3->devfn), NBCFG);
466 static void prepare_msrs(void *info)
468 struct mce m = *(struct mce *)info;
471 wrmsrl(MSR_IA32_MCG_STATUS, m.mcgstatus);
473 if (boot_cpu_has(X86_FEATURE_SMCA)) {
474 if (m.inject_flags == DFR_INT_INJ) {
475 wrmsrl(MSR_AMD64_SMCA_MCx_DESTAT(b), m.status);
476 wrmsrl(MSR_AMD64_SMCA_MCx_DEADDR(b), m.addr);
478 wrmsrl(MSR_AMD64_SMCA_MCx_STATUS(b), m.status);
479 wrmsrl(MSR_AMD64_SMCA_MCx_ADDR(b), m.addr);
482 wrmsrl(MSR_AMD64_SMCA_MCx_MISC(b), m.misc);
483 wrmsrl(MSR_AMD64_SMCA_MCx_SYND(b), m.synd);
485 wrmsrl(MSR_IA32_MCx_STATUS(b), m.status);
486 wrmsrl(MSR_IA32_MCx_ADDR(b), m.addr);
487 wrmsrl(MSR_IA32_MCx_MISC(b), m.misc);
491 static void do_inject(void)
494 unsigned int cpu = i_mce.extcpu;
497 i_mce.tsc = rdtsc_ordered();
500 i_mce.status |= MCI_STATUS_MISCV;
503 i_mce.status |= MCI_STATUS_SYNDV;
505 if (inj_type == SW_INJ) {
506 mce_inject_log(&i_mce);
510 /* prep MCE global settings for the injection */
511 mcg_status = MCG_STATUS_MCIP | MCG_STATUS_EIPV;
513 if (!(i_mce.status & MCI_STATUS_PCC))
514 mcg_status |= MCG_STATUS_RIPV;
517 * Ensure necessary status bits for deferred errors:
518 * - MCx_STATUS[Deferred]: make sure it is a deferred error
519 * - MCx_STATUS[UC] cleared: deferred errors are _not_ UC
521 if (inj_type == DFR_INT_INJ) {
522 i_mce.status |= MCI_STATUS_DEFERRED;
523 i_mce.status |= (i_mce.status & ~MCI_STATUS_UC);
527 * For multi node CPUs, logging and reporting of bank 4 errors happens
528 * only on the node base core. Refer to D18F3x44[NbMcaToMstCpuEn] for
529 * Fam10h and later BKDGs.
531 if (static_cpu_has(X86_FEATURE_AMD_DCM) &&
533 boot_cpu_data.x86 < 0x17) {
534 toggle_nb_mca_mst_cpu(amd_get_nb_id(cpu));
535 cpu = get_nbc_for_node(amd_get_nb_id(cpu));
539 if (!cpu_online(cpu))
542 toggle_hw_mce_inject(cpu, true);
544 i_mce.mcgstatus = mcg_status;
545 i_mce.inject_flags = inj_type;
546 smp_call_function_single(cpu, prepare_msrs, &i_mce, 0);
548 toggle_hw_mce_inject(cpu, false);
552 smp_call_function_single(cpu, trigger_dfr_int, NULL, 0);
555 smp_call_function_single(cpu, trigger_thr_int, NULL, 0);
558 smp_call_function_single(cpu, trigger_mce, NULL, 0);
567 * This denotes into which bank we're injecting and triggers
568 * the injection, at the same time.
570 static int inj_bank_set(void *data, u64 val)
572 struct mce *m = (struct mce *)data;
574 if (val >= n_banks) {
575 pr_err("Non-existent MCE bank: %llu\n", val);
582 /* Reset injection struct */
583 setup_inj_struct(&i_mce);
588 MCE_INJECT_GET(bank);
590 DEFINE_SIMPLE_ATTRIBUTE(bank_fops, inj_bank_get, inj_bank_set, "%llu\n");
592 static const char readme_msg[] =
593 "Description of the files and their usages:\n"
595 "Note1: i refers to the bank number below.\n"
596 "Note2: See respective BKDGs for the exact bit definitions of the files below\n"
597 "as they mirror the hardware registers.\n"
599 "status:\t Set MCi_STATUS: the bits in that MSR control the error type and\n"
600 "\t attributes of the error which caused the MCE.\n"
602 "misc:\t Set MCi_MISC: provide auxiliary info about the error. It is mostly\n"
603 "\t used for error thresholding purposes and its validity is indicated by\n"
604 "\t MCi_STATUS[MiscV].\n"
606 "synd:\t Set MCi_SYND: provide syndrome info about the error. Only valid on\n"
607 "\t Scalable MCA systems, and its validity is indicated by MCi_STATUS[SyndV].\n"
609 "addr:\t Error address value to be written to MCi_ADDR. Log address information\n"
610 "\t associated with the error.\n"
612 "cpu:\t The CPU to inject the error on.\n"
614 "bank:\t Specify the bank you want to inject the error into: the number of\n"
615 "\t banks in a processor varies and is family/model-specific, therefore, the\n"
616 "\t supplied value is sanity-checked. Setting the bank value also triggers the\n"
619 "flags:\t Injection type to be performed. Writing to this file will trigger a\n"
620 "\t real machine check, an APIC interrupt or invoke the error decoder routines\n"
621 "\t for AMD processors.\n"
623 "\t Allowed error injection types:\n"
624 "\t - \"sw\": Software error injection. Decode error to a human-readable \n"
625 "\t format only. Safe to use.\n"
626 "\t - \"hw\": Hardware error injection. Causes the #MC exception handler to \n"
627 "\t handle the error. Be warned: might cause system panic if MCi_STATUS[PCC] \n"
628 "\t is set. Therefore, consider setting (debugfs_mountpoint)/mce/fake_panic \n"
629 "\t before injecting.\n"
630 "\t - \"df\": Trigger APIC interrupt for Deferred error. Causes deferred \n"
631 "\t error APIC interrupt handler to handle the error if the feature is \n"
632 "\t is present in hardware. \n"
633 "\t - \"th\": Trigger APIC interrupt for Threshold errors. Causes threshold \n"
634 "\t APIC interrupt handler to handle the error. \n"
638 inj_readme_read(struct file *filp, char __user *ubuf,
639 size_t cnt, loff_t *ppos)
641 return simple_read_from_buffer(ubuf, cnt, ppos,
642 readme_msg, strlen(readme_msg));
645 static const struct file_operations readme_fops = {
646 .read = inj_readme_read,
649 static struct dfs_node {
652 const struct file_operations *fops;
655 { .name = "status", .fops = &status_fops, .perm = S_IRUSR | S_IWUSR },
656 { .name = "misc", .fops = &misc_fops, .perm = S_IRUSR | S_IWUSR },
657 { .name = "addr", .fops = &addr_fops, .perm = S_IRUSR | S_IWUSR },
658 { .name = "synd", .fops = &synd_fops, .perm = S_IRUSR | S_IWUSR },
659 { .name = "bank", .fops = &bank_fops, .perm = S_IRUSR | S_IWUSR },
660 { .name = "flags", .fops = &flags_fops, .perm = S_IRUSR | S_IWUSR },
661 { .name = "cpu", .fops = &extcpu_fops, .perm = S_IRUSR | S_IWUSR },
662 { .name = "README", .fops = &readme_fops, .perm = S_IRUSR | S_IRGRP | S_IROTH },
665 static int __init debugfs_init(void)
670 rdmsrl(MSR_IA32_MCG_CAP, cap);
671 n_banks = cap & MCG_BANKCNT_MASK;
673 dfs_inj = debugfs_create_dir("mce-inject", NULL);
677 for (i = 0; i < ARRAY_SIZE(dfs_fls); i++) {
678 dfs_fls[i].d = debugfs_create_file(dfs_fls[i].name,
692 debugfs_remove(dfs_fls[i].d);
694 debugfs_remove(dfs_inj);
700 static int __init inject_init(void)
704 if (!alloc_cpumask_var(&mce_inject_cpumask, GFP_KERNEL))
707 err = debugfs_init();
709 free_cpumask_var(mce_inject_cpumask);
713 register_nmi_handler(NMI_LOCAL, mce_raise_notify, 0, "mce_notify");
714 mce_register_injector_chain(&inject_nb);
716 setup_inj_struct(&i_mce);
718 pr_info("Machine check injector initialized\n");
723 static void __exit inject_exit(void)
726 mce_unregister_injector_chain(&inject_nb);
727 unregister_nmi_handler(NMI_LOCAL, "mce_notify");
729 debugfs_remove_recursive(dfs_inj);
732 memset(&dfs_fls, 0, sizeof(dfs_fls));
734 free_cpumask_var(mce_inject_cpumask);
737 module_init(inject_init);
738 module_exit(inject_exit);
739 MODULE_LICENSE("GPL");