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 #include <linux/uaccess.h>
15 #include <linux/module.h>
16 #include <linux/timer.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
20 #include <linux/smp.h>
21 #include <linux/notifier.h>
22 #include <linux/kdebug.h>
23 #include <linux/cpu.h>
24 #include <linux/sched.h>
28 /* Update fake mce registers on current CPU. */
29 static void inject_mce(struct mce *m)
31 struct mce *i = &per_cpu(injectm, m->extcpu);
33 /* Make sure noone reads partially written injectm */
37 /* First set the fields after finished */
38 i->extcpu = m->extcpu;
40 /* Now write record in order, finished last (except above) */
41 memcpy(i, m, sizeof(struct mce));
42 /* Finally activate it */
47 static void raise_poll(struct mce *m)
52 memset(&b, 0xff, sizeof(mce_banks_t));
53 local_irq_save(flags);
54 machine_check_poll(0, &b);
55 local_irq_restore(flags);
59 static void raise_exception(struct mce *m, struct pt_regs *pregs)
65 memset(®s, 0, sizeof(struct pt_regs));
70 /* in mcheck exeception handler, irq will be disabled */
71 local_irq_save(flags);
72 do_machine_check(pregs, 0);
73 local_irq_restore(flags);
77 static cpumask_t mce_inject_cpumask;
79 static int mce_raise_notify(struct notifier_block *self,
80 unsigned long val, void *data)
82 struct die_args *args = (struct die_args *)data;
83 int cpu = smp_processor_id();
84 struct mce *m = &__get_cpu_var(injectm);
85 if (val != DIE_NMI_IPI || !cpu_isset(cpu, mce_inject_cpumask))
87 cpu_clear(cpu, mce_inject_cpumask);
88 if (m->inject_flags & MCJ_EXCEPTION)
89 raise_exception(m, args->regs);
95 static struct notifier_block mce_raise_nb = {
96 .notifier_call = mce_raise_notify,
100 /* Inject mce on current CPU */
101 static int raise_local(void)
103 struct mce *m = &__get_cpu_var(injectm);
104 int context = MCJ_CTX(m->inject_flags);
108 if (m->inject_flags & MCJ_EXCEPTION) {
109 printk(KERN_INFO "Triggering MCE exception on CPU %d\n", cpu);
113 * Could do more to fake interrupts like
114 * calling irq_enter, but the necessary
115 * machinery isn't exported currently.
118 case MCJ_CTX_PROCESS:
119 raise_exception(m, NULL);
122 printk(KERN_INFO "Invalid MCE context\n");
125 printk(KERN_INFO "MCE exception done on CPU %d\n", cpu);
126 } else if (m->status) {
127 printk(KERN_INFO "Starting machine check poll CPU %d\n", cpu);
130 printk(KERN_INFO "Machine check poll done on CPU %d\n", cpu);
137 static void raise_mce(struct mce *m)
139 int context = MCJ_CTX(m->inject_flags);
143 if (context == MCJ_CTX_RANDOM)
146 #ifdef CONFIG_X86_LOCAL_APIC
147 if (m->inject_flags & MCJ_NMI_BROADCAST) {
151 mce_inject_cpumask = cpu_online_map;
152 cpu_clear(get_cpu(), mce_inject_cpumask);
153 for_each_online_cpu(cpu) {
154 struct mce *mcpu = &per_cpu(injectm, cpu);
155 if (!mcpu->finished ||
156 MCJ_CTX(mcpu->inject_flags) != MCJ_CTX_RANDOM)
157 cpu_clear(cpu, mce_inject_cpumask);
159 if (!cpus_empty(mce_inject_cpumask))
160 apic->send_IPI_mask(&mce_inject_cpumask, NMI_VECTOR);
162 while (!cpus_empty(mce_inject_cpumask)) {
163 if (!time_before(jiffies, start + 2*HZ)) {
165 "Timeout waiting for mce inject NMI %lx\n",
166 *cpus_addr(mce_inject_cpumask));
179 /* Error injection interface */
180 static ssize_t mce_write(struct file *filp, const char __user *ubuf,
181 size_t usize, loff_t *off)
185 if (!capable(CAP_SYS_ADMIN))
188 * There are some cases where real MSR reads could slip
191 if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
194 if ((unsigned long)usize > sizeof(struct mce))
195 usize = sizeof(struct mce);
196 if (copy_from_user(&m, ubuf, usize))
199 if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
203 * Need to give user space some time to set everything up,
204 * so do it a jiffie or two later everywhere.
211 static int inject_init(void)
213 printk(KERN_INFO "Machine check injector initialized\n");
214 mce_chrdev_ops.write = mce_write;
215 register_die_notifier(&mce_raise_nb);
219 module_init(inject_init);
221 * Cannot tolerate unloading currently because we cannot
222 * guarantee all openers of mce_chrdev will get a reference to us.
224 MODULE_LICENSE("GPL");