mem.c 19.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 *  linux/drivers/char/mem.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  Added devfs support. 
 *    Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8
 *  Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22
 */

#include <linux/mm.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/mman.h>
#include <linux/random.h>
#include <linux/init.h>
#include <linux/raw.h>
#include <linux/tty.h>
#include <linux/capability.h>
#include <linux/ptrace.h>
#include <linux/device.h>
23 24
#include <linux/highmem.h>
#include <linux/crash_dump.h>
L
Linus Torvalds 已提交
25
#include <linux/backing-dev.h>
26
#include <linux/bootmem.h>
27
#include <linux/splice.h>
28
#include <linux/pfn.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36

#include <asm/uaccess.h>
#include <asm/io.h>

#ifdef CONFIG_IA64
# include <linux/efi.h>
#endif

37 38 39 40 41 42 43 44 45 46 47 48 49
static inline unsigned long size_inside_page(unsigned long start,
					     unsigned long size)
{
	unsigned long sz;

	if (-start & (PAGE_SIZE - 1))
		sz = -start & (PAGE_SIZE - 1);
	else
		sz = PAGE_SIZE;

	return min_t(unsigned long, sz, size);
}

L
Linus Torvalds 已提交
50 51 52 53 54 55 56
/*
 * Architectures vary in how they handle caching for addresses
 * outside of main memory.
 *
 */
static inline int uncached_access(struct file *file, unsigned long addr)
{
57
#if defined(CONFIG_IA64)
L
Linus Torvalds 已提交
58
	/*
59
	 * On ia64, we ignore O_DSYNC because we cannot tolerate memory attribute aliases.
L
Linus Torvalds 已提交
60 61
	 */
	return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
62 63 64 65 66 67 68
#elif defined(CONFIG_MIPS)
	{
		extern int __uncached_access(struct file *file,
					     unsigned long addr);

		return __uncached_access(file, addr);
	}
L
Linus Torvalds 已提交
69 70 71
#else
	/*
	 * Accessing memory above the top the kernel knows about or through a file pointer
72
	 * that was marked O_DSYNC will be done non-cached.
L
Linus Torvalds 已提交
73
	 */
74
	if (file->f_flags & O_DSYNC)
L
Linus Torvalds 已提交
75 76 77 78 79 80
		return 1;
	return addr >= __pa(high_memory);
#endif
}

#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
81
static inline int valid_phys_addr_range(unsigned long addr, size_t count)
L
Linus Torvalds 已提交
82
{
83
	if (addr + count > __pa(high_memory))
L
Linus Torvalds 已提交
84 85 86 87
		return 0;

	return 1;
}
88

89
static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
90 91 92
{
	return 1;
}
L
Linus Torvalds 已提交
93 94
#endif

95
#ifdef CONFIG_STRICT_DEVMEM
96
static inline int range_is_allowed(unsigned long pfn, unsigned long size)
97
{
98 99 100 101 102 103 104 105
	u64 from = ((u64)pfn) << PAGE_SHIFT;
	u64 to = from + size;
	u64 cursor = from;

	while (cursor < to) {
		if (!devmem_is_allowed(pfn)) {
			printk(KERN_INFO
		"Program %s tried to access /dev/mem between %Lx->%Lx.\n",
106 107 108
				current->comm, from, to);
			return 0;
		}
109 110
		cursor += PAGE_SIZE;
		pfn++;
111 112 113 114
	}
	return 1;
}
#else
115
static inline int range_is_allowed(unsigned long pfn, unsigned long size)
116 117 118 119 120
{
	return 1;
}
#endif

121 122 123 124
void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr)
{
}

L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134 135
/*
 * This funcion reads the *physical* memory. The f_pos points directly to the 
 * memory location. 
 */
