xref: /linux/drivers/char/mem.c (revision de2fe5e07d58424bc286fff3fd3c1b0bf933cd58)
1 /*
2  *  linux/drivers/char/mem.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  Added devfs support.
7  *    Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8  *  Shared /dev/zero mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
9  */
10 
11 #include <linux/config.h>
12 #include <linux/mm.h>
13 #include <linux/miscdevice.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/mman.h>
17 #include <linux/random.h>
18 #include <linux/init.h>
19 #include <linux/raw.h>
20 #include <linux/tty.h>
21 #include <linux/capability.h>
22 #include <linux/smp_lock.h>
23 #include <linux/devfs_fs_kernel.h>
24 #include <linux/ptrace.h>
25 #include <linux/device.h>
26 #include <linux/highmem.h>
27 #include <linux/crash_dump.h>
28 #include <linux/backing-dev.h>
29 #include <linux/bootmem.h>
30 
31 #include <asm/uaccess.h>
32 #include <asm/io.h>
33 
34 #ifdef CONFIG_IA64
35 # include <linux/efi.h>
36 #endif
37 
38 /*
39  * Architectures vary in how they handle caching for addresses
40  * outside of main memory.
41  *
42  */
43 static inline int uncached_access(struct file *file, unsigned long addr)
44 {
45 #if defined(__i386__)
46 	/*
47 	 * On the PPro and successors, the MTRRs are used to set
48 	 * memory types for physical addresses outside main memory,
49 	 * so blindly setting PCD or PWT on those pages is wrong.
50 	 * For Pentiums and earlier, the surround logic should disable
51 	 * caching for the high addresses through the KEN pin, but
52 	 * we maintain the tradition of paranoia in this code.
53 	 */
54 	if (file->f_flags & O_SYNC)
55 		return 1;
56  	return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
57 		  test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
58 		  test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
59 		  test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
60 	  && addr >= __pa(high_memory);
61 #elif defined(__x86_64__)
62 	/*
63 	 * This is broken because it can generate memory type aliases,
64 	 * which can cause cache corruptions
65 	 * But it is only available for root and we have to be bug-to-bug
66 	 * compatible with i386.
67 	 */
68 	if (file->f_flags & O_SYNC)
69 		return 1;
70 	/* same behaviour as i386. PAT always set to cached and MTRRs control the
71 	   caching behaviour.
72 	   Hopefully a full PAT implementation will fix that soon. */
73 	return 0;
74 #elif defined(CONFIG_IA64)
75 	/*
76 	 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
77 	 */
78 	return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
79 #else
80 	/*
81 	 * Accessing memory above the top the kernel knows about or through a file pointer
82 	 * that was marked O_SYNC will be done non-cached.
83 	 */
84 	if (file->f_flags & O_SYNC)
85 		return 1;
86 	return addr >= __pa(high_memory);
87 #endif
88 }
89 
90 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
91 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
92 {
93 	if (addr + count > __pa(high_memory))
94 		return 0;
95 
96 	return 1;
97 }
98 
99 static inline int valid_mmap_phys_addr_range(unsigned long addr, size_t size)
100 {
101 	return 1;
102 }
103 #endif
104 
105 /*
106  * This funcion reads the *physical* memory. The f_pos points directly to the
107  * memory location.
108  */
109 static ssize_t read_mem(struct file * file, char __user * buf,
110 			size_t count, loff_t *ppos)
111 {
112 	unsigned long p = *ppos;
113 	ssize_t read, sz;
114 	char *ptr;
115 
116 	if (!valid_phys_addr_range(p, count))
117 		return -EFAULT;
118 	read = 0;
119 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
120 	/* we don't have page 0 mapped on sparc and m68k.. */
121 	if (p < PAGE_SIZE) {
122 		sz = PAGE_SIZE - p;
123 		if (sz > count)
124 			sz = count;
125 		if (sz > 0) {
126 			if (clear_user(buf, sz))
127 				return -EFAULT;
128 			buf += sz;
129 			p += sz;
130 			count -= sz;
131 			read += sz;
132 		}
133 	}
134 #endif
135 
136 	while (count > 0) {
137 		/*
138 		 * Handle first page in case it's not aligned
139 		 */
140 		if (-p & (PAGE_SIZE - 1))
141 			sz = -p & (PAGE_SIZE - 1);
142 		else
143 			sz = PAGE_SIZE;
144 
145 		sz = min_t(unsigned long, sz, count);
146 
147 		/*
148 		 * On ia64 if a page has been mapped somewhere as
149 		 * uncached, then it must also be accessed uncached
150 		 * by the kernel or data corruption may occur
151 		 */
152 		ptr = xlate_dev_mem_ptr(p);
153 
154 		if (copy_to_user(buf, ptr, sz))
155 			return -EFAULT;
156 		buf += sz;
157 		p += sz;
158 		count -= sz;
159 		read += sz;
160 	}
161 
162 	*ppos += read;
163 	return read;
164 }
165 
166 static ssize_t write_mem(struct file * file, const char __user * buf,
167 			 size_t count, loff_t *ppos)
168 {
169 	unsigned long p = *ppos;
170 	ssize_t written, sz;
171 	unsigned long copied;
172 	void *ptr;
173 
174 	if (!valid_phys_addr_range(p, count))
175 		return -EFAULT;
176 
177 	written = 0;
178 
179 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
180 	/* we don't have page 0 mapped on sparc and m68k.. */
181 	if (p < PAGE_SIZE) {
182 		unsigned long sz = PAGE_SIZE - p;
183 		if (sz > count)
184 			sz = count;
185 		/* Hmm. Do something? */
186 		buf += sz;
187 		p += sz;
188 		count -= sz;
189 		written += sz;
190 	}
191 #endif
192 
193 	while (count > 0) {
194 		/*
195 		 * Handle first page in case it's not aligned
196 		 */
197 		if (-p & (PAGE_SIZE - 1))
198 			sz = -p & (PAGE_SIZE - 1);
199 		else
200 			sz = PAGE_SIZE;
201 
202 		sz = min_t(unsigned long, sz, count);
203 
204 		/*
205 		 * On ia64 if a page has been mapped somewhere as
206 		 * uncached, then it must also be accessed uncached
207 		 * by the kernel or data corruption may occur
208 		 */
209 		ptr = xlate_dev_mem_ptr(p);
210 
211 		copied = copy_from_user(ptr, buf, sz);
212 		if (copied) {
213 			written += sz - copied;
214 			if (written)
215 				break;
216 			return -EFAULT;
217 		}
218 		buf += sz;
219 		p += sz;
220 		count -= sz;
221 		written += sz;
222 	}
223 
224 	*ppos += written;
225 	return written;
226 }
227 
228 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
229 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
230 				     unsigned long size, pgprot_t vma_prot)
231 {
232 #ifdef pgprot_noncached
233 	unsigned long offset = pfn << PAGE_SHIFT;
234 
235 	if (uncached_access(file, offset))
236 		return pgprot_noncached(vma_prot);
237 #endif
238 	return vma_prot;
239 }
240 #endif
241 
242 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
243 {
244 	size_t size = vma->vm_end - vma->vm_start;
245 
246 	if (!valid_mmap_phys_addr_range(vma->vm_pgoff << PAGE_SHIFT, size))
247 		return -EINVAL;
248 
249 	vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
250 						 size,
251 						 vma->vm_page_prot);
252 
253 	/* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
254 	if (remap_pfn_range(vma,
255 			    vma->vm_start,
256 			    vma->vm_pgoff,
257 			    size,
258 			    vma->vm_page_prot))
259 		return -EAGAIN;
260 	return 0;
261 }
262 
263 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
264 {
265 	unsigned long pfn;
266 
267 	/* Turn a kernel-virtual address into a physical page frame */
268 	pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
269 
270 	/*
271 	 * RED-PEN: on some architectures there is more mapped memory
272 	 * than available in mem_map which pfn_valid checks
273 	 * for. Perhaps should add a new macro here.
274 	 *
275 	 * RED-PEN: vmalloc is not supported right now.
276 	 */
277 	if (!pfn_valid(pfn))
278 		return -EIO;
279 
280 	vma->vm_pgoff = pfn;
281 	return mmap_mem(file, vma);
282 }
283 
284 #ifdef CONFIG_CRASH_DUMP
285 /*
286  * Read memory corresponding to the old kernel.
287  */
288 static ssize_t read_oldmem(struct file *file, char __user *buf,
289 				size_t count, loff_t *ppos)
290 {
291 	unsigned long pfn, offset;
292 	size_t read = 0, csize;
293 	int rc = 0;
294 
295 	while (count) {
296 		pfn = *ppos / PAGE_SIZE;
297 		if (pfn > saved_max_pfn)
298 			return read;
299 
300 		offset = (unsigned long)(*ppos % PAGE_SIZE);
301 		if (count > PAGE_SIZE - offset)
302 			csize = PAGE_SIZE - offset;
303 		else
304 			csize = count;
305 
306 		rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
307 		if (rc < 0)
308 			return rc;
309 		buf += csize;
310 		*ppos += csize;
311 		read += csize;
312 		count -= csize;
313 	}
314 	return read;
315 }
316 #endif
317 
318 extern long vread(char *buf, char *addr, unsigned long count);
319 extern long vwrite(char *buf, char *addr, unsigned long count);
320 
321 /*
322  * This function reads the *virtual* memory as seen by the kernel.
323  */
324 static ssize_t read_kmem(struct file *file, char __user *buf,
325 			 size_t count, loff_t *ppos)
326 {
327 	unsigned long p = *ppos;
328 	ssize_t low_count, read, sz;
329 	char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
330 
331 	read = 0;
332 	if (p < (unsigned long) high_memory) {
333 		low_count = count;
334 		if (count > (unsigned long) high_memory - p)
335 			low_count = (unsigned long) high_memory - p;
336 
337 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
338 		/* we don't have page 0 mapped on sparc and m68k.. */
339 		if (p < PAGE_SIZE && low_count > 0) {
340 			size_t tmp = PAGE_SIZE - p;
341 			if (tmp > low_count) tmp = low_count;
342 			if (clear_user(buf, tmp))
343 				return -EFAULT;
344 			buf += tmp;
345 			p += tmp;
346 			read += tmp;
347 			low_count -= tmp;
348 			count -= tmp;
349 		}
350 #endif
351 		while (low_count > 0) {
352 			/*
353 			 * Handle first page in case it's not aligned
354 			 */
355 			if (-p & (PAGE_SIZE - 1))
356 				sz = -p & (PAGE_SIZE - 1);
357 			else
358 				sz = PAGE_SIZE;
359 
360 			sz = min_t(unsigned long, sz, low_count);
361 
362 			/*
363 			 * On ia64 if a page has been mapped somewhere as
364 			 * uncached, then it must also be accessed uncached
365 			 * by the kernel or data corruption may occur
366 			 */
367 			kbuf = xlate_dev_kmem_ptr((char *)p);
368 
369 			if (copy_to_user(buf, kbuf, sz))
370 				return -EFAULT;
371 			buf += sz;
372 			p += sz;
373 			read += sz;
374 			low_count -= sz;
375 			count -= sz;
376 		}
377 	}
378 
379 	if (count > 0) {
380 		kbuf = (char *)__get_free_page(GFP_KERNEL);
381 		if (!kbuf)
382 			return -ENOMEM;
383 		while (count > 0) {
384 			int len = count;
385 
386 			if (len > PAGE_SIZE)
387 				len = PAGE_SIZE;
388 			len = vread(kbuf, (char *)p, len);
389 			if (!len)
390 				break;
391 			if (copy_to_user(buf, kbuf, len)) {
392 				free_page((unsigned long)kbuf);
393 				return -EFAULT;
394 			}
395 			count -= len;
396 			buf += len;
397 			read += len;
398 			p += len;
399 		}
400 		free_page((unsigned long)kbuf);
401 	}
402  	*ppos = p;
403  	return read;
404 }
405 
406 
407 static inline ssize_t
408 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
409 	      size_t count, loff_t *ppos)
410 {
411 	ssize_t written, sz;
412 	unsigned long copied;
413 
414 	written = 0;
415 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
416 	/* we don't have page 0 mapped on sparc and m68k.. */
417 	if (realp < PAGE_SIZE) {
418 		unsigned long sz = PAGE_SIZE - realp;
419 		if (sz > count)
420 			sz = count;
421 		/* Hmm. Do something? */
422 		buf += sz;
423 		p += sz;
424 		realp += sz;
425 		count -= sz;
426 		written += sz;
427 	}
428 #endif
429 
430 	while (count > 0) {
431 		char *ptr;
432 		/*
433 		 * Handle first page in case it's not aligned
434 		 */
435 		if (-realp & (PAGE_SIZE - 1))
436 			sz = -realp & (PAGE_SIZE - 1);
437 		else
438 			sz = PAGE_SIZE;
439 
440 		sz = min_t(unsigned long, sz, count);
441 
442 		/*
443 		 * On ia64 if a page has been mapped somewhere as
444 		 * uncached, then it must also be accessed uncached
445 		 * by the kernel or data corruption may occur
446 		 */
447 		ptr = xlate_dev_kmem_ptr(p);
448 
449 		copied = copy_from_user(ptr, buf, sz);
450 		if (copied) {
451 			written += sz - copied;
452 			if (written)
453 				break;
454 			return -EFAULT;
455 		}
456 		buf += sz;
457 		p += sz;
458 		realp += sz;
459 		count -= sz;
460 		written += sz;
461 	}
462 
463 	*ppos += written;
464 	return written;
465 }
466 
467 
468 /*
469  * This function writes to the *virtual* memory as seen by the kernel.
470  */
471 static ssize_t write_kmem(struct file * file, const char __user * buf,
472 			  size_t count, loff_t *ppos)
473 {
474 	unsigned long p = *ppos;
475 	ssize_t wrote = 0;
476 	ssize_t virtr = 0;
477 	ssize_t written;
478 	char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
479 
480 	if (p < (unsigned long) high_memory) {
481 
482 		wrote = count;
483 		if (count > (unsigned long) high_memory - p)
484 			wrote = (unsigned long) high_memory - p;
485 
486 		written = do_write_kmem((void*)p, p, buf, wrote, ppos);
487 		if (written != wrote)
488 			return written;
489 		wrote = written;
490 		p += wrote;
491 		buf += wrote;
492 		count -= wrote;
493 	}
494 
495 	if (count > 0) {
496 		kbuf = (char *)__get_free_page(GFP_KERNEL);
497 		if (!kbuf)
498 			return wrote ? wrote : -ENOMEM;
499 		while (count > 0) {
500 			int len = count;
501 
502 			if (len > PAGE_SIZE)
503 				len = PAGE_SIZE;
504 			if (len) {
505 				written = copy_from_user(kbuf, buf, len);
506 				if (written) {
507 					if (wrote + virtr)
508 						break;
509 					free_page((unsigned long)kbuf);
510 					return -EFAULT;
511 				}
512 			}
513 			len = vwrite(kbuf, (char *)p, len);
514 			count -= len;
515 			buf += len;
516 			virtr += len;
517 			p += len;
518 		}
519 		free_page((unsigned long)kbuf);
520 	}
521 
522  	*ppos = p;
523  	return virtr + wrote;
524 }
525 
526 #if defined(CONFIG_ISA) || !defined(__mc68000__)
527 static ssize_t read_port(struct file * file, char __user * buf,
528 			 size_t count, loff_t *ppos)
529 {
530 	unsigned long i = *ppos;
531 	char __user *tmp = buf;
532 
533 	if (!access_ok(VERIFY_WRITE, buf, count))
534 		return -EFAULT;
535 	while (count-- > 0 && i < 65536) {
536 		if (__put_user(inb(i),tmp) < 0)
537 			return -EFAULT;
538 		i++;
539 		tmp++;
540 	}
541 	*ppos = i;
542 	return tmp-buf;
543 }
544 
545 static ssize_t write_port(struct file * file, const char __user * buf,
546 			  size_t count, loff_t *ppos)
547 {
548 	unsigned long i = *ppos;
549 	const char __user * tmp = buf;
550 
551 	if (!access_ok(VERIFY_READ,buf,count))
552 		return -EFAULT;
553 	while (count-- > 0 && i < 65536) {
554 		char c;
555 		if (__get_user(c, tmp)) {
556 			if (tmp > buf)
557 				break;
558 			return -EFAULT;
559 		}
560 		outb(c,i);
561 		i++;
562 		tmp++;
563 	}
564 	*ppos = i;
565 	return tmp-buf;
566 }
567 #endif
568 
569 static ssize_t read_null(struct file * file, char __user * buf,
570 			 size_t count, loff_t *ppos)
571 {
572 	return 0;
573 }
574 
575 static ssize_t write_null(struct file * file, const char __user * buf,
576 			  size_t count, loff_t *ppos)
577 {
578 	return count;
579 }
580 
581 #ifdef CONFIG_MMU
582 /*
583  * For fun, we are using the MMU for this.
584  */
585 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
586 {
587 	struct mm_struct *mm;
588 	struct vm_area_struct * vma;
589 	unsigned long addr=(unsigned long)buf;
590 
591 	mm = current->mm;
592 	/* Oops, this was forgotten before. -ben */
593 	down_read(&mm->mmap_sem);
594 
595 	/* For private mappings, just map in zero pages. */
596 	for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
597 		unsigned long count;
598 
599 		if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
600 			goto out_up;
601 		if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
602 			break;
603 		count = vma->vm_end - addr;
604 		if (count > size)
605 			count = size;
606 
607 		zap_page_range(vma, addr, count, NULL);
608         	zeromap_page_range(vma, addr, count, PAGE_COPY);
609 
610 		size -= count;
611 		buf += count;
612 		addr += count;
613 		if (size == 0)
614 			goto out_up;
615 	}
616 
617 	up_read(&mm->mmap_sem);
618 
619 	/* The shared case is hard. Let's do the conventional zeroing. */
620 	do {
621 		unsigned long unwritten = clear_user(buf, PAGE_SIZE);
622 		if (unwritten)
623 			return size + unwritten - PAGE_SIZE;
624 		cond_resched();
625 		buf += PAGE_SIZE;
626 		size -= PAGE_SIZE;
627 	} while (size);
628 
629 	return size;
630 out_up:
631 	up_read(&mm->mmap_sem);
632 	return size;
633 }
634 
635 static ssize_t read_zero(struct file * file, char __user * buf,
636 			 size_t count, loff_t *ppos)
637 {
638 	unsigned long left, unwritten, written = 0;
639 
640 	if (!count)
641 		return 0;
642 
643 	if (!access_ok(VERIFY_WRITE, buf, count))
644 		return -EFAULT;
645 
646 	left = count;
647 
648 	/* do we want to be clever? Arbitrary cut-off */
649 	if (count >= PAGE_SIZE*4) {
650 		unsigned long partial;
651 
652 		/* How much left of the page? */
653 		partial = (PAGE_SIZE-1) & -(unsigned long) buf;
654 		unwritten = clear_user(buf, partial);
655 		written = partial - unwritten;
656 		if (unwritten)
657 			goto out;
658 		left -= partial;
659 		buf += partial;
660 		unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
661 		written += (left & PAGE_MASK) - unwritten;
662 		if (unwritten)
663 			goto out;
664 		buf += left & PAGE_MASK;
665 		left &= ~PAGE_MASK;
666 	}
667 	unwritten = clear_user(buf, left);
668 	written += left - unwritten;
669 out:
670 	return written ? written : -EFAULT;
671 }
672 
673 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
674 {
675 	if (vma->vm_flags & VM_SHARED)
676 		return shmem_zero_setup(vma);
677 	if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
678 		return -EAGAIN;
679 	return 0;
680 }
681 #else /* CONFIG_MMU */
682 static ssize_t read_zero(struct file * file, char * buf,
683 			 size_t count, loff_t *ppos)
684 {
685 	size_t todo = count;
686 
687 	while (todo) {
688 		size_t chunk = todo;
689 
690 		if (chunk > 4096)
691 			chunk = 4096;	/* Just for latency reasons */
692 		if (clear_user(buf, chunk))
693 			return -EFAULT;
694 		buf += chunk;
695 		todo -= chunk;
696 		cond_resched();
697 	}
698 	return count;
699 }
700 
701 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
702 {
703 	return -ENOSYS;
704 }
705 #endif /* CONFIG_MMU */
706 
707 static ssize_t write_full(struct file * file, const char __user * buf,
708 			  size_t count, loff_t *ppos)
709 {
710 	return -ENOSPC;
711 }
712 
713 /*
714  * Special lseek() function for /dev/null and /dev/zero.  Most notably, you
715  * can fopen() both devices with "a" now.  This was previously impossible.
716  * -- SRB.
717  */
718 
719 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
720 {
721 	return file->f_pos = 0;
722 }
723 
724 /*
725  * The memory devices use the full 32/64 bits of the offset, and so we cannot
726  * check against negative addresses: they are ok. The return value is weird,
727  * though, in that case (0).
728  *
729  * also note that seeking relative to the "end of file" isn't supported:
730  * it has no meaning, so it returns -EINVAL.
731  */
732 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
733 {
734 	loff_t ret;
735 
736 	mutex_lock(&file->f_dentry->d_inode->i_mutex);
737 	switch (orig) {
738 		case 0:
739 			file->f_pos = offset;
740 			ret = file->f_pos;
741 			force_successful_syscall_return();
742 			break;
743 		case 1:
744 			file->f_pos += offset;
745 			ret = file->f_pos;
746 			force_successful_syscall_return();
747 			break;
748 		default:
749 			ret = -EINVAL;
750 	}
751 	mutex_unlock(&file->f_dentry->d_inode->i_mutex);
752 	return ret;
753 }
754 
755 static int open_port(struct inode * inode, struct file * filp)
756 {
757 	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
758 }
759 
760 #define zero_lseek	null_lseek
761 #define full_lseek      null_lseek
762 #define write_zero	write_null
763 #define read_full       read_zero
764 #define open_mem	open_port
765 #define open_kmem	open_mem
766 #define open_oldmem	open_mem
767 
768 static struct file_operations mem_fops = {
769 	.llseek		= memory_lseek,
770 	.read		= read_mem,
771 	.write		= write_mem,
772 	.mmap		= mmap_mem,
773 	.open		= open_mem,
774 };
775 
776 static struct file_operations kmem_fops = {
777 	.llseek		= memory_lseek,
778 	.read		= read_kmem,
779 	.write		= write_kmem,
780 	.mmap		= mmap_kmem,
781 	.open		= open_kmem,
782 };
783 
784 static struct file_operations null_fops = {
785 	.llseek		= null_lseek,
786 	.read		= read_null,
787 	.write		= write_null,
788 };
789 
790 #if defined(CONFIG_ISA) || !defined(__mc68000__)
791 static struct file_operations port_fops = {
792 	.llseek		= memory_lseek,
793 	.read		= read_port,
794 	.write		= write_port,
795 	.open		= open_port,
796 };
797 #endif
798 
799 static struct file_operations zero_fops = {
800 	.llseek		= zero_lseek,
801 	.read		= read_zero,
802 	.write		= write_zero,
803 	.mmap		= mmap_zero,
804 };
805 
806 static struct backing_dev_info zero_bdi = {
807 	.capabilities	= BDI_CAP_MAP_COPY,
808 };
809 
810 static struct file_operations full_fops = {
811 	.llseek		= full_lseek,
812 	.read		= read_full,
813 	.write		= write_full,
814 };
815 
816 #ifdef CONFIG_CRASH_DUMP
817 static struct file_operations oldmem_fops = {
818 	.read	= read_oldmem,
819 	.open	= open_oldmem,
820 };
821 #endif
822 
823 static ssize_t kmsg_write(struct file * file, const char __user * buf,
824 			  size_t count, loff_t *ppos)
825 {
826 	char *tmp;
827 	ssize_t ret;
828 
829 	tmp = kmalloc(count + 1, GFP_KERNEL);
830 	if (tmp == NULL)
831 		return -ENOMEM;
832 	ret = -EFAULT;
833 	if (!copy_from_user(tmp, buf, count)) {
834 		tmp[count] = 0;
835 		ret = printk("%s", tmp);
836 		if (ret > count)
837 			/* printk can add a prefix */
838 			ret = count;
839 	}
840 	kfree(tmp);
841 	return ret;
842 }
843 
844 static struct file_operations kmsg_fops = {
845 	.write =	kmsg_write,
846 };
847 
848 static int memory_open(struct inode * inode, struct file * filp)
849 {
850 	switch (iminor(inode)) {
851 		case 1:
852 			filp->f_op = &mem_fops;
853 			break;
854 		case 2:
855 			filp->f_op = &kmem_fops;
856 			break;
857 		case 3:
858 			filp->f_op = &null_fops;
859 			break;
860 #if defined(CONFIG_ISA) || !defined(__mc68000__)
861 		case 4:
862 			filp->f_op = &port_fops;
863 			break;
864 #endif
865 		case 5:
866 			filp->f_mapping->backing_dev_info = &zero_bdi;
867 			filp->f_op = &zero_fops;
868 			break;
869 		case 7:
870 			filp->f_op = &full_fops;
871 			break;
872 		case 8:
873 			filp->f_op = &random_fops;
874 			break;
875 		case 9:
876 			filp->f_op = &urandom_fops;
877 			break;
878 		case 11:
879 			filp->f_op = &kmsg_fops;
880 			break;
881 #ifdef CONFIG_CRASH_DUMP
882 		case 12:
883 			filp->f_op = &oldmem_fops;
884 			break;
885 #endif
886 		default:
887 			return -ENXIO;
888 	}
889 	if (filp->f_op && filp->f_op->open)
890 		return filp->f_op->open(inode,filp);
891 	return 0;
892 }
893 
894 static struct file_operations memory_fops = {
895 	.open		= memory_open,	/* just a selector for the real open */
896 };
897 
898 static const struct {
899 	unsigned int		minor;
900 	char			*name;
901 	umode_t			mode;
902 	const struct file_operations	*fops;
903 } devlist[] = { /* list of minor devices */
904 	{1, "mem",     S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
905 	{2, "kmem",    S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
906 	{3, "null",    S_IRUGO | S_IWUGO,           &null_fops},
907 #if defined(CONFIG_ISA) || !defined(__mc68000__)
908 	{4, "port",    S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
909 #endif
910 	{5, "zero",    S_IRUGO | S_IWUGO,           &zero_fops},
911 	{7, "full",    S_IRUGO | S_IWUGO,           &full_fops},
912 	{8, "random",  S_IRUGO | S_IWUSR,           &random_fops},
913 	{9, "urandom", S_IRUGO | S_IWUSR,           &urandom_fops},
914 	{11,"kmsg",    S_IRUGO | S_IWUSR,           &kmsg_fops},
915 #ifdef CONFIG_CRASH_DUMP
916 	{12,"oldmem",    S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
917 #endif
918 };
919 
920 static struct class *mem_class;
921 
922 static int __init chr_dev_init(void)
923 {
924 	int i;
925 
926 	if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
927 		printk("unable to get major %d for memory devs\n", MEM_MAJOR);
928 
929 	mem_class = class_create(THIS_MODULE, "mem");
930 	for (i = 0; i < ARRAY_SIZE(devlist); i++) {
931 		class_device_create(mem_class, NULL,
932 					MKDEV(MEM_MAJOR, devlist[i].minor),
933 					NULL, devlist[i].name);
934 		devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
935 				S_IFCHR | devlist[i].mode, devlist[i].name);
936 	}
937 
938 	return 0;
939 }
940 
941 fs_initcall(chr_dev_init);
942