msr.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
  8. * USA; either version 2 of the License, or (at your option) any later
  9. * version; incorporated herein by reference.
  10. *
  11. * ----------------------------------------------------------------------- */
  12. /*
  13. * x86 MSR access device
  14. *
  15. * This device is accessed by lseek() to the appropriate register number
  16. * and then read/write in chunks of 8 bytes. A larger size means multiple
  17. * reads or writes of the same register.
  18. *
  19. * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
  20. * an SMP box will direct the access to CPU %d.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/init.h>
  27. #include <linux/poll.h>
  28. #include <linux/smp.h>
  29. #include <linux/smp_lock.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 <asm/processor.h>
  37. #include <asm/msr.h>
  38. #include <asm/system.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. if (err == -EFAULT) /* Fix idiotic error code */
  75. err = -EIO;
  76. break;
  77. }
  78. if (copy_to_user(tmp, &data, 8)) {
  79. err = -EFAULT;
  80. break;
  81. }
  82. tmp += 2;
  83. bytes += 8;
  84. }
  85. return bytes ? bytes : err;
  86. }
  87. static ssize_t msr_write(struct file *file, const char __user *buf,
  88. size_t count, loff_t *ppos)
  89. {
  90. const u32 __user *tmp = (const u32 __user *)buf;
  91. u32 data[2];
  92. u32 reg = *ppos;
  93. int cpu = iminor(file->f_path.dentry->d_inode);
  94. int err = 0;
  95. ssize_t bytes = 0;
  96. if (count % 8)
  97. return -EINVAL; /* Invalid chunk size */
  98. for (; count; count -= 8) {
  99. if (copy_from_user(&data, tmp, 8)) {
  100. err = -EFAULT;
  101. break;
  102. }
  103. err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
  104. if (err) {
  105. if (err == -EFAULT) /* Fix idiotic error code */
  106. err = -EIO;
  107. break;
  108. }
  109. tmp += 2;
  110. bytes += 8;
  111. }
  112. return bytes ? bytes : err;
  113. }
  114. static int msr_open(struct inode *inode, struct file *file)
  115. {
  116. unsigned int cpu = iminor(file->f_path.dentry->d_inode);
  117. struct cpuinfo_x86 *c = &cpu_data(cpu);
  118. int ret = 0;
  119. lock_kernel();
  120. cpu = iminor(file->f_path.dentry->d_inode);
  121. if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
  122. ret = -ENXIO; /* No such CPU */
  123. goto out;
  124. }
  125. c = &cpu_data(cpu);
  126. if (!cpu_has(c, X86_FEATURE_MSR))
  127. ret = -EIO; /* MSR not supported */
  128. out:
  129. unlock_kernel();
  130. return ret;
  131. }
  132. /*
  133. * File operations we support
  134. */
  135. static const struct file_operations msr_fops = {
  136. .owner = THIS_MODULE,
  137. .llseek = msr_seek,
  138. .read = msr_read,
  139. .write = msr_write,
  140. .open = msr_open,
  141. };
  142. static int __cpuinit msr_device_create(int cpu)
  143. {
  144. struct device *dev;
  145. dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
  146. "msr%d", cpu);
  147. return IS_ERR(dev) ? PTR_ERR(dev) : 0;
  148. }
  149. static void msr_device_destroy(int cpu)
  150. {
  151. device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  152. }
  153. static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb,
  154. unsigned long action, void *hcpu)
  155. {
  156. unsigned int cpu = (unsigned long)hcpu;
  157. int err = 0;
  158. switch (action) {
  159. case CPU_UP_PREPARE:
  160. err = msr_device_create(cpu);
  161. break;
  162. case CPU_UP_CANCELED:
  163. case CPU_UP_CANCELED_FROZEN:
  164. case CPU_DEAD:
  165. msr_device_destroy(cpu);
  166. break;
  167. }
  168. return err ? NOTIFY_BAD : NOTIFY_OK;
  169. }
  170. static struct notifier_block __refdata msr_class_cpu_notifier = {
  171. .notifier_call = msr_class_cpu_callback,
  172. };
  173. static char *msr_nodename(struct device *dev)
  174. {
  175. return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
  176. }
  177. static int __init msr_init(void)
  178. {
  179. int i, err = 0;
  180. i = 0;
  181. if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
  182. printk(KERN_ERR "msr: unable to get major %d for msr\n",
  183. MSR_MAJOR);
  184. err = -EBUSY;
  185. goto out;
  186. }
  187. msr_class = class_create(THIS_MODULE, "msr");
  188. if (IS_ERR(msr_class)) {
  189. err = PTR_ERR(msr_class);
  190. goto out_chrdev;
  191. }
  192. msr_class->nodename = msr_nodename;
  193. for_each_online_cpu(i) {
  194. err = msr_device_create(i);
  195. if (err != 0)
  196. goto out_class;
  197. }
  198. register_hotcpu_notifier(&msr_class_cpu_notifier);
  199. err = 0;
  200. goto out;
  201. out_class:
  202. i = 0;
  203. for_each_online_cpu(i)
  204. msr_device_destroy(i);
  205. class_destroy(msr_class);
  206. out_chrdev:
  207. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  208. out:
  209. return err;
  210. }
  211. static void __exit msr_exit(void)
  212. {
  213. int cpu = 0;
  214. for_each_online_cpu(cpu)
  215. msr_device_destroy(cpu);
  216. class_destroy(msr_class);
  217. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  218. unregister_hotcpu_notifier(&msr_class_cpu_notifier);
  219. }
  220. module_init(msr_init);
  221. module_exit(msr_exit)
  222. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  223. MODULE_DESCRIPTION("x86 generic MSR driver");
  224. MODULE_LICENSE("GPL");