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 enum cpuhp_state cpuhp_msr_state
;
44 enum allow_write_msrs
{
50 static enum allow_write_msrs allow_writes
= MSR_WRITES_DEFAULT
;
52 static ssize_t
msr_read(struct file
*file
, char __user
*buf
,
53 size_t count
, loff_t
*ppos
)
55 u32 __user
*tmp
= (u32 __user
*) buf
;
58 int cpu
= iminor(file_inode(file
));
63 return -EINVAL
; /* Invalid chunk size */
65 for (; count
; count
-= 8) {
66 err
= rdmsr_safe_on_cpu(cpu
, reg
, &data
[0], &data
[1]);
69 if (copy_to_user(tmp
, &data
, 8)) {
77 return bytes
? bytes
: err
;
80 static int filter_write(u32 reg
)
83 * MSRs writes usually happen all at once, and can easily saturate kmsg.
84 * Only allow one message every 30 seconds.
86 * It's possible to be smarter here and do it (for example) per-MSR, but
87 * it would certainly be more complex, and this is enough at least to
88 * avoid saturating the ring buffer.
90 static DEFINE_RATELIMIT_STATE(fw_rs
, 30 * HZ
, 1);
92 switch (allow_writes
) {
93 case MSR_WRITES_ON
: return 0;
94 case MSR_WRITES_OFF
: return -EPERM
;
98 if (!__ratelimit(&fw_rs
))
101 pr_warn("Write to unrecognized MSR 0x%x by %s (pid: %d).\n",
102 reg
, current
->comm
, current
->pid
);
103 pr_warn("See https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/about for details.\n");
108 static ssize_t
msr_write(struct file
*file
, const char __user
*buf
,
109 size_t count
, loff_t
*ppos
)
111 const u32 __user
*tmp
= (const u32 __user
*)buf
;
114 int cpu
= iminor(file_inode(file
));
118 err
= security_locked_down(LOCKDOWN_MSR
);
122 err
= filter_write(reg
);
127 return -EINVAL
; /* Invalid chunk size */
129 for (; count
; count
-= 8) {
130 if (copy_from_user(&data
, tmp
, 8)) {
135 add_taint(TAINT_CPU_OUT_OF_SPEC
, LOCKDEP_STILL_OK
);
137 err
= wrmsr_safe_on_cpu(cpu
, reg
, data
[0], data
[1]);
145 return bytes
? bytes
: err
;
148 static long msr_ioctl(struct file
*file
, unsigned int ioc
, unsigned long arg
)
150 u32 __user
*uregs
= (u32 __user
*)arg
;
152 int cpu
= iminor(file_inode(file
));
156 case X86_IOC_RDMSR_REGS
:
157 if (!(file
->f_mode
& FMODE_READ
)) {
161 if (copy_from_user(®s
, uregs
, sizeof(regs
))) {
165 err
= rdmsr_safe_regs_on_cpu(cpu
, regs
);
168 if (copy_to_user(uregs
, ®s
, sizeof(regs
)))
172 case X86_IOC_WRMSR_REGS
:
173 if (!(file
->f_mode
& FMODE_WRITE
)) {
177 if (copy_from_user(®s
, uregs
, sizeof(regs
))) {
181 err
= security_locked_down(LOCKDOWN_MSR
);
185 err
= filter_write(regs
[1]);
189 add_taint(TAINT_CPU_OUT_OF_SPEC
, LOCKDEP_STILL_OK
);
191 err
= wrmsr_safe_regs_on_cpu(cpu
, regs
);
194 if (copy_to_user(uregs
, ®s
, sizeof(regs
)))
206 static int msr_open(struct inode
*inode
, struct file
*file
)
208 unsigned int cpu
= iminor(file_inode(file
));
209 struct cpuinfo_x86
*c
;
211 if (!capable(CAP_SYS_RAWIO
))
214 if (cpu
>= nr_cpu_ids
|| !cpu_online(cpu
))
215 return -ENXIO
; /* No such CPU */
218 if (!cpu_has(c
, X86_FEATURE_MSR
))
219 return -EIO
; /* MSR not supported */
225 * File operations we support
227 static const struct file_operations msr_fops
= {
228 .owner
= THIS_MODULE
,
229 .llseek
= no_seek_end_llseek
,
233 .unlocked_ioctl
= msr_ioctl
,
234 .compat_ioctl
= msr_ioctl
,
237 static char *msr_devnode(const struct device
*dev
, umode_t
*mode
)
239 return kasprintf(GFP_KERNEL
, "cpu/%u/msr", MINOR(dev
->devt
));
242 static const struct class msr_class
= {
244 .devnode
= msr_devnode
,
247 static int msr_device_create(unsigned int cpu
)
251 dev
= device_create(&msr_class
, NULL
, MKDEV(MSR_MAJOR
, cpu
), NULL
,
253 return PTR_ERR_OR_ZERO(dev
);
256 static int msr_device_destroy(unsigned int cpu
)
258 device_destroy(&msr_class
, MKDEV(MSR_MAJOR
, cpu
));
262 static int __init
msr_init(void)
266 if (__register_chrdev(MSR_MAJOR
, 0, NR_CPUS
, "cpu/msr", &msr_fops
)) {
267 pr_err("unable to get major %d for msr\n", MSR_MAJOR
);
270 err
= class_register(&msr_class
);
274 err
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "x86/msr:online",
275 msr_device_create
, msr_device_destroy
);
278 cpuhp_msr_state
= err
;
282 class_unregister(&msr_class
);
284 __unregister_chrdev(MSR_MAJOR
, 0, NR_CPUS
, "cpu/msr");
287 module_init(msr_init
);
289 static void __exit
msr_exit(void)
291 cpuhp_remove_state(cpuhp_msr_state
);
292 class_unregister(&msr_class
);
293 __unregister_chrdev(MSR_MAJOR
, 0, NR_CPUS
, "cpu/msr");
295 module_exit(msr_exit
)
297 static int set_allow_writes(const char *val
, const struct kernel_param
*cp
)
299 /* val is NUL-terminated, see kernfs_fop_write() */
300 char *s
= strstrip((char *)val
);
302 if (!strcmp(s
, "on"))
303 allow_writes
= MSR_WRITES_ON
;
304 else if (!strcmp(s
, "off"))
305 allow_writes
= MSR_WRITES_OFF
;
307 allow_writes
= MSR_WRITES_DEFAULT
;
312 static int get_allow_writes(char *buf
, const struct kernel_param
*kp
)
316 switch (allow_writes
) {
317 case MSR_WRITES_ON
: res
= "on"; break;
318 case MSR_WRITES_OFF
: res
= "off"; break;
319 default: res
= "default"; break;
322 return sprintf(buf
, "%s\n", res
);
325 static const struct kernel_param_ops allow_writes_ops
= {
326 .set
= set_allow_writes
,
327 .get
= get_allow_writes
330 module_param_cb(allow_writes
, &allow_writes_ops
, NULL
, 0600);
332 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
333 MODULE_DESCRIPTION("x86 generic MSR driver");
334 MODULE_LICENSE("GPL");