msr.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/smp_lock.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 <asm/processor.h>
  38. #include <asm/msr.h>
  39. #include <asm/system.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->f_mapping->host;
  45. mutex_lock(&inode->i_mutex);
  46. switch (orig) {
  47. case 0:
  48. file->f_pos = offset;
  49. ret = file->f_pos;
  50. break;
  51. case 1:
  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->f_path.dentry->d_inode);
  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->f_path.dentry->d_inode);
  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->f_path.dentry->d_inode);
  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->f_path.dentry->d_inode);
  155. struct cpuinfo_x86 *c = &cpu_data(cpu);
  156. int ret = 0;
  157. lock_kernel();
  158. cpu = iminor(file->f_path.dentry->d_inode);
  159. if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
  160. ret = -ENXIO; /* No such CPU */
  161. goto out;
  162. }
  163. c = &cpu_data(cpu);
  164. if (!cpu_has(c, X86_FEATURE_MSR))
  165. ret = -EIO; /* MSR not supported */
  166. out:
  167. unlock_kernel();
  168. return ret;
  169. }
  170. /*
  171. * File operations we support
  172. */
  173. static const struct file_operations msr_fops = {
  174. .owner = THIS_MODULE,
  175. .llseek = msr_seek,
  176. .read = msr_read,
  177. .write = msr_write,
  178. .open = msr_open,
  179. .unlocked_ioctl = msr_ioctl,
  180. .compat_ioctl = msr_ioctl,
  181. };
  182. static int __cpuinit msr_device_create(int cpu)
  183. {
  184. struct device *dev;
  185. dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
  186. "msr%d", cpu);
  187. return IS_ERR(dev) ? PTR_ERR(dev) : 0;
  188. }
  189. static void msr_device_destroy(int cpu)
  190. {
  191. device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  192. }
  193. static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb,
  194. unsigned long action, void *hcpu)
  195. {
  196. unsigned int cpu = (unsigned long)hcpu;
  197. int err = 0;
  198. switch (action) {
  199. case CPU_UP_PREPARE:
  200. err = msr_device_create(cpu);
  201. break;
  202. case CPU_UP_CANCELED:
  203. case CPU_UP_CANCELED_FROZEN:
  204. case CPU_DEAD:
  205. msr_device_destroy(cpu);
  206. break;
  207. }
  208. return err ? NOTIFY_BAD : NOTIFY_OK;
  209. }
  210. static struct notifier_block __refdata msr_class_cpu_notifier = {
  211. .notifier_call = msr_class_cpu_callback,
  212. };
  213. static char *msr_devnode(struct device *dev, mode_t *mode)
  214. {
  215. return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
  216. }
  217. static int __init msr_init(void)
  218. {
  219. int i, err = 0;
  220. i = 0;
  221. if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
  222. printk(KERN_ERR "msr: unable to get major %d for msr\n",
  223. MSR_MAJOR);
  224. err = -EBUSY;
  225. goto out;
  226. }
  227. msr_class = class_create(THIS_MODULE, "msr");
  228. if (IS_ERR(msr_class)) {
  229. err = PTR_ERR(msr_class);
  230. goto out_chrdev;
  231. }
  232. msr_class->devnode = msr_devnode;
  233. for_each_online_cpu(i) {
  234. err = msr_device_create(i);
  235. if (err != 0)
  236. goto out_class;
  237. }
  238. register_hotcpu_notifier(&msr_class_cpu_notifier);
  239. err = 0;
  240. goto out;
  241. out_class:
  242. i = 0;
  243. for_each_online_cpu(i)
  244. msr_device_destroy(i);
  245. class_destroy(msr_class);
  246. out_chrdev:
  247. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  248. out:
  249. return err;
  250. }
  251. static void __exit msr_exit(void)
  252. {
  253. int cpu = 0;
  254. for_each_online_cpu(cpu)
  255. msr_device_destroy(cpu);
  256. class_destroy(msr_class);
  257. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  258. unregister_hotcpu_notifier(&msr_class_cpu_notifier);
  259. }
  260. module_init(msr_init);
  261. module_exit(msr_exit)
  262. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  263. MODULE_DESCRIPTION("x86 generic MSR driver");
  264. MODULE_LICENSE("GPL");