static ssize_t read_mem(struct file * file, char __user * buf,
			size_t count, loff_t *ppos)
{
	unsigned long p = *ppos;
	ssize_t read, sz;
	char *ptr;

136
	if (!valid_phys_addr_range(p, count))
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		return -EFAULT;
	read = 0;
#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
	/* we don't have page 0 mapped on sparc and m68k.. */
	if (p < PAGE_SIZE) {
		sz = PAGE_SIZE - p;
		if (sz > count) 
			sz = count; 
		if (sz > 0) {
			if (clear_user(buf, sz))
				return -EFAULT;
			buf += sz; 
			p += sz; 
			count -= sz; 
			read += sz; 
		}
	}
#endif

	while (count > 0) {
157 158
		unsigned long remaining;

159
		sz = size_inside_page(p, count);
L
Linus Torvalds 已提交
160

161 162 163
		if (!range_is_allowed(p >> PAGE_SHIFT, count))
			return -EPERM;

L
Linus Torvalds 已提交
164 165 166 167 168 169
		/*
		 * On ia64 if a page has been mapped somewhere as
		 * uncached, then it must also be accessed uncached
		 * by the kernel or data corruption may occur
		 */
		ptr = xlate_dev_mem_ptr(p);
170 171
		if (!ptr)
			return -EFAULT;
L
Linus Torvalds 已提交
172

173
		remaining = copy_to_user(buf, ptr, sz);
174
		unxlate_dev_mem_ptr(p, ptr);
175 176
		if (remaining)
			return -EFAULT;
177

L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
		buf += sz;
		p += sz;
		count -= sz;
		read += sz;
	}

	*ppos += read;
	return read;
}

static ssize_t write_mem(struct file * file, const char __user * buf, 
			 size_t count, loff_t *ppos)
{
	unsigned long p = *ppos;
	ssize_t written, sz;
	unsigned long copied;
	void *ptr;

196
	if (!valid_phys_addr_range(p, count))
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
		return -EFAULT;

	written = 0;

#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
	/* we don't have page 0 mapped on sparc and m68k.. */
	if (p < PAGE_SIZE) {
		unsigned long sz = PAGE_SIZE - p;
		if (sz > count)
			sz = count;
		/* Hmm. Do something? */
		buf += sz;
		p += sz;
		count -= sz;
		written += sz;
	}
#endif

	while (count > 0) {
216
		sz = size_inside_page(p, count);
L
Linus Torvalds 已提交
217

218 219 220
		if (!range_is_allowed(p >> PAGE_SHIFT, sz))
			return -EPERM;

L
Linus Torvalds 已提交
221 222 223 224 225 226
		/*
		 * On ia64 if a page has been mapped somewhere as
		 * uncached, then it must also be accessed uncached
		 * by the kernel or data corruption may occur
		 */
		ptr = xlate_dev_mem_ptr(p);
227 228 229 230 231
		if (!ptr) {
			if (written)
				break;
			return -EFAULT;
		}
L
Linus Torvalds 已提交
232 233

		copied = copy_from_user(ptr, buf, sz);
234
		unxlate_dev_mem_ptr(p, ptr);
L
Linus Torvalds 已提交
235
		if (copied) {
236 237 238
			written += sz - copied;
			if (written)
				break;
L
Linus Torvalds 已提交
239 240
			return -EFAULT;
		}
241

L
Linus Torvalds 已提交
242 243 244 245 246 247 248 249 250 251
		buf += sz;
		p += sz;
		count -= sz;
		written += sz;
	}

	*ppos += written;
	return written;
}

