ia64/linux-2.6.18-xen.hg

view drivers/char/mem.c @ 893:f994bfe9b93b

linux/blktap2: reduce TLB flush scope

c/s 885 added very coarse TLB flushing. Since these flushes always
follow single page updates, single page flushes (when available) are
sufficient.

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