1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* ----------------------------------------------------------------------- *
4 * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
5 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
7 * ----------------------------------------------------------------------- */
10 * x86 MSR access device
12 * This device is accessed by lseek() to the appropriate register number
13 * and then read/write in chunks of 8 bytes. A larger size means multiple
14 * reads or writes of the same register.
16 * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
17 * an SMP box will direct the access to CPU %d.
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #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>
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 <linux/security.h>
39 #include <asm/cpufeature.h>
42 static struct class *msr_class;
43 static enum cpuhp_state cpuhp_msr_state;
45 enum allow_write_msrs {
51 static enum allow_write_msrs allow_writes = MSR_WRITES_DEFAULT;
53 static ssize_t msr_read(struct file *file, char __user *buf,
54 size_t count, loff_t *ppos)
56 u32 __user *tmp = (u32 __user *) buf;
59 int cpu = iminor(file_inode(file));
64 return -EINVAL; /* Invalid chunk size */
66 for (; count; count -= 8) {
67 err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
70 if (copy_to_user(tmp, &data, 8)) {
78 return bytes ? bytes : err;
81 static int filter_write(u32 reg)
84 * MSRs writes usually happen all at once, and can easily saturate kmsg.
85 * Only allow one message every 30 seconds.
87 * It's possible to be smarter here and do it (for example) per-MSR, but
88 * it would certainly be more complex, and this is enough at least to
89 * avoid saturating the ring buffer.
91 static DEFINE_RATELIMIT_STATE(fw_rs, 30 * HZ, 1);
93 switch (allow_writes) {
94 case MSR_WRITES_ON: return 0;
95 case MSR_WRITES_OFF: return -EPERM;
99 if (!__ratelimit(&fw_rs))
102 if (reg == MSR_IA32_ENERGY_PERF_BIAS)
105 pr_err("Write to unrecognized MSR 0x%x by %s (pid: %d). Please report to x86@kernel.org.\n",
106 reg, current->comm, current->pid);
111 static ssize_t msr_write(struct file *file, const char __user *buf,
112 size_t count, loff_t *ppos)
114 const u32 __user *tmp = (const u32 __user *)buf;
117 int cpu = iminor(file_inode(file));
121 err = security_locked_down(LOCKDOWN_MSR);
125 err = filter_write(reg);
130 return -EINVAL; /* Invalid chunk size */
132 for (; count; count -= 8) {
133 if (copy_from_user(&data, tmp, 8)) {
138 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
140 err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
148 return bytes ? bytes : err;
151 static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
153 u32 __user *uregs = (u32 __user *)arg;
155 int cpu = iminor(file_inode(file));
159 case X86_IOC_RDMSR_REGS:
160 if (!(file->f_mode & FMODE_READ)) {
164 if (copy_from_user(®s, uregs, sizeof(regs))) {
168 err = rdmsr_safe_regs_on_cpu(cpu, regs);
171 if (copy_to_user(uregs, ®s, sizeof(regs)))
175 case X86_IOC_WRMSR_REGS:
176 if (!(file->f_mode & FMODE_WRITE)) {
180 if (copy_from_user(®s, uregs, sizeof(regs))) {
184 err = security_locked_down(LOCKDOWN_MSR);
188 err = filter_write(regs[1]);
192 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
194 err = wrmsr_safe_regs_on_cpu(cpu, regs);
197 if (copy_to_user(uregs, ®s, sizeof(regs)))
209 static int msr_open(struct inode *inode, struct file *file)
211 unsigned int cpu = iminor(file_inode(file));
212 struct cpuinfo_x86 *c;
214 if (!capable(CAP_SYS_RAWIO))
217 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
218 return -ENXIO; /* No such CPU */
221 if (!cpu_has(c, X86_FEATURE_MSR))
222 return -EIO; /* MSR not supported */
228 * File operations we support
230 static const struct file_operations msr_fops = {
231 .owner = THIS_MODULE,
232 .llseek = no_seek_end_llseek,
236 .unlocked_ioctl = msr_ioctl,
237 .compat_ioctl = msr_ioctl,
240 static int msr_device_create(unsigned int cpu)
244 dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
246 return PTR_ERR_OR_ZERO(dev);
249 static int msr_device_destroy(unsigned int cpu)
251 device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
255 static char *msr_devnode(struct device *dev, umode_t *mode)
257 return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
260 static int __init msr_init(void)
264 if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
265 pr_err("unable to get major %d for msr\n", MSR_MAJOR);
268 msr_class = class_create(THIS_MODULE, "msr");
269 if (IS_ERR(msr_class)) {
270 err = PTR_ERR(msr_class);
273 msr_class->devnode = msr_devnode;
275 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online",
276 msr_device_create, msr_device_destroy);
279 cpuhp_msr_state = err;
283 class_destroy(msr_class);
285 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
288 module_init(msr_init);
290 static void __exit msr_exit(void)
292 cpuhp_remove_state(cpuhp_msr_state);
293 class_destroy(msr_class);
294 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
296 module_exit(msr_exit)
298 static int set_allow_writes(const char *val, const struct kernel_param *cp)
300 /* val is NUL-terminated, see kernfs_fop_write() */
301 char *s = strstrip((char *)val);
303 if (!strcmp(s, "on"))
304 allow_writes = MSR_WRITES_ON;
305 else if (!strcmp(s, "off"))
306 allow_writes = MSR_WRITES_OFF;
308 allow_writes = MSR_WRITES_DEFAULT;
313 static int get_allow_writes(char *buf, const struct kernel_param *kp)
317 switch (allow_writes) {
318 case MSR_WRITES_ON: res = "on"; break;
319 case MSR_WRITES_OFF: res = "off"; break;
320 default: res = "default"; break;
323 return sprintf(buf, "%s\n", res);
326 static const struct kernel_param_ops allow_writes_ops = {
327 .set = set_allow_writes,
328 .get = get_allow_writes
331 module_param_cb(allow_writes, &allow_writes_ops, NULL, 0600);
333 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
334 MODULE_DESCRIPTION("x86 generic MSR driver");
335 MODULE_LICENSE("GPL");