1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/capability.h>
3 #include <linux/seq_file.h>
4 #include <linux/uaccess.h>
5 #include <linux/proc_fs.h>
6 #include <linux/ctype.h>
7 #include <linux/string.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
10
11 #define LINE_SIZE 80
12
13 #include <asm/mtrr.h>
14
15 #include "mtrr.h"
16
17 #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
18
19 static const char *const mtrr_strings[MTRR_NUM_TYPES] =
20 {
21 "uncachable", /* 0 */
22 "write-combining", /* 1 */
23 "?", /* 2 */
24 "?", /* 3 */
25 "write-through", /* 4 */
26 "write-protect", /* 5 */
27 "write-back", /* 6 */
28 };
29
mtrr_attrib_to_str(int x)30 const char *mtrr_attrib_to_str(int x)
31 {
32 return (x <= 6) ? mtrr_strings[x] : "?";
33 }
34
35 #ifdef CONFIG_PROC_FS
36
37 static int
mtrr_file_add(unsigned long base,unsigned long size,unsigned int type,bool increment,struct file * file,int page)38 mtrr_file_add(unsigned long base, unsigned long size,
39 unsigned int type, bool increment, struct file *file, int page)
40 {
41 unsigned int *fcount = FILE_FCOUNT(file);
42 int reg, max;
43
44 max = num_var_ranges;
45 if (fcount == NULL) {
46 fcount = kcalloc(max, sizeof(*fcount), GFP_KERNEL);
47 if (!fcount)
48 return -ENOMEM;
49 FILE_FCOUNT(file) = fcount;
50 }
51 if (!page) {
52 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
53 return -EINVAL;
54 base >>= PAGE_SHIFT;
55 size >>= PAGE_SHIFT;
56 }
57 reg = mtrr_add_page(base, size, type, true);
58 if (reg >= 0)
59 ++fcount[reg];
60 return reg;
61 }
62
63 static int
mtrr_file_del(unsigned long base,unsigned long size,struct file * file,int page)64 mtrr_file_del(unsigned long base, unsigned long size,
65 struct file *file, int page)
66 {
67 unsigned int *fcount = FILE_FCOUNT(file);
68 int reg;
69
70 if (!page) {
71 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
72 return -EINVAL;
73 base >>= PAGE_SHIFT;
74 size >>= PAGE_SHIFT;
75 }
76 reg = mtrr_del_page(-1, base, size);
77 if (reg < 0)
78 return reg;
79 if (fcount == NULL)
80 return reg;
81 if (fcount[reg] < 1)
82 return -EINVAL;
83 --fcount[reg];
84 return reg;
85 }
86
87 /*
88 * seq_file can seek but we ignore it.
89 *
90 * Format of control line:
91 * "base=%Lx size=%Lx type=%s" or "disable=%d"
92 */
93 static ssize_t
mtrr_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)94 mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
95 {
96 int i, err;
97 unsigned long reg;
98 unsigned long long base, size;
99 char *ptr;
100 char line[LINE_SIZE];
101 int length;
102 size_t linelen;
103
104 if (!capable(CAP_SYS_ADMIN))
105 return -EPERM;
106
107 memset(line, 0, LINE_SIZE);
108
109 len = min_t(size_t, len, LINE_SIZE - 1);
110 length = strncpy_from_user(line, buf, len);
111 if (length < 0)
112 return length;
113
114 linelen = strlen(line);
115 ptr = line + linelen - 1;
116 if (linelen && *ptr == '\n')
117 *ptr = '\0';
118
119 if (!strncmp(line, "disable=", 8)) {
120 reg = simple_strtoul(line + 8, &ptr, 0);
121 err = mtrr_del_page(reg, 0, 0);
122 if (err < 0)
123 return err;
124 return len;
125 }
126
127 if (strncmp(line, "base=", 5))
128 return -EINVAL;
129
130 base = simple_strtoull(line + 5, &ptr, 0);
131 ptr = skip_spaces(ptr);
132
133 if (strncmp(ptr, "size=", 5))
134 return -EINVAL;
135
136 size = simple_strtoull(ptr + 5, &ptr, 0);
137 if ((base & 0xfff) || (size & 0xfff))
138 return -EINVAL;
139 ptr = skip_spaces(ptr);
140
141 if (strncmp(ptr, "type=", 5))
142 return -EINVAL;
143 ptr = skip_spaces(ptr + 5);
144
145 i = match_string(mtrr_strings, MTRR_NUM_TYPES, ptr);
146 if (i < 0)
147 return i;
148
149 base >>= PAGE_SHIFT;
150 size >>= PAGE_SHIFT;
151 err = mtrr_add_page((unsigned long)base, (unsigned long)size, i, true);
152 if (err < 0)
153 return err;
154 return len;
155 }
156
157 static long
mtrr_ioctl(struct file * file,unsigned int cmd,unsigned long __arg)158 mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
159 {
160 int err = 0;
161 mtrr_type type;
162 unsigned long base;
163 unsigned long size;
164 struct mtrr_sentry sentry;
165 struct mtrr_gentry gentry;
166 void __user *arg = (void __user *) __arg;
167
168 switch (cmd) {
169 case MTRRIOC_ADD_ENTRY:
170 case MTRRIOC_SET_ENTRY:
171 case MTRRIOC_DEL_ENTRY:
172 case MTRRIOC_KILL_ENTRY:
173 case MTRRIOC_ADD_PAGE_ENTRY:
174 case MTRRIOC_SET_PAGE_ENTRY:
175 case MTRRIOC_DEL_PAGE_ENTRY:
176 case MTRRIOC_KILL_PAGE_ENTRY:
177 if (copy_from_user(&sentry, arg, sizeof sentry))
178 return -EFAULT;
179 break;
180 case MTRRIOC_GET_ENTRY:
181 case MTRRIOC_GET_PAGE_ENTRY:
182 if (copy_from_user(&gentry, arg, sizeof gentry))
183 return -EFAULT;
184 break;
185 #ifdef CONFIG_COMPAT
186 case MTRRIOC32_ADD_ENTRY:
187 case MTRRIOC32_SET_ENTRY:
188 case MTRRIOC32_DEL_ENTRY:
189 case MTRRIOC32_KILL_ENTRY:
190 case MTRRIOC32_ADD_PAGE_ENTRY:
191 case MTRRIOC32_SET_PAGE_ENTRY:
192 case MTRRIOC32_DEL_PAGE_ENTRY:
193 case MTRRIOC32_KILL_PAGE_ENTRY: {
194 struct mtrr_sentry32 __user *s32;
195
196 s32 = (struct mtrr_sentry32 __user *)__arg;
197 err = get_user(sentry.base, &s32->base);
198 err |= get_user(sentry.size, &s32->size);
199 err |= get_user(sentry.type, &s32->type);
200 if (err)
201 return err;
202 break;
203 }
204 case MTRRIOC32_GET_ENTRY:
205 case MTRRIOC32_GET_PAGE_ENTRY: {
206 struct mtrr_gentry32 __user *g32;
207
208 g32 = (struct mtrr_gentry32 __user *)__arg;
209 err = get_user(gentry.regnum, &g32->regnum);
210 err |= get_user(gentry.base, &g32->base);
211 err |= get_user(gentry.size, &g32->size);
212 err |= get_user(gentry.type, &g32->type);
213 if (err)
214 return err;
215 break;
216 }
217 #endif
218 }
219
220 switch (cmd) {
221 default:
222 return -ENOTTY;
223 case MTRRIOC_ADD_ENTRY:
224 #ifdef CONFIG_COMPAT
225 case MTRRIOC32_ADD_ENTRY:
226 #endif
227 if (!capable(CAP_SYS_ADMIN))
228 return -EPERM;
229 err =
230 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
231 file, 0);
232 break;
233 case MTRRIOC_SET_ENTRY:
234 #ifdef CONFIG_COMPAT
235 case MTRRIOC32_SET_ENTRY:
236 #endif
237 if (!capable(CAP_SYS_ADMIN))
238 return -EPERM;
239 err = mtrr_add(sentry.base, sentry.size, sentry.type, false);
240 break;
241 case MTRRIOC_DEL_ENTRY:
242 #ifdef CONFIG_COMPAT
243 case MTRRIOC32_DEL_ENTRY:
244 #endif
245 if (!capable(CAP_SYS_ADMIN))
246 return -EPERM;
247 err = mtrr_file_del(sentry.base, sentry.size, file, 0);
248 break;
249 case MTRRIOC_KILL_ENTRY:
250 #ifdef CONFIG_COMPAT
251 case MTRRIOC32_KILL_ENTRY:
252 #endif
253 if (!capable(CAP_SYS_ADMIN))
254 return -EPERM;
255 err = mtrr_del(-1, sentry.base, sentry.size);
256 break;
257 case MTRRIOC_GET_ENTRY:
258 #ifdef CONFIG_COMPAT
259 case MTRRIOC32_GET_ENTRY:
260 #endif
261 if (gentry.regnum >= num_var_ranges)
262 return -EINVAL;
263 mtrr_if->get(gentry.regnum, &base, &size, &type);
264
265 /* Hide entries that go above 4GB */
266 if (base + size - 1 >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT))
267 || size >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT)))
268 gentry.base = gentry.size = gentry.type = 0;
269 else {
270 gentry.base = base << PAGE_SHIFT;
271 gentry.size = size << PAGE_SHIFT;
272 gentry.type = type;
273 }
274
275 break;
276 case MTRRIOC_ADD_PAGE_ENTRY:
277 #ifdef CONFIG_COMPAT
278 case MTRRIOC32_ADD_PAGE_ENTRY:
279 #endif
280 if (!capable(CAP_SYS_ADMIN))
281 return -EPERM;
282 err =
283 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
284 file, 1);
285 break;
286 case MTRRIOC_SET_PAGE_ENTRY:
287 #ifdef CONFIG_COMPAT
288 case MTRRIOC32_SET_PAGE_ENTRY:
289 #endif
290 if (!capable(CAP_SYS_ADMIN))
291 return -EPERM;
292 err =
293 mtrr_add_page(sentry.base, sentry.size, sentry.type, false);
294 break;
295 case MTRRIOC_DEL_PAGE_ENTRY:
296 #ifdef CONFIG_COMPAT
297 case MTRRIOC32_DEL_PAGE_ENTRY:
298 #endif
299 if (!capable(CAP_SYS_ADMIN))
300 return -EPERM;
301 err = mtrr_file_del(sentry.base, sentry.size, file, 1);
302 break;
303 case MTRRIOC_KILL_PAGE_ENTRY:
304 #ifdef CONFIG_COMPAT
305 case MTRRIOC32_KILL_PAGE_ENTRY:
306 #endif
307 if (!capable(CAP_SYS_ADMIN))
308 return -EPERM;
309 err = mtrr_del_page(-1, sentry.base, sentry.size);
310 break;
311 case MTRRIOC_GET_PAGE_ENTRY:
312 #ifdef CONFIG_COMPAT
313 case MTRRIOC32_GET_PAGE_ENTRY:
314 #endif
315 if (gentry.regnum >= num_var_ranges)
316 return -EINVAL;
317 mtrr_if->get(gentry.regnum, &base, &size, &type);
318 /* Hide entries that would overflow */
319 if (size != (__typeof__(gentry.size))size)
320 gentry.base = gentry.size = gentry.type = 0;
321 else {
322 gentry.base = base;
323 gentry.size = size;
324 gentry.type = type;
325 }
326 break;
327 }
328
329 if (err)
330 return err;
331
332 switch (cmd) {
333 case MTRRIOC_GET_ENTRY:
334 case MTRRIOC_GET_PAGE_ENTRY:
335 if (copy_to_user(arg, &gentry, sizeof gentry))
336 err = -EFAULT;
337 break;
338 #ifdef CONFIG_COMPAT
339 case MTRRIOC32_GET_ENTRY:
340 case MTRRIOC32_GET_PAGE_ENTRY: {
341 struct mtrr_gentry32 __user *g32;
342
343 g32 = (struct mtrr_gentry32 __user *)__arg;
344 err = put_user(gentry.base, &g32->base);
345 err |= put_user(gentry.size, &g32->size);
346 err |= put_user(gentry.regnum, &g32->regnum);
347 err |= put_user(gentry.type, &g32->type);
348 break;
349 }
350 #endif
351 }
352 return err;
353 }
354
mtrr_close(struct inode * ino,struct file * file)355 static int mtrr_close(struct inode *ino, struct file *file)
356 {
357 unsigned int *fcount = FILE_FCOUNT(file);
358 int i, max;
359
360 if (fcount != NULL) {
361 max = num_var_ranges;
362 for (i = 0; i < max; ++i) {
363 while (fcount[i] > 0) {
364 mtrr_del(i, 0, 0);
365 --fcount[i];
366 }
367 }
368 kfree(fcount);
369 FILE_FCOUNT(file) = NULL;
370 }
371 return single_release(ino, file);
372 }
373
374 static int mtrr_seq_show(struct seq_file *seq, void *offset);
375
mtrr_open(struct inode * inode,struct file * file)376 static int mtrr_open(struct inode *inode, struct file *file)
377 {
378 if (!mtrr_if)
379 return -EIO;
380 if (!mtrr_if->get)
381 return -ENXIO;
382 return single_open(file, mtrr_seq_show, NULL);
383 }
384
385 static const struct file_operations mtrr_fops = {
386 .owner = THIS_MODULE,
387 .open = mtrr_open,
388 .read = seq_read,
389 .llseek = seq_lseek,
390 .write = mtrr_write,
391 .unlocked_ioctl = mtrr_ioctl,
392 .compat_ioctl = mtrr_ioctl,
393 .release = mtrr_close,
394 };
395
mtrr_seq_show(struct seq_file * seq,void * offset)396 static int mtrr_seq_show(struct seq_file *seq, void *offset)
397 {
398 char factor;
399 int i, max;
400 mtrr_type type;
401 unsigned long base, size;
402
403 max = num_var_ranges;
404 for (i = 0; i < max; i++) {
405 mtrr_if->get(i, &base, &size, &type);
406 if (size == 0) {
407 mtrr_usage_table[i] = 0;
408 continue;
409 }
410 if (size < (0x100000 >> PAGE_SHIFT)) {
411 /* less than 1MB */
412 factor = 'K';
413 size <<= PAGE_SHIFT - 10;
414 } else {
415 factor = 'M';
416 size >>= 20 - PAGE_SHIFT;
417 }
418 /* Base can be > 32bit */
419 seq_printf(seq, "reg%02i: base=0x%06lx000 (%5luMB), size=%5lu%cB, count=%d: %s\n",
420 i, base, base >> (20 - PAGE_SHIFT),
421 size, factor,
422 mtrr_usage_table[i], mtrr_attrib_to_str(type));
423 }
424 return 0;
425 }
426
mtrr_if_init(void)427 static int __init mtrr_if_init(void)
428 {
429 struct cpuinfo_x86 *c = &boot_cpu_data;
430
431 if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
432 (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
433 (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
434 (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
435 return -ENODEV;
436
437 proc_create("mtrr", S_IWUSR | S_IRUGO, NULL, &mtrr_fops);
438 return 0;
439 }
440 arch_initcall(mtrr_if_init);
441 #endif /* CONFIG_PROC_FS */
442