Blame view

drivers/xen/mcelog.c 10 KB
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  /******************************************************************************
   * mcelog.c
   * Driver for receiving and transferring machine check error infomation
   *
   * Copyright (c) 2012 Intel Corporation
   * Author: Liu, Jinsong <jinsong.liu@intel.com>
   * Author: Jiang, Yunhong <yunhong.jiang@intel.com>
   * Author: Ke, Liping <liping.ke@intel.com>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License version 2
   * as published by the Free Software Foundation; or, when distributed
   * separately from the Linux kernel or incorporated into other
   * software packages, subject to the following license:
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this source file (the "Software"), to deal in the Software without
   * restriction, including without limitation the rights to use, copy, modify,
   * merge, publish, distribute, sublicense, and/or sell copies of the Software,
   * and to permit persons to whom the Software is furnished to do so, subject to
   * the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in
   * all copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
   * IN THE SOFTWARE.
   */
283c0972d   Joe Perches   xen: Convert prin...
34
  #define pr_fmt(fmt) "xen_mcelog: " fmt
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
35
36
37
38
39
40
41
42
43
  #include <linux/init.h>
  #include <linux/types.h>
  #include <linux/kernel.h>
  #include <linux/slab.h>
  #include <linux/fs.h>
  #include <linux/device.h>
  #include <linux/miscdevice.h>
  #include <linux/uaccess.h>
  #include <linux/capability.h>
a867e5d6b   Liu, Jinsong   xen/mce: add .pol...
44
45
  #include <linux/poll.h>
  #include <linux/sched.h>
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
46
47
48
49
50
51
52
  
  #include <xen/interface/xen.h>
  #include <xen/events.h>
  #include <xen/interface/vcpu.h>
  #include <xen/xen.h>
  #include <asm/xen/hypercall.h>
  #include <asm/xen/hypervisor.h>
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
53
54
55
  static struct mc_info g_mi;
  static struct mcinfo_logical_cpu *g_physinfo;
  static uint32_t ncpus;
1b2a05516   Liu, Jinsong   xen/mce: schedule...
56
  static DEFINE_MUTEX(mcelog_lock);
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
57
58
59
60
61
62
63
64
65
66
  
  static struct xen_mce_log xen_mcelog = {
  	.signature	= XEN_MCE_LOG_SIGNATURE,
  	.len		= XEN_MCE_LOG_LEN,
  	.recordlen	= sizeof(struct xen_mce),
  };
  
  static DEFINE_SPINLOCK(xen_mce_chrdev_state_lock);
  static int xen_mce_chrdev_open_count;	/* #times opened */
  static int xen_mce_chrdev_open_exclu;	/* already open exclusive? */
