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