forked from LLNL/msr-safe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsr_batch.c
288 lines (249 loc) · 6.91 KB
/
msr_batch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright (c) 2011-2017 by Lawrence Livermore National Security, LLC.
* LLNL-CODE-645430
*
* Produced at Lawrence Livermore National Laboratory.
* Written by Marty McFadden, [email protected]
* Kathleen Shoga, [email protected]
* Barry Rountree, [email protected]
*
* All rights reserved.
*
* This file is part of msr-safe.
*
* msr-safe is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* msr-safe is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with msr-safe. If not, see <http://www.gnu.org/licenses/>.
*
* This material is based upon work supported by the U.S. Department of
* Energy's Lawrence Livermore National Laboratory. Office of Science, under
* Award number DE-AC52-07NA27344.
*/
/*
* x86 MSR batch access device
*
* This device is accessed by ioctl() to submit a batch of MSR requests
* which may be used instead of or in addition to the lseek()/write()/read()
* mechanism provided by msr_safe.c
*
* This driver uses /dev/cpu/msr_batch as its device file.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <asm/msr.h>
#include <linux/cpu.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#include "msr_batch.h"
#include "msr_safe.h"
#include "msr_whitelist.h"
static int majordev;
static struct class *cdev_class;
static char cdev_created;
static char cdev_registered;
static char cdev_class_created;
static int msrbatch_open(struct inode *inode, struct file *file)
{
unsigned int cpu;
struct cpuinfo_x86 *c;
cpu = iminor(file->f_path.dentry->d_inode);
if (cpu >= nr_cpu_ids || !cpu_online(cpu))
{
pr_debug("cpu #%u does not exist\n", cpu);
return -ENXIO; // No such CPU
}
c = &cpu_data(cpu);
if (!cpu_has(c, X86_FEATURE_MSR))
{
pr_debug("cpu #%u does not have MSR feature.\n", cpu);
return -EIO; // MSR not supported
}
return 0;
}
static int msrbatch_close(struct inode *inode, struct file *file)
{
return 0;
}
static int msrbatch_apply_whitelist(struct msr_batch_array *oa)
{
struct msr_batch_op *op;
int err = 0;
bool has_sys_rawio_cap = capable(CAP_SYS_RAWIO);
for (op = oa->ops; op < oa->ops + oa->numops; ++op)
{
op->err = 0;
if (op->cpu >= nr_cpu_ids || !cpu_online(op->cpu))
{
pr_debug("No such CPU %d\n", op->cpu);
op->err = err = -ENXIO; // No such CPU
continue;
}
if (has_sys_rawio_cap)
{
op->wmask = 0xffffffffffffffff;
continue;
}
if (!msr_whitelist_maskexists(op->msr))
{
pr_debug("No whitelist entry for MSR %x\n", op->msr);
op->err = err = -EACCES;
}
else
{
op->wmask = msr_whitelist_writemask(op->msr);
/* Check for read-only case */
if (op->wmask == 0 && !op->isrdmsr)
{
pr_debug("MSR %x is read-only\n", op->msr);
op->err = err = -EACCES;
}
}
}
return err;
}
extern int msr_safe_batch(struct msr_batch_array *oa);
static long msrbatch_ioctl(struct file *f, unsigned int ioc, unsigned long arg)
{
int err = 0;
struct msr_batch_array __user *uoa;
struct msr_batch_op __user *uops;
struct msr_batch_array koa;
if (ioc != X86_IOC_MSR_BATCH)
{
pr_debug("Invalid ioctl op %u\n", ioc);
return -ENOTTY;
}
if (!(f->f_mode & FMODE_READ))
{
pr_debug("File not open for reading\n");
return -EBADF;
}
uoa = (struct msr_batch_array *)arg;
if (copy_from_user(&koa, uoa, sizeof(koa)))
{
pr_debug("Copy of batch array descriptor failed\n");
return -EFAULT;
}
if (koa.numops <= 0)
{
pr_debug("Invalid # of ops %d\n", koa.numops);
return -EINVAL;
}
uops = koa.ops;
koa.ops = kmalloc_array(koa.numops, sizeof(*koa.ops), GFP_KERNEL);
if (!koa.ops)
{
return -ENOMEM;
}
if (copy_from_user(koa.ops, uops, koa.numops * sizeof(*koa.ops)))
{
pr_debug("Copy of batch array failed\n");
err = -EFAULT;
goto bundle_alloc;
}
err = msrbatch_apply_whitelist(&koa);
if (err)
{
pr_debug("Failed to apply whitelist %d\n", err);
goto copyout_and_return;
}
err = msr_safe_batch(&koa);
if (err != 0)
{
pr_debug("msr_safe_batch failed: %d\n", err);
goto copyout_and_return;
}
copyout_and_return:
if (copy_to_user(uops, koa.ops, koa.numops * sizeof(*uops)))
{
pr_debug("copy batch data back to user failed\n");
if (!err)
{
err = -EFAULT;
}
}
bundle_alloc:
kfree(koa.ops);
return err;
}
static const struct file_operations fops =
{
.owner = THIS_MODULE,
.open = msrbatch_open,
.unlocked_ioctl = msrbatch_ioctl,
.compat_ioctl = msrbatch_ioctl,
.release = msrbatch_close
};
void msrbatch_cleanup(void)
{
if (cdev_created)
{
cdev_created = 0;
device_destroy(cdev_class, MKDEV(majordev, 0));
}
if (cdev_class_created)
{
cdev_class_created = 0;
class_destroy(cdev_class);
}
if (cdev_registered)
{
cdev_registered = 0;
unregister_chrdev(majordev, "cpu/msr_batch");
}
}
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,39)
static char *msrbatch_nodename(struct device *dev, mode_t *mode)
#else
static char *msrbatch_nodename(struct device *dev, umode_t *mode)
#endif
{
return kasprintf(GFP_KERNEL, "cpu/msr_batch");
}
int msrbatch_init(void)
{
int err;
struct device *dev;
majordev = register_chrdev(0, "cpu/msr_batch", &fops);
if (majordev < 0)
{
pr_debug("msrbatch_init: unable to register chrdev\n");
msrbatch_cleanup();
return -EBUSY;
}
cdev_registered = 1;
cdev_class = class_create(THIS_MODULE, "msr_batch");
if (IS_ERR(cdev_class))
{
err = PTR_ERR(cdev_class);
msrbatch_cleanup();
return err;
}
cdev_class_created = 1;
cdev_class->devnode = msrbatch_nodename;
dev = device_create(cdev_class, NULL, MKDEV(majordev, 0), NULL, "msr_batch");
if (IS_ERR(dev))
{
err = PTR_ERR(dev);
msrbatch_cleanup();
return err;
}
cdev_created = 1;
return 0;
}