a867e5d6b   Liu, Jinsong   xen/mce: add .pol...
67
  static DECLARE_WAIT_QUEUE_HEAD(xen_mce_chrdev_wait);
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  static int xen_mce_chrdev_open(struct inode *inode, struct file *file)
  {
  	spin_lock(&xen_mce_chrdev_state_lock);
  
  	if (xen_mce_chrdev_open_exclu ||
  	    (xen_mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
  		spin_unlock(&xen_mce_chrdev_state_lock);
  
  		return -EBUSY;
  	}
  
  	if (file->f_flags & O_EXCL)
  		xen_mce_chrdev_open_exclu = 1;
  	xen_mce_chrdev_open_count++;
  
  	spin_unlock(&xen_mce_chrdev_state_lock);
  
  	return nonseekable_open(inode, file);
  }
  
  static int xen_mce_chrdev_release(struct inode *inode, struct file *file)
  {
  	spin_lock(&xen_mce_chrdev_state_lock);
  
  	xen_mce_chrdev_open_count--;
  	xen_mce_chrdev_open_exclu = 0;
  
  	spin_unlock(&xen_mce_chrdev_state_lock);
  
  	return 0;
  }
  
  static ssize_t xen_mce_chrdev_read(struct file *filp, char __user *ubuf,
  				size_t usize, loff_t *off)
  {
  	char __user *buf = ubuf;
  	unsigned num;
  	int i, err;
1b2a05516   Liu, Jinsong   xen/mce: schedule...
106
  	mutex_lock(&mcelog_lock);
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  
  	num = xen_mcelog.next;
  
  	/* Only supports full reads right now */
  	err = -EINVAL;
  	if (*off != 0 || usize < XEN_MCE_LOG_LEN*sizeof(struct xen_mce))
  		goto out;
  
  	err = 0;
  	for (i = 0; i < num; i++) {
  		struct xen_mce *m = &xen_mcelog.entry[i];
  
  		err |= copy_to_user(buf, m, sizeof(*m));
  		buf += sizeof(*m);
  	}
  
  	memset(xen_mcelog.entry, 0, num * sizeof(struct xen_mce));
  	xen_mcelog.next = 0;
  
  	if (err)
  		err = -EFAULT;
  
  out:
1b2a05516   Liu, Jinsong   xen/mce: schedule...
130
  	mutex_unlock(&mcelog_lock);
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
131
132
133
  
  	return err ? err : buf - ubuf;
  }
afc9a42b7   Al Viro   the rest of drive...
134
  static __poll_t xen_mce_chrdev_poll(struct file *file, poll_table *wait)
a867e5d6b   Liu, Jinsong   xen/mce: add .pol...
135
136
137
138
  {
  	poll_wait(file, &xen_mce_chrdev_wait, wait);
  
  	if (xen_mcelog.next)
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
139
  		return EPOLLIN | EPOLLRDNORM;
a867e5d6b   Liu, Jinsong   xen/mce: add .pol...
140
141
142
  
  	return 0;
  }
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  static long xen_mce_chrdev_ioctl(struct file *f, unsigned int cmd,
  				unsigned long arg)
  {
  	int __user *p = (int __user *)arg;
  
  	if (!capable(CAP_SYS_ADMIN))
  		return -EPERM;
  
  	switch (cmd) {
  	case MCE_GET_RECORD_LEN:
  		return put_user(sizeof(struct xen_mce), p);
  	case MCE_GET_LOG_LEN:
  		return put_user(XEN_MCE_LOG_LEN, p);
  	case MCE_GETCLEAR_FLAGS: {
  		unsigned flags;
  
  		do {
  			flags = xen_mcelog.flags;
  		} while (cmpxchg(&xen_mcelog.flags, flags, 0) != flags);
  
  		return put_user(flags, p);
  	}
  	default:
  		return -ENOTTY;
  	}
  }
  
  static const struct file_operations xen_mce_chrdev_ops = {
  	.open			= xen_mce_chrdev_open,
  	.release		= xen_mce_chrdev_release,
  	.read			= xen_mce_chrdev_read,
a867e5d6b   Liu, Jinsong   xen/mce: add .pol...
174
  	.poll			= xen_mce_chrdev_poll,
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  	.unlocked_ioctl		= xen_mce_chrdev_ioctl,
  	.llseek			= no_llseek,
  };
  
  static struct miscdevice xen_mce_chrdev_device = {
  	MISC_MCELOG_MINOR,
  	"mcelog",
  	&xen_mce_chrdev_ops,
  };
  
  /*
   * Caller should hold the mcelog_lock
   */
  static void xen_mce_log(struct xen_mce *mce)
  {
  	unsigned entry;
  
  	entry = xen_mcelog.next;
  
  	/*
  	 * When the buffer fills up discard new entries.
  	 * Assume that the earlier errors are the more
  	 * interesting ones:
  	 */
  	if (entry >= XEN_MCE_LOG_LEN) {
  		set_bit(XEN_MCE_OVERFLOW,
  			(unsigned long *)&xen_mcelog.flags);
  		return;
  	}
  
  	memcpy(xen_mcelog.entry + entry, mce, sizeof(struct xen_mce));
  
  	xen_mcelog.next++;
  }
  
  static int convert_log(struct mc_info *mi)
  {
  	struct mcinfo_common *mic;
  	struct mcinfo_global *mc_global;
  	struct mcinfo_bank *mc_bank;
  	struct xen_mce m;
  	uint32_t i;
  
  	mic = NULL;
  	x86_mcinfo_lookup(&mic, mi, MC_TYPE_GLOBAL);
  	if (unlikely(!mic)) {
283c0972d   Joe Perches   xen: Convert prin...
221
222
  		pr_warn("Failed to find global error info
  ");
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
223
224
225
226
227
228
229
230
231
232
233
234
235
  		return -ENODEV;
  	}
  
  	memset(&m, 0, sizeof(struct xen_mce));
  
  	mc_global = (struct mcinfo_global *)mic;
  	m.mcgstatus = mc_global->mc_gstatus;
  	m.apicid = mc_global->mc_apicid;
  
  	for (i = 0; i < ncpus; i++)
  		if (g_physinfo[i].mc_apicid == m.apicid)
  			break;
  	if (unlikely(i == ncpus)) {
283c0972d   Joe Perches   xen: Convert prin...
236
237
  		pr_warn("Failed to match cpu with apicid %d
  ", m.apicid);
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
238
239
240
241
242
243
244
245
246
247
248
  		return -ENODEV;
  	}
  
  	m.socketid = g_physinfo[i].mc_chipid;
  	m.cpu = m.extcpu = g_physinfo[i].mc_cpunr;
  	m.cpuvendor = (__u8)g_physinfo[i].mc_vendor;
  	m.mcgcap = g_physinfo[i].mc_msrvalues[__MC_MSR_MCGCAP].value;
  
  	mic = NULL;
  	x86_mcinfo_lookup(&mic, mi, MC_TYPE_BANK);
  	if (unlikely(!mic)) {
283c0972d   Joe Perches   xen: Convert prin...
249
250
  		pr_warn("Fail to find bank error info
  ");
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
  		return -ENODEV;
  	}
  
  	do {
  		if ((!mic) || (mic->size == 0) ||
  		    (mic->type != MC_TYPE_GLOBAL   &&
  		     mic->type != MC_TYPE_BANK     &&
  		     mic->type != MC_TYPE_EXTENDED &&
  		     mic->type != MC_TYPE_RECOVERY))
  			break;
  
  		if (mic->type == MC_TYPE_BANK) {
  			mc_bank = (struct mcinfo_bank *)mic;
  			m.misc = mc_bank->mc_misc;
  			m.status = mc_bank->mc_status;
  			m.addr = mc_bank->mc_addr;
  			m.tsc = mc_bank->mc_tsc;
  			m.bank = mc_bank->mc_bank;
  			m.finished = 1;
  			/*log this record*/
  			xen_mce_log(&m);
  		}
  		mic = x86_mcinfo_next(mic);
  	} while (1);
  
  	return 0;
  }
  
  static int mc_queue_handle(uint32_t flags)
  {
  	struct xen_mc mc_op;
  	int ret = 0;
  
  	mc_op.cmd = XEN_MC_fetch;
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
285
286
287
288
289
  	set_xen_guest_handle(mc_op.u.mc_fetch.data, &g_mi);
  	do {
  		mc_op.u.mc_fetch.flags = flags;
  		ret = HYPERVISOR_mca(&mc_op);
  		if (ret) {
283c0972d   Joe Perches   xen: Convert prin...
290
291
292
  			pr_err("Failed to fetch %surgent error log
  ",
  			       flags == XEN_MC_URGENT ? "" : "non");
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
293
294
295
296
297
298
299
300
301
  			break;
  		}
  
  		if (mc_op.u.mc_fetch.flags & XEN_MC_NODATA ||
  		    mc_op.u.mc_fetch.flags & XEN_MC_FETCHFAILED)
  			break;
  		else {
  			ret = convert_log(&g_mi);
  			if (ret)
283c0972d   Joe Perches   xen: Convert prin...
302
303
  				pr_warn("Failed to convert this error log, continue acking it anyway
  ");
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
304
305
306
307
  
  			mc_op.u.mc_fetch.flags = flags | XEN_MC_ACK;
  			ret = HYPERVISOR_mca(&mc_op);
  			if (ret) {
283c0972d   Joe Perches   xen: Convert prin...
308
309
  				pr_err("Failed to ack previous error log
  ");
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
310
311
312
313
314
315
316
317
318
  				break;
  			}
  		}
  	} while (1);
  
  	return ret;
  }
  
  /* virq handler for machine check error info*/
1b2a05516   Liu, Jinsong   xen/mce: schedule...
319
  static void xen_mce_work_fn(struct work_struct *work)
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
320
321
  {
  	int err;
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
322

1b2a05516   Liu, Jinsong   xen/mce: schedule...
323
  	mutex_lock(&mcelog_lock);
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
324
325
326
327
  
  	/* urgent mc_info */
  	err = mc_queue_handle(XEN_MC_URGENT);
  	if (err)
283c0972d   Joe Perches   xen: Convert prin...
328
329
  		pr_err("Failed to handle urgent mc_info queue, continue handling nonurgent mc_info queue anyway
  ");
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
330
331
332
333
  
  	/* nonurgent mc_info */
  	err = mc_queue_handle(XEN_MC_NONURGENT);
  	if (err)
283c0972d   Joe Perches   xen: Convert prin...
334
335
  		pr_err("Failed to handle nonurgent mc_info queue
  ");
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
336

a867e5d6b   Liu, Jinsong   xen/mce: add .pol...
337
338
  	/* wake processes polling /dev/mcelog */
  	wake_up_interruptible(&xen_mce_chrdev_wait);
1b2a05516   Liu, Jinsong   xen/mce: schedule...
339
340
341
  	mutex_unlock(&mcelog_lock);
  }
  static DECLARE_WORK(xen_mce_work, xen_mce_work_fn);
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
342

1b2a05516   Liu, Jinsong   xen/mce: schedule...
343
344
345
  static irqreturn_t xen_mce_interrupt(int irq, void *dev_id)
  {
  	schedule_work(&xen_mce_work);
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
346
347
348
349
350
351
352
353
354
355
356
357
  	return IRQ_HANDLED;
  }
  
  static int bind_virq_for_mce(void)
  {
  	int ret;
  	struct xen_mc mc_op;
  
  	memset(&mc_op, 0, sizeof(struct xen_mc));
  
  	/* Fetch physical CPU Numbers */
  	mc_op.cmd = XEN_MC_physcpuinfo;
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
358
359
360
  	set_xen_guest_handle(mc_op.u.mc_physcpuinfo.info, g_physinfo);
  	ret = HYPERVISOR_mca(&mc_op);
  	if (ret) {
283c0972d   Joe Perches   xen: Convert prin...
361
362
  		pr_err("Failed to get CPU numbers
  ");
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
363
364
365
366
367
368
369
370
371
372
373
374
  		return ret;
  	}
  
  	/* Fetch each CPU Physical Info for later reference*/
  	ncpus = mc_op.u.mc_physcpuinfo.ncpus;
  	g_physinfo = kcalloc(ncpus, sizeof(struct mcinfo_logical_cpu),
  			     GFP_KERNEL);
  	if (!g_physinfo)
  		return -ENOMEM;
  	set_xen_guest_handle(mc_op.u.mc_physcpuinfo.info, g_physinfo);
  	ret = HYPERVISOR_mca(&mc_op);
  	if (ret) {
283c0972d   Joe Perches   xen: Convert prin...
375
376
  		pr_err("Failed to get CPU info
  ");
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
377
378
379
380
381
382
383
  		kfree(g_physinfo);
  		return ret;
  	}
  
  	ret  = bind_virq_to_irqhandler(VIRQ_MCA, 0,
  				       xen_mce_interrupt, 0, "mce", NULL);
  	if (ret < 0) {
283c0972d   Joe Perches   xen: Convert prin...
384
385
  		pr_err("Failed to bind virq
  ");
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
386
387
388
389
390
391
392
393
394
  		kfree(g_physinfo);
  		return ret;
  	}
  
  	return 0;
  }
  
  static int __init xen_late_init_mcelog(void)
  {
ebfe79a7c   Dan Carpenter   xen/mce: fix up x...
395
  	int ret;
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
396
  	/* Only DOM0 is responsible for MCE logging */
ebfe79a7c   Dan Carpenter   xen/mce: fix up x...
397
398
399
400
401
402
403
404
405
406
407
  	if (!xen_initial_domain())
  		return -ENODEV;
  
  	/* register character device /dev/mcelog for xen mcelog */
  	ret = misc_register(&xen_mce_chrdev_device);
  	if (ret)
  		return ret;
  
  	ret = bind_virq_for_mce();
  	if (ret)
  		goto deregister;
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
408

b86705901   Juergen Gross   x86/MCE, xen/mcel...
409
410
  	pr_info("/dev/mcelog registered by Xen
  ");
ebfe79a7c   Dan Carpenter   xen/mce: fix up x...
411
412
413
414
415
  	return 0;
  
  deregister:
  	misc_deregister(&xen_mce_chrdev_device);
  	return ret;
cef12ee52   Liu, Jinsong   xen/mce: Add mcel...
416
417
  }
  device_initcall(xen_late_init_mcelog);