2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
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/backing-dev.h>
25 #include <linux/shmem_fs.h>
26 #include <linux/splice.h>
27 #include <linux/pfn.h>
28 #include <linux/export.h>
30 #include <linux/uio.h>
32 #include <linux/uaccess.h>
35 # include <linux/efi.h>
38 #define DEVPORT_MINOR 4
40 static inline unsigned long size_inside_page(unsigned long start
,
45 sz
= PAGE_SIZE
- (start
& (PAGE_SIZE
- 1));
50 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
51 static inline int valid_phys_addr_range(phys_addr_t addr
, size_t count
)
53 return addr
+ count
<= __pa(high_memory
);
56 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
62 #ifdef CONFIG_STRICT_DEVMEM
63 static inline int page_is_allowed(unsigned long pfn
)
65 return devmem_is_allowed(pfn
);
67 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
69 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
74 if (!devmem_is_allowed(pfn
))
82 static inline int page_is_allowed(unsigned long pfn
)
86 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
92 #ifndef unxlate_dev_mem_ptr
93 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
94 void __weak
unxlate_dev_mem_ptr(phys_addr_t phys
, void *addr
)
100 * This funcion reads the *physical* memory. The f_pos points directly to the
103 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
104 size_t count
, loff_t
*ppos
)
106 phys_addr_t p
= *ppos
;
113 if (!valid_phys_addr_range(p
, count
))
116 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
117 /* we don't have page 0 mapped on sparc and m68k.. */
119 sz
= size_inside_page(p
, count
);
121 if (clear_user(buf
, sz
))
132 unsigned long remaining
;
135 sz
= size_inside_page(p
, count
);
137 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
141 /* Show zeros for restricted memory. */
142 remaining
= clear_user(buf
, sz
);
145 * On ia64 if a page has been mapped somewhere as
146 * uncached, then it must also be accessed uncached
147 * by the kernel or data corruption may occur.
149 ptr
= xlate_dev_mem_ptr(p
);
153 remaining
= copy_to_user(buf
, ptr
, sz
);
155 unxlate_dev_mem_ptr(p
, ptr
);
171 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
172 size_t count
, loff_t
*ppos
)
174 phys_addr_t p
= *ppos
;
176 unsigned long copied
;
182 if (!valid_phys_addr_range(p
, count
))
187 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
188 /* we don't have page 0 mapped on sparc and m68k.. */
190 sz
= size_inside_page(p
, count
);
191 /* Hmm. Do something? */
202 sz
= size_inside_page(p
, count
);
204 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
208 /* Skip actual writing when a page is marked as restricted. */
211 * On ia64 if a page has been mapped somewhere as
212 * uncached, then it must also be accessed uncached
213 * by the kernel or data corruption may occur.
215 ptr
= xlate_dev_mem_ptr(p
);
222 copied
= copy_from_user(ptr
, buf
, sz
);
223 unxlate_dev_mem_ptr(p
, ptr
);
225 written
+= sz
- copied
;
242 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
243 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
248 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
251 * Architectures vary in how they handle caching for addresses
252 * outside of main memory.
255 #ifdef pgprot_noncached
256 static int uncached_access(struct file
*file
, phys_addr_t addr
)
258 #if defined(CONFIG_IA64)
260 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
263 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
264 #elif defined(CONFIG_MIPS)
266 extern int __uncached_access(struct file
*file
,
269 return __uncached_access(file
, addr
);
273 * Accessing memory above the top the kernel knows about or through a
275 * that was marked O_DSYNC will be done non-cached.
277 if (file
->f_flags
& O_DSYNC
)
279 return addr
>= __pa(high_memory
);
284 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
285 unsigned long size
, pgprot_t vma_prot
)
287 #ifdef pgprot_noncached
288 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
290 if (uncached_access(file
, offset
))
291 return pgprot_noncached(vma_prot
);
298 static unsigned long get_unmapped_area_mem(struct file
*file
,
304 if (!valid_mmap_phys_addr_range(pgoff
, len
))
305 return (unsigned long) -EINVAL
;
306 return pgoff
<< PAGE_SHIFT
;
309 /* permit direct mmap, for read, write or exec */
310 static unsigned memory_mmap_capabilities(struct file
*file
)
312 return NOMMU_MAP_DIRECT
|
313 NOMMU_MAP_READ
| NOMMU_MAP_WRITE
| NOMMU_MAP_EXEC
;
316 static unsigned zero_mmap_capabilities(struct file
*file
)
318 return NOMMU_MAP_COPY
;
321 /* can't do an in-place private mapping if there's no MMU */
322 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
324 return vma
->vm_flags
& VM_MAYSHARE
;
328 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
334 static const struct vm_operations_struct mmap_mem_ops
= {
335 #ifdef CONFIG_HAVE_IOREMAP_PROT
336 .access
= generic_access_phys
340 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
342 size_t size
= vma
->vm_end
- vma
->vm_start
;
344 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
347 if (!private_mapping_ok(vma
))
350 if (!range_is_allowed(vma
->vm_pgoff
, size
))
353 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
357 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
361 vma
->vm_ops
= &mmap_mem_ops
;
363 /* Remap-pfn-range will mark the range VM_IO */
364 if (remap_pfn_range(vma
,
368 vma
->vm_page_prot
)) {
374 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
378 /* Turn a kernel-virtual address into a physical page frame */
379 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
382 * RED-PEN: on some architectures there is more mapped memory than
383 * available in mem_map which pfn_valid checks for. Perhaps should add a
386 * RED-PEN: vmalloc is not supported right now.
392 return mmap_mem(file
, vma
);
396 * This function reads the *virtual* memory as seen by the kernel.
398 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
399 size_t count
, loff_t
*ppos
)
401 unsigned long p
= *ppos
;
402 ssize_t low_count
, read
, sz
;
403 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
407 if (p
< (unsigned long) high_memory
) {
409 if (count
> (unsigned long)high_memory
- p
)
410 low_count
= (unsigned long)high_memory
- p
;
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 sz
= size_inside_page(p
, low_count
);
416 if (clear_user(buf
, sz
))
425 while (low_count
> 0) {
426 sz
= size_inside_page(p
, low_count
);
429 * On ia64 if a page has been mapped somewhere as
430 * uncached, then it must also be accessed uncached
431 * by the kernel or data corruption may occur
433 kbuf
= xlate_dev_kmem_ptr((void *)p
);
434 if (!virt_addr_valid(kbuf
))
437 if (copy_to_user(buf
, kbuf
, sz
))
448 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
452 sz
= size_inside_page(p
, count
);
453 if (!is_vmalloc_or_module_addr((void *)p
)) {
457 sz
= vread(kbuf
, (char *)p
, sz
);
460 if (copy_to_user(buf
, kbuf
, sz
)) {
469 free_page((unsigned long)kbuf
);
472 return read
? read
: err
;
476 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
477 size_t count
, loff_t
*ppos
)
480 unsigned long copied
;
483 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
484 /* we don't have page 0 mapped on sparc and m68k.. */
486 sz
= size_inside_page(p
, count
);
487 /* Hmm. Do something? */
498 sz
= size_inside_page(p
, count
);
501 * On ia64 if a page has been mapped somewhere as uncached, then
502 * it must also be accessed uncached by the kernel or data
503 * corruption may occur.
505 ptr
= xlate_dev_kmem_ptr((void *)p
);
506 if (!virt_addr_valid(ptr
))
509 copied
= copy_from_user(ptr
, buf
, sz
);
511 written
+= sz
- copied
;
527 * This function writes to the *virtual* memory as seen by the kernel.
529 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
530 size_t count
, loff_t
*ppos
)
532 unsigned long p
= *ppos
;
535 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
538 if (p
< (unsigned long) high_memory
) {
539 unsigned long to_write
= min_t(unsigned long, count
,
540 (unsigned long)high_memory
- p
);
541 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
542 if (wrote
!= to_write
)
550 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
552 return wrote
? wrote
: -ENOMEM
;
554 unsigned long sz
= size_inside_page(p
, count
);
557 if (!is_vmalloc_or_module_addr((void *)p
)) {
561 n
= copy_from_user(kbuf
, buf
, sz
);
566 vwrite(kbuf
, (char *)p
, sz
);
572 free_page((unsigned long)kbuf
);
576 return virtr
+ wrote
? : err
;
579 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
580 size_t count
, loff_t
*ppos
)
582 unsigned long i
= *ppos
;
583 char __user
*tmp
= buf
;
585 if (!access_ok(VERIFY_WRITE
, buf
, count
))
587 while (count
-- > 0 && i
< 65536) {
588 if (__put_user(inb(i
), tmp
) < 0)
597 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
598 size_t count
, loff_t
*ppos
)
600 unsigned long i
= *ppos
;
601 const char __user
*tmp
= buf
;
603 if (!access_ok(VERIFY_READ
, buf
, count
))
605 while (count
-- > 0 && i
< 65536) {
608 if (__get_user(c
, tmp
)) {
621 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
622 size_t count
, loff_t
*ppos
)
627 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
628 size_t count
, loff_t
*ppos
)
633 static ssize_t
read_iter_null(struct kiocb
*iocb
, struct iov_iter
*to
)
638 static ssize_t
write_iter_null(struct kiocb
*iocb
, struct iov_iter
*from
)
640 size_t count
= iov_iter_count(from
);
641 iov_iter_advance(from
, count
);
645 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
646 struct splice_desc
*sd
)
651 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
652 loff_t
*ppos
, size_t len
, unsigned int flags
)
654 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
657 static ssize_t
read_iter_zero(struct kiocb
*iocb
, struct iov_iter
*iter
)
661 while (iov_iter_count(iter
)) {
662 size_t chunk
= iov_iter_count(iter
), n
;
664 if (chunk
> PAGE_SIZE
)
665 chunk
= PAGE_SIZE
; /* Just for latency reasons */
666 n
= iov_iter_zero(chunk
, iter
);
667 if (!n
&& iov_iter_count(iter
))
668 return written
? written
: -EFAULT
;
670 if (signal_pending(current
))
671 return written
? written
: -ERESTARTSYS
;
677 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
682 if (vma
->vm_flags
& VM_SHARED
)
683 return shmem_zero_setup(vma
);
687 static unsigned long get_unmapped_area_zero(struct file
*file
,
688 unsigned long addr
, unsigned long len
,
689 unsigned long pgoff
, unsigned long flags
)
692 if (flags
& MAP_SHARED
) {
694 * mmap_zero() will call shmem_zero_setup() to create a file,
695 * so use shmem's get_unmapped_area in case it can be huge;
696 * and pass NULL for file as in mmap.c's get_unmapped_area(),
697 * so as not to confuse shmem with our handle on "/dev/zero".
699 return shmem_get_unmapped_area(NULL
, addr
, len
, pgoff
, flags
);
702 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
703 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
709 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
710 size_t count
, loff_t
*ppos
)
716 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
717 * can fopen() both devices with "a" now. This was previously impossible.
720 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
722 return file
->f_pos
= 0;
726 * The memory devices use the full 32/64 bits of the offset, and so we cannot
727 * check against negative addresses: they are ok. The return value is weird,
728 * though, in that case (0).
730 * also note that seeking relative to the "end of file" isn't supported:
731 * it has no meaning, so it returns -EINVAL.
733 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
737 inode_lock(file_inode(file
));
740 offset
+= file
->f_pos
;
742 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
743 if ((unsigned long long)offset
>= -MAX_ERRNO
) {
747 file
->f_pos
= offset
;
749 force_successful_syscall_return();
754 inode_unlock(file_inode(file
));
758 static int open_port(struct inode
*inode
, struct file
*filp
)
760 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
763 #define zero_lseek null_lseek
764 #define full_lseek null_lseek
765 #define write_zero write_null
766 #define write_iter_zero write_iter_null
767 #define open_mem open_port
768 #define open_kmem open_mem
770 static const struct file_operations __maybe_unused mem_fops
= {
771 .llseek
= memory_lseek
,
777 .get_unmapped_area
= get_unmapped_area_mem
,
778 .mmap_capabilities
= memory_mmap_capabilities
,
782 static const struct file_operations __maybe_unused kmem_fops
= {
783 .llseek
= memory_lseek
,
789 .get_unmapped_area
= get_unmapped_area_mem
,
790 .mmap_capabilities
= memory_mmap_capabilities
,
794 static const struct file_operations null_fops
= {
795 .llseek
= null_lseek
,
798 .read_iter
= read_iter_null
,
799 .write_iter
= write_iter_null
,
800 .splice_write
= splice_write_null
,
803 static const struct file_operations __maybe_unused port_fops
= {
804 .llseek
= memory_lseek
,
810 static const struct file_operations zero_fops
= {
811 .llseek
= zero_lseek
,
813 .read_iter
= read_iter_zero
,
814 .write_iter
= write_iter_zero
,
816 .get_unmapped_area
= get_unmapped_area_zero
,
818 .mmap_capabilities
= zero_mmap_capabilities
,
822 static const struct file_operations full_fops
= {
823 .llseek
= full_lseek
,
824 .read_iter
= read_iter_zero
,
828 static const struct memdev
{
831 const struct file_operations
*fops
;
835 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
837 #ifdef CONFIG_DEVKMEM
838 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
840 [3] = { "null", 0666, &null_fops
, 0 },
841 #ifdef CONFIG_DEVPORT
842 [4] = { "port", 0, &port_fops
, 0 },
844 [5] = { "zero", 0666, &zero_fops
, 0 },
845 [7] = { "full", 0666, &full_fops
, 0 },
846 [8] = { "random", 0666, &random_fops
, 0 },
847 [9] = { "urandom", 0666, &urandom_fops
, 0 },
849 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
853 static int memory_open(struct inode
*inode
, struct file
*filp
)
856 const struct memdev
*dev
;
858 minor
= iminor(inode
);
859 if (minor
>= ARRAY_SIZE(devlist
))
862 dev
= &devlist
[minor
];
866 filp
->f_op
= dev
->fops
;
867 filp
->f_mode
|= dev
->fmode
;
870 return dev
->fops
->open(inode
, filp
);
875 static const struct file_operations memory_fops
= {
877 .llseek
= noop_llseek
,
880 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
882 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
883 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
887 static struct class *mem_class
;
889 static int __init
chr_dev_init(void)
893 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
894 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
896 mem_class
= class_create(THIS_MODULE
, "mem");
897 if (IS_ERR(mem_class
))
898 return PTR_ERR(mem_class
);
900 mem_class
->devnode
= mem_devnode
;
901 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
902 if (!devlist
[minor
].name
)
908 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
911 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
912 NULL
, devlist
[minor
].name
);
918 fs_initcall(chr_dev_init
);