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>
17 #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
19 static const char *const mtrr_strings[MTRR_NUM_TYPES] =
22 "write-combining", /* 1 */
25 "write-through", /* 4 */
26 "write-protect", /* 5 */
30 const char *mtrr_attrib_to_str(int x)
32 return (x <= 6) ? mtrr_strings[x] : "?";
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);
46 fcount = kcalloc(max, sizeof(*fcount), GFP_KERNEL);
49 FILE_FCOUNT(file) = fcount;
52 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
57 reg = mtrr_add_page(base, size, type, true);
64 mtrr_file_del(unsigned long base, unsigned long size,
65 struct file *file, int page)
67 unsigned int *fcount = FILE_FCOUNT(file);
71 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
76 reg = mtrr_del_page(-1, base, size);
88 * seq_file can seek but we ignore it.
90 * Format of control line:
91 * "base=%Lx size=%Lx type=%s" or "disable=%d"
94 mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
98 unsigned long long base, size;
100 char line[LINE_SIZE];
103 memset(line, 0, LINE_SIZE);
105 len = min_t(size_t, len, LINE_SIZE - 1);
106 length = strncpy_from_user(line, buf, len);
110 ptr = line + length - 1;
111 if (length && *ptr == '\n')
114 if (!strncmp(line, "disable=", 8)) {
115 reg = simple_strtoul(line + 8, &ptr, 0);
116 err = mtrr_del_page(reg, 0, 0);
122 if (strncmp(line, "base=", 5))
125 base = simple_strtoull(line + 5, &ptr, 0);
126 ptr = skip_spaces(ptr);
128 if (strncmp(ptr, "size=", 5))
131 size = simple_strtoull(ptr + 5, &ptr, 0);
132 if ((base & 0xfff) || (size & 0xfff))
134 ptr = skip_spaces(ptr);
136 if (strncmp(ptr, "type=", 5))
138 ptr = skip_spaces(ptr + 5);
140 i = match_string(mtrr_strings, MTRR_NUM_TYPES, ptr);
146 err = mtrr_add_page((unsigned long)base, (unsigned long)size, i, true);
153 mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
159 struct mtrr_sentry sentry;
160 struct mtrr_gentry gentry;
161 void __user *arg = (void __user *) __arg;
163 memset(&gentry, 0, sizeof(gentry));
166 case MTRRIOC_ADD_ENTRY:
167 case MTRRIOC_SET_ENTRY:
168 case MTRRIOC_DEL_ENTRY:
169 case MTRRIOC_KILL_ENTRY:
170 case MTRRIOC_ADD_PAGE_ENTRY:
171 case MTRRIOC_SET_PAGE_ENTRY:
172 case MTRRIOC_DEL_PAGE_ENTRY:
173 case MTRRIOC_KILL_PAGE_ENTRY:
174 if (copy_from_user(&sentry, arg, sizeof(sentry)))
177 case MTRRIOC_GET_ENTRY:
178 case MTRRIOC_GET_PAGE_ENTRY:
179 if (copy_from_user(&gentry, arg, sizeof(gentry)))
183 case MTRRIOC32_ADD_ENTRY:
184 case MTRRIOC32_SET_ENTRY:
185 case MTRRIOC32_DEL_ENTRY:
186 case MTRRIOC32_KILL_ENTRY:
187 case MTRRIOC32_ADD_PAGE_ENTRY:
188 case MTRRIOC32_SET_PAGE_ENTRY:
189 case MTRRIOC32_DEL_PAGE_ENTRY:
190 case MTRRIOC32_KILL_PAGE_ENTRY: {
191 struct mtrr_sentry32 __user *s32;
193 s32 = (struct mtrr_sentry32 __user *)__arg;
194 err = get_user(sentry.base, &s32->base);
195 err |= get_user(sentry.size, &s32->size);
196 err |= get_user(sentry.type, &s32->type);
201 case MTRRIOC32_GET_ENTRY:
202 case MTRRIOC32_GET_PAGE_ENTRY: {
203 struct mtrr_gentry32 __user *g32;
205 g32 = (struct mtrr_gentry32 __user *)__arg;
206 err = get_user(gentry.regnum, &g32->regnum);
207 err |= get_user(gentry.base, &g32->base);
208 err |= get_user(gentry.size, &g32->size);
209 err |= get_user(gentry.type, &g32->type);
220 case MTRRIOC_ADD_ENTRY:
222 case MTRRIOC32_ADD_ENTRY:
225 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
228 case MTRRIOC_SET_ENTRY:
230 case MTRRIOC32_SET_ENTRY:
232 err = mtrr_add(sentry.base, sentry.size, sentry.type, false);
234 case MTRRIOC_DEL_ENTRY:
236 case MTRRIOC32_DEL_ENTRY:
238 err = mtrr_file_del(sentry.base, sentry.size, file, 0);
240 case MTRRIOC_KILL_ENTRY:
242 case MTRRIOC32_KILL_ENTRY:
244 err = mtrr_del(-1, sentry.base, sentry.size);
246 case MTRRIOC_GET_ENTRY:
248 case MTRRIOC32_GET_ENTRY:
250 if (gentry.regnum >= num_var_ranges)
252 mtrr_if->get(gentry.regnum, &base, &size, &type);
254 /* Hide entries that go above 4GB */
255 if (base + size - 1 >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT))
256 || size >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT)))
257 gentry.base = gentry.size = gentry.type = 0;
259 gentry.base = base << PAGE_SHIFT;
260 gentry.size = size << PAGE_SHIFT;
265 case MTRRIOC_ADD_PAGE_ENTRY:
267 case MTRRIOC32_ADD_PAGE_ENTRY:
270 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
273 case MTRRIOC_SET_PAGE_ENTRY:
275 case MTRRIOC32_SET_PAGE_ENTRY:
278 mtrr_add_page(sentry.base, sentry.size, sentry.type, false);
280 case MTRRIOC_DEL_PAGE_ENTRY:
282 case MTRRIOC32_DEL_PAGE_ENTRY:
284 err = mtrr_file_del(sentry.base, sentry.size, file, 1);
286 case MTRRIOC_KILL_PAGE_ENTRY:
288 case MTRRIOC32_KILL_PAGE_ENTRY:
290 err = mtrr_del_page(-1, sentry.base, sentry.size);
292 case MTRRIOC_GET_PAGE_ENTRY:
294 case MTRRIOC32_GET_PAGE_ENTRY:
296 if (gentry.regnum >= num_var_ranges)
298 mtrr_if->get(gentry.regnum, &base, &size, &type);
299 /* Hide entries that would overflow */
300 if (size != (__typeof__(gentry.size))size)
301 gentry.base = gentry.size = gentry.type = 0;
314 case MTRRIOC_GET_ENTRY:
315 case MTRRIOC_GET_PAGE_ENTRY:
316 if (copy_to_user(arg, &gentry, sizeof(gentry)))
320 case MTRRIOC32_GET_ENTRY:
321 case MTRRIOC32_GET_PAGE_ENTRY: {
322 struct mtrr_gentry32 __user *g32;
324 g32 = (struct mtrr_gentry32 __user *)__arg;
325 err = put_user(gentry.base, &g32->base);
326 err |= put_user(gentry.size, &g32->size);
327 err |= put_user(gentry.regnum, &g32->regnum);
328 err |= put_user(gentry.type, &g32->type);
336 static int mtrr_close(struct inode *ino, struct file *file)
338 unsigned int *fcount = FILE_FCOUNT(file);
341 if (fcount != NULL) {
342 max = num_var_ranges;
343 for (i = 0; i < max; ++i) {
344 while (fcount[i] > 0) {
350 FILE_FCOUNT(file) = NULL;
352 return single_release(ino, file);
355 static int mtrr_seq_show(struct seq_file *seq, void *offset)
360 unsigned long base, size;
362 max = num_var_ranges;
363 for (i = 0; i < max; i++) {
364 mtrr_if->get(i, &base, &size, &type);
366 mtrr_usage_table[i] = 0;
369 if (size < (0x100000 >> PAGE_SHIFT)) {
372 size <<= PAGE_SHIFT - 10;
375 size >>= 20 - PAGE_SHIFT;
377 /* Base can be > 32bit */
378 seq_printf(seq, "reg%02i: base=0x%06lx000 (%5luMB), size=%5lu%cB, count=%d: %s\n",
379 i, base, base >> (20 - PAGE_SHIFT),
381 mtrr_usage_table[i], mtrr_attrib_to_str(type));
386 static int mtrr_open(struct inode *inode, struct file *file)
392 if (!capable(CAP_SYS_ADMIN))
394 return single_open(file, mtrr_seq_show, NULL);
397 static const struct proc_ops mtrr_proc_ops = {
398 .proc_open = mtrr_open,
399 .proc_read = seq_read,
400 .proc_lseek = seq_lseek,
401 .proc_write = mtrr_write,
402 .proc_ioctl = mtrr_ioctl,
404 .proc_compat_ioctl = mtrr_ioctl,
406 .proc_release = mtrr_close,
409 static int __init mtrr_if_init(void)
411 struct cpuinfo_x86 *c = &boot_cpu_data;
413 if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
414 (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
415 (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
416 (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
419 proc_create("mtrr", S_IWUSR | S_IRUGO, NULL, &mtrr_proc_ops);
422 arch_initcall(mtrr_if_init);
423 #endif /* CONFIG_PROC_FS */