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(struct mce *m)
103 int context = MCJ_CTX(m->inject_flags);
107 if (m->inject_flags & MCJ_EXCEPTION) {
108 printk(KERN_INFO "Triggering MCE exception on CPU %d\n", cpu);
112 * Could do more to fake interrupts like
113 * calling irq_enter, but the necessary
114 * machinery isn't exported currently.
117 case MCJ_CTX_PROCESS:
118 raise_exception(m, NULL);
121 printk(KERN_INFO "Invalid MCE context\n");
124 printk(KERN_INFO "MCE exception done on CPU %d\n", cpu);
125 } else if (m->status) {
126 printk(KERN_INFO "Starting machine check poll CPU %d\n", cpu);
129 printk(KERN_INFO "Machine check poll done on CPU %d\n", cpu);
136 static void raise_mce(struct mce *m)
138 int context = MCJ_CTX(m->inject_flags);
142 if (context == MCJ_CTX_RANDOM)
145 #ifdef CONFIG_X86_LOCAL_APIC
146 if (m->inject_flags & MCJ_NMI_BROADCAST) {
150 mce_inject_cpumask = cpu_online_map;
151 cpu_clear(get_cpu(), mce_inject_cpumask);
152 for_each_online_cpu(cpu) {
153 struct mce *mcpu = &per_cpu(injectm, cpu);
154 if (!mcpu->finished ||
155 MCJ_CTX(mcpu->inject_flags) != MCJ_CTX_RANDOM)
156 cpu_clear(cpu, mce_inject_cpumask);
158 if (!cpus_empty(mce_inject_cpumask))
159 apic->send_IPI_mask(&mce_inject_cpumask, NMI_VECTOR);
161 while (!cpus_empty(mce_inject_cpumask)) {
162 if (!time_before(jiffies, start + 2*HZ)) {
164 "Timeout waiting for mce inject NMI %lx\n",
165 *cpus_addr(mce_inject_cpumask));
178 /* Error injection interface */
179 static ssize_t mce_write(struct file *filp, const char __user *ubuf,
180 size_t usize, loff_t *off)
184 if (!capable(CAP_SYS_ADMIN))
187 * There are some cases where real MSR reads could slip
190 if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
193 if ((unsigned long)usize > sizeof(struct mce))
194 usize = sizeof(struct mce);
195 if (copy_from_user(&m, ubuf, usize))
198 if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
202 * Need to give user space some time to set everything up,
203 * so do it a jiffie or two later everywhere.
210 static int inject_init(void)
212 printk(KERN_INFO "Machine check injector initialized\n");
213 mce_chrdev_ops.write = mce_write;
214 register_die_notifier(&mce_raise_nb);
218 module_init(inject_init);
220 * Cannot tolerate unloading currently because we cannot
221 * guarantee all openers of mce_chrdev will get a reference to us.
223 MODULE_LICENSE("GPL");