252 253 254 255 256 257
int __attribute__((weak)) phys_mem_access_prot_allowed(struct file *file,
	unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
{
	return 1;
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271
#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
				     unsigned long size, pgprot_t vma_prot)
{
#ifdef pgprot_noncached
	unsigned long offset = pfn << PAGE_SHIFT;

	if (uncached_access(file, offset))
		return pgprot_noncached(vma_prot);
#endif
	return vma_prot;
}
#endif

272 273 274 275 276 277 278 279 280
#ifndef CONFIG_MMU
static unsigned long get_unmapped_area_mem(struct file *file,
					   unsigned long addr,
					   unsigned long len,
					   unsigned long pgoff,
					   unsigned long flags)
{
	if (!valid_mmap_phys_addr_range(pgoff, len))
		return (unsigned long) -EINVAL;
281
	return pgoff << PAGE_SHIFT;
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

/* can't do an in-place private mapping if there's no MMU */
static inline int private_mapping_ok(struct vm_area_struct *vma)
{
	return vma->vm_flags & VM_MAYSHARE;
}
#else
#define get_unmapped_area_mem	NULL

static inline int private_mapping_ok(struct vm_area_struct *vma)
{
	return 1;
}
#endif

298
static const struct vm_operations_struct mmap_mem_ops = {
299 300 301
#ifdef CONFIG_HAVE_IOREMAP_PROT
	.access = generic_access_phys
#endif
302 303
};

L
Linus Torvalds 已提交
304 305
static int mmap_mem(struct file * file, struct vm_area_struct * vma)
{
306 307
	size_t size = vma->vm_end - vma->vm_start;

308
	if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
309 310
		return -EINVAL;

311 312 313
	if (!private_mapping_ok(vma))
		return -ENOSYS;

314 315 316
	if (!range_is_allowed(vma->vm_pgoff, size))
		return -EPERM;

317 318 319 320
	if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
						&vma->vm_page_prot))
		return -EINVAL;

321
	vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
322
						 size,
L
Linus Torvalds 已提交
323 324
						 vma->vm_page_prot);

325 326
	vma->vm_ops = &mmap_mem_ops;

L
Linus Torvalds 已提交
327 328 329 330
	/* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
	if (remap_pfn_range(vma,
			    vma->vm_start,
			    vma->vm_pgoff,
331
			    size,
332
			    vma->vm_page_prot)) {
L
Linus Torvalds 已提交
333
		return -EAGAIN;
334
	}
L
Linus Torvalds 已提交
335 336 337
	return 0;
}

338
#ifdef CONFIG_DEVKMEM
L
Linus Torvalds 已提交
339 340
static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
{
L
Linus Torvalds 已提交
341 342
	unsigned long pfn;

343 344
	/* Turn a kernel-virtual address into a physical page frame */
	pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
L
Linus Torvalds 已提交
345

L
Linus Torvalds 已提交
346 347 348 349 350 351 352
	/*
	 * RED-PEN: on some architectures there is more mapped memory
	 * than available in mem_map which pfn_valid checks
	 * for. Perhaps should add a new macro here.
	 *
	 * RED-PEN: vmalloc is not supported right now.
	 */
L
Linus Torvalds 已提交
353
	if (!pfn_valid(pfn))
L
Linus Torvalds 已提交
354
		return -EIO;
L
Linus Torvalds 已提交
355 356

	vma->vm_pgoff = pfn;
L
Linus Torvalds 已提交
357 358
	return mmap_mem(file, vma);
}
359
#endif
L
Linus Torvalds 已提交
360

361 362 363 364
#ifdef CONFIG_CRASH_DUMP
/*
 * Read memory corresponding to the old kernel.
 */
365
static ssize_t read_oldmem(struct file *file, char __user *buf,
366 367
				size_t count, loff_t *ppos)
{
368 369 370
	unsigned long pfn, offset;
	size_t read = 0, csize;
	int rc = 0;
371

M
Maneesh Soni 已提交
372
	while (count) {
373
		pfn = *ppos / PAGE_SIZE;
374 375
		if (pfn > saved_max_pfn)
			return read;
376

377 378 379 380 381
		offset = (unsigned long)(*ppos % PAGE_SIZE);
		if (count > PAGE_SIZE - offset)
			csize = PAGE_SIZE - offset;
		else
			csize = count;
382

383 384 385
		rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
		if (rc < 0)
			return rc;
386 387 388 389 390 391 392 393
		buf += csize;
		*ppos += csize;
		read += csize;
		count -= csize;
	}
	return read;
}
#endif
L
Linus Torvalds 已提交
394

395
#ifdef CONFIG_DEVKMEM
L
Linus Torvalds 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
/*
 * This function reads the *virtual* memory as seen by the kernel.
 */
