]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - arch/x86/kernel/cpu/mcheck/mce-inject.c
Merge branch 'tip/perf/core' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[mirror_ubuntu-eoan-kernel.git] / arch / x86 / kernel / cpu / mcheck / mce-inject.c
CommitLineData
ea149b36
AK
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 */
98a9c8c3 14#include <linux/uaccess.h>
ea149b36
AK
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/smp.h>
5b7e88ed
HY
21#include <linux/notifier.h>
22#include <linux/kdebug.h>
23#include <linux/cpu.h>
24#include <linux/sched.h>
5a0e3ad6 25#include <linux/gfp.h>
ea149b36 26#include <asm/mce.h>
5b7e88ed 27#include <asm/apic.h>
c410b830 28#include <asm/nmi.h>
ea149b36
AK
29
30/* Update fake mce registers on current CPU. */
31static void inject_mce(struct mce *m)
32{
d620c67f 33 struct mce *i = &per_cpu(injectm, m->extcpu);
ea149b36 34
0d2eb44f 35 /* Make sure no one reads partially written injectm */
ea149b36
AK
36 i->finished = 0;
37 mb();
38 m->finished = 0;
39 /* First set the fields after finished */
d620c67f 40 i->extcpu = m->extcpu;
ea149b36
AK
41 mb();
42 /* Now write record in order, finished last (except above) */
43 memcpy(i, m, sizeof(struct mce));
44 /* Finally activate it */
45 mb();
46 i->finished = 1;
47}
48
0dcc6685 49static void raise_poll(struct mce *m)
5b7e88ed
HY
50{
51 unsigned long flags;
52 mce_banks_t b;
53
54 memset(&b, 0xff, sizeof(mce_banks_t));
55 local_irq_save(flags);
56 machine_check_poll(0, &b);
57 local_irq_restore(flags);
58 m->finished = 0;
59}
60
0dcc6685 61static void raise_exception(struct mce *m, struct pt_regs *pregs)
5b7e88ed
HY
62{
63 struct pt_regs regs;
64 unsigned long flags;
65
66 if (!pregs) {
67 memset(&regs, 0, sizeof(struct pt_regs));
68 regs.ip = m->ip;
69 regs.cs = m->cs;
70 pregs = &regs;
71 }
72 /* in mcheck exeception handler, irq will be disabled */
73 local_irq_save(flags);
74 do_machine_check(pregs, 0);
75 local_irq_restore(flags);
76 m->finished = 0;
77}
78
6ac5c531 79static cpumask_var_t mce_inject_cpumask;
5b7e88ed 80
9c48f1c6 81static int mce_raise_notify(unsigned int cmd, struct pt_regs *regs)
5b7e88ed 82{
5b7e88ed
HY
83 int cpu = smp_processor_id();
84 struct mce *m = &__get_cpu_var(injectm);
9c48f1c6
DZ
85 if (!cpumask_test_cpu(cpu, mce_inject_cpumask))
86 return NMI_DONE;
6ac5c531 87 cpumask_clear_cpu(cpu, mce_inject_cpumask);
0dcc6685 88 if (m->inject_flags & MCJ_EXCEPTION)
9c48f1c6 89 raise_exception(m, regs);
5b7e88ed 90 else if (m->status)
0dcc6685 91 raise_poll(m);
9c48f1c6 92 return NMI_HANDLED;
5b7e88ed
HY
93}
94
ea149b36 95/* Inject mce on current CPU */
14c0abf1 96static int raise_local(void)
ea149b36 97{
14c0abf1 98 struct mce *m = &__get_cpu_var(injectm);
5b7e88ed
HY
99 int context = MCJ_CTX(m->inject_flags);
100 int ret = 0;
d620c67f 101 int cpu = m->extcpu;
ea149b36 102
0dcc6685 103 if (m->inject_flags & MCJ_EXCEPTION) {
ea149b36 104 printk(KERN_INFO "Triggering MCE exception on CPU %d\n", cpu);
5b7e88ed
HY
105 switch (context) {
106 case MCJ_CTX_IRQ:
107 /*
108 * Could do more to fake interrupts like
109 * calling irq_enter, but the necessary
110 * machinery isn't exported currently.
111 */
112 /*FALL THROUGH*/
113 case MCJ_CTX_PROCESS:
0dcc6685 114 raise_exception(m, NULL);
5b7e88ed
HY
115 break;
116 default:
117 printk(KERN_INFO "Invalid MCE context\n");
118 ret = -EINVAL;
119 }
ea149b36 120 printk(KERN_INFO "MCE exception done on CPU %d\n", cpu);
5b7e88ed 121 } else if (m->status) {
ea149b36 122 printk(KERN_INFO "Starting machine check poll CPU %d\n", cpu);
0dcc6685 123 raise_poll(m);
9ff36ee9 124 mce_notify_irq();
5b7e88ed
HY
125 printk(KERN_INFO "Machine check poll done on CPU %d\n", cpu);
126 } else
127 m->finished = 0;
128
129 return ret;
130}
131
132static void raise_mce(struct mce *m)
133{
134 int context = MCJ_CTX(m->inject_flags);
135
136 inject_mce(m);
137
138 if (context == MCJ_CTX_RANDOM)
139 return;
140
141#ifdef CONFIG_X86_LOCAL_APIC
142 if (m->inject_flags & MCJ_NMI_BROADCAST) {
143 unsigned long start;
144 int cpu;
145 get_online_cpus();
6ac5c531
RR
146 cpumask_copy(mce_inject_cpumask, cpu_online_mask);
147 cpumask_clear_cpu(get_cpu(), mce_inject_cpumask);
5b7e88ed
HY
148 for_each_online_cpu(cpu) {
149 struct mce *mcpu = &per_cpu(injectm, cpu);
150 if (!mcpu->finished ||
151 MCJ_CTX(mcpu->inject_flags) != MCJ_CTX_RANDOM)
6ac5c531 152 cpumask_clear_cpu(cpu, mce_inject_cpumask);
5b7e88ed 153 }
6ac5c531
RR
154 if (!cpumask_empty(mce_inject_cpumask))
155 apic->send_IPI_mask(mce_inject_cpumask, NMI_VECTOR);
5b7e88ed 156 start = jiffies;
6ac5c531 157 while (!cpumask_empty(mce_inject_cpumask)) {
5b7e88ed
HY
158 if (!time_before(jiffies, start + 2*HZ)) {
159 printk(KERN_ERR
160 "Timeout waiting for mce inject NMI %lx\n",
6ac5c531 161 *cpumask_bits(mce_inject_cpumask));
5b7e88ed
HY
162 break;
163 }
164 cpu_relax();
165 }
14c0abf1 166 raise_local();
5b7e88ed
HY
167 put_cpu();
168 put_online_cpus();
169 } else
170#endif
14c0abf1 171 raise_local();
ea149b36
AK
172}
173
174/* Error injection interface */
175static ssize_t mce_write(struct file *filp, const char __user *ubuf,
176 size_t usize, loff_t *off)
177{
ea149b36
AK
178 struct mce m;
179
180 if (!capable(CAP_SYS_ADMIN))
181 return -EPERM;
182 /*
183 * There are some cases where real MSR reads could slip
184 * through.
185 */
186 if (!boot_cpu_has(X86_FEATURE_MCE) || !boot_cpu_has(X86_FEATURE_MCA))
187 return -EIO;
188
189 if ((unsigned long)usize > sizeof(struct mce))
190 usize = sizeof(struct mce);
191 if (copy_from_user(&m, ubuf, usize))
192 return -EFAULT;
193
d620c67f 194 if (m.extcpu >= num_possible_cpus() || !cpu_online(m.extcpu))
ea149b36
AK
195 return -EINVAL;
196
ea149b36
AK
197 /*
198 * Need to give user space some time to set everything up,
199 * so do it a jiffie or two later everywhere.
ea149b36 200 */
5b7e88ed
HY
201 schedule_timeout(2);
202 raise_mce(&m);
ea149b36
AK
203 return usize;
204}
205
206static int inject_init(void)
207{
6ac5c531
RR
208 if (!alloc_cpumask_var(&mce_inject_cpumask, GFP_KERNEL))
209 return -ENOMEM;
ea149b36 210 printk(KERN_INFO "Machine check injector initialized\n");
66f5ddf3 211 register_mce_write_callback(mce_write);
9c48f1c6
DZ
212 register_nmi_handler(NMI_LOCAL, mce_raise_notify, 0,
213 "mce_notify");
ea149b36
AK
214 return 0;
215}
216
217module_init(inject_init);
5706001a
PA
218/*
219 * Cannot tolerate unloading currently because we cannot
ea149b36
AK
220 * guarantee all openers of mce_chrdev will get a reference to us.
221 */
222MODULE_LICENSE("GPL");