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