static ssize_t read_kmem(struct file *file, char __user *buf, 
			 size_t count, loff_t *ppos)
{
	unsigned long p = *ppos;
	ssize_t low_count, read, sz;
	char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */

	read = 0;
	if (p < (unsigned long) high_memory) {
		low_count = count;
		if (count > (unsigned long) high_memory - p)
			low_count = (unsigned long) high_memory - p;

#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
		/* we don't have page 0 mapped on sparc and m68k.. */
		if (p < PAGE_SIZE && low_count > 0) {
			size_t tmp = PAGE_SIZE - p;
			if (tmp > low_count) tmp = low_count;
			if (clear_user(buf, tmp))
				return -EFAULT;
			buf += tmp;
			p += tmp;
			read += tmp;
			low_count -= tmp;
			count -= tmp;
		}
#endif
		while (low_count > 0) {
427
			sz = size_inside_page(p, low_count);
L
Linus Torvalds 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

			/*
			 * On ia64 if a page has been mapped somewhere as
			 * uncached, then it must also be accessed uncached
			 * by the kernel or data corruption may occur
			 */
			kbuf = xlate_dev_kmem_ptr((char *)p);

			if (copy_to_user(buf, kbuf, sz))
				return -EFAULT;
			buf += sz;
			p += sz;
			read += sz;
			low_count -= sz;
			count -= sz;
		}
	}

	if (count > 0) {
		kbuf = (char *)__get_free_page(GFP_KERNEL);
		if (!kbuf)
			return -ENOMEM;
		while (count > 0) {
451
			int len = size_inside_page(p, count);
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

			len = vread(kbuf, (char *)p, len);
			if (!len)
				break;
			if (copy_to_user(buf, kbuf, len)) {
				free_page((unsigned long)kbuf);
				return -EFAULT;
			}
			count -= len;
			buf += len;
			read += len;
			p += len;
		}
		free_page((unsigned long)kbuf);
	}
 	*ppos = p;
 	return read;
}


static inline ssize_t
do_write_kmem(void *p, unsigned long realp, const char __user * buf,
	      size_t count, loff_t *ppos)
{
	ssize_t written, sz;
	unsigned long copied;

	written = 0;
#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
	/* we don't have page 0 mapped on sparc and m68k.. */
	if (realp < PAGE_SIZE) {
		unsigned long sz = PAGE_SIZE - realp;
		if (sz > count)
			sz = count;
		/* Hmm. Do something? */
		buf += sz;
		p += sz;
		realp += sz;
		count -= sz;
		written += sz;
	}
#endif

	while (count > 0) {
		char *ptr;

498
		sz = size_inside_page(realp, count);
L
Linus Torvalds 已提交
499 500 501 502 503 504 505 506 507 508

		/*
		 * On ia64 if a page has been mapped somewhere as
		 * uncached, then it must also be accessed uncached
		 * by the kernel or data corruption may occur
		 */
		ptr = xlate_dev_kmem_ptr(p);

		copied = copy_from_user(ptr, buf, sz);
		if (copied) {
509 510 511
			written += sz - copied;
			if (written)
				break;
L
Linus Torvalds 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
			return -EFAULT;
		}
		buf += sz;
		p += sz;
		realp += sz;
		count -= sz;
		written += sz;
	}

	*ppos += written;
	return written;
}


/*
 * This function writes to the *virtual* memory as seen by the kernel.
 */
static ssize_t write_kmem(struct file * file, const char __user * buf, 
			  size_t count, loff_t *ppos)
{
	unsigned long p = *ppos;
	ssize_t wrote = 0;
	ssize_t virtr = 0;
	ssize_t written;
	char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */

	if (p < (unsigned long) high_memory) {

		wrote = count;
		if (count > (unsigned long) high_memory - p)
			wrote = (unsigned long) high_memory - p;

		written = do_write_kmem((void*)p, p, buf, wrote, ppos);
		if (written != wrote)
			return written;
		wrote = written;
		p += wrote;
		buf += wrote;
		count -= wrote;
	}

	if (count > 0) {
		kbuf = (char *)__get_free_page(GFP_KERNEL);
		if (!kbuf)
			return wrote ? wrote : -ENOMEM;
		while (count > 0) {
558
			int len = size_inside_page(p, count);
L
Linus Torvalds 已提交
559

560 561 562 563 564 565
			written = copy_from_user(kbuf, buf, len);
			if (written) {
				if (wrote + virtr)
					break;
				free_page((unsigned long)kbuf);
				return -EFAULT;
L
Linus Torvalds 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578
			}
			len = vwrite(kbuf, (char *)p, len);
			count -= len;
			buf += len;
			virtr += len;
			p += len;
		}
		free_page((unsigned long)kbuf);
	}

 	*ppos = p;
 	return virtr + wrote;
}
579
#endif
L
Linus Torvalds 已提交
580

