if.c 10.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2
#include <linux/init.h>
#include <linux/proc_fs.h>
3
#include <linux/capability.h>
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <linux/ctype.h>
#include <linux/module.h>
#include <linux/seq_file.h>
#include <asm/uaccess.h>

#define LINE_SIZE 80

#include <asm/mtrr.h>
#include "mtrr.h"

/* RED-PEN: this is accessed without any locking */
extern unsigned int *usage_table;


#define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)

J
Jan Beulich 已提交
20
static const char *const mtrr_strings[MTRR_NUM_TYPES] =
L
Linus Torvalds 已提交
21 22 23 24 25 26 27 28 29 30
{
    "uncachable",               /* 0 */
    "write-combining",          /* 1 */
    "?",                        /* 2 */
    "?",                        /* 3 */
    "write-through",            /* 4 */
    "write-protect",            /* 5 */
    "write-back",               /* 6 */
};

J
Jan Beulich 已提交
31
const char *mtrr_attrib_to_str(int x)
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
{
	return (x <= 6) ? mtrr_strings[x] : "?";
}

#ifdef CONFIG_PROC_FS

static int
mtrr_file_add(unsigned long base, unsigned long size,
	      unsigned int type, char increment, struct file *file, int page)
{
	int reg, max;
	unsigned int *fcount = FILE_FCOUNT(file); 

	max = num_var_ranges;
	if (fcount == NULL) {
47
		fcount = kzalloc(max * sizeof *fcount, GFP_KERNEL);
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
		if (!fcount)
			return -ENOMEM;
		FILE_FCOUNT(file) = fcount;
	}
	if (!page) {
		if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
			return -EINVAL;
		base >>= PAGE_SHIFT;
		size >>= PAGE_SHIFT;
	}
	reg = mtrr_add_page(base, size, type, 1);
	if (reg >= 0)
		++fcount[reg];
	return reg;
}

static int
mtrr_file_del(unsigned long base, unsigned long size,
	      struct file *file, int page)
{
	int reg;
	unsigned int *fcount = FILE_FCOUNT(file);

	if (!page) {
		if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
			return -EINVAL;
		base >>= PAGE_SHIFT;
		size >>= PAGE_SHIFT;
	}
	reg = mtrr_del_page(-1, base, size);
	if (reg < 0)
		return reg;
	if (fcount == NULL)
		return reg;
	if (fcount[reg] < 1)
		return -EINVAL;
	--fcount[reg];
	return reg;
}

/* RED-PEN: seq_file can seek now. this is ignored. */
static ssize_t
mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
/*  Format of control line:
    "base=%Lx size=%Lx type=%s"     OR:
    "disable=%d"
*/
{
	int i, err;
	unsigned long reg;
	unsigned long long base, size;
	char *ptr;
	char line[LINE_SIZE];
	size_t linelen;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;
	if (!len)
		return -EINVAL;
	memset(line, 0, LINE_SIZE);
	if (len > LINE_SIZE)
		len = LINE_SIZE;
	if (copy_from_user(line, buf, len - 1))
		return -EFAULT;
	linelen = strlen(line);
	ptr = line + linelen - 1;
	if (linelen && *ptr == '\n')
		*ptr = '\0';
	if (!strncmp(line, "disable=", 8)) {
		reg = simple_strtoul(line + 8, &ptr, 0);
		err = mtrr_del_page(reg, 0, 0);
		if (err < 0)
			return err;
		return len;
	}
	if (strncmp(line, "base=", 5))
		return -EINVAL;
	base = simple_strtoull(line + 5, &ptr, 0);
	for (; isspace(*ptr); ++ptr) ;
	if (strncmp(ptr, "size=", 5))
		return -EINVAL;
	size = simple_strtoull(ptr + 5, &ptr, 0);
	if ((base & 0xfff) || (size & 0xfff))
		return -EINVAL;
	for (; isspace(*ptr); ++ptr) ;
	if (strncmp(ptr, "type=", 5))
		return -EINVAL;
	ptr += 5;
	for (; isspace(*ptr); ++ptr) ;
	for (i = 0; i < MTRR_NUM_TYPES; ++i) {
		if (strcmp(ptr, mtrr_strings[i]))
			continue;
		base >>= PAGE_SHIFT;
		size >>= PAGE_SHIFT;
		err =
		    mtrr_add_page((unsigned long) base, (unsigned long) size, i,
				  1);
		if (err < 0)
			return err;
		return len;
	}
	return -EINVAL;
}

