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];
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)
157 struct mtrr_sentry sentry;
158 struct mtrr_gentry gentry;
159 void __user *arg = (void __user *) __arg;
162 case MTRRIOC_ADD_ENTRY:
163 case MTRRIOC_SET_ENTRY:
164 case MTRRIOC_DEL_ENTRY:
165 case MTRRIOC_KILL_ENTRY:
166 case MTRRIOC_ADD_PAGE_ENTRY:
167 case MTRRIOC_SET_PAGE_ENTRY:
168 case MTRRIOC_DEL_PAGE_ENTRY:
169 case MTRRIOC_KILL_PAGE_ENTRY:
170 if (copy_from_user(&sentry, arg, sizeof sentry))
173 case MTRRIOC_GET_ENTRY:
174 case MTRRIOC_GET_PAGE_ENTRY:
175 if (copy_from_user(&gentry, arg, sizeof gentry))
179 case MTRRIOC32_ADD_ENTRY:
180 case MTRRIOC32_SET_ENTRY:
181 case MTRRIOC32_DEL_ENTRY:
182 case MTRRIOC32_KILL_ENTRY:
183 case MTRRIOC32_ADD_PAGE_ENTRY:
184 case MTRRIOC32_SET_PAGE_ENTRY:
185 case MTRRIOC32_DEL_PAGE_ENTRY:
186 case MTRRIOC32_KILL_PAGE_ENTRY: {
187 struct mtrr_sentry32 __user *s32 = (struct mtrr_sentry32 __user *)__arg;
188 err = get_user(sentry.base, &s32->base);
189 err |= get_user(sentry.size, &s32->size);
190 err |= get_user(sentry.type, &s32->type);
195 case MTRRIOC32_GET_ENTRY:
196 case MTRRIOC32_GET_PAGE_ENTRY: {
197 struct mtrr_gentry32 __user *g32 = (struct mtrr_gentry32 __user *)__arg;
198 err = get_user(gentry.regnum, &g32->regnum);
199 err |= get_user(gentry.base, &g32->base);
200 err |= get_user(gentry.size, &g32->size);
201 err |= get_user(gentry.type, &g32->type);
212 case MTRRIOC_ADD_ENTRY:
213 if (!capable(CAP_SYS_ADMIN))
216 mtrr_file_add(sentry.base, sentry.size, sentry.type, 1,
219 case MTRRIOC_SET_ENTRY:
220 if (!capable(CAP_SYS_ADMIN))
222 err = mtrr_add(sentry.base, sentry.size, sentry.type, 0);
224 case MTRRIOC_DEL_ENTRY:
225 if (!capable(CAP_SYS_ADMIN))
227 err = mtrr_file_del(sentry.base, sentry.size, file, 0);
229 case MTRRIOC_KILL_ENTRY:
230 if (!capable(CAP_SYS_ADMIN))
232 err = mtrr_del(-1, sentry.base, sentry.size);
234 case MTRRIOC_GET_ENTRY:
235 if (gentry.regnum >= num_var_ranges)
237 mtrr_if->get(gentry.regnum, &gentry.base, &gentry.size, &type);
239 /* Hide entries that go above 4GB */
240 if (gentry.base + gentry.size > 0x100000
241 || gentry.size == 0x100000)
242 gentry.base = gentry.size = gentry.type = 0;
244 gentry.base <<= PAGE_SHIFT;
245 gentry.size <<= PAGE_SHIFT;
250 case MTRRIOC_ADD_PAGE_ENTRY:
251 if (!capable(CAP_SYS_ADMIN))
254 mtrr_file_add(sentry.base, sentry.size, sentry.type, 1,
257 case MTRRIOC_SET_PAGE_ENTRY:
258 if (!capable(CAP_SYS_ADMIN))
260 err = mtrr_add_page(sentry.base, sentry.size, sentry.type, 0);
262 case MTRRIOC_DEL_PAGE_ENTRY:
263 if (!capable(CAP_SYS_ADMIN))
265 err = mtrr_file_del(sentry.base, sentry.size, file, 1);
267 case MTRRIOC_KILL_PAGE_ENTRY:
268 if (!capable(CAP_SYS_ADMIN))
270 err = mtrr_del_page(-1, sentry.base, sentry.size);
272 case MTRRIOC_GET_PAGE_ENTRY:
273 if (gentry.regnum >= num_var_ranges)
275 mtrr_if->get(gentry.regnum, &gentry.base, &gentry.size, &type);
284 case MTRRIOC_GET_ENTRY:
285 case MTRRIOC_GET_PAGE_ENTRY:
286 if (copy_to_user(arg, &gentry, sizeof gentry))
290 case MTRRIOC32_GET_ENTRY:
291 case MTRRIOC32_GET_PAGE_ENTRY: {
292 struct mtrr_gentry32 __user *g32 = (struct mtrr_gentry32 __user *)__arg;
293 err = put_user(gentry.base, &g32->base);
294 err |= put_user(gentry.size, &g32->size);
295 err |= put_user(gentry.regnum, &g32->regnum);
296 err |= put_user(gentry.type, &g32->type);
305 mtrr_close(struct inode *ino, struct file *file)
308 unsigned int *fcount = FILE_FCOUNT(file);
310 if (fcount != NULL) {
311 max = num_var_ranges;
312 for (i = 0; i < max; ++i) {
313 while (fcount[i] > 0) {
319 FILE_FCOUNT(file) = NULL;
321 return single_release(ino, file);
324 static int mtrr_seq_show(struct seq_file *seq, void *offset);
326 static int mtrr_open(struct inode *inode, struct file *file)
332 return single_open(file, mtrr_seq_show, NULL);
335 static struct file_operations mtrr_fops = {
336 .owner = THIS_MODULE,
341 .unlocked_ioctl = mtrr_ioctl,
342 .compat_ioctl = mtrr_ioctl,
343 .release = mtrr_close,
347 static struct proc_dir_entry *proc_root_mtrr;
350 static int mtrr_seq_show(struct seq_file *seq, void *offset)
359 max = num_var_ranges;
360 for (i = 0; i < max; i++) {
361 mtrr_if->get(i, &base, &size, &type);
365 if (size < (0x100000 >> PAGE_SHIFT)) {
368 size <<= PAGE_SHIFT - 10;
371 size >>= 20 - PAGE_SHIFT;
373 /* RED-PEN: base can be > 32bit */
374 len += seq_printf(seq,
375 "reg%02i: base=0x%05lx000 (%4liMB), size=%4i%cB: %s, count=%d\n",
376 i, base, base >> (20 - PAGE_SHIFT), size, factor,
377 mtrr_attrib_to_str(type), usage_table[i]);
383 static int __init mtrr_if_init(void)
385 struct cpuinfo_x86 *c = &boot_cpu_data;
387 if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
388 (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
389 (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
390 (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
394 create_proc_entry("mtrr", S_IWUSR | S_IRUGO, &proc_root);
395 if (proc_root_mtrr) {
396 proc_root_mtrr->owner = THIS_MODULE;
397 proc_root_mtrr->proc_fops = &mtrr_fops;
402 arch_initcall(mtrr_if_init);
403 #endif /* CONFIG_PROC_FS */