581
#ifdef CONFIG_DEVPORT
L
Linus Torvalds 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
static ssize_t read_port(struct file * file, char __user * buf,
			 size_t count, loff_t *ppos)
{
	unsigned long i = *ppos;
	char __user *tmp = buf;

	if (!access_ok(VERIFY_WRITE, buf, count))
		return -EFAULT; 
	while (count-- > 0 && i < 65536) {
		if (__put_user(inb(i),tmp) < 0) 
			return -EFAULT;  
		i++;
		tmp++;
	}
	*ppos = i;
	return tmp-buf;
}

static ssize_t write_port(struct file * file, const char __user * buf,
			  size_t count, loff_t *ppos)
{
	unsigned long i = *ppos;
	const char __user * tmp = buf;

	if (!access_ok(VERIFY_READ,buf,count))
		return -EFAULT;
	while (count-- > 0 && i < 65536) {
		char c;
610 611 612
		if (__get_user(c, tmp)) {
			if (tmp > buf)
				break;
L
Linus Torvalds 已提交
613
			return -EFAULT; 
614
		}
L
Linus Torvalds 已提交
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
		outb(c,i);
		i++;
		tmp++;
	}
	*ppos = i;
	return tmp-buf;
}
#endif

static ssize_t read_null(struct file * file, char __user * buf,
			 size_t count, loff_t *ppos)
{
	return 0;
}

static ssize_t write_null(struct file * file, const char __user * buf,
			  size_t count, loff_t *ppos)
{
	return count;
}

636 637 638 639 640 641 642 643 644 645 646 647
static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
			struct splice_desc *sd)
{
	return sd->len;
}

static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
				 loff_t *ppos, size_t len, unsigned int flags)
{
	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
}

L
Linus Torvalds 已提交
648 649 650
static ssize_t read_zero(struct file * file, char __user * buf, 
			 size_t count, loff_t *ppos)
{
N
Nick Piggin 已提交
651
	size_t written;
L
Linus Torvalds 已提交
652 653 654 655 656 657 658

	if (!count)
		return 0;

	if (!access_ok(VERIFY_WRITE, buf, count))
		return -EFAULT;

N
Nick Piggin 已提交
659 660 661 662
	written = 0;
	while (count) {
		unsigned long unwritten;
		size_t chunk = count;
L
Linus Torvalds 已提交
663

N
Nick Piggin 已提交
664 665
		if (chunk > PAGE_SIZE)
			chunk = PAGE_SIZE;	/* Just for latency reasons */
666
		unwritten = __clear_user(buf, chunk);
N
Nick Piggin 已提交
667
		written += chunk - unwritten;
L
Linus Torvalds 已提交
668
		if (unwritten)
N
Nick Piggin 已提交
669
			break;
670 671
		if (signal_pending(current))
			return written ? written : -ERESTARTSYS;
L
Linus Torvalds 已提交
672
		buf += chunk;
N
Nick Piggin 已提交
673
		count -= chunk;
L
Linus Torvalds 已提交
674 675
		cond_resched();
	}
N
Nick Piggin 已提交
676
	return written ? written : -EFAULT;
L
Linus Torvalds 已提交
677 678 679 680
}