152 153
static long
mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
L
Linus Torvalds 已提交
154
{
155
	int err = 0;
L
Linus Torvalds 已提交
156
	mtrr_type type;
J
Jan Beulich 已提交
157
	unsigned long size;
L
Linus Torvalds 已提交
158 159 160 161
	struct mtrr_sentry sentry;
	struct mtrr_gentry gentry;
	void __user *arg = (void __user *) __arg;

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	switch (cmd) {
	case MTRRIOC_ADD_ENTRY:
	case MTRRIOC_SET_ENTRY:
	case MTRRIOC_DEL_ENTRY:
	case MTRRIOC_KILL_ENTRY:
	case MTRRIOC_ADD_PAGE_ENTRY:
	case MTRRIOC_SET_PAGE_ENTRY:
	case MTRRIOC_DEL_PAGE_ENTRY:
	case MTRRIOC_KILL_PAGE_ENTRY:
		if (copy_from_user(&sentry, arg, sizeof sentry))
			return -EFAULT;
		break;
	case MTRRIOC_GET_ENTRY:
	case MTRRIOC_GET_PAGE_ENTRY:
		if (copy_from_user(&gentry, arg, sizeof gentry))
			return -EFAULT;
		break;
#ifdef CONFIG_COMPAT
	case MTRRIOC32_ADD_ENTRY:
	case MTRRIOC32_SET_ENTRY:
	case MTRRIOC32_DEL_ENTRY:
	case MTRRIOC32_KILL_ENTRY:
	case MTRRIOC32_ADD_PAGE_ENTRY:
	case MTRRIOC32_SET_PAGE_ENTRY:
	case MTRRIOC32_DEL_PAGE_ENTRY:
	case MTRRIOC32_KILL_PAGE_ENTRY: {
		struct mtrr_sentry32 __user *s32 = (struct mtrr_sentry32 __user *)__arg;
		err = get_user(sentry.base, &s32->base);
		err |= get_user(sentry.size, &s32->size);
		err |= get_user(sentry.type, &s32->type);
		if (err)
			return err;
		break;
	}
	case MTRRIOC32_GET_ENTRY:
	case MTRRIOC32_GET_PAGE_ENTRY: {
		struct mtrr_gentry32 __user *g32 = (struct mtrr_gentry32 __user *)__arg;
		err = get_user(gentry.regnum, &g32->regnum);
		err |= get_user(gentry.base, &g32->base);
		err |= get_user(gentry.size, &g32->size);
		err |= get_user(gentry.type, &g32->type);
		if (err)
			return err;
		break;
	}
#endif
	}

L
Linus Torvalds 已提交
210 211 212 213
	switch (cmd) {
	default:
		return -ENOTTY;
	case MTRRIOC_ADD_ENTRY:
214 215 216
#ifdef CONFIG_COMPAT
	case MTRRIOC32_ADD_ENTRY:
#endif
L
Linus Torvalds 已提交
217 218 219 220 221 222 223
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		err =
		    mtrr_file_add(sentry.base, sentry.size, sentry.type, 1,
				  file, 0);
		break;
	case MTRRIOC_SET_ENTRY:
224 225 226
#ifdef CONFIG_COMPAT
	case MTRRIOC32_SET_ENTRY:
#endif
L
Linus Torvalds 已提交
227 228 229 230 231
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		err = mtrr_add(sentry.base, sentry.size, sentry.type, 0);
		break;
	case MTRRIOC_DEL_ENTRY:
232 233 234
#ifdef CONFIG_COMPAT
	case MTRRIOC32_DEL_ENTRY:
#endif
L
Linus Torvalds 已提交
235 236 237 238 239
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		err = mtrr_file_del(sentry.base, sentry.size, file, 0);
		break;
	case MTRRIOC_KILL_ENTRY:
240 241 242
#ifdef CONFIG_COMPAT
	case MTRRIOC32_KILL_ENTRY:
#endif
L
Linus Torvalds 已提交
243 244 245 246 247
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		err = mtrr_del(-1, sentry.base, sentry.size);
		break;
	case MTRRIOC_GET_ENTRY:
248 249 250
#ifdef CONFIG_COMPAT
	case MTRRIOC32_GET_ENTRY:
#endif
L
Linus Torvalds 已提交
251 252
		if (gentry.regnum >= num_var_ranges)
			return -EINVAL;
J
Jan Beulich 已提交
253
		mtrr_if->get(gentry.regnum, &gentry.base, &size, &type);
L
Linus Torvalds 已提交
254 255

		/* Hide entries that go above 4GB */
J
Jan Beulich 已提交
256 257
		if (gentry.base + size - 1 >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT))
		    || size >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT)))
