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
)
99 static inline bool should_stop_iteration(void)
103 return fatal_signal_pending(current
);
107 * This funcion reads the *physical* memory. The f_pos points directly to the
110 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
111 size_t count
, loff_t
*ppos
)
113 phys_addr_t p
= *ppos
;
120 if (!valid_phys_addr_range(p
, count
))
123 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
124 /* we don't have page 0 mapped on sparc and m68k.. */
126 sz
= size_inside_page(p
, count
);
128 if (clear_user(buf
, sz
))
139 unsigned long remaining
;
142 sz
= size_inside_page(p
, count
);
144 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
148 /* Show zeros for restricted memory. */
149 remaining
= clear_user(buf
, sz
);
152 * On ia64 if a page has been mapped somewhere as
153 * uncached, then it must also be accessed uncached
154 * by the kernel or data corruption may occur.
156 ptr
= xlate_dev_mem_ptr(p
);
160 remaining
= copy_to_user(buf
, ptr
, sz
);
162 unxlate_dev_mem_ptr(p
, ptr
);
172 if (should_stop_iteration())
180 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
181 size_t count
, loff_t
*ppos
)
183 phys_addr_t p
= *ppos
;
185 unsigned long copied
;
191 if (!valid_phys_addr_range(p
, count
))
196 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
197 /* we don't have page 0 mapped on sparc and m68k.. */
199 sz
= size_inside_page(p
, count
);
200 /* Hmm. Do something? */
211 sz
= size_inside_page(p
, count
);
213 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
217 /* Skip actual writing when a page is marked as restricted. */
220 * On ia64 if a page has been mapped somewhere as
221 * uncached, then it must also be accessed uncached
222 * by the kernel or data corruption may occur.
224 ptr
= xlate_dev_mem_ptr(p
);
231 copied
= copy_from_user(ptr
, buf
, sz
);
232 unxlate_dev_mem_ptr(p
, ptr
);
234 written
+= sz
- copied
;
245 if (should_stop_iteration())
253 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
254 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
259 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
262 * Architectures vary in how they handle caching for addresses
263 * outside of main memory.
266 #ifdef pgprot_noncached
267 static int uncached_access(struct file
*file
, phys_addr_t addr
)
269 #if defined(CONFIG_IA64)
271 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
274 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
275 #elif defined(CONFIG_MIPS)
277 extern int __uncached_access(struct file
*file
,
280 return __uncached_access(file
, addr
);
284 * Accessing memory above the top the kernel knows about or through a
286 * that was marked O_DSYNC will be done non-cached.
288 if (file
->f_flags
& O_DSYNC
)
290 return addr
>= __pa(high_memory
);
295 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
296 unsigned long size
, pgprot_t vma_prot
)
298 #ifdef pgprot_noncached
299 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
301 if (uncached_access(file
, offset
))
302 return pgprot_noncached(vma_prot
);
309 static unsigned long get_unmapped_area_mem(struct file
*file
,
315 if (!valid_mmap_phys_addr_range(pgoff
, len
))
316 return (unsigned long) -EINVAL
;
317 return pgoff
<< PAGE_SHIFT
;
320 /* permit direct mmap, for read, write or exec */
321 static unsigned memory_mmap_capabilities(struct file
*file
)
323 return NOMMU_MAP_DIRECT
|
324 NOMMU_MAP_READ
| NOMMU_MAP_WRITE
| NOMMU_MAP_EXEC
;
327 static unsigned zero_mmap_capabilities(struct file
*file
)
329 return NOMMU_MAP_COPY
;
332 /* can't do an in-place private mapping if there's no MMU */
333 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
335 return vma
->vm_flags
& VM_MAYSHARE
;
339 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
345 static const struct vm_operations_struct mmap_mem_ops
= {
346 #ifdef CONFIG_HAVE_IOREMAP_PROT
347 .access
= generic_access_phys
351 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
353 size_t size
= vma
->vm_end
- vma
->vm_start
;
354 phys_addr_t offset
= (phys_addr_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
356 /* It's illegal to wrap around the end of the physical address space. */
357 if (offset
+ (phys_addr_t
)size
- 1 < offset
)
360 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
363 if (!private_mapping_ok(vma
))
366 if (!range_is_allowed(vma
->vm_pgoff
, size
))
369 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
373 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
377 vma
->vm_ops
= &mmap_mem_ops
;
379 /* Remap-pfn-range will mark the range VM_IO */
380 if (remap_pfn_range(vma
,
384 vma
->vm_page_prot
)) {
390 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
394 /* Turn a kernel-virtual address into a physical page frame */
395 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
398 * RED-PEN: on some architectures there is more mapped memory than
399 * available in mem_map which pfn_valid checks for. Perhaps should add a
402 * RED-PEN: vmalloc is not supported right now.
408 return mmap_mem(file
, vma
);
412 * This function reads the *virtual* memory as seen by the kernel.
414 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
415 size_t count
, loff_t
*ppos
)
417 unsigned long p
= *ppos
;
418 ssize_t low_count
, read
, sz
;
419 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
423 if (p
< (unsigned long) high_memory
) {
425 if (count
> (unsigned long)high_memory
- p
)
426 low_count
= (unsigned long)high_memory
- p
;
428 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
429 /* we don't have page 0 mapped on sparc and m68k.. */
430 if (p
< PAGE_SIZE
&& low_count
> 0) {
431 sz
= size_inside_page(p
, low_count
);
432 if (clear_user(buf
, sz
))
441 while (low_count
> 0) {
442 sz
= size_inside_page(p
, low_count
);
445 * On ia64 if a page has been mapped somewhere as
446 * uncached, then it must also be accessed uncached
447 * by the kernel or data corruption may occur
449 kbuf
= xlate_dev_kmem_ptr((void *)p
);
450 if (!virt_addr_valid(kbuf
))
453 if (copy_to_user(buf
, kbuf
, sz
))
460 if (should_stop_iteration()) {
468 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
472 sz
= size_inside_page(p
, count
);
473 if (!is_vmalloc_or_module_addr((void *)p
)) {
477 sz
= vread(kbuf
, (char *)p
, sz
);
480 if (copy_to_user(buf
, kbuf
, sz
)) {
488 if (should_stop_iteration())
491 free_page((unsigned long)kbuf
);
494 return read
? read
: err
;
498 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
499 size_t count
, loff_t
*ppos
)
502 unsigned long copied
;
505 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
506 /* we don't have page 0 mapped on sparc and m68k.. */
508 sz
= size_inside_page(p
, count
);
509 /* Hmm. Do something? */
520 sz
= size_inside_page(p
, count
);
523 * On ia64 if a page has been mapped somewhere as uncached, then
524 * it must also be accessed uncached by the kernel or data
525 * corruption may occur.
527 ptr
= xlate_dev_kmem_ptr((void *)p
);
528 if (!virt_addr_valid(ptr
))
531 copied
= copy_from_user(ptr
, buf
, sz
);
533 written
+= sz
- copied
;
542 if (should_stop_iteration())
551 * This function writes to the *virtual* memory as seen by the kernel.
553 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
554 size_t count
, loff_t
*ppos
)
556 unsigned long p
= *ppos
;
559 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
562 if (p
< (unsigned long) high_memory
) {
563 unsigned long to_write
= min_t(unsigned long, count
,
564 (unsigned long)high_memory
- p
);
565 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
566 if (wrote
!= to_write
)
574 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
576 return wrote
? wrote
: -ENOMEM
;
578 unsigned long sz
= size_inside_page(p
, count
);
581 if (!is_vmalloc_or_module_addr((void *)p
)) {
585 n
= copy_from_user(kbuf
, buf
, sz
);
590 vwrite(kbuf
, (char *)p
, sz
);
595 if (should_stop_iteration())
598 free_page((unsigned long)kbuf
);
602 return virtr
+ wrote
? : err
;
605 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
606 size_t count
, loff_t
*ppos
)
608 unsigned long i
= *ppos
;
609 char __user
*tmp
= buf
;
611 if (!access_ok(VERIFY_WRITE
, buf
, count
))
613 while (count
-- > 0 && i
< 65536) {
614 if (__put_user(inb(i
), tmp
) < 0)
623 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
624 size_t count
, loff_t
*ppos
)
626 unsigned long i
= *ppos
;
627 const char __user
*tmp
= buf
;
629 if (!access_ok(VERIFY_READ
, buf
, count
))
631 while (count
-- > 0 && i
< 65536) {
634 if (__get_user(c
, tmp
)) {
647 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
648 size_t count
, loff_t
*ppos
)
653 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
654 size_t count
, loff_t
*ppos
)
659 static ssize_t
read_iter_null(struct kiocb
*iocb
, struct iov_iter
*to
)
664 static ssize_t
write_iter_null(struct kiocb
*iocb
, struct iov_iter
*from
)
666 size_t count
= iov_iter_count(from
);
667 iov_iter_advance(from
, count
);
671 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
672 struct splice_desc
*sd
)
677 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
678 loff_t
*ppos
, size_t len
, unsigned int flags
)
680 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
683 static ssize_t
read_iter_zero(struct kiocb
*iocb
, struct iov_iter
*iter
)
687 while (iov_iter_count(iter
)) {
688 size_t chunk
= iov_iter_count(iter
), n
;
690 if (chunk
> PAGE_SIZE
)
691 chunk
= PAGE_SIZE
; /* Just for latency reasons */
692 n
= iov_iter_zero(chunk
, iter
);
693 if (!n
&& iov_iter_count(iter
))
694 return written
? written
: -EFAULT
;
696 if (signal_pending(current
))
697 return written
? written
: -ERESTARTSYS
;
703 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
708 if (vma
->vm_flags
& VM_SHARED
)
709 return shmem_zero_setup(vma
);
713 static unsigned long get_unmapped_area_zero(struct file
*file
,
714 unsigned long addr
, unsigned long len
,
715 unsigned long pgoff
, unsigned long flags
)
718 if (flags
& MAP_SHARED
) {
720 * mmap_zero() will call shmem_zero_setup() to create a file,
721 * so use shmem's get_unmapped_area in case it can be huge;
722 * and pass NULL for file as in mmap.c's get_unmapped_area(),
723 * so as not to confuse shmem with our handle on "/dev/zero".
725 return shmem_get_unmapped_area(NULL
, addr
, len
, pgoff
, flags
);
728 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
729 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
735 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
736 size_t count
, loff_t
*ppos
)
742 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
743 * can fopen() both devices with "a" now. This was previously impossible.
746 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
748 return file
->f_pos
= 0;
752 * The memory devices use the full 32/64 bits of the offset, and so we cannot
753 * check against negative addresses: they are ok. The return value is weird,
754 * though, in that case (0).
756 * also note that seeking relative to the "end of file" isn't supported:
757 * it has no meaning, so it returns -EINVAL.
759 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
763 inode_lock(file_inode(file
));
766 offset
+= file
->f_pos
;
768 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
769 if ((unsigned long long)offset
>= -MAX_ERRNO
) {
773 file
->f_pos
= offset
;
775 force_successful_syscall_return();
780 inode_unlock(file_inode(file
));
784 static int open_port(struct inode
*inode
, struct file
*filp
)
786 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
789 #define zero_lseek null_lseek
790 #define full_lseek null_lseek
791 #define write_zero write_null
792 #define write_iter_zero write_iter_null
793 #define open_mem open_port
794 #define open_kmem open_mem
796 static const struct file_operations __maybe_unused mem_fops
= {
797 .llseek
= memory_lseek
,
803 .get_unmapped_area
= get_unmapped_area_mem
,
804 .mmap_capabilities
= memory_mmap_capabilities
,
808 static const struct file_operations __maybe_unused kmem_fops
= {
809 .llseek
= memory_lseek
,
815 .get_unmapped_area
= get_unmapped_area_mem
,
816 .mmap_capabilities
= memory_mmap_capabilities
,
820 static const struct file_operations null_fops
= {
821 .llseek
= null_lseek
,
824 .read_iter
= read_iter_null
,
825 .write_iter
= write_iter_null
,
826 .splice_write
= splice_write_null
,
829 static const struct file_operations __maybe_unused port_fops
= {
830 .llseek
= memory_lseek
,
836 static const struct file_operations zero_fops
= {
837 .llseek
= zero_lseek
,
839 .read_iter
= read_iter_zero
,
840 .write_iter
= write_iter_zero
,
842 .get_unmapped_area
= get_unmapped_area_zero
,
844 .mmap_capabilities
= zero_mmap_capabilities
,
848 static const struct file_operations full_fops
= {
849 .llseek
= full_lseek
,
850 .read_iter
= read_iter_zero
,
854 static const struct memdev
{
857 const struct file_operations
*fops
;
861 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
863 #ifdef CONFIG_DEVKMEM
864 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
866 [3] = { "null", 0666, &null_fops
, 0 },
867 #ifdef CONFIG_DEVPORT
868 [4] = { "port", 0, &port_fops
, 0 },
870 [5] = { "zero", 0666, &zero_fops
, 0 },
871 [7] = { "full", 0666, &full_fops
, 0 },
872 [8] = { "random", 0666, &random_fops
, 0 },
873 [9] = { "urandom", 0666, &urandom_fops
, 0 },
875 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
879 static int memory_open(struct inode
*inode
, struct file
*filp
)
882 const struct memdev
*dev
;
884 minor
= iminor(inode
);
885 if (minor
>= ARRAY_SIZE(devlist
))
888 dev
= &devlist
[minor
];
892 filp
->f_op
= dev
->fops
;
893 filp
->f_mode
|= dev
->fmode
;
896 return dev
->fops
->open(inode
, filp
);
901 static const struct file_operations memory_fops
= {
903 .llseek
= noop_llseek
,
906 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
908 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
909 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
913 static struct class *mem_class
;
915 static int __init
chr_dev_init(void)
919 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
920 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
922 mem_class
= class_create(THIS_MODULE
, "mem");
923 if (IS_ERR(mem_class
))
924 return PTR_ERR(mem_class
);
926 mem_class
->devnode
= mem_devnode
;
927 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
928 if (!devlist
[minor
].name
)
934 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
937 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
938 NULL
, devlist
[minor
].name
);
944 fs_initcall(chr_dev_init
);