static int mmap_zero(struct file * file, struct vm_area_struct * vma)
{
N
Nick Piggin 已提交
681
#ifndef CONFIG_MMU
L
Linus Torvalds 已提交
682
	return -ENOSYS;
N
Nick Piggin 已提交
683 684 685 686
#endif
	if (vma->vm_flags & VM_SHARED)
		return shmem_zero_setup(vma);
	return 0;
L
Linus Torvalds 已提交
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
}

static ssize_t write_full(struct file * file, const char __user * buf,
			  size_t count, loff_t *ppos)
{
	return -ENOSPC;
}

/*
 * Special lseek() function for /dev/null and /dev/zero.  Most notably, you
 * can fopen() both devices with "a" now.  This was previously impossible.
 * -- SRB.
 */

static loff_t null_lseek(struct file * file, loff_t offset, int orig)
{
	return file->f_pos = 0;
}

/*
 * The memory devices use the full 32/64 bits of the offset, and so we cannot
 * check against negative addresses: they are ok. The return value is weird,
 * though, in that case (0).
 *
 * also note that seeking relative to the "end of file" isn't supported:
 * it has no meaning, so it returns -EINVAL.
 */
static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
{
	loff_t ret;

718
	mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
L
Linus Torvalds 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732
	switch (orig) {
		case 0:
			file->f_pos = offset;
			ret = file->f_pos;
			force_successful_syscall_return();
			break;
		case 1:
			file->f_pos += offset;
			ret = file->f_pos;
			force_successful_syscall_return();
			break;
		default:
			ret = -EINVAL;
	}
733
	mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
L
Linus Torvalds 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747
	return ret;
}

static int open_port(struct inode * inode, struct file * filp)
{
	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
}

#define zero_lseek	null_lseek
#define full_lseek      null_lseek
#define write_zero	write_null
#define read_full       read_zero
#define open_mem	open_port
#define open_kmem	open_mem
748
#define open_oldmem	open_mem
L
Linus Torvalds 已提交
749

750
static const struct file_operations mem_fops = {
L
Linus Torvalds 已提交
751 752 753 754 755
	.llseek		= memory_lseek,
	.read		= read_mem,
	.write		= write_mem,
	.mmap		= mmap_mem,
	.open		= open_mem,
756
	.get_unmapped_area = get_unmapped_area_mem,
L
Linus Torvalds 已提交
757 758
};

759
#ifdef CONFIG_DEVKMEM
760
static const struct file_operations kmem_fops = {
L
Linus Torvalds 已提交
761 762 763 764 765
	.llseek		= memory_lseek,
	.read		= read_kmem,
	.write		= write_kmem,
	.mmap		= mmap_kmem,
	.open		= open_kmem,
766
	.get_unmapped_area = get_unmapped_area_mem,
L
Linus Torvalds 已提交
767
};
768
#endif
L
Linus Torvalds 已提交
769

770
static const struct file_operations null_fops = {
L
Linus Torvalds 已提交
771 772 773
	.llseek		= null_lseek,
	.read		= read_null,
	.write		= write_null,
774
	.splice_write	= splice_write_null,
L
Linus Torvalds 已提交
775 776
};

777
#ifdef CONFIG_DEVPORT
778
static const struct file_operations port_fops = {
L
Linus Torvalds 已提交
779 780 781 782 783 784 785
	.llseek		= memory_lseek,
	.read		= read_port,
	.write		= write_port,
	.open		= open_port,
};
#endif

786
static const struct file_operations zero_fops = {
L
Linus Torvalds 已提交
787 788 789 790 791 792
	.llseek		= zero_lseek,
	.read		= read_zero,
	.write		= write_zero,
	.mmap		= mmap_zero,
};

793 794 795 796
/*
 * capabilities for /dev/zero
 * - permits private mappings, "copies" are taken of the source of zeros
 */
L
Linus Torvalds 已提交
797
static struct backing_dev_info zero_bdi = {
798
	.name		= "char/mem",
L
Linus Torvalds 已提交
799 800 801
	.capabilities	= BDI_CAP_MAP_COPY,
};

802
static const struct file_operations full_fops = {
L
Linus Torvalds 已提交
803 804 805 806 807
	.llseek		= full_lseek,
	.read		= read_full,
	.write		= write_full,
};

