]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/kernel/msr.c
x86/speculation: Consolidate CPU whitelists
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / msr.c
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
9 * USA; either version 2 of the License, or (at your option) any later
10 * version; incorporated herein by reference.
11 *
12 * ----------------------------------------------------------------------- */
13
14 /*
15 * x86 MSR access device
16 *
17 * This device is accessed by lseek() to the appropriate register number
18 * and then read/write in chunks of 8 bytes. A larger size means multiple
19 * reads or writes of the same register.
20 *
21 * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
22 * an SMP box will direct the access to CPU %d.
23 */
24
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27 #include <linux/module.h>
28
29 #include <linux/types.h>
30 #include <linux/errno.h>
31 #include <linux/fcntl.h>
32 #include <linux/init.h>
33 #include <linux/poll.h>
34 #include <linux/smp.h>
35 #include <linux/major.h>
36 #include <linux/fs.h>
37 #include <linux/device.h>
38 #include <linux/cpu.h>
39 #include <linux/notifier.h>
40 #include <linux/uaccess.h>
41 #include <linux/gfp.h>
42
43 #include <asm/cpufeature.h>
44 #include <asm/msr.h>
45
46 static struct class *msr_class;
47 static enum cpuhp_state cpuhp_msr_state;
48
49 static ssize_t msr_read(struct file *file, char __user *buf,
50 size_t count, loff_t *ppos)
51 {
52 u32 __user *tmp = (u32 __user *) buf;
53 u32 data[2];
54 u32 reg = *ppos;
55 int cpu = iminor(file_inode(file));
56 int err = 0;
57 ssize_t bytes = 0;
58
59 if (count % 8)
60 return -EINVAL; /* Invalid chunk size */
61
62 for (; count; count -= 8) {
63 err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
64 if (err)
65 break;
66 if (copy_to_user(tmp, &data, 8)) {
67 err = -EFAULT;
68 break;
69 }
70 tmp += 2;
71 bytes += 8;
72 }
73
74 return bytes ? bytes : err;
75 }
76
77 static ssize_t msr_write(struct file *file, const char __user *buf,
78 size_t count, loff_t *ppos)
79 {
80 const u32 __user *tmp = (const u32 __user *)buf;
81 u32 data[2];
82 u32 reg = *ppos;
83 int cpu = iminor(file_inode(file));
84 int err = 0;
85 ssize_t bytes = 0;
86
87 if (kernel_is_locked_down("Direct MSR access")) {
88 pr_info("Direct access to MSR %x\n", reg);
89 return -EPERM;
90 }
91
92 if (count % 8)
93 return -EINVAL; /* Invalid chunk size */
94
95 for (; count; count -= 8) {
96 if (copy_from_user(&data, tmp, 8)) {
97 err = -EFAULT;
98 break;
99 }
100 err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
101 if (err)
102 break;
103 tmp += 2;
104 bytes += 8;
105 }
106
107 return bytes ? bytes : err;
108 }
109
110 static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
111 {
112 u32 __user *uregs = (u32 __user *)arg;
113 u32 regs[8];
114 int cpu = iminor(file_inode(file));
115 int err;
116
117 switch (ioc) {
118 case X86_IOC_RDMSR_REGS:
119 if (!(file->f_mode & FMODE_READ)) {
120 err = -EBADF;
121 break;
122 }
123 if (copy_from_user(&regs, uregs, sizeof regs)) {
124 err = -EFAULT;
125 break;
126 }
127 err = rdmsr_safe_regs_on_cpu(cpu, regs);
128 if (err)
129 break;
130 if (copy_to_user(uregs, &regs, sizeof regs))
131 err = -EFAULT;
132 break;
133
134 case X86_IOC_WRMSR_REGS:
135 if (!(file->f_mode & FMODE_WRITE)) {
136 err = -EBADF;
137 break;
138 }
139 if (copy_from_user(&regs, uregs, sizeof regs)) {
140 err = -EFAULT;
141 break;
142 }
143 if (kernel_is_locked_down("Direct MSR access")) {
144 pr_info("Direct access to MSR %x\n", regs[1]); /* Display %ecx */
145 err = -EPERM;
146 break;
147 }
148 err = wrmsr_safe_regs_on_cpu(cpu, regs);
149 if (err)
150 break;
151 if (copy_to_user(uregs, &regs, sizeof regs))
152 err = -EFAULT;
153 break;
154
155 default:
156 err = -ENOTTY;
157 break;
158 }
159
160 return err;
161 }
162
163 static int msr_open(struct inode *inode, struct file *file)
164 {
165 unsigned int cpu = iminor(file_inode(file));
166 struct cpuinfo_x86 *c;
167
168 if (!capable(CAP_SYS_RAWIO))
169 return -EPERM;
170
171 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
172 return -ENXIO; /* No such CPU */
173
174 c = &cpu_data(cpu);
175 if (!cpu_has(c, X86_FEATURE_MSR))
176 return -EIO; /* MSR not supported */
177
178 return 0;
179 }
180
181 /*
182 * File operations we support
183 */
184 static const struct file_operations msr_fops = {
185 .owner = THIS_MODULE,
186 .llseek = no_seek_end_llseek,
187 .read = msr_read,
188 .write = msr_write,
189 .open = msr_open,
190 .unlocked_ioctl = msr_ioctl,
191 .compat_ioctl = msr_ioctl,
192 };
193
194 static int msr_device_create(unsigned int cpu)
195 {
196 struct device *dev;
197
198 dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
199 "msr%d", cpu);
200 return PTR_ERR_OR_ZERO(dev);
201 }
202
203 static int msr_device_destroy(unsigned int cpu)
204 {
205 device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
206 return 0;
207 }
208
209 static char *msr_devnode(struct device *dev, umode_t *mode)
210 {
211 return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
212 }
213
214 static int __init msr_init(void)
215 {
216 int err;
217
218 if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
219 pr_err("unable to get major %d for msr\n", MSR_MAJOR);
220 return -EBUSY;
221 }
222 msr_class = class_create(THIS_MODULE, "msr");
223 if (IS_ERR(msr_class)) {
224 err = PTR_ERR(msr_class);
225 goto out_chrdev;
226 }
227 msr_class->devnode = msr_devnode;
228
229 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online",
230 msr_device_create, msr_device_destroy);
231 if (err < 0)
232 goto out_class;
233 cpuhp_msr_state = err;
234 return 0;
235
236 out_class:
237 class_destroy(msr_class);
238 out_chrdev:
239 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
240 return err;
241 }
242 module_init(msr_init);
243
244 static void __exit msr_exit(void)
245 {
246 cpuhp_remove_state(cpuhp_msr_state);
247 class_destroy(msr_class);
248 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
249 }
250 module_exit(msr_exit)
251
252 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
253 MODULE_DESCRIPTION("x86 generic MSR driver");
254 MODULE_LICENSE("GPL");