1 #include <linux/init.h>
2 #include <linux/proc_fs.h>
3 #include <linux/capability.h>
4 #include <linux/ctype.h>
5 #include <linux/module.h>
6 #include <linux/seq_file.h>
7 #include <asm/uaccess.h>
14 /* RED-PEN: this is accessed without any locking */
15 extern unsigned int *usage_table;
18 #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
20 static const char *const mtrr_strings[MTRR_NUM_TYPES] =
23 "write-combining", /* 1 */
26 "write-through", /* 4 */
27 "write-protect", /* 5 */
31 const char *mtrr_attrib_to_str(int x)
33 return (x <= 6) ? mtrr_strings[x] : "?";
39 mtrr_file_add(unsigned long base, unsigned long size,
40 unsigned int type, bool increment, struct file *file, int page)
43 unsigned int *fcount = FILE_FCOUNT(file);
47 fcount = kzalloc(max * sizeof *fcount, GFP_KERNEL);
50 FILE_FCOUNT(file) = fcount;
53 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
58 reg = mtrr_add_page(base, size, type, true);
65 mtrr_file_del(unsigned long base, unsigned long size,
66 struct file *file, int page)
69 unsigned int *fcount = FILE_FCOUNT(file);
72 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
77 reg = mtrr_del_page(-1, base, size);
88 /* RED-PEN: seq_file can seek now. this is ignored. */
90 mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
91 /* Format of control line:
92 "base=%Lx size=%Lx type=%s" OR:
98 unsigned long long base, size;
100 char line[LINE_SIZE];
103 if (!capable(CAP_SYS_ADMIN))
107 memset(line, 0, LINE_SIZE);
110 if (copy_from_user(line, buf, len - 1))
112 linelen = strlen(line);
113 ptr = line + linelen - 1;
114 if (linelen && *ptr == '\n')
116 if (!strncmp(line, "disable=", 8)) {
117 reg = simple_strtoul(line + 8, &ptr, 0);
118 err = mtrr_del_page(reg, 0, 0);
123 if (strncmp(line, "base=", 5))
125 base = simple_strtoull(line + 5, &ptr, 0);
126 for (; isspace(*ptr); ++ptr) ;
127 if (strncmp(ptr, "size=", 5))
129 size = simple_strtoull(ptr + 5, &ptr, 0);
130 if ((base & 0xfff) || (size & 0xfff))
132 for (; isspace(*ptr); ++ptr) ;
133 if (strncmp(ptr, "type=", 5))
136 for (; isspace(*ptr); ++ptr) ;
137 for (i = 0; i < MTRR_NUM_TYPES; ++i) {
138 if (strcmp(ptr, mtrr_strings[i]))
143 mtrr_add_page((unsigned long) base, (unsigned long) size, i,
153 mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
158 struct mtrr_sentry sentry;
159 struct mtrr_gentry gentry;
160 void __user *arg = (void __user *) __arg;
163 case MTRRIOC_ADD_ENTRY:
164 case MTRRIOC_SET_ENTRY:
165 case MTRRIOC_DEL_ENTRY:
166 case MTRRIOC_KILL_ENTRY:
167 case MTRRIOC_ADD_PAGE_ENTRY:
168 case MTRRIOC_SET_PAGE_ENTRY:
169 case MTRRIOC_DEL_PAGE_ENTRY:
170 case MTRRIOC_KILL_PAGE_ENTRY:
171 if (copy_from_user(&sentry, arg, sizeof sentry))
174 case MTRRIOC_GET_ENTRY:
175 case MTRRIOC_GET_PAGE_ENTRY:
176 if (copy_from_user(&gentry, arg, sizeof gentry))
180 case MTRRIOC32_ADD_ENTRY:
181 case MTRRIOC32_SET_ENTRY:
182 case MTRRIOC32_DEL_ENTRY:
183 case MTRRIOC32_KILL_ENTRY:
184 case MTRRIOC32_ADD_PAGE_ENTRY:
185 case MTRRIOC32_SET_PAGE_ENTRY:
186 case MTRRIOC32_DEL_PAGE_ENTRY:
187 case MTRRIOC32_KILL_PAGE_ENTRY: {
188 struct mtrr_sentry32 __user *s32 = (struct mtrr_sentry32 __user *)__arg;
189 err = get_user(sentry.base, &s32->base);
190 err |= get_user(sentry.size, &s32->size);
191 err |= get_user(sentry.type, &s32->type);
196 case MTRRIOC32_GET_ENTRY:
197 case MTRRIOC32_GET_PAGE_ENTRY: {
198 struct mtrr_gentry32 __user *g32 = (struct mtrr_gentry32 __user *)__arg;
199 err = get_user(gentry.regnum, &g32->regnum);
200 err |= get_user(gentry.base, &g32->base);
201 err |= get_user(gentry.size, &g32->size);
202 err |= get_user(gentry.type, &g32->type);
213 case MTRRIOC_ADD_ENTRY:
215 case MTRRIOC32_ADD_ENTRY:
217 if (!capable(CAP_SYS_ADMIN))
220 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
223 case MTRRIOC_SET_ENTRY:
225 case MTRRIOC32_SET_ENTRY:
227 if (!capable(CAP_SYS_ADMIN))
229 err = mtrr_add(sentry.base, sentry.size, sentry.type, false);
231 case MTRRIOC_DEL_ENTRY:
233 case MTRRIOC32_DEL_ENTRY:
235 if (!capable(CAP_SYS_ADMIN))
237 err = mtrr_file_del(sentry.base, sentry.size, file, 0);
239 case MTRRIOC_KILL_ENTRY:
241 case MTRRIOC32_KILL_ENTRY:
243 if (!capable(CAP_SYS_ADMIN))
245 err = mtrr_del(-1, sentry.base, sentry.size);
247 case MTRRIOC_GET_ENTRY:
249 case MTRRIOC32_GET_ENTRY:
251 if (gentry.regnum >= num_var_ranges)
253 mtrr_if->get(gentry.regnum, &gentry.base, &size, &type);
255 /* Hide entries that go above 4GB */
256 if (gentry.base + size - 1 >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT))
257 || size >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT)))
258 gentry.base = gentry.size = gentry.type = 0;
260 gentry.base <<= PAGE_SHIFT;
261 gentry.size = size << PAGE_SHIFT;
266 case MTRRIOC_ADD_PAGE_ENTRY:
268 case MTRRIOC32_ADD_PAGE_ENTRY:
270 if (!capable(CAP_SYS_ADMIN))
273 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
276 case MTRRIOC_SET_PAGE_ENTRY:
278 case MTRRIOC32_SET_PAGE_ENTRY:
280 if (!capable(CAP_SYS_ADMIN))
283 mtrr_add_page(sentry.base, sentry.size, sentry.type, false);
285 case MTRRIOC_DEL_PAGE_ENTRY:
287 case MTRRIOC32_DEL_PAGE_ENTRY:
289 if (!capable(CAP_SYS_ADMIN))
291 err = mtrr_file_del(sentry.base, sentry.size, file, 1);
293 case MTRRIOC_KILL_PAGE_ENTRY:
295 case MTRRIOC32_KILL_PAGE_ENTRY:
297 if (!capable(CAP_SYS_ADMIN))
299 err = mtrr_del_page(-1, sentry.base, sentry.size);
301 case MTRRIOC_GET_PAGE_ENTRY:
303 case MTRRIOC32_GET_PAGE_ENTRY:
305 if (gentry.regnum >= num_var_ranges)
307 mtrr_if->get(gentry.regnum, &gentry.base, &size, &type);
308 /* Hide entries that would overflow */
309 if (size != (__typeof__(gentry.size))size)
310 gentry.base = gentry.size = gentry.type = 0;
322 case MTRRIOC_GET_ENTRY:
323 case MTRRIOC_GET_PAGE_ENTRY:
324 if (copy_to_user(arg, &gentry, sizeof gentry))
328 case MTRRIOC32_GET_ENTRY:
329 case MTRRIOC32_GET_PAGE_ENTRY: {
330 struct mtrr_gentry32 __user *g32 = (struct mtrr_gentry32 __user *)__arg;
331 err = put_user(gentry.base, &g32->base);
332 err |= put_user(gentry.size, &g32->size);
333 err |= put_user(gentry.regnum, &g32->regnum);
334 err |= put_user(gentry.type, &g32->type);
343 mtrr_close(struct inode *ino, struct file *file)
346 unsigned int *fcount = FILE_FCOUNT(file);
348 if (fcount != NULL) {
349 max = num_var_ranges;
350 for (i = 0; i < max; ++i) {
351 while (fcount[i] > 0) {
357 FILE_FCOUNT(file) = NULL;
359 return single_release(ino, file);
362 static int mtrr_seq_show(struct seq_file *seq, void *offset);
364 static int mtrr_open(struct inode *inode, struct file *file)
370 return single_open(file, mtrr_seq_show, NULL);
373 static const struct file_operations mtrr_fops = {
374 .owner = THIS_MODULE,
379 .unlocked_ioctl = mtrr_ioctl,
380 .compat_ioctl = mtrr_ioctl,
381 .release = mtrr_close,
385 static struct proc_dir_entry *proc_root_mtrr;
388 static int mtrr_seq_show(struct seq_file *seq, void *offset)
393 unsigned long base, size;
396 max = num_var_ranges;
397 for (i = 0; i < max; i++) {
398 mtrr_if->get(i, &base, &size, &type);
402 if (size < (0x100000 >> PAGE_SHIFT)) {
405 size <<= PAGE_SHIFT - 10;
408 size >>= 20 - PAGE_SHIFT;
410 /* RED-PEN: base can be > 32bit */
411 len += seq_printf(seq,
412 "reg%02i: base=0x%05lx000 (%4luMB), size=%4lu%cB: %s, count=%d\n",
413 i, base, base >> (20 - PAGE_SHIFT), size, factor,
414 mtrr_attrib_to_str(type), usage_table[i]);
420 static int __init mtrr_if_init(void)
422 struct cpuinfo_x86 *c = &boot_cpu_data;
424 if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
425 (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
426 (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
427 (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
431 create_proc_entry("mtrr", S_IWUSR | S_IRUGO, &proc_root);
432 if (proc_root_mtrr) {
433 proc_root_mtrr->owner = THIS_MODULE;
434 proc_root_mtrr->proc_fops = &mtrr_fops;
439 arch_initcall(mtrr_if_init);
440 #endif /* CONFIG_PROC_FS */