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 #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
16 static const char *const mtrr_strings
[MTRR_NUM_TYPES
] =
19 "write-combining", /* 1 */
22 "write-through", /* 4 */
23 "write-protect", /* 5 */
27 const char *mtrr_attrib_to_str(int x
)
29 return (x
<= 6) ? mtrr_strings
[x
] : "?";
35 mtrr_file_add(unsigned long base
, unsigned long size
,
36 unsigned int type
, bool increment
, struct file
*file
, int page
)
39 unsigned int *fcount
= FILE_FCOUNT(file
);
43 fcount
= kzalloc(max
* sizeof *fcount
, GFP_KERNEL
);
46 FILE_FCOUNT(file
) = fcount
;
49 if ((base
& (PAGE_SIZE
- 1)) || (size
& (PAGE_SIZE
- 1)))
54 reg
= mtrr_add_page(base
, size
, type
, true);
61 mtrr_file_del(unsigned long base
, unsigned long size
,
62 struct file
*file
, int page
)
65 unsigned int *fcount
= FILE_FCOUNT(file
);
68 if ((base
& (PAGE_SIZE
- 1)) || (size
& (PAGE_SIZE
- 1)))
73 reg
= mtrr_del_page(-1, base
, size
);
84 /* RED-PEN: seq_file can seek now. this is ignored. */
86 mtrr_write(struct file
*file
, const char __user
*buf
, size_t len
, loff_t
* ppos
)
87 /* Format of control line:
88 "base=%Lx size=%Lx type=%s" OR:
94 unsigned long long base
, size
;
99 if (!capable(CAP_SYS_ADMIN
))
103 memset(line
, 0, LINE_SIZE
);
106 if (copy_from_user(line
, buf
, len
- 1))
108 linelen
= strlen(line
);
109 ptr
= line
+ linelen
- 1;
110 if (linelen
&& *ptr
== '\n')
112 if (!strncmp(line
, "disable=", 8)) {
113 reg
= simple_strtoul(line
+ 8, &ptr
, 0);
114 err
= mtrr_del_page(reg
, 0, 0);
119 if (strncmp(line
, "base=", 5))
121 base
= simple_strtoull(line
+ 5, &ptr
, 0);
122 for (; isspace(*ptr
); ++ptr
) ;
123 if (strncmp(ptr
, "size=", 5))
125 size
= simple_strtoull(ptr
+ 5, &ptr
, 0);
126 if ((base
& 0xfff) || (size
& 0xfff))
128 for (; isspace(*ptr
); ++ptr
) ;
129 if (strncmp(ptr
, "type=", 5))
132 for (; isspace(*ptr
); ++ptr
) ;
133 for (i
= 0; i
< MTRR_NUM_TYPES
; ++i
) {
134 if (strcmp(ptr
, mtrr_strings
[i
]))
139 mtrr_add_page((unsigned long) base
, (unsigned long) size
, i
,
149 mtrr_ioctl(struct file
*file
, unsigned int cmd
, unsigned long __arg
)
154 struct mtrr_sentry sentry
;
155 struct mtrr_gentry gentry
;
156 void __user
*arg
= (void __user
*) __arg
;
159 case MTRRIOC_ADD_ENTRY
:
160 case MTRRIOC_SET_ENTRY
:
161 case MTRRIOC_DEL_ENTRY
:
162 case MTRRIOC_KILL_ENTRY
:
163 case MTRRIOC_ADD_PAGE_ENTRY
:
164 case MTRRIOC_SET_PAGE_ENTRY
:
165 case MTRRIOC_DEL_PAGE_ENTRY
:
166 case MTRRIOC_KILL_PAGE_ENTRY
:
167 if (copy_from_user(&sentry
, arg
, sizeof sentry
))
170 case MTRRIOC_GET_ENTRY
:
171 case MTRRIOC_GET_PAGE_ENTRY
:
172 if (copy_from_user(&gentry
, arg
, sizeof gentry
))
176 case MTRRIOC32_ADD_ENTRY
:
177 case MTRRIOC32_SET_ENTRY
:
178 case MTRRIOC32_DEL_ENTRY
:
179 case MTRRIOC32_KILL_ENTRY
:
180 case MTRRIOC32_ADD_PAGE_ENTRY
:
181 case MTRRIOC32_SET_PAGE_ENTRY
:
182 case MTRRIOC32_DEL_PAGE_ENTRY
:
183 case MTRRIOC32_KILL_PAGE_ENTRY
: {
184 struct mtrr_sentry32 __user
*s32
= (struct mtrr_sentry32 __user
*)__arg
;
185 err
= get_user(sentry
.base
, &s32
->base
);
186 err
|= get_user(sentry
.size
, &s32
->size
);
187 err
|= get_user(sentry
.type
, &s32
->type
);
192 case MTRRIOC32_GET_ENTRY
:
193 case MTRRIOC32_GET_PAGE_ENTRY
: {
194 struct mtrr_gentry32 __user
*g32
= (struct mtrr_gentry32 __user
*)__arg
;
195 err
= get_user(gentry
.regnum
, &g32
->regnum
);
196 err
|= get_user(gentry
.base
, &g32
->base
);
197 err
|= get_user(gentry
.size
, &g32
->size
);
198 err
|= get_user(gentry
.type
, &g32
->type
);
209 case MTRRIOC_ADD_ENTRY
:
211 case MTRRIOC32_ADD_ENTRY
:
213 if (!capable(CAP_SYS_ADMIN
))
216 mtrr_file_add(sentry
.base
, sentry
.size
, sentry
.type
, true,
219 case MTRRIOC_SET_ENTRY
:
221 case MTRRIOC32_SET_ENTRY
:
223 if (!capable(CAP_SYS_ADMIN
))
225 err
= mtrr_add(sentry
.base
, sentry
.size
, sentry
.type
, false);
227 case MTRRIOC_DEL_ENTRY
:
229 case MTRRIOC32_DEL_ENTRY
:
231 if (!capable(CAP_SYS_ADMIN
))
233 err
= mtrr_file_del(sentry
.base
, sentry
.size
, file
, 0);
235 case MTRRIOC_KILL_ENTRY
:
237 case MTRRIOC32_KILL_ENTRY
:
239 if (!capable(CAP_SYS_ADMIN
))
241 err
= mtrr_del(-1, sentry
.base
, sentry
.size
);
243 case MTRRIOC_GET_ENTRY
:
245 case MTRRIOC32_GET_ENTRY
:
247 if (gentry
.regnum
>= num_var_ranges
)
249 mtrr_if
->get(gentry
.regnum
, &gentry
.base
, &size
, &type
);
251 /* Hide entries that go above 4GB */
252 if (gentry
.base
+ size
- 1 >= (1UL << (8 * sizeof(gentry
.size
) - PAGE_SHIFT
))
253 || size
>= (1UL << (8 * sizeof(gentry
.size
) - PAGE_SHIFT
)))
254 gentry
.base
= gentry
.size
= gentry
.type
= 0;
256 gentry
.base
<<= PAGE_SHIFT
;
257 gentry
.size
= size
<< PAGE_SHIFT
;
262 case MTRRIOC_ADD_PAGE_ENTRY
:
264 case MTRRIOC32_ADD_PAGE_ENTRY
:
266 if (!capable(CAP_SYS_ADMIN
))
269 mtrr_file_add(sentry
.base
, sentry
.size
, sentry
.type
, true,
272 case MTRRIOC_SET_PAGE_ENTRY
:
274 case MTRRIOC32_SET_PAGE_ENTRY
:
276 if (!capable(CAP_SYS_ADMIN
))
279 mtrr_add_page(sentry
.base
, sentry
.size
, sentry
.type
, false);
281 case MTRRIOC_DEL_PAGE_ENTRY
:
283 case MTRRIOC32_DEL_PAGE_ENTRY
:
285 if (!capable(CAP_SYS_ADMIN
))
287 err
= mtrr_file_del(sentry
.base
, sentry
.size
, file
, 1);
289 case MTRRIOC_KILL_PAGE_ENTRY
:
291 case MTRRIOC32_KILL_PAGE_ENTRY
:
293 if (!capable(CAP_SYS_ADMIN
))
295 err
= mtrr_del_page(-1, sentry
.base
, sentry
.size
);
297 case MTRRIOC_GET_PAGE_ENTRY
:
299 case MTRRIOC32_GET_PAGE_ENTRY
:
301 if (gentry
.regnum
>= num_var_ranges
)
303 mtrr_if
->get(gentry
.regnum
, &gentry
.base
, &size
, &type
);
304 /* Hide entries that would overflow */
305 if (size
!= (__typeof__(gentry
.size
))size
)
306 gentry
.base
= gentry
.size
= gentry
.type
= 0;
318 case MTRRIOC_GET_ENTRY
:
319 case MTRRIOC_GET_PAGE_ENTRY
:
320 if (copy_to_user(arg
, &gentry
, sizeof gentry
))
324 case MTRRIOC32_GET_ENTRY
:
325 case MTRRIOC32_GET_PAGE_ENTRY
: {
326 struct mtrr_gentry32 __user
*g32
= (struct mtrr_gentry32 __user
*)__arg
;
327 err
= put_user(gentry
.base
, &g32
->base
);
328 err
|= put_user(gentry
.size
, &g32
->size
);
329 err
|= put_user(gentry
.regnum
, &g32
->regnum
);
330 err
|= put_user(gentry
.type
, &g32
->type
);
339 mtrr_close(struct inode
*ino
, struct file
*file
)
342 unsigned int *fcount
= FILE_FCOUNT(file
);
344 if (fcount
!= NULL
) {
345 max
= num_var_ranges
;
346 for (i
= 0; i
< max
; ++i
) {
347 while (fcount
[i
] > 0) {
353 FILE_FCOUNT(file
) = NULL
;
355 return single_release(ino
, file
);
358 static int mtrr_seq_show(struct seq_file
*seq
, void *offset
);
360 static int mtrr_open(struct inode
*inode
, struct file
*file
)
366 return single_open(file
, mtrr_seq_show
, NULL
);
369 static const struct file_operations mtrr_fops
= {
370 .owner
= THIS_MODULE
,
375 .unlocked_ioctl
= mtrr_ioctl
,
376 .compat_ioctl
= mtrr_ioctl
,
377 .release
= mtrr_close
,
381 static struct proc_dir_entry
*proc_root_mtrr
;
384 static int mtrr_seq_show(struct seq_file
*seq
, void *offset
)
389 unsigned long base
, size
;
392 max
= num_var_ranges
;
393 for (i
= 0; i
< max
; i
++) {
394 mtrr_if
->get(i
, &base
, &size
, &type
);
396 mtrr_usage_table
[i
] = 0;
398 if (size
< (0x100000 >> PAGE_SHIFT
)) {
401 size
<<= PAGE_SHIFT
- 10;
404 size
>>= 20 - PAGE_SHIFT
;
406 /* RED-PEN: base can be > 32bit */
407 len
+= seq_printf(seq
,
408 "reg%02i: base=0x%05lx000 (%4luMB), size=%4lu%cB: %s, count=%d\n",
409 i
, base
, base
>> (20 - PAGE_SHIFT
), size
, factor
,
410 mtrr_attrib_to_str(type
), mtrr_usage_table
[i
]);
416 static int __init
mtrr_if_init(void)
418 struct cpuinfo_x86
*c
= &boot_cpu_data
;
420 if ((!cpu_has(c
, X86_FEATURE_MTRR
)) &&
421 (!cpu_has(c
, X86_FEATURE_K6_MTRR
)) &&
422 (!cpu_has(c
, X86_FEATURE_CYRIX_ARR
)) &&
423 (!cpu_has(c
, X86_FEATURE_CENTAUR_MCR
)))
427 create_proc_entry("mtrr", S_IWUSR
| S_IRUGO
, &proc_root
);
428 if (proc_root_mtrr
) {
429 proc_root_mtrr
->owner
= THIS_MODULE
;
430 proc_root_mtrr
->proc_fops
= &mtrr_fops
;
435 arch_initcall(mtrr_if_init
);
436 #endif /* CONFIG_PROC_FS */