cregit-Linux how code gets into the kernel

Release 4.7 arch/x86/kernel/msr.c

Directory: arch/x86/kernel
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * x86 MSR access device
 *
 * This device is accessed by lseek() to the appropriate register number
 * and then read/write in chunks of 8 bytes.  A larger size means multiple
 * reads or writes of the same register.
 *
 * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
 * an SMP box will direct the access to CPU %d.
 */


#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/smp.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cpu.h>
#include <linux/notifier.h>
#include <linux/uaccess.h>
#include <linux/gfp.h>

#include <asm/cpufeature.h>
#include <asm/msr.h>


static struct class *msr_class;


static ssize_t msr_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { u32 __user *tmp = (u32 __user *) buf; u32 data[2]; u32 reg = *ppos; int cpu = iminor(file_inode(file)); int err = 0; ssize_t bytes = 0; if (count % 8) return -EINVAL; /* Invalid chunk size */ for (; count; count -= 8) { err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]); if (err) break; if (copy_to_user(tmp, &data, 8)) { err = -EFAULT; break; } tmp += 2; bytes += 8; } return bytes ? bytes : err; }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git12079.47%114.29%
h. peter anvinh. peter anvin2315.23%114.29%
al viroal viro63.97%342.86%
dave jonesdave jones10.66%114.29%
nicolas boichatnicolas boichat10.66%114.29%
Total151100.00%7100.00%


static ssize_t msr_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { const u32 __user *tmp = (const u32 __user *)buf; u32 data[2]; u32 reg = *ppos; int cpu = iminor(file_inode(file)); int err = 0; ssize_t bytes = 0; if (count % 8) return -EINVAL; /* Invalid chunk size */ for (; count; count -= 8) { if (copy_from_user(&data, tmp, 8)) { err = -EFAULT; break; } err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]); if (err) break; tmp += 2; bytes += 8; } return bytes ? bytes : err; }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git12179.61%114.29%
h. peter anvinh. peter anvin2315.13%114.29%
al viroal viro63.95%342.86%
nicolas boichatnicolas boichat10.66%114.29%
dave jonesdave jones10.66%114.29%
Total152100.00%7100.00%


static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg) { u32 __user *uregs = (u32 __user *)arg; u32 regs[8]; int cpu = iminor(file_inode(file)); int err; switch (ioc) { case X86_IOC_RDMSR_REGS: if (!(file->f_mode & FMODE_READ)) { err = -EBADF; break; } if (copy_from_user(&regs, uregs, sizeof regs)) { err = -EFAULT; break; } err = rdmsr_safe_regs_on_cpu(cpu, regs); if (err) break; if (copy_to_user(uregs, &regs, sizeof regs)) err = -EFAULT; break; case X86_IOC_WRMSR_REGS: if (!(file->f_mode & FMODE_WRITE)) { err = -EBADF; break; } if (copy_from_user(&regs, uregs, sizeof regs)) { err = -EFAULT; break; } err = wrmsr_safe_regs_on_cpu(cpu, regs); if (err) break; if (copy_to_user(uregs, &regs, sizeof regs)) err = -EFAULT; break; default: err = -ENOTTY; break; } return err; }

Contributors

PersonTokensPropCommitsCommitProp
h. peter anvinh. peter anvin21698.63%150.00%
al viroal viro31.37%150.00%
Total219100.00%2100.00%


static int msr_open(struct inode *inode, struct file *file) { unsigned int cpu = iminor(file_inode(file)); struct cpuinfo_x86 *c; if (!capable(CAP_SYS_RAWIO)) return -EPERM; if (cpu >= nr_cpu_ids || !cpu_online(cpu)) return -ENXIO; /* No such CPU */ c = &cpu_data(cpu); if (!cpu_has(c, X86_FEATURE_MSR)) return -EIO; /* MSR not supported */ return 0; }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git4450.00%327.27%
alan coxalan cox1213.64%19.09%
jonathan corbetjonathan corbet89.09%19.09%
al viroal viro89.09%19.09%
andrew mortonandrew morton77.95%218.18%
frederic weisbeckerfrederic weisbecker55.68%19.09%
brian gerstbrian gerst33.41%19.09%
mike travismike travis11.14%19.09%
Total88100.00%11100.00%

/* * File operations we support */ static const struct file_operations msr_fops = { .owner = THIS_MODULE, .llseek = no_seek_end_llseek, .read = msr_read, .write = msr_write, .open = msr_open, .unlocked_ioctl = msr_ioctl, .compat_ioctl = msr_ioctl, };
static int msr_device_create(int cpu) { struct device *dev; dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL, "msr%d", cpu); return PTR_ERR_OR_ZERO(dev); }

Contributors

PersonTokensPropCommitsCommitProp
hanna linderhanna linder2764.29%114.29%
greg kroah-hartmangreg kroah-hartman1023.81%457.14%
akinobu mitaakinobu mita49.52%114.29%
fabian frederickfabian frederick12.38%114.29%
Total42100.00%7100.00%


static void msr_device_destroy(int cpu) { device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu)); }

Contributors

PersonTokensPropCommitsCommitProp
akinobu mitaakinobu mita21100.00%1100.00%
Total21100.00%1100.00%


