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 /* Does it even fit in phys_addr_t? */
347 if (offset
>> PAGE_SHIFT
!= vma
->vm_pgoff
)
350 /* It's illegal to wrap around the end of the physical address space. */
351 if (offset
+ (phys_addr_t
)size
- 1 < offset
)
354 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
357 if (!private_mapping_ok(vma
))
360 if (!range_is_allowed(vma
->vm_pgoff
, size
))
363 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
367 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
371 vma
->vm_ops
= &mmap_mem_ops
;
373 /* Remap-pfn-range will mark the range VM_IO */
374 if (remap_pfn_range(vma
,
378 vma
->vm_page_prot
)) {
384 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
388 /* Turn a kernel-virtual address into a physical page frame */
389 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
392 * RED-PEN: on some architectures there is more mapped memory than
393 * available in mem_map which pfn_valid checks for. Perhaps should add a
396 * RED-PEN: vmalloc is not supported right now.
402 return mmap_mem(file
, vma
);
406 * This function reads the *virtual* memory as seen by the kernel.
408 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
409 size_t count
, loff_t
*ppos
)
411 unsigned long p
= *ppos
;
412 ssize_t low_count
, read
, sz
;
413 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
417 if (p
< (unsigned long) high_memory
) {
419 if (count
> (unsigned long)high_memory
- p
)
420 low_count
= (unsigned long)high_memory
- p
;
422 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
423 /* we don't have page 0 mapped on sparc and m68k.. */
424 if (p
< PAGE_SIZE
&& low_count
> 0) {
425 sz
= size_inside_page(p
, low_count
);
426 if (clear_user(buf
, sz
))
435 while (low_count
> 0) {
436 sz
= size_inside_page(p
, low_count
);
439 * On ia64 if a page has been mapped somewhere as
440 * uncached, then it must also be accessed uncached
441 * by the kernel or data corruption may occur
443 kbuf
= xlate_dev_kmem_ptr((void *)p
);
444 if (!virt_addr_valid(kbuf
))
447 if (copy_to_user(buf
, kbuf
, sz
))
458 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
462 sz
= size_inside_page(p
, count
);
463 if (!is_vmalloc_or_module_addr((void *)p
)) {
467 sz
= vread(kbuf
, (char *)p
, sz
);
470 if (copy_to_user(buf
, kbuf
, sz
)) {
479 free_page((unsigned long)kbuf
);
482 return read
? read
: err
;
486 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
487 size_t count
, loff_t
*ppos
)
490 unsigned long copied
;
493 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
494 /* we don't have page 0 mapped on sparc and m68k.. */
496 sz
= size_inside_page(p
, count
);
497 /* Hmm. Do something? */
508 sz
= size_inside_page(p
, count
);
511 * On ia64 if a page has been mapped somewhere as uncached, then
512 * it must also be accessed uncached by the kernel or data
513 * corruption may occur.
515 ptr
= xlate_dev_kmem_ptr((void *)p
);
516 if (!virt_addr_valid(ptr
))
519 copied
= copy_from_user(ptr
, buf
, sz
);
521 written
+= sz
- copied
;
537 * This function writes to the *virtual* memory as seen by the kernel.
539 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
540 size_t count
, loff_t
*ppos
)
542 unsigned long p
= *ppos
;
545 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
548 if (p
< (unsigned long) high_memory
) {
549 unsigned long to_write
= min_t(unsigned long, count
,
550 (unsigned long)high_memory
- p
);
551 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
552 if (wrote
!= to_write
)
560 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
562 return wrote
? wrote
: -ENOMEM
;
564 unsigned long sz
= size_inside_page(p
, count
);
567 if (!is_vmalloc_or_module_addr((void *)p
)) {
571 n
= copy_from_user(kbuf
, buf
, sz
);
576 vwrite(kbuf
, (char *)p
, sz
);
582 free_page((unsigned long)kbuf
);
586 return virtr
+ wrote
? : err
;
589 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
590 size_t count
, loff_t
*ppos
)
592 unsigned long i
= *ppos
;
593 char __user
*tmp
= buf
;
595 if (!access_ok(VERIFY_WRITE
, buf
, count
))
597 while (count
-- > 0 && i
< 65536) {
598 if (__put_user(inb(i
), tmp
) < 0)
607 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
608 size_t count
, loff_t
*ppos
)
610 unsigned long i
= *ppos
;
611 const char __user
*tmp
= buf
;
613 if (!access_ok(VERIFY_READ
, buf
, count
))
615 while (count
-- > 0 && i
< 65536) {
618 if (__get_user(c
, tmp
)) {
631 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
632 size_t count
, loff_t
*ppos
)
637 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
638 size_t count
, loff_t
*ppos
)
643 static ssize_t
read_iter_null(struct kiocb
*iocb
, struct iov_iter
*to
)
648 static ssize_t
write_iter_null(struct kiocb
*iocb
, struct iov_iter
*from
)
650 size_t count
= iov_iter_count(from
);
651 iov_iter_advance(from
, count
);
655 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
656 struct splice_desc
*sd
)
661 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
662 loff_t
*ppos
, size_t len
, unsigned int flags
)
664 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
667 static ssize_t
read_iter_zero(struct kiocb
*iocb
, struct iov_iter
*iter
)
671 while (iov_iter_count(iter
)) {
672 size_t chunk
= iov_iter_count(iter
), n
;
674 if (chunk
> PAGE_SIZE
)
675 chunk
= PAGE_SIZE
; /* Just for latency reasons */
676 n
= iov_iter_zero(chunk
, iter
);
677 if (!n
&& iov_iter_count(iter
))
678 return written
? written
: -EFAULT
;
680 if (signal_pending(current
))
681 return written
? written
: -ERESTARTSYS
;
687 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
692 if (vma
->vm_flags
& VM_SHARED
)
693 return shmem_zero_setup(vma
);
697 static unsigned long get_unmapped_area_zero(struct file
*file
,
698 unsigned long addr
, unsigned long len
,
699 unsigned long pgoff
, unsigned long flags
)
702 if (flags
& MAP_SHARED
) {
704 * mmap_zero() will call shmem_zero_setup() to create a file,
705 * so use shmem's get_unmapped_area in case it can be huge;
706 * and pass NULL for file as in mmap.c's get_unmapped_area(),
707 * so as not to confuse shmem with our handle on "/dev/zero".
709 return shmem_get_unmapped_area(NULL
, addr
, len
, pgoff
, flags
);
712 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
713 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
719 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
720 size_t count
, loff_t
*ppos
)
726 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
727 * can fopen() both devices with "a" now. This was previously impossible.
730 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
732 return file
->f_pos
= 0;
736 * The memory devices use the full 32/64 bits of the offset, and so we cannot
737 * check against negative addresses: they are ok. The return value is weird,
738 * though, in that case (0).
740 * also note that seeking relative to the "end of file" isn't supported:
741 * it has no meaning, so it returns -EINVAL.
743 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
747 inode_lock(file_inode(file
));
750 offset
+= file
->f_pos
;
752 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
753 if ((unsigned long long)offset
>= -MAX_ERRNO
) {
757 file
->f_pos
= offset
;
759 force_successful_syscall_return();
764 inode_unlock(file_inode(file
));
768 static int open_port(struct inode
*inode
, struct file
*filp
)
770 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
773 #define zero_lseek null_lseek
774 #define full_lseek null_lseek
775 #define write_zero write_null
776 #define write_iter_zero write_iter_null
777 #define open_mem open_port
778 #define open_kmem open_mem
780 static const struct file_operations __maybe_unused mem_fops
= {
781 .llseek
= memory_lseek
,
787 .get_unmapped_area
= get_unmapped_area_mem
,
788 .mmap_capabilities
= memory_mmap_capabilities
,
792 static const struct file_operations __maybe_unused kmem_fops
= {
793 .llseek
= memory_lseek
,
799 .get_unmapped_area
= get_unmapped_area_mem
,
800 .mmap_capabilities
= memory_mmap_capabilities
,
804 static const struct file_operations null_fops
= {
805 .llseek
= null_lseek
,
808 .read_iter
= read_iter_null
,
809 .write_iter
= write_iter_null
,
810 .splice_write
= splice_write_null
,
813 static const struct file_operations __maybe_unused port_fops
= {
814 .llseek
= memory_lseek
,
820 static const struct file_operations zero_fops
= {
821 .llseek
= zero_lseek
,
823 .read_iter
= read_iter_zero
,
824 .write_iter
= write_iter_zero
,
826 .get_unmapped_area
= get_unmapped_area_zero
,
828 .mmap_capabilities
= zero_mmap_capabilities
,
832 static const struct file_operations full_fops
= {
833 .llseek
= full_lseek
,
834 .read_iter
= read_iter_zero
,
838 static const struct memdev
{
841 const struct file_operations
*fops
;
845 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
847 #ifdef CONFIG_DEVKMEM
848 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
850 [3] = { "null", 0666, &null_fops
, 0 },
851 #ifdef CONFIG_DEVPORT
852 [4] = { "port", 0, &port_fops
, 0 },
854 [5] = { "zero", 0666, &zero_fops
, 0 },
855 [7] = { "full", 0666, &full_fops
, 0 },
856 [8] = { "random", 0666, &random_fops
, 0 },
857 [9] = { "urandom", 0666, &urandom_fops
, 0 },
859 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
863 static int memory_open(struct inode
*inode
, struct file
*filp
)
866 const struct memdev
*dev
;
868 minor
= iminor(inode
);
869 if (minor
>= ARRAY_SIZE(devlist
))
872 dev
= &devlist
[minor
];
876 filp
->f_op
= dev
->fops
;
877 filp
->f_mode
|= dev
->fmode
;
880 return dev
->fops
->open(inode
, filp
);
885 static const struct file_operations memory_fops
= {
887 .llseek
= noop_llseek
,
890 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
892 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
893 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
897 static struct class *mem_class
;
899 static int __init
chr_dev_init(void)
903 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
904 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
906 mem_class
= class_create(THIS_MODULE
, "mem");
907 if (IS_ERR(mem_class
))
908 return PTR_ERR(mem_class
);
910 mem_class
->devnode
= mem_devnode
;
911 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
912 if (!devlist
[minor
].name
)
918 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
921 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
922 NULL
, devlist
[minor
].name
);
928 fs_initcall(chr_dev_init
);