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