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
;
116 if (!valid_phys_addr_range(p
, count
))
119 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
120 /* we don't have page 0 mapped on sparc and m68k.. */
122 sz
= size_inside_page(p
, count
);
124 if (clear_user(buf
, sz
))
134 bounce
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
139 unsigned long remaining
;
142 sz
= size_inside_page(p
, count
);
145 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
151 /* Show zeros for restricted memory. */
152 remaining
= clear_user(buf
, sz
);
155 * On ia64 if a page has been mapped somewhere as
156 * uncached, then it must also be accessed uncached
157 * by the kernel or data corruption may occur.
159 ptr
= xlate_dev_mem_ptr(p
);
163 err
= probe_kernel_read(bounce
, ptr
, sz
);
164 unxlate_dev_mem_ptr(p
, ptr
);
168 remaining
= copy_to_user(buf
, bounce
, sz
);
189 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
190 size_t count
, loff_t
*ppos
)
192 phys_addr_t p
= *ppos
;
194 unsigned long copied
;
200 if (!valid_phys_addr_range(p
, count
))
205 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
206 /* we don't have page 0 mapped on sparc and m68k.. */
208 sz
= size_inside_page(p
, count
);
209 /* Hmm. Do something? */
220 sz
= size_inside_page(p
, count
);
222 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
226 /* Skip actual writing when a page is marked as restricted. */
229 * On ia64 if a page has been mapped somewhere as
230 * uncached, then it must also be accessed uncached
231 * by the kernel or data corruption may occur.
233 ptr
= xlate_dev_mem_ptr(p
);
240 copied
= copy_from_user(ptr
, buf
, sz
);
241 unxlate_dev_mem_ptr(p
, ptr
);
243 written
+= sz
- copied
;
260 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
261 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
266 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
269 * Architectures vary in how they handle caching for addresses
270 * outside of main memory.
273 #ifdef pgprot_noncached
274 static int uncached_access(struct file
*file
, phys_addr_t addr
)
276 #if defined(CONFIG_IA64)
278 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
281 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
282 #elif defined(CONFIG_MIPS)
284 extern int __uncached_access(struct file
*file
,
287 return __uncached_access(file
, addr
);
291 * Accessing memory above the top the kernel knows about or through a
293 * that was marked O_DSYNC will be done non-cached.
295 if (file
->f_flags
& O_DSYNC
)
297 return addr
>= __pa(high_memory
);
302 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
303 unsigned long size
, pgprot_t vma_prot
)
305 #ifdef pgprot_noncached
306 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
308 if (uncached_access(file
, offset
))
309 return pgprot_noncached(vma_prot
);
316 static unsigned long get_unmapped_area_mem(struct file
*file
,
322 if (!valid_mmap_phys_addr_range(pgoff
, len
))
323 return (unsigned long) -EINVAL
;
324 return pgoff
<< PAGE_SHIFT
;
327 /* permit direct mmap, for read, write or exec */
328 static unsigned memory_mmap_capabilities(struct file
*file
)
330 return NOMMU_MAP_DIRECT
|
331 NOMMU_MAP_READ
| NOMMU_MAP_WRITE
| NOMMU_MAP_EXEC
;
334 static unsigned zero_mmap_capabilities(struct file
*file
)
336 return NOMMU_MAP_COPY
;
339 /* can't do an in-place private mapping if there's no MMU */
340 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
342 return vma
->vm_flags
& VM_MAYSHARE
;
346 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
352 static const struct vm_operations_struct mmap_mem_ops
= {
353 #ifdef CONFIG_HAVE_IOREMAP_PROT
354 .access
= generic_access_phys
358 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
360 size_t size
= vma
->vm_end
- vma
->vm_start
;
361 phys_addr_t offset
= (phys_addr_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
363 /* Does it even fit in phys_addr_t? */
364 if (offset
>> PAGE_SHIFT
!= vma
->vm_pgoff
)
367 /* It's illegal to wrap around the end of the physical address space. */
368 if (offset
+ (phys_addr_t
)size
- 1 < offset
)
371 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
374 if (!private_mapping_ok(vma
))
377 if (!range_is_allowed(vma
->vm_pgoff
, size
))
380 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
384 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
388 vma
->vm_ops
= &mmap_mem_ops
;
390 /* Remap-pfn-range will mark the range VM_IO */
391 if (remap_pfn_range(vma
,
395 vma
->vm_page_prot
)) {
401 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
405 /* Turn a kernel-virtual address into a physical page frame */
406 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
409 * RED-PEN: on some architectures there is more mapped memory than
410 * available in mem_map which pfn_valid checks for. Perhaps should add a
413 * RED-PEN: vmalloc is not supported right now.
419 return mmap_mem(file
, vma
);
423 * This function reads the *virtual* memory as seen by the kernel.
425 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
426 size_t count
, loff_t
*ppos
)
428 unsigned long p
= *ppos
;
429 ssize_t low_count
, read
, sz
;
430 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
434 if (p
< (unsigned long) high_memory
) {
436 if (count
> (unsigned long)high_memory
- p
)
437 low_count
= (unsigned long)high_memory
- p
;
439 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
440 /* we don't have page 0 mapped on sparc and m68k.. */
441 if (p
< PAGE_SIZE
&& low_count
> 0) {
442 sz
= size_inside_page(p
, low_count
);
443 if (clear_user(buf
, sz
))
452 while (low_count
> 0) {
453 sz
= size_inside_page(p
, low_count
);
456 * On ia64 if a page has been mapped somewhere as
457 * uncached, then it must also be accessed uncached
458 * by the kernel or data corruption may occur
460 kbuf
= xlate_dev_kmem_ptr((void *)p
);
461 if (!virt_addr_valid(kbuf
))
464 if (copy_to_user(buf
, kbuf
, sz
))
475 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
479 sz
= size_inside_page(p
, count
);
480 if (!is_vmalloc_or_module_addr((void *)p
)) {
484 sz
= vread(kbuf
, (char *)p
, sz
);
487 if (copy_to_user(buf
, kbuf
, sz
)) {
496 free_page((unsigned long)kbuf
);
499 return read
? read
: err
;
503 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
504 size_t count
, loff_t
*ppos
)
507 unsigned long copied
;
510 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
511 /* we don't have page 0 mapped on sparc and m68k.. */
513 sz
= size_inside_page(p
, count
);
514 /* Hmm. Do something? */
525 sz
= size_inside_page(p
, count
);
528 * On ia64 if a page has been mapped somewhere as uncached, then
529 * it must also be accessed uncached by the kernel or data
530 * corruption may occur.
532 ptr
= xlate_dev_kmem_ptr((void *)p
);
533 if (!virt_addr_valid(ptr
))
536 copied
= copy_from_user(ptr
, buf
, sz
);
538 written
+= sz
- copied
;
554 * This function writes to the *virtual* memory as seen by the kernel.
556 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
557 size_t count
, loff_t
*ppos
)
559 unsigned long p
= *ppos
;
562 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
565 if (p
< (unsigned long) high_memory
) {
566 unsigned long to_write
= min_t(unsigned long, count
,
567 (unsigned long)high_memory
- p
);
568 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
569 if (wrote
!= to_write
)
577 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
579 return wrote
? wrote
: -ENOMEM
;
581 unsigned long sz
= size_inside_page(p
, count
);
584 if (!is_vmalloc_or_module_addr((void *)p
)) {
588 n
= copy_from_user(kbuf
, buf
, sz
);
593 vwrite(kbuf
, (char *)p
, sz
);
599 free_page((unsigned long)kbuf
);
603 return virtr
+ wrote
? : err
;
606 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
607 size_t count
, loff_t
*ppos
)
609 unsigned long i
= *ppos
;
610 char __user
*tmp
= buf
;
612 if (!access_ok(VERIFY_WRITE
, buf
, count
))
614 while (count
-- > 0 && i
< 65536) {
615 if (__put_user(inb(i
), tmp
) < 0)
624 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
625 size_t count
, loff_t
*ppos
)
627 unsigned long i
= *ppos
;
628 const char __user
*tmp
= buf
;
630 if (!access_ok(VERIFY_READ
, buf
, count
))
632 while (count
-- > 0 && i
< 65536) {
635 if (__get_user(c
, tmp
)) {
648 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
649 size_t count
, loff_t
*ppos
)
654 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
655 size_t count
, loff_t
*ppos
)
660 static ssize_t
read_iter_null(struct kiocb
*iocb
, struct iov_iter
*to
)
665 static ssize_t
write_iter_null(struct kiocb
*iocb
, struct iov_iter
*from
)
667 size_t count
= iov_iter_count(from
);
668 iov_iter_advance(from
, count
);
672 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
673 struct splice_desc
*sd
)
678 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
679 loff_t
*ppos
, size_t len
, unsigned int flags
)
681 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
684 static ssize_t
read_iter_zero(struct kiocb
*iocb
, struct iov_iter
*iter
)
688 while (iov_iter_count(iter
)) {
689 size_t chunk
= iov_iter_count(iter
), n
;
691 if (chunk
> PAGE_SIZE
)
692 chunk
= PAGE_SIZE
; /* Just for latency reasons */
693 n
= iov_iter_zero(chunk
, iter
);
694 if (!n
&& iov_iter_count(iter
))
695 return written
? written
: -EFAULT
;
697 if (signal_pending(current
))
698 return written
? written
: -ERESTARTSYS
;
704 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
709 if (vma
->vm_flags
& VM_SHARED
)
710 return shmem_zero_setup(vma
);
714 static unsigned long get_unmapped_area_zero(struct file
*file
,
715 unsigned long addr
, unsigned long len
,
716 unsigned long pgoff
, unsigned long flags
)
719 if (flags
& MAP_SHARED
) {
721 * mmap_zero() will call shmem_zero_setup() to create a file,
722 * so use shmem's get_unmapped_area in case it can be huge;
723 * and pass NULL for file as in mmap.c's get_unmapped_area(),
724 * so as not to confuse shmem with our handle on "/dev/zero".
726 return shmem_get_unmapped_area(NULL
, addr
, len
, pgoff
, flags
);
729 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
730 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
736 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
737 size_t count
, loff_t
*ppos
)
743 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
744 * can fopen() both devices with "a" now. This was previously impossible.
747 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
749 return file
->f_pos
= 0;
753 * The memory devices use the full 32/64 bits of the offset, and so we cannot
754 * check against negative addresses: they are ok. The return value is weird,
755 * though, in that case (0).
757 * also note that seeking relative to the "end of file" isn't supported:
758 * it has no meaning, so it returns -EINVAL.
760 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
764 inode_lock(file_inode(file
));
767 offset
+= file
->f_pos
;
769 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
770 if ((unsigned long long)offset
>= -MAX_ERRNO
) {
774 file
->f_pos
= offset
;
776 force_successful_syscall_return();
781 inode_unlock(file_inode(file
));
785 static int open_port(struct inode
*inode
, struct file
*filp
)
787 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
790 #define zero_lseek null_lseek
791 #define full_lseek null_lseek
792 #define write_zero write_null
793 #define write_iter_zero write_iter_null
794 #define open_mem open_port
795 #define open_kmem open_mem
797 static const struct file_operations __maybe_unused mem_fops
= {
798 .llseek
= memory_lseek
,
804 .get_unmapped_area
= get_unmapped_area_mem
,
805 .mmap_capabilities
= memory_mmap_capabilities
,
809 static const struct file_operations __maybe_unused kmem_fops
= {
810 .llseek
= memory_lseek
,
816 .get_unmapped_area
= get_unmapped_area_mem
,
817 .mmap_capabilities
= memory_mmap_capabilities
,
821 static const struct file_operations null_fops
= {
822 .llseek
= null_lseek
,
825 .read_iter
= read_iter_null
,
826 .write_iter
= write_iter_null
,
827 .splice_write
= splice_write_null
,
830 static const struct file_operations __maybe_unused port_fops
= {
831 .llseek
= memory_lseek
,
837 static const struct file_operations zero_fops
= {
838 .llseek
= zero_lseek
,
840 .read_iter
= read_iter_zero
,
841 .write_iter
= write_iter_zero
,
843 .get_unmapped_area
= get_unmapped_area_zero
,
845 .mmap_capabilities
= zero_mmap_capabilities
,
849 static const struct file_operations full_fops
= {
850 .llseek
= full_lseek
,
851 .read_iter
= read_iter_zero
,
855 static const struct memdev
{
858 const struct file_operations
*fops
;
862 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
864 #ifdef CONFIG_DEVKMEM
865 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
867 [3] = { "null", 0666, &null_fops
, 0 },
868 #ifdef CONFIG_DEVPORT
869 [4] = { "port", 0, &port_fops
, 0 },
871 [5] = { "zero", 0666, &zero_fops
, 0 },
872 [7] = { "full", 0666, &full_fops
, 0 },
873 [8] = { "random", 0666, &random_fops
, 0 },
874 [9] = { "urandom", 0666, &urandom_fops
, 0 },
876 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
880 static int memory_open(struct inode
*inode
, struct file
*filp
)
883 const struct memdev
*dev
;
885 minor
= iminor(inode
);
886 if (minor
>= ARRAY_SIZE(devlist
))
889 dev
= &devlist
[minor
];
893 filp
->f_op
= dev
->fops
;
894 filp
->f_mode
|= dev
->fmode
;
897 return dev
->fops
->open(inode
, filp
);
902 static const struct file_operations memory_fops
= {
904 .llseek
= noop_llseek
,
907 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
909 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
910 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
914 static struct class *mem_class
;
916 static int __init
chr_dev_init(void)
920 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
921 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
923 mem_class
= class_create(THIS_MODULE
, "mem");
924 if (IS_ERR(mem_class
))
925 return PTR_ERR(mem_class
);
927 mem_class
->devnode
= mem_devnode
;
928 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
929 if (!devlist
[minor
].name
)
935 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
938 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
939 NULL
, devlist
[minor
].name
);
945 fs_initcall(chr_dev_init
);