forked from rountree/msr-safe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msr_batch.c
238 lines (205 loc) · 5.42 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
// Copyright 2011-2021 Lawrence Livermore National Security, LLC and other
// msr-safe Project Developers. See the top-level COPYRIGHT file for
// details.
//
// SPDX-License-Identifier: GPL-2.0-only
/*
* 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_allowlist.h"
static struct class *cdev_class;
static char cdev_created;
static char cdev_registered;
static char cdev_class_created;
static int msrbatch_apply_allowlist(struct msr_batch_array *oa)
{
struct msr_batch_op *op;
int err = 0;
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 (!msr_allowlist_maskexists(op->msr))
{
pr_debug("No allowlist entry for MSR %x\n", op->msr);
op->err = err = -EACCES;
}
else
{
op->wmask = msr_allowlist_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 = -EROFS;
}
}
}
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 -E2BIG;
}
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_allowlist(&koa);
if (err)
{
pr_debug("Failed to apply allowlist %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,
.unlocked_ioctl = msrbatch_ioctl,
.compat_ioctl = msrbatch_ioctl
};
void msrbatch_cleanup(int majordev)
{
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
#if LINUX_VERSION_CODE <= KERNEL_VERSION(6,2,0)
static char *msrbatch_nodename(struct device *dev, umode_t *mode)
#else
static char *msrbatch_nodename(const struct device *dev, umode_t *mode)
#endif
#endif
{
return kasprintf(GFP_KERNEL, "cpu/msr_batch");
}
int msrbatch_init(int *majordev)
{
int err = 0;
struct device *dev;
err = register_chrdev(*majordev, "cpu/msr_batch", &fops);
if (err < 0)
{
pr_debug("%s: unable to register chrdev\n", __FUNCTION__);
msrbatch_cleanup(*majordev);
return -EBUSY;
}
if (err > 0)
{
*majordev = err;
}
cdev_registered = 1;
cdev_class = class_create(
#if LINUX_VERSION_CODE < KERNEL_VERSION(6,4,0)
THIS_MODULE,
#endif
"msr_batch");
if (IS_ERR(cdev_class))
{
err = PTR_ERR(cdev_class);
msrbatch_cleanup(*majordev);
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(*majordev);
return err;
}
cdev_created = 1;
return 0;
}