msr.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/errno.h>
  26. #include <linux/fcntl.h>
  27. #include <linux/init.h>
  28. #include <linux/poll.h>
  29. #include <linux/smp.h>
  30. #include <linux/major.h>
  31. #include <linux/fs.h>
  32. #include <linux/device.h>
  33. #include <linux/cpu.h>
  34. #include <linux/notifier.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/gfp.h>
  37. #include <asm/processor.h>
  38. #include <asm/msr.h>
  39. static struct class *msr_class;
  40. static loff_t msr_seek(struct file *file, loff_t offset, int orig)
  41. {
  42. loff_t ret;
  43. struct inode *inode = file->f_mapping->host;
  44. mutex_lock(&inode->i_mutex);
  45. switch (orig) {
  46. case 0:
  47. file->f_pos = offset;
  48. ret = file->f_pos;
  49. break;
  50. case 1:
  51. file->f_pos += offset;
  52. ret = file->f_pos;
  53. break;
  54. default:
  55. ret = -EINVAL;
  56. }
  57. mutex_unlock(&inode->i_mutex);
  58. return ret;
  59. }
  60. static ssize_t msr_read(struct file *file, char __user *buf,
  61. size_t count, loff_t *ppos)
  62. {
  63. u32 __user *tmp = (u32 __user *) buf;
  64. u32 data[2];
  65. u32 reg = *ppos;
  66. int cpu = iminor(file->f_path.dentry->d_inode);
  67. int err = 0;
  68. ssize_t bytes = 0;
  69. if (count % 8)
  70. return -EINVAL; /* Invalid chunk size */
  71. for (; count; count -= 8) {
  72. err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
  73. if (err)
  74. break;
  75. if (copy_to_user(tmp, &data, 8)) {
  76. err = -EFAULT;
  77. break;
  78. }
  79. tmp += 2;
  80. bytes += 8;
  81. }
  82. return bytes ? bytes : err;
  83. }
  84. static ssize_t msr_write(struct file *file, const char __user *buf,
  85. size_t count, loff_t *ppos)
  86. {
  87. const u32 __user *tmp = (const u32 __user *)buf;
  88. u32 data[2];
  89. u32 reg = *ppos;
  90. int cpu = iminor(file->f_path.dentry->d_inode);
  91. int err = 0;
  92. ssize_t bytes = 0;
  93. if (count % 8)
  94. return -EINVAL; /* Invalid chunk size */
  95. for (; count; count -= 8) {
  96. if (copy_from_user(&data, tmp, 8)) {
  97. err = -EFAULT;
  98. break;
  99. }
  100. err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
  101. if (err)
  102. break;
  103. tmp += 2;
  104. bytes += 8;
  105. }
  106. return bytes ? bytes : err;
  107. }
  108. static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
  109. {
  110. u32 __user *uregs = (u32 __user *)arg;
  111. u32 regs[8];
  112. int cpu = iminor(file->f_path.dentry->d_inode);
  113. int err;
  114. switch (ioc) {
  115. case X86_IOC_RDMSR_REGS:
  116. if (!(file->f_mode & FMODE_READ)) {
  117. err = -EBADF;
  118. break;
  119. }
  120. if (copy_from_user(&regs, uregs, sizeof regs)) {
  121. err = -EFAULT;
  122. break;
  123. }
  124. err = rdmsr_safe_regs_on_cpu(cpu, regs);
  125. if (err)
  126. break;
  127. if (copy_to_user(uregs, &regs, sizeof regs))
  128. err = -EFAULT;
  129. break;
  130. case X86_IOC_WRMSR_REGS:
  131. if (!(file->f_mode & FMODE_WRITE)) {
  132. err = -EBADF;
  133. break;
  134. }
  135. if (copy_from_user(&regs, uregs, sizeof regs)) {
  136. err = -EFAULT;
  137. break;
  138. }
  139. err = wrmsr_safe_regs_on_cpu(cpu, regs);
  140. if (err)
  141. break;
  142. if (copy_to_user(uregs, &regs, sizeof regs))
  143. err = -EFAULT;
  144. break;
  145. default:
  146. err = -ENOTTY;
  147. break;
  148. }
  149. return err;
  150. }
  151. static int msr_open(struct inode *inode, struct file *file)
  152. {
  153. unsigned int cpu;
  154. struct cpuinfo_x86 *c;
  155. if (!capable(CAP_SYS_RAWIO))
  156. return -EPERM;
  157. cpu = iminor(file->f_path.dentry->d_inode);
  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 __cpuinit 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 IS_ERR(dev) ? PTR_ERR(dev) : 0;
  183. }
  184. static void msr_device_destroy(int cpu)
  185. {
  186. device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  187. }
  188. static int __cpuinit 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. printk(KERN_ERR "msr: unable to get major %d for msr\n",
  218. MSR_MAJOR);
  219. err = -EBUSY;
  220. goto out;
  221. }
  222. msr_class = class_create(THIS_MODULE, "msr");
  223. if (IS_ERR(msr_class)) {
  224. err = PTR_ERR(msr_class);
  225. goto out_chrdev;
  226. }
  227. msr_class->devnode = msr_devnode;
  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. err = 0;
  235. goto out;
  236. out_class:
  237. i = 0;
  238. for_each_online_cpu(i)
  239. msr_device_destroy(i);
  240. class_destroy(msr_class);
  241. out_chrdev:
  242. __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
  243. out:
  244. return err;
  245. }
  246. static void __exit msr_exit(void)
  247. {
  248. int cpu = 0;
  249. for_each_online_cpu(cpu)
  250. msr_device_destroy(cpu);
  251. class_destroy(msr_class);
  252. __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
  253. unregister_hotcpu_notifier(&msr_class_cpu_notifier);
  254. }
  255. module_init(msr_init);
  256. module_exit(msr_exit)
  257. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  258. MODULE_DESCRIPTION("x86 generic MSR driver");
  259. MODULE_LICENSE("GPL");