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/preempt.h>
21 #include <linux/smp.h>
22 #include <linux/notifier.h>
23 #include <linux/kdebug.h>
24 #include <linux/cpu.h>
25 #include <linux/sched.h>
26 #include <linux/gfp.h>
31 /* Update fake mce registers on current CPU. */
32 static void inject_mce(struct mce
*m
)
34 struct mce
*i
= &per_cpu(injectm
, m
->extcpu
);
36 /* Make sure no one reads partially written injectm */
40 /* First set the fields after finished */
41 i
->extcpu
= m
->extcpu
;
43 /* Now write record in order, finished last (except above) */
44 memcpy(i
, m
, sizeof(struct mce
));
45 /* Finally activate it */
50 static void raise_poll(struct mce
*m
)
55 memset(&b
, 0xff, sizeof(mce_banks_t
));
56 local_irq_save(flags
);
57 machine_check_poll(0, &b
);
58 local_irq_restore(flags
);
62 static void raise_exception(struct mce
*m
, struct pt_regs
*pregs
)
68 memset(®s
, 0, sizeof(struct pt_regs
));
73 /* in mcheck exeception handler, irq will be disabled */
74 local_irq_save(flags
);
75 do_machine_check(pregs
, 0);
76 local_irq_restore(flags
);
80 static cpumask_var_t mce_inject_cpumask
;
82 static int mce_raise_notify(unsigned int cmd
, struct pt_regs
*regs
)
84 int cpu
= smp_processor_id();
85 struct mce
*m
= &__get_cpu_var(injectm
);
86 if (!cpumask_test_cpu(cpu
, mce_inject_cpumask
))
88 cpumask_clear_cpu(cpu
, mce_inject_cpumask
);
89 if (m
->inject_flags
& MCJ_EXCEPTION
)
90 raise_exception(m
, regs
);
96 static void mce_irq_ipi(void *info
)
98 int cpu
= smp_processor_id();
99 struct mce
*m
= &__get_cpu_var(injectm
);
101 if (cpumask_test_cpu(cpu
, mce_inject_cpumask
) &&
102 m
->inject_flags
& MCJ_EXCEPTION
) {
103 cpumask_clear_cpu(cpu
, mce_inject_cpumask
);
104 raise_exception(m
, NULL
);
108 /* Inject mce on current CPU */
109 static int raise_local(void)
111 struct mce
*m
= &__get_cpu_var(injectm
);
112 int context
= MCJ_CTX(m
->inject_flags
);
116 if (m
->inject_flags
& MCJ_EXCEPTION
) {
117 printk(KERN_INFO
"Triggering MCE exception on CPU %d\n", cpu
);
121 * Could do more to fake interrupts like
122 * calling irq_enter, but the necessary
123 * machinery isn't exported currently.
126 case MCJ_CTX_PROCESS
:
127 raise_exception(m
, NULL
);
130 printk(KERN_INFO
"Invalid MCE context\n");
133 printk(KERN_INFO
"MCE exception done on CPU %d\n", cpu
);
134 } else if (m
->status
) {
135 printk(KERN_INFO
"Starting machine check poll CPU %d\n", cpu
);
138 printk(KERN_INFO
"Machine check poll done on CPU %d\n", cpu
);
145 static void raise_mce(struct mce
*m
)
147 int context
= MCJ_CTX(m
->inject_flags
);
151 if (context
== MCJ_CTX_RANDOM
)
154 #ifdef CONFIG_X86_LOCAL_APIC
155 if (m
->inject_flags
& (MCJ_IRQ_BRAODCAST
| MCJ_NMI_BROADCAST
)) {
160 cpumask_copy(mce_inject_cpumask
, cpu_online_mask
);
161 cpumask_clear_cpu(get_cpu(), mce_inject_cpumask
);
162 for_each_online_cpu(cpu
) {
163 struct mce
*mcpu
= &per_cpu(injectm
, cpu
);
164 if (!mcpu
->finished
||
165 MCJ_CTX(mcpu
->inject_flags
) != MCJ_CTX_RANDOM
)
166 cpumask_clear_cpu(cpu
, mce_inject_cpumask
);
168 if (!cpumask_empty(mce_inject_cpumask
)) {
169 if (m
->inject_flags
& MCJ_IRQ_BRAODCAST
) {
171 * don't wait because mce_irq_ipi is necessary
172 * to be sync with following raise_local
175 smp_call_function_many(mce_inject_cpumask
,
176 mce_irq_ipi
, NULL
, 0);
178 } else if (m
->inject_flags
& MCJ_NMI_BROADCAST
)
179 apic
->send_IPI_mask(mce_inject_cpumask
,
183 while (!cpumask_empty(mce_inject_cpumask
)) {
184 if (!time_before(jiffies
, start
+ 2*HZ
)) {
186 "Timeout waiting for mce inject %lx\n",
187 *cpumask_bits(mce_inject_cpumask
));
200 /* Error injection interface */
201 static ssize_t
mce_write(struct file
*filp
, const char __user
*ubuf
,
202 size_t usize
, loff_t
*off
)
206 if (!capable(CAP_SYS_ADMIN
))
209 * There are some cases where real MSR reads could slip
212 if (!boot_cpu_has(X86_FEATURE_MCE
) || !boot_cpu_has(X86_FEATURE_MCA
))
215 if ((unsigned long)usize
> sizeof(struct mce
))
216 usize
= sizeof(struct mce
);
217 if (copy_from_user(&m
, ubuf
, usize
))
220 if (m
.extcpu
>= num_possible_cpus() || !cpu_online(m
.extcpu
))
224 * Need to give user space some time to set everything up,
225 * so do it a jiffie or two later everywhere.
232 static int inject_init(void)
234 if (!alloc_cpumask_var(&mce_inject_cpumask
, GFP_KERNEL
))
236 printk(KERN_INFO
"Machine check injector initialized\n");
237 register_mce_write_callback(mce_write
);
238 register_nmi_handler(NMI_LOCAL
, mce_raise_notify
, 0,
243 module_init(inject_init
);
245 * Cannot tolerate unloading currently because we cannot
246 * guarantee all openers of mce_chrdev will get a reference to us.
248 MODULE_LICENSE("GPL");