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