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 static ssize_t
msr_read(struct file
*file
, char __user
*buf
,
46 size_t count
, loff_t
*ppos
)
48 u32 __user
*tmp
= (u32 __user
*) buf
;
51 int cpu
= iminor(file_inode(file
));
56 return -EINVAL
; /* Invalid chunk size */
58 for (; count
; count
-= 8) {
59 err
= rdmsr_safe_on_cpu(cpu
, reg
, &data
[0], &data
[1]);
62 if (copy_to_user(tmp
, &data
, 8)) {
70 return bytes
? bytes
: err
;
73 static ssize_t
msr_write(struct file
*file
, const char __user
*buf
,
74 size_t count
, loff_t
*ppos
)
76 const u32 __user
*tmp
= (const u32 __user
*)buf
;
79 int cpu
= iminor(file_inode(file
));
83 err
= security_locked_down(LOCKDOWN_MSR
);
88 return -EINVAL
; /* Invalid chunk size */
90 for (; count
; count
-= 8) {
91 if (copy_from_user(&data
, tmp
, 8)) {
95 err
= wrmsr_safe_on_cpu(cpu
, reg
, data
[0], data
[1]);
102 return bytes
? bytes
: err
;
105 static long msr_ioctl(struct file
*file
, unsigned int ioc
, unsigned long arg
)
107 u32 __user
*uregs
= (u32 __user
*)arg
;
109 int cpu
= iminor(file_inode(file
));
113 case X86_IOC_RDMSR_REGS
:
114 if (!(file
->f_mode
& FMODE_READ
)) {
118 if (copy_from_user(®s
, uregs
, sizeof(regs
))) {
122 err
= rdmsr_safe_regs_on_cpu(cpu
, regs
);
125 if (copy_to_user(uregs
, ®s
, sizeof(regs
)))
129 case X86_IOC_WRMSR_REGS
:
130 if (!(file
->f_mode
& FMODE_WRITE
)) {
134 if (copy_from_user(®s
, uregs
, sizeof(regs
))) {
138 err
= security_locked_down(LOCKDOWN_MSR
);
141 err
= wrmsr_safe_regs_on_cpu(cpu
, regs
);
144 if (copy_to_user(uregs
, ®s
, sizeof(regs
)))
156 static int msr_open(struct inode
*inode
, struct file
*file
)
158 unsigned int cpu
= iminor(file_inode(file
));
159 struct cpuinfo_x86
*c
;
161 if (!capable(CAP_SYS_RAWIO
))
164 if (cpu
>= nr_cpu_ids
|| !cpu_online(cpu
))
165 return -ENXIO
; /* No such CPU */
168 if (!cpu_has(c
, X86_FEATURE_MSR
))
169 return -EIO
; /* MSR not supported */
175 * File operations we support
177 static const struct file_operations msr_fops
= {
178 .owner
= THIS_MODULE
,
179 .llseek
= no_seek_end_llseek
,
183 .unlocked_ioctl
= msr_ioctl
,
184 .compat_ioctl
= msr_ioctl
,
187 static int msr_device_create(unsigned int cpu
)
191 dev
= device_create(msr_class
, NULL
, MKDEV(MSR_MAJOR
, cpu
), NULL
,
193 return PTR_ERR_OR_ZERO(dev
);
196 static int msr_device_destroy(unsigned int cpu
)
198 device_destroy(msr_class
, MKDEV(MSR_MAJOR
, cpu
));
202 static char *msr_devnode(struct device
*dev
, umode_t
*mode
)
204 return kasprintf(GFP_KERNEL
, "cpu/%u/msr", MINOR(dev
->devt
));
207 static int __init
msr_init(void)
211 if (__register_chrdev(MSR_MAJOR
, 0, NR_CPUS
, "cpu/msr", &msr_fops
)) {
212 pr_err("unable to get major %d for msr\n", MSR_MAJOR
);
215 msr_class
= class_create(THIS_MODULE
, "msr");
216 if (IS_ERR(msr_class
)) {
217 err
= PTR_ERR(msr_class
);
220 msr_class
->devnode
= msr_devnode
;
222 err
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "x86/msr:online",
223 msr_device_create
, msr_device_destroy
);
226 cpuhp_msr_state
= err
;
230 class_destroy(msr_class
);
232 __unregister_chrdev(MSR_MAJOR
, 0, NR_CPUS
, "cpu/msr");
235 module_init(msr_init
);
237 static void __exit
msr_exit(void)
239 cpuhp_remove_state(cpuhp_msr_state
);
240 class_destroy(msr_class
);
241 __unregister_chrdev(MSR_MAJOR
, 0, NR_CPUS
, "cpu/msr");
243 module_exit(msr_exit
)
245 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
246 MODULE_DESCRIPTION("x86 generic MSR driver");
247 MODULE_LICENSE("GPL");