1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/capability.h>
3 #include <linux/seq_file.h>
4 #include <linux/uaccess.h>
5 #include <linux/proc_fs.h>
6 #include <linux/ctype.h>
7 #include <linux/string.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
17 #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
19 static const char *const mtrr_strings
[MTRR_NUM_TYPES
] =
22 "write-combining", /* 1 */
25 "write-through", /* 4 */
26 "write-protect", /* 5 */
30 const 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
, bool increment
, struct file
*file
, int page
)
41 unsigned int *fcount
= FILE_FCOUNT(file
);
46 fcount
= kcalloc(max
, sizeof(*fcount
), GFP_KERNEL
);
49 FILE_FCOUNT(file
) = fcount
;
52 if ((base
& (PAGE_SIZE
- 1)) || (size
& (PAGE_SIZE
- 1)))
57 reg
= mtrr_add_page(base
, size
, type
, true);
64 mtrr_file_del(unsigned long base
, unsigned long size
,
65 struct file
*file
, int page
)
67 unsigned int *fcount
= FILE_FCOUNT(file
);
71 if ((base
& (PAGE_SIZE
- 1)) || (size
& (PAGE_SIZE
- 1)))
76 reg
= mtrr_del_page(-1, base
, size
);
88 * seq_file can seek but we ignore it.
90 * Format of control line:
91 * "base=%Lx size=%Lx type=%s" or "disable=%d"
94 mtrr_write(struct file
*file
, const char __user
*buf
, size_t len
, loff_t
* ppos
)
98 unsigned long long base
, size
;
100 char line
[LINE_SIZE
];
104 memset(line
, 0, LINE_SIZE
);
106 len
= min_t(size_t, len
, LINE_SIZE
- 1);
107 length
= strncpy_from_user(line
, buf
, len
);
111 linelen
= strlen(line
);
112 ptr
= line
+ linelen
- 1;
113 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);
124 if (strncmp(line
, "base=", 5))
127 base
= simple_strtoull(line
+ 5, &ptr
, 0);
128 ptr
= skip_spaces(ptr
);
130 if (strncmp(ptr
, "size=", 5))
133 size
= simple_strtoull(ptr
+ 5, &ptr
, 0);
134 if ((base
& 0xfff) || (size
& 0xfff))
136 ptr
= skip_spaces(ptr
);
138 if (strncmp(ptr
, "type=", 5))
140 ptr
= skip_spaces(ptr
+ 5);
142 i
= match_string(mtrr_strings
, MTRR_NUM_TYPES
, ptr
);
148 err
= mtrr_add_page((unsigned long)base
, (unsigned long)size
, i
, true);
155 mtrr_ioctl(struct file
*file
, unsigned int cmd
, unsigned long __arg
)
161 struct mtrr_sentry sentry
;
162 struct mtrr_gentry gentry
;
163 void __user
*arg
= (void __user
*) __arg
;
165 memset(&gentry
, 0, sizeof(gentry
));
168 case MTRRIOC_ADD_ENTRY
:
169 case MTRRIOC_SET_ENTRY
:
170 case MTRRIOC_DEL_ENTRY
:
171 case MTRRIOC_KILL_ENTRY
:
172 case MTRRIOC_ADD_PAGE_ENTRY
:
173 case MTRRIOC_SET_PAGE_ENTRY
:
174 case MTRRIOC_DEL_PAGE_ENTRY
:
175 case MTRRIOC_KILL_PAGE_ENTRY
:
176 if (copy_from_user(&sentry
, arg
, sizeof(sentry
)))
179 case MTRRIOC_GET_ENTRY
:
180 case MTRRIOC_GET_PAGE_ENTRY
:
181 if (copy_from_user(&gentry
, arg
, sizeof(gentry
)))
185 case MTRRIOC32_ADD_ENTRY
:
186 case MTRRIOC32_SET_ENTRY
:
187 case MTRRIOC32_DEL_ENTRY
:
188 case MTRRIOC32_KILL_ENTRY
:
189 case MTRRIOC32_ADD_PAGE_ENTRY
:
190 case MTRRIOC32_SET_PAGE_ENTRY
:
191 case MTRRIOC32_DEL_PAGE_ENTRY
:
192 case MTRRIOC32_KILL_PAGE_ENTRY
: {
193 struct mtrr_sentry32 __user
*s32
;
195 s32
= (struct mtrr_sentry32 __user
*)__arg
;
196 err
= get_user(sentry
.base
, &s32
->base
);
197 err
|= get_user(sentry
.size
, &s32
->size
);
198 err
|= get_user(sentry
.type
, &s32
->type
);
203 case MTRRIOC32_GET_ENTRY
:
204 case MTRRIOC32_GET_PAGE_ENTRY
: {
205 struct mtrr_gentry32 __user
*g32
;
207 g32
= (struct mtrr_gentry32 __user
*)__arg
;
208 err
= get_user(gentry
.regnum
, &g32
->regnum
);
209 err
|= get_user(gentry
.base
, &g32
->base
);
210 err
|= get_user(gentry
.size
, &g32
->size
);
211 err
|= get_user(gentry
.type
, &g32
->type
);
222 case MTRRIOC_ADD_ENTRY
:
224 case MTRRIOC32_ADD_ENTRY
:
227 mtrr_file_add(sentry
.base
, sentry
.size
, sentry
.type
, true,
230 case MTRRIOC_SET_ENTRY
:
232 case MTRRIOC32_SET_ENTRY
:
234 err
= mtrr_add(sentry
.base
, sentry
.size
, sentry
.type
, false);
236 case MTRRIOC_DEL_ENTRY
:
238 case MTRRIOC32_DEL_ENTRY
:
240 err
= mtrr_file_del(sentry
.base
, sentry
.size
, file
, 0);
242 case MTRRIOC_KILL_ENTRY
:
244 case MTRRIOC32_KILL_ENTRY
:
246 err
= mtrr_del(-1, sentry
.base
, sentry
.size
);
248 case MTRRIOC_GET_ENTRY
:
250 case MTRRIOC32_GET_ENTRY
:
252 if (gentry
.regnum
>= num_var_ranges
)
254 mtrr_if
->get(gentry
.regnum
, &base
, &size
, &type
);
256 /* Hide entries that go above 4GB */
257 if (base
+ size
- 1 >= (1UL << (8 * sizeof(gentry
.size
) - PAGE_SHIFT
))
258 || size
>= (1UL << (8 * sizeof(gentry
.size
) - PAGE_SHIFT
)))
259 gentry
.base
= gentry
.size
= gentry
.type
= 0;
261 gentry
.base
= base
<< PAGE_SHIFT
;
262 gentry
.size
= size
<< PAGE_SHIFT
;
267 case MTRRIOC_ADD_PAGE_ENTRY
:
269 case MTRRIOC32_ADD_PAGE_ENTRY
:
272 mtrr_file_add(sentry
.base
, sentry
.size
, sentry
.type
, true,
275 case MTRRIOC_SET_PAGE_ENTRY
:
277 case MTRRIOC32_SET_PAGE_ENTRY
:
280 mtrr_add_page(sentry
.base
, sentry
.size
, sentry
.type
, false);
282 case MTRRIOC_DEL_PAGE_ENTRY
:
284 case MTRRIOC32_DEL_PAGE_ENTRY
:
286 err
= mtrr_file_del(sentry
.base
, sentry
.size
, file
, 1);
288 case MTRRIOC_KILL_PAGE_ENTRY
:
290 case MTRRIOC32_KILL_PAGE_ENTRY
:
292 err
= mtrr_del_page(-1, sentry
.base
, sentry
.size
);
294 case MTRRIOC_GET_PAGE_ENTRY
:
296 case MTRRIOC32_GET_PAGE_ENTRY
:
298 if (gentry
.regnum
>= num_var_ranges
)
300 mtrr_if
->get(gentry
.regnum
, &base
, &size
, &type
);
301 /* Hide entries that would overflow */
302 if (size
!= (__typeof__(gentry
.size
))size
)
303 gentry
.base
= gentry
.size
= gentry
.type
= 0;
316 case MTRRIOC_GET_ENTRY
:
317 case MTRRIOC_GET_PAGE_ENTRY
:
318 if (copy_to_user(arg
, &gentry
, sizeof(gentry
)))
322 case MTRRIOC32_GET_ENTRY
:
323 case MTRRIOC32_GET_PAGE_ENTRY
: {
324 struct mtrr_gentry32 __user
*g32
;
326 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
);
338 static int mtrr_close(struct inode
*ino
, struct file
*file
)
340 unsigned int *fcount
= FILE_FCOUNT(file
);
343 if (fcount
!= NULL
) {
344 max
= num_var_ranges
;
345 for (i
= 0; i
< max
; ++i
) {
346 while (fcount
[i
] > 0) {
352 FILE_FCOUNT(file
) = NULL
;
354 return single_release(ino
, file
);
357 static int mtrr_seq_show(struct seq_file
*seq
, void *offset
)
362 unsigned long base
, size
;
364 max
= num_var_ranges
;
365 for (i
= 0; i
< max
; i
++) {
366 mtrr_if
->get(i
, &base
, &size
, &type
);
368 mtrr_usage_table
[i
] = 0;
371 if (size
< (0x100000 >> PAGE_SHIFT
)) {
374 size
<<= PAGE_SHIFT
- 10;
377 size
>>= 20 - PAGE_SHIFT
;
379 /* Base can be > 32bit */
380 seq_printf(seq
, "reg%02i: base=0x%06lx000 (%5luMB), size=%5lu%cB, count=%d: %s\n",
381 i
, base
, base
>> (20 - PAGE_SHIFT
),
383 mtrr_usage_table
[i
], mtrr_attrib_to_str(type
));
388 static int mtrr_open(struct inode
*inode
, struct file
*file
)
394 if (!capable(CAP_SYS_ADMIN
))
396 return single_open(file
, mtrr_seq_show
, NULL
);
399 static const struct proc_ops mtrr_proc_ops
= {
400 .proc_open
= mtrr_open
,
401 .proc_read
= seq_read
,
402 .proc_lseek
= seq_lseek
,
403 .proc_write
= mtrr_write
,
404 .proc_ioctl
= mtrr_ioctl
,
406 .proc_compat_ioctl
= mtrr_ioctl
,
408 .proc_release
= mtrr_close
,
411 static int __init
mtrr_if_init(void)
413 struct cpuinfo_x86
*c
= &boot_cpu_data
;
415 if ((!cpu_has(c
, X86_FEATURE_MTRR
)) &&
416 (!cpu_has(c
, X86_FEATURE_K6_MTRR
)) &&
417 (!cpu_has(c
, X86_FEATURE_CYRIX_ARR
)) &&
418 (!cpu_has(c
, X86_FEATURE_CENTAUR_MCR
)))
421 proc_create("mtrr", S_IWUSR
| S_IRUGO
, NULL
, &mtrr_proc_ops
);
424 arch_initcall(mtrr_if_init
);
425 #endif /* CONFIG_PROC_FS */