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];
104 if (!capable(CAP_SYS_ADMIN))
107 memset(line, 0, LINE_SIZE);
109 len = min_t(size_t, len, LINE_SIZE - 1);
110 length = strncpy_from_user(line, buf, len);
114 linelen = strlen(line);
115 ptr = line + linelen - 1;
116 if (linelen && *ptr == '\n')
119 if (!strncmp(line, "disable=", 8)) {
120 reg = simple_strtoul(line + 8, &ptr, 0);
121 err = mtrr_del_page(reg, 0, 0);
127 if (strncmp(line, "base=", 5))
130 base = simple_strtoull(line + 5, &ptr, 0);
131 ptr = skip_spaces(ptr);
133 if (strncmp(ptr, "size=", 5))
136 size = simple_strtoull(ptr + 5, &ptr, 0);
137 if ((base & 0xfff) || (size & 0xfff))
139 ptr = skip_spaces(ptr);
141 if (strncmp(ptr, "type=", 5))
143 ptr = skip_spaces(ptr + 5);
145 i = match_string(mtrr_strings, MTRR_NUM_TYPES, ptr);
151 err = mtrr_add_page((unsigned long)base, (unsigned long)size, i, true);
158 mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
164 struct mtrr_sentry sentry;
165 struct mtrr_gentry gentry;
166 void __user *arg = (void __user *) __arg;
168 memset(&gentry, 0, sizeof(gentry));
171 case MTRRIOC_ADD_ENTRY:
172 case MTRRIOC_SET_ENTRY:
173 case MTRRIOC_DEL_ENTRY:
174 case MTRRIOC_KILL_ENTRY:
175 case MTRRIOC_ADD_PAGE_ENTRY:
176 case MTRRIOC_SET_PAGE_ENTRY:
177 case MTRRIOC_DEL_PAGE_ENTRY:
178 case MTRRIOC_KILL_PAGE_ENTRY:
179 if (copy_from_user(&sentry, arg, sizeof sentry))
182 case MTRRIOC_GET_ENTRY:
183 case MTRRIOC_GET_PAGE_ENTRY:
184 if (copy_from_user(&gentry, arg, sizeof gentry))
188 case MTRRIOC32_ADD_ENTRY:
189 case MTRRIOC32_SET_ENTRY:
190 case MTRRIOC32_DEL_ENTRY:
191 case MTRRIOC32_KILL_ENTRY:
192 case MTRRIOC32_ADD_PAGE_ENTRY:
193 case MTRRIOC32_SET_PAGE_ENTRY:
194 case MTRRIOC32_DEL_PAGE_ENTRY:
195 case MTRRIOC32_KILL_PAGE_ENTRY: {
196 struct mtrr_sentry32 __user *s32;
198 s32 = (struct mtrr_sentry32 __user *)__arg;
199 err = get_user(sentry.base, &s32->base);
200 err |= get_user(sentry.size, &s32->size);
201 err |= get_user(sentry.type, &s32->type);
206 case MTRRIOC32_GET_ENTRY:
207 case MTRRIOC32_GET_PAGE_ENTRY: {
208 struct mtrr_gentry32 __user *g32;
210 g32 = (struct mtrr_gentry32 __user *)__arg;
211 err = get_user(gentry.regnum, &g32->regnum);
212 err |= get_user(gentry.base, &g32->base);
213 err |= get_user(gentry.size, &g32->size);
214 err |= get_user(gentry.type, &g32->type);
225 case MTRRIOC_ADD_ENTRY:
227 case MTRRIOC32_ADD_ENTRY:
229 if (!capable(CAP_SYS_ADMIN))
232 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
235 case MTRRIOC_SET_ENTRY:
237 case MTRRIOC32_SET_ENTRY:
239 if (!capable(CAP_SYS_ADMIN))
241 err = mtrr_add(sentry.base, sentry.size, sentry.type, false);
243 case MTRRIOC_DEL_ENTRY:
245 case MTRRIOC32_DEL_ENTRY:
247 if (!capable(CAP_SYS_ADMIN))
249 err = mtrr_file_del(sentry.base, sentry.size, file, 0);
251 case MTRRIOC_KILL_ENTRY:
253 case MTRRIOC32_KILL_ENTRY:
255 if (!capable(CAP_SYS_ADMIN))
257 err = mtrr_del(-1, sentry.base, sentry.size);
259 case MTRRIOC_GET_ENTRY:
261 case MTRRIOC32_GET_ENTRY:
263 if (gentry.regnum >= num_var_ranges)
265 mtrr_if->get(gentry.regnum, &base, &size, &type);
267 /* Hide entries that go above 4GB */
268 if (base + size - 1 >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT))
269 || size >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT)))
270 gentry.base = gentry.size = gentry.type = 0;
272 gentry.base = base << PAGE_SHIFT;
273 gentry.size = size << PAGE_SHIFT;
278 case MTRRIOC_ADD_PAGE_ENTRY:
280 case MTRRIOC32_ADD_PAGE_ENTRY:
282 if (!capable(CAP_SYS_ADMIN))
285 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
288 case MTRRIOC_SET_PAGE_ENTRY:
290 case MTRRIOC32_SET_PAGE_ENTRY:
292 if (!capable(CAP_SYS_ADMIN))
295 mtrr_add_page(sentry.base, sentry.size, sentry.type, false);
297 case MTRRIOC_DEL_PAGE_ENTRY:
299 case MTRRIOC32_DEL_PAGE_ENTRY:
301 if (!capable(CAP_SYS_ADMIN))
303 err = mtrr_file_del(sentry.base, sentry.size, file, 1);
305 case MTRRIOC_KILL_PAGE_ENTRY:
307 case MTRRIOC32_KILL_PAGE_ENTRY:
309 if (!capable(CAP_SYS_ADMIN))
311 err = mtrr_del_page(-1, sentry.base, sentry.size);
313 case MTRRIOC_GET_PAGE_ENTRY:
315 case MTRRIOC32_GET_PAGE_ENTRY:
317 if (gentry.regnum >= num_var_ranges)
319 mtrr_if->get(gentry.regnum, &base, &size, &type);
320 /* Hide entries that would overflow */
321 if (size != (__typeof__(gentry.size))size)
322 gentry.base = gentry.size = gentry.type = 0;
335 case MTRRIOC_GET_ENTRY:
336 case MTRRIOC_GET_PAGE_ENTRY:
337 if (copy_to_user(arg, &gentry, sizeof gentry))
341 case MTRRIOC32_GET_ENTRY:
342 case MTRRIOC32_GET_PAGE_ENTRY: {
343 struct mtrr_gentry32 __user *g32;
345 g32 = (struct mtrr_gentry32 __user *)__arg;
346 err = put_user(gentry.base, &g32->base);
347 err |= put_user(gentry.size, &g32->size);
348 err |= put_user(gentry.regnum, &g32->regnum);
349 err |= put_user(gentry.type, &g32->type);
357 static int mtrr_close(struct inode *ino, struct file *file)
359 unsigned int *fcount = FILE_FCOUNT(file);
362 if (fcount != NULL) {
363 max = num_var_ranges;
364 for (i = 0; i < max; ++i) {
365 while (fcount[i] > 0) {
371 FILE_FCOUNT(file) = NULL;
373 return single_release(ino, file);
376 static int mtrr_seq_show(struct seq_file *seq, void *offset);
378 static int mtrr_open(struct inode *inode, struct file *file)
384 return single_open(file, mtrr_seq_show, NULL);
387 static const struct file_operations mtrr_fops = {
388 .owner = THIS_MODULE,
393 .unlocked_ioctl = mtrr_ioctl,
394 .compat_ioctl = mtrr_ioctl,
395 .release = mtrr_close,
398 static int mtrr_seq_show(struct seq_file *seq, void *offset)
403 unsigned long base, size;
405 max = num_var_ranges;
406 for (i = 0; i < max; i++) {
407 mtrr_if->get(i, &base, &size, &type);
409 mtrr_usage_table[i] = 0;
412 if (size < (0x100000 >> PAGE_SHIFT)) {
415 size <<= PAGE_SHIFT - 10;
418 size >>= 20 - PAGE_SHIFT;
420 /* Base can be > 32bit */
421 seq_printf(seq, "reg%02i: base=0x%06lx000 (%5luMB), size=%5lu%cB, count=%d: %s\n",
422 i, base, base >> (20 - PAGE_SHIFT),
424 mtrr_usage_table[i], mtrr_attrib_to_str(type));
429 static int __init mtrr_if_init(void)
431 struct cpuinfo_x86 *c = &boot_cpu_data;
433 if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
434 (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
435 (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
436 (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
439 proc_create("mtrr", S_IWUSR | S_IRUGO, NULL, &mtrr_fops);
442 arch_initcall(mtrr_if_init);
443 #endif /* CONFIG_PROC_FS */