msr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* ----------------------------------------------------------------------- *
  3. *
  4. * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
  5. * Copyright 2009 Intel Corporation; author: H. Peter Anvin
  6. *
  7. * ----------------------------------------------------------------------- */
  8. /*
  9. * x86 MSR access device
  10. *
  11. * This device is accessed by lseek() to the appropriate register number
  12. * and then read/write in chunks of 8 bytes. A larger size means multiple
  13. * reads or writes of the same register.
  14. *
  15. * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
  16. * an SMP box will direct the access to CPU %d.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/errno.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/init.h>
  24. #include <linux/poll.h>
  25. #include <linux/smp.h>
  26. #include <linux/major.h>
  27. #include <linux/fs.h>
  28. #include <linux/device.h>
  29. #include <linux/cpu.h>
  30. #include <linux/notifier.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/gfp.h>
  33. #include <linux/security.h>
  34. #include <asm/cpufeature.h>
  35. #include <asm/msr.h>
  36. static enum cpuhp_state cpuhp_msr_state;
  37. enum allow_write_msrs {
  38. MSR_WRITES_ON,
  39. MSR_WRITES_OFF,
  40. MSR_WRITES_DEFAULT,
  41. };
  42. static enum allow_write_msrs allow_writes = MSR_WRITES_DEFAULT;
  43. static ssize_t msr_read(struct file *file, char __user *buf,
  44. size_t count, loff_t *ppos)
  45. {
  46. u32 __user *tmp = (u32 __user *) buf;
  47. u32 data[2];
  48. u32 reg = *ppos;
  49. int cpu = iminor(file_inode(file));
  50. int err = 0;
  51. ssize_t bytes = 0;
  52. if (count % 8)
  53. return -EINVAL; /* Invalid chunk size */
  54. for (; count; count -= 8) {
  55. err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
  56. if (err)
  57. break;
  58. if (copy_to_user(tmp, &data, 8)) {
  59. err = -EFAULT;
  60. break;
  61. }
  62. tmp += 2;
  63. bytes += 8;
  64. }
  65. return bytes ? bytes : err;
  66. }
  67. static int filter_write(u32 reg)
  68. {
  69. /*
  70. * MSRs writes usually happen all at once, and can easily saturate kmsg.
  71. * Only allow one message every 30 seconds.
  72. *
  73. * It's possible to be smarter here and do it (for example) per-MSR, but
  74. * it would certainly be more complex, and this is enough at least to
  75. * avoid saturating the ring buffer.
  76. */
  77. static DEFINE_RATELIMIT_STATE(fw_rs, 30 * HZ, 1);
  78. switch (allow_writes) {
  79. case MSR_WRITES_ON: return 0;
  80. case MSR_WRITES_OFF: return -EPERM;
  81. default: break;
  82. }
  83. if (!__ratelimit(&fw_rs))
  84. return 0;
  85. pr_warn("Write to unrecognized MSR 0x%x by %s (pid: %d).\n",
  86. reg, current->comm, current->pid);
  87. pr_warn("See https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/about for details.\n");
  88. return 0;
  89. }
  90. static ssize_t msr_write(struct file *file, const char __user *buf,
  91. size_t count, loff_t *ppos)
  92. {
  93. const u32 __user *tmp = (const u32 __user *)buf;
  94. u32 data[2];
  95. u32 reg = *ppos;
  96. int cpu = iminor(file_inode(file));
  97. int err = 0;
  98. ssize_t bytes = 0;
  99. err = security_locked_down(LOCKDOWN_MSR);
  100. if (err)
  101. return err;
  102. err = filter_write(reg);
  103. if (err)
  104. return err;
  105. if (count % 8)
  106. return -EINVAL; /* Invalid chunk size */
  107. for (; count; count -= 8) {
  108. if (copy_from_user(&data, tmp, 8)) {
  109. err = -EFAULT;
  110. break;
  111. }
  112. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  113. err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
  114. if (err)
  115. break;
  116. tmp += 2;
  117. bytes += 8;
  118. }
  119. return bytes ? bytes : err;
  120. }
  121. static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
  122. {
  123. u32 __user *uregs = (u32 __user *)arg;
  124. u32 regs[8];
  125. int cpu = iminor(file_inode(file));
  126. int err;
  127. switch (ioc) {
  128. case X86_IOC_RDMSR_REGS:
  129. if (!(file->f_mode & FMODE_READ)) {
  130. err = -EBADF;
  131. break;
  132. }
  133. if (copy_from_user(&regs, uregs, sizeof(regs))) {
  134. err = -EFAULT;
  135. break;
  136. }
  137. err = rdmsr_safe_regs_on_cpu(cpu, regs);
  138. if (err)
  139. break;
  140. if (copy_to_user(uregs, &regs, sizeof(regs)))
  141. err = -EFAULT;
  142. break;
  143. case X86_IOC_WRMSR_REGS:
  144. if (!(file->f_mode & FMODE_WRITE)) {
  145. err = -EBADF;
  146. break;
  147. }
  148. if (copy_from_user(&regs, uregs, sizeof(regs))) {
  149. err = -EFAULT;
  150. break;
  151. }
  152. err = security_locked_down(LOCKDOWN_MSR);
  153. if (err)
  154. break;
  155. err = filter_write(regs[1]);
  156. if (err)
  157. return err;
  158. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  159. err = wrmsr_safe_regs_on_cpu(cpu, regs);
  160. if (err)
  161. break;
  162. if (copy_to_user(uregs, &regs, sizeof(regs)))
  163. err = -EFAULT;
  164. break;
  165. default:
  166. err = -ENOTTY;
  167. break;
  168. }
  169. return err;
  170. }
  171. static int msr_open(struct inode *inode, struct file *file)
  172. {
  173. unsigned int cpu = iminor(file_inode(file));
  174. struct cpuinfo_x86 *c;
  175. if (!capable(CAP_SYS_RAWIO))
  176. return -EPERM;
  177. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  178. return -ENXIO; /* No such CPU */
  179. c = &cpu_data(cpu);
  180. if (!cpu_has(c, X86_FEATURE_MSR))
  181. return -EIO; /* MSR not supported */
  182. return 0;
  183. }
  184. /*
  185. * File operations we support
  186. */
  187. static const struct file_operations msr_fops = {
  188. .owner = THIS_MODULE,
  189. .llseek = no_seek_end_llseek,
  190. .read = msr_read,
  191. .write = msr_write,
  192. .open = msr_open,
  193. .unlocked_ioctl = msr_ioctl,
  194. .compat_ioctl = msr_ioctl,
  195. };
  196. static char *msr_devnode(const struct device *dev, umode_t *mode)
  197. {
  198. return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
  199. }
  200. static const struct class msr_class = {
  201. .name = "msr",
  202. .devnode = msr_devnode,
  203. };
  204. static int msr_device_create(unsigned int cpu)
  205. {
  206. struct device *dev;
  207. dev = device_create(&msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
  208. "msr%d", cpu);
  209. return PTR_ERR_OR_ZERO(dev);
  210. }
  211. static int msr_device_destroy(unsigned int cpu)
  212. {
  213. device_destroy(&msr_class, MKDEV(MSR_MAJOR, cpu));
  214. return 0;
  215. }
  216. static int __init msr_init(void)
  217. {
  218. int err;
  219. if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
  220. pr_err("unable to get major %d for msr\n", MSR_MAJOR);
  221. return -EBUSY;
  222. }
  223. err = class_register(&msr_class);
  224. if (err)
  225. goto out_chrdev;
  226. err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online",
  227. msr_device_create, msr_device_destroy);
  228. if (err < 0)
  229. goto out_class;
  230. cpuhp_msr_state = err;
  231. return 0;
  232. out_class:
  233. class_unregister(&msr_class);
  234. out_chrdev:
  235. __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
  236. return err;
  237. }
  238. module_init(msr_init);
  239. static void __exit msr_exit(void)
  240. {
  241. cpuhp_remove_state(cpuhp_msr_state);
  242. class_unregister(&msr_class);
  243. __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
  244. }
  245. module_exit(msr_exit)
  246. static int set_allow_writes(const char *val, const struct kernel_param *cp)
  247. {
  248. /* val is NUL-terminated, see kernfs_fop_write() */
  249. char *s = strstrip((char *)val);
  250. if (!strcmp(s, "on"))
  251. allow_writes = MSR_WRITES_ON;
  252. else if (!strcmp(s, "off"))
  253. allow_writes = MSR_WRITES_OFF;
  254. else
  255. allow_writes = MSR_WRITES_DEFAULT;
  256. return 0;
  257. }
  258. static int get_allow_writes(char *buf, const struct kernel_param *kp)
  259. {
  260. const char *res;
  261. switch (allow_writes) {
  262. case MSR_WRITES_ON: res = "on"; break;
  263. case MSR_WRITES_OFF: res = "off"; break;
  264. default: res = "default"; break;
  265. }
  266. return sprintf(buf, "%s\n", res);
  267. }
  268. static const struct kernel_param_ops allow_writes_ops = {
  269. .set = set_allow_writes,
  270. .get = get_allow_writes
  271. };
  272. module_param_cb(allow_writes, &allow_writes_ops, NULL, 0600);
  273. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  274. MODULE_DESCRIPTION("x86 generic MSR driver");
  275. MODULE_LICENSE("GPL");