File Index
Files and directories
Sign in
FILE

if.c

File preview with syntax highlighting.

linux-6.19.14/arch/x86/kernel/cpu/mtrr/if.c C
  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>
 10
 11#define LINE_SIZE 80
 12
 13#include <asm/mtrr.h>
 14
 15#include "mtrr.h"
 16
 17#define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
 18
 19static const char *const mtrr_strings[MTRR_NUM_TYPES] =
 20{
 21	"uncachable",		/* 0 */
 22	"write-combining",	/* 1 */
 23	"?",			/* 2 */
 24	"?",			/* 3 */
 25	"write-through",	/* 4 */
 26	"write-protect",	/* 5 */
 27	"write-back",		/* 6 */
 28};
 29
 30const char *mtrr_attrib_to_str(int x)
 31{
 32	return (x <= 6) ? mtrr_strings[x] : "?";
 33}
 34
 35#ifdef CONFIG_PROC_FS
 36
 37static int
 38mtrr_file_add(unsigned long base, unsigned long size,
 39	      unsigned int type, bool increment, struct file *file, int page)
 40{
 41	unsigned int *fcount = FILE_FCOUNT(file);
 42	int reg, max;
 43
 44	max = num_var_ranges;
 45	if (fcount == NULL) {
 46		fcount = kcalloc(max, sizeof(*fcount), GFP_KERNEL);
 47		if (!fcount)
 48			return -ENOMEM;
 49		FILE_FCOUNT(file) = fcount;
 50	}
 51	if (!page) {
 52		if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
 53			return -EINVAL;
 54		base >>= PAGE_SHIFT;
 55		size >>= PAGE_SHIFT;
 56	}
 57	reg = mtrr_add_page(base, size, type, true);
 58	if (reg >= 0)
 59		++fcount[reg];
 60	return reg;
 61}
 62
 63static int
 64mtrr_file_del(unsigned long base, unsigned long size,
 65	      struct file *file, int page)
 66{
 67	unsigned int *fcount = FILE_FCOUNT(file);
 68	int reg;
 69
 70	if (!page) {
 71		if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
 72			return -EINVAL;
 73		base >>= PAGE_SHIFT;
 74		size >>= PAGE_SHIFT;
 75	}
 76	reg = mtrr_del_page(-1, base, size);
 77	if (reg < 0)
 78		return reg;
 79	if (fcount == NULL)
 80		return reg;
 81	if (fcount[reg] < 1)
 82		return -EINVAL;
 83	--fcount[reg];
 84	return reg;
 85}
 86
 87/*
 88 * seq_file can seek but we ignore it.
 89 *
 90 * Format of control line:
 91 *    "base=%Lx size=%Lx type=%s" or "disable=%d"
 92 */
 93static ssize_t
 94mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
 95{
 96	int i, err;
 97	unsigned long reg;
 98	unsigned long long base, size;
 99	char *ptr;
100	char line[LINE_SIZE];
101	int length;
102
103	memset(line, 0, LINE_SIZE);
104
105	len = min_t(size_t, len, LINE_SIZE - 1);
106	length = strncpy_from_user(line, buf, len);
107	if (length < 0)
108		return length;
109
110	ptr = line + length - 1;
111	if (length && *ptr == '\n')
112		*ptr = '\0';
113
114	if (!strncmp(line, "disable=", 8)) {
115		reg = simple_strtoul(line + 8, &ptr, 0);
116		err = mtrr_del_page(reg, 0, 0);
117		if (err < 0)
118			return err;
119		return len;
120	}
121
122	if (strncmp(line, "base=", 5))
123		return -EINVAL;
124
125	base = simple_strtoull(line + 5, &ptr, 0);
126	ptr = skip_spaces(ptr);
127
128	if (strncmp(ptr, "size=", 5))
129		return -EINVAL;
130
131	size = simple_strtoull(ptr + 5, &ptr, 0);
132	if ((base & 0xfff) || (size & 0xfff))
133		return -EINVAL;
134	ptr = skip_spaces(ptr);
135
136	if (strncmp(ptr, "type=", 5))
137		return -EINVAL;
138	ptr = skip_spaces(ptr + 5);
139
140	i = match_string(mtrr_strings, MTRR_NUM_TYPES, ptr);
141	if (i < 0)
142		return i;
143
144	base >>= PAGE_SHIFT;
145	size >>= PAGE_SHIFT;
146	err = mtrr_add_page((unsigned long)base, (unsigned long)size, i, true);
147	if (err < 0)
148		return err;
149	return len;
150}
151
152static long
153mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
154{
155	int err = 0;
156	mtrr_type type;
157	unsigned long base;
158	unsigned long size;
159	struct mtrr_sentry sentry;
160	struct mtrr_gentry gentry;
161	void __user *arg = (void __user *) __arg;
162
163	memset(&gentry, 0, sizeof(gentry));
164
165	switch (cmd) {
166	case MTRRIOC_ADD_ENTRY:
167	case MTRRIOC_SET_ENTRY:
168	case MTRRIOC_DEL_ENTRY:
169	case MTRRIOC_KILL_ENTRY:
170	case MTRRIOC_ADD_PAGE_ENTRY:
171	case MTRRIOC_SET_PAGE_ENTRY:
172	case MTRRIOC_DEL_PAGE_ENTRY:
173	case MTRRIOC_KILL_PAGE_ENTRY:
174		if (copy_from_user(&sentry, arg, sizeof(sentry)))
175			return -EFAULT;
176		break;
177	case MTRRIOC_GET_ENTRY:
178	case MTRRIOC_GET_PAGE_ENTRY:
179		if (copy_from_user(&gentry, arg, sizeof(gentry)))
180			return -EFAULT;
181		break;
182#ifdef CONFIG_COMPAT
183	case MTRRIOC32_ADD_ENTRY:
184	case MTRRIOC32_SET_ENTRY:
185	case MTRRIOC32_DEL_ENTRY:
186	case MTRRIOC32_KILL_ENTRY:
187	case MTRRIOC32_ADD_PAGE_ENTRY:
188	case MTRRIOC32_SET_PAGE_ENTRY:
189	case MTRRIOC32_DEL_PAGE_ENTRY:
190	case MTRRIOC32_KILL_PAGE_ENTRY: {
191		struct mtrr_sentry32 __user *s32;
192
193		s32 = (struct mtrr_sentry32 __user *)__arg;
194		err = get_user(sentry.base, &s32->base);
195		err |= get_user(sentry.size, &s32->size);
196		err |= get_user(sentry.type, &s32->type);
197		if (err)
198			return err;
199		break;
200	}
201	case MTRRIOC32_GET_ENTRY:
202	case MTRRIOC32_GET_PAGE_ENTRY: {
203		struct mtrr_gentry32 __user *g32;
204
205		g32 = (struct mtrr_gentry32 __user *)__arg;
206		err = get_user(gentry.regnum, &g32->regnum);
207		err |= get_user(gentry.base, &g32->base);
208		err |= get_user(gentry.size, &g32->size);
209		err |= get_user(gentry.type, &g32->type);
210		if (err)
211			return err;
212		break;
213	}
214#endif
215	}
216
217	switch (cmd) {
218	default:
219		return -ENOTTY;
220	case MTRRIOC_ADD_ENTRY:
221#ifdef CONFIG_COMPAT
222	case MTRRIOC32_ADD_ENTRY:
223#endif
224		err =
225		    mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
226				  file, 0);
227		break;
228	case MTRRIOC_SET_ENTRY:
229#ifdef CONFIG_COMPAT
230	case MTRRIOC32_SET_ENTRY:
231#endif
232		err = mtrr_add(sentry.base, sentry.size, sentry.type, false);
233		break;
234	case MTRRIOC_DEL_ENTRY:
235#ifdef CONFIG_COMPAT
236	case MTRRIOC32_DEL_ENTRY:
237#endif
238		err = mtrr_file_del(sentry.base, sentry.size, file, 0);
239		break;
240	case MTRRIOC_KILL_ENTRY:
241#ifdef CONFIG_COMPAT
242	case MTRRIOC32_KILL_ENTRY:
243#endif
244		err = mtrr_del(-1, sentry.base, sentry.size);
245		break;
246	case MTRRIOC_GET_ENTRY:
247#ifdef CONFIG_COMPAT
248	case MTRRIOC32_GET_ENTRY:
249#endif
250		if (gentry.regnum >= num_var_ranges)
251			return -EINVAL;
252		mtrr_if->get(gentry.regnum, &base, &size, &type);
253
254		/* Hide entries that go above 4GB */
255		if (base + size - 1 >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT))
256		    || size >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT)))
257			gentry.base = gentry.size = gentry.type = 0;
258		else {
259			gentry.base = base << PAGE_SHIFT;
260			gentry.size = size << PAGE_SHIFT;
261			gentry.type = type;
262		}
263
264		break;
265	case MTRRIOC_ADD_PAGE_ENTRY:
266#ifdef CONFIG_COMPAT
267	case MTRRIOC32_ADD_PAGE_ENTRY:
268#endif
269		err =
270		    mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
271				  file, 1);
272		break;
273	case MTRRIOC_SET_PAGE_ENTRY:
274#ifdef CONFIG_COMPAT
275	case MTRRIOC32_SET_PAGE_ENTRY:
276#endif
277		err =
278		    mtrr_add_page(sentry.base, sentry.size, sentry.type, false);
279		break;
280	case MTRRIOC_DEL_PAGE_ENTRY:
281#ifdef CONFIG_COMPAT
282	case MTRRIOC32_DEL_PAGE_ENTRY:
283#endif
284		err = mtrr_file_del(sentry.base, sentry.size, file, 1);
285		break;
286	case MTRRIOC_KILL_PAGE_ENTRY:
287#ifdef CONFIG_COMPAT
288	case MTRRIOC32_KILL_PAGE_ENTRY:
289#endif
290		err = mtrr_del_page(-1, sentry.base, sentry.size);
291		break;
292	case MTRRIOC_GET_PAGE_ENTRY:
293#ifdef CONFIG_COMPAT
294	case MTRRIOC32_GET_PAGE_ENTRY:
295#endif
296		if (gentry.regnum >= num_var_ranges)
297			return -EINVAL;
298		mtrr_if->get(gentry.regnum, &base, &size, &type);
299		/* Hide entries that would overflow */
300		if (size != (__typeof__(gentry.size))size)
301			gentry.base = gentry.size = gentry.type = 0;
302		else {
303			gentry.base = base;
304			gentry.size = size;
305			gentry.type = type;
306		}
307		break;
308	}
309
310	if (err)
311		return err;
312
313	switch (cmd) {
314	case MTRRIOC_GET_ENTRY:
315	case MTRRIOC_GET_PAGE_ENTRY:
316		if (copy_to_user(arg, &gentry, sizeof(gentry)))
317			err = -EFAULT;
318		break;
319#ifdef CONFIG_COMPAT
320	case MTRRIOC32_GET_ENTRY:
321	case MTRRIOC32_GET_PAGE_ENTRY: {
322		struct mtrr_gentry32 __user *g32;
323
324		g32 = (struct mtrr_gentry32 __user *)__arg;
325		err = put_user(gentry.base, &g32->base);
326		err |= put_user(gentry.size, &g32->size);
327		err |= put_user(gentry.regnum, &g32->regnum);
328		err |= put_user(gentry.type, &g32->type);
329		break;
330	}
331#endif
332	}
333	return err;
334}
335
336static int mtrr_close(struct inode *ino, struct file *file)
337{
338	unsigned int *fcount = FILE_FCOUNT(file);
339	int i, max;
340
341	if (fcount != NULL) {
342		max = num_var_ranges;
343		for (i = 0; i < max; ++i) {
344			while (fcount[i] > 0) {
345				mtrr_del(i, 0, 0);
346				--fcount[i];
347			}
348		}
349		kfree(fcount);
350		FILE_FCOUNT(file) = NULL;
351	}
352	return single_release(ino, file);
353}
354
355static int mtrr_seq_show(struct seq_file *seq, void *offset)
356{
357	char factor;
358	int i, max;
359	mtrr_type type;
360	unsigned long base, size;
361
362	max = num_var_ranges;
363	for (i = 0; i < max; i++) {
364		mtrr_if->get(i, &base, &size, &type);
365		if (size == 0) {
366			mtrr_usage_table[i] = 0;
367			continue;
368		}
369		if (size < (0x100000 >> PAGE_SHIFT)) {
370			/* less than 1MB */
371			factor = 'K';
372			size <<= PAGE_SHIFT - 10;
373		} else {
374			factor = 'M';
375			size >>= 20 - PAGE_SHIFT;
376		}
377		/* Base can be > 32bit */
378		seq_printf(seq, "reg%02i: base=0x%06lx000 (%5luMB), size=%5lu%cB, count=%d: %s\n",
379			   i, base, base >> (20 - PAGE_SHIFT),
380			   size, factor,
381			   mtrr_usage_table[i], mtrr_attrib_to_str(type));
382	}
383	return 0;
384}
385
386static int mtrr_open(struct inode *inode, struct file *file)
387{
388	if (!mtrr_if)
389		return -EIO;
390	if (!mtrr_if->get)
391		return -ENXIO;
392	if (!capable(CAP_SYS_ADMIN))
393		return -EPERM;
394	return single_open(file, mtrr_seq_show, NULL);
395}
396
397static const struct proc_ops mtrr_proc_ops = {
398	.proc_open		= mtrr_open,
399	.proc_read		= seq_read,
400	.proc_lseek		= seq_lseek,
401	.proc_write		= mtrr_write,
402	.proc_ioctl		= mtrr_ioctl,
403#ifdef CONFIG_COMPAT
404	.proc_compat_ioctl	= mtrr_ioctl,
405#endif
406	.proc_release		= mtrr_close,
407};
408
409static int __init mtrr_if_init(void)
410{
411	struct cpuinfo_x86 *c = &boot_cpu_data;
412
413	if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
414	    (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
415	    (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
416	    (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
417		return -ENODEV;
418
419	proc_create("mtrr", S_IWUSR | S_IRUGO, NULL, &mtrr_proc_ops);
420	return 0;
421}
422arch_initcall(mtrr_if_init);
423#endif			/*  CONFIG_PROC_FS  */
~/ Read-only file preview.