L
Linus Torvalds 已提交
258 259 260
			gentry.base = gentry.size = gentry.type = 0;
		else {
			gentry.base <<= PAGE_SHIFT;
J
Jan Beulich 已提交
261
			gentry.size = size << PAGE_SHIFT;
L
Linus Torvalds 已提交
262 263 264 265 266
			gentry.type = type;
		}

		break;
	case MTRRIOC_ADD_PAGE_ENTRY:
267 268 269
#ifdef CONFIG_COMPAT
	case MTRRIOC32_ADD_PAGE_ENTRY:
#endif
L
Linus Torvalds 已提交
270 271 272 273 274 275 276
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		err =
		    mtrr_file_add(sentry.base, sentry.size, sentry.type, 1,
				  file, 1);
		break;
	case MTRRIOC_SET_PAGE_ENTRY:
277 278 279
#ifdef CONFIG_COMPAT
	case MTRRIOC32_SET_PAGE_ENTRY:
#endif
L
Linus Torvalds 已提交
280 281 282 283 284
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		err = mtrr_add_page(sentry.base, sentry.size, sentry.type, 0);
		break;
	case MTRRIOC_DEL_PAGE_ENTRY:
285 286 287
#ifdef CONFIG_COMPAT
	case MTRRIOC32_DEL_PAGE_ENTRY:
#endif
L
Linus Torvalds 已提交
288 289 290 291 292
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		err = mtrr_file_del(sentry.base, sentry.size, file, 1);
		break;
	case MTRRIOC_KILL_PAGE_ENTRY:
293 294 295
#ifdef CONFIG_COMPAT
	case MTRRIOC32_KILL_PAGE_ENTRY:
#endif
L
Linus Torvalds 已提交
296 297 298 299 300
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		err = mtrr_del_page(-1, sentry.base, sentry.size);
		break;
	case MTRRIOC_GET_PAGE_ENTRY:
301 302 303
#ifdef CONFIG_COMPAT
	case MTRRIOC32_GET_PAGE_ENTRY:
#endif
L
Linus Torvalds 已提交
304 305
		if (gentry.regnum >= num_var_ranges)
			return -EINVAL;
J
Jan Beulich 已提交
306 307 308 309 310 311 312 313
		mtrr_if->get(gentry.regnum, &gentry.base, &size, &type);
		/* Hide entries that would overflow */
		if (size != (__typeof__(gentry.size))size)
			gentry.base = gentry.size = gentry.type = 0;
		else {
			gentry.size = size;
			gentry.type = type;
		}
314 315
		break;
	}
L
Linus Torvalds 已提交
316

