]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/x86/kernel/msr.c
x86/pti: Do not enable PTI on CPUs which are not vulnerable to Meltdown
[mirror_ubuntu-artful-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())
88 return -EPERM;
89
90 if (count % 8)
91 return -EINVAL; /* Invalid chunk size */
92
93 for (; count; count -= 8) {
94 if (copy_from_user(&data, tmp, 8)) {
95 err = -EFAULT;
96 break;
97 }
98 err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
99 if (err)
100 break;
101 tmp += 2;
102 bytes += 8;
103 }
104
105 return bytes ? bytes : err;
106 }
107
108 static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
109 {
110 u32 __user *uregs = (u32 __user *)arg;
111 u32 regs[8];
112 int cpu = iminor(file_inode(file));
113 int err;
114
115 switch (ioc) {
116 case X86_IOC_RDMSR_REGS:
117 if (!(file->f_mode & FMODE_READ)) {
118 err = -EBADF;
119 break;
120 }
121 if (copy_from_user(&regs, uregs, sizeof regs)) {
122 err = -EFAULT;
123 break;
124 }
125 err = rdmsr_safe_regs_on_cpu(cpu, regs);
126 if (err)
127 break;
128 if (copy_to_user(uregs, &regs, sizeof regs))
129 err = -EFAULT;
130 break;
131
132 case X86_IOC_WRMSR_REGS:
133 if (!(file->f_mode & FMODE_WRITE)) {
134 err = -EBADF;
135 break;
136 }
137 if (kernel_is_locked_down()) {
138 err = -EPERM;
139 break;
140 }
141 if (copy_from_user(&regs, uregs, sizeof regs)) {
142 err = -EFAULT;
143 break;
144 }
145 err = wrmsr_safe_regs_on_cpu(cpu, regs);
146 if (err)
147 break;
148 if (copy_to_user(uregs, &regs, sizeof regs))
149 err = -EFAULT;
150 break;
151
152 default:
153 err = -ENOTTY;
154 break;
155 }
156
157 return err;
158 }
159
160 static int msr_open(struct inode *inode, struct file *file)
161 {
162 unsigned int cpu = iminor(file_inode(file));
163 struct cpuinfo_x86 *c;
164
165 if (!capable(CAP_SYS_RAWIO))
166 return -EPERM;
167
168 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
169 return -ENXIO; /* No such CPU */
170
171 c = &cpu_data(cpu);
172 if (!cpu_has(c, X86_FEATURE_MSR))
173 return -EIO; /* MSR not supported */
174
175 return 0;
176 }
177
178 /*
179 * File operations we support
180 */
181 static const struct file_operations msr_fops = {
182 .owner = THIS_MODULE,
183 .llseek = no_seek_end_llseek,
184 .read = msr_read,
185 .write = msr_write,
186 .open = msr_open,
187 .unlocked_ioctl = msr_ioctl,
188 .compat_ioctl = msr_ioctl,
189 };
190
191 static int msr_device_create(unsigned int cpu)
192 {
193 struct device *dev;
194
195 dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
196 "msr%d", cpu);
197 return PTR_ERR_OR_ZERO(dev);
198 }
199
200 static int msr_device_destroy(unsigned int cpu)
201 {
202 device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
203 return 0;
204 }
205
206 static char *msr_devnode(struct device *dev, umode_t *mode)
207 {
208 return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
209 }
210
211 static int __init msr_init(void)
212 {
213 int err;
214
215 if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
216 pr_err("unable to get major %d for msr\n", MSR_MAJOR);
217 return -EBUSY;
218 }
219 msr_class = class_create(THIS_MODULE, "msr");
220 if (IS_ERR(msr_class)) {
221 err = PTR_ERR(msr_class);
222 goto out_chrdev;
223 }
224 msr_class->devnode = msr_devnode;
225
226 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online",
227 msr_device_create, msr_device_destroy);
228 if (err < 0)
229 goto out_class;
230 cpuhp_msr_state = err;
231 return 0;
232
233 out_class:
234 class_destroy(msr_class);
235 out_chrdev:
236 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
237 return err;
238 }
239 module_init(msr_init);
240
241 static void __exit msr_exit(void)
242 {
243 cpuhp_remove_state(cpuhp_msr_state);
244 class_destroy(msr_class);
245 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
246 }
247 module_exit(msr_exit)
248
249 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
250 MODULE_DESCRIPTION("x86 generic MSR driver");
251 MODULE_LICENSE("GPL");