static int msr_class_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu) { unsigned int cpu = (unsigned long)hcpu; int err = 0; switch (action) { case CPU_UP_PREPARE: err = msr_device_create(cpu); break; case CPU_UP_CANCELED: case CPU_UP_CANCELED_FROZEN: case CPU_DEAD: msr_device_destroy(cpu); break; } return notifier_from_errno(err); }

Contributors

PersonTokensPropCommitsCommitProp
hanna linderhanna linder5170.83%116.67%
akinobu mitaakinobu mita1520.83%233.33%
rafael j. wysockirafael j. wysocki56.94%233.33%
greg kroah-hartmangreg kroah-hartman11.39%116.67%
Total72100.00%6100.00%

static struct notifier_block __refdata msr_class_cpu_notifier = { .notifier_call = msr_class_cpu_callback, };
static char *msr_devnode(struct device *dev, umode_t *mode) { return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt)); }

Contributors

PersonTokensPropCommitsCommitProp
kay sieverskay sievers3096.77%266.67%
al viroal viro13.23%133.33%
Total31100.00%3100.00%


static int __init msr_init(void) { int i, err = 0; i = 0; if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) { pr_err("unable to get major %d for msr\n", MSR_MAJOR); err = -EBUSY; goto out; } msr_class = class_create(THIS_MODULE, "msr"); if (IS_ERR(msr_class)) { err = PTR_ERR(msr_class); goto out_chrdev; } msr_class->devnode = msr_devnode; cpu_notifier_register_begin(); for_each_online_cpu(i) { err = msr_device_create(i); if (err != 0) goto out_class; } __register_hotcpu_notifier(&msr_class_cpu_notifier); cpu_notifier_register_done(); err = 0; goto out; out_class: i = 0; for_each_online_cpu(i) msr_device_destroy(i); cpu_notifier_register_done(); class_destroy(msr_class); out_chrdev: __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr"); out: return err; }

Contributors

PersonTokensPropCommitsCommitProp
hanna linderhanna linder10662.72%18.33%
pre-gitpre-git3017.75%18.33%
h. peter anvinh. peter anvin105.92%18.33%
kay sieverskay sievers63.55%216.67%
silas boyd-wickizersilas boyd-wickizer63.55%18.33%
srivatsa s. bhatsrivatsa s. bhat42.37%18.33%
greg kroah-hartmangreg kroah-hartman31.78%216.67%
fabian frederickfabian frederick21.18%18.33%
akinobu mitaakinobu mita10.59%18.33%
adrian bunkadrian bunk10.59%18.33%
Total169100.00%12100.00%


static void __exit msr_exit(void) { int cpu = 0; cpu_notifier_register_begin(); for_each_online_cpu(cpu) msr_device_destroy(cpu); class_destroy(msr_class); __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr"); __unregister_hotcpu_notifier(&msr_class_cpu_notifier); cpu_notifier_register_done(); }

Contributors

PersonTokensPropCommitsCommitProp
hanna linderhanna linder2243.14%111.11%
pre-gitpre-git1427.45%222.22%
russ andersonruss anderson59.80%111.11%
silas boyd-wickizersilas boyd-wickizer47.84%111.11%
srivatsa s. bhatsrivatsa s. bhat35.88%111.11%
greg kroah-hartmangreg kroah-hartman11.96%111.11%
akinobu mitaakinobu mita11.96%111.11%
adrian bunkadrian bunk11.96%111.11%
Total51100.00%9100.00%

module_init(msr_init); module_exit(msr_exit) MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>"); MODULE_DESCRIPTION("x86 generic MSR driver"); MODULE_LICENSE("GPL");

Overall Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git40035.09%48.33%
h. peter anvinh. peter anvin28324.82%36.25%
hanna linderhanna linder23220.35%12.08%
akinobu mitaakinobu mita423.68%24.17%
kay sieverskay sievers363.16%24.17%
al viroal viro252.19%510.42%
greg kroah-hartmangreg kroah-hartman161.40%510.42%
alan coxalan cox121.05%12.08%
rusty russellrusty russell100.88%12.08%
silas boyd-wickizersilas boyd-wickizer100.88%12.08%
fabian frederickfabian frederick100.88%24.17%
jonathan corbetjonathan corbet80.70%12.08%
andrew mortonandrew morton70.61%24.17%
srivatsa s. bhatsrivatsa s. bhat70.61%12.08%
dave jonesdave jones60.53%36.25%
linus torvaldslinus torvalds50.44%12.08%
frederic weisbeckerfrederic weisbecker50.44%12.08%
russ andersonruss anderson50.44%12.08%
rafael j. wysockirafael j. wysocki50.44%24.17%
tejun heotejun heo30.26%12.08%
brian gerstbrian gerst30.26%12.08%
jaswinder singh rajputjaswinder singh rajput20.18%12.08%
nicolas boichatnicolas boichat20.18%12.08%
adrian bunkadrian bunk20.18%12.08%
mike travismike travis10.09%12.08%
arjan van de venarjan van de ven10.09%12.08%
sam ravnborgsam ravnborg10.09%12.08%
borislav petkovborislav petkov10.09%12.08%
Total1140100.00%48100.00%
Directory: arch/x86/kernel
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
{% endraw %}