msr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 struct class *msr_class;
  37. static enum cpuhp_state cpuhp_msr_state;
  38. enum allow_write_msrs {
  39. MSR_WRITES_ON,
  40. MSR_WRITES_OFF,
  41. MSR_WRITES_DEFAULT,
  42. };
  43. static enum allow_write_msrs allow_writes = MSR_WRITES_DEFAULT;
  44. static ssize_t msr_read(struct file *file, char __user *buf,
  45. size_t count, loff_t *ppos)
  46. {
  47. u32 __user *tmp = (u32 __user *) buf;
  48. u32 data[2];
  49. u32 reg = *ppos;
  50. int cpu = iminor(file_inode(file));
  51. int err = 0;
  52. ssize_t bytes = 0;
  53. if (count % 8)
  54. return -EINVAL; /* Invalid chunk size */
  55. for (; count; count -= 8) {
  56. err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
  57. if (err)
  58. break;
  59. if (copy_to_user(tmp, &data, 8)) {
  60. err = -EFAULT;
  61. break;
  62. }
  63. tmp += 2;
  64. bytes += 8;
  65. }
  66. return bytes ? bytes : err;
  67. }
  68. static int filter_write(u32 reg)
  69. {
  70. /*
  71. * MSRs writes usually happen all at once, and can easily saturate kmsg.
  72. * Only allow one message every 30 seconds.
  73. *
  74. * It's possible to be smarter here and do it (for example) per-MSR, but
  75. * it would certainly be more complex, and this is enough at least to
  76. * avoid saturating the ring buffer.
  77. */
  78. static DEFINE_RATELIMIT_STATE(fw_rs, 30 * HZ, 1);
  79. switch (allow_writes) {
  80. case MSR_WRITES_ON: return 0;
  81. case MSR_WRITES_OFF: return -EPERM;
  82. default: break;
  83. }
  84. if (!__ratelimit(&fw_rs))
  85. return 0;
  86. pr_warn("Write to unrecognized MSR 0x%x by %s (pid: %d).\n",
  87. reg, current->comm, current->pid);
  88. pr_warn("See https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/about for details.\n");
  89. return 0;
  90. }
  91. static ssize_t msr_write(struct file *file, const char __user *buf,
  92. size_t count, loff_t *ppos)
  93. {
  94. const u32 __user *tmp = (const u32 __user *)buf;
  95. u32 data[2];
  96. u32 reg = *ppos;
  97. int cpu = iminor(file_inode(file));
  98. int err = 0;
  99. ssize_t bytes = 0;
  100. err = security_locked_down(LOCKDOWN_MSR);
  101. if (err)
  102. return err;
  103. err = filter_write(reg);
  104. if (err)
  105. return err;
  106. if (count % 8)
  107. return -EINVAL; /* Invalid chunk size */
  108. for (; count; count -= 8) {
  109. if (copy_from_user(&data, tmp, 8)) {
  110. err = -EFAULT;
  111. break;
  112. }
  113. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  114. err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
  115. if (err)
  116. break;
  117. tmp += 2;
  118. bytes += 8;
  119. }
  120. return bytes ? bytes : err;
  121. }
  122. static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
  123. {
  124. u32 __user *uregs = (u32 __user *)arg;
  125. u32 regs[8];
  126. int cpu = iminor(file_inode(file));
  127. int err;
  128. switch (ioc) {
  129. case X86_IOC_RDMSR_REGS:
  130. if (!(file->f_mode & FMODE_READ)) {
  131. err = -EBADF;
  132. break;
  133. }
  134. if (copy_from_user(&regs, uregs, sizeof(regs))) {
  135. err = -EFAULT;
  136. break;
  137. }
  138. err = rdmsr_safe_regs_on_cpu(cpu, regs);
  139. if (err)
  140. break;
  141. if (copy_to_user(uregs, &regs, sizeof(regs)))
  142. err = -EFAULT;
  143. break;
  144. case X86_IOC_WRMSR_REGS:
  145. if (!(file->f_mode & FMODE_WRITE)) {
  146. err = -EBADF;
  147. break;
  148. }
  149. if (copy_from_user(&regs, uregs, sizeof(regs))) {
  150. err = -EFAULT;
  151. break;
  152. }
  153. err = security_locked_down(LOCKDOWN_MSR);
  154. if (err)
  155. break;
  156. err = filter_write(regs[1]);
  157. if (err)
  158. return err;
  159. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  160. err = wrmsr_safe_regs_on_cpu(cpu, regs);
  161. if (err)
  162. break;
  163. if (copy_to_user(uregs, &regs, sizeof(regs)))
  164. err = -EFAULT;
  165. break;
  166. default:
  167. err = -ENOTTY;
  168. break;
  169. }
  170. return err;
  171. }
  172. static int msr_open(struct inode *inode, struct file *file)
  173. {
  174. unsigned int cpu = iminor(file_inode(file));
  175. struct cpuinfo_x86 *c;
  176. if (!capable(CAP_SYS_RAWIO))
  177. return -EPERM;
  178. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  179. return -ENXIO; /* No such CPU */
  180. c = &cpu_data(cpu);
  181. if (!cpu_has(c, X86_FEATURE_MSR))
  182. return -EIO; /* MSR not supported */
  183. return 0;
  184. }
  185. /*
  186. * File operations we support
  187. */
  188. static const struct file_operations msr_fops = {
  189. .owner = THIS_MODULE,
  190. .llseek = no_seek_end_llseek,
  191. .read = msr_read,
  192. .write = msr_write,
  193. .open = msr_open,
  194. .unlocked_ioctl = msr_ioctl,
  195. .compat_ioctl = msr_ioctl,
  196. };
  197. static int msr_device_create(unsigned int cpu)
  198. {
  199. struct device *dev;
  200. dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
  201. "msr%d", cpu);
  202. return PTR_ERR_OR_ZERO(dev);
  203. }
  204. static int msr_device_destroy(unsigned int cpu)
  205. {
  206. device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  207. return 0;
  208. }
  209. static char *msr_devnode(struct device *dev, umode_t *mode)
  210. {
  211. return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
  212. }
  213. static int __init msr_init(void)
  214. {
  215. int err;
  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. return -EBUSY;
  219. }
  220. msr_class = class_create(THIS_MODULE, "msr");
  221. if (IS_ERR(msr_class)) {
  222. err = PTR_ERR(msr_class);
  223. goto out_chrdev;
  224. }
  225. msr_class->devnode = msr_devnode;
  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_destroy(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_destroy(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 <[email protected]>");
  274. MODULE_DESCRIPTION("x86 generic MSR driver");
  275. MODULE_LICENSE("GPL");