1 #include <linux/init.h>
2 #include <linux/proc_fs.h>
3 #include <linux/ctype.h>
4 #include <linux/module.h>
5 #include <linux/seq_file.h>
6 #include <asm/uaccess.h>
13 /* RED-PEN: this is accessed without any locking */
14 extern unsigned int *usage_table;
17 #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
19 static char *mtrr_strings[MTRR_NUM_TYPES] =
22 "write-combining", /* 1 */
25 "write-through", /* 4 */
26 "write-protect", /* 5 */
30 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, char increment, struct file *file, int page)
42 unsigned int *fcount = FILE_FCOUNT(file);
46 fcount = kmalloc(max * sizeof *fcount, GFP_KERNEL);
49 memset(fcount, 0, max * sizeof *fcount);
50 FILE_FCOUNT(file) = fcount;
53 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
58 reg = mtrr_add_page(base, size, type, 1);
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];
102 if (!capable(CAP_SYS_ADMIN))
104 /* Can't seek (pwrite) on this device */
105 if (ppos != &file->f_pos)
107 memset(line, 0, LINE_SIZE);
110 if (copy_from_user(line, buf, len - 1))
112 ptr = line + strlen(line) - 1;
115 if (!strncmp(line, "disable=", 8)) {
116 reg = simple_strtoul(line + 8, &ptr, 0);
117 err = mtrr_del_page(reg, 0, 0);
122 if (strncmp(line, "base=", 5))
124 base = simple_strtoull(line + 5, &ptr, 0);
125 for (; isspace(*ptr); ++ptr) ;
126 if (strncmp(ptr, "size=", 5))
128 size = simple_strtoull(ptr + 5, &ptr, 0);
129 if ((base & 0xfff) || (size & 0xfff))
131 for (; isspace(*ptr); ++ptr) ;
132 if (strncmp(ptr, "type=", 5))
135 for (; isspace(*ptr); ++ptr) ;
136 for (i = 0; i < MTRR_NUM_TYPES; ++i) {
137 if (strcmp(ptr, mtrr_strings[i]))
142 mtrr_add_page((unsigned long) base, (unsigned long) size, i,
152 mtrr_ioctl(struct inode *inode, struct file *file,
153 unsigned int cmd, unsigned long __arg)
157 struct mtrr_sentry sentry;
158 struct mtrr_gentry gentry;
159 void __user *arg = (void __user *) __arg;
164 case MTRRIOC_ADD_ENTRY:
165 if (!capable(CAP_SYS_ADMIN))
167 if (copy_from_user(&sentry, arg, sizeof sentry))
170 mtrr_file_add(sentry.base, sentry.size, sentry.type, 1,
175 case MTRRIOC_SET_ENTRY:
176 if (!capable(CAP_SYS_ADMIN))
178 if (copy_from_user(&sentry, arg, sizeof sentry))
180 err = mtrr_add(sentry.base, sentry.size, sentry.type, 0);
184 case MTRRIOC_DEL_ENTRY:
185 if (!capable(CAP_SYS_ADMIN))
187 if (copy_from_user(&sentry, arg, sizeof sentry))
189 err = mtrr_file_del(sentry.base, sentry.size, file, 0);
193 case MTRRIOC_KILL_ENTRY:
194 if (!capable(CAP_SYS_ADMIN))
196 if (copy_from_user(&sentry, arg, sizeof sentry))
198 err = mtrr_del(-1, sentry.base, sentry.size);
202 case MTRRIOC_GET_ENTRY:
203 if (copy_from_user(&gentry, arg, sizeof gentry))
205 if (gentry.regnum >= num_var_ranges)
207 mtrr_if->get(gentry.regnum, &gentry.base, &gentry.size, &type);
209 /* Hide entries that go above 4GB */
210 if (gentry.base + gentry.size > 0x100000
211 || gentry.size == 0x100000)
212 gentry.base = gentry.size = gentry.type = 0;
214 gentry.base <<= PAGE_SHIFT;
215 gentry.size <<= PAGE_SHIFT;
219 if (copy_to_user(arg, &gentry, sizeof gentry))
222 case MTRRIOC_ADD_PAGE_ENTRY:
223 if (!capable(CAP_SYS_ADMIN))
225 if (copy_from_user(&sentry, arg, sizeof sentry))
228 mtrr_file_add(sentry.base, sentry.size, sentry.type, 1,
233 case MTRRIOC_SET_PAGE_ENTRY:
234 if (!capable(CAP_SYS_ADMIN))
236 if (copy_from_user(&sentry, arg, sizeof sentry))
238 err = mtrr_add_page(sentry.base, sentry.size, sentry.type, 0);
242 case MTRRIOC_DEL_PAGE_ENTRY:
243 if (!capable(CAP_SYS_ADMIN))
245 if (copy_from_user(&sentry, arg, sizeof sentry))
247 err = mtrr_file_del(sentry.base, sentry.size, file, 1);
251 case MTRRIOC_KILL_PAGE_ENTRY:
252 if (!capable(CAP_SYS_ADMIN))
254 if (copy_from_user(&sentry, arg, sizeof sentry))
256 err = mtrr_del_page(-1, sentry.base, sentry.size);
260 case MTRRIOC_GET_PAGE_ENTRY:
261 if (copy_from_user(&gentry, arg, sizeof gentry))
263 if (gentry.regnum >= num_var_ranges)
265 mtrr_if->get(gentry.regnum, &gentry.base, &gentry.size, &type);
268 if (copy_to_user(arg, &gentry, sizeof gentry))
276 mtrr_close(struct inode *ino, struct file *file)
279 unsigned int *fcount = FILE_FCOUNT(file);
281 if (fcount != NULL) {
282 max = num_var_ranges;
283 for (i = 0; i < max; ++i) {
284 while (fcount[i] > 0) {
290 FILE_FCOUNT(file) = NULL;
292 return single_release(ino, file);
295 static int mtrr_seq_show(struct seq_file *seq, void *offset);
297 static int mtrr_open(struct inode *inode, struct file *file)
303 return single_open(file, mtrr_seq_show, NULL);
306 static struct file_operations mtrr_fops = {
307 .owner = THIS_MODULE,
313 .release = mtrr_close,
317 static struct proc_dir_entry *proc_root_mtrr;
320 static int mtrr_seq_show(struct seq_file *seq, void *offset)
329 max = num_var_ranges;
330 for (i = 0; i < max; i++) {
331 mtrr_if->get(i, &base, &size, &type);
335 if (size < (0x100000 >> PAGE_SHIFT)) {
338 size <<= PAGE_SHIFT - 10;
341 size >>= 20 - PAGE_SHIFT;
343 /* RED-PEN: base can be > 32bit */
344 len += seq_printf(seq,
345 "reg%02i: base=0x%05lx000 (%4liMB), size=%4i%cB: %s, count=%d\n",
346 i, base, base >> (20 - PAGE_SHIFT), size, factor,
347 mtrr_attrib_to_str(type), usage_table[i]);
353 static int __init mtrr_if_init(void)
355 struct cpuinfo_x86 *c = &boot_cpu_data;
357 if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
358 (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
359 (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
360 (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
364 create_proc_entry("mtrr", S_IWUSR | S_IRUGO, &proc_root);
365 if (proc_root_mtrr) {
366 proc_root_mtrr->owner = THIS_MODULE;
367 proc_root_mtrr->proc_fops = &mtrr_fops;
372 arch_initcall(mtrr_if_init);
373 #endif /* CONFIG_PROC_FS */