1 // SPDX-License-Identifier: GPL-2.0
3 * linux/drivers/char/mem.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
9 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
13 #include <linux/miscdevice.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/mman.h>
17 #include <linux/random.h>
18 #include <linux/init.h>
19 #include <linux/raw.h>
20 #include <linux/tty.h>
21 #include <linux/capability.h>
22 #include <linux/ptrace.h>
23 #include <linux/device.h>
24 #include <linux/highmem.h>
25 #include <linux/backing-dev.h>
26 #include <linux/shmem_fs.h>
27 #include <linux/splice.h>
28 #include <linux/pfn.h>
29 #include <linux/export.h>
31 #include <linux/uio.h>
33 #include <linux/uaccess.h>
36 # include <linux/efi.h>
39 #define DEVPORT_MINOR 4
41 static inline unsigned long size_inside_page(unsigned long start
,
46 sz
= PAGE_SIZE
- (start
& (PAGE_SIZE
- 1));
51 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
52 static inline int valid_phys_addr_range(phys_addr_t addr
, size_t count
)
54 return addr
+ count
<= __pa(high_memory
);
57 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
63 #ifdef CONFIG_STRICT_DEVMEM
64 static inline int page_is_allowed(unsigned long pfn
)
66 return devmem_is_allowed(pfn
);
68 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
70 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
75 if (!devmem_is_allowed(pfn
))
83 static inline int page_is_allowed(unsigned long pfn
)
87 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
93 #ifndef unxlate_dev_mem_ptr
94 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
95 void __weak
unxlate_dev_mem_ptr(phys_addr_t phys
, void *addr
)
101 * This funcion reads the *physical* memory. The f_pos points directly to the
104 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
105 size_t count
, loff_t
*ppos
)
107 phys_addr_t p
= *ppos
;
114 if (!valid_phys_addr_range(p
, count
))
117 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
118 /* we don't have page 0 mapped on sparc and m68k.. */
120 sz
= size_inside_page(p
, count
);
122 if (clear_user(buf
, sz
))
133 unsigned long remaining
;
136 sz
= size_inside_page(p
, count
);
138 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
142 /* Show zeros for restricted memory. */
143 remaining
= clear_user(buf
, sz
);
146 * On ia64 if a page has been mapped somewhere as
147 * uncached, then it must also be accessed uncached
148 * by the kernel or data corruption may occur.
150 ptr
= xlate_dev_mem_ptr(p
);
154 remaining
= copy_to_user(buf
, ptr
, sz
);
156 unxlate_dev_mem_ptr(p
, ptr
);
172 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
173 size_t count
, loff_t
*ppos
)
175 phys_addr_t p
= *ppos
;
177 unsigned long copied
;
183 if (!valid_phys_addr_range(p
, count
))
188 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
189 /* we don't have page 0 mapped on sparc and m68k.. */
191 sz
= size_inside_page(p
, count
);
192 /* Hmm. Do something? */
203 sz
= size_inside_page(p
, count
);
205 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
209 /* Skip actual writing when a page is marked as restricted. */
212 * On ia64 if a page has been mapped somewhere as
213 * uncached, then it must also be accessed uncached
214 * by the kernel or data corruption may occur.
216 ptr
= xlate_dev_mem_ptr(p
);
223 copied
= copy_from_user(ptr
, buf
, sz
);
224 unxlate_dev_mem_ptr(p
, ptr
);
226 written
+= sz
- copied
;
243 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
244 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
249 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
252 * Architectures vary in how they handle caching for addresses
253 * outside of main memory.
256 #ifdef pgprot_noncached
257 static int uncached_access(struct file
*file
, phys_addr_t addr
)
259 #if defined(CONFIG_IA64)
261 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
264 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
265 #elif defined(CONFIG_MIPS)
267 extern int __uncached_access(struct file
*file
,
270 return __uncached_access(file
, addr
);
274 * Accessing memory above the top the kernel knows about or through a
276 * that was marked O_DSYNC will be done non-cached.
278 if (file
->f_flags
& O_DSYNC
)
280 return addr
>= __pa(high_memory
);
285 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
286 unsigned long size
, pgprot_t vma_prot
)
288 #ifdef pgprot_noncached
289 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
291 if (uncached_access(file
, offset
))
292 return pgprot_noncached(vma_prot
);
299 static unsigned long get_unmapped_area_mem(struct file
*file
,
305 if (!valid_mmap_phys_addr_range(pgoff
, len
))
306 return (unsigned long) -EINVAL
;
307 return pgoff
<< PAGE_SHIFT
;
310 /* permit direct mmap, for read, write or exec */
311 static unsigned memory_mmap_capabilities(struct file
*file
)
313 return NOMMU_MAP_DIRECT
|
314 NOMMU_MAP_READ
| NOMMU_MAP_WRITE
| NOMMU_MAP_EXEC
;
317 static unsigned zero_mmap_capabilities(struct file
*file
)
319 return NOMMU_MAP_COPY
;
322 /* can't do an in-place private mapping if there's no MMU */
323 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
325 return vma
->vm_flags
& VM_MAYSHARE
;
329 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
335 static const struct vm_operations_struct mmap_mem_ops
= {
336 #ifdef CONFIG_HAVE_IOREMAP_PROT
337 .access
= generic_access_phys
341 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
343 size_t size
= vma
->vm_end
- vma
->vm_start
;
344 phys_addr_t offset
= (phys_addr_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
346 /* It's illegal to wrap around the end of the physical address space. */
347 if (offset
+ (phys_addr_t
)size
- 1 < offset
)
350 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
353 if (!private_mapping_ok(vma
))
356 if (!range_is_allowed(vma
->vm_pgoff
, size
))
359 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
363 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
367 vma
->vm_ops
= &mmap_mem_ops
;
369 /* Remap-pfn-range will mark the range VM_IO */
370 if (remap_pfn_range(vma
,
374 vma
->vm_page_prot
)) {
380 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
384 /* Turn a kernel-virtual address into a physical page frame */
385 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
388 * RED-PEN: on some architectures there is more mapped memory than
389 * available in mem_map which pfn_valid checks for. Perhaps should add a
392 * RED-PEN: vmalloc is not supported right now.
398 return mmap_mem(file
, vma
);
402 * This function reads the *virtual* memory as seen by the kernel.
404 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
405 size_t count
, loff_t
*ppos
)
407 unsigned long p
= *ppos
;
408 ssize_t low_count
, read
, sz
;
409 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
413 if (p
< (unsigned long) high_memory
) {
415 if (count
> (unsigned long)high_memory
- p
)
416 low_count
= (unsigned long)high_memory
- p
;
418 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
419 /* we don't have page 0 mapped on sparc and m68k.. */
420 if (p
< PAGE_SIZE
&& low_count
> 0) {
421 sz
= size_inside_page(p
, low_count
);
422 if (clear_user(buf
, sz
))
431 while (low_count
> 0) {
432 sz
= size_inside_page(p
, low_count
);
435 * On ia64 if a page has been mapped somewhere as
436 * uncached, then it must also be accessed uncached
437 * by the kernel or data corruption may occur
439 kbuf
= xlate_dev_kmem_ptr((void *)p
);
440 if (!virt_addr_valid(kbuf
))
443 if (copy_to_user(buf
, kbuf
, sz
))
454 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
458 sz
= size_inside_page(p
, count
);
459 if (!is_vmalloc_or_module_addr((void *)p
)) {
463 sz
= vread(kbuf
, (char *)p
, sz
);
466 if (copy_to_user(buf
, kbuf
, sz
)) {
475 free_page((unsigned long)kbuf
);
478 return read
? read
: err
;
482 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
483 size_t count
, loff_t
*ppos
)
486 unsigned long copied
;
489 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
490 /* we don't have page 0 mapped on sparc and m68k.. */
492 sz
= size_inside_page(p
, count
);
493 /* Hmm. Do something? */
504 sz
= size_inside_page(p
, count
);
507 * On ia64 if a page has been mapped somewhere as uncached, then
508 * it must also be accessed uncached by the kernel or data
509 * corruption may occur.
511 ptr
= xlate_dev_kmem_ptr((void *)p
);
512 if (!virt_addr_valid(ptr
))
515 copied
= copy_from_user(ptr
, buf
, sz
);
517 written
+= sz
- copied
;
533 * This function writes to the *virtual* memory as seen by the kernel.
535 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
536 size_t count
, loff_t
*ppos
)
538 unsigned long p
= *ppos
;
541 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
544 if (p
< (unsigned long) high_memory
) {
545 unsigned long to_write
= min_t(unsigned long, count
,
546 (unsigned long)high_memory
- p
);
547 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
548 if (wrote
!= to_write
)
556 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
558 return wrote
? wrote
: -ENOMEM
;
560 unsigned long sz
= size_inside_page(p
, count
);
563 if (!is_vmalloc_or_module_addr((void *)p
)) {
567 n
= copy_from_user(kbuf
, buf
, sz
);
572 vwrite(kbuf
, (char *)p
, sz
);
578 free_page((unsigned long)kbuf
);
582 return virtr
+ wrote
? : err
;
585 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
586 size_t count
, loff_t
*ppos
)
588 unsigned long i
= *ppos
;
589 char __user
*tmp
= buf
;
591 if (!access_ok(VERIFY_WRITE
, buf
, count
))
593 while (count
-- > 0 && i
< 65536) {
594 if (__put_user(inb(i
), tmp
) < 0)
603 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
604 size_t count
, loff_t
*ppos
)
606 unsigned long i
= *ppos
;
607 const char __user
*tmp
= buf
;
609 if (!access_ok(VERIFY_READ
, buf
, count
))
611 while (count
-- > 0 && i
< 65536) {
614 if (__get_user(c
, tmp
)) {
627 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
628 size_t count
, loff_t
*ppos
)
633 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
634 size_t count
, loff_t
*ppos
)
639 static ssize_t
read_iter_null(struct kiocb
*iocb
, struct iov_iter
*to
)
644 static ssize_t
write_iter_null(struct kiocb
*iocb
, struct iov_iter
*from
)
646 size_t count
= iov_iter_count(from
);
647 iov_iter_advance(from
, count
);
651 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
652 struct splice_desc
*sd
)
657 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
658 loff_t
*ppos
, size_t len
, unsigned int flags
)
660 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
663 static ssize_t
read_iter_zero(struct kiocb
*iocb
, struct iov_iter
*iter
)
667 while (iov_iter_count(iter
)) {
668 size_t chunk
= iov_iter_count(iter
), n
;
670 if (chunk
> PAGE_SIZE
)
671 chunk
= PAGE_SIZE
; /* Just for latency reasons */
672 n
= iov_iter_zero(chunk
, iter
);
673 if (!n
&& iov_iter_count(iter
))
674 return written
? written
: -EFAULT
;
676 if (signal_pending(current
))
677 return written
? written
: -ERESTARTSYS
;
683 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
688 if (vma
->vm_flags
& VM_SHARED
)
689 return shmem_zero_setup(vma
);
693 static unsigned long get_unmapped_area_zero(struct file
*file
,
694 unsigned long addr
, unsigned long len
,
695 unsigned long pgoff
, unsigned long flags
)
698 if (flags
& MAP_SHARED
) {
700 * mmap_zero() will call shmem_zero_setup() to create a file,
701 * so use shmem's get_unmapped_area in case it can be huge;
702 * and pass NULL for file as in mmap.c's get_unmapped_area(),
703 * so as not to confuse shmem with our handle on "/dev/zero".
705 return shmem_get_unmapped_area(NULL
, addr
, len
, pgoff
, flags
);
708 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
709 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
715 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
716 size_t count
, loff_t
*ppos
)
722 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
723 * can fopen() both devices with "a" now. This was previously impossible.
726 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
728 return file
->f_pos
= 0;
732 * The memory devices use the full 32/64 bits of the offset, and so we cannot
733 * check against negative addresses: they are ok. The return value is weird,
734 * though, in that case (0).
736 * also note that seeking relative to the "end of file" isn't supported:
737 * it has no meaning, so it returns -EINVAL.
739 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
743 inode_lock(file_inode(file
));
746 offset
+= file
->f_pos
;
748 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
749 if ((unsigned long long)offset
>= -MAX_ERRNO
) {
753 file
->f_pos
= offset
;
755 force_successful_syscall_return();
760 inode_unlock(file_inode(file
));
764 static int open_port(struct inode
*inode
, struct file
*filp
)
766 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
769 #define zero_lseek null_lseek
770 #define full_lseek null_lseek
771 #define write_zero write_null
772 #define write_iter_zero write_iter_null
773 #define open_mem open_port
774 #define open_kmem open_mem
776 static const struct file_operations __maybe_unused mem_fops
= {
777 .llseek
= memory_lseek
,
783 .get_unmapped_area
= get_unmapped_area_mem
,
784 .mmap_capabilities
= memory_mmap_capabilities
,
788 static const struct file_operations __maybe_unused kmem_fops
= {
789 .llseek
= memory_lseek
,
795 .get_unmapped_area
= get_unmapped_area_mem
,
796 .mmap_capabilities
= memory_mmap_capabilities
,
800 static const struct file_operations null_fops
= {
801 .llseek
= null_lseek
,
804 .read_iter
= read_iter_null
,
805 .write_iter
= write_iter_null
,
806 .splice_write
= splice_write_null
,
809 static const struct file_operations __maybe_unused port_fops
= {
810 .llseek
= memory_lseek
,
816 static const struct file_operations zero_fops
= {
817 .llseek
= zero_lseek
,
819 .read_iter
= read_iter_zero
,
820 .write_iter
= write_iter_zero
,
822 .get_unmapped_area
= get_unmapped_area_zero
,
824 .mmap_capabilities
= zero_mmap_capabilities
,
828 static const struct file_operations full_fops
= {
829 .llseek
= full_lseek
,
830 .read_iter
= read_iter_zero
,
834 static const struct memdev
{
837 const struct file_operations
*fops
;
841 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
843 #ifdef CONFIG_DEVKMEM
844 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
846 [3] = { "null", 0666, &null_fops
, 0 },
847 #ifdef CONFIG_DEVPORT
848 [4] = { "port", 0, &port_fops
, 0 },
850 [5] = { "zero", 0666, &zero_fops
, 0 },
851 [7] = { "full", 0666, &full_fops
, 0 },
852 [8] = { "random", 0666, &random_fops
, 0 },
853 [9] = { "urandom", 0666, &urandom_fops
, 0 },
855 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
859 static int memory_open(struct inode
*inode
, struct file
*filp
)
862 const struct memdev
*dev
;
864 minor
= iminor(inode
);
865 if (minor
>= ARRAY_SIZE(devlist
))
868 dev
= &devlist
[minor
];
872 filp
->f_op
= dev
->fops
;
873 filp
->f_mode
|= dev
->fmode
;
876 return dev
->fops
->open(inode
, filp
);
881 static const struct file_operations memory_fops
= {
883 .llseek
= noop_llseek
,
886 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
888 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
889 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
893 static struct class *mem_class
;
895 static int __init
chr_dev_init(void)
899 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
900 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
902 mem_class
= class_create(THIS_MODULE
, "mem");
903 if (IS_ERR(mem_class
))
904 return PTR_ERR(mem_class
);
906 mem_class
->devnode
= mem_devnode
;
907 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
908 if (!devlist
[minor
].name
)
914 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
917 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
918 NULL
, devlist
[minor
].name
);
924 fs_initcall(chr_dev_init
);