msr.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
  4. * Copyright 2009 Intel Corporation; author: H. Peter Anvin
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
  9. * USA; either version 2 of the License, or (at your option) any later
  10. * version; incorporated herein by reference.
  11. *
  12. * ----------------------------------------------------------------------- */
  13. /*
  14. * x86 MSR access device
  15. *
  16. * This device is accessed by lseek() to the appropriate register number
  17. * and then read/write in chunks of 8 bytes. A larger size means multiple
  18. * reads or writes of the same register.
  19. *
  20. * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
  21. * an SMP box will direct the access to CPU %d.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/errno.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/init.h>
  29. #include <linux/poll.h>
  30. #include <linux/smp.h>
  31. #include <linux/major.h>
  32. #include <linux/fs.h>
  33. #include <linux/device.h>
  34. #include <linux/cpu.h>
  35. #include <linux/notifier.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/gfp.h>
  38. #include <asm/cpufeature.h>
  39. #include <asm/msr.h>
  40. static struct class *msr_class;
  41. static loff_t msr_seek(struct file *file, loff_t offset, int orig)
  42. {
  43. loff_t ret;
  44. struct inode *inode = file_inode(file);
  45. mutex_lock(&inode->i_mutex);
  46. switch (orig) {
  47. case SEEK_SET:
  48. file->f_pos = offset;
  49. ret = file->f_pos;
  50. break;
  51. case SEEK_CUR:
  52. file->f_pos += offset;
  53. ret = file->f_pos;
  54. break;
  55. default:
  56. ret = -EINVAL;
  57. }
  58. mutex_unlock(&inode->i_mutex);
  59. return ret;
  60. }
  61. static ssize_t msr_read(struct file *file, char __user *buf,
  62. size_t count, loff_t *ppos)
  63. {
  64. u32 __user *tmp = (u32 __user *) buf;
  65. u32 data[2];
  66. u32 reg = *ppos;
  67. int cpu = iminor(file_inode(file));
  68. int err = 0;
  69. ssize_t bytes = 0;
  70. if (count % 8)
  71. return -EINVAL; /* Invalid chunk size */
  72. for (; count; count -= 8) {
  73. err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
  74. if (err)
  75. break;
  76. if (copy_to_user(tmp, &data, 8)) {
  77. err = -EFAULT;
  78. break;
  79. }
  80. tmp += 2;
  81. bytes += 8;
  82. }
  83. return bytes ? bytes : err;
  84. }
  85. static ssize_t msr_write(struct file *file, const char __user *buf,
  86. size_t count, loff_t *ppos)
  87. {
  88. const u32 __user *tmp = (const u32 __user *)buf;
  89. u32 data[2];
  90. u32 reg = *ppos;
  91. int cpu = iminor(file_inode(file));
  92. int err = 0;
  93. ssize_t bytes = 0;
  94. if (count % 8)
  95. return -EINVAL; /* Invalid chunk size */
  96. for (; count; count -= 8) {
  97. if (copy_from_user(&data, tmp, 8)) {
  98. err = -EFAULT;
  99. break;
  100. }
  101. err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
  102. if (err)
  103. break;
  104. tmp += 2;
  105. bytes += 8;
  106. }
  107. return bytes ? bytes : err;
  108. }
  109. static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
  110. {
  111. u32 __user *uregs = (u32 __user *)arg;
  112. u32 regs[8];
  113. int cpu = iminor(file_inode(file));
  114. int err;
  115. switch (ioc) {
  116. case X86_IOC_RDMSR_REGS:
  117. if (!(file->f_mode & FMODE_READ)) {
  118. err = -EBADF;
  119. break;
  120. }
  121. if (copy_from_user(&regs, uregs, sizeof regs)) {
  122. err = -EFAULT;
  123. break;
  124. }
  125. err = rdmsr_safe_regs_on_cpu(cpu, regs);
  126. if (err)
  127. break;
  128. if (copy_to_user(uregs, &regs, sizeof regs))
  129. err = -EFAULT;
  130. break;
  131. case X86_IOC_WRMSR_REGS:
  132. if (!(file->f_mode & FMODE_WRITE)) {
  133. err = -EBADF;
  134. break;
  135. }
  136. if (copy_from_user(&regs, uregs, sizeof regs)) {
  137. err = -EFAULT;
  138. break;
  139. }
  140. err = wrmsr_safe_regs_on_cpu(cpu, regs);
  141. if (err)
  142. break;
  143. if (copy_to_user(uregs, &regs, sizeof regs))
  144. err = -EFAULT;
  145. break;
  146. default:
  147. err = -ENOTTY;
  148. break;
  149. }
  150. return err;
  151. }
  152. static int msr_open(struct inode *inode, struct file *file)
  153. {
  154. unsigned int cpu = iminor(file_inode(file));
  155. struct cpuinfo_x86 *c;
  156. if (!capable(CAP_SYS_RAWIO))
  157. return -EPERM;
  158. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  159. return -ENXIO; /* No such CPU */
  160. c = &cpu_data(cpu);
  161. if (!cpu_has(c, X86_FEATURE_MSR))
  162. return -EIO; /* MSR not supported */
  163. return 0;
  164. }
  165. /*
  166. * File operations we support
  167. */
  168. static const struct file_operations msr_fops = {
  169. .owner = THIS_MODULE,
  170. .llseek = msr_seek,
  171. .read = msr_read,
  172. .write = msr_write,
  173. .open = msr_open,
  174. .unlocked_ioctl = msr_ioctl,
  175. .compat_ioctl = msr_ioctl,
  176. };
  177. static int msr_device_create(int cpu)
  178. {
  179. struct device *dev;
  180. dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
  181. "msr%d", cpu);
  182. return PTR_ERR_OR_ZERO(dev);
  183. }
  184. static void msr_device_destroy(int cpu)
  185. {
  186. device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  187. }
  188. static int msr_class_cpu_callback(struct notifier_block *nfb,
  189. unsigned long action, void *hcpu)
  190. {
  191. unsigned int cpu = (unsigned long)hcpu;
  192. int err = 0;
  193. switch (action) {
  194. case CPU_UP_PREPARE:
  195. err = msr_device_create(cpu);
  196. break;
  197. case CPU_UP_CANCELED:
  198. case CPU_UP_CANCELED_FROZEN:
  199. case CPU_DEAD:
  200. msr_device_destroy(cpu);
  201. break;
  202. }
  203. return notifier_from_errno(err);
  204. }
  205. static struct notifier_block __refdata msr_class_cpu_notifier = {
  206. .notifier_call = msr_class_cpu_callback,
  207. };
  208. static char *msr_devnode(struct device *dev, umode_t *mode)
  209. {
  210. return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
  211. }
  212. static int __init msr_init(void)
  213. {
  214. int i, err = 0;
  215. i = 0;
  216. if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
  217. pr_err("unable to get major %d for msr\n", MSR_MAJOR);
  218. err = -EBUSY;
  219. goto out;
  220. }
  221. msr_class = class_create(THIS_MODULE, "msr");
  222. if (IS_ERR(msr_class)) {
  223. err = PTR_ERR(msr_class);
  224. goto out_chrdev;
  225. }
  226. msr_class->devnode = msr_devnode;
  227. cpu_notifier_register_begin();
  228. for_each_online_cpu(i) {
  229. err = msr_device_create(i);
  230. if (err != 0)
  231. goto out_class;
  232. }
  233. __register_hotcpu_notifier(&msr_class_cpu_notifier);
  234. cpu_notifier_register_done();
  235. err = 0;
  236. goto out;
  237. out_class:
  238. i = 0;
  239. for_each_online_cpu(i)
  240. msr_device_destroy(i);
  241. cpu_notifier_register_done();
  242. class_destroy(msr_class);
  243. out_chrdev:
  244. __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
  245. out:
  246. return err;
  247. }
  248. static void __exit msr_exit(void)
  249. {
  250. int cpu = 0;
  251. cpu_notifier_register_begin();
  252. for_each_online_cpu(cpu)
  253. msr_device_destroy(cpu);
  254. class_destroy(msr_class);
  255. __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
  256. __unregister_hotcpu_notifier(&msr_class_cpu_notifier);
  257. cpu_notifier_register_done();
  258. }
  259. module_init(msr_init);
  260. module_exit(msr_exit)
  261. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  262. MODULE_DESCRIPTION("x86 generic MSR driver");
  263. MODULE_LICENSE("GPL");