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