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

Archive Download this file



interactive