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>
23 /* Update fake mce registers on current CPU. */
24 static void inject_mce(struct mce
*m
)
26 struct mce
*i
= &per_cpu(injectm
, m
->extcpu
);
28 /* Make sure noone reads partially written injectm */
32 /* First set the fields after finished */
33 i
->extcpu
= m
->extcpu
;
35 /* Now write record in order, finished last (except above) */
36 memcpy(i
, m
, sizeof(struct mce
));
37 /* Finally activate it */
43 struct timer_list timer
;
47 /* Inject mce on current CPU */
48 static void raise_mce(unsigned long data
)
50 struct delayed_mce
*dm
= (struct delayed_mce
*)data
;
51 struct mce
*m
= &dm
->m
;
55 if (m
->status
& MCI_STATUS_UC
) {
57 memset(®s
, 0, sizeof(struct pt_regs
));
60 printk(KERN_INFO
"Triggering MCE exception on CPU %d\n", cpu
);
61 do_machine_check(®s
, 0);
62 printk(KERN_INFO
"MCE exception done on CPU %d\n", cpu
);
65 memset(&b
, 0xff, sizeof(mce_banks_t
));
66 printk(KERN_INFO
"Starting machine check poll CPU %d\n", cpu
);
67 machine_check_poll(0, &b
);
69 printk(KERN_INFO
"Finished machine check poll on CPU %d\n",
75 /* Error injection interface */
76 static ssize_t
mce_write(struct file
*filp
, const char __user
*ubuf
,
77 size_t usize
, loff_t
*off
)
79 struct delayed_mce
*dm
;
82 if (!capable(CAP_SYS_ADMIN
))
85 * There are some cases where real MSR reads could slip
88 if (!boot_cpu_has(X86_FEATURE_MCE
) || !boot_cpu_has(X86_FEATURE_MCA
))
91 if ((unsigned long)usize
> sizeof(struct mce
))
92 usize
= sizeof(struct mce
);
93 if (copy_from_user(&m
, ubuf
, usize
))
96 if (m
.extcpu
>= num_possible_cpus() || !cpu_online(m
.extcpu
))
99 dm
= kmalloc(sizeof(struct delayed_mce
), GFP_KERNEL
);
104 * Need to give user space some time to set everything up,
105 * so do it a jiffie or two later everywhere.
106 * Should we use a hrtimer here for better synchronization?
108 memcpy(&dm
->m
, &m
, sizeof(struct mce
));
109 setup_timer(&dm
->timer
, raise_mce
, (unsigned long)dm
);
110 dm
->timer
.expires
= jiffies
+ 2;
111 add_timer_on(&dm
->timer
, m
.extcpu
);
115 static int inject_init(void)
117 printk(KERN_INFO
"Machine check injector initialized\n");
118 mce_chrdev_ops
.write
= mce_write
;
122 module_init(inject_init
);
124 * Cannot tolerate unloading currently because we cannot
125 * guarantee all openers of mce_chrdev will get a reference to us.
127 MODULE_LICENSE("GPL");