1 #include <linux/capability.h>
2 #include <linux/seq_file.h>
3 #include <linux/uaccess.h>
4 #include <linux/proc_fs.h>
5 #include <linux/module.h>
6 #include <linux/ctype.h>
7 #include <linux/init.h>
15 #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
17 static const char *const mtrr_strings
[MTRR_NUM_TYPES
] =
20 "write-combining", /* 1 */
23 "write-through", /* 4 */
24 "write-protect", /* 5 */
28 const char *mtrr_attrib_to_str(int x
)
30 return (x
<= 6) ? mtrr_strings
[x
] : "?";
36 mtrr_file_add(unsigned long base
, unsigned long size
,
37 unsigned int type
, bool increment
, struct file
*file
, int page
)
39 unsigned int *fcount
= FILE_FCOUNT(file
);
44 fcount
= kzalloc(max
* sizeof *fcount
, GFP_KERNEL
);
47 FILE_FCOUNT(file
) = fcount
;
50 if ((base
& (PAGE_SIZE
- 1)) || (size
& (PAGE_SIZE
- 1)))
55 reg
= mtrr_add_page(base
, size
, type
, true);
62 mtrr_file_del(unsigned long base
, unsigned long size
,
63 struct file
*file
, int page
)
65 unsigned int *fcount
= FILE_FCOUNT(file
);
69 if ((base
& (PAGE_SIZE
- 1)) || (size
& (PAGE_SIZE
- 1)))
74 reg
= mtrr_del_page(-1, base
, size
);
86 * seq_file can seek but we ignore it.
88 * Format of control line:
89 * "base=%Lx size=%Lx type=%s" or "disable=%d"
92 mtrr_write(struct file
*file
, const char __user
*buf
, size_t len
, loff_t
* ppos
)
96 unsigned long long base
, size
;
102 if (!capable(CAP_SYS_ADMIN
))
105 memset(line
, 0, LINE_SIZE
);
110 if (length
> LINE_SIZE
- 1)
111 length
= LINE_SIZE
- 1;
116 if (copy_from_user(line
, buf
, length
))
119 linelen
= strlen(line
);
120 ptr
= line
+ linelen
- 1;
121 if (linelen
&& *ptr
== '\n')
124 if (!strncmp(line
, "disable=", 8)) {
125 reg
= simple_strtoul(line
+ 8, &ptr
, 0);
126 err
= mtrr_del_page(reg
, 0, 0);
132 if (strncmp(line
, "base=", 5))
135 base
= simple_strtoull(line
+ 5, &ptr
, 0);
136 while (isspace(*ptr
))
139 if (strncmp(ptr
, "size=", 5))
142 size
= simple_strtoull(ptr
+ 5, &ptr
, 0);
143 if ((base
& 0xfff) || (size
& 0xfff))
145 while (isspace(*ptr
))
148 if (strncmp(ptr
, "type=", 5))
151 while (isspace(*ptr
))
154 for (i
= 0; i
< MTRR_NUM_TYPES
; ++i
) {
155 if (strcmp(ptr
, mtrr_strings
[i
]))
159 err
= mtrr_add_page((unsigned long)base
, (unsigned long)size
, i
, true);
168 mtrr_ioctl(struct file
*file
, unsigned int cmd
, unsigned long __arg
)
173 struct mtrr_sentry sentry
;
174 struct mtrr_gentry gentry
;
175 void __user
*arg
= (void __user
*) __arg
;
178 case MTRRIOC_ADD_ENTRY
:
179 case MTRRIOC_SET_ENTRY
:
180 case MTRRIOC_DEL_ENTRY
:
181 case MTRRIOC_KILL_ENTRY
:
182 case MTRRIOC_ADD_PAGE_ENTRY
:
183 case MTRRIOC_SET_PAGE_ENTRY
:
184 case MTRRIOC_DEL_PAGE_ENTRY
:
185 case MTRRIOC_KILL_PAGE_ENTRY
:
186 if (copy_from_user(&sentry
, arg
, sizeof sentry
))
189 case MTRRIOC_GET_ENTRY
:
190 case MTRRIOC_GET_PAGE_ENTRY
:
191 if (copy_from_user(&gentry
, arg
, sizeof gentry
))
195 case MTRRIOC32_ADD_ENTRY
:
196 case MTRRIOC32_SET_ENTRY
:
197 case MTRRIOC32_DEL_ENTRY
:
198 case MTRRIOC32_KILL_ENTRY
:
199 case MTRRIOC32_ADD_PAGE_ENTRY
:
200 case MTRRIOC32_SET_PAGE_ENTRY
:
201 case MTRRIOC32_DEL_PAGE_ENTRY
:
202 case MTRRIOC32_KILL_PAGE_ENTRY
: {
203 struct mtrr_sentry32 __user
*s32
;
205 s32
= (struct mtrr_sentry32 __user
*)__arg
;
206 err
= get_user(sentry
.base
, &s32
->base
);
207 err
|= get_user(sentry
.size
, &s32
->size
);
208 err
|= get_user(sentry
.type
, &s32
->type
);
213 case MTRRIOC32_GET_ENTRY
:
214 case MTRRIOC32_GET_PAGE_ENTRY
: {
215 struct mtrr_gentry32 __user
*g32
;
217 g32
= (struct mtrr_gentry32 __user
*)__arg
;
218 err
= get_user(gentry
.regnum
, &g32
->regnum
);
219 err
|= get_user(gentry
.base
, &g32
->base
);
220 err
|= get_user(gentry
.size
, &g32
->size
);
221 err
|= get_user(gentry
.type
, &g32
->type
);
232 case MTRRIOC_ADD_ENTRY
:
234 case MTRRIOC32_ADD_ENTRY
:
236 if (!capable(CAP_SYS_ADMIN
))
239 mtrr_file_add(sentry
.base
, sentry
.size
, sentry
.type
, true,
242 case MTRRIOC_SET_ENTRY
:
244 case MTRRIOC32_SET_ENTRY
:
246 if (!capable(CAP_SYS_ADMIN
))
248 err
= mtrr_add(sentry
.base
, sentry
.size
, sentry
.type
, false);
250 case MTRRIOC_DEL_ENTRY
:
252 case MTRRIOC32_DEL_ENTRY
:
254 if (!capable(CAP_SYS_ADMIN
))
256 err
= mtrr_file_del(sentry
.base
, sentry
.size
, file
, 0);
258 case MTRRIOC_KILL_ENTRY
:
260 case MTRRIOC32_KILL_ENTRY
:
262 if (!capable(CAP_SYS_ADMIN
))
264 err
= mtrr_del(-1, sentry
.base
, sentry
.size
);
266 case MTRRIOC_GET_ENTRY
:
268 case MTRRIOC32_GET_ENTRY
:
270 if (gentry
.regnum
>= num_var_ranges
)
272 mtrr_if
->get(gentry
.regnum
, &gentry
.base
, &size
, &type
);
274 /* Hide entries that go above 4GB */
275 if (gentry
.base
+ size
- 1 >= (1UL << (8 * sizeof(gentry
.size
) - PAGE_SHIFT
))
276 || size
>= (1UL << (8 * sizeof(gentry
.size
) - PAGE_SHIFT
)))
277 gentry
.base
= gentry
.size
= gentry
.type
= 0;
279 gentry
.base
<<= PAGE_SHIFT
;
280 gentry
.size
= size
<< PAGE_SHIFT
;
285 case MTRRIOC_ADD_PAGE_ENTRY
:
287 case MTRRIOC32_ADD_PAGE_ENTRY
:
289 if (!capable(CAP_SYS_ADMIN
))
292 mtrr_file_add(sentry
.base
, sentry
.size
, sentry
.type
, true,
295 case MTRRIOC_SET_PAGE_ENTRY
:
297 case MTRRIOC32_SET_PAGE_ENTRY
:
299 if (!capable(CAP_SYS_ADMIN
))
302 mtrr_add_page(sentry
.base
, sentry
.size
, sentry
.type
, false);
304 case MTRRIOC_DEL_PAGE_ENTRY
:
306 case MTRRIOC32_DEL_PAGE_ENTRY
:
308 if (!capable(CAP_SYS_ADMIN
))
310 err
= mtrr_file_del(sentry
.base
, sentry
.size
, file
, 1);
312 case MTRRIOC_KILL_PAGE_ENTRY
:
314 case MTRRIOC32_KILL_PAGE_ENTRY
:
316 if (!capable(CAP_SYS_ADMIN
))
318 err
= mtrr_del_page(-1, sentry
.base
, sentry
.size
);
320 case MTRRIOC_GET_PAGE_ENTRY
:
322 case MTRRIOC32_GET_PAGE_ENTRY
:
324 if (gentry
.regnum
>= num_var_ranges
)
326 mtrr_if
->get(gentry
.regnum
, &gentry
.base
, &size
, &type
);
327 /* Hide entries that would overflow */
328 if (size
!= (__typeof__(gentry
.size
))size
)
329 gentry
.base
= gentry
.size
= gentry
.type
= 0;
341 case MTRRIOC_GET_ENTRY
:
342 case MTRRIOC_GET_PAGE_ENTRY
:
343 if (copy_to_user(arg
, &gentry
, sizeof gentry
))
347 case MTRRIOC32_GET_ENTRY
:
348 case MTRRIOC32_GET_PAGE_ENTRY
: {
349 struct mtrr_gentry32 __user
*g32
;
351 g32
= (struct mtrr_gentry32 __user
*)__arg
;
352 err
= put_user(gentry
.base
, &g32
->base
);
353 err
|= put_user(gentry
.size
, &g32
->size
);
354 err
|= put_user(gentry
.regnum
, &g32
->regnum
);
355 err
|= put_user(gentry
.type
, &g32
->type
);
363 static int mtrr_close(struct inode
*ino
, struct file
*file
)
365 unsigned int *fcount
= FILE_FCOUNT(file
);
368 if (fcount
!= NULL
) {
369 max
= num_var_ranges
;
370 for (i
= 0; i
< max
; ++i
) {
371 while (fcount
[i
] > 0) {
377 FILE_FCOUNT(file
) = NULL
;
379 return single_release(ino
, file
);
382 static int mtrr_seq_show(struct seq_file
*seq
, void *offset
);
384 static int mtrr_open(struct inode
*inode
, struct file
*file
)
390 return single_open(file
, mtrr_seq_show
, NULL
);
393 static const struct file_operations mtrr_fops
= {
394 .owner
= THIS_MODULE
,
399 .unlocked_ioctl
= mtrr_ioctl
,
400 .compat_ioctl
= mtrr_ioctl
,
401 .release
= mtrr_close
,
404 static int mtrr_seq_show(struct seq_file
*seq
, void *offset
)
409 unsigned long base
, size
;
412 max
= num_var_ranges
;
413 for (i
= 0; i
< max
; i
++) {
414 mtrr_if
->get(i
, &base
, &size
, &type
);
416 mtrr_usage_table
[i
] = 0;
419 if (size
< (0x100000 >> PAGE_SHIFT
)) {
422 size
<<= PAGE_SHIFT
- 10;
425 size
>>= 20 - PAGE_SHIFT
;
427 /* Base can be > 32bit */
428 len
+= seq_printf(seq
, "reg%02i: base=0x%06lx000 "
429 "(%5luMB), size=%5lu%cB, count=%d: %s\n",
430 i
, base
, base
>> (20 - PAGE_SHIFT
), size
,
431 factor
, mtrr_usage_table
[i
],
432 mtrr_attrib_to_str(type
));
437 static int __init
mtrr_if_init(void)
439 struct cpuinfo_x86
*c
= &boot_cpu_data
;
441 if ((!cpu_has(c
, X86_FEATURE_MTRR
)) &&
442 (!cpu_has(c
, X86_FEATURE_K6_MTRR
)) &&
443 (!cpu_has(c
, X86_FEATURE_CYRIX_ARR
)) &&
444 (!cpu_has(c
, X86_FEATURE_CENTAUR_MCR
)))
447 proc_create("mtrr", S_IWUSR
| S_IRUGO
, NULL
, &mtrr_fops
);
450 arch_initcall(mtrr_if_init
);
451 #endif /* CONFIG_PROC_FS */