• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/security.h>
38 
39 #include <asm/cpufeature.h>
40 #include <asm/msr.h>
41 
42 static struct class *msr_class;
43 static enum cpuhp_state cpuhp_msr_state;
44 
45 enum allow_write_msrs {
46 	MSR_WRITES_ON,
47 	MSR_WRITES_OFF,
48 	MSR_WRITES_DEFAULT,
49 };
50 
51 static enum allow_write_msrs allow_writes = MSR_WRITES_DEFAULT;
52 
msr_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)53 static ssize_t msr_read(struct file *file, char __user *buf,
54 			size_t count, loff_t *ppos)
55 {
56 	u32 __user *tmp = (u32 __user *) buf;
57 	u32 data[2];
58 	u32 reg = *ppos;
59 	int cpu = iminor(file_inode(file));
60 	int err = 0;
61 	ssize_t bytes = 0;
62 
63 	if (count % 8)
64 		return -EINVAL;	/* Invalid chunk size */
65 
66 	for (; count; count -= 8) {
67 		err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
68 		if (err)
69 			break;
70 		if (copy_to_user(tmp, &data, 8)) {
71 			err = -EFAULT;
72 			break;
73 		}
74 		tmp += 2;
75 		bytes += 8;
76 	}
77 
78 	return bytes ? bytes : err;
79 }
80 
filter_write(u32 reg)81 static int filter_write(u32 reg)
82 {
83 	/*
84 	 * MSRs writes usually happen all at once, and can easily saturate kmsg.
85 	 * Only allow one message every 30 seconds.
86 	 *
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.
90 	 */
91 	static DEFINE_RATELIMIT_STATE(fw_rs, 30 * HZ, 1);
92 
93 	switch (allow_writes) {
94 	case MSR_WRITES_ON:  return 0;
95 	case MSR_WRITES_OFF: return -EPERM;
96 	default: break;
97 	}
98 
99 	if (!__ratelimit(&fw_rs))
100 		return 0;
101 
102 	if (reg == MSR_IA32_ENERGY_PERF_BIAS)
103 		return 0;
104 
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);
107 
108 	return 0;
109 }
110 
msr_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)111 static ssize_t msr_write(struct file *file, const char __user *buf,
112 			 size_t count, loff_t *ppos)
113 {
114 	const u32 __user *tmp = (const u32 __user *)buf;
115 	u32 data[2];
116 	u32 reg = *ppos;
117 	int cpu = iminor(file_inode(file));
118 	int err = 0;
119 	ssize_t bytes = 0;
120 
121 	err = security_locked_down(LOCKDOWN_MSR);
122 	if (err)
123 		return err;
124 
125 	err = filter_write(reg);
126 	if (err)
127 		return err;
128 
129 	if (count % 8)
130 		return -EINVAL;	/* Invalid chunk size */
131 
132 	for (; count; count -= 8) {
133 		if (copy_from_user(&data, tmp, 8)) {
134 			err = -EFAULT;
135 			break;
136 		}
137 
138 		add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
139 
140 		err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
141 		if (err)
142 			break;
143 
144 		tmp += 2;
145 		bytes += 8;
146 	}
147 
148 	return bytes ? bytes : err;
149 }
150 
msr_ioctl(struct file * file,unsigned int ioc,unsigned long arg)151 static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
152 {
153 	u32 __user *uregs = (u32 __user *)arg;
154 	u32 regs[8];
155 	int cpu = iminor(file_inode(file));
156 	int err;
157 
158 	switch (ioc) {
159 	case X86_IOC_RDMSR_REGS:
160 		if (!(file->f_mode & FMODE_READ)) {
161 			err = -EBADF;
162 			break;
163 		}
164 		if (copy_from_user(&regs, uregs, sizeof(regs))) {
165 			err = -EFAULT;
166 			break;
167 		}
168 		err = rdmsr_safe_regs_on_cpu(cpu, regs);
169 		if (err)
170 			break;
171 		if (copy_to_user(uregs, &regs, sizeof(regs)))
172 			err = -EFAULT;
173 		break;
174 
175 	case X86_IOC_WRMSR_REGS:
176 		if (!(file->f_mode & FMODE_WRITE)) {
177 			err = -EBADF;
178 			break;
179 		}
180 		if (copy_from_user(&regs, uregs, sizeof(regs))) {
181 			err = -EFAULT;
182 			break;
183 		}
184 		err = security_locked_down(LOCKDOWN_MSR);
185 		if (err)
186 			break;
187 
188 		err = filter_write(regs[1]);
189 		if (err)
190 			return err;
191 
192 		add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
193 
194 		err = wrmsr_safe_regs_on_cpu(cpu, regs);
195 		if (err)
196 			break;
197 		if (copy_to_user(uregs, &regs, sizeof(regs)))
198 			err = -EFAULT;
199 		break;
200 
201 	default:
202 		err = -ENOTTY;
203 		break;
204 	}
205 
206 	return err;
207 }
208 
msr_open(struct inode * inode,struct file * file)209 static int msr_open(struct inode *inode, struct file *file)
210 {
211 	unsigned int cpu = iminor(file_inode(file));
212 	struct cpuinfo_x86 *c;
213 
214 	if (!capable(CAP_SYS_RAWIO))
215 		return -EPERM;
216 
217 	if (cpu >= nr_cpu_ids || !cpu_online(cpu))
218 		return -ENXIO;	/* No such CPU */
219 
220 	c = &cpu_data(cpu);
221 	if (!cpu_has(c, X86_FEATURE_MSR))
222 		return -EIO;	/* MSR not supported */
223 
224 	return 0;
225 }
226 
227 /*
228  * File operations we support
229  */
230 static const struct file_operations msr_fops = {
231 	.owner = THIS_MODULE,
232 	.llseek = no_seek_end_llseek,
233 	.read = msr_read,
234 	.write = msr_write,
235 	.open = msr_open,
236 	.unlocked_ioctl = msr_ioctl,
237 	.compat_ioctl = msr_ioctl,
238 };
239 
msr_device_create(unsigned int cpu)240 static int msr_device_create(unsigned int cpu)
241 {
242 	struct device *dev;
243 
244 	dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
245 			    "msr%d", cpu);
246 	return PTR_ERR_OR_ZERO(dev);
247 }
248 
msr_device_destroy(unsigned int cpu)249 static int msr_device_destroy(unsigned int cpu)
250 {
251 	device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
252 	return 0;
253 }
254 
msr_devnode(struct device * dev,umode_t * mode)255 static char *msr_devnode(struct device *dev, umode_t *mode)
256 {
257 	return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
258 }
259 
msr_init(void)260 static int __init msr_init(void)
261 {
262 	int err;
263 
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);
266 		return -EBUSY;
267 	}
268 	msr_class = class_create(THIS_MODULE, "msr");
269 	if (IS_ERR(msr_class)) {
270 		err = PTR_ERR(msr_class);
271 		goto out_chrdev;
272 	}
273 	msr_class->devnode = msr_devnode;
274 
275 	err  = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online",
276 				 msr_device_create, msr_device_destroy);
277 	if (err < 0)
278 		goto out_class;
279 	cpuhp_msr_state = err;
280 	return 0;
281 
282 out_class:
283 	class_destroy(msr_class);
284 out_chrdev:
285 	__unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
286 	return err;
287 }
288 module_init(msr_init);
289 
msr_exit(void)290 static void __exit msr_exit(void)
291 {
292 	cpuhp_remove_state(cpuhp_msr_state);
293 	class_destroy(msr_class);
294 	__unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
295 }
module_exit(msr_exit)296 module_exit(msr_exit)
297 
298 static int set_allow_writes(const char *val, const struct kernel_param *cp)
299 {
300 	/* val is NUL-terminated, see kernfs_fop_write() */
301 	char *s = strstrip((char *)val);
302 
303 	if (!strcmp(s, "on"))
304 		allow_writes = MSR_WRITES_ON;
305 	else if (!strcmp(s, "off"))
306 		allow_writes = MSR_WRITES_OFF;
307 	else
308 		allow_writes = MSR_WRITES_DEFAULT;
309 
310 	return 0;
311 }
312 
get_allow_writes(char * buf,const struct kernel_param * kp)313 static int get_allow_writes(char *buf, const struct kernel_param *kp)
314 {
315 	const char *res;
316 
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;
321 	}
322 
323 	return sprintf(buf, "%s\n", res);
324 }
325 
326 static const struct kernel_param_ops allow_writes_ops = {
327 	.set = set_allow_writes,
328 	.get = get_allow_writes
329 };
330 
331 module_param_cb(allow_writes, &allow_writes_ops, NULL, 0600);
332 
333 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
334 MODULE_DESCRIPTION("x86 generic MSR driver");
335 MODULE_LICENSE("GPL");
336