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 pr_warn("Write to unrecognized MSR 0x%x by %s (pid: %d).\n",
103 reg
, current
->comm
, current
->pid
);
104 pr_warn("See https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/about for details.\n");
109 static ssize_t
msr_write(struct file
*file
, const char __user
*buf
,
110 size_t count
, loff_t
*ppos
)
112 const u32 __user
*tmp
= (const u32 __user
*)buf
;
115 int cpu
= iminor(file_inode(file
));
119 err
= security_locked_down(LOCKDOWN_MSR
);
123 err
= filter_write(reg
);
128 return -EINVAL
; /* Invalid chunk size */
130 for (; count
; count
-= 8) {
131 if (copy_from_user(&data
, tmp
, 8)) {
136 add_taint(TAINT_CPU_OUT_OF_SPEC
, LOCKDEP_STILL_OK
);
138 err
= wrmsr_safe_on_cpu(cpu
, reg
, data
[0], data
[1]);
146 return bytes
? bytes
: err
;
149 static long msr_ioctl(struct file
*file
, unsigned int ioc
, unsigned long arg
)
151 u32 __user
*uregs
= (u32 __user
*)arg
;
153 int cpu
= iminor(file_inode(file
));
157 case X86_IOC_RDMSR_REGS
:
158 if (!(file
->f_mode
& FMODE_READ
)) {
162 if (copy_from_user(®s
, uregs
, sizeof(regs
))) {
166 err
= rdmsr_safe_regs_on_cpu(cpu
, regs
);
169 if (copy_to_user(uregs
, ®s
, sizeof(regs
)))
173 case X86_IOC_WRMSR_REGS
:
174 if (!(file
->f_mode
& FMODE_WRITE
)) {
178 if (copy_from_user(®s
, uregs
, sizeof(regs
))) {
182 err
= security_locked_down(LOCKDOWN_MSR
);
186 err
= filter_write(regs
[1]);
190 add_taint(TAINT_CPU_OUT_OF_SPEC
, LOCKDEP_STILL_OK
);
192 err
= wrmsr_safe_regs_on_cpu(cpu
, regs
);
195 if (copy_to_user(uregs
, ®s
, sizeof(regs
)))
207 static int msr_open(struct inode
*inode
, struct file
*file
)
209 unsigned int cpu
= iminor(file_inode(file
));
210 struct cpuinfo_x86
*c
;
212 if (!capable(CAP_SYS_RAWIO
))
215 if (cpu
>= nr_cpu_ids
|| !cpu_online(cpu
))
216 return -ENXIO
; /* No such CPU */
219 if (!cpu_has(c
, X86_FEATURE_MSR
))
220 return -EIO
; /* MSR not supported */
226 * File operations we support
228 static const struct file_operations msr_fops
= {
229 .owner
= THIS_MODULE
,
230 .llseek
= no_seek_end_llseek
,
234 .unlocked_ioctl
= msr_ioctl
,
235 .compat_ioctl
= msr_ioctl
,
238 static int msr_device_create(unsigned int cpu
)
242 dev
= device_create(msr_class
, NULL
, MKDEV(MSR_MAJOR
, cpu
), NULL
,
244 return PTR_ERR_OR_ZERO(dev
);
247 static int msr_device_destroy(unsigned int cpu
)
249 device_destroy(msr_class
, MKDEV(MSR_MAJOR
, cpu
));
253 static char *msr_devnode(struct device
*dev
, umode_t
*mode
)
255 return kasprintf(GFP_KERNEL
, "cpu/%u/msr", MINOR(dev
->devt
));
258 static int __init
msr_init(void)
262 if (__register_chrdev(MSR_MAJOR
, 0, NR_CPUS
, "cpu/msr", &msr_fops
)) {
263 pr_err("unable to get major %d for msr\n", MSR_MAJOR
);
266 msr_class
= class_create(THIS_MODULE
, "msr");
267 if (IS_ERR(msr_class
)) {
268 err
= PTR_ERR(msr_class
);
271 msr_class
->devnode
= msr_devnode
;
273 err
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "x86/msr:online",
274 msr_device_create
, msr_device_destroy
);
277 cpuhp_msr_state
= err
;
281 class_destroy(msr_class
);
283 __unregister_chrdev(MSR_MAJOR
, 0, NR_CPUS
, "cpu/msr");
286 module_init(msr_init
);
288 static void __exit
msr_exit(void)
290 cpuhp_remove_state(cpuhp_msr_state
);
291 class_destroy(msr_class
);
292 __unregister_chrdev(MSR_MAJOR
, 0, NR_CPUS
, "cpu/msr");
294 module_exit(msr_exit
)
296 static int set_allow_writes(const char *val
, const struct kernel_param
*cp
)
298 /* val is NUL-terminated, see kernfs_fop_write() */
299 char *s
= strstrip((char *)val
);
301 if (!strcmp(s
, "on"))
302 allow_writes
= MSR_WRITES_ON
;
303 else if (!strcmp(s
, "off"))
304 allow_writes
= MSR_WRITES_OFF
;
306 allow_writes
= MSR_WRITES_DEFAULT
;
311 static int get_allow_writes(char *buf
, const struct kernel_param
*kp
)
315 switch (allow_writes
) {
316 case MSR_WRITES_ON
: res
= "on"; break;
317 case MSR_WRITES_OFF
: res
= "off"; break;
318 default: res
= "default"; break;
321 return sprintf(buf
, "%s\n", res
);
324 static const struct kernel_param_ops allow_writes_ops
= {
325 .set
= set_allow_writes
,
326 .get
= get_allow_writes
329 module_param_cb(allow_writes
, &allow_writes_ops
, NULL
, 0600);
331 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
332 MODULE_DESCRIPTION("x86 generic MSR driver");
333 MODULE_LICENSE("GPL");