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/string.h>
8 #include <linux/init.h>
16 #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
18 static const char *const mtrr_strings
[MTRR_NUM_TYPES
] =
21 "write-combining", /* 1 */
24 "write-through", /* 4 */
25 "write-protect", /* 5 */
29 const char *mtrr_attrib_to_str(int x
)
31 return (x
<= 6) ? mtrr_strings
[x
] : "?";
37 mtrr_file_add(unsigned long base
, unsigned long size
,
38 unsigned int type
, bool increment
, struct file
*file
, int page
)
40 unsigned int *fcount
= FILE_FCOUNT(file
);
45 fcount
= kzalloc(max
* sizeof *fcount
, GFP_KERNEL
);
48 FILE_FCOUNT(file
) = fcount
;
51 if ((base
& (PAGE_SIZE
- 1)) || (size
& (PAGE_SIZE
- 1)))
56 reg
= mtrr_add_page(base
, size
, type
, true);
63 mtrr_file_del(unsigned long base
, unsigned long size
,
64 struct file
*file
, int page
)
66 unsigned int *fcount
= FILE_FCOUNT(file
);
70 if ((base
& (PAGE_SIZE
- 1)) || (size
& (PAGE_SIZE
- 1)))
75 reg
= mtrr_del_page(-1, base
, size
);
87 * seq_file can seek but we ignore it.
89 * Format of control line:
90 * "base=%Lx size=%Lx type=%s" or "disable=%d"
93 mtrr_write(struct file
*file
, const char __user
*buf
, size_t len
, loff_t
* ppos
)
97 unsigned long long base
, size
;
103 if (!capable(CAP_SYS_ADMIN
))
106 memset(line
, 0, LINE_SIZE
);
111 if (length
> LINE_SIZE
- 1)
112 length
= LINE_SIZE
- 1;
117 if (copy_from_user(line
, buf
, length
))
120 linelen
= strlen(line
);
121 ptr
= line
+ linelen
- 1;
122 if (linelen
&& *ptr
== '\n')
125 if (!strncmp(line
, "disable=", 8)) {
126 reg
= simple_strtoul(line
+ 8, &ptr
, 0);
127 err
= mtrr_del_page(reg
, 0, 0);
133 if (strncmp(line
, "base=", 5))
136 base
= simple_strtoull(line
+ 5, &ptr
, 0);
137 ptr
= skip_spaces(ptr
);
139 if (strncmp(ptr
, "size=", 5))
142 size
= simple_strtoull(ptr
+ 5, &ptr
, 0);
143 if ((base
& 0xfff) || (size
& 0xfff))
145 ptr
= skip_spaces(ptr
);
147 if (strncmp(ptr
, "type=", 5))
149 ptr
= skip_spaces(ptr
+ 5);
151 for (i
= 0; i
< MTRR_NUM_TYPES
; ++i
) {
152 if (strcmp(ptr
, mtrr_strings
[i
]))
156 err
= mtrr_add_page((unsigned long)base
, (unsigned long)size
, i
, true);
165 mtrr_ioctl(struct file
*file
, unsigned int cmd
, unsigned long __arg
)
170 struct mtrr_sentry sentry
;
171 struct mtrr_gentry gentry
;
172 void __user
*arg
= (void __user
*) __arg
;
175 case MTRRIOC_ADD_ENTRY
:
176 case MTRRIOC_SET_ENTRY
:
177 case MTRRIOC_DEL_ENTRY
:
178 case MTRRIOC_KILL_ENTRY
:
179 case MTRRIOC_ADD_PAGE_ENTRY
:
180 case MTRRIOC_SET_PAGE_ENTRY
:
181 case MTRRIOC_DEL_PAGE_ENTRY
:
182 case MTRRIOC_KILL_PAGE_ENTRY
:
183 if (copy_from_user(&sentry
, arg
, sizeof sentry
))
186 case MTRRIOC_GET_ENTRY
:
187 case MTRRIOC_GET_PAGE_ENTRY
:
188 if (copy_from_user(&gentry
, arg
, sizeof gentry
))
192 case MTRRIOC32_ADD_ENTRY
:
193 case MTRRIOC32_SET_ENTRY
:
194 case MTRRIOC32_DEL_ENTRY
:
195 case MTRRIOC32_KILL_ENTRY
:
196 case MTRRIOC32_ADD_PAGE_ENTRY
:
197 case MTRRIOC32_SET_PAGE_ENTRY
:
198 case MTRRIOC32_DEL_PAGE_ENTRY
:
199 case MTRRIOC32_KILL_PAGE_ENTRY
: {
200 struct mtrr_sentry32 __user
*s32
;
202 s32
= (struct mtrr_sentry32 __user
*)__arg
;
203 err
= get_user(sentry
.base
, &s32
->base
);
204 err
|= get_user(sentry
.size
, &s32
->size
);
205 err
|= get_user(sentry
.type
, &s32
->type
);
210 case MTRRIOC32_GET_ENTRY
:
211 case MTRRIOC32_GET_PAGE_ENTRY
: {
212 struct mtrr_gentry32 __user
*g32
;
214 g32
= (struct mtrr_gentry32 __user
*)__arg
;
215 err
= get_user(gentry
.regnum
, &g32
->regnum
);
216 err
|= get_user(gentry
.base
, &g32
->base
);
217 err
|= get_user(gentry
.size
, &g32
->size
);
218 err
|= get_user(gentry
.type
, &g32
->type
);
229 case MTRRIOC_ADD_ENTRY
:
231 case MTRRIOC32_ADD_ENTRY
:
233 if (!capable(CAP_SYS_ADMIN
))
236 mtrr_file_add(sentry
.base
, sentry
.size
, sentry
.type
, true,
239 case MTRRIOC_SET_ENTRY
:
241 case MTRRIOC32_SET_ENTRY
:
243 if (!capable(CAP_SYS_ADMIN
))
245 err
= mtrr_add(sentry
.base
, sentry
.size
, sentry
.type
, false);
247 case MTRRIOC_DEL_ENTRY
:
249 case MTRRIOC32_DEL_ENTRY
:
251 if (!capable(CAP_SYS_ADMIN
))
253 err
= mtrr_file_del(sentry
.base
, sentry
.size
, file
, 0);
255 case MTRRIOC_KILL_ENTRY
:
257 case MTRRIOC32_KILL_ENTRY
:
259 if (!capable(CAP_SYS_ADMIN
))
261 err
= mtrr_del(-1, sentry
.base
, sentry
.size
);
263 case MTRRIOC_GET_ENTRY
:
265 case MTRRIOC32_GET_ENTRY
:
267 if (gentry
.regnum
>= num_var_ranges
)
269 mtrr_if
->get(gentry
.regnum
, &gentry
.base
, &size
, &type
);
271 /* Hide entries that go above 4GB */
272 if (gentry
.base
+ size
- 1 >= (1UL << (8 * sizeof(gentry
.size
) - PAGE_SHIFT
))
273 || size
>= (1UL << (8 * sizeof(gentry
.size
) - PAGE_SHIFT
)))
274 gentry
.base
= gentry
.size
= gentry
.type
= 0;
276 gentry
.base
<<= PAGE_SHIFT
;
277 gentry
.size
= size
<< PAGE_SHIFT
;
282 case MTRRIOC_ADD_PAGE_ENTRY
:
284 case MTRRIOC32_ADD_PAGE_ENTRY
:
286 if (!capable(CAP_SYS_ADMIN
))
289 mtrr_file_add(sentry
.base
, sentry
.size
, sentry
.type
, true,
292 case MTRRIOC_SET_PAGE_ENTRY
:
294 case MTRRIOC32_SET_PAGE_ENTRY
:
296 if (!capable(CAP_SYS_ADMIN
))
299 mtrr_add_page(sentry
.base
, sentry
.size
, sentry
.type
, false);
301 case MTRRIOC_DEL_PAGE_ENTRY
:
303 case MTRRIOC32_DEL_PAGE_ENTRY
:
305 if (!capable(CAP_SYS_ADMIN
))
307 err
= mtrr_file_del(sentry
.base
, sentry
.size
, file
, 1);
309 case MTRRIOC_KILL_PAGE_ENTRY
:
311 case MTRRIOC32_KILL_PAGE_ENTRY
:
313 if (!capable(CAP_SYS_ADMIN
))
315 err
= mtrr_del_page(-1, sentry
.base
, sentry
.size
);
317 case MTRRIOC_GET_PAGE_ENTRY
:
319 case MTRRIOC32_GET_PAGE_ENTRY
:
321 if (gentry
.regnum
>= num_var_ranges
)
323 mtrr_if
->get(gentry
.regnum
, &gentry
.base
, &size
, &type
);
324 /* Hide entries that would overflow */
325 if (size
!= (__typeof__(gentry
.size
))size
)
326 gentry
.base
= gentry
.size
= gentry
.type
= 0;
338 case MTRRIOC_GET_ENTRY
:
339 case MTRRIOC_GET_PAGE_ENTRY
:
340 if (copy_to_user(arg
, &gentry
, sizeof gentry
))
344 case MTRRIOC32_GET_ENTRY
:
345 case MTRRIOC32_GET_PAGE_ENTRY
: {
346 struct mtrr_gentry32 __user
*g32
;
348 g32
= (struct mtrr_gentry32 __user
*)__arg
;
349 err
= put_user(gentry
.base
, &g32
->base
);
350 err
|= put_user(gentry
.size
, &g32
->size
);
351 err
|= put_user(gentry
.regnum
, &g32
->regnum
);
352 err
|= put_user(gentry
.type
, &g32
->type
);
360 static int mtrr_close(struct inode
*ino
, struct file
*file
)
362 unsigned int *fcount
= FILE_FCOUNT(file
);
365 if (fcount
!= NULL
) {
366 max
= num_var_ranges
;
367 for (i
= 0; i
< max
; ++i
) {
368 while (fcount
[i
] > 0) {
374 FILE_FCOUNT(file
) = NULL
;
376 return single_release(ino
, file
);
379 static int mtrr_seq_show(struct seq_file
*seq
, void *offset
);
381 static int mtrr_open(struct inode
*inode
, struct file
*file
)
387 return single_open(file
, mtrr_seq_show
, NULL
);
390 static const struct file_operations mtrr_fops
= {
391 .owner
= THIS_MODULE
,
396 .unlocked_ioctl
= mtrr_ioctl
,
397 .compat_ioctl
= mtrr_ioctl
,
398 .release
= mtrr_close
,
401 static int mtrr_seq_show(struct seq_file
*seq
, void *offset
)
406 unsigned long base
, size
;
409 max
= num_var_ranges
;
410 for (i
= 0; i
< max
; i
++) {
411 mtrr_if
->get(i
, &base
, &size
, &type
);
413 mtrr_usage_table
[i
] = 0;
416 if (size
< (0x100000 >> PAGE_SHIFT
)) {
419 size
<<= PAGE_SHIFT
- 10;
422 size
>>= 20 - PAGE_SHIFT
;
424 /* Base can be > 32bit */
425 len
+= seq_printf(seq
, "reg%02i: base=0x%06lx000 "
426 "(%5luMB), size=%5lu%cB, count=%d: %s\n",
427 i
, base
, base
>> (20 - PAGE_SHIFT
), size
,
428 factor
, mtrr_usage_table
[i
],
429 mtrr_attrib_to_str(type
));
434 static int __init
mtrr_if_init(void)
436 struct cpuinfo_x86
*c
= &boot_cpu_data
;
438 if ((!cpu_has(c
, X86_FEATURE_MTRR
)) &&
439 (!cpu_has(c
, X86_FEATURE_K6_MTRR
)) &&
440 (!cpu_has(c
, X86_FEATURE_CYRIX_ARR
)) &&
441 (!cpu_has(c
, X86_FEATURE_CENTAUR_MCR
)))
444 proc_create("mtrr", S_IWUSR
| S_IRUGO
, NULL
, &mtrr_fops
);
447 arch_initcall(mtrr_if_init
);
448 #endif /* CONFIG_PROC_FS */