Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / cpu / mcheck / mce-inject.c
1 /*
2  * Machine check injection support.
3  * Copyright 2008 Intel Corporation.
4  *
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
8  * of the License.
9  *
10  * Authors:
11  * Andi Kleen
12  * Ying Huang
13  */
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>
19 #include <linux/fs.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>
27 #include <asm/mce.h>
28 #include <asm/apic.h>
29 #include <asm/nmi.h>
30
31 /* Update fake mce registers on current CPU. */
32 static void inject_mce(struct mce *m)
33 {
34         struct mce *i = &per_cpu(injectm, m->extcpu);
35
36         /* Make sure no one reads partially written injectm */
37         i->finished = 0;
38         mb();
39         m->finished = 0;
40         /* First set the fields after finished */
41         i->extcpu = m->extcpu;
42         mb();
43         /* Now write record in order, finished last (except above) */
44         memcpy(i, m, sizeof(struct mce));
45         /* Finally activate it */
46         mb();
47         i->finished = 1;
48 }
49
50 static void raise_poll(struct mce *m)
51 {
52         unsigned long flags;
53         mce_banks_t b;
54
55         memset(&b, 0xff, sizeof(mce_banks_t));
56         local_irq_save(flags);
57         machine_check_poll(0, &b);
58         local_irq_restore(flags);
59         m->finished = 0;
60 }
61
62 static void raise_exception(struct mce *m, struct pt_regs *pregs)
63 {
64         struct pt_regs regs;
65         unsigned long flags;
66
67         if (!pregs) {
68                 memset(&regs, 0, sizeof(struct pt_regs));
69                 regs.ip = m->ip;
70                 regs.cs = m->cs;
71                 pregs = &regs;
72         }
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);
77         m->finished = 0;
78 }
79
80 static cpumask_var_t mce_inject_cpumask;
81
82 static int mce_raise_notify(unsigned int cmd, struct pt_regs *regs)
83 {
84         int cpu = smp_processor_id();
85         struct mce *m = &__get_cpu_var(injectm);
86         if (!cpumask_test_cpu(cpu, mce_inject_cpumask))
87                 return NMI_DONE;
88         cpumask_clear_cpu(cpu, mce_inject_cpumask);
89         if (m->inject_flags & MCJ_EXCEPTION)
90                 raise_exception(m, regs);
91         else if (m->status)
92                 raise_poll(m);
93         return NMI_HANDLED;
94 }
95
96 #if defined(CONFIG_X86_LOCAL_APIC) && !defined(CONFIG_XEN)
97 static void mce_irq_ipi(void *info)
98 {
99         int cpu = smp_processor_id();
100         struct mce *m = &__get_cpu_var(injectm);
101
102         if (cpumask_test_cpu(cpu, mce_inject_cpumask) &&
103                         m->inject_flags & MCJ_EXCEPTION) {
104                 cpumask_clear_cpu(cpu, mce_inject_cpumask);
105                 raise_exception(m, NULL);
106         }
107 }
108 #endif
109
110 /* Inject mce on current CPU */
111 static int raise_local(void)
112 {
113         struct mce *m = &__get_cpu_var(injectm);
114         int context = MCJ_CTX(m->inject_flags);
115         int ret = 0;
116         int cpu = m->extcpu;
117
118         if (m->inject_flags & MCJ_EXCEPTION) {
119                 printk(KERN_INFO "Triggering MCE exception on CPU %d\n", cpu);
120                 switch (context) {
121                 case MCJ_CTX_IRQ:
122                         /*
123                          * Could do more to fake interrupts like
124                          * calling irq_enter, but the necessary
125                          * machinery isn't exported currently.
126                          */
127                         /*FALL THROUGH*/
128                 case MCJ_CTX_PROCESS:
129                         raise_exception(m, NULL);
130                         break;
131                 default:
132                         printk(KERN_INFO "Invalid MCE context\n");
133                         ret = -EINVAL;
134                 }
135                 printk(KERN_INFO "MCE exception done on CPU %d\n", cpu);
136         } else if (m->status) {
137                 printk(KERN_INFO "Starting machine check poll CPU %d\n", cpu);
138                 raise_poll(m);
139                 mce_notify_irq();
140                 printk(KERN_INFO "Machine check poll done on CPU %d\n", cpu);
141         } else
142                 m->finished = 0;
143
144         return ret;
145 }
146
147 static void raise_mce(struct mce *m)
148 {
149         int context = MCJ_CTX(m->inject_flags);
150
151         inject_mce(m);
152
153         if (context == MCJ_CTX_RANDOM)
154                 return;
155
156 #if defined(CONFIG_X86_LOCAL_APIC) && !defined(CONFIG_XEN)
157         if (m->inject_flags & (MCJ_IRQ_BRAODCAST | MCJ_NMI_BROADCAST)) {
158                 unsigned long start;
159                 int cpu;
160
161                 get_online_cpus();
162                 cpumask_copy(mce_inject_cpumask, cpu_online_mask);
163                 cpumask_clear_cpu(get_cpu(), mce_inject_cpumask);
164                 for_each_online_cpu(cpu) {
165                         struct mce *mcpu = &per_cpu(injectm, cpu);
166                         if (!mcpu->finished ||
167                             MCJ_CTX(mcpu->inject_flags) != MCJ_CTX_RANDOM)
168                                 cpumask_clear_cpu(cpu, mce_inject_cpumask);
169                 }
170                 if (!cpumask_empty(mce_inject_cpumask)) {
171                         if (m->inject_flags & MCJ_IRQ_BRAODCAST) {
172                                 /*
173                                  * don't wait because mce_irq_ipi is necessary
174                                  * to be sync with following raise_local
175                                  */
176                                 preempt_disable();
177                                 smp_call_function_many(mce_inject_cpumask,
178                                         mce_irq_ipi, NULL, 0);
179                                 preempt_enable();
180                         } else if (m->inject_flags & MCJ_NMI_BROADCAST)
181                                 apic->send_IPI_mask(mce_inject_cpumask,
182                                                 NMI_VECTOR);
183                 }
184                 start = jiffies;
185                 while (!cpumask_empty(mce_inject_cpumask)) {
186                         if (!time_before(jiffies, start + 2*HZ)) {
187                                 printk(KERN_ERR
188                                 "Timeout waiting for mce inject %lx\n",
189                                         *cpumask_bits(mce_inject_cpumask));
190                                 break;
191                         }
192                         cpu_relax();
193                 }
194                 raise_local();
195                 put_cpu();
196                 put_online_cpus();
197         } else
198 #endif
199                 raise_local();
200 }
201
202 /* Error injection interface */
203 static ssize_t mce_write(struct file *filp, const char __user *ubuf,
204                          size_t usize, loff_t *off)
205 {
206         struct mce m;
207
208         if (!capable(CAP_SYS_ADMIN))
209                 return -EPERM;
210         /*
211          * There are some cases where real MSR reads could slip
212          * through.
213          */
214         if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
215                 return -EIO;
216
217         if ((unsigned long)usize > sizeof(struct mce))
218                 usize = sizeof(struct mce);
219         if (copy_from_user(&m, ubuf, usize))
220                 return -EFAULT;
221
222         if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
223                 return -EINVAL;
224
225         /*
226          * Need to give user space some time to set everything up,
227          * so do it a jiffie or two later everywhere.
228          */
229         schedule_timeout(2);
230         raise_mce(&m);
231         return usize;
232 }
233
234 static int inject_init(void)
235 {
236         if (!alloc_cpumask_var(&mce_inject_cpumask, GFP_KERNEL))
237                 return -ENOMEM;
238         printk(KERN_INFO "Machine check injector initialized\n");
239         register_mce_write_callback(mce_write);
240         register_nmi_handler(NMI_LOCAL, mce_raise_notify, 0,
241                                 "mce_notify");
242         return 0;
243 }
244
245 module_init(inject_init);
246 /*
247  * Cannot tolerate unloading currently because we cannot
248  * guarantee all openers of mce_chrdev will get a reference to us.
249  */
250 MODULE_LICENSE("GPL");