317 318 319 320 321 322
	if (err)
		return err;

	switch(cmd) {
	case MTRRIOC_GET_ENTRY:
	case MTRRIOC_GET_PAGE_ENTRY:
L
Linus Torvalds 已提交
323
		if (copy_to_user(arg, &gentry, sizeof gentry))
324
			err = -EFAULT;
L
Linus Torvalds 已提交
325
		break;
326 327 328 329 330 331 332 333 334 335 336
#ifdef CONFIG_COMPAT
	case MTRRIOC32_GET_ENTRY:
	case MTRRIOC32_GET_PAGE_ENTRY: {
		struct mtrr_gentry32 __user *g32 = (struct mtrr_gentry32 __user *)__arg;
		err = put_user(gentry.base, &g32->base);
		err |= put_user(gentry.size, &g32->size);
		err |= put_user(gentry.regnum, &g32->regnum);
		err |= put_user(gentry.type, &g32->type);
		break;
	}
#endif
L
Linus Torvalds 已提交
337
	}
338
	return err;
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
}

static int
mtrr_close(struct inode *ino, struct file *file)
{
	int i, max;
	unsigned int *fcount = FILE_FCOUNT(file);

	if (fcount != NULL) {
		max = num_var_ranges;
		for (i = 0; i < max; ++i) {
			while (fcount[i] > 0) {
				mtrr_del(i, 0, 0);
				--fcount[i];
			}
		}
		kfree(fcount);
		FILE_FCOUNT(file) = NULL;
	}
	return single_release(ino, file);
}

static int mtrr_seq_show(struct seq_file *seq, void *offset);

static int mtrr_open(struct inode *inode, struct file *file)
{
	if (!mtrr_if) 
		return -EIO;
	if (!mtrr_if->get) 
		return -ENXIO; 
	return single_open(file, mtrr_seq_show, NULL);
}

372
static const struct file_operations mtrr_fops = {
L
Linus Torvalds 已提交
373 374 375 376 377
	.owner   = THIS_MODULE,
	.open	 = mtrr_open, 
	.read    = seq_read,
	.llseek  = seq_lseek,
	.write   = mtrr_write,
378 379
	.unlocked_ioctl = mtrr_ioctl,
	.compat_ioctl = mtrr_ioctl,
L
Linus Torvalds 已提交
380 381 382 383 384 385 386 387 388 389 390 391
	.release = mtrr_close,
};


static struct proc_dir_entry *proc_root_mtrr;


static int mtrr_seq_show(struct seq_file *seq, void *offset)
{
	char factor;
	int i, max, len;
	mtrr_type type;
J
Jan Beulich 已提交
392
	unsigned long base, size;
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

	len = 0;
	max = num_var_ranges;
	for (i = 0; i < max; i++) {
		mtrr_if->get(i, &base, &size, &type);
		if (size == 0)
			usage_table[i] = 0;
		else {
			if (size < (0x100000 >> PAGE_SHIFT)) {
				/* less than 1MB */
				factor = 'K';
				size <<= PAGE_SHIFT - 10;
			} else {
				factor = 'M';
				size >>= 20 - PAGE_SHIFT;
			}
			/* RED-PEN: base can be > 32bit */ 
			len += seq_printf(seq, 
J
Jan Beulich 已提交
411
				   "reg%02i: base=0x%05lx000 (%4luMB), size=%4lu%cB: %s, count=%d\n",
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
			     i, base, base >> (20 - PAGE_SHIFT), size, factor,
			     mtrr_attrib_to_str(type), usage_table[i]);
		}
	}
	return 0;
}

static int __init mtrr_if_init(void)
{
	struct cpuinfo_x86 *c = &boot_cpu_data;

	if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
	    (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
	    (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
	    (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
		return -ENODEV;

	proc_root_mtrr =
	    create_proc_entry("mtrr", S_IWUSR | S_IRUGO, &proc_root);
	if (proc_root_mtrr) {
		proc_root_mtrr->owner = THIS_MODULE;
		proc_root_mtrr->proc_fops = &mtrr_fops;
	}
	return 0;
}

arch_initcall(mtrr_if_init);
#endif			/*  CONFIG_PROC_FS  */