1cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong/****************************************************************************** 2cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * mcelog.c 3cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * Driver for receiving and transferring machine check error infomation 4cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * 5cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * Copyright (c) 2012 Intel Corporation 6cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * Author: Liu, Jinsong <jinsong.liu@intel.com> 7cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * Author: Jiang, Yunhong <yunhong.jiang@intel.com> 8cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * Author: Ke, Liping <liping.ke@intel.com> 9cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * 10cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * This program is free software; you can redistribute it and/or 11cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * modify it under the terms of the GNU General Public License version 2 12cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * as published by the Free Software Foundation; or, when distributed 13cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * separately from the Linux kernel or incorporated into other 14cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * software packages, subject to the following license: 15cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * 16cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * Permission is hereby granted, free of charge, to any person obtaining a copy 17cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * of this source file (the "Software"), to deal in the Software without 18cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * restriction, including without limitation the rights to use, copy, modify, 19cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * merge, publish, distribute, sublicense, and/or sell copies of the Software, 20cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * and to permit persons to whom the Software is furnished to do so, subject to 21cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * the following conditions: 22cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * 23cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * The above copyright notice and this permission notice shall be included in 24cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * all copies or substantial portions of the Software. 25cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * 26cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 31cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 32cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * IN THE SOFTWARE. 33cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong */ 34cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 35283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches#define pr_fmt(fmt) "xen_mcelog: " fmt 36283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches 37cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <linux/init.h> 38cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <linux/types.h> 39cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <linux/kernel.h> 40cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <linux/slab.h> 41cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <linux/fs.h> 42cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <linux/device.h> 43cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <linux/miscdevice.h> 44cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <linux/uaccess.h> 45cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <linux/capability.h> 46a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong#include <linux/poll.h> 47a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong#include <linux/sched.h> 48cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 49cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <xen/interface/xen.h> 50cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <xen/events.h> 51cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <xen/interface/vcpu.h> 52cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <xen/xen.h> 53cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <asm/xen/hypercall.h> 54cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong#include <asm/xen/hypervisor.h> 55cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 56cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic struct mc_info g_mi; 57cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic struct mcinfo_logical_cpu *g_physinfo; 58cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic uint32_t ncpus; 59cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 601b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsongstatic DEFINE_MUTEX(mcelog_lock); 61cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 62cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic struct xen_mce_log xen_mcelog = { 63cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong .signature = XEN_MCE_LOG_SIGNATURE, 64cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong .len = XEN_MCE_LOG_LEN, 65cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong .recordlen = sizeof(struct xen_mce), 66cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong}; 67cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 68cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic DEFINE_SPINLOCK(xen_mce_chrdev_state_lock); 69cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic int xen_mce_chrdev_open_count; /* #times opened */ 70cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic int xen_mce_chrdev_open_exclu; /* already open exclusive? */ 71cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 72a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsongstatic DECLARE_WAIT_QUEUE_HEAD(xen_mce_chrdev_wait); 73a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong 74cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic int xen_mce_chrdev_open(struct inode *inode, struct file *file) 75cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong{ 76cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong spin_lock(&xen_mce_chrdev_state_lock); 77cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 78cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (xen_mce_chrdev_open_exclu || 79cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong (xen_mce_chrdev_open_count && (file->f_flags & O_EXCL))) { 80cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong spin_unlock(&xen_mce_chrdev_state_lock); 81cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 82cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return -EBUSY; 83cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 84cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 85cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (file->f_flags & O_EXCL) 86cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong xen_mce_chrdev_open_exclu = 1; 87cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong xen_mce_chrdev_open_count++; 88cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 89cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong spin_unlock(&xen_mce_chrdev_state_lock); 90cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 91cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return nonseekable_open(inode, file); 92cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong} 93cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 94cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic int xen_mce_chrdev_release(struct inode *inode, struct file *file) 95cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong{ 96cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong spin_lock(&xen_mce_chrdev_state_lock); 97cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 98cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong xen_mce_chrdev_open_count--; 99cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong xen_mce_chrdev_open_exclu = 0; 100cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 101cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong spin_unlock(&xen_mce_chrdev_state_lock); 102cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 103cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return 0; 104cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong} 105cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 106cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic ssize_t xen_mce_chrdev_read(struct file *filp, char __user *ubuf, 107cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong size_t usize, loff_t *off) 108cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong{ 109cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong char __user *buf = ubuf; 110cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong unsigned num; 111cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong int i, err; 112cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 1131b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsong mutex_lock(&mcelog_lock); 114cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 115cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong num = xen_mcelog.next; 116cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 117cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong /* Only supports full reads right now */ 118cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong err = -EINVAL; 119cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (*off != 0 || usize < XEN_MCE_LOG_LEN*sizeof(struct xen_mce)) 120cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong goto out; 121cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 122cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong err = 0; 123cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong for (i = 0; i < num; i++) { 124cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong struct xen_mce *m = &xen_mcelog.entry[i]; 125cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 126cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong err |= copy_to_user(buf, m, sizeof(*m)); 127cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong buf += sizeof(*m); 128cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 129cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 130cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong memset(xen_mcelog.entry, 0, num * sizeof(struct xen_mce)); 131cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong xen_mcelog.next = 0; 132cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 133cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (err) 134cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong err = -EFAULT; 135cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 136cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongout: 1371b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsong mutex_unlock(&mcelog_lock); 138cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 139cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return err ? err : buf - ubuf; 140cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong} 141cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 142a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsongstatic unsigned int xen_mce_chrdev_poll(struct file *file, poll_table *wait) 143a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong{ 144a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong poll_wait(file, &xen_mce_chrdev_wait, wait); 145a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong 146a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong if (xen_mcelog.next) 147a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong return POLLIN | POLLRDNORM; 148a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong 149a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong return 0; 150a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong} 151a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong 152cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic long xen_mce_chrdev_ioctl(struct file *f, unsigned int cmd, 153cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong unsigned long arg) 154cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong{ 155cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong int __user *p = (int __user *)arg; 156cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 157cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (!capable(CAP_SYS_ADMIN)) 158cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return -EPERM; 159cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 160cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong switch (cmd) { 161cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong case MCE_GET_RECORD_LEN: 162cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return put_user(sizeof(struct xen_mce), p); 163cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong case MCE_GET_LOG_LEN: 164cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return put_user(XEN_MCE_LOG_LEN, p); 165cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong case MCE_GETCLEAR_FLAGS: { 166cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong unsigned flags; 167cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 168cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong do { 169cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong flags = xen_mcelog.flags; 170cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } while (cmpxchg(&xen_mcelog.flags, flags, 0) != flags); 171cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 172cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return put_user(flags, p); 173cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 174cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong default: 175cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return -ENOTTY; 176cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 177cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong} 178cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 179cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic const struct file_operations xen_mce_chrdev_ops = { 180cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong .open = xen_mce_chrdev_open, 181cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong .release = xen_mce_chrdev_release, 182cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong .read = xen_mce_chrdev_read, 183a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong .poll = xen_mce_chrdev_poll, 184cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong .unlocked_ioctl = xen_mce_chrdev_ioctl, 185cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong .llseek = no_llseek, 186cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong}; 187cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 188cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic struct miscdevice xen_mce_chrdev_device = { 189cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong MISC_MCELOG_MINOR, 190cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong "mcelog", 191cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong &xen_mce_chrdev_ops, 192cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong}; 193cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 194cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong/* 195cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * Caller should hold the mcelog_lock 196cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong */ 197cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic void xen_mce_log(struct xen_mce *mce) 198cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong{ 199cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong unsigned entry; 200cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 201cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong entry = xen_mcelog.next; 202cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 203cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong /* 204cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * When the buffer fills up discard new entries. 205cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * Assume that the earlier errors are the more 206cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong * interesting ones: 207cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong */ 208cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (entry >= XEN_MCE_LOG_LEN) { 209cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong set_bit(XEN_MCE_OVERFLOW, 210cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong (unsigned long *)&xen_mcelog.flags); 211cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return; 212cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 213cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 214cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong memcpy(xen_mcelog.entry + entry, mce, sizeof(struct xen_mce)); 215cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 216cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong xen_mcelog.next++; 217cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong} 218cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 219cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic int convert_log(struct mc_info *mi) 220cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong{ 221cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong struct mcinfo_common *mic; 222cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong struct mcinfo_global *mc_global; 223cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong struct mcinfo_bank *mc_bank; 224cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong struct xen_mce m; 225cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong uint32_t i; 226cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 227cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mic = NULL; 228cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong x86_mcinfo_lookup(&mic, mi, MC_TYPE_GLOBAL); 229cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (unlikely(!mic)) { 230283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_warn("Failed to find global error info\n"); 231cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return -ENODEV; 232cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 233cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 234cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong memset(&m, 0, sizeof(struct xen_mce)); 235cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 236cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mc_global = (struct mcinfo_global *)mic; 237cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.mcgstatus = mc_global->mc_gstatus; 238cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.apicid = mc_global->mc_apicid; 239cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 240cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong for (i = 0; i < ncpus; i++) 241cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (g_physinfo[i].mc_apicid == m.apicid) 242cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong break; 243cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (unlikely(i == ncpus)) { 244283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_warn("Failed to match cpu with apicid %d\n", m.apicid); 245cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return -ENODEV; 246cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 247cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 248cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.socketid = g_physinfo[i].mc_chipid; 249cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.cpu = m.extcpu = g_physinfo[i].mc_cpunr; 250cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.cpuvendor = (__u8)g_physinfo[i].mc_vendor; 251cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.mcgcap = g_physinfo[i].mc_msrvalues[__MC_MSR_MCGCAP].value; 252cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 253cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mic = NULL; 254cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong x86_mcinfo_lookup(&mic, mi, MC_TYPE_BANK); 255cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (unlikely(!mic)) { 256283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_warn("Fail to find bank error info\n"); 257cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return -ENODEV; 258cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 259cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 260cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong do { 261cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if ((!mic) || (mic->size == 0) || 262cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong (mic->type != MC_TYPE_GLOBAL && 263cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mic->type != MC_TYPE_BANK && 264cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mic->type != MC_TYPE_EXTENDED && 265cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mic->type != MC_TYPE_RECOVERY)) 266cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong break; 267cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 268cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (mic->type == MC_TYPE_BANK) { 269cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mc_bank = (struct mcinfo_bank *)mic; 270cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.misc = mc_bank->mc_misc; 271cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.status = mc_bank->mc_status; 272cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.addr = mc_bank->mc_addr; 273cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.tsc = mc_bank->mc_tsc; 274cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.bank = mc_bank->mc_bank; 275cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong m.finished = 1; 276cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong /*log this record*/ 277cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong xen_mce_log(&m); 278cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 279cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mic = x86_mcinfo_next(mic); 280cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } while (1); 281cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 282cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return 0; 283cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong} 284cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 285cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic int mc_queue_handle(uint32_t flags) 286cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong{ 287cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong struct xen_mc mc_op; 288cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong int ret = 0; 289cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 290cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mc_op.cmd = XEN_MC_fetch; 291cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mc_op.interface_version = XEN_MCA_INTERFACE_VERSION; 292cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong set_xen_guest_handle(mc_op.u.mc_fetch.data, &g_mi); 293cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong do { 294cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mc_op.u.mc_fetch.flags = flags; 295cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong ret = HYPERVISOR_mca(&mc_op); 296cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (ret) { 297283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_err("Failed to fetch %surgent error log\n", 298283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches flags == XEN_MC_URGENT ? "" : "non"); 299cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong break; 300cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 301cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 302cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (mc_op.u.mc_fetch.flags & XEN_MC_NODATA || 303cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mc_op.u.mc_fetch.flags & XEN_MC_FETCHFAILED) 304cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong break; 305cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong else { 306cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong ret = convert_log(&g_mi); 307cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (ret) 308283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_warn("Failed to convert this error log, continue acking it anyway\n"); 309cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 310cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mc_op.u.mc_fetch.flags = flags | XEN_MC_ACK; 311cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong ret = HYPERVISOR_mca(&mc_op); 312cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (ret) { 313283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_err("Failed to ack previous error log\n"); 314cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong break; 315cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 316cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 317cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } while (1); 318cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 319cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return ret; 320cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong} 321cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 322cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong/* virq handler for machine check error info*/ 3231b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsongstatic void xen_mce_work_fn(struct work_struct *work) 324cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong{ 325cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong int err; 326cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 3271b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsong mutex_lock(&mcelog_lock); 328cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 329cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong /* urgent mc_info */ 330cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong err = mc_queue_handle(XEN_MC_URGENT); 331cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (err) 332283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_err("Failed to handle urgent mc_info queue, continue handling nonurgent mc_info queue anyway\n"); 333cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 334cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong /* nonurgent mc_info */ 335cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong err = mc_queue_handle(XEN_MC_NONURGENT); 336cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (err) 337283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_err("Failed to handle nonurgent mc_info queue\n"); 338cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 339a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong /* wake processes polling /dev/mcelog */ 340a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong wake_up_interruptible(&xen_mce_chrdev_wait); 341a867e5d6b362fc2982f19c5223f1946292643e57Liu, Jinsong 3421b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsong mutex_unlock(&mcelog_lock); 3431b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsong} 3441b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsongstatic DECLARE_WORK(xen_mce_work, xen_mce_work_fn); 345cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 3461b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsongstatic irqreturn_t xen_mce_interrupt(int irq, void *dev_id) 3471b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsong{ 3481b2a05516e42149a5e9f0f5aeba2c7fa9574b3f4Liu, Jinsong schedule_work(&xen_mce_work); 349cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return IRQ_HANDLED; 350cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong} 351cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 352cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic int bind_virq_for_mce(void) 353cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong{ 354cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong int ret; 355cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong struct xen_mc mc_op; 356cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 357cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong memset(&mc_op, 0, sizeof(struct xen_mc)); 358cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 359cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong /* Fetch physical CPU Numbers */ 360cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mc_op.cmd = XEN_MC_physcpuinfo; 361cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong mc_op.interface_version = XEN_MCA_INTERFACE_VERSION; 362cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong set_xen_guest_handle(mc_op.u.mc_physcpuinfo.info, g_physinfo); 363cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong ret = HYPERVISOR_mca(&mc_op); 364cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (ret) { 365283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_err("Failed to get CPU numbers\n"); 366cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return ret; 367cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 368cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 369cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong /* Fetch each CPU Physical Info for later reference*/ 370cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong ncpus = mc_op.u.mc_physcpuinfo.ncpus; 371cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong g_physinfo = kcalloc(ncpus, sizeof(struct mcinfo_logical_cpu), 372cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong GFP_KERNEL); 373cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (!g_physinfo) 374cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return -ENOMEM; 375cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong set_xen_guest_handle(mc_op.u.mc_physcpuinfo.info, g_physinfo); 376cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong ret = HYPERVISOR_mca(&mc_op); 377cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (ret) { 378283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_err("Failed to get CPU info\n"); 379cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong kfree(g_physinfo); 380cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return ret; 381cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 382cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 383cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong ret = bind_virq_to_irqhandler(VIRQ_MCA, 0, 384cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong xen_mce_interrupt, 0, "mce", NULL); 385cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (ret < 0) { 386283c0972d53769ee44750cad4c27e3f5fa26ec1fJoe Perches pr_err("Failed to bind virq\n"); 387cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong kfree(g_physinfo); 388cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return ret; 389cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 390cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 391cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return 0; 392cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong} 393cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 394cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongstatic int __init xen_late_init_mcelog(void) 395cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong{ 396cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong /* Only DOM0 is responsible for MCE logging */ 397cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (xen_initial_domain()) { 398cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong /* register character device /dev/mcelog for xen mcelog */ 399cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong if (misc_register(&xen_mce_chrdev_device)) 400cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return -ENODEV; 401cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return bind_virq_for_mce(); 402cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong } 403cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong 404cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong return -ENODEV; 405cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsong} 406cef12ee52b054282461a6d5fe7742755fa6e3bd3Liu, Jinsongdevice_initcall(xen_late_init_mcelog); 407