808
#ifdef CONFIG_CRASH_DUMP
809
static const struct file_operations oldmem_fops = {
810 811 812 813 814
	.read	= read_oldmem,
	.open	= open_oldmem,
};
#endif

L
Linus Torvalds 已提交
815 816 817 818
static ssize_t kmsg_write(struct file * file, const char __user * buf,
			  size_t count, loff_t *ppos)
{
	char *tmp;
819
	ssize_t ret;
L
Linus Torvalds 已提交
820 821 822 823 824 825 826 827

	tmp = kmalloc(count + 1, GFP_KERNEL);
	if (tmp == NULL)
		return -ENOMEM;
	ret = -EFAULT;
	if (!copy_from_user(tmp, buf, count)) {
		tmp[count] = 0;
		ret = printk("%s", tmp);
828 829 830
		if (ret > count)
			/* printk can add a prefix */
			ret = count;
L
Linus Torvalds 已提交
831 832 833 834 835
	}
	kfree(tmp);
	return ret;
}

836
static const struct file_operations kmsg_fops = {
L
Linus Torvalds 已提交
837 838 839
	.write =	kmsg_write,
};

840 841
static const struct memdev {
	const char *name;
842
	mode_t mode;
843 844 845
	const struct file_operations *fops;
	struct backing_dev_info *dev_info;
} devlist[] = {
846
	 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
847
#ifdef CONFIG_DEVKMEM
848
	 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
849
#endif
850
	 [3] = { "null", 0666, &null_fops, NULL },
851
#ifdef CONFIG_DEVPORT
852
	 [4] = { "port", 0, &port_fops, NULL },
L
Linus Torvalds 已提交
853
#endif
854 855 856 857 858
	 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
	 [7] = { "full", 0666, &full_fops, NULL },
	 [8] = { "random", 0666, &random_fops, NULL },
	 [9] = { "urandom", 0666, &urandom_fops, NULL },
	[11] = { "kmsg", 0, &kmsg_fops, NULL },
859
#ifdef CONFIG_CRASH_DUMP
860
	[12] = { "oldmem", 0, &oldmem_fops, NULL },
861
#endif
862 863 864 865
};

static int memory_open(struct inode *inode, struct file *filp)
{
866 867
	int minor;
	const struct memdev *dev;
868

869 870
	minor = iminor(inode);
	if (minor >= ARRAY_SIZE(devlist))
871
		return -ENXIO;
872

873 874
	dev = &devlist[minor];
	if (!dev->fops)
875
		return -ENXIO;
876

877 878 879
	filp->f_op = dev->fops;
	if (dev->dev_info)
		filp->f_mapping->backing_dev_info = dev->dev_info;
880

881
	if (dev->fops->open)
882 883 884
		return dev->fops->open(inode, filp);

	return 0;
L
Linus Torvalds 已提交
885 886
}

887
static const struct file_operations memory_fops = {
888
	.open		= memory_open,
L
Linus Torvalds 已提交
889 890
};

891 892 893 894 895 896 897
static char *mem_devnode(struct device *dev, mode_t *mode)
{
	if (mode && devlist[MINOR(dev->devt)].mode)
		*mode = devlist[MINOR(dev->devt)].mode;
	return NULL;
}

898
static struct class *mem_class;
L
Linus Torvalds 已提交
899 900 901

static int __init chr_dev_init(void)
{
902
	int minor;
P
Peter Zijlstra 已提交
903 904 905 906 907
	int err;

	err = bdi_init(&zero_bdi);
	if (err)
		return err;
L
Linus Torvalds 已提交
908 909 910 911

	if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
		printk("unable to get major %d for memory devs\n", MEM_MAJOR);

912
	mem_class = class_create(THIS_MODULE, "mem");
913
	mem_class->devnode = mem_devnode;
914 915 916 917 918 919
	for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
		if (!devlist[minor].name)
			continue;
		device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
			      NULL, devlist[minor].name);
	}
920

L
Linus Torvalds 已提交
921 922 923 924
	return 0;
}

fs_initcall(chr_dev_init);