xref: /openbmc/linux/arch/x86/kernel/msr.c (revision 22d55f02)
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 /*
10  * x86 MSR access device
11  *
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.
15  *
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.
18  */
19 
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 
22 #include <linux/module.h>
23 
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>
31 #include <linux/fs.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 
38 #include <asm/cpufeature.h>
39 #include <asm/msr.h>
40 
41 static struct class *msr_class;
42 static enum cpuhp_state cpuhp_msr_state;
43 
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 
54 	if (count % 8)
55 		return -EINVAL;	/* Invalid chunk size */
56 
57 	for (; count; count -= 8) {
58 		err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
59 		if (err)
60 			break;
61 		if (copy_to_user(tmp, &data, 8)) {
62 			err = -EFAULT;
63 			break;
64 		}
65 		tmp += 2;
66 		bytes += 8;
67 	}
68 
69 	return bytes ? bytes : err;
70 }
71 
72 static ssize_t msr_write(struct file *file, const char __user *buf,
73 			 size_t count, loff_t *ppos)
74 {
75 	const u32 __user *tmp = (const u32 __user *)buf;
76 	u32 data[2];
77 	u32 reg = *ppos;
78 	int cpu = iminor(file_inode(file));
79 	int err = 0;
80 	ssize_t bytes = 0;
81 
82 	if (count % 8)
83 		return -EINVAL;	/* Invalid chunk size */
84 
85 	for (; count; count -= 8) {
86 		if (copy_from_user(&data, tmp, 8)) {
87 			err = -EFAULT;
88 			break;
89 		}
90 		err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
91 		if (err)
92 			break;
93 		tmp += 2;
94 		bytes += 8;
95 	}
96 
97 	return bytes ? bytes : err;
98 }
99 
100 static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
101 {
102 	u32 __user *uregs = (u32 __user *)arg;
103 	u32 regs[8];
104 	int cpu = iminor(file_inode(file));
105 	int err;
106 
107 	switch (ioc) {
108 	case X86_IOC_RDMSR_REGS:
109 		if (!(file->f_mode & FMODE_READ)) {
110 			err = -EBADF;
111 			break;
112 		}
113 		if (copy_from_user(&regs, uregs, sizeof(regs))) {
114 			err = -EFAULT;
115 			break;
116 		}
117 		err = rdmsr_safe_regs_on_cpu(cpu, regs);
118 		if (err)
119 			break;
120 		if (copy_to_user(uregs, &regs, sizeof(regs)))
121 			err = -EFAULT;
122 		break;
123 
124 	case X86_IOC_WRMSR_REGS:
125 		if (!(file->f_mode & FMODE_WRITE)) {
126 			err = -EBADF;
127 			break;
128 		}
129 		if (copy_from_user(&regs, uregs, sizeof(regs))) {
130 			err = -EFAULT;
131 			break;
132 		}
133 		err = wrmsr_safe_regs_on_cpu(cpu, regs);
134 		if (err)
135 			break;
136 		if (copy_to_user(uregs, &regs, sizeof(regs)))
137 			err = -EFAULT;
138 		break;
139 
140 	default:
141 		err = -ENOTTY;
142 		break;
143 	}
144 
145 	return err;
146 }
147 
148 static int msr_open(struct inode *inode, struct file *file)
149 {
150 	unsigned int cpu = iminor(file_inode(file));
151 	struct cpuinfo_x86 *c;
152 
153 	if (!capable(CAP_SYS_RAWIO))
154 		return -EPERM;
155 
156 	if (cpu >= nr_cpu_ids || !cpu_online(cpu))
157 		return -ENXIO;	/* No such CPU */
158 
159 	c = &cpu_data(cpu);
160 	if (!cpu_has(c, X86_FEATURE_MSR))
161 		return -EIO;	/* MSR not supported */
162 
163 	return 0;
164 }
165 
166 /*
167  * File operations we support
168  */
169 static const struct file_operations msr_fops = {
170 	.owner = THIS_MODULE,
171 	.llseek = no_seek_end_llseek,
172 	.read = msr_read,
173 	.write = msr_write,
174 	.open = msr_open,
175 	.unlocked_ioctl = msr_ioctl,
176 	.compat_ioctl = msr_ioctl,
177 };
178 
179 static int msr_device_create(unsigned int cpu)
180 {
181 	struct device *dev;
182 
183 	dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
184 			    "msr%d", cpu);
185 	return PTR_ERR_OR_ZERO(dev);
186 }
187 
188 static int msr_device_destroy(unsigned int cpu)
189 {
190 	device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
191 	return 0;
192 }
193 
194 static char *msr_devnode(struct device *dev, umode_t *mode)
195 {
196 	return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
197 }
198 
199 static int __init msr_init(void)
200 {
201 	int err;
202 
203 	if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
204 		pr_err("unable to get major %d for msr\n", MSR_MAJOR);
205 		return -EBUSY;
206 	}
207 	msr_class = class_create(THIS_MODULE, "msr");
208 	if (IS_ERR(msr_class)) {
209 		err = PTR_ERR(msr_class);
210 		goto out_chrdev;
211 	}
212 	msr_class->devnode = msr_devnode;
213 
214 	err  = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online",
215 				 msr_device_create, msr_device_destroy);
216 	if (err < 0)
217 		goto out_class;
218 	cpuhp_msr_state = err;
219 	return 0;
220 
221 out_class:
222 	class_destroy(msr_class);
223 out_chrdev:
224 	__unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
225 	return err;
226 }
227 module_init(msr_init);
228 
229 static void __exit msr_exit(void)
230 {
231 	cpuhp_remove_state(cpuhp_msr_state);
232 	class_destroy(msr_class);
233 	__unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
234 }
235 module_exit(msr_exit)
236 
237 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
238 MODULE_DESCRIPTION("x86 generic MSR driver");
239 MODULE_LICENSE("GPL");
240